OSDN Git Service

comment
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  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 "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR,
110   /* a transaction expression */
111   NIC_TRANSACTION
112 } non_integral_constant;
113
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error {
116   /* NULL */
117   NLE_NULL,
118   /* is not a type */
119   NLE_TYPE,
120   /* is not a class or namespace */
121   NLE_CXX98,
122   /* is not a class, namespace, or enumeration */
123   NLE_NOT_CXX98
124 } name_lookup_error;
125
126 /* The various kinds of required token */
127 typedef enum required_token {
128   RT_NONE,
129   RT_SEMICOLON,  /* ';' */
130   RT_OPEN_PAREN, /* '(' */
131   RT_CLOSE_BRACE, /* '}' */
132   RT_OPEN_BRACE,  /* '{' */
133   RT_CLOSE_SQUARE, /* ']' */
134   RT_OPEN_SQUARE,  /* '[' */
135   RT_COMMA, /* ',' */
136   RT_SCOPE, /* '::' */
137   RT_LESS, /* '<' */
138   RT_GREATER, /* '>' */
139   RT_EQ, /* '=' */
140   RT_ELLIPSIS, /* '...' */
141   RT_MULT, /* '*' */
142   RT_COMPL, /* '~' */
143   RT_COLON, /* ':' */
144   RT_COLON_SCOPE, /* ':' or '::' */
145   RT_CLOSE_PAREN, /* ')' */
146   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147   RT_PRAGMA_EOL, /* end of line */
148   RT_NAME, /* identifier */
149
150   /* The type is CPP_KEYWORD */
151   RT_NEW, /* new */
152   RT_DELETE, /* delete */
153   RT_RETURN, /* return */
154   RT_WHILE, /* while */
155   RT_EXTERN, /* extern */
156   RT_STATIC_ASSERT, /* static_assert */
157   RT_DECLTYPE, /* decltype */
158   RT_OPERATOR, /* operator */
159   RT_CLASS, /* class */
160   RT_TEMPLATE, /* template */
161   RT_NAMESPACE, /* namespace */
162   RT_USING, /* using */
163   RT_ASM, /* asm */
164   RT_TRY, /* try */
165   RT_CATCH, /* catch */
166   RT_THROW, /* throw */
167   RT_LABEL, /* __label__ */
168   RT_AT_TRY, /* @try */
169   RT_AT_SYNCHRONIZED, /* @synchronized */
170   RT_AT_THROW, /* @throw */
171
172   RT_SELECT,  /* selection-statement */
173   RT_INTERATION, /* iteration-statement */
174   RT_JUMP, /* jump-statement */
175   RT_CLASS_KEY, /* class-key */
176   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179   RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 } required_token;
181
182 /* Prototypes.  */
183
184 static cp_lexer *cp_lexer_new_main
185   (void);
186 static cp_lexer *cp_lexer_new_from_tokens
187   (cp_token_cache *tokens);
188 static void cp_lexer_destroy
189   (cp_lexer *);
190 static int cp_lexer_saving_tokens
191   (const cp_lexer *);
192 static cp_token *cp_lexer_token_at
193   (cp_lexer *, cp_token_position);
194 static void cp_lexer_get_preprocessor_token
195   (cp_lexer *, cp_token *);
196 static inline cp_token *cp_lexer_peek_token
197   (cp_lexer *);
198 static cp_token *cp_lexer_peek_nth_token
199   (cp_lexer *, size_t);
200 static inline bool cp_lexer_next_token_is
201   (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_not
203   (cp_lexer *, enum cpp_ttype);
204 static bool cp_lexer_next_token_is_keyword
205   (cp_lexer *, enum rid);
206 static cp_token *cp_lexer_consume_token
207   (cp_lexer *);
208 static void cp_lexer_purge_token
209   (cp_lexer *);
210 static void cp_lexer_purge_tokens_after
211   (cp_lexer *, cp_token_position);
212 static void cp_lexer_save_tokens
213   (cp_lexer *);
214 static void cp_lexer_commit_tokens
215   (cp_lexer *);
216 static void cp_lexer_rollback_tokens
217   (cp_lexer *);
218 static void cp_lexer_print_token
219   (FILE *, cp_token *);
220 static inline bool cp_lexer_debugging_p
221   (cp_lexer *);
222 static void cp_lexer_start_debugging
223   (cp_lexer *) ATTRIBUTE_UNUSED;
224 static void cp_lexer_stop_debugging
225   (cp_lexer *) ATTRIBUTE_UNUSED;
226
227 static cp_token_cache *cp_token_cache_new
228   (cp_token *, cp_token *);
229
230 static void cp_parser_initial_pragma
231   (cp_token *);
232
233 static tree cp_literal_operator_id
234   (const char *);
235
236 /* Manifest constants.  */
237 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
238 #define CP_SAVED_TOKEN_STACK 5
239
240 /* Variables.  */
241
242 /* The stream to which debugging output should be written.  */
243 static FILE *cp_lexer_debug_stream;
244
245 /* Nonzero if we are parsing an unevaluated operand: an operand to
246    sizeof, typeof, or alignof.  */
247 int cp_unevaluated_operand;
248
249 /* Dump up to NUM tokens in BUFFER to FILE starting with token
250    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
251    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
252    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
253    highlighted by surrounding it in [[ ]].  */
254
255 static void
256 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
257                       cp_token *start_token, unsigned num,
258                       cp_token *curr_token)
259 {
260   unsigned i, nprinted;
261   cp_token *token;
262   bool do_print;
263
264   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265
266   if (buffer == NULL)
267     return;
268
269   if (num == 0)
270     num = VEC_length (cp_token, buffer);
271
272   if (start_token == NULL)
273     start_token = VEC_address (cp_token, buffer);
274
275   if (start_token > VEC_address (cp_token, buffer))
276     {
277       cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
278       fprintf (file, " ... ");
279     }
280
281   do_print = false;
282   nprinted = 0;
283   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284     {
285       if (token == start_token)
286         do_print = true;
287
288       if (!do_print)
289         continue;
290
291       nprinted++;
292       if (token == curr_token)
293         fprintf (file, "[[");
294
295       cp_lexer_print_token (file, token);
296
297       if (token == curr_token)
298         fprintf (file, "]]");
299
300       switch (token->type)
301         {
302           case CPP_SEMICOLON:
303           case CPP_OPEN_BRACE:
304           case CPP_CLOSE_BRACE:
305           case CPP_EOF:
306             fputc ('\n', file);
307             break;
308
309           default:
310             fputc (' ', file);
311         }
312     }
313
314   if (i == num && i < VEC_length (cp_token, buffer))
315     {
316       fprintf (file, " ... ");
317       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
318                             VEC_length (cp_token, buffer) - 1));
319     }
320
321   fprintf (file, "\n");
322 }
323
324
325 /* Dump all tokens in BUFFER to stderr.  */
326
327 void
328 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
329 {
330   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 }
332
333
334 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
335    description for T.  */
336
337 static void
338 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
339 {
340   if (t)
341     {
342       fprintf (file, "%s: ", desc);
343       print_node_brief (file, "", t, 0);
344     }
345 }
346
347
348 /* Dump parser context C to FILE.  */
349
350 static void
351 cp_debug_print_context (FILE *file, cp_parser_context *c)
352 {
353   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
354   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
355   print_node_brief (file, "", c->object_type, 0);
356   fprintf (file, "}\n");
357 }
358
359
360 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
361
362 static void
363 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
364 {
365   unsigned i;
366   cp_parser_context *c;
367
368   fprintf (file, "Parsing context stack:\n");
369   for (i = 0, c = first; c; c = c->next, i++)
370     {
371       fprintf (file, "\t#%u: ", i);
372       cp_debug_print_context (file, c);
373     }
374 }
375
376
377 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
378
379 static void
380 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
381 {
382   if (flag)
383     fprintf (file, "%s: true\n", desc);
384 }
385
386
387 /* Print an unparsed function entry UF to FILE.  */
388
389 static void
390 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
391 {
392   unsigned i;
393   cp_default_arg_entry *default_arg_fn;
394   tree fn;
395
396   fprintf (file, "\tFunctions with default args:\n");
397   for (i = 0;
398        VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
399                     default_arg_fn);
400        i++)
401     {
402       fprintf (file, "\t\tClass type: ");
403       print_node_brief (file, "", default_arg_fn->class_type, 0);
404       fprintf (file, "\t\tDeclaration: ");
405       print_node_brief (file, "", default_arg_fn->decl, 0);
406       fprintf (file, "\n");
407     }
408
409   fprintf (file, "\n\tFunctions with definitions that require "
410            "post-processing\n\t\t");
411   for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
412     {
413       print_node_brief (file, "", fn, 0);
414       fprintf (file, " ");
415     }
416   fprintf (file, "\n");
417
418   fprintf (file, "\n\tNon-static data members with initializers that require "
419            "post-processing\n\t\t");
420   for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
421     {
422       print_node_brief (file, "", fn, 0);
423       fprintf (file, " ");
424     }
425   fprintf (file, "\n");
426 }
427
428
429 /* Print the stack of unparsed member functions S to FILE.  */
430
431 static void
432 cp_debug_print_unparsed_queues (FILE *file,
433                                 VEC(cp_unparsed_functions_entry, gc) *s)
434 {
435   unsigned i;
436   cp_unparsed_functions_entry *uf;
437
438   fprintf (file, "Unparsed functions\n");
439   for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
440     {
441       fprintf (file, "#%u:\n", i);
442       cp_debug_print_unparsed_function (file, uf);
443     }
444 }
445
446
447 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
448    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
449
450 static void
451 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
452 {
453   cp_token *next_token, *first_token, *start_token;
454
455   if (file == NULL)
456     file = stderr;
457
458   next_token = parser->lexer->next_token;
459   first_token = VEC_address (cp_token, parser->lexer->buffer);
460   start_token = (next_token > first_token + window_size / 2)
461                 ? next_token - window_size / 2
462                 : first_token;
463   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
464                         next_token);
465 }
466
467
468 /* Dump debugging information for the given PARSER.  If FILE is NULL,
469    the output is printed on stderr.  */
470
471 void
472 cp_debug_parser (FILE *file, cp_parser *parser)
473 {
474   const size_t window_size = 20;
475   cp_token *token;
476   expanded_location eloc;
477
478   if (file == NULL)
479     file = stderr;
480
481   fprintf (file, "Parser state\n\n");
482   fprintf (file, "Number of tokens: %u\n",
483            VEC_length (cp_token, parser->lexer->buffer));
484   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
485   cp_debug_print_tree_if_set (file, "Object scope",
486                                      parser->object_scope);
487   cp_debug_print_tree_if_set (file, "Qualifying scope",
488                                      parser->qualifying_scope);
489   cp_debug_print_context_stack (file, parser->context);
490   cp_debug_print_flag (file, "Allow GNU extensions",
491                               parser->allow_gnu_extensions_p);
492   cp_debug_print_flag (file, "'>' token is greater-than",
493                               parser->greater_than_is_operator_p);
494   cp_debug_print_flag (file, "Default args allowed in current "
495                               "parameter list", parser->default_arg_ok_p);
496   cp_debug_print_flag (file, "Parsing integral constant-expression",
497                               parser->integral_constant_expression_p);
498   cp_debug_print_flag (file, "Allow non-constant expression in current "
499                               "constant-expression",
500                               parser->allow_non_integral_constant_expression_p);
501   cp_debug_print_flag (file, "Seen non-constant expression",
502                               parser->non_integral_constant_expression_p);
503   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
504                               "current context",
505                               parser->local_variables_forbidden_p);
506   cp_debug_print_flag (file, "In unbraced linkage specification",
507                               parser->in_unbraced_linkage_specification_p);
508   cp_debug_print_flag (file, "Parsing a declarator",
509                               parser->in_declarator_p);
510   cp_debug_print_flag (file, "In template argument list",
511                               parser->in_template_argument_list_p);
512   cp_debug_print_flag (file, "Parsing an iteration statement",
513                               parser->in_statement & IN_ITERATION_STMT);
514   cp_debug_print_flag (file, "Parsing a switch statement",
515                               parser->in_statement & IN_SWITCH_STMT);
516   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
517                               parser->in_statement & IN_OMP_BLOCK);
518   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
519                               parser->in_statement & IN_OMP_FOR);
520   cp_debug_print_flag (file, "Parsing an if statement",
521                               parser->in_statement & IN_IF_STMT);
522   cp_debug_print_flag (file, "Parsing a type-id in an expression "
523                               "context", parser->in_type_id_in_expr_p);
524   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
525                               parser->implicit_extern_c);
526   cp_debug_print_flag (file, "String expressions should be translated "
527                               "to execution character set",
528                               parser->translate_strings_p);
529   cp_debug_print_flag (file, "Parsing function body outside of a "
530                               "local class", parser->in_function_body);
531   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
532                               parser->colon_corrects_to_scope_p);
533   if (parser->type_definition_forbidden_message)
534     fprintf (file, "Error message for forbidden type definitions: %s\n",
535              parser->type_definition_forbidden_message);
536   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
537   fprintf (file, "Number of class definitions in progress: %u\n",
538            parser->num_classes_being_defined);
539   fprintf (file, "Number of template parameter lists for the current "
540            "declaration: %u\n", parser->num_template_parameter_lists);
541   cp_debug_parser_tokens (file, parser, window_size);
542   token = parser->lexer->next_token;
543   fprintf (file, "Next token to parse:\n");
544   fprintf (file, "\tToken:  ");
545   cp_lexer_print_token (file, token);
546   eloc = expand_location (token->location);
547   fprintf (file, "\n\tFile:   %s\n", eloc.file);
548   fprintf (file, "\tLine:   %d\n", eloc.line);
549   fprintf (file, "\tColumn: %d\n", eloc.column);
550 }
551
552
553 /* Allocate memory for a new lexer object and return it.  */
554
555 static cp_lexer *
556 cp_lexer_alloc (void)
557 {
558   cp_lexer *lexer;
559
560   c_common_no_more_pch ();
561
562   /* Allocate the memory.  */
563   lexer = ggc_alloc_cleared_cp_lexer ();
564
565   /* Initially we are not debugging.  */
566   lexer->debugging_p = false;
567
568   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
569                                    CP_SAVED_TOKEN_STACK);
570
571   /* Create the buffer.  */
572   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
573
574   return lexer;
575 }
576
577
578 /* Create a new main C++ lexer, the lexer that gets tokens from the
579    preprocessor.  */
580
581 static cp_lexer *
582 cp_lexer_new_main (void)
583 {
584   cp_lexer *lexer;
585   cp_token token;
586
587   /* It's possible that parsing the first pragma will load a PCH file,
588      which is a GC collection point.  So we have to do that before
589      allocating any memory.  */
590   cp_parser_initial_pragma (&token);
591
592   lexer = cp_lexer_alloc ();
593
594   /* Put the first token in the buffer.  */
595   VEC_quick_push (cp_token, lexer->buffer, &token);
596
597   /* Get the remaining tokens from the preprocessor.  */
598   while (token.type != CPP_EOF)
599     {
600       cp_lexer_get_preprocessor_token (lexer, &token);
601       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
602     }
603
604   lexer->last_token = VEC_address (cp_token, lexer->buffer)
605                       + VEC_length (cp_token, lexer->buffer)
606                       - 1;
607   lexer->next_token = VEC_length (cp_token, lexer->buffer)
608                       ? VEC_address (cp_token, lexer->buffer)
609                       : &eof_token;
610
611   /* Subsequent preprocessor diagnostics should use compiler
612      diagnostic functions to get the compiler source location.  */
613   done_lexing = true;
614
615   gcc_assert (!lexer->next_token->purged_p);
616   return lexer;
617 }
618
619 /* Create a new lexer whose token stream is primed with the tokens in
620    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
621
622 static cp_lexer *
623 cp_lexer_new_from_tokens (cp_token_cache *cache)
624 {
625   cp_token *first = cache->first;
626   cp_token *last = cache->last;
627   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
628
629   /* We do not own the buffer.  */
630   lexer->buffer = NULL;
631   lexer->next_token = first == last ? &eof_token : first;
632   lexer->last_token = last;
633
634   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
635                                    CP_SAVED_TOKEN_STACK);
636
637   /* Initially we are not debugging.  */
638   lexer->debugging_p = false;
639
640   gcc_assert (!lexer->next_token->purged_p);
641   return lexer;
642 }
643
644 /* Frees all resources associated with LEXER.  */
645
646 static void
647 cp_lexer_destroy (cp_lexer *lexer)
648 {
649   VEC_free (cp_token, gc, lexer->buffer);
650   VEC_free (cp_token_position, heap, lexer->saved_tokens);
651   ggc_free (lexer);
652 }
653
654 /* Returns nonzero if debugging information should be output.  */
655
656 static inline bool
657 cp_lexer_debugging_p (cp_lexer *lexer)
658 {
659   return lexer->debugging_p;
660 }
661
662
663 static inline cp_token_position
664 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
665 {
666   gcc_assert (!previous_p || lexer->next_token != &eof_token);
667
668   return lexer->next_token - previous_p;
669 }
670
671 static inline cp_token *
672 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
673 {
674   return pos;
675 }
676
677 static inline void
678 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
679 {
680   lexer->next_token = cp_lexer_token_at (lexer, pos);
681 }
682
683 static inline cp_token_position
684 cp_lexer_previous_token_position (cp_lexer *lexer)
685 {
686   if (lexer->next_token == &eof_token)
687     return lexer->last_token - 1;
688   else
689     return cp_lexer_token_position (lexer, true);
690 }
691
692 static inline cp_token *
693 cp_lexer_previous_token (cp_lexer *lexer)
694 {
695   cp_token_position tp = cp_lexer_previous_token_position (lexer);
696
697   return cp_lexer_token_at (lexer, tp);
698 }
699
700 /* nonzero if we are presently saving tokens.  */
701
702 static inline int
703 cp_lexer_saving_tokens (const cp_lexer* lexer)
704 {
705   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
706 }
707
708 /* Store the next token from the preprocessor in *TOKEN.  Return true
709    if we reach EOF.  If LEXER is NULL, assume we are handling an
710    initial #pragma pch_preprocess, and thus want the lexer to return
711    processed strings.  */
712
713 static void
714 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
715 {
716   static int is_extern_c = 0;
717
718    /* Get a new token from the preprocessor.  */
719   token->type
720     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
721                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
722   token->keyword = RID_MAX;
723   token->pragma_kind = PRAGMA_NONE;
724   token->purged_p = false;
725
726   /* On some systems, some header files are surrounded by an
727      implicit extern "C" block.  Set a flag in the token if it
728      comes from such a header.  */
729   is_extern_c += pending_lang_change;
730   pending_lang_change = 0;
731   token->implicit_extern_c = is_extern_c > 0;
732
733   /* Check to see if this token is a keyword.  */
734   if (token->type == CPP_NAME)
735     {
736       if (C_IS_RESERVED_WORD (token->u.value))
737         {
738           /* Mark this token as a keyword.  */
739           token->type = CPP_KEYWORD;
740           /* Record which keyword.  */
741           token->keyword = C_RID_CODE (token->u.value);
742         }
743       else
744         {
745           if (warn_cxx0x_compat
746               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
747               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
748             {
749               /* Warn about the C++0x keyword (but still treat it as
750                  an identifier).  */
751               warning (OPT_Wc__0x_compat, 
752                        "identifier %qE is a keyword in C++11",
753                        token->u.value);
754
755               /* Clear out the C_RID_CODE so we don't warn about this
756                  particular identifier-turned-keyword again.  */
757               C_SET_RID_CODE (token->u.value, RID_MAX);
758             }
759
760           token->ambiguous_p = false;
761           token->keyword = RID_MAX;
762         }
763     }
764   else if (token->type == CPP_AT_NAME)
765     {
766       /* This only happens in Objective-C++; it must be a keyword.  */
767       token->type = CPP_KEYWORD;
768       switch (C_RID_CODE (token->u.value))
769         {
770           /* Replace 'class' with '@class', 'private' with '@private',
771              etc.  This prevents confusion with the C++ keyword
772              'class', and makes the tokens consistent with other
773              Objective-C 'AT' keywords.  For example '@class' is
774              reported as RID_AT_CLASS which is consistent with
775              '@synchronized', which is reported as
776              RID_AT_SYNCHRONIZED.
777           */
778         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
779         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
780         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
781         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
782         case RID_THROW:     token->keyword = RID_AT_THROW; break;
783         case RID_TRY:       token->keyword = RID_AT_TRY; break;
784         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
785         default:            token->keyword = C_RID_CODE (token->u.value);
786         }
787     }
788   else if (token->type == CPP_PRAGMA)
789     {
790       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
791       token->pragma_kind = ((enum pragma_kind)
792                             TREE_INT_CST_LOW (token->u.value));
793       token->u.value = NULL_TREE;
794     }
795 }
796
797 /* Update the globals input_location and the input file stack from TOKEN.  */
798 static inline void
799 cp_lexer_set_source_position_from_token (cp_token *token)
800 {
801   if (token->type != CPP_EOF)
802     {
803       input_location = token->location;
804     }
805 }
806
807 /* Return a pointer to the next token in the token stream, but do not
808    consume it.  */
809
810 static inline cp_token *
811 cp_lexer_peek_token (cp_lexer *lexer)
812 {
813   if (cp_lexer_debugging_p (lexer))
814     {
815       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
816       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
817       putc ('\n', cp_lexer_debug_stream);
818     }
819   return lexer->next_token;
820 }
821
822 /* Return true if the next token has the indicated TYPE.  */
823
824 static inline bool
825 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
826 {
827   return cp_lexer_peek_token (lexer)->type == type;
828 }
829
830 /* Return true if the next token does not have the indicated TYPE.  */
831
832 static inline bool
833 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
834 {
835   return !cp_lexer_next_token_is (lexer, type);
836 }
837
838 /* Return true if the next token is the indicated KEYWORD.  */
839
840 static inline bool
841 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
842 {
843   return cp_lexer_peek_token (lexer)->keyword == keyword;
844 }
845
846 /* Return true if the next token is not the indicated KEYWORD.  */
847
848 static inline bool
849 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
850 {
851   return cp_lexer_peek_token (lexer)->keyword != keyword;
852 }
853
854 /* Return true if the next token is a keyword for a decl-specifier.  */
855
856 static bool
857 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
858 {
859   cp_token *token;
860
861   token = cp_lexer_peek_token (lexer);
862   switch (token->keyword) 
863     {
864       /* auto specifier: storage-class-specifier in C++,
865          simple-type-specifier in C++0x.  */
866     case RID_AUTO:
867       /* Storage classes.  */
868     case RID_REGISTER:
869     case RID_STATIC:
870     case RID_EXTERN:
871     case RID_MUTABLE:
872     case RID_THREAD:
873       /* Elaborated type specifiers.  */
874     case RID_ENUM:
875     case RID_CLASS:
876     case RID_STRUCT:
877     case RID_UNION:
878     case RID_TYPENAME:
879       /* Simple type specifiers.  */
880     case RID_CHAR:
881     case RID_CHAR16:
882     case RID_CHAR32:
883     case RID_WCHAR:
884     case RID_BOOL:
885     case RID_SHORT:
886     case RID_INT:
887     case RID_LONG:
888     case RID_INT128:
889     case RID_SIGNED:
890     case RID_UNSIGNED:
891     case RID_FLOAT:
892     case RID_DOUBLE:
893     case RID_VOID:
894       /* GNU extensions.  */ 
895     case RID_ATTRIBUTE:
896     case RID_TYPEOF:
897       /* C++0x extensions.  */
898     case RID_DECLTYPE:
899     case RID_UNDERLYING_TYPE:
900       return true;
901
902     default:
903       return false;
904     }
905 }
906
907 /* Returns TRUE iff the token T begins a decltype type.  */
908
909 static bool
910 token_is_decltype (cp_token *t)
911 {
912   return (t->keyword == RID_DECLTYPE
913           || t->type == CPP_DECLTYPE);
914 }
915
916 /* Returns TRUE iff the next token begins a decltype type.  */
917
918 static bool
919 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
920 {
921   cp_token *t = cp_lexer_peek_token (lexer);
922   return token_is_decltype (t);
923 }
924
925 /* Return a pointer to the Nth token in the token stream.  If N is 1,
926    then this is precisely equivalent to cp_lexer_peek_token (except
927    that it is not inline).  One would like to disallow that case, but
928    there is one case (cp_parser_nth_token_starts_template_id) where
929    the caller passes a variable for N and it might be 1.  */
930
931 static cp_token *
932 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
933 {
934   cp_token *token;
935
936   /* N is 1-based, not zero-based.  */
937   gcc_assert (n > 0);
938
939   if (cp_lexer_debugging_p (lexer))
940     fprintf (cp_lexer_debug_stream,
941              "cp_lexer: peeking ahead %ld at token: ", (long)n);
942
943   --n;
944   token = lexer->next_token;
945   gcc_assert (!n || token != &eof_token);
946   while (n != 0)
947     {
948       ++token;
949       if (token == lexer->last_token)
950         {
951           token = &eof_token;
952           break;
953         }
954
955       if (!token->purged_p)
956         --n;
957     }
958
959   if (cp_lexer_debugging_p (lexer))
960     {
961       cp_lexer_print_token (cp_lexer_debug_stream, token);
962       putc ('\n', cp_lexer_debug_stream);
963     }
964
965   return token;
966 }
967
968 /* Return the next token, and advance the lexer's next_token pointer
969    to point to the next non-purged token.  */
970
971 static cp_token *
972 cp_lexer_consume_token (cp_lexer* lexer)
973 {
974   cp_token *token = lexer->next_token;
975
976   gcc_assert (token != &eof_token);
977   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
978
979   do
980     {
981       lexer->next_token++;
982       if (lexer->next_token == lexer->last_token)
983         {
984           lexer->next_token = &eof_token;
985           break;
986         }
987
988     }
989   while (lexer->next_token->purged_p);
990
991   cp_lexer_set_source_position_from_token (token);
992
993   /* Provide debugging output.  */
994   if (cp_lexer_debugging_p (lexer))
995     {
996       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
997       cp_lexer_print_token (cp_lexer_debug_stream, token);
998       putc ('\n', cp_lexer_debug_stream);
999     }
1000
1001   return token;
1002 }
1003
1004 /* Permanently remove the next token from the token stream, and
1005    advance the next_token pointer to refer to the next non-purged
1006    token.  */
1007
1008 static void
1009 cp_lexer_purge_token (cp_lexer *lexer)
1010 {
1011   cp_token *tok = lexer->next_token;
1012
1013   gcc_assert (tok != &eof_token);
1014   tok->purged_p = true;
1015   tok->location = UNKNOWN_LOCATION;
1016   tok->u.value = NULL_TREE;
1017   tok->keyword = RID_MAX;
1018
1019   do
1020     {
1021       tok++;
1022       if (tok == lexer->last_token)
1023         {
1024           tok = &eof_token;
1025           break;
1026         }
1027     }
1028   while (tok->purged_p);
1029   lexer->next_token = tok;
1030 }
1031
1032 /* Permanently remove all tokens after TOK, up to, but not
1033    including, the token that will be returned next by
1034    cp_lexer_peek_token.  */
1035
1036 static void
1037 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1038 {
1039   cp_token *peek = lexer->next_token;
1040
1041   if (peek == &eof_token)
1042     peek = lexer->last_token;
1043
1044   gcc_assert (tok < peek);
1045
1046   for ( tok += 1; tok != peek; tok += 1)
1047     {
1048       tok->purged_p = true;
1049       tok->location = UNKNOWN_LOCATION;
1050       tok->u.value = NULL_TREE;
1051       tok->keyword = RID_MAX;
1052     }
1053 }
1054
1055 /* Begin saving tokens.  All tokens consumed after this point will be
1056    preserved.  */
1057
1058 static void
1059 cp_lexer_save_tokens (cp_lexer* lexer)
1060 {
1061   /* Provide debugging output.  */
1062   if (cp_lexer_debugging_p (lexer))
1063     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1064
1065   VEC_safe_push (cp_token_position, heap,
1066                  lexer->saved_tokens, lexer->next_token);
1067 }
1068
1069 /* Commit to the portion of the token stream most recently saved.  */
1070
1071 static void
1072 cp_lexer_commit_tokens (cp_lexer* lexer)
1073 {
1074   /* Provide debugging output.  */
1075   if (cp_lexer_debugging_p (lexer))
1076     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1077
1078   VEC_pop (cp_token_position, lexer->saved_tokens);
1079 }
1080
1081 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1082    to the token stream.  Stop saving tokens.  */
1083
1084 static void
1085 cp_lexer_rollback_tokens (cp_lexer* lexer)
1086 {
1087   /* Provide debugging output.  */
1088   if (cp_lexer_debugging_p (lexer))
1089     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1090
1091   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1092 }
1093
1094 /* Print a representation of the TOKEN on the STREAM.  */
1095
1096 static void
1097 cp_lexer_print_token (FILE * stream, cp_token *token)
1098 {
1099   /* We don't use cpp_type2name here because the parser defines
1100      a few tokens of its own.  */
1101   static const char *const token_names[] = {
1102     /* cpplib-defined token types */
1103 #define OP(e, s) #e,
1104 #define TK(e, s) #e,
1105     TTYPE_TABLE
1106 #undef OP
1107 #undef TK
1108     /* C++ parser token types - see "Manifest constants", above.  */
1109     "KEYWORD",
1110     "TEMPLATE_ID",
1111     "NESTED_NAME_SPECIFIER",
1112   };
1113
1114   /* For some tokens, print the associated data.  */
1115   switch (token->type)
1116     {
1117     case CPP_KEYWORD:
1118       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1119          For example, `struct' is mapped to an INTEGER_CST.  */
1120       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1121         break;
1122       /* else fall through */
1123     case CPP_NAME:
1124       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1125       break;
1126
1127     case CPP_STRING:
1128     case CPP_STRING16:
1129     case CPP_STRING32:
1130     case CPP_WSTRING:
1131     case CPP_UTF8STRING:
1132       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1133       break;
1134
1135     case CPP_NUMBER:
1136       print_generic_expr (stream, token->u.value, 0);
1137       break;
1138
1139     default:
1140       /* If we have a name for the token, print it out.  Otherwise, we
1141          simply give the numeric code.  */
1142       if (token->type < ARRAY_SIZE(token_names))
1143         fputs (token_names[token->type], stream);
1144       else
1145         fprintf (stream, "[%d]", token->type);
1146       break;
1147     }
1148 }
1149
1150 /* Start emitting debugging information.  */
1151
1152 static void
1153 cp_lexer_start_debugging (cp_lexer* lexer)
1154 {
1155   lexer->debugging_p = true;
1156   cp_lexer_debug_stream = stderr;
1157 }
1158
1159 /* Stop emitting debugging information.  */
1160
1161 static void
1162 cp_lexer_stop_debugging (cp_lexer* lexer)
1163 {
1164   lexer->debugging_p = false;
1165   cp_lexer_debug_stream = NULL;
1166 }
1167
1168 /* Create a new cp_token_cache, representing a range of tokens.  */
1169
1170 static cp_token_cache *
1171 cp_token_cache_new (cp_token *first, cp_token *last)
1172 {
1173   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1174   cache->first = first;
1175   cache->last = last;
1176   return cache;
1177 }
1178
1179 \f
1180 /* Decl-specifiers.  */
1181
1182 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1183
1184 static void
1185 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1186 {
1187   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1188 }
1189
1190 /* Declarators.  */
1191
1192 /* Nothing other than the parser should be creating declarators;
1193    declarators are a semi-syntactic representation of C++ entities.
1194    Other parts of the front end that need to create entities (like
1195    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1196
1197 static cp_declarator *make_call_declarator
1198   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1199 static cp_declarator *make_array_declarator
1200   (cp_declarator *, tree);
1201 static cp_declarator *make_pointer_declarator
1202   (cp_cv_quals, cp_declarator *);
1203 static cp_declarator *make_reference_declarator
1204   (cp_cv_quals, cp_declarator *, bool);
1205 static cp_parameter_declarator *make_parameter_declarator
1206   (cp_decl_specifier_seq *, cp_declarator *, tree);
1207 static cp_declarator *make_ptrmem_declarator
1208   (cp_cv_quals, tree, cp_declarator *);
1209
1210 /* An erroneous declarator.  */
1211 static cp_declarator *cp_error_declarator;
1212
1213 /* The obstack on which declarators and related data structures are
1214    allocated.  */
1215 static struct obstack declarator_obstack;
1216
1217 /* Alloc BYTES from the declarator memory pool.  */
1218
1219 static inline void *
1220 alloc_declarator (size_t bytes)
1221 {
1222   return obstack_alloc (&declarator_obstack, bytes);
1223 }
1224
1225 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1226    common to all declarators.  */
1227
1228 static cp_declarator *
1229 make_declarator (cp_declarator_kind kind)
1230 {
1231   cp_declarator *declarator;
1232
1233   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1234   declarator->kind = kind;
1235   declarator->attributes = NULL_TREE;
1236   declarator->declarator = NULL;
1237   declarator->parameter_pack_p = false;
1238   declarator->id_loc = UNKNOWN_LOCATION;
1239
1240   return declarator;
1241 }
1242
1243 /* Make a declarator for a generalized identifier.  If
1244    QUALIFYING_SCOPE is non-NULL, the identifier is
1245    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1246    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1247    is, if any.   */
1248
1249 static cp_declarator *
1250 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1251                     special_function_kind sfk)
1252 {
1253   cp_declarator *declarator;
1254
1255   /* It is valid to write:
1256
1257        class C { void f(); };
1258        typedef C D;
1259        void D::f();
1260
1261      The standard is not clear about whether `typedef const C D' is
1262      legal; as of 2002-09-15 the committee is considering that
1263      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1264      well.  */
1265   if (qualifying_scope && TYPE_P (qualifying_scope))
1266     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267
1268   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1269               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1270               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271
1272   declarator = make_declarator (cdk_id);
1273   declarator->u.id.qualifying_scope = qualifying_scope;
1274   declarator->u.id.unqualified_name = unqualified_name;
1275   declarator->u.id.sfk = sfk;
1276   
1277   return declarator;
1278 }
1279
1280 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1281    of modifiers such as const or volatile to apply to the pointer
1282    type, represented as identifiers.  */
1283
1284 cp_declarator *
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1286 {
1287   cp_declarator *declarator;
1288
1289   declarator = make_declarator (cdk_pointer);
1290   declarator->declarator = target;
1291   declarator->u.pointer.qualifiers = cv_qualifiers;
1292   declarator->u.pointer.class_type = NULL_TREE;
1293   if (target)
1294     {
1295       declarator->id_loc = target->id_loc;
1296       declarator->parameter_pack_p = target->parameter_pack_p;
1297       target->parameter_pack_p = false;
1298     }
1299   else
1300     declarator->parameter_pack_p = false;
1301
1302   return declarator;
1303 }
1304
1305 /* Like make_pointer_declarator -- but for references.  */
1306
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309                            bool rvalue_ref)
1310 {
1311   cp_declarator *declarator;
1312
1313   declarator = make_declarator (cdk_reference);
1314   declarator->declarator = target;
1315   declarator->u.reference.qualifiers = cv_qualifiers;
1316   declarator->u.reference.rvalue_ref = rvalue_ref;
1317   if (target)
1318     {
1319       declarator->id_loc = target->id_loc;
1320       declarator->parameter_pack_p = target->parameter_pack_p;
1321       target->parameter_pack_p = false;
1322     }
1323   else
1324     declarator->parameter_pack_p = false;
1325
1326   return declarator;
1327 }
1328
1329 /* Like make_pointer_declarator -- but for a pointer to a non-static
1330    member of CLASS_TYPE.  */
1331
1332 cp_declarator *
1333 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1334                         cp_declarator *pointee)
1335 {
1336   cp_declarator *declarator;
1337
1338   declarator = make_declarator (cdk_ptrmem);
1339   declarator->declarator = pointee;
1340   declarator->u.pointer.qualifiers = cv_qualifiers;
1341   declarator->u.pointer.class_type = class_type;
1342
1343   if (pointee)
1344     {
1345       declarator->parameter_pack_p = pointee->parameter_pack_p;
1346       pointee->parameter_pack_p = false;
1347     }
1348   else
1349     declarator->parameter_pack_p = false;
1350
1351   return declarator;
1352 }
1353
1354 /* Make a declarator for the function given by TARGET, with the
1355    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1356    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1357    indicates what exceptions can be thrown.  */
1358
1359 cp_declarator *
1360 make_call_declarator (cp_declarator *target,
1361                       tree parms,
1362                       cp_cv_quals cv_qualifiers,
1363                       cp_virt_specifiers virt_specifiers,
1364                       tree exception_specification,
1365                       tree late_return_type)
1366 {
1367   cp_declarator *declarator;
1368
1369   declarator = make_declarator (cdk_function);
1370   declarator->declarator = target;
1371   declarator->u.function.parameters = parms;
1372   declarator->u.function.qualifiers = cv_qualifiers;
1373   declarator->u.function.virt_specifiers = virt_specifiers;
1374   declarator->u.function.exception_specification = exception_specification;
1375   declarator->u.function.late_return_type = late_return_type;
1376   if (target)
1377     {
1378       declarator->id_loc = target->id_loc;
1379       declarator->parameter_pack_p = target->parameter_pack_p;
1380       target->parameter_pack_p = false;
1381     }
1382   else
1383     declarator->parameter_pack_p = false;
1384
1385   return declarator;
1386 }
1387
1388 /* Make a declarator for an array of BOUNDS elements, each of which is
1389    defined by ELEMENT.  */
1390
1391 cp_declarator *
1392 make_array_declarator (cp_declarator *element, tree bounds)
1393 {
1394   cp_declarator *declarator;
1395
1396   declarator = make_declarator (cdk_array);
1397   declarator->declarator = element;
1398   declarator->u.array.bounds = bounds;
1399   if (element)
1400     {
1401       declarator->id_loc = element->id_loc;
1402       declarator->parameter_pack_p = element->parameter_pack_p;
1403       element->parameter_pack_p = false;
1404     }
1405   else
1406     declarator->parameter_pack_p = false;
1407
1408   return declarator;
1409 }
1410
1411 /* Determine whether the declarator we've seen so far can be a
1412    parameter pack, when followed by an ellipsis.  */
1413 static bool 
1414 declarator_can_be_parameter_pack (cp_declarator *declarator)
1415 {
1416   /* Search for a declarator name, or any other declarator that goes
1417      after the point where the ellipsis could appear in a parameter
1418      pack. If we find any of these, then this declarator can not be
1419      made into a parameter pack.  */
1420   bool found = false;
1421   while (declarator && !found)
1422     {
1423       switch ((int)declarator->kind)
1424         {
1425         case cdk_id:
1426         case cdk_array:
1427           found = true;
1428           break;
1429
1430         case cdk_error:
1431           return true;
1432
1433         default:
1434           declarator = declarator->declarator;
1435           break;
1436         }
1437     }
1438
1439   return !found;
1440 }
1441
1442 cp_parameter_declarator *no_parameters;
1443
1444 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1445    DECLARATOR and DEFAULT_ARGUMENT.  */
1446
1447 cp_parameter_declarator *
1448 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1449                            cp_declarator *declarator,
1450                            tree default_argument)
1451 {
1452   cp_parameter_declarator *parameter;
1453
1454   parameter = ((cp_parameter_declarator *)
1455                alloc_declarator (sizeof (cp_parameter_declarator)));
1456   parameter->next = NULL;
1457   if (decl_specifiers)
1458     parameter->decl_specifiers = *decl_specifiers;
1459   else
1460     clear_decl_specs (&parameter->decl_specifiers);
1461   parameter->declarator = declarator;
1462   parameter->default_argument = default_argument;
1463   parameter->ellipsis_p = false;
1464
1465   return parameter;
1466 }
1467
1468 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1469
1470 static bool
1471 function_declarator_p (const cp_declarator *declarator)
1472 {
1473   while (declarator)
1474     {
1475       if (declarator->kind == cdk_function
1476           && declarator->declarator->kind == cdk_id)
1477         return true;
1478       if (declarator->kind == cdk_id
1479           || declarator->kind == cdk_error)
1480         return false;
1481       declarator = declarator->declarator;
1482     }
1483   return false;
1484 }
1485  
1486 /* The parser.  */
1487
1488 /* Overview
1489    --------
1490
1491    A cp_parser parses the token stream as specified by the C++
1492    grammar.  Its job is purely parsing, not semantic analysis.  For
1493    example, the parser breaks the token stream into declarators,
1494    expressions, statements, and other similar syntactic constructs.
1495    It does not check that the types of the expressions on either side
1496    of an assignment-statement are compatible, or that a function is
1497    not declared with a parameter of type `void'.
1498
1499    The parser invokes routines elsewhere in the compiler to perform
1500    semantic analysis and to build up the abstract syntax tree for the
1501    code processed.
1502
1503    The parser (and the template instantiation code, which is, in a
1504    way, a close relative of parsing) are the only parts of the
1505    compiler that should be calling push_scope and pop_scope, or
1506    related functions.  The parser (and template instantiation code)
1507    keeps track of what scope is presently active; everything else
1508    should simply honor that.  (The code that generates static
1509    initializers may also need to set the scope, in order to check
1510    access control correctly when emitting the initializers.)
1511
1512    Methodology
1513    -----------
1514
1515    The parser is of the standard recursive-descent variety.  Upcoming
1516    tokens in the token stream are examined in order to determine which
1517    production to use when parsing a non-terminal.  Some C++ constructs
1518    require arbitrary look ahead to disambiguate.  For example, it is
1519    impossible, in the general case, to tell whether a statement is an
1520    expression or declaration without scanning the entire statement.
1521    Therefore, the parser is capable of "parsing tentatively."  When the
1522    parser is not sure what construct comes next, it enters this mode.
1523    Then, while we attempt to parse the construct, the parser queues up
1524    error messages, rather than issuing them immediately, and saves the
1525    tokens it consumes.  If the construct is parsed successfully, the
1526    parser "commits", i.e., it issues any queued error messages and
1527    the tokens that were being preserved are permanently discarded.
1528    If, however, the construct is not parsed successfully, the parser
1529    rolls back its state completely so that it can resume parsing using
1530    a different alternative.
1531
1532    Future Improvements
1533    -------------------
1534
1535    The performance of the parser could probably be improved substantially.
1536    We could often eliminate the need to parse tentatively by looking ahead
1537    a little bit.  In some places, this approach might not entirely eliminate
1538    the need to parse tentatively, but it might still speed up the average
1539    case.  */
1540
1541 /* Flags that are passed to some parsing functions.  These values can
1542    be bitwise-ored together.  */
1543
1544 enum
1545 {
1546   /* No flags.  */
1547   CP_PARSER_FLAGS_NONE = 0x0,
1548   /* The construct is optional.  If it is not present, then no error
1549      should be issued.  */
1550   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1551   /* When parsing a type-specifier, treat user-defined type-names
1552      as non-type identifiers.  */
1553   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1554   /* When parsing a type-specifier, do not try to parse a class-specifier
1555      or enum-specifier.  */
1556   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1557   /* When parsing a decl-specifier-seq, only allow type-specifier or
1558      constexpr.  */
1559   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1560 };
1561
1562 /* This type is used for parameters and variables which hold
1563    combinations of the above flags.  */
1564 typedef int cp_parser_flags;
1565
1566 /* The different kinds of declarators we want to parse.  */
1567
1568 typedef enum cp_parser_declarator_kind
1569 {
1570   /* We want an abstract declarator.  */
1571   CP_PARSER_DECLARATOR_ABSTRACT,
1572   /* We want a named declarator.  */
1573   CP_PARSER_DECLARATOR_NAMED,
1574   /* We don't mind, but the name must be an unqualified-id.  */
1575   CP_PARSER_DECLARATOR_EITHER
1576 } cp_parser_declarator_kind;
1577
1578 /* The precedence values used to parse binary expressions.  The minimum value
1579    of PREC must be 1, because zero is reserved to quickly discriminate
1580    binary operators from other tokens.  */
1581
1582 enum cp_parser_prec
1583 {
1584   PREC_NOT_OPERATOR,
1585   PREC_LOGICAL_OR_EXPRESSION,
1586   PREC_LOGICAL_AND_EXPRESSION,
1587   PREC_INCLUSIVE_OR_EXPRESSION,
1588   PREC_EXCLUSIVE_OR_EXPRESSION,
1589   PREC_AND_EXPRESSION,
1590   PREC_EQUALITY_EXPRESSION,
1591   PREC_RELATIONAL_EXPRESSION,
1592   PREC_SHIFT_EXPRESSION,
1593   PREC_ADDITIVE_EXPRESSION,
1594   PREC_MULTIPLICATIVE_EXPRESSION,
1595   PREC_PM_EXPRESSION,
1596   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1597 };
1598
1599 /* A mapping from a token type to a corresponding tree node type, with a
1600    precedence value.  */
1601
1602 typedef struct cp_parser_binary_operations_map_node
1603 {
1604   /* The token type.  */
1605   enum cpp_ttype token_type;
1606   /* The corresponding tree code.  */
1607   enum tree_code tree_type;
1608   /* The precedence of this operator.  */
1609   enum cp_parser_prec prec;
1610 } cp_parser_binary_operations_map_node;
1611
1612 typedef struct cp_parser_expression_stack_entry
1613 {
1614   /* Left hand side of the binary operation we are currently
1615      parsing.  */
1616   tree lhs;
1617   /* Original tree code for left hand side, if it was a binary
1618      expression itself (used for -Wparentheses).  */
1619   enum tree_code lhs_type;
1620   /* Tree code for the binary operation we are parsing.  */
1621   enum tree_code tree_type;
1622   /* Precedence of the binary operation we are parsing.  */
1623   enum cp_parser_prec prec;
1624 } cp_parser_expression_stack_entry;
1625
1626 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1627    entries because precedence levels on the stack are monotonically
1628    increasing.  */
1629 typedef struct cp_parser_expression_stack_entry
1630   cp_parser_expression_stack[NUM_PREC_VALUES];
1631
1632 /* Prototypes.  */
1633
1634 /* Constructors and destructors.  */
1635
1636 static cp_parser_context *cp_parser_context_new
1637   (cp_parser_context *);
1638
1639 /* Class variables.  */
1640
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1642
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644    Transformed into an associative array (binops_by_token) by
1645    cp_parser_new.  */
1646
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1650
1651   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654
1655   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657
1658   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660
1661   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665
1666   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1668
1669   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1670
1671   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1672
1673   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1674
1675   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1676
1677   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1678 };
1679
1680 /* The same as binops, but initialized by cp_parser_new so that
1681    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1682    for speed.  */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1684
1685 /* Constructors and destructors.  */
1686
1687 /* Construct a new context.  The context below this one on the stack
1688    is given by NEXT.  */
1689
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1692 {
1693   cp_parser_context *context;
1694
1695   /* Allocate the storage.  */
1696   if (cp_parser_context_free_list != NULL)
1697     {
1698       /* Pull the first entry from the free list.  */
1699       context = cp_parser_context_free_list;
1700       cp_parser_context_free_list = context->next;
1701       memset (context, 0, sizeof (*context));
1702     }
1703   else
1704     context = ggc_alloc_cleared_cp_parser_context ();
1705
1706   /* No errors have occurred yet in this context.  */
1707   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708   /* If this is not the bottommost context, copy information that we
1709      need from the previous context.  */
1710   if (next)
1711     {
1712       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713          expression, then we are parsing one in this context, too.  */
1714       context->object_type = next->object_type;
1715       /* Thread the stack.  */
1716       context->next = next;
1717     }
1718
1719   return context;
1720 }
1721
1722 /* Managing the unparsed function queues.  */
1723
1724 #define unparsed_funs_with_default_args \
1725   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728 #define unparsed_nsdmis \
1729   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1730
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1733 {
1734   VEC_safe_push (cp_unparsed_functions_entry, gc,
1735                  parser->unparsed_queues, NULL);
1736   unparsed_funs_with_default_args = NULL;
1737   unparsed_funs_with_definitions = make_tree_vector ();
1738   unparsed_nsdmis = NULL;
1739 }
1740
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1743 {
1744   release_tree_vector (unparsed_funs_with_definitions);
1745   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1746 }
1747
1748 /* Prototypes.  */
1749
1750 /* Constructors and destructors.  */
1751
1752 static cp_parser *cp_parser_new
1753   (void);
1754
1755 /* Routines to parse various constructs.
1756
1757    Those that return `tree' will return the error_mark_node (rather
1758    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759    Sometimes, they will return an ordinary node if error-recovery was
1760    attempted, even though a parse error occurred.  So, to check
1761    whether or not a parse error occurred, you should always use
1762    cp_parser_error_occurred.  If the construct is optional (indicated
1763    either by an `_opt' in the name of the function that does the
1764    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765    the construct is not present.  */
1766
1767 /* Lexical conventions [gram.lex]  */
1768
1769 static tree cp_parser_identifier
1770   (cp_parser *);
1771 static tree cp_parser_string_literal
1772   (cp_parser *, bool, bool);
1773 static tree cp_parser_userdef_char_literal
1774   (cp_parser *);
1775 static tree cp_parser_userdef_string_literal
1776   (cp_token *);
1777 static tree cp_parser_userdef_numeric_literal
1778   (cp_parser *);
1779
1780 /* Basic concepts [gram.basic]  */
1781
1782 static bool cp_parser_translation_unit
1783   (cp_parser *);
1784
1785 /* Expressions [gram.expr]  */
1786
1787 static tree cp_parser_primary_expression
1788   (cp_parser *, bool, bool, bool, cp_id_kind *);
1789 static tree cp_parser_id_expression
1790   (cp_parser *, bool, bool, bool *, bool, bool);
1791 static tree cp_parser_unqualified_id
1792   (cp_parser *, bool, bool, bool, bool);
1793 static tree cp_parser_nested_name_specifier_opt
1794   (cp_parser *, bool, bool, bool, bool);
1795 static tree cp_parser_nested_name_specifier
1796   (cp_parser *, bool, bool, bool, bool);
1797 static tree cp_parser_qualifying_entity
1798   (cp_parser *, bool, bool, bool, bool, bool);
1799 static tree cp_parser_postfix_expression
1800   (cp_parser *, bool, bool, bool, cp_id_kind *);
1801 static tree cp_parser_postfix_open_square_expression
1802   (cp_parser *, tree, bool);
1803 static tree cp_parser_postfix_dot_deref_expression
1804   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1806   (cp_parser *, int, bool, bool, bool *);
1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1809 static void cp_parser_pseudo_destructor_name
1810   (cp_parser *, tree *, tree *);
1811 static tree cp_parser_unary_expression
1812   (cp_parser *, bool, bool, cp_id_kind *);
1813 static enum tree_code cp_parser_unary_operator
1814   (cp_token *);
1815 static tree cp_parser_new_expression
1816   (cp_parser *);
1817 static VEC(tree,gc) *cp_parser_new_placement
1818   (cp_parser *);
1819 static tree cp_parser_new_type_id
1820   (cp_parser *, tree *);
1821 static cp_declarator *cp_parser_new_declarator_opt
1822   (cp_parser *);
1823 static cp_declarator *cp_parser_direct_new_declarator
1824   (cp_parser *);
1825 static VEC(tree,gc) *cp_parser_new_initializer
1826   (cp_parser *);
1827 static tree cp_parser_delete_expression
1828   (cp_parser *);
1829 static tree cp_parser_cast_expression
1830   (cp_parser *, bool, bool, cp_id_kind *);
1831 static tree cp_parser_binary_expression
1832   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1833 static tree cp_parser_question_colon_clause
1834   (cp_parser *, tree);
1835 static tree cp_parser_assignment_expression
1836   (cp_parser *, bool, cp_id_kind *);
1837 static enum tree_code cp_parser_assignment_operator_opt
1838   (cp_parser *);
1839 static tree cp_parser_expression
1840   (cp_parser *, bool, cp_id_kind *);
1841 static tree cp_parser_constant_expression
1842   (cp_parser *, bool, bool *);
1843 static tree cp_parser_builtin_offsetof
1844   (cp_parser *);
1845 static tree cp_parser_lambda_expression
1846   (cp_parser *);
1847 static void cp_parser_lambda_introducer
1848   (cp_parser *, tree);
1849 static bool cp_parser_lambda_declarator_opt
1850   (cp_parser *, tree);
1851 static void cp_parser_lambda_body
1852   (cp_parser *, tree);
1853
1854 /* Statements [gram.stmt.stmt]  */
1855
1856 static void cp_parser_statement
1857   (cp_parser *, tree, bool, bool *);
1858 static void cp_parser_label_for_labeled_statement
1859   (cp_parser *);
1860 static tree cp_parser_expression_statement
1861   (cp_parser *, tree);
1862 static tree cp_parser_compound_statement
1863   (cp_parser *, tree, bool, bool);
1864 static void cp_parser_statement_seq_opt
1865   (cp_parser *, tree);
1866 static tree cp_parser_selection_statement
1867   (cp_parser *, bool *);
1868 static tree cp_parser_condition
1869   (cp_parser *);
1870 static tree cp_parser_iteration_statement
1871   (cp_parser *);
1872 static bool cp_parser_for_init_statement
1873   (cp_parser *, tree *decl);
1874 static tree cp_parser_for
1875   (cp_parser *);
1876 static tree cp_parser_c_for
1877   (cp_parser *, tree, tree);
1878 static tree cp_parser_range_for
1879   (cp_parser *, tree, tree, tree);
1880 static void do_range_for_auto_deduction
1881   (tree, tree);
1882 static tree cp_parser_perform_range_for_lookup
1883   (tree, tree *, tree *);
1884 static tree cp_parser_range_for_member_function
1885   (tree, tree);
1886 static tree cp_parser_jump_statement
1887   (cp_parser *);
1888 static void cp_parser_declaration_statement
1889   (cp_parser *);
1890
1891 static tree cp_parser_implicitly_scoped_statement
1892   (cp_parser *, bool *);
1893 static void cp_parser_already_scoped_statement
1894   (cp_parser *);
1895
1896 /* Declarations [gram.dcl.dcl] */
1897
1898 static void cp_parser_declaration_seq_opt
1899   (cp_parser *);
1900 static void cp_parser_declaration
1901   (cp_parser *);
1902 static void cp_parser_block_declaration
1903   (cp_parser *, bool);
1904 static void cp_parser_simple_declaration
1905   (cp_parser *, bool, tree *);
1906 static void cp_parser_decl_specifier_seq
1907   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1908 static tree cp_parser_storage_class_specifier_opt
1909   (cp_parser *);
1910 static tree cp_parser_function_specifier_opt
1911   (cp_parser *, cp_decl_specifier_seq *);
1912 static tree cp_parser_type_specifier
1913   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1914    int *, bool *);
1915 static tree cp_parser_simple_type_specifier
1916   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1917 static tree cp_parser_type_name
1918   (cp_parser *);
1919 static tree cp_parser_nonclass_name 
1920   (cp_parser* parser);
1921 static tree cp_parser_elaborated_type_specifier
1922   (cp_parser *, bool, bool);
1923 static tree cp_parser_enum_specifier
1924   (cp_parser *);
1925 static void cp_parser_enumerator_list
1926   (cp_parser *, tree);
1927 static void cp_parser_enumerator_definition
1928   (cp_parser *, tree);
1929 static tree cp_parser_namespace_name
1930   (cp_parser *);
1931 static void cp_parser_namespace_definition
1932   (cp_parser *);
1933 static void cp_parser_namespace_body
1934   (cp_parser *);
1935 static tree cp_parser_qualified_namespace_specifier
1936   (cp_parser *);
1937 static void cp_parser_namespace_alias_definition
1938   (cp_parser *);
1939 static bool cp_parser_using_declaration
1940   (cp_parser *, bool);
1941 static void cp_parser_using_directive
1942   (cp_parser *);
1943 static tree cp_parser_alias_declaration
1944   (cp_parser *);
1945 static void cp_parser_asm_definition
1946   (cp_parser *);
1947 static void cp_parser_linkage_specification
1948   (cp_parser *);
1949 static void cp_parser_static_assert
1950   (cp_parser *, bool);
1951 static tree cp_parser_decltype
1952   (cp_parser *);
1953
1954 /* Declarators [gram.dcl.decl] */
1955
1956 static tree cp_parser_init_declarator
1957   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1958 static cp_declarator *cp_parser_declarator
1959   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1960 static cp_declarator *cp_parser_direct_declarator
1961   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1962 static enum tree_code cp_parser_ptr_operator
1963   (cp_parser *, tree *, cp_cv_quals *);
1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1965   (cp_parser *);
1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1967   (cp_parser *);
1968 static tree cp_parser_late_return_type_opt
1969   (cp_parser *, cp_cv_quals);
1970 static tree cp_parser_declarator_id
1971   (cp_parser *, bool);
1972 static tree cp_parser_type_id
1973   (cp_parser *);
1974 static tree cp_parser_template_type_arg
1975   (cp_parser *);
1976 static tree cp_parser_trailing_type_id (cp_parser *);
1977 static tree cp_parser_type_id_1
1978   (cp_parser *, bool, bool);
1979 static void cp_parser_type_specifier_seq
1980   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1981 static tree cp_parser_parameter_declaration_clause
1982   (cp_parser *);
1983 static tree cp_parser_parameter_declaration_list
1984   (cp_parser *, bool *);
1985 static cp_parameter_declarator *cp_parser_parameter_declaration
1986   (cp_parser *, bool, bool *);
1987 static tree cp_parser_default_argument 
1988   (cp_parser *, bool);
1989 static void cp_parser_function_body
1990   (cp_parser *);
1991 static tree cp_parser_initializer
1992   (cp_parser *, bool *, bool *);
1993 static tree cp_parser_initializer_clause
1994   (cp_parser *, bool *);
1995 static tree cp_parser_braced_list
1996   (cp_parser*, bool*);
1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1998   (cp_parser *, bool *);
1999
2000 static bool cp_parser_ctor_initializer_opt_and_function_body
2001   (cp_parser *);
2002
2003 /* Classes [gram.class] */
2004
2005 static tree cp_parser_class_name
2006   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2007 static tree cp_parser_class_specifier
2008   (cp_parser *);
2009 static tree cp_parser_class_head
2010   (cp_parser *, bool *, tree *, tree *);
2011 static enum tag_types cp_parser_class_key
2012   (cp_parser *);
2013 static void cp_parser_member_specification_opt
2014   (cp_parser *);
2015 static void cp_parser_member_declaration
2016   (cp_parser *);
2017 static tree cp_parser_pure_specifier
2018   (cp_parser *);
2019 static tree cp_parser_constant_initializer
2020   (cp_parser *);
2021
2022 /* Derived classes [gram.class.derived] */
2023
2024 static tree cp_parser_base_clause
2025   (cp_parser *);
2026 static tree cp_parser_base_specifier
2027   (cp_parser *);
2028
2029 /* Special member functions [gram.special] */
2030
2031 static tree cp_parser_conversion_function_id
2032   (cp_parser *);
2033 static tree cp_parser_conversion_type_id
2034   (cp_parser *);
2035 static cp_declarator *cp_parser_conversion_declarator_opt
2036   (cp_parser *);
2037 static bool cp_parser_ctor_initializer_opt
2038   (cp_parser *);
2039 static void cp_parser_mem_initializer_list
2040   (cp_parser *);
2041 static tree cp_parser_mem_initializer
2042   (cp_parser *);
2043 static tree cp_parser_mem_initializer_id
2044   (cp_parser *);
2045
2046 /* Overloading [gram.over] */
2047
2048 static tree cp_parser_operator_function_id
2049   (cp_parser *);
2050 static tree cp_parser_operator
2051   (cp_parser *);
2052
2053 /* Templates [gram.temp] */
2054
2055 static void cp_parser_template_declaration
2056   (cp_parser *, bool);
2057 static tree cp_parser_template_parameter_list
2058   (cp_parser *);
2059 static tree cp_parser_template_parameter
2060   (cp_parser *, bool *, bool *);
2061 static tree cp_parser_type_parameter
2062   (cp_parser *, bool *);
2063 static tree cp_parser_template_id
2064   (cp_parser *, bool, bool, bool);
2065 static tree cp_parser_template_name
2066   (cp_parser *, bool, bool, bool, bool *);
2067 static tree cp_parser_template_argument_list
2068   (cp_parser *);
2069 static tree cp_parser_template_argument
2070   (cp_parser *);
2071 static void cp_parser_explicit_instantiation
2072   (cp_parser *);
2073 static void cp_parser_explicit_specialization
2074   (cp_parser *);
2075
2076 /* Exception handling [gram.exception] */
2077
2078 static tree cp_parser_try_block
2079   (cp_parser *);
2080 static bool cp_parser_function_try_block
2081   (cp_parser *);
2082 static void cp_parser_handler_seq
2083   (cp_parser *);
2084 static void cp_parser_handler
2085   (cp_parser *);
2086 static tree cp_parser_exception_declaration
2087   (cp_parser *);
2088 static tree cp_parser_throw_expression
2089   (cp_parser *);
2090 static tree cp_parser_exception_specification_opt
2091   (cp_parser *);
2092 static tree cp_parser_type_id_list
2093   (cp_parser *);
2094
2095 /* GNU Extensions */
2096
2097 static tree cp_parser_asm_specification_opt
2098   (cp_parser *);
2099 static tree cp_parser_asm_operand_list
2100   (cp_parser *);
2101 static tree cp_parser_asm_clobber_list
2102   (cp_parser *);
2103 static tree cp_parser_asm_label_list
2104   (cp_parser *);
2105 static tree cp_parser_attributes_opt
2106   (cp_parser *);
2107 static tree cp_parser_attribute_list
2108   (cp_parser *);
2109 static bool cp_parser_extension_opt
2110   (cp_parser *, int *);
2111 static void cp_parser_label_declaration
2112   (cp_parser *);
2113
2114 /* Transactional Memory Extensions */
2115
2116 static tree cp_parser_transaction
2117   (cp_parser *, enum rid);
2118 static tree cp_parser_transaction_expression
2119   (cp_parser *, enum rid);
2120 static bool cp_parser_function_transaction
2121   (cp_parser *, enum rid);
2122 static tree cp_parser_transaction_cancel
2123   (cp_parser *);
2124
2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2126 static bool cp_parser_pragma
2127   (cp_parser *, enum pragma_context);
2128
2129 /* Objective-C++ Productions */
2130
2131 static tree cp_parser_objc_message_receiver
2132   (cp_parser *);
2133 static tree cp_parser_objc_message_args
2134   (cp_parser *);
2135 static tree cp_parser_objc_message_expression
2136   (cp_parser *);
2137 static tree cp_parser_objc_encode_expression
2138   (cp_parser *);
2139 static tree cp_parser_objc_defs_expression
2140   (cp_parser *);
2141 static tree cp_parser_objc_protocol_expression
2142   (cp_parser *);
2143 static tree cp_parser_objc_selector_expression
2144   (cp_parser *);
2145 static tree cp_parser_objc_expression
2146   (cp_parser *);
2147 static bool cp_parser_objc_selector_p
2148   (enum cpp_ttype);
2149 static tree cp_parser_objc_selector
2150   (cp_parser *);
2151 static tree cp_parser_objc_protocol_refs_opt
2152   (cp_parser *);
2153 static void cp_parser_objc_declaration
2154   (cp_parser *, tree);
2155 static tree cp_parser_objc_statement
2156   (cp_parser *);
2157 static bool cp_parser_objc_valid_prefix_attributes
2158   (cp_parser *, tree *);
2159 static void cp_parser_objc_at_property_declaration 
2160   (cp_parser *) ;
2161 static void cp_parser_objc_at_synthesize_declaration 
2162   (cp_parser *) ;
2163 static void cp_parser_objc_at_dynamic_declaration
2164   (cp_parser *) ;
2165 static tree cp_parser_objc_struct_declaration
2166   (cp_parser *) ;
2167
2168 /* Utility Routines */
2169
2170 static tree cp_parser_lookup_name
2171   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2172 static tree cp_parser_lookup_name_simple
2173   (cp_parser *, tree, location_t);
2174 static tree cp_parser_maybe_treat_template_as_class
2175   (tree, bool);
2176 static bool cp_parser_check_declarator_template_parameters
2177   (cp_parser *, cp_declarator *, location_t);
2178 static bool cp_parser_check_template_parameters
2179   (cp_parser *, unsigned, location_t, cp_declarator *);
2180 static tree cp_parser_simple_cast_expression
2181   (cp_parser *);
2182 static tree cp_parser_global_scope_opt
2183   (cp_parser *, bool);
2184 static bool cp_parser_constructor_declarator_p
2185   (cp_parser *, bool);
2186 static tree cp_parser_function_definition_from_specifiers_and_declarator
2187   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2188 static tree cp_parser_function_definition_after_declarator
2189   (cp_parser *, bool);
2190 static void cp_parser_template_declaration_after_export
2191   (cp_parser *, bool);
2192 static void cp_parser_perform_template_parameter_access_checks
2193   (VEC (deferred_access_check,gc)*);
2194 static tree cp_parser_single_declaration
2195   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2196 static tree cp_parser_functional_cast
2197   (cp_parser *, tree);
2198 static tree cp_parser_save_member_function_body
2199   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2200 static tree cp_parser_save_nsdmi
2201   (cp_parser *);
2202 static tree cp_parser_enclosed_template_argument_list
2203   (cp_parser *);
2204 static void cp_parser_save_default_args
2205   (cp_parser *, tree);
2206 static void cp_parser_late_parsing_for_member
2207   (cp_parser *, tree);
2208 static tree cp_parser_late_parse_one_default_arg
2209   (cp_parser *, tree, tree, tree);
2210 static void cp_parser_late_parsing_nsdmi
2211   (cp_parser *, tree);
2212 static void cp_parser_late_parsing_default_args
2213   (cp_parser *, tree);
2214 static tree cp_parser_sizeof_operand
2215   (cp_parser *, enum rid);
2216 static tree cp_parser_trait_expr
2217   (cp_parser *, enum rid);
2218 static bool cp_parser_declares_only_class_p
2219   (cp_parser *);
2220 static void cp_parser_set_storage_class
2221   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2222 static void cp_parser_set_decl_spec_type
2223   (cp_decl_specifier_seq *, tree, location_t, bool);
2224 static bool cp_parser_friend_p
2225   (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227   (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229   (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231   (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2233   (cp_token *);
2234 static bool cp_parser_next_token_starts_class_definition_p
2235   (cp_parser *);
2236 static bool cp_parser_next_token_ends_template_argument_p
2237   (cp_parser *);
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239   (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2241   (cp_token *);
2242 static void cp_parser_check_class_key
2243   (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245   (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2247   (cp_parser *);
2248 static void cp_parser_pre_parsed_nested_name_specifier
2249   (cp_parser *);
2250 static bool cp_parser_cache_group
2251   (cp_parser *, enum cpp_ttype, unsigned);
2252 static tree cp_parser_cache_defarg
2253   (cp_parser *parser, bool nsdmi);
2254 static void cp_parser_parse_tentatively
2255   (cp_parser *);
2256 static void cp_parser_commit_to_tentative_parse
2257   (cp_parser *);
2258 static void cp_parser_abort_tentative_parse
2259   (cp_parser *);
2260 static bool cp_parser_parse_definitely
2261   (cp_parser *);
2262 static inline bool cp_parser_parsing_tentatively
2263   (cp_parser *);
2264 static bool cp_parser_uncommitted_to_tentative_parse_p
2265   (cp_parser *);
2266 static void cp_parser_error
2267   (cp_parser *, const char *);
2268 static void cp_parser_name_lookup_error
2269   (cp_parser *, tree, tree, name_lookup_error, location_t);
2270 static bool cp_parser_simulate_error
2271   (cp_parser *);
2272 static bool cp_parser_check_type_definition
2273   (cp_parser *);
2274 static void cp_parser_check_for_definition_in_return_type
2275   (cp_declarator *, tree, location_t type_location);
2276 static void cp_parser_check_for_invalid_template_id
2277   (cp_parser *, tree, location_t location);
2278 static bool cp_parser_non_integral_constant_expression
2279   (cp_parser *, non_integral_constant);
2280 static void cp_parser_diagnose_invalid_type_name
2281   (cp_parser *, tree, tree, location_t);
2282 static bool cp_parser_parse_and_diagnose_invalid_type_name
2283   (cp_parser *);
2284 static int cp_parser_skip_to_closing_parenthesis
2285   (cp_parser *, bool, bool, bool);
2286 static void cp_parser_skip_to_end_of_statement
2287   (cp_parser *);
2288 static void cp_parser_consume_semicolon_at_end_of_statement
2289   (cp_parser *);
2290 static void cp_parser_skip_to_end_of_block_or_statement
2291   (cp_parser *);
2292 static bool cp_parser_skip_to_closing_brace
2293   (cp_parser *);
2294 static void cp_parser_skip_to_end_of_template_parameter_list
2295   (cp_parser *);
2296 static void cp_parser_skip_to_pragma_eol
2297   (cp_parser*, cp_token *);
2298 static bool cp_parser_error_occurred
2299   (cp_parser *);
2300 static bool cp_parser_allow_gnu_extensions_p
2301   (cp_parser *);
2302 static bool cp_parser_is_pure_string_literal
2303   (cp_token *);
2304 static bool cp_parser_is_string_literal
2305   (cp_token *);
2306 static bool cp_parser_is_keyword
2307   (cp_token *, enum rid);
2308 static tree cp_parser_make_typename_type
2309   (cp_parser *, tree, tree, location_t location);
2310 static cp_declarator * cp_parser_make_indirect_declarator
2311   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2312
2313 /* Returns nonzero if we are parsing tentatively.  */
2314
2315 static inline bool
2316 cp_parser_parsing_tentatively (cp_parser* parser)
2317 {
2318   return parser->context->next != NULL;
2319 }
2320
2321 /* Returns nonzero if TOKEN is a string literal.  */
2322
2323 static bool
2324 cp_parser_is_pure_string_literal (cp_token* token)
2325 {
2326   return (token->type == CPP_STRING ||
2327           token->type == CPP_STRING16 ||
2328           token->type == CPP_STRING32 ||
2329           token->type == CPP_WSTRING ||
2330           token->type == CPP_UTF8STRING);
2331 }
2332
2333 /* Returns nonzero if TOKEN is a string literal
2334    of a user-defined string literal.  */
2335
2336 static bool
2337 cp_parser_is_string_literal (cp_token* token)
2338 {
2339   return (cp_parser_is_pure_string_literal (token) ||
2340           token->type == CPP_STRING_USERDEF ||
2341           token->type == CPP_STRING16_USERDEF ||
2342           token->type == CPP_STRING32_USERDEF ||
2343           token->type == CPP_WSTRING_USERDEF ||
2344           token->type == CPP_UTF8STRING_USERDEF);
2345 }
2346
2347 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2348
2349 static bool
2350 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2351 {
2352   return token->keyword == keyword;
2353 }
2354
2355 /* If not parsing tentatively, issue a diagnostic of the form
2356       FILE:LINE: MESSAGE before TOKEN
2357    where TOKEN is the next token in the input stream.  MESSAGE
2358    (specified by the caller) is usually of the form "expected
2359    OTHER-TOKEN".  */
2360
2361 static void
2362 cp_parser_error (cp_parser* parser, const char* gmsgid)
2363 {
2364   if (!cp_parser_simulate_error (parser))
2365     {
2366       cp_token *token = cp_lexer_peek_token (parser->lexer);
2367       /* This diagnostic makes more sense if it is tagged to the line
2368          of the token we just peeked at.  */
2369       cp_lexer_set_source_position_from_token (token);
2370
2371       if (token->type == CPP_PRAGMA)
2372         {
2373           error_at (token->location,
2374                     "%<#pragma%> is not allowed here");
2375           cp_parser_skip_to_pragma_eol (parser, token);
2376           return;
2377         }
2378
2379       c_parse_error (gmsgid,
2380                      /* Because c_parser_error does not understand
2381                         CPP_KEYWORD, keywords are treated like
2382                         identifiers.  */
2383                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2384                      token->u.value, token->flags);
2385     }
2386 }
2387
2388 /* Issue an error about name-lookup failing.  NAME is the
2389    IDENTIFIER_NODE DECL is the result of
2390    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2391    the thing that we hoped to find.  */
2392
2393 static void
2394 cp_parser_name_lookup_error (cp_parser* parser,
2395                              tree name,
2396                              tree decl,
2397                              name_lookup_error desired,
2398                              location_t location)
2399 {
2400   /* If name lookup completely failed, tell the user that NAME was not
2401      declared.  */
2402   if (decl == error_mark_node)
2403     {
2404       if (parser->scope && parser->scope != global_namespace)
2405         error_at (location, "%<%E::%E%> has not been declared",
2406                   parser->scope, name);
2407       else if (parser->scope == global_namespace)
2408         error_at (location, "%<::%E%> has not been declared", name);
2409       else if (parser->object_scope
2410                && !CLASS_TYPE_P (parser->object_scope))
2411         error_at (location, "request for member %qE in non-class type %qT",
2412                   name, parser->object_scope);
2413       else if (parser->object_scope)
2414         error_at (location, "%<%T::%E%> has not been declared",
2415                   parser->object_scope, name);
2416       else
2417         error_at (location, "%qE has not been declared", name);
2418     }
2419   else if (parser->scope && parser->scope != global_namespace)
2420     {
2421       switch (desired)
2422         {
2423           case NLE_TYPE:
2424             error_at (location, "%<%E::%E%> is not a type",
2425                                 parser->scope, name);
2426             break;
2427           case NLE_CXX98:
2428             error_at (location, "%<%E::%E%> is not a class or namespace",
2429                                 parser->scope, name);
2430             break;
2431           case NLE_NOT_CXX98:
2432             error_at (location,
2433                       "%<%E::%E%> is not a class, namespace, or enumeration",
2434                       parser->scope, name);
2435             break;
2436           default:
2437             gcc_unreachable ();
2438             
2439         }
2440     }
2441   else if (parser->scope == global_namespace)
2442     {
2443       switch (desired)
2444         {
2445           case NLE_TYPE:
2446             error_at (location, "%<::%E%> is not a type", name);
2447             break;
2448           case NLE_CXX98:
2449             error_at (location, "%<::%E%> is not a class or namespace", name);
2450             break;
2451           case NLE_NOT_CXX98:
2452             error_at (location,
2453                       "%<::%E%> is not a class, namespace, or enumeration",
2454                       name);
2455             break;
2456           default:
2457             gcc_unreachable ();
2458         }
2459     }
2460   else
2461     {
2462       switch (desired)
2463         {
2464           case NLE_TYPE:
2465             error_at (location, "%qE is not a type", name);
2466             break;
2467           case NLE_CXX98:
2468             error_at (location, "%qE is not a class or namespace", name);
2469             break;
2470           case NLE_NOT_CXX98:
2471             error_at (location,
2472                       "%qE is not a class, namespace, or enumeration", name);
2473             break;
2474           default:
2475             gcc_unreachable ();
2476         }
2477     }
2478 }
2479
2480 /* If we are parsing tentatively, remember that an error has occurred
2481    during this tentative parse.  Returns true if the error was
2482    simulated; false if a message should be issued by the caller.  */
2483
2484 static bool
2485 cp_parser_simulate_error (cp_parser* parser)
2486 {
2487   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2488     {
2489       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2490       return true;
2491     }
2492   return false;
2493 }
2494
2495 /* Check for repeated decl-specifiers.  */
2496
2497 static void
2498 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2499                            location_t location)
2500 {
2501   int ds;
2502
2503   for (ds = ds_first; ds != ds_last; ++ds)
2504     {
2505       unsigned count = decl_specs->specs[ds];
2506       if (count < 2)
2507         continue;
2508       /* The "long" specifier is a special case because of "long long".  */
2509       if (ds == ds_long)
2510         {
2511           if (count > 2)
2512             error_at (location, "%<long long long%> is too long for GCC");
2513           else 
2514             pedwarn_cxx98 (location, OPT_Wlong_long, 
2515                            "ISO C++ 1998 does not support %<long long%>");
2516         }
2517       else if (count > 1)
2518         {
2519           static const char *const decl_spec_names[] = {
2520             "signed",
2521             "unsigned",
2522             "short",
2523             "long",
2524             "const",
2525             "volatile",
2526             "restrict",
2527             "inline",
2528             "virtual",
2529             "explicit",
2530             "friend",
2531             "typedef",
2532             "using",
2533             "constexpr",
2534             "__complex",
2535             "__thread"
2536           };
2537           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2538         }
2539     }
2540 }
2541
2542 /* This function is called when a type is defined.  If type
2543    definitions are forbidden at this point, an error message is
2544    issued.  */
2545
2546 static bool
2547 cp_parser_check_type_definition (cp_parser* parser)
2548 {
2549   /* If types are forbidden here, issue a message.  */
2550   if (parser->type_definition_forbidden_message)
2551     {
2552       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2553          in the message need to be interpreted.  */
2554       error (parser->type_definition_forbidden_message);
2555       return false;
2556     }
2557   return true;
2558 }
2559
2560 /* This function is called when the DECLARATOR is processed.  The TYPE
2561    was a type defined in the decl-specifiers.  If it is invalid to
2562    define a type in the decl-specifiers for DECLARATOR, an error is
2563    issued. TYPE_LOCATION is the location of TYPE and is used
2564    for error reporting.  */
2565
2566 static void
2567 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2568                                                tree type, location_t type_location)
2569 {
2570   /* [dcl.fct] forbids type definitions in return types.
2571      Unfortunately, it's not easy to know whether or not we are
2572      processing a return type until after the fact.  */
2573   while (declarator
2574          && (declarator->kind == cdk_pointer
2575              || declarator->kind == cdk_reference
2576              || declarator->kind == cdk_ptrmem))
2577     declarator = declarator->declarator;
2578   if (declarator
2579       && declarator->kind == cdk_function)
2580     {
2581       error_at (type_location,
2582                 "new types may not be defined in a return type");
2583       inform (type_location, 
2584               "(perhaps a semicolon is missing after the definition of %qT)",
2585               type);
2586     }
2587 }
2588
2589 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2590    "<" in any valid C++ program.  If the next token is indeed "<",
2591    issue a message warning the user about what appears to be an
2592    invalid attempt to form a template-id. LOCATION is the location
2593    of the type-specifier (TYPE) */
2594
2595 static void
2596 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2597                                          tree type, location_t location)
2598 {
2599   cp_token_position start = 0;
2600
2601   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2602     {
2603       if (TYPE_P (type))
2604         error_at (location, "%qT is not a template", type);
2605       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2606         error_at (location, "%qE is not a template", type);
2607       else
2608         error_at (location, "invalid template-id");
2609       /* Remember the location of the invalid "<".  */
2610       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2611         start = cp_lexer_token_position (parser->lexer, true);
2612       /* Consume the "<".  */
2613       cp_lexer_consume_token (parser->lexer);
2614       /* Parse the template arguments.  */
2615       cp_parser_enclosed_template_argument_list (parser);
2616       /* Permanently remove the invalid template arguments so that
2617          this error message is not issued again.  */
2618       if (start)
2619         cp_lexer_purge_tokens_after (parser->lexer, start);
2620     }
2621 }
2622
2623 /* If parsing an integral constant-expression, issue an error message
2624    about the fact that THING appeared and return true.  Otherwise,
2625    return false.  In either case, set
2626    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2627
2628 static bool
2629 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2630                                             non_integral_constant thing)
2631 {
2632   parser->non_integral_constant_expression_p = true;
2633   if (parser->integral_constant_expression_p)
2634     {
2635       if (!parser->allow_non_integral_constant_expression_p)
2636         {
2637           const char *msg = NULL;
2638           switch (thing)
2639             {
2640               case NIC_FLOAT:
2641                 error ("floating-point literal "
2642                        "cannot appear in a constant-expression");
2643                 return true;
2644               case NIC_CAST:
2645                 error ("a cast to a type other than an integral or "
2646                        "enumeration type cannot appear in a "
2647                        "constant-expression");
2648                 return true;
2649               case NIC_TYPEID:
2650                 error ("%<typeid%> operator "
2651                        "cannot appear in a constant-expression");
2652                 return true;
2653               case NIC_NCC:
2654                 error ("non-constant compound literals "
2655                        "cannot appear in a constant-expression");
2656                 return true;
2657               case NIC_FUNC_CALL:
2658                 error ("a function call "
2659                        "cannot appear in a constant-expression");
2660                 return true;
2661               case NIC_INC:
2662                 error ("an increment "
2663                        "cannot appear in a constant-expression");
2664                 return true;
2665               case NIC_DEC:
2666                 error ("an decrement "
2667                        "cannot appear in a constant-expression");
2668                 return true;
2669               case NIC_ARRAY_REF:
2670                 error ("an array reference "
2671                        "cannot appear in a constant-expression");
2672                 return true;
2673               case NIC_ADDR_LABEL:
2674                 error ("the address of a label "
2675                        "cannot appear in a constant-expression");
2676                 return true;
2677               case NIC_OVERLOADED:
2678                 error ("calls to overloaded operators "
2679                        "cannot appear in a constant-expression");
2680                 return true;
2681               case NIC_ASSIGNMENT:
2682                 error ("an assignment cannot appear in a constant-expression");
2683                 return true;
2684               case NIC_COMMA:
2685                 error ("a comma operator "
2686                        "cannot appear in a constant-expression");
2687                 return true;
2688               case NIC_CONSTRUCTOR:
2689                 error ("a call to a constructor "
2690                        "cannot appear in a constant-expression");
2691                 return true;
2692               case NIC_TRANSACTION:
2693                 error ("a transaction expression "
2694                        "cannot appear in a constant-expression");
2695                 return true;
2696               case NIC_THIS:
2697                 msg = "this";
2698                 break;
2699               case NIC_FUNC_NAME:
2700                 msg = "__FUNCTION__";
2701                 break;
2702               case NIC_PRETTY_FUNC:
2703                 msg = "__PRETTY_FUNCTION__";
2704                 break;
2705               case NIC_C99_FUNC:
2706                 msg = "__func__";
2707                 break;
2708               case NIC_VA_ARG:
2709                 msg = "va_arg";
2710                 break;
2711               case NIC_ARROW:
2712                 msg = "->";
2713                 break;
2714               case NIC_POINT:
2715                 msg = ".";
2716                 break;
2717               case NIC_STAR:
2718                 msg = "*";
2719                 break;
2720               case NIC_ADDR:
2721                 msg = "&";
2722                 break;
2723               case NIC_PREINCREMENT:
2724                 msg = "++";
2725                 break;
2726               case NIC_PREDECREMENT:
2727                 msg = "--";
2728                 break;
2729               case NIC_NEW:
2730                 msg = "new";
2731                 break;
2732               case NIC_DEL:
2733                 msg = "delete";
2734                 break;
2735               default:
2736                 gcc_unreachable ();
2737             }
2738           if (msg)
2739             error ("%qs cannot appear in a constant-expression", msg);
2740           return true;
2741         }
2742     }
2743   return false;
2744 }
2745
2746 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2747    qualifying scope (or NULL, if none) for ID.  This function commits
2748    to the current active tentative parse, if any.  (Otherwise, the
2749    problematic construct might be encountered again later, resulting
2750    in duplicate error messages.) LOCATION is the location of ID.  */
2751
2752 static void
2753 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2754                                       tree scope, tree id,
2755                                       location_t location)
2756 {
2757   tree decl, old_scope;
2758   cp_parser_commit_to_tentative_parse (parser);
2759   /* Try to lookup the identifier.  */
2760   old_scope = parser->scope;
2761   parser->scope = scope;
2762   decl = cp_parser_lookup_name_simple (parser, id, location);
2763   parser->scope = old_scope;
2764   /* If the lookup found a template-name, it means that the user forgot
2765   to specify an argument list. Emit a useful error message.  */
2766   if (TREE_CODE (decl) == TEMPLATE_DECL)
2767     error_at (location,
2768               "invalid use of template-name %qE without an argument list",
2769               decl);
2770   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2771     error_at (location, "invalid use of destructor %qD as a type", id);
2772   else if (TREE_CODE (decl) == TYPE_DECL)
2773     /* Something like 'unsigned A a;'  */
2774     error_at (location, "invalid combination of multiple type-specifiers");
2775   else if (!parser->scope)
2776     {
2777       /* Issue an error message.  */
2778       error_at (location, "%qE does not name a type", id);
2779       /* If we're in a template class, it's possible that the user was
2780          referring to a type from a base class.  For example:
2781
2782            template <typename T> struct A { typedef T X; };
2783            template <typename T> struct B : public A<T> { X x; };
2784
2785          The user should have said "typename A<T>::X".  */
2786       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2787         inform (location, "C++11 %<constexpr%> only available with "
2788                 "-std=c++11 or -std=gnu++11");
2789       else if (processing_template_decl && current_class_type
2790                && TYPE_BINFO (current_class_type))
2791         {
2792           tree b;
2793
2794           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2795                b;
2796                b = TREE_CHAIN (b))
2797             {
2798               tree base_type = BINFO_TYPE (b);
2799               if (CLASS_TYPE_P (base_type)
2800                   && dependent_type_p (base_type))
2801                 {
2802                   tree field;
2803                   /* Go from a particular instantiation of the
2804                      template (which will have an empty TYPE_FIELDs),
2805                      to the main version.  */
2806                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2807                   for (field = TYPE_FIELDS (base_type);
2808                        field;
2809                        field = DECL_CHAIN (field))
2810                     if (TREE_CODE (field) == TYPE_DECL
2811                         && DECL_NAME (field) == id)
2812                       {
2813                         inform (location, 
2814                                 "(perhaps %<typename %T::%E%> was intended)",
2815                                 BINFO_TYPE (b), id);
2816                         break;
2817                       }
2818                   if (field)
2819                     break;
2820                 }
2821             }
2822         }
2823     }
2824   /* Here we diagnose qualified-ids where the scope is actually correct,
2825      but the identifier does not resolve to a valid type name.  */
2826   else if (parser->scope != error_mark_node)
2827     {
2828       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2829         error_at (location, "%qE in namespace %qE does not name a type",
2830                   id, parser->scope);
2831       else if (CLASS_TYPE_P (parser->scope)
2832                && constructor_name_p (id, parser->scope))
2833         {
2834           /* A<T>::A<T>() */
2835           error_at (location, "%<%T::%E%> names the constructor, not"
2836                     " the type", parser->scope, id);
2837           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2838             error_at (location, "and %qT has no template constructors",
2839                       parser->scope);
2840         }
2841       else if (TYPE_P (parser->scope)
2842                && dependent_scope_p (parser->scope))
2843         error_at (location, "need %<typename%> before %<%T::%E%> because "
2844                   "%qT is a dependent scope",
2845                   parser->scope, id, parser->scope);
2846       else if (TYPE_P (parser->scope))
2847         error_at (location, "%qE in %q#T does not name a type",
2848                   id, parser->scope);
2849       else
2850         gcc_unreachable ();
2851     }
2852 }
2853
2854 /* Check for a common situation where a type-name should be present,
2855    but is not, and issue a sensible error message.  Returns true if an
2856    invalid type-name was detected.
2857
2858    The situation handled by this function are variable declarations of the
2859    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2860    Usually, `ID' should name a type, but if we got here it means that it
2861    does not. We try to emit the best possible error message depending on
2862    how exactly the id-expression looks like.  */
2863
2864 static bool
2865 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2866 {
2867   tree id;
2868   cp_token *token = cp_lexer_peek_token (parser->lexer);
2869
2870   /* Avoid duplicate error about ambiguous lookup.  */
2871   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2872     {
2873       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2874       if (next->type == CPP_NAME && next->ambiguous_p)
2875         goto out;
2876     }
2877
2878   cp_parser_parse_tentatively (parser);
2879   id = cp_parser_id_expression (parser,
2880                                 /*template_keyword_p=*/false,
2881                                 /*check_dependency_p=*/true,
2882                                 /*template_p=*/NULL,
2883                                 /*declarator_p=*/true,
2884                                 /*optional_p=*/false);
2885   /* If the next token is a (, this is a function with no explicit return
2886      type, i.e. constructor, destructor or conversion op.  */
2887   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2888       || TREE_CODE (id) == TYPE_DECL)
2889     {
2890       cp_parser_abort_tentative_parse (parser);
2891       return false;
2892     }
2893   if (!cp_parser_parse_definitely (parser))
2894     return false;
2895
2896   /* Emit a diagnostic for the invalid type.  */
2897   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2898                                         id, token->location);
2899  out:
2900   /* If we aren't in the middle of a declarator (i.e. in a
2901      parameter-declaration-clause), skip to the end of the declaration;
2902      there's no point in trying to process it.  */
2903   if (!parser->in_declarator_p)
2904     cp_parser_skip_to_end_of_block_or_statement (parser);
2905   return true;
2906 }
2907
2908 /* Consume tokens up to, and including, the next non-nested closing `)'.
2909    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2910    are doing error recovery. Returns -1 if OR_COMMA is true and we
2911    found an unnested comma.  */
2912
2913 static int
2914 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2915                                        bool recovering,
2916                                        bool or_comma,
2917                                        bool consume_paren)
2918 {
2919   unsigned paren_depth = 0;
2920   unsigned brace_depth = 0;
2921   unsigned square_depth = 0;
2922
2923   if (recovering && !or_comma
2924       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2925     return 0;
2926
2927   while (true)
2928     {
2929       cp_token * token = cp_lexer_peek_token (parser->lexer);
2930
2931       switch (token->type)
2932         {
2933         case CPP_EOF:
2934         case CPP_PRAGMA_EOL:
2935           /* If we've run out of tokens, then there is no closing `)'.  */
2936           return 0;
2937
2938         /* This is good for lambda expression capture-lists.  */
2939         case CPP_OPEN_SQUARE:
2940           ++square_depth;
2941           break;
2942         case CPP_CLOSE_SQUARE:
2943           if (!square_depth--)
2944             return 0;
2945           break;
2946
2947         case CPP_SEMICOLON:
2948           /* This matches the processing in skip_to_end_of_statement.  */
2949           if (!brace_depth)
2950             return 0;
2951           break;
2952
2953         case CPP_OPEN_BRACE:
2954           ++brace_depth;
2955           break;
2956         case CPP_CLOSE_BRACE:
2957           if (!brace_depth--)
2958             return 0;
2959           break;
2960
2961         case CPP_COMMA:
2962           if (recovering && or_comma && !brace_depth && !paren_depth
2963               && !square_depth)
2964             return -1;
2965           break;
2966
2967         case CPP_OPEN_PAREN:
2968           if (!brace_depth)
2969             ++paren_depth;
2970           break;
2971
2972         case CPP_CLOSE_PAREN:
2973           if (!brace_depth && !paren_depth--)
2974             {
2975               if (consume_paren)
2976                 cp_lexer_consume_token (parser->lexer);
2977               return 1;
2978             }
2979           break;
2980
2981         default:
2982           break;
2983         }
2984
2985       /* Consume the token.  */
2986       cp_lexer_consume_token (parser->lexer);
2987     }
2988 }
2989
2990 /* Consume tokens until we reach the end of the current statement.
2991    Normally, that will be just before consuming a `;'.  However, if a
2992    non-nested `}' comes first, then we stop before consuming that.  */
2993
2994 static void
2995 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2996 {
2997   unsigned nesting_depth = 0;
2998
2999   while (true)
3000     {
3001       cp_token *token = cp_lexer_peek_token (parser->lexer);
3002
3003       switch (token->type)
3004         {
3005         case CPP_EOF:
3006         case CPP_PRAGMA_EOL:
3007           /* If we've run out of tokens, stop.  */
3008           return;
3009
3010         case CPP_SEMICOLON:
3011           /* If the next token is a `;', we have reached the end of the
3012              statement.  */
3013           if (!nesting_depth)
3014             return;
3015           break;
3016
3017         case CPP_CLOSE_BRACE:
3018           /* If this is a non-nested '}', stop before consuming it.
3019              That way, when confronted with something like:
3020
3021                { 3 + }
3022
3023              we stop before consuming the closing '}', even though we
3024              have not yet reached a `;'.  */
3025           if (nesting_depth == 0)
3026             return;
3027
3028           /* If it is the closing '}' for a block that we have
3029              scanned, stop -- but only after consuming the token.
3030              That way given:
3031
3032                 void f g () { ... }
3033                 typedef int I;
3034
3035              we will stop after the body of the erroneously declared
3036              function, but before consuming the following `typedef'
3037              declaration.  */
3038           if (--nesting_depth == 0)
3039             {
3040               cp_lexer_consume_token (parser->lexer);
3041               return;
3042             }
3043
3044         case CPP_OPEN_BRACE:
3045           ++nesting_depth;
3046           break;
3047
3048         default:
3049           break;
3050         }
3051
3052       /* Consume the token.  */
3053       cp_lexer_consume_token (parser->lexer);
3054     }
3055 }
3056
3057 /* This function is called at the end of a statement or declaration.
3058    If the next token is a semicolon, it is consumed; otherwise, error
3059    recovery is attempted.  */
3060
3061 static void
3062 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3063 {
3064   /* Look for the trailing `;'.  */
3065   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3066     {
3067       /* If there is additional (erroneous) input, skip to the end of
3068          the statement.  */
3069       cp_parser_skip_to_end_of_statement (parser);
3070       /* If the next token is now a `;', consume it.  */
3071       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3072         cp_lexer_consume_token (parser->lexer);
3073     }
3074 }
3075
3076 /* Skip tokens until we have consumed an entire block, or until we
3077    have consumed a non-nested `;'.  */
3078
3079 static void
3080 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3081 {
3082   int nesting_depth = 0;
3083
3084   while (nesting_depth >= 0)
3085     {
3086       cp_token *token = cp_lexer_peek_token (parser->lexer);
3087
3088       switch (token->type)
3089         {
3090         case CPP_EOF:
3091         case CPP_PRAGMA_EOL:
3092           /* If we've run out of tokens, stop.  */
3093           return;
3094
3095         case CPP_SEMICOLON:
3096           /* Stop if this is an unnested ';'. */
3097           if (!nesting_depth)
3098             nesting_depth = -1;
3099           break;
3100
3101         case CPP_CLOSE_BRACE:
3102           /* Stop if this is an unnested '}', or closes the outermost
3103              nesting level.  */
3104           nesting_depth--;
3105           if (nesting_depth < 0)
3106             return;
3107           if (!nesting_depth)
3108             nesting_depth = -1;
3109           break;
3110
3111         case CPP_OPEN_BRACE:
3112           /* Nest. */
3113           nesting_depth++;
3114           break;
3115
3116         default:
3117           break;
3118         }
3119
3120       /* Consume the token.  */
3121       cp_lexer_consume_token (parser->lexer);
3122     }
3123 }
3124
3125 /* Skip tokens until a non-nested closing curly brace is the next
3126    token, or there are no more tokens. Return true in the first case,
3127    false otherwise.  */
3128
3129 static bool
3130 cp_parser_skip_to_closing_brace (cp_parser *parser)
3131 {
3132   unsigned nesting_depth = 0;
3133
3134   while (true)
3135     {
3136       cp_token *token = cp_lexer_peek_token (parser->lexer);
3137
3138       switch (token->type)
3139         {
3140         case CPP_EOF:
3141         case CPP_PRAGMA_EOL:
3142           /* If we've run out of tokens, stop.  */
3143           return false;
3144
3145         case CPP_CLOSE_BRACE:
3146           /* If the next token is a non-nested `}', then we have reached
3147              the end of the current block.  */
3148           if (nesting_depth-- == 0)
3149             return true;
3150           break;
3151
3152         case CPP_OPEN_BRACE:
3153           /* If it the next token is a `{', then we are entering a new
3154              block.  Consume the entire block.  */
3155           ++nesting_depth;
3156           break;
3157
3158         default:
3159           break;
3160         }
3161
3162       /* Consume the token.  */
3163       cp_lexer_consume_token (parser->lexer);
3164     }
3165 }
3166
3167 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3168    parameter is the PRAGMA token, allowing us to purge the entire pragma
3169    sequence.  */
3170
3171 static void
3172 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3173 {
3174   cp_token *token;
3175
3176   parser->lexer->in_pragma = false;
3177
3178   do
3179     token = cp_lexer_consume_token (parser->lexer);
3180   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3181
3182   /* Ensure that the pragma is not parsed again.  */
3183   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3184 }
3185
3186 /* Require pragma end of line, resyncing with it as necessary.  The
3187    arguments are as for cp_parser_skip_to_pragma_eol.  */
3188
3189 static void
3190 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3191 {
3192   parser->lexer->in_pragma = false;
3193   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3194     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3195 }
3196
3197 /* This is a simple wrapper around make_typename_type. When the id is
3198    an unresolved identifier node, we can provide a superior diagnostic
3199    using cp_parser_diagnose_invalid_type_name.  */
3200
3201 static tree
3202 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3203                               tree id, location_t id_location)
3204 {
3205   tree result;
3206   if (TREE_CODE (id) == IDENTIFIER_NODE)
3207     {
3208       result = make_typename_type (scope, id, typename_type,
3209                                    /*complain=*/tf_none);
3210       if (result == error_mark_node)
3211         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3212       return result;
3213     }
3214   return make_typename_type (scope, id, typename_type, tf_error);
3215 }
3216
3217 /* This is a wrapper around the
3218    make_{pointer,ptrmem,reference}_declarator functions that decides
3219    which one to call based on the CODE and CLASS_TYPE arguments. The
3220    CODE argument should be one of the values returned by
3221    cp_parser_ptr_operator. */
3222 static cp_declarator *
3223 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3224                                     cp_cv_quals cv_qualifiers,
3225                                     cp_declarator *target)
3226 {
3227   if (code == ERROR_MARK)
3228     return cp_error_declarator;
3229
3230   if (code == INDIRECT_REF)
3231     if (class_type == NULL_TREE)
3232       return make_pointer_declarator (cv_qualifiers, target);
3233     else
3234       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3235   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3236     return make_reference_declarator (cv_qualifiers, target, false);
3237   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3238     return make_reference_declarator (cv_qualifiers, target, true);
3239   gcc_unreachable ();
3240 }
3241
3242 /* Create a new C++ parser.  */
3243
3244 static cp_parser *
3245 cp_parser_new (void)
3246 {
3247   cp_parser *parser;
3248   cp_lexer *lexer;
3249   unsigned i;
3250
3251   /* cp_lexer_new_main is called before doing GC allocation because
3252      cp_lexer_new_main might load a PCH file.  */
3253   lexer = cp_lexer_new_main ();
3254
3255   /* Initialize the binops_by_token so that we can get the tree
3256      directly from the token.  */
3257   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3258     binops_by_token[binops[i].token_type] = binops[i];
3259
3260   parser = ggc_alloc_cleared_cp_parser ();
3261   parser->lexer = lexer;
3262   parser->context = cp_parser_context_new (NULL);
3263
3264   /* For now, we always accept GNU extensions.  */
3265   parser->allow_gnu_extensions_p = 1;
3266
3267   /* The `>' token is a greater-than operator, not the end of a
3268      template-id.  */
3269   parser->greater_than_is_operator_p = true;
3270
3271   parser->default_arg_ok_p = true;
3272
3273   /* We are not parsing a constant-expression.  */
3274   parser->integral_constant_expression_p = false;
3275   parser->allow_non_integral_constant_expression_p = false;
3276   parser->non_integral_constant_expression_p = false;
3277
3278   /* Local variable names are not forbidden.  */
3279   parser->local_variables_forbidden_p = false;
3280
3281   /* We are not processing an `extern "C"' declaration.  */
3282   parser->in_unbraced_linkage_specification_p = false;
3283
3284   /* We are not processing a declarator.  */
3285   parser->in_declarator_p = false;
3286
3287   /* We are not processing a template-argument-list.  */
3288   parser->in_template_argument_list_p = false;
3289
3290   /* We are not in an iteration statement.  */
3291   parser->in_statement = 0;
3292
3293   /* We are not in a switch statement.  */
3294   parser->in_switch_statement_p = false;
3295
3296   /* We are not parsing a type-id inside an expression.  */
3297   parser->in_type_id_in_expr_p = false;
3298
3299   /* Declarations aren't implicitly extern "C".  */
3300   parser->implicit_extern_c = false;
3301
3302   /* String literals should be translated to the execution character set.  */
3303   parser->translate_strings_p = true;
3304
3305   /* We are not parsing a function body.  */
3306   parser->in_function_body = false;
3307
3308   /* We can correct until told otherwise.  */
3309   parser->colon_corrects_to_scope_p = true;
3310
3311   /* The unparsed function queue is empty.  */
3312   push_unparsed_function_queues (parser);
3313
3314   /* There are no classes being defined.  */
3315   parser->num_classes_being_defined = 0;
3316
3317   /* No template parameters apply.  */
3318   parser->num_template_parameter_lists = 0;
3319
3320   return parser;
3321 }
3322
3323 /* Create a cp_lexer structure which will emit the tokens in CACHE
3324    and push it onto the parser's lexer stack.  This is used for delayed
3325    parsing of in-class method bodies and default arguments, and should
3326    not be confused with tentative parsing.  */
3327 static void
3328 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3329 {
3330   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3331   lexer->next = parser->lexer;
3332   parser->lexer = lexer;
3333
3334   /* Move the current source position to that of the first token in the
3335      new lexer.  */
3336   cp_lexer_set_source_position_from_token (lexer->next_token);
3337 }
3338
3339 /* Pop the top lexer off the parser stack.  This is never used for the
3340    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3341 static void
3342 cp_parser_pop_lexer (cp_parser *parser)
3343 {
3344   cp_lexer *lexer = parser->lexer;
3345   parser->lexer = lexer->next;
3346   cp_lexer_destroy (lexer);
3347
3348   /* Put the current source position back where it was before this
3349      lexer was pushed.  */
3350   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3351 }
3352
3353 /* Lexical conventions [gram.lex]  */
3354
3355 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3356    identifier.  */
3357
3358 static tree
3359 cp_parser_identifier (cp_parser* parser)
3360 {
3361   cp_token *token;
3362
3363   /* Look for the identifier.  */
3364   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3365   /* Return the value.  */
3366   return token ? token->u.value : error_mark_node;
3367 }
3368
3369 /* Parse a sequence of adjacent string constants.  Returns a
3370    TREE_STRING representing the combined, nul-terminated string
3371    constant.  If TRANSLATE is true, translate the string to the
3372    execution character set.  If WIDE_OK is true, a wide string is
3373    invalid here.
3374
3375    C++98 [lex.string] says that if a narrow string literal token is
3376    adjacent to a wide string literal token, the behavior is undefined.
3377    However, C99 6.4.5p4 says that this results in a wide string literal.
3378    We follow C99 here, for consistency with the C front end.
3379
3380    This code is largely lifted from lex_string() in c-lex.c.
3381
3382    FUTURE: ObjC++ will need to handle @-strings here.  */
3383 static tree
3384 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3385 {
3386   tree value;
3387   size_t count;
3388   struct obstack str_ob;
3389   cpp_string str, istr, *strs;
3390   cp_token *tok;
3391   enum cpp_ttype type, curr_type;
3392   int have_suffix_p = 0;
3393   tree string_tree;
3394   tree suffix_id = NULL_TREE;
3395   bool curr_tok_is_userdef_p = false;
3396
3397   tok = cp_lexer_peek_token (parser->lexer);
3398   if (!cp_parser_is_string_literal (tok))
3399     {
3400       cp_parser_error (parser, "expected string-literal");
3401       return error_mark_node;
3402     }
3403
3404   if (cpp_userdef_string_p (tok->type))
3405     {
3406       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3407       curr_type = cpp_userdef_string_remove_type (tok->type);
3408       curr_tok_is_userdef_p = true;
3409     }
3410   else
3411     {
3412       string_tree = tok->u.value;
3413       curr_type = tok->type;
3414     }
3415   type = curr_type;
3416
3417   /* Try to avoid the overhead of creating and destroying an obstack
3418      for the common case of just one string.  */
3419   if (!cp_parser_is_string_literal
3420       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3421     {
3422       cp_lexer_consume_token (parser->lexer);
3423
3424       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3425       str.len = TREE_STRING_LENGTH (string_tree);
3426       count = 1;
3427
3428       if (curr_tok_is_userdef_p)
3429         {
3430           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3431           have_suffix_p = 1;
3432           curr_type = cpp_userdef_string_remove_type (tok->type);
3433         }
3434       else
3435         curr_type = tok->type;
3436
3437       strs = &str;
3438     }
3439   else
3440     {
3441       gcc_obstack_init (&str_ob);
3442       count = 0;
3443
3444       do
3445         {
3446           cp_lexer_consume_token (parser->lexer);
3447           count++;
3448           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3449           str.len = TREE_STRING_LENGTH (string_tree);
3450
3451           if (curr_tok_is_userdef_p)
3452             {
3453               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3454               if (have_suffix_p == 0)
3455                 {
3456                   suffix_id = curr_suffix_id;
3457                   have_suffix_p = 1;
3458                 }
3459               else if (have_suffix_p == 1
3460                        && curr_suffix_id != suffix_id)
3461                 {
3462                   error ("inconsistent user-defined literal suffixes"
3463                          " %qD and %qD in string literal",
3464                          suffix_id, curr_suffix_id);
3465                   have_suffix_p = -1;
3466                 }
3467               curr_type = cpp_userdef_string_remove_type (tok->type);
3468             }
3469           else
3470             curr_type = tok->type;
3471
3472           if (type != curr_type)
3473             {
3474               if (type == CPP_STRING)
3475                 type = curr_type;
3476               else if (curr_type != CPP_STRING)
3477                 error_at (tok->location,
3478                           "unsupported non-standard concatenation "
3479                           "of string literals");
3480             }
3481
3482           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3483
3484           tok = cp_lexer_peek_token (parser->lexer);
3485           if (cpp_userdef_string_p (tok->type))
3486             {
3487               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3488               curr_type = cpp_userdef_string_remove_type (tok->type);
3489               curr_tok_is_userdef_p = true;
3490             }
3491           else
3492             {
3493               string_tree = tok->u.value;
3494               curr_type = tok->type;
3495               curr_tok_is_userdef_p = false;
3496             }
3497         }
3498       while (cp_parser_is_string_literal (tok));
3499
3500       strs = (cpp_string *) obstack_finish (&str_ob);
3501     }
3502
3503   if (type != CPP_STRING && !wide_ok)
3504     {
3505       cp_parser_error (parser, "a wide string is invalid in this context");
3506       type = CPP_STRING;
3507     }
3508
3509   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3510       (parse_in, strs, count, &istr, type))
3511     {
3512       value = build_string (istr.len, (const char *)istr.text);
3513       free (CONST_CAST (unsigned char *, istr.text));
3514
3515       switch (type)
3516         {
3517         default:
3518         case CPP_STRING:
3519         case CPP_UTF8STRING:
3520           TREE_TYPE (value) = char_array_type_node;
3521           break;
3522         case CPP_STRING16:
3523           TREE_TYPE (value) = char16_array_type_node;
3524           break;
3525         case CPP_STRING32:
3526           TREE_TYPE (value) = char32_array_type_node;
3527           break;
3528         case CPP_WSTRING:
3529           TREE_TYPE (value) = wchar_array_type_node;
3530           break;
3531         }
3532
3533       value = fix_string_type (value);
3534
3535       if (have_suffix_p)
3536         {
3537           tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3538           tok->u.value = literal;
3539           return cp_parser_userdef_string_literal (tok);
3540         }
3541     }
3542   else
3543     /* cpp_interpret_string has issued an error.  */
3544     value = error_mark_node;
3545
3546   if (count > 1)
3547     obstack_free (&str_ob, 0);
3548
3549   return value;
3550 }
3551
3552 /* Look up a literal operator with the name and the exact arguments.  */
3553
3554 static tree
3555 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3556 {
3557   tree decl, fns;
3558   decl = lookup_name (name);
3559   if (!decl || !is_overloaded_fn (decl))
3560     return error_mark_node;
3561
3562   for (fns = decl; fns; fns = OVL_NEXT (fns))
3563     {
3564       unsigned int ix;
3565       bool found = true;
3566       tree fn = OVL_CURRENT (fns);
3567       tree argtypes = NULL_TREE;
3568       argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3569       if (argtypes != NULL_TREE)
3570         {
3571           for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3572                ++ix, argtypes = TREE_CHAIN (argtypes))
3573             {
3574               tree targ = TREE_VALUE (argtypes);
3575               tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3576               bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3577               bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3578               if ((ptr || arr || !same_type_p (targ, tparm))
3579                   && (!ptr || !arr
3580                       || !same_type_p (TREE_TYPE (targ),
3581                                        TREE_TYPE (tparm))))
3582                 found = false;
3583             }
3584           if (found)
3585             return fn;
3586         }
3587     }
3588
3589   return error_mark_node;
3590 }
3591
3592 /* Parse a user-defined char constant.  Returns a call to a user-defined
3593    literal operator taking the character as an argument.  */
3594
3595 static tree
3596 cp_parser_userdef_char_literal (cp_parser *parser)
3597 {
3598   cp_token *token = cp_lexer_consume_token (parser->lexer);
3599   tree literal = token->u.value;
3600   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3601   tree value = USERDEF_LITERAL_VALUE (literal);
3602   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3603   tree decl, result;
3604
3605   /* Build up a call to the user-defined operator  */
3606   /* Lookup the name we got back from the id-expression.  */
3607   VEC(tree,gc) *args = make_tree_vector ();
3608   VEC_safe_push (tree, gc, args, value);
3609   decl = lookup_literal_operator (name, args);
3610   if (!decl || decl == error_mark_node)
3611     {
3612       error ("unable to find character literal operator %qD with %qT argument",
3613              name, TREE_TYPE (value));
3614       release_tree_vector (args);
3615       return error_mark_node;
3616     }
3617   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3618   release_tree_vector (args);
3619   if (result != error_mark_node)
3620     return result;
3621
3622   error ("unable to find character literal operator %qD with %qT argument",
3623          name, TREE_TYPE (value));
3624   return error_mark_node;
3625 }
3626
3627 /* A subroutine of cp_parser_userdef_numeric_literal to
3628    create a char... template parameter pack from a string node.  */
3629
3630 static tree
3631 make_char_string_pack (tree value)
3632 {
3633   tree charvec;
3634   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3635   const char *str = TREE_STRING_POINTER (value);
3636   int i, len = TREE_STRING_LENGTH (value) - 1;
3637   tree argvec = make_tree_vec (1);
3638
3639   /* Fill in CHARVEC with all of the parameters.  */
3640   charvec = make_tree_vec (len);
3641   for (i = 0; i < len; ++i)
3642     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3643
3644   /* Build the argument packs.  */
3645   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3646   TREE_TYPE (argpack) = char_type_node;
3647
3648   TREE_VEC_ELT (argvec, 0) = argpack;
3649
3650   return argvec;
3651 }
3652
3653 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3654    literal operator.  */
3655
3656 static tree
3657 cp_parser_userdef_numeric_literal (cp_parser *parser)
3658 {
3659   cp_token *token = cp_lexer_consume_token (parser->lexer);
3660   tree literal = token->u.value;
3661   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3662   tree value = USERDEF_LITERAL_VALUE (literal);
3663   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3664   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3665   tree decl, result;
3666   VEC(tree,gc) *args;
3667
3668   /* Look for a literal operator taking the exact type of numeric argument
3669      as the literal value.  */
3670   args = make_tree_vector ();
3671   VEC_safe_push (tree, gc, args, value);
3672   decl = lookup_literal_operator (name, args);
3673   if (decl && decl != error_mark_node)
3674     {
3675       result = finish_call_expr (decl, &args, false, true, tf_none);
3676       if (result != error_mark_node)
3677         {
3678           release_tree_vector (args);
3679           return result;
3680         }
3681     }
3682   release_tree_vector (args);
3683
3684   /* If the numeric argument didn't work, look for a raw literal
3685      operator taking a const char* argument consisting of the number
3686      in string format.  */
3687   args = make_tree_vector ();
3688   VEC_safe_push (tree, gc, args, num_string);
3689   decl = lookup_literal_operator (name, args);
3690   if (decl && decl != error_mark_node)
3691     {
3692       result = finish_call_expr (decl, &args, false, true, tf_none);
3693       if (result != error_mark_node)
3694         {
3695           release_tree_vector (args);
3696           return result;
3697         }
3698     }
3699   release_tree_vector (args);
3700
3701   /* If the raw literal didn't work, look for a non-type template
3702      function with parameter pack char....  Call the function with
3703      template parameter characters representing the number.  */
3704   args = make_tree_vector ();
3705   decl = lookup_literal_operator (name, args);
3706   if (decl && decl != error_mark_node)
3707     {
3708       tree tmpl_args = make_char_string_pack (num_string);
3709       decl = lookup_template_function (decl, tmpl_args);
3710       result = finish_call_expr (decl, &args, false, true, tf_none);
3711       if (result != error_mark_node)
3712         {
3713           release_tree_vector (args);
3714           return result;
3715         }
3716     }
3717   release_tree_vector (args);
3718
3719   error ("unable to find numeric literal operator %qD", name);
3720   return error_mark_node;
3721 }
3722
3723 /* Parse a user-defined string constant.  Returns a call to a user-defined
3724    literal operator taking a character pointer and the length of the string
3725    as arguments.  */
3726
3727 static tree
3728 cp_parser_userdef_string_literal (cp_token *token)
3729 {
3730   tree literal = token->u.value;
3731   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3732   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3733   tree value = USERDEF_LITERAL_VALUE (literal);
3734   int len = TREE_STRING_LENGTH (value)
3735         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3736   tree decl, result;
3737
3738   /* Build up a call to the user-defined operator  */
3739   /* Lookup the name we got back from the id-expression.  */
3740   VEC(tree,gc) *args = make_tree_vector ();
3741   VEC_safe_push (tree, gc, args, value);
3742   VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3743   decl = lookup_name (name);
3744   if (!decl || decl == error_mark_node)
3745     {
3746       error ("unable to find string literal operator %qD", name);
3747       release_tree_vector (args);
3748       return error_mark_node;
3749     }
3750   result = finish_call_expr (decl, &args, false, true, tf_none);
3751   release_tree_vector (args);
3752   if (result != error_mark_node)
3753     return result;
3754
3755   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3756          name, TREE_TYPE (value), size_type_node);
3757   return error_mark_node;
3758 }
3759
3760
3761 /* Basic concepts [gram.basic]  */
3762
3763 /* Parse a translation-unit.
3764
3765    translation-unit:
3766      declaration-seq [opt]
3767
3768    Returns TRUE if all went well.  */
3769
3770 static bool
3771 cp_parser_translation_unit (cp_parser* parser)
3772 {
3773   /* The address of the first non-permanent object on the declarator
3774      obstack.  */
3775   static void *declarator_obstack_base;
3776
3777   bool success;
3778
3779   /* Create the declarator obstack, if necessary.  */
3780   if (!cp_error_declarator)
3781     {
3782       gcc_obstack_init (&declarator_obstack);
3783       /* Create the error declarator.  */
3784       cp_error_declarator = make_declarator (cdk_error);
3785       /* Create the empty parameter list.  */
3786       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3787       /* Remember where the base of the declarator obstack lies.  */
3788       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3789     }
3790
3791   cp_parser_declaration_seq_opt (parser);
3792
3793   /* If there are no tokens left then all went well.  */
3794   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3795     {
3796       /* Get rid of the token array; we don't need it any more.  */
3797       cp_lexer_destroy (parser->lexer);
3798       parser->lexer = NULL;
3799
3800       /* This file might have been a context that's implicitly extern
3801          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3802       if (parser->implicit_extern_c)
3803         {
3804           pop_lang_context ();
3805           parser->implicit_extern_c = false;
3806         }
3807
3808       /* Finish up.  */
3809       finish_translation_unit ();
3810
3811       success = true;
3812     }
3813   else
3814     {
3815       cp_parser_error (parser, "expected declaration");
3816       success = false;
3817     }
3818
3819   /* Make sure the declarator obstack was fully cleaned up.  */
3820   gcc_assert (obstack_next_free (&declarator_obstack)
3821               == declarator_obstack_base);
3822
3823   /* All went well.  */
3824   return success;
3825 }
3826
3827 /* Expressions [gram.expr] */
3828
3829 /* Parse a primary-expression.
3830
3831    primary-expression:
3832      literal
3833      this
3834      ( expression )
3835      id-expression
3836
3837    GNU Extensions:
3838
3839    primary-expression:
3840      ( compound-statement )
3841      __builtin_va_arg ( assignment-expression , type-id )
3842      __builtin_offsetof ( type-id , offsetof-expression )
3843
3844    C++ Extensions:
3845      __has_nothrow_assign ( type-id )   
3846      __has_nothrow_constructor ( type-id )
3847      __has_nothrow_copy ( type-id )
3848      __has_trivial_assign ( type-id )   
3849      __has_trivial_constructor ( type-id )
3850      __has_trivial_copy ( type-id )
3851      __has_trivial_destructor ( type-id )
3852      __has_virtual_destructor ( type-id )     
3853      __is_abstract ( type-id )
3854      __is_base_of ( type-id , type-id )
3855      __is_class ( type-id )
3856      __is_convertible_to ( type-id , type-id )     
3857      __is_empty ( type-id )
3858      __is_enum ( type-id )
3859      __is_final ( type-id )
3860      __is_literal_type ( type-id )
3861      __is_pod ( type-id )
3862      __is_polymorphic ( type-id )
3863      __is_std_layout ( type-id )
3864      __is_trivial ( type-id )
3865      __is_union ( type-id )
3866
3867    Objective-C++ Extension:
3868
3869    primary-expression:
3870      objc-expression
3871
3872    literal:
3873      __null
3874
3875    ADDRESS_P is true iff this expression was immediately preceded by
3876    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3877    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3878    true iff this expression is a template argument.
3879
3880    Returns a representation of the expression.  Upon return, *IDK
3881    indicates what kind of id-expression (if any) was present.  */
3882
3883 static tree
3884 cp_parser_primary_expression (cp_parser *parser,
3885                               bool address_p,
3886                               bool cast_p,
3887                               bool template_arg_p,
3888                               cp_id_kind *idk)
3889 {
3890   cp_token *token = NULL;
3891
3892   /* Assume the primary expression is not an id-expression.  */
3893   *idk = CP_ID_KIND_NONE;
3894
3895   /* Peek at the next token.  */
3896   token = cp_lexer_peek_token (parser->lexer);
3897   switch (token->type)
3898     {
3899       /* literal:
3900            integer-literal
3901            character-literal
3902            floating-literal
3903            string-literal
3904            boolean-literal
3905            pointer-literal
3906            user-defined-literal  */
3907     case CPP_CHAR:
3908     case CPP_CHAR16:
3909     case CPP_CHAR32:
3910     case CPP_WCHAR:
3911     case CPP_NUMBER:
3912       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3913         return cp_parser_userdef_numeric_literal (parser);
3914       token = cp_lexer_consume_token (parser->lexer);
3915       if (TREE_CODE (token->u.value) == FIXED_CST)
3916         {
3917           error_at (token->location,
3918                     "fixed-point types not supported in C++");
3919           return error_mark_node;
3920         }
3921       /* Floating-point literals are only allowed in an integral
3922          constant expression if they are cast to an integral or
3923          enumeration type.  */
3924       if (TREE_CODE (token->u.value) == REAL_CST
3925           && parser->integral_constant_expression_p
3926           && pedantic)
3927         {
3928           /* CAST_P will be set even in invalid code like "int(2.7 +
3929              ...)".   Therefore, we have to check that the next token
3930              is sure to end the cast.  */
3931           if (cast_p)
3932             {
3933               cp_token *next_token;
3934
3935               next_token = cp_lexer_peek_token (parser->lexer);
3936               if (/* The comma at the end of an
3937                      enumerator-definition.  */
3938                   next_token->type != CPP_COMMA
3939                   /* The curly brace at the end of an enum-specifier.  */
3940                   && next_token->type != CPP_CLOSE_BRACE
3941                   /* The end of a statement.  */
3942                   && next_token->type != CPP_SEMICOLON
3943                   /* The end of the cast-expression.  */
3944                   && next_token->type != CPP_CLOSE_PAREN
3945                   /* The end of an array bound.  */
3946                   && next_token->type != CPP_CLOSE_SQUARE
3947                   /* The closing ">" in a template-argument-list.  */
3948                   && (next_token->type != CPP_GREATER
3949                       || parser->greater_than_is_operator_p)
3950                   /* C++0x only: A ">>" treated like two ">" tokens,
3951                      in a template-argument-list.  */
3952                   && (next_token->type != CPP_RSHIFT
3953                       || (cxx_dialect == cxx98)
3954                       || parser->greater_than_is_operator_p))
3955                 cast_p = false;
3956             }
3957
3958           /* If we are within a cast, then the constraint that the
3959              cast is to an integral or enumeration type will be
3960              checked at that point.  If we are not within a cast, then
3961              this code is invalid.  */
3962           if (!cast_p)
3963             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3964         }
3965       return token->u.value;
3966
3967     case CPP_CHAR_USERDEF:
3968     case CPP_CHAR16_USERDEF:
3969     case CPP_CHAR32_USERDEF:
3970     case CPP_WCHAR_USERDEF:
3971       return cp_parser_userdef_char_literal (parser);
3972
3973     case CPP_STRING:
3974     case CPP_STRING16:
3975     case CPP_STRING32:
3976     case CPP_WSTRING:
3977     case CPP_UTF8STRING:
3978     case CPP_STRING_USERDEF:
3979     case CPP_STRING16_USERDEF:
3980     case CPP_STRING32_USERDEF:
3981     case CPP_WSTRING_USERDEF:
3982     case CPP_UTF8STRING_USERDEF:
3983       /* ??? Should wide strings be allowed when parser->translate_strings_p
3984          is false (i.e. in attributes)?  If not, we can kill the third
3985          argument to cp_parser_string_literal.  */
3986       return cp_parser_string_literal (parser,
3987                                        parser->translate_strings_p,
3988                                        true);
3989
3990     case CPP_OPEN_PAREN:
3991       {
3992         tree expr;
3993         bool saved_greater_than_is_operator_p;
3994
3995         /* Consume the `('.  */
3996         cp_lexer_consume_token (parser->lexer);
3997         /* Within a parenthesized expression, a `>' token is always
3998            the greater-than operator.  */
3999         saved_greater_than_is_operator_p
4000           = parser->greater_than_is_operator_p;
4001         parser->greater_than_is_operator_p = true;
4002         /* If we see `( { ' then we are looking at the beginning of
4003            a GNU statement-expression.  */
4004         if (cp_parser_allow_gnu_extensions_p (parser)
4005             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4006           {
4007             /* Statement-expressions are not allowed by the standard.  */
4008             pedwarn (token->location, OPT_pedantic, 
4009                      "ISO C++ forbids braced-groups within expressions");
4010
4011             /* And they're not allowed outside of a function-body; you
4012                cannot, for example, write:
4013
4014                  int i = ({ int j = 3; j + 1; });
4015
4016                at class or namespace scope.  */
4017             if (!parser->in_function_body
4018                 || parser->in_template_argument_list_p)
4019               {
4020                 error_at (token->location,
4021                           "statement-expressions are not allowed outside "
4022                           "functions nor in template-argument lists");
4023                 cp_parser_skip_to_end_of_block_or_statement (parser);
4024                 expr = error_mark_node;
4025               }
4026             else
4027               {
4028                 /* Start the statement-expression.  */
4029                 expr = begin_stmt_expr ();
4030                 /* Parse the compound-statement.  */
4031                 cp_parser_compound_statement (parser, expr, false, false);
4032                 /* Finish up.  */
4033                 expr = finish_stmt_expr (expr, false);
4034               }
4035           }
4036         else
4037           {
4038             /* Parse the parenthesized expression.  */
4039             expr = cp_parser_expression (parser, cast_p, idk);
4040             /* Let the front end know that this expression was
4041                enclosed in parentheses. This matters in case, for
4042                example, the expression is of the form `A::B', since
4043                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4044                not.  */
4045             finish_parenthesized_expr (expr);
4046             /* DR 705: Wrapping an unqualified name in parentheses
4047                suppresses arg-dependent lookup.  We want to pass back
4048                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4049                (c++/37862), but none of the others.  */
4050             if (*idk != CP_ID_KIND_QUALIFIED)
4051               *idk = CP_ID_KIND_NONE;
4052           }
4053         /* The `>' token might be the end of a template-id or
4054            template-parameter-list now.  */
4055         parser->greater_than_is_operator_p
4056           = saved_greater_than_is_operator_p;
4057         /* Consume the `)'.  */
4058         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4059           cp_parser_skip_to_end_of_statement (parser);
4060
4061         return expr;
4062       }
4063
4064     case CPP_OPEN_SQUARE:
4065       if (c_dialect_objc ())
4066         /* We have an Objective-C++ message. */
4067         return cp_parser_objc_expression (parser);
4068       {
4069         tree lam = cp_parser_lambda_expression (parser);
4070         /* Don't warn about a failed tentative parse.  */
4071         if (cp_parser_error_occurred (parser))
4072           return error_mark_node;
4073         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4074         return lam;
4075       }
4076
4077     case CPP_OBJC_STRING:
4078       if (c_dialect_objc ())
4079         /* We have an Objective-C++ string literal. */
4080         return cp_parser_objc_expression (parser);
4081       cp_parser_error (parser, "expected primary-expression");
4082       return error_mark_node;
4083
4084     case CPP_KEYWORD:
4085       switch (token->keyword)
4086         {
4087           /* These two are the boolean literals.  */
4088         case RID_TRUE:
4089           cp_lexer_consume_token (parser->lexer);
4090           return boolean_true_node;
4091         case RID_FALSE:
4092           cp_lexer_consume_token (parser->lexer);
4093           return boolean_false_node;
4094
4095           /* The `__null' literal.  */
4096         case RID_NULL:
4097           cp_lexer_consume_token (parser->lexer);
4098           return null_node;
4099
4100           /* The `nullptr' literal.  */
4101         case RID_NULLPTR:
4102           cp_lexer_consume_token (parser->lexer);
4103           return nullptr_node;
4104
4105           /* Recognize the `this' keyword.  */
4106         case RID_THIS:
4107           cp_lexer_consume_token (parser->lexer);
4108           if (parser->local_variables_forbidden_p)
4109             {
4110               error_at (token->location,
4111                         "%<this%> may not be used in this context");
4112               return error_mark_node;
4113             }
4114           /* Pointers cannot appear in constant-expressions.  */
4115           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4116             return error_mark_node;
4117           return finish_this_expr ();
4118
4119           /* The `operator' keyword can be the beginning of an
4120              id-expression.  */
4121         case RID_OPERATOR:
4122           goto id_expression;
4123
4124         case RID_FUNCTION_NAME:
4125         case RID_PRETTY_FUNCTION_NAME:
4126         case RID_C99_FUNCTION_NAME:
4127           {
4128             non_integral_constant name;
4129
4130             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4131                __func__ are the names of variables -- but they are
4132                treated specially.  Therefore, they are handled here,
4133                rather than relying on the generic id-expression logic
4134                below.  Grammatically, these names are id-expressions.
4135
4136                Consume the token.  */
4137             token = cp_lexer_consume_token (parser->lexer);
4138
4139             switch (token->keyword)
4140               {
4141               case RID_FUNCTION_NAME:
4142                 name = NIC_FUNC_NAME;
4143                 break;
4144               case RID_PRETTY_FUNCTION_NAME:
4145                 name = NIC_PRETTY_FUNC;
4146                 break;
4147               case RID_C99_FUNCTION_NAME:
4148                 name = NIC_C99_FUNC;
4149                 break;
4150               default:
4151                 gcc_unreachable ();
4152               }
4153
4154             if (cp_parser_non_integral_constant_expression (parser, name))
4155               return error_mark_node;
4156
4157             /* Look up the name.  */
4158             return finish_fname (token->u.value);
4159           }
4160
4161         case RID_VA_ARG:
4162           {
4163             tree expression;
4164             tree type;
4165
4166             /* The `__builtin_va_arg' construct is used to handle
4167                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4168             cp_lexer_consume_token (parser->lexer);
4169             /* Look for the opening `('.  */
4170             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4171             /* Now, parse the assignment-expression.  */
4172             expression = cp_parser_assignment_expression (parser,
4173                                                           /*cast_p=*/false, NULL);
4174             /* Look for the `,'.  */
4175             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4176             /* Parse the type-id.  */
4177             type = cp_parser_type_id (parser);
4178             /* Look for the closing `)'.  */
4179             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4180             /* Using `va_arg' in a constant-expression is not
4181                allowed.  */
4182             if (cp_parser_non_integral_constant_expression (parser,
4183                                                             NIC_VA_ARG))
4184               return error_mark_node;
4185             return build_x_va_arg (expression, type);
4186           }
4187
4188         case RID_OFFSETOF:
4189           return cp_parser_builtin_offsetof (parser);
4190
4191         case RID_HAS_NOTHROW_ASSIGN:
4192         case RID_HAS_NOTHROW_CONSTRUCTOR:
4193         case RID_HAS_NOTHROW_COPY:        
4194         case RID_HAS_TRIVIAL_ASSIGN:
4195         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4196         case RID_HAS_TRIVIAL_COPY:        
4197         case RID_HAS_TRIVIAL_DESTRUCTOR:
4198         case RID_HAS_VIRTUAL_DESTRUCTOR:
4199         case RID_IS_ABSTRACT:
4200         case RID_IS_BASE_OF:
4201         case RID_IS_CLASS:
4202         case RID_IS_CONVERTIBLE_TO:
4203         case RID_IS_EMPTY:
4204         case RID_IS_ENUM:
4205         case RID_IS_FINAL:
4206         case RID_IS_LITERAL_TYPE:
4207         case RID_IS_POD:
4208         case RID_IS_POLYMORPHIC:
4209         case RID_IS_STD_LAYOUT:
4210         case RID_IS_TRIVIAL:
4211         case RID_IS_UNION:
4212           return cp_parser_trait_expr (parser, token->keyword);
4213
4214         /* Objective-C++ expressions.  */
4215         case RID_AT_ENCODE:
4216         case RID_AT_PROTOCOL:
4217         case RID_AT_SELECTOR:
4218           return cp_parser_objc_expression (parser);
4219
4220         case RID_TEMPLATE:
4221           if (parser->in_function_body
4222               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4223                   == CPP_LESS))
4224             {
4225               error_at (token->location,
4226                         "a template declaration cannot appear at block scope");
4227               cp_parser_skip_to_end_of_block_or_statement (parser);
4228               return error_mark_node;
4229             }
4230         default:
4231           cp_parser_error (parser, "expected primary-expression");
4232           return error_mark_node;
4233         }
4234
4235       /* An id-expression can start with either an identifier, a
4236          `::' as the beginning of a qualified-id, or the "operator"
4237          keyword.  */
4238     case CPP_NAME:
4239     case CPP_SCOPE:
4240     case CPP_TEMPLATE_ID:
4241     case CPP_NESTED_NAME_SPECIFIER:
4242       {
4243         tree id_expression;
4244         tree decl;
4245         const char *error_msg;
4246         bool template_p;
4247         bool done;
4248         cp_token *id_expr_token;
4249
4250       id_expression:
4251         /* Parse the id-expression.  */
4252         id_expression
4253           = cp_parser_id_expression (parser,
4254                                      /*template_keyword_p=*/false,
4255                                      /*check_dependency_p=*/true,
4256                                      &template_p,
4257                                      /*declarator_p=*/false,
4258                                      /*optional_p=*/false);
4259         if (id_expression == error_mark_node)
4260           return error_mark_node;
4261         id_expr_token = token;
4262         token = cp_lexer_peek_token (parser->lexer);
4263         done = (token->type != CPP_OPEN_SQUARE
4264                 && token->type != CPP_OPEN_PAREN
4265                 && token->type != CPP_DOT
4266                 && token->type != CPP_DEREF
4267                 && token->type != CPP_PLUS_PLUS
4268                 && token->type != CPP_MINUS_MINUS);
4269         /* If we have a template-id, then no further lookup is
4270            required.  If the template-id was for a template-class, we
4271            will sometimes have a TYPE_DECL at this point.  */
4272         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4273                  || TREE_CODE (id_expression) == TYPE_DECL)
4274           decl = id_expression;
4275         /* Look up the name.  */
4276         else
4277           {
4278             tree ambiguous_decls;
4279
4280             /* If we already know that this lookup is ambiguous, then
4281                we've already issued an error message; there's no reason
4282                to check again.  */
4283             if (id_expr_token->type == CPP_NAME
4284                 && id_expr_token->ambiguous_p)
4285               {
4286                 cp_parser_simulate_error (parser);
4287                 return error_mark_node;
4288               }
4289
4290             decl = cp_parser_lookup_name (parser, id_expression,
4291                                           none_type,
4292                                           template_p,
4293                                           /*is_namespace=*/false,
4294                                           /*check_dependency=*/true,
4295                                           &ambiguous_decls,
4296                                           id_expr_token->location);
4297             /* If the lookup was ambiguous, an error will already have
4298                been issued.  */
4299             if (ambiguous_decls)
4300               return error_mark_node;
4301
4302             /* In Objective-C++, we may have an Objective-C 2.0
4303                dot-syntax for classes here.  */
4304             if (c_dialect_objc ()
4305                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4306                 && TREE_CODE (decl) == TYPE_DECL
4307                 && objc_is_class_name (decl))
4308               {
4309                 tree component;
4310                 cp_lexer_consume_token (parser->lexer);
4311                 component = cp_parser_identifier (parser);
4312                 if (component == error_mark_node)
4313                   return error_mark_node;
4314
4315                 return objc_build_class_component_ref (id_expression, component);
4316               }
4317
4318             /* In Objective-C++, an instance variable (ivar) may be preferred
4319                to whatever cp_parser_lookup_name() found.  */
4320             decl = objc_lookup_ivar (decl, id_expression);
4321
4322             /* If name lookup gives us a SCOPE_REF, then the
4323                qualifying scope was dependent.  */
4324             if (TREE_CODE (decl) == SCOPE_REF)
4325               {
4326                 /* At this point, we do not know if DECL is a valid
4327                    integral constant expression.  We assume that it is
4328                    in fact such an expression, so that code like:
4329
4330                       template <int N> struct A {
4331                         int a[B<N>::i];
4332                       };
4333                      
4334                    is accepted.  At template-instantiation time, we
4335                    will check that B<N>::i is actually a constant.  */
4336                 return decl;
4337               }
4338             /* Check to see if DECL is a local variable in a context
4339                where that is forbidden.  */
4340             if (parser->local_variables_forbidden_p
4341                 && local_variable_p (decl))
4342               {
4343                 /* It might be that we only found DECL because we are
4344                    trying to be generous with pre-ISO scoping rules.
4345                    For example, consider:
4346
4347                      int i;
4348                      void g() {
4349                        for (int i = 0; i < 10; ++i) {}
4350                        extern void f(int j = i);
4351                      }
4352
4353                    Here, name look up will originally find the out
4354                    of scope `i'.  We need to issue a warning message,
4355                    but then use the global `i'.  */
4356                 decl = check_for_out_of_scope_variable (decl);
4357                 if (local_variable_p (decl))
4358                   {
4359                     error_at (id_expr_token->location,
4360                               "local variable %qD may not appear in this context",
4361                               decl);
4362                     return error_mark_node;
4363                   }
4364               }
4365           }
4366
4367         decl = (finish_id_expression
4368                 (id_expression, decl, parser->scope,
4369                  idk,
4370                  parser->integral_constant_expression_p,
4371                  parser->allow_non_integral_constant_expression_p,
4372                  &parser->non_integral_constant_expression_p,
4373                  template_p, done, address_p,
4374                  template_arg_p,
4375                  &error_msg,
4376                  id_expr_token->location));
4377         if (error_msg)
4378           cp_parser_error (parser, error_msg);
4379         return decl;
4380       }
4381
4382       /* Anything else is an error.  */
4383     default:
4384       cp_parser_error (parser, "expected primary-expression");
4385       return error_mark_node;
4386     }
4387 }
4388
4389 /* Parse an id-expression.
4390
4391    id-expression:
4392      unqualified-id
4393      qualified-id
4394
4395    qualified-id:
4396      :: [opt] nested-name-specifier template [opt] unqualified-id
4397      :: identifier
4398      :: operator-function-id
4399      :: template-id
4400
4401    Return a representation of the unqualified portion of the
4402    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4403    a `::' or nested-name-specifier.
4404
4405    Often, if the id-expression was a qualified-id, the caller will
4406    want to make a SCOPE_REF to represent the qualified-id.  This
4407    function does not do this in order to avoid wastefully creating
4408    SCOPE_REFs when they are not required.
4409
4410    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4411    `template' keyword.
4412
4413    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4414    uninstantiated templates.
4415
4416    If *TEMPLATE_P is non-NULL, it is set to true iff the
4417    `template' keyword is used to explicitly indicate that the entity
4418    named is a template.
4419
4420    If DECLARATOR_P is true, the id-expression is appearing as part of
4421    a declarator, rather than as part of an expression.  */
4422
4423 static tree
4424 cp_parser_id_expression (cp_parser *parser,
4425                          bool template_keyword_p,
4426                          bool check_dependency_p,
4427                          bool *template_p,
4428                          bool declarator_p,
4429                          bool optional_p)
4430 {
4431   bool global_scope_p;
4432   bool nested_name_specifier_p;
4433
4434   /* Assume the `template' keyword was not used.  */
4435   if (template_p)
4436     *template_p = template_keyword_p;
4437
4438   /* Look for the optional `::' operator.  */
4439   global_scope_p
4440     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4441        != NULL_TREE);
4442   /* Look for the optional nested-name-specifier.  */
4443   nested_name_specifier_p
4444     = (cp_parser_nested_name_specifier_opt (parser,
4445                                             /*typename_keyword_p=*/false,
4446                                             check_dependency_p,
4447                                             /*type_p=*/false,
4448                                             declarator_p)
4449        != NULL_TREE);
4450   /* If there is a nested-name-specifier, then we are looking at
4451      the first qualified-id production.  */
4452   if (nested_name_specifier_p)
4453     {
4454       tree saved_scope;
4455       tree saved_object_scope;
4456       tree saved_qualifying_scope;
4457       tree unqualified_id;
4458       bool is_template;
4459
4460       /* See if the next token is the `template' keyword.  */
4461       if (!template_p)
4462         template_p = &is_template;
4463       *template_p = cp_parser_optional_template_keyword (parser);
4464       /* Name lookup we do during the processing of the
4465          unqualified-id might obliterate SCOPE.  */
4466       saved_scope = parser->scope;
4467       saved_object_scope = parser->object_scope;
4468       saved_qualifying_scope = parser->qualifying_scope;
4469       /* Process the final unqualified-id.  */
4470       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4471                                                  check_dependency_p,
4472                                                  declarator_p,
4473                                                  /*optional_p=*/false);
4474       /* Restore the SAVED_SCOPE for our caller.  */
4475       parser->scope = saved_scope;
4476       parser->object_scope = saved_object_scope;
4477       parser->qualifying_scope = saved_qualifying_scope;
4478
4479       return unqualified_id;
4480     }
4481   /* Otherwise, if we are in global scope, then we are looking at one
4482      of the other qualified-id productions.  */
4483   else if (global_scope_p)
4484     {
4485       cp_token *token;
4486       tree id;
4487
4488       /* Peek at the next token.  */
4489       token = cp_lexer_peek_token (parser->lexer);
4490
4491       /* If it's an identifier, and the next token is not a "<", then
4492          we can avoid the template-id case.  This is an optimization
4493          for this common case.  */
4494       if (token->type == CPP_NAME
4495           && !cp_parser_nth_token_starts_template_argument_list_p
4496                (parser, 2))
4497         return cp_parser_identifier (parser);
4498
4499       cp_parser_parse_tentatively (parser);
4500       /* Try a template-id.  */
4501       id = cp_parser_template_id (parser,
4502                                   /*template_keyword_p=*/false,
4503                                   /*check_dependency_p=*/true,
4504                                   declarator_p);
4505       /* If that worked, we're done.  */
4506       if (cp_parser_parse_definitely (parser))
4507         return id;
4508
4509       /* Peek at the next token.  (Changes in the token buffer may
4510          have invalidated the pointer obtained above.)  */
4511       token = cp_lexer_peek_token (parser->lexer);
4512
4513       switch (token->type)
4514         {
4515         case CPP_NAME:
4516           return cp_parser_identifier (parser);
4517
4518         case CPP_KEYWORD:
4519           if (token->keyword == RID_OPERATOR)
4520             return cp_parser_operator_function_id (parser);
4521           /* Fall through.  */
4522
4523         default:
4524           cp_parser_error (parser, "expected id-expression");
4525           return error_mark_node;
4526         }
4527     }
4528   else
4529     return cp_parser_unqualified_id (parser, template_keyword_p,
4530                                      /*check_dependency_p=*/true,
4531                                      declarator_p,
4532                                      optional_p);
4533 }
4534
4535 /* Parse an unqualified-id.
4536
4537    unqualified-id:
4538      identifier
4539      operator-function-id
4540      conversion-function-id
4541      ~ class-name
4542      template-id
4543
4544    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4545    keyword, in a construct like `A::template ...'.
4546
4547    Returns a representation of unqualified-id.  For the `identifier'
4548    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4549    production a BIT_NOT_EXPR is returned; the operand of the
4550    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4551    other productions, see the documentation accompanying the
4552    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4553    names are looked up in uninstantiated templates.  If DECLARATOR_P
4554    is true, the unqualified-id is appearing as part of a declarator,
4555    rather than as part of an expression.  */
4556
4557 static tree
4558 cp_parser_unqualified_id (cp_parser* parser,
4559                           bool template_keyword_p,
4560                           bool check_dependency_p,
4561                           bool declarator_p,
4562                           bool optional_p)
4563 {
4564   cp_token *token;
4565
4566   /* Peek at the next token.  */
4567   token = cp_lexer_peek_token (parser->lexer);
4568
4569   switch (token->type)
4570     {
4571     case CPP_NAME:
4572       {
4573         tree id;
4574
4575         /* We don't know yet whether or not this will be a
4576            template-id.  */
4577         cp_parser_parse_tentatively (parser);
4578         /* Try a template-id.  */
4579         id = cp_parser_template_id (parser, template_keyword_p,
4580                                     check_dependency_p,
4581                                     declarator_p);
4582         /* If it worked, we're done.  */
4583         if (cp_parser_parse_definitely (parser))
4584           return id;
4585         /* Otherwise, it's an ordinary identifier.  */
4586         return cp_parser_identifier (parser);
4587       }
4588
4589     case CPP_TEMPLATE_ID:
4590       return cp_parser_template_id (parser, template_keyword_p,
4591                                     check_dependency_p,
4592                                     declarator_p);
4593
4594     case CPP_COMPL:
4595       {
4596         tree type_decl;
4597         tree qualifying_scope;
4598         tree object_scope;
4599         tree scope;
4600         bool done;
4601
4602         /* Consume the `~' token.  */
4603         cp_lexer_consume_token (parser->lexer);
4604         /* Parse the class-name.  The standard, as written, seems to
4605            say that:
4606
4607              template <typename T> struct S { ~S (); };
4608              template <typename T> S<T>::~S() {}
4609
4610            is invalid, since `~' must be followed by a class-name, but
4611            `S<T>' is dependent, and so not known to be a class.
4612            That's not right; we need to look in uninstantiated
4613            templates.  A further complication arises from:
4614
4615              template <typename T> void f(T t) {
4616                t.T::~T();
4617              }
4618
4619            Here, it is not possible to look up `T' in the scope of `T'
4620            itself.  We must look in both the current scope, and the
4621            scope of the containing complete expression.
4622
4623            Yet another issue is:
4624
4625              struct S {
4626                int S;
4627                ~S();
4628              };
4629
4630              S::~S() {}
4631
4632            The standard does not seem to say that the `S' in `~S'
4633            should refer to the type `S' and not the data member
4634            `S::S'.  */
4635
4636         /* DR 244 says that we look up the name after the "~" in the
4637            same scope as we looked up the qualifying name.  That idea
4638            isn't fully worked out; it's more complicated than that.  */
4639         scope = parser->scope;
4640         object_scope = parser->object_scope;
4641         qualifying_scope = parser->qualifying_scope;
4642
4643         /* Check for invalid scopes.  */
4644         if (scope == error_mark_node)
4645           {
4646             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4647               cp_lexer_consume_token (parser->lexer);
4648             return error_mark_node;
4649           }
4650         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4651           {
4652             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4653               error_at (token->location,
4654                         "scope %qT before %<~%> is not a class-name",
4655                         scope);
4656             cp_parser_simulate_error (parser);
4657             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4658               cp_lexer_consume_token (parser->lexer);
4659             return error_mark_node;
4660           }
4661         gcc_assert (!scope || TYPE_P (scope));
4662
4663         /* If the name is of the form "X::~X" it's OK even if X is a
4664            typedef.  */
4665         token = cp_lexer_peek_token (parser->lexer);
4666         if (scope
4667             && token->type == CPP_NAME
4668             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4669                 != CPP_LESS)
4670             && (token->u.value == TYPE_IDENTIFIER (scope)
4671                 || (CLASS_TYPE_P (scope)
4672                     && constructor_name_p (token->u.value, scope))))
4673           {
4674             cp_lexer_consume_token (parser->lexer);
4675             return build_nt (BIT_NOT_EXPR, scope);
4676           }
4677
4678         /* If there was an explicit qualification (S::~T), first look
4679            in the scope given by the qualification (i.e., S).
4680
4681            Note: in the calls to cp_parser_class_name below we pass
4682            typename_type so that lookup finds the injected-class-name
4683            rather than the constructor.  */
4684         done = false;
4685         type_decl = NULL_TREE;
4686         if (scope)
4687           {
4688             cp_parser_parse_tentatively (parser);
4689             type_decl = cp_parser_class_name (parser,
4690                                               /*typename_keyword_p=*/false,
4691                                               /*template_keyword_p=*/false,
4692                                               typename_type,
4693                                               /*check_dependency=*/false,
4694                                               /*class_head_p=*/false,
4695                                               declarator_p);
4696             if (cp_parser_parse_definitely (parser))
4697               done = true;
4698           }
4699         /* In "N::S::~S", look in "N" as well.  */
4700         if (!done && scope && qualifying_scope)
4701           {
4702             cp_parser_parse_tentatively (parser);
4703             parser->scope = qualifying_scope;
4704             parser->object_scope = NULL_TREE;
4705             parser->qualifying_scope = NULL_TREE;
4706             type_decl
4707               = cp_parser_class_name (parser,
4708                                       /*typename_keyword_p=*/false,
4709                                       /*template_keyword_p=*/false,
4710                                       typename_type,
4711                                       /*check_dependency=*/false,
4712                                       /*class_head_p=*/false,
4713                                       declarator_p);
4714             if (cp_parser_parse_definitely (parser))
4715               done = true;
4716           }
4717         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4718         else if (!done && object_scope)
4719           {
4720             cp_parser_parse_tentatively (parser);
4721             parser->scope = object_scope;
4722             parser->object_scope = NULL_TREE;
4723             parser->qualifying_scope = NULL_TREE;
4724             type_decl
4725               = cp_parser_class_name (parser,
4726                                       /*typename_keyword_p=*/false,
4727                                       /*template_keyword_p=*/false,
4728                                       typename_type,
4729                                       /*check_dependency=*/false,
4730                                       /*class_head_p=*/false,
4731                                       declarator_p);
4732             if (cp_parser_parse_definitely (parser))
4733               done = true;
4734           }
4735         /* Look in the surrounding context.  */
4736         if (!done)
4737           {
4738             parser->scope = NULL_TREE;
4739             parser->object_scope = NULL_TREE;
4740             parser->qualifying_scope = NULL_TREE;
4741             if (processing_template_decl)
4742               cp_parser_parse_tentatively (parser);
4743             type_decl
4744               = cp_parser_class_name (parser,
4745                                       /*typename_keyword_p=*/false,
4746                                       /*template_keyword_p=*/false,
4747                                       typename_type,
4748                                       /*check_dependency=*/false,
4749                                       /*class_head_p=*/false,
4750                                       declarator_p);
4751             if (processing_template_decl
4752                 && ! cp_parser_parse_definitely (parser))
4753               {
4754                 /* We couldn't find a type with this name, so just accept
4755                    it and check for a match at instantiation time.  */
4756                 type_decl = cp_parser_identifier (parser);
4757                 if (type_decl != error_mark_node)
4758                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4759                 return type_decl;
4760               }
4761           }
4762         /* If an error occurred, assume that the name of the
4763            destructor is the same as the name of the qualifying
4764            class.  That allows us to keep parsing after running
4765            into ill-formed destructor names.  */
4766         if (type_decl == error_mark_node && scope)
4767           return build_nt (BIT_NOT_EXPR, scope);
4768         else if (type_decl == error_mark_node)
4769           return error_mark_node;
4770
4771         /* Check that destructor name and scope match.  */
4772         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4773           {
4774             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4775               error_at (token->location,
4776                         "declaration of %<~%T%> as member of %qT",
4777                         type_decl, scope);
4778             cp_parser_simulate_error (parser);
4779             return error_mark_node;
4780           }
4781
4782         /* [class.dtor]
4783
4784            A typedef-name that names a class shall not be used as the
4785            identifier in the declarator for a destructor declaration.  */
4786         if (declarator_p
4787             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4788             && !DECL_SELF_REFERENCE_P (type_decl)
4789             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4790           error_at (token->location,
4791                     "typedef-name %qD used as destructor declarator",
4792                     type_decl);
4793
4794         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4795       }
4796
4797     case CPP_KEYWORD:
4798       if (token->keyword == RID_OPERATOR)
4799         {
4800           tree id;
4801
4802           /* This could be a template-id, so we try that first.  */
4803           cp_parser_parse_tentatively (parser);
4804           /* Try a template-id.  */
4805           id = cp_parser_template_id (parser, template_keyword_p,
4806                                       /*check_dependency_p=*/true,
4807                                       declarator_p);
4808           /* If that worked, we're done.  */
4809           if (cp_parser_parse_definitely (parser))
4810             return id;
4811           /* We still don't know whether we're looking at an
4812              operator-function-id or a conversion-function-id.  */
4813           cp_parser_parse_tentatively (parser);
4814           /* Try an operator-function-id.  */
4815           id = cp_parser_operator_function_id (parser);
4816           /* If that didn't work, try a conversion-function-id.  */
4817           if (!cp_parser_parse_definitely (parser))
4818             id = cp_parser_conversion_function_id (parser);
4819           else if (UDLIT_OPER_P (id))
4820             {
4821               /* 17.6.3.3.5  */
4822               const char *name = UDLIT_OP_SUFFIX (id);
4823               if (name[0] != '_' && !in_system_header)
4824                 warning (0, "literal operator suffixes not preceded by %<_%>"
4825                             " are reserved for future standardization");
4826             }
4827
4828           return id;
4829         }
4830       /* Fall through.  */
4831
4832     default:
4833       if (optional_p)
4834         return NULL_TREE;
4835       cp_parser_error (parser, "expected unqualified-id");
4836       return error_mark_node;
4837     }
4838 }
4839
4840 /* Parse an (optional) nested-name-specifier.
4841
4842    nested-name-specifier: [C++98]
4843      class-or-namespace-name :: nested-name-specifier [opt]
4844      class-or-namespace-name :: template nested-name-specifier [opt]
4845
4846    nested-name-specifier: [C++0x]
4847      type-name ::
4848      namespace-name ::
4849      nested-name-specifier identifier ::
4850      nested-name-specifier template [opt] simple-template-id ::
4851
4852    PARSER->SCOPE should be set appropriately before this function is
4853    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4854    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4855    in name lookups.
4856
4857    Sets PARSER->SCOPE to the class (TYPE) or namespace
4858    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4859    it unchanged if there is no nested-name-specifier.  Returns the new
4860    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4861
4862    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4863    part of a declaration and/or decl-specifier.  */
4864
4865 static tree
4866 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4867                                      bool typename_keyword_p,
4868                                      bool check_dependency_p,
4869                                      bool type_p,
4870                                      bool is_declaration)
4871 {
4872   bool success = false;
4873   cp_token_position start = 0;
4874   cp_token *token;
4875
4876   /* Remember where the nested-name-specifier starts.  */
4877   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4878     {
4879       start = cp_lexer_token_position (parser->lexer, false);
4880       push_deferring_access_checks (dk_deferred);
4881     }
4882
4883   while (true)
4884     {
4885       tree new_scope;
4886       tree old_scope;
4887       tree saved_qualifying_scope;
4888       bool template_keyword_p;
4889
4890       /* Spot cases that cannot be the beginning of a
4891          nested-name-specifier.  */
4892       token = cp_lexer_peek_token (parser->lexer);
4893
4894       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4895          the already parsed nested-name-specifier.  */
4896       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4897         {
4898           /* Grab the nested-name-specifier and continue the loop.  */
4899           cp_parser_pre_parsed_nested_name_specifier (parser);
4900           /* If we originally encountered this nested-name-specifier
4901              with IS_DECLARATION set to false, we will not have
4902              resolved TYPENAME_TYPEs, so we must do so here.  */
4903           if (is_declaration
4904               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4905             {
4906               new_scope = resolve_typename_type (parser->scope,
4907                                                  /*only_current_p=*/false);
4908               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4909                 parser->scope = new_scope;
4910             }
4911           success = true;
4912           continue;
4913         }
4914
4915       /* Spot cases that cannot be the beginning of a
4916          nested-name-specifier.  On the second and subsequent times
4917          through the loop, we look for the `template' keyword.  */
4918       if (success && token->keyword == RID_TEMPLATE)
4919         ;
4920       /* A template-id can start a nested-name-specifier.  */
4921       else if (token->type == CPP_TEMPLATE_ID)
4922         ;
4923       /* DR 743: decltype can be used in a nested-name-specifier.  */
4924       else if (token_is_decltype (token))
4925         ;
4926       else
4927         {
4928           /* If the next token is not an identifier, then it is
4929              definitely not a type-name or namespace-name.  */
4930           if (token->type != CPP_NAME)
4931             break;
4932           /* If the following token is neither a `<' (to begin a
4933              template-id), nor a `::', then we are not looking at a
4934              nested-name-specifier.  */
4935           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4936
4937           if (token->type == CPP_COLON
4938               && parser->colon_corrects_to_scope_p
4939               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4940             {
4941               error_at (token->location,
4942                         "found %<:%> in nested-name-specifier, expected %<::%>");
4943               token->type = CPP_SCOPE;
4944             }
4945
4946           if (token->type != CPP_SCOPE
4947               && !cp_parser_nth_token_starts_template_argument_list_p
4948                   (parser, 2))
4949             break;
4950         }
4951
4952       /* The nested-name-specifier is optional, so we parse
4953          tentatively.  */
4954       cp_parser_parse_tentatively (parser);
4955
4956       /* Look for the optional `template' keyword, if this isn't the
4957          first time through the loop.  */
4958       if (success)
4959         template_keyword_p = cp_parser_optional_template_keyword (parser);
4960       else
4961         template_keyword_p = false;
4962
4963       /* Save the old scope since the name lookup we are about to do
4964          might destroy it.  */
4965       old_scope = parser->scope;
4966       saved_qualifying_scope = parser->qualifying_scope;
4967       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4968          look up names in "X<T>::I" in order to determine that "Y" is
4969          a template.  So, if we have a typename at this point, we make
4970          an effort to look through it.  */
4971       if (is_declaration
4972           && !typename_keyword_p
4973           && parser->scope
4974           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4975         parser->scope = resolve_typename_type (parser->scope,
4976                                                /*only_current_p=*/false);
4977       /* Parse the qualifying entity.  */
4978       new_scope
4979         = cp_parser_qualifying_entity (parser,
4980                                        typename_keyword_p,
4981                                        template_keyword_p,
4982                                        check_dependency_p,
4983                                        type_p,
4984                                        is_declaration);
4985       /* Look for the `::' token.  */
4986       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4987
4988       /* If we found what we wanted, we keep going; otherwise, we're
4989          done.  */
4990       if (!cp_parser_parse_definitely (parser))
4991         {
4992           bool error_p = false;
4993
4994           /* Restore the OLD_SCOPE since it was valid before the
4995              failed attempt at finding the last
4996              class-or-namespace-name.  */
4997           parser->scope = old_scope;
4998           parser->qualifying_scope = saved_qualifying_scope;
4999
5000           /* If the next token is a decltype, and the one after that is a
5001              `::', then the decltype has failed to resolve to a class or
5002              enumeration type.  Give this error even when parsing
5003              tentatively since it can't possibly be valid--and we're going
5004              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5005              won't get another chance.*/
5006           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5007               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5008                   == CPP_SCOPE))
5009             {
5010               token = cp_lexer_consume_token (parser->lexer);
5011               error_at (token->location, "decltype evaluates to %qT, "
5012                         "which is not a class or enumeration type",
5013                         token->u.value);
5014               parser->scope = error_mark_node;
5015               error_p = true;
5016               /* As below.  */
5017               success = true;
5018               cp_lexer_consume_token (parser->lexer);
5019             }
5020
5021           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5022             break;
5023           /* If the next token is an identifier, and the one after
5024              that is a `::', then any valid interpretation would have
5025              found a class-or-namespace-name.  */
5026           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5027                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5028                      == CPP_SCOPE)
5029                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5030                      != CPP_COMPL))
5031             {
5032               token = cp_lexer_consume_token (parser->lexer);
5033               if (!error_p)
5034                 {
5035                   if (!token->ambiguous_p)
5036                     {
5037                       tree decl;
5038                       tree ambiguous_decls;
5039
5040                       decl = cp_parser_lookup_name (parser, token->u.value,
5041                                                     none_type,
5042                                                     /*is_template=*/false,
5043                                                     /*is_namespace=*/false,
5044                                                     /*check_dependency=*/true,
5045                                                     &ambiguous_decls,
5046                                                     token->location);
5047                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5048                         error_at (token->location,
5049                                   "%qD used without template parameters",
5050                                   decl);
5051                       else if (ambiguous_decls)
5052                         {
5053                           error_at (token->location,
5054                                     "reference to %qD is ambiguous",
5055                                     token->u.value);
5056                           print_candidates (ambiguous_decls);
5057                           decl = error_mark_node;
5058                         }
5059                       else
5060                         {
5061                           if (cxx_dialect != cxx98)
5062                             cp_parser_name_lookup_error
5063                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5064                              token->location);
5065                           else
5066                             cp_parser_name_lookup_error
5067                             (parser, token->u.value, decl, NLE_CXX98,
5068                              token->location);
5069                         }
5070                     }
5071                   parser->scope = error_mark_node;
5072                   error_p = true;
5073                   /* Treat this as a successful nested-name-specifier
5074                      due to:
5075
5076                      [basic.lookup.qual]
5077
5078                      If the name found is not a class-name (clause
5079                      _class_) or namespace-name (_namespace.def_), the
5080                      program is ill-formed.  */
5081                   success = true;
5082                 }
5083               cp_lexer_consume_token (parser->lexer);
5084             }
5085           break;
5086         }
5087       /* We've found one valid nested-name-specifier.  */
5088       success = true;
5089       /* Name lookup always gives us a DECL.  */
5090       if (TREE_CODE (new_scope) == TYPE_DECL)
5091         new_scope = TREE_TYPE (new_scope);
5092       /* Uses of "template" must be followed by actual templates.  */
5093       if (template_keyword_p
5094           && !(CLASS_TYPE_P (new_scope)
5095                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5096                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5097                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5098           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5099                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5100                    == TEMPLATE_ID_EXPR)))
5101         permerror (input_location, TYPE_P (new_scope)
5102                    ? G_("%qT is not a template")
5103                    : G_("%qD is not a template"),
5104                    new_scope);
5105       /* If it is a class scope, try to complete it; we are about to
5106          be looking up names inside the class.  */
5107       if (TYPE_P (new_scope)
5108           /* Since checking types for dependency can be expensive,
5109              avoid doing it if the type is already complete.  */
5110           && !COMPLETE_TYPE_P (new_scope)
5111           /* Do not try to complete dependent types.  */
5112           && !dependent_type_p (new_scope))
5113         {
5114           new_scope = complete_type (new_scope);
5115           /* If it is a typedef to current class, use the current
5116              class instead, as the typedef won't have any names inside
5117              it yet.  */
5118           if (!COMPLETE_TYPE_P (new_scope)
5119               && currently_open_class (new_scope))
5120             new_scope = TYPE_MAIN_VARIANT (new_scope);
5121         }
5122       /* Make sure we look in the right scope the next time through
5123          the loop.  */
5124       parser->scope = new_scope;
5125     }
5126
5127   /* If parsing tentatively, replace the sequence of tokens that makes
5128      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5129      token.  That way, should we re-parse the token stream, we will
5130      not have to repeat the effort required to do the parse, nor will
5131      we issue duplicate error messages.  */
5132   if (success && start)
5133     {
5134       cp_token *token;
5135
5136       token = cp_lexer_token_at (parser->lexer, start);
5137       /* Reset the contents of the START token.  */
5138       token->type = CPP_NESTED_NAME_SPECIFIER;
5139       /* Retrieve any deferred checks.  Do not pop this access checks yet
5140          so the memory will not be reclaimed during token replacing below.  */
5141       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5142       token->u.tree_check_value->value = parser->scope;
5143       token->u.tree_check_value->checks = get_deferred_access_checks ();
5144       token->u.tree_check_value->qualifying_scope =
5145         parser->qualifying_scope;
5146       token->keyword = RID_MAX;
5147
5148       /* Purge all subsequent tokens.  */
5149       cp_lexer_purge_tokens_after (parser->lexer, start);
5150     }
5151
5152   if (start)
5153     pop_to_parent_deferring_access_checks ();
5154
5155   return success ? parser->scope : NULL_TREE;
5156 }
5157
5158 /* Parse a nested-name-specifier.  See
5159    cp_parser_nested_name_specifier_opt for details.  This function
5160    behaves identically, except that it will an issue an error if no
5161    nested-name-specifier is present.  */
5162
5163 static tree
5164 cp_parser_nested_name_specifier (cp_parser *parser,
5165                                  bool typename_keyword_p,
5166                                  bool check_dependency_p,
5167                                  bool type_p,
5168                                  bool is_declaration)
5169 {
5170   tree scope;
5171
5172   /* Look for the nested-name-specifier.  */
5173   scope = cp_parser_nested_name_specifier_opt (parser,
5174                                                typename_keyword_p,
5175                                                check_dependency_p,
5176                                                type_p,
5177                                                is_declaration);
5178   /* If it was not present, issue an error message.  */
5179   if (!scope)
5180     {
5181       cp_parser_error (parser, "expected nested-name-specifier");
5182       parser->scope = NULL_TREE;
5183     }
5184
5185   return scope;
5186 }
5187
5188 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5189    this is either a class-name or a namespace-name (which corresponds
5190    to the class-or-namespace-name production in the grammar). For
5191    C++0x, it can also be a type-name that refers to an enumeration
5192    type or a simple-template-id.
5193
5194    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5195    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5196    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5197    TYPE_P is TRUE iff the next name should be taken as a class-name,
5198    even the same name is declared to be another entity in the same
5199    scope.
5200
5201    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5202    specified by the class-or-namespace-name.  If neither is found the
5203    ERROR_MARK_NODE is returned.  */
5204
5205 static tree
5206 cp_parser_qualifying_entity (cp_parser *parser,
5207                              bool typename_keyword_p,
5208                              bool template_keyword_p,
5209                              bool check_dependency_p,
5210                              bool type_p,
5211                              bool is_declaration)
5212 {
5213   tree saved_scope;
5214   tree saved_qualifying_scope;
5215   tree saved_object_scope;
5216   tree scope;
5217   bool only_class_p;
5218   bool successful_parse_p;
5219
5220   /* DR 743: decltype can appear in a nested-name-specifier.  */
5221   if (cp_lexer_next_token_is_decltype (parser->lexer))
5222     {
5223       scope = cp_parser_decltype (parser);
5224       if (TREE_CODE (scope) != ENUMERAL_TYPE
5225           && !MAYBE_CLASS_TYPE_P (scope))
5226         {
5227           cp_parser_simulate_error (parser);
5228           return error_mark_node;
5229         }
5230       if (TYPE_NAME (scope))
5231         scope = TYPE_NAME (scope);
5232       return scope;
5233     }
5234
5235   /* Before we try to parse the class-name, we must save away the
5236      current PARSER->SCOPE since cp_parser_class_name will destroy
5237      it.  */
5238   saved_scope = parser->scope;
5239   saved_qualifying_scope = parser->qualifying_scope;
5240   saved_object_scope = parser->object_scope;
5241   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5242      there is no need to look for a namespace-name.  */
5243   only_class_p = template_keyword_p 
5244     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5245   if (!only_class_p)
5246     cp_parser_parse_tentatively (parser);
5247   scope = cp_parser_class_name (parser,
5248                                 typename_keyword_p,
5249                                 template_keyword_p,
5250                                 type_p ? class_type : none_type,
5251                                 check_dependency_p,
5252                                 /*class_head_p=*/false,
5253                                 is_declaration);
5254   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5255   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5256   if (!only_class_p 
5257       && cxx_dialect != cxx98
5258       && !successful_parse_p)
5259     {
5260       /* Restore the saved scope.  */
5261       parser->scope = saved_scope;
5262       parser->qualifying_scope = saved_qualifying_scope;
5263       parser->object_scope = saved_object_scope;
5264
5265       /* Parse tentatively.  */
5266       cp_parser_parse_tentatively (parser);
5267      
5268       /* Parse a type-name  */
5269       scope = cp_parser_type_name (parser);
5270
5271       /* "If the name found does not designate a namespace or a class,
5272          enumeration, or dependent type, the program is ill-formed."
5273
5274          We cover classes and dependent types above and namespaces below,
5275          so this code is only looking for enums.  */
5276       if (!scope || TREE_CODE (scope) != TYPE_DECL
5277           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5278         cp_parser_simulate_error (parser);
5279
5280       successful_parse_p = cp_parser_parse_definitely (parser);
5281     }
5282   /* If that didn't work, try for a namespace-name.  */
5283   if (!only_class_p && !successful_parse_p)
5284     {
5285       /* Restore the saved scope.  */
5286       parser->scope = saved_scope;
5287       parser->qualifying_scope = saved_qualifying_scope;
5288       parser->object_scope = saved_object_scope;
5289       /* If we are not looking at an identifier followed by the scope
5290          resolution operator, then this is not part of a
5291          nested-name-specifier.  (Note that this function is only used
5292          to parse the components of a nested-name-specifier.)  */
5293       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5294           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5295         return error_mark_node;
5296       scope = cp_parser_namespace_name (parser);
5297     }
5298
5299   return scope;
5300 }
5301
5302 /* Parse a postfix-expression.
5303
5304    postfix-expression:
5305      primary-expression
5306      postfix-expression [ expression ]
5307      postfix-expression ( expression-list [opt] )
5308      simple-type-specifier ( expression-list [opt] )
5309      typename :: [opt] nested-name-specifier identifier
5310        ( expression-list [opt] )
5311      typename :: [opt] nested-name-specifier template [opt] template-id
5312        ( expression-list [opt] )
5313      postfix-expression . template [opt] id-expression
5314      postfix-expression -> template [opt] id-expression
5315      postfix-expression . pseudo-destructor-name
5316      postfix-expression -> pseudo-destructor-name
5317      postfix-expression ++
5318      postfix-expression --
5319      dynamic_cast < type-id > ( expression )
5320      static_cast < type-id > ( expression )
5321      reinterpret_cast < type-id > ( expression )
5322      const_cast < type-id > ( expression )
5323      typeid ( expression )
5324      typeid ( type-id )
5325
5326    GNU Extension:
5327
5328    postfix-expression:
5329      ( type-id ) { initializer-list , [opt] }
5330
5331    This extension is a GNU version of the C99 compound-literal
5332    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5333    but they are essentially the same concept.)
5334
5335    If ADDRESS_P is true, the postfix expression is the operand of the
5336    `&' operator.  CAST_P is true if this expression is the target of a
5337    cast.
5338
5339    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5340    class member access expressions [expr.ref].
5341
5342    Returns a representation of the expression.  */
5343
5344 static tree
5345 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5346                               bool member_access_only_p,
5347                               cp_id_kind * pidk_return)
5348 {
5349   cp_token *token;
5350   enum rid keyword;
5351   cp_id_kind idk = CP_ID_KIND_NONE;
5352   tree postfix_expression = NULL_TREE;
5353   bool is_member_access = false;
5354
5355   /* Peek at the next token.  */
5356   token = cp_lexer_peek_token (parser->lexer);
5357   /* Some of the productions are determined by keywords.  */
5358   keyword = token->keyword;
5359   switch (keyword)
5360     {
5361     case RID_DYNCAST:
5362     case RID_STATCAST:
5363     case RID_REINTCAST:
5364     case RID_CONSTCAST:
5365       {
5366         tree type;
5367         tree expression;
5368         const char *saved_message;
5369
5370         /* All of these can be handled in the same way from the point
5371            of view of parsing.  Begin by consuming the token
5372            identifying the cast.  */
5373         cp_lexer_consume_token (parser->lexer);
5374
5375         /* New types cannot be defined in the cast.  */
5376         saved_message = parser->type_definition_forbidden_message;
5377         parser->type_definition_forbidden_message
5378           = G_("types may not be defined in casts");
5379
5380         /* Look for the opening `<'.  */
5381         cp_parser_require (parser, CPP_LESS, RT_LESS);
5382         /* Parse the type to which we are casting.  */
5383         type = cp_parser_type_id (parser);
5384         /* Look for the closing `>'.  */
5385         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5386         /* Restore the old message.  */
5387         parser->type_definition_forbidden_message = saved_message;
5388
5389         /* And the expression which is being cast.  */
5390         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5391         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5392         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5393
5394         /* Only type conversions to integral or enumeration types
5395            can be used in constant-expressions.  */
5396         if (!cast_valid_in_integral_constant_expression_p (type)
5397             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5398           return error_mark_node;
5399
5400         switch (keyword)
5401           {
5402           case RID_DYNCAST:
5403             postfix_expression
5404               = build_dynamic_cast (type, expression, tf_warning_or_error);
5405             break;
5406           case RID_STATCAST:
5407             postfix_expression
5408               = build_static_cast (type, expression, tf_warning_or_error);
5409             break;
5410           case RID_REINTCAST:
5411             postfix_expression
5412               = build_reinterpret_cast (type, expression, 
5413                                         tf_warning_or_error);
5414             break;
5415           case RID_CONSTCAST:
5416             postfix_expression
5417               = build_const_cast (type, expression, tf_warning_or_error);
5418             break;
5419           default:
5420             gcc_unreachable ();
5421           }
5422       }
5423       break;
5424
5425     case RID_TYPEID:
5426       {
5427         tree type;
5428         const char *saved_message;
5429         bool saved_in_type_id_in_expr_p;
5430
5431         /* Consume the `typeid' token.  */
5432         cp_lexer_consume_token (parser->lexer);
5433         /* Look for the `(' token.  */
5434         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5435         /* Types cannot be defined in a `typeid' expression.  */
5436         saved_message = parser->type_definition_forbidden_message;
5437         parser->type_definition_forbidden_message
5438           = G_("types may not be defined in a %<typeid%> expression");
5439         /* We can't be sure yet whether we're looking at a type-id or an
5440            expression.  */
5441         cp_parser_parse_tentatively (parser);
5442         /* Try a type-id first.  */
5443         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5444         parser->in_type_id_in_expr_p = true;
5445         type = cp_parser_type_id (parser);
5446         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5447         /* Look for the `)' token.  Otherwise, we can't be sure that
5448            we're not looking at an expression: consider `typeid (int
5449            (3))', for example.  */
5450         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5451         /* If all went well, simply lookup the type-id.  */
5452         if (cp_parser_parse_definitely (parser))
5453           postfix_expression = get_typeid (type);
5454         /* Otherwise, fall back to the expression variant.  */
5455         else
5456           {
5457             tree expression;
5458
5459             /* Look for an expression.  */
5460             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5461             /* Compute its typeid.  */
5462             postfix_expression = build_typeid (expression);
5463             /* Look for the `)' token.  */
5464             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5465           }
5466         /* Restore the saved message.  */
5467         parser->type_definition_forbidden_message = saved_message;
5468         /* `typeid' may not appear in an integral constant expression.  */
5469         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5470           return error_mark_node;
5471       }
5472       break;
5473
5474     case RID_TYPENAME:
5475       {
5476         tree type;
5477         /* The syntax permitted here is the same permitted for an
5478            elaborated-type-specifier.  */
5479         type = cp_parser_elaborated_type_specifier (parser,
5480                                                     /*is_friend=*/false,
5481                                                     /*is_declaration=*/false);
5482         postfix_expression = cp_parser_functional_cast (parser, type);
5483       }
5484       break;
5485
5486     default:
5487       {
5488         tree type;
5489
5490         /* If the next thing is a simple-type-specifier, we may be
5491            looking at a functional cast.  We could also be looking at
5492            an id-expression.  So, we try the functional cast, and if
5493            that doesn't work we fall back to the primary-expression.  */
5494         cp_parser_parse_tentatively (parser);
5495         /* Look for the simple-type-specifier.  */
5496         type = cp_parser_simple_type_specifier (parser,
5497                                                 /*decl_specs=*/NULL,
5498                                                 CP_PARSER_FLAGS_NONE);
5499         /* Parse the cast itself.  */
5500         if (!cp_parser_error_occurred (parser))
5501           postfix_expression
5502             = cp_parser_functional_cast (parser, type);
5503         /* If that worked, we're done.  */
5504         if (cp_parser_parse_definitely (parser))
5505           break;
5506
5507         /* If the functional-cast didn't work out, try a
5508            compound-literal.  */
5509         if (cp_parser_allow_gnu_extensions_p (parser)
5510             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5511           {
5512             VEC(constructor_elt,gc) *initializer_list = NULL;
5513             bool saved_in_type_id_in_expr_p;
5514
5515             cp_parser_parse_tentatively (parser);
5516             /* Consume the `('.  */
5517             cp_lexer_consume_token (parser->lexer);
5518             /* Parse the type.  */
5519             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5520             parser->in_type_id_in_expr_p = true;
5521             type = cp_parser_type_id (parser);
5522             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5523             /* Look for the `)'.  */
5524             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5525             /* Look for the `{'.  */
5526             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5527             /* If things aren't going well, there's no need to
5528                keep going.  */
5529             if (!cp_parser_error_occurred (parser))
5530               {
5531                 bool non_constant_p;
5532                 /* Parse the initializer-list.  */
5533                 initializer_list
5534                   = cp_parser_initializer_list (parser, &non_constant_p);
5535                 /* Allow a trailing `,'.  */
5536                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5537                   cp_lexer_consume_token (parser->lexer);
5538                 /* Look for the final `}'.  */
5539                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5540               }
5541             /* If that worked, we're definitely looking at a
5542                compound-literal expression.  */
5543             if (cp_parser_parse_definitely (parser))
5544               {
5545                 /* Warn the user that a compound literal is not
5546                    allowed in standard C++.  */
5547                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5548                 /* For simplicity, we disallow compound literals in
5549                    constant-expressions.  We could
5550                    allow compound literals of integer type, whose
5551                    initializer was a constant, in constant
5552                    expressions.  Permitting that usage, as a further
5553                    extension, would not change the meaning of any
5554                    currently accepted programs.  (Of course, as
5555                    compound literals are not part of ISO C++, the
5556                    standard has nothing to say.)  */
5557                 if (cp_parser_non_integral_constant_expression (parser,
5558                                                                 NIC_NCC))
5559                   {
5560                     postfix_expression = error_mark_node;
5561                     break;
5562                   }
5563                 /* Form the representation of the compound-literal.  */
5564                 postfix_expression
5565                   = (finish_compound_literal
5566                      (type, build_constructor (init_list_type_node,
5567                                                initializer_list),
5568                       tf_warning_or_error));
5569                 break;
5570               }
5571           }
5572
5573         /* It must be a primary-expression.  */
5574         postfix_expression
5575           = cp_parser_primary_expression (parser, address_p, cast_p,
5576                                           /*template_arg_p=*/false,
5577                                           &idk);
5578       }
5579       break;
5580     }
5581
5582   /* Keep looping until the postfix-expression is complete.  */
5583   while (true)
5584     {
5585       if (idk == CP_ID_KIND_UNQUALIFIED
5586           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5587           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5588         /* It is not a Koenig lookup function call.  */
5589         postfix_expression
5590           = unqualified_name_lookup_error (postfix_expression);
5591
5592       /* Peek at the next token.  */
5593       token = cp_lexer_peek_token (parser->lexer);
5594
5595       switch (token->type)
5596         {
5597         case CPP_OPEN_SQUARE:
5598           postfix_expression
5599             = cp_parser_postfix_open_square_expression (parser,
5600                                                         postfix_expression,
5601                                                         false);
5602           idk = CP_ID_KIND_NONE;
5603           is_member_access = false;
5604           break;
5605
5606         case CPP_OPEN_PAREN:
5607           /* postfix-expression ( expression-list [opt] ) */
5608           {
5609             bool koenig_p;
5610             bool is_builtin_constant_p;
5611             bool saved_integral_constant_expression_p = false;
5612             bool saved_non_integral_constant_expression_p = false;
5613             VEC(tree,gc) *args;
5614
5615             is_member_access = false;
5616
5617             is_builtin_constant_p
5618               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5619             if (is_builtin_constant_p)
5620               {
5621                 /* The whole point of __builtin_constant_p is to allow
5622                    non-constant expressions to appear as arguments.  */
5623                 saved_integral_constant_expression_p
5624                   = parser->integral_constant_expression_p;
5625                 saved_non_integral_constant_expression_p
5626                   = parser->non_integral_constant_expression_p;
5627                 parser->integral_constant_expression_p = false;
5628               }
5629             args = (cp_parser_parenthesized_expression_list
5630                     (parser, non_attr,
5631                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5632                      /*non_constant_p=*/NULL));
5633             if (is_builtin_constant_p)
5634               {
5635                 parser->integral_constant_expression_p
5636                   = saved_integral_constant_expression_p;
5637                 parser->non_integral_constant_expression_p
5638                   = saved_non_integral_constant_expression_p;
5639               }
5640
5641             if (args == NULL)
5642               {
5643                 postfix_expression = error_mark_node;
5644                 break;
5645               }
5646
5647             /* Function calls are not permitted in
5648                constant-expressions.  */
5649             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5650                 && cp_parser_non_integral_constant_expression (parser,
5651                                                                NIC_FUNC_CALL))
5652               {
5653                 postfix_expression = error_mark_node;
5654                 release_tree_vector (args);
5655                 break;
5656               }
5657
5658             koenig_p = false;
5659             if (idk == CP_ID_KIND_UNQUALIFIED
5660                 || idk == CP_ID_KIND_TEMPLATE_ID)
5661               {
5662                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5663                   {
5664                     if (!VEC_empty (tree, args))
5665                       {
5666                         koenig_p = true;
5667                         if (!any_type_dependent_arguments_p (args))
5668                           postfix_expression
5669                             = perform_koenig_lookup (postfix_expression, args,
5670                                                      /*include_std=*/false,
5671                                                      tf_warning_or_error);
5672                       }
5673                     else
5674                       postfix_expression
5675                         = unqualified_fn_lookup_error (postfix_expression);
5676                   }
5677                 /* We do not perform argument-dependent lookup if
5678                    normal lookup finds a non-function, in accordance
5679                    with the expected resolution of DR 218.  */
5680                 else if (!VEC_empty (tree, args)
5681                          && is_overloaded_fn (postfix_expression))
5682                   {
5683                     tree fn = get_first_fn (postfix_expression);
5684                     fn = STRIP_TEMPLATE (fn);
5685
5686                     /* Do not do argument dependent lookup if regular
5687                        lookup finds a member function or a block-scope
5688                        function declaration.  [basic.lookup.argdep]/3  */
5689                     if (!DECL_FUNCTION_MEMBER_P (fn)
5690                         && !DECL_LOCAL_FUNCTION_P (fn))
5691                       {
5692                         koenig_p = true;
5693                         if (!any_type_dependent_arguments_p (args))
5694                           postfix_expression
5695                             = perform_koenig_lookup (postfix_expression, args,
5696                                                      /*include_std=*/false,
5697                                                      tf_warning_or_error);
5698                       }
5699                   }
5700               }
5701
5702             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5703               {
5704                 tree instance = TREE_OPERAND (postfix_expression, 0);
5705                 tree fn = TREE_OPERAND (postfix_expression, 1);
5706
5707                 if (processing_template_decl
5708                     && (type_dependent_expression_p (instance)
5709                         || (!BASELINK_P (fn)
5710                             && TREE_CODE (fn) != FIELD_DECL)
5711                         || type_dependent_expression_p (fn)
5712                         || any_type_dependent_arguments_p (args)))
5713                   {
5714                     postfix_expression
5715                       = build_nt_call_vec (postfix_expression, args);
5716                     release_tree_vector (args);
5717                     break;
5718                   }
5719
5720                 if (BASELINK_P (fn))
5721                   {
5722                   postfix_expression
5723                     = (build_new_method_call
5724                        (instance, fn, &args, NULL_TREE,
5725                         (idk == CP_ID_KIND_QUALIFIED
5726                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5727                          : LOOKUP_NORMAL),
5728                         /*fn_p=*/NULL,
5729                         tf_warning_or_error));
5730                   }
5731                 else
5732                   postfix_expression
5733                     = finish_call_expr (postfix_expression, &args,
5734                                         /*disallow_virtual=*/false,
5735                                         /*koenig_p=*/false,
5736                                         tf_warning_or_error);
5737               }
5738             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5739                      || TREE_CODE (postfix_expression) == MEMBER_REF
5740                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5741               postfix_expression = (build_offset_ref_call_from_tree
5742                                     (postfix_expression, &args));
5743             else if (idk == CP_ID_KIND_QUALIFIED)
5744               /* A call to a static class member, or a namespace-scope
5745                  function.  */
5746               postfix_expression
5747                 = finish_call_expr (postfix_expression, &args,
5748                                     /*disallow_virtual=*/true,
5749                                     koenig_p,
5750                                     tf_warning_or_error);
5751             else
5752               /* All other function calls.  */
5753               postfix_expression
5754                 = finish_call_expr (postfix_expression, &args,
5755                                     /*disallow_virtual=*/false,
5756                                     koenig_p,
5757                                     tf_warning_or_error);
5758
5759             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5760             idk = CP_ID_KIND_NONE;
5761
5762             release_tree_vector (args);
5763           }
5764           break;
5765
5766         case CPP_DOT:
5767         case CPP_DEREF:
5768           /* postfix-expression . template [opt] id-expression
5769              postfix-expression . pseudo-destructor-name
5770              postfix-expression -> template [opt] id-expression
5771              postfix-expression -> pseudo-destructor-name */
5772
5773           /* Consume the `.' or `->' operator.  */
5774           cp_lexer_consume_token (parser->lexer);
5775
5776           postfix_expression
5777             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5778                                                       postfix_expression,
5779                                                       false, &idk,
5780                                                       token->location);
5781
5782           is_member_access = true;
5783           break;
5784
5785         case CPP_PLUS_PLUS:
5786           /* postfix-expression ++  */
5787           /* Consume the `++' token.  */
5788           cp_lexer_consume_token (parser->lexer);
5789           /* Generate a representation for the complete expression.  */
5790           postfix_expression
5791             = finish_increment_expr (postfix_expression,
5792                                      POSTINCREMENT_EXPR);
5793           /* Increments may not appear in constant-expressions.  */
5794           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5795             postfix_expression = error_mark_node;
5796           idk = CP_ID_KIND_NONE;
5797           is_member_access = false;
5798           break;
5799
5800         case CPP_MINUS_MINUS:
5801           /* postfix-expression -- */
5802           /* Consume the `--' token.  */
5803           cp_lexer_consume_token (parser->lexer);
5804           /* Generate a representation for the complete expression.  */
5805           postfix_expression
5806             = finish_increment_expr (postfix_expression,
5807                                      POSTDECREMENT_EXPR);
5808           /* Decrements may not appear in constant-expressions.  */
5809           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5810             postfix_expression = error_mark_node;
5811           idk = CP_ID_KIND_NONE;
5812           is_member_access = false;
5813           break;
5814
5815         default:
5816           if (pidk_return != NULL)
5817             * pidk_return = idk;
5818           if (member_access_only_p)
5819             return is_member_access? postfix_expression : error_mark_node;
5820           else
5821             return postfix_expression;
5822         }
5823     }
5824
5825   /* We should never get here.  */
5826   gcc_unreachable ();
5827   return error_mark_node;
5828 }
5829
5830 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5831    by cp_parser_builtin_offsetof.  We're looking for
5832
5833      postfix-expression [ expression ]
5834      postfix-expression [ braced-init-list ] (C++11)
5835
5836    FOR_OFFSETOF is set if we're being called in that context, which
5837    changes how we deal with integer constant expressions.  */
5838
5839 static tree
5840 cp_parser_postfix_open_square_expression (cp_parser *parser,
5841                                           tree postfix_expression,
5842                                           bool for_offsetof)
5843 {
5844   tree index;
5845
5846   /* Consume the `[' token.  */
5847   cp_lexer_consume_token (parser->lexer);
5848
5849   /* Parse the index expression.  */
5850   /* ??? For offsetof, there is a question of what to allow here.  If
5851      offsetof is not being used in an integral constant expression context,
5852      then we *could* get the right answer by computing the value at runtime.
5853      If we are in an integral constant expression context, then we might
5854      could accept any constant expression; hard to say without analysis.
5855      Rather than open the barn door too wide right away, allow only integer
5856      constant expressions here.  */
5857   if (for_offsetof)
5858     index = cp_parser_constant_expression (parser, false, NULL);
5859   else
5860     {
5861       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5862         {
5863           bool expr_nonconst_p;
5864           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5865           index = cp_parser_braced_list (parser, &expr_nonconst_p);
5866         }
5867       else
5868         index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5869     }
5870
5871   /* Look for the closing `]'.  */
5872   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5873
5874   /* Build the ARRAY_REF.  */
5875   postfix_expression = grok_array_decl (postfix_expression, index);
5876
5877   /* When not doing offsetof, array references are not permitted in
5878      constant-expressions.  */
5879   if (!for_offsetof
5880       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5881     postfix_expression = error_mark_node;
5882
5883   return postfix_expression;
5884 }
5885
5886 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5887    by cp_parser_builtin_offsetof.  We're looking for
5888
5889      postfix-expression . template [opt] id-expression
5890      postfix-expression . pseudo-destructor-name
5891      postfix-expression -> template [opt] id-expression
5892      postfix-expression -> pseudo-destructor-name
5893
5894    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5895    limits what of the above we'll actually accept, but nevermind.
5896    TOKEN_TYPE is the "." or "->" token, which will already have been
5897    removed from the stream.  */
5898
5899 static tree
5900 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5901                                         enum cpp_ttype token_type,
5902                                         tree postfix_expression,
5903                                         bool for_offsetof, cp_id_kind *idk,
5904                                         location_t location)
5905 {
5906   tree name;
5907   bool dependent_p;
5908   bool pseudo_destructor_p;
5909   tree scope = NULL_TREE;
5910
5911   /* If this is a `->' operator, dereference the pointer.  */
5912   if (token_type == CPP_DEREF)
5913     postfix_expression = build_x_arrow (postfix_expression);
5914   /* Check to see whether or not the expression is type-dependent.  */
5915   dependent_p = type_dependent_expression_p (postfix_expression);
5916   /* The identifier following the `->' or `.' is not qualified.  */
5917   parser->scope = NULL_TREE;
5918   parser->qualifying_scope = NULL_TREE;
5919   parser->object_scope = NULL_TREE;
5920   *idk = CP_ID_KIND_NONE;
5921
5922   /* Enter the scope corresponding to the type of the object
5923      given by the POSTFIX_EXPRESSION.  */
5924   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5925     {
5926       scope = TREE_TYPE (postfix_expression);
5927       /* According to the standard, no expression should ever have
5928          reference type.  Unfortunately, we do not currently match
5929          the standard in this respect in that our internal representation
5930          of an expression may have reference type even when the standard
5931          says it does not.  Therefore, we have to manually obtain the
5932          underlying type here.  */
5933       scope = non_reference (scope);
5934       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5935       if (scope == unknown_type_node)
5936         {
5937           error_at (location, "%qE does not have class type",
5938                     postfix_expression);
5939           scope = NULL_TREE;
5940         }
5941       /* Unlike the object expression in other contexts, *this is not
5942          required to be of complete type for purposes of class member
5943          access (5.2.5) outside the member function body.  */
5944       else if (scope != current_class_ref
5945                && !(processing_template_decl && scope == current_class_type))
5946         scope = complete_type_or_else (scope, NULL_TREE);
5947       /* Let the name lookup machinery know that we are processing a
5948          class member access expression.  */
5949       parser->context->object_type = scope;
5950       /* If something went wrong, we want to be able to discern that case,
5951          as opposed to the case where there was no SCOPE due to the type
5952          of expression being dependent.  */
5953       if (!scope)
5954         scope = error_mark_node;
5955       /* If the SCOPE was erroneous, make the various semantic analysis
5956          functions exit quickly -- and without issuing additional error
5957          messages.  */
5958       if (scope == error_mark_node)
5959         postfix_expression = error_mark_node;
5960     }
5961
5962   /* Assume this expression is not a pseudo-destructor access.  */
5963   pseudo_destructor_p = false;
5964
5965   /* If the SCOPE is a scalar type, then, if this is a valid program,
5966      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5967      is type dependent, it can be pseudo-destructor-name or something else.
5968      Try to parse it as pseudo-destructor-name first.  */
5969   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5970     {
5971       tree s;
5972       tree type;
5973
5974       cp_parser_parse_tentatively (parser);
5975       /* Parse the pseudo-destructor-name.  */
5976       s = NULL_TREE;
5977       cp_parser_pseudo_destructor_name (parser, &s, &type);
5978       if (dependent_p
5979           && (cp_parser_error_occurred (parser)
5980               || TREE_CODE (type) != TYPE_DECL
5981               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5982         cp_parser_abort_tentative_parse (parser);
5983       else if (cp_parser_parse_definitely (parser))
5984         {
5985           pseudo_destructor_p = true;
5986           postfix_expression
5987             = finish_pseudo_destructor_expr (postfix_expression,
5988                                              s, TREE_TYPE (type));
5989         }
5990     }
5991
5992   if (!pseudo_destructor_p)
5993     {
5994       /* If the SCOPE is not a scalar type, we are looking at an
5995          ordinary class member access expression, rather than a
5996          pseudo-destructor-name.  */
5997       bool template_p;
5998       cp_token *token = cp_lexer_peek_token (parser->lexer);
5999       /* Parse the id-expression.  */
6000       name = (cp_parser_id_expression
6001               (parser,
6002                cp_parser_optional_template_keyword (parser),
6003                /*check_dependency_p=*/true,
6004                &template_p,
6005                /*declarator_p=*/false,
6006                /*optional_p=*/false));
6007       /* In general, build a SCOPE_REF if the member name is qualified.
6008          However, if the name was not dependent and has already been
6009          resolved; there is no need to build the SCOPE_REF.  For example;
6010
6011              struct X { void f(); };
6012              template <typename T> void f(T* t) { t->X::f(); }
6013
6014          Even though "t" is dependent, "X::f" is not and has been resolved
6015          to a BASELINK; there is no need to include scope information.  */
6016
6017       /* But we do need to remember that there was an explicit scope for
6018          virtual function calls.  */
6019       if (parser->scope)
6020         *idk = CP_ID_KIND_QUALIFIED;
6021
6022       /* If the name is a template-id that names a type, we will get a
6023          TYPE_DECL here.  That is invalid code.  */
6024       if (TREE_CODE (name) == TYPE_DECL)
6025         {
6026           error_at (token->location, "invalid use of %qD", name);
6027           postfix_expression = error_mark_node;
6028         }
6029       else
6030         {
6031           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6032             {
6033               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6034                 {
6035                   error_at (token->location, "%<%D::%D%> is not a class member",
6036                             parser->scope, name);
6037                   postfix_expression = error_mark_node;
6038                 }
6039               else
6040                 name = build_qualified_name (/*type=*/NULL_TREE,
6041                                              parser->scope,
6042                                              name,
6043                                              template_p);
6044               parser->scope = NULL_TREE;
6045               parser->qualifying_scope = NULL_TREE;
6046               parser->object_scope = NULL_TREE;
6047             }
6048           if (scope && name && BASELINK_P (name))
6049             adjust_result_of_qualified_name_lookup
6050               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6051           postfix_expression
6052             = finish_class_member_access_expr (postfix_expression, name,
6053                                                template_p, 
6054                                                tf_warning_or_error);
6055         }
6056     }
6057
6058   /* We no longer need to look up names in the scope of the object on
6059      the left-hand side of the `.' or `->' operator.  */
6060   parser->context->object_type = NULL_TREE;
6061
6062   /* Outside of offsetof, these operators may not appear in
6063      constant-expressions.  */
6064   if (!for_offsetof
6065       && (cp_parser_non_integral_constant_expression
6066           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6067     postfix_expression = error_mark_node;
6068
6069   return postfix_expression;
6070 }
6071
6072 /* Parse a parenthesized expression-list.
6073
6074    expression-list:
6075      assignment-expression
6076      expression-list, assignment-expression
6077
6078    attribute-list:
6079      expression-list
6080      identifier
6081      identifier, expression-list
6082
6083    CAST_P is true if this expression is the target of a cast.
6084
6085    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6086    argument pack.
6087
6088    Returns a vector of trees.  Each element is a representation of an
6089    assignment-expression.  NULL is returned if the ( and or ) are
6090    missing.  An empty, but allocated, vector is returned on no
6091    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6092    if we are parsing an attribute list for an attribute that wants a
6093    plain identifier argument, normal_attr for an attribute that wants
6094    an expression, or non_attr if we aren't parsing an attribute list.  If
6095    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6096    not all of the expressions in the list were constant.  */
6097
6098 static VEC(tree,gc) *
6099 cp_parser_parenthesized_expression_list (cp_parser* parser,
6100                                          int is_attribute_list,
6101                                          bool cast_p,
6102                                          bool allow_expansion_p,
6103                                          bool *non_constant_p)
6104 {
6105   VEC(tree,gc) *expression_list;
6106   bool fold_expr_p = is_attribute_list != non_attr;
6107   tree identifier = NULL_TREE;
6108   bool saved_greater_than_is_operator_p;
6109
6110   /* Assume all the expressions will be constant.  */
6111   if (non_constant_p)
6112     *non_constant_p = false;
6113
6114   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6115     return NULL;
6116
6117   expression_list = make_tree_vector ();
6118
6119   /* Within a parenthesized expression, a `>' token is always
6120      the greater-than operator.  */
6121   saved_greater_than_is_operator_p
6122     = parser->greater_than_is_operator_p;
6123   parser->greater_than_is_operator_p = true;
6124
6125   /* Consume expressions until there are no more.  */
6126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6127     while (true)
6128       {
6129         tree expr;
6130
6131         /* At the beginning of attribute lists, check to see if the
6132            next token is an identifier.  */
6133         if (is_attribute_list == id_attr
6134             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6135           {
6136             cp_token *token;
6137
6138             /* Consume the identifier.  */
6139             token = cp_lexer_consume_token (parser->lexer);
6140             /* Save the identifier.  */
6141             identifier = token->u.value;
6142           }
6143         else
6144           {
6145             bool expr_non_constant_p;
6146
6147             /* Parse the next assignment-expression.  */
6148             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6149               {
6150                 /* A braced-init-list.  */
6151                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6152                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6153                 if (non_constant_p && expr_non_constant_p)
6154                   *non_constant_p = true;
6155               }
6156             else if (non_constant_p)
6157               {
6158                 expr = (cp_parser_constant_expression
6159                         (parser, /*allow_non_constant_p=*/true,
6160                          &expr_non_constant_p));
6161                 if (expr_non_constant_p)
6162                   *non_constant_p = true;
6163               }
6164             else
6165               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6166
6167             if (fold_expr_p)
6168               expr = fold_non_dependent_expr (expr);
6169
6170             /* If we have an ellipsis, then this is an expression
6171                expansion.  */
6172             if (allow_expansion_p
6173                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6174               {
6175                 /* Consume the `...'.  */
6176                 cp_lexer_consume_token (parser->lexer);
6177
6178                 /* Build the argument pack.  */
6179                 expr = make_pack_expansion (expr);
6180               }
6181
6182              /* Add it to the list.  We add error_mark_node
6183                 expressions to the list, so that we can still tell if
6184                 the correct form for a parenthesized expression-list
6185                 is found. That gives better errors.  */
6186             VEC_safe_push (tree, gc, expression_list, expr);
6187
6188             if (expr == error_mark_node)
6189               goto skip_comma;
6190           }
6191
6192         /* After the first item, attribute lists look the same as
6193            expression lists.  */
6194         is_attribute_list = non_attr;
6195
6196       get_comma:;
6197         /* If the next token isn't a `,', then we are done.  */
6198         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6199           break;
6200
6201         /* Otherwise, consume the `,' and keep going.  */
6202         cp_lexer_consume_token (parser->lexer);
6203       }
6204
6205   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6206     {
6207       int ending;
6208
6209     skip_comma:;
6210       /* We try and resync to an unnested comma, as that will give the
6211          user better diagnostics.  */
6212       ending = cp_parser_skip_to_closing_parenthesis (parser,
6213                                                       /*recovering=*/true,
6214                                                       /*or_comma=*/true,
6215                                                       /*consume_paren=*/true);
6216       if (ending < 0)
6217         goto get_comma;
6218       if (!ending)
6219         {
6220           parser->greater_than_is_operator_p
6221             = saved_greater_than_is_operator_p;
6222           return NULL;
6223         }
6224     }
6225
6226   parser->greater_than_is_operator_p
6227     = saved_greater_than_is_operator_p;
6228
6229   if (identifier)
6230     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6231
6232   return expression_list;
6233 }
6234
6235 /* Parse a pseudo-destructor-name.
6236
6237    pseudo-destructor-name:
6238      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6239      :: [opt] nested-name-specifier template template-id :: ~ type-name
6240      :: [opt] nested-name-specifier [opt] ~ type-name
6241
6242    If either of the first two productions is used, sets *SCOPE to the
6243    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6244    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6245    or ERROR_MARK_NODE if the parse fails.  */
6246
6247 static void
6248 cp_parser_pseudo_destructor_name (cp_parser* parser,
6249                                   tree* scope,
6250                                   tree* type)
6251 {
6252   bool nested_name_specifier_p;
6253
6254   /* Assume that things will not work out.  */
6255   *type = error_mark_node;
6256
6257   /* Look for the optional `::' operator.  */
6258   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6259   /* Look for the optional nested-name-specifier.  */
6260   nested_name_specifier_p
6261     = (cp_parser_nested_name_specifier_opt (parser,
6262                                             /*typename_keyword_p=*/false,
6263                                             /*check_dependency_p=*/true,
6264                                             /*type_p=*/false,
6265                                             /*is_declaration=*/false)
6266        != NULL_TREE);
6267   /* Now, if we saw a nested-name-specifier, we might be doing the
6268      second production.  */
6269   if (nested_name_specifier_p
6270       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6271     {
6272       /* Consume the `template' keyword.  */
6273       cp_lexer_consume_token (parser->lexer);
6274       /* Parse the template-id.  */
6275       cp_parser_template_id (parser,
6276                              /*template_keyword_p=*/true,
6277                              /*check_dependency_p=*/false,
6278                              /*is_declaration=*/true);
6279       /* Look for the `::' token.  */
6280       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6281     }
6282   /* If the next token is not a `~', then there might be some
6283      additional qualification.  */
6284   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6285     {
6286       /* At this point, we're looking for "type-name :: ~".  The type-name
6287          must not be a class-name, since this is a pseudo-destructor.  So,
6288          it must be either an enum-name, or a typedef-name -- both of which
6289          are just identifiers.  So, we peek ahead to check that the "::"
6290          and "~" tokens are present; if they are not, then we can avoid
6291          calling type_name.  */
6292       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6293           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6294           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6295         {
6296           cp_parser_error (parser, "non-scalar type");
6297           return;
6298         }
6299
6300       /* Look for the type-name.  */
6301       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6302       if (*scope == error_mark_node)
6303         return;
6304
6305       /* Look for the `::' token.  */
6306       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6307     }
6308   else
6309     *scope = NULL_TREE;
6310
6311   /* Look for the `~'.  */
6312   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6313
6314   /* Once we see the ~, this has to be a pseudo-destructor.  */
6315   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6316     cp_parser_commit_to_tentative_parse (parser);
6317
6318   /* Look for the type-name again.  We are not responsible for
6319      checking that it matches the first type-name.  */
6320   *type = cp_parser_nonclass_name (parser);
6321 }
6322
6323 /* Parse a unary-expression.
6324
6325    unary-expression:
6326      postfix-expression
6327      ++ cast-expression
6328      -- cast-expression
6329      unary-operator cast-expression
6330      sizeof unary-expression
6331      sizeof ( type-id )
6332      alignof ( type-id )  [C++0x]
6333      new-expression
6334      delete-expression
6335
6336    GNU Extensions:
6337
6338    unary-expression:
6339      __extension__ cast-expression
6340      __alignof__ unary-expression
6341      __alignof__ ( type-id )
6342      alignof unary-expression  [C++0x]
6343      __real__ cast-expression
6344      __imag__ cast-expression
6345      && identifier
6346
6347    ADDRESS_P is true iff the unary-expression is appearing as the
6348    operand of the `&' operator.   CAST_P is true if this expression is
6349    the target of a cast.
6350
6351    Returns a representation of the expression.  */
6352
6353 static tree
6354 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6355                             cp_id_kind * pidk)
6356 {
6357   cp_token *token;
6358   enum tree_code unary_operator;
6359
6360   /* Peek at the next token.  */
6361   token = cp_lexer_peek_token (parser->lexer);
6362   /* Some keywords give away the kind of expression.  */
6363   if (token->type == CPP_KEYWORD)
6364     {
6365       enum rid keyword = token->keyword;
6366
6367       switch (keyword)
6368         {
6369         case RID_ALIGNOF:
6370         case RID_SIZEOF:
6371           {
6372             tree operand;
6373             enum tree_code op;
6374
6375             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6376             /* Consume the token.  */
6377             cp_lexer_consume_token (parser->lexer);
6378             /* Parse the operand.  */
6379             operand = cp_parser_sizeof_operand (parser, keyword);
6380
6381             if (TYPE_P (operand))
6382               return cxx_sizeof_or_alignof_type (operand, op, true);
6383             else
6384               {
6385                 /* ISO C++ defines alignof only with types, not with
6386                    expressions. So pedwarn if alignof is used with a non-
6387                    type expression. However, __alignof__ is ok.  */
6388                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6389                   pedwarn (token->location, OPT_pedantic,
6390                            "ISO C++ does not allow %<alignof%> "
6391                            "with a non-type");
6392
6393                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6394               }
6395           }
6396
6397         case RID_NEW:
6398           return cp_parser_new_expression (parser);
6399
6400         case RID_DELETE:
6401           return cp_parser_delete_expression (parser);
6402
6403         case RID_EXTENSION:
6404           {
6405             /* The saved value of the PEDANTIC flag.  */
6406             int saved_pedantic;
6407             tree expr;
6408
6409             /* Save away the PEDANTIC flag.  */
6410             cp_parser_extension_opt (parser, &saved_pedantic);
6411             /* Parse the cast-expression.  */
6412             expr = cp_parser_simple_cast_expression (parser);
6413             /* Restore the PEDANTIC flag.  */
6414             pedantic = saved_pedantic;
6415
6416             return expr;
6417           }
6418
6419         case RID_REALPART:
6420         case RID_IMAGPART:
6421           {
6422             tree expression;
6423
6424             /* Consume the `__real__' or `__imag__' token.  */
6425             cp_lexer_consume_token (parser->lexer);
6426             /* Parse the cast-expression.  */
6427             expression = cp_parser_simple_cast_expression (parser);
6428             /* Create the complete representation.  */
6429             return build_x_unary_op ((keyword == RID_REALPART
6430                                       ? REALPART_EXPR : IMAGPART_EXPR),
6431                                      expression,
6432                                      tf_warning_or_error);
6433           }
6434           break;
6435
6436         case RID_TRANSACTION_ATOMIC:
6437         case RID_TRANSACTION_RELAXED:
6438           return cp_parser_transaction_expression (parser, keyword);
6439
6440         case RID_NOEXCEPT:
6441           {
6442             tree expr;
6443             const char *saved_message;
6444             bool saved_integral_constant_expression_p;
6445             bool saved_non_integral_constant_expression_p;
6446             bool saved_greater_than_is_operator_p;
6447
6448             cp_lexer_consume_token (parser->lexer);
6449             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6450
6451             saved_message = parser->type_definition_forbidden_message;
6452             parser->type_definition_forbidden_message
6453               = G_("types may not be defined in %<noexcept%> expressions");
6454
6455             saved_integral_constant_expression_p
6456               = parser->integral_constant_expression_p;
6457             saved_non_integral_constant_expression_p
6458               = parser->non_integral_constant_expression_p;
6459             parser->integral_constant_expression_p = false;
6460
6461             saved_greater_than_is_operator_p
6462               = parser->greater_than_is_operator_p;
6463             parser->greater_than_is_operator_p = true;
6464
6465             ++cp_unevaluated_operand;
6466             ++c_inhibit_evaluation_warnings;
6467             expr = cp_parser_expression (parser, false, NULL);
6468             --c_inhibit_evaluation_warnings;
6469             --cp_unevaluated_operand;
6470
6471             parser->greater_than_is_operator_p
6472               = saved_greater_than_is_operator_p;
6473
6474             parser->integral_constant_expression_p
6475               = saved_integral_constant_expression_p;
6476             parser->non_integral_constant_expression_p
6477               = saved_non_integral_constant_expression_p;
6478
6479             parser->type_definition_forbidden_message = saved_message;
6480
6481             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6482             return finish_noexcept_expr (expr, tf_warning_or_error);
6483           }
6484
6485         default:
6486           break;
6487         }
6488     }
6489
6490   /* Look for the `:: new' and `:: delete', which also signal the
6491      beginning of a new-expression, or delete-expression,
6492      respectively.  If the next token is `::', then it might be one of
6493      these.  */
6494   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6495     {
6496       enum rid keyword;
6497
6498       /* See if the token after the `::' is one of the keywords in
6499          which we're interested.  */
6500       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6501       /* If it's `new', we have a new-expression.  */
6502       if (keyword == RID_NEW)
6503         return cp_parser_new_expression (parser);
6504       /* Similarly, for `delete'.  */
6505       else if (keyword == RID_DELETE)
6506         return cp_parser_delete_expression (parser);
6507     }
6508
6509   /* Look for a unary operator.  */
6510   unary_operator = cp_parser_unary_operator (token);
6511   /* The `++' and `--' operators can be handled similarly, even though
6512      they are not technically unary-operators in the grammar.  */
6513   if (unary_operator == ERROR_MARK)
6514     {
6515       if (token->type == CPP_PLUS_PLUS)
6516         unary_operator = PREINCREMENT_EXPR;
6517       else if (token->type == CPP_MINUS_MINUS)
6518         unary_operator = PREDECREMENT_EXPR;
6519       /* Handle the GNU address-of-label extension.  */
6520       else if (cp_parser_allow_gnu_extensions_p (parser)
6521                && token->type == CPP_AND_AND)
6522         {
6523           tree identifier;
6524           tree expression;
6525           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6526
6527           /* Consume the '&&' token.  */
6528           cp_lexer_consume_token (parser->lexer);
6529           /* Look for the identifier.  */
6530           identifier = cp_parser_identifier (parser);
6531           /* Create an expression representing the address.  */
6532           expression = finish_label_address_expr (identifier, loc);
6533           if (cp_parser_non_integral_constant_expression (parser,
6534                                                           NIC_ADDR_LABEL))
6535             expression = error_mark_node;
6536           return expression;
6537         }
6538     }
6539   if (unary_operator != ERROR_MARK)
6540     {
6541       tree cast_expression;
6542       tree expression = error_mark_node;
6543       non_integral_constant non_constant_p = NIC_NONE;
6544
6545       /* Consume the operator token.  */
6546       token = cp_lexer_consume_token (parser->lexer);
6547       /* Parse the cast-expression.  */
6548       cast_expression
6549         = cp_parser_cast_expression (parser,
6550                                      unary_operator == ADDR_EXPR,
6551                                      /*cast_p=*/false, pidk);
6552       /* Now, build an appropriate representation.  */
6553       switch (unary_operator)
6554         {
6555         case INDIRECT_REF:
6556           non_constant_p = NIC_STAR;
6557           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6558                                              tf_warning_or_error);
6559           break;
6560
6561         case ADDR_EXPR:
6562            non_constant_p = NIC_ADDR;
6563           /* Fall through.  */
6564         case BIT_NOT_EXPR:
6565           expression = build_x_unary_op (unary_operator, cast_expression,
6566                                          tf_warning_or_error);
6567           break;
6568
6569         case PREINCREMENT_EXPR:
6570         case PREDECREMENT_EXPR:
6571           non_constant_p = unary_operator == PREINCREMENT_EXPR
6572                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6573           /* Fall through.  */
6574         case UNARY_PLUS_EXPR:
6575         case NEGATE_EXPR:
6576         case TRUTH_NOT_EXPR:
6577           expression = finish_unary_op_expr (unary_operator, cast_expression);
6578           break;
6579
6580         default:
6581           gcc_unreachable ();
6582         }
6583
6584       if (non_constant_p != NIC_NONE
6585           && cp_parser_non_integral_constant_expression (parser,
6586                                                          non_constant_p))
6587         expression = error_mark_node;
6588
6589       return expression;
6590     }
6591
6592   return cp_parser_postfix_expression (parser, address_p, cast_p,
6593                                        /*member_access_only_p=*/false,
6594                                        pidk);
6595 }
6596
6597 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6598    unary-operator, the corresponding tree code is returned.  */
6599
6600 static enum tree_code
6601 cp_parser_unary_operator (cp_token* token)
6602 {
6603   switch (token->type)
6604     {
6605     case CPP_MULT:
6606       return INDIRECT_REF;
6607
6608     case CPP_AND:
6609       return ADDR_EXPR;
6610
6611     case CPP_PLUS:
6612       return UNARY_PLUS_EXPR;
6613
6614     case CPP_MINUS:
6615       return NEGATE_EXPR;
6616
6617     case CPP_NOT:
6618       return TRUTH_NOT_EXPR;
6619
6620     case CPP_COMPL:
6621       return BIT_NOT_EXPR;
6622
6623     default:
6624       return ERROR_MARK;
6625     }
6626 }
6627
6628 /* Parse a new-expression.
6629
6630    new-expression:
6631      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6632      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6633
6634    Returns a representation of the expression.  */
6635
6636 static tree
6637 cp_parser_new_expression (cp_parser* parser)
6638 {
6639   bool global_scope_p;
6640   VEC(tree,gc) *placement;
6641   tree type;
6642   VEC(tree,gc) *initializer;
6643   tree nelts;
6644   tree ret;
6645
6646   /* Look for the optional `::' operator.  */
6647   global_scope_p
6648     = (cp_parser_global_scope_opt (parser,
6649                                    /*current_scope_valid_p=*/false)
6650        != NULL_TREE);
6651   /* Look for the `new' operator.  */
6652   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6653   /* There's no easy way to tell a new-placement from the
6654      `( type-id )' construct.  */
6655   cp_parser_parse_tentatively (parser);
6656   /* Look for a new-placement.  */
6657   placement = cp_parser_new_placement (parser);
6658   /* If that didn't work out, there's no new-placement.  */
6659   if (!cp_parser_parse_definitely (parser))
6660     {
6661       if (placement != NULL)
6662         release_tree_vector (placement);
6663       placement = NULL;
6664     }
6665
6666   /* If the next token is a `(', then we have a parenthesized
6667      type-id.  */
6668   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6669     {
6670       cp_token *token;
6671       const char *saved_message = parser->type_definition_forbidden_message;
6672
6673       /* Consume the `('.  */
6674       cp_lexer_consume_token (parser->lexer);
6675
6676       /* Parse the type-id.  */
6677       parser->type_definition_forbidden_message
6678         = G_("types may not be defined in a new-expression");
6679       type = cp_parser_type_id (parser);
6680       parser->type_definition_forbidden_message = saved_message;
6681
6682       /* Look for the closing `)'.  */
6683       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6684       token = cp_lexer_peek_token (parser->lexer);
6685       /* There should not be a direct-new-declarator in this production,
6686          but GCC used to allowed this, so we check and emit a sensible error
6687          message for this case.  */
6688       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6689         {
6690           error_at (token->location,
6691                     "array bound forbidden after parenthesized type-id");
6692           inform (token->location, 
6693                   "try removing the parentheses around the type-id");
6694           cp_parser_direct_new_declarator (parser);
6695         }
6696       nelts = NULL_TREE;
6697     }
6698   /* Otherwise, there must be a new-type-id.  */
6699   else
6700     type = cp_parser_new_type_id (parser, &nelts);
6701
6702   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6703   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6704       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6705     initializer = cp_parser_new_initializer (parser);
6706   else
6707     initializer = NULL;
6708
6709   /* A new-expression may not appear in an integral constant
6710      expression.  */
6711   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6712     ret = error_mark_node;
6713   else
6714     {
6715       /* Create a representation of the new-expression.  */
6716       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6717                        tf_warning_or_error);
6718     }
6719
6720   if (placement != NULL)
6721     release_tree_vector (placement);
6722   if (initializer != NULL)
6723     release_tree_vector (initializer);
6724
6725   return ret;
6726 }
6727
6728 /* Parse a new-placement.
6729
6730    new-placement:
6731      ( expression-list )
6732
6733    Returns the same representation as for an expression-list.  */
6734
6735 static VEC(tree,gc) *
6736 cp_parser_new_placement (cp_parser* parser)
6737 {
6738   VEC(tree,gc) *expression_list;
6739
6740   /* Parse the expression-list.  */
6741   expression_list = (cp_parser_parenthesized_expression_list
6742                      (parser, non_attr, /*cast_p=*/false,
6743                       /*allow_expansion_p=*/true,
6744                       /*non_constant_p=*/NULL));
6745
6746   return expression_list;
6747 }
6748
6749 /* Parse a new-type-id.
6750
6751    new-type-id:
6752      type-specifier-seq new-declarator [opt]
6753
6754    Returns the TYPE allocated.  If the new-type-id indicates an array
6755    type, *NELTS is set to the number of elements in the last array
6756    bound; the TYPE will not include the last array bound.  */
6757
6758 static tree
6759 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6760 {
6761   cp_decl_specifier_seq type_specifier_seq;
6762   cp_declarator *new_declarator;
6763   cp_declarator *declarator;
6764   cp_declarator *outer_declarator;
6765   const char *saved_message;
6766   tree type;
6767
6768   /* The type-specifier sequence must not contain type definitions.
6769      (It cannot contain declarations of new types either, but if they
6770      are not definitions we will catch that because they are not
6771      complete.)  */
6772   saved_message = parser->type_definition_forbidden_message;
6773   parser->type_definition_forbidden_message
6774     = G_("types may not be defined in a new-type-id");
6775   /* Parse the type-specifier-seq.  */
6776   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6777                                 /*is_trailing_return=*/false,
6778                                 &type_specifier_seq);
6779   /* Restore the old message.  */
6780   parser->type_definition_forbidden_message = saved_message;
6781   /* Parse the new-declarator.  */
6782   new_declarator = cp_parser_new_declarator_opt (parser);
6783
6784   /* Determine the number of elements in the last array dimension, if
6785      any.  */
6786   *nelts = NULL_TREE;
6787   /* Skip down to the last array dimension.  */
6788   declarator = new_declarator;
6789   outer_declarator = NULL;
6790   while (declarator && (declarator->kind == cdk_pointer
6791                         || declarator->kind == cdk_ptrmem))
6792     {
6793       outer_declarator = declarator;
6794       declarator = declarator->declarator;
6795     }
6796   while (declarator
6797          && declarator->kind == cdk_array
6798          && declarator->declarator
6799          && declarator->declarator->kind == cdk_array)
6800     {
6801       outer_declarator = declarator;
6802       declarator = declarator->declarator;
6803     }
6804
6805   if (declarator && declarator->kind == cdk_array)
6806     {
6807       *nelts = declarator->u.array.bounds;
6808       if (*nelts == error_mark_node)
6809         *nelts = integer_one_node;
6810
6811       if (outer_declarator)
6812         outer_declarator->declarator = declarator->declarator;
6813       else
6814         new_declarator = NULL;
6815     }
6816
6817   type = groktypename (&type_specifier_seq, new_declarator, false);
6818   return type;
6819 }
6820
6821 /* Parse an (optional) new-declarator.
6822
6823    new-declarator:
6824      ptr-operator new-declarator [opt]
6825      direct-new-declarator
6826
6827    Returns the declarator.  */
6828
6829 static cp_declarator *
6830 cp_parser_new_declarator_opt (cp_parser* parser)
6831 {
6832   enum tree_code code;
6833   tree type;
6834   cp_cv_quals cv_quals;
6835
6836   /* We don't know if there's a ptr-operator next, or not.  */
6837   cp_parser_parse_tentatively (parser);
6838   /* Look for a ptr-operator.  */
6839   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6840   /* If that worked, look for more new-declarators.  */
6841   if (cp_parser_parse_definitely (parser))
6842     {
6843       cp_declarator *declarator;
6844
6845       /* Parse another optional declarator.  */
6846       declarator = cp_parser_new_declarator_opt (parser);
6847
6848       return cp_parser_make_indirect_declarator
6849         (code, type, cv_quals, declarator);
6850     }
6851
6852   /* If the next token is a `[', there is a direct-new-declarator.  */
6853   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6854     return cp_parser_direct_new_declarator (parser);
6855
6856   return NULL;
6857 }
6858
6859 /* Parse a direct-new-declarator.
6860
6861    direct-new-declarator:
6862      [ expression ]
6863      direct-new-declarator [constant-expression]
6864
6865    */
6866
6867 static cp_declarator *
6868 cp_parser_direct_new_declarator (cp_parser* parser)
6869 {
6870   cp_declarator *declarator = NULL;
6871
6872   while (true)
6873     {
6874       tree expression;
6875
6876       /* Look for the opening `['.  */
6877       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6878       /* The first expression is not required to be constant.  */
6879       if (!declarator)
6880         {
6881           cp_token *token = cp_lexer_peek_token (parser->lexer);
6882           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6883           /* The standard requires that the expression have integral
6884              type.  DR 74 adds enumeration types.  We believe that the
6885              real intent is that these expressions be handled like the
6886              expression in a `switch' condition, which also allows
6887              classes with a single conversion to integral or
6888              enumeration type.  */
6889           if (!processing_template_decl)
6890             {
6891               expression
6892                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6893                                               expression,
6894                                               /*complain=*/true);
6895               if (!expression)
6896                 {
6897                   error_at (token->location,
6898                             "expression in new-declarator must have integral "
6899                             "or enumeration type");
6900                   expression = error_mark_node;
6901                 }
6902             }
6903         }
6904       /* But all the other expressions must be.  */
6905       else
6906         expression
6907           = cp_parser_constant_expression (parser,
6908                                            /*allow_non_constant=*/false,
6909                                            NULL);
6910       /* Look for the closing `]'.  */
6911       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6912
6913       /* Add this bound to the declarator.  */
6914       declarator = make_array_declarator (declarator, expression);
6915
6916       /* If the next token is not a `[', then there are no more
6917          bounds.  */
6918       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6919         break;
6920     }
6921
6922   return declarator;
6923 }
6924
6925 /* Parse a new-initializer.
6926
6927    new-initializer:
6928      ( expression-list [opt] )
6929      braced-init-list
6930
6931    Returns a representation of the expression-list.  */
6932
6933 static VEC(tree,gc) *
6934 cp_parser_new_initializer (cp_parser* parser)
6935 {
6936   VEC(tree,gc) *expression_list;
6937
6938   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6939     {
6940       tree t;
6941       bool expr_non_constant_p;
6942       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6943       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6944       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6945       expression_list = make_tree_vector_single (t);
6946     }
6947   else
6948     expression_list = (cp_parser_parenthesized_expression_list
6949                        (parser, non_attr, /*cast_p=*/false,
6950                         /*allow_expansion_p=*/true,
6951                         /*non_constant_p=*/NULL));
6952
6953   return expression_list;
6954 }
6955
6956 /* Parse a delete-expression.
6957
6958    delete-expression:
6959      :: [opt] delete cast-expression
6960      :: [opt] delete [ ] cast-expression
6961
6962    Returns a representation of the expression.  */
6963
6964 static tree
6965 cp_parser_delete_expression (cp_parser* parser)
6966 {
6967   bool global_scope_p;
6968   bool array_p;
6969   tree expression;
6970
6971   /* Look for the optional `::' operator.  */
6972   global_scope_p
6973     = (cp_parser_global_scope_opt (parser,
6974                                    /*current_scope_valid_p=*/false)
6975        != NULL_TREE);
6976   /* Look for the `delete' keyword.  */
6977   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6978   /* See if the array syntax is in use.  */
6979   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6980     {
6981       /* Consume the `[' token.  */
6982       cp_lexer_consume_token (parser->lexer);
6983       /* Look for the `]' token.  */
6984       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6985       /* Remember that this is the `[]' construct.  */
6986       array_p = true;
6987     }
6988   else
6989     array_p = false;
6990
6991   /* Parse the cast-expression.  */
6992   expression = cp_parser_simple_cast_expression (parser);
6993
6994   /* A delete-expression may not appear in an integral constant
6995      expression.  */
6996   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6997     return error_mark_node;
6998
6999   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7000                         tf_warning_or_error);
7001 }
7002
7003 /* Returns true if TOKEN may start a cast-expression and false
7004    otherwise.  */
7005
7006 static bool
7007 cp_parser_token_starts_cast_expression (cp_token *token)
7008 {
7009   switch (token->type)
7010     {
7011     case CPP_COMMA:
7012     case CPP_SEMICOLON:
7013     case CPP_QUERY:
7014     case CPP_COLON:
7015     case CPP_CLOSE_SQUARE:
7016     case CPP_CLOSE_PAREN:
7017     case CPP_CLOSE_BRACE:
7018     case CPP_DOT:
7019     case CPP_DOT_STAR:
7020     case CPP_DEREF:
7021     case CPP_DEREF_STAR:
7022     case CPP_DIV:
7023     case CPP_MOD:
7024     case CPP_LSHIFT:
7025     case CPP_RSHIFT:
7026     case CPP_LESS:
7027     case CPP_GREATER:
7028     case CPP_LESS_EQ:
7029     case CPP_GREATER_EQ:
7030     case CPP_EQ_EQ:
7031     case CPP_NOT_EQ:
7032     case CPP_EQ:
7033     case CPP_MULT_EQ:
7034     case CPP_DIV_EQ:
7035     case CPP_MOD_EQ:
7036     case CPP_PLUS_EQ:
7037     case CPP_MINUS_EQ:
7038     case CPP_RSHIFT_EQ:
7039     case CPP_LSHIFT_EQ:
7040     case CPP_AND_EQ:
7041     case CPP_XOR_EQ:
7042     case CPP_OR_EQ:
7043     case CPP_XOR:
7044     case CPP_OR:
7045     case CPP_OR_OR:
7046     case CPP_EOF:
7047       return false;
7048
7049       /* '[' may start a primary-expression in obj-c++.  */
7050     case CPP_OPEN_SQUARE:
7051       return c_dialect_objc ();
7052
7053     default:
7054       return true;
7055     }
7056 }
7057
7058 /* Parse a cast-expression.
7059
7060    cast-expression:
7061      unary-expression
7062      ( type-id ) cast-expression
7063
7064    ADDRESS_P is true iff the unary-expression is appearing as the
7065    operand of the `&' operator.   CAST_P is true if this expression is
7066    the target of a cast.
7067
7068    Returns a representation of the expression.  */
7069
7070 static tree
7071 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7072                            cp_id_kind * pidk)
7073 {
7074   /* If it's a `(', then we might be looking at a cast.  */
7075   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7076     {
7077       tree type = NULL_TREE;
7078       tree expr = NULL_TREE;
7079       bool compound_literal_p;
7080       const char *saved_message;
7081
7082       /* There's no way to know yet whether or not this is a cast.
7083          For example, `(int (3))' is a unary-expression, while `(int)
7084          3' is a cast.  So, we resort to parsing tentatively.  */
7085       cp_parser_parse_tentatively (parser);
7086       /* Types may not be defined in a cast.  */
7087       saved_message = parser->type_definition_forbidden_message;
7088       parser->type_definition_forbidden_message
7089         = G_("types may not be defined in casts");
7090       /* Consume the `('.  */
7091       cp_lexer_consume_token (parser->lexer);
7092       /* A very tricky bit is that `(struct S) { 3 }' is a
7093          compound-literal (which we permit in C++ as an extension).
7094          But, that construct is not a cast-expression -- it is a
7095          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7096          is legal; if the compound-literal were a cast-expression,
7097          you'd need an extra set of parentheses.)  But, if we parse
7098          the type-id, and it happens to be a class-specifier, then we
7099          will commit to the parse at that point, because we cannot
7100          undo the action that is done when creating a new class.  So,
7101          then we cannot back up and do a postfix-expression.
7102
7103          Therefore, we scan ahead to the closing `)', and check to see
7104          if the token after the `)' is a `{'.  If so, we are not
7105          looking at a cast-expression.
7106
7107          Save tokens so that we can put them back.  */
7108       cp_lexer_save_tokens (parser->lexer);
7109       /* Skip tokens until the next token is a closing parenthesis.
7110          If we find the closing `)', and the next token is a `{', then
7111          we are looking at a compound-literal.  */
7112       compound_literal_p
7113         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7114                                                   /*consume_paren=*/true)
7115            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7116       /* Roll back the tokens we skipped.  */
7117       cp_lexer_rollback_tokens (parser->lexer);
7118       /* If we were looking at a compound-literal, simulate an error
7119          so that the call to cp_parser_parse_definitely below will
7120          fail.  */
7121       if (compound_literal_p)
7122         cp_parser_simulate_error (parser);
7123       else
7124         {
7125           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7126           parser->in_type_id_in_expr_p = true;
7127           /* Look for the type-id.  */
7128           type = cp_parser_type_id (parser);
7129           /* Look for the closing `)'.  */
7130           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7131           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7132         }
7133
7134       /* Restore the saved message.  */
7135       parser->type_definition_forbidden_message = saved_message;
7136
7137       /* At this point this can only be either a cast or a
7138          parenthesized ctor such as `(T ())' that looks like a cast to
7139          function returning T.  */
7140       if (!cp_parser_error_occurred (parser)
7141           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7142                                                      (parser->lexer)))
7143         {
7144           cp_parser_parse_definitely (parser);
7145           expr = cp_parser_cast_expression (parser,
7146                                             /*address_p=*/false,
7147                                             /*cast_p=*/true, pidk);
7148
7149           /* Warn about old-style casts, if so requested.  */
7150           if (warn_old_style_cast
7151               && !in_system_header
7152               && !VOID_TYPE_P (type)
7153               && current_lang_name != lang_name_c)
7154             warning (OPT_Wold_style_cast, "use of old-style cast");
7155
7156           /* Only type conversions to integral or enumeration types
7157              can be used in constant-expressions.  */
7158           if (!cast_valid_in_integral_constant_expression_p (type)
7159               && cp_parser_non_integral_constant_expression (parser,
7160                                                              NIC_CAST))
7161             return error_mark_node;
7162
7163           /* Perform the cast.  */
7164           expr = build_c_cast (input_location, type, expr);
7165           return expr;
7166         }
7167       else 
7168         cp_parser_abort_tentative_parse (parser);
7169     }
7170
7171   /* If we get here, then it's not a cast, so it must be a
7172      unary-expression.  */
7173   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7174 }
7175
7176 /* Parse a binary expression of the general form:
7177
7178    pm-expression:
7179      cast-expression
7180      pm-expression .* cast-expression
7181      pm-expression ->* cast-expression
7182
7183    multiplicative-expression:
7184      pm-expression
7185      multiplicative-expression * pm-expression
7186      multiplicative-expression / pm-expression
7187      multiplicative-expression % pm-expression
7188
7189    additive-expression:
7190      multiplicative-expression
7191      additive-expression + multiplicative-expression
7192      additive-expression - multiplicative-expression
7193
7194    shift-expression:
7195      additive-expression
7196      shift-expression << additive-expression
7197      shift-expression >> additive-expression
7198
7199    relational-expression:
7200      shift-expression
7201      relational-expression < shift-expression
7202      relational-expression > shift-expression
7203      relational-expression <= shift-expression
7204      relational-expression >= shift-expression
7205
7206   GNU Extension:
7207
7208    relational-expression:
7209      relational-expression <? shift-expression
7210      relational-expression >? shift-expression
7211
7212    equality-expression:
7213      relational-expression
7214      equality-expression == relational-expression
7215      equality-expression != relational-expression
7216
7217    and-expression:
7218      equality-expression
7219      and-expression & equality-expression
7220
7221    exclusive-or-expression:
7222      and-expression
7223      exclusive-or-expression ^ and-expression
7224
7225    inclusive-or-expression:
7226      exclusive-or-expression
7227      inclusive-or-expression | exclusive-or-expression
7228
7229    logical-and-expression:
7230      inclusive-or-expression
7231      logical-and-expression && inclusive-or-expression
7232
7233    logical-or-expression:
7234      logical-and-expression
7235      logical-or-expression || logical-and-expression
7236
7237    All these are implemented with a single function like:
7238
7239    binary-expression:
7240      simple-cast-expression
7241      binary-expression <token> binary-expression
7242
7243    CAST_P is true if this expression is the target of a cast.
7244
7245    The binops_by_token map is used to get the tree codes for each <token> type.
7246    binary-expressions are associated according to a precedence table.  */
7247
7248 #define TOKEN_PRECEDENCE(token)                              \
7249 (((token->type == CPP_GREATER                                \
7250    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7251   && !parser->greater_than_is_operator_p)                    \
7252  ? PREC_NOT_OPERATOR                                         \
7253  : binops_by_token[token->type].prec)
7254
7255 static tree
7256 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7257                              bool no_toplevel_fold_p,
7258                              enum cp_parser_prec prec,
7259                              cp_id_kind * pidk)
7260 {
7261   cp_parser_expression_stack stack;
7262   cp_parser_expression_stack_entry *sp = &stack[0];
7263   tree lhs, rhs;
7264   cp_token *token;
7265   enum tree_code tree_type, lhs_type, rhs_type;
7266   enum cp_parser_prec new_prec, lookahead_prec;
7267   tree overload;
7268
7269   /* Parse the first expression.  */
7270   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7271   lhs_type = ERROR_MARK;
7272
7273   for (;;)
7274     {
7275       /* Get an operator token.  */
7276       token = cp_lexer_peek_token (parser->lexer);
7277
7278       if (warn_cxx0x_compat
7279           && token->type == CPP_RSHIFT
7280           && !parser->greater_than_is_operator_p)
7281         {
7282           if (warning_at (token->location, OPT_Wc__0x_compat, 
7283                           "%<>>%> operator is treated as"
7284                           " two right angle brackets in C++11"))
7285             inform (token->location,
7286                     "suggest parentheses around %<>>%> expression");
7287         }
7288
7289       new_prec = TOKEN_PRECEDENCE (token);
7290
7291       /* Popping an entry off the stack means we completed a subexpression:
7292          - either we found a token which is not an operator (`>' where it is not
7293            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7294            will happen repeatedly;
7295          - or, we found an operator which has lower priority.  This is the case
7296            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7297            parsing `3 * 4'.  */
7298       if (new_prec <= prec)
7299         {
7300           if (sp == stack)
7301             break;
7302           else
7303             goto pop;
7304         }
7305
7306      get_rhs:
7307       tree_type = binops_by_token[token->type].tree_type;
7308
7309       /* We used the operator token.  */
7310       cp_lexer_consume_token (parser->lexer);
7311
7312       /* For "false && x" or "true || x", x will never be executed;
7313          disable warnings while evaluating it.  */
7314       if (tree_type == TRUTH_ANDIF_EXPR)
7315         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7316       else if (tree_type == TRUTH_ORIF_EXPR)
7317         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7318
7319       /* Extract another operand.  It may be the RHS of this expression
7320          or the LHS of a new, higher priority expression.  */
7321       rhs = cp_parser_simple_cast_expression (parser);
7322       rhs_type = ERROR_MARK;
7323
7324       /* Get another operator token.  Look up its precedence to avoid
7325          building a useless (immediately popped) stack entry for common
7326          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7327       token = cp_lexer_peek_token (parser->lexer);
7328       lookahead_prec = TOKEN_PRECEDENCE (token);
7329       if (lookahead_prec > new_prec)
7330         {
7331           /* ... and prepare to parse the RHS of the new, higher priority
7332              expression.  Since precedence levels on the stack are
7333              monotonically increasing, we do not have to care about
7334              stack overflows.  */
7335           sp->prec = prec;
7336           sp->tree_type = tree_type;
7337           sp->lhs = lhs;
7338           sp->lhs_type = lhs_type;
7339           sp++;
7340           lhs = rhs;
7341           lhs_type = rhs_type;
7342           prec = new_prec;
7343           new_prec = lookahead_prec;
7344           goto get_rhs;
7345
7346          pop:
7347           lookahead_prec = new_prec;
7348           /* If the stack is not empty, we have parsed into LHS the right side
7349              (`4' in the example above) of an expression we had suspended.
7350              We can use the information on the stack to recover the LHS (`3')
7351              from the stack together with the tree code (`MULT_EXPR'), and
7352              the precedence of the higher level subexpression
7353              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7354              which will be used to actually build the additive expression.  */
7355           --sp;
7356           prec = sp->prec;
7357           tree_type = sp->tree_type;
7358           rhs = lhs;
7359           rhs_type = lhs_type;
7360           lhs = sp->lhs;
7361           lhs_type = sp->lhs_type;
7362         }
7363
7364       /* Undo the disabling of warnings done above.  */
7365       if (tree_type == TRUTH_ANDIF_EXPR)
7366         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7367       else if (tree_type == TRUTH_ORIF_EXPR)
7368         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7369
7370       overload = NULL;
7371       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7372          ERROR_MARK for everything that is not a binary expression.
7373          This makes warn_about_parentheses miss some warnings that
7374          involve unary operators.  For unary expressions we should
7375          pass the correct tree_code unless the unary expression was
7376          surrounded by parentheses.
7377       */
7378       if (no_toplevel_fold_p
7379           && lookahead_prec <= prec
7380           && sp == stack
7381           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7382         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7383       else
7384         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7385                                  &overload, tf_warning_or_error);
7386       lhs_type = tree_type;
7387
7388       /* If the binary operator required the use of an overloaded operator,
7389          then this expression cannot be an integral constant-expression.
7390          An overloaded operator can be used even if both operands are
7391          otherwise permissible in an integral constant-expression if at
7392          least one of the operands is of enumeration type.  */
7393
7394       if (overload
7395           && cp_parser_non_integral_constant_expression (parser,
7396                                                          NIC_OVERLOADED))
7397         return error_mark_node;
7398     }
7399
7400   return lhs;
7401 }
7402
7403
7404 /* Parse the `? expression : assignment-expression' part of a
7405    conditional-expression.  The LOGICAL_OR_EXPR is the
7406    logical-or-expression that started the conditional-expression.
7407    Returns a representation of the entire conditional-expression.
7408
7409    This routine is used by cp_parser_assignment_expression.
7410
7411      ? expression : assignment-expression
7412
7413    GNU Extensions:
7414
7415      ? : assignment-expression */
7416
7417 static tree
7418 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7419 {
7420   tree expr;
7421   tree assignment_expr;
7422   struct cp_token *token;
7423
7424   /* Consume the `?' token.  */
7425   cp_lexer_consume_token (parser->lexer);
7426   token = cp_lexer_peek_token (parser->lexer);
7427   if (cp_parser_allow_gnu_extensions_p (parser)
7428       && token->type == CPP_COLON)
7429     {
7430       pedwarn (token->location, OPT_pedantic, 
7431                "ISO C++ does not allow ?: with omitted middle operand");
7432       /* Implicit true clause.  */
7433       expr = NULL_TREE;
7434       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7435       warn_for_omitted_condop (token->location, logical_or_expr);
7436     }
7437   else
7438     {
7439       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7440       parser->colon_corrects_to_scope_p = false;
7441       /* Parse the expression.  */
7442       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7443       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7444       c_inhibit_evaluation_warnings +=
7445         ((logical_or_expr == truthvalue_true_node)
7446          - (logical_or_expr == truthvalue_false_node));
7447       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7448     }
7449
7450   /* The next token should be a `:'.  */
7451   cp_parser_require (parser, CPP_COLON, RT_COLON);
7452   /* Parse the assignment-expression.  */
7453   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7454   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7455
7456   /* Build the conditional-expression.  */
7457   return build_x_conditional_expr (logical_or_expr,
7458                                    expr,
7459                                    assignment_expr,
7460                                    tf_warning_or_error);
7461 }
7462
7463 /* Parse an assignment-expression.
7464
7465    assignment-expression:
7466      conditional-expression
7467      logical-or-expression assignment-operator assignment_expression
7468      throw-expression
7469
7470    CAST_P is true if this expression is the target of a cast.
7471
7472    Returns a representation for the expression.  */
7473
7474 static tree
7475 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7476                                  cp_id_kind * pidk)
7477 {
7478   tree expr;
7479
7480   /* If the next token is the `throw' keyword, then we're looking at
7481      a throw-expression.  */
7482   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7483     expr = cp_parser_throw_expression (parser);
7484   /* Otherwise, it must be that we are looking at a
7485      logical-or-expression.  */
7486   else
7487     {
7488       /* Parse the binary expressions (logical-or-expression).  */
7489       expr = cp_parser_binary_expression (parser, cast_p, false,
7490                                           PREC_NOT_OPERATOR, pidk);
7491       /* If the next token is a `?' then we're actually looking at a
7492          conditional-expression.  */
7493       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7494         return cp_parser_question_colon_clause (parser, expr);
7495       else
7496         {
7497           enum tree_code assignment_operator;
7498
7499           /* If it's an assignment-operator, we're using the second
7500              production.  */
7501           assignment_operator
7502             = cp_parser_assignment_operator_opt (parser);
7503           if (assignment_operator != ERROR_MARK)
7504             {
7505               bool non_constant_p;
7506
7507               /* Parse the right-hand side of the assignment.  */
7508               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7509
7510               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7511                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7512
7513               /* An assignment may not appear in a
7514                  constant-expression.  */
7515               if (cp_parser_non_integral_constant_expression (parser,
7516                                                               NIC_ASSIGNMENT))
7517                 return error_mark_node;
7518               /* Build the assignment expression.  */
7519               expr = build_x_modify_expr (expr,
7520                                           assignment_operator,
7521                                           rhs,
7522                                           tf_warning_or_error);
7523             }
7524         }
7525     }
7526
7527   return expr;
7528 }
7529
7530 /* Parse an (optional) assignment-operator.
7531
7532    assignment-operator: one of
7533      = *= /= %= += -= >>= <<= &= ^= |=
7534
7535    GNU Extension:
7536
7537    assignment-operator: one of
7538      <?= >?=
7539
7540    If the next token is an assignment operator, the corresponding tree
7541    code is returned, and the token is consumed.  For example, for
7542    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7543    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7544    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7545    operator, ERROR_MARK is returned.  */
7546
7547 static enum tree_code
7548 cp_parser_assignment_operator_opt (cp_parser* parser)
7549 {
7550   enum tree_code op;
7551   cp_token *token;
7552
7553   /* Peek at the next token.  */
7554   token = cp_lexer_peek_token (parser->lexer);
7555
7556   switch (token->type)
7557     {
7558     case CPP_EQ:
7559       op = NOP_EXPR;
7560       break;
7561
7562     case CPP_MULT_EQ:
7563       op = MULT_EXPR;
7564       break;
7565
7566     case CPP_DIV_EQ:
7567       op = TRUNC_DIV_EXPR;
7568       break;
7569
7570     case CPP_MOD_EQ:
7571       op = TRUNC_MOD_EXPR;
7572       break;
7573
7574     case CPP_PLUS_EQ:
7575       op = PLUS_EXPR;
7576       break;
7577
7578     case CPP_MINUS_EQ:
7579       op = MINUS_EXPR;
7580       break;
7581
7582     case CPP_RSHIFT_EQ:
7583       op = RSHIFT_EXPR;
7584       break;
7585
7586     case CPP_LSHIFT_EQ:
7587       op = LSHIFT_EXPR;
7588       break;
7589
7590     case CPP_AND_EQ:
7591       op = BIT_AND_EXPR;
7592       break;
7593
7594     case CPP_XOR_EQ:
7595       op = BIT_XOR_EXPR;
7596       break;
7597
7598     case CPP_OR_EQ:
7599       op = BIT_IOR_EXPR;
7600       break;
7601
7602     default:
7603       /* Nothing else is an assignment operator.  */
7604       op = ERROR_MARK;
7605     }
7606
7607   /* If it was an assignment operator, consume it.  */
7608   if (op != ERROR_MARK)
7609     cp_lexer_consume_token (parser->lexer);
7610
7611   return op;
7612 }
7613
7614 /* Parse an expression.
7615
7616    expression:
7617      assignment-expression
7618      expression , assignment-expression
7619
7620    CAST_P is true if this expression is the target of a cast.
7621
7622    Returns a representation of the expression.  */
7623
7624 static tree
7625 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7626 {
7627   tree expression = NULL_TREE;
7628
7629   while (true)
7630     {
7631       tree assignment_expression;
7632
7633       /* Parse the next assignment-expression.  */
7634       assignment_expression
7635         = cp_parser_assignment_expression (parser, cast_p, pidk);
7636       /* If this is the first assignment-expression, we can just
7637          save it away.  */
7638       if (!expression)
7639         expression = assignment_expression;
7640       else
7641         expression = build_x_compound_expr (expression,
7642                                             assignment_expression,
7643                                             tf_warning_or_error);
7644       /* If the next token is not a comma, then we are done with the
7645          expression.  */
7646       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7647         break;
7648       /* Consume the `,'.  */
7649       cp_lexer_consume_token (parser->lexer);
7650       /* A comma operator cannot appear in a constant-expression.  */
7651       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7652         expression = error_mark_node;
7653     }
7654
7655   return expression;
7656 }
7657
7658 /* Parse a constant-expression.
7659
7660    constant-expression:
7661      conditional-expression
7662
7663   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7664   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7665   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7666   is false, NON_CONSTANT_P should be NULL.  */
7667
7668 static tree
7669 cp_parser_constant_expression (cp_parser* parser,
7670                                bool allow_non_constant_p,
7671                                bool *non_constant_p)
7672 {
7673   bool saved_integral_constant_expression_p;
7674   bool saved_allow_non_integral_constant_expression_p;
7675   bool saved_non_integral_constant_expression_p;
7676   tree expression;
7677
7678   /* It might seem that we could simply parse the
7679      conditional-expression, and then check to see if it were
7680      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7681      one that the compiler can figure out is constant, possibly after
7682      doing some simplifications or optimizations.  The standard has a
7683      precise definition of constant-expression, and we must honor
7684      that, even though it is somewhat more restrictive.
7685
7686      For example:
7687
7688        int i[(2, 3)];
7689
7690      is not a legal declaration, because `(2, 3)' is not a
7691      constant-expression.  The `,' operator is forbidden in a
7692      constant-expression.  However, GCC's constant-folding machinery
7693      will fold this operation to an INTEGER_CST for `3'.  */
7694
7695   /* Save the old settings.  */
7696   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7697   saved_allow_non_integral_constant_expression_p
7698     = parser->allow_non_integral_constant_expression_p;
7699   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7700   /* We are now parsing a constant-expression.  */
7701   parser->integral_constant_expression_p = true;
7702   parser->allow_non_integral_constant_expression_p
7703     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7704   parser->non_integral_constant_expression_p = false;
7705   /* Although the grammar says "conditional-expression", we parse an
7706      "assignment-expression", which also permits "throw-expression"
7707      and the use of assignment operators.  In the case that
7708      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7709      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7710      actually essential that we look for an assignment-expression.
7711      For example, cp_parser_initializer_clauses uses this function to
7712      determine whether a particular assignment-expression is in fact
7713      constant.  */
7714   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7715   /* Restore the old settings.  */
7716   parser->integral_constant_expression_p
7717     = saved_integral_constant_expression_p;
7718   parser->allow_non_integral_constant_expression_p
7719     = saved_allow_non_integral_constant_expression_p;
7720   if (cxx_dialect >= cxx0x)
7721     {
7722       /* Require an rvalue constant expression here; that's what our
7723          callers expect.  Reference constant expressions are handled
7724          separately in e.g. cp_parser_template_argument.  */
7725       bool is_const = potential_rvalue_constant_expression (expression);
7726       parser->non_integral_constant_expression_p = !is_const;
7727       if (!is_const && !allow_non_constant_p)
7728         require_potential_rvalue_constant_expression (expression);
7729     }
7730   if (allow_non_constant_p)
7731     *non_constant_p = parser->non_integral_constant_expression_p;
7732   parser->non_integral_constant_expression_p
7733     = saved_non_integral_constant_expression_p;
7734
7735   return expression;
7736 }
7737
7738 /* Parse __builtin_offsetof.
7739
7740    offsetof-expression:
7741      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7742
7743    offsetof-member-designator:
7744      id-expression
7745      | offsetof-member-designator "." id-expression
7746      | offsetof-member-designator "[" expression "]"
7747      | offsetof-member-designator "->" id-expression  */
7748
7749 static tree
7750 cp_parser_builtin_offsetof (cp_parser *parser)
7751 {
7752   int save_ice_p, save_non_ice_p;
7753   tree type, expr;
7754   cp_id_kind dummy;
7755   cp_token *token;
7756
7757   /* We're about to accept non-integral-constant things, but will
7758      definitely yield an integral constant expression.  Save and
7759      restore these values around our local parsing.  */
7760   save_ice_p = parser->integral_constant_expression_p;
7761   save_non_ice_p = parser->non_integral_constant_expression_p;
7762
7763   /* Consume the "__builtin_offsetof" token.  */
7764   cp_lexer_consume_token (parser->lexer);
7765   /* Consume the opening `('.  */
7766   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7767   /* Parse the type-id.  */
7768   type = cp_parser_type_id (parser);
7769   /* Look for the `,'.  */
7770   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7771   token = cp_lexer_peek_token (parser->lexer);
7772
7773   /* Build the (type *)null that begins the traditional offsetof macro.  */
7774   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7775                             tf_warning_or_error);
7776
7777   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7778   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7779                                                  true, &dummy, token->location);
7780   while (true)
7781     {
7782       token = cp_lexer_peek_token (parser->lexer);
7783       switch (token->type)
7784         {
7785         case CPP_OPEN_SQUARE:
7786           /* offsetof-member-designator "[" expression "]" */
7787           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7788           break;
7789
7790         case CPP_DEREF:
7791           /* offsetof-member-designator "->" identifier */
7792           expr = grok_array_decl (expr, integer_zero_node);
7793           /* FALLTHRU */
7794
7795         case CPP_DOT:
7796           /* offsetof-member-designator "." identifier */
7797           cp_lexer_consume_token (parser->lexer);
7798           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7799                                                          expr, true, &dummy,
7800                                                          token->location);
7801           break;
7802
7803         case CPP_CLOSE_PAREN:
7804           /* Consume the ")" token.  */
7805           cp_lexer_consume_token (parser->lexer);
7806           goto success;
7807
7808         default:
7809           /* Error.  We know the following require will fail, but
7810              that gives the proper error message.  */
7811           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7812           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7813           expr = error_mark_node;
7814           goto failure;
7815         }
7816     }
7817
7818  success:
7819   /* If we're processing a template, we can't finish the semantics yet.
7820      Otherwise we can fold the entire expression now.  */
7821   if (processing_template_decl)
7822     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7823   else
7824     expr = finish_offsetof (expr);
7825
7826  failure:
7827   parser->integral_constant_expression_p = save_ice_p;
7828   parser->non_integral_constant_expression_p = save_non_ice_p;
7829
7830   return expr;
7831 }
7832
7833 /* Parse a trait expression.
7834
7835    Returns a representation of the expression, the underlying type
7836    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7837
7838 static tree
7839 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7840 {
7841   cp_trait_kind kind;
7842   tree type1, type2 = NULL_TREE;
7843   bool binary = false;
7844   cp_decl_specifier_seq decl_specs;
7845
7846   switch (keyword)
7847     {
7848     case RID_HAS_NOTHROW_ASSIGN:
7849       kind = CPTK_HAS_NOTHROW_ASSIGN;
7850       break;
7851     case RID_HAS_NOTHROW_CONSTRUCTOR:
7852       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7853       break;
7854     case RID_HAS_NOTHROW_COPY:
7855       kind = CPTK_HAS_NOTHROW_COPY;
7856       break;
7857     case RID_HAS_TRIVIAL_ASSIGN:
7858       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7859       break;
7860     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7861       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7862       break;
7863     case RID_HAS_TRIVIAL_COPY:
7864       kind = CPTK_HAS_TRIVIAL_COPY;
7865       break;
7866     case RID_HAS_TRIVIAL_DESTRUCTOR:
7867       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7868       break;
7869     case RID_HAS_VIRTUAL_DESTRUCTOR:
7870       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7871       break;
7872     case RID_IS_ABSTRACT:
7873       kind = CPTK_IS_ABSTRACT;
7874       break;
7875     case RID_IS_BASE_OF:
7876       kind = CPTK_IS_BASE_OF;
7877       binary = true;
7878       break;
7879     case RID_IS_CLASS:
7880       kind = CPTK_IS_CLASS;
7881       break;
7882     case RID_IS_CONVERTIBLE_TO:
7883       kind = CPTK_IS_CONVERTIBLE_TO;
7884       binary = true;
7885       break;
7886     case RID_IS_EMPTY:
7887       kind = CPTK_IS_EMPTY;
7888       break;
7889     case RID_IS_ENUM:
7890       kind = CPTK_IS_ENUM;
7891       break;
7892     case RID_IS_FINAL:
7893       kind = CPTK_IS_FINAL;
7894       break;
7895     case RID_IS_LITERAL_TYPE:
7896       kind = CPTK_IS_LITERAL_TYPE;
7897       break;
7898     case RID_IS_POD:
7899       kind = CPTK_IS_POD;
7900       break;
7901     case RID_IS_POLYMORPHIC:
7902       kind = CPTK_IS_POLYMORPHIC;
7903       break;
7904     case RID_IS_STD_LAYOUT:
7905       kind = CPTK_IS_STD_LAYOUT;
7906       break;
7907     case RID_IS_TRIVIAL:
7908       kind = CPTK_IS_TRIVIAL;
7909       break;
7910     case RID_IS_UNION:
7911       kind = CPTK_IS_UNION;
7912       break;
7913     case RID_UNDERLYING_TYPE:
7914       kind = CPTK_UNDERLYING_TYPE;
7915       break;
7916     case RID_BASES:
7917       kind = CPTK_BASES;
7918       break;
7919     case RID_DIRECT_BASES:
7920       kind = CPTK_DIRECT_BASES;
7921       break;
7922     default:
7923       gcc_unreachable ();
7924     }
7925
7926   /* Consume the token.  */
7927   cp_lexer_consume_token (parser->lexer);
7928
7929   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7930
7931   type1 = cp_parser_type_id (parser);
7932
7933   if (type1 == error_mark_node)
7934     return error_mark_node;
7935
7936   /* Build a trivial decl-specifier-seq.  */
7937   clear_decl_specs (&decl_specs);
7938   decl_specs.type = type1;
7939
7940   /* Call grokdeclarator to figure out what type this is.  */
7941   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7942                           /*initialized=*/0, /*attrlist=*/NULL);
7943
7944   if (binary)
7945     {
7946       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7947  
7948       type2 = cp_parser_type_id (parser);
7949
7950       if (type2 == error_mark_node)
7951         return error_mark_node;
7952
7953       /* Build a trivial decl-specifier-seq.  */
7954       clear_decl_specs (&decl_specs);
7955       decl_specs.type = type2;
7956
7957       /* Call grokdeclarator to figure out what type this is.  */
7958       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7959                               /*initialized=*/0, /*attrlist=*/NULL);
7960     }
7961
7962   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7963
7964   /* Complete the trait expression, which may mean either processing
7965      the trait expr now or saving it for template instantiation.  */
7966   switch(kind)
7967     {
7968     case CPTK_UNDERLYING_TYPE:
7969       return finish_underlying_type (type1);
7970     case CPTK_BASES:
7971       return finish_bases (type1, false);
7972     case CPTK_DIRECT_BASES:
7973       return finish_bases (type1, true);
7974     default:
7975       return finish_trait_expr (kind, type1, type2);
7976     }
7977 }
7978
7979 /* Lambdas that appear in variable initializer or default argument scope
7980    get that in their mangling, so we need to record it.  We might as well
7981    use the count for function and namespace scopes as well.  */
7982 static GTY(()) tree lambda_scope;
7983 static GTY(()) int lambda_count;
7984 typedef struct GTY(()) tree_int
7985 {
7986   tree t;
7987   int i;
7988 } tree_int;
7989 DEF_VEC_O(tree_int);
7990 DEF_VEC_ALLOC_O(tree_int,gc);
7991 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7992
7993 static void
7994 start_lambda_scope (tree decl)
7995 {
7996   tree_int ti;
7997   gcc_assert (decl);
7998   /* Once we're inside a function, we ignore other scopes and just push
7999      the function again so that popping works properly.  */
8000   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8001     decl = current_function_decl;
8002   ti.t = lambda_scope;
8003   ti.i = lambda_count;
8004   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
8005   if (lambda_scope != decl)
8006     {
8007       /* Don't reset the count if we're still in the same function.  */
8008       lambda_scope = decl;
8009       lambda_count = 0;
8010     }
8011 }
8012
8013 static void
8014 record_lambda_scope (tree lambda)
8015 {
8016   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8017   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8018 }
8019
8020 static void
8021 finish_lambda_scope (void)
8022 {
8023   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8024   if (lambda_scope != p->t)
8025     {
8026       lambda_scope = p->t;
8027       lambda_count = p->i;
8028     }
8029   VEC_pop (tree_int, lambda_scope_stack);
8030 }
8031
8032 /* Parse a lambda expression.
8033
8034    lambda-expression:
8035      lambda-introducer lambda-declarator [opt] compound-statement
8036
8037    Returns a representation of the expression.  */
8038
8039 static tree
8040 cp_parser_lambda_expression (cp_parser* parser)
8041 {
8042   tree lambda_expr = build_lambda_expr ();
8043   tree type;
8044   bool ok;
8045
8046   LAMBDA_EXPR_LOCATION (lambda_expr)
8047     = cp_lexer_peek_token (parser->lexer)->location;
8048
8049   if (cp_unevaluated_operand)
8050     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8051               "lambda-expression in unevaluated context");
8052
8053   /* We may be in the middle of deferred access check.  Disable
8054      it now.  */
8055   push_deferring_access_checks (dk_no_deferred);
8056
8057   cp_parser_lambda_introducer (parser, lambda_expr);
8058
8059   type = begin_lambda_type (lambda_expr);
8060   if (type == error_mark_node)
8061     return error_mark_node;
8062
8063   record_lambda_scope (lambda_expr);
8064
8065   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8066   determine_visibility (TYPE_NAME (type));
8067
8068   /* Now that we've started the type, add the capture fields for any
8069      explicit captures.  */
8070   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8071
8072   {
8073     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8074     unsigned int saved_num_template_parameter_lists
8075         = parser->num_template_parameter_lists;
8076     unsigned char in_statement = parser->in_statement;
8077     bool in_switch_statement_p = parser->in_switch_statement_p;
8078
8079     parser->num_template_parameter_lists = 0;
8080     parser->in_statement = 0;
8081     parser->in_switch_statement_p = false;
8082
8083     /* By virtue of defining a local class, a lambda expression has access to
8084        the private variables of enclosing classes.  */
8085
8086     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8087
8088     if (ok)
8089       cp_parser_lambda_body (parser, lambda_expr);
8090     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8091       cp_parser_skip_to_end_of_block_or_statement (parser);
8092
8093     /* The capture list was built up in reverse order; fix that now.  */
8094     {
8095       tree newlist = NULL_TREE;
8096       tree elt, next;
8097
8098       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8099            elt; elt = next)
8100         {
8101           next = TREE_CHAIN (elt);
8102           TREE_CHAIN (elt) = newlist;
8103           newlist = elt;
8104         }
8105       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8106     }
8107
8108     if (ok)
8109       maybe_add_lambda_conv_op (type);
8110
8111     type = finish_struct (type, /*attributes=*/NULL_TREE);
8112
8113     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8114     parser->in_statement = in_statement;
8115     parser->in_switch_statement_p = in_switch_statement_p;
8116   }
8117
8118   pop_deferring_access_checks ();
8119
8120   /* This field is only used during parsing of the lambda.  */
8121   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8122
8123   /* This lambda shouldn't have any proxies left at this point.  */
8124   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8125   /* And now that we're done, push proxies for an enclosing lambda.  */
8126   insert_pending_capture_proxies ();
8127
8128   if (ok)
8129     return build_lambda_object (lambda_expr);
8130   else
8131     return error_mark_node;
8132 }
8133
8134 /* Parse the beginning of a lambda expression.
8135
8136    lambda-introducer:
8137      [ lambda-capture [opt] ]
8138
8139    LAMBDA_EXPR is the current representation of the lambda expression.  */
8140
8141 static void
8142 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8143 {
8144   /* Need commas after the first capture.  */
8145   bool first = true;
8146
8147   /* Eat the leading `['.  */
8148   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8149
8150   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8151   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8152       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8153     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8154   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8155     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8156
8157   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8158     {
8159       cp_lexer_consume_token (parser->lexer);
8160       first = false;
8161     }
8162
8163   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8164     {
8165       cp_token* capture_token;
8166       tree capture_id;
8167       tree capture_init_expr;
8168       cp_id_kind idk = CP_ID_KIND_NONE;
8169       bool explicit_init_p = false;
8170
8171       enum capture_kind_type
8172       {
8173         BY_COPY,
8174         BY_REFERENCE
8175       };
8176       enum capture_kind_type capture_kind = BY_COPY;
8177
8178       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8179         {
8180           error ("expected end of capture-list");
8181           return;
8182         }
8183
8184       if (first)
8185         first = false;
8186       else
8187         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8188
8189       /* Possibly capture `this'.  */
8190       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8191         {
8192           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8193           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8194             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8195                      "with by-copy capture default");
8196           cp_lexer_consume_token (parser->lexer);
8197           add_capture (lambda_expr,
8198                        /*id=*/this_identifier,
8199                        /*initializer=*/finish_this_expr(),
8200                        /*by_reference_p=*/false,
8201                        explicit_init_p);
8202           continue;
8203         }
8204
8205       /* Remember whether we want to capture as a reference or not.  */
8206       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8207         {
8208           capture_kind = BY_REFERENCE;
8209           cp_lexer_consume_token (parser->lexer);
8210         }
8211
8212       /* Get the identifier.  */
8213       capture_token = cp_lexer_peek_token (parser->lexer);
8214       capture_id = cp_parser_identifier (parser);
8215
8216       if (capture_id == error_mark_node)
8217         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8218            delimiters, but I modified this to stop on unnested ']' as well.  It
8219            was already changed to stop on unnested '}', so the
8220            "closing_parenthesis" name is no more misleading with my change.  */
8221         {
8222           cp_parser_skip_to_closing_parenthesis (parser,
8223                                                  /*recovering=*/true,
8224                                                  /*or_comma=*/true,
8225                                                  /*consume_paren=*/true);
8226           break;
8227         }
8228
8229       /* Find the initializer for this capture.  */
8230       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8231         {
8232           /* An explicit expression exists.  */
8233           cp_lexer_consume_token (parser->lexer);
8234           pedwarn (input_location, OPT_pedantic,
8235                    "ISO C++ does not allow initializers "
8236                    "in lambda expression capture lists");
8237           capture_init_expr = cp_parser_assignment_expression (parser,
8238                                                                /*cast_p=*/true,
8239                                                                &idk);
8240           explicit_init_p = true;
8241         }
8242       else
8243         {
8244           const char* error_msg;
8245
8246           /* Turn the identifier into an id-expression.  */
8247           capture_init_expr
8248             = cp_parser_lookup_name
8249                 (parser,
8250                  capture_id,
8251                  none_type,
8252                  /*is_template=*/false,
8253                  /*is_namespace=*/false,
8254                  /*check_dependency=*/true,
8255                  /*ambiguous_decls=*/NULL,
8256                  capture_token->location);
8257
8258           if (capture_init_expr == error_mark_node)
8259             {
8260               unqualified_name_lookup_error (capture_id);
8261               continue;
8262             }
8263           else if (DECL_P (capture_init_expr)
8264                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8265                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8266             {
8267               error_at (capture_token->location,
8268                         "capture of non-variable %qD ",
8269                         capture_init_expr);
8270               inform (0, "%q+#D declared here", capture_init_expr);
8271               continue;
8272             }
8273           if (TREE_CODE (capture_init_expr) == VAR_DECL
8274               && decl_storage_duration (capture_init_expr) != dk_auto)
8275             {
8276               pedwarn (capture_token->location, 0, "capture of variable "
8277                        "%qD with non-automatic storage duration",
8278                        capture_init_expr);
8279               inform (0, "%q+#D declared here", capture_init_expr);
8280               continue;
8281             }
8282
8283           capture_init_expr
8284             = finish_id_expression
8285                 (capture_id,
8286                  capture_init_expr,
8287                  parser->scope,
8288                  &idk,
8289                  /*integral_constant_expression_p=*/false,
8290                  /*allow_non_integral_constant_expression_p=*/false,
8291                  /*non_integral_constant_expression_p=*/NULL,
8292                  /*template_p=*/false,
8293                  /*done=*/true,
8294                  /*address_p=*/false,
8295                  /*template_arg_p=*/false,
8296                  &error_msg,
8297                  capture_token->location);
8298         }
8299
8300       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8301           && !explicit_init_p)
8302         {
8303           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8304               && capture_kind == BY_COPY)
8305             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8306                      "of %qD redundant with by-copy capture default",
8307                      capture_id);
8308           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8309               && capture_kind == BY_REFERENCE)
8310             pedwarn (capture_token->location, 0, "explicit by-reference "
8311                      "capture of %qD redundant with by-reference capture "
8312                      "default", capture_id);
8313         }
8314
8315       add_capture (lambda_expr,
8316                    capture_id,
8317                    capture_init_expr,
8318                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8319                    explicit_init_p);
8320     }
8321
8322   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8323 }
8324
8325 /* Parse the (optional) middle of a lambda expression.
8326
8327    lambda-declarator:
8328      ( parameter-declaration-clause [opt] )
8329        attribute-specifier [opt]
8330        mutable [opt]
8331        exception-specification [opt]
8332        lambda-return-type-clause [opt]
8333
8334    LAMBDA_EXPR is the current representation of the lambda expression.  */
8335
8336 static bool
8337 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8338 {
8339   /* 5.1.1.4 of the standard says:
8340        If a lambda-expression does not include a lambda-declarator, it is as if
8341        the lambda-declarator were ().
8342      This means an empty parameter list, no attributes, and no exception
8343      specification.  */
8344   tree param_list = void_list_node;
8345   tree attributes = NULL_TREE;
8346   tree exception_spec = NULL_TREE;
8347   tree t;
8348
8349   /* The lambda-declarator is optional, but must begin with an opening
8350      parenthesis if present.  */
8351   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8352     {
8353       cp_lexer_consume_token (parser->lexer);
8354
8355       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8356
8357       /* Parse parameters.  */
8358       param_list = cp_parser_parameter_declaration_clause (parser);
8359
8360       /* Default arguments shall not be specified in the
8361          parameter-declaration-clause of a lambda-declarator.  */
8362       for (t = param_list; t; t = TREE_CHAIN (t))
8363         if (TREE_PURPOSE (t))
8364           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8365                    "default argument specified for lambda parameter");
8366
8367       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8368
8369       attributes = cp_parser_attributes_opt (parser);
8370
8371       /* Parse optional `mutable' keyword.  */
8372       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8373         {
8374           cp_lexer_consume_token (parser->lexer);
8375           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8376         }
8377
8378       /* Parse optional exception specification.  */
8379       exception_spec = cp_parser_exception_specification_opt (parser);
8380
8381       /* Parse optional trailing return type.  */
8382       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8383         {
8384           cp_lexer_consume_token (parser->lexer);
8385           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8386         }
8387
8388       /* The function parameters must be in scope all the way until after the
8389          trailing-return-type in case of decltype.  */
8390       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8391         pop_binding (DECL_NAME (t), t);
8392
8393       leave_scope ();
8394     }
8395
8396   /* Create the function call operator.
8397
8398      Messing with declarators like this is no uglier than building up the
8399      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8400      other code.  */
8401   {
8402     cp_decl_specifier_seq return_type_specs;
8403     cp_declarator* declarator;
8404     tree fco;
8405     int quals;
8406     void *p;
8407
8408     clear_decl_specs (&return_type_specs);
8409     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8410       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8411     else
8412       /* Maybe we will deduce the return type later, but we can use void
8413          as a placeholder return type anyways.  */
8414       return_type_specs.type = void_type_node;
8415
8416     p = obstack_alloc (&declarator_obstack, 0);
8417
8418     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8419                                      sfk_none);
8420
8421     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8422              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8423     declarator = make_call_declarator (declarator, param_list, quals,
8424                                        VIRT_SPEC_UNSPECIFIED,
8425                                        exception_spec,
8426                                        /*late_return_type=*/NULL_TREE);
8427     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8428
8429     fco = grokmethod (&return_type_specs,
8430                       declarator,
8431                       attributes);
8432     if (fco != error_mark_node)
8433       {
8434         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8435         DECL_ARTIFICIAL (fco) = 1;
8436         /* Give the object parameter a different name.  */
8437         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8438       }
8439
8440     finish_member_declaration (fco);
8441
8442     obstack_free (&declarator_obstack, p);
8443
8444     return (fco != error_mark_node);
8445   }
8446 }
8447
8448 /* Parse the body of a lambda expression, which is simply
8449
8450    compound-statement
8451
8452    but which requires special handling.
8453    LAMBDA_EXPR is the current representation of the lambda expression.  */
8454
8455 static void
8456 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8457 {
8458   bool nested = (current_function_decl != NULL_TREE);
8459   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8460   if (nested)
8461     push_function_context ();
8462   else
8463     /* Still increment function_depth so that we don't GC in the
8464        middle of an expression.  */
8465     ++function_depth;
8466   /* Clear this in case we're in the middle of a default argument.  */
8467   parser->local_variables_forbidden_p = false;
8468
8469   /* Finish the function call operator
8470      - class_specifier
8471      + late_parsing_for_member
8472      + function_definition_after_declarator
8473      + ctor_initializer_opt_and_function_body  */
8474   {
8475     tree fco = lambda_function (lambda_expr);
8476     tree body;
8477     bool done = false;
8478     tree compound_stmt;
8479     tree cap;
8480
8481     /* Let the front end know that we are going to be defining this
8482        function.  */
8483     start_preparsed_function (fco,
8484                               NULL_TREE,
8485                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8486
8487     start_lambda_scope (fco);
8488     body = begin_function_body ();
8489
8490     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8491       goto out;
8492
8493     /* Push the proxies for any explicit captures.  */
8494     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8495          cap = TREE_CHAIN (cap))
8496       build_capture_proxy (TREE_PURPOSE (cap));
8497
8498     compound_stmt = begin_compound_stmt (0);
8499
8500     /* 5.1.1.4 of the standard says:
8501          If a lambda-expression does not include a trailing-return-type, it
8502          is as if the trailing-return-type denotes the following type:
8503           * if the compound-statement is of the form
8504                { return attribute-specifier [opt] expression ; }
8505              the type of the returned expression after lvalue-to-rvalue
8506              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8507              (_conv.array_ 4.2), and function-to-pointer conversion
8508              (_conv.func_ 4.3);
8509           * otherwise, void.  */
8510
8511     /* In a lambda that has neither a lambda-return-type-clause
8512        nor a deducible form, errors should be reported for return statements
8513        in the body.  Since we used void as the placeholder return type, parsing
8514        the body as usual will give such desired behavior.  */
8515     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8516         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8517         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8518       {
8519         tree expr = NULL_TREE;
8520         cp_id_kind idk = CP_ID_KIND_NONE;
8521
8522         /* Parse tentatively in case there's more after the initial return
8523            statement.  */
8524         cp_parser_parse_tentatively (parser);
8525
8526         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8527
8528         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8529
8530         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8531         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8532
8533         if (cp_parser_parse_definitely (parser))
8534           {
8535             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8536
8537             /* Will get error here if type not deduced yet.  */
8538             finish_return_stmt (expr);
8539
8540             done = true;
8541           }
8542       }
8543
8544     if (!done)
8545       {
8546         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8547           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8548         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8549           cp_parser_label_declaration (parser);
8550         cp_parser_statement_seq_opt (parser, NULL_TREE);
8551         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8552         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8553       }
8554
8555     finish_compound_stmt (compound_stmt);
8556
8557   out:
8558     finish_function_body (body);
8559     finish_lambda_scope ();
8560
8561     /* Finish the function and generate code for it if necessary.  */
8562     expand_or_defer_fn (finish_function (/*inline*/2));
8563   }
8564
8565   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8566   if (nested)
8567     pop_function_context();
8568   else
8569     --function_depth;
8570 }
8571
8572 /* Statements [gram.stmt.stmt]  */
8573
8574 /* Parse a statement.
8575
8576    statement:
8577      labeled-statement
8578      expression-statement
8579      compound-statement
8580      selection-statement
8581      iteration-statement
8582      jump-statement
8583      declaration-statement
8584      try-block
8585
8586   TM Extension:
8587
8588    statement:
8589      atomic-statement
8590
8591   IN_COMPOUND is true when the statement is nested inside a
8592   cp_parser_compound_statement; this matters for certain pragmas.
8593
8594   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8595   is a (possibly labeled) if statement which is not enclosed in braces
8596   and has an else clause.  This is used to implement -Wparentheses.  */
8597
8598 static void
8599 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8600                      bool in_compound, bool *if_p)
8601 {
8602   tree statement;
8603   cp_token *token;
8604   location_t statement_location;
8605
8606  restart:
8607   if (if_p != NULL)
8608     *if_p = false;
8609   /* There is no statement yet.  */
8610   statement = NULL_TREE;
8611   /* Peek at the next token.  */
8612   token = cp_lexer_peek_token (parser->lexer);
8613   /* Remember the location of the first token in the statement.  */
8614   statement_location = token->location;
8615   /* If this is a keyword, then that will often determine what kind of
8616      statement we have.  */
8617   if (token->type == CPP_KEYWORD)
8618     {
8619       enum rid keyword = token->keyword;
8620
8621       switch (keyword)
8622         {
8623         case RID_CASE:
8624         case RID_DEFAULT:
8625           /* Looks like a labeled-statement with a case label.
8626              Parse the label, and then use tail recursion to parse
8627              the statement.  */
8628           cp_parser_label_for_labeled_statement (parser);
8629           goto restart;
8630
8631         case RID_IF:
8632         case RID_SWITCH:
8633           statement = cp_parser_selection_statement (parser, if_p);
8634           break;
8635
8636         case RID_WHILE:
8637         case RID_DO:
8638         case RID_FOR:
8639           statement = cp_parser_iteration_statement (parser);
8640           break;
8641
8642         case RID_BREAK:
8643         case RID_CONTINUE:
8644         case RID_RETURN:
8645         case RID_GOTO:
8646           statement = cp_parser_jump_statement (parser);
8647           break;
8648
8649           /* Objective-C++ exception-handling constructs.  */
8650         case RID_AT_TRY:
8651         case RID_AT_CATCH:
8652         case RID_AT_FINALLY:
8653         case RID_AT_SYNCHRONIZED:
8654         case RID_AT_THROW:
8655           statement = cp_parser_objc_statement (parser);
8656           break;
8657
8658         case RID_TRY:
8659           statement = cp_parser_try_block (parser);
8660           break;
8661
8662         case RID_NAMESPACE:
8663           /* This must be a namespace alias definition.  */
8664           cp_parser_declaration_statement (parser);
8665           return;
8666           
8667         case RID_TRANSACTION_ATOMIC:
8668         case RID_TRANSACTION_RELAXED:
8669           statement = cp_parser_transaction (parser, keyword);
8670           break;
8671         case RID_TRANSACTION_CANCEL:
8672           statement = cp_parser_transaction_cancel (parser);
8673           break;
8674
8675         default:
8676           /* It might be a keyword like `int' that can start a
8677              declaration-statement.  */
8678           break;
8679         }
8680     }
8681   else if (token->type == CPP_NAME)
8682     {
8683       /* If the next token is a `:', then we are looking at a
8684          labeled-statement.  */
8685       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8686       if (token->type == CPP_COLON)
8687         {
8688           /* Looks like a labeled-statement with an ordinary label.
8689              Parse the label, and then use tail recursion to parse
8690              the statement.  */
8691           cp_parser_label_for_labeled_statement (parser);
8692           goto restart;
8693         }
8694     }
8695   /* Anything that starts with a `{' must be a compound-statement.  */
8696   else if (token->type == CPP_OPEN_BRACE)
8697     statement = cp_parser_compound_statement (parser, NULL, false, false);
8698   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8699      a statement all its own.  */
8700   else if (token->type == CPP_PRAGMA)
8701     {
8702       /* Only certain OpenMP pragmas are attached to statements, and thus
8703          are considered statements themselves.  All others are not.  In
8704          the context of a compound, accept the pragma as a "statement" and
8705          return so that we can check for a close brace.  Otherwise we
8706          require a real statement and must go back and read one.  */
8707       if (in_compound)
8708         cp_parser_pragma (parser, pragma_compound);
8709       else if (!cp_parser_pragma (parser, pragma_stmt))
8710         goto restart;
8711       return;
8712     }
8713   else if (token->type == CPP_EOF)
8714     {
8715       cp_parser_error (parser, "expected statement");
8716       return;
8717     }
8718
8719   /* Everything else must be a declaration-statement or an
8720      expression-statement.  Try for the declaration-statement
8721      first, unless we are looking at a `;', in which case we know that
8722      we have an expression-statement.  */
8723   if (!statement)
8724     {
8725       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8726         {
8727           cp_parser_parse_tentatively (parser);
8728           /* Try to parse the declaration-statement.  */
8729           cp_parser_declaration_statement (parser);
8730           /* If that worked, we're done.  */
8731           if (cp_parser_parse_definitely (parser))
8732             return;
8733         }
8734       /* Look for an expression-statement instead.  */
8735       statement = cp_parser_expression_statement (parser, in_statement_expr);
8736     }
8737
8738   /* Set the line number for the statement.  */
8739   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8740     SET_EXPR_LOCATION (statement, statement_location);
8741 }
8742
8743 /* Parse the label for a labeled-statement, i.e.
8744
8745    identifier :
8746    case constant-expression :
8747    default :
8748
8749    GNU Extension:
8750    case constant-expression ... constant-expression : statement
8751
8752    When a label is parsed without errors, the label is added to the
8753    parse tree by the finish_* functions, so this function doesn't
8754    have to return the label.  */
8755
8756 static void
8757 cp_parser_label_for_labeled_statement (cp_parser* parser)
8758 {
8759   cp_token *token;
8760   tree label = NULL_TREE;
8761   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8762
8763   /* The next token should be an identifier.  */
8764   token = cp_lexer_peek_token (parser->lexer);
8765   if (token->type != CPP_NAME
8766       && token->type != CPP_KEYWORD)
8767     {
8768       cp_parser_error (parser, "expected labeled-statement");
8769       return;
8770     }
8771
8772   parser->colon_corrects_to_scope_p = false;
8773   switch (token->keyword)
8774     {
8775     case RID_CASE:
8776       {
8777         tree expr, expr_hi;
8778         cp_token *ellipsis;
8779
8780         /* Consume the `case' token.  */
8781         cp_lexer_consume_token (parser->lexer);
8782         /* Parse the constant-expression.  */
8783         expr = cp_parser_constant_expression (parser,
8784                                               /*allow_non_constant_p=*/false,
8785                                               NULL);
8786
8787         ellipsis = cp_lexer_peek_token (parser->lexer);
8788         if (ellipsis->type == CPP_ELLIPSIS)
8789           {
8790             /* Consume the `...' token.  */
8791             cp_lexer_consume_token (parser->lexer);
8792             expr_hi =
8793               cp_parser_constant_expression (parser,
8794                                              /*allow_non_constant_p=*/false,
8795                                              NULL);
8796             /* We don't need to emit warnings here, as the common code
8797                will do this for us.  */
8798           }
8799         else
8800           expr_hi = NULL_TREE;
8801
8802         if (parser->in_switch_statement_p)
8803           finish_case_label (token->location, expr, expr_hi);
8804         else
8805           error_at (token->location,
8806                     "case label %qE not within a switch statement",
8807                     expr);
8808       }
8809       break;
8810
8811     case RID_DEFAULT:
8812       /* Consume the `default' token.  */
8813       cp_lexer_consume_token (parser->lexer);
8814
8815       if (parser->in_switch_statement_p)
8816         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8817       else
8818         error_at (token->location, "case label not within a switch statement");
8819       break;
8820
8821     default:
8822       /* Anything else must be an ordinary label.  */
8823       label = finish_label_stmt (cp_parser_identifier (parser));
8824       break;
8825     }
8826
8827   /* Require the `:' token.  */
8828   cp_parser_require (parser, CPP_COLON, RT_COLON);
8829
8830   /* An ordinary label may optionally be followed by attributes.
8831      However, this is only permitted if the attributes are then
8832      followed by a semicolon.  This is because, for backward
8833      compatibility, when parsing
8834        lab: __attribute__ ((unused)) int i;
8835      we want the attribute to attach to "i", not "lab".  */
8836   if (label != NULL_TREE
8837       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8838     {
8839       tree attrs;
8840
8841       cp_parser_parse_tentatively (parser);
8842       attrs = cp_parser_attributes_opt (parser);
8843       if (attrs == NULL_TREE
8844           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8845         cp_parser_abort_tentative_parse (parser);
8846       else if (!cp_parser_parse_definitely (parser))
8847         ;
8848       else
8849         cplus_decl_attributes (&label, attrs, 0);
8850     }
8851
8852   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8853 }
8854
8855 /* Parse an expression-statement.
8856
8857    expression-statement:
8858      expression [opt] ;
8859
8860    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8861    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8862    indicates whether this expression-statement is part of an
8863    expression statement.  */
8864
8865 static tree
8866 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8867 {
8868   tree statement = NULL_TREE;
8869   cp_token *token = cp_lexer_peek_token (parser->lexer);
8870
8871   /* If the next token is a ';', then there is no expression
8872      statement.  */
8873   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8874     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8875
8876   /* Give a helpful message for "A<T>::type t;" and the like.  */
8877   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8878       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8879     {
8880       if (TREE_CODE (statement) == SCOPE_REF)
8881         error_at (token->location, "need %<typename%> before %qE because "
8882                   "%qT is a dependent scope",
8883                   statement, TREE_OPERAND (statement, 0));
8884       else if (is_overloaded_fn (statement)
8885                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8886         {
8887           /* A::A a; */
8888           tree fn = get_first_fn (statement);
8889           error_at (token->location,
8890                     "%<%T::%D%> names the constructor, not the type",
8891                     DECL_CONTEXT (fn), DECL_NAME (fn));
8892         }
8893     }
8894
8895   /* Consume the final `;'.  */
8896   cp_parser_consume_semicolon_at_end_of_statement (parser);
8897
8898   if (in_statement_expr
8899       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8900     /* This is the final expression statement of a statement
8901        expression.  */
8902     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8903   else if (statement)
8904     statement = finish_expr_stmt (statement);
8905   else
8906     finish_stmt ();
8907
8908   return statement;
8909 }
8910
8911 /* Parse a compound-statement.
8912
8913    compound-statement:
8914      { statement-seq [opt] }
8915
8916    GNU extension:
8917
8918    compound-statement:
8919      { label-declaration-seq [opt] statement-seq [opt] }
8920
8921    label-declaration-seq:
8922      label-declaration
8923      label-declaration-seq label-declaration
8924
8925    Returns a tree representing the statement.  */
8926
8927 static tree
8928 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8929                               bool in_try, bool function_body)
8930 {
8931   tree compound_stmt;
8932
8933   /* Consume the `{'.  */
8934   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8935     return error_mark_node;
8936   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8937       && !function_body)
8938     pedwarn (input_location, OPT_pedantic,
8939              "compound-statement in constexpr function");
8940   /* Begin the compound-statement.  */
8941   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8942   /* If the next keyword is `__label__' we have a label declaration.  */
8943   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8944     cp_parser_label_declaration (parser);
8945   /* Parse an (optional) statement-seq.  */
8946   cp_parser_statement_seq_opt (parser, in_statement_expr);
8947   /* Finish the compound-statement.  */
8948   finish_compound_stmt (compound_stmt);
8949   /* Consume the `}'.  */
8950   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8951
8952   return compound_stmt;
8953 }
8954
8955 /* Parse an (optional) statement-seq.
8956
8957    statement-seq:
8958      statement
8959      statement-seq [opt] statement  */
8960
8961 static void
8962 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8963 {
8964   /* Scan statements until there aren't any more.  */
8965   while (true)
8966     {
8967       cp_token *token = cp_lexer_peek_token (parser->lexer);
8968
8969       /* If we are looking at a `}', then we have run out of
8970          statements; the same is true if we have reached the end
8971          of file, or have stumbled upon a stray '@end'.  */
8972       if (token->type == CPP_CLOSE_BRACE
8973           || token->type == CPP_EOF
8974           || token->type == CPP_PRAGMA_EOL
8975           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8976         break;
8977       
8978       /* If we are in a compound statement and find 'else' then
8979          something went wrong.  */
8980       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8981         {
8982           if (parser->in_statement & IN_IF_STMT) 
8983             break;
8984           else
8985             {
8986               token = cp_lexer_consume_token (parser->lexer);
8987               error_at (token->location, "%<else%> without a previous %<if%>");
8988             }
8989         }
8990
8991       /* Parse the statement.  */
8992       cp_parser_statement (parser, in_statement_expr, true, NULL);
8993     }
8994 }
8995
8996 /* Parse a selection-statement.
8997
8998    selection-statement:
8999      if ( condition ) statement
9000      if ( condition ) statement else statement
9001      switch ( condition ) statement
9002
9003    Returns the new IF_STMT or SWITCH_STMT.
9004
9005    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9006    is a (possibly labeled) if statement which is not enclosed in
9007    braces and has an else clause.  This is used to implement
9008    -Wparentheses.  */
9009
9010 static tree
9011 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9012 {
9013   cp_token *token;
9014   enum rid keyword;
9015
9016   if (if_p != NULL)
9017     *if_p = false;
9018
9019   /* Peek at the next token.  */
9020   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9021
9022   /* See what kind of keyword it is.  */
9023   keyword = token->keyword;
9024   switch (keyword)
9025     {
9026     case RID_IF:
9027     case RID_SWITCH:
9028       {
9029         tree statement;
9030         tree condition;
9031
9032         /* Look for the `('.  */
9033         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9034           {
9035             cp_parser_skip_to_end_of_statement (parser);
9036             return error_mark_node;
9037           }
9038
9039         /* Begin the selection-statement.  */
9040         if (keyword == RID_IF)
9041           statement = begin_if_stmt ();
9042         else
9043           statement = begin_switch_stmt ();
9044
9045         /* Parse the condition.  */
9046         condition = cp_parser_condition (parser);
9047         /* Look for the `)'.  */
9048         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9049           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9050                                                  /*consume_paren=*/true);
9051
9052         if (keyword == RID_IF)
9053           {
9054             bool nested_if;
9055             unsigned char in_statement;
9056
9057             /* Add the condition.  */
9058             finish_if_stmt_cond (condition, statement);
9059
9060             /* Parse the then-clause.  */
9061             in_statement = parser->in_statement;
9062             parser->in_statement |= IN_IF_STMT;
9063             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9064               {
9065                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9066                 add_stmt (build_empty_stmt (loc));
9067                 cp_lexer_consume_token (parser->lexer);
9068                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9069                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9070                               "empty body in an %<if%> statement");
9071                 nested_if = false;
9072               }
9073             else
9074               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9075             parser->in_statement = in_statement;
9076
9077             finish_then_clause (statement);
9078
9079             /* If the next token is `else', parse the else-clause.  */
9080             if (cp_lexer_next_token_is_keyword (parser->lexer,
9081                                                 RID_ELSE))
9082               {
9083                 /* Consume the `else' keyword.  */
9084                 cp_lexer_consume_token (parser->lexer);
9085                 begin_else_clause (statement);
9086                 /* Parse the else-clause.  */
9087                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9088                   {
9089                     location_t loc;
9090                     loc = cp_lexer_peek_token (parser->lexer)->location;
9091                     warning_at (loc,
9092                                 OPT_Wempty_body, "suggest braces around "
9093                                 "empty body in an %<else%> statement");
9094                     add_stmt (build_empty_stmt (loc));
9095                     cp_lexer_consume_token (parser->lexer);
9096                   }
9097                 else
9098                   cp_parser_implicitly_scoped_statement (parser, NULL);
9099
9100                 finish_else_clause (statement);
9101
9102                 /* If we are currently parsing a then-clause, then
9103                    IF_P will not be NULL.  We set it to true to
9104                    indicate that this if statement has an else clause.
9105                    This may trigger the Wparentheses warning below
9106                    when we get back up to the parent if statement.  */
9107                 if (if_p != NULL)
9108                   *if_p = true;
9109               }
9110             else
9111               {
9112                 /* This if statement does not have an else clause.  If
9113                    NESTED_IF is true, then the then-clause is an if
9114                    statement which does have an else clause.  We warn
9115                    about the potential ambiguity.  */
9116                 if (nested_if)
9117                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9118                               "suggest explicit braces to avoid ambiguous"
9119                               " %<else%>");
9120               }
9121
9122             /* Now we're all done with the if-statement.  */
9123             finish_if_stmt (statement);
9124           }
9125         else
9126           {
9127             bool in_switch_statement_p;
9128             unsigned char in_statement;
9129
9130             /* Add the condition.  */
9131             finish_switch_cond (condition, statement);
9132
9133             /* Parse the body of the switch-statement.  */
9134             in_switch_statement_p = parser->in_switch_statement_p;
9135             in_statement = parser->in_statement;
9136             parser->in_switch_statement_p = true;
9137             parser->in_statement |= IN_SWITCH_STMT;
9138             cp_parser_implicitly_scoped_statement (parser, NULL);
9139             parser->in_switch_statement_p = in_switch_statement_p;
9140             parser->in_statement = in_statement;
9141
9142             /* Now we're all done with the switch-statement.  */
9143             finish_switch_stmt (statement);
9144           }
9145
9146         return statement;
9147       }
9148       break;
9149
9150     default:
9151       cp_parser_error (parser, "expected selection-statement");
9152       return error_mark_node;
9153     }
9154 }
9155
9156 /* Parse a condition.
9157
9158    condition:
9159      expression
9160      type-specifier-seq declarator = initializer-clause
9161      type-specifier-seq declarator braced-init-list
9162
9163    GNU Extension:
9164
9165    condition:
9166      type-specifier-seq declarator asm-specification [opt]
9167        attributes [opt] = assignment-expression
9168
9169    Returns the expression that should be tested.  */
9170
9171 static tree
9172 cp_parser_condition (cp_parser* parser)
9173 {
9174   cp_decl_specifier_seq type_specifiers;
9175   const char *saved_message;
9176   int declares_class_or_enum;
9177
9178   /* Try the declaration first.  */
9179   cp_parser_parse_tentatively (parser);
9180   /* New types are not allowed in the type-specifier-seq for a
9181      condition.  */
9182   saved_message = parser->type_definition_forbidden_message;
9183   parser->type_definition_forbidden_message
9184     = G_("types may not be defined in conditions");
9185   /* Parse the type-specifier-seq.  */
9186   cp_parser_decl_specifier_seq (parser,
9187                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9188                                 &type_specifiers,
9189                                 &declares_class_or_enum);
9190   /* Restore the saved message.  */
9191   parser->type_definition_forbidden_message = saved_message;
9192   /* If all is well, we might be looking at a declaration.  */
9193   if (!cp_parser_error_occurred (parser))
9194     {
9195       tree decl;
9196       tree asm_specification;
9197       tree attributes;
9198       cp_declarator *declarator;
9199       tree initializer = NULL_TREE;
9200
9201       /* Parse the declarator.  */
9202       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9203                                          /*ctor_dtor_or_conv_p=*/NULL,
9204                                          /*parenthesized_p=*/NULL,
9205                                          /*member_p=*/false);
9206       /* Parse the attributes.  */
9207       attributes = cp_parser_attributes_opt (parser);
9208       /* Parse the asm-specification.  */
9209       asm_specification = cp_parser_asm_specification_opt (parser);
9210       /* If the next token is not an `=' or '{', then we might still be
9211          looking at an expression.  For example:
9212
9213            if (A(a).x)
9214
9215          looks like a decl-specifier-seq and a declarator -- but then
9216          there is no `=', so this is an expression.  */
9217       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9218           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9219         cp_parser_simulate_error (parser);
9220         
9221       /* If we did see an `=' or '{', then we are looking at a declaration
9222          for sure.  */
9223       if (cp_parser_parse_definitely (parser))
9224         {
9225           tree pushed_scope;
9226           bool non_constant_p;
9227           bool flags = LOOKUP_ONLYCONVERTING;
9228
9229           /* Create the declaration.  */
9230           decl = start_decl (declarator, &type_specifiers,
9231                              /*initialized_p=*/true,
9232                              attributes, /*prefix_attributes=*/NULL_TREE,
9233                              &pushed_scope);
9234
9235           /* Parse the initializer.  */
9236           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9237             {
9238               initializer = cp_parser_braced_list (parser, &non_constant_p);
9239               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9240               flags = 0;
9241             }
9242           else
9243             {
9244               /* Consume the `='.  */
9245               cp_parser_require (parser, CPP_EQ, RT_EQ);
9246               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9247             }
9248           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9249             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9250
9251           /* Process the initializer.  */
9252           cp_finish_decl (decl,
9253                           initializer, !non_constant_p,
9254                           asm_specification,
9255                           flags);
9256
9257           if (pushed_scope)
9258             pop_scope (pushed_scope);
9259
9260           return convert_from_reference (decl);
9261         }
9262     }
9263   /* If we didn't even get past the declarator successfully, we are
9264      definitely not looking at a declaration.  */
9265   else
9266     cp_parser_abort_tentative_parse (parser);
9267
9268   /* Otherwise, we are looking at an expression.  */
9269   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9270 }
9271
9272 /* Parses a for-statement or range-for-statement until the closing ')',
9273    not included. */
9274
9275 static tree
9276 cp_parser_for (cp_parser *parser)
9277 {
9278   tree init, scope, decl;
9279   bool is_range_for;
9280
9281   /* Begin the for-statement.  */
9282   scope = begin_for_scope (&init);
9283
9284   /* Parse the initialization.  */
9285   is_range_for = cp_parser_for_init_statement (parser, &decl);
9286
9287   if (is_range_for)
9288     return cp_parser_range_for (parser, scope, init, decl);
9289   else
9290     return cp_parser_c_for (parser, scope, init);
9291 }
9292
9293 static tree
9294 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9295 {
9296   /* Normal for loop */
9297   tree condition = NULL_TREE;
9298   tree expression = NULL_TREE;
9299   tree stmt;
9300
9301   stmt = begin_for_stmt (scope, init);
9302   /* The for-init-statement has already been parsed in
9303      cp_parser_for_init_statement, so no work is needed here.  */
9304   finish_for_init_stmt (stmt);
9305
9306   /* If there's a condition, process it.  */
9307   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9308     condition = cp_parser_condition (parser);
9309   finish_for_cond (condition, stmt);
9310   /* Look for the `;'.  */
9311   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9312
9313   /* If there's an expression, process it.  */
9314   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9315     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9316   finish_for_expr (expression, stmt);
9317
9318   return stmt;
9319 }
9320
9321 /* Tries to parse a range-based for-statement:
9322
9323   range-based-for:
9324     decl-specifier-seq declarator : expression
9325
9326   The decl-specifier-seq declarator and the `:' are already parsed by
9327   cp_parser_for_init_statement. If processing_template_decl it returns a
9328   newly created RANGE_FOR_STMT; if not, it is converted to a
9329   regular FOR_STMT.  */
9330
9331 static tree
9332 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9333 {
9334   tree stmt, range_expr;
9335
9336   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9337     {
9338       bool expr_non_constant_p;
9339       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9340     }
9341   else
9342     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9343
9344   /* If in template, STMT is converted to a normal for-statement
9345      at instantiation. If not, it is done just ahead. */
9346   if (processing_template_decl)
9347     {
9348       if (check_for_bare_parameter_packs (range_expr))
9349         range_expr = error_mark_node;
9350       stmt = begin_range_for_stmt (scope, init);
9351       finish_range_for_decl (stmt, range_decl, range_expr);
9352       if (!type_dependent_expression_p (range_expr)
9353           /* do_auto_deduction doesn't mess with template init-lists.  */
9354           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9355         do_range_for_auto_deduction (range_decl, range_expr);
9356     }
9357   else
9358     {
9359       stmt = begin_for_stmt (scope, init);
9360       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9361     }
9362   return stmt;
9363 }
9364
9365 /* Subroutine of cp_convert_range_for: given the initializer expression,
9366    builds up the range temporary.  */
9367
9368 static tree
9369 build_range_temp (tree range_expr)
9370 {
9371   tree range_type, range_temp;
9372
9373   /* Find out the type deduced by the declaration
9374      `auto &&__range = range_expr'.  */
9375   range_type = cp_build_reference_type (make_auto (), true);
9376   range_type = do_auto_deduction (range_type, range_expr,
9377                                   type_uses_auto (range_type));
9378
9379   /* Create the __range variable.  */
9380   range_temp = build_decl (input_location, VAR_DECL,
9381                            get_identifier ("__for_range"), range_type);
9382   TREE_USED (range_temp) = 1;
9383   DECL_ARTIFICIAL (range_temp) = 1;
9384
9385   return range_temp;
9386 }
9387
9388 /* Used by cp_parser_range_for in template context: we aren't going to
9389    do a full conversion yet, but we still need to resolve auto in the
9390    type of the for-range-declaration if present.  This is basically
9391    a shortcut version of cp_convert_range_for.  */
9392
9393 static void
9394 do_range_for_auto_deduction (tree decl, tree range_expr)
9395 {
9396   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9397   if (auto_node)
9398     {
9399       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9400       range_temp = convert_from_reference (build_range_temp (range_expr));
9401       iter_type = (cp_parser_perform_range_for_lookup
9402                    (range_temp, &begin_dummy, &end_dummy));
9403       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9404       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9405                                         tf_warning_or_error);
9406       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9407                                             iter_decl, auto_node);
9408     }
9409 }
9410
9411 /* Converts a range-based for-statement into a normal
9412    for-statement, as per the definition.
9413
9414       for (RANGE_DECL : RANGE_EXPR)
9415         BLOCK
9416
9417    should be equivalent to:
9418
9419       {
9420         auto &&__range = RANGE_EXPR;
9421         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9422               __begin != __end;
9423               ++__begin)
9424           {
9425               RANGE_DECL = *__begin;
9426               BLOCK
9427           }
9428       }
9429
9430    If RANGE_EXPR is an array:
9431         BEGIN_EXPR = __range
9432         END_EXPR = __range + ARRAY_SIZE(__range)
9433    Else if RANGE_EXPR has a member 'begin' or 'end':
9434         BEGIN_EXPR = __range.begin()
9435         END_EXPR = __range.end()
9436    Else:
9437         BEGIN_EXPR = begin(__range)
9438         END_EXPR = end(__range);
9439
9440    If __range has a member 'begin' but not 'end', or vice versa, we must
9441    still use the second alternative (it will surely fail, however).
9442    When calling begin()/end() in the third alternative we must use
9443    argument dependent lookup, but always considering 'std' as an associated
9444    namespace.  */
9445
9446 tree
9447 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9448 {
9449   tree begin, end;
9450   tree iter_type, begin_expr, end_expr;
9451   tree condition, expression;
9452
9453   if (range_decl == error_mark_node || range_expr == error_mark_node)
9454     /* If an error happened previously do nothing or else a lot of
9455        unhelpful errors would be issued.  */
9456     begin_expr = end_expr = iter_type = error_mark_node;
9457   else
9458     {
9459       tree range_temp = build_range_temp (range_expr);
9460       pushdecl (range_temp);
9461       cp_finish_decl (range_temp, range_expr,
9462                       /*is_constant_init*/false, NULL_TREE,
9463                       LOOKUP_ONLYCONVERTING);
9464
9465       range_temp = convert_from_reference (range_temp);
9466       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9467                                                       &begin_expr, &end_expr);
9468     }
9469
9470   /* The new for initialization statement.  */
9471   begin = build_decl (input_location, VAR_DECL,
9472                       get_identifier ("__for_begin"), iter_type);
9473   TREE_USED (begin) = 1;
9474   DECL_ARTIFICIAL (begin) = 1;
9475   pushdecl (begin);
9476   cp_finish_decl (begin, begin_expr,
9477                   /*is_constant_init*/false, NULL_TREE,
9478                   LOOKUP_ONLYCONVERTING);
9479
9480   end = build_decl (input_location, VAR_DECL,
9481                     get_identifier ("__for_end"), iter_type);
9482   TREE_USED (end) = 1;
9483   DECL_ARTIFICIAL (end) = 1;
9484   pushdecl (end);
9485   cp_finish_decl (end, end_expr,
9486                   /*is_constant_init*/false, NULL_TREE,
9487                   LOOKUP_ONLYCONVERTING);
9488
9489   finish_for_init_stmt (statement);
9490
9491   /* The new for condition.  */
9492   condition = build_x_binary_op (NE_EXPR,
9493                                  begin, ERROR_MARK,
9494                                  end, ERROR_MARK,
9495                                  NULL, tf_warning_or_error);
9496   finish_for_cond (condition, statement);
9497
9498   /* The new increment expression.  */
9499   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9500   finish_for_expr (expression, statement);
9501
9502   /* The declaration is initialized with *__begin inside the loop body.  */
9503   cp_finish_decl (range_decl,
9504                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9505                   /*is_constant_init*/false, NULL_TREE,
9506                   LOOKUP_ONLYCONVERTING);
9507
9508   return statement;
9509 }
9510
9511 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9512    We need to solve both at the same time because the method used
9513    depends on the existence of members begin or end.
9514    Returns the type deduced for the iterator expression.  */
9515
9516 static tree
9517 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9518 {
9519   if (error_operand_p (range))
9520     {
9521       *begin = *end = error_mark_node;
9522       return error_mark_node;
9523     }
9524
9525   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9526     {
9527       error ("range-based %<for%> expression of type %qT "
9528              "has incomplete type", TREE_TYPE (range));
9529       *begin = *end = error_mark_node;
9530       return error_mark_node;
9531     }
9532   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9533     {
9534       /* If RANGE is an array, we will use pointer arithmetic.  */
9535       *begin = range;
9536       *end = build_binary_op (input_location, PLUS_EXPR,
9537                               range,
9538                               array_type_nelts_top (TREE_TYPE (range)),
9539                               0);
9540       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9541     }
9542   else
9543     {
9544       /* If it is not an array, we must do a bit of magic.  */
9545       tree id_begin, id_end;
9546       tree member_begin, member_end;
9547
9548       *begin = *end = error_mark_node;
9549
9550       id_begin = get_identifier ("begin");
9551       id_end = get_identifier ("end");
9552       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9553                                     /*protect=*/2, /*want_type=*/false,
9554                                     tf_warning_or_error);
9555       member_end = lookup_member (TREE_TYPE (range), id_end,
9556                                   /*protect=*/2, /*want_type=*/false,
9557                                   tf_warning_or_error);
9558
9559       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9560         {
9561           /* Use the member functions.  */
9562           if (member_begin != NULL_TREE)
9563             *begin = cp_parser_range_for_member_function (range, id_begin);
9564           else
9565             error ("range-based %<for%> expression of type %qT has an "
9566                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9567
9568           if (member_end != NULL_TREE)
9569             *end = cp_parser_range_for_member_function (range, id_end);
9570           else
9571             error ("range-based %<for%> expression of type %qT has a "
9572                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9573         }
9574       else
9575         {
9576           /* Use global functions with ADL.  */
9577           VEC(tree,gc) *vec;
9578           vec = make_tree_vector ();
9579
9580           VEC_safe_push (tree, gc, vec, range);
9581
9582           member_begin = perform_koenig_lookup (id_begin, vec,
9583                                                 /*include_std=*/true,
9584                                                 tf_warning_or_error);
9585           *begin = finish_call_expr (member_begin, &vec, false, true,
9586                                      tf_warning_or_error);
9587           member_end = perform_koenig_lookup (id_end, vec,
9588                                               /*include_std=*/true,
9589                                               tf_warning_or_error);
9590           *end = finish_call_expr (member_end, &vec, false, true,
9591                                    tf_warning_or_error);
9592
9593           release_tree_vector (vec);
9594         }
9595
9596       /* Last common checks.  */
9597       if (*begin == error_mark_node || *end == error_mark_node)
9598         {
9599           /* If one of the expressions is an error do no more checks.  */
9600           *begin = *end = error_mark_node;
9601           return error_mark_node;
9602         }
9603       else
9604         {
9605           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9606           /* The unqualified type of the __begin and __end temporaries should
9607              be the same, as required by the multiple auto declaration.  */
9608           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9609             error ("inconsistent begin/end types in range-based %<for%> "
9610                    "statement: %qT and %qT",
9611                    TREE_TYPE (*begin), TREE_TYPE (*end));
9612           return iter_type;
9613         }
9614     }
9615 }
9616
9617 /* Helper function for cp_parser_perform_range_for_lookup.
9618    Builds a tree for RANGE.IDENTIFIER().  */
9619
9620 static tree
9621 cp_parser_range_for_member_function (tree range, tree identifier)
9622 {
9623   tree member, res;
9624   VEC(tree,gc) *vec;
9625
9626   member = finish_class_member_access_expr (range, identifier,
9627                                             false, tf_warning_or_error);
9628   if (member == error_mark_node)
9629     return error_mark_node;
9630
9631   vec = make_tree_vector ();
9632   res = finish_call_expr (member, &vec,
9633                           /*disallow_virtual=*/false,
9634                           /*koenig_p=*/false,
9635                           tf_warning_or_error);
9636   release_tree_vector (vec);
9637   return res;
9638 }
9639
9640 /* Parse an iteration-statement.
9641
9642    iteration-statement:
9643      while ( condition ) statement
9644      do statement while ( expression ) ;
9645      for ( for-init-statement condition [opt] ; expression [opt] )
9646        statement
9647
9648    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9649
9650 static tree
9651 cp_parser_iteration_statement (cp_parser* parser)
9652 {
9653   cp_token *token;
9654   enum rid keyword;
9655   tree statement;
9656   unsigned char in_statement;
9657
9658   /* Peek at the next token.  */
9659   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9660   if (!token)
9661     return error_mark_node;
9662
9663   /* Remember whether or not we are already within an iteration
9664      statement.  */
9665   in_statement = parser->in_statement;
9666
9667   /* See what kind of keyword it is.  */
9668   keyword = token->keyword;
9669   switch (keyword)
9670     {
9671     case RID_WHILE:
9672       {
9673         tree condition;
9674
9675         /* Begin the while-statement.  */
9676         statement = begin_while_stmt ();
9677         /* Look for the `('.  */
9678         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9679         /* Parse the condition.  */
9680         condition = cp_parser_condition (parser);
9681         finish_while_stmt_cond (condition, statement);
9682         /* Look for the `)'.  */
9683         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9684         /* Parse the dependent statement.  */
9685         parser->in_statement = IN_ITERATION_STMT;
9686         cp_parser_already_scoped_statement (parser);
9687         parser->in_statement = in_statement;
9688         /* We're done with the while-statement.  */
9689         finish_while_stmt (statement);
9690       }
9691       break;
9692
9693     case RID_DO:
9694       {
9695         tree expression;
9696
9697         /* Begin the do-statement.  */
9698         statement = begin_do_stmt ();
9699         /* Parse the body of the do-statement.  */
9700         parser->in_statement = IN_ITERATION_STMT;
9701         cp_parser_implicitly_scoped_statement (parser, NULL);
9702         parser->in_statement = in_statement;
9703         finish_do_body (statement);
9704         /* Look for the `while' keyword.  */
9705         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9706         /* Look for the `('.  */
9707         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9708         /* Parse the expression.  */
9709         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9710         /* We're done with the do-statement.  */
9711         finish_do_stmt (expression, statement);
9712         /* Look for the `)'.  */
9713         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9714         /* Look for the `;'.  */
9715         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9716       }
9717       break;
9718
9719     case RID_FOR:
9720       {
9721         /* Look for the `('.  */
9722         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9723
9724         statement = cp_parser_for (parser);
9725
9726         /* Look for the `)'.  */
9727         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9728
9729         /* Parse the body of the for-statement.  */
9730         parser->in_statement = IN_ITERATION_STMT;
9731         cp_parser_already_scoped_statement (parser);
9732         parser->in_statement = in_statement;
9733
9734         /* We're done with the for-statement.  */
9735         finish_for_stmt (statement);
9736       }
9737       break;
9738
9739     default:
9740       cp_parser_error (parser, "expected iteration-statement");
9741       statement = error_mark_node;
9742       break;
9743     }
9744
9745   return statement;
9746 }
9747
9748 /* Parse a for-init-statement or the declarator of a range-based-for.
9749    Returns true if a range-based-for declaration is seen.
9750
9751    for-init-statement:
9752      expression-statement
9753      simple-declaration  */
9754
9755 static bool
9756 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9757 {
9758   /* If the next token is a `;', then we have an empty
9759      expression-statement.  Grammatically, this is also a
9760      simple-declaration, but an invalid one, because it does not
9761      declare anything.  Therefore, if we did not handle this case
9762      specially, we would issue an error message about an invalid
9763      declaration.  */
9764   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9765     {
9766       bool is_range_for = false;
9767       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9768
9769       parser->colon_corrects_to_scope_p = false;
9770
9771       /* We're going to speculatively look for a declaration, falling back
9772          to an expression, if necessary.  */
9773       cp_parser_parse_tentatively (parser);
9774       /* Parse the declaration.  */
9775       cp_parser_simple_declaration (parser,
9776                                     /*function_definition_allowed_p=*/false,
9777                                     decl);
9778       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9779       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9780         {
9781           /* It is a range-for, consume the ':' */
9782           cp_lexer_consume_token (parser->lexer);
9783           is_range_for = true;
9784           if (cxx_dialect < cxx0x)
9785             {
9786               error_at (cp_lexer_peek_token (parser->lexer)->location,
9787                         "range-based %<for%> loops are not allowed "
9788                         "in C++98 mode");
9789               *decl = error_mark_node;
9790             }
9791         }
9792       else
9793           /* The ';' is not consumed yet because we told
9794              cp_parser_simple_declaration not to.  */
9795           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9796
9797       if (cp_parser_parse_definitely (parser))
9798         return is_range_for;
9799       /* If the tentative parse failed, then we shall need to look for an
9800          expression-statement.  */
9801     }
9802   /* If we are here, it is an expression-statement.  */
9803   cp_parser_expression_statement (parser, NULL_TREE);
9804   return false;
9805 }
9806
9807 /* Parse a jump-statement.
9808
9809    jump-statement:
9810      break ;
9811      continue ;
9812      return expression [opt] ;
9813      return braced-init-list ;
9814      goto identifier ;
9815
9816    GNU extension:
9817
9818    jump-statement:
9819      goto * expression ;
9820
9821    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9822
9823 static tree
9824 cp_parser_jump_statement (cp_parser* parser)
9825 {
9826   tree statement = error_mark_node;
9827   cp_token *token;
9828   enum rid keyword;
9829   unsigned char in_statement;
9830
9831   /* Peek at the next token.  */
9832   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9833   if (!token)
9834     return error_mark_node;
9835
9836   /* See what kind of keyword it is.  */
9837   keyword = token->keyword;
9838   switch (keyword)
9839     {
9840     case RID_BREAK:
9841       in_statement = parser->in_statement & ~IN_IF_STMT;      
9842       switch (in_statement)
9843         {
9844         case 0:
9845           error_at (token->location, "break statement not within loop or switch");
9846           break;
9847         default:
9848           gcc_assert ((in_statement & IN_SWITCH_STMT)
9849                       || in_statement == IN_ITERATION_STMT);
9850           statement = finish_break_stmt ();
9851           break;
9852         case IN_OMP_BLOCK:
9853           error_at (token->location, "invalid exit from OpenMP structured block");
9854           break;
9855         case IN_OMP_FOR:
9856           error_at (token->location, "break statement used with OpenMP for loop");
9857           break;
9858         }
9859       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9860       break;
9861
9862     case RID_CONTINUE:
9863       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9864         {
9865         case 0:
9866           error_at (token->location, "continue statement not within a loop");
9867           break;
9868         case IN_ITERATION_STMT:
9869         case IN_OMP_FOR:
9870           statement = finish_continue_stmt ();
9871           break;
9872         case IN_OMP_BLOCK:
9873           error_at (token->location, "invalid exit from OpenMP structured block");
9874           break;
9875         default:
9876           gcc_unreachable ();
9877         }
9878       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9879       break;
9880
9881     case RID_RETURN:
9882       {
9883         tree expr;
9884         bool expr_non_constant_p;
9885
9886         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9887           {
9888             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9889             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9890           }
9891         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9892           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9893         else
9894           /* If the next token is a `;', then there is no
9895              expression.  */
9896           expr = NULL_TREE;
9897         /* Build the return-statement.  */
9898         statement = finish_return_stmt (expr);
9899         /* Look for the final `;'.  */
9900         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9901       }
9902       break;
9903
9904     case RID_GOTO:
9905       /* Create the goto-statement.  */
9906       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9907         {
9908           /* Issue a warning about this use of a GNU extension.  */
9909           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9910           /* Consume the '*' token.  */
9911           cp_lexer_consume_token (parser->lexer);
9912           /* Parse the dependent expression.  */
9913           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9914         }
9915       else
9916         finish_goto_stmt (cp_parser_identifier (parser));
9917       /* Look for the final `;'.  */
9918       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9919       break;
9920
9921     default:
9922       cp_parser_error (parser, "expected jump-statement");
9923       break;
9924     }
9925
9926   return statement;
9927 }
9928
9929 /* Parse a declaration-statement.
9930
9931    declaration-statement:
9932      block-declaration  */
9933
9934 static void
9935 cp_parser_declaration_statement (cp_parser* parser)
9936 {
9937   void *p;
9938
9939   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9940   p = obstack_alloc (&declarator_obstack, 0);
9941
9942  /* Parse the block-declaration.  */
9943   cp_parser_block_declaration (parser, /*statement_p=*/true);
9944
9945   /* Free any declarators allocated.  */
9946   obstack_free (&declarator_obstack, p);
9947
9948   /* Finish off the statement.  */
9949   finish_stmt ();
9950 }
9951
9952 /* Some dependent statements (like `if (cond) statement'), are
9953    implicitly in their own scope.  In other words, if the statement is
9954    a single statement (as opposed to a compound-statement), it is
9955    none-the-less treated as if it were enclosed in braces.  Any
9956    declarations appearing in the dependent statement are out of scope
9957    after control passes that point.  This function parses a statement,
9958    but ensures that is in its own scope, even if it is not a
9959    compound-statement.
9960
9961    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9962    is a (possibly labeled) if statement which is not enclosed in
9963    braces and has an else clause.  This is used to implement
9964    -Wparentheses.
9965
9966    Returns the new statement.  */
9967
9968 static tree
9969 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9970 {
9971   tree statement;
9972
9973   if (if_p != NULL)
9974     *if_p = false;
9975
9976   /* Mark if () ; with a special NOP_EXPR.  */
9977   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9978     {
9979       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9980       cp_lexer_consume_token (parser->lexer);
9981       statement = add_stmt (build_empty_stmt (loc));
9982     }
9983   /* if a compound is opened, we simply parse the statement directly.  */
9984   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9985     statement = cp_parser_compound_statement (parser, NULL, false, false);
9986   /* If the token is not a `{', then we must take special action.  */
9987   else
9988     {
9989       /* Create a compound-statement.  */
9990       statement = begin_compound_stmt (0);
9991       /* Parse the dependent-statement.  */
9992       cp_parser_statement (parser, NULL_TREE, false, if_p);
9993       /* Finish the dummy compound-statement.  */
9994       finish_compound_stmt (statement);
9995     }
9996
9997   /* Return the statement.  */
9998   return statement;
9999 }
10000
10001 /* For some dependent statements (like `while (cond) statement'), we
10002    have already created a scope.  Therefore, even if the dependent
10003    statement is a compound-statement, we do not want to create another
10004    scope.  */
10005
10006 static void
10007 cp_parser_already_scoped_statement (cp_parser* parser)
10008 {
10009   /* If the token is a `{', then we must take special action.  */
10010   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10011     cp_parser_statement (parser, NULL_TREE, false, NULL);
10012   else
10013     {
10014       /* Avoid calling cp_parser_compound_statement, so that we
10015          don't create a new scope.  Do everything else by hand.  */
10016       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10017       /* If the next keyword is `__label__' we have a label declaration.  */
10018       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10019         cp_parser_label_declaration (parser);
10020       /* Parse an (optional) statement-seq.  */
10021       cp_parser_statement_seq_opt (parser, NULL_TREE);
10022       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10023     }
10024 }
10025
10026 /* Declarations [gram.dcl.dcl] */
10027
10028 /* Parse an optional declaration-sequence.
10029
10030    declaration-seq:
10031      declaration
10032      declaration-seq declaration  */
10033
10034 static void
10035 cp_parser_declaration_seq_opt (cp_parser* parser)
10036 {
10037   while (true)
10038     {
10039       cp_token *token;
10040
10041       token = cp_lexer_peek_token (parser->lexer);
10042
10043       if (token->type == CPP_CLOSE_BRACE
10044           || token->type == CPP_EOF
10045           || token->type == CPP_PRAGMA_EOL)
10046         break;
10047
10048       if (token->type == CPP_SEMICOLON)
10049         {
10050           /* A declaration consisting of a single semicolon is
10051              invalid.  Allow it unless we're being pedantic.  */
10052           cp_lexer_consume_token (parser->lexer);
10053           if (!in_system_header)
10054             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10055           continue;
10056         }
10057
10058       /* If we're entering or exiting a region that's implicitly
10059          extern "C", modify the lang context appropriately.  */
10060       if (!parser->implicit_extern_c && token->implicit_extern_c)
10061         {
10062           push_lang_context (lang_name_c);
10063           parser->implicit_extern_c = true;
10064         }
10065       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10066         {
10067           pop_lang_context ();
10068           parser->implicit_extern_c = false;
10069         }
10070
10071       if (token->type == CPP_PRAGMA)
10072         {
10073           /* A top-level declaration can consist solely of a #pragma.
10074              A nested declaration cannot, so this is done here and not
10075              in cp_parser_declaration.  (A #pragma at block scope is
10076              handled in cp_parser_statement.)  */
10077           cp_parser_pragma (parser, pragma_external);
10078           continue;
10079         }
10080
10081       /* Parse the declaration itself.  */
10082       cp_parser_declaration (parser);
10083     }
10084 }
10085
10086 /* Parse a declaration.
10087
10088    declaration:
10089      block-declaration
10090      function-definition
10091      template-declaration
10092      explicit-instantiation
10093      explicit-specialization
10094      linkage-specification
10095      namespace-definition
10096
10097    GNU extension:
10098
10099    declaration:
10100       __extension__ declaration */
10101
10102 static void
10103 cp_parser_declaration (cp_parser* parser)
10104 {
10105   cp_token token1;
10106   cp_token token2;
10107   int saved_pedantic;
10108   void *p;
10109   tree attributes = NULL_TREE;
10110
10111   /* Check for the `__extension__' keyword.  */
10112   if (cp_parser_extension_opt (parser, &saved_pedantic))
10113     {
10114       /* Parse the qualified declaration.  */
10115       cp_parser_declaration (parser);
10116       /* Restore the PEDANTIC flag.  */
10117       pedantic = saved_pedantic;
10118
10119       return;
10120     }
10121
10122   /* Try to figure out what kind of declaration is present.  */
10123   token1 = *cp_lexer_peek_token (parser->lexer);
10124
10125   if (token1.type != CPP_EOF)
10126     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10127   else
10128     {
10129       token2.type = CPP_EOF;
10130       token2.keyword = RID_MAX;
10131     }
10132
10133   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10134   p = obstack_alloc (&declarator_obstack, 0);
10135
10136   /* If the next token is `extern' and the following token is a string
10137      literal, then we have a linkage specification.  */
10138   if (token1.keyword == RID_EXTERN
10139       && cp_parser_is_pure_string_literal (&token2))
10140     cp_parser_linkage_specification (parser);
10141   /* If the next token is `template', then we have either a template
10142      declaration, an explicit instantiation, or an explicit
10143      specialization.  */
10144   else if (token1.keyword == RID_TEMPLATE)
10145     {
10146       /* `template <>' indicates a template specialization.  */
10147       if (token2.type == CPP_LESS
10148           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10149         cp_parser_explicit_specialization (parser);
10150       /* `template <' indicates a template declaration.  */
10151       else if (token2.type == CPP_LESS)
10152         cp_parser_template_declaration (parser, /*member_p=*/false);
10153       /* Anything else must be an explicit instantiation.  */
10154       else
10155         cp_parser_explicit_instantiation (parser);
10156     }
10157   /* If the next token is `export', then we have a template
10158      declaration.  */
10159   else if (token1.keyword == RID_EXPORT)
10160     cp_parser_template_declaration (parser, /*member_p=*/false);
10161   /* If the next token is `extern', 'static' or 'inline' and the one
10162      after that is `template', we have a GNU extended explicit
10163      instantiation directive.  */
10164   else if (cp_parser_allow_gnu_extensions_p (parser)
10165            && (token1.keyword == RID_EXTERN
10166                || token1.keyword == RID_STATIC
10167                || token1.keyword == RID_INLINE)
10168            && token2.keyword == RID_TEMPLATE)
10169     cp_parser_explicit_instantiation (parser);
10170   /* If the next token is `namespace', check for a named or unnamed
10171      namespace definition.  */
10172   else if (token1.keyword == RID_NAMESPACE
10173            && (/* A named namespace definition.  */
10174                (token2.type == CPP_NAME
10175                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10176                     != CPP_EQ))
10177                /* An unnamed namespace definition.  */
10178                || token2.type == CPP_OPEN_BRACE
10179                || token2.keyword == RID_ATTRIBUTE))
10180     cp_parser_namespace_definition (parser);
10181   /* An inline (associated) namespace definition.  */
10182   else if (token1.keyword == RID_INLINE
10183            && token2.keyword == RID_NAMESPACE)
10184     cp_parser_namespace_definition (parser);
10185   /* Objective-C++ declaration/definition.  */
10186   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10187     cp_parser_objc_declaration (parser, NULL_TREE);
10188   else if (c_dialect_objc ()
10189            && token1.keyword == RID_ATTRIBUTE
10190            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10191     cp_parser_objc_declaration (parser, attributes);
10192   /* We must have either a block declaration or a function
10193      definition.  */
10194   else
10195     /* Try to parse a block-declaration, or a function-definition.  */
10196     cp_parser_block_declaration (parser, /*statement_p=*/false);
10197
10198   /* Free any declarators allocated.  */
10199   obstack_free (&declarator_obstack, p);
10200 }
10201
10202 /* Parse a block-declaration.
10203
10204    block-declaration:
10205      simple-declaration
10206      asm-definition
10207      namespace-alias-definition
10208      using-declaration
10209      using-directive
10210
10211    GNU Extension:
10212
10213    block-declaration:
10214      __extension__ block-declaration
10215
10216    C++0x Extension:
10217
10218    block-declaration:
10219      static_assert-declaration
10220
10221    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10222    part of a declaration-statement.  */
10223
10224 static void
10225 cp_parser_block_declaration (cp_parser *parser,
10226                              bool      statement_p)
10227 {
10228   cp_token *token1;
10229   int saved_pedantic;
10230
10231   /* Check for the `__extension__' keyword.  */
10232   if (cp_parser_extension_opt (parser, &saved_pedantic))
10233     {
10234       /* Parse the qualified declaration.  */
10235       cp_parser_block_declaration (parser, statement_p);
10236       /* Restore the PEDANTIC flag.  */
10237       pedantic = saved_pedantic;
10238
10239       return;
10240     }
10241
10242   /* Peek at the next token to figure out which kind of declaration is
10243      present.  */
10244   token1 = cp_lexer_peek_token (parser->lexer);
10245
10246   /* If the next keyword is `asm', we have an asm-definition.  */
10247   if (token1->keyword == RID_ASM)
10248     {
10249       if (statement_p)
10250         cp_parser_commit_to_tentative_parse (parser);
10251       cp_parser_asm_definition (parser);
10252     }
10253   /* If the next keyword is `namespace', we have a
10254      namespace-alias-definition.  */
10255   else if (token1->keyword == RID_NAMESPACE)
10256     cp_parser_namespace_alias_definition (parser);
10257   /* If the next keyword is `using', we have a
10258      using-declaration, a using-directive, or an alias-declaration.  */
10259   else if (token1->keyword == RID_USING)
10260     {
10261       cp_token *token2;
10262
10263       if (statement_p)
10264         cp_parser_commit_to_tentative_parse (parser);
10265       /* If the token after `using' is `namespace', then we have a
10266          using-directive.  */
10267       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10268       if (token2->keyword == RID_NAMESPACE)
10269         cp_parser_using_directive (parser);
10270       /* If the second token after 'using' is '=', then we have an
10271          alias-declaration.  */
10272       else if (cxx_dialect >= cxx0x
10273                && token2->type == CPP_NAME
10274                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10275                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10276                        == RID_ATTRIBUTE)))
10277         cp_parser_alias_declaration (parser);
10278       /* Otherwise, it's a using-declaration.  */
10279       else
10280         cp_parser_using_declaration (parser,
10281                                      /*access_declaration_p=*/false);
10282     }
10283   /* If the next keyword is `__label__' we have a misplaced label
10284      declaration.  */
10285   else if (token1->keyword == RID_LABEL)
10286     {
10287       cp_lexer_consume_token (parser->lexer);
10288       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10289       cp_parser_skip_to_end_of_statement (parser);
10290       /* If the next token is now a `;', consume it.  */
10291       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10292         cp_lexer_consume_token (parser->lexer);
10293     }
10294   /* If the next token is `static_assert' we have a static assertion.  */
10295   else if (token1->keyword == RID_STATIC_ASSERT)
10296     cp_parser_static_assert (parser, /*member_p=*/false);
10297   /* Anything else must be a simple-declaration.  */
10298   else
10299     cp_parser_simple_declaration (parser, !statement_p,
10300                                   /*maybe_range_for_decl*/NULL);
10301 }
10302
10303 /* Parse a simple-declaration.
10304
10305    simple-declaration:
10306      decl-specifier-seq [opt] init-declarator-list [opt] ;
10307
10308    init-declarator-list:
10309      init-declarator
10310      init-declarator-list , init-declarator
10311
10312    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10313    function-definition as a simple-declaration.
10314
10315    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10316    parsed declaration if it is an uninitialized single declarator not followed
10317    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10318    if present, will not be consumed.  */
10319
10320 static void
10321 cp_parser_simple_declaration (cp_parser* parser,
10322                               bool function_definition_allowed_p,
10323                               tree *maybe_range_for_decl)
10324 {
10325   cp_decl_specifier_seq decl_specifiers;
10326   int declares_class_or_enum;
10327   bool saw_declarator;
10328
10329   if (maybe_range_for_decl)
10330     *maybe_range_for_decl = NULL_TREE;
10331
10332   /* Defer access checks until we know what is being declared; the
10333      checks for names appearing in the decl-specifier-seq should be
10334      done as if we were in the scope of the thing being declared.  */
10335   push_deferring_access_checks (dk_deferred);
10336
10337   /* Parse the decl-specifier-seq.  We have to keep track of whether
10338      or not the decl-specifier-seq declares a named class or
10339      enumeration type, since that is the only case in which the
10340      init-declarator-list is allowed to be empty.
10341
10342      [dcl.dcl]
10343
10344      In a simple-declaration, the optional init-declarator-list can be
10345      omitted only when declaring a class or enumeration, that is when
10346      the decl-specifier-seq contains either a class-specifier, an
10347      elaborated-type-specifier, or an enum-specifier.  */
10348   cp_parser_decl_specifier_seq (parser,
10349                                 CP_PARSER_FLAGS_OPTIONAL,
10350                                 &decl_specifiers,
10351                                 &declares_class_or_enum);
10352   /* We no longer need to defer access checks.  */
10353   stop_deferring_access_checks ();
10354
10355   /* In a block scope, a valid declaration must always have a
10356      decl-specifier-seq.  By not trying to parse declarators, we can
10357      resolve the declaration/expression ambiguity more quickly.  */
10358   if (!function_definition_allowed_p
10359       && !decl_specifiers.any_specifiers_p)
10360     {
10361       cp_parser_error (parser, "expected declaration");
10362       goto done;
10363     }
10364
10365   /* If the next two tokens are both identifiers, the code is
10366      erroneous. The usual cause of this situation is code like:
10367
10368        T t;
10369
10370      where "T" should name a type -- but does not.  */
10371   if (!decl_specifiers.any_type_specifiers_p
10372       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10373     {
10374       /* If parsing tentatively, we should commit; we really are
10375          looking at a declaration.  */
10376       cp_parser_commit_to_tentative_parse (parser);
10377       /* Give up.  */
10378       goto done;
10379     }
10380
10381   /* If we have seen at least one decl-specifier, and the next token
10382      is not a parenthesis, then we must be looking at a declaration.
10383      (After "int (" we might be looking at a functional cast.)  */
10384   if (decl_specifiers.any_specifiers_p
10385       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10386       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10387       && !cp_parser_error_occurred (parser))
10388     cp_parser_commit_to_tentative_parse (parser);
10389
10390   /* Keep going until we hit the `;' at the end of the simple
10391      declaration.  */
10392   saw_declarator = false;
10393   while (cp_lexer_next_token_is_not (parser->lexer,
10394                                      CPP_SEMICOLON))
10395     {
10396       cp_token *token;
10397       bool function_definition_p;
10398       tree decl;
10399
10400       if (saw_declarator)
10401         {
10402           /* If we are processing next declarator, coma is expected */
10403           token = cp_lexer_peek_token (parser->lexer);
10404           gcc_assert (token->type == CPP_COMMA);
10405           cp_lexer_consume_token (parser->lexer);
10406           if (maybe_range_for_decl)
10407             *maybe_range_for_decl = error_mark_node;
10408         }
10409       else
10410         saw_declarator = true;
10411
10412       /* Parse the init-declarator.  */
10413       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10414                                         /*checks=*/NULL,
10415                                         function_definition_allowed_p,
10416                                         /*member_p=*/false,
10417                                         declares_class_or_enum,
10418                                         &function_definition_p,
10419                                         maybe_range_for_decl);
10420       /* If an error occurred while parsing tentatively, exit quickly.
10421          (That usually happens when in the body of a function; each
10422          statement is treated as a declaration-statement until proven
10423          otherwise.)  */
10424       if (cp_parser_error_occurred (parser))
10425         goto done;
10426       /* Handle function definitions specially.  */
10427       if (function_definition_p)
10428         {
10429           /* If the next token is a `,', then we are probably
10430              processing something like:
10431
10432                void f() {}, *p;
10433
10434              which is erroneous.  */
10435           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10436             {
10437               cp_token *token = cp_lexer_peek_token (parser->lexer);
10438               error_at (token->location,
10439                         "mixing"
10440                         " declarations and function-definitions is forbidden");
10441             }
10442           /* Otherwise, we're done with the list of declarators.  */
10443           else
10444             {
10445               pop_deferring_access_checks ();
10446               return;
10447             }
10448         }
10449       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10450         *maybe_range_for_decl = decl;
10451       /* The next token should be either a `,' or a `;'.  */
10452       token = cp_lexer_peek_token (parser->lexer);
10453       /* If it's a `,', there are more declarators to come.  */
10454       if (token->type == CPP_COMMA)
10455         /* will be consumed next time around */;
10456       /* If it's a `;', we are done.  */
10457       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10458         break;
10459       /* Anything else is an error.  */
10460       else
10461         {
10462           /* If we have already issued an error message we don't need
10463              to issue another one.  */
10464           if (decl != error_mark_node
10465               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10466             cp_parser_error (parser, "expected %<,%> or %<;%>");
10467           /* Skip tokens until we reach the end of the statement.  */
10468           cp_parser_skip_to_end_of_statement (parser);
10469           /* If the next token is now a `;', consume it.  */
10470           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10471             cp_lexer_consume_token (parser->lexer);
10472           goto done;
10473         }
10474       /* After the first time around, a function-definition is not
10475          allowed -- even if it was OK at first.  For example:
10476
10477            int i, f() {}
10478
10479          is not valid.  */
10480       function_definition_allowed_p = false;
10481     }
10482
10483   /* Issue an error message if no declarators are present, and the
10484      decl-specifier-seq does not itself declare a class or
10485      enumeration.  */
10486   if (!saw_declarator)
10487     {
10488       if (cp_parser_declares_only_class_p (parser))
10489         shadow_tag (&decl_specifiers);
10490       /* Perform any deferred access checks.  */
10491       perform_deferred_access_checks ();
10492     }
10493
10494   /* Consume the `;'.  */
10495   if (!maybe_range_for_decl)
10496       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10497
10498  done:
10499   pop_deferring_access_checks ();
10500 }
10501
10502 /* Parse a decl-specifier-seq.
10503
10504    decl-specifier-seq:
10505      decl-specifier-seq [opt] decl-specifier
10506
10507    decl-specifier:
10508      storage-class-specifier
10509      type-specifier
10510      function-specifier
10511      friend
10512      typedef
10513
10514    GNU Extension:
10515
10516    decl-specifier:
10517      attributes
10518
10519    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10520
10521    The parser flags FLAGS is used to control type-specifier parsing.
10522
10523    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10524    flags:
10525
10526      1: one of the decl-specifiers is an elaborated-type-specifier
10527         (i.e., a type declaration)
10528      2: one of the decl-specifiers is an enum-specifier or a
10529         class-specifier (i.e., a type definition)
10530
10531    */
10532
10533 static void
10534 cp_parser_decl_specifier_seq (cp_parser* parser,
10535                               cp_parser_flags flags,
10536                               cp_decl_specifier_seq *decl_specs,
10537                               int* declares_class_or_enum)
10538 {
10539   bool constructor_possible_p = !parser->in_declarator_p;
10540   cp_token *start_token = NULL;
10541
10542   /* Clear DECL_SPECS.  */
10543   clear_decl_specs (decl_specs);
10544
10545   /* Assume no class or enumeration type is declared.  */
10546   *declares_class_or_enum = 0;
10547
10548   /* Keep reading specifiers until there are no more to read.  */
10549   while (true)
10550     {
10551       bool constructor_p;
10552       bool found_decl_spec;
10553       cp_token *token;
10554
10555       /* Peek at the next token.  */
10556       token = cp_lexer_peek_token (parser->lexer);
10557
10558       /* Save the first token of the decl spec list for error
10559          reporting.  */
10560       if (!start_token)
10561         start_token = token;
10562       /* Handle attributes.  */
10563       if (token->keyword == RID_ATTRIBUTE)
10564         {
10565           /* Parse the attributes.  */
10566           decl_specs->attributes
10567             = chainon (decl_specs->attributes,
10568                        cp_parser_attributes_opt (parser));
10569           continue;
10570         }
10571       /* Assume we will find a decl-specifier keyword.  */
10572       found_decl_spec = true;
10573       /* If the next token is an appropriate keyword, we can simply
10574          add it to the list.  */
10575       switch (token->keyword)
10576         {
10577           /* decl-specifier:
10578                friend
10579                constexpr */
10580         case RID_FRIEND:
10581           if (!at_class_scope_p ())
10582             {
10583               error_at (token->location, "%<friend%> used outside of class");
10584               cp_lexer_purge_token (parser->lexer);
10585             }
10586           else
10587             {
10588               ++decl_specs->specs[(int) ds_friend];
10589               /* Consume the token.  */
10590               cp_lexer_consume_token (parser->lexer);
10591             }
10592           break;
10593
10594         case RID_CONSTEXPR:
10595           ++decl_specs->specs[(int) ds_constexpr];
10596           cp_lexer_consume_token (parser->lexer);
10597           break;
10598
10599           /* function-specifier:
10600                inline
10601                virtual
10602                explicit  */
10603         case RID_INLINE:
10604         case RID_VIRTUAL:
10605         case RID_EXPLICIT:
10606           cp_parser_function_specifier_opt (parser, decl_specs);
10607           break;
10608
10609           /* decl-specifier:
10610                typedef  */
10611         case RID_TYPEDEF:
10612           ++decl_specs->specs[(int) ds_typedef];
10613           /* Consume the token.  */
10614           cp_lexer_consume_token (parser->lexer);
10615           /* A constructor declarator cannot appear in a typedef.  */
10616           constructor_possible_p = false;
10617           /* The "typedef" keyword can only occur in a declaration; we
10618              may as well commit at this point.  */
10619           cp_parser_commit_to_tentative_parse (parser);
10620
10621           if (decl_specs->storage_class != sc_none)
10622             decl_specs->conflicting_specifiers_p = true;
10623           break;
10624
10625           /* storage-class-specifier:
10626                auto
10627                register
10628                static
10629                extern
10630                mutable
10631
10632              GNU Extension:
10633                thread  */
10634         case RID_AUTO:
10635           if (cxx_dialect == cxx98) 
10636             {
10637               /* Consume the token.  */
10638               cp_lexer_consume_token (parser->lexer);
10639
10640               /* Complain about `auto' as a storage specifier, if
10641                  we're complaining about C++0x compatibility.  */
10642               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10643                           " changes meaning in C++11; please remove it");
10644
10645               /* Set the storage class anyway.  */
10646               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10647                                            token->location);
10648             }
10649           else
10650             /* C++0x auto type-specifier.  */
10651             found_decl_spec = false;
10652           break;
10653
10654         case RID_REGISTER:
10655         case RID_STATIC:
10656         case RID_EXTERN:
10657         case RID_MUTABLE:
10658           /* Consume the token.  */
10659           cp_lexer_consume_token (parser->lexer);
10660           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10661                                        token->location);
10662           break;
10663         case RID_THREAD:
10664           /* Consume the token.  */
10665           cp_lexer_consume_token (parser->lexer);
10666           ++decl_specs->specs[(int) ds_thread];
10667           break;
10668
10669         default:
10670           /* We did not yet find a decl-specifier yet.  */
10671           found_decl_spec = false;
10672           break;
10673         }
10674
10675       if (found_decl_spec
10676           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10677           && token->keyword != RID_CONSTEXPR)
10678         error ("decl-specifier invalid in condition");
10679
10680       /* Constructors are a special case.  The `S' in `S()' is not a
10681          decl-specifier; it is the beginning of the declarator.  */
10682       constructor_p
10683         = (!found_decl_spec
10684            && constructor_possible_p
10685            && (cp_parser_constructor_declarator_p
10686                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10687
10688       /* If we don't have a DECL_SPEC yet, then we must be looking at
10689          a type-specifier.  */
10690       if (!found_decl_spec && !constructor_p)
10691         {
10692           int decl_spec_declares_class_or_enum;
10693           bool is_cv_qualifier;
10694           tree type_spec;
10695
10696           type_spec
10697             = cp_parser_type_specifier (parser, flags,
10698                                         decl_specs,
10699                                         /*is_declaration=*/true,
10700                                         &decl_spec_declares_class_or_enum,
10701                                         &is_cv_qualifier);
10702           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10703
10704           /* If this type-specifier referenced a user-defined type
10705              (a typedef, class-name, etc.), then we can't allow any
10706              more such type-specifiers henceforth.
10707
10708              [dcl.spec]
10709
10710              The longest sequence of decl-specifiers that could
10711              possibly be a type name is taken as the
10712              decl-specifier-seq of a declaration.  The sequence shall
10713              be self-consistent as described below.
10714
10715              [dcl.type]
10716
10717              As a general rule, at most one type-specifier is allowed
10718              in the complete decl-specifier-seq of a declaration.  The
10719              only exceptions are the following:
10720
10721              -- const or volatile can be combined with any other
10722                 type-specifier.
10723
10724              -- signed or unsigned can be combined with char, long,
10725                 short, or int.
10726
10727              -- ..
10728
10729              Example:
10730
10731                typedef char* Pc;
10732                void g (const int Pc);
10733
10734              Here, Pc is *not* part of the decl-specifier seq; it's
10735              the declarator.  Therefore, once we see a type-specifier
10736              (other than a cv-qualifier), we forbid any additional
10737              user-defined types.  We *do* still allow things like `int
10738              int' to be considered a decl-specifier-seq, and issue the
10739              error message later.  */
10740           if (type_spec && !is_cv_qualifier)
10741             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10742           /* A constructor declarator cannot follow a type-specifier.  */
10743           if (type_spec)
10744             {
10745               constructor_possible_p = false;
10746               found_decl_spec = true;
10747               if (!is_cv_qualifier)
10748                 decl_specs->any_type_specifiers_p = true;
10749             }
10750         }
10751
10752       /* If we still do not have a DECL_SPEC, then there are no more
10753          decl-specifiers.  */
10754       if (!found_decl_spec)
10755         break;
10756
10757       decl_specs->any_specifiers_p = true;
10758       /* After we see one decl-specifier, further decl-specifiers are
10759          always optional.  */
10760       flags |= CP_PARSER_FLAGS_OPTIONAL;
10761     }
10762
10763   cp_parser_check_decl_spec (decl_specs, start_token->location);
10764
10765   /* Don't allow a friend specifier with a class definition.  */
10766   if (decl_specs->specs[(int) ds_friend] != 0
10767       && (*declares_class_or_enum & 2))
10768     error_at (start_token->location,
10769               "class definition may not be declared a friend");
10770 }
10771
10772 /* Parse an (optional) storage-class-specifier.
10773
10774    storage-class-specifier:
10775      auto
10776      register
10777      static
10778      extern
10779      mutable
10780
10781    GNU Extension:
10782
10783    storage-class-specifier:
10784      thread
10785
10786    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10787
10788 static tree
10789 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10790 {
10791   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10792     {
10793     case RID_AUTO:
10794       if (cxx_dialect != cxx98)
10795         return NULL_TREE;
10796       /* Fall through for C++98.  */
10797
10798     case RID_REGISTER:
10799     case RID_STATIC:
10800     case RID_EXTERN:
10801     case RID_MUTABLE:
10802     case RID_THREAD:
10803       /* Consume the token.  */
10804       return cp_lexer_consume_token (parser->lexer)->u.value;
10805
10806     default:
10807       return NULL_TREE;
10808     }
10809 }
10810
10811 /* Parse an (optional) function-specifier.
10812
10813    function-specifier:
10814      inline
10815      virtual
10816      explicit
10817
10818    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10819    Updates DECL_SPECS, if it is non-NULL.  */
10820
10821 static tree
10822 cp_parser_function_specifier_opt (cp_parser* parser,
10823                                   cp_decl_specifier_seq *decl_specs)
10824 {
10825   cp_token *token = cp_lexer_peek_token (parser->lexer);
10826   switch (token->keyword)
10827     {
10828     case RID_INLINE:
10829       if (decl_specs)
10830         ++decl_specs->specs[(int) ds_inline];
10831       break;
10832
10833     case RID_VIRTUAL:
10834       /* 14.5.2.3 [temp.mem]
10835
10836          A member function template shall not be virtual.  */
10837       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10838         error_at (token->location, "templates may not be %<virtual%>");
10839       else if (decl_specs)
10840         ++decl_specs->specs[(int) ds_virtual];
10841       break;
10842
10843     case RID_EXPLICIT:
10844       if (decl_specs)
10845         ++decl_specs->specs[(int) ds_explicit];
10846       break;
10847
10848     default:
10849       return NULL_TREE;
10850     }
10851
10852   /* Consume the token.  */
10853   return cp_lexer_consume_token (parser->lexer)->u.value;
10854 }
10855
10856 /* Parse a linkage-specification.
10857
10858    linkage-specification:
10859      extern string-literal { declaration-seq [opt] }
10860      extern string-literal declaration  */
10861
10862 static void
10863 cp_parser_linkage_specification (cp_parser* parser)
10864 {
10865   tree linkage;
10866
10867   /* Look for the `extern' keyword.  */
10868   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10869
10870   /* Look for the string-literal.  */
10871   linkage = cp_parser_string_literal (parser, false, false);
10872
10873   /* Transform the literal into an identifier.  If the literal is a
10874      wide-character string, or contains embedded NULs, then we can't
10875      handle it as the user wants.  */
10876   if (strlen (TREE_STRING_POINTER (linkage))
10877       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10878     {
10879       cp_parser_error (parser, "invalid linkage-specification");
10880       /* Assume C++ linkage.  */
10881       linkage = lang_name_cplusplus;
10882     }
10883   else
10884     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10885
10886   /* We're now using the new linkage.  */
10887   push_lang_context (linkage);
10888
10889   /* If the next token is a `{', then we're using the first
10890      production.  */
10891   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10892     {
10893       /* Consume the `{' token.  */
10894       cp_lexer_consume_token (parser->lexer);
10895       /* Parse the declarations.  */
10896       cp_parser_declaration_seq_opt (parser);
10897       /* Look for the closing `}'.  */
10898       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10899     }
10900   /* Otherwise, there's just one declaration.  */
10901   else
10902     {
10903       bool saved_in_unbraced_linkage_specification_p;
10904
10905       saved_in_unbraced_linkage_specification_p
10906         = parser->in_unbraced_linkage_specification_p;
10907       parser->in_unbraced_linkage_specification_p = true;
10908       cp_parser_declaration (parser);
10909       parser->in_unbraced_linkage_specification_p
10910         = saved_in_unbraced_linkage_specification_p;
10911     }
10912
10913   /* We're done with the linkage-specification.  */
10914   pop_lang_context ();
10915 }
10916
10917 /* Parse a static_assert-declaration.
10918
10919    static_assert-declaration:
10920      static_assert ( constant-expression , string-literal ) ; 
10921
10922    If MEMBER_P, this static_assert is a class member.  */
10923
10924 static void 
10925 cp_parser_static_assert(cp_parser *parser, bool member_p)
10926 {
10927   tree condition;
10928   tree message;
10929   cp_token *token;
10930   location_t saved_loc;
10931   bool dummy;
10932
10933   /* Peek at the `static_assert' token so we can keep track of exactly
10934      where the static assertion started.  */
10935   token = cp_lexer_peek_token (parser->lexer);
10936   saved_loc = token->location;
10937
10938   /* Look for the `static_assert' keyword.  */
10939   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10940                                   RT_STATIC_ASSERT))
10941     return;
10942
10943   /*  We know we are in a static assertion; commit to any tentative
10944       parse.  */
10945   if (cp_parser_parsing_tentatively (parser))
10946     cp_parser_commit_to_tentative_parse (parser);
10947
10948   /* Parse the `(' starting the static assertion condition.  */
10949   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10950
10951   /* Parse the constant-expression.  Allow a non-constant expression
10952      here in order to give better diagnostics in finish_static_assert.  */
10953   condition = 
10954     cp_parser_constant_expression (parser,
10955                                    /*allow_non_constant_p=*/true,
10956                                    /*non_constant_p=*/&dummy);
10957
10958   /* Parse the separating `,'.  */
10959   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10960
10961   /* Parse the string-literal message.  */
10962   message = cp_parser_string_literal (parser, 
10963                                       /*translate=*/false,
10964                                       /*wide_ok=*/true);
10965
10966   /* A `)' completes the static assertion.  */
10967   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10968     cp_parser_skip_to_closing_parenthesis (parser, 
10969                                            /*recovering=*/true, 
10970                                            /*or_comma=*/false,
10971                                            /*consume_paren=*/true);
10972
10973   /* A semicolon terminates the declaration.  */
10974   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10975
10976   /* Complete the static assertion, which may mean either processing 
10977      the static assert now or saving it for template instantiation.  */
10978   finish_static_assert (condition, message, saved_loc, member_p);
10979 }
10980
10981 /* Parse a `decltype' type. Returns the type. 
10982
10983    simple-type-specifier:
10984      decltype ( expression )  */
10985
10986 static tree
10987 cp_parser_decltype (cp_parser *parser)
10988 {
10989   tree expr;
10990   bool id_expression_or_member_access_p = false;
10991   const char *saved_message;
10992   bool saved_integral_constant_expression_p;
10993   bool saved_non_integral_constant_expression_p;
10994   cp_token *id_expr_start_token;
10995   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10996
10997   if (start_token->type == CPP_DECLTYPE)
10998     {
10999       /* Already parsed.  */
11000       cp_lexer_consume_token (parser->lexer);
11001       return start_token->u.value;
11002     }
11003
11004   /* Look for the `decltype' token.  */
11005   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11006     return error_mark_node;
11007
11008   /* Types cannot be defined in a `decltype' expression.  Save away the
11009      old message.  */
11010   saved_message = parser->type_definition_forbidden_message;
11011
11012   /* And create the new one.  */
11013   parser->type_definition_forbidden_message
11014     = G_("types may not be defined in %<decltype%> expressions");
11015
11016   /* The restrictions on constant-expressions do not apply inside
11017      decltype expressions.  */
11018   saved_integral_constant_expression_p
11019     = parser->integral_constant_expression_p;
11020   saved_non_integral_constant_expression_p
11021     = parser->non_integral_constant_expression_p;
11022   parser->integral_constant_expression_p = false;
11023
11024   /* Do not actually evaluate the expression.  */
11025   ++cp_unevaluated_operand;
11026
11027   /* Do not warn about problems with the expression.  */
11028   ++c_inhibit_evaluation_warnings;
11029
11030   /* Parse the opening `('.  */
11031   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11032     return error_mark_node;
11033   
11034   /* First, try parsing an id-expression.  */
11035   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11036   cp_parser_parse_tentatively (parser);
11037   expr = cp_parser_id_expression (parser,
11038                                   /*template_keyword_p=*/false,
11039                                   /*check_dependency_p=*/true,
11040                                   /*template_p=*/NULL,
11041                                   /*declarator_p=*/false,
11042                                   /*optional_p=*/false);
11043
11044   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11045     {
11046       bool non_integral_constant_expression_p = false;
11047       tree id_expression = expr;
11048       cp_id_kind idk;
11049       const char *error_msg;
11050
11051       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11052         /* Lookup the name we got back from the id-expression.  */
11053         expr = cp_parser_lookup_name (parser, expr,
11054                                       none_type,
11055                                       /*is_template=*/false,
11056                                       /*is_namespace=*/false,
11057                                       /*check_dependency=*/true,
11058                                       /*ambiguous_decls=*/NULL,
11059                                       id_expr_start_token->location);
11060
11061       if (expr
11062           && expr != error_mark_node
11063           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11064           && TREE_CODE (expr) != TYPE_DECL
11065           && (TREE_CODE (expr) != BIT_NOT_EXPR
11066               || !TYPE_P (TREE_OPERAND (expr, 0)))
11067           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11068         {
11069           /* Complete lookup of the id-expression.  */
11070           expr = (finish_id_expression
11071                   (id_expression, expr, parser->scope, &idk,
11072                    /*integral_constant_expression_p=*/false,
11073                    /*allow_non_integral_constant_expression_p=*/true,
11074                    &non_integral_constant_expression_p,
11075                    /*template_p=*/false,
11076                    /*done=*/true,
11077                    /*address_p=*/false,
11078                    /*template_arg_p=*/false,
11079                    &error_msg,
11080                    id_expr_start_token->location));
11081
11082           if (expr == error_mark_node)
11083             /* We found an id-expression, but it was something that we
11084                should not have found. This is an error, not something
11085                we can recover from, so note that we found an
11086                id-expression and we'll recover as gracefully as
11087                possible.  */
11088             id_expression_or_member_access_p = true;
11089         }
11090
11091       if (expr 
11092           && expr != error_mark_node
11093           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11094         /* We have an id-expression.  */
11095         id_expression_or_member_access_p = true;
11096     }
11097
11098   if (!id_expression_or_member_access_p)
11099     {
11100       /* Abort the id-expression parse.  */
11101       cp_parser_abort_tentative_parse (parser);
11102
11103       /* Parsing tentatively, again.  */
11104       cp_parser_parse_tentatively (parser);
11105
11106       /* Parse a class member access.  */
11107       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11108                                            /*cast_p=*/false,
11109                                            /*member_access_only_p=*/true, NULL);
11110
11111       if (expr 
11112           && expr != error_mark_node
11113           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11114         /* We have an id-expression.  */
11115         id_expression_or_member_access_p = true;
11116     }
11117
11118   if (id_expression_or_member_access_p)
11119     /* We have parsed the complete id-expression or member access.  */
11120     cp_parser_parse_definitely (parser);
11121   else
11122     {
11123       bool saved_greater_than_is_operator_p;
11124
11125       /* Abort our attempt to parse an id-expression or member access
11126          expression.  */
11127       cp_parser_abort_tentative_parse (parser);
11128
11129       /* Within a parenthesized expression, a `>' token is always
11130          the greater-than operator.  */
11131       saved_greater_than_is_operator_p
11132         = parser->greater_than_is_operator_p;
11133       parser->greater_than_is_operator_p = true;
11134
11135       /* Parse a full expression.  */
11136       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11137
11138       /* The `>' token might be the end of a template-id or
11139          template-parameter-list now.  */
11140       parser->greater_than_is_operator_p
11141         = saved_greater_than_is_operator_p;
11142     }
11143
11144   /* Go back to evaluating expressions.  */
11145   --cp_unevaluated_operand;
11146   --c_inhibit_evaluation_warnings;
11147
11148   /* Restore the old message and the integral constant expression
11149      flags.  */
11150   parser->type_definition_forbidden_message = saved_message;
11151   parser->integral_constant_expression_p
11152     = saved_integral_constant_expression_p;
11153   parser->non_integral_constant_expression_p
11154     = saved_non_integral_constant_expression_p;
11155
11156   /* Parse to the closing `)'.  */
11157   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11158     {
11159       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11160                                              /*consume_paren=*/true);
11161       return error_mark_node;
11162     }
11163
11164   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11165                                tf_warning_or_error);
11166
11167   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11168      it again.  */
11169   start_token->type = CPP_DECLTYPE;
11170   start_token->u.value = expr;
11171   start_token->keyword = RID_MAX;
11172   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11173
11174   return expr;
11175 }
11176
11177 /* Special member functions [gram.special] */
11178
11179 /* Parse a conversion-function-id.
11180
11181    conversion-function-id:
11182      operator conversion-type-id
11183
11184    Returns an IDENTIFIER_NODE representing the operator.  */
11185
11186 static tree
11187 cp_parser_conversion_function_id (cp_parser* parser)
11188 {
11189   tree type;
11190   tree saved_scope;
11191   tree saved_qualifying_scope;
11192   tree saved_object_scope;
11193   tree pushed_scope = NULL_TREE;
11194
11195   /* Look for the `operator' token.  */
11196   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11197     return error_mark_node;
11198   /* When we parse the conversion-type-id, the current scope will be
11199      reset.  However, we need that information in able to look up the
11200      conversion function later, so we save it here.  */
11201   saved_scope = parser->scope;
11202   saved_qualifying_scope = parser->qualifying_scope;
11203   saved_object_scope = parser->object_scope;
11204   /* We must enter the scope of the class so that the names of
11205      entities declared within the class are available in the
11206      conversion-type-id.  For example, consider:
11207
11208        struct S {
11209          typedef int I;
11210          operator I();
11211        };
11212
11213        S::operator I() { ... }
11214
11215      In order to see that `I' is a type-name in the definition, we
11216      must be in the scope of `S'.  */
11217   if (saved_scope)
11218     pushed_scope = push_scope (saved_scope);
11219   /* Parse the conversion-type-id.  */
11220   type = cp_parser_conversion_type_id (parser);
11221   /* Leave the scope of the class, if any.  */
11222   if (pushed_scope)
11223     pop_scope (pushed_scope);
11224   /* Restore the saved scope.  */
11225   parser->scope = saved_scope;
11226   parser->qualifying_scope = saved_qualifying_scope;
11227   parser->object_scope = saved_object_scope;
11228   /* If the TYPE is invalid, indicate failure.  */
11229   if (type == error_mark_node)
11230     return error_mark_node;
11231   return mangle_conv_op_name_for_type (type);
11232 }
11233
11234 /* Parse a conversion-type-id:
11235
11236    conversion-type-id:
11237      type-specifier-seq conversion-declarator [opt]
11238
11239    Returns the TYPE specified.  */
11240
11241 static tree
11242 cp_parser_conversion_type_id (cp_parser* parser)
11243 {
11244   tree attributes;
11245   cp_decl_specifier_seq type_specifiers;
11246   cp_declarator *declarator;
11247   tree type_specified;
11248
11249   /* Parse the attributes.  */
11250   attributes = cp_parser_attributes_opt (parser);
11251   /* Parse the type-specifiers.  */
11252   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11253                                 /*is_trailing_return=*/false,
11254                                 &type_specifiers);
11255   /* If that didn't work, stop.  */
11256   if (type_specifiers.type == error_mark_node)
11257     return error_mark_node;
11258   /* Parse the conversion-declarator.  */
11259   declarator = cp_parser_conversion_declarator_opt (parser);
11260
11261   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11262                                     /*initialized=*/0, &attributes);
11263   if (attributes)
11264     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11265
11266   /* Don't give this error when parsing tentatively.  This happens to
11267      work because we always parse this definitively once.  */
11268   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11269       && type_uses_auto (type_specified))
11270     {
11271       error ("invalid use of %<auto%> in conversion operator");
11272       return error_mark_node;
11273     }
11274
11275   return type_specified;
11276 }
11277
11278 /* Parse an (optional) conversion-declarator.
11279
11280    conversion-declarator:
11281      ptr-operator conversion-declarator [opt]
11282
11283    */
11284
11285 static cp_declarator *
11286 cp_parser_conversion_declarator_opt (cp_parser* parser)
11287 {
11288   enum tree_code code;
11289   tree class_type;
11290   cp_cv_quals cv_quals;
11291
11292   /* We don't know if there's a ptr-operator next, or not.  */
11293   cp_parser_parse_tentatively (parser);
11294   /* Try the ptr-operator.  */
11295   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11296   /* If it worked, look for more conversion-declarators.  */
11297   if (cp_parser_parse_definitely (parser))
11298     {
11299       cp_declarator *declarator;
11300
11301       /* Parse another optional declarator.  */
11302       declarator = cp_parser_conversion_declarator_opt (parser);
11303
11304       return cp_parser_make_indirect_declarator
11305         (code, class_type, cv_quals, declarator);
11306    }
11307
11308   return NULL;
11309 }
11310
11311 /* Parse an (optional) ctor-initializer.
11312
11313    ctor-initializer:
11314      : mem-initializer-list
11315
11316    Returns TRUE iff the ctor-initializer was actually present.  */
11317
11318 static bool
11319 cp_parser_ctor_initializer_opt (cp_parser* parser)
11320 {
11321   /* If the next token is not a `:', then there is no
11322      ctor-initializer.  */
11323   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11324     {
11325       /* Do default initialization of any bases and members.  */
11326       if (DECL_CONSTRUCTOR_P (current_function_decl))
11327         finish_mem_initializers (NULL_TREE);
11328
11329       return false;
11330     }
11331
11332   /* Consume the `:' token.  */
11333   cp_lexer_consume_token (parser->lexer);
11334   /* And the mem-initializer-list.  */
11335   cp_parser_mem_initializer_list (parser);
11336
11337   return true;
11338 }
11339
11340 /* Parse a mem-initializer-list.
11341
11342    mem-initializer-list:
11343      mem-initializer ... [opt]
11344      mem-initializer ... [opt] , mem-initializer-list  */
11345
11346 static void
11347 cp_parser_mem_initializer_list (cp_parser* parser)
11348 {
11349   tree mem_initializer_list = NULL_TREE;
11350   tree target_ctor = error_mark_node;
11351   cp_token *token = cp_lexer_peek_token (parser->lexer);
11352
11353   /* Let the semantic analysis code know that we are starting the
11354      mem-initializer-list.  */
11355   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11356     error_at (token->location,
11357               "only constructors take member initializers");
11358
11359   /* Loop through the list.  */
11360   while (true)
11361     {
11362       tree mem_initializer;
11363
11364       token = cp_lexer_peek_token (parser->lexer);
11365       /* Parse the mem-initializer.  */
11366       mem_initializer = cp_parser_mem_initializer (parser);
11367       /* If the next token is a `...', we're expanding member initializers. */
11368       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11369         {
11370           /* Consume the `...'. */
11371           cp_lexer_consume_token (parser->lexer);
11372
11373           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11374              can be expanded but members cannot. */
11375           if (mem_initializer != error_mark_node
11376               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11377             {
11378               error_at (token->location,
11379                         "cannot expand initializer for member %<%D%>",
11380                         TREE_PURPOSE (mem_initializer));
11381               mem_initializer = error_mark_node;
11382             }
11383
11384           /* Construct the pack expansion type. */
11385           if (mem_initializer != error_mark_node)
11386             mem_initializer = make_pack_expansion (mem_initializer);
11387         }
11388       if (target_ctor != error_mark_node
11389           && mem_initializer != error_mark_node)
11390         {
11391           error ("mem-initializer for %qD follows constructor delegation",
11392                  TREE_PURPOSE (mem_initializer));
11393           mem_initializer = error_mark_node;
11394         }
11395       /* Look for a target constructor. */
11396       if (mem_initializer != error_mark_node
11397           && TYPE_P (TREE_PURPOSE (mem_initializer))
11398           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11399         {
11400           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11401           if (mem_initializer_list)
11402             {
11403               error ("constructor delegation follows mem-initializer for %qD",
11404                      TREE_PURPOSE (mem_initializer_list));
11405               mem_initializer = error_mark_node;
11406             }
11407           target_ctor = mem_initializer;
11408         }
11409       /* Add it to the list, unless it was erroneous.  */
11410       if (mem_initializer != error_mark_node)
11411         {
11412           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11413           mem_initializer_list = mem_initializer;
11414         }
11415       /* If the next token is not a `,', we're done.  */
11416       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11417         break;
11418       /* Consume the `,' token.  */
11419       cp_lexer_consume_token (parser->lexer);
11420     }
11421
11422   /* Perform semantic analysis.  */
11423   if (DECL_CONSTRUCTOR_P (current_function_decl))
11424     finish_mem_initializers (mem_initializer_list);
11425 }
11426
11427 /* Parse a mem-initializer.
11428
11429    mem-initializer:
11430      mem-initializer-id ( expression-list [opt] )
11431      mem-initializer-id braced-init-list
11432
11433    GNU extension:
11434
11435    mem-initializer:
11436      ( expression-list [opt] )
11437
11438    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11439    class) or FIELD_DECL (for a non-static data member) to initialize;
11440    the TREE_VALUE is the expression-list.  An empty initialization
11441    list is represented by void_list_node.  */
11442
11443 static tree
11444 cp_parser_mem_initializer (cp_parser* parser)
11445 {
11446   tree mem_initializer_id;
11447   tree expression_list;
11448   tree member;
11449   cp_token *token = cp_lexer_peek_token (parser->lexer);
11450
11451   /* Find out what is being initialized.  */
11452   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11453     {
11454       permerror (token->location,
11455                  "anachronistic old-style base class initializer");
11456       mem_initializer_id = NULL_TREE;
11457     }
11458   else
11459     {
11460       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11461       if (mem_initializer_id == error_mark_node)
11462         return mem_initializer_id;
11463     }
11464   member = expand_member_init (mem_initializer_id);
11465   if (member && !DECL_P (member))
11466     in_base_initializer = 1;
11467
11468   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11469     {
11470       bool expr_non_constant_p;
11471       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11472       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11473       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11474       expression_list = build_tree_list (NULL_TREE, expression_list);
11475     }
11476   else
11477     {
11478       VEC(tree,gc)* vec;
11479       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11480                                                      /*cast_p=*/false,
11481                                                      /*allow_expansion_p=*/true,
11482                                                      /*non_constant_p=*/NULL);
11483       if (vec == NULL)
11484         return error_mark_node;
11485       expression_list = build_tree_list_vec (vec);
11486       release_tree_vector (vec);
11487     }
11488
11489   if (expression_list == error_mark_node)
11490     return error_mark_node;
11491   if (!expression_list)
11492     expression_list = void_type_node;
11493
11494   in_base_initializer = 0;
11495
11496   return member ? build_tree_list (member, expression_list) : error_mark_node;
11497 }
11498
11499 /* Parse a mem-initializer-id.
11500
11501    mem-initializer-id:
11502      :: [opt] nested-name-specifier [opt] class-name
11503      identifier
11504
11505    Returns a TYPE indicating the class to be initializer for the first
11506    production.  Returns an IDENTIFIER_NODE indicating the data member
11507    to be initialized for the second production.  */
11508
11509 static tree
11510 cp_parser_mem_initializer_id (cp_parser* parser)
11511 {
11512   bool global_scope_p;
11513   bool nested_name_specifier_p;
11514   bool template_p = false;
11515   tree id;
11516
11517   cp_token *token = cp_lexer_peek_token (parser->lexer);
11518
11519   /* `typename' is not allowed in this context ([temp.res]).  */
11520   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11521     {
11522       error_at (token->location, 
11523                 "keyword %<typename%> not allowed in this context (a qualified "
11524                 "member initializer is implicitly a type)");
11525       cp_lexer_consume_token (parser->lexer);
11526     }
11527   /* Look for the optional `::' operator.  */
11528   global_scope_p
11529     = (cp_parser_global_scope_opt (parser,
11530                                    /*current_scope_valid_p=*/false)
11531        != NULL_TREE);
11532   /* Look for the optional nested-name-specifier.  The simplest way to
11533      implement:
11534
11535        [temp.res]
11536
11537        The keyword `typename' is not permitted in a base-specifier or
11538        mem-initializer; in these contexts a qualified name that
11539        depends on a template-parameter is implicitly assumed to be a
11540        type name.
11541
11542      is to assume that we have seen the `typename' keyword at this
11543      point.  */
11544   nested_name_specifier_p
11545     = (cp_parser_nested_name_specifier_opt (parser,
11546                                             /*typename_keyword_p=*/true,
11547                                             /*check_dependency_p=*/true,
11548                                             /*type_p=*/true,
11549                                             /*is_declaration=*/true)
11550        != NULL_TREE);
11551   if (nested_name_specifier_p)
11552     template_p = cp_parser_optional_template_keyword (parser);
11553   /* If there is a `::' operator or a nested-name-specifier, then we
11554      are definitely looking for a class-name.  */
11555   if (global_scope_p || nested_name_specifier_p)
11556     return cp_parser_class_name (parser,
11557                                  /*typename_keyword_p=*/true,
11558                                  /*template_keyword_p=*/template_p,
11559                                  typename_type,
11560                                  /*check_dependency_p=*/true,
11561                                  /*class_head_p=*/false,
11562                                  /*is_declaration=*/true);
11563   /* Otherwise, we could also be looking for an ordinary identifier.  */
11564   cp_parser_parse_tentatively (parser);
11565   /* Try a class-name.  */
11566   id = cp_parser_class_name (parser,
11567                              /*typename_keyword_p=*/true,
11568                              /*template_keyword_p=*/false,
11569                              none_type,
11570                              /*check_dependency_p=*/true,
11571                              /*class_head_p=*/false,
11572                              /*is_declaration=*/true);
11573   /* If we found one, we're done.  */
11574   if (cp_parser_parse_definitely (parser))
11575     return id;
11576   /* Otherwise, look for an ordinary identifier.  */
11577   return cp_parser_identifier (parser);
11578 }
11579
11580 /* Overloading [gram.over] */
11581
11582 /* Parse an operator-function-id.
11583
11584    operator-function-id:
11585      operator operator
11586
11587    Returns an IDENTIFIER_NODE for the operator which is a
11588    human-readable spelling of the identifier, e.g., `operator +'.  */
11589
11590 static tree
11591 cp_parser_operator_function_id (cp_parser* parser)
11592 {
11593   /* Look for the `operator' keyword.  */
11594   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11595     return error_mark_node;
11596   /* And then the name of the operator itself.  */
11597   return cp_parser_operator (parser);
11598 }
11599
11600 /* Return an identifier node for a user-defined literal operator.
11601    The suffix identifier is chained to the operator name identifier.  */
11602
11603 static tree
11604 cp_literal_operator_id (const char* name)
11605 {
11606   tree identifier;
11607   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11608                               + strlen (name) + 10);
11609   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11610   identifier = get_identifier (buffer);
11611   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11612
11613   return identifier;
11614 }
11615
11616 /* Parse an operator.
11617
11618    operator:
11619      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11620      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11621      || ++ -- , ->* -> () []
11622
11623    GNU Extensions:
11624
11625    operator:
11626      <? >? <?= >?=
11627
11628    Returns an IDENTIFIER_NODE for the operator which is a
11629    human-readable spelling of the identifier, e.g., `operator +'.  */
11630
11631 static tree
11632 cp_parser_operator (cp_parser* parser)
11633 {
11634   tree id = NULL_TREE;
11635   cp_token *token;
11636
11637   /* Peek at the next token.  */
11638   token = cp_lexer_peek_token (parser->lexer);
11639   /* Figure out which operator we have.  */
11640   switch (token->type)
11641     {
11642     case CPP_KEYWORD:
11643       {
11644         enum tree_code op;
11645
11646         /* The keyword should be either `new' or `delete'.  */
11647         if (token->keyword == RID_NEW)
11648           op = NEW_EXPR;
11649         else if (token->keyword == RID_DELETE)
11650           op = DELETE_EXPR;
11651         else
11652           break;
11653
11654         /* Consume the `new' or `delete' token.  */
11655         cp_lexer_consume_token (parser->lexer);
11656
11657         /* Peek at the next token.  */
11658         token = cp_lexer_peek_token (parser->lexer);
11659         /* If it's a `[' token then this is the array variant of the
11660            operator.  */
11661         if (token->type == CPP_OPEN_SQUARE)
11662           {
11663             /* Consume the `[' token.  */
11664             cp_lexer_consume_token (parser->lexer);
11665             /* Look for the `]' token.  */
11666             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11667             id = ansi_opname (op == NEW_EXPR
11668                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11669           }
11670         /* Otherwise, we have the non-array variant.  */
11671         else
11672           id = ansi_opname (op);
11673
11674         return id;
11675       }
11676
11677     case CPP_PLUS:
11678       id = ansi_opname (PLUS_EXPR);
11679       break;
11680
11681     case CPP_MINUS:
11682       id = ansi_opname (MINUS_EXPR);
11683       break;
11684
11685     case CPP_MULT:
11686       id = ansi_opname (MULT_EXPR);
11687       break;
11688
11689     case CPP_DIV:
11690       id = ansi_opname (TRUNC_DIV_EXPR);
11691       break;
11692
11693     case CPP_MOD:
11694       id = ansi_opname (TRUNC_MOD_EXPR);
11695       break;
11696
11697     case CPP_XOR:
11698       id = ansi_opname (BIT_XOR_EXPR);
11699       break;
11700
11701     case CPP_AND:
11702       id = ansi_opname (BIT_AND_EXPR);
11703       break;
11704
11705     case CPP_OR:
11706       id = ansi_opname (BIT_IOR_EXPR);
11707       break;
11708
11709     case CPP_COMPL:
11710       id = ansi_opname (BIT_NOT_EXPR);
11711       break;
11712
11713     case CPP_NOT:
11714       id = ansi_opname (TRUTH_NOT_EXPR);
11715       break;
11716
11717     case CPP_EQ:
11718       id = ansi_assopname (NOP_EXPR);
11719       break;
11720
11721     case CPP_LESS:
11722       id = ansi_opname (LT_EXPR);
11723       break;
11724
11725     case CPP_GREATER:
11726       id = ansi_opname (GT_EXPR);
11727       break;
11728
11729     case CPP_PLUS_EQ:
11730       id = ansi_assopname (PLUS_EXPR);
11731       break;
11732
11733     case CPP_MINUS_EQ:
11734       id = ansi_assopname (MINUS_EXPR);
11735       break;
11736
11737     case CPP_MULT_EQ:
11738       id = ansi_assopname (MULT_EXPR);
11739       break;
11740
11741     case CPP_DIV_EQ:
11742       id = ansi_assopname (TRUNC_DIV_EXPR);
11743       break;
11744
11745     case CPP_MOD_EQ:
11746       id = ansi_assopname (TRUNC_MOD_EXPR);
11747       break;
11748
11749     case CPP_XOR_EQ:
11750       id = ansi_assopname (BIT_XOR_EXPR);
11751       break;
11752
11753     case CPP_AND_EQ:
11754       id = ansi_assopname (BIT_AND_EXPR);
11755       break;
11756
11757     case CPP_OR_EQ:
11758       id = ansi_assopname (BIT_IOR_EXPR);
11759       break;
11760
11761     case CPP_LSHIFT:
11762       id = ansi_opname (LSHIFT_EXPR);
11763       break;
11764
11765     case CPP_RSHIFT:
11766       id = ansi_opname (RSHIFT_EXPR);
11767       break;
11768
11769     case CPP_LSHIFT_EQ:
11770       id = ansi_assopname (LSHIFT_EXPR);
11771       break;
11772
11773     case CPP_RSHIFT_EQ:
11774       id = ansi_assopname (RSHIFT_EXPR);
11775       break;
11776
11777     case CPP_EQ_EQ:
11778       id = ansi_opname (EQ_EXPR);
11779       break;
11780
11781     case CPP_NOT_EQ:
11782       id = ansi_opname (NE_EXPR);
11783       break;
11784
11785     case CPP_LESS_EQ:
11786       id = ansi_opname (LE_EXPR);
11787       break;
11788
11789     case CPP_GREATER_EQ:
11790       id = ansi_opname (GE_EXPR);
11791       break;
11792
11793     case CPP_AND_AND:
11794       id = ansi_opname (TRUTH_ANDIF_EXPR);
11795       break;
11796
11797     case CPP_OR_OR:
11798       id = ansi_opname (TRUTH_ORIF_EXPR);
11799       break;
11800
11801     case CPP_PLUS_PLUS:
11802       id = ansi_opname (POSTINCREMENT_EXPR);
11803       break;
11804
11805     case CPP_MINUS_MINUS:
11806       id = ansi_opname (PREDECREMENT_EXPR);
11807       break;
11808
11809     case CPP_COMMA:
11810       id = ansi_opname (COMPOUND_EXPR);
11811       break;
11812
11813     case CPP_DEREF_STAR:
11814       id = ansi_opname (MEMBER_REF);
11815       break;
11816
11817     case CPP_DEREF:
11818       id = ansi_opname (COMPONENT_REF);
11819       break;
11820
11821     case CPP_OPEN_PAREN:
11822       /* Consume the `('.  */
11823       cp_lexer_consume_token (parser->lexer);
11824       /* Look for the matching `)'.  */
11825       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11826       return ansi_opname (CALL_EXPR);
11827
11828     case CPP_OPEN_SQUARE:
11829       /* Consume the `['.  */
11830       cp_lexer_consume_token (parser->lexer);
11831       /* Look for the matching `]'.  */
11832       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11833       return ansi_opname (ARRAY_REF);
11834
11835     case CPP_STRING:
11836       if (cxx_dialect == cxx98)
11837         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11838       if (TREE_STRING_LENGTH (token->u.value) > 2)
11839         {
11840           error ("expected empty string after %<operator%> keyword");
11841           return error_mark_node;
11842         }
11843       /* Consume the string.  */
11844       cp_lexer_consume_token (parser->lexer);
11845       /* Look for the suffix identifier.  */
11846       token = cp_lexer_peek_token (parser->lexer);
11847       if (token->type == CPP_NAME)
11848         {
11849           id = cp_parser_identifier (parser);
11850           if (id != error_mark_node)
11851             {
11852               const char *name = IDENTIFIER_POINTER (id);
11853               return cp_literal_operator_id (name);
11854             }
11855         }
11856       else
11857         {
11858           error ("expected suffix identifier");
11859           return error_mark_node;
11860         }
11861
11862     case CPP_STRING_USERDEF:
11863       error ("missing space between %<\"\"%> and suffix identifier");
11864       return error_mark_node;
11865
11866     default:
11867       /* Anything else is an error.  */
11868       break;
11869     }
11870
11871   /* If we have selected an identifier, we need to consume the
11872      operator token.  */
11873   if (id)
11874     cp_lexer_consume_token (parser->lexer);
11875   /* Otherwise, no valid operator name was present.  */
11876   else
11877     {
11878       cp_parser_error (parser, "expected operator");
11879       id = error_mark_node;
11880     }
11881
11882   return id;
11883 }
11884
11885 /* Parse a template-declaration.
11886
11887    template-declaration:
11888      export [opt] template < template-parameter-list > declaration
11889
11890    If MEMBER_P is TRUE, this template-declaration occurs within a
11891    class-specifier.
11892
11893    The grammar rule given by the standard isn't correct.  What
11894    is really meant is:
11895
11896    template-declaration:
11897      export [opt] template-parameter-list-seq
11898        decl-specifier-seq [opt] init-declarator [opt] ;
11899      export [opt] template-parameter-list-seq
11900        function-definition
11901
11902    template-parameter-list-seq:
11903      template-parameter-list-seq [opt]
11904      template < template-parameter-list >  */
11905
11906 static void
11907 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11908 {
11909   /* Check for `export'.  */
11910   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11911     {
11912       /* Consume the `export' token.  */
11913       cp_lexer_consume_token (parser->lexer);
11914       /* Warn that we do not support `export'.  */
11915       warning (0, "keyword %<export%> not implemented, and will be ignored");
11916     }
11917
11918   cp_parser_template_declaration_after_export (parser, member_p);
11919 }
11920
11921 /* Parse a template-parameter-list.
11922
11923    template-parameter-list:
11924      template-parameter
11925      template-parameter-list , template-parameter
11926
11927    Returns a TREE_LIST.  Each node represents a template parameter.
11928    The nodes are connected via their TREE_CHAINs.  */
11929
11930 static tree
11931 cp_parser_template_parameter_list (cp_parser* parser)
11932 {
11933   tree parameter_list = NULL_TREE;
11934
11935   begin_template_parm_list ();
11936
11937   /* The loop below parses the template parms.  We first need to know
11938      the total number of template parms to be able to compute proper
11939      canonical types of each dependent type. So after the loop, when
11940      we know the total number of template parms,
11941      end_template_parm_list computes the proper canonical types and
11942      fixes up the dependent types accordingly.  */
11943   while (true)
11944     {
11945       tree parameter;
11946       bool is_non_type;
11947       bool is_parameter_pack;
11948       location_t parm_loc;
11949
11950       /* Parse the template-parameter.  */
11951       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11952       parameter = cp_parser_template_parameter (parser, 
11953                                                 &is_non_type,
11954                                                 &is_parameter_pack);
11955       /* Add it to the list.  */
11956       if (parameter != error_mark_node)
11957         parameter_list = process_template_parm (parameter_list,
11958                                                 parm_loc,
11959                                                 parameter,
11960                                                 is_non_type,
11961                                                 is_parameter_pack,
11962                                                 0);
11963       else
11964        {
11965          tree err_parm = build_tree_list (parameter, parameter);
11966          parameter_list = chainon (parameter_list, err_parm);
11967        }
11968
11969       /* If the next token is not a `,', we're done.  */
11970       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11971         break;
11972       /* Otherwise, consume the `,' token.  */
11973       cp_lexer_consume_token (parser->lexer);
11974     }
11975
11976   return end_template_parm_list (parameter_list);
11977 }
11978
11979 /* Parse a template-parameter.
11980
11981    template-parameter:
11982      type-parameter
11983      parameter-declaration
11984
11985    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11986    the parameter.  The TREE_PURPOSE is the default value, if any.
11987    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11988    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11989    set to true iff this parameter is a parameter pack. */
11990
11991 static tree
11992 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11993                               bool *is_parameter_pack)
11994 {
11995   cp_token *token;
11996   cp_parameter_declarator *parameter_declarator;
11997   cp_declarator *id_declarator;
11998   tree parm;
11999
12000   /* Assume it is a type parameter or a template parameter.  */
12001   *is_non_type = false;
12002   /* Assume it not a parameter pack. */
12003   *is_parameter_pack = false;
12004   /* Peek at the next token.  */
12005   token = cp_lexer_peek_token (parser->lexer);
12006   /* If it is `class' or `template', we have a type-parameter.  */
12007   if (token->keyword == RID_TEMPLATE)
12008     return cp_parser_type_parameter (parser, is_parameter_pack);
12009   /* If it is `class' or `typename' we do not know yet whether it is a
12010      type parameter or a non-type parameter.  Consider:
12011
12012        template <typename T, typename T::X X> ...
12013
12014      or:
12015
12016        template <class C, class D*> ...
12017
12018      Here, the first parameter is a type parameter, and the second is
12019      a non-type parameter.  We can tell by looking at the token after
12020      the identifier -- if it is a `,', `=', or `>' then we have a type
12021      parameter.  */
12022   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12023     {
12024       /* Peek at the token after `class' or `typename'.  */
12025       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12026       /* If it's an ellipsis, we have a template type parameter
12027          pack. */
12028       if (token->type == CPP_ELLIPSIS)
12029         return cp_parser_type_parameter (parser, is_parameter_pack);
12030       /* If it's an identifier, skip it.  */
12031       if (token->type == CPP_NAME)
12032         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12033       /* Now, see if the token looks like the end of a template
12034          parameter.  */
12035       if (token->type == CPP_COMMA
12036           || token->type == CPP_EQ
12037           || token->type == CPP_GREATER)
12038         return cp_parser_type_parameter (parser, is_parameter_pack);
12039     }
12040
12041   /* Otherwise, it is a non-type parameter.
12042
12043      [temp.param]
12044
12045      When parsing a default template-argument for a non-type
12046      template-parameter, the first non-nested `>' is taken as the end
12047      of the template parameter-list rather than a greater-than
12048      operator.  */
12049   *is_non_type = true;
12050   parameter_declarator
12051      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12052                                         /*parenthesized_p=*/NULL);
12053
12054   /* If the parameter declaration is marked as a parameter pack, set
12055      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12056      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12057      grokdeclarator. */
12058   if (parameter_declarator
12059       && parameter_declarator->declarator
12060       && parameter_declarator->declarator->parameter_pack_p)
12061     {
12062       *is_parameter_pack = true;
12063       parameter_declarator->declarator->parameter_pack_p = false;
12064     }
12065
12066   /* If the next token is an ellipsis, and we don't already have it
12067      marked as a parameter pack, then we have a parameter pack (that
12068      has no declarator).  */
12069   if (!*is_parameter_pack
12070       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12071       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12072     {
12073       /* Consume the `...'.  */
12074       cp_lexer_consume_token (parser->lexer);
12075       maybe_warn_variadic_templates ();
12076       
12077       *is_parameter_pack = true;
12078     }
12079   /* We might end up with a pack expansion as the type of the non-type
12080      template parameter, in which case this is a non-type template
12081      parameter pack.  */
12082   else if (parameter_declarator
12083            && parameter_declarator->decl_specifiers.type
12084            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12085     {
12086       *is_parameter_pack = true;
12087       parameter_declarator->decl_specifiers.type = 
12088         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12089     }
12090
12091   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12092     {
12093       /* Parameter packs cannot have default arguments.  However, a
12094          user may try to do so, so we'll parse them and give an
12095          appropriate diagnostic here.  */
12096
12097       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12098       
12099       /* Find the name of the parameter pack.  */     
12100       id_declarator = parameter_declarator->declarator;
12101       while (id_declarator && id_declarator->kind != cdk_id)
12102         id_declarator = id_declarator->declarator;
12103       
12104       if (id_declarator && id_declarator->kind == cdk_id)
12105         error_at (start_token->location,
12106                   "template parameter pack %qD cannot have a default argument",
12107                   id_declarator->u.id.unqualified_name);
12108       else
12109         error_at (start_token->location,
12110                   "template parameter pack cannot have a default argument");
12111       
12112       /* Parse the default argument, but throw away the result.  */
12113       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12114     }
12115
12116   parm = grokdeclarator (parameter_declarator->declarator,
12117                          &parameter_declarator->decl_specifiers,
12118                          TPARM, /*initialized=*/0,
12119                          /*attrlist=*/NULL);
12120   if (parm == error_mark_node)
12121     return error_mark_node;
12122
12123   return build_tree_list (parameter_declarator->default_argument, parm);
12124 }
12125
12126 /* Parse a type-parameter.
12127
12128    type-parameter:
12129      class identifier [opt]
12130      class identifier [opt] = type-id
12131      typename identifier [opt]
12132      typename identifier [opt] = type-id
12133      template < template-parameter-list > class identifier [opt]
12134      template < template-parameter-list > class identifier [opt]
12135        = id-expression
12136
12137    GNU Extension (variadic templates):
12138
12139    type-parameter:
12140      class ... identifier [opt]
12141      typename ... identifier [opt]
12142
12143    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12144    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12145    the declaration of the parameter.
12146
12147    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12148
12149 static tree
12150 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12151 {
12152   cp_token *token;
12153   tree parameter;
12154
12155   /* Look for a keyword to tell us what kind of parameter this is.  */
12156   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12157   if (!token)
12158     return error_mark_node;
12159
12160   switch (token->keyword)
12161     {
12162     case RID_CLASS:
12163     case RID_TYPENAME:
12164       {
12165         tree identifier;
12166         tree default_argument;
12167
12168         /* If the next token is an ellipsis, we have a template
12169            argument pack. */
12170         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12171           {
12172             /* Consume the `...' token. */
12173             cp_lexer_consume_token (parser->lexer);
12174             maybe_warn_variadic_templates ();
12175
12176             *is_parameter_pack = true;
12177           }
12178
12179         /* If the next token is an identifier, then it names the
12180            parameter.  */
12181         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12182           identifier = cp_parser_identifier (parser);
12183         else
12184           identifier = NULL_TREE;
12185
12186         /* Create the parameter.  */
12187         parameter = finish_template_type_parm (class_type_node, identifier);
12188
12189         /* If the next token is an `=', we have a default argument.  */
12190         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12191           {
12192             /* Consume the `=' token.  */
12193             cp_lexer_consume_token (parser->lexer);
12194             /* Parse the default-argument.  */
12195             push_deferring_access_checks (dk_no_deferred);
12196             default_argument = cp_parser_type_id (parser);
12197
12198             /* Template parameter packs cannot have default
12199                arguments. */
12200             if (*is_parameter_pack)
12201               {
12202                 if (identifier)
12203                   error_at (token->location,
12204                             "template parameter pack %qD cannot have a "
12205                             "default argument", identifier);
12206                 else
12207                   error_at (token->location,
12208                             "template parameter packs cannot have "
12209                             "default arguments");
12210                 default_argument = NULL_TREE;
12211               }
12212             pop_deferring_access_checks ();
12213           }
12214         else
12215           default_argument = NULL_TREE;
12216
12217         /* Create the combined representation of the parameter and the
12218            default argument.  */
12219         parameter = build_tree_list (default_argument, parameter);
12220       }
12221       break;
12222
12223     case RID_TEMPLATE:
12224       {
12225         tree identifier;
12226         tree default_argument;
12227
12228         /* Look for the `<'.  */
12229         cp_parser_require (parser, CPP_LESS, RT_LESS);
12230         /* Parse the template-parameter-list.  */
12231         cp_parser_template_parameter_list (parser);
12232         /* Look for the `>'.  */
12233         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12234         /* Look for the `class' keyword.  */
12235         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12236         /* If the next token is an ellipsis, we have a template
12237            argument pack. */
12238         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12239           {
12240             /* Consume the `...' token. */
12241             cp_lexer_consume_token (parser->lexer);
12242             maybe_warn_variadic_templates ();
12243
12244             *is_parameter_pack = true;
12245           }
12246         /* If the next token is an `=', then there is a
12247            default-argument.  If the next token is a `>', we are at
12248            the end of the parameter-list.  If the next token is a `,',
12249            then we are at the end of this parameter.  */
12250         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12251             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12252             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12253           {
12254             identifier = cp_parser_identifier (parser);
12255             /* Treat invalid names as if the parameter were nameless.  */
12256             if (identifier == error_mark_node)
12257               identifier = NULL_TREE;
12258           }
12259         else
12260           identifier = NULL_TREE;
12261
12262         /* Create the template parameter.  */
12263         parameter = finish_template_template_parm (class_type_node,
12264                                                    identifier);
12265
12266         /* If the next token is an `=', then there is a
12267            default-argument.  */
12268         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12269           {
12270             bool is_template;
12271
12272             /* Consume the `='.  */
12273             cp_lexer_consume_token (parser->lexer);
12274             /* Parse the id-expression.  */
12275             push_deferring_access_checks (dk_no_deferred);
12276             /* save token before parsing the id-expression, for error
12277                reporting */
12278             token = cp_lexer_peek_token (parser->lexer);
12279             default_argument
12280               = cp_parser_id_expression (parser,
12281                                          /*template_keyword_p=*/false,
12282                                          /*check_dependency_p=*/true,
12283                                          /*template_p=*/&is_template,
12284                                          /*declarator_p=*/false,
12285                                          /*optional_p=*/false);
12286             if (TREE_CODE (default_argument) == TYPE_DECL)
12287               /* If the id-expression was a template-id that refers to
12288                  a template-class, we already have the declaration here,
12289                  so no further lookup is needed.  */
12290                  ;
12291             else
12292               /* Look up the name.  */
12293               default_argument
12294                 = cp_parser_lookup_name (parser, default_argument,
12295                                          none_type,
12296                                          /*is_template=*/is_template,
12297                                          /*is_namespace=*/false,
12298                                          /*check_dependency=*/true,
12299                                          /*ambiguous_decls=*/NULL,
12300                                          token->location);
12301             /* See if the default argument is valid.  */
12302             default_argument
12303               = check_template_template_default_arg (default_argument);
12304
12305             /* Template parameter packs cannot have default
12306                arguments. */
12307             if (*is_parameter_pack)
12308               {
12309                 if (identifier)
12310                   error_at (token->location,
12311                             "template parameter pack %qD cannot "
12312                             "have a default argument",
12313                             identifier);
12314                 else
12315                   error_at (token->location, "template parameter packs cannot "
12316                             "have default arguments");
12317                 default_argument = NULL_TREE;
12318               }
12319             pop_deferring_access_checks ();
12320           }
12321         else
12322           default_argument = NULL_TREE;
12323
12324         /* Create the combined representation of the parameter and the
12325            default argument.  */
12326         parameter = build_tree_list (default_argument, parameter);
12327       }
12328       break;
12329
12330     default:
12331       gcc_unreachable ();
12332       break;
12333     }
12334
12335   return parameter;
12336 }
12337
12338 /* Parse a template-id.
12339
12340    template-id:
12341      template-name < template-argument-list [opt] >
12342
12343    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12344    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12345    returned.  Otherwise, if the template-name names a function, or set
12346    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12347    names a class, returns a TYPE_DECL for the specialization.
12348
12349    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12350    uninstantiated templates.  */
12351
12352 static tree
12353 cp_parser_template_id (cp_parser *parser,
12354                        bool template_keyword_p,
12355                        bool check_dependency_p,
12356                        bool is_declaration)
12357 {
12358   int i;
12359   tree templ;
12360   tree arguments;
12361   tree template_id;
12362   cp_token_position start_of_id = 0;
12363   deferred_access_check *chk;
12364   VEC (deferred_access_check,gc) *access_check;
12365   cp_token *next_token = NULL, *next_token_2 = NULL;
12366   bool is_identifier;
12367
12368   /* If the next token corresponds to a template-id, there is no need
12369      to reparse it.  */
12370   next_token = cp_lexer_peek_token (parser->lexer);
12371   if (next_token->type == CPP_TEMPLATE_ID)
12372     {
12373       struct tree_check *check_value;
12374
12375       /* Get the stored value.  */
12376       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12377       /* Perform any access checks that were deferred.  */
12378       access_check = check_value->checks;
12379       if (access_check)
12380         {
12381           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12382             perform_or_defer_access_check (chk->binfo,
12383                                            chk->decl,
12384                                            chk->diag_decl);
12385         }
12386       /* Return the stored value.  */
12387       return check_value->value;
12388     }
12389
12390   /* Avoid performing name lookup if there is no possibility of
12391      finding a template-id.  */
12392   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12393       || (next_token->type == CPP_NAME
12394           && !cp_parser_nth_token_starts_template_argument_list_p
12395                (parser, 2)))
12396     {
12397       cp_parser_error (parser, "expected template-id");
12398       return error_mark_node;
12399     }
12400
12401   /* Remember where the template-id starts.  */
12402   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12403     start_of_id = cp_lexer_token_position (parser->lexer, false);
12404
12405   push_deferring_access_checks (dk_deferred);
12406
12407   /* Parse the template-name.  */
12408   is_identifier = false;
12409   templ = cp_parser_template_name (parser, template_keyword_p,
12410                                    check_dependency_p,
12411                                    is_declaration,
12412                                    &is_identifier);
12413   if (templ == error_mark_node || is_identifier)
12414     {
12415       pop_deferring_access_checks ();
12416       return templ;
12417     }
12418
12419   /* If we find the sequence `[:' after a template-name, it's probably
12420      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12421      parse correctly the argument list.  */
12422   next_token = cp_lexer_peek_token (parser->lexer);
12423   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12424   if (next_token->type == CPP_OPEN_SQUARE
12425       && next_token->flags & DIGRAPH
12426       && next_token_2->type == CPP_COLON
12427       && !(next_token_2->flags & PREV_WHITE))
12428     {
12429       cp_parser_parse_tentatively (parser);
12430       /* Change `:' into `::'.  */
12431       next_token_2->type = CPP_SCOPE;
12432       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12433          CPP_LESS.  */
12434       cp_lexer_consume_token (parser->lexer);
12435
12436       /* Parse the arguments.  */
12437       arguments = cp_parser_enclosed_template_argument_list (parser);
12438       if (!cp_parser_parse_definitely (parser))
12439         {
12440           /* If we couldn't parse an argument list, then we revert our changes
12441              and return simply an error. Maybe this is not a template-id
12442              after all.  */
12443           next_token_2->type = CPP_COLON;
12444           cp_parser_error (parser, "expected %<<%>");
12445           pop_deferring_access_checks ();
12446           return error_mark_node;
12447         }
12448       /* Otherwise, emit an error about the invalid digraph, but continue
12449          parsing because we got our argument list.  */
12450       if (permerror (next_token->location,
12451                      "%<<::%> cannot begin a template-argument list"))
12452         {
12453           static bool hint = false;
12454           inform (next_token->location,
12455                   "%<<:%> is an alternate spelling for %<[%>."
12456                   " Insert whitespace between %<<%> and %<::%>");
12457           if (!hint && !flag_permissive)
12458             {
12459               inform (next_token->location, "(if you use %<-fpermissive%>"
12460                       " G++ will accept your code)");
12461               hint = true;
12462             }
12463         }
12464     }
12465   else
12466     {
12467       /* Look for the `<' that starts the template-argument-list.  */
12468       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12469         {
12470           pop_deferring_access_checks ();
12471           return error_mark_node;
12472         }
12473       /* Parse the arguments.  */
12474       arguments = cp_parser_enclosed_template_argument_list (parser);
12475     }
12476
12477   /* Build a representation of the specialization.  */
12478   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12479     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12480   else if (DECL_TYPE_TEMPLATE_P (templ)
12481            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12482     {
12483       bool entering_scope;
12484       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12485          template (rather than some instantiation thereof) only if
12486          is not nested within some other construct.  For example, in
12487          "template <typename T> void f(T) { A<T>::", A<T> is just an
12488          instantiation of A.  */
12489       entering_scope = (template_parm_scope_p ()
12490                         && cp_lexer_next_token_is (parser->lexer,
12491                                                    CPP_SCOPE));
12492       template_id
12493         = finish_template_type (templ, arguments, entering_scope);
12494     }
12495   else
12496     {
12497       /* If it's not a class-template or a template-template, it should be
12498          a function-template.  */
12499       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12500                    || TREE_CODE (templ) == OVERLOAD
12501                    || BASELINK_P (templ)));
12502
12503       template_id = lookup_template_function (templ, arguments);
12504     }
12505
12506   /* If parsing tentatively, replace the sequence of tokens that makes
12507      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12508      should we re-parse the token stream, we will not have to repeat
12509      the effort required to do the parse, nor will we issue duplicate
12510      error messages about problems during instantiation of the
12511      template.  */
12512   if (start_of_id)
12513     {
12514       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12515
12516       /* Reset the contents of the START_OF_ID token.  */
12517       token->type = CPP_TEMPLATE_ID;
12518       /* Retrieve any deferred checks.  Do not pop this access checks yet
12519          so the memory will not be reclaimed during token replacing below.  */
12520       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12521       token->u.tree_check_value->value = template_id;
12522       token->u.tree_check_value->checks = get_deferred_access_checks ();
12523       token->keyword = RID_MAX;
12524
12525       /* Purge all subsequent tokens.  */
12526       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12527
12528       /* ??? Can we actually assume that, if template_id ==
12529          error_mark_node, we will have issued a diagnostic to the
12530          user, as opposed to simply marking the tentative parse as
12531          failed?  */
12532       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12533         error_at (token->location, "parse error in template argument list");
12534     }
12535
12536   pop_deferring_access_checks ();
12537   return template_id;
12538 }
12539
12540 /* Parse a template-name.
12541
12542    template-name:
12543      identifier
12544
12545    The standard should actually say:
12546
12547    template-name:
12548      identifier
12549      operator-function-id
12550
12551    A defect report has been filed about this issue.
12552
12553    A conversion-function-id cannot be a template name because they cannot
12554    be part of a template-id. In fact, looking at this code:
12555
12556    a.operator K<int>()
12557
12558    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12559    It is impossible to call a templated conversion-function-id with an
12560    explicit argument list, since the only allowed template parameter is
12561    the type to which it is converting.
12562
12563    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12564    `template' keyword, in a construction like:
12565
12566      T::template f<3>()
12567
12568    In that case `f' is taken to be a template-name, even though there
12569    is no way of knowing for sure.
12570
12571    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12572    name refers to a set of overloaded functions, at least one of which
12573    is a template, or an IDENTIFIER_NODE with the name of the template,
12574    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12575    names are looked up inside uninstantiated templates.  */
12576
12577 static tree
12578 cp_parser_template_name (cp_parser* parser,
12579                          bool template_keyword_p,
12580                          bool check_dependency_p,
12581                          bool is_declaration,
12582                          bool *is_identifier)
12583 {
12584   tree identifier;
12585   tree decl;
12586   tree fns;
12587   cp_token *token = cp_lexer_peek_token (parser->lexer);
12588
12589   /* If the next token is `operator', then we have either an
12590      operator-function-id or a conversion-function-id.  */
12591   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12592     {
12593       /* We don't know whether we're looking at an
12594          operator-function-id or a conversion-function-id.  */
12595       cp_parser_parse_tentatively (parser);
12596       /* Try an operator-function-id.  */
12597       identifier = cp_parser_operator_function_id (parser);
12598       /* If that didn't work, try a conversion-function-id.  */
12599       if (!cp_parser_parse_definitely (parser))
12600         {
12601           cp_parser_error (parser, "expected template-name");
12602           return error_mark_node;
12603         }
12604     }
12605   /* Look for the identifier.  */
12606   else
12607     identifier = cp_parser_identifier (parser);
12608
12609   /* If we didn't find an identifier, we don't have a template-id.  */
12610   if (identifier == error_mark_node)
12611     return error_mark_node;
12612
12613   /* If the name immediately followed the `template' keyword, then it
12614      is a template-name.  However, if the next token is not `<', then
12615      we do not treat it as a template-name, since it is not being used
12616      as part of a template-id.  This enables us to handle constructs
12617      like:
12618
12619        template <typename T> struct S { S(); };
12620        template <typename T> S<T>::S();
12621
12622      correctly.  We would treat `S' as a template -- if it were `S<T>'
12623      -- but we do not if there is no `<'.  */
12624
12625   if (processing_template_decl
12626       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12627     {
12628       /* In a declaration, in a dependent context, we pretend that the
12629          "template" keyword was present in order to improve error
12630          recovery.  For example, given:
12631
12632            template <typename T> void f(T::X<int>);
12633
12634          we want to treat "X<int>" as a template-id.  */
12635       if (is_declaration
12636           && !template_keyword_p
12637           && parser->scope && TYPE_P (parser->scope)
12638           && check_dependency_p
12639           && dependent_scope_p (parser->scope)
12640           /* Do not do this for dtors (or ctors), since they never
12641              need the template keyword before their name.  */
12642           && !constructor_name_p (identifier, parser->scope))
12643         {
12644           cp_token_position start = 0;
12645
12646           /* Explain what went wrong.  */
12647           error_at (token->location, "non-template %qD used as template",
12648                     identifier);
12649           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12650                   parser->scope, identifier);
12651           /* If parsing tentatively, find the location of the "<" token.  */
12652           if (cp_parser_simulate_error (parser))
12653             start = cp_lexer_token_position (parser->lexer, true);
12654           /* Parse the template arguments so that we can issue error
12655              messages about them.  */
12656           cp_lexer_consume_token (parser->lexer);
12657           cp_parser_enclosed_template_argument_list (parser);
12658           /* Skip tokens until we find a good place from which to
12659              continue parsing.  */
12660           cp_parser_skip_to_closing_parenthesis (parser,
12661                                                  /*recovering=*/true,
12662                                                  /*or_comma=*/true,
12663                                                  /*consume_paren=*/false);
12664           /* If parsing tentatively, permanently remove the
12665              template argument list.  That will prevent duplicate
12666              error messages from being issued about the missing
12667              "template" keyword.  */
12668           if (start)
12669             cp_lexer_purge_tokens_after (parser->lexer, start);
12670           if (is_identifier)
12671             *is_identifier = true;
12672           return identifier;
12673         }
12674
12675       /* If the "template" keyword is present, then there is generally
12676          no point in doing name-lookup, so we just return IDENTIFIER.
12677          But, if the qualifying scope is non-dependent then we can
12678          (and must) do name-lookup normally.  */
12679       if (template_keyword_p
12680           && (!parser->scope
12681               || (TYPE_P (parser->scope)
12682                   && dependent_type_p (parser->scope))))
12683         return identifier;
12684     }
12685
12686   /* Look up the name.  */
12687   decl = cp_parser_lookup_name (parser, identifier,
12688                                 none_type,
12689                                 /*is_template=*/true,
12690                                 /*is_namespace=*/false,
12691                                 check_dependency_p,
12692                                 /*ambiguous_decls=*/NULL,
12693                                 token->location);
12694
12695   /* If DECL is a template, then the name was a template-name.  */
12696   if (TREE_CODE (decl) == TEMPLATE_DECL)
12697     ;
12698   else
12699     {
12700       tree fn = NULL_TREE;
12701
12702       /* The standard does not explicitly indicate whether a name that
12703          names a set of overloaded declarations, some of which are
12704          templates, is a template-name.  However, such a name should
12705          be a template-name; otherwise, there is no way to form a
12706          template-id for the overloaded templates.  */
12707       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12708       if (TREE_CODE (fns) == OVERLOAD)
12709         for (fn = fns; fn; fn = OVL_NEXT (fn))
12710           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12711             break;
12712
12713       if (!fn)
12714         {
12715           /* The name does not name a template.  */
12716           cp_parser_error (parser, "expected template-name");
12717           return error_mark_node;
12718         }
12719     }
12720
12721   /* If DECL is dependent, and refers to a function, then just return
12722      its name; we will look it up again during template instantiation.  */
12723   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12724     {
12725       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12726       if (TYPE_P (scope) && dependent_type_p (scope))
12727         return identifier;
12728     }
12729
12730   return decl;
12731 }
12732
12733 /* Parse a template-argument-list.
12734
12735    template-argument-list:
12736      template-argument ... [opt]
12737      template-argument-list , template-argument ... [opt]
12738
12739    Returns a TREE_VEC containing the arguments.  */
12740
12741 static tree
12742 cp_parser_template_argument_list (cp_parser* parser)
12743 {
12744   tree fixed_args[10];
12745   unsigned n_args = 0;
12746   unsigned alloced = 10;
12747   tree *arg_ary = fixed_args;
12748   tree vec;
12749   bool saved_in_template_argument_list_p;
12750   bool saved_ice_p;
12751   bool saved_non_ice_p;
12752
12753   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12754   parser->in_template_argument_list_p = true;
12755   /* Even if the template-id appears in an integral
12756      constant-expression, the contents of the argument list do
12757      not.  */
12758   saved_ice_p = parser->integral_constant_expression_p;
12759   parser->integral_constant_expression_p = false;
12760   saved_non_ice_p = parser->non_integral_constant_expression_p;
12761   parser->non_integral_constant_expression_p = false;
12762
12763   /* Parse the arguments.  */
12764   do
12765     {
12766       tree argument;
12767
12768       if (n_args)
12769         /* Consume the comma.  */
12770         cp_lexer_consume_token (parser->lexer);
12771
12772       /* Parse the template-argument.  */
12773       argument = cp_parser_template_argument (parser);
12774
12775       /* If the next token is an ellipsis, we're expanding a template
12776          argument pack. */
12777       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12778         {
12779           if (argument == error_mark_node)
12780             {
12781               cp_token *token = cp_lexer_peek_token (parser->lexer);
12782               error_at (token->location,
12783                         "expected parameter pack before %<...%>");
12784             }
12785           /* Consume the `...' token. */
12786           cp_lexer_consume_token (parser->lexer);
12787
12788           /* Make the argument into a TYPE_PACK_EXPANSION or
12789              EXPR_PACK_EXPANSION. */
12790           argument = make_pack_expansion (argument);
12791         }
12792
12793       if (n_args == alloced)
12794         {
12795           alloced *= 2;
12796
12797           if (arg_ary == fixed_args)
12798             {
12799               arg_ary = XNEWVEC (tree, alloced);
12800               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12801             }
12802           else
12803             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12804         }
12805       arg_ary[n_args++] = argument;
12806     }
12807   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12808
12809   vec = make_tree_vec (n_args);
12810
12811   while (n_args--)
12812     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12813
12814   if (arg_ary != fixed_args)
12815     free (arg_ary);
12816   parser->non_integral_constant_expression_p = saved_non_ice_p;
12817   parser->integral_constant_expression_p = saved_ice_p;
12818   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12819 #ifdef ENABLE_CHECKING
12820   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12821 #endif
12822   return vec;
12823 }
12824
12825 /* Parse a template-argument.
12826
12827    template-argument:
12828      assignment-expression
12829      type-id
12830      id-expression
12831
12832    The representation is that of an assignment-expression, type-id, or
12833    id-expression -- except that the qualified id-expression is
12834    evaluated, so that the value returned is either a DECL or an
12835    OVERLOAD.
12836
12837    Although the standard says "assignment-expression", it forbids
12838    throw-expressions or assignments in the template argument.
12839    Therefore, we use "conditional-expression" instead.  */
12840
12841 static tree
12842 cp_parser_template_argument (cp_parser* parser)
12843 {
12844   tree argument;
12845   bool template_p;
12846   bool address_p;
12847   bool maybe_type_id = false;
12848   cp_token *token = NULL, *argument_start_token = NULL;
12849   cp_id_kind idk;
12850
12851   /* There's really no way to know what we're looking at, so we just
12852      try each alternative in order.
12853
12854        [temp.arg]
12855
12856        In a template-argument, an ambiguity between a type-id and an
12857        expression is resolved to a type-id, regardless of the form of
12858        the corresponding template-parameter.
12859
12860      Therefore, we try a type-id first.  */
12861   cp_parser_parse_tentatively (parser);
12862   argument = cp_parser_template_type_arg (parser);
12863   /* If there was no error parsing the type-id but the next token is a
12864      '>>', our behavior depends on which dialect of C++ we're
12865      parsing. In C++98, we probably found a typo for '> >'. But there
12866      are type-id which are also valid expressions. For instance:
12867
12868      struct X { int operator >> (int); };
12869      template <int V> struct Foo {};
12870      Foo<X () >> 5> r;
12871
12872      Here 'X()' is a valid type-id of a function type, but the user just
12873      wanted to write the expression "X() >> 5". Thus, we remember that we
12874      found a valid type-id, but we still try to parse the argument as an
12875      expression to see what happens. 
12876
12877      In C++0x, the '>>' will be considered two separate '>'
12878      tokens.  */
12879   if (!cp_parser_error_occurred (parser)
12880       && cxx_dialect == cxx98
12881       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12882     {
12883       maybe_type_id = true;
12884       cp_parser_abort_tentative_parse (parser);
12885     }
12886   else
12887     {
12888       /* If the next token isn't a `,' or a `>', then this argument wasn't
12889       really finished. This means that the argument is not a valid
12890       type-id.  */
12891       if (!cp_parser_next_token_ends_template_argument_p (parser))
12892         cp_parser_error (parser, "expected template-argument");
12893       /* If that worked, we're done.  */
12894       if (cp_parser_parse_definitely (parser))
12895         return argument;
12896     }
12897   /* We're still not sure what the argument will be.  */
12898   cp_parser_parse_tentatively (parser);
12899   /* Try a template.  */
12900   argument_start_token = cp_lexer_peek_token (parser->lexer);
12901   argument = cp_parser_id_expression (parser,
12902                                       /*template_keyword_p=*/false,
12903                                       /*check_dependency_p=*/true,
12904                                       &template_p,
12905                                       /*declarator_p=*/false,
12906                                       /*optional_p=*/false);
12907   /* If the next token isn't a `,' or a `>', then this argument wasn't
12908      really finished.  */
12909   if (!cp_parser_next_token_ends_template_argument_p (parser))
12910     cp_parser_error (parser, "expected template-argument");
12911   if (!cp_parser_error_occurred (parser))
12912     {
12913       /* Figure out what is being referred to.  If the id-expression
12914          was for a class template specialization, then we will have a
12915          TYPE_DECL at this point.  There is no need to do name lookup
12916          at this point in that case.  */
12917       if (TREE_CODE (argument) != TYPE_DECL)
12918         argument = cp_parser_lookup_name (parser, argument,
12919                                           none_type,
12920                                           /*is_template=*/template_p,
12921                                           /*is_namespace=*/false,
12922                                           /*check_dependency=*/true,
12923                                           /*ambiguous_decls=*/NULL,
12924                                           argument_start_token->location);
12925       if (TREE_CODE (argument) != TEMPLATE_DECL
12926           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12927         cp_parser_error (parser, "expected template-name");
12928     }
12929   if (cp_parser_parse_definitely (parser))
12930     return argument;
12931   /* It must be a non-type argument.  There permitted cases are given
12932      in [temp.arg.nontype]:
12933
12934      -- an integral constant-expression of integral or enumeration
12935         type; or
12936
12937      -- the name of a non-type template-parameter; or
12938
12939      -- the name of an object or function with external linkage...
12940
12941      -- the address of an object or function with external linkage...
12942
12943      -- a pointer to member...  */
12944   /* Look for a non-type template parameter.  */
12945   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12946     {
12947       cp_parser_parse_tentatively (parser);
12948       argument = cp_parser_primary_expression (parser,
12949                                                /*address_p=*/false,
12950                                                /*cast_p=*/false,
12951                                                /*template_arg_p=*/true,
12952                                                &idk);
12953       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12954           || !cp_parser_next_token_ends_template_argument_p (parser))
12955         cp_parser_simulate_error (parser);
12956       if (cp_parser_parse_definitely (parser))
12957         return argument;
12958     }
12959
12960   /* If the next token is "&", the argument must be the address of an
12961      object or function with external linkage.  */
12962   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12963   if (address_p)
12964     cp_lexer_consume_token (parser->lexer);
12965   /* See if we might have an id-expression.  */
12966   token = cp_lexer_peek_token (parser->lexer);
12967   if (token->type == CPP_NAME
12968       || token->keyword == RID_OPERATOR
12969       || token->type == CPP_SCOPE
12970       || token->type == CPP_TEMPLATE_ID
12971       || token->type == CPP_NESTED_NAME_SPECIFIER)
12972     {
12973       cp_parser_parse_tentatively (parser);
12974       argument = cp_parser_primary_expression (parser,
12975                                                address_p,
12976                                                /*cast_p=*/false,
12977                                                /*template_arg_p=*/true,
12978                                                &idk);
12979       if (cp_parser_error_occurred (parser)
12980           || !cp_parser_next_token_ends_template_argument_p (parser))
12981         cp_parser_abort_tentative_parse (parser);
12982       else
12983         {
12984           tree probe;
12985
12986           if (TREE_CODE (argument) == INDIRECT_REF)
12987             {
12988               gcc_assert (REFERENCE_REF_P (argument));
12989               argument = TREE_OPERAND (argument, 0);
12990             }
12991
12992           /* If we're in a template, we represent a qualified-id referring
12993              to a static data member as a SCOPE_REF even if the scope isn't
12994              dependent so that we can check access control later.  */
12995           probe = argument;
12996           if (TREE_CODE (probe) == SCOPE_REF)
12997             probe = TREE_OPERAND (probe, 1);
12998           if (TREE_CODE (probe) == VAR_DECL)
12999             {
13000               /* A variable without external linkage might still be a
13001                  valid constant-expression, so no error is issued here
13002                  if the external-linkage check fails.  */
13003               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13004                 cp_parser_simulate_error (parser);
13005             }
13006           else if (is_overloaded_fn (argument))
13007             /* All overloaded functions are allowed; if the external
13008                linkage test does not pass, an error will be issued
13009                later.  */
13010             ;
13011           else if (address_p
13012                    && (TREE_CODE (argument) == OFFSET_REF
13013                        || TREE_CODE (argument) == SCOPE_REF))
13014             /* A pointer-to-member.  */
13015             ;
13016           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13017             ;
13018           else
13019             cp_parser_simulate_error (parser);
13020
13021           if (cp_parser_parse_definitely (parser))
13022             {
13023               if (address_p)
13024                 argument = build_x_unary_op (ADDR_EXPR, argument,
13025                                              tf_warning_or_error);
13026               return argument;
13027             }
13028         }
13029     }
13030   /* If the argument started with "&", there are no other valid
13031      alternatives at this point.  */
13032   if (address_p)
13033     {
13034       cp_parser_error (parser, "invalid non-type template argument");
13035       return error_mark_node;
13036     }
13037
13038   /* If the argument wasn't successfully parsed as a type-id followed
13039      by '>>', the argument can only be a constant expression now.
13040      Otherwise, we try parsing the constant-expression tentatively,
13041      because the argument could really be a type-id.  */
13042   if (maybe_type_id)
13043     cp_parser_parse_tentatively (parser);
13044   argument = cp_parser_constant_expression (parser,
13045                                             /*allow_non_constant_p=*/false,
13046                                             /*non_constant_p=*/NULL);
13047   argument = fold_non_dependent_expr (argument);
13048   if (!maybe_type_id)
13049     return argument;
13050   if (!cp_parser_next_token_ends_template_argument_p (parser))
13051     cp_parser_error (parser, "expected template-argument");
13052   if (cp_parser_parse_definitely (parser))
13053     return argument;
13054   /* We did our best to parse the argument as a non type-id, but that
13055      was the only alternative that matched (albeit with a '>' after
13056      it). We can assume it's just a typo from the user, and a
13057      diagnostic will then be issued.  */
13058   return cp_parser_template_type_arg (parser);
13059 }
13060
13061 /* Parse an explicit-instantiation.
13062
13063    explicit-instantiation:
13064      template declaration
13065
13066    Although the standard says `declaration', what it really means is:
13067
13068    explicit-instantiation:
13069      template decl-specifier-seq [opt] declarator [opt] ;
13070
13071    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13072    supposed to be allowed.  A defect report has been filed about this
13073    issue.
13074
13075    GNU Extension:
13076
13077    explicit-instantiation:
13078      storage-class-specifier template
13079        decl-specifier-seq [opt] declarator [opt] ;
13080      function-specifier template
13081        decl-specifier-seq [opt] declarator [opt] ;  */
13082
13083 static void
13084 cp_parser_explicit_instantiation (cp_parser* parser)
13085 {
13086   int declares_class_or_enum;
13087   cp_decl_specifier_seq decl_specifiers;
13088   tree extension_specifier = NULL_TREE;
13089
13090   timevar_push (TV_TEMPLATE_INST);
13091
13092   /* Look for an (optional) storage-class-specifier or
13093      function-specifier.  */
13094   if (cp_parser_allow_gnu_extensions_p (parser))
13095     {
13096       extension_specifier
13097         = cp_parser_storage_class_specifier_opt (parser);
13098       if (!extension_specifier)
13099         extension_specifier
13100           = cp_parser_function_specifier_opt (parser,
13101                                               /*decl_specs=*/NULL);
13102     }
13103
13104   /* Look for the `template' keyword.  */
13105   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13106   /* Let the front end know that we are processing an explicit
13107      instantiation.  */
13108   begin_explicit_instantiation ();
13109   /* [temp.explicit] says that we are supposed to ignore access
13110      control while processing explicit instantiation directives.  */
13111   push_deferring_access_checks (dk_no_check);
13112   /* Parse a decl-specifier-seq.  */
13113   cp_parser_decl_specifier_seq (parser,
13114                                 CP_PARSER_FLAGS_OPTIONAL,
13115                                 &decl_specifiers,
13116                                 &declares_class_or_enum);
13117   /* If there was exactly one decl-specifier, and it declared a class,
13118      and there's no declarator, then we have an explicit type
13119      instantiation.  */
13120   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13121     {
13122       tree type;
13123
13124       type = check_tag_decl (&decl_specifiers);
13125       /* Turn access control back on for names used during
13126          template instantiation.  */
13127       pop_deferring_access_checks ();
13128       if (type)
13129         do_type_instantiation (type, extension_specifier,
13130                                /*complain=*/tf_error);
13131     }
13132   else
13133     {
13134       cp_declarator *declarator;
13135       tree decl;
13136
13137       /* Parse the declarator.  */
13138       declarator
13139         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13140                                 /*ctor_dtor_or_conv_p=*/NULL,
13141                                 /*parenthesized_p=*/NULL,
13142                                 /*member_p=*/false);
13143       if (declares_class_or_enum & 2)
13144         cp_parser_check_for_definition_in_return_type (declarator,
13145                                                        decl_specifiers.type,
13146                                                        decl_specifiers.type_location);
13147       if (declarator != cp_error_declarator)
13148         {
13149           if (decl_specifiers.specs[(int)ds_inline])
13150             permerror (input_location, "explicit instantiation shall not use"
13151                        " %<inline%> specifier");
13152           if (decl_specifiers.specs[(int)ds_constexpr])
13153             permerror (input_location, "explicit instantiation shall not use"
13154                        " %<constexpr%> specifier");
13155
13156           decl = grokdeclarator (declarator, &decl_specifiers,
13157                                  NORMAL, 0, &decl_specifiers.attributes);
13158           /* Turn access control back on for names used during
13159              template instantiation.  */
13160           pop_deferring_access_checks ();
13161           /* Do the explicit instantiation.  */
13162           do_decl_instantiation (decl, extension_specifier);
13163         }
13164       else
13165         {
13166           pop_deferring_access_checks ();
13167           /* Skip the body of the explicit instantiation.  */
13168           cp_parser_skip_to_end_of_statement (parser);
13169         }
13170     }
13171   /* We're done with the instantiation.  */
13172   end_explicit_instantiation ();
13173
13174   cp_parser_consume_semicolon_at_end_of_statement (parser);
13175
13176   timevar_pop (TV_TEMPLATE_INST);
13177 }
13178
13179 /* Parse an explicit-specialization.
13180
13181    explicit-specialization:
13182      template < > declaration
13183
13184    Although the standard says `declaration', what it really means is:
13185
13186    explicit-specialization:
13187      template <> decl-specifier [opt] init-declarator [opt] ;
13188      template <> function-definition
13189      template <> explicit-specialization
13190      template <> template-declaration  */
13191
13192 static void
13193 cp_parser_explicit_specialization (cp_parser* parser)
13194 {
13195   bool need_lang_pop;
13196   cp_token *token = cp_lexer_peek_token (parser->lexer);
13197
13198   /* Look for the `template' keyword.  */
13199   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13200   /* Look for the `<'.  */
13201   cp_parser_require (parser, CPP_LESS, RT_LESS);
13202   /* Look for the `>'.  */
13203   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13204   /* We have processed another parameter list.  */
13205   ++parser->num_template_parameter_lists;
13206   /* [temp]
13207
13208      A template ... explicit specialization ... shall not have C
13209      linkage.  */
13210   if (current_lang_name == lang_name_c)
13211     {
13212       error_at (token->location, "template specialization with C linkage");
13213       /* Give it C++ linkage to avoid confusing other parts of the
13214          front end.  */
13215       push_lang_context (lang_name_cplusplus);
13216       need_lang_pop = true;
13217     }
13218   else
13219     need_lang_pop = false;
13220   /* Let the front end know that we are beginning a specialization.  */
13221   if (!begin_specialization ())
13222     {
13223       end_specialization ();
13224       return;
13225     }
13226
13227   /* If the next keyword is `template', we need to figure out whether
13228      or not we're looking a template-declaration.  */
13229   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13230     {
13231       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13232           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13233         cp_parser_template_declaration_after_export (parser,
13234                                                      /*member_p=*/false);
13235       else
13236         cp_parser_explicit_specialization (parser);
13237     }
13238   else
13239     /* Parse the dependent declaration.  */
13240     cp_parser_single_declaration (parser,
13241                                   /*checks=*/NULL,
13242                                   /*member_p=*/false,
13243                                   /*explicit_specialization_p=*/true,
13244                                   /*friend_p=*/NULL);
13245   /* We're done with the specialization.  */
13246   end_specialization ();
13247   /* For the erroneous case of a template with C linkage, we pushed an
13248      implicit C++ linkage scope; exit that scope now.  */
13249   if (need_lang_pop)
13250     pop_lang_context ();
13251   /* We're done with this parameter list.  */
13252   --parser->num_template_parameter_lists;
13253 }
13254
13255 /* Parse a type-specifier.
13256
13257    type-specifier:
13258      simple-type-specifier
13259      class-specifier
13260      enum-specifier
13261      elaborated-type-specifier
13262      cv-qualifier
13263
13264    GNU Extension:
13265
13266    type-specifier:
13267      __complex__
13268
13269    Returns a representation of the type-specifier.  For a
13270    class-specifier, enum-specifier, or elaborated-type-specifier, a
13271    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13272
13273    The parser flags FLAGS is used to control type-specifier parsing.
13274
13275    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13276    in a decl-specifier-seq.
13277
13278    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13279    class-specifier, enum-specifier, or elaborated-type-specifier, then
13280    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13281    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13282    zero.
13283
13284    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13285    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13286    is set to FALSE.  */
13287
13288 static tree
13289 cp_parser_type_specifier (cp_parser* parser,
13290                           cp_parser_flags flags,
13291                           cp_decl_specifier_seq *decl_specs,
13292                           bool is_declaration,
13293                           int* declares_class_or_enum,
13294                           bool* is_cv_qualifier)
13295 {
13296   tree type_spec = NULL_TREE;
13297   cp_token *token;
13298   enum rid keyword;
13299   cp_decl_spec ds = ds_last;
13300
13301   /* Assume this type-specifier does not declare a new type.  */
13302   if (declares_class_or_enum)
13303     *declares_class_or_enum = 0;
13304   /* And that it does not specify a cv-qualifier.  */
13305   if (is_cv_qualifier)
13306     *is_cv_qualifier = false;
13307   /* Peek at the next token.  */
13308   token = cp_lexer_peek_token (parser->lexer);
13309
13310   /* If we're looking at a keyword, we can use that to guide the
13311      production we choose.  */
13312   keyword = token->keyword;
13313   switch (keyword)
13314     {
13315     case RID_ENUM:
13316       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13317         goto elaborated_type_specifier;
13318
13319       /* Look for the enum-specifier.  */
13320       type_spec = cp_parser_enum_specifier (parser);
13321       /* If that worked, we're done.  */
13322       if (type_spec)
13323         {
13324           if (declares_class_or_enum)
13325             *declares_class_or_enum = 2;
13326           if (decl_specs)
13327             cp_parser_set_decl_spec_type (decl_specs,
13328                                           type_spec,
13329                                           token->location,
13330                                           /*type_definition_p=*/true);
13331           return type_spec;
13332         }
13333       else
13334         goto elaborated_type_specifier;
13335
13336       /* Any of these indicate either a class-specifier, or an
13337          elaborated-type-specifier.  */
13338     case RID_CLASS:
13339     case RID_STRUCT:
13340     case RID_UNION:
13341       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13342         goto elaborated_type_specifier;
13343
13344       /* Parse tentatively so that we can back up if we don't find a
13345          class-specifier.  */
13346       cp_parser_parse_tentatively (parser);
13347       /* Look for the class-specifier.  */
13348       type_spec = cp_parser_class_specifier (parser);
13349       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13350       /* If that worked, we're done.  */
13351       if (cp_parser_parse_definitely (parser))
13352         {
13353           if (declares_class_or_enum)
13354             *declares_class_or_enum = 2;
13355           if (decl_specs)
13356             cp_parser_set_decl_spec_type (decl_specs,
13357                                           type_spec,
13358                                           token->location,
13359                                           /*type_definition_p=*/true);
13360           return type_spec;
13361         }
13362
13363       /* Fall through.  */
13364     elaborated_type_specifier:
13365       /* We're declaring (not defining) a class or enum.  */
13366       if (declares_class_or_enum)
13367         *declares_class_or_enum = 1;
13368
13369       /* Fall through.  */
13370     case RID_TYPENAME:
13371       /* Look for an elaborated-type-specifier.  */
13372       type_spec
13373         = (cp_parser_elaborated_type_specifier
13374            (parser,
13375             decl_specs && decl_specs->specs[(int) ds_friend],
13376             is_declaration));
13377       if (decl_specs)
13378         cp_parser_set_decl_spec_type (decl_specs,
13379                                       type_spec,
13380                                       token->location,
13381                                       /*type_definition_p=*/false);
13382       return type_spec;
13383
13384     case RID_CONST:
13385       ds = ds_const;
13386       if (is_cv_qualifier)
13387         *is_cv_qualifier = true;
13388       break;
13389
13390     case RID_VOLATILE:
13391       ds = ds_volatile;
13392       if (is_cv_qualifier)
13393         *is_cv_qualifier = true;
13394       break;
13395
13396     case RID_RESTRICT:
13397       ds = ds_restrict;
13398       if (is_cv_qualifier)
13399         *is_cv_qualifier = true;
13400       break;
13401
13402     case RID_COMPLEX:
13403       /* The `__complex__' keyword is a GNU extension.  */
13404       ds = ds_complex;
13405       break;
13406
13407     default:
13408       break;
13409     }
13410
13411   /* Handle simple keywords.  */
13412   if (ds != ds_last)
13413     {
13414       if (decl_specs)
13415         {
13416           ++decl_specs->specs[(int)ds];
13417           decl_specs->any_specifiers_p = true;
13418         }
13419       return cp_lexer_consume_token (parser->lexer)->u.value;
13420     }
13421
13422   /* If we do not already have a type-specifier, assume we are looking
13423      at a simple-type-specifier.  */
13424   type_spec = cp_parser_simple_type_specifier (parser,
13425                                                decl_specs,
13426                                                flags);
13427
13428   /* If we didn't find a type-specifier, and a type-specifier was not
13429      optional in this context, issue an error message.  */
13430   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13431     {
13432       cp_parser_error (parser, "expected type specifier");
13433       return error_mark_node;
13434     }
13435
13436   return type_spec;
13437 }
13438
13439 /* Parse a simple-type-specifier.
13440
13441    simple-type-specifier:
13442      :: [opt] nested-name-specifier [opt] type-name
13443      :: [opt] nested-name-specifier template template-id
13444      char
13445      wchar_t
13446      bool
13447      short
13448      int
13449      long
13450      signed
13451      unsigned
13452      float
13453      double
13454      void
13455
13456    C++0x Extension:
13457
13458    simple-type-specifier:
13459      auto
13460      decltype ( expression )   
13461      char16_t
13462      char32_t
13463      __underlying_type ( type-id )
13464
13465    GNU Extension:
13466
13467    simple-type-specifier:
13468      __int128
13469      __typeof__ unary-expression
13470      __typeof__ ( type-id )
13471
13472    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13473    appropriately updated.  */
13474
13475 static tree
13476 cp_parser_simple_type_specifier (cp_parser* parser,
13477                                  cp_decl_specifier_seq *decl_specs,
13478                                  cp_parser_flags flags)
13479 {
13480   tree type = NULL_TREE;
13481   cp_token *token;
13482
13483   /* Peek at the next token.  */
13484   token = cp_lexer_peek_token (parser->lexer);
13485
13486   /* If we're looking at a keyword, things are easy.  */
13487   switch (token->keyword)
13488     {
13489     case RID_CHAR:
13490       if (decl_specs)
13491         decl_specs->explicit_char_p = true;
13492       type = char_type_node;
13493       break;
13494     case RID_CHAR16:
13495       type = char16_type_node;
13496       break;
13497     case RID_CHAR32:
13498       type = char32_type_node;
13499       break;
13500     case RID_WCHAR:
13501       type = wchar_type_node;
13502       break;
13503     case RID_BOOL:
13504       type = boolean_type_node;
13505       break;
13506     case RID_SHORT:
13507       if (decl_specs)
13508         ++decl_specs->specs[(int) ds_short];
13509       type = short_integer_type_node;
13510       break;
13511     case RID_INT:
13512       if (decl_specs)
13513         decl_specs->explicit_int_p = true;
13514       type = integer_type_node;
13515       break;
13516     case RID_INT128:
13517       if (!int128_integer_type_node)
13518         break;
13519       if (decl_specs)
13520         decl_specs->explicit_int128_p = true;
13521       type = int128_integer_type_node;
13522       break;
13523     case RID_LONG:
13524       if (decl_specs)
13525         ++decl_specs->specs[(int) ds_long];
13526       type = long_integer_type_node;
13527       break;
13528     case RID_SIGNED:
13529       if (decl_specs)
13530         ++decl_specs->specs[(int) ds_signed];
13531       type = integer_type_node;
13532       break;
13533     case RID_UNSIGNED:
13534       if (decl_specs)
13535         ++decl_specs->specs[(int) ds_unsigned];
13536       type = unsigned_type_node;
13537       break;
13538     case RID_FLOAT:
13539       type = float_type_node;
13540       break;
13541     case RID_DOUBLE:
13542       type = double_type_node;
13543       break;
13544     case RID_VOID:
13545       type = void_type_node;
13546       break;
13547       
13548     case RID_AUTO:
13549       maybe_warn_cpp0x (CPP0X_AUTO);
13550       type = make_auto ();
13551       break;
13552
13553     case RID_DECLTYPE:
13554       /* Since DR 743, decltype can either be a simple-type-specifier by
13555          itself or begin a nested-name-specifier.  Parsing it will replace
13556          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13557          handling below decide what to do.  */
13558       cp_parser_decltype (parser);
13559       cp_lexer_set_token_position (parser->lexer, token);
13560       break;
13561
13562     case RID_TYPEOF:
13563       /* Consume the `typeof' token.  */
13564       cp_lexer_consume_token (parser->lexer);
13565       /* Parse the operand to `typeof'.  */
13566       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13567       /* If it is not already a TYPE, take its type.  */
13568       if (!TYPE_P (type))
13569         type = finish_typeof (type);
13570
13571       if (decl_specs)
13572         cp_parser_set_decl_spec_type (decl_specs, type,
13573                                       token->location,
13574                                       /*type_definition_p=*/false);
13575
13576       return type;
13577
13578     case RID_UNDERLYING_TYPE:
13579       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13580       if (decl_specs)
13581         cp_parser_set_decl_spec_type (decl_specs, type,
13582                                       token->location,
13583                                       /*type_definition_p=*/false);
13584
13585       return type;
13586
13587     case RID_BASES:
13588     case RID_DIRECT_BASES:
13589       type = cp_parser_trait_expr (parser, token->keyword);
13590       if (decl_specs)
13591        cp_parser_set_decl_spec_type (decl_specs, type,
13592                                      token->location,
13593                                      /*type_definition_p=*/false);
13594       return type;
13595     default:
13596       break;
13597     }
13598
13599   /* If token is an already-parsed decltype not followed by ::,
13600      it's a simple-type-specifier.  */
13601   if (token->type == CPP_DECLTYPE
13602       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13603     {
13604       type = token->u.value;
13605       if (decl_specs)
13606         cp_parser_set_decl_spec_type (decl_specs, type,
13607                                       token->location,
13608                                       /*type_definition_p=*/false);
13609       cp_lexer_consume_token (parser->lexer);
13610       return type;
13611     }
13612
13613   /* If the type-specifier was for a built-in type, we're done.  */
13614   if (type)
13615     {
13616       /* Record the type.  */
13617       if (decl_specs
13618           && (token->keyword != RID_SIGNED
13619               && token->keyword != RID_UNSIGNED
13620               && token->keyword != RID_SHORT
13621               && token->keyword != RID_LONG))
13622         cp_parser_set_decl_spec_type (decl_specs,
13623                                       type,
13624                                       token->location,
13625                                       /*type_definition_p=*/false);
13626       if (decl_specs)
13627         decl_specs->any_specifiers_p = true;
13628
13629       /* Consume the token.  */
13630       cp_lexer_consume_token (parser->lexer);
13631
13632       /* There is no valid C++ program where a non-template type is
13633          followed by a "<".  That usually indicates that the user thought
13634          that the type was a template.  */
13635       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13636
13637       return TYPE_NAME (type);
13638     }
13639
13640   /* The type-specifier must be a user-defined type.  */
13641   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13642     {
13643       bool qualified_p;
13644       bool global_p;
13645
13646       /* Don't gobble tokens or issue error messages if this is an
13647          optional type-specifier.  */
13648       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13649         cp_parser_parse_tentatively (parser);
13650
13651       /* Look for the optional `::' operator.  */
13652       global_p
13653         = (cp_parser_global_scope_opt (parser,
13654                                        /*current_scope_valid_p=*/false)
13655            != NULL_TREE);
13656       /* Look for the nested-name specifier.  */
13657       qualified_p
13658         = (cp_parser_nested_name_specifier_opt (parser,
13659                                                 /*typename_keyword_p=*/false,
13660                                                 /*check_dependency_p=*/true,
13661                                                 /*type_p=*/false,
13662                                                 /*is_declaration=*/false)
13663            != NULL_TREE);
13664       token = cp_lexer_peek_token (parser->lexer);
13665       /* If we have seen a nested-name-specifier, and the next token
13666          is `template', then we are using the template-id production.  */
13667       if (parser->scope
13668           && cp_parser_optional_template_keyword (parser))
13669         {
13670           /* Look for the template-id.  */
13671           type = cp_parser_template_id (parser,
13672                                         /*template_keyword_p=*/true,
13673                                         /*check_dependency_p=*/true,
13674                                         /*is_declaration=*/false);
13675           /* If the template-id did not name a type, we are out of
13676              luck.  */
13677           if (TREE_CODE (type) != TYPE_DECL)
13678             {
13679               cp_parser_error (parser, "expected template-id for type");
13680               type = NULL_TREE;
13681             }
13682         }
13683       /* Otherwise, look for a type-name.  */
13684       else
13685         type = cp_parser_type_name (parser);
13686       /* Keep track of all name-lookups performed in class scopes.  */
13687       if (type
13688           && !global_p
13689           && !qualified_p
13690           && TREE_CODE (type) == TYPE_DECL
13691           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13692         maybe_note_name_used_in_class (DECL_NAME (type), type);
13693       /* If it didn't work out, we don't have a TYPE.  */
13694       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13695           && !cp_parser_parse_definitely (parser))
13696         type = NULL_TREE;
13697       if (type && decl_specs)
13698         cp_parser_set_decl_spec_type (decl_specs, type,
13699                                       token->location,
13700                                       /*type_definition_p=*/false);
13701     }
13702
13703   /* If we didn't get a type-name, issue an error message.  */
13704   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13705     {
13706       cp_parser_error (parser, "expected type-name");
13707       return error_mark_node;
13708     }
13709
13710   if (type && type != error_mark_node)
13711     {
13712       /* See if TYPE is an Objective-C type, and if so, parse and
13713          accept any protocol references following it.  Do this before
13714          the cp_parser_check_for_invalid_template_id() call, because
13715          Objective-C types can be followed by '<...>' which would
13716          enclose protocol names rather than template arguments, and so
13717          everything is fine.  */
13718       if (c_dialect_objc () && !parser->scope
13719           && (objc_is_id (type) || objc_is_class_name (type)))
13720         {
13721           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13722           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13723
13724           /* Clobber the "unqualified" type previously entered into
13725              DECL_SPECS with the new, improved protocol-qualified version.  */
13726           if (decl_specs)
13727             decl_specs->type = qual_type;
13728
13729           return qual_type;
13730         }
13731
13732       /* There is no valid C++ program where a non-template type is
13733          followed by a "<".  That usually indicates that the user
13734          thought that the type was a template.  */
13735       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13736                                                token->location);
13737     }
13738
13739   return type;
13740 }
13741
13742 /* Parse a type-name.
13743
13744    type-name:
13745      class-name
13746      enum-name
13747      typedef-name
13748      simple-template-id [in c++0x]
13749
13750    enum-name:
13751      identifier
13752
13753    typedef-name:
13754      identifier
13755
13756    Returns a TYPE_DECL for the type.  */
13757
13758 static tree
13759 cp_parser_type_name (cp_parser* parser)
13760 {
13761   tree type_decl;
13762
13763   /* We can't know yet whether it is a class-name or not.  */
13764   cp_parser_parse_tentatively (parser);
13765   /* Try a class-name.  */
13766   type_decl = cp_parser_class_name (parser,
13767                                     /*typename_keyword_p=*/false,
13768                                     /*template_keyword_p=*/false,
13769                                     none_type,
13770                                     /*check_dependency_p=*/true,
13771                                     /*class_head_p=*/false,
13772                                     /*is_declaration=*/false);
13773   /* If it's not a class-name, keep looking.  */
13774   if (!cp_parser_parse_definitely (parser))
13775     {
13776       if (cxx_dialect < cxx0x)
13777         /* It must be a typedef-name or an enum-name.  */
13778         return cp_parser_nonclass_name (parser);
13779
13780       cp_parser_parse_tentatively (parser);
13781       /* It is either a simple-template-id representing an
13782          instantiation of an alias template...  */
13783       type_decl = cp_parser_template_id (parser,
13784                                          /*template_keyword_p=*/false,
13785                                          /*check_dependency_p=*/false,
13786                                          /*is_declaration=*/false);
13787       /* Note that this must be an instantiation of an alias template
13788          because [temp.names]/6 says:
13789          
13790              A template-id that names an alias template specialization
13791              is a type-name.
13792
13793          Whereas [temp.names]/7 says:
13794          
13795              A simple-template-id that names a class template
13796              specialization is a class-name.  */
13797       if (type_decl != NULL_TREE
13798           && TREE_CODE (type_decl) == TYPE_DECL
13799           && TYPE_DECL_ALIAS_P (type_decl))
13800         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13801       else
13802         cp_parser_simulate_error (parser);
13803
13804       if (!cp_parser_parse_definitely (parser))
13805         /* ... Or a typedef-name or an enum-name.  */
13806         return cp_parser_nonclass_name (parser);
13807     }
13808
13809   return type_decl;
13810 }
13811
13812 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13813
13814    enum-name:
13815      identifier
13816
13817    typedef-name:
13818      identifier
13819
13820    Returns a TYPE_DECL for the type.  */
13821
13822 static tree
13823 cp_parser_nonclass_name (cp_parser* parser)
13824 {
13825   tree type_decl;
13826   tree identifier;
13827
13828   cp_token *token = cp_lexer_peek_token (parser->lexer);
13829   identifier = cp_parser_identifier (parser);
13830   if (identifier == error_mark_node)
13831     return error_mark_node;
13832
13833   /* Look up the type-name.  */
13834   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13835
13836   if (TREE_CODE (type_decl) == USING_DECL)
13837     {
13838       if (!DECL_DEPENDENT_P (type_decl))
13839         type_decl = strip_using_decl (type_decl);
13840       else if (USING_DECL_TYPENAME_P (type_decl))
13841         {
13842           /* We have found a type introduced by a using
13843              declaration at class scope that refers to a dependent
13844              type.
13845              
13846              using typename :: [opt] nested-name-specifier unqualified-id ;
13847           */
13848           type_decl = make_typename_type (TREE_TYPE (type_decl),
13849                                           DECL_NAME (type_decl),
13850                                           typename_type, tf_error);
13851           if (type_decl != error_mark_node)
13852             type_decl = TYPE_NAME (type_decl);
13853         }
13854     }
13855   
13856   if (TREE_CODE (type_decl) != TYPE_DECL
13857       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13858     {
13859       /* See if this is an Objective-C type.  */
13860       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13861       tree type = objc_get_protocol_qualified_type (identifier, protos);
13862       if (type)
13863         type_decl = TYPE_NAME (type);
13864     }
13865
13866   /* Issue an error if we did not find a type-name.  */
13867   if (TREE_CODE (type_decl) != TYPE_DECL
13868       /* In Objective-C, we have the complication that class names are
13869          normally type names and start declarations (eg, the
13870          "NSObject" in "NSObject *object;"), but can be used in an
13871          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13872          is an expression.  So, a classname followed by a dot is not a
13873          valid type-name.  */
13874       || (objc_is_class_name (TREE_TYPE (type_decl))
13875           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13876     {
13877       if (!cp_parser_simulate_error (parser))
13878         cp_parser_name_lookup_error (parser, identifier, type_decl,
13879                                      NLE_TYPE, token->location);
13880       return error_mark_node;
13881     }
13882   /* Remember that the name was used in the definition of the
13883      current class so that we can check later to see if the
13884      meaning would have been different after the class was
13885      entirely defined.  */
13886   else if (type_decl != error_mark_node
13887            && !parser->scope)
13888     maybe_note_name_used_in_class (identifier, type_decl);
13889   
13890   return type_decl;
13891 }
13892
13893 /* Parse an elaborated-type-specifier.  Note that the grammar given
13894    here incorporates the resolution to DR68.
13895
13896    elaborated-type-specifier:
13897      class-key :: [opt] nested-name-specifier [opt] identifier
13898      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13899      enum-key :: [opt] nested-name-specifier [opt] identifier
13900      typename :: [opt] nested-name-specifier identifier
13901      typename :: [opt] nested-name-specifier template [opt]
13902        template-id
13903
13904    GNU extension:
13905
13906    elaborated-type-specifier:
13907      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13908      class-key attributes :: [opt] nested-name-specifier [opt]
13909                template [opt] template-id
13910      enum attributes :: [opt] nested-name-specifier [opt] identifier
13911
13912    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13913    declared `friend'.  If IS_DECLARATION is TRUE, then this
13914    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13915    something is being declared.
13916
13917    Returns the TYPE specified.  */
13918
13919 static tree
13920 cp_parser_elaborated_type_specifier (cp_parser* parser,
13921                                      bool is_friend,
13922                                      bool is_declaration)
13923 {
13924   enum tag_types tag_type;
13925   tree identifier;
13926   tree type = NULL_TREE;
13927   tree attributes = NULL_TREE;
13928   tree globalscope;
13929   cp_token *token = NULL;
13930
13931   /* See if we're looking at the `enum' keyword.  */
13932   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13933     {
13934       /* Consume the `enum' token.  */
13935       cp_lexer_consume_token (parser->lexer);
13936       /* Remember that it's an enumeration type.  */
13937       tag_type = enum_type;
13938       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13939          enums) is used here.  */
13940       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13941           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13942         {
13943             pedwarn (input_location, 0, "elaborated-type-specifier "
13944                       "for a scoped enum must not use the %<%D%> keyword",
13945                       cp_lexer_peek_token (parser->lexer)->u.value);
13946           /* Consume the `struct' or `class' and parse it anyway.  */
13947           cp_lexer_consume_token (parser->lexer);
13948         }
13949       /* Parse the attributes.  */
13950       attributes = cp_parser_attributes_opt (parser);
13951     }
13952   /* Or, it might be `typename'.  */
13953   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13954                                            RID_TYPENAME))
13955     {
13956       /* Consume the `typename' token.  */
13957       cp_lexer_consume_token (parser->lexer);
13958       /* Remember that it's a `typename' type.  */
13959       tag_type = typename_type;
13960     }
13961   /* Otherwise it must be a class-key.  */
13962   else
13963     {
13964       tag_type = cp_parser_class_key (parser);
13965       if (tag_type == none_type)
13966         return error_mark_node;
13967       /* Parse the attributes.  */
13968       attributes = cp_parser_attributes_opt (parser);
13969     }
13970
13971   /* Look for the `::' operator.  */
13972   globalscope =  cp_parser_global_scope_opt (parser,
13973                                              /*current_scope_valid_p=*/false);
13974   /* Look for the nested-name-specifier.  */
13975   if (tag_type == typename_type && !globalscope)
13976     {
13977       if (!cp_parser_nested_name_specifier (parser,
13978                                            /*typename_keyword_p=*/true,
13979                                            /*check_dependency_p=*/true,
13980                                            /*type_p=*/true,
13981                                             is_declaration))
13982         return error_mark_node;
13983     }
13984   else
13985     /* Even though `typename' is not present, the proposed resolution
13986        to Core Issue 180 says that in `class A<T>::B', `B' should be
13987        considered a type-name, even if `A<T>' is dependent.  */
13988     cp_parser_nested_name_specifier_opt (parser,
13989                                          /*typename_keyword_p=*/true,
13990                                          /*check_dependency_p=*/true,
13991                                          /*type_p=*/true,
13992                                          is_declaration);
13993  /* For everything but enumeration types, consider a template-id.
13994     For an enumeration type, consider only a plain identifier.  */
13995   if (tag_type != enum_type)
13996     {
13997       bool template_p = false;
13998       tree decl;
13999
14000       /* Allow the `template' keyword.  */
14001       template_p = cp_parser_optional_template_keyword (parser);
14002       /* If we didn't see `template', we don't know if there's a
14003          template-id or not.  */
14004       if (!template_p)
14005         cp_parser_parse_tentatively (parser);
14006       /* Parse the template-id.  */
14007       token = cp_lexer_peek_token (parser->lexer);
14008       decl = cp_parser_template_id (parser, template_p,
14009                                     /*check_dependency_p=*/true,
14010                                     is_declaration);
14011       /* If we didn't find a template-id, look for an ordinary
14012          identifier.  */
14013       if (!template_p && !cp_parser_parse_definitely (parser))
14014         ;
14015       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14016          in effect, then we must assume that, upon instantiation, the
14017          template will correspond to a class.  */
14018       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14019                && tag_type == typename_type)
14020         type = make_typename_type (parser->scope, decl,
14021                                    typename_type,
14022                                    /*complain=*/tf_error);
14023       /* If the `typename' keyword is in effect and DECL is not a type
14024          decl. Then type is non existant.   */
14025       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14026         type = NULL_TREE; 
14027       else 
14028         type = check_elaborated_type_specifier (tag_type, decl,
14029                                                 /*allow_template_p=*/true);
14030     }
14031
14032   if (!type)
14033     {
14034       token = cp_lexer_peek_token (parser->lexer);
14035       identifier = cp_parser_identifier (parser);
14036
14037       if (identifier == error_mark_node)
14038         {
14039           parser->scope = NULL_TREE;
14040           return error_mark_node;
14041         }
14042
14043       /* For a `typename', we needn't call xref_tag.  */
14044       if (tag_type == typename_type
14045           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14046         return cp_parser_make_typename_type (parser, parser->scope,
14047                                              identifier,
14048                                              token->location);
14049       /* Look up a qualified name in the usual way.  */
14050       if (parser->scope)
14051         {
14052           tree decl;
14053           tree ambiguous_decls;
14054
14055           decl = cp_parser_lookup_name (parser, identifier,
14056                                         tag_type,
14057                                         /*is_template=*/false,
14058                                         /*is_namespace=*/false,
14059                                         /*check_dependency=*/true,
14060                                         &ambiguous_decls,
14061                                         token->location);
14062
14063           /* If the lookup was ambiguous, an error will already have been
14064              issued.  */
14065           if (ambiguous_decls)
14066             return error_mark_node;
14067
14068           /* If we are parsing friend declaration, DECL may be a
14069              TEMPLATE_DECL tree node here.  However, we need to check
14070              whether this TEMPLATE_DECL results in valid code.  Consider
14071              the following example:
14072
14073                namespace N {
14074                  template <class T> class C {};
14075                }
14076                class X {
14077                  template <class T> friend class N::C; // #1, valid code
14078                };
14079                template <class T> class Y {
14080                  friend class N::C;                    // #2, invalid code
14081                };
14082
14083              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14084              name lookup of `N::C'.  We see that friend declaration must
14085              be template for the code to be valid.  Note that
14086              processing_template_decl does not work here since it is
14087              always 1 for the above two cases.  */
14088
14089           decl = (cp_parser_maybe_treat_template_as_class
14090                   (decl, /*tag_name_p=*/is_friend
14091                          && parser->num_template_parameter_lists));
14092
14093           if (TREE_CODE (decl) != TYPE_DECL)
14094             {
14095               cp_parser_diagnose_invalid_type_name (parser,
14096                                                     parser->scope,
14097                                                     identifier,
14098                                                     token->location);
14099               return error_mark_node;
14100             }
14101
14102           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14103             {
14104               bool allow_template = (parser->num_template_parameter_lists
14105                                       || DECL_SELF_REFERENCE_P (decl));
14106               type = check_elaborated_type_specifier (tag_type, decl, 
14107                                                       allow_template);
14108
14109               if (type == error_mark_node)
14110                 return error_mark_node;
14111             }
14112
14113           /* Forward declarations of nested types, such as
14114
14115                class C1::C2;
14116                class C1::C2::C3;
14117
14118              are invalid unless all components preceding the final '::'
14119              are complete.  If all enclosing types are complete, these
14120              declarations become merely pointless.
14121
14122              Invalid forward declarations of nested types are errors
14123              caught elsewhere in parsing.  Those that are pointless arrive
14124              here.  */
14125
14126           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14127               && !is_friend && !processing_explicit_instantiation)
14128             warning (0, "declaration %qD does not declare anything", decl);
14129
14130           type = TREE_TYPE (decl);
14131         }
14132       else
14133         {
14134           /* An elaborated-type-specifier sometimes introduces a new type and
14135              sometimes names an existing type.  Normally, the rule is that it
14136              introduces a new type only if there is not an existing type of
14137              the same name already in scope.  For example, given:
14138
14139                struct S {};
14140                void f() { struct S s; }
14141
14142              the `struct S' in the body of `f' is the same `struct S' as in
14143              the global scope; the existing definition is used.  However, if
14144              there were no global declaration, this would introduce a new
14145              local class named `S'.
14146
14147              An exception to this rule applies to the following code:
14148
14149                namespace N { struct S; }
14150
14151              Here, the elaborated-type-specifier names a new type
14152              unconditionally; even if there is already an `S' in the
14153              containing scope this declaration names a new type.
14154              This exception only applies if the elaborated-type-specifier
14155              forms the complete declaration:
14156
14157                [class.name]
14158
14159                A declaration consisting solely of `class-key identifier ;' is
14160                either a redeclaration of the name in the current scope or a
14161                forward declaration of the identifier as a class name.  It
14162                introduces the name into the current scope.
14163
14164              We are in this situation precisely when the next token is a `;'.
14165
14166              An exception to the exception is that a `friend' declaration does
14167              *not* name a new type; i.e., given:
14168
14169                struct S { friend struct T; };
14170
14171              `T' is not a new type in the scope of `S'.
14172
14173              Also, `new struct S' or `sizeof (struct S)' never results in the
14174              definition of a new type; a new type can only be declared in a
14175              declaration context.  */
14176
14177           tag_scope ts;
14178           bool template_p;
14179
14180           if (is_friend)
14181             /* Friends have special name lookup rules.  */
14182             ts = ts_within_enclosing_non_class;
14183           else if (is_declaration
14184                    && cp_lexer_next_token_is (parser->lexer,
14185                                               CPP_SEMICOLON))
14186             /* This is a `class-key identifier ;' */
14187             ts = ts_current;
14188           else
14189             ts = ts_global;
14190
14191           template_p =
14192             (parser->num_template_parameter_lists
14193              && (cp_parser_next_token_starts_class_definition_p (parser)
14194                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14195           /* An unqualified name was used to reference this type, so
14196              there were no qualifying templates.  */
14197           if (!cp_parser_check_template_parameters (parser,
14198                                                     /*num_templates=*/0,
14199                                                     token->location,
14200                                                     /*declarator=*/NULL))
14201             return error_mark_node;
14202           type = xref_tag (tag_type, identifier, ts, template_p);
14203         }
14204     }
14205
14206   if (type == error_mark_node)
14207     return error_mark_node;
14208
14209   /* Allow attributes on forward declarations of classes.  */
14210   if (attributes)
14211     {
14212       if (TREE_CODE (type) == TYPENAME_TYPE)
14213         warning (OPT_Wattributes,
14214                  "attributes ignored on uninstantiated type");
14215       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14216                && ! processing_explicit_instantiation)
14217         warning (OPT_Wattributes,
14218                  "attributes ignored on template instantiation");
14219       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14220         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14221       else
14222         warning (OPT_Wattributes,
14223                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14224     }
14225
14226   if (tag_type != enum_type)
14227     {
14228       /* Indicate whether this class was declared as a `class' or as a
14229          `struct'.  */
14230       if (TREE_CODE (type) == RECORD_TYPE)
14231         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14232       cp_parser_check_class_key (tag_type, type);
14233     }
14234
14235   /* A "<" cannot follow an elaborated type specifier.  If that
14236      happens, the user was probably trying to form a template-id.  */
14237   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14238
14239   return type;
14240 }
14241
14242 /* Parse an enum-specifier.
14243
14244    enum-specifier:
14245      enum-head { enumerator-list [opt] }
14246      enum-head { enumerator-list , } [C++0x]
14247
14248    enum-head:
14249      enum-key identifier [opt] enum-base [opt]
14250      enum-key nested-name-specifier identifier enum-base [opt]
14251
14252    enum-key:
14253      enum
14254      enum class   [C++0x]
14255      enum struct  [C++0x]
14256
14257    enum-base:   [C++0x]
14258      : type-specifier-seq
14259
14260    opaque-enum-specifier:
14261      enum-key identifier enum-base [opt] ;
14262
14263    GNU Extensions:
14264      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14265        { enumerator-list [opt] }attributes[opt]
14266      enum-key attributes[opt] identifier [opt] enum-base [opt]
14267        { enumerator-list, }attributes[opt] [C++0x]
14268
14269    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14270    if the token stream isn't an enum-specifier after all.  */
14271
14272 static tree
14273 cp_parser_enum_specifier (cp_parser* parser)
14274 {
14275   tree identifier;
14276   tree type = NULL_TREE;
14277   tree prev_scope;
14278   tree nested_name_specifier = NULL_TREE;
14279   tree attributes;
14280   bool scoped_enum_p = false;
14281   bool has_underlying_type = false;
14282   bool nested_being_defined = false;
14283   bool new_value_list = false;
14284   bool is_new_type = false;
14285   bool is_anonymous = false;
14286   tree underlying_type = NULL_TREE;
14287   cp_token *type_start_token = NULL;
14288   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14289
14290   parser->colon_corrects_to_scope_p = false;
14291
14292   /* Parse tentatively so that we can back up if we don't find a
14293      enum-specifier.  */
14294   cp_parser_parse_tentatively (parser);
14295
14296   /* Caller guarantees that the current token is 'enum', an identifier
14297      possibly follows, and the token after that is an opening brace.
14298      If we don't have an identifier, fabricate an anonymous name for
14299      the enumeration being defined.  */
14300   cp_lexer_consume_token (parser->lexer);
14301
14302   /* Parse the "class" or "struct", which indicates a scoped
14303      enumeration type in C++0x.  */
14304   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14305       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14306     {
14307       if (cxx_dialect < cxx0x)
14308         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14309
14310       /* Consume the `struct' or `class' token.  */
14311       cp_lexer_consume_token (parser->lexer);
14312
14313       scoped_enum_p = true;
14314     }
14315
14316   attributes = cp_parser_attributes_opt (parser);
14317
14318   /* Clear the qualification.  */
14319   parser->scope = NULL_TREE;
14320   parser->qualifying_scope = NULL_TREE;
14321   parser->object_scope = NULL_TREE;
14322
14323   /* Figure out in what scope the declaration is being placed.  */
14324   prev_scope = current_scope ();
14325
14326   type_start_token = cp_lexer_peek_token (parser->lexer);
14327
14328   push_deferring_access_checks (dk_no_check);
14329   nested_name_specifier
14330       = cp_parser_nested_name_specifier_opt (parser,
14331                                              /*typename_keyword_p=*/true,
14332                                              /*check_dependency_p=*/false,
14333                                              /*type_p=*/false,
14334                                              /*is_declaration=*/false);
14335
14336   if (nested_name_specifier)
14337     {
14338       tree name;
14339
14340       identifier = cp_parser_identifier (parser);
14341       name =  cp_parser_lookup_name (parser, identifier,
14342                                      enum_type,
14343                                      /*is_template=*/false,
14344                                      /*is_namespace=*/false,
14345                                      /*check_dependency=*/true,
14346                                      /*ambiguous_decls=*/NULL,
14347                                      input_location);
14348       if (name)
14349         {
14350           type = TREE_TYPE (name);
14351           if (TREE_CODE (type) == TYPENAME_TYPE)
14352             {
14353               /* Are template enums allowed in ISO? */
14354               if (template_parm_scope_p ())
14355                 pedwarn (type_start_token->location, OPT_pedantic,
14356                          "%qD is an enumeration template", name);
14357               /* ignore a typename reference, for it will be solved by name
14358                  in start_enum.  */
14359               type = NULL_TREE;
14360             }
14361         }
14362       else
14363         error_at (type_start_token->location,
14364                   "%qD is not an enumerator-name", identifier);
14365     }
14366   else
14367     {
14368       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14369         identifier = cp_parser_identifier (parser);
14370       else
14371         {
14372           identifier = make_anon_name ();
14373           is_anonymous = true;
14374         }
14375     }
14376   pop_deferring_access_checks ();
14377
14378   /* Check for the `:' that denotes a specified underlying type in C++0x.
14379      Note that a ':' could also indicate a bitfield width, however.  */
14380   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14381     {
14382       cp_decl_specifier_seq type_specifiers;
14383
14384       /* Consume the `:'.  */
14385       cp_lexer_consume_token (parser->lexer);
14386
14387       /* Parse the type-specifier-seq.  */
14388       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14389                                     /*is_trailing_return=*/false,
14390                                     &type_specifiers);
14391
14392       /* At this point this is surely not elaborated type specifier.  */
14393       if (!cp_parser_parse_definitely (parser))
14394         return NULL_TREE;
14395
14396       if (cxx_dialect < cxx0x)
14397         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14398
14399       has_underlying_type = true;
14400
14401       /* If that didn't work, stop.  */
14402       if (type_specifiers.type != error_mark_node)
14403         {
14404           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14405                                             /*initialized=*/0, NULL);
14406           if (underlying_type == error_mark_node)
14407             underlying_type = NULL_TREE;
14408         }
14409     }
14410
14411   /* Look for the `{' but don't consume it yet.  */
14412   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14413     {
14414       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14415         {
14416           cp_parser_error (parser, "expected %<{%>");
14417           if (has_underlying_type)
14418             {
14419               type = NULL_TREE;
14420               goto out;
14421             }
14422         }
14423       /* An opaque-enum-specifier must have a ';' here.  */
14424       if ((scoped_enum_p || underlying_type)
14425           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14426         {
14427           cp_parser_error (parser, "expected %<;%> or %<{%>");
14428           if (has_underlying_type)
14429             {
14430               type = NULL_TREE;
14431               goto out;
14432             }
14433         }
14434     }
14435
14436   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14437     return NULL_TREE;
14438
14439   if (nested_name_specifier)
14440     {
14441       if (CLASS_TYPE_P (nested_name_specifier))
14442         {
14443           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14444           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14445           push_scope (nested_name_specifier);
14446         }
14447       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14448         {
14449           push_nested_namespace (nested_name_specifier);
14450         }
14451     }
14452
14453   /* Issue an error message if type-definitions are forbidden here.  */
14454   if (!cp_parser_check_type_definition (parser))
14455     type = error_mark_node;
14456   else
14457     /* Create the new type.  We do this before consuming the opening
14458        brace so the enum will be recorded as being on the line of its
14459        tag (or the 'enum' keyword, if there is no tag).  */
14460     type = start_enum (identifier, type, underlying_type,
14461                        scoped_enum_p, &is_new_type);
14462
14463   /* If the next token is not '{' it is an opaque-enum-specifier or an
14464      elaborated-type-specifier.  */
14465   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14466     {
14467       timevar_push (TV_PARSE_ENUM);
14468       if (nested_name_specifier)
14469         {
14470           /* The following catches invalid code such as:
14471              enum class S<int>::E { A, B, C }; */
14472           if (!processing_specialization
14473               && CLASS_TYPE_P (nested_name_specifier)
14474               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14475             error_at (type_start_token->location, "cannot add an enumerator "
14476                       "list to a template instantiation");
14477
14478           /* If that scope does not contain the scope in which the
14479              class was originally declared, the program is invalid.  */
14480           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14481             {
14482               if (at_namespace_scope_p ())
14483                 error_at (type_start_token->location,
14484                           "declaration of %qD in namespace %qD which does not "
14485                           "enclose %qD",
14486                           type, prev_scope, nested_name_specifier);
14487               else
14488                 error_at (type_start_token->location,
14489                           "declaration of %qD in %qD which does not enclose %qD",
14490                           type, prev_scope, nested_name_specifier);
14491               type = error_mark_node;
14492             }
14493         }
14494
14495       if (scoped_enum_p)
14496         begin_scope (sk_scoped_enum, type);
14497
14498       /* Consume the opening brace.  */
14499       cp_lexer_consume_token (parser->lexer);
14500
14501       if (type == error_mark_node)
14502         ; /* Nothing to add */
14503       else if (OPAQUE_ENUM_P (type)
14504                || (cxx_dialect > cxx98 && processing_specialization))
14505         {
14506           new_value_list = true;
14507           SET_OPAQUE_ENUM_P (type, false);
14508           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14509         }
14510       else
14511         {
14512           error_at (type_start_token->location, "multiple definition of %q#T", type);
14513           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14514                     "previous definition here");
14515           type = error_mark_node;
14516         }
14517
14518       if (type == error_mark_node)
14519         cp_parser_skip_to_end_of_block_or_statement (parser);
14520       /* If the next token is not '}', then there are some enumerators.  */
14521       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14522         cp_parser_enumerator_list (parser, type);
14523
14524       /* Consume the final '}'.  */
14525       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14526
14527       if (scoped_enum_p)
14528         finish_scope ();
14529       timevar_pop (TV_PARSE_ENUM);
14530     }
14531   else
14532     {
14533       /* If a ';' follows, then it is an opaque-enum-specifier
14534         and additional restrictions apply.  */
14535       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14536         {
14537           if (is_anonymous)
14538             error_at (type_start_token->location,
14539                       "opaque-enum-specifier without name");
14540           else if (nested_name_specifier)
14541             error_at (type_start_token->location,
14542                       "opaque-enum-specifier must use a simple identifier");
14543         }
14544     }
14545
14546   /* Look for trailing attributes to apply to this enumeration, and
14547      apply them if appropriate.  */
14548   if (cp_parser_allow_gnu_extensions_p (parser))
14549     {
14550       tree trailing_attr = cp_parser_attributes_opt (parser);
14551       trailing_attr = chainon (trailing_attr, attributes);
14552       cplus_decl_attributes (&type,
14553                              trailing_attr,
14554                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14555     }
14556
14557   /* Finish up the enumeration.  */
14558   if (type != error_mark_node)
14559     {
14560       if (new_value_list)
14561         finish_enum_value_list (type);
14562       if (is_new_type)
14563         finish_enum (type);
14564     }
14565
14566   if (nested_name_specifier)
14567     {
14568       if (CLASS_TYPE_P (nested_name_specifier))
14569         {
14570           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14571           pop_scope (nested_name_specifier);
14572         }
14573       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14574         {
14575           pop_nested_namespace (nested_name_specifier);
14576         }
14577     }
14578  out:
14579   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14580   return type;
14581 }
14582
14583 /* Parse an enumerator-list.  The enumerators all have the indicated
14584    TYPE.
14585
14586    enumerator-list:
14587      enumerator-definition
14588      enumerator-list , enumerator-definition  */
14589
14590 static void
14591 cp_parser_enumerator_list (cp_parser* parser, tree type)
14592 {
14593   while (true)
14594     {
14595       /* Parse an enumerator-definition.  */
14596       cp_parser_enumerator_definition (parser, type);
14597
14598       /* If the next token is not a ',', we've reached the end of
14599          the list.  */
14600       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14601         break;
14602       /* Otherwise, consume the `,' and keep going.  */
14603       cp_lexer_consume_token (parser->lexer);
14604       /* If the next token is a `}', there is a trailing comma.  */
14605       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14606         {
14607           if (cxx_dialect < cxx0x && !in_system_header)
14608             pedwarn (input_location, OPT_pedantic,
14609                      "comma at end of enumerator list");
14610           break;
14611         }
14612     }
14613 }
14614
14615 /* Parse an enumerator-definition.  The enumerator has the indicated
14616    TYPE.
14617
14618    enumerator-definition:
14619      enumerator
14620      enumerator = constant-expression
14621
14622    enumerator:
14623      identifier  */
14624
14625 static void
14626 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14627 {
14628   tree identifier;
14629   tree value;
14630   location_t loc;
14631
14632   /* Save the input location because we are interested in the location
14633      of the identifier and not the location of the explicit value.  */
14634   loc = cp_lexer_peek_token (parser->lexer)->location;
14635
14636   /* Look for the identifier.  */
14637   identifier = cp_parser_identifier (parser);
14638   if (identifier == error_mark_node)
14639     return;
14640
14641   /* If the next token is an '=', then there is an explicit value.  */
14642   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14643     {
14644       /* Consume the `=' token.  */
14645       cp_lexer_consume_token (parser->lexer);
14646       /* Parse the value.  */
14647       value = cp_parser_constant_expression (parser,
14648                                              /*allow_non_constant_p=*/false,
14649                                              NULL);
14650     }
14651   else
14652     value = NULL_TREE;
14653
14654   /* If we are processing a template, make sure the initializer of the
14655      enumerator doesn't contain any bare template parameter pack.  */
14656   if (check_for_bare_parameter_packs (value))
14657     value = error_mark_node;
14658
14659   /* integral_constant_value will pull out this expression, so make sure
14660      it's folded as appropriate.  */
14661   value = fold_non_dependent_expr (value);
14662
14663   /* Create the enumerator.  */
14664   build_enumerator (identifier, value, type, loc);
14665 }
14666
14667 /* Parse a namespace-name.
14668
14669    namespace-name:
14670      original-namespace-name
14671      namespace-alias
14672
14673    Returns the NAMESPACE_DECL for the namespace.  */
14674
14675 static tree
14676 cp_parser_namespace_name (cp_parser* parser)
14677 {
14678   tree identifier;
14679   tree namespace_decl;
14680
14681   cp_token *token = cp_lexer_peek_token (parser->lexer);
14682
14683   /* Get the name of the namespace.  */
14684   identifier = cp_parser_identifier (parser);
14685   if (identifier == error_mark_node)
14686     return error_mark_node;
14687
14688   /* Look up the identifier in the currently active scope.  Look only
14689      for namespaces, due to:
14690
14691        [basic.lookup.udir]
14692
14693        When looking up a namespace-name in a using-directive or alias
14694        definition, only namespace names are considered.
14695
14696      And:
14697
14698        [basic.lookup.qual]
14699
14700        During the lookup of a name preceding the :: scope resolution
14701        operator, object, function, and enumerator names are ignored.
14702
14703      (Note that cp_parser_qualifying_entity only calls this
14704      function if the token after the name is the scope resolution
14705      operator.)  */
14706   namespace_decl = cp_parser_lookup_name (parser, identifier,
14707                                           none_type,
14708                                           /*is_template=*/false,
14709                                           /*is_namespace=*/true,
14710                                           /*check_dependency=*/true,
14711                                           /*ambiguous_decls=*/NULL,
14712                                           token->location);
14713   /* If it's not a namespace, issue an error.  */
14714   if (namespace_decl == error_mark_node
14715       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14716     {
14717       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14718         error_at (token->location, "%qD is not a namespace-name", identifier);
14719       cp_parser_error (parser, "expected namespace-name");
14720       namespace_decl = error_mark_node;
14721     }
14722
14723   return namespace_decl;
14724 }
14725
14726 /* Parse a namespace-definition.
14727
14728    namespace-definition:
14729      named-namespace-definition
14730      unnamed-namespace-definition
14731
14732    named-namespace-definition:
14733      original-namespace-definition
14734      extension-namespace-definition
14735
14736    original-namespace-definition:
14737      namespace identifier { namespace-body }
14738
14739    extension-namespace-definition:
14740      namespace original-namespace-name { namespace-body }
14741
14742    unnamed-namespace-definition:
14743      namespace { namespace-body } */
14744
14745 static void
14746 cp_parser_namespace_definition (cp_parser* parser)
14747 {
14748   tree identifier, attribs;
14749   bool has_visibility;
14750   bool is_inline;
14751
14752   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14753     {
14754       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14755       is_inline = true;
14756       cp_lexer_consume_token (parser->lexer);
14757     }
14758   else
14759     is_inline = false;
14760
14761   /* Look for the `namespace' keyword.  */
14762   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14763
14764   /* Get the name of the namespace.  We do not attempt to distinguish
14765      between an original-namespace-definition and an
14766      extension-namespace-definition at this point.  The semantic
14767      analysis routines are responsible for that.  */
14768   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14769     identifier = cp_parser_identifier (parser);
14770   else
14771     identifier = NULL_TREE;
14772
14773   /* Parse any specified attributes.  */
14774   attribs = cp_parser_attributes_opt (parser);
14775
14776   /* Look for the `{' to start the namespace.  */
14777   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14778   /* Start the namespace.  */
14779   push_namespace (identifier);
14780
14781   /* "inline namespace" is equivalent to a stub namespace definition
14782      followed by a strong using directive.  */
14783   if (is_inline)
14784     {
14785       tree name_space = current_namespace;
14786       /* Set up namespace association.  */
14787       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14788         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14789                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14790       /* Import the contents of the inline namespace.  */
14791       pop_namespace ();
14792       do_using_directive (name_space);
14793       push_namespace (identifier);
14794     }
14795
14796   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14797
14798   /* Parse the body of the namespace.  */
14799   cp_parser_namespace_body (parser);
14800
14801   if (has_visibility)
14802     pop_visibility (1);
14803
14804   /* Finish the namespace.  */
14805   pop_namespace ();
14806   /* Look for the final `}'.  */
14807   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14808 }
14809
14810 /* Parse a namespace-body.
14811
14812    namespace-body:
14813      declaration-seq [opt]  */
14814
14815 static void
14816 cp_parser_namespace_body (cp_parser* parser)
14817 {
14818   cp_parser_declaration_seq_opt (parser);
14819 }
14820
14821 /* Parse a namespace-alias-definition.
14822
14823    namespace-alias-definition:
14824      namespace identifier = qualified-namespace-specifier ;  */
14825
14826 static void
14827 cp_parser_namespace_alias_definition (cp_parser* parser)
14828 {
14829   tree identifier;
14830   tree namespace_specifier;
14831
14832   cp_token *token = cp_lexer_peek_token (parser->lexer);
14833
14834   /* Look for the `namespace' keyword.  */
14835   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14836   /* Look for the identifier.  */
14837   identifier = cp_parser_identifier (parser);
14838   if (identifier == error_mark_node)
14839     return;
14840   /* Look for the `=' token.  */
14841   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14842       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14843     {
14844       error_at (token->location, "%<namespace%> definition is not allowed here");
14845       /* Skip the definition.  */
14846       cp_lexer_consume_token (parser->lexer);
14847       if (cp_parser_skip_to_closing_brace (parser))
14848         cp_lexer_consume_token (parser->lexer);
14849       return;
14850     }
14851   cp_parser_require (parser, CPP_EQ, RT_EQ);
14852   /* Look for the qualified-namespace-specifier.  */
14853   namespace_specifier
14854     = cp_parser_qualified_namespace_specifier (parser);
14855   /* Look for the `;' token.  */
14856   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14857
14858   /* Register the alias in the symbol table.  */
14859   do_namespace_alias (identifier, namespace_specifier);
14860 }
14861
14862 /* Parse a qualified-namespace-specifier.
14863
14864    qualified-namespace-specifier:
14865      :: [opt] nested-name-specifier [opt] namespace-name
14866
14867    Returns a NAMESPACE_DECL corresponding to the specified
14868    namespace.  */
14869
14870 static tree
14871 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14872 {
14873   /* Look for the optional `::'.  */
14874   cp_parser_global_scope_opt (parser,
14875                               /*current_scope_valid_p=*/false);
14876
14877   /* Look for the optional nested-name-specifier.  */
14878   cp_parser_nested_name_specifier_opt (parser,
14879                                        /*typename_keyword_p=*/false,
14880                                        /*check_dependency_p=*/true,
14881                                        /*type_p=*/false,
14882                                        /*is_declaration=*/true);
14883
14884   return cp_parser_namespace_name (parser);
14885 }
14886
14887 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14888    access declaration.
14889
14890    using-declaration:
14891      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14892      using :: unqualified-id ;  
14893
14894    access-declaration:
14895      qualified-id ;  
14896
14897    */
14898
14899 static bool
14900 cp_parser_using_declaration (cp_parser* parser, 
14901                              bool access_declaration_p)
14902 {
14903   cp_token *token;
14904   bool typename_p = false;
14905   bool global_scope_p;
14906   tree decl;
14907   tree identifier;
14908   tree qscope;
14909   int oldcount = errorcount;
14910   cp_token *diag_token = NULL;
14911
14912   if (access_declaration_p)
14913     {
14914       diag_token = cp_lexer_peek_token (parser->lexer);
14915       cp_parser_parse_tentatively (parser);
14916     }
14917   else
14918     {
14919       /* Look for the `using' keyword.  */
14920       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14921       
14922       /* Peek at the next token.  */
14923       token = cp_lexer_peek_token (parser->lexer);
14924       /* See if it's `typename'.  */
14925       if (token->keyword == RID_TYPENAME)
14926         {
14927           /* Remember that we've seen it.  */
14928           typename_p = true;
14929           /* Consume the `typename' token.  */
14930           cp_lexer_consume_token (parser->lexer);
14931         }
14932     }
14933
14934   /* Look for the optional global scope qualification.  */
14935   global_scope_p
14936     = (cp_parser_global_scope_opt (parser,
14937                                    /*current_scope_valid_p=*/false)
14938        != NULL_TREE);
14939
14940   /* If we saw `typename', or didn't see `::', then there must be a
14941      nested-name-specifier present.  */
14942   if (typename_p || !global_scope_p)
14943     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14944                                               /*check_dependency_p=*/true,
14945                                               /*type_p=*/false,
14946                                               /*is_declaration=*/true);
14947   /* Otherwise, we could be in either of the two productions.  In that
14948      case, treat the nested-name-specifier as optional.  */
14949   else
14950     qscope = cp_parser_nested_name_specifier_opt (parser,
14951                                                   /*typename_keyword_p=*/false,
14952                                                   /*check_dependency_p=*/true,
14953                                                   /*type_p=*/false,
14954                                                   /*is_declaration=*/true);
14955   if (!qscope)
14956     qscope = global_namespace;
14957
14958   if (access_declaration_p && cp_parser_error_occurred (parser))
14959     /* Something has already gone wrong; there's no need to parse
14960        further.  Since an error has occurred, the return value of
14961        cp_parser_parse_definitely will be false, as required.  */
14962     return cp_parser_parse_definitely (parser);
14963
14964   token = cp_lexer_peek_token (parser->lexer);
14965   /* Parse the unqualified-id.  */
14966   identifier = cp_parser_unqualified_id (parser,
14967                                          /*template_keyword_p=*/false,
14968                                          /*check_dependency_p=*/true,
14969                                          /*declarator_p=*/true,
14970                                          /*optional_p=*/false);
14971
14972   if (access_declaration_p)
14973     {
14974       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14975         cp_parser_simulate_error (parser);
14976       if (!cp_parser_parse_definitely (parser))
14977         return false;
14978     }
14979
14980   /* The function we call to handle a using-declaration is different
14981      depending on what scope we are in.  */
14982   if (qscope == error_mark_node || identifier == error_mark_node)
14983     ;
14984   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14985            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14986     /* [namespace.udecl]
14987
14988        A using declaration shall not name a template-id.  */
14989     error_at (token->location,
14990               "a template-id may not appear in a using-declaration");
14991   else
14992     {
14993       if (at_class_scope_p ())
14994         {
14995           /* Create the USING_DECL.  */
14996           decl = do_class_using_decl (parser->scope, identifier);
14997
14998           if (decl && typename_p)
14999             USING_DECL_TYPENAME_P (decl) = 1;
15000
15001           if (check_for_bare_parameter_packs (decl))
15002             return false;
15003           else
15004             /* Add it to the list of members in this class.  */
15005             finish_member_declaration (decl);
15006         }
15007       else
15008         {
15009           decl = cp_parser_lookup_name_simple (parser,
15010                                                identifier,
15011                                                token->location);
15012           if (decl == error_mark_node)
15013             cp_parser_name_lookup_error (parser, identifier,
15014                                          decl, NLE_NULL,
15015                                          token->location);
15016           else if (check_for_bare_parameter_packs (decl))
15017             return false;
15018           else if (!at_namespace_scope_p ())
15019             do_local_using_decl (decl, qscope, identifier);
15020           else
15021             do_toplevel_using_decl (decl, qscope, identifier);
15022         }
15023     }
15024
15025   /* Look for the final `;'.  */
15026   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15027
15028   if (access_declaration_p && errorcount == oldcount)
15029     warning_at (diag_token->location, OPT_Wdeprecated,
15030                 "access declarations are deprecated "
15031                 "in favour of using-declarations; "
15032                 "suggestion: add the %<using%> keyword");
15033
15034   return true;
15035 }
15036
15037 /* Parse an alias-declaration.
15038
15039    alias-declaration:
15040      using identifier attribute-specifier-seq [opt] = type-id  */
15041
15042 static tree
15043 cp_parser_alias_declaration (cp_parser* parser)
15044 {
15045   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15046   location_t id_location;
15047   cp_declarator *declarator;
15048   cp_decl_specifier_seq decl_specs;
15049   bool member_p;
15050   const char *saved_message = NULL;
15051
15052   /* Look for the `using' keyword.  */
15053   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15054   id_location = cp_lexer_peek_token (parser->lexer)->location;
15055   id = cp_parser_identifier (parser);
15056   if (id == error_mark_node)
15057     return error_mark_node;
15058
15059   attributes = cp_parser_attributes_opt (parser);
15060   if (attributes == error_mark_node)
15061     return error_mark_node;
15062
15063   cp_parser_require (parser, CPP_EQ, RT_EQ);
15064
15065   /* Now we are going to parse the type-id of the declaration.  */
15066
15067   /*
15068     [dcl.type]/3 says:
15069
15070         "A type-specifier-seq shall not define a class or enumeration
15071          unless it appears in the type-id of an alias-declaration (7.1.3) that
15072          is not the declaration of a template-declaration."
15073
15074     In other words, if we currently are in an alias template, the
15075     type-id should not define a type.
15076
15077     So let's set parser->type_definition_forbidden_message in that
15078     case; cp_parser_check_type_definition (called by
15079     cp_parser_class_specifier) will then emit an error if a type is
15080     defined in the type-id.  */
15081   if (parser->num_template_parameter_lists)
15082     {
15083       saved_message = parser->type_definition_forbidden_message;
15084       parser->type_definition_forbidden_message =
15085         G_("types may not be defined in alias template declarations");
15086     }
15087
15088   type = cp_parser_type_id (parser);
15089
15090   /* Restore the error message if need be.  */
15091   if (parser->num_template_parameter_lists)
15092     parser->type_definition_forbidden_message = saved_message;
15093
15094   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15095
15096   if (cp_parser_error_occurred (parser))
15097     return error_mark_node;
15098
15099   /* A typedef-name can also be introduced by an alias-declaration. The
15100      identifier following the using keyword becomes a typedef-name. It has
15101      the same semantics as if it were introduced by the typedef
15102      specifier. In particular, it does not define a new type and it shall
15103      not appear in the type-id.  */
15104
15105   clear_decl_specs (&decl_specs);
15106   decl_specs.type = type;
15107   decl_specs.attributes = attributes;
15108   ++decl_specs.specs[(int) ds_typedef];
15109   ++decl_specs.specs[(int) ds_alias];
15110
15111   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15112   declarator->id_loc = id_location;
15113
15114   member_p = at_class_scope_p ();
15115   if (member_p)
15116     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15117                       NULL_TREE, attributes);
15118   else
15119     decl = start_decl (declarator, &decl_specs, 0,
15120                        attributes, NULL_TREE, &pushed_scope);
15121   if (decl == error_mark_node)
15122     return decl;
15123
15124   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15125
15126   if (pushed_scope)
15127     pop_scope (pushed_scope);
15128
15129   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15130      added into the symbol table; otherwise, return the TYPE_DECL.  */
15131   if (DECL_LANG_SPECIFIC (decl)
15132       && DECL_TEMPLATE_INFO (decl)
15133       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15134     {
15135       decl = DECL_TI_TEMPLATE (decl);
15136       if (member_p)
15137         check_member_template (decl);
15138     }
15139
15140   return decl;
15141 }
15142
15143 /* Parse a using-directive.
15144
15145    using-directive:
15146      using namespace :: [opt] nested-name-specifier [opt]
15147        namespace-name ;  */
15148
15149 static void
15150 cp_parser_using_directive (cp_parser* parser)
15151 {
15152   tree namespace_decl;
15153   tree attribs;
15154
15155   /* Look for the `using' keyword.  */
15156   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15157   /* And the `namespace' keyword.  */
15158   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15159   /* Look for the optional `::' operator.  */
15160   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15161   /* And the optional nested-name-specifier.  */
15162   cp_parser_nested_name_specifier_opt (parser,
15163                                        /*typename_keyword_p=*/false,
15164                                        /*check_dependency_p=*/true,
15165                                        /*type_p=*/false,
15166                                        /*is_declaration=*/true);
15167   /* Get the namespace being used.  */
15168   namespace_decl = cp_parser_namespace_name (parser);
15169   /* And any specified attributes.  */
15170   attribs = cp_parser_attributes_opt (parser);
15171   /* Update the symbol table.  */
15172   parse_using_directive (namespace_decl, attribs);
15173   /* Look for the final `;'.  */
15174   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15175 }
15176
15177 /* Parse an asm-definition.
15178
15179    asm-definition:
15180      asm ( string-literal ) ;
15181
15182    GNU Extension:
15183
15184    asm-definition:
15185      asm volatile [opt] ( string-literal ) ;
15186      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15187      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15188                           : asm-operand-list [opt] ) ;
15189      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15190                           : asm-operand-list [opt]
15191                           : asm-clobber-list [opt] ) ;
15192      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15193                                : asm-clobber-list [opt]
15194                                : asm-goto-list ) ;  */
15195
15196 static void
15197 cp_parser_asm_definition (cp_parser* parser)
15198 {
15199   tree string;
15200   tree outputs = NULL_TREE;
15201   tree inputs = NULL_TREE;
15202   tree clobbers = NULL_TREE;
15203   tree labels = NULL_TREE;
15204   tree asm_stmt;
15205   bool volatile_p = false;
15206   bool extended_p = false;
15207   bool invalid_inputs_p = false;
15208   bool invalid_outputs_p = false;
15209   bool goto_p = false;
15210   required_token missing = RT_NONE;
15211
15212   /* Look for the `asm' keyword.  */
15213   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15214   /* See if the next token is `volatile'.  */
15215   if (cp_parser_allow_gnu_extensions_p (parser)
15216       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15217     {
15218       /* Remember that we saw the `volatile' keyword.  */
15219       volatile_p = true;
15220       /* Consume the token.  */
15221       cp_lexer_consume_token (parser->lexer);
15222     }
15223   if (cp_parser_allow_gnu_extensions_p (parser)
15224       && parser->in_function_body
15225       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15226     {
15227       /* Remember that we saw the `goto' keyword.  */
15228       goto_p = true;
15229       /* Consume the token.  */
15230       cp_lexer_consume_token (parser->lexer);
15231     }
15232   /* Look for the opening `('.  */
15233   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15234     return;
15235   /* Look for the string.  */
15236   string = cp_parser_string_literal (parser, false, false);
15237   if (string == error_mark_node)
15238     {
15239       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15240                                              /*consume_paren=*/true);
15241       return;
15242     }
15243
15244   /* If we're allowing GNU extensions, check for the extended assembly
15245      syntax.  Unfortunately, the `:' tokens need not be separated by
15246      a space in C, and so, for compatibility, we tolerate that here
15247      too.  Doing that means that we have to treat the `::' operator as
15248      two `:' tokens.  */
15249   if (cp_parser_allow_gnu_extensions_p (parser)
15250       && parser->in_function_body
15251       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15252           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15253     {
15254       bool inputs_p = false;
15255       bool clobbers_p = false;
15256       bool labels_p = false;
15257
15258       /* The extended syntax was used.  */
15259       extended_p = true;
15260
15261       /* Look for outputs.  */
15262       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15263         {
15264           /* Consume the `:'.  */
15265           cp_lexer_consume_token (parser->lexer);
15266           /* Parse the output-operands.  */
15267           if (cp_lexer_next_token_is_not (parser->lexer,
15268                                           CPP_COLON)
15269               && cp_lexer_next_token_is_not (parser->lexer,
15270                                              CPP_SCOPE)
15271               && cp_lexer_next_token_is_not (parser->lexer,
15272                                              CPP_CLOSE_PAREN)
15273               && !goto_p)
15274             outputs = cp_parser_asm_operand_list (parser);
15275
15276             if (outputs == error_mark_node)
15277               invalid_outputs_p = true;
15278         }
15279       /* If the next token is `::', there are no outputs, and the
15280          next token is the beginning of the inputs.  */
15281       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15282         /* The inputs are coming next.  */
15283         inputs_p = true;
15284
15285       /* Look for inputs.  */
15286       if (inputs_p
15287           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15288         {
15289           /* Consume the `:' or `::'.  */
15290           cp_lexer_consume_token (parser->lexer);
15291           /* Parse the output-operands.  */
15292           if (cp_lexer_next_token_is_not (parser->lexer,
15293                                           CPP_COLON)
15294               && cp_lexer_next_token_is_not (parser->lexer,
15295                                              CPP_SCOPE)
15296               && cp_lexer_next_token_is_not (parser->lexer,
15297                                              CPP_CLOSE_PAREN))
15298             inputs = cp_parser_asm_operand_list (parser);
15299
15300             if (inputs == error_mark_node)
15301               invalid_inputs_p = true;
15302         }
15303       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15304         /* The clobbers are coming next.  */
15305         clobbers_p = true;
15306
15307       /* Look for clobbers.  */
15308       if (clobbers_p
15309           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15310         {
15311           clobbers_p = true;
15312           /* Consume the `:' or `::'.  */
15313           cp_lexer_consume_token (parser->lexer);
15314           /* Parse the clobbers.  */
15315           if (cp_lexer_next_token_is_not (parser->lexer,
15316                                           CPP_COLON)
15317               && cp_lexer_next_token_is_not (parser->lexer,
15318                                              CPP_CLOSE_PAREN))
15319             clobbers = cp_parser_asm_clobber_list (parser);
15320         }
15321       else if (goto_p
15322                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15323         /* The labels are coming next.  */
15324         labels_p = true;
15325
15326       /* Look for labels.  */
15327       if (labels_p
15328           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15329         {
15330           labels_p = true;
15331           /* Consume the `:' or `::'.  */
15332           cp_lexer_consume_token (parser->lexer);
15333           /* Parse the labels.  */
15334           labels = cp_parser_asm_label_list (parser);
15335         }
15336
15337       if (goto_p && !labels_p)
15338         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15339     }
15340   else if (goto_p)
15341     missing = RT_COLON_SCOPE;
15342
15343   /* Look for the closing `)'.  */
15344   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15345                           missing ? missing : RT_CLOSE_PAREN))
15346     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15347                                            /*consume_paren=*/true);
15348   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15349
15350   if (!invalid_inputs_p && !invalid_outputs_p)
15351     {
15352       /* Create the ASM_EXPR.  */
15353       if (parser->in_function_body)
15354         {
15355           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15356                                       inputs, clobbers, labels);
15357           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15358           if (!extended_p)
15359             {
15360               tree temp = asm_stmt;
15361               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15362                 temp = TREE_OPERAND (temp, 0);
15363
15364               ASM_INPUT_P (temp) = 1;
15365             }
15366         }
15367       else
15368         cgraph_add_asm_node (string);
15369     }
15370 }
15371
15372 /* Declarators [gram.dcl.decl] */
15373
15374 /* Parse an init-declarator.
15375
15376    init-declarator:
15377      declarator initializer [opt]
15378
15379    GNU Extension:
15380
15381    init-declarator:
15382      declarator asm-specification [opt] attributes [opt] initializer [opt]
15383
15384    function-definition:
15385      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15386        function-body
15387      decl-specifier-seq [opt] declarator function-try-block
15388
15389    GNU Extension:
15390
15391    function-definition:
15392      __extension__ function-definition
15393
15394    TM Extension:
15395
15396    function-definition:
15397      decl-specifier-seq [opt] declarator function-transaction-block
15398
15399    The DECL_SPECIFIERS apply to this declarator.  Returns a
15400    representation of the entity declared.  If MEMBER_P is TRUE, then
15401    this declarator appears in a class scope.  The new DECL created by
15402    this declarator is returned.
15403
15404    The CHECKS are access checks that should be performed once we know
15405    what entity is being declared (and, therefore, what classes have
15406    befriended it).
15407
15408    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15409    for a function-definition here as well.  If the declarator is a
15410    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15411    be TRUE upon return.  By that point, the function-definition will
15412    have been completely parsed.
15413
15414    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15415    is FALSE.
15416
15417    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15418    parsed declaration if it is an uninitialized single declarator not followed
15419    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15420    if present, will not be consumed.  If returned, this declarator will be
15421    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15422
15423 static tree
15424 cp_parser_init_declarator (cp_parser* parser,
15425                            cp_decl_specifier_seq *decl_specifiers,
15426                            VEC (deferred_access_check,gc)* checks,
15427                            bool function_definition_allowed_p,
15428                            bool member_p,
15429                            int declares_class_or_enum,
15430                            bool* function_definition_p,
15431                            tree* maybe_range_for_decl)
15432 {
15433   cp_token *token = NULL, *asm_spec_start_token = NULL,
15434            *attributes_start_token = NULL;
15435   cp_declarator *declarator;
15436   tree prefix_attributes;
15437   tree attributes;
15438   tree asm_specification;
15439   tree initializer;
15440   tree decl = NULL_TREE;
15441   tree scope;
15442   int is_initialized;
15443   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15444      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15445      "(...)".  */
15446   enum cpp_ttype initialization_kind;
15447   bool is_direct_init = false;
15448   bool is_non_constant_init;
15449   int ctor_dtor_or_conv_p;
15450   bool friend_p;
15451   tree pushed_scope = NULL_TREE;
15452   bool range_for_decl_p = false;
15453
15454   /* Gather the attributes that were provided with the
15455      decl-specifiers.  */
15456   prefix_attributes = decl_specifiers->attributes;
15457
15458   /* Assume that this is not the declarator for a function
15459      definition.  */
15460   if (function_definition_p)
15461     *function_definition_p = false;
15462
15463   /* Defer access checks while parsing the declarator; we cannot know
15464      what names are accessible until we know what is being
15465      declared.  */
15466   resume_deferring_access_checks ();
15467
15468   /* Parse the declarator.  */
15469   token = cp_lexer_peek_token (parser->lexer);
15470   declarator
15471     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15472                             &ctor_dtor_or_conv_p,
15473                             /*parenthesized_p=*/NULL,
15474                             member_p);
15475   /* Gather up the deferred checks.  */
15476   stop_deferring_access_checks ();
15477
15478   /* If the DECLARATOR was erroneous, there's no need to go
15479      further.  */
15480   if (declarator == cp_error_declarator)
15481     return error_mark_node;
15482
15483   /* Check that the number of template-parameter-lists is OK.  */
15484   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15485                                                        token->location))
15486     return error_mark_node;
15487
15488   if (declares_class_or_enum & 2)
15489     cp_parser_check_for_definition_in_return_type (declarator,
15490                                                    decl_specifiers->type,
15491                                                    decl_specifiers->type_location);
15492
15493   /* Figure out what scope the entity declared by the DECLARATOR is
15494      located in.  `grokdeclarator' sometimes changes the scope, so
15495      we compute it now.  */
15496   scope = get_scope_of_declarator (declarator);
15497
15498   /* Perform any lookups in the declared type which were thought to be
15499      dependent, but are not in the scope of the declarator.  */
15500   decl_specifiers->type
15501     = maybe_update_decl_type (decl_specifiers->type, scope);
15502
15503   /* If we're allowing GNU extensions, look for an asm-specification
15504      and attributes.  */
15505   if (cp_parser_allow_gnu_extensions_p (parser))
15506     {
15507       /* Look for an asm-specification.  */
15508       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15509       asm_specification = cp_parser_asm_specification_opt (parser);
15510       /* And attributes.  */
15511       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15512       attributes = cp_parser_attributes_opt (parser);
15513     }
15514   else
15515     {
15516       asm_specification = NULL_TREE;
15517       attributes = NULL_TREE;
15518     }
15519
15520   /* Peek at the next token.  */
15521   token = cp_lexer_peek_token (parser->lexer);
15522   /* Check to see if the token indicates the start of a
15523      function-definition.  */
15524   if (function_declarator_p (declarator)
15525       && cp_parser_token_starts_function_definition_p (token))
15526     {
15527       if (!function_definition_allowed_p)
15528         {
15529           /* If a function-definition should not appear here, issue an
15530              error message.  */
15531           cp_parser_error (parser,
15532                            "a function-definition is not allowed here");
15533           return error_mark_node;
15534         }
15535       else
15536         {
15537           location_t func_brace_location
15538             = cp_lexer_peek_token (parser->lexer)->location;
15539
15540           /* Neither attributes nor an asm-specification are allowed
15541              on a function-definition.  */
15542           if (asm_specification)
15543             error_at (asm_spec_start_token->location,
15544                       "an asm-specification is not allowed "
15545                       "on a function-definition");
15546           if (attributes)
15547             error_at (attributes_start_token->location,
15548                       "attributes are not allowed on a function-definition");
15549           /* This is a function-definition.  */
15550           *function_definition_p = true;
15551
15552           /* Parse the function definition.  */
15553           if (member_p)
15554             decl = cp_parser_save_member_function_body (parser,
15555                                                         decl_specifiers,
15556                                                         declarator,
15557                                                         prefix_attributes);
15558           else
15559             decl
15560               = (cp_parser_function_definition_from_specifiers_and_declarator
15561                  (parser, decl_specifiers, prefix_attributes, declarator));
15562
15563           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15564             {
15565               /* This is where the prologue starts...  */
15566               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15567                 = func_brace_location;
15568             }
15569
15570           return decl;
15571         }
15572     }
15573
15574   /* [dcl.dcl]
15575
15576      Only in function declarations for constructors, destructors, and
15577      type conversions can the decl-specifier-seq be omitted.
15578
15579      We explicitly postpone this check past the point where we handle
15580      function-definitions because we tolerate function-definitions
15581      that are missing their return types in some modes.  */
15582   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15583     {
15584       cp_parser_error (parser,
15585                        "expected constructor, destructor, or type conversion");
15586       return error_mark_node;
15587     }
15588
15589   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15590   if (token->type == CPP_EQ
15591       || token->type == CPP_OPEN_PAREN
15592       || token->type == CPP_OPEN_BRACE)
15593     {
15594       is_initialized = SD_INITIALIZED;
15595       initialization_kind = token->type;
15596       if (maybe_range_for_decl)
15597         *maybe_range_for_decl = error_mark_node;
15598
15599       if (token->type == CPP_EQ
15600           && function_declarator_p (declarator))
15601         {
15602           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15603           if (t2->keyword == RID_DEFAULT)
15604             is_initialized = SD_DEFAULTED;
15605           else if (t2->keyword == RID_DELETE)
15606             is_initialized = SD_DELETED;
15607         }
15608     }
15609   else
15610     {
15611       /* If the init-declarator isn't initialized and isn't followed by a
15612          `,' or `;', it's not a valid init-declarator.  */
15613       if (token->type != CPP_COMMA
15614           && token->type != CPP_SEMICOLON)
15615         {
15616           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15617             range_for_decl_p = true;
15618           else
15619             {
15620               cp_parser_error (parser, "expected initializer");
15621               return error_mark_node;
15622             }
15623         }
15624       is_initialized = SD_UNINITIALIZED;
15625       initialization_kind = CPP_EOF;
15626     }
15627
15628   /* Because start_decl has side-effects, we should only call it if we
15629      know we're going ahead.  By this point, we know that we cannot
15630      possibly be looking at any other construct.  */
15631   cp_parser_commit_to_tentative_parse (parser);
15632
15633   /* If the decl specifiers were bad, issue an error now that we're
15634      sure this was intended to be a declarator.  Then continue
15635      declaring the variable(s), as int, to try to cut down on further
15636      errors.  */
15637   if (decl_specifiers->any_specifiers_p
15638       && decl_specifiers->type == error_mark_node)
15639     {
15640       cp_parser_error (parser, "invalid type in declaration");
15641       decl_specifiers->type = integer_type_node;
15642     }
15643
15644   /* Check to see whether or not this declaration is a friend.  */
15645   friend_p = cp_parser_friend_p (decl_specifiers);
15646
15647   /* Enter the newly declared entry in the symbol table.  If we're
15648      processing a declaration in a class-specifier, we wait until
15649      after processing the initializer.  */
15650   if (!member_p)
15651     {
15652       if (parser->in_unbraced_linkage_specification_p)
15653         decl_specifiers->storage_class = sc_extern;
15654       decl = start_decl (declarator, decl_specifiers,
15655                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15656                          attributes, prefix_attributes,
15657                          &pushed_scope);
15658       /* Adjust location of decl if declarator->id_loc is more appropriate:
15659          set, and decl wasn't merged with another decl, in which case its
15660          location would be different from input_location, and more accurate.  */
15661       if (DECL_P (decl)
15662           && declarator->id_loc != UNKNOWN_LOCATION
15663           && DECL_SOURCE_LOCATION (decl) == input_location)
15664         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15665     }
15666   else if (scope)
15667     /* Enter the SCOPE.  That way unqualified names appearing in the
15668        initializer will be looked up in SCOPE.  */
15669     pushed_scope = push_scope (scope);
15670
15671   /* Perform deferred access control checks, now that we know in which
15672      SCOPE the declared entity resides.  */
15673   if (!member_p && decl)
15674     {
15675       tree saved_current_function_decl = NULL_TREE;
15676
15677       /* If the entity being declared is a function, pretend that we
15678          are in its scope.  If it is a `friend', it may have access to
15679          things that would not otherwise be accessible.  */
15680       if (TREE_CODE (decl) == FUNCTION_DECL)
15681         {
15682           saved_current_function_decl = current_function_decl;
15683           current_function_decl = decl;
15684         }
15685
15686       /* Perform access checks for template parameters.  */
15687       cp_parser_perform_template_parameter_access_checks (checks);
15688
15689       /* Perform the access control checks for the declarator and the
15690          decl-specifiers.  */
15691       perform_deferred_access_checks ();
15692
15693       /* Restore the saved value.  */
15694       if (TREE_CODE (decl) == FUNCTION_DECL)
15695         current_function_decl = saved_current_function_decl;
15696     }
15697
15698   /* Parse the initializer.  */
15699   initializer = NULL_TREE;
15700   is_direct_init = false;
15701   is_non_constant_init = true;
15702   if (is_initialized)
15703     {
15704       if (function_declarator_p (declarator))
15705         {
15706           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15707            if (initialization_kind == CPP_EQ)
15708              initializer = cp_parser_pure_specifier (parser);
15709            else
15710              {
15711                /* If the declaration was erroneous, we don't really
15712                   know what the user intended, so just silently
15713                   consume the initializer.  */
15714                if (decl != error_mark_node)
15715                  error_at (initializer_start_token->location,
15716                            "initializer provided for function");
15717                cp_parser_skip_to_closing_parenthesis (parser,
15718                                                       /*recovering=*/true,
15719                                                       /*or_comma=*/false,
15720                                                       /*consume_paren=*/true);
15721              }
15722         }
15723       else
15724         {
15725           /* We want to record the extra mangling scope for in-class
15726              initializers of class members and initializers of static data
15727              member templates.  The former involves deferring
15728              parsing of the initializer until end of class as with default
15729              arguments.  So right here we only handle the latter.  */
15730           if (!member_p && processing_template_decl)
15731             start_lambda_scope (decl);
15732           initializer = cp_parser_initializer (parser,
15733                                                &is_direct_init,
15734                                                &is_non_constant_init);
15735           if (!member_p && processing_template_decl)
15736             finish_lambda_scope ();
15737         }
15738     }
15739
15740   /* The old parser allows attributes to appear after a parenthesized
15741      initializer.  Mark Mitchell proposed removing this functionality
15742      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15743      attributes -- but ignores them.  */
15744   if (cp_parser_allow_gnu_extensions_p (parser)
15745       && initialization_kind == CPP_OPEN_PAREN)
15746     if (cp_parser_attributes_opt (parser))
15747       warning (OPT_Wattributes,
15748                "attributes after parenthesized initializer ignored");
15749
15750   /* For an in-class declaration, use `grokfield' to create the
15751      declaration.  */
15752   if (member_p)
15753     {
15754       if (pushed_scope)
15755         {
15756           pop_scope (pushed_scope);
15757           pushed_scope = NULL_TREE;
15758         }
15759       decl = grokfield (declarator, decl_specifiers,
15760                         initializer, !is_non_constant_init,
15761                         /*asmspec=*/NULL_TREE,
15762                         prefix_attributes);
15763       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15764         cp_parser_save_default_args (parser, decl);
15765     }
15766
15767   /* Finish processing the declaration.  But, skip member
15768      declarations.  */
15769   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15770     {
15771       cp_finish_decl (decl,
15772                       initializer, !is_non_constant_init,
15773                       asm_specification,
15774                       /* If the initializer is in parentheses, then this is
15775                          a direct-initialization, which means that an
15776                          `explicit' constructor is OK.  Otherwise, an
15777                          `explicit' constructor cannot be used.  */
15778                       ((is_direct_init || !is_initialized)
15779                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15780     }
15781   else if ((cxx_dialect != cxx98) && friend_p
15782            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15783     /* Core issue #226 (C++0x only): A default template-argument
15784        shall not be specified in a friend class template
15785        declaration. */
15786     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15787                              /*is_partial=*/0, /*is_friend_decl=*/1);
15788
15789   if (!friend_p && pushed_scope)
15790     pop_scope (pushed_scope);
15791
15792   return decl;
15793 }
15794
15795 /* Parse a declarator.
15796
15797    declarator:
15798      direct-declarator
15799      ptr-operator declarator
15800
15801    abstract-declarator:
15802      ptr-operator abstract-declarator [opt]
15803      direct-abstract-declarator
15804
15805    GNU Extensions:
15806
15807    declarator:
15808      attributes [opt] direct-declarator
15809      attributes [opt] ptr-operator declarator
15810
15811    abstract-declarator:
15812      attributes [opt] ptr-operator abstract-declarator [opt]
15813      attributes [opt] direct-abstract-declarator
15814
15815    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15816    detect constructor, destructor or conversion operators. It is set
15817    to -1 if the declarator is a name, and +1 if it is a
15818    function. Otherwise it is set to zero. Usually you just want to
15819    test for >0, but internally the negative value is used.
15820
15821    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15822    a decl-specifier-seq unless it declares a constructor, destructor,
15823    or conversion.  It might seem that we could check this condition in
15824    semantic analysis, rather than parsing, but that makes it difficult
15825    to handle something like `f()'.  We want to notice that there are
15826    no decl-specifiers, and therefore realize that this is an
15827    expression, not a declaration.)
15828
15829    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15830    the declarator is a direct-declarator of the form "(...)".
15831
15832    MEMBER_P is true iff this declarator is a member-declarator.  */
15833
15834 static cp_declarator *
15835 cp_parser_declarator (cp_parser* parser,
15836                       cp_parser_declarator_kind dcl_kind,
15837                       int* ctor_dtor_or_conv_p,
15838                       bool* parenthesized_p,
15839                       bool member_p)
15840 {
15841   cp_declarator *declarator;
15842   enum tree_code code;
15843   cp_cv_quals cv_quals;
15844   tree class_type;
15845   tree attributes = NULL_TREE;
15846
15847   /* Assume this is not a constructor, destructor, or type-conversion
15848      operator.  */
15849   if (ctor_dtor_or_conv_p)
15850     *ctor_dtor_or_conv_p = 0;
15851
15852   if (cp_parser_allow_gnu_extensions_p (parser))
15853     attributes = cp_parser_attributes_opt (parser);
15854
15855   /* Check for the ptr-operator production.  */
15856   cp_parser_parse_tentatively (parser);
15857   /* Parse the ptr-operator.  */
15858   code = cp_parser_ptr_operator (parser,
15859                                  &class_type,
15860                                  &cv_quals);
15861   /* If that worked, then we have a ptr-operator.  */
15862   if (cp_parser_parse_definitely (parser))
15863     {
15864       /* If a ptr-operator was found, then this declarator was not
15865          parenthesized.  */
15866       if (parenthesized_p)
15867         *parenthesized_p = true;
15868       /* The dependent declarator is optional if we are parsing an
15869          abstract-declarator.  */
15870       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15871         cp_parser_parse_tentatively (parser);
15872
15873       /* Parse the dependent declarator.  */
15874       declarator = cp_parser_declarator (parser, dcl_kind,
15875                                          /*ctor_dtor_or_conv_p=*/NULL,
15876                                          /*parenthesized_p=*/NULL,
15877                                          /*member_p=*/false);
15878
15879       /* If we are parsing an abstract-declarator, we must handle the
15880          case where the dependent declarator is absent.  */
15881       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15882           && !cp_parser_parse_definitely (parser))
15883         declarator = NULL;
15884
15885       declarator = cp_parser_make_indirect_declarator
15886         (code, class_type, cv_quals, declarator);
15887     }
15888   /* Everything else is a direct-declarator.  */
15889   else
15890     {
15891       if (parenthesized_p)
15892         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15893                                                    CPP_OPEN_PAREN);
15894       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15895                                                 ctor_dtor_or_conv_p,
15896                                                 member_p);
15897     }
15898
15899   if (attributes && declarator && declarator != cp_error_declarator)
15900     declarator->attributes = attributes;
15901
15902   return declarator;
15903 }
15904
15905 /* Parse a direct-declarator or direct-abstract-declarator.
15906
15907    direct-declarator:
15908      declarator-id
15909      direct-declarator ( parameter-declaration-clause )
15910        cv-qualifier-seq [opt]
15911        exception-specification [opt]
15912      direct-declarator [ constant-expression [opt] ]
15913      ( declarator )
15914
15915    direct-abstract-declarator:
15916      direct-abstract-declarator [opt]
15917        ( parameter-declaration-clause )
15918        cv-qualifier-seq [opt]
15919        exception-specification [opt]
15920      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15921      ( abstract-declarator )
15922
15923    Returns a representation of the declarator.  DCL_KIND is
15924    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15925    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15926    we are parsing a direct-declarator.  It is
15927    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15928    of ambiguity we prefer an abstract declarator, as per
15929    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15930    cp_parser_declarator.  */
15931
15932 static cp_declarator *
15933 cp_parser_direct_declarator (cp_parser* parser,
15934                              cp_parser_declarator_kind dcl_kind,
15935                              int* ctor_dtor_or_conv_p,
15936                              bool member_p)
15937 {
15938   cp_token *token;
15939   cp_declarator *declarator = NULL;
15940   tree scope = NULL_TREE;
15941   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15942   bool saved_in_declarator_p = parser->in_declarator_p;
15943   bool first = true;
15944   tree pushed_scope = NULL_TREE;
15945
15946   while (true)
15947     {
15948       /* Peek at the next token.  */
15949       token = cp_lexer_peek_token (parser->lexer);
15950       if (token->type == CPP_OPEN_PAREN)
15951         {
15952           /* This is either a parameter-declaration-clause, or a
15953              parenthesized declarator. When we know we are parsing a
15954              named declarator, it must be a parenthesized declarator
15955              if FIRST is true. For instance, `(int)' is a
15956              parameter-declaration-clause, with an omitted
15957              direct-abstract-declarator. But `((*))', is a
15958              parenthesized abstract declarator. Finally, when T is a
15959              template parameter `(T)' is a
15960              parameter-declaration-clause, and not a parenthesized
15961              named declarator.
15962
15963              We first try and parse a parameter-declaration-clause,
15964              and then try a nested declarator (if FIRST is true).
15965
15966              It is not an error for it not to be a
15967              parameter-declaration-clause, even when FIRST is
15968              false. Consider,
15969
15970                int i (int);
15971                int i (3);
15972
15973              The first is the declaration of a function while the
15974              second is the definition of a variable, including its
15975              initializer.
15976
15977              Having seen only the parenthesis, we cannot know which of
15978              these two alternatives should be selected.  Even more
15979              complex are examples like:
15980
15981                int i (int (a));
15982                int i (int (3));
15983
15984              The former is a function-declaration; the latter is a
15985              variable initialization.
15986
15987              Thus again, we try a parameter-declaration-clause, and if
15988              that fails, we back out and return.  */
15989
15990           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15991             {
15992               tree params;
15993               unsigned saved_num_template_parameter_lists;
15994               bool is_declarator = false;
15995               tree t;
15996
15997               /* In a member-declarator, the only valid interpretation
15998                  of a parenthesis is the start of a
15999                  parameter-declaration-clause.  (It is invalid to
16000                  initialize a static data member with a parenthesized
16001                  initializer; only the "=" form of initialization is
16002                  permitted.)  */
16003               if (!member_p)
16004                 cp_parser_parse_tentatively (parser);
16005
16006               /* Consume the `('.  */
16007               cp_lexer_consume_token (parser->lexer);
16008               if (first)
16009                 {
16010                   /* If this is going to be an abstract declarator, we're
16011                      in a declarator and we can't have default args.  */
16012                   parser->default_arg_ok_p = false;
16013                   parser->in_declarator_p = true;
16014                 }
16015
16016               /* Inside the function parameter list, surrounding
16017                  template-parameter-lists do not apply.  */
16018               saved_num_template_parameter_lists
16019                 = parser->num_template_parameter_lists;
16020               parser->num_template_parameter_lists = 0;
16021
16022               begin_scope (sk_function_parms, NULL_TREE);
16023
16024               /* Parse the parameter-declaration-clause.  */
16025               params = cp_parser_parameter_declaration_clause (parser);
16026
16027               parser->num_template_parameter_lists
16028                 = saved_num_template_parameter_lists;
16029
16030               /* Consume the `)'.  */
16031               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16032
16033               /* If all went well, parse the cv-qualifier-seq and the
16034                  exception-specification.  */
16035               if (member_p || cp_parser_parse_definitely (parser))
16036                 {
16037                   cp_cv_quals cv_quals;
16038                   cp_virt_specifiers virt_specifiers;
16039                   tree exception_specification;
16040                   tree late_return;
16041
16042                   is_declarator = true;
16043
16044                   if (ctor_dtor_or_conv_p)
16045                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16046                   first = false;
16047
16048                   /* Parse the cv-qualifier-seq.  */
16049                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16050                   /* And the exception-specification.  */
16051                   exception_specification
16052                     = cp_parser_exception_specification_opt (parser);
16053                   /* Parse the virt-specifier-seq.  */
16054                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16055
16056                   late_return = (cp_parser_late_return_type_opt
16057                                  (parser, member_p ? cv_quals : -1));
16058
16059                   /* Create the function-declarator.  */
16060                   declarator = make_call_declarator (declarator,
16061                                                      params,
16062                                                      cv_quals,
16063                                                      virt_specifiers,
16064                                                      exception_specification,
16065                                                      late_return);
16066                   /* Any subsequent parameter lists are to do with
16067                      return type, so are not those of the declared
16068                      function.  */
16069                   parser->default_arg_ok_p = false;
16070                 }
16071
16072               /* Remove the function parms from scope.  */
16073               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16074                 pop_binding (DECL_NAME (t), t);
16075               leave_scope();
16076
16077               if (is_declarator)
16078                 /* Repeat the main loop.  */
16079                 continue;
16080             }
16081
16082           /* If this is the first, we can try a parenthesized
16083              declarator.  */
16084           if (first)
16085             {
16086               bool saved_in_type_id_in_expr_p;
16087
16088               parser->default_arg_ok_p = saved_default_arg_ok_p;
16089               parser->in_declarator_p = saved_in_declarator_p;
16090
16091               /* Consume the `('.  */
16092               cp_lexer_consume_token (parser->lexer);
16093               /* Parse the nested declarator.  */
16094               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16095               parser->in_type_id_in_expr_p = true;
16096               declarator
16097                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16098                                         /*parenthesized_p=*/NULL,
16099                                         member_p);
16100               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16101               first = false;
16102               /* Expect a `)'.  */
16103               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16104                 declarator = cp_error_declarator;
16105               if (declarator == cp_error_declarator)
16106                 break;
16107
16108               goto handle_declarator;
16109             }
16110           /* Otherwise, we must be done.  */
16111           else
16112             break;
16113         }
16114       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16115                && token->type == CPP_OPEN_SQUARE)
16116         {
16117           /* Parse an array-declarator.  */
16118           tree bounds;
16119
16120           if (ctor_dtor_or_conv_p)
16121             *ctor_dtor_or_conv_p = 0;
16122
16123           first = false;
16124           parser->default_arg_ok_p = false;
16125           parser->in_declarator_p = true;
16126           /* Consume the `['.  */
16127           cp_lexer_consume_token (parser->lexer);
16128           /* Peek at the next token.  */
16129           token = cp_lexer_peek_token (parser->lexer);
16130           /* If the next token is `]', then there is no
16131              constant-expression.  */
16132           if (token->type != CPP_CLOSE_SQUARE)
16133             {
16134               bool non_constant_p;
16135
16136               bounds
16137                 = cp_parser_constant_expression (parser,
16138                                                  /*allow_non_constant=*/true,
16139                                                  &non_constant_p);
16140               if (!non_constant_p)
16141                 /* OK */;
16142               else if (error_operand_p (bounds))
16143                 /* Already gave an error.  */;
16144               else if (!parser->in_function_body
16145                        || current_binding_level->kind == sk_function_parms)
16146                 {
16147                   /* Normally, the array bound must be an integral constant
16148                      expression.  However, as an extension, we allow VLAs
16149                      in function scopes as long as they aren't part of a
16150                      parameter declaration.  */
16151                   cp_parser_error (parser,
16152                                    "array bound is not an integer constant");
16153                   bounds = error_mark_node;
16154                 }
16155               else if (processing_template_decl)
16156                 {
16157                   /* Remember this wasn't a constant-expression.  */
16158                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16159                   TREE_SIDE_EFFECTS (bounds) = 1;
16160                 }
16161             }
16162           else
16163             bounds = NULL_TREE;
16164           /* Look for the closing `]'.  */
16165           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16166             {
16167               declarator = cp_error_declarator;
16168               break;
16169             }
16170
16171           declarator = make_array_declarator (declarator, bounds);
16172         }
16173       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16174         {
16175           {
16176             tree qualifying_scope;
16177             tree unqualified_name;
16178             special_function_kind sfk;
16179             bool abstract_ok;
16180             bool pack_expansion_p = false;
16181             cp_token *declarator_id_start_token;
16182
16183             /* Parse a declarator-id */
16184             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16185             if (abstract_ok)
16186               {
16187                 cp_parser_parse_tentatively (parser);
16188
16189                 /* If we see an ellipsis, we should be looking at a
16190                    parameter pack. */
16191                 if (token->type == CPP_ELLIPSIS)
16192                   {
16193                     /* Consume the `...' */
16194                     cp_lexer_consume_token (parser->lexer);
16195
16196                     pack_expansion_p = true;
16197                   }
16198               }
16199
16200             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16201             unqualified_name
16202               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16203             qualifying_scope = parser->scope;
16204             if (abstract_ok)
16205               {
16206                 bool okay = false;
16207
16208                 if (!unqualified_name && pack_expansion_p)
16209                   {
16210                     /* Check whether an error occurred. */
16211                     okay = !cp_parser_error_occurred (parser);
16212
16213                     /* We already consumed the ellipsis to mark a
16214                        parameter pack, but we have no way to report it,
16215                        so abort the tentative parse. We will be exiting
16216                        immediately anyway. */
16217                     cp_parser_abort_tentative_parse (parser);
16218                   }
16219                 else
16220                   okay = cp_parser_parse_definitely (parser);
16221
16222                 if (!okay)
16223                   unqualified_name = error_mark_node;
16224                 else if (unqualified_name
16225                          && (qualifying_scope
16226                              || (TREE_CODE (unqualified_name)
16227                                  != IDENTIFIER_NODE)))
16228                   {
16229                     cp_parser_error (parser, "expected unqualified-id");
16230                     unqualified_name = error_mark_node;
16231                   }
16232               }
16233
16234             if (!unqualified_name)
16235               return NULL;
16236             if (unqualified_name == error_mark_node)
16237               {
16238                 declarator = cp_error_declarator;
16239                 pack_expansion_p = false;
16240                 declarator->parameter_pack_p = false;
16241                 break;
16242               }
16243
16244             if (qualifying_scope && at_namespace_scope_p ()
16245                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16246               {
16247                 /* In the declaration of a member of a template class
16248                    outside of the class itself, the SCOPE will sometimes
16249                    be a TYPENAME_TYPE.  For example, given:
16250
16251                    template <typename T>
16252                    int S<T>::R::i = 3;
16253
16254                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16255                    this context, we must resolve S<T>::R to an ordinary
16256                    type, rather than a typename type.
16257
16258                    The reason we normally avoid resolving TYPENAME_TYPEs
16259                    is that a specialization of `S' might render
16260                    `S<T>::R' not a type.  However, if `S' is
16261                    specialized, then this `i' will not be used, so there
16262                    is no harm in resolving the types here.  */
16263                 tree type;
16264
16265                 /* Resolve the TYPENAME_TYPE.  */
16266                 type = resolve_typename_type (qualifying_scope,
16267                                               /*only_current_p=*/false);
16268                 /* If that failed, the declarator is invalid.  */
16269                 if (TREE_CODE (type) == TYPENAME_TYPE)
16270                   {
16271                     if (typedef_variant_p (type))
16272                       error_at (declarator_id_start_token->location,
16273                                 "cannot define member of dependent typedef "
16274                                 "%qT", type);
16275                     else
16276                       error_at (declarator_id_start_token->location,
16277                                 "%<%T::%E%> is not a type",
16278                                 TYPE_CONTEXT (qualifying_scope),
16279                                 TYPE_IDENTIFIER (qualifying_scope));
16280                   }
16281                 qualifying_scope = type;
16282               }
16283
16284             sfk = sfk_none;
16285
16286             if (unqualified_name)
16287               {
16288                 tree class_type;
16289
16290                 if (qualifying_scope
16291                     && CLASS_TYPE_P (qualifying_scope))
16292                   class_type = qualifying_scope;
16293                 else
16294                   class_type = current_class_type;
16295
16296                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16297                   {
16298                     tree name_type = TREE_TYPE (unqualified_name);
16299                     if (class_type && same_type_p (name_type, class_type))
16300                       {
16301                         if (qualifying_scope
16302                             && CLASSTYPE_USE_TEMPLATE (name_type))
16303                           {
16304                             error_at (declarator_id_start_token->location,
16305                                       "invalid use of constructor as a template");
16306                             inform (declarator_id_start_token->location,
16307                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16308                                     "name the constructor in a qualified name",
16309                                     class_type,
16310                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16311                                     class_type, name_type);
16312                             declarator = cp_error_declarator;
16313                             break;
16314                           }
16315                         else
16316                           unqualified_name = constructor_name (class_type);
16317                       }
16318                     else
16319                       {
16320                         /* We do not attempt to print the declarator
16321                            here because we do not have enough
16322                            information about its original syntactic
16323                            form.  */
16324                         cp_parser_error (parser, "invalid declarator");
16325                         declarator = cp_error_declarator;
16326                         break;
16327                       }
16328                   }
16329
16330                 if (class_type)
16331                   {
16332                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16333                       sfk = sfk_destructor;
16334                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16335                       sfk = sfk_conversion;
16336                     else if (/* There's no way to declare a constructor
16337                                 for an anonymous type, even if the type
16338                                 got a name for linkage purposes.  */
16339                              !TYPE_WAS_ANONYMOUS (class_type)
16340                              && constructor_name_p (unqualified_name,
16341                                                     class_type))
16342                       {
16343                         unqualified_name = constructor_name (class_type);
16344                         sfk = sfk_constructor;
16345                       }
16346                     else if (is_overloaded_fn (unqualified_name)
16347                              && DECL_CONSTRUCTOR_P (get_first_fn
16348                                                     (unqualified_name)))
16349                       sfk = sfk_constructor;
16350
16351                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16352                       *ctor_dtor_or_conv_p = -1;
16353                   }
16354               }
16355             declarator = make_id_declarator (qualifying_scope,
16356                                              unqualified_name,
16357                                              sfk);
16358             declarator->id_loc = token->location;
16359             declarator->parameter_pack_p = pack_expansion_p;
16360
16361             if (pack_expansion_p)
16362               maybe_warn_variadic_templates ();
16363           }
16364
16365         handle_declarator:;
16366           scope = get_scope_of_declarator (declarator);
16367           if (scope)
16368             /* Any names that appear after the declarator-id for a
16369                member are looked up in the containing scope.  */
16370             pushed_scope = push_scope (scope);
16371           parser->in_declarator_p = true;
16372           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16373               || (declarator && declarator->kind == cdk_id))
16374             /* Default args are only allowed on function
16375                declarations.  */
16376             parser->default_arg_ok_p = saved_default_arg_ok_p;
16377           else
16378             parser->default_arg_ok_p = false;
16379
16380           first = false;
16381         }
16382       /* We're done.  */
16383       else
16384         break;
16385     }
16386
16387   /* For an abstract declarator, we might wind up with nothing at this
16388      point.  That's an error; the declarator is not optional.  */
16389   if (!declarator)
16390     cp_parser_error (parser, "expected declarator");
16391
16392   /* If we entered a scope, we must exit it now.  */
16393   if (pushed_scope)
16394     pop_scope (pushed_scope);
16395
16396   parser->default_arg_ok_p = saved_default_arg_ok_p;
16397   parser->in_declarator_p = saved_in_declarator_p;
16398
16399   return declarator;
16400 }
16401
16402 /* Parse a ptr-operator.
16403
16404    ptr-operator:
16405      * cv-qualifier-seq [opt]
16406      &
16407      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16408
16409    GNU Extension:
16410
16411    ptr-operator:
16412      & cv-qualifier-seq [opt]
16413
16414    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16415    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16416    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16417    filled in with the TYPE containing the member.  *CV_QUALS is
16418    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16419    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16420    Note that the tree codes returned by this function have nothing
16421    to do with the types of trees that will be eventually be created
16422    to represent the pointer or reference type being parsed. They are
16423    just constants with suggestive names. */
16424 static enum tree_code
16425 cp_parser_ptr_operator (cp_parser* parser,
16426                         tree* type,
16427                         cp_cv_quals *cv_quals)
16428 {
16429   enum tree_code code = ERROR_MARK;
16430   cp_token *token;
16431
16432   /* Assume that it's not a pointer-to-member.  */
16433   *type = NULL_TREE;
16434   /* And that there are no cv-qualifiers.  */
16435   *cv_quals = TYPE_UNQUALIFIED;
16436
16437   /* Peek at the next token.  */
16438   token = cp_lexer_peek_token (parser->lexer);
16439
16440   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16441   if (token->type == CPP_MULT)
16442     code = INDIRECT_REF;
16443   else if (token->type == CPP_AND)
16444     code = ADDR_EXPR;
16445   else if ((cxx_dialect != cxx98) &&
16446            token->type == CPP_AND_AND) /* C++0x only */
16447     code = NON_LVALUE_EXPR;
16448
16449   if (code != ERROR_MARK)
16450     {
16451       /* Consume the `*', `&' or `&&'.  */
16452       cp_lexer_consume_token (parser->lexer);
16453
16454       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16455          `&', if we are allowing GNU extensions.  (The only qualifier
16456          that can legally appear after `&' is `restrict', but that is
16457          enforced during semantic analysis.  */
16458       if (code == INDIRECT_REF
16459           || cp_parser_allow_gnu_extensions_p (parser))
16460         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16461     }
16462   else
16463     {
16464       /* Try the pointer-to-member case.  */
16465       cp_parser_parse_tentatively (parser);
16466       /* Look for the optional `::' operator.  */
16467       cp_parser_global_scope_opt (parser,
16468                                   /*current_scope_valid_p=*/false);
16469       /* Look for the nested-name specifier.  */
16470       token = cp_lexer_peek_token (parser->lexer);
16471       cp_parser_nested_name_specifier (parser,
16472                                        /*typename_keyword_p=*/false,
16473                                        /*check_dependency_p=*/true,
16474                                        /*type_p=*/false,
16475                                        /*is_declaration=*/false);
16476       /* If we found it, and the next token is a `*', then we are
16477          indeed looking at a pointer-to-member operator.  */
16478       if (!cp_parser_error_occurred (parser)
16479           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16480         {
16481           /* Indicate that the `*' operator was used.  */
16482           code = INDIRECT_REF;
16483
16484           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16485             error_at (token->location, "%qD is a namespace", parser->scope);
16486           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16487             error_at (token->location, "cannot form pointer to member of "
16488                       "non-class %q#T", parser->scope);
16489           else
16490             {
16491               /* The type of which the member is a member is given by the
16492                  current SCOPE.  */
16493               *type = parser->scope;
16494               /* The next name will not be qualified.  */
16495               parser->scope = NULL_TREE;
16496               parser->qualifying_scope = NULL_TREE;
16497               parser->object_scope = NULL_TREE;
16498               /* Look for the optional cv-qualifier-seq.  */
16499               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16500             }
16501         }
16502       /* If that didn't work we don't have a ptr-operator.  */
16503       if (!cp_parser_parse_definitely (parser))
16504         cp_parser_error (parser, "expected ptr-operator");
16505     }
16506
16507   return code;
16508 }
16509
16510 /* Parse an (optional) cv-qualifier-seq.
16511
16512    cv-qualifier-seq:
16513      cv-qualifier cv-qualifier-seq [opt]
16514
16515    cv-qualifier:
16516      const
16517      volatile
16518
16519    GNU Extension:
16520
16521    cv-qualifier:
16522      __restrict__
16523
16524    Returns a bitmask representing the cv-qualifiers.  */
16525
16526 static cp_cv_quals
16527 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16528 {
16529   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16530
16531   while (true)
16532     {
16533       cp_token *token;
16534       cp_cv_quals cv_qualifier;
16535
16536       /* Peek at the next token.  */
16537       token = cp_lexer_peek_token (parser->lexer);
16538       /* See if it's a cv-qualifier.  */
16539       switch (token->keyword)
16540         {
16541         case RID_CONST:
16542           cv_qualifier = TYPE_QUAL_CONST;
16543           break;
16544
16545         case RID_VOLATILE:
16546           cv_qualifier = TYPE_QUAL_VOLATILE;
16547           break;
16548
16549         case RID_RESTRICT:
16550           cv_qualifier = TYPE_QUAL_RESTRICT;
16551           break;
16552
16553         default:
16554           cv_qualifier = TYPE_UNQUALIFIED;
16555           break;
16556         }
16557
16558       if (!cv_qualifier)
16559         break;
16560
16561       if (cv_quals & cv_qualifier)
16562         {
16563           error_at (token->location, "duplicate cv-qualifier");
16564           cp_lexer_purge_token (parser->lexer);
16565         }
16566       else
16567         {
16568           cp_lexer_consume_token (parser->lexer);
16569           cv_quals |= cv_qualifier;
16570         }
16571     }
16572
16573   return cv_quals;
16574 }
16575
16576 /* Parse an (optional) virt-specifier-seq.
16577
16578    virt-specifier-seq:
16579      virt-specifier virt-specifier-seq [opt]
16580
16581    virt-specifier:
16582      override
16583      final
16584
16585    Returns a bitmask representing the virt-specifiers.  */
16586
16587 static cp_virt_specifiers
16588 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16589 {
16590   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16591
16592   while (true)
16593     {
16594       cp_token *token;
16595       cp_virt_specifiers virt_specifier;
16596
16597       /* Peek at the next token.  */
16598       token = cp_lexer_peek_token (parser->lexer);
16599       /* See if it's a virt-specifier-qualifier.  */
16600       if (token->type != CPP_NAME)
16601         break;
16602       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16603         {
16604           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16605           virt_specifier = VIRT_SPEC_OVERRIDE;
16606         }
16607       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16608         {
16609           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16610           virt_specifier = VIRT_SPEC_FINAL;
16611         }
16612       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16613         {
16614           virt_specifier = VIRT_SPEC_FINAL;
16615         }
16616       else
16617         break;
16618
16619       if (virt_specifiers & virt_specifier)
16620         {
16621           error_at (token->location, "duplicate virt-specifier");
16622           cp_lexer_purge_token (parser->lexer);
16623         }
16624       else
16625         {
16626           cp_lexer_consume_token (parser->lexer);
16627           virt_specifiers |= virt_specifier;
16628         }
16629     }
16630   return virt_specifiers;
16631 }
16632
16633 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16634    is in scope even though it isn't real.  */
16635
16636 static void
16637 inject_this_parameter (tree ctype, cp_cv_quals quals)
16638 {
16639   tree this_parm;
16640
16641   if (current_class_ptr)
16642     {
16643       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16644       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16645       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16646           && cp_type_quals (type) == quals)
16647         return;
16648     }
16649
16650   this_parm = build_this_parm (ctype, quals);
16651   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16652   current_class_ptr = NULL_TREE;
16653   current_class_ref
16654     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16655   current_class_ptr = this_parm;
16656 }
16657
16658 /* Parse a late-specified return type, if any.  This is not a separate
16659    non-terminal, but part of a function declarator, which looks like
16660
16661    -> trailing-type-specifier-seq abstract-declarator(opt)
16662
16663    Returns the type indicated by the type-id.
16664
16665    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16666    function.  */
16667
16668 static tree
16669 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16670 {
16671   cp_token *token;
16672   tree type;
16673
16674   /* Peek at the next token.  */
16675   token = cp_lexer_peek_token (parser->lexer);
16676   /* A late-specified return type is indicated by an initial '->'. */
16677   if (token->type != CPP_DEREF)
16678     return NULL_TREE;
16679
16680   /* Consume the ->.  */
16681   cp_lexer_consume_token (parser->lexer);
16682
16683   if (quals >= 0)
16684     {
16685       /* DR 1207: 'this' is in scope in the trailing return type.  */
16686       gcc_assert (current_class_ptr == NULL_TREE);
16687       inject_this_parameter (current_class_type, quals);
16688     }
16689
16690   type = cp_parser_trailing_type_id (parser);
16691
16692   if (quals >= 0)
16693     current_class_ptr = current_class_ref = NULL_TREE;
16694
16695   return type;
16696 }
16697
16698 /* Parse a declarator-id.
16699
16700    declarator-id:
16701      id-expression
16702      :: [opt] nested-name-specifier [opt] type-name
16703
16704    In the `id-expression' case, the value returned is as for
16705    cp_parser_id_expression if the id-expression was an unqualified-id.
16706    If the id-expression was a qualified-id, then a SCOPE_REF is
16707    returned.  The first operand is the scope (either a NAMESPACE_DECL
16708    or TREE_TYPE), but the second is still just a representation of an
16709    unqualified-id.  */
16710
16711 static tree
16712 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16713 {
16714   tree id;
16715   /* The expression must be an id-expression.  Assume that qualified
16716      names are the names of types so that:
16717
16718        template <class T>
16719        int S<T>::R::i = 3;
16720
16721      will work; we must treat `S<T>::R' as the name of a type.
16722      Similarly, assume that qualified names are templates, where
16723      required, so that:
16724
16725        template <class T>
16726        int S<T>::R<T>::i = 3;
16727
16728      will work, too.  */
16729   id = cp_parser_id_expression (parser,
16730                                 /*template_keyword_p=*/false,
16731                                 /*check_dependency_p=*/false,
16732                                 /*template_p=*/NULL,
16733                                 /*declarator_p=*/true,
16734                                 optional_p);
16735   if (id && BASELINK_P (id))
16736     id = BASELINK_FUNCTIONS (id);
16737   return id;
16738 }
16739
16740 /* Parse a type-id.
16741
16742    type-id:
16743      type-specifier-seq abstract-declarator [opt]
16744
16745    Returns the TYPE specified.  */
16746
16747 static tree
16748 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16749                      bool is_trailing_return)
16750 {
16751   cp_decl_specifier_seq type_specifier_seq;
16752   cp_declarator *abstract_declarator;
16753
16754   /* Parse the type-specifier-seq.  */
16755   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16756                                 is_trailing_return,
16757                                 &type_specifier_seq);
16758   if (type_specifier_seq.type == error_mark_node)
16759     return error_mark_node;
16760
16761   /* There might or might not be an abstract declarator.  */
16762   cp_parser_parse_tentatively (parser);
16763   /* Look for the declarator.  */
16764   abstract_declarator
16765     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16766                             /*parenthesized_p=*/NULL,
16767                             /*member_p=*/false);
16768   /* Check to see if there really was a declarator.  */
16769   if (!cp_parser_parse_definitely (parser))
16770     abstract_declarator = NULL;
16771
16772   if (type_specifier_seq.type
16773       && type_uses_auto (type_specifier_seq.type))
16774     {
16775       /* A type-id with type 'auto' is only ok if the abstract declarator
16776          is a function declarator with a late-specified return type.  */
16777       if (abstract_declarator
16778           && abstract_declarator->kind == cdk_function
16779           && abstract_declarator->u.function.late_return_type)
16780         /* OK */;
16781       else
16782         {
16783           error ("invalid use of %<auto%>");
16784           return error_mark_node;
16785         }
16786     }
16787   
16788   return groktypename (&type_specifier_seq, abstract_declarator,
16789                        is_template_arg);
16790 }
16791
16792 static tree cp_parser_type_id (cp_parser *parser)
16793 {
16794   return cp_parser_type_id_1 (parser, false, false);
16795 }
16796
16797 static tree cp_parser_template_type_arg (cp_parser *parser)
16798 {
16799   tree r;
16800   const char *saved_message = parser->type_definition_forbidden_message;
16801   parser->type_definition_forbidden_message
16802     = G_("types may not be defined in template arguments");
16803   r = cp_parser_type_id_1 (parser, true, false);
16804   parser->type_definition_forbidden_message = saved_message;
16805   return r;
16806 }
16807
16808 static tree cp_parser_trailing_type_id (cp_parser *parser)
16809 {
16810   return cp_parser_type_id_1 (parser, false, true);
16811 }
16812
16813 /* Parse a type-specifier-seq.
16814
16815    type-specifier-seq:
16816      type-specifier type-specifier-seq [opt]
16817
16818    GNU extension:
16819
16820    type-specifier-seq:
16821      attributes type-specifier-seq [opt]
16822
16823    If IS_DECLARATION is true, we are at the start of a "condition" or
16824    exception-declaration, so we might be followed by a declarator-id.
16825
16826    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16827    i.e. we've just seen "->".
16828
16829    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16830
16831 static void
16832 cp_parser_type_specifier_seq (cp_parser* parser,
16833                               bool is_declaration,
16834                               bool is_trailing_return,
16835                               cp_decl_specifier_seq *type_specifier_seq)
16836 {
16837   bool seen_type_specifier = false;
16838   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16839   cp_token *start_token = NULL;
16840
16841   /* Clear the TYPE_SPECIFIER_SEQ.  */
16842   clear_decl_specs (type_specifier_seq);
16843
16844   /* In the context of a trailing return type, enum E { } is an
16845      elaborated-type-specifier followed by a function-body, not an
16846      enum-specifier.  */
16847   if (is_trailing_return)
16848     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16849
16850   /* Parse the type-specifiers and attributes.  */
16851   while (true)
16852     {
16853       tree type_specifier;
16854       bool is_cv_qualifier;
16855
16856       /* Check for attributes first.  */
16857       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16858         {
16859           type_specifier_seq->attributes =
16860             chainon (type_specifier_seq->attributes,
16861                      cp_parser_attributes_opt (parser));
16862           continue;
16863         }
16864
16865       /* record the token of the beginning of the type specifier seq,
16866          for error reporting purposes*/
16867      if (!start_token)
16868        start_token = cp_lexer_peek_token (parser->lexer);
16869
16870       /* Look for the type-specifier.  */
16871       type_specifier = cp_parser_type_specifier (parser,
16872                                                  flags,
16873                                                  type_specifier_seq,
16874                                                  /*is_declaration=*/false,
16875                                                  NULL,
16876                                                  &is_cv_qualifier);
16877       if (!type_specifier)
16878         {
16879           /* If the first type-specifier could not be found, this is not a
16880              type-specifier-seq at all.  */
16881           if (!seen_type_specifier)
16882             {
16883               cp_parser_error (parser, "expected type-specifier");
16884               type_specifier_seq->type = error_mark_node;
16885               return;
16886             }
16887           /* If subsequent type-specifiers could not be found, the
16888              type-specifier-seq is complete.  */
16889           break;
16890         }
16891
16892       seen_type_specifier = true;
16893       /* The standard says that a condition can be:
16894
16895             type-specifier-seq declarator = assignment-expression
16896
16897          However, given:
16898
16899            struct S {};
16900            if (int S = ...)
16901
16902          we should treat the "S" as a declarator, not as a
16903          type-specifier.  The standard doesn't say that explicitly for
16904          type-specifier-seq, but it does say that for
16905          decl-specifier-seq in an ordinary declaration.  Perhaps it
16906          would be clearer just to allow a decl-specifier-seq here, and
16907          then add a semantic restriction that if any decl-specifiers
16908          that are not type-specifiers appear, the program is invalid.  */
16909       if (is_declaration && !is_cv_qualifier)
16910         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16911     }
16912
16913   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16914 }
16915
16916 /* Parse a parameter-declaration-clause.
16917
16918    parameter-declaration-clause:
16919      parameter-declaration-list [opt] ... [opt]
16920      parameter-declaration-list , ...
16921
16922    Returns a representation for the parameter declarations.  A return
16923    value of NULL indicates a parameter-declaration-clause consisting
16924    only of an ellipsis.  */
16925
16926 static tree
16927 cp_parser_parameter_declaration_clause (cp_parser* parser)
16928 {
16929   tree parameters;
16930   cp_token *token;
16931   bool ellipsis_p;
16932   bool is_error;
16933
16934   /* Peek at the next token.  */
16935   token = cp_lexer_peek_token (parser->lexer);
16936   /* Check for trivial parameter-declaration-clauses.  */
16937   if (token->type == CPP_ELLIPSIS)
16938     {
16939       /* Consume the `...' token.  */
16940       cp_lexer_consume_token (parser->lexer);
16941       return NULL_TREE;
16942     }
16943   else if (token->type == CPP_CLOSE_PAREN)
16944     /* There are no parameters.  */
16945     {
16946 #ifndef NO_IMPLICIT_EXTERN_C
16947       if (in_system_header && current_class_type == NULL
16948           && current_lang_name == lang_name_c)
16949         return NULL_TREE;
16950       else
16951 #endif
16952         return void_list_node;
16953     }
16954   /* Check for `(void)', too, which is a special case.  */
16955   else if (token->keyword == RID_VOID
16956            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16957                == CPP_CLOSE_PAREN))
16958     {
16959       /* Consume the `void' token.  */
16960       cp_lexer_consume_token (parser->lexer);
16961       /* There are no parameters.  */
16962       return void_list_node;
16963     }
16964
16965   /* Parse the parameter-declaration-list.  */
16966   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16967   /* If a parse error occurred while parsing the
16968      parameter-declaration-list, then the entire
16969      parameter-declaration-clause is erroneous.  */
16970   if (is_error)
16971     return NULL;
16972
16973   /* Peek at the next token.  */
16974   token = cp_lexer_peek_token (parser->lexer);
16975   /* If it's a `,', the clause should terminate with an ellipsis.  */
16976   if (token->type == CPP_COMMA)
16977     {
16978       /* Consume the `,'.  */
16979       cp_lexer_consume_token (parser->lexer);
16980       /* Expect an ellipsis.  */
16981       ellipsis_p
16982         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16983     }
16984   /* It might also be `...' if the optional trailing `,' was
16985      omitted.  */
16986   else if (token->type == CPP_ELLIPSIS)
16987     {
16988       /* Consume the `...' token.  */
16989       cp_lexer_consume_token (parser->lexer);
16990       /* And remember that we saw it.  */
16991       ellipsis_p = true;
16992     }
16993   else
16994     ellipsis_p = false;
16995
16996   /* Finish the parameter list.  */
16997   if (!ellipsis_p)
16998     parameters = chainon (parameters, void_list_node);
16999
17000   return parameters;
17001 }
17002
17003 /* Parse a parameter-declaration-list.
17004
17005    parameter-declaration-list:
17006      parameter-declaration
17007      parameter-declaration-list , parameter-declaration
17008
17009    Returns a representation of the parameter-declaration-list, as for
17010    cp_parser_parameter_declaration_clause.  However, the
17011    `void_list_node' is never appended to the list.  Upon return,
17012    *IS_ERROR will be true iff an error occurred.  */
17013
17014 static tree
17015 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17016 {
17017   tree parameters = NULL_TREE;
17018   tree *tail = &parameters; 
17019   bool saved_in_unbraced_linkage_specification_p;
17020   int index = 0;
17021
17022   /* Assume all will go well.  */
17023   *is_error = false;
17024   /* The special considerations that apply to a function within an
17025      unbraced linkage specifications do not apply to the parameters
17026      to the function.  */
17027   saved_in_unbraced_linkage_specification_p 
17028     = parser->in_unbraced_linkage_specification_p;
17029   parser->in_unbraced_linkage_specification_p = false;
17030
17031   /* Look for more parameters.  */
17032   while (true)
17033     {
17034       cp_parameter_declarator *parameter;
17035       tree decl = error_mark_node;
17036       bool parenthesized_p = false;
17037       /* Parse the parameter.  */
17038       parameter
17039         = cp_parser_parameter_declaration (parser,
17040                                            /*template_parm_p=*/false,
17041                                            &parenthesized_p);
17042
17043       /* We don't know yet if the enclosing context is deprecated, so wait
17044          and warn in grokparms if appropriate.  */
17045       deprecated_state = DEPRECATED_SUPPRESS;
17046
17047       if (parameter)
17048         decl = grokdeclarator (parameter->declarator,
17049                                &parameter->decl_specifiers,
17050                                PARM,
17051                                parameter->default_argument != NULL_TREE,
17052                                &parameter->decl_specifiers.attributes);
17053
17054       deprecated_state = DEPRECATED_NORMAL;
17055
17056       /* If a parse error occurred parsing the parameter declaration,
17057          then the entire parameter-declaration-list is erroneous.  */
17058       if (decl == error_mark_node)
17059         {
17060           *is_error = true;
17061           parameters = error_mark_node;
17062           break;
17063         }
17064
17065       if (parameter->decl_specifiers.attributes)
17066         cplus_decl_attributes (&decl,
17067                                parameter->decl_specifiers.attributes,
17068                                0);
17069       if (DECL_NAME (decl))
17070         decl = pushdecl (decl);
17071
17072       if (decl != error_mark_node)
17073         {
17074           retrofit_lang_decl (decl);
17075           DECL_PARM_INDEX (decl) = ++index;
17076           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17077         }
17078
17079       /* Add the new parameter to the list.  */
17080       *tail = build_tree_list (parameter->default_argument, decl);
17081       tail = &TREE_CHAIN (*tail);
17082
17083       /* Peek at the next token.  */
17084       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17085           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17086           /* These are for Objective-C++ */
17087           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17088           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17089         /* The parameter-declaration-list is complete.  */
17090         break;
17091       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17092         {
17093           cp_token *token;
17094
17095           /* Peek at the next token.  */
17096           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17097           /* If it's an ellipsis, then the list is complete.  */
17098           if (token->type == CPP_ELLIPSIS)
17099             break;
17100           /* Otherwise, there must be more parameters.  Consume the
17101              `,'.  */
17102           cp_lexer_consume_token (parser->lexer);
17103           /* When parsing something like:
17104
17105                 int i(float f, double d)
17106
17107              we can tell after seeing the declaration for "f" that we
17108              are not looking at an initialization of a variable "i",
17109              but rather at the declaration of a function "i".
17110
17111              Due to the fact that the parsing of template arguments
17112              (as specified to a template-id) requires backtracking we
17113              cannot use this technique when inside a template argument
17114              list.  */
17115           if (!parser->in_template_argument_list_p
17116               && !parser->in_type_id_in_expr_p
17117               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17118               /* However, a parameter-declaration of the form
17119                  "foat(f)" (which is a valid declaration of a
17120                  parameter "f") can also be interpreted as an
17121                  expression (the conversion of "f" to "float").  */
17122               && !parenthesized_p)
17123             cp_parser_commit_to_tentative_parse (parser);
17124         }
17125       else
17126         {
17127           cp_parser_error (parser, "expected %<,%> or %<...%>");
17128           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17129             cp_parser_skip_to_closing_parenthesis (parser,
17130                                                    /*recovering=*/true,
17131                                                    /*or_comma=*/false,
17132                                                    /*consume_paren=*/false);
17133           break;
17134         }
17135     }
17136
17137   parser->in_unbraced_linkage_specification_p
17138     = saved_in_unbraced_linkage_specification_p;
17139
17140   return parameters;
17141 }
17142
17143 /* Parse a parameter declaration.
17144
17145    parameter-declaration:
17146      decl-specifier-seq ... [opt] declarator
17147      decl-specifier-seq declarator = assignment-expression
17148      decl-specifier-seq ... [opt] abstract-declarator [opt]
17149      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17150
17151    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17152    declares a template parameter.  (In that case, a non-nested `>'
17153    token encountered during the parsing of the assignment-expression
17154    is not interpreted as a greater-than operator.)
17155
17156    Returns a representation of the parameter, or NULL if an error
17157    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17158    true iff the declarator is of the form "(p)".  */
17159
17160 static cp_parameter_declarator *
17161 cp_parser_parameter_declaration (cp_parser *parser,
17162                                  bool template_parm_p,
17163                                  bool *parenthesized_p)
17164 {
17165   int declares_class_or_enum;
17166   cp_decl_specifier_seq decl_specifiers;
17167   cp_declarator *declarator;
17168   tree default_argument;
17169   cp_token *token = NULL, *declarator_token_start = NULL;
17170   const char *saved_message;
17171
17172   /* In a template parameter, `>' is not an operator.
17173
17174      [temp.param]
17175
17176      When parsing a default template-argument for a non-type
17177      template-parameter, the first non-nested `>' is taken as the end
17178      of the template parameter-list rather than a greater-than
17179      operator.  */
17180
17181   /* Type definitions may not appear in parameter types.  */
17182   saved_message = parser->type_definition_forbidden_message;
17183   parser->type_definition_forbidden_message
17184     = G_("types may not be defined in parameter types");
17185
17186   /* Parse the declaration-specifiers.  */
17187   cp_parser_decl_specifier_seq (parser,
17188                                 CP_PARSER_FLAGS_NONE,
17189                                 &decl_specifiers,
17190                                 &declares_class_or_enum);
17191
17192   /* Complain about missing 'typename' or other invalid type names.  */
17193   if (!decl_specifiers.any_type_specifiers_p)
17194     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17195
17196   /* If an error occurred, there's no reason to attempt to parse the
17197      rest of the declaration.  */
17198   if (cp_parser_error_occurred (parser))
17199     {
17200       parser->type_definition_forbidden_message = saved_message;
17201       return NULL;
17202     }
17203
17204   /* Peek at the next token.  */
17205   token = cp_lexer_peek_token (parser->lexer);
17206
17207   /* If the next token is a `)', `,', `=', `>', or `...', then there
17208      is no declarator. However, when variadic templates are enabled,
17209      there may be a declarator following `...'.  */
17210   if (token->type == CPP_CLOSE_PAREN
17211       || token->type == CPP_COMMA
17212       || token->type == CPP_EQ
17213       || token->type == CPP_GREATER)
17214     {
17215       declarator = NULL;
17216       if (parenthesized_p)
17217         *parenthesized_p = false;
17218     }
17219   /* Otherwise, there should be a declarator.  */
17220   else
17221     {
17222       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17223       parser->default_arg_ok_p = false;
17224
17225       /* After seeing a decl-specifier-seq, if the next token is not a
17226          "(", there is no possibility that the code is a valid
17227          expression.  Therefore, if parsing tentatively, we commit at
17228          this point.  */
17229       if (!parser->in_template_argument_list_p
17230           /* In an expression context, having seen:
17231
17232                (int((char ...
17233
17234              we cannot be sure whether we are looking at a
17235              function-type (taking a "char" as a parameter) or a cast
17236              of some object of type "char" to "int".  */
17237           && !parser->in_type_id_in_expr_p
17238           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17239           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17240           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17241         cp_parser_commit_to_tentative_parse (parser);
17242       /* Parse the declarator.  */
17243       declarator_token_start = token;
17244       declarator = cp_parser_declarator (parser,
17245                                          CP_PARSER_DECLARATOR_EITHER,
17246                                          /*ctor_dtor_or_conv_p=*/NULL,
17247                                          parenthesized_p,
17248                                          /*member_p=*/false);
17249       parser->default_arg_ok_p = saved_default_arg_ok_p;
17250       /* After the declarator, allow more attributes.  */
17251       decl_specifiers.attributes
17252         = chainon (decl_specifiers.attributes,
17253                    cp_parser_attributes_opt (parser));
17254     }
17255
17256   /* If the next token is an ellipsis, and we have not seen a
17257      declarator name, and the type of the declarator contains parameter
17258      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17259      a parameter pack expansion expression. Otherwise, leave the
17260      ellipsis for a C-style variadic function. */
17261   token = cp_lexer_peek_token (parser->lexer);
17262   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17263     {
17264       tree type = decl_specifiers.type;
17265
17266       if (type && DECL_P (type))
17267         type = TREE_TYPE (type);
17268
17269       if (type
17270           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17271           && declarator_can_be_parameter_pack (declarator)
17272           && (!declarator || !declarator->parameter_pack_p)
17273           && uses_parameter_packs (type))
17274         {
17275           /* Consume the `...'. */
17276           cp_lexer_consume_token (parser->lexer);
17277           maybe_warn_variadic_templates ();
17278           
17279           /* Build a pack expansion type */
17280           if (declarator)
17281             declarator->parameter_pack_p = true;
17282           else
17283             decl_specifiers.type = make_pack_expansion (type);
17284         }
17285     }
17286
17287   /* The restriction on defining new types applies only to the type
17288      of the parameter, not to the default argument.  */
17289   parser->type_definition_forbidden_message = saved_message;
17290
17291   /* If the next token is `=', then process a default argument.  */
17292   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17293     {
17294       token = cp_lexer_peek_token (parser->lexer);
17295       /* If we are defining a class, then the tokens that make up the
17296          default argument must be saved and processed later.  */
17297       if (!template_parm_p && at_class_scope_p ()
17298           && TYPE_BEING_DEFINED (current_class_type)
17299           && !LAMBDA_TYPE_P (current_class_type))
17300         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17301       /* Outside of a class definition, we can just parse the
17302          assignment-expression.  */
17303       else
17304         default_argument
17305           = cp_parser_default_argument (parser, template_parm_p);
17306
17307       if (!parser->default_arg_ok_p)
17308         {
17309           if (flag_permissive)
17310             warning (0, "deprecated use of default argument for parameter of non-function");
17311           else
17312             {
17313               error_at (token->location,
17314                         "default arguments are only "
17315                         "permitted for function parameters");
17316               default_argument = NULL_TREE;
17317             }
17318         }
17319       else if ((declarator && declarator->parameter_pack_p)
17320                || (decl_specifiers.type
17321                    && PACK_EXPANSION_P (decl_specifiers.type)))
17322         {
17323           /* Find the name of the parameter pack.  */     
17324           cp_declarator *id_declarator = declarator;
17325           while (id_declarator && id_declarator->kind != cdk_id)
17326             id_declarator = id_declarator->declarator;
17327           
17328           if (id_declarator && id_declarator->kind == cdk_id)
17329             error_at (declarator_token_start->location,
17330                       template_parm_p
17331                       ? G_("template parameter pack %qD "
17332                            "cannot have a default argument")
17333                       : G_("parameter pack %qD cannot have "
17334                            "a default argument"),
17335                       id_declarator->u.id.unqualified_name);
17336           else
17337             error_at (declarator_token_start->location,
17338                       template_parm_p
17339                       ? G_("template parameter pack cannot have "
17340                            "a default argument")
17341                       : G_("parameter pack cannot have a "
17342                            "default argument"));
17343
17344           default_argument = NULL_TREE;
17345         }
17346     }
17347   else
17348     default_argument = NULL_TREE;
17349
17350   return make_parameter_declarator (&decl_specifiers,
17351                                     declarator,
17352                                     default_argument);
17353 }
17354
17355 /* Parse a default argument and return it.
17356
17357    TEMPLATE_PARM_P is true if this is a default argument for a
17358    non-type template parameter.  */
17359 static tree
17360 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17361 {
17362   tree default_argument = NULL_TREE;
17363   bool saved_greater_than_is_operator_p;
17364   bool saved_local_variables_forbidden_p;
17365   bool non_constant_p, is_direct_init;
17366
17367   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17368      set correctly.  */
17369   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17370   parser->greater_than_is_operator_p = !template_parm_p;
17371   /* Local variable names (and the `this' keyword) may not
17372      appear in a default argument.  */
17373   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17374   parser->local_variables_forbidden_p = true;
17375   /* Parse the assignment-expression.  */
17376   if (template_parm_p)
17377     push_deferring_access_checks (dk_no_deferred);
17378   default_argument
17379     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17380   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17381     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17382   if (template_parm_p)
17383     pop_deferring_access_checks ();
17384   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17385   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17386
17387   return default_argument;
17388 }
17389
17390 /* Parse a function-body.
17391
17392    function-body:
17393      compound_statement  */
17394
17395 static void
17396 cp_parser_function_body (cp_parser *parser)
17397 {
17398   cp_parser_compound_statement (parser, NULL, false, true);
17399 }
17400
17401 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17402    true if a ctor-initializer was present.  */
17403
17404 static bool
17405 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17406 {
17407   tree body, list;
17408   bool ctor_initializer_p;
17409   const bool check_body_p =
17410      DECL_CONSTRUCTOR_P (current_function_decl)
17411      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17412   tree last = NULL;
17413
17414   /* Begin the function body.  */
17415   body = begin_function_body ();
17416   /* Parse the optional ctor-initializer.  */
17417   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17418
17419   /* If we're parsing a constexpr constructor definition, we need
17420      to check that the constructor body is indeed empty.  However,
17421      before we get to cp_parser_function_body lot of junk has been
17422      generated, so we can't just check that we have an empty block.
17423      Rather we take a snapshot of the outermost block, and check whether
17424      cp_parser_function_body changed its state.  */
17425   if (check_body_p)
17426     {
17427       list = body;
17428       if (TREE_CODE (list) == BIND_EXPR)
17429         list = BIND_EXPR_BODY (list);
17430       if (TREE_CODE (list) == STATEMENT_LIST
17431           && STATEMENT_LIST_TAIL (list) != NULL)
17432         last = STATEMENT_LIST_TAIL (list)->stmt;
17433     }
17434   /* Parse the function-body.  */
17435   cp_parser_function_body (parser);
17436   if (check_body_p)
17437     check_constexpr_ctor_body (last, list);
17438   /* Finish the function body.  */
17439   finish_function_body (body);
17440
17441   return ctor_initializer_p;
17442 }
17443
17444 /* Parse an initializer.
17445
17446    initializer:
17447      = initializer-clause
17448      ( expression-list )
17449
17450    Returns an expression representing the initializer.  If no
17451    initializer is present, NULL_TREE is returned.
17452
17453    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17454    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17455    set to TRUE if there is no initializer present.  If there is an
17456    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17457    is set to true; otherwise it is set to false.  */
17458
17459 static tree
17460 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17461                        bool* non_constant_p)
17462 {
17463   cp_token *token;
17464   tree init;
17465
17466   /* Peek at the next token.  */
17467   token = cp_lexer_peek_token (parser->lexer);
17468
17469   /* Let our caller know whether or not this initializer was
17470      parenthesized.  */
17471   *is_direct_init = (token->type != CPP_EQ);
17472   /* Assume that the initializer is constant.  */
17473   *non_constant_p = false;
17474
17475   if (token->type == CPP_EQ)
17476     {
17477       /* Consume the `='.  */
17478       cp_lexer_consume_token (parser->lexer);
17479       /* Parse the initializer-clause.  */
17480       init = cp_parser_initializer_clause (parser, non_constant_p);
17481     }
17482   else if (token->type == CPP_OPEN_PAREN)
17483     {
17484       VEC(tree,gc) *vec;
17485       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17486                                                      /*cast_p=*/false,
17487                                                      /*allow_expansion_p=*/true,
17488                                                      non_constant_p);
17489       if (vec == NULL)
17490         return error_mark_node;
17491       init = build_tree_list_vec (vec);
17492       release_tree_vector (vec);
17493     }
17494   else if (token->type == CPP_OPEN_BRACE)
17495     {
17496       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17497       init = cp_parser_braced_list (parser, non_constant_p);
17498       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17499     }
17500   else
17501     {
17502       /* Anything else is an error.  */
17503       cp_parser_error (parser, "expected initializer");
17504       init = error_mark_node;
17505     }
17506
17507   return init;
17508 }
17509
17510 /* Parse an initializer-clause.
17511
17512    initializer-clause:
17513      assignment-expression
17514      braced-init-list
17515
17516    Returns an expression representing the initializer.
17517
17518    If the `assignment-expression' production is used the value
17519    returned is simply a representation for the expression.
17520
17521    Otherwise, calls cp_parser_braced_list.  */
17522
17523 static tree
17524 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17525 {
17526   tree initializer;
17527
17528   /* Assume the expression is constant.  */
17529   *non_constant_p = false;
17530
17531   /* If it is not a `{', then we are looking at an
17532      assignment-expression.  */
17533   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17534     {
17535       initializer
17536         = cp_parser_constant_expression (parser,
17537                                         /*allow_non_constant_p=*/true,
17538                                         non_constant_p);
17539     }
17540   else
17541     initializer = cp_parser_braced_list (parser, non_constant_p);
17542
17543   return initializer;
17544 }
17545
17546 /* Parse a brace-enclosed initializer list.
17547
17548    braced-init-list:
17549      { initializer-list , [opt] }
17550      { }
17551
17552    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17553    the elements of the initializer-list (or NULL, if the last
17554    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17555    NULL_TREE.  There is no way to detect whether or not the optional
17556    trailing `,' was provided.  NON_CONSTANT_P is as for
17557    cp_parser_initializer.  */     
17558
17559 static tree
17560 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17561 {
17562   tree initializer;
17563
17564   /* Consume the `{' token.  */
17565   cp_lexer_consume_token (parser->lexer);
17566   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17567   initializer = make_node (CONSTRUCTOR);
17568   /* If it's not a `}', then there is a non-trivial initializer.  */
17569   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17570     {
17571       /* Parse the initializer list.  */
17572       CONSTRUCTOR_ELTS (initializer)
17573         = cp_parser_initializer_list (parser, non_constant_p);
17574       /* A trailing `,' token is allowed.  */
17575       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17576         cp_lexer_consume_token (parser->lexer);
17577     }
17578   /* Now, there should be a trailing `}'.  */
17579   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17580   TREE_TYPE (initializer) = init_list_type_node;
17581   return initializer;
17582 }
17583
17584 /* Parse an initializer-list.
17585
17586    initializer-list:
17587      initializer-clause ... [opt]
17588      initializer-list , initializer-clause ... [opt]
17589
17590    GNU Extension:
17591
17592    initializer-list:
17593      designation initializer-clause ...[opt]
17594      initializer-list , designation initializer-clause ...[opt]
17595
17596    designation:
17597      . identifier =
17598      identifier :
17599      [ constant-expression ] =
17600
17601    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17602    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17603    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17604    as for cp_parser_initializer.  */
17605
17606 static VEC(constructor_elt,gc) *
17607 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17608 {
17609   VEC(constructor_elt,gc) *v = NULL;
17610
17611   /* Assume all of the expressions are constant.  */
17612   *non_constant_p = false;
17613
17614   /* Parse the rest of the list.  */
17615   while (true)
17616     {
17617       cp_token *token;
17618       tree designator;
17619       tree initializer;
17620       bool clause_non_constant_p;
17621
17622       /* If the next token is an identifier and the following one is a
17623          colon, we are looking at the GNU designated-initializer
17624          syntax.  */
17625       if (cp_parser_allow_gnu_extensions_p (parser)
17626           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17627           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17628         {
17629           /* Warn the user that they are using an extension.  */
17630           pedwarn (input_location, OPT_pedantic, 
17631                    "ISO C++ does not allow designated initializers");
17632           /* Consume the identifier.  */
17633           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17634           /* Consume the `:'.  */
17635           cp_lexer_consume_token (parser->lexer);
17636         }
17637       /* Also handle the C99 syntax, '. id ='.  */
17638       else if (cp_parser_allow_gnu_extensions_p (parser)
17639                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17640                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17641                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17642         {
17643           /* Warn the user that they are using an extension.  */
17644           pedwarn (input_location, OPT_pedantic,
17645                    "ISO C++ does not allow C99 designated initializers");
17646           /* Consume the `.'.  */
17647           cp_lexer_consume_token (parser->lexer);
17648           /* Consume the identifier.  */
17649           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17650           /* Consume the `='.  */
17651           cp_lexer_consume_token (parser->lexer);
17652         }
17653       /* Also handle C99 array designators, '[ const ] ='.  */
17654       else if (cp_parser_allow_gnu_extensions_p (parser)
17655                && !c_dialect_objc ()
17656                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17657         {
17658           /* In C++11, [ could start a lambda-introducer.  */
17659           cp_parser_parse_tentatively (parser);
17660           cp_lexer_consume_token (parser->lexer);
17661           designator = cp_parser_constant_expression (parser, false, NULL);
17662           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17663           cp_parser_require (parser, CPP_EQ, RT_EQ);
17664           if (!cp_parser_parse_definitely (parser))
17665             designator = NULL_TREE;
17666         }
17667       else
17668         designator = NULL_TREE;
17669
17670       /* Parse the initializer.  */
17671       initializer = cp_parser_initializer_clause (parser,
17672                                                   &clause_non_constant_p);
17673       /* If any clause is non-constant, so is the entire initializer.  */
17674       if (clause_non_constant_p)
17675         *non_constant_p = true;
17676
17677       /* If we have an ellipsis, this is an initializer pack
17678          expansion.  */
17679       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17680         {
17681           /* Consume the `...'.  */
17682           cp_lexer_consume_token (parser->lexer);
17683
17684           /* Turn the initializer into an initializer expansion.  */
17685           initializer = make_pack_expansion (initializer);
17686         }
17687
17688       /* Add it to the vector.  */
17689       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17690
17691       /* If the next token is not a comma, we have reached the end of
17692          the list.  */
17693       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17694         break;
17695
17696       /* Peek at the next token.  */
17697       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17698       /* If the next token is a `}', then we're still done.  An
17699          initializer-clause can have a trailing `,' after the
17700          initializer-list and before the closing `}'.  */
17701       if (token->type == CPP_CLOSE_BRACE)
17702         break;
17703
17704       /* Consume the `,' token.  */
17705       cp_lexer_consume_token (parser->lexer);
17706     }
17707
17708   return v;
17709 }
17710
17711 /* Classes [gram.class] */
17712
17713 /* Parse a class-name.
17714
17715    class-name:
17716      identifier
17717      template-id
17718
17719    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17720    to indicate that names looked up in dependent types should be
17721    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17722    keyword has been used to indicate that the name that appears next
17723    is a template.  TAG_TYPE indicates the explicit tag given before
17724    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17725    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17726    is the class being defined in a class-head.
17727
17728    Returns the TYPE_DECL representing the class.  */
17729
17730 static tree
17731 cp_parser_class_name (cp_parser *parser,
17732                       bool typename_keyword_p,
17733                       bool template_keyword_p,
17734                       enum tag_types tag_type,
17735                       bool check_dependency_p,
17736                       bool class_head_p,
17737                       bool is_declaration)
17738 {
17739   tree decl;
17740   tree scope;
17741   bool typename_p;
17742   cp_token *token;
17743   tree identifier = NULL_TREE;
17744
17745   /* All class-names start with an identifier.  */
17746   token = cp_lexer_peek_token (parser->lexer);
17747   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17748     {
17749       cp_parser_error (parser, "expected class-name");
17750       return error_mark_node;
17751     }
17752
17753   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17754      to a template-id, so we save it here.  */
17755   scope = parser->scope;
17756   if (scope == error_mark_node)
17757     return error_mark_node;
17758
17759   /* Any name names a type if we're following the `typename' keyword
17760      in a qualified name where the enclosing scope is type-dependent.  */
17761   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17762                 && dependent_type_p (scope));
17763   /* Handle the common case (an identifier, but not a template-id)
17764      efficiently.  */
17765   if (token->type == CPP_NAME
17766       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17767     {
17768       cp_token *identifier_token;
17769       bool ambiguous_p;
17770
17771       /* Look for the identifier.  */
17772       identifier_token = cp_lexer_peek_token (parser->lexer);
17773       ambiguous_p = identifier_token->ambiguous_p;
17774       identifier = cp_parser_identifier (parser);
17775       /* If the next token isn't an identifier, we are certainly not
17776          looking at a class-name.  */
17777       if (identifier == error_mark_node)
17778         decl = error_mark_node;
17779       /* If we know this is a type-name, there's no need to look it
17780          up.  */
17781       else if (typename_p)
17782         decl = identifier;
17783       else
17784         {
17785           tree ambiguous_decls;
17786           /* If we already know that this lookup is ambiguous, then
17787              we've already issued an error message; there's no reason
17788              to check again.  */
17789           if (ambiguous_p)
17790             {
17791               cp_parser_simulate_error (parser);
17792               return error_mark_node;
17793             }
17794           /* If the next token is a `::', then the name must be a type
17795              name.
17796
17797              [basic.lookup.qual]
17798
17799              During the lookup for a name preceding the :: scope
17800              resolution operator, object, function, and enumerator
17801              names are ignored.  */
17802           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17803             tag_type = typename_type;
17804           /* Look up the name.  */
17805           decl = cp_parser_lookup_name (parser, identifier,
17806                                         tag_type,
17807                                         /*is_template=*/false,
17808                                         /*is_namespace=*/false,
17809                                         check_dependency_p,
17810                                         &ambiguous_decls,
17811                                         identifier_token->location);
17812           if (ambiguous_decls)
17813             {
17814               if (cp_parser_parsing_tentatively (parser))
17815                 cp_parser_simulate_error (parser);
17816               return error_mark_node;
17817             }
17818         }
17819     }
17820   else
17821     {
17822       /* Try a template-id.  */
17823       decl = cp_parser_template_id (parser, template_keyword_p,
17824                                     check_dependency_p,
17825                                     is_declaration);
17826       if (decl == error_mark_node)
17827         return error_mark_node;
17828     }
17829
17830   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17831
17832   /* If this is a typename, create a TYPENAME_TYPE.  */
17833   if (typename_p && decl != error_mark_node)
17834     {
17835       decl = make_typename_type (scope, decl, typename_type,
17836                                  /*complain=*/tf_error);
17837       if (decl != error_mark_node)
17838         decl = TYPE_NAME (decl);
17839     }
17840
17841   /* Check to see that it is really the name of a class.  */
17842   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17843       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17844       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17845     /* Situations like this:
17846
17847          template <typename T> struct A {
17848            typename T::template X<int>::I i;
17849          };
17850
17851        are problematic.  Is `T::template X<int>' a class-name?  The
17852        standard does not seem to be definitive, but there is no other
17853        valid interpretation of the following `::'.  Therefore, those
17854        names are considered class-names.  */
17855     {
17856       decl = make_typename_type (scope, decl, tag_type, tf_error);
17857       if (decl != error_mark_node)
17858         decl = TYPE_NAME (decl);
17859     }
17860   else if (TREE_CODE (decl) != TYPE_DECL
17861            || TREE_TYPE (decl) == error_mark_node
17862            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17863            /* In Objective-C 2.0, a classname followed by '.' starts a
17864               dot-syntax expression, and it's not a type-name.  */
17865            || (c_dialect_objc ()
17866                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17867                && objc_is_class_name (decl)))
17868     decl = error_mark_node;
17869
17870   if (decl == error_mark_node)
17871     cp_parser_error (parser, "expected class-name");
17872   else if (identifier && !parser->scope)
17873     maybe_note_name_used_in_class (identifier, decl);
17874
17875   return decl;
17876 }
17877
17878 /* Parse a class-specifier.
17879
17880    class-specifier:
17881      class-head { member-specification [opt] }
17882
17883    Returns the TREE_TYPE representing the class.  */
17884
17885 static tree
17886 cp_parser_class_specifier_1 (cp_parser* parser)
17887 {
17888   tree type;
17889   tree attributes = NULL_TREE;
17890   bool nested_name_specifier_p;
17891   unsigned saved_num_template_parameter_lists;
17892   bool saved_in_function_body;
17893   unsigned char in_statement;
17894   bool in_switch_statement_p;
17895   bool saved_in_unbraced_linkage_specification_p;
17896   tree old_scope = NULL_TREE;
17897   tree scope = NULL_TREE;
17898   tree bases;
17899   cp_token *closing_brace;
17900
17901   push_deferring_access_checks (dk_no_deferred);
17902
17903   /* Parse the class-head.  */
17904   type = cp_parser_class_head (parser,
17905                                &nested_name_specifier_p,
17906                                &attributes,
17907                                &bases);
17908   /* If the class-head was a semantic disaster, skip the entire body
17909      of the class.  */
17910   if (!type)
17911     {
17912       cp_parser_skip_to_end_of_block_or_statement (parser);
17913       pop_deferring_access_checks ();
17914       return error_mark_node;
17915     }
17916
17917   /* Look for the `{'.  */
17918   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17919     {
17920       pop_deferring_access_checks ();
17921       return error_mark_node;
17922     }
17923
17924   /* Process the base classes. If they're invalid, skip the 
17925      entire class body.  */
17926   if (!xref_basetypes (type, bases))
17927     {
17928       /* Consuming the closing brace yields better error messages
17929          later on.  */
17930       if (cp_parser_skip_to_closing_brace (parser))
17931         cp_lexer_consume_token (parser->lexer);
17932       pop_deferring_access_checks ();
17933       return error_mark_node;
17934     }
17935
17936   /* Issue an error message if type-definitions are forbidden here.  */
17937   cp_parser_check_type_definition (parser);
17938   /* Remember that we are defining one more class.  */
17939   ++parser->num_classes_being_defined;
17940   /* Inside the class, surrounding template-parameter-lists do not
17941      apply.  */
17942   saved_num_template_parameter_lists
17943     = parser->num_template_parameter_lists;
17944   parser->num_template_parameter_lists = 0;
17945   /* We are not in a function body.  */
17946   saved_in_function_body = parser->in_function_body;
17947   parser->in_function_body = false;
17948   /* Or in a loop.  */
17949   in_statement = parser->in_statement;
17950   parser->in_statement = 0;
17951   /* Or in a switch.  */
17952   in_switch_statement_p = parser->in_switch_statement_p;
17953   parser->in_switch_statement_p = false;
17954   /* We are not immediately inside an extern "lang" block.  */
17955   saved_in_unbraced_linkage_specification_p
17956     = parser->in_unbraced_linkage_specification_p;
17957   parser->in_unbraced_linkage_specification_p = false;
17958
17959   /* Start the class.  */
17960   if (nested_name_specifier_p)
17961     {
17962       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17963       old_scope = push_inner_scope (scope);
17964     }
17965   type = begin_class_definition (type, attributes);
17966
17967   if (type == error_mark_node)
17968     /* If the type is erroneous, skip the entire body of the class.  */
17969     cp_parser_skip_to_closing_brace (parser);
17970   else
17971     /* Parse the member-specification.  */
17972     cp_parser_member_specification_opt (parser);
17973
17974   /* Look for the trailing `}'.  */
17975   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17976   /* Look for trailing attributes to apply to this class.  */
17977   if (cp_parser_allow_gnu_extensions_p (parser))
17978     attributes = cp_parser_attributes_opt (parser);
17979   if (type != error_mark_node)
17980     type = finish_struct (type, attributes);
17981   if (nested_name_specifier_p)
17982     pop_inner_scope (old_scope, scope);
17983
17984   /* We've finished a type definition.  Check for the common syntax
17985      error of forgetting a semicolon after the definition.  We need to
17986      be careful, as we can't just check for not-a-semicolon and be done
17987      with it; the user might have typed:
17988
17989      class X { } c = ...;
17990      class X { } *p = ...;
17991
17992      and so forth.  Instead, enumerate all the possible tokens that
17993      might follow this production; if we don't see one of them, then
17994      complain and silently insert the semicolon.  */
17995   {
17996     cp_token *token = cp_lexer_peek_token (parser->lexer);
17997     bool want_semicolon = true;
17998
17999     switch (token->type)
18000       {
18001       case CPP_NAME:
18002       case CPP_SEMICOLON:
18003       case CPP_MULT:
18004       case CPP_AND:
18005       case CPP_OPEN_PAREN:
18006       case CPP_CLOSE_PAREN:
18007       case CPP_COMMA:
18008         want_semicolon = false;
18009         break;
18010
18011         /* While it's legal for type qualifiers and storage class
18012            specifiers to follow type definitions in the grammar, only
18013            compiler testsuites contain code like that.  Assume that if
18014            we see such code, then what we're really seeing is a case
18015            like:
18016
18017            class X { }
18018            const <type> var = ...;
18019
18020            or
18021
18022            class Y { }
18023            static <type> func (...) ...
18024
18025            i.e. the qualifier or specifier applies to the next
18026            declaration.  To do so, however, we need to look ahead one
18027            more token to see if *that* token is a type specifier.
18028
18029            This code could be improved to handle:
18030
18031            class Z { }
18032            static const <type> var = ...;  */
18033       case CPP_KEYWORD:
18034         if (keyword_is_decl_specifier (token->keyword))
18035           {
18036             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18037
18038             /* Handling user-defined types here would be nice, but very
18039                tricky.  */
18040             want_semicolon
18041               = (lookahead->type == CPP_KEYWORD
18042                  && keyword_begins_type_specifier (lookahead->keyword));
18043           }
18044         break;
18045       default:
18046         break;
18047       }
18048
18049     /* If we don't have a type, then something is very wrong and we
18050        shouldn't try to do anything clever.  Likewise for not seeing the
18051        closing brace.  */
18052     if (closing_brace && TYPE_P (type) && want_semicolon)
18053       {
18054         cp_token_position prev
18055           = cp_lexer_previous_token_position (parser->lexer);
18056         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18057         location_t loc = prev_token->location;
18058
18059         if (CLASSTYPE_DECLARED_CLASS (type))
18060           error_at (loc, "expected %<;%> after class definition");
18061         else if (TREE_CODE (type) == RECORD_TYPE)
18062           error_at (loc, "expected %<;%> after struct definition");
18063         else if (TREE_CODE (type) == UNION_TYPE)
18064           error_at (loc, "expected %<;%> after union definition");
18065         else
18066           gcc_unreachable ();
18067
18068         /* Unget one token and smash it to look as though we encountered
18069            a semicolon in the input stream.  */
18070         cp_lexer_set_token_position (parser->lexer, prev);
18071         token = cp_lexer_peek_token (parser->lexer);
18072         token->type = CPP_SEMICOLON;
18073         token->keyword = RID_MAX;
18074       }
18075   }
18076
18077   /* If this class is not itself within the scope of another class,
18078      then we need to parse the bodies of all of the queued function
18079      definitions.  Note that the queued functions defined in a class
18080      are not always processed immediately following the
18081      class-specifier for that class.  Consider:
18082
18083        struct A {
18084          struct B { void f() { sizeof (A); } };
18085        };
18086
18087      If `f' were processed before the processing of `A' were
18088      completed, there would be no way to compute the size of `A'.
18089      Note that the nesting we are interested in here is lexical --
18090      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18091      for:
18092
18093        struct A { struct B; };
18094        struct A::B { void f() { } };
18095
18096      there is no need to delay the parsing of `A::B::f'.  */
18097   if (--parser->num_classes_being_defined == 0)
18098     {
18099       tree decl;
18100       tree class_type = NULL_TREE;
18101       tree pushed_scope = NULL_TREE;
18102       unsigned ix;
18103       cp_default_arg_entry *e;
18104       tree save_ccp, save_ccr;
18105
18106       /* In a first pass, parse default arguments to the functions.
18107          Then, in a second pass, parse the bodies of the functions.
18108          This two-phased approach handles cases like:
18109
18110             struct S {
18111               void f() { g(); }
18112               void g(int i = 3);
18113             };
18114
18115          */
18116       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18117                         ix, e)
18118         {
18119           decl = e->decl;
18120           /* If there are default arguments that have not yet been processed,
18121              take care of them now.  */
18122           if (class_type != e->class_type)
18123             {
18124               if (pushed_scope)
18125                 pop_scope (pushed_scope);
18126               class_type = e->class_type;
18127               pushed_scope = push_scope (class_type);
18128             }
18129           /* Make sure that any template parameters are in scope.  */
18130           maybe_begin_member_template_processing (decl);
18131           /* Parse the default argument expressions.  */
18132           cp_parser_late_parsing_default_args (parser, decl);
18133           /* Remove any template parameters from the symbol table.  */
18134           maybe_end_member_template_processing ();
18135         }
18136       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18137       /* Now parse any NSDMIs.  */
18138       save_ccp = current_class_ptr;
18139       save_ccr = current_class_ref;
18140       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18141         {
18142           if (class_type != DECL_CONTEXT (decl))
18143             {
18144               if (pushed_scope)
18145                 pop_scope (pushed_scope);
18146               class_type = DECL_CONTEXT (decl);
18147               pushed_scope = push_scope (class_type);
18148             }
18149           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18150           cp_parser_late_parsing_nsdmi (parser, decl);
18151         }
18152       VEC_truncate (tree, unparsed_nsdmis, 0);
18153       current_class_ptr = save_ccp;
18154       current_class_ref = save_ccr;
18155       if (pushed_scope)
18156         pop_scope (pushed_scope);
18157       /* Now parse the body of the functions.  */
18158       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18159         cp_parser_late_parsing_for_member (parser, decl);
18160       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18161     }
18162
18163   /* Put back any saved access checks.  */
18164   pop_deferring_access_checks ();
18165
18166   /* Restore saved state.  */
18167   parser->in_switch_statement_p = in_switch_statement_p;
18168   parser->in_statement = in_statement;
18169   parser->in_function_body = saved_in_function_body;
18170   parser->num_template_parameter_lists
18171     = saved_num_template_parameter_lists;
18172   parser->in_unbraced_linkage_specification_p
18173     = saved_in_unbraced_linkage_specification_p;
18174
18175   return type;
18176 }
18177
18178 static tree
18179 cp_parser_class_specifier (cp_parser* parser)
18180 {
18181   tree ret;
18182   timevar_push (TV_PARSE_STRUCT);
18183   ret = cp_parser_class_specifier_1 (parser);
18184   timevar_pop (TV_PARSE_STRUCT);
18185   return ret;
18186 }
18187
18188 /* Parse a class-head.
18189
18190    class-head:
18191      class-key identifier [opt] base-clause [opt]
18192      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18193      class-key nested-name-specifier [opt] template-id
18194        base-clause [opt]
18195
18196    class-virt-specifier:
18197      final
18198
18199    GNU Extensions:
18200      class-key attributes identifier [opt] base-clause [opt]
18201      class-key attributes nested-name-specifier identifier base-clause [opt]
18202      class-key attributes nested-name-specifier [opt] template-id
18203        base-clause [opt]
18204
18205    Upon return BASES is initialized to the list of base classes (or
18206    NULL, if there are none) in the same form returned by
18207    cp_parser_base_clause.
18208
18209    Returns the TYPE of the indicated class.  Sets
18210    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18211    involving a nested-name-specifier was used, and FALSE otherwise.
18212
18213    Returns error_mark_node if this is not a class-head.
18214
18215    Returns NULL_TREE if the class-head is syntactically valid, but
18216    semantically invalid in a way that means we should skip the entire
18217    body of the class.  */
18218
18219 static tree
18220 cp_parser_class_head (cp_parser* parser,
18221                       bool* nested_name_specifier_p,
18222                       tree *attributes_p,
18223                       tree *bases)
18224 {
18225   tree nested_name_specifier;
18226   enum tag_types class_key;
18227   tree id = NULL_TREE;
18228   tree type = NULL_TREE;
18229   tree attributes;
18230   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18231   bool template_id_p = false;
18232   bool qualified_p = false;
18233   bool invalid_nested_name_p = false;
18234   bool invalid_explicit_specialization_p = false;
18235   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18236   tree pushed_scope = NULL_TREE;
18237   unsigned num_templates;
18238   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18239   /* Assume no nested-name-specifier will be present.  */
18240   *nested_name_specifier_p = false;
18241   /* Assume no template parameter lists will be used in defining the
18242      type.  */
18243   num_templates = 0;
18244   parser->colon_corrects_to_scope_p = false;
18245
18246   *bases = NULL_TREE;
18247
18248   /* Look for the class-key.  */
18249   class_key = cp_parser_class_key (parser);
18250   if (class_key == none_type)
18251     return error_mark_node;
18252
18253   /* Parse the attributes.  */
18254   attributes = cp_parser_attributes_opt (parser);
18255
18256   /* If the next token is `::', that is invalid -- but sometimes
18257      people do try to write:
18258
18259        struct ::S {};
18260
18261      Handle this gracefully by accepting the extra qualifier, and then
18262      issuing an error about it later if this really is a
18263      class-head.  If it turns out just to be an elaborated type
18264      specifier, remain silent.  */
18265   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18266     qualified_p = true;
18267
18268   push_deferring_access_checks (dk_no_check);
18269
18270   /* Determine the name of the class.  Begin by looking for an
18271      optional nested-name-specifier.  */
18272   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18273   nested_name_specifier
18274     = cp_parser_nested_name_specifier_opt (parser,
18275                                            /*typename_keyword_p=*/false,
18276                                            /*check_dependency_p=*/false,
18277                                            /*type_p=*/false,
18278                                            /*is_declaration=*/false);
18279   /* If there was a nested-name-specifier, then there *must* be an
18280      identifier.  */
18281   if (nested_name_specifier)
18282     {
18283       type_start_token = cp_lexer_peek_token (parser->lexer);
18284       /* Although the grammar says `identifier', it really means
18285          `class-name' or `template-name'.  You are only allowed to
18286          define a class that has already been declared with this
18287          syntax.
18288
18289          The proposed resolution for Core Issue 180 says that wherever
18290          you see `class T::X' you should treat `X' as a type-name.
18291
18292          It is OK to define an inaccessible class; for example:
18293
18294            class A { class B; };
18295            class A::B {};
18296
18297          We do not know if we will see a class-name, or a
18298          template-name.  We look for a class-name first, in case the
18299          class-name is a template-id; if we looked for the
18300          template-name first we would stop after the template-name.  */
18301       cp_parser_parse_tentatively (parser);
18302       type = cp_parser_class_name (parser,
18303                                    /*typename_keyword_p=*/false,
18304                                    /*template_keyword_p=*/false,
18305                                    class_type,
18306                                    /*check_dependency_p=*/false,
18307                                    /*class_head_p=*/true,
18308                                    /*is_declaration=*/false);
18309       /* If that didn't work, ignore the nested-name-specifier.  */
18310       if (!cp_parser_parse_definitely (parser))
18311         {
18312           invalid_nested_name_p = true;
18313           type_start_token = cp_lexer_peek_token (parser->lexer);
18314           id = cp_parser_identifier (parser);
18315           if (id == error_mark_node)
18316             id = NULL_TREE;
18317         }
18318       /* If we could not find a corresponding TYPE, treat this
18319          declaration like an unqualified declaration.  */
18320       if (type == error_mark_node)
18321         nested_name_specifier = NULL_TREE;
18322       /* Otherwise, count the number of templates used in TYPE and its
18323          containing scopes.  */
18324       else
18325         {
18326           tree scope;
18327
18328           for (scope = TREE_TYPE (type);
18329                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18330                scope = (TYPE_P (scope)
18331                         ? TYPE_CONTEXT (scope)
18332                         : DECL_CONTEXT (scope)))
18333             if (TYPE_P (scope)
18334                 && CLASS_TYPE_P (scope)
18335                 && CLASSTYPE_TEMPLATE_INFO (scope)
18336                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18337                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18338               ++num_templates;
18339         }
18340     }
18341   /* Otherwise, the identifier is optional.  */
18342   else
18343     {
18344       /* We don't know whether what comes next is a template-id,
18345          an identifier, or nothing at all.  */
18346       cp_parser_parse_tentatively (parser);
18347       /* Check for a template-id.  */
18348       type_start_token = cp_lexer_peek_token (parser->lexer);
18349       id = cp_parser_template_id (parser,
18350                                   /*template_keyword_p=*/false,
18351                                   /*check_dependency_p=*/true,
18352                                   /*is_declaration=*/true);
18353       /* If that didn't work, it could still be an identifier.  */
18354       if (!cp_parser_parse_definitely (parser))
18355         {
18356           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18357             {
18358               type_start_token = cp_lexer_peek_token (parser->lexer);
18359               id = cp_parser_identifier (parser);
18360             }
18361           else
18362             id = NULL_TREE;
18363         }
18364       else
18365         {
18366           template_id_p = true;
18367           ++num_templates;
18368         }
18369     }
18370
18371   pop_deferring_access_checks ();
18372
18373   if (id)
18374     {
18375       cp_parser_check_for_invalid_template_id (parser, id,
18376                                                type_start_token->location);
18377     }
18378   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18379
18380   /* If it's not a `:' or a `{' then we can't really be looking at a
18381      class-head, since a class-head only appears as part of a
18382      class-specifier.  We have to detect this situation before calling
18383      xref_tag, since that has irreversible side-effects.  */
18384   if (!cp_parser_next_token_starts_class_definition_p (parser))
18385     {
18386       cp_parser_error (parser, "expected %<{%> or %<:%>");
18387       type = error_mark_node;
18388       goto out;
18389     }
18390
18391   /* At this point, we're going ahead with the class-specifier, even
18392      if some other problem occurs.  */
18393   cp_parser_commit_to_tentative_parse (parser);
18394   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18395     {
18396       cp_parser_error (parser,
18397                        "cannot specify %<override%> for a class");
18398       type = error_mark_node;
18399       goto out;
18400     }
18401   /* Issue the error about the overly-qualified name now.  */
18402   if (qualified_p)
18403     {
18404       cp_parser_error (parser,
18405                        "global qualification of class name is invalid");
18406       type = error_mark_node;
18407       goto out;
18408     }
18409   else if (invalid_nested_name_p)
18410     {
18411       cp_parser_error (parser,
18412                        "qualified name does not name a class");
18413       type = error_mark_node;
18414       goto out;
18415     }
18416   else if (nested_name_specifier)
18417     {
18418       tree scope;
18419
18420       /* Reject typedef-names in class heads.  */
18421       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18422         {
18423           error_at (type_start_token->location,
18424                     "invalid class name in declaration of %qD",
18425                     type);
18426           type = NULL_TREE;
18427           goto done;
18428         }
18429
18430       /* Figure out in what scope the declaration is being placed.  */
18431       scope = current_scope ();
18432       /* If that scope does not contain the scope in which the
18433          class was originally declared, the program is invalid.  */
18434       if (scope && !is_ancestor (scope, nested_name_specifier))
18435         {
18436           if (at_namespace_scope_p ())
18437             error_at (type_start_token->location,
18438                       "declaration of %qD in namespace %qD which does not "
18439                       "enclose %qD",
18440                       type, scope, nested_name_specifier);
18441           else
18442             error_at (type_start_token->location,
18443                       "declaration of %qD in %qD which does not enclose %qD",
18444                       type, scope, nested_name_specifier);
18445           type = NULL_TREE;
18446           goto done;
18447         }
18448       /* [dcl.meaning]
18449
18450          A declarator-id shall not be qualified except for the
18451          definition of a ... nested class outside of its class
18452          ... [or] the definition or explicit instantiation of a
18453          class member of a namespace outside of its namespace.  */
18454       if (scope == nested_name_specifier)
18455         {
18456           permerror (nested_name_specifier_token_start->location,
18457                      "extra qualification not allowed");
18458           nested_name_specifier = NULL_TREE;
18459           num_templates = 0;
18460         }
18461     }
18462   /* An explicit-specialization must be preceded by "template <>".  If
18463      it is not, try to recover gracefully.  */
18464   if (at_namespace_scope_p ()
18465       && parser->num_template_parameter_lists == 0
18466       && template_id_p)
18467     {
18468       error_at (type_start_token->location,
18469                 "an explicit specialization must be preceded by %<template <>%>");
18470       invalid_explicit_specialization_p = true;
18471       /* Take the same action that would have been taken by
18472          cp_parser_explicit_specialization.  */
18473       ++parser->num_template_parameter_lists;
18474       begin_specialization ();
18475     }
18476   /* There must be no "return" statements between this point and the
18477      end of this function; set "type "to the correct return value and
18478      use "goto done;" to return.  */
18479   /* Make sure that the right number of template parameters were
18480      present.  */
18481   if (!cp_parser_check_template_parameters (parser, num_templates,
18482                                             type_start_token->location,
18483                                             /*declarator=*/NULL))
18484     {
18485       /* If something went wrong, there is no point in even trying to
18486          process the class-definition.  */
18487       type = NULL_TREE;
18488       goto done;
18489     }
18490
18491   /* Look up the type.  */
18492   if (template_id_p)
18493     {
18494       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18495           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18496               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18497         {
18498           error_at (type_start_token->location,
18499                     "function template %qD redeclared as a class template", id);
18500           type = error_mark_node;
18501         }
18502       else
18503         {
18504           type = TREE_TYPE (id);
18505           type = maybe_process_partial_specialization (type);
18506         }
18507       if (nested_name_specifier)
18508         pushed_scope = push_scope (nested_name_specifier);
18509     }
18510   else if (nested_name_specifier)
18511     {
18512       tree class_type;
18513
18514       /* Given:
18515
18516             template <typename T> struct S { struct T };
18517             template <typename T> struct S<T>::T { };
18518
18519          we will get a TYPENAME_TYPE when processing the definition of
18520          `S::T'.  We need to resolve it to the actual type before we
18521          try to define it.  */
18522       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18523         {
18524           class_type = resolve_typename_type (TREE_TYPE (type),
18525                                               /*only_current_p=*/false);
18526           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18527             type = TYPE_NAME (class_type);
18528           else
18529             {
18530               cp_parser_error (parser, "could not resolve typename type");
18531               type = error_mark_node;
18532             }
18533         }
18534
18535       if (maybe_process_partial_specialization (TREE_TYPE (type))
18536           == error_mark_node)
18537         {
18538           type = NULL_TREE;
18539           goto done;
18540         }
18541
18542       class_type = current_class_type;
18543       /* Enter the scope indicated by the nested-name-specifier.  */
18544       pushed_scope = push_scope (nested_name_specifier);
18545       /* Get the canonical version of this type.  */
18546       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18547       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18548           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18549         {
18550           type = push_template_decl (type);
18551           if (type == error_mark_node)
18552             {
18553               type = NULL_TREE;
18554               goto done;
18555             }
18556         }
18557
18558       type = TREE_TYPE (type);
18559       *nested_name_specifier_p = true;
18560     }
18561   else      /* The name is not a nested name.  */
18562     {
18563       /* If the class was unnamed, create a dummy name.  */
18564       if (!id)
18565         id = make_anon_name ();
18566       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18567                        parser->num_template_parameter_lists);
18568     }
18569
18570   /* Indicate whether this class was declared as a `class' or as a
18571      `struct'.  */
18572   if (TREE_CODE (type) == RECORD_TYPE)
18573     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18574   cp_parser_check_class_key (class_key, type);
18575
18576   /* If this type was already complete, and we see another definition,
18577      that's an error.  */
18578   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18579     {
18580       error_at (type_start_token->location, "redefinition of %q#T",
18581                 type);
18582       error_at (type_start_token->location, "previous definition of %q+#T",
18583                 type);
18584       type = NULL_TREE;
18585       goto done;
18586     }
18587   else if (type == error_mark_node)
18588     type = NULL_TREE;
18589
18590   /* We will have entered the scope containing the class; the names of
18591      base classes should be looked up in that context.  For example:
18592
18593        struct A { struct B {}; struct C; };
18594        struct A::C : B {};
18595
18596      is valid.  */
18597
18598   /* Get the list of base-classes, if there is one.  */
18599   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18600     *bases = cp_parser_base_clause (parser);
18601
18602  done:
18603   /* Leave the scope given by the nested-name-specifier.  We will
18604      enter the class scope itself while processing the members.  */
18605   if (pushed_scope)
18606     pop_scope (pushed_scope);
18607
18608   if (invalid_explicit_specialization_p)
18609     {
18610       end_specialization ();
18611       --parser->num_template_parameter_lists;
18612     }
18613
18614   if (type)
18615     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18616   *attributes_p = attributes;
18617   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18618     CLASSTYPE_FINAL (type) = 1;
18619  out:
18620   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18621   return type;
18622 }
18623
18624 /* Parse a class-key.
18625
18626    class-key:
18627      class
18628      struct
18629      union
18630
18631    Returns the kind of class-key specified, or none_type to indicate
18632    error.  */
18633
18634 static enum tag_types
18635 cp_parser_class_key (cp_parser* parser)
18636 {
18637   cp_token *token;
18638   enum tag_types tag_type;
18639
18640   /* Look for the class-key.  */
18641   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18642   if (!token)
18643     return none_type;
18644
18645   /* Check to see if the TOKEN is a class-key.  */
18646   tag_type = cp_parser_token_is_class_key (token);
18647   if (!tag_type)
18648     cp_parser_error (parser, "expected class-key");
18649   return tag_type;
18650 }
18651
18652 /* Parse an (optional) member-specification.
18653
18654    member-specification:
18655      member-declaration member-specification [opt]
18656      access-specifier : member-specification [opt]  */
18657
18658 static void
18659 cp_parser_member_specification_opt (cp_parser* parser)
18660 {
18661   while (true)
18662     {
18663       cp_token *token;
18664       enum rid keyword;
18665
18666       /* Peek at the next token.  */
18667       token = cp_lexer_peek_token (parser->lexer);
18668       /* If it's a `}', or EOF then we've seen all the members.  */
18669       if (token->type == CPP_CLOSE_BRACE
18670           || token->type == CPP_EOF
18671           || token->type == CPP_PRAGMA_EOL)
18672         break;
18673
18674       /* See if this token is a keyword.  */
18675       keyword = token->keyword;
18676       switch (keyword)
18677         {
18678         case RID_PUBLIC:
18679         case RID_PROTECTED:
18680         case RID_PRIVATE:
18681           /* Consume the access-specifier.  */
18682           cp_lexer_consume_token (parser->lexer);
18683           /* Remember which access-specifier is active.  */
18684           current_access_specifier = token->u.value;
18685           /* Look for the `:'.  */
18686           cp_parser_require (parser, CPP_COLON, RT_COLON);
18687           break;
18688
18689         default:
18690           /* Accept #pragmas at class scope.  */
18691           if (token->type == CPP_PRAGMA)
18692             {
18693               cp_parser_pragma (parser, pragma_external);
18694               break;
18695             }
18696
18697           /* Otherwise, the next construction must be a
18698              member-declaration.  */
18699           cp_parser_member_declaration (parser);
18700         }
18701     }
18702 }
18703
18704 /* Parse a member-declaration.
18705
18706    member-declaration:
18707      decl-specifier-seq [opt] member-declarator-list [opt] ;
18708      function-definition ; [opt]
18709      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18710      using-declaration
18711      template-declaration
18712      alias-declaration
18713
18714    member-declarator-list:
18715      member-declarator
18716      member-declarator-list , member-declarator
18717
18718    member-declarator:
18719      declarator pure-specifier [opt]
18720      declarator constant-initializer [opt]
18721      identifier [opt] : constant-expression
18722
18723    GNU Extensions:
18724
18725    member-declaration:
18726      __extension__ member-declaration
18727
18728    member-declarator:
18729      declarator attributes [opt] pure-specifier [opt]
18730      declarator attributes [opt] constant-initializer [opt]
18731      identifier [opt] attributes [opt] : constant-expression  
18732
18733    C++0x Extensions:
18734
18735    member-declaration:
18736      static_assert-declaration  */
18737
18738 static void
18739 cp_parser_member_declaration (cp_parser* parser)
18740 {
18741   cp_decl_specifier_seq decl_specifiers;
18742   tree prefix_attributes;
18743   tree decl;
18744   int declares_class_or_enum;
18745   bool friend_p;
18746   cp_token *token = NULL;
18747   cp_token *decl_spec_token_start = NULL;
18748   cp_token *initializer_token_start = NULL;
18749   int saved_pedantic;
18750   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18751
18752   /* Check for the `__extension__' keyword.  */
18753   if (cp_parser_extension_opt (parser, &saved_pedantic))
18754     {
18755       /* Recurse.  */
18756       cp_parser_member_declaration (parser);
18757       /* Restore the old value of the PEDANTIC flag.  */
18758       pedantic = saved_pedantic;
18759
18760       return;
18761     }
18762
18763   /* Check for a template-declaration.  */
18764   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18765     {
18766       /* An explicit specialization here is an error condition, and we
18767          expect the specialization handler to detect and report this.  */
18768       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18769           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18770         cp_parser_explicit_specialization (parser);
18771       else
18772         cp_parser_template_declaration (parser, /*member_p=*/true);
18773
18774       return;
18775     }
18776
18777   /* Check for a using-declaration.  */
18778   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18779     {
18780       if (cxx_dialect < cxx0x)
18781         {
18782           /* Parse the using-declaration.  */
18783           cp_parser_using_declaration (parser,
18784                                        /*access_declaration_p=*/false);
18785           return;
18786         }
18787       else
18788         {
18789           tree decl;
18790           cp_parser_parse_tentatively (parser);
18791           decl = cp_parser_alias_declaration (parser);
18792           if (cp_parser_parse_definitely (parser))
18793             finish_member_declaration (decl);
18794           else
18795             cp_parser_using_declaration (parser,
18796                                          /*access_declaration_p=*/false);
18797           return;
18798         }
18799     }
18800
18801   /* Check for @defs.  */
18802   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18803     {
18804       tree ivar, member;
18805       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18806       ivar = ivar_chains;
18807       while (ivar)
18808         {
18809           member = ivar;
18810           ivar = TREE_CHAIN (member);
18811           TREE_CHAIN (member) = NULL_TREE;
18812           finish_member_declaration (member);
18813         }
18814       return;
18815     }
18816
18817   /* If the next token is `static_assert' we have a static assertion.  */
18818   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18819     {
18820       cp_parser_static_assert (parser, /*member_p=*/true);
18821       return;
18822     }
18823
18824   parser->colon_corrects_to_scope_p = false;
18825
18826   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18827       goto out;
18828
18829   /* Parse the decl-specifier-seq.  */
18830   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18831   cp_parser_decl_specifier_seq (parser,
18832                                 CP_PARSER_FLAGS_OPTIONAL,
18833                                 &decl_specifiers,
18834                                 &declares_class_or_enum);
18835   prefix_attributes = decl_specifiers.attributes;
18836   decl_specifiers.attributes = NULL_TREE;
18837   /* Check for an invalid type-name.  */
18838   if (!decl_specifiers.any_type_specifiers_p
18839       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18840     goto out;
18841   /* If there is no declarator, then the decl-specifier-seq should
18842      specify a type.  */
18843   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18844     {
18845       /* If there was no decl-specifier-seq, and the next token is a
18846          `;', then we have something like:
18847
18848            struct S { ; };
18849
18850          [class.mem]
18851
18852          Each member-declaration shall declare at least one member
18853          name of the class.  */
18854       if (!decl_specifiers.any_specifiers_p)
18855         {
18856           cp_token *token = cp_lexer_peek_token (parser->lexer);
18857           if (!in_system_header_at (token->location))
18858             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18859         }
18860       else
18861         {
18862           tree type;
18863
18864           /* See if this declaration is a friend.  */
18865           friend_p = cp_parser_friend_p (&decl_specifiers);
18866           /* If there were decl-specifiers, check to see if there was
18867              a class-declaration.  */
18868           type = check_tag_decl (&decl_specifiers);
18869           /* Nested classes have already been added to the class, but
18870              a `friend' needs to be explicitly registered.  */
18871           if (friend_p)
18872             {
18873               /* If the `friend' keyword was present, the friend must
18874                  be introduced with a class-key.  */
18875                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18876                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18877                           "in C++03 a class-key must be used "
18878                           "when declaring a friend");
18879                /* In this case:
18880
18881                     template <typename T> struct A {
18882                       friend struct A<T>::B;
18883                     };
18884
18885                   A<T>::B will be represented by a TYPENAME_TYPE, and
18886                   therefore not recognized by check_tag_decl.  */
18887                if (!type)
18888                  {
18889                    type = decl_specifiers.type;
18890                    if (type && TREE_CODE (type) == TYPE_DECL)
18891                      type = TREE_TYPE (type);
18892                  }
18893                if (!type || !TYPE_P (type))
18894                  error_at (decl_spec_token_start->location,
18895                            "friend declaration does not name a class or "
18896                            "function");
18897                else
18898                  make_friend_class (current_class_type, type,
18899                                     /*complain=*/true);
18900             }
18901           /* If there is no TYPE, an error message will already have
18902              been issued.  */
18903           else if (!type || type == error_mark_node)
18904             ;
18905           /* An anonymous aggregate has to be handled specially; such
18906              a declaration really declares a data member (with a
18907              particular type), as opposed to a nested class.  */
18908           else if (ANON_AGGR_TYPE_P (type))
18909             {
18910               /* Remove constructors and such from TYPE, now that we
18911                  know it is an anonymous aggregate.  */
18912               fixup_anonymous_aggr (type);
18913               /* And make the corresponding data member.  */
18914               decl = build_decl (decl_spec_token_start->location,
18915                                  FIELD_DECL, NULL_TREE, type);
18916               /* Add it to the class.  */
18917               finish_member_declaration (decl);
18918             }
18919           else
18920             cp_parser_check_access_in_redeclaration
18921                                               (TYPE_NAME (type),
18922                                                decl_spec_token_start->location);
18923         }
18924     }
18925   else
18926     {
18927       bool assume_semicolon = false;
18928
18929       /* See if these declarations will be friends.  */
18930       friend_p = cp_parser_friend_p (&decl_specifiers);
18931
18932       /* Keep going until we hit the `;' at the end of the
18933          declaration.  */
18934       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18935         {
18936           tree attributes = NULL_TREE;
18937           tree first_attribute;
18938
18939           /* Peek at the next token.  */
18940           token = cp_lexer_peek_token (parser->lexer);
18941
18942           /* Check for a bitfield declaration.  */
18943           if (token->type == CPP_COLON
18944               || (token->type == CPP_NAME
18945                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18946                   == CPP_COLON))
18947             {
18948               tree identifier;
18949               tree width;
18950
18951               /* Get the name of the bitfield.  Note that we cannot just
18952                  check TOKEN here because it may have been invalidated by
18953                  the call to cp_lexer_peek_nth_token above.  */
18954               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18955                 identifier = cp_parser_identifier (parser);
18956               else
18957                 identifier = NULL_TREE;
18958
18959               /* Consume the `:' token.  */
18960               cp_lexer_consume_token (parser->lexer);
18961               /* Get the width of the bitfield.  */
18962               width
18963                 = cp_parser_constant_expression (parser,
18964                                                  /*allow_non_constant=*/false,
18965                                                  NULL);
18966
18967               /* Look for attributes that apply to the bitfield.  */
18968               attributes = cp_parser_attributes_opt (parser);
18969               /* Remember which attributes are prefix attributes and
18970                  which are not.  */
18971               first_attribute = attributes;
18972               /* Combine the attributes.  */
18973               attributes = chainon (prefix_attributes, attributes);
18974
18975               /* Create the bitfield declaration.  */
18976               decl = grokbitfield (identifier
18977                                    ? make_id_declarator (NULL_TREE,
18978                                                          identifier,
18979                                                          sfk_none)
18980                                    : NULL,
18981                                    &decl_specifiers,
18982                                    width,
18983                                    attributes);
18984             }
18985           else
18986             {
18987               cp_declarator *declarator;
18988               tree initializer;
18989               tree asm_specification;
18990               int ctor_dtor_or_conv_p;
18991
18992               /* Parse the declarator.  */
18993               declarator
18994                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18995                                         &ctor_dtor_or_conv_p,
18996                                         /*parenthesized_p=*/NULL,
18997                                         /*member_p=*/true);
18998
18999               /* If something went wrong parsing the declarator, make sure
19000                  that we at least consume some tokens.  */
19001               if (declarator == cp_error_declarator)
19002                 {
19003                   /* Skip to the end of the statement.  */
19004                   cp_parser_skip_to_end_of_statement (parser);
19005                   /* If the next token is not a semicolon, that is
19006                      probably because we just skipped over the body of
19007                      a function.  So, we consume a semicolon if
19008                      present, but do not issue an error message if it
19009                      is not present.  */
19010                   if (cp_lexer_next_token_is (parser->lexer,
19011                                               CPP_SEMICOLON))
19012                     cp_lexer_consume_token (parser->lexer);
19013                   goto out;
19014                 }
19015
19016               if (declares_class_or_enum & 2)
19017                 cp_parser_check_for_definition_in_return_type
19018                                             (declarator, decl_specifiers.type,
19019                                              decl_specifiers.type_location);
19020
19021               /* Look for an asm-specification.  */
19022               asm_specification = cp_parser_asm_specification_opt (parser);
19023               /* Look for attributes that apply to the declaration.  */
19024               attributes = cp_parser_attributes_opt (parser);
19025               /* Remember which attributes are prefix attributes and
19026                  which are not.  */
19027               first_attribute = attributes;
19028               /* Combine the attributes.  */
19029               attributes = chainon (prefix_attributes, attributes);
19030
19031               /* If it's an `=', then we have a constant-initializer or a
19032                  pure-specifier.  It is not correct to parse the
19033                  initializer before registering the member declaration
19034                  since the member declaration should be in scope while
19035                  its initializer is processed.  However, the rest of the
19036                  front end does not yet provide an interface that allows
19037                  us to handle this correctly.  */
19038               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19039                 {
19040                   /* In [class.mem]:
19041
19042                      A pure-specifier shall be used only in the declaration of
19043                      a virtual function.
19044
19045                      A member-declarator can contain a constant-initializer
19046                      only if it declares a static member of integral or
19047                      enumeration type.
19048
19049                      Therefore, if the DECLARATOR is for a function, we look
19050                      for a pure-specifier; otherwise, we look for a
19051                      constant-initializer.  When we call `grokfield', it will
19052                      perform more stringent semantics checks.  */
19053                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19054                   if (function_declarator_p (declarator)
19055                       || (decl_specifiers.type
19056                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19057                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19058                               == FUNCTION_TYPE)))
19059                     initializer = cp_parser_pure_specifier (parser);
19060                   else if (decl_specifiers.storage_class != sc_static)
19061                     initializer = cp_parser_save_nsdmi (parser);
19062                   else if (cxx_dialect >= cxx0x)
19063                     {
19064                       bool nonconst;
19065                       /* Don't require a constant rvalue in C++11, since we
19066                          might want a reference constant.  We'll enforce
19067                          constancy later.  */
19068                       cp_lexer_consume_token (parser->lexer);
19069                       /* Parse the initializer.  */
19070                       initializer = cp_parser_initializer_clause (parser,
19071                                                                   &nonconst);
19072                     }
19073                   else
19074                     /* Parse the initializer.  */
19075                     initializer = cp_parser_constant_initializer (parser);
19076                 }
19077               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19078                        && !function_declarator_p (declarator))
19079                 {
19080                   bool x;
19081                   if (decl_specifiers.storage_class != sc_static)
19082                     initializer = cp_parser_save_nsdmi (parser);
19083                   else
19084                     initializer = cp_parser_initializer (parser, &x, &x);
19085                 }
19086               /* Otherwise, there is no initializer.  */
19087               else
19088                 initializer = NULL_TREE;
19089
19090               /* See if we are probably looking at a function
19091                  definition.  We are certainly not looking at a
19092                  member-declarator.  Calling `grokfield' has
19093                  side-effects, so we must not do it unless we are sure
19094                  that we are looking at a member-declarator.  */
19095               if (cp_parser_token_starts_function_definition_p
19096                   (cp_lexer_peek_token (parser->lexer)))
19097                 {
19098                   /* The grammar does not allow a pure-specifier to be
19099                      used when a member function is defined.  (It is
19100                      possible that this fact is an oversight in the
19101                      standard, since a pure function may be defined
19102                      outside of the class-specifier.  */
19103                   if (initializer)
19104                     error_at (initializer_token_start->location,
19105                               "pure-specifier on function-definition");
19106                   decl = cp_parser_save_member_function_body (parser,
19107                                                               &decl_specifiers,
19108                                                               declarator,
19109                                                               attributes);
19110                   /* If the member was not a friend, declare it here.  */
19111                   if (!friend_p)
19112                     finish_member_declaration (decl);
19113                   /* Peek at the next token.  */
19114                   token = cp_lexer_peek_token (parser->lexer);
19115                   /* If the next token is a semicolon, consume it.  */
19116                   if (token->type == CPP_SEMICOLON)
19117                     cp_lexer_consume_token (parser->lexer);
19118                   goto out;
19119                 }
19120               else
19121                 if (declarator->kind == cdk_function)
19122                   declarator->id_loc = token->location;
19123                 /* Create the declaration.  */
19124                 decl = grokfield (declarator, &decl_specifiers,
19125                                   initializer, /*init_const_expr_p=*/true,
19126                                   asm_specification,
19127                                   attributes);
19128             }
19129
19130           /* Reset PREFIX_ATTRIBUTES.  */
19131           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19132             attributes = TREE_CHAIN (attributes);
19133           if (attributes)
19134             TREE_CHAIN (attributes) = NULL_TREE;
19135
19136           /* If there is any qualification still in effect, clear it
19137              now; we will be starting fresh with the next declarator.  */
19138           parser->scope = NULL_TREE;
19139           parser->qualifying_scope = NULL_TREE;
19140           parser->object_scope = NULL_TREE;
19141           /* If it's a `,', then there are more declarators.  */
19142           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19143             cp_lexer_consume_token (parser->lexer);
19144           /* If the next token isn't a `;', then we have a parse error.  */
19145           else if (cp_lexer_next_token_is_not (parser->lexer,
19146                                                CPP_SEMICOLON))
19147             {
19148               /* The next token might be a ways away from where the
19149                  actual semicolon is missing.  Find the previous token
19150                  and use that for our error position.  */
19151               cp_token *token = cp_lexer_previous_token (parser->lexer);
19152               error_at (token->location,
19153                         "expected %<;%> at end of member declaration");
19154
19155               /* Assume that the user meant to provide a semicolon.  If
19156                  we were to cp_parser_skip_to_end_of_statement, we might
19157                  skip to a semicolon inside a member function definition
19158                  and issue nonsensical error messages.  */
19159               assume_semicolon = true;
19160             }
19161
19162           if (decl)
19163             {
19164               /* Add DECL to the list of members.  */
19165               if (!friend_p)
19166                 finish_member_declaration (decl);
19167
19168               if (TREE_CODE (decl) == FUNCTION_DECL)
19169                 cp_parser_save_default_args (parser, decl);
19170               else if (TREE_CODE (decl) == FIELD_DECL
19171                        && !DECL_C_BIT_FIELD (decl)
19172                        && DECL_INITIAL (decl))
19173                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19174                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19175             }
19176
19177           if (assume_semicolon)
19178             goto out;
19179         }
19180     }
19181
19182   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19183  out:
19184   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19185 }
19186
19187 /* Parse a pure-specifier.
19188
19189    pure-specifier:
19190      = 0
19191
19192    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19193    Otherwise, ERROR_MARK_NODE is returned.  */
19194
19195 static tree
19196 cp_parser_pure_specifier (cp_parser* parser)
19197 {
19198   cp_token *token;
19199
19200   /* Look for the `=' token.  */
19201   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19202     return error_mark_node;
19203   /* Look for the `0' token.  */
19204   token = cp_lexer_peek_token (parser->lexer);
19205
19206   if (token->type == CPP_EOF
19207       || token->type == CPP_PRAGMA_EOL)
19208     return error_mark_node;
19209
19210   cp_lexer_consume_token (parser->lexer);
19211
19212   /* Accept = default or = delete in c++0x mode.  */
19213   if (token->keyword == RID_DEFAULT
19214       || token->keyword == RID_DELETE)
19215     {
19216       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19217       return token->u.value;
19218     }
19219
19220   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19221   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19222     {
19223       cp_parser_error (parser,
19224                        "invalid pure specifier (only %<= 0%> is allowed)");
19225       cp_parser_skip_to_end_of_statement (parser);
19226       return error_mark_node;
19227     }
19228   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19229     {
19230       error_at (token->location, "templates may not be %<virtual%>");
19231       return error_mark_node;
19232     }
19233
19234   return integer_zero_node;
19235 }
19236
19237 /* Parse a constant-initializer.
19238
19239    constant-initializer:
19240      = constant-expression
19241
19242    Returns a representation of the constant-expression.  */
19243
19244 static tree
19245 cp_parser_constant_initializer (cp_parser* parser)
19246 {
19247   /* Look for the `=' token.  */
19248   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19249     return error_mark_node;
19250
19251   /* It is invalid to write:
19252
19253        struct S { static const int i = { 7 }; };
19254
19255      */
19256   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19257     {
19258       cp_parser_error (parser,
19259                        "a brace-enclosed initializer is not allowed here");
19260       /* Consume the opening brace.  */
19261       cp_lexer_consume_token (parser->lexer);
19262       /* Skip the initializer.  */
19263       cp_parser_skip_to_closing_brace (parser);
19264       /* Look for the trailing `}'.  */
19265       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19266
19267       return error_mark_node;
19268     }
19269
19270   return cp_parser_constant_expression (parser,
19271                                         /*allow_non_constant=*/false,
19272                                         NULL);
19273 }
19274
19275 /* Derived classes [gram.class.derived] */
19276
19277 /* Parse a base-clause.
19278
19279    base-clause:
19280      : base-specifier-list
19281
19282    base-specifier-list:
19283      base-specifier ... [opt]
19284      base-specifier-list , base-specifier ... [opt]
19285
19286    Returns a TREE_LIST representing the base-classes, in the order in
19287    which they were declared.  The representation of each node is as
19288    described by cp_parser_base_specifier.
19289
19290    In the case that no bases are specified, this function will return
19291    NULL_TREE, not ERROR_MARK_NODE.  */
19292
19293 static tree
19294 cp_parser_base_clause (cp_parser* parser)
19295 {
19296   tree bases = NULL_TREE;
19297
19298   /* Look for the `:' that begins the list.  */
19299   cp_parser_require (parser, CPP_COLON, RT_COLON);
19300
19301   /* Scan the base-specifier-list.  */
19302   while (true)
19303     {
19304       cp_token *token;
19305       tree base;
19306       bool pack_expansion_p = false;
19307
19308       /* Look for the base-specifier.  */
19309       base = cp_parser_base_specifier (parser);
19310       /* Look for the (optional) ellipsis. */
19311       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19312         {
19313           /* Consume the `...'. */
19314           cp_lexer_consume_token (parser->lexer);
19315
19316           pack_expansion_p = true;
19317         }
19318
19319       /* Add BASE to the front of the list.  */
19320       if (base && base != error_mark_node)
19321         {
19322           if (pack_expansion_p)
19323             /* Make this a pack expansion type. */
19324             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19325
19326           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19327             {
19328               TREE_CHAIN (base) = bases;
19329               bases = base;
19330             }
19331         }
19332       /* Peek at the next token.  */
19333       token = cp_lexer_peek_token (parser->lexer);
19334       /* If it's not a comma, then the list is complete.  */
19335       if (token->type != CPP_COMMA)
19336         break;
19337       /* Consume the `,'.  */
19338       cp_lexer_consume_token (parser->lexer);
19339     }
19340
19341   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19342      base class had a qualified name.  However, the next name that
19343      appears is certainly not qualified.  */
19344   parser->scope = NULL_TREE;
19345   parser->qualifying_scope = NULL_TREE;
19346   parser->object_scope = NULL_TREE;
19347
19348   return nreverse (bases);
19349 }
19350
19351 /* Parse a base-specifier.
19352
19353    base-specifier:
19354      :: [opt] nested-name-specifier [opt] class-name
19355      virtual access-specifier [opt] :: [opt] nested-name-specifier
19356        [opt] class-name
19357      access-specifier virtual [opt] :: [opt] nested-name-specifier
19358        [opt] class-name
19359
19360    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19361    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19362    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19363    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19364
19365 static tree
19366 cp_parser_base_specifier (cp_parser* parser)
19367 {
19368   cp_token *token;
19369   bool done = false;
19370   bool virtual_p = false;
19371   bool duplicate_virtual_error_issued_p = false;
19372   bool duplicate_access_error_issued_p = false;
19373   bool class_scope_p, template_p;
19374   tree access = access_default_node;
19375   tree type;
19376
19377   /* Process the optional `virtual' and `access-specifier'.  */
19378   while (!done)
19379     {
19380       /* Peek at the next token.  */
19381       token = cp_lexer_peek_token (parser->lexer);
19382       /* Process `virtual'.  */
19383       switch (token->keyword)
19384         {
19385         case RID_VIRTUAL:
19386           /* If `virtual' appears more than once, issue an error.  */
19387           if (virtual_p && !duplicate_virtual_error_issued_p)
19388             {
19389               cp_parser_error (parser,
19390                                "%<virtual%> specified more than once in base-specified");
19391               duplicate_virtual_error_issued_p = true;
19392             }
19393
19394           virtual_p = true;
19395
19396           /* Consume the `virtual' token.  */
19397           cp_lexer_consume_token (parser->lexer);
19398
19399           break;
19400
19401         case RID_PUBLIC:
19402         case RID_PROTECTED:
19403         case RID_PRIVATE:
19404           /* If more than one access specifier appears, issue an
19405              error.  */
19406           if (access != access_default_node
19407               && !duplicate_access_error_issued_p)
19408             {
19409               cp_parser_error (parser,
19410                                "more than one access specifier in base-specified");
19411               duplicate_access_error_issued_p = true;
19412             }
19413
19414           access = ridpointers[(int) token->keyword];
19415
19416           /* Consume the access-specifier.  */
19417           cp_lexer_consume_token (parser->lexer);
19418
19419           break;
19420
19421         default:
19422           done = true;
19423           break;
19424         }
19425     }
19426   /* It is not uncommon to see programs mechanically, erroneously, use
19427      the 'typename' keyword to denote (dependent) qualified types
19428      as base classes.  */
19429   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19430     {
19431       token = cp_lexer_peek_token (parser->lexer);
19432       if (!processing_template_decl)
19433         error_at (token->location,
19434                   "keyword %<typename%> not allowed outside of templates");
19435       else
19436         error_at (token->location,
19437                   "keyword %<typename%> not allowed in this context "
19438                   "(the base class is implicitly a type)");
19439       cp_lexer_consume_token (parser->lexer);
19440     }
19441
19442   /* Look for the optional `::' operator.  */
19443   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19444   /* Look for the nested-name-specifier.  The simplest way to
19445      implement:
19446
19447        [temp.res]
19448
19449        The keyword `typename' is not permitted in a base-specifier or
19450        mem-initializer; in these contexts a qualified name that
19451        depends on a template-parameter is implicitly assumed to be a
19452        type name.
19453
19454      is to pretend that we have seen the `typename' keyword at this
19455      point.  */
19456   cp_parser_nested_name_specifier_opt (parser,
19457                                        /*typename_keyword_p=*/true,
19458                                        /*check_dependency_p=*/true,
19459                                        typename_type,
19460                                        /*is_declaration=*/true);
19461   /* If the base class is given by a qualified name, assume that names
19462      we see are type names or templates, as appropriate.  */
19463   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19464   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19465
19466   if (!parser->scope
19467       && cp_lexer_next_token_is_decltype (parser->lexer))
19468     /* DR 950 allows decltype as a base-specifier.  */
19469     type = cp_parser_decltype (parser);
19470   else
19471     {
19472       /* Otherwise, look for the class-name.  */
19473       type = cp_parser_class_name (parser,
19474                                    class_scope_p,
19475                                    template_p,
19476                                    typename_type,
19477                                    /*check_dependency_p=*/true,
19478                                    /*class_head_p=*/false,
19479                                    /*is_declaration=*/true);
19480       type = TREE_TYPE (type);
19481     }
19482
19483   if (type == error_mark_node)
19484     return error_mark_node;
19485
19486   return finish_base_specifier (type, access, virtual_p);
19487 }
19488
19489 /* Exception handling [gram.exception] */
19490
19491 /* Parse an (optional) noexcept-specification.
19492
19493    noexcept-specification:
19494      noexcept ( constant-expression ) [opt]
19495
19496    If no noexcept-specification is present, returns NULL_TREE.
19497    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19498    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19499    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19500    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19501    in which case a boolean condition is returned instead.  */
19502
19503 static tree
19504 cp_parser_noexcept_specification_opt (cp_parser* parser,
19505                                       bool require_constexpr,
19506                                       bool* consumed_expr,
19507                                       bool return_cond)
19508 {
19509   cp_token *token;
19510   const char *saved_message;
19511
19512   /* Peek at the next token.  */
19513   token = cp_lexer_peek_token (parser->lexer);
19514
19515   /* Is it a noexcept-specification?  */
19516   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19517     {
19518       tree expr;
19519       cp_lexer_consume_token (parser->lexer);
19520
19521       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19522         {
19523           cp_lexer_consume_token (parser->lexer);
19524
19525           if (require_constexpr)
19526             {
19527               /* Types may not be defined in an exception-specification.  */
19528               saved_message = parser->type_definition_forbidden_message;
19529               parser->type_definition_forbidden_message
19530               = G_("types may not be defined in an exception-specification");
19531
19532               expr = cp_parser_constant_expression (parser, false, NULL);
19533
19534               /* Restore the saved message.  */
19535               parser->type_definition_forbidden_message = saved_message;
19536             }
19537           else
19538             {
19539               expr = cp_parser_expression (parser, false, NULL);
19540               *consumed_expr = true;
19541             }
19542
19543           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19544         }
19545       else
19546         {
19547           expr = boolean_true_node;
19548           if (!require_constexpr)
19549             *consumed_expr = false;
19550         }
19551
19552       /* We cannot build a noexcept-spec right away because this will check
19553          that expr is a constexpr.  */
19554       if (!return_cond)
19555         return build_noexcept_spec (expr, tf_warning_or_error);
19556       else
19557         return expr;
19558     }
19559   else
19560     return NULL_TREE;
19561 }
19562
19563 /* Parse an (optional) exception-specification.
19564
19565    exception-specification:
19566      throw ( type-id-list [opt] )
19567
19568    Returns a TREE_LIST representing the exception-specification.  The
19569    TREE_VALUE of each node is a type.  */
19570
19571 static tree
19572 cp_parser_exception_specification_opt (cp_parser* parser)
19573 {
19574   cp_token *token;
19575   tree type_id_list;
19576   const char *saved_message;
19577
19578   /* Peek at the next token.  */
19579   token = cp_lexer_peek_token (parser->lexer);
19580
19581   /* Is it a noexcept-specification?  */
19582   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19583                                                       false);
19584   if (type_id_list != NULL_TREE)
19585     return type_id_list;
19586
19587   /* If it's not `throw', then there's no exception-specification.  */
19588   if (!cp_parser_is_keyword (token, RID_THROW))
19589     return NULL_TREE;
19590
19591 #if 0
19592   /* Enable this once a lot of code has transitioned to noexcept?  */
19593   if (cxx_dialect == cxx0x && !in_system_header)
19594     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19595              "deprecated in C++0x; use %<noexcept%> instead");
19596 #endif
19597
19598   /* Consume the `throw'.  */
19599   cp_lexer_consume_token (parser->lexer);
19600
19601   /* Look for the `('.  */
19602   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19603
19604   /* Peek at the next token.  */
19605   token = cp_lexer_peek_token (parser->lexer);
19606   /* If it's not a `)', then there is a type-id-list.  */
19607   if (token->type != CPP_CLOSE_PAREN)
19608     {
19609       /* Types may not be defined in an exception-specification.  */
19610       saved_message = parser->type_definition_forbidden_message;
19611       parser->type_definition_forbidden_message
19612         = G_("types may not be defined in an exception-specification");
19613       /* Parse the type-id-list.  */
19614       type_id_list = cp_parser_type_id_list (parser);
19615       /* Restore the saved message.  */
19616       parser->type_definition_forbidden_message = saved_message;
19617     }
19618   else
19619     type_id_list = empty_except_spec;
19620
19621   /* Look for the `)'.  */
19622   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19623
19624   return type_id_list;
19625 }
19626
19627 /* Parse an (optional) type-id-list.
19628
19629    type-id-list:
19630      type-id ... [opt]
19631      type-id-list , type-id ... [opt]
19632
19633    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19634    in the order that the types were presented.  */
19635
19636 static tree
19637 cp_parser_type_id_list (cp_parser* parser)
19638 {
19639   tree types = NULL_TREE;
19640
19641   while (true)
19642     {
19643       cp_token *token;
19644       tree type;
19645
19646       /* Get the next type-id.  */
19647       type = cp_parser_type_id (parser);
19648       /* Parse the optional ellipsis. */
19649       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19650         {
19651           /* Consume the `...'. */
19652           cp_lexer_consume_token (parser->lexer);
19653
19654           /* Turn the type into a pack expansion expression. */
19655           type = make_pack_expansion (type);
19656         }
19657       /* Add it to the list.  */
19658       types = add_exception_specifier (types, type, /*complain=*/1);
19659       /* Peek at the next token.  */
19660       token = cp_lexer_peek_token (parser->lexer);
19661       /* If it is not a `,', we are done.  */
19662       if (token->type != CPP_COMMA)
19663         break;
19664       /* Consume the `,'.  */
19665       cp_lexer_consume_token (parser->lexer);
19666     }
19667
19668   return nreverse (types);
19669 }
19670
19671 /* Parse a try-block.
19672
19673    try-block:
19674      try compound-statement handler-seq  */
19675
19676 static tree
19677 cp_parser_try_block (cp_parser* parser)
19678 {
19679   tree try_block;
19680
19681   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19682   try_block = begin_try_block ();
19683   cp_parser_compound_statement (parser, NULL, true, false);
19684   finish_try_block (try_block);
19685   cp_parser_handler_seq (parser);
19686   finish_handler_sequence (try_block);
19687
19688   return try_block;
19689 }
19690
19691 /* Parse a function-try-block.
19692
19693    function-try-block:
19694      try ctor-initializer [opt] function-body handler-seq  */
19695
19696 static bool
19697 cp_parser_function_try_block (cp_parser* parser)
19698 {
19699   tree compound_stmt;
19700   tree try_block;
19701   bool ctor_initializer_p;
19702
19703   /* Look for the `try' keyword.  */
19704   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19705     return false;
19706   /* Let the rest of the front end know where we are.  */
19707   try_block = begin_function_try_block (&compound_stmt);
19708   /* Parse the function-body.  */
19709   ctor_initializer_p
19710     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19711   /* We're done with the `try' part.  */
19712   finish_function_try_block (try_block);
19713   /* Parse the handlers.  */
19714   cp_parser_handler_seq (parser);
19715   /* We're done with the handlers.  */
19716   finish_function_handler_sequence (try_block, compound_stmt);
19717
19718   return ctor_initializer_p;
19719 }
19720
19721 /* Parse a handler-seq.
19722
19723    handler-seq:
19724      handler handler-seq [opt]  */
19725
19726 static void
19727 cp_parser_handler_seq (cp_parser* parser)
19728 {
19729   while (true)
19730     {
19731       cp_token *token;
19732
19733       /* Parse the handler.  */
19734       cp_parser_handler (parser);
19735       /* Peek at the next token.  */
19736       token = cp_lexer_peek_token (parser->lexer);
19737       /* If it's not `catch' then there are no more handlers.  */
19738       if (!cp_parser_is_keyword (token, RID_CATCH))
19739         break;
19740     }
19741 }
19742
19743 /* Parse a handler.
19744
19745    handler:
19746      catch ( exception-declaration ) compound-statement  */
19747
19748 static void
19749 cp_parser_handler (cp_parser* parser)
19750 {
19751   tree handler;
19752   tree declaration;
19753
19754   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19755   handler = begin_handler ();
19756   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19757   declaration = cp_parser_exception_declaration (parser);
19758   finish_handler_parms (declaration, handler);
19759   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19760   cp_parser_compound_statement (parser, NULL, false, false);
19761   finish_handler (handler);
19762 }
19763
19764 /* Parse an exception-declaration.
19765
19766    exception-declaration:
19767      type-specifier-seq declarator
19768      type-specifier-seq abstract-declarator
19769      type-specifier-seq
19770      ...
19771
19772    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19773    ellipsis variant is used.  */
19774
19775 static tree
19776 cp_parser_exception_declaration (cp_parser* parser)
19777 {
19778   cp_decl_specifier_seq type_specifiers;
19779   cp_declarator *declarator;
19780   const char *saved_message;
19781
19782   /* If it's an ellipsis, it's easy to handle.  */
19783   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19784     {
19785       /* Consume the `...' token.  */
19786       cp_lexer_consume_token (parser->lexer);
19787       return NULL_TREE;
19788     }
19789
19790   /* Types may not be defined in exception-declarations.  */
19791   saved_message = parser->type_definition_forbidden_message;
19792   parser->type_definition_forbidden_message
19793     = G_("types may not be defined in exception-declarations");
19794
19795   /* Parse the type-specifier-seq.  */
19796   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19797                                 /*is_trailing_return=*/false,
19798                                 &type_specifiers);
19799   /* If it's a `)', then there is no declarator.  */
19800   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19801     declarator = NULL;
19802   else
19803     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19804                                        /*ctor_dtor_or_conv_p=*/NULL,
19805                                        /*parenthesized_p=*/NULL,
19806                                        /*member_p=*/false);
19807
19808   /* Restore the saved message.  */
19809   parser->type_definition_forbidden_message = saved_message;
19810
19811   if (!type_specifiers.any_specifiers_p)
19812     return error_mark_node;
19813
19814   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19815 }
19816
19817 /* Parse a throw-expression.
19818
19819    throw-expression:
19820      throw assignment-expression [opt]
19821
19822    Returns a THROW_EXPR representing the throw-expression.  */
19823
19824 static tree
19825 cp_parser_throw_expression (cp_parser* parser)
19826 {
19827   tree expression;
19828   cp_token* token;
19829
19830   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19831   token = cp_lexer_peek_token (parser->lexer);
19832   /* Figure out whether or not there is an assignment-expression
19833      following the "throw" keyword.  */
19834   if (token->type == CPP_COMMA
19835       || token->type == CPP_SEMICOLON
19836       || token->type == CPP_CLOSE_PAREN
19837       || token->type == CPP_CLOSE_SQUARE
19838       || token->type == CPP_CLOSE_BRACE
19839       || token->type == CPP_COLON)
19840     expression = NULL_TREE;
19841   else
19842     expression = cp_parser_assignment_expression (parser,
19843                                                   /*cast_p=*/false, NULL);
19844
19845   return build_throw (expression);
19846 }
19847
19848 /* GNU Extensions */
19849
19850 /* Parse an (optional) asm-specification.
19851
19852    asm-specification:
19853      asm ( string-literal )
19854
19855    If the asm-specification is present, returns a STRING_CST
19856    corresponding to the string-literal.  Otherwise, returns
19857    NULL_TREE.  */
19858
19859 static tree
19860 cp_parser_asm_specification_opt (cp_parser* parser)
19861 {
19862   cp_token *token;
19863   tree asm_specification;
19864
19865   /* Peek at the next token.  */
19866   token = cp_lexer_peek_token (parser->lexer);
19867   /* If the next token isn't the `asm' keyword, then there's no
19868      asm-specification.  */
19869   if (!cp_parser_is_keyword (token, RID_ASM))
19870     return NULL_TREE;
19871
19872   /* Consume the `asm' token.  */
19873   cp_lexer_consume_token (parser->lexer);
19874   /* Look for the `('.  */
19875   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19876
19877   /* Look for the string-literal.  */
19878   asm_specification = cp_parser_string_literal (parser, false, false);
19879
19880   /* Look for the `)'.  */
19881   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19882
19883   return asm_specification;
19884 }
19885
19886 /* Parse an asm-operand-list.
19887
19888    asm-operand-list:
19889      asm-operand
19890      asm-operand-list , asm-operand
19891
19892    asm-operand:
19893      string-literal ( expression )
19894      [ string-literal ] string-literal ( expression )
19895
19896    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19897    each node is the expression.  The TREE_PURPOSE is itself a
19898    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19899    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19900    is a STRING_CST for the string literal before the parenthesis. Returns
19901    ERROR_MARK_NODE if any of the operands are invalid.  */
19902
19903 static tree
19904 cp_parser_asm_operand_list (cp_parser* parser)
19905 {
19906   tree asm_operands = NULL_TREE;
19907   bool invalid_operands = false;
19908
19909   while (true)
19910     {
19911       tree string_literal;
19912       tree expression;
19913       tree name;
19914
19915       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19916         {
19917           /* Consume the `[' token.  */
19918           cp_lexer_consume_token (parser->lexer);
19919           /* Read the operand name.  */
19920           name = cp_parser_identifier (parser);
19921           if (name != error_mark_node)
19922             name = build_string (IDENTIFIER_LENGTH (name),
19923                                  IDENTIFIER_POINTER (name));
19924           /* Look for the closing `]'.  */
19925           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19926         }
19927       else
19928         name = NULL_TREE;
19929       /* Look for the string-literal.  */
19930       string_literal = cp_parser_string_literal (parser, false, false);
19931
19932       /* Look for the `('.  */
19933       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19934       /* Parse the expression.  */
19935       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19936       /* Look for the `)'.  */
19937       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19938
19939       if (name == error_mark_node 
19940           || string_literal == error_mark_node 
19941           || expression == error_mark_node)
19942         invalid_operands = true;
19943
19944       /* Add this operand to the list.  */
19945       asm_operands = tree_cons (build_tree_list (name, string_literal),
19946                                 expression,
19947                                 asm_operands);
19948       /* If the next token is not a `,', there are no more
19949          operands.  */
19950       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19951         break;
19952       /* Consume the `,'.  */
19953       cp_lexer_consume_token (parser->lexer);
19954     }
19955
19956   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19957 }
19958
19959 /* Parse an asm-clobber-list.
19960
19961    asm-clobber-list:
19962      string-literal
19963      asm-clobber-list , string-literal
19964
19965    Returns a TREE_LIST, indicating the clobbers in the order that they
19966    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19967
19968 static tree
19969 cp_parser_asm_clobber_list (cp_parser* parser)
19970 {
19971   tree clobbers = NULL_TREE;
19972
19973   while (true)
19974     {
19975       tree string_literal;
19976
19977       /* Look for the string literal.  */
19978       string_literal = cp_parser_string_literal (parser, false, false);
19979       /* Add it to the list.  */
19980       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19981       /* If the next token is not a `,', then the list is
19982          complete.  */
19983       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19984         break;
19985       /* Consume the `,' token.  */
19986       cp_lexer_consume_token (parser->lexer);
19987     }
19988
19989   return clobbers;
19990 }
19991
19992 /* Parse an asm-label-list.
19993
19994    asm-label-list:
19995      identifier
19996      asm-label-list , identifier
19997
19998    Returns a TREE_LIST, indicating the labels in the order that they
19999    appeared.  The TREE_VALUE of each node is a label.  */
20000
20001 static tree
20002 cp_parser_asm_label_list (cp_parser* parser)
20003 {
20004   tree labels = NULL_TREE;
20005
20006   while (true)
20007     {
20008       tree identifier, label, name;
20009
20010       /* Look for the identifier.  */
20011       identifier = cp_parser_identifier (parser);
20012       if (!error_operand_p (identifier))
20013         {
20014           label = lookup_label (identifier);
20015           if (TREE_CODE (label) == LABEL_DECL)
20016             {
20017               TREE_USED (label) = 1;
20018               check_goto (label);
20019               name = build_string (IDENTIFIER_LENGTH (identifier),
20020                                    IDENTIFIER_POINTER (identifier));
20021               labels = tree_cons (name, label, labels);
20022             }
20023         }
20024       /* If the next token is not a `,', then the list is
20025          complete.  */
20026       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20027         break;
20028       /* Consume the `,' token.  */
20029       cp_lexer_consume_token (parser->lexer);
20030     }
20031
20032   return nreverse (labels);
20033 }
20034
20035 /* Parse an (optional) series of attributes.
20036
20037    attributes:
20038      attributes attribute
20039
20040    attribute:
20041      __attribute__ (( attribute-list [opt] ))
20042
20043    The return value is as for cp_parser_attribute_list.  */
20044
20045 static tree
20046 cp_parser_attributes_opt (cp_parser* parser)
20047 {
20048   tree attributes = NULL_TREE;
20049
20050   while (true)
20051     {
20052       cp_token *token;
20053       tree attribute_list;
20054
20055       /* Peek at the next token.  */
20056       token = cp_lexer_peek_token (parser->lexer);
20057       /* If it's not `__attribute__', then we're done.  */
20058       if (token->keyword != RID_ATTRIBUTE)
20059         break;
20060
20061       /* Consume the `__attribute__' keyword.  */
20062       cp_lexer_consume_token (parser->lexer);
20063       /* Look for the two `(' tokens.  */
20064       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20065       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20066
20067       /* Peek at the next token.  */
20068       token = cp_lexer_peek_token (parser->lexer);
20069       if (token->type != CPP_CLOSE_PAREN)
20070         /* Parse the attribute-list.  */
20071         attribute_list = cp_parser_attribute_list (parser);
20072       else
20073         /* If the next token is a `)', then there is no attribute
20074            list.  */
20075         attribute_list = NULL;
20076
20077       /* Look for the two `)' tokens.  */
20078       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20079       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20080
20081       /* Add these new attributes to the list.  */
20082       attributes = chainon (attributes, attribute_list);
20083     }
20084
20085   return attributes;
20086 }
20087
20088 /* Parse an attribute-list.
20089
20090    attribute-list:
20091      attribute
20092      attribute-list , attribute
20093
20094    attribute:
20095      identifier
20096      identifier ( identifier )
20097      identifier ( identifier , expression-list )
20098      identifier ( expression-list )
20099
20100    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20101    to an attribute.  The TREE_PURPOSE of each node is the identifier
20102    indicating which attribute is in use.  The TREE_VALUE represents
20103    the arguments, if any.  */
20104
20105 static tree
20106 cp_parser_attribute_list (cp_parser* parser)
20107 {
20108   tree attribute_list = NULL_TREE;
20109   bool save_translate_strings_p = parser->translate_strings_p;
20110
20111   parser->translate_strings_p = false;
20112   while (true)
20113     {
20114       cp_token *token;
20115       tree identifier;
20116       tree attribute;
20117
20118       /* Look for the identifier.  We also allow keywords here; for
20119          example `__attribute__ ((const))' is legal.  */
20120       token = cp_lexer_peek_token (parser->lexer);
20121       if (token->type == CPP_NAME
20122           || token->type == CPP_KEYWORD)
20123         {
20124           tree arguments = NULL_TREE;
20125
20126           /* Consume the token.  */
20127           token = cp_lexer_consume_token (parser->lexer);
20128
20129           /* Save away the identifier that indicates which attribute
20130              this is.  */
20131           identifier = (token->type == CPP_KEYWORD) 
20132             /* For keywords, use the canonical spelling, not the
20133                parsed identifier.  */
20134             ? ridpointers[(int) token->keyword]
20135             : token->u.value;
20136           
20137           attribute = build_tree_list (identifier, NULL_TREE);
20138
20139           /* Peek at the next token.  */
20140           token = cp_lexer_peek_token (parser->lexer);
20141           /* If it's an `(', then parse the attribute arguments.  */
20142           if (token->type == CPP_OPEN_PAREN)
20143             {
20144               VEC(tree,gc) *vec;
20145               int attr_flag = (attribute_takes_identifier_p (identifier)
20146                                ? id_attr : normal_attr);
20147               vec = cp_parser_parenthesized_expression_list
20148                     (parser, attr_flag, /*cast_p=*/false,
20149                      /*allow_expansion_p=*/false,
20150                      /*non_constant_p=*/NULL);
20151               if (vec == NULL)
20152                 arguments = error_mark_node;
20153               else
20154                 {
20155                   arguments = build_tree_list_vec (vec);
20156                   release_tree_vector (vec);
20157                 }
20158               /* Save the arguments away.  */
20159               TREE_VALUE (attribute) = arguments;
20160             }
20161
20162           if (arguments != error_mark_node)
20163             {
20164               /* Add this attribute to the list.  */
20165               TREE_CHAIN (attribute) = attribute_list;
20166               attribute_list = attribute;
20167             }
20168
20169           token = cp_lexer_peek_token (parser->lexer);
20170         }
20171       /* Now, look for more attributes.  If the next token isn't a
20172          `,', we're done.  */
20173       if (token->type != CPP_COMMA)
20174         break;
20175
20176       /* Consume the comma and keep going.  */
20177       cp_lexer_consume_token (parser->lexer);
20178     }
20179   parser->translate_strings_p = save_translate_strings_p;
20180
20181   /* We built up the list in reverse order.  */
20182   return nreverse (attribute_list);
20183 }
20184
20185 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20186    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20187    current value of the PEDANTIC flag, regardless of whether or not
20188    the `__extension__' keyword is present.  The caller is responsible
20189    for restoring the value of the PEDANTIC flag.  */
20190
20191 static bool
20192 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20193 {
20194   /* Save the old value of the PEDANTIC flag.  */
20195   *saved_pedantic = pedantic;
20196
20197   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20198     {
20199       /* Consume the `__extension__' token.  */
20200       cp_lexer_consume_token (parser->lexer);
20201       /* We're not being pedantic while the `__extension__' keyword is
20202          in effect.  */
20203       pedantic = 0;
20204
20205       return true;
20206     }
20207
20208   return false;
20209 }
20210
20211 /* Parse a label declaration.
20212
20213    label-declaration:
20214      __label__ label-declarator-seq ;
20215
20216    label-declarator-seq:
20217      identifier , label-declarator-seq
20218      identifier  */
20219
20220 static void
20221 cp_parser_label_declaration (cp_parser* parser)
20222 {
20223   /* Look for the `__label__' keyword.  */
20224   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20225
20226   while (true)
20227     {
20228       tree identifier;
20229
20230       /* Look for an identifier.  */
20231       identifier = cp_parser_identifier (parser);
20232       /* If we failed, stop.  */
20233       if (identifier == error_mark_node)
20234         break;
20235       /* Declare it as a label.  */
20236       finish_label_decl (identifier);
20237       /* If the next token is a `;', stop.  */
20238       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20239         break;
20240       /* Look for the `,' separating the label declarations.  */
20241       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20242     }
20243
20244   /* Look for the final `;'.  */
20245   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20246 }
20247
20248 /* Support Functions */
20249
20250 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20251    NAME should have one of the representations used for an
20252    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20253    is returned.  If PARSER->SCOPE is a dependent type, then a
20254    SCOPE_REF is returned.
20255
20256    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20257    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20258    was formed.  Abstractly, such entities should not be passed to this
20259    function, because they do not need to be looked up, but it is
20260    simpler to check for this special case here, rather than at the
20261    call-sites.
20262
20263    In cases not explicitly covered above, this function returns a
20264    DECL, OVERLOAD, or baselink representing the result of the lookup.
20265    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20266    is returned.
20267
20268    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20269    (e.g., "struct") that was used.  In that case bindings that do not
20270    refer to types are ignored.
20271
20272    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20273    ignored.
20274
20275    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20276    are ignored.
20277
20278    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20279    types.
20280
20281    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20282    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20283    NULL_TREE otherwise.  */
20284
20285 static tree
20286 cp_parser_lookup_name (cp_parser *parser, tree name,
20287                        enum tag_types tag_type,
20288                        bool is_template,
20289                        bool is_namespace,
20290                        bool check_dependency,
20291                        tree *ambiguous_decls,
20292                        location_t name_location)
20293 {
20294   int flags = 0;
20295   tree decl;
20296   tree object_type = parser->context->object_type;
20297
20298   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20299     flags |= LOOKUP_COMPLAIN;
20300
20301   /* Assume that the lookup will be unambiguous.  */
20302   if (ambiguous_decls)
20303     *ambiguous_decls = NULL_TREE;
20304
20305   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20306      no longer valid.  Note that if we are parsing tentatively, and
20307      the parse fails, OBJECT_TYPE will be automatically restored.  */
20308   parser->context->object_type = NULL_TREE;
20309
20310   if (name == error_mark_node)
20311     return error_mark_node;
20312
20313   /* A template-id has already been resolved; there is no lookup to
20314      do.  */
20315   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20316     return name;
20317   if (BASELINK_P (name))
20318     {
20319       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20320                   == TEMPLATE_ID_EXPR);
20321       return name;
20322     }
20323
20324   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20325      it should already have been checked to make sure that the name
20326      used matches the type being destroyed.  */
20327   if (TREE_CODE (name) == BIT_NOT_EXPR)
20328     {
20329       tree type;
20330
20331       /* Figure out to which type this destructor applies.  */
20332       if (parser->scope)
20333         type = parser->scope;
20334       else if (object_type)
20335         type = object_type;
20336       else
20337         type = current_class_type;
20338       /* If that's not a class type, there is no destructor.  */
20339       if (!type || !CLASS_TYPE_P (type))
20340         return error_mark_node;
20341       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20342         lazily_declare_fn (sfk_destructor, type);
20343       if (!CLASSTYPE_DESTRUCTORS (type))
20344           return error_mark_node;
20345       /* If it was a class type, return the destructor.  */
20346       return CLASSTYPE_DESTRUCTORS (type);
20347     }
20348
20349   /* By this point, the NAME should be an ordinary identifier.  If
20350      the id-expression was a qualified name, the qualifying scope is
20351      stored in PARSER->SCOPE at this point.  */
20352   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20353
20354   /* Perform the lookup.  */
20355   if (parser->scope)
20356     {
20357       bool dependent_p;
20358
20359       if (parser->scope == error_mark_node)
20360         return error_mark_node;
20361
20362       /* If the SCOPE is dependent, the lookup must be deferred until
20363          the template is instantiated -- unless we are explicitly
20364          looking up names in uninstantiated templates.  Even then, we
20365          cannot look up the name if the scope is not a class type; it
20366          might, for example, be a template type parameter.  */
20367       dependent_p = (TYPE_P (parser->scope)
20368                      && dependent_scope_p (parser->scope));
20369       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20370           && dependent_p)
20371         /* Defer lookup.  */
20372         decl = error_mark_node;
20373       else
20374         {
20375           tree pushed_scope = NULL_TREE;
20376
20377           /* If PARSER->SCOPE is a dependent type, then it must be a
20378              class type, and we must not be checking dependencies;
20379              otherwise, we would have processed this lookup above.  So
20380              that PARSER->SCOPE is not considered a dependent base by
20381              lookup_member, we must enter the scope here.  */
20382           if (dependent_p)
20383             pushed_scope = push_scope (parser->scope);
20384
20385           /* If the PARSER->SCOPE is a template specialization, it
20386              may be instantiated during name lookup.  In that case,
20387              errors may be issued.  Even if we rollback the current
20388              tentative parse, those errors are valid.  */
20389           decl = lookup_qualified_name (parser->scope, name,
20390                                         tag_type != none_type,
20391                                         /*complain=*/true);
20392
20393           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20394              lookup result and the nested-name-specifier nominates a class C:
20395                * if the name specified after the nested-name-specifier, when
20396                looked up in C, is the injected-class-name of C (Clause 9), or
20397                * if the name specified after the nested-name-specifier is the
20398                same as the identifier or the simple-template-id's template-
20399                name in the last component of the nested-name-specifier,
20400              the name is instead considered to name the constructor of
20401              class C. [ Note: for example, the constructor is not an
20402              acceptable lookup result in an elaborated-type-specifier so
20403              the constructor would not be used in place of the
20404              injected-class-name. --end note ] Such a constructor name
20405              shall be used only in the declarator-id of a declaration that
20406              names a constructor or in a using-declaration.  */
20407           if (tag_type == none_type
20408               && DECL_SELF_REFERENCE_P (decl)
20409               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20410             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20411                                           tag_type != none_type,
20412                                           /*complain=*/true);
20413
20414           /* If we have a single function from a using decl, pull it out.  */
20415           if (TREE_CODE (decl) == OVERLOAD
20416               && !really_overloaded_fn (decl))
20417             decl = OVL_FUNCTION (decl);
20418
20419           if (pushed_scope)
20420             pop_scope (pushed_scope);
20421         }
20422
20423       /* If the scope is a dependent type and either we deferred lookup or
20424          we did lookup but didn't find the name, rememeber the name.  */
20425       if (decl == error_mark_node && TYPE_P (parser->scope)
20426           && dependent_type_p (parser->scope))
20427         {
20428           if (tag_type)
20429             {
20430               tree type;
20431
20432               /* The resolution to Core Issue 180 says that `struct
20433                  A::B' should be considered a type-name, even if `A'
20434                  is dependent.  */
20435               type = make_typename_type (parser->scope, name, tag_type,
20436                                          /*complain=*/tf_error);
20437               decl = TYPE_NAME (type);
20438             }
20439           else if (is_template
20440                    && (cp_parser_next_token_ends_template_argument_p (parser)
20441                        || cp_lexer_next_token_is (parser->lexer,
20442                                                   CPP_CLOSE_PAREN)))
20443             decl = make_unbound_class_template (parser->scope,
20444                                                 name, NULL_TREE,
20445                                                 /*complain=*/tf_error);
20446           else
20447             decl = build_qualified_name (/*type=*/NULL_TREE,
20448                                          parser->scope, name,
20449                                          is_template);
20450         }
20451       parser->qualifying_scope = parser->scope;
20452       parser->object_scope = NULL_TREE;
20453     }
20454   else if (object_type)
20455     {
20456       tree object_decl = NULL_TREE;
20457       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20458          OBJECT_TYPE is not a class.  */
20459       if (CLASS_TYPE_P (object_type))
20460         /* If the OBJECT_TYPE is a template specialization, it may
20461            be instantiated during name lookup.  In that case, errors
20462            may be issued.  Even if we rollback the current tentative
20463            parse, those errors are valid.  */
20464         object_decl = lookup_member (object_type,
20465                                      name,
20466                                      /*protect=*/0,
20467                                      tag_type != none_type,
20468                                      tf_warning_or_error);
20469       /* Look it up in the enclosing context, too.  */
20470       decl = lookup_name_real (name, tag_type != none_type,
20471                                /*nonclass=*/0,
20472                                /*block_p=*/true, is_namespace, flags);
20473       parser->object_scope = object_type;
20474       parser->qualifying_scope = NULL_TREE;
20475       if (object_decl)
20476         decl = object_decl;
20477     }
20478   else
20479     {
20480       decl = lookup_name_real (name, tag_type != none_type,
20481                                /*nonclass=*/0,
20482                                /*block_p=*/true, is_namespace, flags);
20483       parser->qualifying_scope = NULL_TREE;
20484       parser->object_scope = NULL_TREE;
20485     }
20486
20487   /* If the lookup failed, let our caller know.  */
20488   if (!decl || decl == error_mark_node)
20489     return error_mark_node;
20490
20491   /* Pull out the template from an injected-class-name (or multiple).  */
20492   if (is_template)
20493     decl = maybe_get_template_decl_from_type_decl (decl);
20494
20495   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20496   if (TREE_CODE (decl) == TREE_LIST)
20497     {
20498       if (ambiguous_decls)
20499         *ambiguous_decls = decl;
20500       /* The error message we have to print is too complicated for
20501          cp_parser_error, so we incorporate its actions directly.  */
20502       if (!cp_parser_simulate_error (parser))
20503         {
20504           error_at (name_location, "reference to %qD is ambiguous",
20505                     name);
20506           print_candidates (decl);
20507         }
20508       return error_mark_node;
20509     }
20510
20511   gcc_assert (DECL_P (decl)
20512               || TREE_CODE (decl) == OVERLOAD
20513               || TREE_CODE (decl) == SCOPE_REF
20514               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20515               || BASELINK_P (decl));
20516
20517   /* If we have resolved the name of a member declaration, check to
20518      see if the declaration is accessible.  When the name resolves to
20519      set of overloaded functions, accessibility is checked when
20520      overload resolution is done.
20521
20522      During an explicit instantiation, access is not checked at all,
20523      as per [temp.explicit].  */
20524   if (DECL_P (decl))
20525     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20526
20527   maybe_record_typedef_use (decl);
20528
20529   return decl;
20530 }
20531
20532 /* Like cp_parser_lookup_name, but for use in the typical case where
20533    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20534    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20535
20536 static tree
20537 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20538 {
20539   return cp_parser_lookup_name (parser, name,
20540                                 none_type,
20541                                 /*is_template=*/false,
20542                                 /*is_namespace=*/false,
20543                                 /*check_dependency=*/true,
20544                                 /*ambiguous_decls=*/NULL,
20545                                 location);
20546 }
20547
20548 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20549    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20550    true, the DECL indicates the class being defined in a class-head,
20551    or declared in an elaborated-type-specifier.
20552
20553    Otherwise, return DECL.  */
20554
20555 static tree
20556 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20557 {
20558   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20559      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20560
20561        struct A {
20562          template <typename T> struct B;
20563        };
20564
20565        template <typename T> struct A::B {};
20566
20567      Similarly, in an elaborated-type-specifier:
20568
20569        namespace N { struct X{}; }
20570
20571        struct A {
20572          template <typename T> friend struct N::X;
20573        };
20574
20575      However, if the DECL refers to a class type, and we are in
20576      the scope of the class, then the name lookup automatically
20577      finds the TYPE_DECL created by build_self_reference rather
20578      than a TEMPLATE_DECL.  For example, in:
20579
20580        template <class T> struct S {
20581          S s;
20582        };
20583
20584      there is no need to handle such case.  */
20585
20586   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20587     return DECL_TEMPLATE_RESULT (decl);
20588
20589   return decl;
20590 }
20591
20592 /* If too many, or too few, template-parameter lists apply to the
20593    declarator, issue an error message.  Returns TRUE if all went well,
20594    and FALSE otherwise.  */
20595
20596 static bool
20597 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20598                                                 cp_declarator *declarator,
20599                                                 location_t declarator_location)
20600 {
20601   unsigned num_templates;
20602
20603   /* We haven't seen any classes that involve template parameters yet.  */
20604   num_templates = 0;
20605
20606   switch (declarator->kind)
20607     {
20608     case cdk_id:
20609       if (declarator->u.id.qualifying_scope)
20610         {
20611           tree scope;
20612
20613           scope = declarator->u.id.qualifying_scope;
20614
20615           while (scope && CLASS_TYPE_P (scope))
20616             {
20617               /* You're supposed to have one `template <...>'
20618                  for every template class, but you don't need one
20619                  for a full specialization.  For example:
20620
20621                  template <class T> struct S{};
20622                  template <> struct S<int> { void f(); };
20623                  void S<int>::f () {}
20624
20625                  is correct; there shouldn't be a `template <>' for
20626                  the definition of `S<int>::f'.  */
20627               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20628                 /* If SCOPE does not have template information of any
20629                    kind, then it is not a template, nor is it nested
20630                    within a template.  */
20631                 break;
20632               if (explicit_class_specialization_p (scope))
20633                 break;
20634               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20635                 ++num_templates;
20636
20637               scope = TYPE_CONTEXT (scope);
20638             }
20639         }
20640       else if (TREE_CODE (declarator->u.id.unqualified_name)
20641                == TEMPLATE_ID_EXPR)
20642         /* If the DECLARATOR has the form `X<y>' then it uses one
20643            additional level of template parameters.  */
20644         ++num_templates;
20645
20646       return cp_parser_check_template_parameters 
20647         (parser, num_templates, declarator_location, declarator);
20648
20649
20650     case cdk_function:
20651     case cdk_array:
20652     case cdk_pointer:
20653     case cdk_reference:
20654     case cdk_ptrmem:
20655       return (cp_parser_check_declarator_template_parameters
20656               (parser, declarator->declarator, declarator_location));
20657
20658     case cdk_error:
20659       return true;
20660
20661     default:
20662       gcc_unreachable ();
20663     }
20664   return false;
20665 }
20666
20667 /* NUM_TEMPLATES were used in the current declaration.  If that is
20668    invalid, return FALSE and issue an error messages.  Otherwise,
20669    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20670    declarator and we can print more accurate diagnostics.  */
20671
20672 static bool
20673 cp_parser_check_template_parameters (cp_parser* parser,
20674                                      unsigned num_templates,
20675                                      location_t location,
20676                                      cp_declarator *declarator)
20677 {
20678   /* If there are the same number of template classes and parameter
20679      lists, that's OK.  */
20680   if (parser->num_template_parameter_lists == num_templates)
20681     return true;
20682   /* If there are more, but only one more, then we are referring to a
20683      member template.  That's OK too.  */
20684   if (parser->num_template_parameter_lists == num_templates + 1)
20685     return true;
20686   /* If there are more template classes than parameter lists, we have
20687      something like:
20688
20689        template <class T> void S<T>::R<T>::f ();  */
20690   if (parser->num_template_parameter_lists < num_templates)
20691     {
20692       if (declarator && !current_function_decl)
20693         error_at (location, "specializing member %<%T::%E%> "
20694                   "requires %<template<>%> syntax", 
20695                   declarator->u.id.qualifying_scope,
20696                   declarator->u.id.unqualified_name);
20697       else if (declarator)
20698         error_at (location, "invalid declaration of %<%T::%E%>",
20699                   declarator->u.id.qualifying_scope,
20700                   declarator->u.id.unqualified_name);
20701       else 
20702         error_at (location, "too few template-parameter-lists");
20703       return false;
20704     }
20705   /* Otherwise, there are too many template parameter lists.  We have
20706      something like:
20707
20708      template <class T> template <class U> void S::f();  */
20709   error_at (location, "too many template-parameter-lists");
20710   return false;
20711 }
20712
20713 /* Parse an optional `::' token indicating that the following name is
20714    from the global namespace.  If so, PARSER->SCOPE is set to the
20715    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20716    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20717    Returns the new value of PARSER->SCOPE, if the `::' token is
20718    present, and NULL_TREE otherwise.  */
20719
20720 static tree
20721 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20722 {
20723   cp_token *token;
20724
20725   /* Peek at the next token.  */
20726   token = cp_lexer_peek_token (parser->lexer);
20727   /* If we're looking at a `::' token then we're starting from the
20728      global namespace, not our current location.  */
20729   if (token->type == CPP_SCOPE)
20730     {
20731       /* Consume the `::' token.  */
20732       cp_lexer_consume_token (parser->lexer);
20733       /* Set the SCOPE so that we know where to start the lookup.  */
20734       parser->scope = global_namespace;
20735       parser->qualifying_scope = global_namespace;
20736       parser->object_scope = NULL_TREE;
20737
20738       return parser->scope;
20739     }
20740   else if (!current_scope_valid_p)
20741     {
20742       parser->scope = NULL_TREE;
20743       parser->qualifying_scope = NULL_TREE;
20744       parser->object_scope = NULL_TREE;
20745     }
20746
20747   return NULL_TREE;
20748 }
20749
20750 /* Returns TRUE if the upcoming token sequence is the start of a
20751    constructor declarator.  If FRIEND_P is true, the declarator is
20752    preceded by the `friend' specifier.  */
20753
20754 static bool
20755 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20756 {
20757   bool constructor_p;
20758   tree nested_name_specifier;
20759   cp_token *next_token;
20760
20761   /* The common case is that this is not a constructor declarator, so
20762      try to avoid doing lots of work if at all possible.  It's not
20763      valid declare a constructor at function scope.  */
20764   if (parser->in_function_body)
20765     return false;
20766   /* And only certain tokens can begin a constructor declarator.  */
20767   next_token = cp_lexer_peek_token (parser->lexer);
20768   if (next_token->type != CPP_NAME
20769       && next_token->type != CPP_SCOPE
20770       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20771       && next_token->type != CPP_TEMPLATE_ID)
20772     return false;
20773
20774   /* Parse tentatively; we are going to roll back all of the tokens
20775      consumed here.  */
20776   cp_parser_parse_tentatively (parser);
20777   /* Assume that we are looking at a constructor declarator.  */
20778   constructor_p = true;
20779
20780   /* Look for the optional `::' operator.  */
20781   cp_parser_global_scope_opt (parser,
20782                               /*current_scope_valid_p=*/false);
20783   /* Look for the nested-name-specifier.  */
20784   nested_name_specifier
20785     = (cp_parser_nested_name_specifier_opt (parser,
20786                                             /*typename_keyword_p=*/false,
20787                                             /*check_dependency_p=*/false,
20788                                             /*type_p=*/false,
20789                                             /*is_declaration=*/false));
20790   /* Outside of a class-specifier, there must be a
20791      nested-name-specifier.  */
20792   if (!nested_name_specifier &&
20793       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20794        || friend_p))
20795     constructor_p = false;
20796   else if (nested_name_specifier == error_mark_node)
20797     constructor_p = false;
20798
20799   /* If we have a class scope, this is easy; DR 147 says that S::S always
20800      names the constructor, and no other qualified name could.  */
20801   if (constructor_p && nested_name_specifier
20802       && CLASS_TYPE_P (nested_name_specifier))
20803     {
20804       tree id = cp_parser_unqualified_id (parser,
20805                                           /*template_keyword_p=*/false,
20806                                           /*check_dependency_p=*/false,
20807                                           /*declarator_p=*/true,
20808                                           /*optional_p=*/false);
20809       if (is_overloaded_fn (id))
20810         id = DECL_NAME (get_first_fn (id));
20811       if (!constructor_name_p (id, nested_name_specifier))
20812         constructor_p = false;
20813     }
20814   /* If we still think that this might be a constructor-declarator,
20815      look for a class-name.  */
20816   else if (constructor_p)
20817     {
20818       /* If we have:
20819
20820            template <typename T> struct S {
20821              S();
20822            };
20823
20824          we must recognize that the nested `S' names a class.  */
20825       tree type_decl;
20826       type_decl = cp_parser_class_name (parser,
20827                                         /*typename_keyword_p=*/false,
20828                                         /*template_keyword_p=*/false,
20829                                         none_type,
20830                                         /*check_dependency_p=*/false,
20831                                         /*class_head_p=*/false,
20832                                         /*is_declaration=*/false);
20833       /* If there was no class-name, then this is not a constructor.  */
20834       constructor_p = !cp_parser_error_occurred (parser);
20835
20836       /* If we're still considering a constructor, we have to see a `(',
20837          to begin the parameter-declaration-clause, followed by either a
20838          `)', an `...', or a decl-specifier.  We need to check for a
20839          type-specifier to avoid being fooled into thinking that:
20840
20841            S (f) (int);
20842
20843          is a constructor.  (It is actually a function named `f' that
20844          takes one parameter (of type `int') and returns a value of type
20845          `S'.  */
20846       if (constructor_p
20847           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20848         constructor_p = false;
20849
20850       if (constructor_p
20851           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20852           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20853           /* A parameter declaration begins with a decl-specifier,
20854              which is either the "attribute" keyword, a storage class
20855              specifier, or (usually) a type-specifier.  */
20856           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20857         {
20858           tree type;
20859           tree pushed_scope = NULL_TREE;
20860           unsigned saved_num_template_parameter_lists;
20861
20862           /* Names appearing in the type-specifier should be looked up
20863              in the scope of the class.  */
20864           if (current_class_type)
20865             type = NULL_TREE;
20866           else
20867             {
20868               type = TREE_TYPE (type_decl);
20869               if (TREE_CODE (type) == TYPENAME_TYPE)
20870                 {
20871                   type = resolve_typename_type (type,
20872                                                 /*only_current_p=*/false);
20873                   if (TREE_CODE (type) == TYPENAME_TYPE)
20874                     {
20875                       cp_parser_abort_tentative_parse (parser);
20876                       return false;
20877                     }
20878                 }
20879               pushed_scope = push_scope (type);
20880             }
20881
20882           /* Inside the constructor parameter list, surrounding
20883              template-parameter-lists do not apply.  */
20884           saved_num_template_parameter_lists
20885             = parser->num_template_parameter_lists;
20886           parser->num_template_parameter_lists = 0;
20887
20888           /* Look for the type-specifier.  */
20889           cp_parser_type_specifier (parser,
20890                                     CP_PARSER_FLAGS_NONE,
20891                                     /*decl_specs=*/NULL,
20892                                     /*is_declarator=*/true,
20893                                     /*declares_class_or_enum=*/NULL,
20894                                     /*is_cv_qualifier=*/NULL);
20895
20896           parser->num_template_parameter_lists
20897             = saved_num_template_parameter_lists;
20898
20899           /* Leave the scope of the class.  */
20900           if (pushed_scope)
20901             pop_scope (pushed_scope);
20902
20903           constructor_p = !cp_parser_error_occurred (parser);
20904         }
20905     }
20906
20907   /* We did not really want to consume any tokens.  */
20908   cp_parser_abort_tentative_parse (parser);
20909
20910   return constructor_p;
20911 }
20912
20913 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20914    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20915    they must be performed once we are in the scope of the function.
20916
20917    Returns the function defined.  */
20918
20919 static tree
20920 cp_parser_function_definition_from_specifiers_and_declarator
20921   (cp_parser* parser,
20922    cp_decl_specifier_seq *decl_specifiers,
20923    tree attributes,
20924    const cp_declarator *declarator)
20925 {
20926   tree fn;
20927   bool success_p;
20928
20929   /* Begin the function-definition.  */
20930   success_p = start_function (decl_specifiers, declarator, attributes);
20931
20932   /* The things we're about to see are not directly qualified by any
20933      template headers we've seen thus far.  */
20934   reset_specialization ();
20935
20936   /* If there were names looked up in the decl-specifier-seq that we
20937      did not check, check them now.  We must wait until we are in the
20938      scope of the function to perform the checks, since the function
20939      might be a friend.  */
20940   perform_deferred_access_checks ();
20941
20942   if (!success_p)
20943     {
20944       /* Skip the entire function.  */
20945       cp_parser_skip_to_end_of_block_or_statement (parser);
20946       fn = error_mark_node;
20947     }
20948   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20949     {
20950       /* Seen already, skip it.  An error message has already been output.  */
20951       cp_parser_skip_to_end_of_block_or_statement (parser);
20952       fn = current_function_decl;
20953       current_function_decl = NULL_TREE;
20954       /* If this is a function from a class, pop the nested class.  */
20955       if (current_class_name)
20956         pop_nested_class ();
20957     }
20958   else
20959     {
20960       timevar_id_t tv;
20961       if (DECL_DECLARED_INLINE_P (current_function_decl))
20962         tv = TV_PARSE_INLINE;
20963       else
20964         tv = TV_PARSE_FUNC;
20965       timevar_push (tv);
20966       fn = cp_parser_function_definition_after_declarator (parser,
20967                                                          /*inline_p=*/false);
20968       timevar_pop (tv);
20969     }
20970
20971   return fn;
20972 }
20973
20974 /* Parse the part of a function-definition that follows the
20975    declarator.  INLINE_P is TRUE iff this function is an inline
20976    function defined within a class-specifier.
20977
20978    Returns the function defined.  */
20979
20980 static tree
20981 cp_parser_function_definition_after_declarator (cp_parser* parser,
20982                                                 bool inline_p)
20983 {
20984   tree fn;
20985   bool ctor_initializer_p = false;
20986   bool saved_in_unbraced_linkage_specification_p;
20987   bool saved_in_function_body;
20988   unsigned saved_num_template_parameter_lists;
20989   cp_token *token;
20990
20991   saved_in_function_body = parser->in_function_body;
20992   parser->in_function_body = true;
20993   /* If the next token is `return', then the code may be trying to
20994      make use of the "named return value" extension that G++ used to
20995      support.  */
20996   token = cp_lexer_peek_token (parser->lexer);
20997   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20998     {
20999       /* Consume the `return' keyword.  */
21000       cp_lexer_consume_token (parser->lexer);
21001       /* Look for the identifier that indicates what value is to be
21002          returned.  */
21003       cp_parser_identifier (parser);
21004       /* Issue an error message.  */
21005       error_at (token->location,
21006                 "named return values are no longer supported");
21007       /* Skip tokens until we reach the start of the function body.  */
21008       while (true)
21009         {
21010           cp_token *token = cp_lexer_peek_token (parser->lexer);
21011           if (token->type == CPP_OPEN_BRACE
21012               || token->type == CPP_EOF
21013               || token->type == CPP_PRAGMA_EOL)
21014             break;
21015           cp_lexer_consume_token (parser->lexer);
21016         }
21017     }
21018   /* The `extern' in `extern "C" void f () { ... }' does not apply to
21019      anything declared inside `f'.  */
21020   saved_in_unbraced_linkage_specification_p
21021     = parser->in_unbraced_linkage_specification_p;
21022   parser->in_unbraced_linkage_specification_p = false;
21023   /* Inside the function, surrounding template-parameter-lists do not
21024      apply.  */
21025   saved_num_template_parameter_lists
21026     = parser->num_template_parameter_lists;
21027   parser->num_template_parameter_lists = 0;
21028
21029   start_lambda_scope (current_function_decl);
21030
21031   /* If the next token is `try', `__transaction_atomic', or
21032      `__transaction_relaxed`, then we are looking at either function-try-block
21033      or function-transaction-block.  Note that all of these include the
21034      function-body.  */
21035   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21036     ctor_initializer_p = cp_parser_function_transaction (parser,
21037         RID_TRANSACTION_ATOMIC);
21038   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21039       RID_TRANSACTION_RELAXED))
21040     ctor_initializer_p = cp_parser_function_transaction (parser,
21041         RID_TRANSACTION_RELAXED);
21042   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21043     ctor_initializer_p = cp_parser_function_try_block (parser);
21044   else
21045     ctor_initializer_p
21046       = cp_parser_ctor_initializer_opt_and_function_body (parser);
21047
21048   finish_lambda_scope ();
21049
21050   /* Finish the function.  */
21051   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21052                         (inline_p ? 2 : 0));
21053   /* Generate code for it, if necessary.  */
21054   expand_or_defer_fn (fn);
21055   /* Restore the saved values.  */
21056   parser->in_unbraced_linkage_specification_p
21057     = saved_in_unbraced_linkage_specification_p;
21058   parser->num_template_parameter_lists
21059     = saved_num_template_parameter_lists;
21060   parser->in_function_body = saved_in_function_body;
21061
21062   return fn;
21063 }
21064
21065 /* Parse a template-declaration, assuming that the `export' (and
21066    `extern') keywords, if present, has already been scanned.  MEMBER_P
21067    is as for cp_parser_template_declaration.  */
21068
21069 static void
21070 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21071 {
21072   tree decl = NULL_TREE;
21073   VEC (deferred_access_check,gc) *checks;
21074   tree parameter_list;
21075   bool friend_p = false;
21076   bool need_lang_pop;
21077   cp_token *token;
21078
21079   /* Look for the `template' keyword.  */
21080   token = cp_lexer_peek_token (parser->lexer);
21081   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21082     return;
21083
21084   /* And the `<'.  */
21085   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21086     return;
21087   if (at_class_scope_p () && current_function_decl)
21088     {
21089       /* 14.5.2.2 [temp.mem]
21090
21091          A local class shall not have member templates.  */
21092       error_at (token->location,
21093                 "invalid declaration of member template in local class");
21094       cp_parser_skip_to_end_of_block_or_statement (parser);
21095       return;
21096     }
21097   /* [temp]
21098
21099      A template ... shall not have C linkage.  */
21100   if (current_lang_name == lang_name_c)
21101     {
21102       error_at (token->location, "template with C linkage");
21103       /* Give it C++ linkage to avoid confusing other parts of the
21104          front end.  */
21105       push_lang_context (lang_name_cplusplus);
21106       need_lang_pop = true;
21107     }
21108   else
21109     need_lang_pop = false;
21110
21111   /* We cannot perform access checks on the template parameter
21112      declarations until we know what is being declared, just as we
21113      cannot check the decl-specifier list.  */
21114   push_deferring_access_checks (dk_deferred);
21115
21116   /* If the next token is `>', then we have an invalid
21117      specialization.  Rather than complain about an invalid template
21118      parameter, issue an error message here.  */
21119   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21120     {
21121       cp_parser_error (parser, "invalid explicit specialization");
21122       begin_specialization ();
21123       parameter_list = NULL_TREE;
21124     }
21125   else
21126     {
21127       /* Parse the template parameters.  */
21128       parameter_list = cp_parser_template_parameter_list (parser);
21129       fixup_template_parms ();
21130     }
21131
21132   /* Get the deferred access checks from the parameter list.  These
21133      will be checked once we know what is being declared, as for a
21134      member template the checks must be performed in the scope of the
21135      class containing the member.  */
21136   checks = get_deferred_access_checks ();
21137
21138   /* Look for the `>'.  */
21139   cp_parser_skip_to_end_of_template_parameter_list (parser);
21140   /* We just processed one more parameter list.  */
21141   ++parser->num_template_parameter_lists;
21142   /* If the next token is `template', there are more template
21143      parameters.  */
21144   if (cp_lexer_next_token_is_keyword (parser->lexer,
21145                                       RID_TEMPLATE))
21146     cp_parser_template_declaration_after_export (parser, member_p);
21147   else if (cxx_dialect >= cxx0x
21148            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21149     decl = cp_parser_alias_declaration (parser);
21150   else
21151     {
21152       /* There are no access checks when parsing a template, as we do not
21153          know if a specialization will be a friend.  */
21154       push_deferring_access_checks (dk_no_check);
21155       token = cp_lexer_peek_token (parser->lexer);
21156       decl = cp_parser_single_declaration (parser,
21157                                            checks,
21158                                            member_p,
21159                                            /*explicit_specialization_p=*/false,
21160                                            &friend_p);
21161       pop_deferring_access_checks ();
21162
21163       /* If this is a member template declaration, let the front
21164          end know.  */
21165       if (member_p && !friend_p && decl)
21166         {
21167           if (TREE_CODE (decl) == TYPE_DECL)
21168             cp_parser_check_access_in_redeclaration (decl, token->location);
21169
21170           decl = finish_member_template_decl (decl);
21171         }
21172       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21173         make_friend_class (current_class_type, TREE_TYPE (decl),
21174                            /*complain=*/true);
21175     }
21176   /* We are done with the current parameter list.  */
21177   --parser->num_template_parameter_lists;
21178
21179   pop_deferring_access_checks ();
21180
21181   /* Finish up.  */
21182   finish_template_decl (parameter_list);
21183
21184   /* Check the template arguments for a literal operator template.  */
21185   if (decl
21186       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21187       && UDLIT_OPER_P (DECL_NAME (decl)))
21188     {
21189       bool ok = true;
21190       if (parameter_list == NULL_TREE)
21191         ok = false;
21192       else
21193         {
21194           int num_parms = TREE_VEC_LENGTH (parameter_list);
21195           if (num_parms != 1)
21196             ok = false;
21197           else
21198             {
21199               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21200               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21201               if (TREE_TYPE (parm) != char_type_node
21202                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21203                 ok = false;
21204             }
21205         }
21206       if (!ok)
21207         error ("literal operator template %qD has invalid parameter list."
21208                "  Expected non-type template argument pack <char...>",
21209                decl);
21210     }
21211   /* Register member declarations.  */
21212   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21213     finish_member_declaration (decl);
21214   /* For the erroneous case of a template with C linkage, we pushed an
21215      implicit C++ linkage scope; exit that scope now.  */
21216   if (need_lang_pop)
21217     pop_lang_context ();
21218   /* If DECL is a function template, we must return to parse it later.
21219      (Even though there is no definition, there might be default
21220      arguments that need handling.)  */
21221   if (member_p && decl
21222       && (TREE_CODE (decl) == FUNCTION_DECL
21223           || DECL_FUNCTION_TEMPLATE_P (decl)))
21224     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21225 }
21226
21227 /* Perform the deferred access checks from a template-parameter-list.
21228    CHECKS is a TREE_LIST of access checks, as returned by
21229    get_deferred_access_checks.  */
21230
21231 static void
21232 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21233 {
21234   ++processing_template_parmlist;
21235   perform_access_checks (checks);
21236   --processing_template_parmlist;
21237 }
21238
21239 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21240    `function-definition' sequence.  MEMBER_P is true, this declaration
21241    appears in a class scope.
21242
21243    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21244    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21245
21246 static tree
21247 cp_parser_single_declaration (cp_parser* parser,
21248                               VEC (deferred_access_check,gc)* checks,
21249                               bool member_p,
21250                               bool explicit_specialization_p,
21251                               bool* friend_p)
21252 {
21253   int declares_class_or_enum;
21254   tree decl = NULL_TREE;
21255   cp_decl_specifier_seq decl_specifiers;
21256   bool function_definition_p = false;
21257   cp_token *decl_spec_token_start;
21258
21259   /* This function is only used when processing a template
21260      declaration.  */
21261   gcc_assert (innermost_scope_kind () == sk_template_parms
21262               || innermost_scope_kind () == sk_template_spec);
21263
21264   /* Defer access checks until we know what is being declared.  */
21265   push_deferring_access_checks (dk_deferred);
21266
21267   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21268      alternative.  */
21269   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21270   cp_parser_decl_specifier_seq (parser,
21271                                 CP_PARSER_FLAGS_OPTIONAL,
21272                                 &decl_specifiers,
21273                                 &declares_class_or_enum);
21274   if (friend_p)
21275     *friend_p = cp_parser_friend_p (&decl_specifiers);
21276
21277   /* There are no template typedefs.  */
21278   if (decl_specifiers.specs[(int) ds_typedef])
21279     {
21280       error_at (decl_spec_token_start->location,
21281                 "template declaration of %<typedef%>");
21282       decl = error_mark_node;
21283     }
21284
21285   /* Gather up the access checks that occurred the
21286      decl-specifier-seq.  */
21287   stop_deferring_access_checks ();
21288
21289   /* Check for the declaration of a template class.  */
21290   if (declares_class_or_enum)
21291     {
21292       if (cp_parser_declares_only_class_p (parser))
21293         {
21294           decl = shadow_tag (&decl_specifiers);
21295
21296           /* In this case:
21297
21298                struct C {
21299                  friend template <typename T> struct A<T>::B;
21300                };
21301
21302              A<T>::B will be represented by a TYPENAME_TYPE, and
21303              therefore not recognized by shadow_tag.  */
21304           if (friend_p && *friend_p
21305               && !decl
21306               && decl_specifiers.type
21307               && TYPE_P (decl_specifiers.type))
21308             decl = decl_specifiers.type;
21309
21310           if (decl && decl != error_mark_node)
21311             decl = TYPE_NAME (decl);
21312           else
21313             decl = error_mark_node;
21314
21315           /* Perform access checks for template parameters.  */
21316           cp_parser_perform_template_parameter_access_checks (checks);
21317         }
21318     }
21319
21320   /* Complain about missing 'typename' or other invalid type names.  */
21321   if (!decl_specifiers.any_type_specifiers_p
21322       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21323     {
21324       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21325          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21326          the rest of this declaration.  */
21327       decl = error_mark_node;
21328       goto out;
21329     }
21330
21331   /* If it's not a template class, try for a template function.  If
21332      the next token is a `;', then this declaration does not declare
21333      anything.  But, if there were errors in the decl-specifiers, then
21334      the error might well have come from an attempted class-specifier.
21335      In that case, there's no need to warn about a missing declarator.  */
21336   if (!decl
21337       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21338           || decl_specifiers.type != error_mark_node))
21339     {
21340       decl = cp_parser_init_declarator (parser,
21341                                         &decl_specifiers,
21342                                         checks,
21343                                         /*function_definition_allowed_p=*/true,
21344                                         member_p,
21345                                         declares_class_or_enum,
21346                                         &function_definition_p,
21347                                         NULL);
21348
21349     /* 7.1.1-1 [dcl.stc]
21350
21351        A storage-class-specifier shall not be specified in an explicit
21352        specialization...  */
21353     if (decl
21354         && explicit_specialization_p
21355         && decl_specifiers.storage_class != sc_none)
21356       {
21357         error_at (decl_spec_token_start->location,
21358                   "explicit template specialization cannot have a storage class");
21359         decl = error_mark_node;
21360       }
21361     }
21362
21363   /* Look for a trailing `;' after the declaration.  */
21364   if (!function_definition_p
21365       && (decl == error_mark_node
21366           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21367     cp_parser_skip_to_end_of_block_or_statement (parser);
21368
21369  out:
21370   pop_deferring_access_checks ();
21371
21372   /* Clear any current qualification; whatever comes next is the start
21373      of something new.  */
21374   parser->scope = NULL_TREE;
21375   parser->qualifying_scope = NULL_TREE;
21376   parser->object_scope = NULL_TREE;
21377
21378   return decl;
21379 }
21380
21381 /* Parse a cast-expression that is not the operand of a unary "&".  */
21382
21383 static tree
21384 cp_parser_simple_cast_expression (cp_parser *parser)
21385 {
21386   return cp_parser_cast_expression (parser, /*address_p=*/false,
21387                                     /*cast_p=*/false, NULL);
21388 }
21389
21390 /* Parse a functional cast to TYPE.  Returns an expression
21391    representing the cast.  */
21392
21393 static tree
21394 cp_parser_functional_cast (cp_parser* parser, tree type)
21395 {
21396   VEC(tree,gc) *vec;
21397   tree expression_list;
21398   tree cast;
21399   bool nonconst_p;
21400
21401   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21402     {
21403       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21404       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21405       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21406       if (TREE_CODE (type) == TYPE_DECL)
21407         type = TREE_TYPE (type);
21408       return finish_compound_literal (type, expression_list,
21409                                       tf_warning_or_error);
21410     }
21411
21412
21413   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21414                                                  /*cast_p=*/true,
21415                                                  /*allow_expansion_p=*/true,
21416                                                  /*non_constant_p=*/NULL);
21417   if (vec == NULL)
21418     expression_list = error_mark_node;
21419   else
21420     {
21421       expression_list = build_tree_list_vec (vec);
21422       release_tree_vector (vec);
21423     }
21424
21425   cast = build_functional_cast (type, expression_list,
21426                                 tf_warning_or_error);
21427   /* [expr.const]/1: In an integral constant expression "only type
21428      conversions to integral or enumeration type can be used".  */
21429   if (TREE_CODE (type) == TYPE_DECL)
21430     type = TREE_TYPE (type);
21431   if (cast != error_mark_node
21432       && !cast_valid_in_integral_constant_expression_p (type)
21433       && cp_parser_non_integral_constant_expression (parser,
21434                                                      NIC_CONSTRUCTOR))
21435     return error_mark_node;
21436   return cast;
21437 }
21438
21439 /* Save the tokens that make up the body of a member function defined
21440    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21441    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21442    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21443    for the member function.  */
21444
21445 static tree
21446 cp_parser_save_member_function_body (cp_parser* parser,
21447                                      cp_decl_specifier_seq *decl_specifiers,
21448                                      cp_declarator *declarator,
21449                                      tree attributes)
21450 {
21451   cp_token *first;
21452   cp_token *last;
21453   tree fn;
21454
21455   /* Create the FUNCTION_DECL.  */
21456   fn = grokmethod (decl_specifiers, declarator, attributes);
21457   /* If something went badly wrong, bail out now.  */
21458   if (fn == error_mark_node)
21459     {
21460       /* If there's a function-body, skip it.  */
21461       if (cp_parser_token_starts_function_definition_p
21462           (cp_lexer_peek_token (parser->lexer)))
21463         cp_parser_skip_to_end_of_block_or_statement (parser);
21464       return error_mark_node;
21465     }
21466
21467   /* Remember it, if there default args to post process.  */
21468   cp_parser_save_default_args (parser, fn);
21469
21470   /* Save away the tokens that make up the body of the
21471      function.  */
21472   first = parser->lexer->next_token;
21473   /* We can have braced-init-list mem-initializers before the fn body.  */
21474   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21475     {
21476       cp_lexer_consume_token (parser->lexer);
21477       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21478              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21479         {
21480           /* cache_group will stop after an un-nested { } pair, too.  */
21481           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21482             break;
21483
21484           /* variadic mem-inits have ... after the ')'.  */
21485           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21486             cp_lexer_consume_token (parser->lexer);
21487         }
21488     }
21489   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21490   /* Handle function try blocks.  */
21491   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21492     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21493   last = parser->lexer->next_token;
21494
21495   /* Save away the inline definition; we will process it when the
21496      class is complete.  */
21497   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21498   DECL_PENDING_INLINE_P (fn) = 1;
21499
21500   /* We need to know that this was defined in the class, so that
21501      friend templates are handled correctly.  */
21502   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21503
21504   /* Add FN to the queue of functions to be parsed later.  */
21505   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21506
21507   return fn;
21508 }
21509
21510 /* Save the tokens that make up the in-class initializer for a non-static
21511    data member.  Returns a DEFAULT_ARG.  */
21512
21513 static tree
21514 cp_parser_save_nsdmi (cp_parser* parser)
21515 {
21516   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21517 }
21518
21519 /* Parse a template-argument-list, as well as the trailing ">" (but
21520    not the opening "<").  See cp_parser_template_argument_list for the
21521    return value.  */
21522
21523 static tree
21524 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21525 {
21526   tree arguments;
21527   tree saved_scope;
21528   tree saved_qualifying_scope;
21529   tree saved_object_scope;
21530   bool saved_greater_than_is_operator_p;
21531   int saved_unevaluated_operand;
21532   int saved_inhibit_evaluation_warnings;
21533
21534   /* [temp.names]
21535
21536      When parsing a template-id, the first non-nested `>' is taken as
21537      the end of the template-argument-list rather than a greater-than
21538      operator.  */
21539   saved_greater_than_is_operator_p
21540     = parser->greater_than_is_operator_p;
21541   parser->greater_than_is_operator_p = false;
21542   /* Parsing the argument list may modify SCOPE, so we save it
21543      here.  */
21544   saved_scope = parser->scope;
21545   saved_qualifying_scope = parser->qualifying_scope;
21546   saved_object_scope = parser->object_scope;
21547   /* We need to evaluate the template arguments, even though this
21548      template-id may be nested within a "sizeof".  */
21549   saved_unevaluated_operand = cp_unevaluated_operand;
21550   cp_unevaluated_operand = 0;
21551   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21552   c_inhibit_evaluation_warnings = 0;
21553   /* Parse the template-argument-list itself.  */
21554   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21555       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21556     arguments = NULL_TREE;
21557   else
21558     arguments = cp_parser_template_argument_list (parser);
21559   /* Look for the `>' that ends the template-argument-list. If we find
21560      a '>>' instead, it's probably just a typo.  */
21561   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21562     {
21563       if (cxx_dialect != cxx98)
21564         {
21565           /* In C++0x, a `>>' in a template argument list or cast
21566              expression is considered to be two separate `>'
21567              tokens. So, change the current token to a `>', but don't
21568              consume it: it will be consumed later when the outer
21569              template argument list (or cast expression) is parsed.
21570              Note that this replacement of `>' for `>>' is necessary
21571              even if we are parsing tentatively: in the tentative
21572              case, after calling
21573              cp_parser_enclosed_template_argument_list we will always
21574              throw away all of the template arguments and the first
21575              closing `>', either because the template argument list
21576              was erroneous or because we are replacing those tokens
21577              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21578              not have been thrown away) is needed either to close an
21579              outer template argument list or to complete a new-style
21580              cast.  */
21581           cp_token *token = cp_lexer_peek_token (parser->lexer);
21582           token->type = CPP_GREATER;
21583         }
21584       else if (!saved_greater_than_is_operator_p)
21585         {
21586           /* If we're in a nested template argument list, the '>>' has
21587             to be a typo for '> >'. We emit the error message, but we
21588             continue parsing and we push a '>' as next token, so that
21589             the argument list will be parsed correctly.  Note that the
21590             global source location is still on the token before the
21591             '>>', so we need to say explicitly where we want it.  */
21592           cp_token *token = cp_lexer_peek_token (parser->lexer);
21593           error_at (token->location, "%<>>%> should be %<> >%> "
21594                     "within a nested template argument list");
21595
21596           token->type = CPP_GREATER;
21597         }
21598       else
21599         {
21600           /* If this is not a nested template argument list, the '>>'
21601             is a typo for '>'. Emit an error message and continue.
21602             Same deal about the token location, but here we can get it
21603             right by consuming the '>>' before issuing the diagnostic.  */
21604           cp_token *token = cp_lexer_consume_token (parser->lexer);
21605           error_at (token->location,
21606                     "spurious %<>>%>, use %<>%> to terminate "
21607                     "a template argument list");
21608         }
21609     }
21610   else
21611     cp_parser_skip_to_end_of_template_parameter_list (parser);
21612   /* The `>' token might be a greater-than operator again now.  */
21613   parser->greater_than_is_operator_p
21614     = saved_greater_than_is_operator_p;
21615   /* Restore the SAVED_SCOPE.  */
21616   parser->scope = saved_scope;
21617   parser->qualifying_scope = saved_qualifying_scope;
21618   parser->object_scope = saved_object_scope;
21619   cp_unevaluated_operand = saved_unevaluated_operand;
21620   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21621
21622   return arguments;
21623 }
21624
21625 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21626    arguments, or the body of the function have not yet been parsed,
21627    parse them now.  */
21628
21629 static void
21630 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21631 {
21632   timevar_push (TV_PARSE_INMETH);
21633   /* If this member is a template, get the underlying
21634      FUNCTION_DECL.  */
21635   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21636     member_function = DECL_TEMPLATE_RESULT (member_function);
21637
21638   /* There should not be any class definitions in progress at this
21639      point; the bodies of members are only parsed outside of all class
21640      definitions.  */
21641   gcc_assert (parser->num_classes_being_defined == 0);
21642   /* While we're parsing the member functions we might encounter more
21643      classes.  We want to handle them right away, but we don't want
21644      them getting mixed up with functions that are currently in the
21645      queue.  */
21646   push_unparsed_function_queues (parser);
21647
21648   /* Make sure that any template parameters are in scope.  */
21649   maybe_begin_member_template_processing (member_function);
21650
21651   /* If the body of the function has not yet been parsed, parse it
21652      now.  */
21653   if (DECL_PENDING_INLINE_P (member_function))
21654     {
21655       tree function_scope;
21656       cp_token_cache *tokens;
21657
21658       /* The function is no longer pending; we are processing it.  */
21659       tokens = DECL_PENDING_INLINE_INFO (member_function);
21660       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21661       DECL_PENDING_INLINE_P (member_function) = 0;
21662
21663       /* If this is a local class, enter the scope of the containing
21664          function.  */
21665       function_scope = current_function_decl;
21666       if (function_scope)
21667         push_function_context ();
21668
21669       /* Push the body of the function onto the lexer stack.  */
21670       cp_parser_push_lexer_for_tokens (parser, tokens);
21671
21672       /* Let the front end know that we going to be defining this
21673          function.  */
21674       start_preparsed_function (member_function, NULL_TREE,
21675                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21676
21677       /* Don't do access checking if it is a templated function.  */
21678       if (processing_template_decl)
21679         push_deferring_access_checks (dk_no_check);
21680
21681       /* Now, parse the body of the function.  */
21682       cp_parser_function_definition_after_declarator (parser,
21683                                                       /*inline_p=*/true);
21684
21685       if (processing_template_decl)
21686         pop_deferring_access_checks ();
21687
21688       /* Leave the scope of the containing function.  */
21689       if (function_scope)
21690         pop_function_context ();
21691       cp_parser_pop_lexer (parser);
21692     }
21693
21694   /* Remove any template parameters from the symbol table.  */
21695   maybe_end_member_template_processing ();
21696
21697   /* Restore the queue.  */
21698   pop_unparsed_function_queues (parser);
21699   timevar_pop (TV_PARSE_INMETH);
21700 }
21701
21702 /* If DECL contains any default args, remember it on the unparsed
21703    functions queue.  */
21704
21705 static void
21706 cp_parser_save_default_args (cp_parser* parser, tree decl)
21707 {
21708   tree probe;
21709
21710   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21711        probe;
21712        probe = TREE_CHAIN (probe))
21713     if (TREE_PURPOSE (probe))
21714       {
21715         cp_default_arg_entry *entry
21716           = VEC_safe_push (cp_default_arg_entry, gc,
21717                            unparsed_funs_with_default_args, NULL);
21718         entry->class_type = current_class_type;
21719         entry->decl = decl;
21720         break;
21721       }
21722 }
21723
21724 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21725    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21726    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21727    from the parameter-type-list.  */
21728
21729 static tree
21730 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21731                                       tree default_arg, tree parmtype)
21732 {
21733   cp_token_cache *tokens;
21734   tree parsed_arg;
21735   bool dummy;
21736
21737   if (default_arg == error_mark_node)
21738     return error_mark_node;
21739
21740   /* Push the saved tokens for the default argument onto the parser's
21741      lexer stack.  */
21742   tokens = DEFARG_TOKENS (default_arg);
21743   cp_parser_push_lexer_for_tokens (parser, tokens);
21744
21745   start_lambda_scope (decl);
21746
21747   /* Parse the default argument.  */
21748   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21749   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21750     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21751
21752   finish_lambda_scope ();
21753
21754   if (!processing_template_decl)
21755     {
21756       /* In a non-template class, check conversions now.  In a template,
21757          we'll wait and instantiate these as needed.  */
21758       if (TREE_CODE (decl) == PARM_DECL)
21759         parsed_arg = check_default_argument (parmtype, parsed_arg);
21760       else
21761         {
21762           int flags = LOOKUP_IMPLICIT;
21763           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21764               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21765             flags = LOOKUP_NORMAL;
21766           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21767         }
21768     }
21769
21770   /* If the token stream has not been completely used up, then
21771      there was extra junk after the end of the default
21772      argument.  */
21773   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21774     {
21775       if (TREE_CODE (decl) == PARM_DECL)
21776         cp_parser_error (parser, "expected %<,%>");
21777       else
21778         cp_parser_error (parser, "expected %<;%>");
21779     }
21780
21781   /* Revert to the main lexer.  */
21782   cp_parser_pop_lexer (parser);
21783
21784   return parsed_arg;
21785 }
21786
21787 /* FIELD is a non-static data member with an initializer which we saved for
21788    later; parse it now.  */
21789
21790 static void
21791 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21792 {
21793   tree def;
21794
21795   push_unparsed_function_queues (parser);
21796   def = cp_parser_late_parse_one_default_arg (parser, field,
21797                                               DECL_INITIAL (field),
21798                                               NULL_TREE);
21799   pop_unparsed_function_queues (parser);
21800
21801   DECL_INITIAL (field) = def;
21802 }
21803
21804 /* FN is a FUNCTION_DECL which may contains a parameter with an
21805    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21806    assumes that the current scope is the scope in which the default
21807    argument should be processed.  */
21808
21809 static void
21810 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21811 {
21812   bool saved_local_variables_forbidden_p;
21813   tree parm, parmdecl;
21814
21815   /* While we're parsing the default args, we might (due to the
21816      statement expression extension) encounter more classes.  We want
21817      to handle them right away, but we don't want them getting mixed
21818      up with default args that are currently in the queue.  */
21819   push_unparsed_function_queues (parser);
21820
21821   /* Local variable names (and the `this' keyword) may not appear
21822      in a default argument.  */
21823   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21824   parser->local_variables_forbidden_p = true;
21825
21826   push_defarg_context (fn);
21827
21828   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21829          parmdecl = DECL_ARGUMENTS (fn);
21830        parm && parm != void_list_node;
21831        parm = TREE_CHAIN (parm),
21832          parmdecl = DECL_CHAIN (parmdecl))
21833     {
21834       tree default_arg = TREE_PURPOSE (parm);
21835       tree parsed_arg;
21836       VEC(tree,gc) *insts;
21837       tree copy;
21838       unsigned ix;
21839
21840       if (!default_arg)
21841         continue;
21842
21843       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21844         /* This can happen for a friend declaration for a function
21845            already declared with default arguments.  */
21846         continue;
21847
21848       parsed_arg
21849         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21850                                                 default_arg,
21851                                                 TREE_VALUE (parm));
21852       if (parsed_arg == error_mark_node)
21853         {
21854           continue;
21855         }
21856
21857       TREE_PURPOSE (parm) = parsed_arg;
21858
21859       /* Update any instantiations we've already created.  */
21860       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21861            VEC_iterate (tree, insts, ix, copy); ix++)
21862         TREE_PURPOSE (copy) = parsed_arg;
21863     }
21864
21865   pop_defarg_context ();
21866
21867   /* Make sure no default arg is missing.  */
21868   check_default_args (fn);
21869
21870   /* Restore the state of local_variables_forbidden_p.  */
21871   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21872
21873   /* Restore the queue.  */
21874   pop_unparsed_function_queues (parser);
21875 }
21876
21877 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21878    either a TYPE or an expression, depending on the form of the
21879    input.  The KEYWORD indicates which kind of expression we have
21880    encountered.  */
21881
21882 static tree
21883 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21884 {
21885   tree expr = NULL_TREE;
21886   const char *saved_message;
21887   char *tmp;
21888   bool saved_integral_constant_expression_p;
21889   bool saved_non_integral_constant_expression_p;
21890   bool pack_expansion_p = false;
21891
21892   /* Types cannot be defined in a `sizeof' expression.  Save away the
21893      old message.  */
21894   saved_message = parser->type_definition_forbidden_message;
21895   /* And create the new one.  */
21896   tmp = concat ("types may not be defined in %<",
21897                 IDENTIFIER_POINTER (ridpointers[keyword]),
21898                 "%> expressions", NULL);
21899   parser->type_definition_forbidden_message = tmp;
21900
21901   /* The restrictions on constant-expressions do not apply inside
21902      sizeof expressions.  */
21903   saved_integral_constant_expression_p
21904     = parser->integral_constant_expression_p;
21905   saved_non_integral_constant_expression_p
21906     = parser->non_integral_constant_expression_p;
21907   parser->integral_constant_expression_p = false;
21908
21909   /* If it's a `...', then we are computing the length of a parameter
21910      pack.  */
21911   if (keyword == RID_SIZEOF
21912       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21913     {
21914       /* Consume the `...'.  */
21915       cp_lexer_consume_token (parser->lexer);
21916       maybe_warn_variadic_templates ();
21917
21918       /* Note that this is an expansion.  */
21919       pack_expansion_p = true;
21920     }
21921
21922   /* Do not actually evaluate the expression.  */
21923   ++cp_unevaluated_operand;
21924   ++c_inhibit_evaluation_warnings;
21925   /* If it's a `(', then we might be looking at the type-id
21926      construction.  */
21927   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21928     {
21929       tree type;
21930       bool saved_in_type_id_in_expr_p;
21931
21932       /* We can't be sure yet whether we're looking at a type-id or an
21933          expression.  */
21934       cp_parser_parse_tentatively (parser);
21935       /* Consume the `('.  */
21936       cp_lexer_consume_token (parser->lexer);
21937       /* Parse the type-id.  */
21938       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21939       parser->in_type_id_in_expr_p = true;
21940       type = cp_parser_type_id (parser);
21941       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21942       /* Now, look for the trailing `)'.  */
21943       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21944       /* If all went well, then we're done.  */
21945       if (cp_parser_parse_definitely (parser))
21946         {
21947           cp_decl_specifier_seq decl_specs;
21948
21949           /* Build a trivial decl-specifier-seq.  */
21950           clear_decl_specs (&decl_specs);
21951           decl_specs.type = type;
21952
21953           /* Call grokdeclarator to figure out what type this is.  */
21954           expr = grokdeclarator (NULL,
21955                                  &decl_specs,
21956                                  TYPENAME,
21957                                  /*initialized=*/0,
21958                                  /*attrlist=*/NULL);
21959         }
21960     }
21961
21962   /* If the type-id production did not work out, then we must be
21963      looking at the unary-expression production.  */
21964   if (!expr)
21965     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21966                                        /*cast_p=*/false, NULL);
21967
21968   if (pack_expansion_p)
21969     /* Build a pack expansion. */
21970     expr = make_pack_expansion (expr);
21971
21972   /* Go back to evaluating expressions.  */
21973   --cp_unevaluated_operand;
21974   --c_inhibit_evaluation_warnings;
21975
21976   /* Free the message we created.  */
21977   free (tmp);
21978   /* And restore the old one.  */
21979   parser->type_definition_forbidden_message = saved_message;
21980   parser->integral_constant_expression_p
21981     = saved_integral_constant_expression_p;
21982   parser->non_integral_constant_expression_p
21983     = saved_non_integral_constant_expression_p;
21984
21985   return expr;
21986 }
21987
21988 /* If the current declaration has no declarator, return true.  */
21989
21990 static bool
21991 cp_parser_declares_only_class_p (cp_parser *parser)
21992 {
21993   /* If the next token is a `;' or a `,' then there is no
21994      declarator.  */
21995   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21996           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21997 }
21998
21999 /* Update the DECL_SPECS to reflect the storage class indicated by
22000    KEYWORD.  */
22001
22002 static void
22003 cp_parser_set_storage_class (cp_parser *parser,
22004                              cp_decl_specifier_seq *decl_specs,
22005                              enum rid keyword,
22006                              location_t location)
22007 {
22008   cp_storage_class storage_class;
22009
22010   if (parser->in_unbraced_linkage_specification_p)
22011     {
22012       error_at (location, "invalid use of %qD in linkage specification",
22013                 ridpointers[keyword]);
22014       return;
22015     }
22016   else if (decl_specs->storage_class != sc_none)
22017     {
22018       decl_specs->conflicting_specifiers_p = true;
22019       return;
22020     }
22021
22022   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22023       && decl_specs->specs[(int) ds_thread])
22024     {
22025       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22026       decl_specs->specs[(int) ds_thread] = 0;
22027     }
22028
22029   switch (keyword)
22030     {
22031     case RID_AUTO:
22032       storage_class = sc_auto;
22033       break;
22034     case RID_REGISTER:
22035       storage_class = sc_register;
22036       break;
22037     case RID_STATIC:
22038       storage_class = sc_static;
22039       break;
22040     case RID_EXTERN:
22041       storage_class = sc_extern;
22042       break;
22043     case RID_MUTABLE:
22044       storage_class = sc_mutable;
22045       break;
22046     default:
22047       gcc_unreachable ();
22048     }
22049   decl_specs->storage_class = storage_class;
22050
22051   /* A storage class specifier cannot be applied alongside a typedef 
22052      specifier. If there is a typedef specifier present then set 
22053      conflicting_specifiers_p which will trigger an error later
22054      on in grokdeclarator. */
22055   if (decl_specs->specs[(int)ds_typedef])
22056     decl_specs->conflicting_specifiers_p = true;
22057 }
22058
22059 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22060    is true, the type is a class or enum definition.  */
22061
22062 static void
22063 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22064                               tree type_spec,
22065                               location_t location,
22066                               bool type_definition_p)
22067 {
22068   decl_specs->any_specifiers_p = true;
22069
22070   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22071      (with, for example, in "typedef int wchar_t;") we remember that
22072      this is what happened.  In system headers, we ignore these
22073      declarations so that G++ can work with system headers that are not
22074      C++-safe.  */
22075   if (decl_specs->specs[(int) ds_typedef]
22076       && !type_definition_p
22077       && (type_spec == boolean_type_node
22078           || type_spec == char16_type_node
22079           || type_spec == char32_type_node
22080           || type_spec == wchar_type_node)
22081       && (decl_specs->type
22082           || decl_specs->specs[(int) ds_long]
22083           || decl_specs->specs[(int) ds_short]
22084           || decl_specs->specs[(int) ds_unsigned]
22085           || decl_specs->specs[(int) ds_signed]))
22086     {
22087       decl_specs->redefined_builtin_type = type_spec;
22088       if (!decl_specs->type)
22089         {
22090           decl_specs->type = type_spec;
22091           decl_specs->type_definition_p = false;
22092           decl_specs->type_location = location;
22093         }
22094     }
22095   else if (decl_specs->type)
22096     decl_specs->multiple_types_p = true;
22097   else
22098     {
22099       decl_specs->type = type_spec;
22100       decl_specs->type_definition_p = type_definition_p;
22101       decl_specs->redefined_builtin_type = NULL_TREE;
22102       decl_specs->type_location = location;
22103     }
22104 }
22105
22106 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22107    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22108
22109 static bool
22110 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22111 {
22112   return decl_specifiers->specs[(int) ds_friend] != 0;
22113 }
22114
22115 /* Issue an error message indicating that TOKEN_DESC was expected.
22116    If KEYWORD is true, it indicated this function is called by
22117    cp_parser_require_keword and the required token can only be
22118    a indicated keyword. */
22119
22120 static void
22121 cp_parser_required_error (cp_parser *parser,
22122                           required_token token_desc,
22123                           bool keyword)
22124 {
22125   switch (token_desc)
22126     {
22127       case RT_NEW:
22128         cp_parser_error (parser, "expected %<new%>");
22129         return;
22130       case RT_DELETE:
22131         cp_parser_error (parser, "expected %<delete%>");
22132         return;
22133       case RT_RETURN:
22134         cp_parser_error (parser, "expected %<return%>");
22135         return;
22136       case RT_WHILE:
22137         cp_parser_error (parser, "expected %<while%>");
22138         return;
22139       case RT_EXTERN:
22140         cp_parser_error (parser, "expected %<extern%>");
22141         return;
22142       case RT_STATIC_ASSERT:
22143         cp_parser_error (parser, "expected %<static_assert%>");
22144         return;
22145       case RT_DECLTYPE:
22146         cp_parser_error (parser, "expected %<decltype%>");
22147         return;
22148       case RT_OPERATOR:
22149         cp_parser_error (parser, "expected %<operator%>");
22150         return;
22151       case RT_CLASS:
22152         cp_parser_error (parser, "expected %<class%>");
22153         return;
22154       case RT_TEMPLATE:
22155         cp_parser_error (parser, "expected %<template%>");
22156         return;
22157       case RT_NAMESPACE:
22158         cp_parser_error (parser, "expected %<namespace%>");
22159         return;
22160       case RT_USING:
22161         cp_parser_error (parser, "expected %<using%>");
22162         return;
22163       case RT_ASM:
22164         cp_parser_error (parser, "expected %<asm%>");
22165         return;
22166       case RT_TRY:
22167         cp_parser_error (parser, "expected %<try%>");
22168         return;
22169       case RT_CATCH:
22170         cp_parser_error (parser, "expected %<catch%>");
22171         return;
22172       case RT_THROW:
22173         cp_parser_error (parser, "expected %<throw%>");
22174         return;
22175       case RT_LABEL:
22176         cp_parser_error (parser, "expected %<__label__%>");
22177         return;
22178       case RT_AT_TRY:
22179         cp_parser_error (parser, "expected %<@try%>");
22180         return;
22181       case RT_AT_SYNCHRONIZED:
22182         cp_parser_error (parser, "expected %<@synchronized%>");
22183         return;
22184       case RT_AT_THROW:
22185         cp_parser_error (parser, "expected %<@throw%>");
22186         return;
22187       case RT_TRANSACTION_ATOMIC:
22188         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22189         return;
22190       case RT_TRANSACTION_RELAXED:
22191         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22192         return;
22193       default:
22194         break;
22195     }
22196   if (!keyword)
22197     {
22198       switch (token_desc)
22199         {
22200           case RT_SEMICOLON:
22201             cp_parser_error (parser, "expected %<;%>");
22202             return;
22203           case RT_OPEN_PAREN:
22204             cp_parser_error (parser, "expected %<(%>");
22205             return;
22206           case RT_CLOSE_BRACE:
22207             cp_parser_error (parser, "expected %<}%>");
22208             return;
22209           case RT_OPEN_BRACE:
22210             cp_parser_error (parser, "expected %<{%>");
22211             return;
22212           case RT_CLOSE_SQUARE:
22213             cp_parser_error (parser, "expected %<]%>");
22214             return;
22215           case RT_OPEN_SQUARE:
22216             cp_parser_error (parser, "expected %<[%>");
22217             return;
22218           case RT_COMMA:
22219             cp_parser_error (parser, "expected %<,%>");
22220             return;
22221           case RT_SCOPE:
22222             cp_parser_error (parser, "expected %<::%>");
22223             return;
22224           case RT_LESS:
22225             cp_parser_error (parser, "expected %<<%>");
22226             return;
22227           case RT_GREATER:
22228             cp_parser_error (parser, "expected %<>%>");
22229             return;
22230           case RT_EQ:
22231             cp_parser_error (parser, "expected %<=%>");
22232             return;
22233           case RT_ELLIPSIS:
22234             cp_parser_error (parser, "expected %<...%>");
22235             return;
22236           case RT_MULT:
22237             cp_parser_error (parser, "expected %<*%>");
22238             return;
22239           case RT_COMPL:
22240             cp_parser_error (parser, "expected %<~%>");
22241             return;
22242           case RT_COLON:
22243             cp_parser_error (parser, "expected %<:%>");
22244             return;
22245           case RT_COLON_SCOPE:
22246             cp_parser_error (parser, "expected %<:%> or %<::%>");
22247             return;
22248           case RT_CLOSE_PAREN:
22249             cp_parser_error (parser, "expected %<)%>");
22250             return;
22251           case RT_COMMA_CLOSE_PAREN:
22252             cp_parser_error (parser, "expected %<,%> or %<)%>");
22253             return;
22254           case RT_PRAGMA_EOL:
22255             cp_parser_error (parser, "expected end of line");
22256             return;
22257           case RT_NAME:
22258             cp_parser_error (parser, "expected identifier");
22259             return;
22260           case RT_SELECT:
22261             cp_parser_error (parser, "expected selection-statement");
22262             return;
22263           case RT_INTERATION:
22264             cp_parser_error (parser, "expected iteration-statement");
22265             return;
22266           case RT_JUMP:
22267             cp_parser_error (parser, "expected jump-statement");
22268             return;
22269           case RT_CLASS_KEY:
22270             cp_parser_error (parser, "expected class-key");
22271             return;
22272           case RT_CLASS_TYPENAME_TEMPLATE:
22273             cp_parser_error (parser,
22274                  "expected %<class%>, %<typename%>, or %<template%>");
22275             return;
22276           default:
22277             gcc_unreachable ();
22278         }
22279     }
22280   else
22281     gcc_unreachable ();
22282 }
22283
22284
22285
22286 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22287    issue an error message indicating that TOKEN_DESC was expected.
22288
22289    Returns the token consumed, if the token had the appropriate type.
22290    Otherwise, returns NULL.  */
22291
22292 static cp_token *
22293 cp_parser_require (cp_parser* parser,
22294                    enum cpp_ttype type,
22295                    required_token token_desc)
22296 {
22297   if (cp_lexer_next_token_is (parser->lexer, type))
22298     return cp_lexer_consume_token (parser->lexer);
22299   else
22300     {
22301       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22302       if (!cp_parser_simulate_error (parser))
22303         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22304       return NULL;
22305     }
22306 }
22307
22308 /* An error message is produced if the next token is not '>'.
22309    All further tokens are skipped until the desired token is
22310    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22311
22312 static void
22313 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22314 {
22315   /* Current level of '< ... >'.  */
22316   unsigned level = 0;
22317   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22318   unsigned nesting_depth = 0;
22319
22320   /* Are we ready, yet?  If not, issue error message.  */
22321   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22322     return;
22323
22324   /* Skip tokens until the desired token is found.  */
22325   while (true)
22326     {
22327       /* Peek at the next token.  */
22328       switch (cp_lexer_peek_token (parser->lexer)->type)
22329         {
22330         case CPP_LESS:
22331           if (!nesting_depth)
22332             ++level;
22333           break;
22334
22335         case CPP_RSHIFT:
22336           if (cxx_dialect == cxx98)
22337             /* C++0x views the `>>' operator as two `>' tokens, but
22338                C++98 does not. */
22339             break;
22340           else if (!nesting_depth && level-- == 0)
22341             {
22342               /* We've hit a `>>' where the first `>' closes the
22343                  template argument list, and the second `>' is
22344                  spurious.  Just consume the `>>' and stop; we've
22345                  already produced at least one error.  */
22346               cp_lexer_consume_token (parser->lexer);
22347               return;
22348             }
22349           /* Fall through for C++0x, so we handle the second `>' in
22350              the `>>'.  */
22351
22352         case CPP_GREATER:
22353           if (!nesting_depth && level-- == 0)
22354             {
22355               /* We've reached the token we want, consume it and stop.  */
22356               cp_lexer_consume_token (parser->lexer);
22357               return;
22358             }
22359           break;
22360
22361         case CPP_OPEN_PAREN:
22362         case CPP_OPEN_SQUARE:
22363           ++nesting_depth;
22364           break;
22365
22366         case CPP_CLOSE_PAREN:
22367         case CPP_CLOSE_SQUARE:
22368           if (nesting_depth-- == 0)
22369             return;
22370           break;
22371
22372         case CPP_EOF:
22373         case CPP_PRAGMA_EOL:
22374         case CPP_SEMICOLON:
22375         case CPP_OPEN_BRACE:
22376         case CPP_CLOSE_BRACE:
22377           /* The '>' was probably forgotten, don't look further.  */
22378           return;
22379
22380         default:
22381           break;
22382         }
22383
22384       /* Consume this token.  */
22385       cp_lexer_consume_token (parser->lexer);
22386     }
22387 }
22388
22389 /* If the next token is the indicated keyword, consume it.  Otherwise,
22390    issue an error message indicating that TOKEN_DESC was expected.
22391
22392    Returns the token consumed, if the token had the appropriate type.
22393    Otherwise, returns NULL.  */
22394
22395 static cp_token *
22396 cp_parser_require_keyword (cp_parser* parser,
22397                            enum rid keyword,
22398                            required_token token_desc)
22399 {
22400   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22401
22402   if (token && token->keyword != keyword)
22403     {
22404       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22405       return NULL;
22406     }
22407
22408   return token;
22409 }
22410
22411 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22412    function-definition.  */
22413
22414 static bool
22415 cp_parser_token_starts_function_definition_p (cp_token* token)
22416 {
22417   return (/* An ordinary function-body begins with an `{'.  */
22418           token->type == CPP_OPEN_BRACE
22419           /* A ctor-initializer begins with a `:'.  */
22420           || token->type == CPP_COLON
22421           /* A function-try-block begins with `try'.  */
22422           || token->keyword == RID_TRY
22423           /* A function-transaction-block begins with `__transaction_atomic'
22424              or `__transaction_relaxed'.  */
22425           || token->keyword == RID_TRANSACTION_ATOMIC
22426           || token->keyword == RID_TRANSACTION_RELAXED
22427           /* The named return value extension begins with `return'.  */
22428           || token->keyword == RID_RETURN);
22429 }
22430
22431 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22432    definition.  */
22433
22434 static bool
22435 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22436 {
22437   cp_token *token;
22438
22439   token = cp_lexer_peek_token (parser->lexer);
22440   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22441 }
22442
22443 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22444    C++0x) ending a template-argument.  */
22445
22446 static bool
22447 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22448 {
22449   cp_token *token;
22450
22451   token = cp_lexer_peek_token (parser->lexer);
22452   return (token->type == CPP_COMMA 
22453           || token->type == CPP_GREATER
22454           || token->type == CPP_ELLIPSIS
22455           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22456 }
22457
22458 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22459    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22460
22461 static bool
22462 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22463                                                      size_t n)
22464 {
22465   cp_token *token;
22466
22467   token = cp_lexer_peek_nth_token (parser->lexer, n);
22468   if (token->type == CPP_LESS)
22469     return true;
22470   /* Check for the sequence `<::' in the original code. It would be lexed as
22471      `[:', where `[' is a digraph, and there is no whitespace before
22472      `:'.  */
22473   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22474     {
22475       cp_token *token2;
22476       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22477       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22478         return true;
22479     }
22480   return false;
22481 }
22482
22483 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22484    or none_type otherwise.  */
22485
22486 static enum tag_types
22487 cp_parser_token_is_class_key (cp_token* token)
22488 {
22489   switch (token->keyword)
22490     {
22491     case RID_CLASS:
22492       return class_type;
22493     case RID_STRUCT:
22494       return record_type;
22495     case RID_UNION:
22496       return union_type;
22497
22498     default:
22499       return none_type;
22500     }
22501 }
22502
22503 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22504
22505 static void
22506 cp_parser_check_class_key (enum tag_types class_key, tree type)
22507 {
22508   if (type == error_mark_node)
22509     return;
22510   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22511     {
22512       permerror (input_location, "%qs tag used in naming %q#T",
22513                  class_key == union_type ? "union"
22514                  : class_key == record_type ? "struct" : "class",
22515                  type);
22516       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22517               "%q#T was previously declared here", type);
22518     }
22519 }
22520
22521 /* Issue an error message if DECL is redeclared with different
22522    access than its original declaration [class.access.spec/3].
22523    This applies to nested classes and nested class templates.
22524    [class.mem/1].  */
22525
22526 static void
22527 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22528 {
22529   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22530     return;
22531
22532   if ((TREE_PRIVATE (decl)
22533        != (current_access_specifier == access_private_node))
22534       || (TREE_PROTECTED (decl)
22535           != (current_access_specifier == access_protected_node)))
22536     error_at (location, "%qD redeclared with different access", decl);
22537 }
22538
22539 /* Look for the `template' keyword, as a syntactic disambiguator.
22540    Return TRUE iff it is present, in which case it will be
22541    consumed.  */
22542
22543 static bool
22544 cp_parser_optional_template_keyword (cp_parser *parser)
22545 {
22546   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22547     {
22548       /* The `template' keyword can only be used within templates;
22549          outside templates the parser can always figure out what is a
22550          template and what is not.  */
22551       if (!processing_template_decl)
22552         {
22553           cp_token *token = cp_lexer_peek_token (parser->lexer);
22554           error_at (token->location,
22555                     "%<template%> (as a disambiguator) is only allowed "
22556                     "within templates");
22557           /* If this part of the token stream is rescanned, the same
22558              error message would be generated.  So, we purge the token
22559              from the stream.  */
22560           cp_lexer_purge_token (parser->lexer);
22561           return false;
22562         }
22563       else
22564         {
22565           /* Consume the `template' keyword.  */
22566           cp_lexer_consume_token (parser->lexer);
22567           return true;
22568         }
22569     }
22570
22571   return false;
22572 }
22573
22574 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22575    set PARSER->SCOPE, and perform other related actions.  */
22576
22577 static void
22578 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22579 {
22580   int i;
22581   struct tree_check *check_value;
22582   deferred_access_check *chk;
22583   VEC (deferred_access_check,gc) *checks;
22584
22585   /* Get the stored value.  */
22586   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22587   /* Perform any access checks that were deferred.  */
22588   checks = check_value->checks;
22589   if (checks)
22590     {
22591       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22592         perform_or_defer_access_check (chk->binfo,
22593                                        chk->decl,
22594                                        chk->diag_decl);
22595     }
22596   /* Set the scope from the stored value.  */
22597   parser->scope = check_value->value;
22598   parser->qualifying_scope = check_value->qualifying_scope;
22599   parser->object_scope = NULL_TREE;
22600 }
22601
22602 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22603    encounter the end of a block before what we were looking for.  */
22604
22605 static bool
22606 cp_parser_cache_group (cp_parser *parser,
22607                        enum cpp_ttype end,
22608                        unsigned depth)
22609 {
22610   while (true)
22611     {
22612       cp_token *token = cp_lexer_peek_token (parser->lexer);
22613
22614       /* Abort a parenthesized expression if we encounter a semicolon.  */
22615       if ((end == CPP_CLOSE_PAREN || depth == 0)
22616           && token->type == CPP_SEMICOLON)
22617         return true;
22618       /* If we've reached the end of the file, stop.  */
22619       if (token->type == CPP_EOF
22620           || (end != CPP_PRAGMA_EOL
22621               && token->type == CPP_PRAGMA_EOL))
22622         return true;
22623       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22624         /* We've hit the end of an enclosing block, so there's been some
22625            kind of syntax error.  */
22626         return true;
22627
22628       /* Consume the token.  */
22629       cp_lexer_consume_token (parser->lexer);
22630       /* See if it starts a new group.  */
22631       if (token->type == CPP_OPEN_BRACE)
22632         {
22633           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22634           /* In theory this should probably check end == '}', but
22635              cp_parser_save_member_function_body needs it to exit
22636              after either '}' or ')' when called with ')'.  */
22637           if (depth == 0)
22638             return false;
22639         }
22640       else if (token->type == CPP_OPEN_PAREN)
22641         {
22642           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22643           if (depth == 0 && end == CPP_CLOSE_PAREN)
22644             return false;
22645         }
22646       else if (token->type == CPP_PRAGMA)
22647         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22648       else if (token->type == end)
22649         return false;
22650     }
22651 }
22652
22653 /* Like above, for caching a default argument or NSDMI.  Both of these are
22654    terminated by a non-nested comma, but it can be unclear whether or not a
22655    comma is nested in a template argument list unless we do more parsing.
22656    In order to handle this ambiguity, when we encounter a ',' after a '<'
22657    we try to parse what follows as a parameter-declaration-list (in the
22658    case of a default argument) or a member-declarator (in the case of an
22659    NSDMI).  If that succeeds, then we stop caching.  */
22660
22661 static tree
22662 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22663 {
22664   unsigned depth = 0;
22665   int maybe_template_id = 0;
22666   cp_token *first_token;
22667   cp_token *token;
22668   tree default_argument;
22669
22670   /* Add tokens until we have processed the entire default
22671      argument.  We add the range [first_token, token).  */
22672   first_token = cp_lexer_peek_token (parser->lexer);
22673   if (first_token->type == CPP_OPEN_BRACE)
22674     {
22675       /* For list-initialization, this is straightforward.  */
22676       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22677       token = cp_lexer_peek_token (parser->lexer);
22678     }
22679   else while (true)
22680     {
22681       bool done = false;
22682
22683       /* Peek at the next token.  */
22684       token = cp_lexer_peek_token (parser->lexer);
22685       /* What we do depends on what token we have.  */
22686       switch (token->type)
22687         {
22688           /* In valid code, a default argument must be
22689              immediately followed by a `,' `)', or `...'.  */
22690         case CPP_COMMA:
22691           if (depth == 0 && maybe_template_id)
22692             {
22693               /* If we've seen a '<', we might be in a
22694                  template-argument-list.  Until Core issue 325 is
22695                  resolved, we don't know how this situation ought
22696                  to be handled, so try to DTRT.  We check whether
22697                  what comes after the comma is a valid parameter
22698                  declaration list.  If it is, then the comma ends
22699                  the default argument; otherwise the default
22700                  argument continues.  */
22701               bool error = false;
22702               tree t;
22703
22704               /* Set ITALP so cp_parser_parameter_declaration_list
22705                  doesn't decide to commit to this parse.  */
22706               bool saved_italp = parser->in_template_argument_list_p;
22707               parser->in_template_argument_list_p = true;
22708
22709               cp_parser_parse_tentatively (parser);
22710               cp_lexer_consume_token (parser->lexer);
22711
22712               if (nsdmi)
22713                 {
22714                   int ctor_dtor_or_conv_p;
22715                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22716                                         &ctor_dtor_or_conv_p,
22717                                         /*parenthesized_p=*/NULL,
22718                                         /*member_p=*/true);
22719                 }
22720               else
22721                 {
22722                   begin_scope (sk_function_parms, NULL_TREE);
22723                   cp_parser_parameter_declaration_list (parser, &error);
22724                   for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22725                     pop_binding (DECL_NAME (t), t);
22726                   leave_scope ();
22727                 }
22728               if (!cp_parser_error_occurred (parser) && !error)
22729                 done = true;
22730               cp_parser_abort_tentative_parse (parser);
22731
22732               parser->in_template_argument_list_p = saved_italp;
22733               break;
22734             }
22735         case CPP_CLOSE_PAREN:
22736         case CPP_ELLIPSIS:
22737           /* If we run into a non-nested `;', `}', or `]',
22738              then the code is invalid -- but the default
22739              argument is certainly over.  */
22740         case CPP_SEMICOLON:
22741         case CPP_CLOSE_BRACE:
22742         case CPP_CLOSE_SQUARE:
22743           if (depth == 0)
22744             done = true;
22745           /* Update DEPTH, if necessary.  */
22746           else if (token->type == CPP_CLOSE_PAREN
22747                    || token->type == CPP_CLOSE_BRACE
22748                    || token->type == CPP_CLOSE_SQUARE)
22749             --depth;
22750           break;
22751
22752         case CPP_OPEN_PAREN:
22753         case CPP_OPEN_SQUARE:
22754         case CPP_OPEN_BRACE:
22755           ++depth;
22756           break;
22757
22758         case CPP_LESS:
22759           if (depth == 0)
22760             /* This might be the comparison operator, or it might
22761                start a template argument list.  */
22762             ++maybe_template_id;
22763           break;
22764
22765         case CPP_RSHIFT:
22766           if (cxx_dialect == cxx98)
22767             break;
22768           /* Fall through for C++0x, which treats the `>>'
22769              operator like two `>' tokens in certain
22770              cases.  */
22771
22772         case CPP_GREATER:
22773           if (depth == 0)
22774             {
22775               /* This might be an operator, or it might close a
22776                  template argument list.  But if a previous '<'
22777                  started a template argument list, this will have
22778                  closed it, so we can't be in one anymore.  */
22779               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22780               if (maybe_template_id < 0)
22781                 maybe_template_id = 0;
22782             }
22783           break;
22784
22785           /* If we run out of tokens, issue an error message.  */
22786         case CPP_EOF:
22787         case CPP_PRAGMA_EOL:
22788           error_at (token->location, "file ends in default argument");
22789           done = true;
22790           break;
22791
22792         case CPP_NAME:
22793         case CPP_SCOPE:
22794           /* In these cases, we should look for template-ids.
22795              For example, if the default argument is
22796              `X<int, double>()', we need to do name lookup to
22797              figure out whether or not `X' is a template; if
22798              so, the `,' does not end the default argument.
22799
22800              That is not yet done.  */
22801           break;
22802
22803         default:
22804           break;
22805         }
22806
22807       /* If we've reached the end, stop.  */
22808       if (done)
22809         break;
22810
22811       /* Add the token to the token block.  */
22812       token = cp_lexer_consume_token (parser->lexer);
22813     }
22814
22815   /* Create a DEFAULT_ARG to represent the unparsed default
22816      argument.  */
22817   default_argument = make_node (DEFAULT_ARG);
22818   DEFARG_TOKENS (default_argument)
22819     = cp_token_cache_new (first_token, token);
22820   DEFARG_INSTANTIATIONS (default_argument) = NULL;
22821
22822   return default_argument;
22823 }
22824
22825 /* Begin parsing tentatively.  We always save tokens while parsing
22826    tentatively so that if the tentative parsing fails we can restore the
22827    tokens.  */
22828
22829 static void
22830 cp_parser_parse_tentatively (cp_parser* parser)
22831 {
22832   /* Enter a new parsing context.  */
22833   parser->context = cp_parser_context_new (parser->context);
22834   /* Begin saving tokens.  */
22835   cp_lexer_save_tokens (parser->lexer);
22836   /* In order to avoid repetitive access control error messages,
22837      access checks are queued up until we are no longer parsing
22838      tentatively.  */
22839   push_deferring_access_checks (dk_deferred);
22840 }
22841
22842 /* Commit to the currently active tentative parse.  */
22843
22844 static void
22845 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22846 {
22847   cp_parser_context *context;
22848   cp_lexer *lexer;
22849
22850   /* Mark all of the levels as committed.  */
22851   lexer = parser->lexer;
22852   for (context = parser->context; context->next; context = context->next)
22853     {
22854       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22855         break;
22856       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22857       while (!cp_lexer_saving_tokens (lexer))
22858         lexer = lexer->next;
22859       cp_lexer_commit_tokens (lexer);
22860     }
22861 }
22862
22863 /* Abort the currently active tentative parse.  All consumed tokens
22864    will be rolled back, and no diagnostics will be issued.  */
22865
22866 static void
22867 cp_parser_abort_tentative_parse (cp_parser* parser)
22868 {
22869   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22870               || errorcount > 0);
22871   cp_parser_simulate_error (parser);
22872   /* Now, pretend that we want to see if the construct was
22873      successfully parsed.  */
22874   cp_parser_parse_definitely (parser);
22875 }
22876
22877 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22878    token stream.  Otherwise, commit to the tokens we have consumed.
22879    Returns true if no error occurred; false otherwise.  */
22880
22881 static bool
22882 cp_parser_parse_definitely (cp_parser* parser)
22883 {
22884   bool error_occurred;
22885   cp_parser_context *context;
22886
22887   /* Remember whether or not an error occurred, since we are about to
22888      destroy that information.  */
22889   error_occurred = cp_parser_error_occurred (parser);
22890   /* Remove the topmost context from the stack.  */
22891   context = parser->context;
22892   parser->context = context->next;
22893   /* If no parse errors occurred, commit to the tentative parse.  */
22894   if (!error_occurred)
22895     {
22896       /* Commit to the tokens read tentatively, unless that was
22897          already done.  */
22898       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22899         cp_lexer_commit_tokens (parser->lexer);
22900
22901       pop_to_parent_deferring_access_checks ();
22902     }
22903   /* Otherwise, if errors occurred, roll back our state so that things
22904      are just as they were before we began the tentative parse.  */
22905   else
22906     {
22907       cp_lexer_rollback_tokens (parser->lexer);
22908       pop_deferring_access_checks ();
22909     }
22910   /* Add the context to the front of the free list.  */
22911   context->next = cp_parser_context_free_list;
22912   cp_parser_context_free_list = context;
22913
22914   return !error_occurred;
22915 }
22916
22917 /* Returns true if we are parsing tentatively and are not committed to
22918    this tentative parse.  */
22919
22920 static bool
22921 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22922 {
22923   return (cp_parser_parsing_tentatively (parser)
22924           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22925 }
22926
22927 /* Returns nonzero iff an error has occurred during the most recent
22928    tentative parse.  */
22929
22930 static bool
22931 cp_parser_error_occurred (cp_parser* parser)
22932 {
22933   return (cp_parser_parsing_tentatively (parser)
22934           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22935 }
22936
22937 /* Returns nonzero if GNU extensions are allowed.  */
22938
22939 static bool
22940 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22941 {
22942   return parser->allow_gnu_extensions_p;
22943 }
22944 \f
22945 /* Objective-C++ Productions */
22946
22947
22948 /* Parse an Objective-C expression, which feeds into a primary-expression
22949    above.
22950
22951    objc-expression:
22952      objc-message-expression
22953      objc-string-literal
22954      objc-encode-expression
22955      objc-protocol-expression
22956      objc-selector-expression
22957
22958   Returns a tree representation of the expression.  */
22959
22960 static tree
22961 cp_parser_objc_expression (cp_parser* parser)
22962 {
22963   /* Try to figure out what kind of declaration is present.  */
22964   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22965
22966   switch (kwd->type)
22967     {
22968     case CPP_OPEN_SQUARE:
22969       return cp_parser_objc_message_expression (parser);
22970
22971     case CPP_OBJC_STRING:
22972       kwd = cp_lexer_consume_token (parser->lexer);
22973       return objc_build_string_object (kwd->u.value);
22974
22975     case CPP_KEYWORD:
22976       switch (kwd->keyword)
22977         {
22978         case RID_AT_ENCODE:
22979           return cp_parser_objc_encode_expression (parser);
22980
22981         case RID_AT_PROTOCOL:
22982           return cp_parser_objc_protocol_expression (parser);
22983
22984         case RID_AT_SELECTOR:
22985           return cp_parser_objc_selector_expression (parser);
22986
22987         default:
22988           break;
22989         }
22990     default:
22991       error_at (kwd->location,
22992                 "misplaced %<@%D%> Objective-C++ construct",
22993                 kwd->u.value);
22994       cp_parser_skip_to_end_of_block_or_statement (parser);
22995     }
22996
22997   return error_mark_node;
22998 }
22999
23000 /* Parse an Objective-C message expression.
23001
23002    objc-message-expression:
23003      [ objc-message-receiver objc-message-args ]
23004
23005    Returns a representation of an Objective-C message.  */
23006
23007 static tree
23008 cp_parser_objc_message_expression (cp_parser* parser)
23009 {
23010   tree receiver, messageargs;
23011
23012   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
23013   receiver = cp_parser_objc_message_receiver (parser);
23014   messageargs = cp_parser_objc_message_args (parser);
23015   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23016
23017   return objc_build_message_expr (receiver, messageargs);
23018 }
23019
23020 /* Parse an objc-message-receiver.
23021
23022    objc-message-receiver:
23023      expression
23024      simple-type-specifier
23025
23026   Returns a representation of the type or expression.  */
23027
23028 static tree
23029 cp_parser_objc_message_receiver (cp_parser* parser)
23030 {
23031   tree rcv;
23032
23033   /* An Objective-C message receiver may be either (1) a type
23034      or (2) an expression.  */
23035   cp_parser_parse_tentatively (parser);
23036   rcv = cp_parser_expression (parser, false, NULL);
23037
23038   if (cp_parser_parse_definitely (parser))
23039     return rcv;
23040
23041   rcv = cp_parser_simple_type_specifier (parser,
23042                                          /*decl_specs=*/NULL,
23043                                          CP_PARSER_FLAGS_NONE);
23044
23045   return objc_get_class_reference (rcv);
23046 }
23047
23048 /* Parse the arguments and selectors comprising an Objective-C message.
23049
23050    objc-message-args:
23051      objc-selector
23052      objc-selector-args
23053      objc-selector-args , objc-comma-args
23054
23055    objc-selector-args:
23056      objc-selector [opt] : assignment-expression
23057      objc-selector-args objc-selector [opt] : assignment-expression
23058
23059    objc-comma-args:
23060      assignment-expression
23061      objc-comma-args , assignment-expression
23062
23063    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23064    selector arguments and TREE_VALUE containing a list of comma
23065    arguments.  */
23066
23067 static tree
23068 cp_parser_objc_message_args (cp_parser* parser)
23069 {
23070   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23071   bool maybe_unary_selector_p = true;
23072   cp_token *token = cp_lexer_peek_token (parser->lexer);
23073
23074   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23075     {
23076       tree selector = NULL_TREE, arg;
23077
23078       if (token->type != CPP_COLON)
23079         selector = cp_parser_objc_selector (parser);
23080
23081       /* Detect if we have a unary selector.  */
23082       if (maybe_unary_selector_p
23083           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23084         return build_tree_list (selector, NULL_TREE);
23085
23086       maybe_unary_selector_p = false;
23087       cp_parser_require (parser, CPP_COLON, RT_COLON);
23088       arg = cp_parser_assignment_expression (parser, false, NULL);
23089
23090       sel_args
23091         = chainon (sel_args,
23092                    build_tree_list (selector, arg));
23093
23094       token = cp_lexer_peek_token (parser->lexer);
23095     }
23096
23097   /* Handle non-selector arguments, if any. */
23098   while (token->type == CPP_COMMA)
23099     {
23100       tree arg;
23101
23102       cp_lexer_consume_token (parser->lexer);
23103       arg = cp_parser_assignment_expression (parser, false, NULL);
23104
23105       addl_args
23106         = chainon (addl_args,
23107                    build_tree_list (NULL_TREE, arg));
23108
23109       token = cp_lexer_peek_token (parser->lexer);
23110     }
23111
23112   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23113     {
23114       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23115       return build_tree_list (error_mark_node, error_mark_node);
23116     }
23117
23118   return build_tree_list (sel_args, addl_args);
23119 }
23120
23121 /* Parse an Objective-C encode expression.
23122
23123    objc-encode-expression:
23124      @encode objc-typename
23125
23126    Returns an encoded representation of the type argument.  */
23127
23128 static tree
23129 cp_parser_objc_encode_expression (cp_parser* parser)
23130 {
23131   tree type;
23132   cp_token *token;
23133
23134   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
23135   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23136   token = cp_lexer_peek_token (parser->lexer);
23137   type = complete_type (cp_parser_type_id (parser));
23138   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23139
23140   if (!type)
23141     {
23142       error_at (token->location, 
23143                 "%<@encode%> must specify a type as an argument");
23144       return error_mark_node;
23145     }
23146
23147   /* This happens if we find @encode(T) (where T is a template
23148      typename or something dependent on a template typename) when
23149      parsing a template.  In that case, we can't compile it
23150      immediately, but we rather create an AT_ENCODE_EXPR which will
23151      need to be instantiated when the template is used.
23152   */
23153   if (dependent_type_p (type))
23154     {
23155       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23156       TREE_READONLY (value) = 1;
23157       return value;
23158     }
23159
23160   return objc_build_encode_expr (type);
23161 }
23162
23163 /* Parse an Objective-C @defs expression.  */
23164
23165 static tree
23166 cp_parser_objc_defs_expression (cp_parser *parser)
23167 {
23168   tree name;
23169
23170   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
23171   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23172   name = cp_parser_identifier (parser);
23173   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23174
23175   return objc_get_class_ivars (name);
23176 }
23177
23178 /* Parse an Objective-C protocol expression.
23179
23180   objc-protocol-expression:
23181     @protocol ( identifier )
23182
23183   Returns a representation of the protocol expression.  */
23184
23185 static tree
23186 cp_parser_objc_protocol_expression (cp_parser* parser)
23187 {
23188   tree proto;
23189
23190   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23191   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23192   proto = cp_parser_identifier (parser);
23193   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23194
23195   return objc_build_protocol_expr (proto);
23196 }
23197
23198 /* Parse an Objective-C selector expression.
23199
23200    objc-selector-expression:
23201      @selector ( objc-method-signature )
23202
23203    objc-method-signature:
23204      objc-selector
23205      objc-selector-seq
23206
23207    objc-selector-seq:
23208      objc-selector :
23209      objc-selector-seq objc-selector :
23210
23211   Returns a representation of the method selector.  */
23212
23213 static tree
23214 cp_parser_objc_selector_expression (cp_parser* parser)
23215 {
23216   tree sel_seq = NULL_TREE;
23217   bool maybe_unary_selector_p = true;
23218   cp_token *token;
23219   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23220
23221   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23222   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23223   token = cp_lexer_peek_token (parser->lexer);
23224
23225   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23226          || token->type == CPP_SCOPE)
23227     {
23228       tree selector = NULL_TREE;
23229
23230       if (token->type != CPP_COLON
23231           || token->type == CPP_SCOPE)
23232         selector = cp_parser_objc_selector (parser);
23233
23234       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23235           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23236         {
23237           /* Detect if we have a unary selector.  */
23238           if (maybe_unary_selector_p)
23239             {
23240               sel_seq = selector;
23241               goto finish_selector;
23242             }
23243           else
23244             {
23245               cp_parser_error (parser, "expected %<:%>");
23246             }
23247         }
23248       maybe_unary_selector_p = false;
23249       token = cp_lexer_consume_token (parser->lexer);
23250
23251       if (token->type == CPP_SCOPE)
23252         {
23253           sel_seq
23254             = chainon (sel_seq,
23255                        build_tree_list (selector, NULL_TREE));
23256           sel_seq
23257             = chainon (sel_seq,
23258                        build_tree_list (NULL_TREE, NULL_TREE));
23259         }
23260       else
23261         sel_seq
23262           = chainon (sel_seq,
23263                      build_tree_list (selector, NULL_TREE));
23264
23265       token = cp_lexer_peek_token (parser->lexer);
23266     }
23267
23268  finish_selector:
23269   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23270
23271   return objc_build_selector_expr (loc, sel_seq);
23272 }
23273
23274 /* Parse a list of identifiers.
23275
23276    objc-identifier-list:
23277      identifier
23278      objc-identifier-list , identifier
23279
23280    Returns a TREE_LIST of identifier nodes.  */
23281
23282 static tree
23283 cp_parser_objc_identifier_list (cp_parser* parser)
23284 {
23285   tree identifier;
23286   tree list;
23287   cp_token *sep;
23288
23289   identifier = cp_parser_identifier (parser);
23290   if (identifier == error_mark_node)
23291     return error_mark_node;      
23292
23293   list = build_tree_list (NULL_TREE, identifier);
23294   sep = cp_lexer_peek_token (parser->lexer);
23295
23296   while (sep->type == CPP_COMMA)
23297     {
23298       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23299       identifier = cp_parser_identifier (parser);
23300       if (identifier == error_mark_node)
23301         return list;
23302
23303       list = chainon (list, build_tree_list (NULL_TREE,
23304                                              identifier));
23305       sep = cp_lexer_peek_token (parser->lexer);
23306     }
23307   
23308   return list;
23309 }
23310
23311 /* Parse an Objective-C alias declaration.
23312
23313    objc-alias-declaration:
23314      @compatibility_alias identifier identifier ;
23315
23316    This function registers the alias mapping with the Objective-C front end.
23317    It returns nothing.  */
23318
23319 static void
23320 cp_parser_objc_alias_declaration (cp_parser* parser)
23321 {
23322   tree alias, orig;
23323
23324   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23325   alias = cp_parser_identifier (parser);
23326   orig = cp_parser_identifier (parser);
23327   objc_declare_alias (alias, orig);
23328   cp_parser_consume_semicolon_at_end_of_statement (parser);
23329 }
23330
23331 /* Parse an Objective-C class forward-declaration.
23332
23333    objc-class-declaration:
23334      @class objc-identifier-list ;
23335
23336    The function registers the forward declarations with the Objective-C
23337    front end.  It returns nothing.  */
23338
23339 static void
23340 cp_parser_objc_class_declaration (cp_parser* parser)
23341 {
23342   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23343   while (true)
23344     {
23345       tree id;
23346       
23347       id = cp_parser_identifier (parser);
23348       if (id == error_mark_node)
23349         break;
23350       
23351       objc_declare_class (id);
23352
23353       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23354         cp_lexer_consume_token (parser->lexer);
23355       else
23356         break;
23357     }
23358   cp_parser_consume_semicolon_at_end_of_statement (parser);
23359 }
23360
23361 /* Parse a list of Objective-C protocol references.
23362
23363    objc-protocol-refs-opt:
23364      objc-protocol-refs [opt]
23365
23366    objc-protocol-refs:
23367      < objc-identifier-list >
23368
23369    Returns a TREE_LIST of identifiers, if any.  */
23370
23371 static tree
23372 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23373 {
23374   tree protorefs = NULL_TREE;
23375
23376   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23377     {
23378       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23379       protorefs = cp_parser_objc_identifier_list (parser);
23380       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23381     }
23382
23383   return protorefs;
23384 }
23385
23386 /* Parse a Objective-C visibility specification.  */
23387
23388 static void
23389 cp_parser_objc_visibility_spec (cp_parser* parser)
23390 {
23391   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23392
23393   switch (vis->keyword)
23394     {
23395     case RID_AT_PRIVATE:
23396       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23397       break;
23398     case RID_AT_PROTECTED:
23399       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23400       break;
23401     case RID_AT_PUBLIC:
23402       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23403       break;
23404     case RID_AT_PACKAGE:
23405       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23406       break;
23407     default:
23408       return;
23409     }
23410
23411   /* Eat '@private'/'@protected'/'@public'.  */
23412   cp_lexer_consume_token (parser->lexer);
23413 }
23414
23415 /* Parse an Objective-C method type.  Return 'true' if it is a class
23416    (+) method, and 'false' if it is an instance (-) method.  */
23417
23418 static inline bool
23419 cp_parser_objc_method_type (cp_parser* parser)
23420 {
23421   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23422     return true;
23423   else
23424     return false;
23425 }
23426
23427 /* Parse an Objective-C protocol qualifier.  */
23428
23429 static tree
23430 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23431 {
23432   tree quals = NULL_TREE, node;
23433   cp_token *token = cp_lexer_peek_token (parser->lexer);
23434
23435   node = token->u.value;
23436
23437   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23438          && (node == ridpointers [(int) RID_IN]
23439              || node == ridpointers [(int) RID_OUT]
23440              || node == ridpointers [(int) RID_INOUT]
23441              || node == ridpointers [(int) RID_BYCOPY]
23442              || node == ridpointers [(int) RID_BYREF]
23443              || node == ridpointers [(int) RID_ONEWAY]))
23444     {
23445       quals = tree_cons (NULL_TREE, node, quals);
23446       cp_lexer_consume_token (parser->lexer);
23447       token = cp_lexer_peek_token (parser->lexer);
23448       node = token->u.value;
23449     }
23450
23451   return quals;
23452 }
23453
23454 /* Parse an Objective-C typename.  */
23455
23456 static tree
23457 cp_parser_objc_typename (cp_parser* parser)
23458 {
23459   tree type_name = NULL_TREE;
23460
23461   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23462     {
23463       tree proto_quals, cp_type = NULL_TREE;
23464
23465       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23466       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23467
23468       /* An ObjC type name may consist of just protocol qualifiers, in which
23469          case the type shall default to 'id'.  */
23470       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23471         {
23472           cp_type = cp_parser_type_id (parser);
23473           
23474           /* If the type could not be parsed, an error has already
23475              been produced.  For error recovery, behave as if it had
23476              not been specified, which will use the default type
23477              'id'.  */
23478           if (cp_type == error_mark_node)
23479             {
23480               cp_type = NULL_TREE;
23481               /* We need to skip to the closing parenthesis as
23482                  cp_parser_type_id() does not seem to do it for
23483                  us.  */
23484               cp_parser_skip_to_closing_parenthesis (parser,
23485                                                      /*recovering=*/true,
23486                                                      /*or_comma=*/false,
23487                                                      /*consume_paren=*/false);
23488             }
23489         }
23490
23491       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23492       type_name = build_tree_list (proto_quals, cp_type);
23493     }
23494
23495   return type_name;
23496 }
23497
23498 /* Check to see if TYPE refers to an Objective-C selector name.  */
23499
23500 static bool
23501 cp_parser_objc_selector_p (enum cpp_ttype type)
23502 {
23503   return (type == CPP_NAME || type == CPP_KEYWORD
23504           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23505           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23506           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23507           || type == CPP_XOR || type == CPP_XOR_EQ);
23508 }
23509
23510 /* Parse an Objective-C selector.  */
23511
23512 static tree
23513 cp_parser_objc_selector (cp_parser* parser)
23514 {
23515   cp_token *token = cp_lexer_consume_token (parser->lexer);
23516
23517   if (!cp_parser_objc_selector_p (token->type))
23518     {
23519       error_at (token->location, "invalid Objective-C++ selector name");
23520       return error_mark_node;
23521     }
23522
23523   /* C++ operator names are allowed to appear in ObjC selectors.  */
23524   switch (token->type)
23525     {
23526     case CPP_AND_AND: return get_identifier ("and");
23527     case CPP_AND_EQ: return get_identifier ("and_eq");
23528     case CPP_AND: return get_identifier ("bitand");
23529     case CPP_OR: return get_identifier ("bitor");
23530     case CPP_COMPL: return get_identifier ("compl");
23531     case CPP_NOT: return get_identifier ("not");
23532     case CPP_NOT_EQ: return get_identifier ("not_eq");
23533     case CPP_OR_OR: return get_identifier ("or");
23534     case CPP_OR_EQ: return get_identifier ("or_eq");
23535     case CPP_XOR: return get_identifier ("xor");
23536     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23537     default: return token->u.value;
23538     }
23539 }
23540
23541 /* Parse an Objective-C params list.  */
23542
23543 static tree
23544 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23545 {
23546   tree params = NULL_TREE;
23547   bool maybe_unary_selector_p = true;
23548   cp_token *token = cp_lexer_peek_token (parser->lexer);
23549
23550   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23551     {
23552       tree selector = NULL_TREE, type_name, identifier;
23553       tree parm_attr = NULL_TREE;
23554
23555       if (token->keyword == RID_ATTRIBUTE)
23556         break;
23557
23558       if (token->type != CPP_COLON)
23559         selector = cp_parser_objc_selector (parser);
23560
23561       /* Detect if we have a unary selector.  */
23562       if (maybe_unary_selector_p
23563           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23564         {
23565           params = selector; /* Might be followed by attributes.  */
23566           break;
23567         }
23568
23569       maybe_unary_selector_p = false;
23570       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23571         {
23572           /* Something went quite wrong.  There should be a colon
23573              here, but there is not.  Stop parsing parameters.  */
23574           break;
23575         }
23576       type_name = cp_parser_objc_typename (parser);
23577       /* New ObjC allows attributes on parameters too.  */
23578       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23579         parm_attr = cp_parser_attributes_opt (parser);
23580       identifier = cp_parser_identifier (parser);
23581
23582       params
23583         = chainon (params,
23584                    objc_build_keyword_decl (selector,
23585                                             type_name,
23586                                             identifier,
23587                                             parm_attr));
23588
23589       token = cp_lexer_peek_token (parser->lexer);
23590     }
23591
23592   if (params == NULL_TREE)
23593     {
23594       cp_parser_error (parser, "objective-c++ method declaration is expected");
23595       return error_mark_node;
23596     }
23597
23598   /* We allow tail attributes for the method.  */
23599   if (token->keyword == RID_ATTRIBUTE)
23600     {
23601       *attributes = cp_parser_attributes_opt (parser);
23602       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23603           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23604         return params;
23605       cp_parser_error (parser, 
23606                        "method attributes must be specified at the end");
23607       return error_mark_node;
23608     }
23609
23610   if (params == NULL_TREE)
23611     {
23612       cp_parser_error (parser, "objective-c++ method declaration is expected");
23613       return error_mark_node;
23614     }
23615   return params;
23616 }
23617
23618 /* Parse the non-keyword Objective-C params.  */
23619
23620 static tree
23621 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23622                                        tree* attributes)
23623 {
23624   tree params = make_node (TREE_LIST);
23625   cp_token *token = cp_lexer_peek_token (parser->lexer);
23626   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23627
23628   while (token->type == CPP_COMMA)
23629     {
23630       cp_parameter_declarator *parmdecl;
23631       tree parm;
23632
23633       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23634       token = cp_lexer_peek_token (parser->lexer);
23635
23636       if (token->type == CPP_ELLIPSIS)
23637         {
23638           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23639           *ellipsisp = true;
23640           token = cp_lexer_peek_token (parser->lexer);
23641           break;
23642         }
23643
23644       /* TODO: parse attributes for tail parameters.  */
23645       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23646       parm = grokdeclarator (parmdecl->declarator,
23647                              &parmdecl->decl_specifiers,
23648                              PARM, /*initialized=*/0,
23649                              /*attrlist=*/NULL);
23650
23651       chainon (params, build_tree_list (NULL_TREE, parm));
23652       token = cp_lexer_peek_token (parser->lexer);
23653     }
23654
23655   /* We allow tail attributes for the method.  */
23656   if (token->keyword == RID_ATTRIBUTE)
23657     {
23658       if (*attributes == NULL_TREE)
23659         {
23660           *attributes = cp_parser_attributes_opt (parser);
23661           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23662               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23663             return params;
23664         }
23665       else        
23666         /* We have an error, but parse the attributes, so that we can 
23667            carry on.  */
23668         *attributes = cp_parser_attributes_opt (parser);
23669
23670       cp_parser_error (parser, 
23671                        "method attributes must be specified at the end");
23672       return error_mark_node;
23673     }
23674
23675   return params;
23676 }
23677
23678 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23679
23680 static void
23681 cp_parser_objc_interstitial_code (cp_parser* parser)
23682 {
23683   cp_token *token = cp_lexer_peek_token (parser->lexer);
23684
23685   /* If the next token is `extern' and the following token is a string
23686      literal, then we have a linkage specification.  */
23687   if (token->keyword == RID_EXTERN
23688       && cp_parser_is_pure_string_literal
23689          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23690     cp_parser_linkage_specification (parser);
23691   /* Handle #pragma, if any.  */
23692   else if (token->type == CPP_PRAGMA)
23693     cp_parser_pragma (parser, pragma_external);
23694   /* Allow stray semicolons.  */
23695   else if (token->type == CPP_SEMICOLON)
23696     cp_lexer_consume_token (parser->lexer);
23697   /* Mark methods as optional or required, when building protocols.  */
23698   else if (token->keyword == RID_AT_OPTIONAL)
23699     {
23700       cp_lexer_consume_token (parser->lexer);
23701       objc_set_method_opt (true);
23702     }
23703   else if (token->keyword == RID_AT_REQUIRED)
23704     {
23705       cp_lexer_consume_token (parser->lexer);
23706       objc_set_method_opt (false);
23707     }
23708   else if (token->keyword == RID_NAMESPACE)
23709     cp_parser_namespace_definition (parser);
23710   /* Other stray characters must generate errors.  */
23711   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23712     {
23713       cp_lexer_consume_token (parser->lexer);
23714       error ("stray %qs between Objective-C++ methods",
23715              token->type == CPP_OPEN_BRACE ? "{" : "}");
23716     }
23717   /* Finally, try to parse a block-declaration, or a function-definition.  */
23718   else
23719     cp_parser_block_declaration (parser, /*statement_p=*/false);
23720 }
23721
23722 /* Parse a method signature.  */
23723
23724 static tree
23725 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23726 {
23727   tree rettype, kwdparms, optparms;
23728   bool ellipsis = false;
23729   bool is_class_method;
23730
23731   is_class_method = cp_parser_objc_method_type (parser);
23732   rettype = cp_parser_objc_typename (parser);
23733   *attributes = NULL_TREE;
23734   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23735   if (kwdparms == error_mark_node)
23736     return error_mark_node;
23737   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23738   if (optparms == error_mark_node)
23739     return error_mark_node;
23740
23741   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23742 }
23743
23744 static bool
23745 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23746 {
23747   tree tattr;  
23748   cp_lexer_save_tokens (parser->lexer);
23749   tattr = cp_parser_attributes_opt (parser);
23750   gcc_assert (tattr) ;
23751   
23752   /* If the attributes are followed by a method introducer, this is not allowed.
23753      Dump the attributes and flag the situation.  */
23754   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23755       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23756     return true;
23757
23758   /* Otherwise, the attributes introduce some interstitial code, possibly so
23759      rewind to allow that check.  */
23760   cp_lexer_rollback_tokens (parser->lexer);
23761   return false;  
23762 }
23763
23764 /* Parse an Objective-C method prototype list.  */
23765
23766 static void
23767 cp_parser_objc_method_prototype_list (cp_parser* parser)
23768 {
23769   cp_token *token = cp_lexer_peek_token (parser->lexer);
23770
23771   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23772     {
23773       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23774         {
23775           tree attributes, sig;
23776           bool is_class_method;
23777           if (token->type == CPP_PLUS)
23778             is_class_method = true;
23779           else
23780             is_class_method = false;
23781           sig = cp_parser_objc_method_signature (parser, &attributes);
23782           if (sig == error_mark_node)
23783             {
23784               cp_parser_skip_to_end_of_block_or_statement (parser);
23785               token = cp_lexer_peek_token (parser->lexer);
23786               continue;
23787             }
23788           objc_add_method_declaration (is_class_method, sig, attributes);
23789           cp_parser_consume_semicolon_at_end_of_statement (parser);
23790         }
23791       else if (token->keyword == RID_AT_PROPERTY)
23792         cp_parser_objc_at_property_declaration (parser);
23793       else if (token->keyword == RID_ATTRIBUTE 
23794                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23795         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23796                     OPT_Wattributes, 
23797                     "prefix attributes are ignored for methods");
23798       else
23799         /* Allow for interspersed non-ObjC++ code.  */
23800         cp_parser_objc_interstitial_code (parser);
23801
23802       token = cp_lexer_peek_token (parser->lexer);
23803     }
23804
23805   if (token->type != CPP_EOF)
23806     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23807   else
23808     cp_parser_error (parser, "expected %<@end%>");
23809
23810   objc_finish_interface ();
23811 }
23812
23813 /* Parse an Objective-C method definition list.  */
23814
23815 static void
23816 cp_parser_objc_method_definition_list (cp_parser* parser)
23817 {
23818   cp_token *token = cp_lexer_peek_token (parser->lexer);
23819
23820   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23821     {
23822       tree meth;
23823
23824       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23825         {
23826           cp_token *ptk;
23827           tree sig, attribute;
23828           bool is_class_method;
23829           if (token->type == CPP_PLUS)
23830             is_class_method = true;
23831           else
23832             is_class_method = false;
23833           push_deferring_access_checks (dk_deferred);
23834           sig = cp_parser_objc_method_signature (parser, &attribute);
23835           if (sig == error_mark_node)
23836             {
23837               cp_parser_skip_to_end_of_block_or_statement (parser);
23838               token = cp_lexer_peek_token (parser->lexer);
23839               continue;
23840             }
23841           objc_start_method_definition (is_class_method, sig, attribute,
23842                                         NULL_TREE);
23843
23844           /* For historical reasons, we accept an optional semicolon.  */
23845           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23846             cp_lexer_consume_token (parser->lexer);
23847
23848           ptk = cp_lexer_peek_token (parser->lexer);
23849           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23850                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23851             {
23852               perform_deferred_access_checks ();
23853               stop_deferring_access_checks ();
23854               meth = cp_parser_function_definition_after_declarator (parser,
23855                                                                      false);
23856               pop_deferring_access_checks ();
23857               objc_finish_method_definition (meth);
23858             }
23859         }
23860       /* The following case will be removed once @synthesize is
23861          completely implemented.  */
23862       else if (token->keyword == RID_AT_PROPERTY)
23863         cp_parser_objc_at_property_declaration (parser);
23864       else if (token->keyword == RID_AT_SYNTHESIZE)
23865         cp_parser_objc_at_synthesize_declaration (parser);
23866       else if (token->keyword == RID_AT_DYNAMIC)
23867         cp_parser_objc_at_dynamic_declaration (parser);
23868       else if (token->keyword == RID_ATTRIBUTE 
23869                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23870         warning_at (token->location, OPT_Wattributes,
23871                     "prefix attributes are ignored for methods");
23872       else
23873         /* Allow for interspersed non-ObjC++ code.  */
23874         cp_parser_objc_interstitial_code (parser);
23875
23876       token = cp_lexer_peek_token (parser->lexer);
23877     }
23878
23879   if (token->type != CPP_EOF)
23880     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23881   else
23882     cp_parser_error (parser, "expected %<@end%>");
23883
23884   objc_finish_implementation ();
23885 }
23886
23887 /* Parse Objective-C ivars.  */
23888
23889 static void
23890 cp_parser_objc_class_ivars (cp_parser* parser)
23891 {
23892   cp_token *token = cp_lexer_peek_token (parser->lexer);
23893
23894   if (token->type != CPP_OPEN_BRACE)
23895     return;     /* No ivars specified.  */
23896
23897   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23898   token = cp_lexer_peek_token (parser->lexer);
23899
23900   while (token->type != CPP_CLOSE_BRACE 
23901         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23902     {
23903       cp_decl_specifier_seq declspecs;
23904       int decl_class_or_enum_p;
23905       tree prefix_attributes;
23906
23907       cp_parser_objc_visibility_spec (parser);
23908
23909       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23910         break;
23911
23912       cp_parser_decl_specifier_seq (parser,
23913                                     CP_PARSER_FLAGS_OPTIONAL,
23914                                     &declspecs,
23915                                     &decl_class_or_enum_p);
23916
23917       /* auto, register, static, extern, mutable.  */
23918       if (declspecs.storage_class != sc_none)
23919         {
23920           cp_parser_error (parser, "invalid type for instance variable");         
23921           declspecs.storage_class = sc_none;
23922         }
23923
23924       /* __thread.  */
23925       if (declspecs.specs[(int) ds_thread])
23926         {
23927           cp_parser_error (parser, "invalid type for instance variable");
23928           declspecs.specs[(int) ds_thread] = 0;
23929         }
23930       
23931       /* typedef.  */
23932       if (declspecs.specs[(int) ds_typedef])
23933         {
23934           cp_parser_error (parser, "invalid type for instance variable");
23935           declspecs.specs[(int) ds_typedef] = 0;
23936         }
23937
23938       prefix_attributes = declspecs.attributes;
23939       declspecs.attributes = NULL_TREE;
23940
23941       /* Keep going until we hit the `;' at the end of the
23942          declaration.  */
23943       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23944         {
23945           tree width = NULL_TREE, attributes, first_attribute, decl;
23946           cp_declarator *declarator = NULL;
23947           int ctor_dtor_or_conv_p;
23948
23949           /* Check for a (possibly unnamed) bitfield declaration.  */
23950           token = cp_lexer_peek_token (parser->lexer);
23951           if (token->type == CPP_COLON)
23952             goto eat_colon;
23953
23954           if (token->type == CPP_NAME
23955               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23956                   == CPP_COLON))
23957             {
23958               /* Get the name of the bitfield.  */
23959               declarator = make_id_declarator (NULL_TREE,
23960                                                cp_parser_identifier (parser),
23961                                                sfk_none);
23962
23963              eat_colon:
23964               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23965               /* Get the width of the bitfield.  */
23966               width
23967                 = cp_parser_constant_expression (parser,
23968                                                  /*allow_non_constant=*/false,
23969                                                  NULL);
23970             }
23971           else
23972             {
23973               /* Parse the declarator.  */
23974               declarator
23975                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23976                                         &ctor_dtor_or_conv_p,
23977                                         /*parenthesized_p=*/NULL,
23978                                         /*member_p=*/false);
23979             }
23980
23981           /* Look for attributes that apply to the ivar.  */
23982           attributes = cp_parser_attributes_opt (parser);
23983           /* Remember which attributes are prefix attributes and
23984              which are not.  */
23985           first_attribute = attributes;
23986           /* Combine the attributes.  */
23987           attributes = chainon (prefix_attributes, attributes);
23988
23989           if (width)
23990               /* Create the bitfield declaration.  */
23991               decl = grokbitfield (declarator, &declspecs,
23992                                    width,
23993                                    attributes);
23994           else
23995             decl = grokfield (declarator, &declspecs,
23996                               NULL_TREE, /*init_const_expr_p=*/false,
23997                               NULL_TREE, attributes);
23998
23999           /* Add the instance variable.  */
24000           if (decl != error_mark_node && decl != NULL_TREE)
24001             objc_add_instance_variable (decl);
24002
24003           /* Reset PREFIX_ATTRIBUTES.  */
24004           while (attributes && TREE_CHAIN (attributes) != first_attribute)
24005             attributes = TREE_CHAIN (attributes);
24006           if (attributes)
24007             TREE_CHAIN (attributes) = NULL_TREE;
24008
24009           token = cp_lexer_peek_token (parser->lexer);
24010
24011           if (token->type == CPP_COMMA)
24012             {
24013               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24014               continue;
24015             }
24016           break;
24017         }
24018
24019       cp_parser_consume_semicolon_at_end_of_statement (parser);
24020       token = cp_lexer_peek_token (parser->lexer);
24021     }
24022
24023   if (token->keyword == RID_AT_END)
24024     cp_parser_error (parser, "expected %<}%>");
24025
24026   /* Do not consume the RID_AT_END, so it will be read again as terminating
24027      the @interface of @implementation.  */ 
24028   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24029     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
24030     
24031   /* For historical reasons, we accept an optional semicolon.  */
24032   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24033     cp_lexer_consume_token (parser->lexer);
24034 }
24035
24036 /* Parse an Objective-C protocol declaration.  */
24037
24038 static void
24039 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24040 {
24041   tree proto, protorefs;
24042   cp_token *tok;
24043
24044   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24045   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24046     {
24047       tok = cp_lexer_peek_token (parser->lexer);
24048       error_at (tok->location, "identifier expected after %<@protocol%>");
24049       cp_parser_consume_semicolon_at_end_of_statement (parser);
24050       return;
24051     }
24052
24053   /* See if we have a forward declaration or a definition.  */
24054   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24055
24056   /* Try a forward declaration first.  */
24057   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24058     {
24059       while (true)
24060         {
24061           tree id;
24062           
24063           id = cp_parser_identifier (parser);
24064           if (id == error_mark_node)
24065             break;
24066           
24067           objc_declare_protocol (id, attributes);
24068           
24069           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24070             cp_lexer_consume_token (parser->lexer);
24071           else
24072             break;
24073         }
24074       cp_parser_consume_semicolon_at_end_of_statement (parser);
24075     }
24076
24077   /* Ok, we got a full-fledged definition (or at least should).  */
24078   else
24079     {
24080       proto = cp_parser_identifier (parser);
24081       protorefs = cp_parser_objc_protocol_refs_opt (parser);
24082       objc_start_protocol (proto, protorefs, attributes);
24083       cp_parser_objc_method_prototype_list (parser);
24084     }
24085 }
24086
24087 /* Parse an Objective-C superclass or category.  */
24088
24089 static void
24090 cp_parser_objc_superclass_or_category (cp_parser *parser, 
24091                                        bool iface_p,
24092                                        tree *super,
24093                                        tree *categ, bool *is_class_extension)
24094 {
24095   cp_token *next = cp_lexer_peek_token (parser->lexer);
24096
24097   *super = *categ = NULL_TREE;
24098   *is_class_extension = false;
24099   if (next->type == CPP_COLON)
24100     {
24101       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24102       *super = cp_parser_identifier (parser);
24103     }
24104   else if (next->type == CPP_OPEN_PAREN)
24105     {
24106       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24107
24108       /* If there is no category name, and this is an @interface, we
24109          have a class extension.  */
24110       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24111         {
24112           *categ = NULL_TREE;
24113           *is_class_extension = true;
24114         }
24115       else
24116         *categ = cp_parser_identifier (parser);
24117
24118       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24119     }
24120 }
24121
24122 /* Parse an Objective-C class interface.  */
24123
24124 static void
24125 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24126 {
24127   tree name, super, categ, protos;
24128   bool is_class_extension;
24129
24130   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
24131   name = cp_parser_identifier (parser);
24132   if (name == error_mark_node)
24133     {
24134       /* It's hard to recover because even if valid @interface stuff
24135          is to follow, we can't compile it (or validate it) if we
24136          don't even know which class it refers to.  Let's assume this
24137          was a stray '@interface' token in the stream and skip it.
24138       */
24139       return;
24140     }
24141   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24142                                          &is_class_extension);
24143   protos = cp_parser_objc_protocol_refs_opt (parser);
24144
24145   /* We have either a class or a category on our hands.  */
24146   if (categ || is_class_extension)
24147     objc_start_category_interface (name, categ, protos, attributes);
24148   else
24149     {
24150       objc_start_class_interface (name, super, protos, attributes);
24151       /* Handle instance variable declarations, if any.  */
24152       cp_parser_objc_class_ivars (parser);
24153       objc_continue_interface ();
24154     }
24155
24156   cp_parser_objc_method_prototype_list (parser);
24157 }
24158
24159 /* Parse an Objective-C class implementation.  */
24160
24161 static void
24162 cp_parser_objc_class_implementation (cp_parser* parser)
24163 {
24164   tree name, super, categ;
24165   bool is_class_extension;
24166
24167   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
24168   name = cp_parser_identifier (parser);
24169   if (name == error_mark_node)
24170     {
24171       /* It's hard to recover because even if valid @implementation
24172          stuff is to follow, we can't compile it (or validate it) if
24173          we don't even know which class it refers to.  Let's assume
24174          this was a stray '@implementation' token in the stream and
24175          skip it.
24176       */
24177       return;
24178     }
24179   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24180                                          &is_class_extension);
24181
24182   /* We have either a class or a category on our hands.  */
24183   if (categ)
24184     objc_start_category_implementation (name, categ);
24185   else
24186     {
24187       objc_start_class_implementation (name, super);
24188       /* Handle instance variable declarations, if any.  */
24189       cp_parser_objc_class_ivars (parser);
24190       objc_continue_implementation ();
24191     }
24192
24193   cp_parser_objc_method_definition_list (parser);
24194 }
24195
24196 /* Consume the @end token and finish off the implementation.  */
24197
24198 static void
24199 cp_parser_objc_end_implementation (cp_parser* parser)
24200 {
24201   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24202   objc_finish_implementation ();
24203 }
24204
24205 /* Parse an Objective-C declaration.  */
24206
24207 static void
24208 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24209 {
24210   /* Try to figure out what kind of declaration is present.  */
24211   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24212
24213   if (attributes)
24214     switch (kwd->keyword)
24215       {
24216         case RID_AT_ALIAS:
24217         case RID_AT_CLASS:
24218         case RID_AT_END:
24219           error_at (kwd->location, "attributes may not be specified before"
24220                     " the %<@%D%> Objective-C++ keyword",
24221                     kwd->u.value);
24222           attributes = NULL;
24223           break;
24224         case RID_AT_IMPLEMENTATION:
24225           warning_at (kwd->location, OPT_Wattributes,
24226                       "prefix attributes are ignored before %<@%D%>",
24227                       kwd->u.value);
24228           attributes = NULL;
24229         default:
24230           break;
24231       }
24232
24233   switch (kwd->keyword)
24234     {
24235     case RID_AT_ALIAS:
24236       cp_parser_objc_alias_declaration (parser);
24237       break;
24238     case RID_AT_CLASS:
24239       cp_parser_objc_class_declaration (parser);
24240       break;
24241     case RID_AT_PROTOCOL:
24242       cp_parser_objc_protocol_declaration (parser, attributes);
24243       break;
24244     case RID_AT_INTERFACE:
24245       cp_parser_objc_class_interface (parser, attributes);
24246       break;
24247     case RID_AT_IMPLEMENTATION:
24248       cp_parser_objc_class_implementation (parser);
24249       break;
24250     case RID_AT_END:
24251       cp_parser_objc_end_implementation (parser);
24252       break;
24253     default:
24254       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24255                 kwd->u.value);
24256       cp_parser_skip_to_end_of_block_or_statement (parser);
24257     }
24258 }
24259
24260 /* Parse an Objective-C try-catch-finally statement.
24261
24262    objc-try-catch-finally-stmt:
24263      @try compound-statement objc-catch-clause-seq [opt]
24264        objc-finally-clause [opt]
24265
24266    objc-catch-clause-seq:
24267      objc-catch-clause objc-catch-clause-seq [opt]
24268
24269    objc-catch-clause:
24270      @catch ( objc-exception-declaration ) compound-statement
24271
24272    objc-finally-clause:
24273      @finally compound-statement
24274
24275    objc-exception-declaration:
24276      parameter-declaration
24277      '...'
24278
24279    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24280
24281    Returns NULL_TREE.
24282
24283    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24284    for C.  Keep them in sync.  */   
24285
24286 static tree
24287 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24288 {
24289   location_t location;
24290   tree stmt;
24291
24292   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24293   location = cp_lexer_peek_token (parser->lexer)->location;
24294   objc_maybe_warn_exceptions (location);
24295   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24296      node, lest it get absorbed into the surrounding block.  */
24297   stmt = push_stmt_list ();
24298   cp_parser_compound_statement (parser, NULL, false, false);
24299   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24300
24301   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24302     {
24303       cp_parameter_declarator *parm;
24304       tree parameter_declaration = error_mark_node;
24305       bool seen_open_paren = false;
24306
24307       cp_lexer_consume_token (parser->lexer);
24308       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24309         seen_open_paren = true;
24310       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24311         {
24312           /* We have "@catch (...)" (where the '...' are literally
24313              what is in the code).  Skip the '...'.
24314              parameter_declaration is set to NULL_TREE, and
24315              objc_being_catch_clauses() knows that that means
24316              '...'.  */
24317           cp_lexer_consume_token (parser->lexer);
24318           parameter_declaration = NULL_TREE;
24319         }
24320       else
24321         {
24322           /* We have "@catch (NSException *exception)" or something
24323              like that.  Parse the parameter declaration.  */
24324           parm = cp_parser_parameter_declaration (parser, false, NULL);
24325           if (parm == NULL)
24326             parameter_declaration = error_mark_node;
24327           else
24328             parameter_declaration = grokdeclarator (parm->declarator,
24329                                                     &parm->decl_specifiers,
24330                                                     PARM, /*initialized=*/0,
24331                                                     /*attrlist=*/NULL);
24332         }
24333       if (seen_open_paren)
24334         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24335       else
24336         {
24337           /* If there was no open parenthesis, we are recovering from
24338              an error, and we are trying to figure out what mistake
24339              the user has made.  */
24340
24341           /* If there is an immediate closing parenthesis, the user
24342              probably forgot the opening one (ie, they typed "@catch
24343              NSException *e)".  Parse the closing parenthesis and keep
24344              going.  */
24345           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24346             cp_lexer_consume_token (parser->lexer);
24347           
24348           /* If these is no immediate closing parenthesis, the user
24349              probably doesn't know that parenthesis are required at
24350              all (ie, they typed "@catch NSException *e").  So, just
24351              forget about the closing parenthesis and keep going.  */
24352         }
24353       objc_begin_catch_clause (parameter_declaration);
24354       cp_parser_compound_statement (parser, NULL, false, false);
24355       objc_finish_catch_clause ();
24356     }
24357   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24358     {
24359       cp_lexer_consume_token (parser->lexer);
24360       location = cp_lexer_peek_token (parser->lexer)->location;
24361       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24362          node, lest it get absorbed into the surrounding block.  */
24363       stmt = push_stmt_list ();
24364       cp_parser_compound_statement (parser, NULL, false, false);
24365       objc_build_finally_clause (location, pop_stmt_list (stmt));
24366     }
24367
24368   return objc_finish_try_stmt ();
24369 }
24370
24371 /* Parse an Objective-C synchronized statement.
24372
24373    objc-synchronized-stmt:
24374      @synchronized ( expression ) compound-statement
24375
24376    Returns NULL_TREE.  */
24377
24378 static tree
24379 cp_parser_objc_synchronized_statement (cp_parser *parser)
24380 {
24381   location_t location;
24382   tree lock, stmt;
24383
24384   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24385
24386   location = cp_lexer_peek_token (parser->lexer)->location;
24387   objc_maybe_warn_exceptions (location);
24388   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24389   lock = cp_parser_expression (parser, false, NULL);
24390   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24391
24392   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24393      node, lest it get absorbed into the surrounding block.  */
24394   stmt = push_stmt_list ();
24395   cp_parser_compound_statement (parser, NULL, false, false);
24396
24397   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24398 }
24399
24400 /* Parse an Objective-C throw statement.
24401
24402    objc-throw-stmt:
24403      @throw assignment-expression [opt] ;
24404
24405    Returns a constructed '@throw' statement.  */
24406
24407 static tree
24408 cp_parser_objc_throw_statement (cp_parser *parser)
24409 {
24410   tree expr = NULL_TREE;
24411   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24412
24413   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24414
24415   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24416     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24417
24418   cp_parser_consume_semicolon_at_end_of_statement (parser);
24419
24420   return objc_build_throw_stmt (loc, expr);
24421 }
24422
24423 /* Parse an Objective-C statement.  */
24424
24425 static tree
24426 cp_parser_objc_statement (cp_parser * parser)
24427 {
24428   /* Try to figure out what kind of declaration is present.  */
24429   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24430
24431   switch (kwd->keyword)
24432     {
24433     case RID_AT_TRY:
24434       return cp_parser_objc_try_catch_finally_statement (parser);
24435     case RID_AT_SYNCHRONIZED:
24436       return cp_parser_objc_synchronized_statement (parser);
24437     case RID_AT_THROW:
24438       return cp_parser_objc_throw_statement (parser);
24439     default:
24440       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24441                kwd->u.value);
24442       cp_parser_skip_to_end_of_block_or_statement (parser);
24443     }
24444
24445   return error_mark_node;
24446 }
24447
24448 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24449    look ahead to see if an objc keyword follows the attributes.  This
24450    is to detect the use of prefix attributes on ObjC @interface and 
24451    @protocol.  */
24452
24453 static bool
24454 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24455 {
24456   cp_lexer_save_tokens (parser->lexer);
24457   *attrib = cp_parser_attributes_opt (parser);
24458   gcc_assert (*attrib);
24459   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24460     {
24461       cp_lexer_commit_tokens (parser->lexer);
24462       return true;
24463     }
24464   cp_lexer_rollback_tokens (parser->lexer);
24465   return false;  
24466 }
24467
24468 /* This routine is a minimal replacement for
24469    c_parser_struct_declaration () used when parsing the list of
24470    types/names or ObjC++ properties.  For example, when parsing the
24471    code
24472
24473    @property (readonly) int a, b, c;
24474
24475    this function is responsible for parsing "int a, int b, int c" and
24476    returning the declarations as CHAIN of DECLs.
24477
24478    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24479    similar parsing.  */
24480 static tree
24481 cp_parser_objc_struct_declaration (cp_parser *parser)
24482 {
24483   tree decls = NULL_TREE;
24484   cp_decl_specifier_seq declspecs;
24485   int decl_class_or_enum_p;
24486   tree prefix_attributes;
24487
24488   cp_parser_decl_specifier_seq (parser,
24489                                 CP_PARSER_FLAGS_NONE,
24490                                 &declspecs,
24491                                 &decl_class_or_enum_p);
24492
24493   if (declspecs.type == error_mark_node)
24494     return error_mark_node;
24495
24496   /* auto, register, static, extern, mutable.  */
24497   if (declspecs.storage_class != sc_none)
24498     {
24499       cp_parser_error (parser, "invalid type for property");
24500       declspecs.storage_class = sc_none;
24501     }
24502   
24503   /* __thread.  */
24504   if (declspecs.specs[(int) ds_thread])
24505     {
24506       cp_parser_error (parser, "invalid type for property");
24507       declspecs.specs[(int) ds_thread] = 0;
24508     }
24509   
24510   /* typedef.  */
24511   if (declspecs.specs[(int) ds_typedef])
24512     {
24513       cp_parser_error (parser, "invalid type for property");
24514       declspecs.specs[(int) ds_typedef] = 0;
24515     }
24516
24517   prefix_attributes = declspecs.attributes;
24518   declspecs.attributes = NULL_TREE;
24519
24520   /* Keep going until we hit the `;' at the end of the declaration. */
24521   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24522     {
24523       tree attributes, first_attribute, decl;
24524       cp_declarator *declarator;
24525       cp_token *token;
24526
24527       /* Parse the declarator.  */
24528       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24529                                          NULL, NULL, false);
24530
24531       /* Look for attributes that apply to the ivar.  */
24532       attributes = cp_parser_attributes_opt (parser);
24533       /* Remember which attributes are prefix attributes and
24534          which are not.  */
24535       first_attribute = attributes;
24536       /* Combine the attributes.  */
24537       attributes = chainon (prefix_attributes, attributes);
24538       
24539       decl = grokfield (declarator, &declspecs,
24540                         NULL_TREE, /*init_const_expr_p=*/false,
24541                         NULL_TREE, attributes);
24542
24543       if (decl == error_mark_node || decl == NULL_TREE)
24544         return error_mark_node;
24545       
24546       /* Reset PREFIX_ATTRIBUTES.  */
24547       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24548         attributes = TREE_CHAIN (attributes);
24549       if (attributes)
24550         TREE_CHAIN (attributes) = NULL_TREE;
24551
24552       DECL_CHAIN (decl) = decls;
24553       decls = decl;
24554
24555       token = cp_lexer_peek_token (parser->lexer);
24556       if (token->type == CPP_COMMA)
24557         {
24558           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24559           continue;
24560         }
24561       else
24562         break;
24563     }
24564   return decls;
24565 }
24566
24567 /* Parse an Objective-C @property declaration.  The syntax is:
24568
24569    objc-property-declaration:
24570      '@property' objc-property-attributes[opt] struct-declaration ;
24571
24572    objc-property-attributes:
24573     '(' objc-property-attribute-list ')'
24574
24575    objc-property-attribute-list:
24576      objc-property-attribute
24577      objc-property-attribute-list, objc-property-attribute
24578
24579    objc-property-attribute
24580      'getter' = identifier
24581      'setter' = identifier
24582      'readonly'
24583      'readwrite'
24584      'assign'
24585      'retain'
24586      'copy'
24587      'nonatomic'
24588
24589   For example:
24590     @property NSString *name;
24591     @property (readonly) id object;
24592     @property (retain, nonatomic, getter=getTheName) id name;
24593     @property int a, b, c;
24594
24595    PS: This function is identical to
24596    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24597 static void 
24598 cp_parser_objc_at_property_declaration (cp_parser *parser)
24599 {
24600   /* The following variables hold the attributes of the properties as
24601      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24602      seen.  When we see an attribute, we set them to 'true' (if they
24603      are boolean properties) or to the identifier (if they have an
24604      argument, ie, for getter and setter).  Note that here we only
24605      parse the list of attributes, check the syntax and accumulate the
24606      attributes that we find.  objc_add_property_declaration() will
24607      then process the information.  */
24608   bool property_assign = false;
24609   bool property_copy = false;
24610   tree property_getter_ident = NULL_TREE;
24611   bool property_nonatomic = false;
24612   bool property_readonly = false;
24613   bool property_readwrite = false;
24614   bool property_retain = false;
24615   tree property_setter_ident = NULL_TREE;
24616
24617   /* 'properties' is the list of properties that we read.  Usually a
24618      single one, but maybe more (eg, in "@property int a, b, c;" there
24619      are three).  */
24620   tree properties;
24621   location_t loc;
24622
24623   loc = cp_lexer_peek_token (parser->lexer)->location;
24624
24625   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24626
24627   /* Parse the optional attribute list...  */
24628   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24629     {
24630       /* Eat the '('.  */
24631       cp_lexer_consume_token (parser->lexer);
24632
24633       while (true)
24634         {
24635           bool syntax_error = false;
24636           cp_token *token = cp_lexer_peek_token (parser->lexer);
24637           enum rid keyword;
24638
24639           if (token->type != CPP_NAME)
24640             {
24641               cp_parser_error (parser, "expected identifier");
24642               break;
24643             }
24644           keyword = C_RID_CODE (token->u.value);
24645           cp_lexer_consume_token (parser->lexer);
24646           switch (keyword)
24647             {
24648             case RID_ASSIGN:    property_assign = true;    break;
24649             case RID_COPY:      property_copy = true;      break;
24650             case RID_NONATOMIC: property_nonatomic = true; break;
24651             case RID_READONLY:  property_readonly = true;  break;
24652             case RID_READWRITE: property_readwrite = true; break;
24653             case RID_RETAIN:    property_retain = true;    break;
24654
24655             case RID_GETTER:
24656             case RID_SETTER:
24657               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24658                 {
24659                   if (keyword == RID_GETTER)
24660                     cp_parser_error (parser,
24661                                      "missing %<=%> (after %<getter%> attribute)");
24662                   else
24663                     cp_parser_error (parser,
24664                                      "missing %<=%> (after %<setter%> attribute)");
24665                   syntax_error = true;
24666                   break;
24667                 }
24668               cp_lexer_consume_token (parser->lexer); /* eat the = */
24669               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24670                 {
24671                   cp_parser_error (parser, "expected identifier");
24672                   syntax_error = true;
24673                   break;
24674                 }
24675               if (keyword == RID_SETTER)
24676                 {
24677                   if (property_setter_ident != NULL_TREE)
24678                     {
24679                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24680                       cp_lexer_consume_token (parser->lexer);
24681                     }
24682                   else
24683                     property_setter_ident = cp_parser_objc_selector (parser);
24684                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24685                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24686                   else
24687                     cp_lexer_consume_token (parser->lexer);
24688                 }
24689               else
24690                 {
24691                   if (property_getter_ident != NULL_TREE)
24692                     {
24693                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24694                       cp_lexer_consume_token (parser->lexer);
24695                     }
24696                   else
24697                     property_getter_ident = cp_parser_objc_selector (parser);
24698                 }
24699               break;
24700             default:
24701               cp_parser_error (parser, "unknown property attribute");
24702               syntax_error = true;
24703               break;
24704             }
24705
24706           if (syntax_error)
24707             break;
24708
24709           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24710             cp_lexer_consume_token (parser->lexer);
24711           else
24712             break;
24713         }
24714
24715       /* FIXME: "@property (setter, assign);" will generate a spurious
24716          "error: expected â€˜)’ before â€˜,’ token".  This is because
24717          cp_parser_require, unlike the C counterpart, will produce an
24718          error even if we are in error recovery.  */
24719       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24720         {
24721           cp_parser_skip_to_closing_parenthesis (parser,
24722                                                  /*recovering=*/true,
24723                                                  /*or_comma=*/false,
24724                                                  /*consume_paren=*/true);
24725         }
24726     }
24727
24728   /* ... and the property declaration(s).  */
24729   properties = cp_parser_objc_struct_declaration (parser);
24730
24731   if (properties == error_mark_node)
24732     {
24733       cp_parser_skip_to_end_of_statement (parser);
24734       /* If the next token is now a `;', consume it.  */
24735       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24736         cp_lexer_consume_token (parser->lexer);
24737       return;
24738     }
24739
24740   if (properties == NULL_TREE)
24741     cp_parser_error (parser, "expected identifier");
24742   else
24743     {
24744       /* Comma-separated properties are chained together in
24745          reverse order; add them one by one.  */
24746       properties = nreverse (properties);
24747       
24748       for (; properties; properties = TREE_CHAIN (properties))
24749         objc_add_property_declaration (loc, copy_node (properties),
24750                                        property_readonly, property_readwrite,
24751                                        property_assign, property_retain,
24752                                        property_copy, property_nonatomic,
24753                                        property_getter_ident, property_setter_ident);
24754     }
24755   
24756   cp_parser_consume_semicolon_at_end_of_statement (parser);
24757 }
24758
24759 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24760
24761    objc-synthesize-declaration:
24762      @synthesize objc-synthesize-identifier-list ;
24763
24764    objc-synthesize-identifier-list:
24765      objc-synthesize-identifier
24766      objc-synthesize-identifier-list, objc-synthesize-identifier
24767
24768    objc-synthesize-identifier
24769      identifier
24770      identifier = identifier
24771
24772   For example:
24773     @synthesize MyProperty;
24774     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24775
24776   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24777   for C.  Keep them in sync.
24778 */
24779 static void 
24780 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24781 {
24782   tree list = NULL_TREE;
24783   location_t loc;
24784   loc = cp_lexer_peek_token (parser->lexer)->location;
24785
24786   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24787   while (true)
24788     {
24789       tree property, ivar;
24790       property = cp_parser_identifier (parser);
24791       if (property == error_mark_node)
24792         {
24793           cp_parser_consume_semicolon_at_end_of_statement (parser);
24794           return;
24795         }
24796       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24797         {
24798           cp_lexer_consume_token (parser->lexer);
24799           ivar = cp_parser_identifier (parser);
24800           if (ivar == error_mark_node)
24801             {
24802               cp_parser_consume_semicolon_at_end_of_statement (parser);
24803               return;
24804             }
24805         }
24806       else
24807         ivar = NULL_TREE;
24808       list = chainon (list, build_tree_list (ivar, property));
24809       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24810         cp_lexer_consume_token (parser->lexer);
24811       else
24812         break;
24813     }
24814   cp_parser_consume_semicolon_at_end_of_statement (parser);
24815   objc_add_synthesize_declaration (loc, list);
24816 }
24817
24818 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24819
24820    objc-dynamic-declaration:
24821      @dynamic identifier-list ;
24822
24823    For example:
24824      @dynamic MyProperty;
24825      @dynamic MyProperty, AnotherProperty;
24826
24827   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24828   for C.  Keep them in sync.
24829 */
24830 static void 
24831 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24832 {
24833   tree list = NULL_TREE;
24834   location_t loc;
24835   loc = cp_lexer_peek_token (parser->lexer)->location;
24836
24837   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24838   while (true)
24839     {
24840       tree property;
24841       property = cp_parser_identifier (parser);
24842       if (property == error_mark_node)
24843         {
24844           cp_parser_consume_semicolon_at_end_of_statement (parser);
24845           return;
24846         }
24847       list = chainon (list, build_tree_list (NULL, property));
24848       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24849         cp_lexer_consume_token (parser->lexer);
24850       else
24851         break;
24852     }
24853   cp_parser_consume_semicolon_at_end_of_statement (parser);
24854   objc_add_dynamic_declaration (loc, list);
24855 }
24856
24857 \f
24858 /* OpenMP 2.5 parsing routines.  */
24859
24860 /* Returns name of the next clause.
24861    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24862    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24863    returned and the token is consumed.  */
24864
24865 static pragma_omp_clause
24866 cp_parser_omp_clause_name (cp_parser *parser)
24867 {
24868   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24869
24870   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24871     result = PRAGMA_OMP_CLAUSE_IF;
24872   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24873     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24874   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24875     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24876   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24877     {
24878       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24879       const char *p = IDENTIFIER_POINTER (id);
24880
24881       switch (p[0])
24882         {
24883         case 'c':
24884           if (!strcmp ("collapse", p))
24885             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24886           else if (!strcmp ("copyin", p))
24887             result = PRAGMA_OMP_CLAUSE_COPYIN;
24888           else if (!strcmp ("copyprivate", p))
24889             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24890           break;
24891         case 'f':
24892           if (!strcmp ("final", p))
24893             result = PRAGMA_OMP_CLAUSE_FINAL;
24894           else if (!strcmp ("firstprivate", p))
24895             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24896           break;
24897         case 'l':
24898           if (!strcmp ("lastprivate", p))
24899             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24900           break;
24901         case 'm':
24902           if (!strcmp ("mergeable", p))
24903             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24904           break;
24905         case 'n':
24906           if (!strcmp ("nowait", p))
24907             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24908           else if (!strcmp ("num_threads", p))
24909             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24910           break;
24911         case 'o':
24912           if (!strcmp ("ordered", p))
24913             result = PRAGMA_OMP_CLAUSE_ORDERED;
24914           break;
24915         case 'r':
24916           if (!strcmp ("reduction", p))
24917             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24918           break;
24919         case 's':
24920           if (!strcmp ("schedule", p))
24921             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24922           else if (!strcmp ("shared", p))
24923             result = PRAGMA_OMP_CLAUSE_SHARED;
24924           break;
24925         case 'u':
24926           if (!strcmp ("untied", p))
24927             result = PRAGMA_OMP_CLAUSE_UNTIED;
24928           break;
24929         }
24930     }
24931
24932   if (result != PRAGMA_OMP_CLAUSE_NONE)
24933     cp_lexer_consume_token (parser->lexer);
24934
24935   return result;
24936 }
24937
24938 /* Validate that a clause of the given type does not already exist.  */
24939
24940 static void
24941 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24942                            const char *name, location_t location)
24943 {
24944   tree c;
24945
24946   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24947     if (OMP_CLAUSE_CODE (c) == code)
24948       {
24949         error_at (location, "too many %qs clauses", name);
24950         break;
24951       }
24952 }
24953
24954 /* OpenMP 2.5:
24955    variable-list:
24956      identifier
24957      variable-list , identifier
24958
24959    In addition, we match a closing parenthesis.  An opening parenthesis
24960    will have been consumed by the caller.
24961
24962    If KIND is nonzero, create the appropriate node and install the decl
24963    in OMP_CLAUSE_DECL and add the node to the head of the list.
24964
24965    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24966    return the list created.  */
24967
24968 static tree
24969 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24970                                 tree list)
24971 {
24972   cp_token *token;
24973   while (1)
24974     {
24975       tree name, decl;
24976
24977       token = cp_lexer_peek_token (parser->lexer);
24978       name = cp_parser_id_expression (parser, /*template_p=*/false,
24979                                       /*check_dependency_p=*/true,
24980                                       /*template_p=*/NULL,
24981                                       /*declarator_p=*/false,
24982                                       /*optional_p=*/false);
24983       if (name == error_mark_node)
24984         goto skip_comma;
24985
24986       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24987       if (decl == error_mark_node)
24988         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24989                                      token->location);
24990       else if (kind != 0)
24991         {
24992           tree u = build_omp_clause (token->location, kind);
24993           OMP_CLAUSE_DECL (u) = decl;
24994           OMP_CLAUSE_CHAIN (u) = list;
24995           list = u;
24996         }
24997       else
24998         list = tree_cons (decl, NULL_TREE, list);
24999
25000     get_comma:
25001       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25002         break;
25003       cp_lexer_consume_token (parser->lexer);
25004     }
25005
25006   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25007     {
25008       int ending;
25009
25010       /* Try to resync to an unnested comma.  Copied from
25011          cp_parser_parenthesized_expression_list.  */
25012     skip_comma:
25013       ending = cp_parser_skip_to_closing_parenthesis (parser,
25014                                                       /*recovering=*/true,
25015                                                       /*or_comma=*/true,
25016                                                       /*consume_paren=*/true);
25017       if (ending < 0)
25018         goto get_comma;
25019     }
25020
25021   return list;
25022 }
25023
25024 /* Similarly, but expect leading and trailing parenthesis.  This is a very
25025    common case for omp clauses.  */
25026
25027 static tree
25028 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25029 {
25030   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25031     return cp_parser_omp_var_list_no_open (parser, kind, list);
25032   return list;
25033 }
25034
25035 /* OpenMP 3.0:
25036    collapse ( constant-expression ) */
25037
25038 static tree
25039 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25040 {
25041   tree c, num;
25042   location_t loc;
25043   HOST_WIDE_INT n;
25044
25045   loc = cp_lexer_peek_token (parser->lexer)->location;
25046   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25047     return list;
25048
25049   num = cp_parser_constant_expression (parser, false, NULL);
25050
25051   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25052     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25053                                            /*or_comma=*/false,
25054                                            /*consume_paren=*/true);
25055
25056   if (num == error_mark_node)
25057     return list;
25058   num = fold_non_dependent_expr (num);
25059   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25060       || !host_integerp (num, 0)
25061       || (n = tree_low_cst (num, 0)) <= 0
25062       || (int) n != n)
25063     {
25064       error_at (loc, "collapse argument needs positive constant integer expression");
25065       return list;
25066     }
25067
25068   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25069   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25070   OMP_CLAUSE_CHAIN (c) = list;
25071   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25072
25073   return c;
25074 }
25075
25076 /* OpenMP 2.5:
25077    default ( shared | none ) */
25078
25079 static tree
25080 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25081 {
25082   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25083   tree c;
25084
25085   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25086     return list;
25087   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25088     {
25089       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25090       const char *p = IDENTIFIER_POINTER (id);
25091
25092       switch (p[0])
25093         {
25094         case 'n':
25095           if (strcmp ("none", p) != 0)
25096             goto invalid_kind;
25097           kind = OMP_CLAUSE_DEFAULT_NONE;
25098           break;
25099
25100         case 's':
25101           if (strcmp ("shared", p) != 0)
25102             goto invalid_kind;
25103           kind = OMP_CLAUSE_DEFAULT_SHARED;
25104           break;
25105
25106         default:
25107           goto invalid_kind;
25108         }
25109
25110       cp_lexer_consume_token (parser->lexer);
25111     }
25112   else
25113     {
25114     invalid_kind:
25115       cp_parser_error (parser, "expected %<none%> or %<shared%>");
25116     }
25117
25118   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25119     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25120                                            /*or_comma=*/false,
25121                                            /*consume_paren=*/true);
25122
25123   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25124     return list;
25125
25126   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25127   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25128   OMP_CLAUSE_CHAIN (c) = list;
25129   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25130
25131   return c;
25132 }
25133
25134 /* OpenMP 3.1:
25135    final ( expression ) */
25136
25137 static tree
25138 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25139 {
25140   tree t, c;
25141
25142   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25143     return list;
25144
25145   t = cp_parser_condition (parser);
25146
25147   if (t == error_mark_node
25148       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25149     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25150                                            /*or_comma=*/false,
25151                                            /*consume_paren=*/true);
25152
25153   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25154
25155   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25156   OMP_CLAUSE_FINAL_EXPR (c) = t;
25157   OMP_CLAUSE_CHAIN (c) = list;
25158
25159   return c;
25160 }
25161
25162 /* OpenMP 2.5:
25163    if ( expression ) */
25164
25165 static tree
25166 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25167 {
25168   tree t, c;
25169
25170   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25171     return list;
25172
25173   t = cp_parser_condition (parser);
25174
25175   if (t == error_mark_node
25176       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25177     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25178                                            /*or_comma=*/false,
25179                                            /*consume_paren=*/true);
25180
25181   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25182
25183   c = build_omp_clause (location, OMP_CLAUSE_IF);
25184   OMP_CLAUSE_IF_EXPR (c) = t;
25185   OMP_CLAUSE_CHAIN (c) = list;
25186
25187   return c;
25188 }
25189
25190 /* OpenMP 3.1:
25191    mergeable */
25192
25193 static tree
25194 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25195                                 tree list, location_t location)
25196 {
25197   tree c;
25198
25199   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25200                              location);
25201
25202   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25203   OMP_CLAUSE_CHAIN (c) = list;
25204   return c;
25205 }
25206
25207 /* OpenMP 2.5:
25208    nowait */
25209
25210 static tree
25211 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25212                              tree list, location_t location)
25213 {
25214   tree c;
25215
25216   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25217
25218   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25219   OMP_CLAUSE_CHAIN (c) = list;
25220   return c;
25221 }
25222
25223 /* OpenMP 2.5:
25224    num_threads ( expression ) */
25225
25226 static tree
25227 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25228                                   location_t location)
25229 {
25230   tree t, c;
25231
25232   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25233     return list;
25234
25235   t = cp_parser_expression (parser, false, NULL);
25236
25237   if (t == error_mark_node
25238       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25239     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25240                                            /*or_comma=*/false,
25241                                            /*consume_paren=*/true);
25242
25243   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25244                              "num_threads", location);
25245
25246   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25247   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25248   OMP_CLAUSE_CHAIN (c) = list;
25249
25250   return c;
25251 }
25252
25253 /* OpenMP 2.5:
25254    ordered */
25255
25256 static tree
25257 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25258                               tree list, location_t location)
25259 {
25260   tree c;
25261
25262   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25263                              "ordered", location);
25264
25265   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25266   OMP_CLAUSE_CHAIN (c) = list;
25267   return c;
25268 }
25269
25270 /* OpenMP 2.5:
25271    reduction ( reduction-operator : variable-list )
25272
25273    reduction-operator:
25274      One of: + * - & ^ | && ||
25275
25276    OpenMP 3.1:
25277
25278    reduction-operator:
25279      One of: + * - & ^ | && || min max  */
25280
25281 static tree
25282 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25283 {
25284   enum tree_code code;
25285   tree nlist, c;
25286
25287   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25288     return list;
25289
25290   switch (cp_lexer_peek_token (parser->lexer)->type)
25291     {
25292     case CPP_PLUS:
25293       code = PLUS_EXPR;
25294       break;
25295     case CPP_MULT:
25296       code = MULT_EXPR;
25297       break;
25298     case CPP_MINUS:
25299       code = MINUS_EXPR;
25300       break;
25301     case CPP_AND:
25302       code = BIT_AND_EXPR;
25303       break;
25304     case CPP_XOR:
25305       code = BIT_XOR_EXPR;
25306       break;
25307     case CPP_OR:
25308       code = BIT_IOR_EXPR;
25309       break;
25310     case CPP_AND_AND:
25311       code = TRUTH_ANDIF_EXPR;
25312       break;
25313     case CPP_OR_OR:
25314       code = TRUTH_ORIF_EXPR;
25315       break;
25316     case CPP_NAME:
25317       {
25318         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25319         const char *p = IDENTIFIER_POINTER (id);
25320
25321         if (strcmp (p, "min") == 0)
25322           {
25323             code = MIN_EXPR;
25324             break;
25325           }
25326         if (strcmp (p, "max") == 0)
25327           {
25328             code = MAX_EXPR;
25329             break;
25330           }
25331       }
25332       /* FALLTHROUGH */
25333     default:
25334       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25335                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25336     resync_fail:
25337       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25338                                              /*or_comma=*/false,
25339                                              /*consume_paren=*/true);
25340       return list;
25341     }
25342   cp_lexer_consume_token (parser->lexer);
25343
25344   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25345     goto resync_fail;
25346
25347   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25348   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25349     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25350
25351   return nlist;
25352 }
25353
25354 /* OpenMP 2.5:
25355    schedule ( schedule-kind )
25356    schedule ( schedule-kind , expression )
25357
25358    schedule-kind:
25359      static | dynamic | guided | runtime | auto  */
25360
25361 static tree
25362 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25363 {
25364   tree c, t;
25365
25366   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25367     return list;
25368
25369   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25370
25371   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25372     {
25373       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25374       const char *p = IDENTIFIER_POINTER (id);
25375
25376       switch (p[0])
25377         {
25378         case 'd':
25379           if (strcmp ("dynamic", p) != 0)
25380             goto invalid_kind;
25381           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25382           break;
25383
25384         case 'g':
25385           if (strcmp ("guided", p) != 0)
25386             goto invalid_kind;
25387           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25388           break;
25389
25390         case 'r':
25391           if (strcmp ("runtime", p) != 0)
25392             goto invalid_kind;
25393           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25394           break;
25395
25396         default:
25397           goto invalid_kind;
25398         }
25399     }
25400   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25401     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25402   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25403     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25404   else
25405     goto invalid_kind;
25406   cp_lexer_consume_token (parser->lexer);
25407
25408   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25409     {
25410       cp_token *token;
25411       cp_lexer_consume_token (parser->lexer);
25412
25413       token = cp_lexer_peek_token (parser->lexer);
25414       t = cp_parser_assignment_expression (parser, false, NULL);
25415
25416       if (t == error_mark_node)
25417         goto resync_fail;
25418       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25419         error_at (token->location, "schedule %<runtime%> does not take "
25420                   "a %<chunk_size%> parameter");
25421       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25422         error_at (token->location, "schedule %<auto%> does not take "
25423                   "a %<chunk_size%> parameter");
25424       else
25425         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25426
25427       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25428         goto resync_fail;
25429     }
25430   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25431     goto resync_fail;
25432
25433   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25434   OMP_CLAUSE_CHAIN (c) = list;
25435   return c;
25436
25437  invalid_kind:
25438   cp_parser_error (parser, "invalid schedule kind");
25439  resync_fail:
25440   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25441                                          /*or_comma=*/false,
25442                                          /*consume_paren=*/true);
25443   return list;
25444 }
25445
25446 /* OpenMP 3.0:
25447    untied */
25448
25449 static tree
25450 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25451                              tree list, location_t location)
25452 {
25453   tree c;
25454
25455   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25456
25457   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25458   OMP_CLAUSE_CHAIN (c) = list;
25459   return c;
25460 }
25461
25462 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25463    is a bitmask in MASK.  Return the list of clauses found; the result
25464    of clause default goes in *pdefault.  */
25465
25466 static tree
25467 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25468                            const char *where, cp_token *pragma_tok)
25469 {
25470   tree clauses = NULL;
25471   bool first = true;
25472   cp_token *token = NULL;
25473
25474   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25475     {
25476       pragma_omp_clause c_kind;
25477       const char *c_name;
25478       tree prev = clauses;
25479
25480       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25481         cp_lexer_consume_token (parser->lexer);
25482
25483       token = cp_lexer_peek_token (parser->lexer);
25484       c_kind = cp_parser_omp_clause_name (parser);
25485       first = false;
25486
25487       switch (c_kind)
25488         {
25489         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25490           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25491                                                    token->location);
25492           c_name = "collapse";
25493           break;
25494         case PRAGMA_OMP_CLAUSE_COPYIN:
25495           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25496           c_name = "copyin";
25497           break;
25498         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25499           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25500                                             clauses);
25501           c_name = "copyprivate";
25502           break;
25503         case PRAGMA_OMP_CLAUSE_DEFAULT:
25504           clauses = cp_parser_omp_clause_default (parser, clauses,
25505                                                   token->location);
25506           c_name = "default";
25507           break;
25508         case PRAGMA_OMP_CLAUSE_FINAL:
25509           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25510           c_name = "final";
25511           break;
25512         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25513           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25514                                             clauses);
25515           c_name = "firstprivate";
25516           break;
25517         case PRAGMA_OMP_CLAUSE_IF:
25518           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25519           c_name = "if";
25520           break;
25521         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25522           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25523                                             clauses);
25524           c_name = "lastprivate";
25525           break;
25526         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25527           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25528                                                     token->location);
25529           c_name = "mergeable";
25530           break;
25531         case PRAGMA_OMP_CLAUSE_NOWAIT:
25532           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25533           c_name = "nowait";
25534           break;
25535         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25536           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25537                                                       token->location);
25538           c_name = "num_threads";
25539           break;
25540         case PRAGMA_OMP_CLAUSE_ORDERED:
25541           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25542                                                   token->location);
25543           c_name = "ordered";
25544           break;
25545         case PRAGMA_OMP_CLAUSE_PRIVATE:
25546           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25547                                             clauses);
25548           c_name = "private";
25549           break;
25550         case PRAGMA_OMP_CLAUSE_REDUCTION:
25551           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25552           c_name = "reduction";
25553           break;
25554         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25555           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25556                                                    token->location);
25557           c_name = "schedule";
25558           break;
25559         case PRAGMA_OMP_CLAUSE_SHARED:
25560           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25561                                             clauses);
25562           c_name = "shared";
25563           break;
25564         case PRAGMA_OMP_CLAUSE_UNTIED:
25565           clauses = cp_parser_omp_clause_untied (parser, clauses,
25566                                                  token->location);
25567           c_name = "nowait";
25568           break;
25569         default:
25570           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25571           goto saw_error;
25572         }
25573
25574       if (((mask >> c_kind) & 1) == 0)
25575         {
25576           /* Remove the invalid clause(s) from the list to avoid
25577              confusing the rest of the compiler.  */
25578           clauses = prev;
25579           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25580         }
25581     }
25582  saw_error:
25583   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25584   return finish_omp_clauses (clauses);
25585 }
25586
25587 /* OpenMP 2.5:
25588    structured-block:
25589      statement
25590
25591    In practice, we're also interested in adding the statement to an
25592    outer node.  So it is convenient if we work around the fact that
25593    cp_parser_statement calls add_stmt.  */
25594
25595 static unsigned
25596 cp_parser_begin_omp_structured_block (cp_parser *parser)
25597 {
25598   unsigned save = parser->in_statement;
25599
25600   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25601      This preserves the "not within loop or switch" style error messages
25602      for nonsense cases like
25603         void foo() {
25604         #pragma omp single
25605           break;
25606         }
25607   */
25608   if (parser->in_statement)
25609     parser->in_statement = IN_OMP_BLOCK;
25610
25611   return save;
25612 }
25613
25614 static void
25615 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25616 {
25617   parser->in_statement = save;
25618 }
25619
25620 static tree
25621 cp_parser_omp_structured_block (cp_parser *parser)
25622 {
25623   tree stmt = begin_omp_structured_block ();
25624   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25625
25626   cp_parser_statement (parser, NULL_TREE, false, NULL);
25627
25628   cp_parser_end_omp_structured_block (parser, save);
25629   return finish_omp_structured_block (stmt);
25630 }
25631
25632 /* OpenMP 2.5:
25633    # pragma omp atomic new-line
25634      expression-stmt
25635
25636    expression-stmt:
25637      x binop= expr | x++ | ++x | x-- | --x
25638    binop:
25639      +, *, -, /, &, ^, |, <<, >>
25640
25641   where x is an lvalue expression with scalar type.
25642
25643    OpenMP 3.1:
25644    # pragma omp atomic new-line
25645      update-stmt
25646
25647    # pragma omp atomic read new-line
25648      read-stmt
25649
25650    # pragma omp atomic write new-line
25651      write-stmt
25652
25653    # pragma omp atomic update new-line
25654      update-stmt
25655
25656    # pragma omp atomic capture new-line
25657      capture-stmt
25658
25659    # pragma omp atomic capture new-line
25660      capture-block
25661
25662    read-stmt:
25663      v = x
25664    write-stmt:
25665      x = expr
25666    update-stmt:
25667      expression-stmt | x = x binop expr
25668    capture-stmt:
25669      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25670    capture-block:
25671      { v = x; update-stmt; } | { update-stmt; v = x; }
25672
25673   where x and v are lvalue expressions with scalar type.  */
25674
25675 static void
25676 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25677 {
25678   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25679   tree rhs1 = NULL_TREE, orig_lhs;
25680   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25681   bool structured_block = false;
25682
25683   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25684     {
25685       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25686       const char *p = IDENTIFIER_POINTER (id);
25687
25688       if (!strcmp (p, "read"))
25689         code = OMP_ATOMIC_READ;
25690       else if (!strcmp (p, "write"))
25691         code = NOP_EXPR;
25692       else if (!strcmp (p, "update"))
25693         code = OMP_ATOMIC;
25694       else if (!strcmp (p, "capture"))
25695         code = OMP_ATOMIC_CAPTURE_NEW;
25696       else
25697         p = NULL;
25698       if (p)
25699         cp_lexer_consume_token (parser->lexer);
25700     }
25701   cp_parser_require_pragma_eol (parser, pragma_tok);
25702
25703   switch (code)
25704     {
25705     case OMP_ATOMIC_READ:
25706     case NOP_EXPR: /* atomic write */
25707       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25708                                       /*cast_p=*/false, NULL);
25709       if (v == error_mark_node)
25710         goto saw_error;
25711       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25712         goto saw_error;
25713       if (code == NOP_EXPR)
25714         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25715       else
25716         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25717                                           /*cast_p=*/false, NULL);
25718       if (lhs == error_mark_node)
25719         goto saw_error;
25720       if (code == NOP_EXPR)
25721         {
25722           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25723              opcode.  */
25724           code = OMP_ATOMIC;
25725           rhs = lhs;
25726           lhs = v;
25727           v = NULL_TREE;
25728         }
25729       goto done;
25730     case OMP_ATOMIC_CAPTURE_NEW:
25731       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25732         {
25733           cp_lexer_consume_token (parser->lexer);
25734           structured_block = true;
25735         }
25736       else
25737         {
25738           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25739                                           /*cast_p=*/false, NULL);
25740           if (v == error_mark_node)
25741             goto saw_error;
25742           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25743             goto saw_error;
25744         }
25745     default:
25746       break;
25747     }
25748
25749 restart:
25750   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25751                                     /*cast_p=*/false, NULL);
25752   orig_lhs = lhs;
25753   switch (TREE_CODE (lhs))
25754     {
25755     case ERROR_MARK:
25756       goto saw_error;
25757
25758     case POSTINCREMENT_EXPR:
25759       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25760         code = OMP_ATOMIC_CAPTURE_OLD;
25761       /* FALLTHROUGH */
25762     case PREINCREMENT_EXPR:
25763       lhs = TREE_OPERAND (lhs, 0);
25764       opcode = PLUS_EXPR;
25765       rhs = integer_one_node;
25766       break;
25767
25768     case POSTDECREMENT_EXPR:
25769       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25770         code = OMP_ATOMIC_CAPTURE_OLD;
25771       /* FALLTHROUGH */
25772     case PREDECREMENT_EXPR:
25773       lhs = TREE_OPERAND (lhs, 0);
25774       opcode = MINUS_EXPR;
25775       rhs = integer_one_node;
25776       break;
25777
25778     case COMPOUND_EXPR:
25779       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25780          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25781          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25782          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25783          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25784                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25785             == BOOLEAN_TYPE)
25786        /* Undo effects of boolean_increment for post {in,de}crement.  */
25787        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25788       /* FALLTHRU */
25789     case MODIFY_EXPR:
25790       if (TREE_CODE (lhs) == MODIFY_EXPR
25791          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25792         {
25793           /* Undo effects of boolean_increment.  */
25794           if (integer_onep (TREE_OPERAND (lhs, 1)))
25795             {
25796               /* This is pre or post increment.  */
25797               rhs = TREE_OPERAND (lhs, 1);
25798               lhs = TREE_OPERAND (lhs, 0);
25799               opcode = NOP_EXPR;
25800               if (code == OMP_ATOMIC_CAPTURE_NEW
25801                   && !structured_block
25802                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25803                 code = OMP_ATOMIC_CAPTURE_OLD;
25804               break;
25805             }
25806         }
25807       /* FALLTHRU */
25808     default:
25809       switch (cp_lexer_peek_token (parser->lexer)->type)
25810         {
25811         case CPP_MULT_EQ:
25812           opcode = MULT_EXPR;
25813           break;
25814         case CPP_DIV_EQ:
25815           opcode = TRUNC_DIV_EXPR;
25816           break;
25817         case CPP_PLUS_EQ:
25818           opcode = PLUS_EXPR;
25819           break;
25820         case CPP_MINUS_EQ:
25821           opcode = MINUS_EXPR;
25822           break;
25823         case CPP_LSHIFT_EQ:
25824           opcode = LSHIFT_EXPR;
25825           break;
25826         case CPP_RSHIFT_EQ:
25827           opcode = RSHIFT_EXPR;
25828           break;
25829         case CPP_AND_EQ:
25830           opcode = BIT_AND_EXPR;
25831           break;
25832         case CPP_OR_EQ:
25833           opcode = BIT_IOR_EXPR;
25834           break;
25835         case CPP_XOR_EQ:
25836           opcode = BIT_XOR_EXPR;
25837           break;
25838         case CPP_EQ:
25839           if (structured_block || code == OMP_ATOMIC)
25840             {
25841               enum cp_parser_prec oprec;
25842               cp_token *token;
25843               cp_lexer_consume_token (parser->lexer);
25844               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25845                                                  /*cast_p=*/false, NULL);
25846               if (rhs1 == error_mark_node)
25847                 goto saw_error;
25848               token = cp_lexer_peek_token (parser->lexer);
25849               switch (token->type)
25850                 {
25851                 case CPP_SEMICOLON:
25852                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25853                     {
25854                       code = OMP_ATOMIC_CAPTURE_OLD;
25855                       v = lhs;
25856                       lhs = NULL_TREE;
25857                       lhs1 = rhs1;
25858                       rhs1 = NULL_TREE;
25859                       cp_lexer_consume_token (parser->lexer);
25860                       goto restart;
25861                     }
25862                   cp_parser_error (parser,
25863                                    "invalid form of %<#pragma omp atomic%>");
25864                   goto saw_error;
25865                 case CPP_MULT:
25866                   opcode = MULT_EXPR;
25867                   break;
25868                 case CPP_DIV:
25869                   opcode = TRUNC_DIV_EXPR;
25870                   break;
25871                 case CPP_PLUS:
25872                   opcode = PLUS_EXPR;
25873                   break;
25874                 case CPP_MINUS:
25875                   opcode = MINUS_EXPR;
25876                   break;
25877                 case CPP_LSHIFT:
25878                   opcode = LSHIFT_EXPR;
25879                   break;
25880                 case CPP_RSHIFT:
25881                   opcode = RSHIFT_EXPR;
25882                   break;
25883                 case CPP_AND:
25884                   opcode = BIT_AND_EXPR;
25885                   break;
25886                 case CPP_OR:
25887                   opcode = BIT_IOR_EXPR;
25888                   break;
25889                 case CPP_XOR:
25890                   opcode = BIT_XOR_EXPR;
25891                   break;
25892                 default:
25893                   cp_parser_error (parser,
25894                                    "invalid operator for %<#pragma omp atomic%>");
25895                   goto saw_error;
25896                 }
25897               oprec = TOKEN_PRECEDENCE (token);
25898               gcc_assert (oprec != PREC_NOT_OPERATOR);
25899               if (commutative_tree_code (opcode))
25900                 oprec = (enum cp_parser_prec) (oprec - 1);
25901               cp_lexer_consume_token (parser->lexer);
25902               rhs = cp_parser_binary_expression (parser, false, false,
25903                                                  oprec, NULL);
25904               if (rhs == error_mark_node)
25905                 goto saw_error;
25906               goto stmt_done;
25907             }
25908           /* FALLTHROUGH */
25909         default:
25910           cp_parser_error (parser,
25911                            "invalid operator for %<#pragma omp atomic%>");
25912           goto saw_error;
25913         }
25914       cp_lexer_consume_token (parser->lexer);
25915
25916       rhs = cp_parser_expression (parser, false, NULL);
25917       if (rhs == error_mark_node)
25918         goto saw_error;
25919       break;
25920     }
25921 stmt_done:
25922   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25923     {
25924       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25925         goto saw_error;
25926       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25927                                       /*cast_p=*/false, NULL);
25928       if (v == error_mark_node)
25929         goto saw_error;
25930       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25931         goto saw_error;
25932       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25933                                          /*cast_p=*/false, NULL);
25934       if (lhs1 == error_mark_node)
25935         goto saw_error;
25936     }
25937   if (structured_block)
25938     {
25939       cp_parser_consume_semicolon_at_end_of_statement (parser);
25940       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25941     }
25942 done:
25943   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25944   if (!structured_block)
25945     cp_parser_consume_semicolon_at_end_of_statement (parser);
25946   return;
25947
25948  saw_error:
25949   cp_parser_skip_to_end_of_block_or_statement (parser);
25950   if (structured_block)
25951     {
25952       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25953         cp_lexer_consume_token (parser->lexer);
25954       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25955         {
25956           cp_parser_skip_to_end_of_block_or_statement (parser);
25957           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25958             cp_lexer_consume_token (parser->lexer);
25959         }
25960     }
25961 }
25962
25963
25964 /* OpenMP 2.5:
25965    # pragma omp barrier new-line  */
25966
25967 static void
25968 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25969 {
25970   cp_parser_require_pragma_eol (parser, pragma_tok);
25971   finish_omp_barrier ();
25972 }
25973
25974 /* OpenMP 2.5:
25975    # pragma omp critical [(name)] new-line
25976      structured-block  */
25977
25978 static tree
25979 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25980 {
25981   tree stmt, name = NULL;
25982
25983   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25984     {
25985       cp_lexer_consume_token (parser->lexer);
25986
25987       name = cp_parser_identifier (parser);
25988
25989       if (name == error_mark_node
25990           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25991         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25992                                                /*or_comma=*/false,
25993                                                /*consume_paren=*/true);
25994       if (name == error_mark_node)
25995         name = NULL;
25996     }
25997   cp_parser_require_pragma_eol (parser, pragma_tok);
25998
25999   stmt = cp_parser_omp_structured_block (parser);
26000   return c_finish_omp_critical (input_location, stmt, name);
26001 }
26002
26003 /* OpenMP 2.5:
26004    # pragma omp flush flush-vars[opt] new-line
26005
26006    flush-vars:
26007      ( variable-list ) */
26008
26009 static void
26010 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26011 {
26012   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26013     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26014   cp_parser_require_pragma_eol (parser, pragma_tok);
26015
26016   finish_omp_flush ();
26017 }
26018
26019 /* Helper function, to parse omp for increment expression.  */
26020
26021 static tree
26022 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26023 {
26024   tree cond = cp_parser_binary_expression (parser, false, true,
26025                                            PREC_NOT_OPERATOR, NULL);
26026   if (cond == error_mark_node
26027       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26028     {
26029       cp_parser_skip_to_end_of_statement (parser);
26030       return error_mark_node;
26031     }
26032
26033   switch (TREE_CODE (cond))
26034     {
26035     case GT_EXPR:
26036     case GE_EXPR:
26037     case LT_EXPR:
26038     case LE_EXPR:
26039       break;
26040     default:
26041       return error_mark_node;
26042     }
26043
26044   /* If decl is an iterator, preserve LHS and RHS of the relational
26045      expr until finish_omp_for.  */
26046   if (decl
26047       && (type_dependent_expression_p (decl)
26048           || CLASS_TYPE_P (TREE_TYPE (decl))))
26049     return cond;
26050
26051   return build_x_binary_op (TREE_CODE (cond),
26052                             TREE_OPERAND (cond, 0), ERROR_MARK,
26053                             TREE_OPERAND (cond, 1), ERROR_MARK,
26054                             /*overload=*/NULL, tf_warning_or_error);
26055 }
26056
26057 /* Helper function, to parse omp for increment expression.  */
26058
26059 static tree
26060 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26061 {
26062   cp_token *token = cp_lexer_peek_token (parser->lexer);
26063   enum tree_code op;
26064   tree lhs, rhs;
26065   cp_id_kind idk;
26066   bool decl_first;
26067
26068   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26069     {
26070       op = (token->type == CPP_PLUS_PLUS
26071             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26072       cp_lexer_consume_token (parser->lexer);
26073       lhs = cp_parser_cast_expression (parser, false, false, NULL);
26074       if (lhs != decl)
26075         return error_mark_node;
26076       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26077     }
26078
26079   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26080   if (lhs != decl)
26081     return error_mark_node;
26082
26083   token = cp_lexer_peek_token (parser->lexer);
26084   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26085     {
26086       op = (token->type == CPP_PLUS_PLUS
26087             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26088       cp_lexer_consume_token (parser->lexer);
26089       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26090     }
26091
26092   op = cp_parser_assignment_operator_opt (parser);
26093   if (op == ERROR_MARK)
26094     return error_mark_node;
26095
26096   if (op != NOP_EXPR)
26097     {
26098       rhs = cp_parser_assignment_expression (parser, false, NULL);
26099       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26100       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26101     }
26102
26103   lhs = cp_parser_binary_expression (parser, false, false,
26104                                      PREC_ADDITIVE_EXPRESSION, NULL);
26105   token = cp_lexer_peek_token (parser->lexer);
26106   decl_first = lhs == decl;
26107   if (decl_first)
26108     lhs = NULL_TREE;
26109   if (token->type != CPP_PLUS
26110       && token->type != CPP_MINUS)
26111     return error_mark_node;
26112
26113   do
26114     {
26115       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26116       cp_lexer_consume_token (parser->lexer);
26117       rhs = cp_parser_binary_expression (parser, false, false,
26118                                          PREC_ADDITIVE_EXPRESSION, NULL);
26119       token = cp_lexer_peek_token (parser->lexer);
26120       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26121         {
26122           if (lhs == NULL_TREE)
26123             {
26124               if (op == PLUS_EXPR)
26125                 lhs = rhs;
26126               else
26127                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
26128             }
26129           else
26130             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
26131                                      NULL, tf_warning_or_error);
26132         }
26133     }
26134   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26135
26136   if (!decl_first)
26137     {
26138       if (rhs != decl || op == MINUS_EXPR)
26139         return error_mark_node;
26140       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26141     }
26142   else
26143     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26144
26145   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26146 }
26147
26148 /* Parse the restricted form of the for statement allowed by OpenMP.  */
26149
26150 static tree
26151 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26152 {
26153   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26154   tree real_decl, initv, condv, incrv, declv;
26155   tree this_pre_body, cl;
26156   location_t loc_first;
26157   bool collapse_err = false;
26158   int i, collapse = 1, nbraces = 0;
26159   VEC(tree,gc) *for_block = make_tree_vector ();
26160
26161   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26162     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26163       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26164
26165   gcc_assert (collapse >= 1);
26166
26167   declv = make_tree_vec (collapse);
26168   initv = make_tree_vec (collapse);
26169   condv = make_tree_vec (collapse);
26170   incrv = make_tree_vec (collapse);
26171
26172   loc_first = cp_lexer_peek_token (parser->lexer)->location;
26173
26174   for (i = 0; i < collapse; i++)
26175     {
26176       int bracecount = 0;
26177       bool add_private_clause = false;
26178       location_t loc;
26179
26180       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26181         {
26182           cp_parser_error (parser, "for statement expected");
26183           return NULL;
26184         }
26185       loc = cp_lexer_consume_token (parser->lexer)->location;
26186
26187       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26188         return NULL;
26189
26190       init = decl = real_decl = NULL;
26191       this_pre_body = push_stmt_list ();
26192       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26193         {
26194           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26195
26196              init-expr:
26197                        var = lb
26198                        integer-type var = lb
26199                        random-access-iterator-type var = lb
26200                        pointer-type var = lb
26201           */
26202           cp_decl_specifier_seq type_specifiers;
26203
26204           /* First, try to parse as an initialized declaration.  See
26205              cp_parser_condition, from whence the bulk of this is copied.  */
26206
26207           cp_parser_parse_tentatively (parser);
26208           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26209                                         /*is_trailing_return=*/false,
26210                                         &type_specifiers);
26211           if (cp_parser_parse_definitely (parser))
26212             {
26213               /* If parsing a type specifier seq succeeded, then this
26214                  MUST be a initialized declaration.  */
26215               tree asm_specification, attributes;
26216               cp_declarator *declarator;
26217
26218               declarator = cp_parser_declarator (parser,
26219                                                  CP_PARSER_DECLARATOR_NAMED,
26220                                                  /*ctor_dtor_or_conv_p=*/NULL,
26221                                                  /*parenthesized_p=*/NULL,
26222                                                  /*member_p=*/false);
26223               attributes = cp_parser_attributes_opt (parser);
26224               asm_specification = cp_parser_asm_specification_opt (parser);
26225
26226               if (declarator == cp_error_declarator) 
26227                 cp_parser_skip_to_end_of_statement (parser);
26228
26229               else 
26230                 {
26231                   tree pushed_scope, auto_node;
26232
26233                   decl = start_decl (declarator, &type_specifiers,
26234                                      SD_INITIALIZED, attributes,
26235                                      /*prefix_attributes=*/NULL_TREE,
26236                                      &pushed_scope);
26237
26238                   auto_node = type_uses_auto (TREE_TYPE (decl));
26239                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26240                     {
26241                       if (cp_lexer_next_token_is (parser->lexer, 
26242                                                   CPP_OPEN_PAREN))
26243                         error ("parenthesized initialization is not allowed in "
26244                                "OpenMP %<for%> loop");
26245                       else
26246                         /* Trigger an error.  */
26247                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26248
26249                       init = error_mark_node;
26250                       cp_parser_skip_to_end_of_statement (parser);
26251                     }
26252                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26253                            || type_dependent_expression_p (decl)
26254                            || auto_node)
26255                     {
26256                       bool is_direct_init, is_non_constant_init;
26257
26258                       init = cp_parser_initializer (parser,
26259                                                     &is_direct_init,
26260                                                     &is_non_constant_init);
26261
26262                       if (auto_node)
26263                         {
26264                           TREE_TYPE (decl)
26265                             = do_auto_deduction (TREE_TYPE (decl), init,
26266                                                  auto_node);
26267
26268                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26269                               && !type_dependent_expression_p (decl))
26270                             goto non_class;
26271                         }
26272                       
26273                       cp_finish_decl (decl, init, !is_non_constant_init,
26274                                       asm_specification,
26275                                       LOOKUP_ONLYCONVERTING);
26276                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26277                         {
26278                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26279                           init = NULL_TREE;
26280                         }
26281                       else
26282                         init = pop_stmt_list (this_pre_body);
26283                       this_pre_body = NULL_TREE;
26284                     }
26285                   else
26286                     {
26287                       /* Consume '='.  */
26288                       cp_lexer_consume_token (parser->lexer);
26289                       init = cp_parser_assignment_expression (parser, false, NULL);
26290
26291                     non_class:
26292                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26293                         init = error_mark_node;
26294                       else
26295                         cp_finish_decl (decl, NULL_TREE,
26296                                         /*init_const_expr_p=*/false,
26297                                         asm_specification,
26298                                         LOOKUP_ONLYCONVERTING);
26299                     }
26300
26301                   if (pushed_scope)
26302                     pop_scope (pushed_scope);
26303                 }
26304             }
26305           else 
26306             {
26307               cp_id_kind idk;
26308               /* If parsing a type specifier sequence failed, then
26309                  this MUST be a simple expression.  */
26310               cp_parser_parse_tentatively (parser);
26311               decl = cp_parser_primary_expression (parser, false, false,
26312                                                    false, &idk);
26313               if (!cp_parser_error_occurred (parser)
26314                   && decl
26315                   && DECL_P (decl)
26316                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26317                 {
26318                   tree rhs;
26319
26320                   cp_parser_parse_definitely (parser);
26321                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26322                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26323                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26324                                                          rhs,
26325                                                          tf_warning_or_error));
26326                   add_private_clause = true;
26327                 }
26328               else
26329                 {
26330                   decl = NULL;
26331                   cp_parser_abort_tentative_parse (parser);
26332                   init = cp_parser_expression (parser, false, NULL);
26333                   if (init)
26334                     {
26335                       if (TREE_CODE (init) == MODIFY_EXPR
26336                           || TREE_CODE (init) == MODOP_EXPR)
26337                         real_decl = TREE_OPERAND (init, 0);
26338                     }
26339                 }
26340             }
26341         }
26342       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26343       if (this_pre_body)
26344         {
26345           this_pre_body = pop_stmt_list (this_pre_body);
26346           if (pre_body)
26347             {
26348               tree t = pre_body;
26349               pre_body = push_stmt_list ();
26350               add_stmt (t);
26351               add_stmt (this_pre_body);
26352               pre_body = pop_stmt_list (pre_body);
26353             }
26354           else
26355             pre_body = this_pre_body;
26356         }
26357
26358       if (decl)
26359         real_decl = decl;
26360       if (par_clauses != NULL && real_decl != NULL_TREE)
26361         {
26362           tree *c;
26363           for (c = par_clauses; *c ; )
26364             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26365                 && OMP_CLAUSE_DECL (*c) == real_decl)
26366               {
26367                 error_at (loc, "iteration variable %qD"
26368                           " should not be firstprivate", real_decl);
26369                 *c = OMP_CLAUSE_CHAIN (*c);
26370               }
26371             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26372                      && OMP_CLAUSE_DECL (*c) == real_decl)
26373               {
26374                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26375                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26376                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26377                 OMP_CLAUSE_DECL (l) = real_decl;
26378                 OMP_CLAUSE_CHAIN (l) = clauses;
26379                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26380                 clauses = l;
26381                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26382                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26383                 add_private_clause = false;
26384               }
26385             else
26386               {
26387                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26388                     && OMP_CLAUSE_DECL (*c) == real_decl)
26389                   add_private_clause = false;
26390                 c = &OMP_CLAUSE_CHAIN (*c);
26391               }
26392         }
26393
26394       if (add_private_clause)
26395         {
26396           tree c;
26397           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26398             {
26399               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26400                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26401                   && OMP_CLAUSE_DECL (c) == decl)
26402                 break;
26403               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26404                        && OMP_CLAUSE_DECL (c) == decl)
26405                 error_at (loc, "iteration variable %qD "
26406                           "should not be firstprivate",
26407                           decl);
26408               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26409                        && OMP_CLAUSE_DECL (c) == decl)
26410                 error_at (loc, "iteration variable %qD should not be reduction",
26411                           decl);
26412             }
26413           if (c == NULL)
26414             {
26415               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26416               OMP_CLAUSE_DECL (c) = decl;
26417               c = finish_omp_clauses (c);
26418               if (c)
26419                 {
26420                   OMP_CLAUSE_CHAIN (c) = clauses;
26421                   clauses = c;
26422                 }
26423             }
26424         }
26425
26426       cond = NULL;
26427       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26428         cond = cp_parser_omp_for_cond (parser, decl);
26429       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26430
26431       incr = NULL;
26432       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26433         {
26434           /* If decl is an iterator, preserve the operator on decl
26435              until finish_omp_for.  */
26436           if (real_decl
26437               && ((processing_template_decl
26438                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26439                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26440             incr = cp_parser_omp_for_incr (parser, real_decl);
26441           else
26442             incr = cp_parser_expression (parser, false, NULL);
26443         }
26444
26445       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26446         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26447                                                /*or_comma=*/false,
26448                                                /*consume_paren=*/true);
26449
26450       TREE_VEC_ELT (declv, i) = decl;
26451       TREE_VEC_ELT (initv, i) = init;
26452       TREE_VEC_ELT (condv, i) = cond;
26453       TREE_VEC_ELT (incrv, i) = incr;
26454
26455       if (i == collapse - 1)
26456         break;
26457
26458       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26459          in between the collapsed for loops to be still considered perfectly
26460          nested.  Hopefully the final version clarifies this.
26461          For now handle (multiple) {'s and empty statements.  */
26462       cp_parser_parse_tentatively (parser);
26463       do
26464         {
26465           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26466             break;
26467           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26468             {
26469               cp_lexer_consume_token (parser->lexer);
26470               bracecount++;
26471             }
26472           else if (bracecount
26473                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26474             cp_lexer_consume_token (parser->lexer);
26475           else
26476             {
26477               loc = cp_lexer_peek_token (parser->lexer)->location;
26478               error_at (loc, "not enough collapsed for loops");
26479               collapse_err = true;
26480               cp_parser_abort_tentative_parse (parser);
26481               declv = NULL_TREE;
26482               break;
26483             }
26484         }
26485       while (1);
26486
26487       if (declv)
26488         {
26489           cp_parser_parse_definitely (parser);
26490           nbraces += bracecount;
26491         }
26492     }
26493
26494   /* Note that we saved the original contents of this flag when we entered
26495      the structured block, and so we don't need to re-save it here.  */
26496   parser->in_statement = IN_OMP_FOR;
26497
26498   /* Note that the grammar doesn't call for a structured block here,
26499      though the loop as a whole is a structured block.  */
26500   body = push_stmt_list ();
26501   cp_parser_statement (parser, NULL_TREE, false, NULL);
26502   body = pop_stmt_list (body);
26503
26504   if (declv == NULL_TREE)
26505     ret = NULL_TREE;
26506   else
26507     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26508                           pre_body, clauses);
26509
26510   while (nbraces)
26511     {
26512       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26513         {
26514           cp_lexer_consume_token (parser->lexer);
26515           nbraces--;
26516         }
26517       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26518         cp_lexer_consume_token (parser->lexer);
26519       else
26520         {
26521           if (!collapse_err)
26522             {
26523               error_at (cp_lexer_peek_token (parser->lexer)->location,
26524                         "collapsed loops not perfectly nested");
26525             }
26526           collapse_err = true;
26527           cp_parser_statement_seq_opt (parser, NULL);
26528           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26529             break;
26530         }
26531     }
26532
26533   while (!VEC_empty (tree, for_block))
26534     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26535   release_tree_vector (for_block);
26536
26537   return ret;
26538 }
26539
26540 /* OpenMP 2.5:
26541    #pragma omp for for-clause[optseq] new-line
26542      for-loop  */
26543
26544 #define OMP_FOR_CLAUSE_MASK                             \
26545         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26546         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26547         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26548         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26549         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26550         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26551         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26552         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26553
26554 static tree
26555 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26556 {
26557   tree clauses, sb, ret;
26558   unsigned int save;
26559
26560   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26561                                        "#pragma omp for", pragma_tok);
26562
26563   sb = begin_omp_structured_block ();
26564   save = cp_parser_begin_omp_structured_block (parser);
26565
26566   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26567
26568   cp_parser_end_omp_structured_block (parser, save);
26569   add_stmt (finish_omp_structured_block (sb));
26570
26571   return ret;
26572 }
26573
26574 /* OpenMP 2.5:
26575    # pragma omp master new-line
26576      structured-block  */
26577
26578 static tree
26579 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26580 {
26581   cp_parser_require_pragma_eol (parser, pragma_tok);
26582   return c_finish_omp_master (input_location,
26583                               cp_parser_omp_structured_block (parser));
26584 }
26585
26586 /* OpenMP 2.5:
26587    # pragma omp ordered new-line
26588      structured-block  */
26589
26590 static tree
26591 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26592 {
26593   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26594   cp_parser_require_pragma_eol (parser, pragma_tok);
26595   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26596 }
26597
26598 /* OpenMP 2.5:
26599
26600    section-scope:
26601      { section-sequence }
26602
26603    section-sequence:
26604      section-directive[opt] structured-block
26605      section-sequence section-directive structured-block  */
26606
26607 static tree
26608 cp_parser_omp_sections_scope (cp_parser *parser)
26609 {
26610   tree stmt, substmt;
26611   bool error_suppress = false;
26612   cp_token *tok;
26613
26614   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26615     return NULL_TREE;
26616
26617   stmt = push_stmt_list ();
26618
26619   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26620     {
26621       unsigned save;
26622
26623       substmt = begin_omp_structured_block ();
26624       save = cp_parser_begin_omp_structured_block (parser);
26625
26626       while (1)
26627         {
26628           cp_parser_statement (parser, NULL_TREE, false, NULL);
26629
26630           tok = cp_lexer_peek_token (parser->lexer);
26631           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26632             break;
26633           if (tok->type == CPP_CLOSE_BRACE)
26634             break;
26635           if (tok->type == CPP_EOF)
26636             break;
26637         }
26638
26639       cp_parser_end_omp_structured_block (parser, save);
26640       substmt = finish_omp_structured_block (substmt);
26641       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26642       add_stmt (substmt);
26643     }
26644
26645   while (1)
26646     {
26647       tok = cp_lexer_peek_token (parser->lexer);
26648       if (tok->type == CPP_CLOSE_BRACE)
26649         break;
26650       if (tok->type == CPP_EOF)
26651         break;
26652
26653       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26654         {
26655           cp_lexer_consume_token (parser->lexer);
26656           cp_parser_require_pragma_eol (parser, tok);
26657           error_suppress = false;
26658         }
26659       else if (!error_suppress)
26660         {
26661           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26662           error_suppress = true;
26663         }
26664
26665       substmt = cp_parser_omp_structured_block (parser);
26666       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26667       add_stmt (substmt);
26668     }
26669   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26670
26671   substmt = pop_stmt_list (stmt);
26672
26673   stmt = make_node (OMP_SECTIONS);
26674   TREE_TYPE (stmt) = void_type_node;
26675   OMP_SECTIONS_BODY (stmt) = substmt;
26676
26677   add_stmt (stmt);
26678   return stmt;
26679 }
26680
26681 /* OpenMP 2.5:
26682    # pragma omp sections sections-clause[optseq] newline
26683      sections-scope  */
26684
26685 #define OMP_SECTIONS_CLAUSE_MASK                        \
26686         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26687         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26688         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26689         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26690         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26691
26692 static tree
26693 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26694 {
26695   tree clauses, ret;
26696
26697   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26698                                        "#pragma omp sections", pragma_tok);
26699
26700   ret = cp_parser_omp_sections_scope (parser);
26701   if (ret)
26702     OMP_SECTIONS_CLAUSES (ret) = clauses;
26703
26704   return ret;
26705 }
26706
26707 /* OpenMP 2.5:
26708    # pragma parallel parallel-clause new-line
26709    # pragma parallel for parallel-for-clause new-line
26710    # pragma parallel sections parallel-sections-clause new-line  */
26711
26712 #define OMP_PARALLEL_CLAUSE_MASK                        \
26713         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26714         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26715         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26716         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26717         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26718         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26719         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26720         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26721
26722 static tree
26723 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26724 {
26725   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26726   const char *p_name = "#pragma omp parallel";
26727   tree stmt, clauses, par_clause, ws_clause, block;
26728   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26729   unsigned int save;
26730   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26731
26732   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26733     {
26734       cp_lexer_consume_token (parser->lexer);
26735       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26736       p_name = "#pragma omp parallel for";
26737       mask |= OMP_FOR_CLAUSE_MASK;
26738       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26739     }
26740   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26741     {
26742       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26743       const char *p = IDENTIFIER_POINTER (id);
26744       if (strcmp (p, "sections") == 0)
26745         {
26746           cp_lexer_consume_token (parser->lexer);
26747           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26748           p_name = "#pragma omp parallel sections";
26749           mask |= OMP_SECTIONS_CLAUSE_MASK;
26750           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26751         }
26752     }
26753
26754   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26755   block = begin_omp_parallel ();
26756   save = cp_parser_begin_omp_structured_block (parser);
26757
26758   switch (p_kind)
26759     {
26760     case PRAGMA_OMP_PARALLEL:
26761       cp_parser_statement (parser, NULL_TREE, false, NULL);
26762       par_clause = clauses;
26763       break;
26764
26765     case PRAGMA_OMP_PARALLEL_FOR:
26766       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26767       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26768       break;
26769
26770     case PRAGMA_OMP_PARALLEL_SECTIONS:
26771       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26772       stmt = cp_parser_omp_sections_scope (parser);
26773       if (stmt)
26774         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26775       break;
26776
26777     default:
26778       gcc_unreachable ();
26779     }
26780
26781   cp_parser_end_omp_structured_block (parser, save);
26782   stmt = finish_omp_parallel (par_clause, block);
26783   if (p_kind != PRAGMA_OMP_PARALLEL)
26784     OMP_PARALLEL_COMBINED (stmt) = 1;
26785   return stmt;
26786 }
26787
26788 /* OpenMP 2.5:
26789    # pragma omp single single-clause[optseq] new-line
26790      structured-block  */
26791
26792 #define OMP_SINGLE_CLAUSE_MASK                          \
26793         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26794         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26795         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26796         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26797
26798 static tree
26799 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26800 {
26801   tree stmt = make_node (OMP_SINGLE);
26802   TREE_TYPE (stmt) = void_type_node;
26803
26804   OMP_SINGLE_CLAUSES (stmt)
26805     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26806                                  "#pragma omp single", pragma_tok);
26807   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26808
26809   return add_stmt (stmt);
26810 }
26811
26812 /* OpenMP 3.0:
26813    # pragma omp task task-clause[optseq] new-line
26814      structured-block  */
26815
26816 #define OMP_TASK_CLAUSE_MASK                            \
26817         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26818         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26819         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26820         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26821         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26822         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26823         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26824         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26825
26826 static tree
26827 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26828 {
26829   tree clauses, block;
26830   unsigned int save;
26831
26832   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26833                                        "#pragma omp task", pragma_tok);
26834   block = begin_omp_task ();
26835   save = cp_parser_begin_omp_structured_block (parser);
26836   cp_parser_statement (parser, NULL_TREE, false, NULL);
26837   cp_parser_end_omp_structured_block (parser, save);
26838   return finish_omp_task (clauses, block);
26839 }
26840
26841 /* OpenMP 3.0:
26842    # pragma omp taskwait new-line  */
26843
26844 static void
26845 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26846 {
26847   cp_parser_require_pragma_eol (parser, pragma_tok);
26848   finish_omp_taskwait ();
26849 }
26850
26851 /* OpenMP 3.1:
26852    # pragma omp taskyield new-line  */
26853
26854 static void
26855 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26856 {
26857   cp_parser_require_pragma_eol (parser, pragma_tok);
26858   finish_omp_taskyield ();
26859 }
26860
26861 /* OpenMP 2.5:
26862    # pragma omp threadprivate (variable-list) */
26863
26864 static void
26865 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26866 {
26867   tree vars;
26868
26869   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26870   cp_parser_require_pragma_eol (parser, pragma_tok);
26871
26872   finish_omp_threadprivate (vars);
26873 }
26874
26875 /* Main entry point to OpenMP statement pragmas.  */
26876
26877 static void
26878 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26879 {
26880   tree stmt;
26881
26882   switch (pragma_tok->pragma_kind)
26883     {
26884     case PRAGMA_OMP_ATOMIC:
26885       cp_parser_omp_atomic (parser, pragma_tok);
26886       return;
26887     case PRAGMA_OMP_CRITICAL:
26888       stmt = cp_parser_omp_critical (parser, pragma_tok);
26889       break;
26890     case PRAGMA_OMP_FOR:
26891       stmt = cp_parser_omp_for (parser, pragma_tok);
26892       break;
26893     case PRAGMA_OMP_MASTER:
26894       stmt = cp_parser_omp_master (parser, pragma_tok);
26895       break;
26896     case PRAGMA_OMP_ORDERED:
26897       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26898       break;
26899     case PRAGMA_OMP_PARALLEL:
26900       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26901       break;
26902     case PRAGMA_OMP_SECTIONS:
26903       stmt = cp_parser_omp_sections (parser, pragma_tok);
26904       break;
26905     case PRAGMA_OMP_SINGLE:
26906       stmt = cp_parser_omp_single (parser, pragma_tok);
26907       break;
26908     case PRAGMA_OMP_TASK:
26909       stmt = cp_parser_omp_task (parser, pragma_tok);
26910       break;
26911     default:
26912       gcc_unreachable ();
26913     }
26914
26915   if (stmt)
26916     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26917 }
26918 \f
26919 /* Transactional Memory parsing routines.  */
26920
26921 /* Parse a transaction attribute.
26922
26923    txn-attribute:
26924         attribute
26925         [ [ identifier ] ]
26926
26927    ??? Simplify this when C++0x bracket attributes are
26928    implemented properly.  */
26929
26930 static tree
26931 cp_parser_txn_attribute_opt (cp_parser *parser)
26932 {
26933   cp_token *token;
26934   tree attr_name, attr = NULL;
26935
26936   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26937     return cp_parser_attributes_opt (parser);
26938
26939   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26940     return NULL_TREE;
26941   cp_lexer_consume_token (parser->lexer);
26942   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26943     goto error1;
26944
26945   token = cp_lexer_peek_token (parser->lexer);
26946   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26947     {
26948       token = cp_lexer_consume_token (parser->lexer);
26949
26950       attr_name = (token->type == CPP_KEYWORD
26951                    /* For keywords, use the canonical spelling,
26952                       not the parsed identifier.  */
26953                    ? ridpointers[(int) token->keyword]
26954                    : token->u.value);
26955       attr = build_tree_list (attr_name, NULL_TREE);
26956     }
26957   else
26958     cp_parser_error (parser, "expected identifier");
26959
26960   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26961  error1:
26962   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26963   return attr;
26964 }
26965
26966 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26967
26968    transaction-statement:
26969      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
26970        compound-statement
26971      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
26972 */
26973
26974 static tree
26975 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26976 {
26977   unsigned char old_in = parser->in_transaction;
26978   unsigned char this_in = 1, new_in;
26979   cp_token *token;
26980   tree stmt, attrs, noex;
26981
26982   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26983       || keyword == RID_TRANSACTION_RELAXED);
26984   token = cp_parser_require_keyword (parser, keyword,
26985       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26986           : RT_TRANSACTION_RELAXED));
26987   gcc_assert (token != NULL);
26988
26989   if (keyword == RID_TRANSACTION_RELAXED)
26990     this_in |= TM_STMT_ATTR_RELAXED;
26991   else
26992     {
26993       attrs = cp_parser_txn_attribute_opt (parser);
26994       if (attrs)
26995         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26996     }
26997
26998   /* Parse a noexcept specification.  */
26999   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27000
27001   /* Keep track if we're in the lexical scope of an outer transaction.  */
27002   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27003
27004   stmt = begin_transaction_stmt (token->location, NULL, this_in);
27005
27006   parser->in_transaction = new_in;
27007   cp_parser_compound_statement (parser, NULL, false, false);
27008   parser->in_transaction = old_in;
27009
27010   finish_transaction_stmt (stmt, NULL, this_in, noex);
27011
27012   return stmt;
27013 }
27014
27015 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27016
27017    transaction-expression:
27018      __transaction_atomic txn-noexcept-spec[opt] ( expression )
27019      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27020 */
27021
27022 static tree
27023 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27024 {
27025   unsigned char old_in = parser->in_transaction;
27026   unsigned char this_in = 1;
27027   cp_token *token;
27028   tree expr, noex;
27029   bool noex_expr;
27030
27031   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27032       || keyword == RID_TRANSACTION_RELAXED);
27033
27034   if (!flag_tm)
27035     error (keyword == RID_TRANSACTION_RELAXED
27036            ? G_("%<__transaction_relaxed%> without transactional memory "
27037                 "support enabled")
27038            : G_("%<__transaction_atomic%> without transactional memory "
27039                 "support enabled"));
27040
27041   token = cp_parser_require_keyword (parser, keyword,
27042       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27043           : RT_TRANSACTION_RELAXED));
27044   gcc_assert (token != NULL);
27045
27046   if (keyword == RID_TRANSACTION_RELAXED)
27047     this_in |= TM_STMT_ATTR_RELAXED;
27048
27049   /* Set this early.  This might mean that we allow transaction_cancel in
27050      an expression that we find out later actually has to be a constexpr.
27051      However, we expect that cxx_constant_value will be able to deal with
27052      this; also, if the noexcept has no constexpr, then what we parse next
27053      really is a transaction's body.  */
27054   parser->in_transaction = this_in;
27055
27056   /* Parse a noexcept specification.  */
27057   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27058                                                true);
27059
27060   if (!noex || !noex_expr
27061       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27062     {
27063       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27064
27065       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27066       finish_parenthesized_expr (expr);
27067
27068       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27069     }
27070   else
27071     {
27072       /* The only expression that is available got parsed for the noexcept
27073          already.  noexcept is true then.  */
27074       expr = noex;
27075       noex = boolean_true_node;
27076     }
27077
27078   expr = build_transaction_expr (token->location, expr, this_in, noex);
27079   parser->in_transaction = old_in;
27080
27081   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27082     return error_mark_node;
27083
27084   return (flag_tm ? expr : error_mark_node);
27085 }
27086
27087 /* Parse a function-transaction-block.
27088
27089    function-transaction-block:
27090      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27091          function-body
27092      __transaction_atomic txn-attribute[opt] function-try-block
27093      __transaction_relaxed ctor-initializer[opt] function-body
27094      __transaction_relaxed function-try-block
27095 */
27096
27097 static bool
27098 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27099 {
27100   unsigned char old_in = parser->in_transaction;
27101   unsigned char new_in = 1;
27102   tree compound_stmt, stmt, attrs;
27103   bool ctor_initializer_p;
27104   cp_token *token;
27105
27106   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27107       || keyword == RID_TRANSACTION_RELAXED);
27108   token = cp_parser_require_keyword (parser, keyword,
27109       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27110           : RT_TRANSACTION_RELAXED));
27111   gcc_assert (token != NULL);
27112
27113   if (keyword == RID_TRANSACTION_RELAXED)
27114     new_in |= TM_STMT_ATTR_RELAXED;
27115   else
27116     {
27117       attrs = cp_parser_txn_attribute_opt (parser);
27118       if (attrs)
27119         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27120     }
27121
27122   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27123
27124   parser->in_transaction = new_in;
27125
27126   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27127     ctor_initializer_p = cp_parser_function_try_block (parser);
27128   else
27129     ctor_initializer_p
27130       = cp_parser_ctor_initializer_opt_and_function_body (parser);
27131
27132   parser->in_transaction = old_in;
27133
27134   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27135
27136   return ctor_initializer_p;
27137 }
27138
27139 /* Parse a __transaction_cancel statement.
27140
27141    cancel-statement:
27142      __transaction_cancel txn-attribute[opt] ;
27143      __transaction_cancel txn-attribute[opt] throw-expression ;
27144
27145    ??? Cancel and throw is not yet implemented.  */
27146
27147 static tree
27148 cp_parser_transaction_cancel (cp_parser *parser)
27149 {
27150   cp_token *token;
27151   bool is_outer = false;
27152   tree stmt, attrs;
27153
27154   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27155                                      RT_TRANSACTION_CANCEL);
27156   gcc_assert (token != NULL);
27157
27158   attrs = cp_parser_txn_attribute_opt (parser);
27159   if (attrs)
27160     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27161
27162   /* ??? Parse cancel-and-throw here.  */
27163
27164   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27165
27166   if (!flag_tm)
27167     {
27168       error_at (token->location, "%<__transaction_cancel%> without "
27169                 "transactional memory support enabled");
27170       return error_mark_node;
27171     }
27172   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27173     {
27174       error_at (token->location, "%<__transaction_cancel%> within a "
27175                 "%<__transaction_relaxed%>");
27176       return error_mark_node;
27177     }
27178   else if (is_outer)
27179     {
27180       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27181           && !is_tm_may_cancel_outer (current_function_decl))
27182         {
27183           error_at (token->location, "outer %<__transaction_cancel%> not "
27184                     "within outer %<__transaction_atomic%>");
27185           error_at (token->location,
27186                     "  or a %<transaction_may_cancel_outer%> function");
27187           return error_mark_node;
27188         }
27189     }
27190   else if (parser->in_transaction == 0)
27191     {
27192       error_at (token->location, "%<__transaction_cancel%> not within "
27193                 "%<__transaction_atomic%>");
27194       return error_mark_node;
27195     }
27196
27197   stmt = build_tm_abort_call (token->location, is_outer);
27198   add_stmt (stmt);
27199   finish_stmt ();
27200
27201   return stmt;
27202 }
27203 \f
27204 /* The parser.  */
27205
27206 static GTY (()) cp_parser *the_parser;
27207
27208 \f
27209 /* Special handling for the first token or line in the file.  The first
27210    thing in the file might be #pragma GCC pch_preprocess, which loads a
27211    PCH file, which is a GC collection point.  So we need to handle this
27212    first pragma without benefit of an existing lexer structure.
27213
27214    Always returns one token to the caller in *FIRST_TOKEN.  This is
27215    either the true first token of the file, or the first token after
27216    the initial pragma.  */
27217
27218 static void
27219 cp_parser_initial_pragma (cp_token *first_token)
27220 {
27221   tree name = NULL;
27222
27223   cp_lexer_get_preprocessor_token (NULL, first_token);
27224   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27225     return;
27226
27227   cp_lexer_get_preprocessor_token (NULL, first_token);
27228   if (first_token->type == CPP_STRING)
27229     {
27230       name = first_token->u.value;
27231
27232       cp_lexer_get_preprocessor_token (NULL, first_token);
27233       if (first_token->type != CPP_PRAGMA_EOL)
27234         error_at (first_token->location,
27235                   "junk at end of %<#pragma GCC pch_preprocess%>");
27236     }
27237   else
27238     error_at (first_token->location, "expected string literal");
27239
27240   /* Skip to the end of the pragma.  */
27241   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27242     cp_lexer_get_preprocessor_token (NULL, first_token);
27243
27244   /* Now actually load the PCH file.  */
27245   if (name)
27246     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27247
27248   /* Read one more token to return to our caller.  We have to do this
27249      after reading the PCH file in, since its pointers have to be
27250      live.  */
27251   cp_lexer_get_preprocessor_token (NULL, first_token);
27252 }
27253
27254 /* Normal parsing of a pragma token.  Here we can (and must) use the
27255    regular lexer.  */
27256
27257 static bool
27258 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27259 {
27260   cp_token *pragma_tok;
27261   unsigned int id;
27262
27263   pragma_tok = cp_lexer_consume_token (parser->lexer);
27264   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27265   parser->lexer->in_pragma = true;
27266
27267   id = pragma_tok->pragma_kind;
27268   switch (id)
27269     {
27270     case PRAGMA_GCC_PCH_PREPROCESS:
27271       error_at (pragma_tok->location,
27272                 "%<#pragma GCC pch_preprocess%> must be first");
27273       break;
27274
27275     case PRAGMA_OMP_BARRIER:
27276       switch (context)
27277         {
27278         case pragma_compound:
27279           cp_parser_omp_barrier (parser, pragma_tok);
27280           return false;
27281         case pragma_stmt:
27282           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27283                     "used in compound statements");
27284           break;
27285         default:
27286           goto bad_stmt;
27287         }
27288       break;
27289
27290     case PRAGMA_OMP_FLUSH:
27291       switch (context)
27292         {
27293         case pragma_compound:
27294           cp_parser_omp_flush (parser, pragma_tok);
27295           return false;
27296         case pragma_stmt:
27297           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27298                     "used in compound statements");
27299           break;
27300         default:
27301           goto bad_stmt;
27302         }
27303       break;
27304
27305     case PRAGMA_OMP_TASKWAIT:
27306       switch (context)
27307         {
27308         case pragma_compound:
27309           cp_parser_omp_taskwait (parser, pragma_tok);
27310           return false;
27311         case pragma_stmt:
27312           error_at (pragma_tok->location,
27313                     "%<#pragma omp taskwait%> may only be "
27314                     "used in compound statements");
27315           break;
27316         default:
27317           goto bad_stmt;
27318         }
27319       break;
27320
27321     case PRAGMA_OMP_TASKYIELD:
27322       switch (context)
27323         {
27324         case pragma_compound:
27325           cp_parser_omp_taskyield (parser, pragma_tok);
27326           return false;
27327         case pragma_stmt:
27328           error_at (pragma_tok->location,
27329                     "%<#pragma omp taskyield%> may only be "
27330                     "used in compound statements");
27331           break;
27332         default:
27333           goto bad_stmt;
27334         }
27335       break;
27336
27337     case PRAGMA_OMP_THREADPRIVATE:
27338       cp_parser_omp_threadprivate (parser, pragma_tok);
27339       return false;
27340
27341     case PRAGMA_OMP_ATOMIC:
27342     case PRAGMA_OMP_CRITICAL:
27343     case PRAGMA_OMP_FOR:
27344     case PRAGMA_OMP_MASTER:
27345     case PRAGMA_OMP_ORDERED:
27346     case PRAGMA_OMP_PARALLEL:
27347     case PRAGMA_OMP_SECTIONS:
27348     case PRAGMA_OMP_SINGLE:
27349     case PRAGMA_OMP_TASK:
27350       if (context == pragma_external)
27351         goto bad_stmt;
27352       cp_parser_omp_construct (parser, pragma_tok);
27353       return true;
27354
27355     case PRAGMA_OMP_SECTION:
27356       error_at (pragma_tok->location, 
27357                 "%<#pragma omp section%> may only be used in "
27358                 "%<#pragma omp sections%> construct");
27359       break;
27360
27361     default:
27362       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27363       c_invoke_pragma_handler (id);
27364       break;
27365
27366     bad_stmt:
27367       cp_parser_error (parser, "expected declaration specifiers");
27368       break;
27369     }
27370
27371   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27372   return false;
27373 }
27374
27375 /* The interface the pragma parsers have to the lexer.  */
27376
27377 enum cpp_ttype
27378 pragma_lex (tree *value)
27379 {
27380   cp_token *tok;
27381   enum cpp_ttype ret;
27382
27383   tok = cp_lexer_peek_token (the_parser->lexer);
27384
27385   ret = tok->type;
27386   *value = tok->u.value;
27387
27388   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27389     ret = CPP_EOF;
27390   else if (ret == CPP_STRING)
27391     *value = cp_parser_string_literal (the_parser, false, false);
27392   else
27393     {
27394       cp_lexer_consume_token (the_parser->lexer);
27395       if (ret == CPP_KEYWORD)
27396         ret = CPP_NAME;
27397     }
27398
27399   return ret;
27400 }
27401
27402 \f
27403 /* External interface.  */
27404
27405 /* Parse one entire translation unit.  */
27406
27407 void
27408 c_parse_file (void)
27409 {
27410   static bool already_called = false;
27411
27412   if (already_called)
27413     {
27414       sorry ("inter-module optimizations not implemented for C++");
27415       return;
27416     }
27417   already_called = true;
27418
27419   the_parser = cp_parser_new ();
27420   push_deferring_access_checks (flag_access_control
27421                                 ? dk_no_deferred : dk_no_check);
27422   cp_parser_translation_unit (the_parser);
27423   the_parser = NULL;
27424 }
27425
27426 #include "gt-cp-parser.h"