OSDN Git Service

20e42af46c4d797ffce89955a0efd73a4e1755b3
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011, 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_pedantic, 
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
4172             /* The `__builtin_va_arg' construct is used to handle
4173                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4174             cp_lexer_consume_token (parser->lexer);
4175             /* Look for the opening `('.  */
4176             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4177             /* Now, parse the assignment-expression.  */
4178             expression = cp_parser_assignment_expression (parser,
4179                                                           /*cast_p=*/false, NULL);
4180             /* Look for the `,'.  */
4181             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4182             /* Parse the type-id.  */
4183             type = cp_parser_type_id (parser);
4184             /* Look for the closing `)'.  */
4185             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4186             /* Using `va_arg' in a constant-expression is not
4187                allowed.  */
4188             if (cp_parser_non_integral_constant_expression (parser,
4189                                                             NIC_VA_ARG))
4190               return error_mark_node;
4191             return build_x_va_arg (expression, type);
4192           }
4193
4194         case RID_OFFSETOF:
4195           return cp_parser_builtin_offsetof (parser);
4196
4197         case RID_HAS_NOTHROW_ASSIGN:
4198         case RID_HAS_NOTHROW_CONSTRUCTOR:
4199         case RID_HAS_NOTHROW_COPY:        
4200         case RID_HAS_TRIVIAL_ASSIGN:
4201         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4202         case RID_HAS_TRIVIAL_COPY:        
4203         case RID_HAS_TRIVIAL_DESTRUCTOR:
4204         case RID_HAS_VIRTUAL_DESTRUCTOR:
4205         case RID_IS_ABSTRACT:
4206         case RID_IS_BASE_OF:
4207         case RID_IS_CLASS:
4208         case RID_IS_CONVERTIBLE_TO:
4209         case RID_IS_EMPTY:
4210         case RID_IS_ENUM:
4211         case RID_IS_FINAL:
4212         case RID_IS_LITERAL_TYPE:
4213         case RID_IS_POD:
4214         case RID_IS_POLYMORPHIC:
4215         case RID_IS_STD_LAYOUT:
4216         case RID_IS_TRIVIAL:
4217         case RID_IS_UNION:
4218           return cp_parser_trait_expr (parser, token->keyword);
4219
4220         /* Objective-C++ expressions.  */
4221         case RID_AT_ENCODE:
4222         case RID_AT_PROTOCOL:
4223         case RID_AT_SELECTOR:
4224           return cp_parser_objc_expression (parser);
4225
4226         case RID_TEMPLATE:
4227           if (parser->in_function_body
4228               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4229                   == CPP_LESS))
4230             {
4231               error_at (token->location,
4232                         "a template declaration cannot appear at block scope");
4233               cp_parser_skip_to_end_of_block_or_statement (parser);
4234               return error_mark_node;
4235             }
4236         default:
4237           cp_parser_error (parser, "expected primary-expression");
4238           return error_mark_node;
4239         }
4240
4241       /* An id-expression can start with either an identifier, a
4242          `::' as the beginning of a qualified-id, or the "operator"
4243          keyword.  */
4244     case CPP_NAME:
4245     case CPP_SCOPE:
4246     case CPP_TEMPLATE_ID:
4247     case CPP_NESTED_NAME_SPECIFIER:
4248       {
4249         tree id_expression;
4250         tree decl;
4251         const char *error_msg;
4252         bool template_p;
4253         bool done;
4254         cp_token *id_expr_token;
4255
4256       id_expression:
4257         /* Parse the id-expression.  */
4258         id_expression
4259           = cp_parser_id_expression (parser,
4260                                      /*template_keyword_p=*/false,
4261                                      /*check_dependency_p=*/true,
4262                                      &template_p,
4263                                      /*declarator_p=*/false,
4264                                      /*optional_p=*/false);
4265         if (id_expression == error_mark_node)
4266           return error_mark_node;
4267         id_expr_token = token;
4268         token = cp_lexer_peek_token (parser->lexer);
4269         done = (token->type != CPP_OPEN_SQUARE
4270                 && token->type != CPP_OPEN_PAREN
4271                 && token->type != CPP_DOT
4272                 && token->type != CPP_DEREF
4273                 && token->type != CPP_PLUS_PLUS
4274                 && token->type != CPP_MINUS_MINUS);
4275         /* If we have a template-id, then no further lookup is
4276            required.  If the template-id was for a template-class, we
4277            will sometimes have a TYPE_DECL at this point.  */
4278         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4279                  || TREE_CODE (id_expression) == TYPE_DECL)
4280           decl = id_expression;
4281         /* Look up the name.  */
4282         else
4283           {
4284             tree ambiguous_decls;
4285
4286             /* If we already know that this lookup is ambiguous, then
4287                we've already issued an error message; there's no reason
4288                to check again.  */
4289             if (id_expr_token->type == CPP_NAME
4290                 && id_expr_token->ambiguous_p)
4291               {
4292                 cp_parser_simulate_error (parser);
4293                 return error_mark_node;
4294               }
4295
4296             decl = cp_parser_lookup_name (parser, id_expression,
4297                                           none_type,
4298                                           template_p,
4299                                           /*is_namespace=*/false,
4300                                           /*check_dependency=*/true,
4301                                           &ambiguous_decls,
4302                                           id_expr_token->location);
4303             /* If the lookup was ambiguous, an error will already have
4304                been issued.  */
4305             if (ambiguous_decls)
4306               return error_mark_node;
4307
4308             /* In Objective-C++, we may have an Objective-C 2.0
4309                dot-syntax for classes here.  */
4310             if (c_dialect_objc ()
4311                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4312                 && TREE_CODE (decl) == TYPE_DECL
4313                 && objc_is_class_name (decl))
4314               {
4315                 tree component;
4316                 cp_lexer_consume_token (parser->lexer);
4317                 component = cp_parser_identifier (parser);
4318                 if (component == error_mark_node)
4319                   return error_mark_node;
4320
4321                 return objc_build_class_component_ref (id_expression, component);
4322               }
4323
4324             /* In Objective-C++, an instance variable (ivar) may be preferred
4325                to whatever cp_parser_lookup_name() found.  */
4326             decl = objc_lookup_ivar (decl, id_expression);
4327
4328             /* If name lookup gives us a SCOPE_REF, then the
4329                qualifying scope was dependent.  */
4330             if (TREE_CODE (decl) == SCOPE_REF)
4331               {
4332                 /* At this point, we do not know if DECL is a valid
4333                    integral constant expression.  We assume that it is
4334                    in fact such an expression, so that code like:
4335
4336                       template <int N> struct A {
4337                         int a[B<N>::i];
4338                       };
4339                      
4340                    is accepted.  At template-instantiation time, we
4341                    will check that B<N>::i is actually a constant.  */
4342                 return decl;
4343               }
4344             /* Check to see if DECL is a local variable in a context
4345                where that is forbidden.  */
4346             if (parser->local_variables_forbidden_p
4347                 && local_variable_p (decl))
4348               {
4349                 /* It might be that we only found DECL because we are
4350                    trying to be generous with pre-ISO scoping rules.
4351                    For example, consider:
4352
4353                      int i;
4354                      void g() {
4355                        for (int i = 0; i < 10; ++i) {}
4356                        extern void f(int j = i);
4357                      }
4358
4359                    Here, name look up will originally find the out
4360                    of scope `i'.  We need to issue a warning message,
4361                    but then use the global `i'.  */
4362                 decl = check_for_out_of_scope_variable (decl);
4363                 if (local_variable_p (decl))
4364                   {
4365                     error_at (id_expr_token->location,
4366                               "local variable %qD may not appear in this context",
4367                               decl);
4368                     return error_mark_node;
4369                   }
4370               }
4371           }
4372
4373         decl = (finish_id_expression
4374                 (id_expression, decl, parser->scope,
4375                  idk,
4376                  parser->integral_constant_expression_p,
4377                  parser->allow_non_integral_constant_expression_p,
4378                  &parser->non_integral_constant_expression_p,
4379                  template_p, done, address_p,
4380                  template_arg_p,
4381                  &error_msg,
4382                  id_expr_token->location));
4383         if (error_msg)
4384           cp_parser_error (parser, error_msg);
4385         return decl;
4386       }
4387
4388       /* Anything else is an error.  */
4389     default:
4390       cp_parser_error (parser, "expected primary-expression");
4391       return error_mark_node;
4392     }
4393 }
4394
4395 /* Parse an id-expression.
4396
4397    id-expression:
4398      unqualified-id
4399      qualified-id
4400
4401    qualified-id:
4402      :: [opt] nested-name-specifier template [opt] unqualified-id
4403      :: identifier
4404      :: operator-function-id
4405      :: template-id
4406
4407    Return a representation of the unqualified portion of the
4408    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4409    a `::' or nested-name-specifier.
4410
4411    Often, if the id-expression was a qualified-id, the caller will
4412    want to make a SCOPE_REF to represent the qualified-id.  This
4413    function does not do this in order to avoid wastefully creating
4414    SCOPE_REFs when they are not required.
4415
4416    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4417    `template' keyword.
4418
4419    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4420    uninstantiated templates.
4421
4422    If *TEMPLATE_P is non-NULL, it is set to true iff the
4423    `template' keyword is used to explicitly indicate that the entity
4424    named is a template.
4425
4426    If DECLARATOR_P is true, the id-expression is appearing as part of
4427    a declarator, rather than as part of an expression.  */
4428
4429 static tree
4430 cp_parser_id_expression (cp_parser *parser,
4431                          bool template_keyword_p,
4432                          bool check_dependency_p,
4433                          bool *template_p,
4434                          bool declarator_p,
4435                          bool optional_p)
4436 {
4437   bool global_scope_p;
4438   bool nested_name_specifier_p;
4439
4440   /* Assume the `template' keyword was not used.  */
4441   if (template_p)
4442     *template_p = template_keyword_p;
4443
4444   /* Look for the optional `::' operator.  */
4445   global_scope_p
4446     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4447        != NULL_TREE);
4448   /* Look for the optional nested-name-specifier.  */
4449   nested_name_specifier_p
4450     = (cp_parser_nested_name_specifier_opt (parser,
4451                                             /*typename_keyword_p=*/false,
4452                                             check_dependency_p,
4453                                             /*type_p=*/false,
4454                                             declarator_p)
4455        != NULL_TREE);
4456   /* If there is a nested-name-specifier, then we are looking at
4457      the first qualified-id production.  */
4458   if (nested_name_specifier_p)
4459     {
4460       tree saved_scope;
4461       tree saved_object_scope;
4462       tree saved_qualifying_scope;
4463       tree unqualified_id;
4464       bool is_template;
4465
4466       /* See if the next token is the `template' keyword.  */
4467       if (!template_p)
4468         template_p = &is_template;
4469       *template_p = cp_parser_optional_template_keyword (parser);
4470       /* Name lookup we do during the processing of the
4471          unqualified-id might obliterate SCOPE.  */
4472       saved_scope = parser->scope;
4473       saved_object_scope = parser->object_scope;
4474       saved_qualifying_scope = parser->qualifying_scope;
4475       /* Process the final unqualified-id.  */
4476       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4477                                                  check_dependency_p,
4478                                                  declarator_p,
4479                                                  /*optional_p=*/false);
4480       /* Restore the SAVED_SCOPE for our caller.  */
4481       parser->scope = saved_scope;
4482       parser->object_scope = saved_object_scope;
4483       parser->qualifying_scope = saved_qualifying_scope;
4484
4485       return unqualified_id;
4486     }
4487   /* Otherwise, if we are in global scope, then we are looking at one
4488      of the other qualified-id productions.  */
4489   else if (global_scope_p)
4490     {
4491       cp_token *token;
4492       tree id;
4493
4494       /* Peek at the next token.  */
4495       token = cp_lexer_peek_token (parser->lexer);
4496
4497       /* If it's an identifier, and the next token is not a "<", then
4498          we can avoid the template-id case.  This is an optimization
4499          for this common case.  */
4500       if (token->type == CPP_NAME
4501           && !cp_parser_nth_token_starts_template_argument_list_p
4502                (parser, 2))
4503         return cp_parser_identifier (parser);
4504
4505       cp_parser_parse_tentatively (parser);
4506       /* Try a template-id.  */
4507       id = cp_parser_template_id (parser,
4508                                   /*template_keyword_p=*/false,
4509                                   /*check_dependency_p=*/true,
4510                                   declarator_p);
4511       /* If that worked, we're done.  */
4512       if (cp_parser_parse_definitely (parser))
4513         return id;
4514
4515       /* Peek at the next token.  (Changes in the token buffer may
4516          have invalidated the pointer obtained above.)  */
4517       token = cp_lexer_peek_token (parser->lexer);
4518
4519       switch (token->type)
4520         {
4521         case CPP_NAME:
4522           return cp_parser_identifier (parser);
4523
4524         case CPP_KEYWORD:
4525           if (token->keyword == RID_OPERATOR)
4526             return cp_parser_operator_function_id (parser);
4527           /* Fall through.  */
4528
4529         default:
4530           cp_parser_error (parser, "expected id-expression");
4531           return error_mark_node;
4532         }
4533     }
4534   else
4535     return cp_parser_unqualified_id (parser, template_keyword_p,
4536                                      /*check_dependency_p=*/true,
4537                                      declarator_p,
4538                                      optional_p);
4539 }
4540
4541 /* Parse an unqualified-id.
4542
4543    unqualified-id:
4544      identifier
4545      operator-function-id
4546      conversion-function-id
4547      ~ class-name
4548      template-id
4549
4550    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4551    keyword, in a construct like `A::template ...'.
4552
4553    Returns a representation of unqualified-id.  For the `identifier'
4554    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4555    production a BIT_NOT_EXPR is returned; the operand of the
4556    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4557    other productions, see the documentation accompanying the
4558    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4559    names are looked up in uninstantiated templates.  If DECLARATOR_P
4560    is true, the unqualified-id is appearing as part of a declarator,
4561    rather than as part of an expression.  */
4562
4563 static tree
4564 cp_parser_unqualified_id (cp_parser* parser,
4565                           bool template_keyword_p,
4566                           bool check_dependency_p,
4567                           bool declarator_p,
4568                           bool optional_p)
4569 {
4570   cp_token *token;
4571
4572   /* Peek at the next token.  */
4573   token = cp_lexer_peek_token (parser->lexer);
4574
4575   switch (token->type)
4576     {
4577     case CPP_NAME:
4578       {
4579         tree id;
4580
4581         /* We don't know yet whether or not this will be a
4582            template-id.  */
4583         cp_parser_parse_tentatively (parser);
4584         /* Try a template-id.  */
4585         id = cp_parser_template_id (parser, template_keyword_p,
4586                                     check_dependency_p,
4587                                     declarator_p);
4588         /* If it worked, we're done.  */
4589         if (cp_parser_parse_definitely (parser))
4590           return id;
4591         /* Otherwise, it's an ordinary identifier.  */
4592         return cp_parser_identifier (parser);
4593       }
4594
4595     case CPP_TEMPLATE_ID:
4596       return cp_parser_template_id (parser, template_keyword_p,
4597                                     check_dependency_p,
4598                                     declarator_p);
4599
4600     case CPP_COMPL:
4601       {
4602         tree type_decl;
4603         tree qualifying_scope;
4604         tree object_scope;
4605         tree scope;
4606         bool done;
4607
4608         /* Consume the `~' token.  */
4609         cp_lexer_consume_token (parser->lexer);
4610         /* Parse the class-name.  The standard, as written, seems to
4611            say that:
4612
4613              template <typename T> struct S { ~S (); };
4614              template <typename T> S<T>::~S() {}
4615
4616            is invalid, since `~' must be followed by a class-name, but
4617            `S<T>' is dependent, and so not known to be a class.
4618            That's not right; we need to look in uninstantiated
4619            templates.  A further complication arises from:
4620
4621              template <typename T> void f(T t) {
4622                t.T::~T();
4623              }
4624
4625            Here, it is not possible to look up `T' in the scope of `T'
4626            itself.  We must look in both the current scope, and the
4627            scope of the containing complete expression.
4628
4629            Yet another issue is:
4630
4631              struct S {
4632                int S;
4633                ~S();
4634              };
4635
4636              S::~S() {}
4637
4638            The standard does not seem to say that the `S' in `~S'
4639            should refer to the type `S' and not the data member
4640            `S::S'.  */
4641
4642         /* DR 244 says that we look up the name after the "~" in the
4643            same scope as we looked up the qualifying name.  That idea
4644            isn't fully worked out; it's more complicated than that.  */
4645         scope = parser->scope;
4646         object_scope = parser->object_scope;
4647         qualifying_scope = parser->qualifying_scope;
4648
4649         /* Check for invalid scopes.  */
4650         if (scope == error_mark_node)
4651           {
4652             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4653               cp_lexer_consume_token (parser->lexer);
4654             return error_mark_node;
4655           }
4656         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4657           {
4658             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4659               error_at (token->location,
4660                         "scope %qT before %<~%> is not a class-name",
4661                         scope);
4662             cp_parser_simulate_error (parser);
4663             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4664               cp_lexer_consume_token (parser->lexer);
4665             return error_mark_node;
4666           }
4667         gcc_assert (!scope || TYPE_P (scope));
4668
4669         /* If the name is of the form "X::~X" it's OK even if X is a
4670            typedef.  */
4671         token = cp_lexer_peek_token (parser->lexer);
4672         if (scope
4673             && token->type == CPP_NAME
4674             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4675                 != CPP_LESS)
4676             && (token->u.value == TYPE_IDENTIFIER (scope)
4677                 || (CLASS_TYPE_P (scope)
4678                     && constructor_name_p (token->u.value, scope))))
4679           {
4680             cp_lexer_consume_token (parser->lexer);
4681             return build_nt (BIT_NOT_EXPR, scope);
4682           }
4683
4684         /* If there was an explicit qualification (S::~T), first look
4685            in the scope given by the qualification (i.e., S).
4686
4687            Note: in the calls to cp_parser_class_name below we pass
4688            typename_type so that lookup finds the injected-class-name
4689            rather than the constructor.  */
4690         done = false;
4691         type_decl = NULL_TREE;
4692         if (scope)
4693           {
4694             cp_parser_parse_tentatively (parser);
4695             type_decl = cp_parser_class_name (parser,
4696                                               /*typename_keyword_p=*/false,
4697                                               /*template_keyword_p=*/false,
4698                                               typename_type,
4699                                               /*check_dependency=*/false,
4700                                               /*class_head_p=*/false,
4701                                               declarator_p);
4702             if (cp_parser_parse_definitely (parser))
4703               done = true;
4704           }
4705         /* In "N::S::~S", look in "N" as well.  */
4706         if (!done && scope && qualifying_scope)
4707           {
4708             cp_parser_parse_tentatively (parser);
4709             parser->scope = qualifying_scope;
4710             parser->object_scope = NULL_TREE;
4711             parser->qualifying_scope = NULL_TREE;
4712             type_decl
4713               = cp_parser_class_name (parser,
4714                                       /*typename_keyword_p=*/false,
4715                                       /*template_keyword_p=*/false,
4716                                       typename_type,
4717                                       /*check_dependency=*/false,
4718                                       /*class_head_p=*/false,
4719                                       declarator_p);
4720             if (cp_parser_parse_definitely (parser))
4721               done = true;
4722           }
4723         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4724         else if (!done && object_scope)
4725           {
4726             cp_parser_parse_tentatively (parser);
4727             parser->scope = object_scope;
4728             parser->object_scope = NULL_TREE;
4729             parser->qualifying_scope = NULL_TREE;
4730             type_decl
4731               = cp_parser_class_name (parser,
4732                                       /*typename_keyword_p=*/false,
4733                                       /*template_keyword_p=*/false,
4734                                       typename_type,
4735                                       /*check_dependency=*/false,
4736                                       /*class_head_p=*/false,
4737                                       declarator_p);
4738             if (cp_parser_parse_definitely (parser))
4739               done = true;
4740           }
4741         /* Look in the surrounding context.  */
4742         if (!done)
4743           {
4744             parser->scope = NULL_TREE;
4745             parser->object_scope = NULL_TREE;
4746             parser->qualifying_scope = NULL_TREE;
4747             if (processing_template_decl)
4748               cp_parser_parse_tentatively (parser);
4749             type_decl
4750               = cp_parser_class_name (parser,
4751                                       /*typename_keyword_p=*/false,
4752                                       /*template_keyword_p=*/false,
4753                                       typename_type,
4754                                       /*check_dependency=*/false,
4755                                       /*class_head_p=*/false,
4756                                       declarator_p);
4757             if (processing_template_decl
4758                 && ! cp_parser_parse_definitely (parser))
4759               {
4760                 /* We couldn't find a type with this name, so just accept
4761                    it and check for a match at instantiation time.  */
4762                 type_decl = cp_parser_identifier (parser);
4763                 if (type_decl != error_mark_node)
4764                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4765                 return type_decl;
4766               }
4767           }
4768         /* If an error occurred, assume that the name of the
4769            destructor is the same as the name of the qualifying
4770            class.  That allows us to keep parsing after running
4771            into ill-formed destructor names.  */
4772         if (type_decl == error_mark_node && scope)
4773           return build_nt (BIT_NOT_EXPR, scope);
4774         else if (type_decl == error_mark_node)
4775           return error_mark_node;
4776
4777         /* Check that destructor name and scope match.  */
4778         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4779           {
4780             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4781               error_at (token->location,
4782                         "declaration of %<~%T%> as member of %qT",
4783                         type_decl, scope);
4784             cp_parser_simulate_error (parser);
4785             return error_mark_node;
4786           }
4787
4788         /* [class.dtor]
4789
4790            A typedef-name that names a class shall not be used as the
4791            identifier in the declarator for a destructor declaration.  */
4792         if (declarator_p
4793             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4794             && !DECL_SELF_REFERENCE_P (type_decl)
4795             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4796           error_at (token->location,
4797                     "typedef-name %qD used as destructor declarator",
4798                     type_decl);
4799
4800         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4801       }
4802
4803     case CPP_KEYWORD:
4804       if (token->keyword == RID_OPERATOR)
4805         {
4806           tree id;
4807
4808           /* This could be a template-id, so we try that first.  */
4809           cp_parser_parse_tentatively (parser);
4810           /* Try a template-id.  */
4811           id = cp_parser_template_id (parser, template_keyword_p,
4812                                       /*check_dependency_p=*/true,
4813                                       declarator_p);
4814           /* If that worked, we're done.  */
4815           if (cp_parser_parse_definitely (parser))
4816             return id;
4817           /* We still don't know whether we're looking at an
4818              operator-function-id or a conversion-function-id.  */
4819           cp_parser_parse_tentatively (parser);
4820           /* Try an operator-function-id.  */
4821           id = cp_parser_operator_function_id (parser);
4822           /* If that didn't work, try a conversion-function-id.  */
4823           if (!cp_parser_parse_definitely (parser))
4824             id = cp_parser_conversion_function_id (parser);
4825           else if (UDLIT_OPER_P (id))
4826             {
4827               /* 17.6.3.3.5  */
4828               const char *name = UDLIT_OP_SUFFIX (id);
4829               if (name[0] != '_' && !in_system_header)
4830                 warning (0, "literal operator suffixes not preceded by %<_%>"
4831                             " are reserved for future standardization");
4832             }
4833
4834           return id;
4835         }
4836       /* Fall through.  */
4837
4838     default:
4839       if (optional_p)
4840         return NULL_TREE;
4841       cp_parser_error (parser, "expected unqualified-id");
4842       return error_mark_node;
4843     }
4844 }
4845
4846 /* Parse an (optional) nested-name-specifier.
4847
4848    nested-name-specifier: [C++98]
4849      class-or-namespace-name :: nested-name-specifier [opt]
4850      class-or-namespace-name :: template nested-name-specifier [opt]
4851
4852    nested-name-specifier: [C++0x]
4853      type-name ::
4854      namespace-name ::
4855      nested-name-specifier identifier ::
4856      nested-name-specifier template [opt] simple-template-id ::
4857
4858    PARSER->SCOPE should be set appropriately before this function is
4859    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4860    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4861    in name lookups.
4862
4863    Sets PARSER->SCOPE to the class (TYPE) or namespace
4864    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4865    it unchanged if there is no nested-name-specifier.  Returns the new
4866    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4867
4868    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4869    part of a declaration and/or decl-specifier.  */
4870
4871 static tree
4872 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4873                                      bool typename_keyword_p,
4874                                      bool check_dependency_p,
4875                                      bool type_p,
4876                                      bool is_declaration)
4877 {
4878   bool success = false;
4879   cp_token_position start = 0;
4880   cp_token *token;
4881
4882   /* Remember where the nested-name-specifier starts.  */
4883   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4884     {
4885       start = cp_lexer_token_position (parser->lexer, false);
4886       push_deferring_access_checks (dk_deferred);
4887     }
4888
4889   while (true)
4890     {
4891       tree new_scope;
4892       tree old_scope;
4893       tree saved_qualifying_scope;
4894       bool template_keyword_p;
4895
4896       /* Spot cases that cannot be the beginning of a
4897          nested-name-specifier.  */
4898       token = cp_lexer_peek_token (parser->lexer);
4899
4900       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4901          the already parsed nested-name-specifier.  */
4902       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4903         {
4904           /* Grab the nested-name-specifier and continue the loop.  */
4905           cp_parser_pre_parsed_nested_name_specifier (parser);
4906           /* If we originally encountered this nested-name-specifier
4907              with IS_DECLARATION set to false, we will not have
4908              resolved TYPENAME_TYPEs, so we must do so here.  */
4909           if (is_declaration
4910               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4911             {
4912               new_scope = resolve_typename_type (parser->scope,
4913                                                  /*only_current_p=*/false);
4914               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4915                 parser->scope = new_scope;
4916             }
4917           success = true;
4918           continue;
4919         }
4920
4921       /* Spot cases that cannot be the beginning of a
4922          nested-name-specifier.  On the second and subsequent times
4923          through the loop, we look for the `template' keyword.  */
4924       if (success && token->keyword == RID_TEMPLATE)
4925         ;
4926       /* A template-id can start a nested-name-specifier.  */
4927       else if (token->type == CPP_TEMPLATE_ID)
4928         ;
4929       /* DR 743: decltype can be used in a nested-name-specifier.  */
4930       else if (token_is_decltype (token))
4931         ;
4932       else
4933         {
4934           /* If the next token is not an identifier, then it is
4935              definitely not a type-name or namespace-name.  */
4936           if (token->type != CPP_NAME)
4937             break;
4938           /* If the following token is neither a `<' (to begin a
4939              template-id), nor a `::', then we are not looking at a
4940              nested-name-specifier.  */
4941           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4942
4943           if (token->type == CPP_COLON
4944               && parser->colon_corrects_to_scope_p
4945               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4946             {
4947               error_at (token->location,
4948                         "found %<:%> in nested-name-specifier, expected %<::%>");
4949               token->type = CPP_SCOPE;
4950             }
4951
4952           if (token->type != CPP_SCOPE
4953               && !cp_parser_nth_token_starts_template_argument_list_p
4954                   (parser, 2))
4955             break;
4956         }
4957
4958       /* The nested-name-specifier is optional, so we parse
4959          tentatively.  */
4960       cp_parser_parse_tentatively (parser);
4961
4962       /* Look for the optional `template' keyword, if this isn't the
4963          first time through the loop.  */
4964       if (success)
4965         template_keyword_p = cp_parser_optional_template_keyword (parser);
4966       else
4967         template_keyword_p = false;
4968
4969       /* Save the old scope since the name lookup we are about to do
4970          might destroy it.  */
4971       old_scope = parser->scope;
4972       saved_qualifying_scope = parser->qualifying_scope;
4973       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4974          look up names in "X<T>::I" in order to determine that "Y" is
4975          a template.  So, if we have a typename at this point, we make
4976          an effort to look through it.  */
4977       if (is_declaration
4978           && !typename_keyword_p
4979           && parser->scope
4980           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4981         parser->scope = resolve_typename_type (parser->scope,
4982                                                /*only_current_p=*/false);
4983       /* Parse the qualifying entity.  */
4984       new_scope
4985         = cp_parser_qualifying_entity (parser,
4986                                        typename_keyword_p,
4987                                        template_keyword_p,
4988                                        check_dependency_p,
4989                                        type_p,
4990                                        is_declaration);
4991       /* Look for the `::' token.  */
4992       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4993
4994       /* If we found what we wanted, we keep going; otherwise, we're
4995          done.  */
4996       if (!cp_parser_parse_definitely (parser))
4997         {
4998           bool error_p = false;
4999
5000           /* Restore the OLD_SCOPE since it was valid before the
5001              failed attempt at finding the last
5002              class-or-namespace-name.  */
5003           parser->scope = old_scope;
5004           parser->qualifying_scope = saved_qualifying_scope;
5005
5006           /* If the next token is a decltype, and the one after that is a
5007              `::', then the decltype has failed to resolve to a class or
5008              enumeration type.  Give this error even when parsing
5009              tentatively since it can't possibly be valid--and we're going
5010              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5011              won't get another chance.*/
5012           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5013               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5014                   == CPP_SCOPE))
5015             {
5016               token = cp_lexer_consume_token (parser->lexer);
5017               error_at (token->location, "decltype evaluates to %qT, "
5018                         "which is not a class or enumeration type",
5019                         token->u.value);
5020               parser->scope = error_mark_node;
5021               error_p = true;
5022               /* As below.  */
5023               success = true;
5024               cp_lexer_consume_token (parser->lexer);
5025             }
5026
5027           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5028             break;
5029           /* If the next token is an identifier, and the one after
5030              that is a `::', then any valid interpretation would have
5031              found a class-or-namespace-name.  */
5032           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5033                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5034                      == CPP_SCOPE)
5035                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5036                      != CPP_COMPL))
5037             {
5038               token = cp_lexer_consume_token (parser->lexer);
5039               if (!error_p)
5040                 {
5041                   if (!token->ambiguous_p)
5042                     {
5043                       tree decl;
5044                       tree ambiguous_decls;
5045
5046                       decl = cp_parser_lookup_name (parser, token->u.value,
5047                                                     none_type,
5048                                                     /*is_template=*/false,
5049                                                     /*is_namespace=*/false,
5050                                                     /*check_dependency=*/true,
5051                                                     &ambiguous_decls,
5052                                                     token->location);
5053                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5054                         error_at (token->location,
5055                                   "%qD used without template parameters",
5056                                   decl);
5057                       else if (ambiguous_decls)
5058                         {
5059                           error_at (token->location,
5060                                     "reference to %qD is ambiguous",
5061                                     token->u.value);
5062                           print_candidates (ambiguous_decls);
5063                           decl = error_mark_node;
5064                         }
5065                       else
5066                         {
5067                           if (cxx_dialect != cxx98)
5068                             cp_parser_name_lookup_error
5069                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5070                              token->location);
5071                           else
5072                             cp_parser_name_lookup_error
5073                             (parser, token->u.value, decl, NLE_CXX98,
5074                              token->location);
5075                         }
5076                     }
5077                   parser->scope = error_mark_node;
5078                   error_p = true;
5079                   /* Treat this as a successful nested-name-specifier
5080                      due to:
5081
5082                      [basic.lookup.qual]
5083
5084                      If the name found is not a class-name (clause
5085                      _class_) or namespace-name (_namespace.def_), the
5086                      program is ill-formed.  */
5087                   success = true;
5088                 }
5089               cp_lexer_consume_token (parser->lexer);
5090             }
5091           break;
5092         }
5093       /* We've found one valid nested-name-specifier.  */
5094       success = true;
5095       /* Name lookup always gives us a DECL.  */
5096       if (TREE_CODE (new_scope) == TYPE_DECL)
5097         new_scope = TREE_TYPE (new_scope);
5098       /* Uses of "template" must be followed by actual templates.  */
5099       if (template_keyword_p
5100           && !(CLASS_TYPE_P (new_scope)
5101                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5102                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5103                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5104           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5105                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5106                    == TEMPLATE_ID_EXPR)))
5107         permerror (input_location, TYPE_P (new_scope)
5108                    ? G_("%qT is not a template")
5109                    : G_("%qD is not a template"),
5110                    new_scope);
5111       /* If it is a class scope, try to complete it; we are about to
5112          be looking up names inside the class.  */
5113       if (TYPE_P (new_scope)
5114           /* Since checking types for dependency can be expensive,
5115              avoid doing it if the type is already complete.  */
5116           && !COMPLETE_TYPE_P (new_scope)
5117           /* Do not try to complete dependent types.  */
5118           && !dependent_type_p (new_scope))
5119         {
5120           new_scope = complete_type (new_scope);
5121           /* If it is a typedef to current class, use the current
5122              class instead, as the typedef won't have any names inside
5123              it yet.  */
5124           if (!COMPLETE_TYPE_P (new_scope)
5125               && currently_open_class (new_scope))
5126             new_scope = TYPE_MAIN_VARIANT (new_scope);
5127         }
5128       /* Make sure we look in the right scope the next time through
5129          the loop.  */
5130       parser->scope = new_scope;
5131     }
5132
5133   /* If parsing tentatively, replace the sequence of tokens that makes
5134      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5135      token.  That way, should we re-parse the token stream, we will
5136      not have to repeat the effort required to do the parse, nor will
5137      we issue duplicate error messages.  */
5138   if (success && start)
5139     {
5140       cp_token *token;
5141
5142       token = cp_lexer_token_at (parser->lexer, start);
5143       /* Reset the contents of the START token.  */
5144       token->type = CPP_NESTED_NAME_SPECIFIER;
5145       /* Retrieve any deferred checks.  Do not pop this access checks yet
5146          so the memory will not be reclaimed during token replacing below.  */
5147       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5148       token->u.tree_check_value->value = parser->scope;
5149       token->u.tree_check_value->checks = get_deferred_access_checks ();
5150       token->u.tree_check_value->qualifying_scope =
5151         parser->qualifying_scope;
5152       token->keyword = RID_MAX;
5153
5154       /* Purge all subsequent tokens.  */
5155       cp_lexer_purge_tokens_after (parser->lexer, start);
5156     }
5157
5158   if (start)
5159     pop_to_parent_deferring_access_checks ();
5160
5161   return success ? parser->scope : NULL_TREE;
5162 }
5163
5164 /* Parse a nested-name-specifier.  See
5165    cp_parser_nested_name_specifier_opt for details.  This function
5166    behaves identically, except that it will an issue an error if no
5167    nested-name-specifier is present.  */
5168
5169 static tree
5170 cp_parser_nested_name_specifier (cp_parser *parser,
5171                                  bool typename_keyword_p,
5172                                  bool check_dependency_p,
5173                                  bool type_p,
5174                                  bool is_declaration)
5175 {
5176   tree scope;
5177
5178   /* Look for the nested-name-specifier.  */
5179   scope = cp_parser_nested_name_specifier_opt (parser,
5180                                                typename_keyword_p,
5181                                                check_dependency_p,
5182                                                type_p,
5183                                                is_declaration);
5184   /* If it was not present, issue an error message.  */
5185   if (!scope)
5186     {
5187       cp_parser_error (parser, "expected nested-name-specifier");
5188       parser->scope = NULL_TREE;
5189     }
5190
5191   return scope;
5192 }
5193
5194 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5195    this is either a class-name or a namespace-name (which corresponds
5196    to the class-or-namespace-name production in the grammar). For
5197    C++0x, it can also be a type-name that refers to an enumeration
5198    type or a simple-template-id.
5199
5200    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5201    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5202    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5203    TYPE_P is TRUE iff the next name should be taken as a class-name,
5204    even the same name is declared to be another entity in the same
5205    scope.
5206
5207    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5208    specified by the class-or-namespace-name.  If neither is found the
5209    ERROR_MARK_NODE is returned.  */
5210
5211 static tree
5212 cp_parser_qualifying_entity (cp_parser *parser,
5213                              bool typename_keyword_p,
5214                              bool template_keyword_p,
5215                              bool check_dependency_p,
5216                              bool type_p,
5217                              bool is_declaration)
5218 {
5219   tree saved_scope;
5220   tree saved_qualifying_scope;
5221   tree saved_object_scope;
5222   tree scope;
5223   bool only_class_p;
5224   bool successful_parse_p;
5225
5226   /* DR 743: decltype can appear in a nested-name-specifier.  */
5227   if (cp_lexer_next_token_is_decltype (parser->lexer))
5228     {
5229       scope = cp_parser_decltype (parser);
5230       if (TREE_CODE (scope) != ENUMERAL_TYPE
5231           && !MAYBE_CLASS_TYPE_P (scope))
5232         {
5233           cp_parser_simulate_error (parser);
5234           return error_mark_node;
5235         }
5236       if (TYPE_NAME (scope))
5237         scope = TYPE_NAME (scope);
5238       return scope;
5239     }
5240
5241   /* Before we try to parse the class-name, we must save away the
5242      current PARSER->SCOPE since cp_parser_class_name will destroy
5243      it.  */
5244   saved_scope = parser->scope;
5245   saved_qualifying_scope = parser->qualifying_scope;
5246   saved_object_scope = parser->object_scope;
5247   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5248      there is no need to look for a namespace-name.  */
5249   only_class_p = template_keyword_p 
5250     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5251   if (!only_class_p)
5252     cp_parser_parse_tentatively (parser);
5253   scope = cp_parser_class_name (parser,
5254                                 typename_keyword_p,
5255                                 template_keyword_p,
5256                                 type_p ? class_type : none_type,
5257                                 check_dependency_p,
5258                                 /*class_head_p=*/false,
5259                                 is_declaration);
5260   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5261   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5262   if (!only_class_p 
5263       && cxx_dialect != cxx98
5264       && !successful_parse_p)
5265     {
5266       /* Restore the saved scope.  */
5267       parser->scope = saved_scope;
5268       parser->qualifying_scope = saved_qualifying_scope;
5269       parser->object_scope = saved_object_scope;
5270
5271       /* Parse tentatively.  */
5272       cp_parser_parse_tentatively (parser);
5273      
5274       /* Parse a type-name  */
5275       scope = cp_parser_type_name (parser);
5276
5277       /* "If the name found does not designate a namespace or a class,
5278          enumeration, or dependent type, the program is ill-formed."
5279
5280          We cover classes and dependent types above and namespaces below,
5281          so this code is only looking for enums.  */
5282       if (!scope || TREE_CODE (scope) != TYPE_DECL
5283           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5284         cp_parser_simulate_error (parser);
5285
5286       successful_parse_p = cp_parser_parse_definitely (parser);
5287     }
5288   /* If that didn't work, try for a namespace-name.  */
5289   if (!only_class_p && !successful_parse_p)
5290     {
5291       /* Restore the saved scope.  */
5292       parser->scope = saved_scope;
5293       parser->qualifying_scope = saved_qualifying_scope;
5294       parser->object_scope = saved_object_scope;
5295       /* If we are not looking at an identifier followed by the scope
5296          resolution operator, then this is not part of a
5297          nested-name-specifier.  (Note that this function is only used
5298          to parse the components of a nested-name-specifier.)  */
5299       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5300           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5301         return error_mark_node;
5302       scope = cp_parser_namespace_name (parser);
5303     }
5304
5305   return scope;
5306 }
5307
5308 /* Parse a postfix-expression.
5309
5310    postfix-expression:
5311      primary-expression
5312      postfix-expression [ expression ]
5313      postfix-expression ( expression-list [opt] )
5314      simple-type-specifier ( expression-list [opt] )
5315      typename :: [opt] nested-name-specifier identifier
5316        ( expression-list [opt] )
5317      typename :: [opt] nested-name-specifier template [opt] template-id
5318        ( expression-list [opt] )
5319      postfix-expression . template [opt] id-expression
5320      postfix-expression -> template [opt] id-expression
5321      postfix-expression . pseudo-destructor-name
5322      postfix-expression -> pseudo-destructor-name
5323      postfix-expression ++
5324      postfix-expression --
5325      dynamic_cast < type-id > ( expression )
5326      static_cast < type-id > ( expression )
5327      reinterpret_cast < type-id > ( expression )
5328      const_cast < type-id > ( expression )
5329      typeid ( expression )
5330      typeid ( type-id )
5331
5332    GNU Extension:
5333
5334    postfix-expression:
5335      ( type-id ) { initializer-list , [opt] }
5336
5337    This extension is a GNU version of the C99 compound-literal
5338    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5339    but they are essentially the same concept.)
5340
5341    If ADDRESS_P is true, the postfix expression is the operand of the
5342    `&' operator.  CAST_P is true if this expression is the target of a
5343    cast.
5344
5345    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5346    class member access expressions [expr.ref].
5347
5348    Returns a representation of the expression.  */
5349
5350 static tree
5351 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5352                               bool member_access_only_p,
5353                               cp_id_kind * pidk_return)
5354 {
5355   cp_token *token;
5356   enum rid keyword;
5357   cp_id_kind idk = CP_ID_KIND_NONE;
5358   tree postfix_expression = NULL_TREE;
5359   bool is_member_access = false;
5360
5361   /* Peek at the next token.  */
5362   token = cp_lexer_peek_token (parser->lexer);
5363   /* Some of the productions are determined by keywords.  */
5364   keyword = token->keyword;
5365   switch (keyword)
5366     {
5367     case RID_DYNCAST:
5368     case RID_STATCAST:
5369     case RID_REINTCAST:
5370     case RID_CONSTCAST:
5371       {
5372         tree type;
5373         tree expression;
5374         const char *saved_message;
5375
5376         /* All of these can be handled in the same way from the point
5377            of view of parsing.  Begin by consuming the token
5378            identifying the cast.  */
5379         cp_lexer_consume_token (parser->lexer);
5380
5381         /* New types cannot be defined in the cast.  */
5382         saved_message = parser->type_definition_forbidden_message;
5383         parser->type_definition_forbidden_message
5384           = G_("types may not be defined in casts");
5385
5386         /* Look for the opening `<'.  */
5387         cp_parser_require (parser, CPP_LESS, RT_LESS);
5388         /* Parse the type to which we are casting.  */
5389         type = cp_parser_type_id (parser);
5390         /* Look for the closing `>'.  */
5391         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5392         /* Restore the old message.  */
5393         parser->type_definition_forbidden_message = saved_message;
5394
5395         /* And the expression which is being cast.  */
5396         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5397         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5398         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5399
5400         /* Only type conversions to integral or enumeration types
5401            can be used in constant-expressions.  */
5402         if (!cast_valid_in_integral_constant_expression_p (type)
5403             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5404           return error_mark_node;
5405
5406         switch (keyword)
5407           {
5408           case RID_DYNCAST:
5409             postfix_expression
5410               = build_dynamic_cast (type, expression, tf_warning_or_error);
5411             break;
5412           case RID_STATCAST:
5413             postfix_expression
5414               = build_static_cast (type, expression, tf_warning_or_error);
5415             break;
5416           case RID_REINTCAST:
5417             postfix_expression
5418               = build_reinterpret_cast (type, expression, 
5419                                         tf_warning_or_error);
5420             break;
5421           case RID_CONSTCAST:
5422             postfix_expression
5423               = build_const_cast (type, expression, tf_warning_or_error);
5424             break;
5425           default:
5426             gcc_unreachable ();
5427           }
5428       }
5429       break;
5430
5431     case RID_TYPEID:
5432       {
5433         tree type;
5434         const char *saved_message;
5435         bool saved_in_type_id_in_expr_p;
5436
5437         /* Consume the `typeid' token.  */
5438         cp_lexer_consume_token (parser->lexer);
5439         /* Look for the `(' token.  */
5440         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5441         /* Types cannot be defined in a `typeid' expression.  */
5442         saved_message = parser->type_definition_forbidden_message;
5443         parser->type_definition_forbidden_message
5444           = G_("types may not be defined in a %<typeid%> expression");
5445         /* We can't be sure yet whether we're looking at a type-id or an
5446            expression.  */
5447         cp_parser_parse_tentatively (parser);
5448         /* Try a type-id first.  */
5449         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5450         parser->in_type_id_in_expr_p = true;
5451         type = cp_parser_type_id (parser);
5452         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5453         /* Look for the `)' token.  Otherwise, we can't be sure that
5454            we're not looking at an expression: consider `typeid (int
5455            (3))', for example.  */
5456         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5457         /* If all went well, simply lookup the type-id.  */
5458         if (cp_parser_parse_definitely (parser))
5459           postfix_expression = get_typeid (type);
5460         /* Otherwise, fall back to the expression variant.  */
5461         else
5462           {
5463             tree expression;
5464
5465             /* Look for an expression.  */
5466             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5467             /* Compute its typeid.  */
5468             postfix_expression = build_typeid (expression);
5469             /* Look for the `)' token.  */
5470             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5471           }
5472         /* Restore the saved message.  */
5473         parser->type_definition_forbidden_message = saved_message;
5474         /* `typeid' may not appear in an integral constant expression.  */
5475         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5476           return error_mark_node;
5477       }
5478       break;
5479
5480     case RID_TYPENAME:
5481       {
5482         tree type;
5483         /* The syntax permitted here is the same permitted for an
5484            elaborated-type-specifier.  */
5485         type = cp_parser_elaborated_type_specifier (parser,
5486                                                     /*is_friend=*/false,
5487                                                     /*is_declaration=*/false);
5488         postfix_expression = cp_parser_functional_cast (parser, type);
5489       }
5490       break;
5491
5492     default:
5493       {
5494         tree type;
5495
5496         /* If the next thing is a simple-type-specifier, we may be
5497            looking at a functional cast.  We could also be looking at
5498            an id-expression.  So, we try the functional cast, and if
5499            that doesn't work we fall back to the primary-expression.  */
5500         cp_parser_parse_tentatively (parser);
5501         /* Look for the simple-type-specifier.  */
5502         type = cp_parser_simple_type_specifier (parser,
5503                                                 /*decl_specs=*/NULL,
5504                                                 CP_PARSER_FLAGS_NONE);
5505         /* Parse the cast itself.  */
5506         if (!cp_parser_error_occurred (parser))
5507           postfix_expression
5508             = cp_parser_functional_cast (parser, type);
5509         /* If that worked, we're done.  */
5510         if (cp_parser_parse_definitely (parser))
5511           break;
5512
5513         /* If the functional-cast didn't work out, try a
5514            compound-literal.  */
5515         if (cp_parser_allow_gnu_extensions_p (parser)
5516             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5517           {
5518             VEC(constructor_elt,gc) *initializer_list = NULL;
5519             bool saved_in_type_id_in_expr_p;
5520
5521             cp_parser_parse_tentatively (parser);
5522             /* Consume the `('.  */
5523             cp_lexer_consume_token (parser->lexer);
5524             /* Parse the type.  */
5525             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5526             parser->in_type_id_in_expr_p = true;
5527             type = cp_parser_type_id (parser);
5528             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5529             /* Look for the `)'.  */
5530             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5531             /* Look for the `{'.  */
5532             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5533             /* If things aren't going well, there's no need to
5534                keep going.  */
5535             if (!cp_parser_error_occurred (parser))
5536               {
5537                 bool non_constant_p;
5538                 /* Parse the initializer-list.  */
5539                 initializer_list
5540                   = cp_parser_initializer_list (parser, &non_constant_p);
5541                 /* Allow a trailing `,'.  */
5542                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5543                   cp_lexer_consume_token (parser->lexer);
5544                 /* Look for the final `}'.  */
5545                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5546               }
5547             /* If that worked, we're definitely looking at a
5548                compound-literal expression.  */
5549             if (cp_parser_parse_definitely (parser))
5550               {
5551                 /* Warn the user that a compound literal is not
5552                    allowed in standard C++.  */
5553                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5554                 /* For simplicity, we disallow compound literals in
5555                    constant-expressions.  We could
5556                    allow compound literals of integer type, whose
5557                    initializer was a constant, in constant
5558                    expressions.  Permitting that usage, as a further
5559                    extension, would not change the meaning of any
5560                    currently accepted programs.  (Of course, as
5561                    compound literals are not part of ISO C++, the
5562                    standard has nothing to say.)  */
5563                 if (cp_parser_non_integral_constant_expression (parser,
5564                                                                 NIC_NCC))
5565                   {
5566                     postfix_expression = error_mark_node;
5567                     break;
5568                   }
5569                 /* Form the representation of the compound-literal.  */
5570                 postfix_expression
5571                   = (finish_compound_literal
5572                      (type, build_constructor (init_list_type_node,
5573                                                initializer_list),
5574                       tf_warning_or_error));
5575                 break;
5576               }
5577           }
5578
5579         /* It must be a primary-expression.  */
5580         postfix_expression
5581           = cp_parser_primary_expression (parser, address_p, cast_p,
5582                                           /*template_arg_p=*/false,
5583                                           &idk);
5584       }
5585       break;
5586     }
5587
5588   /* Keep looping until the postfix-expression is complete.  */
5589   while (true)
5590     {
5591       if (idk == CP_ID_KIND_UNQUALIFIED
5592           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5593           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5594         /* It is not a Koenig lookup function call.  */
5595         postfix_expression
5596           = unqualified_name_lookup_error (postfix_expression);
5597
5598       /* Peek at the next token.  */
5599       token = cp_lexer_peek_token (parser->lexer);
5600
5601       switch (token->type)
5602         {
5603         case CPP_OPEN_SQUARE:
5604           postfix_expression
5605             = cp_parser_postfix_open_square_expression (parser,
5606                                                         postfix_expression,
5607                                                         false);
5608           idk = CP_ID_KIND_NONE;
5609           is_member_access = false;
5610           break;
5611
5612         case CPP_OPEN_PAREN:
5613           /* postfix-expression ( expression-list [opt] ) */
5614           {
5615             bool koenig_p;
5616             bool is_builtin_constant_p;
5617             bool saved_integral_constant_expression_p = false;
5618             bool saved_non_integral_constant_expression_p = false;
5619             VEC(tree,gc) *args;
5620
5621             is_member_access = false;
5622
5623             is_builtin_constant_p
5624               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5625             if (is_builtin_constant_p)
5626               {
5627                 /* The whole point of __builtin_constant_p is to allow
5628                    non-constant expressions to appear as arguments.  */
5629                 saved_integral_constant_expression_p
5630                   = parser->integral_constant_expression_p;
5631                 saved_non_integral_constant_expression_p
5632                   = parser->non_integral_constant_expression_p;
5633                 parser->integral_constant_expression_p = false;
5634               }
5635             args = (cp_parser_parenthesized_expression_list
5636                     (parser, non_attr,
5637                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5638                      /*non_constant_p=*/NULL));
5639             if (is_builtin_constant_p)
5640               {
5641                 parser->integral_constant_expression_p
5642                   = saved_integral_constant_expression_p;
5643                 parser->non_integral_constant_expression_p
5644                   = saved_non_integral_constant_expression_p;
5645               }
5646
5647             if (args == NULL)
5648               {
5649                 postfix_expression = error_mark_node;
5650                 break;
5651               }
5652
5653             /* Function calls are not permitted in
5654                constant-expressions.  */
5655             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5656                 && cp_parser_non_integral_constant_expression (parser,
5657                                                                NIC_FUNC_CALL))
5658               {
5659                 postfix_expression = error_mark_node;
5660                 release_tree_vector (args);
5661                 break;
5662               }
5663
5664             koenig_p = false;
5665             if (idk == CP_ID_KIND_UNQUALIFIED
5666                 || idk == CP_ID_KIND_TEMPLATE_ID)
5667               {
5668                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5669                   {
5670                     if (!VEC_empty (tree, args))
5671                       {
5672                         koenig_p = true;
5673                         if (!any_type_dependent_arguments_p (args))
5674                           postfix_expression
5675                             = perform_koenig_lookup (postfix_expression, args,
5676                                                      /*include_std=*/false,
5677                                                      tf_warning_or_error);
5678                       }
5679                     else
5680                       postfix_expression
5681                         = unqualified_fn_lookup_error (postfix_expression);
5682                   }
5683                 /* We do not perform argument-dependent lookup if
5684                    normal lookup finds a non-function, in accordance
5685                    with the expected resolution of DR 218.  */
5686                 else if (!VEC_empty (tree, args)
5687                          && is_overloaded_fn (postfix_expression))
5688                   {
5689                     tree fn = get_first_fn (postfix_expression);
5690                     fn = STRIP_TEMPLATE (fn);
5691
5692                     /* Do not do argument dependent lookup if regular
5693                        lookup finds a member function or a block-scope
5694                        function declaration.  [basic.lookup.argdep]/3  */
5695                     if (!DECL_FUNCTION_MEMBER_P (fn)
5696                         && !DECL_LOCAL_FUNCTION_P (fn))
5697                       {
5698                         koenig_p = true;
5699                         if (!any_type_dependent_arguments_p (args))
5700                           postfix_expression
5701                             = perform_koenig_lookup (postfix_expression, args,
5702                                                      /*include_std=*/false,
5703                                                      tf_warning_or_error);
5704                       }
5705                   }
5706               }
5707
5708             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5709               {
5710                 tree instance = TREE_OPERAND (postfix_expression, 0);
5711                 tree fn = TREE_OPERAND (postfix_expression, 1);
5712
5713                 if (processing_template_decl
5714                     && (type_dependent_expression_p (instance)
5715                         || (!BASELINK_P (fn)
5716                             && TREE_CODE (fn) != FIELD_DECL)
5717                         || type_dependent_expression_p (fn)
5718                         || any_type_dependent_arguments_p (args)))
5719                   {
5720                     postfix_expression
5721                       = build_nt_call_vec (postfix_expression, args);
5722                     release_tree_vector (args);
5723                     break;
5724                   }
5725
5726                 if (BASELINK_P (fn))
5727                   {
5728                   postfix_expression
5729                     = (build_new_method_call
5730                        (instance, fn, &args, NULL_TREE,
5731                         (idk == CP_ID_KIND_QUALIFIED
5732                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5733                          : LOOKUP_NORMAL),
5734                         /*fn_p=*/NULL,
5735                         tf_warning_or_error));
5736                   }
5737                 else
5738                   postfix_expression
5739                     = finish_call_expr (postfix_expression, &args,
5740                                         /*disallow_virtual=*/false,
5741                                         /*koenig_p=*/false,
5742                                         tf_warning_or_error);
5743               }
5744             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5745                      || TREE_CODE (postfix_expression) == MEMBER_REF
5746                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5747               postfix_expression = (build_offset_ref_call_from_tree
5748                                     (postfix_expression, &args));
5749             else if (idk == CP_ID_KIND_QUALIFIED)
5750               /* A call to a static class member, or a namespace-scope
5751                  function.  */
5752               postfix_expression
5753                 = finish_call_expr (postfix_expression, &args,
5754                                     /*disallow_virtual=*/true,
5755                                     koenig_p,
5756                                     tf_warning_or_error);
5757             else
5758               /* All other function calls.  */
5759               postfix_expression
5760                 = finish_call_expr (postfix_expression, &args,
5761                                     /*disallow_virtual=*/false,
5762                                     koenig_p,
5763                                     tf_warning_or_error);
5764
5765             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5766             idk = CP_ID_KIND_NONE;
5767
5768             release_tree_vector (args);
5769           }
5770           break;
5771
5772         case CPP_DOT:
5773         case CPP_DEREF:
5774           /* postfix-expression . template [opt] id-expression
5775              postfix-expression . pseudo-destructor-name
5776              postfix-expression -> template [opt] id-expression
5777              postfix-expression -> pseudo-destructor-name */
5778
5779           /* Consume the `.' or `->' operator.  */
5780           cp_lexer_consume_token (parser->lexer);
5781
5782           postfix_expression
5783             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5784                                                       postfix_expression,
5785                                                       false, &idk,
5786                                                       token->location);
5787
5788           is_member_access = true;
5789           break;
5790
5791         case CPP_PLUS_PLUS:
5792           /* postfix-expression ++  */
5793           /* Consume the `++' token.  */
5794           cp_lexer_consume_token (parser->lexer);
5795           /* Generate a representation for the complete expression.  */
5796           postfix_expression
5797             = finish_increment_expr (postfix_expression,
5798                                      POSTINCREMENT_EXPR);
5799           /* Increments may not appear in constant-expressions.  */
5800           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5801             postfix_expression = error_mark_node;
5802           idk = CP_ID_KIND_NONE;
5803           is_member_access = false;
5804           break;
5805
5806         case CPP_MINUS_MINUS:
5807           /* postfix-expression -- */
5808           /* Consume the `--' token.  */
5809           cp_lexer_consume_token (parser->lexer);
5810           /* Generate a representation for the complete expression.  */
5811           postfix_expression
5812             = finish_increment_expr (postfix_expression,
5813                                      POSTDECREMENT_EXPR);
5814           /* Decrements may not appear in constant-expressions.  */
5815           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5816             postfix_expression = error_mark_node;
5817           idk = CP_ID_KIND_NONE;
5818           is_member_access = false;
5819           break;
5820
5821         default:
5822           if (pidk_return != NULL)
5823             * pidk_return = idk;
5824           if (member_access_only_p)
5825             return is_member_access? postfix_expression : error_mark_node;
5826           else
5827             return postfix_expression;
5828         }
5829     }
5830
5831   /* We should never get here.  */
5832   gcc_unreachable ();
5833   return error_mark_node;
5834 }
5835
5836 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5837    by cp_parser_builtin_offsetof.  We're looking for
5838
5839      postfix-expression [ expression ]
5840      postfix-expression [ braced-init-list ] (C++11)
5841
5842    FOR_OFFSETOF is set if we're being called in that context, which
5843    changes how we deal with integer constant expressions.  */
5844
5845 static tree
5846 cp_parser_postfix_open_square_expression (cp_parser *parser,
5847                                           tree postfix_expression,
5848                                           bool for_offsetof)
5849 {
5850   tree index;
5851
5852   /* Consume the `[' token.  */
5853   cp_lexer_consume_token (parser->lexer);
5854
5855   /* Parse the index expression.  */
5856   /* ??? For offsetof, there is a question of what to allow here.  If
5857      offsetof is not being used in an integral constant expression context,
5858      then we *could* get the right answer by computing the value at runtime.
5859      If we are in an integral constant expression context, then we might
5860      could accept any constant expression; hard to say without analysis.
5861      Rather than open the barn door too wide right away, allow only integer
5862      constant expressions here.  */
5863   if (for_offsetof)
5864     index = cp_parser_constant_expression (parser, false, NULL);
5865   else
5866     {
5867       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5868         {
5869           bool expr_nonconst_p;
5870           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5871           index = cp_parser_braced_list (parser, &expr_nonconst_p);
5872         }
5873       else
5874         index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5875     }
5876
5877   /* Look for the closing `]'.  */
5878   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5879
5880   /* Build the ARRAY_REF.  */
5881   postfix_expression = grok_array_decl (postfix_expression, index);
5882
5883   /* When not doing offsetof, array references are not permitted in
5884      constant-expressions.  */
5885   if (!for_offsetof
5886       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5887     postfix_expression = error_mark_node;
5888
5889   return postfix_expression;
5890 }
5891
5892 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5893    by cp_parser_builtin_offsetof.  We're looking for
5894
5895      postfix-expression . template [opt] id-expression
5896      postfix-expression . pseudo-destructor-name
5897      postfix-expression -> template [opt] id-expression
5898      postfix-expression -> pseudo-destructor-name
5899
5900    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5901    limits what of the above we'll actually accept, but nevermind.
5902    TOKEN_TYPE is the "." or "->" token, which will already have been
5903    removed from the stream.  */
5904
5905 static tree
5906 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5907                                         enum cpp_ttype token_type,
5908                                         tree postfix_expression,
5909                                         bool for_offsetof, cp_id_kind *idk,
5910                                         location_t location)
5911 {
5912   tree name;
5913   bool dependent_p;
5914   bool pseudo_destructor_p;
5915   tree scope = NULL_TREE;
5916
5917   /* If this is a `->' operator, dereference the pointer.  */
5918   if (token_type == CPP_DEREF)
5919     postfix_expression = build_x_arrow (postfix_expression);
5920   /* Check to see whether or not the expression is type-dependent.  */
5921   dependent_p = type_dependent_expression_p (postfix_expression);
5922   /* The identifier following the `->' or `.' is not qualified.  */
5923   parser->scope = NULL_TREE;
5924   parser->qualifying_scope = NULL_TREE;
5925   parser->object_scope = NULL_TREE;
5926   *idk = CP_ID_KIND_NONE;
5927
5928   /* Enter the scope corresponding to the type of the object
5929      given by the POSTFIX_EXPRESSION.  */
5930   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5931     {
5932       scope = TREE_TYPE (postfix_expression);
5933       /* According to the standard, no expression should ever have
5934          reference type.  Unfortunately, we do not currently match
5935          the standard in this respect in that our internal representation
5936          of an expression may have reference type even when the standard
5937          says it does not.  Therefore, we have to manually obtain the
5938          underlying type here.  */
5939       scope = non_reference (scope);
5940       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5941       if (scope == unknown_type_node)
5942         {
5943           error_at (location, "%qE does not have class type",
5944                     postfix_expression);
5945           scope = NULL_TREE;
5946         }
5947       /* Unlike the object expression in other contexts, *this is not
5948          required to be of complete type for purposes of class member
5949          access (5.2.5) outside the member function body.  */
5950       else if (scope != current_class_ref
5951                && !(processing_template_decl && scope == current_class_type))
5952         scope = complete_type_or_else (scope, NULL_TREE);
5953       /* Let the name lookup machinery know that we are processing a
5954          class member access expression.  */
5955       parser->context->object_type = scope;
5956       /* If something went wrong, we want to be able to discern that case,
5957          as opposed to the case where there was no SCOPE due to the type
5958          of expression being dependent.  */
5959       if (!scope)
5960         scope = error_mark_node;
5961       /* If the SCOPE was erroneous, make the various semantic analysis
5962          functions exit quickly -- and without issuing additional error
5963          messages.  */
5964       if (scope == error_mark_node)
5965         postfix_expression = error_mark_node;
5966     }
5967
5968   /* Assume this expression is not a pseudo-destructor access.  */
5969   pseudo_destructor_p = false;
5970
5971   /* If the SCOPE is a scalar type, then, if this is a valid program,
5972      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5973      is type dependent, it can be pseudo-destructor-name or something else.
5974      Try to parse it as pseudo-destructor-name first.  */
5975   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5976     {
5977       tree s;
5978       tree type;
5979
5980       cp_parser_parse_tentatively (parser);
5981       /* Parse the pseudo-destructor-name.  */
5982       s = NULL_TREE;
5983       cp_parser_pseudo_destructor_name (parser, &s, &type);
5984       if (dependent_p
5985           && (cp_parser_error_occurred (parser)
5986               || TREE_CODE (type) != TYPE_DECL
5987               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5988         cp_parser_abort_tentative_parse (parser);
5989       else if (cp_parser_parse_definitely (parser))
5990         {
5991           pseudo_destructor_p = true;
5992           postfix_expression
5993             = finish_pseudo_destructor_expr (postfix_expression,
5994                                              s, TREE_TYPE (type));
5995         }
5996     }
5997
5998   if (!pseudo_destructor_p)
5999     {
6000       /* If the SCOPE is not a scalar type, we are looking at an
6001          ordinary class member access expression, rather than a
6002          pseudo-destructor-name.  */
6003       bool template_p;
6004       cp_token *token = cp_lexer_peek_token (parser->lexer);
6005       /* Parse the id-expression.  */
6006       name = (cp_parser_id_expression
6007               (parser,
6008                cp_parser_optional_template_keyword (parser),
6009                /*check_dependency_p=*/true,
6010                &template_p,
6011                /*declarator_p=*/false,
6012                /*optional_p=*/false));
6013       /* In general, build a SCOPE_REF if the member name is qualified.
6014          However, if the name was not dependent and has already been
6015          resolved; there is no need to build the SCOPE_REF.  For example;
6016
6017              struct X { void f(); };
6018              template <typename T> void f(T* t) { t->X::f(); }
6019
6020          Even though "t" is dependent, "X::f" is not and has been resolved
6021          to a BASELINK; there is no need to include scope information.  */
6022
6023       /* But we do need to remember that there was an explicit scope for
6024          virtual function calls.  */
6025       if (parser->scope)
6026         *idk = CP_ID_KIND_QUALIFIED;
6027
6028       /* If the name is a template-id that names a type, we will get a
6029          TYPE_DECL here.  That is invalid code.  */
6030       if (TREE_CODE (name) == TYPE_DECL)
6031         {
6032           error_at (token->location, "invalid use of %qD", name);
6033           postfix_expression = error_mark_node;
6034         }
6035       else
6036         {
6037           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6038             {
6039               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6040                 {
6041                   error_at (token->location, "%<%D::%D%> is not a class member",
6042                             parser->scope, name);
6043                   postfix_expression = error_mark_node;
6044                 }
6045               else
6046                 name = build_qualified_name (/*type=*/NULL_TREE,
6047                                              parser->scope,
6048                                              name,
6049                                              template_p);
6050               parser->scope = NULL_TREE;
6051               parser->qualifying_scope = NULL_TREE;
6052               parser->object_scope = NULL_TREE;
6053             }
6054           if (parser->scope && name && BASELINK_P (name))
6055             adjust_result_of_qualified_name_lookup
6056               (name, parser->scope, scope);
6057           postfix_expression
6058             = finish_class_member_access_expr (postfix_expression, name,
6059                                                template_p, 
6060                                                tf_warning_or_error);
6061         }
6062     }
6063
6064   /* We no longer need to look up names in the scope of the object on
6065      the left-hand side of the `.' or `->' operator.  */
6066   parser->context->object_type = NULL_TREE;
6067
6068   /* Outside of offsetof, these operators may not appear in
6069      constant-expressions.  */
6070   if (!for_offsetof
6071       && (cp_parser_non_integral_constant_expression
6072           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6073     postfix_expression = error_mark_node;
6074
6075   return postfix_expression;
6076 }
6077
6078 /* Parse a parenthesized expression-list.
6079
6080    expression-list:
6081      assignment-expression
6082      expression-list, assignment-expression
6083
6084    attribute-list:
6085      expression-list
6086      identifier
6087      identifier, expression-list
6088
6089    CAST_P is true if this expression is the target of a cast.
6090
6091    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6092    argument pack.
6093
6094    Returns a vector of trees.  Each element is a representation of an
6095    assignment-expression.  NULL is returned if the ( and or ) are
6096    missing.  An empty, but allocated, vector is returned on no
6097    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6098    if we are parsing an attribute list for an attribute that wants a
6099    plain identifier argument, normal_attr for an attribute that wants
6100    an expression, or non_attr if we aren't parsing an attribute list.  If
6101    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6102    not all of the expressions in the list were constant.  */
6103
6104 static VEC(tree,gc) *
6105 cp_parser_parenthesized_expression_list (cp_parser* parser,
6106                                          int is_attribute_list,
6107                                          bool cast_p,
6108                                          bool allow_expansion_p,
6109                                          bool *non_constant_p)
6110 {
6111   VEC(tree,gc) *expression_list;
6112   bool fold_expr_p = is_attribute_list != non_attr;
6113   tree identifier = NULL_TREE;
6114   bool saved_greater_than_is_operator_p;
6115
6116   /* Assume all the expressions will be constant.  */
6117   if (non_constant_p)
6118     *non_constant_p = false;
6119
6120   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6121     return NULL;
6122
6123   expression_list = make_tree_vector ();
6124
6125   /* Within a parenthesized expression, a `>' token is always
6126      the greater-than operator.  */
6127   saved_greater_than_is_operator_p
6128     = parser->greater_than_is_operator_p;
6129   parser->greater_than_is_operator_p = true;
6130
6131   /* Consume expressions until there are no more.  */
6132   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6133     while (true)
6134       {
6135         tree expr;
6136
6137         /* At the beginning of attribute lists, check to see if the
6138            next token is an identifier.  */
6139         if (is_attribute_list == id_attr
6140             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6141           {
6142             cp_token *token;
6143
6144             /* Consume the identifier.  */
6145             token = cp_lexer_consume_token (parser->lexer);
6146             /* Save the identifier.  */
6147             identifier = token->u.value;
6148           }
6149         else
6150           {
6151             bool expr_non_constant_p;
6152
6153             /* Parse the next assignment-expression.  */
6154             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6155               {
6156                 /* A braced-init-list.  */
6157                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6158                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6159                 if (non_constant_p && expr_non_constant_p)
6160                   *non_constant_p = true;
6161               }
6162             else if (non_constant_p)
6163               {
6164                 expr = (cp_parser_constant_expression
6165                         (parser, /*allow_non_constant_p=*/true,
6166                          &expr_non_constant_p));
6167                 if (expr_non_constant_p)
6168                   *non_constant_p = true;
6169               }
6170             else
6171               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6172
6173             if (fold_expr_p)
6174               expr = fold_non_dependent_expr (expr);
6175
6176             /* If we have an ellipsis, then this is an expression
6177                expansion.  */
6178             if (allow_expansion_p
6179                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6180               {
6181                 /* Consume the `...'.  */
6182                 cp_lexer_consume_token (parser->lexer);
6183
6184                 /* Build the argument pack.  */
6185                 expr = make_pack_expansion (expr);
6186               }
6187
6188              /* Add it to the list.  We add error_mark_node
6189                 expressions to the list, so that we can still tell if
6190                 the correct form for a parenthesized expression-list
6191                 is found. That gives better errors.  */
6192             VEC_safe_push (tree, gc, expression_list, expr);
6193
6194             if (expr == error_mark_node)
6195               goto skip_comma;
6196           }
6197
6198         /* After the first item, attribute lists look the same as
6199            expression lists.  */
6200         is_attribute_list = non_attr;
6201
6202       get_comma:;
6203         /* If the next token isn't a `,', then we are done.  */
6204         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6205           break;
6206
6207         /* Otherwise, consume the `,' and keep going.  */
6208         cp_lexer_consume_token (parser->lexer);
6209       }
6210
6211   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6212     {
6213       int ending;
6214
6215     skip_comma:;
6216       /* We try and resync to an unnested comma, as that will give the
6217          user better diagnostics.  */
6218       ending = cp_parser_skip_to_closing_parenthesis (parser,
6219                                                       /*recovering=*/true,
6220                                                       /*or_comma=*/true,
6221                                                       /*consume_paren=*/true);
6222       if (ending < 0)
6223         goto get_comma;
6224       if (!ending)
6225         {
6226           parser->greater_than_is_operator_p
6227             = saved_greater_than_is_operator_p;
6228           return NULL;
6229         }
6230     }
6231
6232   parser->greater_than_is_operator_p
6233     = saved_greater_than_is_operator_p;
6234
6235   if (identifier)
6236     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6237
6238   return expression_list;
6239 }
6240
6241 /* Parse a pseudo-destructor-name.
6242
6243    pseudo-destructor-name:
6244      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6245      :: [opt] nested-name-specifier template template-id :: ~ type-name
6246      :: [opt] nested-name-specifier [opt] ~ type-name
6247
6248    If either of the first two productions is used, sets *SCOPE to the
6249    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6250    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6251    or ERROR_MARK_NODE if the parse fails.  */
6252
6253 static void
6254 cp_parser_pseudo_destructor_name (cp_parser* parser,
6255                                   tree* scope,
6256                                   tree* type)
6257 {
6258   bool nested_name_specifier_p;
6259
6260   /* Assume that things will not work out.  */
6261   *type = error_mark_node;
6262
6263   /* Look for the optional `::' operator.  */
6264   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6265   /* Look for the optional nested-name-specifier.  */
6266   nested_name_specifier_p
6267     = (cp_parser_nested_name_specifier_opt (parser,
6268                                             /*typename_keyword_p=*/false,
6269                                             /*check_dependency_p=*/true,
6270                                             /*type_p=*/false,
6271                                             /*is_declaration=*/false)
6272        != NULL_TREE);
6273   /* Now, if we saw a nested-name-specifier, we might be doing the
6274      second production.  */
6275   if (nested_name_specifier_p
6276       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6277     {
6278       /* Consume the `template' keyword.  */
6279       cp_lexer_consume_token (parser->lexer);
6280       /* Parse the template-id.  */
6281       cp_parser_template_id (parser,
6282                              /*template_keyword_p=*/true,
6283                              /*check_dependency_p=*/false,
6284                              /*is_declaration=*/true);
6285       /* Look for the `::' token.  */
6286       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6287     }
6288   /* If the next token is not a `~', then there might be some
6289      additional qualification.  */
6290   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6291     {
6292       /* At this point, we're looking for "type-name :: ~".  The type-name
6293          must not be a class-name, since this is a pseudo-destructor.  So,
6294          it must be either an enum-name, or a typedef-name -- both of which
6295          are just identifiers.  So, we peek ahead to check that the "::"
6296          and "~" tokens are present; if they are not, then we can avoid
6297          calling type_name.  */
6298       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6299           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6300           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6301         {
6302           cp_parser_error (parser, "non-scalar type");
6303           return;
6304         }
6305
6306       /* Look for the type-name.  */
6307       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6308       if (*scope == error_mark_node)
6309         return;
6310
6311       /* Look for the `::' token.  */
6312       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6313     }
6314   else
6315     *scope = NULL_TREE;
6316
6317   /* Look for the `~'.  */
6318   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6319
6320   /* Once we see the ~, this has to be a pseudo-destructor.  */
6321   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6322     cp_parser_commit_to_tentative_parse (parser);
6323
6324   /* Look for the type-name again.  We are not responsible for
6325      checking that it matches the first type-name.  */
6326   *type = cp_parser_nonclass_name (parser);
6327 }
6328
6329 /* Parse a unary-expression.
6330
6331    unary-expression:
6332      postfix-expression
6333      ++ cast-expression
6334      -- cast-expression
6335      unary-operator cast-expression
6336      sizeof unary-expression
6337      sizeof ( type-id )
6338      alignof ( type-id )  [C++0x]
6339      new-expression
6340      delete-expression
6341
6342    GNU Extensions:
6343
6344    unary-expression:
6345      __extension__ cast-expression
6346      __alignof__ unary-expression
6347      __alignof__ ( type-id )
6348      alignof unary-expression  [C++0x]
6349      __real__ cast-expression
6350      __imag__ cast-expression
6351      && identifier
6352
6353    ADDRESS_P is true iff the unary-expression is appearing as the
6354    operand of the `&' operator.   CAST_P is true if this expression is
6355    the target of a cast.
6356
6357    Returns a representation of the expression.  */
6358
6359 static tree
6360 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6361                             cp_id_kind * pidk)
6362 {
6363   cp_token *token;
6364   enum tree_code unary_operator;
6365
6366   /* Peek at the next token.  */
6367   token = cp_lexer_peek_token (parser->lexer);
6368   /* Some keywords give away the kind of expression.  */
6369   if (token->type == CPP_KEYWORD)
6370     {
6371       enum rid keyword = token->keyword;
6372
6373       switch (keyword)
6374         {
6375         case RID_ALIGNOF:
6376         case RID_SIZEOF:
6377           {
6378             tree operand;
6379             enum tree_code op;
6380
6381             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6382             /* Consume the token.  */
6383             cp_lexer_consume_token (parser->lexer);
6384             /* Parse the operand.  */
6385             operand = cp_parser_sizeof_operand (parser, keyword);
6386
6387             if (TYPE_P (operand))
6388               return cxx_sizeof_or_alignof_type (operand, op, true);
6389             else
6390               {
6391                 /* ISO C++ defines alignof only with types, not with
6392                    expressions. So pedwarn if alignof is used with a non-
6393                    type expression. However, __alignof__ is ok.  */
6394                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6395                   pedwarn (token->location, OPT_pedantic,
6396                            "ISO C++ does not allow %<alignof%> "
6397                            "with a non-type");
6398
6399                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6400               }
6401           }
6402
6403         case RID_NEW:
6404           return cp_parser_new_expression (parser);
6405
6406         case RID_DELETE:
6407           return cp_parser_delete_expression (parser);
6408
6409         case RID_EXTENSION:
6410           {
6411             /* The saved value of the PEDANTIC flag.  */
6412             int saved_pedantic;
6413             tree expr;
6414
6415             /* Save away the PEDANTIC flag.  */
6416             cp_parser_extension_opt (parser, &saved_pedantic);
6417             /* Parse the cast-expression.  */
6418             expr = cp_parser_simple_cast_expression (parser);
6419             /* Restore the PEDANTIC flag.  */
6420             pedantic = saved_pedantic;
6421
6422             return expr;
6423           }
6424
6425         case RID_REALPART:
6426         case RID_IMAGPART:
6427           {
6428             tree expression;
6429
6430             /* Consume the `__real__' or `__imag__' token.  */
6431             cp_lexer_consume_token (parser->lexer);
6432             /* Parse the cast-expression.  */
6433             expression = cp_parser_simple_cast_expression (parser);
6434             /* Create the complete representation.  */
6435             return build_x_unary_op ((keyword == RID_REALPART
6436                                       ? REALPART_EXPR : IMAGPART_EXPR),
6437                                      expression,
6438                                      tf_warning_or_error);
6439           }
6440           break;
6441
6442         case RID_TRANSACTION_ATOMIC:
6443         case RID_TRANSACTION_RELAXED:
6444           return cp_parser_transaction_expression (parser, keyword);
6445
6446         case RID_NOEXCEPT:
6447           {
6448             tree expr;
6449             const char *saved_message;
6450             bool saved_integral_constant_expression_p;
6451             bool saved_non_integral_constant_expression_p;
6452             bool saved_greater_than_is_operator_p;
6453
6454             cp_lexer_consume_token (parser->lexer);
6455             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6456
6457             saved_message = parser->type_definition_forbidden_message;
6458             parser->type_definition_forbidden_message
6459               = G_("types may not be defined in %<noexcept%> expressions");
6460
6461             saved_integral_constant_expression_p
6462               = parser->integral_constant_expression_p;
6463             saved_non_integral_constant_expression_p
6464               = parser->non_integral_constant_expression_p;
6465             parser->integral_constant_expression_p = false;
6466
6467             saved_greater_than_is_operator_p
6468               = parser->greater_than_is_operator_p;
6469             parser->greater_than_is_operator_p = true;
6470
6471             ++cp_unevaluated_operand;
6472             ++c_inhibit_evaluation_warnings;
6473             expr = cp_parser_expression (parser, false, NULL);
6474             --c_inhibit_evaluation_warnings;
6475             --cp_unevaluated_operand;
6476
6477             parser->greater_than_is_operator_p
6478               = saved_greater_than_is_operator_p;
6479
6480             parser->integral_constant_expression_p
6481               = saved_integral_constant_expression_p;
6482             parser->non_integral_constant_expression_p
6483               = saved_non_integral_constant_expression_p;
6484
6485             parser->type_definition_forbidden_message = saved_message;
6486
6487             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6488             return finish_noexcept_expr (expr, tf_warning_or_error);
6489           }
6490
6491         default:
6492           break;
6493         }
6494     }
6495
6496   /* Look for the `:: new' and `:: delete', which also signal the
6497      beginning of a new-expression, or delete-expression,
6498      respectively.  If the next token is `::', then it might be one of
6499      these.  */
6500   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6501     {
6502       enum rid keyword;
6503
6504       /* See if the token after the `::' is one of the keywords in
6505          which we're interested.  */
6506       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6507       /* If it's `new', we have a new-expression.  */
6508       if (keyword == RID_NEW)
6509         return cp_parser_new_expression (parser);
6510       /* Similarly, for `delete'.  */
6511       else if (keyword == RID_DELETE)
6512         return cp_parser_delete_expression (parser);
6513     }
6514
6515   /* Look for a unary operator.  */
6516   unary_operator = cp_parser_unary_operator (token);
6517   /* The `++' and `--' operators can be handled similarly, even though
6518      they are not technically unary-operators in the grammar.  */
6519   if (unary_operator == ERROR_MARK)
6520     {
6521       if (token->type == CPP_PLUS_PLUS)
6522         unary_operator = PREINCREMENT_EXPR;
6523       else if (token->type == CPP_MINUS_MINUS)
6524         unary_operator = PREDECREMENT_EXPR;
6525       /* Handle the GNU address-of-label extension.  */
6526       else if (cp_parser_allow_gnu_extensions_p (parser)
6527                && token->type == CPP_AND_AND)
6528         {
6529           tree identifier;
6530           tree expression;
6531           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6532
6533           /* Consume the '&&' token.  */
6534           cp_lexer_consume_token (parser->lexer);
6535           /* Look for the identifier.  */
6536           identifier = cp_parser_identifier (parser);
6537           /* Create an expression representing the address.  */
6538           expression = finish_label_address_expr (identifier, loc);
6539           if (cp_parser_non_integral_constant_expression (parser,
6540                                                           NIC_ADDR_LABEL))
6541             expression = error_mark_node;
6542           return expression;
6543         }
6544     }
6545   if (unary_operator != ERROR_MARK)
6546     {
6547       tree cast_expression;
6548       tree expression = error_mark_node;
6549       non_integral_constant non_constant_p = NIC_NONE;
6550
6551       /* Consume the operator token.  */
6552       token = cp_lexer_consume_token (parser->lexer);
6553       /* Parse the cast-expression.  */
6554       cast_expression
6555         = cp_parser_cast_expression (parser,
6556                                      unary_operator == ADDR_EXPR,
6557                                      /*cast_p=*/false, pidk);
6558       /* Now, build an appropriate representation.  */
6559       switch (unary_operator)
6560         {
6561         case INDIRECT_REF:
6562           non_constant_p = NIC_STAR;
6563           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6564                                              tf_warning_or_error);
6565           break;
6566
6567         case ADDR_EXPR:
6568            non_constant_p = NIC_ADDR;
6569           /* Fall through.  */
6570         case BIT_NOT_EXPR:
6571           expression = build_x_unary_op (unary_operator, cast_expression,
6572                                          tf_warning_or_error);
6573           break;
6574
6575         case PREINCREMENT_EXPR:
6576         case PREDECREMENT_EXPR:
6577           non_constant_p = unary_operator == PREINCREMENT_EXPR
6578                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6579           /* Fall through.  */
6580         case UNARY_PLUS_EXPR:
6581         case NEGATE_EXPR:
6582         case TRUTH_NOT_EXPR:
6583           expression = finish_unary_op_expr (unary_operator, cast_expression);
6584           break;
6585
6586         default:
6587           gcc_unreachable ();
6588         }
6589
6590       if (non_constant_p != NIC_NONE
6591           && cp_parser_non_integral_constant_expression (parser,
6592                                                          non_constant_p))
6593         expression = error_mark_node;
6594
6595       return expression;
6596     }
6597
6598   return cp_parser_postfix_expression (parser, address_p, cast_p,
6599                                        /*member_access_only_p=*/false,
6600                                        pidk);
6601 }
6602
6603 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6604    unary-operator, the corresponding tree code is returned.  */
6605
6606 static enum tree_code
6607 cp_parser_unary_operator (cp_token* token)
6608 {
6609   switch (token->type)
6610     {
6611     case CPP_MULT:
6612       return INDIRECT_REF;
6613
6614     case CPP_AND:
6615       return ADDR_EXPR;
6616
6617     case CPP_PLUS:
6618       return UNARY_PLUS_EXPR;
6619
6620     case CPP_MINUS:
6621       return NEGATE_EXPR;
6622
6623     case CPP_NOT:
6624       return TRUTH_NOT_EXPR;
6625
6626     case CPP_COMPL:
6627       return BIT_NOT_EXPR;
6628
6629     default:
6630       return ERROR_MARK;
6631     }
6632 }
6633
6634 /* Parse a new-expression.
6635
6636    new-expression:
6637      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6638      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6639
6640    Returns a representation of the expression.  */
6641
6642 static tree
6643 cp_parser_new_expression (cp_parser* parser)
6644 {
6645   bool global_scope_p;
6646   VEC(tree,gc) *placement;
6647   tree type;
6648   VEC(tree,gc) *initializer;
6649   tree nelts;
6650   tree ret;
6651
6652   /* Look for the optional `::' operator.  */
6653   global_scope_p
6654     = (cp_parser_global_scope_opt (parser,
6655                                    /*current_scope_valid_p=*/false)
6656        != NULL_TREE);
6657   /* Look for the `new' operator.  */
6658   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6659   /* There's no easy way to tell a new-placement from the
6660      `( type-id )' construct.  */
6661   cp_parser_parse_tentatively (parser);
6662   /* Look for a new-placement.  */
6663   placement = cp_parser_new_placement (parser);
6664   /* If that didn't work out, there's no new-placement.  */
6665   if (!cp_parser_parse_definitely (parser))
6666     {
6667       if (placement != NULL)
6668         release_tree_vector (placement);
6669       placement = NULL;
6670     }
6671
6672   /* If the next token is a `(', then we have a parenthesized
6673      type-id.  */
6674   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6675     {
6676       cp_token *token;
6677       const char *saved_message = parser->type_definition_forbidden_message;
6678
6679       /* Consume the `('.  */
6680       cp_lexer_consume_token (parser->lexer);
6681
6682       /* Parse the type-id.  */
6683       parser->type_definition_forbidden_message
6684         = G_("types may not be defined in a new-expression");
6685       type = cp_parser_type_id (parser);
6686       parser->type_definition_forbidden_message = saved_message;
6687
6688       /* Look for the closing `)'.  */
6689       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6690       token = cp_lexer_peek_token (parser->lexer);
6691       /* There should not be a direct-new-declarator in this production,
6692          but GCC used to allowed this, so we check and emit a sensible error
6693          message for this case.  */
6694       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6695         {
6696           error_at (token->location,
6697                     "array bound forbidden after parenthesized type-id");
6698           inform (token->location, 
6699                   "try removing the parentheses around the type-id");
6700           cp_parser_direct_new_declarator (parser);
6701         }
6702       nelts = NULL_TREE;
6703     }
6704   /* Otherwise, there must be a new-type-id.  */
6705   else
6706     type = cp_parser_new_type_id (parser, &nelts);
6707
6708   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6709   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6710       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6711     initializer = cp_parser_new_initializer (parser);
6712   else
6713     initializer = NULL;
6714
6715   /* A new-expression may not appear in an integral constant
6716      expression.  */
6717   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6718     ret = error_mark_node;
6719   else
6720     {
6721       /* Create a representation of the new-expression.  */
6722       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6723                        tf_warning_or_error);
6724     }
6725
6726   if (placement != NULL)
6727     release_tree_vector (placement);
6728   if (initializer != NULL)
6729     release_tree_vector (initializer);
6730
6731   return ret;
6732 }
6733
6734 /* Parse a new-placement.
6735
6736    new-placement:
6737      ( expression-list )
6738
6739    Returns the same representation as for an expression-list.  */
6740
6741 static VEC(tree,gc) *
6742 cp_parser_new_placement (cp_parser* parser)
6743 {
6744   VEC(tree,gc) *expression_list;
6745
6746   /* Parse the expression-list.  */
6747   expression_list = (cp_parser_parenthesized_expression_list
6748                      (parser, non_attr, /*cast_p=*/false,
6749                       /*allow_expansion_p=*/true,
6750                       /*non_constant_p=*/NULL));
6751
6752   return expression_list;
6753 }
6754
6755 /* Parse a new-type-id.
6756
6757    new-type-id:
6758      type-specifier-seq new-declarator [opt]
6759
6760    Returns the TYPE allocated.  If the new-type-id indicates an array
6761    type, *NELTS is set to the number of elements in the last array
6762    bound; the TYPE will not include the last array bound.  */
6763
6764 static tree
6765 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6766 {
6767   cp_decl_specifier_seq type_specifier_seq;
6768   cp_declarator *new_declarator;
6769   cp_declarator *declarator;
6770   cp_declarator *outer_declarator;
6771   const char *saved_message;
6772   tree type;
6773
6774   /* The type-specifier sequence must not contain type definitions.
6775      (It cannot contain declarations of new types either, but if they
6776      are not definitions we will catch that because they are not
6777      complete.)  */
6778   saved_message = parser->type_definition_forbidden_message;
6779   parser->type_definition_forbidden_message
6780     = G_("types may not be defined in a new-type-id");
6781   /* Parse the type-specifier-seq.  */
6782   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6783                                 /*is_trailing_return=*/false,
6784                                 &type_specifier_seq);
6785   /* Restore the old message.  */
6786   parser->type_definition_forbidden_message = saved_message;
6787   /* Parse the new-declarator.  */
6788   new_declarator = cp_parser_new_declarator_opt (parser);
6789
6790   /* Determine the number of elements in the last array dimension, if
6791      any.  */
6792   *nelts = NULL_TREE;
6793   /* Skip down to the last array dimension.  */
6794   declarator = new_declarator;
6795   outer_declarator = NULL;
6796   while (declarator && (declarator->kind == cdk_pointer
6797                         || declarator->kind == cdk_ptrmem))
6798     {
6799       outer_declarator = declarator;
6800       declarator = declarator->declarator;
6801     }
6802   while (declarator
6803          && declarator->kind == cdk_array
6804          && declarator->declarator
6805          && declarator->declarator->kind == cdk_array)
6806     {
6807       outer_declarator = declarator;
6808       declarator = declarator->declarator;
6809     }
6810
6811   if (declarator && declarator->kind == cdk_array)
6812     {
6813       *nelts = declarator->u.array.bounds;
6814       if (*nelts == error_mark_node)
6815         *nelts = integer_one_node;
6816
6817       if (outer_declarator)
6818         outer_declarator->declarator = declarator->declarator;
6819       else
6820         new_declarator = NULL;
6821     }
6822
6823   type = groktypename (&type_specifier_seq, new_declarator, false);
6824   return type;
6825 }
6826
6827 /* Parse an (optional) new-declarator.
6828
6829    new-declarator:
6830      ptr-operator new-declarator [opt]
6831      direct-new-declarator
6832
6833    Returns the declarator.  */
6834
6835 static cp_declarator *
6836 cp_parser_new_declarator_opt (cp_parser* parser)
6837 {
6838   enum tree_code code;
6839   tree type;
6840   cp_cv_quals cv_quals;
6841
6842   /* We don't know if there's a ptr-operator next, or not.  */
6843   cp_parser_parse_tentatively (parser);
6844   /* Look for a ptr-operator.  */
6845   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6846   /* If that worked, look for more new-declarators.  */
6847   if (cp_parser_parse_definitely (parser))
6848     {
6849       cp_declarator *declarator;
6850
6851       /* Parse another optional declarator.  */
6852       declarator = cp_parser_new_declarator_opt (parser);
6853
6854       return cp_parser_make_indirect_declarator
6855         (code, type, cv_quals, declarator);
6856     }
6857
6858   /* If the next token is a `[', there is a direct-new-declarator.  */
6859   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6860     return cp_parser_direct_new_declarator (parser);
6861
6862   return NULL;
6863 }
6864
6865 /* Parse a direct-new-declarator.
6866
6867    direct-new-declarator:
6868      [ expression ]
6869      direct-new-declarator [constant-expression]
6870
6871    */
6872
6873 static cp_declarator *
6874 cp_parser_direct_new_declarator (cp_parser* parser)
6875 {
6876   cp_declarator *declarator = NULL;
6877
6878   while (true)
6879     {
6880       tree expression;
6881
6882       /* Look for the opening `['.  */
6883       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6884       /* The first expression is not required to be constant.  */
6885       if (!declarator)
6886         {
6887           cp_token *token = cp_lexer_peek_token (parser->lexer);
6888           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6889           /* The standard requires that the expression have integral
6890              type.  DR 74 adds enumeration types.  We believe that the
6891              real intent is that these expressions be handled like the
6892              expression in a `switch' condition, which also allows
6893              classes with a single conversion to integral or
6894              enumeration type.  */
6895           if (!processing_template_decl)
6896             {
6897               expression
6898                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6899                                               expression,
6900                                               /*complain=*/true);
6901               if (!expression)
6902                 {
6903                   error_at (token->location,
6904                             "expression in new-declarator must have integral "
6905                             "or enumeration type");
6906                   expression = error_mark_node;
6907                 }
6908             }
6909         }
6910       /* But all the other expressions must be.  */
6911       else
6912         expression
6913           = cp_parser_constant_expression (parser,
6914                                            /*allow_non_constant=*/false,
6915                                            NULL);
6916       /* Look for the closing `]'.  */
6917       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6918
6919       /* Add this bound to the declarator.  */
6920       declarator = make_array_declarator (declarator, expression);
6921
6922       /* If the next token is not a `[', then there are no more
6923          bounds.  */
6924       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6925         break;
6926     }
6927
6928   return declarator;
6929 }
6930
6931 /* Parse a new-initializer.
6932
6933    new-initializer:
6934      ( expression-list [opt] )
6935      braced-init-list
6936
6937    Returns a representation of the expression-list.  */
6938
6939 static VEC(tree,gc) *
6940 cp_parser_new_initializer (cp_parser* parser)
6941 {
6942   VEC(tree,gc) *expression_list;
6943
6944   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6945     {
6946       tree t;
6947       bool expr_non_constant_p;
6948       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6949       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6950       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6951       expression_list = make_tree_vector_single (t);
6952     }
6953   else
6954     expression_list = (cp_parser_parenthesized_expression_list
6955                        (parser, non_attr, /*cast_p=*/false,
6956                         /*allow_expansion_p=*/true,
6957                         /*non_constant_p=*/NULL));
6958
6959   return expression_list;
6960 }
6961
6962 /* Parse a delete-expression.
6963
6964    delete-expression:
6965      :: [opt] delete cast-expression
6966      :: [opt] delete [ ] cast-expression
6967
6968    Returns a representation of the expression.  */
6969
6970 static tree
6971 cp_parser_delete_expression (cp_parser* parser)
6972 {
6973   bool global_scope_p;
6974   bool array_p;
6975   tree expression;
6976
6977   /* Look for the optional `::' operator.  */
6978   global_scope_p
6979     = (cp_parser_global_scope_opt (parser,
6980                                    /*current_scope_valid_p=*/false)
6981        != NULL_TREE);
6982   /* Look for the `delete' keyword.  */
6983   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6984   /* See if the array syntax is in use.  */
6985   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6986     {
6987       /* Consume the `[' token.  */
6988       cp_lexer_consume_token (parser->lexer);
6989       /* Look for the `]' token.  */
6990       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6991       /* Remember that this is the `[]' construct.  */
6992       array_p = true;
6993     }
6994   else
6995     array_p = false;
6996
6997   /* Parse the cast-expression.  */
6998   expression = cp_parser_simple_cast_expression (parser);
6999
7000   /* A delete-expression may not appear in an integral constant
7001      expression.  */
7002   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
7003     return error_mark_node;
7004
7005   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
7006                         tf_warning_or_error);
7007 }
7008
7009 /* Returns true if TOKEN may start a cast-expression and false
7010    otherwise.  */
7011
7012 static bool
7013 cp_parser_token_starts_cast_expression (cp_token *token)
7014 {
7015   switch (token->type)
7016     {
7017     case CPP_COMMA:
7018     case CPP_SEMICOLON:
7019     case CPP_QUERY:
7020     case CPP_COLON:
7021     case CPP_CLOSE_SQUARE:
7022     case CPP_CLOSE_PAREN:
7023     case CPP_CLOSE_BRACE:
7024     case CPP_DOT:
7025     case CPP_DOT_STAR:
7026     case CPP_DEREF:
7027     case CPP_DEREF_STAR:
7028     case CPP_DIV:
7029     case CPP_MOD:
7030     case CPP_LSHIFT:
7031     case CPP_RSHIFT:
7032     case CPP_LESS:
7033     case CPP_GREATER:
7034     case CPP_LESS_EQ:
7035     case CPP_GREATER_EQ:
7036     case CPP_EQ_EQ:
7037     case CPP_NOT_EQ:
7038     case CPP_EQ:
7039     case CPP_MULT_EQ:
7040     case CPP_DIV_EQ:
7041     case CPP_MOD_EQ:
7042     case CPP_PLUS_EQ:
7043     case CPP_MINUS_EQ:
7044     case CPP_RSHIFT_EQ:
7045     case CPP_LSHIFT_EQ:
7046     case CPP_AND_EQ:
7047     case CPP_XOR_EQ:
7048     case CPP_OR_EQ:
7049     case CPP_XOR:
7050     case CPP_OR:
7051     case CPP_OR_OR:
7052     case CPP_EOF:
7053       return false;
7054
7055       /* '[' may start a primary-expression in obj-c++.  */
7056     case CPP_OPEN_SQUARE:
7057       return c_dialect_objc ();
7058
7059     default:
7060       return true;
7061     }
7062 }
7063
7064 /* Parse a cast-expression.
7065
7066    cast-expression:
7067      unary-expression
7068      ( type-id ) cast-expression
7069
7070    ADDRESS_P is true iff the unary-expression is appearing as the
7071    operand of the `&' operator.   CAST_P is true if this expression is
7072    the target of a cast.
7073
7074    Returns a representation of the expression.  */
7075
7076 static tree
7077 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7078                            cp_id_kind * pidk)
7079 {
7080   /* If it's a `(', then we might be looking at a cast.  */
7081   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7082     {
7083       tree type = NULL_TREE;
7084       tree expr = NULL_TREE;
7085       bool compound_literal_p;
7086       const char *saved_message;
7087
7088       /* There's no way to know yet whether or not this is a cast.
7089          For example, `(int (3))' is a unary-expression, while `(int)
7090          3' is a cast.  So, we resort to parsing tentatively.  */
7091       cp_parser_parse_tentatively (parser);
7092       /* Types may not be defined in a cast.  */
7093       saved_message = parser->type_definition_forbidden_message;
7094       parser->type_definition_forbidden_message
7095         = G_("types may not be defined in casts");
7096       /* Consume the `('.  */
7097       cp_lexer_consume_token (parser->lexer);
7098       /* A very tricky bit is that `(struct S) { 3 }' is a
7099          compound-literal (which we permit in C++ as an extension).
7100          But, that construct is not a cast-expression -- it is a
7101          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7102          is legal; if the compound-literal were a cast-expression,
7103          you'd need an extra set of parentheses.)  But, if we parse
7104          the type-id, and it happens to be a class-specifier, then we
7105          will commit to the parse at that point, because we cannot
7106          undo the action that is done when creating a new class.  So,
7107          then we cannot back up and do a postfix-expression.
7108
7109          Therefore, we scan ahead to the closing `)', and check to see
7110          if the token after the `)' is a `{'.  If so, we are not
7111          looking at a cast-expression.
7112
7113          Save tokens so that we can put them back.  */
7114       cp_lexer_save_tokens (parser->lexer);
7115       /* Skip tokens until the next token is a closing parenthesis.
7116          If we find the closing `)', and the next token is a `{', then
7117          we are looking at a compound-literal.  */
7118       compound_literal_p
7119         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7120                                                   /*consume_paren=*/true)
7121            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7122       /* Roll back the tokens we skipped.  */
7123       cp_lexer_rollback_tokens (parser->lexer);
7124       /* If we were looking at a compound-literal, simulate an error
7125          so that the call to cp_parser_parse_definitely below will
7126          fail.  */
7127       if (compound_literal_p)
7128         cp_parser_simulate_error (parser);
7129       else
7130         {
7131           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7132           parser->in_type_id_in_expr_p = true;
7133           /* Look for the type-id.  */
7134           type = cp_parser_type_id (parser);
7135           /* Look for the closing `)'.  */
7136           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7137           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7138         }
7139
7140       /* Restore the saved message.  */
7141       parser->type_definition_forbidden_message = saved_message;
7142
7143       /* At this point this can only be either a cast or a
7144          parenthesized ctor such as `(T ())' that looks like a cast to
7145          function returning T.  */
7146       if (!cp_parser_error_occurred (parser)
7147           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7148                                                      (parser->lexer)))
7149         {
7150           cp_parser_parse_definitely (parser);
7151           expr = cp_parser_cast_expression (parser,
7152                                             /*address_p=*/false,
7153                                             /*cast_p=*/true, pidk);
7154
7155           /* Warn about old-style casts, if so requested.  */
7156           if (warn_old_style_cast
7157               && !in_system_header
7158               && !VOID_TYPE_P (type)
7159               && current_lang_name != lang_name_c)
7160             warning (OPT_Wold_style_cast, "use of old-style cast");
7161
7162           /* Only type conversions to integral or enumeration types
7163              can be used in constant-expressions.  */
7164           if (!cast_valid_in_integral_constant_expression_p (type)
7165               && cp_parser_non_integral_constant_expression (parser,
7166                                                              NIC_CAST))
7167             return error_mark_node;
7168
7169           /* Perform the cast.  */
7170           expr = build_c_cast (input_location, type, expr);
7171           return expr;
7172         }
7173       else 
7174         cp_parser_abort_tentative_parse (parser);
7175     }
7176
7177   /* If we get here, then it's not a cast, so it must be a
7178      unary-expression.  */
7179   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7180 }
7181
7182 /* Parse a binary expression of the general form:
7183
7184    pm-expression:
7185      cast-expression
7186      pm-expression .* cast-expression
7187      pm-expression ->* cast-expression
7188
7189    multiplicative-expression:
7190      pm-expression
7191      multiplicative-expression * pm-expression
7192      multiplicative-expression / pm-expression
7193      multiplicative-expression % pm-expression
7194
7195    additive-expression:
7196      multiplicative-expression
7197      additive-expression + multiplicative-expression
7198      additive-expression - multiplicative-expression
7199
7200    shift-expression:
7201      additive-expression
7202      shift-expression << additive-expression
7203      shift-expression >> additive-expression
7204
7205    relational-expression:
7206      shift-expression
7207      relational-expression < shift-expression
7208      relational-expression > shift-expression
7209      relational-expression <= shift-expression
7210      relational-expression >= shift-expression
7211
7212   GNU Extension:
7213
7214    relational-expression:
7215      relational-expression <? shift-expression
7216      relational-expression >? shift-expression
7217
7218    equality-expression:
7219      relational-expression
7220      equality-expression == relational-expression
7221      equality-expression != relational-expression
7222
7223    and-expression:
7224      equality-expression
7225      and-expression & equality-expression
7226
7227    exclusive-or-expression:
7228      and-expression
7229      exclusive-or-expression ^ and-expression
7230
7231    inclusive-or-expression:
7232      exclusive-or-expression
7233      inclusive-or-expression | exclusive-or-expression
7234
7235    logical-and-expression:
7236      inclusive-or-expression
7237      logical-and-expression && inclusive-or-expression
7238
7239    logical-or-expression:
7240      logical-and-expression
7241      logical-or-expression || logical-and-expression
7242
7243    All these are implemented with a single function like:
7244
7245    binary-expression:
7246      simple-cast-expression
7247      binary-expression <token> binary-expression
7248
7249    CAST_P is true if this expression is the target of a cast.
7250
7251    The binops_by_token map is used to get the tree codes for each <token> type.
7252    binary-expressions are associated according to a precedence table.  */
7253
7254 #define TOKEN_PRECEDENCE(token)                              \
7255 (((token->type == CPP_GREATER                                \
7256    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7257   && !parser->greater_than_is_operator_p)                    \
7258  ? PREC_NOT_OPERATOR                                         \
7259  : binops_by_token[token->type].prec)
7260
7261 static tree
7262 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7263                              bool no_toplevel_fold_p,
7264                              enum cp_parser_prec prec,
7265                              cp_id_kind * pidk)
7266 {
7267   cp_parser_expression_stack stack;
7268   cp_parser_expression_stack_entry *sp = &stack[0];
7269   tree lhs, rhs;
7270   cp_token *token;
7271   enum tree_code tree_type, lhs_type, rhs_type;
7272   enum cp_parser_prec new_prec, lookahead_prec;
7273   tree overload;
7274
7275   /* Parse the first expression.  */
7276   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7277   lhs_type = ERROR_MARK;
7278
7279   if (cp_parser_error_occurred (parser))
7280     return error_mark_node;
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_pedantic, 
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_pedantic,
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_pedantic,
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, but we can use void
8422          as a placeholder return type anyways.  */
8423       return_type_specs.type = void_type_node;
8424
8425     p = obstack_alloc (&declarator_obstack, 0);
8426
8427     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8428                                      sfk_none);
8429
8430     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8431              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8432     declarator = make_call_declarator (declarator, param_list, quals,
8433                                        VIRT_SPEC_UNSPECIFIED,
8434                                        exception_spec,
8435                                        /*late_return_type=*/NULL_TREE);
8436     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8437
8438     fco = grokmethod (&return_type_specs,
8439                       declarator,
8440                       attributes);
8441     if (fco != error_mark_node)
8442       {
8443         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8444         DECL_ARTIFICIAL (fco) = 1;
8445         /* Give the object parameter a different name.  */
8446         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8447       }
8448
8449     finish_member_declaration (fco);
8450
8451     obstack_free (&declarator_obstack, p);
8452
8453     return (fco != error_mark_node);
8454   }
8455 }
8456
8457 /* Parse the body of a lambda expression, which is simply
8458
8459    compound-statement
8460
8461    but which requires special handling.
8462    LAMBDA_EXPR is the current representation of the lambda expression.  */
8463
8464 static void
8465 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8466 {
8467   bool nested = (current_function_decl != NULL_TREE);
8468   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8469   if (nested)
8470     push_function_context ();
8471   else
8472     /* Still increment function_depth so that we don't GC in the
8473        middle of an expression.  */
8474     ++function_depth;
8475   /* Clear this in case we're in the middle of a default argument.  */
8476   parser->local_variables_forbidden_p = false;
8477
8478   /* Finish the function call operator
8479      - class_specifier
8480      + late_parsing_for_member
8481      + function_definition_after_declarator
8482      + ctor_initializer_opt_and_function_body  */
8483   {
8484     tree fco = lambda_function (lambda_expr);
8485     tree body;
8486     bool done = false;
8487     tree compound_stmt;
8488     tree cap;
8489
8490     /* Let the front end know that we are going to be defining this
8491        function.  */
8492     start_preparsed_function (fco,
8493                               NULL_TREE,
8494                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8495
8496     start_lambda_scope (fco);
8497     body = begin_function_body ();
8498
8499     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8500       goto out;
8501
8502     /* Push the proxies for any explicit captures.  */
8503     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8504          cap = TREE_CHAIN (cap))
8505       build_capture_proxy (TREE_PURPOSE (cap));
8506
8507     compound_stmt = begin_compound_stmt (0);
8508
8509     /* 5.1.1.4 of the standard says:
8510          If a lambda-expression does not include a trailing-return-type, it
8511          is as if the trailing-return-type denotes the following type:
8512           * if the compound-statement is of the form
8513                { return attribute-specifier [opt] expression ; }
8514              the type of the returned expression after lvalue-to-rvalue
8515              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8516              (_conv.array_ 4.2), and function-to-pointer conversion
8517              (_conv.func_ 4.3);
8518           * otherwise, void.  */
8519
8520     /* In a lambda that has neither a lambda-return-type-clause
8521        nor a deducible form, errors should be reported for return statements
8522        in the body.  Since we used void as the placeholder return type, parsing
8523        the body as usual will give such desired behavior.  */
8524     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8525         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8526         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8527       {
8528         tree expr = NULL_TREE;
8529         cp_id_kind idk = CP_ID_KIND_NONE;
8530
8531         /* Parse tentatively in case there's more after the initial return
8532            statement.  */
8533         cp_parser_parse_tentatively (parser);
8534
8535         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8536
8537         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8538
8539         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8540         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8541
8542         if (cp_parser_parse_definitely (parser))
8543           {
8544             apply_lambda_return_type (lambda_expr, 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         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8556           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8557         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8558           cp_parser_label_declaration (parser);
8559         cp_parser_statement_seq_opt (parser, NULL_TREE);
8560         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8561         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8562       }
8563
8564     finish_compound_stmt (compound_stmt);
8565
8566   out:
8567     finish_function_body (body);
8568     finish_lambda_scope ();
8569
8570     /* Finish the function and generate code for it if necessary.  */
8571     expand_or_defer_fn (finish_function (/*inline*/2));
8572   }
8573
8574   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8575   if (nested)
8576     pop_function_context();
8577   else
8578     --function_depth;
8579 }
8580
8581 /* Statements [gram.stmt.stmt]  */
8582
8583 /* Parse a statement.
8584
8585    statement:
8586      labeled-statement
8587      expression-statement
8588      compound-statement
8589      selection-statement
8590      iteration-statement
8591      jump-statement
8592      declaration-statement
8593      try-block
8594
8595   TM Extension:
8596
8597    statement:
8598      atomic-statement
8599
8600   IN_COMPOUND is true when the statement is nested inside a
8601   cp_parser_compound_statement; this matters for certain pragmas.
8602
8603   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8604   is a (possibly labeled) if statement which is not enclosed in braces
8605   and has an else clause.  This is used to implement -Wparentheses.  */
8606
8607 static void
8608 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8609                      bool in_compound, bool *if_p)
8610 {
8611   tree statement;
8612   cp_token *token;
8613   location_t statement_location;
8614
8615  restart:
8616   if (if_p != NULL)
8617     *if_p = false;
8618   /* There is no statement yet.  */
8619   statement = NULL_TREE;
8620   /* Peek at the next token.  */
8621   token = cp_lexer_peek_token (parser->lexer);
8622   /* Remember the location of the first token in the statement.  */
8623   statement_location = token->location;
8624   /* If this is a keyword, then that will often determine what kind of
8625      statement we have.  */
8626   if (token->type == CPP_KEYWORD)
8627     {
8628       enum rid keyword = token->keyword;
8629
8630       switch (keyword)
8631         {
8632         case RID_CASE:
8633         case RID_DEFAULT:
8634           /* Looks like a labeled-statement with a case label.
8635              Parse the label, and then use tail recursion to parse
8636              the statement.  */
8637           cp_parser_label_for_labeled_statement (parser);
8638           goto restart;
8639
8640         case RID_IF:
8641         case RID_SWITCH:
8642           statement = cp_parser_selection_statement (parser, if_p);
8643           break;
8644
8645         case RID_WHILE:
8646         case RID_DO:
8647         case RID_FOR:
8648           statement = cp_parser_iteration_statement (parser);
8649           break;
8650
8651         case RID_BREAK:
8652         case RID_CONTINUE:
8653         case RID_RETURN:
8654         case RID_GOTO:
8655           statement = cp_parser_jump_statement (parser);
8656           break;
8657
8658           /* Objective-C++ exception-handling constructs.  */
8659         case RID_AT_TRY:
8660         case RID_AT_CATCH:
8661         case RID_AT_FINALLY:
8662         case RID_AT_SYNCHRONIZED:
8663         case RID_AT_THROW:
8664           statement = cp_parser_objc_statement (parser);
8665           break;
8666
8667         case RID_TRY:
8668           statement = cp_parser_try_block (parser);
8669           break;
8670
8671         case RID_NAMESPACE:
8672           /* This must be a namespace alias definition.  */
8673           cp_parser_declaration_statement (parser);
8674           return;
8675           
8676         case RID_TRANSACTION_ATOMIC:
8677         case RID_TRANSACTION_RELAXED:
8678           statement = cp_parser_transaction (parser, keyword);
8679           break;
8680         case RID_TRANSACTION_CANCEL:
8681           statement = cp_parser_transaction_cancel (parser);
8682           break;
8683
8684         default:
8685           /* It might be a keyword like `int' that can start a
8686              declaration-statement.  */
8687           break;
8688         }
8689     }
8690   else if (token->type == CPP_NAME)
8691     {
8692       /* If the next token is a `:', then we are looking at a
8693          labeled-statement.  */
8694       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8695       if (token->type == CPP_COLON)
8696         {
8697           /* Looks like a labeled-statement with an ordinary label.
8698              Parse the label, and then use tail recursion to parse
8699              the statement.  */
8700           cp_parser_label_for_labeled_statement (parser);
8701           goto restart;
8702         }
8703     }
8704   /* Anything that starts with a `{' must be a compound-statement.  */
8705   else if (token->type == CPP_OPEN_BRACE)
8706     statement = cp_parser_compound_statement (parser, NULL, false, false);
8707   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8708      a statement all its own.  */
8709   else if (token->type == CPP_PRAGMA)
8710     {
8711       /* Only certain OpenMP pragmas are attached to statements, and thus
8712          are considered statements themselves.  All others are not.  In
8713          the context of a compound, accept the pragma as a "statement" and
8714          return so that we can check for a close brace.  Otherwise we
8715          require a real statement and must go back and read one.  */
8716       if (in_compound)
8717         cp_parser_pragma (parser, pragma_compound);
8718       else if (!cp_parser_pragma (parser, pragma_stmt))
8719         goto restart;
8720       return;
8721     }
8722   else if (token->type == CPP_EOF)
8723     {
8724       cp_parser_error (parser, "expected statement");
8725       return;
8726     }
8727
8728   /* Everything else must be a declaration-statement or an
8729      expression-statement.  Try for the declaration-statement
8730      first, unless we are looking at a `;', in which case we know that
8731      we have an expression-statement.  */
8732   if (!statement)
8733     {
8734       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8735         {
8736           cp_parser_parse_tentatively (parser);
8737           /* Try to parse the declaration-statement.  */
8738           cp_parser_declaration_statement (parser);
8739           /* If that worked, we're done.  */
8740           if (cp_parser_parse_definitely (parser))
8741             return;
8742         }
8743       /* Look for an expression-statement instead.  */
8744       statement = cp_parser_expression_statement (parser, in_statement_expr);
8745     }
8746
8747   /* Set the line number for the statement.  */
8748   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8749     SET_EXPR_LOCATION (statement, statement_location);
8750 }
8751
8752 /* Parse the label for a labeled-statement, i.e.
8753
8754    identifier :
8755    case constant-expression :
8756    default :
8757
8758    GNU Extension:
8759    case constant-expression ... constant-expression : statement
8760
8761    When a label is parsed without errors, the label is added to the
8762    parse tree by the finish_* functions, so this function doesn't
8763    have to return the label.  */
8764
8765 static void
8766 cp_parser_label_for_labeled_statement (cp_parser* parser)
8767 {
8768   cp_token *token;
8769   tree label = NULL_TREE;
8770   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8771
8772   /* The next token should be an identifier.  */
8773   token = cp_lexer_peek_token (parser->lexer);
8774   if (token->type != CPP_NAME
8775       && token->type != CPP_KEYWORD)
8776     {
8777       cp_parser_error (parser, "expected labeled-statement");
8778       return;
8779     }
8780
8781   parser->colon_corrects_to_scope_p = false;
8782   switch (token->keyword)
8783     {
8784     case RID_CASE:
8785       {
8786         tree expr, expr_hi;
8787         cp_token *ellipsis;
8788
8789         /* Consume the `case' token.  */
8790         cp_lexer_consume_token (parser->lexer);
8791         /* Parse the constant-expression.  */
8792         expr = cp_parser_constant_expression (parser,
8793                                               /*allow_non_constant_p=*/false,
8794                                               NULL);
8795
8796         ellipsis = cp_lexer_peek_token (parser->lexer);
8797         if (ellipsis->type == CPP_ELLIPSIS)
8798           {
8799             /* Consume the `...' token.  */
8800             cp_lexer_consume_token (parser->lexer);
8801             expr_hi =
8802               cp_parser_constant_expression (parser,
8803                                              /*allow_non_constant_p=*/false,
8804                                              NULL);
8805             /* We don't need to emit warnings here, as the common code
8806                will do this for us.  */
8807           }
8808         else
8809           expr_hi = NULL_TREE;
8810
8811         if (parser->in_switch_statement_p)
8812           finish_case_label (token->location, expr, expr_hi);
8813         else
8814           error_at (token->location,
8815                     "case label %qE not within a switch statement",
8816                     expr);
8817       }
8818       break;
8819
8820     case RID_DEFAULT:
8821       /* Consume the `default' token.  */
8822       cp_lexer_consume_token (parser->lexer);
8823
8824       if (parser->in_switch_statement_p)
8825         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8826       else
8827         error_at (token->location, "case label not within a switch statement");
8828       break;
8829
8830     default:
8831       /* Anything else must be an ordinary label.  */
8832       label = finish_label_stmt (cp_parser_identifier (parser));
8833       break;
8834     }
8835
8836   /* Require the `:' token.  */
8837   cp_parser_require (parser, CPP_COLON, RT_COLON);
8838
8839   /* An ordinary label may optionally be followed by attributes.
8840      However, this is only permitted if the attributes are then
8841      followed by a semicolon.  This is because, for backward
8842      compatibility, when parsing
8843        lab: __attribute__ ((unused)) int i;
8844      we want the attribute to attach to "i", not "lab".  */
8845   if (label != NULL_TREE
8846       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8847     {
8848       tree attrs;
8849
8850       cp_parser_parse_tentatively (parser);
8851       attrs = cp_parser_attributes_opt (parser);
8852       if (attrs == NULL_TREE
8853           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8854         cp_parser_abort_tentative_parse (parser);
8855       else if (!cp_parser_parse_definitely (parser))
8856         ;
8857       else
8858         cplus_decl_attributes (&label, attrs, 0);
8859     }
8860
8861   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8862 }
8863
8864 /* Parse an expression-statement.
8865
8866    expression-statement:
8867      expression [opt] ;
8868
8869    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8870    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8871    indicates whether this expression-statement is part of an
8872    expression statement.  */
8873
8874 static tree
8875 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8876 {
8877   tree statement = NULL_TREE;
8878   cp_token *token = cp_lexer_peek_token (parser->lexer);
8879
8880   /* If the next token is a ';', then there is no expression
8881      statement.  */
8882   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8883     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8884
8885   /* Give a helpful message for "A<T>::type t;" and the like.  */
8886   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8887       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8888     {
8889       if (TREE_CODE (statement) == SCOPE_REF)
8890         error_at (token->location, "need %<typename%> before %qE because "
8891                   "%qT is a dependent scope",
8892                   statement, TREE_OPERAND (statement, 0));
8893       else if (is_overloaded_fn (statement)
8894                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8895         {
8896           /* A::A a; */
8897           tree fn = get_first_fn (statement);
8898           error_at (token->location,
8899                     "%<%T::%D%> names the constructor, not the type",
8900                     DECL_CONTEXT (fn), DECL_NAME (fn));
8901         }
8902     }
8903
8904   /* Consume the final `;'.  */
8905   cp_parser_consume_semicolon_at_end_of_statement (parser);
8906
8907   if (in_statement_expr
8908       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8909     /* This is the final expression statement of a statement
8910        expression.  */
8911     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8912   else if (statement)
8913     statement = finish_expr_stmt (statement);
8914   else
8915     finish_stmt ();
8916
8917   return statement;
8918 }
8919
8920 /* Parse a compound-statement.
8921
8922    compound-statement:
8923      { statement-seq [opt] }
8924
8925    GNU extension:
8926
8927    compound-statement:
8928      { label-declaration-seq [opt] statement-seq [opt] }
8929
8930    label-declaration-seq:
8931      label-declaration
8932      label-declaration-seq label-declaration
8933
8934    Returns a tree representing the statement.  */
8935
8936 static tree
8937 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8938                               bool in_try, bool function_body)
8939 {
8940   tree compound_stmt;
8941
8942   /* Consume the `{'.  */
8943   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8944     return error_mark_node;
8945   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8946       && !function_body)
8947     pedwarn (input_location, OPT_pedantic,
8948              "compound-statement in constexpr function");
8949   /* Begin the compound-statement.  */
8950   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8951   /* If the next keyword is `__label__' we have a label declaration.  */
8952   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8953     cp_parser_label_declaration (parser);
8954   /* Parse an (optional) statement-seq.  */
8955   cp_parser_statement_seq_opt (parser, in_statement_expr);
8956   /* Finish the compound-statement.  */
8957   finish_compound_stmt (compound_stmt);
8958   /* Consume the `}'.  */
8959   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8960
8961   return compound_stmt;
8962 }
8963
8964 /* Parse an (optional) statement-seq.
8965
8966    statement-seq:
8967      statement
8968      statement-seq [opt] statement  */
8969
8970 static void
8971 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8972 {
8973   /* Scan statements until there aren't any more.  */
8974   while (true)
8975     {
8976       cp_token *token = cp_lexer_peek_token (parser->lexer);
8977
8978       /* If we are looking at a `}', then we have run out of
8979          statements; the same is true if we have reached the end
8980          of file, or have stumbled upon a stray '@end'.  */
8981       if (token->type == CPP_CLOSE_BRACE
8982           || token->type == CPP_EOF
8983           || token->type == CPP_PRAGMA_EOL
8984           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8985         break;
8986       
8987       /* If we are in a compound statement and find 'else' then
8988          something went wrong.  */
8989       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8990         {
8991           if (parser->in_statement & IN_IF_STMT) 
8992             break;
8993           else
8994             {
8995               token = cp_lexer_consume_token (parser->lexer);
8996               error_at (token->location, "%<else%> without a previous %<if%>");
8997             }
8998         }
8999
9000       /* Parse the statement.  */
9001       cp_parser_statement (parser, in_statement_expr, true, NULL);
9002     }
9003 }
9004
9005 /* Parse a selection-statement.
9006
9007    selection-statement:
9008      if ( condition ) statement
9009      if ( condition ) statement else statement
9010      switch ( condition ) statement
9011
9012    Returns the new IF_STMT or SWITCH_STMT.
9013
9014    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9015    is a (possibly labeled) if statement which is not enclosed in
9016    braces and has an else clause.  This is used to implement
9017    -Wparentheses.  */
9018
9019 static tree
9020 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9021 {
9022   cp_token *token;
9023   enum rid keyword;
9024
9025   if (if_p != NULL)
9026     *if_p = false;
9027
9028   /* Peek at the next token.  */
9029   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9030
9031   /* See what kind of keyword it is.  */
9032   keyword = token->keyword;
9033   switch (keyword)
9034     {
9035     case RID_IF:
9036     case RID_SWITCH:
9037       {
9038         tree statement;
9039         tree condition;
9040
9041         /* Look for the `('.  */
9042         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9043           {
9044             cp_parser_skip_to_end_of_statement (parser);
9045             return error_mark_node;
9046           }
9047
9048         /* Begin the selection-statement.  */
9049         if (keyword == RID_IF)
9050           statement = begin_if_stmt ();
9051         else
9052           statement = begin_switch_stmt ();
9053
9054         /* Parse the condition.  */
9055         condition = cp_parser_condition (parser);
9056         /* Look for the `)'.  */
9057         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9058           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9059                                                  /*consume_paren=*/true);
9060
9061         if (keyword == RID_IF)
9062           {
9063             bool nested_if;
9064             unsigned char in_statement;
9065
9066             /* Add the condition.  */
9067             finish_if_stmt_cond (condition, statement);
9068
9069             /* Parse the then-clause.  */
9070             in_statement = parser->in_statement;
9071             parser->in_statement |= IN_IF_STMT;
9072             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9073               {
9074                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9075                 add_stmt (build_empty_stmt (loc));
9076                 cp_lexer_consume_token (parser->lexer);
9077                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9078                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9079                               "empty body in an %<if%> statement");
9080                 nested_if = false;
9081               }
9082             else
9083               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9084             parser->in_statement = in_statement;
9085
9086             finish_then_clause (statement);
9087
9088             /* If the next token is `else', parse the else-clause.  */
9089             if (cp_lexer_next_token_is_keyword (parser->lexer,
9090                                                 RID_ELSE))
9091               {
9092                 /* Consume the `else' keyword.  */
9093                 cp_lexer_consume_token (parser->lexer);
9094                 begin_else_clause (statement);
9095                 /* Parse the else-clause.  */
9096                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9097                   {
9098                     location_t loc;
9099                     loc = cp_lexer_peek_token (parser->lexer)->location;
9100                     warning_at (loc,
9101                                 OPT_Wempty_body, "suggest braces around "
9102                                 "empty body in an %<else%> statement");
9103                     add_stmt (build_empty_stmt (loc));
9104                     cp_lexer_consume_token (parser->lexer);
9105                   }
9106                 else
9107                   cp_parser_implicitly_scoped_statement (parser, NULL);
9108
9109                 finish_else_clause (statement);
9110
9111                 /* If we are currently parsing a then-clause, then
9112                    IF_P will not be NULL.  We set it to true to
9113                    indicate that this if statement has an else clause.
9114                    This may trigger the Wparentheses warning below
9115                    when we get back up to the parent if statement.  */
9116                 if (if_p != NULL)
9117                   *if_p = true;
9118               }
9119             else
9120               {
9121                 /* This if statement does not have an else clause.  If
9122                    NESTED_IF is true, then the then-clause is an if
9123                    statement which does have an else clause.  We warn
9124                    about the potential ambiguity.  */
9125                 if (nested_if)
9126                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9127                               "suggest explicit braces to avoid ambiguous"
9128                               " %<else%>");
9129               }
9130
9131             /* Now we're all done with the if-statement.  */
9132             finish_if_stmt (statement);
9133           }
9134         else
9135           {
9136             bool in_switch_statement_p;
9137             unsigned char in_statement;
9138
9139             /* Add the condition.  */
9140             finish_switch_cond (condition, statement);
9141
9142             /* Parse the body of the switch-statement.  */
9143             in_switch_statement_p = parser->in_switch_statement_p;
9144             in_statement = parser->in_statement;
9145             parser->in_switch_statement_p = true;
9146             parser->in_statement |= IN_SWITCH_STMT;
9147             cp_parser_implicitly_scoped_statement (parser, NULL);
9148             parser->in_switch_statement_p = in_switch_statement_p;
9149             parser->in_statement = in_statement;
9150
9151             /* Now we're all done with the switch-statement.  */
9152             finish_switch_stmt (statement);
9153           }
9154
9155         return statement;
9156       }
9157       break;
9158
9159     default:
9160       cp_parser_error (parser, "expected selection-statement");
9161       return error_mark_node;
9162     }
9163 }
9164
9165 /* Parse a condition.
9166
9167    condition:
9168      expression
9169      type-specifier-seq declarator = initializer-clause
9170      type-specifier-seq declarator braced-init-list
9171
9172    GNU Extension:
9173
9174    condition:
9175      type-specifier-seq declarator asm-specification [opt]
9176        attributes [opt] = assignment-expression
9177
9178    Returns the expression that should be tested.  */
9179
9180 static tree
9181 cp_parser_condition (cp_parser* parser)
9182 {
9183   cp_decl_specifier_seq type_specifiers;
9184   const char *saved_message;
9185   int declares_class_or_enum;
9186
9187   /* Try the declaration first.  */
9188   cp_parser_parse_tentatively (parser);
9189   /* New types are not allowed in the type-specifier-seq for a
9190      condition.  */
9191   saved_message = parser->type_definition_forbidden_message;
9192   parser->type_definition_forbidden_message
9193     = G_("types may not be defined in conditions");
9194   /* Parse the type-specifier-seq.  */
9195   cp_parser_decl_specifier_seq (parser,
9196                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9197                                 &type_specifiers,
9198                                 &declares_class_or_enum);
9199   /* Restore the saved message.  */
9200   parser->type_definition_forbidden_message = saved_message;
9201   /* If all is well, we might be looking at a declaration.  */
9202   if (!cp_parser_error_occurred (parser))
9203     {
9204       tree decl;
9205       tree asm_specification;
9206       tree attributes;
9207       cp_declarator *declarator;
9208       tree initializer = NULL_TREE;
9209
9210       /* Parse the declarator.  */
9211       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9212                                          /*ctor_dtor_or_conv_p=*/NULL,
9213                                          /*parenthesized_p=*/NULL,
9214                                          /*member_p=*/false);
9215       /* Parse the attributes.  */
9216       attributes = cp_parser_attributes_opt (parser);
9217       /* Parse the asm-specification.  */
9218       asm_specification = cp_parser_asm_specification_opt (parser);
9219       /* If the next token is not an `=' or '{', then we might still be
9220          looking at an expression.  For example:
9221
9222            if (A(a).x)
9223
9224          looks like a decl-specifier-seq and a declarator -- but then
9225          there is no `=', so this is an expression.  */
9226       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9227           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9228         cp_parser_simulate_error (parser);
9229         
9230       /* If we did see an `=' or '{', then we are looking at a declaration
9231          for sure.  */
9232       if (cp_parser_parse_definitely (parser))
9233         {
9234           tree pushed_scope;
9235           bool non_constant_p;
9236           bool flags = LOOKUP_ONLYCONVERTING;
9237
9238           /* Create the declaration.  */
9239           decl = start_decl (declarator, &type_specifiers,
9240                              /*initialized_p=*/true,
9241                              attributes, /*prefix_attributes=*/NULL_TREE,
9242                              &pushed_scope);
9243
9244           /* Parse the initializer.  */
9245           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9246             {
9247               initializer = cp_parser_braced_list (parser, &non_constant_p);
9248               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9249               flags = 0;
9250             }
9251           else
9252             {
9253               /* Consume the `='.  */
9254               cp_parser_require (parser, CPP_EQ, RT_EQ);
9255               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9256             }
9257           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9258             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9259
9260           /* Process the initializer.  */
9261           cp_finish_decl (decl,
9262                           initializer, !non_constant_p,
9263                           asm_specification,
9264                           flags);
9265
9266           if (pushed_scope)
9267             pop_scope (pushed_scope);
9268
9269           return convert_from_reference (decl);
9270         }
9271     }
9272   /* If we didn't even get past the declarator successfully, we are
9273      definitely not looking at a declaration.  */
9274   else
9275     cp_parser_abort_tentative_parse (parser);
9276
9277   /* Otherwise, we are looking at an expression.  */
9278   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9279 }
9280
9281 /* Parses a for-statement or range-for-statement until the closing ')',
9282    not included. */
9283
9284 static tree
9285 cp_parser_for (cp_parser *parser)
9286 {
9287   tree init, scope, decl;
9288   bool is_range_for;
9289
9290   /* Begin the for-statement.  */
9291   scope = begin_for_scope (&init);
9292
9293   /* Parse the initialization.  */
9294   is_range_for = cp_parser_for_init_statement (parser, &decl);
9295
9296   if (is_range_for)
9297     return cp_parser_range_for (parser, scope, init, decl);
9298   else
9299     return cp_parser_c_for (parser, scope, init);
9300 }
9301
9302 static tree
9303 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9304 {
9305   /* Normal for loop */
9306   tree condition = NULL_TREE;
9307   tree expression = NULL_TREE;
9308   tree stmt;
9309
9310   stmt = begin_for_stmt (scope, init);
9311   /* The for-init-statement has already been parsed in
9312      cp_parser_for_init_statement, so no work is needed here.  */
9313   finish_for_init_stmt (stmt);
9314
9315   /* If there's a condition, process it.  */
9316   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9317     condition = cp_parser_condition (parser);
9318   finish_for_cond (condition, stmt);
9319   /* Look for the `;'.  */
9320   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9321
9322   /* If there's an expression, process it.  */
9323   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9324     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9325   finish_for_expr (expression, stmt);
9326
9327   return stmt;
9328 }
9329
9330 /* Tries to parse a range-based for-statement:
9331
9332   range-based-for:
9333     decl-specifier-seq declarator : expression
9334
9335   The decl-specifier-seq declarator and the `:' are already parsed by
9336   cp_parser_for_init_statement. If processing_template_decl it returns a
9337   newly created RANGE_FOR_STMT; if not, it is converted to a
9338   regular FOR_STMT.  */
9339
9340 static tree
9341 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9342 {
9343   tree stmt, range_expr;
9344
9345   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9346     {
9347       bool expr_non_constant_p;
9348       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9349     }
9350   else
9351     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9352
9353   /* If in template, STMT is converted to a normal for-statement
9354      at instantiation. If not, it is done just ahead. */
9355   if (processing_template_decl)
9356     {
9357       if (check_for_bare_parameter_packs (range_expr))
9358         range_expr = error_mark_node;
9359       stmt = begin_range_for_stmt (scope, init);
9360       finish_range_for_decl (stmt, range_decl, range_expr);
9361       if (!type_dependent_expression_p (range_expr)
9362           /* do_auto_deduction doesn't mess with template init-lists.  */
9363           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9364         do_range_for_auto_deduction (range_decl, range_expr);
9365     }
9366   else
9367     {
9368       stmt = begin_for_stmt (scope, init);
9369       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9370     }
9371   return stmt;
9372 }
9373
9374 /* Subroutine of cp_convert_range_for: given the initializer expression,
9375    builds up the range temporary.  */
9376
9377 static tree
9378 build_range_temp (tree range_expr)
9379 {
9380   tree range_type, range_temp;
9381
9382   /* Find out the type deduced by the declaration
9383      `auto &&__range = range_expr'.  */
9384   range_type = cp_build_reference_type (make_auto (), true);
9385   range_type = do_auto_deduction (range_type, range_expr,
9386                                   type_uses_auto (range_type));
9387
9388   /* Create the __range variable.  */
9389   range_temp = build_decl (input_location, VAR_DECL,
9390                            get_identifier ("__for_range"), range_type);
9391   TREE_USED (range_temp) = 1;
9392   DECL_ARTIFICIAL (range_temp) = 1;
9393
9394   return range_temp;
9395 }
9396
9397 /* Used by cp_parser_range_for in template context: we aren't going to
9398    do a full conversion yet, but we still need to resolve auto in the
9399    type of the for-range-declaration if present.  This is basically
9400    a shortcut version of cp_convert_range_for.  */
9401
9402 static void
9403 do_range_for_auto_deduction (tree decl, tree range_expr)
9404 {
9405   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9406   if (auto_node)
9407     {
9408       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9409       range_temp = convert_from_reference (build_range_temp (range_expr));
9410       iter_type = (cp_parser_perform_range_for_lookup
9411                    (range_temp, &begin_dummy, &end_dummy));
9412       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9413       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9414                                         tf_warning_or_error);
9415       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9416                                             iter_decl, auto_node);
9417     }
9418 }
9419
9420 /* Converts a range-based for-statement into a normal
9421    for-statement, as per the definition.
9422
9423       for (RANGE_DECL : RANGE_EXPR)
9424         BLOCK
9425
9426    should be equivalent to:
9427
9428       {
9429         auto &&__range = RANGE_EXPR;
9430         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9431               __begin != __end;
9432               ++__begin)
9433           {
9434               RANGE_DECL = *__begin;
9435               BLOCK
9436           }
9437       }
9438
9439    If RANGE_EXPR is an array:
9440         BEGIN_EXPR = __range
9441         END_EXPR = __range + ARRAY_SIZE(__range)
9442    Else if RANGE_EXPR has a member 'begin' or 'end':
9443         BEGIN_EXPR = __range.begin()
9444         END_EXPR = __range.end()
9445    Else:
9446         BEGIN_EXPR = begin(__range)
9447         END_EXPR = end(__range);
9448
9449    If __range has a member 'begin' but not 'end', or vice versa, we must
9450    still use the second alternative (it will surely fail, however).
9451    When calling begin()/end() in the third alternative we must use
9452    argument dependent lookup, but always considering 'std' as an associated
9453    namespace.  */
9454
9455 tree
9456 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9457 {
9458   tree begin, end;
9459   tree iter_type, begin_expr, end_expr;
9460   tree condition, expression;
9461
9462   if (range_decl == error_mark_node || range_expr == error_mark_node)
9463     /* If an error happened previously do nothing or else a lot of
9464        unhelpful errors would be issued.  */
9465     begin_expr = end_expr = iter_type = error_mark_node;
9466   else
9467     {
9468       tree range_temp = build_range_temp (range_expr);
9469       pushdecl (range_temp);
9470       cp_finish_decl (range_temp, range_expr,
9471                       /*is_constant_init*/false, NULL_TREE,
9472                       LOOKUP_ONLYCONVERTING);
9473
9474       range_temp = convert_from_reference (range_temp);
9475       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9476                                                       &begin_expr, &end_expr);
9477     }
9478
9479   /* The new for initialization statement.  */
9480   begin = build_decl (input_location, VAR_DECL,
9481                       get_identifier ("__for_begin"), iter_type);
9482   TREE_USED (begin) = 1;
9483   DECL_ARTIFICIAL (begin) = 1;
9484   pushdecl (begin);
9485   cp_finish_decl (begin, begin_expr,
9486                   /*is_constant_init*/false, NULL_TREE,
9487                   LOOKUP_ONLYCONVERTING);
9488
9489   end = build_decl (input_location, VAR_DECL,
9490                     get_identifier ("__for_end"), iter_type);
9491   TREE_USED (end) = 1;
9492   DECL_ARTIFICIAL (end) = 1;
9493   pushdecl (end);
9494   cp_finish_decl (end, end_expr,
9495                   /*is_constant_init*/false, NULL_TREE,
9496                   LOOKUP_ONLYCONVERTING);
9497
9498   finish_for_init_stmt (statement);
9499
9500   /* The new for condition.  */
9501   condition = build_x_binary_op (NE_EXPR,
9502                                  begin, ERROR_MARK,
9503                                  end, ERROR_MARK,
9504                                  NULL, tf_warning_or_error);
9505   finish_for_cond (condition, statement);
9506
9507   /* The new increment expression.  */
9508   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9509   finish_for_expr (expression, statement);
9510
9511   /* The declaration is initialized with *__begin inside the loop body.  */
9512   cp_finish_decl (range_decl,
9513                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9514                   /*is_constant_init*/false, NULL_TREE,
9515                   LOOKUP_ONLYCONVERTING);
9516
9517   return statement;
9518 }
9519
9520 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9521    We need to solve both at the same time because the method used
9522    depends on the existence of members begin or end.
9523    Returns the type deduced for the iterator expression.  */
9524
9525 static tree
9526 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9527 {
9528   if (error_operand_p (range))
9529     {
9530       *begin = *end = error_mark_node;
9531       return error_mark_node;
9532     }
9533
9534   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9535     {
9536       error ("range-based %<for%> expression of type %qT "
9537              "has incomplete type", TREE_TYPE (range));
9538       *begin = *end = error_mark_node;
9539       return error_mark_node;
9540     }
9541   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9542     {
9543       /* If RANGE is an array, we will use pointer arithmetic.  */
9544       *begin = range;
9545       *end = build_binary_op (input_location, PLUS_EXPR,
9546                               range,
9547                               array_type_nelts_top (TREE_TYPE (range)),
9548                               0);
9549       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9550     }
9551   else
9552     {
9553       /* If it is not an array, we must do a bit of magic.  */
9554       tree id_begin, id_end;
9555       tree member_begin, member_end;
9556
9557       *begin = *end = error_mark_node;
9558
9559       id_begin = get_identifier ("begin");
9560       id_end = get_identifier ("end");
9561       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9562                                     /*protect=*/2, /*want_type=*/false,
9563                                     tf_warning_or_error);
9564       member_end = lookup_member (TREE_TYPE (range), id_end,
9565                                   /*protect=*/2, /*want_type=*/false,
9566                                   tf_warning_or_error);
9567
9568       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9569         {
9570           /* Use the member functions.  */
9571           if (member_begin != NULL_TREE)
9572             *begin = cp_parser_range_for_member_function (range, id_begin);
9573           else
9574             error ("range-based %<for%> expression of type %qT has an "
9575                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9576
9577           if (member_end != NULL_TREE)
9578             *end = cp_parser_range_for_member_function (range, id_end);
9579           else
9580             error ("range-based %<for%> expression of type %qT has a "
9581                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9582         }
9583       else
9584         {
9585           /* Use global functions with ADL.  */
9586           VEC(tree,gc) *vec;
9587           vec = make_tree_vector ();
9588
9589           VEC_safe_push (tree, gc, vec, range);
9590
9591           member_begin = perform_koenig_lookup (id_begin, vec,
9592                                                 /*include_std=*/true,
9593                                                 tf_warning_or_error);
9594           *begin = finish_call_expr (member_begin, &vec, false, true,
9595                                      tf_warning_or_error);
9596           member_end = perform_koenig_lookup (id_end, vec,
9597                                               /*include_std=*/true,
9598                                               tf_warning_or_error);
9599           *end = finish_call_expr (member_end, &vec, false, true,
9600                                    tf_warning_or_error);
9601
9602           release_tree_vector (vec);
9603         }
9604
9605       /* Last common checks.  */
9606       if (*begin == error_mark_node || *end == error_mark_node)
9607         {
9608           /* If one of the expressions is an error do no more checks.  */
9609           *begin = *end = error_mark_node;
9610           return error_mark_node;
9611         }
9612       else
9613         {
9614           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9615           /* The unqualified type of the __begin and __end temporaries should
9616              be the same, as required by the multiple auto declaration.  */
9617           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9618             error ("inconsistent begin/end types in range-based %<for%> "
9619                    "statement: %qT and %qT",
9620                    TREE_TYPE (*begin), TREE_TYPE (*end));
9621           return iter_type;
9622         }
9623     }
9624 }
9625
9626 /* Helper function for cp_parser_perform_range_for_lookup.
9627    Builds a tree for RANGE.IDENTIFIER().  */
9628
9629 static tree
9630 cp_parser_range_for_member_function (tree range, tree identifier)
9631 {
9632   tree member, res;
9633   VEC(tree,gc) *vec;
9634
9635   member = finish_class_member_access_expr (range, identifier,
9636                                             false, tf_warning_or_error);
9637   if (member == error_mark_node)
9638     return error_mark_node;
9639
9640   vec = make_tree_vector ();
9641   res = finish_call_expr (member, &vec,
9642                           /*disallow_virtual=*/false,
9643                           /*koenig_p=*/false,
9644                           tf_warning_or_error);
9645   release_tree_vector (vec);
9646   return res;
9647 }
9648
9649 /* Parse an iteration-statement.
9650
9651    iteration-statement:
9652      while ( condition ) statement
9653      do statement while ( expression ) ;
9654      for ( for-init-statement condition [opt] ; expression [opt] )
9655        statement
9656
9657    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9658
9659 static tree
9660 cp_parser_iteration_statement (cp_parser* parser)
9661 {
9662   cp_token *token;
9663   enum rid keyword;
9664   tree statement;
9665   unsigned char in_statement;
9666
9667   /* Peek at the next token.  */
9668   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9669   if (!token)
9670     return error_mark_node;
9671
9672   /* Remember whether or not we are already within an iteration
9673      statement.  */
9674   in_statement = parser->in_statement;
9675
9676   /* See what kind of keyword it is.  */
9677   keyword = token->keyword;
9678   switch (keyword)
9679     {
9680     case RID_WHILE:
9681       {
9682         tree condition;
9683
9684         /* Begin the while-statement.  */
9685         statement = begin_while_stmt ();
9686         /* Look for the `('.  */
9687         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9688         /* Parse the condition.  */
9689         condition = cp_parser_condition (parser);
9690         finish_while_stmt_cond (condition, statement);
9691         /* Look for the `)'.  */
9692         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9693         /* Parse the dependent statement.  */
9694         parser->in_statement = IN_ITERATION_STMT;
9695         cp_parser_already_scoped_statement (parser);
9696         parser->in_statement = in_statement;
9697         /* We're done with the while-statement.  */
9698         finish_while_stmt (statement);
9699       }
9700       break;
9701
9702     case RID_DO:
9703       {
9704         tree expression;
9705
9706         /* Begin the do-statement.  */
9707         statement = begin_do_stmt ();
9708         /* Parse the body of the do-statement.  */
9709         parser->in_statement = IN_ITERATION_STMT;
9710         cp_parser_implicitly_scoped_statement (parser, NULL);
9711         parser->in_statement = in_statement;
9712         finish_do_body (statement);
9713         /* Look for the `while' keyword.  */
9714         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9715         /* Look for the `('.  */
9716         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9717         /* Parse the expression.  */
9718         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9719         /* We're done with the do-statement.  */
9720         finish_do_stmt (expression, statement);
9721         /* Look for the `)'.  */
9722         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9723         /* Look for the `;'.  */
9724         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9725       }
9726       break;
9727
9728     case RID_FOR:
9729       {
9730         /* Look for the `('.  */
9731         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9732
9733         statement = cp_parser_for (parser);
9734
9735         /* Look for the `)'.  */
9736         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9737
9738         /* Parse the body of the for-statement.  */
9739         parser->in_statement = IN_ITERATION_STMT;
9740         cp_parser_already_scoped_statement (parser);
9741         parser->in_statement = in_statement;
9742
9743         /* We're done with the for-statement.  */
9744         finish_for_stmt (statement);
9745       }
9746       break;
9747
9748     default:
9749       cp_parser_error (parser, "expected iteration-statement");
9750       statement = error_mark_node;
9751       break;
9752     }
9753
9754   return statement;
9755 }
9756
9757 /* Parse a for-init-statement or the declarator of a range-based-for.
9758    Returns true if a range-based-for declaration is seen.
9759
9760    for-init-statement:
9761      expression-statement
9762      simple-declaration  */
9763
9764 static bool
9765 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9766 {
9767   /* If the next token is a `;', then we have an empty
9768      expression-statement.  Grammatically, this is also a
9769      simple-declaration, but an invalid one, because it does not
9770      declare anything.  Therefore, if we did not handle this case
9771      specially, we would issue an error message about an invalid
9772      declaration.  */
9773   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9774     {
9775       bool is_range_for = false;
9776       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9777
9778       parser->colon_corrects_to_scope_p = false;
9779
9780       /* We're going to speculatively look for a declaration, falling back
9781          to an expression, if necessary.  */
9782       cp_parser_parse_tentatively (parser);
9783       /* Parse the declaration.  */
9784       cp_parser_simple_declaration (parser,
9785                                     /*function_definition_allowed_p=*/false,
9786                                     decl);
9787       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9788       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9789         {
9790           /* It is a range-for, consume the ':' */
9791           cp_lexer_consume_token (parser->lexer);
9792           is_range_for = true;
9793           if (cxx_dialect < cxx0x)
9794             {
9795               error_at (cp_lexer_peek_token (parser->lexer)->location,
9796                         "range-based %<for%> loops are not allowed "
9797                         "in C++98 mode");
9798               *decl = error_mark_node;
9799             }
9800         }
9801       else
9802           /* The ';' is not consumed yet because we told
9803              cp_parser_simple_declaration not to.  */
9804           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9805
9806       if (cp_parser_parse_definitely (parser))
9807         return is_range_for;
9808       /* If the tentative parse failed, then we shall need to look for an
9809          expression-statement.  */
9810     }
9811   /* If we are here, it is an expression-statement.  */
9812   cp_parser_expression_statement (parser, NULL_TREE);
9813   return false;
9814 }
9815
9816 /* Parse a jump-statement.
9817
9818    jump-statement:
9819      break ;
9820      continue ;
9821      return expression [opt] ;
9822      return braced-init-list ;
9823      goto identifier ;
9824
9825    GNU extension:
9826
9827    jump-statement:
9828      goto * expression ;
9829
9830    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9831
9832 static tree
9833 cp_parser_jump_statement (cp_parser* parser)
9834 {
9835   tree statement = error_mark_node;
9836   cp_token *token;
9837   enum rid keyword;
9838   unsigned char in_statement;
9839
9840   /* Peek at the next token.  */
9841   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9842   if (!token)
9843     return error_mark_node;
9844
9845   /* See what kind of keyword it is.  */
9846   keyword = token->keyword;
9847   switch (keyword)
9848     {
9849     case RID_BREAK:
9850       in_statement = parser->in_statement & ~IN_IF_STMT;      
9851       switch (in_statement)
9852         {
9853         case 0:
9854           error_at (token->location, "break statement not within loop or switch");
9855           break;
9856         default:
9857           gcc_assert ((in_statement & IN_SWITCH_STMT)
9858                       || in_statement == IN_ITERATION_STMT);
9859           statement = finish_break_stmt ();
9860           break;
9861         case IN_OMP_BLOCK:
9862           error_at (token->location, "invalid exit from OpenMP structured block");
9863           break;
9864         case IN_OMP_FOR:
9865           error_at (token->location, "break statement used with OpenMP for loop");
9866           break;
9867         }
9868       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9869       break;
9870
9871     case RID_CONTINUE:
9872       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9873         {
9874         case 0:
9875           error_at (token->location, "continue statement not within a loop");
9876           break;
9877         case IN_ITERATION_STMT:
9878         case IN_OMP_FOR:
9879           statement = finish_continue_stmt ();
9880           break;
9881         case IN_OMP_BLOCK:
9882           error_at (token->location, "invalid exit from OpenMP structured block");
9883           break;
9884         default:
9885           gcc_unreachable ();
9886         }
9887       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9888       break;
9889
9890     case RID_RETURN:
9891       {
9892         tree expr;
9893         bool expr_non_constant_p;
9894
9895         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9896           {
9897             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9898             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9899           }
9900         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9901           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9902         else
9903           /* If the next token is a `;', then there is no
9904              expression.  */
9905           expr = NULL_TREE;
9906         /* Build the return-statement.  */
9907         statement = finish_return_stmt (expr);
9908         /* Look for the final `;'.  */
9909         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9910       }
9911       break;
9912
9913     case RID_GOTO:
9914       /* Create the goto-statement.  */
9915       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9916         {
9917           /* Issue a warning about this use of a GNU extension.  */
9918           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9919           /* Consume the '*' token.  */
9920           cp_lexer_consume_token (parser->lexer);
9921           /* Parse the dependent expression.  */
9922           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9923         }
9924       else
9925         finish_goto_stmt (cp_parser_identifier (parser));
9926       /* Look for the final `;'.  */
9927       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9928       break;
9929
9930     default:
9931       cp_parser_error (parser, "expected jump-statement");
9932       break;
9933     }
9934
9935   return statement;
9936 }
9937
9938 /* Parse a declaration-statement.
9939
9940    declaration-statement:
9941      block-declaration  */
9942
9943 static void
9944 cp_parser_declaration_statement (cp_parser* parser)
9945 {
9946   void *p;
9947
9948   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9949   p = obstack_alloc (&declarator_obstack, 0);
9950
9951  /* Parse the block-declaration.  */
9952   cp_parser_block_declaration (parser, /*statement_p=*/true);
9953
9954   /* Free any declarators allocated.  */
9955   obstack_free (&declarator_obstack, p);
9956
9957   /* Finish off the statement.  */
9958   finish_stmt ();
9959 }
9960
9961 /* Some dependent statements (like `if (cond) statement'), are
9962    implicitly in their own scope.  In other words, if the statement is
9963    a single statement (as opposed to a compound-statement), it is
9964    none-the-less treated as if it were enclosed in braces.  Any
9965    declarations appearing in the dependent statement are out of scope
9966    after control passes that point.  This function parses a statement,
9967    but ensures that is in its own scope, even if it is not a
9968    compound-statement.
9969
9970    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9971    is a (possibly labeled) if statement which is not enclosed in
9972    braces and has an else clause.  This is used to implement
9973    -Wparentheses.
9974
9975    Returns the new statement.  */
9976
9977 static tree
9978 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9979 {
9980   tree statement;
9981
9982   if (if_p != NULL)
9983     *if_p = false;
9984
9985   /* Mark if () ; with a special NOP_EXPR.  */
9986   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9987     {
9988       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9989       cp_lexer_consume_token (parser->lexer);
9990       statement = add_stmt (build_empty_stmt (loc));
9991     }
9992   /* if a compound is opened, we simply parse the statement directly.  */
9993   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9994     statement = cp_parser_compound_statement (parser, NULL, false, false);
9995   /* If the token is not a `{', then we must take special action.  */
9996   else
9997     {
9998       /* Create a compound-statement.  */
9999       statement = begin_compound_stmt (0);
10000       /* Parse the dependent-statement.  */
10001       cp_parser_statement (parser, NULL_TREE, false, if_p);
10002       /* Finish the dummy compound-statement.  */
10003       finish_compound_stmt (statement);
10004     }
10005
10006   /* Return the statement.  */
10007   return statement;
10008 }
10009
10010 /* For some dependent statements (like `while (cond) statement'), we
10011    have already created a scope.  Therefore, even if the dependent
10012    statement is a compound-statement, we do not want to create another
10013    scope.  */
10014
10015 static void
10016 cp_parser_already_scoped_statement (cp_parser* parser)
10017 {
10018   /* If the token is a `{', then we must take special action.  */
10019   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10020     cp_parser_statement (parser, NULL_TREE, false, NULL);
10021   else
10022     {
10023       /* Avoid calling cp_parser_compound_statement, so that we
10024          don't create a new scope.  Do everything else by hand.  */
10025       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10026       /* If the next keyword is `__label__' we have a label declaration.  */
10027       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10028         cp_parser_label_declaration (parser);
10029       /* Parse an (optional) statement-seq.  */
10030       cp_parser_statement_seq_opt (parser, NULL_TREE);
10031       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10032     }
10033 }
10034
10035 /* Declarations [gram.dcl.dcl] */
10036
10037 /* Parse an optional declaration-sequence.
10038
10039    declaration-seq:
10040      declaration
10041      declaration-seq declaration  */
10042
10043 static void
10044 cp_parser_declaration_seq_opt (cp_parser* parser)
10045 {
10046   while (true)
10047     {
10048       cp_token *token;
10049
10050       token = cp_lexer_peek_token (parser->lexer);
10051
10052       if (token->type == CPP_CLOSE_BRACE
10053           || token->type == CPP_EOF
10054           || token->type == CPP_PRAGMA_EOL)
10055         break;
10056
10057       if (token->type == CPP_SEMICOLON)
10058         {
10059           /* A declaration consisting of a single semicolon is
10060              invalid.  Allow it unless we're being pedantic.  */
10061           cp_lexer_consume_token (parser->lexer);
10062           if (!in_system_header)
10063             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10064           continue;
10065         }
10066
10067       /* If we're entering or exiting a region that's implicitly
10068          extern "C", modify the lang context appropriately.  */
10069       if (!parser->implicit_extern_c && token->implicit_extern_c)
10070         {
10071           push_lang_context (lang_name_c);
10072           parser->implicit_extern_c = true;
10073         }
10074       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10075         {
10076           pop_lang_context ();
10077           parser->implicit_extern_c = false;
10078         }
10079
10080       if (token->type == CPP_PRAGMA)
10081         {
10082           /* A top-level declaration can consist solely of a #pragma.
10083              A nested declaration cannot, so this is done here and not
10084              in cp_parser_declaration.  (A #pragma at block scope is
10085              handled in cp_parser_statement.)  */
10086           cp_parser_pragma (parser, pragma_external);
10087           continue;
10088         }
10089
10090       /* Parse the declaration itself.  */
10091       cp_parser_declaration (parser);
10092     }
10093 }
10094
10095 /* Parse a declaration.
10096
10097    declaration:
10098      block-declaration
10099      function-definition
10100      template-declaration
10101      explicit-instantiation
10102      explicit-specialization
10103      linkage-specification
10104      namespace-definition
10105
10106    GNU extension:
10107
10108    declaration:
10109       __extension__ declaration */
10110
10111 static void
10112 cp_parser_declaration (cp_parser* parser)
10113 {
10114   cp_token token1;
10115   cp_token token2;
10116   int saved_pedantic;
10117   void *p;
10118   tree attributes = NULL_TREE;
10119
10120   /* Check for the `__extension__' keyword.  */
10121   if (cp_parser_extension_opt (parser, &saved_pedantic))
10122     {
10123       /* Parse the qualified declaration.  */
10124       cp_parser_declaration (parser);
10125       /* Restore the PEDANTIC flag.  */
10126       pedantic = saved_pedantic;
10127
10128       return;
10129     }
10130
10131   /* Try to figure out what kind of declaration is present.  */
10132   token1 = *cp_lexer_peek_token (parser->lexer);
10133
10134   if (token1.type != CPP_EOF)
10135     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10136   else
10137     {
10138       token2.type = CPP_EOF;
10139       token2.keyword = RID_MAX;
10140     }
10141
10142   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10143   p = obstack_alloc (&declarator_obstack, 0);
10144
10145   /* If the next token is `extern' and the following token is a string
10146      literal, then we have a linkage specification.  */
10147   if (token1.keyword == RID_EXTERN
10148       && cp_parser_is_pure_string_literal (&token2))
10149     cp_parser_linkage_specification (parser);
10150   /* If the next token is `template', then we have either a template
10151      declaration, an explicit instantiation, or an explicit
10152      specialization.  */
10153   else if (token1.keyword == RID_TEMPLATE)
10154     {
10155       /* `template <>' indicates a template specialization.  */
10156       if (token2.type == CPP_LESS
10157           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10158         cp_parser_explicit_specialization (parser);
10159       /* `template <' indicates a template declaration.  */
10160       else if (token2.type == CPP_LESS)
10161         cp_parser_template_declaration (parser, /*member_p=*/false);
10162       /* Anything else must be an explicit instantiation.  */
10163       else
10164         cp_parser_explicit_instantiation (parser);
10165     }
10166   /* If the next token is `export', then we have a template
10167      declaration.  */
10168   else if (token1.keyword == RID_EXPORT)
10169     cp_parser_template_declaration (parser, /*member_p=*/false);
10170   /* If the next token is `extern', 'static' or 'inline' and the one
10171      after that is `template', we have a GNU extended explicit
10172      instantiation directive.  */
10173   else if (cp_parser_allow_gnu_extensions_p (parser)
10174            && (token1.keyword == RID_EXTERN
10175                || token1.keyword == RID_STATIC
10176                || token1.keyword == RID_INLINE)
10177            && token2.keyword == RID_TEMPLATE)
10178     cp_parser_explicit_instantiation (parser);
10179   /* If the next token is `namespace', check for a named or unnamed
10180      namespace definition.  */
10181   else if (token1.keyword == RID_NAMESPACE
10182            && (/* A named namespace definition.  */
10183                (token2.type == CPP_NAME
10184                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10185                     != CPP_EQ))
10186                /* An unnamed namespace definition.  */
10187                || token2.type == CPP_OPEN_BRACE
10188                || token2.keyword == RID_ATTRIBUTE))
10189     cp_parser_namespace_definition (parser);
10190   /* An inline (associated) namespace definition.  */
10191   else if (token1.keyword == RID_INLINE
10192            && token2.keyword == RID_NAMESPACE)
10193     cp_parser_namespace_definition (parser);
10194   /* Objective-C++ declaration/definition.  */
10195   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10196     cp_parser_objc_declaration (parser, NULL_TREE);
10197   else if (c_dialect_objc ()
10198            && token1.keyword == RID_ATTRIBUTE
10199            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10200     cp_parser_objc_declaration (parser, attributes);
10201   /* We must have either a block declaration or a function
10202      definition.  */
10203   else
10204     /* Try to parse a block-declaration, or a function-definition.  */
10205     cp_parser_block_declaration (parser, /*statement_p=*/false);
10206
10207   /* Free any declarators allocated.  */
10208   obstack_free (&declarator_obstack, p);
10209 }
10210
10211 /* Parse a block-declaration.
10212
10213    block-declaration:
10214      simple-declaration
10215      asm-definition
10216      namespace-alias-definition
10217      using-declaration
10218      using-directive
10219
10220    GNU Extension:
10221
10222    block-declaration:
10223      __extension__ block-declaration
10224
10225    C++0x Extension:
10226
10227    block-declaration:
10228      static_assert-declaration
10229
10230    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10231    part of a declaration-statement.  */
10232
10233 static void
10234 cp_parser_block_declaration (cp_parser *parser,
10235                              bool      statement_p)
10236 {
10237   cp_token *token1;
10238   int saved_pedantic;
10239
10240   /* Check for the `__extension__' keyword.  */
10241   if (cp_parser_extension_opt (parser, &saved_pedantic))
10242     {
10243       /* Parse the qualified declaration.  */
10244       cp_parser_block_declaration (parser, statement_p);
10245       /* Restore the PEDANTIC flag.  */
10246       pedantic = saved_pedantic;
10247
10248       return;
10249     }
10250
10251   /* Peek at the next token to figure out which kind of declaration is
10252      present.  */
10253   token1 = cp_lexer_peek_token (parser->lexer);
10254
10255   /* If the next keyword is `asm', we have an asm-definition.  */
10256   if (token1->keyword == RID_ASM)
10257     {
10258       if (statement_p)
10259         cp_parser_commit_to_tentative_parse (parser);
10260       cp_parser_asm_definition (parser);
10261     }
10262   /* If the next keyword is `namespace', we have a
10263      namespace-alias-definition.  */
10264   else if (token1->keyword == RID_NAMESPACE)
10265     cp_parser_namespace_alias_definition (parser);
10266   /* If the next keyword is `using', we have a
10267      using-declaration, a using-directive, or an alias-declaration.  */
10268   else if (token1->keyword == RID_USING)
10269     {
10270       cp_token *token2;
10271
10272       if (statement_p)
10273         cp_parser_commit_to_tentative_parse (parser);
10274       /* If the token after `using' is `namespace', then we have a
10275          using-directive.  */
10276       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10277       if (token2->keyword == RID_NAMESPACE)
10278         cp_parser_using_directive (parser);
10279       /* If the second token after 'using' is '=', then we have an
10280          alias-declaration.  */
10281       else if (cxx_dialect >= cxx0x
10282                && token2->type == CPP_NAME
10283                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10284                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10285                        == RID_ATTRIBUTE)))
10286         cp_parser_alias_declaration (parser);
10287       /* Otherwise, it's a using-declaration.  */
10288       else
10289         cp_parser_using_declaration (parser,
10290                                      /*access_declaration_p=*/false);
10291     }
10292   /* If the next keyword is `__label__' we have a misplaced label
10293      declaration.  */
10294   else if (token1->keyword == RID_LABEL)
10295     {
10296       cp_lexer_consume_token (parser->lexer);
10297       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10298       cp_parser_skip_to_end_of_statement (parser);
10299       /* If the next token is now a `;', consume it.  */
10300       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10301         cp_lexer_consume_token (parser->lexer);
10302     }
10303   /* If the next token is `static_assert' we have a static assertion.  */
10304   else if (token1->keyword == RID_STATIC_ASSERT)
10305     cp_parser_static_assert (parser, /*member_p=*/false);
10306   /* Anything else must be a simple-declaration.  */
10307   else
10308     cp_parser_simple_declaration (parser, !statement_p,
10309                                   /*maybe_range_for_decl*/NULL);
10310 }
10311
10312 /* Parse a simple-declaration.
10313
10314    simple-declaration:
10315      decl-specifier-seq [opt] init-declarator-list [opt] ;
10316
10317    init-declarator-list:
10318      init-declarator
10319      init-declarator-list , init-declarator
10320
10321    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10322    function-definition as a simple-declaration.
10323
10324    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10325    parsed declaration if it is an uninitialized single declarator not followed
10326    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10327    if present, will not be consumed.  */
10328
10329 static void
10330 cp_parser_simple_declaration (cp_parser* parser,
10331                               bool function_definition_allowed_p,
10332                               tree *maybe_range_for_decl)
10333 {
10334   cp_decl_specifier_seq decl_specifiers;
10335   int declares_class_or_enum;
10336   bool saw_declarator;
10337
10338   if (maybe_range_for_decl)
10339     *maybe_range_for_decl = NULL_TREE;
10340
10341   /* Defer access checks until we know what is being declared; the
10342      checks for names appearing in the decl-specifier-seq should be
10343      done as if we were in the scope of the thing being declared.  */
10344   push_deferring_access_checks (dk_deferred);
10345
10346   /* Parse the decl-specifier-seq.  We have to keep track of whether
10347      or not the decl-specifier-seq declares a named class or
10348      enumeration type, since that is the only case in which the
10349      init-declarator-list is allowed to be empty.
10350
10351      [dcl.dcl]
10352
10353      In a simple-declaration, the optional init-declarator-list can be
10354      omitted only when declaring a class or enumeration, that is when
10355      the decl-specifier-seq contains either a class-specifier, an
10356      elaborated-type-specifier, or an enum-specifier.  */
10357   cp_parser_decl_specifier_seq (parser,
10358                                 CP_PARSER_FLAGS_OPTIONAL,
10359                                 &decl_specifiers,
10360                                 &declares_class_or_enum);
10361   /* We no longer need to defer access checks.  */
10362   stop_deferring_access_checks ();
10363
10364   /* In a block scope, a valid declaration must always have a
10365      decl-specifier-seq.  By not trying to parse declarators, we can
10366      resolve the declaration/expression ambiguity more quickly.  */
10367   if (!function_definition_allowed_p
10368       && !decl_specifiers.any_specifiers_p)
10369     {
10370       cp_parser_error (parser, "expected declaration");
10371       goto done;
10372     }
10373
10374   /* If the next two tokens are both identifiers, the code is
10375      erroneous. The usual cause of this situation is code like:
10376
10377        T t;
10378
10379      where "T" should name a type -- but does not.  */
10380   if (!decl_specifiers.any_type_specifiers_p
10381       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10382     {
10383       /* If parsing tentatively, we should commit; we really are
10384          looking at a declaration.  */
10385       cp_parser_commit_to_tentative_parse (parser);
10386       /* Give up.  */
10387       goto done;
10388     }
10389
10390   /* If we have seen at least one decl-specifier, and the next token
10391      is not a parenthesis, then we must be looking at a declaration.
10392      (After "int (" we might be looking at a functional cast.)  */
10393   if (decl_specifiers.any_specifiers_p
10394       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10395       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10396       && !cp_parser_error_occurred (parser))
10397     cp_parser_commit_to_tentative_parse (parser);
10398
10399   /* Keep going until we hit the `;' at the end of the simple
10400      declaration.  */
10401   saw_declarator = false;
10402   while (cp_lexer_next_token_is_not (parser->lexer,
10403                                      CPP_SEMICOLON))
10404     {
10405       cp_token *token;
10406       bool function_definition_p;
10407       tree decl;
10408
10409       if (saw_declarator)
10410         {
10411           /* If we are processing next declarator, coma is expected */
10412           token = cp_lexer_peek_token (parser->lexer);
10413           gcc_assert (token->type == CPP_COMMA);
10414           cp_lexer_consume_token (parser->lexer);
10415           if (maybe_range_for_decl)
10416             *maybe_range_for_decl = error_mark_node;
10417         }
10418       else
10419         saw_declarator = true;
10420
10421       /* Parse the init-declarator.  */
10422       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10423                                         /*checks=*/NULL,
10424                                         function_definition_allowed_p,
10425                                         /*member_p=*/false,
10426                                         declares_class_or_enum,
10427                                         &function_definition_p,
10428                                         maybe_range_for_decl);
10429       /* If an error occurred while parsing tentatively, exit quickly.
10430          (That usually happens when in the body of a function; each
10431          statement is treated as a declaration-statement until proven
10432          otherwise.)  */
10433       if (cp_parser_error_occurred (parser))
10434         goto done;
10435       /* Handle function definitions specially.  */
10436       if (function_definition_p)
10437         {
10438           /* If the next token is a `,', then we are probably
10439              processing something like:
10440
10441                void f() {}, *p;
10442
10443              which is erroneous.  */
10444           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10445             {
10446               cp_token *token = cp_lexer_peek_token (parser->lexer);
10447               error_at (token->location,
10448                         "mixing"
10449                         " declarations and function-definitions is forbidden");
10450             }
10451           /* Otherwise, we're done with the list of declarators.  */
10452           else
10453             {
10454               pop_deferring_access_checks ();
10455               return;
10456             }
10457         }
10458       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10459         *maybe_range_for_decl = decl;
10460       /* The next token should be either a `,' or a `;'.  */
10461       token = cp_lexer_peek_token (parser->lexer);
10462       /* If it's a `,', there are more declarators to come.  */
10463       if (token->type == CPP_COMMA)
10464         /* will be consumed next time around */;
10465       /* If it's a `;', we are done.  */
10466       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10467         break;
10468       /* Anything else is an error.  */
10469       else
10470         {
10471           /* If we have already issued an error message we don't need
10472              to issue another one.  */
10473           if (decl != error_mark_node
10474               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10475             cp_parser_error (parser, "expected %<,%> or %<;%>");
10476           /* Skip tokens until we reach the end of the statement.  */
10477           cp_parser_skip_to_end_of_statement (parser);
10478           /* If the next token is now a `;', consume it.  */
10479           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10480             cp_lexer_consume_token (parser->lexer);
10481           goto done;
10482         }
10483       /* After the first time around, a function-definition is not
10484          allowed -- even if it was OK at first.  For example:
10485
10486            int i, f() {}
10487
10488          is not valid.  */
10489       function_definition_allowed_p = false;
10490     }
10491
10492   /* Issue an error message if no declarators are present, and the
10493      decl-specifier-seq does not itself declare a class or
10494      enumeration.  */
10495   if (!saw_declarator)
10496     {
10497       if (cp_parser_declares_only_class_p (parser))
10498         shadow_tag (&decl_specifiers);
10499       /* Perform any deferred access checks.  */
10500       perform_deferred_access_checks ();
10501     }
10502
10503   /* Consume the `;'.  */
10504   if (!maybe_range_for_decl)
10505       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10506
10507  done:
10508   pop_deferring_access_checks ();
10509 }
10510
10511 /* Parse a decl-specifier-seq.
10512
10513    decl-specifier-seq:
10514      decl-specifier-seq [opt] decl-specifier
10515
10516    decl-specifier:
10517      storage-class-specifier
10518      type-specifier
10519      function-specifier
10520      friend
10521      typedef
10522
10523    GNU Extension:
10524
10525    decl-specifier:
10526      attributes
10527
10528    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10529
10530    The parser flags FLAGS is used to control type-specifier parsing.
10531
10532    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10533    flags:
10534
10535      1: one of the decl-specifiers is an elaborated-type-specifier
10536         (i.e., a type declaration)
10537      2: one of the decl-specifiers is an enum-specifier or a
10538         class-specifier (i.e., a type definition)
10539
10540    */
10541
10542 static void
10543 cp_parser_decl_specifier_seq (cp_parser* parser,
10544                               cp_parser_flags flags,
10545                               cp_decl_specifier_seq *decl_specs,
10546                               int* declares_class_or_enum)
10547 {
10548   bool constructor_possible_p = !parser->in_declarator_p;
10549   cp_token *start_token = NULL;
10550
10551   /* Clear DECL_SPECS.  */
10552   clear_decl_specs (decl_specs);
10553
10554   /* Assume no class or enumeration type is declared.  */
10555   *declares_class_or_enum = 0;
10556
10557   /* Keep reading specifiers until there are no more to read.  */
10558   while (true)
10559     {
10560       bool constructor_p;
10561       bool found_decl_spec;
10562       cp_token *token;
10563
10564       /* Peek at the next token.  */
10565       token = cp_lexer_peek_token (parser->lexer);
10566
10567       /* Save the first token of the decl spec list for error
10568          reporting.  */
10569       if (!start_token)
10570         start_token = token;
10571       /* Handle attributes.  */
10572       if (token->keyword == RID_ATTRIBUTE)
10573         {
10574           /* Parse the attributes.  */
10575           decl_specs->attributes
10576             = chainon (decl_specs->attributes,
10577                        cp_parser_attributes_opt (parser));
10578           continue;
10579         }
10580       /* Assume we will find a decl-specifier keyword.  */
10581       found_decl_spec = true;
10582       /* If the next token is an appropriate keyword, we can simply
10583          add it to the list.  */
10584       switch (token->keyword)
10585         {
10586           /* decl-specifier:
10587                friend
10588                constexpr */
10589         case RID_FRIEND:
10590           if (!at_class_scope_p ())
10591             {
10592               error_at (token->location, "%<friend%> used outside of class");
10593               cp_lexer_purge_token (parser->lexer);
10594             }
10595           else
10596             {
10597               ++decl_specs->specs[(int) ds_friend];
10598               /* Consume the token.  */
10599               cp_lexer_consume_token (parser->lexer);
10600             }
10601           break;
10602
10603         case RID_CONSTEXPR:
10604           ++decl_specs->specs[(int) ds_constexpr];
10605           cp_lexer_consume_token (parser->lexer);
10606           break;
10607
10608           /* function-specifier:
10609                inline
10610                virtual
10611                explicit  */
10612         case RID_INLINE:
10613         case RID_VIRTUAL:
10614         case RID_EXPLICIT:
10615           cp_parser_function_specifier_opt (parser, decl_specs);
10616           break;
10617
10618           /* decl-specifier:
10619                typedef  */
10620         case RID_TYPEDEF:
10621           ++decl_specs->specs[(int) ds_typedef];
10622           /* Consume the token.  */
10623           cp_lexer_consume_token (parser->lexer);
10624           /* A constructor declarator cannot appear in a typedef.  */
10625           constructor_possible_p = false;
10626           /* The "typedef" keyword can only occur in a declaration; we
10627              may as well commit at this point.  */
10628           cp_parser_commit_to_tentative_parse (parser);
10629
10630           if (decl_specs->storage_class != sc_none)
10631             decl_specs->conflicting_specifiers_p = true;
10632           break;
10633
10634           /* storage-class-specifier:
10635                auto
10636                register
10637                static
10638                extern
10639                mutable
10640
10641              GNU Extension:
10642                thread  */
10643         case RID_AUTO:
10644           if (cxx_dialect == cxx98) 
10645             {
10646               /* Consume the token.  */
10647               cp_lexer_consume_token (parser->lexer);
10648
10649               /* Complain about `auto' as a storage specifier, if
10650                  we're complaining about C++0x compatibility.  */
10651               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10652                           " changes meaning in C++11; please remove it");
10653
10654               /* Set the storage class anyway.  */
10655               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10656                                            token->location);
10657             }
10658           else
10659             /* C++0x auto type-specifier.  */
10660             found_decl_spec = false;
10661           break;
10662
10663         case RID_REGISTER:
10664         case RID_STATIC:
10665         case RID_EXTERN:
10666         case RID_MUTABLE:
10667           /* Consume the token.  */
10668           cp_lexer_consume_token (parser->lexer);
10669           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10670                                        token->location);
10671           break;
10672         case RID_THREAD:
10673           /* Consume the token.  */
10674           cp_lexer_consume_token (parser->lexer);
10675           ++decl_specs->specs[(int) ds_thread];
10676           break;
10677
10678         default:
10679           /* We did not yet find a decl-specifier yet.  */
10680           found_decl_spec = false;
10681           break;
10682         }
10683
10684       if (found_decl_spec
10685           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10686           && token->keyword != RID_CONSTEXPR)
10687         error ("decl-specifier invalid in condition");
10688
10689       /* Constructors are a special case.  The `S' in `S()' is not a
10690          decl-specifier; it is the beginning of the declarator.  */
10691       constructor_p
10692         = (!found_decl_spec
10693            && constructor_possible_p
10694            && (cp_parser_constructor_declarator_p
10695                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10696
10697       /* If we don't have a DECL_SPEC yet, then we must be looking at
10698          a type-specifier.  */
10699       if (!found_decl_spec && !constructor_p)
10700         {
10701           int decl_spec_declares_class_or_enum;
10702           bool is_cv_qualifier;
10703           tree type_spec;
10704
10705           type_spec
10706             = cp_parser_type_specifier (parser, flags,
10707                                         decl_specs,
10708                                         /*is_declaration=*/true,
10709                                         &decl_spec_declares_class_or_enum,
10710                                         &is_cv_qualifier);
10711           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10712
10713           /* If this type-specifier referenced a user-defined type
10714              (a typedef, class-name, etc.), then we can't allow any
10715              more such type-specifiers henceforth.
10716
10717              [dcl.spec]
10718
10719              The longest sequence of decl-specifiers that could
10720              possibly be a type name is taken as the
10721              decl-specifier-seq of a declaration.  The sequence shall
10722              be self-consistent as described below.
10723
10724              [dcl.type]
10725
10726              As a general rule, at most one type-specifier is allowed
10727              in the complete decl-specifier-seq of a declaration.  The
10728              only exceptions are the following:
10729
10730              -- const or volatile can be combined with any other
10731                 type-specifier.
10732
10733              -- signed or unsigned can be combined with char, long,
10734                 short, or int.
10735
10736              -- ..
10737
10738              Example:
10739
10740                typedef char* Pc;
10741                void g (const int Pc);
10742
10743              Here, Pc is *not* part of the decl-specifier seq; it's
10744              the declarator.  Therefore, once we see a type-specifier
10745              (other than a cv-qualifier), we forbid any additional
10746              user-defined types.  We *do* still allow things like `int
10747              int' to be considered a decl-specifier-seq, and issue the
10748              error message later.  */
10749           if (type_spec && !is_cv_qualifier)
10750             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10751           /* A constructor declarator cannot follow a type-specifier.  */
10752           if (type_spec)
10753             {
10754               constructor_possible_p = false;
10755               found_decl_spec = true;
10756               if (!is_cv_qualifier)
10757                 decl_specs->any_type_specifiers_p = true;
10758             }
10759         }
10760
10761       /* If we still do not have a DECL_SPEC, then there are no more
10762          decl-specifiers.  */
10763       if (!found_decl_spec)
10764         break;
10765
10766       decl_specs->any_specifiers_p = true;
10767       /* After we see one decl-specifier, further decl-specifiers are
10768          always optional.  */
10769       flags |= CP_PARSER_FLAGS_OPTIONAL;
10770     }
10771
10772   cp_parser_check_decl_spec (decl_specs, start_token->location);
10773
10774   /* Don't allow a friend specifier with a class definition.  */
10775   if (decl_specs->specs[(int) ds_friend] != 0
10776       && (*declares_class_or_enum & 2))
10777     error_at (start_token->location,
10778               "class definition may not be declared a friend");
10779 }
10780
10781 /* Parse an (optional) storage-class-specifier.
10782
10783    storage-class-specifier:
10784      auto
10785      register
10786      static
10787      extern
10788      mutable
10789
10790    GNU Extension:
10791
10792    storage-class-specifier:
10793      thread
10794
10795    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10796
10797 static tree
10798 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10799 {
10800   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10801     {
10802     case RID_AUTO:
10803       if (cxx_dialect != cxx98)
10804         return NULL_TREE;
10805       /* Fall through for C++98.  */
10806
10807     case RID_REGISTER:
10808     case RID_STATIC:
10809     case RID_EXTERN:
10810     case RID_MUTABLE:
10811     case RID_THREAD:
10812       /* Consume the token.  */
10813       return cp_lexer_consume_token (parser->lexer)->u.value;
10814
10815     default:
10816       return NULL_TREE;
10817     }
10818 }
10819
10820 /* Parse an (optional) function-specifier.
10821
10822    function-specifier:
10823      inline
10824      virtual
10825      explicit
10826
10827    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10828    Updates DECL_SPECS, if it is non-NULL.  */
10829
10830 static tree
10831 cp_parser_function_specifier_opt (cp_parser* parser,
10832                                   cp_decl_specifier_seq *decl_specs)
10833 {
10834   cp_token *token = cp_lexer_peek_token (parser->lexer);
10835   switch (token->keyword)
10836     {
10837     case RID_INLINE:
10838       if (decl_specs)
10839         ++decl_specs->specs[(int) ds_inline];
10840       break;
10841
10842     case RID_VIRTUAL:
10843       /* 14.5.2.3 [temp.mem]
10844
10845          A member function template shall not be virtual.  */
10846       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10847         error_at (token->location, "templates may not be %<virtual%>");
10848       else if (decl_specs)
10849         ++decl_specs->specs[(int) ds_virtual];
10850       break;
10851
10852     case RID_EXPLICIT:
10853       if (decl_specs)
10854         ++decl_specs->specs[(int) ds_explicit];
10855       break;
10856
10857     default:
10858       return NULL_TREE;
10859     }
10860
10861   /* Consume the token.  */
10862   return cp_lexer_consume_token (parser->lexer)->u.value;
10863 }
10864
10865 /* Parse a linkage-specification.
10866
10867    linkage-specification:
10868      extern string-literal { declaration-seq [opt] }
10869      extern string-literal declaration  */
10870
10871 static void
10872 cp_parser_linkage_specification (cp_parser* parser)
10873 {
10874   tree linkage;
10875
10876   /* Look for the `extern' keyword.  */
10877   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10878
10879   /* Look for the string-literal.  */
10880   linkage = cp_parser_string_literal (parser, false, false);
10881
10882   /* Transform the literal into an identifier.  If the literal is a
10883      wide-character string, or contains embedded NULs, then we can't
10884      handle it as the user wants.  */
10885   if (strlen (TREE_STRING_POINTER (linkage))
10886       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10887     {
10888       cp_parser_error (parser, "invalid linkage-specification");
10889       /* Assume C++ linkage.  */
10890       linkage = lang_name_cplusplus;
10891     }
10892   else
10893     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10894
10895   /* We're now using the new linkage.  */
10896   push_lang_context (linkage);
10897
10898   /* If the next token is a `{', then we're using the first
10899      production.  */
10900   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10901     {
10902       /* Consume the `{' token.  */
10903       cp_lexer_consume_token (parser->lexer);
10904       /* Parse the declarations.  */
10905       cp_parser_declaration_seq_opt (parser);
10906       /* Look for the closing `}'.  */
10907       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10908     }
10909   /* Otherwise, there's just one declaration.  */
10910   else
10911     {
10912       bool saved_in_unbraced_linkage_specification_p;
10913
10914       saved_in_unbraced_linkage_specification_p
10915         = parser->in_unbraced_linkage_specification_p;
10916       parser->in_unbraced_linkage_specification_p = true;
10917       cp_parser_declaration (parser);
10918       parser->in_unbraced_linkage_specification_p
10919         = saved_in_unbraced_linkage_specification_p;
10920     }
10921
10922   /* We're done with the linkage-specification.  */
10923   pop_lang_context ();
10924 }
10925
10926 /* Parse a static_assert-declaration.
10927
10928    static_assert-declaration:
10929      static_assert ( constant-expression , string-literal ) ; 
10930
10931    If MEMBER_P, this static_assert is a class member.  */
10932
10933 static void 
10934 cp_parser_static_assert(cp_parser *parser, bool member_p)
10935 {
10936   tree condition;
10937   tree message;
10938   cp_token *token;
10939   location_t saved_loc;
10940   bool dummy;
10941
10942   /* Peek at the `static_assert' token so we can keep track of exactly
10943      where the static assertion started.  */
10944   token = cp_lexer_peek_token (parser->lexer);
10945   saved_loc = token->location;
10946
10947   /* Look for the `static_assert' keyword.  */
10948   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10949                                   RT_STATIC_ASSERT))
10950     return;
10951
10952   /*  We know we are in a static assertion; commit to any tentative
10953       parse.  */
10954   if (cp_parser_parsing_tentatively (parser))
10955     cp_parser_commit_to_tentative_parse (parser);
10956
10957   /* Parse the `(' starting the static assertion condition.  */
10958   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10959
10960   /* Parse the constant-expression.  Allow a non-constant expression
10961      here in order to give better diagnostics in finish_static_assert.  */
10962   condition = 
10963     cp_parser_constant_expression (parser,
10964                                    /*allow_non_constant_p=*/true,
10965                                    /*non_constant_p=*/&dummy);
10966
10967   /* Parse the separating `,'.  */
10968   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10969
10970   /* Parse the string-literal message.  */
10971   message = cp_parser_string_literal (parser, 
10972                                       /*translate=*/false,
10973                                       /*wide_ok=*/true);
10974
10975   /* A `)' completes the static assertion.  */
10976   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10977     cp_parser_skip_to_closing_parenthesis (parser, 
10978                                            /*recovering=*/true, 
10979                                            /*or_comma=*/false,
10980                                            /*consume_paren=*/true);
10981
10982   /* A semicolon terminates the declaration.  */
10983   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10984
10985   /* Complete the static assertion, which may mean either processing 
10986      the static assert now or saving it for template instantiation.  */
10987   finish_static_assert (condition, message, saved_loc, member_p);
10988 }
10989
10990 /* Parse a `decltype' type. Returns the type. 
10991
10992    simple-type-specifier:
10993      decltype ( expression )  */
10994
10995 static tree
10996 cp_parser_decltype (cp_parser *parser)
10997 {
10998   tree expr;
10999   bool id_expression_or_member_access_p = false;
11000   const char *saved_message;
11001   bool saved_integral_constant_expression_p;
11002   bool saved_non_integral_constant_expression_p;
11003   cp_token *id_expr_start_token;
11004   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11005
11006   if (start_token->type == CPP_DECLTYPE)
11007     {
11008       /* Already parsed.  */
11009       cp_lexer_consume_token (parser->lexer);
11010       return start_token->u.value;
11011     }
11012
11013   /* Look for the `decltype' token.  */
11014   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
11015     return error_mark_node;
11016
11017   /* Types cannot be defined in a `decltype' expression.  Save away the
11018      old message.  */
11019   saved_message = parser->type_definition_forbidden_message;
11020
11021   /* And create the new one.  */
11022   parser->type_definition_forbidden_message
11023     = G_("types may not be defined in %<decltype%> expressions");
11024
11025   /* The restrictions on constant-expressions do not apply inside
11026      decltype expressions.  */
11027   saved_integral_constant_expression_p
11028     = parser->integral_constant_expression_p;
11029   saved_non_integral_constant_expression_p
11030     = parser->non_integral_constant_expression_p;
11031   parser->integral_constant_expression_p = false;
11032
11033   /* Do not actually evaluate the expression.  */
11034   ++cp_unevaluated_operand;
11035
11036   /* Do not warn about problems with the expression.  */
11037   ++c_inhibit_evaluation_warnings;
11038
11039   /* Parse the opening `('.  */
11040   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11041     return error_mark_node;
11042   
11043   /* First, try parsing an id-expression.  */
11044   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11045   cp_parser_parse_tentatively (parser);
11046   expr = cp_parser_id_expression (parser,
11047                                   /*template_keyword_p=*/false,
11048                                   /*check_dependency_p=*/true,
11049                                   /*template_p=*/NULL,
11050                                   /*declarator_p=*/false,
11051                                   /*optional_p=*/false);
11052
11053   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11054     {
11055       bool non_integral_constant_expression_p = false;
11056       tree id_expression = expr;
11057       cp_id_kind idk;
11058       const char *error_msg;
11059
11060       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11061         /* Lookup the name we got back from the id-expression.  */
11062         expr = cp_parser_lookup_name (parser, expr,
11063                                       none_type,
11064                                       /*is_template=*/false,
11065                                       /*is_namespace=*/false,
11066                                       /*check_dependency=*/true,
11067                                       /*ambiguous_decls=*/NULL,
11068                                       id_expr_start_token->location);
11069
11070       if (expr
11071           && expr != error_mark_node
11072           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11073           && TREE_CODE (expr) != TYPE_DECL
11074           && (TREE_CODE (expr) != BIT_NOT_EXPR
11075               || !TYPE_P (TREE_OPERAND (expr, 0)))
11076           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11077         {
11078           /* Complete lookup of the id-expression.  */
11079           expr = (finish_id_expression
11080                   (id_expression, expr, parser->scope, &idk,
11081                    /*integral_constant_expression_p=*/false,
11082                    /*allow_non_integral_constant_expression_p=*/true,
11083                    &non_integral_constant_expression_p,
11084                    /*template_p=*/false,
11085                    /*done=*/true,
11086                    /*address_p=*/false,
11087                    /*template_arg_p=*/false,
11088                    &error_msg,
11089                    id_expr_start_token->location));
11090
11091           if (expr == error_mark_node)
11092             /* We found an id-expression, but it was something that we
11093                should not have found. This is an error, not something
11094                we can recover from, so note that we found an
11095                id-expression and we'll recover as gracefully as
11096                possible.  */
11097             id_expression_or_member_access_p = true;
11098         }
11099
11100       if (expr 
11101           && expr != error_mark_node
11102           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11103         /* We have an id-expression.  */
11104         id_expression_or_member_access_p = true;
11105     }
11106
11107   if (!id_expression_or_member_access_p)
11108     {
11109       /* Abort the id-expression parse.  */
11110       cp_parser_abort_tentative_parse (parser);
11111
11112       /* Parsing tentatively, again.  */
11113       cp_parser_parse_tentatively (parser);
11114
11115       /* Parse a class member access.  */
11116       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11117                                            /*cast_p=*/false,
11118                                            /*member_access_only_p=*/true, NULL);
11119
11120       if (expr 
11121           && expr != error_mark_node
11122           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11123         /* We have an id-expression.  */
11124         id_expression_or_member_access_p = true;
11125     }
11126
11127   if (id_expression_or_member_access_p)
11128     /* We have parsed the complete id-expression or member access.  */
11129     cp_parser_parse_definitely (parser);
11130   else
11131     {
11132       bool saved_greater_than_is_operator_p;
11133
11134       /* Abort our attempt to parse an id-expression or member access
11135          expression.  */
11136       cp_parser_abort_tentative_parse (parser);
11137
11138       /* Within a parenthesized expression, a `>' token is always
11139          the greater-than operator.  */
11140       saved_greater_than_is_operator_p
11141         = parser->greater_than_is_operator_p;
11142       parser->greater_than_is_operator_p = true;
11143
11144       /* Parse a full expression.  */
11145       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11146
11147       /* The `>' token might be the end of a template-id or
11148          template-parameter-list now.  */
11149       parser->greater_than_is_operator_p
11150         = saved_greater_than_is_operator_p;
11151     }
11152
11153   /* Go back to evaluating expressions.  */
11154   --cp_unevaluated_operand;
11155   --c_inhibit_evaluation_warnings;
11156
11157   /* Restore the old message and the integral constant expression
11158      flags.  */
11159   parser->type_definition_forbidden_message = saved_message;
11160   parser->integral_constant_expression_p
11161     = saved_integral_constant_expression_p;
11162   parser->non_integral_constant_expression_p
11163     = saved_non_integral_constant_expression_p;
11164
11165   /* Parse to the closing `)'.  */
11166   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11167     {
11168       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11169                                              /*consume_paren=*/true);
11170       return error_mark_node;
11171     }
11172
11173   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11174                                tf_warning_or_error);
11175
11176   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11177      it again.  */
11178   start_token->type = CPP_DECLTYPE;
11179   start_token->u.value = expr;
11180   start_token->keyword = RID_MAX;
11181   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11182
11183   return expr;
11184 }
11185
11186 /* Special member functions [gram.special] */
11187
11188 /* Parse a conversion-function-id.
11189
11190    conversion-function-id:
11191      operator conversion-type-id
11192
11193    Returns an IDENTIFIER_NODE representing the operator.  */
11194
11195 static tree
11196 cp_parser_conversion_function_id (cp_parser* parser)
11197 {
11198   tree type;
11199   tree saved_scope;
11200   tree saved_qualifying_scope;
11201   tree saved_object_scope;
11202   tree pushed_scope = NULL_TREE;
11203
11204   /* Look for the `operator' token.  */
11205   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11206     return error_mark_node;
11207   /* When we parse the conversion-type-id, the current scope will be
11208      reset.  However, we need that information in able to look up the
11209      conversion function later, so we save it here.  */
11210   saved_scope = parser->scope;
11211   saved_qualifying_scope = parser->qualifying_scope;
11212   saved_object_scope = parser->object_scope;
11213   /* We must enter the scope of the class so that the names of
11214      entities declared within the class are available in the
11215      conversion-type-id.  For example, consider:
11216
11217        struct S {
11218          typedef int I;
11219          operator I();
11220        };
11221
11222        S::operator I() { ... }
11223
11224      In order to see that `I' is a type-name in the definition, we
11225      must be in the scope of `S'.  */
11226   if (saved_scope)
11227     pushed_scope = push_scope (saved_scope);
11228   /* Parse the conversion-type-id.  */
11229   type = cp_parser_conversion_type_id (parser);
11230   /* Leave the scope of the class, if any.  */
11231   if (pushed_scope)
11232     pop_scope (pushed_scope);
11233   /* Restore the saved scope.  */
11234   parser->scope = saved_scope;
11235   parser->qualifying_scope = saved_qualifying_scope;
11236   parser->object_scope = saved_object_scope;
11237   /* If the TYPE is invalid, indicate failure.  */
11238   if (type == error_mark_node)
11239     return error_mark_node;
11240   return mangle_conv_op_name_for_type (type);
11241 }
11242
11243 /* Parse a conversion-type-id:
11244
11245    conversion-type-id:
11246      type-specifier-seq conversion-declarator [opt]
11247
11248    Returns the TYPE specified.  */
11249
11250 static tree
11251 cp_parser_conversion_type_id (cp_parser* parser)
11252 {
11253   tree attributes;
11254   cp_decl_specifier_seq type_specifiers;
11255   cp_declarator *declarator;
11256   tree type_specified;
11257
11258   /* Parse the attributes.  */
11259   attributes = cp_parser_attributes_opt (parser);
11260   /* Parse the type-specifiers.  */
11261   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11262                                 /*is_trailing_return=*/false,
11263                                 &type_specifiers);
11264   /* If that didn't work, stop.  */
11265   if (type_specifiers.type == error_mark_node)
11266     return error_mark_node;
11267   /* Parse the conversion-declarator.  */
11268   declarator = cp_parser_conversion_declarator_opt (parser);
11269
11270   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11271                                     /*initialized=*/0, &attributes);
11272   if (attributes)
11273     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11274
11275   /* Don't give this error when parsing tentatively.  This happens to
11276      work because we always parse this definitively once.  */
11277   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11278       && type_uses_auto (type_specified))
11279     {
11280       error ("invalid use of %<auto%> in conversion operator");
11281       return error_mark_node;
11282     }
11283
11284   return type_specified;
11285 }
11286
11287 /* Parse an (optional) conversion-declarator.
11288
11289    conversion-declarator:
11290      ptr-operator conversion-declarator [opt]
11291
11292    */
11293
11294 static cp_declarator *
11295 cp_parser_conversion_declarator_opt (cp_parser* parser)
11296 {
11297   enum tree_code code;
11298   tree class_type;
11299   cp_cv_quals cv_quals;
11300
11301   /* We don't know if there's a ptr-operator next, or not.  */
11302   cp_parser_parse_tentatively (parser);
11303   /* Try the ptr-operator.  */
11304   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11305   /* If it worked, look for more conversion-declarators.  */
11306   if (cp_parser_parse_definitely (parser))
11307     {
11308       cp_declarator *declarator;
11309
11310       /* Parse another optional declarator.  */
11311       declarator = cp_parser_conversion_declarator_opt (parser);
11312
11313       return cp_parser_make_indirect_declarator
11314         (code, class_type, cv_quals, declarator);
11315    }
11316
11317   return NULL;
11318 }
11319
11320 /* Parse an (optional) ctor-initializer.
11321
11322    ctor-initializer:
11323      : mem-initializer-list
11324
11325    Returns TRUE iff the ctor-initializer was actually present.  */
11326
11327 static bool
11328 cp_parser_ctor_initializer_opt (cp_parser* parser)
11329 {
11330   /* If the next token is not a `:', then there is no
11331      ctor-initializer.  */
11332   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11333     {
11334       /* Do default initialization of any bases and members.  */
11335       if (DECL_CONSTRUCTOR_P (current_function_decl))
11336         finish_mem_initializers (NULL_TREE);
11337
11338       return false;
11339     }
11340
11341   /* Consume the `:' token.  */
11342   cp_lexer_consume_token (parser->lexer);
11343   /* And the mem-initializer-list.  */
11344   cp_parser_mem_initializer_list (parser);
11345
11346   return true;
11347 }
11348
11349 /* Parse a mem-initializer-list.
11350
11351    mem-initializer-list:
11352      mem-initializer ... [opt]
11353      mem-initializer ... [opt] , mem-initializer-list  */
11354
11355 static void
11356 cp_parser_mem_initializer_list (cp_parser* parser)
11357 {
11358   tree mem_initializer_list = NULL_TREE;
11359   tree target_ctor = error_mark_node;
11360   cp_token *token = cp_lexer_peek_token (parser->lexer);
11361
11362   /* Let the semantic analysis code know that we are starting the
11363      mem-initializer-list.  */
11364   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11365     error_at (token->location,
11366               "only constructors take member initializers");
11367
11368   /* Loop through the list.  */
11369   while (true)
11370     {
11371       tree mem_initializer;
11372
11373       token = cp_lexer_peek_token (parser->lexer);
11374       /* Parse the mem-initializer.  */
11375       mem_initializer = cp_parser_mem_initializer (parser);
11376       /* If the next token is a `...', we're expanding member initializers. */
11377       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11378         {
11379           /* Consume the `...'. */
11380           cp_lexer_consume_token (parser->lexer);
11381
11382           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11383              can be expanded but members cannot. */
11384           if (mem_initializer != error_mark_node
11385               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11386             {
11387               error_at (token->location,
11388                         "cannot expand initializer for member %<%D%>",
11389                         TREE_PURPOSE (mem_initializer));
11390               mem_initializer = error_mark_node;
11391             }
11392
11393           /* Construct the pack expansion type. */
11394           if (mem_initializer != error_mark_node)
11395             mem_initializer = make_pack_expansion (mem_initializer);
11396         }
11397       if (target_ctor != error_mark_node
11398           && mem_initializer != error_mark_node)
11399         {
11400           error ("mem-initializer for %qD follows constructor delegation",
11401                  TREE_PURPOSE (mem_initializer));
11402           mem_initializer = error_mark_node;
11403         }
11404       /* Look for a target constructor. */
11405       if (mem_initializer != error_mark_node
11406           && TYPE_P (TREE_PURPOSE (mem_initializer))
11407           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11408         {
11409           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11410           if (mem_initializer_list)
11411             {
11412               error ("constructor delegation follows mem-initializer for %qD",
11413                      TREE_PURPOSE (mem_initializer_list));
11414               mem_initializer = error_mark_node;
11415             }
11416           target_ctor = mem_initializer;
11417         }
11418       /* Add it to the list, unless it was erroneous.  */
11419       if (mem_initializer != error_mark_node)
11420         {
11421           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11422           mem_initializer_list = mem_initializer;
11423         }
11424       /* If the next token is not a `,', we're done.  */
11425       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11426         break;
11427       /* Consume the `,' token.  */
11428       cp_lexer_consume_token (parser->lexer);
11429     }
11430
11431   /* Perform semantic analysis.  */
11432   if (DECL_CONSTRUCTOR_P (current_function_decl))
11433     finish_mem_initializers (mem_initializer_list);
11434 }
11435
11436 /* Parse a mem-initializer.
11437
11438    mem-initializer:
11439      mem-initializer-id ( expression-list [opt] )
11440      mem-initializer-id braced-init-list
11441
11442    GNU extension:
11443
11444    mem-initializer:
11445      ( expression-list [opt] )
11446
11447    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11448    class) or FIELD_DECL (for a non-static data member) to initialize;
11449    the TREE_VALUE is the expression-list.  An empty initialization
11450    list is represented by void_list_node.  */
11451
11452 static tree
11453 cp_parser_mem_initializer (cp_parser* parser)
11454 {
11455   tree mem_initializer_id;
11456   tree expression_list;
11457   tree member;
11458   cp_token *token = cp_lexer_peek_token (parser->lexer);
11459
11460   /* Find out what is being initialized.  */
11461   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11462     {
11463       permerror (token->location,
11464                  "anachronistic old-style base class initializer");
11465       mem_initializer_id = NULL_TREE;
11466     }
11467   else
11468     {
11469       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11470       if (mem_initializer_id == error_mark_node)
11471         return mem_initializer_id;
11472     }
11473   member = expand_member_init (mem_initializer_id);
11474   if (member && !DECL_P (member))
11475     in_base_initializer = 1;
11476
11477   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11478     {
11479       bool expr_non_constant_p;
11480       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11481       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11482       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11483       expression_list = build_tree_list (NULL_TREE, expression_list);
11484     }
11485   else
11486     {
11487       VEC(tree,gc)* vec;
11488       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11489                                                      /*cast_p=*/false,
11490                                                      /*allow_expansion_p=*/true,
11491                                                      /*non_constant_p=*/NULL);
11492       if (vec == NULL)
11493         return error_mark_node;
11494       expression_list = build_tree_list_vec (vec);
11495       release_tree_vector (vec);
11496     }
11497
11498   if (expression_list == error_mark_node)
11499     return error_mark_node;
11500   if (!expression_list)
11501     expression_list = void_type_node;
11502
11503   in_base_initializer = 0;
11504
11505   return member ? build_tree_list (member, expression_list) : error_mark_node;
11506 }
11507
11508 /* Parse a mem-initializer-id.
11509
11510    mem-initializer-id:
11511      :: [opt] nested-name-specifier [opt] class-name
11512      identifier
11513
11514    Returns a TYPE indicating the class to be initializer for the first
11515    production.  Returns an IDENTIFIER_NODE indicating the data member
11516    to be initialized for the second production.  */
11517
11518 static tree
11519 cp_parser_mem_initializer_id (cp_parser* parser)
11520 {
11521   bool global_scope_p;
11522   bool nested_name_specifier_p;
11523   bool template_p = false;
11524   tree id;
11525
11526   cp_token *token = cp_lexer_peek_token (parser->lexer);
11527
11528   /* `typename' is not allowed in this context ([temp.res]).  */
11529   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11530     {
11531       error_at (token->location, 
11532                 "keyword %<typename%> not allowed in this context (a qualified "
11533                 "member initializer is implicitly a type)");
11534       cp_lexer_consume_token (parser->lexer);
11535     }
11536   /* Look for the optional `::' operator.  */
11537   global_scope_p
11538     = (cp_parser_global_scope_opt (parser,
11539                                    /*current_scope_valid_p=*/false)
11540        != NULL_TREE);
11541   /* Look for the optional nested-name-specifier.  The simplest way to
11542      implement:
11543
11544        [temp.res]
11545
11546        The keyword `typename' is not permitted in a base-specifier or
11547        mem-initializer; in these contexts a qualified name that
11548        depends on a template-parameter is implicitly assumed to be a
11549        type name.
11550
11551      is to assume that we have seen the `typename' keyword at this
11552      point.  */
11553   nested_name_specifier_p
11554     = (cp_parser_nested_name_specifier_opt (parser,
11555                                             /*typename_keyword_p=*/true,
11556                                             /*check_dependency_p=*/true,
11557                                             /*type_p=*/true,
11558                                             /*is_declaration=*/true)
11559        != NULL_TREE);
11560   if (nested_name_specifier_p)
11561     template_p = cp_parser_optional_template_keyword (parser);
11562   /* If there is a `::' operator or a nested-name-specifier, then we
11563      are definitely looking for a class-name.  */
11564   if (global_scope_p || nested_name_specifier_p)
11565     return cp_parser_class_name (parser,
11566                                  /*typename_keyword_p=*/true,
11567                                  /*template_keyword_p=*/template_p,
11568                                  typename_type,
11569                                  /*check_dependency_p=*/true,
11570                                  /*class_head_p=*/false,
11571                                  /*is_declaration=*/true);
11572   /* Otherwise, we could also be looking for an ordinary identifier.  */
11573   cp_parser_parse_tentatively (parser);
11574   /* Try a class-name.  */
11575   id = cp_parser_class_name (parser,
11576                              /*typename_keyword_p=*/true,
11577                              /*template_keyword_p=*/false,
11578                              none_type,
11579                              /*check_dependency_p=*/true,
11580                              /*class_head_p=*/false,
11581                              /*is_declaration=*/true);
11582   /* If we found one, we're done.  */
11583   if (cp_parser_parse_definitely (parser))
11584     return id;
11585   /* Otherwise, look for an ordinary identifier.  */
11586   return cp_parser_identifier (parser);
11587 }
11588
11589 /* Overloading [gram.over] */
11590
11591 /* Parse an operator-function-id.
11592
11593    operator-function-id:
11594      operator operator
11595
11596    Returns an IDENTIFIER_NODE for the operator which is a
11597    human-readable spelling of the identifier, e.g., `operator +'.  */
11598
11599 static tree
11600 cp_parser_operator_function_id (cp_parser* parser)
11601 {
11602   /* Look for the `operator' keyword.  */
11603   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11604     return error_mark_node;
11605   /* And then the name of the operator itself.  */
11606   return cp_parser_operator (parser);
11607 }
11608
11609 /* Return an identifier node for a user-defined literal operator.
11610    The suffix identifier is chained to the operator name identifier.  */
11611
11612 static tree
11613 cp_literal_operator_id (const char* name)
11614 {
11615   tree identifier;
11616   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11617                               + strlen (name) + 10);
11618   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11619   identifier = get_identifier (buffer);
11620   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11621
11622   return identifier;
11623 }
11624
11625 /* Parse an operator.
11626
11627    operator:
11628      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11629      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11630      || ++ -- , ->* -> () []
11631
11632    GNU Extensions:
11633
11634    operator:
11635      <? >? <?= >?=
11636
11637    Returns an IDENTIFIER_NODE for the operator which is a
11638    human-readable spelling of the identifier, e.g., `operator +'.  */
11639
11640 static tree
11641 cp_parser_operator (cp_parser* parser)
11642 {
11643   tree id = NULL_TREE;
11644   cp_token *token;
11645
11646   /* Peek at the next token.  */
11647   token = cp_lexer_peek_token (parser->lexer);
11648   /* Figure out which operator we have.  */
11649   switch (token->type)
11650     {
11651     case CPP_KEYWORD:
11652       {
11653         enum tree_code op;
11654
11655         /* The keyword should be either `new' or `delete'.  */
11656         if (token->keyword == RID_NEW)
11657           op = NEW_EXPR;
11658         else if (token->keyword == RID_DELETE)
11659           op = DELETE_EXPR;
11660         else
11661           break;
11662
11663         /* Consume the `new' or `delete' token.  */
11664         cp_lexer_consume_token (parser->lexer);
11665
11666         /* Peek at the next token.  */
11667         token = cp_lexer_peek_token (parser->lexer);
11668         /* If it's a `[' token then this is the array variant of the
11669            operator.  */
11670         if (token->type == CPP_OPEN_SQUARE)
11671           {
11672             /* Consume the `[' token.  */
11673             cp_lexer_consume_token (parser->lexer);
11674             /* Look for the `]' token.  */
11675             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11676             id = ansi_opname (op == NEW_EXPR
11677                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11678           }
11679         /* Otherwise, we have the non-array variant.  */
11680         else
11681           id = ansi_opname (op);
11682
11683         return id;
11684       }
11685
11686     case CPP_PLUS:
11687       id = ansi_opname (PLUS_EXPR);
11688       break;
11689
11690     case CPP_MINUS:
11691       id = ansi_opname (MINUS_EXPR);
11692       break;
11693
11694     case CPP_MULT:
11695       id = ansi_opname (MULT_EXPR);
11696       break;
11697
11698     case CPP_DIV:
11699       id = ansi_opname (TRUNC_DIV_EXPR);
11700       break;
11701
11702     case CPP_MOD:
11703       id = ansi_opname (TRUNC_MOD_EXPR);
11704       break;
11705
11706     case CPP_XOR:
11707       id = ansi_opname (BIT_XOR_EXPR);
11708       break;
11709
11710     case CPP_AND:
11711       id = ansi_opname (BIT_AND_EXPR);
11712       break;
11713
11714     case CPP_OR:
11715       id = ansi_opname (BIT_IOR_EXPR);
11716       break;
11717
11718     case CPP_COMPL:
11719       id = ansi_opname (BIT_NOT_EXPR);
11720       break;
11721
11722     case CPP_NOT:
11723       id = ansi_opname (TRUTH_NOT_EXPR);
11724       break;
11725
11726     case CPP_EQ:
11727       id = ansi_assopname (NOP_EXPR);
11728       break;
11729
11730     case CPP_LESS:
11731       id = ansi_opname (LT_EXPR);
11732       break;
11733
11734     case CPP_GREATER:
11735       id = ansi_opname (GT_EXPR);
11736       break;
11737
11738     case CPP_PLUS_EQ:
11739       id = ansi_assopname (PLUS_EXPR);
11740       break;
11741
11742     case CPP_MINUS_EQ:
11743       id = ansi_assopname (MINUS_EXPR);
11744       break;
11745
11746     case CPP_MULT_EQ:
11747       id = ansi_assopname (MULT_EXPR);
11748       break;
11749
11750     case CPP_DIV_EQ:
11751       id = ansi_assopname (TRUNC_DIV_EXPR);
11752       break;
11753
11754     case CPP_MOD_EQ:
11755       id = ansi_assopname (TRUNC_MOD_EXPR);
11756       break;
11757
11758     case CPP_XOR_EQ:
11759       id = ansi_assopname (BIT_XOR_EXPR);
11760       break;
11761
11762     case CPP_AND_EQ:
11763       id = ansi_assopname (BIT_AND_EXPR);
11764       break;
11765
11766     case CPP_OR_EQ:
11767       id = ansi_assopname (BIT_IOR_EXPR);
11768       break;
11769
11770     case CPP_LSHIFT:
11771       id = ansi_opname (LSHIFT_EXPR);
11772       break;
11773
11774     case CPP_RSHIFT:
11775       id = ansi_opname (RSHIFT_EXPR);
11776       break;
11777
11778     case CPP_LSHIFT_EQ:
11779       id = ansi_assopname (LSHIFT_EXPR);
11780       break;
11781
11782     case CPP_RSHIFT_EQ:
11783       id = ansi_assopname (RSHIFT_EXPR);
11784       break;
11785
11786     case CPP_EQ_EQ:
11787       id = ansi_opname (EQ_EXPR);
11788       break;
11789
11790     case CPP_NOT_EQ:
11791       id = ansi_opname (NE_EXPR);
11792       break;
11793
11794     case CPP_LESS_EQ:
11795       id = ansi_opname (LE_EXPR);
11796       break;
11797
11798     case CPP_GREATER_EQ:
11799       id = ansi_opname (GE_EXPR);
11800       break;
11801
11802     case CPP_AND_AND:
11803       id = ansi_opname (TRUTH_ANDIF_EXPR);
11804       break;
11805
11806     case CPP_OR_OR:
11807       id = ansi_opname (TRUTH_ORIF_EXPR);
11808       break;
11809
11810     case CPP_PLUS_PLUS:
11811       id = ansi_opname (POSTINCREMENT_EXPR);
11812       break;
11813
11814     case CPP_MINUS_MINUS:
11815       id = ansi_opname (PREDECREMENT_EXPR);
11816       break;
11817
11818     case CPP_COMMA:
11819       id = ansi_opname (COMPOUND_EXPR);
11820       break;
11821
11822     case CPP_DEREF_STAR:
11823       id = ansi_opname (MEMBER_REF);
11824       break;
11825
11826     case CPP_DEREF:
11827       id = ansi_opname (COMPONENT_REF);
11828       break;
11829
11830     case CPP_OPEN_PAREN:
11831       /* Consume the `('.  */
11832       cp_lexer_consume_token (parser->lexer);
11833       /* Look for the matching `)'.  */
11834       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11835       return ansi_opname (CALL_EXPR);
11836
11837     case CPP_OPEN_SQUARE:
11838       /* Consume the `['.  */
11839       cp_lexer_consume_token (parser->lexer);
11840       /* Look for the matching `]'.  */
11841       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11842       return ansi_opname (ARRAY_REF);
11843
11844     case CPP_STRING:
11845       if (cxx_dialect == cxx98)
11846         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11847       if (TREE_STRING_LENGTH (token->u.value) > 2)
11848         {
11849           error ("expected empty string after %<operator%> keyword");
11850           return error_mark_node;
11851         }
11852       /* Consume the string.  */
11853       cp_lexer_consume_token (parser->lexer);
11854       /* Look for the suffix identifier.  */
11855       token = cp_lexer_peek_token (parser->lexer);
11856       if (token->type == CPP_NAME)
11857         {
11858           id = cp_parser_identifier (parser);
11859           if (id != error_mark_node)
11860             {
11861               const char *name = IDENTIFIER_POINTER (id);
11862               return cp_literal_operator_id (name);
11863             }
11864         }
11865       else
11866         {
11867           error ("expected suffix identifier");
11868           return error_mark_node;
11869         }
11870
11871     case CPP_STRING_USERDEF:
11872       error ("missing space between %<\"\"%> and suffix identifier");
11873       return error_mark_node;
11874
11875     default:
11876       /* Anything else is an error.  */
11877       break;
11878     }
11879
11880   /* If we have selected an identifier, we need to consume the
11881      operator token.  */
11882   if (id)
11883     cp_lexer_consume_token (parser->lexer);
11884   /* Otherwise, no valid operator name was present.  */
11885   else
11886     {
11887       cp_parser_error (parser, "expected operator");
11888       id = error_mark_node;
11889     }
11890
11891   return id;
11892 }
11893
11894 /* Parse a template-declaration.
11895
11896    template-declaration:
11897      export [opt] template < template-parameter-list > declaration
11898
11899    If MEMBER_P is TRUE, this template-declaration occurs within a
11900    class-specifier.
11901
11902    The grammar rule given by the standard isn't correct.  What
11903    is really meant is:
11904
11905    template-declaration:
11906      export [opt] template-parameter-list-seq
11907        decl-specifier-seq [opt] init-declarator [opt] ;
11908      export [opt] template-parameter-list-seq
11909        function-definition
11910
11911    template-parameter-list-seq:
11912      template-parameter-list-seq [opt]
11913      template < template-parameter-list >  */
11914
11915 static void
11916 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11917 {
11918   /* Check for `export'.  */
11919   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11920     {
11921       /* Consume the `export' token.  */
11922       cp_lexer_consume_token (parser->lexer);
11923       /* Warn that we do not support `export'.  */
11924       warning (0, "keyword %<export%> not implemented, and will be ignored");
11925     }
11926
11927   cp_parser_template_declaration_after_export (parser, member_p);
11928 }
11929
11930 /* Parse a template-parameter-list.
11931
11932    template-parameter-list:
11933      template-parameter
11934      template-parameter-list , template-parameter
11935
11936    Returns a TREE_LIST.  Each node represents a template parameter.
11937    The nodes are connected via their TREE_CHAINs.  */
11938
11939 static tree
11940 cp_parser_template_parameter_list (cp_parser* parser)
11941 {
11942   tree parameter_list = NULL_TREE;
11943
11944   begin_template_parm_list ();
11945
11946   /* The loop below parses the template parms.  We first need to know
11947      the total number of template parms to be able to compute proper
11948      canonical types of each dependent type. So after the loop, when
11949      we know the total number of template parms,
11950      end_template_parm_list computes the proper canonical types and
11951      fixes up the dependent types accordingly.  */
11952   while (true)
11953     {
11954       tree parameter;
11955       bool is_non_type;
11956       bool is_parameter_pack;
11957       location_t parm_loc;
11958
11959       /* Parse the template-parameter.  */
11960       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11961       parameter = cp_parser_template_parameter (parser, 
11962                                                 &is_non_type,
11963                                                 &is_parameter_pack);
11964       /* Add it to the list.  */
11965       if (parameter != error_mark_node)
11966         parameter_list = process_template_parm (parameter_list,
11967                                                 parm_loc,
11968                                                 parameter,
11969                                                 is_non_type,
11970                                                 is_parameter_pack,
11971                                                 0);
11972       else
11973        {
11974          tree err_parm = build_tree_list (parameter, parameter);
11975          parameter_list = chainon (parameter_list, err_parm);
11976        }
11977
11978       /* If the next token is not a `,', we're done.  */
11979       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11980         break;
11981       /* Otherwise, consume the `,' token.  */
11982       cp_lexer_consume_token (parser->lexer);
11983     }
11984
11985   return end_template_parm_list (parameter_list);
11986 }
11987
11988 /* Parse a template-parameter.
11989
11990    template-parameter:
11991      type-parameter
11992      parameter-declaration
11993
11994    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11995    the parameter.  The TREE_PURPOSE is the default value, if any.
11996    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11997    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11998    set to true iff this parameter is a parameter pack. */
11999
12000 static tree
12001 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
12002                               bool *is_parameter_pack)
12003 {
12004   cp_token *token;
12005   cp_parameter_declarator *parameter_declarator;
12006   cp_declarator *id_declarator;
12007   tree parm;
12008
12009   /* Assume it is a type parameter or a template parameter.  */
12010   *is_non_type = false;
12011   /* Assume it not a parameter pack. */
12012   *is_parameter_pack = false;
12013   /* Peek at the next token.  */
12014   token = cp_lexer_peek_token (parser->lexer);
12015   /* If it is `class' or `template', we have a type-parameter.  */
12016   if (token->keyword == RID_TEMPLATE)
12017     return cp_parser_type_parameter (parser, is_parameter_pack);
12018   /* If it is `class' or `typename' we do not know yet whether it is a
12019      type parameter or a non-type parameter.  Consider:
12020
12021        template <typename T, typename T::X X> ...
12022
12023      or:
12024
12025        template <class C, class D*> ...
12026
12027      Here, the first parameter is a type parameter, and the second is
12028      a non-type parameter.  We can tell by looking at the token after
12029      the identifier -- if it is a `,', `=', or `>' then we have a type
12030      parameter.  */
12031   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12032     {
12033       /* Peek at the token after `class' or `typename'.  */
12034       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12035       /* If it's an ellipsis, we have a template type parameter
12036          pack. */
12037       if (token->type == CPP_ELLIPSIS)
12038         return cp_parser_type_parameter (parser, is_parameter_pack);
12039       /* If it's an identifier, skip it.  */
12040       if (token->type == CPP_NAME)
12041         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12042       /* Now, see if the token looks like the end of a template
12043          parameter.  */
12044       if (token->type == CPP_COMMA
12045           || token->type == CPP_EQ
12046           || token->type == CPP_GREATER)
12047         return cp_parser_type_parameter (parser, is_parameter_pack);
12048     }
12049
12050   /* Otherwise, it is a non-type parameter.
12051
12052      [temp.param]
12053
12054      When parsing a default template-argument for a non-type
12055      template-parameter, the first non-nested `>' is taken as the end
12056      of the template parameter-list rather than a greater-than
12057      operator.  */
12058   *is_non_type = true;
12059   parameter_declarator
12060      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12061                                         /*parenthesized_p=*/NULL);
12062
12063   /* If the parameter declaration is marked as a parameter pack, set
12064      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12065      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12066      grokdeclarator. */
12067   if (parameter_declarator
12068       && parameter_declarator->declarator
12069       && parameter_declarator->declarator->parameter_pack_p)
12070     {
12071       *is_parameter_pack = true;
12072       parameter_declarator->declarator->parameter_pack_p = false;
12073     }
12074
12075   /* If the next token is an ellipsis, and we don't already have it
12076      marked as a parameter pack, then we have a parameter pack (that
12077      has no declarator).  */
12078   if (!*is_parameter_pack
12079       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12080       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12081     {
12082       /* Consume the `...'.  */
12083       cp_lexer_consume_token (parser->lexer);
12084       maybe_warn_variadic_templates ();
12085       
12086       *is_parameter_pack = true;
12087     }
12088   /* We might end up with a pack expansion as the type of the non-type
12089      template parameter, in which case this is a non-type template
12090      parameter pack.  */
12091   else if (parameter_declarator
12092            && parameter_declarator->decl_specifiers.type
12093            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12094     {
12095       *is_parameter_pack = true;
12096       parameter_declarator->decl_specifiers.type = 
12097         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12098     }
12099
12100   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12101     {
12102       /* Parameter packs cannot have default arguments.  However, a
12103          user may try to do so, so we'll parse them and give an
12104          appropriate diagnostic here.  */
12105
12106       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12107       
12108       /* Find the name of the parameter pack.  */     
12109       id_declarator = parameter_declarator->declarator;
12110       while (id_declarator && id_declarator->kind != cdk_id)
12111         id_declarator = id_declarator->declarator;
12112       
12113       if (id_declarator && id_declarator->kind == cdk_id)
12114         error_at (start_token->location,
12115                   "template parameter pack %qD cannot have a default argument",
12116                   id_declarator->u.id.unqualified_name);
12117       else
12118         error_at (start_token->location,
12119                   "template parameter pack cannot have a default argument");
12120       
12121       /* Parse the default argument, but throw away the result.  */
12122       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12123     }
12124
12125   parm = grokdeclarator (parameter_declarator->declarator,
12126                          &parameter_declarator->decl_specifiers,
12127                          TPARM, /*initialized=*/0,
12128                          /*attrlist=*/NULL);
12129   if (parm == error_mark_node)
12130     return error_mark_node;
12131
12132   return build_tree_list (parameter_declarator->default_argument, parm);
12133 }
12134
12135 /* Parse a type-parameter.
12136
12137    type-parameter:
12138      class identifier [opt]
12139      class identifier [opt] = type-id
12140      typename identifier [opt]
12141      typename identifier [opt] = type-id
12142      template < template-parameter-list > class identifier [opt]
12143      template < template-parameter-list > class identifier [opt]
12144        = id-expression
12145
12146    GNU Extension (variadic templates):
12147
12148    type-parameter:
12149      class ... identifier [opt]
12150      typename ... identifier [opt]
12151
12152    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12153    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12154    the declaration of the parameter.
12155
12156    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12157
12158 static tree
12159 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12160 {
12161   cp_token *token;
12162   tree parameter;
12163
12164   /* Look for a keyword to tell us what kind of parameter this is.  */
12165   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12166   if (!token)
12167     return error_mark_node;
12168
12169   switch (token->keyword)
12170     {
12171     case RID_CLASS:
12172     case RID_TYPENAME:
12173       {
12174         tree identifier;
12175         tree default_argument;
12176
12177         /* If the next token is an ellipsis, we have a template
12178            argument pack. */
12179         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12180           {
12181             /* Consume the `...' token. */
12182             cp_lexer_consume_token (parser->lexer);
12183             maybe_warn_variadic_templates ();
12184
12185             *is_parameter_pack = true;
12186           }
12187
12188         /* If the next token is an identifier, then it names the
12189            parameter.  */
12190         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12191           identifier = cp_parser_identifier (parser);
12192         else
12193           identifier = NULL_TREE;
12194
12195         /* Create the parameter.  */
12196         parameter = finish_template_type_parm (class_type_node, identifier);
12197
12198         /* If the next token is an `=', we have a default argument.  */
12199         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12200           {
12201             /* Consume the `=' token.  */
12202             cp_lexer_consume_token (parser->lexer);
12203             /* Parse the default-argument.  */
12204             push_deferring_access_checks (dk_no_deferred);
12205             default_argument = cp_parser_type_id (parser);
12206
12207             /* Template parameter packs cannot have default
12208                arguments. */
12209             if (*is_parameter_pack)
12210               {
12211                 if (identifier)
12212                   error_at (token->location,
12213                             "template parameter pack %qD cannot have a "
12214                             "default argument", identifier);
12215                 else
12216                   error_at (token->location,
12217                             "template parameter packs cannot have "
12218                             "default arguments");
12219                 default_argument = NULL_TREE;
12220               }
12221             pop_deferring_access_checks ();
12222           }
12223         else
12224           default_argument = NULL_TREE;
12225
12226         /* Create the combined representation of the parameter and the
12227            default argument.  */
12228         parameter = build_tree_list (default_argument, parameter);
12229       }
12230       break;
12231
12232     case RID_TEMPLATE:
12233       {
12234         tree identifier;
12235         tree default_argument;
12236
12237         /* Look for the `<'.  */
12238         cp_parser_require (parser, CPP_LESS, RT_LESS);
12239         /* Parse the template-parameter-list.  */
12240         cp_parser_template_parameter_list (parser);
12241         /* Look for the `>'.  */
12242         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12243         /* Look for the `class' keyword.  */
12244         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12245         /* If the next token is an ellipsis, we have a template
12246            argument pack. */
12247         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12248           {
12249             /* Consume the `...' token. */
12250             cp_lexer_consume_token (parser->lexer);
12251             maybe_warn_variadic_templates ();
12252
12253             *is_parameter_pack = true;
12254           }
12255         /* If the next token is an `=', then there is a
12256            default-argument.  If the next token is a `>', we are at
12257            the end of the parameter-list.  If the next token is a `,',
12258            then we are at the end of this parameter.  */
12259         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12260             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12261             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12262           {
12263             identifier = cp_parser_identifier (parser);
12264             /* Treat invalid names as if the parameter were nameless.  */
12265             if (identifier == error_mark_node)
12266               identifier = NULL_TREE;
12267           }
12268         else
12269           identifier = NULL_TREE;
12270
12271         /* Create the template parameter.  */
12272         parameter = finish_template_template_parm (class_type_node,
12273                                                    identifier);
12274
12275         /* If the next token is an `=', then there is a
12276            default-argument.  */
12277         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12278           {
12279             bool is_template;
12280
12281             /* Consume the `='.  */
12282             cp_lexer_consume_token (parser->lexer);
12283             /* Parse the id-expression.  */
12284             push_deferring_access_checks (dk_no_deferred);
12285             /* save token before parsing the id-expression, for error
12286                reporting */
12287             token = cp_lexer_peek_token (parser->lexer);
12288             default_argument
12289               = cp_parser_id_expression (parser,
12290                                          /*template_keyword_p=*/false,
12291                                          /*check_dependency_p=*/true,
12292                                          /*template_p=*/&is_template,
12293                                          /*declarator_p=*/false,
12294                                          /*optional_p=*/false);
12295             if (TREE_CODE (default_argument) == TYPE_DECL)
12296               /* If the id-expression was a template-id that refers to
12297                  a template-class, we already have the declaration here,
12298                  so no further lookup is needed.  */
12299                  ;
12300             else
12301               /* Look up the name.  */
12302               default_argument
12303                 = cp_parser_lookup_name (parser, default_argument,
12304                                          none_type,
12305                                          /*is_template=*/is_template,
12306                                          /*is_namespace=*/false,
12307                                          /*check_dependency=*/true,
12308                                          /*ambiguous_decls=*/NULL,
12309                                          token->location);
12310             /* See if the default argument is valid.  */
12311             default_argument
12312               = check_template_template_default_arg (default_argument);
12313
12314             /* Template parameter packs cannot have default
12315                arguments. */
12316             if (*is_parameter_pack)
12317               {
12318                 if (identifier)
12319                   error_at (token->location,
12320                             "template parameter pack %qD cannot "
12321                             "have a default argument",
12322                             identifier);
12323                 else
12324                   error_at (token->location, "template parameter packs cannot "
12325                             "have default arguments");
12326                 default_argument = NULL_TREE;
12327               }
12328             pop_deferring_access_checks ();
12329           }
12330         else
12331           default_argument = NULL_TREE;
12332
12333         /* Create the combined representation of the parameter and the
12334            default argument.  */
12335         parameter = build_tree_list (default_argument, parameter);
12336       }
12337       break;
12338
12339     default:
12340       gcc_unreachable ();
12341       break;
12342     }
12343
12344   return parameter;
12345 }
12346
12347 /* Parse a template-id.
12348
12349    template-id:
12350      template-name < template-argument-list [opt] >
12351
12352    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12353    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12354    returned.  Otherwise, if the template-name names a function, or set
12355    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12356    names a class, returns a TYPE_DECL for the specialization.
12357
12358    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12359    uninstantiated templates.  */
12360
12361 static tree
12362 cp_parser_template_id (cp_parser *parser,
12363                        bool template_keyword_p,
12364                        bool check_dependency_p,
12365                        bool is_declaration)
12366 {
12367   int i;
12368   tree templ;
12369   tree arguments;
12370   tree template_id;
12371   cp_token_position start_of_id = 0;
12372   deferred_access_check *chk;
12373   VEC (deferred_access_check,gc) *access_check;
12374   cp_token *next_token = NULL, *next_token_2 = NULL;
12375   bool is_identifier;
12376
12377   /* If the next token corresponds to a template-id, there is no need
12378      to reparse it.  */
12379   next_token = cp_lexer_peek_token (parser->lexer);
12380   if (next_token->type == CPP_TEMPLATE_ID)
12381     {
12382       struct tree_check *check_value;
12383
12384       /* Get the stored value.  */
12385       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12386       /* Perform any access checks that were deferred.  */
12387       access_check = check_value->checks;
12388       if (access_check)
12389         {
12390           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12391             perform_or_defer_access_check (chk->binfo,
12392                                            chk->decl,
12393                                            chk->diag_decl);
12394         }
12395       /* Return the stored value.  */
12396       return check_value->value;
12397     }
12398
12399   /* Avoid performing name lookup if there is no possibility of
12400      finding a template-id.  */
12401   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12402       || (next_token->type == CPP_NAME
12403           && !cp_parser_nth_token_starts_template_argument_list_p
12404                (parser, 2)))
12405     {
12406       cp_parser_error (parser, "expected template-id");
12407       return error_mark_node;
12408     }
12409
12410   /* Remember where the template-id starts.  */
12411   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12412     start_of_id = cp_lexer_token_position (parser->lexer, false);
12413
12414   push_deferring_access_checks (dk_deferred);
12415
12416   /* Parse the template-name.  */
12417   is_identifier = false;
12418   templ = cp_parser_template_name (parser, template_keyword_p,
12419                                    check_dependency_p,
12420                                    is_declaration,
12421                                    &is_identifier);
12422   if (templ == error_mark_node || is_identifier)
12423     {
12424       pop_deferring_access_checks ();
12425       return templ;
12426     }
12427
12428   /* If we find the sequence `[:' after a template-name, it's probably
12429      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12430      parse correctly the argument list.  */
12431   next_token = cp_lexer_peek_token (parser->lexer);
12432   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12433   if (next_token->type == CPP_OPEN_SQUARE
12434       && next_token->flags & DIGRAPH
12435       && next_token_2->type == CPP_COLON
12436       && !(next_token_2->flags & PREV_WHITE))
12437     {
12438       cp_parser_parse_tentatively (parser);
12439       /* Change `:' into `::'.  */
12440       next_token_2->type = CPP_SCOPE;
12441       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12442          CPP_LESS.  */
12443       cp_lexer_consume_token (parser->lexer);
12444
12445       /* Parse the arguments.  */
12446       arguments = cp_parser_enclosed_template_argument_list (parser);
12447       if (!cp_parser_parse_definitely (parser))
12448         {
12449           /* If we couldn't parse an argument list, then we revert our changes
12450              and return simply an error. Maybe this is not a template-id
12451              after all.  */
12452           next_token_2->type = CPP_COLON;
12453           cp_parser_error (parser, "expected %<<%>");
12454           pop_deferring_access_checks ();
12455           return error_mark_node;
12456         }
12457       /* Otherwise, emit an error about the invalid digraph, but continue
12458          parsing because we got our argument list.  */
12459       if (permerror (next_token->location,
12460                      "%<<::%> cannot begin a template-argument list"))
12461         {
12462           static bool hint = false;
12463           inform (next_token->location,
12464                   "%<<:%> is an alternate spelling for %<[%>."
12465                   " Insert whitespace between %<<%> and %<::%>");
12466           if (!hint && !flag_permissive)
12467             {
12468               inform (next_token->location, "(if you use %<-fpermissive%>"
12469                       " G++ will accept your code)");
12470               hint = true;
12471             }
12472         }
12473     }
12474   else
12475     {
12476       /* Look for the `<' that starts the template-argument-list.  */
12477       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12478         {
12479           pop_deferring_access_checks ();
12480           return error_mark_node;
12481         }
12482       /* Parse the arguments.  */
12483       arguments = cp_parser_enclosed_template_argument_list (parser);
12484     }
12485
12486   /* Build a representation of the specialization.  */
12487   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12488     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12489   else if (DECL_TYPE_TEMPLATE_P (templ)
12490            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12491     {
12492       bool entering_scope;
12493       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12494          template (rather than some instantiation thereof) only if
12495          is not nested within some other construct.  For example, in
12496          "template <typename T> void f(T) { A<T>::", A<T> is just an
12497          instantiation of A.  */
12498       entering_scope = (template_parm_scope_p ()
12499                         && cp_lexer_next_token_is (parser->lexer,
12500                                                    CPP_SCOPE));
12501       template_id
12502         = finish_template_type (templ, arguments, entering_scope);
12503     }
12504   else
12505     {
12506       /* If it's not a class-template or a template-template, it should be
12507          a function-template.  */
12508       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12509                    || TREE_CODE (templ) == OVERLOAD
12510                    || BASELINK_P (templ)));
12511
12512       template_id = lookup_template_function (templ, arguments);
12513     }
12514
12515   /* If parsing tentatively, replace the sequence of tokens that makes
12516      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12517      should we re-parse the token stream, we will not have to repeat
12518      the effort required to do the parse, nor will we issue duplicate
12519      error messages about problems during instantiation of the
12520      template.  */
12521   if (start_of_id)
12522     {
12523       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12524
12525       /* Reset the contents of the START_OF_ID token.  */
12526       token->type = CPP_TEMPLATE_ID;
12527       /* Retrieve any deferred checks.  Do not pop this access checks yet
12528          so the memory will not be reclaimed during token replacing below.  */
12529       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12530       token->u.tree_check_value->value = template_id;
12531       token->u.tree_check_value->checks = get_deferred_access_checks ();
12532       token->keyword = RID_MAX;
12533
12534       /* Purge all subsequent tokens.  */
12535       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12536
12537       /* ??? Can we actually assume that, if template_id ==
12538          error_mark_node, we will have issued a diagnostic to the
12539          user, as opposed to simply marking the tentative parse as
12540          failed?  */
12541       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12542         error_at (token->location, "parse error in template argument list");
12543     }
12544
12545   pop_deferring_access_checks ();
12546   return template_id;
12547 }
12548
12549 /* Parse a template-name.
12550
12551    template-name:
12552      identifier
12553
12554    The standard should actually say:
12555
12556    template-name:
12557      identifier
12558      operator-function-id
12559
12560    A defect report has been filed about this issue.
12561
12562    A conversion-function-id cannot be a template name because they cannot
12563    be part of a template-id. In fact, looking at this code:
12564
12565    a.operator K<int>()
12566
12567    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12568    It is impossible to call a templated conversion-function-id with an
12569    explicit argument list, since the only allowed template parameter is
12570    the type to which it is converting.
12571
12572    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12573    `template' keyword, in a construction like:
12574
12575      T::template f<3>()
12576
12577    In that case `f' is taken to be a template-name, even though there
12578    is no way of knowing for sure.
12579
12580    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12581    name refers to a set of overloaded functions, at least one of which
12582    is a template, or an IDENTIFIER_NODE with the name of the template,
12583    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12584    names are looked up inside uninstantiated templates.  */
12585
12586 static tree
12587 cp_parser_template_name (cp_parser* parser,
12588                          bool template_keyword_p,
12589                          bool check_dependency_p,
12590                          bool is_declaration,
12591                          bool *is_identifier)
12592 {
12593   tree identifier;
12594   tree decl;
12595   tree fns;
12596   cp_token *token = cp_lexer_peek_token (parser->lexer);
12597
12598   /* If the next token is `operator', then we have either an
12599      operator-function-id or a conversion-function-id.  */
12600   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12601     {
12602       /* We don't know whether we're looking at an
12603          operator-function-id or a conversion-function-id.  */
12604       cp_parser_parse_tentatively (parser);
12605       /* Try an operator-function-id.  */
12606       identifier = cp_parser_operator_function_id (parser);
12607       /* If that didn't work, try a conversion-function-id.  */
12608       if (!cp_parser_parse_definitely (parser))
12609         {
12610           cp_parser_error (parser, "expected template-name");
12611           return error_mark_node;
12612         }
12613     }
12614   /* Look for the identifier.  */
12615   else
12616     identifier = cp_parser_identifier (parser);
12617
12618   /* If we didn't find an identifier, we don't have a template-id.  */
12619   if (identifier == error_mark_node)
12620     return error_mark_node;
12621
12622   /* If the name immediately followed the `template' keyword, then it
12623      is a template-name.  However, if the next token is not `<', then
12624      we do not treat it as a template-name, since it is not being used
12625      as part of a template-id.  This enables us to handle constructs
12626      like:
12627
12628        template <typename T> struct S { S(); };
12629        template <typename T> S<T>::S();
12630
12631      correctly.  We would treat `S' as a template -- if it were `S<T>'
12632      -- but we do not if there is no `<'.  */
12633
12634   if (processing_template_decl
12635       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12636     {
12637       /* In a declaration, in a dependent context, we pretend that the
12638          "template" keyword was present in order to improve error
12639          recovery.  For example, given:
12640
12641            template <typename T> void f(T::X<int>);
12642
12643          we want to treat "X<int>" as a template-id.  */
12644       if (is_declaration
12645           && !template_keyword_p
12646           && parser->scope && TYPE_P (parser->scope)
12647           && check_dependency_p
12648           && dependent_scope_p (parser->scope)
12649           /* Do not do this for dtors (or ctors), since they never
12650              need the template keyword before their name.  */
12651           && !constructor_name_p (identifier, parser->scope))
12652         {
12653           cp_token_position start = 0;
12654
12655           /* Explain what went wrong.  */
12656           error_at (token->location, "non-template %qD used as template",
12657                     identifier);
12658           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12659                   parser->scope, identifier);
12660           /* If parsing tentatively, find the location of the "<" token.  */
12661           if (cp_parser_simulate_error (parser))
12662             start = cp_lexer_token_position (parser->lexer, true);
12663           /* Parse the template arguments so that we can issue error
12664              messages about them.  */
12665           cp_lexer_consume_token (parser->lexer);
12666           cp_parser_enclosed_template_argument_list (parser);
12667           /* Skip tokens until we find a good place from which to
12668              continue parsing.  */
12669           cp_parser_skip_to_closing_parenthesis (parser,
12670                                                  /*recovering=*/true,
12671                                                  /*or_comma=*/true,
12672                                                  /*consume_paren=*/false);
12673           /* If parsing tentatively, permanently remove the
12674              template argument list.  That will prevent duplicate
12675              error messages from being issued about the missing
12676              "template" keyword.  */
12677           if (start)
12678             cp_lexer_purge_tokens_after (parser->lexer, start);
12679           if (is_identifier)
12680             *is_identifier = true;
12681           return identifier;
12682         }
12683
12684       /* If the "template" keyword is present, then there is generally
12685          no point in doing name-lookup, so we just return IDENTIFIER.
12686          But, if the qualifying scope is non-dependent then we can
12687          (and must) do name-lookup normally.  */
12688       if (template_keyword_p
12689           && (!parser->scope
12690               || (TYPE_P (parser->scope)
12691                   && dependent_type_p (parser->scope))))
12692         return identifier;
12693     }
12694
12695   /* Look up the name.  */
12696   decl = cp_parser_lookup_name (parser, identifier,
12697                                 none_type,
12698                                 /*is_template=*/true,
12699                                 /*is_namespace=*/false,
12700                                 check_dependency_p,
12701                                 /*ambiguous_decls=*/NULL,
12702                                 token->location);
12703
12704   /* If DECL is a template, then the name was a template-name.  */
12705   if (TREE_CODE (decl) == TEMPLATE_DECL)
12706     ;
12707   else
12708     {
12709       tree fn = NULL_TREE;
12710
12711       /* The standard does not explicitly indicate whether a name that
12712          names a set of overloaded declarations, some of which are
12713          templates, is a template-name.  However, such a name should
12714          be a template-name; otherwise, there is no way to form a
12715          template-id for the overloaded templates.  */
12716       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12717       if (TREE_CODE (fns) == OVERLOAD)
12718         for (fn = fns; fn; fn = OVL_NEXT (fn))
12719           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12720             break;
12721
12722       if (!fn)
12723         {
12724           /* The name does not name a template.  */
12725           cp_parser_error (parser, "expected template-name");
12726           return error_mark_node;
12727         }
12728     }
12729
12730   /* If DECL is dependent, and refers to a function, then just return
12731      its name; we will look it up again during template instantiation.  */
12732   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12733     {
12734       tree scope = ovl_scope (decl);
12735       if (TYPE_P (scope) && dependent_type_p (scope))
12736         return identifier;
12737     }
12738
12739   return decl;
12740 }
12741
12742 /* Parse a template-argument-list.
12743
12744    template-argument-list:
12745      template-argument ... [opt]
12746      template-argument-list , template-argument ... [opt]
12747
12748    Returns a TREE_VEC containing the arguments.  */
12749
12750 static tree
12751 cp_parser_template_argument_list (cp_parser* parser)
12752 {
12753   tree fixed_args[10];
12754   unsigned n_args = 0;
12755   unsigned alloced = 10;
12756   tree *arg_ary = fixed_args;
12757   tree vec;
12758   bool saved_in_template_argument_list_p;
12759   bool saved_ice_p;
12760   bool saved_non_ice_p;
12761
12762   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12763   parser->in_template_argument_list_p = true;
12764   /* Even if the template-id appears in an integral
12765      constant-expression, the contents of the argument list do
12766      not.  */
12767   saved_ice_p = parser->integral_constant_expression_p;
12768   parser->integral_constant_expression_p = false;
12769   saved_non_ice_p = parser->non_integral_constant_expression_p;
12770   parser->non_integral_constant_expression_p = false;
12771
12772   /* Parse the arguments.  */
12773   do
12774     {
12775       tree argument;
12776
12777       if (n_args)
12778         /* Consume the comma.  */
12779         cp_lexer_consume_token (parser->lexer);
12780
12781       /* Parse the template-argument.  */
12782       argument = cp_parser_template_argument (parser);
12783
12784       /* If the next token is an ellipsis, we're expanding a template
12785          argument pack. */
12786       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12787         {
12788           if (argument == error_mark_node)
12789             {
12790               cp_token *token = cp_lexer_peek_token (parser->lexer);
12791               error_at (token->location,
12792                         "expected parameter pack before %<...%>");
12793             }
12794           /* Consume the `...' token. */
12795           cp_lexer_consume_token (parser->lexer);
12796
12797           /* Make the argument into a TYPE_PACK_EXPANSION or
12798              EXPR_PACK_EXPANSION. */
12799           argument = make_pack_expansion (argument);
12800         }
12801
12802       if (n_args == alloced)
12803         {
12804           alloced *= 2;
12805
12806           if (arg_ary == fixed_args)
12807             {
12808               arg_ary = XNEWVEC (tree, alloced);
12809               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12810             }
12811           else
12812             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12813         }
12814       arg_ary[n_args++] = argument;
12815     }
12816   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12817
12818   vec = make_tree_vec (n_args);
12819
12820   while (n_args--)
12821     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12822
12823   if (arg_ary != fixed_args)
12824     free (arg_ary);
12825   parser->non_integral_constant_expression_p = saved_non_ice_p;
12826   parser->integral_constant_expression_p = saved_ice_p;
12827   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12828 #ifdef ENABLE_CHECKING
12829   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12830 #endif
12831   return vec;
12832 }
12833
12834 /* Parse a template-argument.
12835
12836    template-argument:
12837      assignment-expression
12838      type-id
12839      id-expression
12840
12841    The representation is that of an assignment-expression, type-id, or
12842    id-expression -- except that the qualified id-expression is
12843    evaluated, so that the value returned is either a DECL or an
12844    OVERLOAD.
12845
12846    Although the standard says "assignment-expression", it forbids
12847    throw-expressions or assignments in the template argument.
12848    Therefore, we use "conditional-expression" instead.  */
12849
12850 static tree
12851 cp_parser_template_argument (cp_parser* parser)
12852 {
12853   tree argument;
12854   bool template_p;
12855   bool address_p;
12856   bool maybe_type_id = false;
12857   cp_token *token = NULL, *argument_start_token = NULL;
12858   cp_id_kind idk;
12859
12860   /* There's really no way to know what we're looking at, so we just
12861      try each alternative in order.
12862
12863        [temp.arg]
12864
12865        In a template-argument, an ambiguity between a type-id and an
12866        expression is resolved to a type-id, regardless of the form of
12867        the corresponding template-parameter.
12868
12869      Therefore, we try a type-id first.  */
12870   cp_parser_parse_tentatively (parser);
12871   argument = cp_parser_template_type_arg (parser);
12872   /* If there was no error parsing the type-id but the next token is a
12873      '>>', our behavior depends on which dialect of C++ we're
12874      parsing. In C++98, we probably found a typo for '> >'. But there
12875      are type-id which are also valid expressions. For instance:
12876
12877      struct X { int operator >> (int); };
12878      template <int V> struct Foo {};
12879      Foo<X () >> 5> r;
12880
12881      Here 'X()' is a valid type-id of a function type, but the user just
12882      wanted to write the expression "X() >> 5". Thus, we remember that we
12883      found a valid type-id, but we still try to parse the argument as an
12884      expression to see what happens. 
12885
12886      In C++0x, the '>>' will be considered two separate '>'
12887      tokens.  */
12888   if (!cp_parser_error_occurred (parser)
12889       && cxx_dialect == cxx98
12890       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12891     {
12892       maybe_type_id = true;
12893       cp_parser_abort_tentative_parse (parser);
12894     }
12895   else
12896     {
12897       /* If the next token isn't a `,' or a `>', then this argument wasn't
12898       really finished. This means that the argument is not a valid
12899       type-id.  */
12900       if (!cp_parser_next_token_ends_template_argument_p (parser))
12901         cp_parser_error (parser, "expected template-argument");
12902       /* If that worked, we're done.  */
12903       if (cp_parser_parse_definitely (parser))
12904         return argument;
12905     }
12906   /* We're still not sure what the argument will be.  */
12907   cp_parser_parse_tentatively (parser);
12908   /* Try a template.  */
12909   argument_start_token = cp_lexer_peek_token (parser->lexer);
12910   argument = cp_parser_id_expression (parser,
12911                                       /*template_keyword_p=*/false,
12912                                       /*check_dependency_p=*/true,
12913                                       &template_p,
12914                                       /*declarator_p=*/false,
12915                                       /*optional_p=*/false);
12916   /* If the next token isn't a `,' or a `>', then this argument wasn't
12917      really finished.  */
12918   if (!cp_parser_next_token_ends_template_argument_p (parser))
12919     cp_parser_error (parser, "expected template-argument");
12920   if (!cp_parser_error_occurred (parser))
12921     {
12922       /* Figure out what is being referred to.  If the id-expression
12923          was for a class template specialization, then we will have a
12924          TYPE_DECL at this point.  There is no need to do name lookup
12925          at this point in that case.  */
12926       if (TREE_CODE (argument) != TYPE_DECL)
12927         argument = cp_parser_lookup_name (parser, argument,
12928                                           none_type,
12929                                           /*is_template=*/template_p,
12930                                           /*is_namespace=*/false,
12931                                           /*check_dependency=*/true,
12932                                           /*ambiguous_decls=*/NULL,
12933                                           argument_start_token->location);
12934       if (TREE_CODE (argument) != TEMPLATE_DECL
12935           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12936         cp_parser_error (parser, "expected template-name");
12937     }
12938   if (cp_parser_parse_definitely (parser))
12939     return argument;
12940   /* It must be a non-type argument.  There permitted cases are given
12941      in [temp.arg.nontype]:
12942
12943      -- an integral constant-expression of integral or enumeration
12944         type; or
12945
12946      -- the name of a non-type template-parameter; or
12947
12948      -- the name of an object or function with external linkage...
12949
12950      -- the address of an object or function with external linkage...
12951
12952      -- a pointer to member...  */
12953   /* Look for a non-type template parameter.  */
12954   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12955     {
12956       cp_parser_parse_tentatively (parser);
12957       argument = cp_parser_primary_expression (parser,
12958                                                /*address_p=*/false,
12959                                                /*cast_p=*/false,
12960                                                /*template_arg_p=*/true,
12961                                                &idk);
12962       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12963           || !cp_parser_next_token_ends_template_argument_p (parser))
12964         cp_parser_simulate_error (parser);
12965       if (cp_parser_parse_definitely (parser))
12966         return argument;
12967     }
12968
12969   /* If the next token is "&", the argument must be the address of an
12970      object or function with external linkage.  */
12971   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12972   if (address_p)
12973     cp_lexer_consume_token (parser->lexer);
12974   /* See if we might have an id-expression.  */
12975   token = cp_lexer_peek_token (parser->lexer);
12976   if (token->type == CPP_NAME
12977       || token->keyword == RID_OPERATOR
12978       || token->type == CPP_SCOPE
12979       || token->type == CPP_TEMPLATE_ID
12980       || token->type == CPP_NESTED_NAME_SPECIFIER)
12981     {
12982       cp_parser_parse_tentatively (parser);
12983       argument = cp_parser_primary_expression (parser,
12984                                                address_p,
12985                                                /*cast_p=*/false,
12986                                                /*template_arg_p=*/true,
12987                                                &idk);
12988       if (cp_parser_error_occurred (parser)
12989           || !cp_parser_next_token_ends_template_argument_p (parser))
12990         cp_parser_abort_tentative_parse (parser);
12991       else
12992         {
12993           tree probe;
12994
12995           if (TREE_CODE (argument) == INDIRECT_REF)
12996             {
12997               gcc_assert (REFERENCE_REF_P (argument));
12998               argument = TREE_OPERAND (argument, 0);
12999             }
13000
13001           /* If we're in a template, we represent a qualified-id referring
13002              to a static data member as a SCOPE_REF even if the scope isn't
13003              dependent so that we can check access control later.  */
13004           probe = argument;
13005           if (TREE_CODE (probe) == SCOPE_REF)
13006             probe = TREE_OPERAND (probe, 1);
13007           if (TREE_CODE (probe) == VAR_DECL)
13008             {
13009               /* A variable without external linkage might still be a
13010                  valid constant-expression, so no error is issued here
13011                  if the external-linkage check fails.  */
13012               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
13013                 cp_parser_simulate_error (parser);
13014             }
13015           else if (is_overloaded_fn (argument))
13016             /* All overloaded functions are allowed; if the external
13017                linkage test does not pass, an error will be issued
13018                later.  */
13019             ;
13020           else if (address_p
13021                    && (TREE_CODE (argument) == OFFSET_REF
13022                        || TREE_CODE (argument) == SCOPE_REF))
13023             /* A pointer-to-member.  */
13024             ;
13025           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13026             ;
13027           else
13028             cp_parser_simulate_error (parser);
13029
13030           if (cp_parser_parse_definitely (parser))
13031             {
13032               if (address_p)
13033                 argument = build_x_unary_op (ADDR_EXPR, argument,
13034                                              tf_warning_or_error);
13035               return argument;
13036             }
13037         }
13038     }
13039   /* If the argument started with "&", there are no other valid
13040      alternatives at this point.  */
13041   if (address_p)
13042     {
13043       cp_parser_error (parser, "invalid non-type template argument");
13044       return error_mark_node;
13045     }
13046
13047   /* If the argument wasn't successfully parsed as a type-id followed
13048      by '>>', the argument can only be a constant expression now.
13049      Otherwise, we try parsing the constant-expression tentatively,
13050      because the argument could really be a type-id.  */
13051   if (maybe_type_id)
13052     cp_parser_parse_tentatively (parser);
13053   argument = cp_parser_constant_expression (parser,
13054                                             /*allow_non_constant_p=*/false,
13055                                             /*non_constant_p=*/NULL);
13056   argument = fold_non_dependent_expr (argument);
13057   if (!maybe_type_id)
13058     return argument;
13059   if (!cp_parser_next_token_ends_template_argument_p (parser))
13060     cp_parser_error (parser, "expected template-argument");
13061   if (cp_parser_parse_definitely (parser))
13062     return argument;
13063   /* We did our best to parse the argument as a non type-id, but that
13064      was the only alternative that matched (albeit with a '>' after
13065      it). We can assume it's just a typo from the user, and a
13066      diagnostic will then be issued.  */
13067   return cp_parser_template_type_arg (parser);
13068 }
13069
13070 /* Parse an explicit-instantiation.
13071
13072    explicit-instantiation:
13073      template declaration
13074
13075    Although the standard says `declaration', what it really means is:
13076
13077    explicit-instantiation:
13078      template decl-specifier-seq [opt] declarator [opt] ;
13079
13080    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13081    supposed to be allowed.  A defect report has been filed about this
13082    issue.
13083
13084    GNU Extension:
13085
13086    explicit-instantiation:
13087      storage-class-specifier template
13088        decl-specifier-seq [opt] declarator [opt] ;
13089      function-specifier template
13090        decl-specifier-seq [opt] declarator [opt] ;  */
13091
13092 static void
13093 cp_parser_explicit_instantiation (cp_parser* parser)
13094 {
13095   int declares_class_or_enum;
13096   cp_decl_specifier_seq decl_specifiers;
13097   tree extension_specifier = NULL_TREE;
13098
13099   timevar_push (TV_TEMPLATE_INST);
13100
13101   /* Look for an (optional) storage-class-specifier or
13102      function-specifier.  */
13103   if (cp_parser_allow_gnu_extensions_p (parser))
13104     {
13105       extension_specifier
13106         = cp_parser_storage_class_specifier_opt (parser);
13107       if (!extension_specifier)
13108         extension_specifier
13109           = cp_parser_function_specifier_opt (parser,
13110                                               /*decl_specs=*/NULL);
13111     }
13112
13113   /* Look for the `template' keyword.  */
13114   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13115   /* Let the front end know that we are processing an explicit
13116      instantiation.  */
13117   begin_explicit_instantiation ();
13118   /* [temp.explicit] says that we are supposed to ignore access
13119      control while processing explicit instantiation directives.  */
13120   push_deferring_access_checks (dk_no_check);
13121   /* Parse a decl-specifier-seq.  */
13122   cp_parser_decl_specifier_seq (parser,
13123                                 CP_PARSER_FLAGS_OPTIONAL,
13124                                 &decl_specifiers,
13125                                 &declares_class_or_enum);
13126   /* If there was exactly one decl-specifier, and it declared a class,
13127      and there's no declarator, then we have an explicit type
13128      instantiation.  */
13129   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13130     {
13131       tree type;
13132
13133       type = check_tag_decl (&decl_specifiers);
13134       /* Turn access control back on for names used during
13135          template instantiation.  */
13136       pop_deferring_access_checks ();
13137       if (type)
13138         do_type_instantiation (type, extension_specifier,
13139                                /*complain=*/tf_error);
13140     }
13141   else
13142     {
13143       cp_declarator *declarator;
13144       tree decl;
13145
13146       /* Parse the declarator.  */
13147       declarator
13148         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13149                                 /*ctor_dtor_or_conv_p=*/NULL,
13150                                 /*parenthesized_p=*/NULL,
13151                                 /*member_p=*/false);
13152       if (declares_class_or_enum & 2)
13153         cp_parser_check_for_definition_in_return_type (declarator,
13154                                                        decl_specifiers.type,
13155                                                        decl_specifiers.type_location);
13156       if (declarator != cp_error_declarator)
13157         {
13158           if (decl_specifiers.specs[(int)ds_inline])
13159             permerror (input_location, "explicit instantiation shall not use"
13160                        " %<inline%> specifier");
13161           if (decl_specifiers.specs[(int)ds_constexpr])
13162             permerror (input_location, "explicit instantiation shall not use"
13163                        " %<constexpr%> specifier");
13164
13165           decl = grokdeclarator (declarator, &decl_specifiers,
13166                                  NORMAL, 0, &decl_specifiers.attributes);
13167           /* Turn access control back on for names used during
13168              template instantiation.  */
13169           pop_deferring_access_checks ();
13170           /* Do the explicit instantiation.  */
13171           do_decl_instantiation (decl, extension_specifier);
13172         }
13173       else
13174         {
13175           pop_deferring_access_checks ();
13176           /* Skip the body of the explicit instantiation.  */
13177           cp_parser_skip_to_end_of_statement (parser);
13178         }
13179     }
13180   /* We're done with the instantiation.  */
13181   end_explicit_instantiation ();
13182
13183   cp_parser_consume_semicolon_at_end_of_statement (parser);
13184
13185   timevar_pop (TV_TEMPLATE_INST);
13186 }
13187
13188 /* Parse an explicit-specialization.
13189
13190    explicit-specialization:
13191      template < > declaration
13192
13193    Although the standard says `declaration', what it really means is:
13194
13195    explicit-specialization:
13196      template <> decl-specifier [opt] init-declarator [opt] ;
13197      template <> function-definition
13198      template <> explicit-specialization
13199      template <> template-declaration  */
13200
13201 static void
13202 cp_parser_explicit_specialization (cp_parser* parser)
13203 {
13204   bool need_lang_pop;
13205   cp_token *token = cp_lexer_peek_token (parser->lexer);
13206
13207   /* Look for the `template' keyword.  */
13208   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13209   /* Look for the `<'.  */
13210   cp_parser_require (parser, CPP_LESS, RT_LESS);
13211   /* Look for the `>'.  */
13212   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13213   /* We have processed another parameter list.  */
13214   ++parser->num_template_parameter_lists;
13215   /* [temp]
13216
13217      A template ... explicit specialization ... shall not have C
13218      linkage.  */
13219   if (current_lang_name == lang_name_c)
13220     {
13221       error_at (token->location, "template specialization with C linkage");
13222       /* Give it C++ linkage to avoid confusing other parts of the
13223          front end.  */
13224       push_lang_context (lang_name_cplusplus);
13225       need_lang_pop = true;
13226     }
13227   else
13228     need_lang_pop = false;
13229   /* Let the front end know that we are beginning a specialization.  */
13230   if (!begin_specialization ())
13231     {
13232       end_specialization ();
13233       return;
13234     }
13235
13236   /* If the next keyword is `template', we need to figure out whether
13237      or not we're looking a template-declaration.  */
13238   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13239     {
13240       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13241           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13242         cp_parser_template_declaration_after_export (parser,
13243                                                      /*member_p=*/false);
13244       else
13245         cp_parser_explicit_specialization (parser);
13246     }
13247   else
13248     /* Parse the dependent declaration.  */
13249     cp_parser_single_declaration (parser,
13250                                   /*checks=*/NULL,
13251                                   /*member_p=*/false,
13252                                   /*explicit_specialization_p=*/true,
13253                                   /*friend_p=*/NULL);
13254   /* We're done with the specialization.  */
13255   end_specialization ();
13256   /* For the erroneous case of a template with C linkage, we pushed an
13257      implicit C++ linkage scope; exit that scope now.  */
13258   if (need_lang_pop)
13259     pop_lang_context ();
13260   /* We're done with this parameter list.  */
13261   --parser->num_template_parameter_lists;
13262 }
13263
13264 /* Parse a type-specifier.
13265
13266    type-specifier:
13267      simple-type-specifier
13268      class-specifier
13269      enum-specifier
13270      elaborated-type-specifier
13271      cv-qualifier
13272
13273    GNU Extension:
13274
13275    type-specifier:
13276      __complex__
13277
13278    Returns a representation of the type-specifier.  For a
13279    class-specifier, enum-specifier, or elaborated-type-specifier, a
13280    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13281
13282    The parser flags FLAGS is used to control type-specifier parsing.
13283
13284    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13285    in a decl-specifier-seq.
13286
13287    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13288    class-specifier, enum-specifier, or elaborated-type-specifier, then
13289    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13290    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13291    zero.
13292
13293    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13294    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13295    is set to FALSE.  */
13296
13297 static tree
13298 cp_parser_type_specifier (cp_parser* parser,
13299                           cp_parser_flags flags,
13300                           cp_decl_specifier_seq *decl_specs,
13301                           bool is_declaration,
13302                           int* declares_class_or_enum,
13303                           bool* is_cv_qualifier)
13304 {
13305   tree type_spec = NULL_TREE;
13306   cp_token *token;
13307   enum rid keyword;
13308   cp_decl_spec ds = ds_last;
13309
13310   /* Assume this type-specifier does not declare a new type.  */
13311   if (declares_class_or_enum)
13312     *declares_class_or_enum = 0;
13313   /* And that it does not specify a cv-qualifier.  */
13314   if (is_cv_qualifier)
13315     *is_cv_qualifier = false;
13316   /* Peek at the next token.  */
13317   token = cp_lexer_peek_token (parser->lexer);
13318
13319   /* If we're looking at a keyword, we can use that to guide the
13320      production we choose.  */
13321   keyword = token->keyword;
13322   switch (keyword)
13323     {
13324     case RID_ENUM:
13325       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13326         goto elaborated_type_specifier;
13327
13328       /* Look for the enum-specifier.  */
13329       type_spec = cp_parser_enum_specifier (parser);
13330       /* If that worked, we're done.  */
13331       if (type_spec)
13332         {
13333           if (declares_class_or_enum)
13334             *declares_class_or_enum = 2;
13335           if (decl_specs)
13336             cp_parser_set_decl_spec_type (decl_specs,
13337                                           type_spec,
13338                                           token->location,
13339                                           /*type_definition_p=*/true);
13340           return type_spec;
13341         }
13342       else
13343         goto elaborated_type_specifier;
13344
13345       /* Any of these indicate either a class-specifier, or an
13346          elaborated-type-specifier.  */
13347     case RID_CLASS:
13348     case RID_STRUCT:
13349     case RID_UNION:
13350       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13351         goto elaborated_type_specifier;
13352
13353       /* Parse tentatively so that we can back up if we don't find a
13354          class-specifier.  */
13355       cp_parser_parse_tentatively (parser);
13356       /* Look for the class-specifier.  */
13357       type_spec = cp_parser_class_specifier (parser);
13358       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13359       /* If that worked, we're done.  */
13360       if (cp_parser_parse_definitely (parser))
13361         {
13362           if (declares_class_or_enum)
13363             *declares_class_or_enum = 2;
13364           if (decl_specs)
13365             cp_parser_set_decl_spec_type (decl_specs,
13366                                           type_spec,
13367                                           token->location,
13368                                           /*type_definition_p=*/true);
13369           return type_spec;
13370         }
13371
13372       /* Fall through.  */
13373     elaborated_type_specifier:
13374       /* We're declaring (not defining) a class or enum.  */
13375       if (declares_class_or_enum)
13376         *declares_class_or_enum = 1;
13377
13378       /* Fall through.  */
13379     case RID_TYPENAME:
13380       /* Look for an elaborated-type-specifier.  */
13381       type_spec
13382         = (cp_parser_elaborated_type_specifier
13383            (parser,
13384             decl_specs && decl_specs->specs[(int) ds_friend],
13385             is_declaration));
13386       if (decl_specs)
13387         cp_parser_set_decl_spec_type (decl_specs,
13388                                       type_spec,
13389                                       token->location,
13390                                       /*type_definition_p=*/false);
13391       return type_spec;
13392
13393     case RID_CONST:
13394       ds = ds_const;
13395       if (is_cv_qualifier)
13396         *is_cv_qualifier = true;
13397       break;
13398
13399     case RID_VOLATILE:
13400       ds = ds_volatile;
13401       if (is_cv_qualifier)
13402         *is_cv_qualifier = true;
13403       break;
13404
13405     case RID_RESTRICT:
13406       ds = ds_restrict;
13407       if (is_cv_qualifier)
13408         *is_cv_qualifier = true;
13409       break;
13410
13411     case RID_COMPLEX:
13412       /* The `__complex__' keyword is a GNU extension.  */
13413       ds = ds_complex;
13414       break;
13415
13416     default:
13417       break;
13418     }
13419
13420   /* Handle simple keywords.  */
13421   if (ds != ds_last)
13422     {
13423       if (decl_specs)
13424         {
13425           ++decl_specs->specs[(int)ds];
13426           decl_specs->any_specifiers_p = true;
13427         }
13428       return cp_lexer_consume_token (parser->lexer)->u.value;
13429     }
13430
13431   /* If we do not already have a type-specifier, assume we are looking
13432      at a simple-type-specifier.  */
13433   type_spec = cp_parser_simple_type_specifier (parser,
13434                                                decl_specs,
13435                                                flags);
13436
13437   /* If we didn't find a type-specifier, and a type-specifier was not
13438      optional in this context, issue an error message.  */
13439   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13440     {
13441       cp_parser_error (parser, "expected type specifier");
13442       return error_mark_node;
13443     }
13444
13445   return type_spec;
13446 }
13447
13448 /* Parse a simple-type-specifier.
13449
13450    simple-type-specifier:
13451      :: [opt] nested-name-specifier [opt] type-name
13452      :: [opt] nested-name-specifier template template-id
13453      char
13454      wchar_t
13455      bool
13456      short
13457      int
13458      long
13459      signed
13460      unsigned
13461      float
13462      double
13463      void
13464
13465    C++0x Extension:
13466
13467    simple-type-specifier:
13468      auto
13469      decltype ( expression )   
13470      char16_t
13471      char32_t
13472      __underlying_type ( type-id )
13473
13474    GNU Extension:
13475
13476    simple-type-specifier:
13477      __int128
13478      __typeof__ unary-expression
13479      __typeof__ ( type-id )
13480
13481    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13482    appropriately updated.  */
13483
13484 static tree
13485 cp_parser_simple_type_specifier (cp_parser* parser,
13486                                  cp_decl_specifier_seq *decl_specs,
13487                                  cp_parser_flags flags)
13488 {
13489   tree type = NULL_TREE;
13490   cp_token *token;
13491
13492   /* Peek at the next token.  */
13493   token = cp_lexer_peek_token (parser->lexer);
13494
13495   /* If we're looking at a keyword, things are easy.  */
13496   switch (token->keyword)
13497     {
13498     case RID_CHAR:
13499       if (decl_specs)
13500         decl_specs->explicit_char_p = true;
13501       type = char_type_node;
13502       break;
13503     case RID_CHAR16:
13504       type = char16_type_node;
13505       break;
13506     case RID_CHAR32:
13507       type = char32_type_node;
13508       break;
13509     case RID_WCHAR:
13510       type = wchar_type_node;
13511       break;
13512     case RID_BOOL:
13513       type = boolean_type_node;
13514       break;
13515     case RID_SHORT:
13516       if (decl_specs)
13517         ++decl_specs->specs[(int) ds_short];
13518       type = short_integer_type_node;
13519       break;
13520     case RID_INT:
13521       if (decl_specs)
13522         decl_specs->explicit_int_p = true;
13523       type = integer_type_node;
13524       break;
13525     case RID_INT128:
13526       if (!int128_integer_type_node)
13527         break;
13528       if (decl_specs)
13529         decl_specs->explicit_int128_p = true;
13530       type = int128_integer_type_node;
13531       break;
13532     case RID_LONG:
13533       if (decl_specs)
13534         ++decl_specs->specs[(int) ds_long];
13535       type = long_integer_type_node;
13536       break;
13537     case RID_SIGNED:
13538       if (decl_specs)
13539         ++decl_specs->specs[(int) ds_signed];
13540       type = integer_type_node;
13541       break;
13542     case RID_UNSIGNED:
13543       if (decl_specs)
13544         ++decl_specs->specs[(int) ds_unsigned];
13545       type = unsigned_type_node;
13546       break;
13547     case RID_FLOAT:
13548       type = float_type_node;
13549       break;
13550     case RID_DOUBLE:
13551       type = double_type_node;
13552       break;
13553     case RID_VOID:
13554       type = void_type_node;
13555       break;
13556       
13557     case RID_AUTO:
13558       maybe_warn_cpp0x (CPP0X_AUTO);
13559       type = make_auto ();
13560       break;
13561
13562     case RID_DECLTYPE:
13563       /* Since DR 743, decltype can either be a simple-type-specifier by
13564          itself or begin a nested-name-specifier.  Parsing it will replace
13565          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13566          handling below decide what to do.  */
13567       cp_parser_decltype (parser);
13568       cp_lexer_set_token_position (parser->lexer, token);
13569       break;
13570
13571     case RID_TYPEOF:
13572       /* Consume the `typeof' token.  */
13573       cp_lexer_consume_token (parser->lexer);
13574       /* Parse the operand to `typeof'.  */
13575       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13576       /* If it is not already a TYPE, take its type.  */
13577       if (!TYPE_P (type))
13578         type = finish_typeof (type);
13579
13580       if (decl_specs)
13581         cp_parser_set_decl_spec_type (decl_specs, type,
13582                                       token->location,
13583                                       /*type_definition_p=*/false);
13584
13585       return type;
13586
13587     case RID_UNDERLYING_TYPE:
13588       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13589       if (decl_specs)
13590         cp_parser_set_decl_spec_type (decl_specs, type,
13591                                       token->location,
13592                                       /*type_definition_p=*/false);
13593
13594       return type;
13595
13596     case RID_BASES:
13597     case RID_DIRECT_BASES:
13598       type = cp_parser_trait_expr (parser, token->keyword);
13599       if (decl_specs)
13600        cp_parser_set_decl_spec_type (decl_specs, type,
13601                                      token->location,
13602                                      /*type_definition_p=*/false);
13603       return type;
13604     default:
13605       break;
13606     }
13607
13608   /* If token is an already-parsed decltype not followed by ::,
13609      it's a simple-type-specifier.  */
13610   if (token->type == CPP_DECLTYPE
13611       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13612     {
13613       type = token->u.value;
13614       if (decl_specs)
13615         cp_parser_set_decl_spec_type (decl_specs, type,
13616                                       token->location,
13617                                       /*type_definition_p=*/false);
13618       cp_lexer_consume_token (parser->lexer);
13619       return type;
13620     }
13621
13622   /* If the type-specifier was for a built-in type, we're done.  */
13623   if (type)
13624     {
13625       /* Record the type.  */
13626       if (decl_specs
13627           && (token->keyword != RID_SIGNED
13628               && token->keyword != RID_UNSIGNED
13629               && token->keyword != RID_SHORT
13630               && token->keyword != RID_LONG))
13631         cp_parser_set_decl_spec_type (decl_specs,
13632                                       type,
13633                                       token->location,
13634                                       /*type_definition_p=*/false);
13635       if (decl_specs)
13636         decl_specs->any_specifiers_p = true;
13637
13638       /* Consume the token.  */
13639       cp_lexer_consume_token (parser->lexer);
13640
13641       /* There is no valid C++ program where a non-template type is
13642          followed by a "<".  That usually indicates that the user thought
13643          that the type was a template.  */
13644       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13645
13646       return TYPE_NAME (type);
13647     }
13648
13649   /* The type-specifier must be a user-defined type.  */
13650   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13651     {
13652       bool qualified_p;
13653       bool global_p;
13654
13655       /* Don't gobble tokens or issue error messages if this is an
13656          optional type-specifier.  */
13657       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13658         cp_parser_parse_tentatively (parser);
13659
13660       /* Look for the optional `::' operator.  */
13661       global_p
13662         = (cp_parser_global_scope_opt (parser,
13663                                        /*current_scope_valid_p=*/false)
13664            != NULL_TREE);
13665       /* Look for the nested-name specifier.  */
13666       qualified_p
13667         = (cp_parser_nested_name_specifier_opt (parser,
13668                                                 /*typename_keyword_p=*/false,
13669                                                 /*check_dependency_p=*/true,
13670                                                 /*type_p=*/false,
13671                                                 /*is_declaration=*/false)
13672            != NULL_TREE);
13673       token = cp_lexer_peek_token (parser->lexer);
13674       /* If we have seen a nested-name-specifier, and the next token
13675          is `template', then we are using the template-id production.  */
13676       if (parser->scope
13677           && cp_parser_optional_template_keyword (parser))
13678         {
13679           /* Look for the template-id.  */
13680           type = cp_parser_template_id (parser,
13681                                         /*template_keyword_p=*/true,
13682                                         /*check_dependency_p=*/true,
13683                                         /*is_declaration=*/false);
13684           /* If the template-id did not name a type, we are out of
13685              luck.  */
13686           if (TREE_CODE (type) != TYPE_DECL)
13687             {
13688               cp_parser_error (parser, "expected template-id for type");
13689               type = NULL_TREE;
13690             }
13691         }
13692       /* Otherwise, look for a type-name.  */
13693       else
13694         type = cp_parser_type_name (parser);
13695       /* Keep track of all name-lookups performed in class scopes.  */
13696       if (type
13697           && !global_p
13698           && !qualified_p
13699           && TREE_CODE (type) == TYPE_DECL
13700           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13701         maybe_note_name_used_in_class (DECL_NAME (type), type);
13702       /* If it didn't work out, we don't have a TYPE.  */
13703       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13704           && !cp_parser_parse_definitely (parser))
13705         type = NULL_TREE;
13706       if (type && decl_specs)
13707         cp_parser_set_decl_spec_type (decl_specs, type,
13708                                       token->location,
13709                                       /*type_definition_p=*/false);
13710     }
13711
13712   /* If we didn't get a type-name, issue an error message.  */
13713   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13714     {
13715       cp_parser_error (parser, "expected type-name");
13716       return error_mark_node;
13717     }
13718
13719   if (type && type != error_mark_node)
13720     {
13721       /* See if TYPE is an Objective-C type, and if so, parse and
13722          accept any protocol references following it.  Do this before
13723          the cp_parser_check_for_invalid_template_id() call, because
13724          Objective-C types can be followed by '<...>' which would
13725          enclose protocol names rather than template arguments, and so
13726          everything is fine.  */
13727       if (c_dialect_objc () && !parser->scope
13728           && (objc_is_id (type) || objc_is_class_name (type)))
13729         {
13730           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13731           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13732
13733           /* Clobber the "unqualified" type previously entered into
13734              DECL_SPECS with the new, improved protocol-qualified version.  */
13735           if (decl_specs)
13736             decl_specs->type = qual_type;
13737
13738           return qual_type;
13739         }
13740
13741       /* There is no valid C++ program where a non-template type is
13742          followed by a "<".  That usually indicates that the user
13743          thought that the type was a template.  */
13744       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13745                                                token->location);
13746     }
13747
13748   return type;
13749 }
13750
13751 /* Parse a type-name.
13752
13753    type-name:
13754      class-name
13755      enum-name
13756      typedef-name
13757      simple-template-id [in c++0x]
13758
13759    enum-name:
13760      identifier
13761
13762    typedef-name:
13763      identifier
13764
13765    Returns a TYPE_DECL for the type.  */
13766
13767 static tree
13768 cp_parser_type_name (cp_parser* parser)
13769 {
13770   tree type_decl;
13771
13772   /* We can't know yet whether it is a class-name or not.  */
13773   cp_parser_parse_tentatively (parser);
13774   /* Try a class-name.  */
13775   type_decl = cp_parser_class_name (parser,
13776                                     /*typename_keyword_p=*/false,
13777                                     /*template_keyword_p=*/false,
13778                                     none_type,
13779                                     /*check_dependency_p=*/true,
13780                                     /*class_head_p=*/false,
13781                                     /*is_declaration=*/false);
13782   /* If it's not a class-name, keep looking.  */
13783   if (!cp_parser_parse_definitely (parser))
13784     {
13785       if (cxx_dialect < cxx0x)
13786         /* It must be a typedef-name or an enum-name.  */
13787         return cp_parser_nonclass_name (parser);
13788
13789       cp_parser_parse_tentatively (parser);
13790       /* It is either a simple-template-id representing an
13791          instantiation of an alias template...  */
13792       type_decl = cp_parser_template_id (parser,
13793                                          /*template_keyword_p=*/false,
13794                                          /*check_dependency_p=*/false,
13795                                          /*is_declaration=*/false);
13796       /* Note that this must be an instantiation of an alias template
13797          because [temp.names]/6 says:
13798          
13799              A template-id that names an alias template specialization
13800              is a type-name.
13801
13802          Whereas [temp.names]/7 says:
13803          
13804              A simple-template-id that names a class template
13805              specialization is a class-name.  */
13806       if (type_decl != NULL_TREE
13807           && TREE_CODE (type_decl) == TYPE_DECL
13808           && TYPE_DECL_ALIAS_P (type_decl))
13809         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13810       else
13811         cp_parser_simulate_error (parser);
13812
13813       if (!cp_parser_parse_definitely (parser))
13814         /* ... Or a typedef-name or an enum-name.  */
13815         return cp_parser_nonclass_name (parser);
13816     }
13817
13818   return type_decl;
13819 }
13820
13821 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13822
13823    enum-name:
13824      identifier
13825
13826    typedef-name:
13827      identifier
13828
13829    Returns a TYPE_DECL for the type.  */
13830
13831 static tree
13832 cp_parser_nonclass_name (cp_parser* parser)
13833 {
13834   tree type_decl;
13835   tree identifier;
13836
13837   cp_token *token = cp_lexer_peek_token (parser->lexer);
13838   identifier = cp_parser_identifier (parser);
13839   if (identifier == error_mark_node)
13840     return error_mark_node;
13841
13842   /* Look up the type-name.  */
13843   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13844
13845   if (TREE_CODE (type_decl) == USING_DECL)
13846     {
13847       if (!DECL_DEPENDENT_P (type_decl))
13848         type_decl = strip_using_decl (type_decl);
13849       else if (USING_DECL_TYPENAME_P (type_decl))
13850         {
13851           /* We have found a type introduced by a using
13852              declaration at class scope that refers to a dependent
13853              type.
13854              
13855              using typename :: [opt] nested-name-specifier unqualified-id ;
13856           */
13857           type_decl = make_typename_type (TREE_TYPE (type_decl),
13858                                           DECL_NAME (type_decl),
13859                                           typename_type, tf_error);
13860           if (type_decl != error_mark_node)
13861             type_decl = TYPE_NAME (type_decl);
13862         }
13863     }
13864   
13865   if (TREE_CODE (type_decl) != TYPE_DECL
13866       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13867     {
13868       /* See if this is an Objective-C type.  */
13869       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13870       tree type = objc_get_protocol_qualified_type (identifier, protos);
13871       if (type)
13872         type_decl = TYPE_NAME (type);
13873     }
13874
13875   /* Issue an error if we did not find a type-name.  */
13876   if (TREE_CODE (type_decl) != TYPE_DECL
13877       /* In Objective-C, we have the complication that class names are
13878          normally type names and start declarations (eg, the
13879          "NSObject" in "NSObject *object;"), but can be used in an
13880          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13881          is an expression.  So, a classname followed by a dot is not a
13882          valid type-name.  */
13883       || (objc_is_class_name (TREE_TYPE (type_decl))
13884           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13885     {
13886       if (!cp_parser_simulate_error (parser))
13887         cp_parser_name_lookup_error (parser, identifier, type_decl,
13888                                      NLE_TYPE, token->location);
13889       return error_mark_node;
13890     }
13891   /* Remember that the name was used in the definition of the
13892      current class so that we can check later to see if the
13893      meaning would have been different after the class was
13894      entirely defined.  */
13895   else if (type_decl != error_mark_node
13896            && !parser->scope)
13897     maybe_note_name_used_in_class (identifier, type_decl);
13898   
13899   return type_decl;
13900 }
13901
13902 /* Parse an elaborated-type-specifier.  Note that the grammar given
13903    here incorporates the resolution to DR68.
13904
13905    elaborated-type-specifier:
13906      class-key :: [opt] nested-name-specifier [opt] identifier
13907      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13908      enum-key :: [opt] nested-name-specifier [opt] identifier
13909      typename :: [opt] nested-name-specifier identifier
13910      typename :: [opt] nested-name-specifier template [opt]
13911        template-id
13912
13913    GNU extension:
13914
13915    elaborated-type-specifier:
13916      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13917      class-key attributes :: [opt] nested-name-specifier [opt]
13918                template [opt] template-id
13919      enum attributes :: [opt] nested-name-specifier [opt] identifier
13920
13921    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13922    declared `friend'.  If IS_DECLARATION is TRUE, then this
13923    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13924    something is being declared.
13925
13926    Returns the TYPE specified.  */
13927
13928 static tree
13929 cp_parser_elaborated_type_specifier (cp_parser* parser,
13930                                      bool is_friend,
13931                                      bool is_declaration)
13932 {
13933   enum tag_types tag_type;
13934   tree identifier;
13935   tree type = NULL_TREE;
13936   tree attributes = NULL_TREE;
13937   tree globalscope;
13938   cp_token *token = NULL;
13939
13940   /* See if we're looking at the `enum' keyword.  */
13941   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13942     {
13943       /* Consume the `enum' token.  */
13944       cp_lexer_consume_token (parser->lexer);
13945       /* Remember that it's an enumeration type.  */
13946       tag_type = enum_type;
13947       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13948          enums) is used here.  */
13949       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13950           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13951         {
13952             pedwarn (input_location, 0, "elaborated-type-specifier "
13953                       "for a scoped enum must not use the %<%D%> keyword",
13954                       cp_lexer_peek_token (parser->lexer)->u.value);
13955           /* Consume the `struct' or `class' and parse it anyway.  */
13956           cp_lexer_consume_token (parser->lexer);
13957         }
13958       /* Parse the attributes.  */
13959       attributes = cp_parser_attributes_opt (parser);
13960     }
13961   /* Or, it might be `typename'.  */
13962   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13963                                            RID_TYPENAME))
13964     {
13965       /* Consume the `typename' token.  */
13966       cp_lexer_consume_token (parser->lexer);
13967       /* Remember that it's a `typename' type.  */
13968       tag_type = typename_type;
13969     }
13970   /* Otherwise it must be a class-key.  */
13971   else
13972     {
13973       tag_type = cp_parser_class_key (parser);
13974       if (tag_type == none_type)
13975         return error_mark_node;
13976       /* Parse the attributes.  */
13977       attributes = cp_parser_attributes_opt (parser);
13978     }
13979
13980   /* Look for the `::' operator.  */
13981   globalscope =  cp_parser_global_scope_opt (parser,
13982                                              /*current_scope_valid_p=*/false);
13983   /* Look for the nested-name-specifier.  */
13984   if (tag_type == typename_type && !globalscope)
13985     {
13986       if (!cp_parser_nested_name_specifier (parser,
13987                                            /*typename_keyword_p=*/true,
13988                                            /*check_dependency_p=*/true,
13989                                            /*type_p=*/true,
13990                                             is_declaration))
13991         return error_mark_node;
13992     }
13993   else
13994     /* Even though `typename' is not present, the proposed resolution
13995        to Core Issue 180 says that in `class A<T>::B', `B' should be
13996        considered a type-name, even if `A<T>' is dependent.  */
13997     cp_parser_nested_name_specifier_opt (parser,
13998                                          /*typename_keyword_p=*/true,
13999                                          /*check_dependency_p=*/true,
14000                                          /*type_p=*/true,
14001                                          is_declaration);
14002  /* For everything but enumeration types, consider a template-id.
14003     For an enumeration type, consider only a plain identifier.  */
14004   if (tag_type != enum_type)
14005     {
14006       bool template_p = false;
14007       tree decl;
14008
14009       /* Allow the `template' keyword.  */
14010       template_p = cp_parser_optional_template_keyword (parser);
14011       /* If we didn't see `template', we don't know if there's a
14012          template-id or not.  */
14013       if (!template_p)
14014         cp_parser_parse_tentatively (parser);
14015       /* Parse the template-id.  */
14016       token = cp_lexer_peek_token (parser->lexer);
14017       decl = cp_parser_template_id (parser, template_p,
14018                                     /*check_dependency_p=*/true,
14019                                     is_declaration);
14020       /* If we didn't find a template-id, look for an ordinary
14021          identifier.  */
14022       if (!template_p && !cp_parser_parse_definitely (parser))
14023         ;
14024       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14025          in effect, then we must assume that, upon instantiation, the
14026          template will correspond to a class.  */
14027       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14028                && tag_type == typename_type)
14029         type = make_typename_type (parser->scope, decl,
14030                                    typename_type,
14031                                    /*complain=*/tf_error);
14032       /* If the `typename' keyword is in effect and DECL is not a type
14033          decl. Then type is non existant.   */
14034       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14035         type = NULL_TREE; 
14036       else 
14037         type = check_elaborated_type_specifier (tag_type, decl,
14038                                                 /*allow_template_p=*/true);
14039     }
14040
14041   if (!type)
14042     {
14043       token = cp_lexer_peek_token (parser->lexer);
14044       identifier = cp_parser_identifier (parser);
14045
14046       if (identifier == error_mark_node)
14047         {
14048           parser->scope = NULL_TREE;
14049           return error_mark_node;
14050         }
14051
14052       /* For a `typename', we needn't call xref_tag.  */
14053       if (tag_type == typename_type
14054           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14055         return cp_parser_make_typename_type (parser, parser->scope,
14056                                              identifier,
14057                                              token->location);
14058       /* Look up a qualified name in the usual way.  */
14059       if (parser->scope)
14060         {
14061           tree decl;
14062           tree ambiguous_decls;
14063
14064           decl = cp_parser_lookup_name (parser, identifier,
14065                                         tag_type,
14066                                         /*is_template=*/false,
14067                                         /*is_namespace=*/false,
14068                                         /*check_dependency=*/true,
14069                                         &ambiguous_decls,
14070                                         token->location);
14071
14072           /* If the lookup was ambiguous, an error will already have been
14073              issued.  */
14074           if (ambiguous_decls)
14075             return error_mark_node;
14076
14077           /* If we are parsing friend declaration, DECL may be a
14078              TEMPLATE_DECL tree node here.  However, we need to check
14079              whether this TEMPLATE_DECL results in valid code.  Consider
14080              the following example:
14081
14082                namespace N {
14083                  template <class T> class C {};
14084                }
14085                class X {
14086                  template <class T> friend class N::C; // #1, valid code
14087                };
14088                template <class T> class Y {
14089                  friend class N::C;                    // #2, invalid code
14090                };
14091
14092              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14093              name lookup of `N::C'.  We see that friend declaration must
14094              be template for the code to be valid.  Note that
14095              processing_template_decl does not work here since it is
14096              always 1 for the above two cases.  */
14097
14098           decl = (cp_parser_maybe_treat_template_as_class
14099                   (decl, /*tag_name_p=*/is_friend
14100                          && parser->num_template_parameter_lists));
14101
14102           if (TREE_CODE (decl) != TYPE_DECL)
14103             {
14104               cp_parser_diagnose_invalid_type_name (parser,
14105                                                     parser->scope,
14106                                                     identifier,
14107                                                     token->location);
14108               return error_mark_node;
14109             }
14110
14111           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14112             {
14113               bool allow_template = (parser->num_template_parameter_lists
14114                                       || DECL_SELF_REFERENCE_P (decl));
14115               type = check_elaborated_type_specifier (tag_type, decl, 
14116                                                       allow_template);
14117
14118               if (type == error_mark_node)
14119                 return error_mark_node;
14120             }
14121
14122           /* Forward declarations of nested types, such as
14123
14124                class C1::C2;
14125                class C1::C2::C3;
14126
14127              are invalid unless all components preceding the final '::'
14128              are complete.  If all enclosing types are complete, these
14129              declarations become merely pointless.
14130
14131              Invalid forward declarations of nested types are errors
14132              caught elsewhere in parsing.  Those that are pointless arrive
14133              here.  */
14134
14135           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14136               && !is_friend && !processing_explicit_instantiation)
14137             warning (0, "declaration %qD does not declare anything", decl);
14138
14139           type = TREE_TYPE (decl);
14140         }
14141       else
14142         {
14143           /* An elaborated-type-specifier sometimes introduces a new type and
14144              sometimes names an existing type.  Normally, the rule is that it
14145              introduces a new type only if there is not an existing type of
14146              the same name already in scope.  For example, given:
14147
14148                struct S {};
14149                void f() { struct S s; }
14150
14151              the `struct S' in the body of `f' is the same `struct S' as in
14152              the global scope; the existing definition is used.  However, if
14153              there were no global declaration, this would introduce a new
14154              local class named `S'.
14155
14156              An exception to this rule applies to the following code:
14157
14158                namespace N { struct S; }
14159
14160              Here, the elaborated-type-specifier names a new type
14161              unconditionally; even if there is already an `S' in the
14162              containing scope this declaration names a new type.
14163              This exception only applies if the elaborated-type-specifier
14164              forms the complete declaration:
14165
14166                [class.name]
14167
14168                A declaration consisting solely of `class-key identifier ;' is
14169                either a redeclaration of the name in the current scope or a
14170                forward declaration of the identifier as a class name.  It
14171                introduces the name into the current scope.
14172
14173              We are in this situation precisely when the next token is a `;'.
14174
14175              An exception to the exception is that a `friend' declaration does
14176              *not* name a new type; i.e., given:
14177
14178                struct S { friend struct T; };
14179
14180              `T' is not a new type in the scope of `S'.
14181
14182              Also, `new struct S' or `sizeof (struct S)' never results in the
14183              definition of a new type; a new type can only be declared in a
14184              declaration context.  */
14185
14186           tag_scope ts;
14187           bool template_p;
14188
14189           if (is_friend)
14190             /* Friends have special name lookup rules.  */
14191             ts = ts_within_enclosing_non_class;
14192           else if (is_declaration
14193                    && cp_lexer_next_token_is (parser->lexer,
14194                                               CPP_SEMICOLON))
14195             /* This is a `class-key identifier ;' */
14196             ts = ts_current;
14197           else
14198             ts = ts_global;
14199
14200           template_p =
14201             (parser->num_template_parameter_lists
14202              && (cp_parser_next_token_starts_class_definition_p (parser)
14203                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14204           /* An unqualified name was used to reference this type, so
14205              there were no qualifying templates.  */
14206           if (!cp_parser_check_template_parameters (parser,
14207                                                     /*num_templates=*/0,
14208                                                     token->location,
14209                                                     /*declarator=*/NULL))
14210             return error_mark_node;
14211           type = xref_tag (tag_type, identifier, ts, template_p);
14212         }
14213     }
14214
14215   if (type == error_mark_node)
14216     return error_mark_node;
14217
14218   /* Allow attributes on forward declarations of classes.  */
14219   if (attributes)
14220     {
14221       if (TREE_CODE (type) == TYPENAME_TYPE)
14222         warning (OPT_Wattributes,
14223                  "attributes ignored on uninstantiated type");
14224       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14225                && ! processing_explicit_instantiation)
14226         warning (OPT_Wattributes,
14227                  "attributes ignored on template instantiation");
14228       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14229         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14230       else
14231         warning (OPT_Wattributes,
14232                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14233     }
14234
14235   if (tag_type != enum_type)
14236     {
14237       /* Indicate whether this class was declared as a `class' or as a
14238          `struct'.  */
14239       if (TREE_CODE (type) == RECORD_TYPE)
14240         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14241       cp_parser_check_class_key (tag_type, type);
14242     }
14243
14244   /* A "<" cannot follow an elaborated type specifier.  If that
14245      happens, the user was probably trying to form a template-id.  */
14246   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14247
14248   return type;
14249 }
14250
14251 /* Parse an enum-specifier.
14252
14253    enum-specifier:
14254      enum-head { enumerator-list [opt] }
14255      enum-head { enumerator-list , } [C++0x]
14256
14257    enum-head:
14258      enum-key identifier [opt] enum-base [opt]
14259      enum-key nested-name-specifier identifier enum-base [opt]
14260
14261    enum-key:
14262      enum
14263      enum class   [C++0x]
14264      enum struct  [C++0x]
14265
14266    enum-base:   [C++0x]
14267      : type-specifier-seq
14268
14269    opaque-enum-specifier:
14270      enum-key identifier enum-base [opt] ;
14271
14272    GNU Extensions:
14273      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14274        { enumerator-list [opt] }attributes[opt]
14275      enum-key attributes[opt] identifier [opt] enum-base [opt]
14276        { enumerator-list, }attributes[opt] [C++0x]
14277
14278    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14279    if the token stream isn't an enum-specifier after all.  */
14280
14281 static tree
14282 cp_parser_enum_specifier (cp_parser* parser)
14283 {
14284   tree identifier;
14285   tree type = NULL_TREE;
14286   tree prev_scope;
14287   tree nested_name_specifier = NULL_TREE;
14288   tree attributes;
14289   bool scoped_enum_p = false;
14290   bool has_underlying_type = false;
14291   bool nested_being_defined = false;
14292   bool new_value_list = false;
14293   bool is_new_type = false;
14294   bool is_anonymous = false;
14295   tree underlying_type = NULL_TREE;
14296   cp_token *type_start_token = NULL;
14297   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14298
14299   parser->colon_corrects_to_scope_p = false;
14300
14301   /* Parse tentatively so that we can back up if we don't find a
14302      enum-specifier.  */
14303   cp_parser_parse_tentatively (parser);
14304
14305   /* Caller guarantees that the current token is 'enum', an identifier
14306      possibly follows, and the token after that is an opening brace.
14307      If we don't have an identifier, fabricate an anonymous name for
14308      the enumeration being defined.  */
14309   cp_lexer_consume_token (parser->lexer);
14310
14311   /* Parse the "class" or "struct", which indicates a scoped
14312      enumeration type in C++0x.  */
14313   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14314       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14315     {
14316       if (cxx_dialect < cxx0x)
14317         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14318
14319       /* Consume the `struct' or `class' token.  */
14320       cp_lexer_consume_token (parser->lexer);
14321
14322       scoped_enum_p = true;
14323     }
14324
14325   attributes = cp_parser_attributes_opt (parser);
14326
14327   /* Clear the qualification.  */
14328   parser->scope = NULL_TREE;
14329   parser->qualifying_scope = NULL_TREE;
14330   parser->object_scope = NULL_TREE;
14331
14332   /* Figure out in what scope the declaration is being placed.  */
14333   prev_scope = current_scope ();
14334
14335   type_start_token = cp_lexer_peek_token (parser->lexer);
14336
14337   push_deferring_access_checks (dk_no_check);
14338   nested_name_specifier
14339       = cp_parser_nested_name_specifier_opt (parser,
14340                                              /*typename_keyword_p=*/true,
14341                                              /*check_dependency_p=*/false,
14342                                              /*type_p=*/false,
14343                                              /*is_declaration=*/false);
14344
14345   if (nested_name_specifier)
14346     {
14347       tree name;
14348
14349       identifier = cp_parser_identifier (parser);
14350       name =  cp_parser_lookup_name (parser, identifier,
14351                                      enum_type,
14352                                      /*is_template=*/false,
14353                                      /*is_namespace=*/false,
14354                                      /*check_dependency=*/true,
14355                                      /*ambiguous_decls=*/NULL,
14356                                      input_location);
14357       if (name)
14358         {
14359           type = TREE_TYPE (name);
14360           if (TREE_CODE (type) == TYPENAME_TYPE)
14361             {
14362               /* Are template enums allowed in ISO? */
14363               if (template_parm_scope_p ())
14364                 pedwarn (type_start_token->location, OPT_pedantic,
14365                          "%qD is an enumeration template", name);
14366               /* ignore a typename reference, for it will be solved by name
14367                  in start_enum.  */
14368               type = NULL_TREE;
14369             }
14370         }
14371       else
14372         error_at (type_start_token->location,
14373                   "%qD is not an enumerator-name", identifier);
14374     }
14375   else
14376     {
14377       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14378         identifier = cp_parser_identifier (parser);
14379       else
14380         {
14381           identifier = make_anon_name ();
14382           is_anonymous = true;
14383         }
14384     }
14385   pop_deferring_access_checks ();
14386
14387   /* Check for the `:' that denotes a specified underlying type in C++0x.
14388      Note that a ':' could also indicate a bitfield width, however.  */
14389   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14390     {
14391       cp_decl_specifier_seq type_specifiers;
14392
14393       /* Consume the `:'.  */
14394       cp_lexer_consume_token (parser->lexer);
14395
14396       /* Parse the type-specifier-seq.  */
14397       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14398                                     /*is_trailing_return=*/false,
14399                                     &type_specifiers);
14400
14401       /* At this point this is surely not elaborated type specifier.  */
14402       if (!cp_parser_parse_definitely (parser))
14403         return NULL_TREE;
14404
14405       if (cxx_dialect < cxx0x)
14406         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14407
14408       has_underlying_type = true;
14409
14410       /* If that didn't work, stop.  */
14411       if (type_specifiers.type != error_mark_node)
14412         {
14413           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14414                                             /*initialized=*/0, NULL);
14415           if (underlying_type == error_mark_node)
14416             underlying_type = NULL_TREE;
14417         }
14418     }
14419
14420   /* Look for the `{' but don't consume it yet.  */
14421   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14422     {
14423       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14424         {
14425           cp_parser_error (parser, "expected %<{%>");
14426           if (has_underlying_type)
14427             {
14428               type = NULL_TREE;
14429               goto out;
14430             }
14431         }
14432       /* An opaque-enum-specifier must have a ';' here.  */
14433       if ((scoped_enum_p || underlying_type)
14434           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14435         {
14436           cp_parser_error (parser, "expected %<;%> or %<{%>");
14437           if (has_underlying_type)
14438             {
14439               type = NULL_TREE;
14440               goto out;
14441             }
14442         }
14443     }
14444
14445   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14446     return NULL_TREE;
14447
14448   if (nested_name_specifier)
14449     {
14450       if (CLASS_TYPE_P (nested_name_specifier))
14451         {
14452           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14453           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14454           push_scope (nested_name_specifier);
14455         }
14456       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14457         {
14458           push_nested_namespace (nested_name_specifier);
14459         }
14460     }
14461
14462   /* Issue an error message if type-definitions are forbidden here.  */
14463   if (!cp_parser_check_type_definition (parser))
14464     type = error_mark_node;
14465   else
14466     /* Create the new type.  We do this before consuming the opening
14467        brace so the enum will be recorded as being on the line of its
14468        tag (or the 'enum' keyword, if there is no tag).  */
14469     type = start_enum (identifier, type, underlying_type,
14470                        scoped_enum_p, &is_new_type);
14471
14472   /* If the next token is not '{' it is an opaque-enum-specifier or an
14473      elaborated-type-specifier.  */
14474   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14475     {
14476       timevar_push (TV_PARSE_ENUM);
14477       if (nested_name_specifier)
14478         {
14479           /* The following catches invalid code such as:
14480              enum class S<int>::E { A, B, C }; */
14481           if (!processing_specialization
14482               && CLASS_TYPE_P (nested_name_specifier)
14483               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14484             error_at (type_start_token->location, "cannot add an enumerator "
14485                       "list to a template instantiation");
14486
14487           /* If that scope does not contain the scope in which the
14488              class was originally declared, the program is invalid.  */
14489           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14490             {
14491               if (at_namespace_scope_p ())
14492                 error_at (type_start_token->location,
14493                           "declaration of %qD in namespace %qD which does not "
14494                           "enclose %qD",
14495                           type, prev_scope, nested_name_specifier);
14496               else
14497                 error_at (type_start_token->location,
14498                           "declaration of %qD in %qD which does not enclose %qD",
14499                           type, prev_scope, nested_name_specifier);
14500               type = error_mark_node;
14501             }
14502         }
14503
14504       if (scoped_enum_p)
14505         begin_scope (sk_scoped_enum, type);
14506
14507       /* Consume the opening brace.  */
14508       cp_lexer_consume_token (parser->lexer);
14509
14510       if (type == error_mark_node)
14511         ; /* Nothing to add */
14512       else if (OPAQUE_ENUM_P (type)
14513                || (cxx_dialect > cxx98 && processing_specialization))
14514         {
14515           new_value_list = true;
14516           SET_OPAQUE_ENUM_P (type, false);
14517           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14518         }
14519       else
14520         {
14521           error_at (type_start_token->location, "multiple definition of %q#T", type);
14522           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14523                     "previous definition here");
14524           type = error_mark_node;
14525         }
14526
14527       if (type == error_mark_node)
14528         cp_parser_skip_to_end_of_block_or_statement (parser);
14529       /* If the next token is not '}', then there are some enumerators.  */
14530       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14531         cp_parser_enumerator_list (parser, type);
14532
14533       /* Consume the final '}'.  */
14534       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14535
14536       if (scoped_enum_p)
14537         finish_scope ();
14538       timevar_pop (TV_PARSE_ENUM);
14539     }
14540   else
14541     {
14542       /* If a ';' follows, then it is an opaque-enum-specifier
14543         and additional restrictions apply.  */
14544       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14545         {
14546           if (is_anonymous)
14547             error_at (type_start_token->location,
14548                       "opaque-enum-specifier without name");
14549           else if (nested_name_specifier)
14550             error_at (type_start_token->location,
14551                       "opaque-enum-specifier must use a simple identifier");
14552         }
14553     }
14554
14555   /* Look for trailing attributes to apply to this enumeration, and
14556      apply them if appropriate.  */
14557   if (cp_parser_allow_gnu_extensions_p (parser))
14558     {
14559       tree trailing_attr = cp_parser_attributes_opt (parser);
14560       trailing_attr = chainon (trailing_attr, attributes);
14561       cplus_decl_attributes (&type,
14562                              trailing_attr,
14563                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14564     }
14565
14566   /* Finish up the enumeration.  */
14567   if (type != error_mark_node)
14568     {
14569       if (new_value_list)
14570         finish_enum_value_list (type);
14571       if (is_new_type)
14572         finish_enum (type);
14573     }
14574
14575   if (nested_name_specifier)
14576     {
14577       if (CLASS_TYPE_P (nested_name_specifier))
14578         {
14579           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14580           pop_scope (nested_name_specifier);
14581         }
14582       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14583         {
14584           pop_nested_namespace (nested_name_specifier);
14585         }
14586     }
14587  out:
14588   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14589   return type;
14590 }
14591
14592 /* Parse an enumerator-list.  The enumerators all have the indicated
14593    TYPE.
14594
14595    enumerator-list:
14596      enumerator-definition
14597      enumerator-list , enumerator-definition  */
14598
14599 static void
14600 cp_parser_enumerator_list (cp_parser* parser, tree type)
14601 {
14602   while (true)
14603     {
14604       /* Parse an enumerator-definition.  */
14605       cp_parser_enumerator_definition (parser, type);
14606
14607       /* If the next token is not a ',', we've reached the end of
14608          the list.  */
14609       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14610         break;
14611       /* Otherwise, consume the `,' and keep going.  */
14612       cp_lexer_consume_token (parser->lexer);
14613       /* If the next token is a `}', there is a trailing comma.  */
14614       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14615         {
14616           if (cxx_dialect < cxx0x && !in_system_header)
14617             pedwarn (input_location, OPT_pedantic,
14618                      "comma at end of enumerator list");
14619           break;
14620         }
14621     }
14622 }
14623
14624 /* Parse an enumerator-definition.  The enumerator has the indicated
14625    TYPE.
14626
14627    enumerator-definition:
14628      enumerator
14629      enumerator = constant-expression
14630
14631    enumerator:
14632      identifier  */
14633
14634 static void
14635 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14636 {
14637   tree identifier;
14638   tree value;
14639   location_t loc;
14640
14641   /* Save the input location because we are interested in the location
14642      of the identifier and not the location of the explicit value.  */
14643   loc = cp_lexer_peek_token (parser->lexer)->location;
14644
14645   /* Look for the identifier.  */
14646   identifier = cp_parser_identifier (parser);
14647   if (identifier == error_mark_node)
14648     return;
14649
14650   /* If the next token is an '=', then there is an explicit value.  */
14651   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14652     {
14653       /* Consume the `=' token.  */
14654       cp_lexer_consume_token (parser->lexer);
14655       /* Parse the value.  */
14656       value = cp_parser_constant_expression (parser,
14657                                              /*allow_non_constant_p=*/false,
14658                                              NULL);
14659     }
14660   else
14661     value = NULL_TREE;
14662
14663   /* If we are processing a template, make sure the initializer of the
14664      enumerator doesn't contain any bare template parameter pack.  */
14665   if (check_for_bare_parameter_packs (value))
14666     value = error_mark_node;
14667
14668   /* integral_constant_value will pull out this expression, so make sure
14669      it's folded as appropriate.  */
14670   value = fold_non_dependent_expr (value);
14671
14672   /* Create the enumerator.  */
14673   build_enumerator (identifier, value, type, loc);
14674 }
14675
14676 /* Parse a namespace-name.
14677
14678    namespace-name:
14679      original-namespace-name
14680      namespace-alias
14681
14682    Returns the NAMESPACE_DECL for the namespace.  */
14683
14684 static tree
14685 cp_parser_namespace_name (cp_parser* parser)
14686 {
14687   tree identifier;
14688   tree namespace_decl;
14689
14690   cp_token *token = cp_lexer_peek_token (parser->lexer);
14691
14692   /* Get the name of the namespace.  */
14693   identifier = cp_parser_identifier (parser);
14694   if (identifier == error_mark_node)
14695     return error_mark_node;
14696
14697   /* Look up the identifier in the currently active scope.  Look only
14698      for namespaces, due to:
14699
14700        [basic.lookup.udir]
14701
14702        When looking up a namespace-name in a using-directive or alias
14703        definition, only namespace names are considered.
14704
14705      And:
14706
14707        [basic.lookup.qual]
14708
14709        During the lookup of a name preceding the :: scope resolution
14710        operator, object, function, and enumerator names are ignored.
14711
14712      (Note that cp_parser_qualifying_entity only calls this
14713      function if the token after the name is the scope resolution
14714      operator.)  */
14715   namespace_decl = cp_parser_lookup_name (parser, identifier,
14716                                           none_type,
14717                                           /*is_template=*/false,
14718                                           /*is_namespace=*/true,
14719                                           /*check_dependency=*/true,
14720                                           /*ambiguous_decls=*/NULL,
14721                                           token->location);
14722   /* If it's not a namespace, issue an error.  */
14723   if (namespace_decl == error_mark_node
14724       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14725     {
14726       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14727         error_at (token->location, "%qD is not a namespace-name", identifier);
14728       cp_parser_error (parser, "expected namespace-name");
14729       namespace_decl = error_mark_node;
14730     }
14731
14732   return namespace_decl;
14733 }
14734
14735 /* Parse a namespace-definition.
14736
14737    namespace-definition:
14738      named-namespace-definition
14739      unnamed-namespace-definition
14740
14741    named-namespace-definition:
14742      original-namespace-definition
14743      extension-namespace-definition
14744
14745    original-namespace-definition:
14746      namespace identifier { namespace-body }
14747
14748    extension-namespace-definition:
14749      namespace original-namespace-name { namespace-body }
14750
14751    unnamed-namespace-definition:
14752      namespace { namespace-body } */
14753
14754 static void
14755 cp_parser_namespace_definition (cp_parser* parser)
14756 {
14757   tree identifier, attribs;
14758   bool has_visibility;
14759   bool is_inline;
14760
14761   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14762     {
14763       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14764       is_inline = true;
14765       cp_lexer_consume_token (parser->lexer);
14766     }
14767   else
14768     is_inline = false;
14769
14770   /* Look for the `namespace' keyword.  */
14771   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14772
14773   /* Get the name of the namespace.  We do not attempt to distinguish
14774      between an original-namespace-definition and an
14775      extension-namespace-definition at this point.  The semantic
14776      analysis routines are responsible for that.  */
14777   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14778     identifier = cp_parser_identifier (parser);
14779   else
14780     identifier = NULL_TREE;
14781
14782   /* Parse any specified attributes.  */
14783   attribs = cp_parser_attributes_opt (parser);
14784
14785   /* Look for the `{' to start the namespace.  */
14786   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14787   /* Start the namespace.  */
14788   push_namespace (identifier);
14789
14790   /* "inline namespace" is equivalent to a stub namespace definition
14791      followed by a strong using directive.  */
14792   if (is_inline)
14793     {
14794       tree name_space = current_namespace;
14795       /* Set up namespace association.  */
14796       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14797         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14798                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14799       /* Import the contents of the inline namespace.  */
14800       pop_namespace ();
14801       do_using_directive (name_space);
14802       push_namespace (identifier);
14803     }
14804
14805   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14806
14807   /* Parse the body of the namespace.  */
14808   cp_parser_namespace_body (parser);
14809
14810   if (has_visibility)
14811     pop_visibility (1);
14812
14813   /* Finish the namespace.  */
14814   pop_namespace ();
14815   /* Look for the final `}'.  */
14816   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14817 }
14818
14819 /* Parse a namespace-body.
14820
14821    namespace-body:
14822      declaration-seq [opt]  */
14823
14824 static void
14825 cp_parser_namespace_body (cp_parser* parser)
14826 {
14827   cp_parser_declaration_seq_opt (parser);
14828 }
14829
14830 /* Parse a namespace-alias-definition.
14831
14832    namespace-alias-definition:
14833      namespace identifier = qualified-namespace-specifier ;  */
14834
14835 static void
14836 cp_parser_namespace_alias_definition (cp_parser* parser)
14837 {
14838   tree identifier;
14839   tree namespace_specifier;
14840
14841   cp_token *token = cp_lexer_peek_token (parser->lexer);
14842
14843   /* Look for the `namespace' keyword.  */
14844   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14845   /* Look for the identifier.  */
14846   identifier = cp_parser_identifier (parser);
14847   if (identifier == error_mark_node)
14848     return;
14849   /* Look for the `=' token.  */
14850   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14851       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14852     {
14853       error_at (token->location, "%<namespace%> definition is not allowed here");
14854       /* Skip the definition.  */
14855       cp_lexer_consume_token (parser->lexer);
14856       if (cp_parser_skip_to_closing_brace (parser))
14857         cp_lexer_consume_token (parser->lexer);
14858       return;
14859     }
14860   cp_parser_require (parser, CPP_EQ, RT_EQ);
14861   /* Look for the qualified-namespace-specifier.  */
14862   namespace_specifier
14863     = cp_parser_qualified_namespace_specifier (parser);
14864   /* Look for the `;' token.  */
14865   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14866
14867   /* Register the alias in the symbol table.  */
14868   do_namespace_alias (identifier, namespace_specifier);
14869 }
14870
14871 /* Parse a qualified-namespace-specifier.
14872
14873    qualified-namespace-specifier:
14874      :: [opt] nested-name-specifier [opt] namespace-name
14875
14876    Returns a NAMESPACE_DECL corresponding to the specified
14877    namespace.  */
14878
14879 static tree
14880 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14881 {
14882   /* Look for the optional `::'.  */
14883   cp_parser_global_scope_opt (parser,
14884                               /*current_scope_valid_p=*/false);
14885
14886   /* Look for the optional nested-name-specifier.  */
14887   cp_parser_nested_name_specifier_opt (parser,
14888                                        /*typename_keyword_p=*/false,
14889                                        /*check_dependency_p=*/true,
14890                                        /*type_p=*/false,
14891                                        /*is_declaration=*/true);
14892
14893   return cp_parser_namespace_name (parser);
14894 }
14895
14896 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14897    access declaration.
14898
14899    using-declaration:
14900      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14901      using :: unqualified-id ;  
14902
14903    access-declaration:
14904      qualified-id ;  
14905
14906    */
14907
14908 static bool
14909 cp_parser_using_declaration (cp_parser* parser, 
14910                              bool access_declaration_p)
14911 {
14912   cp_token *token;
14913   bool typename_p = false;
14914   bool global_scope_p;
14915   tree decl;
14916   tree identifier;
14917   tree qscope;
14918   int oldcount = errorcount;
14919   cp_token *diag_token = NULL;
14920
14921   if (access_declaration_p)
14922     {
14923       diag_token = cp_lexer_peek_token (parser->lexer);
14924       cp_parser_parse_tentatively (parser);
14925     }
14926   else
14927     {
14928       /* Look for the `using' keyword.  */
14929       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14930       
14931       /* Peek at the next token.  */
14932       token = cp_lexer_peek_token (parser->lexer);
14933       /* See if it's `typename'.  */
14934       if (token->keyword == RID_TYPENAME)
14935         {
14936           /* Remember that we've seen it.  */
14937           typename_p = true;
14938           /* Consume the `typename' token.  */
14939           cp_lexer_consume_token (parser->lexer);
14940         }
14941     }
14942
14943   /* Look for the optional global scope qualification.  */
14944   global_scope_p
14945     = (cp_parser_global_scope_opt (parser,
14946                                    /*current_scope_valid_p=*/false)
14947        != NULL_TREE);
14948
14949   /* If we saw `typename', or didn't see `::', then there must be a
14950      nested-name-specifier present.  */
14951   if (typename_p || !global_scope_p)
14952     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14953                                               /*check_dependency_p=*/true,
14954                                               /*type_p=*/false,
14955                                               /*is_declaration=*/true);
14956   /* Otherwise, we could be in either of the two productions.  In that
14957      case, treat the nested-name-specifier as optional.  */
14958   else
14959     qscope = cp_parser_nested_name_specifier_opt (parser,
14960                                                   /*typename_keyword_p=*/false,
14961                                                   /*check_dependency_p=*/true,
14962                                                   /*type_p=*/false,
14963                                                   /*is_declaration=*/true);
14964   if (!qscope)
14965     qscope = global_namespace;
14966
14967   if (access_declaration_p && cp_parser_error_occurred (parser))
14968     /* Something has already gone wrong; there's no need to parse
14969        further.  Since an error has occurred, the return value of
14970        cp_parser_parse_definitely will be false, as required.  */
14971     return cp_parser_parse_definitely (parser);
14972
14973   token = cp_lexer_peek_token (parser->lexer);
14974   /* Parse the unqualified-id.  */
14975   identifier = cp_parser_unqualified_id (parser,
14976                                          /*template_keyword_p=*/false,
14977                                          /*check_dependency_p=*/true,
14978                                          /*declarator_p=*/true,
14979                                          /*optional_p=*/false);
14980
14981   if (access_declaration_p)
14982     {
14983       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14984         cp_parser_simulate_error (parser);
14985       if (!cp_parser_parse_definitely (parser))
14986         return false;
14987     }
14988
14989   /* The function we call to handle a using-declaration is different
14990      depending on what scope we are in.  */
14991   if (qscope == error_mark_node || identifier == error_mark_node)
14992     ;
14993   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14994            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14995     /* [namespace.udecl]
14996
14997        A using declaration shall not name a template-id.  */
14998     error_at (token->location,
14999               "a template-id may not appear in a using-declaration");
15000   else
15001     {
15002       if (at_class_scope_p ())
15003         {
15004           /* Create the USING_DECL.  */
15005           decl = do_class_using_decl (parser->scope, identifier);
15006
15007           if (decl && typename_p)
15008             USING_DECL_TYPENAME_P (decl) = 1;
15009
15010           if (check_for_bare_parameter_packs (decl))
15011             return false;
15012           else
15013             /* Add it to the list of members in this class.  */
15014             finish_member_declaration (decl);
15015         }
15016       else
15017         {
15018           decl = cp_parser_lookup_name_simple (parser,
15019                                                identifier,
15020                                                token->location);
15021           if (decl == error_mark_node)
15022             cp_parser_name_lookup_error (parser, identifier,
15023                                          decl, NLE_NULL,
15024                                          token->location);
15025           else if (check_for_bare_parameter_packs (decl))
15026             return false;
15027           else if (!at_namespace_scope_p ())
15028             do_local_using_decl (decl, qscope, identifier);
15029           else
15030             do_toplevel_using_decl (decl, qscope, identifier);
15031         }
15032     }
15033
15034   /* Look for the final `;'.  */
15035   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15036
15037   if (access_declaration_p && errorcount == oldcount)
15038     warning_at (diag_token->location, OPT_Wdeprecated,
15039                 "access declarations are deprecated "
15040                 "in favour of using-declarations; "
15041                 "suggestion: add the %<using%> keyword");
15042
15043   return true;
15044 }
15045
15046 /* Parse an alias-declaration.
15047
15048    alias-declaration:
15049      using identifier attribute-specifier-seq [opt] = type-id  */
15050
15051 static tree
15052 cp_parser_alias_declaration (cp_parser* parser)
15053 {
15054   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15055   location_t id_location;
15056   cp_declarator *declarator;
15057   cp_decl_specifier_seq decl_specs;
15058   bool member_p;
15059   const char *saved_message = NULL;
15060
15061   /* Look for the `using' keyword.  */
15062   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15063   id_location = cp_lexer_peek_token (parser->lexer)->location;
15064   id = cp_parser_identifier (parser);
15065   if (id == error_mark_node)
15066     return error_mark_node;
15067
15068   attributes = cp_parser_attributes_opt (parser);
15069   if (attributes == error_mark_node)
15070     return error_mark_node;
15071
15072   cp_parser_require (parser, CPP_EQ, RT_EQ);
15073
15074   /* Now we are going to parse the type-id of the declaration.  */
15075
15076   /*
15077     [dcl.type]/3 says:
15078
15079         "A type-specifier-seq shall not define a class or enumeration
15080          unless it appears in the type-id of an alias-declaration (7.1.3) that
15081          is not the declaration of a template-declaration."
15082
15083     In other words, if we currently are in an alias template, the
15084     type-id should not define a type.
15085
15086     So let's set parser->type_definition_forbidden_message in that
15087     case; cp_parser_check_type_definition (called by
15088     cp_parser_class_specifier) will then emit an error if a type is
15089     defined in the type-id.  */
15090   if (parser->num_template_parameter_lists)
15091     {
15092       saved_message = parser->type_definition_forbidden_message;
15093       parser->type_definition_forbidden_message =
15094         G_("types may not be defined in alias template declarations");
15095     }
15096
15097   type = cp_parser_type_id (parser);
15098
15099   /* Restore the error message if need be.  */
15100   if (parser->num_template_parameter_lists)
15101     parser->type_definition_forbidden_message = saved_message;
15102
15103   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15104
15105   if (cp_parser_error_occurred (parser))
15106     return error_mark_node;
15107
15108   /* A typedef-name can also be introduced by an alias-declaration. The
15109      identifier following the using keyword becomes a typedef-name. It has
15110      the same semantics as if it were introduced by the typedef
15111      specifier. In particular, it does not define a new type and it shall
15112      not appear in the type-id.  */
15113
15114   clear_decl_specs (&decl_specs);
15115   decl_specs.type = type;
15116   decl_specs.attributes = attributes;
15117   ++decl_specs.specs[(int) ds_typedef];
15118   ++decl_specs.specs[(int) ds_alias];
15119
15120   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15121   declarator->id_loc = id_location;
15122
15123   member_p = at_class_scope_p ();
15124   if (member_p)
15125     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15126                       NULL_TREE, attributes);
15127   else
15128     decl = start_decl (declarator, &decl_specs, 0,
15129                        attributes, NULL_TREE, &pushed_scope);
15130   if (decl == error_mark_node)
15131     return decl;
15132
15133   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15134
15135   if (pushed_scope)
15136     pop_scope (pushed_scope);
15137
15138   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15139      added into the symbol table; otherwise, return the TYPE_DECL.  */
15140   if (DECL_LANG_SPECIFIC (decl)
15141       && DECL_TEMPLATE_INFO (decl)
15142       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15143     {
15144       decl = DECL_TI_TEMPLATE (decl);
15145       if (member_p)
15146         check_member_template (decl);
15147     }
15148
15149   return decl;
15150 }
15151
15152 /* Parse a using-directive.
15153
15154    using-directive:
15155      using namespace :: [opt] nested-name-specifier [opt]
15156        namespace-name ;  */
15157
15158 static void
15159 cp_parser_using_directive (cp_parser* parser)
15160 {
15161   tree namespace_decl;
15162   tree attribs;
15163
15164   /* Look for the `using' keyword.  */
15165   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15166   /* And the `namespace' keyword.  */
15167   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15168   /* Look for the optional `::' operator.  */
15169   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15170   /* And the optional nested-name-specifier.  */
15171   cp_parser_nested_name_specifier_opt (parser,
15172                                        /*typename_keyword_p=*/false,
15173                                        /*check_dependency_p=*/true,
15174                                        /*type_p=*/false,
15175                                        /*is_declaration=*/true);
15176   /* Get the namespace being used.  */
15177   namespace_decl = cp_parser_namespace_name (parser);
15178   /* And any specified attributes.  */
15179   attribs = cp_parser_attributes_opt (parser);
15180   /* Update the symbol table.  */
15181   parse_using_directive (namespace_decl, attribs);
15182   /* Look for the final `;'.  */
15183   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15184 }
15185
15186 /* Parse an asm-definition.
15187
15188    asm-definition:
15189      asm ( string-literal ) ;
15190
15191    GNU Extension:
15192
15193    asm-definition:
15194      asm volatile [opt] ( string-literal ) ;
15195      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15196      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15197                           : asm-operand-list [opt] ) ;
15198      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15199                           : asm-operand-list [opt]
15200                           : asm-clobber-list [opt] ) ;
15201      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15202                                : asm-clobber-list [opt]
15203                                : asm-goto-list ) ;  */
15204
15205 static void
15206 cp_parser_asm_definition (cp_parser* parser)
15207 {
15208   tree string;
15209   tree outputs = NULL_TREE;
15210   tree inputs = NULL_TREE;
15211   tree clobbers = NULL_TREE;
15212   tree labels = NULL_TREE;
15213   tree asm_stmt;
15214   bool volatile_p = false;
15215   bool extended_p = false;
15216   bool invalid_inputs_p = false;
15217   bool invalid_outputs_p = false;
15218   bool goto_p = false;
15219   required_token missing = RT_NONE;
15220
15221   /* Look for the `asm' keyword.  */
15222   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15223   /* See if the next token is `volatile'.  */
15224   if (cp_parser_allow_gnu_extensions_p (parser)
15225       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15226     {
15227       /* Remember that we saw the `volatile' keyword.  */
15228       volatile_p = true;
15229       /* Consume the token.  */
15230       cp_lexer_consume_token (parser->lexer);
15231     }
15232   if (cp_parser_allow_gnu_extensions_p (parser)
15233       && parser->in_function_body
15234       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15235     {
15236       /* Remember that we saw the `goto' keyword.  */
15237       goto_p = true;
15238       /* Consume the token.  */
15239       cp_lexer_consume_token (parser->lexer);
15240     }
15241   /* Look for the opening `('.  */
15242   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15243     return;
15244   /* Look for the string.  */
15245   string = cp_parser_string_literal (parser, false, false);
15246   if (string == error_mark_node)
15247     {
15248       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15249                                              /*consume_paren=*/true);
15250       return;
15251     }
15252
15253   /* If we're allowing GNU extensions, check for the extended assembly
15254      syntax.  Unfortunately, the `:' tokens need not be separated by
15255      a space in C, and so, for compatibility, we tolerate that here
15256      too.  Doing that means that we have to treat the `::' operator as
15257      two `:' tokens.  */
15258   if (cp_parser_allow_gnu_extensions_p (parser)
15259       && parser->in_function_body
15260       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15261           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15262     {
15263       bool inputs_p = false;
15264       bool clobbers_p = false;
15265       bool labels_p = false;
15266
15267       /* The extended syntax was used.  */
15268       extended_p = true;
15269
15270       /* Look for outputs.  */
15271       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15272         {
15273           /* Consume the `:'.  */
15274           cp_lexer_consume_token (parser->lexer);
15275           /* Parse the output-operands.  */
15276           if (cp_lexer_next_token_is_not (parser->lexer,
15277                                           CPP_COLON)
15278               && cp_lexer_next_token_is_not (parser->lexer,
15279                                              CPP_SCOPE)
15280               && cp_lexer_next_token_is_not (parser->lexer,
15281                                              CPP_CLOSE_PAREN)
15282               && !goto_p)
15283             outputs = cp_parser_asm_operand_list (parser);
15284
15285             if (outputs == error_mark_node)
15286               invalid_outputs_p = true;
15287         }
15288       /* If the next token is `::', there are no outputs, and the
15289          next token is the beginning of the inputs.  */
15290       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15291         /* The inputs are coming next.  */
15292         inputs_p = true;
15293
15294       /* Look for inputs.  */
15295       if (inputs_p
15296           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15297         {
15298           /* Consume the `:' or `::'.  */
15299           cp_lexer_consume_token (parser->lexer);
15300           /* Parse the output-operands.  */
15301           if (cp_lexer_next_token_is_not (parser->lexer,
15302                                           CPP_COLON)
15303               && cp_lexer_next_token_is_not (parser->lexer,
15304                                              CPP_SCOPE)
15305               && cp_lexer_next_token_is_not (parser->lexer,
15306                                              CPP_CLOSE_PAREN))
15307             inputs = cp_parser_asm_operand_list (parser);
15308
15309             if (inputs == error_mark_node)
15310               invalid_inputs_p = true;
15311         }
15312       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15313         /* The clobbers are coming next.  */
15314         clobbers_p = true;
15315
15316       /* Look for clobbers.  */
15317       if (clobbers_p
15318           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15319         {
15320           clobbers_p = true;
15321           /* Consume the `:' or `::'.  */
15322           cp_lexer_consume_token (parser->lexer);
15323           /* Parse the clobbers.  */
15324           if (cp_lexer_next_token_is_not (parser->lexer,
15325                                           CPP_COLON)
15326               && cp_lexer_next_token_is_not (parser->lexer,
15327                                              CPP_CLOSE_PAREN))
15328             clobbers = cp_parser_asm_clobber_list (parser);
15329         }
15330       else if (goto_p
15331                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15332         /* The labels are coming next.  */
15333         labels_p = true;
15334
15335       /* Look for labels.  */
15336       if (labels_p
15337           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15338         {
15339           labels_p = true;
15340           /* Consume the `:' or `::'.  */
15341           cp_lexer_consume_token (parser->lexer);
15342           /* Parse the labels.  */
15343           labels = cp_parser_asm_label_list (parser);
15344         }
15345
15346       if (goto_p && !labels_p)
15347         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15348     }
15349   else if (goto_p)
15350     missing = RT_COLON_SCOPE;
15351
15352   /* Look for the closing `)'.  */
15353   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15354                           missing ? missing : RT_CLOSE_PAREN))
15355     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15356                                            /*consume_paren=*/true);
15357   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15358
15359   if (!invalid_inputs_p && !invalid_outputs_p)
15360     {
15361       /* Create the ASM_EXPR.  */
15362       if (parser->in_function_body)
15363         {
15364           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15365                                       inputs, clobbers, labels);
15366           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15367           if (!extended_p)
15368             {
15369               tree temp = asm_stmt;
15370               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15371                 temp = TREE_OPERAND (temp, 0);
15372
15373               ASM_INPUT_P (temp) = 1;
15374             }
15375         }
15376       else
15377         cgraph_add_asm_node (string);
15378     }
15379 }
15380
15381 /* Declarators [gram.dcl.decl] */
15382
15383 /* Parse an init-declarator.
15384
15385    init-declarator:
15386      declarator initializer [opt]
15387
15388    GNU Extension:
15389
15390    init-declarator:
15391      declarator asm-specification [opt] attributes [opt] initializer [opt]
15392
15393    function-definition:
15394      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15395        function-body
15396      decl-specifier-seq [opt] declarator function-try-block
15397
15398    GNU Extension:
15399
15400    function-definition:
15401      __extension__ function-definition
15402
15403    TM Extension:
15404
15405    function-definition:
15406      decl-specifier-seq [opt] declarator function-transaction-block
15407
15408    The DECL_SPECIFIERS apply to this declarator.  Returns a
15409    representation of the entity declared.  If MEMBER_P is TRUE, then
15410    this declarator appears in a class scope.  The new DECL created by
15411    this declarator is returned.
15412
15413    The CHECKS are access checks that should be performed once we know
15414    what entity is being declared (and, therefore, what classes have
15415    befriended it).
15416
15417    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15418    for a function-definition here as well.  If the declarator is a
15419    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15420    be TRUE upon return.  By that point, the function-definition will
15421    have been completely parsed.
15422
15423    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15424    is FALSE.
15425
15426    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15427    parsed declaration if it is an uninitialized single declarator not followed
15428    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15429    if present, will not be consumed.  If returned, this declarator will be
15430    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15431
15432 static tree
15433 cp_parser_init_declarator (cp_parser* parser,
15434                            cp_decl_specifier_seq *decl_specifiers,
15435                            VEC (deferred_access_check,gc)* checks,
15436                            bool function_definition_allowed_p,
15437                            bool member_p,
15438                            int declares_class_or_enum,
15439                            bool* function_definition_p,
15440                            tree* maybe_range_for_decl)
15441 {
15442   cp_token *token = NULL, *asm_spec_start_token = NULL,
15443            *attributes_start_token = NULL;
15444   cp_declarator *declarator;
15445   tree prefix_attributes;
15446   tree attributes;
15447   tree asm_specification;
15448   tree initializer;
15449   tree decl = NULL_TREE;
15450   tree scope;
15451   int is_initialized;
15452   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15453      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15454      "(...)".  */
15455   enum cpp_ttype initialization_kind;
15456   bool is_direct_init = false;
15457   bool is_non_constant_init;
15458   int ctor_dtor_or_conv_p;
15459   bool friend_p;
15460   tree pushed_scope = NULL_TREE;
15461   bool range_for_decl_p = false;
15462
15463   /* Gather the attributes that were provided with the
15464      decl-specifiers.  */
15465   prefix_attributes = decl_specifiers->attributes;
15466
15467   /* Assume that this is not the declarator for a function
15468      definition.  */
15469   if (function_definition_p)
15470     *function_definition_p = false;
15471
15472   /* Defer access checks while parsing the declarator; we cannot know
15473      what names are accessible until we know what is being
15474      declared.  */
15475   resume_deferring_access_checks ();
15476
15477   /* Parse the declarator.  */
15478   token = cp_lexer_peek_token (parser->lexer);
15479   declarator
15480     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15481                             &ctor_dtor_or_conv_p,
15482                             /*parenthesized_p=*/NULL,
15483                             member_p);
15484   /* Gather up the deferred checks.  */
15485   stop_deferring_access_checks ();
15486
15487   /* If the DECLARATOR was erroneous, there's no need to go
15488      further.  */
15489   if (declarator == cp_error_declarator)
15490     return error_mark_node;
15491
15492   /* Check that the number of template-parameter-lists is OK.  */
15493   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15494                                                        token->location))
15495     return error_mark_node;
15496
15497   if (declares_class_or_enum & 2)
15498     cp_parser_check_for_definition_in_return_type (declarator,
15499                                                    decl_specifiers->type,
15500                                                    decl_specifiers->type_location);
15501
15502   /* Figure out what scope the entity declared by the DECLARATOR is
15503      located in.  `grokdeclarator' sometimes changes the scope, so
15504      we compute it now.  */
15505   scope = get_scope_of_declarator (declarator);
15506
15507   /* Perform any lookups in the declared type which were thought to be
15508      dependent, but are not in the scope of the declarator.  */
15509   decl_specifiers->type
15510     = maybe_update_decl_type (decl_specifiers->type, scope);
15511
15512   /* If we're allowing GNU extensions, look for an asm-specification
15513      and attributes.  */
15514   if (cp_parser_allow_gnu_extensions_p (parser))
15515     {
15516       /* Look for an asm-specification.  */
15517       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15518       asm_specification = cp_parser_asm_specification_opt (parser);
15519       /* And attributes.  */
15520       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15521       attributes = cp_parser_attributes_opt (parser);
15522     }
15523   else
15524     {
15525       asm_specification = NULL_TREE;
15526       attributes = NULL_TREE;
15527     }
15528
15529   /* Peek at the next token.  */
15530   token = cp_lexer_peek_token (parser->lexer);
15531   /* Check to see if the token indicates the start of a
15532      function-definition.  */
15533   if (function_declarator_p (declarator)
15534       && cp_parser_token_starts_function_definition_p (token))
15535     {
15536       if (!function_definition_allowed_p)
15537         {
15538           /* If a function-definition should not appear here, issue an
15539              error message.  */
15540           cp_parser_error (parser,
15541                            "a function-definition is not allowed here");
15542           return error_mark_node;
15543         }
15544       else
15545         {
15546           location_t func_brace_location
15547             = cp_lexer_peek_token (parser->lexer)->location;
15548
15549           /* Neither attributes nor an asm-specification are allowed
15550              on a function-definition.  */
15551           if (asm_specification)
15552             error_at (asm_spec_start_token->location,
15553                       "an asm-specification is not allowed "
15554                       "on a function-definition");
15555           if (attributes)
15556             error_at (attributes_start_token->location,
15557                       "attributes are not allowed on a function-definition");
15558           /* This is a function-definition.  */
15559           *function_definition_p = true;
15560
15561           /* Parse the function definition.  */
15562           if (member_p)
15563             decl = cp_parser_save_member_function_body (parser,
15564                                                         decl_specifiers,
15565                                                         declarator,
15566                                                         prefix_attributes);
15567           else
15568             decl
15569               = (cp_parser_function_definition_from_specifiers_and_declarator
15570                  (parser, decl_specifiers, prefix_attributes, declarator));
15571
15572           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15573             {
15574               /* This is where the prologue starts...  */
15575               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15576                 = func_brace_location;
15577             }
15578
15579           return decl;
15580         }
15581     }
15582
15583   /* [dcl.dcl]
15584
15585      Only in function declarations for constructors, destructors, and
15586      type conversions can the decl-specifier-seq be omitted.
15587
15588      We explicitly postpone this check past the point where we handle
15589      function-definitions because we tolerate function-definitions
15590      that are missing their return types in some modes.  */
15591   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15592     {
15593       cp_parser_error (parser,
15594                        "expected constructor, destructor, or type conversion");
15595       return error_mark_node;
15596     }
15597
15598   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15599   if (token->type == CPP_EQ
15600       || token->type == CPP_OPEN_PAREN
15601       || token->type == CPP_OPEN_BRACE)
15602     {
15603       is_initialized = SD_INITIALIZED;
15604       initialization_kind = token->type;
15605       if (maybe_range_for_decl)
15606         *maybe_range_for_decl = error_mark_node;
15607
15608       if (token->type == CPP_EQ
15609           && function_declarator_p (declarator))
15610         {
15611           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15612           if (t2->keyword == RID_DEFAULT)
15613             is_initialized = SD_DEFAULTED;
15614           else if (t2->keyword == RID_DELETE)
15615             is_initialized = SD_DELETED;
15616         }
15617     }
15618   else
15619     {
15620       /* If the init-declarator isn't initialized and isn't followed by a
15621          `,' or `;', it's not a valid init-declarator.  */
15622       if (token->type != CPP_COMMA
15623           && token->type != CPP_SEMICOLON)
15624         {
15625           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15626             range_for_decl_p = true;
15627           else
15628             {
15629               cp_parser_error (parser, "expected initializer");
15630               return error_mark_node;
15631             }
15632         }
15633       is_initialized = SD_UNINITIALIZED;
15634       initialization_kind = CPP_EOF;
15635     }
15636
15637   /* Because start_decl has side-effects, we should only call it if we
15638      know we're going ahead.  By this point, we know that we cannot
15639      possibly be looking at any other construct.  */
15640   cp_parser_commit_to_tentative_parse (parser);
15641
15642   /* If the decl specifiers were bad, issue an error now that we're
15643      sure this was intended to be a declarator.  Then continue
15644      declaring the variable(s), as int, to try to cut down on further
15645      errors.  */
15646   if (decl_specifiers->any_specifiers_p
15647       && decl_specifiers->type == error_mark_node)
15648     {
15649       cp_parser_error (parser, "invalid type in declaration");
15650       decl_specifiers->type = integer_type_node;
15651     }
15652
15653   /* Check to see whether or not this declaration is a friend.  */
15654   friend_p = cp_parser_friend_p (decl_specifiers);
15655
15656   /* Enter the newly declared entry in the symbol table.  If we're
15657      processing a declaration in a class-specifier, we wait until
15658      after processing the initializer.  */
15659   if (!member_p)
15660     {
15661       if (parser->in_unbraced_linkage_specification_p)
15662         decl_specifiers->storage_class = sc_extern;
15663       decl = start_decl (declarator, decl_specifiers,
15664                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15665                          attributes, prefix_attributes,
15666                          &pushed_scope);
15667       /* Adjust location of decl if declarator->id_loc is more appropriate:
15668          set, and decl wasn't merged with another decl, in which case its
15669          location would be different from input_location, and more accurate.  */
15670       if (DECL_P (decl)
15671           && declarator->id_loc != UNKNOWN_LOCATION
15672           && DECL_SOURCE_LOCATION (decl) == input_location)
15673         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15674     }
15675   else if (scope)
15676     /* Enter the SCOPE.  That way unqualified names appearing in the
15677        initializer will be looked up in SCOPE.  */
15678     pushed_scope = push_scope (scope);
15679
15680   /* Perform deferred access control checks, now that we know in which
15681      SCOPE the declared entity resides.  */
15682   if (!member_p && decl)
15683     {
15684       tree saved_current_function_decl = NULL_TREE;
15685
15686       /* If the entity being declared is a function, pretend that we
15687          are in its scope.  If it is a `friend', it may have access to
15688          things that would not otherwise be accessible.  */
15689       if (TREE_CODE (decl) == FUNCTION_DECL)
15690         {
15691           saved_current_function_decl = current_function_decl;
15692           current_function_decl = decl;
15693         }
15694
15695       /* Perform access checks for template parameters.  */
15696       cp_parser_perform_template_parameter_access_checks (checks);
15697
15698       /* Perform the access control checks for the declarator and the
15699          decl-specifiers.  */
15700       perform_deferred_access_checks ();
15701
15702       /* Restore the saved value.  */
15703       if (TREE_CODE (decl) == FUNCTION_DECL)
15704         current_function_decl = saved_current_function_decl;
15705     }
15706
15707   /* Parse the initializer.  */
15708   initializer = NULL_TREE;
15709   is_direct_init = false;
15710   is_non_constant_init = true;
15711   if (is_initialized)
15712     {
15713       if (function_declarator_p (declarator))
15714         {
15715           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15716            if (initialization_kind == CPP_EQ)
15717              initializer = cp_parser_pure_specifier (parser);
15718            else
15719              {
15720                /* If the declaration was erroneous, we don't really
15721                   know what the user intended, so just silently
15722                   consume the initializer.  */
15723                if (decl != error_mark_node)
15724                  error_at (initializer_start_token->location,
15725                            "initializer provided for function");
15726                cp_parser_skip_to_closing_parenthesis (parser,
15727                                                       /*recovering=*/true,
15728                                                       /*or_comma=*/false,
15729                                                       /*consume_paren=*/true);
15730              }
15731         }
15732       else
15733         {
15734           /* We want to record the extra mangling scope for in-class
15735              initializers of class members and initializers of static data
15736              member templates.  The former involves deferring
15737              parsing of the initializer until end of class as with default
15738              arguments.  So right here we only handle the latter.  */
15739           if (!member_p && processing_template_decl)
15740             start_lambda_scope (decl);
15741           initializer = cp_parser_initializer (parser,
15742                                                &is_direct_init,
15743                                                &is_non_constant_init);
15744           if (!member_p && processing_template_decl)
15745             finish_lambda_scope ();
15746         }
15747     }
15748
15749   /* The old parser allows attributes to appear after a parenthesized
15750      initializer.  Mark Mitchell proposed removing this functionality
15751      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15752      attributes -- but ignores them.  */
15753   if (cp_parser_allow_gnu_extensions_p (parser)
15754       && initialization_kind == CPP_OPEN_PAREN)
15755     if (cp_parser_attributes_opt (parser))
15756       warning (OPT_Wattributes,
15757                "attributes after parenthesized initializer ignored");
15758
15759   /* For an in-class declaration, use `grokfield' to create the
15760      declaration.  */
15761   if (member_p)
15762     {
15763       if (pushed_scope)
15764         {
15765           pop_scope (pushed_scope);
15766           pushed_scope = NULL_TREE;
15767         }
15768       decl = grokfield (declarator, decl_specifiers,
15769                         initializer, !is_non_constant_init,
15770                         /*asmspec=*/NULL_TREE,
15771                         prefix_attributes);
15772       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15773         cp_parser_save_default_args (parser, decl);
15774     }
15775
15776   /* Finish processing the declaration.  But, skip member
15777      declarations.  */
15778   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15779     {
15780       cp_finish_decl (decl,
15781                       initializer, !is_non_constant_init,
15782                       asm_specification,
15783                       /* If the initializer is in parentheses, then this is
15784                          a direct-initialization, which means that an
15785                          `explicit' constructor is OK.  Otherwise, an
15786                          `explicit' constructor cannot be used.  */
15787                       ((is_direct_init || !is_initialized)
15788                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15789     }
15790   else if ((cxx_dialect != cxx98) && friend_p
15791            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15792     /* Core issue #226 (C++0x only): A default template-argument
15793        shall not be specified in a friend class template
15794        declaration. */
15795     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15796                              /*is_partial=*/0, /*is_friend_decl=*/1);
15797
15798   if (!friend_p && pushed_scope)
15799     pop_scope (pushed_scope);
15800
15801   return decl;
15802 }
15803
15804 /* Parse a declarator.
15805
15806    declarator:
15807      direct-declarator
15808      ptr-operator declarator
15809
15810    abstract-declarator:
15811      ptr-operator abstract-declarator [opt]
15812      direct-abstract-declarator
15813
15814    GNU Extensions:
15815
15816    declarator:
15817      attributes [opt] direct-declarator
15818      attributes [opt] ptr-operator declarator
15819
15820    abstract-declarator:
15821      attributes [opt] ptr-operator abstract-declarator [opt]
15822      attributes [opt] direct-abstract-declarator
15823
15824    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15825    detect constructor, destructor or conversion operators. It is set
15826    to -1 if the declarator is a name, and +1 if it is a
15827    function. Otherwise it is set to zero. Usually you just want to
15828    test for >0, but internally the negative value is used.
15829
15830    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15831    a decl-specifier-seq unless it declares a constructor, destructor,
15832    or conversion.  It might seem that we could check this condition in
15833    semantic analysis, rather than parsing, but that makes it difficult
15834    to handle something like `f()'.  We want to notice that there are
15835    no decl-specifiers, and therefore realize that this is an
15836    expression, not a declaration.)
15837
15838    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15839    the declarator is a direct-declarator of the form "(...)".
15840
15841    MEMBER_P is true iff this declarator is a member-declarator.  */
15842
15843 static cp_declarator *
15844 cp_parser_declarator (cp_parser* parser,
15845                       cp_parser_declarator_kind dcl_kind,
15846                       int* ctor_dtor_or_conv_p,
15847                       bool* parenthesized_p,
15848                       bool member_p)
15849 {
15850   cp_declarator *declarator;
15851   enum tree_code code;
15852   cp_cv_quals cv_quals;
15853   tree class_type;
15854   tree attributes = NULL_TREE;
15855
15856   /* Assume this is not a constructor, destructor, or type-conversion
15857      operator.  */
15858   if (ctor_dtor_or_conv_p)
15859     *ctor_dtor_or_conv_p = 0;
15860
15861   if (cp_parser_allow_gnu_extensions_p (parser))
15862     attributes = cp_parser_attributes_opt (parser);
15863
15864   /* Check for the ptr-operator production.  */
15865   cp_parser_parse_tentatively (parser);
15866   /* Parse the ptr-operator.  */
15867   code = cp_parser_ptr_operator (parser,
15868                                  &class_type,
15869                                  &cv_quals);
15870   /* If that worked, then we have a ptr-operator.  */
15871   if (cp_parser_parse_definitely (parser))
15872     {
15873       /* If a ptr-operator was found, then this declarator was not
15874          parenthesized.  */
15875       if (parenthesized_p)
15876         *parenthesized_p = true;
15877       /* The dependent declarator is optional if we are parsing an
15878          abstract-declarator.  */
15879       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15880         cp_parser_parse_tentatively (parser);
15881
15882       /* Parse the dependent declarator.  */
15883       declarator = cp_parser_declarator (parser, dcl_kind,
15884                                          /*ctor_dtor_or_conv_p=*/NULL,
15885                                          /*parenthesized_p=*/NULL,
15886                                          /*member_p=*/false);
15887
15888       /* If we are parsing an abstract-declarator, we must handle the
15889          case where the dependent declarator is absent.  */
15890       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15891           && !cp_parser_parse_definitely (parser))
15892         declarator = NULL;
15893
15894       declarator = cp_parser_make_indirect_declarator
15895         (code, class_type, cv_quals, declarator);
15896     }
15897   /* Everything else is a direct-declarator.  */
15898   else
15899     {
15900       if (parenthesized_p)
15901         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15902                                                    CPP_OPEN_PAREN);
15903       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15904                                                 ctor_dtor_or_conv_p,
15905                                                 member_p);
15906     }
15907
15908   if (attributes && declarator && declarator != cp_error_declarator)
15909     declarator->attributes = attributes;
15910
15911   return declarator;
15912 }
15913
15914 /* Parse a direct-declarator or direct-abstract-declarator.
15915
15916    direct-declarator:
15917      declarator-id
15918      direct-declarator ( parameter-declaration-clause )
15919        cv-qualifier-seq [opt]
15920        exception-specification [opt]
15921      direct-declarator [ constant-expression [opt] ]
15922      ( declarator )
15923
15924    direct-abstract-declarator:
15925      direct-abstract-declarator [opt]
15926        ( parameter-declaration-clause )
15927        cv-qualifier-seq [opt]
15928        exception-specification [opt]
15929      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15930      ( abstract-declarator )
15931
15932    Returns a representation of the declarator.  DCL_KIND is
15933    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15934    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15935    we are parsing a direct-declarator.  It is
15936    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15937    of ambiguity we prefer an abstract declarator, as per
15938    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15939    cp_parser_declarator.  */
15940
15941 static cp_declarator *
15942 cp_parser_direct_declarator (cp_parser* parser,
15943                              cp_parser_declarator_kind dcl_kind,
15944                              int* ctor_dtor_or_conv_p,
15945                              bool member_p)
15946 {
15947   cp_token *token;
15948   cp_declarator *declarator = NULL;
15949   tree scope = NULL_TREE;
15950   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15951   bool saved_in_declarator_p = parser->in_declarator_p;
15952   bool first = true;
15953   tree pushed_scope = NULL_TREE;
15954
15955   while (true)
15956     {
15957       /* Peek at the next token.  */
15958       token = cp_lexer_peek_token (parser->lexer);
15959       if (token->type == CPP_OPEN_PAREN)
15960         {
15961           /* This is either a parameter-declaration-clause, or a
15962              parenthesized declarator. When we know we are parsing a
15963              named declarator, it must be a parenthesized declarator
15964              if FIRST is true. For instance, `(int)' is a
15965              parameter-declaration-clause, with an omitted
15966              direct-abstract-declarator. But `((*))', is a
15967              parenthesized abstract declarator. Finally, when T is a
15968              template parameter `(T)' is a
15969              parameter-declaration-clause, and not a parenthesized
15970              named declarator.
15971
15972              We first try and parse a parameter-declaration-clause,
15973              and then try a nested declarator (if FIRST is true).
15974
15975              It is not an error for it not to be a
15976              parameter-declaration-clause, even when FIRST is
15977              false. Consider,
15978
15979                int i (int);
15980                int i (3);
15981
15982              The first is the declaration of a function while the
15983              second is the definition of a variable, including its
15984              initializer.
15985
15986              Having seen only the parenthesis, we cannot know which of
15987              these two alternatives should be selected.  Even more
15988              complex are examples like:
15989
15990                int i (int (a));
15991                int i (int (3));
15992
15993              The former is a function-declaration; the latter is a
15994              variable initialization.
15995
15996              Thus again, we try a parameter-declaration-clause, and if
15997              that fails, we back out and return.  */
15998
15999           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16000             {
16001               tree params;
16002               unsigned saved_num_template_parameter_lists;
16003               bool is_declarator = false;
16004               tree t;
16005
16006               /* In a member-declarator, the only valid interpretation
16007                  of a parenthesis is the start of a
16008                  parameter-declaration-clause.  (It is invalid to
16009                  initialize a static data member with a parenthesized
16010                  initializer; only the "=" form of initialization is
16011                  permitted.)  */
16012               if (!member_p)
16013                 cp_parser_parse_tentatively (parser);
16014
16015               /* Consume the `('.  */
16016               cp_lexer_consume_token (parser->lexer);
16017               if (first)
16018                 {
16019                   /* If this is going to be an abstract declarator, we're
16020                      in a declarator and we can't have default args.  */
16021                   parser->default_arg_ok_p = false;
16022                   parser->in_declarator_p = true;
16023                 }
16024
16025               /* Inside the function parameter list, surrounding
16026                  template-parameter-lists do not apply.  */
16027               saved_num_template_parameter_lists
16028                 = parser->num_template_parameter_lists;
16029               parser->num_template_parameter_lists = 0;
16030
16031               begin_scope (sk_function_parms, NULL_TREE);
16032
16033               /* Parse the parameter-declaration-clause.  */
16034               params = cp_parser_parameter_declaration_clause (parser);
16035
16036               parser->num_template_parameter_lists
16037                 = saved_num_template_parameter_lists;
16038
16039               /* Consume the `)'.  */
16040               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16041
16042               /* If all went well, parse the cv-qualifier-seq and the
16043                  exception-specification.  */
16044               if (member_p || cp_parser_parse_definitely (parser))
16045                 {
16046                   cp_cv_quals cv_quals;
16047                   cp_virt_specifiers virt_specifiers;
16048                   tree exception_specification;
16049                   tree late_return;
16050
16051                   is_declarator = true;
16052
16053                   if (ctor_dtor_or_conv_p)
16054                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16055                   first = false;
16056
16057                   /* Parse the cv-qualifier-seq.  */
16058                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16059                   /* And the exception-specification.  */
16060                   exception_specification
16061                     = cp_parser_exception_specification_opt (parser);
16062                   /* Parse the virt-specifier-seq.  */
16063                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16064
16065                   late_return = (cp_parser_late_return_type_opt
16066                                  (parser, member_p ? cv_quals : -1));
16067
16068                   /* Create the function-declarator.  */
16069                   declarator = make_call_declarator (declarator,
16070                                                      params,
16071                                                      cv_quals,
16072                                                      virt_specifiers,
16073                                                      exception_specification,
16074                                                      late_return);
16075                   /* Any subsequent parameter lists are to do with
16076                      return type, so are not those of the declared
16077                      function.  */
16078                   parser->default_arg_ok_p = false;
16079                 }
16080
16081               /* Remove the function parms from scope.  */
16082               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16083                 pop_binding (DECL_NAME (t), t);
16084               leave_scope();
16085
16086               if (is_declarator)
16087                 /* Repeat the main loop.  */
16088                 continue;
16089             }
16090
16091           /* If this is the first, we can try a parenthesized
16092              declarator.  */
16093           if (first)
16094             {
16095               bool saved_in_type_id_in_expr_p;
16096
16097               parser->default_arg_ok_p = saved_default_arg_ok_p;
16098               parser->in_declarator_p = saved_in_declarator_p;
16099
16100               /* Consume the `('.  */
16101               cp_lexer_consume_token (parser->lexer);
16102               /* Parse the nested declarator.  */
16103               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16104               parser->in_type_id_in_expr_p = true;
16105               declarator
16106                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16107                                         /*parenthesized_p=*/NULL,
16108                                         member_p);
16109               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16110               first = false;
16111               /* Expect a `)'.  */
16112               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16113                 declarator = cp_error_declarator;
16114               if (declarator == cp_error_declarator)
16115                 break;
16116
16117               goto handle_declarator;
16118             }
16119           /* Otherwise, we must be done.  */
16120           else
16121             break;
16122         }
16123       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16124                && token->type == CPP_OPEN_SQUARE)
16125         {
16126           /* Parse an array-declarator.  */
16127           tree bounds;
16128
16129           if (ctor_dtor_or_conv_p)
16130             *ctor_dtor_or_conv_p = 0;
16131
16132           first = false;
16133           parser->default_arg_ok_p = false;
16134           parser->in_declarator_p = true;
16135           /* Consume the `['.  */
16136           cp_lexer_consume_token (parser->lexer);
16137           /* Peek at the next token.  */
16138           token = cp_lexer_peek_token (parser->lexer);
16139           /* If the next token is `]', then there is no
16140              constant-expression.  */
16141           if (token->type != CPP_CLOSE_SQUARE)
16142             {
16143               bool non_constant_p;
16144
16145               bounds
16146                 = cp_parser_constant_expression (parser,
16147                                                  /*allow_non_constant=*/true,
16148                                                  &non_constant_p);
16149               if (!non_constant_p)
16150                 /* OK */;
16151               else if (error_operand_p (bounds))
16152                 /* Already gave an error.  */;
16153               else if (!parser->in_function_body
16154                        || current_binding_level->kind == sk_function_parms)
16155                 {
16156                   /* Normally, the array bound must be an integral constant
16157                      expression.  However, as an extension, we allow VLAs
16158                      in function scopes as long as they aren't part of a
16159                      parameter declaration.  */
16160                   cp_parser_error (parser,
16161                                    "array bound is not an integer constant");
16162                   bounds = error_mark_node;
16163                 }
16164               else if (processing_template_decl)
16165                 {
16166                   /* Remember this wasn't a constant-expression.  */
16167                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16168                   TREE_SIDE_EFFECTS (bounds) = 1;
16169                 }
16170             }
16171           else
16172             bounds = NULL_TREE;
16173           /* Look for the closing `]'.  */
16174           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16175             {
16176               declarator = cp_error_declarator;
16177               break;
16178             }
16179
16180           declarator = make_array_declarator (declarator, bounds);
16181         }
16182       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16183         {
16184           {
16185             tree qualifying_scope;
16186             tree unqualified_name;
16187             special_function_kind sfk;
16188             bool abstract_ok;
16189             bool pack_expansion_p = false;
16190             cp_token *declarator_id_start_token;
16191
16192             /* Parse a declarator-id */
16193             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16194             if (abstract_ok)
16195               {
16196                 cp_parser_parse_tentatively (parser);
16197
16198                 /* If we see an ellipsis, we should be looking at a
16199                    parameter pack. */
16200                 if (token->type == CPP_ELLIPSIS)
16201                   {
16202                     /* Consume the `...' */
16203                     cp_lexer_consume_token (parser->lexer);
16204
16205                     pack_expansion_p = true;
16206                   }
16207               }
16208
16209             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16210             unqualified_name
16211               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16212             qualifying_scope = parser->scope;
16213             if (abstract_ok)
16214               {
16215                 bool okay = false;
16216
16217                 if (!unqualified_name && pack_expansion_p)
16218                   {
16219                     /* Check whether an error occurred. */
16220                     okay = !cp_parser_error_occurred (parser);
16221
16222                     /* We already consumed the ellipsis to mark a
16223                        parameter pack, but we have no way to report it,
16224                        so abort the tentative parse. We will be exiting
16225                        immediately anyway. */
16226                     cp_parser_abort_tentative_parse (parser);
16227                   }
16228                 else
16229                   okay = cp_parser_parse_definitely (parser);
16230
16231                 if (!okay)
16232                   unqualified_name = error_mark_node;
16233                 else if (unqualified_name
16234                          && (qualifying_scope
16235                              || (TREE_CODE (unqualified_name)
16236                                  != IDENTIFIER_NODE)))
16237                   {
16238                     cp_parser_error (parser, "expected unqualified-id");
16239                     unqualified_name = error_mark_node;
16240                   }
16241               }
16242
16243             if (!unqualified_name)
16244               return NULL;
16245             if (unqualified_name == error_mark_node)
16246               {
16247                 declarator = cp_error_declarator;
16248                 pack_expansion_p = false;
16249                 declarator->parameter_pack_p = false;
16250                 break;
16251               }
16252
16253             if (qualifying_scope && at_namespace_scope_p ()
16254                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16255               {
16256                 /* In the declaration of a member of a template class
16257                    outside of the class itself, the SCOPE will sometimes
16258                    be a TYPENAME_TYPE.  For example, given:
16259
16260                    template <typename T>
16261                    int S<T>::R::i = 3;
16262
16263                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16264                    this context, we must resolve S<T>::R to an ordinary
16265                    type, rather than a typename type.
16266
16267                    The reason we normally avoid resolving TYPENAME_TYPEs
16268                    is that a specialization of `S' might render
16269                    `S<T>::R' not a type.  However, if `S' is
16270                    specialized, then this `i' will not be used, so there
16271                    is no harm in resolving the types here.  */
16272                 tree type;
16273
16274                 /* Resolve the TYPENAME_TYPE.  */
16275                 type = resolve_typename_type (qualifying_scope,
16276                                               /*only_current_p=*/false);
16277                 /* If that failed, the declarator is invalid.  */
16278                 if (TREE_CODE (type) == TYPENAME_TYPE)
16279                   {
16280                     if (typedef_variant_p (type))
16281                       error_at (declarator_id_start_token->location,
16282                                 "cannot define member of dependent typedef "
16283                                 "%qT", type);
16284                     else
16285                       error_at (declarator_id_start_token->location,
16286                                 "%<%T::%E%> is not a type",
16287                                 TYPE_CONTEXT (qualifying_scope),
16288                                 TYPE_IDENTIFIER (qualifying_scope));
16289                   }
16290                 qualifying_scope = type;
16291               }
16292
16293             sfk = sfk_none;
16294
16295             if (unqualified_name)
16296               {
16297                 tree class_type;
16298
16299                 if (qualifying_scope
16300                     && CLASS_TYPE_P (qualifying_scope))
16301                   class_type = qualifying_scope;
16302                 else
16303                   class_type = current_class_type;
16304
16305                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16306                   {
16307                     tree name_type = TREE_TYPE (unqualified_name);
16308                     if (class_type && same_type_p (name_type, class_type))
16309                       {
16310                         if (qualifying_scope
16311                             && CLASSTYPE_USE_TEMPLATE (name_type))
16312                           {
16313                             error_at (declarator_id_start_token->location,
16314                                       "invalid use of constructor as a template");
16315                             inform (declarator_id_start_token->location,
16316                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16317                                     "name the constructor in a qualified name",
16318                                     class_type,
16319                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16320                                     class_type, name_type);
16321                             declarator = cp_error_declarator;
16322                             break;
16323                           }
16324                         else
16325                           unqualified_name = constructor_name (class_type);
16326                       }
16327                     else
16328                       {
16329                         /* We do not attempt to print the declarator
16330                            here because we do not have enough
16331                            information about its original syntactic
16332                            form.  */
16333                         cp_parser_error (parser, "invalid declarator");
16334                         declarator = cp_error_declarator;
16335                         break;
16336                       }
16337                   }
16338
16339                 if (class_type)
16340                   {
16341                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16342                       sfk = sfk_destructor;
16343                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16344                       sfk = sfk_conversion;
16345                     else if (/* There's no way to declare a constructor
16346                                 for an anonymous type, even if the type
16347                                 got a name for linkage purposes.  */
16348                              !TYPE_WAS_ANONYMOUS (class_type)
16349                              && constructor_name_p (unqualified_name,
16350                                                     class_type))
16351                       {
16352                         unqualified_name = constructor_name (class_type);
16353                         sfk = sfk_constructor;
16354                       }
16355                     else if (is_overloaded_fn (unqualified_name)
16356                              && DECL_CONSTRUCTOR_P (get_first_fn
16357                                                     (unqualified_name)))
16358                       sfk = sfk_constructor;
16359
16360                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16361                       *ctor_dtor_or_conv_p = -1;
16362                   }
16363               }
16364             declarator = make_id_declarator (qualifying_scope,
16365                                              unqualified_name,
16366                                              sfk);
16367             declarator->id_loc = token->location;
16368             declarator->parameter_pack_p = pack_expansion_p;
16369
16370             if (pack_expansion_p)
16371               maybe_warn_variadic_templates ();
16372           }
16373
16374         handle_declarator:;
16375           scope = get_scope_of_declarator (declarator);
16376           if (scope)
16377             /* Any names that appear after the declarator-id for a
16378                member are looked up in the containing scope.  */
16379             pushed_scope = push_scope (scope);
16380           parser->in_declarator_p = true;
16381           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16382               || (declarator && declarator->kind == cdk_id))
16383             /* Default args are only allowed on function
16384                declarations.  */
16385             parser->default_arg_ok_p = saved_default_arg_ok_p;
16386           else
16387             parser->default_arg_ok_p = false;
16388
16389           first = false;
16390         }
16391       /* We're done.  */
16392       else
16393         break;
16394     }
16395
16396   /* For an abstract declarator, we might wind up with nothing at this
16397      point.  That's an error; the declarator is not optional.  */
16398   if (!declarator)
16399     cp_parser_error (parser, "expected declarator");
16400
16401   /* If we entered a scope, we must exit it now.  */
16402   if (pushed_scope)
16403     pop_scope (pushed_scope);
16404
16405   parser->default_arg_ok_p = saved_default_arg_ok_p;
16406   parser->in_declarator_p = saved_in_declarator_p;
16407
16408   return declarator;
16409 }
16410
16411 /* Parse a ptr-operator.
16412
16413    ptr-operator:
16414      * cv-qualifier-seq [opt]
16415      &
16416      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16417
16418    GNU Extension:
16419
16420    ptr-operator:
16421      & cv-qualifier-seq [opt]
16422
16423    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16424    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16425    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16426    filled in with the TYPE containing the member.  *CV_QUALS is
16427    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16428    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16429    Note that the tree codes returned by this function have nothing
16430    to do with the types of trees that will be eventually be created
16431    to represent the pointer or reference type being parsed. They are
16432    just constants with suggestive names. */
16433 static enum tree_code
16434 cp_parser_ptr_operator (cp_parser* parser,
16435                         tree* type,
16436                         cp_cv_quals *cv_quals)
16437 {
16438   enum tree_code code = ERROR_MARK;
16439   cp_token *token;
16440
16441   /* Assume that it's not a pointer-to-member.  */
16442   *type = NULL_TREE;
16443   /* And that there are no cv-qualifiers.  */
16444   *cv_quals = TYPE_UNQUALIFIED;
16445
16446   /* Peek at the next token.  */
16447   token = cp_lexer_peek_token (parser->lexer);
16448
16449   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16450   if (token->type == CPP_MULT)
16451     code = INDIRECT_REF;
16452   else if (token->type == CPP_AND)
16453     code = ADDR_EXPR;
16454   else if ((cxx_dialect != cxx98) &&
16455            token->type == CPP_AND_AND) /* C++0x only */
16456     code = NON_LVALUE_EXPR;
16457
16458   if (code != ERROR_MARK)
16459     {
16460       /* Consume the `*', `&' or `&&'.  */
16461       cp_lexer_consume_token (parser->lexer);
16462
16463       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16464          `&', if we are allowing GNU extensions.  (The only qualifier
16465          that can legally appear after `&' is `restrict', but that is
16466          enforced during semantic analysis.  */
16467       if (code == INDIRECT_REF
16468           || cp_parser_allow_gnu_extensions_p (parser))
16469         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16470     }
16471   else
16472     {
16473       /* Try the pointer-to-member case.  */
16474       cp_parser_parse_tentatively (parser);
16475       /* Look for the optional `::' operator.  */
16476       cp_parser_global_scope_opt (parser,
16477                                   /*current_scope_valid_p=*/false);
16478       /* Look for the nested-name specifier.  */
16479       token = cp_lexer_peek_token (parser->lexer);
16480       cp_parser_nested_name_specifier (parser,
16481                                        /*typename_keyword_p=*/false,
16482                                        /*check_dependency_p=*/true,
16483                                        /*type_p=*/false,
16484                                        /*is_declaration=*/false);
16485       /* If we found it, and the next token is a `*', then we are
16486          indeed looking at a pointer-to-member operator.  */
16487       if (!cp_parser_error_occurred (parser)
16488           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16489         {
16490           /* Indicate that the `*' operator was used.  */
16491           code = INDIRECT_REF;
16492
16493           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16494             error_at (token->location, "%qD is a namespace", parser->scope);
16495           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16496             error_at (token->location, "cannot form pointer to member of "
16497                       "non-class %q#T", parser->scope);
16498           else
16499             {
16500               /* The type of which the member is a member is given by the
16501                  current SCOPE.  */
16502               *type = parser->scope;
16503               /* The next name will not be qualified.  */
16504               parser->scope = NULL_TREE;
16505               parser->qualifying_scope = NULL_TREE;
16506               parser->object_scope = NULL_TREE;
16507               /* Look for the optional cv-qualifier-seq.  */
16508               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16509             }
16510         }
16511       /* If that didn't work we don't have a ptr-operator.  */
16512       if (!cp_parser_parse_definitely (parser))
16513         cp_parser_error (parser, "expected ptr-operator");
16514     }
16515
16516   return code;
16517 }
16518
16519 /* Parse an (optional) cv-qualifier-seq.
16520
16521    cv-qualifier-seq:
16522      cv-qualifier cv-qualifier-seq [opt]
16523
16524    cv-qualifier:
16525      const
16526      volatile
16527
16528    GNU Extension:
16529
16530    cv-qualifier:
16531      __restrict__
16532
16533    Returns a bitmask representing the cv-qualifiers.  */
16534
16535 static cp_cv_quals
16536 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16537 {
16538   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16539
16540   while (true)
16541     {
16542       cp_token *token;
16543       cp_cv_quals cv_qualifier;
16544
16545       /* Peek at the next token.  */
16546       token = cp_lexer_peek_token (parser->lexer);
16547       /* See if it's a cv-qualifier.  */
16548       switch (token->keyword)
16549         {
16550         case RID_CONST:
16551           cv_qualifier = TYPE_QUAL_CONST;
16552           break;
16553
16554         case RID_VOLATILE:
16555           cv_qualifier = TYPE_QUAL_VOLATILE;
16556           break;
16557
16558         case RID_RESTRICT:
16559           cv_qualifier = TYPE_QUAL_RESTRICT;
16560           break;
16561
16562         default:
16563           cv_qualifier = TYPE_UNQUALIFIED;
16564           break;
16565         }
16566
16567       if (!cv_qualifier)
16568         break;
16569
16570       if (cv_quals & cv_qualifier)
16571         {
16572           error_at (token->location, "duplicate cv-qualifier");
16573           cp_lexer_purge_token (parser->lexer);
16574         }
16575       else
16576         {
16577           cp_lexer_consume_token (parser->lexer);
16578           cv_quals |= cv_qualifier;
16579         }
16580     }
16581
16582   return cv_quals;
16583 }
16584
16585 /* Parse an (optional) virt-specifier-seq.
16586
16587    virt-specifier-seq:
16588      virt-specifier virt-specifier-seq [opt]
16589
16590    virt-specifier:
16591      override
16592      final
16593
16594    Returns a bitmask representing the virt-specifiers.  */
16595
16596 static cp_virt_specifiers
16597 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16598 {
16599   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16600
16601   while (true)
16602     {
16603       cp_token *token;
16604       cp_virt_specifiers virt_specifier;
16605
16606       /* Peek at the next token.  */
16607       token = cp_lexer_peek_token (parser->lexer);
16608       /* See if it's a virt-specifier-qualifier.  */
16609       if (token->type != CPP_NAME)
16610         break;
16611       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16612         {
16613           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16614           virt_specifier = VIRT_SPEC_OVERRIDE;
16615         }
16616       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16617         {
16618           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16619           virt_specifier = VIRT_SPEC_FINAL;
16620         }
16621       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16622         {
16623           virt_specifier = VIRT_SPEC_FINAL;
16624         }
16625       else
16626         break;
16627
16628       if (virt_specifiers & virt_specifier)
16629         {
16630           error_at (token->location, "duplicate virt-specifier");
16631           cp_lexer_purge_token (parser->lexer);
16632         }
16633       else
16634         {
16635           cp_lexer_consume_token (parser->lexer);
16636           virt_specifiers |= virt_specifier;
16637         }
16638     }
16639   return virt_specifiers;
16640 }
16641
16642 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16643    is in scope even though it isn't real.  */
16644
16645 static void
16646 inject_this_parameter (tree ctype, cp_cv_quals quals)
16647 {
16648   tree this_parm;
16649
16650   if (current_class_ptr)
16651     {
16652       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16653       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16654       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16655           && cp_type_quals (type) == quals)
16656         return;
16657     }
16658
16659   this_parm = build_this_parm (ctype, quals);
16660   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16661   current_class_ptr = NULL_TREE;
16662   current_class_ref
16663     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16664   current_class_ptr = this_parm;
16665 }
16666
16667 /* Parse a late-specified return type, if any.  This is not a separate
16668    non-terminal, but part of a function declarator, which looks like
16669
16670    -> trailing-type-specifier-seq abstract-declarator(opt)
16671
16672    Returns the type indicated by the type-id.
16673
16674    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16675    function.  */
16676
16677 static tree
16678 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16679 {
16680   cp_token *token;
16681   tree type;
16682
16683   /* Peek at the next token.  */
16684   token = cp_lexer_peek_token (parser->lexer);
16685   /* A late-specified return type is indicated by an initial '->'. */
16686   if (token->type != CPP_DEREF)
16687     return NULL_TREE;
16688
16689   /* Consume the ->.  */
16690   cp_lexer_consume_token (parser->lexer);
16691
16692   if (quals >= 0)
16693     {
16694       /* DR 1207: 'this' is in scope in the trailing return type.  */
16695       gcc_assert (current_class_ptr == NULL_TREE);
16696       inject_this_parameter (current_class_type, quals);
16697     }
16698
16699   type = cp_parser_trailing_type_id (parser);
16700
16701   if (quals >= 0)
16702     current_class_ptr = current_class_ref = NULL_TREE;
16703
16704   return type;
16705 }
16706
16707 /* Parse a declarator-id.
16708
16709    declarator-id:
16710      id-expression
16711      :: [opt] nested-name-specifier [opt] type-name
16712
16713    In the `id-expression' case, the value returned is as for
16714    cp_parser_id_expression if the id-expression was an unqualified-id.
16715    If the id-expression was a qualified-id, then a SCOPE_REF is
16716    returned.  The first operand is the scope (either a NAMESPACE_DECL
16717    or TREE_TYPE), but the second is still just a representation of an
16718    unqualified-id.  */
16719
16720 static tree
16721 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16722 {
16723   tree id;
16724   /* The expression must be an id-expression.  Assume that qualified
16725      names are the names of types so that:
16726
16727        template <class T>
16728        int S<T>::R::i = 3;
16729
16730      will work; we must treat `S<T>::R' as the name of a type.
16731      Similarly, assume that qualified names are templates, where
16732      required, so that:
16733
16734        template <class T>
16735        int S<T>::R<T>::i = 3;
16736
16737      will work, too.  */
16738   id = cp_parser_id_expression (parser,
16739                                 /*template_keyword_p=*/false,
16740                                 /*check_dependency_p=*/false,
16741                                 /*template_p=*/NULL,
16742                                 /*declarator_p=*/true,
16743                                 optional_p);
16744   if (id && BASELINK_P (id))
16745     id = BASELINK_FUNCTIONS (id);
16746   return id;
16747 }
16748
16749 /* Parse a type-id.
16750
16751    type-id:
16752      type-specifier-seq abstract-declarator [opt]
16753
16754    Returns the TYPE specified.  */
16755
16756 static tree
16757 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16758                      bool is_trailing_return)
16759 {
16760   cp_decl_specifier_seq type_specifier_seq;
16761   cp_declarator *abstract_declarator;
16762
16763   /* Parse the type-specifier-seq.  */
16764   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16765                                 is_trailing_return,
16766                                 &type_specifier_seq);
16767   if (type_specifier_seq.type == error_mark_node)
16768     return error_mark_node;
16769
16770   /* There might or might not be an abstract declarator.  */
16771   cp_parser_parse_tentatively (parser);
16772   /* Look for the declarator.  */
16773   abstract_declarator
16774     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16775                             /*parenthesized_p=*/NULL,
16776                             /*member_p=*/false);
16777   /* Check to see if there really was a declarator.  */
16778   if (!cp_parser_parse_definitely (parser))
16779     abstract_declarator = NULL;
16780
16781   if (type_specifier_seq.type
16782       && type_uses_auto (type_specifier_seq.type))
16783     {
16784       /* A type-id with type 'auto' is only ok if the abstract declarator
16785          is a function declarator with a late-specified return type.  */
16786       if (abstract_declarator
16787           && abstract_declarator->kind == cdk_function
16788           && abstract_declarator->u.function.late_return_type)
16789         /* OK */;
16790       else
16791         {
16792           error ("invalid use of %<auto%>");
16793           return error_mark_node;
16794         }
16795     }
16796   
16797   return groktypename (&type_specifier_seq, abstract_declarator,
16798                        is_template_arg);
16799 }
16800
16801 static tree cp_parser_type_id (cp_parser *parser)
16802 {
16803   return cp_parser_type_id_1 (parser, false, false);
16804 }
16805
16806 static tree cp_parser_template_type_arg (cp_parser *parser)
16807 {
16808   tree r;
16809   const char *saved_message = parser->type_definition_forbidden_message;
16810   parser->type_definition_forbidden_message
16811     = G_("types may not be defined in template arguments");
16812   r = cp_parser_type_id_1 (parser, true, false);
16813   parser->type_definition_forbidden_message = saved_message;
16814   return r;
16815 }
16816
16817 static tree cp_parser_trailing_type_id (cp_parser *parser)
16818 {
16819   return cp_parser_type_id_1 (parser, false, true);
16820 }
16821
16822 /* Parse a type-specifier-seq.
16823
16824    type-specifier-seq:
16825      type-specifier type-specifier-seq [opt]
16826
16827    GNU extension:
16828
16829    type-specifier-seq:
16830      attributes type-specifier-seq [opt]
16831
16832    If IS_DECLARATION is true, we are at the start of a "condition" or
16833    exception-declaration, so we might be followed by a declarator-id.
16834
16835    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16836    i.e. we've just seen "->".
16837
16838    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16839
16840 static void
16841 cp_parser_type_specifier_seq (cp_parser* parser,
16842                               bool is_declaration,
16843                               bool is_trailing_return,
16844                               cp_decl_specifier_seq *type_specifier_seq)
16845 {
16846   bool seen_type_specifier = false;
16847   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16848   cp_token *start_token = NULL;
16849
16850   /* Clear the TYPE_SPECIFIER_SEQ.  */
16851   clear_decl_specs (type_specifier_seq);
16852
16853   /* In the context of a trailing return type, enum E { } is an
16854      elaborated-type-specifier followed by a function-body, not an
16855      enum-specifier.  */
16856   if (is_trailing_return)
16857     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16858
16859   /* Parse the type-specifiers and attributes.  */
16860   while (true)
16861     {
16862       tree type_specifier;
16863       bool is_cv_qualifier;
16864
16865       /* Check for attributes first.  */
16866       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16867         {
16868           type_specifier_seq->attributes =
16869             chainon (type_specifier_seq->attributes,
16870                      cp_parser_attributes_opt (parser));
16871           continue;
16872         }
16873
16874       /* record the token of the beginning of the type specifier seq,
16875          for error reporting purposes*/
16876      if (!start_token)
16877        start_token = cp_lexer_peek_token (parser->lexer);
16878
16879       /* Look for the type-specifier.  */
16880       type_specifier = cp_parser_type_specifier (parser,
16881                                                  flags,
16882                                                  type_specifier_seq,
16883                                                  /*is_declaration=*/false,
16884                                                  NULL,
16885                                                  &is_cv_qualifier);
16886       if (!type_specifier)
16887         {
16888           /* If the first type-specifier could not be found, this is not a
16889              type-specifier-seq at all.  */
16890           if (!seen_type_specifier)
16891             {
16892               cp_parser_error (parser, "expected type-specifier");
16893               type_specifier_seq->type = error_mark_node;
16894               return;
16895             }
16896           /* If subsequent type-specifiers could not be found, the
16897              type-specifier-seq is complete.  */
16898           break;
16899         }
16900
16901       seen_type_specifier = true;
16902       /* The standard says that a condition can be:
16903
16904             type-specifier-seq declarator = assignment-expression
16905
16906          However, given:
16907
16908            struct S {};
16909            if (int S = ...)
16910
16911          we should treat the "S" as a declarator, not as a
16912          type-specifier.  The standard doesn't say that explicitly for
16913          type-specifier-seq, but it does say that for
16914          decl-specifier-seq in an ordinary declaration.  Perhaps it
16915          would be clearer just to allow a decl-specifier-seq here, and
16916          then add a semantic restriction that if any decl-specifiers
16917          that are not type-specifiers appear, the program is invalid.  */
16918       if (is_declaration && !is_cv_qualifier)
16919         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16920     }
16921
16922   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16923 }
16924
16925 /* Parse a parameter-declaration-clause.
16926
16927    parameter-declaration-clause:
16928      parameter-declaration-list [opt] ... [opt]
16929      parameter-declaration-list , ...
16930
16931    Returns a representation for the parameter declarations.  A return
16932    value of NULL indicates a parameter-declaration-clause consisting
16933    only of an ellipsis.  */
16934
16935 static tree
16936 cp_parser_parameter_declaration_clause (cp_parser* parser)
16937 {
16938   tree parameters;
16939   cp_token *token;
16940   bool ellipsis_p;
16941   bool is_error;
16942
16943   /* Peek at the next token.  */
16944   token = cp_lexer_peek_token (parser->lexer);
16945   /* Check for trivial parameter-declaration-clauses.  */
16946   if (token->type == CPP_ELLIPSIS)
16947     {
16948       /* Consume the `...' token.  */
16949       cp_lexer_consume_token (parser->lexer);
16950       return NULL_TREE;
16951     }
16952   else if (token->type == CPP_CLOSE_PAREN)
16953     /* There are no parameters.  */
16954     {
16955 #ifndef NO_IMPLICIT_EXTERN_C
16956       if (in_system_header && current_class_type == NULL
16957           && current_lang_name == lang_name_c)
16958         return NULL_TREE;
16959       else
16960 #endif
16961         return void_list_node;
16962     }
16963   /* Check for `(void)', too, which is a special case.  */
16964   else if (token->keyword == RID_VOID
16965            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16966                == CPP_CLOSE_PAREN))
16967     {
16968       /* Consume the `void' token.  */
16969       cp_lexer_consume_token (parser->lexer);
16970       /* There are no parameters.  */
16971       return void_list_node;
16972     }
16973
16974   /* Parse the parameter-declaration-list.  */
16975   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16976   /* If a parse error occurred while parsing the
16977      parameter-declaration-list, then the entire
16978      parameter-declaration-clause is erroneous.  */
16979   if (is_error)
16980     return NULL;
16981
16982   /* Peek at the next token.  */
16983   token = cp_lexer_peek_token (parser->lexer);
16984   /* If it's a `,', the clause should terminate with an ellipsis.  */
16985   if (token->type == CPP_COMMA)
16986     {
16987       /* Consume the `,'.  */
16988       cp_lexer_consume_token (parser->lexer);
16989       /* Expect an ellipsis.  */
16990       ellipsis_p
16991         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16992     }
16993   /* It might also be `...' if the optional trailing `,' was
16994      omitted.  */
16995   else if (token->type == CPP_ELLIPSIS)
16996     {
16997       /* Consume the `...' token.  */
16998       cp_lexer_consume_token (parser->lexer);
16999       /* And remember that we saw it.  */
17000       ellipsis_p = true;
17001     }
17002   else
17003     ellipsis_p = false;
17004
17005   /* Finish the parameter list.  */
17006   if (!ellipsis_p)
17007     parameters = chainon (parameters, void_list_node);
17008
17009   return parameters;
17010 }
17011
17012 /* Parse a parameter-declaration-list.
17013
17014    parameter-declaration-list:
17015      parameter-declaration
17016      parameter-declaration-list , parameter-declaration
17017
17018    Returns a representation of the parameter-declaration-list, as for
17019    cp_parser_parameter_declaration_clause.  However, the
17020    `void_list_node' is never appended to the list.  Upon return,
17021    *IS_ERROR will be true iff an error occurred.  */
17022
17023 static tree
17024 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17025 {
17026   tree parameters = NULL_TREE;
17027   tree *tail = &parameters; 
17028   bool saved_in_unbraced_linkage_specification_p;
17029   int index = 0;
17030
17031   /* Assume all will go well.  */
17032   *is_error = false;
17033   /* The special considerations that apply to a function within an
17034      unbraced linkage specifications do not apply to the parameters
17035      to the function.  */
17036   saved_in_unbraced_linkage_specification_p 
17037     = parser->in_unbraced_linkage_specification_p;
17038   parser->in_unbraced_linkage_specification_p = false;
17039
17040   /* Look for more parameters.  */
17041   while (true)
17042     {
17043       cp_parameter_declarator *parameter;
17044       tree decl = error_mark_node;
17045       bool parenthesized_p = false;
17046       /* Parse the parameter.  */
17047       parameter
17048         = cp_parser_parameter_declaration (parser,
17049                                            /*template_parm_p=*/false,
17050                                            &parenthesized_p);
17051
17052       /* We don't know yet if the enclosing context is deprecated, so wait
17053          and warn in grokparms if appropriate.  */
17054       deprecated_state = DEPRECATED_SUPPRESS;
17055
17056       if (parameter)
17057         decl = grokdeclarator (parameter->declarator,
17058                                &parameter->decl_specifiers,
17059                                PARM,
17060                                parameter->default_argument != NULL_TREE,
17061                                &parameter->decl_specifiers.attributes);
17062
17063       deprecated_state = DEPRECATED_NORMAL;
17064
17065       /* If a parse error occurred parsing the parameter declaration,
17066          then the entire parameter-declaration-list is erroneous.  */
17067       if (decl == error_mark_node)
17068         {
17069           *is_error = true;
17070           parameters = error_mark_node;
17071           break;
17072         }
17073
17074       if (parameter->decl_specifiers.attributes)
17075         cplus_decl_attributes (&decl,
17076                                parameter->decl_specifiers.attributes,
17077                                0);
17078       if (DECL_NAME (decl))
17079         decl = pushdecl (decl);
17080
17081       if (decl != error_mark_node)
17082         {
17083           retrofit_lang_decl (decl);
17084           DECL_PARM_INDEX (decl) = ++index;
17085           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17086         }
17087
17088       /* Add the new parameter to the list.  */
17089       *tail = build_tree_list (parameter->default_argument, decl);
17090       tail = &TREE_CHAIN (*tail);
17091
17092       /* Peek at the next token.  */
17093       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17094           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17095           /* These are for Objective-C++ */
17096           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17097           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17098         /* The parameter-declaration-list is complete.  */
17099         break;
17100       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17101         {
17102           cp_token *token;
17103
17104           /* Peek at the next token.  */
17105           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17106           /* If it's an ellipsis, then the list is complete.  */
17107           if (token->type == CPP_ELLIPSIS)
17108             break;
17109           /* Otherwise, there must be more parameters.  Consume the
17110              `,'.  */
17111           cp_lexer_consume_token (parser->lexer);
17112           /* When parsing something like:
17113
17114                 int i(float f, double d)
17115
17116              we can tell after seeing the declaration for "f" that we
17117              are not looking at an initialization of a variable "i",
17118              but rather at the declaration of a function "i".
17119
17120              Due to the fact that the parsing of template arguments
17121              (as specified to a template-id) requires backtracking we
17122              cannot use this technique when inside a template argument
17123              list.  */
17124           if (!parser->in_template_argument_list_p
17125               && !parser->in_type_id_in_expr_p
17126               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17127               /* However, a parameter-declaration of the form
17128                  "foat(f)" (which is a valid declaration of a
17129                  parameter "f") can also be interpreted as an
17130                  expression (the conversion of "f" to "float").  */
17131               && !parenthesized_p)
17132             cp_parser_commit_to_tentative_parse (parser);
17133         }
17134       else
17135         {
17136           cp_parser_error (parser, "expected %<,%> or %<...%>");
17137           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17138             cp_parser_skip_to_closing_parenthesis (parser,
17139                                                    /*recovering=*/true,
17140                                                    /*or_comma=*/false,
17141                                                    /*consume_paren=*/false);
17142           break;
17143         }
17144     }
17145
17146   parser->in_unbraced_linkage_specification_p
17147     = saved_in_unbraced_linkage_specification_p;
17148
17149   return parameters;
17150 }
17151
17152 /* Parse a parameter declaration.
17153
17154    parameter-declaration:
17155      decl-specifier-seq ... [opt] declarator
17156      decl-specifier-seq declarator = assignment-expression
17157      decl-specifier-seq ... [opt] abstract-declarator [opt]
17158      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17159
17160    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17161    declares a template parameter.  (In that case, a non-nested `>'
17162    token encountered during the parsing of the assignment-expression
17163    is not interpreted as a greater-than operator.)
17164
17165    Returns a representation of the parameter, or NULL if an error
17166    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17167    true iff the declarator is of the form "(p)".  */
17168
17169 static cp_parameter_declarator *
17170 cp_parser_parameter_declaration (cp_parser *parser,
17171                                  bool template_parm_p,
17172                                  bool *parenthesized_p)
17173 {
17174   int declares_class_or_enum;
17175   cp_decl_specifier_seq decl_specifiers;
17176   cp_declarator *declarator;
17177   tree default_argument;
17178   cp_token *token = NULL, *declarator_token_start = NULL;
17179   const char *saved_message;
17180
17181   /* In a template parameter, `>' is not an operator.
17182
17183      [temp.param]
17184
17185      When parsing a default template-argument for a non-type
17186      template-parameter, the first non-nested `>' is taken as the end
17187      of the template parameter-list rather than a greater-than
17188      operator.  */
17189
17190   /* Type definitions may not appear in parameter types.  */
17191   saved_message = parser->type_definition_forbidden_message;
17192   parser->type_definition_forbidden_message
17193     = G_("types may not be defined in parameter types");
17194
17195   /* Parse the declaration-specifiers.  */
17196   cp_parser_decl_specifier_seq (parser,
17197                                 CP_PARSER_FLAGS_NONE,
17198                                 &decl_specifiers,
17199                                 &declares_class_or_enum);
17200
17201   /* Complain about missing 'typename' or other invalid type names.  */
17202   if (!decl_specifiers.any_type_specifiers_p)
17203     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17204
17205   /* If an error occurred, there's no reason to attempt to parse the
17206      rest of the declaration.  */
17207   if (cp_parser_error_occurred (parser))
17208     {
17209       parser->type_definition_forbidden_message = saved_message;
17210       return NULL;
17211     }
17212
17213   /* Peek at the next token.  */
17214   token = cp_lexer_peek_token (parser->lexer);
17215
17216   /* If the next token is a `)', `,', `=', `>', or `...', then there
17217      is no declarator. However, when variadic templates are enabled,
17218      there may be a declarator following `...'.  */
17219   if (token->type == CPP_CLOSE_PAREN
17220       || token->type == CPP_COMMA
17221       || token->type == CPP_EQ
17222       || token->type == CPP_GREATER)
17223     {
17224       declarator = NULL;
17225       if (parenthesized_p)
17226         *parenthesized_p = false;
17227     }
17228   /* Otherwise, there should be a declarator.  */
17229   else
17230     {
17231       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17232       parser->default_arg_ok_p = false;
17233
17234       /* After seeing a decl-specifier-seq, if the next token is not a
17235          "(", there is no possibility that the code is a valid
17236          expression.  Therefore, if parsing tentatively, we commit at
17237          this point.  */
17238       if (!parser->in_template_argument_list_p
17239           /* In an expression context, having seen:
17240
17241                (int((char ...
17242
17243              we cannot be sure whether we are looking at a
17244              function-type (taking a "char" as a parameter) or a cast
17245              of some object of type "char" to "int".  */
17246           && !parser->in_type_id_in_expr_p
17247           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17248           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17249           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17250         cp_parser_commit_to_tentative_parse (parser);
17251       /* Parse the declarator.  */
17252       declarator_token_start = token;
17253       declarator = cp_parser_declarator (parser,
17254                                          CP_PARSER_DECLARATOR_EITHER,
17255                                          /*ctor_dtor_or_conv_p=*/NULL,
17256                                          parenthesized_p,
17257                                          /*member_p=*/false);
17258       parser->default_arg_ok_p = saved_default_arg_ok_p;
17259       /* After the declarator, allow more attributes.  */
17260       decl_specifiers.attributes
17261         = chainon (decl_specifiers.attributes,
17262                    cp_parser_attributes_opt (parser));
17263     }
17264
17265   /* If the next token is an ellipsis, and we have not seen a
17266      declarator name, and the type of the declarator contains parameter
17267      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17268      a parameter pack expansion expression. Otherwise, leave the
17269      ellipsis for a C-style variadic function. */
17270   token = cp_lexer_peek_token (parser->lexer);
17271   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17272     {
17273       tree type = decl_specifiers.type;
17274
17275       if (type && DECL_P (type))
17276         type = TREE_TYPE (type);
17277
17278       if (type
17279           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17280           && declarator_can_be_parameter_pack (declarator)
17281           && (!declarator || !declarator->parameter_pack_p)
17282           && uses_parameter_packs (type))
17283         {
17284           /* Consume the `...'. */
17285           cp_lexer_consume_token (parser->lexer);
17286           maybe_warn_variadic_templates ();
17287           
17288           /* Build a pack expansion type */
17289           if (declarator)
17290             declarator->parameter_pack_p = true;
17291           else
17292             decl_specifiers.type = make_pack_expansion (type);
17293         }
17294     }
17295
17296   /* The restriction on defining new types applies only to the type
17297      of the parameter, not to the default argument.  */
17298   parser->type_definition_forbidden_message = saved_message;
17299
17300   /* If the next token is `=', then process a default argument.  */
17301   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17302     {
17303       token = cp_lexer_peek_token (parser->lexer);
17304       /* If we are defining a class, then the tokens that make up the
17305          default argument must be saved and processed later.  */
17306       if (!template_parm_p && at_class_scope_p ()
17307           && TYPE_BEING_DEFINED (current_class_type)
17308           && !LAMBDA_TYPE_P (current_class_type))
17309         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17310       /* Outside of a class definition, we can just parse the
17311          assignment-expression.  */
17312       else
17313         default_argument
17314           = cp_parser_default_argument (parser, template_parm_p);
17315
17316       if (!parser->default_arg_ok_p)
17317         {
17318           if (flag_permissive)
17319             warning (0, "deprecated use of default argument for parameter of non-function");
17320           else
17321             {
17322               error_at (token->location,
17323                         "default arguments are only "
17324                         "permitted for function parameters");
17325               default_argument = NULL_TREE;
17326             }
17327         }
17328       else if ((declarator && declarator->parameter_pack_p)
17329                || (decl_specifiers.type
17330                    && PACK_EXPANSION_P (decl_specifiers.type)))
17331         {
17332           /* Find the name of the parameter pack.  */     
17333           cp_declarator *id_declarator = declarator;
17334           while (id_declarator && id_declarator->kind != cdk_id)
17335             id_declarator = id_declarator->declarator;
17336           
17337           if (id_declarator && id_declarator->kind == cdk_id)
17338             error_at (declarator_token_start->location,
17339                       template_parm_p
17340                       ? G_("template parameter pack %qD "
17341                            "cannot have a default argument")
17342                       : G_("parameter pack %qD cannot have "
17343                            "a default argument"),
17344                       id_declarator->u.id.unqualified_name);
17345           else
17346             error_at (declarator_token_start->location,
17347                       template_parm_p
17348                       ? G_("template parameter pack cannot have "
17349                            "a default argument")
17350                       : G_("parameter pack cannot have a "
17351                            "default argument"));
17352
17353           default_argument = NULL_TREE;
17354         }
17355     }
17356   else
17357     default_argument = NULL_TREE;
17358
17359   return make_parameter_declarator (&decl_specifiers,
17360                                     declarator,
17361                                     default_argument);
17362 }
17363
17364 /* Parse a default argument and return it.
17365
17366    TEMPLATE_PARM_P is true if this is a default argument for a
17367    non-type template parameter.  */
17368 static tree
17369 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17370 {
17371   tree default_argument = NULL_TREE;
17372   bool saved_greater_than_is_operator_p;
17373   bool saved_local_variables_forbidden_p;
17374   bool non_constant_p, is_direct_init;
17375
17376   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17377      set correctly.  */
17378   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17379   parser->greater_than_is_operator_p = !template_parm_p;
17380   /* Local variable names (and the `this' keyword) may not
17381      appear in a default argument.  */
17382   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17383   parser->local_variables_forbidden_p = true;
17384   /* Parse the assignment-expression.  */
17385   if (template_parm_p)
17386     push_deferring_access_checks (dk_no_deferred);
17387   default_argument
17388     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17389   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17390     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17391   if (template_parm_p)
17392     pop_deferring_access_checks ();
17393   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17394   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17395
17396   return default_argument;
17397 }
17398
17399 /* Parse a function-body.
17400
17401    function-body:
17402      compound_statement  */
17403
17404 static void
17405 cp_parser_function_body (cp_parser *parser)
17406 {
17407   cp_parser_compound_statement (parser, NULL, false, true);
17408 }
17409
17410 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17411    true if a ctor-initializer was present.  */
17412
17413 static bool
17414 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17415 {
17416   tree body, list;
17417   bool ctor_initializer_p;
17418   const bool check_body_p =
17419      DECL_CONSTRUCTOR_P (current_function_decl)
17420      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17421   tree last = NULL;
17422
17423   /* Begin the function body.  */
17424   body = begin_function_body ();
17425   /* Parse the optional ctor-initializer.  */
17426   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17427
17428   /* If we're parsing a constexpr constructor definition, we need
17429      to check that the constructor body is indeed empty.  However,
17430      before we get to cp_parser_function_body lot of junk has been
17431      generated, so we can't just check that we have an empty block.
17432      Rather we take a snapshot of the outermost block, and check whether
17433      cp_parser_function_body changed its state.  */
17434   if (check_body_p)
17435     {
17436       list = cur_stmt_list;
17437       if (STATEMENT_LIST_TAIL (list))
17438         last = STATEMENT_LIST_TAIL (list)->stmt;
17439     }
17440   /* Parse the function-body.  */
17441   cp_parser_function_body (parser);
17442   if (check_body_p)
17443     check_constexpr_ctor_body (last, list);
17444   /* Finish the function body.  */
17445   finish_function_body (body);
17446
17447   return ctor_initializer_p;
17448 }
17449
17450 /* Parse an initializer.
17451
17452    initializer:
17453      = initializer-clause
17454      ( expression-list )
17455
17456    Returns an expression representing the initializer.  If no
17457    initializer is present, NULL_TREE is returned.
17458
17459    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17460    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17461    set to TRUE if there is no initializer present.  If there is an
17462    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17463    is set to true; otherwise it is set to false.  */
17464
17465 static tree
17466 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17467                        bool* non_constant_p)
17468 {
17469   cp_token *token;
17470   tree init;
17471
17472   /* Peek at the next token.  */
17473   token = cp_lexer_peek_token (parser->lexer);
17474
17475   /* Let our caller know whether or not this initializer was
17476      parenthesized.  */
17477   *is_direct_init = (token->type != CPP_EQ);
17478   /* Assume that the initializer is constant.  */
17479   *non_constant_p = false;
17480
17481   if (token->type == CPP_EQ)
17482     {
17483       /* Consume the `='.  */
17484       cp_lexer_consume_token (parser->lexer);
17485       /* Parse the initializer-clause.  */
17486       init = cp_parser_initializer_clause (parser, non_constant_p);
17487     }
17488   else if (token->type == CPP_OPEN_PAREN)
17489     {
17490       VEC(tree,gc) *vec;
17491       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17492                                                      /*cast_p=*/false,
17493                                                      /*allow_expansion_p=*/true,
17494                                                      non_constant_p);
17495       if (vec == NULL)
17496         return error_mark_node;
17497       init = build_tree_list_vec (vec);
17498       release_tree_vector (vec);
17499     }
17500   else if (token->type == CPP_OPEN_BRACE)
17501     {
17502       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17503       init = cp_parser_braced_list (parser, non_constant_p);
17504       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17505     }
17506   else
17507     {
17508       /* Anything else is an error.  */
17509       cp_parser_error (parser, "expected initializer");
17510       init = error_mark_node;
17511     }
17512
17513   return init;
17514 }
17515
17516 /* Parse an initializer-clause.
17517
17518    initializer-clause:
17519      assignment-expression
17520      braced-init-list
17521
17522    Returns an expression representing the initializer.
17523
17524    If the `assignment-expression' production is used the value
17525    returned is simply a representation for the expression.
17526
17527    Otherwise, calls cp_parser_braced_list.  */
17528
17529 static tree
17530 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17531 {
17532   tree initializer;
17533
17534   /* Assume the expression is constant.  */
17535   *non_constant_p = false;
17536
17537   /* If it is not a `{', then we are looking at an
17538      assignment-expression.  */
17539   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17540     {
17541       initializer
17542         = cp_parser_constant_expression (parser,
17543                                         /*allow_non_constant_p=*/true,
17544                                         non_constant_p);
17545     }
17546   else
17547     initializer = cp_parser_braced_list (parser, non_constant_p);
17548
17549   return initializer;
17550 }
17551
17552 /* Parse a brace-enclosed initializer list.
17553
17554    braced-init-list:
17555      { initializer-list , [opt] }
17556      { }
17557
17558    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17559    the elements of the initializer-list (or NULL, if the last
17560    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17561    NULL_TREE.  There is no way to detect whether or not the optional
17562    trailing `,' was provided.  NON_CONSTANT_P is as for
17563    cp_parser_initializer.  */     
17564
17565 static tree
17566 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17567 {
17568   tree initializer;
17569
17570   /* Consume the `{' token.  */
17571   cp_lexer_consume_token (parser->lexer);
17572   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17573   initializer = make_node (CONSTRUCTOR);
17574   /* If it's not a `}', then there is a non-trivial initializer.  */
17575   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17576     {
17577       /* Parse the initializer list.  */
17578       CONSTRUCTOR_ELTS (initializer)
17579         = cp_parser_initializer_list (parser, non_constant_p);
17580       /* A trailing `,' token is allowed.  */
17581       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17582         cp_lexer_consume_token (parser->lexer);
17583     }
17584   /* Now, there should be a trailing `}'.  */
17585   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17586   TREE_TYPE (initializer) = init_list_type_node;
17587   return initializer;
17588 }
17589
17590 /* Parse an initializer-list.
17591
17592    initializer-list:
17593      initializer-clause ... [opt]
17594      initializer-list , initializer-clause ... [opt]
17595
17596    GNU Extension:
17597
17598    initializer-list:
17599      designation initializer-clause ...[opt]
17600      initializer-list , designation initializer-clause ...[opt]
17601
17602    designation:
17603      . identifier =
17604      identifier :
17605      [ constant-expression ] =
17606
17607    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17608    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17609    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17610    as for cp_parser_initializer.  */
17611
17612 static VEC(constructor_elt,gc) *
17613 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17614 {
17615   VEC(constructor_elt,gc) *v = NULL;
17616
17617   /* Assume all of the expressions are constant.  */
17618   *non_constant_p = false;
17619
17620   /* Parse the rest of the list.  */
17621   while (true)
17622     {
17623       cp_token *token;
17624       tree designator;
17625       tree initializer;
17626       bool clause_non_constant_p;
17627
17628       /* If the next token is an identifier and the following one is a
17629          colon, we are looking at the GNU designated-initializer
17630          syntax.  */
17631       if (cp_parser_allow_gnu_extensions_p (parser)
17632           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17633           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17634         {
17635           /* Warn the user that they are using an extension.  */
17636           pedwarn (input_location, OPT_pedantic, 
17637                    "ISO C++ does not allow designated initializers");
17638           /* Consume the identifier.  */
17639           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17640           /* Consume the `:'.  */
17641           cp_lexer_consume_token (parser->lexer);
17642         }
17643       /* Also handle the C99 syntax, '. id ='.  */
17644       else if (cp_parser_allow_gnu_extensions_p (parser)
17645                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17646                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17647                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17648         {
17649           /* Warn the user that they are using an extension.  */
17650           pedwarn (input_location, OPT_pedantic,
17651                    "ISO C++ does not allow C99 designated initializers");
17652           /* Consume the `.'.  */
17653           cp_lexer_consume_token (parser->lexer);
17654           /* Consume the identifier.  */
17655           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17656           /* Consume the `='.  */
17657           cp_lexer_consume_token (parser->lexer);
17658         }
17659       /* Also handle C99 array designators, '[ const ] ='.  */
17660       else if (cp_parser_allow_gnu_extensions_p (parser)
17661                && !c_dialect_objc ()
17662                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17663         {
17664           /* In C++11, [ could start a lambda-introducer.  */
17665           cp_parser_parse_tentatively (parser);
17666           cp_lexer_consume_token (parser->lexer);
17667           designator = cp_parser_constant_expression (parser, false, NULL);
17668           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17669           cp_parser_require (parser, CPP_EQ, RT_EQ);
17670           if (!cp_parser_parse_definitely (parser))
17671             designator = NULL_TREE;
17672         }
17673       else
17674         designator = NULL_TREE;
17675
17676       /* Parse the initializer.  */
17677       initializer = cp_parser_initializer_clause (parser,
17678                                                   &clause_non_constant_p);
17679       /* If any clause is non-constant, so is the entire initializer.  */
17680       if (clause_non_constant_p)
17681         *non_constant_p = true;
17682
17683       /* If we have an ellipsis, this is an initializer pack
17684          expansion.  */
17685       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17686         {
17687           /* Consume the `...'.  */
17688           cp_lexer_consume_token (parser->lexer);
17689
17690           /* Turn the initializer into an initializer expansion.  */
17691           initializer = make_pack_expansion (initializer);
17692         }
17693
17694       /* Add it to the vector.  */
17695       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17696
17697       /* If the next token is not a comma, we have reached the end of
17698          the list.  */
17699       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17700         break;
17701
17702       /* Peek at the next token.  */
17703       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17704       /* If the next token is a `}', then we're still done.  An
17705          initializer-clause can have a trailing `,' after the
17706          initializer-list and before the closing `}'.  */
17707       if (token->type == CPP_CLOSE_BRACE)
17708         break;
17709
17710       /* Consume the `,' token.  */
17711       cp_lexer_consume_token (parser->lexer);
17712     }
17713
17714   return v;
17715 }
17716
17717 /* Classes [gram.class] */
17718
17719 /* Parse a class-name.
17720
17721    class-name:
17722      identifier
17723      template-id
17724
17725    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17726    to indicate that names looked up in dependent types should be
17727    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17728    keyword has been used to indicate that the name that appears next
17729    is a template.  TAG_TYPE indicates the explicit tag given before
17730    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17731    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17732    is the class being defined in a class-head.
17733
17734    Returns the TYPE_DECL representing the class.  */
17735
17736 static tree
17737 cp_parser_class_name (cp_parser *parser,
17738                       bool typename_keyword_p,
17739                       bool template_keyword_p,
17740                       enum tag_types tag_type,
17741                       bool check_dependency_p,
17742                       bool class_head_p,
17743                       bool is_declaration)
17744 {
17745   tree decl;
17746   tree scope;
17747   bool typename_p;
17748   cp_token *token;
17749   tree identifier = NULL_TREE;
17750
17751   /* All class-names start with an identifier.  */
17752   token = cp_lexer_peek_token (parser->lexer);
17753   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17754     {
17755       cp_parser_error (parser, "expected class-name");
17756       return error_mark_node;
17757     }
17758
17759   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17760      to a template-id, so we save it here.  */
17761   scope = parser->scope;
17762   if (scope == error_mark_node)
17763     return error_mark_node;
17764
17765   /* Any name names a type if we're following the `typename' keyword
17766      in a qualified name where the enclosing scope is type-dependent.  */
17767   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17768                 && dependent_type_p (scope));
17769   /* Handle the common case (an identifier, but not a template-id)
17770      efficiently.  */
17771   if (token->type == CPP_NAME
17772       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17773     {
17774       cp_token *identifier_token;
17775       bool ambiguous_p;
17776
17777       /* Look for the identifier.  */
17778       identifier_token = cp_lexer_peek_token (parser->lexer);
17779       ambiguous_p = identifier_token->ambiguous_p;
17780       identifier = cp_parser_identifier (parser);
17781       /* If the next token isn't an identifier, we are certainly not
17782          looking at a class-name.  */
17783       if (identifier == error_mark_node)
17784         decl = error_mark_node;
17785       /* If we know this is a type-name, there's no need to look it
17786          up.  */
17787       else if (typename_p)
17788         decl = identifier;
17789       else
17790         {
17791           tree ambiguous_decls;
17792           /* If we already know that this lookup is ambiguous, then
17793              we've already issued an error message; there's no reason
17794              to check again.  */
17795           if (ambiguous_p)
17796             {
17797               cp_parser_simulate_error (parser);
17798               return error_mark_node;
17799             }
17800           /* If the next token is a `::', then the name must be a type
17801              name.
17802
17803              [basic.lookup.qual]
17804
17805              During the lookup for a name preceding the :: scope
17806              resolution operator, object, function, and enumerator
17807              names are ignored.  */
17808           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17809             tag_type = typename_type;
17810           /* Look up the name.  */
17811           decl = cp_parser_lookup_name (parser, identifier,
17812                                         tag_type,
17813                                         /*is_template=*/false,
17814                                         /*is_namespace=*/false,
17815                                         check_dependency_p,
17816                                         &ambiguous_decls,
17817                                         identifier_token->location);
17818           if (ambiguous_decls)
17819             {
17820               if (cp_parser_parsing_tentatively (parser))
17821                 cp_parser_simulate_error (parser);
17822               return error_mark_node;
17823             }
17824         }
17825     }
17826   else
17827     {
17828       /* Try a template-id.  */
17829       decl = cp_parser_template_id (parser, template_keyword_p,
17830                                     check_dependency_p,
17831                                     is_declaration);
17832       if (decl == error_mark_node)
17833         return error_mark_node;
17834     }
17835
17836   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17837
17838   /* If this is a typename, create a TYPENAME_TYPE.  */
17839   if (typename_p && decl != error_mark_node)
17840     {
17841       decl = make_typename_type (scope, decl, typename_type,
17842                                  /*complain=*/tf_error);
17843       if (decl != error_mark_node)
17844         decl = TYPE_NAME (decl);
17845     }
17846
17847   decl = strip_using_decl (decl);
17848
17849   /* Check to see that it is really the name of a class.  */
17850   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17851       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17852       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17853     /* Situations like this:
17854
17855          template <typename T> struct A {
17856            typename T::template X<int>::I i;
17857          };
17858
17859        are problematic.  Is `T::template X<int>' a class-name?  The
17860        standard does not seem to be definitive, but there is no other
17861        valid interpretation of the following `::'.  Therefore, those
17862        names are considered class-names.  */
17863     {
17864       decl = make_typename_type (scope, decl, tag_type, tf_error);
17865       if (decl != error_mark_node)
17866         decl = TYPE_NAME (decl);
17867     }
17868   else if (TREE_CODE (decl) != TYPE_DECL
17869            || TREE_TYPE (decl) == error_mark_node
17870            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17871            /* In Objective-C 2.0, a classname followed by '.' starts a
17872               dot-syntax expression, and it's not a type-name.  */
17873            || (c_dialect_objc ()
17874                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17875                && objc_is_class_name (decl)))
17876     decl = error_mark_node;
17877
17878   if (decl == error_mark_node)
17879     cp_parser_error (parser, "expected class-name");
17880   else if (identifier && !parser->scope)
17881     maybe_note_name_used_in_class (identifier, decl);
17882
17883   return decl;
17884 }
17885
17886 /* Parse a class-specifier.
17887
17888    class-specifier:
17889      class-head { member-specification [opt] }
17890
17891    Returns the TREE_TYPE representing the class.  */
17892
17893 static tree
17894 cp_parser_class_specifier_1 (cp_parser* parser)
17895 {
17896   tree type;
17897   tree attributes = NULL_TREE;
17898   bool nested_name_specifier_p;
17899   unsigned saved_num_template_parameter_lists;
17900   bool saved_in_function_body;
17901   unsigned char in_statement;
17902   bool in_switch_statement_p;
17903   bool saved_in_unbraced_linkage_specification_p;
17904   tree old_scope = NULL_TREE;
17905   tree scope = NULL_TREE;
17906   tree bases;
17907   cp_token *closing_brace;
17908
17909   push_deferring_access_checks (dk_no_deferred);
17910
17911   /* Parse the class-head.  */
17912   type = cp_parser_class_head (parser,
17913                                &nested_name_specifier_p,
17914                                &attributes,
17915                                &bases);
17916   /* If the class-head was a semantic disaster, skip the entire body
17917      of the class.  */
17918   if (!type)
17919     {
17920       cp_parser_skip_to_end_of_block_or_statement (parser);
17921       pop_deferring_access_checks ();
17922       return error_mark_node;
17923     }
17924
17925   /* Look for the `{'.  */
17926   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17927     {
17928       pop_deferring_access_checks ();
17929       return error_mark_node;
17930     }
17931
17932   /* Process the base classes. If they're invalid, skip the 
17933      entire class body.  */
17934   if (!xref_basetypes (type, bases))
17935     {
17936       /* Consuming the closing brace yields better error messages
17937          later on.  */
17938       if (cp_parser_skip_to_closing_brace (parser))
17939         cp_lexer_consume_token (parser->lexer);
17940       pop_deferring_access_checks ();
17941       return error_mark_node;
17942     }
17943
17944   /* Issue an error message if type-definitions are forbidden here.  */
17945   cp_parser_check_type_definition (parser);
17946   /* Remember that we are defining one more class.  */
17947   ++parser->num_classes_being_defined;
17948   /* Inside the class, surrounding template-parameter-lists do not
17949      apply.  */
17950   saved_num_template_parameter_lists
17951     = parser->num_template_parameter_lists;
17952   parser->num_template_parameter_lists = 0;
17953   /* We are not in a function body.  */
17954   saved_in_function_body = parser->in_function_body;
17955   parser->in_function_body = false;
17956   /* Or in a loop.  */
17957   in_statement = parser->in_statement;
17958   parser->in_statement = 0;
17959   /* Or in a switch.  */
17960   in_switch_statement_p = parser->in_switch_statement_p;
17961   parser->in_switch_statement_p = false;
17962   /* We are not immediately inside an extern "lang" block.  */
17963   saved_in_unbraced_linkage_specification_p
17964     = parser->in_unbraced_linkage_specification_p;
17965   parser->in_unbraced_linkage_specification_p = false;
17966
17967   /* Start the class.  */
17968   if (nested_name_specifier_p)
17969     {
17970       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17971       old_scope = push_inner_scope (scope);
17972     }
17973   type = begin_class_definition (type, attributes);
17974
17975   if (type == error_mark_node)
17976     /* If the type is erroneous, skip the entire body of the class.  */
17977     cp_parser_skip_to_closing_brace (parser);
17978   else
17979     /* Parse the member-specification.  */
17980     cp_parser_member_specification_opt (parser);
17981
17982   /* Look for the trailing `}'.  */
17983   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17984   /* Look for trailing attributes to apply to this class.  */
17985   if (cp_parser_allow_gnu_extensions_p (parser))
17986     attributes = cp_parser_attributes_opt (parser);
17987   if (type != error_mark_node)
17988     type = finish_struct (type, attributes);
17989   if (nested_name_specifier_p)
17990     pop_inner_scope (old_scope, scope);
17991
17992   /* We've finished a type definition.  Check for the common syntax
17993      error of forgetting a semicolon after the definition.  We need to
17994      be careful, as we can't just check for not-a-semicolon and be done
17995      with it; the user might have typed:
17996
17997      class X { } c = ...;
17998      class X { } *p = ...;
17999
18000      and so forth.  Instead, enumerate all the possible tokens that
18001      might follow this production; if we don't see one of them, then
18002      complain and silently insert the semicolon.  */
18003   {
18004     cp_token *token = cp_lexer_peek_token (parser->lexer);
18005     bool want_semicolon = true;
18006
18007     switch (token->type)
18008       {
18009       case CPP_NAME:
18010       case CPP_SEMICOLON:
18011       case CPP_MULT:
18012       case CPP_AND:
18013       case CPP_OPEN_PAREN:
18014       case CPP_CLOSE_PAREN:
18015       case CPP_COMMA:
18016         want_semicolon = false;
18017         break;
18018
18019         /* While it's legal for type qualifiers and storage class
18020            specifiers to follow type definitions in the grammar, only
18021            compiler testsuites contain code like that.  Assume that if
18022            we see such code, then what we're really seeing is a case
18023            like:
18024
18025            class X { }
18026            const <type> var = ...;
18027
18028            or
18029
18030            class Y { }
18031            static <type> func (...) ...
18032
18033            i.e. the qualifier or specifier applies to the next
18034            declaration.  To do so, however, we need to look ahead one
18035            more token to see if *that* token is a type specifier.
18036
18037            This code could be improved to handle:
18038
18039            class Z { }
18040            static const <type> var = ...;  */
18041       case CPP_KEYWORD:
18042         if (keyword_is_decl_specifier (token->keyword))
18043           {
18044             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18045
18046             /* Handling user-defined types here would be nice, but very
18047                tricky.  */
18048             want_semicolon
18049               = (lookahead->type == CPP_KEYWORD
18050                  && keyword_begins_type_specifier (lookahead->keyword));
18051           }
18052         break;
18053       default:
18054         break;
18055       }
18056
18057     /* If we don't have a type, then something is very wrong and we
18058        shouldn't try to do anything clever.  Likewise for not seeing the
18059        closing brace.  */
18060     if (closing_brace && TYPE_P (type) && want_semicolon)
18061       {
18062         cp_token_position prev
18063           = cp_lexer_previous_token_position (parser->lexer);
18064         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18065         location_t loc = prev_token->location;
18066
18067         if (CLASSTYPE_DECLARED_CLASS (type))
18068           error_at (loc, "expected %<;%> after class definition");
18069         else if (TREE_CODE (type) == RECORD_TYPE)
18070           error_at (loc, "expected %<;%> after struct definition");
18071         else if (TREE_CODE (type) == UNION_TYPE)
18072           error_at (loc, "expected %<;%> after union definition");
18073         else
18074           gcc_unreachable ();
18075
18076         /* Unget one token and smash it to look as though we encountered
18077            a semicolon in the input stream.  */
18078         cp_lexer_set_token_position (parser->lexer, prev);
18079         token = cp_lexer_peek_token (parser->lexer);
18080         token->type = CPP_SEMICOLON;
18081         token->keyword = RID_MAX;
18082       }
18083   }
18084
18085   /* If this class is not itself within the scope of another class,
18086      then we need to parse the bodies of all of the queued function
18087      definitions.  Note that the queued functions defined in a class
18088      are not always processed immediately following the
18089      class-specifier for that class.  Consider:
18090
18091        struct A {
18092          struct B { void f() { sizeof (A); } };
18093        };
18094
18095      If `f' were processed before the processing of `A' were
18096      completed, there would be no way to compute the size of `A'.
18097      Note that the nesting we are interested in here is lexical --
18098      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18099      for:
18100
18101        struct A { struct B; };
18102        struct A::B { void f() { } };
18103
18104      there is no need to delay the parsing of `A::B::f'.  */
18105   if (--parser->num_classes_being_defined == 0)
18106     {
18107       tree decl;
18108       tree class_type = NULL_TREE;
18109       tree pushed_scope = NULL_TREE;
18110       unsigned ix;
18111       cp_default_arg_entry *e;
18112       tree save_ccp, save_ccr;
18113
18114       /* In a first pass, parse default arguments to the functions.
18115          Then, in a second pass, parse the bodies of the functions.
18116          This two-phased approach handles cases like:
18117
18118             struct S {
18119               void f() { g(); }
18120               void g(int i = 3);
18121             };
18122
18123          */
18124       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18125                         ix, e)
18126         {
18127           decl = e->decl;
18128           /* If there are default arguments that have not yet been processed,
18129              take care of them now.  */
18130           if (class_type != e->class_type)
18131             {
18132               if (pushed_scope)
18133                 pop_scope (pushed_scope);
18134               class_type = e->class_type;
18135               pushed_scope = push_scope (class_type);
18136             }
18137           /* Make sure that any template parameters are in scope.  */
18138           maybe_begin_member_template_processing (decl);
18139           /* Parse the default argument expressions.  */
18140           cp_parser_late_parsing_default_args (parser, decl);
18141           /* Remove any template parameters from the symbol table.  */
18142           maybe_end_member_template_processing ();
18143         }
18144       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18145       /* Now parse any NSDMIs.  */
18146       save_ccp = current_class_ptr;
18147       save_ccr = current_class_ref;
18148       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18149         {
18150           if (class_type != DECL_CONTEXT (decl))
18151             {
18152               if (pushed_scope)
18153                 pop_scope (pushed_scope);
18154               class_type = DECL_CONTEXT (decl);
18155               pushed_scope = push_scope (class_type);
18156             }
18157           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18158           cp_parser_late_parsing_nsdmi (parser, decl);
18159         }
18160       VEC_truncate (tree, unparsed_nsdmis, 0);
18161       current_class_ptr = save_ccp;
18162       current_class_ref = save_ccr;
18163       if (pushed_scope)
18164         pop_scope (pushed_scope);
18165       /* Now parse the body of the functions.  */
18166       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18167         cp_parser_late_parsing_for_member (parser, decl);
18168       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18169     }
18170
18171   /* Put back any saved access checks.  */
18172   pop_deferring_access_checks ();
18173
18174   /* Restore saved state.  */
18175   parser->in_switch_statement_p = in_switch_statement_p;
18176   parser->in_statement = in_statement;
18177   parser->in_function_body = saved_in_function_body;
18178   parser->num_template_parameter_lists
18179     = saved_num_template_parameter_lists;
18180   parser->in_unbraced_linkage_specification_p
18181     = saved_in_unbraced_linkage_specification_p;
18182
18183   return type;
18184 }
18185
18186 static tree
18187 cp_parser_class_specifier (cp_parser* parser)
18188 {
18189   tree ret;
18190   timevar_push (TV_PARSE_STRUCT);
18191   ret = cp_parser_class_specifier_1 (parser);
18192   timevar_pop (TV_PARSE_STRUCT);
18193   return ret;
18194 }
18195
18196 /* Parse a class-head.
18197
18198    class-head:
18199      class-key identifier [opt] base-clause [opt]
18200      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18201      class-key nested-name-specifier [opt] template-id
18202        base-clause [opt]
18203
18204    class-virt-specifier:
18205      final
18206
18207    GNU Extensions:
18208      class-key attributes identifier [opt] base-clause [opt]
18209      class-key attributes nested-name-specifier identifier base-clause [opt]
18210      class-key attributes nested-name-specifier [opt] template-id
18211        base-clause [opt]
18212
18213    Upon return BASES is initialized to the list of base classes (or
18214    NULL, if there are none) in the same form returned by
18215    cp_parser_base_clause.
18216
18217    Returns the TYPE of the indicated class.  Sets
18218    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18219    involving a nested-name-specifier was used, and FALSE otherwise.
18220
18221    Returns error_mark_node if this is not a class-head.
18222
18223    Returns NULL_TREE if the class-head is syntactically valid, but
18224    semantically invalid in a way that means we should skip the entire
18225    body of the class.  */
18226
18227 static tree
18228 cp_parser_class_head (cp_parser* parser,
18229                       bool* nested_name_specifier_p,
18230                       tree *attributes_p,
18231                       tree *bases)
18232 {
18233   tree nested_name_specifier;
18234   enum tag_types class_key;
18235   tree id = NULL_TREE;
18236   tree type = NULL_TREE;
18237   tree attributes;
18238   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18239   bool template_id_p = false;
18240   bool qualified_p = false;
18241   bool invalid_nested_name_p = false;
18242   bool invalid_explicit_specialization_p = false;
18243   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18244   tree pushed_scope = NULL_TREE;
18245   unsigned num_templates;
18246   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18247   /* Assume no nested-name-specifier will be present.  */
18248   *nested_name_specifier_p = false;
18249   /* Assume no template parameter lists will be used in defining the
18250      type.  */
18251   num_templates = 0;
18252   parser->colon_corrects_to_scope_p = false;
18253
18254   *bases = NULL_TREE;
18255
18256   /* Look for the class-key.  */
18257   class_key = cp_parser_class_key (parser);
18258   if (class_key == none_type)
18259     return error_mark_node;
18260
18261   /* Parse the attributes.  */
18262   attributes = cp_parser_attributes_opt (parser);
18263
18264   /* If the next token is `::', that is invalid -- but sometimes
18265      people do try to write:
18266
18267        struct ::S {};
18268
18269      Handle this gracefully by accepting the extra qualifier, and then
18270      issuing an error about it later if this really is a
18271      class-head.  If it turns out just to be an elaborated type
18272      specifier, remain silent.  */
18273   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18274     qualified_p = true;
18275
18276   push_deferring_access_checks (dk_no_check);
18277
18278   /* Determine the name of the class.  Begin by looking for an
18279      optional nested-name-specifier.  */
18280   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18281   nested_name_specifier
18282     = cp_parser_nested_name_specifier_opt (parser,
18283                                            /*typename_keyword_p=*/false,
18284                                            /*check_dependency_p=*/false,
18285                                            /*type_p=*/false,
18286                                            /*is_declaration=*/false);
18287   /* If there was a nested-name-specifier, then there *must* be an
18288      identifier.  */
18289   if (nested_name_specifier)
18290     {
18291       type_start_token = cp_lexer_peek_token (parser->lexer);
18292       /* Although the grammar says `identifier', it really means
18293          `class-name' or `template-name'.  You are only allowed to
18294          define a class that has already been declared with this
18295          syntax.
18296
18297          The proposed resolution for Core Issue 180 says that wherever
18298          you see `class T::X' you should treat `X' as a type-name.
18299
18300          It is OK to define an inaccessible class; for example:
18301
18302            class A { class B; };
18303            class A::B {};
18304
18305          We do not know if we will see a class-name, or a
18306          template-name.  We look for a class-name first, in case the
18307          class-name is a template-id; if we looked for the
18308          template-name first we would stop after the template-name.  */
18309       cp_parser_parse_tentatively (parser);
18310       type = cp_parser_class_name (parser,
18311                                    /*typename_keyword_p=*/false,
18312                                    /*template_keyword_p=*/false,
18313                                    class_type,
18314                                    /*check_dependency_p=*/false,
18315                                    /*class_head_p=*/true,
18316                                    /*is_declaration=*/false);
18317       /* If that didn't work, ignore the nested-name-specifier.  */
18318       if (!cp_parser_parse_definitely (parser))
18319         {
18320           invalid_nested_name_p = true;
18321           type_start_token = cp_lexer_peek_token (parser->lexer);
18322           id = cp_parser_identifier (parser);
18323           if (id == error_mark_node)
18324             id = NULL_TREE;
18325         }
18326       /* If we could not find a corresponding TYPE, treat this
18327          declaration like an unqualified declaration.  */
18328       if (type == error_mark_node)
18329         nested_name_specifier = NULL_TREE;
18330       /* Otherwise, count the number of templates used in TYPE and its
18331          containing scopes.  */
18332       else
18333         {
18334           tree scope;
18335
18336           for (scope = TREE_TYPE (type);
18337                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18338                scope = (TYPE_P (scope)
18339                         ? TYPE_CONTEXT (scope)
18340                         : DECL_CONTEXT (scope)))
18341             if (TYPE_P (scope)
18342                 && CLASS_TYPE_P (scope)
18343                 && CLASSTYPE_TEMPLATE_INFO (scope)
18344                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18345                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18346               ++num_templates;
18347         }
18348     }
18349   /* Otherwise, the identifier is optional.  */
18350   else
18351     {
18352       /* We don't know whether what comes next is a template-id,
18353          an identifier, or nothing at all.  */
18354       cp_parser_parse_tentatively (parser);
18355       /* Check for a template-id.  */
18356       type_start_token = cp_lexer_peek_token (parser->lexer);
18357       id = cp_parser_template_id (parser,
18358                                   /*template_keyword_p=*/false,
18359                                   /*check_dependency_p=*/true,
18360                                   /*is_declaration=*/true);
18361       /* If that didn't work, it could still be an identifier.  */
18362       if (!cp_parser_parse_definitely (parser))
18363         {
18364           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18365             {
18366               type_start_token = cp_lexer_peek_token (parser->lexer);
18367               id = cp_parser_identifier (parser);
18368             }
18369           else
18370             id = NULL_TREE;
18371         }
18372       else
18373         {
18374           template_id_p = true;
18375           ++num_templates;
18376         }
18377     }
18378
18379   pop_deferring_access_checks ();
18380
18381   if (id)
18382     {
18383       cp_parser_check_for_invalid_template_id (parser, id,
18384                                                type_start_token->location);
18385     }
18386   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18387
18388   /* If it's not a `:' or a `{' then we can't really be looking at a
18389      class-head, since a class-head only appears as part of a
18390      class-specifier.  We have to detect this situation before calling
18391      xref_tag, since that has irreversible side-effects.  */
18392   if (!cp_parser_next_token_starts_class_definition_p (parser))
18393     {
18394       cp_parser_error (parser, "expected %<{%> or %<:%>");
18395       type = error_mark_node;
18396       goto out;
18397     }
18398
18399   /* At this point, we're going ahead with the class-specifier, even
18400      if some other problem occurs.  */
18401   cp_parser_commit_to_tentative_parse (parser);
18402   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18403     {
18404       cp_parser_error (parser,
18405                        "cannot specify %<override%> for a class");
18406       type = error_mark_node;
18407       goto out;
18408     }
18409   /* Issue the error about the overly-qualified name now.  */
18410   if (qualified_p)
18411     {
18412       cp_parser_error (parser,
18413                        "global qualification of class name is invalid");
18414       type = error_mark_node;
18415       goto out;
18416     }
18417   else if (invalid_nested_name_p)
18418     {
18419       cp_parser_error (parser,
18420                        "qualified name does not name a class");
18421       type = error_mark_node;
18422       goto out;
18423     }
18424   else if (nested_name_specifier)
18425     {
18426       tree scope;
18427
18428       /* Reject typedef-names in class heads.  */
18429       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18430         {
18431           error_at (type_start_token->location,
18432                     "invalid class name in declaration of %qD",
18433                     type);
18434           type = NULL_TREE;
18435           goto done;
18436         }
18437
18438       /* Figure out in what scope the declaration is being placed.  */
18439       scope = current_scope ();
18440       /* If that scope does not contain the scope in which the
18441          class was originally declared, the program is invalid.  */
18442       if (scope && !is_ancestor (scope, nested_name_specifier))
18443         {
18444           if (at_namespace_scope_p ())
18445             error_at (type_start_token->location,
18446                       "declaration of %qD in namespace %qD which does not "
18447                       "enclose %qD",
18448                       type, scope, nested_name_specifier);
18449           else
18450             error_at (type_start_token->location,
18451                       "declaration of %qD in %qD which does not enclose %qD",
18452                       type, scope, nested_name_specifier);
18453           type = NULL_TREE;
18454           goto done;
18455         }
18456       /* [dcl.meaning]
18457
18458          A declarator-id shall not be qualified except for the
18459          definition of a ... nested class outside of its class
18460          ... [or] the definition or explicit instantiation of a
18461          class member of a namespace outside of its namespace.  */
18462       if (scope == nested_name_specifier)
18463         {
18464           permerror (nested_name_specifier_token_start->location,
18465                      "extra qualification not allowed");
18466           nested_name_specifier = NULL_TREE;
18467           num_templates = 0;
18468         }
18469     }
18470   /* An explicit-specialization must be preceded by "template <>".  If
18471      it is not, try to recover gracefully.  */
18472   if (at_namespace_scope_p ()
18473       && parser->num_template_parameter_lists == 0
18474       && template_id_p)
18475     {
18476       error_at (type_start_token->location,
18477                 "an explicit specialization must be preceded by %<template <>%>");
18478       invalid_explicit_specialization_p = true;
18479       /* Take the same action that would have been taken by
18480          cp_parser_explicit_specialization.  */
18481       ++parser->num_template_parameter_lists;
18482       begin_specialization ();
18483     }
18484   /* There must be no "return" statements between this point and the
18485      end of this function; set "type "to the correct return value and
18486      use "goto done;" to return.  */
18487   /* Make sure that the right number of template parameters were
18488      present.  */
18489   if (!cp_parser_check_template_parameters (parser, num_templates,
18490                                             type_start_token->location,
18491                                             /*declarator=*/NULL))
18492     {
18493       /* If something went wrong, there is no point in even trying to
18494          process the class-definition.  */
18495       type = NULL_TREE;
18496       goto done;
18497     }
18498
18499   /* Look up the type.  */
18500   if (template_id_p)
18501     {
18502       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18503           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18504               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18505         {
18506           error_at (type_start_token->location,
18507                     "function template %qD redeclared as a class template", id);
18508           type = error_mark_node;
18509         }
18510       else
18511         {
18512           type = TREE_TYPE (id);
18513           type = maybe_process_partial_specialization (type);
18514         }
18515       if (nested_name_specifier)
18516         pushed_scope = push_scope (nested_name_specifier);
18517     }
18518   else if (nested_name_specifier)
18519     {
18520       tree class_type;
18521
18522       /* Given:
18523
18524             template <typename T> struct S { struct T };
18525             template <typename T> struct S<T>::T { };
18526
18527          we will get a TYPENAME_TYPE when processing the definition of
18528          `S::T'.  We need to resolve it to the actual type before we
18529          try to define it.  */
18530       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18531         {
18532           class_type = resolve_typename_type (TREE_TYPE (type),
18533                                               /*only_current_p=*/false);
18534           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18535             type = TYPE_NAME (class_type);
18536           else
18537             {
18538               cp_parser_error (parser, "could not resolve typename type");
18539               type = error_mark_node;
18540             }
18541         }
18542
18543       if (maybe_process_partial_specialization (TREE_TYPE (type))
18544           == error_mark_node)
18545         {
18546           type = NULL_TREE;
18547           goto done;
18548         }
18549
18550       class_type = current_class_type;
18551       /* Enter the scope indicated by the nested-name-specifier.  */
18552       pushed_scope = push_scope (nested_name_specifier);
18553       /* Get the canonical version of this type.  */
18554       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18555       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18556           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18557         {
18558           type = push_template_decl (type);
18559           if (type == error_mark_node)
18560             {
18561               type = NULL_TREE;
18562               goto done;
18563             }
18564         }
18565
18566       type = TREE_TYPE (type);
18567       *nested_name_specifier_p = true;
18568     }
18569   else      /* The name is not a nested name.  */
18570     {
18571       /* If the class was unnamed, create a dummy name.  */
18572       if (!id)
18573         id = make_anon_name ();
18574       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18575                        parser->num_template_parameter_lists);
18576     }
18577
18578   /* Indicate whether this class was declared as a `class' or as a
18579      `struct'.  */
18580   if (TREE_CODE (type) == RECORD_TYPE)
18581     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18582   cp_parser_check_class_key (class_key, type);
18583
18584   /* If this type was already complete, and we see another definition,
18585      that's an error.  */
18586   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18587     {
18588       error_at (type_start_token->location, "redefinition of %q#T",
18589                 type);
18590       error_at (type_start_token->location, "previous definition of %q+#T",
18591                 type);
18592       type = NULL_TREE;
18593       goto done;
18594     }
18595   else if (type == error_mark_node)
18596     type = NULL_TREE;
18597
18598   /* We will have entered the scope containing the class; the names of
18599      base classes should be looked up in that context.  For example:
18600
18601        struct A { struct B {}; struct C; };
18602        struct A::C : B {};
18603
18604      is valid.  */
18605
18606   /* Get the list of base-classes, if there is one.  */
18607   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18608     *bases = cp_parser_base_clause (parser);
18609
18610  done:
18611   /* Leave the scope given by the nested-name-specifier.  We will
18612      enter the class scope itself while processing the members.  */
18613   if (pushed_scope)
18614     pop_scope (pushed_scope);
18615
18616   if (invalid_explicit_specialization_p)
18617     {
18618       end_specialization ();
18619       --parser->num_template_parameter_lists;
18620     }
18621
18622   if (type)
18623     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18624   *attributes_p = attributes;
18625   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18626     CLASSTYPE_FINAL (type) = 1;
18627  out:
18628   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18629   return type;
18630 }
18631
18632 /* Parse a class-key.
18633
18634    class-key:
18635      class
18636      struct
18637      union
18638
18639    Returns the kind of class-key specified, or none_type to indicate
18640    error.  */
18641
18642 static enum tag_types
18643 cp_parser_class_key (cp_parser* parser)
18644 {
18645   cp_token *token;
18646   enum tag_types tag_type;
18647
18648   /* Look for the class-key.  */
18649   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18650   if (!token)
18651     return none_type;
18652
18653   /* Check to see if the TOKEN is a class-key.  */
18654   tag_type = cp_parser_token_is_class_key (token);
18655   if (!tag_type)
18656     cp_parser_error (parser, "expected class-key");
18657   return tag_type;
18658 }
18659
18660 /* Parse an (optional) member-specification.
18661
18662    member-specification:
18663      member-declaration member-specification [opt]
18664      access-specifier : member-specification [opt]  */
18665
18666 static void
18667 cp_parser_member_specification_opt (cp_parser* parser)
18668 {
18669   while (true)
18670     {
18671       cp_token *token;
18672       enum rid keyword;
18673
18674       /* Peek at the next token.  */
18675       token = cp_lexer_peek_token (parser->lexer);
18676       /* If it's a `}', or EOF then we've seen all the members.  */
18677       if (token->type == CPP_CLOSE_BRACE
18678           || token->type == CPP_EOF
18679           || token->type == CPP_PRAGMA_EOL)
18680         break;
18681
18682       /* See if this token is a keyword.  */
18683       keyword = token->keyword;
18684       switch (keyword)
18685         {
18686         case RID_PUBLIC:
18687         case RID_PROTECTED:
18688         case RID_PRIVATE:
18689           /* Consume the access-specifier.  */
18690           cp_lexer_consume_token (parser->lexer);
18691           /* Remember which access-specifier is active.  */
18692           current_access_specifier = token->u.value;
18693           /* Look for the `:'.  */
18694           cp_parser_require (parser, CPP_COLON, RT_COLON);
18695           break;
18696
18697         default:
18698           /* Accept #pragmas at class scope.  */
18699           if (token->type == CPP_PRAGMA)
18700             {
18701               cp_parser_pragma (parser, pragma_external);
18702               break;
18703             }
18704
18705           /* Otherwise, the next construction must be a
18706              member-declaration.  */
18707           cp_parser_member_declaration (parser);
18708         }
18709     }
18710 }
18711
18712 /* Parse a member-declaration.
18713
18714    member-declaration:
18715      decl-specifier-seq [opt] member-declarator-list [opt] ;
18716      function-definition ; [opt]
18717      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18718      using-declaration
18719      template-declaration
18720      alias-declaration
18721
18722    member-declarator-list:
18723      member-declarator
18724      member-declarator-list , member-declarator
18725
18726    member-declarator:
18727      declarator pure-specifier [opt]
18728      declarator constant-initializer [opt]
18729      identifier [opt] : constant-expression
18730
18731    GNU Extensions:
18732
18733    member-declaration:
18734      __extension__ member-declaration
18735
18736    member-declarator:
18737      declarator attributes [opt] pure-specifier [opt]
18738      declarator attributes [opt] constant-initializer [opt]
18739      identifier [opt] attributes [opt] : constant-expression  
18740
18741    C++0x Extensions:
18742
18743    member-declaration:
18744      static_assert-declaration  */
18745
18746 static void
18747 cp_parser_member_declaration (cp_parser* parser)
18748 {
18749   cp_decl_specifier_seq decl_specifiers;
18750   tree prefix_attributes;
18751   tree decl;
18752   int declares_class_or_enum;
18753   bool friend_p;
18754   cp_token *token = NULL;
18755   cp_token *decl_spec_token_start = NULL;
18756   cp_token *initializer_token_start = NULL;
18757   int saved_pedantic;
18758   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18759
18760   /* Check for the `__extension__' keyword.  */
18761   if (cp_parser_extension_opt (parser, &saved_pedantic))
18762     {
18763       /* Recurse.  */
18764       cp_parser_member_declaration (parser);
18765       /* Restore the old value of the PEDANTIC flag.  */
18766       pedantic = saved_pedantic;
18767
18768       return;
18769     }
18770
18771   /* Check for a template-declaration.  */
18772   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18773     {
18774       /* An explicit specialization here is an error condition, and we
18775          expect the specialization handler to detect and report this.  */
18776       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18777           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18778         cp_parser_explicit_specialization (parser);
18779       else
18780         cp_parser_template_declaration (parser, /*member_p=*/true);
18781
18782       return;
18783     }
18784
18785   /* Check for a using-declaration.  */
18786   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18787     {
18788       if (cxx_dialect < cxx0x)
18789         {
18790           /* Parse the using-declaration.  */
18791           cp_parser_using_declaration (parser,
18792                                        /*access_declaration_p=*/false);
18793           return;
18794         }
18795       else
18796         {
18797           tree decl;
18798           cp_parser_parse_tentatively (parser);
18799           decl = cp_parser_alias_declaration (parser);
18800           if (cp_parser_parse_definitely (parser))
18801             finish_member_declaration (decl);
18802           else
18803             cp_parser_using_declaration (parser,
18804                                          /*access_declaration_p=*/false);
18805           return;
18806         }
18807     }
18808
18809   /* Check for @defs.  */
18810   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18811     {
18812       tree ivar, member;
18813       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18814       ivar = ivar_chains;
18815       while (ivar)
18816         {
18817           member = ivar;
18818           ivar = TREE_CHAIN (member);
18819           TREE_CHAIN (member) = NULL_TREE;
18820           finish_member_declaration (member);
18821         }
18822       return;
18823     }
18824
18825   /* If the next token is `static_assert' we have a static assertion.  */
18826   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18827     {
18828       cp_parser_static_assert (parser, /*member_p=*/true);
18829       return;
18830     }
18831
18832   parser->colon_corrects_to_scope_p = false;
18833
18834   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18835       goto out;
18836
18837   /* Parse the decl-specifier-seq.  */
18838   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18839   cp_parser_decl_specifier_seq (parser,
18840                                 CP_PARSER_FLAGS_OPTIONAL,
18841                                 &decl_specifiers,
18842                                 &declares_class_or_enum);
18843   prefix_attributes = decl_specifiers.attributes;
18844   decl_specifiers.attributes = NULL_TREE;
18845   /* Check for an invalid type-name.  */
18846   if (!decl_specifiers.any_type_specifiers_p
18847       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18848     goto out;
18849   /* If there is no declarator, then the decl-specifier-seq should
18850      specify a type.  */
18851   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18852     {
18853       /* If there was no decl-specifier-seq, and the next token is a
18854          `;', then we have something like:
18855
18856            struct S { ; };
18857
18858          [class.mem]
18859
18860          Each member-declaration shall declare at least one member
18861          name of the class.  */
18862       if (!decl_specifiers.any_specifiers_p)
18863         {
18864           cp_token *token = cp_lexer_peek_token (parser->lexer);
18865           if (!in_system_header_at (token->location))
18866             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18867         }
18868       else
18869         {
18870           tree type;
18871
18872           /* See if this declaration is a friend.  */
18873           friend_p = cp_parser_friend_p (&decl_specifiers);
18874           /* If there were decl-specifiers, check to see if there was
18875              a class-declaration.  */
18876           type = check_tag_decl (&decl_specifiers);
18877           /* Nested classes have already been added to the class, but
18878              a `friend' needs to be explicitly registered.  */
18879           if (friend_p)
18880             {
18881               /* If the `friend' keyword was present, the friend must
18882                  be introduced with a class-key.  */
18883                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18884                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18885                           "in C++03 a class-key must be used "
18886                           "when declaring a friend");
18887                /* In this case:
18888
18889                     template <typename T> struct A {
18890                       friend struct A<T>::B;
18891                     };
18892
18893                   A<T>::B will be represented by a TYPENAME_TYPE, and
18894                   therefore not recognized by check_tag_decl.  */
18895                if (!type)
18896                  {
18897                    type = decl_specifiers.type;
18898                    if (type && TREE_CODE (type) == TYPE_DECL)
18899                      type = TREE_TYPE (type);
18900                  }
18901                if (!type || !TYPE_P (type))
18902                  error_at (decl_spec_token_start->location,
18903                            "friend declaration does not name a class or "
18904                            "function");
18905                else
18906                  make_friend_class (current_class_type, type,
18907                                     /*complain=*/true);
18908             }
18909           /* If there is no TYPE, an error message will already have
18910              been issued.  */
18911           else if (!type || type == error_mark_node)
18912             ;
18913           /* An anonymous aggregate has to be handled specially; such
18914              a declaration really declares a data member (with a
18915              particular type), as opposed to a nested class.  */
18916           else if (ANON_AGGR_TYPE_P (type))
18917             {
18918               /* Remove constructors and such from TYPE, now that we
18919                  know it is an anonymous aggregate.  */
18920               fixup_anonymous_aggr (type);
18921               /* And make the corresponding data member.  */
18922               decl = build_decl (decl_spec_token_start->location,
18923                                  FIELD_DECL, NULL_TREE, type);
18924               /* Add it to the class.  */
18925               finish_member_declaration (decl);
18926             }
18927           else
18928             cp_parser_check_access_in_redeclaration
18929                                               (TYPE_NAME (type),
18930                                                decl_spec_token_start->location);
18931         }
18932     }
18933   else
18934     {
18935       bool assume_semicolon = false;
18936
18937       /* See if these declarations will be friends.  */
18938       friend_p = cp_parser_friend_p (&decl_specifiers);
18939
18940       /* Keep going until we hit the `;' at the end of the
18941          declaration.  */
18942       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18943         {
18944           tree attributes = NULL_TREE;
18945           tree first_attribute;
18946
18947           /* Peek at the next token.  */
18948           token = cp_lexer_peek_token (parser->lexer);
18949
18950           /* Check for a bitfield declaration.  */
18951           if (token->type == CPP_COLON
18952               || (token->type == CPP_NAME
18953                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18954                   == CPP_COLON))
18955             {
18956               tree identifier;
18957               tree width;
18958
18959               /* Get the name of the bitfield.  Note that we cannot just
18960                  check TOKEN here because it may have been invalidated by
18961                  the call to cp_lexer_peek_nth_token above.  */
18962               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18963                 identifier = cp_parser_identifier (parser);
18964               else
18965                 identifier = NULL_TREE;
18966
18967               /* Consume the `:' token.  */
18968               cp_lexer_consume_token (parser->lexer);
18969               /* Get the width of the bitfield.  */
18970               width
18971                 = cp_parser_constant_expression (parser,
18972                                                  /*allow_non_constant=*/false,
18973                                                  NULL);
18974
18975               /* Look for attributes that apply to the bitfield.  */
18976               attributes = cp_parser_attributes_opt (parser);
18977               /* Remember which attributes are prefix attributes and
18978                  which are not.  */
18979               first_attribute = attributes;
18980               /* Combine the attributes.  */
18981               attributes = chainon (prefix_attributes, attributes);
18982
18983               /* Create the bitfield declaration.  */
18984               decl = grokbitfield (identifier
18985                                    ? make_id_declarator (NULL_TREE,
18986                                                          identifier,
18987                                                          sfk_none)
18988                                    : NULL,
18989                                    &decl_specifiers,
18990                                    width,
18991                                    attributes);
18992             }
18993           else
18994             {
18995               cp_declarator *declarator;
18996               tree initializer;
18997               tree asm_specification;
18998               int ctor_dtor_or_conv_p;
18999
19000               /* Parse the declarator.  */
19001               declarator
19002                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19003                                         &ctor_dtor_or_conv_p,
19004                                         /*parenthesized_p=*/NULL,
19005                                         /*member_p=*/true);
19006
19007               /* If something went wrong parsing the declarator, make sure
19008                  that we at least consume some tokens.  */
19009               if (declarator == cp_error_declarator)
19010                 {
19011                   /* Skip to the end of the statement.  */
19012                   cp_parser_skip_to_end_of_statement (parser);
19013                   /* If the next token is not a semicolon, that is
19014                      probably because we just skipped over the body of
19015                      a function.  So, we consume a semicolon if
19016                      present, but do not issue an error message if it
19017                      is not present.  */
19018                   if (cp_lexer_next_token_is (parser->lexer,
19019                                               CPP_SEMICOLON))
19020                     cp_lexer_consume_token (parser->lexer);
19021                   goto out;
19022                 }
19023
19024               if (declares_class_or_enum & 2)
19025                 cp_parser_check_for_definition_in_return_type
19026                                             (declarator, decl_specifiers.type,
19027                                              decl_specifiers.type_location);
19028
19029               /* Look for an asm-specification.  */
19030               asm_specification = cp_parser_asm_specification_opt (parser);
19031               /* Look for attributes that apply to the declaration.  */
19032               attributes = cp_parser_attributes_opt (parser);
19033               /* Remember which attributes are prefix attributes and
19034                  which are not.  */
19035               first_attribute = attributes;
19036               /* Combine the attributes.  */
19037               attributes = chainon (prefix_attributes, attributes);
19038
19039               /* If it's an `=', then we have a constant-initializer or a
19040                  pure-specifier.  It is not correct to parse the
19041                  initializer before registering the member declaration
19042                  since the member declaration should be in scope while
19043                  its initializer is processed.  However, the rest of the
19044                  front end does not yet provide an interface that allows
19045                  us to handle this correctly.  */
19046               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19047                 {
19048                   /* In [class.mem]:
19049
19050                      A pure-specifier shall be used only in the declaration of
19051                      a virtual function.
19052
19053                      A member-declarator can contain a constant-initializer
19054                      only if it declares a static member of integral or
19055                      enumeration type.
19056
19057                      Therefore, if the DECLARATOR is for a function, we look
19058                      for a pure-specifier; otherwise, we look for a
19059                      constant-initializer.  When we call `grokfield', it will
19060                      perform more stringent semantics checks.  */
19061                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19062                   if (function_declarator_p (declarator)
19063                       || (decl_specifiers.type
19064                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19065                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19066                               == FUNCTION_TYPE)))
19067                     initializer = cp_parser_pure_specifier (parser);
19068                   else if (decl_specifiers.storage_class != sc_static)
19069                     initializer = cp_parser_save_nsdmi (parser);
19070                   else if (cxx_dialect >= cxx0x)
19071                     {
19072                       bool nonconst;
19073                       /* Don't require a constant rvalue in C++11, since we
19074                          might want a reference constant.  We'll enforce
19075                          constancy later.  */
19076                       cp_lexer_consume_token (parser->lexer);
19077                       /* Parse the initializer.  */
19078                       initializer = cp_parser_initializer_clause (parser,
19079                                                                   &nonconst);
19080                     }
19081                   else
19082                     /* Parse the initializer.  */
19083                     initializer = cp_parser_constant_initializer (parser);
19084                 }
19085               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19086                        && !function_declarator_p (declarator))
19087                 {
19088                   bool x;
19089                   if (decl_specifiers.storage_class != sc_static)
19090                     initializer = cp_parser_save_nsdmi (parser);
19091                   else
19092                     initializer = cp_parser_initializer (parser, &x, &x);
19093                 }
19094               /* Otherwise, there is no initializer.  */
19095               else
19096                 initializer = NULL_TREE;
19097
19098               /* See if we are probably looking at a function
19099                  definition.  We are certainly not looking at a
19100                  member-declarator.  Calling `grokfield' has
19101                  side-effects, so we must not do it unless we are sure
19102                  that we are looking at a member-declarator.  */
19103               if (cp_parser_token_starts_function_definition_p
19104                   (cp_lexer_peek_token (parser->lexer)))
19105                 {
19106                   /* The grammar does not allow a pure-specifier to be
19107                      used when a member function is defined.  (It is
19108                      possible that this fact is an oversight in the
19109                      standard, since a pure function may be defined
19110                      outside of the class-specifier.  */
19111                   if (initializer && initializer_token_start)
19112                     error_at (initializer_token_start->location,
19113                               "pure-specifier on function-definition");
19114                   decl = cp_parser_save_member_function_body (parser,
19115                                                               &decl_specifiers,
19116                                                               declarator,
19117                                                               attributes);
19118                   /* If the member was not a friend, declare it here.  */
19119                   if (!friend_p)
19120                     finish_member_declaration (decl);
19121                   /* Peek at the next token.  */
19122                   token = cp_lexer_peek_token (parser->lexer);
19123                   /* If the next token is a semicolon, consume it.  */
19124                   if (token->type == CPP_SEMICOLON)
19125                     cp_lexer_consume_token (parser->lexer);
19126                   goto out;
19127                 }
19128               else
19129                 if (declarator->kind == cdk_function)
19130                   declarator->id_loc = token->location;
19131                 /* Create the declaration.  */
19132                 decl = grokfield (declarator, &decl_specifiers,
19133                                   initializer, /*init_const_expr_p=*/true,
19134                                   asm_specification,
19135                                   attributes);
19136             }
19137
19138           /* Reset PREFIX_ATTRIBUTES.  */
19139           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19140             attributes = TREE_CHAIN (attributes);
19141           if (attributes)
19142             TREE_CHAIN (attributes) = NULL_TREE;
19143
19144           /* If there is any qualification still in effect, clear it
19145              now; we will be starting fresh with the next declarator.  */
19146           parser->scope = NULL_TREE;
19147           parser->qualifying_scope = NULL_TREE;
19148           parser->object_scope = NULL_TREE;
19149           /* If it's a `,', then there are more declarators.  */
19150           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19151             cp_lexer_consume_token (parser->lexer);
19152           /* If the next token isn't a `;', then we have a parse error.  */
19153           else if (cp_lexer_next_token_is_not (parser->lexer,
19154                                                CPP_SEMICOLON))
19155             {
19156               /* The next token might be a ways away from where the
19157                  actual semicolon is missing.  Find the previous token
19158                  and use that for our error position.  */
19159               cp_token *token = cp_lexer_previous_token (parser->lexer);
19160               error_at (token->location,
19161                         "expected %<;%> at end of member declaration");
19162
19163               /* Assume that the user meant to provide a semicolon.  If
19164                  we were to cp_parser_skip_to_end_of_statement, we might
19165                  skip to a semicolon inside a member function definition
19166                  and issue nonsensical error messages.  */
19167               assume_semicolon = true;
19168             }
19169
19170           if (decl)
19171             {
19172               /* Add DECL to the list of members.  */
19173               if (!friend_p)
19174                 finish_member_declaration (decl);
19175
19176               if (TREE_CODE (decl) == FUNCTION_DECL)
19177                 cp_parser_save_default_args (parser, decl);
19178               else if (TREE_CODE (decl) == FIELD_DECL
19179                        && !DECL_C_BIT_FIELD (decl)
19180                        && DECL_INITIAL (decl))
19181                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19182                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19183             }
19184
19185           if (assume_semicolon)
19186             goto out;
19187         }
19188     }
19189
19190   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19191  out:
19192   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19193 }
19194
19195 /* Parse a pure-specifier.
19196
19197    pure-specifier:
19198      = 0
19199
19200    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19201    Otherwise, ERROR_MARK_NODE is returned.  */
19202
19203 static tree
19204 cp_parser_pure_specifier (cp_parser* parser)
19205 {
19206   cp_token *token;
19207
19208   /* Look for the `=' token.  */
19209   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19210     return error_mark_node;
19211   /* Look for the `0' token.  */
19212   token = cp_lexer_peek_token (parser->lexer);
19213
19214   if (token->type == CPP_EOF
19215       || token->type == CPP_PRAGMA_EOL)
19216     return error_mark_node;
19217
19218   cp_lexer_consume_token (parser->lexer);
19219
19220   /* Accept = default or = delete in c++0x mode.  */
19221   if (token->keyword == RID_DEFAULT
19222       || token->keyword == RID_DELETE)
19223     {
19224       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19225       return token->u.value;
19226     }
19227
19228   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19229   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19230     {
19231       cp_parser_error (parser,
19232                        "invalid pure specifier (only %<= 0%> is allowed)");
19233       cp_parser_skip_to_end_of_statement (parser);
19234       return error_mark_node;
19235     }
19236   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19237     {
19238       error_at (token->location, "templates may not be %<virtual%>");
19239       return error_mark_node;
19240     }
19241
19242   return integer_zero_node;
19243 }
19244
19245 /* Parse a constant-initializer.
19246
19247    constant-initializer:
19248      = constant-expression
19249
19250    Returns a representation of the constant-expression.  */
19251
19252 static tree
19253 cp_parser_constant_initializer (cp_parser* parser)
19254 {
19255   /* Look for the `=' token.  */
19256   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19257     return error_mark_node;
19258
19259   /* It is invalid to write:
19260
19261        struct S { static const int i = { 7 }; };
19262
19263      */
19264   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19265     {
19266       cp_parser_error (parser,
19267                        "a brace-enclosed initializer is not allowed here");
19268       /* Consume the opening brace.  */
19269       cp_lexer_consume_token (parser->lexer);
19270       /* Skip the initializer.  */
19271       cp_parser_skip_to_closing_brace (parser);
19272       /* Look for the trailing `}'.  */
19273       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19274
19275       return error_mark_node;
19276     }
19277
19278   return cp_parser_constant_expression (parser,
19279                                         /*allow_non_constant=*/false,
19280                                         NULL);
19281 }
19282
19283 /* Derived classes [gram.class.derived] */
19284
19285 /* Parse a base-clause.
19286
19287    base-clause:
19288      : base-specifier-list
19289
19290    base-specifier-list:
19291      base-specifier ... [opt]
19292      base-specifier-list , base-specifier ... [opt]
19293
19294    Returns a TREE_LIST representing the base-classes, in the order in
19295    which they were declared.  The representation of each node is as
19296    described by cp_parser_base_specifier.
19297
19298    In the case that no bases are specified, this function will return
19299    NULL_TREE, not ERROR_MARK_NODE.  */
19300
19301 static tree
19302 cp_parser_base_clause (cp_parser* parser)
19303 {
19304   tree bases = NULL_TREE;
19305
19306   /* Look for the `:' that begins the list.  */
19307   cp_parser_require (parser, CPP_COLON, RT_COLON);
19308
19309   /* Scan the base-specifier-list.  */
19310   while (true)
19311     {
19312       cp_token *token;
19313       tree base;
19314       bool pack_expansion_p = false;
19315
19316       /* Look for the base-specifier.  */
19317       base = cp_parser_base_specifier (parser);
19318       /* Look for the (optional) ellipsis. */
19319       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19320         {
19321           /* Consume the `...'. */
19322           cp_lexer_consume_token (parser->lexer);
19323
19324           pack_expansion_p = true;
19325         }
19326
19327       /* Add BASE to the front of the list.  */
19328       if (base && base != error_mark_node)
19329         {
19330           if (pack_expansion_p)
19331             /* Make this a pack expansion type. */
19332             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19333
19334           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19335             {
19336               TREE_CHAIN (base) = bases;
19337               bases = base;
19338             }
19339         }
19340       /* Peek at the next token.  */
19341       token = cp_lexer_peek_token (parser->lexer);
19342       /* If it's not a comma, then the list is complete.  */
19343       if (token->type != CPP_COMMA)
19344         break;
19345       /* Consume the `,'.  */
19346       cp_lexer_consume_token (parser->lexer);
19347     }
19348
19349   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19350      base class had a qualified name.  However, the next name that
19351      appears is certainly not qualified.  */
19352   parser->scope = NULL_TREE;
19353   parser->qualifying_scope = NULL_TREE;
19354   parser->object_scope = NULL_TREE;
19355
19356   return nreverse (bases);
19357 }
19358
19359 /* Parse a base-specifier.
19360
19361    base-specifier:
19362      :: [opt] nested-name-specifier [opt] class-name
19363      virtual access-specifier [opt] :: [opt] nested-name-specifier
19364        [opt] class-name
19365      access-specifier virtual [opt] :: [opt] nested-name-specifier
19366        [opt] class-name
19367
19368    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19369    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19370    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19371    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19372
19373 static tree
19374 cp_parser_base_specifier (cp_parser* parser)
19375 {
19376   cp_token *token;
19377   bool done = false;
19378   bool virtual_p = false;
19379   bool duplicate_virtual_error_issued_p = false;
19380   bool duplicate_access_error_issued_p = false;
19381   bool class_scope_p, template_p;
19382   tree access = access_default_node;
19383   tree type;
19384
19385   /* Process the optional `virtual' and `access-specifier'.  */
19386   while (!done)
19387     {
19388       /* Peek at the next token.  */
19389       token = cp_lexer_peek_token (parser->lexer);
19390       /* Process `virtual'.  */
19391       switch (token->keyword)
19392         {
19393         case RID_VIRTUAL:
19394           /* If `virtual' appears more than once, issue an error.  */
19395           if (virtual_p && !duplicate_virtual_error_issued_p)
19396             {
19397               cp_parser_error (parser,
19398                                "%<virtual%> specified more than once in base-specified");
19399               duplicate_virtual_error_issued_p = true;
19400             }
19401
19402           virtual_p = true;
19403
19404           /* Consume the `virtual' token.  */
19405           cp_lexer_consume_token (parser->lexer);
19406
19407           break;
19408
19409         case RID_PUBLIC:
19410         case RID_PROTECTED:
19411         case RID_PRIVATE:
19412           /* If more than one access specifier appears, issue an
19413              error.  */
19414           if (access != access_default_node
19415               && !duplicate_access_error_issued_p)
19416             {
19417               cp_parser_error (parser,
19418                                "more than one access specifier in base-specified");
19419               duplicate_access_error_issued_p = true;
19420             }
19421
19422           access = ridpointers[(int) token->keyword];
19423
19424           /* Consume the access-specifier.  */
19425           cp_lexer_consume_token (parser->lexer);
19426
19427           break;
19428
19429         default:
19430           done = true;
19431           break;
19432         }
19433     }
19434   /* It is not uncommon to see programs mechanically, erroneously, use
19435      the 'typename' keyword to denote (dependent) qualified types
19436      as base classes.  */
19437   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19438     {
19439       token = cp_lexer_peek_token (parser->lexer);
19440       if (!processing_template_decl)
19441         error_at (token->location,
19442                   "keyword %<typename%> not allowed outside of templates");
19443       else
19444         error_at (token->location,
19445                   "keyword %<typename%> not allowed in this context "
19446                   "(the base class is implicitly a type)");
19447       cp_lexer_consume_token (parser->lexer);
19448     }
19449
19450   /* Look for the optional `::' operator.  */
19451   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19452   /* Look for the nested-name-specifier.  The simplest way to
19453      implement:
19454
19455        [temp.res]
19456
19457        The keyword `typename' is not permitted in a base-specifier or
19458        mem-initializer; in these contexts a qualified name that
19459        depends on a template-parameter is implicitly assumed to be a
19460        type name.
19461
19462      is to pretend that we have seen the `typename' keyword at this
19463      point.  */
19464   cp_parser_nested_name_specifier_opt (parser,
19465                                        /*typename_keyword_p=*/true,
19466                                        /*check_dependency_p=*/true,
19467                                        typename_type,
19468                                        /*is_declaration=*/true);
19469   /* If the base class is given by a qualified name, assume that names
19470      we see are type names or templates, as appropriate.  */
19471   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19472   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19473
19474   if (!parser->scope
19475       && cp_lexer_next_token_is_decltype (parser->lexer))
19476     /* DR 950 allows decltype as a base-specifier.  */
19477     type = cp_parser_decltype (parser);
19478   else
19479     {
19480       /* Otherwise, look for the class-name.  */
19481       type = cp_parser_class_name (parser,
19482                                    class_scope_p,
19483                                    template_p,
19484                                    typename_type,
19485                                    /*check_dependency_p=*/true,
19486                                    /*class_head_p=*/false,
19487                                    /*is_declaration=*/true);
19488       type = TREE_TYPE (type);
19489     }
19490
19491   if (type == error_mark_node)
19492     return error_mark_node;
19493
19494   return finish_base_specifier (type, access, virtual_p);
19495 }
19496
19497 /* Exception handling [gram.exception] */
19498
19499 /* Parse an (optional) noexcept-specification.
19500
19501    noexcept-specification:
19502      noexcept ( constant-expression ) [opt]
19503
19504    If no noexcept-specification is present, returns NULL_TREE.
19505    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19506    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19507    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19508    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19509    in which case a boolean condition is returned instead.  */
19510
19511 static tree
19512 cp_parser_noexcept_specification_opt (cp_parser* parser,
19513                                       bool require_constexpr,
19514                                       bool* consumed_expr,
19515                                       bool return_cond)
19516 {
19517   cp_token *token;
19518   const char *saved_message;
19519
19520   /* Peek at the next token.  */
19521   token = cp_lexer_peek_token (parser->lexer);
19522
19523   /* Is it a noexcept-specification?  */
19524   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19525     {
19526       tree expr;
19527       cp_lexer_consume_token (parser->lexer);
19528
19529       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19530         {
19531           cp_lexer_consume_token (parser->lexer);
19532
19533           if (require_constexpr)
19534             {
19535               /* Types may not be defined in an exception-specification.  */
19536               saved_message = parser->type_definition_forbidden_message;
19537               parser->type_definition_forbidden_message
19538               = G_("types may not be defined in an exception-specification");
19539
19540               expr = cp_parser_constant_expression (parser, false, NULL);
19541
19542               /* Restore the saved message.  */
19543               parser->type_definition_forbidden_message = saved_message;
19544             }
19545           else
19546             {
19547               expr = cp_parser_expression (parser, false, NULL);
19548               *consumed_expr = true;
19549             }
19550
19551           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19552         }
19553       else
19554         {
19555           expr = boolean_true_node;
19556           if (!require_constexpr)
19557             *consumed_expr = false;
19558         }
19559
19560       /* We cannot build a noexcept-spec right away because this will check
19561          that expr is a constexpr.  */
19562       if (!return_cond)
19563         return build_noexcept_spec (expr, tf_warning_or_error);
19564       else
19565         return expr;
19566     }
19567   else
19568     return NULL_TREE;
19569 }
19570
19571 /* Parse an (optional) exception-specification.
19572
19573    exception-specification:
19574      throw ( type-id-list [opt] )
19575
19576    Returns a TREE_LIST representing the exception-specification.  The
19577    TREE_VALUE of each node is a type.  */
19578
19579 static tree
19580 cp_parser_exception_specification_opt (cp_parser* parser)
19581 {
19582   cp_token *token;
19583   tree type_id_list;
19584   const char *saved_message;
19585
19586   /* Peek at the next token.  */
19587   token = cp_lexer_peek_token (parser->lexer);
19588
19589   /* Is it a noexcept-specification?  */
19590   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19591                                                       false);
19592   if (type_id_list != NULL_TREE)
19593     return type_id_list;
19594
19595   /* If it's not `throw', then there's no exception-specification.  */
19596   if (!cp_parser_is_keyword (token, RID_THROW))
19597     return NULL_TREE;
19598
19599 #if 0
19600   /* Enable this once a lot of code has transitioned to noexcept?  */
19601   if (cxx_dialect == cxx0x && !in_system_header)
19602     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19603              "deprecated in C++0x; use %<noexcept%> instead");
19604 #endif
19605
19606   /* Consume the `throw'.  */
19607   cp_lexer_consume_token (parser->lexer);
19608
19609   /* Look for the `('.  */
19610   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19611
19612   /* Peek at the next token.  */
19613   token = cp_lexer_peek_token (parser->lexer);
19614   /* If it's not a `)', then there is a type-id-list.  */
19615   if (token->type != CPP_CLOSE_PAREN)
19616     {
19617       /* Types may not be defined in an exception-specification.  */
19618       saved_message = parser->type_definition_forbidden_message;
19619       parser->type_definition_forbidden_message
19620         = G_("types may not be defined in an exception-specification");
19621       /* Parse the type-id-list.  */
19622       type_id_list = cp_parser_type_id_list (parser);
19623       /* Restore the saved message.  */
19624       parser->type_definition_forbidden_message = saved_message;
19625     }
19626   else
19627     type_id_list = empty_except_spec;
19628
19629   /* Look for the `)'.  */
19630   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19631
19632   return type_id_list;
19633 }
19634
19635 /* Parse an (optional) type-id-list.
19636
19637    type-id-list:
19638      type-id ... [opt]
19639      type-id-list , type-id ... [opt]
19640
19641    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19642    in the order that the types were presented.  */
19643
19644 static tree
19645 cp_parser_type_id_list (cp_parser* parser)
19646 {
19647   tree types = NULL_TREE;
19648
19649   while (true)
19650     {
19651       cp_token *token;
19652       tree type;
19653
19654       /* Get the next type-id.  */
19655       type = cp_parser_type_id (parser);
19656       /* Parse the optional ellipsis. */
19657       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19658         {
19659           /* Consume the `...'. */
19660           cp_lexer_consume_token (parser->lexer);
19661
19662           /* Turn the type into a pack expansion expression. */
19663           type = make_pack_expansion (type);
19664         }
19665       /* Add it to the list.  */
19666       types = add_exception_specifier (types, type, /*complain=*/1);
19667       /* Peek at the next token.  */
19668       token = cp_lexer_peek_token (parser->lexer);
19669       /* If it is not a `,', we are done.  */
19670       if (token->type != CPP_COMMA)
19671         break;
19672       /* Consume the `,'.  */
19673       cp_lexer_consume_token (parser->lexer);
19674     }
19675
19676   return nreverse (types);
19677 }
19678
19679 /* Parse a try-block.
19680
19681    try-block:
19682      try compound-statement handler-seq  */
19683
19684 static tree
19685 cp_parser_try_block (cp_parser* parser)
19686 {
19687   tree try_block;
19688
19689   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19690   try_block = begin_try_block ();
19691   cp_parser_compound_statement (parser, NULL, true, false);
19692   finish_try_block (try_block);
19693   cp_parser_handler_seq (parser);
19694   finish_handler_sequence (try_block);
19695
19696   return try_block;
19697 }
19698
19699 /* Parse a function-try-block.
19700
19701    function-try-block:
19702      try ctor-initializer [opt] function-body handler-seq  */
19703
19704 static bool
19705 cp_parser_function_try_block (cp_parser* parser)
19706 {
19707   tree compound_stmt;
19708   tree try_block;
19709   bool ctor_initializer_p;
19710
19711   /* Look for the `try' keyword.  */
19712   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19713     return false;
19714   /* Let the rest of the front end know where we are.  */
19715   try_block = begin_function_try_block (&compound_stmt);
19716   /* Parse the function-body.  */
19717   ctor_initializer_p
19718     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19719   /* We're done with the `try' part.  */
19720   finish_function_try_block (try_block);
19721   /* Parse the handlers.  */
19722   cp_parser_handler_seq (parser);
19723   /* We're done with the handlers.  */
19724   finish_function_handler_sequence (try_block, compound_stmt);
19725
19726   return ctor_initializer_p;
19727 }
19728
19729 /* Parse a handler-seq.
19730
19731    handler-seq:
19732      handler handler-seq [opt]  */
19733
19734 static void
19735 cp_parser_handler_seq (cp_parser* parser)
19736 {
19737   while (true)
19738     {
19739       cp_token *token;
19740
19741       /* Parse the handler.  */
19742       cp_parser_handler (parser);
19743       /* Peek at the next token.  */
19744       token = cp_lexer_peek_token (parser->lexer);
19745       /* If it's not `catch' then there are no more handlers.  */
19746       if (!cp_parser_is_keyword (token, RID_CATCH))
19747         break;
19748     }
19749 }
19750
19751 /* Parse a handler.
19752
19753    handler:
19754      catch ( exception-declaration ) compound-statement  */
19755
19756 static void
19757 cp_parser_handler (cp_parser* parser)
19758 {
19759   tree handler;
19760   tree declaration;
19761
19762   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19763   handler = begin_handler ();
19764   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19765   declaration = cp_parser_exception_declaration (parser);
19766   finish_handler_parms (declaration, handler);
19767   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19768   cp_parser_compound_statement (parser, NULL, false, false);
19769   finish_handler (handler);
19770 }
19771
19772 /* Parse an exception-declaration.
19773
19774    exception-declaration:
19775      type-specifier-seq declarator
19776      type-specifier-seq abstract-declarator
19777      type-specifier-seq
19778      ...
19779
19780    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19781    ellipsis variant is used.  */
19782
19783 static tree
19784 cp_parser_exception_declaration (cp_parser* parser)
19785 {
19786   cp_decl_specifier_seq type_specifiers;
19787   cp_declarator *declarator;
19788   const char *saved_message;
19789
19790   /* If it's an ellipsis, it's easy to handle.  */
19791   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19792     {
19793       /* Consume the `...' token.  */
19794       cp_lexer_consume_token (parser->lexer);
19795       return NULL_TREE;
19796     }
19797
19798   /* Types may not be defined in exception-declarations.  */
19799   saved_message = parser->type_definition_forbidden_message;
19800   parser->type_definition_forbidden_message
19801     = G_("types may not be defined in exception-declarations");
19802
19803   /* Parse the type-specifier-seq.  */
19804   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19805                                 /*is_trailing_return=*/false,
19806                                 &type_specifiers);
19807   /* If it's a `)', then there is no declarator.  */
19808   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19809     declarator = NULL;
19810   else
19811     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19812                                        /*ctor_dtor_or_conv_p=*/NULL,
19813                                        /*parenthesized_p=*/NULL,
19814                                        /*member_p=*/false);
19815
19816   /* Restore the saved message.  */
19817   parser->type_definition_forbidden_message = saved_message;
19818
19819   if (!type_specifiers.any_specifiers_p)
19820     return error_mark_node;
19821
19822   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19823 }
19824
19825 /* Parse a throw-expression.
19826
19827    throw-expression:
19828      throw assignment-expression [opt]
19829
19830    Returns a THROW_EXPR representing the throw-expression.  */
19831
19832 static tree
19833 cp_parser_throw_expression (cp_parser* parser)
19834 {
19835   tree expression;
19836   cp_token* token;
19837
19838   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19839   token = cp_lexer_peek_token (parser->lexer);
19840   /* Figure out whether or not there is an assignment-expression
19841      following the "throw" keyword.  */
19842   if (token->type == CPP_COMMA
19843       || token->type == CPP_SEMICOLON
19844       || token->type == CPP_CLOSE_PAREN
19845       || token->type == CPP_CLOSE_SQUARE
19846       || token->type == CPP_CLOSE_BRACE
19847       || token->type == CPP_COLON)
19848     expression = NULL_TREE;
19849   else
19850     expression = cp_parser_assignment_expression (parser,
19851                                                   /*cast_p=*/false, NULL);
19852
19853   return build_throw (expression);
19854 }
19855
19856 /* GNU Extensions */
19857
19858 /* Parse an (optional) asm-specification.
19859
19860    asm-specification:
19861      asm ( string-literal )
19862
19863    If the asm-specification is present, returns a STRING_CST
19864    corresponding to the string-literal.  Otherwise, returns
19865    NULL_TREE.  */
19866
19867 static tree
19868 cp_parser_asm_specification_opt (cp_parser* parser)
19869 {
19870   cp_token *token;
19871   tree asm_specification;
19872
19873   /* Peek at the next token.  */
19874   token = cp_lexer_peek_token (parser->lexer);
19875   /* If the next token isn't the `asm' keyword, then there's no
19876      asm-specification.  */
19877   if (!cp_parser_is_keyword (token, RID_ASM))
19878     return NULL_TREE;
19879
19880   /* Consume the `asm' token.  */
19881   cp_lexer_consume_token (parser->lexer);
19882   /* Look for the `('.  */
19883   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19884
19885   /* Look for the string-literal.  */
19886   asm_specification = cp_parser_string_literal (parser, false, false);
19887
19888   /* Look for the `)'.  */
19889   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19890
19891   return asm_specification;
19892 }
19893
19894 /* Parse an asm-operand-list.
19895
19896    asm-operand-list:
19897      asm-operand
19898      asm-operand-list , asm-operand
19899
19900    asm-operand:
19901      string-literal ( expression )
19902      [ string-literal ] string-literal ( expression )
19903
19904    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19905    each node is the expression.  The TREE_PURPOSE is itself a
19906    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19907    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19908    is a STRING_CST for the string literal before the parenthesis. Returns
19909    ERROR_MARK_NODE if any of the operands are invalid.  */
19910
19911 static tree
19912 cp_parser_asm_operand_list (cp_parser* parser)
19913 {
19914   tree asm_operands = NULL_TREE;
19915   bool invalid_operands = false;
19916
19917   while (true)
19918     {
19919       tree string_literal;
19920       tree expression;
19921       tree name;
19922
19923       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19924         {
19925           /* Consume the `[' token.  */
19926           cp_lexer_consume_token (parser->lexer);
19927           /* Read the operand name.  */
19928           name = cp_parser_identifier (parser);
19929           if (name != error_mark_node)
19930             name = build_string (IDENTIFIER_LENGTH (name),
19931                                  IDENTIFIER_POINTER (name));
19932           /* Look for the closing `]'.  */
19933           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19934         }
19935       else
19936         name = NULL_TREE;
19937       /* Look for the string-literal.  */
19938       string_literal = cp_parser_string_literal (parser, false, false);
19939
19940       /* Look for the `('.  */
19941       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19942       /* Parse the expression.  */
19943       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19944       /* Look for the `)'.  */
19945       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19946
19947       if (name == error_mark_node 
19948           || string_literal == error_mark_node 
19949           || expression == error_mark_node)
19950         invalid_operands = true;
19951
19952       /* Add this operand to the list.  */
19953       asm_operands = tree_cons (build_tree_list (name, string_literal),
19954                                 expression,
19955                                 asm_operands);
19956       /* If the next token is not a `,', there are no more
19957          operands.  */
19958       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19959         break;
19960       /* Consume the `,'.  */
19961       cp_lexer_consume_token (parser->lexer);
19962     }
19963
19964   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19965 }
19966
19967 /* Parse an asm-clobber-list.
19968
19969    asm-clobber-list:
19970      string-literal
19971      asm-clobber-list , string-literal
19972
19973    Returns a TREE_LIST, indicating the clobbers in the order that they
19974    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19975
19976 static tree
19977 cp_parser_asm_clobber_list (cp_parser* parser)
19978 {
19979   tree clobbers = NULL_TREE;
19980
19981   while (true)
19982     {
19983       tree string_literal;
19984
19985       /* Look for the string literal.  */
19986       string_literal = cp_parser_string_literal (parser, false, false);
19987       /* Add it to the list.  */
19988       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19989       /* If the next token is not a `,', then the list is
19990          complete.  */
19991       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19992         break;
19993       /* Consume the `,' token.  */
19994       cp_lexer_consume_token (parser->lexer);
19995     }
19996
19997   return clobbers;
19998 }
19999
20000 /* Parse an asm-label-list.
20001
20002    asm-label-list:
20003      identifier
20004      asm-label-list , identifier
20005
20006    Returns a TREE_LIST, indicating the labels in the order that they
20007    appeared.  The TREE_VALUE of each node is a label.  */
20008
20009 static tree
20010 cp_parser_asm_label_list (cp_parser* parser)
20011 {
20012   tree labels = NULL_TREE;
20013
20014   while (true)
20015     {
20016       tree identifier, label, name;
20017
20018       /* Look for the identifier.  */
20019       identifier = cp_parser_identifier (parser);
20020       if (!error_operand_p (identifier))
20021         {
20022           label = lookup_label (identifier);
20023           if (TREE_CODE (label) == LABEL_DECL)
20024             {
20025               TREE_USED (label) = 1;
20026               check_goto (label);
20027               name = build_string (IDENTIFIER_LENGTH (identifier),
20028                                    IDENTIFIER_POINTER (identifier));
20029               labels = tree_cons (name, label, labels);
20030             }
20031         }
20032       /* If the next token is not a `,', then the list is
20033          complete.  */
20034       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20035         break;
20036       /* Consume the `,' token.  */
20037       cp_lexer_consume_token (parser->lexer);
20038     }
20039
20040   return nreverse (labels);
20041 }
20042
20043 /* Parse an (optional) series of attributes.
20044
20045    attributes:
20046      attributes attribute
20047
20048    attribute:
20049      __attribute__ (( attribute-list [opt] ))
20050
20051    The return value is as for cp_parser_attribute_list.  */
20052
20053 static tree
20054 cp_parser_attributes_opt (cp_parser* parser)
20055 {
20056   tree attributes = NULL_TREE;
20057
20058   while (true)
20059     {
20060       cp_token *token;
20061       tree attribute_list;
20062
20063       /* Peek at the next token.  */
20064       token = cp_lexer_peek_token (parser->lexer);
20065       /* If it's not `__attribute__', then we're done.  */
20066       if (token->keyword != RID_ATTRIBUTE)
20067         break;
20068
20069       /* Consume the `__attribute__' keyword.  */
20070       cp_lexer_consume_token (parser->lexer);
20071       /* Look for the two `(' tokens.  */
20072       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20073       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20074
20075       /* Peek at the next token.  */
20076       token = cp_lexer_peek_token (parser->lexer);
20077       if (token->type != CPP_CLOSE_PAREN)
20078         /* Parse the attribute-list.  */
20079         attribute_list = cp_parser_attribute_list (parser);
20080       else
20081         /* If the next token is a `)', then there is no attribute
20082            list.  */
20083         attribute_list = NULL;
20084
20085       /* Look for the two `)' tokens.  */
20086       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20087       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20088
20089       /* Add these new attributes to the list.  */
20090       attributes = chainon (attributes, attribute_list);
20091     }
20092
20093   return attributes;
20094 }
20095
20096 /* Parse an attribute-list.
20097
20098    attribute-list:
20099      attribute
20100      attribute-list , attribute
20101
20102    attribute:
20103      identifier
20104      identifier ( identifier )
20105      identifier ( identifier , expression-list )
20106      identifier ( expression-list )
20107
20108    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20109    to an attribute.  The TREE_PURPOSE of each node is the identifier
20110    indicating which attribute is in use.  The TREE_VALUE represents
20111    the arguments, if any.  */
20112
20113 static tree
20114 cp_parser_attribute_list (cp_parser* parser)
20115 {
20116   tree attribute_list = NULL_TREE;
20117   bool save_translate_strings_p = parser->translate_strings_p;
20118
20119   parser->translate_strings_p = false;
20120   while (true)
20121     {
20122       cp_token *token;
20123       tree identifier;
20124       tree attribute;
20125
20126       /* Look for the identifier.  We also allow keywords here; for
20127          example `__attribute__ ((const))' is legal.  */
20128       token = cp_lexer_peek_token (parser->lexer);
20129       if (token->type == CPP_NAME
20130           || token->type == CPP_KEYWORD)
20131         {
20132           tree arguments = NULL_TREE;
20133
20134           /* Consume the token.  */
20135           token = cp_lexer_consume_token (parser->lexer);
20136
20137           /* Save away the identifier that indicates which attribute
20138              this is.  */
20139           identifier = (token->type == CPP_KEYWORD) 
20140             /* For keywords, use the canonical spelling, not the
20141                parsed identifier.  */
20142             ? ridpointers[(int) token->keyword]
20143             : token->u.value;
20144           
20145           attribute = build_tree_list (identifier, NULL_TREE);
20146
20147           /* Peek at the next token.  */
20148           token = cp_lexer_peek_token (parser->lexer);
20149           /* If it's an `(', then parse the attribute arguments.  */
20150           if (token->type == CPP_OPEN_PAREN)
20151             {
20152               VEC(tree,gc) *vec;
20153               int attr_flag = (attribute_takes_identifier_p (identifier)
20154                                ? id_attr : normal_attr);
20155               vec = cp_parser_parenthesized_expression_list
20156                     (parser, attr_flag, /*cast_p=*/false,
20157                      /*allow_expansion_p=*/false,
20158                      /*non_constant_p=*/NULL);
20159               if (vec == NULL)
20160                 arguments = error_mark_node;
20161               else
20162                 {
20163                   arguments = build_tree_list_vec (vec);
20164                   release_tree_vector (vec);
20165                 }
20166               /* Save the arguments away.  */
20167               TREE_VALUE (attribute) = arguments;
20168             }
20169
20170           if (arguments != error_mark_node)
20171             {
20172               /* Add this attribute to the list.  */
20173               TREE_CHAIN (attribute) = attribute_list;
20174               attribute_list = attribute;
20175             }
20176
20177           token = cp_lexer_peek_token (parser->lexer);
20178         }
20179       /* Now, look for more attributes.  If the next token isn't a
20180          `,', we're done.  */
20181       if (token->type != CPP_COMMA)
20182         break;
20183
20184       /* Consume the comma and keep going.  */
20185       cp_lexer_consume_token (parser->lexer);
20186     }
20187   parser->translate_strings_p = save_translate_strings_p;
20188
20189   /* We built up the list in reverse order.  */
20190   return nreverse (attribute_list);
20191 }
20192
20193 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20194    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20195    current value of the PEDANTIC flag, regardless of whether or not
20196    the `__extension__' keyword is present.  The caller is responsible
20197    for restoring the value of the PEDANTIC flag.  */
20198
20199 static bool
20200 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20201 {
20202   /* Save the old value of the PEDANTIC flag.  */
20203   *saved_pedantic = pedantic;
20204
20205   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20206     {
20207       /* Consume the `__extension__' token.  */
20208       cp_lexer_consume_token (parser->lexer);
20209       /* We're not being pedantic while the `__extension__' keyword is
20210          in effect.  */
20211       pedantic = 0;
20212
20213       return true;
20214     }
20215
20216   return false;
20217 }
20218
20219 /* Parse a label declaration.
20220
20221    label-declaration:
20222      __label__ label-declarator-seq ;
20223
20224    label-declarator-seq:
20225      identifier , label-declarator-seq
20226      identifier  */
20227
20228 static void
20229 cp_parser_label_declaration (cp_parser* parser)
20230 {
20231   /* Look for the `__label__' keyword.  */
20232   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20233
20234   while (true)
20235     {
20236       tree identifier;
20237
20238       /* Look for an identifier.  */
20239       identifier = cp_parser_identifier (parser);
20240       /* If we failed, stop.  */
20241       if (identifier == error_mark_node)
20242         break;
20243       /* Declare it as a label.  */
20244       finish_label_decl (identifier);
20245       /* If the next token is a `;', stop.  */
20246       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20247         break;
20248       /* Look for the `,' separating the label declarations.  */
20249       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20250     }
20251
20252   /* Look for the final `;'.  */
20253   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20254 }
20255
20256 /* Support Functions */
20257
20258 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20259    NAME should have one of the representations used for an
20260    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20261    is returned.  If PARSER->SCOPE is a dependent type, then a
20262    SCOPE_REF is returned.
20263
20264    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20265    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20266    was formed.  Abstractly, such entities should not be passed to this
20267    function, because they do not need to be looked up, but it is
20268    simpler to check for this special case here, rather than at the
20269    call-sites.
20270
20271    In cases not explicitly covered above, this function returns a
20272    DECL, OVERLOAD, or baselink representing the result of the lookup.
20273    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20274    is returned.
20275
20276    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20277    (e.g., "struct") that was used.  In that case bindings that do not
20278    refer to types are ignored.
20279
20280    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20281    ignored.
20282
20283    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20284    are ignored.
20285
20286    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20287    types.
20288
20289    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20290    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20291    NULL_TREE otherwise.  */
20292
20293 static tree
20294 cp_parser_lookup_name (cp_parser *parser, tree name,
20295                        enum tag_types tag_type,
20296                        bool is_template,
20297                        bool is_namespace,
20298                        bool check_dependency,
20299                        tree *ambiguous_decls,
20300                        location_t name_location)
20301 {
20302   int flags = 0;
20303   tree decl;
20304   tree object_type = parser->context->object_type;
20305
20306   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20307     flags |= LOOKUP_COMPLAIN;
20308
20309   /* Assume that the lookup will be unambiguous.  */
20310   if (ambiguous_decls)
20311     *ambiguous_decls = NULL_TREE;
20312
20313   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20314      no longer valid.  Note that if we are parsing tentatively, and
20315      the parse fails, OBJECT_TYPE will be automatically restored.  */
20316   parser->context->object_type = NULL_TREE;
20317
20318   if (name == error_mark_node)
20319     return error_mark_node;
20320
20321   /* A template-id has already been resolved; there is no lookup to
20322      do.  */
20323   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20324     return name;
20325   if (BASELINK_P (name))
20326     {
20327       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20328                   == TEMPLATE_ID_EXPR);
20329       return name;
20330     }
20331
20332   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20333      it should already have been checked to make sure that the name
20334      used matches the type being destroyed.  */
20335   if (TREE_CODE (name) == BIT_NOT_EXPR)
20336     {
20337       tree type;
20338
20339       /* Figure out to which type this destructor applies.  */
20340       if (parser->scope)
20341         type = parser->scope;
20342       else if (object_type)
20343         type = object_type;
20344       else
20345         type = current_class_type;
20346       /* If that's not a class type, there is no destructor.  */
20347       if (!type || !CLASS_TYPE_P (type))
20348         return error_mark_node;
20349       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20350         lazily_declare_fn (sfk_destructor, type);
20351       if (!CLASSTYPE_DESTRUCTORS (type))
20352           return error_mark_node;
20353       /* If it was a class type, return the destructor.  */
20354       return CLASSTYPE_DESTRUCTORS (type);
20355     }
20356
20357   /* By this point, the NAME should be an ordinary identifier.  If
20358      the id-expression was a qualified name, the qualifying scope is
20359      stored in PARSER->SCOPE at this point.  */
20360   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20361
20362   /* Perform the lookup.  */
20363   if (parser->scope)
20364     {
20365       bool dependent_p;
20366
20367       if (parser->scope == error_mark_node)
20368         return error_mark_node;
20369
20370       /* If the SCOPE is dependent, the lookup must be deferred until
20371          the template is instantiated -- unless we are explicitly
20372          looking up names in uninstantiated templates.  Even then, we
20373          cannot look up the name if the scope is not a class type; it
20374          might, for example, be a template type parameter.  */
20375       dependent_p = (TYPE_P (parser->scope)
20376                      && dependent_scope_p (parser->scope));
20377       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20378           && dependent_p)
20379         /* Defer lookup.  */
20380         decl = error_mark_node;
20381       else
20382         {
20383           tree pushed_scope = NULL_TREE;
20384
20385           /* If PARSER->SCOPE is a dependent type, then it must be a
20386              class type, and we must not be checking dependencies;
20387              otherwise, we would have processed this lookup above.  So
20388              that PARSER->SCOPE is not considered a dependent base by
20389              lookup_member, we must enter the scope here.  */
20390           if (dependent_p)
20391             pushed_scope = push_scope (parser->scope);
20392
20393           /* If the PARSER->SCOPE is a template specialization, it
20394              may be instantiated during name lookup.  In that case,
20395              errors may be issued.  Even if we rollback the current
20396              tentative parse, those errors are valid.  */
20397           decl = lookup_qualified_name (parser->scope, name,
20398                                         tag_type != none_type,
20399                                         /*complain=*/true);
20400
20401           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20402              lookup result and the nested-name-specifier nominates a class C:
20403                * if the name specified after the nested-name-specifier, when
20404                looked up in C, is the injected-class-name of C (Clause 9), or
20405                * if the name specified after the nested-name-specifier is the
20406                same as the identifier or the simple-template-id's template-
20407                name in the last component of the nested-name-specifier,
20408              the name is instead considered to name the constructor of
20409              class C. [ Note: for example, the constructor is not an
20410              acceptable lookup result in an elaborated-type-specifier so
20411              the constructor would not be used in place of the
20412              injected-class-name. --end note ] Such a constructor name
20413              shall be used only in the declarator-id of a declaration that
20414              names a constructor or in a using-declaration.  */
20415           if (tag_type == none_type
20416               && DECL_SELF_REFERENCE_P (decl)
20417               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20418             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20419                                           tag_type != none_type,
20420                                           /*complain=*/true);
20421
20422           /* If we have a single function from a using decl, pull it out.  */
20423           if (TREE_CODE (decl) == OVERLOAD
20424               && !really_overloaded_fn (decl))
20425             decl = OVL_FUNCTION (decl);
20426
20427           if (pushed_scope)
20428             pop_scope (pushed_scope);
20429         }
20430
20431       /* If the scope is a dependent type and either we deferred lookup or
20432          we did lookup but didn't find the name, rememeber the name.  */
20433       if (decl == error_mark_node && TYPE_P (parser->scope)
20434           && dependent_type_p (parser->scope))
20435         {
20436           if (tag_type)
20437             {
20438               tree type;
20439
20440               /* The resolution to Core Issue 180 says that `struct
20441                  A::B' should be considered a type-name, even if `A'
20442                  is dependent.  */
20443               type = make_typename_type (parser->scope, name, tag_type,
20444                                          /*complain=*/tf_error);
20445               decl = TYPE_NAME (type);
20446             }
20447           else if (is_template
20448                    && (cp_parser_next_token_ends_template_argument_p (parser)
20449                        || cp_lexer_next_token_is (parser->lexer,
20450                                                   CPP_CLOSE_PAREN)))
20451             decl = make_unbound_class_template (parser->scope,
20452                                                 name, NULL_TREE,
20453                                                 /*complain=*/tf_error);
20454           else
20455             decl = build_qualified_name (/*type=*/NULL_TREE,
20456                                          parser->scope, name,
20457                                          is_template);
20458         }
20459       parser->qualifying_scope = parser->scope;
20460       parser->object_scope = NULL_TREE;
20461     }
20462   else if (object_type)
20463     {
20464       tree object_decl = NULL_TREE;
20465       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20466          OBJECT_TYPE is not a class.  */
20467       if (CLASS_TYPE_P (object_type))
20468         /* If the OBJECT_TYPE is a template specialization, it may
20469            be instantiated during name lookup.  In that case, errors
20470            may be issued.  Even if we rollback the current tentative
20471            parse, those errors are valid.  */
20472         object_decl = lookup_member (object_type,
20473                                      name,
20474                                      /*protect=*/0,
20475                                      tag_type != none_type,
20476                                      tf_warning_or_error);
20477       /* Look it up in the enclosing context, too.  */
20478       decl = lookup_name_real (name, tag_type != none_type,
20479                                /*nonclass=*/0,
20480                                /*block_p=*/true, is_namespace, flags);
20481       parser->object_scope = object_type;
20482       parser->qualifying_scope = NULL_TREE;
20483       if (object_decl)
20484         decl = object_decl;
20485     }
20486   else
20487     {
20488       decl = lookup_name_real (name, tag_type != none_type,
20489                                /*nonclass=*/0,
20490                                /*block_p=*/true, is_namespace, flags);
20491       parser->qualifying_scope = NULL_TREE;
20492       parser->object_scope = NULL_TREE;
20493     }
20494
20495   /* If the lookup failed, let our caller know.  */
20496   if (!decl || decl == error_mark_node)
20497     return error_mark_node;
20498
20499   /* Pull out the template from an injected-class-name (or multiple).  */
20500   if (is_template)
20501     decl = maybe_get_template_decl_from_type_decl (decl);
20502
20503   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20504   if (TREE_CODE (decl) == TREE_LIST)
20505     {
20506       if (ambiguous_decls)
20507         *ambiguous_decls = decl;
20508       /* The error message we have to print is too complicated for
20509          cp_parser_error, so we incorporate its actions directly.  */
20510       if (!cp_parser_simulate_error (parser))
20511         {
20512           error_at (name_location, "reference to %qD is ambiguous",
20513                     name);
20514           print_candidates (decl);
20515         }
20516       return error_mark_node;
20517     }
20518
20519   gcc_assert (DECL_P (decl)
20520               || TREE_CODE (decl) == OVERLOAD
20521               || TREE_CODE (decl) == SCOPE_REF
20522               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20523               || BASELINK_P (decl));
20524
20525   /* If we have resolved the name of a member declaration, check to
20526      see if the declaration is accessible.  When the name resolves to
20527      set of overloaded functions, accessibility is checked when
20528      overload resolution is done.
20529
20530      During an explicit instantiation, access is not checked at all,
20531      as per [temp.explicit].  */
20532   if (DECL_P (decl))
20533     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20534
20535   maybe_record_typedef_use (decl);
20536
20537   return decl;
20538 }
20539
20540 /* Like cp_parser_lookup_name, but for use in the typical case where
20541    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20542    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20543
20544 static tree
20545 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20546 {
20547   return cp_parser_lookup_name (parser, name,
20548                                 none_type,
20549                                 /*is_template=*/false,
20550                                 /*is_namespace=*/false,
20551                                 /*check_dependency=*/true,
20552                                 /*ambiguous_decls=*/NULL,
20553                                 location);
20554 }
20555
20556 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20557    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20558    true, the DECL indicates the class being defined in a class-head,
20559    or declared in an elaborated-type-specifier.
20560
20561    Otherwise, return DECL.  */
20562
20563 static tree
20564 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20565 {
20566   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20567      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20568
20569        struct A {
20570          template <typename T> struct B;
20571        };
20572
20573        template <typename T> struct A::B {};
20574
20575      Similarly, in an elaborated-type-specifier:
20576
20577        namespace N { struct X{}; }
20578
20579        struct A {
20580          template <typename T> friend struct N::X;
20581        };
20582
20583      However, if the DECL refers to a class type, and we are in
20584      the scope of the class, then the name lookup automatically
20585      finds the TYPE_DECL created by build_self_reference rather
20586      than a TEMPLATE_DECL.  For example, in:
20587
20588        template <class T> struct S {
20589          S s;
20590        };
20591
20592      there is no need to handle such case.  */
20593
20594   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20595     return DECL_TEMPLATE_RESULT (decl);
20596
20597   return decl;
20598 }
20599
20600 /* If too many, or too few, template-parameter lists apply to the
20601    declarator, issue an error message.  Returns TRUE if all went well,
20602    and FALSE otherwise.  */
20603
20604 static bool
20605 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20606                                                 cp_declarator *declarator,
20607                                                 location_t declarator_location)
20608 {
20609   unsigned num_templates;
20610
20611   /* We haven't seen any classes that involve template parameters yet.  */
20612   num_templates = 0;
20613
20614   switch (declarator->kind)
20615     {
20616     case cdk_id:
20617       if (declarator->u.id.qualifying_scope)
20618         {
20619           tree scope;
20620
20621           scope = declarator->u.id.qualifying_scope;
20622
20623           while (scope && CLASS_TYPE_P (scope))
20624             {
20625               /* You're supposed to have one `template <...>'
20626                  for every template class, but you don't need one
20627                  for a full specialization.  For example:
20628
20629                  template <class T> struct S{};
20630                  template <> struct S<int> { void f(); };
20631                  void S<int>::f () {}
20632
20633                  is correct; there shouldn't be a `template <>' for
20634                  the definition of `S<int>::f'.  */
20635               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20636                 /* If SCOPE does not have template information of any
20637                    kind, then it is not a template, nor is it nested
20638                    within a template.  */
20639                 break;
20640               if (explicit_class_specialization_p (scope))
20641                 break;
20642               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20643                 ++num_templates;
20644
20645               scope = TYPE_CONTEXT (scope);
20646             }
20647         }
20648       else if (TREE_CODE (declarator->u.id.unqualified_name)
20649                == TEMPLATE_ID_EXPR)
20650         /* If the DECLARATOR has the form `X<y>' then it uses one
20651            additional level of template parameters.  */
20652         ++num_templates;
20653
20654       return cp_parser_check_template_parameters 
20655         (parser, num_templates, declarator_location, declarator);
20656
20657
20658     case cdk_function:
20659     case cdk_array:
20660     case cdk_pointer:
20661     case cdk_reference:
20662     case cdk_ptrmem:
20663       return (cp_parser_check_declarator_template_parameters
20664               (parser, declarator->declarator, declarator_location));
20665
20666     case cdk_error:
20667       return true;
20668
20669     default:
20670       gcc_unreachable ();
20671     }
20672   return false;
20673 }
20674
20675 /* NUM_TEMPLATES were used in the current declaration.  If that is
20676    invalid, return FALSE and issue an error messages.  Otherwise,
20677    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20678    declarator and we can print more accurate diagnostics.  */
20679
20680 static bool
20681 cp_parser_check_template_parameters (cp_parser* parser,
20682                                      unsigned num_templates,
20683                                      location_t location,
20684                                      cp_declarator *declarator)
20685 {
20686   /* If there are the same number of template classes and parameter
20687      lists, that's OK.  */
20688   if (parser->num_template_parameter_lists == num_templates)
20689     return true;
20690   /* If there are more, but only one more, then we are referring to a
20691      member template.  That's OK too.  */
20692   if (parser->num_template_parameter_lists == num_templates + 1)
20693     return true;
20694   /* If there are more template classes than parameter lists, we have
20695      something like:
20696
20697        template <class T> void S<T>::R<T>::f ();  */
20698   if (parser->num_template_parameter_lists < num_templates)
20699     {
20700       if (declarator && !current_function_decl)
20701         error_at (location, "specializing member %<%T::%E%> "
20702                   "requires %<template<>%> syntax", 
20703                   declarator->u.id.qualifying_scope,
20704                   declarator->u.id.unqualified_name);
20705       else if (declarator)
20706         error_at (location, "invalid declaration of %<%T::%E%>",
20707                   declarator->u.id.qualifying_scope,
20708                   declarator->u.id.unqualified_name);
20709       else 
20710         error_at (location, "too few template-parameter-lists");
20711       return false;
20712     }
20713   /* Otherwise, there are too many template parameter lists.  We have
20714      something like:
20715
20716      template <class T> template <class U> void S::f();  */
20717   error_at (location, "too many template-parameter-lists");
20718   return false;
20719 }
20720
20721 /* Parse an optional `::' token indicating that the following name is
20722    from the global namespace.  If so, PARSER->SCOPE is set to the
20723    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20724    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20725    Returns the new value of PARSER->SCOPE, if the `::' token is
20726    present, and NULL_TREE otherwise.  */
20727
20728 static tree
20729 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20730 {
20731   cp_token *token;
20732
20733   /* Peek at the next token.  */
20734   token = cp_lexer_peek_token (parser->lexer);
20735   /* If we're looking at a `::' token then we're starting from the
20736      global namespace, not our current location.  */
20737   if (token->type == CPP_SCOPE)
20738     {
20739       /* Consume the `::' token.  */
20740       cp_lexer_consume_token (parser->lexer);
20741       /* Set the SCOPE so that we know where to start the lookup.  */
20742       parser->scope = global_namespace;
20743       parser->qualifying_scope = global_namespace;
20744       parser->object_scope = NULL_TREE;
20745
20746       return parser->scope;
20747     }
20748   else if (!current_scope_valid_p)
20749     {
20750       parser->scope = NULL_TREE;
20751       parser->qualifying_scope = NULL_TREE;
20752       parser->object_scope = NULL_TREE;
20753     }
20754
20755   return NULL_TREE;
20756 }
20757
20758 /* Returns TRUE if the upcoming token sequence is the start of a
20759    constructor declarator.  If FRIEND_P is true, the declarator is
20760    preceded by the `friend' specifier.  */
20761
20762 static bool
20763 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20764 {
20765   bool constructor_p;
20766   tree nested_name_specifier;
20767   cp_token *next_token;
20768
20769   /* The common case is that this is not a constructor declarator, so
20770      try to avoid doing lots of work if at all possible.  It's not
20771      valid declare a constructor at function scope.  */
20772   if (parser->in_function_body)
20773     return false;
20774   /* And only certain tokens can begin a constructor declarator.  */
20775   next_token = cp_lexer_peek_token (parser->lexer);
20776   if (next_token->type != CPP_NAME
20777       && next_token->type != CPP_SCOPE
20778       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20779       && next_token->type != CPP_TEMPLATE_ID)
20780     return false;
20781
20782   /* Parse tentatively; we are going to roll back all of the tokens
20783      consumed here.  */
20784   cp_parser_parse_tentatively (parser);
20785   /* Assume that we are looking at a constructor declarator.  */
20786   constructor_p = true;
20787
20788   /* Look for the optional `::' operator.  */
20789   cp_parser_global_scope_opt (parser,
20790                               /*current_scope_valid_p=*/false);
20791   /* Look for the nested-name-specifier.  */
20792   nested_name_specifier
20793     = (cp_parser_nested_name_specifier_opt (parser,
20794                                             /*typename_keyword_p=*/false,
20795                                             /*check_dependency_p=*/false,
20796                                             /*type_p=*/false,
20797                                             /*is_declaration=*/false));
20798   /* Outside of a class-specifier, there must be a
20799      nested-name-specifier.  */
20800   if (!nested_name_specifier &&
20801       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20802        || friend_p))
20803     constructor_p = false;
20804   else if (nested_name_specifier == error_mark_node)
20805     constructor_p = false;
20806
20807   /* If we have a class scope, this is easy; DR 147 says that S::S always
20808      names the constructor, and no other qualified name could.  */
20809   if (constructor_p && nested_name_specifier
20810       && CLASS_TYPE_P (nested_name_specifier))
20811     {
20812       tree id = cp_parser_unqualified_id (parser,
20813                                           /*template_keyword_p=*/false,
20814                                           /*check_dependency_p=*/false,
20815                                           /*declarator_p=*/true,
20816                                           /*optional_p=*/false);
20817       if (is_overloaded_fn (id))
20818         id = DECL_NAME (get_first_fn (id));
20819       if (!constructor_name_p (id, nested_name_specifier))
20820         constructor_p = false;
20821     }
20822   /* If we still think that this might be a constructor-declarator,
20823      look for a class-name.  */
20824   else if (constructor_p)
20825     {
20826       /* If we have:
20827
20828            template <typename T> struct S {
20829              S();
20830            };
20831
20832          we must recognize that the nested `S' names a class.  */
20833       tree type_decl;
20834       type_decl = cp_parser_class_name (parser,
20835                                         /*typename_keyword_p=*/false,
20836                                         /*template_keyword_p=*/false,
20837                                         none_type,
20838                                         /*check_dependency_p=*/false,
20839                                         /*class_head_p=*/false,
20840                                         /*is_declaration=*/false);
20841       /* If there was no class-name, then this is not a constructor.  */
20842       constructor_p = !cp_parser_error_occurred (parser);
20843
20844       /* If we're still considering a constructor, we have to see a `(',
20845          to begin the parameter-declaration-clause, followed by either a
20846          `)', an `...', or a decl-specifier.  We need to check for a
20847          type-specifier to avoid being fooled into thinking that:
20848
20849            S (f) (int);
20850
20851          is a constructor.  (It is actually a function named `f' that
20852          takes one parameter (of type `int') and returns a value of type
20853          `S'.  */
20854       if (constructor_p
20855           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20856         constructor_p = false;
20857
20858       if (constructor_p
20859           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20860           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20861           /* A parameter declaration begins with a decl-specifier,
20862              which is either the "attribute" keyword, a storage class
20863              specifier, or (usually) a type-specifier.  */
20864           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20865         {
20866           tree type;
20867           tree pushed_scope = NULL_TREE;
20868           unsigned saved_num_template_parameter_lists;
20869
20870           /* Names appearing in the type-specifier should be looked up
20871              in the scope of the class.  */
20872           if (current_class_type)
20873             type = NULL_TREE;
20874           else
20875             {
20876               type = TREE_TYPE (type_decl);
20877               if (TREE_CODE (type) == TYPENAME_TYPE)
20878                 {
20879                   type = resolve_typename_type (type,
20880                                                 /*only_current_p=*/false);
20881                   if (TREE_CODE (type) == TYPENAME_TYPE)
20882                     {
20883                       cp_parser_abort_tentative_parse (parser);
20884                       return false;
20885                     }
20886                 }
20887               pushed_scope = push_scope (type);
20888             }
20889
20890           /* Inside the constructor parameter list, surrounding
20891              template-parameter-lists do not apply.  */
20892           saved_num_template_parameter_lists
20893             = parser->num_template_parameter_lists;
20894           parser->num_template_parameter_lists = 0;
20895
20896           /* Look for the type-specifier.  */
20897           cp_parser_type_specifier (parser,
20898                                     CP_PARSER_FLAGS_NONE,
20899                                     /*decl_specs=*/NULL,
20900                                     /*is_declarator=*/true,
20901                                     /*declares_class_or_enum=*/NULL,
20902                                     /*is_cv_qualifier=*/NULL);
20903
20904           parser->num_template_parameter_lists
20905             = saved_num_template_parameter_lists;
20906
20907           /* Leave the scope of the class.  */
20908           if (pushed_scope)
20909             pop_scope (pushed_scope);
20910
20911           constructor_p = !cp_parser_error_occurred (parser);
20912         }
20913     }
20914
20915   /* We did not really want to consume any tokens.  */
20916   cp_parser_abort_tentative_parse (parser);
20917
20918   return constructor_p;
20919 }
20920
20921 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20922    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20923    they must be performed once we are in the scope of the function.
20924
20925    Returns the function defined.  */
20926
20927 static tree
20928 cp_parser_function_definition_from_specifiers_and_declarator
20929   (cp_parser* parser,
20930    cp_decl_specifier_seq *decl_specifiers,
20931    tree attributes,
20932    const cp_declarator *declarator)
20933 {
20934   tree fn;
20935   bool success_p;
20936
20937   /* Begin the function-definition.  */
20938   success_p = start_function (decl_specifiers, declarator, attributes);
20939
20940   /* The things we're about to see are not directly qualified by any
20941      template headers we've seen thus far.  */
20942   reset_specialization ();
20943
20944   /* If there were names looked up in the decl-specifier-seq that we
20945      did not check, check them now.  We must wait until we are in the
20946      scope of the function to perform the checks, since the function
20947      might be a friend.  */
20948   perform_deferred_access_checks ();
20949
20950   if (!success_p)
20951     {
20952       /* Skip the entire function.  */
20953       cp_parser_skip_to_end_of_block_or_statement (parser);
20954       fn = error_mark_node;
20955     }
20956   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20957     {
20958       /* Seen already, skip it.  An error message has already been output.  */
20959       cp_parser_skip_to_end_of_block_or_statement (parser);
20960       fn = current_function_decl;
20961       current_function_decl = NULL_TREE;
20962       /* If this is a function from a class, pop the nested class.  */
20963       if (current_class_name)
20964         pop_nested_class ();
20965     }
20966   else
20967     {
20968       timevar_id_t tv;
20969       if (DECL_DECLARED_INLINE_P (current_function_decl))
20970         tv = TV_PARSE_INLINE;
20971       else
20972         tv = TV_PARSE_FUNC;
20973       timevar_push (tv);
20974       fn = cp_parser_function_definition_after_declarator (parser,
20975                                                          /*inline_p=*/false);
20976       timevar_pop (tv);
20977     }
20978
20979   return fn;
20980 }
20981
20982 /* Parse the part of a function-definition that follows the
20983    declarator.  INLINE_P is TRUE iff this function is an inline
20984    function defined within a class-specifier.
20985
20986    Returns the function defined.  */
20987
20988 static tree
20989 cp_parser_function_definition_after_declarator (cp_parser* parser,
20990                                                 bool inline_p)
20991 {
20992   tree fn;
20993   bool ctor_initializer_p = false;
20994   bool saved_in_unbraced_linkage_specification_p;
20995   bool saved_in_function_body;
20996   unsigned saved_num_template_parameter_lists;
20997   cp_token *token;
20998
20999   saved_in_function_body = parser->in_function_body;
21000   parser->in_function_body = true;
21001   /* If the next token is `return', then the code may be trying to
21002      make use of the "named return value" extension that G++ used to
21003      support.  */
21004   token = cp_lexer_peek_token (parser->lexer);
21005   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21006     {
21007       /* Consume the `return' keyword.  */
21008       cp_lexer_consume_token (parser->lexer);
21009       /* Look for the identifier that indicates what value is to be
21010          returned.  */
21011       cp_parser_identifier (parser);
21012       /* Issue an error message.  */
21013       error_at (token->location,
21014                 "named return values are no longer supported");
21015       /* Skip tokens until we reach the start of the function body.  */
21016       while (true)
21017         {
21018           cp_token *token = cp_lexer_peek_token (parser->lexer);
21019           if (token->type == CPP_OPEN_BRACE
21020               || token->type == CPP_EOF
21021               || token->type == CPP_PRAGMA_EOL)
21022             break;
21023           cp_lexer_consume_token (parser->lexer);
21024         }
21025     }
21026   /* The `extern' in `extern "C" void f () { ... }' does not apply to
21027      anything declared inside `f'.  */
21028   saved_in_unbraced_linkage_specification_p
21029     = parser->in_unbraced_linkage_specification_p;
21030   parser->in_unbraced_linkage_specification_p = false;
21031   /* Inside the function, surrounding template-parameter-lists do not
21032      apply.  */
21033   saved_num_template_parameter_lists
21034     = parser->num_template_parameter_lists;
21035   parser->num_template_parameter_lists = 0;
21036
21037   start_lambda_scope (current_function_decl);
21038
21039   /* If the next token is `try', `__transaction_atomic', or
21040      `__transaction_relaxed`, then we are looking at either function-try-block
21041      or function-transaction-block.  Note that all of these include the
21042      function-body.  */
21043   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21044     ctor_initializer_p = cp_parser_function_transaction (parser,
21045         RID_TRANSACTION_ATOMIC);
21046   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21047       RID_TRANSACTION_RELAXED))
21048     ctor_initializer_p = cp_parser_function_transaction (parser,
21049         RID_TRANSACTION_RELAXED);
21050   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21051     ctor_initializer_p = cp_parser_function_try_block (parser);
21052   else
21053     ctor_initializer_p
21054       = cp_parser_ctor_initializer_opt_and_function_body (parser);
21055
21056   finish_lambda_scope ();
21057
21058   /* Finish the function.  */
21059   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21060                         (inline_p ? 2 : 0));
21061   /* Generate code for it, if necessary.  */
21062   expand_or_defer_fn (fn);
21063   /* Restore the saved values.  */
21064   parser->in_unbraced_linkage_specification_p
21065     = saved_in_unbraced_linkage_specification_p;
21066   parser->num_template_parameter_lists
21067     = saved_num_template_parameter_lists;
21068   parser->in_function_body = saved_in_function_body;
21069
21070   return fn;
21071 }
21072
21073 /* Parse a template-declaration, assuming that the `export' (and
21074    `extern') keywords, if present, has already been scanned.  MEMBER_P
21075    is as for cp_parser_template_declaration.  */
21076
21077 static void
21078 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21079 {
21080   tree decl = NULL_TREE;
21081   VEC (deferred_access_check,gc) *checks;
21082   tree parameter_list;
21083   bool friend_p = false;
21084   bool need_lang_pop;
21085   cp_token *token;
21086
21087   /* Look for the `template' keyword.  */
21088   token = cp_lexer_peek_token (parser->lexer);
21089   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21090     return;
21091
21092   /* And the `<'.  */
21093   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21094     return;
21095   if (at_class_scope_p () && current_function_decl)
21096     {
21097       /* 14.5.2.2 [temp.mem]
21098
21099          A local class shall not have member templates.  */
21100       error_at (token->location,
21101                 "invalid declaration of member template in local class");
21102       cp_parser_skip_to_end_of_block_or_statement (parser);
21103       return;
21104     }
21105   /* [temp]
21106
21107      A template ... shall not have C linkage.  */
21108   if (current_lang_name == lang_name_c)
21109     {
21110       error_at (token->location, "template with C linkage");
21111       /* Give it C++ linkage to avoid confusing other parts of the
21112          front end.  */
21113       push_lang_context (lang_name_cplusplus);
21114       need_lang_pop = true;
21115     }
21116   else
21117     need_lang_pop = false;
21118
21119   /* We cannot perform access checks on the template parameter
21120      declarations until we know what is being declared, just as we
21121      cannot check the decl-specifier list.  */
21122   push_deferring_access_checks (dk_deferred);
21123
21124   /* If the next token is `>', then we have an invalid
21125      specialization.  Rather than complain about an invalid template
21126      parameter, issue an error message here.  */
21127   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21128     {
21129       cp_parser_error (parser, "invalid explicit specialization");
21130       begin_specialization ();
21131       parameter_list = NULL_TREE;
21132     }
21133   else
21134     {
21135       /* Parse the template parameters.  */
21136       parameter_list = cp_parser_template_parameter_list (parser);
21137       fixup_template_parms ();
21138     }
21139
21140   /* Get the deferred access checks from the parameter list.  These
21141      will be checked once we know what is being declared, as for a
21142      member template the checks must be performed in the scope of the
21143      class containing the member.  */
21144   checks = get_deferred_access_checks ();
21145
21146   /* Look for the `>'.  */
21147   cp_parser_skip_to_end_of_template_parameter_list (parser);
21148   /* We just processed one more parameter list.  */
21149   ++parser->num_template_parameter_lists;
21150   /* If the next token is `template', there are more template
21151      parameters.  */
21152   if (cp_lexer_next_token_is_keyword (parser->lexer,
21153                                       RID_TEMPLATE))
21154     cp_parser_template_declaration_after_export (parser, member_p);
21155   else if (cxx_dialect >= cxx0x
21156            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21157     decl = cp_parser_alias_declaration (parser);
21158   else
21159     {
21160       /* There are no access checks when parsing a template, as we do not
21161          know if a specialization will be a friend.  */
21162       push_deferring_access_checks (dk_no_check);
21163       token = cp_lexer_peek_token (parser->lexer);
21164       decl = cp_parser_single_declaration (parser,
21165                                            checks,
21166                                            member_p,
21167                                            /*explicit_specialization_p=*/false,
21168                                            &friend_p);
21169       pop_deferring_access_checks ();
21170
21171       /* If this is a member template declaration, let the front
21172          end know.  */
21173       if (member_p && !friend_p && decl)
21174         {
21175           if (TREE_CODE (decl) == TYPE_DECL)
21176             cp_parser_check_access_in_redeclaration (decl, token->location);
21177
21178           decl = finish_member_template_decl (decl);
21179         }
21180       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21181         make_friend_class (current_class_type, TREE_TYPE (decl),
21182                            /*complain=*/true);
21183     }
21184   /* We are done with the current parameter list.  */
21185   --parser->num_template_parameter_lists;
21186
21187   pop_deferring_access_checks ();
21188
21189   /* Finish up.  */
21190   finish_template_decl (parameter_list);
21191
21192   /* Check the template arguments for a literal operator template.  */
21193   if (decl
21194       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21195       && UDLIT_OPER_P (DECL_NAME (decl)))
21196     {
21197       bool ok = true;
21198       if (parameter_list == NULL_TREE)
21199         ok = false;
21200       else
21201         {
21202           int num_parms = TREE_VEC_LENGTH (parameter_list);
21203           if (num_parms != 1)
21204             ok = false;
21205           else
21206             {
21207               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21208               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21209               if (TREE_TYPE (parm) != char_type_node
21210                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21211                 ok = false;
21212             }
21213         }
21214       if (!ok)
21215         error ("literal operator template %qD has invalid parameter list."
21216                "  Expected non-type template argument pack <char...>",
21217                decl);
21218     }
21219   /* Register member declarations.  */
21220   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21221     finish_member_declaration (decl);
21222   /* For the erroneous case of a template with C linkage, we pushed an
21223      implicit C++ linkage scope; exit that scope now.  */
21224   if (need_lang_pop)
21225     pop_lang_context ();
21226   /* If DECL is a function template, we must return to parse it later.
21227      (Even though there is no definition, there might be default
21228      arguments that need handling.)  */
21229   if (member_p && decl
21230       && (TREE_CODE (decl) == FUNCTION_DECL
21231           || DECL_FUNCTION_TEMPLATE_P (decl)))
21232     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21233 }
21234
21235 /* Perform the deferred access checks from a template-parameter-list.
21236    CHECKS is a TREE_LIST of access checks, as returned by
21237    get_deferred_access_checks.  */
21238
21239 static void
21240 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21241 {
21242   ++processing_template_parmlist;
21243   perform_access_checks (checks);
21244   --processing_template_parmlist;
21245 }
21246
21247 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21248    `function-definition' sequence.  MEMBER_P is true, this declaration
21249    appears in a class scope.
21250
21251    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21252    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21253
21254 static tree
21255 cp_parser_single_declaration (cp_parser* parser,
21256                               VEC (deferred_access_check,gc)* checks,
21257                               bool member_p,
21258                               bool explicit_specialization_p,
21259                               bool* friend_p)
21260 {
21261   int declares_class_or_enum;
21262   tree decl = NULL_TREE;
21263   cp_decl_specifier_seq decl_specifiers;
21264   bool function_definition_p = false;
21265   cp_token *decl_spec_token_start;
21266
21267   /* This function is only used when processing a template
21268      declaration.  */
21269   gcc_assert (innermost_scope_kind () == sk_template_parms
21270               || innermost_scope_kind () == sk_template_spec);
21271
21272   /* Defer access checks until we know what is being declared.  */
21273   push_deferring_access_checks (dk_deferred);
21274
21275   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21276      alternative.  */
21277   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21278   cp_parser_decl_specifier_seq (parser,
21279                                 CP_PARSER_FLAGS_OPTIONAL,
21280                                 &decl_specifiers,
21281                                 &declares_class_or_enum);
21282   if (friend_p)
21283     *friend_p = cp_parser_friend_p (&decl_specifiers);
21284
21285   /* There are no template typedefs.  */
21286   if (decl_specifiers.specs[(int) ds_typedef])
21287     {
21288       error_at (decl_spec_token_start->location,
21289                 "template declaration of %<typedef%>");
21290       decl = error_mark_node;
21291     }
21292
21293   /* Gather up the access checks that occurred the
21294      decl-specifier-seq.  */
21295   stop_deferring_access_checks ();
21296
21297   /* Check for the declaration of a template class.  */
21298   if (declares_class_or_enum)
21299     {
21300       if (cp_parser_declares_only_class_p (parser))
21301         {
21302           decl = shadow_tag (&decl_specifiers);
21303
21304           /* In this case:
21305
21306                struct C {
21307                  friend template <typename T> struct A<T>::B;
21308                };
21309
21310              A<T>::B will be represented by a TYPENAME_TYPE, and
21311              therefore not recognized by shadow_tag.  */
21312           if (friend_p && *friend_p
21313               && !decl
21314               && decl_specifiers.type
21315               && TYPE_P (decl_specifiers.type))
21316             decl = decl_specifiers.type;
21317
21318           if (decl && decl != error_mark_node)
21319             decl = TYPE_NAME (decl);
21320           else
21321             decl = error_mark_node;
21322
21323           /* Perform access checks for template parameters.  */
21324           cp_parser_perform_template_parameter_access_checks (checks);
21325         }
21326     }
21327
21328   /* Complain about missing 'typename' or other invalid type names.  */
21329   if (!decl_specifiers.any_type_specifiers_p
21330       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21331     {
21332       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21333          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21334          the rest of this declaration.  */
21335       decl = error_mark_node;
21336       goto out;
21337     }
21338
21339   /* If it's not a template class, try for a template function.  If
21340      the next token is a `;', then this declaration does not declare
21341      anything.  But, if there were errors in the decl-specifiers, then
21342      the error might well have come from an attempted class-specifier.
21343      In that case, there's no need to warn about a missing declarator.  */
21344   if (!decl
21345       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21346           || decl_specifiers.type != error_mark_node))
21347     {
21348       decl = cp_parser_init_declarator (parser,
21349                                         &decl_specifiers,
21350                                         checks,
21351                                         /*function_definition_allowed_p=*/true,
21352                                         member_p,
21353                                         declares_class_or_enum,
21354                                         &function_definition_p,
21355                                         NULL);
21356
21357     /* 7.1.1-1 [dcl.stc]
21358
21359        A storage-class-specifier shall not be specified in an explicit
21360        specialization...  */
21361     if (decl
21362         && explicit_specialization_p
21363         && decl_specifiers.storage_class != sc_none)
21364       {
21365         error_at (decl_spec_token_start->location,
21366                   "explicit template specialization cannot have a storage class");
21367         decl = error_mark_node;
21368       }
21369     }
21370
21371   /* Look for a trailing `;' after the declaration.  */
21372   if (!function_definition_p
21373       && (decl == error_mark_node
21374           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21375     cp_parser_skip_to_end_of_block_or_statement (parser);
21376
21377  out:
21378   pop_deferring_access_checks ();
21379
21380   /* Clear any current qualification; whatever comes next is the start
21381      of something new.  */
21382   parser->scope = NULL_TREE;
21383   parser->qualifying_scope = NULL_TREE;
21384   parser->object_scope = NULL_TREE;
21385
21386   return decl;
21387 }
21388
21389 /* Parse a cast-expression that is not the operand of a unary "&".  */
21390
21391 static tree
21392 cp_parser_simple_cast_expression (cp_parser *parser)
21393 {
21394   return cp_parser_cast_expression (parser, /*address_p=*/false,
21395                                     /*cast_p=*/false, NULL);
21396 }
21397
21398 /* Parse a functional cast to TYPE.  Returns an expression
21399    representing the cast.  */
21400
21401 static tree
21402 cp_parser_functional_cast (cp_parser* parser, tree type)
21403 {
21404   VEC(tree,gc) *vec;
21405   tree expression_list;
21406   tree cast;
21407   bool nonconst_p;
21408
21409   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21410     {
21411       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21412       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21413       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21414       if (TREE_CODE (type) == TYPE_DECL)
21415         type = TREE_TYPE (type);
21416       return finish_compound_literal (type, expression_list,
21417                                       tf_warning_or_error);
21418     }
21419
21420
21421   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21422                                                  /*cast_p=*/true,
21423                                                  /*allow_expansion_p=*/true,
21424                                                  /*non_constant_p=*/NULL);
21425   if (vec == NULL)
21426     expression_list = error_mark_node;
21427   else
21428     {
21429       expression_list = build_tree_list_vec (vec);
21430       release_tree_vector (vec);
21431     }
21432
21433   cast = build_functional_cast (type, expression_list,
21434                                 tf_warning_or_error);
21435   /* [expr.const]/1: In an integral constant expression "only type
21436      conversions to integral or enumeration type can be used".  */
21437   if (TREE_CODE (type) == TYPE_DECL)
21438     type = TREE_TYPE (type);
21439   if (cast != error_mark_node
21440       && !cast_valid_in_integral_constant_expression_p (type)
21441       && cp_parser_non_integral_constant_expression (parser,
21442                                                      NIC_CONSTRUCTOR))
21443     return error_mark_node;
21444   return cast;
21445 }
21446
21447 /* Save the tokens that make up the body of a member function defined
21448    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21449    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21450    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21451    for the member function.  */
21452
21453 static tree
21454 cp_parser_save_member_function_body (cp_parser* parser,
21455                                      cp_decl_specifier_seq *decl_specifiers,
21456                                      cp_declarator *declarator,
21457                                      tree attributes)
21458 {
21459   cp_token *first;
21460   cp_token *last;
21461   tree fn;
21462
21463   /* Create the FUNCTION_DECL.  */
21464   fn = grokmethod (decl_specifiers, declarator, attributes);
21465   /* If something went badly wrong, bail out now.  */
21466   if (fn == error_mark_node)
21467     {
21468       /* If there's a function-body, skip it.  */
21469       if (cp_parser_token_starts_function_definition_p
21470           (cp_lexer_peek_token (parser->lexer)))
21471         cp_parser_skip_to_end_of_block_or_statement (parser);
21472       return error_mark_node;
21473     }
21474
21475   /* Remember it, if there default args to post process.  */
21476   cp_parser_save_default_args (parser, fn);
21477
21478   /* Save away the tokens that make up the body of the
21479      function.  */
21480   first = parser->lexer->next_token;
21481   /* We can have braced-init-list mem-initializers before the fn body.  */
21482   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21483     {
21484       cp_lexer_consume_token (parser->lexer);
21485       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21486              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21487         {
21488           /* cache_group will stop after an un-nested { } pair, too.  */
21489           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21490             break;
21491
21492           /* variadic mem-inits have ... after the ')'.  */
21493           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21494             cp_lexer_consume_token (parser->lexer);
21495         }
21496     }
21497   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21498   /* Handle function try blocks.  */
21499   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21500     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21501   last = parser->lexer->next_token;
21502
21503   /* Save away the inline definition; we will process it when the
21504      class is complete.  */
21505   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21506   DECL_PENDING_INLINE_P (fn) = 1;
21507
21508   /* We need to know that this was defined in the class, so that
21509      friend templates are handled correctly.  */
21510   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21511
21512   /* Add FN to the queue of functions to be parsed later.  */
21513   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21514
21515   return fn;
21516 }
21517
21518 /* Save the tokens that make up the in-class initializer for a non-static
21519    data member.  Returns a DEFAULT_ARG.  */
21520
21521 static tree
21522 cp_parser_save_nsdmi (cp_parser* parser)
21523 {
21524   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21525 }
21526
21527 /* Parse a template-argument-list, as well as the trailing ">" (but
21528    not the opening "<").  See cp_parser_template_argument_list for the
21529    return value.  */
21530
21531 static tree
21532 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21533 {
21534   tree arguments;
21535   tree saved_scope;
21536   tree saved_qualifying_scope;
21537   tree saved_object_scope;
21538   bool saved_greater_than_is_operator_p;
21539   int saved_unevaluated_operand;
21540   int saved_inhibit_evaluation_warnings;
21541
21542   /* [temp.names]
21543
21544      When parsing a template-id, the first non-nested `>' is taken as
21545      the end of the template-argument-list rather than a greater-than
21546      operator.  */
21547   saved_greater_than_is_operator_p
21548     = parser->greater_than_is_operator_p;
21549   parser->greater_than_is_operator_p = false;
21550   /* Parsing the argument list may modify SCOPE, so we save it
21551      here.  */
21552   saved_scope = parser->scope;
21553   saved_qualifying_scope = parser->qualifying_scope;
21554   saved_object_scope = parser->object_scope;
21555   /* We need to evaluate the template arguments, even though this
21556      template-id may be nested within a "sizeof".  */
21557   saved_unevaluated_operand = cp_unevaluated_operand;
21558   cp_unevaluated_operand = 0;
21559   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21560   c_inhibit_evaluation_warnings = 0;
21561   /* Parse the template-argument-list itself.  */
21562   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21563       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21564     arguments = NULL_TREE;
21565   else
21566     arguments = cp_parser_template_argument_list (parser);
21567   /* Look for the `>' that ends the template-argument-list. If we find
21568      a '>>' instead, it's probably just a typo.  */
21569   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21570     {
21571       if (cxx_dialect != cxx98)
21572         {
21573           /* In C++0x, a `>>' in a template argument list or cast
21574              expression is considered to be two separate `>'
21575              tokens. So, change the current token to a `>', but don't
21576              consume it: it will be consumed later when the outer
21577              template argument list (or cast expression) is parsed.
21578              Note that this replacement of `>' for `>>' is necessary
21579              even if we are parsing tentatively: in the tentative
21580              case, after calling
21581              cp_parser_enclosed_template_argument_list we will always
21582              throw away all of the template arguments and the first
21583              closing `>', either because the template argument list
21584              was erroneous or because we are replacing those tokens
21585              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21586              not have been thrown away) is needed either to close an
21587              outer template argument list or to complete a new-style
21588              cast.  */
21589           cp_token *token = cp_lexer_peek_token (parser->lexer);
21590           token->type = CPP_GREATER;
21591         }
21592       else if (!saved_greater_than_is_operator_p)
21593         {
21594           /* If we're in a nested template argument list, the '>>' has
21595             to be a typo for '> >'. We emit the error message, but we
21596             continue parsing and we push a '>' as next token, so that
21597             the argument list will be parsed correctly.  Note that the
21598             global source location is still on the token before the
21599             '>>', so we need to say explicitly where we want it.  */
21600           cp_token *token = cp_lexer_peek_token (parser->lexer);
21601           error_at (token->location, "%<>>%> should be %<> >%> "
21602                     "within a nested template argument list");
21603
21604           token->type = CPP_GREATER;
21605         }
21606       else
21607         {
21608           /* If this is not a nested template argument list, the '>>'
21609             is a typo for '>'. Emit an error message and continue.
21610             Same deal about the token location, but here we can get it
21611             right by consuming the '>>' before issuing the diagnostic.  */
21612           cp_token *token = cp_lexer_consume_token (parser->lexer);
21613           error_at (token->location,
21614                     "spurious %<>>%>, use %<>%> to terminate "
21615                     "a template argument list");
21616         }
21617     }
21618   else
21619     cp_parser_skip_to_end_of_template_parameter_list (parser);
21620   /* The `>' token might be a greater-than operator again now.  */
21621   parser->greater_than_is_operator_p
21622     = saved_greater_than_is_operator_p;
21623   /* Restore the SAVED_SCOPE.  */
21624   parser->scope = saved_scope;
21625   parser->qualifying_scope = saved_qualifying_scope;
21626   parser->object_scope = saved_object_scope;
21627   cp_unevaluated_operand = saved_unevaluated_operand;
21628   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21629
21630   return arguments;
21631 }
21632
21633 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21634    arguments, or the body of the function have not yet been parsed,
21635    parse them now.  */
21636
21637 static void
21638 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21639 {
21640   timevar_push (TV_PARSE_INMETH);
21641   /* If this member is a template, get the underlying
21642      FUNCTION_DECL.  */
21643   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21644     member_function = DECL_TEMPLATE_RESULT (member_function);
21645
21646   /* There should not be any class definitions in progress at this
21647      point; the bodies of members are only parsed outside of all class
21648      definitions.  */
21649   gcc_assert (parser->num_classes_being_defined == 0);
21650   /* While we're parsing the member functions we might encounter more
21651      classes.  We want to handle them right away, but we don't want
21652      them getting mixed up with functions that are currently in the
21653      queue.  */
21654   push_unparsed_function_queues (parser);
21655
21656   /* Make sure that any template parameters are in scope.  */
21657   maybe_begin_member_template_processing (member_function);
21658
21659   /* If the body of the function has not yet been parsed, parse it
21660      now.  */
21661   if (DECL_PENDING_INLINE_P (member_function))
21662     {
21663       tree function_scope;
21664       cp_token_cache *tokens;
21665
21666       /* The function is no longer pending; we are processing it.  */
21667       tokens = DECL_PENDING_INLINE_INFO (member_function);
21668       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21669       DECL_PENDING_INLINE_P (member_function) = 0;
21670
21671       /* If this is a local class, enter the scope of the containing
21672          function.  */
21673       function_scope = current_function_decl;
21674       if (function_scope)
21675         push_function_context ();
21676
21677       /* Push the body of the function onto the lexer stack.  */
21678       cp_parser_push_lexer_for_tokens (parser, tokens);
21679
21680       /* Let the front end know that we going to be defining this
21681          function.  */
21682       start_preparsed_function (member_function, NULL_TREE,
21683                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21684
21685       /* Don't do access checking if it is a templated function.  */
21686       if (processing_template_decl)
21687         push_deferring_access_checks (dk_no_check);
21688
21689       /* Now, parse the body of the function.  */
21690       cp_parser_function_definition_after_declarator (parser,
21691                                                       /*inline_p=*/true);
21692
21693       if (processing_template_decl)
21694         pop_deferring_access_checks ();
21695
21696       /* Leave the scope of the containing function.  */
21697       if (function_scope)
21698         pop_function_context ();
21699       cp_parser_pop_lexer (parser);
21700     }
21701
21702   /* Remove any template parameters from the symbol table.  */
21703   maybe_end_member_template_processing ();
21704
21705   /* Restore the queue.  */
21706   pop_unparsed_function_queues (parser);
21707   timevar_pop (TV_PARSE_INMETH);
21708 }
21709
21710 /* If DECL contains any default args, remember it on the unparsed
21711    functions queue.  */
21712
21713 static void
21714 cp_parser_save_default_args (cp_parser* parser, tree decl)
21715 {
21716   tree probe;
21717
21718   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21719        probe;
21720        probe = TREE_CHAIN (probe))
21721     if (TREE_PURPOSE (probe))
21722       {
21723         cp_default_arg_entry *entry
21724           = VEC_safe_push (cp_default_arg_entry, gc,
21725                            unparsed_funs_with_default_args, NULL);
21726         entry->class_type = current_class_type;
21727         entry->decl = decl;
21728         break;
21729       }
21730 }
21731
21732 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21733    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21734    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21735    from the parameter-type-list.  */
21736
21737 static tree
21738 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21739                                       tree default_arg, tree parmtype)
21740 {
21741   cp_token_cache *tokens;
21742   tree parsed_arg;
21743   bool dummy;
21744
21745   if (default_arg == error_mark_node)
21746     return error_mark_node;
21747
21748   /* Push the saved tokens for the default argument onto the parser's
21749      lexer stack.  */
21750   tokens = DEFARG_TOKENS (default_arg);
21751   cp_parser_push_lexer_for_tokens (parser, tokens);
21752
21753   start_lambda_scope (decl);
21754
21755   /* Parse the default argument.  */
21756   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21757   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21758     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21759
21760   finish_lambda_scope ();
21761
21762   if (!processing_template_decl)
21763     {
21764       /* In a non-template class, check conversions now.  In a template,
21765          we'll wait and instantiate these as needed.  */
21766       if (TREE_CODE (decl) == PARM_DECL)
21767         parsed_arg = check_default_argument (parmtype, parsed_arg);
21768       else
21769         {
21770           int flags = LOOKUP_IMPLICIT;
21771           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21772               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21773             flags = LOOKUP_NORMAL;
21774           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21775         }
21776     }
21777
21778   /* If the token stream has not been completely used up, then
21779      there was extra junk after the end of the default
21780      argument.  */
21781   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21782     {
21783       if (TREE_CODE (decl) == PARM_DECL)
21784         cp_parser_error (parser, "expected %<,%>");
21785       else
21786         cp_parser_error (parser, "expected %<;%>");
21787     }
21788
21789   /* Revert to the main lexer.  */
21790   cp_parser_pop_lexer (parser);
21791
21792   return parsed_arg;
21793 }
21794
21795 /* FIELD is a non-static data member with an initializer which we saved for
21796    later; parse it now.  */
21797
21798 static void
21799 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21800 {
21801   tree def;
21802
21803   push_unparsed_function_queues (parser);
21804   def = cp_parser_late_parse_one_default_arg (parser, field,
21805                                               DECL_INITIAL (field),
21806                                               NULL_TREE);
21807   pop_unparsed_function_queues (parser);
21808
21809   DECL_INITIAL (field) = def;
21810 }
21811
21812 /* FN is a FUNCTION_DECL which may contains a parameter with an
21813    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21814    assumes that the current scope is the scope in which the default
21815    argument should be processed.  */
21816
21817 static void
21818 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21819 {
21820   bool saved_local_variables_forbidden_p;
21821   tree parm, parmdecl;
21822
21823   /* While we're parsing the default args, we might (due to the
21824      statement expression extension) encounter more classes.  We want
21825      to handle them right away, but we don't want them getting mixed
21826      up with default args that are currently in the queue.  */
21827   push_unparsed_function_queues (parser);
21828
21829   /* Local variable names (and the `this' keyword) may not appear
21830      in a default argument.  */
21831   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21832   parser->local_variables_forbidden_p = true;
21833
21834   push_defarg_context (fn);
21835
21836   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21837          parmdecl = DECL_ARGUMENTS (fn);
21838        parm && parm != void_list_node;
21839        parm = TREE_CHAIN (parm),
21840          parmdecl = DECL_CHAIN (parmdecl))
21841     {
21842       tree default_arg = TREE_PURPOSE (parm);
21843       tree parsed_arg;
21844       VEC(tree,gc) *insts;
21845       tree copy;
21846       unsigned ix;
21847
21848       if (!default_arg)
21849         continue;
21850
21851       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21852         /* This can happen for a friend declaration for a function
21853            already declared with default arguments.  */
21854         continue;
21855
21856       parsed_arg
21857         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21858                                                 default_arg,
21859                                                 TREE_VALUE (parm));
21860       if (parsed_arg == error_mark_node)
21861         {
21862           continue;
21863         }
21864
21865       TREE_PURPOSE (parm) = parsed_arg;
21866
21867       /* Update any instantiations we've already created.  */
21868       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21869            VEC_iterate (tree, insts, ix, copy); ix++)
21870         TREE_PURPOSE (copy) = parsed_arg;
21871     }
21872
21873   pop_defarg_context ();
21874
21875   /* Make sure no default arg is missing.  */
21876   check_default_args (fn);
21877
21878   /* Restore the state of local_variables_forbidden_p.  */
21879   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21880
21881   /* Restore the queue.  */
21882   pop_unparsed_function_queues (parser);
21883 }
21884
21885 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21886    either a TYPE or an expression, depending on the form of the
21887    input.  The KEYWORD indicates which kind of expression we have
21888    encountered.  */
21889
21890 static tree
21891 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21892 {
21893   tree expr = NULL_TREE;
21894   const char *saved_message;
21895   char *tmp;
21896   bool saved_integral_constant_expression_p;
21897   bool saved_non_integral_constant_expression_p;
21898   bool pack_expansion_p = false;
21899
21900   /* Types cannot be defined in a `sizeof' expression.  Save away the
21901      old message.  */
21902   saved_message = parser->type_definition_forbidden_message;
21903   /* And create the new one.  */
21904   tmp = concat ("types may not be defined in %<",
21905                 IDENTIFIER_POINTER (ridpointers[keyword]),
21906                 "%> expressions", NULL);
21907   parser->type_definition_forbidden_message = tmp;
21908
21909   /* The restrictions on constant-expressions do not apply inside
21910      sizeof expressions.  */
21911   saved_integral_constant_expression_p
21912     = parser->integral_constant_expression_p;
21913   saved_non_integral_constant_expression_p
21914     = parser->non_integral_constant_expression_p;
21915   parser->integral_constant_expression_p = false;
21916
21917   /* If it's a `...', then we are computing the length of a parameter
21918      pack.  */
21919   if (keyword == RID_SIZEOF
21920       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21921     {
21922       /* Consume the `...'.  */
21923       cp_lexer_consume_token (parser->lexer);
21924       maybe_warn_variadic_templates ();
21925
21926       /* Note that this is an expansion.  */
21927       pack_expansion_p = true;
21928     }
21929
21930   /* Do not actually evaluate the expression.  */
21931   ++cp_unevaluated_operand;
21932   ++c_inhibit_evaluation_warnings;
21933   /* If it's a `(', then we might be looking at the type-id
21934      construction.  */
21935   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21936     {
21937       tree type;
21938       bool saved_in_type_id_in_expr_p;
21939
21940       /* We can't be sure yet whether we're looking at a type-id or an
21941          expression.  */
21942       cp_parser_parse_tentatively (parser);
21943       /* Consume the `('.  */
21944       cp_lexer_consume_token (parser->lexer);
21945       /* Parse the type-id.  */
21946       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21947       parser->in_type_id_in_expr_p = true;
21948       type = cp_parser_type_id (parser);
21949       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21950       /* Now, look for the trailing `)'.  */
21951       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21952       /* If all went well, then we're done.  */
21953       if (cp_parser_parse_definitely (parser))
21954         {
21955           cp_decl_specifier_seq decl_specs;
21956
21957           /* Build a trivial decl-specifier-seq.  */
21958           clear_decl_specs (&decl_specs);
21959           decl_specs.type = type;
21960
21961           /* Call grokdeclarator to figure out what type this is.  */
21962           expr = grokdeclarator (NULL,
21963                                  &decl_specs,
21964                                  TYPENAME,
21965                                  /*initialized=*/0,
21966                                  /*attrlist=*/NULL);
21967         }
21968     }
21969
21970   /* If the type-id production did not work out, then we must be
21971      looking at the unary-expression production.  */
21972   if (!expr)
21973     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21974                                        /*cast_p=*/false, NULL);
21975
21976   if (pack_expansion_p)
21977     /* Build a pack expansion. */
21978     expr = make_pack_expansion (expr);
21979
21980   /* Go back to evaluating expressions.  */
21981   --cp_unevaluated_operand;
21982   --c_inhibit_evaluation_warnings;
21983
21984   /* Free the message we created.  */
21985   free (tmp);
21986   /* And restore the old one.  */
21987   parser->type_definition_forbidden_message = saved_message;
21988   parser->integral_constant_expression_p
21989     = saved_integral_constant_expression_p;
21990   parser->non_integral_constant_expression_p
21991     = saved_non_integral_constant_expression_p;
21992
21993   return expr;
21994 }
21995
21996 /* If the current declaration has no declarator, return true.  */
21997
21998 static bool
21999 cp_parser_declares_only_class_p (cp_parser *parser)
22000 {
22001   /* If the next token is a `;' or a `,' then there is no
22002      declarator.  */
22003   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22004           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22005 }
22006
22007 /* Update the DECL_SPECS to reflect the storage class indicated by
22008    KEYWORD.  */
22009
22010 static void
22011 cp_parser_set_storage_class (cp_parser *parser,
22012                              cp_decl_specifier_seq *decl_specs,
22013                              enum rid keyword,
22014                              location_t location)
22015 {
22016   cp_storage_class storage_class;
22017
22018   if (parser->in_unbraced_linkage_specification_p)
22019     {
22020       error_at (location, "invalid use of %qD in linkage specification",
22021                 ridpointers[keyword]);
22022       return;
22023     }
22024   else if (decl_specs->storage_class != sc_none)
22025     {
22026       decl_specs->conflicting_specifiers_p = true;
22027       return;
22028     }
22029
22030   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22031       && decl_specs->specs[(int) ds_thread])
22032     {
22033       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22034       decl_specs->specs[(int) ds_thread] = 0;
22035     }
22036
22037   switch (keyword)
22038     {
22039     case RID_AUTO:
22040       storage_class = sc_auto;
22041       break;
22042     case RID_REGISTER:
22043       storage_class = sc_register;
22044       break;
22045     case RID_STATIC:
22046       storage_class = sc_static;
22047       break;
22048     case RID_EXTERN:
22049       storage_class = sc_extern;
22050       break;
22051     case RID_MUTABLE:
22052       storage_class = sc_mutable;
22053       break;
22054     default:
22055       gcc_unreachable ();
22056     }
22057   decl_specs->storage_class = storage_class;
22058
22059   /* A storage class specifier cannot be applied alongside a typedef 
22060      specifier. If there is a typedef specifier present then set 
22061      conflicting_specifiers_p which will trigger an error later
22062      on in grokdeclarator. */
22063   if (decl_specs->specs[(int)ds_typedef])
22064     decl_specs->conflicting_specifiers_p = true;
22065 }
22066
22067 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22068    is true, the type is a class or enum definition.  */
22069
22070 static void
22071 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22072                               tree type_spec,
22073                               location_t location,
22074                               bool type_definition_p)
22075 {
22076   decl_specs->any_specifiers_p = true;
22077
22078   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22079      (with, for example, in "typedef int wchar_t;") we remember that
22080      this is what happened.  In system headers, we ignore these
22081      declarations so that G++ can work with system headers that are not
22082      C++-safe.  */
22083   if (decl_specs->specs[(int) ds_typedef]
22084       && !type_definition_p
22085       && (type_spec == boolean_type_node
22086           || type_spec == char16_type_node
22087           || type_spec == char32_type_node
22088           || type_spec == wchar_type_node)
22089       && (decl_specs->type
22090           || decl_specs->specs[(int) ds_long]
22091           || decl_specs->specs[(int) ds_short]
22092           || decl_specs->specs[(int) ds_unsigned]
22093           || decl_specs->specs[(int) ds_signed]))
22094     {
22095       decl_specs->redefined_builtin_type = type_spec;
22096       if (!decl_specs->type)
22097         {
22098           decl_specs->type = type_spec;
22099           decl_specs->type_definition_p = false;
22100           decl_specs->type_location = location;
22101         }
22102     }
22103   else if (decl_specs->type)
22104     decl_specs->multiple_types_p = true;
22105   else
22106     {
22107       decl_specs->type = type_spec;
22108       decl_specs->type_definition_p = type_definition_p;
22109       decl_specs->redefined_builtin_type = NULL_TREE;
22110       decl_specs->type_location = location;
22111     }
22112 }
22113
22114 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22115    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22116
22117 static bool
22118 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22119 {
22120   return decl_specifiers->specs[(int) ds_friend] != 0;
22121 }
22122
22123 /* Issue an error message indicating that TOKEN_DESC was expected.
22124    If KEYWORD is true, it indicated this function is called by
22125    cp_parser_require_keword and the required token can only be
22126    a indicated keyword. */
22127
22128 static void
22129 cp_parser_required_error (cp_parser *parser,
22130                           required_token token_desc,
22131                           bool keyword)
22132 {
22133   switch (token_desc)
22134     {
22135       case RT_NEW:
22136         cp_parser_error (parser, "expected %<new%>");
22137         return;
22138       case RT_DELETE:
22139         cp_parser_error (parser, "expected %<delete%>");
22140         return;
22141       case RT_RETURN:
22142         cp_parser_error (parser, "expected %<return%>");
22143         return;
22144       case RT_WHILE:
22145         cp_parser_error (parser, "expected %<while%>");
22146         return;
22147       case RT_EXTERN:
22148         cp_parser_error (parser, "expected %<extern%>");
22149         return;
22150       case RT_STATIC_ASSERT:
22151         cp_parser_error (parser, "expected %<static_assert%>");
22152         return;
22153       case RT_DECLTYPE:
22154         cp_parser_error (parser, "expected %<decltype%>");
22155         return;
22156       case RT_OPERATOR:
22157         cp_parser_error (parser, "expected %<operator%>");
22158         return;
22159       case RT_CLASS:
22160         cp_parser_error (parser, "expected %<class%>");
22161         return;
22162       case RT_TEMPLATE:
22163         cp_parser_error (parser, "expected %<template%>");
22164         return;
22165       case RT_NAMESPACE:
22166         cp_parser_error (parser, "expected %<namespace%>");
22167         return;
22168       case RT_USING:
22169         cp_parser_error (parser, "expected %<using%>");
22170         return;
22171       case RT_ASM:
22172         cp_parser_error (parser, "expected %<asm%>");
22173         return;
22174       case RT_TRY:
22175         cp_parser_error (parser, "expected %<try%>");
22176         return;
22177       case RT_CATCH:
22178         cp_parser_error (parser, "expected %<catch%>");
22179         return;
22180       case RT_THROW:
22181         cp_parser_error (parser, "expected %<throw%>");
22182         return;
22183       case RT_LABEL:
22184         cp_parser_error (parser, "expected %<__label__%>");
22185         return;
22186       case RT_AT_TRY:
22187         cp_parser_error (parser, "expected %<@try%>");
22188         return;
22189       case RT_AT_SYNCHRONIZED:
22190         cp_parser_error (parser, "expected %<@synchronized%>");
22191         return;
22192       case RT_AT_THROW:
22193         cp_parser_error (parser, "expected %<@throw%>");
22194         return;
22195       case RT_TRANSACTION_ATOMIC:
22196         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22197         return;
22198       case RT_TRANSACTION_RELAXED:
22199         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22200         return;
22201       default:
22202         break;
22203     }
22204   if (!keyword)
22205     {
22206       switch (token_desc)
22207         {
22208           case RT_SEMICOLON:
22209             cp_parser_error (parser, "expected %<;%>");
22210             return;
22211           case RT_OPEN_PAREN:
22212             cp_parser_error (parser, "expected %<(%>");
22213             return;
22214           case RT_CLOSE_BRACE:
22215             cp_parser_error (parser, "expected %<}%>");
22216             return;
22217           case RT_OPEN_BRACE:
22218             cp_parser_error (parser, "expected %<{%>");
22219             return;
22220           case RT_CLOSE_SQUARE:
22221             cp_parser_error (parser, "expected %<]%>");
22222             return;
22223           case RT_OPEN_SQUARE:
22224             cp_parser_error (parser, "expected %<[%>");
22225             return;
22226           case RT_COMMA:
22227             cp_parser_error (parser, "expected %<,%>");
22228             return;
22229           case RT_SCOPE:
22230             cp_parser_error (parser, "expected %<::%>");
22231             return;
22232           case RT_LESS:
22233             cp_parser_error (parser, "expected %<<%>");
22234             return;
22235           case RT_GREATER:
22236             cp_parser_error (parser, "expected %<>%>");
22237             return;
22238           case RT_EQ:
22239             cp_parser_error (parser, "expected %<=%>");
22240             return;
22241           case RT_ELLIPSIS:
22242             cp_parser_error (parser, "expected %<...%>");
22243             return;
22244           case RT_MULT:
22245             cp_parser_error (parser, "expected %<*%>");
22246             return;
22247           case RT_COMPL:
22248             cp_parser_error (parser, "expected %<~%>");
22249             return;
22250           case RT_COLON:
22251             cp_parser_error (parser, "expected %<:%>");
22252             return;
22253           case RT_COLON_SCOPE:
22254             cp_parser_error (parser, "expected %<:%> or %<::%>");
22255             return;
22256           case RT_CLOSE_PAREN:
22257             cp_parser_error (parser, "expected %<)%>");
22258             return;
22259           case RT_COMMA_CLOSE_PAREN:
22260             cp_parser_error (parser, "expected %<,%> or %<)%>");
22261             return;
22262           case RT_PRAGMA_EOL:
22263             cp_parser_error (parser, "expected end of line");
22264             return;
22265           case RT_NAME:
22266             cp_parser_error (parser, "expected identifier");
22267             return;
22268           case RT_SELECT:
22269             cp_parser_error (parser, "expected selection-statement");
22270             return;
22271           case RT_INTERATION:
22272             cp_parser_error (parser, "expected iteration-statement");
22273             return;
22274           case RT_JUMP:
22275             cp_parser_error (parser, "expected jump-statement");
22276             return;
22277           case RT_CLASS_KEY:
22278             cp_parser_error (parser, "expected class-key");
22279             return;
22280           case RT_CLASS_TYPENAME_TEMPLATE:
22281             cp_parser_error (parser,
22282                  "expected %<class%>, %<typename%>, or %<template%>");
22283             return;
22284           default:
22285             gcc_unreachable ();
22286         }
22287     }
22288   else
22289     gcc_unreachable ();
22290 }
22291
22292
22293
22294 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22295    issue an error message indicating that TOKEN_DESC was expected.
22296
22297    Returns the token consumed, if the token had the appropriate type.
22298    Otherwise, returns NULL.  */
22299
22300 static cp_token *
22301 cp_parser_require (cp_parser* parser,
22302                    enum cpp_ttype type,
22303                    required_token token_desc)
22304 {
22305   if (cp_lexer_next_token_is (parser->lexer, type))
22306     return cp_lexer_consume_token (parser->lexer);
22307   else
22308     {
22309       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22310       if (!cp_parser_simulate_error (parser))
22311         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22312       return NULL;
22313     }
22314 }
22315
22316 /* An error message is produced if the next token is not '>'.
22317    All further tokens are skipped until the desired token is
22318    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22319
22320 static void
22321 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22322 {
22323   /* Current level of '< ... >'.  */
22324   unsigned level = 0;
22325   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22326   unsigned nesting_depth = 0;
22327
22328   /* Are we ready, yet?  If not, issue error message.  */
22329   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22330     return;
22331
22332   /* Skip tokens until the desired token is found.  */
22333   while (true)
22334     {
22335       /* Peek at the next token.  */
22336       switch (cp_lexer_peek_token (parser->lexer)->type)
22337         {
22338         case CPP_LESS:
22339           if (!nesting_depth)
22340             ++level;
22341           break;
22342
22343         case CPP_RSHIFT:
22344           if (cxx_dialect == cxx98)
22345             /* C++0x views the `>>' operator as two `>' tokens, but
22346                C++98 does not. */
22347             break;
22348           else if (!nesting_depth && level-- == 0)
22349             {
22350               /* We've hit a `>>' where the first `>' closes the
22351                  template argument list, and the second `>' is
22352                  spurious.  Just consume the `>>' and stop; we've
22353                  already produced at least one error.  */
22354               cp_lexer_consume_token (parser->lexer);
22355               return;
22356             }
22357           /* Fall through for C++0x, so we handle the second `>' in
22358              the `>>'.  */
22359
22360         case CPP_GREATER:
22361           if (!nesting_depth && level-- == 0)
22362             {
22363               /* We've reached the token we want, consume it and stop.  */
22364               cp_lexer_consume_token (parser->lexer);
22365               return;
22366             }
22367           break;
22368
22369         case CPP_OPEN_PAREN:
22370         case CPP_OPEN_SQUARE:
22371           ++nesting_depth;
22372           break;
22373
22374         case CPP_CLOSE_PAREN:
22375         case CPP_CLOSE_SQUARE:
22376           if (nesting_depth-- == 0)
22377             return;
22378           break;
22379
22380         case CPP_EOF:
22381         case CPP_PRAGMA_EOL:
22382         case CPP_SEMICOLON:
22383         case CPP_OPEN_BRACE:
22384         case CPP_CLOSE_BRACE:
22385           /* The '>' was probably forgotten, don't look further.  */
22386           return;
22387
22388         default:
22389           break;
22390         }
22391
22392       /* Consume this token.  */
22393       cp_lexer_consume_token (parser->lexer);
22394     }
22395 }
22396
22397 /* If the next token is the indicated keyword, consume it.  Otherwise,
22398    issue an error message indicating that TOKEN_DESC was expected.
22399
22400    Returns the token consumed, if the token had the appropriate type.
22401    Otherwise, returns NULL.  */
22402
22403 static cp_token *
22404 cp_parser_require_keyword (cp_parser* parser,
22405                            enum rid keyword,
22406                            required_token token_desc)
22407 {
22408   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22409
22410   if (token && token->keyword != keyword)
22411     {
22412       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22413       return NULL;
22414     }
22415
22416   return token;
22417 }
22418
22419 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22420    function-definition.  */
22421
22422 static bool
22423 cp_parser_token_starts_function_definition_p (cp_token* token)
22424 {
22425   return (/* An ordinary function-body begins with an `{'.  */
22426           token->type == CPP_OPEN_BRACE
22427           /* A ctor-initializer begins with a `:'.  */
22428           || token->type == CPP_COLON
22429           /* A function-try-block begins with `try'.  */
22430           || token->keyword == RID_TRY
22431           /* A function-transaction-block begins with `__transaction_atomic'
22432              or `__transaction_relaxed'.  */
22433           || token->keyword == RID_TRANSACTION_ATOMIC
22434           || token->keyword == RID_TRANSACTION_RELAXED
22435           /* The named return value extension begins with `return'.  */
22436           || token->keyword == RID_RETURN);
22437 }
22438
22439 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22440    definition.  */
22441
22442 static bool
22443 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22444 {
22445   cp_token *token;
22446
22447   token = cp_lexer_peek_token (parser->lexer);
22448   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22449 }
22450
22451 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22452    C++0x) ending a template-argument.  */
22453
22454 static bool
22455 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22456 {
22457   cp_token *token;
22458
22459   token = cp_lexer_peek_token (parser->lexer);
22460   return (token->type == CPP_COMMA 
22461           || token->type == CPP_GREATER
22462           || token->type == CPP_ELLIPSIS
22463           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22464 }
22465
22466 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22467    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22468
22469 static bool
22470 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22471                                                      size_t n)
22472 {
22473   cp_token *token;
22474
22475   token = cp_lexer_peek_nth_token (parser->lexer, n);
22476   if (token->type == CPP_LESS)
22477     return true;
22478   /* Check for the sequence `<::' in the original code. It would be lexed as
22479      `[:', where `[' is a digraph, and there is no whitespace before
22480      `:'.  */
22481   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22482     {
22483       cp_token *token2;
22484       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22485       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22486         return true;
22487     }
22488   return false;
22489 }
22490
22491 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22492    or none_type otherwise.  */
22493
22494 static enum tag_types
22495 cp_parser_token_is_class_key (cp_token* token)
22496 {
22497   switch (token->keyword)
22498     {
22499     case RID_CLASS:
22500       return class_type;
22501     case RID_STRUCT:
22502       return record_type;
22503     case RID_UNION:
22504       return union_type;
22505
22506     default:
22507       return none_type;
22508     }
22509 }
22510
22511 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22512
22513 static void
22514 cp_parser_check_class_key (enum tag_types class_key, tree type)
22515 {
22516   if (type == error_mark_node)
22517     return;
22518   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22519     {
22520       permerror (input_location, "%qs tag used in naming %q#T",
22521                  class_key == union_type ? "union"
22522                  : class_key == record_type ? "struct" : "class",
22523                  type);
22524       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22525               "%q#T was previously declared here", type);
22526     }
22527 }
22528
22529 /* Issue an error message if DECL is redeclared with different
22530    access than its original declaration [class.access.spec/3].
22531    This applies to nested classes and nested class templates.
22532    [class.mem/1].  */
22533
22534 static void
22535 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22536 {
22537   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22538     return;
22539
22540   if ((TREE_PRIVATE (decl)
22541        != (current_access_specifier == access_private_node))
22542       || (TREE_PROTECTED (decl)
22543           != (current_access_specifier == access_protected_node)))
22544     error_at (location, "%qD redeclared with different access", decl);
22545 }
22546
22547 /* Look for the `template' keyword, as a syntactic disambiguator.
22548    Return TRUE iff it is present, in which case it will be
22549    consumed.  */
22550
22551 static bool
22552 cp_parser_optional_template_keyword (cp_parser *parser)
22553 {
22554   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22555     {
22556       /* The `template' keyword can only be used within templates;
22557          outside templates the parser can always figure out what is a
22558          template and what is not.  */
22559       if (!processing_template_decl)
22560         {
22561           cp_token *token = cp_lexer_peek_token (parser->lexer);
22562           error_at (token->location,
22563                     "%<template%> (as a disambiguator) is only allowed "
22564                     "within templates");
22565           /* If this part of the token stream is rescanned, the same
22566              error message would be generated.  So, we purge the token
22567              from the stream.  */
22568           cp_lexer_purge_token (parser->lexer);
22569           return false;
22570         }
22571       else
22572         {
22573           /* Consume the `template' keyword.  */
22574           cp_lexer_consume_token (parser->lexer);
22575           return true;
22576         }
22577     }
22578
22579   return false;
22580 }
22581
22582 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22583    set PARSER->SCOPE, and perform other related actions.  */
22584
22585 static void
22586 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22587 {
22588   int i;
22589   struct tree_check *check_value;
22590   deferred_access_check *chk;
22591   VEC (deferred_access_check,gc) *checks;
22592
22593   /* Get the stored value.  */
22594   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22595   /* Perform any access checks that were deferred.  */
22596   checks = check_value->checks;
22597   if (checks)
22598     {
22599       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22600         perform_or_defer_access_check (chk->binfo,
22601                                        chk->decl,
22602                                        chk->diag_decl);
22603     }
22604   /* Set the scope from the stored value.  */
22605   parser->scope = check_value->value;
22606   parser->qualifying_scope = check_value->qualifying_scope;
22607   parser->object_scope = NULL_TREE;
22608 }
22609
22610 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22611    encounter the end of a block before what we were looking for.  */
22612
22613 static bool
22614 cp_parser_cache_group (cp_parser *parser,
22615                        enum cpp_ttype end,
22616                        unsigned depth)
22617 {
22618   while (true)
22619     {
22620       cp_token *token = cp_lexer_peek_token (parser->lexer);
22621
22622       /* Abort a parenthesized expression if we encounter a semicolon.  */
22623       if ((end == CPP_CLOSE_PAREN || depth == 0)
22624           && token->type == CPP_SEMICOLON)
22625         return true;
22626       /* If we've reached the end of the file, stop.  */
22627       if (token->type == CPP_EOF
22628           || (end != CPP_PRAGMA_EOL
22629               && token->type == CPP_PRAGMA_EOL))
22630         return true;
22631       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22632         /* We've hit the end of an enclosing block, so there's been some
22633            kind of syntax error.  */
22634         return true;
22635
22636       /* Consume the token.  */
22637       cp_lexer_consume_token (parser->lexer);
22638       /* See if it starts a new group.  */
22639       if (token->type == CPP_OPEN_BRACE)
22640         {
22641           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22642           /* In theory this should probably check end == '}', but
22643              cp_parser_save_member_function_body needs it to exit
22644              after either '}' or ')' when called with ')'.  */
22645           if (depth == 0)
22646             return false;
22647         }
22648       else if (token->type == CPP_OPEN_PAREN)
22649         {
22650           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22651           if (depth == 0 && end == CPP_CLOSE_PAREN)
22652             return false;
22653         }
22654       else if (token->type == CPP_PRAGMA)
22655         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22656       else if (token->type == end)
22657         return false;
22658     }
22659 }
22660
22661 /* Like above, for caching a default argument or NSDMI.  Both of these are
22662    terminated by a non-nested comma, but it can be unclear whether or not a
22663    comma is nested in a template argument list unless we do more parsing.
22664    In order to handle this ambiguity, when we encounter a ',' after a '<'
22665    we try to parse what follows as a parameter-declaration-list (in the
22666    case of a default argument) or a member-declarator (in the case of an
22667    NSDMI).  If that succeeds, then we stop caching.  */
22668
22669 static tree
22670 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22671 {
22672   unsigned depth = 0;
22673   int maybe_template_id = 0;
22674   cp_token *first_token;
22675   cp_token *token;
22676   tree default_argument;
22677
22678   /* Add tokens until we have processed the entire default
22679      argument.  We add the range [first_token, token).  */
22680   first_token = cp_lexer_peek_token (parser->lexer);
22681   if (first_token->type == CPP_OPEN_BRACE)
22682     {
22683       /* For list-initialization, this is straightforward.  */
22684       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22685       token = cp_lexer_peek_token (parser->lexer);
22686     }
22687   else while (true)
22688     {
22689       bool done = false;
22690
22691       /* Peek at the next token.  */
22692       token = cp_lexer_peek_token (parser->lexer);
22693       /* What we do depends on what token we have.  */
22694       switch (token->type)
22695         {
22696           /* In valid code, a default argument must be
22697              immediately followed by a `,' `)', or `...'.  */
22698         case CPP_COMMA:
22699           if (depth == 0 && maybe_template_id)
22700             {
22701               /* If we've seen a '<', we might be in a
22702                  template-argument-list.  Until Core issue 325 is
22703                  resolved, we don't know how this situation ought
22704                  to be handled, so try to DTRT.  We check whether
22705                  what comes after the comma is a valid parameter
22706                  declaration list.  If it is, then the comma ends
22707                  the default argument; otherwise the default
22708                  argument continues.  */
22709               bool error = false;
22710               tree t;
22711
22712               /* Set ITALP so cp_parser_parameter_declaration_list
22713                  doesn't decide to commit to this parse.  */
22714               bool saved_italp = parser->in_template_argument_list_p;
22715               parser->in_template_argument_list_p = true;
22716
22717               cp_parser_parse_tentatively (parser);
22718               cp_lexer_consume_token (parser->lexer);
22719
22720               if (nsdmi)
22721                 {
22722                   int ctor_dtor_or_conv_p;
22723                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22724                                         &ctor_dtor_or_conv_p,
22725                                         /*parenthesized_p=*/NULL,
22726                                         /*member_p=*/true);
22727                 }
22728               else
22729                 {
22730                   begin_scope (sk_function_parms, NULL_TREE);
22731                   cp_parser_parameter_declaration_list (parser, &error);
22732                   for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22733                     pop_binding (DECL_NAME (t), t);
22734                   leave_scope ();
22735                 }
22736               if (!cp_parser_error_occurred (parser) && !error)
22737                 done = true;
22738               cp_parser_abort_tentative_parse (parser);
22739
22740               parser->in_template_argument_list_p = saved_italp;
22741               break;
22742             }
22743         case CPP_CLOSE_PAREN:
22744         case CPP_ELLIPSIS:
22745           /* If we run into a non-nested `;', `}', or `]',
22746              then the code is invalid -- but the default
22747              argument is certainly over.  */
22748         case CPP_SEMICOLON:
22749         case CPP_CLOSE_BRACE:
22750         case CPP_CLOSE_SQUARE:
22751           if (depth == 0)
22752             done = true;
22753           /* Update DEPTH, if necessary.  */
22754           else if (token->type == CPP_CLOSE_PAREN
22755                    || token->type == CPP_CLOSE_BRACE
22756                    || token->type == CPP_CLOSE_SQUARE)
22757             --depth;
22758           break;
22759
22760         case CPP_OPEN_PAREN:
22761         case CPP_OPEN_SQUARE:
22762         case CPP_OPEN_BRACE:
22763           ++depth;
22764           break;
22765
22766         case CPP_LESS:
22767           if (depth == 0)
22768             /* This might be the comparison operator, or it might
22769                start a template argument list.  */
22770             ++maybe_template_id;
22771           break;
22772
22773         case CPP_RSHIFT:
22774           if (cxx_dialect == cxx98)
22775             break;
22776           /* Fall through for C++0x, which treats the `>>'
22777              operator like two `>' tokens in certain
22778              cases.  */
22779
22780         case CPP_GREATER:
22781           if (depth == 0)
22782             {
22783               /* This might be an operator, or it might close a
22784                  template argument list.  But if a previous '<'
22785                  started a template argument list, this will have
22786                  closed it, so we can't be in one anymore.  */
22787               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22788               if (maybe_template_id < 0)
22789                 maybe_template_id = 0;
22790             }
22791           break;
22792
22793           /* If we run out of tokens, issue an error message.  */
22794         case CPP_EOF:
22795         case CPP_PRAGMA_EOL:
22796           error_at (token->location, "file ends in default argument");
22797           done = true;
22798           break;
22799
22800         case CPP_NAME:
22801         case CPP_SCOPE:
22802           /* In these cases, we should look for template-ids.
22803              For example, if the default argument is
22804              `X<int, double>()', we need to do name lookup to
22805              figure out whether or not `X' is a template; if
22806              so, the `,' does not end the default argument.
22807
22808              That is not yet done.  */
22809           break;
22810
22811         default:
22812           break;
22813         }
22814
22815       /* If we've reached the end, stop.  */
22816       if (done)
22817         break;
22818
22819       /* Add the token to the token block.  */
22820       token = cp_lexer_consume_token (parser->lexer);
22821     }
22822
22823   /* Create a DEFAULT_ARG to represent the unparsed default
22824      argument.  */
22825   default_argument = make_node (DEFAULT_ARG);
22826   DEFARG_TOKENS (default_argument)
22827     = cp_token_cache_new (first_token, token);
22828   DEFARG_INSTANTIATIONS (default_argument) = NULL;
22829
22830   return default_argument;
22831 }
22832
22833 /* Begin parsing tentatively.  We always save tokens while parsing
22834    tentatively so that if the tentative parsing fails we can restore the
22835    tokens.  */
22836
22837 static void
22838 cp_parser_parse_tentatively (cp_parser* parser)
22839 {
22840   /* Enter a new parsing context.  */
22841   parser->context = cp_parser_context_new (parser->context);
22842   /* Begin saving tokens.  */
22843   cp_lexer_save_tokens (parser->lexer);
22844   /* In order to avoid repetitive access control error messages,
22845      access checks are queued up until we are no longer parsing
22846      tentatively.  */
22847   push_deferring_access_checks (dk_deferred);
22848 }
22849
22850 /* Commit to the currently active tentative parse.  */
22851
22852 static void
22853 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22854 {
22855   cp_parser_context *context;
22856   cp_lexer *lexer;
22857
22858   /* Mark all of the levels as committed.  */
22859   lexer = parser->lexer;
22860   for (context = parser->context; context->next; context = context->next)
22861     {
22862       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22863         break;
22864       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22865       while (!cp_lexer_saving_tokens (lexer))
22866         lexer = lexer->next;
22867       cp_lexer_commit_tokens (lexer);
22868     }
22869 }
22870
22871 /* Abort the currently active tentative parse.  All consumed tokens
22872    will be rolled back, and no diagnostics will be issued.  */
22873
22874 static void
22875 cp_parser_abort_tentative_parse (cp_parser* parser)
22876 {
22877   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22878               || errorcount > 0);
22879   cp_parser_simulate_error (parser);
22880   /* Now, pretend that we want to see if the construct was
22881      successfully parsed.  */
22882   cp_parser_parse_definitely (parser);
22883 }
22884
22885 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22886    token stream.  Otherwise, commit to the tokens we have consumed.
22887    Returns true if no error occurred; false otherwise.  */
22888
22889 static bool
22890 cp_parser_parse_definitely (cp_parser* parser)
22891 {
22892   bool error_occurred;
22893   cp_parser_context *context;
22894
22895   /* Remember whether or not an error occurred, since we are about to
22896      destroy that information.  */
22897   error_occurred = cp_parser_error_occurred (parser);
22898   /* Remove the topmost context from the stack.  */
22899   context = parser->context;
22900   parser->context = context->next;
22901   /* If no parse errors occurred, commit to the tentative parse.  */
22902   if (!error_occurred)
22903     {
22904       /* Commit to the tokens read tentatively, unless that was
22905          already done.  */
22906       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22907         cp_lexer_commit_tokens (parser->lexer);
22908
22909       pop_to_parent_deferring_access_checks ();
22910     }
22911   /* Otherwise, if errors occurred, roll back our state so that things
22912      are just as they were before we began the tentative parse.  */
22913   else
22914     {
22915       cp_lexer_rollback_tokens (parser->lexer);
22916       pop_deferring_access_checks ();
22917     }
22918   /* Add the context to the front of the free list.  */
22919   context->next = cp_parser_context_free_list;
22920   cp_parser_context_free_list = context;
22921
22922   return !error_occurred;
22923 }
22924
22925 /* Returns true if we are parsing tentatively and are not committed to
22926    this tentative parse.  */
22927
22928 static bool
22929 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22930 {
22931   return (cp_parser_parsing_tentatively (parser)
22932           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22933 }
22934
22935 /* Returns nonzero iff an error has occurred during the most recent
22936    tentative parse.  */
22937
22938 static bool
22939 cp_parser_error_occurred (cp_parser* parser)
22940 {
22941   return (cp_parser_parsing_tentatively (parser)
22942           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22943 }
22944
22945 /* Returns nonzero if GNU extensions are allowed.  */
22946
22947 static bool
22948 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22949 {
22950   return parser->allow_gnu_extensions_p;
22951 }
22952 \f
22953 /* Objective-C++ Productions */
22954
22955
22956 /* Parse an Objective-C expression, which feeds into a primary-expression
22957    above.
22958
22959    objc-expression:
22960      objc-message-expression
22961      objc-string-literal
22962      objc-encode-expression
22963      objc-protocol-expression
22964      objc-selector-expression
22965
22966   Returns a tree representation of the expression.  */
22967
22968 static tree
22969 cp_parser_objc_expression (cp_parser* parser)
22970 {
22971   /* Try to figure out what kind of declaration is present.  */
22972   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22973
22974   switch (kwd->type)
22975     {
22976     case CPP_OPEN_SQUARE:
22977       return cp_parser_objc_message_expression (parser);
22978
22979     case CPP_OBJC_STRING:
22980       kwd = cp_lexer_consume_token (parser->lexer);
22981       return objc_build_string_object (kwd->u.value);
22982
22983     case CPP_KEYWORD:
22984       switch (kwd->keyword)
22985         {
22986         case RID_AT_ENCODE:
22987           return cp_parser_objc_encode_expression (parser);
22988
22989         case RID_AT_PROTOCOL:
22990           return cp_parser_objc_protocol_expression (parser);
22991
22992         case RID_AT_SELECTOR:
22993           return cp_parser_objc_selector_expression (parser);
22994
22995         default:
22996           break;
22997         }
22998     default:
22999       error_at (kwd->location,
23000                 "misplaced %<@%D%> Objective-C++ construct",
23001                 kwd->u.value);
23002       cp_parser_skip_to_end_of_block_or_statement (parser);
23003     }
23004
23005   return error_mark_node;
23006 }
23007
23008 /* Parse an Objective-C message expression.
23009
23010    objc-message-expression:
23011      [ objc-message-receiver objc-message-args ]
23012
23013    Returns a representation of an Objective-C message.  */
23014
23015 static tree
23016 cp_parser_objc_message_expression (cp_parser* parser)
23017 {
23018   tree receiver, messageargs;
23019
23020   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
23021   receiver = cp_parser_objc_message_receiver (parser);
23022   messageargs = cp_parser_objc_message_args (parser);
23023   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23024
23025   return objc_build_message_expr (receiver, messageargs);
23026 }
23027
23028 /* Parse an objc-message-receiver.
23029
23030    objc-message-receiver:
23031      expression
23032      simple-type-specifier
23033
23034   Returns a representation of the type or expression.  */
23035
23036 static tree
23037 cp_parser_objc_message_receiver (cp_parser* parser)
23038 {
23039   tree rcv;
23040
23041   /* An Objective-C message receiver may be either (1) a type
23042      or (2) an expression.  */
23043   cp_parser_parse_tentatively (parser);
23044   rcv = cp_parser_expression (parser, false, NULL);
23045
23046   if (cp_parser_parse_definitely (parser))
23047     return rcv;
23048
23049   rcv = cp_parser_simple_type_specifier (parser,
23050                                          /*decl_specs=*/NULL,
23051                                          CP_PARSER_FLAGS_NONE);
23052
23053   return objc_get_class_reference (rcv);
23054 }
23055
23056 /* Parse the arguments and selectors comprising an Objective-C message.
23057
23058    objc-message-args:
23059      objc-selector
23060      objc-selector-args
23061      objc-selector-args , objc-comma-args
23062
23063    objc-selector-args:
23064      objc-selector [opt] : assignment-expression
23065      objc-selector-args objc-selector [opt] : assignment-expression
23066
23067    objc-comma-args:
23068      assignment-expression
23069      objc-comma-args , assignment-expression
23070
23071    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23072    selector arguments and TREE_VALUE containing a list of comma
23073    arguments.  */
23074
23075 static tree
23076 cp_parser_objc_message_args (cp_parser* parser)
23077 {
23078   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23079   bool maybe_unary_selector_p = true;
23080   cp_token *token = cp_lexer_peek_token (parser->lexer);
23081
23082   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23083     {
23084       tree selector = NULL_TREE, arg;
23085
23086       if (token->type != CPP_COLON)
23087         selector = cp_parser_objc_selector (parser);
23088
23089       /* Detect if we have a unary selector.  */
23090       if (maybe_unary_selector_p
23091           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23092         return build_tree_list (selector, NULL_TREE);
23093
23094       maybe_unary_selector_p = false;
23095       cp_parser_require (parser, CPP_COLON, RT_COLON);
23096       arg = cp_parser_assignment_expression (parser, false, NULL);
23097
23098       sel_args
23099         = chainon (sel_args,
23100                    build_tree_list (selector, arg));
23101
23102       token = cp_lexer_peek_token (parser->lexer);
23103     }
23104
23105   /* Handle non-selector arguments, if any. */
23106   while (token->type == CPP_COMMA)
23107     {
23108       tree arg;
23109
23110       cp_lexer_consume_token (parser->lexer);
23111       arg = cp_parser_assignment_expression (parser, false, NULL);
23112
23113       addl_args
23114         = chainon (addl_args,
23115                    build_tree_list (NULL_TREE, arg));
23116
23117       token = cp_lexer_peek_token (parser->lexer);
23118     }
23119
23120   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23121     {
23122       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23123       return build_tree_list (error_mark_node, error_mark_node);
23124     }
23125
23126   return build_tree_list (sel_args, addl_args);
23127 }
23128
23129 /* Parse an Objective-C encode expression.
23130
23131    objc-encode-expression:
23132      @encode objc-typename
23133
23134    Returns an encoded representation of the type argument.  */
23135
23136 static tree
23137 cp_parser_objc_encode_expression (cp_parser* parser)
23138 {
23139   tree type;
23140   cp_token *token;
23141
23142   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
23143   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23144   token = cp_lexer_peek_token (parser->lexer);
23145   type = complete_type (cp_parser_type_id (parser));
23146   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23147
23148   if (!type)
23149     {
23150       error_at (token->location, 
23151                 "%<@encode%> must specify a type as an argument");
23152       return error_mark_node;
23153     }
23154
23155   /* This happens if we find @encode(T) (where T is a template
23156      typename or something dependent on a template typename) when
23157      parsing a template.  In that case, we can't compile it
23158      immediately, but we rather create an AT_ENCODE_EXPR which will
23159      need to be instantiated when the template is used.
23160   */
23161   if (dependent_type_p (type))
23162     {
23163       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23164       TREE_READONLY (value) = 1;
23165       return value;
23166     }
23167
23168   return objc_build_encode_expr (type);
23169 }
23170
23171 /* Parse an Objective-C @defs expression.  */
23172
23173 static tree
23174 cp_parser_objc_defs_expression (cp_parser *parser)
23175 {
23176   tree name;
23177
23178   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
23179   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23180   name = cp_parser_identifier (parser);
23181   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23182
23183   return objc_get_class_ivars (name);
23184 }
23185
23186 /* Parse an Objective-C protocol expression.
23187
23188   objc-protocol-expression:
23189     @protocol ( identifier )
23190
23191   Returns a representation of the protocol expression.  */
23192
23193 static tree
23194 cp_parser_objc_protocol_expression (cp_parser* parser)
23195 {
23196   tree proto;
23197
23198   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23199   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23200   proto = cp_parser_identifier (parser);
23201   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23202
23203   return objc_build_protocol_expr (proto);
23204 }
23205
23206 /* Parse an Objective-C selector expression.
23207
23208    objc-selector-expression:
23209      @selector ( objc-method-signature )
23210
23211    objc-method-signature:
23212      objc-selector
23213      objc-selector-seq
23214
23215    objc-selector-seq:
23216      objc-selector :
23217      objc-selector-seq objc-selector :
23218
23219   Returns a representation of the method selector.  */
23220
23221 static tree
23222 cp_parser_objc_selector_expression (cp_parser* parser)
23223 {
23224   tree sel_seq = NULL_TREE;
23225   bool maybe_unary_selector_p = true;
23226   cp_token *token;
23227   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23228
23229   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23230   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23231   token = cp_lexer_peek_token (parser->lexer);
23232
23233   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23234          || token->type == CPP_SCOPE)
23235     {
23236       tree selector = NULL_TREE;
23237
23238       if (token->type != CPP_COLON
23239           || token->type == CPP_SCOPE)
23240         selector = cp_parser_objc_selector (parser);
23241
23242       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23243           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23244         {
23245           /* Detect if we have a unary selector.  */
23246           if (maybe_unary_selector_p)
23247             {
23248               sel_seq = selector;
23249               goto finish_selector;
23250             }
23251           else
23252             {
23253               cp_parser_error (parser, "expected %<:%>");
23254             }
23255         }
23256       maybe_unary_selector_p = false;
23257       token = cp_lexer_consume_token (parser->lexer);
23258
23259       if (token->type == CPP_SCOPE)
23260         {
23261           sel_seq
23262             = chainon (sel_seq,
23263                        build_tree_list (selector, NULL_TREE));
23264           sel_seq
23265             = chainon (sel_seq,
23266                        build_tree_list (NULL_TREE, NULL_TREE));
23267         }
23268       else
23269         sel_seq
23270           = chainon (sel_seq,
23271                      build_tree_list (selector, NULL_TREE));
23272
23273       token = cp_lexer_peek_token (parser->lexer);
23274     }
23275
23276  finish_selector:
23277   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23278
23279   return objc_build_selector_expr (loc, sel_seq);
23280 }
23281
23282 /* Parse a list of identifiers.
23283
23284    objc-identifier-list:
23285      identifier
23286      objc-identifier-list , identifier
23287
23288    Returns a TREE_LIST of identifier nodes.  */
23289
23290 static tree
23291 cp_parser_objc_identifier_list (cp_parser* parser)
23292 {
23293   tree identifier;
23294   tree list;
23295   cp_token *sep;
23296
23297   identifier = cp_parser_identifier (parser);
23298   if (identifier == error_mark_node)
23299     return error_mark_node;      
23300
23301   list = build_tree_list (NULL_TREE, identifier);
23302   sep = cp_lexer_peek_token (parser->lexer);
23303
23304   while (sep->type == CPP_COMMA)
23305     {
23306       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23307       identifier = cp_parser_identifier (parser);
23308       if (identifier == error_mark_node)
23309         return list;
23310
23311       list = chainon (list, build_tree_list (NULL_TREE,
23312                                              identifier));
23313       sep = cp_lexer_peek_token (parser->lexer);
23314     }
23315   
23316   return list;
23317 }
23318
23319 /* Parse an Objective-C alias declaration.
23320
23321    objc-alias-declaration:
23322      @compatibility_alias identifier identifier ;
23323
23324    This function registers the alias mapping with the Objective-C front end.
23325    It returns nothing.  */
23326
23327 static void
23328 cp_parser_objc_alias_declaration (cp_parser* parser)
23329 {
23330   tree alias, orig;
23331
23332   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23333   alias = cp_parser_identifier (parser);
23334   orig = cp_parser_identifier (parser);
23335   objc_declare_alias (alias, orig);
23336   cp_parser_consume_semicolon_at_end_of_statement (parser);
23337 }
23338
23339 /* Parse an Objective-C class forward-declaration.
23340
23341    objc-class-declaration:
23342      @class objc-identifier-list ;
23343
23344    The function registers the forward declarations with the Objective-C
23345    front end.  It returns nothing.  */
23346
23347 static void
23348 cp_parser_objc_class_declaration (cp_parser* parser)
23349 {
23350   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23351   while (true)
23352     {
23353       tree id;
23354       
23355       id = cp_parser_identifier (parser);
23356       if (id == error_mark_node)
23357         break;
23358       
23359       objc_declare_class (id);
23360
23361       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23362         cp_lexer_consume_token (parser->lexer);
23363       else
23364         break;
23365     }
23366   cp_parser_consume_semicolon_at_end_of_statement (parser);
23367 }
23368
23369 /* Parse a list of Objective-C protocol references.
23370
23371    objc-protocol-refs-opt:
23372      objc-protocol-refs [opt]
23373
23374    objc-protocol-refs:
23375      < objc-identifier-list >
23376
23377    Returns a TREE_LIST of identifiers, if any.  */
23378
23379 static tree
23380 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23381 {
23382   tree protorefs = NULL_TREE;
23383
23384   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23385     {
23386       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23387       protorefs = cp_parser_objc_identifier_list (parser);
23388       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23389     }
23390
23391   return protorefs;
23392 }
23393
23394 /* Parse a Objective-C visibility specification.  */
23395
23396 static void
23397 cp_parser_objc_visibility_spec (cp_parser* parser)
23398 {
23399   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23400
23401   switch (vis->keyword)
23402     {
23403     case RID_AT_PRIVATE:
23404       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23405       break;
23406     case RID_AT_PROTECTED:
23407       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23408       break;
23409     case RID_AT_PUBLIC:
23410       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23411       break;
23412     case RID_AT_PACKAGE:
23413       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23414       break;
23415     default:
23416       return;
23417     }
23418
23419   /* Eat '@private'/'@protected'/'@public'.  */
23420   cp_lexer_consume_token (parser->lexer);
23421 }
23422
23423 /* Parse an Objective-C method type.  Return 'true' if it is a class
23424    (+) method, and 'false' if it is an instance (-) method.  */
23425
23426 static inline bool
23427 cp_parser_objc_method_type (cp_parser* parser)
23428 {
23429   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23430     return true;
23431   else
23432     return false;
23433 }
23434
23435 /* Parse an Objective-C protocol qualifier.  */
23436
23437 static tree
23438 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23439 {
23440   tree quals = NULL_TREE, node;
23441   cp_token *token = cp_lexer_peek_token (parser->lexer);
23442
23443   node = token->u.value;
23444
23445   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23446          && (node == ridpointers [(int) RID_IN]
23447              || node == ridpointers [(int) RID_OUT]
23448              || node == ridpointers [(int) RID_INOUT]
23449              || node == ridpointers [(int) RID_BYCOPY]
23450              || node == ridpointers [(int) RID_BYREF]
23451              || node == ridpointers [(int) RID_ONEWAY]))
23452     {
23453       quals = tree_cons (NULL_TREE, node, quals);
23454       cp_lexer_consume_token (parser->lexer);
23455       token = cp_lexer_peek_token (parser->lexer);
23456       node = token->u.value;
23457     }
23458
23459   return quals;
23460 }
23461
23462 /* Parse an Objective-C typename.  */
23463
23464 static tree
23465 cp_parser_objc_typename (cp_parser* parser)
23466 {
23467   tree type_name = NULL_TREE;
23468
23469   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23470     {
23471       tree proto_quals, cp_type = NULL_TREE;
23472
23473       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23474       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23475
23476       /* An ObjC type name may consist of just protocol qualifiers, in which
23477          case the type shall default to 'id'.  */
23478       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23479         {
23480           cp_type = cp_parser_type_id (parser);
23481           
23482           /* If the type could not be parsed, an error has already
23483              been produced.  For error recovery, behave as if it had
23484              not been specified, which will use the default type
23485              'id'.  */
23486           if (cp_type == error_mark_node)
23487             {
23488               cp_type = NULL_TREE;
23489               /* We need to skip to the closing parenthesis as
23490                  cp_parser_type_id() does not seem to do it for
23491                  us.  */
23492               cp_parser_skip_to_closing_parenthesis (parser,
23493                                                      /*recovering=*/true,
23494                                                      /*or_comma=*/false,
23495                                                      /*consume_paren=*/false);
23496             }
23497         }
23498
23499       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23500       type_name = build_tree_list (proto_quals, cp_type);
23501     }
23502
23503   return type_name;
23504 }
23505
23506 /* Check to see if TYPE refers to an Objective-C selector name.  */
23507
23508 static bool
23509 cp_parser_objc_selector_p (enum cpp_ttype type)
23510 {
23511   return (type == CPP_NAME || type == CPP_KEYWORD
23512           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23513           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23514           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23515           || type == CPP_XOR || type == CPP_XOR_EQ);
23516 }
23517
23518 /* Parse an Objective-C selector.  */
23519
23520 static tree
23521 cp_parser_objc_selector (cp_parser* parser)
23522 {
23523   cp_token *token = cp_lexer_consume_token (parser->lexer);
23524
23525   if (!cp_parser_objc_selector_p (token->type))
23526     {
23527       error_at (token->location, "invalid Objective-C++ selector name");
23528       return error_mark_node;
23529     }
23530
23531   /* C++ operator names are allowed to appear in ObjC selectors.  */
23532   switch (token->type)
23533     {
23534     case CPP_AND_AND: return get_identifier ("and");
23535     case CPP_AND_EQ: return get_identifier ("and_eq");
23536     case CPP_AND: return get_identifier ("bitand");
23537     case CPP_OR: return get_identifier ("bitor");
23538     case CPP_COMPL: return get_identifier ("compl");
23539     case CPP_NOT: return get_identifier ("not");
23540     case CPP_NOT_EQ: return get_identifier ("not_eq");
23541     case CPP_OR_OR: return get_identifier ("or");
23542     case CPP_OR_EQ: return get_identifier ("or_eq");
23543     case CPP_XOR: return get_identifier ("xor");
23544     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23545     default: return token->u.value;
23546     }
23547 }
23548
23549 /* Parse an Objective-C params list.  */
23550
23551 static tree
23552 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23553 {
23554   tree params = NULL_TREE;
23555   bool maybe_unary_selector_p = true;
23556   cp_token *token = cp_lexer_peek_token (parser->lexer);
23557
23558   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23559     {
23560       tree selector = NULL_TREE, type_name, identifier;
23561       tree parm_attr = NULL_TREE;
23562
23563       if (token->keyword == RID_ATTRIBUTE)
23564         break;
23565
23566       if (token->type != CPP_COLON)
23567         selector = cp_parser_objc_selector (parser);
23568
23569       /* Detect if we have a unary selector.  */
23570       if (maybe_unary_selector_p
23571           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23572         {
23573           params = selector; /* Might be followed by attributes.  */
23574           break;
23575         }
23576
23577       maybe_unary_selector_p = false;
23578       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23579         {
23580           /* Something went quite wrong.  There should be a colon
23581              here, but there is not.  Stop parsing parameters.  */
23582           break;
23583         }
23584       type_name = cp_parser_objc_typename (parser);
23585       /* New ObjC allows attributes on parameters too.  */
23586       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23587         parm_attr = cp_parser_attributes_opt (parser);
23588       identifier = cp_parser_identifier (parser);
23589
23590       params
23591         = chainon (params,
23592                    objc_build_keyword_decl (selector,
23593                                             type_name,
23594                                             identifier,
23595                                             parm_attr));
23596
23597       token = cp_lexer_peek_token (parser->lexer);
23598     }
23599
23600   if (params == NULL_TREE)
23601     {
23602       cp_parser_error (parser, "objective-c++ method declaration is expected");
23603       return error_mark_node;
23604     }
23605
23606   /* We allow tail attributes for the method.  */
23607   if (token->keyword == RID_ATTRIBUTE)
23608     {
23609       *attributes = cp_parser_attributes_opt (parser);
23610       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23611           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23612         return params;
23613       cp_parser_error (parser, 
23614                        "method attributes must be specified at the end");
23615       return error_mark_node;
23616     }
23617
23618   if (params == NULL_TREE)
23619     {
23620       cp_parser_error (parser, "objective-c++ method declaration is expected");
23621       return error_mark_node;
23622     }
23623   return params;
23624 }
23625
23626 /* Parse the non-keyword Objective-C params.  */
23627
23628 static tree
23629 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23630                                        tree* attributes)
23631 {
23632   tree params = make_node (TREE_LIST);
23633   cp_token *token = cp_lexer_peek_token (parser->lexer);
23634   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23635
23636   while (token->type == CPP_COMMA)
23637     {
23638       cp_parameter_declarator *parmdecl;
23639       tree parm;
23640
23641       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23642       token = cp_lexer_peek_token (parser->lexer);
23643
23644       if (token->type == CPP_ELLIPSIS)
23645         {
23646           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23647           *ellipsisp = true;
23648           token = cp_lexer_peek_token (parser->lexer);
23649           break;
23650         }
23651
23652       /* TODO: parse attributes for tail parameters.  */
23653       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23654       parm = grokdeclarator (parmdecl->declarator,
23655                              &parmdecl->decl_specifiers,
23656                              PARM, /*initialized=*/0,
23657                              /*attrlist=*/NULL);
23658
23659       chainon (params, build_tree_list (NULL_TREE, parm));
23660       token = cp_lexer_peek_token (parser->lexer);
23661     }
23662
23663   /* We allow tail attributes for the method.  */
23664   if (token->keyword == RID_ATTRIBUTE)
23665     {
23666       if (*attributes == NULL_TREE)
23667         {
23668           *attributes = cp_parser_attributes_opt (parser);
23669           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23670               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23671             return params;
23672         }
23673       else        
23674         /* We have an error, but parse the attributes, so that we can 
23675            carry on.  */
23676         *attributes = cp_parser_attributes_opt (parser);
23677
23678       cp_parser_error (parser, 
23679                        "method attributes must be specified at the end");
23680       return error_mark_node;
23681     }
23682
23683   return params;
23684 }
23685
23686 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23687
23688 static void
23689 cp_parser_objc_interstitial_code (cp_parser* parser)
23690 {
23691   cp_token *token = cp_lexer_peek_token (parser->lexer);
23692
23693   /* If the next token is `extern' and the following token is a string
23694      literal, then we have a linkage specification.  */
23695   if (token->keyword == RID_EXTERN
23696       && cp_parser_is_pure_string_literal
23697          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23698     cp_parser_linkage_specification (parser);
23699   /* Handle #pragma, if any.  */
23700   else if (token->type == CPP_PRAGMA)
23701     cp_parser_pragma (parser, pragma_external);
23702   /* Allow stray semicolons.  */
23703   else if (token->type == CPP_SEMICOLON)
23704     cp_lexer_consume_token (parser->lexer);
23705   /* Mark methods as optional or required, when building protocols.  */
23706   else if (token->keyword == RID_AT_OPTIONAL)
23707     {
23708       cp_lexer_consume_token (parser->lexer);
23709       objc_set_method_opt (true);
23710     }
23711   else if (token->keyword == RID_AT_REQUIRED)
23712     {
23713       cp_lexer_consume_token (parser->lexer);
23714       objc_set_method_opt (false);
23715     }
23716   else if (token->keyword == RID_NAMESPACE)
23717     cp_parser_namespace_definition (parser);
23718   /* Other stray characters must generate errors.  */
23719   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23720     {
23721       cp_lexer_consume_token (parser->lexer);
23722       error ("stray %qs between Objective-C++ methods",
23723              token->type == CPP_OPEN_BRACE ? "{" : "}");
23724     }
23725   /* Finally, try to parse a block-declaration, or a function-definition.  */
23726   else
23727     cp_parser_block_declaration (parser, /*statement_p=*/false);
23728 }
23729
23730 /* Parse a method signature.  */
23731
23732 static tree
23733 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23734 {
23735   tree rettype, kwdparms, optparms;
23736   bool ellipsis = false;
23737   bool is_class_method;
23738
23739   is_class_method = cp_parser_objc_method_type (parser);
23740   rettype = cp_parser_objc_typename (parser);
23741   *attributes = NULL_TREE;
23742   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23743   if (kwdparms == error_mark_node)
23744     return error_mark_node;
23745   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23746   if (optparms == error_mark_node)
23747     return error_mark_node;
23748
23749   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23750 }
23751
23752 static bool
23753 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23754 {
23755   tree tattr;  
23756   cp_lexer_save_tokens (parser->lexer);
23757   tattr = cp_parser_attributes_opt (parser);
23758   gcc_assert (tattr) ;
23759   
23760   /* If the attributes are followed by a method introducer, this is not allowed.
23761      Dump the attributes and flag the situation.  */
23762   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23763       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23764     return true;
23765
23766   /* Otherwise, the attributes introduce some interstitial code, possibly so
23767      rewind to allow that check.  */
23768   cp_lexer_rollback_tokens (parser->lexer);
23769   return false;  
23770 }
23771
23772 /* Parse an Objective-C method prototype list.  */
23773
23774 static void
23775 cp_parser_objc_method_prototype_list (cp_parser* parser)
23776 {
23777   cp_token *token = cp_lexer_peek_token (parser->lexer);
23778
23779   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23780     {
23781       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23782         {
23783           tree attributes, sig;
23784           bool is_class_method;
23785           if (token->type == CPP_PLUS)
23786             is_class_method = true;
23787           else
23788             is_class_method = false;
23789           sig = cp_parser_objc_method_signature (parser, &attributes);
23790           if (sig == error_mark_node)
23791             {
23792               cp_parser_skip_to_end_of_block_or_statement (parser);
23793               token = cp_lexer_peek_token (parser->lexer);
23794               continue;
23795             }
23796           objc_add_method_declaration (is_class_method, sig, attributes);
23797           cp_parser_consume_semicolon_at_end_of_statement (parser);
23798         }
23799       else if (token->keyword == RID_AT_PROPERTY)
23800         cp_parser_objc_at_property_declaration (parser);
23801       else if (token->keyword == RID_ATTRIBUTE 
23802                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23803         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23804                     OPT_Wattributes, 
23805                     "prefix attributes are ignored for methods");
23806       else
23807         /* Allow for interspersed non-ObjC++ code.  */
23808         cp_parser_objc_interstitial_code (parser);
23809
23810       token = cp_lexer_peek_token (parser->lexer);
23811     }
23812
23813   if (token->type != CPP_EOF)
23814     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23815   else
23816     cp_parser_error (parser, "expected %<@end%>");
23817
23818   objc_finish_interface ();
23819 }
23820
23821 /* Parse an Objective-C method definition list.  */
23822
23823 static void
23824 cp_parser_objc_method_definition_list (cp_parser* parser)
23825 {
23826   cp_token *token = cp_lexer_peek_token (parser->lexer);
23827
23828   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23829     {
23830       tree meth;
23831
23832       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23833         {
23834           cp_token *ptk;
23835           tree sig, attribute;
23836           bool is_class_method;
23837           if (token->type == CPP_PLUS)
23838             is_class_method = true;
23839           else
23840             is_class_method = false;
23841           push_deferring_access_checks (dk_deferred);
23842           sig = cp_parser_objc_method_signature (parser, &attribute);
23843           if (sig == error_mark_node)
23844             {
23845               cp_parser_skip_to_end_of_block_or_statement (parser);
23846               token = cp_lexer_peek_token (parser->lexer);
23847               continue;
23848             }
23849           objc_start_method_definition (is_class_method, sig, attribute,
23850                                         NULL_TREE);
23851
23852           /* For historical reasons, we accept an optional semicolon.  */
23853           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23854             cp_lexer_consume_token (parser->lexer);
23855
23856           ptk = cp_lexer_peek_token (parser->lexer);
23857           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23858                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23859             {
23860               perform_deferred_access_checks ();
23861               stop_deferring_access_checks ();
23862               meth = cp_parser_function_definition_after_declarator (parser,
23863                                                                      false);
23864               pop_deferring_access_checks ();
23865               objc_finish_method_definition (meth);
23866             }
23867         }
23868       /* The following case will be removed once @synthesize is
23869          completely implemented.  */
23870       else if (token->keyword == RID_AT_PROPERTY)
23871         cp_parser_objc_at_property_declaration (parser);
23872       else if (token->keyword == RID_AT_SYNTHESIZE)
23873         cp_parser_objc_at_synthesize_declaration (parser);
23874       else if (token->keyword == RID_AT_DYNAMIC)
23875         cp_parser_objc_at_dynamic_declaration (parser);
23876       else if (token->keyword == RID_ATTRIBUTE 
23877                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23878         warning_at (token->location, OPT_Wattributes,
23879                     "prefix attributes are ignored for methods");
23880       else
23881         /* Allow for interspersed non-ObjC++ code.  */
23882         cp_parser_objc_interstitial_code (parser);
23883
23884       token = cp_lexer_peek_token (parser->lexer);
23885     }
23886
23887   if (token->type != CPP_EOF)
23888     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23889   else
23890     cp_parser_error (parser, "expected %<@end%>");
23891
23892   objc_finish_implementation ();
23893 }
23894
23895 /* Parse Objective-C ivars.  */
23896
23897 static void
23898 cp_parser_objc_class_ivars (cp_parser* parser)
23899 {
23900   cp_token *token = cp_lexer_peek_token (parser->lexer);
23901
23902   if (token->type != CPP_OPEN_BRACE)
23903     return;     /* No ivars specified.  */
23904
23905   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23906   token = cp_lexer_peek_token (parser->lexer);
23907
23908   while (token->type != CPP_CLOSE_BRACE 
23909         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23910     {
23911       cp_decl_specifier_seq declspecs;
23912       int decl_class_or_enum_p;
23913       tree prefix_attributes;
23914
23915       cp_parser_objc_visibility_spec (parser);
23916
23917       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23918         break;
23919
23920       cp_parser_decl_specifier_seq (parser,
23921                                     CP_PARSER_FLAGS_OPTIONAL,
23922                                     &declspecs,
23923                                     &decl_class_or_enum_p);
23924
23925       /* auto, register, static, extern, mutable.  */
23926       if (declspecs.storage_class != sc_none)
23927         {
23928           cp_parser_error (parser, "invalid type for instance variable");         
23929           declspecs.storage_class = sc_none;
23930         }
23931
23932       /* __thread.  */
23933       if (declspecs.specs[(int) ds_thread])
23934         {
23935           cp_parser_error (parser, "invalid type for instance variable");
23936           declspecs.specs[(int) ds_thread] = 0;
23937         }
23938       
23939       /* typedef.  */
23940       if (declspecs.specs[(int) ds_typedef])
23941         {
23942           cp_parser_error (parser, "invalid type for instance variable");
23943           declspecs.specs[(int) ds_typedef] = 0;
23944         }
23945
23946       prefix_attributes = declspecs.attributes;
23947       declspecs.attributes = NULL_TREE;
23948
23949       /* Keep going until we hit the `;' at the end of the
23950          declaration.  */
23951       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23952         {
23953           tree width = NULL_TREE, attributes, first_attribute, decl;
23954           cp_declarator *declarator = NULL;
23955           int ctor_dtor_or_conv_p;
23956
23957           /* Check for a (possibly unnamed) bitfield declaration.  */
23958           token = cp_lexer_peek_token (parser->lexer);
23959           if (token->type == CPP_COLON)
23960             goto eat_colon;
23961
23962           if (token->type == CPP_NAME
23963               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23964                   == CPP_COLON))
23965             {
23966               /* Get the name of the bitfield.  */
23967               declarator = make_id_declarator (NULL_TREE,
23968                                                cp_parser_identifier (parser),
23969                                                sfk_none);
23970
23971              eat_colon:
23972               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23973               /* Get the width of the bitfield.  */
23974               width
23975                 = cp_parser_constant_expression (parser,
23976                                                  /*allow_non_constant=*/false,
23977                                                  NULL);
23978             }
23979           else
23980             {
23981               /* Parse the declarator.  */
23982               declarator
23983                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23984                                         &ctor_dtor_or_conv_p,
23985                                         /*parenthesized_p=*/NULL,
23986                                         /*member_p=*/false);
23987             }
23988
23989           /* Look for attributes that apply to the ivar.  */
23990           attributes = cp_parser_attributes_opt (parser);
23991           /* Remember which attributes are prefix attributes and
23992              which are not.  */
23993           first_attribute = attributes;
23994           /* Combine the attributes.  */
23995           attributes = chainon (prefix_attributes, attributes);
23996
23997           if (width)
23998               /* Create the bitfield declaration.  */
23999               decl = grokbitfield (declarator, &declspecs,
24000                                    width,
24001                                    attributes);
24002           else
24003             decl = grokfield (declarator, &declspecs,
24004                               NULL_TREE, /*init_const_expr_p=*/false,
24005                               NULL_TREE, attributes);
24006
24007           /* Add the instance variable.  */
24008           if (decl != error_mark_node && decl != NULL_TREE)
24009             objc_add_instance_variable (decl);
24010
24011           /* Reset PREFIX_ATTRIBUTES.  */
24012           while (attributes && TREE_CHAIN (attributes) != first_attribute)
24013             attributes = TREE_CHAIN (attributes);
24014           if (attributes)
24015             TREE_CHAIN (attributes) = NULL_TREE;
24016
24017           token = cp_lexer_peek_token (parser->lexer);
24018
24019           if (token->type == CPP_COMMA)
24020             {
24021               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24022               continue;
24023             }
24024           break;
24025         }
24026
24027       cp_parser_consume_semicolon_at_end_of_statement (parser);
24028       token = cp_lexer_peek_token (parser->lexer);
24029     }
24030
24031   if (token->keyword == RID_AT_END)
24032     cp_parser_error (parser, "expected %<}%>");
24033
24034   /* Do not consume the RID_AT_END, so it will be read again as terminating
24035      the @interface of @implementation.  */ 
24036   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24037     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
24038     
24039   /* For historical reasons, we accept an optional semicolon.  */
24040   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24041     cp_lexer_consume_token (parser->lexer);
24042 }
24043
24044 /* Parse an Objective-C protocol declaration.  */
24045
24046 static void
24047 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24048 {
24049   tree proto, protorefs;
24050   cp_token *tok;
24051
24052   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24053   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24054     {
24055       tok = cp_lexer_peek_token (parser->lexer);
24056       error_at (tok->location, "identifier expected after %<@protocol%>");
24057       cp_parser_consume_semicolon_at_end_of_statement (parser);
24058       return;
24059     }
24060
24061   /* See if we have a forward declaration or a definition.  */
24062   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24063
24064   /* Try a forward declaration first.  */
24065   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24066     {
24067       while (true)
24068         {
24069           tree id;
24070           
24071           id = cp_parser_identifier (parser);
24072           if (id == error_mark_node)
24073             break;
24074           
24075           objc_declare_protocol (id, attributes);
24076           
24077           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24078             cp_lexer_consume_token (parser->lexer);
24079           else
24080             break;
24081         }
24082       cp_parser_consume_semicolon_at_end_of_statement (parser);
24083     }
24084
24085   /* Ok, we got a full-fledged definition (or at least should).  */
24086   else
24087     {
24088       proto = cp_parser_identifier (parser);
24089       protorefs = cp_parser_objc_protocol_refs_opt (parser);
24090       objc_start_protocol (proto, protorefs, attributes);
24091       cp_parser_objc_method_prototype_list (parser);
24092     }
24093 }
24094
24095 /* Parse an Objective-C superclass or category.  */
24096
24097 static void
24098 cp_parser_objc_superclass_or_category (cp_parser *parser, 
24099                                        bool iface_p,
24100                                        tree *super,
24101                                        tree *categ, bool *is_class_extension)
24102 {
24103   cp_token *next = cp_lexer_peek_token (parser->lexer);
24104
24105   *super = *categ = NULL_TREE;
24106   *is_class_extension = false;
24107   if (next->type == CPP_COLON)
24108     {
24109       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24110       *super = cp_parser_identifier (parser);
24111     }
24112   else if (next->type == CPP_OPEN_PAREN)
24113     {
24114       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24115
24116       /* If there is no category name, and this is an @interface, we
24117          have a class extension.  */
24118       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24119         {
24120           *categ = NULL_TREE;
24121           *is_class_extension = true;
24122         }
24123       else
24124         *categ = cp_parser_identifier (parser);
24125
24126       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24127     }
24128 }
24129
24130 /* Parse an Objective-C class interface.  */
24131
24132 static void
24133 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24134 {
24135   tree name, super, categ, protos;
24136   bool is_class_extension;
24137
24138   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
24139   name = cp_parser_identifier (parser);
24140   if (name == error_mark_node)
24141     {
24142       /* It's hard to recover because even if valid @interface stuff
24143          is to follow, we can't compile it (or validate it) if we
24144          don't even know which class it refers to.  Let's assume this
24145          was a stray '@interface' token in the stream and skip it.
24146       */
24147       return;
24148     }
24149   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24150                                          &is_class_extension);
24151   protos = cp_parser_objc_protocol_refs_opt (parser);
24152
24153   /* We have either a class or a category on our hands.  */
24154   if (categ || is_class_extension)
24155     objc_start_category_interface (name, categ, protos, attributes);
24156   else
24157     {
24158       objc_start_class_interface (name, super, protos, attributes);
24159       /* Handle instance variable declarations, if any.  */
24160       cp_parser_objc_class_ivars (parser);
24161       objc_continue_interface ();
24162     }
24163
24164   cp_parser_objc_method_prototype_list (parser);
24165 }
24166
24167 /* Parse an Objective-C class implementation.  */
24168
24169 static void
24170 cp_parser_objc_class_implementation (cp_parser* parser)
24171 {
24172   tree name, super, categ;
24173   bool is_class_extension;
24174
24175   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
24176   name = cp_parser_identifier (parser);
24177   if (name == error_mark_node)
24178     {
24179       /* It's hard to recover because even if valid @implementation
24180          stuff is to follow, we can't compile it (or validate it) if
24181          we don't even know which class it refers to.  Let's assume
24182          this was a stray '@implementation' token in the stream and
24183          skip it.
24184       */
24185       return;
24186     }
24187   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24188                                          &is_class_extension);
24189
24190   /* We have either a class or a category on our hands.  */
24191   if (categ)
24192     objc_start_category_implementation (name, categ);
24193   else
24194     {
24195       objc_start_class_implementation (name, super);
24196       /* Handle instance variable declarations, if any.  */
24197       cp_parser_objc_class_ivars (parser);
24198       objc_continue_implementation ();
24199     }
24200
24201   cp_parser_objc_method_definition_list (parser);
24202 }
24203
24204 /* Consume the @end token and finish off the implementation.  */
24205
24206 static void
24207 cp_parser_objc_end_implementation (cp_parser* parser)
24208 {
24209   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24210   objc_finish_implementation ();
24211 }
24212
24213 /* Parse an Objective-C declaration.  */
24214
24215 static void
24216 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24217 {
24218   /* Try to figure out what kind of declaration is present.  */
24219   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24220
24221   if (attributes)
24222     switch (kwd->keyword)
24223       {
24224         case RID_AT_ALIAS:
24225         case RID_AT_CLASS:
24226         case RID_AT_END:
24227           error_at (kwd->location, "attributes may not be specified before"
24228                     " the %<@%D%> Objective-C++ keyword",
24229                     kwd->u.value);
24230           attributes = NULL;
24231           break;
24232         case RID_AT_IMPLEMENTATION:
24233           warning_at (kwd->location, OPT_Wattributes,
24234                       "prefix attributes are ignored before %<@%D%>",
24235                       kwd->u.value);
24236           attributes = NULL;
24237         default:
24238           break;
24239       }
24240
24241   switch (kwd->keyword)
24242     {
24243     case RID_AT_ALIAS:
24244       cp_parser_objc_alias_declaration (parser);
24245       break;
24246     case RID_AT_CLASS:
24247       cp_parser_objc_class_declaration (parser);
24248       break;
24249     case RID_AT_PROTOCOL:
24250       cp_parser_objc_protocol_declaration (parser, attributes);
24251       break;
24252     case RID_AT_INTERFACE:
24253       cp_parser_objc_class_interface (parser, attributes);
24254       break;
24255     case RID_AT_IMPLEMENTATION:
24256       cp_parser_objc_class_implementation (parser);
24257       break;
24258     case RID_AT_END:
24259       cp_parser_objc_end_implementation (parser);
24260       break;
24261     default:
24262       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24263                 kwd->u.value);
24264       cp_parser_skip_to_end_of_block_or_statement (parser);
24265     }
24266 }
24267
24268 /* Parse an Objective-C try-catch-finally statement.
24269
24270    objc-try-catch-finally-stmt:
24271      @try compound-statement objc-catch-clause-seq [opt]
24272        objc-finally-clause [opt]
24273
24274    objc-catch-clause-seq:
24275      objc-catch-clause objc-catch-clause-seq [opt]
24276
24277    objc-catch-clause:
24278      @catch ( objc-exception-declaration ) compound-statement
24279
24280    objc-finally-clause:
24281      @finally compound-statement
24282
24283    objc-exception-declaration:
24284      parameter-declaration
24285      '...'
24286
24287    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24288
24289    Returns NULL_TREE.
24290
24291    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24292    for C.  Keep them in sync.  */   
24293
24294 static tree
24295 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24296 {
24297   location_t location;
24298   tree stmt;
24299
24300   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24301   location = cp_lexer_peek_token (parser->lexer)->location;
24302   objc_maybe_warn_exceptions (location);
24303   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24304      node, lest it get absorbed into the surrounding block.  */
24305   stmt = push_stmt_list ();
24306   cp_parser_compound_statement (parser, NULL, false, false);
24307   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24308
24309   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24310     {
24311       cp_parameter_declarator *parm;
24312       tree parameter_declaration = error_mark_node;
24313       bool seen_open_paren = false;
24314
24315       cp_lexer_consume_token (parser->lexer);
24316       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24317         seen_open_paren = true;
24318       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24319         {
24320           /* We have "@catch (...)" (where the '...' are literally
24321              what is in the code).  Skip the '...'.
24322              parameter_declaration is set to NULL_TREE, and
24323              objc_being_catch_clauses() knows that that means
24324              '...'.  */
24325           cp_lexer_consume_token (parser->lexer);
24326           parameter_declaration = NULL_TREE;
24327         }
24328       else
24329         {
24330           /* We have "@catch (NSException *exception)" or something
24331              like that.  Parse the parameter declaration.  */
24332           parm = cp_parser_parameter_declaration (parser, false, NULL);
24333           if (parm == NULL)
24334             parameter_declaration = error_mark_node;
24335           else
24336             parameter_declaration = grokdeclarator (parm->declarator,
24337                                                     &parm->decl_specifiers,
24338                                                     PARM, /*initialized=*/0,
24339                                                     /*attrlist=*/NULL);
24340         }
24341       if (seen_open_paren)
24342         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24343       else
24344         {
24345           /* If there was no open parenthesis, we are recovering from
24346              an error, and we are trying to figure out what mistake
24347              the user has made.  */
24348
24349           /* If there is an immediate closing parenthesis, the user
24350              probably forgot the opening one (ie, they typed "@catch
24351              NSException *e)".  Parse the closing parenthesis and keep
24352              going.  */
24353           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24354             cp_lexer_consume_token (parser->lexer);
24355           
24356           /* If these is no immediate closing parenthesis, the user
24357              probably doesn't know that parenthesis are required at
24358              all (ie, they typed "@catch NSException *e").  So, just
24359              forget about the closing parenthesis and keep going.  */
24360         }
24361       objc_begin_catch_clause (parameter_declaration);
24362       cp_parser_compound_statement (parser, NULL, false, false);
24363       objc_finish_catch_clause ();
24364     }
24365   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24366     {
24367       cp_lexer_consume_token (parser->lexer);
24368       location = cp_lexer_peek_token (parser->lexer)->location;
24369       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24370          node, lest it get absorbed into the surrounding block.  */
24371       stmt = push_stmt_list ();
24372       cp_parser_compound_statement (parser, NULL, false, false);
24373       objc_build_finally_clause (location, pop_stmt_list (stmt));
24374     }
24375
24376   return objc_finish_try_stmt ();
24377 }
24378
24379 /* Parse an Objective-C synchronized statement.
24380
24381    objc-synchronized-stmt:
24382      @synchronized ( expression ) compound-statement
24383
24384    Returns NULL_TREE.  */
24385
24386 static tree
24387 cp_parser_objc_synchronized_statement (cp_parser *parser)
24388 {
24389   location_t location;
24390   tree lock, stmt;
24391
24392   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24393
24394   location = cp_lexer_peek_token (parser->lexer)->location;
24395   objc_maybe_warn_exceptions (location);
24396   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24397   lock = cp_parser_expression (parser, false, NULL);
24398   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24399
24400   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24401      node, lest it get absorbed into the surrounding block.  */
24402   stmt = push_stmt_list ();
24403   cp_parser_compound_statement (parser, NULL, false, false);
24404
24405   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24406 }
24407
24408 /* Parse an Objective-C throw statement.
24409
24410    objc-throw-stmt:
24411      @throw assignment-expression [opt] ;
24412
24413    Returns a constructed '@throw' statement.  */
24414
24415 static tree
24416 cp_parser_objc_throw_statement (cp_parser *parser)
24417 {
24418   tree expr = NULL_TREE;
24419   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24420
24421   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24422
24423   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24424     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24425
24426   cp_parser_consume_semicolon_at_end_of_statement (parser);
24427
24428   return objc_build_throw_stmt (loc, expr);
24429 }
24430
24431 /* Parse an Objective-C statement.  */
24432
24433 static tree
24434 cp_parser_objc_statement (cp_parser * parser)
24435 {
24436   /* Try to figure out what kind of declaration is present.  */
24437   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24438
24439   switch (kwd->keyword)
24440     {
24441     case RID_AT_TRY:
24442       return cp_parser_objc_try_catch_finally_statement (parser);
24443     case RID_AT_SYNCHRONIZED:
24444       return cp_parser_objc_synchronized_statement (parser);
24445     case RID_AT_THROW:
24446       return cp_parser_objc_throw_statement (parser);
24447     default:
24448       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24449                kwd->u.value);
24450       cp_parser_skip_to_end_of_block_or_statement (parser);
24451     }
24452
24453   return error_mark_node;
24454 }
24455
24456 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24457    look ahead to see if an objc keyword follows the attributes.  This
24458    is to detect the use of prefix attributes on ObjC @interface and 
24459    @protocol.  */
24460
24461 static bool
24462 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24463 {
24464   cp_lexer_save_tokens (parser->lexer);
24465   *attrib = cp_parser_attributes_opt (parser);
24466   gcc_assert (*attrib);
24467   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24468     {
24469       cp_lexer_commit_tokens (parser->lexer);
24470       return true;
24471     }
24472   cp_lexer_rollback_tokens (parser->lexer);
24473   return false;  
24474 }
24475
24476 /* This routine is a minimal replacement for
24477    c_parser_struct_declaration () used when parsing the list of
24478    types/names or ObjC++ properties.  For example, when parsing the
24479    code
24480
24481    @property (readonly) int a, b, c;
24482
24483    this function is responsible for parsing "int a, int b, int c" and
24484    returning the declarations as CHAIN of DECLs.
24485
24486    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24487    similar parsing.  */
24488 static tree
24489 cp_parser_objc_struct_declaration (cp_parser *parser)
24490 {
24491   tree decls = NULL_TREE;
24492   cp_decl_specifier_seq declspecs;
24493   int decl_class_or_enum_p;
24494   tree prefix_attributes;
24495
24496   cp_parser_decl_specifier_seq (parser,
24497                                 CP_PARSER_FLAGS_NONE,
24498                                 &declspecs,
24499                                 &decl_class_or_enum_p);
24500
24501   if (declspecs.type == error_mark_node)
24502     return error_mark_node;
24503
24504   /* auto, register, static, extern, mutable.  */
24505   if (declspecs.storage_class != sc_none)
24506     {
24507       cp_parser_error (parser, "invalid type for property");
24508       declspecs.storage_class = sc_none;
24509     }
24510   
24511   /* __thread.  */
24512   if (declspecs.specs[(int) ds_thread])
24513     {
24514       cp_parser_error (parser, "invalid type for property");
24515       declspecs.specs[(int) ds_thread] = 0;
24516     }
24517   
24518   /* typedef.  */
24519   if (declspecs.specs[(int) ds_typedef])
24520     {
24521       cp_parser_error (parser, "invalid type for property");
24522       declspecs.specs[(int) ds_typedef] = 0;
24523     }
24524
24525   prefix_attributes = declspecs.attributes;
24526   declspecs.attributes = NULL_TREE;
24527
24528   /* Keep going until we hit the `;' at the end of the declaration. */
24529   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24530     {
24531       tree attributes, first_attribute, decl;
24532       cp_declarator *declarator;
24533       cp_token *token;
24534
24535       /* Parse the declarator.  */
24536       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24537                                          NULL, NULL, false);
24538
24539       /* Look for attributes that apply to the ivar.  */
24540       attributes = cp_parser_attributes_opt (parser);
24541       /* Remember which attributes are prefix attributes and
24542          which are not.  */
24543       first_attribute = attributes;
24544       /* Combine the attributes.  */
24545       attributes = chainon (prefix_attributes, attributes);
24546       
24547       decl = grokfield (declarator, &declspecs,
24548                         NULL_TREE, /*init_const_expr_p=*/false,
24549                         NULL_TREE, attributes);
24550
24551       if (decl == error_mark_node || decl == NULL_TREE)
24552         return error_mark_node;
24553       
24554       /* Reset PREFIX_ATTRIBUTES.  */
24555       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24556         attributes = TREE_CHAIN (attributes);
24557       if (attributes)
24558         TREE_CHAIN (attributes) = NULL_TREE;
24559
24560       DECL_CHAIN (decl) = decls;
24561       decls = decl;
24562
24563       token = cp_lexer_peek_token (parser->lexer);
24564       if (token->type == CPP_COMMA)
24565         {
24566           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24567           continue;
24568         }
24569       else
24570         break;
24571     }
24572   return decls;
24573 }
24574
24575 /* Parse an Objective-C @property declaration.  The syntax is:
24576
24577    objc-property-declaration:
24578      '@property' objc-property-attributes[opt] struct-declaration ;
24579
24580    objc-property-attributes:
24581     '(' objc-property-attribute-list ')'
24582
24583    objc-property-attribute-list:
24584      objc-property-attribute
24585      objc-property-attribute-list, objc-property-attribute
24586
24587    objc-property-attribute
24588      'getter' = identifier
24589      'setter' = identifier
24590      'readonly'
24591      'readwrite'
24592      'assign'
24593      'retain'
24594      'copy'
24595      'nonatomic'
24596
24597   For example:
24598     @property NSString *name;
24599     @property (readonly) id object;
24600     @property (retain, nonatomic, getter=getTheName) id name;
24601     @property int a, b, c;
24602
24603    PS: This function is identical to
24604    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24605 static void 
24606 cp_parser_objc_at_property_declaration (cp_parser *parser)
24607 {
24608   /* The following variables hold the attributes of the properties as
24609      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24610      seen.  When we see an attribute, we set them to 'true' (if they
24611      are boolean properties) or to the identifier (if they have an
24612      argument, ie, for getter and setter).  Note that here we only
24613      parse the list of attributes, check the syntax and accumulate the
24614      attributes that we find.  objc_add_property_declaration() will
24615      then process the information.  */
24616   bool property_assign = false;
24617   bool property_copy = false;
24618   tree property_getter_ident = NULL_TREE;
24619   bool property_nonatomic = false;
24620   bool property_readonly = false;
24621   bool property_readwrite = false;
24622   bool property_retain = false;
24623   tree property_setter_ident = NULL_TREE;
24624
24625   /* 'properties' is the list of properties that we read.  Usually a
24626      single one, but maybe more (eg, in "@property int a, b, c;" there
24627      are three).  */
24628   tree properties;
24629   location_t loc;
24630
24631   loc = cp_lexer_peek_token (parser->lexer)->location;
24632
24633   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24634
24635   /* Parse the optional attribute list...  */
24636   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24637     {
24638       /* Eat the '('.  */
24639       cp_lexer_consume_token (parser->lexer);
24640
24641       while (true)
24642         {
24643           bool syntax_error = false;
24644           cp_token *token = cp_lexer_peek_token (parser->lexer);
24645           enum rid keyword;
24646
24647           if (token->type != CPP_NAME)
24648             {
24649               cp_parser_error (parser, "expected identifier");
24650               break;
24651             }
24652           keyword = C_RID_CODE (token->u.value);
24653           cp_lexer_consume_token (parser->lexer);
24654           switch (keyword)
24655             {
24656             case RID_ASSIGN:    property_assign = true;    break;
24657             case RID_COPY:      property_copy = true;      break;
24658             case RID_NONATOMIC: property_nonatomic = true; break;
24659             case RID_READONLY:  property_readonly = true;  break;
24660             case RID_READWRITE: property_readwrite = true; break;
24661             case RID_RETAIN:    property_retain = true;    break;
24662
24663             case RID_GETTER:
24664             case RID_SETTER:
24665               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24666                 {
24667                   if (keyword == RID_GETTER)
24668                     cp_parser_error (parser,
24669                                      "missing %<=%> (after %<getter%> attribute)");
24670                   else
24671                     cp_parser_error (parser,
24672                                      "missing %<=%> (after %<setter%> attribute)");
24673                   syntax_error = true;
24674                   break;
24675                 }
24676               cp_lexer_consume_token (parser->lexer); /* eat the = */
24677               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24678                 {
24679                   cp_parser_error (parser, "expected identifier");
24680                   syntax_error = true;
24681                   break;
24682                 }
24683               if (keyword == RID_SETTER)
24684                 {
24685                   if (property_setter_ident != NULL_TREE)
24686                     {
24687                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24688                       cp_lexer_consume_token (parser->lexer);
24689                     }
24690                   else
24691                     property_setter_ident = cp_parser_objc_selector (parser);
24692                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24693                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24694                   else
24695                     cp_lexer_consume_token (parser->lexer);
24696                 }
24697               else
24698                 {
24699                   if (property_getter_ident != NULL_TREE)
24700                     {
24701                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24702                       cp_lexer_consume_token (parser->lexer);
24703                     }
24704                   else
24705                     property_getter_ident = cp_parser_objc_selector (parser);
24706                 }
24707               break;
24708             default:
24709               cp_parser_error (parser, "unknown property attribute");
24710               syntax_error = true;
24711               break;
24712             }
24713
24714           if (syntax_error)
24715             break;
24716
24717           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24718             cp_lexer_consume_token (parser->lexer);
24719           else
24720             break;
24721         }
24722
24723       /* FIXME: "@property (setter, assign);" will generate a spurious
24724          "error: expected â€˜)’ before â€˜,’ token".  This is because
24725          cp_parser_require, unlike the C counterpart, will produce an
24726          error even if we are in error recovery.  */
24727       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24728         {
24729           cp_parser_skip_to_closing_parenthesis (parser,
24730                                                  /*recovering=*/true,
24731                                                  /*or_comma=*/false,
24732                                                  /*consume_paren=*/true);
24733         }
24734     }
24735
24736   /* ... and the property declaration(s).  */
24737   properties = cp_parser_objc_struct_declaration (parser);
24738
24739   if (properties == error_mark_node)
24740     {
24741       cp_parser_skip_to_end_of_statement (parser);
24742       /* If the next token is now a `;', consume it.  */
24743       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24744         cp_lexer_consume_token (parser->lexer);
24745       return;
24746     }
24747
24748   if (properties == NULL_TREE)
24749     cp_parser_error (parser, "expected identifier");
24750   else
24751     {
24752       /* Comma-separated properties are chained together in
24753          reverse order; add them one by one.  */
24754       properties = nreverse (properties);
24755       
24756       for (; properties; properties = TREE_CHAIN (properties))
24757         objc_add_property_declaration (loc, copy_node (properties),
24758                                        property_readonly, property_readwrite,
24759                                        property_assign, property_retain,
24760                                        property_copy, property_nonatomic,
24761                                        property_getter_ident, property_setter_ident);
24762     }
24763   
24764   cp_parser_consume_semicolon_at_end_of_statement (parser);
24765 }
24766
24767 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24768
24769    objc-synthesize-declaration:
24770      @synthesize objc-synthesize-identifier-list ;
24771
24772    objc-synthesize-identifier-list:
24773      objc-synthesize-identifier
24774      objc-synthesize-identifier-list, objc-synthesize-identifier
24775
24776    objc-synthesize-identifier
24777      identifier
24778      identifier = identifier
24779
24780   For example:
24781     @synthesize MyProperty;
24782     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24783
24784   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24785   for C.  Keep them in sync.
24786 */
24787 static void 
24788 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24789 {
24790   tree list = NULL_TREE;
24791   location_t loc;
24792   loc = cp_lexer_peek_token (parser->lexer)->location;
24793
24794   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24795   while (true)
24796     {
24797       tree property, ivar;
24798       property = cp_parser_identifier (parser);
24799       if (property == error_mark_node)
24800         {
24801           cp_parser_consume_semicolon_at_end_of_statement (parser);
24802           return;
24803         }
24804       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24805         {
24806           cp_lexer_consume_token (parser->lexer);
24807           ivar = cp_parser_identifier (parser);
24808           if (ivar == error_mark_node)
24809             {
24810               cp_parser_consume_semicolon_at_end_of_statement (parser);
24811               return;
24812             }
24813         }
24814       else
24815         ivar = NULL_TREE;
24816       list = chainon (list, build_tree_list (ivar, property));
24817       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24818         cp_lexer_consume_token (parser->lexer);
24819       else
24820         break;
24821     }
24822   cp_parser_consume_semicolon_at_end_of_statement (parser);
24823   objc_add_synthesize_declaration (loc, list);
24824 }
24825
24826 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24827
24828    objc-dynamic-declaration:
24829      @dynamic identifier-list ;
24830
24831    For example:
24832      @dynamic MyProperty;
24833      @dynamic MyProperty, AnotherProperty;
24834
24835   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24836   for C.  Keep them in sync.
24837 */
24838 static void 
24839 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24840 {
24841   tree list = NULL_TREE;
24842   location_t loc;
24843   loc = cp_lexer_peek_token (parser->lexer)->location;
24844
24845   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24846   while (true)
24847     {
24848       tree property;
24849       property = cp_parser_identifier (parser);
24850       if (property == error_mark_node)
24851         {
24852           cp_parser_consume_semicolon_at_end_of_statement (parser);
24853           return;
24854         }
24855       list = chainon (list, build_tree_list (NULL, property));
24856       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24857         cp_lexer_consume_token (parser->lexer);
24858       else
24859         break;
24860     }
24861   cp_parser_consume_semicolon_at_end_of_statement (parser);
24862   objc_add_dynamic_declaration (loc, list);
24863 }
24864
24865 \f
24866 /* OpenMP 2.5 parsing routines.  */
24867
24868 /* Returns name of the next clause.
24869    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24870    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24871    returned and the token is consumed.  */
24872
24873 static pragma_omp_clause
24874 cp_parser_omp_clause_name (cp_parser *parser)
24875 {
24876   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24877
24878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24879     result = PRAGMA_OMP_CLAUSE_IF;
24880   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24881     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24882   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24883     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24884   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24885     {
24886       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24887       const char *p = IDENTIFIER_POINTER (id);
24888
24889       switch (p[0])
24890         {
24891         case 'c':
24892           if (!strcmp ("collapse", p))
24893             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24894           else if (!strcmp ("copyin", p))
24895             result = PRAGMA_OMP_CLAUSE_COPYIN;
24896           else if (!strcmp ("copyprivate", p))
24897             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24898           break;
24899         case 'f':
24900           if (!strcmp ("final", p))
24901             result = PRAGMA_OMP_CLAUSE_FINAL;
24902           else if (!strcmp ("firstprivate", p))
24903             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24904           break;
24905         case 'l':
24906           if (!strcmp ("lastprivate", p))
24907             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24908           break;
24909         case 'm':
24910           if (!strcmp ("mergeable", p))
24911             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24912           break;
24913         case 'n':
24914           if (!strcmp ("nowait", p))
24915             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24916           else if (!strcmp ("num_threads", p))
24917             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24918           break;
24919         case 'o':
24920           if (!strcmp ("ordered", p))
24921             result = PRAGMA_OMP_CLAUSE_ORDERED;
24922           break;
24923         case 'r':
24924           if (!strcmp ("reduction", p))
24925             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24926           break;
24927         case 's':
24928           if (!strcmp ("schedule", p))
24929             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24930           else if (!strcmp ("shared", p))
24931             result = PRAGMA_OMP_CLAUSE_SHARED;
24932           break;
24933         case 'u':
24934           if (!strcmp ("untied", p))
24935             result = PRAGMA_OMP_CLAUSE_UNTIED;
24936           break;
24937         }
24938     }
24939
24940   if (result != PRAGMA_OMP_CLAUSE_NONE)
24941     cp_lexer_consume_token (parser->lexer);
24942
24943   return result;
24944 }
24945
24946 /* Validate that a clause of the given type does not already exist.  */
24947
24948 static void
24949 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24950                            const char *name, location_t location)
24951 {
24952   tree c;
24953
24954   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24955     if (OMP_CLAUSE_CODE (c) == code)
24956       {
24957         error_at (location, "too many %qs clauses", name);
24958         break;
24959       }
24960 }
24961
24962 /* OpenMP 2.5:
24963    variable-list:
24964      identifier
24965      variable-list , identifier
24966
24967    In addition, we match a closing parenthesis.  An opening parenthesis
24968    will have been consumed by the caller.
24969
24970    If KIND is nonzero, create the appropriate node and install the decl
24971    in OMP_CLAUSE_DECL and add the node to the head of the list.
24972
24973    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24974    return the list created.  */
24975
24976 static tree
24977 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24978                                 tree list)
24979 {
24980   cp_token *token;
24981   while (1)
24982     {
24983       tree name, decl;
24984
24985       token = cp_lexer_peek_token (parser->lexer);
24986       name = cp_parser_id_expression (parser, /*template_p=*/false,
24987                                       /*check_dependency_p=*/true,
24988                                       /*template_p=*/NULL,
24989                                       /*declarator_p=*/false,
24990                                       /*optional_p=*/false);
24991       if (name == error_mark_node)
24992         goto skip_comma;
24993
24994       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24995       if (decl == error_mark_node)
24996         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24997                                      token->location);
24998       else if (kind != 0)
24999         {
25000           tree u = build_omp_clause (token->location, kind);
25001           OMP_CLAUSE_DECL (u) = decl;
25002           OMP_CLAUSE_CHAIN (u) = list;
25003           list = u;
25004         }
25005       else
25006         list = tree_cons (decl, NULL_TREE, list);
25007
25008     get_comma:
25009       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
25010         break;
25011       cp_lexer_consume_token (parser->lexer);
25012     }
25013
25014   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25015     {
25016       int ending;
25017
25018       /* Try to resync to an unnested comma.  Copied from
25019          cp_parser_parenthesized_expression_list.  */
25020     skip_comma:
25021       ending = cp_parser_skip_to_closing_parenthesis (parser,
25022                                                       /*recovering=*/true,
25023                                                       /*or_comma=*/true,
25024                                                       /*consume_paren=*/true);
25025       if (ending < 0)
25026         goto get_comma;
25027     }
25028
25029   return list;
25030 }
25031
25032 /* Similarly, but expect leading and trailing parenthesis.  This is a very
25033    common case for omp clauses.  */
25034
25035 static tree
25036 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25037 {
25038   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25039     return cp_parser_omp_var_list_no_open (parser, kind, list);
25040   return list;
25041 }
25042
25043 /* OpenMP 3.0:
25044    collapse ( constant-expression ) */
25045
25046 static tree
25047 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25048 {
25049   tree c, num;
25050   location_t loc;
25051   HOST_WIDE_INT n;
25052
25053   loc = cp_lexer_peek_token (parser->lexer)->location;
25054   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25055     return list;
25056
25057   num = cp_parser_constant_expression (parser, false, NULL);
25058
25059   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25060     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25061                                            /*or_comma=*/false,
25062                                            /*consume_paren=*/true);
25063
25064   if (num == error_mark_node)
25065     return list;
25066   num = fold_non_dependent_expr (num);
25067   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25068       || !host_integerp (num, 0)
25069       || (n = tree_low_cst (num, 0)) <= 0
25070       || (int) n != n)
25071     {
25072       error_at (loc, "collapse argument needs positive constant integer expression");
25073       return list;
25074     }
25075
25076   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25077   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25078   OMP_CLAUSE_CHAIN (c) = list;
25079   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25080
25081   return c;
25082 }
25083
25084 /* OpenMP 2.5:
25085    default ( shared | none ) */
25086
25087 static tree
25088 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25089 {
25090   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25091   tree c;
25092
25093   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25094     return list;
25095   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25096     {
25097       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25098       const char *p = IDENTIFIER_POINTER (id);
25099
25100       switch (p[0])
25101         {
25102         case 'n':
25103           if (strcmp ("none", p) != 0)
25104             goto invalid_kind;
25105           kind = OMP_CLAUSE_DEFAULT_NONE;
25106           break;
25107
25108         case 's':
25109           if (strcmp ("shared", p) != 0)
25110             goto invalid_kind;
25111           kind = OMP_CLAUSE_DEFAULT_SHARED;
25112           break;
25113
25114         default:
25115           goto invalid_kind;
25116         }
25117
25118       cp_lexer_consume_token (parser->lexer);
25119     }
25120   else
25121     {
25122     invalid_kind:
25123       cp_parser_error (parser, "expected %<none%> or %<shared%>");
25124     }
25125
25126   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25127     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25128                                            /*or_comma=*/false,
25129                                            /*consume_paren=*/true);
25130
25131   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25132     return list;
25133
25134   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25135   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25136   OMP_CLAUSE_CHAIN (c) = list;
25137   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25138
25139   return c;
25140 }
25141
25142 /* OpenMP 3.1:
25143    final ( expression ) */
25144
25145 static tree
25146 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25147 {
25148   tree t, c;
25149
25150   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25151     return list;
25152
25153   t = cp_parser_condition (parser);
25154
25155   if (t == error_mark_node
25156       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25157     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25158                                            /*or_comma=*/false,
25159                                            /*consume_paren=*/true);
25160
25161   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25162
25163   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25164   OMP_CLAUSE_FINAL_EXPR (c) = t;
25165   OMP_CLAUSE_CHAIN (c) = list;
25166
25167   return c;
25168 }
25169
25170 /* OpenMP 2.5:
25171    if ( expression ) */
25172
25173 static tree
25174 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25175 {
25176   tree t, c;
25177
25178   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25179     return list;
25180
25181   t = cp_parser_condition (parser);
25182
25183   if (t == error_mark_node
25184       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25185     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25186                                            /*or_comma=*/false,
25187                                            /*consume_paren=*/true);
25188
25189   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25190
25191   c = build_omp_clause (location, OMP_CLAUSE_IF);
25192   OMP_CLAUSE_IF_EXPR (c) = t;
25193   OMP_CLAUSE_CHAIN (c) = list;
25194
25195   return c;
25196 }
25197
25198 /* OpenMP 3.1:
25199    mergeable */
25200
25201 static tree
25202 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25203                                 tree list, location_t location)
25204 {
25205   tree c;
25206
25207   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25208                              location);
25209
25210   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25211   OMP_CLAUSE_CHAIN (c) = list;
25212   return c;
25213 }
25214
25215 /* OpenMP 2.5:
25216    nowait */
25217
25218 static tree
25219 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25220                              tree list, location_t location)
25221 {
25222   tree c;
25223
25224   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25225
25226   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25227   OMP_CLAUSE_CHAIN (c) = list;
25228   return c;
25229 }
25230
25231 /* OpenMP 2.5:
25232    num_threads ( expression ) */
25233
25234 static tree
25235 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25236                                   location_t location)
25237 {
25238   tree t, c;
25239
25240   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25241     return list;
25242
25243   t = cp_parser_expression (parser, false, NULL);
25244
25245   if (t == error_mark_node
25246       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25247     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25248                                            /*or_comma=*/false,
25249                                            /*consume_paren=*/true);
25250
25251   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25252                              "num_threads", location);
25253
25254   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25255   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25256   OMP_CLAUSE_CHAIN (c) = list;
25257
25258   return c;
25259 }
25260
25261 /* OpenMP 2.5:
25262    ordered */
25263
25264 static tree
25265 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25266                               tree list, location_t location)
25267 {
25268   tree c;
25269
25270   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25271                              "ordered", location);
25272
25273   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25274   OMP_CLAUSE_CHAIN (c) = list;
25275   return c;
25276 }
25277
25278 /* OpenMP 2.5:
25279    reduction ( reduction-operator : variable-list )
25280
25281    reduction-operator:
25282      One of: + * - & ^ | && ||
25283
25284    OpenMP 3.1:
25285
25286    reduction-operator:
25287      One of: + * - & ^ | && || min max  */
25288
25289 static tree
25290 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25291 {
25292   enum tree_code code;
25293   tree nlist, c;
25294
25295   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25296     return list;
25297
25298   switch (cp_lexer_peek_token (parser->lexer)->type)
25299     {
25300     case CPP_PLUS:
25301       code = PLUS_EXPR;
25302       break;
25303     case CPP_MULT:
25304       code = MULT_EXPR;
25305       break;
25306     case CPP_MINUS:
25307       code = MINUS_EXPR;
25308       break;
25309     case CPP_AND:
25310       code = BIT_AND_EXPR;
25311       break;
25312     case CPP_XOR:
25313       code = BIT_XOR_EXPR;
25314       break;
25315     case CPP_OR:
25316       code = BIT_IOR_EXPR;
25317       break;
25318     case CPP_AND_AND:
25319       code = TRUTH_ANDIF_EXPR;
25320       break;
25321     case CPP_OR_OR:
25322       code = TRUTH_ORIF_EXPR;
25323       break;
25324     case CPP_NAME:
25325       {
25326         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25327         const char *p = IDENTIFIER_POINTER (id);
25328
25329         if (strcmp (p, "min") == 0)
25330           {
25331             code = MIN_EXPR;
25332             break;
25333           }
25334         if (strcmp (p, "max") == 0)
25335           {
25336             code = MAX_EXPR;
25337             break;
25338           }
25339       }
25340       /* FALLTHROUGH */
25341     default:
25342       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25343                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25344     resync_fail:
25345       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25346                                              /*or_comma=*/false,
25347                                              /*consume_paren=*/true);
25348       return list;
25349     }
25350   cp_lexer_consume_token (parser->lexer);
25351
25352   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25353     goto resync_fail;
25354
25355   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25356   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25357     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25358
25359   return nlist;
25360 }
25361
25362 /* OpenMP 2.5:
25363    schedule ( schedule-kind )
25364    schedule ( schedule-kind , expression )
25365
25366    schedule-kind:
25367      static | dynamic | guided | runtime | auto  */
25368
25369 static tree
25370 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25371 {
25372   tree c, t;
25373
25374   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25375     return list;
25376
25377   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25378
25379   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25380     {
25381       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25382       const char *p = IDENTIFIER_POINTER (id);
25383
25384       switch (p[0])
25385         {
25386         case 'd':
25387           if (strcmp ("dynamic", p) != 0)
25388             goto invalid_kind;
25389           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25390           break;
25391
25392         case 'g':
25393           if (strcmp ("guided", p) != 0)
25394             goto invalid_kind;
25395           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25396           break;
25397
25398         case 'r':
25399           if (strcmp ("runtime", p) != 0)
25400             goto invalid_kind;
25401           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25402           break;
25403
25404         default:
25405           goto invalid_kind;
25406         }
25407     }
25408   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25409     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25410   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25411     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25412   else
25413     goto invalid_kind;
25414   cp_lexer_consume_token (parser->lexer);
25415
25416   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25417     {
25418       cp_token *token;
25419       cp_lexer_consume_token (parser->lexer);
25420
25421       token = cp_lexer_peek_token (parser->lexer);
25422       t = cp_parser_assignment_expression (parser, false, NULL);
25423
25424       if (t == error_mark_node)
25425         goto resync_fail;
25426       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25427         error_at (token->location, "schedule %<runtime%> does not take "
25428                   "a %<chunk_size%> parameter");
25429       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25430         error_at (token->location, "schedule %<auto%> does not take "
25431                   "a %<chunk_size%> parameter");
25432       else
25433         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25434
25435       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25436         goto resync_fail;
25437     }
25438   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25439     goto resync_fail;
25440
25441   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25442   OMP_CLAUSE_CHAIN (c) = list;
25443   return c;
25444
25445  invalid_kind:
25446   cp_parser_error (parser, "invalid schedule kind");
25447  resync_fail:
25448   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25449                                          /*or_comma=*/false,
25450                                          /*consume_paren=*/true);
25451   return list;
25452 }
25453
25454 /* OpenMP 3.0:
25455    untied */
25456
25457 static tree
25458 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25459                              tree list, location_t location)
25460 {
25461   tree c;
25462
25463   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25464
25465   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25466   OMP_CLAUSE_CHAIN (c) = list;
25467   return c;
25468 }
25469
25470 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25471    is a bitmask in MASK.  Return the list of clauses found; the result
25472    of clause default goes in *pdefault.  */
25473
25474 static tree
25475 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25476                            const char *where, cp_token *pragma_tok)
25477 {
25478   tree clauses = NULL;
25479   bool first = true;
25480   cp_token *token = NULL;
25481
25482   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25483     {
25484       pragma_omp_clause c_kind;
25485       const char *c_name;
25486       tree prev = clauses;
25487
25488       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25489         cp_lexer_consume_token (parser->lexer);
25490
25491       token = cp_lexer_peek_token (parser->lexer);
25492       c_kind = cp_parser_omp_clause_name (parser);
25493       first = false;
25494
25495       switch (c_kind)
25496         {
25497         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25498           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25499                                                    token->location);
25500           c_name = "collapse";
25501           break;
25502         case PRAGMA_OMP_CLAUSE_COPYIN:
25503           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25504           c_name = "copyin";
25505           break;
25506         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25507           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25508                                             clauses);
25509           c_name = "copyprivate";
25510           break;
25511         case PRAGMA_OMP_CLAUSE_DEFAULT:
25512           clauses = cp_parser_omp_clause_default (parser, clauses,
25513                                                   token->location);
25514           c_name = "default";
25515           break;
25516         case PRAGMA_OMP_CLAUSE_FINAL:
25517           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25518           c_name = "final";
25519           break;
25520         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25521           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25522                                             clauses);
25523           c_name = "firstprivate";
25524           break;
25525         case PRAGMA_OMP_CLAUSE_IF:
25526           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25527           c_name = "if";
25528           break;
25529         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25530           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25531                                             clauses);
25532           c_name = "lastprivate";
25533           break;
25534         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25535           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25536                                                     token->location);
25537           c_name = "mergeable";
25538           break;
25539         case PRAGMA_OMP_CLAUSE_NOWAIT:
25540           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25541           c_name = "nowait";
25542           break;
25543         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25544           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25545                                                       token->location);
25546           c_name = "num_threads";
25547           break;
25548         case PRAGMA_OMP_CLAUSE_ORDERED:
25549           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25550                                                   token->location);
25551           c_name = "ordered";
25552           break;
25553         case PRAGMA_OMP_CLAUSE_PRIVATE:
25554           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25555                                             clauses);
25556           c_name = "private";
25557           break;
25558         case PRAGMA_OMP_CLAUSE_REDUCTION:
25559           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25560           c_name = "reduction";
25561           break;
25562         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25563           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25564                                                    token->location);
25565           c_name = "schedule";
25566           break;
25567         case PRAGMA_OMP_CLAUSE_SHARED:
25568           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25569                                             clauses);
25570           c_name = "shared";
25571           break;
25572         case PRAGMA_OMP_CLAUSE_UNTIED:
25573           clauses = cp_parser_omp_clause_untied (parser, clauses,
25574                                                  token->location);
25575           c_name = "nowait";
25576           break;
25577         default:
25578           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25579           goto saw_error;
25580         }
25581
25582       if (((mask >> c_kind) & 1) == 0)
25583         {
25584           /* Remove the invalid clause(s) from the list to avoid
25585              confusing the rest of the compiler.  */
25586           clauses = prev;
25587           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25588         }
25589     }
25590  saw_error:
25591   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25592   return finish_omp_clauses (clauses);
25593 }
25594
25595 /* OpenMP 2.5:
25596    structured-block:
25597      statement
25598
25599    In practice, we're also interested in adding the statement to an
25600    outer node.  So it is convenient if we work around the fact that
25601    cp_parser_statement calls add_stmt.  */
25602
25603 static unsigned
25604 cp_parser_begin_omp_structured_block (cp_parser *parser)
25605 {
25606   unsigned save = parser->in_statement;
25607
25608   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25609      This preserves the "not within loop or switch" style error messages
25610      for nonsense cases like
25611         void foo() {
25612         #pragma omp single
25613           break;
25614         }
25615   */
25616   if (parser->in_statement)
25617     parser->in_statement = IN_OMP_BLOCK;
25618
25619   return save;
25620 }
25621
25622 static void
25623 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25624 {
25625   parser->in_statement = save;
25626 }
25627
25628 static tree
25629 cp_parser_omp_structured_block (cp_parser *parser)
25630 {
25631   tree stmt = begin_omp_structured_block ();
25632   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25633
25634   cp_parser_statement (parser, NULL_TREE, false, NULL);
25635
25636   cp_parser_end_omp_structured_block (parser, save);
25637   return finish_omp_structured_block (stmt);
25638 }
25639
25640 /* OpenMP 2.5:
25641    # pragma omp atomic new-line
25642      expression-stmt
25643
25644    expression-stmt:
25645      x binop= expr | x++ | ++x | x-- | --x
25646    binop:
25647      +, *, -, /, &, ^, |, <<, >>
25648
25649   where x is an lvalue expression with scalar type.
25650
25651    OpenMP 3.1:
25652    # pragma omp atomic new-line
25653      update-stmt
25654
25655    # pragma omp atomic read new-line
25656      read-stmt
25657
25658    # pragma omp atomic write new-line
25659      write-stmt
25660
25661    # pragma omp atomic update new-line
25662      update-stmt
25663
25664    # pragma omp atomic capture new-line
25665      capture-stmt
25666
25667    # pragma omp atomic capture new-line
25668      capture-block
25669
25670    read-stmt:
25671      v = x
25672    write-stmt:
25673      x = expr
25674    update-stmt:
25675      expression-stmt | x = x binop expr
25676    capture-stmt:
25677      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25678    capture-block:
25679      { v = x; update-stmt; } | { update-stmt; v = x; }
25680
25681   where x and v are lvalue expressions with scalar type.  */
25682
25683 static void
25684 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25685 {
25686   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25687   tree rhs1 = NULL_TREE, orig_lhs;
25688   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25689   bool structured_block = false;
25690
25691   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25692     {
25693       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25694       const char *p = IDENTIFIER_POINTER (id);
25695
25696       if (!strcmp (p, "read"))
25697         code = OMP_ATOMIC_READ;
25698       else if (!strcmp (p, "write"))
25699         code = NOP_EXPR;
25700       else if (!strcmp (p, "update"))
25701         code = OMP_ATOMIC;
25702       else if (!strcmp (p, "capture"))
25703         code = OMP_ATOMIC_CAPTURE_NEW;
25704       else
25705         p = NULL;
25706       if (p)
25707         cp_lexer_consume_token (parser->lexer);
25708     }
25709   cp_parser_require_pragma_eol (parser, pragma_tok);
25710
25711   switch (code)
25712     {
25713     case OMP_ATOMIC_READ:
25714     case NOP_EXPR: /* atomic write */
25715       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25716                                       /*cast_p=*/false, NULL);
25717       if (v == error_mark_node)
25718         goto saw_error;
25719       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25720         goto saw_error;
25721       if (code == NOP_EXPR)
25722         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25723       else
25724         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25725                                           /*cast_p=*/false, NULL);
25726       if (lhs == error_mark_node)
25727         goto saw_error;
25728       if (code == NOP_EXPR)
25729         {
25730           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25731              opcode.  */
25732           code = OMP_ATOMIC;
25733           rhs = lhs;
25734           lhs = v;
25735           v = NULL_TREE;
25736         }
25737       goto done;
25738     case OMP_ATOMIC_CAPTURE_NEW:
25739       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25740         {
25741           cp_lexer_consume_token (parser->lexer);
25742           structured_block = true;
25743         }
25744       else
25745         {
25746           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25747                                           /*cast_p=*/false, NULL);
25748           if (v == error_mark_node)
25749             goto saw_error;
25750           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25751             goto saw_error;
25752         }
25753     default:
25754       break;
25755     }
25756
25757 restart:
25758   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25759                                     /*cast_p=*/false, NULL);
25760   orig_lhs = lhs;
25761   switch (TREE_CODE (lhs))
25762     {
25763     case ERROR_MARK:
25764       goto saw_error;
25765
25766     case POSTINCREMENT_EXPR:
25767       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25768         code = OMP_ATOMIC_CAPTURE_OLD;
25769       /* FALLTHROUGH */
25770     case PREINCREMENT_EXPR:
25771       lhs = TREE_OPERAND (lhs, 0);
25772       opcode = PLUS_EXPR;
25773       rhs = integer_one_node;
25774       break;
25775
25776     case POSTDECREMENT_EXPR:
25777       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25778         code = OMP_ATOMIC_CAPTURE_OLD;
25779       /* FALLTHROUGH */
25780     case PREDECREMENT_EXPR:
25781       lhs = TREE_OPERAND (lhs, 0);
25782       opcode = MINUS_EXPR;
25783       rhs = integer_one_node;
25784       break;
25785
25786     case COMPOUND_EXPR:
25787       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25788          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25789          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25790          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25791          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25792                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25793             == BOOLEAN_TYPE)
25794        /* Undo effects of boolean_increment for post {in,de}crement.  */
25795        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25796       /* FALLTHRU */
25797     case MODIFY_EXPR:
25798       if (TREE_CODE (lhs) == MODIFY_EXPR
25799          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25800         {
25801           /* Undo effects of boolean_increment.  */
25802           if (integer_onep (TREE_OPERAND (lhs, 1)))
25803             {
25804               /* This is pre or post increment.  */
25805               rhs = TREE_OPERAND (lhs, 1);
25806               lhs = TREE_OPERAND (lhs, 0);
25807               opcode = NOP_EXPR;
25808               if (code == OMP_ATOMIC_CAPTURE_NEW
25809                   && !structured_block
25810                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25811                 code = OMP_ATOMIC_CAPTURE_OLD;
25812               break;
25813             }
25814         }
25815       /* FALLTHRU */
25816     default:
25817       switch (cp_lexer_peek_token (parser->lexer)->type)
25818         {
25819         case CPP_MULT_EQ:
25820           opcode = MULT_EXPR;
25821           break;
25822         case CPP_DIV_EQ:
25823           opcode = TRUNC_DIV_EXPR;
25824           break;
25825         case CPP_PLUS_EQ:
25826           opcode = PLUS_EXPR;
25827           break;
25828         case CPP_MINUS_EQ:
25829           opcode = MINUS_EXPR;
25830           break;
25831         case CPP_LSHIFT_EQ:
25832           opcode = LSHIFT_EXPR;
25833           break;
25834         case CPP_RSHIFT_EQ:
25835           opcode = RSHIFT_EXPR;
25836           break;
25837         case CPP_AND_EQ:
25838           opcode = BIT_AND_EXPR;
25839           break;
25840         case CPP_OR_EQ:
25841           opcode = BIT_IOR_EXPR;
25842           break;
25843         case CPP_XOR_EQ:
25844           opcode = BIT_XOR_EXPR;
25845           break;
25846         case CPP_EQ:
25847           if (structured_block || code == OMP_ATOMIC)
25848             {
25849               enum cp_parser_prec oprec;
25850               cp_token *token;
25851               cp_lexer_consume_token (parser->lexer);
25852               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25853                                                  /*cast_p=*/false, NULL);
25854               if (rhs1 == error_mark_node)
25855                 goto saw_error;
25856               token = cp_lexer_peek_token (parser->lexer);
25857               switch (token->type)
25858                 {
25859                 case CPP_SEMICOLON:
25860                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25861                     {
25862                       code = OMP_ATOMIC_CAPTURE_OLD;
25863                       v = lhs;
25864                       lhs = NULL_TREE;
25865                       lhs1 = rhs1;
25866                       rhs1 = NULL_TREE;
25867                       cp_lexer_consume_token (parser->lexer);
25868                       goto restart;
25869                     }
25870                   cp_parser_error (parser,
25871                                    "invalid form of %<#pragma omp atomic%>");
25872                   goto saw_error;
25873                 case CPP_MULT:
25874                   opcode = MULT_EXPR;
25875                   break;
25876                 case CPP_DIV:
25877                   opcode = TRUNC_DIV_EXPR;
25878                   break;
25879                 case CPP_PLUS:
25880                   opcode = PLUS_EXPR;
25881                   break;
25882                 case CPP_MINUS:
25883                   opcode = MINUS_EXPR;
25884                   break;
25885                 case CPP_LSHIFT:
25886                   opcode = LSHIFT_EXPR;
25887                   break;
25888                 case CPP_RSHIFT:
25889                   opcode = RSHIFT_EXPR;
25890                   break;
25891                 case CPP_AND:
25892                   opcode = BIT_AND_EXPR;
25893                   break;
25894                 case CPP_OR:
25895                   opcode = BIT_IOR_EXPR;
25896                   break;
25897                 case CPP_XOR:
25898                   opcode = BIT_XOR_EXPR;
25899                   break;
25900                 default:
25901                   cp_parser_error (parser,
25902                                    "invalid operator for %<#pragma omp atomic%>");
25903                   goto saw_error;
25904                 }
25905               oprec = TOKEN_PRECEDENCE (token);
25906               gcc_assert (oprec != PREC_NOT_OPERATOR);
25907               if (commutative_tree_code (opcode))
25908                 oprec = (enum cp_parser_prec) (oprec - 1);
25909               cp_lexer_consume_token (parser->lexer);
25910               rhs = cp_parser_binary_expression (parser, false, false,
25911                                                  oprec, NULL);
25912               if (rhs == error_mark_node)
25913                 goto saw_error;
25914               goto stmt_done;
25915             }
25916           /* FALLTHROUGH */
25917         default:
25918           cp_parser_error (parser,
25919                            "invalid operator for %<#pragma omp atomic%>");
25920           goto saw_error;
25921         }
25922       cp_lexer_consume_token (parser->lexer);
25923
25924       rhs = cp_parser_expression (parser, false, NULL);
25925       if (rhs == error_mark_node)
25926         goto saw_error;
25927       break;
25928     }
25929 stmt_done:
25930   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25931     {
25932       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25933         goto saw_error;
25934       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25935                                       /*cast_p=*/false, NULL);
25936       if (v == error_mark_node)
25937         goto saw_error;
25938       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25939         goto saw_error;
25940       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25941                                          /*cast_p=*/false, NULL);
25942       if (lhs1 == error_mark_node)
25943         goto saw_error;
25944     }
25945   if (structured_block)
25946     {
25947       cp_parser_consume_semicolon_at_end_of_statement (parser);
25948       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25949     }
25950 done:
25951   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25952   if (!structured_block)
25953     cp_parser_consume_semicolon_at_end_of_statement (parser);
25954   return;
25955
25956  saw_error:
25957   cp_parser_skip_to_end_of_block_or_statement (parser);
25958   if (structured_block)
25959     {
25960       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25961         cp_lexer_consume_token (parser->lexer);
25962       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25963         {
25964           cp_parser_skip_to_end_of_block_or_statement (parser);
25965           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25966             cp_lexer_consume_token (parser->lexer);
25967         }
25968     }
25969 }
25970
25971
25972 /* OpenMP 2.5:
25973    # pragma omp barrier new-line  */
25974
25975 static void
25976 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25977 {
25978   cp_parser_require_pragma_eol (parser, pragma_tok);
25979   finish_omp_barrier ();
25980 }
25981
25982 /* OpenMP 2.5:
25983    # pragma omp critical [(name)] new-line
25984      structured-block  */
25985
25986 static tree
25987 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25988 {
25989   tree stmt, name = NULL;
25990
25991   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25992     {
25993       cp_lexer_consume_token (parser->lexer);
25994
25995       name = cp_parser_identifier (parser);
25996
25997       if (name == error_mark_node
25998           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25999         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26000                                                /*or_comma=*/false,
26001                                                /*consume_paren=*/true);
26002       if (name == error_mark_node)
26003         name = NULL;
26004     }
26005   cp_parser_require_pragma_eol (parser, pragma_tok);
26006
26007   stmt = cp_parser_omp_structured_block (parser);
26008   return c_finish_omp_critical (input_location, stmt, name);
26009 }
26010
26011 /* OpenMP 2.5:
26012    # pragma omp flush flush-vars[opt] new-line
26013
26014    flush-vars:
26015      ( variable-list ) */
26016
26017 static void
26018 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26019 {
26020   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26021     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26022   cp_parser_require_pragma_eol (parser, pragma_tok);
26023
26024   finish_omp_flush ();
26025 }
26026
26027 /* Helper function, to parse omp for increment expression.  */
26028
26029 static tree
26030 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26031 {
26032   tree cond = cp_parser_binary_expression (parser, false, true,
26033                                            PREC_NOT_OPERATOR, NULL);
26034   if (cond == error_mark_node
26035       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26036     {
26037       cp_parser_skip_to_end_of_statement (parser);
26038       return error_mark_node;
26039     }
26040
26041   switch (TREE_CODE (cond))
26042     {
26043     case GT_EXPR:
26044     case GE_EXPR:
26045     case LT_EXPR:
26046     case LE_EXPR:
26047       break;
26048     default:
26049       return error_mark_node;
26050     }
26051
26052   /* If decl is an iterator, preserve LHS and RHS of the relational
26053      expr until finish_omp_for.  */
26054   if (decl
26055       && (type_dependent_expression_p (decl)
26056           || CLASS_TYPE_P (TREE_TYPE (decl))))
26057     return cond;
26058
26059   return build_x_binary_op (TREE_CODE (cond),
26060                             TREE_OPERAND (cond, 0), ERROR_MARK,
26061                             TREE_OPERAND (cond, 1), ERROR_MARK,
26062                             /*overload=*/NULL, tf_warning_or_error);
26063 }
26064
26065 /* Helper function, to parse omp for increment expression.  */
26066
26067 static tree
26068 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26069 {
26070   cp_token *token = cp_lexer_peek_token (parser->lexer);
26071   enum tree_code op;
26072   tree lhs, rhs;
26073   cp_id_kind idk;
26074   bool decl_first;
26075
26076   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26077     {
26078       op = (token->type == CPP_PLUS_PLUS
26079             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26080       cp_lexer_consume_token (parser->lexer);
26081       lhs = cp_parser_cast_expression (parser, false, false, NULL);
26082       if (lhs != decl)
26083         return error_mark_node;
26084       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26085     }
26086
26087   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26088   if (lhs != decl)
26089     return error_mark_node;
26090
26091   token = cp_lexer_peek_token (parser->lexer);
26092   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26093     {
26094       op = (token->type == CPP_PLUS_PLUS
26095             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26096       cp_lexer_consume_token (parser->lexer);
26097       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26098     }
26099
26100   op = cp_parser_assignment_operator_opt (parser);
26101   if (op == ERROR_MARK)
26102     return error_mark_node;
26103
26104   if (op != NOP_EXPR)
26105     {
26106       rhs = cp_parser_assignment_expression (parser, false, NULL);
26107       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26108       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26109     }
26110
26111   lhs = cp_parser_binary_expression (parser, false, false,
26112                                      PREC_ADDITIVE_EXPRESSION, NULL);
26113   token = cp_lexer_peek_token (parser->lexer);
26114   decl_first = lhs == decl;
26115   if (decl_first)
26116     lhs = NULL_TREE;
26117   if (token->type != CPP_PLUS
26118       && token->type != CPP_MINUS)
26119     return error_mark_node;
26120
26121   do
26122     {
26123       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26124       cp_lexer_consume_token (parser->lexer);
26125       rhs = cp_parser_binary_expression (parser, false, false,
26126                                          PREC_ADDITIVE_EXPRESSION, NULL);
26127       token = cp_lexer_peek_token (parser->lexer);
26128       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26129         {
26130           if (lhs == NULL_TREE)
26131             {
26132               if (op == PLUS_EXPR)
26133                 lhs = rhs;
26134               else
26135                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
26136             }
26137           else
26138             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
26139                                      NULL, tf_warning_or_error);
26140         }
26141     }
26142   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26143
26144   if (!decl_first)
26145     {
26146       if (rhs != decl || op == MINUS_EXPR)
26147         return error_mark_node;
26148       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26149     }
26150   else
26151     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26152
26153   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26154 }
26155
26156 /* Parse the restricted form of the for statement allowed by OpenMP.  */
26157
26158 static tree
26159 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26160 {
26161   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26162   tree real_decl, initv, condv, incrv, declv;
26163   tree this_pre_body, cl;
26164   location_t loc_first;
26165   bool collapse_err = false;
26166   int i, collapse = 1, nbraces = 0;
26167   VEC(tree,gc) *for_block = make_tree_vector ();
26168
26169   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26170     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26171       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26172
26173   gcc_assert (collapse >= 1);
26174
26175   declv = make_tree_vec (collapse);
26176   initv = make_tree_vec (collapse);
26177   condv = make_tree_vec (collapse);
26178   incrv = make_tree_vec (collapse);
26179
26180   loc_first = cp_lexer_peek_token (parser->lexer)->location;
26181
26182   for (i = 0; i < collapse; i++)
26183     {
26184       int bracecount = 0;
26185       bool add_private_clause = false;
26186       location_t loc;
26187
26188       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26189         {
26190           cp_parser_error (parser, "for statement expected");
26191           return NULL;
26192         }
26193       loc = cp_lexer_consume_token (parser->lexer)->location;
26194
26195       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26196         return NULL;
26197
26198       init = decl = real_decl = NULL;
26199       this_pre_body = push_stmt_list ();
26200       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26201         {
26202           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26203
26204              init-expr:
26205                        var = lb
26206                        integer-type var = lb
26207                        random-access-iterator-type var = lb
26208                        pointer-type var = lb
26209           */
26210           cp_decl_specifier_seq type_specifiers;
26211
26212           /* First, try to parse as an initialized declaration.  See
26213              cp_parser_condition, from whence the bulk of this is copied.  */
26214
26215           cp_parser_parse_tentatively (parser);
26216           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26217                                         /*is_trailing_return=*/false,
26218                                         &type_specifiers);
26219           if (cp_parser_parse_definitely (parser))
26220             {
26221               /* If parsing a type specifier seq succeeded, then this
26222                  MUST be a initialized declaration.  */
26223               tree asm_specification, attributes;
26224               cp_declarator *declarator;
26225
26226               declarator = cp_parser_declarator (parser,
26227                                                  CP_PARSER_DECLARATOR_NAMED,
26228                                                  /*ctor_dtor_or_conv_p=*/NULL,
26229                                                  /*parenthesized_p=*/NULL,
26230                                                  /*member_p=*/false);
26231               attributes = cp_parser_attributes_opt (parser);
26232               asm_specification = cp_parser_asm_specification_opt (parser);
26233
26234               if (declarator == cp_error_declarator) 
26235                 cp_parser_skip_to_end_of_statement (parser);
26236
26237               else 
26238                 {
26239                   tree pushed_scope, auto_node;
26240
26241                   decl = start_decl (declarator, &type_specifiers,
26242                                      SD_INITIALIZED, attributes,
26243                                      /*prefix_attributes=*/NULL_TREE,
26244                                      &pushed_scope);
26245
26246                   auto_node = type_uses_auto (TREE_TYPE (decl));
26247                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26248                     {
26249                       if (cp_lexer_next_token_is (parser->lexer, 
26250                                                   CPP_OPEN_PAREN))
26251                         error ("parenthesized initialization is not allowed in "
26252                                "OpenMP %<for%> loop");
26253                       else
26254                         /* Trigger an error.  */
26255                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26256
26257                       init = error_mark_node;
26258                       cp_parser_skip_to_end_of_statement (parser);
26259                     }
26260                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26261                            || type_dependent_expression_p (decl)
26262                            || auto_node)
26263                     {
26264                       bool is_direct_init, is_non_constant_init;
26265
26266                       init = cp_parser_initializer (parser,
26267                                                     &is_direct_init,
26268                                                     &is_non_constant_init);
26269
26270                       if (auto_node)
26271                         {
26272                           TREE_TYPE (decl)
26273                             = do_auto_deduction (TREE_TYPE (decl), init,
26274                                                  auto_node);
26275
26276                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26277                               && !type_dependent_expression_p (decl))
26278                             goto non_class;
26279                         }
26280                       
26281                       cp_finish_decl (decl, init, !is_non_constant_init,
26282                                       asm_specification,
26283                                       LOOKUP_ONLYCONVERTING);
26284                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26285                         {
26286                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26287                           init = NULL_TREE;
26288                         }
26289                       else
26290                         init = pop_stmt_list (this_pre_body);
26291                       this_pre_body = NULL_TREE;
26292                     }
26293                   else
26294                     {
26295                       /* Consume '='.  */
26296                       cp_lexer_consume_token (parser->lexer);
26297                       init = cp_parser_assignment_expression (parser, false, NULL);
26298
26299                     non_class:
26300                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26301                         init = error_mark_node;
26302                       else
26303                         cp_finish_decl (decl, NULL_TREE,
26304                                         /*init_const_expr_p=*/false,
26305                                         asm_specification,
26306                                         LOOKUP_ONLYCONVERTING);
26307                     }
26308
26309                   if (pushed_scope)
26310                     pop_scope (pushed_scope);
26311                 }
26312             }
26313           else 
26314             {
26315               cp_id_kind idk;
26316               /* If parsing a type specifier sequence failed, then
26317                  this MUST be a simple expression.  */
26318               cp_parser_parse_tentatively (parser);
26319               decl = cp_parser_primary_expression (parser, false, false,
26320                                                    false, &idk);
26321               if (!cp_parser_error_occurred (parser)
26322                   && decl
26323                   && DECL_P (decl)
26324                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26325                 {
26326                   tree rhs;
26327
26328                   cp_parser_parse_definitely (parser);
26329                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26330                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26331                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26332                                                          rhs,
26333                                                          tf_warning_or_error));
26334                   add_private_clause = true;
26335                 }
26336               else
26337                 {
26338                   decl = NULL;
26339                   cp_parser_abort_tentative_parse (parser);
26340                   init = cp_parser_expression (parser, false, NULL);
26341                   if (init)
26342                     {
26343                       if (TREE_CODE (init) == MODIFY_EXPR
26344                           || TREE_CODE (init) == MODOP_EXPR)
26345                         real_decl = TREE_OPERAND (init, 0);
26346                     }
26347                 }
26348             }
26349         }
26350       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26351       if (this_pre_body)
26352         {
26353           this_pre_body = pop_stmt_list (this_pre_body);
26354           if (pre_body)
26355             {
26356               tree t = pre_body;
26357               pre_body = push_stmt_list ();
26358               add_stmt (t);
26359               add_stmt (this_pre_body);
26360               pre_body = pop_stmt_list (pre_body);
26361             }
26362           else
26363             pre_body = this_pre_body;
26364         }
26365
26366       if (decl)
26367         real_decl = decl;
26368       if (par_clauses != NULL && real_decl != NULL_TREE)
26369         {
26370           tree *c;
26371           for (c = par_clauses; *c ; )
26372             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26373                 && OMP_CLAUSE_DECL (*c) == real_decl)
26374               {
26375                 error_at (loc, "iteration variable %qD"
26376                           " should not be firstprivate", real_decl);
26377                 *c = OMP_CLAUSE_CHAIN (*c);
26378               }
26379             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26380                      && OMP_CLAUSE_DECL (*c) == real_decl)
26381               {
26382                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26383                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26384                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26385                 OMP_CLAUSE_DECL (l) = real_decl;
26386                 OMP_CLAUSE_CHAIN (l) = clauses;
26387                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26388                 clauses = l;
26389                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26390                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26391                 add_private_clause = false;
26392               }
26393             else
26394               {
26395                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26396                     && OMP_CLAUSE_DECL (*c) == real_decl)
26397                   add_private_clause = false;
26398                 c = &OMP_CLAUSE_CHAIN (*c);
26399               }
26400         }
26401
26402       if (add_private_clause)
26403         {
26404           tree c;
26405           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26406             {
26407               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26408                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26409                   && OMP_CLAUSE_DECL (c) == decl)
26410                 break;
26411               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26412                        && OMP_CLAUSE_DECL (c) == decl)
26413                 error_at (loc, "iteration variable %qD "
26414                           "should not be firstprivate",
26415                           decl);
26416               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26417                        && OMP_CLAUSE_DECL (c) == decl)
26418                 error_at (loc, "iteration variable %qD should not be reduction",
26419                           decl);
26420             }
26421           if (c == NULL)
26422             {
26423               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26424               OMP_CLAUSE_DECL (c) = decl;
26425               c = finish_omp_clauses (c);
26426               if (c)
26427                 {
26428                   OMP_CLAUSE_CHAIN (c) = clauses;
26429                   clauses = c;
26430                 }
26431             }
26432         }
26433
26434       cond = NULL;
26435       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26436         cond = cp_parser_omp_for_cond (parser, decl);
26437       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26438
26439       incr = NULL;
26440       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26441         {
26442           /* If decl is an iterator, preserve the operator on decl
26443              until finish_omp_for.  */
26444           if (real_decl
26445               && ((processing_template_decl
26446                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26447                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26448             incr = cp_parser_omp_for_incr (parser, real_decl);
26449           else
26450             incr = cp_parser_expression (parser, false, NULL);
26451         }
26452
26453       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26454         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26455                                                /*or_comma=*/false,
26456                                                /*consume_paren=*/true);
26457
26458       TREE_VEC_ELT (declv, i) = decl;
26459       TREE_VEC_ELT (initv, i) = init;
26460       TREE_VEC_ELT (condv, i) = cond;
26461       TREE_VEC_ELT (incrv, i) = incr;
26462
26463       if (i == collapse - 1)
26464         break;
26465
26466       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26467          in between the collapsed for loops to be still considered perfectly
26468          nested.  Hopefully the final version clarifies this.
26469          For now handle (multiple) {'s and empty statements.  */
26470       cp_parser_parse_tentatively (parser);
26471       do
26472         {
26473           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26474             break;
26475           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26476             {
26477               cp_lexer_consume_token (parser->lexer);
26478               bracecount++;
26479             }
26480           else if (bracecount
26481                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26482             cp_lexer_consume_token (parser->lexer);
26483           else
26484             {
26485               loc = cp_lexer_peek_token (parser->lexer)->location;
26486               error_at (loc, "not enough collapsed for loops");
26487               collapse_err = true;
26488               cp_parser_abort_tentative_parse (parser);
26489               declv = NULL_TREE;
26490               break;
26491             }
26492         }
26493       while (1);
26494
26495       if (declv)
26496         {
26497           cp_parser_parse_definitely (parser);
26498           nbraces += bracecount;
26499         }
26500     }
26501
26502   /* Note that we saved the original contents of this flag when we entered
26503      the structured block, and so we don't need to re-save it here.  */
26504   parser->in_statement = IN_OMP_FOR;
26505
26506   /* Note that the grammar doesn't call for a structured block here,
26507      though the loop as a whole is a structured block.  */
26508   body = push_stmt_list ();
26509   cp_parser_statement (parser, NULL_TREE, false, NULL);
26510   body = pop_stmt_list (body);
26511
26512   if (declv == NULL_TREE)
26513     ret = NULL_TREE;
26514   else
26515     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26516                           pre_body, clauses);
26517
26518   while (nbraces)
26519     {
26520       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26521         {
26522           cp_lexer_consume_token (parser->lexer);
26523           nbraces--;
26524         }
26525       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26526         cp_lexer_consume_token (parser->lexer);
26527       else
26528         {
26529           if (!collapse_err)
26530             {
26531               error_at (cp_lexer_peek_token (parser->lexer)->location,
26532                         "collapsed loops not perfectly nested");
26533             }
26534           collapse_err = true;
26535           cp_parser_statement_seq_opt (parser, NULL);
26536           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26537             break;
26538         }
26539     }
26540
26541   while (!VEC_empty (tree, for_block))
26542     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26543   release_tree_vector (for_block);
26544
26545   return ret;
26546 }
26547
26548 /* OpenMP 2.5:
26549    #pragma omp for for-clause[optseq] new-line
26550      for-loop  */
26551
26552 #define OMP_FOR_CLAUSE_MASK                             \
26553         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26554         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26555         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26556         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26557         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26558         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26559         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26560         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26561
26562 static tree
26563 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26564 {
26565   tree clauses, sb, ret;
26566   unsigned int save;
26567
26568   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26569                                        "#pragma omp for", pragma_tok);
26570
26571   sb = begin_omp_structured_block ();
26572   save = cp_parser_begin_omp_structured_block (parser);
26573
26574   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26575
26576   cp_parser_end_omp_structured_block (parser, save);
26577   add_stmt (finish_omp_structured_block (sb));
26578
26579   return ret;
26580 }
26581
26582 /* OpenMP 2.5:
26583    # pragma omp master new-line
26584      structured-block  */
26585
26586 static tree
26587 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26588 {
26589   cp_parser_require_pragma_eol (parser, pragma_tok);
26590   return c_finish_omp_master (input_location,
26591                               cp_parser_omp_structured_block (parser));
26592 }
26593
26594 /* OpenMP 2.5:
26595    # pragma omp ordered new-line
26596      structured-block  */
26597
26598 static tree
26599 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26600 {
26601   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26602   cp_parser_require_pragma_eol (parser, pragma_tok);
26603   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26604 }
26605
26606 /* OpenMP 2.5:
26607
26608    section-scope:
26609      { section-sequence }
26610
26611    section-sequence:
26612      section-directive[opt] structured-block
26613      section-sequence section-directive structured-block  */
26614
26615 static tree
26616 cp_parser_omp_sections_scope (cp_parser *parser)
26617 {
26618   tree stmt, substmt;
26619   bool error_suppress = false;
26620   cp_token *tok;
26621
26622   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26623     return NULL_TREE;
26624
26625   stmt = push_stmt_list ();
26626
26627   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26628     {
26629       unsigned save;
26630
26631       substmt = begin_omp_structured_block ();
26632       save = cp_parser_begin_omp_structured_block (parser);
26633
26634       while (1)
26635         {
26636           cp_parser_statement (parser, NULL_TREE, false, NULL);
26637
26638           tok = cp_lexer_peek_token (parser->lexer);
26639           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26640             break;
26641           if (tok->type == CPP_CLOSE_BRACE)
26642             break;
26643           if (tok->type == CPP_EOF)
26644             break;
26645         }
26646
26647       cp_parser_end_omp_structured_block (parser, save);
26648       substmt = finish_omp_structured_block (substmt);
26649       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26650       add_stmt (substmt);
26651     }
26652
26653   while (1)
26654     {
26655       tok = cp_lexer_peek_token (parser->lexer);
26656       if (tok->type == CPP_CLOSE_BRACE)
26657         break;
26658       if (tok->type == CPP_EOF)
26659         break;
26660
26661       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26662         {
26663           cp_lexer_consume_token (parser->lexer);
26664           cp_parser_require_pragma_eol (parser, tok);
26665           error_suppress = false;
26666         }
26667       else if (!error_suppress)
26668         {
26669           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26670           error_suppress = true;
26671         }
26672
26673       substmt = cp_parser_omp_structured_block (parser);
26674       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26675       add_stmt (substmt);
26676     }
26677   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26678
26679   substmt = pop_stmt_list (stmt);
26680
26681   stmt = make_node (OMP_SECTIONS);
26682   TREE_TYPE (stmt) = void_type_node;
26683   OMP_SECTIONS_BODY (stmt) = substmt;
26684
26685   add_stmt (stmt);
26686   return stmt;
26687 }
26688
26689 /* OpenMP 2.5:
26690    # pragma omp sections sections-clause[optseq] newline
26691      sections-scope  */
26692
26693 #define OMP_SECTIONS_CLAUSE_MASK                        \
26694         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26695         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26696         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26697         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26698         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26699
26700 static tree
26701 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26702 {
26703   tree clauses, ret;
26704
26705   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26706                                        "#pragma omp sections", pragma_tok);
26707
26708   ret = cp_parser_omp_sections_scope (parser);
26709   if (ret)
26710     OMP_SECTIONS_CLAUSES (ret) = clauses;
26711
26712   return ret;
26713 }
26714
26715 /* OpenMP 2.5:
26716    # pragma parallel parallel-clause new-line
26717    # pragma parallel for parallel-for-clause new-line
26718    # pragma parallel sections parallel-sections-clause new-line  */
26719
26720 #define OMP_PARALLEL_CLAUSE_MASK                        \
26721         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26722         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26723         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26724         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26725         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26726         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26727         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26728         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26729
26730 static tree
26731 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26732 {
26733   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26734   const char *p_name = "#pragma omp parallel";
26735   tree stmt, clauses, par_clause, ws_clause, block;
26736   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26737   unsigned int save;
26738   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26739
26740   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26741     {
26742       cp_lexer_consume_token (parser->lexer);
26743       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26744       p_name = "#pragma omp parallel for";
26745       mask |= OMP_FOR_CLAUSE_MASK;
26746       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26747     }
26748   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26749     {
26750       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26751       const char *p = IDENTIFIER_POINTER (id);
26752       if (strcmp (p, "sections") == 0)
26753         {
26754           cp_lexer_consume_token (parser->lexer);
26755           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26756           p_name = "#pragma omp parallel sections";
26757           mask |= OMP_SECTIONS_CLAUSE_MASK;
26758           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26759         }
26760     }
26761
26762   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26763   block = begin_omp_parallel ();
26764   save = cp_parser_begin_omp_structured_block (parser);
26765
26766   switch (p_kind)
26767     {
26768     case PRAGMA_OMP_PARALLEL:
26769       cp_parser_statement (parser, NULL_TREE, false, NULL);
26770       par_clause = clauses;
26771       break;
26772
26773     case PRAGMA_OMP_PARALLEL_FOR:
26774       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26775       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26776       break;
26777
26778     case PRAGMA_OMP_PARALLEL_SECTIONS:
26779       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26780       stmt = cp_parser_omp_sections_scope (parser);
26781       if (stmt)
26782         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26783       break;
26784
26785     default:
26786       gcc_unreachable ();
26787     }
26788
26789   cp_parser_end_omp_structured_block (parser, save);
26790   stmt = finish_omp_parallel (par_clause, block);
26791   if (p_kind != PRAGMA_OMP_PARALLEL)
26792     OMP_PARALLEL_COMBINED (stmt) = 1;
26793   return stmt;
26794 }
26795
26796 /* OpenMP 2.5:
26797    # pragma omp single single-clause[optseq] new-line
26798      structured-block  */
26799
26800 #define OMP_SINGLE_CLAUSE_MASK                          \
26801         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26802         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26803         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26804         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26805
26806 static tree
26807 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26808 {
26809   tree stmt = make_node (OMP_SINGLE);
26810   TREE_TYPE (stmt) = void_type_node;
26811
26812   OMP_SINGLE_CLAUSES (stmt)
26813     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26814                                  "#pragma omp single", pragma_tok);
26815   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26816
26817   return add_stmt (stmt);
26818 }
26819
26820 /* OpenMP 3.0:
26821    # pragma omp task task-clause[optseq] new-line
26822      structured-block  */
26823
26824 #define OMP_TASK_CLAUSE_MASK                            \
26825         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26826         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26827         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26828         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26829         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26830         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26831         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26832         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26833
26834 static tree
26835 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26836 {
26837   tree clauses, block;
26838   unsigned int save;
26839
26840   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26841                                        "#pragma omp task", pragma_tok);
26842   block = begin_omp_task ();
26843   save = cp_parser_begin_omp_structured_block (parser);
26844   cp_parser_statement (parser, NULL_TREE, false, NULL);
26845   cp_parser_end_omp_structured_block (parser, save);
26846   return finish_omp_task (clauses, block);
26847 }
26848
26849 /* OpenMP 3.0:
26850    # pragma omp taskwait new-line  */
26851
26852 static void
26853 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26854 {
26855   cp_parser_require_pragma_eol (parser, pragma_tok);
26856   finish_omp_taskwait ();
26857 }
26858
26859 /* OpenMP 3.1:
26860    # pragma omp taskyield new-line  */
26861
26862 static void
26863 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26864 {
26865   cp_parser_require_pragma_eol (parser, pragma_tok);
26866   finish_omp_taskyield ();
26867 }
26868
26869 /* OpenMP 2.5:
26870    # pragma omp threadprivate (variable-list) */
26871
26872 static void
26873 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26874 {
26875   tree vars;
26876
26877   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26878   cp_parser_require_pragma_eol (parser, pragma_tok);
26879
26880   finish_omp_threadprivate (vars);
26881 }
26882
26883 /* Main entry point to OpenMP statement pragmas.  */
26884
26885 static void
26886 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26887 {
26888   tree stmt;
26889
26890   switch (pragma_tok->pragma_kind)
26891     {
26892     case PRAGMA_OMP_ATOMIC:
26893       cp_parser_omp_atomic (parser, pragma_tok);
26894       return;
26895     case PRAGMA_OMP_CRITICAL:
26896       stmt = cp_parser_omp_critical (parser, pragma_tok);
26897       break;
26898     case PRAGMA_OMP_FOR:
26899       stmt = cp_parser_omp_for (parser, pragma_tok);
26900       break;
26901     case PRAGMA_OMP_MASTER:
26902       stmt = cp_parser_omp_master (parser, pragma_tok);
26903       break;
26904     case PRAGMA_OMP_ORDERED:
26905       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26906       break;
26907     case PRAGMA_OMP_PARALLEL:
26908       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26909       break;
26910     case PRAGMA_OMP_SECTIONS:
26911       stmt = cp_parser_omp_sections (parser, pragma_tok);
26912       break;
26913     case PRAGMA_OMP_SINGLE:
26914       stmt = cp_parser_omp_single (parser, pragma_tok);
26915       break;
26916     case PRAGMA_OMP_TASK:
26917       stmt = cp_parser_omp_task (parser, pragma_tok);
26918       break;
26919     default:
26920       gcc_unreachable ();
26921     }
26922
26923   if (stmt)
26924     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26925 }
26926 \f
26927 /* Transactional Memory parsing routines.  */
26928
26929 /* Parse a transaction attribute.
26930
26931    txn-attribute:
26932         attribute
26933         [ [ identifier ] ]
26934
26935    ??? Simplify this when C++0x bracket attributes are
26936    implemented properly.  */
26937
26938 static tree
26939 cp_parser_txn_attribute_opt (cp_parser *parser)
26940 {
26941   cp_token *token;
26942   tree attr_name, attr = NULL;
26943
26944   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26945     return cp_parser_attributes_opt (parser);
26946
26947   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26948     return NULL_TREE;
26949   cp_lexer_consume_token (parser->lexer);
26950   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26951     goto error1;
26952
26953   token = cp_lexer_peek_token (parser->lexer);
26954   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26955     {
26956       token = cp_lexer_consume_token (parser->lexer);
26957
26958       attr_name = (token->type == CPP_KEYWORD
26959                    /* For keywords, use the canonical spelling,
26960                       not the parsed identifier.  */
26961                    ? ridpointers[(int) token->keyword]
26962                    : token->u.value);
26963       attr = build_tree_list (attr_name, NULL_TREE);
26964     }
26965   else
26966     cp_parser_error (parser, "expected identifier");
26967
26968   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26969  error1:
26970   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26971   return attr;
26972 }
26973
26974 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26975
26976    transaction-statement:
26977      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
26978        compound-statement
26979      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
26980 */
26981
26982 static tree
26983 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26984 {
26985   unsigned char old_in = parser->in_transaction;
26986   unsigned char this_in = 1, new_in;
26987   cp_token *token;
26988   tree stmt, attrs, noex;
26989
26990   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26991       || keyword == RID_TRANSACTION_RELAXED);
26992   token = cp_parser_require_keyword (parser, keyword,
26993       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26994           : RT_TRANSACTION_RELAXED));
26995   gcc_assert (token != NULL);
26996
26997   if (keyword == RID_TRANSACTION_RELAXED)
26998     this_in |= TM_STMT_ATTR_RELAXED;
26999   else
27000     {
27001       attrs = cp_parser_txn_attribute_opt (parser);
27002       if (attrs)
27003         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27004     }
27005
27006   /* Parse a noexcept specification.  */
27007   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
27008
27009   /* Keep track if we're in the lexical scope of an outer transaction.  */
27010   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
27011
27012   stmt = begin_transaction_stmt (token->location, NULL, this_in);
27013
27014   parser->in_transaction = new_in;
27015   cp_parser_compound_statement (parser, NULL, false, false);
27016   parser->in_transaction = old_in;
27017
27018   finish_transaction_stmt (stmt, NULL, this_in, noex);
27019
27020   return stmt;
27021 }
27022
27023 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27024
27025    transaction-expression:
27026      __transaction_atomic txn-noexcept-spec[opt] ( expression )
27027      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27028 */
27029
27030 static tree
27031 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27032 {
27033   unsigned char old_in = parser->in_transaction;
27034   unsigned char this_in = 1;
27035   cp_token *token;
27036   tree expr, noex;
27037   bool noex_expr;
27038
27039   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27040       || keyword == RID_TRANSACTION_RELAXED);
27041
27042   if (!flag_tm)
27043     error (keyword == RID_TRANSACTION_RELAXED
27044            ? G_("%<__transaction_relaxed%> without transactional memory "
27045                 "support enabled")
27046            : G_("%<__transaction_atomic%> without transactional memory "
27047                 "support enabled"));
27048
27049   token = cp_parser_require_keyword (parser, keyword,
27050       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27051           : RT_TRANSACTION_RELAXED));
27052   gcc_assert (token != NULL);
27053
27054   if (keyword == RID_TRANSACTION_RELAXED)
27055     this_in |= TM_STMT_ATTR_RELAXED;
27056
27057   /* Set this early.  This might mean that we allow transaction_cancel in
27058      an expression that we find out later actually has to be a constexpr.
27059      However, we expect that cxx_constant_value will be able to deal with
27060      this; also, if the noexcept has no constexpr, then what we parse next
27061      really is a transaction's body.  */
27062   parser->in_transaction = this_in;
27063
27064   /* Parse a noexcept specification.  */
27065   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27066                                                true);
27067
27068   if (!noex || !noex_expr
27069       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27070     {
27071       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27072
27073       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27074       finish_parenthesized_expr (expr);
27075
27076       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27077     }
27078   else
27079     {
27080       /* The only expression that is available got parsed for the noexcept
27081          already.  noexcept is true then.  */
27082       expr = noex;
27083       noex = boolean_true_node;
27084     }
27085
27086   expr = build_transaction_expr (token->location, expr, this_in, noex);
27087   parser->in_transaction = old_in;
27088
27089   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27090     return error_mark_node;
27091
27092   return (flag_tm ? expr : error_mark_node);
27093 }
27094
27095 /* Parse a function-transaction-block.
27096
27097    function-transaction-block:
27098      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27099          function-body
27100      __transaction_atomic txn-attribute[opt] function-try-block
27101      __transaction_relaxed ctor-initializer[opt] function-body
27102      __transaction_relaxed function-try-block
27103 */
27104
27105 static bool
27106 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27107 {
27108   unsigned char old_in = parser->in_transaction;
27109   unsigned char new_in = 1;
27110   tree compound_stmt, stmt, attrs;
27111   bool ctor_initializer_p;
27112   cp_token *token;
27113
27114   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27115       || keyword == RID_TRANSACTION_RELAXED);
27116   token = cp_parser_require_keyword (parser, keyword,
27117       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27118           : RT_TRANSACTION_RELAXED));
27119   gcc_assert (token != NULL);
27120
27121   if (keyword == RID_TRANSACTION_RELAXED)
27122     new_in |= TM_STMT_ATTR_RELAXED;
27123   else
27124     {
27125       attrs = cp_parser_txn_attribute_opt (parser);
27126       if (attrs)
27127         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27128     }
27129
27130   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27131
27132   parser->in_transaction = new_in;
27133
27134   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27135     ctor_initializer_p = cp_parser_function_try_block (parser);
27136   else
27137     ctor_initializer_p
27138       = cp_parser_ctor_initializer_opt_and_function_body (parser);
27139
27140   parser->in_transaction = old_in;
27141
27142   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27143
27144   return ctor_initializer_p;
27145 }
27146
27147 /* Parse a __transaction_cancel statement.
27148
27149    cancel-statement:
27150      __transaction_cancel txn-attribute[opt] ;
27151      __transaction_cancel txn-attribute[opt] throw-expression ;
27152
27153    ??? Cancel and throw is not yet implemented.  */
27154
27155 static tree
27156 cp_parser_transaction_cancel (cp_parser *parser)
27157 {
27158   cp_token *token;
27159   bool is_outer = false;
27160   tree stmt, attrs;
27161
27162   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27163                                      RT_TRANSACTION_CANCEL);
27164   gcc_assert (token != NULL);
27165
27166   attrs = cp_parser_txn_attribute_opt (parser);
27167   if (attrs)
27168     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27169
27170   /* ??? Parse cancel-and-throw here.  */
27171
27172   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27173
27174   if (!flag_tm)
27175     {
27176       error_at (token->location, "%<__transaction_cancel%> without "
27177                 "transactional memory support enabled");
27178       return error_mark_node;
27179     }
27180   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27181     {
27182       error_at (token->location, "%<__transaction_cancel%> within a "
27183                 "%<__transaction_relaxed%>");
27184       return error_mark_node;
27185     }
27186   else if (is_outer)
27187     {
27188       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27189           && !is_tm_may_cancel_outer (current_function_decl))
27190         {
27191           error_at (token->location, "outer %<__transaction_cancel%> not "
27192                     "within outer %<__transaction_atomic%>");
27193           error_at (token->location,
27194                     "  or a %<transaction_may_cancel_outer%> function");
27195           return error_mark_node;
27196         }
27197     }
27198   else if (parser->in_transaction == 0)
27199     {
27200       error_at (token->location, "%<__transaction_cancel%> not within "
27201                 "%<__transaction_atomic%>");
27202       return error_mark_node;
27203     }
27204
27205   stmt = build_tm_abort_call (token->location, is_outer);
27206   add_stmt (stmt);
27207   finish_stmt ();
27208
27209   return stmt;
27210 }
27211 \f
27212 /* The parser.  */
27213
27214 static GTY (()) cp_parser *the_parser;
27215
27216 \f
27217 /* Special handling for the first token or line in the file.  The first
27218    thing in the file might be #pragma GCC pch_preprocess, which loads a
27219    PCH file, which is a GC collection point.  So we need to handle this
27220    first pragma without benefit of an existing lexer structure.
27221
27222    Always returns one token to the caller in *FIRST_TOKEN.  This is
27223    either the true first token of the file, or the first token after
27224    the initial pragma.  */
27225
27226 static void
27227 cp_parser_initial_pragma (cp_token *first_token)
27228 {
27229   tree name = NULL;
27230
27231   cp_lexer_get_preprocessor_token (NULL, first_token);
27232   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27233     return;
27234
27235   cp_lexer_get_preprocessor_token (NULL, first_token);
27236   if (first_token->type == CPP_STRING)
27237     {
27238       name = first_token->u.value;
27239
27240       cp_lexer_get_preprocessor_token (NULL, first_token);
27241       if (first_token->type != CPP_PRAGMA_EOL)
27242         error_at (first_token->location,
27243                   "junk at end of %<#pragma GCC pch_preprocess%>");
27244     }
27245   else
27246     error_at (first_token->location, "expected string literal");
27247
27248   /* Skip to the end of the pragma.  */
27249   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27250     cp_lexer_get_preprocessor_token (NULL, first_token);
27251
27252   /* Now actually load the PCH file.  */
27253   if (name)
27254     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27255
27256   /* Read one more token to return to our caller.  We have to do this
27257      after reading the PCH file in, since its pointers have to be
27258      live.  */
27259   cp_lexer_get_preprocessor_token (NULL, first_token);
27260 }
27261
27262 /* Normal parsing of a pragma token.  Here we can (and must) use the
27263    regular lexer.  */
27264
27265 static bool
27266 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27267 {
27268   cp_token *pragma_tok;
27269   unsigned int id;
27270
27271   pragma_tok = cp_lexer_consume_token (parser->lexer);
27272   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27273   parser->lexer->in_pragma = true;
27274
27275   id = pragma_tok->pragma_kind;
27276   switch (id)
27277     {
27278     case PRAGMA_GCC_PCH_PREPROCESS:
27279       error_at (pragma_tok->location,
27280                 "%<#pragma GCC pch_preprocess%> must be first");
27281       break;
27282
27283     case PRAGMA_OMP_BARRIER:
27284       switch (context)
27285         {
27286         case pragma_compound:
27287           cp_parser_omp_barrier (parser, pragma_tok);
27288           return false;
27289         case pragma_stmt:
27290           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27291                     "used in compound statements");
27292           break;
27293         default:
27294           goto bad_stmt;
27295         }
27296       break;
27297
27298     case PRAGMA_OMP_FLUSH:
27299       switch (context)
27300         {
27301         case pragma_compound:
27302           cp_parser_omp_flush (parser, pragma_tok);
27303           return false;
27304         case pragma_stmt:
27305           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27306                     "used in compound statements");
27307           break;
27308         default:
27309           goto bad_stmt;
27310         }
27311       break;
27312
27313     case PRAGMA_OMP_TASKWAIT:
27314       switch (context)
27315         {
27316         case pragma_compound:
27317           cp_parser_omp_taskwait (parser, pragma_tok);
27318           return false;
27319         case pragma_stmt:
27320           error_at (pragma_tok->location,
27321                     "%<#pragma omp taskwait%> may only be "
27322                     "used in compound statements");
27323           break;
27324         default:
27325           goto bad_stmt;
27326         }
27327       break;
27328
27329     case PRAGMA_OMP_TASKYIELD:
27330       switch (context)
27331         {
27332         case pragma_compound:
27333           cp_parser_omp_taskyield (parser, pragma_tok);
27334           return false;
27335         case pragma_stmt:
27336           error_at (pragma_tok->location,
27337                     "%<#pragma omp taskyield%> may only be "
27338                     "used in compound statements");
27339           break;
27340         default:
27341           goto bad_stmt;
27342         }
27343       break;
27344
27345     case PRAGMA_OMP_THREADPRIVATE:
27346       cp_parser_omp_threadprivate (parser, pragma_tok);
27347       return false;
27348
27349     case PRAGMA_OMP_ATOMIC:
27350     case PRAGMA_OMP_CRITICAL:
27351     case PRAGMA_OMP_FOR:
27352     case PRAGMA_OMP_MASTER:
27353     case PRAGMA_OMP_ORDERED:
27354     case PRAGMA_OMP_PARALLEL:
27355     case PRAGMA_OMP_SECTIONS:
27356     case PRAGMA_OMP_SINGLE:
27357     case PRAGMA_OMP_TASK:
27358       if (context == pragma_external)
27359         goto bad_stmt;
27360       cp_parser_omp_construct (parser, pragma_tok);
27361       return true;
27362
27363     case PRAGMA_OMP_SECTION:
27364       error_at (pragma_tok->location, 
27365                 "%<#pragma omp section%> may only be used in "
27366                 "%<#pragma omp sections%> construct");
27367       break;
27368
27369     default:
27370       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27371       c_invoke_pragma_handler (id);
27372       break;
27373
27374     bad_stmt:
27375       cp_parser_error (parser, "expected declaration specifiers");
27376       break;
27377     }
27378
27379   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27380   return false;
27381 }
27382
27383 /* The interface the pragma parsers have to the lexer.  */
27384
27385 enum cpp_ttype
27386 pragma_lex (tree *value)
27387 {
27388   cp_token *tok;
27389   enum cpp_ttype ret;
27390
27391   tok = cp_lexer_peek_token (the_parser->lexer);
27392
27393   ret = tok->type;
27394   *value = tok->u.value;
27395
27396   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27397     ret = CPP_EOF;
27398   else if (ret == CPP_STRING)
27399     *value = cp_parser_string_literal (the_parser, false, false);
27400   else
27401     {
27402       cp_lexer_consume_token (the_parser->lexer);
27403       if (ret == CPP_KEYWORD)
27404         ret = CPP_NAME;
27405     }
27406
27407   return ret;
27408 }
27409
27410 \f
27411 /* External interface.  */
27412
27413 /* Parse one entire translation unit.  */
27414
27415 void
27416 c_parse_file (void)
27417 {
27418   static bool already_called = false;
27419
27420   if (already_called)
27421     {
27422       sorry ("inter-module optimizations not implemented for C++");
27423       return;
27424     }
27425   already_called = true;
27426
27427   the_parser = cp_parser_new ();
27428   push_deferring_access_checks (flag_access_control
27429                                 ? dk_no_deferred : dk_no_check);
27430   cp_parser_translation_unit (the_parser);
27431   the_parser = NULL;
27432 }
27433
27434 #include "gt-cp-parser.h"