f0f7e987efb7352c6ec73c5425ec6bb63f2f2d9a
[platform/upstream/gcc.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011, 2012  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "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               && ix == VEC_length (tree, args)
3586               /* May be this should be sufficient_parms_p instead,
3587                  depending on how exactly should user-defined literals
3588                  work in presence of default arguments on the literal
3589                  operator parameters.  */
3590               && argtypes == void_list_node)
3591             return fn;
3592         }
3593     }
3594
3595   return error_mark_node;
3596 }
3597
3598 /* Parse a user-defined char constant.  Returns a call to a user-defined
3599    literal operator taking the character as an argument.  */
3600
3601 static tree
3602 cp_parser_userdef_char_literal (cp_parser *parser)
3603 {
3604   cp_token *token = cp_lexer_consume_token (parser->lexer);
3605   tree literal = token->u.value;
3606   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3607   tree value = USERDEF_LITERAL_VALUE (literal);
3608   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3609   tree decl, result;
3610
3611   /* Build up a call to the user-defined operator  */
3612   /* Lookup the name we got back from the id-expression.  */
3613   VEC(tree,gc) *args = make_tree_vector ();
3614   VEC_safe_push (tree, gc, args, value);
3615   decl = lookup_literal_operator (name, args);
3616   if (!decl || decl == error_mark_node)
3617     {
3618       error ("unable to find character literal operator %qD with %qT argument",
3619              name, TREE_TYPE (value));
3620       release_tree_vector (args);
3621       return error_mark_node;
3622     }
3623   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3624   release_tree_vector (args);
3625   if (result != error_mark_node)
3626     return result;
3627
3628   error ("unable to find character literal operator %qD with %qT argument",
3629          name, TREE_TYPE (value));
3630   return error_mark_node;
3631 }
3632
3633 /* A subroutine of cp_parser_userdef_numeric_literal to
3634    create a char... template parameter pack from a string node.  */
3635
3636 static tree
3637 make_char_string_pack (tree value)
3638 {
3639   tree charvec;
3640   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3641   const char *str = TREE_STRING_POINTER (value);
3642   int i, len = TREE_STRING_LENGTH (value) - 1;
3643   tree argvec = make_tree_vec (1);
3644
3645   /* Fill in CHARVEC with all of the parameters.  */
3646   charvec = make_tree_vec (len);
3647   for (i = 0; i < len; ++i)
3648     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3649
3650   /* Build the argument packs.  */
3651   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3652   TREE_TYPE (argpack) = char_type_node;
3653
3654   TREE_VEC_ELT (argvec, 0) = argpack;
3655
3656   return argvec;
3657 }
3658
3659 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3660    literal operator.  */
3661
3662 static tree
3663 cp_parser_userdef_numeric_literal (cp_parser *parser)
3664 {
3665   cp_token *token = cp_lexer_consume_token (parser->lexer);
3666   tree literal = token->u.value;
3667   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3668   tree value = USERDEF_LITERAL_VALUE (literal);
3669   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3670   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3671   tree decl, result;
3672   VEC(tree,gc) *args;
3673
3674   /* Look for a literal operator taking the exact type of numeric argument
3675      as the literal value.  */
3676   args = make_tree_vector ();
3677   VEC_safe_push (tree, gc, args, value);
3678   decl = lookup_literal_operator (name, args);
3679   if (decl && decl != error_mark_node)
3680     {
3681       result = finish_call_expr (decl, &args, false, true, tf_none);
3682       if (result != error_mark_node)
3683         {
3684           release_tree_vector (args);
3685           return result;
3686         }
3687     }
3688   release_tree_vector (args);
3689
3690   /* If the numeric argument didn't work, look for a raw literal
3691      operator taking a const char* argument consisting of the number
3692      in string format.  */
3693   args = make_tree_vector ();
3694   VEC_safe_push (tree, gc, args, num_string);
3695   decl = lookup_literal_operator (name, args);
3696   if (decl && decl != error_mark_node)
3697     {
3698       result = finish_call_expr (decl, &args, false, true, tf_none);
3699       if (result != error_mark_node)
3700         {
3701           release_tree_vector (args);
3702           return result;
3703         }
3704     }
3705   release_tree_vector (args);
3706
3707   /* If the raw literal didn't work, look for a non-type template
3708      function with parameter pack char....  Call the function with
3709      template parameter characters representing the number.  */
3710   args = make_tree_vector ();
3711   decl = lookup_literal_operator (name, args);
3712   if (decl && decl != error_mark_node)
3713     {
3714       tree tmpl_args = make_char_string_pack (num_string);
3715       decl = lookup_template_function (decl, tmpl_args);
3716       result = finish_call_expr (decl, &args, false, true, tf_none);
3717       if (result != error_mark_node)
3718         {
3719           release_tree_vector (args);
3720           return result;
3721         }
3722     }
3723   release_tree_vector (args);
3724
3725   error ("unable to find numeric literal operator %qD", name);
3726   return error_mark_node;
3727 }
3728
3729 /* Parse a user-defined string constant.  Returns a call to a user-defined
3730    literal operator taking a character pointer and the length of the string
3731    as arguments.  */
3732
3733 static tree
3734 cp_parser_userdef_string_literal (cp_token *token)
3735 {
3736   tree literal = token->u.value;
3737   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3738   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3739   tree value = USERDEF_LITERAL_VALUE (literal);
3740   int len = TREE_STRING_LENGTH (value)
3741         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3742   tree decl, result;
3743
3744   /* Build up a call to the user-defined operator  */
3745   /* Lookup the name we got back from the id-expression.  */
3746   VEC(tree,gc) *args = make_tree_vector ();
3747   VEC_safe_push (tree, gc, args, value);
3748   VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3749   decl = lookup_name (name);
3750   if (!decl || decl == error_mark_node)
3751     {
3752       error ("unable to find string literal operator %qD", name);
3753       release_tree_vector (args);
3754       return error_mark_node;
3755     }
3756   result = finish_call_expr (decl, &args, false, true, tf_none);
3757   release_tree_vector (args);
3758   if (result != error_mark_node)
3759     return result;
3760
3761   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3762          name, TREE_TYPE (value), size_type_node);
3763   return error_mark_node;
3764 }
3765
3766
3767 /* Basic concepts [gram.basic]  */
3768
3769 /* Parse a translation-unit.
3770
3771    translation-unit:
3772      declaration-seq [opt]
3773
3774    Returns TRUE if all went well.  */
3775
3776 static bool
3777 cp_parser_translation_unit (cp_parser* parser)
3778 {
3779   /* The address of the first non-permanent object on the declarator
3780      obstack.  */
3781   static void *declarator_obstack_base;
3782
3783   bool success;
3784
3785   /* Create the declarator obstack, if necessary.  */
3786   if (!cp_error_declarator)
3787     {
3788       gcc_obstack_init (&declarator_obstack);
3789       /* Create the error declarator.  */
3790       cp_error_declarator = make_declarator (cdk_error);
3791       /* Create the empty parameter list.  */
3792       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3793       /* Remember where the base of the declarator obstack lies.  */
3794       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3795     }
3796
3797   cp_parser_declaration_seq_opt (parser);
3798
3799   /* If there are no tokens left then all went well.  */
3800   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3801     {
3802       /* Get rid of the token array; we don't need it any more.  */
3803       cp_lexer_destroy (parser->lexer);
3804       parser->lexer = NULL;
3805
3806       /* This file might have been a context that's implicitly extern
3807          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3808       if (parser->implicit_extern_c)
3809         {
3810           pop_lang_context ();
3811           parser->implicit_extern_c = false;
3812         }
3813
3814       /* Finish up.  */
3815       finish_translation_unit ();
3816
3817       success = true;
3818     }
3819   else
3820     {
3821       cp_parser_error (parser, "expected declaration");
3822       success = false;
3823     }
3824
3825   /* Make sure the declarator obstack was fully cleaned up.  */
3826   gcc_assert (obstack_next_free (&declarator_obstack)
3827               == declarator_obstack_base);
3828
3829   /* All went well.  */
3830   return success;
3831 }
3832
3833 /* Expressions [gram.expr] */
3834
3835 /* Parse a primary-expression.
3836
3837    primary-expression:
3838      literal
3839      this
3840      ( expression )
3841      id-expression
3842
3843    GNU Extensions:
3844
3845    primary-expression:
3846      ( compound-statement )
3847      __builtin_va_arg ( assignment-expression , type-id )
3848      __builtin_offsetof ( type-id , offsetof-expression )
3849
3850    C++ Extensions:
3851      __has_nothrow_assign ( type-id )   
3852      __has_nothrow_constructor ( type-id )
3853      __has_nothrow_copy ( type-id )
3854      __has_trivial_assign ( type-id )   
3855      __has_trivial_constructor ( type-id )
3856      __has_trivial_copy ( type-id )
3857      __has_trivial_destructor ( type-id )
3858      __has_virtual_destructor ( type-id )     
3859      __is_abstract ( type-id )
3860      __is_base_of ( type-id , type-id )
3861      __is_class ( type-id )
3862      __is_convertible_to ( type-id , type-id )     
3863      __is_empty ( type-id )
3864      __is_enum ( type-id )
3865      __is_final ( type-id )
3866      __is_literal_type ( type-id )
3867      __is_pod ( type-id )
3868      __is_polymorphic ( type-id )
3869      __is_std_layout ( type-id )
3870      __is_trivial ( type-id )
3871      __is_union ( type-id )
3872
3873    Objective-C++ Extension:
3874
3875    primary-expression:
3876      objc-expression
3877
3878    literal:
3879      __null
3880
3881    ADDRESS_P is true iff this expression was immediately preceded by
3882    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3883    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3884    true iff this expression is a template argument.
3885
3886    Returns a representation of the expression.  Upon return, *IDK
3887    indicates what kind of id-expression (if any) was present.  */
3888
3889 static tree
3890 cp_parser_primary_expression (cp_parser *parser,
3891                               bool address_p,
3892                               bool cast_p,
3893                               bool template_arg_p,
3894                               cp_id_kind *idk)
3895 {
3896   cp_token *token = NULL;
3897
3898   /* Assume the primary expression is not an id-expression.  */
3899   *idk = CP_ID_KIND_NONE;
3900
3901   /* Peek at the next token.  */
3902   token = cp_lexer_peek_token (parser->lexer);
3903   switch (token->type)
3904     {
3905       /* literal:
3906            integer-literal
3907            character-literal
3908            floating-literal
3909            string-literal
3910            boolean-literal
3911            pointer-literal
3912            user-defined-literal  */
3913     case CPP_CHAR:
3914     case CPP_CHAR16:
3915     case CPP_CHAR32:
3916     case CPP_WCHAR:
3917     case CPP_NUMBER:
3918       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3919         return cp_parser_userdef_numeric_literal (parser);
3920       token = cp_lexer_consume_token (parser->lexer);
3921       if (TREE_CODE (token->u.value) == FIXED_CST)
3922         {
3923           error_at (token->location,
3924                     "fixed-point types not supported in C++");
3925           return error_mark_node;
3926         }
3927       /* Floating-point literals are only allowed in an integral
3928          constant expression if they are cast to an integral or
3929          enumeration type.  */
3930       if (TREE_CODE (token->u.value) == REAL_CST
3931           && parser->integral_constant_expression_p
3932           && pedantic)
3933         {
3934           /* CAST_P will be set even in invalid code like "int(2.7 +
3935              ...)".   Therefore, we have to check that the next token
3936              is sure to end the cast.  */
3937           if (cast_p)
3938             {
3939               cp_token *next_token;
3940
3941               next_token = cp_lexer_peek_token (parser->lexer);
3942               if (/* The comma at the end of an
3943                      enumerator-definition.  */
3944                   next_token->type != CPP_COMMA
3945                   /* The curly brace at the end of an enum-specifier.  */
3946                   && next_token->type != CPP_CLOSE_BRACE
3947                   /* The end of a statement.  */
3948                   && next_token->type != CPP_SEMICOLON
3949                   /* The end of the cast-expression.  */
3950                   && next_token->type != CPP_CLOSE_PAREN
3951                   /* The end of an array bound.  */
3952                   && next_token->type != CPP_CLOSE_SQUARE
3953                   /* The closing ">" in a template-argument-list.  */
3954                   && (next_token->type != CPP_GREATER
3955                       || parser->greater_than_is_operator_p)
3956                   /* C++0x only: A ">>" treated like two ">" tokens,
3957                      in a template-argument-list.  */
3958                   && (next_token->type != CPP_RSHIFT
3959                       || (cxx_dialect == cxx98)
3960                       || parser->greater_than_is_operator_p))
3961                 cast_p = false;
3962             }
3963
3964           /* If we are within a cast, then the constraint that the
3965              cast is to an integral or enumeration type will be
3966              checked at that point.  If we are not within a cast, then
3967              this code is invalid.  */
3968           if (!cast_p)
3969             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3970         }
3971       return token->u.value;
3972
3973     case CPP_CHAR_USERDEF:
3974     case CPP_CHAR16_USERDEF:
3975     case CPP_CHAR32_USERDEF:
3976     case CPP_WCHAR_USERDEF:
3977       return cp_parser_userdef_char_literal (parser);
3978
3979     case CPP_STRING:
3980     case CPP_STRING16:
3981     case CPP_STRING32:
3982     case CPP_WSTRING:
3983     case CPP_UTF8STRING:
3984     case CPP_STRING_USERDEF:
3985     case CPP_STRING16_USERDEF:
3986     case CPP_STRING32_USERDEF:
3987     case CPP_WSTRING_USERDEF:
3988     case CPP_UTF8STRING_USERDEF:
3989       /* ??? Should wide strings be allowed when parser->translate_strings_p
3990          is false (i.e. in attributes)?  If not, we can kill the third
3991          argument to cp_parser_string_literal.  */
3992       return cp_parser_string_literal (parser,
3993                                        parser->translate_strings_p,
3994                                        true);
3995
3996     case CPP_OPEN_PAREN:
3997       {
3998         tree expr;
3999         bool saved_greater_than_is_operator_p;
4000
4001         /* Consume the `('.  */
4002         cp_lexer_consume_token (parser->lexer);
4003         /* Within a parenthesized expression, a `>' token is always
4004            the greater-than operator.  */
4005         saved_greater_than_is_operator_p
4006           = parser->greater_than_is_operator_p;
4007         parser->greater_than_is_operator_p = true;
4008         /* If we see `( { ' then we are looking at the beginning of
4009            a GNU statement-expression.  */
4010         if (cp_parser_allow_gnu_extensions_p (parser)
4011             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4012           {
4013             /* Statement-expressions are not allowed by the standard.  */
4014             pedwarn (token->location, OPT_Wpedantic, 
4015                      "ISO C++ forbids braced-groups within expressions");
4016
4017             /* And they're not allowed outside of a function-body; you
4018                cannot, for example, write:
4019
4020                  int i = ({ int j = 3; j + 1; });
4021
4022                at class or namespace scope.  */
4023             if (!parser->in_function_body
4024                 || parser->in_template_argument_list_p)
4025               {
4026                 error_at (token->location,
4027                           "statement-expressions are not allowed outside "
4028                           "functions nor in template-argument lists");
4029                 cp_parser_skip_to_end_of_block_or_statement (parser);
4030                 expr = error_mark_node;
4031               }
4032             else
4033               {
4034                 /* Start the statement-expression.  */
4035                 expr = begin_stmt_expr ();
4036                 /* Parse the compound-statement.  */
4037                 cp_parser_compound_statement (parser, expr, false, false);
4038                 /* Finish up.  */
4039                 expr = finish_stmt_expr (expr, false);
4040               }
4041           }
4042         else
4043           {
4044             /* Parse the parenthesized expression.  */
4045             expr = cp_parser_expression (parser, cast_p, idk);
4046             /* Let the front end know that this expression was
4047                enclosed in parentheses. This matters in case, for
4048                example, the expression is of the form `A::B', since
4049                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4050                not.  */
4051             finish_parenthesized_expr (expr);
4052             /* DR 705: Wrapping an unqualified name in parentheses
4053                suppresses arg-dependent lookup.  We want to pass back
4054                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4055                (c++/37862), but none of the others.  */
4056             if (*idk != CP_ID_KIND_QUALIFIED)
4057               *idk = CP_ID_KIND_NONE;
4058           }
4059         /* The `>' token might be the end of a template-id or
4060            template-parameter-list now.  */
4061         parser->greater_than_is_operator_p
4062           = saved_greater_than_is_operator_p;
4063         /* Consume the `)'.  */
4064         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4065           cp_parser_skip_to_end_of_statement (parser);
4066
4067         return expr;
4068       }
4069
4070     case CPP_OPEN_SQUARE:
4071       if (c_dialect_objc ())
4072         /* We have an Objective-C++ message. */
4073         return cp_parser_objc_expression (parser);
4074       {
4075         tree lam = cp_parser_lambda_expression (parser);
4076         /* Don't warn about a failed tentative parse.  */
4077         if (cp_parser_error_occurred (parser))
4078           return error_mark_node;
4079         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4080         return lam;
4081       }
4082
4083     case CPP_OBJC_STRING:
4084       if (c_dialect_objc ())
4085         /* We have an Objective-C++ string literal. */
4086         return cp_parser_objc_expression (parser);
4087       cp_parser_error (parser, "expected primary-expression");
4088       return error_mark_node;
4089
4090     case CPP_KEYWORD:
4091       switch (token->keyword)
4092         {
4093           /* These two are the boolean literals.  */
4094         case RID_TRUE:
4095           cp_lexer_consume_token (parser->lexer);
4096           return boolean_true_node;
4097         case RID_FALSE:
4098           cp_lexer_consume_token (parser->lexer);
4099           return boolean_false_node;
4100
4101           /* The `__null' literal.  */
4102         case RID_NULL:
4103           cp_lexer_consume_token (parser->lexer);
4104           return null_node;
4105
4106           /* The `nullptr' literal.  */
4107         case RID_NULLPTR:
4108           cp_lexer_consume_token (parser->lexer);
4109           return nullptr_node;
4110
4111           /* Recognize the `this' keyword.  */
4112         case RID_THIS:
4113           cp_lexer_consume_token (parser->lexer);
4114           if (parser->local_variables_forbidden_p)
4115             {
4116               error_at (token->location,
4117                         "%<this%> may not be used in this context");
4118               return error_mark_node;
4119             }
4120           /* Pointers cannot appear in constant-expressions.  */
4121           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4122             return error_mark_node;
4123           return finish_this_expr ();
4124
4125           /* The `operator' keyword can be the beginning of an
4126              id-expression.  */
4127         case RID_OPERATOR:
4128           goto id_expression;
4129
4130         case RID_FUNCTION_NAME:
4131         case RID_PRETTY_FUNCTION_NAME:
4132         case RID_C99_FUNCTION_NAME:
4133           {
4134             non_integral_constant name;
4135
4136             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4137                __func__ are the names of variables -- but they are
4138                treated specially.  Therefore, they are handled here,
4139                rather than relying on the generic id-expression logic
4140                below.  Grammatically, these names are id-expressions.
4141
4142                Consume the token.  */
4143             token = cp_lexer_consume_token (parser->lexer);
4144
4145             switch (token->keyword)
4146               {
4147               case RID_FUNCTION_NAME:
4148                 name = NIC_FUNC_NAME;
4149                 break;
4150               case RID_PRETTY_FUNCTION_NAME:
4151                 name = NIC_PRETTY_FUNC;
4152                 break;
4153               case RID_C99_FUNCTION_NAME:
4154                 name = NIC_C99_FUNC;
4155                 break;
4156               default:
4157                 gcc_unreachable ();
4158               }
4159
4160             if (cp_parser_non_integral_constant_expression (parser, name))
4161               return error_mark_node;
4162
4163             /* Look up the name.  */
4164             return finish_fname (token->u.value);
4165           }
4166
4167         case RID_VA_ARG:
4168           {
4169             tree expression;
4170             tree type;
4171             source_location type_location;
4172
4173             /* The `__builtin_va_arg' construct is used to handle
4174                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4175             cp_lexer_consume_token (parser->lexer);
4176             /* Look for the opening `('.  */
4177             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4178             /* Now, parse the assignment-expression.  */
4179             expression = cp_parser_assignment_expression (parser,
4180                                                           /*cast_p=*/false, NULL);
4181             /* Look for the `,'.  */
4182             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4183             type_location = cp_lexer_peek_token (parser->lexer)->location;
4184             /* Parse the type-id.  */
4185             type = cp_parser_type_id (parser);
4186             /* Look for the closing `)'.  */
4187             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4188             /* Using `va_arg' in a constant-expression is not
4189                allowed.  */
4190             if (cp_parser_non_integral_constant_expression (parser,
4191                                                             NIC_VA_ARG))
4192               return error_mark_node;
4193             return build_x_va_arg (type_location, expression, type);
4194           }
4195
4196         case RID_OFFSETOF:
4197           return cp_parser_builtin_offsetof (parser);
4198
4199         case RID_HAS_NOTHROW_ASSIGN:
4200         case RID_HAS_NOTHROW_CONSTRUCTOR:
4201         case RID_HAS_NOTHROW_COPY:        
4202         case RID_HAS_TRIVIAL_ASSIGN:
4203         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4204         case RID_HAS_TRIVIAL_COPY:        
4205         case RID_HAS_TRIVIAL_DESTRUCTOR:
4206         case RID_HAS_VIRTUAL_DESTRUCTOR:
4207         case RID_IS_ABSTRACT:
4208         case RID_IS_BASE_OF:
4209         case RID_IS_CLASS:
4210         case RID_IS_CONVERTIBLE_TO:
4211         case RID_IS_EMPTY:
4212         case RID_IS_ENUM:
4213         case RID_IS_FINAL:
4214         case RID_IS_LITERAL_TYPE:
4215         case RID_IS_POD:
4216         case RID_IS_POLYMORPHIC:
4217         case RID_IS_STD_LAYOUT:
4218         case RID_IS_TRIVIAL:
4219         case RID_IS_UNION:
4220           return cp_parser_trait_expr (parser, token->keyword);
4221
4222         /* Objective-C++ expressions.  */
4223         case RID_AT_ENCODE:
4224         case RID_AT_PROTOCOL:
4225         case RID_AT_SELECTOR:
4226           return cp_parser_objc_expression (parser);
4227
4228         case RID_TEMPLATE:
4229           if (parser->in_function_body
4230               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4231                   == CPP_LESS))
4232             {
4233               error_at (token->location,
4234                         "a template declaration cannot appear at block scope");
4235               cp_parser_skip_to_end_of_block_or_statement (parser);
4236               return error_mark_node;
4237             }
4238         default:
4239           cp_parser_error (parser, "expected primary-expression");
4240           return error_mark_node;
4241         }
4242
4243       /* An id-expression can start with either an identifier, a
4244          `::' as the beginning of a qualified-id, or the "operator"
4245          keyword.  */
4246     case CPP_NAME:
4247     case CPP_SCOPE:
4248     case CPP_TEMPLATE_ID:
4249     case CPP_NESTED_NAME_SPECIFIER:
4250       {
4251         tree id_expression;
4252         tree decl;
4253         const char *error_msg;
4254         bool template_p;
4255         bool done;
4256         cp_token *id_expr_token;
4257
4258       id_expression:
4259         /* Parse the id-expression.  */
4260         id_expression
4261           = cp_parser_id_expression (parser,
4262                                      /*template_keyword_p=*/false,
4263                                      /*check_dependency_p=*/true,
4264                                      &template_p,
4265                                      /*declarator_p=*/false,
4266                                      /*optional_p=*/false);
4267         if (id_expression == error_mark_node)
4268           return error_mark_node;
4269         id_expr_token = token;
4270         token = cp_lexer_peek_token (parser->lexer);
4271         done = (token->type != CPP_OPEN_SQUARE
4272                 && token->type != CPP_OPEN_PAREN
4273                 && token->type != CPP_DOT
4274                 && token->type != CPP_DEREF
4275                 && token->type != CPP_PLUS_PLUS
4276                 && token->type != CPP_MINUS_MINUS);
4277         /* If we have a template-id, then no further lookup is
4278            required.  If the template-id was for a template-class, we
4279            will sometimes have a TYPE_DECL at this point.  */
4280         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4281                  || TREE_CODE (id_expression) == TYPE_DECL)
4282           decl = id_expression;
4283         /* Look up the name.  */
4284         else
4285           {
4286             tree ambiguous_decls;
4287
4288             /* If we already know that this lookup is ambiguous, then
4289                we've already issued an error message; there's no reason
4290                to check again.  */
4291             if (id_expr_token->type == CPP_NAME
4292                 && id_expr_token->ambiguous_p)
4293               {
4294                 cp_parser_simulate_error (parser);
4295                 return error_mark_node;
4296               }
4297
4298             decl = cp_parser_lookup_name (parser, id_expression,
4299                                           none_type,
4300                                           template_p,
4301                                           /*is_namespace=*/false,
4302                                           /*check_dependency=*/true,
4303                                           &ambiguous_decls,
4304                                           id_expr_token->location);
4305             /* If the lookup was ambiguous, an error will already have
4306                been issued.  */
4307             if (ambiguous_decls)
4308               return error_mark_node;
4309
4310             /* In Objective-C++, we may have an Objective-C 2.0
4311                dot-syntax for classes here.  */
4312             if (c_dialect_objc ()
4313                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4314                 && TREE_CODE (decl) == TYPE_DECL
4315                 && objc_is_class_name (decl))
4316               {
4317                 tree component;
4318                 cp_lexer_consume_token (parser->lexer);
4319                 component = cp_parser_identifier (parser);
4320                 if (component == error_mark_node)
4321                   return error_mark_node;
4322
4323                 return objc_build_class_component_ref (id_expression, component);
4324               }
4325
4326             /* In Objective-C++, an instance variable (ivar) may be preferred
4327                to whatever cp_parser_lookup_name() found.  */
4328             decl = objc_lookup_ivar (decl, id_expression);
4329
4330             /* If name lookup gives us a SCOPE_REF, then the
4331                qualifying scope was dependent.  */
4332             if (TREE_CODE (decl) == SCOPE_REF)
4333               {
4334                 /* At this point, we do not know if DECL is a valid
4335                    integral constant expression.  We assume that it is
4336                    in fact such an expression, so that code like:
4337
4338                       template <int N> struct A {
4339                         int a[B<N>::i];
4340                       };
4341                      
4342                    is accepted.  At template-instantiation time, we
4343                    will check that B<N>::i is actually a constant.  */
4344                 return decl;
4345               }
4346             /* Check to see if DECL is a local variable in a context
4347                where that is forbidden.  */
4348             if (parser->local_variables_forbidden_p
4349                 && local_variable_p (decl))
4350               {
4351                 /* It might be that we only found DECL because we are
4352                    trying to be generous with pre-ISO scoping rules.
4353                    For example, consider:
4354
4355                      int i;
4356                      void g() {
4357                        for (int i = 0; i < 10; ++i) {}
4358                        extern void f(int j = i);
4359                      }
4360
4361                    Here, name look up will originally find the out
4362                    of scope `i'.  We need to issue a warning message,
4363                    but then use the global `i'.  */
4364                 decl = check_for_out_of_scope_variable (decl);
4365                 if (local_variable_p (decl))
4366                   {
4367                     error_at (id_expr_token->location,
4368                               "local variable %qD may not appear in this context",
4369                               decl);
4370                     return error_mark_node;
4371                   }
4372               }
4373           }
4374
4375         decl = (finish_id_expression
4376                 (id_expression, decl, parser->scope,
4377                  idk,
4378                  parser->integral_constant_expression_p,
4379                  parser->allow_non_integral_constant_expression_p,
4380                  &parser->non_integral_constant_expression_p,
4381                  template_p, done, address_p,
4382                  template_arg_p,
4383                  &error_msg,
4384                  id_expr_token->location));
4385         if (error_msg)
4386           cp_parser_error (parser, error_msg);
4387         return decl;
4388       }
4389
4390       /* Anything else is an error.  */
4391     default:
4392       cp_parser_error (parser, "expected primary-expression");
4393       return error_mark_node;
4394     }
4395 }
4396
4397 /* Parse an id-expression.
4398
4399    id-expression:
4400      unqualified-id
4401      qualified-id
4402
4403    qualified-id:
4404      :: [opt] nested-name-specifier template [opt] unqualified-id
4405      :: identifier
4406      :: operator-function-id
4407      :: template-id
4408
4409    Return a representation of the unqualified portion of the
4410    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4411    a `::' or nested-name-specifier.
4412
4413    Often, if the id-expression was a qualified-id, the caller will
4414    want to make a SCOPE_REF to represent the qualified-id.  This
4415    function does not do this in order to avoid wastefully creating
4416    SCOPE_REFs when they are not required.
4417
4418    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4419    `template' keyword.
4420
4421    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4422    uninstantiated templates.
4423
4424    If *TEMPLATE_P is non-NULL, it is set to true iff the
4425    `template' keyword is used to explicitly indicate that the entity
4426    named is a template.
4427
4428    If DECLARATOR_P is true, the id-expression is appearing as part of
4429    a declarator, rather than as part of an expression.  */
4430
4431 static tree
4432 cp_parser_id_expression (cp_parser *parser,
4433                          bool template_keyword_p,
4434                          bool check_dependency_p,
4435                          bool *template_p,
4436                          bool declarator_p,
4437                          bool optional_p)
4438 {
4439   bool global_scope_p;
4440   bool nested_name_specifier_p;
4441
4442   /* Assume the `template' keyword was not used.  */
4443   if (template_p)
4444     *template_p = template_keyword_p;
4445
4446   /* Look for the optional `::' operator.  */
4447   global_scope_p
4448     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4449        != NULL_TREE);
4450   /* Look for the optional nested-name-specifier.  */
4451   nested_name_specifier_p
4452     = (cp_parser_nested_name_specifier_opt (parser,
4453                                             /*typename_keyword_p=*/false,
4454                                             check_dependency_p,
4455                                             /*type_p=*/false,
4456                                             declarator_p)
4457        != NULL_TREE);
4458   /* If there is a nested-name-specifier, then we are looking at
4459      the first qualified-id production.  */
4460   if (nested_name_specifier_p)
4461     {
4462       tree saved_scope;
4463       tree saved_object_scope;
4464       tree saved_qualifying_scope;
4465       tree unqualified_id;
4466       bool is_template;
4467
4468       /* See if the next token is the `template' keyword.  */
4469       if (!template_p)
4470         template_p = &is_template;
4471       *template_p = cp_parser_optional_template_keyword (parser);
4472       /* Name lookup we do during the processing of the
4473          unqualified-id might obliterate SCOPE.  */
4474       saved_scope = parser->scope;
4475       saved_object_scope = parser->object_scope;
4476       saved_qualifying_scope = parser->qualifying_scope;
4477       /* Process the final unqualified-id.  */
4478       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4479                                                  check_dependency_p,
4480                                                  declarator_p,
4481                                                  /*optional_p=*/false);
4482       /* Restore the SAVED_SCOPE for our caller.  */
4483       parser->scope = saved_scope;
4484       parser->object_scope = saved_object_scope;
4485       parser->qualifying_scope = saved_qualifying_scope;
4486
4487       return unqualified_id;
4488     }
4489   /* Otherwise, if we are in global scope, then we are looking at one
4490      of the other qualified-id productions.  */
4491   else if (global_scope_p)
4492     {
4493       cp_token *token;
4494       tree id;
4495
4496       /* Peek at the next token.  */
4497       token = cp_lexer_peek_token (parser->lexer);
4498
4499       /* If it's an identifier, and the next token is not a "<", then
4500          we can avoid the template-id case.  This is an optimization
4501          for this common case.  */
4502       if (token->type == CPP_NAME
4503           && !cp_parser_nth_token_starts_template_argument_list_p
4504                (parser, 2))
4505         return cp_parser_identifier (parser);
4506
4507       cp_parser_parse_tentatively (parser);
4508       /* Try a template-id.  */
4509       id = cp_parser_template_id (parser,
4510                                   /*template_keyword_p=*/false,
4511                                   /*check_dependency_p=*/true,
4512                                   declarator_p);
4513       /* If that worked, we're done.  */
4514       if (cp_parser_parse_definitely (parser))
4515         return id;
4516
4517       /* Peek at the next token.  (Changes in the token buffer may
4518          have invalidated the pointer obtained above.)  */
4519       token = cp_lexer_peek_token (parser->lexer);
4520
4521       switch (token->type)
4522         {
4523         case CPP_NAME:
4524           return cp_parser_identifier (parser);
4525
4526         case CPP_KEYWORD:
4527           if (token->keyword == RID_OPERATOR)
4528             return cp_parser_operator_function_id (parser);
4529           /* Fall through.  */
4530
4531         default:
4532           cp_parser_error (parser, "expected id-expression");
4533           return error_mark_node;
4534         }
4535     }
4536   else
4537     return cp_parser_unqualified_id (parser, template_keyword_p,
4538                                      /*check_dependency_p=*/true,
4539                                      declarator_p,
4540                                      optional_p);
4541 }
4542
4543 /* Parse an unqualified-id.
4544
4545    unqualified-id:
4546      identifier
4547      operator-function-id
4548      conversion-function-id
4549      ~ class-name
4550      template-id
4551
4552    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4553    keyword, in a construct like `A::template ...'.
4554
4555    Returns a representation of unqualified-id.  For the `identifier'
4556    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4557    production a BIT_NOT_EXPR is returned; the operand of the
4558    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4559    other productions, see the documentation accompanying the
4560    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4561    names are looked up in uninstantiated templates.  If DECLARATOR_P
4562    is true, the unqualified-id is appearing as part of a declarator,
4563    rather than as part of an expression.  */
4564
4565 static tree
4566 cp_parser_unqualified_id (cp_parser* parser,
4567                           bool template_keyword_p,
4568                           bool check_dependency_p,
4569                           bool declarator_p,
4570                           bool optional_p)
4571 {
4572   cp_token *token;
4573
4574   /* Peek at the next token.  */
4575   token = cp_lexer_peek_token (parser->lexer);
4576
4577   switch (token->type)
4578     {
4579     case CPP_NAME:
4580       {
4581         tree id;
4582
4583         /* We don't know yet whether or not this will be a
4584            template-id.  */
4585         cp_parser_parse_tentatively (parser);
4586         /* Try a template-id.  */
4587         id = cp_parser_template_id (parser, template_keyword_p,
4588                                     check_dependency_p,
4589                                     declarator_p);
4590         /* If it worked, we're done.  */
4591         if (cp_parser_parse_definitely (parser))
4592           return id;
4593         /* Otherwise, it's an ordinary identifier.  */
4594         return cp_parser_identifier (parser);
4595       }
4596
4597     case CPP_TEMPLATE_ID:
4598       return cp_parser_template_id (parser, template_keyword_p,
4599                                     check_dependency_p,
4600                                     declarator_p);
4601
4602     case CPP_COMPL:
4603       {
4604         tree type_decl;
4605         tree qualifying_scope;
4606         tree object_scope;
4607         tree scope;
4608         bool done;
4609
4610         /* Consume the `~' token.  */
4611         cp_lexer_consume_token (parser->lexer);
4612         /* Parse the class-name.  The standard, as written, seems to
4613            say that:
4614
4615              template <typename T> struct S { ~S (); };
4616              template <typename T> S<T>::~S() {}
4617
4618            is invalid, since `~' must be followed by a class-name, but
4619            `S<T>' is dependent, and so not known to be a class.
4620            That's not right; we need to look in uninstantiated
4621            templates.  A further complication arises from:
4622
4623              template <typename T> void f(T t) {
4624                t.T::~T();
4625              }
4626
4627            Here, it is not possible to look up `T' in the scope of `T'
4628            itself.  We must look in both the current scope, and the
4629            scope of the containing complete expression.
4630
4631            Yet another issue is:
4632
4633              struct S {
4634                int S;
4635                ~S();
4636              };
4637
4638              S::~S() {}
4639
4640            The standard does not seem to say that the `S' in `~S'
4641            should refer to the type `S' and not the data member
4642            `S::S'.  */
4643
4644         /* DR 244 says that we look up the name after the "~" in the
4645            same scope as we looked up the qualifying name.  That idea
4646            isn't fully worked out; it's more complicated than that.  */
4647         scope = parser->scope;
4648         object_scope = parser->object_scope;
4649         qualifying_scope = parser->qualifying_scope;
4650
4651         /* Check for invalid scopes.  */
4652         if (scope == error_mark_node)
4653           {
4654             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4655               cp_lexer_consume_token (parser->lexer);
4656             return error_mark_node;
4657           }
4658         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4659           {
4660             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4661               error_at (token->location,
4662                         "scope %qT before %<~%> is not a class-name",
4663                         scope);
4664             cp_parser_simulate_error (parser);
4665             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4666               cp_lexer_consume_token (parser->lexer);
4667             return error_mark_node;
4668           }
4669         gcc_assert (!scope || TYPE_P (scope));
4670
4671         /* If the name is of the form "X::~X" it's OK even if X is a
4672            typedef.  */
4673         token = cp_lexer_peek_token (parser->lexer);
4674         if (scope
4675             && token->type == CPP_NAME
4676             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4677                 != CPP_LESS)
4678             && (token->u.value == TYPE_IDENTIFIER (scope)
4679                 || (CLASS_TYPE_P (scope)
4680                     && constructor_name_p (token->u.value, scope))))
4681           {
4682             cp_lexer_consume_token (parser->lexer);
4683             return build_nt (BIT_NOT_EXPR, scope);
4684           }
4685
4686         /* If there was an explicit qualification (S::~T), first look
4687            in the scope given by the qualification (i.e., S).
4688
4689            Note: in the calls to cp_parser_class_name below we pass
4690            typename_type so that lookup finds the injected-class-name
4691            rather than the constructor.  */
4692         done = false;
4693         type_decl = NULL_TREE;
4694         if (scope)
4695           {
4696             cp_parser_parse_tentatively (parser);
4697             type_decl = cp_parser_class_name (parser,
4698                                               /*typename_keyword_p=*/false,
4699                                               /*template_keyword_p=*/false,
4700                                               typename_type,
4701                                               /*check_dependency=*/false,
4702                                               /*class_head_p=*/false,
4703                                               declarator_p);
4704             if (cp_parser_parse_definitely (parser))
4705               done = true;
4706           }
4707         /* In "N::S::~S", look in "N" as well.  */
4708         if (!done && scope && qualifying_scope)
4709           {
4710             cp_parser_parse_tentatively (parser);
4711             parser->scope = qualifying_scope;
4712             parser->object_scope = NULL_TREE;
4713             parser->qualifying_scope = NULL_TREE;
4714             type_decl
4715               = cp_parser_class_name (parser,
4716                                       /*typename_keyword_p=*/false,
4717                                       /*template_keyword_p=*/false,
4718                                       typename_type,
4719                                       /*check_dependency=*/false,
4720                                       /*class_head_p=*/false,
4721                                       declarator_p);
4722             if (cp_parser_parse_definitely (parser))
4723               done = true;
4724           }
4725         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4726         else if (!done && object_scope)
4727           {
4728             cp_parser_parse_tentatively (parser);
4729             parser->scope = object_scope;
4730             parser->object_scope = NULL_TREE;
4731             parser->qualifying_scope = NULL_TREE;
4732             type_decl
4733               = cp_parser_class_name (parser,
4734                                       /*typename_keyword_p=*/false,
4735                                       /*template_keyword_p=*/false,
4736                                       typename_type,
4737                                       /*check_dependency=*/false,
4738                                       /*class_head_p=*/false,
4739                                       declarator_p);
4740             if (cp_parser_parse_definitely (parser))
4741               done = true;
4742           }
4743         /* Look in the surrounding context.  */
4744         if (!done)
4745           {
4746             parser->scope = NULL_TREE;
4747             parser->object_scope = NULL_TREE;
4748             parser->qualifying_scope = NULL_TREE;
4749             if (processing_template_decl)
4750               cp_parser_parse_tentatively (parser);
4751             type_decl
4752               = cp_parser_class_name (parser,
4753                                       /*typename_keyword_p=*/false,
4754                                       /*template_keyword_p=*/false,
4755                                       typename_type,
4756                                       /*check_dependency=*/false,
4757                                       /*class_head_p=*/false,
4758                                       declarator_p);
4759             if (processing_template_decl
4760                 && ! cp_parser_parse_definitely (parser))
4761               {
4762                 /* We couldn't find a type with this name, so just accept
4763                    it and check for a match at instantiation time.  */
4764                 type_decl = cp_parser_identifier (parser);
4765                 if (type_decl != error_mark_node)
4766                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4767                 return type_decl;
4768               }
4769           }
4770         /* If an error occurred, assume that the name of the
4771            destructor is the same as the name of the qualifying
4772            class.  That allows us to keep parsing after running
4773            into ill-formed destructor names.  */
4774         if (type_decl == error_mark_node && scope)
4775           return build_nt (BIT_NOT_EXPR, scope);
4776         else if (type_decl == error_mark_node)
4777           return error_mark_node;
4778
4779         /* Check that destructor name and scope match.  */
4780         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4781           {
4782             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4783               error_at (token->location,
4784                         "declaration of %<~%T%> as member of %qT",
4785                         type_decl, scope);
4786             cp_parser_simulate_error (parser);
4787             return error_mark_node;
4788           }
4789
4790         /* [class.dtor]
4791
4792            A typedef-name that names a class shall not be used as the
4793            identifier in the declarator for a destructor declaration.  */
4794         if (declarator_p
4795             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4796             && !DECL_SELF_REFERENCE_P (type_decl)
4797             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4798           error_at (token->location,
4799                     "typedef-name %qD used as destructor declarator",
4800                     type_decl);
4801
4802         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4803       }
4804
4805     case CPP_KEYWORD:
4806       if (token->keyword == RID_OPERATOR)
4807         {
4808           tree id;
4809
4810           /* This could be a template-id, so we try that first.  */
4811           cp_parser_parse_tentatively (parser);
4812           /* Try a template-id.  */
4813           id = cp_parser_template_id (parser, template_keyword_p,
4814                                       /*check_dependency_p=*/true,
4815                                       declarator_p);
4816           /* If that worked, we're done.  */
4817           if (cp_parser_parse_definitely (parser))
4818             return id;
4819           /* We still don't know whether we're looking at an
4820              operator-function-id or a conversion-function-id.  */
4821           cp_parser_parse_tentatively (parser);
4822           /* Try an operator-function-id.  */
4823           id = cp_parser_operator_function_id (parser);
4824           /* If that didn't work, try a conversion-function-id.  */
4825           if (!cp_parser_parse_definitely (parser))
4826             id = cp_parser_conversion_function_id (parser);
4827           else if (UDLIT_OPER_P (id))
4828             {
4829               /* 17.6.3.3.5  */
4830               const char *name = UDLIT_OP_SUFFIX (id);
4831               if (name[0] != '_' && !in_system_header)
4832                 warning (0, "literal operator suffixes not preceded by %<_%>"
4833                             " are reserved for future standardization");
4834             }
4835
4836           return id;
4837         }
4838       /* Fall through.  */
4839
4840     default:
4841       if (optional_p)
4842         return NULL_TREE;
4843       cp_parser_error (parser, "expected unqualified-id");
4844       return error_mark_node;
4845     }
4846 }
4847
4848 /* Parse an (optional) nested-name-specifier.
4849
4850    nested-name-specifier: [C++98]
4851      class-or-namespace-name :: nested-name-specifier [opt]
4852      class-or-namespace-name :: template nested-name-specifier [opt]
4853
4854    nested-name-specifier: [C++0x]
4855      type-name ::
4856      namespace-name ::
4857      nested-name-specifier identifier ::
4858      nested-name-specifier template [opt] simple-template-id ::
4859
4860    PARSER->SCOPE should be set appropriately before this function is
4861    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4862    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4863    in name lookups.
4864
4865    Sets PARSER->SCOPE to the class (TYPE) or namespace
4866    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4867    it unchanged if there is no nested-name-specifier.  Returns the new
4868    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4869
4870    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4871    part of a declaration and/or decl-specifier.  */
4872
4873 static tree
4874 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4875                                      bool typename_keyword_p,
4876                                      bool check_dependency_p,
4877                                      bool type_p,
4878                                      bool is_declaration)
4879 {
4880   bool success = false;
4881   cp_token_position start = 0;
4882   cp_token *token;
4883
4884   /* Remember where the nested-name-specifier starts.  */
4885   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4886     {
4887       start = cp_lexer_token_position (parser->lexer, false);
4888       push_deferring_access_checks (dk_deferred);
4889     }
4890
4891   while (true)
4892     {
4893       tree new_scope;
4894       tree old_scope;
4895       tree saved_qualifying_scope;
4896       bool template_keyword_p;
4897
4898       /* Spot cases that cannot be the beginning of a
4899          nested-name-specifier.  */
4900       token = cp_lexer_peek_token (parser->lexer);
4901
4902       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4903          the already parsed nested-name-specifier.  */
4904       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4905         {
4906           /* Grab the nested-name-specifier and continue the loop.  */
4907           cp_parser_pre_parsed_nested_name_specifier (parser);
4908           /* If we originally encountered this nested-name-specifier
4909              with IS_DECLARATION set to false, we will not have
4910              resolved TYPENAME_TYPEs, so we must do so here.  */
4911           if (is_declaration
4912               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4913             {
4914               new_scope = resolve_typename_type (parser->scope,
4915                                                  /*only_current_p=*/false);
4916               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4917                 parser->scope = new_scope;
4918             }
4919           success = true;
4920           continue;
4921         }
4922
4923       /* Spot cases that cannot be the beginning of a
4924          nested-name-specifier.  On the second and subsequent times
4925          through the loop, we look for the `template' keyword.  */
4926       if (success && token->keyword == RID_TEMPLATE)
4927         ;
4928       /* A template-id can start a nested-name-specifier.  */
4929       else if (token->type == CPP_TEMPLATE_ID)
4930         ;
4931       /* DR 743: decltype can be used in a nested-name-specifier.  */
4932       else if (token_is_decltype (token))
4933         ;
4934       else
4935         {
4936           /* If the next token is not an identifier, then it is
4937              definitely not a type-name or namespace-name.  */
4938           if (token->type != CPP_NAME)
4939             break;
4940           /* If the following token is neither a `<' (to begin a
4941              template-id), nor a `::', then we are not looking at a
4942              nested-name-specifier.  */
4943           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4944
4945           if (token->type == CPP_COLON
4946               && parser->colon_corrects_to_scope_p
4947               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4948             {
4949               error_at (token->location,
4950                         "found %<:%> in nested-name-specifier, expected %<::%>");
4951               token->type = CPP_SCOPE;
4952             }
4953
4954           if (token->type != CPP_SCOPE
4955               && !cp_parser_nth_token_starts_template_argument_list_p
4956                   (parser, 2))
4957             break;
4958         }
4959
4960       /* The nested-name-specifier is optional, so we parse
4961          tentatively.  */
4962       cp_parser_parse_tentatively (parser);
4963
4964       /* Look for the optional `template' keyword, if this isn't the
4965          first time through the loop.  */
4966       if (success)
4967         template_keyword_p = cp_parser_optional_template_keyword (parser);
4968       else
4969         template_keyword_p = false;
4970
4971       /* Save the old scope since the name lookup we are about to do
4972          might destroy it.  */
4973       old_scope = parser->scope;
4974       saved_qualifying_scope = parser->qualifying_scope;
4975       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4976          look up names in "X<T>::I" in order to determine that "Y" is
4977          a template.  So, if we have a typename at this point, we make
4978          an effort to look through it.  */
4979       if (is_declaration
4980           && !typename_keyword_p
4981           && parser->scope
4982           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4983         parser->scope = resolve_typename_type (parser->scope,
4984                                                /*only_current_p=*/false);
4985       /* Parse the qualifying entity.  */
4986       new_scope
4987         = cp_parser_qualifying_entity (parser,
4988                                        typename_keyword_p,
4989                                        template_keyword_p,
4990                                        check_dependency_p,
4991                                        type_p,
4992                                        is_declaration);
4993       /* Look for the `::' token.  */
4994       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4995
4996       /* If we found what we wanted, we keep going; otherwise, we're
4997          done.  */
4998       if (!cp_parser_parse_definitely (parser))
4999         {
5000           bool error_p = false;
5001
5002           /* Restore the OLD_SCOPE since it was valid before the
5003              failed attempt at finding the last
5004              class-or-namespace-name.  */
5005           parser->scope = old_scope;
5006           parser->qualifying_scope = saved_qualifying_scope;
5007
5008           /* If the next token is a decltype, and the one after that is a
5009              `::', then the decltype has failed to resolve to a class or
5010              enumeration type.  Give this error even when parsing
5011              tentatively since it can't possibly be valid--and we're going
5012              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5013              won't get another chance.*/
5014           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5015               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5016                   == CPP_SCOPE))
5017             {
5018               token = cp_lexer_consume_token (parser->lexer);
5019               error_at (token->location, "decltype evaluates to %qT, "
5020                         "which is not a class or enumeration type",
5021                         token->u.value);
5022               parser->scope = error_mark_node;
5023               error_p = true;
5024               /* As below.  */
5025               success = true;
5026               cp_lexer_consume_token (parser->lexer);
5027             }
5028
5029           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5030             break;
5031           /* If the next token is an identifier, and the one after
5032              that is a `::', then any valid interpretation would have
5033              found a class-or-namespace-name.  */
5034           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5035                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5036                      == CPP_SCOPE)
5037                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5038                      != CPP_COMPL))
5039             {
5040               token = cp_lexer_consume_token (parser->lexer);
5041               if (!error_p)
5042                 {
5043                   if (!token->ambiguous_p)
5044                     {
5045                       tree decl;
5046                       tree ambiguous_decls;
5047
5048                       decl = cp_parser_lookup_name (parser, token->u.value,
5049                                                     none_type,
5050                                                     /*is_template=*/false,
5051                                                     /*is_namespace=*/false,
5052                                                     /*check_dependency=*/true,
5053                                                     &ambiguous_decls,
5054                                                     token->location);
5055                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5056                         error_at (token->location,
5057                                   "%qD used without template parameters",
5058                                   decl);
5059                       else if (ambiguous_decls)
5060                         {
5061                           error_at (token->location,
5062                                     "reference to %qD is ambiguous",
5063                                     token->u.value);
5064                           print_candidates (ambiguous_decls);
5065                           decl = error_mark_node;
5066                         }
5067                       else
5068                         {
5069                           if (cxx_dialect != cxx98)
5070                             cp_parser_name_lookup_error
5071                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5072                              token->location);
5073                           else
5074                             cp_parser_name_lookup_error
5075                             (parser, token->u.value, decl, NLE_CXX98,
5076                              token->location);
5077                         }
5078                     }
5079                   parser->scope = error_mark_node;
5080                   error_p = true;
5081                   /* Treat this as a successful nested-name-specifier
5082                      due to:
5083
5084                      [basic.lookup.qual]
5085
5086                      If the name found is not a class-name (clause
5087                      _class_) or namespace-name (_namespace.def_), the
5088                      program is ill-formed.  */
5089                   success = true;
5090                 }
5091               cp_lexer_consume_token (parser->lexer);
5092             }
5093           break;
5094         }
5095       /* We've found one valid nested-name-specifier.  */
5096       success = true;
5097       /* Name lookup always gives us a DECL.  */
5098       if (TREE_CODE (new_scope) == TYPE_DECL)
5099         new_scope = TREE_TYPE (new_scope);
5100       /* Uses of "template" must be followed by actual templates.  */
5101       if (template_keyword_p
5102           && !(CLASS_TYPE_P (new_scope)
5103                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5104                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5105                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5106           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5107                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5108                    == TEMPLATE_ID_EXPR)))
5109         permerror (input_location, TYPE_P (new_scope)
5110                    ? G_("%qT is not a template")
5111                    : G_("%qD is not a template"),
5112                    new_scope);
5113       /* If it is a class scope, try to complete it; we are about to
5114          be looking up names inside the class.  */
5115       if (TYPE_P (new_scope)
5116           /* Since checking types for dependency can be expensive,
5117              avoid doing it if the type is already complete.  */
5118           && !COMPLETE_TYPE_P (new_scope)
5119           /* Do not try to complete dependent types.  */
5120           && !dependent_type_p (new_scope))
5121         {
5122           new_scope = complete_type (new_scope);
5123           /* If it is a typedef to current class, use the current
5124              class instead, as the typedef won't have any names inside
5125              it yet.  */
5126           if (!COMPLETE_TYPE_P (new_scope)
5127               && currently_open_class (new_scope))
5128             new_scope = TYPE_MAIN_VARIANT (new_scope);
5129         }
5130       /* Make sure we look in the right scope the next time through
5131          the loop.  */
5132       parser->scope = new_scope;
5133     }
5134
5135   /* If parsing tentatively, replace the sequence of tokens that makes
5136      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5137      token.  That way, should we re-parse the token stream, we will
5138      not have to repeat the effort required to do the parse, nor will
5139      we issue duplicate error messages.  */
5140   if (success && start)
5141     {
5142       cp_token *token;
5143
5144       token = cp_lexer_token_at (parser->lexer, start);
5145       /* Reset the contents of the START token.  */
5146       token->type = CPP_NESTED_NAME_SPECIFIER;
5147       /* Retrieve any deferred checks.  Do not pop this access checks yet
5148          so the memory will not be reclaimed during token replacing below.  */
5149       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5150       token->u.tree_check_value->value = parser->scope;
5151       token->u.tree_check_value->checks = get_deferred_access_checks ();
5152       token->u.tree_check_value->qualifying_scope =
5153         parser->qualifying_scope;
5154       token->keyword = RID_MAX;
5155
5156       /* Purge all subsequent tokens.  */
5157       cp_lexer_purge_tokens_after (parser->lexer, start);
5158     }
5159
5160   if (start)
5161     pop_to_parent_deferring_access_checks ();
5162
5163   return success ? parser->scope : NULL_TREE;
5164 }
5165
5166 /* Parse a nested-name-specifier.  See
5167    cp_parser_nested_name_specifier_opt for details.  This function
5168    behaves identically, except that it will an issue an error if no
5169    nested-name-specifier is present.  */
5170
5171 static tree
5172 cp_parser_nested_name_specifier (cp_parser *parser,
5173                                  bool typename_keyword_p,
5174                                  bool check_dependency_p,
5175                                  bool type_p,
5176                                  bool is_declaration)
5177 {
5178   tree scope;
5179
5180   /* Look for the nested-name-specifier.  */
5181   scope = cp_parser_nested_name_specifier_opt (parser,
5182                                                typename_keyword_p,
5183                                                check_dependency_p,
5184                                                type_p,
5185                                                is_declaration);
5186   /* If it was not present, issue an error message.  */
5187   if (!scope)
5188     {
5189       cp_parser_error (parser, "expected nested-name-specifier");
5190       parser->scope = NULL_TREE;
5191     }
5192
5193   return scope;
5194 }
5195
5196 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5197    this is either a class-name or a namespace-name (which corresponds
5198    to the class-or-namespace-name production in the grammar). For
5199    C++0x, it can also be a type-name that refers to an enumeration
5200    type or a simple-template-id.
5201
5202    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5203    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5204    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5205    TYPE_P is TRUE iff the next name should be taken as a class-name,
5206    even the same name is declared to be another entity in the same
5207    scope.
5208
5209    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5210    specified by the class-or-namespace-name.  If neither is found the
5211    ERROR_MARK_NODE is returned.  */
5212
5213 static tree
5214 cp_parser_qualifying_entity (cp_parser *parser,
5215                              bool typename_keyword_p,
5216                              bool template_keyword_p,
5217                              bool check_dependency_p,
5218                              bool type_p,
5219                              bool is_declaration)
5220 {
5221   tree saved_scope;
5222   tree saved_qualifying_scope;
5223   tree saved_object_scope;
5224   tree scope;
5225   bool only_class_p;
5226   bool successful_parse_p;
5227
5228   /* DR 743: decltype can appear in a nested-name-specifier.  */
5229   if (cp_lexer_next_token_is_decltype (parser->lexer))
5230     {
5231       scope = cp_parser_decltype (parser);
5232       if (TREE_CODE (scope) != ENUMERAL_TYPE
5233           && !MAYBE_CLASS_TYPE_P (scope))
5234         {
5235           cp_parser_simulate_error (parser);
5236           return error_mark_node;
5237         }
5238       if (TYPE_NAME (scope))
5239         scope = TYPE_NAME (scope);
5240       return scope;
5241     }
5242
5243   /* Before we try to parse the class-name, we must save away the
5244      current PARSER->SCOPE since cp_parser_class_name will destroy
5245      it.  */
5246   saved_scope = parser->scope;
5247   saved_qualifying_scope = parser->qualifying_scope;
5248   saved_object_scope = parser->object_scope;
5249   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5250      there is no need to look for a namespace-name.  */
5251   only_class_p = template_keyword_p 
5252     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5253   if (!only_class_p)
5254     cp_parser_parse_tentatively (parser);
5255   scope = cp_parser_class_name (parser,
5256                                 typename_keyword_p,
5257                                 template_keyword_p,
5258                                 type_p ? class_type : none_type,
5259                                 check_dependency_p,
5260                                 /*class_head_p=*/false,
5261                                 is_declaration);
5262   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5263   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5264   if (!only_class_p 
5265       && cxx_dialect != cxx98
5266       && !successful_parse_p)
5267     {
5268       /* Restore the saved scope.  */
5269       parser->scope = saved_scope;
5270       parser->qualifying_scope = saved_qualifying_scope;
5271       parser->object_scope = saved_object_scope;
5272
5273       /* Parse tentatively.  */
5274       cp_parser_parse_tentatively (parser);
5275      
5276       /* Parse a type-name  */
5277       scope = cp_parser_type_name (parser);
5278
5279       /* "If the name found does not designate a namespace or a class,
5280          enumeration, or dependent type, the program is ill-formed."
5281
5282          We cover classes and dependent types above and namespaces below,
5283          so this code is only looking for enums.  */
5284       if (!scope || TREE_CODE (scope) != TYPE_DECL
5285           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5286         cp_parser_simulate_error (parser);
5287
5288       successful_parse_p = cp_parser_parse_definitely (parser);
5289     }
5290   /* If that didn't work, try for a namespace-name.  */
5291   if (!only_class_p && !successful_parse_p)
5292     {
5293       /* Restore the saved scope.  */
5294       parser->scope = saved_scope;
5295       parser->qualifying_scope = saved_qualifying_scope;
5296       parser->object_scope = saved_object_scope;
5297       /* If we are not looking at an identifier followed by the scope
5298          resolution operator, then this is not part of a
5299          nested-name-specifier.  (Note that this function is only used
5300          to parse the components of a nested-name-specifier.)  */
5301       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5302           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5303         return error_mark_node;
5304       scope = cp_parser_namespace_name (parser);
5305     }
5306
5307   return scope;
5308 }
5309
5310 /* Parse a postfix-expression.
5311
5312    postfix-expression:
5313      primary-expression
5314      postfix-expression [ expression ]
5315      postfix-expression ( expression-list [opt] )
5316      simple-type-specifier ( expression-list [opt] )
5317      typename :: [opt] nested-name-specifier identifier
5318        ( expression-list [opt] )
5319      typename :: [opt] nested-name-specifier template [opt] template-id
5320        ( expression-list [opt] )
5321      postfix-expression . template [opt] id-expression
5322      postfix-expression -> template [opt] id-expression
5323      postfix-expression . pseudo-destructor-name
5324      postfix-expression -> pseudo-destructor-name
5325      postfix-expression ++
5326      postfix-expression --
5327      dynamic_cast < type-id > ( expression )
5328      static_cast < type-id > ( expression )
5329      reinterpret_cast < type-id > ( expression )
5330      const_cast < type-id > ( expression )
5331      typeid ( expression )
5332      typeid ( type-id )
5333
5334    GNU Extension:
5335
5336    postfix-expression:
5337      ( type-id ) { initializer-list , [opt] }
5338
5339    This extension is a GNU version of the C99 compound-literal
5340    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5341    but they are essentially the same concept.)
5342
5343    If ADDRESS_P is true, the postfix expression is the operand of the
5344    `&' operator.  CAST_P is true if this expression is the target of a
5345    cast.
5346
5347    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5348    class member access expressions [expr.ref].
5349
5350    Returns a representation of the expression.  */
5351
5352 static tree
5353 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5354                               bool member_access_only_p,
5355                               cp_id_kind * pidk_return)
5356 {
5357   cp_token *token;
5358   enum rid keyword;
5359   cp_id_kind idk = CP_ID_KIND_NONE;
5360   tree postfix_expression = NULL_TREE;
5361   bool is_member_access = false;
5362
5363   /* Peek at the next token.  */
5364   token = cp_lexer_peek_token (parser->lexer);
5365   /* Some of the productions are determined by keywords.  */
5366   keyword = token->keyword;
5367   switch (keyword)
5368     {
5369     case RID_DYNCAST:
5370     case RID_STATCAST:
5371     case RID_REINTCAST:
5372     case RID_CONSTCAST:
5373       {
5374         tree type;
5375         tree expression;
5376         const char *saved_message;
5377
5378         /* All of these can be handled in the same way from the point
5379            of view of parsing.  Begin by consuming the token
5380            identifying the cast.  */
5381         cp_lexer_consume_token (parser->lexer);
5382
5383         /* New types cannot be defined in the cast.  */
5384         saved_message = parser->type_definition_forbidden_message;
5385         parser->type_definition_forbidden_message
5386           = G_("types may not be defined in casts");
5387
5388         /* Look for the opening `<'.  */
5389         cp_parser_require (parser, CPP_LESS, RT_LESS);
5390         /* Parse the type to which we are casting.  */
5391         type = cp_parser_type_id (parser);
5392         /* Look for the closing `>'.  */
5393         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5394         /* Restore the old message.  */
5395         parser->type_definition_forbidden_message = saved_message;
5396
5397         /* And the expression which is being cast.  */
5398         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5399         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5400         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5401
5402         /* Only type conversions to integral or enumeration types
5403            can be used in constant-expressions.  */
5404         if (!cast_valid_in_integral_constant_expression_p (type)
5405             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5406           return error_mark_node;
5407
5408         switch (keyword)
5409           {
5410           case RID_DYNCAST:
5411             postfix_expression
5412               = build_dynamic_cast (type, expression, tf_warning_or_error);
5413             break;
5414           case RID_STATCAST:
5415             postfix_expression
5416               = build_static_cast (type, expression, tf_warning_or_error);
5417             break;
5418           case RID_REINTCAST:
5419             postfix_expression
5420               = build_reinterpret_cast (type, expression, 
5421                                         tf_warning_or_error);
5422             break;
5423           case RID_CONSTCAST:
5424             postfix_expression
5425               = build_const_cast (type, expression, tf_warning_or_error);
5426             break;
5427           default:
5428             gcc_unreachable ();
5429           }
5430       }
5431       break;
5432
5433     case RID_TYPEID:
5434       {
5435         tree type;
5436         const char *saved_message;
5437         bool saved_in_type_id_in_expr_p;
5438
5439         /* Consume the `typeid' token.  */
5440         cp_lexer_consume_token (parser->lexer);
5441         /* Look for the `(' token.  */
5442         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5443         /* Types cannot be defined in a `typeid' expression.  */
5444         saved_message = parser->type_definition_forbidden_message;
5445         parser->type_definition_forbidden_message
5446           = G_("types may not be defined in a %<typeid%> expression");
5447         /* We can't be sure yet whether we're looking at a type-id or an
5448            expression.  */
5449         cp_parser_parse_tentatively (parser);
5450         /* Try a type-id first.  */
5451         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5452         parser->in_type_id_in_expr_p = true;
5453         type = cp_parser_type_id (parser);
5454         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5455         /* Look for the `)' token.  Otherwise, we can't be sure that
5456            we're not looking at an expression: consider `typeid (int
5457            (3))', for example.  */
5458         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5459         /* If all went well, simply lookup the type-id.  */
5460         if (cp_parser_parse_definitely (parser))
5461           postfix_expression = get_typeid (type);
5462         /* Otherwise, fall back to the expression variant.  */
5463         else
5464           {
5465             tree expression;
5466
5467             /* Look for an expression.  */
5468             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5469             /* Compute its typeid.  */
5470             postfix_expression = build_typeid (expression);
5471             /* Look for the `)' token.  */
5472             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5473           }
5474         /* Restore the saved message.  */
5475         parser->type_definition_forbidden_message = saved_message;
5476         /* `typeid' may not appear in an integral constant expression.  */
5477         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5478           return error_mark_node;
5479       }
5480       break;
5481
5482     case RID_TYPENAME:
5483       {
5484         tree type;
5485         /* The syntax permitted here is the same permitted for an
5486            elaborated-type-specifier.  */
5487         type = cp_parser_elaborated_type_specifier (parser,
5488                                                     /*is_friend=*/false,
5489                                                     /*is_declaration=*/false);
5490         postfix_expression = cp_parser_functional_cast (parser, type);
5491       }
5492       break;
5493
5494     default:
5495       {
5496         tree type;
5497
5498         /* If the next thing is a simple-type-specifier, we may be
5499            looking at a functional cast.  We could also be looking at
5500            an id-expression.  So, we try the functional cast, and if
5501            that doesn't work we fall back to the primary-expression.  */
5502         cp_parser_parse_tentatively (parser);
5503         /* Look for the simple-type-specifier.  */
5504         type = cp_parser_simple_type_specifier (parser,
5505                                                 /*decl_specs=*/NULL,
5506                                                 CP_PARSER_FLAGS_NONE);
5507         /* Parse the cast itself.  */
5508         if (!cp_parser_error_occurred (parser))
5509           postfix_expression
5510             = cp_parser_functional_cast (parser, type);
5511         /* If that worked, we're done.  */
5512         if (cp_parser_parse_definitely (parser))
5513           break;
5514
5515         /* If the functional-cast didn't work out, try a
5516            compound-literal.  */
5517         if (cp_parser_allow_gnu_extensions_p (parser)
5518             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5519           {
5520             VEC(constructor_elt,gc) *initializer_list = NULL;
5521             bool saved_in_type_id_in_expr_p;
5522
5523             cp_parser_parse_tentatively (parser);
5524             /* Consume the `('.  */
5525             cp_lexer_consume_token (parser->lexer);
5526             /* Parse the type.  */
5527             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5528             parser->in_type_id_in_expr_p = true;
5529             type = cp_parser_type_id (parser);
5530             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5531             /* Look for the `)'.  */
5532             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5533             /* Look for the `{'.  */
5534             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5535             /* If things aren't going well, there's no need to
5536                keep going.  */
5537             if (!cp_parser_error_occurred (parser))
5538               {
5539                 bool non_constant_p;
5540                 /* Parse the initializer-list.  */
5541                 initializer_list
5542                   = cp_parser_initializer_list (parser, &non_constant_p);
5543                 /* Allow a trailing `,'.  */
5544                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5545                   cp_lexer_consume_token (parser->lexer);
5546                 /* Look for the final `}'.  */
5547                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5548               }
5549             /* If that worked, we're definitely looking at a
5550                compound-literal expression.  */
5551             if (cp_parser_parse_definitely (parser))
5552               {
5553                 /* Warn the user that a compound literal is not
5554                    allowed in standard C++.  */
5555                 pedwarn (input_location, OPT_Wpedantic, "ISO C++ forbids compound-literals");
5556                 /* For simplicity, we disallow compound literals in
5557                    constant-expressions.  We could
5558                    allow compound literals of integer type, whose
5559                    initializer was a constant, in constant
5560                    expressions.  Permitting that usage, as a further
5561                    extension, would not change the meaning of any
5562                    currently accepted programs.  (Of course, as
5563                    compound literals are not part of ISO C++, the
5564                    standard has nothing to say.)  */
5565                 if (cp_parser_non_integral_constant_expression (parser,
5566                                                                 NIC_NCC))
5567                   {
5568                     postfix_expression = error_mark_node;
5569                     break;
5570                   }
5571                 /* Form the representation of the compound-literal.  */
5572                 postfix_expression
5573                   = (finish_compound_literal
5574                      (type, build_constructor (init_list_type_node,
5575                                                initializer_list),
5576                       tf_warning_or_error));
5577                 break;
5578               }
5579           }
5580
5581         /* It must be a primary-expression.  */
5582         postfix_expression
5583           = cp_parser_primary_expression (parser, address_p, cast_p,
5584                                           /*template_arg_p=*/false,
5585                                           &idk);
5586       }
5587       break;
5588     }
5589
5590   /* Keep looping until the postfix-expression is complete.  */
5591   while (true)
5592     {
5593       if (idk == CP_ID_KIND_UNQUALIFIED
5594           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5595           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5596         /* It is not a Koenig lookup function call.  */
5597         postfix_expression
5598           = unqualified_name_lookup_error (postfix_expression);
5599
5600       /* Peek at the next token.  */
5601       token = cp_lexer_peek_token (parser->lexer);
5602
5603       switch (token->type)
5604         {
5605         case CPP_OPEN_SQUARE:
5606           postfix_expression
5607             = cp_parser_postfix_open_square_expression (parser,
5608                                                         postfix_expression,
5609                                                         false);
5610           idk = CP_ID_KIND_NONE;
5611           is_member_access = false;
5612           break;
5613
5614         case CPP_OPEN_PAREN:
5615           /* postfix-expression ( expression-list [opt] ) */
5616           {
5617             bool koenig_p;
5618             bool is_builtin_constant_p;
5619             bool saved_integral_constant_expression_p = false;
5620             bool saved_non_integral_constant_expression_p = false;
5621             VEC(tree,gc) *args;
5622
5623             is_member_access = false;
5624
5625             is_builtin_constant_p
5626               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5627             if (is_builtin_constant_p)
5628               {
5629                 /* The whole point of __builtin_constant_p is to allow
5630                    non-constant expressions to appear as arguments.  */
5631                 saved_integral_constant_expression_p
5632                   = parser->integral_constant_expression_p;
5633                 saved_non_integral_constant_expression_p
5634                   = parser->non_integral_constant_expression_p;
5635                 parser->integral_constant_expression_p = false;
5636               }
5637             args = (cp_parser_parenthesized_expression_list
5638                     (parser, non_attr,
5639                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5640                      /*non_constant_p=*/NULL));
5641             if (is_builtin_constant_p)
5642               {
5643                 parser->integral_constant_expression_p
5644                   = saved_integral_constant_expression_p;
5645                 parser->non_integral_constant_expression_p
5646                   = saved_non_integral_constant_expression_p;
5647               }
5648
5649             if (args == NULL)
5650               {
5651                 postfix_expression = error_mark_node;
5652                 break;
5653               }
5654
5655             /* Function calls are not permitted in
5656                constant-expressions.  */
5657             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5658                 && cp_parser_non_integral_constant_expression (parser,
5659                                                                NIC_FUNC_CALL))
5660               {
5661                 postfix_expression = error_mark_node;
5662                 release_tree_vector (args);
5663                 break;
5664               }
5665
5666             koenig_p = false;
5667             if (idk == CP_ID_KIND_UNQUALIFIED
5668                 || idk == CP_ID_KIND_TEMPLATE_ID)
5669               {
5670                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5671                   {
5672                     if (!VEC_empty (tree, args))
5673                       {
5674                         koenig_p = true;
5675                         if (!any_type_dependent_arguments_p (args))
5676                           postfix_expression
5677                             = perform_koenig_lookup (postfix_expression, args,
5678                                                      /*include_std=*/false,
5679                                                      tf_warning_or_error);
5680                       }
5681                     else
5682                       postfix_expression
5683                         = unqualified_fn_lookup_error (postfix_expression);
5684                   }
5685                 /* We do not perform argument-dependent lookup if
5686                    normal lookup finds a non-function, in accordance
5687                    with the expected resolution of DR 218.  */
5688                 else if (!VEC_empty (tree, args)
5689                          && is_overloaded_fn (postfix_expression))
5690                   {
5691                     tree fn = get_first_fn (postfix_expression);
5692                     fn = STRIP_TEMPLATE (fn);
5693
5694                     /* Do not do argument dependent lookup if regular
5695                        lookup finds a member function or a block-scope
5696                        function declaration.  [basic.lookup.argdep]/3  */
5697                     if (!DECL_FUNCTION_MEMBER_P (fn)
5698                         && !DECL_LOCAL_FUNCTION_P (fn))
5699                       {
5700                         koenig_p = true;
5701                         if (!any_type_dependent_arguments_p (args))
5702                           postfix_expression
5703                             = perform_koenig_lookup (postfix_expression, args,
5704                                                      /*include_std=*/false,
5705                                                      tf_warning_or_error);
5706                       }
5707                   }
5708               }
5709
5710             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5711               {
5712                 tree instance = TREE_OPERAND (postfix_expression, 0);
5713                 tree fn = TREE_OPERAND (postfix_expression, 1);
5714
5715                 if (processing_template_decl
5716                     && (type_dependent_expression_p (instance)
5717                         || (!BASELINK_P (fn)
5718                             && TREE_CODE (fn) != FIELD_DECL)
5719                         || type_dependent_expression_p (fn)
5720                         || any_type_dependent_arguments_p (args)))
5721                   {
5722                     postfix_expression
5723                       = build_nt_call_vec (postfix_expression, args);
5724                     release_tree_vector (args);
5725                     break;
5726                   }
5727
5728                 if (BASELINK_P (fn))
5729                   {
5730                   postfix_expression
5731                     = (build_new_method_call
5732                        (instance, fn, &args, NULL_TREE,
5733                         (idk == CP_ID_KIND_QUALIFIED
5734                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5735                          : LOOKUP_NORMAL),
5736                         /*fn_p=*/NULL,
5737                         tf_warning_or_error));
5738                   }
5739                 else
5740                   postfix_expression
5741                     = finish_call_expr (postfix_expression, &args,
5742                                         /*disallow_virtual=*/false,
5743                                         /*koenig_p=*/false,
5744                                         tf_warning_or_error);
5745               }
5746             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5747                      || TREE_CODE (postfix_expression) == MEMBER_REF
5748                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5749               postfix_expression = (build_offset_ref_call_from_tree
5750                                     (postfix_expression, &args));
5751             else if (idk == CP_ID_KIND_QUALIFIED)
5752               /* A call to a static class member, or a namespace-scope
5753                  function.  */
5754               postfix_expression
5755                 = finish_call_expr (postfix_expression, &args,
5756                                     /*disallow_virtual=*/true,
5757                                     koenig_p,
5758                                     tf_warning_or_error);
5759             else
5760               /* All other function calls.  */
5761               postfix_expression
5762                 = finish_call_expr (postfix_expression, &args,
5763                                     /*disallow_virtual=*/false,
5764                                     koenig_p,
5765                                     tf_warning_or_error);
5766
5767             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5768             idk = CP_ID_KIND_NONE;
5769
5770             release_tree_vector (args);
5771           }
5772           break;
5773
5774         case CPP_DOT:
5775         case CPP_DEREF:
5776           /* postfix-expression . template [opt] id-expression
5777              postfix-expression . pseudo-destructor-name
5778              postfix-expression -> template [opt] id-expression
5779              postfix-expression -> pseudo-destructor-name */
5780
5781           /* Consume the `.' or `->' operator.  */
5782           cp_lexer_consume_token (parser->lexer);
5783
5784           postfix_expression
5785             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5786                                                       postfix_expression,
5787                                                       false, &idk,
5788                                                       token->location);
5789
5790           is_member_access = true;
5791           break;
5792
5793         case CPP_PLUS_PLUS:
5794           /* postfix-expression ++  */
5795           /* Consume the `++' token.  */
5796           cp_lexer_consume_token (parser->lexer);
5797           /* Generate a representation for the complete expression.  */
5798           postfix_expression
5799             = finish_increment_expr (postfix_expression,
5800                                      POSTINCREMENT_EXPR);
5801           /* Increments may not appear in constant-expressions.  */
5802           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5803             postfix_expression = error_mark_node;
5804           idk = CP_ID_KIND_NONE;
5805           is_member_access = false;
5806           break;
5807
5808         case CPP_MINUS_MINUS:
5809           /* postfix-expression -- */
5810           /* Consume the `--' token.  */
5811           cp_lexer_consume_token (parser->lexer);
5812           /* Generate a representation for the complete expression.  */
5813           postfix_expression
5814             = finish_increment_expr (postfix_expression,
5815                                      POSTDECREMENT_EXPR);
5816           /* Decrements may not appear in constant-expressions.  */
5817           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5818             postfix_expression = error_mark_node;
5819           idk = CP_ID_KIND_NONE;
5820           is_member_access = false;
5821           break;
5822
5823         default:
5824           if (pidk_return != NULL)
5825             * pidk_return = idk;
5826           if (member_access_only_p)
5827             return is_member_access? postfix_expression : error_mark_node;
5828           else
5829             return postfix_expression;
5830         }
5831     }
5832
5833   /* We should never get here.  */
5834   gcc_unreachable ();
5835   return error_mark_node;
5836 }
5837
5838 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5839    by cp_parser_builtin_offsetof.  We're looking for
5840
5841      postfix-expression [ expression ]
5842      postfix-expression [ braced-init-list ] (C++11)
5843
5844    FOR_OFFSETOF is set if we're being called in that context, which
5845    changes how we deal with integer constant expressions.  */
5846
5847 static tree
5848 cp_parser_postfix_open_square_expression (cp_parser *parser,
5849                                           tree postfix_expression,
5850                                           bool for_offsetof)
5851 {
5852   tree index;
5853
5854   /* Consume the `[' token.  */
5855   cp_lexer_consume_token (parser->lexer);
5856
5857   /* Parse the index expression.  */
5858   /* ??? For offsetof, there is a question of what to allow here.  If
5859      offsetof is not being used in an integral constant expression context,
5860      then we *could* get the right answer by computing the value at runtime.
5861      If we are in an integral constant expression context, then we might
5862      could accept any constant expression; hard to say without analysis.
5863      Rather than open the barn door too wide right away, allow only integer
5864      constant expressions here.  */
5865   if (for_offsetof)
5866     index = cp_parser_constant_expression (parser, false, NULL);
5867   else
5868     {
5869       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5870         {
5871           bool expr_nonconst_p;
5872           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5873           index = cp_parser_braced_list (parser, &expr_nonconst_p);
5874         }
5875       else
5876         index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5877     }
5878
5879   /* Look for the closing `]'.  */
5880   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5881
5882   /* Build the ARRAY_REF.  */
5883   postfix_expression = grok_array_decl (postfix_expression, index);
5884
5885   /* When not doing offsetof, array references are not permitted in
5886      constant-expressions.  */
5887   if (!for_offsetof
5888       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5889     postfix_expression = error_mark_node;
5890
5891   return postfix_expression;
5892 }
5893
5894 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5895    by cp_parser_builtin_offsetof.  We're looking for
5896
5897      postfix-expression . template [opt] id-expression
5898      postfix-expression . pseudo-destructor-name
5899      postfix-expression -> template [opt] id-expression
5900      postfix-expression -> pseudo-destructor-name
5901
5902    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5903    limits what of the above we'll actually accept, but nevermind.
5904    TOKEN_TYPE is the "." or "->" token, which will already have been
5905    removed from the stream.  */
5906
5907 static tree
5908 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5909                                         enum cpp_ttype token_type,
5910                                         tree postfix_expression,
5911                                         bool for_offsetof, cp_id_kind *idk,
5912                                         location_t location)
5913 {
5914   tree name;
5915   bool dependent_p;
5916   bool pseudo_destructor_p;
5917   tree scope = NULL_TREE;
5918
5919   /* If this is a `->' operator, dereference the pointer.  */
5920   if (token_type == CPP_DEREF)
5921     postfix_expression = build_x_arrow (postfix_expression,
5922                                         tf_warning_or_error);
5923   /* Check to see whether or not the expression is type-dependent.  */
5924   dependent_p = type_dependent_expression_p (postfix_expression);
5925   /* The identifier following the `->' or `.' is not qualified.  */
5926   parser->scope = NULL_TREE;
5927   parser->qualifying_scope = NULL_TREE;
5928   parser->object_scope = NULL_TREE;
5929   *idk = CP_ID_KIND_NONE;
5930
5931   /* Enter the scope corresponding to the type of the object
5932      given by the POSTFIX_EXPRESSION.  */
5933   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5934     {
5935       scope = TREE_TYPE (postfix_expression);
5936       /* According to the standard, no expression should ever have
5937          reference type.  Unfortunately, we do not currently match
5938          the standard in this respect in that our internal representation
5939          of an expression may have reference type even when the standard
5940          says it does not.  Therefore, we have to manually obtain the
5941          underlying type here.  */
5942       scope = non_reference (scope);
5943       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5944       if (scope == unknown_type_node)
5945         {
5946           error_at (location, "%qE does not have class type",
5947                     postfix_expression);
5948           scope = NULL_TREE;
5949         }
5950       /* Unlike the object expression in other contexts, *this is not
5951          required to be of complete type for purposes of class member
5952          access (5.2.5) outside the member function body.  */
5953       else if (scope != current_class_ref
5954                && !(processing_template_decl && scope == current_class_type))
5955         scope = complete_type_or_else (scope, NULL_TREE);
5956       /* Let the name lookup machinery know that we are processing a
5957          class member access expression.  */
5958       parser->context->object_type = scope;
5959       /* If something went wrong, we want to be able to discern that case,
5960          as opposed to the case where there was no SCOPE due to the type
5961          of expression being dependent.  */
5962       if (!scope)
5963         scope = error_mark_node;
5964       /* If the SCOPE was erroneous, make the various semantic analysis
5965          functions exit quickly -- and without issuing additional error
5966          messages.  */
5967       if (scope == error_mark_node)
5968         postfix_expression = error_mark_node;
5969     }
5970
5971   /* Assume this expression is not a pseudo-destructor access.  */
5972   pseudo_destructor_p = false;
5973
5974   /* If the SCOPE is a scalar type, then, if this is a valid program,
5975      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5976      is type dependent, it can be pseudo-destructor-name or something else.
5977      Try to parse it as pseudo-destructor-name first.  */
5978   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5979     {
5980       tree s;
5981       tree type;
5982
5983       cp_parser_parse_tentatively (parser);
5984       /* Parse the pseudo-destructor-name.  */
5985       s = NULL_TREE;
5986       cp_parser_pseudo_destructor_name (parser, &s, &type);
5987       if (dependent_p
5988           && (cp_parser_error_occurred (parser)
5989               || TREE_CODE (type) != TYPE_DECL
5990               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5991         cp_parser_abort_tentative_parse (parser);
5992       else if (cp_parser_parse_definitely (parser))
5993         {
5994           pseudo_destructor_p = true;
5995           postfix_expression
5996             = finish_pseudo_destructor_expr (postfix_expression,
5997                                              s, TREE_TYPE (type));
5998         }
5999     }
6000
6001   if (!pseudo_destructor_p)
6002     {
6003       /* If the SCOPE is not a scalar type, we are looking at an
6004          ordinary class member access expression, rather than a
6005          pseudo-destructor-name.  */
6006       bool template_p;
6007       cp_token *token = cp_lexer_peek_token (parser->lexer);
6008       /* Parse the id-expression.  */
6009       name = (cp_parser_id_expression
6010               (parser,
6011                cp_parser_optional_template_keyword (parser),
6012                /*check_dependency_p=*/true,
6013                &template_p,
6014                /*declarator_p=*/false,
6015                /*optional_p=*/false));
6016       /* In general, build a SCOPE_REF if the member name is qualified.
6017          However, if the name was not dependent and has already been
6018          resolved; there is no need to build the SCOPE_REF.  For example;
6019
6020              struct X { void f(); };
6021              template <typename T> void f(T* t) { t->X::f(); }
6022
6023          Even though "t" is dependent, "X::f" is not and has been resolved
6024          to a BASELINK; there is no need to include scope information.  */
6025
6026       /* But we do need to remember that there was an explicit scope for
6027          virtual function calls.  */
6028       if (parser->scope)
6029         *idk = CP_ID_KIND_QUALIFIED;
6030
6031       /* If the name is a template-id that names a type, we will get a
6032          TYPE_DECL here.  That is invalid code.  */
6033       if (TREE_CODE (name) == TYPE_DECL)
6034         {
6035           error_at (token->location, "invalid use of %qD", name);
6036           postfix_expression = error_mark_node;
6037         }
6038       else
6039         {
6040           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6041             {
6042               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6043                 {
6044                   error_at (token->location, "%<%D::%D%> is not a class member",
6045                             parser->scope, name);
6046                   postfix_expression = error_mark_node;
6047                 }
6048               else
6049                 name = build_qualified_name (/*type=*/NULL_TREE,
6050                                              parser->scope,
6051                                              name,
6052                                              template_p);
6053               parser->scope = NULL_TREE;
6054               parser->qualifying_scope = NULL_TREE;
6055               parser->object_scope = NULL_TREE;
6056             }
6057           if (parser->scope && name && BASELINK_P (name))
6058             adjust_result_of_qualified_name_lookup
6059               (name, parser->scope, scope);
6060           postfix_expression
6061             = finish_class_member_access_expr (postfix_expression, name,
6062                                                template_p, 
6063                                                tf_warning_or_error);
6064         }
6065     }
6066
6067   /* We no longer need to look up names in the scope of the object on
6068      the left-hand side of the `.' or `->' operator.  */
6069   parser->context->object_type = NULL_TREE;
6070
6071   /* Outside of offsetof, these operators may not appear in
6072      constant-expressions.  */
6073   if (!for_offsetof
6074       && (cp_parser_non_integral_constant_expression
6075           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6076     postfix_expression = error_mark_node;
6077
6078   return postfix_expression;
6079 }
6080
6081 /* Parse a parenthesized expression-list.
6082
6083    expression-list:
6084      assignment-expression
6085      expression-list, assignment-expression
6086
6087    attribute-list:
6088      expression-list
6089      identifier
6090      identifier, expression-list
6091
6092    CAST_P is true if this expression is the target of a cast.
6093
6094    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6095    argument pack.
6096
6097    Returns a vector of trees.  Each element is a representation of an
6098    assignment-expression.  NULL is returned if the ( and or ) are
6099    missing.  An empty, but allocated, vector is returned on no
6100    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6101    if we are parsing an attribute list for an attribute that wants a
6102    plain identifier argument, normal_attr for an attribute that wants
6103    an expression, or non_attr if we aren't parsing an attribute list.  If
6104    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6105    not all of the expressions in the list were constant.  */
6106
6107 static VEC(tree,gc) *
6108 cp_parser_parenthesized_expression_list (cp_parser* parser,
6109                                          int is_attribute_list,
6110                                          bool cast_p,
6111                                          bool allow_expansion_p,
6112                                          bool *non_constant_p)
6113 {
6114   VEC(tree,gc) *expression_list;
6115   bool fold_expr_p = is_attribute_list != non_attr;
6116   tree identifier = NULL_TREE;
6117   bool saved_greater_than_is_operator_p;
6118
6119   /* Assume all the expressions will be constant.  */
6120   if (non_constant_p)
6121     *non_constant_p = false;
6122
6123   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6124     return NULL;
6125
6126   expression_list = make_tree_vector ();
6127
6128   /* Within a parenthesized expression, a `>' token is always
6129      the greater-than operator.  */
6130   saved_greater_than_is_operator_p
6131     = parser->greater_than_is_operator_p;
6132   parser->greater_than_is_operator_p = true;
6133
6134   /* Consume expressions until there are no more.  */
6135   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6136     while (true)
6137       {
6138         tree expr;
6139
6140         /* At the beginning of attribute lists, check to see if the
6141            next token is an identifier.  */
6142         if (is_attribute_list == id_attr
6143             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6144           {
6145             cp_token *token;
6146
6147             /* Consume the identifier.  */
6148             token = cp_lexer_consume_token (parser->lexer);
6149             /* Save the identifier.  */
6150             identifier = token->u.value;
6151           }
6152         else
6153           {
6154             bool expr_non_constant_p;
6155
6156             /* Parse the next assignment-expression.  */
6157             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6158               {
6159                 /* A braced-init-list.  */
6160                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6161                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6162                 if (non_constant_p && expr_non_constant_p)
6163                   *non_constant_p = true;
6164               }
6165             else if (non_constant_p)
6166               {
6167                 expr = (cp_parser_constant_expression
6168                         (parser, /*allow_non_constant_p=*/true,
6169                          &expr_non_constant_p));
6170                 if (expr_non_constant_p)
6171                   *non_constant_p = true;
6172               }
6173             else
6174               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6175
6176             if (fold_expr_p)
6177               expr = fold_non_dependent_expr (expr);
6178
6179             /* If we have an ellipsis, then this is an expression
6180                expansion.  */
6181             if (allow_expansion_p
6182                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6183               {
6184                 /* Consume the `...'.  */
6185                 cp_lexer_consume_token (parser->lexer);
6186
6187                 /* Build the argument pack.  */
6188                 expr = make_pack_expansion (expr);
6189               }
6190
6191              /* Add it to the list.  We add error_mark_node
6192                 expressions to the list, so that we can still tell if
6193                 the correct form for a parenthesized expression-list
6194                 is found. That gives better errors.  */
6195             VEC_safe_push (tree, gc, expression_list, expr);
6196
6197             if (expr == error_mark_node)
6198               goto skip_comma;
6199           }
6200
6201         /* After the first item, attribute lists look the same as
6202            expression lists.  */
6203         is_attribute_list = non_attr;
6204
6205       get_comma:;
6206         /* If the next token isn't a `,', then we are done.  */
6207         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6208           break;
6209
6210         /* Otherwise, consume the `,' and keep going.  */
6211         cp_lexer_consume_token (parser->lexer);
6212       }
6213
6214   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6215     {
6216       int ending;
6217
6218     skip_comma:;
6219       /* We try and resync to an unnested comma, as that will give the
6220          user better diagnostics.  */
6221       ending = cp_parser_skip_to_closing_parenthesis (parser,
6222                                                       /*recovering=*/true,
6223                                                       /*or_comma=*/true,
6224                                                       /*consume_paren=*/true);
6225       if (ending < 0)
6226         goto get_comma;
6227       if (!ending)
6228         {
6229           parser->greater_than_is_operator_p
6230             = saved_greater_than_is_operator_p;
6231           return NULL;
6232         }
6233     }
6234
6235   parser->greater_than_is_operator_p
6236     = saved_greater_than_is_operator_p;
6237
6238   if (identifier)
6239     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6240
6241   return expression_list;
6242 }
6243
6244 /* Parse a pseudo-destructor-name.
6245
6246    pseudo-destructor-name:
6247      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6248      :: [opt] nested-name-specifier template template-id :: ~ type-name
6249      :: [opt] nested-name-specifier [opt] ~ type-name
6250
6251    If either of the first two productions is used, sets *SCOPE to the
6252    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6253    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6254    or ERROR_MARK_NODE if the parse fails.  */
6255
6256 static void
6257 cp_parser_pseudo_destructor_name (cp_parser* parser,
6258                                   tree* scope,
6259                                   tree* type)
6260 {
6261   bool nested_name_specifier_p;
6262
6263   /* Assume that things will not work out.  */
6264   *type = error_mark_node;
6265
6266   /* Look for the optional `::' operator.  */
6267   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6268   /* Look for the optional nested-name-specifier.  */
6269   nested_name_specifier_p
6270     = (cp_parser_nested_name_specifier_opt (parser,
6271                                             /*typename_keyword_p=*/false,
6272                                             /*check_dependency_p=*/true,
6273                                             /*type_p=*/false,
6274                                             /*is_declaration=*/false)
6275        != NULL_TREE);
6276   /* Now, if we saw a nested-name-specifier, we might be doing the
6277      second production.  */
6278   if (nested_name_specifier_p
6279       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6280     {
6281       /* Consume the `template' keyword.  */
6282       cp_lexer_consume_token (parser->lexer);
6283       /* Parse the template-id.  */
6284       cp_parser_template_id (parser,
6285                              /*template_keyword_p=*/true,
6286                              /*check_dependency_p=*/false,
6287                              /*is_declaration=*/true);
6288       /* Look for the `::' token.  */
6289       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6290     }
6291   /* If the next token is not a `~', then there might be some
6292      additional qualification.  */
6293   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6294     {
6295       /* At this point, we're looking for "type-name :: ~".  The type-name
6296          must not be a class-name, since this is a pseudo-destructor.  So,
6297          it must be either an enum-name, or a typedef-name -- both of which
6298          are just identifiers.  So, we peek ahead to check that the "::"
6299          and "~" tokens are present; if they are not, then we can avoid
6300          calling type_name.  */
6301       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6302           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6303           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6304         {
6305           cp_parser_error (parser, "non-scalar type");
6306           return;
6307         }
6308
6309       /* Look for the type-name.  */
6310       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6311       if (*scope == error_mark_node)
6312         return;
6313
6314       /* Look for the `::' token.  */
6315       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6316     }
6317   else
6318     *scope = NULL_TREE;
6319
6320   /* Look for the `~'.  */
6321   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6322
6323   /* Once we see the ~, this has to be a pseudo-destructor.  */
6324   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6325     cp_parser_commit_to_tentative_parse (parser);
6326
6327   /* Look for the type-name again.  We are not responsible for
6328      checking that it matches the first type-name.  */
6329   *type = cp_parser_nonclass_name (parser);
6330 }
6331
6332 /* Parse a unary-expression.
6333
6334    unary-expression:
6335      postfix-expression
6336      ++ cast-expression
6337      -- cast-expression
6338      unary-operator cast-expression
6339      sizeof unary-expression
6340      sizeof ( type-id )
6341      alignof ( type-id )  [C++0x]
6342      new-expression
6343      delete-expression
6344
6345    GNU Extensions:
6346
6347    unary-expression:
6348      __extension__ cast-expression
6349      __alignof__ unary-expression
6350      __alignof__ ( type-id )
6351      alignof unary-expression  [C++0x]
6352      __real__ cast-expression
6353      __imag__ cast-expression
6354      && identifier
6355
6356    ADDRESS_P is true iff the unary-expression is appearing as the
6357    operand of the `&' operator.   CAST_P is true if this expression is
6358    the target of a cast.
6359
6360    Returns a representation of the expression.  */
6361
6362 static tree
6363 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6364                             cp_id_kind * pidk)
6365 {
6366   cp_token *token;
6367   enum tree_code unary_operator;
6368
6369   /* Peek at the next token.  */
6370   token = cp_lexer_peek_token (parser->lexer);
6371   /* Some keywords give away the kind of expression.  */
6372   if (token->type == CPP_KEYWORD)
6373     {
6374       enum rid keyword = token->keyword;
6375
6376       switch (keyword)
6377         {
6378         case RID_ALIGNOF:
6379         case RID_SIZEOF:
6380           {
6381             tree operand;
6382             enum tree_code op;
6383
6384             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6385             /* Consume the token.  */
6386             cp_lexer_consume_token (parser->lexer);
6387             /* Parse the operand.  */
6388             operand = cp_parser_sizeof_operand (parser, keyword);
6389
6390             if (TYPE_P (operand))
6391               return cxx_sizeof_or_alignof_type (operand, op, true);
6392             else
6393               {
6394                 /* ISO C++ defines alignof only with types, not with
6395                    expressions. So pedwarn if alignof is used with a non-
6396                    type expression. However, __alignof__ is ok.  */
6397                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6398                   pedwarn (token->location, OPT_Wpedantic,
6399                            "ISO C++ does not allow %<alignof%> "
6400                            "with a non-type");
6401
6402                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6403               }
6404           }
6405
6406         case RID_NEW:
6407           return cp_parser_new_expression (parser);
6408
6409         case RID_DELETE:
6410           return cp_parser_delete_expression (parser);
6411
6412         case RID_EXTENSION:
6413           {
6414             /* The saved value of the PEDANTIC flag.  */
6415             int saved_pedantic;
6416             tree expr;
6417
6418             /* Save away the PEDANTIC flag.  */
6419             cp_parser_extension_opt (parser, &saved_pedantic);
6420             /* Parse the cast-expression.  */
6421             expr = cp_parser_simple_cast_expression (parser);
6422             /* Restore the PEDANTIC flag.  */
6423             pedantic = saved_pedantic;
6424
6425             return expr;
6426           }
6427
6428         case RID_REALPART:
6429         case RID_IMAGPART:
6430           {
6431             tree expression;
6432
6433             /* Consume the `__real__' or `__imag__' token.  */
6434             cp_lexer_consume_token (parser->lexer);
6435             /* Parse the cast-expression.  */
6436             expression = cp_parser_simple_cast_expression (parser);
6437             /* Create the complete representation.  */
6438             return build_x_unary_op ((keyword == RID_REALPART
6439                                       ? REALPART_EXPR : IMAGPART_EXPR),
6440                                      expression,
6441                                      tf_warning_or_error);
6442           }
6443           break;
6444
6445         case RID_TRANSACTION_ATOMIC:
6446         case RID_TRANSACTION_RELAXED:
6447           return cp_parser_transaction_expression (parser, keyword);
6448
6449         case RID_NOEXCEPT:
6450           {
6451             tree expr;
6452             const char *saved_message;
6453             bool saved_integral_constant_expression_p;
6454             bool saved_non_integral_constant_expression_p;
6455             bool saved_greater_than_is_operator_p;
6456
6457             cp_lexer_consume_token (parser->lexer);
6458             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6459
6460             saved_message = parser->type_definition_forbidden_message;
6461             parser->type_definition_forbidden_message
6462               = G_("types may not be defined in %<noexcept%> expressions");
6463
6464             saved_integral_constant_expression_p
6465               = parser->integral_constant_expression_p;
6466             saved_non_integral_constant_expression_p
6467               = parser->non_integral_constant_expression_p;
6468             parser->integral_constant_expression_p = false;
6469
6470             saved_greater_than_is_operator_p
6471               = parser->greater_than_is_operator_p;
6472             parser->greater_than_is_operator_p = true;
6473
6474             ++cp_unevaluated_operand;
6475             ++c_inhibit_evaluation_warnings;
6476             expr = cp_parser_expression (parser, false, NULL);
6477             --c_inhibit_evaluation_warnings;
6478             --cp_unevaluated_operand;
6479
6480             parser->greater_than_is_operator_p
6481               = saved_greater_than_is_operator_p;
6482
6483             parser->integral_constant_expression_p
6484               = saved_integral_constant_expression_p;
6485             parser->non_integral_constant_expression_p
6486               = saved_non_integral_constant_expression_p;
6487
6488             parser->type_definition_forbidden_message = saved_message;
6489
6490             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6491             return finish_noexcept_expr (expr, tf_warning_or_error);
6492           }
6493
6494         default:
6495           break;
6496         }
6497     }
6498
6499   /* Look for the `:: new' and `:: delete', which also signal the
6500      beginning of a new-expression, or delete-expression,
6501      respectively.  If the next token is `::', then it might be one of
6502      these.  */
6503   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6504     {
6505       enum rid keyword;
6506
6507       /* See if the token after the `::' is one of the keywords in
6508          which we're interested.  */
6509       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6510       /* If it's `new', we have a new-expression.  */
6511       if (keyword == RID_NEW)
6512         return cp_parser_new_expression (parser);
6513       /* Similarly, for `delete'.  */
6514       else if (keyword == RID_DELETE)
6515         return cp_parser_delete_expression (parser);
6516     }
6517
6518   /* Look for a unary operator.  */
6519   unary_operator = cp_parser_unary_operator (token);
6520   /* The `++' and `--' operators can be handled similarly, even though
6521      they are not technically unary-operators in the grammar.  */
6522   if (unary_operator == ERROR_MARK)
6523     {
6524       if (token->type == CPP_PLUS_PLUS)
6525         unary_operator = PREINCREMENT_EXPR;
6526       else if (token->type == CPP_MINUS_MINUS)
6527         unary_operator = PREDECREMENT_EXPR;
6528       /* Handle the GNU address-of-label extension.  */
6529       else if (cp_parser_allow_gnu_extensions_p (parser)
6530                && token->type == CPP_AND_AND)
6531         {
6532           tree identifier;
6533           tree expression;
6534           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6535
6536           /* Consume the '&&' token.  */
6537           cp_lexer_consume_token (parser->lexer);
6538           /* Look for the identifier.  */
6539           identifier = cp_parser_identifier (parser);
6540           /* Create an expression representing the address.  */
6541           expression = finish_label_address_expr (identifier, loc);
6542           if (cp_parser_non_integral_constant_expression (parser,
6543                                                           NIC_ADDR_LABEL))
6544             expression = error_mark_node;
6545           return expression;
6546         }
6547     }
6548   if (unary_operator != ERROR_MARK)
6549     {
6550       tree cast_expression;
6551       tree expression = error_mark_node;
6552       non_integral_constant non_constant_p = NIC_NONE;
6553
6554       /* Consume the operator token.  */
6555       token = cp_lexer_consume_token (parser->lexer);
6556       /* Parse the cast-expression.  */
6557       cast_expression
6558         = cp_parser_cast_expression (parser,
6559                                      unary_operator == ADDR_EXPR,
6560                                      /*cast_p=*/false, pidk);
6561       /* Now, build an appropriate representation.  */
6562       switch (unary_operator)
6563         {
6564         case INDIRECT_REF:
6565           non_constant_p = NIC_STAR;
6566           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6567                                              tf_warning_or_error);
6568           break;
6569
6570         case ADDR_EXPR:
6571            non_constant_p = NIC_ADDR;
6572           /* Fall through.  */
6573         case BIT_NOT_EXPR:
6574           expression = build_x_unary_op (unary_operator, cast_expression,
6575                                          tf_warning_or_error);
6576           break;
6577
6578         case PREINCREMENT_EXPR:
6579         case PREDECREMENT_EXPR:
6580           non_constant_p = unary_operator == PREINCREMENT_EXPR
6581                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6582           /* Fall through.  */
6583         case UNARY_PLUS_EXPR:
6584         case NEGATE_EXPR:
6585         case TRUTH_NOT_EXPR:
6586           expression = finish_unary_op_expr (unary_operator, cast_expression);
6587           break;
6588
6589         default:
6590           gcc_unreachable ();
6591         }
6592
6593       if (non_constant_p != NIC_NONE
6594           && cp_parser_non_integral_constant_expression (parser,
6595                                                          non_constant_p))
6596         expression = error_mark_node;
6597
6598       return expression;
6599     }
6600
6601   return cp_parser_postfix_expression (parser, address_p, cast_p,
6602                                        /*member_access_only_p=*/false,
6603                                        pidk);
6604 }
6605
6606 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6607    unary-operator, the corresponding tree code is returned.  */
6608
6609 static enum tree_code
6610 cp_parser_unary_operator (cp_token* token)
6611 {
6612   switch (token->type)
6613     {
6614     case CPP_MULT:
6615       return INDIRECT_REF;
6616
6617     case CPP_AND:
6618       return ADDR_EXPR;
6619
6620     case CPP_PLUS:
6621       return UNARY_PLUS_EXPR;
6622
6623     case CPP_MINUS:
6624       return NEGATE_EXPR;
6625
6626     case CPP_NOT:
6627       return TRUTH_NOT_EXPR;
6628
6629     case CPP_COMPL:
6630       return BIT_NOT_EXPR;
6631
6632     default:
6633       return ERROR_MARK;
6634     }
6635 }
6636
6637 /* Parse a new-expression.
6638
6639    new-expression:
6640      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6641      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6642
6643    Returns a representation of the expression.  */
6644
6645 static tree
6646 cp_parser_new_expression (cp_parser* parser)
6647 {
6648   bool global_scope_p;
6649   VEC(tree,gc) *placement;
6650   tree type;
6651   VEC(tree,gc) *initializer;
6652   tree nelts;
6653   tree ret;
6654
6655   /* Look for the optional `::' operator.  */
6656   global_scope_p
6657     = (cp_parser_global_scope_opt (parser,
6658                                    /*current_scope_valid_p=*/false)
6659        != NULL_TREE);
6660   /* Look for the `new' operator.  */
6661   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6662   /* There's no easy way to tell a new-placement from the
6663      `( type-id )' construct.  */
6664   cp_parser_parse_tentatively (parser);
6665   /* Look for a new-placement.  */
6666   placement = cp_parser_new_placement (parser);
6667   /* If that didn't work out, there's no new-placement.  */
6668   if (!cp_parser_parse_definitely (parser))
6669     {
6670       if (placement != NULL)
6671         release_tree_vector (placement);
6672       placement = NULL;
6673     }
6674
6675   /* If the next token is a `(', then we have a parenthesized
6676      type-id.  */
6677   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6678     {
6679       cp_token *token;
6680       const char *saved_message = parser->type_definition_forbidden_message;
6681
6682       /* Consume the `('.  */
6683       cp_lexer_consume_token (parser->lexer);
6684
6685       /* Parse the type-id.  */
6686       parser->type_definition_forbidden_message
6687         = G_("types may not be defined in a new-expression");
6688       type = cp_parser_type_id (parser);
6689       parser->type_definition_forbidden_message = saved_message;
6690
6691       /* Look for the closing `)'.  */
6692       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6693       token = cp_lexer_peek_token (parser->lexer);
6694       /* There should not be a direct-new-declarator in this production,
6695          but GCC used to allowed this, so we check and emit a sensible error
6696          message for this case.  */
6697       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6698         {
6699           error_at (token->location,
6700                     "array bound forbidden after parenthesized type-id");
6701           inform (token->location, 
6702                   "try removing the parentheses around the type-id");
6703           cp_parser_direct_new_declarator (parser);
6704         }
6705       nelts = NULL_TREE;
6706     }
6707   /* Otherwise, there must be a new-type-id.  */
6708   else
6709     type = cp_parser_new_type_id (parser, &nelts);
6710
6711   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6712   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6713       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6714     initializer = cp_parser_new_initializer (parser);
6715   else
6716     initializer = NULL;
6717
6718   /* A new-expression may not appear in an integral constant
6719      expression.  */
6720   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6721     ret = error_mark_node;
6722   else
6723     {
6724       /* Create a representation of the new-expression.  */
6725       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6726                        tf_warning_or_error);
6727     }
6728
6729   if (placement != NULL)
6730     release_tree_vector (placement);
6731   if (initializer != NULL)
6732     release_tree_vector (initializer);
6733
6734   return ret;
6735 }
6736
6737 /* Parse a new-placement.
6738
6739    new-placement:
6740      ( expression-list )
6741
6742    Returns the same representation as for an expression-list.  */
6743
6744 static VEC(tree,gc) *
6745 cp_parser_new_placement (cp_parser* parser)
6746 {
6747   VEC(tree,gc) *expression_list;
6748
6749   /* Parse the expression-list.  */
6750   expression_list = (cp_parser_parenthesized_expression_list
6751                      (parser, non_attr, /*cast_p=*/false,
6752                       /*allow_expansion_p=*/true,
6753                       /*non_constant_p=*/NULL));
6754
6755   return expression_list;
6756 }
6757
6758 /* Parse a new-type-id.
6759
6760    new-type-id:
6761      type-specifier-seq new-declarator [opt]
6762
6763    Returns the TYPE allocated.  If the new-type-id indicates an array
6764    type, *NELTS is set to the number of elements in the last array
6765    bound; the TYPE will not include the last array bound.  */
6766
6767 static tree
6768 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6769 {
6770   cp_decl_specifier_seq type_specifier_seq;
6771   cp_declarator *new_declarator;
6772   cp_declarator *declarator;
6773   cp_declarator *outer_declarator;
6774   const char *saved_message;
6775   tree type;
6776
6777   /* The type-specifier sequence must not contain type definitions.
6778      (It cannot contain declarations of new types either, but if they
6779      are not definitions we will catch that because they are not
6780      complete.)  */
6781   saved_message = parser->type_definition_forbidden_message;
6782   parser->type_definition_forbidden_message
6783     = G_("types may not be defined in a new-type-id");
6784   /* Parse the type-specifier-seq.  */
6785   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6786                                 /*is_trailing_return=*/false,
6787                                 &type_specifier_seq);
6788   /* Restore the old message.  */
6789   parser->type_definition_forbidden_message = saved_message;
6790   /* Parse the new-declarator.  */
6791   new_declarator = cp_parser_new_declarator_opt (parser);
6792
6793   /* Determine the number of elements in the last array dimension, if
6794      any.  */
6795   *nelts = NULL_TREE;
6796   /* Skip down to the last array dimension.  */
6797   declarator = new_declarator;
6798   outer_declarator = NULL;
6799   while (declarator && (declarator->kind == cdk_pointer
6800                         || declarator->kind == cdk_ptrmem))
6801     {
6802       outer_declarator = declarator;
6803       declarator = declarator->declarator;
6804     }
6805   while (declarator
6806          && declarator->kind == cdk_array
6807          && declarator->declarator
6808          && declarator->declarator->kind == cdk_array)
6809     {
6810       outer_declarator = declarator;
6811       declarator = declarator->declarator;
6812     }
6813
6814   if (declarator && declarator->kind == cdk_array)
6815     {
6816       *nelts = declarator->u.array.bounds;
6817       if (*nelts == error_mark_node)
6818         *nelts = integer_one_node;
6819
6820       if (outer_declarator)
6821         outer_declarator->declarator = declarator->declarator;
6822       else
6823         new_declarator = NULL;
6824     }
6825
6826   type = groktypename (&type_specifier_seq, new_declarator, false);
6827   return type;
6828 }
6829
6830 /* Parse an (optional) new-declarator.
6831
6832    new-declarator:
6833      ptr-operator new-declarator [opt]
6834      direct-new-declarator
6835
6836    Returns the declarator.  */
6837
6838 static cp_declarator *
6839 cp_parser_new_declarator_opt (cp_parser* parser)
6840 {
6841   enum tree_code code;
6842   tree type;
6843   cp_cv_quals cv_quals;
6844
6845   /* We don't know if there's a ptr-operator next, or not.  */
6846   cp_parser_parse_tentatively (parser);
6847   /* Look for a ptr-operator.  */
6848   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6849   /* If that worked, look for more new-declarators.  */
6850   if (cp_parser_parse_definitely (parser))
6851     {
6852       cp_declarator *declarator;
6853
6854       /* Parse another optional declarator.  */
6855       declarator = cp_parser_new_declarator_opt (parser);
6856
6857       return cp_parser_make_indirect_declarator
6858         (code, type, cv_quals, declarator);
6859     }
6860
6861   /* If the next token is a `[', there is a direct-new-declarator.  */
6862   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6863     return cp_parser_direct_new_declarator (parser);
6864
6865   return NULL;
6866 }
6867
6868 /* Parse a direct-new-declarator.
6869
6870    direct-new-declarator:
6871      [ expression ]
6872      direct-new-declarator [constant-expression]
6873
6874    */
6875
6876 static cp_declarator *
6877 cp_parser_direct_new_declarator (cp_parser* parser)
6878 {
6879   cp_declarator *declarator = NULL;
6880
6881   while (true)
6882     {
6883       tree expression;
6884
6885       /* Look for the opening `['.  */
6886       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6887       /* The first expression is not required to be constant.  */
6888       if (!declarator)
6889         {
6890           cp_token *token = cp_lexer_peek_token (parser->lexer);
6891           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6892           /* The standard requires that the expression have integral
6893              type.  DR 74 adds enumeration types.  We believe that the
6894              real intent is that these expressions be handled like the
6895              expression in a `switch' condition, which also allows
6896              classes with a single conversion to integral or
6897              enumeration type.  */
6898           if (!processing_template_decl)
6899             {
6900               expression
6901                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6902                                               expression,
6903                                               /*complain=*/true);
6904               if (!expression)
6905                 {
6906                   error_at (token->location,
6907                             "expression in new-declarator must have integral "
6908                             "or enumeration type");
6909                   expression = error_mark_node;
6910                 }
6911             }
6912         }
6913       /* But all the other expressions must be.  */
6914       else
6915         expression
6916           = cp_parser_constant_expression (parser,
6917                                            /*allow_non_constant=*/false,
6918                                            NULL);
6919       /* Look for the closing `]'.  */
6920       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6921
6922       /* Add this bound to the declarator.  */
6923       declarator = make_array_declarator (declarator, expression);
6924
6925       /* If the next token is not a `[', then there are no more
6926          bounds.  */
6927       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6928         break;
6929     }
6930
6931   return declarator;
6932 }
6933
6934 /* Parse a new-initializer.
6935
6936    new-initializer:
6937      ( expression-list [opt] )
6938      braced-init-list
6939
6940    Returns a representation of the expression-list.  */
6941
6942 static VEC(tree,gc) *
6943 cp_parser_new_initializer (cp_parser* parser)
6944 {
6945   VEC(tree,gc) *expression_list;
6946
6947   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6948     {
6949       tree t;
6950       bool expr_non_constant_p;
6951       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6952       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6953       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6954       expression_list = make_tree_vector_single (t);
6955     }
6956   else
6957     expression_list = (cp_parser_parenthesized_expression_list
6958                        (parser, non_attr, /*cast_p=*/false,
6959                         /*allow_expansion_p=*/true,
6960                         /*non_constant_p=*/NULL));
6961
6962   return expression_list;
6963 }
6964
6965 /* Parse a delete-expression.
6966
6967    delete-expression:
6968      :: [opt] delete cast-expression
6969      :: [opt] delete [ ] cast-expression
6970
6971    Returns a representation of the expression.  */
6972
6973 static tree
6974 cp_parser_delete_expression (cp_parser* parser)
6975 {
6976   bool global_scope_p;
6977   bool array_p;
6978   tree expression;
6979
6980   /* Look for the optional `::' operator.  */
6981   global_scope_p
6982     = (cp_parser_global_scope_opt (parser,
6983                                    /*current_scope_valid_p=*/false)
6984        != NULL_TREE);
6985   /* Look for the `delete' keyword.  */
6986   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6987   /* See if the array syntax is in use.  */
6988   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6989     {
6990       /* Consume the `[' token.  */
6991       cp_lexer_consume_token (parser->lexer);
6992       /* Look for the `]' token.  */
6993       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6994       /* Remember that this is the `[]' construct.  */
6995       array_p = true;
6996     }
6997   else
6998     array_p = false;
6999
7000   /* Parse the cast-expression.  */
7001   expression = cp_parser_simple_cast_expression (parser);
7002
7003   /* A delete-expression may not appear in an integral constant
7004      expression.  */
7005   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7006     return error_mark_node;
7007
7008   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7009                         tf_warning_or_error);
7010 }
7011
7012 /* Returns true if TOKEN may start a cast-expression and false
7013    otherwise.  */
7014
7015 static bool
7016 cp_parser_token_starts_cast_expression (cp_token *token)
7017 {
7018   switch (token->type)
7019     {
7020     case CPP_COMMA:
7021     case CPP_SEMICOLON:
7022     case CPP_QUERY:
7023     case CPP_COLON:
7024     case CPP_CLOSE_SQUARE:
7025     case CPP_CLOSE_PAREN:
7026     case CPP_CLOSE_BRACE:
7027     case CPP_DOT:
7028     case CPP_DOT_STAR:
7029     case CPP_DEREF:
7030     case CPP_DEREF_STAR:
7031     case CPP_DIV:
7032     case CPP_MOD:
7033     case CPP_LSHIFT:
7034     case CPP_RSHIFT:
7035     case CPP_LESS:
7036     case CPP_GREATER:
7037     case CPP_LESS_EQ:
7038     case CPP_GREATER_EQ:
7039     case CPP_EQ_EQ:
7040     case CPP_NOT_EQ:
7041     case CPP_EQ:
7042     case CPP_MULT_EQ:
7043     case CPP_DIV_EQ:
7044     case CPP_MOD_EQ:
7045     case CPP_PLUS_EQ:
7046     case CPP_MINUS_EQ:
7047     case CPP_RSHIFT_EQ:
7048     case CPP_LSHIFT_EQ:
7049     case CPP_AND_EQ:
7050     case CPP_XOR_EQ:
7051     case CPP_OR_EQ:
7052     case CPP_XOR:
7053     case CPP_OR:
7054     case CPP_OR_OR:
7055     case CPP_EOF:
7056       return false;
7057
7058       /* '[' may start a primary-expression in obj-c++.  */
7059     case CPP_OPEN_SQUARE:
7060       return c_dialect_objc ();
7061
7062     default:
7063       return true;
7064     }
7065 }
7066
7067 /* Parse a cast-expression.
7068
7069    cast-expression:
7070      unary-expression
7071      ( type-id ) cast-expression
7072
7073    ADDRESS_P is true iff the unary-expression is appearing as the
7074    operand of the `&' operator.   CAST_P is true if this expression is
7075    the target of a cast.
7076
7077    Returns a representation of the expression.  */
7078
7079 static tree
7080 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7081                            cp_id_kind * pidk)
7082 {
7083   /* If it's a `(', then we might be looking at a cast.  */
7084   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7085     {
7086       tree type = NULL_TREE;
7087       tree expr = NULL_TREE;
7088       bool compound_literal_p;
7089       const char *saved_message;
7090
7091       /* There's no way to know yet whether or not this is a cast.
7092          For example, `(int (3))' is a unary-expression, while `(int)
7093          3' is a cast.  So, we resort to parsing tentatively.  */
7094       cp_parser_parse_tentatively (parser);
7095       /* Types may not be defined in a cast.  */
7096       saved_message = parser->type_definition_forbidden_message;
7097       parser->type_definition_forbidden_message
7098         = G_("types may not be defined in casts");
7099       /* Consume the `('.  */
7100       cp_lexer_consume_token (parser->lexer);
7101       /* A very tricky bit is that `(struct S) { 3 }' is a
7102          compound-literal (which we permit in C++ as an extension).
7103          But, that construct is not a cast-expression -- it is a
7104          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7105          is legal; if the compound-literal were a cast-expression,
7106          you'd need an extra set of parentheses.)  But, if we parse
7107          the type-id, and it happens to be a class-specifier, then we
7108          will commit to the parse at that point, because we cannot
7109          undo the action that is done when creating a new class.  So,
7110          then we cannot back up and do a postfix-expression.
7111
7112          Therefore, we scan ahead to the closing `)', and check to see
7113          if the token after the `)' is a `{'.  If so, we are not
7114          looking at a cast-expression.
7115
7116          Save tokens so that we can put them back.  */
7117       cp_lexer_save_tokens (parser->lexer);
7118       /* Skip tokens until the next token is a closing parenthesis.
7119          If we find the closing `)', and the next token is a `{', then
7120          we are looking at a compound-literal.  */
7121       compound_literal_p
7122         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7123                                                   /*consume_paren=*/true)
7124            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7125       /* Roll back the tokens we skipped.  */
7126       cp_lexer_rollback_tokens (parser->lexer);
7127       /* If we were looking at a compound-literal, simulate an error
7128          so that the call to cp_parser_parse_definitely below will
7129          fail.  */
7130       if (compound_literal_p)
7131         cp_parser_simulate_error (parser);
7132       else
7133         {
7134           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7135           parser->in_type_id_in_expr_p = true;
7136           /* Look for the type-id.  */
7137           type = cp_parser_type_id (parser);
7138           /* Look for the closing `)'.  */
7139           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7140           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7141         }
7142
7143       /* Restore the saved message.  */
7144       parser->type_definition_forbidden_message = saved_message;
7145
7146       /* At this point this can only be either a cast or a
7147          parenthesized ctor such as `(T ())' that looks like a cast to
7148          function returning T.  */
7149       if (!cp_parser_error_occurred (parser)
7150           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7151                                                      (parser->lexer)))
7152         {
7153           cp_parser_parse_definitely (parser);
7154           expr = cp_parser_cast_expression (parser,
7155                                             /*address_p=*/false,
7156                                             /*cast_p=*/true, pidk);
7157
7158           /* Warn about old-style casts, if so requested.  */
7159           if (warn_old_style_cast
7160               && !in_system_header
7161               && !VOID_TYPE_P (type)
7162               && current_lang_name != lang_name_c)
7163             warning (OPT_Wold_style_cast, "use of old-style cast");
7164
7165           /* Only type conversions to integral or enumeration types
7166              can be used in constant-expressions.  */
7167           if (!cast_valid_in_integral_constant_expression_p (type)
7168               && cp_parser_non_integral_constant_expression (parser,
7169                                                              NIC_CAST))
7170             return error_mark_node;
7171
7172           /* Perform the cast.  */
7173           expr = build_c_cast (input_location, type, expr);
7174           return expr;
7175         }
7176       else 
7177         cp_parser_abort_tentative_parse (parser);
7178     }
7179
7180   /* If we get here, then it's not a cast, so it must be a
7181      unary-expression.  */
7182   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7183 }
7184
7185 /* Parse a binary expression of the general form:
7186
7187    pm-expression:
7188      cast-expression
7189      pm-expression .* cast-expression
7190      pm-expression ->* cast-expression
7191
7192    multiplicative-expression:
7193      pm-expression
7194      multiplicative-expression * pm-expression
7195      multiplicative-expression / pm-expression
7196      multiplicative-expression % pm-expression
7197
7198    additive-expression:
7199      multiplicative-expression
7200      additive-expression + multiplicative-expression
7201      additive-expression - multiplicative-expression
7202
7203    shift-expression:
7204      additive-expression
7205      shift-expression << additive-expression
7206      shift-expression >> additive-expression
7207
7208    relational-expression:
7209      shift-expression
7210      relational-expression < shift-expression
7211      relational-expression > shift-expression
7212      relational-expression <= shift-expression
7213      relational-expression >= shift-expression
7214
7215   GNU Extension:
7216
7217    relational-expression:
7218      relational-expression <? shift-expression
7219      relational-expression >? shift-expression
7220
7221    equality-expression:
7222      relational-expression
7223      equality-expression == relational-expression
7224      equality-expression != relational-expression
7225
7226    and-expression:
7227      equality-expression
7228      and-expression & equality-expression
7229
7230    exclusive-or-expression:
7231      and-expression
7232      exclusive-or-expression ^ and-expression
7233
7234    inclusive-or-expression:
7235      exclusive-or-expression
7236      inclusive-or-expression | exclusive-or-expression
7237
7238    logical-and-expression:
7239      inclusive-or-expression
7240      logical-and-expression && inclusive-or-expression
7241
7242    logical-or-expression:
7243      logical-and-expression
7244      logical-or-expression || logical-and-expression
7245
7246    All these are implemented with a single function like:
7247
7248    binary-expression:
7249      simple-cast-expression
7250      binary-expression <token> binary-expression
7251
7252    CAST_P is true if this expression is the target of a cast.
7253
7254    The binops_by_token map is used to get the tree codes for each <token> type.
7255    binary-expressions are associated according to a precedence table.  */
7256
7257 #define TOKEN_PRECEDENCE(token)                              \
7258 (((token->type == CPP_GREATER                                \
7259    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7260   && !parser->greater_than_is_operator_p)                    \
7261  ? PREC_NOT_OPERATOR                                         \
7262  : binops_by_token[token->type].prec)
7263
7264 static tree
7265 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7266                              bool no_toplevel_fold_p,
7267                              enum cp_parser_prec prec,
7268                              cp_id_kind * pidk)
7269 {
7270   cp_parser_expression_stack stack;
7271   cp_parser_expression_stack_entry *sp = &stack[0];
7272   tree lhs, rhs;
7273   cp_token *token;
7274   enum tree_code tree_type, lhs_type, rhs_type;
7275   enum cp_parser_prec new_prec, lookahead_prec;
7276   tree overload;
7277
7278   /* Parse the first expression.  */
7279   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7280   lhs_type = ERROR_MARK;
7281
7282   for (;;)
7283     {
7284       /* Get an operator token.  */
7285       token = cp_lexer_peek_token (parser->lexer);
7286
7287       if (warn_cxx0x_compat
7288           && token->type == CPP_RSHIFT
7289           && !parser->greater_than_is_operator_p)
7290         {
7291           if (warning_at (token->location, OPT_Wc__0x_compat, 
7292                           "%<>>%> operator is treated as"
7293                           " two right angle brackets in C++11"))
7294             inform (token->location,
7295                     "suggest parentheses around %<>>%> expression");
7296         }
7297
7298       new_prec = TOKEN_PRECEDENCE (token);
7299
7300       /* Popping an entry off the stack means we completed a subexpression:
7301          - either we found a token which is not an operator (`>' where it is not
7302            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7303            will happen repeatedly;
7304          - or, we found an operator which has lower priority.  This is the case
7305            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7306            parsing `3 * 4'.  */
7307       if (new_prec <= prec)
7308         {
7309           if (sp == stack)
7310             break;
7311           else
7312             goto pop;
7313         }
7314
7315      get_rhs:
7316       tree_type = binops_by_token[token->type].tree_type;
7317
7318       /* We used the operator token.  */
7319       cp_lexer_consume_token (parser->lexer);
7320
7321       /* For "false && x" or "true || x", x will never be executed;
7322          disable warnings while evaluating it.  */
7323       if (tree_type == TRUTH_ANDIF_EXPR)
7324         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7325       else if (tree_type == TRUTH_ORIF_EXPR)
7326         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7327
7328       /* Extract another operand.  It may be the RHS of this expression
7329          or the LHS of a new, higher priority expression.  */
7330       rhs = cp_parser_simple_cast_expression (parser);
7331       rhs_type = ERROR_MARK;
7332
7333       /* Get another operator token.  Look up its precedence to avoid
7334          building a useless (immediately popped) stack entry for common
7335          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7336       token = cp_lexer_peek_token (parser->lexer);
7337       lookahead_prec = TOKEN_PRECEDENCE (token);
7338       if (lookahead_prec > new_prec)
7339         {
7340           /* ... and prepare to parse the RHS of the new, higher priority
7341              expression.  Since precedence levels on the stack are
7342              monotonically increasing, we do not have to care about
7343              stack overflows.  */
7344           sp->prec = prec;
7345           sp->tree_type = tree_type;
7346           sp->lhs = lhs;
7347           sp->lhs_type = lhs_type;
7348           sp++;
7349           lhs = rhs;
7350           lhs_type = rhs_type;
7351           prec = new_prec;
7352           new_prec = lookahead_prec;
7353           goto get_rhs;
7354
7355          pop:
7356           lookahead_prec = new_prec;
7357           /* If the stack is not empty, we have parsed into LHS the right side
7358              (`4' in the example above) of an expression we had suspended.
7359              We can use the information on the stack to recover the LHS (`3')
7360              from the stack together with the tree code (`MULT_EXPR'), and
7361              the precedence of the higher level subexpression
7362              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7363              which will be used to actually build the additive expression.  */
7364           --sp;
7365           prec = sp->prec;
7366           tree_type = sp->tree_type;
7367           rhs = lhs;
7368           rhs_type = lhs_type;
7369           lhs = sp->lhs;
7370           lhs_type = sp->lhs_type;
7371         }
7372
7373       /* Undo the disabling of warnings done above.  */
7374       if (tree_type == TRUTH_ANDIF_EXPR)
7375         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7376       else if (tree_type == TRUTH_ORIF_EXPR)
7377         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7378
7379       overload = NULL;
7380       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7381          ERROR_MARK for everything that is not a binary expression.
7382          This makes warn_about_parentheses miss some warnings that
7383          involve unary operators.  For unary expressions we should
7384          pass the correct tree_code unless the unary expression was
7385          surrounded by parentheses.
7386       */
7387       if (no_toplevel_fold_p
7388           && lookahead_prec <= prec
7389           && sp == stack
7390           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7391         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7392       else
7393         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7394                                  &overload, tf_warning_or_error);
7395       lhs_type = tree_type;
7396
7397       /* If the binary operator required the use of an overloaded operator,
7398          then this expression cannot be an integral constant-expression.
7399          An overloaded operator can be used even if both operands are
7400          otherwise permissible in an integral constant-expression if at
7401          least one of the operands is of enumeration type.  */
7402
7403       if (overload
7404           && cp_parser_non_integral_constant_expression (parser,
7405                                                          NIC_OVERLOADED))
7406         return error_mark_node;
7407     }
7408
7409   return lhs;
7410 }
7411
7412
7413 /* Parse the `? expression : assignment-expression' part of a
7414    conditional-expression.  The LOGICAL_OR_EXPR is the
7415    logical-or-expression that started the conditional-expression.
7416    Returns a representation of the entire conditional-expression.
7417
7418    This routine is used by cp_parser_assignment_expression.
7419
7420      ? expression : assignment-expression
7421
7422    GNU Extensions:
7423
7424      ? : assignment-expression */
7425
7426 static tree
7427 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7428 {
7429   tree expr;
7430   tree assignment_expr;
7431   struct cp_token *token;
7432
7433   /* Consume the `?' token.  */
7434   cp_lexer_consume_token (parser->lexer);
7435   token = cp_lexer_peek_token (parser->lexer);
7436   if (cp_parser_allow_gnu_extensions_p (parser)
7437       && token->type == CPP_COLON)
7438     {
7439       pedwarn (token->location, OPT_Wpedantic, 
7440                "ISO C++ does not allow ?: with omitted middle operand");
7441       /* Implicit true clause.  */
7442       expr = NULL_TREE;
7443       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7444       warn_for_omitted_condop (token->location, logical_or_expr);
7445     }
7446   else
7447     {
7448       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7449       parser->colon_corrects_to_scope_p = false;
7450       /* Parse the expression.  */
7451       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7452       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7453       c_inhibit_evaluation_warnings +=
7454         ((logical_or_expr == truthvalue_true_node)
7455          - (logical_or_expr == truthvalue_false_node));
7456       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7457     }
7458
7459   /* The next token should be a `:'.  */
7460   cp_parser_require (parser, CPP_COLON, RT_COLON);
7461   /* Parse the assignment-expression.  */
7462   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7463   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7464
7465   /* Build the conditional-expression.  */
7466   return build_x_conditional_expr (logical_or_expr,
7467                                    expr,
7468                                    assignment_expr,
7469                                    tf_warning_or_error);
7470 }
7471
7472 /* Parse an assignment-expression.
7473
7474    assignment-expression:
7475      conditional-expression
7476      logical-or-expression assignment-operator assignment_expression
7477      throw-expression
7478
7479    CAST_P is true if this expression is the target of a cast.
7480
7481    Returns a representation for the expression.  */
7482
7483 static tree
7484 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7485                                  cp_id_kind * pidk)
7486 {
7487   tree expr;
7488
7489   /* If the next token is the `throw' keyword, then we're looking at
7490      a throw-expression.  */
7491   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7492     expr = cp_parser_throw_expression (parser);
7493   /* Otherwise, it must be that we are looking at a
7494      logical-or-expression.  */
7495   else
7496     {
7497       /* Parse the binary expressions (logical-or-expression).  */
7498       expr = cp_parser_binary_expression (parser, cast_p, false,
7499                                           PREC_NOT_OPERATOR, pidk);
7500       /* If the next token is a `?' then we're actually looking at a
7501          conditional-expression.  */
7502       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7503         return cp_parser_question_colon_clause (parser, expr);
7504       else
7505         {
7506           enum tree_code assignment_operator;
7507
7508           /* If it's an assignment-operator, we're using the second
7509              production.  */
7510           assignment_operator
7511             = cp_parser_assignment_operator_opt (parser);
7512           if (assignment_operator != ERROR_MARK)
7513             {
7514               bool non_constant_p;
7515
7516               /* Parse the right-hand side of the assignment.  */
7517               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7518
7519               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7520                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7521
7522               /* An assignment may not appear in a
7523                  constant-expression.  */
7524               if (cp_parser_non_integral_constant_expression (parser,
7525                                                               NIC_ASSIGNMENT))
7526                 return error_mark_node;
7527               /* Build the assignment expression.  */
7528               expr = build_x_modify_expr (expr,
7529                                           assignment_operator,
7530                                           rhs,
7531                                           tf_warning_or_error);
7532             }
7533         }
7534     }
7535
7536   return expr;
7537 }
7538
7539 /* Parse an (optional) assignment-operator.
7540
7541    assignment-operator: one of
7542      = *= /= %= += -= >>= <<= &= ^= |=
7543
7544    GNU Extension:
7545
7546    assignment-operator: one of
7547      <?= >?=
7548
7549    If the next token is an assignment operator, the corresponding tree
7550    code is returned, and the token is consumed.  For example, for
7551    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7552    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7553    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7554    operator, ERROR_MARK is returned.  */
7555
7556 static enum tree_code
7557 cp_parser_assignment_operator_opt (cp_parser* parser)
7558 {
7559   enum tree_code op;
7560   cp_token *token;
7561
7562   /* Peek at the next token.  */
7563   token = cp_lexer_peek_token (parser->lexer);
7564
7565   switch (token->type)
7566     {
7567     case CPP_EQ:
7568       op = NOP_EXPR;
7569       break;
7570
7571     case CPP_MULT_EQ:
7572       op = MULT_EXPR;
7573       break;
7574
7575     case CPP_DIV_EQ:
7576       op = TRUNC_DIV_EXPR;
7577       break;
7578
7579     case CPP_MOD_EQ:
7580       op = TRUNC_MOD_EXPR;
7581       break;
7582
7583     case CPP_PLUS_EQ:
7584       op = PLUS_EXPR;
7585       break;
7586
7587     case CPP_MINUS_EQ:
7588       op = MINUS_EXPR;
7589       break;
7590
7591     case CPP_RSHIFT_EQ:
7592       op = RSHIFT_EXPR;
7593       break;
7594
7595     case CPP_LSHIFT_EQ:
7596       op = LSHIFT_EXPR;
7597       break;
7598
7599     case CPP_AND_EQ:
7600       op = BIT_AND_EXPR;
7601       break;
7602
7603     case CPP_XOR_EQ:
7604       op = BIT_XOR_EXPR;
7605       break;
7606
7607     case CPP_OR_EQ:
7608       op = BIT_IOR_EXPR;
7609       break;
7610
7611     default:
7612       /* Nothing else is an assignment operator.  */
7613       op = ERROR_MARK;
7614     }
7615
7616   /* If it was an assignment operator, consume it.  */
7617   if (op != ERROR_MARK)
7618     cp_lexer_consume_token (parser->lexer);
7619
7620   return op;
7621 }
7622
7623 /* Parse an expression.
7624
7625    expression:
7626      assignment-expression
7627      expression , assignment-expression
7628
7629    CAST_P is true if this expression is the target of a cast.
7630
7631    Returns a representation of the expression.  */
7632
7633 static tree
7634 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7635 {
7636   tree expression = NULL_TREE;
7637
7638   while (true)
7639     {
7640       tree assignment_expression;
7641
7642       /* Parse the next assignment-expression.  */
7643       assignment_expression
7644         = cp_parser_assignment_expression (parser, cast_p, pidk);
7645       /* If this is the first assignment-expression, we can just
7646          save it away.  */
7647       if (!expression)
7648         expression = assignment_expression;
7649       else
7650         expression = build_x_compound_expr (expression,
7651                                             assignment_expression,
7652                                             tf_warning_or_error);
7653       /* If the next token is not a comma, then we are done with the
7654          expression.  */
7655       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7656         break;
7657       /* Consume the `,'.  */
7658       cp_lexer_consume_token (parser->lexer);
7659       /* A comma operator cannot appear in a constant-expression.  */
7660       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7661         expression = error_mark_node;
7662     }
7663
7664   return expression;
7665 }
7666
7667 /* Parse a constant-expression.
7668
7669    constant-expression:
7670      conditional-expression
7671
7672   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7673   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7674   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7675   is false, NON_CONSTANT_P should be NULL.  */
7676
7677 static tree
7678 cp_parser_constant_expression (cp_parser* parser,
7679                                bool allow_non_constant_p,
7680                                bool *non_constant_p)
7681 {
7682   bool saved_integral_constant_expression_p;
7683   bool saved_allow_non_integral_constant_expression_p;
7684   bool saved_non_integral_constant_expression_p;
7685   tree expression;
7686
7687   /* It might seem that we could simply parse the
7688      conditional-expression, and then check to see if it were
7689      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7690      one that the compiler can figure out is constant, possibly after
7691      doing some simplifications or optimizations.  The standard has a
7692      precise definition of constant-expression, and we must honor
7693      that, even though it is somewhat more restrictive.
7694
7695      For example:
7696
7697        int i[(2, 3)];
7698
7699      is not a legal declaration, because `(2, 3)' is not a
7700      constant-expression.  The `,' operator is forbidden in a
7701      constant-expression.  However, GCC's constant-folding machinery
7702      will fold this operation to an INTEGER_CST for `3'.  */
7703
7704   /* Save the old settings.  */
7705   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7706   saved_allow_non_integral_constant_expression_p
7707     = parser->allow_non_integral_constant_expression_p;
7708   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7709   /* We are now parsing a constant-expression.  */
7710   parser->integral_constant_expression_p = true;
7711   parser->allow_non_integral_constant_expression_p
7712     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7713   parser->non_integral_constant_expression_p = false;
7714   /* Although the grammar says "conditional-expression", we parse an
7715      "assignment-expression", which also permits "throw-expression"
7716      and the use of assignment operators.  In the case that
7717      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7718      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7719      actually essential that we look for an assignment-expression.
7720      For example, cp_parser_initializer_clauses uses this function to
7721      determine whether a particular assignment-expression is in fact
7722      constant.  */
7723   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7724   /* Restore the old settings.  */
7725   parser->integral_constant_expression_p
7726     = saved_integral_constant_expression_p;
7727   parser->allow_non_integral_constant_expression_p
7728     = saved_allow_non_integral_constant_expression_p;
7729   if (cxx_dialect >= cxx0x)
7730     {
7731       /* Require an rvalue constant expression here; that's what our
7732          callers expect.  Reference constant expressions are handled
7733          separately in e.g. cp_parser_template_argument.  */
7734       bool is_const = potential_rvalue_constant_expression (expression);
7735       parser->non_integral_constant_expression_p = !is_const;
7736       if (!is_const && !allow_non_constant_p)
7737         require_potential_rvalue_constant_expression (expression);
7738     }
7739   if (allow_non_constant_p)
7740     *non_constant_p = parser->non_integral_constant_expression_p;
7741   parser->non_integral_constant_expression_p
7742     = saved_non_integral_constant_expression_p;
7743
7744   return expression;
7745 }
7746
7747 /* Parse __builtin_offsetof.
7748
7749    offsetof-expression:
7750      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7751
7752    offsetof-member-designator:
7753      id-expression
7754      | offsetof-member-designator "." id-expression
7755      | offsetof-member-designator "[" expression "]"
7756      | offsetof-member-designator "->" id-expression  */
7757
7758 static tree
7759 cp_parser_builtin_offsetof (cp_parser *parser)
7760 {
7761   int save_ice_p, save_non_ice_p;
7762   tree type, expr;
7763   cp_id_kind dummy;
7764   cp_token *token;
7765
7766   /* We're about to accept non-integral-constant things, but will
7767      definitely yield an integral constant expression.  Save and
7768      restore these values around our local parsing.  */
7769   save_ice_p = parser->integral_constant_expression_p;
7770   save_non_ice_p = parser->non_integral_constant_expression_p;
7771
7772   /* Consume the "__builtin_offsetof" token.  */
7773   cp_lexer_consume_token (parser->lexer);
7774   /* Consume the opening `('.  */
7775   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7776   /* Parse the type-id.  */
7777   type = cp_parser_type_id (parser);
7778   /* Look for the `,'.  */
7779   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7780   token = cp_lexer_peek_token (parser->lexer);
7781
7782   /* Build the (type *)null that begins the traditional offsetof macro.  */
7783   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7784                             tf_warning_or_error);
7785
7786   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7787   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7788                                                  true, &dummy, token->location);
7789   while (true)
7790     {
7791       token = cp_lexer_peek_token (parser->lexer);
7792       switch (token->type)
7793         {
7794         case CPP_OPEN_SQUARE:
7795           /* offsetof-member-designator "[" expression "]" */
7796           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7797           break;
7798
7799         case CPP_DEREF:
7800           /* offsetof-member-designator "->" identifier */
7801           expr = grok_array_decl (expr, integer_zero_node);
7802           /* FALLTHRU */
7803
7804         case CPP_DOT:
7805           /* offsetof-member-designator "." identifier */
7806           cp_lexer_consume_token (parser->lexer);
7807           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7808                                                          expr, true, &dummy,
7809                                                          token->location);
7810           break;
7811
7812         case CPP_CLOSE_PAREN:
7813           /* Consume the ")" token.  */
7814           cp_lexer_consume_token (parser->lexer);
7815           goto success;
7816
7817         default:
7818           /* Error.  We know the following require will fail, but
7819              that gives the proper error message.  */
7820           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7821           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7822           expr = error_mark_node;
7823           goto failure;
7824         }
7825     }
7826
7827  success:
7828   /* If we're processing a template, we can't finish the semantics yet.
7829      Otherwise we can fold the entire expression now.  */
7830   if (processing_template_decl)
7831     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7832   else
7833     expr = finish_offsetof (expr);
7834
7835  failure:
7836   parser->integral_constant_expression_p = save_ice_p;
7837   parser->non_integral_constant_expression_p = save_non_ice_p;
7838
7839   return expr;
7840 }
7841
7842 /* Parse a trait expression.
7843
7844    Returns a representation of the expression, the underlying type
7845    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7846
7847 static tree
7848 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7849 {
7850   cp_trait_kind kind;
7851   tree type1, type2 = NULL_TREE;
7852   bool binary = false;
7853   cp_decl_specifier_seq decl_specs;
7854
7855   switch (keyword)
7856     {
7857     case RID_HAS_NOTHROW_ASSIGN:
7858       kind = CPTK_HAS_NOTHROW_ASSIGN;
7859       break;
7860     case RID_HAS_NOTHROW_CONSTRUCTOR:
7861       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7862       break;
7863     case RID_HAS_NOTHROW_COPY:
7864       kind = CPTK_HAS_NOTHROW_COPY;
7865       break;
7866     case RID_HAS_TRIVIAL_ASSIGN:
7867       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7868       break;
7869     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7870       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7871       break;
7872     case RID_HAS_TRIVIAL_COPY:
7873       kind = CPTK_HAS_TRIVIAL_COPY;
7874       break;
7875     case RID_HAS_TRIVIAL_DESTRUCTOR:
7876       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7877       break;
7878     case RID_HAS_VIRTUAL_DESTRUCTOR:
7879       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7880       break;
7881     case RID_IS_ABSTRACT:
7882       kind = CPTK_IS_ABSTRACT;
7883       break;
7884     case RID_IS_BASE_OF:
7885       kind = CPTK_IS_BASE_OF;
7886       binary = true;
7887       break;
7888     case RID_IS_CLASS:
7889       kind = CPTK_IS_CLASS;
7890       break;
7891     case RID_IS_CONVERTIBLE_TO:
7892       kind = CPTK_IS_CONVERTIBLE_TO;
7893       binary = true;
7894       break;
7895     case RID_IS_EMPTY:
7896       kind = CPTK_IS_EMPTY;
7897       break;
7898     case RID_IS_ENUM:
7899       kind = CPTK_IS_ENUM;
7900       break;
7901     case RID_IS_FINAL:
7902       kind = CPTK_IS_FINAL;
7903       break;
7904     case RID_IS_LITERAL_TYPE:
7905       kind = CPTK_IS_LITERAL_TYPE;
7906       break;
7907     case RID_IS_POD:
7908       kind = CPTK_IS_POD;
7909       break;
7910     case RID_IS_POLYMORPHIC:
7911       kind = CPTK_IS_POLYMORPHIC;
7912       break;
7913     case RID_IS_STD_LAYOUT:
7914       kind = CPTK_IS_STD_LAYOUT;
7915       break;
7916     case RID_IS_TRIVIAL:
7917       kind = CPTK_IS_TRIVIAL;
7918       break;
7919     case RID_IS_UNION:
7920       kind = CPTK_IS_UNION;
7921       break;
7922     case RID_UNDERLYING_TYPE:
7923       kind = CPTK_UNDERLYING_TYPE;
7924       break;
7925     case RID_BASES:
7926       kind = CPTK_BASES;
7927       break;
7928     case RID_DIRECT_BASES:
7929       kind = CPTK_DIRECT_BASES;
7930       break;
7931     default:
7932       gcc_unreachable ();
7933     }
7934
7935   /* Consume the token.  */
7936   cp_lexer_consume_token (parser->lexer);
7937
7938   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7939
7940   type1 = cp_parser_type_id (parser);
7941
7942   if (type1 == error_mark_node)
7943     return error_mark_node;
7944
7945   /* Build a trivial decl-specifier-seq.  */
7946   clear_decl_specs (&decl_specs);
7947   decl_specs.type = type1;
7948
7949   /* Call grokdeclarator to figure out what type this is.  */
7950   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7951                           /*initialized=*/0, /*attrlist=*/NULL);
7952
7953   if (binary)
7954     {
7955       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7956  
7957       type2 = cp_parser_type_id (parser);
7958
7959       if (type2 == error_mark_node)
7960         return error_mark_node;
7961
7962       /* Build a trivial decl-specifier-seq.  */
7963       clear_decl_specs (&decl_specs);
7964       decl_specs.type = type2;
7965
7966       /* Call grokdeclarator to figure out what type this is.  */
7967       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7968                               /*initialized=*/0, /*attrlist=*/NULL);
7969     }
7970
7971   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7972
7973   /* Complete the trait expression, which may mean either processing
7974      the trait expr now or saving it for template instantiation.  */
7975   switch(kind)
7976     {
7977     case CPTK_UNDERLYING_TYPE:
7978       return finish_underlying_type (type1);
7979     case CPTK_BASES:
7980       return finish_bases (type1, false);
7981     case CPTK_DIRECT_BASES:
7982       return finish_bases (type1, true);
7983     default:
7984       return finish_trait_expr (kind, type1, type2);
7985     }
7986 }
7987
7988 /* Lambdas that appear in variable initializer or default argument scope
7989    get that in their mangling, so we need to record it.  We might as well
7990    use the count for function and namespace scopes as well.  */
7991 static GTY(()) tree lambda_scope;
7992 static GTY(()) int lambda_count;
7993 typedef struct GTY(()) tree_int
7994 {
7995   tree t;
7996   int i;
7997 } tree_int;
7998 DEF_VEC_O(tree_int);
7999 DEF_VEC_ALLOC_O(tree_int,gc);
8000 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
8001
8002 static void
8003 start_lambda_scope (tree decl)
8004 {
8005   tree_int ti;
8006   gcc_assert (decl);
8007   /* Once we're inside a function, we ignore other scopes and just push
8008      the function again so that popping works properly.  */
8009   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
8010     decl = current_function_decl;
8011   ti.t = lambda_scope;
8012   ti.i = lambda_count;
8013   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
8014   if (lambda_scope != decl)
8015     {
8016       /* Don't reset the count if we're still in the same function.  */
8017       lambda_scope = decl;
8018       lambda_count = 0;
8019     }
8020 }
8021
8022 static void
8023 record_lambda_scope (tree lambda)
8024 {
8025   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8026   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8027 }
8028
8029 static void
8030 finish_lambda_scope (void)
8031 {
8032   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8033   if (lambda_scope != p->t)
8034     {
8035       lambda_scope = p->t;
8036       lambda_count = p->i;
8037     }
8038   VEC_pop (tree_int, lambda_scope_stack);
8039 }
8040
8041 /* Parse a lambda expression.
8042
8043    lambda-expression:
8044      lambda-introducer lambda-declarator [opt] compound-statement
8045
8046    Returns a representation of the expression.  */
8047
8048 static tree
8049 cp_parser_lambda_expression (cp_parser* parser)
8050 {
8051   tree lambda_expr = build_lambda_expr ();
8052   tree type;
8053   bool ok;
8054
8055   LAMBDA_EXPR_LOCATION (lambda_expr)
8056     = cp_lexer_peek_token (parser->lexer)->location;
8057
8058   if (cp_unevaluated_operand)
8059     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8060               "lambda-expression in unevaluated context");
8061
8062   /* We may be in the middle of deferred access check.  Disable
8063      it now.  */
8064   push_deferring_access_checks (dk_no_deferred);
8065
8066   cp_parser_lambda_introducer (parser, lambda_expr);
8067
8068   type = begin_lambda_type (lambda_expr);
8069   if (type == error_mark_node)
8070     return error_mark_node;
8071
8072   record_lambda_scope (lambda_expr);
8073
8074   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8075   determine_visibility (TYPE_NAME (type));
8076
8077   /* Now that we've started the type, add the capture fields for any
8078      explicit captures.  */
8079   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8080
8081   {
8082     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8083     unsigned int saved_num_template_parameter_lists
8084         = parser->num_template_parameter_lists;
8085     unsigned char in_statement = parser->in_statement;
8086     bool in_switch_statement_p = parser->in_switch_statement_p;
8087
8088     parser->num_template_parameter_lists = 0;
8089     parser->in_statement = 0;
8090     parser->in_switch_statement_p = false;
8091
8092     /* By virtue of defining a local class, a lambda expression has access to
8093        the private variables of enclosing classes.  */
8094
8095     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8096
8097     if (ok)
8098       cp_parser_lambda_body (parser, lambda_expr);
8099     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8100       cp_parser_skip_to_end_of_block_or_statement (parser);
8101
8102     /* The capture list was built up in reverse order; fix that now.  */
8103     {
8104       tree newlist = NULL_TREE;
8105       tree elt, next;
8106
8107       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8108            elt; elt = next)
8109         {
8110           next = TREE_CHAIN (elt);
8111           TREE_CHAIN (elt) = newlist;
8112           newlist = elt;
8113         }
8114       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8115     }
8116
8117     if (ok)
8118       maybe_add_lambda_conv_op (type);
8119
8120     type = finish_struct (type, /*attributes=*/NULL_TREE);
8121
8122     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8123     parser->in_statement = in_statement;
8124     parser->in_switch_statement_p = in_switch_statement_p;
8125   }
8126
8127   pop_deferring_access_checks ();
8128
8129   /* This field is only used during parsing of the lambda.  */
8130   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8131
8132   /* This lambda shouldn't have any proxies left at this point.  */
8133   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8134   /* And now that we're done, push proxies for an enclosing lambda.  */
8135   insert_pending_capture_proxies ();
8136
8137   if (ok)
8138     return build_lambda_object (lambda_expr);
8139   else
8140     return error_mark_node;
8141 }
8142
8143 /* Parse the beginning of a lambda expression.
8144
8145    lambda-introducer:
8146      [ lambda-capture [opt] ]
8147
8148    LAMBDA_EXPR is the current representation of the lambda expression.  */
8149
8150 static void
8151 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8152 {
8153   /* Need commas after the first capture.  */
8154   bool first = true;
8155
8156   /* Eat the leading `['.  */
8157   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8158
8159   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8160   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8161       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8162     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8163   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8164     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8165
8166   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8167     {
8168       cp_lexer_consume_token (parser->lexer);
8169       first = false;
8170     }
8171
8172   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8173     {
8174       cp_token* capture_token;
8175       tree capture_id;
8176       tree capture_init_expr;
8177       cp_id_kind idk = CP_ID_KIND_NONE;
8178       bool explicit_init_p = false;
8179
8180       enum capture_kind_type
8181       {
8182         BY_COPY,
8183         BY_REFERENCE
8184       };
8185       enum capture_kind_type capture_kind = BY_COPY;
8186
8187       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8188         {
8189           error ("expected end of capture-list");
8190           return;
8191         }
8192
8193       if (first)
8194         first = false;
8195       else
8196         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8197
8198       /* Possibly capture `this'.  */
8199       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8200         {
8201           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8202           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8203             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8204                      "with by-copy capture default");
8205           cp_lexer_consume_token (parser->lexer);
8206           add_capture (lambda_expr,
8207                        /*id=*/this_identifier,
8208                        /*initializer=*/finish_this_expr(),
8209                        /*by_reference_p=*/false,
8210                        explicit_init_p);
8211           continue;
8212         }
8213
8214       /* Remember whether we want to capture as a reference or not.  */
8215       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8216         {
8217           capture_kind = BY_REFERENCE;
8218           cp_lexer_consume_token (parser->lexer);
8219         }
8220
8221       /* Get the identifier.  */
8222       capture_token = cp_lexer_peek_token (parser->lexer);
8223       capture_id = cp_parser_identifier (parser);
8224
8225       if (capture_id == error_mark_node)
8226         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8227            delimiters, but I modified this to stop on unnested ']' as well.  It
8228            was already changed to stop on unnested '}', so the
8229            "closing_parenthesis" name is no more misleading with my change.  */
8230         {
8231           cp_parser_skip_to_closing_parenthesis (parser,
8232                                                  /*recovering=*/true,
8233                                                  /*or_comma=*/true,
8234                                                  /*consume_paren=*/true);
8235           break;
8236         }
8237
8238       /* Find the initializer for this capture.  */
8239       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8240         {
8241           /* An explicit expression exists.  */
8242           cp_lexer_consume_token (parser->lexer);
8243           pedwarn (input_location, OPT_Wpedantic,
8244                    "ISO C++ does not allow initializers "
8245                    "in lambda expression capture lists");
8246           capture_init_expr = cp_parser_assignment_expression (parser,
8247                                                                /*cast_p=*/true,
8248                                                                &idk);
8249           explicit_init_p = true;
8250         }
8251       else
8252         {
8253           const char* error_msg;
8254
8255           /* Turn the identifier into an id-expression.  */
8256           capture_init_expr
8257             = cp_parser_lookup_name
8258                 (parser,
8259                  capture_id,
8260                  none_type,
8261                  /*is_template=*/false,
8262                  /*is_namespace=*/false,
8263                  /*check_dependency=*/true,
8264                  /*ambiguous_decls=*/NULL,
8265                  capture_token->location);
8266
8267           if (capture_init_expr == error_mark_node)
8268             {
8269               unqualified_name_lookup_error (capture_id);
8270               continue;
8271             }
8272           else if (DECL_P (capture_init_expr)
8273                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8274                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8275             {
8276               error_at (capture_token->location,
8277                         "capture of non-variable %qD ",
8278                         capture_init_expr);
8279               inform (0, "%q+#D declared here", capture_init_expr);
8280               continue;
8281             }
8282           if (TREE_CODE (capture_init_expr) == VAR_DECL
8283               && decl_storage_duration (capture_init_expr) != dk_auto)
8284             {
8285               pedwarn (capture_token->location, 0, "capture of variable "
8286                        "%qD with non-automatic storage duration",
8287                        capture_init_expr);
8288               inform (0, "%q+#D declared here", capture_init_expr);
8289               continue;
8290             }
8291
8292           capture_init_expr
8293             = finish_id_expression
8294                 (capture_id,
8295                  capture_init_expr,
8296                  parser->scope,
8297                  &idk,
8298                  /*integral_constant_expression_p=*/false,
8299                  /*allow_non_integral_constant_expression_p=*/false,
8300                  /*non_integral_constant_expression_p=*/NULL,
8301                  /*template_p=*/false,
8302                  /*done=*/true,
8303                  /*address_p=*/false,
8304                  /*template_arg_p=*/false,
8305                  &error_msg,
8306                  capture_token->location);
8307         }
8308
8309       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8310           && !explicit_init_p)
8311         {
8312           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8313               && capture_kind == BY_COPY)
8314             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8315                      "of %qD redundant with by-copy capture default",
8316                      capture_id);
8317           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8318               && capture_kind == BY_REFERENCE)
8319             pedwarn (capture_token->location, 0, "explicit by-reference "
8320                      "capture of %qD redundant with by-reference capture "
8321                      "default", capture_id);
8322         }
8323
8324       add_capture (lambda_expr,
8325                    capture_id,
8326                    capture_init_expr,
8327                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8328                    explicit_init_p);
8329     }
8330
8331   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8332 }
8333
8334 /* Parse the (optional) middle of a lambda expression.
8335
8336    lambda-declarator:
8337      ( parameter-declaration-clause [opt] )
8338        attribute-specifier [opt]
8339        mutable [opt]
8340        exception-specification [opt]
8341        lambda-return-type-clause [opt]
8342
8343    LAMBDA_EXPR is the current representation of the lambda expression.  */
8344
8345 static bool
8346 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8347 {
8348   /* 5.1.1.4 of the standard says:
8349        If a lambda-expression does not include a lambda-declarator, it is as if
8350        the lambda-declarator were ().
8351      This means an empty parameter list, no attributes, and no exception
8352      specification.  */
8353   tree param_list = void_list_node;
8354   tree attributes = NULL_TREE;
8355   tree exception_spec = NULL_TREE;
8356   tree t;
8357
8358   /* The lambda-declarator is optional, but must begin with an opening
8359      parenthesis if present.  */
8360   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8361     {
8362       cp_lexer_consume_token (parser->lexer);
8363
8364       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8365
8366       /* Parse parameters.  */
8367       param_list = cp_parser_parameter_declaration_clause (parser);
8368
8369       /* Default arguments shall not be specified in the
8370          parameter-declaration-clause of a lambda-declarator.  */
8371       for (t = param_list; t; t = TREE_CHAIN (t))
8372         if (TREE_PURPOSE (t))
8373           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_Wpedantic,
8374                    "default argument specified for lambda parameter");
8375
8376       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8377
8378       attributes = cp_parser_attributes_opt (parser);
8379
8380       /* Parse optional `mutable' keyword.  */
8381       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8382         {
8383           cp_lexer_consume_token (parser->lexer);
8384           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8385         }
8386
8387       /* Parse optional exception specification.  */
8388       exception_spec = cp_parser_exception_specification_opt (parser);
8389
8390       /* Parse optional trailing return type.  */
8391       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8392         {
8393           cp_lexer_consume_token (parser->lexer);
8394           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8395         }
8396
8397       /* The function parameters must be in scope all the way until after the
8398          trailing-return-type in case of decltype.  */
8399       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8400         pop_binding (DECL_NAME (t), t);
8401
8402       leave_scope ();
8403     }
8404
8405   /* Create the function call operator.
8406
8407      Messing with declarators like this is no uglier than building up the
8408      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8409      other code.  */
8410   {
8411     cp_decl_specifier_seq return_type_specs;
8412     cp_declarator* declarator;
8413     tree fco;
8414     int quals;
8415     void *p;
8416
8417     clear_decl_specs (&return_type_specs);
8418     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8419       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8420     else
8421       /* Maybe we will deduce the return type later.  */
8422       return_type_specs.type = make_auto ();
8423
8424     p = obstack_alloc (&declarator_obstack, 0);
8425
8426     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8427                                      sfk_none);
8428
8429     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8430              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8431     declarator = make_call_declarator (declarator, param_list, quals,
8432                                        VIRT_SPEC_UNSPECIFIED,
8433                                        exception_spec,
8434                                        /*late_return_type=*/NULL_TREE);
8435     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8436
8437     fco = grokmethod (&return_type_specs,
8438                       declarator,
8439                       attributes);
8440     if (fco != error_mark_node)
8441       {
8442         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8443         DECL_ARTIFICIAL (fco) = 1;
8444         /* Give the object parameter a different name.  */
8445         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8446       }
8447
8448     finish_member_declaration (fco);
8449
8450     obstack_free (&declarator_obstack, p);
8451
8452     return (fco != error_mark_node);
8453   }
8454 }
8455
8456 /* Parse the body of a lambda expression, which is simply
8457
8458    compound-statement
8459
8460    but which requires special handling.
8461    LAMBDA_EXPR is the current representation of the lambda expression.  */
8462
8463 static void
8464 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8465 {
8466   bool nested = (current_function_decl != NULL_TREE);
8467   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8468   if (nested)
8469     push_function_context ();
8470   else
8471     /* Still increment function_depth so that we don't GC in the
8472        middle of an expression.  */
8473     ++function_depth;
8474   /* Clear this in case we're in the middle of a default argument.  */
8475   parser->local_variables_forbidden_p = false;
8476
8477   /* Finish the function call operator
8478      - class_specifier
8479      + late_parsing_for_member
8480      + function_definition_after_declarator
8481      + ctor_initializer_opt_and_function_body  */
8482   {
8483     tree fco = lambda_function (lambda_expr);
8484     tree body;
8485     bool done = false;
8486     tree compound_stmt;
8487     tree cap;
8488
8489     /* Let the front end know that we are going to be defining this
8490        function.  */
8491     start_preparsed_function (fco,
8492                               NULL_TREE,
8493                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8494
8495     start_lambda_scope (fco);
8496     body = begin_function_body ();
8497
8498     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8499       goto out;
8500
8501     /* Push the proxies for any explicit captures.  */
8502     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8503          cap = TREE_CHAIN (cap))
8504       build_capture_proxy (TREE_PURPOSE (cap));
8505
8506     compound_stmt = begin_compound_stmt (0);
8507
8508     /* 5.1.1.4 of the standard says:
8509          If a lambda-expression does not include a trailing-return-type, it
8510          is as if the trailing-return-type denotes the following type:
8511           * if the compound-statement is of the form
8512                { return attribute-specifier [opt] expression ; }
8513              the type of the returned expression after lvalue-to-rvalue
8514              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8515              (_conv.array_ 4.2), and function-to-pointer conversion
8516              (_conv.func_ 4.3);
8517           * otherwise, void.  */
8518
8519     /* In a lambda that has neither a lambda-return-type-clause
8520        nor a deducible form, errors should be reported for return statements
8521        in the body.  Since we used void as the placeholder return type, parsing
8522        the body as usual will give such desired behavior.  */
8523     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8524         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8525         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8526       {
8527         tree expr = NULL_TREE;
8528         cp_id_kind idk = CP_ID_KIND_NONE;
8529
8530         /* Parse tentatively in case there's more after the initial return
8531            statement.  */
8532         cp_parser_parse_tentatively (parser);
8533
8534         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8535
8536         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8537
8538         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8539         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8540
8541         if (cp_parser_parse_definitely (parser))
8542           {
8543             if (!processing_template_decl)
8544               apply_deduced_return_type (fco, lambda_return_type (expr));
8545
8546             /* Will get error here if type not deduced yet.  */
8547             finish_return_stmt (expr);
8548
8549             done = true;
8550           }
8551       }
8552
8553     if (!done)
8554       {
8555         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8556           cp_parser_label_declaration (parser);
8557         cp_parser_statement_seq_opt (parser, NULL_TREE);
8558         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8559       }
8560
8561     finish_compound_stmt (compound_stmt);
8562
8563   out:
8564     finish_function_body (body);
8565     finish_lambda_scope ();
8566
8567     /* Finish the function and generate code for it if necessary.  */
8568     expand_or_defer_fn (finish_function (/*inline*/2));
8569   }
8570
8571   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8572   if (nested)
8573     pop_function_context();
8574   else
8575     --function_depth;
8576 }
8577
8578 /* Statements [gram.stmt.stmt]  */
8579
8580 /* Parse a statement.
8581
8582    statement:
8583      labeled-statement
8584      expression-statement
8585      compound-statement
8586      selection-statement
8587      iteration-statement
8588      jump-statement
8589      declaration-statement
8590      try-block
8591
8592   TM Extension:
8593
8594    statement:
8595      atomic-statement
8596
8597   IN_COMPOUND is true when the statement is nested inside a
8598   cp_parser_compound_statement; this matters for certain pragmas.
8599
8600   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8601   is a (possibly labeled) if statement which is not enclosed in braces
8602   and has an else clause.  This is used to implement -Wparentheses.  */
8603
8604 static void
8605 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8606                      bool in_compound, bool *if_p)
8607 {
8608   tree statement;
8609   cp_token *token;
8610   location_t statement_location;
8611
8612  restart:
8613   if (if_p != NULL)
8614     *if_p = false;
8615   /* There is no statement yet.  */
8616   statement = NULL_TREE;
8617   /* Peek at the next token.  */
8618   token = cp_lexer_peek_token (parser->lexer);
8619   /* Remember the location of the first token in the statement.  */
8620   statement_location = token->location;
8621   /* If this is a keyword, then that will often determine what kind of
8622      statement we have.  */
8623   if (token->type == CPP_KEYWORD)
8624     {
8625       enum rid keyword = token->keyword;
8626
8627       switch (keyword)
8628         {
8629         case RID_CASE:
8630         case RID_DEFAULT:
8631           /* Looks like a labeled-statement with a case label.
8632              Parse the label, and then use tail recursion to parse
8633              the statement.  */
8634           cp_parser_label_for_labeled_statement (parser);
8635           goto restart;
8636
8637         case RID_IF:
8638         case RID_SWITCH:
8639           statement = cp_parser_selection_statement (parser, if_p);
8640           break;
8641
8642         case RID_WHILE:
8643         case RID_DO:
8644         case RID_FOR:
8645           statement = cp_parser_iteration_statement (parser);
8646           break;
8647
8648         case RID_BREAK:
8649         case RID_CONTINUE:
8650         case RID_RETURN:
8651         case RID_GOTO:
8652           statement = cp_parser_jump_statement (parser);
8653           break;
8654
8655           /* Objective-C++ exception-handling constructs.  */
8656         case RID_AT_TRY:
8657         case RID_AT_CATCH:
8658         case RID_AT_FINALLY:
8659         case RID_AT_SYNCHRONIZED:
8660         case RID_AT_THROW:
8661           statement = cp_parser_objc_statement (parser);
8662           break;
8663
8664         case RID_TRY:
8665           statement = cp_parser_try_block (parser);
8666           break;
8667
8668         case RID_NAMESPACE:
8669           /* This must be a namespace alias definition.  */
8670           cp_parser_declaration_statement (parser);
8671           return;
8672           
8673         case RID_TRANSACTION_ATOMIC:
8674         case RID_TRANSACTION_RELAXED:
8675           statement = cp_parser_transaction (parser, keyword);
8676           break;
8677         case RID_TRANSACTION_CANCEL:
8678           statement = cp_parser_transaction_cancel (parser);
8679           break;
8680
8681         default:
8682           /* It might be a keyword like `int' that can start a
8683              declaration-statement.  */
8684           break;
8685         }
8686     }
8687   else if (token->type == CPP_NAME)
8688     {
8689       /* If the next token is a `:', then we are looking at a
8690          labeled-statement.  */
8691       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8692       if (token->type == CPP_COLON)
8693         {
8694           /* Looks like a labeled-statement with an ordinary label.
8695              Parse the label, and then use tail recursion to parse
8696              the statement.  */
8697           cp_parser_label_for_labeled_statement (parser);
8698           goto restart;
8699         }
8700     }
8701   /* Anything that starts with a `{' must be a compound-statement.  */
8702   else if (token->type == CPP_OPEN_BRACE)
8703     statement = cp_parser_compound_statement (parser, NULL, false, false);
8704   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8705      a statement all its own.  */
8706   else if (token->type == CPP_PRAGMA)
8707     {
8708       /* Only certain OpenMP pragmas are attached to statements, and thus
8709          are considered statements themselves.  All others are not.  In
8710          the context of a compound, accept the pragma as a "statement" and
8711          return so that we can check for a close brace.  Otherwise we
8712          require a real statement and must go back and read one.  */
8713       if (in_compound)
8714         cp_parser_pragma (parser, pragma_compound);
8715       else if (!cp_parser_pragma (parser, pragma_stmt))
8716         goto restart;
8717       return;
8718     }
8719   else if (token->type == CPP_EOF)
8720     {
8721       cp_parser_error (parser, "expected statement");
8722       return;
8723     }
8724
8725   /* Everything else must be a declaration-statement or an
8726      expression-statement.  Try for the declaration-statement
8727      first, unless we are looking at a `;', in which case we know that
8728      we have an expression-statement.  */
8729   if (!statement)
8730     {
8731       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8732         {
8733           cp_parser_parse_tentatively (parser);
8734           /* Try to parse the declaration-statement.  */
8735           cp_parser_declaration_statement (parser);
8736           /* If that worked, we're done.  */
8737           if (cp_parser_parse_definitely (parser))
8738             return;
8739         }
8740       /* Look for an expression-statement instead.  */
8741       statement = cp_parser_expression_statement (parser, in_statement_expr);
8742     }
8743
8744   /* Set the line number for the statement.  */
8745   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8746     SET_EXPR_LOCATION (statement, statement_location);
8747 }
8748
8749 /* Parse the label for a labeled-statement, i.e.
8750
8751    identifier :
8752    case constant-expression :
8753    default :
8754
8755    GNU Extension:
8756    case constant-expression ... constant-expression : statement
8757
8758    When a label is parsed without errors, the label is added to the
8759    parse tree by the finish_* functions, so this function doesn't
8760    have to return the label.  */
8761
8762 static void
8763 cp_parser_label_for_labeled_statement (cp_parser* parser)
8764 {
8765   cp_token *token;
8766   tree label = NULL_TREE;
8767   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8768
8769   /* The next token should be an identifier.  */
8770   token = cp_lexer_peek_token (parser->lexer);
8771   if (token->type != CPP_NAME
8772       && token->type != CPP_KEYWORD)
8773     {
8774       cp_parser_error (parser, "expected labeled-statement");
8775       return;
8776     }
8777
8778   parser->colon_corrects_to_scope_p = false;
8779   switch (token->keyword)
8780     {
8781     case RID_CASE:
8782       {
8783         tree expr, expr_hi;
8784         cp_token *ellipsis;
8785
8786         /* Consume the `case' token.  */
8787         cp_lexer_consume_token (parser->lexer);
8788         /* Parse the constant-expression.  */
8789         expr = cp_parser_constant_expression (parser,
8790                                               /*allow_non_constant_p=*/false,
8791                                               NULL);
8792
8793         ellipsis = cp_lexer_peek_token (parser->lexer);
8794         if (ellipsis->type == CPP_ELLIPSIS)
8795           {
8796             /* Consume the `...' token.  */
8797             cp_lexer_consume_token (parser->lexer);
8798             expr_hi =
8799               cp_parser_constant_expression (parser,
8800                                              /*allow_non_constant_p=*/false,
8801                                              NULL);
8802             /* We don't need to emit warnings here, as the common code
8803                will do this for us.  */
8804           }
8805         else
8806           expr_hi = NULL_TREE;
8807
8808         if (parser->in_switch_statement_p)
8809           finish_case_label (token->location, expr, expr_hi);
8810         else
8811           error_at (token->location,
8812                     "case label %qE not within a switch statement",
8813                     expr);
8814       }
8815       break;
8816
8817     case RID_DEFAULT:
8818       /* Consume the `default' token.  */
8819       cp_lexer_consume_token (parser->lexer);
8820
8821       if (parser->in_switch_statement_p)
8822         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8823       else
8824         error_at (token->location, "case label not within a switch statement");
8825       break;
8826
8827     default:
8828       /* Anything else must be an ordinary label.  */
8829       label = finish_label_stmt (cp_parser_identifier (parser));
8830       break;
8831     }
8832
8833   /* Require the `:' token.  */
8834   cp_parser_require (parser, CPP_COLON, RT_COLON);
8835
8836   /* An ordinary label may optionally be followed by attributes.
8837      However, this is only permitted if the attributes are then
8838      followed by a semicolon.  This is because, for backward
8839      compatibility, when parsing
8840        lab: __attribute__ ((unused)) int i;
8841      we want the attribute to attach to "i", not "lab".  */
8842   if (label != NULL_TREE
8843       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8844     {
8845       tree attrs;
8846
8847       cp_parser_parse_tentatively (parser);
8848       attrs = cp_parser_attributes_opt (parser);
8849       if (attrs == NULL_TREE
8850           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8851         cp_parser_abort_tentative_parse (parser);
8852       else if (!cp_parser_parse_definitely (parser))
8853         ;
8854       else
8855         cplus_decl_attributes (&label, attrs, 0);
8856     }
8857
8858   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8859 }
8860
8861 /* Parse an expression-statement.
8862
8863    expression-statement:
8864      expression [opt] ;
8865
8866    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8867    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8868    indicates whether this expression-statement is part of an
8869    expression statement.  */
8870
8871 static tree
8872 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8873 {
8874   tree statement = NULL_TREE;
8875   cp_token *token = cp_lexer_peek_token (parser->lexer);
8876
8877   /* If the next token is a ';', then there is no expression
8878      statement.  */
8879   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8880     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8881
8882   /* Give a helpful message for "A<T>::type t;" and the like.  */
8883   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8884       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8885     {
8886       if (TREE_CODE (statement) == SCOPE_REF)
8887         error_at (token->location, "need %<typename%> before %qE because "
8888                   "%qT is a dependent scope",
8889                   statement, TREE_OPERAND (statement, 0));
8890       else if (is_overloaded_fn (statement)
8891                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8892         {
8893           /* A::A a; */
8894           tree fn = get_first_fn (statement);
8895           error_at (token->location,
8896                     "%<%T::%D%> names the constructor, not the type",
8897                     DECL_CONTEXT (fn), DECL_NAME (fn));
8898         }
8899     }
8900
8901   /* Consume the final `;'.  */
8902   cp_parser_consume_semicolon_at_end_of_statement (parser);
8903
8904   if (in_statement_expr
8905       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8906     /* This is the final expression statement of a statement
8907        expression.  */
8908     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8909   else if (statement)
8910     statement = finish_expr_stmt (statement);
8911   else
8912     finish_stmt ();
8913
8914   return statement;
8915 }
8916
8917 /* Parse a compound-statement.
8918
8919    compound-statement:
8920      { statement-seq [opt] }
8921
8922    GNU extension:
8923
8924    compound-statement:
8925      { label-declaration-seq [opt] statement-seq [opt] }
8926
8927    label-declaration-seq:
8928      label-declaration
8929      label-declaration-seq label-declaration
8930
8931    Returns a tree representing the statement.  */
8932
8933 static tree
8934 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8935                               bool in_try, bool function_body)
8936 {
8937   tree compound_stmt;
8938
8939   /* Consume the `{'.  */
8940   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8941     return error_mark_node;
8942   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8943       && !function_body)
8944     pedwarn (input_location, OPT_Wpedantic,
8945              "compound-statement in constexpr function");
8946   /* Begin the compound-statement.  */
8947   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8948   /* If the next keyword is `__label__' we have a label declaration.  */
8949   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8950     cp_parser_label_declaration (parser);
8951   /* Parse an (optional) statement-seq.  */
8952   cp_parser_statement_seq_opt (parser, in_statement_expr);
8953   /* Finish the compound-statement.  */
8954   finish_compound_stmt (compound_stmt);
8955   /* Consume the `}'.  */
8956   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8957
8958   return compound_stmt;
8959 }
8960
8961 /* Parse an (optional) statement-seq.
8962
8963    statement-seq:
8964      statement
8965      statement-seq [opt] statement  */
8966
8967 static void
8968 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8969 {
8970   /* Scan statements until there aren't any more.  */
8971   while (true)
8972     {
8973       cp_token *token = cp_lexer_peek_token (parser->lexer);
8974
8975       /* If we are looking at a `}', then we have run out of
8976          statements; the same is true if we have reached the end
8977          of file, or have stumbled upon a stray '@end'.  */
8978       if (token->type == CPP_CLOSE_BRACE
8979           || token->type == CPP_EOF
8980           || token->type == CPP_PRAGMA_EOL
8981           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8982         break;
8983       
8984       /* If we are in a compound statement and find 'else' then
8985          something went wrong.  */
8986       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8987         {
8988           if (parser->in_statement & IN_IF_STMT) 
8989             break;
8990           else
8991             {
8992               token = cp_lexer_consume_token (parser->lexer);
8993               error_at (token->location, "%<else%> without a previous %<if%>");
8994             }
8995         }
8996
8997       /* Parse the statement.  */
8998       cp_parser_statement (parser, in_statement_expr, true, NULL);
8999     }
9000 }
9001
9002 /* Parse a selection-statement.
9003
9004    selection-statement:
9005      if ( condition ) statement
9006      if ( condition ) statement else statement
9007      switch ( condition ) statement
9008
9009    Returns the new IF_STMT or SWITCH_STMT.
9010
9011    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9012    is a (possibly labeled) if statement which is not enclosed in
9013    braces and has an else clause.  This is used to implement
9014    -Wparentheses.  */
9015
9016 static tree
9017 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9018 {
9019   cp_token *token;
9020   enum rid keyword;
9021
9022   if (if_p != NULL)
9023     *if_p = false;
9024
9025   /* Peek at the next token.  */
9026   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9027
9028   /* See what kind of keyword it is.  */
9029   keyword = token->keyword;
9030   switch (keyword)
9031     {
9032     case RID_IF:
9033     case RID_SWITCH:
9034       {
9035         tree statement;
9036         tree condition;
9037
9038         /* Look for the `('.  */
9039         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9040           {
9041             cp_parser_skip_to_end_of_statement (parser);
9042             return error_mark_node;
9043           }
9044
9045         /* Begin the selection-statement.  */
9046         if (keyword == RID_IF)
9047           statement = begin_if_stmt ();
9048         else
9049           statement = begin_switch_stmt ();
9050
9051         /* Parse the condition.  */
9052         condition = cp_parser_condition (parser);
9053         /* Look for the `)'.  */
9054         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9055           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9056                                                  /*consume_paren=*/true);
9057
9058         if (keyword == RID_IF)
9059           {
9060             bool nested_if;
9061             unsigned char in_statement;
9062
9063             /* Add the condition.  */
9064             finish_if_stmt_cond (condition, statement);
9065
9066             /* Parse the then-clause.  */
9067             in_statement = parser->in_statement;
9068             parser->in_statement |= IN_IF_STMT;
9069             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9070               {
9071                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9072                 add_stmt (build_empty_stmt (loc));
9073                 cp_lexer_consume_token (parser->lexer);
9074                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9075                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9076                               "empty body in an %<if%> statement");
9077                 nested_if = false;
9078               }
9079             else
9080               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9081             parser->in_statement = in_statement;
9082
9083             finish_then_clause (statement);
9084
9085             /* If the next token is `else', parse the else-clause.  */
9086             if (cp_lexer_next_token_is_keyword (parser->lexer,
9087                                                 RID_ELSE))
9088               {
9089                 /* Consume the `else' keyword.  */
9090                 cp_lexer_consume_token (parser->lexer);
9091                 begin_else_clause (statement);
9092                 /* Parse the else-clause.  */
9093                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9094                   {
9095                     location_t loc;
9096                     loc = cp_lexer_peek_token (parser->lexer)->location;
9097                     warning_at (loc,
9098                                 OPT_Wempty_body, "suggest braces around "
9099                                 "empty body in an %<else%> statement");
9100                     add_stmt (build_empty_stmt (loc));
9101                     cp_lexer_consume_token (parser->lexer);
9102                   }
9103                 else
9104                   cp_parser_implicitly_scoped_statement (parser, NULL);
9105
9106                 finish_else_clause (statement);
9107
9108                 /* If we are currently parsing a then-clause, then
9109                    IF_P will not be NULL.  We set it to true to
9110                    indicate that this if statement has an else clause.
9111                    This may trigger the Wparentheses warning below
9112                    when we get back up to the parent if statement.  */
9113                 if (if_p != NULL)
9114                   *if_p = true;
9115               }
9116             else
9117               {
9118                 /* This if statement does not have an else clause.  If
9119                    NESTED_IF is true, then the then-clause is an if
9120                    statement which does have an else clause.  We warn
9121                    about the potential ambiguity.  */
9122                 if (nested_if)
9123                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9124                               "suggest explicit braces to avoid ambiguous"
9125                               " %<else%>");
9126               }
9127
9128             /* Now we're all done with the if-statement.  */
9129             finish_if_stmt (statement);
9130           }
9131         else
9132           {
9133             bool in_switch_statement_p;
9134             unsigned char in_statement;
9135
9136             /* Add the condition.  */
9137             finish_switch_cond (condition, statement);
9138
9139             /* Parse the body of the switch-statement.  */
9140             in_switch_statement_p = parser->in_switch_statement_p;
9141             in_statement = parser->in_statement;
9142             parser->in_switch_statement_p = true;
9143             parser->in_statement |= IN_SWITCH_STMT;
9144             cp_parser_implicitly_scoped_statement (parser, NULL);
9145             parser->in_switch_statement_p = in_switch_statement_p;
9146             parser->in_statement = in_statement;
9147
9148             /* Now we're all done with the switch-statement.  */
9149             finish_switch_stmt (statement);
9150           }
9151
9152         return statement;
9153       }
9154       break;
9155
9156     default:
9157       cp_parser_error (parser, "expected selection-statement");
9158       return error_mark_node;
9159     }
9160 }
9161
9162 /* Parse a condition.
9163
9164    condition:
9165      expression
9166      type-specifier-seq declarator = initializer-clause
9167      type-specifier-seq declarator braced-init-list
9168
9169    GNU Extension:
9170
9171    condition:
9172      type-specifier-seq declarator asm-specification [opt]
9173        attributes [opt] = assignment-expression
9174
9175    Returns the expression that should be tested.  */
9176
9177 static tree
9178 cp_parser_condition (cp_parser* parser)
9179 {
9180   cp_decl_specifier_seq type_specifiers;
9181   const char *saved_message;
9182   int declares_class_or_enum;
9183
9184   /* Try the declaration first.  */
9185   cp_parser_parse_tentatively (parser);
9186   /* New types are not allowed in the type-specifier-seq for a
9187      condition.  */
9188   saved_message = parser->type_definition_forbidden_message;
9189   parser->type_definition_forbidden_message
9190     = G_("types may not be defined in conditions");
9191   /* Parse the type-specifier-seq.  */
9192   cp_parser_decl_specifier_seq (parser,
9193                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9194                                 &type_specifiers,
9195                                 &declares_class_or_enum);
9196   /* Restore the saved message.  */
9197   parser->type_definition_forbidden_message = saved_message;
9198   /* If all is well, we might be looking at a declaration.  */
9199   if (!cp_parser_error_occurred (parser))
9200     {
9201       tree decl;
9202       tree asm_specification;
9203       tree attributes;
9204       cp_declarator *declarator;
9205       tree initializer = NULL_TREE;
9206
9207       /* Parse the declarator.  */
9208       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9209                                          /*ctor_dtor_or_conv_p=*/NULL,
9210                                          /*parenthesized_p=*/NULL,
9211                                          /*member_p=*/false);
9212       /* Parse the attributes.  */
9213       attributes = cp_parser_attributes_opt (parser);
9214       /* Parse the asm-specification.  */
9215       asm_specification = cp_parser_asm_specification_opt (parser);
9216       /* If the next token is not an `=' or '{', then we might still be
9217          looking at an expression.  For example:
9218
9219            if (A(a).x)
9220
9221          looks like a decl-specifier-seq and a declarator -- but then
9222          there is no `=', so this is an expression.  */
9223       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9224           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9225         cp_parser_simulate_error (parser);
9226         
9227       /* If we did see an `=' or '{', then we are looking at a declaration
9228          for sure.  */
9229       if (cp_parser_parse_definitely (parser))
9230         {
9231           tree pushed_scope;
9232           bool non_constant_p;
9233           bool flags = LOOKUP_ONLYCONVERTING;
9234
9235           /* Create the declaration.  */
9236           decl = start_decl (declarator, &type_specifiers,
9237                              /*initialized_p=*/true,
9238                              attributes, /*prefix_attributes=*/NULL_TREE,
9239                              &pushed_scope);
9240
9241           /* Parse the initializer.  */
9242           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9243             {
9244               initializer = cp_parser_braced_list (parser, &non_constant_p);
9245               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9246               flags = 0;
9247             }
9248           else
9249             {
9250               /* Consume the `='.  */
9251               cp_parser_require (parser, CPP_EQ, RT_EQ);
9252               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9253             }
9254           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9255             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9256
9257           /* Process the initializer.  */
9258           cp_finish_decl (decl,
9259                           initializer, !non_constant_p,
9260                           asm_specification,
9261                           flags);
9262
9263           if (pushed_scope)
9264             pop_scope (pushed_scope);
9265
9266           return convert_from_reference (decl);
9267         }
9268     }
9269   /* If we didn't even get past the declarator successfully, we are
9270      definitely not looking at a declaration.  */
9271   else
9272     cp_parser_abort_tentative_parse (parser);
9273
9274   /* Otherwise, we are looking at an expression.  */
9275   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9276 }
9277
9278 /* Parses a for-statement or range-for-statement until the closing ')',
9279    not included. */
9280
9281 static tree
9282 cp_parser_for (cp_parser *parser)
9283 {
9284   tree init, scope, decl;
9285   bool is_range_for;
9286
9287   /* Begin the for-statement.  */
9288   scope = begin_for_scope (&init);
9289
9290   /* Parse the initialization.  */
9291   is_range_for = cp_parser_for_init_statement (parser, &decl);
9292
9293   if (is_range_for)
9294     return cp_parser_range_for (parser, scope, init, decl);
9295   else
9296     return cp_parser_c_for (parser, scope, init);
9297 }
9298
9299 static tree
9300 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9301 {
9302   /* Normal for loop */
9303   tree condition = NULL_TREE;
9304   tree expression = NULL_TREE;
9305   tree stmt;
9306
9307   stmt = begin_for_stmt (scope, init);
9308   /* The for-init-statement has already been parsed in
9309      cp_parser_for_init_statement, so no work is needed here.  */
9310   finish_for_init_stmt (stmt);
9311
9312   /* If there's a condition, process it.  */
9313   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9314     condition = cp_parser_condition (parser);
9315   finish_for_cond (condition, stmt);
9316   /* Look for the `;'.  */
9317   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9318
9319   /* If there's an expression, process it.  */
9320   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9321     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9322   finish_for_expr (expression, stmt);
9323
9324   return stmt;
9325 }
9326
9327 /* Tries to parse a range-based for-statement:
9328
9329   range-based-for:
9330     decl-specifier-seq declarator : expression
9331
9332   The decl-specifier-seq declarator and the `:' are already parsed by
9333   cp_parser_for_init_statement. If processing_template_decl it returns a
9334   newly created RANGE_FOR_STMT; if not, it is converted to a
9335   regular FOR_STMT.  */
9336
9337 static tree
9338 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9339 {
9340   tree stmt, range_expr;
9341
9342   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9343     {
9344       bool expr_non_constant_p;
9345       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9346     }
9347   else
9348     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9349
9350   /* If in template, STMT is converted to a normal for-statement
9351      at instantiation. If not, it is done just ahead. */
9352   if (processing_template_decl)
9353     {
9354       if (check_for_bare_parameter_packs (range_expr))
9355         range_expr = error_mark_node;
9356       stmt = begin_range_for_stmt (scope, init);
9357       finish_range_for_decl (stmt, range_decl, range_expr);
9358       if (!type_dependent_expression_p (range_expr)
9359           /* do_auto_deduction doesn't mess with template init-lists.  */
9360           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9361         do_range_for_auto_deduction (range_decl, range_expr);
9362     }
9363   else
9364     {
9365       stmt = begin_for_stmt (scope, init);
9366       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9367     }
9368   return stmt;
9369 }
9370
9371 /* Subroutine of cp_convert_range_for: given the initializer expression,
9372    builds up the range temporary.  */
9373
9374 static tree
9375 build_range_temp (tree range_expr)
9376 {
9377   tree range_type, range_temp;
9378
9379   /* Find out the type deduced by the declaration
9380      `auto &&__range = range_expr'.  */
9381   range_type = cp_build_reference_type (make_auto (), true);
9382   range_type = do_auto_deduction (range_type, range_expr,
9383                                   type_uses_auto (range_type));
9384
9385   /* Create the __range variable.  */
9386   range_temp = build_decl (input_location, VAR_DECL,
9387                            get_identifier ("__for_range"), range_type);
9388   TREE_USED (range_temp) = 1;
9389   DECL_ARTIFICIAL (range_temp) = 1;
9390
9391   return range_temp;
9392 }
9393
9394 /* Used by cp_parser_range_for in template context: we aren't going to
9395    do a full conversion yet, but we still need to resolve auto in the
9396    type of the for-range-declaration if present.  This is basically
9397    a shortcut version of cp_convert_range_for.  */
9398
9399 static void
9400 do_range_for_auto_deduction (tree decl, tree range_expr)
9401 {
9402   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9403   if (auto_node)
9404     {
9405       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9406       range_temp = convert_from_reference (build_range_temp (range_expr));
9407       iter_type = (cp_parser_perform_range_for_lookup
9408                    (range_temp, &begin_dummy, &end_dummy));
9409       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9410       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9411                                         tf_warning_or_error);
9412       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9413                                             iter_decl, auto_node);
9414     }
9415 }
9416
9417 /* Converts a range-based for-statement into a normal
9418    for-statement, as per the definition.
9419
9420       for (RANGE_DECL : RANGE_EXPR)
9421         BLOCK
9422
9423    should be equivalent to:
9424
9425       {
9426         auto &&__range = RANGE_EXPR;
9427         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9428               __begin != __end;
9429               ++__begin)
9430           {
9431               RANGE_DECL = *__begin;
9432               BLOCK
9433           }
9434       }
9435
9436    If RANGE_EXPR is an array:
9437         BEGIN_EXPR = __range
9438         END_EXPR = __range + ARRAY_SIZE(__range)
9439    Else if RANGE_EXPR has a member 'begin' or 'end':
9440         BEGIN_EXPR = __range.begin()
9441         END_EXPR = __range.end()
9442    Else:
9443         BEGIN_EXPR = begin(__range)
9444         END_EXPR = end(__range);
9445
9446    If __range has a member 'begin' but not 'end', or vice versa, we must
9447    still use the second alternative (it will surely fail, however).
9448    When calling begin()/end() in the third alternative we must use
9449    argument dependent lookup, but always considering 'std' as an associated
9450    namespace.  */
9451
9452 tree
9453 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9454 {
9455   tree begin, end;
9456   tree iter_type, begin_expr, end_expr;
9457   tree condition, expression;
9458
9459   if (range_decl == error_mark_node || range_expr == error_mark_node)
9460     /* If an error happened previously do nothing or else a lot of
9461        unhelpful errors would be issued.  */
9462     begin_expr = end_expr = iter_type = error_mark_node;
9463   else
9464     {
9465       tree range_temp = build_range_temp (range_expr);
9466       pushdecl (range_temp);
9467       cp_finish_decl (range_temp, range_expr,
9468                       /*is_constant_init*/false, NULL_TREE,
9469                       LOOKUP_ONLYCONVERTING);
9470
9471       range_temp = convert_from_reference (range_temp);
9472       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9473                                                       &begin_expr, &end_expr);
9474     }
9475
9476   /* The new for initialization statement.  */
9477   begin = build_decl (input_location, VAR_DECL,
9478                       get_identifier ("__for_begin"), iter_type);
9479   TREE_USED (begin) = 1;
9480   DECL_ARTIFICIAL (begin) = 1;
9481   pushdecl (begin);
9482   cp_finish_decl (begin, begin_expr,
9483                   /*is_constant_init*/false, NULL_TREE,
9484                   LOOKUP_ONLYCONVERTING);
9485
9486   end = build_decl (input_location, VAR_DECL,
9487                     get_identifier ("__for_end"), iter_type);
9488   TREE_USED (end) = 1;
9489   DECL_ARTIFICIAL (end) = 1;
9490   pushdecl (end);
9491   cp_finish_decl (end, end_expr,
9492                   /*is_constant_init*/false, NULL_TREE,
9493                   LOOKUP_ONLYCONVERTING);
9494
9495   finish_for_init_stmt (statement);
9496
9497   /* The new for condition.  */
9498   condition = build_x_binary_op (NE_EXPR,
9499                                  begin, ERROR_MARK,
9500                                  end, ERROR_MARK,
9501                                  NULL, tf_warning_or_error);
9502   finish_for_cond (condition, statement);
9503
9504   /* The new increment expression.  */
9505   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9506   finish_for_expr (expression, statement);
9507
9508   /* The declaration is initialized with *__begin inside the loop body.  */
9509   cp_finish_decl (range_decl,
9510                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9511                   /*is_constant_init*/false, NULL_TREE,
9512                   LOOKUP_ONLYCONVERTING);
9513
9514   return statement;
9515 }
9516
9517 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9518    We need to solve both at the same time because the method used
9519    depends on the existence of members begin or end.
9520    Returns the type deduced for the iterator expression.  */
9521
9522 static tree
9523 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9524 {
9525   if (error_operand_p (range))
9526     {
9527       *begin = *end = error_mark_node;
9528       return error_mark_node;
9529     }
9530
9531   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9532     {
9533       error ("range-based %<for%> expression of type %qT "
9534              "has incomplete type", TREE_TYPE (range));
9535       *begin = *end = error_mark_node;
9536       return error_mark_node;
9537     }
9538   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9539     {
9540       /* If RANGE is an array, we will use pointer arithmetic.  */
9541       *begin = range;
9542       *end = build_binary_op (input_location, PLUS_EXPR,
9543                               range,
9544                               array_type_nelts_top (TREE_TYPE (range)),
9545                               0);
9546       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9547     }
9548   else
9549     {
9550       /* If it is not an array, we must do a bit of magic.  */
9551       tree id_begin, id_end;
9552       tree member_begin, member_end;
9553
9554       *begin = *end = error_mark_node;
9555
9556       id_begin = get_identifier ("begin");
9557       id_end = get_identifier ("end");
9558       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9559                                     /*protect=*/2, /*want_type=*/false,
9560                                     tf_warning_or_error);
9561       member_end = lookup_member (TREE_TYPE (range), id_end,
9562                                   /*protect=*/2, /*want_type=*/false,
9563                                   tf_warning_or_error);
9564
9565       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9566         {
9567           /* Use the member functions.  */
9568           if (member_begin != NULL_TREE)
9569             *begin = cp_parser_range_for_member_function (range, id_begin);
9570           else
9571             error ("range-based %<for%> expression of type %qT has an "
9572                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9573
9574           if (member_end != NULL_TREE)
9575             *end = cp_parser_range_for_member_function (range, id_end);
9576           else
9577             error ("range-based %<for%> expression of type %qT has a "
9578                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9579         }
9580       else
9581         {
9582           /* Use global functions with ADL.  */
9583           VEC(tree,gc) *vec;
9584           vec = make_tree_vector ();
9585
9586           VEC_safe_push (tree, gc, vec, range);
9587
9588           member_begin = perform_koenig_lookup (id_begin, vec,
9589                                                 /*include_std=*/true,
9590                                                 tf_warning_or_error);
9591           *begin = finish_call_expr (member_begin, &vec, false, true,
9592                                      tf_warning_or_error);
9593           member_end = perform_koenig_lookup (id_end, vec,
9594                                               /*include_std=*/true,
9595                                               tf_warning_or_error);
9596           *end = finish_call_expr (member_end, &vec, false, true,
9597                                    tf_warning_or_error);
9598
9599           release_tree_vector (vec);
9600         }
9601
9602       /* Last common checks.  */
9603       if (*begin == error_mark_node || *end == error_mark_node)
9604         {
9605           /* If one of the expressions is an error do no more checks.  */
9606           *begin = *end = error_mark_node;
9607           return error_mark_node;
9608         }
9609       else
9610         {
9611           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9612           /* The unqualified type of the __begin and __end temporaries should
9613              be the same, as required by the multiple auto declaration.  */
9614           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9615             error ("inconsistent begin/end types in range-based %<for%> "
9616                    "statement: %qT and %qT",
9617                    TREE_TYPE (*begin), TREE_TYPE (*end));
9618           return iter_type;
9619         }
9620     }
9621 }
9622
9623 /* Helper function for cp_parser_perform_range_for_lookup.
9624    Builds a tree for RANGE.IDENTIFIER().  */
9625
9626 static tree
9627 cp_parser_range_for_member_function (tree range, tree identifier)
9628 {
9629   tree member, res;
9630   VEC(tree,gc) *vec;
9631
9632   member = finish_class_member_access_expr (range, identifier,
9633                                             false, tf_warning_or_error);
9634   if (member == error_mark_node)
9635     return error_mark_node;
9636
9637   vec = make_tree_vector ();
9638   res = finish_call_expr (member, &vec,
9639                           /*disallow_virtual=*/false,
9640                           /*koenig_p=*/false,
9641                           tf_warning_or_error);
9642   release_tree_vector (vec);
9643   return res;
9644 }
9645
9646 /* Parse an iteration-statement.
9647
9648    iteration-statement:
9649      while ( condition ) statement
9650      do statement while ( expression ) ;
9651      for ( for-init-statement condition [opt] ; expression [opt] )
9652        statement
9653
9654    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9655
9656 static tree
9657 cp_parser_iteration_statement (cp_parser* parser)
9658 {
9659   cp_token *token;
9660   enum rid keyword;
9661   tree statement;
9662   unsigned char in_statement;
9663
9664   /* Peek at the next token.  */
9665   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9666   if (!token)
9667     return error_mark_node;
9668
9669   /* Remember whether or not we are already within an iteration
9670      statement.  */
9671   in_statement = parser->in_statement;
9672
9673   /* See what kind of keyword it is.  */
9674   keyword = token->keyword;
9675   switch (keyword)
9676     {
9677     case RID_WHILE:
9678       {
9679         tree condition;
9680
9681         /* Begin the while-statement.  */
9682         statement = begin_while_stmt ();
9683         /* Look for the `('.  */
9684         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9685         /* Parse the condition.  */
9686         condition = cp_parser_condition (parser);
9687         finish_while_stmt_cond (condition, statement);
9688         /* Look for the `)'.  */
9689         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9690         /* Parse the dependent statement.  */
9691         parser->in_statement = IN_ITERATION_STMT;
9692         cp_parser_already_scoped_statement (parser);
9693         parser->in_statement = in_statement;
9694         /* We're done with the while-statement.  */
9695         finish_while_stmt (statement);
9696       }
9697       break;
9698
9699     case RID_DO:
9700       {
9701         tree expression;
9702
9703         /* Begin the do-statement.  */
9704         statement = begin_do_stmt ();
9705         /* Parse the body of the do-statement.  */
9706         parser->in_statement = IN_ITERATION_STMT;
9707         cp_parser_implicitly_scoped_statement (parser, NULL);
9708         parser->in_statement = in_statement;
9709         finish_do_body (statement);
9710         /* Look for the `while' keyword.  */
9711         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9712         /* Look for the `('.  */
9713         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9714         /* Parse the expression.  */
9715         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9716         /* We're done with the do-statement.  */
9717         finish_do_stmt (expression, statement);
9718         /* Look for the `)'.  */
9719         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9720         /* Look for the `;'.  */
9721         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9722       }
9723       break;
9724
9725     case RID_FOR:
9726       {
9727         /* Look for the `('.  */
9728         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9729
9730         statement = cp_parser_for (parser);
9731
9732         /* Look for the `)'.  */
9733         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9734
9735         /* Parse the body of the for-statement.  */
9736         parser->in_statement = IN_ITERATION_STMT;
9737         cp_parser_already_scoped_statement (parser);
9738         parser->in_statement = in_statement;
9739
9740         /* We're done with the for-statement.  */
9741         finish_for_stmt (statement);
9742       }
9743       break;
9744
9745     default:
9746       cp_parser_error (parser, "expected iteration-statement");
9747       statement = error_mark_node;
9748       break;
9749     }
9750
9751   return statement;
9752 }
9753
9754 /* Parse a for-init-statement or the declarator of a range-based-for.
9755    Returns true if a range-based-for declaration is seen.
9756
9757    for-init-statement:
9758      expression-statement
9759      simple-declaration  */
9760
9761 static bool
9762 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9763 {
9764   /* If the next token is a `;', then we have an empty
9765      expression-statement.  Grammatically, this is also a
9766      simple-declaration, but an invalid one, because it does not
9767      declare anything.  Therefore, if we did not handle this case
9768      specially, we would issue an error message about an invalid
9769      declaration.  */
9770   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9771     {
9772       bool is_range_for = false;
9773       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9774
9775       parser->colon_corrects_to_scope_p = false;
9776
9777       /* We're going to speculatively look for a declaration, falling back
9778          to an expression, if necessary.  */
9779       cp_parser_parse_tentatively (parser);
9780       /* Parse the declaration.  */
9781       cp_parser_simple_declaration (parser,
9782                                     /*function_definition_allowed_p=*/false,
9783                                     decl);
9784       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9785       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9786         {
9787           /* It is a range-for, consume the ':' */
9788           cp_lexer_consume_token (parser->lexer);
9789           is_range_for = true;
9790           if (cxx_dialect < cxx0x)
9791             {
9792               error_at (cp_lexer_peek_token (parser->lexer)->location,
9793                         "range-based %<for%> loops are not allowed "
9794                         "in C++98 mode");
9795               *decl = error_mark_node;
9796             }
9797         }
9798       else
9799           /* The ';' is not consumed yet because we told
9800              cp_parser_simple_declaration not to.  */
9801           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9802
9803       if (cp_parser_parse_definitely (parser))
9804         return is_range_for;
9805       /* If the tentative parse failed, then we shall need to look for an
9806          expression-statement.  */
9807     }
9808   /* If we are here, it is an expression-statement.  */
9809   cp_parser_expression_statement (parser, NULL_TREE);
9810   return false;
9811 }
9812
9813 /* Parse a jump-statement.
9814
9815    jump-statement:
9816      break ;
9817      continue ;
9818      return expression [opt] ;
9819      return braced-init-list ;
9820      goto identifier ;
9821
9822    GNU extension:
9823
9824    jump-statement:
9825      goto * expression ;
9826
9827    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9828
9829 static tree
9830 cp_parser_jump_statement (cp_parser* parser)
9831 {
9832   tree statement = error_mark_node;
9833   cp_token *token;
9834   enum rid keyword;
9835   unsigned char in_statement;
9836
9837   /* Peek at the next token.  */
9838   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9839   if (!token)
9840     return error_mark_node;
9841
9842   /* See what kind of keyword it is.  */
9843   keyword = token->keyword;
9844   switch (keyword)
9845     {
9846     case RID_BREAK:
9847       in_statement = parser->in_statement & ~IN_IF_STMT;      
9848       switch (in_statement)
9849         {
9850         case 0:
9851           error_at (token->location, "break statement not within loop or switch");
9852           break;
9853         default:
9854           gcc_assert ((in_statement & IN_SWITCH_STMT)
9855                       || in_statement == IN_ITERATION_STMT);
9856           statement = finish_break_stmt ();
9857           break;
9858         case IN_OMP_BLOCK:
9859           error_at (token->location, "invalid exit from OpenMP structured block");
9860           break;
9861         case IN_OMP_FOR:
9862           error_at (token->location, "break statement used with OpenMP for loop");
9863           break;
9864         }
9865       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9866       break;
9867
9868     case RID_CONTINUE:
9869       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9870         {
9871         case 0:
9872           error_at (token->location, "continue statement not within a loop");
9873           break;
9874         case IN_ITERATION_STMT:
9875         case IN_OMP_FOR:
9876           statement = finish_continue_stmt ();
9877           break;
9878         case IN_OMP_BLOCK:
9879           error_at (token->location, "invalid exit from OpenMP structured block");
9880           break;
9881         default:
9882           gcc_unreachable ();
9883         }
9884       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9885       break;
9886
9887     case RID_RETURN:
9888       {
9889         tree expr;
9890         bool expr_non_constant_p;
9891
9892         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9893           {
9894             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9895             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9896           }
9897         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9898           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9899         else
9900           /* If the next token is a `;', then there is no
9901              expression.  */
9902           expr = NULL_TREE;
9903         /* Build the return-statement.  */
9904         statement = finish_return_stmt (expr);
9905         /* Look for the final `;'.  */
9906         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9907       }
9908       break;
9909
9910     case RID_GOTO:
9911       /* Create the goto-statement.  */
9912       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9913         {
9914           /* Issue a warning about this use of a GNU extension.  */
9915           pedwarn (token->location, OPT_Wpedantic, "ISO C++ forbids computed gotos");
9916           /* Consume the '*' token.  */
9917           cp_lexer_consume_token (parser->lexer);
9918           /* Parse the dependent expression.  */
9919           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9920         }
9921       else
9922         finish_goto_stmt (cp_parser_identifier (parser));
9923       /* Look for the final `;'.  */
9924       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9925       break;
9926
9927     default:
9928       cp_parser_error (parser, "expected jump-statement");
9929       break;
9930     }
9931
9932   return statement;
9933 }
9934
9935 /* Parse a declaration-statement.
9936
9937    declaration-statement:
9938      block-declaration  */
9939
9940 static void
9941 cp_parser_declaration_statement (cp_parser* parser)
9942 {
9943   void *p;
9944
9945   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9946   p = obstack_alloc (&declarator_obstack, 0);
9947
9948  /* Parse the block-declaration.  */
9949   cp_parser_block_declaration (parser, /*statement_p=*/true);
9950
9951   /* Free any declarators allocated.  */
9952   obstack_free (&declarator_obstack, p);
9953
9954   /* Finish off the statement.  */
9955   finish_stmt ();
9956 }
9957
9958 /* Some dependent statements (like `if (cond) statement'), are
9959    implicitly in their own scope.  In other words, if the statement is
9960    a single statement (as opposed to a compound-statement), it is
9961    none-the-less treated as if it were enclosed in braces.  Any
9962    declarations appearing in the dependent statement are out of scope
9963    after control passes that point.  This function parses a statement,
9964    but ensures that is in its own scope, even if it is not a
9965    compound-statement.
9966
9967    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9968    is a (possibly labeled) if statement which is not enclosed in
9969    braces and has an else clause.  This is used to implement
9970    -Wparentheses.
9971
9972    Returns the new statement.  */
9973
9974 static tree
9975 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9976 {
9977   tree statement;
9978
9979   if (if_p != NULL)
9980     *if_p = false;
9981
9982   /* Mark if () ; with a special NOP_EXPR.  */
9983   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9984     {
9985       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9986       cp_lexer_consume_token (parser->lexer);
9987       statement = add_stmt (build_empty_stmt (loc));
9988     }
9989   /* if a compound is opened, we simply parse the statement directly.  */
9990   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9991     statement = cp_parser_compound_statement (parser, NULL, false, false);
9992   /* If the token is not a `{', then we must take special action.  */
9993   else
9994     {
9995       /* Create a compound-statement.  */
9996       statement = begin_compound_stmt (0);
9997       /* Parse the dependent-statement.  */
9998       cp_parser_statement (parser, NULL_TREE, false, if_p);
9999       /* Finish the dummy compound-statement.  */
10000       finish_compound_stmt (statement);
10001     }
10002
10003   /* Return the statement.  */
10004   return statement;
10005 }
10006
10007 /* For some dependent statements (like `while (cond) statement'), we
10008    have already created a scope.  Therefore, even if the dependent
10009    statement is a compound-statement, we do not want to create another
10010    scope.  */
10011
10012 static void
10013 cp_parser_already_scoped_statement (cp_parser* parser)
10014 {
10015   /* If the token is a `{', then we must take special action.  */
10016   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10017     cp_parser_statement (parser, NULL_TREE, false, NULL);
10018   else
10019     {
10020       /* Avoid calling cp_parser_compound_statement, so that we
10021          don't create a new scope.  Do everything else by hand.  */
10022       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10023       /* If the next keyword is `__label__' we have a label declaration.  */
10024       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10025         cp_parser_label_declaration (parser);
10026       /* Parse an (optional) statement-seq.  */
10027       cp_parser_statement_seq_opt (parser, NULL_TREE);
10028       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10029     }
10030 }
10031
10032 /* Declarations [gram.dcl.dcl] */
10033
10034 /* Parse an optional declaration-sequence.
10035
10036    declaration-seq:
10037      declaration
10038      declaration-seq declaration  */
10039
10040 static void
10041 cp_parser_declaration_seq_opt (cp_parser* parser)
10042 {
10043   while (true)
10044     {
10045       cp_token *token;
10046
10047       token = cp_lexer_peek_token (parser->lexer);
10048
10049       if (token->type == CPP_CLOSE_BRACE
10050           || token->type == CPP_EOF
10051           || token->type == CPP_PRAGMA_EOL)
10052         break;
10053
10054       if (token->type == CPP_SEMICOLON)
10055         {
10056           /* A declaration consisting of a single semicolon is
10057              invalid.  Allow it unless we're being pedantic.  */
10058           cp_lexer_consume_token (parser->lexer);
10059           if (!in_system_header)
10060             pedwarn (input_location, OPT_Wpedantic, "extra %<;%>");
10061           continue;
10062         }
10063
10064       /* If we're entering or exiting a region that's implicitly
10065          extern "C", modify the lang context appropriately.  */
10066       if (!parser->implicit_extern_c && token->implicit_extern_c)
10067         {
10068           push_lang_context (lang_name_c);
10069           parser->implicit_extern_c = true;
10070         }
10071       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10072         {
10073           pop_lang_context ();
10074           parser->implicit_extern_c = false;
10075         }
10076
10077       if (token->type == CPP_PRAGMA)
10078         {
10079           /* A top-level declaration can consist solely of a #pragma.
10080              A nested declaration cannot, so this is done here and not
10081              in cp_parser_declaration.  (A #pragma at block scope is
10082              handled in cp_parser_statement.)  */
10083           cp_parser_pragma (parser, pragma_external);
10084           continue;
10085         }
10086
10087       /* Parse the declaration itself.  */
10088       cp_parser_declaration (parser);
10089     }
10090 }
10091
10092 /* Parse a declaration.
10093
10094    declaration:
10095      block-declaration
10096      function-definition
10097      template-declaration
10098      explicit-instantiation
10099      explicit-specialization
10100      linkage-specification
10101      namespace-definition
10102
10103    GNU extension:
10104
10105    declaration:
10106       __extension__ declaration */
10107
10108 static void
10109 cp_parser_declaration (cp_parser* parser)
10110 {
10111   cp_token token1;
10112   cp_token token2;
10113   int saved_pedantic;
10114   void *p;
10115   tree attributes = NULL_TREE;
10116
10117   /* Check for the `__extension__' keyword.  */
10118   if (cp_parser_extension_opt (parser, &saved_pedantic))
10119     {
10120       /* Parse the qualified declaration.  */
10121       cp_parser_declaration (parser);
10122       /* Restore the PEDANTIC flag.  */
10123       pedantic = saved_pedantic;
10124
10125       return;
10126     }
10127
10128   /* Try to figure out what kind of declaration is present.  */
10129   token1 = *cp_lexer_peek_token (parser->lexer);
10130
10131   if (token1.type != CPP_EOF)
10132     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10133   else
10134     {
10135       token2.type = CPP_EOF;
10136       token2.keyword = RID_MAX;
10137     }
10138
10139   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10140   p = obstack_alloc (&declarator_obstack, 0);
10141
10142   /* If the next token is `extern' and the following token is a string
10143      literal, then we have a linkage specification.  */
10144   if (token1.keyword == RID_EXTERN
10145       && cp_parser_is_pure_string_literal (&token2))
10146     cp_parser_linkage_specification (parser);
10147   /* If the next token is `template', then we have either a template
10148      declaration, an explicit instantiation, or an explicit
10149      specialization.  */
10150   else if (token1.keyword == RID_TEMPLATE)
10151     {
10152       /* `template <>' indicates a template specialization.  */
10153       if (token2.type == CPP_LESS
10154           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10155         cp_parser_explicit_specialization (parser);
10156       /* `template <' indicates a template declaration.  */
10157       else if (token2.type == CPP_LESS)
10158         cp_parser_template_declaration (parser, /*member_p=*/false);
10159       /* Anything else must be an explicit instantiation.  */
10160       else
10161         cp_parser_explicit_instantiation (parser);
10162     }
10163   /* If the next token is `export', then we have a template
10164      declaration.  */
10165   else if (token1.keyword == RID_EXPORT)
10166     cp_parser_template_declaration (parser, /*member_p=*/false);
10167   /* If the next token is `extern', 'static' or 'inline' and the one
10168      after that is `template', we have a GNU extended explicit
10169      instantiation directive.  */
10170   else if (cp_parser_allow_gnu_extensions_p (parser)
10171            && (token1.keyword == RID_EXTERN
10172                || token1.keyword == RID_STATIC
10173                || token1.keyword == RID_INLINE)
10174            && token2.keyword == RID_TEMPLATE)
10175     cp_parser_explicit_instantiation (parser);
10176   /* If the next token is `namespace', check for a named or unnamed
10177      namespace definition.  */
10178   else if (token1.keyword == RID_NAMESPACE
10179            && (/* A named namespace definition.  */
10180                (token2.type == CPP_NAME
10181                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10182                     != CPP_EQ))
10183                /* An unnamed namespace definition.  */
10184                || token2.type == CPP_OPEN_BRACE
10185                || token2.keyword == RID_ATTRIBUTE))
10186     cp_parser_namespace_definition (parser);
10187   /* An inline (associated) namespace definition.  */
10188   else if (token1.keyword == RID_INLINE
10189            && token2.keyword == RID_NAMESPACE)
10190     cp_parser_namespace_definition (parser);
10191   /* Objective-C++ declaration/definition.  */
10192   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10193     cp_parser_objc_declaration (parser, NULL_TREE);
10194   else if (c_dialect_objc ()
10195            && token1.keyword == RID_ATTRIBUTE
10196            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10197     cp_parser_objc_declaration (parser, attributes);
10198   /* We must have either a block declaration or a function
10199      definition.  */
10200   else
10201     /* Try to parse a block-declaration, or a function-definition.  */
10202     cp_parser_block_declaration (parser, /*statement_p=*/false);
10203
10204   /* Free any declarators allocated.  */
10205   obstack_free (&declarator_obstack, p);
10206 }
10207
10208 /* Parse a block-declaration.
10209
10210    block-declaration:
10211      simple-declaration
10212      asm-definition
10213      namespace-alias-definition
10214      using-declaration
10215      using-directive
10216
10217    GNU Extension:
10218
10219    block-declaration:
10220      __extension__ block-declaration
10221
10222    C++0x Extension:
10223
10224    block-declaration:
10225      static_assert-declaration
10226
10227    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10228    part of a declaration-statement.  */
10229
10230 static void
10231 cp_parser_block_declaration (cp_parser *parser,
10232                              bool      statement_p)
10233 {
10234   cp_token *token1;
10235   int saved_pedantic;
10236
10237   /* Check for the `__extension__' keyword.  */
10238   if (cp_parser_extension_opt (parser, &saved_pedantic))
10239     {
10240       /* Parse the qualified declaration.  */
10241       cp_parser_block_declaration (parser, statement_p);
10242       /* Restore the PEDANTIC flag.  */
10243       pedantic = saved_pedantic;
10244
10245       return;
10246     }
10247
10248   /* Peek at the next token to figure out which kind of declaration is
10249      present.  */
10250   token1 = cp_lexer_peek_token (parser->lexer);
10251
10252   /* If the next keyword is `asm', we have an asm-definition.  */
10253   if (token1->keyword == RID_ASM)
10254     {
10255       if (statement_p)
10256         cp_parser_commit_to_tentative_parse (parser);
10257       cp_parser_asm_definition (parser);
10258     }
10259   /* If the next keyword is `namespace', we have a
10260      namespace-alias-definition.  */
10261   else if (token1->keyword == RID_NAMESPACE)
10262     cp_parser_namespace_alias_definition (parser);
10263   /* If the next keyword is `using', we have a
10264      using-declaration, a using-directive, or an alias-declaration.  */
10265   else if (token1->keyword == RID_USING)
10266     {
10267       cp_token *token2;
10268
10269       if (statement_p)
10270         cp_parser_commit_to_tentative_parse (parser);
10271       /* If the token after `using' is `namespace', then we have a
10272          using-directive.  */
10273       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10274       if (token2->keyword == RID_NAMESPACE)
10275         cp_parser_using_directive (parser);
10276       /* If the second token after 'using' is '=', then we have an
10277          alias-declaration.  */
10278       else if (cxx_dialect >= cxx0x
10279                && token2->type == CPP_NAME
10280                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10281                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10282                        == RID_ATTRIBUTE)))
10283         cp_parser_alias_declaration (parser);
10284       /* Otherwise, it's a using-declaration.  */
10285       else
10286         cp_parser_using_declaration (parser,
10287                                      /*access_declaration_p=*/false);
10288     }
10289   /* If the next keyword is `__label__' we have a misplaced label
10290      declaration.  */
10291   else if (token1->keyword == RID_LABEL)
10292     {
10293       cp_lexer_consume_token (parser->lexer);
10294       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10295       cp_parser_skip_to_end_of_statement (parser);
10296       /* If the next token is now a `;', consume it.  */
10297       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10298         cp_lexer_consume_token (parser->lexer);
10299     }
10300   /* If the next token is `static_assert' we have a static assertion.  */
10301   else if (token1->keyword == RID_STATIC_ASSERT)
10302     cp_parser_static_assert (parser, /*member_p=*/false);
10303   /* Anything else must be a simple-declaration.  */
10304   else
10305     cp_parser_simple_declaration (parser, !statement_p,
10306                                   /*maybe_range_for_decl*/NULL);
10307 }
10308
10309 /* Parse a simple-declaration.
10310
10311    simple-declaration:
10312      decl-specifier-seq [opt] init-declarator-list [opt] ;
10313
10314    init-declarator-list:
10315      init-declarator
10316      init-declarator-list , init-declarator
10317
10318    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10319    function-definition as a simple-declaration.
10320
10321    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10322    parsed declaration if it is an uninitialized single declarator not followed
10323    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10324    if present, will not be consumed.  */
10325
10326 static void
10327 cp_parser_simple_declaration (cp_parser* parser,
10328                               bool function_definition_allowed_p,
10329                               tree *maybe_range_for_decl)
10330 {
10331   cp_decl_specifier_seq decl_specifiers;
10332   int declares_class_or_enum;
10333   bool saw_declarator;
10334
10335   if (maybe_range_for_decl)
10336     *maybe_range_for_decl = NULL_TREE;
10337
10338   /* Defer access checks until we know what is being declared; the
10339      checks for names appearing in the decl-specifier-seq should be
10340      done as if we were in the scope of the thing being declared.  */
10341   push_deferring_access_checks (dk_deferred);
10342
10343   /* Parse the decl-specifier-seq.  We have to keep track of whether
10344      or not the decl-specifier-seq declares a named class or
10345      enumeration type, since that is the only case in which the
10346      init-declarator-list is allowed to be empty.
10347
10348      [dcl.dcl]
10349
10350      In a simple-declaration, the optional init-declarator-list can be
10351      omitted only when declaring a class or enumeration, that is when
10352      the decl-specifier-seq contains either a class-specifier, an
10353      elaborated-type-specifier, or an enum-specifier.  */
10354   cp_parser_decl_specifier_seq (parser,
10355                                 CP_PARSER_FLAGS_OPTIONAL,
10356                                 &decl_specifiers,
10357                                 &declares_class_or_enum);
10358   /* We no longer need to defer access checks.  */
10359   stop_deferring_access_checks ();
10360
10361   /* In a block scope, a valid declaration must always have a
10362      decl-specifier-seq.  By not trying to parse declarators, we can
10363      resolve the declaration/expression ambiguity more quickly.  */
10364   if (!function_definition_allowed_p
10365       && !decl_specifiers.any_specifiers_p)
10366     {
10367       cp_parser_error (parser, "expected declaration");
10368       goto done;
10369     }
10370
10371   /* If the next two tokens are both identifiers, the code is
10372      erroneous. The usual cause of this situation is code like:
10373
10374        T t;
10375
10376      where "T" should name a type -- but does not.  */
10377   if (!decl_specifiers.any_type_specifiers_p
10378       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10379     {
10380       /* If parsing tentatively, we should commit; we really are
10381          looking at a declaration.  */
10382       cp_parser_commit_to_tentative_parse (parser);
10383       /* Give up.  */
10384       goto done;
10385     }
10386
10387   /* If we have seen at least one decl-specifier, and the next token
10388      is not a parenthesis, then we must be looking at a declaration.
10389      (After "int (" we might be looking at a functional cast.)  */
10390   if (decl_specifiers.any_specifiers_p
10391       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10392       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10393       && !cp_parser_error_occurred (parser))
10394     cp_parser_commit_to_tentative_parse (parser);
10395
10396   /* Keep going until we hit the `;' at the end of the simple
10397      declaration.  */
10398   saw_declarator = false;
10399   while (cp_lexer_next_token_is_not (parser->lexer,
10400                                      CPP_SEMICOLON))
10401     {
10402       cp_token *token;
10403       bool function_definition_p;
10404       tree decl;
10405
10406       if (saw_declarator)
10407         {
10408           /* If we are processing next declarator, coma is expected */
10409           token = cp_lexer_peek_token (parser->lexer);
10410           gcc_assert (token->type == CPP_COMMA);
10411           cp_lexer_consume_token (parser->lexer);
10412           if (maybe_range_for_decl)
10413             *maybe_range_for_decl = error_mark_node;
10414         }
10415       else
10416         saw_declarator = true;
10417
10418       /* Parse the init-declarator.  */
10419       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10420                                         /*checks=*/NULL,
10421                                         function_definition_allowed_p,
10422                                         /*member_p=*/false,
10423                                         declares_class_or_enum,
10424                                         &function_definition_p,
10425                                         maybe_range_for_decl);
10426       /* If an error occurred while parsing tentatively, exit quickly.
10427          (That usually happens when in the body of a function; each
10428          statement is treated as a declaration-statement until proven
10429          otherwise.)  */
10430       if (cp_parser_error_occurred (parser))
10431         goto done;
10432       /* Handle function definitions specially.  */
10433       if (function_definition_p)
10434         {
10435           /* If the next token is a `,', then we are probably
10436              processing something like:
10437
10438                void f() {}, *p;
10439
10440              which is erroneous.  */
10441           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10442             {
10443               cp_token *token = cp_lexer_peek_token (parser->lexer);
10444               error_at (token->location,
10445                         "mixing"
10446                         " declarations and function-definitions is forbidden");
10447             }
10448           /* Otherwise, we're done with the list of declarators.  */
10449           else
10450             {
10451               pop_deferring_access_checks ();
10452               return;
10453             }
10454         }
10455       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10456         *maybe_range_for_decl = decl;
10457       /* The next token should be either a `,' or a `;'.  */
10458       token = cp_lexer_peek_token (parser->lexer);
10459       /* If it's a `,', there are more declarators to come.  */
10460       if (token->type == CPP_COMMA)
10461         /* will be consumed next time around */;
10462       /* If it's a `;', we are done.  */
10463       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10464         break;
10465       /* Anything else is an error.  */
10466       else
10467         {
10468           /* If we have already issued an error message we don't need
10469              to issue another one.  */
10470           if (decl != error_mark_node
10471               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10472             cp_parser_error (parser, "expected %<,%> or %<;%>");
10473           /* Skip tokens until we reach the end of the statement.  */
10474           cp_parser_skip_to_end_of_statement (parser);
10475           /* If the next token is now a `;', consume it.  */
10476           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10477             cp_lexer_consume_token (parser->lexer);
10478           goto done;
10479         }
10480       /* After the first time around, a function-definition is not
10481          allowed -- even if it was OK at first.  For example:
10482
10483            int i, f() {}
10484
10485          is not valid.  */
10486       function_definition_allowed_p = false;
10487     }
10488
10489   /* Issue an error message if no declarators are present, and the
10490      decl-specifier-seq does not itself declare a class or
10491      enumeration.  */
10492   if (!saw_declarator)
10493     {
10494       if (cp_parser_declares_only_class_p (parser))
10495         shadow_tag (&decl_specifiers);
10496       /* Perform any deferred access checks.  */
10497       perform_deferred_access_checks ();
10498     }
10499
10500   /* Consume the `;'.  */
10501   if (!maybe_range_for_decl)
10502       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10503
10504  done:
10505   pop_deferring_access_checks ();
10506 }
10507
10508 /* Parse a decl-specifier-seq.
10509
10510    decl-specifier-seq:
10511      decl-specifier-seq [opt] decl-specifier
10512
10513    decl-specifier:
10514      storage-class-specifier
10515      type-specifier
10516      function-specifier
10517      friend
10518      typedef
10519
10520    GNU Extension:
10521
10522    decl-specifier:
10523      attributes
10524
10525    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10526
10527    The parser flags FLAGS is used to control type-specifier parsing.
10528
10529    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10530    flags:
10531
10532      1: one of the decl-specifiers is an elaborated-type-specifier
10533         (i.e., a type declaration)
10534      2: one of the decl-specifiers is an enum-specifier or a
10535         class-specifier (i.e., a type definition)
10536
10537    */
10538
10539 static void
10540 cp_parser_decl_specifier_seq (cp_parser* parser,
10541                               cp_parser_flags flags,
10542                               cp_decl_specifier_seq *decl_specs,
10543                               int* declares_class_or_enum)
10544 {
10545   bool constructor_possible_p = !parser->in_declarator_p;
10546   cp_token *start_token = NULL;
10547
10548   /* Clear DECL_SPECS.  */
10549   clear_decl_specs (decl_specs);
10550
10551   /* Assume no class or enumeration type is declared.  */
10552   *declares_class_or_enum = 0;
10553
10554   /* Keep reading specifiers until there are no more to read.  */
10555   while (true)
10556     {
10557       bool constructor_p;
10558       bool found_decl_spec;
10559       cp_token *token;
10560
10561       /* Peek at the next token.  */
10562       token = cp_lexer_peek_token (parser->lexer);
10563
10564       /* Save the first token of the decl spec list for error
10565          reporting.  */
10566       if (!start_token)
10567         start_token = token;
10568       /* Handle attributes.  */
10569       if (token->keyword == RID_ATTRIBUTE)
10570         {
10571           /* Parse the attributes.  */
10572           decl_specs->attributes
10573             = chainon (decl_specs->attributes,
10574                        cp_parser_attributes_opt (parser));
10575           continue;
10576         }
10577       /* Assume we will find a decl-specifier keyword.  */
10578       found_decl_spec = true;
10579       /* If the next token is an appropriate keyword, we can simply
10580          add it to the list.  */
10581       switch (token->keyword)
10582         {
10583           /* decl-specifier:
10584                friend
10585                constexpr */
10586         case RID_FRIEND:
10587           if (!at_class_scope_p ())
10588             {
10589               error_at (token->location, "%<friend%> used outside of class");
10590               cp_lexer_purge_token (parser->lexer);
10591             }
10592           else
10593             {
10594               ++decl_specs->specs[(int) ds_friend];
10595               /* Consume the token.  */
10596               cp_lexer_consume_token (parser->lexer);
10597             }
10598           break;
10599
10600         case RID_CONSTEXPR:
10601           ++decl_specs->specs[(int) ds_constexpr];
10602           cp_lexer_consume_token (parser->lexer);
10603           break;
10604
10605           /* function-specifier:
10606                inline
10607                virtual
10608                explicit  */
10609         case RID_INLINE:
10610         case RID_VIRTUAL:
10611         case RID_EXPLICIT:
10612           cp_parser_function_specifier_opt (parser, decl_specs);
10613           break;
10614
10615           /* decl-specifier:
10616                typedef  */
10617         case RID_TYPEDEF:
10618           ++decl_specs->specs[(int) ds_typedef];
10619           /* Consume the token.  */
10620           cp_lexer_consume_token (parser->lexer);
10621           /* A constructor declarator cannot appear in a typedef.  */
10622           constructor_possible_p = false;
10623           /* The "typedef" keyword can only occur in a declaration; we
10624              may as well commit at this point.  */
10625           cp_parser_commit_to_tentative_parse (parser);
10626
10627           if (decl_specs->storage_class != sc_none)
10628             decl_specs->conflicting_specifiers_p = true;
10629           break;
10630
10631           /* storage-class-specifier:
10632                auto
10633                register
10634                static
10635                extern
10636                mutable
10637
10638              GNU Extension:
10639                thread  */
10640         case RID_AUTO:
10641           if (cxx_dialect == cxx98) 
10642             {
10643               /* Consume the token.  */
10644               cp_lexer_consume_token (parser->lexer);
10645
10646               /* Complain about `auto' as a storage specifier, if
10647                  we're complaining about C++0x compatibility.  */
10648               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10649                           " changes meaning in C++11; please remove it");
10650
10651               /* Set the storage class anyway.  */
10652               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10653                                            token->location);
10654             }
10655           else
10656             /* C++0x auto type-specifier.  */
10657             found_decl_spec = false;
10658           break;
10659
10660         case RID_REGISTER:
10661         case RID_STATIC:
10662         case RID_EXTERN:
10663         case RID_MUTABLE:
10664           /* Consume the token.  */
10665           cp_lexer_consume_token (parser->lexer);
10666           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10667                                        token->location);
10668           break;
10669         case RID_THREAD:
10670           /* Consume the token.  */
10671           cp_lexer_consume_token (parser->lexer);
10672           ++decl_specs->specs[(int) ds_thread];
10673           break;
10674
10675         default:
10676           /* We did not yet find a decl-specifier yet.  */
10677           found_decl_spec = false;
10678           break;
10679         }
10680
10681       if (found_decl_spec
10682           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10683           && token->keyword != RID_CONSTEXPR)
10684         error ("decl-specifier invalid in condition");
10685
10686       /* Constructors are a special case.  The `S' in `S()' is not a
10687          decl-specifier; it is the beginning of the declarator.  */
10688       constructor_p
10689         = (!found_decl_spec
10690            && constructor_possible_p
10691            && (cp_parser_constructor_declarator_p
10692                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10693
10694       /* If we don't have a DECL_SPEC yet, then we must be looking at
10695          a type-specifier.  */
10696       if (!found_decl_spec && !constructor_p)
10697         {
10698           int decl_spec_declares_class_or_enum;
10699           bool is_cv_qualifier;
10700           tree type_spec;
10701
10702           type_spec
10703             = cp_parser_type_specifier (parser, flags,
10704                                         decl_specs,
10705                                         /*is_declaration=*/true,
10706                                         &decl_spec_declares_class_or_enum,
10707                                         &is_cv_qualifier);
10708           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10709
10710           /* If this type-specifier referenced a user-defined type
10711              (a typedef, class-name, etc.), then we can't allow any
10712              more such type-specifiers henceforth.
10713
10714              [dcl.spec]
10715
10716              The longest sequence of decl-specifiers that could
10717              possibly be a type name is taken as the
10718              decl-specifier-seq of a declaration.  The sequence shall
10719              be self-consistent as described below.
10720
10721              [dcl.type]
10722
10723              As a general rule, at most one type-specifier is allowed
10724              in the complete decl-specifier-seq of a declaration.  The
10725              only exceptions are the following:
10726
10727              -- const or volatile can be combined with any other
10728                 type-specifier.
10729
10730              -- signed or unsigned can be combined with char, long,
10731                 short, or int.
10732
10733              -- ..
10734
10735              Example:
10736
10737                typedef char* Pc;
10738                void g (const int Pc);
10739
10740              Here, Pc is *not* part of the decl-specifier seq; it's
10741              the declarator.  Therefore, once we see a type-specifier
10742              (other than a cv-qualifier), we forbid any additional
10743              user-defined types.  We *do* still allow things like `int
10744              int' to be considered a decl-specifier-seq, and issue the
10745              error message later.  */
10746           if (type_spec && !is_cv_qualifier)
10747             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10748           /* A constructor declarator cannot follow a type-specifier.  */
10749           if (type_spec)
10750             {
10751               constructor_possible_p = false;
10752               found_decl_spec = true;
10753               if (!is_cv_qualifier)
10754                 decl_specs->any_type_specifiers_p = true;
10755             }
10756         }
10757
10758       /* If we still do not have a DECL_SPEC, then there are no more
10759          decl-specifiers.  */
10760       if (!found_decl_spec)
10761         break;
10762
10763       decl_specs->any_specifiers_p = true;
10764       /* After we see one decl-specifier, further decl-specifiers are
10765          always optional.  */
10766       flags |= CP_PARSER_FLAGS_OPTIONAL;
10767     }
10768
10769   cp_parser_check_decl_spec (decl_specs, start_token->location);
10770
10771   /* Don't allow a friend specifier with a class definition.  */
10772   if (decl_specs->specs[(int) ds_friend] != 0
10773       && (*declares_class_or_enum & 2))
10774     error_at (start_token->location,
10775               "class definition may not be declared a friend");
10776 }
10777
10778 /* Parse an (optional) storage-class-specifier.
10779
10780    storage-class-specifier:
10781      auto
10782      register
10783      static
10784      extern
10785      mutable
10786
10787    GNU Extension:
10788
10789    storage-class-specifier:
10790      thread
10791
10792    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10793
10794 static tree
10795 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10796 {
10797   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10798     {
10799     case RID_AUTO:
10800       if (cxx_dialect != cxx98)
10801         return NULL_TREE;
10802       /* Fall through for C++98.  */
10803
10804     case RID_REGISTER:
10805     case RID_STATIC:
10806     case RID_EXTERN:
10807     case RID_MUTABLE:
10808     case RID_THREAD:
10809       /* Consume the token.  */
10810       return cp_lexer_consume_token (parser->lexer)->u.value;
10811
10812     default:
10813       return NULL_TREE;
10814     }
10815 }
10816
10817 /* Parse an (optional) function-specifier.
10818
10819    function-specifier:
10820      inline
10821      virtual
10822      explicit
10823
10824    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10825    Updates DECL_SPECS, if it is non-NULL.  */
10826
10827 static tree
10828 cp_parser_function_specifier_opt (cp_parser* parser,
10829                                   cp_decl_specifier_seq *decl_specs)
10830 {
10831   cp_token *token = cp_lexer_peek_token (parser->lexer);
10832   switch (token->keyword)
10833     {
10834     case RID_INLINE:
10835       if (decl_specs)
10836         ++decl_specs->specs[(int) ds_inline];
10837       break;
10838
10839     case RID_VIRTUAL:
10840       /* 14.5.2.3 [temp.mem]
10841
10842          A member function template shall not be virtual.  */
10843       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10844         error_at (token->location, "templates may not be %<virtual%>");
10845       else if (decl_specs)
10846         ++decl_specs->specs[(int) ds_virtual];
10847       break;
10848
10849     case RID_EXPLICIT:
10850       if (decl_specs)
10851         ++decl_specs->specs[(int) ds_explicit];
10852       break;
10853
10854     default:
10855       return NULL_TREE;
10856     }
10857
10858   /* Consume the token.  */
10859   return cp_lexer_consume_token (parser->lexer)->u.value;
10860 }
10861
10862 /* Parse a linkage-specification.
10863
10864    linkage-specification:
10865      extern string-literal { declaration-seq [opt] }
10866      extern string-literal declaration  */
10867
10868 static void
10869 cp_parser_linkage_specification (cp_parser* parser)
10870 {
10871   tree linkage;
10872
10873   /* Look for the `extern' keyword.  */
10874   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10875
10876   /* Look for the string-literal.  */
10877   linkage = cp_parser_string_literal (parser, false, false);
10878
10879   /* Transform the literal into an identifier.  If the literal is a
10880      wide-character string, or contains embedded NULs, then we can't
10881      handle it as the user wants.  */
10882   if (strlen (TREE_STRING_POINTER (linkage))
10883       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10884     {
10885       cp_parser_error (parser, "invalid linkage-specification");
10886       /* Assume C++ linkage.  */
10887       linkage = lang_name_cplusplus;
10888     }
10889   else
10890     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10891
10892   /* We're now using the new linkage.  */
10893   push_lang_context (linkage);
10894
10895   /* If the next token is a `{', then we're using the first
10896      production.  */
10897   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10898     {
10899       /* Consume the `{' token.  */
10900       cp_lexer_consume_token (parser->lexer);
10901       /* Parse the declarations.  */
10902       cp_parser_declaration_seq_opt (parser);
10903       /* Look for the closing `}'.  */
10904       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10905     }
10906   /* Otherwise, there's just one declaration.  */
10907   else
10908     {
10909       bool saved_in_unbraced_linkage_specification_p;
10910
10911       saved_in_unbraced_linkage_specification_p
10912         = parser->in_unbraced_linkage_specification_p;
10913       parser->in_unbraced_linkage_specification_p = true;
10914       cp_parser_declaration (parser);
10915       parser->in_unbraced_linkage_specification_p
10916         = saved_in_unbraced_linkage_specification_p;
10917     }
10918
10919   /* We're done with the linkage-specification.  */
10920   pop_lang_context ();
10921 }
10922
10923 /* Parse a static_assert-declaration.
10924
10925    static_assert-declaration:
10926      static_assert ( constant-expression , string-literal ) ; 
10927
10928    If MEMBER_P, this static_assert is a class member.  */
10929
10930 static void 
10931 cp_parser_static_assert(cp_parser *parser, bool member_p)
10932 {
10933   tree condition;
10934   tree message;
10935   cp_token *token;
10936   location_t saved_loc;
10937   bool dummy;
10938
10939   /* Peek at the `static_assert' token so we can keep track of exactly
10940      where the static assertion started.  */
10941   token = cp_lexer_peek_token (parser->lexer);
10942   saved_loc = token->location;
10943
10944   /* Look for the `static_assert' keyword.  */
10945   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10946                                   RT_STATIC_ASSERT))
10947     return;
10948
10949   /*  We know we are in a static assertion; commit to any tentative
10950       parse.  */
10951   if (cp_parser_parsing_tentatively (parser))
10952     cp_parser_commit_to_tentative_parse (parser);
10953
10954   /* Parse the `(' starting the static assertion condition.  */
10955   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10956
10957   /* Parse the constant-expression.  Allow a non-constant expression
10958      here in order to give better diagnostics in finish_static_assert.  */
10959   condition = 
10960     cp_parser_constant_expression (parser,
10961                                    /*allow_non_constant_p=*/true,
10962                                    /*non_constant_p=*/&dummy);
10963
10964   /* Parse the separating `,'.  */
10965   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10966
10967   /* Parse the string-literal message.  */
10968   message = cp_parser_string_literal (parser, 
10969                                       /*translate=*/false,
10970                                       /*wide_ok=*/true);
10971
10972   /* A `)' completes the static assertion.  */
10973   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10974     cp_parser_skip_to_closing_parenthesis (parser, 
10975                                            /*recovering=*/true, 
10976                                            /*or_comma=*/false,
10977                                            /*consume_paren=*/true);
10978
10979   /* A semicolon terminates the declaration.  */
10980   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10981
10982   /* Complete the static assertion, which may mean either processing 
10983      the static assert now or saving it for template instantiation.  */
10984   finish_static_assert (condition, message, saved_loc, member_p);
10985 }
10986
10987 /* Parse a `decltype' type. Returns the type. 
10988
10989    simple-type-specifier:
10990      decltype ( expression )  */
10991
10992 static tree
10993 cp_parser_decltype (cp_parser *parser)
10994 {
10995   tree expr;
10996   bool id_expression_or_member_access_p = false;
10997   const char *saved_message;
10998   bool saved_integral_constant_expression_p;
10999   bool saved_non_integral_constant_expression_p;
11000   cp_token *id_expr_start_token;
11001   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11002
11003   if (start_token->type == CPP_DECLTYPE)
11004     {
11005       /* Already parsed.  */
11006       cp_lexer_consume_token (parser->lexer);
11007       return start_token->u.value;
11008     }
11009
11010   /* Look for the `decltype' token.  */
11011   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11012     return error_mark_node;
11013
11014   /* Types cannot be defined in a `decltype' expression.  Save away the
11015      old message.  */
11016   saved_message = parser->type_definition_forbidden_message;
11017
11018   /* And create the new one.  */
11019   parser->type_definition_forbidden_message
11020     = G_("types may not be defined in %<decltype%> expressions");
11021
11022   /* The restrictions on constant-expressions do not apply inside
11023      decltype expressions.  */
11024   saved_integral_constant_expression_p
11025     = parser->integral_constant_expression_p;
11026   saved_non_integral_constant_expression_p
11027     = parser->non_integral_constant_expression_p;
11028   parser->integral_constant_expression_p = false;
11029
11030   /* Do not actually evaluate the expression.  */
11031   ++cp_unevaluated_operand;
11032
11033   /* Do not warn about problems with the expression.  */
11034   ++c_inhibit_evaluation_warnings;
11035
11036   /* Parse the opening `('.  */
11037   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11038     return error_mark_node;
11039   
11040   /* First, try parsing an id-expression.  */
11041   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11042   cp_parser_parse_tentatively (parser);
11043   expr = cp_parser_id_expression (parser,
11044                                   /*template_keyword_p=*/false,
11045                                   /*check_dependency_p=*/true,
11046                                   /*template_p=*/NULL,
11047                                   /*declarator_p=*/false,
11048                                   /*optional_p=*/false);
11049
11050   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11051     {
11052       bool non_integral_constant_expression_p = false;
11053       tree id_expression = expr;
11054       cp_id_kind idk;
11055       const char *error_msg;
11056
11057       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11058         /* Lookup the name we got back from the id-expression.  */
11059         expr = cp_parser_lookup_name (parser, expr,
11060                                       none_type,
11061                                       /*is_template=*/false,
11062                                       /*is_namespace=*/false,
11063                                       /*check_dependency=*/true,
11064                                       /*ambiguous_decls=*/NULL,
11065                                       id_expr_start_token->location);
11066
11067       if (expr
11068           && expr != error_mark_node
11069           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11070           && TREE_CODE (expr) != TYPE_DECL
11071           && (TREE_CODE (expr) != BIT_NOT_EXPR
11072               || !TYPE_P (TREE_OPERAND (expr, 0)))
11073           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11074         {
11075           /* Complete lookup of the id-expression.  */
11076           expr = (finish_id_expression
11077                   (id_expression, expr, parser->scope, &idk,
11078                    /*integral_constant_expression_p=*/false,
11079                    /*allow_non_integral_constant_expression_p=*/true,
11080                    &non_integral_constant_expression_p,
11081                    /*template_p=*/false,
11082                    /*done=*/true,
11083                    /*address_p=*/false,
11084                    /*template_arg_p=*/false,
11085                    &error_msg,
11086                    id_expr_start_token->location));
11087
11088           if (expr == error_mark_node)
11089             /* We found an id-expression, but it was something that we
11090                should not have found. This is an error, not something
11091                we can recover from, so note that we found an
11092                id-expression and we'll recover as gracefully as
11093                possible.  */
11094             id_expression_or_member_access_p = true;
11095         }
11096
11097       if (expr 
11098           && expr != error_mark_node
11099           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11100         /* We have an id-expression.  */
11101         id_expression_or_member_access_p = true;
11102     }
11103
11104   if (!id_expression_or_member_access_p)
11105     {
11106       /* Abort the id-expression parse.  */
11107       cp_parser_abort_tentative_parse (parser);
11108
11109       /* Parsing tentatively, again.  */
11110       cp_parser_parse_tentatively (parser);
11111
11112       /* Parse a class member access.  */
11113       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11114                                            /*cast_p=*/false,
11115                                            /*member_access_only_p=*/true, NULL);
11116
11117       if (expr 
11118           && expr != error_mark_node
11119           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11120         /* We have an id-expression.  */
11121         id_expression_or_member_access_p = true;
11122     }
11123
11124   if (id_expression_or_member_access_p)
11125     /* We have parsed the complete id-expression or member access.  */
11126     cp_parser_parse_definitely (parser);
11127   else
11128     {
11129       bool saved_greater_than_is_operator_p;
11130
11131       /* Abort our attempt to parse an id-expression or member access
11132          expression.  */
11133       cp_parser_abort_tentative_parse (parser);
11134
11135       /* Within a parenthesized expression, a `>' token is always
11136          the greater-than operator.  */
11137       saved_greater_than_is_operator_p
11138         = parser->greater_than_is_operator_p;
11139       parser->greater_than_is_operator_p = true;
11140
11141       /* Parse a full expression.  */
11142       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11143
11144       /* The `>' token might be the end of a template-id or
11145          template-parameter-list now.  */
11146       parser->greater_than_is_operator_p
11147         = saved_greater_than_is_operator_p;
11148     }
11149
11150   /* Go back to evaluating expressions.  */
11151   --cp_unevaluated_operand;
11152   --c_inhibit_evaluation_warnings;
11153
11154   /* Restore the old message and the integral constant expression
11155      flags.  */
11156   parser->type_definition_forbidden_message = saved_message;
11157   parser->integral_constant_expression_p
11158     = saved_integral_constant_expression_p;
11159   parser->non_integral_constant_expression_p
11160     = saved_non_integral_constant_expression_p;
11161
11162   /* Parse to the closing `)'.  */
11163   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11164     {
11165       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11166                                              /*consume_paren=*/true);
11167       return error_mark_node;
11168     }
11169
11170   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11171                                tf_warning_or_error);
11172
11173   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11174      it again.  */
11175   start_token->type = CPP_DECLTYPE;
11176   start_token->u.value = expr;
11177   start_token->keyword = RID_MAX;
11178   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11179
11180   return expr;
11181 }
11182
11183 /* Special member functions [gram.special] */
11184
11185 /* Parse a conversion-function-id.
11186
11187    conversion-function-id:
11188      operator conversion-type-id
11189
11190    Returns an IDENTIFIER_NODE representing the operator.  */
11191
11192 static tree
11193 cp_parser_conversion_function_id (cp_parser* parser)
11194 {
11195   tree type;
11196   tree saved_scope;
11197   tree saved_qualifying_scope;
11198   tree saved_object_scope;
11199   tree pushed_scope = NULL_TREE;
11200
11201   /* Look for the `operator' token.  */
11202   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11203     return error_mark_node;
11204   /* When we parse the conversion-type-id, the current scope will be
11205      reset.  However, we need that information in able to look up the
11206      conversion function later, so we save it here.  */
11207   saved_scope = parser->scope;
11208   saved_qualifying_scope = parser->qualifying_scope;
11209   saved_object_scope = parser->object_scope;
11210   /* We must enter the scope of the class so that the names of
11211      entities declared within the class are available in the
11212      conversion-type-id.  For example, consider:
11213
11214        struct S {
11215          typedef int I;
11216          operator I();
11217        };
11218
11219        S::operator I() { ... }
11220
11221      In order to see that `I' is a type-name in the definition, we
11222      must be in the scope of `S'.  */
11223   if (saved_scope)
11224     pushed_scope = push_scope (saved_scope);
11225   /* Parse the conversion-type-id.  */
11226   type = cp_parser_conversion_type_id (parser);
11227   /* Leave the scope of the class, if any.  */
11228   if (pushed_scope)
11229     pop_scope (pushed_scope);
11230   /* Restore the saved scope.  */
11231   parser->scope = saved_scope;
11232   parser->qualifying_scope = saved_qualifying_scope;
11233   parser->object_scope = saved_object_scope;
11234   /* If the TYPE is invalid, indicate failure.  */
11235   if (type == error_mark_node)
11236     return error_mark_node;
11237   return mangle_conv_op_name_for_type (type);
11238 }
11239
11240 /* Parse a conversion-type-id:
11241
11242    conversion-type-id:
11243      type-specifier-seq conversion-declarator [opt]
11244
11245    Returns the TYPE specified.  */
11246
11247 static tree
11248 cp_parser_conversion_type_id (cp_parser* parser)
11249 {
11250   tree attributes;
11251   cp_decl_specifier_seq type_specifiers;
11252   cp_declarator *declarator;
11253   tree type_specified;
11254
11255   /* Parse the attributes.  */
11256   attributes = cp_parser_attributes_opt (parser);
11257   /* Parse the type-specifiers.  */
11258   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11259                                 /*is_trailing_return=*/false,
11260                                 &type_specifiers);
11261   /* If that didn't work, stop.  */
11262   if (type_specifiers.type == error_mark_node)
11263     return error_mark_node;
11264   /* Parse the conversion-declarator.  */
11265   declarator = cp_parser_conversion_declarator_opt (parser);
11266
11267   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11268                                     /*initialized=*/0, &attributes);
11269   if (attributes)
11270     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11271
11272   /* Don't give this error when parsing tentatively.  This happens to
11273      work because we always parse this definitively once.  */
11274   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11275       && type_uses_auto (type_specified))
11276     {
11277       if (cxx_dialect < cxx1y)
11278         {
11279           error ("invalid use of %<auto%> in conversion operator");
11280           return error_mark_node;
11281         }
11282       else if (template_parm_scope_p ())
11283         warning (0, "use of %<auto%> in member template "
11284                  "conversion operator can never be deduced");
11285     }
11286
11287   return type_specified;
11288 }
11289
11290 /* Parse an (optional) conversion-declarator.
11291
11292    conversion-declarator:
11293      ptr-operator conversion-declarator [opt]
11294
11295    */
11296
11297 static cp_declarator *
11298 cp_parser_conversion_declarator_opt (cp_parser* parser)
11299 {
11300   enum tree_code code;
11301   tree class_type;
11302   cp_cv_quals cv_quals;
11303
11304   /* We don't know if there's a ptr-operator next, or not.  */
11305   cp_parser_parse_tentatively (parser);
11306   /* Try the ptr-operator.  */
11307   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11308   /* If it worked, look for more conversion-declarators.  */
11309   if (cp_parser_parse_definitely (parser))
11310     {
11311       cp_declarator *declarator;
11312
11313       /* Parse another optional declarator.  */
11314       declarator = cp_parser_conversion_declarator_opt (parser);
11315
11316       return cp_parser_make_indirect_declarator
11317         (code, class_type, cv_quals, declarator);
11318    }
11319
11320   return NULL;
11321 }
11322
11323 /* Parse an (optional) ctor-initializer.
11324
11325    ctor-initializer:
11326      : mem-initializer-list
11327
11328    Returns TRUE iff the ctor-initializer was actually present.  */
11329
11330 static bool
11331 cp_parser_ctor_initializer_opt (cp_parser* parser)
11332 {
11333   /* If the next token is not a `:', then there is no
11334      ctor-initializer.  */
11335   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11336     {
11337       /* Do default initialization of any bases and members.  */
11338       if (DECL_CONSTRUCTOR_P (current_function_decl))
11339         finish_mem_initializers (NULL_TREE);
11340
11341       return false;
11342     }
11343
11344   /* Consume the `:' token.  */
11345   cp_lexer_consume_token (parser->lexer);
11346   /* And the mem-initializer-list.  */
11347   cp_parser_mem_initializer_list (parser);
11348
11349   return true;
11350 }
11351
11352 /* Parse a mem-initializer-list.
11353
11354    mem-initializer-list:
11355      mem-initializer ... [opt]
11356      mem-initializer ... [opt] , mem-initializer-list  */
11357
11358 static void
11359 cp_parser_mem_initializer_list (cp_parser* parser)
11360 {
11361   tree mem_initializer_list = NULL_TREE;
11362   tree target_ctor = error_mark_node;
11363   cp_token *token = cp_lexer_peek_token (parser->lexer);
11364
11365   /* Let the semantic analysis code know that we are starting the
11366      mem-initializer-list.  */
11367   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11368     error_at (token->location,
11369               "only constructors take member initializers");
11370
11371   /* Loop through the list.  */
11372   while (true)
11373     {
11374       tree mem_initializer;
11375
11376       token = cp_lexer_peek_token (parser->lexer);
11377       /* Parse the mem-initializer.  */
11378       mem_initializer = cp_parser_mem_initializer (parser);
11379       /* If the next token is a `...', we're expanding member initializers. */
11380       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11381         {
11382           /* Consume the `...'. */
11383           cp_lexer_consume_token (parser->lexer);
11384
11385           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11386              can be expanded but members cannot. */
11387           if (mem_initializer != error_mark_node
11388               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11389             {
11390               error_at (token->location,
11391                         "cannot expand initializer for member %<%D%>",
11392                         TREE_PURPOSE (mem_initializer));
11393               mem_initializer = error_mark_node;
11394             }
11395
11396           /* Construct the pack expansion type. */
11397           if (mem_initializer != error_mark_node)
11398             mem_initializer = make_pack_expansion (mem_initializer);
11399         }
11400       if (target_ctor != error_mark_node
11401           && mem_initializer != error_mark_node)
11402         {
11403           error ("mem-initializer for %qD follows constructor delegation",
11404                  TREE_PURPOSE (mem_initializer));
11405           mem_initializer = error_mark_node;
11406         }
11407       /* Look for a target constructor. */
11408       if (mem_initializer != error_mark_node
11409           && TYPE_P (TREE_PURPOSE (mem_initializer))
11410           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11411         {
11412           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11413           if (mem_initializer_list)
11414             {
11415               error ("constructor delegation follows mem-initializer for %qD",
11416                      TREE_PURPOSE (mem_initializer_list));
11417               mem_initializer = error_mark_node;
11418             }
11419           target_ctor = mem_initializer;
11420         }
11421       /* Add it to the list, unless it was erroneous.  */
11422       if (mem_initializer != error_mark_node)
11423         {
11424           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11425           mem_initializer_list = mem_initializer;
11426         }
11427       /* If the next token is not a `,', we're done.  */
11428       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11429         break;
11430       /* Consume the `,' token.  */
11431       cp_lexer_consume_token (parser->lexer);
11432     }
11433
11434   /* Perform semantic analysis.  */
11435   if (DECL_CONSTRUCTOR_P (current_function_decl))
11436     finish_mem_initializers (mem_initializer_list);
11437 }
11438
11439 /* Parse a mem-initializer.
11440
11441    mem-initializer:
11442      mem-initializer-id ( expression-list [opt] )
11443      mem-initializer-id braced-init-list
11444
11445    GNU extension:
11446
11447    mem-initializer:
11448      ( expression-list [opt] )
11449
11450    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11451    class) or FIELD_DECL (for a non-static data member) to initialize;
11452    the TREE_VALUE is the expression-list.  An empty initialization
11453    list is represented by void_list_node.  */
11454
11455 static tree
11456 cp_parser_mem_initializer (cp_parser* parser)
11457 {
11458   tree mem_initializer_id;
11459   tree expression_list;
11460   tree member;
11461   cp_token *token = cp_lexer_peek_token (parser->lexer);
11462
11463   /* Find out what is being initialized.  */
11464   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11465     {
11466       permerror (token->location,
11467                  "anachronistic old-style base class initializer");
11468       mem_initializer_id = NULL_TREE;
11469     }
11470   else
11471     {
11472       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11473       if (mem_initializer_id == error_mark_node)
11474         return mem_initializer_id;
11475     }
11476   member = expand_member_init (mem_initializer_id);
11477   if (member && !DECL_P (member))
11478     in_base_initializer = 1;
11479
11480   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11481     {
11482       bool expr_non_constant_p;
11483       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11484       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11485       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11486       expression_list = build_tree_list (NULL_TREE, expression_list);
11487     }
11488   else
11489     {
11490       VEC(tree,gc)* vec;
11491       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11492                                                      /*cast_p=*/false,
11493                                                      /*allow_expansion_p=*/true,
11494                                                      /*non_constant_p=*/NULL);
11495       if (vec == NULL)
11496         return error_mark_node;
11497       expression_list = build_tree_list_vec (vec);
11498       release_tree_vector (vec);
11499     }
11500
11501   if (expression_list == error_mark_node)
11502     return error_mark_node;
11503   if (!expression_list)
11504     expression_list = void_type_node;
11505
11506   in_base_initializer = 0;
11507
11508   return member ? build_tree_list (member, expression_list) : error_mark_node;
11509 }
11510
11511 /* Parse a mem-initializer-id.
11512
11513    mem-initializer-id:
11514      :: [opt] nested-name-specifier [opt] class-name
11515      identifier
11516
11517    Returns a TYPE indicating the class to be initializer for the first
11518    production.  Returns an IDENTIFIER_NODE indicating the data member
11519    to be initialized for the second production.  */
11520
11521 static tree
11522 cp_parser_mem_initializer_id (cp_parser* parser)
11523 {
11524   bool global_scope_p;
11525   bool nested_name_specifier_p;
11526   bool template_p = false;
11527   tree id;
11528
11529   cp_token *token = cp_lexer_peek_token (parser->lexer);
11530
11531   /* `typename' is not allowed in this context ([temp.res]).  */
11532   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11533     {
11534       error_at (token->location, 
11535                 "keyword %<typename%> not allowed in this context (a qualified "
11536                 "member initializer is implicitly a type)");
11537       cp_lexer_consume_token (parser->lexer);
11538     }
11539   /* Look for the optional `::' operator.  */
11540   global_scope_p
11541     = (cp_parser_global_scope_opt (parser,
11542                                    /*current_scope_valid_p=*/false)
11543        != NULL_TREE);
11544   /* Look for the optional nested-name-specifier.  The simplest way to
11545      implement:
11546
11547        [temp.res]
11548
11549        The keyword `typename' is not permitted in a base-specifier or
11550        mem-initializer; in these contexts a qualified name that
11551        depends on a template-parameter is implicitly assumed to be a
11552        type name.
11553
11554      is to assume that we have seen the `typename' keyword at this
11555      point.  */
11556   nested_name_specifier_p
11557     = (cp_parser_nested_name_specifier_opt (parser,
11558                                             /*typename_keyword_p=*/true,
11559                                             /*check_dependency_p=*/true,
11560                                             /*type_p=*/true,
11561                                             /*is_declaration=*/true)
11562        != NULL_TREE);
11563   if (nested_name_specifier_p)
11564     template_p = cp_parser_optional_template_keyword (parser);
11565   /* If there is a `::' operator or a nested-name-specifier, then we
11566      are definitely looking for a class-name.  */
11567   if (global_scope_p || nested_name_specifier_p)
11568     return cp_parser_class_name (parser,
11569                                  /*typename_keyword_p=*/true,
11570                                  /*template_keyword_p=*/template_p,
11571                                  typename_type,
11572                                  /*check_dependency_p=*/true,
11573                                  /*class_head_p=*/false,
11574                                  /*is_declaration=*/true);
11575   /* Otherwise, we could also be looking for an ordinary identifier.  */
11576   cp_parser_parse_tentatively (parser);
11577   /* Try a class-name.  */
11578   id = cp_parser_class_name (parser,
11579                              /*typename_keyword_p=*/true,
11580                              /*template_keyword_p=*/false,
11581                              none_type,
11582                              /*check_dependency_p=*/true,
11583                              /*class_head_p=*/false,
11584                              /*is_declaration=*/true);
11585   /* If we found one, we're done.  */
11586   if (cp_parser_parse_definitely (parser))
11587     return id;
11588   /* Otherwise, look for an ordinary identifier.  */
11589   return cp_parser_identifier (parser);
11590 }
11591
11592 /* Overloading [gram.over] */
11593
11594 /* Parse an operator-function-id.
11595
11596    operator-function-id:
11597      operator operator
11598
11599    Returns an IDENTIFIER_NODE for the operator which is a
11600    human-readable spelling of the identifier, e.g., `operator +'.  */
11601
11602 static tree
11603 cp_parser_operator_function_id (cp_parser* parser)
11604 {
11605   /* Look for the `operator' keyword.  */
11606   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11607     return error_mark_node;
11608   /* And then the name of the operator itself.  */
11609   return cp_parser_operator (parser);
11610 }
11611
11612 /* Return an identifier node for a user-defined literal operator.
11613    The suffix identifier is chained to the operator name identifier.  */
11614
11615 static tree
11616 cp_literal_operator_id (const char* name)
11617 {
11618   tree identifier;
11619   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11620                               + strlen (name) + 10);
11621   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11622   identifier = get_identifier (buffer);
11623   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11624
11625   return identifier;
11626 }
11627
11628 /* Parse an operator.
11629
11630    operator:
11631      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11632      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11633      || ++ -- , ->* -> () []
11634
11635    GNU Extensions:
11636
11637    operator:
11638      <? >? <?= >?=
11639
11640    Returns an IDENTIFIER_NODE for the operator which is a
11641    human-readable spelling of the identifier, e.g., `operator +'.  */
11642
11643 static tree
11644 cp_parser_operator (cp_parser* parser)
11645 {
11646   tree id = NULL_TREE;
11647   cp_token *token;
11648
11649   /* Peek at the next token.  */
11650   token = cp_lexer_peek_token (parser->lexer);
11651   /* Figure out which operator we have.  */
11652   switch (token->type)
11653     {
11654     case CPP_KEYWORD:
11655       {
11656         enum tree_code op;
11657
11658         /* The keyword should be either `new' or `delete'.  */
11659         if (token->keyword == RID_NEW)
11660           op = NEW_EXPR;
11661         else if (token->keyword == RID_DELETE)
11662           op = DELETE_EXPR;
11663         else
11664           break;
11665
11666         /* Consume the `new' or `delete' token.  */
11667         cp_lexer_consume_token (parser->lexer);
11668
11669         /* Peek at the next token.  */
11670         token = cp_lexer_peek_token (parser->lexer);
11671         /* If it's a `[' token then this is the array variant of the
11672            operator.  */
11673         if (token->type == CPP_OPEN_SQUARE)
11674           {
11675             /* Consume the `[' token.  */
11676             cp_lexer_consume_token (parser->lexer);
11677             /* Look for the `]' token.  */
11678             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11679             id = ansi_opname (op == NEW_EXPR
11680                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11681           }
11682         /* Otherwise, we have the non-array variant.  */
11683         else
11684           id = ansi_opname (op);
11685
11686         return id;
11687       }
11688
11689     case CPP_PLUS:
11690       id = ansi_opname (PLUS_EXPR);
11691       break;
11692
11693     case CPP_MINUS:
11694       id = ansi_opname (MINUS_EXPR);
11695       break;
11696
11697     case CPP_MULT:
11698       id = ansi_opname (MULT_EXPR);
11699       break;
11700
11701     case CPP_DIV:
11702       id = ansi_opname (TRUNC_DIV_EXPR);
11703       break;
11704
11705     case CPP_MOD:
11706       id = ansi_opname (TRUNC_MOD_EXPR);
11707       break;
11708
11709     case CPP_XOR:
11710       id = ansi_opname (BIT_XOR_EXPR);
11711       break;
11712
11713     case CPP_AND:
11714       id = ansi_opname (BIT_AND_EXPR);
11715       break;
11716
11717     case CPP_OR:
11718       id = ansi_opname (BIT_IOR_EXPR);
11719       break;
11720
11721     case CPP_COMPL:
11722       id = ansi_opname (BIT_NOT_EXPR);
11723       break;
11724
11725     case CPP_NOT:
11726       id = ansi_opname (TRUTH_NOT_EXPR);
11727       break;
11728
11729     case CPP_EQ:
11730       id = ansi_assopname (NOP_EXPR);
11731       break;
11732
11733     case CPP_LESS:
11734       id = ansi_opname (LT_EXPR);
11735       break;
11736
11737     case CPP_GREATER:
11738       id = ansi_opname (GT_EXPR);
11739       break;
11740
11741     case CPP_PLUS_EQ:
11742       id = ansi_assopname (PLUS_EXPR);
11743       break;
11744
11745     case CPP_MINUS_EQ:
11746       id = ansi_assopname (MINUS_EXPR);
11747       break;
11748
11749     case CPP_MULT_EQ:
11750       id = ansi_assopname (MULT_EXPR);
11751       break;
11752
11753     case CPP_DIV_EQ:
11754       id = ansi_assopname (TRUNC_DIV_EXPR);
11755       break;
11756
11757     case CPP_MOD_EQ:
11758       id = ansi_assopname (TRUNC_MOD_EXPR);
11759       break;
11760
11761     case CPP_XOR_EQ:
11762       id = ansi_assopname (BIT_XOR_EXPR);
11763       break;
11764
11765     case CPP_AND_EQ:
11766       id = ansi_assopname (BIT_AND_EXPR);
11767       break;
11768
11769     case CPP_OR_EQ:
11770       id = ansi_assopname (BIT_IOR_EXPR);
11771       break;
11772
11773     case CPP_LSHIFT:
11774       id = ansi_opname (LSHIFT_EXPR);
11775       break;
11776
11777     case CPP_RSHIFT:
11778       id = ansi_opname (RSHIFT_EXPR);
11779       break;
11780
11781     case CPP_LSHIFT_EQ:
11782       id = ansi_assopname (LSHIFT_EXPR);
11783       break;
11784
11785     case CPP_RSHIFT_EQ:
11786       id = ansi_assopname (RSHIFT_EXPR);
11787       break;
11788
11789     case CPP_EQ_EQ:
11790       id = ansi_opname (EQ_EXPR);
11791       break;
11792
11793     case CPP_NOT_EQ:
11794       id = ansi_opname (NE_EXPR);
11795       break;
11796
11797     case CPP_LESS_EQ:
11798       id = ansi_opname (LE_EXPR);
11799       break;
11800
11801     case CPP_GREATER_EQ:
11802       id = ansi_opname (GE_EXPR);
11803       break;
11804
11805     case CPP_AND_AND:
11806       id = ansi_opname (TRUTH_ANDIF_EXPR);
11807       break;
11808
11809     case CPP_OR_OR:
11810       id = ansi_opname (TRUTH_ORIF_EXPR);
11811       break;
11812
11813     case CPP_PLUS_PLUS:
11814       id = ansi_opname (POSTINCREMENT_EXPR);
11815       break;
11816
11817     case CPP_MINUS_MINUS:
11818       id = ansi_opname (PREDECREMENT_EXPR);
11819       break;
11820
11821     case CPP_COMMA:
11822       id = ansi_opname (COMPOUND_EXPR);
11823       break;
11824
11825     case CPP_DEREF_STAR:
11826       id = ansi_opname (MEMBER_REF);
11827       break;
11828
11829     case CPP_DEREF:
11830       id = ansi_opname (COMPONENT_REF);
11831       break;
11832
11833     case CPP_OPEN_PAREN:
11834       /* Consume the `('.  */
11835       cp_lexer_consume_token (parser->lexer);
11836       /* Look for the matching `)'.  */
11837       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11838       return ansi_opname (CALL_EXPR);
11839
11840     case CPP_OPEN_SQUARE:
11841       /* Consume the `['.  */
11842       cp_lexer_consume_token (parser->lexer);
11843       /* Look for the matching `]'.  */
11844       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11845       return ansi_opname (ARRAY_REF);
11846
11847     case CPP_STRING:
11848       if (cxx_dialect == cxx98)
11849         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11850       if (TREE_STRING_LENGTH (token->u.value) > 2)
11851         {
11852           error ("expected empty string after %<operator%> keyword");
11853           return error_mark_node;
11854         }
11855       /* Consume the string.  */
11856       cp_lexer_consume_token (parser->lexer);
11857       /* Look for the suffix identifier.  */
11858       token = cp_lexer_peek_token (parser->lexer);
11859       if (token->type == CPP_NAME)
11860         {
11861           id = cp_parser_identifier (parser);
11862           if (id != error_mark_node)
11863             {
11864               const char *name = IDENTIFIER_POINTER (id);
11865               return cp_literal_operator_id (name);
11866             }
11867         }
11868       else
11869         {
11870           error ("expected suffix identifier");
11871           return error_mark_node;
11872         }
11873
11874     case CPP_STRING_USERDEF:
11875       error ("missing space between %<\"\"%> and suffix identifier");
11876       return error_mark_node;
11877
11878     default:
11879       /* Anything else is an error.  */
11880       break;
11881     }
11882
11883   /* If we have selected an identifier, we need to consume the
11884      operator token.  */
11885   if (id)
11886     cp_lexer_consume_token (parser->lexer);
11887   /* Otherwise, no valid operator name was present.  */
11888   else
11889     {
11890       cp_parser_error (parser, "expected operator");
11891       id = error_mark_node;
11892     }
11893
11894   return id;
11895 }
11896
11897 /* Parse a template-declaration.
11898
11899    template-declaration:
11900      export [opt] template < template-parameter-list > declaration
11901
11902    If MEMBER_P is TRUE, this template-declaration occurs within a
11903    class-specifier.
11904
11905    The grammar rule given by the standard isn't correct.  What
11906    is really meant is:
11907
11908    template-declaration:
11909      export [opt] template-parameter-list-seq
11910        decl-specifier-seq [opt] init-declarator [opt] ;
11911      export [opt] template-parameter-list-seq
11912        function-definition
11913
11914    template-parameter-list-seq:
11915      template-parameter-list-seq [opt]
11916      template < template-parameter-list >  */
11917
11918 static void
11919 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11920 {
11921   /* Check for `export'.  */
11922   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11923     {
11924       /* Consume the `export' token.  */
11925       cp_lexer_consume_token (parser->lexer);
11926       /* Warn that we do not support `export'.  */
11927       warning (0, "keyword %<export%> not implemented, and will be ignored");
11928     }
11929
11930   cp_parser_template_declaration_after_export (parser, member_p);
11931 }
11932
11933 /* Parse a template-parameter-list.
11934
11935    template-parameter-list:
11936      template-parameter
11937      template-parameter-list , template-parameter
11938
11939    Returns a TREE_LIST.  Each node represents a template parameter.
11940    The nodes are connected via their TREE_CHAINs.  */
11941
11942 static tree
11943 cp_parser_template_parameter_list (cp_parser* parser)
11944 {
11945   tree parameter_list = NULL_TREE;
11946
11947   begin_template_parm_list ();
11948
11949   /* The loop below parses the template parms.  We first need to know
11950      the total number of template parms to be able to compute proper
11951      canonical types of each dependent type. So after the loop, when
11952      we know the total number of template parms,
11953      end_template_parm_list computes the proper canonical types and
11954      fixes up the dependent types accordingly.  */
11955   while (true)
11956     {
11957       tree parameter;
11958       bool is_non_type;
11959       bool is_parameter_pack;
11960       location_t parm_loc;
11961
11962       /* Parse the template-parameter.  */
11963       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11964       parameter = cp_parser_template_parameter (parser, 
11965                                                 &is_non_type,
11966                                                 &is_parameter_pack);
11967       /* Add it to the list.  */
11968       if (parameter != error_mark_node)
11969         parameter_list = process_template_parm (parameter_list,
11970                                                 parm_loc,
11971                                                 parameter,
11972                                                 is_non_type,
11973                                                 is_parameter_pack,
11974                                                 0);
11975       else
11976        {
11977          tree err_parm = build_tree_list (parameter, parameter);
11978          parameter_list = chainon (parameter_list, err_parm);
11979        }
11980
11981       /* If the next token is not a `,', we're done.  */
11982       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11983         break;
11984       /* Otherwise, consume the `,' token.  */
11985       cp_lexer_consume_token (parser->lexer);
11986     }
11987
11988   return end_template_parm_list (parameter_list);
11989 }
11990
11991 /* Parse a template-parameter.
11992
11993    template-parameter:
11994      type-parameter
11995      parameter-declaration
11996
11997    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11998    the parameter.  The TREE_PURPOSE is the default value, if any.
11999    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
12000    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
12001    set to true iff this parameter is a parameter pack. */
12002
12003 static tree
12004 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12005                               bool *is_parameter_pack)
12006 {
12007   cp_token *token;
12008   cp_parameter_declarator *parameter_declarator;
12009   cp_declarator *id_declarator;
12010   tree parm;
12011
12012   /* Assume it is a type parameter or a template parameter.  */
12013   *is_non_type = false;
12014   /* Assume it not a parameter pack. */
12015   *is_parameter_pack = false;
12016   /* Peek at the next token.  */
12017   token = cp_lexer_peek_token (parser->lexer);
12018   /* If it is `class' or `template', we have a type-parameter.  */
12019   if (token->keyword == RID_TEMPLATE)
12020     return cp_parser_type_parameter (parser, is_parameter_pack);
12021   /* If it is `class' or `typename' we do not know yet whether it is a
12022      type parameter or a non-type parameter.  Consider:
12023
12024        template <typename T, typename T::X X> ...
12025
12026      or:
12027
12028        template <class C, class D*> ...
12029
12030      Here, the first parameter is a type parameter, and the second is
12031      a non-type parameter.  We can tell by looking at the token after
12032      the identifier -- if it is a `,', `=', or `>' then we have a type
12033      parameter.  */
12034   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12035     {
12036       /* Peek at the token after `class' or `typename'.  */
12037       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12038       /* If it's an ellipsis, we have a template type parameter
12039          pack. */
12040       if (token->type == CPP_ELLIPSIS)
12041         return cp_parser_type_parameter (parser, is_parameter_pack);
12042       /* If it's an identifier, skip it.  */
12043       if (token->type == CPP_NAME)
12044         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12045       /* Now, see if the token looks like the end of a template
12046          parameter.  */
12047       if (token->type == CPP_COMMA
12048           || token->type == CPP_EQ
12049           || token->type == CPP_GREATER)
12050         return cp_parser_type_parameter (parser, is_parameter_pack);
12051     }
12052
12053   /* Otherwise, it is a non-type parameter.
12054
12055      [temp.param]
12056
12057      When parsing a default template-argument for a non-type
12058      template-parameter, the first non-nested `>' is taken as the end
12059      of the template parameter-list rather than a greater-than
12060      operator.  */
12061   *is_non_type = true;
12062   parameter_declarator
12063      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12064                                         /*parenthesized_p=*/NULL);
12065
12066   /* If the parameter declaration is marked as a parameter pack, set
12067      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12068      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12069      grokdeclarator. */
12070   if (parameter_declarator
12071       && parameter_declarator->declarator
12072       && parameter_declarator->declarator->parameter_pack_p)
12073     {
12074       *is_parameter_pack = true;
12075       parameter_declarator->declarator->parameter_pack_p = false;
12076     }
12077
12078   /* If the next token is an ellipsis, and we don't already have it
12079      marked as a parameter pack, then we have a parameter pack (that
12080      has no declarator).  */
12081   if (!*is_parameter_pack
12082       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12083       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12084     {
12085       /* Consume the `...'.  */
12086       cp_lexer_consume_token (parser->lexer);
12087       maybe_warn_variadic_templates ();
12088       
12089       *is_parameter_pack = true;
12090     }
12091   /* We might end up with a pack expansion as the type of the non-type
12092      template parameter, in which case this is a non-type template
12093      parameter pack.  */
12094   else if (parameter_declarator
12095            && parameter_declarator->decl_specifiers.type
12096            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12097     {
12098       *is_parameter_pack = true;
12099       parameter_declarator->decl_specifiers.type = 
12100         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12101     }
12102
12103   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12104     {
12105       /* Parameter packs cannot have default arguments.  However, a
12106          user may try to do so, so we'll parse them and give an
12107          appropriate diagnostic here.  */
12108
12109       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12110       
12111       /* Find the name of the parameter pack.  */     
12112       id_declarator = parameter_declarator->declarator;
12113       while (id_declarator && id_declarator->kind != cdk_id)
12114         id_declarator = id_declarator->declarator;
12115       
12116       if (id_declarator && id_declarator->kind == cdk_id)
12117         error_at (start_token->location,
12118                   "template parameter pack %qD cannot have a default argument",
12119                   id_declarator->u.id.unqualified_name);
12120       else
12121         error_at (start_token->location,
12122                   "template parameter pack cannot have a default argument");
12123       
12124       /* Parse the default argument, but throw away the result.  */
12125       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12126     }
12127
12128   parm = grokdeclarator (parameter_declarator->declarator,
12129                          &parameter_declarator->decl_specifiers,
12130                          TPARM, /*initialized=*/0,
12131                          /*attrlist=*/NULL);
12132   if (parm == error_mark_node)
12133     return error_mark_node;
12134
12135   return build_tree_list (parameter_declarator->default_argument, parm);
12136 }
12137
12138 /* Parse a type-parameter.
12139
12140    type-parameter:
12141      class identifier [opt]
12142      class identifier [opt] = type-id
12143      typename identifier [opt]
12144      typename identifier [opt] = type-id
12145      template < template-parameter-list > class identifier [opt]
12146      template < template-parameter-list > class identifier [opt]
12147        = id-expression
12148
12149    GNU Extension (variadic templates):
12150
12151    type-parameter:
12152      class ... identifier [opt]
12153      typename ... identifier [opt]
12154
12155    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12156    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12157    the declaration of the parameter.
12158
12159    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12160
12161 static tree
12162 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12163 {
12164   cp_token *token;
12165   tree parameter;
12166
12167   /* Look for a keyword to tell us what kind of parameter this is.  */
12168   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12169   if (!token)
12170     return error_mark_node;
12171
12172   switch (token->keyword)
12173     {
12174     case RID_CLASS:
12175     case RID_TYPENAME:
12176       {
12177         tree identifier;
12178         tree default_argument;
12179
12180         /* If the next token is an ellipsis, we have a template
12181            argument pack. */
12182         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12183           {
12184             /* Consume the `...' token. */
12185             cp_lexer_consume_token (parser->lexer);
12186             maybe_warn_variadic_templates ();
12187
12188             *is_parameter_pack = true;
12189           }
12190
12191         /* If the next token is an identifier, then it names the
12192            parameter.  */
12193         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12194           identifier = cp_parser_identifier (parser);
12195         else
12196           identifier = NULL_TREE;
12197
12198         /* Create the parameter.  */
12199         parameter = finish_template_type_parm (class_type_node, identifier);
12200
12201         /* If the next token is an `=', we have a default argument.  */
12202         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12203           {
12204             /* Consume the `=' token.  */
12205             cp_lexer_consume_token (parser->lexer);
12206             /* Parse the default-argument.  */
12207             push_deferring_access_checks (dk_no_deferred);
12208             default_argument = cp_parser_type_id (parser);
12209
12210             /* Template parameter packs cannot have default
12211                arguments. */
12212             if (*is_parameter_pack)
12213               {
12214                 if (identifier)
12215                   error_at (token->location,
12216                             "template parameter pack %qD cannot have a "
12217                             "default argument", identifier);
12218                 else
12219                   error_at (token->location,
12220                             "template parameter packs cannot have "
12221                             "default arguments");
12222                 default_argument = NULL_TREE;
12223               }
12224             pop_deferring_access_checks ();
12225           }
12226         else
12227           default_argument = NULL_TREE;
12228
12229         /* Create the combined representation of the parameter and the
12230            default argument.  */
12231         parameter = build_tree_list (default_argument, parameter);
12232       }
12233       break;
12234
12235     case RID_TEMPLATE:
12236       {
12237         tree identifier;
12238         tree default_argument;
12239
12240         /* Look for the `<'.  */
12241         cp_parser_require (parser, CPP_LESS, RT_LESS);
12242         /* Parse the template-parameter-list.  */
12243         cp_parser_template_parameter_list (parser);
12244         /* Look for the `>'.  */
12245         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12246         /* Look for the `class' keyword.  */
12247         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12248         /* If the next token is an ellipsis, we have a template
12249            argument pack. */
12250         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12251           {
12252             /* Consume the `...' token. */
12253             cp_lexer_consume_token (parser->lexer);
12254             maybe_warn_variadic_templates ();
12255
12256             *is_parameter_pack = true;
12257           }
12258         /* If the next token is an `=', then there is a
12259            default-argument.  If the next token is a `>', we are at
12260            the end of the parameter-list.  If the next token is a `,',
12261            then we are at the end of this parameter.  */
12262         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12263             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12264             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12265           {
12266             identifier = cp_parser_identifier (parser);
12267             /* Treat invalid names as if the parameter were nameless.  */
12268             if (identifier == error_mark_node)
12269               identifier = NULL_TREE;
12270           }
12271         else
12272           identifier = NULL_TREE;
12273
12274         /* Create the template parameter.  */
12275         parameter = finish_template_template_parm (class_type_node,
12276                                                    identifier);
12277
12278         /* If the next token is an `=', then there is a
12279            default-argument.  */
12280         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12281           {
12282             bool is_template;
12283
12284             /* Consume the `='.  */
12285             cp_lexer_consume_token (parser->lexer);
12286             /* Parse the id-expression.  */
12287             push_deferring_access_checks (dk_no_deferred);
12288             /* save token before parsing the id-expression, for error
12289                reporting */
12290             token = cp_lexer_peek_token (parser->lexer);
12291             default_argument
12292               = cp_parser_id_expression (parser,
12293                                          /*template_keyword_p=*/false,
12294                                          /*check_dependency_p=*/true,
12295                                          /*template_p=*/&is_template,
12296                                          /*declarator_p=*/false,
12297                                          /*optional_p=*/false);
12298             if (TREE_CODE (default_argument) == TYPE_DECL)
12299               /* If the id-expression was a template-id that refers to
12300                  a template-class, we already have the declaration here,
12301                  so no further lookup is needed.  */
12302                  ;
12303             else
12304               /* Look up the name.  */
12305               default_argument
12306                 = cp_parser_lookup_name (parser, default_argument,
12307                                          none_type,
12308                                          /*is_template=*/is_template,
12309                                          /*is_namespace=*/false,
12310                                          /*check_dependency=*/true,
12311                                          /*ambiguous_decls=*/NULL,
12312                                          token->location);
12313             /* See if the default argument is valid.  */
12314             default_argument
12315               = check_template_template_default_arg (default_argument);
12316
12317             /* Template parameter packs cannot have default
12318                arguments. */
12319             if (*is_parameter_pack)
12320               {
12321                 if (identifier)
12322                   error_at (token->location,
12323                             "template parameter pack %qD cannot "
12324                             "have a default argument",
12325                             identifier);
12326                 else
12327                   error_at (token->location, "template parameter packs cannot "
12328                             "have default arguments");
12329                 default_argument = NULL_TREE;
12330               }
12331             pop_deferring_access_checks ();
12332           }
12333         else
12334           default_argument = NULL_TREE;
12335
12336         /* Create the combined representation of the parameter and the
12337            default argument.  */
12338         parameter = build_tree_list (default_argument, parameter);
12339       }
12340       break;
12341
12342     default:
12343       gcc_unreachable ();
12344       break;
12345     }
12346
12347   return parameter;
12348 }
12349
12350 /* Parse a template-id.
12351
12352    template-id:
12353      template-name < template-argument-list [opt] >
12354
12355    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12356    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12357    returned.  Otherwise, if the template-name names a function, or set
12358    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12359    names a class, returns a TYPE_DECL for the specialization.
12360
12361    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12362    uninstantiated templates.  */
12363
12364 static tree
12365 cp_parser_template_id (cp_parser *parser,
12366                        bool template_keyword_p,
12367                        bool check_dependency_p,
12368                        bool is_declaration)
12369 {
12370   int i;
12371   tree templ;
12372   tree arguments;
12373   tree template_id;
12374   cp_token_position start_of_id = 0;
12375   deferred_access_check *chk;
12376   VEC (deferred_access_check,gc) *access_check;
12377   cp_token *next_token = NULL, *next_token_2 = NULL;
12378   bool is_identifier;
12379
12380   /* If the next token corresponds to a template-id, there is no need
12381      to reparse it.  */
12382   next_token = cp_lexer_peek_token (parser->lexer);
12383   if (next_token->type == CPP_TEMPLATE_ID)
12384     {
12385       struct tree_check *check_value;
12386
12387       /* Get the stored value.  */
12388       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12389       /* Perform any access checks that were deferred.  */
12390       access_check = check_value->checks;
12391       if (access_check)
12392         {
12393           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12394             perform_or_defer_access_check (chk->binfo,
12395                                            chk->decl,
12396                                            chk->diag_decl);
12397         }
12398       /* Return the stored value.  */
12399       return check_value->value;
12400     }
12401
12402   /* Avoid performing name lookup if there is no possibility of
12403      finding a template-id.  */
12404   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12405       || (next_token->type == CPP_NAME
12406           && !cp_parser_nth_token_starts_template_argument_list_p
12407                (parser, 2)))
12408     {
12409       cp_parser_error (parser, "expected template-id");
12410       return error_mark_node;
12411     }
12412
12413   /* Remember where the template-id starts.  */
12414   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12415     start_of_id = cp_lexer_token_position (parser->lexer, false);
12416
12417   push_deferring_access_checks (dk_deferred);
12418
12419   /* Parse the template-name.  */
12420   is_identifier = false;
12421   templ = cp_parser_template_name (parser, template_keyword_p,
12422                                    check_dependency_p,
12423                                    is_declaration,
12424                                    &is_identifier);
12425   if (templ == error_mark_node || is_identifier)
12426     {
12427       pop_deferring_access_checks ();
12428       return templ;
12429     }
12430
12431   /* If we find the sequence `[:' after a template-name, it's probably
12432      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12433      parse correctly the argument list.  */
12434   next_token = cp_lexer_peek_token (parser->lexer);
12435   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12436   if (next_token->type == CPP_OPEN_SQUARE
12437       && next_token->flags & DIGRAPH
12438       && next_token_2->type == CPP_COLON
12439       && !(next_token_2->flags & PREV_WHITE))
12440     {
12441       cp_parser_parse_tentatively (parser);
12442       /* Change `:' into `::'.  */
12443       next_token_2->type = CPP_SCOPE;
12444       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12445          CPP_LESS.  */
12446       cp_lexer_consume_token (parser->lexer);
12447
12448       /* Parse the arguments.  */
12449       arguments = cp_parser_enclosed_template_argument_list (parser);
12450       if (!cp_parser_parse_definitely (parser))
12451         {
12452           /* If we couldn't parse an argument list, then we revert our changes
12453              and return simply an error. Maybe this is not a template-id
12454              after all.  */
12455           next_token_2->type = CPP_COLON;
12456           cp_parser_error (parser, "expected %<<%>");
12457           pop_deferring_access_checks ();
12458           return error_mark_node;
12459         }
12460       /* Otherwise, emit an error about the invalid digraph, but continue
12461          parsing because we got our argument list.  */
12462       if (permerror (next_token->location,
12463                      "%<<::%> cannot begin a template-argument list"))
12464         {
12465           static bool hint = false;
12466           inform (next_token->location,
12467                   "%<<:%> is an alternate spelling for %<[%>."
12468                   " Insert whitespace between %<<%> and %<::%>");
12469           if (!hint && !flag_permissive)
12470             {
12471               inform (next_token->location, "(if you use %<-fpermissive%>"
12472                       " G++ will accept your code)");
12473               hint = true;
12474             }
12475         }
12476     }
12477   else
12478     {
12479       /* Look for the `<' that starts the template-argument-list.  */
12480       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12481         {
12482           pop_deferring_access_checks ();
12483           return error_mark_node;
12484         }
12485       /* Parse the arguments.  */
12486       arguments = cp_parser_enclosed_template_argument_list (parser);
12487     }
12488
12489   /* Build a representation of the specialization.  */
12490   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12491     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12492   else if (DECL_TYPE_TEMPLATE_P (templ)
12493            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12494     {
12495       bool entering_scope;
12496       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12497          template (rather than some instantiation thereof) only if
12498          is not nested within some other construct.  For example, in
12499          "template <typename T> void f(T) { A<T>::", A<T> is just an
12500          instantiation of A.  */
12501       entering_scope = (template_parm_scope_p ()
12502                         && cp_lexer_next_token_is (parser->lexer,
12503                                                    CPP_SCOPE));
12504       template_id
12505         = finish_template_type (templ, arguments, entering_scope);
12506     }
12507   else
12508     {
12509       /* If it's not a class-template or a template-template, it should be
12510          a function-template.  */
12511       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12512                    || TREE_CODE (templ) == OVERLOAD
12513                    || BASELINK_P (templ)));
12514
12515       template_id = lookup_template_function (templ, arguments);
12516     }
12517
12518   /* If parsing tentatively, replace the sequence of tokens that makes
12519      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12520      should we re-parse the token stream, we will not have to repeat
12521      the effort required to do the parse, nor will we issue duplicate
12522      error messages about problems during instantiation of the
12523      template.  */
12524   if (start_of_id)
12525     {
12526       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12527
12528       /* Reset the contents of the START_OF_ID token.  */
12529       token->type = CPP_TEMPLATE_ID;
12530       /* Retrieve any deferred checks.  Do not pop this access checks yet
12531          so the memory will not be reclaimed during token replacing below.  */
12532       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12533       token->u.tree_check_value->value = template_id;
12534       token->u.tree_check_value->checks = get_deferred_access_checks ();
12535       token->keyword = RID_MAX;
12536
12537       /* Purge all subsequent tokens.  */
12538       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12539
12540       /* ??? Can we actually assume that, if template_id ==
12541          error_mark_node, we will have issued a diagnostic to the
12542          user, as opposed to simply marking the tentative parse as
12543          failed?  */
12544       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12545         error_at (token->location, "parse error in template argument list");
12546     }
12547
12548   pop_deferring_access_checks ();
12549   return template_id;
12550 }
12551
12552 /* Parse a template-name.
12553
12554    template-name:
12555      identifier
12556
12557    The standard should actually say:
12558
12559    template-name:
12560      identifier
12561      operator-function-id
12562
12563    A defect report has been filed about this issue.
12564
12565    A conversion-function-id cannot be a template name because they cannot
12566    be part of a template-id. In fact, looking at this code:
12567
12568    a.operator K<int>()
12569
12570    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12571    It is impossible to call a templated conversion-function-id with an
12572    explicit argument list, since the only allowed template parameter is
12573    the type to which it is converting.
12574
12575    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12576    `template' keyword, in a construction like:
12577
12578      T::template f<3>()
12579
12580    In that case `f' is taken to be a template-name, even though there
12581    is no way of knowing for sure.
12582
12583    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12584    name refers to a set of overloaded functions, at least one of which
12585    is a template, or an IDENTIFIER_NODE with the name of the template,
12586    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12587    names are looked up inside uninstantiated templates.  */
12588
12589 static tree
12590 cp_parser_template_name (cp_parser* parser,
12591                          bool template_keyword_p,
12592                          bool check_dependency_p,
12593                          bool is_declaration,
12594                          bool *is_identifier)
12595 {
12596   tree identifier;
12597   tree decl;
12598   tree fns;
12599   cp_token *token = cp_lexer_peek_token (parser->lexer);
12600
12601   /* If the next token is `operator', then we have either an
12602      operator-function-id or a conversion-function-id.  */
12603   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12604     {
12605       /* We don't know whether we're looking at an
12606          operator-function-id or a conversion-function-id.  */
12607       cp_parser_parse_tentatively (parser);
12608       /* Try an operator-function-id.  */
12609       identifier = cp_parser_operator_function_id (parser);
12610       /* If that didn't work, try a conversion-function-id.  */
12611       if (!cp_parser_parse_definitely (parser))
12612         {
12613           cp_parser_error (parser, "expected template-name");
12614           return error_mark_node;
12615         }
12616     }
12617   /* Look for the identifier.  */
12618   else
12619     identifier = cp_parser_identifier (parser);
12620
12621   /* If we didn't find an identifier, we don't have a template-id.  */
12622   if (identifier == error_mark_node)
12623     return error_mark_node;
12624
12625   /* If the name immediately followed the `template' keyword, then it
12626      is a template-name.  However, if the next token is not `<', then
12627      we do not treat it as a template-name, since it is not being used
12628      as part of a template-id.  This enables us to handle constructs
12629      like:
12630
12631        template <typename T> struct S { S(); };
12632        template <typename T> S<T>::S();
12633
12634      correctly.  We would treat `S' as a template -- if it were `S<T>'
12635      -- but we do not if there is no `<'.  */
12636
12637   if (processing_template_decl
12638       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12639     {
12640       /* In a declaration, in a dependent context, we pretend that the
12641          "template" keyword was present in order to improve error
12642          recovery.  For example, given:
12643
12644            template <typename T> void f(T::X<int>);
12645
12646          we want to treat "X<int>" as a template-id.  */
12647       if (is_declaration
12648           && !template_keyword_p
12649           && parser->scope && TYPE_P (parser->scope)
12650           && check_dependency_p
12651           && dependent_scope_p (parser->scope)
12652           /* Do not do this for dtors (or ctors), since they never
12653              need the template keyword before their name.  */
12654           && !constructor_name_p (identifier, parser->scope))
12655         {
12656           cp_token_position start = 0;
12657
12658           /* Explain what went wrong.  */
12659           error_at (token->location, "non-template %qD used as template",
12660                     identifier);
12661           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12662                   parser->scope, identifier);
12663           /* If parsing tentatively, find the location of the "<" token.  */
12664           if (cp_parser_simulate_error (parser))
12665             start = cp_lexer_token_position (parser->lexer, true);
12666           /* Parse the template arguments so that we can issue error
12667              messages about them.  */
12668           cp_lexer_consume_token (parser->lexer);
12669           cp_parser_enclosed_template_argument_list (parser);
12670           /* Skip tokens until we find a good place from which to
12671              continue parsing.  */
12672           cp_parser_skip_to_closing_parenthesis (parser,
12673                                                  /*recovering=*/true,
12674                                                  /*or_comma=*/true,
12675                                                  /*consume_paren=*/false);
12676           /* If parsing tentatively, permanently remove the
12677              template argument list.  That will prevent duplicate
12678              error messages from being issued about the missing
12679              "template" keyword.  */
12680           if (start)
12681             cp_lexer_purge_tokens_after (parser->lexer, start);
12682           if (is_identifier)
12683             *is_identifier = true;
12684           return identifier;
12685         }
12686
12687       /* If the "template" keyword is present, then there is generally
12688          no point in doing name-lookup, so we just return IDENTIFIER.
12689          But, if the qualifying scope is non-dependent then we can
12690          (and must) do name-lookup normally.  */
12691       if (template_keyword_p
12692           && (!parser->scope
12693               || (TYPE_P (parser->scope)
12694                   && dependent_type_p (parser->scope))))
12695         return identifier;
12696     }
12697
12698   /* Look up the name.  */
12699   decl = cp_parser_lookup_name (parser, identifier,
12700                                 none_type,
12701                                 /*is_template=*/true,
12702                                 /*is_namespace=*/false,
12703                                 check_dependency_p,
12704                                 /*ambiguous_decls=*/NULL,
12705                                 token->location);
12706
12707   /* If DECL is a template, then the name was a template-name.  */
12708   if (TREE_CODE (decl) == TEMPLATE_DECL)
12709     ;
12710   else
12711     {
12712       tree fn = NULL_TREE;
12713
12714       /* The standard does not explicitly indicate whether a name that
12715          names a set of overloaded declarations, some of which are
12716          templates, is a template-name.  However, such a name should
12717          be a template-name; otherwise, there is no way to form a
12718          template-id for the overloaded templates.  */
12719       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12720       if (TREE_CODE (fns) == OVERLOAD)
12721         for (fn = fns; fn; fn = OVL_NEXT (fn))
12722           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12723             break;
12724
12725       if (!fn)
12726         {
12727           /* The name does not name a template.  */
12728           cp_parser_error (parser, "expected template-name");
12729           return error_mark_node;
12730         }
12731     }
12732
12733   /* If DECL is dependent, and refers to a function, then just return
12734      its name; we will look it up again during template instantiation.  */
12735   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12736     {
12737       tree scope = ovl_scope (decl);
12738       if (TYPE_P (scope) && dependent_type_p (scope))
12739         return identifier;
12740     }
12741
12742   return decl;
12743 }
12744
12745 /* Parse a template-argument-list.
12746
12747    template-argument-list:
12748      template-argument ... [opt]
12749      template-argument-list , template-argument ... [opt]
12750
12751    Returns a TREE_VEC containing the arguments.  */
12752
12753 static tree
12754 cp_parser_template_argument_list (cp_parser* parser)
12755 {
12756   tree fixed_args[10];
12757   unsigned n_args = 0;
12758   unsigned alloced = 10;
12759   tree *arg_ary = fixed_args;
12760   tree vec;
12761   bool saved_in_template_argument_list_p;
12762   bool saved_ice_p;
12763   bool saved_non_ice_p;
12764
12765   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12766   parser->in_template_argument_list_p = true;
12767   /* Even if the template-id appears in an integral
12768      constant-expression, the contents of the argument list do
12769      not.  */
12770   saved_ice_p = parser->integral_constant_expression_p;
12771   parser->integral_constant_expression_p = false;
12772   saved_non_ice_p = parser->non_integral_constant_expression_p;
12773   parser->non_integral_constant_expression_p = false;
12774
12775   /* Parse the arguments.  */
12776   do
12777     {
12778       tree argument;
12779
12780       if (n_args)
12781         /* Consume the comma.  */
12782         cp_lexer_consume_token (parser->lexer);
12783
12784       /* Parse the template-argument.  */
12785       argument = cp_parser_template_argument (parser);
12786
12787       /* If the next token is an ellipsis, we're expanding a template
12788          argument pack. */
12789       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12790         {
12791           if (argument == error_mark_node)
12792             {
12793               cp_token *token = cp_lexer_peek_token (parser->lexer);
12794               error_at (token->location,
12795                         "expected parameter pack before %<...%>");
12796             }
12797           /* Consume the `...' token. */
12798           cp_lexer_consume_token (parser->lexer);
12799
12800           /* Make the argument into a TYPE_PACK_EXPANSION or
12801              EXPR_PACK_EXPANSION. */
12802           argument = make_pack_expansion (argument);
12803         }
12804
12805       if (n_args == alloced)
12806         {
12807           alloced *= 2;
12808
12809           if (arg_ary == fixed_args)
12810             {
12811               arg_ary = XNEWVEC (tree, alloced);
12812               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12813             }
12814           else
12815             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12816         }
12817       arg_ary[n_args++] = argument;
12818     }
12819   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12820
12821   vec = make_tree_vec (n_args);
12822
12823   while (n_args--)
12824     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12825
12826   if (arg_ary != fixed_args)
12827     free (arg_ary);
12828   parser->non_integral_constant_expression_p = saved_non_ice_p;
12829   parser->integral_constant_expression_p = saved_ice_p;
12830   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12831 #ifdef ENABLE_CHECKING
12832   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12833 #endif
12834   return vec;
12835 }
12836
12837 /* Parse a template-argument.
12838
12839    template-argument:
12840      assignment-expression
12841      type-id
12842      id-expression
12843
12844    The representation is that of an assignment-expression, type-id, or
12845    id-expression -- except that the qualified id-expression is
12846    evaluated, so that the value returned is either a DECL or an
12847    OVERLOAD.
12848
12849    Although the standard says "assignment-expression", it forbids
12850    throw-expressions or assignments in the template argument.
12851    Therefore, we use "conditional-expression" instead.  */
12852
12853 static tree
12854 cp_parser_template_argument (cp_parser* parser)
12855 {
12856   tree argument;
12857   bool template_p;
12858   bool address_p;
12859   bool maybe_type_id = false;
12860   cp_token *token = NULL, *argument_start_token = NULL;
12861   cp_id_kind idk;
12862
12863   /* There's really no way to know what we're looking at, so we just
12864      try each alternative in order.
12865
12866        [temp.arg]
12867
12868        In a template-argument, an ambiguity between a type-id and an
12869        expression is resolved to a type-id, regardless of the form of
12870        the corresponding template-parameter.
12871
12872      Therefore, we try a type-id first.  */
12873   cp_parser_parse_tentatively (parser);
12874   argument = cp_parser_template_type_arg (parser);
12875   /* If there was no error parsing the type-id but the next token is a
12876      '>>', our behavior depends on which dialect of C++ we're
12877      parsing. In C++98, we probably found a typo for '> >'. But there
12878      are type-id which are also valid expressions. For instance:
12879
12880      struct X { int operator >> (int); };
12881      template <int V> struct Foo {};
12882      Foo<X () >> 5> r;
12883
12884      Here 'X()' is a valid type-id of a function type, but the user just
12885      wanted to write the expression "X() >> 5". Thus, we remember that we
12886      found a valid type-id, but we still try to parse the argument as an
12887      expression to see what happens. 
12888
12889      In C++0x, the '>>' will be considered two separate '>'
12890      tokens.  */
12891   if (!cp_parser_error_occurred (parser)
12892       && cxx_dialect == cxx98
12893       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12894     {
12895       maybe_type_id = true;
12896       cp_parser_abort_tentative_parse (parser);
12897     }
12898   else
12899     {
12900       /* If the next token isn't a `,' or a `>', then this argument wasn't
12901       really finished. This means that the argument is not a valid
12902       type-id.  */
12903       if (!cp_parser_next_token_ends_template_argument_p (parser))
12904         cp_parser_error (parser, "expected template-argument");
12905       /* If that worked, we're done.  */
12906       if (cp_parser_parse_definitely (parser))
12907         return argument;
12908     }
12909   /* We're still not sure what the argument will be.  */
12910   cp_parser_parse_tentatively (parser);
12911   /* Try a template.  */
12912   argument_start_token = cp_lexer_peek_token (parser->lexer);
12913   argument = cp_parser_id_expression (parser,
12914                                       /*template_keyword_p=*/false,
12915                                       /*check_dependency_p=*/true,
12916                                       &template_p,
12917                                       /*declarator_p=*/false,
12918                                       /*optional_p=*/false);
12919   /* If the next token isn't a `,' or a `>', then this argument wasn't
12920      really finished.  */
12921   if (!cp_parser_next_token_ends_template_argument_p (parser))
12922     cp_parser_error (parser, "expected template-argument");
12923   if (!cp_parser_error_occurred (parser))
12924     {
12925       /* Figure out what is being referred to.  If the id-expression
12926          was for a class template specialization, then we will have a
12927          TYPE_DECL at this point.  There is no need to do name lookup
12928          at this point in that case.  */
12929       if (TREE_CODE (argument) != TYPE_DECL)
12930         argument = cp_parser_lookup_name (parser, argument,
12931                                           none_type,
12932                                           /*is_template=*/template_p,
12933                                           /*is_namespace=*/false,
12934                                           /*check_dependency=*/true,
12935                                           /*ambiguous_decls=*/NULL,
12936                                           argument_start_token->location);
12937       if (TREE_CODE (argument) != TEMPLATE_DECL
12938           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12939         cp_parser_error (parser, "expected template-name");
12940     }
12941   if (cp_parser_parse_definitely (parser))
12942     return argument;
12943   /* It must be a non-type argument.  There permitted cases are given
12944      in [temp.arg.nontype]:
12945
12946      -- an integral constant-expression of integral or enumeration
12947         type; or
12948
12949      -- the name of a non-type template-parameter; or
12950
12951      -- the name of an object or function with external linkage...
12952
12953      -- the address of an object or function with external linkage...
12954
12955      -- a pointer to member...  */
12956   /* Look for a non-type template parameter.  */
12957   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12958     {
12959       cp_parser_parse_tentatively (parser);
12960       argument = cp_parser_primary_expression (parser,
12961                                                /*address_p=*/false,
12962                                                /*cast_p=*/false,
12963                                                /*template_arg_p=*/true,
12964                                                &idk);
12965       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12966           || !cp_parser_next_token_ends_template_argument_p (parser))
12967         cp_parser_simulate_error (parser);
12968       if (cp_parser_parse_definitely (parser))
12969         return argument;
12970     }
12971
12972   /* If the next token is "&", the argument must be the address of an
12973      object or function with external linkage.  */
12974   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12975   if (address_p)
12976     cp_lexer_consume_token (parser->lexer);
12977   /* See if we might have an id-expression.  */
12978   token = cp_lexer_peek_token (parser->lexer);
12979   if (token->type == CPP_NAME
12980       || token->keyword == RID_OPERATOR
12981       || token->type == CPP_SCOPE
12982       || token->type == CPP_TEMPLATE_ID
12983       || token->type == CPP_NESTED_NAME_SPECIFIER)
12984     {
12985       cp_parser_parse_tentatively (parser);
12986       argument = cp_parser_primary_expression (parser,
12987                                                address_p,
12988                                                /*cast_p=*/false,
12989                                                /*template_arg_p=*/true,
12990                                                &idk);
12991       if (cp_parser_error_occurred (parser)
12992           || !cp_parser_next_token_ends_template_argument_p (parser))
12993         cp_parser_abort_tentative_parse (parser);
12994       else
12995         {
12996           tree probe;
12997
12998           if (TREE_CODE (argument) == INDIRECT_REF)
12999             {
13000               gcc_assert (REFERENCE_REF_P (argument));
13001               argument = TREE_OPERAND (argument, 0);
13002             }
13003
13004           /* If we're in a template, we represent a qualified-id referring
13005              to a static data member as a SCOPE_REF even if the scope isn't
13006              dependent so that we can check access control later.  */
13007           probe = argument;
13008           if (TREE_CODE (probe) == SCOPE_REF)
13009             probe = TREE_OPERAND (probe, 1);
13010           if (TREE_CODE (probe) == VAR_DECL)
13011             {
13012               /* A variable without external linkage might still be a
13013                  valid constant-expression, so no error is issued here
13014                  if the external-linkage check fails.  */
13015               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13016                 cp_parser_simulate_error (parser);
13017             }
13018           else if (is_overloaded_fn (argument))
13019             /* All overloaded functions are allowed; if the external
13020                linkage test does not pass, an error will be issued
13021                later.  */
13022             ;
13023           else if (address_p
13024                    && (TREE_CODE (argument) == OFFSET_REF
13025                        || TREE_CODE (argument) == SCOPE_REF))
13026             /* A pointer-to-member.  */
13027             ;
13028           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13029             ;
13030           else
13031             cp_parser_simulate_error (parser);
13032
13033           if (cp_parser_parse_definitely (parser))
13034             {
13035               if (address_p)
13036                 argument = build_x_unary_op (ADDR_EXPR, argument,
13037                                              tf_warning_or_error);
13038               return argument;
13039             }
13040         }
13041     }
13042   /* If the argument started with "&", there are no other valid
13043      alternatives at this point.  */
13044   if (address_p)
13045     {
13046       cp_parser_error (parser, "invalid non-type template argument");
13047       return error_mark_node;
13048     }
13049
13050   /* If the argument wasn't successfully parsed as a type-id followed
13051      by '>>', the argument can only be a constant expression now.
13052      Otherwise, we try parsing the constant-expression tentatively,
13053      because the argument could really be a type-id.  */
13054   if (maybe_type_id)
13055     cp_parser_parse_tentatively (parser);
13056   argument = cp_parser_constant_expression (parser,
13057                                             /*allow_non_constant_p=*/false,
13058                                             /*non_constant_p=*/NULL);
13059   argument = fold_non_dependent_expr (argument);
13060   if (!maybe_type_id)
13061     return argument;
13062   if (!cp_parser_next_token_ends_template_argument_p (parser))
13063     cp_parser_error (parser, "expected template-argument");
13064   if (cp_parser_parse_definitely (parser))
13065     return argument;
13066   /* We did our best to parse the argument as a non type-id, but that
13067      was the only alternative that matched (albeit with a '>' after
13068      it). We can assume it's just a typo from the user, and a
13069      diagnostic will then be issued.  */
13070   return cp_parser_template_type_arg (parser);
13071 }
13072
13073 /* Parse an explicit-instantiation.
13074
13075    explicit-instantiation:
13076      template declaration
13077
13078    Although the standard says `declaration', what it really means is:
13079
13080    explicit-instantiation:
13081      template decl-specifier-seq [opt] declarator [opt] ;
13082
13083    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13084    supposed to be allowed.  A defect report has been filed about this
13085    issue.
13086
13087    GNU Extension:
13088
13089    explicit-instantiation:
13090      storage-class-specifier template
13091        decl-specifier-seq [opt] declarator [opt] ;
13092      function-specifier template
13093        decl-specifier-seq [opt] declarator [opt] ;  */
13094
13095 static void
13096 cp_parser_explicit_instantiation (cp_parser* parser)
13097 {
13098   int declares_class_or_enum;
13099   cp_decl_specifier_seq decl_specifiers;
13100   tree extension_specifier = NULL_TREE;
13101
13102   timevar_push (TV_TEMPLATE_INST);
13103
13104   /* Look for an (optional) storage-class-specifier or
13105      function-specifier.  */
13106   if (cp_parser_allow_gnu_extensions_p (parser))
13107     {
13108       extension_specifier
13109         = cp_parser_storage_class_specifier_opt (parser);
13110       if (!extension_specifier)
13111         extension_specifier
13112           = cp_parser_function_specifier_opt (parser,
13113                                               /*decl_specs=*/NULL);
13114     }
13115
13116   /* Look for the `template' keyword.  */
13117   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13118   /* Let the front end know that we are processing an explicit
13119      instantiation.  */
13120   begin_explicit_instantiation ();
13121   /* [temp.explicit] says that we are supposed to ignore access
13122      control while processing explicit instantiation directives.  */
13123   push_deferring_access_checks (dk_no_check);
13124   /* Parse a decl-specifier-seq.  */
13125   cp_parser_decl_specifier_seq (parser,
13126                                 CP_PARSER_FLAGS_OPTIONAL,
13127                                 &decl_specifiers,
13128                                 &declares_class_or_enum);
13129   /* If there was exactly one decl-specifier, and it declared a class,
13130      and there's no declarator, then we have an explicit type
13131      instantiation.  */
13132   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13133     {
13134       tree type;
13135
13136       type = check_tag_decl (&decl_specifiers);
13137       /* Turn access control back on for names used during
13138          template instantiation.  */
13139       pop_deferring_access_checks ();
13140       if (type)
13141         do_type_instantiation (type, extension_specifier,
13142                                /*complain=*/tf_error);
13143     }
13144   else
13145     {
13146       cp_declarator *declarator;
13147       tree decl;
13148
13149       /* Parse the declarator.  */
13150       declarator
13151         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13152                                 /*ctor_dtor_or_conv_p=*/NULL,
13153                                 /*parenthesized_p=*/NULL,
13154                                 /*member_p=*/false);
13155       if (declares_class_or_enum & 2)
13156         cp_parser_check_for_definition_in_return_type (declarator,
13157                                                        decl_specifiers.type,
13158                                                        decl_specifiers.type_location);
13159       if (declarator != cp_error_declarator)
13160         {
13161           if (decl_specifiers.specs[(int)ds_inline])
13162             permerror (input_location, "explicit instantiation shall not use"
13163                        " %<inline%> specifier");
13164           if (decl_specifiers.specs[(int)ds_constexpr])
13165             permerror (input_location, "explicit instantiation shall not use"
13166                        " %<constexpr%> specifier");
13167
13168           decl = grokdeclarator (declarator, &decl_specifiers,
13169                                  NORMAL, 0, &decl_specifiers.attributes);
13170           /* Turn access control back on for names used during
13171              template instantiation.  */
13172           pop_deferring_access_checks ();
13173           /* Do the explicit instantiation.  */
13174           do_decl_instantiation (decl, extension_specifier);
13175         }
13176       else
13177         {
13178           pop_deferring_access_checks ();
13179           /* Skip the body of the explicit instantiation.  */
13180           cp_parser_skip_to_end_of_statement (parser);
13181         }
13182     }
13183   /* We're done with the instantiation.  */
13184   end_explicit_instantiation ();
13185
13186   cp_parser_consume_semicolon_at_end_of_statement (parser);
13187
13188   timevar_pop (TV_TEMPLATE_INST);
13189 }
13190
13191 /* Parse an explicit-specialization.
13192
13193    explicit-specialization:
13194      template < > declaration
13195
13196    Although the standard says `declaration', what it really means is:
13197
13198    explicit-specialization:
13199      template <> decl-specifier [opt] init-declarator [opt] ;
13200      template <> function-definition
13201      template <> explicit-specialization
13202      template <> template-declaration  */
13203
13204 static void
13205 cp_parser_explicit_specialization (cp_parser* parser)
13206 {
13207   bool need_lang_pop;
13208   cp_token *token = cp_lexer_peek_token (parser->lexer);
13209
13210   /* Look for the `template' keyword.  */
13211   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13212   /* Look for the `<'.  */
13213   cp_parser_require (parser, CPP_LESS, RT_LESS);
13214   /* Look for the `>'.  */
13215   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13216   /* We have processed another parameter list.  */
13217   ++parser->num_template_parameter_lists;
13218   /* [temp]
13219
13220      A template ... explicit specialization ... shall not have C
13221      linkage.  */
13222   if (current_lang_name == lang_name_c)
13223     {
13224       error_at (token->location, "template specialization with C linkage");
13225       /* Give it C++ linkage to avoid confusing other parts of the
13226          front end.  */
13227       push_lang_context (lang_name_cplusplus);
13228       need_lang_pop = true;
13229     }
13230   else
13231     need_lang_pop = false;
13232   /* Let the front end know that we are beginning a specialization.  */
13233   if (!begin_specialization ())
13234     {
13235       end_specialization ();
13236       return;
13237     }
13238
13239   /* If the next keyword is `template', we need to figure out whether
13240      or not we're looking a template-declaration.  */
13241   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13242     {
13243       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13244           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13245         cp_parser_template_declaration_after_export (parser,
13246                                                      /*member_p=*/false);
13247       else
13248         cp_parser_explicit_specialization (parser);
13249     }
13250   else
13251     /* Parse the dependent declaration.  */
13252     cp_parser_single_declaration (parser,
13253                                   /*checks=*/NULL,
13254                                   /*member_p=*/false,
13255                                   /*explicit_specialization_p=*/true,
13256                                   /*friend_p=*/NULL);
13257   /* We're done with the specialization.  */
13258   end_specialization ();
13259   /* For the erroneous case of a template with C linkage, we pushed an
13260      implicit C++ linkage scope; exit that scope now.  */
13261   if (need_lang_pop)
13262     pop_lang_context ();
13263   /* We're done with this parameter list.  */
13264   --parser->num_template_parameter_lists;
13265 }
13266
13267 /* Parse a type-specifier.
13268
13269    type-specifier:
13270      simple-type-specifier
13271      class-specifier
13272      enum-specifier
13273      elaborated-type-specifier
13274      cv-qualifier
13275
13276    GNU Extension:
13277
13278    type-specifier:
13279      __complex__
13280
13281    Returns a representation of the type-specifier.  For a
13282    class-specifier, enum-specifier, or elaborated-type-specifier, a
13283    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13284
13285    The parser flags FLAGS is used to control type-specifier parsing.
13286
13287    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13288    in a decl-specifier-seq.
13289
13290    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13291    class-specifier, enum-specifier, or elaborated-type-specifier, then
13292    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13293    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13294    zero.
13295
13296    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13297    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13298    is set to FALSE.  */
13299
13300 static tree
13301 cp_parser_type_specifier (cp_parser* parser,
13302                           cp_parser_flags flags,
13303                           cp_decl_specifier_seq *decl_specs,
13304                           bool is_declaration,
13305                           int* declares_class_or_enum,
13306                           bool* is_cv_qualifier)
13307 {
13308   tree type_spec = NULL_TREE;
13309   cp_token *token;
13310   enum rid keyword;
13311   cp_decl_spec ds = ds_last;
13312
13313   /* Assume this type-specifier does not declare a new type.  */
13314   if (declares_class_or_enum)
13315     *declares_class_or_enum = 0;
13316   /* And that it does not specify a cv-qualifier.  */
13317   if (is_cv_qualifier)
13318     *is_cv_qualifier = false;
13319   /* Peek at the next token.  */
13320   token = cp_lexer_peek_token (parser->lexer);
13321
13322   /* If we're looking at a keyword, we can use that to guide the
13323      production we choose.  */
13324   keyword = token->keyword;
13325   switch (keyword)
13326     {
13327     case RID_ENUM:
13328       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13329         goto elaborated_type_specifier;
13330
13331       /* Look for the enum-specifier.  */
13332       type_spec = cp_parser_enum_specifier (parser);
13333       /* If that worked, we're done.  */
13334       if (type_spec)
13335         {
13336           if (declares_class_or_enum)
13337             *declares_class_or_enum = 2;
13338           if (decl_specs)
13339             cp_parser_set_decl_spec_type (decl_specs,
13340                                           type_spec,
13341                                           token->location,
13342                                           /*type_definition_p=*/true);
13343           return type_spec;
13344         }
13345       else
13346         goto elaborated_type_specifier;
13347
13348       /* Any of these indicate either a class-specifier, or an
13349          elaborated-type-specifier.  */
13350     case RID_CLASS:
13351     case RID_STRUCT:
13352     case RID_UNION:
13353       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13354         goto elaborated_type_specifier;
13355
13356       /* Parse tentatively so that we can back up if we don't find a
13357          class-specifier.  */
13358       cp_parser_parse_tentatively (parser);
13359       /* Look for the class-specifier.  */
13360       type_spec = cp_parser_class_specifier (parser);
13361       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13362       /* If that worked, we're done.  */
13363       if (cp_parser_parse_definitely (parser))
13364         {
13365           if (declares_class_or_enum)
13366             *declares_class_or_enum = 2;
13367           if (decl_specs)
13368             cp_parser_set_decl_spec_type (decl_specs,
13369                                           type_spec,
13370                                           token->location,
13371                                           /*type_definition_p=*/true);
13372           return type_spec;
13373         }
13374
13375       /* Fall through.  */
13376     elaborated_type_specifier:
13377       /* We're declaring (not defining) a class or enum.  */
13378       if (declares_class_or_enum)
13379         *declares_class_or_enum = 1;
13380
13381       /* Fall through.  */
13382     case RID_TYPENAME:
13383       /* Look for an elaborated-type-specifier.  */
13384       type_spec
13385         = (cp_parser_elaborated_type_specifier
13386            (parser,
13387             decl_specs && decl_specs->specs[(int) ds_friend],
13388             is_declaration));
13389       if (decl_specs)
13390         cp_parser_set_decl_spec_type (decl_specs,
13391                                       type_spec,
13392                                       token->location,
13393                                       /*type_definition_p=*/false);
13394       return type_spec;
13395
13396     case RID_CONST:
13397       ds = ds_const;
13398       if (is_cv_qualifier)
13399         *is_cv_qualifier = true;
13400       break;
13401
13402     case RID_VOLATILE:
13403       ds = ds_volatile;
13404       if (is_cv_qualifier)
13405         *is_cv_qualifier = true;
13406       break;
13407
13408     case RID_RESTRICT:
13409       ds = ds_restrict;
13410       if (is_cv_qualifier)
13411         *is_cv_qualifier = true;
13412       break;
13413
13414     case RID_COMPLEX:
13415       /* The `__complex__' keyword is a GNU extension.  */
13416       ds = ds_complex;
13417       break;
13418
13419     default:
13420       break;
13421     }
13422
13423   /* Handle simple keywords.  */
13424   if (ds != ds_last)
13425     {
13426       if (decl_specs)
13427         {
13428           ++decl_specs->specs[(int)ds];
13429           decl_specs->any_specifiers_p = true;
13430         }
13431       return cp_lexer_consume_token (parser->lexer)->u.value;
13432     }
13433
13434   /* If we do not already have a type-specifier, assume we are looking
13435      at a simple-type-specifier.  */
13436   type_spec = cp_parser_simple_type_specifier (parser,
13437                                                decl_specs,
13438                                                flags);
13439
13440   /* If we didn't find a type-specifier, and a type-specifier was not
13441      optional in this context, issue an error message.  */
13442   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13443     {
13444       cp_parser_error (parser, "expected type specifier");
13445       return error_mark_node;
13446     }
13447
13448   return type_spec;
13449 }
13450
13451 /* Parse a simple-type-specifier.
13452
13453    simple-type-specifier:
13454      :: [opt] nested-name-specifier [opt] type-name
13455      :: [opt] nested-name-specifier template template-id
13456      char
13457      wchar_t
13458      bool
13459      short
13460      int
13461      long
13462      signed
13463      unsigned
13464      float
13465      double
13466      void
13467
13468    C++0x Extension:
13469
13470    simple-type-specifier:
13471      auto
13472      decltype ( expression )   
13473      char16_t
13474      char32_t
13475      __underlying_type ( type-id )
13476
13477    GNU Extension:
13478
13479    simple-type-specifier:
13480      __int128
13481      __typeof__ unary-expression
13482      __typeof__ ( type-id )
13483
13484    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13485    appropriately updated.  */
13486
13487 static tree
13488 cp_parser_simple_type_specifier (cp_parser* parser,
13489                                  cp_decl_specifier_seq *decl_specs,
13490                                  cp_parser_flags flags)
13491 {
13492   tree type = NULL_TREE;
13493   cp_token *token;
13494
13495   /* Peek at the next token.  */
13496   token = cp_lexer_peek_token (parser->lexer);
13497
13498   /* If we're looking at a keyword, things are easy.  */
13499   switch (token->keyword)
13500     {
13501     case RID_CHAR:
13502       if (decl_specs)
13503         decl_specs->explicit_char_p = true;
13504       type = char_type_node;
13505       break;
13506     case RID_CHAR16:
13507       type = char16_type_node;
13508       break;
13509     case RID_CHAR32:
13510       type = char32_type_node;
13511       break;
13512     case RID_WCHAR:
13513       type = wchar_type_node;
13514       break;
13515     case RID_BOOL:
13516       type = boolean_type_node;
13517       break;
13518     case RID_SHORT:
13519       if (decl_specs)
13520         ++decl_specs->specs[(int) ds_short];
13521       type = short_integer_type_node;
13522       break;
13523     case RID_INT:
13524       if (decl_specs)
13525         decl_specs->explicit_int_p = true;
13526       type = integer_type_node;
13527       break;
13528     case RID_INT128:
13529       if (!int128_integer_type_node)
13530         break;
13531       if (decl_specs)
13532         decl_specs->explicit_int128_p = true;
13533       type = int128_integer_type_node;
13534       break;
13535     case RID_LONG:
13536       if (decl_specs)
13537         ++decl_specs->specs[(int) ds_long];
13538       type = long_integer_type_node;
13539       break;
13540     case RID_SIGNED:
13541       if (decl_specs)
13542         ++decl_specs->specs[(int) ds_signed];
13543       type = integer_type_node;
13544       break;
13545     case RID_UNSIGNED:
13546       if (decl_specs)
13547         ++decl_specs->specs[(int) ds_unsigned];
13548       type = unsigned_type_node;
13549       break;
13550     case RID_FLOAT:
13551       type = float_type_node;
13552       break;
13553     case RID_DOUBLE:
13554       type = double_type_node;
13555       break;
13556     case RID_VOID:
13557       type = void_type_node;
13558       break;
13559       
13560     case RID_AUTO:
13561       maybe_warn_cpp0x (CPP0X_AUTO);
13562       type = make_auto ();
13563       break;
13564
13565     case RID_DECLTYPE:
13566       /* Since DR 743, decltype can either be a simple-type-specifier by
13567          itself or begin a nested-name-specifier.  Parsing it will replace
13568          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13569          handling below decide what to do.  */
13570       cp_parser_decltype (parser);
13571       cp_lexer_set_token_position (parser->lexer, token);
13572       break;
13573
13574     case RID_TYPEOF:
13575       /* Consume the `typeof' token.  */
13576       cp_lexer_consume_token (parser->lexer);
13577       /* Parse the operand to `typeof'.  */
13578       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13579       /* If it is not already a TYPE, take its type.  */
13580       if (!TYPE_P (type))
13581         type = finish_typeof (type);
13582
13583       if (decl_specs)
13584         cp_parser_set_decl_spec_type (decl_specs, type,
13585                                       token->location,
13586                                       /*type_definition_p=*/false);
13587
13588       return type;
13589
13590     case RID_UNDERLYING_TYPE:
13591       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13592       if (decl_specs)
13593         cp_parser_set_decl_spec_type (decl_specs, type,
13594                                       token->location,
13595                                       /*type_definition_p=*/false);
13596
13597       return type;
13598
13599     case RID_BASES:
13600     case RID_DIRECT_BASES:
13601       type = cp_parser_trait_expr (parser, token->keyword);
13602       if (decl_specs)
13603        cp_parser_set_decl_spec_type (decl_specs, type,
13604                                      token->location,
13605                                      /*type_definition_p=*/false);
13606       return type;
13607     default:
13608       break;
13609     }
13610
13611   /* If token is an already-parsed decltype not followed by ::,
13612      it's a simple-type-specifier.  */
13613   if (token->type == CPP_DECLTYPE
13614       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13615     {
13616       type = token->u.value;
13617       if (decl_specs)
13618         cp_parser_set_decl_spec_type (decl_specs, type,
13619                                       token->location,
13620                                       /*type_definition_p=*/false);
13621       cp_lexer_consume_token (parser->lexer);
13622       return type;
13623     }
13624
13625   /* If the type-specifier was for a built-in type, we're done.  */
13626   if (type)
13627     {
13628       /* Record the type.  */
13629       if (decl_specs
13630           && (token->keyword != RID_SIGNED
13631               && token->keyword != RID_UNSIGNED
13632               && token->keyword != RID_SHORT
13633               && token->keyword != RID_LONG))
13634         cp_parser_set_decl_spec_type (decl_specs,
13635                                       type,
13636                                       token->location,
13637                                       /*type_definition_p=*/false);
13638       if (decl_specs)
13639         decl_specs->any_specifiers_p = true;
13640
13641       /* Consume the token.  */
13642       cp_lexer_consume_token (parser->lexer);
13643
13644       /* There is no valid C++ program where a non-template type is
13645          followed by a "<".  That usually indicates that the user thought
13646          that the type was a template.  */
13647       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13648
13649       return TYPE_NAME (type);
13650     }
13651
13652   /* The type-specifier must be a user-defined type.  */
13653   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13654     {
13655       bool qualified_p;
13656       bool global_p;
13657
13658       /* Don't gobble tokens or issue error messages if this is an
13659          optional type-specifier.  */
13660       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13661         cp_parser_parse_tentatively (parser);
13662
13663       /* Look for the optional `::' operator.  */
13664       global_p
13665         = (cp_parser_global_scope_opt (parser,
13666                                        /*current_scope_valid_p=*/false)
13667            != NULL_TREE);
13668       /* Look for the nested-name specifier.  */
13669       qualified_p
13670         = (cp_parser_nested_name_specifier_opt (parser,
13671                                                 /*typename_keyword_p=*/false,
13672                                                 /*check_dependency_p=*/true,
13673                                                 /*type_p=*/false,
13674                                                 /*is_declaration=*/false)
13675            != NULL_TREE);
13676       token = cp_lexer_peek_token (parser->lexer);
13677       /* If we have seen a nested-name-specifier, and the next token
13678          is `template', then we are using the template-id production.  */
13679       if (parser->scope
13680           && cp_parser_optional_template_keyword (parser))
13681         {
13682           /* Look for the template-id.  */
13683           type = cp_parser_template_id (parser,
13684                                         /*template_keyword_p=*/true,
13685                                         /*check_dependency_p=*/true,
13686                                         /*is_declaration=*/false);
13687           /* If the template-id did not name a type, we are out of
13688              luck.  */
13689           if (TREE_CODE (type) != TYPE_DECL)
13690             {
13691               cp_parser_error (parser, "expected template-id for type");
13692               type = NULL_TREE;
13693             }
13694         }
13695       /* Otherwise, look for a type-name.  */
13696       else
13697         type = cp_parser_type_name (parser);
13698       /* Keep track of all name-lookups performed in class scopes.  */
13699       if (type
13700           && !global_p
13701           && !qualified_p
13702           && TREE_CODE (type) == TYPE_DECL
13703           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13704         maybe_note_name_used_in_class (DECL_NAME (type), type);
13705       /* If it didn't work out, we don't have a TYPE.  */
13706       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13707           && !cp_parser_parse_definitely (parser))
13708         type = NULL_TREE;
13709       if (type && decl_specs)
13710         cp_parser_set_decl_spec_type (decl_specs, type,
13711                                       token->location,
13712                                       /*type_definition_p=*/false);
13713     }
13714
13715   /* If we didn't get a type-name, issue an error message.  */
13716   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13717     {
13718       cp_parser_error (parser, "expected type-name");
13719       return error_mark_node;
13720     }
13721
13722   if (type && type != error_mark_node)
13723     {
13724       /* See if TYPE is an Objective-C type, and if so, parse and
13725          accept any protocol references following it.  Do this before
13726          the cp_parser_check_for_invalid_template_id() call, because
13727          Objective-C types can be followed by '<...>' which would
13728          enclose protocol names rather than template arguments, and so
13729          everything is fine.  */
13730       if (c_dialect_objc () && !parser->scope
13731           && (objc_is_id (type) || objc_is_class_name (type)))
13732         {
13733           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13734           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13735
13736           /* Clobber the "unqualified" type previously entered into
13737              DECL_SPECS with the new, improved protocol-qualified version.  */
13738           if (decl_specs)
13739             decl_specs->type = qual_type;
13740
13741           return qual_type;
13742         }
13743
13744       /* There is no valid C++ program where a non-template type is
13745          followed by a "<".  That usually indicates that the user
13746          thought that the type was a template.  */
13747       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13748                                                token->location);
13749     }
13750
13751   return type;
13752 }
13753
13754 /* Parse a type-name.
13755
13756    type-name:
13757      class-name
13758      enum-name
13759      typedef-name
13760      simple-template-id [in c++0x]
13761
13762    enum-name:
13763      identifier
13764
13765    typedef-name:
13766      identifier
13767
13768    Returns a TYPE_DECL for the type.  */
13769
13770 static tree
13771 cp_parser_type_name (cp_parser* parser)
13772 {
13773   tree type_decl;
13774
13775   /* We can't know yet whether it is a class-name or not.  */
13776   cp_parser_parse_tentatively (parser);
13777   /* Try a class-name.  */
13778   type_decl = cp_parser_class_name (parser,
13779                                     /*typename_keyword_p=*/false,
13780                                     /*template_keyword_p=*/false,
13781                                     none_type,
13782                                     /*check_dependency_p=*/true,
13783                                     /*class_head_p=*/false,
13784                                     /*is_declaration=*/false);
13785   /* If it's not a class-name, keep looking.  */
13786   if (!cp_parser_parse_definitely (parser))
13787     {
13788       if (cxx_dialect < cxx0x)
13789         /* It must be a typedef-name or an enum-name.  */
13790         return cp_parser_nonclass_name (parser);
13791
13792       cp_parser_parse_tentatively (parser);
13793       /* It is either a simple-template-id representing an
13794          instantiation of an alias template...  */
13795       type_decl = cp_parser_template_id (parser,
13796                                          /*template_keyword_p=*/false,
13797                                          /*check_dependency_p=*/false,
13798                                          /*is_declaration=*/false);
13799       /* Note that this must be an instantiation of an alias template
13800          because [temp.names]/6 says:
13801          
13802              A template-id that names an alias template specialization
13803              is a type-name.
13804
13805          Whereas [temp.names]/7 says:
13806          
13807              A simple-template-id that names a class template
13808              specialization is a class-name.  */
13809       if (type_decl != NULL_TREE
13810           && TREE_CODE (type_decl) == TYPE_DECL
13811           && TYPE_DECL_ALIAS_P (type_decl))
13812         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13813       else
13814         cp_parser_simulate_error (parser);
13815
13816       if (!cp_parser_parse_definitely (parser))
13817         /* ... Or a typedef-name or an enum-name.  */
13818         return cp_parser_nonclass_name (parser);
13819     }
13820
13821   return type_decl;
13822 }
13823
13824 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13825
13826    enum-name:
13827      identifier
13828
13829    typedef-name:
13830      identifier
13831
13832    Returns a TYPE_DECL for the type.  */
13833
13834 static tree
13835 cp_parser_nonclass_name (cp_parser* parser)
13836 {
13837   tree type_decl;
13838   tree identifier;
13839
13840   cp_token *token = cp_lexer_peek_token (parser->lexer);
13841   identifier = cp_parser_identifier (parser);
13842   if (identifier == error_mark_node)
13843     return error_mark_node;
13844
13845   /* Look up the type-name.  */
13846   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13847
13848   if (TREE_CODE (type_decl) == USING_DECL)
13849     {
13850       if (!DECL_DEPENDENT_P (type_decl))
13851         type_decl = strip_using_decl (type_decl);
13852       else if (USING_DECL_TYPENAME_P (type_decl))
13853         {
13854           /* We have found a type introduced by a using
13855              declaration at class scope that refers to a dependent
13856              type.
13857              
13858              using typename :: [opt] nested-name-specifier unqualified-id ;
13859           */
13860           type_decl = make_typename_type (TREE_TYPE (type_decl),
13861                                           DECL_NAME (type_decl),
13862                                           typename_type, tf_error);
13863           if (type_decl != error_mark_node)
13864             type_decl = TYPE_NAME (type_decl);
13865         }
13866     }
13867   
13868   if (TREE_CODE (type_decl) != TYPE_DECL
13869       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13870     {
13871       /* See if this is an Objective-C type.  */
13872       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13873       tree type = objc_get_protocol_qualified_type (identifier, protos);
13874       if (type)
13875         type_decl = TYPE_NAME (type);
13876     }
13877
13878   /* Issue an error if we did not find a type-name.  */
13879   if (TREE_CODE (type_decl) != TYPE_DECL
13880       /* In Objective-C, we have the complication that class names are
13881          normally type names and start declarations (eg, the
13882          "NSObject" in "NSObject *object;"), but can be used in an
13883          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13884          is an expression.  So, a classname followed by a dot is not a
13885          valid type-name.  */
13886       || (objc_is_class_name (TREE_TYPE (type_decl))
13887           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13888     {
13889       if (!cp_parser_simulate_error (parser))
13890         cp_parser_name_lookup_error (parser, identifier, type_decl,
13891                                      NLE_TYPE, token->location);
13892       return error_mark_node;
13893     }
13894   /* Remember that the name was used in the definition of the
13895      current class so that we can check later to see if the
13896      meaning would have been different after the class was
13897      entirely defined.  */
13898   else if (type_decl != error_mark_node
13899            && !parser->scope)
13900     maybe_note_name_used_in_class (identifier, type_decl);
13901   
13902   return type_decl;
13903 }
13904
13905 /* Parse an elaborated-type-specifier.  Note that the grammar given
13906    here incorporates the resolution to DR68.
13907
13908    elaborated-type-specifier:
13909      class-key :: [opt] nested-name-specifier [opt] identifier
13910      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13911      enum-key :: [opt] nested-name-specifier [opt] identifier
13912      typename :: [opt] nested-name-specifier identifier
13913      typename :: [opt] nested-name-specifier template [opt]
13914        template-id
13915
13916    GNU extension:
13917
13918    elaborated-type-specifier:
13919      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13920      class-key attributes :: [opt] nested-name-specifier [opt]
13921                template [opt] template-id
13922      enum attributes :: [opt] nested-name-specifier [opt] identifier
13923
13924    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13925    declared `friend'.  If IS_DECLARATION is TRUE, then this
13926    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13927    something is being declared.
13928
13929    Returns the TYPE specified.  */
13930
13931 static tree
13932 cp_parser_elaborated_type_specifier (cp_parser* parser,
13933                                      bool is_friend,
13934                                      bool is_declaration)
13935 {
13936   enum tag_types tag_type;
13937   tree identifier;
13938   tree type = NULL_TREE;
13939   tree attributes = NULL_TREE;
13940   tree globalscope;
13941   cp_token *token = NULL;
13942
13943   /* See if we're looking at the `enum' keyword.  */
13944   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13945     {
13946       /* Consume the `enum' token.  */
13947       cp_lexer_consume_token (parser->lexer);
13948       /* Remember that it's an enumeration type.  */
13949       tag_type = enum_type;
13950       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13951          enums) is used here.  */
13952       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13953           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13954         {
13955             pedwarn (input_location, 0, "elaborated-type-specifier "
13956                       "for a scoped enum must not use the %<%D%> keyword",
13957                       cp_lexer_peek_token (parser->lexer)->u.value);
13958           /* Consume the `struct' or `class' and parse it anyway.  */
13959           cp_lexer_consume_token (parser->lexer);
13960         }
13961       /* Parse the attributes.  */
13962       attributes = cp_parser_attributes_opt (parser);
13963     }
13964   /* Or, it might be `typename'.  */
13965   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13966                                            RID_TYPENAME))
13967     {
13968       /* Consume the `typename' token.  */
13969       cp_lexer_consume_token (parser->lexer);
13970       /* Remember that it's a `typename' type.  */
13971       tag_type = typename_type;
13972     }
13973   /* Otherwise it must be a class-key.  */
13974   else
13975     {
13976       tag_type = cp_parser_class_key (parser);
13977       if (tag_type == none_type)
13978         return error_mark_node;
13979       /* Parse the attributes.  */
13980       attributes = cp_parser_attributes_opt (parser);
13981     }
13982
13983   /* Look for the `::' operator.  */
13984   globalscope =  cp_parser_global_scope_opt (parser,
13985                                              /*current_scope_valid_p=*/false);
13986   /* Look for the nested-name-specifier.  */
13987   if (tag_type == typename_type && !globalscope)
13988     {
13989       if (!cp_parser_nested_name_specifier (parser,
13990                                            /*typename_keyword_p=*/true,
13991                                            /*check_dependency_p=*/true,
13992                                            /*type_p=*/true,
13993                                             is_declaration))
13994         return error_mark_node;
13995     }
13996   else
13997     /* Even though `typename' is not present, the proposed resolution
13998        to Core Issue 180 says that in `class A<T>::B', `B' should be
13999        considered a type-name, even if `A<T>' is dependent.  */
14000     cp_parser_nested_name_specifier_opt (parser,
14001                                          /*typename_keyword_p=*/true,
14002                                          /*check_dependency_p=*/true,
14003                                          /*type_p=*/true,
14004                                          is_declaration);
14005  /* For everything but enumeration types, consider a template-id.
14006     For an enumeration type, consider only a plain identifier.  */
14007   if (tag_type != enum_type)
14008     {
14009       bool template_p = false;
14010       tree decl;
14011
14012       /* Allow the `template' keyword.  */
14013       template_p = cp_parser_optional_template_keyword (parser);
14014       /* If we didn't see `template', we don't know if there's a
14015          template-id or not.  */
14016       if (!template_p)
14017         cp_parser_parse_tentatively (parser);
14018       /* Parse the template-id.  */
14019       token = cp_lexer_peek_token (parser->lexer);
14020       decl = cp_parser_template_id (parser, template_p,
14021                                     /*check_dependency_p=*/true,
14022                                     is_declaration);
14023       /* If we didn't find a template-id, look for an ordinary
14024          identifier.  */
14025       if (!template_p && !cp_parser_parse_definitely (parser))
14026         ;
14027       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14028          in effect, then we must assume that, upon instantiation, the
14029          template will correspond to a class.  */
14030       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14031                && tag_type == typename_type)
14032         type = make_typename_type (parser->scope, decl,
14033                                    typename_type,
14034                                    /*complain=*/tf_error);
14035       /* If the `typename' keyword is in effect and DECL is not a type
14036          decl. Then type is non existant.   */
14037       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14038         type = NULL_TREE; 
14039       else 
14040         type = check_elaborated_type_specifier (tag_type, decl,
14041                                                 /*allow_template_p=*/true);
14042     }
14043
14044   if (!type)
14045     {
14046       token = cp_lexer_peek_token (parser->lexer);
14047       identifier = cp_parser_identifier (parser);
14048
14049       if (identifier == error_mark_node)
14050         {
14051           parser->scope = NULL_TREE;
14052           return error_mark_node;
14053         }
14054
14055       /* For a `typename', we needn't call xref_tag.  */
14056       if (tag_type == typename_type
14057           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14058         return cp_parser_make_typename_type (parser, parser->scope,
14059                                              identifier,
14060                                              token->location);
14061       /* Look up a qualified name in the usual way.  */
14062       if (parser->scope)
14063         {
14064           tree decl;
14065           tree ambiguous_decls;
14066
14067           decl = cp_parser_lookup_name (parser, identifier,
14068                                         tag_type,
14069                                         /*is_template=*/false,
14070                                         /*is_namespace=*/false,
14071                                         /*check_dependency=*/true,
14072                                         &ambiguous_decls,
14073                                         token->location);
14074
14075           /* If the lookup was ambiguous, an error will already have been
14076              issued.  */
14077           if (ambiguous_decls)
14078             return error_mark_node;
14079
14080           /* If we are parsing friend declaration, DECL may be a
14081              TEMPLATE_DECL tree node here.  However, we need to check
14082              whether this TEMPLATE_DECL results in valid code.  Consider
14083              the following example:
14084
14085                namespace N {
14086                  template <class T> class C {};
14087                }
14088                class X {
14089                  template <class T> friend class N::C; // #1, valid code
14090                };
14091                template <class T> class Y {
14092                  friend class N::C;                    // #2, invalid code
14093                };
14094
14095              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14096              name lookup of `N::C'.  We see that friend declaration must
14097              be template for the code to be valid.  Note that
14098              processing_template_decl does not work here since it is
14099              always 1 for the above two cases.  */
14100
14101           decl = (cp_parser_maybe_treat_template_as_class
14102                   (decl, /*tag_name_p=*/is_friend
14103                          && parser->num_template_parameter_lists));
14104
14105           if (TREE_CODE (decl) != TYPE_DECL)
14106             {
14107               cp_parser_diagnose_invalid_type_name (parser,
14108                                                     parser->scope,
14109                                                     identifier,
14110                                                     token->location);
14111               return error_mark_node;
14112             }
14113
14114           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14115             {
14116               bool allow_template = (parser->num_template_parameter_lists
14117                                       || DECL_SELF_REFERENCE_P (decl));
14118               type = check_elaborated_type_specifier (tag_type, decl, 
14119                                                       allow_template);
14120
14121               if (type == error_mark_node)
14122                 return error_mark_node;
14123             }
14124
14125           /* Forward declarations of nested types, such as
14126
14127                class C1::C2;
14128                class C1::C2::C3;
14129
14130              are invalid unless all components preceding the final '::'
14131              are complete.  If all enclosing types are complete, these
14132              declarations become merely pointless.
14133
14134              Invalid forward declarations of nested types are errors
14135              caught elsewhere in parsing.  Those that are pointless arrive
14136              here.  */
14137
14138           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14139               && !is_friend && !processing_explicit_instantiation)
14140             warning (0, "declaration %qD does not declare anything", decl);
14141
14142           type = TREE_TYPE (decl);
14143         }
14144       else
14145         {
14146           /* An elaborated-type-specifier sometimes introduces a new type and
14147              sometimes names an existing type.  Normally, the rule is that it
14148              introduces a new type only if there is not an existing type of
14149              the same name already in scope.  For example, given:
14150
14151                struct S {};
14152                void f() { struct S s; }
14153
14154              the `struct S' in the body of `f' is the same `struct S' as in
14155              the global scope; the existing definition is used.  However, if
14156              there were no global declaration, this would introduce a new
14157              local class named `S'.
14158
14159              An exception to this rule applies to the following code:
14160
14161                namespace N { struct S; }
14162
14163              Here, the elaborated-type-specifier names a new type
14164              unconditionally; even if there is already an `S' in the
14165              containing scope this declaration names a new type.
14166              This exception only applies if the elaborated-type-specifier
14167              forms the complete declaration:
14168
14169                [class.name]
14170
14171                A declaration consisting solely of `class-key identifier ;' is
14172                either a redeclaration of the name in the current scope or a
14173                forward declaration of the identifier as a class name.  It
14174                introduces the name into the current scope.
14175
14176              We are in this situation precisely when the next token is a `;'.
14177
14178              An exception to the exception is that a `friend' declaration does
14179              *not* name a new type; i.e., given:
14180
14181                struct S { friend struct T; };
14182
14183              `T' is not a new type in the scope of `S'.
14184
14185              Also, `new struct S' or `sizeof (struct S)' never results in the
14186              definition of a new type; a new type can only be declared in a
14187              declaration context.  */
14188
14189           tag_scope ts;
14190           bool template_p;
14191
14192           if (is_friend)
14193             /* Friends have special name lookup rules.  */
14194             ts = ts_within_enclosing_non_class;
14195           else if (is_declaration
14196                    && cp_lexer_next_token_is (parser->lexer,
14197                                               CPP_SEMICOLON))
14198             /* This is a `class-key identifier ;' */
14199             ts = ts_current;
14200           else
14201             ts = ts_global;
14202
14203           template_p =
14204             (parser->num_template_parameter_lists
14205              && (cp_parser_next_token_starts_class_definition_p (parser)
14206                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14207           /* An unqualified name was used to reference this type, so
14208              there were no qualifying templates.  */
14209           if (!cp_parser_check_template_parameters (parser,
14210                                                     /*num_templates=*/0,
14211                                                     token->location,
14212                                                     /*declarator=*/NULL))
14213             return error_mark_node;
14214           type = xref_tag (tag_type, identifier, ts, template_p);
14215         }
14216     }
14217
14218   if (type == error_mark_node)
14219     return error_mark_node;
14220
14221   /* Allow attributes on forward declarations of classes.  */
14222   if (attributes)
14223     {
14224       if (TREE_CODE (type) == TYPENAME_TYPE)
14225         warning (OPT_Wattributes,
14226                  "attributes ignored on uninstantiated type");
14227       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14228                && ! processing_explicit_instantiation)
14229         warning (OPT_Wattributes,
14230                  "attributes ignored on template instantiation");
14231       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14232         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14233       else
14234         warning (OPT_Wattributes,
14235                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14236     }
14237
14238   if (tag_type != enum_type)
14239     {
14240       /* Indicate whether this class was declared as a `class' or as a
14241          `struct'.  */
14242       if (TREE_CODE (type) == RECORD_TYPE)
14243         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14244       cp_parser_check_class_key (tag_type, type);
14245     }
14246
14247   /* A "<" cannot follow an elaborated type specifier.  If that
14248      happens, the user was probably trying to form a template-id.  */
14249   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14250
14251   return type;
14252 }
14253
14254 /* Parse an enum-specifier.
14255
14256    enum-specifier:
14257      enum-head { enumerator-list [opt] }
14258      enum-head { enumerator-list , } [C++0x]
14259
14260    enum-head:
14261      enum-key identifier [opt] enum-base [opt]
14262      enum-key nested-name-specifier identifier enum-base [opt]
14263
14264    enum-key:
14265      enum
14266      enum class   [C++0x]
14267      enum struct  [C++0x]
14268
14269    enum-base:   [C++0x]
14270      : type-specifier-seq
14271
14272    opaque-enum-specifier:
14273      enum-key identifier enum-base [opt] ;
14274
14275    GNU Extensions:
14276      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14277        { enumerator-list [opt] }attributes[opt]
14278      enum-key attributes[opt] identifier [opt] enum-base [opt]
14279        { enumerator-list, }attributes[opt] [C++0x]
14280
14281    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14282    if the token stream isn't an enum-specifier after all.  */
14283
14284 static tree
14285 cp_parser_enum_specifier (cp_parser* parser)
14286 {
14287   tree identifier;
14288   tree type = NULL_TREE;
14289   tree prev_scope;
14290   tree nested_name_specifier = NULL_TREE;
14291   tree attributes;
14292   bool scoped_enum_p = false;
14293   bool has_underlying_type = false;
14294   bool nested_being_defined = false;
14295   bool new_value_list = false;
14296   bool is_new_type = false;
14297   bool is_anonymous = false;
14298   tree underlying_type = NULL_TREE;
14299   cp_token *type_start_token = NULL;
14300   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14301
14302   parser->colon_corrects_to_scope_p = false;
14303
14304   /* Parse tentatively so that we can back up if we don't find a
14305      enum-specifier.  */
14306   cp_parser_parse_tentatively (parser);
14307
14308   /* Caller guarantees that the current token is 'enum', an identifier
14309      possibly follows, and the token after that is an opening brace.
14310      If we don't have an identifier, fabricate an anonymous name for
14311      the enumeration being defined.  */
14312   cp_lexer_consume_token (parser->lexer);
14313
14314   /* Parse the "class" or "struct", which indicates a scoped
14315      enumeration type in C++0x.  */
14316   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14317       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14318     {
14319       if (cxx_dialect < cxx0x)
14320         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14321
14322       /* Consume the `struct' or `class' token.  */
14323       cp_lexer_consume_token (parser->lexer);
14324
14325       scoped_enum_p = true;
14326     }
14327
14328   attributes = cp_parser_attributes_opt (parser);
14329
14330   /* Clear the qualification.  */
14331   parser->scope = NULL_TREE;
14332   parser->qualifying_scope = NULL_TREE;
14333   parser->object_scope = NULL_TREE;
14334
14335   /* Figure out in what scope the declaration is being placed.  */
14336   prev_scope = current_scope ();
14337
14338   type_start_token = cp_lexer_peek_token (parser->lexer);
14339
14340   push_deferring_access_checks (dk_no_check);
14341   nested_name_specifier
14342       = cp_parser_nested_name_specifier_opt (parser,
14343                                              /*typename_keyword_p=*/true,
14344                                              /*check_dependency_p=*/false,
14345                                              /*type_p=*/false,
14346                                              /*is_declaration=*/false);
14347
14348   if (nested_name_specifier)
14349     {
14350       tree name;
14351
14352       identifier = cp_parser_identifier (parser);
14353       name =  cp_parser_lookup_name (parser, identifier,
14354                                      enum_type,
14355                                      /*is_template=*/false,
14356                                      /*is_namespace=*/false,
14357                                      /*check_dependency=*/true,
14358                                      /*ambiguous_decls=*/NULL,
14359                                      input_location);
14360       if (name)
14361         {
14362           type = TREE_TYPE (name);
14363           if (TREE_CODE (type) == TYPENAME_TYPE)
14364             {
14365               /* Are template enums allowed in ISO? */
14366               if (template_parm_scope_p ())
14367                 pedwarn (type_start_token->location, OPT_Wpedantic,
14368                          "%qD is an enumeration template", name);
14369               /* ignore a typename reference, for it will be solved by name
14370                  in start_enum.  */
14371               type = NULL_TREE;
14372             }
14373         }
14374       else
14375         error_at (type_start_token->location,
14376                   "%qD is not an enumerator-name", identifier);
14377     }
14378   else
14379     {
14380       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14381         identifier = cp_parser_identifier (parser);
14382       else
14383         {
14384           identifier = make_anon_name ();
14385           is_anonymous = true;
14386         }
14387     }
14388   pop_deferring_access_checks ();
14389
14390   /* Check for the `:' that denotes a specified underlying type in C++0x.
14391      Note that a ':' could also indicate a bitfield width, however.  */
14392   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14393     {
14394       cp_decl_specifier_seq type_specifiers;
14395
14396       /* Consume the `:'.  */
14397       cp_lexer_consume_token (parser->lexer);
14398
14399       /* Parse the type-specifier-seq.  */
14400       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14401                                     /*is_trailing_return=*/false,
14402                                     &type_specifiers);
14403
14404       /* At this point this is surely not elaborated type specifier.  */
14405       if (!cp_parser_parse_definitely (parser))
14406         return NULL_TREE;
14407
14408       if (cxx_dialect < cxx0x)
14409         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14410
14411       has_underlying_type = true;
14412
14413       /* If that didn't work, stop.  */
14414       if (type_specifiers.type != error_mark_node)
14415         {
14416           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14417                                             /*initialized=*/0, NULL);
14418           if (underlying_type == error_mark_node)
14419             underlying_type = NULL_TREE;
14420         }
14421     }
14422
14423   /* Look for the `{' but don't consume it yet.  */
14424   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14425     {
14426       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14427         {
14428           cp_parser_error (parser, "expected %<{%>");
14429           if (has_underlying_type)
14430             {
14431               type = NULL_TREE;
14432               goto out;
14433             }
14434         }
14435       /* An opaque-enum-specifier must have a ';' here.  */
14436       if ((scoped_enum_p || underlying_type)
14437           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14438         {
14439           cp_parser_error (parser, "expected %<;%> or %<{%>");
14440           if (has_underlying_type)
14441             {
14442               type = NULL_TREE;
14443               goto out;
14444             }
14445         }
14446     }
14447
14448   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14449     return NULL_TREE;
14450
14451   if (nested_name_specifier)
14452     {
14453       if (CLASS_TYPE_P (nested_name_specifier))
14454         {
14455           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14456           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14457           push_scope (nested_name_specifier);
14458         }
14459       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14460         {
14461           push_nested_namespace (nested_name_specifier);
14462         }
14463     }
14464
14465   /* Issue an error message if type-definitions are forbidden here.  */
14466   if (!cp_parser_check_type_definition (parser))
14467     type = error_mark_node;
14468   else
14469     /* Create the new type.  We do this before consuming the opening
14470        brace so the enum will be recorded as being on the line of its
14471        tag (or the 'enum' keyword, if there is no tag).  */
14472     type = start_enum (identifier, type, underlying_type,
14473                        scoped_enum_p, &is_new_type);
14474
14475   /* If the next token is not '{' it is an opaque-enum-specifier or an
14476      elaborated-type-specifier.  */
14477   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14478     {
14479       timevar_push (TV_PARSE_ENUM);
14480       if (nested_name_specifier)
14481         {
14482           /* The following catches invalid code such as:
14483              enum class S<int>::E { A, B, C }; */
14484           if (!processing_specialization
14485               && CLASS_TYPE_P (nested_name_specifier)
14486               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14487             error_at (type_start_token->location, "cannot add an enumerator "
14488                       "list to a template instantiation");
14489
14490           /* If that scope does not contain the scope in which the
14491              class was originally declared, the program is invalid.  */
14492           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14493             {
14494               if (at_namespace_scope_p ())
14495                 error_at (type_start_token->location,
14496                           "declaration of %qD in namespace %qD which does not "
14497                           "enclose %qD",
14498                           type, prev_scope, nested_name_specifier);
14499               else
14500                 error_at (type_start_token->location,
14501                           "declaration of %qD in %qD which does not enclose %qD",
14502                           type, prev_scope, nested_name_specifier);
14503               type = error_mark_node;
14504             }
14505         }
14506
14507       if (scoped_enum_p)
14508         begin_scope (sk_scoped_enum, type);
14509
14510       /* Consume the opening brace.  */
14511       cp_lexer_consume_token (parser->lexer);
14512
14513       if (type == error_mark_node)
14514         ; /* Nothing to add */
14515       else if (OPAQUE_ENUM_P (type)
14516                || (cxx_dialect > cxx98 && processing_specialization))
14517         {
14518           new_value_list = true;
14519           SET_OPAQUE_ENUM_P (type, false);
14520           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14521         }
14522       else
14523         {
14524           error_at (type_start_token->location, "multiple definition of %q#T", type);
14525           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14526                     "previous definition here");
14527           type = error_mark_node;
14528         }
14529
14530       if (type == error_mark_node)
14531         cp_parser_skip_to_end_of_block_or_statement (parser);
14532       /* If the next token is not '}', then there are some enumerators.  */
14533       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14534         cp_parser_enumerator_list (parser, type);
14535
14536       /* Consume the final '}'.  */
14537       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14538
14539       if (scoped_enum_p)
14540         finish_scope ();
14541       timevar_pop (TV_PARSE_ENUM);
14542     }
14543   else
14544     {
14545       /* If a ';' follows, then it is an opaque-enum-specifier
14546         and additional restrictions apply.  */
14547       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14548         {
14549           if (is_anonymous)
14550             error_at (type_start_token->location,
14551                       "opaque-enum-specifier without name");
14552           else if (nested_name_specifier)
14553             error_at (type_start_token->location,
14554                       "opaque-enum-specifier must use a simple identifier");
14555         }
14556     }
14557
14558   /* Look for trailing attributes to apply to this enumeration, and
14559      apply them if appropriate.  */
14560   if (cp_parser_allow_gnu_extensions_p (parser))
14561     {
14562       tree trailing_attr = cp_parser_attributes_opt (parser);
14563       trailing_attr = chainon (trailing_attr, attributes);
14564       cplus_decl_attributes (&type,
14565                              trailing_attr,
14566                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14567     }
14568
14569   /* Finish up the enumeration.  */
14570   if (type != error_mark_node)
14571     {
14572       if (new_value_list)
14573         finish_enum_value_list (type);
14574       if (is_new_type)
14575         finish_enum (type);
14576     }
14577
14578   if (nested_name_specifier)
14579     {
14580       if (CLASS_TYPE_P (nested_name_specifier))
14581         {
14582           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14583           pop_scope (nested_name_specifier);
14584         }
14585       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14586         {
14587           pop_nested_namespace (nested_name_specifier);
14588         }
14589     }
14590  out:
14591   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14592   return type;
14593 }
14594
14595 /* Parse an enumerator-list.  The enumerators all have the indicated
14596    TYPE.
14597
14598    enumerator-list:
14599      enumerator-definition
14600      enumerator-list , enumerator-definition  */
14601
14602 static void
14603 cp_parser_enumerator_list (cp_parser* parser, tree type)
14604 {
14605   while (true)
14606     {
14607       /* Parse an enumerator-definition.  */
14608       cp_parser_enumerator_definition (parser, type);
14609
14610       /* If the next token is not a ',', we've reached the end of
14611          the list.  */
14612       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14613         break;
14614       /* Otherwise, consume the `,' and keep going.  */
14615       cp_lexer_consume_token (parser->lexer);
14616       /* If the next token is a `}', there is a trailing comma.  */
14617       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14618         {
14619           if (cxx_dialect < cxx0x && !in_system_header)
14620             pedwarn (input_location, OPT_Wpedantic,
14621                      "comma at end of enumerator list");
14622           break;
14623         }
14624     }
14625 }
14626
14627 /* Parse an enumerator-definition.  The enumerator has the indicated
14628    TYPE.
14629
14630    enumerator-definition:
14631      enumerator
14632      enumerator = constant-expression
14633
14634    enumerator:
14635      identifier  */
14636
14637 static void
14638 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14639 {
14640   tree identifier;
14641   tree value;
14642   location_t loc;
14643
14644   /* Save the input location because we are interested in the location
14645      of the identifier and not the location of the explicit value.  */
14646   loc = cp_lexer_peek_token (parser->lexer)->location;
14647
14648   /* Look for the identifier.  */
14649   identifier = cp_parser_identifier (parser);
14650   if (identifier == error_mark_node)
14651     return;
14652
14653   /* If the next token is an '=', then there is an explicit value.  */
14654   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14655     {
14656       /* Consume the `=' token.  */
14657       cp_lexer_consume_token (parser->lexer);
14658       /* Parse the value.  */
14659       value = cp_parser_constant_expression (parser,
14660                                              /*allow_non_constant_p=*/false,
14661                                              NULL);
14662     }
14663   else
14664     value = NULL_TREE;
14665
14666   /* If we are processing a template, make sure the initializer of the
14667      enumerator doesn't contain any bare template parameter pack.  */
14668   if (check_for_bare_parameter_packs (value))
14669     value = error_mark_node;
14670
14671   /* integral_constant_value will pull out this expression, so make sure
14672      it's folded as appropriate.  */
14673   value = fold_non_dependent_expr (value);
14674
14675   /* Create the enumerator.  */
14676   build_enumerator (identifier, value, type, loc);
14677 }
14678
14679 /* Parse a namespace-name.
14680
14681    namespace-name:
14682      original-namespace-name
14683      namespace-alias
14684
14685    Returns the NAMESPACE_DECL for the namespace.  */
14686
14687 static tree
14688 cp_parser_namespace_name (cp_parser* parser)
14689 {
14690   tree identifier;
14691   tree namespace_decl;
14692
14693   cp_token *token = cp_lexer_peek_token (parser->lexer);
14694
14695   /* Get the name of the namespace.  */
14696   identifier = cp_parser_identifier (parser);
14697   if (identifier == error_mark_node)
14698     return error_mark_node;
14699
14700   /* Look up the identifier in the currently active scope.  Look only
14701      for namespaces, due to:
14702
14703        [basic.lookup.udir]
14704
14705        When looking up a namespace-name in a using-directive or alias
14706        definition, only namespace names are considered.
14707
14708      And:
14709
14710        [basic.lookup.qual]
14711
14712        During the lookup of a name preceding the :: scope resolution
14713        operator, object, function, and enumerator names are ignored.
14714
14715      (Note that cp_parser_qualifying_entity only calls this
14716      function if the token after the name is the scope resolution
14717      operator.)  */
14718   namespace_decl = cp_parser_lookup_name (parser, identifier,
14719                                           none_type,
14720                                           /*is_template=*/false,
14721                                           /*is_namespace=*/true,
14722                                           /*check_dependency=*/true,
14723                                           /*ambiguous_decls=*/NULL,
14724                                           token->location);
14725   /* If it's not a namespace, issue an error.  */
14726   if (namespace_decl == error_mark_node
14727       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14728     {
14729       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14730         error_at (token->location, "%qD is not a namespace-name", identifier);
14731       cp_parser_error (parser, "expected namespace-name");
14732       namespace_decl = error_mark_node;
14733     }
14734
14735   return namespace_decl;
14736 }
14737
14738 /* Parse a namespace-definition.
14739
14740    namespace-definition:
14741      named-namespace-definition
14742      unnamed-namespace-definition
14743
14744    named-namespace-definition:
14745      original-namespace-definition
14746      extension-namespace-definition
14747
14748    original-namespace-definition:
14749      namespace identifier { namespace-body }
14750
14751    extension-namespace-definition:
14752      namespace original-namespace-name { namespace-body }
14753
14754    unnamed-namespace-definition:
14755      namespace { namespace-body } */
14756
14757 static void
14758 cp_parser_namespace_definition (cp_parser* parser)
14759 {
14760   tree identifier, attribs;
14761   bool has_visibility;
14762   bool is_inline;
14763
14764   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14765     {
14766       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14767       is_inline = true;
14768       cp_lexer_consume_token (parser->lexer);
14769     }
14770   else
14771     is_inline = false;
14772
14773   /* Look for the `namespace' keyword.  */
14774   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14775
14776   /* Get the name of the namespace.  We do not attempt to distinguish
14777      between an original-namespace-definition and an
14778      extension-namespace-definition at this point.  The semantic
14779      analysis routines are responsible for that.  */
14780   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14781     identifier = cp_parser_identifier (parser);
14782   else
14783     identifier = NULL_TREE;
14784
14785   /* Parse any specified attributes.  */
14786   attribs = cp_parser_attributes_opt (parser);
14787
14788   /* Look for the `{' to start the namespace.  */
14789   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14790   /* Start the namespace.  */
14791   push_namespace (identifier);
14792
14793   /* "inline namespace" is equivalent to a stub namespace definition
14794      followed by a strong using directive.  */
14795   if (is_inline)
14796     {
14797       tree name_space = current_namespace;
14798       /* Set up namespace association.  */
14799       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14800         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14801                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14802       /* Import the contents of the inline namespace.  */
14803       pop_namespace ();
14804       do_using_directive (name_space);
14805       push_namespace (identifier);
14806     }
14807
14808   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14809
14810   /* Parse the body of the namespace.  */
14811   cp_parser_namespace_body (parser);
14812
14813   if (has_visibility)
14814     pop_visibility (1);
14815
14816   /* Finish the namespace.  */
14817   pop_namespace ();
14818   /* Look for the final `}'.  */
14819   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14820 }
14821
14822 /* Parse a namespace-body.
14823
14824    namespace-body:
14825      declaration-seq [opt]  */
14826
14827 static void
14828 cp_parser_namespace_body (cp_parser* parser)
14829 {
14830   cp_parser_declaration_seq_opt (parser);
14831 }
14832
14833 /* Parse a namespace-alias-definition.
14834
14835    namespace-alias-definition:
14836      namespace identifier = qualified-namespace-specifier ;  */
14837
14838 static void
14839 cp_parser_namespace_alias_definition (cp_parser* parser)
14840 {
14841   tree identifier;
14842   tree namespace_specifier;
14843
14844   cp_token *token = cp_lexer_peek_token (parser->lexer);
14845
14846   /* Look for the `namespace' keyword.  */
14847   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14848   /* Look for the identifier.  */
14849   identifier = cp_parser_identifier (parser);
14850   if (identifier == error_mark_node)
14851     return;
14852   /* Look for the `=' token.  */
14853   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14854       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14855     {
14856       error_at (token->location, "%<namespace%> definition is not allowed here");
14857       /* Skip the definition.  */
14858       cp_lexer_consume_token (parser->lexer);
14859       if (cp_parser_skip_to_closing_brace (parser))
14860         cp_lexer_consume_token (parser->lexer);
14861       return;
14862     }
14863   cp_parser_require (parser, CPP_EQ, RT_EQ);
14864   /* Look for the qualified-namespace-specifier.  */
14865   namespace_specifier
14866     = cp_parser_qualified_namespace_specifier (parser);
14867   /* Look for the `;' token.  */
14868   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14869
14870   /* Register the alias in the symbol table.  */
14871   do_namespace_alias (identifier, namespace_specifier);
14872 }
14873
14874 /* Parse a qualified-namespace-specifier.
14875
14876    qualified-namespace-specifier:
14877      :: [opt] nested-name-specifier [opt] namespace-name
14878
14879    Returns a NAMESPACE_DECL corresponding to the specified
14880    namespace.  */
14881
14882 static tree
14883 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14884 {
14885   /* Look for the optional `::'.  */
14886   cp_parser_global_scope_opt (parser,
14887                               /*current_scope_valid_p=*/false);
14888
14889   /* Look for the optional nested-name-specifier.  */
14890   cp_parser_nested_name_specifier_opt (parser,
14891                                        /*typename_keyword_p=*/false,
14892                                        /*check_dependency_p=*/true,
14893                                        /*type_p=*/false,
14894                                        /*is_declaration=*/true);
14895
14896   return cp_parser_namespace_name (parser);
14897 }
14898
14899 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14900    access declaration.
14901
14902    using-declaration:
14903      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14904      using :: unqualified-id ;  
14905
14906    access-declaration:
14907      qualified-id ;  
14908
14909    */
14910
14911 static bool
14912 cp_parser_using_declaration (cp_parser* parser, 
14913                              bool access_declaration_p)
14914 {
14915   cp_token *token;
14916   bool typename_p = false;
14917   bool global_scope_p;
14918   tree decl;
14919   tree identifier;
14920   tree qscope;
14921   int oldcount = errorcount;
14922   cp_token *diag_token = NULL;
14923
14924   if (access_declaration_p)
14925     {
14926       diag_token = cp_lexer_peek_token (parser->lexer);
14927       cp_parser_parse_tentatively (parser);
14928     }
14929   else
14930     {
14931       /* Look for the `using' keyword.  */
14932       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14933       
14934       /* Peek at the next token.  */
14935       token = cp_lexer_peek_token (parser->lexer);
14936       /* See if it's `typename'.  */
14937       if (token->keyword == RID_TYPENAME)
14938         {
14939           /* Remember that we've seen it.  */
14940           typename_p = true;
14941           /* Consume the `typename' token.  */
14942           cp_lexer_consume_token (parser->lexer);
14943         }
14944     }
14945
14946   /* Look for the optional global scope qualification.  */
14947   global_scope_p
14948     = (cp_parser_global_scope_opt (parser,
14949                                    /*current_scope_valid_p=*/false)
14950        != NULL_TREE);
14951
14952   /* If we saw `typename', or didn't see `::', then there must be a
14953      nested-name-specifier present.  */
14954   if (typename_p || !global_scope_p)
14955     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14956                                               /*check_dependency_p=*/true,
14957                                               /*type_p=*/false,
14958                                               /*is_declaration=*/true);
14959   /* Otherwise, we could be in either of the two productions.  In that
14960      case, treat the nested-name-specifier as optional.  */
14961   else
14962     qscope = cp_parser_nested_name_specifier_opt (parser,
14963                                                   /*typename_keyword_p=*/false,
14964                                                   /*check_dependency_p=*/true,
14965                                                   /*type_p=*/false,
14966                                                   /*is_declaration=*/true);
14967   if (!qscope)
14968     qscope = global_namespace;
14969
14970   if (access_declaration_p && cp_parser_error_occurred (parser))
14971     /* Something has already gone wrong; there's no need to parse
14972        further.  Since an error has occurred, the return value of
14973        cp_parser_parse_definitely will be false, as required.  */
14974     return cp_parser_parse_definitely (parser);
14975
14976   token = cp_lexer_peek_token (parser->lexer);
14977   /* Parse the unqualified-id.  */
14978   identifier = cp_parser_unqualified_id (parser,
14979                                          /*template_keyword_p=*/false,
14980                                          /*check_dependency_p=*/true,
14981                                          /*declarator_p=*/true,
14982                                          /*optional_p=*/false);
14983
14984   if (access_declaration_p)
14985     {
14986       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14987         cp_parser_simulate_error (parser);
14988       if (!cp_parser_parse_definitely (parser))
14989         return false;
14990     }
14991
14992   /* The function we call to handle a using-declaration is different
14993      depending on what scope we are in.  */
14994   if (qscope == error_mark_node || identifier == error_mark_node)
14995     ;
14996   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14997            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14998     /* [namespace.udecl]
14999
15000        A using declaration shall not name a template-id.  */
15001     error_at (token->location,
15002               "a template-id may not appear in a using-declaration");
15003   else
15004     {
15005       if (at_class_scope_p ())
15006         {
15007           /* Create the USING_DECL.  */
15008           decl = do_class_using_decl (parser->scope, identifier);
15009
15010           if (decl && typename_p)
15011             USING_DECL_TYPENAME_P (decl) = 1;
15012
15013           if (check_for_bare_parameter_packs (decl))
15014             return false;
15015           else
15016             /* Add it to the list of members in this class.  */
15017             finish_member_declaration (decl);
15018         }
15019       else
15020         {
15021           decl = cp_parser_lookup_name_simple (parser,
15022                                                identifier,
15023                                                token->location);
15024           if (decl == error_mark_node)
15025             cp_parser_name_lookup_error (parser, identifier,
15026                                          decl, NLE_NULL,
15027                                          token->location);
15028           else if (check_for_bare_parameter_packs (decl))
15029             return false;
15030           else if (!at_namespace_scope_p ())
15031             do_local_using_decl (decl, qscope, identifier);
15032           else
15033             do_toplevel_using_decl (decl, qscope, identifier);
15034         }
15035     }
15036
15037   /* Look for the final `;'.  */
15038   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15039
15040   if (access_declaration_p && errorcount == oldcount)
15041     warning_at (diag_token->location, OPT_Wdeprecated,
15042                 "access declarations are deprecated "
15043                 "in favour of using-declarations; "
15044                 "suggestion: add the %<using%> keyword");
15045
15046   return true;
15047 }
15048
15049 /* Parse an alias-declaration.
15050
15051    alias-declaration:
15052      using identifier attribute-specifier-seq [opt] = type-id  */
15053
15054 static tree
15055 cp_parser_alias_declaration (cp_parser* parser)
15056 {
15057   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15058   location_t id_location;
15059   cp_declarator *declarator;
15060   cp_decl_specifier_seq decl_specs;
15061   bool member_p;
15062   const char *saved_message = NULL;
15063
15064   /* Look for the `using' keyword.  */
15065   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15066   id_location = cp_lexer_peek_token (parser->lexer)->location;
15067   id = cp_parser_identifier (parser);
15068   if (id == error_mark_node)
15069     return error_mark_node;
15070
15071   attributes = cp_parser_attributes_opt (parser);
15072   if (attributes == error_mark_node)
15073     return error_mark_node;
15074
15075   cp_parser_require (parser, CPP_EQ, RT_EQ);
15076
15077   /* Now we are going to parse the type-id of the declaration.  */
15078
15079   /*
15080     [dcl.type]/3 says:
15081
15082         "A type-specifier-seq shall not define a class or enumeration
15083          unless it appears in the type-id of an alias-declaration (7.1.3) that
15084          is not the declaration of a template-declaration."
15085
15086     In other words, if we currently are in an alias template, the
15087     type-id should not define a type.
15088
15089     So let's set parser->type_definition_forbidden_message in that
15090     case; cp_parser_check_type_definition (called by
15091     cp_parser_class_specifier) will then emit an error if a type is
15092     defined in the type-id.  */
15093   if (parser->num_template_parameter_lists)
15094     {
15095       saved_message = parser->type_definition_forbidden_message;
15096       parser->type_definition_forbidden_message =
15097         G_("types may not be defined in alias template declarations");
15098     }
15099
15100   type = cp_parser_type_id (parser);
15101
15102   /* Restore the error message if need be.  */
15103   if (parser->num_template_parameter_lists)
15104     parser->type_definition_forbidden_message = saved_message;
15105
15106   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15107
15108   if (cp_parser_error_occurred (parser))
15109     return error_mark_node;
15110
15111   /* A typedef-name can also be introduced by an alias-declaration. The
15112      identifier following the using keyword becomes a typedef-name. It has
15113      the same semantics as if it were introduced by the typedef
15114      specifier. In particular, it does not define a new type and it shall
15115      not appear in the type-id.  */
15116
15117   clear_decl_specs (&decl_specs);
15118   decl_specs.type = type;
15119   decl_specs.attributes = attributes;
15120   ++decl_specs.specs[(int) ds_typedef];
15121   ++decl_specs.specs[(int) ds_alias];
15122
15123   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15124   declarator->id_loc = id_location;
15125
15126   member_p = at_class_scope_p ();
15127   if (member_p)
15128     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15129                       NULL_TREE, attributes);
15130   else
15131     decl = start_decl (declarator, &decl_specs, 0,
15132                        attributes, NULL_TREE, &pushed_scope);
15133   if (decl == error_mark_node)
15134     return decl;
15135
15136   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15137
15138   if (pushed_scope)
15139     pop_scope (pushed_scope);
15140
15141   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15142      added into the symbol table; otherwise, return the TYPE_DECL.  */
15143   if (DECL_LANG_SPECIFIC (decl)
15144       && DECL_TEMPLATE_INFO (decl)
15145       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15146     {
15147       decl = DECL_TI_TEMPLATE (decl);
15148       if (member_p)
15149         check_member_template (decl);
15150     }
15151
15152   return decl;
15153 }
15154
15155 /* Parse a using-directive.
15156
15157    using-directive:
15158      using namespace :: [opt] nested-name-specifier [opt]
15159        namespace-name ;  */
15160
15161 static void
15162 cp_parser_using_directive (cp_parser* parser)
15163 {
15164   tree namespace_decl;
15165   tree attribs;
15166
15167   /* Look for the `using' keyword.  */
15168   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15169   /* And the `namespace' keyword.  */
15170   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15171   /* Look for the optional `::' operator.  */
15172   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15173   /* And the optional nested-name-specifier.  */
15174   cp_parser_nested_name_specifier_opt (parser,
15175                                        /*typename_keyword_p=*/false,
15176                                        /*check_dependency_p=*/true,
15177                                        /*type_p=*/false,
15178                                        /*is_declaration=*/true);
15179   /* Get the namespace being used.  */
15180   namespace_decl = cp_parser_namespace_name (parser);
15181   /* And any specified attributes.  */
15182   attribs = cp_parser_attributes_opt (parser);
15183   /* Update the symbol table.  */
15184   parse_using_directive (namespace_decl, attribs);
15185   /* Look for the final `;'.  */
15186   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15187 }
15188
15189 /* Parse an asm-definition.
15190
15191    asm-definition:
15192      asm ( string-literal ) ;
15193
15194    GNU Extension:
15195
15196    asm-definition:
15197      asm volatile [opt] ( string-literal ) ;
15198      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15199      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15200                           : asm-operand-list [opt] ) ;
15201      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15202                           : asm-operand-list [opt]
15203                           : asm-clobber-list [opt] ) ;
15204      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15205                                : asm-clobber-list [opt]
15206                                : asm-goto-list ) ;  */
15207
15208 static void
15209 cp_parser_asm_definition (cp_parser* parser)
15210 {
15211   tree string;
15212   tree outputs = NULL_TREE;
15213   tree inputs = NULL_TREE;
15214   tree clobbers = NULL_TREE;
15215   tree labels = NULL_TREE;
15216   tree asm_stmt;
15217   bool volatile_p = false;
15218   bool extended_p = false;
15219   bool invalid_inputs_p = false;
15220   bool invalid_outputs_p = false;
15221   bool goto_p = false;
15222   required_token missing = RT_NONE;
15223
15224   /* Look for the `asm' keyword.  */
15225   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15226   /* See if the next token is `volatile'.  */
15227   if (cp_parser_allow_gnu_extensions_p (parser)
15228       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15229     {
15230       /* Remember that we saw the `volatile' keyword.  */
15231       volatile_p = true;
15232       /* Consume the token.  */
15233       cp_lexer_consume_token (parser->lexer);
15234     }
15235   if (cp_parser_allow_gnu_extensions_p (parser)
15236       && parser->in_function_body
15237       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15238     {
15239       /* Remember that we saw the `goto' keyword.  */
15240       goto_p = true;
15241       /* Consume the token.  */
15242       cp_lexer_consume_token (parser->lexer);
15243     }
15244   /* Look for the opening `('.  */
15245   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15246     return;
15247   /* Look for the string.  */
15248   string = cp_parser_string_literal (parser, false, false);
15249   if (string == error_mark_node)
15250     {
15251       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15252                                              /*consume_paren=*/true);
15253       return;
15254     }
15255
15256   /* If we're allowing GNU extensions, check for the extended assembly
15257      syntax.  Unfortunately, the `:' tokens need not be separated by
15258      a space in C, and so, for compatibility, we tolerate that here
15259      too.  Doing that means that we have to treat the `::' operator as
15260      two `:' tokens.  */
15261   if (cp_parser_allow_gnu_extensions_p (parser)
15262       && parser->in_function_body
15263       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15264           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15265     {
15266       bool inputs_p = false;
15267       bool clobbers_p = false;
15268       bool labels_p = false;
15269
15270       /* The extended syntax was used.  */
15271       extended_p = true;
15272
15273       /* Look for outputs.  */
15274       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15275         {
15276           /* Consume the `:'.  */
15277           cp_lexer_consume_token (parser->lexer);
15278           /* Parse the output-operands.  */
15279           if (cp_lexer_next_token_is_not (parser->lexer,
15280                                           CPP_COLON)
15281               && cp_lexer_next_token_is_not (parser->lexer,
15282                                              CPP_SCOPE)
15283               && cp_lexer_next_token_is_not (parser->lexer,
15284                                              CPP_CLOSE_PAREN)
15285               && !goto_p)
15286             outputs = cp_parser_asm_operand_list (parser);
15287
15288             if (outputs == error_mark_node)
15289               invalid_outputs_p = true;
15290         }
15291       /* If the next token is `::', there are no outputs, and the
15292          next token is the beginning of the inputs.  */
15293       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15294         /* The inputs are coming next.  */
15295         inputs_p = true;
15296
15297       /* Look for inputs.  */
15298       if (inputs_p
15299           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15300         {
15301           /* Consume the `:' or `::'.  */
15302           cp_lexer_consume_token (parser->lexer);
15303           /* Parse the output-operands.  */
15304           if (cp_lexer_next_token_is_not (parser->lexer,
15305                                           CPP_COLON)
15306               && cp_lexer_next_token_is_not (parser->lexer,
15307                                              CPP_SCOPE)
15308               && cp_lexer_next_token_is_not (parser->lexer,
15309                                              CPP_CLOSE_PAREN))
15310             inputs = cp_parser_asm_operand_list (parser);
15311
15312             if (inputs == error_mark_node)
15313               invalid_inputs_p = true;
15314         }
15315       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15316         /* The clobbers are coming next.  */
15317         clobbers_p = true;
15318
15319       /* Look for clobbers.  */
15320       if (clobbers_p
15321           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15322         {
15323           clobbers_p = true;
15324           /* Consume the `:' or `::'.  */
15325           cp_lexer_consume_token (parser->lexer);
15326           /* Parse the clobbers.  */
15327           if (cp_lexer_next_token_is_not (parser->lexer,
15328                                           CPP_COLON)
15329               && cp_lexer_next_token_is_not (parser->lexer,
15330                                              CPP_CLOSE_PAREN))
15331             clobbers = cp_parser_asm_clobber_list (parser);
15332         }
15333       else if (goto_p
15334                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15335         /* The labels are coming next.  */
15336         labels_p = true;
15337
15338       /* Look for labels.  */
15339       if (labels_p
15340           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15341         {
15342           labels_p = true;
15343           /* Consume the `:' or `::'.  */
15344           cp_lexer_consume_token (parser->lexer);
15345           /* Parse the labels.  */
15346           labels = cp_parser_asm_label_list (parser);
15347         }
15348
15349       if (goto_p && !labels_p)
15350         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15351     }
15352   else if (goto_p)
15353     missing = RT_COLON_SCOPE;
15354
15355   /* Look for the closing `)'.  */
15356   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15357                           missing ? missing : RT_CLOSE_PAREN))
15358     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15359                                            /*consume_paren=*/true);
15360   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15361
15362   if (!invalid_inputs_p && !invalid_outputs_p)
15363     {
15364       /* Create the ASM_EXPR.  */
15365       if (parser->in_function_body)
15366         {
15367           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15368                                       inputs, clobbers, labels);
15369           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15370           if (!extended_p)
15371             {
15372               tree temp = asm_stmt;
15373               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15374                 temp = TREE_OPERAND (temp, 0);
15375
15376               ASM_INPUT_P (temp) = 1;
15377             }
15378         }
15379       else
15380         cgraph_add_asm_node (string);
15381     }
15382 }
15383
15384 /* Declarators [gram.dcl.decl] */
15385
15386 /* Parse an init-declarator.
15387
15388    init-declarator:
15389      declarator initializer [opt]
15390
15391    GNU Extension:
15392
15393    init-declarator:
15394      declarator asm-specification [opt] attributes [opt] initializer [opt]
15395
15396    function-definition:
15397      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15398        function-body
15399      decl-specifier-seq [opt] declarator function-try-block
15400
15401    GNU Extension:
15402
15403    function-definition:
15404      __extension__ function-definition
15405
15406    TM Extension:
15407
15408    function-definition:
15409      decl-specifier-seq [opt] declarator function-transaction-block
15410
15411    The DECL_SPECIFIERS apply to this declarator.  Returns a
15412    representation of the entity declared.  If MEMBER_P is TRUE, then
15413    this declarator appears in a class scope.  The new DECL created by
15414    this declarator is returned.
15415
15416    The CHECKS are access checks that should be performed once we know
15417    what entity is being declared (and, therefore, what classes have
15418    befriended it).
15419
15420    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15421    for a function-definition here as well.  If the declarator is a
15422    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15423    be TRUE upon return.  By that point, the function-definition will
15424    have been completely parsed.
15425
15426    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15427    is FALSE.
15428
15429    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15430    parsed declaration if it is an uninitialized single declarator not followed
15431    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15432    if present, will not be consumed.  If returned, this declarator will be
15433    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15434
15435 static tree
15436 cp_parser_init_declarator (cp_parser* parser,
15437                            cp_decl_specifier_seq *decl_specifiers,
15438                            VEC (deferred_access_check,gc)* checks,
15439                            bool function_definition_allowed_p,
15440                            bool member_p,
15441                            int declares_class_or_enum,
15442                            bool* function_definition_p,
15443                            tree* maybe_range_for_decl)
15444 {
15445   cp_token *token = NULL, *asm_spec_start_token = NULL,
15446            *attributes_start_token = NULL;
15447   cp_declarator *declarator;
15448   tree prefix_attributes;
15449   tree attributes;
15450   tree asm_specification;
15451   tree initializer;
15452   tree decl = NULL_TREE;
15453   tree scope;
15454   int is_initialized;
15455   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15456      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15457      "(...)".  */
15458   enum cpp_ttype initialization_kind;
15459   bool is_direct_init = false;
15460   bool is_non_constant_init;
15461   int ctor_dtor_or_conv_p;
15462   bool friend_p;
15463   tree pushed_scope = NULL_TREE;
15464   bool range_for_decl_p = false;
15465
15466   /* Gather the attributes that were provided with the
15467      decl-specifiers.  */
15468   prefix_attributes = decl_specifiers->attributes;
15469
15470   /* Assume that this is not the declarator for a function
15471      definition.  */
15472   if (function_definition_p)
15473     *function_definition_p = false;
15474
15475   /* Defer access checks while parsing the declarator; we cannot know
15476      what names are accessible until we know what is being
15477      declared.  */
15478   resume_deferring_access_checks ();
15479
15480   /* Parse the declarator.  */
15481   token = cp_lexer_peek_token (parser->lexer);
15482   declarator
15483     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15484                             &ctor_dtor_or_conv_p,
15485                             /*parenthesized_p=*/NULL,
15486                             member_p);
15487   /* Gather up the deferred checks.  */
15488   stop_deferring_access_checks ();
15489
15490   /* If the DECLARATOR was erroneous, there's no need to go
15491      further.  */
15492   if (declarator == cp_error_declarator)
15493     return error_mark_node;
15494
15495   /* Check that the number of template-parameter-lists is OK.  */
15496   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15497                                                        token->location))
15498     return error_mark_node;
15499
15500   if (declares_class_or_enum & 2)
15501     cp_parser_check_for_definition_in_return_type (declarator,
15502                                                    decl_specifiers->type,
15503                                                    decl_specifiers->type_location);
15504
15505   /* Figure out what scope the entity declared by the DECLARATOR is
15506      located in.  `grokdeclarator' sometimes changes the scope, so
15507      we compute it now.  */
15508   scope = get_scope_of_declarator (declarator);
15509
15510   /* Perform any lookups in the declared type which were thought to be
15511      dependent, but are not in the scope of the declarator.  */
15512   decl_specifiers->type
15513     = maybe_update_decl_type (decl_specifiers->type, scope);
15514
15515   /* If we're allowing GNU extensions, look for an asm-specification
15516      and attributes.  */
15517   if (cp_parser_allow_gnu_extensions_p (parser))
15518     {
15519       /* Look for an asm-specification.  */
15520       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15521       asm_specification = cp_parser_asm_specification_opt (parser);
15522       /* And attributes.  */
15523       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15524       attributes = cp_parser_attributes_opt (parser);
15525     }
15526   else
15527     {
15528       asm_specification = NULL_TREE;
15529       attributes = NULL_TREE;
15530     }
15531
15532   /* Peek at the next token.  */
15533   token = cp_lexer_peek_token (parser->lexer);
15534   /* Check to see if the token indicates the start of a
15535      function-definition.  */
15536   if (function_declarator_p (declarator)
15537       && cp_parser_token_starts_function_definition_p (token))
15538     {
15539       if (!function_definition_allowed_p)
15540         {
15541           /* If a function-definition should not appear here, issue an
15542              error message.  */
15543           cp_parser_error (parser,
15544                            "a function-definition is not allowed here");
15545           return error_mark_node;
15546         }
15547       else
15548         {
15549           location_t func_brace_location
15550             = cp_lexer_peek_token (parser->lexer)->location;
15551
15552           /* Neither attributes nor an asm-specification are allowed
15553              on a function-definition.  */
15554           if (asm_specification)
15555             error_at (asm_spec_start_token->location,
15556                       "an asm-specification is not allowed "
15557                       "on a function-definition");
15558           if (attributes)
15559             error_at (attributes_start_token->location,
15560                       "attributes are not allowed on a function-definition");
15561           /* This is a function-definition.  */
15562           *function_definition_p = true;
15563
15564           /* Parse the function definition.  */
15565           if (member_p)
15566             decl = cp_parser_save_member_function_body (parser,
15567                                                         decl_specifiers,
15568                                                         declarator,
15569                                                         prefix_attributes);
15570           else
15571             decl
15572               = (cp_parser_function_definition_from_specifiers_and_declarator
15573                  (parser, decl_specifiers, prefix_attributes, declarator));
15574
15575           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15576             {
15577               /* This is where the prologue starts...  */
15578               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15579                 = func_brace_location;
15580             }
15581
15582           return decl;
15583         }
15584     }
15585
15586   /* [dcl.dcl]
15587
15588      Only in function declarations for constructors, destructors, and
15589      type conversions can the decl-specifier-seq be omitted.
15590
15591      We explicitly postpone this check past the point where we handle
15592      function-definitions because we tolerate function-definitions
15593      that are missing their return types in some modes.  */
15594   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15595     {
15596       cp_parser_error (parser,
15597                        "expected constructor, destructor, or type conversion");
15598       return error_mark_node;
15599     }
15600
15601   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15602   if (token->type == CPP_EQ
15603       || token->type == CPP_OPEN_PAREN
15604       || token->type == CPP_OPEN_BRACE)
15605     {
15606       is_initialized = SD_INITIALIZED;
15607       initialization_kind = token->type;
15608       if (maybe_range_for_decl)
15609         *maybe_range_for_decl = error_mark_node;
15610
15611       if (token->type == CPP_EQ
15612           && function_declarator_p (declarator))
15613         {
15614           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15615           if (t2->keyword == RID_DEFAULT)
15616             is_initialized = SD_DEFAULTED;
15617           else if (t2->keyword == RID_DELETE)
15618             is_initialized = SD_DELETED;
15619         }
15620     }
15621   else
15622     {
15623       /* If the init-declarator isn't initialized and isn't followed by a
15624          `,' or `;', it's not a valid init-declarator.  */
15625       if (token->type != CPP_COMMA
15626           && token->type != CPP_SEMICOLON)
15627         {
15628           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15629             range_for_decl_p = true;
15630           else
15631             {
15632               cp_parser_error (parser, "expected initializer");
15633               return error_mark_node;
15634             }
15635         }
15636       is_initialized = SD_UNINITIALIZED;
15637       initialization_kind = CPP_EOF;
15638     }
15639
15640   /* Because start_decl has side-effects, we should only call it if we
15641      know we're going ahead.  By this point, we know that we cannot
15642      possibly be looking at any other construct.  */
15643   cp_parser_commit_to_tentative_parse (parser);
15644
15645   /* If the decl specifiers were bad, issue an error now that we're
15646      sure this was intended to be a declarator.  Then continue
15647      declaring the variable(s), as int, to try to cut down on further
15648      errors.  */
15649   if (decl_specifiers->any_specifiers_p
15650       && decl_specifiers->type == error_mark_node)
15651     {
15652       cp_parser_error (parser, "invalid type in declaration");
15653       decl_specifiers->type = integer_type_node;
15654     }
15655
15656   /* Check to see whether or not this declaration is a friend.  */
15657   friend_p = cp_parser_friend_p (decl_specifiers);
15658
15659   /* Enter the newly declared entry in the symbol table.  If we're
15660      processing a declaration in a class-specifier, we wait until
15661      after processing the initializer.  */
15662   if (!member_p)
15663     {
15664       if (parser->in_unbraced_linkage_specification_p)
15665         decl_specifiers->storage_class = sc_extern;
15666       decl = start_decl (declarator, decl_specifiers,
15667                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15668                          attributes, prefix_attributes,
15669                          &pushed_scope);
15670       /* Adjust location of decl if declarator->id_loc is more appropriate:
15671          set, and decl wasn't merged with another decl, in which case its
15672          location would be different from input_location, and more accurate.  */
15673       if (DECL_P (decl)
15674           && declarator->id_loc != UNKNOWN_LOCATION
15675           && DECL_SOURCE_LOCATION (decl) == input_location)
15676         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15677     }
15678   else if (scope)
15679     /* Enter the SCOPE.  That way unqualified names appearing in the
15680        initializer will be looked up in SCOPE.  */
15681     pushed_scope = push_scope (scope);
15682
15683   /* Perform deferred access control checks, now that we know in which
15684      SCOPE the declared entity resides.  */
15685   if (!member_p && decl)
15686     {
15687       tree saved_current_function_decl = NULL_TREE;
15688
15689       /* If the entity being declared is a function, pretend that we
15690          are in its scope.  If it is a `friend', it may have access to
15691          things that would not otherwise be accessible.  */
15692       if (TREE_CODE (decl) == FUNCTION_DECL)
15693         {
15694           saved_current_function_decl = current_function_decl;
15695           current_function_decl = decl;
15696         }
15697
15698       /* Perform access checks for template parameters.  */
15699       cp_parser_perform_template_parameter_access_checks (checks);
15700
15701       /* Perform the access control checks for the declarator and the
15702          decl-specifiers.  */
15703       perform_deferred_access_checks ();
15704
15705       /* Restore the saved value.  */
15706       if (TREE_CODE (decl) == FUNCTION_DECL)
15707         current_function_decl = saved_current_function_decl;
15708     }
15709
15710   /* Parse the initializer.  */
15711   initializer = NULL_TREE;
15712   is_direct_init = false;
15713   is_non_constant_init = true;
15714   if (is_initialized)
15715     {
15716       if (function_declarator_p (declarator))
15717         {
15718           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15719            if (initialization_kind == CPP_EQ)
15720              initializer = cp_parser_pure_specifier (parser);
15721            else
15722              {
15723                /* If the declaration was erroneous, we don't really
15724                   know what the user intended, so just silently
15725                   consume the initializer.  */
15726                if (decl != error_mark_node)
15727                  error_at (initializer_start_token->location,
15728                            "initializer provided for function");
15729                cp_parser_skip_to_closing_parenthesis (parser,
15730                                                       /*recovering=*/true,
15731                                                       /*or_comma=*/false,
15732                                                       /*consume_paren=*/true);
15733              }
15734         }
15735       else
15736         {
15737           /* We want to record the extra mangling scope for in-class
15738              initializers of class members and initializers of static data
15739              member templates.  The former involves deferring
15740              parsing of the initializer until end of class as with default
15741              arguments.  So right here we only handle the latter.  */
15742           if (!member_p && processing_template_decl)
15743             start_lambda_scope (decl);
15744           initializer = cp_parser_initializer (parser,
15745                                                &is_direct_init,
15746                                                &is_non_constant_init);
15747           if (!member_p && processing_template_decl)
15748             finish_lambda_scope ();
15749         }
15750     }
15751
15752   /* The old parser allows attributes to appear after a parenthesized
15753      initializer.  Mark Mitchell proposed removing this functionality
15754      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15755      attributes -- but ignores them.  */
15756   if (cp_parser_allow_gnu_extensions_p (parser)
15757       && initialization_kind == CPP_OPEN_PAREN)
15758     if (cp_parser_attributes_opt (parser))
15759       warning (OPT_Wattributes,
15760                "attributes after parenthesized initializer ignored");
15761
15762   /* For an in-class declaration, use `grokfield' to create the
15763      declaration.  */
15764   if (member_p)
15765     {
15766       if (pushed_scope)
15767         {
15768           pop_scope (pushed_scope);
15769           pushed_scope = NULL_TREE;
15770         }
15771       decl = grokfield (declarator, decl_specifiers,
15772                         initializer, !is_non_constant_init,
15773                         /*asmspec=*/NULL_TREE,
15774                         prefix_attributes);
15775       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15776         cp_parser_save_default_args (parser, decl);
15777     }
15778
15779   /* Finish processing the declaration.  But, skip member
15780      declarations.  */
15781   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15782     {
15783       cp_finish_decl (decl,
15784                       initializer, !is_non_constant_init,
15785                       asm_specification,
15786                       /* If the initializer is in parentheses, then this is
15787                          a direct-initialization, which means that an
15788                          `explicit' constructor is OK.  Otherwise, an
15789                          `explicit' constructor cannot be used.  */
15790                       ((is_direct_init || !is_initialized)
15791                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15792     }
15793   else if ((cxx_dialect != cxx98) && friend_p
15794            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15795     /* Core issue #226 (C++0x only): A default template-argument
15796        shall not be specified in a friend class template
15797        declaration. */
15798     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15799                              /*is_partial=*/0, /*is_friend_decl=*/1);
15800
15801   if (!friend_p && pushed_scope)
15802     pop_scope (pushed_scope);
15803
15804   return decl;
15805 }
15806
15807 /* Parse a declarator.
15808
15809    declarator:
15810      direct-declarator
15811      ptr-operator declarator
15812
15813    abstract-declarator:
15814      ptr-operator abstract-declarator [opt]
15815      direct-abstract-declarator
15816
15817    GNU Extensions:
15818
15819    declarator:
15820      attributes [opt] direct-declarator
15821      attributes [opt] ptr-operator declarator
15822
15823    abstract-declarator:
15824      attributes [opt] ptr-operator abstract-declarator [opt]
15825      attributes [opt] direct-abstract-declarator
15826
15827    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15828    detect constructor, destructor or conversion operators. It is set
15829    to -1 if the declarator is a name, and +1 if it is a
15830    function. Otherwise it is set to zero. Usually you just want to
15831    test for >0, but internally the negative value is used.
15832
15833    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15834    a decl-specifier-seq unless it declares a constructor, destructor,
15835    or conversion.  It might seem that we could check this condition in
15836    semantic analysis, rather than parsing, but that makes it difficult
15837    to handle something like `f()'.  We want to notice that there are
15838    no decl-specifiers, and therefore realize that this is an
15839    expression, not a declaration.)
15840
15841    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15842    the declarator is a direct-declarator of the form "(...)".
15843
15844    MEMBER_P is true iff this declarator is a member-declarator.  */
15845
15846 static cp_declarator *
15847 cp_parser_declarator (cp_parser* parser,
15848                       cp_parser_declarator_kind dcl_kind,
15849                       int* ctor_dtor_or_conv_p,
15850                       bool* parenthesized_p,
15851                       bool member_p)
15852 {
15853   cp_declarator *declarator;
15854   enum tree_code code;
15855   cp_cv_quals cv_quals;
15856   tree class_type;
15857   tree attributes = NULL_TREE;
15858
15859   /* Assume this is not a constructor, destructor, or type-conversion
15860      operator.  */
15861   if (ctor_dtor_or_conv_p)
15862     *ctor_dtor_or_conv_p = 0;
15863
15864   if (cp_parser_allow_gnu_extensions_p (parser))
15865     attributes = cp_parser_attributes_opt (parser);
15866
15867   /* Check for the ptr-operator production.  */
15868   cp_parser_parse_tentatively (parser);
15869   /* Parse the ptr-operator.  */
15870   code = cp_parser_ptr_operator (parser,
15871                                  &class_type,
15872                                  &cv_quals);
15873   /* If that worked, then we have a ptr-operator.  */
15874   if (cp_parser_parse_definitely (parser))
15875     {
15876       /* If a ptr-operator was found, then this declarator was not
15877          parenthesized.  */
15878       if (parenthesized_p)
15879         *parenthesized_p = true;
15880       /* The dependent declarator is optional if we are parsing an
15881          abstract-declarator.  */
15882       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15883         cp_parser_parse_tentatively (parser);
15884
15885       /* Parse the dependent declarator.  */
15886       declarator = cp_parser_declarator (parser, dcl_kind,
15887                                          /*ctor_dtor_or_conv_p=*/NULL,
15888                                          /*parenthesized_p=*/NULL,
15889                                          /*member_p=*/false);
15890
15891       /* If we are parsing an abstract-declarator, we must handle the
15892          case where the dependent declarator is absent.  */
15893       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15894           && !cp_parser_parse_definitely (parser))
15895         declarator = NULL;
15896
15897       declarator = cp_parser_make_indirect_declarator
15898         (code, class_type, cv_quals, declarator);
15899     }
15900   /* Everything else is a direct-declarator.  */
15901   else
15902     {
15903       if (parenthesized_p)
15904         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15905                                                    CPP_OPEN_PAREN);
15906       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15907                                                 ctor_dtor_or_conv_p,
15908                                                 member_p);
15909     }
15910
15911   if (attributes && declarator && declarator != cp_error_declarator)
15912     declarator->attributes = attributes;
15913
15914   return declarator;
15915 }
15916
15917 /* Parse a direct-declarator or direct-abstract-declarator.
15918
15919    direct-declarator:
15920      declarator-id
15921      direct-declarator ( parameter-declaration-clause )
15922        cv-qualifier-seq [opt]
15923        exception-specification [opt]
15924      direct-declarator [ constant-expression [opt] ]
15925      ( declarator )
15926
15927    direct-abstract-declarator:
15928      direct-abstract-declarator [opt]
15929        ( parameter-declaration-clause )
15930        cv-qualifier-seq [opt]
15931        exception-specification [opt]
15932      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15933      ( abstract-declarator )
15934
15935    Returns a representation of the declarator.  DCL_KIND is
15936    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15937    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15938    we are parsing a direct-declarator.  It is
15939    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15940    of ambiguity we prefer an abstract declarator, as per
15941    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15942    cp_parser_declarator.  */
15943
15944 static cp_declarator *
15945 cp_parser_direct_declarator (cp_parser* parser,
15946                              cp_parser_declarator_kind dcl_kind,
15947                              int* ctor_dtor_or_conv_p,
15948                              bool member_p)
15949 {
15950   cp_token *token;
15951   cp_declarator *declarator = NULL;
15952   tree scope = NULL_TREE;
15953   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15954   bool saved_in_declarator_p = parser->in_declarator_p;
15955   bool first = true;
15956   tree pushed_scope = NULL_TREE;
15957
15958   while (true)
15959     {
15960       /* Peek at the next token.  */
15961       token = cp_lexer_peek_token (parser->lexer);
15962       if (token->type == CPP_OPEN_PAREN)
15963         {
15964           /* This is either a parameter-declaration-clause, or a
15965              parenthesized declarator. When we know we are parsing a
15966              named declarator, it must be a parenthesized declarator
15967              if FIRST is true. For instance, `(int)' is a
15968              parameter-declaration-clause, with an omitted
15969              direct-abstract-declarator. But `((*))', is a
15970              parenthesized abstract declarator. Finally, when T is a
15971              template parameter `(T)' is a
15972              parameter-declaration-clause, and not a parenthesized
15973              named declarator.
15974
15975              We first try and parse a parameter-declaration-clause,
15976              and then try a nested declarator (if FIRST is true).
15977
15978              It is not an error for it not to be a
15979              parameter-declaration-clause, even when FIRST is
15980              false. Consider,
15981
15982                int i (int);
15983                int i (3);
15984
15985              The first is the declaration of a function while the
15986              second is the definition of a variable, including its
15987              initializer.
15988
15989              Having seen only the parenthesis, we cannot know which of
15990              these two alternatives should be selected.  Even more
15991              complex are examples like:
15992
15993                int i (int (a));
15994                int i (int (3));
15995
15996              The former is a function-declaration; the latter is a
15997              variable initialization.
15998
15999              Thus again, we try a parameter-declaration-clause, and if
16000              that fails, we back out and return.  */
16001
16002           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16003             {
16004               tree params;
16005               unsigned saved_num_template_parameter_lists;
16006               bool is_declarator = false;
16007               tree t;
16008
16009               /* In a member-declarator, the only valid interpretation
16010                  of a parenthesis is the start of a
16011                  parameter-declaration-clause.  (It is invalid to
16012                  initialize a static data member with a parenthesized
16013                  initializer; only the "=" form of initialization is
16014                  permitted.)  */
16015               if (!member_p)
16016                 cp_parser_parse_tentatively (parser);
16017
16018               /* Consume the `('.  */
16019               cp_lexer_consume_token (parser->lexer);
16020               if (first)
16021                 {
16022                   /* If this is going to be an abstract declarator, we're
16023                      in a declarator and we can't have default args.  */
16024                   parser->default_arg_ok_p = false;
16025                   parser->in_declarator_p = true;
16026                 }
16027
16028               /* Inside the function parameter list, surrounding
16029                  template-parameter-lists do not apply.  */
16030               saved_num_template_parameter_lists
16031                 = parser->num_template_parameter_lists;
16032               parser->num_template_parameter_lists = 0;
16033
16034               begin_scope (sk_function_parms, NULL_TREE);
16035
16036               /* Parse the parameter-declaration-clause.  */
16037               params = cp_parser_parameter_declaration_clause (parser);
16038
16039               parser->num_template_parameter_lists
16040                 = saved_num_template_parameter_lists;
16041
16042               /* Consume the `)'.  */
16043               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16044
16045               /* If all went well, parse the cv-qualifier-seq and the
16046                  exception-specification.  */
16047               if (member_p || cp_parser_parse_definitely (parser))
16048                 {
16049                   cp_cv_quals cv_quals;
16050                   cp_virt_specifiers virt_specifiers;
16051                   tree exception_specification;
16052                   tree late_return;
16053
16054                   is_declarator = true;
16055
16056                   if (ctor_dtor_or_conv_p)
16057                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16058                   first = false;
16059
16060                   /* Parse the cv-qualifier-seq.  */
16061                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16062                   /* And the exception-specification.  */
16063                   exception_specification
16064                     = cp_parser_exception_specification_opt (parser);
16065                   /* Parse the virt-specifier-seq.  */
16066                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16067
16068                   late_return = (cp_parser_late_return_type_opt
16069                                  (parser, member_p ? cv_quals : -1));
16070
16071                   /* Create the function-declarator.  */
16072                   declarator = make_call_declarator (declarator,
16073                                                      params,
16074                                                      cv_quals,
16075                                                      virt_specifiers,
16076                                                      exception_specification,
16077                                                      late_return);
16078                   /* Any subsequent parameter lists are to do with
16079                      return type, so are not those of the declared
16080                      function.  */
16081                   parser->default_arg_ok_p = false;
16082                 }
16083
16084               /* Remove the function parms from scope.  */
16085               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16086                 pop_binding (DECL_NAME (t), t);
16087               leave_scope();
16088
16089               if (is_declarator)
16090                 /* Repeat the main loop.  */
16091                 continue;
16092             }
16093
16094           /* If this is the first, we can try a parenthesized
16095              declarator.  */
16096           if (first)
16097             {
16098               bool saved_in_type_id_in_expr_p;
16099
16100               parser->default_arg_ok_p = saved_default_arg_ok_p;
16101               parser->in_declarator_p = saved_in_declarator_p;
16102
16103               /* Consume the `('.  */
16104               cp_lexer_consume_token (parser->lexer);
16105               /* Parse the nested declarator.  */
16106               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16107               parser->in_type_id_in_expr_p = true;
16108               declarator
16109                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16110                                         /*parenthesized_p=*/NULL,
16111                                         member_p);
16112               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16113               first = false;
16114               /* Expect a `)'.  */
16115               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16116                 declarator = cp_error_declarator;
16117               if (declarator == cp_error_declarator)
16118                 break;
16119
16120               goto handle_declarator;
16121             }
16122           /* Otherwise, we must be done.  */
16123           else
16124             break;
16125         }
16126       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16127                && token->type == CPP_OPEN_SQUARE)
16128         {
16129           /* Parse an array-declarator.  */
16130           tree bounds;
16131
16132           if (ctor_dtor_or_conv_p)
16133             *ctor_dtor_or_conv_p = 0;
16134
16135           first = false;
16136           parser->default_arg_ok_p = false;
16137           parser->in_declarator_p = true;
16138           /* Consume the `['.  */
16139           cp_lexer_consume_token (parser->lexer);
16140           /* Peek at the next token.  */
16141           token = cp_lexer_peek_token (parser->lexer);
16142           /* If the next token is `]', then there is no
16143              constant-expression.  */
16144           if (token->type != CPP_CLOSE_SQUARE)
16145             {
16146               bool non_constant_p;
16147
16148               bounds
16149                 = cp_parser_constant_expression (parser,
16150                                                  /*allow_non_constant=*/true,
16151                                                  &non_constant_p);
16152               if (!non_constant_p)
16153                 /* OK */;
16154               else if (error_operand_p (bounds))
16155                 /* Already gave an error.  */;
16156               else if (!parser->in_function_body
16157                        || current_binding_level->kind == sk_function_parms)
16158                 {
16159                   /* Normally, the array bound must be an integral constant
16160                      expression.  However, as an extension, we allow VLAs
16161                      in function scopes as long as they aren't part of a
16162                      parameter declaration.  */
16163                   cp_parser_error (parser,
16164                                    "array bound is not an integer constant");
16165                   bounds = error_mark_node;
16166                 }
16167               else if (processing_template_decl)
16168                 {
16169                   /* Remember this wasn't a constant-expression.  */
16170                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16171                   TREE_SIDE_EFFECTS (bounds) = 1;
16172                 }
16173             }
16174           else
16175             bounds = NULL_TREE;
16176           /* Look for the closing `]'.  */
16177           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16178             {
16179               declarator = cp_error_declarator;
16180               break;
16181             }
16182
16183           declarator = make_array_declarator (declarator, bounds);
16184         }
16185       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16186         {
16187           {
16188             tree qualifying_scope;
16189             tree unqualified_name;
16190             special_function_kind sfk;
16191             bool abstract_ok;
16192             bool pack_expansion_p = false;
16193             cp_token *declarator_id_start_token;
16194
16195             /* Parse a declarator-id */
16196             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16197             if (abstract_ok)
16198               {
16199                 cp_parser_parse_tentatively (parser);
16200
16201                 /* If we see an ellipsis, we should be looking at a
16202                    parameter pack. */
16203                 if (token->type == CPP_ELLIPSIS)
16204                   {
16205                     /* Consume the `...' */
16206                     cp_lexer_consume_token (parser->lexer);
16207
16208                     pack_expansion_p = true;
16209                   }
16210               }
16211
16212             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16213             unqualified_name
16214               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16215             qualifying_scope = parser->scope;
16216             if (abstract_ok)
16217               {
16218                 bool okay = false;
16219
16220                 if (!unqualified_name && pack_expansion_p)
16221                   {
16222                     /* Check whether an error occurred. */
16223                     okay = !cp_parser_error_occurred (parser);
16224
16225                     /* We already consumed the ellipsis to mark a
16226                        parameter pack, but we have no way to report it,
16227                        so abort the tentative parse. We will be exiting
16228                        immediately anyway. */
16229                     cp_parser_abort_tentative_parse (parser);
16230                   }
16231                 else
16232                   okay = cp_parser_parse_definitely (parser);
16233
16234                 if (!okay)
16235                   unqualified_name = error_mark_node;
16236                 else if (unqualified_name
16237                          && (qualifying_scope
16238                              || (TREE_CODE (unqualified_name)
16239                                  != IDENTIFIER_NODE)))
16240                   {
16241                     cp_parser_error (parser, "expected unqualified-id");
16242                     unqualified_name = error_mark_node;
16243                   }
16244               }
16245
16246             if (!unqualified_name)
16247               return NULL;
16248             if (unqualified_name == error_mark_node)
16249               {
16250                 declarator = cp_error_declarator;
16251                 pack_expansion_p = false;
16252                 declarator->parameter_pack_p = false;
16253                 break;
16254               }
16255
16256             if (qualifying_scope && at_namespace_scope_p ()
16257                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16258               {
16259                 /* In the declaration of a member of a template class
16260                    outside of the class itself, the SCOPE will sometimes
16261                    be a TYPENAME_TYPE.  For example, given:
16262
16263                    template <typename T>
16264                    int S<T>::R::i = 3;
16265
16266                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16267                    this context, we must resolve S<T>::R to an ordinary
16268                    type, rather than a typename type.
16269
16270                    The reason we normally avoid resolving TYPENAME_TYPEs
16271                    is that a specialization of `S' might render
16272                    `S<T>::R' not a type.  However, if `S' is
16273                    specialized, then this `i' will not be used, so there
16274                    is no harm in resolving the types here.  */
16275                 tree type;
16276
16277                 /* Resolve the TYPENAME_TYPE.  */
16278                 type = resolve_typename_type (qualifying_scope,
16279                                               /*only_current_p=*/false);
16280                 /* If that failed, the declarator is invalid.  */
16281                 if (TREE_CODE (type) == TYPENAME_TYPE)
16282                   {
16283                     if (typedef_variant_p (type))
16284                       error_at (declarator_id_start_token->location,
16285                                 "cannot define member of dependent typedef "
16286                                 "%qT", type);
16287                     else
16288                       error_at (declarator_id_start_token->location,
16289                                 "%<%T::%E%> is not a type",
16290                                 TYPE_CONTEXT (qualifying_scope),
16291                                 TYPE_IDENTIFIER (qualifying_scope));
16292                   }
16293                 qualifying_scope = type;
16294               }
16295
16296             sfk = sfk_none;
16297
16298             if (unqualified_name)
16299               {
16300                 tree class_type;
16301
16302                 if (qualifying_scope
16303                     && CLASS_TYPE_P (qualifying_scope))
16304                   class_type = qualifying_scope;
16305                 else
16306                   class_type = current_class_type;
16307
16308                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16309                   {
16310                     tree name_type = TREE_TYPE (unqualified_name);
16311                     if (class_type && same_type_p (name_type, class_type))
16312                       {
16313                         if (qualifying_scope
16314                             && CLASSTYPE_USE_TEMPLATE (name_type))
16315                           {
16316                             error_at (declarator_id_start_token->location,
16317                                       "invalid use of constructor as a template");
16318                             inform (declarator_id_start_token->location,
16319                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16320                                     "name the constructor in a qualified name",
16321                                     class_type,
16322                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16323                                     class_type, name_type);
16324                             declarator = cp_error_declarator;
16325                             break;
16326                           }
16327                         else
16328                           unqualified_name = constructor_name (class_type);
16329                       }
16330                     else
16331                       {
16332                         /* We do not attempt to print the declarator
16333                            here because we do not have enough
16334                            information about its original syntactic
16335                            form.  */
16336                         cp_parser_error (parser, "invalid declarator");
16337                         declarator = cp_error_declarator;
16338                         break;
16339                       }
16340                   }
16341
16342                 if (class_type)
16343                   {
16344                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16345                       sfk = sfk_destructor;
16346                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16347                       sfk = sfk_conversion;
16348                     else if (/* There's no way to declare a constructor
16349                                 for an anonymous type, even if the type
16350                                 got a name for linkage purposes.  */
16351                              !TYPE_WAS_ANONYMOUS (class_type)
16352                              && constructor_name_p (unqualified_name,
16353                                                     class_type))
16354                       {
16355                         unqualified_name = constructor_name (class_type);
16356                         sfk = sfk_constructor;
16357                       }
16358                     else if (is_overloaded_fn (unqualified_name)
16359                              && DECL_CONSTRUCTOR_P (get_first_fn
16360                                                     (unqualified_name)))
16361                       sfk = sfk_constructor;
16362
16363                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16364                       *ctor_dtor_or_conv_p = -1;
16365                   }
16366               }
16367             declarator = make_id_declarator (qualifying_scope,
16368                                              unqualified_name,
16369                                              sfk);
16370             declarator->id_loc = token->location;
16371             declarator->parameter_pack_p = pack_expansion_p;
16372
16373             if (pack_expansion_p)
16374               maybe_warn_variadic_templates ();
16375           }
16376
16377         handle_declarator:;
16378           scope = get_scope_of_declarator (declarator);
16379           if (scope)
16380             /* Any names that appear after the declarator-id for a
16381                member are looked up in the containing scope.  */
16382             pushed_scope = push_scope (scope);
16383           parser->in_declarator_p = true;
16384           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16385               || (declarator && declarator->kind == cdk_id))
16386             /* Default args are only allowed on function
16387                declarations.  */
16388             parser->default_arg_ok_p = saved_default_arg_ok_p;
16389           else
16390             parser->default_arg_ok_p = false;
16391
16392           first = false;
16393         }
16394       /* We're done.  */
16395       else
16396         break;
16397     }
16398
16399   /* For an abstract declarator, we might wind up with nothing at this
16400      point.  That's an error; the declarator is not optional.  */
16401   if (!declarator)
16402     cp_parser_error (parser, "expected declarator");
16403
16404   /* If we entered a scope, we must exit it now.  */
16405   if (pushed_scope)
16406     pop_scope (pushed_scope);
16407
16408   parser->default_arg_ok_p = saved_default_arg_ok_p;
16409   parser->in_declarator_p = saved_in_declarator_p;
16410
16411   return declarator;
16412 }
16413
16414 /* Parse a ptr-operator.
16415
16416    ptr-operator:
16417      * cv-qualifier-seq [opt]
16418      &
16419      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16420
16421    GNU Extension:
16422
16423    ptr-operator:
16424      & cv-qualifier-seq [opt]
16425
16426    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16427    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16428    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16429    filled in with the TYPE containing the member.  *CV_QUALS is
16430    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16431    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16432    Note that the tree codes returned by this function have nothing
16433    to do with the types of trees that will be eventually be created
16434    to represent the pointer or reference type being parsed. They are
16435    just constants with suggestive names. */
16436 static enum tree_code
16437 cp_parser_ptr_operator (cp_parser* parser,
16438                         tree* type,
16439                         cp_cv_quals *cv_quals)
16440 {
16441   enum tree_code code = ERROR_MARK;
16442   cp_token *token;
16443
16444   /* Assume that it's not a pointer-to-member.  */
16445   *type = NULL_TREE;
16446   /* And that there are no cv-qualifiers.  */
16447   *cv_quals = TYPE_UNQUALIFIED;
16448
16449   /* Peek at the next token.  */
16450   token = cp_lexer_peek_token (parser->lexer);
16451
16452   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16453   if (token->type == CPP_MULT)
16454     code = INDIRECT_REF;
16455   else if (token->type == CPP_AND)
16456     code = ADDR_EXPR;
16457   else if ((cxx_dialect != cxx98) &&
16458            token->type == CPP_AND_AND) /* C++0x only */
16459     code = NON_LVALUE_EXPR;
16460
16461   if (code != ERROR_MARK)
16462     {
16463       /* Consume the `*', `&' or `&&'.  */
16464       cp_lexer_consume_token (parser->lexer);
16465
16466       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16467          `&', if we are allowing GNU extensions.  (The only qualifier
16468          that can legally appear after `&' is `restrict', but that is
16469          enforced during semantic analysis.  */
16470       if (code == INDIRECT_REF
16471           || cp_parser_allow_gnu_extensions_p (parser))
16472         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16473     }
16474   else
16475     {
16476       /* Try the pointer-to-member case.  */
16477       cp_parser_parse_tentatively (parser);
16478       /* Look for the optional `::' operator.  */
16479       cp_parser_global_scope_opt (parser,
16480                                   /*current_scope_valid_p=*/false);
16481       /* Look for the nested-name specifier.  */
16482       token = cp_lexer_peek_token (parser->lexer);
16483       cp_parser_nested_name_specifier (parser,
16484                                        /*typename_keyword_p=*/false,
16485                                        /*check_dependency_p=*/true,
16486                                        /*type_p=*/false,
16487                                        /*is_declaration=*/false);
16488       /* If we found it, and the next token is a `*', then we are
16489          indeed looking at a pointer-to-member operator.  */
16490       if (!cp_parser_error_occurred (parser)
16491           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16492         {
16493           /* Indicate that the `*' operator was used.  */
16494           code = INDIRECT_REF;
16495
16496           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16497             error_at (token->location, "%qD is a namespace", parser->scope);
16498           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16499             error_at (token->location, "cannot form pointer to member of "
16500                       "non-class %q#T", parser->scope);
16501           else
16502             {
16503               /* The type of which the member is a member is given by the
16504                  current SCOPE.  */
16505               *type = parser->scope;
16506               /* The next name will not be qualified.  */
16507               parser->scope = NULL_TREE;
16508               parser->qualifying_scope = NULL_TREE;
16509               parser->object_scope = NULL_TREE;
16510               /* Look for the optional cv-qualifier-seq.  */
16511               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16512             }
16513         }
16514       /* If that didn't work we don't have a ptr-operator.  */
16515       if (!cp_parser_parse_definitely (parser))
16516         cp_parser_error (parser, "expected ptr-operator");
16517     }
16518
16519   return code;
16520 }
16521
16522 /* Parse an (optional) cv-qualifier-seq.
16523
16524    cv-qualifier-seq:
16525      cv-qualifier cv-qualifier-seq [opt]
16526
16527    cv-qualifier:
16528      const
16529      volatile
16530
16531    GNU Extension:
16532
16533    cv-qualifier:
16534      __restrict__
16535
16536    Returns a bitmask representing the cv-qualifiers.  */
16537
16538 static cp_cv_quals
16539 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16540 {
16541   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16542
16543   while (true)
16544     {
16545       cp_token *token;
16546       cp_cv_quals cv_qualifier;
16547
16548       /* Peek at the next token.  */
16549       token = cp_lexer_peek_token (parser->lexer);
16550       /* See if it's a cv-qualifier.  */
16551       switch (token->keyword)
16552         {
16553         case RID_CONST:
16554           cv_qualifier = TYPE_QUAL_CONST;
16555           break;
16556
16557         case RID_VOLATILE:
16558           cv_qualifier = TYPE_QUAL_VOLATILE;
16559           break;
16560
16561         case RID_RESTRICT:
16562           cv_qualifier = TYPE_QUAL_RESTRICT;
16563           break;
16564
16565         default:
16566           cv_qualifier = TYPE_UNQUALIFIED;
16567           break;
16568         }
16569
16570       if (!cv_qualifier)
16571         break;
16572
16573       if (cv_quals & cv_qualifier)
16574         {
16575           error_at (token->location, "duplicate cv-qualifier");
16576           cp_lexer_purge_token (parser->lexer);
16577         }
16578       else
16579         {
16580           cp_lexer_consume_token (parser->lexer);
16581           cv_quals |= cv_qualifier;
16582         }
16583     }
16584
16585   return cv_quals;
16586 }
16587
16588 /* Parse an (optional) virt-specifier-seq.
16589
16590    virt-specifier-seq:
16591      virt-specifier virt-specifier-seq [opt]
16592
16593    virt-specifier:
16594      override
16595      final
16596
16597    Returns a bitmask representing the virt-specifiers.  */
16598
16599 static cp_virt_specifiers
16600 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16601 {
16602   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16603
16604   while (true)
16605     {
16606       cp_token *token;
16607       cp_virt_specifiers virt_specifier;
16608
16609       /* Peek at the next token.  */
16610       token = cp_lexer_peek_token (parser->lexer);
16611       /* See if it's a virt-specifier-qualifier.  */
16612       if (token->type != CPP_NAME)
16613         break;
16614       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16615         {
16616           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16617           virt_specifier = VIRT_SPEC_OVERRIDE;
16618         }
16619       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16620         {
16621           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16622           virt_specifier = VIRT_SPEC_FINAL;
16623         }
16624       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16625         {
16626           virt_specifier = VIRT_SPEC_FINAL;
16627         }
16628       else
16629         break;
16630
16631       if (virt_specifiers & virt_specifier)
16632         {
16633           error_at (token->location, "duplicate virt-specifier");
16634           cp_lexer_purge_token (parser->lexer);
16635         }
16636       else
16637         {
16638           cp_lexer_consume_token (parser->lexer);
16639           virt_specifiers |= virt_specifier;
16640         }
16641     }
16642   return virt_specifiers;
16643 }
16644
16645 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16646    is in scope even though it isn't real.  */
16647
16648 static void
16649 inject_this_parameter (tree ctype, cp_cv_quals quals)
16650 {
16651   tree this_parm;
16652
16653   if (current_class_ptr)
16654     {
16655       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16656       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16657       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16658           && cp_type_quals (type) == quals)
16659         return;
16660     }
16661
16662   this_parm = build_this_parm (ctype, quals);
16663   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16664   current_class_ptr = NULL_TREE;
16665   current_class_ref
16666     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16667   current_class_ptr = this_parm;
16668 }
16669
16670 /* Parse a late-specified return type, if any.  This is not a separate
16671    non-terminal, but part of a function declarator, which looks like
16672
16673    -> trailing-type-specifier-seq abstract-declarator(opt)
16674
16675    Returns the type indicated by the type-id.
16676
16677    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16678    function.  */
16679
16680 static tree
16681 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16682 {
16683   cp_token *token;
16684   tree type;
16685
16686   /* Peek at the next token.  */
16687   token = cp_lexer_peek_token (parser->lexer);
16688   /* A late-specified return type is indicated by an initial '->'. */
16689   if (token->type != CPP_DEREF)
16690     return NULL_TREE;
16691
16692   /* Consume the ->.  */
16693   cp_lexer_consume_token (parser->lexer);
16694
16695   if (quals >= 0)
16696     {
16697       /* DR 1207: 'this' is in scope in the trailing return type.  */
16698       gcc_assert (current_class_ptr == NULL_TREE);
16699       inject_this_parameter (current_class_type, quals);
16700     }
16701
16702   type = cp_parser_trailing_type_id (parser);
16703
16704   if (quals >= 0)
16705     current_class_ptr = current_class_ref = NULL_TREE;
16706
16707   return type;
16708 }
16709
16710 /* Parse a declarator-id.
16711
16712    declarator-id:
16713      id-expression
16714      :: [opt] nested-name-specifier [opt] type-name
16715
16716    In the `id-expression' case, the value returned is as for
16717    cp_parser_id_expression if the id-expression was an unqualified-id.
16718    If the id-expression was a qualified-id, then a SCOPE_REF is
16719    returned.  The first operand is the scope (either a NAMESPACE_DECL
16720    or TREE_TYPE), but the second is still just a representation of an
16721    unqualified-id.  */
16722
16723 static tree
16724 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16725 {
16726   tree id;
16727   /* The expression must be an id-expression.  Assume that qualified
16728      names are the names of types so that:
16729
16730        template <class T>
16731        int S<T>::R::i = 3;
16732
16733      will work; we must treat `S<T>::R' as the name of a type.
16734      Similarly, assume that qualified names are templates, where
16735      required, so that:
16736
16737        template <class T>
16738        int S<T>::R<T>::i = 3;
16739
16740      will work, too.  */
16741   id = cp_parser_id_expression (parser,
16742                                 /*template_keyword_p=*/false,
16743                                 /*check_dependency_p=*/false,
16744                                 /*template_p=*/NULL,
16745                                 /*declarator_p=*/true,
16746                                 optional_p);
16747   if (id && BASELINK_P (id))
16748     id = BASELINK_FUNCTIONS (id);
16749   return id;
16750 }
16751
16752 /* Parse a type-id.
16753
16754    type-id:
16755      type-specifier-seq abstract-declarator [opt]
16756
16757    Returns the TYPE specified.  */
16758
16759 static tree
16760 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16761                      bool is_trailing_return)
16762 {
16763   cp_decl_specifier_seq type_specifier_seq;
16764   cp_declarator *abstract_declarator;
16765
16766   /* Parse the type-specifier-seq.  */
16767   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16768                                 is_trailing_return,
16769                                 &type_specifier_seq);
16770   if (type_specifier_seq.type == error_mark_node)
16771     return error_mark_node;
16772
16773   /* There might or might not be an abstract declarator.  */
16774   cp_parser_parse_tentatively (parser);
16775   /* Look for the declarator.  */
16776   abstract_declarator
16777     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16778                             /*parenthesized_p=*/NULL,
16779                             /*member_p=*/false);
16780   /* Check to see if there really was a declarator.  */
16781   if (!cp_parser_parse_definitely (parser))
16782     abstract_declarator = NULL;
16783
16784   if (type_specifier_seq.type
16785       && type_uses_auto (type_specifier_seq.type))
16786     {
16787       /* A type-id with type 'auto' is only ok if the abstract declarator
16788          is a function declarator with a late-specified return type.  */
16789       if (abstract_declarator
16790           && abstract_declarator->kind == cdk_function
16791           && abstract_declarator->u.function.late_return_type)
16792         /* OK */;
16793       else
16794         {
16795           error ("invalid use of %<auto%>");
16796           return error_mark_node;
16797         }
16798     }
16799   
16800   return groktypename (&type_specifier_seq, abstract_declarator,
16801                        is_template_arg);
16802 }
16803
16804 static tree cp_parser_type_id (cp_parser *parser)
16805 {
16806   return cp_parser_type_id_1 (parser, false, false);
16807 }
16808
16809 static tree cp_parser_template_type_arg (cp_parser *parser)
16810 {
16811   tree r;
16812   const char *saved_message = parser->type_definition_forbidden_message;
16813   parser->type_definition_forbidden_message
16814     = G_("types may not be defined in template arguments");
16815   r = cp_parser_type_id_1 (parser, true, false);
16816   parser->type_definition_forbidden_message = saved_message;
16817   return r;
16818 }
16819
16820 static tree cp_parser_trailing_type_id (cp_parser *parser)
16821 {
16822   return cp_parser_type_id_1 (parser, false, true);
16823 }
16824
16825 /* Parse a type-specifier-seq.
16826
16827    type-specifier-seq:
16828      type-specifier type-specifier-seq [opt]
16829
16830    GNU extension:
16831
16832    type-specifier-seq:
16833      attributes type-specifier-seq [opt]
16834
16835    If IS_DECLARATION is true, we are at the start of a "condition" or
16836    exception-declaration, so we might be followed by a declarator-id.
16837
16838    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16839    i.e. we've just seen "->".
16840
16841    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16842
16843 static void
16844 cp_parser_type_specifier_seq (cp_parser* parser,
16845                               bool is_declaration,
16846                               bool is_trailing_return,
16847                               cp_decl_specifier_seq *type_specifier_seq)
16848 {
16849   bool seen_type_specifier = false;
16850   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16851   cp_token *start_token = NULL;
16852
16853   /* Clear the TYPE_SPECIFIER_SEQ.  */
16854   clear_decl_specs (type_specifier_seq);
16855
16856   /* In the context of a trailing return type, enum E { } is an
16857      elaborated-type-specifier followed by a function-body, not an
16858      enum-specifier.  */
16859   if (is_trailing_return)
16860     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16861
16862   /* Parse the type-specifiers and attributes.  */
16863   while (true)
16864     {
16865       tree type_specifier;
16866       bool is_cv_qualifier;
16867
16868       /* Check for attributes first.  */
16869       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16870         {
16871           type_specifier_seq->attributes =
16872             chainon (type_specifier_seq->attributes,
16873                      cp_parser_attributes_opt (parser));
16874           continue;
16875         }
16876
16877       /* record the token of the beginning of the type specifier seq,
16878          for error reporting purposes*/
16879      if (!start_token)
16880        start_token = cp_lexer_peek_token (parser->lexer);
16881
16882       /* Look for the type-specifier.  */
16883       type_specifier = cp_parser_type_specifier (parser,
16884                                                  flags,
16885                                                  type_specifier_seq,
16886                                                  /*is_declaration=*/false,
16887                                                  NULL,
16888                                                  &is_cv_qualifier);
16889       if (!type_specifier)
16890         {
16891           /* If the first type-specifier could not be found, this is not a
16892              type-specifier-seq at all.  */
16893           if (!seen_type_specifier)
16894             {
16895               cp_parser_error (parser, "expected type-specifier");
16896               type_specifier_seq->type = error_mark_node;
16897               return;
16898             }
16899           /* If subsequent type-specifiers could not be found, the
16900              type-specifier-seq is complete.  */
16901           break;
16902         }
16903
16904       seen_type_specifier = true;
16905       /* The standard says that a condition can be:
16906
16907             type-specifier-seq declarator = assignment-expression
16908
16909          However, given:
16910
16911            struct S {};
16912            if (int S = ...)
16913
16914          we should treat the "S" as a declarator, not as a
16915          type-specifier.  The standard doesn't say that explicitly for
16916          type-specifier-seq, but it does say that for
16917          decl-specifier-seq in an ordinary declaration.  Perhaps it
16918          would be clearer just to allow a decl-specifier-seq here, and
16919          then add a semantic restriction that if any decl-specifiers
16920          that are not type-specifiers appear, the program is invalid.  */
16921       if (is_declaration && !is_cv_qualifier)
16922         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16923     }
16924
16925   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16926 }
16927
16928 /* Parse a parameter-declaration-clause.
16929
16930    parameter-declaration-clause:
16931      parameter-declaration-list [opt] ... [opt]
16932      parameter-declaration-list , ...
16933
16934    Returns a representation for the parameter declarations.  A return
16935    value of NULL indicates a parameter-declaration-clause consisting
16936    only of an ellipsis.  */
16937
16938 static tree
16939 cp_parser_parameter_declaration_clause (cp_parser* parser)
16940 {
16941   tree parameters;
16942   cp_token *token;
16943   bool ellipsis_p;
16944   bool is_error;
16945
16946   /* Peek at the next token.  */
16947   token = cp_lexer_peek_token (parser->lexer);
16948   /* Check for trivial parameter-declaration-clauses.  */
16949   if (token->type == CPP_ELLIPSIS)
16950     {
16951       /* Consume the `...' token.  */
16952       cp_lexer_consume_token (parser->lexer);
16953       return NULL_TREE;
16954     }
16955   else if (token->type == CPP_CLOSE_PAREN)
16956     /* There are no parameters.  */
16957     {
16958 #ifndef NO_IMPLICIT_EXTERN_C
16959       if (in_system_header && current_class_type == NULL
16960           && current_lang_name == lang_name_c)
16961         return NULL_TREE;
16962       else
16963 #endif
16964         return void_list_node;
16965     }
16966   /* Check for `(void)', too, which is a special case.  */
16967   else if (token->keyword == RID_VOID
16968            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16969                == CPP_CLOSE_PAREN))
16970     {
16971       /* Consume the `void' token.  */
16972       cp_lexer_consume_token (parser->lexer);
16973       /* There are no parameters.  */
16974       return void_list_node;
16975     }
16976
16977   /* Parse the parameter-declaration-list.  */
16978   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16979   /* If a parse error occurred while parsing the
16980      parameter-declaration-list, then the entire
16981      parameter-declaration-clause is erroneous.  */
16982   if (is_error)
16983     return NULL;
16984
16985   /* Peek at the next token.  */
16986   token = cp_lexer_peek_token (parser->lexer);
16987   /* If it's a `,', the clause should terminate with an ellipsis.  */
16988   if (token->type == CPP_COMMA)
16989     {
16990       /* Consume the `,'.  */
16991       cp_lexer_consume_token (parser->lexer);
16992       /* Expect an ellipsis.  */
16993       ellipsis_p
16994         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16995     }
16996   /* It might also be `...' if the optional trailing `,' was
16997      omitted.  */
16998   else if (token->type == CPP_ELLIPSIS)
16999     {
17000       /* Consume the `...' token.  */
17001       cp_lexer_consume_token (parser->lexer);
17002       /* And remember that we saw it.  */
17003       ellipsis_p = true;
17004     }
17005   else
17006     ellipsis_p = false;
17007
17008   /* Finish the parameter list.  */
17009   if (!ellipsis_p)
17010     parameters = chainon (parameters, void_list_node);
17011
17012   return parameters;
17013 }
17014
17015 /* Parse a parameter-declaration-list.
17016
17017    parameter-declaration-list:
17018      parameter-declaration
17019      parameter-declaration-list , parameter-declaration
17020
17021    Returns a representation of the parameter-declaration-list, as for
17022    cp_parser_parameter_declaration_clause.  However, the
17023    `void_list_node' is never appended to the list.  Upon return,
17024    *IS_ERROR will be true iff an error occurred.  */
17025
17026 static tree
17027 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17028 {
17029   tree parameters = NULL_TREE;
17030   tree *tail = &parameters; 
17031   bool saved_in_unbraced_linkage_specification_p;
17032   int index = 0;
17033
17034   /* Assume all will go well.  */
17035   *is_error = false;
17036   /* The special considerations that apply to a function within an
17037      unbraced linkage specifications do not apply to the parameters
17038      to the function.  */
17039   saved_in_unbraced_linkage_specification_p 
17040     = parser->in_unbraced_linkage_specification_p;
17041   parser->in_unbraced_linkage_specification_p = false;
17042
17043   /* Look for more parameters.  */
17044   while (true)
17045     {
17046       cp_parameter_declarator *parameter;
17047       tree decl = error_mark_node;
17048       bool parenthesized_p = false;
17049       /* Parse the parameter.  */
17050       parameter
17051         = cp_parser_parameter_declaration (parser,
17052                                            /*template_parm_p=*/false,
17053                                            &parenthesized_p);
17054
17055       /* We don't know yet if the enclosing context is deprecated, so wait
17056          and warn in grokparms if appropriate.  */
17057       deprecated_state = DEPRECATED_SUPPRESS;
17058
17059       if (parameter)
17060         decl = grokdeclarator (parameter->declarator,
17061                                &parameter->decl_specifiers,
17062                                PARM,
17063                                parameter->default_argument != NULL_TREE,
17064                                &parameter->decl_specifiers.attributes);
17065
17066       deprecated_state = DEPRECATED_NORMAL;
17067
17068       /* If a parse error occurred parsing the parameter declaration,
17069          then the entire parameter-declaration-list is erroneous.  */
17070       if (decl == error_mark_node)
17071         {
17072           *is_error = true;
17073           parameters = error_mark_node;
17074           break;
17075         }
17076
17077       if (parameter->decl_specifiers.attributes)
17078         cplus_decl_attributes (&decl,
17079                                parameter->decl_specifiers.attributes,
17080                                0);
17081       if (DECL_NAME (decl))
17082         decl = pushdecl (decl);
17083
17084       if (decl != error_mark_node)
17085         {
17086           retrofit_lang_decl (decl);
17087           DECL_PARM_INDEX (decl) = ++index;
17088           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17089         }
17090
17091       /* Add the new parameter to the list.  */
17092       *tail = build_tree_list (parameter->default_argument, decl);
17093       tail = &TREE_CHAIN (*tail);
17094
17095       /* Peek at the next token.  */
17096       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17097           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17098           /* These are for Objective-C++ */
17099           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17100           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17101         /* The parameter-declaration-list is complete.  */
17102         break;
17103       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17104         {
17105           cp_token *token;
17106
17107           /* Peek at the next token.  */
17108           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17109           /* If it's an ellipsis, then the list is complete.  */
17110           if (token->type == CPP_ELLIPSIS)
17111             break;
17112           /* Otherwise, there must be more parameters.  Consume the
17113              `,'.  */
17114           cp_lexer_consume_token (parser->lexer);
17115           /* When parsing something like:
17116
17117                 int i(float f, double d)
17118
17119              we can tell after seeing the declaration for "f" that we
17120              are not looking at an initialization of a variable "i",
17121              but rather at the declaration of a function "i".
17122
17123              Due to the fact that the parsing of template arguments
17124              (as specified to a template-id) requires backtracking we
17125              cannot use this technique when inside a template argument
17126              list.  */
17127           if (!parser->in_template_argument_list_p
17128               && !parser->in_type_id_in_expr_p
17129               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17130               /* However, a parameter-declaration of the form
17131                  "foat(f)" (which is a valid declaration of a
17132                  parameter "f") can also be interpreted as an
17133                  expression (the conversion of "f" to "float").  */
17134               && !parenthesized_p)
17135             cp_parser_commit_to_tentative_parse (parser);
17136         }
17137       else
17138         {
17139           cp_parser_error (parser, "expected %<,%> or %<...%>");
17140           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17141             cp_parser_skip_to_closing_parenthesis (parser,
17142                                                    /*recovering=*/true,
17143                                                    /*or_comma=*/false,
17144                                                    /*consume_paren=*/false);
17145           break;
17146         }
17147     }
17148
17149   parser->in_unbraced_linkage_specification_p
17150     = saved_in_unbraced_linkage_specification_p;
17151
17152   return parameters;
17153 }
17154
17155 /* Parse a parameter declaration.
17156
17157    parameter-declaration:
17158      decl-specifier-seq ... [opt] declarator
17159      decl-specifier-seq declarator = assignment-expression
17160      decl-specifier-seq ... [opt] abstract-declarator [opt]
17161      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17162
17163    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17164    declares a template parameter.  (In that case, a non-nested `>'
17165    token encountered during the parsing of the assignment-expression
17166    is not interpreted as a greater-than operator.)
17167
17168    Returns a representation of the parameter, or NULL if an error
17169    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17170    true iff the declarator is of the form "(p)".  */
17171
17172 static cp_parameter_declarator *
17173 cp_parser_parameter_declaration (cp_parser *parser,
17174                                  bool template_parm_p,
17175                                  bool *parenthesized_p)
17176 {
17177   int declares_class_or_enum;
17178   cp_decl_specifier_seq decl_specifiers;
17179   cp_declarator *declarator;
17180   tree default_argument;
17181   cp_token *token = NULL, *declarator_token_start = NULL;
17182   const char *saved_message;
17183
17184   /* In a template parameter, `>' is not an operator.
17185
17186      [temp.param]
17187
17188      When parsing a default template-argument for a non-type
17189      template-parameter, the first non-nested `>' is taken as the end
17190      of the template parameter-list rather than a greater-than
17191      operator.  */
17192
17193   /* Type definitions may not appear in parameter types.  */
17194   saved_message = parser->type_definition_forbidden_message;
17195   parser->type_definition_forbidden_message
17196     = G_("types may not be defined in parameter types");
17197
17198   /* Parse the declaration-specifiers.  */
17199   cp_parser_decl_specifier_seq (parser,
17200                                 CP_PARSER_FLAGS_NONE,
17201                                 &decl_specifiers,
17202                                 &declares_class_or_enum);
17203
17204   /* Complain about missing 'typename' or other invalid type names.  */
17205   if (!decl_specifiers.any_type_specifiers_p)
17206     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17207
17208   /* If an error occurred, there's no reason to attempt to parse the
17209      rest of the declaration.  */
17210   if (cp_parser_error_occurred (parser))
17211     {
17212       parser->type_definition_forbidden_message = saved_message;
17213       return NULL;
17214     }
17215
17216   /* Peek at the next token.  */
17217   token = cp_lexer_peek_token (parser->lexer);
17218
17219   /* If the next token is a `)', `,', `=', `>', or `...', then there
17220      is no declarator. However, when variadic templates are enabled,
17221      there may be a declarator following `...'.  */
17222   if (token->type == CPP_CLOSE_PAREN
17223       || token->type == CPP_COMMA
17224       || token->type == CPP_EQ
17225       || token->type == CPP_GREATER)
17226     {
17227       declarator = NULL;
17228       if (parenthesized_p)
17229         *parenthesized_p = false;
17230     }
17231   /* Otherwise, there should be a declarator.  */
17232   else
17233     {
17234       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17235       parser->default_arg_ok_p = false;
17236
17237       /* After seeing a decl-specifier-seq, if the next token is not a
17238          "(", there is no possibility that the code is a valid
17239          expression.  Therefore, if parsing tentatively, we commit at
17240          this point.  */
17241       if (!parser->in_template_argument_list_p
17242           /* In an expression context, having seen:
17243
17244                (int((char ...
17245
17246              we cannot be sure whether we are looking at a
17247              function-type (taking a "char" as a parameter) or a cast
17248              of some object of type "char" to "int".  */
17249           && !parser->in_type_id_in_expr_p
17250           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17251           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17252           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17253         cp_parser_commit_to_tentative_parse (parser);
17254       /* Parse the declarator.  */
17255       declarator_token_start = token;
17256       declarator = cp_parser_declarator (parser,
17257                                          CP_PARSER_DECLARATOR_EITHER,
17258                                          /*ctor_dtor_or_conv_p=*/NULL,
17259                                          parenthesized_p,
17260                                          /*member_p=*/false);
17261       parser->default_arg_ok_p = saved_default_arg_ok_p;
17262       /* After the declarator, allow more attributes.  */
17263       decl_specifiers.attributes
17264         = chainon (decl_specifiers.attributes,
17265                    cp_parser_attributes_opt (parser));
17266     }
17267
17268   /* If the next token is an ellipsis, and we have not seen a
17269      declarator name, and the type of the declarator contains parameter
17270      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17271      a parameter pack expansion expression. Otherwise, leave the
17272      ellipsis for a C-style variadic function. */
17273   token = cp_lexer_peek_token (parser->lexer);
17274   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17275     {
17276       tree type = decl_specifiers.type;
17277
17278       if (type && DECL_P (type))
17279         type = TREE_TYPE (type);
17280
17281       if (type
17282           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17283           && declarator_can_be_parameter_pack (declarator)
17284           && (!declarator || !declarator->parameter_pack_p)
17285           && uses_parameter_packs (type))
17286         {
17287           /* Consume the `...'. */
17288           cp_lexer_consume_token (parser->lexer);
17289           maybe_warn_variadic_templates ();
17290           
17291           /* Build a pack expansion type */
17292           if (declarator)
17293             declarator->parameter_pack_p = true;
17294           else
17295             decl_specifiers.type = make_pack_expansion (type);
17296         }
17297     }
17298
17299   /* The restriction on defining new types applies only to the type
17300      of the parameter, not to the default argument.  */
17301   parser->type_definition_forbidden_message = saved_message;
17302
17303   /* If the next token is `=', then process a default argument.  */
17304   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17305     {
17306       token = cp_lexer_peek_token (parser->lexer);
17307       /* If we are defining a class, then the tokens that make up the
17308          default argument must be saved and processed later.  */
17309       if (!template_parm_p && at_class_scope_p ()
17310           && TYPE_BEING_DEFINED (current_class_type)
17311           && !LAMBDA_TYPE_P (current_class_type))
17312         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17313       /* Outside of a class definition, we can just parse the
17314          assignment-expression.  */
17315       else
17316         default_argument
17317           = cp_parser_default_argument (parser, template_parm_p);
17318
17319       if (!parser->default_arg_ok_p)
17320         {
17321           if (flag_permissive)
17322             warning (0, "deprecated use of default argument for parameter of non-function");
17323           else
17324             {
17325               error_at (token->location,
17326                         "default arguments are only "
17327                         "permitted for function parameters");
17328               default_argument = NULL_TREE;
17329             }
17330         }
17331       else if ((declarator && declarator->parameter_pack_p)
17332                || (decl_specifiers.type
17333                    && PACK_EXPANSION_P (decl_specifiers.type)))
17334         {
17335           /* Find the name of the parameter pack.  */     
17336           cp_declarator *id_declarator = declarator;
17337           while (id_declarator && id_declarator->kind != cdk_id)
17338             id_declarator = id_declarator->declarator;
17339           
17340           if (id_declarator && id_declarator->kind == cdk_id)
17341             error_at (declarator_token_start->location,
17342                       template_parm_p
17343                       ? G_("template parameter pack %qD "
17344                            "cannot have a default argument")
17345                       : G_("parameter pack %qD cannot have "
17346                            "a default argument"),
17347                       id_declarator->u.id.unqualified_name);
17348           else
17349             error_at (declarator_token_start->location,
17350                       template_parm_p
17351                       ? G_("template parameter pack cannot have "
17352                            "a default argument")
17353                       : G_("parameter pack cannot have a "
17354                            "default argument"));
17355
17356           default_argument = NULL_TREE;
17357         }
17358     }
17359   else
17360     default_argument = NULL_TREE;
17361
17362   return make_parameter_declarator (&decl_specifiers,
17363                                     declarator,
17364                                     default_argument);
17365 }
17366
17367 /* Parse a default argument and return it.
17368
17369    TEMPLATE_PARM_P is true if this is a default argument for a
17370    non-type template parameter.  */
17371 static tree
17372 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17373 {
17374   tree default_argument = NULL_TREE;
17375   bool saved_greater_than_is_operator_p;
17376   bool saved_local_variables_forbidden_p;
17377   bool non_constant_p, is_direct_init;
17378
17379   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17380      set correctly.  */
17381   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17382   parser->greater_than_is_operator_p = !template_parm_p;
17383   /* Local variable names (and the `this' keyword) may not
17384      appear in a default argument.  */
17385   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17386   parser->local_variables_forbidden_p = true;
17387   /* Parse the assignment-expression.  */
17388   if (template_parm_p)
17389     push_deferring_access_checks (dk_no_deferred);
17390   default_argument
17391     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17392   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17393     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17394   if (template_parm_p)
17395     pop_deferring_access_checks ();
17396   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17397   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17398
17399   return default_argument;
17400 }
17401
17402 /* Parse a function-body.
17403
17404    function-body:
17405      compound_statement  */
17406
17407 static void
17408 cp_parser_function_body (cp_parser *parser)
17409 {
17410   cp_parser_compound_statement (parser, NULL, false, true);
17411 }
17412
17413 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17414    true if a ctor-initializer was present.  */
17415
17416 static bool
17417 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17418 {
17419   tree body, list;
17420   bool ctor_initializer_p;
17421   const bool check_body_p =
17422      DECL_CONSTRUCTOR_P (current_function_decl)
17423      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17424   tree last = NULL;
17425
17426   /* Begin the function body.  */
17427   body = begin_function_body ();
17428   /* Parse the optional ctor-initializer.  */
17429   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17430
17431   /* If we're parsing a constexpr constructor definition, we need
17432      to check that the constructor body is indeed empty.  However,
17433      before we get to cp_parser_function_body lot of junk has been
17434      generated, so we can't just check that we have an empty block.
17435      Rather we take a snapshot of the outermost block, and check whether
17436      cp_parser_function_body changed its state.  */
17437   if (check_body_p)
17438     {
17439       list = cur_stmt_list;
17440       if (STATEMENT_LIST_TAIL (list))
17441         last = STATEMENT_LIST_TAIL (list)->stmt;
17442     }
17443   /* Parse the function-body.  */
17444   cp_parser_function_body (parser);
17445   if (check_body_p)
17446     check_constexpr_ctor_body (last, list);
17447   /* Finish the function body.  */
17448   finish_function_body (body);
17449
17450   return ctor_initializer_p;
17451 }
17452
17453 /* Parse an initializer.
17454
17455    initializer:
17456      = initializer-clause
17457      ( expression-list )
17458
17459    Returns an expression representing the initializer.  If no
17460    initializer is present, NULL_TREE is returned.
17461
17462    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17463    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17464    set to TRUE if there is no initializer present.  If there is an
17465    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17466    is set to true; otherwise it is set to false.  */
17467
17468 static tree
17469 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17470                        bool* non_constant_p)
17471 {
17472   cp_token *token;
17473   tree init;
17474
17475   /* Peek at the next token.  */
17476   token = cp_lexer_peek_token (parser->lexer);
17477
17478   /* Let our caller know whether or not this initializer was
17479      parenthesized.  */
17480   *is_direct_init = (token->type != CPP_EQ);
17481   /* Assume that the initializer is constant.  */
17482   *non_constant_p = false;
17483
17484   if (token->type == CPP_EQ)
17485     {
17486       /* Consume the `='.  */
17487       cp_lexer_consume_token (parser->lexer);
17488       /* Parse the initializer-clause.  */
17489       init = cp_parser_initializer_clause (parser, non_constant_p);
17490     }
17491   else if (token->type == CPP_OPEN_PAREN)
17492     {
17493       VEC(tree,gc) *vec;
17494       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17495                                                      /*cast_p=*/false,
17496                                                      /*allow_expansion_p=*/true,
17497                                                      non_constant_p);
17498       if (vec == NULL)
17499         return error_mark_node;
17500       init = build_tree_list_vec (vec);
17501       release_tree_vector (vec);
17502     }
17503   else if (token->type == CPP_OPEN_BRACE)
17504     {
17505       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17506       init = cp_parser_braced_list (parser, non_constant_p);
17507       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17508     }
17509   else
17510     {
17511       /* Anything else is an error.  */
17512       cp_parser_error (parser, "expected initializer");
17513       init = error_mark_node;
17514     }
17515
17516   return init;
17517 }
17518
17519 /* Parse an initializer-clause.
17520
17521    initializer-clause:
17522      assignment-expression
17523      braced-init-list
17524
17525    Returns an expression representing the initializer.
17526
17527    If the `assignment-expression' production is used the value
17528    returned is simply a representation for the expression.
17529
17530    Otherwise, calls cp_parser_braced_list.  */
17531
17532 static tree
17533 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17534 {
17535   tree initializer;
17536
17537   /* Assume the expression is constant.  */
17538   *non_constant_p = false;
17539
17540   /* If it is not a `{', then we are looking at an
17541      assignment-expression.  */
17542   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17543     {
17544       initializer
17545         = cp_parser_constant_expression (parser,
17546                                         /*allow_non_constant_p=*/true,
17547                                         non_constant_p);
17548     }
17549   else
17550     initializer = cp_parser_braced_list (parser, non_constant_p);
17551
17552   return initializer;
17553 }
17554
17555 /* Parse a brace-enclosed initializer list.
17556
17557    braced-init-list:
17558      { initializer-list , [opt] }
17559      { }
17560
17561    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17562    the elements of the initializer-list (or NULL, if the last
17563    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17564    NULL_TREE.  There is no way to detect whether or not the optional
17565    trailing `,' was provided.  NON_CONSTANT_P is as for
17566    cp_parser_initializer.  */     
17567
17568 static tree
17569 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17570 {
17571   tree initializer;
17572
17573   /* Consume the `{' token.  */
17574   cp_lexer_consume_token (parser->lexer);
17575   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17576   initializer = make_node (CONSTRUCTOR);
17577   /* If it's not a `}', then there is a non-trivial initializer.  */
17578   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17579     {
17580       /* Parse the initializer list.  */
17581       CONSTRUCTOR_ELTS (initializer)
17582         = cp_parser_initializer_list (parser, non_constant_p);
17583       /* A trailing `,' token is allowed.  */
17584       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17585         cp_lexer_consume_token (parser->lexer);
17586     }
17587   /* Now, there should be a trailing `}'.  */
17588   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17589   TREE_TYPE (initializer) = init_list_type_node;
17590   return initializer;
17591 }
17592
17593 /* Parse an initializer-list.
17594
17595    initializer-list:
17596      initializer-clause ... [opt]
17597      initializer-list , initializer-clause ... [opt]
17598
17599    GNU Extension:
17600
17601    initializer-list:
17602      designation initializer-clause ...[opt]
17603      initializer-list , designation initializer-clause ...[opt]
17604
17605    designation:
17606      . identifier =
17607      identifier :
17608      [ constant-expression ] =
17609
17610    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17611    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17612    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17613    as for cp_parser_initializer.  */
17614
17615 static VEC(constructor_elt,gc) *
17616 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17617 {
17618   VEC(constructor_elt,gc) *v = NULL;
17619
17620   /* Assume all of the expressions are constant.  */
17621   *non_constant_p = false;
17622
17623   /* Parse the rest of the list.  */
17624   while (true)
17625     {
17626       cp_token *token;
17627       tree designator;
17628       tree initializer;
17629       bool clause_non_constant_p;
17630
17631       /* If the next token is an identifier and the following one is a
17632          colon, we are looking at the GNU designated-initializer
17633          syntax.  */
17634       if (cp_parser_allow_gnu_extensions_p (parser)
17635           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17636           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17637         {
17638           /* Warn the user that they are using an extension.  */
17639           pedwarn (input_location, OPT_Wpedantic, 
17640                    "ISO C++ does not allow designated initializers");
17641           /* Consume the identifier.  */
17642           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17643           /* Consume the `:'.  */
17644           cp_lexer_consume_token (parser->lexer);
17645         }
17646       /* Also handle the C99 syntax, '. id ='.  */
17647       else if (cp_parser_allow_gnu_extensions_p (parser)
17648                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17649                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17650                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17651         {
17652           /* Warn the user that they are using an extension.  */
17653           pedwarn (input_location, OPT_Wpedantic,
17654                    "ISO C++ does not allow C99 designated initializers");
17655           /* Consume the `.'.  */
17656           cp_lexer_consume_token (parser->lexer);
17657           /* Consume the identifier.  */
17658           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17659           /* Consume the `='.  */
17660           cp_lexer_consume_token (parser->lexer);
17661         }
17662       /* Also handle C99 array designators, '[ const ] ='.  */
17663       else if (cp_parser_allow_gnu_extensions_p (parser)
17664                && !c_dialect_objc ()
17665                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17666         {
17667           /* In C++11, [ could start a lambda-introducer.  */
17668           cp_parser_parse_tentatively (parser);
17669           cp_lexer_consume_token (parser->lexer);
17670           designator = cp_parser_constant_expression (parser, false, NULL);
17671           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17672           cp_parser_require (parser, CPP_EQ, RT_EQ);
17673           if (!cp_parser_parse_definitely (parser))
17674             designator = NULL_TREE;
17675         }
17676       else
17677         designator = NULL_TREE;
17678
17679       /* Parse the initializer.  */
17680       initializer = cp_parser_initializer_clause (parser,
17681                                                   &clause_non_constant_p);
17682       /* If any clause is non-constant, so is the entire initializer.  */
17683       if (clause_non_constant_p)
17684         *non_constant_p = true;
17685
17686       /* If we have an ellipsis, this is an initializer pack
17687          expansion.  */
17688       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17689         {
17690           /* Consume the `...'.  */
17691           cp_lexer_consume_token (parser->lexer);
17692
17693           /* Turn the initializer into an initializer expansion.  */
17694           initializer = make_pack_expansion (initializer);
17695         }
17696
17697       /* Add it to the vector.  */
17698       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17699
17700       /* If the next token is not a comma, we have reached the end of
17701          the list.  */
17702       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17703         break;
17704
17705       /* Peek at the next token.  */
17706       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17707       /* If the next token is a `}', then we're still done.  An
17708          initializer-clause can have a trailing `,' after the
17709          initializer-list and before the closing `}'.  */
17710       if (token->type == CPP_CLOSE_BRACE)
17711         break;
17712
17713       /* Consume the `,' token.  */
17714       cp_lexer_consume_token (parser->lexer);
17715     }
17716
17717   return v;
17718 }
17719
17720 /* Classes [gram.class] */
17721
17722 /* Parse a class-name.
17723
17724    class-name:
17725      identifier
17726      template-id
17727
17728    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17729    to indicate that names looked up in dependent types should be
17730    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17731    keyword has been used to indicate that the name that appears next
17732    is a template.  TAG_TYPE indicates the explicit tag given before
17733    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17734    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17735    is the class being defined in a class-head.
17736
17737    Returns the TYPE_DECL representing the class.  */
17738
17739 static tree
17740 cp_parser_class_name (cp_parser *parser,
17741                       bool typename_keyword_p,
17742                       bool template_keyword_p,
17743                       enum tag_types tag_type,
17744                       bool check_dependency_p,
17745                       bool class_head_p,
17746                       bool is_declaration)
17747 {
17748   tree decl;
17749   tree scope;
17750   bool typename_p;
17751   cp_token *token;
17752   tree identifier = NULL_TREE;
17753
17754   /* All class-names start with an identifier.  */
17755   token = cp_lexer_peek_token (parser->lexer);
17756   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17757     {
17758       cp_parser_error (parser, "expected class-name");
17759       return error_mark_node;
17760     }
17761
17762   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17763      to a template-id, so we save it here.  */
17764   scope = parser->scope;
17765   if (scope == error_mark_node)
17766     return error_mark_node;
17767
17768   /* Any name names a type if we're following the `typename' keyword
17769      in a qualified name where the enclosing scope is type-dependent.  */
17770   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17771                 && dependent_type_p (scope));
17772   /* Handle the common case (an identifier, but not a template-id)
17773      efficiently.  */
17774   if (token->type == CPP_NAME
17775       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17776     {
17777       cp_token *identifier_token;
17778       bool ambiguous_p;
17779
17780       /* Look for the identifier.  */
17781       identifier_token = cp_lexer_peek_token (parser->lexer);
17782       ambiguous_p = identifier_token->ambiguous_p;
17783       identifier = cp_parser_identifier (parser);
17784       /* If the next token isn't an identifier, we are certainly not
17785          looking at a class-name.  */
17786       if (identifier == error_mark_node)
17787         decl = error_mark_node;
17788       /* If we know this is a type-name, there's no need to look it
17789          up.  */
17790       else if (typename_p)
17791         decl = identifier;
17792       else
17793         {
17794           tree ambiguous_decls;
17795           /* If we already know that this lookup is ambiguous, then
17796              we've already issued an error message; there's no reason
17797              to check again.  */
17798           if (ambiguous_p)
17799             {
17800               cp_parser_simulate_error (parser);
17801               return error_mark_node;
17802             }
17803           /* If the next token is a `::', then the name must be a type
17804              name.
17805
17806              [basic.lookup.qual]
17807
17808              During the lookup for a name preceding the :: scope
17809              resolution operator, object, function, and enumerator
17810              names are ignored.  */
17811           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17812             tag_type = typename_type;
17813           /* Look up the name.  */
17814           decl = cp_parser_lookup_name (parser, identifier,
17815                                         tag_type,
17816                                         /*is_template=*/false,
17817                                         /*is_namespace=*/false,
17818                                         check_dependency_p,
17819                                         &ambiguous_decls,
17820                                         identifier_token->location);
17821           if (ambiguous_decls)
17822             {
17823               if (cp_parser_parsing_tentatively (parser))
17824                 cp_parser_simulate_error (parser);
17825               return error_mark_node;
17826             }
17827         }
17828     }
17829   else
17830     {
17831       /* Try a template-id.  */
17832       decl = cp_parser_template_id (parser, template_keyword_p,
17833                                     check_dependency_p,
17834                                     is_declaration);
17835       if (decl == error_mark_node)
17836         return error_mark_node;
17837     }
17838
17839   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17840
17841   /* If this is a typename, create a TYPENAME_TYPE.  */
17842   if (typename_p && decl != error_mark_node)
17843     {
17844       decl = make_typename_type (scope, decl, typename_type,
17845                                  /*complain=*/tf_error);
17846       if (decl != error_mark_node)
17847         decl = TYPE_NAME (decl);
17848     }
17849
17850   decl = strip_using_decl (decl);
17851
17852   /* Check to see that it is really the name of a class.  */
17853   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17854       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17855       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17856     /* Situations like this:
17857
17858          template <typename T> struct A {
17859            typename T::template X<int>::I i;
17860          };
17861
17862        are problematic.  Is `T::template X<int>' a class-name?  The
17863        standard does not seem to be definitive, but there is no other
17864        valid interpretation of the following `::'.  Therefore, those
17865        names are considered class-names.  */
17866     {
17867       decl = make_typename_type (scope, decl, tag_type, tf_error);
17868       if (decl != error_mark_node)
17869         decl = TYPE_NAME (decl);
17870     }
17871   else if (TREE_CODE (decl) != TYPE_DECL
17872            || TREE_TYPE (decl) == error_mark_node
17873            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17874            /* In Objective-C 2.0, a classname followed by '.' starts a
17875               dot-syntax expression, and it's not a type-name.  */
17876            || (c_dialect_objc ()
17877                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17878                && objc_is_class_name (decl)))
17879     decl = error_mark_node;
17880
17881   if (decl == error_mark_node)
17882     cp_parser_error (parser, "expected class-name");
17883   else if (identifier && !parser->scope)
17884     maybe_note_name_used_in_class (identifier, decl);
17885
17886   return decl;
17887 }
17888
17889 /* Parse a class-specifier.
17890
17891    class-specifier:
17892      class-head { member-specification [opt] }
17893
17894    Returns the TREE_TYPE representing the class.  */
17895
17896 static tree
17897 cp_parser_class_specifier_1 (cp_parser* parser)
17898 {
17899   tree type;
17900   tree attributes = NULL_TREE;
17901   bool nested_name_specifier_p;
17902   unsigned saved_num_template_parameter_lists;
17903   bool saved_in_function_body;
17904   unsigned char in_statement;
17905   bool in_switch_statement_p;
17906   bool saved_in_unbraced_linkage_specification_p;
17907   tree old_scope = NULL_TREE;
17908   tree scope = NULL_TREE;
17909   tree bases;
17910   cp_token *closing_brace;
17911
17912   push_deferring_access_checks (dk_no_deferred);
17913
17914   /* Parse the class-head.  */
17915   type = cp_parser_class_head (parser,
17916                                &nested_name_specifier_p,
17917                                &attributes,
17918                                &bases);
17919   /* If the class-head was a semantic disaster, skip the entire body
17920      of the class.  */
17921   if (!type)
17922     {
17923       cp_parser_skip_to_end_of_block_or_statement (parser);
17924       pop_deferring_access_checks ();
17925       return error_mark_node;
17926     }
17927
17928   /* Look for the `{'.  */
17929   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17930     {
17931       pop_deferring_access_checks ();
17932       return error_mark_node;
17933     }
17934
17935   /* Process the base classes. If they're invalid, skip the 
17936      entire class body.  */
17937   if (!xref_basetypes (type, bases))
17938     {
17939       /* Consuming the closing brace yields better error messages
17940          later on.  */
17941       if (cp_parser_skip_to_closing_brace (parser))
17942         cp_lexer_consume_token (parser->lexer);
17943       pop_deferring_access_checks ();
17944       return error_mark_node;
17945     }
17946
17947   /* Issue an error message if type-definitions are forbidden here.  */
17948   cp_parser_check_type_definition (parser);
17949   /* Remember that we are defining one more class.  */
17950   ++parser->num_classes_being_defined;
17951   /* Inside the class, surrounding template-parameter-lists do not
17952      apply.  */
17953   saved_num_template_parameter_lists
17954     = parser->num_template_parameter_lists;
17955   parser->num_template_parameter_lists = 0;
17956   /* We are not in a function body.  */
17957   saved_in_function_body = parser->in_function_body;
17958   parser->in_function_body = false;
17959   /* Or in a loop.  */
17960   in_statement = parser->in_statement;
17961   parser->in_statement = 0;
17962   /* Or in a switch.  */
17963   in_switch_statement_p = parser->in_switch_statement_p;
17964   parser->in_switch_statement_p = false;
17965   /* We are not immediately inside an extern "lang" block.  */
17966   saved_in_unbraced_linkage_specification_p
17967     = parser->in_unbraced_linkage_specification_p;
17968   parser->in_unbraced_linkage_specification_p = false;
17969
17970   /* Start the class.  */
17971   if (nested_name_specifier_p)
17972     {
17973       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17974       old_scope = push_inner_scope (scope);
17975     }
17976   type = begin_class_definition (type, attributes);
17977
17978   if (type == error_mark_node)
17979     /* If the type is erroneous, skip the entire body of the class.  */
17980     cp_parser_skip_to_closing_brace (parser);
17981   else
17982     /* Parse the member-specification.  */
17983     cp_parser_member_specification_opt (parser);
17984
17985   /* Look for the trailing `}'.  */
17986   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17987   /* Look for trailing attributes to apply to this class.  */
17988   if (cp_parser_allow_gnu_extensions_p (parser))
17989     attributes = cp_parser_attributes_opt (parser);
17990   if (type != error_mark_node)
17991     type = finish_struct (type, attributes);
17992   if (nested_name_specifier_p)
17993     pop_inner_scope (old_scope, scope);
17994
17995   /* We've finished a type definition.  Check for the common syntax
17996      error of forgetting a semicolon after the definition.  We need to
17997      be careful, as we can't just check for not-a-semicolon and be done
17998      with it; the user might have typed:
17999
18000      class X { } c = ...;
18001      class X { } *p = ...;
18002
18003      and so forth.  Instead, enumerate all the possible tokens that
18004      might follow this production; if we don't see one of them, then
18005      complain and silently insert the semicolon.  */
18006   {
18007     cp_token *token = cp_lexer_peek_token (parser->lexer);
18008     bool want_semicolon = true;
18009
18010     switch (token->type)
18011       {
18012       case CPP_NAME:
18013       case CPP_SEMICOLON:
18014       case CPP_MULT:
18015       case CPP_AND:
18016       case CPP_OPEN_PAREN:
18017       case CPP_CLOSE_PAREN:
18018       case CPP_COMMA:
18019         want_semicolon = false;
18020         break;
18021
18022         /* While it's legal for type qualifiers and storage class
18023            specifiers to follow type definitions in the grammar, only
18024            compiler testsuites contain code like that.  Assume that if
18025            we see such code, then what we're really seeing is a case
18026            like:
18027
18028            class X { }
18029            const <type> var = ...;
18030
18031            or
18032
18033            class Y { }
18034            static <type> func (...) ...
18035
18036            i.e. the qualifier or specifier applies to the next
18037            declaration.  To do so, however, we need to look ahead one
18038            more token to see if *that* token is a type specifier.
18039
18040            This code could be improved to handle:
18041
18042            class Z { }
18043            static const <type> var = ...;  */
18044       case CPP_KEYWORD:
18045         if (keyword_is_decl_specifier (token->keyword))
18046           {
18047             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18048
18049             /* Handling user-defined types here would be nice, but very
18050                tricky.  */
18051             want_semicolon
18052               = (lookahead->type == CPP_KEYWORD
18053                  && keyword_begins_type_specifier (lookahead->keyword));
18054           }
18055         break;
18056       default:
18057         break;
18058       }
18059
18060     /* If we don't have a type, then something is very wrong and we
18061        shouldn't try to do anything clever.  Likewise for not seeing the
18062        closing brace.  */
18063     if (closing_brace && TYPE_P (type) && want_semicolon)
18064       {
18065         cp_token_position prev
18066           = cp_lexer_previous_token_position (parser->lexer);
18067         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18068         location_t loc = prev_token->location;
18069
18070         if (CLASSTYPE_DECLARED_CLASS (type))
18071           error_at (loc, "expected %<;%> after class definition");
18072         else if (TREE_CODE (type) == RECORD_TYPE)
18073           error_at (loc, "expected %<;%> after struct definition");
18074         else if (TREE_CODE (type) == UNION_TYPE)
18075           error_at (loc, "expected %<;%> after union definition");
18076         else
18077           gcc_unreachable ();
18078
18079         /* Unget one token and smash it to look as though we encountered
18080            a semicolon in the input stream.  */
18081         cp_lexer_set_token_position (parser->lexer, prev);
18082         token = cp_lexer_peek_token (parser->lexer);
18083         token->type = CPP_SEMICOLON;
18084         token->keyword = RID_MAX;
18085       }
18086   }
18087
18088   /* If this class is not itself within the scope of another class,
18089      then we need to parse the bodies of all of the queued function
18090      definitions.  Note that the queued functions defined in a class
18091      are not always processed immediately following the
18092      class-specifier for that class.  Consider:
18093
18094        struct A {
18095          struct B { void f() { sizeof (A); } };
18096        };
18097
18098      If `f' were processed before the processing of `A' were
18099      completed, there would be no way to compute the size of `A'.
18100      Note that the nesting we are interested in here is lexical --
18101      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18102      for:
18103
18104        struct A { struct B; };
18105        struct A::B { void f() { } };
18106
18107      there is no need to delay the parsing of `A::B::f'.  */
18108   if (--parser->num_classes_being_defined == 0)
18109     {
18110       tree decl;
18111       tree class_type = NULL_TREE;
18112       tree pushed_scope = NULL_TREE;
18113       unsigned ix;
18114       cp_default_arg_entry *e;
18115       tree save_ccp, save_ccr;
18116
18117       /* In a first pass, parse default arguments to the functions.
18118          Then, in a second pass, parse the bodies of the functions.
18119          This two-phased approach handles cases like:
18120
18121             struct S {
18122               void f() { g(); }
18123               void g(int i = 3);
18124             };
18125
18126          */
18127       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18128                         ix, e)
18129         {
18130           decl = e->decl;
18131           /* If there are default arguments that have not yet been processed,
18132              take care of them now.  */
18133           if (class_type != e->class_type)
18134             {
18135               if (pushed_scope)
18136                 pop_scope (pushed_scope);
18137               class_type = e->class_type;
18138               pushed_scope = push_scope (class_type);
18139             }
18140           /* Make sure that any template parameters are in scope.  */
18141           maybe_begin_member_template_processing (decl);
18142           /* Parse the default argument expressions.  */
18143           cp_parser_late_parsing_default_args (parser, decl);
18144           /* Remove any template parameters from the symbol table.  */
18145           maybe_end_member_template_processing ();
18146         }
18147       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18148       /* Now parse any NSDMIs.  */
18149       save_ccp = current_class_ptr;
18150       save_ccr = current_class_ref;
18151       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18152         {
18153           if (class_type != DECL_CONTEXT (decl))
18154             {
18155               if (pushed_scope)
18156                 pop_scope (pushed_scope);
18157               class_type = DECL_CONTEXT (decl);
18158               pushed_scope = push_scope (class_type);
18159             }
18160           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18161           cp_parser_late_parsing_nsdmi (parser, decl);
18162         }
18163       VEC_truncate (tree, unparsed_nsdmis, 0);
18164       current_class_ptr = save_ccp;
18165       current_class_ref = save_ccr;
18166       if (pushed_scope)
18167         pop_scope (pushed_scope);
18168       /* Now parse the body of the functions.  */
18169       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18170         cp_parser_late_parsing_for_member (parser, decl);
18171       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18172     }
18173
18174   /* Put back any saved access checks.  */
18175   pop_deferring_access_checks ();
18176
18177   /* Restore saved state.  */
18178   parser->in_switch_statement_p = in_switch_statement_p;
18179   parser->in_statement = in_statement;
18180   parser->in_function_body = saved_in_function_body;
18181   parser->num_template_parameter_lists
18182     = saved_num_template_parameter_lists;
18183   parser->in_unbraced_linkage_specification_p
18184     = saved_in_unbraced_linkage_specification_p;
18185
18186   return type;
18187 }
18188
18189 static tree
18190 cp_parser_class_specifier (cp_parser* parser)
18191 {
18192   tree ret;
18193   timevar_push (TV_PARSE_STRUCT);
18194   ret = cp_parser_class_specifier_1 (parser);
18195   timevar_pop (TV_PARSE_STRUCT);
18196   return ret;
18197 }
18198
18199 /* Parse a class-head.
18200
18201    class-head:
18202      class-key identifier [opt] base-clause [opt]
18203      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18204      class-key nested-name-specifier [opt] template-id
18205        base-clause [opt]
18206
18207    class-virt-specifier:
18208      final
18209
18210    GNU Extensions:
18211      class-key attributes identifier [opt] base-clause [opt]
18212      class-key attributes nested-name-specifier identifier base-clause [opt]
18213      class-key attributes nested-name-specifier [opt] template-id
18214        base-clause [opt]
18215
18216    Upon return BASES is initialized to the list of base classes (or
18217    NULL, if there are none) in the same form returned by
18218    cp_parser_base_clause.
18219
18220    Returns the TYPE of the indicated class.  Sets
18221    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18222    involving a nested-name-specifier was used, and FALSE otherwise.
18223
18224    Returns error_mark_node if this is not a class-head.
18225
18226    Returns NULL_TREE if the class-head is syntactically valid, but
18227    semantically invalid in a way that means we should skip the entire
18228    body of the class.  */
18229
18230 static tree
18231 cp_parser_class_head (cp_parser* parser,
18232                       bool* nested_name_specifier_p,
18233                       tree *attributes_p,
18234                       tree *bases)
18235 {
18236   tree nested_name_specifier;
18237   enum tag_types class_key;
18238   tree id = NULL_TREE;
18239   tree type = NULL_TREE;
18240   tree attributes;
18241   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18242   bool template_id_p = false;
18243   bool qualified_p = false;
18244   bool invalid_nested_name_p = false;
18245   bool invalid_explicit_specialization_p = false;
18246   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18247   tree pushed_scope = NULL_TREE;
18248   unsigned num_templates;
18249   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18250   /* Assume no nested-name-specifier will be present.  */
18251   *nested_name_specifier_p = false;
18252   /* Assume no template parameter lists will be used in defining the
18253      type.  */
18254   num_templates = 0;
18255   parser->colon_corrects_to_scope_p = false;
18256
18257   *bases = NULL_TREE;
18258
18259   /* Look for the class-key.  */
18260   class_key = cp_parser_class_key (parser);
18261   if (class_key == none_type)
18262     return error_mark_node;
18263
18264   /* Parse the attributes.  */
18265   attributes = cp_parser_attributes_opt (parser);
18266
18267   /* If the next token is `::', that is invalid -- but sometimes
18268      people do try to write:
18269
18270        struct ::S {};
18271
18272      Handle this gracefully by accepting the extra qualifier, and then
18273      issuing an error about it later if this really is a
18274      class-head.  If it turns out just to be an elaborated type
18275      specifier, remain silent.  */
18276   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18277     qualified_p = true;
18278
18279   push_deferring_access_checks (dk_no_check);
18280
18281   /* Determine the name of the class.  Begin by looking for an
18282      optional nested-name-specifier.  */
18283   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18284   nested_name_specifier
18285     = cp_parser_nested_name_specifier_opt (parser,
18286                                            /*typename_keyword_p=*/false,
18287                                            /*check_dependency_p=*/false,
18288                                            /*type_p=*/false,
18289                                            /*is_declaration=*/false);
18290   /* If there was a nested-name-specifier, then there *must* be an
18291      identifier.  */
18292   if (nested_name_specifier)
18293     {
18294       type_start_token = cp_lexer_peek_token (parser->lexer);
18295       /* Although the grammar says `identifier', it really means
18296          `class-name' or `template-name'.  You are only allowed to
18297          define a class that has already been declared with this
18298          syntax.
18299
18300          The proposed resolution for Core Issue 180 says that wherever
18301          you see `class T::X' you should treat `X' as a type-name.
18302
18303          It is OK to define an inaccessible class; for example:
18304
18305            class A { class B; };
18306            class A::B {};
18307
18308          We do not know if we will see a class-name, or a
18309          template-name.  We look for a class-name first, in case the
18310          class-name is a template-id; if we looked for the
18311          template-name first we would stop after the template-name.  */
18312       cp_parser_parse_tentatively (parser);
18313       type = cp_parser_class_name (parser,
18314                                    /*typename_keyword_p=*/false,
18315                                    /*template_keyword_p=*/false,
18316                                    class_type,
18317                                    /*check_dependency_p=*/false,
18318                                    /*class_head_p=*/true,
18319                                    /*is_declaration=*/false);
18320       /* If that didn't work, ignore the nested-name-specifier.  */
18321       if (!cp_parser_parse_definitely (parser))
18322         {
18323           invalid_nested_name_p = true;
18324           type_start_token = cp_lexer_peek_token (parser->lexer);
18325           id = cp_parser_identifier (parser);
18326           if (id == error_mark_node)
18327             id = NULL_TREE;
18328         }
18329       /* If we could not find a corresponding TYPE, treat this
18330          declaration like an unqualified declaration.  */
18331       if (type == error_mark_node)
18332         nested_name_specifier = NULL_TREE;
18333       /* Otherwise, count the number of templates used in TYPE and its
18334          containing scopes.  */
18335       else
18336         {
18337           tree scope;
18338
18339           for (scope = TREE_TYPE (type);
18340                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18341                scope = (TYPE_P (scope)
18342                         ? TYPE_CONTEXT (scope)
18343                         : DECL_CONTEXT (scope)))
18344             if (TYPE_P (scope)
18345                 && CLASS_TYPE_P (scope)
18346                 && CLASSTYPE_TEMPLATE_INFO (scope)
18347                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18348                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18349               ++num_templates;
18350         }
18351     }
18352   /* Otherwise, the identifier is optional.  */
18353   else
18354     {
18355       /* We don't know whether what comes next is a template-id,
18356          an identifier, or nothing at all.  */
18357       cp_parser_parse_tentatively (parser);
18358       /* Check for a template-id.  */
18359       type_start_token = cp_lexer_peek_token (parser->lexer);
18360       id = cp_parser_template_id (parser,
18361                                   /*template_keyword_p=*/false,
18362                                   /*check_dependency_p=*/true,
18363                                   /*is_declaration=*/true);
18364       /* If that didn't work, it could still be an identifier.  */
18365       if (!cp_parser_parse_definitely (parser))
18366         {
18367           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18368             {
18369               type_start_token = cp_lexer_peek_token (parser->lexer);
18370               id = cp_parser_identifier (parser);
18371             }
18372           else
18373             id = NULL_TREE;
18374         }
18375       else
18376         {
18377           template_id_p = true;
18378           ++num_templates;
18379         }
18380     }
18381
18382   pop_deferring_access_checks ();
18383
18384   if (id)
18385     {
18386       cp_parser_check_for_invalid_template_id (parser, id,
18387                                                type_start_token->location);
18388     }
18389   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18390
18391   /* If it's not a `:' or a `{' then we can't really be looking at a
18392      class-head, since a class-head only appears as part of a
18393      class-specifier.  We have to detect this situation before calling
18394      xref_tag, since that has irreversible side-effects.  */
18395   if (!cp_parser_next_token_starts_class_definition_p (parser))
18396     {
18397       cp_parser_error (parser, "expected %<{%> or %<:%>");
18398       type = error_mark_node;
18399       goto out;
18400     }
18401
18402   /* At this point, we're going ahead with the class-specifier, even
18403      if some other problem occurs.  */
18404   cp_parser_commit_to_tentative_parse (parser);
18405   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18406     {
18407       cp_parser_error (parser,
18408                        "cannot specify %<override%> for a class");
18409       type = error_mark_node;
18410       goto out;
18411     }
18412   /* Issue the error about the overly-qualified name now.  */
18413   if (qualified_p)
18414     {
18415       cp_parser_error (parser,
18416                        "global qualification of class name is invalid");
18417       type = error_mark_node;
18418       goto out;
18419     }
18420   else if (invalid_nested_name_p)
18421     {
18422       cp_parser_error (parser,
18423                        "qualified name does not name a class");
18424       type = error_mark_node;
18425       goto out;
18426     }
18427   else if (nested_name_specifier)
18428     {
18429       tree scope;
18430
18431       /* Reject typedef-names in class heads.  */
18432       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18433         {
18434           error_at (type_start_token->location,
18435                     "invalid class name in declaration of %qD",
18436                     type);
18437           type = NULL_TREE;
18438           goto done;
18439         }
18440
18441       /* Figure out in what scope the declaration is being placed.  */
18442       scope = current_scope ();
18443       /* If that scope does not contain the scope in which the
18444          class was originally declared, the program is invalid.  */
18445       if (scope && !is_ancestor (scope, nested_name_specifier))
18446         {
18447           if (at_namespace_scope_p ())
18448             error_at (type_start_token->location,
18449                       "declaration of %qD in namespace %qD which does not "
18450                       "enclose %qD",
18451                       type, scope, nested_name_specifier);
18452           else
18453             error_at (type_start_token->location,
18454                       "declaration of %qD in %qD which does not enclose %qD",
18455                       type, scope, nested_name_specifier);
18456           type = NULL_TREE;
18457           goto done;
18458         }
18459       /* [dcl.meaning]
18460
18461          A declarator-id shall not be qualified except for the
18462          definition of a ... nested class outside of its class
18463          ... [or] the definition or explicit instantiation of a
18464          class member of a namespace outside of its namespace.  */
18465       if (scope == nested_name_specifier)
18466         {
18467           permerror (nested_name_specifier_token_start->location,
18468                      "extra qualification not allowed");
18469           nested_name_specifier = NULL_TREE;
18470           num_templates = 0;
18471         }
18472     }
18473   /* An explicit-specialization must be preceded by "template <>".  If
18474      it is not, try to recover gracefully.  */
18475   if (at_namespace_scope_p ()
18476       && parser->num_template_parameter_lists == 0
18477       && template_id_p)
18478     {
18479       error_at (type_start_token->location,
18480                 "an explicit specialization must be preceded by %<template <>%>");
18481       invalid_explicit_specialization_p = true;
18482       /* Take the same action that would have been taken by
18483          cp_parser_explicit_specialization.  */
18484       ++parser->num_template_parameter_lists;
18485       begin_specialization ();
18486     }
18487   /* There must be no "return" statements between this point and the
18488      end of this function; set "type "to the correct return value and
18489      use "goto done;" to return.  */
18490   /* Make sure that the right number of template parameters were
18491      present.  */
18492   if (!cp_parser_check_template_parameters (parser, num_templates,
18493                                             type_start_token->location,
18494                                             /*declarator=*/NULL))
18495     {
18496       /* If something went wrong, there is no point in even trying to
18497          process the class-definition.  */
18498       type = NULL_TREE;
18499       goto done;
18500     }
18501
18502   /* Look up the type.  */
18503   if (template_id_p)
18504     {
18505       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18506           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18507               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18508         {
18509           error_at (type_start_token->location,
18510                     "function template %qD redeclared as a class template", id);
18511           type = error_mark_node;
18512         }
18513       else
18514         {
18515           type = TREE_TYPE (id);
18516           type = maybe_process_partial_specialization (type);
18517         }
18518       if (nested_name_specifier)
18519         pushed_scope = push_scope (nested_name_specifier);
18520     }
18521   else if (nested_name_specifier)
18522     {
18523       tree class_type;
18524
18525       /* Given:
18526
18527             template <typename T> struct S { struct T };
18528             template <typename T> struct S<T>::T { };
18529
18530          we will get a TYPENAME_TYPE when processing the definition of
18531          `S::T'.  We need to resolve it to the actual type before we
18532          try to define it.  */
18533       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18534         {
18535           class_type = resolve_typename_type (TREE_TYPE (type),
18536                                               /*only_current_p=*/false);
18537           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18538             type = TYPE_NAME (class_type);
18539           else
18540             {
18541               cp_parser_error (parser, "could not resolve typename type");
18542               type = error_mark_node;
18543             }
18544         }
18545
18546       if (maybe_process_partial_specialization (TREE_TYPE (type))
18547           == error_mark_node)
18548         {
18549           type = NULL_TREE;
18550           goto done;
18551         }
18552
18553       class_type = current_class_type;
18554       /* Enter the scope indicated by the nested-name-specifier.  */
18555       pushed_scope = push_scope (nested_name_specifier);
18556       /* Get the canonical version of this type.  */
18557       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18558       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18559           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18560         {
18561           type = push_template_decl (type);
18562           if (type == error_mark_node)
18563             {
18564               type = NULL_TREE;
18565               goto done;
18566             }
18567         }
18568
18569       type = TREE_TYPE (type);
18570       *nested_name_specifier_p = true;
18571     }
18572   else      /* The name is not a nested name.  */
18573     {
18574       /* If the class was unnamed, create a dummy name.  */
18575       if (!id)
18576         id = make_anon_name ();
18577       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18578                        parser->num_template_parameter_lists);
18579     }
18580
18581   /* Indicate whether this class was declared as a `class' or as a
18582      `struct'.  */
18583   if (TREE_CODE (type) == RECORD_TYPE)
18584     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18585   cp_parser_check_class_key (class_key, type);
18586
18587   /* If this type was already complete, and we see another definition,
18588      that's an error.  */
18589   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18590     {
18591       error_at (type_start_token->location, "redefinition of %q#T",
18592                 type);
18593       error_at (type_start_token->location, "previous definition of %q+#T",
18594                 type);
18595       type = NULL_TREE;
18596       goto done;
18597     }
18598   else if (type == error_mark_node)
18599     type = NULL_TREE;
18600
18601   /* We will have entered the scope containing the class; the names of
18602      base classes should be looked up in that context.  For example:
18603
18604        struct A { struct B {}; struct C; };
18605        struct A::C : B {};
18606
18607      is valid.  */
18608
18609   /* Get the list of base-classes, if there is one.  */
18610   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18611     *bases = cp_parser_base_clause (parser);
18612
18613  done:
18614   /* Leave the scope given by the nested-name-specifier.  We will
18615      enter the class scope itself while processing the members.  */
18616   if (pushed_scope)
18617     pop_scope (pushed_scope);
18618
18619   if (invalid_explicit_specialization_p)
18620     {
18621       end_specialization ();
18622       --parser->num_template_parameter_lists;
18623     }
18624
18625   if (type)
18626     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18627   *attributes_p = attributes;
18628   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18629     CLASSTYPE_FINAL (type) = 1;
18630  out:
18631   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18632   return type;
18633 }
18634
18635 /* Parse a class-key.
18636
18637    class-key:
18638      class
18639      struct
18640      union
18641
18642    Returns the kind of class-key specified, or none_type to indicate
18643    error.  */
18644
18645 static enum tag_types
18646 cp_parser_class_key (cp_parser* parser)
18647 {
18648   cp_token *token;
18649   enum tag_types tag_type;
18650
18651   /* Look for the class-key.  */
18652   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18653   if (!token)
18654     return none_type;
18655
18656   /* Check to see if the TOKEN is a class-key.  */
18657   tag_type = cp_parser_token_is_class_key (token);
18658   if (!tag_type)
18659     cp_parser_error (parser, "expected class-key");
18660   return tag_type;
18661 }
18662
18663 /* Parse an (optional) member-specification.
18664
18665    member-specification:
18666      member-declaration member-specification [opt]
18667      access-specifier : member-specification [opt]  */
18668
18669 static void
18670 cp_parser_member_specification_opt (cp_parser* parser)
18671 {
18672   while (true)
18673     {
18674       cp_token *token;
18675       enum rid keyword;
18676
18677       /* Peek at the next token.  */
18678       token = cp_lexer_peek_token (parser->lexer);
18679       /* If it's a `}', or EOF then we've seen all the members.  */
18680       if (token->type == CPP_CLOSE_BRACE
18681           || token->type == CPP_EOF
18682           || token->type == CPP_PRAGMA_EOL)
18683         break;
18684
18685       /* See if this token is a keyword.  */
18686       keyword = token->keyword;
18687       switch (keyword)
18688         {
18689         case RID_PUBLIC:
18690         case RID_PROTECTED:
18691         case RID_PRIVATE:
18692           /* Consume the access-specifier.  */
18693           cp_lexer_consume_token (parser->lexer);
18694           /* Remember which access-specifier is active.  */
18695           current_access_specifier = token->u.value;
18696           /* Look for the `:'.  */
18697           cp_parser_require (parser, CPP_COLON, RT_COLON);
18698           break;
18699
18700         default:
18701           /* Accept #pragmas at class scope.  */
18702           if (token->type == CPP_PRAGMA)
18703             {
18704               cp_parser_pragma (parser, pragma_external);
18705               break;
18706             }
18707
18708           /* Otherwise, the next construction must be a
18709              member-declaration.  */
18710           cp_parser_member_declaration (parser);
18711         }
18712     }
18713 }
18714
18715 /* Parse a member-declaration.
18716
18717    member-declaration:
18718      decl-specifier-seq [opt] member-declarator-list [opt] ;
18719      function-definition ; [opt]
18720      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18721      using-declaration
18722      template-declaration
18723      alias-declaration
18724
18725    member-declarator-list:
18726      member-declarator
18727      member-declarator-list , member-declarator
18728
18729    member-declarator:
18730      declarator pure-specifier [opt]
18731      declarator constant-initializer [opt]
18732      identifier [opt] : constant-expression
18733
18734    GNU Extensions:
18735
18736    member-declaration:
18737      __extension__ member-declaration
18738
18739    member-declarator:
18740      declarator attributes [opt] pure-specifier [opt]
18741      declarator attributes [opt] constant-initializer [opt]
18742      identifier [opt] attributes [opt] : constant-expression  
18743
18744    C++0x Extensions:
18745
18746    member-declaration:
18747      static_assert-declaration  */
18748
18749 static void
18750 cp_parser_member_declaration (cp_parser* parser)
18751 {
18752   cp_decl_specifier_seq decl_specifiers;
18753   tree prefix_attributes;
18754   tree decl;
18755   int declares_class_or_enum;
18756   bool friend_p;
18757   cp_token *token = NULL;
18758   cp_token *decl_spec_token_start = NULL;
18759   cp_token *initializer_token_start = NULL;
18760   int saved_pedantic;
18761   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18762
18763   /* Check for the `__extension__' keyword.  */
18764   if (cp_parser_extension_opt (parser, &saved_pedantic))
18765     {
18766       /* Recurse.  */
18767       cp_parser_member_declaration (parser);
18768       /* Restore the old value of the PEDANTIC flag.  */
18769       pedantic = saved_pedantic;
18770
18771       return;
18772     }
18773
18774   /* Check for a template-declaration.  */
18775   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18776     {
18777       /* An explicit specialization here is an error condition, and we
18778          expect the specialization handler to detect and report this.  */
18779       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18780           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18781         cp_parser_explicit_specialization (parser);
18782       else
18783         cp_parser_template_declaration (parser, /*member_p=*/true);
18784
18785       return;
18786     }
18787
18788   /* Check for a using-declaration.  */
18789   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18790     {
18791       if (cxx_dialect < cxx0x)
18792         {
18793           /* Parse the using-declaration.  */
18794           cp_parser_using_declaration (parser,
18795                                        /*access_declaration_p=*/false);
18796           return;
18797         }
18798       else
18799         {
18800           tree decl;
18801           cp_parser_parse_tentatively (parser);
18802           decl = cp_parser_alias_declaration (parser);
18803           if (cp_parser_parse_definitely (parser))
18804             finish_member_declaration (decl);
18805           else
18806             cp_parser_using_declaration (parser,
18807                                          /*access_declaration_p=*/false);
18808           return;
18809         }
18810     }
18811
18812   /* Check for @defs.  */
18813   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18814     {
18815       tree ivar, member;
18816       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18817       ivar = ivar_chains;
18818       while (ivar)
18819         {
18820           member = ivar;
18821           ivar = TREE_CHAIN (member);
18822           TREE_CHAIN (member) = NULL_TREE;
18823           finish_member_declaration (member);
18824         }
18825       return;
18826     }
18827
18828   /* If the next token is `static_assert' we have a static assertion.  */
18829   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18830     {
18831       cp_parser_static_assert (parser, /*member_p=*/true);
18832       return;
18833     }
18834
18835   parser->colon_corrects_to_scope_p = false;
18836
18837   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18838       goto out;
18839
18840   /* Parse the decl-specifier-seq.  */
18841   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18842   cp_parser_decl_specifier_seq (parser,
18843                                 CP_PARSER_FLAGS_OPTIONAL,
18844                                 &decl_specifiers,
18845                                 &declares_class_or_enum);
18846   prefix_attributes = decl_specifiers.attributes;
18847   decl_specifiers.attributes = NULL_TREE;
18848   /* Check for an invalid type-name.  */
18849   if (!decl_specifiers.any_type_specifiers_p
18850       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18851     goto out;
18852   /* If there is no declarator, then the decl-specifier-seq should
18853      specify a type.  */
18854   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18855     {
18856       /* If there was no decl-specifier-seq, and the next token is a
18857          `;', then we have something like:
18858
18859            struct S { ; };
18860
18861          [class.mem]
18862
18863          Each member-declaration shall declare at least one member
18864          name of the class.  */
18865       if (!decl_specifiers.any_specifiers_p)
18866         {
18867           cp_token *token = cp_lexer_peek_token (parser->lexer);
18868           if (!in_system_header_at (token->location))
18869             pedwarn (token->location, OPT_Wpedantic, "extra %<;%>");
18870         }
18871       else
18872         {
18873           tree type;
18874
18875           /* See if this declaration is a friend.  */
18876           friend_p = cp_parser_friend_p (&decl_specifiers);
18877           /* If there were decl-specifiers, check to see if there was
18878              a class-declaration.  */
18879           type = check_tag_decl (&decl_specifiers);
18880           /* Nested classes have already been added to the class, but
18881              a `friend' needs to be explicitly registered.  */
18882           if (friend_p)
18883             {
18884               /* If the `friend' keyword was present, the friend must
18885                  be introduced with a class-key.  */
18886                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18887                  pedwarn (decl_spec_token_start->location, OPT_Wpedantic,
18888                           "in C++03 a class-key must be used "
18889                           "when declaring a friend");
18890                /* In this case:
18891
18892                     template <typename T> struct A {
18893                       friend struct A<T>::B;
18894                     };
18895
18896                   A<T>::B will be represented by a TYPENAME_TYPE, and
18897                   therefore not recognized by check_tag_decl.  */
18898                if (!type)
18899                  {
18900                    type = decl_specifiers.type;
18901                    if (type && TREE_CODE (type) == TYPE_DECL)
18902                      type = TREE_TYPE (type);
18903                  }
18904                if (!type || !TYPE_P (type))
18905                  error_at (decl_spec_token_start->location,
18906                            "friend declaration does not name a class or "
18907                            "function");
18908                else
18909                  make_friend_class (current_class_type, type,
18910                                     /*complain=*/true);
18911             }
18912           /* If there is no TYPE, an error message will already have
18913              been issued.  */
18914           else if (!type || type == error_mark_node)
18915             ;
18916           /* An anonymous aggregate has to be handled specially; such
18917              a declaration really declares a data member (with a
18918              particular type), as opposed to a nested class.  */
18919           else if (ANON_AGGR_TYPE_P (type))
18920             {
18921               /* Remove constructors and such from TYPE, now that we
18922                  know it is an anonymous aggregate.  */
18923               fixup_anonymous_aggr (type);
18924               /* And make the corresponding data member.  */
18925               decl = build_decl (decl_spec_token_start->location,
18926                                  FIELD_DECL, NULL_TREE, type);
18927               /* Add it to the class.  */
18928               finish_member_declaration (decl);
18929             }
18930           else
18931             cp_parser_check_access_in_redeclaration
18932                                               (TYPE_NAME (type),
18933                                                decl_spec_token_start->location);
18934         }
18935     }
18936   else
18937     {
18938       bool assume_semicolon = false;
18939
18940       /* See if these declarations will be friends.  */
18941       friend_p = cp_parser_friend_p (&decl_specifiers);
18942
18943       /* Keep going until we hit the `;' at the end of the
18944          declaration.  */
18945       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18946         {
18947           tree attributes = NULL_TREE;
18948           tree first_attribute;
18949
18950           /* Peek at the next token.  */
18951           token = cp_lexer_peek_token (parser->lexer);
18952
18953           /* Check for a bitfield declaration.  */
18954           if (token->type == CPP_COLON
18955               || (token->type == CPP_NAME
18956                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18957                   == CPP_COLON))
18958             {
18959               tree identifier;
18960               tree width;
18961
18962               /* Get the name of the bitfield.  Note that we cannot just
18963                  check TOKEN here because it may have been invalidated by
18964                  the call to cp_lexer_peek_nth_token above.  */
18965               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18966                 identifier = cp_parser_identifier (parser);
18967               else
18968                 identifier = NULL_TREE;
18969
18970               /* Consume the `:' token.  */
18971               cp_lexer_consume_token (parser->lexer);
18972               /* Get the width of the bitfield.  */
18973               width
18974                 = cp_parser_constant_expression (parser,
18975                                                  /*allow_non_constant=*/false,
18976                                                  NULL);
18977
18978               /* Look for attributes that apply to the bitfield.  */
18979               attributes = cp_parser_attributes_opt (parser);
18980               /* Remember which attributes are prefix attributes and
18981                  which are not.  */
18982               first_attribute = attributes;
18983               /* Combine the attributes.  */
18984               attributes = chainon (prefix_attributes, attributes);
18985
18986               /* Create the bitfield declaration.  */
18987               decl = grokbitfield (identifier
18988                                    ? make_id_declarator (NULL_TREE,
18989                                                          identifier,
18990                                                          sfk_none)
18991                                    : NULL,
18992                                    &decl_specifiers,
18993                                    width,
18994                                    attributes);
18995             }
18996           else
18997             {
18998               cp_declarator *declarator;
18999               tree initializer;
19000               tree asm_specification;
19001               int ctor_dtor_or_conv_p;
19002
19003               /* Parse the declarator.  */
19004               declarator
19005                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19006                                         &ctor_dtor_or_conv_p,
19007                                         /*parenthesized_p=*/NULL,
19008                                         /*member_p=*/true);
19009
19010               /* If something went wrong parsing the declarator, make sure
19011                  that we at least consume some tokens.  */
19012               if (declarator == cp_error_declarator)
19013                 {
19014                   /* Skip to the end of the statement.  */
19015                   cp_parser_skip_to_end_of_statement (parser);
19016                   /* If the next token is not a semicolon, that is
19017                      probably because we just skipped over the body of
19018                      a function.  So, we consume a semicolon if
19019                      present, but do not issue an error message if it
19020                      is not present.  */
19021                   if (cp_lexer_next_token_is (parser->lexer,
19022                                               CPP_SEMICOLON))
19023                     cp_lexer_consume_token (parser->lexer);
19024                   goto out;
19025                 }
19026
19027               if (declares_class_or_enum & 2)
19028                 cp_parser_check_for_definition_in_return_type
19029                                             (declarator, decl_specifiers.type,
19030                                              decl_specifiers.type_location);
19031
19032               /* Look for an asm-specification.  */
19033               asm_specification = cp_parser_asm_specification_opt (parser);
19034               /* Look for attributes that apply to the declaration.  */
19035               attributes = cp_parser_attributes_opt (parser);
19036               /* Remember which attributes are prefix attributes and
19037                  which are not.  */
19038               first_attribute = attributes;
19039               /* Combine the attributes.  */
19040               attributes = chainon (prefix_attributes, attributes);
19041
19042               /* If it's an `=', then we have a constant-initializer or a
19043                  pure-specifier.  It is not correct to parse the
19044                  initializer before registering the member declaration
19045                  since the member declaration should be in scope while
19046                  its initializer is processed.  However, the rest of the
19047                  front end does not yet provide an interface that allows
19048                  us to handle this correctly.  */
19049               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19050                 {
19051                   /* In [class.mem]:
19052
19053                      A pure-specifier shall be used only in the declaration of
19054                      a virtual function.
19055
19056                      A member-declarator can contain a constant-initializer
19057                      only if it declares a static member of integral or
19058                      enumeration type.
19059
19060                      Therefore, if the DECLARATOR is for a function, we look
19061                      for a pure-specifier; otherwise, we look for a
19062                      constant-initializer.  When we call `grokfield', it will
19063                      perform more stringent semantics checks.  */
19064                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19065                   if (function_declarator_p (declarator)
19066                       || (decl_specifiers.type
19067                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19068                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19069                               == FUNCTION_TYPE)))
19070                     initializer = cp_parser_pure_specifier (parser);
19071                   else if (decl_specifiers.storage_class != sc_static)
19072                     initializer = cp_parser_save_nsdmi (parser);
19073                   else if (cxx_dialect >= cxx0x)
19074                     {
19075                       bool nonconst;
19076                       /* Don't require a constant rvalue in C++11, since we
19077                          might want a reference constant.  We'll enforce
19078                          constancy later.  */
19079                       cp_lexer_consume_token (parser->lexer);
19080                       /* Parse the initializer.  */
19081                       initializer = cp_parser_initializer_clause (parser,
19082                                                                   &nonconst);
19083                     }
19084                   else
19085                     /* Parse the initializer.  */
19086                     initializer = cp_parser_constant_initializer (parser);
19087                 }
19088               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19089                        && !function_declarator_p (declarator))
19090                 {
19091                   bool x;
19092                   if (decl_specifiers.storage_class != sc_static)
19093                     initializer = cp_parser_save_nsdmi (parser);
19094                   else
19095                     initializer = cp_parser_initializer (parser, &x, &x);
19096                 }
19097               /* Otherwise, there is no initializer.  */
19098               else
19099                 initializer = NULL_TREE;
19100
19101               /* See if we are probably looking at a function
19102                  definition.  We are certainly not looking at a
19103                  member-declarator.  Calling `grokfield' has
19104                  side-effects, so we must not do it unless we are sure
19105                  that we are looking at a member-declarator.  */
19106               if (cp_parser_token_starts_function_definition_p
19107                   (cp_lexer_peek_token (parser->lexer)))
19108                 {
19109                   /* The grammar does not allow a pure-specifier to be
19110                      used when a member function is defined.  (It is
19111                      possible that this fact is an oversight in the
19112                      standard, since a pure function may be defined
19113                      outside of the class-specifier.  */
19114                   if (initializer && initializer_token_start)
19115                     error_at (initializer_token_start->location,
19116                               "pure-specifier on function-definition");
19117                   decl = cp_parser_save_member_function_body (parser,
19118                                                               &decl_specifiers,
19119                                                               declarator,
19120                                                               attributes);
19121                   /* If the member was not a friend, declare it here.  */
19122                   if (!friend_p)
19123                     finish_member_declaration (decl);
19124                   /* Peek at the next token.  */
19125                   token = cp_lexer_peek_token (parser->lexer);
19126                   /* If the next token is a semicolon, consume it.  */
19127                   if (token->type == CPP_SEMICOLON)
19128                     cp_lexer_consume_token (parser->lexer);
19129                   goto out;
19130                 }
19131               else
19132                 if (declarator->kind == cdk_function)
19133                   declarator->id_loc = token->location;
19134                 /* Create the declaration.  */
19135                 decl = grokfield (declarator, &decl_specifiers,
19136                                   initializer, /*init_const_expr_p=*/true,
19137                                   asm_specification,
19138                                   attributes);
19139             }
19140
19141           /* Reset PREFIX_ATTRIBUTES.  */
19142           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19143             attributes = TREE_CHAIN (attributes);
19144           if (attributes)
19145             TREE_CHAIN (attributes) = NULL_TREE;
19146
19147           /* If there is any qualification still in effect, clear it
19148              now; we will be starting fresh with the next declarator.  */
19149           parser->scope = NULL_TREE;
19150           parser->qualifying_scope = NULL_TREE;
19151           parser->object_scope = NULL_TREE;
19152           /* If it's a `,', then there are more declarators.  */
19153           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19154             cp_lexer_consume_token (parser->lexer);
19155           /* If the next token isn't a `;', then we have a parse error.  */
19156           else if (cp_lexer_next_token_is_not (parser->lexer,
19157                                                CPP_SEMICOLON))
19158             {
19159               /* The next token might be a ways away from where the
19160                  actual semicolon is missing.  Find the previous token
19161                  and use that for our error position.  */
19162               cp_token *token = cp_lexer_previous_token (parser->lexer);
19163               error_at (token->location,
19164                         "expected %<;%> at end of member declaration");
19165
19166               /* Assume that the user meant to provide a semicolon.  If
19167                  we were to cp_parser_skip_to_end_of_statement, we might
19168                  skip to a semicolon inside a member function definition
19169                  and issue nonsensical error messages.  */
19170               assume_semicolon = true;
19171             }
19172
19173           if (decl)
19174             {
19175               /* Add DECL to the list of members.  */
19176               if (!friend_p)
19177                 finish_member_declaration (decl);
19178
19179               if (TREE_CODE (decl) == FUNCTION_DECL)
19180                 cp_parser_save_default_args (parser, decl);
19181               else if (TREE_CODE (decl) == FIELD_DECL
19182                        && !DECL_C_BIT_FIELD (decl)
19183                        && DECL_INITIAL (decl))
19184                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19185                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19186             }
19187
19188           if (assume_semicolon)
19189             goto out;
19190         }
19191     }
19192
19193   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19194  out:
19195   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19196 }
19197
19198 /* Parse a pure-specifier.
19199
19200    pure-specifier:
19201      = 0
19202
19203    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19204    Otherwise, ERROR_MARK_NODE is returned.  */
19205
19206 static tree
19207 cp_parser_pure_specifier (cp_parser* parser)
19208 {
19209   cp_token *token;
19210
19211   /* Look for the `=' token.  */
19212   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19213     return error_mark_node;
19214   /* Look for the `0' token.  */
19215   token = cp_lexer_peek_token (parser->lexer);
19216
19217   if (token->type == CPP_EOF
19218       || token->type == CPP_PRAGMA_EOL)
19219     return error_mark_node;
19220
19221   cp_lexer_consume_token (parser->lexer);
19222
19223   /* Accept = default or = delete in c++0x mode.  */
19224   if (token->keyword == RID_DEFAULT
19225       || token->keyword == RID_DELETE)
19226     {
19227       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19228       return token->u.value;
19229     }
19230
19231   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19232   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19233     {
19234       cp_parser_error (parser,
19235                        "invalid pure specifier (only %<= 0%> is allowed)");
19236       cp_parser_skip_to_end_of_statement (parser);
19237       return error_mark_node;
19238     }
19239   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19240     {
19241       error_at (token->location, "templates may not be %<virtual%>");
19242       return error_mark_node;
19243     }
19244
19245   return integer_zero_node;
19246 }
19247
19248 /* Parse a constant-initializer.
19249
19250    constant-initializer:
19251      = constant-expression
19252
19253    Returns a representation of the constant-expression.  */
19254
19255 static tree
19256 cp_parser_constant_initializer (cp_parser* parser)
19257 {
19258   /* Look for the `=' token.  */
19259   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19260     return error_mark_node;
19261
19262   /* It is invalid to write:
19263
19264        struct S { static const int i = { 7 }; };
19265
19266      */
19267   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19268     {
19269       cp_parser_error (parser,
19270                        "a brace-enclosed initializer is not allowed here");
19271       /* Consume the opening brace.  */
19272       cp_lexer_consume_token (parser->lexer);
19273       /* Skip the initializer.  */
19274       cp_parser_skip_to_closing_brace (parser);
19275       /* Look for the trailing `}'.  */
19276       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19277
19278       return error_mark_node;
19279     }
19280
19281   return cp_parser_constant_expression (parser,
19282                                         /*allow_non_constant=*/false,
19283                                         NULL);
19284 }
19285
19286 /* Derived classes [gram.class.derived] */
19287
19288 /* Parse a base-clause.
19289
19290    base-clause:
19291      : base-specifier-list
19292
19293    base-specifier-list:
19294      base-specifier ... [opt]
19295      base-specifier-list , base-specifier ... [opt]
19296
19297    Returns a TREE_LIST representing the base-classes, in the order in
19298    which they were declared.  The representation of each node is as
19299    described by cp_parser_base_specifier.
19300
19301    In the case that no bases are specified, this function will return
19302    NULL_TREE, not ERROR_MARK_NODE.  */
19303
19304 static tree
19305 cp_parser_base_clause (cp_parser* parser)
19306 {
19307   tree bases = NULL_TREE;
19308
19309   /* Look for the `:' that begins the list.  */
19310   cp_parser_require (parser, CPP_COLON, RT_COLON);
19311
19312   /* Scan the base-specifier-list.  */
19313   while (true)
19314     {
19315       cp_token *token;
19316       tree base;
19317       bool pack_expansion_p = false;
19318
19319       /* Look for the base-specifier.  */
19320       base = cp_parser_base_specifier (parser);
19321       /* Look for the (optional) ellipsis. */
19322       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19323         {
19324           /* Consume the `...'. */
19325           cp_lexer_consume_token (parser->lexer);
19326
19327           pack_expansion_p = true;
19328         }
19329
19330       /* Add BASE to the front of the list.  */
19331       if (base && base != error_mark_node)
19332         {
19333           if (pack_expansion_p)
19334             /* Make this a pack expansion type. */
19335             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19336
19337           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19338             {
19339               TREE_CHAIN (base) = bases;
19340               bases = base;
19341             }
19342         }
19343       /* Peek at the next token.  */
19344       token = cp_lexer_peek_token (parser->lexer);
19345       /* If it's not a comma, then the list is complete.  */
19346       if (token->type != CPP_COMMA)
19347         break;
19348       /* Consume the `,'.  */
19349       cp_lexer_consume_token (parser->lexer);
19350     }
19351
19352   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19353      base class had a qualified name.  However, the next name that
19354      appears is certainly not qualified.  */
19355   parser->scope = NULL_TREE;
19356   parser->qualifying_scope = NULL_TREE;
19357   parser->object_scope = NULL_TREE;
19358
19359   return nreverse (bases);
19360 }
19361
19362 /* Parse a base-specifier.
19363
19364    base-specifier:
19365      :: [opt] nested-name-specifier [opt] class-name
19366      virtual access-specifier [opt] :: [opt] nested-name-specifier
19367        [opt] class-name
19368      access-specifier virtual [opt] :: [opt] nested-name-specifier
19369        [opt] class-name
19370
19371    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19372    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19373    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19374    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19375
19376 static tree
19377 cp_parser_base_specifier (cp_parser* parser)
19378 {
19379   cp_token *token;
19380   bool done = false;
19381   bool virtual_p = false;
19382   bool duplicate_virtual_error_issued_p = false;
19383   bool duplicate_access_error_issued_p = false;
19384   bool class_scope_p, template_p;
19385   tree access = access_default_node;
19386   tree type;
19387
19388   /* Process the optional `virtual' and `access-specifier'.  */
19389   while (!done)
19390     {
19391       /* Peek at the next token.  */
19392       token = cp_lexer_peek_token (parser->lexer);
19393       /* Process `virtual'.  */
19394       switch (token->keyword)
19395         {
19396         case RID_VIRTUAL:
19397           /* If `virtual' appears more than once, issue an error.  */
19398           if (virtual_p && !duplicate_virtual_error_issued_p)
19399             {
19400               cp_parser_error (parser,
19401                                "%<virtual%> specified more than once in base-specified");
19402               duplicate_virtual_error_issued_p = true;
19403             }
19404
19405           virtual_p = true;
19406
19407           /* Consume the `virtual' token.  */
19408           cp_lexer_consume_token (parser->lexer);
19409
19410           break;
19411
19412         case RID_PUBLIC:
19413         case RID_PROTECTED:
19414         case RID_PRIVATE:
19415           /* If more than one access specifier appears, issue an
19416              error.  */
19417           if (access != access_default_node
19418               && !duplicate_access_error_issued_p)
19419             {
19420               cp_parser_error (parser,
19421                                "more than one access specifier in base-specified");
19422               duplicate_access_error_issued_p = true;
19423             }
19424
19425           access = ridpointers[(int) token->keyword];
19426
19427           /* Consume the access-specifier.  */
19428           cp_lexer_consume_token (parser->lexer);
19429
19430           break;
19431
19432         default:
19433           done = true;
19434           break;
19435         }
19436     }
19437   /* It is not uncommon to see programs mechanically, erroneously, use
19438      the 'typename' keyword to denote (dependent) qualified types
19439      as base classes.  */
19440   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19441     {
19442       token = cp_lexer_peek_token (parser->lexer);
19443       if (!processing_template_decl)
19444         error_at (token->location,
19445                   "keyword %<typename%> not allowed outside of templates");
19446       else
19447         error_at (token->location,
19448                   "keyword %<typename%> not allowed in this context "
19449                   "(the base class is implicitly a type)");
19450       cp_lexer_consume_token (parser->lexer);
19451     }
19452
19453   /* Look for the optional `::' operator.  */
19454   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19455   /* Look for the nested-name-specifier.  The simplest way to
19456      implement:
19457
19458        [temp.res]
19459
19460        The keyword `typename' is not permitted in a base-specifier or
19461        mem-initializer; in these contexts a qualified name that
19462        depends on a template-parameter is implicitly assumed to be a
19463        type name.
19464
19465      is to pretend that we have seen the `typename' keyword at this
19466      point.  */
19467   cp_parser_nested_name_specifier_opt (parser,
19468                                        /*typename_keyword_p=*/true,
19469                                        /*check_dependency_p=*/true,
19470                                        typename_type,
19471                                        /*is_declaration=*/true);
19472   /* If the base class is given by a qualified name, assume that names
19473      we see are type names or templates, as appropriate.  */
19474   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19475   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19476
19477   if (!parser->scope
19478       && cp_lexer_next_token_is_decltype (parser->lexer))
19479     /* DR 950 allows decltype as a base-specifier.  */
19480     type = cp_parser_decltype (parser);
19481   else
19482     {
19483       /* Otherwise, look for the class-name.  */
19484       type = cp_parser_class_name (parser,
19485                                    class_scope_p,
19486                                    template_p,
19487                                    typename_type,
19488                                    /*check_dependency_p=*/true,
19489                                    /*class_head_p=*/false,
19490                                    /*is_declaration=*/true);
19491       type = TREE_TYPE (type);
19492     }
19493
19494   if (type == error_mark_node)
19495     return error_mark_node;
19496
19497   return finish_base_specifier (type, access, virtual_p);
19498 }
19499
19500 /* Exception handling [gram.exception] */
19501
19502 /* Parse an (optional) noexcept-specification.
19503
19504    noexcept-specification:
19505      noexcept ( constant-expression ) [opt]
19506
19507    If no noexcept-specification is present, returns NULL_TREE.
19508    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19509    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19510    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19511    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19512    in which case a boolean condition is returned instead.  */
19513
19514 static tree
19515 cp_parser_noexcept_specification_opt (cp_parser* parser,
19516                                       bool require_constexpr,
19517                                       bool* consumed_expr,
19518                                       bool return_cond)
19519 {
19520   cp_token *token;
19521   const char *saved_message;
19522
19523   /* Peek at the next token.  */
19524   token = cp_lexer_peek_token (parser->lexer);
19525
19526   /* Is it a noexcept-specification?  */
19527   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19528     {
19529       tree expr;
19530       cp_lexer_consume_token (parser->lexer);
19531
19532       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19533         {
19534           cp_lexer_consume_token (parser->lexer);
19535
19536           if (require_constexpr)
19537             {
19538               /* Types may not be defined in an exception-specification.  */
19539               saved_message = parser->type_definition_forbidden_message;
19540               parser->type_definition_forbidden_message
19541               = G_("types may not be defined in an exception-specification");
19542
19543               expr = cp_parser_constant_expression (parser, false, NULL);
19544
19545               /* Restore the saved message.  */
19546               parser->type_definition_forbidden_message = saved_message;
19547             }
19548           else
19549             {
19550               expr = cp_parser_expression (parser, false, NULL);
19551               *consumed_expr = true;
19552             }
19553
19554           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19555         }
19556       else
19557         {
19558           expr = boolean_true_node;
19559           if (!require_constexpr)
19560             *consumed_expr = false;
19561         }
19562
19563       /* We cannot build a noexcept-spec right away because this will check
19564          that expr is a constexpr.  */
19565       if (!return_cond)
19566         return build_noexcept_spec (expr, tf_warning_or_error);
19567       else
19568         return expr;
19569     }
19570   else
19571     return NULL_TREE;
19572 }
19573
19574 /* Parse an (optional) exception-specification.
19575
19576    exception-specification:
19577      throw ( type-id-list [opt] )
19578
19579    Returns a TREE_LIST representing the exception-specification.  The
19580    TREE_VALUE of each node is a type.  */
19581
19582 static tree
19583 cp_parser_exception_specification_opt (cp_parser* parser)
19584 {
19585   cp_token *token;
19586   tree type_id_list;
19587   const char *saved_message;
19588
19589   /* Peek at the next token.  */
19590   token = cp_lexer_peek_token (parser->lexer);
19591
19592   /* Is it a noexcept-specification?  */
19593   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19594                                                       false);
19595   if (type_id_list != NULL_TREE)
19596     return type_id_list;
19597
19598   /* If it's not `throw', then there's no exception-specification.  */
19599   if (!cp_parser_is_keyword (token, RID_THROW))
19600     return NULL_TREE;
19601
19602 #if 0
19603   /* Enable this once a lot of code has transitioned to noexcept?  */
19604   if (cxx_dialect >= cxx0x && !in_system_header)
19605     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19606              "deprecated in C++0x; use %<noexcept%> instead");
19607 #endif
19608
19609   /* Consume the `throw'.  */
19610   cp_lexer_consume_token (parser->lexer);
19611
19612   /* Look for the `('.  */
19613   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19614
19615   /* Peek at the next token.  */
19616   token = cp_lexer_peek_token (parser->lexer);
19617   /* If it's not a `)', then there is a type-id-list.  */
19618   if (token->type != CPP_CLOSE_PAREN)
19619     {
19620       /* Types may not be defined in an exception-specification.  */
19621       saved_message = parser->type_definition_forbidden_message;
19622       parser->type_definition_forbidden_message
19623         = G_("types may not be defined in an exception-specification");
19624       /* Parse the type-id-list.  */
19625       type_id_list = cp_parser_type_id_list (parser);
19626       /* Restore the saved message.  */
19627       parser->type_definition_forbidden_message = saved_message;
19628     }
19629   else
19630     type_id_list = empty_except_spec;
19631
19632   /* Look for the `)'.  */
19633   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19634
19635   return type_id_list;
19636 }
19637
19638 /* Parse an (optional) type-id-list.
19639
19640    type-id-list:
19641      type-id ... [opt]
19642      type-id-list , type-id ... [opt]
19643
19644    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19645    in the order that the types were presented.  */
19646
19647 static tree
19648 cp_parser_type_id_list (cp_parser* parser)
19649 {
19650   tree types = NULL_TREE;
19651
19652   while (true)
19653     {
19654       cp_token *token;
19655       tree type;
19656
19657       /* Get the next type-id.  */
19658       type = cp_parser_type_id (parser);
19659       /* Parse the optional ellipsis. */
19660       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19661         {
19662           /* Consume the `...'. */
19663           cp_lexer_consume_token (parser->lexer);
19664
19665           /* Turn the type into a pack expansion expression. */
19666           type = make_pack_expansion (type);
19667         }
19668       /* Add it to the list.  */
19669       types = add_exception_specifier (types, type, /*complain=*/1);
19670       /* Peek at the next token.  */
19671       token = cp_lexer_peek_token (parser->lexer);
19672       /* If it is not a `,', we are done.  */
19673       if (token->type != CPP_COMMA)
19674         break;
19675       /* Consume the `,'.  */
19676       cp_lexer_consume_token (parser->lexer);
19677     }
19678
19679   return nreverse (types);
19680 }
19681
19682 /* Parse a try-block.
19683
19684    try-block:
19685      try compound-statement handler-seq  */
19686
19687 static tree
19688 cp_parser_try_block (cp_parser* parser)
19689 {
19690   tree try_block;
19691
19692   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19693   try_block = begin_try_block ();
19694   cp_parser_compound_statement (parser, NULL, true, false);
19695   finish_try_block (try_block);
19696   cp_parser_handler_seq (parser);
19697   finish_handler_sequence (try_block);
19698
19699   return try_block;
19700 }
19701
19702 /* Parse a function-try-block.
19703
19704    function-try-block:
19705      try ctor-initializer [opt] function-body handler-seq  */
19706
19707 static bool
19708 cp_parser_function_try_block (cp_parser* parser)
19709 {
19710   tree compound_stmt;
19711   tree try_block;
19712   bool ctor_initializer_p;
19713
19714   /* Look for the `try' keyword.  */
19715   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19716     return false;
19717   /* Let the rest of the front end know where we are.  */
19718   try_block = begin_function_try_block (&compound_stmt);
19719   /* Parse the function-body.  */
19720   ctor_initializer_p
19721     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19722   /* We're done with the `try' part.  */
19723   finish_function_try_block (try_block);
19724   /* Parse the handlers.  */
19725   cp_parser_handler_seq (parser);
19726   /* We're done with the handlers.  */
19727   finish_function_handler_sequence (try_block, compound_stmt);
19728
19729   return ctor_initializer_p;
19730 }
19731
19732 /* Parse a handler-seq.
19733
19734    handler-seq:
19735      handler handler-seq [opt]  */
19736
19737 static void
19738 cp_parser_handler_seq (cp_parser* parser)
19739 {
19740   while (true)
19741     {
19742       cp_token *token;
19743
19744       /* Parse the handler.  */
19745       cp_parser_handler (parser);
19746       /* Peek at the next token.  */
19747       token = cp_lexer_peek_token (parser->lexer);
19748       /* If it's not `catch' then there are no more handlers.  */
19749       if (!cp_parser_is_keyword (token, RID_CATCH))
19750         break;
19751     }
19752 }
19753
19754 /* Parse a handler.
19755
19756    handler:
19757      catch ( exception-declaration ) compound-statement  */
19758
19759 static void
19760 cp_parser_handler (cp_parser* parser)
19761 {
19762   tree handler;
19763   tree declaration;
19764
19765   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19766   handler = begin_handler ();
19767   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19768   declaration = cp_parser_exception_declaration (parser);
19769   finish_handler_parms (declaration, handler);
19770   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19771   cp_parser_compound_statement (parser, NULL, false, false);
19772   finish_handler (handler);
19773 }
19774
19775 /* Parse an exception-declaration.
19776
19777    exception-declaration:
19778      type-specifier-seq declarator
19779      type-specifier-seq abstract-declarator
19780      type-specifier-seq
19781      ...
19782
19783    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19784    ellipsis variant is used.  */
19785
19786 static tree
19787 cp_parser_exception_declaration (cp_parser* parser)
19788 {
19789   cp_decl_specifier_seq type_specifiers;
19790   cp_declarator *declarator;
19791   const char *saved_message;
19792
19793   /* If it's an ellipsis, it's easy to handle.  */
19794   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19795     {
19796       /* Consume the `...' token.  */
19797       cp_lexer_consume_token (parser->lexer);
19798       return NULL_TREE;
19799     }
19800
19801   /* Types may not be defined in exception-declarations.  */
19802   saved_message = parser->type_definition_forbidden_message;
19803   parser->type_definition_forbidden_message
19804     = G_("types may not be defined in exception-declarations");
19805
19806   /* Parse the type-specifier-seq.  */
19807   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19808                                 /*is_trailing_return=*/false,
19809                                 &type_specifiers);
19810   /* If it's a `)', then there is no declarator.  */
19811   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19812     declarator = NULL;
19813   else
19814     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19815                                        /*ctor_dtor_or_conv_p=*/NULL,
19816                                        /*parenthesized_p=*/NULL,
19817                                        /*member_p=*/false);
19818
19819   /* Restore the saved message.  */
19820   parser->type_definition_forbidden_message = saved_message;
19821
19822   if (!type_specifiers.any_specifiers_p)
19823     return error_mark_node;
19824
19825   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19826 }
19827
19828 /* Parse a throw-expression.
19829
19830    throw-expression:
19831      throw assignment-expression [opt]
19832
19833    Returns a THROW_EXPR representing the throw-expression.  */
19834
19835 static tree
19836 cp_parser_throw_expression (cp_parser* parser)
19837 {
19838   tree expression;
19839   cp_token* token;
19840
19841   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19842   token = cp_lexer_peek_token (parser->lexer);
19843   /* Figure out whether or not there is an assignment-expression
19844      following the "throw" keyword.  */
19845   if (token->type == CPP_COMMA
19846       || token->type == CPP_SEMICOLON
19847       || token->type == CPP_CLOSE_PAREN
19848       || token->type == CPP_CLOSE_SQUARE
19849       || token->type == CPP_CLOSE_BRACE
19850       || token->type == CPP_COLON)
19851     expression = NULL_TREE;
19852   else
19853     expression = cp_parser_assignment_expression (parser,
19854                                                   /*cast_p=*/false, NULL);
19855
19856   return build_throw (expression);
19857 }
19858
19859 /* GNU Extensions */
19860
19861 /* Parse an (optional) asm-specification.
19862
19863    asm-specification:
19864      asm ( string-literal )
19865
19866    If the asm-specification is present, returns a STRING_CST
19867    corresponding to the string-literal.  Otherwise, returns
19868    NULL_TREE.  */
19869
19870 static tree
19871 cp_parser_asm_specification_opt (cp_parser* parser)
19872 {
19873   cp_token *token;
19874   tree asm_specification;
19875
19876   /* Peek at the next token.  */
19877   token = cp_lexer_peek_token (parser->lexer);
19878   /* If the next token isn't the `asm' keyword, then there's no
19879      asm-specification.  */
19880   if (!cp_parser_is_keyword (token, RID_ASM))
19881     return NULL_TREE;
19882
19883   /* Consume the `asm' token.  */
19884   cp_lexer_consume_token (parser->lexer);
19885   /* Look for the `('.  */
19886   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19887
19888   /* Look for the string-literal.  */
19889   asm_specification = cp_parser_string_literal (parser, false, false);
19890
19891   /* Look for the `)'.  */
19892   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19893
19894   return asm_specification;
19895 }
19896
19897 /* Parse an asm-operand-list.
19898
19899    asm-operand-list:
19900      asm-operand
19901      asm-operand-list , asm-operand
19902
19903    asm-operand:
19904      string-literal ( expression )
19905      [ string-literal ] string-literal ( expression )
19906
19907    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19908    each node is the expression.  The TREE_PURPOSE is itself a
19909    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19910    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19911    is a STRING_CST for the string literal before the parenthesis. Returns
19912    ERROR_MARK_NODE if any of the operands are invalid.  */
19913
19914 static tree
19915 cp_parser_asm_operand_list (cp_parser* parser)
19916 {
19917   tree asm_operands = NULL_TREE;
19918   bool invalid_operands = false;
19919
19920   while (true)
19921     {
19922       tree string_literal;
19923       tree expression;
19924       tree name;
19925
19926       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19927         {
19928           /* Consume the `[' token.  */
19929           cp_lexer_consume_token (parser->lexer);
19930           /* Read the operand name.  */
19931           name = cp_parser_identifier (parser);
19932           if (name != error_mark_node)
19933             name = build_string (IDENTIFIER_LENGTH (name),
19934                                  IDENTIFIER_POINTER (name));
19935           /* Look for the closing `]'.  */
19936           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19937         }
19938       else
19939         name = NULL_TREE;
19940       /* Look for the string-literal.  */
19941       string_literal = cp_parser_string_literal (parser, false, false);
19942
19943       /* Look for the `('.  */
19944       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19945       /* Parse the expression.  */
19946       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19947       /* Look for the `)'.  */
19948       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19949
19950       if (name == error_mark_node 
19951           || string_literal == error_mark_node 
19952           || expression == error_mark_node)
19953         invalid_operands = true;
19954
19955       /* Add this operand to the list.  */
19956       asm_operands = tree_cons (build_tree_list (name, string_literal),
19957                                 expression,
19958                                 asm_operands);
19959       /* If the next token is not a `,', there are no more
19960          operands.  */
19961       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19962         break;
19963       /* Consume the `,'.  */
19964       cp_lexer_consume_token (parser->lexer);
19965     }
19966
19967   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19968 }
19969
19970 /* Parse an asm-clobber-list.
19971
19972    asm-clobber-list:
19973      string-literal
19974      asm-clobber-list , string-literal
19975
19976    Returns a TREE_LIST, indicating the clobbers in the order that they
19977    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19978
19979 static tree
19980 cp_parser_asm_clobber_list (cp_parser* parser)
19981 {
19982   tree clobbers = NULL_TREE;
19983
19984   while (true)
19985     {
19986       tree string_literal;
19987
19988       /* Look for the string literal.  */
19989       string_literal = cp_parser_string_literal (parser, false, false);
19990       /* Add it to the list.  */
19991       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19992       /* If the next token is not a `,', then the list is
19993          complete.  */
19994       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19995         break;
19996       /* Consume the `,' token.  */
19997       cp_lexer_consume_token (parser->lexer);
19998     }
19999
20000   return clobbers;
20001 }
20002
20003 /* Parse an asm-label-list.
20004
20005    asm-label-list:
20006      identifier
20007      asm-label-list , identifier
20008
20009    Returns a TREE_LIST, indicating the labels in the order that they
20010    appeared.  The TREE_VALUE of each node is a label.  */
20011
20012 static tree
20013 cp_parser_asm_label_list (cp_parser* parser)
20014 {
20015   tree labels = NULL_TREE;
20016
20017   while (true)
20018     {
20019       tree identifier, label, name;
20020
20021       /* Look for the identifier.  */
20022       identifier = cp_parser_identifier (parser);
20023       if (!error_operand_p (identifier))
20024         {
20025           label = lookup_label (identifier);
20026           if (TREE_CODE (label) == LABEL_DECL)
20027             {
20028               TREE_USED (label) = 1;
20029               check_goto (label);
20030               name = build_string (IDENTIFIER_LENGTH (identifier),
20031                                    IDENTIFIER_POINTER (identifier));
20032               labels = tree_cons (name, label, labels);
20033             }
20034         }
20035       /* If the next token is not a `,', then the list is
20036          complete.  */
20037       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20038         break;
20039       /* Consume the `,' token.  */
20040       cp_lexer_consume_token (parser->lexer);
20041     }
20042
20043   return nreverse (labels);
20044 }
20045
20046 /* Parse an (optional) series of attributes.
20047
20048    attributes:
20049      attributes attribute
20050
20051    attribute:
20052      __attribute__ (( attribute-list [opt] ))
20053
20054    The return value is as for cp_parser_attribute_list.  */
20055
20056 static tree
20057 cp_parser_attributes_opt (cp_parser* parser)
20058 {
20059   tree attributes = NULL_TREE;
20060
20061   while (true)
20062     {
20063       cp_token *token;
20064       tree attribute_list;
20065
20066       /* Peek at the next token.  */
20067       token = cp_lexer_peek_token (parser->lexer);
20068       /* If it's not `__attribute__', then we're done.  */
20069       if (token->keyword != RID_ATTRIBUTE)
20070         break;
20071
20072       /* Consume the `__attribute__' keyword.  */
20073       cp_lexer_consume_token (parser->lexer);
20074       /* Look for the two `(' tokens.  */
20075       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20076       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20077
20078       /* Peek at the next token.  */
20079       token = cp_lexer_peek_token (parser->lexer);
20080       if (token->type != CPP_CLOSE_PAREN)
20081         /* Parse the attribute-list.  */
20082         attribute_list = cp_parser_attribute_list (parser);
20083       else
20084         /* If the next token is a `)', then there is no attribute
20085            list.  */
20086         attribute_list = NULL;
20087
20088       /* Look for the two `)' tokens.  */
20089       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20090       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20091
20092       /* Add these new attributes to the list.  */
20093       attributes = chainon (attributes, attribute_list);
20094     }
20095
20096   return attributes;
20097 }
20098
20099 /* Parse an attribute-list.
20100
20101    attribute-list:
20102      attribute
20103      attribute-list , attribute
20104
20105    attribute:
20106      identifier
20107      identifier ( identifier )
20108      identifier ( identifier , expression-list )
20109      identifier ( expression-list )
20110
20111    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20112    to an attribute.  The TREE_PURPOSE of each node is the identifier
20113    indicating which attribute is in use.  The TREE_VALUE represents
20114    the arguments, if any.  */
20115
20116 static tree
20117 cp_parser_attribute_list (cp_parser* parser)
20118 {
20119   tree attribute_list = NULL_TREE;
20120   bool save_translate_strings_p = parser->translate_strings_p;
20121
20122   parser->translate_strings_p = false;
20123   while (true)
20124     {
20125       cp_token *token;
20126       tree identifier;
20127       tree attribute;
20128
20129       /* Look for the identifier.  We also allow keywords here; for
20130          example `__attribute__ ((const))' is legal.  */
20131       token = cp_lexer_peek_token (parser->lexer);
20132       if (token->type == CPP_NAME
20133           || token->type == CPP_KEYWORD)
20134         {
20135           tree arguments = NULL_TREE;
20136
20137           /* Consume the token.  */
20138           token = cp_lexer_consume_token (parser->lexer);
20139
20140           /* Save away the identifier that indicates which attribute
20141              this is.  */
20142           identifier = (token->type == CPP_KEYWORD) 
20143             /* For keywords, use the canonical spelling, not the
20144                parsed identifier.  */
20145             ? ridpointers[(int) token->keyword]
20146             : token->u.value;
20147           
20148           attribute = build_tree_list (identifier, NULL_TREE);
20149
20150           /* Peek at the next token.  */
20151           token = cp_lexer_peek_token (parser->lexer);
20152           /* If it's an `(', then parse the attribute arguments.  */
20153           if (token->type == CPP_OPEN_PAREN)
20154             {
20155               VEC(tree,gc) *vec;
20156               int attr_flag = (attribute_takes_identifier_p (identifier)
20157                                ? id_attr : normal_attr);
20158               vec = cp_parser_parenthesized_expression_list
20159                     (parser, attr_flag, /*cast_p=*/false,
20160                      /*allow_expansion_p=*/false,
20161                      /*non_constant_p=*/NULL);
20162               if (vec == NULL)
20163                 arguments = error_mark_node;
20164               else
20165                 {
20166                   arguments = build_tree_list_vec (vec);
20167                   release_tree_vector (vec);
20168                 }
20169               /* Save the arguments away.  */
20170               TREE_VALUE (attribute) = arguments;
20171             }
20172
20173           if (arguments != error_mark_node)
20174             {
20175               /* Add this attribute to the list.  */
20176               TREE_CHAIN (attribute) = attribute_list;
20177               attribute_list = attribute;
20178             }
20179
20180           token = cp_lexer_peek_token (parser->lexer);
20181         }
20182       /* Now, look for more attributes.  If the next token isn't a
20183          `,', we're done.  */
20184       if (token->type != CPP_COMMA)
20185         break;
20186
20187       /* Consume the comma and keep going.  */
20188       cp_lexer_consume_token (parser->lexer);
20189     }
20190   parser->translate_strings_p = save_translate_strings_p;
20191
20192   /* We built up the list in reverse order.  */
20193   return nreverse (attribute_list);
20194 }
20195
20196 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20197    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20198    current value of the PEDANTIC flag, regardless of whether or not
20199    the `__extension__' keyword is present.  The caller is responsible
20200    for restoring the value of the PEDANTIC flag.  */
20201
20202 static bool
20203 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20204 {
20205   /* Save the old value of the PEDANTIC flag.  */
20206   *saved_pedantic = pedantic;
20207
20208   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20209     {
20210       /* Consume the `__extension__' token.  */
20211       cp_lexer_consume_token (parser->lexer);
20212       /* We're not being pedantic while the `__extension__' keyword is
20213          in effect.  */
20214       pedantic = 0;
20215
20216       return true;
20217     }
20218
20219   return false;
20220 }
20221
20222 /* Parse a label declaration.
20223
20224    label-declaration:
20225      __label__ label-declarator-seq ;
20226
20227    label-declarator-seq:
20228      identifier , label-declarator-seq
20229      identifier  */
20230
20231 static void
20232 cp_parser_label_declaration (cp_parser* parser)
20233 {
20234   /* Look for the `__label__' keyword.  */
20235   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20236
20237   while (true)
20238     {
20239       tree identifier;
20240
20241       /* Look for an identifier.  */
20242       identifier = cp_parser_identifier (parser);
20243       /* If we failed, stop.  */
20244       if (identifier == error_mark_node)
20245         break;
20246       /* Declare it as a label.  */
20247       finish_label_decl (identifier);
20248       /* If the next token is a `;', stop.  */
20249       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20250         break;
20251       /* Look for the `,' separating the label declarations.  */
20252       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20253     }
20254
20255   /* Look for the final `;'.  */
20256   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20257 }
20258
20259 /* Support Functions */
20260
20261 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20262    NAME should have one of the representations used for an
20263    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20264    is returned.  If PARSER->SCOPE is a dependent type, then a
20265    SCOPE_REF is returned.
20266
20267    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20268    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20269    was formed.  Abstractly, such entities should not be passed to this
20270    function, because they do not need to be looked up, but it is
20271    simpler to check for this special case here, rather than at the
20272    call-sites.
20273
20274    In cases not explicitly covered above, this function returns a
20275    DECL, OVERLOAD, or baselink representing the result of the lookup.
20276    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20277    is returned.
20278
20279    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20280    (e.g., "struct") that was used.  In that case bindings that do not
20281    refer to types are ignored.
20282
20283    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20284    ignored.
20285
20286    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20287    are ignored.
20288
20289    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20290    types.
20291
20292    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20293    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20294    NULL_TREE otherwise.  */
20295
20296 static tree
20297 cp_parser_lookup_name (cp_parser *parser, tree name,
20298                        enum tag_types tag_type,
20299                        bool is_template,
20300                        bool is_namespace,
20301                        bool check_dependency,
20302                        tree *ambiguous_decls,
20303                        location_t name_location)
20304 {
20305   int flags = 0;
20306   tree decl;
20307   tree object_type = parser->context->object_type;
20308
20309   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20310     flags |= LOOKUP_COMPLAIN;
20311
20312   /* Assume that the lookup will be unambiguous.  */
20313   if (ambiguous_decls)
20314     *ambiguous_decls = NULL_TREE;
20315
20316   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20317      no longer valid.  Note that if we are parsing tentatively, and
20318      the parse fails, OBJECT_TYPE will be automatically restored.  */
20319   parser->context->object_type = NULL_TREE;
20320
20321   if (name == error_mark_node)
20322     return error_mark_node;
20323
20324   /* A template-id has already been resolved; there is no lookup to
20325      do.  */
20326   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20327     return name;
20328   if (BASELINK_P (name))
20329     {
20330       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20331                   == TEMPLATE_ID_EXPR);
20332       return name;
20333     }
20334
20335   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20336      it should already have been checked to make sure that the name
20337      used matches the type being destroyed.  */
20338   if (TREE_CODE (name) == BIT_NOT_EXPR)
20339     {
20340       tree type;
20341
20342       /* Figure out to which type this destructor applies.  */
20343       if (parser->scope)
20344         type = parser->scope;
20345       else if (object_type)
20346         type = object_type;
20347       else
20348         type = current_class_type;
20349       /* If that's not a class type, there is no destructor.  */
20350       if (!type || !CLASS_TYPE_P (type))
20351         return error_mark_node;
20352       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20353         lazily_declare_fn (sfk_destructor, type);
20354       if (!CLASSTYPE_DESTRUCTORS (type))
20355           return error_mark_node;
20356       /* If it was a class type, return the destructor.  */
20357       return CLASSTYPE_DESTRUCTORS (type);
20358     }
20359
20360   /* By this point, the NAME should be an ordinary identifier.  If
20361      the id-expression was a qualified name, the qualifying scope is
20362      stored in PARSER->SCOPE at this point.  */
20363   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20364
20365   /* Perform the lookup.  */
20366   if (parser->scope)
20367     {
20368       bool dependent_p;
20369
20370       if (parser->scope == error_mark_node)
20371         return error_mark_node;
20372
20373       /* If the SCOPE is dependent, the lookup must be deferred until
20374          the template is instantiated -- unless we are explicitly
20375          looking up names in uninstantiated templates.  Even then, we
20376          cannot look up the name if the scope is not a class type; it
20377          might, for example, be a template type parameter.  */
20378       dependent_p = (TYPE_P (parser->scope)
20379                      && dependent_scope_p (parser->scope));
20380       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20381           && dependent_p)
20382         /* Defer lookup.  */
20383         decl = error_mark_node;
20384       else
20385         {
20386           tree pushed_scope = NULL_TREE;
20387
20388           /* If PARSER->SCOPE is a dependent type, then it must be a
20389              class type, and we must not be checking dependencies;
20390              otherwise, we would have processed this lookup above.  So
20391              that PARSER->SCOPE is not considered a dependent base by
20392              lookup_member, we must enter the scope here.  */
20393           if (dependent_p)
20394             pushed_scope = push_scope (parser->scope);
20395
20396           /* If the PARSER->SCOPE is a template specialization, it
20397              may be instantiated during name lookup.  In that case,
20398              errors may be issued.  Even if we rollback the current
20399              tentative parse, those errors are valid.  */
20400           decl = lookup_qualified_name (parser->scope, name,
20401                                         tag_type != none_type,
20402                                         /*complain=*/true);
20403
20404           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20405              lookup result and the nested-name-specifier nominates a class C:
20406                * if the name specified after the nested-name-specifier, when
20407                looked up in C, is the injected-class-name of C (Clause 9), or
20408                * if the name specified after the nested-name-specifier is the
20409                same as the identifier or the simple-template-id's template-
20410                name in the last component of the nested-name-specifier,
20411              the name is instead considered to name the constructor of
20412              class C. [ Note: for example, the constructor is not an
20413              acceptable lookup result in an elaborated-type-specifier so
20414              the constructor would not be used in place of the
20415              injected-class-name. --end note ] Such a constructor name
20416              shall be used only in the declarator-id of a declaration that
20417              names a constructor or in a using-declaration.  */
20418           if (tag_type == none_type
20419               && DECL_SELF_REFERENCE_P (decl)
20420               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20421             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20422                                           tag_type != none_type,
20423                                           /*complain=*/true);
20424
20425           /* If we have a single function from a using decl, pull it out.  */
20426           if (TREE_CODE (decl) == OVERLOAD
20427               && !really_overloaded_fn (decl))
20428             decl = OVL_FUNCTION (decl);
20429
20430           if (pushed_scope)
20431             pop_scope (pushed_scope);
20432         }
20433
20434       /* If the scope is a dependent type and either we deferred lookup or
20435          we did lookup but didn't find the name, rememeber the name.  */
20436       if (decl == error_mark_node && TYPE_P (parser->scope)
20437           && dependent_type_p (parser->scope))
20438         {
20439           if (tag_type)
20440             {
20441               tree type;
20442
20443               /* The resolution to Core Issue 180 says that `struct
20444                  A::B' should be considered a type-name, even if `A'
20445                  is dependent.  */
20446               type = make_typename_type (parser->scope, name, tag_type,
20447                                          /*complain=*/tf_error);
20448               decl = TYPE_NAME (type);
20449             }
20450           else if (is_template
20451                    && (cp_parser_next_token_ends_template_argument_p (parser)
20452                        || cp_lexer_next_token_is (parser->lexer,
20453                                                   CPP_CLOSE_PAREN)))
20454             decl = make_unbound_class_template (parser->scope,
20455                                                 name, NULL_TREE,
20456                                                 /*complain=*/tf_error);
20457           else
20458             decl = build_qualified_name (/*type=*/NULL_TREE,
20459                                          parser->scope, name,
20460                                          is_template);
20461         }
20462       parser->qualifying_scope = parser->scope;
20463       parser->object_scope = NULL_TREE;
20464     }
20465   else if (object_type)
20466     {
20467       tree object_decl = NULL_TREE;
20468       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20469          OBJECT_TYPE is not a class.  */
20470       if (CLASS_TYPE_P (object_type))
20471         /* If the OBJECT_TYPE is a template specialization, it may
20472            be instantiated during name lookup.  In that case, errors
20473            may be issued.  Even if we rollback the current tentative
20474            parse, those errors are valid.  */
20475         object_decl = lookup_member (object_type,
20476                                      name,
20477                                      /*protect=*/0,
20478                                      tag_type != none_type,
20479                                      tf_warning_or_error);
20480       /* Look it up in the enclosing context, too.  */
20481       decl = lookup_name_real (name, tag_type != none_type,
20482                                /*nonclass=*/0,
20483                                /*block_p=*/true, is_namespace, flags);
20484       parser->object_scope = object_type;
20485       parser->qualifying_scope = NULL_TREE;
20486       if (object_decl)
20487         decl = object_decl;
20488     }
20489   else
20490     {
20491       decl = lookup_name_real (name, tag_type != none_type,
20492                                /*nonclass=*/0,
20493                                /*block_p=*/true, is_namespace, flags);
20494       parser->qualifying_scope = NULL_TREE;
20495       parser->object_scope = NULL_TREE;
20496     }
20497
20498   /* If the lookup failed, let our caller know.  */
20499   if (!decl || decl == error_mark_node)
20500     return error_mark_node;
20501
20502   /* Pull out the template from an injected-class-name (or multiple).  */
20503   if (is_template)
20504     decl = maybe_get_template_decl_from_type_decl (decl);
20505
20506   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20507   if (TREE_CODE (decl) == TREE_LIST)
20508     {
20509       if (ambiguous_decls)
20510         *ambiguous_decls = decl;
20511       /* The error message we have to print is too complicated for
20512          cp_parser_error, so we incorporate its actions directly.  */
20513       if (!cp_parser_simulate_error (parser))
20514         {
20515           error_at (name_location, "reference to %qD is ambiguous",
20516                     name);
20517           print_candidates (decl);
20518         }
20519       return error_mark_node;
20520     }
20521
20522   gcc_assert (DECL_P (decl)
20523               || TREE_CODE (decl) == OVERLOAD
20524               || TREE_CODE (decl) == SCOPE_REF
20525               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20526               || BASELINK_P (decl));
20527
20528   /* If we have resolved the name of a member declaration, check to
20529      see if the declaration is accessible.  When the name resolves to
20530      set of overloaded functions, accessibility is checked when
20531      overload resolution is done.
20532
20533      During an explicit instantiation, access is not checked at all,
20534      as per [temp.explicit].  */
20535   if (DECL_P (decl))
20536     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20537
20538   maybe_record_typedef_use (decl);
20539
20540   return decl;
20541 }
20542
20543 /* Like cp_parser_lookup_name, but for use in the typical case where
20544    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20545    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20546
20547 static tree
20548 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20549 {
20550   return cp_parser_lookup_name (parser, name,
20551                                 none_type,
20552                                 /*is_template=*/false,
20553                                 /*is_namespace=*/false,
20554                                 /*check_dependency=*/true,
20555                                 /*ambiguous_decls=*/NULL,
20556                                 location);
20557 }
20558
20559 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20560    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20561    true, the DECL indicates the class being defined in a class-head,
20562    or declared in an elaborated-type-specifier.
20563
20564    Otherwise, return DECL.  */
20565
20566 static tree
20567 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20568 {
20569   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20570      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20571
20572        struct A {
20573          template <typename T> struct B;
20574        };
20575
20576        template <typename T> struct A::B {};
20577
20578      Similarly, in an elaborated-type-specifier:
20579
20580        namespace N { struct X{}; }
20581
20582        struct A {
20583          template <typename T> friend struct N::X;
20584        };
20585
20586      However, if the DECL refers to a class type, and we are in
20587      the scope of the class, then the name lookup automatically
20588      finds the TYPE_DECL created by build_self_reference rather
20589      than a TEMPLATE_DECL.  For example, in:
20590
20591        template <class T> struct S {
20592          S s;
20593        };
20594
20595      there is no need to handle such case.  */
20596
20597   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20598     return DECL_TEMPLATE_RESULT (decl);
20599
20600   return decl;
20601 }
20602
20603 /* If too many, or too few, template-parameter lists apply to the
20604    declarator, issue an error message.  Returns TRUE if all went well,
20605    and FALSE otherwise.  */
20606
20607 static bool
20608 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20609                                                 cp_declarator *declarator,
20610                                                 location_t declarator_location)
20611 {
20612   unsigned num_templates;
20613
20614   /* We haven't seen any classes that involve template parameters yet.  */
20615   num_templates = 0;
20616
20617   switch (declarator->kind)
20618     {
20619     case cdk_id:
20620       if (declarator->u.id.qualifying_scope)
20621         {
20622           tree scope;
20623
20624           scope = declarator->u.id.qualifying_scope;
20625
20626           while (scope && CLASS_TYPE_P (scope))
20627             {
20628               /* You're supposed to have one `template <...>'
20629                  for every template class, but you don't need one
20630                  for a full specialization.  For example:
20631
20632                  template <class T> struct S{};
20633                  template <> struct S<int> { void f(); };
20634                  void S<int>::f () {}
20635
20636                  is correct; there shouldn't be a `template <>' for
20637                  the definition of `S<int>::f'.  */
20638               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20639                 /* If SCOPE does not have template information of any
20640                    kind, then it is not a template, nor is it nested
20641                    within a template.  */
20642                 break;
20643               if (explicit_class_specialization_p (scope))
20644                 break;
20645               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20646                 ++num_templates;
20647
20648               scope = TYPE_CONTEXT (scope);
20649             }
20650         }
20651       else if (TREE_CODE (declarator->u.id.unqualified_name)
20652                == TEMPLATE_ID_EXPR)
20653         /* If the DECLARATOR has the form `X<y>' then it uses one
20654            additional level of template parameters.  */
20655         ++num_templates;
20656
20657       return cp_parser_check_template_parameters 
20658         (parser, num_templates, declarator_location, declarator);
20659
20660
20661     case cdk_function:
20662     case cdk_array:
20663     case cdk_pointer:
20664     case cdk_reference:
20665     case cdk_ptrmem:
20666       return (cp_parser_check_declarator_template_parameters
20667               (parser, declarator->declarator, declarator_location));
20668
20669     case cdk_error:
20670       return true;
20671
20672     default:
20673       gcc_unreachable ();
20674     }
20675   return false;
20676 }
20677
20678 /* NUM_TEMPLATES were used in the current declaration.  If that is
20679    invalid, return FALSE and issue an error messages.  Otherwise,
20680    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20681    declarator and we can print more accurate diagnostics.  */
20682
20683 static bool
20684 cp_parser_check_template_parameters (cp_parser* parser,
20685                                      unsigned num_templates,
20686                                      location_t location,
20687                                      cp_declarator *declarator)
20688 {
20689   /* If there are the same number of template classes and parameter
20690      lists, that's OK.  */
20691   if (parser->num_template_parameter_lists == num_templates)
20692     return true;
20693   /* If there are more, but only one more, then we are referring to a
20694      member template.  That's OK too.  */
20695   if (parser->num_template_parameter_lists == num_templates + 1)
20696     return true;
20697   /* If there are more template classes than parameter lists, we have
20698      something like:
20699
20700        template <class T> void S<T>::R<T>::f ();  */
20701   if (parser->num_template_parameter_lists < num_templates)
20702     {
20703       if (declarator && !current_function_decl)
20704         error_at (location, "specializing member %<%T::%E%> "
20705                   "requires %<template<>%> syntax", 
20706                   declarator->u.id.qualifying_scope,
20707                   declarator->u.id.unqualified_name);
20708       else if (declarator)
20709         error_at (location, "invalid declaration of %<%T::%E%>",
20710                   declarator->u.id.qualifying_scope,
20711                   declarator->u.id.unqualified_name);
20712       else 
20713         error_at (location, "too few template-parameter-lists");
20714       return false;
20715     }
20716   /* Otherwise, there are too many template parameter lists.  We have
20717      something like:
20718
20719      template <class T> template <class U> void S::f();  */
20720   error_at (location, "too many template-parameter-lists");
20721   return false;
20722 }
20723
20724 /* Parse an optional `::' token indicating that the following name is
20725    from the global namespace.  If so, PARSER->SCOPE is set to the
20726    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20727    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20728    Returns the new value of PARSER->SCOPE, if the `::' token is
20729    present, and NULL_TREE otherwise.  */
20730
20731 static tree
20732 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20733 {
20734   cp_token *token;
20735
20736   /* Peek at the next token.  */
20737   token = cp_lexer_peek_token (parser->lexer);
20738   /* If we're looking at a `::' token then we're starting from the
20739      global namespace, not our current location.  */
20740   if (token->type == CPP_SCOPE)
20741     {
20742       /* Consume the `::' token.  */
20743       cp_lexer_consume_token (parser->lexer);
20744       /* Set the SCOPE so that we know where to start the lookup.  */
20745       parser->scope = global_namespace;
20746       parser->qualifying_scope = global_namespace;
20747       parser->object_scope = NULL_TREE;
20748
20749       return parser->scope;
20750     }
20751   else if (!current_scope_valid_p)
20752     {
20753       parser->scope = NULL_TREE;
20754       parser->qualifying_scope = NULL_TREE;
20755       parser->object_scope = NULL_TREE;
20756     }
20757
20758   return NULL_TREE;
20759 }
20760
20761 /* Returns TRUE if the upcoming token sequence is the start of a
20762    constructor declarator.  If FRIEND_P is true, the declarator is
20763    preceded by the `friend' specifier.  */
20764
20765 static bool
20766 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20767 {
20768   bool constructor_p;
20769   tree nested_name_specifier;
20770   cp_token *next_token;
20771
20772   /* The common case is that this is not a constructor declarator, so
20773      try to avoid doing lots of work if at all possible.  It's not
20774      valid declare a constructor at function scope.  */
20775   if (parser->in_function_body)
20776     return false;
20777   /* And only certain tokens can begin a constructor declarator.  */
20778   next_token = cp_lexer_peek_token (parser->lexer);
20779   if (next_token->type != CPP_NAME
20780       && next_token->type != CPP_SCOPE
20781       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20782       && next_token->type != CPP_TEMPLATE_ID)
20783     return false;
20784
20785   /* Parse tentatively; we are going to roll back all of the tokens
20786      consumed here.  */
20787   cp_parser_parse_tentatively (parser);
20788   /* Assume that we are looking at a constructor declarator.  */
20789   constructor_p = true;
20790
20791   /* Look for the optional `::' operator.  */
20792   cp_parser_global_scope_opt (parser,
20793                               /*current_scope_valid_p=*/false);
20794   /* Look for the nested-name-specifier.  */
20795   nested_name_specifier
20796     = (cp_parser_nested_name_specifier_opt (parser,
20797                                             /*typename_keyword_p=*/false,
20798                                             /*check_dependency_p=*/false,
20799                                             /*type_p=*/false,
20800                                             /*is_declaration=*/false));
20801   /* Outside of a class-specifier, there must be a
20802      nested-name-specifier.  */
20803   if (!nested_name_specifier &&
20804       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20805        || friend_p))
20806     constructor_p = false;
20807   else if (nested_name_specifier == error_mark_node)
20808     constructor_p = false;
20809
20810   /* If we have a class scope, this is easy; DR 147 says that S::S always
20811      names the constructor, and no other qualified name could.  */
20812   if (constructor_p && nested_name_specifier
20813       && CLASS_TYPE_P (nested_name_specifier))
20814     {
20815       tree id = cp_parser_unqualified_id (parser,
20816                                           /*template_keyword_p=*/false,
20817                                           /*check_dependency_p=*/false,
20818                                           /*declarator_p=*/true,
20819                                           /*optional_p=*/false);
20820       if (is_overloaded_fn (id))
20821         id = DECL_NAME (get_first_fn (id));
20822       if (!constructor_name_p (id, nested_name_specifier))
20823         constructor_p = false;
20824     }
20825   /* If we still think that this might be a constructor-declarator,
20826      look for a class-name.  */
20827   else if (constructor_p)
20828     {
20829       /* If we have:
20830
20831            template <typename T> struct S {
20832              S();
20833            };
20834
20835          we must recognize that the nested `S' names a class.  */
20836       tree type_decl;
20837       type_decl = cp_parser_class_name (parser,
20838                                         /*typename_keyword_p=*/false,
20839                                         /*template_keyword_p=*/false,
20840                                         none_type,
20841                                         /*check_dependency_p=*/false,
20842                                         /*class_head_p=*/false,
20843                                         /*is_declaration=*/false);
20844       /* If there was no class-name, then this is not a constructor.  */
20845       constructor_p = !cp_parser_error_occurred (parser);
20846
20847       /* If we're still considering a constructor, we have to see a `(',
20848          to begin the parameter-declaration-clause, followed by either a
20849          `)', an `...', or a decl-specifier.  We need to check for a
20850          type-specifier to avoid being fooled into thinking that:
20851
20852            S (f) (int);
20853
20854          is a constructor.  (It is actually a function named `f' that
20855          takes one parameter (of type `int') and returns a value of type
20856          `S'.  */
20857       if (constructor_p
20858           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20859         constructor_p = false;
20860
20861       if (constructor_p
20862           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20863           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20864           /* A parameter declaration begins with a decl-specifier,
20865              which is either the "attribute" keyword, a storage class
20866              specifier, or (usually) a type-specifier.  */
20867           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20868         {
20869           tree type;
20870           tree pushed_scope = NULL_TREE;
20871           unsigned saved_num_template_parameter_lists;
20872
20873           /* Names appearing in the type-specifier should be looked up
20874              in the scope of the class.  */
20875           if (current_class_type)
20876             type = NULL_TREE;
20877           else
20878             {
20879               type = TREE_TYPE (type_decl);
20880               if (TREE_CODE (type) == TYPENAME_TYPE)
20881                 {
20882                   type = resolve_typename_type (type,
20883                                                 /*only_current_p=*/false);
20884                   if (TREE_CODE (type) == TYPENAME_TYPE)
20885                     {
20886                       cp_parser_abort_tentative_parse (parser);
20887                       return false;
20888                     }
20889                 }
20890               pushed_scope = push_scope (type);
20891             }
20892
20893           /* Inside the constructor parameter list, surrounding
20894              template-parameter-lists do not apply.  */
20895           saved_num_template_parameter_lists
20896             = parser->num_template_parameter_lists;
20897           parser->num_template_parameter_lists = 0;
20898
20899           /* Look for the type-specifier.  */
20900           cp_parser_type_specifier (parser,
20901                                     CP_PARSER_FLAGS_NONE,
20902                                     /*decl_specs=*/NULL,
20903                                     /*is_declarator=*/true,
20904                                     /*declares_class_or_enum=*/NULL,
20905                                     /*is_cv_qualifier=*/NULL);
20906
20907           parser->num_template_parameter_lists
20908             = saved_num_template_parameter_lists;
20909
20910           /* Leave the scope of the class.  */
20911           if (pushed_scope)
20912             pop_scope (pushed_scope);
20913
20914           constructor_p = !cp_parser_error_occurred (parser);
20915         }
20916     }
20917
20918   /* We did not really want to consume any tokens.  */
20919   cp_parser_abort_tentative_parse (parser);
20920
20921   return constructor_p;
20922 }
20923
20924 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20925    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20926    they must be performed once we are in the scope of the function.
20927
20928    Returns the function defined.  */
20929
20930 static tree
20931 cp_parser_function_definition_from_specifiers_and_declarator
20932   (cp_parser* parser,
20933    cp_decl_specifier_seq *decl_specifiers,
20934    tree attributes,
20935    const cp_declarator *declarator)
20936 {
20937   tree fn;
20938   bool success_p;
20939
20940   /* Begin the function-definition.  */
20941   success_p = start_function (decl_specifiers, declarator, attributes);
20942
20943   /* The things we're about to see are not directly qualified by any
20944      template headers we've seen thus far.  */
20945   reset_specialization ();
20946
20947   /* If there were names looked up in the decl-specifier-seq that we
20948      did not check, check them now.  We must wait until we are in the
20949      scope of the function to perform the checks, since the function
20950      might be a friend.  */
20951   perform_deferred_access_checks ();
20952
20953   if (!success_p)
20954     {
20955       /* Skip the entire function.  */
20956       cp_parser_skip_to_end_of_block_or_statement (parser);
20957       fn = error_mark_node;
20958     }
20959   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20960     {
20961       /* Seen already, skip it.  An error message has already been output.  */
20962       cp_parser_skip_to_end_of_block_or_statement (parser);
20963       fn = current_function_decl;
20964       current_function_decl = NULL_TREE;
20965       /* If this is a function from a class, pop the nested class.  */
20966       if (current_class_name)
20967         pop_nested_class ();
20968     }
20969   else
20970     {
20971       timevar_id_t tv;
20972       if (DECL_DECLARED_INLINE_P (current_function_decl))
20973         tv = TV_PARSE_INLINE;
20974       else
20975         tv = TV_PARSE_FUNC;
20976       timevar_push (tv);
20977       fn = cp_parser_function_definition_after_declarator (parser,
20978                                                          /*inline_p=*/false);
20979       timevar_pop (tv);
20980     }
20981
20982   return fn;
20983 }
20984
20985 /* Parse the part of a function-definition that follows the
20986    declarator.  INLINE_P is TRUE iff this function is an inline
20987    function defined within a class-specifier.
20988
20989    Returns the function defined.  */
20990
20991 static tree
20992 cp_parser_function_definition_after_declarator (cp_parser* parser,
20993                                                 bool inline_p)
20994 {
20995   tree fn;
20996   bool ctor_initializer_p = false;
20997   bool saved_in_unbraced_linkage_specification_p;
20998   bool saved_in_function_body;
20999   unsigned saved_num_template_parameter_lists;
21000   cp_token *token;
21001
21002   saved_in_function_body = parser->in_function_body;
21003   parser->in_function_body = true;
21004   /* If the next token is `return', then the code may be trying to
21005      make use of the "named return value" extension that G++ used to
21006      support.  */
21007   token = cp_lexer_peek_token (parser->lexer);
21008   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21009     {
21010       /* Consume the `return' keyword.  */
21011       cp_lexer_consume_token (parser->lexer);
21012       /* Look for the identifier that indicates what value is to be
21013          returned.  */
21014       cp_parser_identifier (parser);
21015       /* Issue an error message.  */
21016       error_at (token->location,
21017                 "named return values are no longer supported");
21018       /* Skip tokens until we reach the start of the function body.  */
21019       while (true)
21020         {
21021           cp_token *token = cp_lexer_peek_token (parser->lexer);
21022           if (token->type == CPP_OPEN_BRACE
21023               || token->type == CPP_EOF
21024               || token->type == CPP_PRAGMA_EOL)
21025             break;
21026           cp_lexer_consume_token (parser->lexer);
21027         }
21028     }
21029   /* The `extern' in `extern "C" void f () { ... }' does not apply to
21030      anything declared inside `f'.  */
21031   saved_in_unbraced_linkage_specification_p
21032     = parser->in_unbraced_linkage_specification_p;
21033   parser->in_unbraced_linkage_specification_p = false;
21034   /* Inside the function, surrounding template-parameter-lists do not
21035      apply.  */
21036   saved_num_template_parameter_lists
21037     = parser->num_template_parameter_lists;
21038   parser->num_template_parameter_lists = 0;
21039
21040   start_lambda_scope (current_function_decl);
21041
21042   /* If the next token is `try', `__transaction_atomic', or
21043      `__transaction_relaxed`, then we are looking at either function-try-block
21044      or function-transaction-block.  Note that all of these include the
21045      function-body.  */
21046   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21047     ctor_initializer_p = cp_parser_function_transaction (parser,
21048         RID_TRANSACTION_ATOMIC);
21049   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21050       RID_TRANSACTION_RELAXED))
21051     ctor_initializer_p = cp_parser_function_transaction (parser,
21052         RID_TRANSACTION_RELAXED);
21053   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21054     ctor_initializer_p = cp_parser_function_try_block (parser);
21055   else
21056     ctor_initializer_p
21057       = cp_parser_ctor_initializer_opt_and_function_body (parser);
21058
21059   finish_lambda_scope ();
21060
21061   /* Finish the function.  */
21062   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21063                         (inline_p ? 2 : 0));
21064   /* Generate code for it, if necessary.  */
21065   expand_or_defer_fn (fn);
21066   /* Restore the saved values.  */
21067   parser->in_unbraced_linkage_specification_p
21068     = saved_in_unbraced_linkage_specification_p;
21069   parser->num_template_parameter_lists
21070     = saved_num_template_parameter_lists;
21071   parser->in_function_body = saved_in_function_body;
21072
21073   return fn;
21074 }
21075
21076 /* Parse a template-declaration, assuming that the `export' (and
21077    `extern') keywords, if present, has already been scanned.  MEMBER_P
21078    is as for cp_parser_template_declaration.  */
21079
21080 static void
21081 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21082 {
21083   tree decl = NULL_TREE;
21084   VEC (deferred_access_check,gc) *checks;
21085   tree parameter_list;
21086   bool friend_p = false;
21087   bool need_lang_pop;
21088   cp_token *token;
21089
21090   /* Look for the `template' keyword.  */
21091   token = cp_lexer_peek_token (parser->lexer);
21092   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21093     return;
21094
21095   /* And the `<'.  */
21096   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21097     return;
21098   if (at_class_scope_p () && current_function_decl)
21099     {
21100       /* 14.5.2.2 [temp.mem]
21101
21102          A local class shall not have member templates.  */
21103       error_at (token->location,
21104                 "invalid declaration of member template in local class");
21105       cp_parser_skip_to_end_of_block_or_statement (parser);
21106       return;
21107     }
21108   /* [temp]
21109
21110      A template ... shall not have C linkage.  */
21111   if (current_lang_name == lang_name_c)
21112     {
21113       error_at (token->location, "template with C linkage");
21114       /* Give it C++ linkage to avoid confusing other parts of the
21115          front end.  */
21116       push_lang_context (lang_name_cplusplus);
21117       need_lang_pop = true;
21118     }
21119   else
21120     need_lang_pop = false;
21121
21122   /* We cannot perform access checks on the template parameter
21123      declarations until we know what is being declared, just as we
21124      cannot check the decl-specifier list.  */
21125   push_deferring_access_checks (dk_deferred);
21126
21127   /* If the next token is `>', then we have an invalid
21128      specialization.  Rather than complain about an invalid template
21129      parameter, issue an error message here.  */
21130   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21131     {
21132       cp_parser_error (parser, "invalid explicit specialization");
21133       begin_specialization ();
21134       parameter_list = NULL_TREE;
21135     }
21136   else
21137     {
21138       /* Parse the template parameters.  */
21139       parameter_list = cp_parser_template_parameter_list (parser);
21140       fixup_template_parms ();
21141     }
21142
21143   /* Get the deferred access checks from the parameter list.  These
21144      will be checked once we know what is being declared, as for a
21145      member template the checks must be performed in the scope of the
21146      class containing the member.  */
21147   checks = get_deferred_access_checks ();
21148
21149   /* Look for the `>'.  */
21150   cp_parser_skip_to_end_of_template_parameter_list (parser);
21151   /* We just processed one more parameter list.  */
21152   ++parser->num_template_parameter_lists;
21153   /* If the next token is `template', there are more template
21154      parameters.  */
21155   if (cp_lexer_next_token_is_keyword (parser->lexer,
21156                                       RID_TEMPLATE))
21157     cp_parser_template_declaration_after_export (parser, member_p);
21158   else if (cxx_dialect >= cxx0x
21159            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21160     decl = cp_parser_alias_declaration (parser);
21161   else
21162     {
21163       /* There are no access checks when parsing a template, as we do not
21164          know if a specialization will be a friend.  */
21165       push_deferring_access_checks (dk_no_check);
21166       token = cp_lexer_peek_token (parser->lexer);
21167       decl = cp_parser_single_declaration (parser,
21168                                            checks,
21169                                            member_p,
21170                                            /*explicit_specialization_p=*/false,
21171                                            &friend_p);
21172       pop_deferring_access_checks ();
21173
21174       /* If this is a member template declaration, let the front
21175          end know.  */
21176       if (member_p && !friend_p && decl)
21177         {
21178           if (TREE_CODE (decl) == TYPE_DECL)
21179             cp_parser_check_access_in_redeclaration (decl, token->location);
21180
21181           decl = finish_member_template_decl (decl);
21182         }
21183       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21184         make_friend_class (current_class_type, TREE_TYPE (decl),
21185                            /*complain=*/true);
21186     }
21187   /* We are done with the current parameter list.  */
21188   --parser->num_template_parameter_lists;
21189
21190   pop_deferring_access_checks ();
21191
21192   /* Finish up.  */
21193   finish_template_decl (parameter_list);
21194
21195   /* Check the template arguments for a literal operator template.  */
21196   if (decl
21197       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21198       && UDLIT_OPER_P (DECL_NAME (decl)))
21199     {
21200       bool ok = true;
21201       if (parameter_list == NULL_TREE)
21202         ok = false;
21203       else
21204         {
21205           int num_parms = TREE_VEC_LENGTH (parameter_list);
21206           if (num_parms != 1)
21207             ok = false;
21208           else
21209             {
21210               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21211               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21212               if (TREE_TYPE (parm) != char_type_node
21213                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21214                 ok = false;
21215             }
21216         }
21217       if (!ok)
21218         error ("literal operator template %qD has invalid parameter list."
21219                "  Expected non-type template argument pack <char...>",
21220                decl);
21221     }
21222   /* Register member declarations.  */
21223   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21224     finish_member_declaration (decl);
21225   /* For the erroneous case of a template with C linkage, we pushed an
21226      implicit C++ linkage scope; exit that scope now.  */
21227   if (need_lang_pop)
21228     pop_lang_context ();
21229   /* If DECL is a function template, we must return to parse it later.
21230      (Even though there is no definition, there might be default
21231      arguments that need handling.)  */
21232   if (member_p && decl
21233       && (TREE_CODE (decl) == FUNCTION_DECL
21234           || DECL_FUNCTION_TEMPLATE_P (decl)))
21235     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21236 }
21237
21238 /* Perform the deferred access checks from a template-parameter-list.
21239    CHECKS is a TREE_LIST of access checks, as returned by
21240    get_deferred_access_checks.  */
21241
21242 static void
21243 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21244 {
21245   ++processing_template_parmlist;
21246   perform_access_checks (checks);
21247   --processing_template_parmlist;
21248 }
21249
21250 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21251    `function-definition' sequence.  MEMBER_P is true, this declaration
21252    appears in a class scope.
21253
21254    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21255    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21256
21257 static tree
21258 cp_parser_single_declaration (cp_parser* parser,
21259                               VEC (deferred_access_check,gc)* checks,
21260                               bool member_p,
21261                               bool explicit_specialization_p,
21262                               bool* friend_p)
21263 {
21264   int declares_class_or_enum;
21265   tree decl = NULL_TREE;
21266   cp_decl_specifier_seq decl_specifiers;
21267   bool function_definition_p = false;
21268   cp_token *decl_spec_token_start;
21269
21270   /* This function is only used when processing a template
21271      declaration.  */
21272   gcc_assert (innermost_scope_kind () == sk_template_parms
21273               || innermost_scope_kind () == sk_template_spec);
21274
21275   /* Defer access checks until we know what is being declared.  */
21276   push_deferring_access_checks (dk_deferred);
21277
21278   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21279      alternative.  */
21280   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21281   cp_parser_decl_specifier_seq (parser,
21282                                 CP_PARSER_FLAGS_OPTIONAL,
21283                                 &decl_specifiers,
21284                                 &declares_class_or_enum);
21285   if (friend_p)
21286     *friend_p = cp_parser_friend_p (&decl_specifiers);
21287
21288   /* There are no template typedefs.  */
21289   if (decl_specifiers.specs[(int) ds_typedef])
21290     {
21291       error_at (decl_spec_token_start->location,
21292                 "template declaration of %<typedef%>");
21293       decl = error_mark_node;
21294     }
21295
21296   /* Gather up the access checks that occurred the
21297      decl-specifier-seq.  */
21298   stop_deferring_access_checks ();
21299
21300   /* Check for the declaration of a template class.  */
21301   if (declares_class_or_enum)
21302     {
21303       if (cp_parser_declares_only_class_p (parser))
21304         {
21305           decl = shadow_tag (&decl_specifiers);
21306
21307           /* In this case:
21308
21309                struct C {
21310                  friend template <typename T> struct A<T>::B;
21311                };
21312
21313              A<T>::B will be represented by a TYPENAME_TYPE, and
21314              therefore not recognized by shadow_tag.  */
21315           if (friend_p && *friend_p
21316               && !decl
21317               && decl_specifiers.type
21318               && TYPE_P (decl_specifiers.type))
21319             decl = decl_specifiers.type;
21320
21321           if (decl && decl != error_mark_node)
21322             decl = TYPE_NAME (decl);
21323           else
21324             decl = error_mark_node;
21325
21326           /* Perform access checks for template parameters.  */
21327           cp_parser_perform_template_parameter_access_checks (checks);
21328         }
21329     }
21330
21331   /* Complain about missing 'typename' or other invalid type names.  */
21332   if (!decl_specifiers.any_type_specifiers_p
21333       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21334     {
21335       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21336          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21337          the rest of this declaration.  */
21338       decl = error_mark_node;
21339       goto out;
21340     }
21341
21342   /* If it's not a template class, try for a template function.  If
21343      the next token is a `;', then this declaration does not declare
21344      anything.  But, if there were errors in the decl-specifiers, then
21345      the error might well have come from an attempted class-specifier.
21346      In that case, there's no need to warn about a missing declarator.  */
21347   if (!decl
21348       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21349           || decl_specifiers.type != error_mark_node))
21350     {
21351       decl = cp_parser_init_declarator (parser,
21352                                         &decl_specifiers,
21353                                         checks,
21354                                         /*function_definition_allowed_p=*/true,
21355                                         member_p,
21356                                         declares_class_or_enum,
21357                                         &function_definition_p,
21358                                         NULL);
21359
21360     /* 7.1.1-1 [dcl.stc]
21361
21362        A storage-class-specifier shall not be specified in an explicit
21363        specialization...  */
21364     if (decl
21365         && explicit_specialization_p
21366         && decl_specifiers.storage_class != sc_none)
21367       {
21368         error_at (decl_spec_token_start->location,
21369                   "explicit template specialization cannot have a storage class");
21370         decl = error_mark_node;
21371       }
21372     }
21373
21374   /* Look for a trailing `;' after the declaration.  */
21375   if (!function_definition_p
21376       && (decl == error_mark_node
21377           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21378     cp_parser_skip_to_end_of_block_or_statement (parser);
21379
21380  out:
21381   pop_deferring_access_checks ();
21382
21383   /* Clear any current qualification; whatever comes next is the start
21384      of something new.  */
21385   parser->scope = NULL_TREE;
21386   parser->qualifying_scope = NULL_TREE;
21387   parser->object_scope = NULL_TREE;
21388
21389   return decl;
21390 }
21391
21392 /* Parse a cast-expression that is not the operand of a unary "&".  */
21393
21394 static tree
21395 cp_parser_simple_cast_expression (cp_parser *parser)
21396 {
21397   return cp_parser_cast_expression (parser, /*address_p=*/false,
21398                                     /*cast_p=*/false, NULL);
21399 }
21400
21401 /* Parse a functional cast to TYPE.  Returns an expression
21402    representing the cast.  */
21403
21404 static tree
21405 cp_parser_functional_cast (cp_parser* parser, tree type)
21406 {
21407   VEC(tree,gc) *vec;
21408   tree expression_list;
21409   tree cast;
21410   bool nonconst_p;
21411
21412   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21413     {
21414       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21415       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21416       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21417       if (TREE_CODE (type) == TYPE_DECL)
21418         type = TREE_TYPE (type);
21419       return finish_compound_literal (type, expression_list,
21420                                       tf_warning_or_error);
21421     }
21422
21423
21424   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21425                                                  /*cast_p=*/true,
21426                                                  /*allow_expansion_p=*/true,
21427                                                  /*non_constant_p=*/NULL);
21428   if (vec == NULL)
21429     expression_list = error_mark_node;
21430   else
21431     {
21432       expression_list = build_tree_list_vec (vec);
21433       release_tree_vector (vec);
21434     }
21435
21436   cast = build_functional_cast (type, expression_list,
21437                                 tf_warning_or_error);
21438   /* [expr.const]/1: In an integral constant expression "only type
21439      conversions to integral or enumeration type can be used".  */
21440   if (TREE_CODE (type) == TYPE_DECL)
21441     type = TREE_TYPE (type);
21442   if (cast != error_mark_node
21443       && !cast_valid_in_integral_constant_expression_p (type)
21444       && cp_parser_non_integral_constant_expression (parser,
21445                                                      NIC_CONSTRUCTOR))
21446     return error_mark_node;
21447   return cast;
21448 }
21449
21450 /* Save the tokens that make up the body of a member function defined
21451    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21452    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21453    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21454    for the member function.  */
21455
21456 static tree
21457 cp_parser_save_member_function_body (cp_parser* parser,
21458                                      cp_decl_specifier_seq *decl_specifiers,
21459                                      cp_declarator *declarator,
21460                                      tree attributes)
21461 {
21462   cp_token *first;
21463   cp_token *last;
21464   tree fn;
21465
21466   /* Create the FUNCTION_DECL.  */
21467   fn = grokmethod (decl_specifiers, declarator, attributes);
21468   /* If something went badly wrong, bail out now.  */
21469   if (fn == error_mark_node)
21470     {
21471       /* If there's a function-body, skip it.  */
21472       if (cp_parser_token_starts_function_definition_p
21473           (cp_lexer_peek_token (parser->lexer)))
21474         cp_parser_skip_to_end_of_block_or_statement (parser);
21475       return error_mark_node;
21476     }
21477
21478   /* Remember it, if there default args to post process.  */
21479   cp_parser_save_default_args (parser, fn);
21480
21481   /* Save away the tokens that make up the body of the
21482      function.  */
21483   first = parser->lexer->next_token;
21484   /* We can have braced-init-list mem-initializers before the fn body.  */
21485   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21486     {
21487       cp_lexer_consume_token (parser->lexer);
21488       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21489              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21490         {
21491           /* cache_group will stop after an un-nested { } pair, too.  */
21492           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21493             break;
21494
21495           /* variadic mem-inits have ... after the ')'.  */
21496           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21497             cp_lexer_consume_token (parser->lexer);
21498         }
21499     }
21500   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21501   /* Handle function try blocks.  */
21502   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21503     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21504   last = parser->lexer->next_token;
21505
21506   /* Save away the inline definition; we will process it when the
21507      class is complete.  */
21508   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21509   DECL_PENDING_INLINE_P (fn) = 1;
21510
21511   /* We need to know that this was defined in the class, so that
21512      friend templates are handled correctly.  */
21513   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21514
21515   /* Add FN to the queue of functions to be parsed later.  */
21516   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21517
21518   return fn;
21519 }
21520
21521 /* Save the tokens that make up the in-class initializer for a non-static
21522    data member.  Returns a DEFAULT_ARG.  */
21523
21524 static tree
21525 cp_parser_save_nsdmi (cp_parser* parser)
21526 {
21527   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21528 }
21529
21530 /* Parse a template-argument-list, as well as the trailing ">" (but
21531    not the opening "<").  See cp_parser_template_argument_list for the
21532    return value.  */
21533
21534 static tree
21535 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21536 {
21537   tree arguments;
21538   tree saved_scope;
21539   tree saved_qualifying_scope;
21540   tree saved_object_scope;
21541   bool saved_greater_than_is_operator_p;
21542   int saved_unevaluated_operand;
21543   int saved_inhibit_evaluation_warnings;
21544
21545   /* [temp.names]
21546
21547      When parsing a template-id, the first non-nested `>' is taken as
21548      the end of the template-argument-list rather than a greater-than
21549      operator.  */
21550   saved_greater_than_is_operator_p
21551     = parser->greater_than_is_operator_p;
21552   parser->greater_than_is_operator_p = false;
21553   /* Parsing the argument list may modify SCOPE, so we save it
21554      here.  */
21555   saved_scope = parser->scope;
21556   saved_qualifying_scope = parser->qualifying_scope;
21557   saved_object_scope = parser->object_scope;
21558   /* We need to evaluate the template arguments, even though this
21559      template-id may be nested within a "sizeof".  */
21560   saved_unevaluated_operand = cp_unevaluated_operand;
21561   cp_unevaluated_operand = 0;
21562   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21563   c_inhibit_evaluation_warnings = 0;
21564   /* Parse the template-argument-list itself.  */
21565   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21566       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21567     arguments = NULL_TREE;
21568   else
21569     arguments = cp_parser_template_argument_list (parser);
21570   /* Look for the `>' that ends the template-argument-list. If we find
21571      a '>>' instead, it's probably just a typo.  */
21572   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21573     {
21574       if (cxx_dialect != cxx98)
21575         {
21576           /* In C++0x, a `>>' in a template argument list or cast
21577              expression is considered to be two separate `>'
21578              tokens. So, change the current token to a `>', but don't
21579              consume it: it will be consumed later when the outer
21580              template argument list (or cast expression) is parsed.
21581              Note that this replacement of `>' for `>>' is necessary
21582              even if we are parsing tentatively: in the tentative
21583              case, after calling
21584              cp_parser_enclosed_template_argument_list we will always
21585              throw away all of the template arguments and the first
21586              closing `>', either because the template argument list
21587              was erroneous or because we are replacing those tokens
21588              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21589              not have been thrown away) is needed either to close an
21590              outer template argument list or to complete a new-style
21591              cast.  */
21592           cp_token *token = cp_lexer_peek_token (parser->lexer);
21593           token->type = CPP_GREATER;
21594         }
21595       else if (!saved_greater_than_is_operator_p)
21596         {
21597           /* If we're in a nested template argument list, the '>>' has
21598             to be a typo for '> >'. We emit the error message, but we
21599             continue parsing and we push a '>' as next token, so that
21600             the argument list will be parsed correctly.  Note that the
21601             global source location is still on the token before the
21602             '>>', so we need to say explicitly where we want it.  */
21603           cp_token *token = cp_lexer_peek_token (parser->lexer);
21604           error_at (token->location, "%<>>%> should be %<> >%> "
21605                     "within a nested template argument list");
21606
21607           token->type = CPP_GREATER;
21608         }
21609       else
21610         {
21611           /* If this is not a nested template argument list, the '>>'
21612             is a typo for '>'. Emit an error message and continue.
21613             Same deal about the token location, but here we can get it
21614             right by consuming the '>>' before issuing the diagnostic.  */
21615           cp_token *token = cp_lexer_consume_token (parser->lexer);
21616           error_at (token->location,
21617                     "spurious %<>>%>, use %<>%> to terminate "
21618                     "a template argument list");
21619         }
21620     }
21621   else
21622     cp_parser_skip_to_end_of_template_parameter_list (parser);
21623   /* The `>' token might be a greater-than operator again now.  */
21624   parser->greater_than_is_operator_p
21625     = saved_greater_than_is_operator_p;
21626   /* Restore the SAVED_SCOPE.  */
21627   parser->scope = saved_scope;
21628   parser->qualifying_scope = saved_qualifying_scope;
21629   parser->object_scope = saved_object_scope;
21630   cp_unevaluated_operand = saved_unevaluated_operand;
21631   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21632
21633   return arguments;
21634 }
21635
21636 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21637    arguments, or the body of the function have not yet been parsed,
21638    parse them now.  */
21639
21640 static void
21641 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21642 {
21643   timevar_push (TV_PARSE_INMETH);
21644   /* If this member is a template, get the underlying
21645      FUNCTION_DECL.  */
21646   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21647     member_function = DECL_TEMPLATE_RESULT (member_function);
21648
21649   /* There should not be any class definitions in progress at this
21650      point; the bodies of members are only parsed outside of all class
21651      definitions.  */
21652   gcc_assert (parser->num_classes_being_defined == 0);
21653   /* While we're parsing the member functions we might encounter more
21654      classes.  We want to handle them right away, but we don't want
21655      them getting mixed up with functions that are currently in the
21656      queue.  */
21657   push_unparsed_function_queues (parser);
21658
21659   /* Make sure that any template parameters are in scope.  */
21660   maybe_begin_member_template_processing (member_function);
21661
21662   /* If the body of the function has not yet been parsed, parse it
21663      now.  */
21664   if (DECL_PENDING_INLINE_P (member_function))
21665     {
21666       tree function_scope;
21667       cp_token_cache *tokens;
21668
21669       /* The function is no longer pending; we are processing it.  */
21670       tokens = DECL_PENDING_INLINE_INFO (member_function);
21671       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21672       DECL_PENDING_INLINE_P (member_function) = 0;
21673
21674       /* If this is a local class, enter the scope of the containing
21675          function.  */
21676       function_scope = current_function_decl;
21677       if (function_scope)
21678         push_function_context ();
21679
21680       /* Push the body of the function onto the lexer stack.  */
21681       cp_parser_push_lexer_for_tokens (parser, tokens);
21682
21683       /* Let the front end know that we going to be defining this
21684          function.  */
21685       start_preparsed_function (member_function, NULL_TREE,
21686                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21687
21688       /* Don't do access checking if it is a templated function.  */
21689       if (processing_template_decl)
21690         push_deferring_access_checks (dk_no_check);
21691
21692       /* Now, parse the body of the function.  */
21693       cp_parser_function_definition_after_declarator (parser,
21694                                                       /*inline_p=*/true);
21695
21696       if (processing_template_decl)
21697         pop_deferring_access_checks ();
21698
21699       /* Leave the scope of the containing function.  */
21700       if (function_scope)
21701         pop_function_context ();
21702       cp_parser_pop_lexer (parser);
21703     }
21704
21705   /* Remove any template parameters from the symbol table.  */
21706   maybe_end_member_template_processing ();
21707
21708   /* Restore the queue.  */
21709   pop_unparsed_function_queues (parser);
21710   timevar_pop (TV_PARSE_INMETH);
21711 }
21712
21713 /* If DECL contains any default args, remember it on the unparsed
21714    functions queue.  */
21715
21716 static void
21717 cp_parser_save_default_args (cp_parser* parser, tree decl)
21718 {
21719   tree probe;
21720
21721   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21722        probe;
21723        probe = TREE_CHAIN (probe))
21724     if (TREE_PURPOSE (probe))
21725       {
21726         cp_default_arg_entry *entry
21727           = VEC_safe_push (cp_default_arg_entry, gc,
21728                            unparsed_funs_with_default_args, NULL);
21729         entry->class_type = current_class_type;
21730         entry->decl = decl;
21731         break;
21732       }
21733 }
21734
21735 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21736    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21737    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21738    from the parameter-type-list.  */
21739
21740 static tree
21741 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21742                                       tree default_arg, tree parmtype)
21743 {
21744   cp_token_cache *tokens;
21745   tree parsed_arg;
21746   bool dummy;
21747
21748   if (default_arg == error_mark_node)
21749     return error_mark_node;
21750
21751   /* Push the saved tokens for the default argument onto the parser's
21752      lexer stack.  */
21753   tokens = DEFARG_TOKENS (default_arg);
21754   cp_parser_push_lexer_for_tokens (parser, tokens);
21755
21756   start_lambda_scope (decl);
21757
21758   /* Parse the default argument.  */
21759   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21760   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21761     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21762
21763   finish_lambda_scope ();
21764
21765   if (!processing_template_decl)
21766     {
21767       /* In a non-template class, check conversions now.  In a template,
21768          we'll wait and instantiate these as needed.  */
21769       if (TREE_CODE (decl) == PARM_DECL)
21770         parsed_arg = check_default_argument (parmtype, parsed_arg);
21771       else
21772         {
21773           int flags = LOOKUP_IMPLICIT;
21774           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21775               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21776             flags = LOOKUP_NORMAL;
21777           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21778         }
21779     }
21780
21781   /* If the token stream has not been completely used up, then
21782      there was extra junk after the end of the default
21783      argument.  */
21784   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21785     {
21786       if (TREE_CODE (decl) == PARM_DECL)
21787         cp_parser_error (parser, "expected %<,%>");
21788       else
21789         cp_parser_error (parser, "expected %<;%>");
21790     }
21791
21792   /* Revert to the main lexer.  */
21793   cp_parser_pop_lexer (parser);
21794
21795   return parsed_arg;
21796 }
21797
21798 /* FIELD is a non-static data member with an initializer which we saved for
21799    later; parse it now.  */
21800
21801 static void
21802 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21803 {
21804   tree def;
21805
21806   push_unparsed_function_queues (parser);
21807   def = cp_parser_late_parse_one_default_arg (parser, field,
21808                                               DECL_INITIAL (field),
21809                                               NULL_TREE);
21810   pop_unparsed_function_queues (parser);
21811
21812   DECL_INITIAL (field) = def;
21813 }
21814
21815 /* FN is a FUNCTION_DECL which may contains a parameter with an
21816    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21817    assumes that the current scope is the scope in which the default
21818    argument should be processed.  */
21819
21820 static void
21821 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21822 {
21823   bool saved_local_variables_forbidden_p;
21824   tree parm, parmdecl;
21825
21826   /* While we're parsing the default args, we might (due to the
21827      statement expression extension) encounter more classes.  We want
21828      to handle them right away, but we don't want them getting mixed
21829      up with default args that are currently in the queue.  */
21830   push_unparsed_function_queues (parser);
21831
21832   /* Local variable names (and the `this' keyword) may not appear
21833      in a default argument.  */
21834   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21835   parser->local_variables_forbidden_p = true;
21836
21837   push_defarg_context (fn);
21838
21839   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21840          parmdecl = DECL_ARGUMENTS (fn);
21841        parm && parm != void_list_node;
21842        parm = TREE_CHAIN (parm),
21843          parmdecl = DECL_CHAIN (parmdecl))
21844     {
21845       tree default_arg = TREE_PURPOSE (parm);
21846       tree parsed_arg;
21847       VEC(tree,gc) *insts;
21848       tree copy;
21849       unsigned ix;
21850
21851       if (!default_arg)
21852         continue;
21853
21854       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21855         /* This can happen for a friend declaration for a function
21856            already declared with default arguments.  */
21857         continue;
21858
21859       parsed_arg
21860         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21861                                                 default_arg,
21862                                                 TREE_VALUE (parm));
21863       if (parsed_arg == error_mark_node)
21864         {
21865           continue;
21866         }
21867
21868       TREE_PURPOSE (parm) = parsed_arg;
21869
21870       /* Update any instantiations we've already created.  */
21871       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21872            VEC_iterate (tree, insts, ix, copy); ix++)
21873         TREE_PURPOSE (copy) = parsed_arg;
21874     }
21875
21876   pop_defarg_context ();
21877
21878   /* Make sure no default arg is missing.  */
21879   check_default_args (fn);
21880
21881   /* Restore the state of local_variables_forbidden_p.  */
21882   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21883
21884   /* Restore the queue.  */
21885   pop_unparsed_function_queues (parser);
21886 }
21887
21888 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21889    either a TYPE or an expression, depending on the form of the
21890    input.  The KEYWORD indicates which kind of expression we have
21891    encountered.  */
21892
21893 static tree
21894 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21895 {
21896   tree expr = NULL_TREE;
21897   const char *saved_message;
21898   char *tmp;
21899   bool saved_integral_constant_expression_p;
21900   bool saved_non_integral_constant_expression_p;
21901   bool pack_expansion_p = false;
21902
21903   /* Types cannot be defined in a `sizeof' expression.  Save away the
21904      old message.  */
21905   saved_message = parser->type_definition_forbidden_message;
21906   /* And create the new one.  */
21907   tmp = concat ("types may not be defined in %<",
21908                 IDENTIFIER_POINTER (ridpointers[keyword]),
21909                 "%> expressions", NULL);
21910   parser->type_definition_forbidden_message = tmp;
21911
21912   /* The restrictions on constant-expressions do not apply inside
21913      sizeof expressions.  */
21914   saved_integral_constant_expression_p
21915     = parser->integral_constant_expression_p;
21916   saved_non_integral_constant_expression_p
21917     = parser->non_integral_constant_expression_p;
21918   parser->integral_constant_expression_p = false;
21919
21920   /* If it's a `...', then we are computing the length of a parameter
21921      pack.  */
21922   if (keyword == RID_SIZEOF
21923       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21924     {
21925       /* Consume the `...'.  */
21926       cp_lexer_consume_token (parser->lexer);
21927       maybe_warn_variadic_templates ();
21928
21929       /* Note that this is an expansion.  */
21930       pack_expansion_p = true;
21931     }
21932
21933   /* Do not actually evaluate the expression.  */
21934   ++cp_unevaluated_operand;
21935   ++c_inhibit_evaluation_warnings;
21936   /* If it's a `(', then we might be looking at the type-id
21937      construction.  */
21938   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21939     {
21940       tree type;
21941       bool saved_in_type_id_in_expr_p;
21942
21943       /* We can't be sure yet whether we're looking at a type-id or an
21944          expression.  */
21945       cp_parser_parse_tentatively (parser);
21946       /* Consume the `('.  */
21947       cp_lexer_consume_token (parser->lexer);
21948       /* Parse the type-id.  */
21949       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21950       parser->in_type_id_in_expr_p = true;
21951       type = cp_parser_type_id (parser);
21952       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21953       /* Now, look for the trailing `)'.  */
21954       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21955       /* If all went well, then we're done.  */
21956       if (cp_parser_parse_definitely (parser))
21957         {
21958           cp_decl_specifier_seq decl_specs;
21959
21960           /* Build a trivial decl-specifier-seq.  */
21961           clear_decl_specs (&decl_specs);
21962           decl_specs.type = type;
21963
21964           /* Call grokdeclarator to figure out what type this is.  */
21965           expr = grokdeclarator (NULL,
21966                                  &decl_specs,
21967                                  TYPENAME,
21968                                  /*initialized=*/0,
21969                                  /*attrlist=*/NULL);
21970         }
21971     }
21972
21973   /* If the type-id production did not work out, then we must be
21974      looking at the unary-expression production.  */
21975   if (!expr)
21976     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21977                                        /*cast_p=*/false, NULL);
21978
21979   if (pack_expansion_p)
21980     /* Build a pack expansion. */
21981     expr = make_pack_expansion (expr);
21982
21983   /* Go back to evaluating expressions.  */
21984   --cp_unevaluated_operand;
21985   --c_inhibit_evaluation_warnings;
21986
21987   /* Free the message we created.  */
21988   free (tmp);
21989   /* And restore the old one.  */
21990   parser->type_definition_forbidden_message = saved_message;
21991   parser->integral_constant_expression_p
21992     = saved_integral_constant_expression_p;
21993   parser->non_integral_constant_expression_p
21994     = saved_non_integral_constant_expression_p;
21995
21996   return expr;
21997 }
21998
21999 /* If the current declaration has no declarator, return true.  */
22000
22001 static bool
22002 cp_parser_declares_only_class_p (cp_parser *parser)
22003 {
22004   /* If the next token is a `;' or a `,' then there is no
22005      declarator.  */
22006   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22007           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22008 }
22009
22010 /* Update the DECL_SPECS to reflect the storage class indicated by
22011    KEYWORD.  */
22012
22013 static void
22014 cp_parser_set_storage_class (cp_parser *parser,
22015                              cp_decl_specifier_seq *decl_specs,
22016                              enum rid keyword,
22017                              location_t location)
22018 {
22019   cp_storage_class storage_class;
22020
22021   if (parser->in_unbraced_linkage_specification_p)
22022     {
22023       error_at (location, "invalid use of %qD in linkage specification",
22024                 ridpointers[keyword]);
22025       return;
22026     }
22027   else if (decl_specs->storage_class != sc_none)
22028     {
22029       decl_specs->conflicting_specifiers_p = true;
22030       return;
22031     }
22032
22033   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22034       && decl_specs->specs[(int) ds_thread])
22035     {
22036       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22037       decl_specs->specs[(int) ds_thread] = 0;
22038     }
22039
22040   switch (keyword)
22041     {
22042     case RID_AUTO:
22043       storage_class = sc_auto;
22044       break;
22045     case RID_REGISTER:
22046       storage_class = sc_register;
22047       break;
22048     case RID_STATIC:
22049       storage_class = sc_static;
22050       break;
22051     case RID_EXTERN:
22052       storage_class = sc_extern;
22053       break;
22054     case RID_MUTABLE:
22055       storage_class = sc_mutable;
22056       break;
22057     default:
22058       gcc_unreachable ();
22059     }
22060   decl_specs->storage_class = storage_class;
22061
22062   /* A storage class specifier cannot be applied alongside a typedef 
22063      specifier. If there is a typedef specifier present then set 
22064      conflicting_specifiers_p which will trigger an error later
22065      on in grokdeclarator. */
22066   if (decl_specs->specs[(int)ds_typedef])
22067     decl_specs->conflicting_specifiers_p = true;
22068 }
22069
22070 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22071    is true, the type is a class or enum definition.  */
22072
22073 static void
22074 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22075                               tree type_spec,
22076                               location_t location,
22077                               bool type_definition_p)
22078 {
22079   decl_specs->any_specifiers_p = true;
22080
22081   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22082      (with, for example, in "typedef int wchar_t;") we remember that
22083      this is what happened.  In system headers, we ignore these
22084      declarations so that G++ can work with system headers that are not
22085      C++-safe.  */
22086   if (decl_specs->specs[(int) ds_typedef]
22087       && !type_definition_p
22088       && (type_spec == boolean_type_node
22089           || type_spec == char16_type_node
22090           || type_spec == char32_type_node
22091           || type_spec == wchar_type_node)
22092       && (decl_specs->type
22093           || decl_specs->specs[(int) ds_long]
22094           || decl_specs->specs[(int) ds_short]
22095           || decl_specs->specs[(int) ds_unsigned]
22096           || decl_specs->specs[(int) ds_signed]))
22097     {
22098       decl_specs->redefined_builtin_type = type_spec;
22099       if (!decl_specs->type)
22100         {
22101           decl_specs->type = type_spec;
22102           decl_specs->type_definition_p = false;
22103           decl_specs->type_location = location;
22104         }
22105     }
22106   else if (decl_specs->type)
22107     decl_specs->multiple_types_p = true;
22108   else
22109     {
22110       decl_specs->type = type_spec;
22111       decl_specs->type_definition_p = type_definition_p;
22112       decl_specs->redefined_builtin_type = NULL_TREE;
22113       decl_specs->type_location = location;
22114     }
22115 }
22116
22117 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22118    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22119
22120 static bool
22121 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22122 {
22123   return decl_specifiers->specs[(int) ds_friend] != 0;
22124 }
22125
22126 /* Issue an error message indicating that TOKEN_DESC was expected.
22127    If KEYWORD is true, it indicated this function is called by
22128    cp_parser_require_keword and the required token can only be
22129    a indicated keyword. */
22130
22131 static void
22132 cp_parser_required_error (cp_parser *parser,
22133                           required_token token_desc,
22134                           bool keyword)
22135 {
22136   switch (token_desc)
22137     {
22138       case RT_NEW:
22139         cp_parser_error (parser, "expected %<new%>");
22140         return;
22141       case RT_DELETE:
22142         cp_parser_error (parser, "expected %<delete%>");
22143         return;
22144       case RT_RETURN:
22145         cp_parser_error (parser, "expected %<return%>");
22146         return;
22147       case RT_WHILE:
22148         cp_parser_error (parser, "expected %<while%>");
22149         return;
22150       case RT_EXTERN:
22151         cp_parser_error (parser, "expected %<extern%>");
22152         return;
22153       case RT_STATIC_ASSERT:
22154         cp_parser_error (parser, "expected %<static_assert%>");
22155         return;
22156       case RT_DECLTYPE:
22157         cp_parser_error (parser, "expected %<decltype%>");
22158         return;
22159       case RT_OPERATOR:
22160         cp_parser_error (parser, "expected %<operator%>");
22161         return;
22162       case RT_CLASS:
22163         cp_parser_error (parser, "expected %<class%>");
22164         return;
22165       case RT_TEMPLATE:
22166         cp_parser_error (parser, "expected %<template%>");
22167         return;
22168       case RT_NAMESPACE:
22169         cp_parser_error (parser, "expected %<namespace%>");
22170         return;
22171       case RT_USING:
22172         cp_parser_error (parser, "expected %<using%>");
22173         return;
22174       case RT_ASM:
22175         cp_parser_error (parser, "expected %<asm%>");
22176         return;
22177       case RT_TRY:
22178         cp_parser_error (parser, "expected %<try%>");
22179         return;
22180       case RT_CATCH:
22181         cp_parser_error (parser, "expected %<catch%>");
22182         return;
22183       case RT_THROW:
22184         cp_parser_error (parser, "expected %<throw%>");
22185         return;
22186       case RT_LABEL:
22187         cp_parser_error (parser, "expected %<__label__%>");
22188         return;
22189       case RT_AT_TRY:
22190         cp_parser_error (parser, "expected %<@try%>");
22191         return;
22192       case RT_AT_SYNCHRONIZED:
22193         cp_parser_error (parser, "expected %<@synchronized%>");
22194         return;
22195       case RT_AT_THROW:
22196         cp_parser_error (parser, "expected %<@throw%>");
22197         return;
22198       case RT_TRANSACTION_ATOMIC:
22199         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22200         return;
22201       case RT_TRANSACTION_RELAXED:
22202         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22203         return;
22204       default:
22205         break;
22206     }
22207   if (!keyword)
22208     {
22209       switch (token_desc)
22210         {
22211           case RT_SEMICOLON:
22212             cp_parser_error (parser, "expected %<;%>");
22213             return;
22214           case RT_OPEN_PAREN:
22215             cp_parser_error (parser, "expected %<(%>");
22216             return;
22217           case RT_CLOSE_BRACE:
22218             cp_parser_error (parser, "expected %<}%>");
22219             return;
22220           case RT_OPEN_BRACE:
22221             cp_parser_error (parser, "expected %<{%>");
22222             return;
22223           case RT_CLOSE_SQUARE:
22224             cp_parser_error (parser, "expected %<]%>");
22225             return;
22226           case RT_OPEN_SQUARE:
22227             cp_parser_error (parser, "expected %<[%>");
22228             return;
22229           case RT_COMMA:
22230             cp_parser_error (parser, "expected %<,%>");
22231             return;
22232           case RT_SCOPE:
22233             cp_parser_error (parser, "expected %<::%>");
22234             return;
22235           case RT_LESS:
22236             cp_parser_error (parser, "expected %<<%>");
22237             return;
22238           case RT_GREATER:
22239             cp_parser_error (parser, "expected %<>%>");
22240             return;
22241           case RT_EQ:
22242             cp_parser_error (parser, "expected %<=%>");
22243             return;
22244           case RT_ELLIPSIS:
22245             cp_parser_error (parser, "expected %<...%>");
22246             return;
22247           case RT_MULT:
22248             cp_parser_error (parser, "expected %<*%>");
22249             return;
22250           case RT_COMPL:
22251             cp_parser_error (parser, "expected %<~%>");
22252             return;
22253           case RT_COLON:
22254             cp_parser_error (parser, "expected %<:%>");
22255             return;
22256           case RT_COLON_SCOPE:
22257             cp_parser_error (parser, "expected %<:%> or %<::%>");
22258             return;
22259           case RT_CLOSE_PAREN:
22260             cp_parser_error (parser, "expected %<)%>");
22261             return;
22262           case RT_COMMA_CLOSE_PAREN:
22263             cp_parser_error (parser, "expected %<,%> or %<)%>");
22264             return;
22265           case RT_PRAGMA_EOL:
22266             cp_parser_error (parser, "expected end of line");
22267             return;
22268           case RT_NAME:
22269             cp_parser_error (parser, "expected identifier");
22270             return;
22271           case RT_SELECT:
22272             cp_parser_error (parser, "expected selection-statement");
22273             return;
22274           case RT_INTERATION:
22275             cp_parser_error (parser, "expected iteration-statement");
22276             return;
22277           case RT_JUMP:
22278             cp_parser_error (parser, "expected jump-statement");
22279             return;
22280           case RT_CLASS_KEY:
22281             cp_parser_error (parser, "expected class-key");
22282             return;
22283           case RT_CLASS_TYPENAME_TEMPLATE:
22284             cp_parser_error (parser,
22285                  "expected %<class%>, %<typename%>, or %<template%>");
22286             return;
22287           default:
22288             gcc_unreachable ();
22289         }
22290     }
22291   else
22292     gcc_unreachable ();
22293 }
22294
22295
22296
22297 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22298    issue an error message indicating that TOKEN_DESC was expected.
22299
22300    Returns the token consumed, if the token had the appropriate type.
22301    Otherwise, returns NULL.  */
22302
22303 static cp_token *
22304 cp_parser_require (cp_parser* parser,
22305                    enum cpp_ttype type,
22306                    required_token token_desc)
22307 {
22308   if (cp_lexer_next_token_is (parser->lexer, type))
22309     return cp_lexer_consume_token (parser->lexer);
22310   else
22311     {
22312       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22313       if (!cp_parser_simulate_error (parser))
22314         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22315       return NULL;
22316     }
22317 }
22318
22319 /* An error message is produced if the next token is not '>'.
22320    All further tokens are skipped until the desired token is
22321    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22322
22323 static void
22324 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22325 {
22326   /* Current level of '< ... >'.  */
22327   unsigned level = 0;
22328   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22329   unsigned nesting_depth = 0;
22330
22331   /* Are we ready, yet?  If not, issue error message.  */
22332   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22333     return;
22334
22335   /* Skip tokens until the desired token is found.  */
22336   while (true)
22337     {
22338       /* Peek at the next token.  */
22339       switch (cp_lexer_peek_token (parser->lexer)->type)
22340         {
22341         case CPP_LESS:
22342           if (!nesting_depth)
22343             ++level;
22344           break;
22345
22346         case CPP_RSHIFT:
22347           if (cxx_dialect == cxx98)
22348             /* C++0x views the `>>' operator as two `>' tokens, but
22349                C++98 does not. */
22350             break;
22351           else if (!nesting_depth && level-- == 0)
22352             {
22353               /* We've hit a `>>' where the first `>' closes the
22354                  template argument list, and the second `>' is
22355                  spurious.  Just consume the `>>' and stop; we've
22356                  already produced at least one error.  */
22357               cp_lexer_consume_token (parser->lexer);
22358               return;
22359             }
22360           /* Fall through for C++0x, so we handle the second `>' in
22361              the `>>'.  */
22362
22363         case CPP_GREATER:
22364           if (!nesting_depth && level-- == 0)
22365             {
22366               /* We've reached the token we want, consume it and stop.  */
22367               cp_lexer_consume_token (parser->lexer);
22368               return;
22369             }
22370           break;
22371
22372         case CPP_OPEN_PAREN:
22373         case CPP_OPEN_SQUARE:
22374           ++nesting_depth;
22375           break;
22376
22377         case CPP_CLOSE_PAREN:
22378         case CPP_CLOSE_SQUARE:
22379           if (nesting_depth-- == 0)
22380             return;
22381           break;
22382
22383         case CPP_EOF:
22384         case CPP_PRAGMA_EOL:
22385         case CPP_SEMICOLON:
22386         case CPP_OPEN_BRACE:
22387         case CPP_CLOSE_BRACE:
22388           /* The '>' was probably forgotten, don't look further.  */
22389           return;
22390
22391         default:
22392           break;
22393         }
22394
22395       /* Consume this token.  */
22396       cp_lexer_consume_token (parser->lexer);
22397     }
22398 }
22399
22400 /* If the next token is the indicated keyword, consume it.  Otherwise,
22401    issue an error message indicating that TOKEN_DESC was expected.
22402
22403    Returns the token consumed, if the token had the appropriate type.
22404    Otherwise, returns NULL.  */
22405
22406 static cp_token *
22407 cp_parser_require_keyword (cp_parser* parser,
22408                            enum rid keyword,
22409                            required_token token_desc)
22410 {
22411   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22412
22413   if (token && token->keyword != keyword)
22414     {
22415       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22416       return NULL;
22417     }
22418
22419   return token;
22420 }
22421
22422 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22423    function-definition.  */
22424
22425 static bool
22426 cp_parser_token_starts_function_definition_p (cp_token* token)
22427 {
22428   return (/* An ordinary function-body begins with an `{'.  */
22429           token->type == CPP_OPEN_BRACE
22430           /* A ctor-initializer begins with a `:'.  */
22431           || token->type == CPP_COLON
22432           /* A function-try-block begins with `try'.  */
22433           || token->keyword == RID_TRY
22434           /* A function-transaction-block begins with `__transaction_atomic'
22435              or `__transaction_relaxed'.  */
22436           || token->keyword == RID_TRANSACTION_ATOMIC
22437           || token->keyword == RID_TRANSACTION_RELAXED
22438           /* The named return value extension begins with `return'.  */
22439           || token->keyword == RID_RETURN);
22440 }
22441
22442 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22443    definition.  */
22444
22445 static bool
22446 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22447 {
22448   cp_token *token;
22449
22450   token = cp_lexer_peek_token (parser->lexer);
22451   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22452 }
22453
22454 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22455    C++0x) ending a template-argument.  */
22456
22457 static bool
22458 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22459 {
22460   cp_token *token;
22461
22462   token = cp_lexer_peek_token (parser->lexer);
22463   return (token->type == CPP_COMMA 
22464           || token->type == CPP_GREATER
22465           || token->type == CPP_ELLIPSIS
22466           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22467 }
22468
22469 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22470    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22471
22472 static bool
22473 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22474                                                      size_t n)
22475 {
22476   cp_token *token;
22477
22478   token = cp_lexer_peek_nth_token (parser->lexer, n);
22479   if (token->type == CPP_LESS)
22480     return true;
22481   /* Check for the sequence `<::' in the original code. It would be lexed as
22482      `[:', where `[' is a digraph, and there is no whitespace before
22483      `:'.  */
22484   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22485     {
22486       cp_token *token2;
22487       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22488       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22489         return true;
22490     }
22491   return false;
22492 }
22493
22494 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22495    or none_type otherwise.  */
22496
22497 static enum tag_types
22498 cp_parser_token_is_class_key (cp_token* token)
22499 {
22500   switch (token->keyword)
22501     {
22502     case RID_CLASS:
22503       return class_type;
22504     case RID_STRUCT:
22505       return record_type;
22506     case RID_UNION:
22507       return union_type;
22508
22509     default:
22510       return none_type;
22511     }
22512 }
22513
22514 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22515
22516 static void
22517 cp_parser_check_class_key (enum tag_types class_key, tree type)
22518 {
22519   if (type == error_mark_node)
22520     return;
22521   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22522     {
22523       permerror (input_location, "%qs tag used in naming %q#T",
22524                  class_key == union_type ? "union"
22525                  : class_key == record_type ? "struct" : "class",
22526                  type);
22527       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22528               "%q#T was previously declared here", type);
22529     }
22530 }
22531
22532 /* Issue an error message if DECL is redeclared with different
22533    access than its original declaration [class.access.spec/3].
22534    This applies to nested classes and nested class templates.
22535    [class.mem/1].  */
22536
22537 static void
22538 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22539 {
22540   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22541     return;
22542
22543   if ((TREE_PRIVATE (decl)
22544        != (current_access_specifier == access_private_node))
22545       || (TREE_PROTECTED (decl)
22546           != (current_access_specifier == access_protected_node)))
22547     error_at (location, "%qD redeclared with different access", decl);
22548 }
22549
22550 /* Look for the `template' keyword, as a syntactic disambiguator.
22551    Return TRUE iff it is present, in which case it will be
22552    consumed.  */
22553
22554 static bool
22555 cp_parser_optional_template_keyword (cp_parser *parser)
22556 {
22557   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22558     {
22559       /* The `template' keyword can only be used within templates;
22560          outside templates the parser can always figure out what is a
22561          template and what is not.  */
22562       if (!processing_template_decl)
22563         {
22564           cp_token *token = cp_lexer_peek_token (parser->lexer);
22565           error_at (token->location,
22566                     "%<template%> (as a disambiguator) is only allowed "
22567                     "within templates");
22568           /* If this part of the token stream is rescanned, the same
22569              error message would be generated.  So, we purge the token
22570              from the stream.  */
22571           cp_lexer_purge_token (parser->lexer);
22572           return false;
22573         }
22574       else
22575         {
22576           /* Consume the `template' keyword.  */
22577           cp_lexer_consume_token (parser->lexer);
22578           return true;
22579         }
22580     }
22581
22582   return false;
22583 }
22584
22585 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22586    set PARSER->SCOPE, and perform other related actions.  */
22587
22588 static void
22589 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22590 {
22591   int i;
22592   struct tree_check *check_value;
22593   deferred_access_check *chk;
22594   VEC (deferred_access_check,gc) *checks;
22595
22596   /* Get the stored value.  */
22597   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22598   /* Perform any access checks that were deferred.  */
22599   checks = check_value->checks;
22600   if (checks)
22601     {
22602       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22603         perform_or_defer_access_check (chk->binfo,
22604                                        chk->decl,
22605                                        chk->diag_decl);
22606     }
22607   /* Set the scope from the stored value.  */
22608   parser->scope = check_value->value;
22609   parser->qualifying_scope = check_value->qualifying_scope;
22610   parser->object_scope = NULL_TREE;
22611 }
22612
22613 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22614    encounter the end of a block before what we were looking for.  */
22615
22616 static bool
22617 cp_parser_cache_group (cp_parser *parser,
22618                        enum cpp_ttype end,
22619                        unsigned depth)
22620 {
22621   while (true)
22622     {
22623       cp_token *token = cp_lexer_peek_token (parser->lexer);
22624
22625       /* Abort a parenthesized expression if we encounter a semicolon.  */
22626       if ((end == CPP_CLOSE_PAREN || depth == 0)
22627           && token->type == CPP_SEMICOLON)
22628         return true;
22629       /* If we've reached the end of the file, stop.  */
22630       if (token->type == CPP_EOF
22631           || (end != CPP_PRAGMA_EOL
22632               && token->type == CPP_PRAGMA_EOL))
22633         return true;
22634       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22635         /* We've hit the end of an enclosing block, so there's been some
22636            kind of syntax error.  */
22637         return true;
22638
22639       /* Consume the token.  */
22640       cp_lexer_consume_token (parser->lexer);
22641       /* See if it starts a new group.  */
22642       if (token->type == CPP_OPEN_BRACE)
22643         {
22644           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22645           /* In theory this should probably check end == '}', but
22646              cp_parser_save_member_function_body needs it to exit
22647              after either '}' or ')' when called with ')'.  */
22648           if (depth == 0)
22649             return false;
22650         }
22651       else if (token->type == CPP_OPEN_PAREN)
22652         {
22653           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22654           if (depth == 0 && end == CPP_CLOSE_PAREN)
22655             return false;
22656         }
22657       else if (token->type == CPP_PRAGMA)
22658         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22659       else if (token->type == end)
22660         return false;
22661     }
22662 }
22663
22664 /* Like above, for caching a default argument or NSDMI.  Both of these are
22665    terminated by a non-nested comma, but it can be unclear whether or not a
22666    comma is nested in a template argument list unless we do more parsing.
22667    In order to handle this ambiguity, when we encounter a ',' after a '<'
22668    we try to parse what follows as a parameter-declaration-list (in the
22669    case of a default argument) or a member-declarator (in the case of an
22670    NSDMI).  If that succeeds, then we stop caching.  */
22671
22672 static tree
22673 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22674 {
22675   unsigned depth = 0;
22676   int maybe_template_id = 0;
22677   cp_token *first_token;
22678   cp_token *token;
22679   tree default_argument;
22680
22681   /* Add tokens until we have processed the entire default
22682      argument.  We add the range [first_token, token).  */
22683   first_token = cp_lexer_peek_token (parser->lexer);
22684   if (first_token->type == CPP_OPEN_BRACE)
22685     {
22686       /* For list-initialization, this is straightforward.  */
22687       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22688       token = cp_lexer_peek_token (parser->lexer);
22689     }
22690   else while (true)
22691     {
22692       bool done = false;
22693
22694       /* Peek at the next token.  */
22695       token = cp_lexer_peek_token (parser->lexer);
22696       /* What we do depends on what token we have.  */
22697       switch (token->type)
22698         {
22699           /* In valid code, a default argument must be
22700              immediately followed by a `,' `)', or `...'.  */
22701         case CPP_COMMA:
22702           if (depth == 0 && maybe_template_id)
22703             {
22704               /* If we've seen a '<', we might be in a
22705                  template-argument-list.  Until Core issue 325 is
22706                  resolved, we don't know how this situation ought
22707                  to be handled, so try to DTRT.  We check whether
22708                  what comes after the comma is a valid parameter
22709                  declaration list.  If it is, then the comma ends
22710                  the default argument; otherwise the default
22711                  argument continues.  */
22712               bool error = false;
22713               tree t;
22714
22715               /* Set ITALP so cp_parser_parameter_declaration_list
22716                  doesn't decide to commit to this parse.  */
22717               bool saved_italp = parser->in_template_argument_list_p;
22718               parser->in_template_argument_list_p = true;
22719
22720               cp_parser_parse_tentatively (parser);
22721               cp_lexer_consume_token (parser->lexer);
22722
22723               if (nsdmi)
22724                 {
22725                   int ctor_dtor_or_conv_p;
22726                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22727                                         &ctor_dtor_or_conv_p,
22728                                         /*parenthesized_p=*/NULL,
22729                                         /*member_p=*/true);
22730                 }
22731               else
22732                 {
22733                   begin_scope (sk_function_parms, NULL_TREE);
22734                   cp_parser_parameter_declaration_list (parser, &error);
22735                   for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22736                     pop_binding (DECL_NAME (t), t);
22737                   leave_scope ();
22738                 }
22739               if (!cp_parser_error_occurred (parser) && !error)
22740                 done = true;
22741               cp_parser_abort_tentative_parse (parser);
22742
22743               parser->in_template_argument_list_p = saved_italp;
22744               break;
22745             }
22746         case CPP_CLOSE_PAREN:
22747         case CPP_ELLIPSIS:
22748           /* If we run into a non-nested `;', `}', or `]',
22749              then the code is invalid -- but the default
22750              argument is certainly over.  */
22751         case CPP_SEMICOLON:
22752         case CPP_CLOSE_BRACE:
22753         case CPP_CLOSE_SQUARE:
22754           if (depth == 0)
22755             done = true;
22756           /* Update DEPTH, if necessary.  */
22757           else if (token->type == CPP_CLOSE_PAREN
22758                    || token->type == CPP_CLOSE_BRACE
22759                    || token->type == CPP_CLOSE_SQUARE)
22760             --depth;
22761           break;
22762
22763         case CPP_OPEN_PAREN:
22764         case CPP_OPEN_SQUARE:
22765         case CPP_OPEN_BRACE:
22766           ++depth;
22767           break;
22768
22769         case CPP_LESS:
22770           if (depth == 0)
22771             /* This might be the comparison operator, or it might
22772                start a template argument list.  */
22773             ++maybe_template_id;
22774           break;
22775
22776         case CPP_RSHIFT:
22777           if (cxx_dialect == cxx98)
22778             break;
22779           /* Fall through for C++0x, which treats the `>>'
22780              operator like two `>' tokens in certain
22781              cases.  */
22782
22783         case CPP_GREATER:
22784           if (depth == 0)
22785             {
22786               /* This might be an operator, or it might close a
22787                  template argument list.  But if a previous '<'
22788                  started a template argument list, this will have
22789                  closed it, so we can't be in one anymore.  */
22790               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22791               if (maybe_template_id < 0)
22792                 maybe_template_id = 0;
22793             }
22794           break;
22795
22796           /* If we run out of tokens, issue an error message.  */
22797         case CPP_EOF:
22798         case CPP_PRAGMA_EOL:
22799           error_at (token->location, "file ends in default argument");
22800           done = true;
22801           break;
22802
22803         case CPP_NAME:
22804         case CPP_SCOPE:
22805           /* In these cases, we should look for template-ids.
22806              For example, if the default argument is
22807              `X<int, double>()', we need to do name lookup to
22808              figure out whether or not `X' is a template; if
22809              so, the `,' does not end the default argument.
22810
22811              That is not yet done.  */
22812           break;
22813
22814         default:
22815           break;
22816         }
22817
22818       /* If we've reached the end, stop.  */
22819       if (done)
22820         break;
22821
22822       /* Add the token to the token block.  */
22823       token = cp_lexer_consume_token (parser->lexer);
22824     }
22825
22826   /* Create a DEFAULT_ARG to represent the unparsed default
22827      argument.  */
22828   default_argument = make_node (DEFAULT_ARG);
22829   DEFARG_TOKENS (default_argument)
22830     = cp_token_cache_new (first_token, token);
22831   DEFARG_INSTANTIATIONS (default_argument) = NULL;
22832
22833   return default_argument;
22834 }
22835
22836 /* Begin parsing tentatively.  We always save tokens while parsing
22837    tentatively so that if the tentative parsing fails we can restore the
22838    tokens.  */
22839
22840 static void
22841 cp_parser_parse_tentatively (cp_parser* parser)
22842 {
22843   /* Enter a new parsing context.  */
22844   parser->context = cp_parser_context_new (parser->context);
22845   /* Begin saving tokens.  */
22846   cp_lexer_save_tokens (parser->lexer);
22847   /* In order to avoid repetitive access control error messages,
22848      access checks are queued up until we are no longer parsing
22849      tentatively.  */
22850   push_deferring_access_checks (dk_deferred);
22851 }
22852
22853 /* Commit to the currently active tentative parse.  */
22854
22855 static void
22856 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22857 {
22858   cp_parser_context *context;
22859   cp_lexer *lexer;
22860
22861   /* Mark all of the levels as committed.  */
22862   lexer = parser->lexer;
22863   for (context = parser->context; context->next; context = context->next)
22864     {
22865       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22866         break;
22867       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22868       while (!cp_lexer_saving_tokens (lexer))
22869         lexer = lexer->next;
22870       cp_lexer_commit_tokens (lexer);
22871     }
22872 }
22873
22874 /* Abort the currently active tentative parse.  All consumed tokens
22875    will be rolled back, and no diagnostics will be issued.  */
22876
22877 static void
22878 cp_parser_abort_tentative_parse (cp_parser* parser)
22879 {
22880   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22881               || errorcount > 0);
22882   cp_parser_simulate_error (parser);
22883   /* Now, pretend that we want to see if the construct was
22884      successfully parsed.  */
22885   cp_parser_parse_definitely (parser);
22886 }
22887
22888 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22889    token stream.  Otherwise, commit to the tokens we have consumed.
22890    Returns true if no error occurred; false otherwise.  */
22891
22892 static bool
22893 cp_parser_parse_definitely (cp_parser* parser)
22894 {
22895   bool error_occurred;
22896   cp_parser_context *context;
22897
22898   /* Remember whether or not an error occurred, since we are about to
22899      destroy that information.  */
22900   error_occurred = cp_parser_error_occurred (parser);
22901   /* Remove the topmost context from the stack.  */
22902   context = parser->context;
22903   parser->context = context->next;
22904   /* If no parse errors occurred, commit to the tentative parse.  */
22905   if (!error_occurred)
22906     {
22907       /* Commit to the tokens read tentatively, unless that was
22908          already done.  */
22909       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22910         cp_lexer_commit_tokens (parser->lexer);
22911
22912       pop_to_parent_deferring_access_checks ();
22913     }
22914   /* Otherwise, if errors occurred, roll back our state so that things
22915      are just as they were before we began the tentative parse.  */
22916   else
22917     {
22918       cp_lexer_rollback_tokens (parser->lexer);
22919       pop_deferring_access_checks ();
22920     }
22921   /* Add the context to the front of the free list.  */
22922   context->next = cp_parser_context_free_list;
22923   cp_parser_context_free_list = context;
22924
22925   return !error_occurred;
22926 }
22927
22928 /* Returns true if we are parsing tentatively and are not committed to
22929    this tentative parse.  */
22930
22931 static bool
22932 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22933 {
22934   return (cp_parser_parsing_tentatively (parser)
22935           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22936 }
22937
22938 /* Returns nonzero iff an error has occurred during the most recent
22939    tentative parse.  */
22940
22941 static bool
22942 cp_parser_error_occurred (cp_parser* parser)
22943 {
22944   return (cp_parser_parsing_tentatively (parser)
22945           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22946 }
22947
22948 /* Returns nonzero if GNU extensions are allowed.  */
22949
22950 static bool
22951 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22952 {
22953   return parser->allow_gnu_extensions_p;
22954 }
22955 \f
22956 /* Objective-C++ Productions */
22957
22958
22959 /* Parse an Objective-C expression, which feeds into a primary-expression
22960    above.
22961
22962    objc-expression:
22963      objc-message-expression
22964      objc-string-literal
22965      objc-encode-expression
22966      objc-protocol-expression
22967      objc-selector-expression
22968
22969   Returns a tree representation of the expression.  */
22970
22971 static tree
22972 cp_parser_objc_expression (cp_parser* parser)
22973 {
22974   /* Try to figure out what kind of declaration is present.  */
22975   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22976
22977   switch (kwd->type)
22978     {
22979     case CPP_OPEN_SQUARE:
22980       return cp_parser_objc_message_expression (parser);
22981
22982     case CPP_OBJC_STRING:
22983       kwd = cp_lexer_consume_token (parser->lexer);
22984       return objc_build_string_object (kwd->u.value);
22985
22986     case CPP_KEYWORD:
22987       switch (kwd->keyword)
22988         {
22989         case RID_AT_ENCODE:
22990           return cp_parser_objc_encode_expression (parser);
22991
22992         case RID_AT_PROTOCOL:
22993           return cp_parser_objc_protocol_expression (parser);
22994
22995         case RID_AT_SELECTOR:
22996           return cp_parser_objc_selector_expression (parser);
22997
22998         default:
22999           break;
23000         }
23001     default:
23002       error_at (kwd->location,
23003                 "misplaced %<@%D%> Objective-C++ construct",
23004                 kwd->u.value);
23005       cp_parser_skip_to_end_of_block_or_statement (parser);
23006     }
23007
23008   return error_mark_node;
23009 }
23010
23011 /* Parse an Objective-C message expression.
23012
23013    objc-message-expression:
23014      [ objc-message-receiver objc-message-args ]
23015
23016    Returns a representation of an Objective-C message.  */
23017
23018 static tree
23019 cp_parser_objc_message_expression (cp_parser* parser)
23020 {
23021   tree receiver, messageargs;
23022
23023   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
23024   receiver = cp_parser_objc_message_receiver (parser);
23025   messageargs = cp_parser_objc_message_args (parser);
23026   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23027
23028   return objc_build_message_expr (receiver, messageargs);
23029 }
23030
23031 /* Parse an objc-message-receiver.
23032
23033    objc-message-receiver:
23034      expression
23035      simple-type-specifier
23036
23037   Returns a representation of the type or expression.  */
23038
23039 static tree
23040 cp_parser_objc_message_receiver (cp_parser* parser)
23041 {
23042   tree rcv;
23043
23044   /* An Objective-C message receiver may be either (1) a type
23045      or (2) an expression.  */
23046   cp_parser_parse_tentatively (parser);
23047   rcv = cp_parser_expression (parser, false, NULL);
23048
23049   if (cp_parser_parse_definitely (parser))
23050     return rcv;
23051
23052   rcv = cp_parser_simple_type_specifier (parser,
23053                                          /*decl_specs=*/NULL,
23054                                          CP_PARSER_FLAGS_NONE);
23055
23056   return objc_get_class_reference (rcv);
23057 }
23058
23059 /* Parse the arguments and selectors comprising an Objective-C message.
23060
23061    objc-message-args:
23062      objc-selector
23063      objc-selector-args
23064      objc-selector-args , objc-comma-args
23065
23066    objc-selector-args:
23067      objc-selector [opt] : assignment-expression
23068      objc-selector-args objc-selector [opt] : assignment-expression
23069
23070    objc-comma-args:
23071      assignment-expression
23072      objc-comma-args , assignment-expression
23073
23074    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23075    selector arguments and TREE_VALUE containing a list of comma
23076    arguments.  */
23077
23078 static tree
23079 cp_parser_objc_message_args (cp_parser* parser)
23080 {
23081   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23082   bool maybe_unary_selector_p = true;
23083   cp_token *token = cp_lexer_peek_token (parser->lexer);
23084
23085   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23086     {
23087       tree selector = NULL_TREE, arg;
23088
23089       if (token->type != CPP_COLON)
23090         selector = cp_parser_objc_selector (parser);
23091
23092       /* Detect if we have a unary selector.  */
23093       if (maybe_unary_selector_p
23094           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23095         return build_tree_list (selector, NULL_TREE);
23096
23097       maybe_unary_selector_p = false;
23098       cp_parser_require (parser, CPP_COLON, RT_COLON);
23099       arg = cp_parser_assignment_expression (parser, false, NULL);
23100
23101       sel_args
23102         = chainon (sel_args,
23103                    build_tree_list (selector, arg));
23104
23105       token = cp_lexer_peek_token (parser->lexer);
23106     }
23107
23108   /* Handle non-selector arguments, if any. */
23109   while (token->type == CPP_COMMA)
23110     {
23111       tree arg;
23112
23113       cp_lexer_consume_token (parser->lexer);
23114       arg = cp_parser_assignment_expression (parser, false, NULL);
23115
23116       addl_args
23117         = chainon (addl_args,
23118                    build_tree_list (NULL_TREE, arg));
23119
23120       token = cp_lexer_peek_token (parser->lexer);
23121     }
23122
23123   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23124     {
23125       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23126       return build_tree_list (error_mark_node, error_mark_node);
23127     }
23128
23129   return build_tree_list (sel_args, addl_args);
23130 }
23131
23132 /* Parse an Objective-C encode expression.
23133
23134    objc-encode-expression:
23135      @encode objc-typename
23136
23137    Returns an encoded representation of the type argument.  */
23138
23139 static tree
23140 cp_parser_objc_encode_expression (cp_parser* parser)
23141 {
23142   tree type;
23143   cp_token *token;
23144
23145   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
23146   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23147   token = cp_lexer_peek_token (parser->lexer);
23148   type = complete_type (cp_parser_type_id (parser));
23149   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23150
23151   if (!type)
23152     {
23153       error_at (token->location, 
23154                 "%<@encode%> must specify a type as an argument");
23155       return error_mark_node;
23156     }
23157
23158   /* This happens if we find @encode(T) (where T is a template
23159      typename or something dependent on a template typename) when
23160      parsing a template.  In that case, we can't compile it
23161      immediately, but we rather create an AT_ENCODE_EXPR which will
23162      need to be instantiated when the template is used.
23163   */
23164   if (dependent_type_p (type))
23165     {
23166       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23167       TREE_READONLY (value) = 1;
23168       return value;
23169     }
23170
23171   return objc_build_encode_expr (type);
23172 }
23173
23174 /* Parse an Objective-C @defs expression.  */
23175
23176 static tree
23177 cp_parser_objc_defs_expression (cp_parser *parser)
23178 {
23179   tree name;
23180
23181   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
23182   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23183   name = cp_parser_identifier (parser);
23184   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23185
23186   return objc_get_class_ivars (name);
23187 }
23188
23189 /* Parse an Objective-C protocol expression.
23190
23191   objc-protocol-expression:
23192     @protocol ( identifier )
23193
23194   Returns a representation of the protocol expression.  */
23195
23196 static tree
23197 cp_parser_objc_protocol_expression (cp_parser* parser)
23198 {
23199   tree proto;
23200
23201   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23202   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23203   proto = cp_parser_identifier (parser);
23204   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23205
23206   return objc_build_protocol_expr (proto);
23207 }
23208
23209 /* Parse an Objective-C selector expression.
23210
23211    objc-selector-expression:
23212      @selector ( objc-method-signature )
23213
23214    objc-method-signature:
23215      objc-selector
23216      objc-selector-seq
23217
23218    objc-selector-seq:
23219      objc-selector :
23220      objc-selector-seq objc-selector :
23221
23222   Returns a representation of the method selector.  */
23223
23224 static tree
23225 cp_parser_objc_selector_expression (cp_parser* parser)
23226 {
23227   tree sel_seq = NULL_TREE;
23228   bool maybe_unary_selector_p = true;
23229   cp_token *token;
23230   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23231
23232   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23233   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23234   token = cp_lexer_peek_token (parser->lexer);
23235
23236   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23237          || token->type == CPP_SCOPE)
23238     {
23239       tree selector = NULL_TREE;
23240
23241       if (token->type != CPP_COLON
23242           || token->type == CPP_SCOPE)
23243         selector = cp_parser_objc_selector (parser);
23244
23245       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23246           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23247         {
23248           /* Detect if we have a unary selector.  */
23249           if (maybe_unary_selector_p)
23250             {
23251               sel_seq = selector;
23252               goto finish_selector;
23253             }
23254           else
23255             {
23256               cp_parser_error (parser, "expected %<:%>");
23257             }
23258         }
23259       maybe_unary_selector_p = false;
23260       token = cp_lexer_consume_token (parser->lexer);
23261
23262       if (token->type == CPP_SCOPE)
23263         {
23264           sel_seq
23265             = chainon (sel_seq,
23266                        build_tree_list (selector, NULL_TREE));
23267           sel_seq
23268             = chainon (sel_seq,
23269                        build_tree_list (NULL_TREE, NULL_TREE));
23270         }
23271       else
23272         sel_seq
23273           = chainon (sel_seq,
23274                      build_tree_list (selector, NULL_TREE));
23275
23276       token = cp_lexer_peek_token (parser->lexer);
23277     }
23278
23279  finish_selector:
23280   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23281
23282   return objc_build_selector_expr (loc, sel_seq);
23283 }
23284
23285 /* Parse a list of identifiers.
23286
23287    objc-identifier-list:
23288      identifier
23289      objc-identifier-list , identifier
23290
23291    Returns a TREE_LIST of identifier nodes.  */
23292
23293 static tree
23294 cp_parser_objc_identifier_list (cp_parser* parser)
23295 {
23296   tree identifier;
23297   tree list;
23298   cp_token *sep;
23299
23300   identifier = cp_parser_identifier (parser);
23301   if (identifier == error_mark_node)
23302     return error_mark_node;      
23303
23304   list = build_tree_list (NULL_TREE, identifier);
23305   sep = cp_lexer_peek_token (parser->lexer);
23306
23307   while (sep->type == CPP_COMMA)
23308     {
23309       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23310       identifier = cp_parser_identifier (parser);
23311       if (identifier == error_mark_node)
23312         return list;
23313
23314       list = chainon (list, build_tree_list (NULL_TREE,
23315                                              identifier));
23316       sep = cp_lexer_peek_token (parser->lexer);
23317     }
23318   
23319   return list;
23320 }
23321
23322 /* Parse an Objective-C alias declaration.
23323
23324    objc-alias-declaration:
23325      @compatibility_alias identifier identifier ;
23326
23327    This function registers the alias mapping with the Objective-C front end.
23328    It returns nothing.  */
23329
23330 static void
23331 cp_parser_objc_alias_declaration (cp_parser* parser)
23332 {
23333   tree alias, orig;
23334
23335   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23336   alias = cp_parser_identifier (parser);
23337   orig = cp_parser_identifier (parser);
23338   objc_declare_alias (alias, orig);
23339   cp_parser_consume_semicolon_at_end_of_statement (parser);
23340 }
23341
23342 /* Parse an Objective-C class forward-declaration.
23343
23344    objc-class-declaration:
23345      @class objc-identifier-list ;
23346
23347    The function registers the forward declarations with the Objective-C
23348    front end.  It returns nothing.  */
23349
23350 static void
23351 cp_parser_objc_class_declaration (cp_parser* parser)
23352 {
23353   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23354   while (true)
23355     {
23356       tree id;
23357       
23358       id = cp_parser_identifier (parser);
23359       if (id == error_mark_node)
23360         break;
23361       
23362       objc_declare_class (id);
23363
23364       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23365         cp_lexer_consume_token (parser->lexer);
23366       else
23367         break;
23368     }
23369   cp_parser_consume_semicolon_at_end_of_statement (parser);
23370 }
23371
23372 /* Parse a list of Objective-C protocol references.
23373
23374    objc-protocol-refs-opt:
23375      objc-protocol-refs [opt]
23376
23377    objc-protocol-refs:
23378      < objc-identifier-list >
23379
23380    Returns a TREE_LIST of identifiers, if any.  */
23381
23382 static tree
23383 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23384 {
23385   tree protorefs = NULL_TREE;
23386
23387   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23388     {
23389       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23390       protorefs = cp_parser_objc_identifier_list (parser);
23391       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23392     }
23393
23394   return protorefs;
23395 }
23396
23397 /* Parse a Objective-C visibility specification.  */
23398
23399 static void
23400 cp_parser_objc_visibility_spec (cp_parser* parser)
23401 {
23402   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23403
23404   switch (vis->keyword)
23405     {
23406     case RID_AT_PRIVATE:
23407       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23408       break;
23409     case RID_AT_PROTECTED:
23410       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23411       break;
23412     case RID_AT_PUBLIC:
23413       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23414       break;
23415     case RID_AT_PACKAGE:
23416       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23417       break;
23418     default:
23419       return;
23420     }
23421
23422   /* Eat '@private'/'@protected'/'@public'.  */
23423   cp_lexer_consume_token (parser->lexer);
23424 }
23425
23426 /* Parse an Objective-C method type.  Return 'true' if it is a class
23427    (+) method, and 'false' if it is an instance (-) method.  */
23428
23429 static inline bool
23430 cp_parser_objc_method_type (cp_parser* parser)
23431 {
23432   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23433     return true;
23434   else
23435     return false;
23436 }
23437
23438 /* Parse an Objective-C protocol qualifier.  */
23439
23440 static tree
23441 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23442 {
23443   tree quals = NULL_TREE, node;
23444   cp_token *token = cp_lexer_peek_token (parser->lexer);
23445
23446   node = token->u.value;
23447
23448   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23449          && (node == ridpointers [(int) RID_IN]
23450              || node == ridpointers [(int) RID_OUT]
23451              || node == ridpointers [(int) RID_INOUT]
23452              || node == ridpointers [(int) RID_BYCOPY]
23453              || node == ridpointers [(int) RID_BYREF]
23454              || node == ridpointers [(int) RID_ONEWAY]))
23455     {
23456       quals = tree_cons (NULL_TREE, node, quals);
23457       cp_lexer_consume_token (parser->lexer);
23458       token = cp_lexer_peek_token (parser->lexer);
23459       node = token->u.value;
23460     }
23461
23462   return quals;
23463 }
23464
23465 /* Parse an Objective-C typename.  */
23466
23467 static tree
23468 cp_parser_objc_typename (cp_parser* parser)
23469 {
23470   tree type_name = NULL_TREE;
23471
23472   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23473     {
23474       tree proto_quals, cp_type = NULL_TREE;
23475
23476       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23477       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23478
23479       /* An ObjC type name may consist of just protocol qualifiers, in which
23480          case the type shall default to 'id'.  */
23481       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23482         {
23483           cp_type = cp_parser_type_id (parser);
23484           
23485           /* If the type could not be parsed, an error has already
23486              been produced.  For error recovery, behave as if it had
23487              not been specified, which will use the default type
23488              'id'.  */
23489           if (cp_type == error_mark_node)
23490             {
23491               cp_type = NULL_TREE;
23492               /* We need to skip to the closing parenthesis as
23493                  cp_parser_type_id() does not seem to do it for
23494                  us.  */
23495               cp_parser_skip_to_closing_parenthesis (parser,
23496                                                      /*recovering=*/true,
23497                                                      /*or_comma=*/false,
23498                                                      /*consume_paren=*/false);
23499             }
23500         }
23501
23502       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23503       type_name = build_tree_list (proto_quals, cp_type);
23504     }
23505
23506   return type_name;
23507 }
23508
23509 /* Check to see if TYPE refers to an Objective-C selector name.  */
23510
23511 static bool
23512 cp_parser_objc_selector_p (enum cpp_ttype type)
23513 {
23514   return (type == CPP_NAME || type == CPP_KEYWORD
23515           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23516           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23517           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23518           || type == CPP_XOR || type == CPP_XOR_EQ);
23519 }
23520
23521 /* Parse an Objective-C selector.  */
23522
23523 static tree
23524 cp_parser_objc_selector (cp_parser* parser)
23525 {
23526   cp_token *token = cp_lexer_consume_token (parser->lexer);
23527
23528   if (!cp_parser_objc_selector_p (token->type))
23529     {
23530       error_at (token->location, "invalid Objective-C++ selector name");
23531       return error_mark_node;
23532     }
23533
23534   /* C++ operator names are allowed to appear in ObjC selectors.  */
23535   switch (token->type)
23536     {
23537     case CPP_AND_AND: return get_identifier ("and");
23538     case CPP_AND_EQ: return get_identifier ("and_eq");
23539     case CPP_AND: return get_identifier ("bitand");
23540     case CPP_OR: return get_identifier ("bitor");
23541     case CPP_COMPL: return get_identifier ("compl");
23542     case CPP_NOT: return get_identifier ("not");
23543     case CPP_NOT_EQ: return get_identifier ("not_eq");
23544     case CPP_OR_OR: return get_identifier ("or");
23545     case CPP_OR_EQ: return get_identifier ("or_eq");
23546     case CPP_XOR: return get_identifier ("xor");
23547     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23548     default: return token->u.value;
23549     }
23550 }
23551
23552 /* Parse an Objective-C params list.  */
23553
23554 static tree
23555 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23556 {
23557   tree params = NULL_TREE;
23558   bool maybe_unary_selector_p = true;
23559   cp_token *token = cp_lexer_peek_token (parser->lexer);
23560
23561   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23562     {
23563       tree selector = NULL_TREE, type_name, identifier;
23564       tree parm_attr = NULL_TREE;
23565
23566       if (token->keyword == RID_ATTRIBUTE)
23567         break;
23568
23569       if (token->type != CPP_COLON)
23570         selector = cp_parser_objc_selector (parser);
23571
23572       /* Detect if we have a unary selector.  */
23573       if (maybe_unary_selector_p
23574           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23575         {
23576           params = selector; /* Might be followed by attributes.  */
23577           break;
23578         }
23579
23580       maybe_unary_selector_p = false;
23581       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23582         {
23583           /* Something went quite wrong.  There should be a colon
23584              here, but there is not.  Stop parsing parameters.  */
23585           break;
23586         }
23587       type_name = cp_parser_objc_typename (parser);
23588       /* New ObjC allows attributes on parameters too.  */
23589       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23590         parm_attr = cp_parser_attributes_opt (parser);
23591       identifier = cp_parser_identifier (parser);
23592
23593       params
23594         = chainon (params,
23595                    objc_build_keyword_decl (selector,
23596                                             type_name,
23597                                             identifier,
23598                                             parm_attr));
23599
23600       token = cp_lexer_peek_token (parser->lexer);
23601     }
23602
23603   if (params == NULL_TREE)
23604     {
23605       cp_parser_error (parser, "objective-c++ method declaration is expected");
23606       return error_mark_node;
23607     }
23608
23609   /* We allow tail attributes for the method.  */
23610   if (token->keyword == RID_ATTRIBUTE)
23611     {
23612       *attributes = cp_parser_attributes_opt (parser);
23613       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23614           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23615         return params;
23616       cp_parser_error (parser, 
23617                        "method attributes must be specified at the end");
23618       return error_mark_node;
23619     }
23620
23621   if (params == NULL_TREE)
23622     {
23623       cp_parser_error (parser, "objective-c++ method declaration is expected");
23624       return error_mark_node;
23625     }
23626   return params;
23627 }
23628
23629 /* Parse the non-keyword Objective-C params.  */
23630
23631 static tree
23632 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23633                                        tree* attributes)
23634 {
23635   tree params = make_node (TREE_LIST);
23636   cp_token *token = cp_lexer_peek_token (parser->lexer);
23637   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23638
23639   while (token->type == CPP_COMMA)
23640     {
23641       cp_parameter_declarator *parmdecl;
23642       tree parm;
23643
23644       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23645       token = cp_lexer_peek_token (parser->lexer);
23646
23647       if (token->type == CPP_ELLIPSIS)
23648         {
23649           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23650           *ellipsisp = true;
23651           token = cp_lexer_peek_token (parser->lexer);
23652           break;
23653         }
23654
23655       /* TODO: parse attributes for tail parameters.  */
23656       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23657       parm = grokdeclarator (parmdecl->declarator,
23658                              &parmdecl->decl_specifiers,
23659                              PARM, /*initialized=*/0,
23660                              /*attrlist=*/NULL);
23661
23662       chainon (params, build_tree_list (NULL_TREE, parm));
23663       token = cp_lexer_peek_token (parser->lexer);
23664     }
23665
23666   /* We allow tail attributes for the method.  */
23667   if (token->keyword == RID_ATTRIBUTE)
23668     {
23669       if (*attributes == NULL_TREE)
23670         {
23671           *attributes = cp_parser_attributes_opt (parser);
23672           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23673               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23674             return params;
23675         }
23676       else        
23677         /* We have an error, but parse the attributes, so that we can 
23678            carry on.  */
23679         *attributes = cp_parser_attributes_opt (parser);
23680
23681       cp_parser_error (parser, 
23682                        "method attributes must be specified at the end");
23683       return error_mark_node;
23684     }
23685
23686   return params;
23687 }
23688
23689 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23690
23691 static void
23692 cp_parser_objc_interstitial_code (cp_parser* parser)
23693 {
23694   cp_token *token = cp_lexer_peek_token (parser->lexer);
23695
23696   /* If the next token is `extern' and the following token is a string
23697      literal, then we have a linkage specification.  */
23698   if (token->keyword == RID_EXTERN
23699       && cp_parser_is_pure_string_literal
23700          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23701     cp_parser_linkage_specification (parser);
23702   /* Handle #pragma, if any.  */
23703   else if (token->type == CPP_PRAGMA)
23704     cp_parser_pragma (parser, pragma_external);
23705   /* Allow stray semicolons.  */
23706   else if (token->type == CPP_SEMICOLON)
23707     cp_lexer_consume_token (parser->lexer);
23708   /* Mark methods as optional or required, when building protocols.  */
23709   else if (token->keyword == RID_AT_OPTIONAL)
23710     {
23711       cp_lexer_consume_token (parser->lexer);
23712       objc_set_method_opt (true);
23713     }
23714   else if (token->keyword == RID_AT_REQUIRED)
23715     {
23716       cp_lexer_consume_token (parser->lexer);
23717       objc_set_method_opt (false);
23718     }
23719   else if (token->keyword == RID_NAMESPACE)
23720     cp_parser_namespace_definition (parser);
23721   /* Other stray characters must generate errors.  */
23722   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23723     {
23724       cp_lexer_consume_token (parser->lexer);
23725       error ("stray %qs between Objective-C++ methods",
23726              token->type == CPP_OPEN_BRACE ? "{" : "}");
23727     }
23728   /* Finally, try to parse a block-declaration, or a function-definition.  */
23729   else
23730     cp_parser_block_declaration (parser, /*statement_p=*/false);
23731 }
23732
23733 /* Parse a method signature.  */
23734
23735 static tree
23736 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23737 {
23738   tree rettype, kwdparms, optparms;
23739   bool ellipsis = false;
23740   bool is_class_method;
23741
23742   is_class_method = cp_parser_objc_method_type (parser);
23743   rettype = cp_parser_objc_typename (parser);
23744   *attributes = NULL_TREE;
23745   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23746   if (kwdparms == error_mark_node)
23747     return error_mark_node;
23748   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23749   if (optparms == error_mark_node)
23750     return error_mark_node;
23751
23752   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23753 }
23754
23755 static bool
23756 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23757 {
23758   tree tattr;  
23759   cp_lexer_save_tokens (parser->lexer);
23760   tattr = cp_parser_attributes_opt (parser);
23761   gcc_assert (tattr) ;
23762   
23763   /* If the attributes are followed by a method introducer, this is not allowed.
23764      Dump the attributes and flag the situation.  */
23765   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23766       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23767     return true;
23768
23769   /* Otherwise, the attributes introduce some interstitial code, possibly so
23770      rewind to allow that check.  */
23771   cp_lexer_rollback_tokens (parser->lexer);
23772   return false;  
23773 }
23774
23775 /* Parse an Objective-C method prototype list.  */
23776
23777 static void
23778 cp_parser_objc_method_prototype_list (cp_parser* parser)
23779 {
23780   cp_token *token = cp_lexer_peek_token (parser->lexer);
23781
23782   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23783     {
23784       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23785         {
23786           tree attributes, sig;
23787           bool is_class_method;
23788           if (token->type == CPP_PLUS)
23789             is_class_method = true;
23790           else
23791             is_class_method = false;
23792           sig = cp_parser_objc_method_signature (parser, &attributes);
23793           if (sig == error_mark_node)
23794             {
23795               cp_parser_skip_to_end_of_block_or_statement (parser);
23796               token = cp_lexer_peek_token (parser->lexer);
23797               continue;
23798             }
23799           objc_add_method_declaration (is_class_method, sig, attributes);
23800           cp_parser_consume_semicolon_at_end_of_statement (parser);
23801         }
23802       else if (token->keyword == RID_AT_PROPERTY)
23803         cp_parser_objc_at_property_declaration (parser);
23804       else if (token->keyword == RID_ATTRIBUTE 
23805                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23806         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23807                     OPT_Wattributes, 
23808                     "prefix attributes are ignored for methods");
23809       else
23810         /* Allow for interspersed non-ObjC++ code.  */
23811         cp_parser_objc_interstitial_code (parser);
23812
23813       token = cp_lexer_peek_token (parser->lexer);
23814     }
23815
23816   if (token->type != CPP_EOF)
23817     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23818   else
23819     cp_parser_error (parser, "expected %<@end%>");
23820
23821   objc_finish_interface ();
23822 }
23823
23824 /* Parse an Objective-C method definition list.  */
23825
23826 static void
23827 cp_parser_objc_method_definition_list (cp_parser* parser)
23828 {
23829   cp_token *token = cp_lexer_peek_token (parser->lexer);
23830
23831   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23832     {
23833       tree meth;
23834
23835       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23836         {
23837           cp_token *ptk;
23838           tree sig, attribute;
23839           bool is_class_method;
23840           if (token->type == CPP_PLUS)
23841             is_class_method = true;
23842           else
23843             is_class_method = false;
23844           push_deferring_access_checks (dk_deferred);
23845           sig = cp_parser_objc_method_signature (parser, &attribute);
23846           if (sig == error_mark_node)
23847             {
23848               cp_parser_skip_to_end_of_block_or_statement (parser);
23849               token = cp_lexer_peek_token (parser->lexer);
23850               continue;
23851             }
23852           objc_start_method_definition (is_class_method, sig, attribute,
23853                                         NULL_TREE);
23854
23855           /* For historical reasons, we accept an optional semicolon.  */
23856           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23857             cp_lexer_consume_token (parser->lexer);
23858
23859           ptk = cp_lexer_peek_token (parser->lexer);
23860           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23861                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23862             {
23863               perform_deferred_access_checks ();
23864               stop_deferring_access_checks ();
23865               meth = cp_parser_function_definition_after_declarator (parser,
23866                                                                      false);
23867               pop_deferring_access_checks ();
23868               objc_finish_method_definition (meth);
23869             }
23870         }
23871       /* The following case will be removed once @synthesize is
23872          completely implemented.  */
23873       else if (token->keyword == RID_AT_PROPERTY)
23874         cp_parser_objc_at_property_declaration (parser);
23875       else if (token->keyword == RID_AT_SYNTHESIZE)
23876         cp_parser_objc_at_synthesize_declaration (parser);
23877       else if (token->keyword == RID_AT_DYNAMIC)
23878         cp_parser_objc_at_dynamic_declaration (parser);
23879       else if (token->keyword == RID_ATTRIBUTE 
23880                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23881         warning_at (token->location, OPT_Wattributes,
23882                     "prefix attributes are ignored for methods");
23883       else
23884         /* Allow for interspersed non-ObjC++ code.  */
23885         cp_parser_objc_interstitial_code (parser);
23886
23887       token = cp_lexer_peek_token (parser->lexer);
23888     }
23889
23890   if (token->type != CPP_EOF)
23891     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23892   else
23893     cp_parser_error (parser, "expected %<@end%>");
23894
23895   objc_finish_implementation ();
23896 }
23897
23898 /* Parse Objective-C ivars.  */
23899
23900 static void
23901 cp_parser_objc_class_ivars (cp_parser* parser)
23902 {
23903   cp_token *token = cp_lexer_peek_token (parser->lexer);
23904
23905   if (token->type != CPP_OPEN_BRACE)
23906     return;     /* No ivars specified.  */
23907
23908   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23909   token = cp_lexer_peek_token (parser->lexer);
23910
23911   while (token->type != CPP_CLOSE_BRACE 
23912         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23913     {
23914       cp_decl_specifier_seq declspecs;
23915       int decl_class_or_enum_p;
23916       tree prefix_attributes;
23917
23918       cp_parser_objc_visibility_spec (parser);
23919
23920       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23921         break;
23922
23923       cp_parser_decl_specifier_seq (parser,
23924                                     CP_PARSER_FLAGS_OPTIONAL,
23925                                     &declspecs,
23926                                     &decl_class_or_enum_p);
23927
23928       /* auto, register, static, extern, mutable.  */
23929       if (declspecs.storage_class != sc_none)
23930         {
23931           cp_parser_error (parser, "invalid type for instance variable");         
23932           declspecs.storage_class = sc_none;
23933         }
23934
23935       /* __thread.  */
23936       if (declspecs.specs[(int) ds_thread])
23937         {
23938           cp_parser_error (parser, "invalid type for instance variable");
23939           declspecs.specs[(int) ds_thread] = 0;
23940         }
23941       
23942       /* typedef.  */
23943       if (declspecs.specs[(int) ds_typedef])
23944         {
23945           cp_parser_error (parser, "invalid type for instance variable");
23946           declspecs.specs[(int) ds_typedef] = 0;
23947         }
23948
23949       prefix_attributes = declspecs.attributes;
23950       declspecs.attributes = NULL_TREE;
23951
23952       /* Keep going until we hit the `;' at the end of the
23953          declaration.  */
23954       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23955         {
23956           tree width = NULL_TREE, attributes, first_attribute, decl;
23957           cp_declarator *declarator = NULL;
23958           int ctor_dtor_or_conv_p;
23959
23960           /* Check for a (possibly unnamed) bitfield declaration.  */
23961           token = cp_lexer_peek_token (parser->lexer);
23962           if (token->type == CPP_COLON)
23963             goto eat_colon;
23964
23965           if (token->type == CPP_NAME
23966               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23967                   == CPP_COLON))
23968             {
23969               /* Get the name of the bitfield.  */
23970               declarator = make_id_declarator (NULL_TREE,
23971                                                cp_parser_identifier (parser),
23972                                                sfk_none);
23973
23974              eat_colon:
23975               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23976               /* Get the width of the bitfield.  */
23977               width
23978                 = cp_parser_constant_expression (parser,
23979                                                  /*allow_non_constant=*/false,
23980                                                  NULL);
23981             }
23982           else
23983             {
23984               /* Parse the declarator.  */
23985               declarator
23986                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23987                                         &ctor_dtor_or_conv_p,
23988                                         /*parenthesized_p=*/NULL,
23989                                         /*member_p=*/false);
23990             }
23991
23992           /* Look for attributes that apply to the ivar.  */
23993           attributes = cp_parser_attributes_opt (parser);
23994           /* Remember which attributes are prefix attributes and
23995              which are not.  */
23996           first_attribute = attributes;
23997           /* Combine the attributes.  */
23998           attributes = chainon (prefix_attributes, attributes);
23999
24000           if (width)
24001               /* Create the bitfield declaration.  */
24002               decl = grokbitfield (declarator, &declspecs,
24003                                    width,
24004                                    attributes);
24005           else
24006             decl = grokfield (declarator, &declspecs,
24007                               NULL_TREE, /*init_const_expr_p=*/false,
24008                               NULL_TREE, attributes);
24009
24010           /* Add the instance variable.  */
24011           if (decl != error_mark_node && decl != NULL_TREE)
24012             objc_add_instance_variable (decl);
24013
24014           /* Reset PREFIX_ATTRIBUTES.  */
24015           while (attributes && TREE_CHAIN (attributes) != first_attribute)
24016             attributes = TREE_CHAIN (attributes);
24017           if (attributes)
24018             TREE_CHAIN (attributes) = NULL_TREE;
24019
24020           token = cp_lexer_peek_token (parser->lexer);
24021
24022           if (token->type == CPP_COMMA)
24023             {
24024               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24025               continue;
24026             }
24027           break;
24028         }
24029
24030       cp_parser_consume_semicolon_at_end_of_statement (parser);
24031       token = cp_lexer_peek_token (parser->lexer);
24032     }
24033
24034   if (token->keyword == RID_AT_END)
24035     cp_parser_error (parser, "expected %<}%>");
24036
24037   /* Do not consume the RID_AT_END, so it will be read again as terminating
24038      the @interface of @implementation.  */ 
24039   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24040     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
24041     
24042   /* For historical reasons, we accept an optional semicolon.  */
24043   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24044     cp_lexer_consume_token (parser->lexer);
24045 }
24046
24047 /* Parse an Objective-C protocol declaration.  */
24048
24049 static void
24050 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24051 {
24052   tree proto, protorefs;
24053   cp_token *tok;
24054
24055   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24056   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24057     {
24058       tok = cp_lexer_peek_token (parser->lexer);
24059       error_at (tok->location, "identifier expected after %<@protocol%>");
24060       cp_parser_consume_semicolon_at_end_of_statement (parser);
24061       return;
24062     }
24063
24064   /* See if we have a forward declaration or a definition.  */
24065   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24066
24067   /* Try a forward declaration first.  */
24068   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24069     {
24070       while (true)
24071         {
24072           tree id;
24073           
24074           id = cp_parser_identifier (parser);
24075           if (id == error_mark_node)
24076             break;
24077           
24078           objc_declare_protocol (id, attributes);
24079           
24080           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24081             cp_lexer_consume_token (parser->lexer);
24082           else
24083             break;
24084         }
24085       cp_parser_consume_semicolon_at_end_of_statement (parser);
24086     }
24087
24088   /* Ok, we got a full-fledged definition (or at least should).  */
24089   else
24090     {
24091       proto = cp_parser_identifier (parser);
24092       protorefs = cp_parser_objc_protocol_refs_opt (parser);
24093       objc_start_protocol (proto, protorefs, attributes);
24094       cp_parser_objc_method_prototype_list (parser);
24095     }
24096 }
24097
24098 /* Parse an Objective-C superclass or category.  */
24099
24100 static void
24101 cp_parser_objc_superclass_or_category (cp_parser *parser, 
24102                                        bool iface_p,
24103                                        tree *super,
24104                                        tree *categ, bool *is_class_extension)
24105 {
24106   cp_token *next = cp_lexer_peek_token (parser->lexer);
24107
24108   *super = *categ = NULL_TREE;
24109   *is_class_extension = false;
24110   if (next->type == CPP_COLON)
24111     {
24112       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24113       *super = cp_parser_identifier (parser);
24114     }
24115   else if (next->type == CPP_OPEN_PAREN)
24116     {
24117       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24118
24119       /* If there is no category name, and this is an @interface, we
24120          have a class extension.  */
24121       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24122         {
24123           *categ = NULL_TREE;
24124           *is_class_extension = true;
24125         }
24126       else
24127         *categ = cp_parser_identifier (parser);
24128
24129       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24130     }
24131 }
24132
24133 /* Parse an Objective-C class interface.  */
24134
24135 static void
24136 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24137 {
24138   tree name, super, categ, protos;
24139   bool is_class_extension;
24140
24141   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
24142   name = cp_parser_identifier (parser);
24143   if (name == error_mark_node)
24144     {
24145       /* It's hard to recover because even if valid @interface stuff
24146          is to follow, we can't compile it (or validate it) if we
24147          don't even know which class it refers to.  Let's assume this
24148          was a stray '@interface' token in the stream and skip it.
24149       */
24150       return;
24151     }
24152   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24153                                          &is_class_extension);
24154   protos = cp_parser_objc_protocol_refs_opt (parser);
24155
24156   /* We have either a class or a category on our hands.  */
24157   if (categ || is_class_extension)
24158     objc_start_category_interface (name, categ, protos, attributes);
24159   else
24160     {
24161       objc_start_class_interface (name, super, protos, attributes);
24162       /* Handle instance variable declarations, if any.  */
24163       cp_parser_objc_class_ivars (parser);
24164       objc_continue_interface ();
24165     }
24166
24167   cp_parser_objc_method_prototype_list (parser);
24168 }
24169
24170 /* Parse an Objective-C class implementation.  */
24171
24172 static void
24173 cp_parser_objc_class_implementation (cp_parser* parser)
24174 {
24175   tree name, super, categ;
24176   bool is_class_extension;
24177
24178   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
24179   name = cp_parser_identifier (parser);
24180   if (name == error_mark_node)
24181     {
24182       /* It's hard to recover because even if valid @implementation
24183          stuff is to follow, we can't compile it (or validate it) if
24184          we don't even know which class it refers to.  Let's assume
24185          this was a stray '@implementation' token in the stream and
24186          skip it.
24187       */
24188       return;
24189     }
24190   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24191                                          &is_class_extension);
24192
24193   /* We have either a class or a category on our hands.  */
24194   if (categ)
24195     objc_start_category_implementation (name, categ);
24196   else
24197     {
24198       objc_start_class_implementation (name, super);
24199       /* Handle instance variable declarations, if any.  */
24200       cp_parser_objc_class_ivars (parser);
24201       objc_continue_implementation ();
24202     }
24203
24204   cp_parser_objc_method_definition_list (parser);
24205 }
24206
24207 /* Consume the @end token and finish off the implementation.  */
24208
24209 static void
24210 cp_parser_objc_end_implementation (cp_parser* parser)
24211 {
24212   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24213   objc_finish_implementation ();
24214 }
24215
24216 /* Parse an Objective-C declaration.  */
24217
24218 static void
24219 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24220 {
24221   /* Try to figure out what kind of declaration is present.  */
24222   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24223
24224   if (attributes)
24225     switch (kwd->keyword)
24226       {
24227         case RID_AT_ALIAS:
24228         case RID_AT_CLASS:
24229         case RID_AT_END:
24230           error_at (kwd->location, "attributes may not be specified before"
24231                     " the %<@%D%> Objective-C++ keyword",
24232                     kwd->u.value);
24233           attributes = NULL;
24234           break;
24235         case RID_AT_IMPLEMENTATION:
24236           warning_at (kwd->location, OPT_Wattributes,
24237                       "prefix attributes are ignored before %<@%D%>",
24238                       kwd->u.value);
24239           attributes = NULL;
24240         default:
24241           break;
24242       }
24243
24244   switch (kwd->keyword)
24245     {
24246     case RID_AT_ALIAS:
24247       cp_parser_objc_alias_declaration (parser);
24248       break;
24249     case RID_AT_CLASS:
24250       cp_parser_objc_class_declaration (parser);
24251       break;
24252     case RID_AT_PROTOCOL:
24253       cp_parser_objc_protocol_declaration (parser, attributes);
24254       break;
24255     case RID_AT_INTERFACE:
24256       cp_parser_objc_class_interface (parser, attributes);
24257       break;
24258     case RID_AT_IMPLEMENTATION:
24259       cp_parser_objc_class_implementation (parser);
24260       break;
24261     case RID_AT_END:
24262       cp_parser_objc_end_implementation (parser);
24263       break;
24264     default:
24265       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24266                 kwd->u.value);
24267       cp_parser_skip_to_end_of_block_or_statement (parser);
24268     }
24269 }
24270
24271 /* Parse an Objective-C try-catch-finally statement.
24272
24273    objc-try-catch-finally-stmt:
24274      @try compound-statement objc-catch-clause-seq [opt]
24275        objc-finally-clause [opt]
24276
24277    objc-catch-clause-seq:
24278      objc-catch-clause objc-catch-clause-seq [opt]
24279
24280    objc-catch-clause:
24281      @catch ( objc-exception-declaration ) compound-statement
24282
24283    objc-finally-clause:
24284      @finally compound-statement
24285
24286    objc-exception-declaration:
24287      parameter-declaration
24288      '...'
24289
24290    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24291
24292    Returns NULL_TREE.
24293
24294    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24295    for C.  Keep them in sync.  */   
24296
24297 static tree
24298 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24299 {
24300   location_t location;
24301   tree stmt;
24302
24303   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24304   location = cp_lexer_peek_token (parser->lexer)->location;
24305   objc_maybe_warn_exceptions (location);
24306   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24307      node, lest it get absorbed into the surrounding block.  */
24308   stmt = push_stmt_list ();
24309   cp_parser_compound_statement (parser, NULL, false, false);
24310   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24311
24312   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24313     {
24314       cp_parameter_declarator *parm;
24315       tree parameter_declaration = error_mark_node;
24316       bool seen_open_paren = false;
24317
24318       cp_lexer_consume_token (parser->lexer);
24319       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24320         seen_open_paren = true;
24321       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24322         {
24323           /* We have "@catch (...)" (where the '...' are literally
24324              what is in the code).  Skip the '...'.
24325              parameter_declaration is set to NULL_TREE, and
24326              objc_being_catch_clauses() knows that that means
24327              '...'.  */
24328           cp_lexer_consume_token (parser->lexer);
24329           parameter_declaration = NULL_TREE;
24330         }
24331       else
24332         {
24333           /* We have "@catch (NSException *exception)" or something
24334              like that.  Parse the parameter declaration.  */
24335           parm = cp_parser_parameter_declaration (parser, false, NULL);
24336           if (parm == NULL)
24337             parameter_declaration = error_mark_node;
24338           else
24339             parameter_declaration = grokdeclarator (parm->declarator,
24340                                                     &parm->decl_specifiers,
24341                                                     PARM, /*initialized=*/0,
24342                                                     /*attrlist=*/NULL);
24343         }
24344       if (seen_open_paren)
24345         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24346       else
24347         {
24348           /* If there was no open parenthesis, we are recovering from
24349              an error, and we are trying to figure out what mistake
24350              the user has made.  */
24351
24352           /* If there is an immediate closing parenthesis, the user
24353              probably forgot the opening one (ie, they typed "@catch
24354              NSException *e)".  Parse the closing parenthesis and keep
24355              going.  */
24356           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24357             cp_lexer_consume_token (parser->lexer);
24358           
24359           /* If these is no immediate closing parenthesis, the user
24360              probably doesn't know that parenthesis are required at
24361              all (ie, they typed "@catch NSException *e").  So, just
24362              forget about the closing parenthesis and keep going.  */
24363         }
24364       objc_begin_catch_clause (parameter_declaration);
24365       cp_parser_compound_statement (parser, NULL, false, false);
24366       objc_finish_catch_clause ();
24367     }
24368   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24369     {
24370       cp_lexer_consume_token (parser->lexer);
24371       location = cp_lexer_peek_token (parser->lexer)->location;
24372       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24373          node, lest it get absorbed into the surrounding block.  */
24374       stmt = push_stmt_list ();
24375       cp_parser_compound_statement (parser, NULL, false, false);
24376       objc_build_finally_clause (location, pop_stmt_list (stmt));
24377     }
24378
24379   return objc_finish_try_stmt ();
24380 }
24381
24382 /* Parse an Objective-C synchronized statement.
24383
24384    objc-synchronized-stmt:
24385      @synchronized ( expression ) compound-statement
24386
24387    Returns NULL_TREE.  */
24388
24389 static tree
24390 cp_parser_objc_synchronized_statement (cp_parser *parser)
24391 {
24392   location_t location;
24393   tree lock, stmt;
24394
24395   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24396
24397   location = cp_lexer_peek_token (parser->lexer)->location;
24398   objc_maybe_warn_exceptions (location);
24399   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24400   lock = cp_parser_expression (parser, false, NULL);
24401   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24402
24403   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24404      node, lest it get absorbed into the surrounding block.  */
24405   stmt = push_stmt_list ();
24406   cp_parser_compound_statement (parser, NULL, false, false);
24407
24408   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24409 }
24410
24411 /* Parse an Objective-C throw statement.
24412
24413    objc-throw-stmt:
24414      @throw assignment-expression [opt] ;
24415
24416    Returns a constructed '@throw' statement.  */
24417
24418 static tree
24419 cp_parser_objc_throw_statement (cp_parser *parser)
24420 {
24421   tree expr = NULL_TREE;
24422   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24423
24424   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24425
24426   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24427     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24428
24429   cp_parser_consume_semicolon_at_end_of_statement (parser);
24430
24431   return objc_build_throw_stmt (loc, expr);
24432 }
24433
24434 /* Parse an Objective-C statement.  */
24435
24436 static tree
24437 cp_parser_objc_statement (cp_parser * parser)
24438 {
24439   /* Try to figure out what kind of declaration is present.  */
24440   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24441
24442   switch (kwd->keyword)
24443     {
24444     case RID_AT_TRY:
24445       return cp_parser_objc_try_catch_finally_statement (parser);
24446     case RID_AT_SYNCHRONIZED:
24447       return cp_parser_objc_synchronized_statement (parser);
24448     case RID_AT_THROW:
24449       return cp_parser_objc_throw_statement (parser);
24450     default:
24451       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24452                kwd->u.value);
24453       cp_parser_skip_to_end_of_block_or_statement (parser);
24454     }
24455
24456   return error_mark_node;
24457 }
24458
24459 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24460    look ahead to see if an objc keyword follows the attributes.  This
24461    is to detect the use of prefix attributes on ObjC @interface and 
24462    @protocol.  */
24463
24464 static bool
24465 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24466 {
24467   cp_lexer_save_tokens (parser->lexer);
24468   *attrib = cp_parser_attributes_opt (parser);
24469   gcc_assert (*attrib);
24470   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24471     {
24472       cp_lexer_commit_tokens (parser->lexer);
24473       return true;
24474     }
24475   cp_lexer_rollback_tokens (parser->lexer);
24476   return false;  
24477 }
24478
24479 /* This routine is a minimal replacement for
24480    c_parser_struct_declaration () used when parsing the list of
24481    types/names or ObjC++ properties.  For example, when parsing the
24482    code
24483
24484    @property (readonly) int a, b, c;
24485
24486    this function is responsible for parsing "int a, int b, int c" and
24487    returning the declarations as CHAIN of DECLs.
24488
24489    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24490    similar parsing.  */
24491 static tree
24492 cp_parser_objc_struct_declaration (cp_parser *parser)
24493 {
24494   tree decls = NULL_TREE;
24495   cp_decl_specifier_seq declspecs;
24496   int decl_class_or_enum_p;
24497   tree prefix_attributes;
24498
24499   cp_parser_decl_specifier_seq (parser,
24500                                 CP_PARSER_FLAGS_NONE,
24501                                 &declspecs,
24502                                 &decl_class_or_enum_p);
24503
24504   if (declspecs.type == error_mark_node)
24505     return error_mark_node;
24506
24507   /* auto, register, static, extern, mutable.  */
24508   if (declspecs.storage_class != sc_none)
24509     {
24510       cp_parser_error (parser, "invalid type for property");
24511       declspecs.storage_class = sc_none;
24512     }
24513   
24514   /* __thread.  */
24515   if (declspecs.specs[(int) ds_thread])
24516     {
24517       cp_parser_error (parser, "invalid type for property");
24518       declspecs.specs[(int) ds_thread] = 0;
24519     }
24520   
24521   /* typedef.  */
24522   if (declspecs.specs[(int) ds_typedef])
24523     {
24524       cp_parser_error (parser, "invalid type for property");
24525       declspecs.specs[(int) ds_typedef] = 0;
24526     }
24527
24528   prefix_attributes = declspecs.attributes;
24529   declspecs.attributes = NULL_TREE;
24530
24531   /* Keep going until we hit the `;' at the end of the declaration. */
24532   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24533     {
24534       tree attributes, first_attribute, decl;
24535       cp_declarator *declarator;
24536       cp_token *token;
24537
24538       /* Parse the declarator.  */
24539       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24540                                          NULL, NULL, false);
24541
24542       /* Look for attributes that apply to the ivar.  */
24543       attributes = cp_parser_attributes_opt (parser);
24544       /* Remember which attributes are prefix attributes and
24545          which are not.  */
24546       first_attribute = attributes;
24547       /* Combine the attributes.  */
24548       attributes = chainon (prefix_attributes, attributes);
24549       
24550       decl = grokfield (declarator, &declspecs,
24551                         NULL_TREE, /*init_const_expr_p=*/false,
24552                         NULL_TREE, attributes);
24553
24554       if (decl == error_mark_node || decl == NULL_TREE)
24555         return error_mark_node;
24556       
24557       /* Reset PREFIX_ATTRIBUTES.  */
24558       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24559         attributes = TREE_CHAIN (attributes);
24560       if (attributes)
24561         TREE_CHAIN (attributes) = NULL_TREE;
24562
24563       DECL_CHAIN (decl) = decls;
24564       decls = decl;
24565
24566       token = cp_lexer_peek_token (parser->lexer);
24567       if (token->type == CPP_COMMA)
24568         {
24569           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24570           continue;
24571         }
24572       else
24573         break;
24574     }
24575   return decls;
24576 }
24577
24578 /* Parse an Objective-C @property declaration.  The syntax is:
24579
24580    objc-property-declaration:
24581      '@property' objc-property-attributes[opt] struct-declaration ;
24582
24583    objc-property-attributes:
24584     '(' objc-property-attribute-list ')'
24585
24586    objc-property-attribute-list:
24587      objc-property-attribute
24588      objc-property-attribute-list, objc-property-attribute
24589
24590    objc-property-attribute
24591      'getter' = identifier
24592      'setter' = identifier
24593      'readonly'
24594      'readwrite'
24595      'assign'
24596      'retain'
24597      'copy'
24598      'nonatomic'
24599
24600   For example:
24601     @property NSString *name;
24602     @property (readonly) id object;
24603     @property (retain, nonatomic, getter=getTheName) id name;
24604     @property int a, b, c;
24605
24606    PS: This function is identical to
24607    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24608 static void 
24609 cp_parser_objc_at_property_declaration (cp_parser *parser)
24610 {
24611   /* The following variables hold the attributes of the properties as
24612      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24613      seen.  When we see an attribute, we set them to 'true' (if they
24614      are boolean properties) or to the identifier (if they have an
24615      argument, ie, for getter and setter).  Note that here we only
24616      parse the list of attributes, check the syntax and accumulate the
24617      attributes that we find.  objc_add_property_declaration() will
24618      then process the information.  */
24619   bool property_assign = false;
24620   bool property_copy = false;
24621   tree property_getter_ident = NULL_TREE;
24622   bool property_nonatomic = false;
24623   bool property_readonly = false;
24624   bool property_readwrite = false;
24625   bool property_retain = false;
24626   tree property_setter_ident = NULL_TREE;
24627
24628   /* 'properties' is the list of properties that we read.  Usually a
24629      single one, but maybe more (eg, in "@property int a, b, c;" there
24630      are three).  */
24631   tree properties;
24632   location_t loc;
24633
24634   loc = cp_lexer_peek_token (parser->lexer)->location;
24635
24636   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24637
24638   /* Parse the optional attribute list...  */
24639   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24640     {
24641       /* Eat the '('.  */
24642       cp_lexer_consume_token (parser->lexer);
24643
24644       while (true)
24645         {
24646           bool syntax_error = false;
24647           cp_token *token = cp_lexer_peek_token (parser->lexer);
24648           enum rid keyword;
24649
24650           if (token->type != CPP_NAME)
24651             {
24652               cp_parser_error (parser, "expected identifier");
24653               break;
24654             }
24655           keyword = C_RID_CODE (token->u.value);
24656           cp_lexer_consume_token (parser->lexer);
24657           switch (keyword)
24658             {
24659             case RID_ASSIGN:    property_assign = true;    break;
24660             case RID_COPY:      property_copy = true;      break;
24661             case RID_NONATOMIC: property_nonatomic = true; break;
24662             case RID_READONLY:  property_readonly = true;  break;
24663             case RID_READWRITE: property_readwrite = true; break;
24664             case RID_RETAIN:    property_retain = true;    break;
24665
24666             case RID_GETTER:
24667             case RID_SETTER:
24668               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24669                 {
24670                   if (keyword == RID_GETTER)
24671                     cp_parser_error (parser,
24672                                      "missing %<=%> (after %<getter%> attribute)");
24673                   else
24674                     cp_parser_error (parser,
24675                                      "missing %<=%> (after %<setter%> attribute)");
24676                   syntax_error = true;
24677                   break;
24678                 }
24679               cp_lexer_consume_token (parser->lexer); /* eat the = */
24680               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24681                 {
24682                   cp_parser_error (parser, "expected identifier");
24683                   syntax_error = true;
24684                   break;
24685                 }
24686               if (keyword == RID_SETTER)
24687                 {
24688                   if (property_setter_ident != NULL_TREE)
24689                     {
24690                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24691                       cp_lexer_consume_token (parser->lexer);
24692                     }
24693                   else
24694                     property_setter_ident = cp_parser_objc_selector (parser);
24695                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24696                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24697                   else
24698                     cp_lexer_consume_token (parser->lexer);
24699                 }
24700               else
24701                 {
24702                   if (property_getter_ident != NULL_TREE)
24703                     {
24704                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24705                       cp_lexer_consume_token (parser->lexer);
24706                     }
24707                   else
24708                     property_getter_ident = cp_parser_objc_selector (parser);
24709                 }
24710               break;
24711             default:
24712               cp_parser_error (parser, "unknown property attribute");
24713               syntax_error = true;
24714               break;
24715             }
24716
24717           if (syntax_error)
24718             break;
24719
24720           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24721             cp_lexer_consume_token (parser->lexer);
24722           else
24723             break;
24724         }
24725
24726       /* FIXME: "@property (setter, assign);" will generate a spurious
24727          "error: expected â€˜)’ before â€˜,’ token".  This is because
24728          cp_parser_require, unlike the C counterpart, will produce an
24729          error even if we are in error recovery.  */
24730       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24731         {
24732           cp_parser_skip_to_closing_parenthesis (parser,
24733                                                  /*recovering=*/true,
24734                                                  /*or_comma=*/false,
24735                                                  /*consume_paren=*/true);
24736         }
24737     }
24738
24739   /* ... and the property declaration(s).  */
24740   properties = cp_parser_objc_struct_declaration (parser);
24741
24742   if (properties == error_mark_node)
24743     {
24744       cp_parser_skip_to_end_of_statement (parser);
24745       /* If the next token is now a `;', consume it.  */
24746       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24747         cp_lexer_consume_token (parser->lexer);
24748       return;
24749     }
24750
24751   if (properties == NULL_TREE)
24752     cp_parser_error (parser, "expected identifier");
24753   else
24754     {
24755       /* Comma-separated properties are chained together in
24756          reverse order; add them one by one.  */
24757       properties = nreverse (properties);
24758       
24759       for (; properties; properties = TREE_CHAIN (properties))
24760         objc_add_property_declaration (loc, copy_node (properties),
24761                                        property_readonly, property_readwrite,
24762                                        property_assign, property_retain,
24763                                        property_copy, property_nonatomic,
24764                                        property_getter_ident, property_setter_ident);
24765     }
24766   
24767   cp_parser_consume_semicolon_at_end_of_statement (parser);
24768 }
24769
24770 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24771
24772    objc-synthesize-declaration:
24773      @synthesize objc-synthesize-identifier-list ;
24774
24775    objc-synthesize-identifier-list:
24776      objc-synthesize-identifier
24777      objc-synthesize-identifier-list, objc-synthesize-identifier
24778
24779    objc-synthesize-identifier
24780      identifier
24781      identifier = identifier
24782
24783   For example:
24784     @synthesize MyProperty;
24785     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24786
24787   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24788   for C.  Keep them in sync.
24789 */
24790 static void 
24791 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24792 {
24793   tree list = NULL_TREE;
24794   location_t loc;
24795   loc = cp_lexer_peek_token (parser->lexer)->location;
24796
24797   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24798   while (true)
24799     {
24800       tree property, ivar;
24801       property = cp_parser_identifier (parser);
24802       if (property == error_mark_node)
24803         {
24804           cp_parser_consume_semicolon_at_end_of_statement (parser);
24805           return;
24806         }
24807       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24808         {
24809           cp_lexer_consume_token (parser->lexer);
24810           ivar = cp_parser_identifier (parser);
24811           if (ivar == error_mark_node)
24812             {
24813               cp_parser_consume_semicolon_at_end_of_statement (parser);
24814               return;
24815             }
24816         }
24817       else
24818         ivar = NULL_TREE;
24819       list = chainon (list, build_tree_list (ivar, property));
24820       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24821         cp_lexer_consume_token (parser->lexer);
24822       else
24823         break;
24824     }
24825   cp_parser_consume_semicolon_at_end_of_statement (parser);
24826   objc_add_synthesize_declaration (loc, list);
24827 }
24828
24829 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24830
24831    objc-dynamic-declaration:
24832      @dynamic identifier-list ;
24833
24834    For example:
24835      @dynamic MyProperty;
24836      @dynamic MyProperty, AnotherProperty;
24837
24838   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24839   for C.  Keep them in sync.
24840 */
24841 static void 
24842 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24843 {
24844   tree list = NULL_TREE;
24845   location_t loc;
24846   loc = cp_lexer_peek_token (parser->lexer)->location;
24847
24848   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24849   while (true)
24850     {
24851       tree property;
24852       property = cp_parser_identifier (parser);
24853       if (property == error_mark_node)
24854         {
24855           cp_parser_consume_semicolon_at_end_of_statement (parser);
24856           return;
24857         }
24858       list = chainon (list, build_tree_list (NULL, property));
24859       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24860         cp_lexer_consume_token (parser->lexer);
24861       else
24862         break;
24863     }
24864   cp_parser_consume_semicolon_at_end_of_statement (parser);
24865   objc_add_dynamic_declaration (loc, list);
24866 }
24867
24868 \f
24869 /* OpenMP 2.5 parsing routines.  */
24870
24871 /* Returns name of the next clause.
24872    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24873    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24874    returned and the token is consumed.  */
24875
24876 static pragma_omp_clause
24877 cp_parser_omp_clause_name (cp_parser *parser)
24878 {
24879   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24880
24881   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24882     result = PRAGMA_OMP_CLAUSE_IF;
24883   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24884     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24885   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24886     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24887   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24888     {
24889       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24890       const char *p = IDENTIFIER_POINTER (id);
24891
24892       switch (p[0])
24893         {
24894         case 'c':
24895           if (!strcmp ("collapse", p))
24896             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24897           else if (!strcmp ("copyin", p))
24898             result = PRAGMA_OMP_CLAUSE_COPYIN;
24899           else if (!strcmp ("copyprivate", p))
24900             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24901           break;
24902         case 'f':
24903           if (!strcmp ("final", p))
24904             result = PRAGMA_OMP_CLAUSE_FINAL;
24905           else if (!strcmp ("firstprivate", p))
24906             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24907           break;
24908         case 'l':
24909           if (!strcmp ("lastprivate", p))
24910             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24911           break;
24912         case 'm':
24913           if (!strcmp ("mergeable", p))
24914             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24915           break;
24916         case 'n':
24917           if (!strcmp ("nowait", p))
24918             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24919           else if (!strcmp ("num_threads", p))
24920             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24921           break;
24922         case 'o':
24923           if (!strcmp ("ordered", p))
24924             result = PRAGMA_OMP_CLAUSE_ORDERED;
24925           break;
24926         case 'r':
24927           if (!strcmp ("reduction", p))
24928             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24929           break;
24930         case 's':
24931           if (!strcmp ("schedule", p))
24932             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24933           else if (!strcmp ("shared", p))
24934             result = PRAGMA_OMP_CLAUSE_SHARED;
24935           break;
24936         case 'u':
24937           if (!strcmp ("untied", p))
24938             result = PRAGMA_OMP_CLAUSE_UNTIED;
24939           break;
24940         }
24941     }
24942
24943   if (result != PRAGMA_OMP_CLAUSE_NONE)
24944     cp_lexer_consume_token (parser->lexer);
24945
24946   return result;
24947 }
24948
24949 /* Validate that a clause of the given type does not already exist.  */
24950
24951 static void
24952 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24953                            const char *name, location_t location)
24954 {
24955   tree c;
24956
24957   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24958     if (OMP_CLAUSE_CODE (c) == code)
24959       {
24960         error_at (location, "too many %qs clauses", name);
24961         break;
24962       }
24963 }
24964
24965 /* OpenMP 2.5:
24966    variable-list:
24967      identifier
24968      variable-list , identifier
24969
24970    In addition, we match a closing parenthesis.  An opening parenthesis
24971    will have been consumed by the caller.
24972
24973    If KIND is nonzero, create the appropriate node and install the decl
24974    in OMP_CLAUSE_DECL and add the node to the head of the list.
24975
24976    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24977    return the list created.  */
24978
24979 static tree
24980 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24981                                 tree list)
24982 {
24983   cp_token *token;
24984   while (1)
24985     {
24986       tree name, decl;
24987
24988       token = cp_lexer_peek_token (parser->lexer);
24989       name = cp_parser_id_expression (parser, /*template_p=*/false,
24990                                       /*check_dependency_p=*/true,
24991                                       /*template_p=*/NULL,
24992                                       /*declarator_p=*/false,
24993                                       /*optional_p=*/false);
24994       if (name == error_mark_node)
24995         goto skip_comma;
24996
24997       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24998       if (decl == error_mark_node)
24999         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
25000                                      token->location);
25001       else if (kind != 0)
25002         {
25003           tree u = build_omp_clause (token->location, kind);
25004           OMP_CLAUSE_DECL (u) = decl;
25005           OMP_CLAUSE_CHAIN (u) = list;
25006           list = u;
25007         }
25008       else
25009         list = tree_cons (decl, NULL_TREE, list);
25010
25011     get_comma:
25012       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25013         break;
25014       cp_lexer_consume_token (parser->lexer);
25015     }
25016
25017   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25018     {
25019       int ending;
25020
25021       /* Try to resync to an unnested comma.  Copied from
25022          cp_parser_parenthesized_expression_list.  */
25023     skip_comma:
25024       ending = cp_parser_skip_to_closing_parenthesis (parser,
25025                                                       /*recovering=*/true,
25026                                                       /*or_comma=*/true,
25027                                                       /*consume_paren=*/true);
25028       if (ending < 0)
25029         goto get_comma;
25030     }
25031
25032   return list;
25033 }
25034
25035 /* Similarly, but expect leading and trailing parenthesis.  This is a very
25036    common case for omp clauses.  */
25037
25038 static tree
25039 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25040 {
25041   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25042     return cp_parser_omp_var_list_no_open (parser, kind, list);
25043   return list;
25044 }
25045
25046 /* OpenMP 3.0:
25047    collapse ( constant-expression ) */
25048
25049 static tree
25050 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25051 {
25052   tree c, num;
25053   location_t loc;
25054   HOST_WIDE_INT n;
25055
25056   loc = cp_lexer_peek_token (parser->lexer)->location;
25057   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25058     return list;
25059
25060   num = cp_parser_constant_expression (parser, false, NULL);
25061
25062   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25063     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25064                                            /*or_comma=*/false,
25065                                            /*consume_paren=*/true);
25066
25067   if (num == error_mark_node)
25068     return list;
25069   num = fold_non_dependent_expr (num);
25070   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25071       || !host_integerp (num, 0)
25072       || (n = tree_low_cst (num, 0)) <= 0
25073       || (int) n != n)
25074     {
25075       error_at (loc, "collapse argument needs positive constant integer expression");
25076       return list;
25077     }
25078
25079   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25080   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25081   OMP_CLAUSE_CHAIN (c) = list;
25082   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25083
25084   return c;
25085 }
25086
25087 /* OpenMP 2.5:
25088    default ( shared | none ) */
25089
25090 static tree
25091 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25092 {
25093   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25094   tree c;
25095
25096   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25097     return list;
25098   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25099     {
25100       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25101       const char *p = IDENTIFIER_POINTER (id);
25102
25103       switch (p[0])
25104         {
25105         case 'n':
25106           if (strcmp ("none", p) != 0)
25107             goto invalid_kind;
25108           kind = OMP_CLAUSE_DEFAULT_NONE;
25109           break;
25110
25111         case 's':
25112           if (strcmp ("shared", p) != 0)
25113             goto invalid_kind;
25114           kind = OMP_CLAUSE_DEFAULT_SHARED;
25115           break;
25116
25117         default:
25118           goto invalid_kind;
25119         }
25120
25121       cp_lexer_consume_token (parser->lexer);
25122     }
25123   else
25124     {
25125     invalid_kind:
25126       cp_parser_error (parser, "expected %<none%> or %<shared%>");
25127     }
25128
25129   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25130     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25131                                            /*or_comma=*/false,
25132                                            /*consume_paren=*/true);
25133
25134   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25135     return list;
25136
25137   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25138   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25139   OMP_CLAUSE_CHAIN (c) = list;
25140   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25141
25142   return c;
25143 }
25144
25145 /* OpenMP 3.1:
25146    final ( expression ) */
25147
25148 static tree
25149 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25150 {
25151   tree t, c;
25152
25153   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25154     return list;
25155
25156   t = cp_parser_condition (parser);
25157
25158   if (t == error_mark_node
25159       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25160     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25161                                            /*or_comma=*/false,
25162                                            /*consume_paren=*/true);
25163
25164   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25165
25166   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25167   OMP_CLAUSE_FINAL_EXPR (c) = t;
25168   OMP_CLAUSE_CHAIN (c) = list;
25169
25170   return c;
25171 }
25172
25173 /* OpenMP 2.5:
25174    if ( expression ) */
25175
25176 static tree
25177 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25178 {
25179   tree t, c;
25180
25181   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25182     return list;
25183
25184   t = cp_parser_condition (parser);
25185
25186   if (t == error_mark_node
25187       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25188     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25189                                            /*or_comma=*/false,
25190                                            /*consume_paren=*/true);
25191
25192   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25193
25194   c = build_omp_clause (location, OMP_CLAUSE_IF);
25195   OMP_CLAUSE_IF_EXPR (c) = t;
25196   OMP_CLAUSE_CHAIN (c) = list;
25197
25198   return c;
25199 }
25200
25201 /* OpenMP 3.1:
25202    mergeable */
25203
25204 static tree
25205 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25206                                 tree list, location_t location)
25207 {
25208   tree c;
25209
25210   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25211                              location);
25212
25213   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25214   OMP_CLAUSE_CHAIN (c) = list;
25215   return c;
25216 }
25217
25218 /* OpenMP 2.5:
25219    nowait */
25220
25221 static tree
25222 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25223                              tree list, location_t location)
25224 {
25225   tree c;
25226
25227   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25228
25229   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25230   OMP_CLAUSE_CHAIN (c) = list;
25231   return c;
25232 }
25233
25234 /* OpenMP 2.5:
25235    num_threads ( expression ) */
25236
25237 static tree
25238 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25239                                   location_t location)
25240 {
25241   tree t, c;
25242
25243   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25244     return list;
25245
25246   t = cp_parser_expression (parser, false, NULL);
25247
25248   if (t == error_mark_node
25249       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25250     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25251                                            /*or_comma=*/false,
25252                                            /*consume_paren=*/true);
25253
25254   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25255                              "num_threads", location);
25256
25257   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25258   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25259   OMP_CLAUSE_CHAIN (c) = list;
25260
25261   return c;
25262 }
25263
25264 /* OpenMP 2.5:
25265    ordered */
25266
25267 static tree
25268 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25269                               tree list, location_t location)
25270 {
25271   tree c;
25272
25273   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25274                              "ordered", location);
25275
25276   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25277   OMP_CLAUSE_CHAIN (c) = list;
25278   return c;
25279 }
25280
25281 /* OpenMP 2.5:
25282    reduction ( reduction-operator : variable-list )
25283
25284    reduction-operator:
25285      One of: + * - & ^ | && ||
25286
25287    OpenMP 3.1:
25288
25289    reduction-operator:
25290      One of: + * - & ^ | && || min max  */
25291
25292 static tree
25293 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25294 {
25295   enum tree_code code;
25296   tree nlist, c;
25297
25298   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25299     return list;
25300
25301   switch (cp_lexer_peek_token (parser->lexer)->type)
25302     {
25303     case CPP_PLUS:
25304       code = PLUS_EXPR;
25305       break;
25306     case CPP_MULT:
25307       code = MULT_EXPR;
25308       break;
25309     case CPP_MINUS:
25310       code = MINUS_EXPR;
25311       break;
25312     case CPP_AND:
25313       code = BIT_AND_EXPR;
25314       break;
25315     case CPP_XOR:
25316       code = BIT_XOR_EXPR;
25317       break;
25318     case CPP_OR:
25319       code = BIT_IOR_EXPR;
25320       break;
25321     case CPP_AND_AND:
25322       code = TRUTH_ANDIF_EXPR;
25323       break;
25324     case CPP_OR_OR:
25325       code = TRUTH_ORIF_EXPR;
25326       break;
25327     case CPP_NAME:
25328       {
25329         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25330         const char *p = IDENTIFIER_POINTER (id);
25331
25332         if (strcmp (p, "min") == 0)
25333           {
25334             code = MIN_EXPR;
25335             break;
25336           }
25337         if (strcmp (p, "max") == 0)
25338           {
25339             code = MAX_EXPR;
25340             break;
25341           }
25342       }
25343       /* FALLTHROUGH */
25344     default:
25345       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25346                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25347     resync_fail:
25348       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25349                                              /*or_comma=*/false,
25350                                              /*consume_paren=*/true);
25351       return list;
25352     }
25353   cp_lexer_consume_token (parser->lexer);
25354
25355   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25356     goto resync_fail;
25357
25358   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25359   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25360     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25361
25362   return nlist;
25363 }
25364
25365 /* OpenMP 2.5:
25366    schedule ( schedule-kind )
25367    schedule ( schedule-kind , expression )
25368
25369    schedule-kind:
25370      static | dynamic | guided | runtime | auto  */
25371
25372 static tree
25373 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25374 {
25375   tree c, t;
25376
25377   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25378     return list;
25379
25380   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25381
25382   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25383     {
25384       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25385       const char *p = IDENTIFIER_POINTER (id);
25386
25387       switch (p[0])
25388         {
25389         case 'd':
25390           if (strcmp ("dynamic", p) != 0)
25391             goto invalid_kind;
25392           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25393           break;
25394
25395         case 'g':
25396           if (strcmp ("guided", p) != 0)
25397             goto invalid_kind;
25398           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25399           break;
25400
25401         case 'r':
25402           if (strcmp ("runtime", p) != 0)
25403             goto invalid_kind;
25404           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25405           break;
25406
25407         default:
25408           goto invalid_kind;
25409         }
25410     }
25411   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25412     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25413   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25414     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25415   else
25416     goto invalid_kind;
25417   cp_lexer_consume_token (parser->lexer);
25418
25419   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25420     {
25421       cp_token *token;
25422       cp_lexer_consume_token (parser->lexer);
25423
25424       token = cp_lexer_peek_token (parser->lexer);
25425       t = cp_parser_assignment_expression (parser, false, NULL);
25426
25427       if (t == error_mark_node)
25428         goto resync_fail;
25429       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25430         error_at (token->location, "schedule %<runtime%> does not take "
25431                   "a %<chunk_size%> parameter");
25432       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25433         error_at (token->location, "schedule %<auto%> does not take "
25434                   "a %<chunk_size%> parameter");
25435       else
25436         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25437
25438       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25439         goto resync_fail;
25440     }
25441   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25442     goto resync_fail;
25443
25444   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25445   OMP_CLAUSE_CHAIN (c) = list;
25446   return c;
25447
25448  invalid_kind:
25449   cp_parser_error (parser, "invalid schedule kind");
25450  resync_fail:
25451   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25452                                          /*or_comma=*/false,
25453                                          /*consume_paren=*/true);
25454   return list;
25455 }
25456
25457 /* OpenMP 3.0:
25458    untied */
25459
25460 static tree
25461 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25462                              tree list, location_t location)
25463 {
25464   tree c;
25465
25466   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25467
25468   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25469   OMP_CLAUSE_CHAIN (c) = list;
25470   return c;
25471 }
25472
25473 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25474    is a bitmask in MASK.  Return the list of clauses found; the result
25475    of clause default goes in *pdefault.  */
25476
25477 static tree
25478 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25479                            const char *where, cp_token *pragma_tok)
25480 {
25481   tree clauses = NULL;
25482   bool first = true;
25483   cp_token *token = NULL;
25484
25485   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25486     {
25487       pragma_omp_clause c_kind;
25488       const char *c_name;
25489       tree prev = clauses;
25490
25491       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25492         cp_lexer_consume_token (parser->lexer);
25493
25494       token = cp_lexer_peek_token (parser->lexer);
25495       c_kind = cp_parser_omp_clause_name (parser);
25496       first = false;
25497
25498       switch (c_kind)
25499         {
25500         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25501           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25502                                                    token->location);
25503           c_name = "collapse";
25504           break;
25505         case PRAGMA_OMP_CLAUSE_COPYIN:
25506           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25507           c_name = "copyin";
25508           break;
25509         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25510           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25511                                             clauses);
25512           c_name = "copyprivate";
25513           break;
25514         case PRAGMA_OMP_CLAUSE_DEFAULT:
25515           clauses = cp_parser_omp_clause_default (parser, clauses,
25516                                                   token->location);
25517           c_name = "default";
25518           break;
25519         case PRAGMA_OMP_CLAUSE_FINAL:
25520           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25521           c_name = "final";
25522           break;
25523         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25524           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25525                                             clauses);
25526           c_name = "firstprivate";
25527           break;
25528         case PRAGMA_OMP_CLAUSE_IF:
25529           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25530           c_name = "if";
25531           break;
25532         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25533           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25534                                             clauses);
25535           c_name = "lastprivate";
25536           break;
25537         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25538           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25539                                                     token->location);
25540           c_name = "mergeable";
25541           break;
25542         case PRAGMA_OMP_CLAUSE_NOWAIT:
25543           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25544           c_name = "nowait";
25545           break;
25546         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25547           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25548                                                       token->location);
25549           c_name = "num_threads";
25550           break;
25551         case PRAGMA_OMP_CLAUSE_ORDERED:
25552           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25553                                                   token->location);
25554           c_name = "ordered";
25555           break;
25556         case PRAGMA_OMP_CLAUSE_PRIVATE:
25557           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25558                                             clauses);
25559           c_name = "private";
25560           break;
25561         case PRAGMA_OMP_CLAUSE_REDUCTION:
25562           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25563           c_name = "reduction";
25564           break;
25565         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25566           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25567                                                    token->location);
25568           c_name = "schedule";
25569           break;
25570         case PRAGMA_OMP_CLAUSE_SHARED:
25571           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25572                                             clauses);
25573           c_name = "shared";
25574           break;
25575         case PRAGMA_OMP_CLAUSE_UNTIED:
25576           clauses = cp_parser_omp_clause_untied (parser, clauses,
25577                                                  token->location);
25578           c_name = "nowait";
25579           break;
25580         default:
25581           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25582           goto saw_error;
25583         }
25584
25585       if (((mask >> c_kind) & 1) == 0)
25586         {
25587           /* Remove the invalid clause(s) from the list to avoid
25588              confusing the rest of the compiler.  */
25589           clauses = prev;
25590           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25591         }
25592     }
25593  saw_error:
25594   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25595   return finish_omp_clauses (clauses);
25596 }
25597
25598 /* OpenMP 2.5:
25599    structured-block:
25600      statement
25601
25602    In practice, we're also interested in adding the statement to an
25603    outer node.  So it is convenient if we work around the fact that
25604    cp_parser_statement calls add_stmt.  */
25605
25606 static unsigned
25607 cp_parser_begin_omp_structured_block (cp_parser *parser)
25608 {
25609   unsigned save = parser->in_statement;
25610
25611   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25612      This preserves the "not within loop or switch" style error messages
25613      for nonsense cases like
25614         void foo() {
25615         #pragma omp single
25616           break;
25617         }
25618   */
25619   if (parser->in_statement)
25620     parser->in_statement = IN_OMP_BLOCK;
25621
25622   return save;
25623 }
25624
25625 static void
25626 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25627 {
25628   parser->in_statement = save;
25629 }
25630
25631 static tree
25632 cp_parser_omp_structured_block (cp_parser *parser)
25633 {
25634   tree stmt = begin_omp_structured_block ();
25635   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25636
25637   cp_parser_statement (parser, NULL_TREE, false, NULL);
25638
25639   cp_parser_end_omp_structured_block (parser, save);
25640   return finish_omp_structured_block (stmt);
25641 }
25642
25643 /* OpenMP 2.5:
25644    # pragma omp atomic new-line
25645      expression-stmt
25646
25647    expression-stmt:
25648      x binop= expr | x++ | ++x | x-- | --x
25649    binop:
25650      +, *, -, /, &, ^, |, <<, >>
25651
25652   where x is an lvalue expression with scalar type.
25653
25654    OpenMP 3.1:
25655    # pragma omp atomic new-line
25656      update-stmt
25657
25658    # pragma omp atomic read new-line
25659      read-stmt
25660
25661    # pragma omp atomic write new-line
25662      write-stmt
25663
25664    # pragma omp atomic update new-line
25665      update-stmt
25666
25667    # pragma omp atomic capture new-line
25668      capture-stmt
25669
25670    # pragma omp atomic capture new-line
25671      capture-block
25672
25673    read-stmt:
25674      v = x
25675    write-stmt:
25676      x = expr
25677    update-stmt:
25678      expression-stmt | x = x binop expr
25679    capture-stmt:
25680      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25681    capture-block:
25682      { v = x; update-stmt; } | { update-stmt; v = x; }
25683
25684   where x and v are lvalue expressions with scalar type.  */
25685
25686 static void
25687 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25688 {
25689   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25690   tree rhs1 = NULL_TREE, orig_lhs;
25691   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25692   bool structured_block = false;
25693
25694   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25695     {
25696       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25697       const char *p = IDENTIFIER_POINTER (id);
25698
25699       if (!strcmp (p, "read"))
25700         code = OMP_ATOMIC_READ;
25701       else if (!strcmp (p, "write"))
25702         code = NOP_EXPR;
25703       else if (!strcmp (p, "update"))
25704         code = OMP_ATOMIC;
25705       else if (!strcmp (p, "capture"))
25706         code = OMP_ATOMIC_CAPTURE_NEW;
25707       else
25708         p = NULL;
25709       if (p)
25710         cp_lexer_consume_token (parser->lexer);
25711     }
25712   cp_parser_require_pragma_eol (parser, pragma_tok);
25713
25714   switch (code)
25715     {
25716     case OMP_ATOMIC_READ:
25717     case NOP_EXPR: /* atomic write */
25718       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25719                                       /*cast_p=*/false, NULL);
25720       if (v == error_mark_node)
25721         goto saw_error;
25722       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25723         goto saw_error;
25724       if (code == NOP_EXPR)
25725         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25726       else
25727         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25728                                           /*cast_p=*/false, NULL);
25729       if (lhs == error_mark_node)
25730         goto saw_error;
25731       if (code == NOP_EXPR)
25732         {
25733           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25734              opcode.  */
25735           code = OMP_ATOMIC;
25736           rhs = lhs;
25737           lhs = v;
25738           v = NULL_TREE;
25739         }
25740       goto done;
25741     case OMP_ATOMIC_CAPTURE_NEW:
25742       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25743         {
25744           cp_lexer_consume_token (parser->lexer);
25745           structured_block = true;
25746         }
25747       else
25748         {
25749           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25750                                           /*cast_p=*/false, NULL);
25751           if (v == error_mark_node)
25752             goto saw_error;
25753           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25754             goto saw_error;
25755         }
25756     default:
25757       break;
25758     }
25759
25760 restart:
25761   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25762                                     /*cast_p=*/false, NULL);
25763   orig_lhs = lhs;
25764   switch (TREE_CODE (lhs))
25765     {
25766     case ERROR_MARK:
25767       goto saw_error;
25768
25769     case POSTINCREMENT_EXPR:
25770       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25771         code = OMP_ATOMIC_CAPTURE_OLD;
25772       /* FALLTHROUGH */
25773     case PREINCREMENT_EXPR:
25774       lhs = TREE_OPERAND (lhs, 0);
25775       opcode = PLUS_EXPR;
25776       rhs = integer_one_node;
25777       break;
25778
25779     case POSTDECREMENT_EXPR:
25780       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25781         code = OMP_ATOMIC_CAPTURE_OLD;
25782       /* FALLTHROUGH */
25783     case PREDECREMENT_EXPR:
25784       lhs = TREE_OPERAND (lhs, 0);
25785       opcode = MINUS_EXPR;
25786       rhs = integer_one_node;
25787       break;
25788
25789     case COMPOUND_EXPR:
25790       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25791          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25792          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25793          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25794          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25795                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25796             == BOOLEAN_TYPE)
25797        /* Undo effects of boolean_increment for post {in,de}crement.  */
25798        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25799       /* FALLTHRU */
25800     case MODIFY_EXPR:
25801       if (TREE_CODE (lhs) == MODIFY_EXPR
25802          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25803         {
25804           /* Undo effects of boolean_increment.  */
25805           if (integer_onep (TREE_OPERAND (lhs, 1)))
25806             {
25807               /* This is pre or post increment.  */
25808               rhs = TREE_OPERAND (lhs, 1);
25809               lhs = TREE_OPERAND (lhs, 0);
25810               opcode = NOP_EXPR;
25811               if (code == OMP_ATOMIC_CAPTURE_NEW
25812                   && !structured_block
25813                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25814                 code = OMP_ATOMIC_CAPTURE_OLD;
25815               break;
25816             }
25817         }
25818       /* FALLTHRU */
25819     default:
25820       switch (cp_lexer_peek_token (parser->lexer)->type)
25821         {
25822         case CPP_MULT_EQ:
25823           opcode = MULT_EXPR;
25824           break;
25825         case CPP_DIV_EQ:
25826           opcode = TRUNC_DIV_EXPR;
25827           break;
25828         case CPP_PLUS_EQ:
25829           opcode = PLUS_EXPR;
25830           break;
25831         case CPP_MINUS_EQ:
25832           opcode = MINUS_EXPR;
25833           break;
25834         case CPP_LSHIFT_EQ:
25835           opcode = LSHIFT_EXPR;
25836           break;
25837         case CPP_RSHIFT_EQ:
25838           opcode = RSHIFT_EXPR;
25839           break;
25840         case CPP_AND_EQ:
25841           opcode = BIT_AND_EXPR;
25842           break;
25843         case CPP_OR_EQ:
25844           opcode = BIT_IOR_EXPR;
25845           break;
25846         case CPP_XOR_EQ:
25847           opcode = BIT_XOR_EXPR;
25848           break;
25849         case CPP_EQ:
25850           if (structured_block || code == OMP_ATOMIC)
25851             {
25852               enum cp_parser_prec oprec;
25853               cp_token *token;
25854               cp_lexer_consume_token (parser->lexer);
25855               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25856                                                  /*cast_p=*/false, NULL);
25857               if (rhs1 == error_mark_node)
25858                 goto saw_error;
25859               token = cp_lexer_peek_token (parser->lexer);
25860               switch (token->type)
25861                 {
25862                 case CPP_SEMICOLON:
25863                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25864                     {
25865                       code = OMP_ATOMIC_CAPTURE_OLD;
25866                       v = lhs;
25867                       lhs = NULL_TREE;
25868                       lhs1 = rhs1;
25869                       rhs1 = NULL_TREE;
25870                       cp_lexer_consume_token (parser->lexer);
25871                       goto restart;
25872                     }
25873                   cp_parser_error (parser,
25874                                    "invalid form of %<#pragma omp atomic%>");
25875                   goto saw_error;
25876                 case CPP_MULT:
25877                   opcode = MULT_EXPR;
25878                   break;
25879                 case CPP_DIV:
25880                   opcode = TRUNC_DIV_EXPR;
25881                   break;
25882                 case CPP_PLUS:
25883                   opcode = PLUS_EXPR;
25884                   break;
25885                 case CPP_MINUS:
25886                   opcode = MINUS_EXPR;
25887                   break;
25888                 case CPP_LSHIFT:
25889                   opcode = LSHIFT_EXPR;
25890                   break;
25891                 case CPP_RSHIFT:
25892                   opcode = RSHIFT_EXPR;
25893                   break;
25894                 case CPP_AND:
25895                   opcode = BIT_AND_EXPR;
25896                   break;
25897                 case CPP_OR:
25898                   opcode = BIT_IOR_EXPR;
25899                   break;
25900                 case CPP_XOR:
25901                   opcode = BIT_XOR_EXPR;
25902                   break;
25903                 default:
25904                   cp_parser_error (parser,
25905                                    "invalid operator for %<#pragma omp atomic%>");
25906                   goto saw_error;
25907                 }
25908               oprec = TOKEN_PRECEDENCE (token);
25909               gcc_assert (oprec != PREC_NOT_OPERATOR);
25910               if (commutative_tree_code (opcode))
25911                 oprec = (enum cp_parser_prec) (oprec - 1);
25912               cp_lexer_consume_token (parser->lexer);
25913               rhs = cp_parser_binary_expression (parser, false, false,
25914                                                  oprec, NULL);
25915               if (rhs == error_mark_node)
25916                 goto saw_error;
25917               goto stmt_done;
25918             }
25919           /* FALLTHROUGH */
25920         default:
25921           cp_parser_error (parser,
25922                            "invalid operator for %<#pragma omp atomic%>");
25923           goto saw_error;
25924         }
25925       cp_lexer_consume_token (parser->lexer);
25926
25927       rhs = cp_parser_expression (parser, false, NULL);
25928       if (rhs == error_mark_node)
25929         goto saw_error;
25930       break;
25931     }
25932 stmt_done:
25933   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25934     {
25935       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25936         goto saw_error;
25937       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25938                                       /*cast_p=*/false, NULL);
25939       if (v == error_mark_node)
25940         goto saw_error;
25941       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25942         goto saw_error;
25943       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25944                                          /*cast_p=*/false, NULL);
25945       if (lhs1 == error_mark_node)
25946         goto saw_error;
25947     }
25948   if (structured_block)
25949     {
25950       cp_parser_consume_semicolon_at_end_of_statement (parser);
25951       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25952     }
25953 done:
25954   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25955   if (!structured_block)
25956     cp_parser_consume_semicolon_at_end_of_statement (parser);
25957   return;
25958
25959  saw_error:
25960   cp_parser_skip_to_end_of_block_or_statement (parser);
25961   if (structured_block)
25962     {
25963       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25964         cp_lexer_consume_token (parser->lexer);
25965       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25966         {
25967           cp_parser_skip_to_end_of_block_or_statement (parser);
25968           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25969             cp_lexer_consume_token (parser->lexer);
25970         }
25971     }
25972 }
25973
25974
25975 /* OpenMP 2.5:
25976    # pragma omp barrier new-line  */
25977
25978 static void
25979 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25980 {
25981   cp_parser_require_pragma_eol (parser, pragma_tok);
25982   finish_omp_barrier ();
25983 }
25984
25985 /* OpenMP 2.5:
25986    # pragma omp critical [(name)] new-line
25987      structured-block  */
25988
25989 static tree
25990 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25991 {
25992   tree stmt, name = NULL;
25993
25994   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25995     {
25996       cp_lexer_consume_token (parser->lexer);
25997
25998       name = cp_parser_identifier (parser);
25999
26000       if (name == error_mark_node
26001           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26002         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26003                                                /*or_comma=*/false,
26004                                                /*consume_paren=*/true);
26005       if (name == error_mark_node)
26006         name = NULL;
26007     }
26008   cp_parser_require_pragma_eol (parser, pragma_tok);
26009
26010   stmt = cp_parser_omp_structured_block (parser);
26011   return c_finish_omp_critical (input_location, stmt, name);
26012 }
26013
26014 /* OpenMP 2.5:
26015    # pragma omp flush flush-vars[opt] new-line
26016
26017    flush-vars:
26018      ( variable-list ) */
26019
26020 static void
26021 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26022 {
26023   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26024     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26025   cp_parser_require_pragma_eol (parser, pragma_tok);
26026
26027   finish_omp_flush ();
26028 }
26029
26030 /* Helper function, to parse omp for increment expression.  */
26031
26032 static tree
26033 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26034 {
26035   tree cond = cp_parser_binary_expression (parser, false, true,
26036                                            PREC_NOT_OPERATOR, NULL);
26037   if (cond == error_mark_node
26038       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26039     {
26040       cp_parser_skip_to_end_of_statement (parser);
26041       return error_mark_node;
26042     }
26043
26044   switch (TREE_CODE (cond))
26045     {
26046     case GT_EXPR:
26047     case GE_EXPR:
26048     case LT_EXPR:
26049     case LE_EXPR:
26050       break;
26051     default:
26052       return error_mark_node;
26053     }
26054
26055   /* If decl is an iterator, preserve LHS and RHS of the relational
26056      expr until finish_omp_for.  */
26057   if (decl
26058       && (type_dependent_expression_p (decl)
26059           || CLASS_TYPE_P (TREE_TYPE (decl))))
26060     return cond;
26061
26062   return build_x_binary_op (TREE_CODE (cond),
26063                             TREE_OPERAND (cond, 0), ERROR_MARK,
26064                             TREE_OPERAND (cond, 1), ERROR_MARK,
26065                             /*overload=*/NULL, tf_warning_or_error);
26066 }
26067
26068 /* Helper function, to parse omp for increment expression.  */
26069
26070 static tree
26071 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26072 {
26073   cp_token *token = cp_lexer_peek_token (parser->lexer);
26074   enum tree_code op;
26075   tree lhs, rhs;
26076   cp_id_kind idk;
26077   bool decl_first;
26078
26079   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26080     {
26081       op = (token->type == CPP_PLUS_PLUS
26082             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26083       cp_lexer_consume_token (parser->lexer);
26084       lhs = cp_parser_cast_expression (parser, false, false, NULL);
26085       if (lhs != decl)
26086         return error_mark_node;
26087       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26088     }
26089
26090   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26091   if (lhs != decl)
26092     return error_mark_node;
26093
26094   token = cp_lexer_peek_token (parser->lexer);
26095   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26096     {
26097       op = (token->type == CPP_PLUS_PLUS
26098             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26099       cp_lexer_consume_token (parser->lexer);
26100       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26101     }
26102
26103   op = cp_parser_assignment_operator_opt (parser);
26104   if (op == ERROR_MARK)
26105     return error_mark_node;
26106
26107   if (op != NOP_EXPR)
26108     {
26109       rhs = cp_parser_assignment_expression (parser, false, NULL);
26110       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26111       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26112     }
26113
26114   lhs = cp_parser_binary_expression (parser, false, false,
26115                                      PREC_ADDITIVE_EXPRESSION, NULL);
26116   token = cp_lexer_peek_token (parser->lexer);
26117   decl_first = lhs == decl;
26118   if (decl_first)
26119     lhs = NULL_TREE;
26120   if (token->type != CPP_PLUS
26121       && token->type != CPP_MINUS)
26122     return error_mark_node;
26123
26124   do
26125     {
26126       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26127       cp_lexer_consume_token (parser->lexer);
26128       rhs = cp_parser_binary_expression (parser, false, false,
26129                                          PREC_ADDITIVE_EXPRESSION, NULL);
26130       token = cp_lexer_peek_token (parser->lexer);
26131       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26132         {
26133           if (lhs == NULL_TREE)
26134             {
26135               if (op == PLUS_EXPR)
26136                 lhs = rhs;
26137               else
26138                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
26139             }
26140           else
26141             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
26142                                      NULL, tf_warning_or_error);
26143         }
26144     }
26145   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26146
26147   if (!decl_first)
26148     {
26149       if (rhs != decl || op == MINUS_EXPR)
26150         return error_mark_node;
26151       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26152     }
26153   else
26154     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26155
26156   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26157 }
26158
26159 /* Parse the restricted form of the for statement allowed by OpenMP.  */
26160
26161 static tree
26162 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26163 {
26164   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26165   tree real_decl, initv, condv, incrv, declv;
26166   tree this_pre_body, cl;
26167   location_t loc_first;
26168   bool collapse_err = false;
26169   int i, collapse = 1, nbraces = 0;
26170   VEC(tree,gc) *for_block = make_tree_vector ();
26171
26172   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26173     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26174       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26175
26176   gcc_assert (collapse >= 1);
26177
26178   declv = make_tree_vec (collapse);
26179   initv = make_tree_vec (collapse);
26180   condv = make_tree_vec (collapse);
26181   incrv = make_tree_vec (collapse);
26182
26183   loc_first = cp_lexer_peek_token (parser->lexer)->location;
26184
26185   for (i = 0; i < collapse; i++)
26186     {
26187       int bracecount = 0;
26188       bool add_private_clause = false;
26189       location_t loc;
26190
26191       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26192         {
26193           cp_parser_error (parser, "for statement expected");
26194           return NULL;
26195         }
26196       loc = cp_lexer_consume_token (parser->lexer)->location;
26197
26198       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26199         return NULL;
26200
26201       init = decl = real_decl = NULL;
26202       this_pre_body = push_stmt_list ();
26203       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26204         {
26205           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26206
26207              init-expr:
26208                        var = lb
26209                        integer-type var = lb
26210                        random-access-iterator-type var = lb
26211                        pointer-type var = lb
26212           */
26213           cp_decl_specifier_seq type_specifiers;
26214
26215           /* First, try to parse as an initialized declaration.  See
26216              cp_parser_condition, from whence the bulk of this is copied.  */
26217
26218           cp_parser_parse_tentatively (parser);
26219           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26220                                         /*is_trailing_return=*/false,
26221                                         &type_specifiers);
26222           if (cp_parser_parse_definitely (parser))
26223             {
26224               /* If parsing a type specifier seq succeeded, then this
26225                  MUST be a initialized declaration.  */
26226               tree asm_specification, attributes;
26227               cp_declarator *declarator;
26228
26229               declarator = cp_parser_declarator (parser,
26230                                                  CP_PARSER_DECLARATOR_NAMED,
26231                                                  /*ctor_dtor_or_conv_p=*/NULL,
26232                                                  /*parenthesized_p=*/NULL,
26233                                                  /*member_p=*/false);
26234               attributes = cp_parser_attributes_opt (parser);
26235               asm_specification = cp_parser_asm_specification_opt (parser);
26236
26237               if (declarator == cp_error_declarator) 
26238                 cp_parser_skip_to_end_of_statement (parser);
26239
26240               else 
26241                 {
26242                   tree pushed_scope, auto_node;
26243
26244                   decl = start_decl (declarator, &type_specifiers,
26245                                      SD_INITIALIZED, attributes,
26246                                      /*prefix_attributes=*/NULL_TREE,
26247                                      &pushed_scope);
26248
26249                   auto_node = type_uses_auto (TREE_TYPE (decl));
26250                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26251                     {
26252                       if (cp_lexer_next_token_is (parser->lexer, 
26253                                                   CPP_OPEN_PAREN))
26254                         error ("parenthesized initialization is not allowed in "
26255                                "OpenMP %<for%> loop");
26256                       else
26257                         /* Trigger an error.  */
26258                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26259
26260                       init = error_mark_node;
26261                       cp_parser_skip_to_end_of_statement (parser);
26262                     }
26263                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26264                            || type_dependent_expression_p (decl)
26265                            || auto_node)
26266                     {
26267                       bool is_direct_init, is_non_constant_init;
26268
26269                       init = cp_parser_initializer (parser,
26270                                                     &is_direct_init,
26271                                                     &is_non_constant_init);
26272
26273                       if (auto_node)
26274                         {
26275                           TREE_TYPE (decl)
26276                             = do_auto_deduction (TREE_TYPE (decl), init,
26277                                                  auto_node);
26278
26279                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26280                               && !type_dependent_expression_p (decl))
26281                             goto non_class;
26282                         }
26283                       
26284                       cp_finish_decl (decl, init, !is_non_constant_init,
26285                                       asm_specification,
26286                                       LOOKUP_ONLYCONVERTING);
26287                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26288                         {
26289                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26290                           init = NULL_TREE;
26291                         }
26292                       else
26293                         init = pop_stmt_list (this_pre_body);
26294                       this_pre_body = NULL_TREE;
26295                     }
26296                   else
26297                     {
26298                       /* Consume '='.  */
26299                       cp_lexer_consume_token (parser->lexer);
26300                       init = cp_parser_assignment_expression (parser, false, NULL);
26301
26302                     non_class:
26303                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26304                         init = error_mark_node;
26305                       else
26306                         cp_finish_decl (decl, NULL_TREE,
26307                                         /*init_const_expr_p=*/false,
26308                                         asm_specification,
26309                                         LOOKUP_ONLYCONVERTING);
26310                     }
26311
26312                   if (pushed_scope)
26313                     pop_scope (pushed_scope);
26314                 }
26315             }
26316           else 
26317             {
26318               cp_id_kind idk;
26319               /* If parsing a type specifier sequence failed, then
26320                  this MUST be a simple expression.  */
26321               cp_parser_parse_tentatively (parser);
26322               decl = cp_parser_primary_expression (parser, false, false,
26323                                                    false, &idk);
26324               if (!cp_parser_error_occurred (parser)
26325                   && decl
26326                   && DECL_P (decl)
26327                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26328                 {
26329                   tree rhs;
26330
26331                   cp_parser_parse_definitely (parser);
26332                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26333                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26334                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26335                                                          rhs,
26336                                                          tf_warning_or_error));
26337                   add_private_clause = true;
26338                 }
26339               else
26340                 {
26341                   decl = NULL;
26342                   cp_parser_abort_tentative_parse (parser);
26343                   init = cp_parser_expression (parser, false, NULL);
26344                   if (init)
26345                     {
26346                       if (TREE_CODE (init) == MODIFY_EXPR
26347                           || TREE_CODE (init) == MODOP_EXPR)
26348                         real_decl = TREE_OPERAND (init, 0);
26349                     }
26350                 }
26351             }
26352         }
26353       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26354       if (this_pre_body)
26355         {
26356           this_pre_body = pop_stmt_list (this_pre_body);
26357           if (pre_body)
26358             {
26359               tree t = pre_body;
26360               pre_body = push_stmt_list ();
26361               add_stmt (t);
26362               add_stmt (this_pre_body);
26363               pre_body = pop_stmt_list (pre_body);
26364             }
26365           else
26366             pre_body = this_pre_body;
26367         }
26368
26369       if (decl)
26370         real_decl = decl;
26371       if (par_clauses != NULL && real_decl != NULL_TREE)
26372         {
26373           tree *c;
26374           for (c = par_clauses; *c ; )
26375             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26376                 && OMP_CLAUSE_DECL (*c) == real_decl)
26377               {
26378                 error_at (loc, "iteration variable %qD"
26379                           " should not be firstprivate", real_decl);
26380                 *c = OMP_CLAUSE_CHAIN (*c);
26381               }
26382             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26383                      && OMP_CLAUSE_DECL (*c) == real_decl)
26384               {
26385                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26386                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26387                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26388                 OMP_CLAUSE_DECL (l) = real_decl;
26389                 OMP_CLAUSE_CHAIN (l) = clauses;
26390                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26391                 clauses = l;
26392                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26393                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26394                 add_private_clause = false;
26395               }
26396             else
26397               {
26398                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26399                     && OMP_CLAUSE_DECL (*c) == real_decl)
26400                   add_private_clause = false;
26401                 c = &OMP_CLAUSE_CHAIN (*c);
26402               }
26403         }
26404
26405       if (add_private_clause)
26406         {
26407           tree c;
26408           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26409             {
26410               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26411                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26412                   && OMP_CLAUSE_DECL (c) == decl)
26413                 break;
26414               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26415                        && OMP_CLAUSE_DECL (c) == decl)
26416                 error_at (loc, "iteration variable %qD "
26417                           "should not be firstprivate",
26418                           decl);
26419               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26420                        && OMP_CLAUSE_DECL (c) == decl)
26421                 error_at (loc, "iteration variable %qD should not be reduction",
26422                           decl);
26423             }
26424           if (c == NULL)
26425             {
26426               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26427               OMP_CLAUSE_DECL (c) = decl;
26428               c = finish_omp_clauses (c);
26429               if (c)
26430                 {
26431                   OMP_CLAUSE_CHAIN (c) = clauses;
26432                   clauses = c;
26433                 }
26434             }
26435         }
26436
26437       cond = NULL;
26438       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26439         cond = cp_parser_omp_for_cond (parser, decl);
26440       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26441
26442       incr = NULL;
26443       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26444         {
26445           /* If decl is an iterator, preserve the operator on decl
26446              until finish_omp_for.  */
26447           if (real_decl
26448               && ((processing_template_decl
26449                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26450                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26451             incr = cp_parser_omp_for_incr (parser, real_decl);
26452           else
26453             incr = cp_parser_expression (parser, false, NULL);
26454         }
26455
26456       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26457         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26458                                                /*or_comma=*/false,
26459                                                /*consume_paren=*/true);
26460
26461       TREE_VEC_ELT (declv, i) = decl;
26462       TREE_VEC_ELT (initv, i) = init;
26463       TREE_VEC_ELT (condv, i) = cond;
26464       TREE_VEC_ELT (incrv, i) = incr;
26465
26466       if (i == collapse - 1)
26467         break;
26468
26469       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26470          in between the collapsed for loops to be still considered perfectly
26471          nested.  Hopefully the final version clarifies this.
26472          For now handle (multiple) {'s and empty statements.  */
26473       cp_parser_parse_tentatively (parser);
26474       do
26475         {
26476           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26477             break;
26478           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26479             {
26480               cp_lexer_consume_token (parser->lexer);
26481               bracecount++;
26482             }
26483           else if (bracecount
26484                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26485             cp_lexer_consume_token (parser->lexer);
26486           else
26487             {
26488               loc = cp_lexer_peek_token (parser->lexer)->location;
26489               error_at (loc, "not enough collapsed for loops");
26490               collapse_err = true;
26491               cp_parser_abort_tentative_parse (parser);
26492               declv = NULL_TREE;
26493               break;
26494             }
26495         }
26496       while (1);
26497
26498       if (declv)
26499         {
26500           cp_parser_parse_definitely (parser);
26501           nbraces += bracecount;
26502         }
26503     }
26504
26505   /* Note that we saved the original contents of this flag when we entered
26506      the structured block, and so we don't need to re-save it here.  */
26507   parser->in_statement = IN_OMP_FOR;
26508
26509   /* Note that the grammar doesn't call for a structured block here,
26510      though the loop as a whole is a structured block.  */
26511   body = push_stmt_list ();
26512   cp_parser_statement (parser, NULL_TREE, false, NULL);
26513   body = pop_stmt_list (body);
26514
26515   if (declv == NULL_TREE)
26516     ret = NULL_TREE;
26517   else
26518     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26519                           pre_body, clauses);
26520
26521   while (nbraces)
26522     {
26523       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26524         {
26525           cp_lexer_consume_token (parser->lexer);
26526           nbraces--;
26527         }
26528       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26529         cp_lexer_consume_token (parser->lexer);
26530       else
26531         {
26532           if (!collapse_err)
26533             {
26534               error_at (cp_lexer_peek_token (parser->lexer)->location,
26535                         "collapsed loops not perfectly nested");
26536             }
26537           collapse_err = true;
26538           cp_parser_statement_seq_opt (parser, NULL);
26539           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26540             break;
26541         }
26542     }
26543
26544   while (!VEC_empty (tree, for_block))
26545     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26546   release_tree_vector (for_block);
26547
26548   return ret;
26549 }
26550
26551 /* OpenMP 2.5:
26552    #pragma omp for for-clause[optseq] new-line
26553      for-loop  */
26554
26555 #define OMP_FOR_CLAUSE_MASK                             \
26556         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26557         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26558         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26559         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26560         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26561         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26562         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26563         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26564
26565 static tree
26566 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26567 {
26568   tree clauses, sb, ret;
26569   unsigned int save;
26570
26571   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26572                                        "#pragma omp for", pragma_tok);
26573
26574   sb = begin_omp_structured_block ();
26575   save = cp_parser_begin_omp_structured_block (parser);
26576
26577   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26578
26579   cp_parser_end_omp_structured_block (parser, save);
26580   add_stmt (finish_omp_structured_block (sb));
26581
26582   return ret;
26583 }
26584
26585 /* OpenMP 2.5:
26586    # pragma omp master new-line
26587      structured-block  */
26588
26589 static tree
26590 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26591 {
26592   cp_parser_require_pragma_eol (parser, pragma_tok);
26593   return c_finish_omp_master (input_location,
26594                               cp_parser_omp_structured_block (parser));
26595 }
26596
26597 /* OpenMP 2.5:
26598    # pragma omp ordered new-line
26599      structured-block  */
26600
26601 static tree
26602 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26603 {
26604   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26605   cp_parser_require_pragma_eol (parser, pragma_tok);
26606   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26607 }
26608
26609 /* OpenMP 2.5:
26610
26611    section-scope:
26612      { section-sequence }
26613
26614    section-sequence:
26615      section-directive[opt] structured-block
26616      section-sequence section-directive structured-block  */
26617
26618 static tree
26619 cp_parser_omp_sections_scope (cp_parser *parser)
26620 {
26621   tree stmt, substmt;
26622   bool error_suppress = false;
26623   cp_token *tok;
26624
26625   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26626     return NULL_TREE;
26627
26628   stmt = push_stmt_list ();
26629
26630   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26631     {
26632       unsigned save;
26633
26634       substmt = begin_omp_structured_block ();
26635       save = cp_parser_begin_omp_structured_block (parser);
26636
26637       while (1)
26638         {
26639           cp_parser_statement (parser, NULL_TREE, false, NULL);
26640
26641           tok = cp_lexer_peek_token (parser->lexer);
26642           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26643             break;
26644           if (tok->type == CPP_CLOSE_BRACE)
26645             break;
26646           if (tok->type == CPP_EOF)
26647             break;
26648         }
26649
26650       cp_parser_end_omp_structured_block (parser, save);
26651       substmt = finish_omp_structured_block (substmt);
26652       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26653       add_stmt (substmt);
26654     }
26655
26656   while (1)
26657     {
26658       tok = cp_lexer_peek_token (parser->lexer);
26659       if (tok->type == CPP_CLOSE_BRACE)
26660         break;
26661       if (tok->type == CPP_EOF)
26662         break;
26663
26664       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26665         {
26666           cp_lexer_consume_token (parser->lexer);
26667           cp_parser_require_pragma_eol (parser, tok);
26668           error_suppress = false;
26669         }
26670       else if (!error_suppress)
26671         {
26672           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26673           error_suppress = true;
26674         }
26675
26676       substmt = cp_parser_omp_structured_block (parser);
26677       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26678       add_stmt (substmt);
26679     }
26680   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26681
26682   substmt = pop_stmt_list (stmt);
26683
26684   stmt = make_node (OMP_SECTIONS);
26685   TREE_TYPE (stmt) = void_type_node;
26686   OMP_SECTIONS_BODY (stmt) = substmt;
26687
26688   add_stmt (stmt);
26689   return stmt;
26690 }
26691
26692 /* OpenMP 2.5:
26693    # pragma omp sections sections-clause[optseq] newline
26694      sections-scope  */
26695
26696 #define OMP_SECTIONS_CLAUSE_MASK                        \
26697         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26698         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26699         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26700         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26701         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26702
26703 static tree
26704 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26705 {
26706   tree clauses, ret;
26707
26708   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26709                                        "#pragma omp sections", pragma_tok);
26710
26711   ret = cp_parser_omp_sections_scope (parser);
26712   if (ret)
26713     OMP_SECTIONS_CLAUSES (ret) = clauses;
26714
26715   return ret;
26716 }
26717
26718 /* OpenMP 2.5:
26719    # pragma parallel parallel-clause new-line
26720    # pragma parallel for parallel-for-clause new-line
26721    # pragma parallel sections parallel-sections-clause new-line  */
26722
26723 #define OMP_PARALLEL_CLAUSE_MASK                        \
26724         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26725         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26726         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26727         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26728         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26729         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26730         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26731         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26732
26733 static tree
26734 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26735 {
26736   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26737   const char *p_name = "#pragma omp parallel";
26738   tree stmt, clauses, par_clause, ws_clause, block;
26739   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26740   unsigned int save;
26741   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26742
26743   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26744     {
26745       cp_lexer_consume_token (parser->lexer);
26746       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26747       p_name = "#pragma omp parallel for";
26748       mask |= OMP_FOR_CLAUSE_MASK;
26749       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26750     }
26751   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26752     {
26753       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26754       const char *p = IDENTIFIER_POINTER (id);
26755       if (strcmp (p, "sections") == 0)
26756         {
26757           cp_lexer_consume_token (parser->lexer);
26758           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26759           p_name = "#pragma omp parallel sections";
26760           mask |= OMP_SECTIONS_CLAUSE_MASK;
26761           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26762         }
26763     }
26764
26765   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26766   block = begin_omp_parallel ();
26767   save = cp_parser_begin_omp_structured_block (parser);
26768
26769   switch (p_kind)
26770     {
26771     case PRAGMA_OMP_PARALLEL:
26772       cp_parser_statement (parser, NULL_TREE, false, NULL);
26773       par_clause = clauses;
26774       break;
26775
26776     case PRAGMA_OMP_PARALLEL_FOR:
26777       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26778       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26779       break;
26780
26781     case PRAGMA_OMP_PARALLEL_SECTIONS:
26782       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26783       stmt = cp_parser_omp_sections_scope (parser);
26784       if (stmt)
26785         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26786       break;
26787
26788     default:
26789       gcc_unreachable ();
26790     }
26791
26792   cp_parser_end_omp_structured_block (parser, save);
26793   stmt = finish_omp_parallel (par_clause, block);
26794   if (p_kind != PRAGMA_OMP_PARALLEL)
26795     OMP_PARALLEL_COMBINED (stmt) = 1;
26796   return stmt;
26797 }
26798
26799 /* OpenMP 2.5:
26800    # pragma omp single single-clause[optseq] new-line
26801      structured-block  */
26802
26803 #define OMP_SINGLE_CLAUSE_MASK                          \
26804         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26805         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26806         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26807         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26808
26809 static tree
26810 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26811 {
26812   tree stmt = make_node (OMP_SINGLE);
26813   TREE_TYPE (stmt) = void_type_node;
26814
26815   OMP_SINGLE_CLAUSES (stmt)
26816     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26817                                  "#pragma omp single", pragma_tok);
26818   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26819
26820   return add_stmt (stmt);
26821 }
26822
26823 /* OpenMP 3.0:
26824    # pragma omp task task-clause[optseq] new-line
26825      structured-block  */
26826
26827 #define OMP_TASK_CLAUSE_MASK                            \
26828         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26829         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26830         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26831         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26832         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26833         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26834         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26835         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26836
26837 static tree
26838 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26839 {
26840   tree clauses, block;
26841   unsigned int save;
26842
26843   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26844                                        "#pragma omp task", pragma_tok);
26845   block = begin_omp_task ();
26846   save = cp_parser_begin_omp_structured_block (parser);
26847   cp_parser_statement (parser, NULL_TREE, false, NULL);
26848   cp_parser_end_omp_structured_block (parser, save);
26849   return finish_omp_task (clauses, block);
26850 }
26851
26852 /* OpenMP 3.0:
26853    # pragma omp taskwait new-line  */
26854
26855 static void
26856 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26857 {
26858   cp_parser_require_pragma_eol (parser, pragma_tok);
26859   finish_omp_taskwait ();
26860 }
26861
26862 /* OpenMP 3.1:
26863    # pragma omp taskyield new-line  */
26864
26865 static void
26866 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26867 {
26868   cp_parser_require_pragma_eol (parser, pragma_tok);
26869   finish_omp_taskyield ();
26870 }
26871
26872 /* OpenMP 2.5:
26873    # pragma omp threadprivate (variable-list) */
26874
26875 static void
26876 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26877 {
26878   tree vars;
26879
26880   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26881   cp_parser_require_pragma_eol (parser, pragma_tok);
26882
26883   finish_omp_threadprivate (vars);
26884 }
26885
26886 /* Main entry point to OpenMP statement pragmas.  */
26887
26888 static void
26889 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26890 {
26891   tree stmt;
26892
26893   switch (pragma_tok->pragma_kind)
26894     {
26895     case PRAGMA_OMP_ATOMIC:
26896       cp_parser_omp_atomic (parser, pragma_tok);
26897       return;
26898     case PRAGMA_OMP_CRITICAL:
26899       stmt = cp_parser_omp_critical (parser, pragma_tok);
26900       break;
26901     case PRAGMA_OMP_FOR:
26902       stmt = cp_parser_omp_for (parser, pragma_tok);
26903       break;
26904     case PRAGMA_OMP_MASTER:
26905       stmt = cp_parser_omp_master (parser, pragma_tok);
26906       break;
26907     case PRAGMA_OMP_ORDERED:
26908       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26909       break;
26910     case PRAGMA_OMP_PARALLEL:
26911       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26912       break;
26913     case PRAGMA_OMP_SECTIONS:
26914       stmt = cp_parser_omp_sections (parser, pragma_tok);
26915       break;
26916     case PRAGMA_OMP_SINGLE:
26917       stmt = cp_parser_omp_single (parser, pragma_tok);
26918       break;
26919     case PRAGMA_OMP_TASK:
26920       stmt = cp_parser_omp_task (parser, pragma_tok);
26921       break;
26922     default:
26923       gcc_unreachable ();
26924     }
26925
26926   if (stmt)
26927     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26928 }
26929 \f
26930 /* Transactional Memory parsing routines.  */
26931
26932 /* Parse a transaction attribute.
26933
26934    txn-attribute:
26935         attribute
26936         [ [ identifier ] ]
26937
26938    ??? Simplify this when C++0x bracket attributes are
26939    implemented properly.  */
26940
26941 static tree
26942 cp_parser_txn_attribute_opt (cp_parser *parser)
26943 {
26944   cp_token *token;
26945   tree attr_name, attr = NULL;
26946
26947   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26948     return cp_parser_attributes_opt (parser);
26949
26950   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26951     return NULL_TREE;
26952   cp_lexer_consume_token (parser->lexer);
26953   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26954     goto error1;
26955
26956   token = cp_lexer_peek_token (parser->lexer);
26957   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26958     {
26959       token = cp_lexer_consume_token (parser->lexer);
26960
26961       attr_name = (token->type == CPP_KEYWORD
26962                    /* For keywords, use the canonical spelling,
26963                       not the parsed identifier.  */
26964                    ? ridpointers[(int) token->keyword]
26965                    : token->u.value);
26966       attr = build_tree_list (attr_name, NULL_TREE);
26967     }
26968   else
26969     cp_parser_error (parser, "expected identifier");
26970
26971   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26972  error1:
26973   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26974   return attr;
26975 }
26976
26977 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26978
26979    transaction-statement:
26980      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
26981        compound-statement
26982      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
26983 */
26984
26985 static tree
26986 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26987 {
26988   unsigned char old_in = parser->in_transaction;
26989   unsigned char this_in = 1, new_in;
26990   cp_token *token;
26991   tree stmt, attrs, noex;
26992
26993   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26994       || keyword == RID_TRANSACTION_RELAXED);
26995   token = cp_parser_require_keyword (parser, keyword,
26996       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26997           : RT_TRANSACTION_RELAXED));
26998   gcc_assert (token != NULL);
26999
27000   if (keyword == RID_TRANSACTION_RELAXED)
27001     this_in |= TM_STMT_ATTR_RELAXED;
27002   else
27003     {
27004       attrs = cp_parser_txn_attribute_opt (parser);
27005       if (attrs)
27006         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27007     }
27008
27009   /* Parse a noexcept specification.  */
27010   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27011
27012   /* Keep track if we're in the lexical scope of an outer transaction.  */
27013   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27014
27015   stmt = begin_transaction_stmt (token->location, NULL, this_in);
27016
27017   parser->in_transaction = new_in;
27018   cp_parser_compound_statement (parser, NULL, false, false);
27019   parser->in_transaction = old_in;
27020
27021   finish_transaction_stmt (stmt, NULL, this_in, noex);
27022
27023   return stmt;
27024 }
27025
27026 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27027
27028    transaction-expression:
27029      __transaction_atomic txn-noexcept-spec[opt] ( expression )
27030      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27031 */
27032
27033 static tree
27034 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27035 {
27036   unsigned char old_in = parser->in_transaction;
27037   unsigned char this_in = 1;
27038   cp_token *token;
27039   tree expr, noex;
27040   bool noex_expr;
27041
27042   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27043       || keyword == RID_TRANSACTION_RELAXED);
27044
27045   if (!flag_tm)
27046     error (keyword == RID_TRANSACTION_RELAXED
27047            ? G_("%<__transaction_relaxed%> without transactional memory "
27048                 "support enabled")
27049            : G_("%<__transaction_atomic%> without transactional memory "
27050                 "support enabled"));
27051
27052   token = cp_parser_require_keyword (parser, keyword,
27053       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27054           : RT_TRANSACTION_RELAXED));
27055   gcc_assert (token != NULL);
27056
27057   if (keyword == RID_TRANSACTION_RELAXED)
27058     this_in |= TM_STMT_ATTR_RELAXED;
27059
27060   /* Set this early.  This might mean that we allow transaction_cancel in
27061      an expression that we find out later actually has to be a constexpr.
27062      However, we expect that cxx_constant_value will be able to deal with
27063      this; also, if the noexcept has no constexpr, then what we parse next
27064      really is a transaction's body.  */
27065   parser->in_transaction = this_in;
27066
27067   /* Parse a noexcept specification.  */
27068   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27069                                                true);
27070
27071   if (!noex || !noex_expr
27072       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27073     {
27074       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27075
27076       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27077       finish_parenthesized_expr (expr);
27078
27079       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27080     }
27081   else
27082     {
27083       /* The only expression that is available got parsed for the noexcept
27084          already.  noexcept is true then.  */
27085       expr = noex;
27086       noex = boolean_true_node;
27087     }
27088
27089   expr = build_transaction_expr (token->location, expr, this_in, noex);
27090   parser->in_transaction = old_in;
27091
27092   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27093     return error_mark_node;
27094
27095   return (flag_tm ? expr : error_mark_node);
27096 }
27097
27098 /* Parse a function-transaction-block.
27099
27100    function-transaction-block:
27101      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27102          function-body
27103      __transaction_atomic txn-attribute[opt] function-try-block
27104      __transaction_relaxed ctor-initializer[opt] function-body
27105      __transaction_relaxed function-try-block
27106 */
27107
27108 static bool
27109 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27110 {
27111   unsigned char old_in = parser->in_transaction;
27112   unsigned char new_in = 1;
27113   tree compound_stmt, stmt, attrs;
27114   bool ctor_initializer_p;
27115   cp_token *token;
27116
27117   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27118       || keyword == RID_TRANSACTION_RELAXED);
27119   token = cp_parser_require_keyword (parser, keyword,
27120       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27121           : RT_TRANSACTION_RELAXED));
27122   gcc_assert (token != NULL);
27123
27124   if (keyword == RID_TRANSACTION_RELAXED)
27125     new_in |= TM_STMT_ATTR_RELAXED;
27126   else
27127     {
27128       attrs = cp_parser_txn_attribute_opt (parser);
27129       if (attrs)
27130         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27131     }
27132
27133   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27134
27135   parser->in_transaction = new_in;
27136
27137   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27138     ctor_initializer_p = cp_parser_function_try_block (parser);
27139   else
27140     ctor_initializer_p
27141       = cp_parser_ctor_initializer_opt_and_function_body (parser);
27142
27143   parser->in_transaction = old_in;
27144
27145   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27146
27147   return ctor_initializer_p;
27148 }
27149
27150 /* Parse a __transaction_cancel statement.
27151
27152    cancel-statement:
27153      __transaction_cancel txn-attribute[opt] ;
27154      __transaction_cancel txn-attribute[opt] throw-expression ;
27155
27156    ??? Cancel and throw is not yet implemented.  */
27157
27158 static tree
27159 cp_parser_transaction_cancel (cp_parser *parser)
27160 {
27161   cp_token *token;
27162   bool is_outer = false;
27163   tree stmt, attrs;
27164
27165   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27166                                      RT_TRANSACTION_CANCEL);
27167   gcc_assert (token != NULL);
27168
27169   attrs = cp_parser_txn_attribute_opt (parser);
27170   if (attrs)
27171     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27172
27173   /* ??? Parse cancel-and-throw here.  */
27174
27175   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27176
27177   if (!flag_tm)
27178     {
27179       error_at (token->location, "%<__transaction_cancel%> without "
27180                 "transactional memory support enabled");
27181       return error_mark_node;
27182     }
27183   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27184     {
27185       error_at (token->location, "%<__transaction_cancel%> within a "
27186                 "%<__transaction_relaxed%>");
27187       return error_mark_node;
27188     }
27189   else if (is_outer)
27190     {
27191       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27192           && !is_tm_may_cancel_outer (current_function_decl))
27193         {
27194           error_at (token->location, "outer %<__transaction_cancel%> not "
27195                     "within outer %<__transaction_atomic%>");
27196           error_at (token->location,
27197                     "  or a %<transaction_may_cancel_outer%> function");
27198           return error_mark_node;
27199         }
27200     }
27201   else if (parser->in_transaction == 0)
27202     {
27203       error_at (token->location, "%<__transaction_cancel%> not within "
27204                 "%<__transaction_atomic%>");
27205       return error_mark_node;
27206     }
27207
27208   stmt = build_tm_abort_call (token->location, is_outer);
27209   add_stmt (stmt);
27210   finish_stmt ();
27211
27212   return stmt;
27213 }
27214 \f
27215 /* The parser.  */
27216
27217 static GTY (()) cp_parser *the_parser;
27218
27219 \f
27220 /* Special handling for the first token or line in the file.  The first
27221    thing in the file might be #pragma GCC pch_preprocess, which loads a
27222    PCH file, which is a GC collection point.  So we need to handle this
27223    first pragma without benefit of an existing lexer structure.
27224
27225    Always returns one token to the caller in *FIRST_TOKEN.  This is
27226    either the true first token of the file, or the first token after
27227    the initial pragma.  */
27228
27229 static void
27230 cp_parser_initial_pragma (cp_token *first_token)
27231 {
27232   tree name = NULL;
27233
27234   cp_lexer_get_preprocessor_token (NULL, first_token);
27235   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27236     return;
27237
27238   cp_lexer_get_preprocessor_token (NULL, first_token);
27239   if (first_token->type == CPP_STRING)
27240     {
27241       name = first_token->u.value;
27242
27243       cp_lexer_get_preprocessor_token (NULL, first_token);
27244       if (first_token->type != CPP_PRAGMA_EOL)
27245         error_at (first_token->location,
27246                   "junk at end of %<#pragma GCC pch_preprocess%>");
27247     }
27248   else
27249     error_at (first_token->location, "expected string literal");
27250
27251   /* Skip to the end of the pragma.  */
27252   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27253     cp_lexer_get_preprocessor_token (NULL, first_token);
27254
27255   /* Now actually load the PCH file.  */
27256   if (name)
27257     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27258
27259   /* Read one more token to return to our caller.  We have to do this
27260      after reading the PCH file in, since its pointers have to be
27261      live.  */
27262   cp_lexer_get_preprocessor_token (NULL, first_token);
27263 }
27264
27265 /* Normal parsing of a pragma token.  Here we can (and must) use the
27266    regular lexer.  */
27267
27268 static bool
27269 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27270 {
27271   cp_token *pragma_tok;
27272   unsigned int id;
27273
27274   pragma_tok = cp_lexer_consume_token (parser->lexer);
27275   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27276   parser->lexer->in_pragma = true;
27277
27278   id = pragma_tok->pragma_kind;
27279   switch (id)
27280     {
27281     case PRAGMA_GCC_PCH_PREPROCESS:
27282       error_at (pragma_tok->location,
27283                 "%<#pragma GCC pch_preprocess%> must be first");
27284       break;
27285
27286     case PRAGMA_OMP_BARRIER:
27287       switch (context)
27288         {
27289         case pragma_compound:
27290           cp_parser_omp_barrier (parser, pragma_tok);
27291           return false;
27292         case pragma_stmt:
27293           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27294                     "used in compound statements");
27295           break;
27296         default:
27297           goto bad_stmt;
27298         }
27299       break;
27300
27301     case PRAGMA_OMP_FLUSH:
27302       switch (context)
27303         {
27304         case pragma_compound:
27305           cp_parser_omp_flush (parser, pragma_tok);
27306           return false;
27307         case pragma_stmt:
27308           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27309                     "used in compound statements");
27310           break;
27311         default:
27312           goto bad_stmt;
27313         }
27314       break;
27315
27316     case PRAGMA_OMP_TASKWAIT:
27317       switch (context)
27318         {
27319         case pragma_compound:
27320           cp_parser_omp_taskwait (parser, pragma_tok);
27321           return false;
27322         case pragma_stmt:
27323           error_at (pragma_tok->location,
27324                     "%<#pragma omp taskwait%> may only be "
27325                     "used in compound statements");
27326           break;
27327         default:
27328           goto bad_stmt;
27329         }
27330       break;
27331
27332     case PRAGMA_OMP_TASKYIELD:
27333       switch (context)
27334         {
27335         case pragma_compound:
27336           cp_parser_omp_taskyield (parser, pragma_tok);
27337           return false;
27338         case pragma_stmt:
27339           error_at (pragma_tok->location,
27340                     "%<#pragma omp taskyield%> may only be "
27341                     "used in compound statements");
27342           break;
27343         default:
27344           goto bad_stmt;
27345         }
27346       break;
27347
27348     case PRAGMA_OMP_THREADPRIVATE:
27349       cp_parser_omp_threadprivate (parser, pragma_tok);
27350       return false;
27351
27352     case PRAGMA_OMP_ATOMIC:
27353     case PRAGMA_OMP_CRITICAL:
27354     case PRAGMA_OMP_FOR:
27355     case PRAGMA_OMP_MASTER:
27356     case PRAGMA_OMP_ORDERED:
27357     case PRAGMA_OMP_PARALLEL:
27358     case PRAGMA_OMP_SECTIONS:
27359     case PRAGMA_OMP_SINGLE:
27360     case PRAGMA_OMP_TASK:
27361       if (context == pragma_external)
27362         goto bad_stmt;
27363       cp_parser_omp_construct (parser, pragma_tok);
27364       return true;
27365
27366     case PRAGMA_OMP_SECTION:
27367       error_at (pragma_tok->location, 
27368                 "%<#pragma omp section%> may only be used in "
27369                 "%<#pragma omp sections%> construct");
27370       break;
27371
27372     default:
27373       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27374       c_invoke_pragma_handler (id);
27375       break;
27376
27377     bad_stmt:
27378       cp_parser_error (parser, "expected declaration specifiers");
27379       break;
27380     }
27381
27382   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27383   return false;
27384 }
27385
27386 /* The interface the pragma parsers have to the lexer.  */
27387
27388 enum cpp_ttype
27389 pragma_lex (tree *value)
27390 {
27391   cp_token *tok;
27392   enum cpp_ttype ret;
27393
27394   tok = cp_lexer_peek_token (the_parser->lexer);
27395
27396   ret = tok->type;
27397   *value = tok->u.value;
27398
27399   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27400     ret = CPP_EOF;
27401   else if (ret == CPP_STRING)
27402     *value = cp_parser_string_literal (the_parser, false, false);
27403   else
27404     {
27405       cp_lexer_consume_token (the_parser->lexer);
27406       if (ret == CPP_KEYWORD)
27407         ret = CPP_NAME;
27408     }
27409
27410   return ret;
27411 }
27412
27413 \f
27414 /* External interface.  */
27415
27416 /* Parse one entire translation unit.  */
27417
27418 void
27419 c_parse_file (void)
27420 {
27421   static bool already_called = false;
27422
27423   if (already_called)
27424     {
27425       sorry ("inter-module optimizations not implemented for C++");
27426       return;
27427     }
27428   already_called = true;
27429
27430   the_parser = cp_parser_new ();
27431   push_deferring_access_checks (flag_access_control
27432                                 ? dk_no_deferred : dk_no_check);
27433   cp_parser_translation_unit (the_parser);
27434   the_parser = NULL;
27435 }
27436
27437 #include "gt-cp-parser.h"