OSDN Git Service

PR c++/51496
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR,
110   /* a transaction expression */
111   NIC_TRANSACTION
112 } non_integral_constant;
113
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error {
116   /* NULL */
117   NLE_NULL,
118   /* is not a type */
119   NLE_TYPE,
120   /* is not a class or namespace */
121   NLE_CXX98,
122   /* is not a class, namespace, or enumeration */
123   NLE_NOT_CXX98
124 } name_lookup_error;
125
126 /* The various kinds of required token */
127 typedef enum required_token {
128   RT_NONE,
129   RT_SEMICOLON,  /* ';' */
130   RT_OPEN_PAREN, /* '(' */
131   RT_CLOSE_BRACE, /* '}' */
132   RT_OPEN_BRACE,  /* '{' */
133   RT_CLOSE_SQUARE, /* ']' */
134   RT_OPEN_SQUARE,  /* '[' */
135   RT_COMMA, /* ',' */
136   RT_SCOPE, /* '::' */
137   RT_LESS, /* '<' */
138   RT_GREATER, /* '>' */
139   RT_EQ, /* '=' */
140   RT_ELLIPSIS, /* '...' */
141   RT_MULT, /* '*' */
142   RT_COMPL, /* '~' */
143   RT_COLON, /* ':' */
144   RT_COLON_SCOPE, /* ':' or '::' */
145   RT_CLOSE_PAREN, /* ')' */
146   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147   RT_PRAGMA_EOL, /* end of line */
148   RT_NAME, /* identifier */
149
150   /* The type is CPP_KEYWORD */
151   RT_NEW, /* new */
152   RT_DELETE, /* delete */
153   RT_RETURN, /* return */
154   RT_WHILE, /* while */
155   RT_EXTERN, /* extern */
156   RT_STATIC_ASSERT, /* static_assert */
157   RT_DECLTYPE, /* decltype */
158   RT_OPERATOR, /* operator */
159   RT_CLASS, /* class */
160   RT_TEMPLATE, /* template */
161   RT_NAMESPACE, /* namespace */
162   RT_USING, /* using */
163   RT_ASM, /* asm */
164   RT_TRY, /* try */
165   RT_CATCH, /* catch */
166   RT_THROW, /* throw */
167   RT_LABEL, /* __label__ */
168   RT_AT_TRY, /* @try */
169   RT_AT_SYNCHRONIZED, /* @synchronized */
170   RT_AT_THROW, /* @throw */
171
172   RT_SELECT,  /* selection-statement */
173   RT_INTERATION, /* iteration-statement */
174   RT_JUMP, /* jump-statement */
175   RT_CLASS_KEY, /* class-key */
176   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179   RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 } required_token;
181
182 /* Prototypes.  */
183
184 static cp_lexer *cp_lexer_new_main
185   (void);
186 static cp_lexer *cp_lexer_new_from_tokens
187   (cp_token_cache *tokens);
188 static void cp_lexer_destroy
189   (cp_lexer *);
190 static int cp_lexer_saving_tokens
191   (const cp_lexer *);
192 static cp_token *cp_lexer_token_at
193   (cp_lexer *, cp_token_position);
194 static void cp_lexer_get_preprocessor_token
195   (cp_lexer *, cp_token *);
196 static inline cp_token *cp_lexer_peek_token
197   (cp_lexer *);
198 static cp_token *cp_lexer_peek_nth_token
199   (cp_lexer *, size_t);
200 static inline bool cp_lexer_next_token_is
201   (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_not
203   (cp_lexer *, enum cpp_ttype);
204 static bool cp_lexer_next_token_is_keyword
205   (cp_lexer *, enum rid);
206 static cp_token *cp_lexer_consume_token
207   (cp_lexer *);
208 static void cp_lexer_purge_token
209   (cp_lexer *);
210 static void cp_lexer_purge_tokens_after
211   (cp_lexer *, cp_token_position);
212 static void cp_lexer_save_tokens
213   (cp_lexer *);
214 static void cp_lexer_commit_tokens
215   (cp_lexer *);
216 static void cp_lexer_rollback_tokens
217   (cp_lexer *);
218 static void cp_lexer_print_token
219   (FILE *, cp_token *);
220 static inline bool cp_lexer_debugging_p
221   (cp_lexer *);
222 static void cp_lexer_start_debugging
223   (cp_lexer *) ATTRIBUTE_UNUSED;
224 static void cp_lexer_stop_debugging
225   (cp_lexer *) ATTRIBUTE_UNUSED;
226
227 static cp_token_cache *cp_token_cache_new
228   (cp_token *, cp_token *);
229
230 static void cp_parser_initial_pragma
231   (cp_token *);
232
233 static tree cp_literal_operator_id
234   (const char *);
235
236 /* Manifest constants.  */
237 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
238 #define CP_SAVED_TOKEN_STACK 5
239
240 /* Variables.  */
241
242 /* The stream to which debugging output should be written.  */
243 static FILE *cp_lexer_debug_stream;
244
245 /* Nonzero if we are parsing an unevaluated operand: an operand to
246    sizeof, typeof, or alignof.  */
247 int cp_unevaluated_operand;
248
249 /* Dump up to NUM tokens in BUFFER to FILE starting with token
250    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
251    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
252    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
253    highlighted by surrounding it in [[ ]].  */
254
255 static void
256 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
257                       cp_token *start_token, unsigned num,
258                       cp_token *curr_token)
259 {
260   unsigned i, nprinted;
261   cp_token *token;
262   bool do_print;
263
264   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265
266   if (buffer == NULL)
267     return;
268
269   if (num == 0)
270     num = VEC_length (cp_token, buffer);
271
272   if (start_token == NULL)
273     start_token = VEC_address (cp_token, buffer);
274
275   if (start_token > VEC_address (cp_token, buffer))
276     {
277       cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
278       fprintf (file, " ... ");
279     }
280
281   do_print = false;
282   nprinted = 0;
283   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284     {
285       if (token == start_token)
286         do_print = true;
287
288       if (!do_print)
289         continue;
290
291       nprinted++;
292       if (token == curr_token)
293         fprintf (file, "[[");
294
295       cp_lexer_print_token (file, token);
296
297       if (token == curr_token)
298         fprintf (file, "]]");
299
300       switch (token->type)
301         {
302           case CPP_SEMICOLON:
303           case CPP_OPEN_BRACE:
304           case CPP_CLOSE_BRACE:
305           case CPP_EOF:
306             fputc ('\n', file);
307             break;
308
309           default:
310             fputc (' ', file);
311         }
312     }
313
314   if (i == num && i < VEC_length (cp_token, buffer))
315     {
316       fprintf (file, " ... ");
317       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
318                             VEC_length (cp_token, buffer) - 1));
319     }
320
321   fprintf (file, "\n");
322 }
323
324
325 /* Dump all tokens in BUFFER to stderr.  */
326
327 void
328 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
329 {
330   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 }
332
333
334 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
335    description for T.  */
336
337 static void
338 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
339 {
340   if (t)
341     {
342       fprintf (file, "%s: ", desc);
343       print_node_brief (file, "", t, 0);
344     }
345 }
346
347
348 /* Dump parser context C to FILE.  */
349
350 static void
351 cp_debug_print_context (FILE *file, cp_parser_context *c)
352 {
353   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
354   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
355   print_node_brief (file, "", c->object_type, 0);
356   fprintf (file, "}\n");
357 }
358
359
360 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
361
362 static void
363 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
364 {
365   unsigned i;
366   cp_parser_context *c;
367
368   fprintf (file, "Parsing context stack:\n");
369   for (i = 0, c = first; c; c = c->next, i++)
370     {
371       fprintf (file, "\t#%u: ", i);
372       cp_debug_print_context (file, c);
373     }
374 }
375
376
377 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
378
379 static void
380 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
381 {
382   if (flag)
383     fprintf (file, "%s: true\n", desc);
384 }
385
386
387 /* Print an unparsed function entry UF to FILE.  */
388
389 static void
390 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
391 {
392   unsigned i;
393   cp_default_arg_entry *default_arg_fn;
394   tree fn;
395
396   fprintf (file, "\tFunctions with default args:\n");
397   for (i = 0;
398        VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
399                     default_arg_fn);
400        i++)
401     {
402       fprintf (file, "\t\tClass type: ");
403       print_node_brief (file, "", default_arg_fn->class_type, 0);
404       fprintf (file, "\t\tDeclaration: ");
405       print_node_brief (file, "", default_arg_fn->decl, 0);
406       fprintf (file, "\n");
407     }
408
409   fprintf (file, "\n\tFunctions with definitions that require "
410            "post-processing\n\t\t");
411   for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
412     {
413       print_node_brief (file, "", fn, 0);
414       fprintf (file, " ");
415     }
416   fprintf (file, "\n");
417
418   fprintf (file, "\n\tNon-static data members with initializers that require "
419            "post-processing\n\t\t");
420   for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
421     {
422       print_node_brief (file, "", fn, 0);
423       fprintf (file, " ");
424     }
425   fprintf (file, "\n");
426 }
427
428
429 /* Print the stack of unparsed member functions S to FILE.  */
430
431 static void
432 cp_debug_print_unparsed_queues (FILE *file,
433                                 VEC(cp_unparsed_functions_entry, gc) *s)
434 {
435   unsigned i;
436   cp_unparsed_functions_entry *uf;
437
438   fprintf (file, "Unparsed functions\n");
439   for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
440     {
441       fprintf (file, "#%u:\n", i);
442       cp_debug_print_unparsed_function (file, uf);
443     }
444 }
445
446
447 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
448    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
449
450 static void
451 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
452 {
453   cp_token *next_token, *first_token, *start_token;
454
455   if (file == NULL)
456     file = stderr;
457
458   next_token = parser->lexer->next_token;
459   first_token = VEC_address (cp_token, parser->lexer->buffer);
460   start_token = (next_token > first_token + window_size / 2)
461                 ? next_token - window_size / 2
462                 : first_token;
463   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
464                         next_token);
465 }
466
467
468 /* Dump debugging information for the given PARSER.  If FILE is NULL,
469    the output is printed on stderr.  */
470
471 void
472 cp_debug_parser (FILE *file, cp_parser *parser)
473 {
474   const size_t window_size = 20;
475   cp_token *token;
476   expanded_location eloc;
477
478   if (file == NULL)
479     file = stderr;
480
481   fprintf (file, "Parser state\n\n");
482   fprintf (file, "Number of tokens: %u\n",
483            VEC_length (cp_token, parser->lexer->buffer));
484   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
485   cp_debug_print_tree_if_set (file, "Object scope",
486                                      parser->object_scope);
487   cp_debug_print_tree_if_set (file, "Qualifying scope",
488                                      parser->qualifying_scope);
489   cp_debug_print_context_stack (file, parser->context);
490   cp_debug_print_flag (file, "Allow GNU extensions",
491                               parser->allow_gnu_extensions_p);
492   cp_debug_print_flag (file, "'>' token is greater-than",
493                               parser->greater_than_is_operator_p);
494   cp_debug_print_flag (file, "Default args allowed in current "
495                               "parameter list", parser->default_arg_ok_p);
496   cp_debug_print_flag (file, "Parsing integral constant-expression",
497                               parser->integral_constant_expression_p);
498   cp_debug_print_flag (file, "Allow non-constant expression in current "
499                               "constant-expression",
500                               parser->allow_non_integral_constant_expression_p);
501   cp_debug_print_flag (file, "Seen non-constant expression",
502                               parser->non_integral_constant_expression_p);
503   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
504                               "current context",
505                               parser->local_variables_forbidden_p);
506   cp_debug_print_flag (file, "In unbraced linkage specification",
507                               parser->in_unbraced_linkage_specification_p);
508   cp_debug_print_flag (file, "Parsing a declarator",
509                               parser->in_declarator_p);
510   cp_debug_print_flag (file, "In template argument list",
511                               parser->in_template_argument_list_p);
512   cp_debug_print_flag (file, "Parsing an iteration statement",
513                               parser->in_statement & IN_ITERATION_STMT);
514   cp_debug_print_flag (file, "Parsing a switch statement",
515                               parser->in_statement & IN_SWITCH_STMT);
516   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
517                               parser->in_statement & IN_OMP_BLOCK);
518   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
519                               parser->in_statement & IN_OMP_FOR);
520   cp_debug_print_flag (file, "Parsing an if statement",
521                               parser->in_statement & IN_IF_STMT);
522   cp_debug_print_flag (file, "Parsing a type-id in an expression "
523                               "context", parser->in_type_id_in_expr_p);
524   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
525                               parser->implicit_extern_c);
526   cp_debug_print_flag (file, "String expressions should be translated "
527                               "to execution character set",
528                               parser->translate_strings_p);
529   cp_debug_print_flag (file, "Parsing function body outside of a "
530                               "local class", parser->in_function_body);
531   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
532                               parser->colon_corrects_to_scope_p);
533   if (parser->type_definition_forbidden_message)
534     fprintf (file, "Error message for forbidden type definitions: %s\n",
535              parser->type_definition_forbidden_message);
536   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
537   fprintf (file, "Number of class definitions in progress: %u\n",
538            parser->num_classes_being_defined);
539   fprintf (file, "Number of template parameter lists for the current "
540            "declaration: %u\n", parser->num_template_parameter_lists);
541   cp_debug_parser_tokens (file, parser, window_size);
542   token = parser->lexer->next_token;
543   fprintf (file, "Next token to parse:\n");
544   fprintf (file, "\tToken:  ");
545   cp_lexer_print_token (file, token);
546   eloc = expand_location (token->location);
547   fprintf (file, "\n\tFile:   %s\n", eloc.file);
548   fprintf (file, "\tLine:   %d\n", eloc.line);
549   fprintf (file, "\tColumn: %d\n", eloc.column);
550 }
551
552
553 /* Allocate memory for a new lexer object and return it.  */
554
555 static cp_lexer *
556 cp_lexer_alloc (void)
557 {
558   cp_lexer *lexer;
559
560   c_common_no_more_pch ();
561
562   /* Allocate the memory.  */
563   lexer = ggc_alloc_cleared_cp_lexer ();
564
565   /* Initially we are not debugging.  */
566   lexer->debugging_p = false;
567
568   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
569                                    CP_SAVED_TOKEN_STACK);
570
571   /* Create the buffer.  */
572   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
573
574   return lexer;
575 }
576
577
578 /* Create a new main C++ lexer, the lexer that gets tokens from the
579    preprocessor.  */
580
581 static cp_lexer *
582 cp_lexer_new_main (void)
583 {
584   cp_lexer *lexer;
585   cp_token token;
586
587   /* It's possible that parsing the first pragma will load a PCH file,
588      which is a GC collection point.  So we have to do that before
589      allocating any memory.  */
590   cp_parser_initial_pragma (&token);
591
592   lexer = cp_lexer_alloc ();
593
594   /* Put the first token in the buffer.  */
595   VEC_quick_push (cp_token, lexer->buffer, &token);
596
597   /* Get the remaining tokens from the preprocessor.  */
598   while (token.type != CPP_EOF)
599     {
600       cp_lexer_get_preprocessor_token (lexer, &token);
601       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
602     }
603
604   lexer->last_token = VEC_address (cp_token, lexer->buffer)
605                       + VEC_length (cp_token, lexer->buffer)
606                       - 1;
607   lexer->next_token = VEC_length (cp_token, lexer->buffer)
608                       ? VEC_address (cp_token, lexer->buffer)
609                       : &eof_token;
610
611   /* Subsequent preprocessor diagnostics should use compiler
612      diagnostic functions to get the compiler source location.  */
613   done_lexing = true;
614
615   gcc_assert (!lexer->next_token->purged_p);
616   return lexer;
617 }
618
619 /* Create a new lexer whose token stream is primed with the tokens in
620    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
621
622 static cp_lexer *
623 cp_lexer_new_from_tokens (cp_token_cache *cache)
624 {
625   cp_token *first = cache->first;
626   cp_token *last = cache->last;
627   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
628
629   /* We do not own the buffer.  */
630   lexer->buffer = NULL;
631   lexer->next_token = first == last ? &eof_token : first;
632   lexer->last_token = last;
633
634   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
635                                    CP_SAVED_TOKEN_STACK);
636
637   /* Initially we are not debugging.  */
638   lexer->debugging_p = false;
639
640   gcc_assert (!lexer->next_token->purged_p);
641   return lexer;
642 }
643
644 /* Frees all resources associated with LEXER.  */
645
646 static void
647 cp_lexer_destroy (cp_lexer *lexer)
648 {
649   VEC_free (cp_token, gc, lexer->buffer);
650   VEC_free (cp_token_position, heap, lexer->saved_tokens);
651   ggc_free (lexer);
652 }
653
654 /* Returns nonzero if debugging information should be output.  */
655
656 static inline bool
657 cp_lexer_debugging_p (cp_lexer *lexer)
658 {
659   return lexer->debugging_p;
660 }
661
662
663 static inline cp_token_position
664 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
665 {
666   gcc_assert (!previous_p || lexer->next_token != &eof_token);
667
668   return lexer->next_token - previous_p;
669 }
670
671 static inline cp_token *
672 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
673 {
674   return pos;
675 }
676
677 static inline void
678 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
679 {
680   lexer->next_token = cp_lexer_token_at (lexer, pos);
681 }
682
683 static inline cp_token_position
684 cp_lexer_previous_token_position (cp_lexer *lexer)
685 {
686   if (lexer->next_token == &eof_token)
687     return lexer->last_token - 1;
688   else
689     return cp_lexer_token_position (lexer, true);
690 }
691
692 static inline cp_token *
693 cp_lexer_previous_token (cp_lexer *lexer)
694 {
695   cp_token_position tp = cp_lexer_previous_token_position (lexer);
696
697   return cp_lexer_token_at (lexer, tp);
698 }
699
700 /* nonzero if we are presently saving tokens.  */
701
702 static inline int
703 cp_lexer_saving_tokens (const cp_lexer* lexer)
704 {
705   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
706 }
707
708 /* Store the next token from the preprocessor in *TOKEN.  Return true
709    if we reach EOF.  If LEXER is NULL, assume we are handling an
710    initial #pragma pch_preprocess, and thus want the lexer to return
711    processed strings.  */
712
713 static void
714 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
715 {
716   static int is_extern_c = 0;
717
718    /* Get a new token from the preprocessor.  */
719   token->type
720     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
721                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
722   token->keyword = RID_MAX;
723   token->pragma_kind = PRAGMA_NONE;
724   token->purged_p = false;
725
726   /* On some systems, some header files are surrounded by an
727      implicit extern "C" block.  Set a flag in the token if it
728      comes from such a header.  */
729   is_extern_c += pending_lang_change;
730   pending_lang_change = 0;
731   token->implicit_extern_c = is_extern_c > 0;
732
733   /* Check to see if this token is a keyword.  */
734   if (token->type == CPP_NAME)
735     {
736       if (C_IS_RESERVED_WORD (token->u.value))
737         {
738           /* Mark this token as a keyword.  */
739           token->type = CPP_KEYWORD;
740           /* Record which keyword.  */
741           token->keyword = C_RID_CODE (token->u.value);
742         }
743       else
744         {
745           if (warn_cxx0x_compat
746               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
747               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
748             {
749               /* Warn about the C++0x keyword (but still treat it as
750                  an identifier).  */
751               warning (OPT_Wc__0x_compat, 
752                        "identifier %qE is a keyword in C++11",
753                        token->u.value);
754
755               /* Clear out the C_RID_CODE so we don't warn about this
756                  particular identifier-turned-keyword again.  */
757               C_SET_RID_CODE (token->u.value, RID_MAX);
758             }
759
760           token->ambiguous_p = false;
761           token->keyword = RID_MAX;
762         }
763     }
764   else if (token->type == CPP_AT_NAME)
765     {
766       /* This only happens in Objective-C++; it must be a keyword.  */
767       token->type = CPP_KEYWORD;
768       switch (C_RID_CODE (token->u.value))
769         {
770           /* Replace 'class' with '@class', 'private' with '@private',
771              etc.  This prevents confusion with the C++ keyword
772              'class', and makes the tokens consistent with other
773              Objective-C 'AT' keywords.  For example '@class' is
774              reported as RID_AT_CLASS which is consistent with
775              '@synchronized', which is reported as
776              RID_AT_SYNCHRONIZED.
777           */
778         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
779         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
780         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
781         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
782         case RID_THROW:     token->keyword = RID_AT_THROW; break;
783         case RID_TRY:       token->keyword = RID_AT_TRY; break;
784         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
785         default:            token->keyword = C_RID_CODE (token->u.value);
786         }
787     }
788   else if (token->type == CPP_PRAGMA)
789     {
790       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
791       token->pragma_kind = ((enum pragma_kind)
792                             TREE_INT_CST_LOW (token->u.value));
793       token->u.value = NULL_TREE;
794     }
795 }
796
797 /* Update the globals input_location and the input file stack from TOKEN.  */
798 static inline void
799 cp_lexer_set_source_position_from_token (cp_token *token)
800 {
801   if (token->type != CPP_EOF)
802     {
803       input_location = token->location;
804     }
805 }
806
807 /* Return a pointer to the next token in the token stream, but do not
808    consume it.  */
809
810 static inline cp_token *
811 cp_lexer_peek_token (cp_lexer *lexer)
812 {
813   if (cp_lexer_debugging_p (lexer))
814     {
815       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
816       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
817       putc ('\n', cp_lexer_debug_stream);
818     }
819   return lexer->next_token;
820 }
821
822 /* Return true if the next token has the indicated TYPE.  */
823
824 static inline bool
825 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
826 {
827   return cp_lexer_peek_token (lexer)->type == type;
828 }
829
830 /* Return true if the next token does not have the indicated TYPE.  */
831
832 static inline bool
833 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
834 {
835   return !cp_lexer_next_token_is (lexer, type);
836 }
837
838 /* Return true if the next token is the indicated KEYWORD.  */
839
840 static inline bool
841 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
842 {
843   return cp_lexer_peek_token (lexer)->keyword == keyword;
844 }
845
846 /* Return true if the next token is not the indicated KEYWORD.  */
847
848 static inline bool
849 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
850 {
851   return cp_lexer_peek_token (lexer)->keyword != keyword;
852 }
853
854 /* Return true if the next token is a keyword for a decl-specifier.  */
855
856 static bool
857 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
858 {
859   cp_token *token;
860
861   token = cp_lexer_peek_token (lexer);
862   switch (token->keyword) 
863     {
864       /* auto specifier: storage-class-specifier in C++,
865          simple-type-specifier in C++0x.  */
866     case RID_AUTO:
867       /* Storage classes.  */
868     case RID_REGISTER:
869     case RID_STATIC:
870     case RID_EXTERN:
871     case RID_MUTABLE:
872     case RID_THREAD:
873       /* Elaborated type specifiers.  */
874     case RID_ENUM:
875     case RID_CLASS:
876     case RID_STRUCT:
877     case RID_UNION:
878     case RID_TYPENAME:
879       /* Simple type specifiers.  */
880     case RID_CHAR:
881     case RID_CHAR16:
882     case RID_CHAR32:
883     case RID_WCHAR:
884     case RID_BOOL:
885     case RID_SHORT:
886     case RID_INT:
887     case RID_LONG:
888     case RID_INT128:
889     case RID_SIGNED:
890     case RID_UNSIGNED:
891     case RID_FLOAT:
892     case RID_DOUBLE:
893     case RID_VOID:
894       /* GNU extensions.  */ 
895     case RID_ATTRIBUTE:
896     case RID_TYPEOF:
897       /* C++0x extensions.  */
898     case RID_DECLTYPE:
899     case RID_UNDERLYING_TYPE:
900       return true;
901
902     default:
903       return false;
904     }
905 }
906
907 /* Returns TRUE iff the token T begins a decltype type.  */
908
909 static bool
910 token_is_decltype (cp_token *t)
911 {
912   return (t->keyword == RID_DECLTYPE
913           || t->type == CPP_DECLTYPE);
914 }
915
916 /* Returns TRUE iff the next token begins a decltype type.  */
917
918 static bool
919 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
920 {
921   cp_token *t = cp_lexer_peek_token (lexer);
922   return token_is_decltype (t);
923 }
924
925 /* Return a pointer to the Nth token in the token stream.  If N is 1,
926    then this is precisely equivalent to cp_lexer_peek_token (except
927    that it is not inline).  One would like to disallow that case, but
928    there is one case (cp_parser_nth_token_starts_template_id) where
929    the caller passes a variable for N and it might be 1.  */
930
931 static cp_token *
932 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
933 {
934   cp_token *token;
935
936   /* N is 1-based, not zero-based.  */
937   gcc_assert (n > 0);
938
939   if (cp_lexer_debugging_p (lexer))
940     fprintf (cp_lexer_debug_stream,
941              "cp_lexer: peeking ahead %ld at token: ", (long)n);
942
943   --n;
944   token = lexer->next_token;
945   gcc_assert (!n || token != &eof_token);
946   while (n != 0)
947     {
948       ++token;
949       if (token == lexer->last_token)
950         {
951           token = &eof_token;
952           break;
953         }
954
955       if (!token->purged_p)
956         --n;
957     }
958
959   if (cp_lexer_debugging_p (lexer))
960     {
961       cp_lexer_print_token (cp_lexer_debug_stream, token);
962       putc ('\n', cp_lexer_debug_stream);
963     }
964
965   return token;
966 }
967
968 /* Return the next token, and advance the lexer's next_token pointer
969    to point to the next non-purged token.  */
970
971 static cp_token *
972 cp_lexer_consume_token (cp_lexer* lexer)
973 {
974   cp_token *token = lexer->next_token;
975
976   gcc_assert (token != &eof_token);
977   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
978
979   do
980     {
981       lexer->next_token++;
982       if (lexer->next_token == lexer->last_token)
983         {
984           lexer->next_token = &eof_token;
985           break;
986         }
987
988     }
989   while (lexer->next_token->purged_p);
990
991   cp_lexer_set_source_position_from_token (token);
992
993   /* Provide debugging output.  */
994   if (cp_lexer_debugging_p (lexer))
995     {
996       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
997       cp_lexer_print_token (cp_lexer_debug_stream, token);
998       putc ('\n', cp_lexer_debug_stream);
999     }
1000
1001   return token;
1002 }
1003
1004 /* Permanently remove the next token from the token stream, and
1005    advance the next_token pointer to refer to the next non-purged
1006    token.  */
1007
1008 static void
1009 cp_lexer_purge_token (cp_lexer *lexer)
1010 {
1011   cp_token *tok = lexer->next_token;
1012
1013   gcc_assert (tok != &eof_token);
1014   tok->purged_p = true;
1015   tok->location = UNKNOWN_LOCATION;
1016   tok->u.value = NULL_TREE;
1017   tok->keyword = RID_MAX;
1018
1019   do
1020     {
1021       tok++;
1022       if (tok == lexer->last_token)
1023         {
1024           tok = &eof_token;
1025           break;
1026         }
1027     }
1028   while (tok->purged_p);
1029   lexer->next_token = tok;
1030 }
1031
1032 /* Permanently remove all tokens after TOK, up to, but not
1033    including, the token that will be returned next by
1034    cp_lexer_peek_token.  */
1035
1036 static void
1037 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1038 {
1039   cp_token *peek = lexer->next_token;
1040
1041   if (peek == &eof_token)
1042     peek = lexer->last_token;
1043
1044   gcc_assert (tok < peek);
1045
1046   for ( tok += 1; tok != peek; tok += 1)
1047     {
1048       tok->purged_p = true;
1049       tok->location = UNKNOWN_LOCATION;
1050       tok->u.value = NULL_TREE;
1051       tok->keyword = RID_MAX;
1052     }
1053 }
1054
1055 /* Begin saving tokens.  All tokens consumed after this point will be
1056    preserved.  */
1057
1058 static void
1059 cp_lexer_save_tokens (cp_lexer* lexer)
1060 {
1061   /* Provide debugging output.  */
1062   if (cp_lexer_debugging_p (lexer))
1063     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1064
1065   VEC_safe_push (cp_token_position, heap,
1066                  lexer->saved_tokens, lexer->next_token);
1067 }
1068
1069 /* Commit to the portion of the token stream most recently saved.  */
1070
1071 static void
1072 cp_lexer_commit_tokens (cp_lexer* lexer)
1073 {
1074   /* Provide debugging output.  */
1075   if (cp_lexer_debugging_p (lexer))
1076     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1077
1078   VEC_pop (cp_token_position, lexer->saved_tokens);
1079 }
1080
1081 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1082    to the token stream.  Stop saving tokens.  */
1083
1084 static void
1085 cp_lexer_rollback_tokens (cp_lexer* lexer)
1086 {
1087   /* Provide debugging output.  */
1088   if (cp_lexer_debugging_p (lexer))
1089     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1090
1091   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1092 }
1093
1094 /* Print a representation of the TOKEN on the STREAM.  */
1095
1096 static void
1097 cp_lexer_print_token (FILE * stream, cp_token *token)
1098 {
1099   /* We don't use cpp_type2name here because the parser defines
1100      a few tokens of its own.  */
1101   static const char *const token_names[] = {
1102     /* cpplib-defined token types */
1103 #define OP(e, s) #e,
1104 #define TK(e, s) #e,
1105     TTYPE_TABLE
1106 #undef OP
1107 #undef TK
1108     /* C++ parser token types - see "Manifest constants", above.  */
1109     "KEYWORD",
1110     "TEMPLATE_ID",
1111     "NESTED_NAME_SPECIFIER",
1112   };
1113
1114   /* For some tokens, print the associated data.  */
1115   switch (token->type)
1116     {
1117     case CPP_KEYWORD:
1118       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1119          For example, `struct' is mapped to an INTEGER_CST.  */
1120       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1121         break;
1122       /* else fall through */
1123     case CPP_NAME:
1124       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1125       break;
1126
1127     case CPP_STRING:
1128     case CPP_STRING16:
1129     case CPP_STRING32:
1130     case CPP_WSTRING:
1131     case CPP_UTF8STRING:
1132       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1133       break;
1134
1135     case CPP_NUMBER:
1136       print_generic_expr (stream, token->u.value, 0);
1137       break;
1138
1139     default:
1140       /* If we have a name for the token, print it out.  Otherwise, we
1141          simply give the numeric code.  */
1142       if (token->type < ARRAY_SIZE(token_names))
1143         fputs (token_names[token->type], stream);
1144       else
1145         fprintf (stream, "[%d]", token->type);
1146       break;
1147     }
1148 }
1149
1150 /* Start emitting debugging information.  */
1151
1152 static void
1153 cp_lexer_start_debugging (cp_lexer* lexer)
1154 {
1155   lexer->debugging_p = true;
1156   cp_lexer_debug_stream = stderr;
1157 }
1158
1159 /* Stop emitting debugging information.  */
1160
1161 static void
1162 cp_lexer_stop_debugging (cp_lexer* lexer)
1163 {
1164   lexer->debugging_p = false;
1165   cp_lexer_debug_stream = NULL;
1166 }
1167
1168 /* Create a new cp_token_cache, representing a range of tokens.  */
1169
1170 static cp_token_cache *
1171 cp_token_cache_new (cp_token *first, cp_token *last)
1172 {
1173   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1174   cache->first = first;
1175   cache->last = last;
1176   return cache;
1177 }
1178
1179 \f
1180 /* Decl-specifiers.  */
1181
1182 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1183
1184 static void
1185 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1186 {
1187   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1188 }
1189
1190 /* Declarators.  */
1191
1192 /* Nothing other than the parser should be creating declarators;
1193    declarators are a semi-syntactic representation of C++ entities.
1194    Other parts of the front end that need to create entities (like
1195    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1196
1197 static cp_declarator *make_call_declarator
1198   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1199 static cp_declarator *make_array_declarator
1200   (cp_declarator *, tree);
1201 static cp_declarator *make_pointer_declarator
1202   (cp_cv_quals, cp_declarator *);
1203 static cp_declarator *make_reference_declarator
1204   (cp_cv_quals, cp_declarator *, bool);
1205 static cp_parameter_declarator *make_parameter_declarator
1206   (cp_decl_specifier_seq *, cp_declarator *, tree);
1207 static cp_declarator *make_ptrmem_declarator
1208   (cp_cv_quals, tree, cp_declarator *);
1209
1210 /* An erroneous declarator.  */
1211 static cp_declarator *cp_error_declarator;
1212
1213 /* The obstack on which declarators and related data structures are
1214    allocated.  */
1215 static struct obstack declarator_obstack;
1216
1217 /* Alloc BYTES from the declarator memory pool.  */
1218
1219 static inline void *
1220 alloc_declarator (size_t bytes)
1221 {
1222   return obstack_alloc (&declarator_obstack, bytes);
1223 }
1224
1225 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1226    common to all declarators.  */
1227
1228 static cp_declarator *
1229 make_declarator (cp_declarator_kind kind)
1230 {
1231   cp_declarator *declarator;
1232
1233   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1234   declarator->kind = kind;
1235   declarator->attributes = NULL_TREE;
1236   declarator->declarator = NULL;
1237   declarator->parameter_pack_p = false;
1238   declarator->id_loc = UNKNOWN_LOCATION;
1239
1240   return declarator;
1241 }
1242
1243 /* Make a declarator for a generalized identifier.  If
1244    QUALIFYING_SCOPE is non-NULL, the identifier is
1245    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1246    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1247    is, if any.   */
1248
1249 static cp_declarator *
1250 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1251                     special_function_kind sfk)
1252 {
1253   cp_declarator *declarator;
1254
1255   /* It is valid to write:
1256
1257        class C { void f(); };
1258        typedef C D;
1259        void D::f();
1260
1261      The standard is not clear about whether `typedef const C D' is
1262      legal; as of 2002-09-15 the committee is considering that
1263      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1264      well.  */
1265   if (qualifying_scope && TYPE_P (qualifying_scope))
1266     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267
1268   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1269               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1270               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271
1272   declarator = make_declarator (cdk_id);
1273   declarator->u.id.qualifying_scope = qualifying_scope;
1274   declarator->u.id.unqualified_name = unqualified_name;
1275   declarator->u.id.sfk = sfk;
1276   
1277   return declarator;
1278 }
1279
1280 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1281    of modifiers such as const or volatile to apply to the pointer
1282    type, represented as identifiers.  */
1283
1284 cp_declarator *
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1286 {
1287   cp_declarator *declarator;
1288
1289   declarator = make_declarator (cdk_pointer);
1290   declarator->declarator = target;
1291   declarator->u.pointer.qualifiers = cv_qualifiers;
1292   declarator->u.pointer.class_type = NULL_TREE;
1293   if (target)
1294     {
1295       declarator->id_loc = target->id_loc;
1296       declarator->parameter_pack_p = target->parameter_pack_p;
1297       target->parameter_pack_p = false;
1298     }
1299   else
1300     declarator->parameter_pack_p = false;
1301
1302   return declarator;
1303 }
1304
1305 /* Like make_pointer_declarator -- but for references.  */
1306
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309                            bool rvalue_ref)
1310 {
1311   cp_declarator *declarator;
1312
1313   declarator = make_declarator (cdk_reference);
1314   declarator->declarator = target;
1315   declarator->u.reference.qualifiers = cv_qualifiers;
1316   declarator->u.reference.rvalue_ref = rvalue_ref;
1317   if (target)
1318     {
1319       declarator->id_loc = target->id_loc;
1320       declarator->parameter_pack_p = target->parameter_pack_p;
1321       target->parameter_pack_p = false;
1322     }
1323   else
1324     declarator->parameter_pack_p = false;
1325
1326   return declarator;
1327 }
1328
1329 /* Like make_pointer_declarator -- but for a pointer to a non-static
1330    member of CLASS_TYPE.  */
1331
1332 cp_declarator *
1333 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1334                         cp_declarator *pointee)
1335 {
1336   cp_declarator *declarator;
1337
1338   declarator = make_declarator (cdk_ptrmem);
1339   declarator->declarator = pointee;
1340   declarator->u.pointer.qualifiers = cv_qualifiers;
1341   declarator->u.pointer.class_type = class_type;
1342
1343   if (pointee)
1344     {
1345       declarator->parameter_pack_p = pointee->parameter_pack_p;
1346       pointee->parameter_pack_p = false;
1347     }
1348   else
1349     declarator->parameter_pack_p = false;
1350
1351   return declarator;
1352 }
1353
1354 /* Make a declarator for the function given by TARGET, with the
1355    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1356    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1357    indicates what exceptions can be thrown.  */
1358
1359 cp_declarator *
1360 make_call_declarator (cp_declarator *target,
1361                       tree parms,
1362                       cp_cv_quals cv_qualifiers,
1363                       cp_virt_specifiers virt_specifiers,
1364                       tree exception_specification,
1365                       tree late_return_type)
1366 {
1367   cp_declarator *declarator;
1368
1369   declarator = make_declarator (cdk_function);
1370   declarator->declarator = target;
1371   declarator->u.function.parameters = parms;
1372   declarator->u.function.qualifiers = cv_qualifiers;
1373   declarator->u.function.virt_specifiers = virt_specifiers;
1374   declarator->u.function.exception_specification = exception_specification;
1375   declarator->u.function.late_return_type = late_return_type;
1376   if (target)
1377     {
1378       declarator->id_loc = target->id_loc;
1379       declarator->parameter_pack_p = target->parameter_pack_p;
1380       target->parameter_pack_p = false;
1381     }
1382   else
1383     declarator->parameter_pack_p = false;
1384
1385   return declarator;
1386 }
1387
1388 /* Make a declarator for an array of BOUNDS elements, each of which is
1389    defined by ELEMENT.  */
1390
1391 cp_declarator *
1392 make_array_declarator (cp_declarator *element, tree bounds)
1393 {
1394   cp_declarator *declarator;
1395
1396   declarator = make_declarator (cdk_array);
1397   declarator->declarator = element;
1398   declarator->u.array.bounds = bounds;
1399   if (element)
1400     {
1401       declarator->id_loc = element->id_loc;
1402       declarator->parameter_pack_p = element->parameter_pack_p;
1403       element->parameter_pack_p = false;
1404     }
1405   else
1406     declarator->parameter_pack_p = false;
1407
1408   return declarator;
1409 }
1410
1411 /* Determine whether the declarator we've seen so far can be a
1412    parameter pack, when followed by an ellipsis.  */
1413 static bool 
1414 declarator_can_be_parameter_pack (cp_declarator *declarator)
1415 {
1416   /* Search for a declarator name, or any other declarator that goes
1417      after the point where the ellipsis could appear in a parameter
1418      pack. If we find any of these, then this declarator can not be
1419      made into a parameter pack.  */
1420   bool found = false;
1421   while (declarator && !found)
1422     {
1423       switch ((int)declarator->kind)
1424         {
1425         case cdk_id:
1426         case cdk_array:
1427           found = true;
1428           break;
1429
1430         case cdk_error:
1431           return true;
1432
1433         default:
1434           declarator = declarator->declarator;
1435           break;
1436         }
1437     }
1438
1439   return !found;
1440 }
1441
1442 cp_parameter_declarator *no_parameters;
1443
1444 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1445    DECLARATOR and DEFAULT_ARGUMENT.  */
1446
1447 cp_parameter_declarator *
1448 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1449                            cp_declarator *declarator,
1450                            tree default_argument)
1451 {
1452   cp_parameter_declarator *parameter;
1453
1454   parameter = ((cp_parameter_declarator *)
1455                alloc_declarator (sizeof (cp_parameter_declarator)));
1456   parameter->next = NULL;
1457   if (decl_specifiers)
1458     parameter->decl_specifiers = *decl_specifiers;
1459   else
1460     clear_decl_specs (&parameter->decl_specifiers);
1461   parameter->declarator = declarator;
1462   parameter->default_argument = default_argument;
1463   parameter->ellipsis_p = false;
1464
1465   return parameter;
1466 }
1467
1468 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1469
1470 static bool
1471 function_declarator_p (const cp_declarator *declarator)
1472 {
1473   while (declarator)
1474     {
1475       if (declarator->kind == cdk_function
1476           && declarator->declarator->kind == cdk_id)
1477         return true;
1478       if (declarator->kind == cdk_id
1479           || declarator->kind == cdk_error)
1480         return false;
1481       declarator = declarator->declarator;
1482     }
1483   return false;
1484 }
1485  
1486 /* The parser.  */
1487
1488 /* Overview
1489    --------
1490
1491    A cp_parser parses the token stream as specified by the C++
1492    grammar.  Its job is purely parsing, not semantic analysis.  For
1493    example, the parser breaks the token stream into declarators,
1494    expressions, statements, and other similar syntactic constructs.
1495    It does not check that the types of the expressions on either side
1496    of an assignment-statement are compatible, or that a function is
1497    not declared with a parameter of type `void'.
1498
1499    The parser invokes routines elsewhere in the compiler to perform
1500    semantic analysis and to build up the abstract syntax tree for the
1501    code processed.
1502
1503    The parser (and the template instantiation code, which is, in a
1504    way, a close relative of parsing) are the only parts of the
1505    compiler that should be calling push_scope and pop_scope, or
1506    related functions.  The parser (and template instantiation code)
1507    keeps track of what scope is presently active; everything else
1508    should simply honor that.  (The code that generates static
1509    initializers may also need to set the scope, in order to check
1510    access control correctly when emitting the initializers.)
1511
1512    Methodology
1513    -----------
1514
1515    The parser is of the standard recursive-descent variety.  Upcoming
1516    tokens in the token stream are examined in order to determine which
1517    production to use when parsing a non-terminal.  Some C++ constructs
1518    require arbitrary look ahead to disambiguate.  For example, it is
1519    impossible, in the general case, to tell whether a statement is an
1520    expression or declaration without scanning the entire statement.
1521    Therefore, the parser is capable of "parsing tentatively."  When the
1522    parser is not sure what construct comes next, it enters this mode.
1523    Then, while we attempt to parse the construct, the parser queues up
1524    error messages, rather than issuing them immediately, and saves the
1525    tokens it consumes.  If the construct is parsed successfully, the
1526    parser "commits", i.e., it issues any queued error messages and
1527    the tokens that were being preserved are permanently discarded.
1528    If, however, the construct is not parsed successfully, the parser
1529    rolls back its state completely so that it can resume parsing using
1530    a different alternative.
1531
1532    Future Improvements
1533    -------------------
1534
1535    The performance of the parser could probably be improved substantially.
1536    We could often eliminate the need to parse tentatively by looking ahead
1537    a little bit.  In some places, this approach might not entirely eliminate
1538    the need to parse tentatively, but it might still speed up the average
1539    case.  */
1540
1541 /* Flags that are passed to some parsing functions.  These values can
1542    be bitwise-ored together.  */
1543
1544 enum
1545 {
1546   /* No flags.  */
1547   CP_PARSER_FLAGS_NONE = 0x0,
1548   /* The construct is optional.  If it is not present, then no error
1549      should be issued.  */
1550   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1551   /* When parsing a type-specifier, treat user-defined type-names
1552      as non-type identifiers.  */
1553   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1554   /* When parsing a type-specifier, do not try to parse a class-specifier
1555      or enum-specifier.  */
1556   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1557   /* When parsing a decl-specifier-seq, only allow type-specifier or
1558      constexpr.  */
1559   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1560 };
1561
1562 /* This type is used for parameters and variables which hold
1563    combinations of the above flags.  */
1564 typedef int cp_parser_flags;
1565
1566 /* The different kinds of declarators we want to parse.  */
1567
1568 typedef enum cp_parser_declarator_kind
1569 {
1570   /* We want an abstract declarator.  */
1571   CP_PARSER_DECLARATOR_ABSTRACT,
1572   /* We want a named declarator.  */
1573   CP_PARSER_DECLARATOR_NAMED,
1574   /* We don't mind, but the name must be an unqualified-id.  */
1575   CP_PARSER_DECLARATOR_EITHER
1576 } cp_parser_declarator_kind;
1577
1578 /* The precedence values used to parse binary expressions.  The minimum value
1579    of PREC must be 1, because zero is reserved to quickly discriminate
1580    binary operators from other tokens.  */
1581
1582 enum cp_parser_prec
1583 {
1584   PREC_NOT_OPERATOR,
1585   PREC_LOGICAL_OR_EXPRESSION,
1586   PREC_LOGICAL_AND_EXPRESSION,
1587   PREC_INCLUSIVE_OR_EXPRESSION,
1588   PREC_EXCLUSIVE_OR_EXPRESSION,
1589   PREC_AND_EXPRESSION,
1590   PREC_EQUALITY_EXPRESSION,
1591   PREC_RELATIONAL_EXPRESSION,
1592   PREC_SHIFT_EXPRESSION,
1593   PREC_ADDITIVE_EXPRESSION,
1594   PREC_MULTIPLICATIVE_EXPRESSION,
1595   PREC_PM_EXPRESSION,
1596   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1597 };
1598
1599 /* A mapping from a token type to a corresponding tree node type, with a
1600    precedence value.  */
1601
1602 typedef struct cp_parser_binary_operations_map_node
1603 {
1604   /* The token type.  */
1605   enum cpp_ttype token_type;
1606   /* The corresponding tree code.  */
1607   enum tree_code tree_type;
1608   /* The precedence of this operator.  */
1609   enum cp_parser_prec prec;
1610 } cp_parser_binary_operations_map_node;
1611
1612 typedef struct cp_parser_expression_stack_entry
1613 {
1614   /* Left hand side of the binary operation we are currently
1615      parsing.  */
1616   tree lhs;
1617   /* Original tree code for left hand side, if it was a binary
1618      expression itself (used for -Wparentheses).  */
1619   enum tree_code lhs_type;
1620   /* Tree code for the binary operation we are parsing.  */
1621   enum tree_code tree_type;
1622   /* Precedence of the binary operation we are parsing.  */
1623   enum cp_parser_prec prec;
1624 } cp_parser_expression_stack_entry;
1625
1626 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1627    entries because precedence levels on the stack are monotonically
1628    increasing.  */
1629 typedef struct cp_parser_expression_stack_entry
1630   cp_parser_expression_stack[NUM_PREC_VALUES];
1631
1632 /* Prototypes.  */
1633
1634 /* Constructors and destructors.  */
1635
1636 static cp_parser_context *cp_parser_context_new
1637   (cp_parser_context *);
1638
1639 /* Class variables.  */
1640
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1642
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644    Transformed into an associative array (binops_by_token) by
1645    cp_parser_new.  */
1646
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1650
1651   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654
1655   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657
1658   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660
1661   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665
1666   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1668
1669   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1670
1671   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1672
1673   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1674
1675   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1676
1677   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1678 };
1679
1680 /* The same as binops, but initialized by cp_parser_new so that
1681    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1682    for speed.  */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1684
1685 /* Constructors and destructors.  */
1686
1687 /* Construct a new context.  The context below this one on the stack
1688    is given by NEXT.  */
1689
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1692 {
1693   cp_parser_context *context;
1694
1695   /* Allocate the storage.  */
1696   if (cp_parser_context_free_list != NULL)
1697     {
1698       /* Pull the first entry from the free list.  */
1699       context = cp_parser_context_free_list;
1700       cp_parser_context_free_list = context->next;
1701       memset (context, 0, sizeof (*context));
1702     }
1703   else
1704     context = ggc_alloc_cleared_cp_parser_context ();
1705
1706   /* No errors have occurred yet in this context.  */
1707   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708   /* If this is not the bottommost context, copy information that we
1709      need from the previous context.  */
1710   if (next)
1711     {
1712       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713          expression, then we are parsing one in this context, too.  */
1714       context->object_type = next->object_type;
1715       /* Thread the stack.  */
1716       context->next = next;
1717     }
1718
1719   return context;
1720 }
1721
1722 /* Managing the unparsed function queues.  */
1723
1724 #define unparsed_funs_with_default_args \
1725   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728 #define unparsed_nsdmis \
1729   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1730
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1733 {
1734   VEC_safe_push (cp_unparsed_functions_entry, gc,
1735                  parser->unparsed_queues, NULL);
1736   unparsed_funs_with_default_args = NULL;
1737   unparsed_funs_with_definitions = make_tree_vector ();
1738   unparsed_nsdmis = NULL;
1739 }
1740
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1743 {
1744   release_tree_vector (unparsed_funs_with_definitions);
1745   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1746 }
1747
1748 /* Prototypes.  */
1749
1750 /* Constructors and destructors.  */
1751
1752 static cp_parser *cp_parser_new
1753   (void);
1754
1755 /* Routines to parse various constructs.
1756
1757    Those that return `tree' will return the error_mark_node (rather
1758    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759    Sometimes, they will return an ordinary node if error-recovery was
1760    attempted, even though a parse error occurred.  So, to check
1761    whether or not a parse error occurred, you should always use
1762    cp_parser_error_occurred.  If the construct is optional (indicated
1763    either by an `_opt' in the name of the function that does the
1764    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765    the construct is not present.  */
1766
1767 /* Lexical conventions [gram.lex]  */
1768
1769 static tree cp_parser_identifier
1770   (cp_parser *);
1771 static tree cp_parser_string_literal
1772   (cp_parser *, bool, bool);
1773 static tree cp_parser_userdef_char_literal
1774   (cp_parser *);
1775 static tree cp_parser_userdef_string_literal
1776   (cp_token *);
1777 static tree cp_parser_userdef_numeric_literal
1778   (cp_parser *);
1779
1780 /* Basic concepts [gram.basic]  */
1781
1782 static bool cp_parser_translation_unit
1783   (cp_parser *);
1784
1785 /* Expressions [gram.expr]  */
1786
1787 static tree cp_parser_primary_expression
1788   (cp_parser *, bool, bool, bool, cp_id_kind *);
1789 static tree cp_parser_id_expression
1790   (cp_parser *, bool, bool, bool *, bool, bool);
1791 static tree cp_parser_unqualified_id
1792   (cp_parser *, bool, bool, bool, bool);
1793 static tree cp_parser_nested_name_specifier_opt
1794   (cp_parser *, bool, bool, bool, bool);
1795 static tree cp_parser_nested_name_specifier
1796   (cp_parser *, bool, bool, bool, bool);
1797 static tree cp_parser_qualifying_entity
1798   (cp_parser *, bool, bool, bool, bool, bool);
1799 static tree cp_parser_postfix_expression
1800   (cp_parser *, bool, bool, bool, cp_id_kind *);
1801 static tree cp_parser_postfix_open_square_expression
1802   (cp_parser *, tree, bool);
1803 static tree cp_parser_postfix_dot_deref_expression
1804   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1806   (cp_parser *, int, bool, bool, bool *);
1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1809 static void cp_parser_pseudo_destructor_name
1810   (cp_parser *, tree *, tree *);
1811 static tree cp_parser_unary_expression
1812   (cp_parser *, bool, bool, cp_id_kind *);
1813 static enum tree_code cp_parser_unary_operator
1814   (cp_token *);
1815 static tree cp_parser_new_expression
1816   (cp_parser *);
1817 static VEC(tree,gc) *cp_parser_new_placement
1818   (cp_parser *);
1819 static tree cp_parser_new_type_id
1820   (cp_parser *, tree *);
1821 static cp_declarator *cp_parser_new_declarator_opt
1822   (cp_parser *);
1823 static cp_declarator *cp_parser_direct_new_declarator
1824   (cp_parser *);
1825 static VEC(tree,gc) *cp_parser_new_initializer
1826   (cp_parser *);
1827 static tree cp_parser_delete_expression
1828   (cp_parser *);
1829 static tree cp_parser_cast_expression
1830   (cp_parser *, bool, bool, cp_id_kind *);
1831 static tree cp_parser_binary_expression
1832   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1833 static tree cp_parser_question_colon_clause
1834   (cp_parser *, tree);
1835 static tree cp_parser_assignment_expression
1836   (cp_parser *, bool, cp_id_kind *);
1837 static enum tree_code cp_parser_assignment_operator_opt
1838   (cp_parser *);
1839 static tree cp_parser_expression
1840   (cp_parser *, bool, cp_id_kind *);
1841 static tree cp_parser_constant_expression
1842   (cp_parser *, bool, bool *);
1843 static tree cp_parser_builtin_offsetof
1844   (cp_parser *);
1845 static tree cp_parser_lambda_expression
1846   (cp_parser *);
1847 static void cp_parser_lambda_introducer
1848   (cp_parser *, tree);
1849 static bool cp_parser_lambda_declarator_opt
1850   (cp_parser *, tree);
1851 static void cp_parser_lambda_body
1852   (cp_parser *, tree);
1853
1854 /* Statements [gram.stmt.stmt]  */
1855
1856 static void cp_parser_statement
1857   (cp_parser *, tree, bool, bool *);
1858 static void cp_parser_label_for_labeled_statement
1859   (cp_parser *);
1860 static tree cp_parser_expression_statement
1861   (cp_parser *, tree);
1862 static tree cp_parser_compound_statement
1863   (cp_parser *, tree, bool, bool);
1864 static void cp_parser_statement_seq_opt
1865   (cp_parser *, tree);
1866 static tree cp_parser_selection_statement
1867   (cp_parser *, bool *);
1868 static tree cp_parser_condition
1869   (cp_parser *);
1870 static tree cp_parser_iteration_statement
1871   (cp_parser *);
1872 static bool cp_parser_for_init_statement
1873   (cp_parser *, tree *decl);
1874 static tree cp_parser_for
1875   (cp_parser *);
1876 static tree cp_parser_c_for
1877   (cp_parser *, tree, tree);
1878 static tree cp_parser_range_for
1879   (cp_parser *, tree, tree, tree);
1880 static void do_range_for_auto_deduction
1881   (tree, tree);
1882 static tree cp_parser_perform_range_for_lookup
1883   (tree, tree *, tree *);
1884 static tree cp_parser_range_for_member_function
1885   (tree, tree);
1886 static tree cp_parser_jump_statement
1887   (cp_parser *);
1888 static void cp_parser_declaration_statement
1889   (cp_parser *);
1890
1891 static tree cp_parser_implicitly_scoped_statement
1892   (cp_parser *, bool *);
1893 static void cp_parser_already_scoped_statement
1894   (cp_parser *);
1895
1896 /* Declarations [gram.dcl.dcl] */
1897
1898 static void cp_parser_declaration_seq_opt
1899   (cp_parser *);
1900 static void cp_parser_declaration
1901   (cp_parser *);
1902 static void cp_parser_block_declaration
1903   (cp_parser *, bool);
1904 static void cp_parser_simple_declaration
1905   (cp_parser *, bool, tree *);
1906 static void cp_parser_decl_specifier_seq
1907   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1908 static tree cp_parser_storage_class_specifier_opt
1909   (cp_parser *);
1910 static tree cp_parser_function_specifier_opt
1911   (cp_parser *, cp_decl_specifier_seq *);
1912 static tree cp_parser_type_specifier
1913   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1914    int *, bool *);
1915 static tree cp_parser_simple_type_specifier
1916   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1917 static tree cp_parser_type_name
1918   (cp_parser *);
1919 static tree cp_parser_nonclass_name 
1920   (cp_parser* parser);
1921 static tree cp_parser_elaborated_type_specifier
1922   (cp_parser *, bool, bool);
1923 static tree cp_parser_enum_specifier
1924   (cp_parser *);
1925 static void cp_parser_enumerator_list
1926   (cp_parser *, tree);
1927 static void cp_parser_enumerator_definition
1928   (cp_parser *, tree);
1929 static tree cp_parser_namespace_name
1930   (cp_parser *);
1931 static void cp_parser_namespace_definition
1932   (cp_parser *);
1933 static void cp_parser_namespace_body
1934   (cp_parser *);
1935 static tree cp_parser_qualified_namespace_specifier
1936   (cp_parser *);
1937 static void cp_parser_namespace_alias_definition
1938   (cp_parser *);
1939 static bool cp_parser_using_declaration
1940   (cp_parser *, bool);
1941 static void cp_parser_using_directive
1942   (cp_parser *);
1943 static tree cp_parser_alias_declaration
1944   (cp_parser *);
1945 static void cp_parser_asm_definition
1946   (cp_parser *);
1947 static void cp_parser_linkage_specification
1948   (cp_parser *);
1949 static void cp_parser_static_assert
1950   (cp_parser *, bool);
1951 static tree cp_parser_decltype
1952   (cp_parser *);
1953
1954 /* Declarators [gram.dcl.decl] */
1955
1956 static tree cp_parser_init_declarator
1957   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1958 static cp_declarator *cp_parser_declarator
1959   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1960 static cp_declarator *cp_parser_direct_declarator
1961   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1962 static enum tree_code cp_parser_ptr_operator
1963   (cp_parser *, tree *, cp_cv_quals *);
1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1965   (cp_parser *);
1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1967   (cp_parser *);
1968 static tree cp_parser_late_return_type_opt
1969   (cp_parser *, cp_cv_quals);
1970 static tree cp_parser_declarator_id
1971   (cp_parser *, bool);
1972 static tree cp_parser_type_id
1973   (cp_parser *);
1974 static tree cp_parser_template_type_arg
1975   (cp_parser *);
1976 static tree cp_parser_trailing_type_id (cp_parser *);
1977 static tree cp_parser_type_id_1
1978   (cp_parser *, bool, bool);
1979 static void cp_parser_type_specifier_seq
1980   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1981 static tree cp_parser_parameter_declaration_clause
1982   (cp_parser *);
1983 static tree cp_parser_parameter_declaration_list
1984   (cp_parser *, bool *);
1985 static cp_parameter_declarator *cp_parser_parameter_declaration
1986   (cp_parser *, bool, bool *);
1987 static tree cp_parser_default_argument 
1988   (cp_parser *, bool);
1989 static void cp_parser_function_body
1990   (cp_parser *);
1991 static tree cp_parser_initializer
1992   (cp_parser *, bool *, bool *);
1993 static tree cp_parser_initializer_clause
1994   (cp_parser *, bool *);
1995 static tree cp_parser_braced_list
1996   (cp_parser*, bool*);
1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1998   (cp_parser *, bool *);
1999
2000 static bool cp_parser_ctor_initializer_opt_and_function_body
2001   (cp_parser *);
2002
2003 /* Classes [gram.class] */
2004
2005 static tree cp_parser_class_name
2006   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2007 static tree cp_parser_class_specifier
2008   (cp_parser *);
2009 static tree cp_parser_class_head
2010   (cp_parser *, bool *, tree *, tree *);
2011 static enum tag_types cp_parser_class_key
2012   (cp_parser *);
2013 static void cp_parser_member_specification_opt
2014   (cp_parser *);
2015 static void cp_parser_member_declaration
2016   (cp_parser *);
2017 static tree cp_parser_pure_specifier
2018   (cp_parser *);
2019 static tree cp_parser_constant_initializer
2020   (cp_parser *);
2021
2022 /* Derived classes [gram.class.derived] */
2023
2024 static tree cp_parser_base_clause
2025   (cp_parser *);
2026 static tree cp_parser_base_specifier
2027   (cp_parser *);
2028
2029 /* Special member functions [gram.special] */
2030
2031 static tree cp_parser_conversion_function_id
2032   (cp_parser *);
2033 static tree cp_parser_conversion_type_id
2034   (cp_parser *);
2035 static cp_declarator *cp_parser_conversion_declarator_opt
2036   (cp_parser *);
2037 static bool cp_parser_ctor_initializer_opt
2038   (cp_parser *);
2039 static void cp_parser_mem_initializer_list
2040   (cp_parser *);
2041 static tree cp_parser_mem_initializer
2042   (cp_parser *);
2043 static tree cp_parser_mem_initializer_id
2044   (cp_parser *);
2045
2046 /* Overloading [gram.over] */
2047
2048 static tree cp_parser_operator_function_id
2049   (cp_parser *);
2050 static tree cp_parser_operator
2051   (cp_parser *);
2052
2053 /* Templates [gram.temp] */
2054
2055 static void cp_parser_template_declaration
2056   (cp_parser *, bool);
2057 static tree cp_parser_template_parameter_list
2058   (cp_parser *);
2059 static tree cp_parser_template_parameter
2060   (cp_parser *, bool *, bool *);
2061 static tree cp_parser_type_parameter
2062   (cp_parser *, bool *);
2063 static tree cp_parser_template_id
2064   (cp_parser *, bool, bool, bool);
2065 static tree cp_parser_template_name
2066   (cp_parser *, bool, bool, bool, bool *);
2067 static tree cp_parser_template_argument_list
2068   (cp_parser *);
2069 static tree cp_parser_template_argument
2070   (cp_parser *);
2071 static void cp_parser_explicit_instantiation
2072   (cp_parser *);
2073 static void cp_parser_explicit_specialization
2074   (cp_parser *);
2075
2076 /* Exception handling [gram.exception] */
2077
2078 static tree cp_parser_try_block
2079   (cp_parser *);
2080 static bool cp_parser_function_try_block
2081   (cp_parser *);
2082 static void cp_parser_handler_seq
2083   (cp_parser *);
2084 static void cp_parser_handler
2085   (cp_parser *);
2086 static tree cp_parser_exception_declaration
2087   (cp_parser *);
2088 static tree cp_parser_throw_expression
2089   (cp_parser *);
2090 static tree cp_parser_exception_specification_opt
2091   (cp_parser *);
2092 static tree cp_parser_type_id_list
2093   (cp_parser *);
2094
2095 /* GNU Extensions */
2096
2097 static tree cp_parser_asm_specification_opt
2098   (cp_parser *);
2099 static tree cp_parser_asm_operand_list
2100   (cp_parser *);
2101 static tree cp_parser_asm_clobber_list
2102   (cp_parser *);
2103 static tree cp_parser_asm_label_list
2104   (cp_parser *);
2105 static tree cp_parser_attributes_opt
2106   (cp_parser *);
2107 static tree cp_parser_attribute_list
2108   (cp_parser *);
2109 static bool cp_parser_extension_opt
2110   (cp_parser *, int *);
2111 static void cp_parser_label_declaration
2112   (cp_parser *);
2113
2114 /* Transactional Memory Extensions */
2115
2116 static tree cp_parser_transaction
2117   (cp_parser *, enum rid);
2118 static tree cp_parser_transaction_expression
2119   (cp_parser *, enum rid);
2120 static bool cp_parser_function_transaction
2121   (cp_parser *, enum rid);
2122 static tree cp_parser_transaction_cancel
2123   (cp_parser *);
2124
2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2126 static bool cp_parser_pragma
2127   (cp_parser *, enum pragma_context);
2128
2129 /* Objective-C++ Productions */
2130
2131 static tree cp_parser_objc_message_receiver
2132   (cp_parser *);
2133 static tree cp_parser_objc_message_args
2134   (cp_parser *);
2135 static tree cp_parser_objc_message_expression
2136   (cp_parser *);
2137 static tree cp_parser_objc_encode_expression
2138   (cp_parser *);
2139 static tree cp_parser_objc_defs_expression
2140   (cp_parser *);
2141 static tree cp_parser_objc_protocol_expression
2142   (cp_parser *);
2143 static tree cp_parser_objc_selector_expression
2144   (cp_parser *);
2145 static tree cp_parser_objc_expression
2146   (cp_parser *);
2147 static bool cp_parser_objc_selector_p
2148   (enum cpp_ttype);
2149 static tree cp_parser_objc_selector
2150   (cp_parser *);
2151 static tree cp_parser_objc_protocol_refs_opt
2152   (cp_parser *);
2153 static void cp_parser_objc_declaration
2154   (cp_parser *, tree);
2155 static tree cp_parser_objc_statement
2156   (cp_parser *);
2157 static bool cp_parser_objc_valid_prefix_attributes
2158   (cp_parser *, tree *);
2159 static void cp_parser_objc_at_property_declaration 
2160   (cp_parser *) ;
2161 static void cp_parser_objc_at_synthesize_declaration 
2162   (cp_parser *) ;
2163 static void cp_parser_objc_at_dynamic_declaration
2164   (cp_parser *) ;
2165 static tree cp_parser_objc_struct_declaration
2166   (cp_parser *) ;
2167
2168 /* Utility Routines */
2169
2170 static tree cp_parser_lookup_name
2171   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2172 static tree cp_parser_lookup_name_simple
2173   (cp_parser *, tree, location_t);
2174 static tree cp_parser_maybe_treat_template_as_class
2175   (tree, bool);
2176 static bool cp_parser_check_declarator_template_parameters
2177   (cp_parser *, cp_declarator *, location_t);
2178 static bool cp_parser_check_template_parameters
2179   (cp_parser *, unsigned, location_t, cp_declarator *);
2180 static tree cp_parser_simple_cast_expression
2181   (cp_parser *);
2182 static tree cp_parser_global_scope_opt
2183   (cp_parser *, bool);
2184 static bool cp_parser_constructor_declarator_p
2185   (cp_parser *, bool);
2186 static tree cp_parser_function_definition_from_specifiers_and_declarator
2187   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2188 static tree cp_parser_function_definition_after_declarator
2189   (cp_parser *, bool);
2190 static void cp_parser_template_declaration_after_export
2191   (cp_parser *, bool);
2192 static void cp_parser_perform_template_parameter_access_checks
2193   (VEC (deferred_access_check,gc)*);
2194 static tree cp_parser_single_declaration
2195   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2196 static tree cp_parser_functional_cast
2197   (cp_parser *, tree);
2198 static tree cp_parser_save_member_function_body
2199   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2200 static tree cp_parser_save_nsdmi
2201   (cp_parser *);
2202 static tree cp_parser_enclosed_template_argument_list
2203   (cp_parser *);
2204 static void cp_parser_save_default_args
2205   (cp_parser *, tree);
2206 static void cp_parser_late_parsing_for_member
2207   (cp_parser *, tree);
2208 static tree cp_parser_late_parse_one_default_arg
2209   (cp_parser *, tree, tree, tree);
2210 static void cp_parser_late_parsing_nsdmi
2211   (cp_parser *, tree);
2212 static void cp_parser_late_parsing_default_args
2213   (cp_parser *, tree);
2214 static tree cp_parser_sizeof_operand
2215   (cp_parser *, enum rid);
2216 static tree cp_parser_trait_expr
2217   (cp_parser *, enum rid);
2218 static bool cp_parser_declares_only_class_p
2219   (cp_parser *);
2220 static void cp_parser_set_storage_class
2221   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2222 static void cp_parser_set_decl_spec_type
2223   (cp_decl_specifier_seq *, tree, location_t, bool);
2224 static bool cp_parser_friend_p
2225   (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227   (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229   (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231   (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2233   (cp_token *);
2234 static bool cp_parser_next_token_starts_class_definition_p
2235   (cp_parser *);
2236 static bool cp_parser_next_token_ends_template_argument_p
2237   (cp_parser *);
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239   (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2241   (cp_token *);
2242 static void cp_parser_check_class_key
2243   (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245   (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2247   (cp_parser *);
2248 static void cp_parser_pre_parsed_nested_name_specifier
2249   (cp_parser *);
2250 static bool cp_parser_cache_group
2251   (cp_parser *, enum cpp_ttype, unsigned);
2252 static void cp_parser_parse_tentatively
2253   (cp_parser *);
2254 static void cp_parser_commit_to_tentative_parse
2255   (cp_parser *);
2256 static void cp_parser_abort_tentative_parse
2257   (cp_parser *);
2258 static bool cp_parser_parse_definitely
2259   (cp_parser *);
2260 static inline bool cp_parser_parsing_tentatively
2261   (cp_parser *);
2262 static bool cp_parser_uncommitted_to_tentative_parse_p
2263   (cp_parser *);
2264 static void cp_parser_error
2265   (cp_parser *, const char *);
2266 static void cp_parser_name_lookup_error
2267   (cp_parser *, tree, tree, name_lookup_error, location_t);
2268 static bool cp_parser_simulate_error
2269   (cp_parser *);
2270 static bool cp_parser_check_type_definition
2271   (cp_parser *);
2272 static void cp_parser_check_for_definition_in_return_type
2273   (cp_declarator *, tree, location_t type_location);
2274 static void cp_parser_check_for_invalid_template_id
2275   (cp_parser *, tree, location_t location);
2276 static bool cp_parser_non_integral_constant_expression
2277   (cp_parser *, non_integral_constant);
2278 static void cp_parser_diagnose_invalid_type_name
2279   (cp_parser *, tree, tree, location_t);
2280 static bool cp_parser_parse_and_diagnose_invalid_type_name
2281   (cp_parser *);
2282 static int cp_parser_skip_to_closing_parenthesis
2283   (cp_parser *, bool, bool, bool);
2284 static void cp_parser_skip_to_end_of_statement
2285   (cp_parser *);
2286 static void cp_parser_consume_semicolon_at_end_of_statement
2287   (cp_parser *);
2288 static void cp_parser_skip_to_end_of_block_or_statement
2289   (cp_parser *);
2290 static bool cp_parser_skip_to_closing_brace
2291   (cp_parser *);
2292 static void cp_parser_skip_to_end_of_template_parameter_list
2293   (cp_parser *);
2294 static void cp_parser_skip_to_pragma_eol
2295   (cp_parser*, cp_token *);
2296 static bool cp_parser_error_occurred
2297   (cp_parser *);
2298 static bool cp_parser_allow_gnu_extensions_p
2299   (cp_parser *);
2300 static bool cp_parser_is_pure_string_literal
2301   (cp_token *);
2302 static bool cp_parser_is_string_literal
2303   (cp_token *);
2304 static bool cp_parser_is_keyword
2305   (cp_token *, enum rid);
2306 static tree cp_parser_make_typename_type
2307   (cp_parser *, tree, tree, location_t location);
2308 static cp_declarator * cp_parser_make_indirect_declarator
2309   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2310
2311 /* Returns nonzero if we are parsing tentatively.  */
2312
2313 static inline bool
2314 cp_parser_parsing_tentatively (cp_parser* parser)
2315 {
2316   return parser->context->next != NULL;
2317 }
2318
2319 /* Returns nonzero if TOKEN is a string literal.  */
2320
2321 static bool
2322 cp_parser_is_pure_string_literal (cp_token* token)
2323 {
2324   return (token->type == CPP_STRING ||
2325           token->type == CPP_STRING16 ||
2326           token->type == CPP_STRING32 ||
2327           token->type == CPP_WSTRING ||
2328           token->type == CPP_UTF8STRING);
2329 }
2330
2331 /* Returns nonzero if TOKEN is a string literal
2332    of a user-defined string literal.  */
2333
2334 static bool
2335 cp_parser_is_string_literal (cp_token* token)
2336 {
2337   return (cp_parser_is_pure_string_literal (token) ||
2338           token->type == CPP_STRING_USERDEF ||
2339           token->type == CPP_STRING16_USERDEF ||
2340           token->type == CPP_STRING32_USERDEF ||
2341           token->type == CPP_WSTRING_USERDEF ||
2342           token->type == CPP_UTF8STRING_USERDEF);
2343 }
2344
2345 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2346
2347 static bool
2348 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2349 {
2350   return token->keyword == keyword;
2351 }
2352
2353 /* If not parsing tentatively, issue a diagnostic of the form
2354       FILE:LINE: MESSAGE before TOKEN
2355    where TOKEN is the next token in the input stream.  MESSAGE
2356    (specified by the caller) is usually of the form "expected
2357    OTHER-TOKEN".  */
2358
2359 static void
2360 cp_parser_error (cp_parser* parser, const char* gmsgid)
2361 {
2362   if (!cp_parser_simulate_error (parser))
2363     {
2364       cp_token *token = cp_lexer_peek_token (parser->lexer);
2365       /* This diagnostic makes more sense if it is tagged to the line
2366          of the token we just peeked at.  */
2367       cp_lexer_set_source_position_from_token (token);
2368
2369       if (token->type == CPP_PRAGMA)
2370         {
2371           error_at (token->location,
2372                     "%<#pragma%> is not allowed here");
2373           cp_parser_skip_to_pragma_eol (parser, token);
2374           return;
2375         }
2376
2377       c_parse_error (gmsgid,
2378                      /* Because c_parser_error does not understand
2379                         CPP_KEYWORD, keywords are treated like
2380                         identifiers.  */
2381                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2382                      token->u.value, token->flags);
2383     }
2384 }
2385
2386 /* Issue an error about name-lookup failing.  NAME is the
2387    IDENTIFIER_NODE DECL is the result of
2388    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2389    the thing that we hoped to find.  */
2390
2391 static void
2392 cp_parser_name_lookup_error (cp_parser* parser,
2393                              tree name,
2394                              tree decl,
2395                              name_lookup_error desired,
2396                              location_t location)
2397 {
2398   /* If name lookup completely failed, tell the user that NAME was not
2399      declared.  */
2400   if (decl == error_mark_node)
2401     {
2402       if (parser->scope && parser->scope != global_namespace)
2403         error_at (location, "%<%E::%E%> has not been declared",
2404                   parser->scope, name);
2405       else if (parser->scope == global_namespace)
2406         error_at (location, "%<::%E%> has not been declared", name);
2407       else if (parser->object_scope
2408                && !CLASS_TYPE_P (parser->object_scope))
2409         error_at (location, "request for member %qE in non-class type %qT",
2410                   name, parser->object_scope);
2411       else if (parser->object_scope)
2412         error_at (location, "%<%T::%E%> has not been declared",
2413                   parser->object_scope, name);
2414       else
2415         error_at (location, "%qE has not been declared", name);
2416     }
2417   else if (parser->scope && parser->scope != global_namespace)
2418     {
2419       switch (desired)
2420         {
2421           case NLE_TYPE:
2422             error_at (location, "%<%E::%E%> is not a type",
2423                                 parser->scope, name);
2424             break;
2425           case NLE_CXX98:
2426             error_at (location, "%<%E::%E%> is not a class or namespace",
2427                                 parser->scope, name);
2428             break;
2429           case NLE_NOT_CXX98:
2430             error_at (location,
2431                       "%<%E::%E%> is not a class, namespace, or enumeration",
2432                       parser->scope, name);
2433             break;
2434           default:
2435             gcc_unreachable ();
2436             
2437         }
2438     }
2439   else if (parser->scope == global_namespace)
2440     {
2441       switch (desired)
2442         {
2443           case NLE_TYPE:
2444             error_at (location, "%<::%E%> is not a type", name);
2445             break;
2446           case NLE_CXX98:
2447             error_at (location, "%<::%E%> is not a class or namespace", name);
2448             break;
2449           case NLE_NOT_CXX98:
2450             error_at (location,
2451                       "%<::%E%> is not a class, namespace, or enumeration",
2452                       name);
2453             break;
2454           default:
2455             gcc_unreachable ();
2456         }
2457     }
2458   else
2459     {
2460       switch (desired)
2461         {
2462           case NLE_TYPE:
2463             error_at (location, "%qE is not a type", name);
2464             break;
2465           case NLE_CXX98:
2466             error_at (location, "%qE is not a class or namespace", name);
2467             break;
2468           case NLE_NOT_CXX98:
2469             error_at (location,
2470                       "%qE is not a class, namespace, or enumeration", name);
2471             break;
2472           default:
2473             gcc_unreachable ();
2474         }
2475     }
2476 }
2477
2478 /* If we are parsing tentatively, remember that an error has occurred
2479    during this tentative parse.  Returns true if the error was
2480    simulated; false if a message should be issued by the caller.  */
2481
2482 static bool
2483 cp_parser_simulate_error (cp_parser* parser)
2484 {
2485   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2486     {
2487       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2488       return true;
2489     }
2490   return false;
2491 }
2492
2493 /* Check for repeated decl-specifiers.  */
2494
2495 static void
2496 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2497                            location_t location)
2498 {
2499   int ds;
2500
2501   for (ds = ds_first; ds != ds_last; ++ds)
2502     {
2503       unsigned count = decl_specs->specs[ds];
2504       if (count < 2)
2505         continue;
2506       /* The "long" specifier is a special case because of "long long".  */
2507       if (ds == ds_long)
2508         {
2509           if (count > 2)
2510             error_at (location, "%<long long long%> is too long for GCC");
2511           else 
2512             pedwarn_cxx98 (location, OPT_Wlong_long, 
2513                            "ISO C++ 1998 does not support %<long long%>");
2514         }
2515       else if (count > 1)
2516         {
2517           static const char *const decl_spec_names[] = {
2518             "signed",
2519             "unsigned",
2520             "short",
2521             "long",
2522             "const",
2523             "volatile",
2524             "restrict",
2525             "inline",
2526             "virtual",
2527             "explicit",
2528             "friend",
2529             "typedef",
2530             "using",
2531             "constexpr",
2532             "__complex",
2533             "__thread"
2534           };
2535           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2536         }
2537     }
2538 }
2539
2540 /* This function is called when a type is defined.  If type
2541    definitions are forbidden at this point, an error message is
2542    issued.  */
2543
2544 static bool
2545 cp_parser_check_type_definition (cp_parser* parser)
2546 {
2547   /* If types are forbidden here, issue a message.  */
2548   if (parser->type_definition_forbidden_message)
2549     {
2550       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2551          in the message need to be interpreted.  */
2552       error (parser->type_definition_forbidden_message);
2553       return false;
2554     }
2555   return true;
2556 }
2557
2558 /* This function is called when the DECLARATOR is processed.  The TYPE
2559    was a type defined in the decl-specifiers.  If it is invalid to
2560    define a type in the decl-specifiers for DECLARATOR, an error is
2561    issued. TYPE_LOCATION is the location of TYPE and is used
2562    for error reporting.  */
2563
2564 static void
2565 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2566                                                tree type, location_t type_location)
2567 {
2568   /* [dcl.fct] forbids type definitions in return types.
2569      Unfortunately, it's not easy to know whether or not we are
2570      processing a return type until after the fact.  */
2571   while (declarator
2572          && (declarator->kind == cdk_pointer
2573              || declarator->kind == cdk_reference
2574              || declarator->kind == cdk_ptrmem))
2575     declarator = declarator->declarator;
2576   if (declarator
2577       && declarator->kind == cdk_function)
2578     {
2579       error_at (type_location,
2580                 "new types may not be defined in a return type");
2581       inform (type_location, 
2582               "(perhaps a semicolon is missing after the definition of %qT)",
2583               type);
2584     }
2585 }
2586
2587 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2588    "<" in any valid C++ program.  If the next token is indeed "<",
2589    issue a message warning the user about what appears to be an
2590    invalid attempt to form a template-id. LOCATION is the location
2591    of the type-specifier (TYPE) */
2592
2593 static void
2594 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2595                                          tree type, location_t location)
2596 {
2597   cp_token_position start = 0;
2598
2599   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2600     {
2601       if (TYPE_P (type))
2602         error_at (location, "%qT is not a template", type);
2603       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2604         error_at (location, "%qE is not a template", type);
2605       else
2606         error_at (location, "invalid template-id");
2607       /* Remember the location of the invalid "<".  */
2608       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2609         start = cp_lexer_token_position (parser->lexer, true);
2610       /* Consume the "<".  */
2611       cp_lexer_consume_token (parser->lexer);
2612       /* Parse the template arguments.  */
2613       cp_parser_enclosed_template_argument_list (parser);
2614       /* Permanently remove the invalid template arguments so that
2615          this error message is not issued again.  */
2616       if (start)
2617         cp_lexer_purge_tokens_after (parser->lexer, start);
2618     }
2619 }
2620
2621 /* If parsing an integral constant-expression, issue an error message
2622    about the fact that THING appeared and return true.  Otherwise,
2623    return false.  In either case, set
2624    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2625
2626 static bool
2627 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2628                                             non_integral_constant thing)
2629 {
2630   parser->non_integral_constant_expression_p = true;
2631   if (parser->integral_constant_expression_p)
2632     {
2633       if (!parser->allow_non_integral_constant_expression_p)
2634         {
2635           const char *msg = NULL;
2636           switch (thing)
2637             {
2638               case NIC_FLOAT:
2639                 error ("floating-point literal "
2640                        "cannot appear in a constant-expression");
2641                 return true;
2642               case NIC_CAST:
2643                 error ("a cast to a type other than an integral or "
2644                        "enumeration type cannot appear in a "
2645                        "constant-expression");
2646                 return true;
2647               case NIC_TYPEID:
2648                 error ("%<typeid%> operator "
2649                        "cannot appear in a constant-expression");
2650                 return true;
2651               case NIC_NCC:
2652                 error ("non-constant compound literals "
2653                        "cannot appear in a constant-expression");
2654                 return true;
2655               case NIC_FUNC_CALL:
2656                 error ("a function call "
2657                        "cannot appear in a constant-expression");
2658                 return true;
2659               case NIC_INC:
2660                 error ("an increment "
2661                        "cannot appear in a constant-expression");
2662                 return true;
2663               case NIC_DEC:
2664                 error ("an decrement "
2665                        "cannot appear in a constant-expression");
2666                 return true;
2667               case NIC_ARRAY_REF:
2668                 error ("an array reference "
2669                        "cannot appear in a constant-expression");
2670                 return true;
2671               case NIC_ADDR_LABEL:
2672                 error ("the address of a label "
2673                        "cannot appear in a constant-expression");
2674                 return true;
2675               case NIC_OVERLOADED:
2676                 error ("calls to overloaded operators "
2677                        "cannot appear in a constant-expression");
2678                 return true;
2679               case NIC_ASSIGNMENT:
2680                 error ("an assignment cannot appear in a constant-expression");
2681                 return true;
2682               case NIC_COMMA:
2683                 error ("a comma operator "
2684                        "cannot appear in a constant-expression");
2685                 return true;
2686               case NIC_CONSTRUCTOR:
2687                 error ("a call to a constructor "
2688                        "cannot appear in a constant-expression");
2689                 return true;
2690               case NIC_TRANSACTION:
2691                 error ("a transaction expression "
2692                        "cannot appear in a constant-expression");
2693                 return true;
2694               case NIC_THIS:
2695                 msg = "this";
2696                 break;
2697               case NIC_FUNC_NAME:
2698                 msg = "__FUNCTION__";
2699                 break;
2700               case NIC_PRETTY_FUNC:
2701                 msg = "__PRETTY_FUNCTION__";
2702                 break;
2703               case NIC_C99_FUNC:
2704                 msg = "__func__";
2705                 break;
2706               case NIC_VA_ARG:
2707                 msg = "va_arg";
2708                 break;
2709               case NIC_ARROW:
2710                 msg = "->";
2711                 break;
2712               case NIC_POINT:
2713                 msg = ".";
2714                 break;
2715               case NIC_STAR:
2716                 msg = "*";
2717                 break;
2718               case NIC_ADDR:
2719                 msg = "&";
2720                 break;
2721               case NIC_PREINCREMENT:
2722                 msg = "++";
2723                 break;
2724               case NIC_PREDECREMENT:
2725                 msg = "--";
2726                 break;
2727               case NIC_NEW:
2728                 msg = "new";
2729                 break;
2730               case NIC_DEL:
2731                 msg = "delete";
2732                 break;
2733               default:
2734                 gcc_unreachable ();
2735             }
2736           if (msg)
2737             error ("%qs cannot appear in a constant-expression", msg);
2738           return true;
2739         }
2740     }
2741   return false;
2742 }
2743
2744 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2745    qualifying scope (or NULL, if none) for ID.  This function commits
2746    to the current active tentative parse, if any.  (Otherwise, the
2747    problematic construct might be encountered again later, resulting
2748    in duplicate error messages.) LOCATION is the location of ID.  */
2749
2750 static void
2751 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2752                                       tree scope, tree id,
2753                                       location_t location)
2754 {
2755   tree decl, old_scope;
2756   cp_parser_commit_to_tentative_parse (parser);
2757   /* Try to lookup the identifier.  */
2758   old_scope = parser->scope;
2759   parser->scope = scope;
2760   decl = cp_parser_lookup_name_simple (parser, id, location);
2761   parser->scope = old_scope;
2762   /* If the lookup found a template-name, it means that the user forgot
2763   to specify an argument list. Emit a useful error message.  */
2764   if (TREE_CODE (decl) == TEMPLATE_DECL)
2765     error_at (location,
2766               "invalid use of template-name %qE without an argument list",
2767               decl);
2768   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2769     error_at (location, "invalid use of destructor %qD as a type", id);
2770   else if (TREE_CODE (decl) == TYPE_DECL)
2771     /* Something like 'unsigned A a;'  */
2772     error_at (location, "invalid combination of multiple type-specifiers");
2773   else if (!parser->scope)
2774     {
2775       /* Issue an error message.  */
2776       error_at (location, "%qE does not name a type", id);
2777       /* If we're in a template class, it's possible that the user was
2778          referring to a type from a base class.  For example:
2779
2780            template <typename T> struct A { typedef T X; };
2781            template <typename T> struct B : public A<T> { X x; };
2782
2783          The user should have said "typename A<T>::X".  */
2784       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2785         inform (location, "C++11 %<constexpr%> only available with "
2786                 "-std=c++11 or -std=gnu++11");
2787       else if (processing_template_decl && current_class_type
2788                && TYPE_BINFO (current_class_type))
2789         {
2790           tree b;
2791
2792           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2793                b;
2794                b = TREE_CHAIN (b))
2795             {
2796               tree base_type = BINFO_TYPE (b);
2797               if (CLASS_TYPE_P (base_type)
2798                   && dependent_type_p (base_type))
2799                 {
2800                   tree field;
2801                   /* Go from a particular instantiation of the
2802                      template (which will have an empty TYPE_FIELDs),
2803                      to the main version.  */
2804                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2805                   for (field = TYPE_FIELDS (base_type);
2806                        field;
2807                        field = DECL_CHAIN (field))
2808                     if (TREE_CODE (field) == TYPE_DECL
2809                         && DECL_NAME (field) == id)
2810                       {
2811                         inform (location, 
2812                                 "(perhaps %<typename %T::%E%> was intended)",
2813                                 BINFO_TYPE (b), id);
2814                         break;
2815                       }
2816                   if (field)
2817                     break;
2818                 }
2819             }
2820         }
2821     }
2822   /* Here we diagnose qualified-ids where the scope is actually correct,
2823      but the identifier does not resolve to a valid type name.  */
2824   else if (parser->scope != error_mark_node)
2825     {
2826       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2827         error_at (location, "%qE in namespace %qE does not name a type",
2828                   id, parser->scope);
2829       else if (CLASS_TYPE_P (parser->scope)
2830                && constructor_name_p (id, parser->scope))
2831         {
2832           /* A<T>::A<T>() */
2833           error_at (location, "%<%T::%E%> names the constructor, not"
2834                     " the type", parser->scope, id);
2835           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2836             error_at (location, "and %qT has no template constructors",
2837                       parser->scope);
2838         }
2839       else if (TYPE_P (parser->scope)
2840                && dependent_scope_p (parser->scope))
2841         error_at (location, "need %<typename%> before %<%T::%E%> because "
2842                   "%qT is a dependent scope",
2843                   parser->scope, id, parser->scope);
2844       else if (TYPE_P (parser->scope))
2845         error_at (location, "%qE in %q#T does not name a type",
2846                   id, parser->scope);
2847       else
2848         gcc_unreachable ();
2849     }
2850 }
2851
2852 /* Check for a common situation where a type-name should be present,
2853    but is not, and issue a sensible error message.  Returns true if an
2854    invalid type-name was detected.
2855
2856    The situation handled by this function are variable declarations of the
2857    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2858    Usually, `ID' should name a type, but if we got here it means that it
2859    does not. We try to emit the best possible error message depending on
2860    how exactly the id-expression looks like.  */
2861
2862 static bool
2863 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2864 {
2865   tree id;
2866   cp_token *token = cp_lexer_peek_token (parser->lexer);
2867
2868   /* Avoid duplicate error about ambiguous lookup.  */
2869   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2870     {
2871       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2872       if (next->type == CPP_NAME && next->ambiguous_p)
2873         goto out;
2874     }
2875
2876   cp_parser_parse_tentatively (parser);
2877   id = cp_parser_id_expression (parser,
2878                                 /*template_keyword_p=*/false,
2879                                 /*check_dependency_p=*/true,
2880                                 /*template_p=*/NULL,
2881                                 /*declarator_p=*/true,
2882                                 /*optional_p=*/false);
2883   /* If the next token is a (, this is a function with no explicit return
2884      type, i.e. constructor, destructor or conversion op.  */
2885   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2886       || TREE_CODE (id) == TYPE_DECL)
2887     {
2888       cp_parser_abort_tentative_parse (parser);
2889       return false;
2890     }
2891   if (!cp_parser_parse_definitely (parser))
2892     return false;
2893
2894   /* Emit a diagnostic for the invalid type.  */
2895   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2896                                         id, token->location);
2897  out:
2898   /* If we aren't in the middle of a declarator (i.e. in a
2899      parameter-declaration-clause), skip to the end of the declaration;
2900      there's no point in trying to process it.  */
2901   if (!parser->in_declarator_p)
2902     cp_parser_skip_to_end_of_block_or_statement (parser);
2903   return true;
2904 }
2905
2906 /* Consume tokens up to, and including, the next non-nested closing `)'.
2907    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2908    are doing error recovery. Returns -1 if OR_COMMA is true and we
2909    found an unnested comma.  */
2910
2911 static int
2912 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2913                                        bool recovering,
2914                                        bool or_comma,
2915                                        bool consume_paren)
2916 {
2917   unsigned paren_depth = 0;
2918   unsigned brace_depth = 0;
2919   unsigned square_depth = 0;
2920
2921   if (recovering && !or_comma
2922       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2923     return 0;
2924
2925   while (true)
2926     {
2927       cp_token * token = cp_lexer_peek_token (parser->lexer);
2928
2929       switch (token->type)
2930         {
2931         case CPP_EOF:
2932         case CPP_PRAGMA_EOL:
2933           /* If we've run out of tokens, then there is no closing `)'.  */
2934           return 0;
2935
2936         /* This is good for lambda expression capture-lists.  */
2937         case CPP_OPEN_SQUARE:
2938           ++square_depth;
2939           break;
2940         case CPP_CLOSE_SQUARE:
2941           if (!square_depth--)
2942             return 0;
2943           break;
2944
2945         case CPP_SEMICOLON:
2946           /* This matches the processing in skip_to_end_of_statement.  */
2947           if (!brace_depth)
2948             return 0;
2949           break;
2950
2951         case CPP_OPEN_BRACE:
2952           ++brace_depth;
2953           break;
2954         case CPP_CLOSE_BRACE:
2955           if (!brace_depth--)
2956             return 0;
2957           break;
2958
2959         case CPP_COMMA:
2960           if (recovering && or_comma && !brace_depth && !paren_depth
2961               && !square_depth)
2962             return -1;
2963           break;
2964
2965         case CPP_OPEN_PAREN:
2966           if (!brace_depth)
2967             ++paren_depth;
2968           break;
2969
2970         case CPP_CLOSE_PAREN:
2971           if (!brace_depth && !paren_depth--)
2972             {
2973               if (consume_paren)
2974                 cp_lexer_consume_token (parser->lexer);
2975               return 1;
2976             }
2977           break;
2978
2979         default:
2980           break;
2981         }
2982
2983       /* Consume the token.  */
2984       cp_lexer_consume_token (parser->lexer);
2985     }
2986 }
2987
2988 /* Consume tokens until we reach the end of the current statement.
2989    Normally, that will be just before consuming a `;'.  However, if a
2990    non-nested `}' comes first, then we stop before consuming that.  */
2991
2992 static void
2993 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2994 {
2995   unsigned nesting_depth = 0;
2996
2997   while (true)
2998     {
2999       cp_token *token = cp_lexer_peek_token (parser->lexer);
3000
3001       switch (token->type)
3002         {
3003         case CPP_EOF:
3004         case CPP_PRAGMA_EOL:
3005           /* If we've run out of tokens, stop.  */
3006           return;
3007
3008         case CPP_SEMICOLON:
3009           /* If the next token is a `;', we have reached the end of the
3010              statement.  */
3011           if (!nesting_depth)
3012             return;
3013           break;
3014
3015         case CPP_CLOSE_BRACE:
3016           /* If this is a non-nested '}', stop before consuming it.
3017              That way, when confronted with something like:
3018
3019                { 3 + }
3020
3021              we stop before consuming the closing '}', even though we
3022              have not yet reached a `;'.  */
3023           if (nesting_depth == 0)
3024             return;
3025
3026           /* If it is the closing '}' for a block that we have
3027              scanned, stop -- but only after consuming the token.
3028              That way given:
3029
3030                 void f g () { ... }
3031                 typedef int I;
3032
3033              we will stop after the body of the erroneously declared
3034              function, but before consuming the following `typedef'
3035              declaration.  */
3036           if (--nesting_depth == 0)
3037             {
3038               cp_lexer_consume_token (parser->lexer);
3039               return;
3040             }
3041
3042         case CPP_OPEN_BRACE:
3043           ++nesting_depth;
3044           break;
3045
3046         default:
3047           break;
3048         }
3049
3050       /* Consume the token.  */
3051       cp_lexer_consume_token (parser->lexer);
3052     }
3053 }
3054
3055 /* This function is called at the end of a statement or declaration.
3056    If the next token is a semicolon, it is consumed; otherwise, error
3057    recovery is attempted.  */
3058
3059 static void
3060 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3061 {
3062   /* Look for the trailing `;'.  */
3063   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3064     {
3065       /* If there is additional (erroneous) input, skip to the end of
3066          the statement.  */
3067       cp_parser_skip_to_end_of_statement (parser);
3068       /* If the next token is now a `;', consume it.  */
3069       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3070         cp_lexer_consume_token (parser->lexer);
3071     }
3072 }
3073
3074 /* Skip tokens until we have consumed an entire block, or until we
3075    have consumed a non-nested `;'.  */
3076
3077 static void
3078 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3079 {
3080   int nesting_depth = 0;
3081
3082   while (nesting_depth >= 0)
3083     {
3084       cp_token *token = cp_lexer_peek_token (parser->lexer);
3085
3086       switch (token->type)
3087         {
3088         case CPP_EOF:
3089         case CPP_PRAGMA_EOL:
3090           /* If we've run out of tokens, stop.  */
3091           return;
3092
3093         case CPP_SEMICOLON:
3094           /* Stop if this is an unnested ';'. */
3095           if (!nesting_depth)
3096             nesting_depth = -1;
3097           break;
3098
3099         case CPP_CLOSE_BRACE:
3100           /* Stop if this is an unnested '}', or closes the outermost
3101              nesting level.  */
3102           nesting_depth--;
3103           if (nesting_depth < 0)
3104             return;
3105           if (!nesting_depth)
3106             nesting_depth = -1;
3107           break;
3108
3109         case CPP_OPEN_BRACE:
3110           /* Nest. */
3111           nesting_depth++;
3112           break;
3113
3114         default:
3115           break;
3116         }
3117
3118       /* Consume the token.  */
3119       cp_lexer_consume_token (parser->lexer);
3120     }
3121 }
3122
3123 /* Skip tokens until a non-nested closing curly brace is the next
3124    token, or there are no more tokens. Return true in the first case,
3125    false otherwise.  */
3126
3127 static bool
3128 cp_parser_skip_to_closing_brace (cp_parser *parser)
3129 {
3130   unsigned nesting_depth = 0;
3131
3132   while (true)
3133     {
3134       cp_token *token = cp_lexer_peek_token (parser->lexer);
3135
3136       switch (token->type)
3137         {
3138         case CPP_EOF:
3139         case CPP_PRAGMA_EOL:
3140           /* If we've run out of tokens, stop.  */
3141           return false;
3142
3143         case CPP_CLOSE_BRACE:
3144           /* If the next token is a non-nested `}', then we have reached
3145              the end of the current block.  */
3146           if (nesting_depth-- == 0)
3147             return true;
3148           break;
3149
3150         case CPP_OPEN_BRACE:
3151           /* If it the next token is a `{', then we are entering a new
3152              block.  Consume the entire block.  */
3153           ++nesting_depth;
3154           break;
3155
3156         default:
3157           break;
3158         }
3159
3160       /* Consume the token.  */
3161       cp_lexer_consume_token (parser->lexer);
3162     }
3163 }
3164
3165 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3166    parameter is the PRAGMA token, allowing us to purge the entire pragma
3167    sequence.  */
3168
3169 static void
3170 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3171 {
3172   cp_token *token;
3173
3174   parser->lexer->in_pragma = false;
3175
3176   do
3177     token = cp_lexer_consume_token (parser->lexer);
3178   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3179
3180   /* Ensure that the pragma is not parsed again.  */
3181   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3182 }
3183
3184 /* Require pragma end of line, resyncing with it as necessary.  The
3185    arguments are as for cp_parser_skip_to_pragma_eol.  */
3186
3187 static void
3188 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3189 {
3190   parser->lexer->in_pragma = false;
3191   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3192     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3193 }
3194
3195 /* This is a simple wrapper around make_typename_type. When the id is
3196    an unresolved identifier node, we can provide a superior diagnostic
3197    using cp_parser_diagnose_invalid_type_name.  */
3198
3199 static tree
3200 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3201                               tree id, location_t id_location)
3202 {
3203   tree result;
3204   if (TREE_CODE (id) == IDENTIFIER_NODE)
3205     {
3206       result = make_typename_type (scope, id, typename_type,
3207                                    /*complain=*/tf_none);
3208       if (result == error_mark_node)
3209         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3210       return result;
3211     }
3212   return make_typename_type (scope, id, typename_type, tf_error);
3213 }
3214
3215 /* This is a wrapper around the
3216    make_{pointer,ptrmem,reference}_declarator functions that decides
3217    which one to call based on the CODE and CLASS_TYPE arguments. The
3218    CODE argument should be one of the values returned by
3219    cp_parser_ptr_operator. */
3220 static cp_declarator *
3221 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3222                                     cp_cv_quals cv_qualifiers,
3223                                     cp_declarator *target)
3224 {
3225   if (code == ERROR_MARK)
3226     return cp_error_declarator;
3227
3228   if (code == INDIRECT_REF)
3229     if (class_type == NULL_TREE)
3230       return make_pointer_declarator (cv_qualifiers, target);
3231     else
3232       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3233   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3234     return make_reference_declarator (cv_qualifiers, target, false);
3235   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3236     return make_reference_declarator (cv_qualifiers, target, true);
3237   gcc_unreachable ();
3238 }
3239
3240 /* Create a new C++ parser.  */
3241
3242 static cp_parser *
3243 cp_parser_new (void)
3244 {
3245   cp_parser *parser;
3246   cp_lexer *lexer;
3247   unsigned i;
3248
3249   /* cp_lexer_new_main is called before doing GC allocation because
3250      cp_lexer_new_main might load a PCH file.  */
3251   lexer = cp_lexer_new_main ();
3252
3253   /* Initialize the binops_by_token so that we can get the tree
3254      directly from the token.  */
3255   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3256     binops_by_token[binops[i].token_type] = binops[i];
3257
3258   parser = ggc_alloc_cleared_cp_parser ();
3259   parser->lexer = lexer;
3260   parser->context = cp_parser_context_new (NULL);
3261
3262   /* For now, we always accept GNU extensions.  */
3263   parser->allow_gnu_extensions_p = 1;
3264
3265   /* The `>' token is a greater-than operator, not the end of a
3266      template-id.  */
3267   parser->greater_than_is_operator_p = true;
3268
3269   parser->default_arg_ok_p = true;
3270
3271   /* We are not parsing a constant-expression.  */
3272   parser->integral_constant_expression_p = false;
3273   parser->allow_non_integral_constant_expression_p = false;
3274   parser->non_integral_constant_expression_p = false;
3275
3276   /* Local variable names are not forbidden.  */
3277   parser->local_variables_forbidden_p = false;
3278
3279   /* We are not processing an `extern "C"' declaration.  */
3280   parser->in_unbraced_linkage_specification_p = false;
3281
3282   /* We are not processing a declarator.  */
3283   parser->in_declarator_p = false;
3284
3285   /* We are not processing a template-argument-list.  */
3286   parser->in_template_argument_list_p = false;
3287
3288   /* We are not in an iteration statement.  */
3289   parser->in_statement = 0;
3290
3291   /* We are not in a switch statement.  */
3292   parser->in_switch_statement_p = false;
3293
3294   /* We are not parsing a type-id inside an expression.  */
3295   parser->in_type_id_in_expr_p = false;
3296
3297   /* Declarations aren't implicitly extern "C".  */
3298   parser->implicit_extern_c = false;
3299
3300   /* String literals should be translated to the execution character set.  */
3301   parser->translate_strings_p = true;
3302
3303   /* We are not parsing a function body.  */
3304   parser->in_function_body = false;
3305
3306   /* We can correct until told otherwise.  */
3307   parser->colon_corrects_to_scope_p = true;
3308
3309   /* The unparsed function queue is empty.  */
3310   push_unparsed_function_queues (parser);
3311
3312   /* There are no classes being defined.  */
3313   parser->num_classes_being_defined = 0;
3314
3315   /* No template parameters apply.  */
3316   parser->num_template_parameter_lists = 0;
3317
3318   return parser;
3319 }
3320
3321 /* Create a cp_lexer structure which will emit the tokens in CACHE
3322    and push it onto the parser's lexer stack.  This is used for delayed
3323    parsing of in-class method bodies and default arguments, and should
3324    not be confused with tentative parsing.  */
3325 static void
3326 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3327 {
3328   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3329   lexer->next = parser->lexer;
3330   parser->lexer = lexer;
3331
3332   /* Move the current source position to that of the first token in the
3333      new lexer.  */
3334   cp_lexer_set_source_position_from_token (lexer->next_token);
3335 }
3336
3337 /* Pop the top lexer off the parser stack.  This is never used for the
3338    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3339 static void
3340 cp_parser_pop_lexer (cp_parser *parser)
3341 {
3342   cp_lexer *lexer = parser->lexer;
3343   parser->lexer = lexer->next;
3344   cp_lexer_destroy (lexer);
3345
3346   /* Put the current source position back where it was before this
3347      lexer was pushed.  */
3348   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3349 }
3350
3351 /* Lexical conventions [gram.lex]  */
3352
3353 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3354    identifier.  */
3355
3356 static tree
3357 cp_parser_identifier (cp_parser* parser)
3358 {
3359   cp_token *token;
3360
3361   /* Look for the identifier.  */
3362   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3363   /* Return the value.  */
3364   return token ? token->u.value : error_mark_node;
3365 }
3366
3367 /* Parse a sequence of adjacent string constants.  Returns a
3368    TREE_STRING representing the combined, nul-terminated string
3369    constant.  If TRANSLATE is true, translate the string to the
3370    execution character set.  If WIDE_OK is true, a wide string is
3371    invalid here.
3372
3373    C++98 [lex.string] says that if a narrow string literal token is
3374    adjacent to a wide string literal token, the behavior is undefined.
3375    However, C99 6.4.5p4 says that this results in a wide string literal.
3376    We follow C99 here, for consistency with the C front end.
3377
3378    This code is largely lifted from lex_string() in c-lex.c.
3379
3380    FUTURE: ObjC++ will need to handle @-strings here.  */
3381 static tree
3382 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3383 {
3384   tree value;
3385   size_t count;
3386   struct obstack str_ob;
3387   cpp_string str, istr, *strs;
3388   cp_token *tok;
3389   enum cpp_ttype type, curr_type;
3390   int have_suffix_p = 0;
3391   tree string_tree;
3392   tree suffix_id = NULL_TREE;
3393   bool curr_tok_is_userdef_p = false;
3394
3395   tok = cp_lexer_peek_token (parser->lexer);
3396   if (!cp_parser_is_string_literal (tok))
3397     {
3398       cp_parser_error (parser, "expected string-literal");
3399       return error_mark_node;
3400     }
3401
3402   if (cpp_userdef_string_p (tok->type))
3403     {
3404       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3405       curr_type = cpp_userdef_string_remove_type (tok->type);
3406       curr_tok_is_userdef_p = true;
3407     }
3408   else
3409     {
3410       string_tree = tok->u.value;
3411       curr_type = tok->type;
3412     }
3413   type = curr_type;
3414
3415   /* Try to avoid the overhead of creating and destroying an obstack
3416      for the common case of just one string.  */
3417   if (!cp_parser_is_string_literal
3418       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3419     {
3420       cp_lexer_consume_token (parser->lexer);
3421
3422       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3423       str.len = TREE_STRING_LENGTH (string_tree);
3424       count = 1;
3425
3426       if (curr_tok_is_userdef_p)
3427         {
3428           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3429           have_suffix_p = 1;
3430           curr_type = cpp_userdef_string_remove_type (tok->type);
3431         }
3432       else
3433         curr_type = tok->type;
3434
3435       strs = &str;
3436     }
3437   else
3438     {
3439       gcc_obstack_init (&str_ob);
3440       count = 0;
3441
3442       do
3443         {
3444           cp_lexer_consume_token (parser->lexer);
3445           count++;
3446           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3447           str.len = TREE_STRING_LENGTH (string_tree);
3448
3449           if (curr_tok_is_userdef_p)
3450             {
3451               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3452               if (have_suffix_p == 0)
3453                 {
3454                   suffix_id = curr_suffix_id;
3455                   have_suffix_p = 1;
3456                 }
3457               else if (have_suffix_p == 1
3458                        && curr_suffix_id != suffix_id)
3459                 {
3460                   error ("inconsistent user-defined literal suffixes"
3461                          " %qD and %qD in string literal",
3462                          suffix_id, curr_suffix_id);
3463                   have_suffix_p = -1;
3464                 }
3465               curr_type = cpp_userdef_string_remove_type (tok->type);
3466             }
3467           else
3468             curr_type = tok->type;
3469
3470           if (type != curr_type)
3471             {
3472               if (type == CPP_STRING)
3473                 type = curr_type;
3474               else if (curr_type != CPP_STRING)
3475                 error_at (tok->location,
3476                           "unsupported non-standard concatenation "
3477                           "of string literals");
3478             }
3479
3480           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3481
3482           tok = cp_lexer_peek_token (parser->lexer);
3483           if (cpp_userdef_string_p (tok->type))
3484             {
3485               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3486               curr_type = cpp_userdef_string_remove_type (tok->type);
3487               curr_tok_is_userdef_p = true;
3488             }
3489           else
3490             {
3491               string_tree = tok->u.value;
3492               curr_type = tok->type;
3493               curr_tok_is_userdef_p = false;
3494             }
3495         }
3496       while (cp_parser_is_string_literal (tok));
3497
3498       strs = (cpp_string *) obstack_finish (&str_ob);
3499     }
3500
3501   if (type != CPP_STRING && !wide_ok)
3502     {
3503       cp_parser_error (parser, "a wide string is invalid in this context");
3504       type = CPP_STRING;
3505     }
3506
3507   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3508       (parse_in, strs, count, &istr, type))
3509     {
3510       value = build_string (istr.len, (const char *)istr.text);
3511       free (CONST_CAST (unsigned char *, istr.text));
3512
3513       switch (type)
3514         {
3515         default:
3516         case CPP_STRING:
3517         case CPP_UTF8STRING:
3518           TREE_TYPE (value) = char_array_type_node;
3519           break;
3520         case CPP_STRING16:
3521           TREE_TYPE (value) = char16_array_type_node;
3522           break;
3523         case CPP_STRING32:
3524           TREE_TYPE (value) = char32_array_type_node;
3525           break;
3526         case CPP_WSTRING:
3527           TREE_TYPE (value) = wchar_array_type_node;
3528           break;
3529         }
3530
3531       value = fix_string_type (value);
3532
3533       if (have_suffix_p)
3534         {
3535           tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3536           tok->u.value = literal;
3537           return cp_parser_userdef_string_literal (tok);
3538         }
3539     }
3540   else
3541     /* cpp_interpret_string has issued an error.  */
3542     value = error_mark_node;
3543
3544   if (count > 1)
3545     obstack_free (&str_ob, 0);
3546
3547   return value;
3548 }
3549
3550 /* Look up a literal operator with the name and the exact arguments.  */
3551
3552 static tree
3553 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3554 {
3555   tree decl, fns;
3556   decl = lookup_name (name);
3557   if (!decl || !is_overloaded_fn (decl))
3558     return error_mark_node;
3559
3560   for (fns = decl; fns; fns = OVL_NEXT (fns))
3561     {
3562       unsigned int ix;
3563       bool found = true;
3564       tree fn = OVL_CURRENT (fns);
3565       tree argtypes = NULL_TREE;
3566       argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3567       if (argtypes != NULL_TREE)
3568         {
3569           for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3570                ++ix, argtypes = TREE_CHAIN (argtypes))
3571             {
3572               tree targ = TREE_VALUE (argtypes);
3573               tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3574               bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3575               bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3576               if ((ptr || arr || !same_type_p (targ, tparm))
3577                   && (!ptr || !arr
3578                       || !same_type_p (TREE_TYPE (targ),
3579                                        TREE_TYPE (tparm))))
3580                 found = false;
3581             }
3582           if (found)
3583             return fn;
3584         }
3585     }
3586
3587   return error_mark_node;
3588 }
3589
3590 /* Parse a user-defined char constant.  Returns a call to a user-defined
3591    literal operator taking the character as an argument.  */
3592
3593 static tree
3594 cp_parser_userdef_char_literal (cp_parser *parser)
3595 {
3596   cp_token *token = cp_lexer_consume_token (parser->lexer);
3597   tree literal = token->u.value;
3598   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3599   tree value = USERDEF_LITERAL_VALUE (literal);
3600   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3601   tree decl, result;
3602
3603   /* Build up a call to the user-defined operator  */
3604   /* Lookup the name we got back from the id-expression.  */
3605   VEC(tree,gc) *args = make_tree_vector ();
3606   VEC_safe_push (tree, gc, args, value);
3607   decl = lookup_literal_operator (name, args);
3608   if (!decl || decl == error_mark_node)
3609     {
3610       error ("unable to find character literal operator %qD with %qT argument",
3611              name, TREE_TYPE (value));
3612       release_tree_vector (args);
3613       return error_mark_node;
3614     }
3615   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3616   release_tree_vector (args);
3617   if (result != error_mark_node)
3618     return result;
3619
3620   error ("unable to find character literal operator %qD with %qT argument",
3621          name, TREE_TYPE (value));
3622   return error_mark_node;
3623 }
3624
3625 /* A subroutine of cp_parser_userdef_numeric_literal to
3626    create a char... template parameter pack from a string node.  */
3627
3628 static tree
3629 make_char_string_pack (tree value)
3630 {
3631   tree charvec;
3632   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3633   const char *str = TREE_STRING_POINTER (value);
3634   int i, len = TREE_STRING_LENGTH (value) - 1;
3635   tree argvec = make_tree_vec (1);
3636
3637   /* Fill in CHARVEC with all of the parameters.  */
3638   charvec = make_tree_vec (len);
3639   for (i = 0; i < len; ++i)
3640     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3641
3642   /* Build the argument packs.  */
3643   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3644   TREE_TYPE (argpack) = char_type_node;
3645
3646   TREE_VEC_ELT (argvec, 0) = argpack;
3647
3648   return argvec;
3649 }
3650
3651 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3652    literal operator.  */
3653
3654 static tree
3655 cp_parser_userdef_numeric_literal (cp_parser *parser)
3656 {
3657   cp_token *token = cp_lexer_consume_token (parser->lexer);
3658   tree literal = token->u.value;
3659   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3660   tree value = USERDEF_LITERAL_VALUE (literal);
3661   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3662   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3663   tree decl, result;
3664   VEC(tree,gc) *args;
3665
3666   /* Look for a literal operator taking the exact type of numeric argument
3667      as the literal value.  */
3668   args = make_tree_vector ();
3669   VEC_safe_push (tree, gc, args, value);
3670   decl = lookup_literal_operator (name, args);
3671   if (decl && decl != error_mark_node)
3672     {
3673       result = finish_call_expr (decl, &args, false, true, tf_none);
3674       if (result != error_mark_node)
3675         {
3676           release_tree_vector (args);
3677           return result;
3678         }
3679     }
3680   release_tree_vector (args);
3681
3682   /* If the numeric argument didn't work, look for a raw literal
3683      operator taking a const char* argument consisting of the number
3684      in string format.  */
3685   args = make_tree_vector ();
3686   VEC_safe_push (tree, gc, args, num_string);
3687   decl = lookup_literal_operator (name, args);
3688   if (decl && decl != error_mark_node)
3689     {
3690       result = finish_call_expr (decl, &args, false, true, tf_none);
3691       if (result != error_mark_node)
3692         {
3693           release_tree_vector (args);
3694           return result;
3695         }
3696     }
3697   release_tree_vector (args);
3698
3699   /* If the raw literal didn't work, look for a non-type template
3700      function with parameter pack char....  Call the function with
3701      template parameter characters representing the number.  */
3702   args = make_tree_vector ();
3703   decl = lookup_literal_operator (name, args);
3704   if (decl && decl != error_mark_node)
3705     {
3706       tree tmpl_args = make_char_string_pack (num_string);
3707       decl = lookup_template_function (decl, tmpl_args);
3708       result = finish_call_expr (decl, &args, false, true, tf_none);
3709       if (result != error_mark_node)
3710         {
3711           release_tree_vector (args);
3712           return result;
3713         }
3714     }
3715   release_tree_vector (args);
3716
3717   error ("unable to find numeric literal operator %qD", name);
3718   return error_mark_node;
3719 }
3720
3721 /* Parse a user-defined string constant.  Returns a call to a user-defined
3722    literal operator taking a character pointer and the length of the string
3723    as arguments.  */
3724
3725 static tree
3726 cp_parser_userdef_string_literal (cp_token *token)
3727 {
3728   tree literal = token->u.value;
3729   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3730   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3731   tree value = USERDEF_LITERAL_VALUE (literal);
3732   int len = TREE_STRING_LENGTH (value)
3733         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3734   tree decl, result;
3735
3736   /* Build up a call to the user-defined operator  */
3737   /* Lookup the name we got back from the id-expression.  */
3738   VEC(tree,gc) *args = make_tree_vector ();
3739   VEC_safe_push (tree, gc, args, value);
3740   VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3741   decl = lookup_name (name);
3742   if (!decl || decl == error_mark_node)
3743     {
3744       error ("unable to find string literal operator %qD", name);
3745       release_tree_vector (args);
3746       return error_mark_node;
3747     }
3748   result = finish_call_expr (decl, &args, false, true, tf_none);
3749   release_tree_vector (args);
3750   if (result != error_mark_node)
3751     return result;
3752
3753   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3754          name, TREE_TYPE (value), size_type_node);
3755   return error_mark_node;
3756 }
3757
3758
3759 /* Basic concepts [gram.basic]  */
3760
3761 /* Parse a translation-unit.
3762
3763    translation-unit:
3764      declaration-seq [opt]
3765
3766    Returns TRUE if all went well.  */
3767
3768 static bool
3769 cp_parser_translation_unit (cp_parser* parser)
3770 {
3771   /* The address of the first non-permanent object on the declarator
3772      obstack.  */
3773   static void *declarator_obstack_base;
3774
3775   bool success;
3776
3777   /* Create the declarator obstack, if necessary.  */
3778   if (!cp_error_declarator)
3779     {
3780       gcc_obstack_init (&declarator_obstack);
3781       /* Create the error declarator.  */
3782       cp_error_declarator = make_declarator (cdk_error);
3783       /* Create the empty parameter list.  */
3784       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3785       /* Remember where the base of the declarator obstack lies.  */
3786       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3787     }
3788
3789   cp_parser_declaration_seq_opt (parser);
3790
3791   /* If there are no tokens left then all went well.  */
3792   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3793     {
3794       /* Get rid of the token array; we don't need it any more.  */
3795       cp_lexer_destroy (parser->lexer);
3796       parser->lexer = NULL;
3797
3798       /* This file might have been a context that's implicitly extern
3799          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3800       if (parser->implicit_extern_c)
3801         {
3802           pop_lang_context ();
3803           parser->implicit_extern_c = false;
3804         }
3805
3806       /* Finish up.  */
3807       finish_translation_unit ();
3808
3809       success = true;
3810     }
3811   else
3812     {
3813       cp_parser_error (parser, "expected declaration");
3814       success = false;
3815     }
3816
3817   /* Make sure the declarator obstack was fully cleaned up.  */
3818   gcc_assert (obstack_next_free (&declarator_obstack)
3819               == declarator_obstack_base);
3820
3821   /* All went well.  */
3822   return success;
3823 }
3824
3825 /* Expressions [gram.expr] */
3826
3827 /* Parse a primary-expression.
3828
3829    primary-expression:
3830      literal
3831      this
3832      ( expression )
3833      id-expression
3834
3835    GNU Extensions:
3836
3837    primary-expression:
3838      ( compound-statement )
3839      __builtin_va_arg ( assignment-expression , type-id )
3840      __builtin_offsetof ( type-id , offsetof-expression )
3841
3842    C++ Extensions:
3843      __has_nothrow_assign ( type-id )   
3844      __has_nothrow_constructor ( type-id )
3845      __has_nothrow_copy ( type-id )
3846      __has_trivial_assign ( type-id )   
3847      __has_trivial_constructor ( type-id )
3848      __has_trivial_copy ( type-id )
3849      __has_trivial_destructor ( type-id )
3850      __has_virtual_destructor ( type-id )     
3851      __is_abstract ( type-id )
3852      __is_base_of ( type-id , type-id )
3853      __is_class ( type-id )
3854      __is_convertible_to ( type-id , type-id )     
3855      __is_empty ( type-id )
3856      __is_enum ( type-id )
3857      __is_literal_type ( type-id )
3858      __is_pod ( type-id )
3859      __is_polymorphic ( type-id )
3860      __is_std_layout ( type-id )
3861      __is_trivial ( type-id )
3862      __is_union ( type-id )
3863
3864    Objective-C++ Extension:
3865
3866    primary-expression:
3867      objc-expression
3868
3869    literal:
3870      __null
3871
3872    ADDRESS_P is true iff this expression was immediately preceded by
3873    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3874    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3875    true iff this expression is a template argument.
3876
3877    Returns a representation of the expression.  Upon return, *IDK
3878    indicates what kind of id-expression (if any) was present.  */
3879
3880 static tree
3881 cp_parser_primary_expression (cp_parser *parser,
3882                               bool address_p,
3883                               bool cast_p,
3884                               bool template_arg_p,
3885                               cp_id_kind *idk)
3886 {
3887   cp_token *token = NULL;
3888
3889   /* Assume the primary expression is not an id-expression.  */
3890   *idk = CP_ID_KIND_NONE;
3891
3892   /* Peek at the next token.  */
3893   token = cp_lexer_peek_token (parser->lexer);
3894   switch (token->type)
3895     {
3896       /* literal:
3897            integer-literal
3898            character-literal
3899            floating-literal
3900            string-literal
3901            boolean-literal
3902            pointer-literal
3903            user-defined-literal  */
3904     case CPP_CHAR:
3905     case CPP_CHAR16:
3906     case CPP_CHAR32:
3907     case CPP_WCHAR:
3908     case CPP_NUMBER:
3909       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3910         return cp_parser_userdef_numeric_literal (parser);
3911       token = cp_lexer_consume_token (parser->lexer);
3912       if (TREE_CODE (token->u.value) == FIXED_CST)
3913         {
3914           error_at (token->location,
3915                     "fixed-point types not supported in C++");
3916           return error_mark_node;
3917         }
3918       /* Floating-point literals are only allowed in an integral
3919          constant expression if they are cast to an integral or
3920          enumeration type.  */
3921       if (TREE_CODE (token->u.value) == REAL_CST
3922           && parser->integral_constant_expression_p
3923           && pedantic)
3924         {
3925           /* CAST_P will be set even in invalid code like "int(2.7 +
3926              ...)".   Therefore, we have to check that the next token
3927              is sure to end the cast.  */
3928           if (cast_p)
3929             {
3930               cp_token *next_token;
3931
3932               next_token = cp_lexer_peek_token (parser->lexer);
3933               if (/* The comma at the end of an
3934                      enumerator-definition.  */
3935                   next_token->type != CPP_COMMA
3936                   /* The curly brace at the end of an enum-specifier.  */
3937                   && next_token->type != CPP_CLOSE_BRACE
3938                   /* The end of a statement.  */
3939                   && next_token->type != CPP_SEMICOLON
3940                   /* The end of the cast-expression.  */
3941                   && next_token->type != CPP_CLOSE_PAREN
3942                   /* The end of an array bound.  */
3943                   && next_token->type != CPP_CLOSE_SQUARE
3944                   /* The closing ">" in a template-argument-list.  */
3945                   && (next_token->type != CPP_GREATER
3946                       || parser->greater_than_is_operator_p)
3947                   /* C++0x only: A ">>" treated like two ">" tokens,
3948                      in a template-argument-list.  */
3949                   && (next_token->type != CPP_RSHIFT
3950                       || (cxx_dialect == cxx98)
3951                       || parser->greater_than_is_operator_p))
3952                 cast_p = false;
3953             }
3954
3955           /* If we are within a cast, then the constraint that the
3956              cast is to an integral or enumeration type will be
3957              checked at that point.  If we are not within a cast, then
3958              this code is invalid.  */
3959           if (!cast_p)
3960             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3961         }
3962       return token->u.value;
3963
3964     case CPP_CHAR_USERDEF:
3965     case CPP_CHAR16_USERDEF:
3966     case CPP_CHAR32_USERDEF:
3967     case CPP_WCHAR_USERDEF:
3968       return cp_parser_userdef_char_literal (parser);
3969
3970     case CPP_STRING:
3971     case CPP_STRING16:
3972     case CPP_STRING32:
3973     case CPP_WSTRING:
3974     case CPP_UTF8STRING:
3975     case CPP_STRING_USERDEF:
3976     case CPP_STRING16_USERDEF:
3977     case CPP_STRING32_USERDEF:
3978     case CPP_WSTRING_USERDEF:
3979     case CPP_UTF8STRING_USERDEF:
3980       /* ??? Should wide strings be allowed when parser->translate_strings_p
3981          is false (i.e. in attributes)?  If not, we can kill the third
3982          argument to cp_parser_string_literal.  */
3983       return cp_parser_string_literal (parser,
3984                                        parser->translate_strings_p,
3985                                        true);
3986
3987     case CPP_OPEN_PAREN:
3988       {
3989         tree expr;
3990         bool saved_greater_than_is_operator_p;
3991
3992         /* Consume the `('.  */
3993         cp_lexer_consume_token (parser->lexer);
3994         /* Within a parenthesized expression, a `>' token is always
3995            the greater-than operator.  */
3996         saved_greater_than_is_operator_p
3997           = parser->greater_than_is_operator_p;
3998         parser->greater_than_is_operator_p = true;
3999         /* If we see `( { ' then we are looking at the beginning of
4000            a GNU statement-expression.  */
4001         if (cp_parser_allow_gnu_extensions_p (parser)
4002             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4003           {
4004             /* Statement-expressions are not allowed by the standard.  */
4005             pedwarn (token->location, OPT_pedantic, 
4006                      "ISO C++ forbids braced-groups within expressions");
4007
4008             /* And they're not allowed outside of a function-body; you
4009                cannot, for example, write:
4010
4011                  int i = ({ int j = 3; j + 1; });
4012
4013                at class or namespace scope.  */
4014             if (!parser->in_function_body
4015                 || parser->in_template_argument_list_p)
4016               {
4017                 error_at (token->location,
4018                           "statement-expressions are not allowed outside "
4019                           "functions nor in template-argument lists");
4020                 cp_parser_skip_to_end_of_block_or_statement (parser);
4021                 expr = error_mark_node;
4022               }
4023             else
4024               {
4025                 /* Start the statement-expression.  */
4026                 expr = begin_stmt_expr ();
4027                 /* Parse the compound-statement.  */
4028                 cp_parser_compound_statement (parser, expr, false, false);
4029                 /* Finish up.  */
4030                 expr = finish_stmt_expr (expr, false);
4031               }
4032           }
4033         else
4034           {
4035             /* Parse the parenthesized expression.  */
4036             expr = cp_parser_expression (parser, cast_p, idk);
4037             /* Let the front end know that this expression was
4038                enclosed in parentheses. This matters in case, for
4039                example, the expression is of the form `A::B', since
4040                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4041                not.  */
4042             finish_parenthesized_expr (expr);
4043             /* DR 705: Wrapping an unqualified name in parentheses
4044                suppresses arg-dependent lookup.  We want to pass back
4045                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4046                (c++/37862), but none of the others.  */
4047             if (*idk != CP_ID_KIND_QUALIFIED)
4048               *idk = CP_ID_KIND_NONE;
4049           }
4050         /* The `>' token might be the end of a template-id or
4051            template-parameter-list now.  */
4052         parser->greater_than_is_operator_p
4053           = saved_greater_than_is_operator_p;
4054         /* Consume the `)'.  */
4055         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4056           cp_parser_skip_to_end_of_statement (parser);
4057
4058         return expr;
4059       }
4060
4061     case CPP_OPEN_SQUARE:
4062       if (c_dialect_objc ())
4063         /* We have an Objective-C++ message. */
4064         return cp_parser_objc_expression (parser);
4065       {
4066         tree lam = cp_parser_lambda_expression (parser);
4067         /* Don't warn about a failed tentative parse.  */
4068         if (cp_parser_error_occurred (parser))
4069           return error_mark_node;
4070         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4071         return lam;
4072       }
4073
4074     case CPP_OBJC_STRING:
4075       if (c_dialect_objc ())
4076         /* We have an Objective-C++ string literal. */
4077         return cp_parser_objc_expression (parser);
4078       cp_parser_error (parser, "expected primary-expression");
4079       return error_mark_node;
4080
4081     case CPP_KEYWORD:
4082       switch (token->keyword)
4083         {
4084           /* These two are the boolean literals.  */
4085         case RID_TRUE:
4086           cp_lexer_consume_token (parser->lexer);
4087           return boolean_true_node;
4088         case RID_FALSE:
4089           cp_lexer_consume_token (parser->lexer);
4090           return boolean_false_node;
4091
4092           /* The `__null' literal.  */
4093         case RID_NULL:
4094           cp_lexer_consume_token (parser->lexer);
4095           return null_node;
4096
4097           /* The `nullptr' literal.  */
4098         case RID_NULLPTR:
4099           cp_lexer_consume_token (parser->lexer);
4100           return nullptr_node;
4101
4102           /* Recognize the `this' keyword.  */
4103         case RID_THIS:
4104           cp_lexer_consume_token (parser->lexer);
4105           if (parser->local_variables_forbidden_p)
4106             {
4107               error_at (token->location,
4108                         "%<this%> may not be used in this context");
4109               return error_mark_node;
4110             }
4111           /* Pointers cannot appear in constant-expressions.  */
4112           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4113             return error_mark_node;
4114           return finish_this_expr ();
4115
4116           /* The `operator' keyword can be the beginning of an
4117              id-expression.  */
4118         case RID_OPERATOR:
4119           goto id_expression;
4120
4121         case RID_FUNCTION_NAME:
4122         case RID_PRETTY_FUNCTION_NAME:
4123         case RID_C99_FUNCTION_NAME:
4124           {
4125             non_integral_constant name;
4126
4127             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4128                __func__ are the names of variables -- but they are
4129                treated specially.  Therefore, they are handled here,
4130                rather than relying on the generic id-expression logic
4131                below.  Grammatically, these names are id-expressions.
4132
4133                Consume the token.  */
4134             token = cp_lexer_consume_token (parser->lexer);
4135
4136             switch (token->keyword)
4137               {
4138               case RID_FUNCTION_NAME:
4139                 name = NIC_FUNC_NAME;
4140                 break;
4141               case RID_PRETTY_FUNCTION_NAME:
4142                 name = NIC_PRETTY_FUNC;
4143                 break;
4144               case RID_C99_FUNCTION_NAME:
4145                 name = NIC_C99_FUNC;
4146                 break;
4147               default:
4148                 gcc_unreachable ();
4149               }
4150
4151             if (cp_parser_non_integral_constant_expression (parser, name))
4152               return error_mark_node;
4153
4154             /* Look up the name.  */
4155             return finish_fname (token->u.value);
4156           }
4157
4158         case RID_VA_ARG:
4159           {
4160             tree expression;
4161             tree type;
4162
4163             /* The `__builtin_va_arg' construct is used to handle
4164                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4165             cp_lexer_consume_token (parser->lexer);
4166             /* Look for the opening `('.  */
4167             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4168             /* Now, parse the assignment-expression.  */
4169             expression = cp_parser_assignment_expression (parser,
4170                                                           /*cast_p=*/false, NULL);
4171             /* Look for the `,'.  */
4172             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4173             /* Parse the type-id.  */
4174             type = cp_parser_type_id (parser);
4175             /* Look for the closing `)'.  */
4176             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4177             /* Using `va_arg' in a constant-expression is not
4178                allowed.  */
4179             if (cp_parser_non_integral_constant_expression (parser,
4180                                                             NIC_VA_ARG))
4181               return error_mark_node;
4182             return build_x_va_arg (expression, type);
4183           }
4184
4185         case RID_OFFSETOF:
4186           return cp_parser_builtin_offsetof (parser);
4187
4188         case RID_HAS_NOTHROW_ASSIGN:
4189         case RID_HAS_NOTHROW_CONSTRUCTOR:
4190         case RID_HAS_NOTHROW_COPY:        
4191         case RID_HAS_TRIVIAL_ASSIGN:
4192         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4193         case RID_HAS_TRIVIAL_COPY:        
4194         case RID_HAS_TRIVIAL_DESTRUCTOR:
4195         case RID_HAS_VIRTUAL_DESTRUCTOR:
4196         case RID_IS_ABSTRACT:
4197         case RID_IS_BASE_OF:
4198         case RID_IS_CLASS:
4199         case RID_IS_CONVERTIBLE_TO:
4200         case RID_IS_EMPTY:
4201         case RID_IS_ENUM:
4202         case RID_IS_LITERAL_TYPE:
4203         case RID_IS_POD:
4204         case RID_IS_POLYMORPHIC:
4205         case RID_IS_STD_LAYOUT:
4206         case RID_IS_TRIVIAL:
4207         case RID_IS_UNION:
4208           return cp_parser_trait_expr (parser, token->keyword);
4209
4210         /* Objective-C++ expressions.  */
4211         case RID_AT_ENCODE:
4212         case RID_AT_PROTOCOL:
4213         case RID_AT_SELECTOR:
4214           return cp_parser_objc_expression (parser);
4215
4216         case RID_TEMPLATE:
4217           if (parser->in_function_body
4218               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4219                   == CPP_LESS))
4220             {
4221               error_at (token->location,
4222                         "a template declaration cannot appear at block scope");
4223               cp_parser_skip_to_end_of_block_or_statement (parser);
4224               return error_mark_node;
4225             }
4226         default:
4227           cp_parser_error (parser, "expected primary-expression");
4228           return error_mark_node;
4229         }
4230
4231       /* An id-expression can start with either an identifier, a
4232          `::' as the beginning of a qualified-id, or the "operator"
4233          keyword.  */
4234     case CPP_NAME:
4235     case CPP_SCOPE:
4236     case CPP_TEMPLATE_ID:
4237     case CPP_NESTED_NAME_SPECIFIER:
4238       {
4239         tree id_expression;
4240         tree decl;
4241         const char *error_msg;
4242         bool template_p;
4243         bool done;
4244         cp_token *id_expr_token;
4245
4246       id_expression:
4247         /* Parse the id-expression.  */
4248         id_expression
4249           = cp_parser_id_expression (parser,
4250                                      /*template_keyword_p=*/false,
4251                                      /*check_dependency_p=*/true,
4252                                      &template_p,
4253                                      /*declarator_p=*/false,
4254                                      /*optional_p=*/false);
4255         if (id_expression == error_mark_node)
4256           return error_mark_node;
4257         id_expr_token = token;
4258         token = cp_lexer_peek_token (parser->lexer);
4259         done = (token->type != CPP_OPEN_SQUARE
4260                 && token->type != CPP_OPEN_PAREN
4261                 && token->type != CPP_DOT
4262                 && token->type != CPP_DEREF
4263                 && token->type != CPP_PLUS_PLUS
4264                 && token->type != CPP_MINUS_MINUS);
4265         /* If we have a template-id, then no further lookup is
4266            required.  If the template-id was for a template-class, we
4267            will sometimes have a TYPE_DECL at this point.  */
4268         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4269                  || TREE_CODE (id_expression) == TYPE_DECL)
4270           decl = id_expression;
4271         /* Look up the name.  */
4272         else
4273           {
4274             tree ambiguous_decls;
4275
4276             /* If we already know that this lookup is ambiguous, then
4277                we've already issued an error message; there's no reason
4278                to check again.  */
4279             if (id_expr_token->type == CPP_NAME
4280                 && id_expr_token->ambiguous_p)
4281               {
4282                 cp_parser_simulate_error (parser);
4283                 return error_mark_node;
4284               }
4285
4286             decl = cp_parser_lookup_name (parser, id_expression,
4287                                           none_type,
4288                                           template_p,
4289                                           /*is_namespace=*/false,
4290                                           /*check_dependency=*/true,
4291                                           &ambiguous_decls,
4292                                           id_expr_token->location);
4293             /* If the lookup was ambiguous, an error will already have
4294                been issued.  */
4295             if (ambiguous_decls)
4296               return error_mark_node;
4297
4298             /* In Objective-C++, we may have an Objective-C 2.0
4299                dot-syntax for classes here.  */
4300             if (c_dialect_objc ()
4301                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4302                 && TREE_CODE (decl) == TYPE_DECL
4303                 && objc_is_class_name (decl))
4304               {
4305                 tree component;
4306                 cp_lexer_consume_token (parser->lexer);
4307                 component = cp_parser_identifier (parser);
4308                 if (component == error_mark_node)
4309                   return error_mark_node;
4310
4311                 return objc_build_class_component_ref (id_expression, component);
4312               }
4313
4314             /* In Objective-C++, an instance variable (ivar) may be preferred
4315                to whatever cp_parser_lookup_name() found.  */
4316             decl = objc_lookup_ivar (decl, id_expression);
4317
4318             /* If name lookup gives us a SCOPE_REF, then the
4319                qualifying scope was dependent.  */
4320             if (TREE_CODE (decl) == SCOPE_REF)
4321               {
4322                 /* At this point, we do not know if DECL is a valid
4323                    integral constant expression.  We assume that it is
4324                    in fact such an expression, so that code like:
4325
4326                       template <int N> struct A {
4327                         int a[B<N>::i];
4328                       };
4329                      
4330                    is accepted.  At template-instantiation time, we
4331                    will check that B<N>::i is actually a constant.  */
4332                 return decl;
4333               }
4334             /* Check to see if DECL is a local variable in a context
4335                where that is forbidden.  */
4336             if (parser->local_variables_forbidden_p
4337                 && local_variable_p (decl))
4338               {
4339                 /* It might be that we only found DECL because we are
4340                    trying to be generous with pre-ISO scoping rules.
4341                    For example, consider:
4342
4343                      int i;
4344                      void g() {
4345                        for (int i = 0; i < 10; ++i) {}
4346                        extern void f(int j = i);
4347                      }
4348
4349                    Here, name look up will originally find the out
4350                    of scope `i'.  We need to issue a warning message,
4351                    but then use the global `i'.  */
4352                 decl = check_for_out_of_scope_variable (decl);
4353                 if (local_variable_p (decl))
4354                   {
4355                     error_at (id_expr_token->location,
4356                               "local variable %qD may not appear in this context",
4357                               decl);
4358                     return error_mark_node;
4359                   }
4360               }
4361           }
4362
4363         decl = (finish_id_expression
4364                 (id_expression, decl, parser->scope,
4365                  idk,
4366                  parser->integral_constant_expression_p,
4367                  parser->allow_non_integral_constant_expression_p,
4368                  &parser->non_integral_constant_expression_p,
4369                  template_p, done, address_p,
4370                  template_arg_p,
4371                  &error_msg,
4372                  id_expr_token->location));
4373         if (error_msg)
4374           cp_parser_error (parser, error_msg);
4375         return decl;
4376       }
4377
4378       /* Anything else is an error.  */
4379     default:
4380       cp_parser_error (parser, "expected primary-expression");
4381       return error_mark_node;
4382     }
4383 }
4384
4385 /* Parse an id-expression.
4386
4387    id-expression:
4388      unqualified-id
4389      qualified-id
4390
4391    qualified-id:
4392      :: [opt] nested-name-specifier template [opt] unqualified-id
4393      :: identifier
4394      :: operator-function-id
4395      :: template-id
4396
4397    Return a representation of the unqualified portion of the
4398    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4399    a `::' or nested-name-specifier.
4400
4401    Often, if the id-expression was a qualified-id, the caller will
4402    want to make a SCOPE_REF to represent the qualified-id.  This
4403    function does not do this in order to avoid wastefully creating
4404    SCOPE_REFs when they are not required.
4405
4406    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4407    `template' keyword.
4408
4409    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4410    uninstantiated templates.
4411
4412    If *TEMPLATE_P is non-NULL, it is set to true iff the
4413    `template' keyword is used to explicitly indicate that the entity
4414    named is a template.
4415
4416    If DECLARATOR_P is true, the id-expression is appearing as part of
4417    a declarator, rather than as part of an expression.  */
4418
4419 static tree
4420 cp_parser_id_expression (cp_parser *parser,
4421                          bool template_keyword_p,
4422                          bool check_dependency_p,
4423                          bool *template_p,
4424                          bool declarator_p,
4425                          bool optional_p)
4426 {
4427   bool global_scope_p;
4428   bool nested_name_specifier_p;
4429
4430   /* Assume the `template' keyword was not used.  */
4431   if (template_p)
4432     *template_p = template_keyword_p;
4433
4434   /* Look for the optional `::' operator.  */
4435   global_scope_p
4436     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4437        != NULL_TREE);
4438   /* Look for the optional nested-name-specifier.  */
4439   nested_name_specifier_p
4440     = (cp_parser_nested_name_specifier_opt (parser,
4441                                             /*typename_keyword_p=*/false,
4442                                             check_dependency_p,
4443                                             /*type_p=*/false,
4444                                             declarator_p)
4445        != NULL_TREE);
4446   /* If there is a nested-name-specifier, then we are looking at
4447      the first qualified-id production.  */
4448   if (nested_name_specifier_p)
4449     {
4450       tree saved_scope;
4451       tree saved_object_scope;
4452       tree saved_qualifying_scope;
4453       tree unqualified_id;
4454       bool is_template;
4455
4456       /* See if the next token is the `template' keyword.  */
4457       if (!template_p)
4458         template_p = &is_template;
4459       *template_p = cp_parser_optional_template_keyword (parser);
4460       /* Name lookup we do during the processing of the
4461          unqualified-id might obliterate SCOPE.  */
4462       saved_scope = parser->scope;
4463       saved_object_scope = parser->object_scope;
4464       saved_qualifying_scope = parser->qualifying_scope;
4465       /* Process the final unqualified-id.  */
4466       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4467                                                  check_dependency_p,
4468                                                  declarator_p,
4469                                                  /*optional_p=*/false);
4470       /* Restore the SAVED_SCOPE for our caller.  */
4471       parser->scope = saved_scope;
4472       parser->object_scope = saved_object_scope;
4473       parser->qualifying_scope = saved_qualifying_scope;
4474
4475       return unqualified_id;
4476     }
4477   /* Otherwise, if we are in global scope, then we are looking at one
4478      of the other qualified-id productions.  */
4479   else if (global_scope_p)
4480     {
4481       cp_token *token;
4482       tree id;
4483
4484       /* Peek at the next token.  */
4485       token = cp_lexer_peek_token (parser->lexer);
4486
4487       /* If it's an identifier, and the next token is not a "<", then
4488          we can avoid the template-id case.  This is an optimization
4489          for this common case.  */
4490       if (token->type == CPP_NAME
4491           && !cp_parser_nth_token_starts_template_argument_list_p
4492                (parser, 2))
4493         return cp_parser_identifier (parser);
4494
4495       cp_parser_parse_tentatively (parser);
4496       /* Try a template-id.  */
4497       id = cp_parser_template_id (parser,
4498                                   /*template_keyword_p=*/false,
4499                                   /*check_dependency_p=*/true,
4500                                   declarator_p);
4501       /* If that worked, we're done.  */
4502       if (cp_parser_parse_definitely (parser))
4503         return id;
4504
4505       /* Peek at the next token.  (Changes in the token buffer may
4506          have invalidated the pointer obtained above.)  */
4507       token = cp_lexer_peek_token (parser->lexer);
4508
4509       switch (token->type)
4510         {
4511         case CPP_NAME:
4512           return cp_parser_identifier (parser);
4513
4514         case CPP_KEYWORD:
4515           if (token->keyword == RID_OPERATOR)
4516             return cp_parser_operator_function_id (parser);
4517           /* Fall through.  */
4518
4519         default:
4520           cp_parser_error (parser, "expected id-expression");
4521           return error_mark_node;
4522         }
4523     }
4524   else
4525     return cp_parser_unqualified_id (parser, template_keyword_p,
4526                                      /*check_dependency_p=*/true,
4527                                      declarator_p,
4528                                      optional_p);
4529 }
4530
4531 /* Parse an unqualified-id.
4532
4533    unqualified-id:
4534      identifier
4535      operator-function-id
4536      conversion-function-id
4537      ~ class-name
4538      template-id
4539
4540    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4541    keyword, in a construct like `A::template ...'.
4542
4543    Returns a representation of unqualified-id.  For the `identifier'
4544    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4545    production a BIT_NOT_EXPR is returned; the operand of the
4546    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4547    other productions, see the documentation accompanying the
4548    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4549    names are looked up in uninstantiated templates.  If DECLARATOR_P
4550    is true, the unqualified-id is appearing as part of a declarator,
4551    rather than as part of an expression.  */
4552
4553 static tree
4554 cp_parser_unqualified_id (cp_parser* parser,
4555                           bool template_keyword_p,
4556                           bool check_dependency_p,
4557                           bool declarator_p,
4558                           bool optional_p)
4559 {
4560   cp_token *token;
4561
4562   /* Peek at the next token.  */
4563   token = cp_lexer_peek_token (parser->lexer);
4564
4565   switch (token->type)
4566     {
4567     case CPP_NAME:
4568       {
4569         tree id;
4570
4571         /* We don't know yet whether or not this will be a
4572            template-id.  */
4573         cp_parser_parse_tentatively (parser);
4574         /* Try a template-id.  */
4575         id = cp_parser_template_id (parser, template_keyword_p,
4576                                     check_dependency_p,
4577                                     declarator_p);
4578         /* If it worked, we're done.  */
4579         if (cp_parser_parse_definitely (parser))
4580           return id;
4581         /* Otherwise, it's an ordinary identifier.  */
4582         return cp_parser_identifier (parser);
4583       }
4584
4585     case CPP_TEMPLATE_ID:
4586       return cp_parser_template_id (parser, template_keyword_p,
4587                                     check_dependency_p,
4588                                     declarator_p);
4589
4590     case CPP_COMPL:
4591       {
4592         tree type_decl;
4593         tree qualifying_scope;
4594         tree object_scope;
4595         tree scope;
4596         bool done;
4597
4598         /* Consume the `~' token.  */
4599         cp_lexer_consume_token (parser->lexer);
4600         /* Parse the class-name.  The standard, as written, seems to
4601            say that:
4602
4603              template <typename T> struct S { ~S (); };
4604              template <typename T> S<T>::~S() {}
4605
4606            is invalid, since `~' must be followed by a class-name, but
4607            `S<T>' is dependent, and so not known to be a class.
4608            That's not right; we need to look in uninstantiated
4609            templates.  A further complication arises from:
4610
4611              template <typename T> void f(T t) {
4612                t.T::~T();
4613              }
4614
4615            Here, it is not possible to look up `T' in the scope of `T'
4616            itself.  We must look in both the current scope, and the
4617            scope of the containing complete expression.
4618
4619            Yet another issue is:
4620
4621              struct S {
4622                int S;
4623                ~S();
4624              };
4625
4626              S::~S() {}
4627
4628            The standard does not seem to say that the `S' in `~S'
4629            should refer to the type `S' and not the data member
4630            `S::S'.  */
4631
4632         /* DR 244 says that we look up the name after the "~" in the
4633            same scope as we looked up the qualifying name.  That idea
4634            isn't fully worked out; it's more complicated than that.  */
4635         scope = parser->scope;
4636         object_scope = parser->object_scope;
4637         qualifying_scope = parser->qualifying_scope;
4638
4639         /* Check for invalid scopes.  */
4640         if (scope == error_mark_node)
4641           {
4642             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4643               cp_lexer_consume_token (parser->lexer);
4644             return error_mark_node;
4645           }
4646         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4647           {
4648             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4649               error_at (token->location,
4650                         "scope %qT before %<~%> is not a class-name",
4651                         scope);
4652             cp_parser_simulate_error (parser);
4653             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4654               cp_lexer_consume_token (parser->lexer);
4655             return error_mark_node;
4656           }
4657         gcc_assert (!scope || TYPE_P (scope));
4658
4659         /* If the name is of the form "X::~X" it's OK even if X is a
4660            typedef.  */
4661         token = cp_lexer_peek_token (parser->lexer);
4662         if (scope
4663             && token->type == CPP_NAME
4664             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4665                 != CPP_LESS)
4666             && (token->u.value == TYPE_IDENTIFIER (scope)
4667                 || (CLASS_TYPE_P (scope)
4668                     && constructor_name_p (token->u.value, scope))))
4669           {
4670             cp_lexer_consume_token (parser->lexer);
4671             return build_nt (BIT_NOT_EXPR, scope);
4672           }
4673
4674         /* If there was an explicit qualification (S::~T), first look
4675            in the scope given by the qualification (i.e., S).
4676
4677            Note: in the calls to cp_parser_class_name below we pass
4678            typename_type so that lookup finds the injected-class-name
4679            rather than the constructor.  */
4680         done = false;
4681         type_decl = NULL_TREE;
4682         if (scope)
4683           {
4684             cp_parser_parse_tentatively (parser);
4685             type_decl = cp_parser_class_name (parser,
4686                                               /*typename_keyword_p=*/false,
4687                                               /*template_keyword_p=*/false,
4688                                               typename_type,
4689                                               /*check_dependency=*/false,
4690                                               /*class_head_p=*/false,
4691                                               declarator_p);
4692             if (cp_parser_parse_definitely (parser))
4693               done = true;
4694           }
4695         /* In "N::S::~S", look in "N" as well.  */
4696         if (!done && scope && qualifying_scope)
4697           {
4698             cp_parser_parse_tentatively (parser);
4699             parser->scope = qualifying_scope;
4700             parser->object_scope = NULL_TREE;
4701             parser->qualifying_scope = NULL_TREE;
4702             type_decl
4703               = cp_parser_class_name (parser,
4704                                       /*typename_keyword_p=*/false,
4705                                       /*template_keyword_p=*/false,
4706                                       typename_type,
4707                                       /*check_dependency=*/false,
4708                                       /*class_head_p=*/false,
4709                                       declarator_p);
4710             if (cp_parser_parse_definitely (parser))
4711               done = true;
4712           }
4713         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4714         else if (!done && object_scope)
4715           {
4716             cp_parser_parse_tentatively (parser);
4717             parser->scope = object_scope;
4718             parser->object_scope = NULL_TREE;
4719             parser->qualifying_scope = NULL_TREE;
4720             type_decl
4721               = cp_parser_class_name (parser,
4722                                       /*typename_keyword_p=*/false,
4723                                       /*template_keyword_p=*/false,
4724                                       typename_type,
4725                                       /*check_dependency=*/false,
4726                                       /*class_head_p=*/false,
4727                                       declarator_p);
4728             if (cp_parser_parse_definitely (parser))
4729               done = true;
4730           }
4731         /* Look in the surrounding context.  */
4732         if (!done)
4733           {
4734             parser->scope = NULL_TREE;
4735             parser->object_scope = NULL_TREE;
4736             parser->qualifying_scope = NULL_TREE;
4737             if (processing_template_decl)
4738               cp_parser_parse_tentatively (parser);
4739             type_decl
4740               = cp_parser_class_name (parser,
4741                                       /*typename_keyword_p=*/false,
4742                                       /*template_keyword_p=*/false,
4743                                       typename_type,
4744                                       /*check_dependency=*/false,
4745                                       /*class_head_p=*/false,
4746                                       declarator_p);
4747             if (processing_template_decl
4748                 && ! cp_parser_parse_definitely (parser))
4749               {
4750                 /* We couldn't find a type with this name, so just accept
4751                    it and check for a match at instantiation time.  */
4752                 type_decl = cp_parser_identifier (parser);
4753                 if (type_decl != error_mark_node)
4754                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4755                 return type_decl;
4756               }
4757           }
4758         /* If an error occurred, assume that the name of the
4759            destructor is the same as the name of the qualifying
4760            class.  That allows us to keep parsing after running
4761            into ill-formed destructor names.  */
4762         if (type_decl == error_mark_node && scope)
4763           return build_nt (BIT_NOT_EXPR, scope);
4764         else if (type_decl == error_mark_node)
4765           return error_mark_node;
4766
4767         /* Check that destructor name and scope match.  */
4768         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4769           {
4770             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4771               error_at (token->location,
4772                         "declaration of %<~%T%> as member of %qT",
4773                         type_decl, scope);
4774             cp_parser_simulate_error (parser);
4775             return error_mark_node;
4776           }
4777
4778         /* [class.dtor]
4779
4780            A typedef-name that names a class shall not be used as the
4781            identifier in the declarator for a destructor declaration.  */
4782         if (declarator_p
4783             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4784             && !DECL_SELF_REFERENCE_P (type_decl)
4785             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4786           error_at (token->location,
4787                     "typedef-name %qD used as destructor declarator",
4788                     type_decl);
4789
4790         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4791       }
4792
4793     case CPP_KEYWORD:
4794       if (token->keyword == RID_OPERATOR)
4795         {
4796           tree id;
4797
4798           /* This could be a template-id, so we try that first.  */
4799           cp_parser_parse_tentatively (parser);
4800           /* Try a template-id.  */
4801           id = cp_parser_template_id (parser, template_keyword_p,
4802                                       /*check_dependency_p=*/true,
4803                                       declarator_p);
4804           /* If that worked, we're done.  */
4805           if (cp_parser_parse_definitely (parser))
4806             return id;
4807           /* We still don't know whether we're looking at an
4808              operator-function-id or a conversion-function-id.  */
4809           cp_parser_parse_tentatively (parser);
4810           /* Try an operator-function-id.  */
4811           id = cp_parser_operator_function_id (parser);
4812           /* If that didn't work, try a conversion-function-id.  */
4813           if (!cp_parser_parse_definitely (parser))
4814             id = cp_parser_conversion_function_id (parser);
4815           else if (UDLIT_OPER_P (id))
4816             {
4817               /* 17.6.3.3.5  */
4818               const char *name = UDLIT_OP_SUFFIX (id);
4819               if (name[0] != '_' && !in_system_header)
4820                 warning (0, "literal operator suffixes not preceded by %<_%>"
4821                             " are reserved for future standardization");
4822             }
4823
4824           return id;
4825         }
4826       /* Fall through.  */
4827
4828     default:
4829       if (optional_p)
4830         return NULL_TREE;
4831       cp_parser_error (parser, "expected unqualified-id");
4832       return error_mark_node;
4833     }
4834 }
4835
4836 /* Parse an (optional) nested-name-specifier.
4837
4838    nested-name-specifier: [C++98]
4839      class-or-namespace-name :: nested-name-specifier [opt]
4840      class-or-namespace-name :: template nested-name-specifier [opt]
4841
4842    nested-name-specifier: [C++0x]
4843      type-name ::
4844      namespace-name ::
4845      nested-name-specifier identifier ::
4846      nested-name-specifier template [opt] simple-template-id ::
4847
4848    PARSER->SCOPE should be set appropriately before this function is
4849    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4850    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4851    in name lookups.
4852
4853    Sets PARSER->SCOPE to the class (TYPE) or namespace
4854    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4855    it unchanged if there is no nested-name-specifier.  Returns the new
4856    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4857
4858    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4859    part of a declaration and/or decl-specifier.  */
4860
4861 static tree
4862 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4863                                      bool typename_keyword_p,
4864                                      bool check_dependency_p,
4865                                      bool type_p,
4866                                      bool is_declaration)
4867 {
4868   bool success = false;
4869   cp_token_position start = 0;
4870   cp_token *token;
4871
4872   /* Remember where the nested-name-specifier starts.  */
4873   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4874     {
4875       start = cp_lexer_token_position (parser->lexer, false);
4876       push_deferring_access_checks (dk_deferred);
4877     }
4878
4879   while (true)
4880     {
4881       tree new_scope;
4882       tree old_scope;
4883       tree saved_qualifying_scope;
4884       bool template_keyword_p;
4885
4886       /* Spot cases that cannot be the beginning of a
4887          nested-name-specifier.  */
4888       token = cp_lexer_peek_token (parser->lexer);
4889
4890       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4891          the already parsed nested-name-specifier.  */
4892       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4893         {
4894           /* Grab the nested-name-specifier and continue the loop.  */
4895           cp_parser_pre_parsed_nested_name_specifier (parser);
4896           /* If we originally encountered this nested-name-specifier
4897              with IS_DECLARATION set to false, we will not have
4898              resolved TYPENAME_TYPEs, so we must do so here.  */
4899           if (is_declaration
4900               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4901             {
4902               new_scope = resolve_typename_type (parser->scope,
4903                                                  /*only_current_p=*/false);
4904               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4905                 parser->scope = new_scope;
4906             }
4907           success = true;
4908           continue;
4909         }
4910
4911       /* Spot cases that cannot be the beginning of a
4912          nested-name-specifier.  On the second and subsequent times
4913          through the loop, we look for the `template' keyword.  */
4914       if (success && token->keyword == RID_TEMPLATE)
4915         ;
4916       /* A template-id can start a nested-name-specifier.  */
4917       else if (token->type == CPP_TEMPLATE_ID)
4918         ;
4919       /* DR 743: decltype can be used in a nested-name-specifier.  */
4920       else if (token_is_decltype (token))
4921         ;
4922       else
4923         {
4924           /* If the next token is not an identifier, then it is
4925              definitely not a type-name or namespace-name.  */
4926           if (token->type != CPP_NAME)
4927             break;
4928           /* If the following token is neither a `<' (to begin a
4929              template-id), nor a `::', then we are not looking at a
4930              nested-name-specifier.  */
4931           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4932
4933           if (token->type == CPP_COLON
4934               && parser->colon_corrects_to_scope_p
4935               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4936             {
4937               error_at (token->location,
4938                         "found %<:%> in nested-name-specifier, expected %<::%>");
4939               token->type = CPP_SCOPE;
4940             }
4941
4942           if (token->type != CPP_SCOPE
4943               && !cp_parser_nth_token_starts_template_argument_list_p
4944                   (parser, 2))
4945             break;
4946         }
4947
4948       /* The nested-name-specifier is optional, so we parse
4949          tentatively.  */
4950       cp_parser_parse_tentatively (parser);
4951
4952       /* Look for the optional `template' keyword, if this isn't the
4953          first time through the loop.  */
4954       if (success)
4955         template_keyword_p = cp_parser_optional_template_keyword (parser);
4956       else
4957         template_keyword_p = false;
4958
4959       /* Save the old scope since the name lookup we are about to do
4960          might destroy it.  */
4961       old_scope = parser->scope;
4962       saved_qualifying_scope = parser->qualifying_scope;
4963       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4964          look up names in "X<T>::I" in order to determine that "Y" is
4965          a template.  So, if we have a typename at this point, we make
4966          an effort to look through it.  */
4967       if (is_declaration
4968           && !typename_keyword_p
4969           && parser->scope
4970           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4971         parser->scope = resolve_typename_type (parser->scope,
4972                                                /*only_current_p=*/false);
4973       /* Parse the qualifying entity.  */
4974       new_scope
4975         = cp_parser_qualifying_entity (parser,
4976                                        typename_keyword_p,
4977                                        template_keyword_p,
4978                                        check_dependency_p,
4979                                        type_p,
4980                                        is_declaration);
4981       /* Look for the `::' token.  */
4982       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4983
4984       /* If we found what we wanted, we keep going; otherwise, we're
4985          done.  */
4986       if (!cp_parser_parse_definitely (parser))
4987         {
4988           bool error_p = false;
4989
4990           /* Restore the OLD_SCOPE since it was valid before the
4991              failed attempt at finding the last
4992              class-or-namespace-name.  */
4993           parser->scope = old_scope;
4994           parser->qualifying_scope = saved_qualifying_scope;
4995
4996           /* If the next token is a decltype, and the one after that is a
4997              `::', then the decltype has failed to resolve to a class or
4998              enumeration type.  Give this error even when parsing
4999              tentatively since it can't possibly be valid--and we're going
5000              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5001              won't get another chance.*/
5002           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5003               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5004                   == CPP_SCOPE))
5005             {
5006               token = cp_lexer_consume_token (parser->lexer);
5007               error_at (token->location, "decltype evaluates to %qT, "
5008                         "which is not a class or enumeration type",
5009                         token->u.value);
5010               parser->scope = error_mark_node;
5011               error_p = true;
5012               /* As below.  */
5013               success = true;
5014               cp_lexer_consume_token (parser->lexer);
5015             }
5016
5017           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5018             break;
5019           /* If the next token is an identifier, and the one after
5020              that is a `::', then any valid interpretation would have
5021              found a class-or-namespace-name.  */
5022           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5023                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5024                      == CPP_SCOPE)
5025                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5026                      != CPP_COMPL))
5027             {
5028               token = cp_lexer_consume_token (parser->lexer);
5029               if (!error_p)
5030                 {
5031                   if (!token->ambiguous_p)
5032                     {
5033                       tree decl;
5034                       tree ambiguous_decls;
5035
5036                       decl = cp_parser_lookup_name (parser, token->u.value,
5037                                                     none_type,
5038                                                     /*is_template=*/false,
5039                                                     /*is_namespace=*/false,
5040                                                     /*check_dependency=*/true,
5041                                                     &ambiguous_decls,
5042                                                     token->location);
5043                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5044                         error_at (token->location,
5045                                   "%qD used without template parameters",
5046                                   decl);
5047                       else if (ambiguous_decls)
5048                         {
5049                           error_at (token->location,
5050                                     "reference to %qD is ambiguous",
5051                                     token->u.value);
5052                           print_candidates (ambiguous_decls);
5053                           decl = error_mark_node;
5054                         }
5055                       else
5056                         {
5057                           if (cxx_dialect != cxx98)
5058                             cp_parser_name_lookup_error
5059                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5060                              token->location);
5061                           else
5062                             cp_parser_name_lookup_error
5063                             (parser, token->u.value, decl, NLE_CXX98,
5064                              token->location);
5065                         }
5066                     }
5067                   parser->scope = error_mark_node;
5068                   error_p = true;
5069                   /* Treat this as a successful nested-name-specifier
5070                      due to:
5071
5072                      [basic.lookup.qual]
5073
5074                      If the name found is not a class-name (clause
5075                      _class_) or namespace-name (_namespace.def_), the
5076                      program is ill-formed.  */
5077                   success = true;
5078                 }
5079               cp_lexer_consume_token (parser->lexer);
5080             }
5081           break;
5082         }
5083       /* We've found one valid nested-name-specifier.  */
5084       success = true;
5085       /* Name lookup always gives us a DECL.  */
5086       if (TREE_CODE (new_scope) == TYPE_DECL)
5087         new_scope = TREE_TYPE (new_scope);
5088       /* Uses of "template" must be followed by actual templates.  */
5089       if (template_keyword_p
5090           && !(CLASS_TYPE_P (new_scope)
5091                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5092                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5093                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5094           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5095                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5096                    == TEMPLATE_ID_EXPR)))
5097         permerror (input_location, TYPE_P (new_scope)
5098                    ? G_("%qT is not a template")
5099                    : G_("%qD is not a template"),
5100                    new_scope);
5101       /* If it is a class scope, try to complete it; we are about to
5102          be looking up names inside the class.  */
5103       if (TYPE_P (new_scope)
5104           /* Since checking types for dependency can be expensive,
5105              avoid doing it if the type is already complete.  */
5106           && !COMPLETE_TYPE_P (new_scope)
5107           /* Do not try to complete dependent types.  */
5108           && !dependent_type_p (new_scope))
5109         {
5110           new_scope = complete_type (new_scope);
5111           /* If it is a typedef to current class, use the current
5112              class instead, as the typedef won't have any names inside
5113              it yet.  */
5114           if (!COMPLETE_TYPE_P (new_scope)
5115               && currently_open_class (new_scope))
5116             new_scope = TYPE_MAIN_VARIANT (new_scope);
5117         }
5118       /* Make sure we look in the right scope the next time through
5119          the loop.  */
5120       parser->scope = new_scope;
5121     }
5122
5123   /* If parsing tentatively, replace the sequence of tokens that makes
5124      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5125      token.  That way, should we re-parse the token stream, we will
5126      not have to repeat the effort required to do the parse, nor will
5127      we issue duplicate error messages.  */
5128   if (success && start)
5129     {
5130       cp_token *token;
5131
5132       token = cp_lexer_token_at (parser->lexer, start);
5133       /* Reset the contents of the START token.  */
5134       token->type = CPP_NESTED_NAME_SPECIFIER;
5135       /* Retrieve any deferred checks.  Do not pop this access checks yet
5136          so the memory will not be reclaimed during token replacing below.  */
5137       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5138       token->u.tree_check_value->value = parser->scope;
5139       token->u.tree_check_value->checks = get_deferred_access_checks ();
5140       token->u.tree_check_value->qualifying_scope =
5141         parser->qualifying_scope;
5142       token->keyword = RID_MAX;
5143
5144       /* Purge all subsequent tokens.  */
5145       cp_lexer_purge_tokens_after (parser->lexer, start);
5146     }
5147
5148   if (start)
5149     pop_to_parent_deferring_access_checks ();
5150
5151   return success ? parser->scope : NULL_TREE;
5152 }
5153
5154 /* Parse a nested-name-specifier.  See
5155    cp_parser_nested_name_specifier_opt for details.  This function
5156    behaves identically, except that it will an issue an error if no
5157    nested-name-specifier is present.  */
5158
5159 static tree
5160 cp_parser_nested_name_specifier (cp_parser *parser,
5161                                  bool typename_keyword_p,
5162                                  bool check_dependency_p,
5163                                  bool type_p,
5164                                  bool is_declaration)
5165 {
5166   tree scope;
5167
5168   /* Look for the nested-name-specifier.  */
5169   scope = cp_parser_nested_name_specifier_opt (parser,
5170                                                typename_keyword_p,
5171                                                check_dependency_p,
5172                                                type_p,
5173                                                is_declaration);
5174   /* If it was not present, issue an error message.  */
5175   if (!scope)
5176     {
5177       cp_parser_error (parser, "expected nested-name-specifier");
5178       parser->scope = NULL_TREE;
5179     }
5180
5181   return scope;
5182 }
5183
5184 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5185    this is either a class-name or a namespace-name (which corresponds
5186    to the class-or-namespace-name production in the grammar). For
5187    C++0x, it can also be a type-name that refers to an enumeration
5188    type or a simple-template-id.
5189
5190    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5191    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5192    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5193    TYPE_P is TRUE iff the next name should be taken as a class-name,
5194    even the same name is declared to be another entity in the same
5195    scope.
5196
5197    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5198    specified by the class-or-namespace-name.  If neither is found the
5199    ERROR_MARK_NODE is returned.  */
5200
5201 static tree
5202 cp_parser_qualifying_entity (cp_parser *parser,
5203                              bool typename_keyword_p,
5204                              bool template_keyword_p,
5205                              bool check_dependency_p,
5206                              bool type_p,
5207                              bool is_declaration)
5208 {
5209   tree saved_scope;
5210   tree saved_qualifying_scope;
5211   tree saved_object_scope;
5212   tree scope;
5213   bool only_class_p;
5214   bool successful_parse_p;
5215
5216   /* DR 743: decltype can appear in a nested-name-specifier.  */
5217   if (cp_lexer_next_token_is_decltype (parser->lexer))
5218     {
5219       scope = cp_parser_decltype (parser);
5220       if (TREE_CODE (scope) != ENUMERAL_TYPE
5221           && !MAYBE_CLASS_TYPE_P (scope))
5222         {
5223           cp_parser_simulate_error (parser);
5224           return error_mark_node;
5225         }
5226       if (TYPE_NAME (scope))
5227         scope = TYPE_NAME (scope);
5228       return scope;
5229     }
5230
5231   /* Before we try to parse the class-name, we must save away the
5232      current PARSER->SCOPE since cp_parser_class_name will destroy
5233      it.  */
5234   saved_scope = parser->scope;
5235   saved_qualifying_scope = parser->qualifying_scope;
5236   saved_object_scope = parser->object_scope;
5237   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5238      there is no need to look for a namespace-name.  */
5239   only_class_p = template_keyword_p 
5240     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5241   if (!only_class_p)
5242     cp_parser_parse_tentatively (parser);
5243   scope = cp_parser_class_name (parser,
5244                                 typename_keyword_p,
5245                                 template_keyword_p,
5246                                 type_p ? class_type : none_type,
5247                                 check_dependency_p,
5248                                 /*class_head_p=*/false,
5249                                 is_declaration);
5250   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5251   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5252   if (!only_class_p 
5253       && cxx_dialect != cxx98
5254       && !successful_parse_p)
5255     {
5256       /* Restore the saved scope.  */
5257       parser->scope = saved_scope;
5258       parser->qualifying_scope = saved_qualifying_scope;
5259       parser->object_scope = saved_object_scope;
5260
5261       /* Parse tentatively.  */
5262       cp_parser_parse_tentatively (parser);
5263      
5264       /* Parse a type-name  */
5265       scope = cp_parser_type_name (parser);
5266
5267       /* "If the name found does not designate a namespace or a class,
5268          enumeration, or dependent type, the program is ill-formed."
5269
5270          We cover classes and dependent types above and namespaces below,
5271          so this code is only looking for enums.  */
5272       if (!scope || TREE_CODE (scope) != TYPE_DECL
5273           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5274         cp_parser_simulate_error (parser);
5275
5276       successful_parse_p = cp_parser_parse_definitely (parser);
5277     }
5278   /* If that didn't work, try for a namespace-name.  */
5279   if (!only_class_p && !successful_parse_p)
5280     {
5281       /* Restore the saved scope.  */
5282       parser->scope = saved_scope;
5283       parser->qualifying_scope = saved_qualifying_scope;
5284       parser->object_scope = saved_object_scope;
5285       /* If we are not looking at an identifier followed by the scope
5286          resolution operator, then this is not part of a
5287          nested-name-specifier.  (Note that this function is only used
5288          to parse the components of a nested-name-specifier.)  */
5289       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5290           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5291         return error_mark_node;
5292       scope = cp_parser_namespace_name (parser);
5293     }
5294
5295   return scope;
5296 }
5297
5298 /* Parse a postfix-expression.
5299
5300    postfix-expression:
5301      primary-expression
5302      postfix-expression [ expression ]
5303      postfix-expression ( expression-list [opt] )
5304      simple-type-specifier ( expression-list [opt] )
5305      typename :: [opt] nested-name-specifier identifier
5306        ( expression-list [opt] )
5307      typename :: [opt] nested-name-specifier template [opt] template-id
5308        ( expression-list [opt] )
5309      postfix-expression . template [opt] id-expression
5310      postfix-expression -> template [opt] id-expression
5311      postfix-expression . pseudo-destructor-name
5312      postfix-expression -> pseudo-destructor-name
5313      postfix-expression ++
5314      postfix-expression --
5315      dynamic_cast < type-id > ( expression )
5316      static_cast < type-id > ( expression )
5317      reinterpret_cast < type-id > ( expression )
5318      const_cast < type-id > ( expression )
5319      typeid ( expression )
5320      typeid ( type-id )
5321
5322    GNU Extension:
5323
5324    postfix-expression:
5325      ( type-id ) { initializer-list , [opt] }
5326
5327    This extension is a GNU version of the C99 compound-literal
5328    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5329    but they are essentially the same concept.)
5330
5331    If ADDRESS_P is true, the postfix expression is the operand of the
5332    `&' operator.  CAST_P is true if this expression is the target of a
5333    cast.
5334
5335    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5336    class member access expressions [expr.ref].
5337
5338    Returns a representation of the expression.  */
5339
5340 static tree
5341 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5342                               bool member_access_only_p,
5343                               cp_id_kind * pidk_return)
5344 {
5345   cp_token *token;
5346   enum rid keyword;
5347   cp_id_kind idk = CP_ID_KIND_NONE;
5348   tree postfix_expression = NULL_TREE;
5349   bool is_member_access = false;
5350
5351   /* Peek at the next token.  */
5352   token = cp_lexer_peek_token (parser->lexer);
5353   /* Some of the productions are determined by keywords.  */
5354   keyword = token->keyword;
5355   switch (keyword)
5356     {
5357     case RID_DYNCAST:
5358     case RID_STATCAST:
5359     case RID_REINTCAST:
5360     case RID_CONSTCAST:
5361       {
5362         tree type;
5363         tree expression;
5364         const char *saved_message;
5365
5366         /* All of these can be handled in the same way from the point
5367            of view of parsing.  Begin by consuming the token
5368            identifying the cast.  */
5369         cp_lexer_consume_token (parser->lexer);
5370
5371         /* New types cannot be defined in the cast.  */
5372         saved_message = parser->type_definition_forbidden_message;
5373         parser->type_definition_forbidden_message
5374           = G_("types may not be defined in casts");
5375
5376         /* Look for the opening `<'.  */
5377         cp_parser_require (parser, CPP_LESS, RT_LESS);
5378         /* Parse the type to which we are casting.  */
5379         type = cp_parser_type_id (parser);
5380         /* Look for the closing `>'.  */
5381         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5382         /* Restore the old message.  */
5383         parser->type_definition_forbidden_message = saved_message;
5384
5385         /* And the expression which is being cast.  */
5386         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5387         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5388         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5389
5390         /* Only type conversions to integral or enumeration types
5391            can be used in constant-expressions.  */
5392         if (!cast_valid_in_integral_constant_expression_p (type)
5393             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5394           return error_mark_node;
5395
5396         switch (keyword)
5397           {
5398           case RID_DYNCAST:
5399             postfix_expression
5400               = build_dynamic_cast (type, expression, tf_warning_or_error);
5401             break;
5402           case RID_STATCAST:
5403             postfix_expression
5404               = build_static_cast (type, expression, tf_warning_or_error);
5405             break;
5406           case RID_REINTCAST:
5407             postfix_expression
5408               = build_reinterpret_cast (type, expression, 
5409                                         tf_warning_or_error);
5410             break;
5411           case RID_CONSTCAST:
5412             postfix_expression
5413               = build_const_cast (type, expression, tf_warning_or_error);
5414             break;
5415           default:
5416             gcc_unreachable ();
5417           }
5418       }
5419       break;
5420
5421     case RID_TYPEID:
5422       {
5423         tree type;
5424         const char *saved_message;
5425         bool saved_in_type_id_in_expr_p;
5426
5427         /* Consume the `typeid' token.  */
5428         cp_lexer_consume_token (parser->lexer);
5429         /* Look for the `(' token.  */
5430         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5431         /* Types cannot be defined in a `typeid' expression.  */
5432         saved_message = parser->type_definition_forbidden_message;
5433         parser->type_definition_forbidden_message
5434           = G_("types may not be defined in a %<typeid%> expression");
5435         /* We can't be sure yet whether we're looking at a type-id or an
5436            expression.  */
5437         cp_parser_parse_tentatively (parser);
5438         /* Try a type-id first.  */
5439         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5440         parser->in_type_id_in_expr_p = true;
5441         type = cp_parser_type_id (parser);
5442         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5443         /* Look for the `)' token.  Otherwise, we can't be sure that
5444            we're not looking at an expression: consider `typeid (int
5445            (3))', for example.  */
5446         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5447         /* If all went well, simply lookup the type-id.  */
5448         if (cp_parser_parse_definitely (parser))
5449           postfix_expression = get_typeid (type);
5450         /* Otherwise, fall back to the expression variant.  */
5451         else
5452           {
5453             tree expression;
5454
5455             /* Look for an expression.  */
5456             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5457             /* Compute its typeid.  */
5458             postfix_expression = build_typeid (expression);
5459             /* Look for the `)' token.  */
5460             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5461           }
5462         /* Restore the saved message.  */
5463         parser->type_definition_forbidden_message = saved_message;
5464         /* `typeid' may not appear in an integral constant expression.  */
5465         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5466           return error_mark_node;
5467       }
5468       break;
5469
5470     case RID_TYPENAME:
5471       {
5472         tree type;
5473         /* The syntax permitted here is the same permitted for an
5474            elaborated-type-specifier.  */
5475         type = cp_parser_elaborated_type_specifier (parser,
5476                                                     /*is_friend=*/false,
5477                                                     /*is_declaration=*/false);
5478         postfix_expression = cp_parser_functional_cast (parser, type);
5479       }
5480       break;
5481
5482     default:
5483       {
5484         tree type;
5485
5486         /* If the next thing is a simple-type-specifier, we may be
5487            looking at a functional cast.  We could also be looking at
5488            an id-expression.  So, we try the functional cast, and if
5489            that doesn't work we fall back to the primary-expression.  */
5490         cp_parser_parse_tentatively (parser);
5491         /* Look for the simple-type-specifier.  */
5492         type = cp_parser_simple_type_specifier (parser,
5493                                                 /*decl_specs=*/NULL,
5494                                                 CP_PARSER_FLAGS_NONE);
5495         /* Parse the cast itself.  */
5496         if (!cp_parser_error_occurred (parser))
5497           postfix_expression
5498             = cp_parser_functional_cast (parser, type);
5499         /* If that worked, we're done.  */
5500         if (cp_parser_parse_definitely (parser))
5501           break;
5502
5503         /* If the functional-cast didn't work out, try a
5504            compound-literal.  */
5505         if (cp_parser_allow_gnu_extensions_p (parser)
5506             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5507           {
5508             VEC(constructor_elt,gc) *initializer_list = NULL;
5509             bool saved_in_type_id_in_expr_p;
5510
5511             cp_parser_parse_tentatively (parser);
5512             /* Consume the `('.  */
5513             cp_lexer_consume_token (parser->lexer);
5514             /* Parse the type.  */
5515             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5516             parser->in_type_id_in_expr_p = true;
5517             type = cp_parser_type_id (parser);
5518             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5519             /* Look for the `)'.  */
5520             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5521             /* Look for the `{'.  */
5522             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5523             /* If things aren't going well, there's no need to
5524                keep going.  */
5525             if (!cp_parser_error_occurred (parser))
5526               {
5527                 bool non_constant_p;
5528                 /* Parse the initializer-list.  */
5529                 initializer_list
5530                   = cp_parser_initializer_list (parser, &non_constant_p);
5531                 /* Allow a trailing `,'.  */
5532                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5533                   cp_lexer_consume_token (parser->lexer);
5534                 /* Look for the final `}'.  */
5535                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5536               }
5537             /* If that worked, we're definitely looking at a
5538                compound-literal expression.  */
5539             if (cp_parser_parse_definitely (parser))
5540               {
5541                 /* Warn the user that a compound literal is not
5542                    allowed in standard C++.  */
5543                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5544                 /* For simplicity, we disallow compound literals in
5545                    constant-expressions.  We could
5546                    allow compound literals of integer type, whose
5547                    initializer was a constant, in constant
5548                    expressions.  Permitting that usage, as a further
5549                    extension, would not change the meaning of any
5550                    currently accepted programs.  (Of course, as
5551                    compound literals are not part of ISO C++, the
5552                    standard has nothing to say.)  */
5553                 if (cp_parser_non_integral_constant_expression (parser,
5554                                                                 NIC_NCC))
5555                   {
5556                     postfix_expression = error_mark_node;
5557                     break;
5558                   }
5559                 /* Form the representation of the compound-literal.  */
5560                 postfix_expression
5561                   = (finish_compound_literal
5562                      (type, build_constructor (init_list_type_node,
5563                                                initializer_list),
5564                       tf_warning_or_error));
5565                 break;
5566               }
5567           }
5568
5569         /* It must be a primary-expression.  */
5570         postfix_expression
5571           = cp_parser_primary_expression (parser, address_p, cast_p,
5572                                           /*template_arg_p=*/false,
5573                                           &idk);
5574       }
5575       break;
5576     }
5577
5578   /* Keep looping until the postfix-expression is complete.  */
5579   while (true)
5580     {
5581       if (idk == CP_ID_KIND_UNQUALIFIED
5582           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5583           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5584         /* It is not a Koenig lookup function call.  */
5585         postfix_expression
5586           = unqualified_name_lookup_error (postfix_expression);
5587
5588       /* Peek at the next token.  */
5589       token = cp_lexer_peek_token (parser->lexer);
5590
5591       switch (token->type)
5592         {
5593         case CPP_OPEN_SQUARE:
5594           postfix_expression
5595             = cp_parser_postfix_open_square_expression (parser,
5596                                                         postfix_expression,
5597                                                         false);
5598           idk = CP_ID_KIND_NONE;
5599           is_member_access = false;
5600           break;
5601
5602         case CPP_OPEN_PAREN:
5603           /* postfix-expression ( expression-list [opt] ) */
5604           {
5605             bool koenig_p;
5606             bool is_builtin_constant_p;
5607             bool saved_integral_constant_expression_p = false;
5608             bool saved_non_integral_constant_expression_p = false;
5609             VEC(tree,gc) *args;
5610
5611             is_member_access = false;
5612
5613             is_builtin_constant_p
5614               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5615             if (is_builtin_constant_p)
5616               {
5617                 /* The whole point of __builtin_constant_p is to allow
5618                    non-constant expressions to appear as arguments.  */
5619                 saved_integral_constant_expression_p
5620                   = parser->integral_constant_expression_p;
5621                 saved_non_integral_constant_expression_p
5622                   = parser->non_integral_constant_expression_p;
5623                 parser->integral_constant_expression_p = false;
5624               }
5625             args = (cp_parser_parenthesized_expression_list
5626                     (parser, non_attr,
5627                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5628                      /*non_constant_p=*/NULL));
5629             if (is_builtin_constant_p)
5630               {
5631                 parser->integral_constant_expression_p
5632                   = saved_integral_constant_expression_p;
5633                 parser->non_integral_constant_expression_p
5634                   = saved_non_integral_constant_expression_p;
5635               }
5636
5637             if (args == NULL)
5638               {
5639                 postfix_expression = error_mark_node;
5640                 break;
5641               }
5642
5643             /* Function calls are not permitted in
5644                constant-expressions.  */
5645             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5646                 && cp_parser_non_integral_constant_expression (parser,
5647                                                                NIC_FUNC_CALL))
5648               {
5649                 postfix_expression = error_mark_node;
5650                 release_tree_vector (args);
5651                 break;
5652               }
5653
5654             koenig_p = false;
5655             if (idk == CP_ID_KIND_UNQUALIFIED
5656                 || idk == CP_ID_KIND_TEMPLATE_ID)
5657               {
5658                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5659                   {
5660                     if (!VEC_empty (tree, args))
5661                       {
5662                         koenig_p = true;
5663                         if (!any_type_dependent_arguments_p (args))
5664                           postfix_expression
5665                             = perform_koenig_lookup (postfix_expression, args,
5666                                                      /*include_std=*/false,
5667                                                      tf_warning_or_error);
5668                       }
5669                     else
5670                       postfix_expression
5671                         = unqualified_fn_lookup_error (postfix_expression);
5672                   }
5673                 /* We do not perform argument-dependent lookup if
5674                    normal lookup finds a non-function, in accordance
5675                    with the expected resolution of DR 218.  */
5676                 else if (!VEC_empty (tree, args)
5677                          && is_overloaded_fn (postfix_expression))
5678                   {
5679                     tree fn = get_first_fn (postfix_expression);
5680                     fn = STRIP_TEMPLATE (fn);
5681
5682                     /* Do not do argument dependent lookup if regular
5683                        lookup finds a member function or a block-scope
5684                        function declaration.  [basic.lookup.argdep]/3  */
5685                     if (!DECL_FUNCTION_MEMBER_P (fn)
5686                         && !DECL_LOCAL_FUNCTION_P (fn))
5687                       {
5688                         koenig_p = true;
5689                         if (!any_type_dependent_arguments_p (args))
5690                           postfix_expression
5691                             = perform_koenig_lookup (postfix_expression, args,
5692                                                      /*include_std=*/false,
5693                                                      tf_warning_or_error);
5694                       }
5695                   }
5696               }
5697
5698             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5699               {
5700                 tree instance = TREE_OPERAND (postfix_expression, 0);
5701                 tree fn = TREE_OPERAND (postfix_expression, 1);
5702
5703                 if (processing_template_decl
5704                     && (type_dependent_expression_p (instance)
5705                         || (!BASELINK_P (fn)
5706                             && TREE_CODE (fn) != FIELD_DECL)
5707                         || type_dependent_expression_p (fn)
5708                         || any_type_dependent_arguments_p (args)))
5709                   {
5710                     postfix_expression
5711                       = build_nt_call_vec (postfix_expression, args);
5712                     release_tree_vector (args);
5713                     break;
5714                   }
5715
5716                 if (BASELINK_P (fn))
5717                   {
5718                   postfix_expression
5719                     = (build_new_method_call
5720                        (instance, fn, &args, NULL_TREE,
5721                         (idk == CP_ID_KIND_QUALIFIED
5722                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5723                          : LOOKUP_NORMAL),
5724                         /*fn_p=*/NULL,
5725                         tf_warning_or_error));
5726                   }
5727                 else
5728                   postfix_expression
5729                     = finish_call_expr (postfix_expression, &args,
5730                                         /*disallow_virtual=*/false,
5731                                         /*koenig_p=*/false,
5732                                         tf_warning_or_error);
5733               }
5734             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5735                      || TREE_CODE (postfix_expression) == MEMBER_REF
5736                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5737               postfix_expression = (build_offset_ref_call_from_tree
5738                                     (postfix_expression, &args));
5739             else if (idk == CP_ID_KIND_QUALIFIED)
5740               /* A call to a static class member, or a namespace-scope
5741                  function.  */
5742               postfix_expression
5743                 = finish_call_expr (postfix_expression, &args,
5744                                     /*disallow_virtual=*/true,
5745                                     koenig_p,
5746                                     tf_warning_or_error);
5747             else
5748               /* All other function calls.  */
5749               postfix_expression
5750                 = finish_call_expr (postfix_expression, &args,
5751                                     /*disallow_virtual=*/false,
5752                                     koenig_p,
5753                                     tf_warning_or_error);
5754
5755             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5756             idk = CP_ID_KIND_NONE;
5757
5758             release_tree_vector (args);
5759           }
5760           break;
5761
5762         case CPP_DOT:
5763         case CPP_DEREF:
5764           /* postfix-expression . template [opt] id-expression
5765              postfix-expression . pseudo-destructor-name
5766              postfix-expression -> template [opt] id-expression
5767              postfix-expression -> pseudo-destructor-name */
5768
5769           /* Consume the `.' or `->' operator.  */
5770           cp_lexer_consume_token (parser->lexer);
5771
5772           postfix_expression
5773             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5774                                                       postfix_expression,
5775                                                       false, &idk,
5776                                                       token->location);
5777
5778           is_member_access = true;
5779           break;
5780
5781         case CPP_PLUS_PLUS:
5782           /* postfix-expression ++  */
5783           /* Consume the `++' token.  */
5784           cp_lexer_consume_token (parser->lexer);
5785           /* Generate a representation for the complete expression.  */
5786           postfix_expression
5787             = finish_increment_expr (postfix_expression,
5788                                      POSTINCREMENT_EXPR);
5789           /* Increments may not appear in constant-expressions.  */
5790           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5791             postfix_expression = error_mark_node;
5792           idk = CP_ID_KIND_NONE;
5793           is_member_access = false;
5794           break;
5795
5796         case CPP_MINUS_MINUS:
5797           /* postfix-expression -- */
5798           /* Consume the `--' token.  */
5799           cp_lexer_consume_token (parser->lexer);
5800           /* Generate a representation for the complete expression.  */
5801           postfix_expression
5802             = finish_increment_expr (postfix_expression,
5803                                      POSTDECREMENT_EXPR);
5804           /* Decrements may not appear in constant-expressions.  */
5805           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5806             postfix_expression = error_mark_node;
5807           idk = CP_ID_KIND_NONE;
5808           is_member_access = false;
5809           break;
5810
5811         default:
5812           if (pidk_return != NULL)
5813             * pidk_return = idk;
5814           if (member_access_only_p)
5815             return is_member_access? postfix_expression : error_mark_node;
5816           else
5817             return postfix_expression;
5818         }
5819     }
5820
5821   /* We should never get here.  */
5822   gcc_unreachable ();
5823   return error_mark_node;
5824 }
5825
5826 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5827    by cp_parser_builtin_offsetof.  We're looking for
5828
5829      postfix-expression [ expression ]
5830
5831    FOR_OFFSETOF is set if we're being called in that context, which
5832    changes how we deal with integer constant expressions.  */
5833
5834 static tree
5835 cp_parser_postfix_open_square_expression (cp_parser *parser,
5836                                           tree postfix_expression,
5837                                           bool for_offsetof)
5838 {
5839   tree index;
5840
5841   /* Consume the `[' token.  */
5842   cp_lexer_consume_token (parser->lexer);
5843
5844   /* Parse the index expression.  */
5845   /* ??? For offsetof, there is a question of what to allow here.  If
5846      offsetof is not being used in an integral constant expression context,
5847      then we *could* get the right answer by computing the value at runtime.
5848      If we are in an integral constant expression context, then we might
5849      could accept any constant expression; hard to say without analysis.
5850      Rather than open the barn door too wide right away, allow only integer
5851      constant expressions here.  */
5852   if (for_offsetof)
5853     index = cp_parser_constant_expression (parser, false, NULL);
5854   else
5855     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5856
5857   /* Look for the closing `]'.  */
5858   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5859
5860   /* Build the ARRAY_REF.  */
5861   postfix_expression = grok_array_decl (postfix_expression, index);
5862
5863   /* When not doing offsetof, array references are not permitted in
5864      constant-expressions.  */
5865   if (!for_offsetof
5866       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5867     postfix_expression = error_mark_node;
5868
5869   return postfix_expression;
5870 }
5871
5872 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5873    by cp_parser_builtin_offsetof.  We're looking for
5874
5875      postfix-expression . template [opt] id-expression
5876      postfix-expression . pseudo-destructor-name
5877      postfix-expression -> template [opt] id-expression
5878      postfix-expression -> pseudo-destructor-name
5879
5880    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5881    limits what of the above we'll actually accept, but nevermind.
5882    TOKEN_TYPE is the "." or "->" token, which will already have been
5883    removed from the stream.  */
5884
5885 static tree
5886 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5887                                         enum cpp_ttype token_type,
5888                                         tree postfix_expression,
5889                                         bool for_offsetof, cp_id_kind *idk,
5890                                         location_t location)
5891 {
5892   tree name;
5893   bool dependent_p;
5894   bool pseudo_destructor_p;
5895   tree scope = NULL_TREE;
5896
5897   /* If this is a `->' operator, dereference the pointer.  */
5898   if (token_type == CPP_DEREF)
5899     postfix_expression = build_x_arrow (postfix_expression);
5900   /* Check to see whether or not the expression is type-dependent.  */
5901   dependent_p = type_dependent_expression_p (postfix_expression);
5902   /* The identifier following the `->' or `.' is not qualified.  */
5903   parser->scope = NULL_TREE;
5904   parser->qualifying_scope = NULL_TREE;
5905   parser->object_scope = NULL_TREE;
5906   *idk = CP_ID_KIND_NONE;
5907
5908   /* Enter the scope corresponding to the type of the object
5909      given by the POSTFIX_EXPRESSION.  */
5910   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5911     {
5912       scope = TREE_TYPE (postfix_expression);
5913       /* According to the standard, no expression should ever have
5914          reference type.  Unfortunately, we do not currently match
5915          the standard in this respect in that our internal representation
5916          of an expression may have reference type even when the standard
5917          says it does not.  Therefore, we have to manually obtain the
5918          underlying type here.  */
5919       scope = non_reference (scope);
5920       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5921       if (scope == unknown_type_node)
5922         {
5923           error_at (location, "%qE does not have class type",
5924                     postfix_expression);
5925           scope = NULL_TREE;
5926         }
5927       /* Unlike the object expression in other contexts, *this is not
5928          required to be of complete type for purposes of class member
5929          access (5.2.5) outside the member function body.  */
5930       else if (scope != current_class_ref
5931                && !(processing_template_decl && scope == current_class_type))
5932         scope = complete_type_or_else (scope, NULL_TREE);
5933       /* Let the name lookup machinery know that we are processing a
5934          class member access expression.  */
5935       parser->context->object_type = scope;
5936       /* If something went wrong, we want to be able to discern that case,
5937          as opposed to the case where there was no SCOPE due to the type
5938          of expression being dependent.  */
5939       if (!scope)
5940         scope = error_mark_node;
5941       /* If the SCOPE was erroneous, make the various semantic analysis
5942          functions exit quickly -- and without issuing additional error
5943          messages.  */
5944       if (scope == error_mark_node)
5945         postfix_expression = error_mark_node;
5946     }
5947
5948   /* Assume this expression is not a pseudo-destructor access.  */
5949   pseudo_destructor_p = false;
5950
5951   /* If the SCOPE is a scalar type, then, if this is a valid program,
5952      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5953      is type dependent, it can be pseudo-destructor-name or something else.
5954      Try to parse it as pseudo-destructor-name first.  */
5955   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5956     {
5957       tree s;
5958       tree type;
5959
5960       cp_parser_parse_tentatively (parser);
5961       /* Parse the pseudo-destructor-name.  */
5962       s = NULL_TREE;
5963       cp_parser_pseudo_destructor_name (parser, &s, &type);
5964       if (dependent_p
5965           && (cp_parser_error_occurred (parser)
5966               || TREE_CODE (type) != TYPE_DECL
5967               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5968         cp_parser_abort_tentative_parse (parser);
5969       else if (cp_parser_parse_definitely (parser))
5970         {
5971           pseudo_destructor_p = true;
5972           postfix_expression
5973             = finish_pseudo_destructor_expr (postfix_expression,
5974                                              s, TREE_TYPE (type));
5975         }
5976     }
5977
5978   if (!pseudo_destructor_p)
5979     {
5980       /* If the SCOPE is not a scalar type, we are looking at an
5981          ordinary class member access expression, rather than a
5982          pseudo-destructor-name.  */
5983       bool template_p;
5984       cp_token *token = cp_lexer_peek_token (parser->lexer);
5985       /* Parse the id-expression.  */
5986       name = (cp_parser_id_expression
5987               (parser,
5988                cp_parser_optional_template_keyword (parser),
5989                /*check_dependency_p=*/true,
5990                &template_p,
5991                /*declarator_p=*/false,
5992                /*optional_p=*/false));
5993       /* In general, build a SCOPE_REF if the member name is qualified.
5994          However, if the name was not dependent and has already been
5995          resolved; there is no need to build the SCOPE_REF.  For example;
5996
5997              struct X { void f(); };
5998              template <typename T> void f(T* t) { t->X::f(); }
5999
6000          Even though "t" is dependent, "X::f" is not and has been resolved
6001          to a BASELINK; there is no need to include scope information.  */
6002
6003       /* But we do need to remember that there was an explicit scope for
6004          virtual function calls.  */
6005       if (parser->scope)
6006         *idk = CP_ID_KIND_QUALIFIED;
6007
6008       /* If the name is a template-id that names a type, we will get a
6009          TYPE_DECL here.  That is invalid code.  */
6010       if (TREE_CODE (name) == TYPE_DECL)
6011         {
6012           error_at (token->location, "invalid use of %qD", name);
6013           postfix_expression = error_mark_node;
6014         }
6015       else
6016         {
6017           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6018             {
6019               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6020                 {
6021                   error_at (token->location, "%<%D::%D%> is not a class member",
6022                             parser->scope, name);
6023                   postfix_expression = error_mark_node;
6024                 }
6025               else
6026                 name = build_qualified_name (/*type=*/NULL_TREE,
6027                                              parser->scope,
6028                                              name,
6029                                              template_p);
6030               parser->scope = NULL_TREE;
6031               parser->qualifying_scope = NULL_TREE;
6032               parser->object_scope = NULL_TREE;
6033             }
6034           if (scope && name && BASELINK_P (name))
6035             adjust_result_of_qualified_name_lookup
6036               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6037           postfix_expression
6038             = finish_class_member_access_expr (postfix_expression, name,
6039                                                template_p, 
6040                                                tf_warning_or_error);
6041         }
6042     }
6043
6044   /* We no longer need to look up names in the scope of the object on
6045      the left-hand side of the `.' or `->' operator.  */
6046   parser->context->object_type = NULL_TREE;
6047
6048   /* Outside of offsetof, these operators may not appear in
6049      constant-expressions.  */
6050   if (!for_offsetof
6051       && (cp_parser_non_integral_constant_expression
6052           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6053     postfix_expression = error_mark_node;
6054
6055   return postfix_expression;
6056 }
6057
6058 /* Parse a parenthesized expression-list.
6059
6060    expression-list:
6061      assignment-expression
6062      expression-list, assignment-expression
6063
6064    attribute-list:
6065      expression-list
6066      identifier
6067      identifier, expression-list
6068
6069    CAST_P is true if this expression is the target of a cast.
6070
6071    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6072    argument pack.
6073
6074    Returns a vector of trees.  Each element is a representation of an
6075    assignment-expression.  NULL is returned if the ( and or ) are
6076    missing.  An empty, but allocated, vector is returned on no
6077    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6078    if we are parsing an attribute list for an attribute that wants a
6079    plain identifier argument, normal_attr for an attribute that wants
6080    an expression, or non_attr if we aren't parsing an attribute list.  If
6081    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6082    not all of the expressions in the list were constant.  */
6083
6084 static VEC(tree,gc) *
6085 cp_parser_parenthesized_expression_list (cp_parser* parser,
6086                                          int is_attribute_list,
6087                                          bool cast_p,
6088                                          bool allow_expansion_p,
6089                                          bool *non_constant_p)
6090 {
6091   VEC(tree,gc) *expression_list;
6092   bool fold_expr_p = is_attribute_list != non_attr;
6093   tree identifier = NULL_TREE;
6094   bool saved_greater_than_is_operator_p;
6095
6096   /* Assume all the expressions will be constant.  */
6097   if (non_constant_p)
6098     *non_constant_p = false;
6099
6100   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6101     return NULL;
6102
6103   expression_list = make_tree_vector ();
6104
6105   /* Within a parenthesized expression, a `>' token is always
6106      the greater-than operator.  */
6107   saved_greater_than_is_operator_p
6108     = parser->greater_than_is_operator_p;
6109   parser->greater_than_is_operator_p = true;
6110
6111   /* Consume expressions until there are no more.  */
6112   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6113     while (true)
6114       {
6115         tree expr;
6116
6117         /* At the beginning of attribute lists, check to see if the
6118            next token is an identifier.  */
6119         if (is_attribute_list == id_attr
6120             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6121           {
6122             cp_token *token;
6123
6124             /* Consume the identifier.  */
6125             token = cp_lexer_consume_token (parser->lexer);
6126             /* Save the identifier.  */
6127             identifier = token->u.value;
6128           }
6129         else
6130           {
6131             bool expr_non_constant_p;
6132
6133             /* Parse the next assignment-expression.  */
6134             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6135               {
6136                 /* A braced-init-list.  */
6137                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6138                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6139                 if (non_constant_p && expr_non_constant_p)
6140                   *non_constant_p = true;
6141               }
6142             else if (non_constant_p)
6143               {
6144                 expr = (cp_parser_constant_expression
6145                         (parser, /*allow_non_constant_p=*/true,
6146                          &expr_non_constant_p));
6147                 if (expr_non_constant_p)
6148                   *non_constant_p = true;
6149               }
6150             else
6151               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6152
6153             if (fold_expr_p)
6154               expr = fold_non_dependent_expr (expr);
6155
6156             /* If we have an ellipsis, then this is an expression
6157                expansion.  */
6158             if (allow_expansion_p
6159                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6160               {
6161                 /* Consume the `...'.  */
6162                 cp_lexer_consume_token (parser->lexer);
6163
6164                 /* Build the argument pack.  */
6165                 expr = make_pack_expansion (expr);
6166               }
6167
6168              /* Add it to the list.  We add error_mark_node
6169                 expressions to the list, so that we can still tell if
6170                 the correct form for a parenthesized expression-list
6171                 is found. That gives better errors.  */
6172             VEC_safe_push (tree, gc, expression_list, expr);
6173
6174             if (expr == error_mark_node)
6175               goto skip_comma;
6176           }
6177
6178         /* After the first item, attribute lists look the same as
6179            expression lists.  */
6180         is_attribute_list = non_attr;
6181
6182       get_comma:;
6183         /* If the next token isn't a `,', then we are done.  */
6184         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6185           break;
6186
6187         /* Otherwise, consume the `,' and keep going.  */
6188         cp_lexer_consume_token (parser->lexer);
6189       }
6190
6191   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6192     {
6193       int ending;
6194
6195     skip_comma:;
6196       /* We try and resync to an unnested comma, as that will give the
6197          user better diagnostics.  */
6198       ending = cp_parser_skip_to_closing_parenthesis (parser,
6199                                                       /*recovering=*/true,
6200                                                       /*or_comma=*/true,
6201                                                       /*consume_paren=*/true);
6202       if (ending < 0)
6203         goto get_comma;
6204       if (!ending)
6205         {
6206           parser->greater_than_is_operator_p
6207             = saved_greater_than_is_operator_p;
6208           return NULL;
6209         }
6210     }
6211
6212   parser->greater_than_is_operator_p
6213     = saved_greater_than_is_operator_p;
6214
6215   if (identifier)
6216     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6217
6218   return expression_list;
6219 }
6220
6221 /* Parse a pseudo-destructor-name.
6222
6223    pseudo-destructor-name:
6224      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6225      :: [opt] nested-name-specifier template template-id :: ~ type-name
6226      :: [opt] nested-name-specifier [opt] ~ type-name
6227
6228    If either of the first two productions is used, sets *SCOPE to the
6229    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6230    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6231    or ERROR_MARK_NODE if the parse fails.  */
6232
6233 static void
6234 cp_parser_pseudo_destructor_name (cp_parser* parser,
6235                                   tree* scope,
6236                                   tree* type)
6237 {
6238   bool nested_name_specifier_p;
6239
6240   /* Assume that things will not work out.  */
6241   *type = error_mark_node;
6242
6243   /* Look for the optional `::' operator.  */
6244   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6245   /* Look for the optional nested-name-specifier.  */
6246   nested_name_specifier_p
6247     = (cp_parser_nested_name_specifier_opt (parser,
6248                                             /*typename_keyword_p=*/false,
6249                                             /*check_dependency_p=*/true,
6250                                             /*type_p=*/false,
6251                                             /*is_declaration=*/false)
6252        != NULL_TREE);
6253   /* Now, if we saw a nested-name-specifier, we might be doing the
6254      second production.  */
6255   if (nested_name_specifier_p
6256       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6257     {
6258       /* Consume the `template' keyword.  */
6259       cp_lexer_consume_token (parser->lexer);
6260       /* Parse the template-id.  */
6261       cp_parser_template_id (parser,
6262                              /*template_keyword_p=*/true,
6263                              /*check_dependency_p=*/false,
6264                              /*is_declaration=*/true);
6265       /* Look for the `::' token.  */
6266       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6267     }
6268   /* If the next token is not a `~', then there might be some
6269      additional qualification.  */
6270   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6271     {
6272       /* At this point, we're looking for "type-name :: ~".  The type-name
6273          must not be a class-name, since this is a pseudo-destructor.  So,
6274          it must be either an enum-name, or a typedef-name -- both of which
6275          are just identifiers.  So, we peek ahead to check that the "::"
6276          and "~" tokens are present; if they are not, then we can avoid
6277          calling type_name.  */
6278       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6279           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6280           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6281         {
6282           cp_parser_error (parser, "non-scalar type");
6283           return;
6284         }
6285
6286       /* Look for the type-name.  */
6287       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6288       if (*scope == error_mark_node)
6289         return;
6290
6291       /* Look for the `::' token.  */
6292       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6293     }
6294   else
6295     *scope = NULL_TREE;
6296
6297   /* Look for the `~'.  */
6298   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6299
6300   /* Once we see the ~, this has to be a pseudo-destructor.  */
6301   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6302     cp_parser_commit_to_tentative_parse (parser);
6303
6304   /* Look for the type-name again.  We are not responsible for
6305      checking that it matches the first type-name.  */
6306   *type = cp_parser_nonclass_name (parser);
6307 }
6308
6309 /* Parse a unary-expression.
6310
6311    unary-expression:
6312      postfix-expression
6313      ++ cast-expression
6314      -- cast-expression
6315      unary-operator cast-expression
6316      sizeof unary-expression
6317      sizeof ( type-id )
6318      alignof ( type-id )  [C++0x]
6319      new-expression
6320      delete-expression
6321
6322    GNU Extensions:
6323
6324    unary-expression:
6325      __extension__ cast-expression
6326      __alignof__ unary-expression
6327      __alignof__ ( type-id )
6328      alignof unary-expression  [C++0x]
6329      __real__ cast-expression
6330      __imag__ cast-expression
6331      && identifier
6332
6333    ADDRESS_P is true iff the unary-expression is appearing as the
6334    operand of the `&' operator.   CAST_P is true if this expression is
6335    the target of a cast.
6336
6337    Returns a representation of the expression.  */
6338
6339 static tree
6340 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6341                             cp_id_kind * pidk)
6342 {
6343   cp_token *token;
6344   enum tree_code unary_operator;
6345
6346   /* Peek at the next token.  */
6347   token = cp_lexer_peek_token (parser->lexer);
6348   /* Some keywords give away the kind of expression.  */
6349   if (token->type == CPP_KEYWORD)
6350     {
6351       enum rid keyword = token->keyword;
6352
6353       switch (keyword)
6354         {
6355         case RID_ALIGNOF:
6356         case RID_SIZEOF:
6357           {
6358             tree operand;
6359             enum tree_code op;
6360
6361             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6362             /* Consume the token.  */
6363             cp_lexer_consume_token (parser->lexer);
6364             /* Parse the operand.  */
6365             operand = cp_parser_sizeof_operand (parser, keyword);
6366
6367             if (TYPE_P (operand))
6368               return cxx_sizeof_or_alignof_type (operand, op, true);
6369             else
6370               {
6371                 /* ISO C++ defines alignof only with types, not with
6372                    expressions. So pedwarn if alignof is used with a non-
6373                    type expression. However, __alignof__ is ok.  */
6374                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6375                   pedwarn (token->location, OPT_pedantic,
6376                            "ISO C++ does not allow %<alignof%> "
6377                            "with a non-type");
6378
6379                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6380               }
6381           }
6382
6383         case RID_NEW:
6384           return cp_parser_new_expression (parser);
6385
6386         case RID_DELETE:
6387           return cp_parser_delete_expression (parser);
6388
6389         case RID_EXTENSION:
6390           {
6391             /* The saved value of the PEDANTIC flag.  */
6392             int saved_pedantic;
6393             tree expr;
6394
6395             /* Save away the PEDANTIC flag.  */
6396             cp_parser_extension_opt (parser, &saved_pedantic);
6397             /* Parse the cast-expression.  */
6398             expr = cp_parser_simple_cast_expression (parser);
6399             /* Restore the PEDANTIC flag.  */
6400             pedantic = saved_pedantic;
6401
6402             return expr;
6403           }
6404
6405         case RID_REALPART:
6406         case RID_IMAGPART:
6407           {
6408             tree expression;
6409
6410             /* Consume the `__real__' or `__imag__' token.  */
6411             cp_lexer_consume_token (parser->lexer);
6412             /* Parse the cast-expression.  */
6413             expression = cp_parser_simple_cast_expression (parser);
6414             /* Create the complete representation.  */
6415             return build_x_unary_op ((keyword == RID_REALPART
6416                                       ? REALPART_EXPR : IMAGPART_EXPR),
6417                                      expression,
6418                                      tf_warning_or_error);
6419           }
6420           break;
6421
6422         case RID_TRANSACTION_ATOMIC:
6423         case RID_TRANSACTION_RELAXED:
6424           return cp_parser_transaction_expression (parser, keyword);
6425
6426         case RID_NOEXCEPT:
6427           {
6428             tree expr;
6429             const char *saved_message;
6430             bool saved_integral_constant_expression_p;
6431             bool saved_non_integral_constant_expression_p;
6432             bool saved_greater_than_is_operator_p;
6433
6434             cp_lexer_consume_token (parser->lexer);
6435             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6436
6437             saved_message = parser->type_definition_forbidden_message;
6438             parser->type_definition_forbidden_message
6439               = G_("types may not be defined in %<noexcept%> expressions");
6440
6441             saved_integral_constant_expression_p
6442               = parser->integral_constant_expression_p;
6443             saved_non_integral_constant_expression_p
6444               = parser->non_integral_constant_expression_p;
6445             parser->integral_constant_expression_p = false;
6446
6447             saved_greater_than_is_operator_p
6448               = parser->greater_than_is_operator_p;
6449             parser->greater_than_is_operator_p = true;
6450
6451             ++cp_unevaluated_operand;
6452             ++c_inhibit_evaluation_warnings;
6453             expr = cp_parser_expression (parser, false, NULL);
6454             --c_inhibit_evaluation_warnings;
6455             --cp_unevaluated_operand;
6456
6457             parser->greater_than_is_operator_p
6458               = saved_greater_than_is_operator_p;
6459
6460             parser->integral_constant_expression_p
6461               = saved_integral_constant_expression_p;
6462             parser->non_integral_constant_expression_p
6463               = saved_non_integral_constant_expression_p;
6464
6465             parser->type_definition_forbidden_message = saved_message;
6466
6467             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6468             return finish_noexcept_expr (expr, tf_warning_or_error);
6469           }
6470
6471         default:
6472           break;
6473         }
6474     }
6475
6476   /* Look for the `:: new' and `:: delete', which also signal the
6477      beginning of a new-expression, or delete-expression,
6478      respectively.  If the next token is `::', then it might be one of
6479      these.  */
6480   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6481     {
6482       enum rid keyword;
6483
6484       /* See if the token after the `::' is one of the keywords in
6485          which we're interested.  */
6486       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6487       /* If it's `new', we have a new-expression.  */
6488       if (keyword == RID_NEW)
6489         return cp_parser_new_expression (parser);
6490       /* Similarly, for `delete'.  */
6491       else if (keyword == RID_DELETE)
6492         return cp_parser_delete_expression (parser);
6493     }
6494
6495   /* Look for a unary operator.  */
6496   unary_operator = cp_parser_unary_operator (token);
6497   /* The `++' and `--' operators can be handled similarly, even though
6498      they are not technically unary-operators in the grammar.  */
6499   if (unary_operator == ERROR_MARK)
6500     {
6501       if (token->type == CPP_PLUS_PLUS)
6502         unary_operator = PREINCREMENT_EXPR;
6503       else if (token->type == CPP_MINUS_MINUS)
6504         unary_operator = PREDECREMENT_EXPR;
6505       /* Handle the GNU address-of-label extension.  */
6506       else if (cp_parser_allow_gnu_extensions_p (parser)
6507                && token->type == CPP_AND_AND)
6508         {
6509           tree identifier;
6510           tree expression;
6511           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6512
6513           /* Consume the '&&' token.  */
6514           cp_lexer_consume_token (parser->lexer);
6515           /* Look for the identifier.  */
6516           identifier = cp_parser_identifier (parser);
6517           /* Create an expression representing the address.  */
6518           expression = finish_label_address_expr (identifier, loc);
6519           if (cp_parser_non_integral_constant_expression (parser,
6520                                                           NIC_ADDR_LABEL))
6521             expression = error_mark_node;
6522           return expression;
6523         }
6524     }
6525   if (unary_operator != ERROR_MARK)
6526     {
6527       tree cast_expression;
6528       tree expression = error_mark_node;
6529       non_integral_constant non_constant_p = NIC_NONE;
6530
6531       /* Consume the operator token.  */
6532       token = cp_lexer_consume_token (parser->lexer);
6533       /* Parse the cast-expression.  */
6534       cast_expression
6535         = cp_parser_cast_expression (parser,
6536                                      unary_operator == ADDR_EXPR,
6537                                      /*cast_p=*/false, pidk);
6538       /* Now, build an appropriate representation.  */
6539       switch (unary_operator)
6540         {
6541         case INDIRECT_REF:
6542           non_constant_p = NIC_STAR;
6543           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6544                                              tf_warning_or_error);
6545           break;
6546
6547         case ADDR_EXPR:
6548            non_constant_p = NIC_ADDR;
6549           /* Fall through.  */
6550         case BIT_NOT_EXPR:
6551           expression = build_x_unary_op (unary_operator, cast_expression,
6552                                          tf_warning_or_error);
6553           break;
6554
6555         case PREINCREMENT_EXPR:
6556         case PREDECREMENT_EXPR:
6557           non_constant_p = unary_operator == PREINCREMENT_EXPR
6558                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6559           /* Fall through.  */
6560         case UNARY_PLUS_EXPR:
6561         case NEGATE_EXPR:
6562         case TRUTH_NOT_EXPR:
6563           expression = finish_unary_op_expr (unary_operator, cast_expression);
6564           break;
6565
6566         default:
6567           gcc_unreachable ();
6568         }
6569
6570       if (non_constant_p != NIC_NONE
6571           && cp_parser_non_integral_constant_expression (parser,
6572                                                          non_constant_p))
6573         expression = error_mark_node;
6574
6575       return expression;
6576     }
6577
6578   return cp_parser_postfix_expression (parser, address_p, cast_p,
6579                                        /*member_access_only_p=*/false,
6580                                        pidk);
6581 }
6582
6583 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6584    unary-operator, the corresponding tree code is returned.  */
6585
6586 static enum tree_code
6587 cp_parser_unary_operator (cp_token* token)
6588 {
6589   switch (token->type)
6590     {
6591     case CPP_MULT:
6592       return INDIRECT_REF;
6593
6594     case CPP_AND:
6595       return ADDR_EXPR;
6596
6597     case CPP_PLUS:
6598       return UNARY_PLUS_EXPR;
6599
6600     case CPP_MINUS:
6601       return NEGATE_EXPR;
6602
6603     case CPP_NOT:
6604       return TRUTH_NOT_EXPR;
6605
6606     case CPP_COMPL:
6607       return BIT_NOT_EXPR;
6608
6609     default:
6610       return ERROR_MARK;
6611     }
6612 }
6613
6614 /* Parse a new-expression.
6615
6616    new-expression:
6617      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6618      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6619
6620    Returns a representation of the expression.  */
6621
6622 static tree
6623 cp_parser_new_expression (cp_parser* parser)
6624 {
6625   bool global_scope_p;
6626   VEC(tree,gc) *placement;
6627   tree type;
6628   VEC(tree,gc) *initializer;
6629   tree nelts;
6630   tree ret;
6631
6632   /* Look for the optional `::' operator.  */
6633   global_scope_p
6634     = (cp_parser_global_scope_opt (parser,
6635                                    /*current_scope_valid_p=*/false)
6636        != NULL_TREE);
6637   /* Look for the `new' operator.  */
6638   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6639   /* There's no easy way to tell a new-placement from the
6640      `( type-id )' construct.  */
6641   cp_parser_parse_tentatively (parser);
6642   /* Look for a new-placement.  */
6643   placement = cp_parser_new_placement (parser);
6644   /* If that didn't work out, there's no new-placement.  */
6645   if (!cp_parser_parse_definitely (parser))
6646     {
6647       if (placement != NULL)
6648         release_tree_vector (placement);
6649       placement = NULL;
6650     }
6651
6652   /* If the next token is a `(', then we have a parenthesized
6653      type-id.  */
6654   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6655     {
6656       cp_token *token;
6657       /* Consume the `('.  */
6658       cp_lexer_consume_token (parser->lexer);
6659       /* Parse the type-id.  */
6660       type = cp_parser_type_id (parser);
6661       /* Look for the closing `)'.  */
6662       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6663       token = cp_lexer_peek_token (parser->lexer);
6664       /* There should not be a direct-new-declarator in this production,
6665          but GCC used to allowed this, so we check and emit a sensible error
6666          message for this case.  */
6667       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6668         {
6669           error_at (token->location,
6670                     "array bound forbidden after parenthesized type-id");
6671           inform (token->location, 
6672                   "try removing the parentheses around the type-id");
6673           cp_parser_direct_new_declarator (parser);
6674         }
6675       nelts = NULL_TREE;
6676     }
6677   /* Otherwise, there must be a new-type-id.  */
6678   else
6679     type = cp_parser_new_type_id (parser, &nelts);
6680
6681   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6682   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6683       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6684     initializer = cp_parser_new_initializer (parser);
6685   else
6686     initializer = NULL;
6687
6688   /* A new-expression may not appear in an integral constant
6689      expression.  */
6690   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6691     ret = error_mark_node;
6692   else
6693     {
6694       /* Create a representation of the new-expression.  */
6695       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6696                        tf_warning_or_error);
6697     }
6698
6699   if (placement != NULL)
6700     release_tree_vector (placement);
6701   if (initializer != NULL)
6702     release_tree_vector (initializer);
6703
6704   return ret;
6705 }
6706
6707 /* Parse a new-placement.
6708
6709    new-placement:
6710      ( expression-list )
6711
6712    Returns the same representation as for an expression-list.  */
6713
6714 static VEC(tree,gc) *
6715 cp_parser_new_placement (cp_parser* parser)
6716 {
6717   VEC(tree,gc) *expression_list;
6718
6719   /* Parse the expression-list.  */
6720   expression_list = (cp_parser_parenthesized_expression_list
6721                      (parser, non_attr, /*cast_p=*/false,
6722                       /*allow_expansion_p=*/true,
6723                       /*non_constant_p=*/NULL));
6724
6725   return expression_list;
6726 }
6727
6728 /* Parse a new-type-id.
6729
6730    new-type-id:
6731      type-specifier-seq new-declarator [opt]
6732
6733    Returns the TYPE allocated.  If the new-type-id indicates an array
6734    type, *NELTS is set to the number of elements in the last array
6735    bound; the TYPE will not include the last array bound.  */
6736
6737 static tree
6738 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6739 {
6740   cp_decl_specifier_seq type_specifier_seq;
6741   cp_declarator *new_declarator;
6742   cp_declarator *declarator;
6743   cp_declarator *outer_declarator;
6744   const char *saved_message;
6745   tree type;
6746
6747   /* The type-specifier sequence must not contain type definitions.
6748      (It cannot contain declarations of new types either, but if they
6749      are not definitions we will catch that because they are not
6750      complete.)  */
6751   saved_message = parser->type_definition_forbidden_message;
6752   parser->type_definition_forbidden_message
6753     = G_("types may not be defined in a new-type-id");
6754   /* Parse the type-specifier-seq.  */
6755   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6756                                 /*is_trailing_return=*/false,
6757                                 &type_specifier_seq);
6758   /* Restore the old message.  */
6759   parser->type_definition_forbidden_message = saved_message;
6760   /* Parse the new-declarator.  */
6761   new_declarator = cp_parser_new_declarator_opt (parser);
6762
6763   /* Determine the number of elements in the last array dimension, if
6764      any.  */
6765   *nelts = NULL_TREE;
6766   /* Skip down to the last array dimension.  */
6767   declarator = new_declarator;
6768   outer_declarator = NULL;
6769   while (declarator && (declarator->kind == cdk_pointer
6770                         || declarator->kind == cdk_ptrmem))
6771     {
6772       outer_declarator = declarator;
6773       declarator = declarator->declarator;
6774     }
6775   while (declarator
6776          && declarator->kind == cdk_array
6777          && declarator->declarator
6778          && declarator->declarator->kind == cdk_array)
6779     {
6780       outer_declarator = declarator;
6781       declarator = declarator->declarator;
6782     }
6783
6784   if (declarator && declarator->kind == cdk_array)
6785     {
6786       *nelts = declarator->u.array.bounds;
6787       if (*nelts == error_mark_node)
6788         *nelts = integer_one_node;
6789
6790       if (outer_declarator)
6791         outer_declarator->declarator = declarator->declarator;
6792       else
6793         new_declarator = NULL;
6794     }
6795
6796   type = groktypename (&type_specifier_seq, new_declarator, false);
6797   return type;
6798 }
6799
6800 /* Parse an (optional) new-declarator.
6801
6802    new-declarator:
6803      ptr-operator new-declarator [opt]
6804      direct-new-declarator
6805
6806    Returns the declarator.  */
6807
6808 static cp_declarator *
6809 cp_parser_new_declarator_opt (cp_parser* parser)
6810 {
6811   enum tree_code code;
6812   tree type;
6813   cp_cv_quals cv_quals;
6814
6815   /* We don't know if there's a ptr-operator next, or not.  */
6816   cp_parser_parse_tentatively (parser);
6817   /* Look for a ptr-operator.  */
6818   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6819   /* If that worked, look for more new-declarators.  */
6820   if (cp_parser_parse_definitely (parser))
6821     {
6822       cp_declarator *declarator;
6823
6824       /* Parse another optional declarator.  */
6825       declarator = cp_parser_new_declarator_opt (parser);
6826
6827       return cp_parser_make_indirect_declarator
6828         (code, type, cv_quals, declarator);
6829     }
6830
6831   /* If the next token is a `[', there is a direct-new-declarator.  */
6832   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6833     return cp_parser_direct_new_declarator (parser);
6834
6835   return NULL;
6836 }
6837
6838 /* Parse a direct-new-declarator.
6839
6840    direct-new-declarator:
6841      [ expression ]
6842      direct-new-declarator [constant-expression]
6843
6844    */
6845
6846 static cp_declarator *
6847 cp_parser_direct_new_declarator (cp_parser* parser)
6848 {
6849   cp_declarator *declarator = NULL;
6850
6851   while (true)
6852     {
6853       tree expression;
6854
6855       /* Look for the opening `['.  */
6856       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6857       /* The first expression is not required to be constant.  */
6858       if (!declarator)
6859         {
6860           cp_token *token = cp_lexer_peek_token (parser->lexer);
6861           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6862           /* The standard requires that the expression have integral
6863              type.  DR 74 adds enumeration types.  We believe that the
6864              real intent is that these expressions be handled like the
6865              expression in a `switch' condition, which also allows
6866              classes with a single conversion to integral or
6867              enumeration type.  */
6868           if (!processing_template_decl)
6869             {
6870               expression
6871                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6872                                               expression,
6873                                               /*complain=*/true);
6874               if (!expression)
6875                 {
6876                   error_at (token->location,
6877                             "expression in new-declarator must have integral "
6878                             "or enumeration type");
6879                   expression = error_mark_node;
6880                 }
6881             }
6882         }
6883       /* But all the other expressions must be.  */
6884       else
6885         expression
6886           = cp_parser_constant_expression (parser,
6887                                            /*allow_non_constant=*/false,
6888                                            NULL);
6889       /* Look for the closing `]'.  */
6890       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6891
6892       /* Add this bound to the declarator.  */
6893       declarator = make_array_declarator (declarator, expression);
6894
6895       /* If the next token is not a `[', then there are no more
6896          bounds.  */
6897       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6898         break;
6899     }
6900
6901   return declarator;
6902 }
6903
6904 /* Parse a new-initializer.
6905
6906    new-initializer:
6907      ( expression-list [opt] )
6908      braced-init-list
6909
6910    Returns a representation of the expression-list.  */
6911
6912 static VEC(tree,gc) *
6913 cp_parser_new_initializer (cp_parser* parser)
6914 {
6915   VEC(tree,gc) *expression_list;
6916
6917   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6918     {
6919       tree t;
6920       bool expr_non_constant_p;
6921       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6922       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6923       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6924       expression_list = make_tree_vector_single (t);
6925     }
6926   else
6927     expression_list = (cp_parser_parenthesized_expression_list
6928                        (parser, non_attr, /*cast_p=*/false,
6929                         /*allow_expansion_p=*/true,
6930                         /*non_constant_p=*/NULL));
6931
6932   return expression_list;
6933 }
6934
6935 /* Parse a delete-expression.
6936
6937    delete-expression:
6938      :: [opt] delete cast-expression
6939      :: [opt] delete [ ] cast-expression
6940
6941    Returns a representation of the expression.  */
6942
6943 static tree
6944 cp_parser_delete_expression (cp_parser* parser)
6945 {
6946   bool global_scope_p;
6947   bool array_p;
6948   tree expression;
6949
6950   /* Look for the optional `::' operator.  */
6951   global_scope_p
6952     = (cp_parser_global_scope_opt (parser,
6953                                    /*current_scope_valid_p=*/false)
6954        != NULL_TREE);
6955   /* Look for the `delete' keyword.  */
6956   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6957   /* See if the array syntax is in use.  */
6958   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6959     {
6960       /* Consume the `[' token.  */
6961       cp_lexer_consume_token (parser->lexer);
6962       /* Look for the `]' token.  */
6963       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6964       /* Remember that this is the `[]' construct.  */
6965       array_p = true;
6966     }
6967   else
6968     array_p = false;
6969
6970   /* Parse the cast-expression.  */
6971   expression = cp_parser_simple_cast_expression (parser);
6972
6973   /* A delete-expression may not appear in an integral constant
6974      expression.  */
6975   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6976     return error_mark_node;
6977
6978   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6979                         tf_warning_or_error);
6980 }
6981
6982 /* Returns true if TOKEN may start a cast-expression and false
6983    otherwise.  */
6984
6985 static bool
6986 cp_parser_token_starts_cast_expression (cp_token *token)
6987 {
6988   switch (token->type)
6989     {
6990     case CPP_COMMA:
6991     case CPP_SEMICOLON:
6992     case CPP_QUERY:
6993     case CPP_COLON:
6994     case CPP_CLOSE_SQUARE:
6995     case CPP_CLOSE_PAREN:
6996     case CPP_CLOSE_BRACE:
6997     case CPP_DOT:
6998     case CPP_DOT_STAR:
6999     case CPP_DEREF:
7000     case CPP_DEREF_STAR:
7001     case CPP_DIV:
7002     case CPP_MOD:
7003     case CPP_LSHIFT:
7004     case CPP_RSHIFT:
7005     case CPP_LESS:
7006     case CPP_GREATER:
7007     case CPP_LESS_EQ:
7008     case CPP_GREATER_EQ:
7009     case CPP_EQ_EQ:
7010     case CPP_NOT_EQ:
7011     case CPP_EQ:
7012     case CPP_MULT_EQ:
7013     case CPP_DIV_EQ:
7014     case CPP_MOD_EQ:
7015     case CPP_PLUS_EQ:
7016     case CPP_MINUS_EQ:
7017     case CPP_RSHIFT_EQ:
7018     case CPP_LSHIFT_EQ:
7019     case CPP_AND_EQ:
7020     case CPP_XOR_EQ:
7021     case CPP_OR_EQ:
7022     case CPP_XOR:
7023     case CPP_OR:
7024     case CPP_OR_OR:
7025     case CPP_EOF:
7026       return false;
7027
7028       /* '[' may start a primary-expression in obj-c++.  */
7029     case CPP_OPEN_SQUARE:
7030       return c_dialect_objc ();
7031
7032     default:
7033       return true;
7034     }
7035 }
7036
7037 /* Parse a cast-expression.
7038
7039    cast-expression:
7040      unary-expression
7041      ( type-id ) cast-expression
7042
7043    ADDRESS_P is true iff the unary-expression is appearing as the
7044    operand of the `&' operator.   CAST_P is true if this expression is
7045    the target of a cast.
7046
7047    Returns a representation of the expression.  */
7048
7049 static tree
7050 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7051                            cp_id_kind * pidk)
7052 {
7053   /* If it's a `(', then we might be looking at a cast.  */
7054   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7055     {
7056       tree type = NULL_TREE;
7057       tree expr = NULL_TREE;
7058       bool compound_literal_p;
7059       const char *saved_message;
7060
7061       /* There's no way to know yet whether or not this is a cast.
7062          For example, `(int (3))' is a unary-expression, while `(int)
7063          3' is a cast.  So, we resort to parsing tentatively.  */
7064       cp_parser_parse_tentatively (parser);
7065       /* Types may not be defined in a cast.  */
7066       saved_message = parser->type_definition_forbidden_message;
7067       parser->type_definition_forbidden_message
7068         = G_("types may not be defined in casts");
7069       /* Consume the `('.  */
7070       cp_lexer_consume_token (parser->lexer);
7071       /* A very tricky bit is that `(struct S) { 3 }' is a
7072          compound-literal (which we permit in C++ as an extension).
7073          But, that construct is not a cast-expression -- it is a
7074          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7075          is legal; if the compound-literal were a cast-expression,
7076          you'd need an extra set of parentheses.)  But, if we parse
7077          the type-id, and it happens to be a class-specifier, then we
7078          will commit to the parse at that point, because we cannot
7079          undo the action that is done when creating a new class.  So,
7080          then we cannot back up and do a postfix-expression.
7081
7082          Therefore, we scan ahead to the closing `)', and check to see
7083          if the token after the `)' is a `{'.  If so, we are not
7084          looking at a cast-expression.
7085
7086          Save tokens so that we can put them back.  */
7087       cp_lexer_save_tokens (parser->lexer);
7088       /* Skip tokens until the next token is a closing parenthesis.
7089          If we find the closing `)', and the next token is a `{', then
7090          we are looking at a compound-literal.  */
7091       compound_literal_p
7092         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7093                                                   /*consume_paren=*/true)
7094            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7095       /* Roll back the tokens we skipped.  */
7096       cp_lexer_rollback_tokens (parser->lexer);
7097       /* If we were looking at a compound-literal, simulate an error
7098          so that the call to cp_parser_parse_definitely below will
7099          fail.  */
7100       if (compound_literal_p)
7101         cp_parser_simulate_error (parser);
7102       else
7103         {
7104           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7105           parser->in_type_id_in_expr_p = true;
7106           /* Look for the type-id.  */
7107           type = cp_parser_type_id (parser);
7108           /* Look for the closing `)'.  */
7109           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7110           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7111         }
7112
7113       /* Restore the saved message.  */
7114       parser->type_definition_forbidden_message = saved_message;
7115
7116       /* At this point this can only be either a cast or a
7117          parenthesized ctor such as `(T ())' that looks like a cast to
7118          function returning T.  */
7119       if (!cp_parser_error_occurred (parser)
7120           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7121                                                      (parser->lexer)))
7122         {
7123           cp_parser_parse_definitely (parser);
7124           expr = cp_parser_cast_expression (parser,
7125                                             /*address_p=*/false,
7126                                             /*cast_p=*/true, pidk);
7127
7128           /* Warn about old-style casts, if so requested.  */
7129           if (warn_old_style_cast
7130               && !in_system_header
7131               && !VOID_TYPE_P (type)
7132               && current_lang_name != lang_name_c)
7133             warning (OPT_Wold_style_cast, "use of old-style cast");
7134
7135           /* Only type conversions to integral or enumeration types
7136              can be used in constant-expressions.  */
7137           if (!cast_valid_in_integral_constant_expression_p (type)
7138               && cp_parser_non_integral_constant_expression (parser,
7139                                                              NIC_CAST))
7140             return error_mark_node;
7141
7142           /* Perform the cast.  */
7143           expr = build_c_cast (input_location, type, expr);
7144           return expr;
7145         }
7146       else 
7147         cp_parser_abort_tentative_parse (parser);
7148     }
7149
7150   /* If we get here, then it's not a cast, so it must be a
7151      unary-expression.  */
7152   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7153 }
7154
7155 /* Parse a binary expression of the general form:
7156
7157    pm-expression:
7158      cast-expression
7159      pm-expression .* cast-expression
7160      pm-expression ->* cast-expression
7161
7162    multiplicative-expression:
7163      pm-expression
7164      multiplicative-expression * pm-expression
7165      multiplicative-expression / pm-expression
7166      multiplicative-expression % pm-expression
7167
7168    additive-expression:
7169      multiplicative-expression
7170      additive-expression + multiplicative-expression
7171      additive-expression - multiplicative-expression
7172
7173    shift-expression:
7174      additive-expression
7175      shift-expression << additive-expression
7176      shift-expression >> additive-expression
7177
7178    relational-expression:
7179      shift-expression
7180      relational-expression < shift-expression
7181      relational-expression > shift-expression
7182      relational-expression <= shift-expression
7183      relational-expression >= shift-expression
7184
7185   GNU Extension:
7186
7187    relational-expression:
7188      relational-expression <? shift-expression
7189      relational-expression >? shift-expression
7190
7191    equality-expression:
7192      relational-expression
7193      equality-expression == relational-expression
7194      equality-expression != relational-expression
7195
7196    and-expression:
7197      equality-expression
7198      and-expression & equality-expression
7199
7200    exclusive-or-expression:
7201      and-expression
7202      exclusive-or-expression ^ and-expression
7203
7204    inclusive-or-expression:
7205      exclusive-or-expression
7206      inclusive-or-expression | exclusive-or-expression
7207
7208    logical-and-expression:
7209      inclusive-or-expression
7210      logical-and-expression && inclusive-or-expression
7211
7212    logical-or-expression:
7213      logical-and-expression
7214      logical-or-expression || logical-and-expression
7215
7216    All these are implemented with a single function like:
7217
7218    binary-expression:
7219      simple-cast-expression
7220      binary-expression <token> binary-expression
7221
7222    CAST_P is true if this expression is the target of a cast.
7223
7224    The binops_by_token map is used to get the tree codes for each <token> type.
7225    binary-expressions are associated according to a precedence table.  */
7226
7227 #define TOKEN_PRECEDENCE(token)                              \
7228 (((token->type == CPP_GREATER                                \
7229    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7230   && !parser->greater_than_is_operator_p)                    \
7231  ? PREC_NOT_OPERATOR                                         \
7232  : binops_by_token[token->type].prec)
7233
7234 static tree
7235 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7236                              bool no_toplevel_fold_p,
7237                              enum cp_parser_prec prec,
7238                              cp_id_kind * pidk)
7239 {
7240   cp_parser_expression_stack stack;
7241   cp_parser_expression_stack_entry *sp = &stack[0];
7242   tree lhs, rhs;
7243   cp_token *token;
7244   enum tree_code tree_type, lhs_type, rhs_type;
7245   enum cp_parser_prec new_prec, lookahead_prec;
7246   tree overload;
7247
7248   /* Parse the first expression.  */
7249   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7250   lhs_type = ERROR_MARK;
7251
7252   for (;;)
7253     {
7254       /* Get an operator token.  */
7255       token = cp_lexer_peek_token (parser->lexer);
7256
7257       if (warn_cxx0x_compat
7258           && token->type == CPP_RSHIFT
7259           && !parser->greater_than_is_operator_p)
7260         {
7261           if (warning_at (token->location, OPT_Wc__0x_compat, 
7262                           "%<>>%> operator is treated as"
7263                           " two right angle brackets in C++11"))
7264             inform (token->location,
7265                     "suggest parentheses around %<>>%> expression");
7266         }
7267
7268       new_prec = TOKEN_PRECEDENCE (token);
7269
7270       /* Popping an entry off the stack means we completed a subexpression:
7271          - either we found a token which is not an operator (`>' where it is not
7272            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7273            will happen repeatedly;
7274          - or, we found an operator which has lower priority.  This is the case
7275            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7276            parsing `3 * 4'.  */
7277       if (new_prec <= prec)
7278         {
7279           if (sp == stack)
7280             break;
7281           else
7282             goto pop;
7283         }
7284
7285      get_rhs:
7286       tree_type = binops_by_token[token->type].tree_type;
7287
7288       /* We used the operator token.  */
7289       cp_lexer_consume_token (parser->lexer);
7290
7291       /* For "false && x" or "true || x", x will never be executed;
7292          disable warnings while evaluating it.  */
7293       if (tree_type == TRUTH_ANDIF_EXPR)
7294         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7295       else if (tree_type == TRUTH_ORIF_EXPR)
7296         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7297
7298       /* Extract another operand.  It may be the RHS of this expression
7299          or the LHS of a new, higher priority expression.  */
7300       rhs = cp_parser_simple_cast_expression (parser);
7301       rhs_type = ERROR_MARK;
7302
7303       /* Get another operator token.  Look up its precedence to avoid
7304          building a useless (immediately popped) stack entry for common
7305          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7306       token = cp_lexer_peek_token (parser->lexer);
7307       lookahead_prec = TOKEN_PRECEDENCE (token);
7308       if (lookahead_prec > new_prec)
7309         {
7310           /* ... and prepare to parse the RHS of the new, higher priority
7311              expression.  Since precedence levels on the stack are
7312              monotonically increasing, we do not have to care about
7313              stack overflows.  */
7314           sp->prec = prec;
7315           sp->tree_type = tree_type;
7316           sp->lhs = lhs;
7317           sp->lhs_type = lhs_type;
7318           sp++;
7319           lhs = rhs;
7320           lhs_type = rhs_type;
7321           prec = new_prec;
7322           new_prec = lookahead_prec;
7323           goto get_rhs;
7324
7325          pop:
7326           lookahead_prec = new_prec;
7327           /* If the stack is not empty, we have parsed into LHS the right side
7328              (`4' in the example above) of an expression we had suspended.
7329              We can use the information on the stack to recover the LHS (`3')
7330              from the stack together with the tree code (`MULT_EXPR'), and
7331              the precedence of the higher level subexpression
7332              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7333              which will be used to actually build the additive expression.  */
7334           --sp;
7335           prec = sp->prec;
7336           tree_type = sp->tree_type;
7337           rhs = lhs;
7338           rhs_type = lhs_type;
7339           lhs = sp->lhs;
7340           lhs_type = sp->lhs_type;
7341         }
7342
7343       /* Undo the disabling of warnings done above.  */
7344       if (tree_type == TRUTH_ANDIF_EXPR)
7345         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7346       else if (tree_type == TRUTH_ORIF_EXPR)
7347         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7348
7349       overload = NULL;
7350       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7351          ERROR_MARK for everything that is not a binary expression.
7352          This makes warn_about_parentheses miss some warnings that
7353          involve unary operators.  For unary expressions we should
7354          pass the correct tree_code unless the unary expression was
7355          surrounded by parentheses.
7356       */
7357       if (no_toplevel_fold_p
7358           && lookahead_prec <= prec
7359           && sp == stack
7360           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7361         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7362       else
7363         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7364                                  &overload, tf_warning_or_error);
7365       lhs_type = tree_type;
7366
7367       /* If the binary operator required the use of an overloaded operator,
7368          then this expression cannot be an integral constant-expression.
7369          An overloaded operator can be used even if both operands are
7370          otherwise permissible in an integral constant-expression if at
7371          least one of the operands is of enumeration type.  */
7372
7373       if (overload
7374           && cp_parser_non_integral_constant_expression (parser,
7375                                                          NIC_OVERLOADED))
7376         return error_mark_node;
7377     }
7378
7379   return lhs;
7380 }
7381
7382
7383 /* Parse the `? expression : assignment-expression' part of a
7384    conditional-expression.  The LOGICAL_OR_EXPR is the
7385    logical-or-expression that started the conditional-expression.
7386    Returns a representation of the entire conditional-expression.
7387
7388    This routine is used by cp_parser_assignment_expression.
7389
7390      ? expression : assignment-expression
7391
7392    GNU Extensions:
7393
7394      ? : assignment-expression */
7395
7396 static tree
7397 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7398 {
7399   tree expr;
7400   tree assignment_expr;
7401   struct cp_token *token;
7402
7403   /* Consume the `?' token.  */
7404   cp_lexer_consume_token (parser->lexer);
7405   token = cp_lexer_peek_token (parser->lexer);
7406   if (cp_parser_allow_gnu_extensions_p (parser)
7407       && token->type == CPP_COLON)
7408     {
7409       pedwarn (token->location, OPT_pedantic, 
7410                "ISO C++ does not allow ?: with omitted middle operand");
7411       /* Implicit true clause.  */
7412       expr = NULL_TREE;
7413       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7414       warn_for_omitted_condop (token->location, logical_or_expr);
7415     }
7416   else
7417     {
7418       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7419       parser->colon_corrects_to_scope_p = false;
7420       /* Parse the expression.  */
7421       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7422       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7423       c_inhibit_evaluation_warnings +=
7424         ((logical_or_expr == truthvalue_true_node)
7425          - (logical_or_expr == truthvalue_false_node));
7426       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7427     }
7428
7429   /* The next token should be a `:'.  */
7430   cp_parser_require (parser, CPP_COLON, RT_COLON);
7431   /* Parse the assignment-expression.  */
7432   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7433   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7434
7435   /* Build the conditional-expression.  */
7436   return build_x_conditional_expr (logical_or_expr,
7437                                    expr,
7438                                    assignment_expr,
7439                                    tf_warning_or_error);
7440 }
7441
7442 /* Parse an assignment-expression.
7443
7444    assignment-expression:
7445      conditional-expression
7446      logical-or-expression assignment-operator assignment_expression
7447      throw-expression
7448
7449    CAST_P is true if this expression is the target of a cast.
7450
7451    Returns a representation for the expression.  */
7452
7453 static tree
7454 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7455                                  cp_id_kind * pidk)
7456 {
7457   tree expr;
7458
7459   /* If the next token is the `throw' keyword, then we're looking at
7460      a throw-expression.  */
7461   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7462     expr = cp_parser_throw_expression (parser);
7463   /* Otherwise, it must be that we are looking at a
7464      logical-or-expression.  */
7465   else
7466     {
7467       /* Parse the binary expressions (logical-or-expression).  */
7468       expr = cp_parser_binary_expression (parser, cast_p, false,
7469                                           PREC_NOT_OPERATOR, pidk);
7470       /* If the next token is a `?' then we're actually looking at a
7471          conditional-expression.  */
7472       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7473         return cp_parser_question_colon_clause (parser, expr);
7474       else
7475         {
7476           enum tree_code assignment_operator;
7477
7478           /* If it's an assignment-operator, we're using the second
7479              production.  */
7480           assignment_operator
7481             = cp_parser_assignment_operator_opt (parser);
7482           if (assignment_operator != ERROR_MARK)
7483             {
7484               bool non_constant_p;
7485
7486               /* Parse the right-hand side of the assignment.  */
7487               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7488
7489               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7490                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7491
7492               /* An assignment may not appear in a
7493                  constant-expression.  */
7494               if (cp_parser_non_integral_constant_expression (parser,
7495                                                               NIC_ASSIGNMENT))
7496                 return error_mark_node;
7497               /* Build the assignment expression.  */
7498               expr = build_x_modify_expr (expr,
7499                                           assignment_operator,
7500                                           rhs,
7501                                           tf_warning_or_error);
7502             }
7503         }
7504     }
7505
7506   return expr;
7507 }
7508
7509 /* Parse an (optional) assignment-operator.
7510
7511    assignment-operator: one of
7512      = *= /= %= += -= >>= <<= &= ^= |=
7513
7514    GNU Extension:
7515
7516    assignment-operator: one of
7517      <?= >?=
7518
7519    If the next token is an assignment operator, the corresponding tree
7520    code is returned, and the token is consumed.  For example, for
7521    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7522    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7523    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7524    operator, ERROR_MARK is returned.  */
7525
7526 static enum tree_code
7527 cp_parser_assignment_operator_opt (cp_parser* parser)
7528 {
7529   enum tree_code op;
7530   cp_token *token;
7531
7532   /* Peek at the next token.  */
7533   token = cp_lexer_peek_token (parser->lexer);
7534
7535   switch (token->type)
7536     {
7537     case CPP_EQ:
7538       op = NOP_EXPR;
7539       break;
7540
7541     case CPP_MULT_EQ:
7542       op = MULT_EXPR;
7543       break;
7544
7545     case CPP_DIV_EQ:
7546       op = TRUNC_DIV_EXPR;
7547       break;
7548
7549     case CPP_MOD_EQ:
7550       op = TRUNC_MOD_EXPR;
7551       break;
7552
7553     case CPP_PLUS_EQ:
7554       op = PLUS_EXPR;
7555       break;
7556
7557     case CPP_MINUS_EQ:
7558       op = MINUS_EXPR;
7559       break;
7560
7561     case CPP_RSHIFT_EQ:
7562       op = RSHIFT_EXPR;
7563       break;
7564
7565     case CPP_LSHIFT_EQ:
7566       op = LSHIFT_EXPR;
7567       break;
7568
7569     case CPP_AND_EQ:
7570       op = BIT_AND_EXPR;
7571       break;
7572
7573     case CPP_XOR_EQ:
7574       op = BIT_XOR_EXPR;
7575       break;
7576
7577     case CPP_OR_EQ:
7578       op = BIT_IOR_EXPR;
7579       break;
7580
7581     default:
7582       /* Nothing else is an assignment operator.  */
7583       op = ERROR_MARK;
7584     }
7585
7586   /* If it was an assignment operator, consume it.  */
7587   if (op != ERROR_MARK)
7588     cp_lexer_consume_token (parser->lexer);
7589
7590   return op;
7591 }
7592
7593 /* Parse an expression.
7594
7595    expression:
7596      assignment-expression
7597      expression , assignment-expression
7598
7599    CAST_P is true if this expression is the target of a cast.
7600
7601    Returns a representation of the expression.  */
7602
7603 static tree
7604 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7605 {
7606   tree expression = NULL_TREE;
7607
7608   while (true)
7609     {
7610       tree assignment_expression;
7611
7612       /* Parse the next assignment-expression.  */
7613       assignment_expression
7614         = cp_parser_assignment_expression (parser, cast_p, pidk);
7615       /* If this is the first assignment-expression, we can just
7616          save it away.  */
7617       if (!expression)
7618         expression = assignment_expression;
7619       else
7620         expression = build_x_compound_expr (expression,
7621                                             assignment_expression,
7622                                             tf_warning_or_error);
7623       /* If the next token is not a comma, then we are done with the
7624          expression.  */
7625       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7626         break;
7627       /* Consume the `,'.  */
7628       cp_lexer_consume_token (parser->lexer);
7629       /* A comma operator cannot appear in a constant-expression.  */
7630       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7631         expression = error_mark_node;
7632     }
7633
7634   return expression;
7635 }
7636
7637 /* Parse a constant-expression.
7638
7639    constant-expression:
7640      conditional-expression
7641
7642   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7643   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7644   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7645   is false, NON_CONSTANT_P should be NULL.  */
7646
7647 static tree
7648 cp_parser_constant_expression (cp_parser* parser,
7649                                bool allow_non_constant_p,
7650                                bool *non_constant_p)
7651 {
7652   bool saved_integral_constant_expression_p;
7653   bool saved_allow_non_integral_constant_expression_p;
7654   bool saved_non_integral_constant_expression_p;
7655   tree expression;
7656
7657   /* It might seem that we could simply parse the
7658      conditional-expression, and then check to see if it were
7659      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7660      one that the compiler can figure out is constant, possibly after
7661      doing some simplifications or optimizations.  The standard has a
7662      precise definition of constant-expression, and we must honor
7663      that, even though it is somewhat more restrictive.
7664
7665      For example:
7666
7667        int i[(2, 3)];
7668
7669      is not a legal declaration, because `(2, 3)' is not a
7670      constant-expression.  The `,' operator is forbidden in a
7671      constant-expression.  However, GCC's constant-folding machinery
7672      will fold this operation to an INTEGER_CST for `3'.  */
7673
7674   /* Save the old settings.  */
7675   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7676   saved_allow_non_integral_constant_expression_p
7677     = parser->allow_non_integral_constant_expression_p;
7678   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7679   /* We are now parsing a constant-expression.  */
7680   parser->integral_constant_expression_p = true;
7681   parser->allow_non_integral_constant_expression_p
7682     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7683   parser->non_integral_constant_expression_p = false;
7684   /* Although the grammar says "conditional-expression", we parse an
7685      "assignment-expression", which also permits "throw-expression"
7686      and the use of assignment operators.  In the case that
7687      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7688      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7689      actually essential that we look for an assignment-expression.
7690      For example, cp_parser_initializer_clauses uses this function to
7691      determine whether a particular assignment-expression is in fact
7692      constant.  */
7693   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7694   /* Restore the old settings.  */
7695   parser->integral_constant_expression_p
7696     = saved_integral_constant_expression_p;
7697   parser->allow_non_integral_constant_expression_p
7698     = saved_allow_non_integral_constant_expression_p;
7699   if (cxx_dialect >= cxx0x)
7700     {
7701       /* Require an rvalue constant expression here; that's what our
7702          callers expect.  Reference constant expressions are handled
7703          separately in e.g. cp_parser_template_argument.  */
7704       bool is_const = potential_rvalue_constant_expression (expression);
7705       parser->non_integral_constant_expression_p = !is_const;
7706       if (!is_const && !allow_non_constant_p)
7707         require_potential_rvalue_constant_expression (expression);
7708     }
7709   if (allow_non_constant_p)
7710     *non_constant_p = parser->non_integral_constant_expression_p;
7711   parser->non_integral_constant_expression_p
7712     = saved_non_integral_constant_expression_p;
7713
7714   return expression;
7715 }
7716
7717 /* Parse __builtin_offsetof.
7718
7719    offsetof-expression:
7720      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7721
7722    offsetof-member-designator:
7723      id-expression
7724      | offsetof-member-designator "." id-expression
7725      | offsetof-member-designator "[" expression "]"
7726      | offsetof-member-designator "->" id-expression  */
7727
7728 static tree
7729 cp_parser_builtin_offsetof (cp_parser *parser)
7730 {
7731   int save_ice_p, save_non_ice_p;
7732   tree type, expr;
7733   cp_id_kind dummy;
7734   cp_token *token;
7735
7736   /* We're about to accept non-integral-constant things, but will
7737      definitely yield an integral constant expression.  Save and
7738      restore these values around our local parsing.  */
7739   save_ice_p = parser->integral_constant_expression_p;
7740   save_non_ice_p = parser->non_integral_constant_expression_p;
7741
7742   /* Consume the "__builtin_offsetof" token.  */
7743   cp_lexer_consume_token (parser->lexer);
7744   /* Consume the opening `('.  */
7745   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7746   /* Parse the type-id.  */
7747   type = cp_parser_type_id (parser);
7748   /* Look for the `,'.  */
7749   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7750   token = cp_lexer_peek_token (parser->lexer);
7751
7752   /* Build the (type *)null that begins the traditional offsetof macro.  */
7753   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7754                             tf_warning_or_error);
7755
7756   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7757   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7758                                                  true, &dummy, token->location);
7759   while (true)
7760     {
7761       token = cp_lexer_peek_token (parser->lexer);
7762       switch (token->type)
7763         {
7764         case CPP_OPEN_SQUARE:
7765           /* offsetof-member-designator "[" expression "]" */
7766           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7767           break;
7768
7769         case CPP_DEREF:
7770           /* offsetof-member-designator "->" identifier */
7771           expr = grok_array_decl (expr, integer_zero_node);
7772           /* FALLTHRU */
7773
7774         case CPP_DOT:
7775           /* offsetof-member-designator "." identifier */
7776           cp_lexer_consume_token (parser->lexer);
7777           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7778                                                          expr, true, &dummy,
7779                                                          token->location);
7780           break;
7781
7782         case CPP_CLOSE_PAREN:
7783           /* Consume the ")" token.  */
7784           cp_lexer_consume_token (parser->lexer);
7785           goto success;
7786
7787         default:
7788           /* Error.  We know the following require will fail, but
7789              that gives the proper error message.  */
7790           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7791           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7792           expr = error_mark_node;
7793           goto failure;
7794         }
7795     }
7796
7797  success:
7798   /* If we're processing a template, we can't finish the semantics yet.
7799      Otherwise we can fold the entire expression now.  */
7800   if (processing_template_decl)
7801     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7802   else
7803     expr = finish_offsetof (expr);
7804
7805  failure:
7806   parser->integral_constant_expression_p = save_ice_p;
7807   parser->non_integral_constant_expression_p = save_non_ice_p;
7808
7809   return expr;
7810 }
7811
7812 /* Parse a trait expression.
7813
7814    Returns a representation of the expression, the underlying type
7815    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7816
7817 static tree
7818 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7819 {
7820   cp_trait_kind kind;
7821   tree type1, type2 = NULL_TREE;
7822   bool binary = false;
7823   cp_decl_specifier_seq decl_specs;
7824
7825   switch (keyword)
7826     {
7827     case RID_HAS_NOTHROW_ASSIGN:
7828       kind = CPTK_HAS_NOTHROW_ASSIGN;
7829       break;
7830     case RID_HAS_NOTHROW_CONSTRUCTOR:
7831       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7832       break;
7833     case RID_HAS_NOTHROW_COPY:
7834       kind = CPTK_HAS_NOTHROW_COPY;
7835       break;
7836     case RID_HAS_TRIVIAL_ASSIGN:
7837       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7838       break;
7839     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7840       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7841       break;
7842     case RID_HAS_TRIVIAL_COPY:
7843       kind = CPTK_HAS_TRIVIAL_COPY;
7844       break;
7845     case RID_HAS_TRIVIAL_DESTRUCTOR:
7846       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7847       break;
7848     case RID_HAS_VIRTUAL_DESTRUCTOR:
7849       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7850       break;
7851     case RID_IS_ABSTRACT:
7852       kind = CPTK_IS_ABSTRACT;
7853       break;
7854     case RID_IS_BASE_OF:
7855       kind = CPTK_IS_BASE_OF;
7856       binary = true;
7857       break;
7858     case RID_IS_CLASS:
7859       kind = CPTK_IS_CLASS;
7860       break;
7861     case RID_IS_CONVERTIBLE_TO:
7862       kind = CPTK_IS_CONVERTIBLE_TO;
7863       binary = true;
7864       break;
7865     case RID_IS_EMPTY:
7866       kind = CPTK_IS_EMPTY;
7867       break;
7868     case RID_IS_ENUM:
7869       kind = CPTK_IS_ENUM;
7870       break;
7871     case RID_IS_LITERAL_TYPE:
7872       kind = CPTK_IS_LITERAL_TYPE;
7873       break;
7874     case RID_IS_POD:
7875       kind = CPTK_IS_POD;
7876       break;
7877     case RID_IS_POLYMORPHIC:
7878       kind = CPTK_IS_POLYMORPHIC;
7879       break;
7880     case RID_IS_STD_LAYOUT:
7881       kind = CPTK_IS_STD_LAYOUT;
7882       break;
7883     case RID_IS_TRIVIAL:
7884       kind = CPTK_IS_TRIVIAL;
7885       break;
7886     case RID_IS_UNION:
7887       kind = CPTK_IS_UNION;
7888       break;
7889     case RID_UNDERLYING_TYPE:
7890       kind = CPTK_UNDERLYING_TYPE;
7891       break;
7892     case RID_BASES:
7893       kind = CPTK_BASES;
7894       break;
7895     case RID_DIRECT_BASES:
7896       kind = CPTK_DIRECT_BASES;
7897       break;
7898     default:
7899       gcc_unreachable ();
7900     }
7901
7902   /* Consume the token.  */
7903   cp_lexer_consume_token (parser->lexer);
7904
7905   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7906
7907   type1 = cp_parser_type_id (parser);
7908
7909   if (type1 == error_mark_node)
7910     return error_mark_node;
7911
7912   /* Build a trivial decl-specifier-seq.  */
7913   clear_decl_specs (&decl_specs);
7914   decl_specs.type = type1;
7915
7916   /* Call grokdeclarator to figure out what type this is.  */
7917   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7918                           /*initialized=*/0, /*attrlist=*/NULL);
7919
7920   if (binary)
7921     {
7922       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7923  
7924       type2 = cp_parser_type_id (parser);
7925
7926       if (type2 == error_mark_node)
7927         return error_mark_node;
7928
7929       /* Build a trivial decl-specifier-seq.  */
7930       clear_decl_specs (&decl_specs);
7931       decl_specs.type = type2;
7932
7933       /* Call grokdeclarator to figure out what type this is.  */
7934       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7935                               /*initialized=*/0, /*attrlist=*/NULL);
7936     }
7937
7938   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7939
7940   /* Complete the trait expression, which may mean either processing
7941      the trait expr now or saving it for template instantiation.  */
7942   switch(kind)
7943     {
7944     case CPTK_UNDERLYING_TYPE:
7945       return finish_underlying_type (type1);
7946     case CPTK_BASES:
7947       return finish_bases (type1, false);
7948     case CPTK_DIRECT_BASES:
7949       return finish_bases (type1, true);
7950     default:
7951       return finish_trait_expr (kind, type1, type2);
7952     }
7953 }
7954
7955 /* Lambdas that appear in variable initializer or default argument scope
7956    get that in their mangling, so we need to record it.  We might as well
7957    use the count for function and namespace scopes as well.  */
7958 static GTY(()) tree lambda_scope;
7959 static GTY(()) int lambda_count;
7960 typedef struct GTY(()) tree_int
7961 {
7962   tree t;
7963   int i;
7964 } tree_int;
7965 DEF_VEC_O(tree_int);
7966 DEF_VEC_ALLOC_O(tree_int,gc);
7967 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7968
7969 static void
7970 start_lambda_scope (tree decl)
7971 {
7972   tree_int ti;
7973   gcc_assert (decl);
7974   /* Once we're inside a function, we ignore other scopes and just push
7975      the function again so that popping works properly.  */
7976   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7977     decl = current_function_decl;
7978   ti.t = lambda_scope;
7979   ti.i = lambda_count;
7980   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7981   if (lambda_scope != decl)
7982     {
7983       /* Don't reset the count if we're still in the same function.  */
7984       lambda_scope = decl;
7985       lambda_count = 0;
7986     }
7987 }
7988
7989 static void
7990 record_lambda_scope (tree lambda)
7991 {
7992   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7993   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7994 }
7995
7996 static void
7997 finish_lambda_scope (void)
7998 {
7999   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8000   if (lambda_scope != p->t)
8001     {
8002       lambda_scope = p->t;
8003       lambda_count = p->i;
8004     }
8005   VEC_pop (tree_int, lambda_scope_stack);
8006 }
8007
8008 /* Parse a lambda expression.
8009
8010    lambda-expression:
8011      lambda-introducer lambda-declarator [opt] compound-statement
8012
8013    Returns a representation of the expression.  */
8014
8015 static tree
8016 cp_parser_lambda_expression (cp_parser* parser)
8017 {
8018   tree lambda_expr = build_lambda_expr ();
8019   tree type;
8020   bool ok;
8021
8022   LAMBDA_EXPR_LOCATION (lambda_expr)
8023     = cp_lexer_peek_token (parser->lexer)->location;
8024
8025   if (cp_unevaluated_operand)
8026     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8027               "lambda-expression in unevaluated context");
8028
8029   /* We may be in the middle of deferred access check.  Disable
8030      it now.  */
8031   push_deferring_access_checks (dk_no_deferred);
8032
8033   cp_parser_lambda_introducer (parser, lambda_expr);
8034
8035   type = begin_lambda_type (lambda_expr);
8036
8037   record_lambda_scope (lambda_expr);
8038
8039   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8040   determine_visibility (TYPE_NAME (type));
8041
8042   /* Now that we've started the type, add the capture fields for any
8043      explicit captures.  */
8044   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8045
8046   {
8047     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8048     unsigned int saved_num_template_parameter_lists
8049         = parser->num_template_parameter_lists;
8050     unsigned char in_statement = parser->in_statement;
8051     bool in_switch_statement_p = parser->in_switch_statement_p;
8052
8053     parser->num_template_parameter_lists = 0;
8054     parser->in_statement = 0;
8055     parser->in_switch_statement_p = false;
8056
8057     /* By virtue of defining a local class, a lambda expression has access to
8058        the private variables of enclosing classes.  */
8059
8060     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8061
8062     if (ok)
8063       cp_parser_lambda_body (parser, lambda_expr);
8064     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8065       cp_parser_skip_to_end_of_block_or_statement (parser);
8066
8067     /* The capture list was built up in reverse order; fix that now.  */
8068     {
8069       tree newlist = NULL_TREE;
8070       tree elt, next;
8071
8072       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8073            elt; elt = next)
8074         {
8075           next = TREE_CHAIN (elt);
8076           TREE_CHAIN (elt) = newlist;
8077           newlist = elt;
8078         }
8079       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8080     }
8081
8082     if (ok)
8083       maybe_add_lambda_conv_op (type);
8084
8085     type = finish_struct (type, /*attributes=*/NULL_TREE);
8086
8087     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8088     parser->in_statement = in_statement;
8089     parser->in_switch_statement_p = in_switch_statement_p;
8090   }
8091
8092   pop_deferring_access_checks ();
8093
8094   /* This field is only used during parsing of the lambda.  */
8095   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8096
8097   /* This lambda shouldn't have any proxies left at this point.  */
8098   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8099   /* And now that we're done, push proxies for an enclosing lambda.  */
8100   insert_pending_capture_proxies ();
8101
8102   if (ok)
8103     return build_lambda_object (lambda_expr);
8104   else
8105     return error_mark_node;
8106 }
8107
8108 /* Parse the beginning of a lambda expression.
8109
8110    lambda-introducer:
8111      [ lambda-capture [opt] ]
8112
8113    LAMBDA_EXPR is the current representation of the lambda expression.  */
8114
8115 static void
8116 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8117 {
8118   /* Need commas after the first capture.  */
8119   bool first = true;
8120
8121   /* Eat the leading `['.  */
8122   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8123
8124   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8125   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8126       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8127     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8128   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8129     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8130
8131   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8132     {
8133       cp_lexer_consume_token (parser->lexer);
8134       first = false;
8135     }
8136
8137   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8138     {
8139       cp_token* capture_token;
8140       tree capture_id;
8141       tree capture_init_expr;
8142       cp_id_kind idk = CP_ID_KIND_NONE;
8143       bool explicit_init_p = false;
8144
8145       enum capture_kind_type
8146       {
8147         BY_COPY,
8148         BY_REFERENCE
8149       };
8150       enum capture_kind_type capture_kind = BY_COPY;
8151
8152       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8153         {
8154           error ("expected end of capture-list");
8155           return;
8156         }
8157
8158       if (first)
8159         first = false;
8160       else
8161         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8162
8163       /* Possibly capture `this'.  */
8164       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8165         {
8166           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8167           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8168             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8169                      "with by-copy capture default");
8170           cp_lexer_consume_token (parser->lexer);
8171           add_capture (lambda_expr,
8172                        /*id=*/this_identifier,
8173                        /*initializer=*/finish_this_expr(),
8174                        /*by_reference_p=*/false,
8175                        explicit_init_p);
8176           continue;
8177         }
8178
8179       /* Remember whether we want to capture as a reference or not.  */
8180       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8181         {
8182           capture_kind = BY_REFERENCE;
8183           cp_lexer_consume_token (parser->lexer);
8184         }
8185
8186       /* Get the identifier.  */
8187       capture_token = cp_lexer_peek_token (parser->lexer);
8188       capture_id = cp_parser_identifier (parser);
8189
8190       if (capture_id == error_mark_node)
8191         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8192            delimiters, but I modified this to stop on unnested ']' as well.  It
8193            was already changed to stop on unnested '}', so the
8194            "closing_parenthesis" name is no more misleading with my change.  */
8195         {
8196           cp_parser_skip_to_closing_parenthesis (parser,
8197                                                  /*recovering=*/true,
8198                                                  /*or_comma=*/true,
8199                                                  /*consume_paren=*/true);
8200           break;
8201         }
8202
8203       /* Find the initializer for this capture.  */
8204       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8205         {
8206           /* An explicit expression exists.  */
8207           cp_lexer_consume_token (parser->lexer);
8208           pedwarn (input_location, OPT_pedantic,
8209                    "ISO C++ does not allow initializers "
8210                    "in lambda expression capture lists");
8211           capture_init_expr = cp_parser_assignment_expression (parser,
8212                                                                /*cast_p=*/true,
8213                                                                &idk);
8214           explicit_init_p = true;
8215         }
8216       else
8217         {
8218           const char* error_msg;
8219
8220           /* Turn the identifier into an id-expression.  */
8221           capture_init_expr
8222             = cp_parser_lookup_name
8223                 (parser,
8224                  capture_id,
8225                  none_type,
8226                  /*is_template=*/false,
8227                  /*is_namespace=*/false,
8228                  /*check_dependency=*/true,
8229                  /*ambiguous_decls=*/NULL,
8230                  capture_token->location);
8231
8232           if (capture_init_expr == error_mark_node)
8233             {
8234               unqualified_name_lookup_error (capture_id);
8235               continue;
8236             }
8237           else if (DECL_P (capture_init_expr)
8238                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8239                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8240             {
8241               error_at (capture_token->location,
8242                         "capture of non-variable %qD ",
8243                         capture_init_expr);
8244               inform (0, "%q+#D declared here", capture_init_expr);
8245               continue;
8246             }
8247           if (TREE_CODE (capture_init_expr) == VAR_DECL
8248               && decl_storage_duration (capture_init_expr) != dk_auto)
8249             {
8250               pedwarn (capture_token->location, 0, "capture of variable "
8251                        "%qD with non-automatic storage duration",
8252                        capture_init_expr);
8253               inform (0, "%q+#D declared here", capture_init_expr);
8254               continue;
8255             }
8256
8257           capture_init_expr
8258             = finish_id_expression
8259                 (capture_id,
8260                  capture_init_expr,
8261                  parser->scope,
8262                  &idk,
8263                  /*integral_constant_expression_p=*/false,
8264                  /*allow_non_integral_constant_expression_p=*/false,
8265                  /*non_integral_constant_expression_p=*/NULL,
8266                  /*template_p=*/false,
8267                  /*done=*/true,
8268                  /*address_p=*/false,
8269                  /*template_arg_p=*/false,
8270                  &error_msg,
8271                  capture_token->location);
8272         }
8273
8274       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8275           && !explicit_init_p)
8276         {
8277           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8278               && capture_kind == BY_COPY)
8279             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8280                      "of %qD redundant with by-copy capture default",
8281                      capture_id);
8282           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8283               && capture_kind == BY_REFERENCE)
8284             pedwarn (capture_token->location, 0, "explicit by-reference "
8285                      "capture of %qD redundant with by-reference capture "
8286                      "default", capture_id);
8287         }
8288
8289       add_capture (lambda_expr,
8290                    capture_id,
8291                    capture_init_expr,
8292                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8293                    explicit_init_p);
8294     }
8295
8296   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8297 }
8298
8299 /* Parse the (optional) middle of a lambda expression.
8300
8301    lambda-declarator:
8302      ( parameter-declaration-clause [opt] )
8303        attribute-specifier [opt]
8304        mutable [opt]
8305        exception-specification [opt]
8306        lambda-return-type-clause [opt]
8307
8308    LAMBDA_EXPR is the current representation of the lambda expression.  */
8309
8310 static bool
8311 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8312 {
8313   /* 5.1.1.4 of the standard says:
8314        If a lambda-expression does not include a lambda-declarator, it is as if
8315        the lambda-declarator were ().
8316      This means an empty parameter list, no attributes, and no exception
8317      specification.  */
8318   tree param_list = void_list_node;
8319   tree attributes = NULL_TREE;
8320   tree exception_spec = NULL_TREE;
8321   tree t;
8322
8323   /* The lambda-declarator is optional, but must begin with an opening
8324      parenthesis if present.  */
8325   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8326     {
8327       cp_lexer_consume_token (parser->lexer);
8328
8329       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8330
8331       /* Parse parameters.  */
8332       param_list = cp_parser_parameter_declaration_clause (parser);
8333
8334       /* Default arguments shall not be specified in the
8335          parameter-declaration-clause of a lambda-declarator.  */
8336       for (t = param_list; t; t = TREE_CHAIN (t))
8337         if (TREE_PURPOSE (t))
8338           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8339                    "default argument specified for lambda parameter");
8340
8341       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8342
8343       attributes = cp_parser_attributes_opt (parser);
8344
8345       /* Parse optional `mutable' keyword.  */
8346       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8347         {
8348           cp_lexer_consume_token (parser->lexer);
8349           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8350         }
8351
8352       /* Parse optional exception specification.  */
8353       exception_spec = cp_parser_exception_specification_opt (parser);
8354
8355       /* Parse optional trailing return type.  */
8356       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8357         {
8358           cp_lexer_consume_token (parser->lexer);
8359           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8360         }
8361
8362       /* The function parameters must be in scope all the way until after the
8363          trailing-return-type in case of decltype.  */
8364       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8365         pop_binding (DECL_NAME (t), t);
8366
8367       leave_scope ();
8368     }
8369
8370   /* Create the function call operator.
8371
8372      Messing with declarators like this is no uglier than building up the
8373      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8374      other code.  */
8375   {
8376     cp_decl_specifier_seq return_type_specs;
8377     cp_declarator* declarator;
8378     tree fco;
8379     int quals;
8380     void *p;
8381
8382     clear_decl_specs (&return_type_specs);
8383     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8384       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8385     else
8386       /* Maybe we will deduce the return type later, but we can use void
8387          as a placeholder return type anyways.  */
8388       return_type_specs.type = void_type_node;
8389
8390     p = obstack_alloc (&declarator_obstack, 0);
8391
8392     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8393                                      sfk_none);
8394
8395     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8396              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8397     declarator = make_call_declarator (declarator, param_list, quals,
8398                                        VIRT_SPEC_UNSPECIFIED,
8399                                        exception_spec,
8400                                        /*late_return_type=*/NULL_TREE);
8401     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8402
8403     fco = grokmethod (&return_type_specs,
8404                       declarator,
8405                       attributes);
8406     if (fco != error_mark_node)
8407       {
8408         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8409         DECL_ARTIFICIAL (fco) = 1;
8410         /* Give the object parameter a different name.  */
8411         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8412       }
8413
8414     finish_member_declaration (fco);
8415
8416     obstack_free (&declarator_obstack, p);
8417
8418     return (fco != error_mark_node);
8419   }
8420 }
8421
8422 /* Parse the body of a lambda expression, which is simply
8423
8424    compound-statement
8425
8426    but which requires special handling.
8427    LAMBDA_EXPR is the current representation of the lambda expression.  */
8428
8429 static void
8430 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8431 {
8432   bool nested = (current_function_decl != NULL_TREE);
8433   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8434   if (nested)
8435     push_function_context ();
8436   else
8437     /* Still increment function_depth so that we don't GC in the
8438        middle of an expression.  */
8439     ++function_depth;
8440   /* Clear this in case we're in the middle of a default argument.  */
8441   parser->local_variables_forbidden_p = false;
8442
8443   /* Finish the function call operator
8444      - class_specifier
8445      + late_parsing_for_member
8446      + function_definition_after_declarator
8447      + ctor_initializer_opt_and_function_body  */
8448   {
8449     tree fco = lambda_function (lambda_expr);
8450     tree body;
8451     bool done = false;
8452     tree compound_stmt;
8453     tree cap;
8454
8455     /* Let the front end know that we are going to be defining this
8456        function.  */
8457     start_preparsed_function (fco,
8458                               NULL_TREE,
8459                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8460
8461     start_lambda_scope (fco);
8462     body = begin_function_body ();
8463
8464     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8465       goto out;
8466
8467     /* Push the proxies for any explicit captures.  */
8468     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8469          cap = TREE_CHAIN (cap))
8470       build_capture_proxy (TREE_PURPOSE (cap));
8471
8472     compound_stmt = begin_compound_stmt (0);
8473
8474     /* 5.1.1.4 of the standard says:
8475          If a lambda-expression does not include a trailing-return-type, it
8476          is as if the trailing-return-type denotes the following type:
8477           * if the compound-statement is of the form
8478                { return attribute-specifier [opt] expression ; }
8479              the type of the returned expression after lvalue-to-rvalue
8480              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8481              (_conv.array_ 4.2), and function-to-pointer conversion
8482              (_conv.func_ 4.3);
8483           * otherwise, void.  */
8484
8485     /* In a lambda that has neither a lambda-return-type-clause
8486        nor a deducible form, errors should be reported for return statements
8487        in the body.  Since we used void as the placeholder return type, parsing
8488        the body as usual will give such desired behavior.  */
8489     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8490         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8491         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8492       {
8493         tree expr = NULL_TREE;
8494         cp_id_kind idk = CP_ID_KIND_NONE;
8495
8496         /* Parse tentatively in case there's more after the initial return
8497            statement.  */
8498         cp_parser_parse_tentatively (parser);
8499
8500         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8501
8502         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8503
8504         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8505         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8506
8507         if (cp_parser_parse_definitely (parser))
8508           {
8509             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8510
8511             /* Will get error here if type not deduced yet.  */
8512             finish_return_stmt (expr);
8513
8514             done = true;
8515           }
8516       }
8517
8518     if (!done)
8519       {
8520         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8521           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8522         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8523           cp_parser_label_declaration (parser);
8524         cp_parser_statement_seq_opt (parser, NULL_TREE);
8525         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8526         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8527       }
8528
8529     finish_compound_stmt (compound_stmt);
8530
8531   out:
8532     finish_function_body (body);
8533     finish_lambda_scope ();
8534
8535     /* Finish the function and generate code for it if necessary.  */
8536     expand_or_defer_fn (finish_function (/*inline*/2));
8537   }
8538
8539   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8540   if (nested)
8541     pop_function_context();
8542   else
8543     --function_depth;
8544 }
8545
8546 /* Statements [gram.stmt.stmt]  */
8547
8548 /* Parse a statement.
8549
8550    statement:
8551      labeled-statement
8552      expression-statement
8553      compound-statement
8554      selection-statement
8555      iteration-statement
8556      jump-statement
8557      declaration-statement
8558      try-block
8559
8560   TM Extension:
8561
8562    statement:
8563      atomic-statement
8564
8565   IN_COMPOUND is true when the statement is nested inside a
8566   cp_parser_compound_statement; this matters for certain pragmas.
8567
8568   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8569   is a (possibly labeled) if statement which is not enclosed in braces
8570   and has an else clause.  This is used to implement -Wparentheses.  */
8571
8572 static void
8573 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8574                      bool in_compound, bool *if_p)
8575 {
8576   tree statement;
8577   cp_token *token;
8578   location_t statement_location;
8579
8580  restart:
8581   if (if_p != NULL)
8582     *if_p = false;
8583   /* There is no statement yet.  */
8584   statement = NULL_TREE;
8585   /* Peek at the next token.  */
8586   token = cp_lexer_peek_token (parser->lexer);
8587   /* Remember the location of the first token in the statement.  */
8588   statement_location = token->location;
8589   /* If this is a keyword, then that will often determine what kind of
8590      statement we have.  */
8591   if (token->type == CPP_KEYWORD)
8592     {
8593       enum rid keyword = token->keyword;
8594
8595       switch (keyword)
8596         {
8597         case RID_CASE:
8598         case RID_DEFAULT:
8599           /* Looks like a labeled-statement with a case label.
8600              Parse the label, and then use tail recursion to parse
8601              the statement.  */
8602           cp_parser_label_for_labeled_statement (parser);
8603           goto restart;
8604
8605         case RID_IF:
8606         case RID_SWITCH:
8607           statement = cp_parser_selection_statement (parser, if_p);
8608           break;
8609
8610         case RID_WHILE:
8611         case RID_DO:
8612         case RID_FOR:
8613           statement = cp_parser_iteration_statement (parser);
8614           break;
8615
8616         case RID_BREAK:
8617         case RID_CONTINUE:
8618         case RID_RETURN:
8619         case RID_GOTO:
8620           statement = cp_parser_jump_statement (parser);
8621           break;
8622
8623           /* Objective-C++ exception-handling constructs.  */
8624         case RID_AT_TRY:
8625         case RID_AT_CATCH:
8626         case RID_AT_FINALLY:
8627         case RID_AT_SYNCHRONIZED:
8628         case RID_AT_THROW:
8629           statement = cp_parser_objc_statement (parser);
8630           break;
8631
8632         case RID_TRY:
8633           statement = cp_parser_try_block (parser);
8634           break;
8635
8636         case RID_NAMESPACE:
8637           /* This must be a namespace alias definition.  */
8638           cp_parser_declaration_statement (parser);
8639           return;
8640           
8641         case RID_TRANSACTION_ATOMIC:
8642         case RID_TRANSACTION_RELAXED:
8643           statement = cp_parser_transaction (parser, keyword);
8644           break;
8645         case RID_TRANSACTION_CANCEL:
8646           statement = cp_parser_transaction_cancel (parser);
8647           break;
8648
8649         default:
8650           /* It might be a keyword like `int' that can start a
8651              declaration-statement.  */
8652           break;
8653         }
8654     }
8655   else if (token->type == CPP_NAME)
8656     {
8657       /* If the next token is a `:', then we are looking at a
8658          labeled-statement.  */
8659       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8660       if (token->type == CPP_COLON)
8661         {
8662           /* Looks like a labeled-statement with an ordinary label.
8663              Parse the label, and then use tail recursion to parse
8664              the statement.  */
8665           cp_parser_label_for_labeled_statement (parser);
8666           goto restart;
8667         }
8668     }
8669   /* Anything that starts with a `{' must be a compound-statement.  */
8670   else if (token->type == CPP_OPEN_BRACE)
8671     statement = cp_parser_compound_statement (parser, NULL, false, false);
8672   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8673      a statement all its own.  */
8674   else if (token->type == CPP_PRAGMA)
8675     {
8676       /* Only certain OpenMP pragmas are attached to statements, and thus
8677          are considered statements themselves.  All others are not.  In
8678          the context of a compound, accept the pragma as a "statement" and
8679          return so that we can check for a close brace.  Otherwise we
8680          require a real statement and must go back and read one.  */
8681       if (in_compound)
8682         cp_parser_pragma (parser, pragma_compound);
8683       else if (!cp_parser_pragma (parser, pragma_stmt))
8684         goto restart;
8685       return;
8686     }
8687   else if (token->type == CPP_EOF)
8688     {
8689       cp_parser_error (parser, "expected statement");
8690       return;
8691     }
8692
8693   /* Everything else must be a declaration-statement or an
8694      expression-statement.  Try for the declaration-statement
8695      first, unless we are looking at a `;', in which case we know that
8696      we have an expression-statement.  */
8697   if (!statement)
8698     {
8699       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8700         {
8701           cp_parser_parse_tentatively (parser);
8702           /* Try to parse the declaration-statement.  */
8703           cp_parser_declaration_statement (parser);
8704           /* If that worked, we're done.  */
8705           if (cp_parser_parse_definitely (parser))
8706             return;
8707         }
8708       /* Look for an expression-statement instead.  */
8709       statement = cp_parser_expression_statement (parser, in_statement_expr);
8710     }
8711
8712   /* Set the line number for the statement.  */
8713   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8714     SET_EXPR_LOCATION (statement, statement_location);
8715 }
8716
8717 /* Parse the label for a labeled-statement, i.e.
8718
8719    identifier :
8720    case constant-expression :
8721    default :
8722
8723    GNU Extension:
8724    case constant-expression ... constant-expression : statement
8725
8726    When a label is parsed without errors, the label is added to the
8727    parse tree by the finish_* functions, so this function doesn't
8728    have to return the label.  */
8729
8730 static void
8731 cp_parser_label_for_labeled_statement (cp_parser* parser)
8732 {
8733   cp_token *token;
8734   tree label = NULL_TREE;
8735   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8736
8737   /* The next token should be an identifier.  */
8738   token = cp_lexer_peek_token (parser->lexer);
8739   if (token->type != CPP_NAME
8740       && token->type != CPP_KEYWORD)
8741     {
8742       cp_parser_error (parser, "expected labeled-statement");
8743       return;
8744     }
8745
8746   parser->colon_corrects_to_scope_p = false;
8747   switch (token->keyword)
8748     {
8749     case RID_CASE:
8750       {
8751         tree expr, expr_hi;
8752         cp_token *ellipsis;
8753
8754         /* Consume the `case' token.  */
8755         cp_lexer_consume_token (parser->lexer);
8756         /* Parse the constant-expression.  */
8757         expr = cp_parser_constant_expression (parser,
8758                                               /*allow_non_constant_p=*/false,
8759                                               NULL);
8760
8761         ellipsis = cp_lexer_peek_token (parser->lexer);
8762         if (ellipsis->type == CPP_ELLIPSIS)
8763           {
8764             /* Consume the `...' token.  */
8765             cp_lexer_consume_token (parser->lexer);
8766             expr_hi =
8767               cp_parser_constant_expression (parser,
8768                                              /*allow_non_constant_p=*/false,
8769                                              NULL);
8770             /* We don't need to emit warnings here, as the common code
8771                will do this for us.  */
8772           }
8773         else
8774           expr_hi = NULL_TREE;
8775
8776         if (parser->in_switch_statement_p)
8777           finish_case_label (token->location, expr, expr_hi);
8778         else
8779           error_at (token->location,
8780                     "case label %qE not within a switch statement",
8781                     expr);
8782       }
8783       break;
8784
8785     case RID_DEFAULT:
8786       /* Consume the `default' token.  */
8787       cp_lexer_consume_token (parser->lexer);
8788
8789       if (parser->in_switch_statement_p)
8790         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8791       else
8792         error_at (token->location, "case label not within a switch statement");
8793       break;
8794
8795     default:
8796       /* Anything else must be an ordinary label.  */
8797       label = finish_label_stmt (cp_parser_identifier (parser));
8798       break;
8799     }
8800
8801   /* Require the `:' token.  */
8802   cp_parser_require (parser, CPP_COLON, RT_COLON);
8803
8804   /* An ordinary label may optionally be followed by attributes.
8805      However, this is only permitted if the attributes are then
8806      followed by a semicolon.  This is because, for backward
8807      compatibility, when parsing
8808        lab: __attribute__ ((unused)) int i;
8809      we want the attribute to attach to "i", not "lab".  */
8810   if (label != NULL_TREE
8811       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8812     {
8813       tree attrs;
8814
8815       cp_parser_parse_tentatively (parser);
8816       attrs = cp_parser_attributes_opt (parser);
8817       if (attrs == NULL_TREE
8818           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8819         cp_parser_abort_tentative_parse (parser);
8820       else if (!cp_parser_parse_definitely (parser))
8821         ;
8822       else
8823         cplus_decl_attributes (&label, attrs, 0);
8824     }
8825
8826   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8827 }
8828
8829 /* Parse an expression-statement.
8830
8831    expression-statement:
8832      expression [opt] ;
8833
8834    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8835    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8836    indicates whether this expression-statement is part of an
8837    expression statement.  */
8838
8839 static tree
8840 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8841 {
8842   tree statement = NULL_TREE;
8843   cp_token *token = cp_lexer_peek_token (parser->lexer);
8844
8845   /* If the next token is a ';', then there is no expression
8846      statement.  */
8847   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8848     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8849
8850   /* Give a helpful message for "A<T>::type t;" and the like.  */
8851   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8852       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8853     {
8854       if (TREE_CODE (statement) == SCOPE_REF)
8855         error_at (token->location, "need %<typename%> before %qE because "
8856                   "%qT is a dependent scope",
8857                   statement, TREE_OPERAND (statement, 0));
8858       else if (is_overloaded_fn (statement)
8859                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8860         {
8861           /* A::A a; */
8862           tree fn = get_first_fn (statement);
8863           error_at (token->location,
8864                     "%<%T::%D%> names the constructor, not the type",
8865                     DECL_CONTEXT (fn), DECL_NAME (fn));
8866         }
8867     }
8868
8869   /* Consume the final `;'.  */
8870   cp_parser_consume_semicolon_at_end_of_statement (parser);
8871
8872   if (in_statement_expr
8873       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8874     /* This is the final expression statement of a statement
8875        expression.  */
8876     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8877   else if (statement)
8878     statement = finish_expr_stmt (statement);
8879   else
8880     finish_stmt ();
8881
8882   return statement;
8883 }
8884
8885 /* Parse a compound-statement.
8886
8887    compound-statement:
8888      { statement-seq [opt] }
8889
8890    GNU extension:
8891
8892    compound-statement:
8893      { label-declaration-seq [opt] statement-seq [opt] }
8894
8895    label-declaration-seq:
8896      label-declaration
8897      label-declaration-seq label-declaration
8898
8899    Returns a tree representing the statement.  */
8900
8901 static tree
8902 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8903                               bool in_try, bool function_body)
8904 {
8905   tree compound_stmt;
8906
8907   /* Consume the `{'.  */
8908   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8909     return error_mark_node;
8910   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8911       && !function_body)
8912     pedwarn (input_location, OPT_pedantic,
8913              "compound-statement in constexpr function");
8914   /* Begin the compound-statement.  */
8915   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8916   /* If the next keyword is `__label__' we have a label declaration.  */
8917   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8918     cp_parser_label_declaration (parser);
8919   /* Parse an (optional) statement-seq.  */
8920   cp_parser_statement_seq_opt (parser, in_statement_expr);
8921   /* Finish the compound-statement.  */
8922   finish_compound_stmt (compound_stmt);
8923   /* Consume the `}'.  */
8924   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8925
8926   return compound_stmt;
8927 }
8928
8929 /* Parse an (optional) statement-seq.
8930
8931    statement-seq:
8932      statement
8933      statement-seq [opt] statement  */
8934
8935 static void
8936 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8937 {
8938   /* Scan statements until there aren't any more.  */
8939   while (true)
8940     {
8941       cp_token *token = cp_lexer_peek_token (parser->lexer);
8942
8943       /* If we are looking at a `}', then we have run out of
8944          statements; the same is true if we have reached the end
8945          of file, or have stumbled upon a stray '@end'.  */
8946       if (token->type == CPP_CLOSE_BRACE
8947           || token->type == CPP_EOF
8948           || token->type == CPP_PRAGMA_EOL
8949           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8950         break;
8951       
8952       /* If we are in a compound statement and find 'else' then
8953          something went wrong.  */
8954       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8955         {
8956           if (parser->in_statement & IN_IF_STMT) 
8957             break;
8958           else
8959             {
8960               token = cp_lexer_consume_token (parser->lexer);
8961               error_at (token->location, "%<else%> without a previous %<if%>");
8962             }
8963         }
8964
8965       /* Parse the statement.  */
8966       cp_parser_statement (parser, in_statement_expr, true, NULL);
8967     }
8968 }
8969
8970 /* Parse a selection-statement.
8971
8972    selection-statement:
8973      if ( condition ) statement
8974      if ( condition ) statement else statement
8975      switch ( condition ) statement
8976
8977    Returns the new IF_STMT or SWITCH_STMT.
8978
8979    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8980    is a (possibly labeled) if statement which is not enclosed in
8981    braces and has an else clause.  This is used to implement
8982    -Wparentheses.  */
8983
8984 static tree
8985 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8986 {
8987   cp_token *token;
8988   enum rid keyword;
8989
8990   if (if_p != NULL)
8991     *if_p = false;
8992
8993   /* Peek at the next token.  */
8994   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8995
8996   /* See what kind of keyword it is.  */
8997   keyword = token->keyword;
8998   switch (keyword)
8999     {
9000     case RID_IF:
9001     case RID_SWITCH:
9002       {
9003         tree statement;
9004         tree condition;
9005
9006         /* Look for the `('.  */
9007         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9008           {
9009             cp_parser_skip_to_end_of_statement (parser);
9010             return error_mark_node;
9011           }
9012
9013         /* Begin the selection-statement.  */
9014         if (keyword == RID_IF)
9015           statement = begin_if_stmt ();
9016         else
9017           statement = begin_switch_stmt ();
9018
9019         /* Parse the condition.  */
9020         condition = cp_parser_condition (parser);
9021         /* Look for the `)'.  */
9022         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9023           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9024                                                  /*consume_paren=*/true);
9025
9026         if (keyword == RID_IF)
9027           {
9028             bool nested_if;
9029             unsigned char in_statement;
9030
9031             /* Add the condition.  */
9032             finish_if_stmt_cond (condition, statement);
9033
9034             /* Parse the then-clause.  */
9035             in_statement = parser->in_statement;
9036             parser->in_statement |= IN_IF_STMT;
9037             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9038               {
9039                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9040                 add_stmt (build_empty_stmt (loc));
9041                 cp_lexer_consume_token (parser->lexer);
9042                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9043                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9044                               "empty body in an %<if%> statement");
9045                 nested_if = false;
9046               }
9047             else
9048               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9049             parser->in_statement = in_statement;
9050
9051             finish_then_clause (statement);
9052
9053             /* If the next token is `else', parse the else-clause.  */
9054             if (cp_lexer_next_token_is_keyword (parser->lexer,
9055                                                 RID_ELSE))
9056               {
9057                 /* Consume the `else' keyword.  */
9058                 cp_lexer_consume_token (parser->lexer);
9059                 begin_else_clause (statement);
9060                 /* Parse the else-clause.  */
9061                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9062                   {
9063                     location_t loc;
9064                     loc = cp_lexer_peek_token (parser->lexer)->location;
9065                     warning_at (loc,
9066                                 OPT_Wempty_body, "suggest braces around "
9067                                 "empty body in an %<else%> statement");
9068                     add_stmt (build_empty_stmt (loc));
9069                     cp_lexer_consume_token (parser->lexer);
9070                   }
9071                 else
9072                   cp_parser_implicitly_scoped_statement (parser, NULL);
9073
9074                 finish_else_clause (statement);
9075
9076                 /* If we are currently parsing a then-clause, then
9077                    IF_P will not be NULL.  We set it to true to
9078                    indicate that this if statement has an else clause.
9079                    This may trigger the Wparentheses warning below
9080                    when we get back up to the parent if statement.  */
9081                 if (if_p != NULL)
9082                   *if_p = true;
9083               }
9084             else
9085               {
9086                 /* This if statement does not have an else clause.  If
9087                    NESTED_IF is true, then the then-clause is an if
9088                    statement which does have an else clause.  We warn
9089                    about the potential ambiguity.  */
9090                 if (nested_if)
9091                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9092                               "suggest explicit braces to avoid ambiguous"
9093                               " %<else%>");
9094               }
9095
9096             /* Now we're all done with the if-statement.  */
9097             finish_if_stmt (statement);
9098           }
9099         else
9100           {
9101             bool in_switch_statement_p;
9102             unsigned char in_statement;
9103
9104             /* Add the condition.  */
9105             finish_switch_cond (condition, statement);
9106
9107             /* Parse the body of the switch-statement.  */
9108             in_switch_statement_p = parser->in_switch_statement_p;
9109             in_statement = parser->in_statement;
9110             parser->in_switch_statement_p = true;
9111             parser->in_statement |= IN_SWITCH_STMT;
9112             cp_parser_implicitly_scoped_statement (parser, NULL);
9113             parser->in_switch_statement_p = in_switch_statement_p;
9114             parser->in_statement = in_statement;
9115
9116             /* Now we're all done with the switch-statement.  */
9117             finish_switch_stmt (statement);
9118           }
9119
9120         return statement;
9121       }
9122       break;
9123
9124     default:
9125       cp_parser_error (parser, "expected selection-statement");
9126       return error_mark_node;
9127     }
9128 }
9129
9130 /* Parse a condition.
9131
9132    condition:
9133      expression
9134      type-specifier-seq declarator = initializer-clause
9135      type-specifier-seq declarator braced-init-list
9136
9137    GNU Extension:
9138
9139    condition:
9140      type-specifier-seq declarator asm-specification [opt]
9141        attributes [opt] = assignment-expression
9142
9143    Returns the expression that should be tested.  */
9144
9145 static tree
9146 cp_parser_condition (cp_parser* parser)
9147 {
9148   cp_decl_specifier_seq type_specifiers;
9149   const char *saved_message;
9150   int declares_class_or_enum;
9151
9152   /* Try the declaration first.  */
9153   cp_parser_parse_tentatively (parser);
9154   /* New types are not allowed in the type-specifier-seq for a
9155      condition.  */
9156   saved_message = parser->type_definition_forbidden_message;
9157   parser->type_definition_forbidden_message
9158     = G_("types may not be defined in conditions");
9159   /* Parse the type-specifier-seq.  */
9160   cp_parser_decl_specifier_seq (parser,
9161                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9162                                 &type_specifiers,
9163                                 &declares_class_or_enum);
9164   /* Restore the saved message.  */
9165   parser->type_definition_forbidden_message = saved_message;
9166   /* If all is well, we might be looking at a declaration.  */
9167   if (!cp_parser_error_occurred (parser))
9168     {
9169       tree decl;
9170       tree asm_specification;
9171       tree attributes;
9172       cp_declarator *declarator;
9173       tree initializer = NULL_TREE;
9174
9175       /* Parse the declarator.  */
9176       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9177                                          /*ctor_dtor_or_conv_p=*/NULL,
9178                                          /*parenthesized_p=*/NULL,
9179                                          /*member_p=*/false);
9180       /* Parse the attributes.  */
9181       attributes = cp_parser_attributes_opt (parser);
9182       /* Parse the asm-specification.  */
9183       asm_specification = cp_parser_asm_specification_opt (parser);
9184       /* If the next token is not an `=' or '{', then we might still be
9185          looking at an expression.  For example:
9186
9187            if (A(a).x)
9188
9189          looks like a decl-specifier-seq and a declarator -- but then
9190          there is no `=', so this is an expression.  */
9191       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9192           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9193         cp_parser_simulate_error (parser);
9194         
9195       /* If we did see an `=' or '{', then we are looking at a declaration
9196          for sure.  */
9197       if (cp_parser_parse_definitely (parser))
9198         {
9199           tree pushed_scope;
9200           bool non_constant_p;
9201           bool flags = LOOKUP_ONLYCONVERTING;
9202
9203           /* Create the declaration.  */
9204           decl = start_decl (declarator, &type_specifiers,
9205                              /*initialized_p=*/true,
9206                              attributes, /*prefix_attributes=*/NULL_TREE,
9207                              &pushed_scope);
9208
9209           /* Parse the initializer.  */
9210           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9211             {
9212               initializer = cp_parser_braced_list (parser, &non_constant_p);
9213               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9214               flags = 0;
9215             }
9216           else
9217             {
9218               /* Consume the `='.  */
9219               cp_parser_require (parser, CPP_EQ, RT_EQ);
9220               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9221             }
9222           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9223             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9224
9225           /* Process the initializer.  */
9226           cp_finish_decl (decl,
9227                           initializer, !non_constant_p,
9228                           asm_specification,
9229                           flags);
9230
9231           if (pushed_scope)
9232             pop_scope (pushed_scope);
9233
9234           return convert_from_reference (decl);
9235         }
9236     }
9237   /* If we didn't even get past the declarator successfully, we are
9238      definitely not looking at a declaration.  */
9239   else
9240     cp_parser_abort_tentative_parse (parser);
9241
9242   /* Otherwise, we are looking at an expression.  */
9243   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9244 }
9245
9246 /* Parses a for-statement or range-for-statement until the closing ')',
9247    not included. */
9248
9249 static tree
9250 cp_parser_for (cp_parser *parser)
9251 {
9252   tree init, scope, decl;
9253   bool is_range_for;
9254
9255   /* Begin the for-statement.  */
9256   scope = begin_for_scope (&init);
9257
9258   /* Parse the initialization.  */
9259   is_range_for = cp_parser_for_init_statement (parser, &decl);
9260
9261   if (is_range_for)
9262     return cp_parser_range_for (parser, scope, init, decl);
9263   else
9264     return cp_parser_c_for (parser, scope, init);
9265 }
9266
9267 static tree
9268 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9269 {
9270   /* Normal for loop */
9271   tree condition = NULL_TREE;
9272   tree expression = NULL_TREE;
9273   tree stmt;
9274
9275   stmt = begin_for_stmt (scope, init);
9276   /* The for-init-statement has already been parsed in
9277      cp_parser_for_init_statement, so no work is needed here.  */
9278   finish_for_init_stmt (stmt);
9279
9280   /* If there's a condition, process it.  */
9281   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9282     condition = cp_parser_condition (parser);
9283   finish_for_cond (condition, stmt);
9284   /* Look for the `;'.  */
9285   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9286
9287   /* If there's an expression, process it.  */
9288   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9289     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9290   finish_for_expr (expression, stmt);
9291
9292   return stmt;
9293 }
9294
9295 /* Tries to parse a range-based for-statement:
9296
9297   range-based-for:
9298     decl-specifier-seq declarator : expression
9299
9300   The decl-specifier-seq declarator and the `:' are already parsed by
9301   cp_parser_for_init_statement. If processing_template_decl it returns a
9302   newly created RANGE_FOR_STMT; if not, it is converted to a
9303   regular FOR_STMT.  */
9304
9305 static tree
9306 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9307 {
9308   tree stmt, range_expr;
9309
9310   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9311     {
9312       bool expr_non_constant_p;
9313       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9314     }
9315   else
9316     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9317
9318   /* If in template, STMT is converted to a normal for-statement
9319      at instantiation. If not, it is done just ahead. */
9320   if (processing_template_decl)
9321     {
9322       if (check_for_bare_parameter_packs (range_expr))
9323         range_expr = error_mark_node;
9324       stmt = begin_range_for_stmt (scope, init);
9325       finish_range_for_decl (stmt, range_decl, range_expr);
9326       if (!type_dependent_expression_p (range_expr)
9327           /* do_auto_deduction doesn't mess with template init-lists.  */
9328           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9329         do_range_for_auto_deduction (range_decl, range_expr);
9330     }
9331   else
9332     {
9333       stmt = begin_for_stmt (scope, init);
9334       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9335     }
9336   return stmt;
9337 }
9338
9339 /* Subroutine of cp_convert_range_for: given the initializer expression,
9340    builds up the range temporary.  */
9341
9342 static tree
9343 build_range_temp (tree range_expr)
9344 {
9345   tree range_type, range_temp;
9346
9347   /* Find out the type deduced by the declaration
9348      `auto &&__range = range_expr'.  */
9349   range_type = cp_build_reference_type (make_auto (), true);
9350   range_type = do_auto_deduction (range_type, range_expr,
9351                                   type_uses_auto (range_type));
9352
9353   /* Create the __range variable.  */
9354   range_temp = build_decl (input_location, VAR_DECL,
9355                            get_identifier ("__for_range"), range_type);
9356   TREE_USED (range_temp) = 1;
9357   DECL_ARTIFICIAL (range_temp) = 1;
9358
9359   return range_temp;
9360 }
9361
9362 /* Used by cp_parser_range_for in template context: we aren't going to
9363    do a full conversion yet, but we still need to resolve auto in the
9364    type of the for-range-declaration if present.  This is basically
9365    a shortcut version of cp_convert_range_for.  */
9366
9367 static void
9368 do_range_for_auto_deduction (tree decl, tree range_expr)
9369 {
9370   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9371   if (auto_node)
9372     {
9373       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9374       range_temp = convert_from_reference (build_range_temp (range_expr));
9375       iter_type = (cp_parser_perform_range_for_lookup
9376                    (range_temp, &begin_dummy, &end_dummy));
9377       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9378       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9379                                         tf_warning_or_error);
9380       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9381                                             iter_decl, auto_node);
9382     }
9383 }
9384
9385 /* Converts a range-based for-statement into a normal
9386    for-statement, as per the definition.
9387
9388       for (RANGE_DECL : RANGE_EXPR)
9389         BLOCK
9390
9391    should be equivalent to:
9392
9393       {
9394         auto &&__range = RANGE_EXPR;
9395         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9396               __begin != __end;
9397               ++__begin)
9398           {
9399               RANGE_DECL = *__begin;
9400               BLOCK
9401           }
9402       }
9403
9404    If RANGE_EXPR is an array:
9405         BEGIN_EXPR = __range
9406         END_EXPR = __range + ARRAY_SIZE(__range)
9407    Else if RANGE_EXPR has a member 'begin' or 'end':
9408         BEGIN_EXPR = __range.begin()
9409         END_EXPR = __range.end()
9410    Else:
9411         BEGIN_EXPR = begin(__range)
9412         END_EXPR = end(__range);
9413
9414    If __range has a member 'begin' but not 'end', or vice versa, we must
9415    still use the second alternative (it will surely fail, however).
9416    When calling begin()/end() in the third alternative we must use
9417    argument dependent lookup, but always considering 'std' as an associated
9418    namespace.  */
9419
9420 tree
9421 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9422 {
9423   tree begin, end;
9424   tree iter_type, begin_expr, end_expr;
9425   tree condition, expression;
9426
9427   if (range_decl == error_mark_node || range_expr == error_mark_node)
9428     /* If an error happened previously do nothing or else a lot of
9429        unhelpful errors would be issued.  */
9430     begin_expr = end_expr = iter_type = error_mark_node;
9431   else
9432     {
9433       tree range_temp = build_range_temp (range_expr);
9434       pushdecl (range_temp);
9435       cp_finish_decl (range_temp, range_expr,
9436                       /*is_constant_init*/false, NULL_TREE,
9437                       LOOKUP_ONLYCONVERTING);
9438
9439       range_temp = convert_from_reference (range_temp);
9440       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9441                                                       &begin_expr, &end_expr);
9442     }
9443
9444   /* The new for initialization statement.  */
9445   begin = build_decl (input_location, VAR_DECL,
9446                       get_identifier ("__for_begin"), iter_type);
9447   TREE_USED (begin) = 1;
9448   DECL_ARTIFICIAL (begin) = 1;
9449   pushdecl (begin);
9450   cp_finish_decl (begin, begin_expr,
9451                   /*is_constant_init*/false, NULL_TREE,
9452                   LOOKUP_ONLYCONVERTING);
9453
9454   end = build_decl (input_location, VAR_DECL,
9455                     get_identifier ("__for_end"), iter_type);
9456   TREE_USED (end) = 1;
9457   DECL_ARTIFICIAL (end) = 1;
9458   pushdecl (end);
9459   cp_finish_decl (end, end_expr,
9460                   /*is_constant_init*/false, NULL_TREE,
9461                   LOOKUP_ONLYCONVERTING);
9462
9463   finish_for_init_stmt (statement);
9464
9465   /* The new for condition.  */
9466   condition = build_x_binary_op (NE_EXPR,
9467                                  begin, ERROR_MARK,
9468                                  end, ERROR_MARK,
9469                                  NULL, tf_warning_or_error);
9470   finish_for_cond (condition, statement);
9471
9472   /* The new increment expression.  */
9473   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9474   finish_for_expr (expression, statement);
9475
9476   /* The declaration is initialized with *__begin inside the loop body.  */
9477   cp_finish_decl (range_decl,
9478                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9479                   /*is_constant_init*/false, NULL_TREE,
9480                   LOOKUP_ONLYCONVERTING);
9481
9482   return statement;
9483 }
9484
9485 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9486    We need to solve both at the same time because the method used
9487    depends on the existence of members begin or end.
9488    Returns the type deduced for the iterator expression.  */
9489
9490 static tree
9491 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9492 {
9493   if (error_operand_p (range))
9494     {
9495       *begin = *end = error_mark_node;
9496       return error_mark_node;
9497     }
9498
9499   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9500     {
9501       error ("range-based %<for%> expression of type %qT "
9502              "has incomplete type", TREE_TYPE (range));
9503       *begin = *end = error_mark_node;
9504       return error_mark_node;
9505     }
9506   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9507     {
9508       /* If RANGE is an array, we will use pointer arithmetic.  */
9509       *begin = range;
9510       *end = build_binary_op (input_location, PLUS_EXPR,
9511                               range,
9512                               array_type_nelts_top (TREE_TYPE (range)),
9513                               0);
9514       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9515     }
9516   else
9517     {
9518       /* If it is not an array, we must do a bit of magic.  */
9519       tree id_begin, id_end;
9520       tree member_begin, member_end;
9521
9522       *begin = *end = error_mark_node;
9523
9524       id_begin = get_identifier ("begin");
9525       id_end = get_identifier ("end");
9526       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9527                                     /*protect=*/2, /*want_type=*/false,
9528                                     tf_warning_or_error);
9529       member_end = lookup_member (TREE_TYPE (range), id_end,
9530                                   /*protect=*/2, /*want_type=*/false,
9531                                   tf_warning_or_error);
9532
9533       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9534         {
9535           /* Use the member functions.  */
9536           if (member_begin != NULL_TREE)
9537             *begin = cp_parser_range_for_member_function (range, id_begin);
9538           else
9539             error ("range-based %<for%> expression of type %qT has an "
9540                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9541
9542           if (member_end != NULL_TREE)
9543             *end = cp_parser_range_for_member_function (range, id_end);
9544           else
9545             error ("range-based %<for%> expression of type %qT has a "
9546                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9547         }
9548       else
9549         {
9550           /* Use global functions with ADL.  */
9551           VEC(tree,gc) *vec;
9552           vec = make_tree_vector ();
9553
9554           VEC_safe_push (tree, gc, vec, range);
9555
9556           member_begin = perform_koenig_lookup (id_begin, vec,
9557                                                 /*include_std=*/true,
9558                                                 tf_warning_or_error);
9559           *begin = finish_call_expr (member_begin, &vec, false, true,
9560                                      tf_warning_or_error);
9561           member_end = perform_koenig_lookup (id_end, vec,
9562                                               /*include_std=*/true,
9563                                               tf_warning_or_error);
9564           *end = finish_call_expr (member_end, &vec, false, true,
9565                                    tf_warning_or_error);
9566
9567           release_tree_vector (vec);
9568         }
9569
9570       /* Last common checks.  */
9571       if (*begin == error_mark_node || *end == error_mark_node)
9572         {
9573           /* If one of the expressions is an error do no more checks.  */
9574           *begin = *end = error_mark_node;
9575           return error_mark_node;
9576         }
9577       else
9578         {
9579           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9580           /* The unqualified type of the __begin and __end temporaries should
9581              be the same, as required by the multiple auto declaration.  */
9582           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9583             error ("inconsistent begin/end types in range-based %<for%> "
9584                    "statement: %qT and %qT",
9585                    TREE_TYPE (*begin), TREE_TYPE (*end));
9586           return iter_type;
9587         }
9588     }
9589 }
9590
9591 /* Helper function for cp_parser_perform_range_for_lookup.
9592    Builds a tree for RANGE.IDENTIFIER().  */
9593
9594 static tree
9595 cp_parser_range_for_member_function (tree range, tree identifier)
9596 {
9597   tree member, res;
9598   VEC(tree,gc) *vec;
9599
9600   member = finish_class_member_access_expr (range, identifier,
9601                                             false, tf_warning_or_error);
9602   if (member == error_mark_node)
9603     return error_mark_node;
9604
9605   vec = make_tree_vector ();
9606   res = finish_call_expr (member, &vec,
9607                           /*disallow_virtual=*/false,
9608                           /*koenig_p=*/false,
9609                           tf_warning_or_error);
9610   release_tree_vector (vec);
9611   return res;
9612 }
9613
9614 /* Parse an iteration-statement.
9615
9616    iteration-statement:
9617      while ( condition ) statement
9618      do statement while ( expression ) ;
9619      for ( for-init-statement condition [opt] ; expression [opt] )
9620        statement
9621
9622    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9623
9624 static tree
9625 cp_parser_iteration_statement (cp_parser* parser)
9626 {
9627   cp_token *token;
9628   enum rid keyword;
9629   tree statement;
9630   unsigned char in_statement;
9631
9632   /* Peek at the next token.  */
9633   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9634   if (!token)
9635     return error_mark_node;
9636
9637   /* Remember whether or not we are already within an iteration
9638      statement.  */
9639   in_statement = parser->in_statement;
9640
9641   /* See what kind of keyword it is.  */
9642   keyword = token->keyword;
9643   switch (keyword)
9644     {
9645     case RID_WHILE:
9646       {
9647         tree condition;
9648
9649         /* Begin the while-statement.  */
9650         statement = begin_while_stmt ();
9651         /* Look for the `('.  */
9652         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9653         /* Parse the condition.  */
9654         condition = cp_parser_condition (parser);
9655         finish_while_stmt_cond (condition, statement);
9656         /* Look for the `)'.  */
9657         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9658         /* Parse the dependent statement.  */
9659         parser->in_statement = IN_ITERATION_STMT;
9660         cp_parser_already_scoped_statement (parser);
9661         parser->in_statement = in_statement;
9662         /* We're done with the while-statement.  */
9663         finish_while_stmt (statement);
9664       }
9665       break;
9666
9667     case RID_DO:
9668       {
9669         tree expression;
9670
9671         /* Begin the do-statement.  */
9672         statement = begin_do_stmt ();
9673         /* Parse the body of the do-statement.  */
9674         parser->in_statement = IN_ITERATION_STMT;
9675         cp_parser_implicitly_scoped_statement (parser, NULL);
9676         parser->in_statement = in_statement;
9677         finish_do_body (statement);
9678         /* Look for the `while' keyword.  */
9679         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9680         /* Look for the `('.  */
9681         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9682         /* Parse the expression.  */
9683         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9684         /* We're done with the do-statement.  */
9685         finish_do_stmt (expression, statement);
9686         /* Look for the `)'.  */
9687         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9688         /* Look for the `;'.  */
9689         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9690       }
9691       break;
9692
9693     case RID_FOR:
9694       {
9695         /* Look for the `('.  */
9696         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9697
9698         statement = cp_parser_for (parser);
9699
9700         /* Look for the `)'.  */
9701         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9702
9703         /* Parse the body of the for-statement.  */
9704         parser->in_statement = IN_ITERATION_STMT;
9705         cp_parser_already_scoped_statement (parser);
9706         parser->in_statement = in_statement;
9707
9708         /* We're done with the for-statement.  */
9709         finish_for_stmt (statement);
9710       }
9711       break;
9712
9713     default:
9714       cp_parser_error (parser, "expected iteration-statement");
9715       statement = error_mark_node;
9716       break;
9717     }
9718
9719   return statement;
9720 }
9721
9722 /* Parse a for-init-statement or the declarator of a range-based-for.
9723    Returns true if a range-based-for declaration is seen.
9724
9725    for-init-statement:
9726      expression-statement
9727      simple-declaration  */
9728
9729 static bool
9730 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9731 {
9732   /* If the next token is a `;', then we have an empty
9733      expression-statement.  Grammatically, this is also a
9734      simple-declaration, but an invalid one, because it does not
9735      declare anything.  Therefore, if we did not handle this case
9736      specially, we would issue an error message about an invalid
9737      declaration.  */
9738   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9739     {
9740       bool is_range_for = false;
9741       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9742
9743       parser->colon_corrects_to_scope_p = false;
9744
9745       /* We're going to speculatively look for a declaration, falling back
9746          to an expression, if necessary.  */
9747       cp_parser_parse_tentatively (parser);
9748       /* Parse the declaration.  */
9749       cp_parser_simple_declaration (parser,
9750                                     /*function_definition_allowed_p=*/false,
9751                                     decl);
9752       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9753       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9754         {
9755           /* It is a range-for, consume the ':' */
9756           cp_lexer_consume_token (parser->lexer);
9757           is_range_for = true;
9758           if (cxx_dialect < cxx0x)
9759             {
9760               error_at (cp_lexer_peek_token (parser->lexer)->location,
9761                         "range-based %<for%> loops are not allowed "
9762                         "in C++98 mode");
9763               *decl = error_mark_node;
9764             }
9765         }
9766       else
9767           /* The ';' is not consumed yet because we told
9768              cp_parser_simple_declaration not to.  */
9769           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9770
9771       if (cp_parser_parse_definitely (parser))
9772         return is_range_for;
9773       /* If the tentative parse failed, then we shall need to look for an
9774          expression-statement.  */
9775     }
9776   /* If we are here, it is an expression-statement.  */
9777   cp_parser_expression_statement (parser, NULL_TREE);
9778   return false;
9779 }
9780
9781 /* Parse a jump-statement.
9782
9783    jump-statement:
9784      break ;
9785      continue ;
9786      return expression [opt] ;
9787      return braced-init-list ;
9788      goto identifier ;
9789
9790    GNU extension:
9791
9792    jump-statement:
9793      goto * expression ;
9794
9795    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9796
9797 static tree
9798 cp_parser_jump_statement (cp_parser* parser)
9799 {
9800   tree statement = error_mark_node;
9801   cp_token *token;
9802   enum rid keyword;
9803   unsigned char in_statement;
9804
9805   /* Peek at the next token.  */
9806   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9807   if (!token)
9808     return error_mark_node;
9809
9810   /* See what kind of keyword it is.  */
9811   keyword = token->keyword;
9812   switch (keyword)
9813     {
9814     case RID_BREAK:
9815       in_statement = parser->in_statement & ~IN_IF_STMT;      
9816       switch (in_statement)
9817         {
9818         case 0:
9819           error_at (token->location, "break statement not within loop or switch");
9820           break;
9821         default:
9822           gcc_assert ((in_statement & IN_SWITCH_STMT)
9823                       || in_statement == IN_ITERATION_STMT);
9824           statement = finish_break_stmt ();
9825           break;
9826         case IN_OMP_BLOCK:
9827           error_at (token->location, "invalid exit from OpenMP structured block");
9828           break;
9829         case IN_OMP_FOR:
9830           error_at (token->location, "break statement used with OpenMP for loop");
9831           break;
9832         }
9833       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9834       break;
9835
9836     case RID_CONTINUE:
9837       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9838         {
9839         case 0:
9840           error_at (token->location, "continue statement not within a loop");
9841           break;
9842         case IN_ITERATION_STMT:
9843         case IN_OMP_FOR:
9844           statement = finish_continue_stmt ();
9845           break;
9846         case IN_OMP_BLOCK:
9847           error_at (token->location, "invalid exit from OpenMP structured block");
9848           break;
9849         default:
9850           gcc_unreachable ();
9851         }
9852       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9853       break;
9854
9855     case RID_RETURN:
9856       {
9857         tree expr;
9858         bool expr_non_constant_p;
9859
9860         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9861           {
9862             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9863             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9864           }
9865         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9866           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9867         else
9868           /* If the next token is a `;', then there is no
9869              expression.  */
9870           expr = NULL_TREE;
9871         /* Build the return-statement.  */
9872         statement = finish_return_stmt (expr);
9873         /* Look for the final `;'.  */
9874         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9875       }
9876       break;
9877
9878     case RID_GOTO:
9879       /* Create the goto-statement.  */
9880       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9881         {
9882           /* Issue a warning about this use of a GNU extension.  */
9883           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9884           /* Consume the '*' token.  */
9885           cp_lexer_consume_token (parser->lexer);
9886           /* Parse the dependent expression.  */
9887           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9888         }
9889       else
9890         finish_goto_stmt (cp_parser_identifier (parser));
9891       /* Look for the final `;'.  */
9892       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9893       break;
9894
9895     default:
9896       cp_parser_error (parser, "expected jump-statement");
9897       break;
9898     }
9899
9900   return statement;
9901 }
9902
9903 /* Parse a declaration-statement.
9904
9905    declaration-statement:
9906      block-declaration  */
9907
9908 static void
9909 cp_parser_declaration_statement (cp_parser* parser)
9910 {
9911   void *p;
9912
9913   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9914   p = obstack_alloc (&declarator_obstack, 0);
9915
9916  /* Parse the block-declaration.  */
9917   cp_parser_block_declaration (parser, /*statement_p=*/true);
9918
9919   /* Free any declarators allocated.  */
9920   obstack_free (&declarator_obstack, p);
9921
9922   /* Finish off the statement.  */
9923   finish_stmt ();
9924 }
9925
9926 /* Some dependent statements (like `if (cond) statement'), are
9927    implicitly in their own scope.  In other words, if the statement is
9928    a single statement (as opposed to a compound-statement), it is
9929    none-the-less treated as if it were enclosed in braces.  Any
9930    declarations appearing in the dependent statement are out of scope
9931    after control passes that point.  This function parses a statement,
9932    but ensures that is in its own scope, even if it is not a
9933    compound-statement.
9934
9935    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9936    is a (possibly labeled) if statement which is not enclosed in
9937    braces and has an else clause.  This is used to implement
9938    -Wparentheses.
9939
9940    Returns the new statement.  */
9941
9942 static tree
9943 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9944 {
9945   tree statement;
9946
9947   if (if_p != NULL)
9948     *if_p = false;
9949
9950   /* Mark if () ; with a special NOP_EXPR.  */
9951   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9952     {
9953       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9954       cp_lexer_consume_token (parser->lexer);
9955       statement = add_stmt (build_empty_stmt (loc));
9956     }
9957   /* if a compound is opened, we simply parse the statement directly.  */
9958   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9959     statement = cp_parser_compound_statement (parser, NULL, false, false);
9960   /* If the token is not a `{', then we must take special action.  */
9961   else
9962     {
9963       /* Create a compound-statement.  */
9964       statement = begin_compound_stmt (0);
9965       /* Parse the dependent-statement.  */
9966       cp_parser_statement (parser, NULL_TREE, false, if_p);
9967       /* Finish the dummy compound-statement.  */
9968       finish_compound_stmt (statement);
9969     }
9970
9971   /* Return the statement.  */
9972   return statement;
9973 }
9974
9975 /* For some dependent statements (like `while (cond) statement'), we
9976    have already created a scope.  Therefore, even if the dependent
9977    statement is a compound-statement, we do not want to create another
9978    scope.  */
9979
9980 static void
9981 cp_parser_already_scoped_statement (cp_parser* parser)
9982 {
9983   /* If the token is a `{', then we must take special action.  */
9984   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9985     cp_parser_statement (parser, NULL_TREE, false, NULL);
9986   else
9987     {
9988       /* Avoid calling cp_parser_compound_statement, so that we
9989          don't create a new scope.  Do everything else by hand.  */
9990       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9991       /* If the next keyword is `__label__' we have a label declaration.  */
9992       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9993         cp_parser_label_declaration (parser);
9994       /* Parse an (optional) statement-seq.  */
9995       cp_parser_statement_seq_opt (parser, NULL_TREE);
9996       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9997     }
9998 }
9999
10000 /* Declarations [gram.dcl.dcl] */
10001
10002 /* Parse an optional declaration-sequence.
10003
10004    declaration-seq:
10005      declaration
10006      declaration-seq declaration  */
10007
10008 static void
10009 cp_parser_declaration_seq_opt (cp_parser* parser)
10010 {
10011   while (true)
10012     {
10013       cp_token *token;
10014
10015       token = cp_lexer_peek_token (parser->lexer);
10016
10017       if (token->type == CPP_CLOSE_BRACE
10018           || token->type == CPP_EOF
10019           || token->type == CPP_PRAGMA_EOL)
10020         break;
10021
10022       if (token->type == CPP_SEMICOLON)
10023         {
10024           /* A declaration consisting of a single semicolon is
10025              invalid.  Allow it unless we're being pedantic.  */
10026           cp_lexer_consume_token (parser->lexer);
10027           if (!in_system_header)
10028             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10029           continue;
10030         }
10031
10032       /* If we're entering or exiting a region that's implicitly
10033          extern "C", modify the lang context appropriately.  */
10034       if (!parser->implicit_extern_c && token->implicit_extern_c)
10035         {
10036           push_lang_context (lang_name_c);
10037           parser->implicit_extern_c = true;
10038         }
10039       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10040         {
10041           pop_lang_context ();
10042           parser->implicit_extern_c = false;
10043         }
10044
10045       if (token->type == CPP_PRAGMA)
10046         {
10047           /* A top-level declaration can consist solely of a #pragma.
10048              A nested declaration cannot, so this is done here and not
10049              in cp_parser_declaration.  (A #pragma at block scope is
10050              handled in cp_parser_statement.)  */
10051           cp_parser_pragma (parser, pragma_external);
10052           continue;
10053         }
10054
10055       /* Parse the declaration itself.  */
10056       cp_parser_declaration (parser);
10057     }
10058 }
10059
10060 /* Parse a declaration.
10061
10062    declaration:
10063      block-declaration
10064      function-definition
10065      template-declaration
10066      explicit-instantiation
10067      explicit-specialization
10068      linkage-specification
10069      namespace-definition
10070
10071    GNU extension:
10072
10073    declaration:
10074       __extension__ declaration */
10075
10076 static void
10077 cp_parser_declaration (cp_parser* parser)
10078 {
10079   cp_token token1;
10080   cp_token token2;
10081   int saved_pedantic;
10082   void *p;
10083   tree attributes = NULL_TREE;
10084
10085   /* Check for the `__extension__' keyword.  */
10086   if (cp_parser_extension_opt (parser, &saved_pedantic))
10087     {
10088       /* Parse the qualified declaration.  */
10089       cp_parser_declaration (parser);
10090       /* Restore the PEDANTIC flag.  */
10091       pedantic = saved_pedantic;
10092
10093       return;
10094     }
10095
10096   /* Try to figure out what kind of declaration is present.  */
10097   token1 = *cp_lexer_peek_token (parser->lexer);
10098
10099   if (token1.type != CPP_EOF)
10100     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10101   else
10102     {
10103       token2.type = CPP_EOF;
10104       token2.keyword = RID_MAX;
10105     }
10106
10107   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10108   p = obstack_alloc (&declarator_obstack, 0);
10109
10110   /* If the next token is `extern' and the following token is a string
10111      literal, then we have a linkage specification.  */
10112   if (token1.keyword == RID_EXTERN
10113       && cp_parser_is_pure_string_literal (&token2))
10114     cp_parser_linkage_specification (parser);
10115   /* If the next token is `template', then we have either a template
10116      declaration, an explicit instantiation, or an explicit
10117      specialization.  */
10118   else if (token1.keyword == RID_TEMPLATE)
10119     {
10120       /* `template <>' indicates a template specialization.  */
10121       if (token2.type == CPP_LESS
10122           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10123         cp_parser_explicit_specialization (parser);
10124       /* `template <' indicates a template declaration.  */
10125       else if (token2.type == CPP_LESS)
10126         cp_parser_template_declaration (parser, /*member_p=*/false);
10127       /* Anything else must be an explicit instantiation.  */
10128       else
10129         cp_parser_explicit_instantiation (parser);
10130     }
10131   /* If the next token is `export', then we have a template
10132      declaration.  */
10133   else if (token1.keyword == RID_EXPORT)
10134     cp_parser_template_declaration (parser, /*member_p=*/false);
10135   /* If the next token is `extern', 'static' or 'inline' and the one
10136      after that is `template', we have a GNU extended explicit
10137      instantiation directive.  */
10138   else if (cp_parser_allow_gnu_extensions_p (parser)
10139            && (token1.keyword == RID_EXTERN
10140                || token1.keyword == RID_STATIC
10141                || token1.keyword == RID_INLINE)
10142            && token2.keyword == RID_TEMPLATE)
10143     cp_parser_explicit_instantiation (parser);
10144   /* If the next token is `namespace', check for a named or unnamed
10145      namespace definition.  */
10146   else if (token1.keyword == RID_NAMESPACE
10147            && (/* A named namespace definition.  */
10148                (token2.type == CPP_NAME
10149                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10150                     != CPP_EQ))
10151                /* An unnamed namespace definition.  */
10152                || token2.type == CPP_OPEN_BRACE
10153                || token2.keyword == RID_ATTRIBUTE))
10154     cp_parser_namespace_definition (parser);
10155   /* An inline (associated) namespace definition.  */
10156   else if (token1.keyword == RID_INLINE
10157            && token2.keyword == RID_NAMESPACE)
10158     cp_parser_namespace_definition (parser);
10159   /* Objective-C++ declaration/definition.  */
10160   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10161     cp_parser_objc_declaration (parser, NULL_TREE);
10162   else if (c_dialect_objc ()
10163            && token1.keyword == RID_ATTRIBUTE
10164            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10165     cp_parser_objc_declaration (parser, attributes);
10166   /* We must have either a block declaration or a function
10167      definition.  */
10168   else
10169     /* Try to parse a block-declaration, or a function-definition.  */
10170     cp_parser_block_declaration (parser, /*statement_p=*/false);
10171
10172   /* Free any declarators allocated.  */
10173   obstack_free (&declarator_obstack, p);
10174 }
10175
10176 /* Parse a block-declaration.
10177
10178    block-declaration:
10179      simple-declaration
10180      asm-definition
10181      namespace-alias-definition
10182      using-declaration
10183      using-directive
10184
10185    GNU Extension:
10186
10187    block-declaration:
10188      __extension__ block-declaration
10189
10190    C++0x Extension:
10191
10192    block-declaration:
10193      static_assert-declaration
10194
10195    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10196    part of a declaration-statement.  */
10197
10198 static void
10199 cp_parser_block_declaration (cp_parser *parser,
10200                              bool      statement_p)
10201 {
10202   cp_token *token1;
10203   int saved_pedantic;
10204
10205   /* Check for the `__extension__' keyword.  */
10206   if (cp_parser_extension_opt (parser, &saved_pedantic))
10207     {
10208       /* Parse the qualified declaration.  */
10209       cp_parser_block_declaration (parser, statement_p);
10210       /* Restore the PEDANTIC flag.  */
10211       pedantic = saved_pedantic;
10212
10213       return;
10214     }
10215
10216   /* Peek at the next token to figure out which kind of declaration is
10217      present.  */
10218   token1 = cp_lexer_peek_token (parser->lexer);
10219
10220   /* If the next keyword is `asm', we have an asm-definition.  */
10221   if (token1->keyword == RID_ASM)
10222     {
10223       if (statement_p)
10224         cp_parser_commit_to_tentative_parse (parser);
10225       cp_parser_asm_definition (parser);
10226     }
10227   /* If the next keyword is `namespace', we have a
10228      namespace-alias-definition.  */
10229   else if (token1->keyword == RID_NAMESPACE)
10230     cp_parser_namespace_alias_definition (parser);
10231   /* If the next keyword is `using', we have a
10232      using-declaration, a using-directive, or an alias-declaration.  */
10233   else if (token1->keyword == RID_USING)
10234     {
10235       cp_token *token2;
10236
10237       if (statement_p)
10238         cp_parser_commit_to_tentative_parse (parser);
10239       /* If the token after `using' is `namespace', then we have a
10240          using-directive.  */
10241       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10242       if (token2->keyword == RID_NAMESPACE)
10243         cp_parser_using_directive (parser);
10244       /* If the second token after 'using' is '=', then we have an
10245          alias-declaration.  */
10246       else if (cxx_dialect >= cxx0x
10247                && token2->type == CPP_NAME
10248                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10249                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10250                        == RID_ATTRIBUTE)))
10251         cp_parser_alias_declaration (parser);
10252       /* Otherwise, it's a using-declaration.  */
10253       else
10254         cp_parser_using_declaration (parser,
10255                                      /*access_declaration_p=*/false);
10256     }
10257   /* If the next keyword is `__label__' we have a misplaced label
10258      declaration.  */
10259   else if (token1->keyword == RID_LABEL)
10260     {
10261       cp_lexer_consume_token (parser->lexer);
10262       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10263       cp_parser_skip_to_end_of_statement (parser);
10264       /* If the next token is now a `;', consume it.  */
10265       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10266         cp_lexer_consume_token (parser->lexer);
10267     }
10268   /* If the next token is `static_assert' we have a static assertion.  */
10269   else if (token1->keyword == RID_STATIC_ASSERT)
10270     cp_parser_static_assert (parser, /*member_p=*/false);
10271   /* Anything else must be a simple-declaration.  */
10272   else
10273     cp_parser_simple_declaration (parser, !statement_p,
10274                                   /*maybe_range_for_decl*/NULL);
10275 }
10276
10277 /* Parse a simple-declaration.
10278
10279    simple-declaration:
10280      decl-specifier-seq [opt] init-declarator-list [opt] ;
10281
10282    init-declarator-list:
10283      init-declarator
10284      init-declarator-list , init-declarator
10285
10286    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10287    function-definition as a simple-declaration.
10288
10289    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10290    parsed declaration if it is an uninitialized single declarator not followed
10291    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10292    if present, will not be consumed.  */
10293
10294 static void
10295 cp_parser_simple_declaration (cp_parser* parser,
10296                               bool function_definition_allowed_p,
10297                               tree *maybe_range_for_decl)
10298 {
10299   cp_decl_specifier_seq decl_specifiers;
10300   int declares_class_or_enum;
10301   bool saw_declarator;
10302
10303   if (maybe_range_for_decl)
10304     *maybe_range_for_decl = NULL_TREE;
10305
10306   /* Defer access checks until we know what is being declared; the
10307      checks for names appearing in the decl-specifier-seq should be
10308      done as if we were in the scope of the thing being declared.  */
10309   push_deferring_access_checks (dk_deferred);
10310
10311   /* Parse the decl-specifier-seq.  We have to keep track of whether
10312      or not the decl-specifier-seq declares a named class or
10313      enumeration type, since that is the only case in which the
10314      init-declarator-list is allowed to be empty.
10315
10316      [dcl.dcl]
10317
10318      In a simple-declaration, the optional init-declarator-list can be
10319      omitted only when declaring a class or enumeration, that is when
10320      the decl-specifier-seq contains either a class-specifier, an
10321      elaborated-type-specifier, or an enum-specifier.  */
10322   cp_parser_decl_specifier_seq (parser,
10323                                 CP_PARSER_FLAGS_OPTIONAL,
10324                                 &decl_specifiers,
10325                                 &declares_class_or_enum);
10326   /* We no longer need to defer access checks.  */
10327   stop_deferring_access_checks ();
10328
10329   /* In a block scope, a valid declaration must always have a
10330      decl-specifier-seq.  By not trying to parse declarators, we can
10331      resolve the declaration/expression ambiguity more quickly.  */
10332   if (!function_definition_allowed_p
10333       && !decl_specifiers.any_specifiers_p)
10334     {
10335       cp_parser_error (parser, "expected declaration");
10336       goto done;
10337     }
10338
10339   /* If the next two tokens are both identifiers, the code is
10340      erroneous. The usual cause of this situation is code like:
10341
10342        T t;
10343
10344      where "T" should name a type -- but does not.  */
10345   if (!decl_specifiers.any_type_specifiers_p
10346       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10347     {
10348       /* If parsing tentatively, we should commit; we really are
10349          looking at a declaration.  */
10350       cp_parser_commit_to_tentative_parse (parser);
10351       /* Give up.  */
10352       goto done;
10353     }
10354
10355   /* If we have seen at least one decl-specifier, and the next token
10356      is not a parenthesis, then we must be looking at a declaration.
10357      (After "int (" we might be looking at a functional cast.)  */
10358   if (decl_specifiers.any_specifiers_p
10359       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10360       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10361       && !cp_parser_error_occurred (parser))
10362     cp_parser_commit_to_tentative_parse (parser);
10363
10364   /* Keep going until we hit the `;' at the end of the simple
10365      declaration.  */
10366   saw_declarator = false;
10367   while (cp_lexer_next_token_is_not (parser->lexer,
10368                                      CPP_SEMICOLON))
10369     {
10370       cp_token *token;
10371       bool function_definition_p;
10372       tree decl;
10373
10374       if (saw_declarator)
10375         {
10376           /* If we are processing next declarator, coma is expected */
10377           token = cp_lexer_peek_token (parser->lexer);
10378           gcc_assert (token->type == CPP_COMMA);
10379           cp_lexer_consume_token (parser->lexer);
10380           if (maybe_range_for_decl)
10381             *maybe_range_for_decl = error_mark_node;
10382         }
10383       else
10384         saw_declarator = true;
10385
10386       /* Parse the init-declarator.  */
10387       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10388                                         /*checks=*/NULL,
10389                                         function_definition_allowed_p,
10390                                         /*member_p=*/false,
10391                                         declares_class_or_enum,
10392                                         &function_definition_p,
10393                                         maybe_range_for_decl);
10394       /* If an error occurred while parsing tentatively, exit quickly.
10395          (That usually happens when in the body of a function; each
10396          statement is treated as a declaration-statement until proven
10397          otherwise.)  */
10398       if (cp_parser_error_occurred (parser))
10399         goto done;
10400       /* Handle function definitions specially.  */
10401       if (function_definition_p)
10402         {
10403           /* If the next token is a `,', then we are probably
10404              processing something like:
10405
10406                void f() {}, *p;
10407
10408              which is erroneous.  */
10409           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10410             {
10411               cp_token *token = cp_lexer_peek_token (parser->lexer);
10412               error_at (token->location,
10413                         "mixing"
10414                         " declarations and function-definitions is forbidden");
10415             }
10416           /* Otherwise, we're done with the list of declarators.  */
10417           else
10418             {
10419               pop_deferring_access_checks ();
10420               return;
10421             }
10422         }
10423       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10424         *maybe_range_for_decl = decl;
10425       /* The next token should be either a `,' or a `;'.  */
10426       token = cp_lexer_peek_token (parser->lexer);
10427       /* If it's a `,', there are more declarators to come.  */
10428       if (token->type == CPP_COMMA)
10429         /* will be consumed next time around */;
10430       /* If it's a `;', we are done.  */
10431       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10432         break;
10433       /* Anything else is an error.  */
10434       else
10435         {
10436           /* If we have already issued an error message we don't need
10437              to issue another one.  */
10438           if (decl != error_mark_node
10439               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10440             cp_parser_error (parser, "expected %<,%> or %<;%>");
10441           /* Skip tokens until we reach the end of the statement.  */
10442           cp_parser_skip_to_end_of_statement (parser);
10443           /* If the next token is now a `;', consume it.  */
10444           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10445             cp_lexer_consume_token (parser->lexer);
10446           goto done;
10447         }
10448       /* After the first time around, a function-definition is not
10449          allowed -- even if it was OK at first.  For example:
10450
10451            int i, f() {}
10452
10453          is not valid.  */
10454       function_definition_allowed_p = false;
10455     }
10456
10457   /* Issue an error message if no declarators are present, and the
10458      decl-specifier-seq does not itself declare a class or
10459      enumeration.  */
10460   if (!saw_declarator)
10461     {
10462       if (cp_parser_declares_only_class_p (parser))
10463         shadow_tag (&decl_specifiers);
10464       /* Perform any deferred access checks.  */
10465       perform_deferred_access_checks ();
10466     }
10467
10468   /* Consume the `;'.  */
10469   if (!maybe_range_for_decl)
10470       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10471
10472  done:
10473   pop_deferring_access_checks ();
10474 }
10475
10476 /* Parse a decl-specifier-seq.
10477
10478    decl-specifier-seq:
10479      decl-specifier-seq [opt] decl-specifier
10480
10481    decl-specifier:
10482      storage-class-specifier
10483      type-specifier
10484      function-specifier
10485      friend
10486      typedef
10487
10488    GNU Extension:
10489
10490    decl-specifier:
10491      attributes
10492
10493    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10494
10495    The parser flags FLAGS is used to control type-specifier parsing.
10496
10497    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10498    flags:
10499
10500      1: one of the decl-specifiers is an elaborated-type-specifier
10501         (i.e., a type declaration)
10502      2: one of the decl-specifiers is an enum-specifier or a
10503         class-specifier (i.e., a type definition)
10504
10505    */
10506
10507 static void
10508 cp_parser_decl_specifier_seq (cp_parser* parser,
10509                               cp_parser_flags flags,
10510                               cp_decl_specifier_seq *decl_specs,
10511                               int* declares_class_or_enum)
10512 {
10513   bool constructor_possible_p = !parser->in_declarator_p;
10514   cp_token *start_token = NULL;
10515
10516   /* Clear DECL_SPECS.  */
10517   clear_decl_specs (decl_specs);
10518
10519   /* Assume no class or enumeration type is declared.  */
10520   *declares_class_or_enum = 0;
10521
10522   /* Keep reading specifiers until there are no more to read.  */
10523   while (true)
10524     {
10525       bool constructor_p;
10526       bool found_decl_spec;
10527       cp_token *token;
10528
10529       /* Peek at the next token.  */
10530       token = cp_lexer_peek_token (parser->lexer);
10531
10532       /* Save the first token of the decl spec list for error
10533          reporting.  */
10534       if (!start_token)
10535         start_token = token;
10536       /* Handle attributes.  */
10537       if (token->keyword == RID_ATTRIBUTE)
10538         {
10539           /* Parse the attributes.  */
10540           decl_specs->attributes
10541             = chainon (decl_specs->attributes,
10542                        cp_parser_attributes_opt (parser));
10543           continue;
10544         }
10545       /* Assume we will find a decl-specifier keyword.  */
10546       found_decl_spec = true;
10547       /* If the next token is an appropriate keyword, we can simply
10548          add it to the list.  */
10549       switch (token->keyword)
10550         {
10551           /* decl-specifier:
10552                friend
10553                constexpr */
10554         case RID_FRIEND:
10555           if (!at_class_scope_p ())
10556             {
10557               error_at (token->location, "%<friend%> used outside of class");
10558               cp_lexer_purge_token (parser->lexer);
10559             }
10560           else
10561             {
10562               ++decl_specs->specs[(int) ds_friend];
10563               /* Consume the token.  */
10564               cp_lexer_consume_token (parser->lexer);
10565             }
10566           break;
10567
10568         case RID_CONSTEXPR:
10569           ++decl_specs->specs[(int) ds_constexpr];
10570           cp_lexer_consume_token (parser->lexer);
10571           break;
10572
10573           /* function-specifier:
10574                inline
10575                virtual
10576                explicit  */
10577         case RID_INLINE:
10578         case RID_VIRTUAL:
10579         case RID_EXPLICIT:
10580           cp_parser_function_specifier_opt (parser, decl_specs);
10581           break;
10582
10583           /* decl-specifier:
10584                typedef  */
10585         case RID_TYPEDEF:
10586           ++decl_specs->specs[(int) ds_typedef];
10587           /* Consume the token.  */
10588           cp_lexer_consume_token (parser->lexer);
10589           /* A constructor declarator cannot appear in a typedef.  */
10590           constructor_possible_p = false;
10591           /* The "typedef" keyword can only occur in a declaration; we
10592              may as well commit at this point.  */
10593           cp_parser_commit_to_tentative_parse (parser);
10594
10595           if (decl_specs->storage_class != sc_none)
10596             decl_specs->conflicting_specifiers_p = true;
10597           break;
10598
10599           /* storage-class-specifier:
10600                auto
10601                register
10602                static
10603                extern
10604                mutable
10605
10606              GNU Extension:
10607                thread  */
10608         case RID_AUTO:
10609           if (cxx_dialect == cxx98) 
10610             {
10611               /* Consume the token.  */
10612               cp_lexer_consume_token (parser->lexer);
10613
10614               /* Complain about `auto' as a storage specifier, if
10615                  we're complaining about C++0x compatibility.  */
10616               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10617                           " changes meaning in C++11; please remove it");
10618
10619               /* Set the storage class anyway.  */
10620               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10621                                            token->location);
10622             }
10623           else
10624             /* C++0x auto type-specifier.  */
10625             found_decl_spec = false;
10626           break;
10627
10628         case RID_REGISTER:
10629         case RID_STATIC:
10630         case RID_EXTERN:
10631         case RID_MUTABLE:
10632           /* Consume the token.  */
10633           cp_lexer_consume_token (parser->lexer);
10634           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10635                                        token->location);
10636           break;
10637         case RID_THREAD:
10638           /* Consume the token.  */
10639           cp_lexer_consume_token (parser->lexer);
10640           ++decl_specs->specs[(int) ds_thread];
10641           break;
10642
10643         default:
10644           /* We did not yet find a decl-specifier yet.  */
10645           found_decl_spec = false;
10646           break;
10647         }
10648
10649       if (found_decl_spec
10650           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10651           && token->keyword != RID_CONSTEXPR)
10652         error ("decl-specifier invalid in condition");
10653
10654       /* Constructors are a special case.  The `S' in `S()' is not a
10655          decl-specifier; it is the beginning of the declarator.  */
10656       constructor_p
10657         = (!found_decl_spec
10658            && constructor_possible_p
10659            && (cp_parser_constructor_declarator_p
10660                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10661
10662       /* If we don't have a DECL_SPEC yet, then we must be looking at
10663          a type-specifier.  */
10664       if (!found_decl_spec && !constructor_p)
10665         {
10666           int decl_spec_declares_class_or_enum;
10667           bool is_cv_qualifier;
10668           tree type_spec;
10669
10670           type_spec
10671             = cp_parser_type_specifier (parser, flags,
10672                                         decl_specs,
10673                                         /*is_declaration=*/true,
10674                                         &decl_spec_declares_class_or_enum,
10675                                         &is_cv_qualifier);
10676           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10677
10678           /* If this type-specifier referenced a user-defined type
10679              (a typedef, class-name, etc.), then we can't allow any
10680              more such type-specifiers henceforth.
10681
10682              [dcl.spec]
10683
10684              The longest sequence of decl-specifiers that could
10685              possibly be a type name is taken as the
10686              decl-specifier-seq of a declaration.  The sequence shall
10687              be self-consistent as described below.
10688
10689              [dcl.type]
10690
10691              As a general rule, at most one type-specifier is allowed
10692              in the complete decl-specifier-seq of a declaration.  The
10693              only exceptions are the following:
10694
10695              -- const or volatile can be combined with any other
10696                 type-specifier.
10697
10698              -- signed or unsigned can be combined with char, long,
10699                 short, or int.
10700
10701              -- ..
10702
10703              Example:
10704
10705                typedef char* Pc;
10706                void g (const int Pc);
10707
10708              Here, Pc is *not* part of the decl-specifier seq; it's
10709              the declarator.  Therefore, once we see a type-specifier
10710              (other than a cv-qualifier), we forbid any additional
10711              user-defined types.  We *do* still allow things like `int
10712              int' to be considered a decl-specifier-seq, and issue the
10713              error message later.  */
10714           if (type_spec && !is_cv_qualifier)
10715             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10716           /* A constructor declarator cannot follow a type-specifier.  */
10717           if (type_spec)
10718             {
10719               constructor_possible_p = false;
10720               found_decl_spec = true;
10721               if (!is_cv_qualifier)
10722                 decl_specs->any_type_specifiers_p = true;
10723             }
10724         }
10725
10726       /* If we still do not have a DECL_SPEC, then there are no more
10727          decl-specifiers.  */
10728       if (!found_decl_spec)
10729         break;
10730
10731       decl_specs->any_specifiers_p = true;
10732       /* After we see one decl-specifier, further decl-specifiers are
10733          always optional.  */
10734       flags |= CP_PARSER_FLAGS_OPTIONAL;
10735     }
10736
10737   cp_parser_check_decl_spec (decl_specs, start_token->location);
10738
10739   /* Don't allow a friend specifier with a class definition.  */
10740   if (decl_specs->specs[(int) ds_friend] != 0
10741       && (*declares_class_or_enum & 2))
10742     error_at (start_token->location,
10743               "class definition may not be declared a friend");
10744 }
10745
10746 /* Parse an (optional) storage-class-specifier.
10747
10748    storage-class-specifier:
10749      auto
10750      register
10751      static
10752      extern
10753      mutable
10754
10755    GNU Extension:
10756
10757    storage-class-specifier:
10758      thread
10759
10760    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10761
10762 static tree
10763 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10764 {
10765   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10766     {
10767     case RID_AUTO:
10768       if (cxx_dialect != cxx98)
10769         return NULL_TREE;
10770       /* Fall through for C++98.  */
10771
10772     case RID_REGISTER:
10773     case RID_STATIC:
10774     case RID_EXTERN:
10775     case RID_MUTABLE:
10776     case RID_THREAD:
10777       /* Consume the token.  */
10778       return cp_lexer_consume_token (parser->lexer)->u.value;
10779
10780     default:
10781       return NULL_TREE;
10782     }
10783 }
10784
10785 /* Parse an (optional) function-specifier.
10786
10787    function-specifier:
10788      inline
10789      virtual
10790      explicit
10791
10792    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10793    Updates DECL_SPECS, if it is non-NULL.  */
10794
10795 static tree
10796 cp_parser_function_specifier_opt (cp_parser* parser,
10797                                   cp_decl_specifier_seq *decl_specs)
10798 {
10799   cp_token *token = cp_lexer_peek_token (parser->lexer);
10800   switch (token->keyword)
10801     {
10802     case RID_INLINE:
10803       if (decl_specs)
10804         ++decl_specs->specs[(int) ds_inline];
10805       break;
10806
10807     case RID_VIRTUAL:
10808       /* 14.5.2.3 [temp.mem]
10809
10810          A member function template shall not be virtual.  */
10811       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10812         error_at (token->location, "templates may not be %<virtual%>");
10813       else if (decl_specs)
10814         ++decl_specs->specs[(int) ds_virtual];
10815       break;
10816
10817     case RID_EXPLICIT:
10818       if (decl_specs)
10819         ++decl_specs->specs[(int) ds_explicit];
10820       break;
10821
10822     default:
10823       return NULL_TREE;
10824     }
10825
10826   /* Consume the token.  */
10827   return cp_lexer_consume_token (parser->lexer)->u.value;
10828 }
10829
10830 /* Parse a linkage-specification.
10831
10832    linkage-specification:
10833      extern string-literal { declaration-seq [opt] }
10834      extern string-literal declaration  */
10835
10836 static void
10837 cp_parser_linkage_specification (cp_parser* parser)
10838 {
10839   tree linkage;
10840
10841   /* Look for the `extern' keyword.  */
10842   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10843
10844   /* Look for the string-literal.  */
10845   linkage = cp_parser_string_literal (parser, false, false);
10846
10847   /* Transform the literal into an identifier.  If the literal is a
10848      wide-character string, or contains embedded NULs, then we can't
10849      handle it as the user wants.  */
10850   if (strlen (TREE_STRING_POINTER (linkage))
10851       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10852     {
10853       cp_parser_error (parser, "invalid linkage-specification");
10854       /* Assume C++ linkage.  */
10855       linkage = lang_name_cplusplus;
10856     }
10857   else
10858     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10859
10860   /* We're now using the new linkage.  */
10861   push_lang_context (linkage);
10862
10863   /* If the next token is a `{', then we're using the first
10864      production.  */
10865   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10866     {
10867       /* Consume the `{' token.  */
10868       cp_lexer_consume_token (parser->lexer);
10869       /* Parse the declarations.  */
10870       cp_parser_declaration_seq_opt (parser);
10871       /* Look for the closing `}'.  */
10872       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10873     }
10874   /* Otherwise, there's just one declaration.  */
10875   else
10876     {
10877       bool saved_in_unbraced_linkage_specification_p;
10878
10879       saved_in_unbraced_linkage_specification_p
10880         = parser->in_unbraced_linkage_specification_p;
10881       parser->in_unbraced_linkage_specification_p = true;
10882       cp_parser_declaration (parser);
10883       parser->in_unbraced_linkage_specification_p
10884         = saved_in_unbraced_linkage_specification_p;
10885     }
10886
10887   /* We're done with the linkage-specification.  */
10888   pop_lang_context ();
10889 }
10890
10891 /* Parse a static_assert-declaration.
10892
10893    static_assert-declaration:
10894      static_assert ( constant-expression , string-literal ) ; 
10895
10896    If MEMBER_P, this static_assert is a class member.  */
10897
10898 static void 
10899 cp_parser_static_assert(cp_parser *parser, bool member_p)
10900 {
10901   tree condition;
10902   tree message;
10903   cp_token *token;
10904   location_t saved_loc;
10905   bool dummy;
10906
10907   /* Peek at the `static_assert' token so we can keep track of exactly
10908      where the static assertion started.  */
10909   token = cp_lexer_peek_token (parser->lexer);
10910   saved_loc = token->location;
10911
10912   /* Look for the `static_assert' keyword.  */
10913   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10914                                   RT_STATIC_ASSERT))
10915     return;
10916
10917   /*  We know we are in a static assertion; commit to any tentative
10918       parse.  */
10919   if (cp_parser_parsing_tentatively (parser))
10920     cp_parser_commit_to_tentative_parse (parser);
10921
10922   /* Parse the `(' starting the static assertion condition.  */
10923   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10924
10925   /* Parse the constant-expression.  Allow a non-constant expression
10926      here in order to give better diagnostics in finish_static_assert.  */
10927   condition = 
10928     cp_parser_constant_expression (parser,
10929                                    /*allow_non_constant_p=*/true,
10930                                    /*non_constant_p=*/&dummy);
10931
10932   /* Parse the separating `,'.  */
10933   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10934
10935   /* Parse the string-literal message.  */
10936   message = cp_parser_string_literal (parser, 
10937                                       /*translate=*/false,
10938                                       /*wide_ok=*/true);
10939
10940   /* A `)' completes the static assertion.  */
10941   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10942     cp_parser_skip_to_closing_parenthesis (parser, 
10943                                            /*recovering=*/true, 
10944                                            /*or_comma=*/false,
10945                                            /*consume_paren=*/true);
10946
10947   /* A semicolon terminates the declaration.  */
10948   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10949
10950   /* Complete the static assertion, which may mean either processing 
10951      the static assert now or saving it for template instantiation.  */
10952   finish_static_assert (condition, message, saved_loc, member_p);
10953 }
10954
10955 /* Parse a `decltype' type. Returns the type. 
10956
10957    simple-type-specifier:
10958      decltype ( expression )  */
10959
10960 static tree
10961 cp_parser_decltype (cp_parser *parser)
10962 {
10963   tree expr;
10964   bool id_expression_or_member_access_p = false;
10965   const char *saved_message;
10966   bool saved_integral_constant_expression_p;
10967   bool saved_non_integral_constant_expression_p;
10968   cp_token *id_expr_start_token;
10969   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10970
10971   if (start_token->type == CPP_DECLTYPE)
10972     {
10973       /* Already parsed.  */
10974       cp_lexer_consume_token (parser->lexer);
10975       return start_token->u.value;
10976     }
10977
10978   /* Look for the `decltype' token.  */
10979   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10980     return error_mark_node;
10981
10982   /* Types cannot be defined in a `decltype' expression.  Save away the
10983      old message.  */
10984   saved_message = parser->type_definition_forbidden_message;
10985
10986   /* And create the new one.  */
10987   parser->type_definition_forbidden_message
10988     = G_("types may not be defined in %<decltype%> expressions");
10989
10990   /* The restrictions on constant-expressions do not apply inside
10991      decltype expressions.  */
10992   saved_integral_constant_expression_p
10993     = parser->integral_constant_expression_p;
10994   saved_non_integral_constant_expression_p
10995     = parser->non_integral_constant_expression_p;
10996   parser->integral_constant_expression_p = false;
10997
10998   /* Do not actually evaluate the expression.  */
10999   ++cp_unevaluated_operand;
11000
11001   /* Do not warn about problems with the expression.  */
11002   ++c_inhibit_evaluation_warnings;
11003
11004   /* Parse the opening `('.  */
11005   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11006     return error_mark_node;
11007   
11008   /* First, try parsing an id-expression.  */
11009   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11010   cp_parser_parse_tentatively (parser);
11011   expr = cp_parser_id_expression (parser,
11012                                   /*template_keyword_p=*/false,
11013                                   /*check_dependency_p=*/true,
11014                                   /*template_p=*/NULL,
11015                                   /*declarator_p=*/false,
11016                                   /*optional_p=*/false);
11017
11018   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11019     {
11020       bool non_integral_constant_expression_p = false;
11021       tree id_expression = expr;
11022       cp_id_kind idk;
11023       const char *error_msg;
11024
11025       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11026         /* Lookup the name we got back from the id-expression.  */
11027         expr = cp_parser_lookup_name (parser, expr,
11028                                       none_type,
11029                                       /*is_template=*/false,
11030                                       /*is_namespace=*/false,
11031                                       /*check_dependency=*/true,
11032                                       /*ambiguous_decls=*/NULL,
11033                                       id_expr_start_token->location);
11034
11035       if (expr
11036           && expr != error_mark_node
11037           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11038           && TREE_CODE (expr) != TYPE_DECL
11039           && (TREE_CODE (expr) != BIT_NOT_EXPR
11040               || !TYPE_P (TREE_OPERAND (expr, 0)))
11041           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11042         {
11043           /* Complete lookup of the id-expression.  */
11044           expr = (finish_id_expression
11045                   (id_expression, expr, parser->scope, &idk,
11046                    /*integral_constant_expression_p=*/false,
11047                    /*allow_non_integral_constant_expression_p=*/true,
11048                    &non_integral_constant_expression_p,
11049                    /*template_p=*/false,
11050                    /*done=*/true,
11051                    /*address_p=*/false,
11052                    /*template_arg_p=*/false,
11053                    &error_msg,
11054                    id_expr_start_token->location));
11055
11056           if (expr == error_mark_node)
11057             /* We found an id-expression, but it was something that we
11058                should not have found. This is an error, not something
11059                we can recover from, so note that we found an
11060                id-expression and we'll recover as gracefully as
11061                possible.  */
11062             id_expression_or_member_access_p = true;
11063         }
11064
11065       if (expr 
11066           && expr != error_mark_node
11067           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11068         /* We have an id-expression.  */
11069         id_expression_or_member_access_p = true;
11070     }
11071
11072   if (!id_expression_or_member_access_p)
11073     {
11074       /* Abort the id-expression parse.  */
11075       cp_parser_abort_tentative_parse (parser);
11076
11077       /* Parsing tentatively, again.  */
11078       cp_parser_parse_tentatively (parser);
11079
11080       /* Parse a class member access.  */
11081       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11082                                            /*cast_p=*/false,
11083                                            /*member_access_only_p=*/true, NULL);
11084
11085       if (expr 
11086           && expr != error_mark_node
11087           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11088         /* We have an id-expression.  */
11089         id_expression_or_member_access_p = true;
11090     }
11091
11092   if (id_expression_or_member_access_p)
11093     /* We have parsed the complete id-expression or member access.  */
11094     cp_parser_parse_definitely (parser);
11095   else
11096     {
11097       bool saved_greater_than_is_operator_p;
11098
11099       /* Abort our attempt to parse an id-expression or member access
11100          expression.  */
11101       cp_parser_abort_tentative_parse (parser);
11102
11103       /* Within a parenthesized expression, a `>' token is always
11104          the greater-than operator.  */
11105       saved_greater_than_is_operator_p
11106         = parser->greater_than_is_operator_p;
11107       parser->greater_than_is_operator_p = true;
11108
11109       /* Parse a full expression.  */
11110       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11111
11112       /* The `>' token might be the end of a template-id or
11113          template-parameter-list now.  */
11114       parser->greater_than_is_operator_p
11115         = saved_greater_than_is_operator_p;
11116     }
11117
11118   /* Go back to evaluating expressions.  */
11119   --cp_unevaluated_operand;
11120   --c_inhibit_evaluation_warnings;
11121
11122   /* Restore the old message and the integral constant expression
11123      flags.  */
11124   parser->type_definition_forbidden_message = saved_message;
11125   parser->integral_constant_expression_p
11126     = saved_integral_constant_expression_p;
11127   parser->non_integral_constant_expression_p
11128     = saved_non_integral_constant_expression_p;
11129
11130   /* Parse to the closing `)'.  */
11131   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11132     {
11133       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11134                                              /*consume_paren=*/true);
11135       return error_mark_node;
11136     }
11137
11138   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11139                                tf_warning_or_error);
11140
11141   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11142      it again.  */
11143   start_token->type = CPP_DECLTYPE;
11144   start_token->u.value = expr;
11145   start_token->keyword = RID_MAX;
11146   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11147
11148   return expr;
11149 }
11150
11151 /* Special member functions [gram.special] */
11152
11153 /* Parse a conversion-function-id.
11154
11155    conversion-function-id:
11156      operator conversion-type-id
11157
11158    Returns an IDENTIFIER_NODE representing the operator.  */
11159
11160 static tree
11161 cp_parser_conversion_function_id (cp_parser* parser)
11162 {
11163   tree type;
11164   tree saved_scope;
11165   tree saved_qualifying_scope;
11166   tree saved_object_scope;
11167   tree pushed_scope = NULL_TREE;
11168
11169   /* Look for the `operator' token.  */
11170   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11171     return error_mark_node;
11172   /* When we parse the conversion-type-id, the current scope will be
11173      reset.  However, we need that information in able to look up the
11174      conversion function later, so we save it here.  */
11175   saved_scope = parser->scope;
11176   saved_qualifying_scope = parser->qualifying_scope;
11177   saved_object_scope = parser->object_scope;
11178   /* We must enter the scope of the class so that the names of
11179      entities declared within the class are available in the
11180      conversion-type-id.  For example, consider:
11181
11182        struct S {
11183          typedef int I;
11184          operator I();
11185        };
11186
11187        S::operator I() { ... }
11188
11189      In order to see that `I' is a type-name in the definition, we
11190      must be in the scope of `S'.  */
11191   if (saved_scope)
11192     pushed_scope = push_scope (saved_scope);
11193   /* Parse the conversion-type-id.  */
11194   type = cp_parser_conversion_type_id (parser);
11195   /* Leave the scope of the class, if any.  */
11196   if (pushed_scope)
11197     pop_scope (pushed_scope);
11198   /* Restore the saved scope.  */
11199   parser->scope = saved_scope;
11200   parser->qualifying_scope = saved_qualifying_scope;
11201   parser->object_scope = saved_object_scope;
11202   /* If the TYPE is invalid, indicate failure.  */
11203   if (type == error_mark_node)
11204     return error_mark_node;
11205   return mangle_conv_op_name_for_type (type);
11206 }
11207
11208 /* Parse a conversion-type-id:
11209
11210    conversion-type-id:
11211      type-specifier-seq conversion-declarator [opt]
11212
11213    Returns the TYPE specified.  */
11214
11215 static tree
11216 cp_parser_conversion_type_id (cp_parser* parser)
11217 {
11218   tree attributes;
11219   cp_decl_specifier_seq type_specifiers;
11220   cp_declarator *declarator;
11221   tree type_specified;
11222
11223   /* Parse the attributes.  */
11224   attributes = cp_parser_attributes_opt (parser);
11225   /* Parse the type-specifiers.  */
11226   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11227                                 /*is_trailing_return=*/false,
11228                                 &type_specifiers);
11229   /* If that didn't work, stop.  */
11230   if (type_specifiers.type == error_mark_node)
11231     return error_mark_node;
11232   /* Parse the conversion-declarator.  */
11233   declarator = cp_parser_conversion_declarator_opt (parser);
11234
11235   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11236                                     /*initialized=*/0, &attributes);
11237   if (attributes)
11238     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11239
11240   /* Don't give this error when parsing tentatively.  This happens to
11241      work because we always parse this definitively once.  */
11242   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11243       && type_uses_auto (type_specified))
11244     {
11245       error ("invalid use of %<auto%> in conversion operator");
11246       return error_mark_node;
11247     }
11248
11249   return type_specified;
11250 }
11251
11252 /* Parse an (optional) conversion-declarator.
11253
11254    conversion-declarator:
11255      ptr-operator conversion-declarator [opt]
11256
11257    */
11258
11259 static cp_declarator *
11260 cp_parser_conversion_declarator_opt (cp_parser* parser)
11261 {
11262   enum tree_code code;
11263   tree class_type;
11264   cp_cv_quals cv_quals;
11265
11266   /* We don't know if there's a ptr-operator next, or not.  */
11267   cp_parser_parse_tentatively (parser);
11268   /* Try the ptr-operator.  */
11269   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11270   /* If it worked, look for more conversion-declarators.  */
11271   if (cp_parser_parse_definitely (parser))
11272     {
11273       cp_declarator *declarator;
11274
11275       /* Parse another optional declarator.  */
11276       declarator = cp_parser_conversion_declarator_opt (parser);
11277
11278       return cp_parser_make_indirect_declarator
11279         (code, class_type, cv_quals, declarator);
11280    }
11281
11282   return NULL;
11283 }
11284
11285 /* Parse an (optional) ctor-initializer.
11286
11287    ctor-initializer:
11288      : mem-initializer-list
11289
11290    Returns TRUE iff the ctor-initializer was actually present.  */
11291
11292 static bool
11293 cp_parser_ctor_initializer_opt (cp_parser* parser)
11294 {
11295   /* If the next token is not a `:', then there is no
11296      ctor-initializer.  */
11297   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11298     {
11299       /* Do default initialization of any bases and members.  */
11300       if (DECL_CONSTRUCTOR_P (current_function_decl))
11301         finish_mem_initializers (NULL_TREE);
11302
11303       return false;
11304     }
11305
11306   /* Consume the `:' token.  */
11307   cp_lexer_consume_token (parser->lexer);
11308   /* And the mem-initializer-list.  */
11309   cp_parser_mem_initializer_list (parser);
11310
11311   return true;
11312 }
11313
11314 /* Parse a mem-initializer-list.
11315
11316    mem-initializer-list:
11317      mem-initializer ... [opt]
11318      mem-initializer ... [opt] , mem-initializer-list  */
11319
11320 static void
11321 cp_parser_mem_initializer_list (cp_parser* parser)
11322 {
11323   tree mem_initializer_list = NULL_TREE;
11324   tree target_ctor = error_mark_node;
11325   cp_token *token = cp_lexer_peek_token (parser->lexer);
11326
11327   /* Let the semantic analysis code know that we are starting the
11328      mem-initializer-list.  */
11329   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11330     error_at (token->location,
11331               "only constructors take member initializers");
11332
11333   /* Loop through the list.  */
11334   while (true)
11335     {
11336       tree mem_initializer;
11337
11338       token = cp_lexer_peek_token (parser->lexer);
11339       /* Parse the mem-initializer.  */
11340       mem_initializer = cp_parser_mem_initializer (parser);
11341       /* If the next token is a `...', we're expanding member initializers. */
11342       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11343         {
11344           /* Consume the `...'. */
11345           cp_lexer_consume_token (parser->lexer);
11346
11347           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11348              can be expanded but members cannot. */
11349           if (mem_initializer != error_mark_node
11350               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11351             {
11352               error_at (token->location,
11353                         "cannot expand initializer for member %<%D%>",
11354                         TREE_PURPOSE (mem_initializer));
11355               mem_initializer = error_mark_node;
11356             }
11357
11358           /* Construct the pack expansion type. */
11359           if (mem_initializer != error_mark_node)
11360             mem_initializer = make_pack_expansion (mem_initializer);
11361         }
11362       if (target_ctor != error_mark_node
11363           && mem_initializer != error_mark_node)
11364         {
11365           error ("mem-initializer for %qD follows constructor delegation",
11366                  TREE_PURPOSE (mem_initializer));
11367           mem_initializer = error_mark_node;
11368         }
11369       /* Look for a target constructor. */
11370       if (mem_initializer != error_mark_node
11371           && TYPE_P (TREE_PURPOSE (mem_initializer))
11372           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11373         {
11374           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11375           if (mem_initializer_list)
11376             {
11377               error ("constructor delegation follows mem-initializer for %qD",
11378                      TREE_PURPOSE (mem_initializer_list));
11379               mem_initializer = error_mark_node;
11380             }
11381           target_ctor = mem_initializer;
11382         }
11383       /* Add it to the list, unless it was erroneous.  */
11384       if (mem_initializer != error_mark_node)
11385         {
11386           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11387           mem_initializer_list = mem_initializer;
11388         }
11389       /* If the next token is not a `,', we're done.  */
11390       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11391         break;
11392       /* Consume the `,' token.  */
11393       cp_lexer_consume_token (parser->lexer);
11394     }
11395
11396   /* Perform semantic analysis.  */
11397   if (DECL_CONSTRUCTOR_P (current_function_decl))
11398     finish_mem_initializers (mem_initializer_list);
11399 }
11400
11401 /* Parse a mem-initializer.
11402
11403    mem-initializer:
11404      mem-initializer-id ( expression-list [opt] )
11405      mem-initializer-id braced-init-list
11406
11407    GNU extension:
11408
11409    mem-initializer:
11410      ( expression-list [opt] )
11411
11412    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11413    class) or FIELD_DECL (for a non-static data member) to initialize;
11414    the TREE_VALUE is the expression-list.  An empty initialization
11415    list is represented by void_list_node.  */
11416
11417 static tree
11418 cp_parser_mem_initializer (cp_parser* parser)
11419 {
11420   tree mem_initializer_id;
11421   tree expression_list;
11422   tree member;
11423   cp_token *token = cp_lexer_peek_token (parser->lexer);
11424
11425   /* Find out what is being initialized.  */
11426   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11427     {
11428       permerror (token->location,
11429                  "anachronistic old-style base class initializer");
11430       mem_initializer_id = NULL_TREE;
11431     }
11432   else
11433     {
11434       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11435       if (mem_initializer_id == error_mark_node)
11436         return mem_initializer_id;
11437     }
11438   member = expand_member_init (mem_initializer_id);
11439   if (member && !DECL_P (member))
11440     in_base_initializer = 1;
11441
11442   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11443     {
11444       bool expr_non_constant_p;
11445       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11446       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11447       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11448       expression_list = build_tree_list (NULL_TREE, expression_list);
11449     }
11450   else
11451     {
11452       VEC(tree,gc)* vec;
11453       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11454                                                      /*cast_p=*/false,
11455                                                      /*allow_expansion_p=*/true,
11456                                                      /*non_constant_p=*/NULL);
11457       if (vec == NULL)
11458         return error_mark_node;
11459       expression_list = build_tree_list_vec (vec);
11460       release_tree_vector (vec);
11461     }
11462
11463   if (expression_list == error_mark_node)
11464     return error_mark_node;
11465   if (!expression_list)
11466     expression_list = void_type_node;
11467
11468   in_base_initializer = 0;
11469
11470   return member ? build_tree_list (member, expression_list) : error_mark_node;
11471 }
11472
11473 /* Parse a mem-initializer-id.
11474
11475    mem-initializer-id:
11476      :: [opt] nested-name-specifier [opt] class-name
11477      identifier
11478
11479    Returns a TYPE indicating the class to be initializer for the first
11480    production.  Returns an IDENTIFIER_NODE indicating the data member
11481    to be initialized for the second production.  */
11482
11483 static tree
11484 cp_parser_mem_initializer_id (cp_parser* parser)
11485 {
11486   bool global_scope_p;
11487   bool nested_name_specifier_p;
11488   bool template_p = false;
11489   tree id;
11490
11491   cp_token *token = cp_lexer_peek_token (parser->lexer);
11492
11493   /* `typename' is not allowed in this context ([temp.res]).  */
11494   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11495     {
11496       error_at (token->location, 
11497                 "keyword %<typename%> not allowed in this context (a qualified "
11498                 "member initializer is implicitly a type)");
11499       cp_lexer_consume_token (parser->lexer);
11500     }
11501   /* Look for the optional `::' operator.  */
11502   global_scope_p
11503     = (cp_parser_global_scope_opt (parser,
11504                                    /*current_scope_valid_p=*/false)
11505        != NULL_TREE);
11506   /* Look for the optional nested-name-specifier.  The simplest way to
11507      implement:
11508
11509        [temp.res]
11510
11511        The keyword `typename' is not permitted in a base-specifier or
11512        mem-initializer; in these contexts a qualified name that
11513        depends on a template-parameter is implicitly assumed to be a
11514        type name.
11515
11516      is to assume that we have seen the `typename' keyword at this
11517      point.  */
11518   nested_name_specifier_p
11519     = (cp_parser_nested_name_specifier_opt (parser,
11520                                             /*typename_keyword_p=*/true,
11521                                             /*check_dependency_p=*/true,
11522                                             /*type_p=*/true,
11523                                             /*is_declaration=*/true)
11524        != NULL_TREE);
11525   if (nested_name_specifier_p)
11526     template_p = cp_parser_optional_template_keyword (parser);
11527   /* If there is a `::' operator or a nested-name-specifier, then we
11528      are definitely looking for a class-name.  */
11529   if (global_scope_p || nested_name_specifier_p)
11530     return cp_parser_class_name (parser,
11531                                  /*typename_keyword_p=*/true,
11532                                  /*template_keyword_p=*/template_p,
11533                                  typename_type,
11534                                  /*check_dependency_p=*/true,
11535                                  /*class_head_p=*/false,
11536                                  /*is_declaration=*/true);
11537   /* Otherwise, we could also be looking for an ordinary identifier.  */
11538   cp_parser_parse_tentatively (parser);
11539   /* Try a class-name.  */
11540   id = cp_parser_class_name (parser,
11541                              /*typename_keyword_p=*/true,
11542                              /*template_keyword_p=*/false,
11543                              none_type,
11544                              /*check_dependency_p=*/true,
11545                              /*class_head_p=*/false,
11546                              /*is_declaration=*/true);
11547   /* If we found one, we're done.  */
11548   if (cp_parser_parse_definitely (parser))
11549     return id;
11550   /* Otherwise, look for an ordinary identifier.  */
11551   return cp_parser_identifier (parser);
11552 }
11553
11554 /* Overloading [gram.over] */
11555
11556 /* Parse an operator-function-id.
11557
11558    operator-function-id:
11559      operator operator
11560
11561    Returns an IDENTIFIER_NODE for the operator which is a
11562    human-readable spelling of the identifier, e.g., `operator +'.  */
11563
11564 static tree
11565 cp_parser_operator_function_id (cp_parser* parser)
11566 {
11567   /* Look for the `operator' keyword.  */
11568   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11569     return error_mark_node;
11570   /* And then the name of the operator itself.  */
11571   return cp_parser_operator (parser);
11572 }
11573
11574 /* Return an identifier node for a user-defined literal operator.
11575    The suffix identifier is chained to the operator name identifier.  */
11576
11577 static tree
11578 cp_literal_operator_id (const char* name)
11579 {
11580   tree identifier;
11581   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11582                               + strlen (name) + 10);
11583   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11584   identifier = get_identifier (buffer);
11585   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11586
11587   return identifier;
11588 }
11589
11590 /* Parse an operator.
11591
11592    operator:
11593      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11594      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11595      || ++ -- , ->* -> () []
11596
11597    GNU Extensions:
11598
11599    operator:
11600      <? >? <?= >?=
11601
11602    Returns an IDENTIFIER_NODE for the operator which is a
11603    human-readable spelling of the identifier, e.g., `operator +'.  */
11604
11605 static tree
11606 cp_parser_operator (cp_parser* parser)
11607 {
11608   tree id = NULL_TREE;
11609   cp_token *token;
11610
11611   /* Peek at the next token.  */
11612   token = cp_lexer_peek_token (parser->lexer);
11613   /* Figure out which operator we have.  */
11614   switch (token->type)
11615     {
11616     case CPP_KEYWORD:
11617       {
11618         enum tree_code op;
11619
11620         /* The keyword should be either `new' or `delete'.  */
11621         if (token->keyword == RID_NEW)
11622           op = NEW_EXPR;
11623         else if (token->keyword == RID_DELETE)
11624           op = DELETE_EXPR;
11625         else
11626           break;
11627
11628         /* Consume the `new' or `delete' token.  */
11629         cp_lexer_consume_token (parser->lexer);
11630
11631         /* Peek at the next token.  */
11632         token = cp_lexer_peek_token (parser->lexer);
11633         /* If it's a `[' token then this is the array variant of the
11634            operator.  */
11635         if (token->type == CPP_OPEN_SQUARE)
11636           {
11637             /* Consume the `[' token.  */
11638             cp_lexer_consume_token (parser->lexer);
11639             /* Look for the `]' token.  */
11640             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11641             id = ansi_opname (op == NEW_EXPR
11642                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11643           }
11644         /* Otherwise, we have the non-array variant.  */
11645         else
11646           id = ansi_opname (op);
11647
11648         return id;
11649       }
11650
11651     case CPP_PLUS:
11652       id = ansi_opname (PLUS_EXPR);
11653       break;
11654
11655     case CPP_MINUS:
11656       id = ansi_opname (MINUS_EXPR);
11657       break;
11658
11659     case CPP_MULT:
11660       id = ansi_opname (MULT_EXPR);
11661       break;
11662
11663     case CPP_DIV:
11664       id = ansi_opname (TRUNC_DIV_EXPR);
11665       break;
11666
11667     case CPP_MOD:
11668       id = ansi_opname (TRUNC_MOD_EXPR);
11669       break;
11670
11671     case CPP_XOR:
11672       id = ansi_opname (BIT_XOR_EXPR);
11673       break;
11674
11675     case CPP_AND:
11676       id = ansi_opname (BIT_AND_EXPR);
11677       break;
11678
11679     case CPP_OR:
11680       id = ansi_opname (BIT_IOR_EXPR);
11681       break;
11682
11683     case CPP_COMPL:
11684       id = ansi_opname (BIT_NOT_EXPR);
11685       break;
11686
11687     case CPP_NOT:
11688       id = ansi_opname (TRUTH_NOT_EXPR);
11689       break;
11690
11691     case CPP_EQ:
11692       id = ansi_assopname (NOP_EXPR);
11693       break;
11694
11695     case CPP_LESS:
11696       id = ansi_opname (LT_EXPR);
11697       break;
11698
11699     case CPP_GREATER:
11700       id = ansi_opname (GT_EXPR);
11701       break;
11702
11703     case CPP_PLUS_EQ:
11704       id = ansi_assopname (PLUS_EXPR);
11705       break;
11706
11707     case CPP_MINUS_EQ:
11708       id = ansi_assopname (MINUS_EXPR);
11709       break;
11710
11711     case CPP_MULT_EQ:
11712       id = ansi_assopname (MULT_EXPR);
11713       break;
11714
11715     case CPP_DIV_EQ:
11716       id = ansi_assopname (TRUNC_DIV_EXPR);
11717       break;
11718
11719     case CPP_MOD_EQ:
11720       id = ansi_assopname (TRUNC_MOD_EXPR);
11721       break;
11722
11723     case CPP_XOR_EQ:
11724       id = ansi_assopname (BIT_XOR_EXPR);
11725       break;
11726
11727     case CPP_AND_EQ:
11728       id = ansi_assopname (BIT_AND_EXPR);
11729       break;
11730
11731     case CPP_OR_EQ:
11732       id = ansi_assopname (BIT_IOR_EXPR);
11733       break;
11734
11735     case CPP_LSHIFT:
11736       id = ansi_opname (LSHIFT_EXPR);
11737       break;
11738
11739     case CPP_RSHIFT:
11740       id = ansi_opname (RSHIFT_EXPR);
11741       break;
11742
11743     case CPP_LSHIFT_EQ:
11744       id = ansi_assopname (LSHIFT_EXPR);
11745       break;
11746
11747     case CPP_RSHIFT_EQ:
11748       id = ansi_assopname (RSHIFT_EXPR);
11749       break;
11750
11751     case CPP_EQ_EQ:
11752       id = ansi_opname (EQ_EXPR);
11753       break;
11754
11755     case CPP_NOT_EQ:
11756       id = ansi_opname (NE_EXPR);
11757       break;
11758
11759     case CPP_LESS_EQ:
11760       id = ansi_opname (LE_EXPR);
11761       break;
11762
11763     case CPP_GREATER_EQ:
11764       id = ansi_opname (GE_EXPR);
11765       break;
11766
11767     case CPP_AND_AND:
11768       id = ansi_opname (TRUTH_ANDIF_EXPR);
11769       break;
11770
11771     case CPP_OR_OR:
11772       id = ansi_opname (TRUTH_ORIF_EXPR);
11773       break;
11774
11775     case CPP_PLUS_PLUS:
11776       id = ansi_opname (POSTINCREMENT_EXPR);
11777       break;
11778
11779     case CPP_MINUS_MINUS:
11780       id = ansi_opname (PREDECREMENT_EXPR);
11781       break;
11782
11783     case CPP_COMMA:
11784       id = ansi_opname (COMPOUND_EXPR);
11785       break;
11786
11787     case CPP_DEREF_STAR:
11788       id = ansi_opname (MEMBER_REF);
11789       break;
11790
11791     case CPP_DEREF:
11792       id = ansi_opname (COMPONENT_REF);
11793       break;
11794
11795     case CPP_OPEN_PAREN:
11796       /* Consume the `('.  */
11797       cp_lexer_consume_token (parser->lexer);
11798       /* Look for the matching `)'.  */
11799       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11800       return ansi_opname (CALL_EXPR);
11801
11802     case CPP_OPEN_SQUARE:
11803       /* Consume the `['.  */
11804       cp_lexer_consume_token (parser->lexer);
11805       /* Look for the matching `]'.  */
11806       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11807       return ansi_opname (ARRAY_REF);
11808
11809     case CPP_STRING:
11810       if (cxx_dialect == cxx98)
11811         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11812       if (TREE_STRING_LENGTH (token->u.value) > 2)
11813         {
11814           error ("expected empty string after %<operator%> keyword");
11815           return error_mark_node;
11816         }
11817       /* Consume the string.  */
11818       cp_lexer_consume_token (parser->lexer);
11819       /* Look for the suffix identifier.  */
11820       token = cp_lexer_peek_token (parser->lexer);
11821       if (token->type == CPP_NAME)
11822         {
11823           id = cp_parser_identifier (parser);
11824           if (id != error_mark_node)
11825             {
11826               const char *name = IDENTIFIER_POINTER (id);
11827               return cp_literal_operator_id (name);
11828             }
11829         }
11830       else
11831         {
11832           error ("expected suffix identifier");
11833           return error_mark_node;
11834         }
11835
11836     case CPP_STRING_USERDEF:
11837       error ("missing space between %<\"\"%> and suffix identifier");
11838       return error_mark_node;
11839
11840     default:
11841       /* Anything else is an error.  */
11842       break;
11843     }
11844
11845   /* If we have selected an identifier, we need to consume the
11846      operator token.  */
11847   if (id)
11848     cp_lexer_consume_token (parser->lexer);
11849   /* Otherwise, no valid operator name was present.  */
11850   else
11851     {
11852       cp_parser_error (parser, "expected operator");
11853       id = error_mark_node;
11854     }
11855
11856   return id;
11857 }
11858
11859 /* Parse a template-declaration.
11860
11861    template-declaration:
11862      export [opt] template < template-parameter-list > declaration
11863
11864    If MEMBER_P is TRUE, this template-declaration occurs within a
11865    class-specifier.
11866
11867    The grammar rule given by the standard isn't correct.  What
11868    is really meant is:
11869
11870    template-declaration:
11871      export [opt] template-parameter-list-seq
11872        decl-specifier-seq [opt] init-declarator [opt] ;
11873      export [opt] template-parameter-list-seq
11874        function-definition
11875
11876    template-parameter-list-seq:
11877      template-parameter-list-seq [opt]
11878      template < template-parameter-list >  */
11879
11880 static void
11881 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11882 {
11883   /* Check for `export'.  */
11884   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11885     {
11886       /* Consume the `export' token.  */
11887       cp_lexer_consume_token (parser->lexer);
11888       /* Warn that we do not support `export'.  */
11889       warning (0, "keyword %<export%> not implemented, and will be ignored");
11890     }
11891
11892   cp_parser_template_declaration_after_export (parser, member_p);
11893 }
11894
11895 /* Parse a template-parameter-list.
11896
11897    template-parameter-list:
11898      template-parameter
11899      template-parameter-list , template-parameter
11900
11901    Returns a TREE_LIST.  Each node represents a template parameter.
11902    The nodes are connected via their TREE_CHAINs.  */
11903
11904 static tree
11905 cp_parser_template_parameter_list (cp_parser* parser)
11906 {
11907   tree parameter_list = NULL_TREE;
11908
11909   begin_template_parm_list ();
11910
11911   /* The loop below parses the template parms.  We first need to know
11912      the total number of template parms to be able to compute proper
11913      canonical types of each dependent type. So after the loop, when
11914      we know the total number of template parms,
11915      end_template_parm_list computes the proper canonical types and
11916      fixes up the dependent types accordingly.  */
11917   while (true)
11918     {
11919       tree parameter;
11920       bool is_non_type;
11921       bool is_parameter_pack;
11922       location_t parm_loc;
11923
11924       /* Parse the template-parameter.  */
11925       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11926       parameter = cp_parser_template_parameter (parser, 
11927                                                 &is_non_type,
11928                                                 &is_parameter_pack);
11929       /* Add it to the list.  */
11930       if (parameter != error_mark_node)
11931         parameter_list = process_template_parm (parameter_list,
11932                                                 parm_loc,
11933                                                 parameter,
11934                                                 is_non_type,
11935                                                 is_parameter_pack,
11936                                                 0);
11937       else
11938        {
11939          tree err_parm = build_tree_list (parameter, parameter);
11940          parameter_list = chainon (parameter_list, err_parm);
11941        }
11942
11943       /* If the next token is not a `,', we're done.  */
11944       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11945         break;
11946       /* Otherwise, consume the `,' token.  */
11947       cp_lexer_consume_token (parser->lexer);
11948     }
11949
11950   return end_template_parm_list (parameter_list);
11951 }
11952
11953 /* Parse a template-parameter.
11954
11955    template-parameter:
11956      type-parameter
11957      parameter-declaration
11958
11959    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11960    the parameter.  The TREE_PURPOSE is the default value, if any.
11961    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11962    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11963    set to true iff this parameter is a parameter pack. */
11964
11965 static tree
11966 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11967                               bool *is_parameter_pack)
11968 {
11969   cp_token *token;
11970   cp_parameter_declarator *parameter_declarator;
11971   cp_declarator *id_declarator;
11972   tree parm;
11973
11974   /* Assume it is a type parameter or a template parameter.  */
11975   *is_non_type = false;
11976   /* Assume it not a parameter pack. */
11977   *is_parameter_pack = false;
11978   /* Peek at the next token.  */
11979   token = cp_lexer_peek_token (parser->lexer);
11980   /* If it is `class' or `template', we have a type-parameter.  */
11981   if (token->keyword == RID_TEMPLATE)
11982     return cp_parser_type_parameter (parser, is_parameter_pack);
11983   /* If it is `class' or `typename' we do not know yet whether it is a
11984      type parameter or a non-type parameter.  Consider:
11985
11986        template <typename T, typename T::X X> ...
11987
11988      or:
11989
11990        template <class C, class D*> ...
11991
11992      Here, the first parameter is a type parameter, and the second is
11993      a non-type parameter.  We can tell by looking at the token after
11994      the identifier -- if it is a `,', `=', or `>' then we have a type
11995      parameter.  */
11996   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11997     {
11998       /* Peek at the token after `class' or `typename'.  */
11999       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12000       /* If it's an ellipsis, we have a template type parameter
12001          pack. */
12002       if (token->type == CPP_ELLIPSIS)
12003         return cp_parser_type_parameter (parser, is_parameter_pack);
12004       /* If it's an identifier, skip it.  */
12005       if (token->type == CPP_NAME)
12006         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12007       /* Now, see if the token looks like the end of a template
12008          parameter.  */
12009       if (token->type == CPP_COMMA
12010           || token->type == CPP_EQ
12011           || token->type == CPP_GREATER)
12012         return cp_parser_type_parameter (parser, is_parameter_pack);
12013     }
12014
12015   /* Otherwise, it is a non-type parameter.
12016
12017      [temp.param]
12018
12019      When parsing a default template-argument for a non-type
12020      template-parameter, the first non-nested `>' is taken as the end
12021      of the template parameter-list rather than a greater-than
12022      operator.  */
12023   *is_non_type = true;
12024   parameter_declarator
12025      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12026                                         /*parenthesized_p=*/NULL);
12027
12028   /* If the parameter declaration is marked as a parameter pack, set
12029      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12030      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12031      grokdeclarator. */
12032   if (parameter_declarator
12033       && parameter_declarator->declarator
12034       && parameter_declarator->declarator->parameter_pack_p)
12035     {
12036       *is_parameter_pack = true;
12037       parameter_declarator->declarator->parameter_pack_p = false;
12038     }
12039
12040   /* If the next token is an ellipsis, and we don't already have it
12041      marked as a parameter pack, then we have a parameter pack (that
12042      has no declarator).  */
12043   if (!*is_parameter_pack
12044       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12045       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12046     {
12047       /* Consume the `...'.  */
12048       cp_lexer_consume_token (parser->lexer);
12049       maybe_warn_variadic_templates ();
12050       
12051       *is_parameter_pack = true;
12052     }
12053   /* We might end up with a pack expansion as the type of the non-type
12054      template parameter, in which case this is a non-type template
12055      parameter pack.  */
12056   else if (parameter_declarator
12057            && parameter_declarator->decl_specifiers.type
12058            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12059     {
12060       *is_parameter_pack = true;
12061       parameter_declarator->decl_specifiers.type = 
12062         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12063     }
12064
12065   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12066     {
12067       /* Parameter packs cannot have default arguments.  However, a
12068          user may try to do so, so we'll parse them and give an
12069          appropriate diagnostic here.  */
12070
12071       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12072       
12073       /* Find the name of the parameter pack.  */     
12074       id_declarator = parameter_declarator->declarator;
12075       while (id_declarator && id_declarator->kind != cdk_id)
12076         id_declarator = id_declarator->declarator;
12077       
12078       if (id_declarator && id_declarator->kind == cdk_id)
12079         error_at (start_token->location,
12080                   "template parameter pack %qD cannot have a default argument",
12081                   id_declarator->u.id.unqualified_name);
12082       else
12083         error_at (start_token->location,
12084                   "template parameter pack cannot have a default argument");
12085       
12086       /* Parse the default argument, but throw away the result.  */
12087       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12088     }
12089
12090   parm = grokdeclarator (parameter_declarator->declarator,
12091                          &parameter_declarator->decl_specifiers,
12092                          TPARM, /*initialized=*/0,
12093                          /*attrlist=*/NULL);
12094   if (parm == error_mark_node)
12095     return error_mark_node;
12096
12097   return build_tree_list (parameter_declarator->default_argument, parm);
12098 }
12099
12100 /* Parse a type-parameter.
12101
12102    type-parameter:
12103      class identifier [opt]
12104      class identifier [opt] = type-id
12105      typename identifier [opt]
12106      typename identifier [opt] = type-id
12107      template < template-parameter-list > class identifier [opt]
12108      template < template-parameter-list > class identifier [opt]
12109        = id-expression
12110
12111    GNU Extension (variadic templates):
12112
12113    type-parameter:
12114      class ... identifier [opt]
12115      typename ... identifier [opt]
12116
12117    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12118    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12119    the declaration of the parameter.
12120
12121    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12122
12123 static tree
12124 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12125 {
12126   cp_token *token;
12127   tree parameter;
12128
12129   /* Look for a keyword to tell us what kind of parameter this is.  */
12130   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12131   if (!token)
12132     return error_mark_node;
12133
12134   switch (token->keyword)
12135     {
12136     case RID_CLASS:
12137     case RID_TYPENAME:
12138       {
12139         tree identifier;
12140         tree default_argument;
12141
12142         /* If the next token is an ellipsis, we have a template
12143            argument pack. */
12144         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12145           {
12146             /* Consume the `...' token. */
12147             cp_lexer_consume_token (parser->lexer);
12148             maybe_warn_variadic_templates ();
12149
12150             *is_parameter_pack = true;
12151           }
12152
12153         /* If the next token is an identifier, then it names the
12154            parameter.  */
12155         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12156           identifier = cp_parser_identifier (parser);
12157         else
12158           identifier = NULL_TREE;
12159
12160         /* Create the parameter.  */
12161         parameter = finish_template_type_parm (class_type_node, identifier);
12162
12163         /* If the next token is an `=', we have a default argument.  */
12164         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12165           {
12166             /* Consume the `=' token.  */
12167             cp_lexer_consume_token (parser->lexer);
12168             /* Parse the default-argument.  */
12169             push_deferring_access_checks (dk_no_deferred);
12170             default_argument = cp_parser_type_id (parser);
12171
12172             /* Template parameter packs cannot have default
12173                arguments. */
12174             if (*is_parameter_pack)
12175               {
12176                 if (identifier)
12177                   error_at (token->location,
12178                             "template parameter pack %qD cannot have a "
12179                             "default argument", identifier);
12180                 else
12181                   error_at (token->location,
12182                             "template parameter packs cannot have "
12183                             "default arguments");
12184                 default_argument = NULL_TREE;
12185               }
12186             pop_deferring_access_checks ();
12187           }
12188         else
12189           default_argument = NULL_TREE;
12190
12191         /* Create the combined representation of the parameter and the
12192            default argument.  */
12193         parameter = build_tree_list (default_argument, parameter);
12194       }
12195       break;
12196
12197     case RID_TEMPLATE:
12198       {
12199         tree identifier;
12200         tree default_argument;
12201
12202         /* Look for the `<'.  */
12203         cp_parser_require (parser, CPP_LESS, RT_LESS);
12204         /* Parse the template-parameter-list.  */
12205         cp_parser_template_parameter_list (parser);
12206         /* Look for the `>'.  */
12207         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12208         /* Look for the `class' keyword.  */
12209         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12210         /* If the next token is an ellipsis, we have a template
12211            argument pack. */
12212         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12213           {
12214             /* Consume the `...' token. */
12215             cp_lexer_consume_token (parser->lexer);
12216             maybe_warn_variadic_templates ();
12217
12218             *is_parameter_pack = true;
12219           }
12220         /* If the next token is an `=', then there is a
12221            default-argument.  If the next token is a `>', we are at
12222            the end of the parameter-list.  If the next token is a `,',
12223            then we are at the end of this parameter.  */
12224         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12225             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12226             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12227           {
12228             identifier = cp_parser_identifier (parser);
12229             /* Treat invalid names as if the parameter were nameless.  */
12230             if (identifier == error_mark_node)
12231               identifier = NULL_TREE;
12232           }
12233         else
12234           identifier = NULL_TREE;
12235
12236         /* Create the template parameter.  */
12237         parameter = finish_template_template_parm (class_type_node,
12238                                                    identifier);
12239
12240         /* If the next token is an `=', then there is a
12241            default-argument.  */
12242         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12243           {
12244             bool is_template;
12245
12246             /* Consume the `='.  */
12247             cp_lexer_consume_token (parser->lexer);
12248             /* Parse the id-expression.  */
12249             push_deferring_access_checks (dk_no_deferred);
12250             /* save token before parsing the id-expression, for error
12251                reporting */
12252             token = cp_lexer_peek_token (parser->lexer);
12253             default_argument
12254               = cp_parser_id_expression (parser,
12255                                          /*template_keyword_p=*/false,
12256                                          /*check_dependency_p=*/true,
12257                                          /*template_p=*/&is_template,
12258                                          /*declarator_p=*/false,
12259                                          /*optional_p=*/false);
12260             if (TREE_CODE (default_argument) == TYPE_DECL)
12261               /* If the id-expression was a template-id that refers to
12262                  a template-class, we already have the declaration here,
12263                  so no further lookup is needed.  */
12264                  ;
12265             else
12266               /* Look up the name.  */
12267               default_argument
12268                 = cp_parser_lookup_name (parser, default_argument,
12269                                          none_type,
12270                                          /*is_template=*/is_template,
12271                                          /*is_namespace=*/false,
12272                                          /*check_dependency=*/true,
12273                                          /*ambiguous_decls=*/NULL,
12274                                          token->location);
12275             /* See if the default argument is valid.  */
12276             default_argument
12277               = check_template_template_default_arg (default_argument);
12278
12279             /* Template parameter packs cannot have default
12280                arguments. */
12281             if (*is_parameter_pack)
12282               {
12283                 if (identifier)
12284                   error_at (token->location,
12285                             "template parameter pack %qD cannot "
12286                             "have a default argument",
12287                             identifier);
12288                 else
12289                   error_at (token->location, "template parameter packs cannot "
12290                             "have default arguments");
12291                 default_argument = NULL_TREE;
12292               }
12293             pop_deferring_access_checks ();
12294           }
12295         else
12296           default_argument = NULL_TREE;
12297
12298         /* Create the combined representation of the parameter and the
12299            default argument.  */
12300         parameter = build_tree_list (default_argument, parameter);
12301       }
12302       break;
12303
12304     default:
12305       gcc_unreachable ();
12306       break;
12307     }
12308
12309   return parameter;
12310 }
12311
12312 /* Parse a template-id.
12313
12314    template-id:
12315      template-name < template-argument-list [opt] >
12316
12317    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12318    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12319    returned.  Otherwise, if the template-name names a function, or set
12320    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12321    names a class, returns a TYPE_DECL for the specialization.
12322
12323    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12324    uninstantiated templates.  */
12325
12326 static tree
12327 cp_parser_template_id (cp_parser *parser,
12328                        bool template_keyword_p,
12329                        bool check_dependency_p,
12330                        bool is_declaration)
12331 {
12332   int i;
12333   tree templ;
12334   tree arguments;
12335   tree template_id;
12336   cp_token_position start_of_id = 0;
12337   deferred_access_check *chk;
12338   VEC (deferred_access_check,gc) *access_check;
12339   cp_token *next_token = NULL, *next_token_2 = NULL;
12340   bool is_identifier;
12341
12342   /* If the next token corresponds to a template-id, there is no need
12343      to reparse it.  */
12344   next_token = cp_lexer_peek_token (parser->lexer);
12345   if (next_token->type == CPP_TEMPLATE_ID)
12346     {
12347       struct tree_check *check_value;
12348
12349       /* Get the stored value.  */
12350       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12351       /* Perform any access checks that were deferred.  */
12352       access_check = check_value->checks;
12353       if (access_check)
12354         {
12355           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12356             perform_or_defer_access_check (chk->binfo,
12357                                            chk->decl,
12358                                            chk->diag_decl);
12359         }
12360       /* Return the stored value.  */
12361       return check_value->value;
12362     }
12363
12364   /* Avoid performing name lookup if there is no possibility of
12365      finding a template-id.  */
12366   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12367       || (next_token->type == CPP_NAME
12368           && !cp_parser_nth_token_starts_template_argument_list_p
12369                (parser, 2)))
12370     {
12371       cp_parser_error (parser, "expected template-id");
12372       return error_mark_node;
12373     }
12374
12375   /* Remember where the template-id starts.  */
12376   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12377     start_of_id = cp_lexer_token_position (parser->lexer, false);
12378
12379   push_deferring_access_checks (dk_deferred);
12380
12381   /* Parse the template-name.  */
12382   is_identifier = false;
12383   templ = cp_parser_template_name (parser, template_keyword_p,
12384                                    check_dependency_p,
12385                                    is_declaration,
12386                                    &is_identifier);
12387   if (templ == error_mark_node || is_identifier)
12388     {
12389       pop_deferring_access_checks ();
12390       return templ;
12391     }
12392
12393   /* If we find the sequence `[:' after a template-name, it's probably
12394      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12395      parse correctly the argument list.  */
12396   next_token = cp_lexer_peek_token (parser->lexer);
12397   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12398   if (next_token->type == CPP_OPEN_SQUARE
12399       && next_token->flags & DIGRAPH
12400       && next_token_2->type == CPP_COLON
12401       && !(next_token_2->flags & PREV_WHITE))
12402     {
12403       cp_parser_parse_tentatively (parser);
12404       /* Change `:' into `::'.  */
12405       next_token_2->type = CPP_SCOPE;
12406       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12407          CPP_LESS.  */
12408       cp_lexer_consume_token (parser->lexer);
12409
12410       /* Parse the arguments.  */
12411       arguments = cp_parser_enclosed_template_argument_list (parser);
12412       if (!cp_parser_parse_definitely (parser))
12413         {
12414           /* If we couldn't parse an argument list, then we revert our changes
12415              and return simply an error. Maybe this is not a template-id
12416              after all.  */
12417           next_token_2->type = CPP_COLON;
12418           cp_parser_error (parser, "expected %<<%>");
12419           pop_deferring_access_checks ();
12420           return error_mark_node;
12421         }
12422       /* Otherwise, emit an error about the invalid digraph, but continue
12423          parsing because we got our argument list.  */
12424       if (permerror (next_token->location,
12425                      "%<<::%> cannot begin a template-argument list"))
12426         {
12427           static bool hint = false;
12428           inform (next_token->location,
12429                   "%<<:%> is an alternate spelling for %<[%>."
12430                   " Insert whitespace between %<<%> and %<::%>");
12431           if (!hint && !flag_permissive)
12432             {
12433               inform (next_token->location, "(if you use %<-fpermissive%>"
12434                       " G++ will accept your code)");
12435               hint = true;
12436             }
12437         }
12438     }
12439   else
12440     {
12441       /* Look for the `<' that starts the template-argument-list.  */
12442       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12443         {
12444           pop_deferring_access_checks ();
12445           return error_mark_node;
12446         }
12447       /* Parse the arguments.  */
12448       arguments = cp_parser_enclosed_template_argument_list (parser);
12449     }
12450
12451   /* Build a representation of the specialization.  */
12452   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12453     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12454   else if (DECL_TYPE_TEMPLATE_P (templ)
12455            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12456     {
12457       bool entering_scope;
12458       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12459          template (rather than some instantiation thereof) only if
12460          is not nested within some other construct.  For example, in
12461          "template <typename T> void f(T) { A<T>::", A<T> is just an
12462          instantiation of A.  */
12463       entering_scope = (template_parm_scope_p ()
12464                         && cp_lexer_next_token_is (parser->lexer,
12465                                                    CPP_SCOPE));
12466       template_id
12467         = finish_template_type (templ, arguments, entering_scope);
12468     }
12469   else
12470     {
12471       /* If it's not a class-template or a template-template, it should be
12472          a function-template.  */
12473       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12474                    || TREE_CODE (templ) == OVERLOAD
12475                    || BASELINK_P (templ)));
12476
12477       template_id = lookup_template_function (templ, arguments);
12478     }
12479
12480   /* If parsing tentatively, replace the sequence of tokens that makes
12481      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12482      should we re-parse the token stream, we will not have to repeat
12483      the effort required to do the parse, nor will we issue duplicate
12484      error messages about problems during instantiation of the
12485      template.  */
12486   if (start_of_id)
12487     {
12488       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12489
12490       /* Reset the contents of the START_OF_ID token.  */
12491       token->type = CPP_TEMPLATE_ID;
12492       /* Retrieve any deferred checks.  Do not pop this access checks yet
12493          so the memory will not be reclaimed during token replacing below.  */
12494       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12495       token->u.tree_check_value->value = template_id;
12496       token->u.tree_check_value->checks = get_deferred_access_checks ();
12497       token->keyword = RID_MAX;
12498
12499       /* Purge all subsequent tokens.  */
12500       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12501
12502       /* ??? Can we actually assume that, if template_id ==
12503          error_mark_node, we will have issued a diagnostic to the
12504          user, as opposed to simply marking the tentative parse as
12505          failed?  */
12506       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12507         error_at (token->location, "parse error in template argument list");
12508     }
12509
12510   pop_deferring_access_checks ();
12511   return template_id;
12512 }
12513
12514 /* Parse a template-name.
12515
12516    template-name:
12517      identifier
12518
12519    The standard should actually say:
12520
12521    template-name:
12522      identifier
12523      operator-function-id
12524
12525    A defect report has been filed about this issue.
12526
12527    A conversion-function-id cannot be a template name because they cannot
12528    be part of a template-id. In fact, looking at this code:
12529
12530    a.operator K<int>()
12531
12532    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12533    It is impossible to call a templated conversion-function-id with an
12534    explicit argument list, since the only allowed template parameter is
12535    the type to which it is converting.
12536
12537    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12538    `template' keyword, in a construction like:
12539
12540      T::template f<3>()
12541
12542    In that case `f' is taken to be a template-name, even though there
12543    is no way of knowing for sure.
12544
12545    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12546    name refers to a set of overloaded functions, at least one of which
12547    is a template, or an IDENTIFIER_NODE with the name of the template,
12548    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12549    names are looked up inside uninstantiated templates.  */
12550
12551 static tree
12552 cp_parser_template_name (cp_parser* parser,
12553                          bool template_keyword_p,
12554                          bool check_dependency_p,
12555                          bool is_declaration,
12556                          bool *is_identifier)
12557 {
12558   tree identifier;
12559   tree decl;
12560   tree fns;
12561   cp_token *token = cp_lexer_peek_token (parser->lexer);
12562
12563   /* If the next token is `operator', then we have either an
12564      operator-function-id or a conversion-function-id.  */
12565   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12566     {
12567       /* We don't know whether we're looking at an
12568          operator-function-id or a conversion-function-id.  */
12569       cp_parser_parse_tentatively (parser);
12570       /* Try an operator-function-id.  */
12571       identifier = cp_parser_operator_function_id (parser);
12572       /* If that didn't work, try a conversion-function-id.  */
12573       if (!cp_parser_parse_definitely (parser))
12574         {
12575           cp_parser_error (parser, "expected template-name");
12576           return error_mark_node;
12577         }
12578     }
12579   /* Look for the identifier.  */
12580   else
12581     identifier = cp_parser_identifier (parser);
12582
12583   /* If we didn't find an identifier, we don't have a template-id.  */
12584   if (identifier == error_mark_node)
12585     return error_mark_node;
12586
12587   /* If the name immediately followed the `template' keyword, then it
12588      is a template-name.  However, if the next token is not `<', then
12589      we do not treat it as a template-name, since it is not being used
12590      as part of a template-id.  This enables us to handle constructs
12591      like:
12592
12593        template <typename T> struct S { S(); };
12594        template <typename T> S<T>::S();
12595
12596      correctly.  We would treat `S' as a template -- if it were `S<T>'
12597      -- but we do not if there is no `<'.  */
12598
12599   if (processing_template_decl
12600       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12601     {
12602       /* In a declaration, in a dependent context, we pretend that the
12603          "template" keyword was present in order to improve error
12604          recovery.  For example, given:
12605
12606            template <typename T> void f(T::X<int>);
12607
12608          we want to treat "X<int>" as a template-id.  */
12609       if (is_declaration
12610           && !template_keyword_p
12611           && parser->scope && TYPE_P (parser->scope)
12612           && check_dependency_p
12613           && dependent_scope_p (parser->scope)
12614           /* Do not do this for dtors (or ctors), since they never
12615              need the template keyword before their name.  */
12616           && !constructor_name_p (identifier, parser->scope))
12617         {
12618           cp_token_position start = 0;
12619
12620           /* Explain what went wrong.  */
12621           error_at (token->location, "non-template %qD used as template",
12622                     identifier);
12623           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12624                   parser->scope, identifier);
12625           /* If parsing tentatively, find the location of the "<" token.  */
12626           if (cp_parser_simulate_error (parser))
12627             start = cp_lexer_token_position (parser->lexer, true);
12628           /* Parse the template arguments so that we can issue error
12629              messages about them.  */
12630           cp_lexer_consume_token (parser->lexer);
12631           cp_parser_enclosed_template_argument_list (parser);
12632           /* Skip tokens until we find a good place from which to
12633              continue parsing.  */
12634           cp_parser_skip_to_closing_parenthesis (parser,
12635                                                  /*recovering=*/true,
12636                                                  /*or_comma=*/true,
12637                                                  /*consume_paren=*/false);
12638           /* If parsing tentatively, permanently remove the
12639              template argument list.  That will prevent duplicate
12640              error messages from being issued about the missing
12641              "template" keyword.  */
12642           if (start)
12643             cp_lexer_purge_tokens_after (parser->lexer, start);
12644           if (is_identifier)
12645             *is_identifier = true;
12646           return identifier;
12647         }
12648
12649       /* If the "template" keyword is present, then there is generally
12650          no point in doing name-lookup, so we just return IDENTIFIER.
12651          But, if the qualifying scope is non-dependent then we can
12652          (and must) do name-lookup normally.  */
12653       if (template_keyword_p
12654           && (!parser->scope
12655               || (TYPE_P (parser->scope)
12656                   && dependent_type_p (parser->scope))))
12657         return identifier;
12658     }
12659
12660   /* Look up the name.  */
12661   decl = cp_parser_lookup_name (parser, identifier,
12662                                 none_type,
12663                                 /*is_template=*/true,
12664                                 /*is_namespace=*/false,
12665                                 check_dependency_p,
12666                                 /*ambiguous_decls=*/NULL,
12667                                 token->location);
12668
12669   /* If DECL is a template, then the name was a template-name.  */
12670   if (TREE_CODE (decl) == TEMPLATE_DECL)
12671     ;
12672   else
12673     {
12674       tree fn = NULL_TREE;
12675
12676       /* The standard does not explicitly indicate whether a name that
12677          names a set of overloaded declarations, some of which are
12678          templates, is a template-name.  However, such a name should
12679          be a template-name; otherwise, there is no way to form a
12680          template-id for the overloaded templates.  */
12681       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12682       if (TREE_CODE (fns) == OVERLOAD)
12683         for (fn = fns; fn; fn = OVL_NEXT (fn))
12684           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12685             break;
12686
12687       if (!fn)
12688         {
12689           /* The name does not name a template.  */
12690           cp_parser_error (parser, "expected template-name");
12691           return error_mark_node;
12692         }
12693     }
12694
12695   /* If DECL is dependent, and refers to a function, then just return
12696      its name; we will look it up again during template instantiation.  */
12697   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12698     {
12699       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12700       if (TYPE_P (scope) && dependent_type_p (scope))
12701         return identifier;
12702     }
12703
12704   return decl;
12705 }
12706
12707 /* Parse a template-argument-list.
12708
12709    template-argument-list:
12710      template-argument ... [opt]
12711      template-argument-list , template-argument ... [opt]
12712
12713    Returns a TREE_VEC containing the arguments.  */
12714
12715 static tree
12716 cp_parser_template_argument_list (cp_parser* parser)
12717 {
12718   tree fixed_args[10];
12719   unsigned n_args = 0;
12720   unsigned alloced = 10;
12721   tree *arg_ary = fixed_args;
12722   tree vec;
12723   bool saved_in_template_argument_list_p;
12724   bool saved_ice_p;
12725   bool saved_non_ice_p;
12726
12727   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12728   parser->in_template_argument_list_p = true;
12729   /* Even if the template-id appears in an integral
12730      constant-expression, the contents of the argument list do
12731      not.  */
12732   saved_ice_p = parser->integral_constant_expression_p;
12733   parser->integral_constant_expression_p = false;
12734   saved_non_ice_p = parser->non_integral_constant_expression_p;
12735   parser->non_integral_constant_expression_p = false;
12736
12737   /* Parse the arguments.  */
12738   do
12739     {
12740       tree argument;
12741
12742       if (n_args)
12743         /* Consume the comma.  */
12744         cp_lexer_consume_token (parser->lexer);
12745
12746       /* Parse the template-argument.  */
12747       argument = cp_parser_template_argument (parser);
12748
12749       /* If the next token is an ellipsis, we're expanding a template
12750          argument pack. */
12751       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12752         {
12753           if (argument == error_mark_node)
12754             {
12755               cp_token *token = cp_lexer_peek_token (parser->lexer);
12756               error_at (token->location,
12757                         "expected parameter pack before %<...%>");
12758             }
12759           /* Consume the `...' token. */
12760           cp_lexer_consume_token (parser->lexer);
12761
12762           /* Make the argument into a TYPE_PACK_EXPANSION or
12763              EXPR_PACK_EXPANSION. */
12764           argument = make_pack_expansion (argument);
12765         }
12766
12767       if (n_args == alloced)
12768         {
12769           alloced *= 2;
12770
12771           if (arg_ary == fixed_args)
12772             {
12773               arg_ary = XNEWVEC (tree, alloced);
12774               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12775             }
12776           else
12777             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12778         }
12779       arg_ary[n_args++] = argument;
12780     }
12781   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12782
12783   vec = make_tree_vec (n_args);
12784
12785   while (n_args--)
12786     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12787
12788   if (arg_ary != fixed_args)
12789     free (arg_ary);
12790   parser->non_integral_constant_expression_p = saved_non_ice_p;
12791   parser->integral_constant_expression_p = saved_ice_p;
12792   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12793 #ifdef ENABLE_CHECKING
12794   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12795 #endif
12796   return vec;
12797 }
12798
12799 /* Parse a template-argument.
12800
12801    template-argument:
12802      assignment-expression
12803      type-id
12804      id-expression
12805
12806    The representation is that of an assignment-expression, type-id, or
12807    id-expression -- except that the qualified id-expression is
12808    evaluated, so that the value returned is either a DECL or an
12809    OVERLOAD.
12810
12811    Although the standard says "assignment-expression", it forbids
12812    throw-expressions or assignments in the template argument.
12813    Therefore, we use "conditional-expression" instead.  */
12814
12815 static tree
12816 cp_parser_template_argument (cp_parser* parser)
12817 {
12818   tree argument;
12819   bool template_p;
12820   bool address_p;
12821   bool maybe_type_id = false;
12822   cp_token *token = NULL, *argument_start_token = NULL;
12823   cp_id_kind idk;
12824
12825   /* There's really no way to know what we're looking at, so we just
12826      try each alternative in order.
12827
12828        [temp.arg]
12829
12830        In a template-argument, an ambiguity between a type-id and an
12831        expression is resolved to a type-id, regardless of the form of
12832        the corresponding template-parameter.
12833
12834      Therefore, we try a type-id first.  */
12835   cp_parser_parse_tentatively (parser);
12836   argument = cp_parser_template_type_arg (parser);
12837   /* If there was no error parsing the type-id but the next token is a
12838      '>>', our behavior depends on which dialect of C++ we're
12839      parsing. In C++98, we probably found a typo for '> >'. But there
12840      are type-id which are also valid expressions. For instance:
12841
12842      struct X { int operator >> (int); };
12843      template <int V> struct Foo {};
12844      Foo<X () >> 5> r;
12845
12846      Here 'X()' is a valid type-id of a function type, but the user just
12847      wanted to write the expression "X() >> 5". Thus, we remember that we
12848      found a valid type-id, but we still try to parse the argument as an
12849      expression to see what happens. 
12850
12851      In C++0x, the '>>' will be considered two separate '>'
12852      tokens.  */
12853   if (!cp_parser_error_occurred (parser)
12854       && cxx_dialect == cxx98
12855       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12856     {
12857       maybe_type_id = true;
12858       cp_parser_abort_tentative_parse (parser);
12859     }
12860   else
12861     {
12862       /* If the next token isn't a `,' or a `>', then this argument wasn't
12863       really finished. This means that the argument is not a valid
12864       type-id.  */
12865       if (!cp_parser_next_token_ends_template_argument_p (parser))
12866         cp_parser_error (parser, "expected template-argument");
12867       /* If that worked, we're done.  */
12868       if (cp_parser_parse_definitely (parser))
12869         return argument;
12870     }
12871   /* We're still not sure what the argument will be.  */
12872   cp_parser_parse_tentatively (parser);
12873   /* Try a template.  */
12874   argument_start_token = cp_lexer_peek_token (parser->lexer);
12875   argument = cp_parser_id_expression (parser,
12876                                       /*template_keyword_p=*/false,
12877                                       /*check_dependency_p=*/true,
12878                                       &template_p,
12879                                       /*declarator_p=*/false,
12880                                       /*optional_p=*/false);
12881   /* If the next token isn't a `,' or a `>', then this argument wasn't
12882      really finished.  */
12883   if (!cp_parser_next_token_ends_template_argument_p (parser))
12884     cp_parser_error (parser, "expected template-argument");
12885   if (!cp_parser_error_occurred (parser))
12886     {
12887       /* Figure out what is being referred to.  If the id-expression
12888          was for a class template specialization, then we will have a
12889          TYPE_DECL at this point.  There is no need to do name lookup
12890          at this point in that case.  */
12891       if (TREE_CODE (argument) != TYPE_DECL)
12892         argument = cp_parser_lookup_name (parser, argument,
12893                                           none_type,
12894                                           /*is_template=*/template_p,
12895                                           /*is_namespace=*/false,
12896                                           /*check_dependency=*/true,
12897                                           /*ambiguous_decls=*/NULL,
12898                                           argument_start_token->location);
12899       if (TREE_CODE (argument) != TEMPLATE_DECL
12900           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12901         cp_parser_error (parser, "expected template-name");
12902     }
12903   if (cp_parser_parse_definitely (parser))
12904     return argument;
12905   /* It must be a non-type argument.  There permitted cases are given
12906      in [temp.arg.nontype]:
12907
12908      -- an integral constant-expression of integral or enumeration
12909         type; or
12910
12911      -- the name of a non-type template-parameter; or
12912
12913      -- the name of an object or function with external linkage...
12914
12915      -- the address of an object or function with external linkage...
12916
12917      -- a pointer to member...  */
12918   /* Look for a non-type template parameter.  */
12919   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12920     {
12921       cp_parser_parse_tentatively (parser);
12922       argument = cp_parser_primary_expression (parser,
12923                                                /*address_p=*/false,
12924                                                /*cast_p=*/false,
12925                                                /*template_arg_p=*/true,
12926                                                &idk);
12927       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12928           || !cp_parser_next_token_ends_template_argument_p (parser))
12929         cp_parser_simulate_error (parser);
12930       if (cp_parser_parse_definitely (parser))
12931         return argument;
12932     }
12933
12934   /* If the next token is "&", the argument must be the address of an
12935      object or function with external linkage.  */
12936   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12937   if (address_p)
12938     cp_lexer_consume_token (parser->lexer);
12939   /* See if we might have an id-expression.  */
12940   token = cp_lexer_peek_token (parser->lexer);
12941   if (token->type == CPP_NAME
12942       || token->keyword == RID_OPERATOR
12943       || token->type == CPP_SCOPE
12944       || token->type == CPP_TEMPLATE_ID
12945       || token->type == CPP_NESTED_NAME_SPECIFIER)
12946     {
12947       cp_parser_parse_tentatively (parser);
12948       argument = cp_parser_primary_expression (parser,
12949                                                address_p,
12950                                                /*cast_p=*/false,
12951                                                /*template_arg_p=*/true,
12952                                                &idk);
12953       if (cp_parser_error_occurred (parser)
12954           || !cp_parser_next_token_ends_template_argument_p (parser))
12955         cp_parser_abort_tentative_parse (parser);
12956       else
12957         {
12958           tree probe;
12959
12960           if (TREE_CODE (argument) == INDIRECT_REF)
12961             {
12962               gcc_assert (REFERENCE_REF_P (argument));
12963               argument = TREE_OPERAND (argument, 0);
12964             }
12965
12966           /* If we're in a template, we represent a qualified-id referring
12967              to a static data member as a SCOPE_REF even if the scope isn't
12968              dependent so that we can check access control later.  */
12969           probe = argument;
12970           if (TREE_CODE (probe) == SCOPE_REF)
12971             probe = TREE_OPERAND (probe, 1);
12972           if (TREE_CODE (probe) == VAR_DECL)
12973             {
12974               /* A variable without external linkage might still be a
12975                  valid constant-expression, so no error is issued here
12976                  if the external-linkage check fails.  */
12977               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12978                 cp_parser_simulate_error (parser);
12979             }
12980           else if (is_overloaded_fn (argument))
12981             /* All overloaded functions are allowed; if the external
12982                linkage test does not pass, an error will be issued
12983                later.  */
12984             ;
12985           else if (address_p
12986                    && (TREE_CODE (argument) == OFFSET_REF
12987                        || TREE_CODE (argument) == SCOPE_REF))
12988             /* A pointer-to-member.  */
12989             ;
12990           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12991             ;
12992           else
12993             cp_parser_simulate_error (parser);
12994
12995           if (cp_parser_parse_definitely (parser))
12996             {
12997               if (address_p)
12998                 argument = build_x_unary_op (ADDR_EXPR, argument,
12999                                              tf_warning_or_error);
13000               return argument;
13001             }
13002         }
13003     }
13004   /* If the argument started with "&", there are no other valid
13005      alternatives at this point.  */
13006   if (address_p)
13007     {
13008       cp_parser_error (parser, "invalid non-type template argument");
13009       return error_mark_node;
13010     }
13011
13012   /* If the argument wasn't successfully parsed as a type-id followed
13013      by '>>', the argument can only be a constant expression now.
13014      Otherwise, we try parsing the constant-expression tentatively,
13015      because the argument could really be a type-id.  */
13016   if (maybe_type_id)
13017     cp_parser_parse_tentatively (parser);
13018   argument = cp_parser_constant_expression (parser,
13019                                             /*allow_non_constant_p=*/false,
13020                                             /*non_constant_p=*/NULL);
13021   argument = fold_non_dependent_expr (argument);
13022   if (!maybe_type_id)
13023     return argument;
13024   if (!cp_parser_next_token_ends_template_argument_p (parser))
13025     cp_parser_error (parser, "expected template-argument");
13026   if (cp_parser_parse_definitely (parser))
13027     return argument;
13028   /* We did our best to parse the argument as a non type-id, but that
13029      was the only alternative that matched (albeit with a '>' after
13030      it). We can assume it's just a typo from the user, and a
13031      diagnostic will then be issued.  */
13032   return cp_parser_template_type_arg (parser);
13033 }
13034
13035 /* Parse an explicit-instantiation.
13036
13037    explicit-instantiation:
13038      template declaration
13039
13040    Although the standard says `declaration', what it really means is:
13041
13042    explicit-instantiation:
13043      template decl-specifier-seq [opt] declarator [opt] ;
13044
13045    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13046    supposed to be allowed.  A defect report has been filed about this
13047    issue.
13048
13049    GNU Extension:
13050
13051    explicit-instantiation:
13052      storage-class-specifier template
13053        decl-specifier-seq [opt] declarator [opt] ;
13054      function-specifier template
13055        decl-specifier-seq [opt] declarator [opt] ;  */
13056
13057 static void
13058 cp_parser_explicit_instantiation (cp_parser* parser)
13059 {
13060   int declares_class_or_enum;
13061   cp_decl_specifier_seq decl_specifiers;
13062   tree extension_specifier = NULL_TREE;
13063
13064   timevar_push (TV_TEMPLATE_INST);
13065
13066   /* Look for an (optional) storage-class-specifier or
13067      function-specifier.  */
13068   if (cp_parser_allow_gnu_extensions_p (parser))
13069     {
13070       extension_specifier
13071         = cp_parser_storage_class_specifier_opt (parser);
13072       if (!extension_specifier)
13073         extension_specifier
13074           = cp_parser_function_specifier_opt (parser,
13075                                               /*decl_specs=*/NULL);
13076     }
13077
13078   /* Look for the `template' keyword.  */
13079   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13080   /* Let the front end know that we are processing an explicit
13081      instantiation.  */
13082   begin_explicit_instantiation ();
13083   /* [temp.explicit] says that we are supposed to ignore access
13084      control while processing explicit instantiation directives.  */
13085   push_deferring_access_checks (dk_no_check);
13086   /* Parse a decl-specifier-seq.  */
13087   cp_parser_decl_specifier_seq (parser,
13088                                 CP_PARSER_FLAGS_OPTIONAL,
13089                                 &decl_specifiers,
13090                                 &declares_class_or_enum);
13091   /* If there was exactly one decl-specifier, and it declared a class,
13092      and there's no declarator, then we have an explicit type
13093      instantiation.  */
13094   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13095     {
13096       tree type;
13097
13098       type = check_tag_decl (&decl_specifiers);
13099       /* Turn access control back on for names used during
13100          template instantiation.  */
13101       pop_deferring_access_checks ();
13102       if (type)
13103         do_type_instantiation (type, extension_specifier,
13104                                /*complain=*/tf_error);
13105     }
13106   else
13107     {
13108       cp_declarator *declarator;
13109       tree decl;
13110
13111       /* Parse the declarator.  */
13112       declarator
13113         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13114                                 /*ctor_dtor_or_conv_p=*/NULL,
13115                                 /*parenthesized_p=*/NULL,
13116                                 /*member_p=*/false);
13117       if (declares_class_or_enum & 2)
13118         cp_parser_check_for_definition_in_return_type (declarator,
13119                                                        decl_specifiers.type,
13120                                                        decl_specifiers.type_location);
13121       if (declarator != cp_error_declarator)
13122         {
13123           if (decl_specifiers.specs[(int)ds_inline])
13124             permerror (input_location, "explicit instantiation shall not use"
13125                        " %<inline%> specifier");
13126           if (decl_specifiers.specs[(int)ds_constexpr])
13127             permerror (input_location, "explicit instantiation shall not use"
13128                        " %<constexpr%> specifier");
13129
13130           decl = grokdeclarator (declarator, &decl_specifiers,
13131                                  NORMAL, 0, &decl_specifiers.attributes);
13132           /* Turn access control back on for names used during
13133              template instantiation.  */
13134           pop_deferring_access_checks ();
13135           /* Do the explicit instantiation.  */
13136           do_decl_instantiation (decl, extension_specifier);
13137         }
13138       else
13139         {
13140           pop_deferring_access_checks ();
13141           /* Skip the body of the explicit instantiation.  */
13142           cp_parser_skip_to_end_of_statement (parser);
13143         }
13144     }
13145   /* We're done with the instantiation.  */
13146   end_explicit_instantiation ();
13147
13148   cp_parser_consume_semicolon_at_end_of_statement (parser);
13149
13150   timevar_pop (TV_TEMPLATE_INST);
13151 }
13152
13153 /* Parse an explicit-specialization.
13154
13155    explicit-specialization:
13156      template < > declaration
13157
13158    Although the standard says `declaration', what it really means is:
13159
13160    explicit-specialization:
13161      template <> decl-specifier [opt] init-declarator [opt] ;
13162      template <> function-definition
13163      template <> explicit-specialization
13164      template <> template-declaration  */
13165
13166 static void
13167 cp_parser_explicit_specialization (cp_parser* parser)
13168 {
13169   bool need_lang_pop;
13170   cp_token *token = cp_lexer_peek_token (parser->lexer);
13171
13172   /* Look for the `template' keyword.  */
13173   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13174   /* Look for the `<'.  */
13175   cp_parser_require (parser, CPP_LESS, RT_LESS);
13176   /* Look for the `>'.  */
13177   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13178   /* We have processed another parameter list.  */
13179   ++parser->num_template_parameter_lists;
13180   /* [temp]
13181
13182      A template ... explicit specialization ... shall not have C
13183      linkage.  */
13184   if (current_lang_name == lang_name_c)
13185     {
13186       error_at (token->location, "template specialization with C linkage");
13187       /* Give it C++ linkage to avoid confusing other parts of the
13188          front end.  */
13189       push_lang_context (lang_name_cplusplus);
13190       need_lang_pop = true;
13191     }
13192   else
13193     need_lang_pop = false;
13194   /* Let the front end know that we are beginning a specialization.  */
13195   if (!begin_specialization ())
13196     {
13197       end_specialization ();
13198       return;
13199     }
13200
13201   /* If the next keyword is `template', we need to figure out whether
13202      or not we're looking a template-declaration.  */
13203   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13204     {
13205       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13206           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13207         cp_parser_template_declaration_after_export (parser,
13208                                                      /*member_p=*/false);
13209       else
13210         cp_parser_explicit_specialization (parser);
13211     }
13212   else
13213     /* Parse the dependent declaration.  */
13214     cp_parser_single_declaration (parser,
13215                                   /*checks=*/NULL,
13216                                   /*member_p=*/false,
13217                                   /*explicit_specialization_p=*/true,
13218                                   /*friend_p=*/NULL);
13219   /* We're done with the specialization.  */
13220   end_specialization ();
13221   /* For the erroneous case of a template with C linkage, we pushed an
13222      implicit C++ linkage scope; exit that scope now.  */
13223   if (need_lang_pop)
13224     pop_lang_context ();
13225   /* We're done with this parameter list.  */
13226   --parser->num_template_parameter_lists;
13227 }
13228
13229 /* Parse a type-specifier.
13230
13231    type-specifier:
13232      simple-type-specifier
13233      class-specifier
13234      enum-specifier
13235      elaborated-type-specifier
13236      cv-qualifier
13237
13238    GNU Extension:
13239
13240    type-specifier:
13241      __complex__
13242
13243    Returns a representation of the type-specifier.  For a
13244    class-specifier, enum-specifier, or elaborated-type-specifier, a
13245    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13246
13247    The parser flags FLAGS is used to control type-specifier parsing.
13248
13249    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13250    in a decl-specifier-seq.
13251
13252    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13253    class-specifier, enum-specifier, or elaborated-type-specifier, then
13254    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13255    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13256    zero.
13257
13258    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13259    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13260    is set to FALSE.  */
13261
13262 static tree
13263 cp_parser_type_specifier (cp_parser* parser,
13264                           cp_parser_flags flags,
13265                           cp_decl_specifier_seq *decl_specs,
13266                           bool is_declaration,
13267                           int* declares_class_or_enum,
13268                           bool* is_cv_qualifier)
13269 {
13270   tree type_spec = NULL_TREE;
13271   cp_token *token;
13272   enum rid keyword;
13273   cp_decl_spec ds = ds_last;
13274
13275   /* Assume this type-specifier does not declare a new type.  */
13276   if (declares_class_or_enum)
13277     *declares_class_or_enum = 0;
13278   /* And that it does not specify a cv-qualifier.  */
13279   if (is_cv_qualifier)
13280     *is_cv_qualifier = false;
13281   /* Peek at the next token.  */
13282   token = cp_lexer_peek_token (parser->lexer);
13283
13284   /* If we're looking at a keyword, we can use that to guide the
13285      production we choose.  */
13286   keyword = token->keyword;
13287   switch (keyword)
13288     {
13289     case RID_ENUM:
13290       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13291         goto elaborated_type_specifier;
13292
13293       /* Look for the enum-specifier.  */
13294       type_spec = cp_parser_enum_specifier (parser);
13295       /* If that worked, we're done.  */
13296       if (type_spec)
13297         {
13298           if (declares_class_or_enum)
13299             *declares_class_or_enum = 2;
13300           if (decl_specs)
13301             cp_parser_set_decl_spec_type (decl_specs,
13302                                           type_spec,
13303                                           token->location,
13304                                           /*type_definition_p=*/true);
13305           return type_spec;
13306         }
13307       else
13308         goto elaborated_type_specifier;
13309
13310       /* Any of these indicate either a class-specifier, or an
13311          elaborated-type-specifier.  */
13312     case RID_CLASS:
13313     case RID_STRUCT:
13314     case RID_UNION:
13315       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13316         goto elaborated_type_specifier;
13317
13318       /* Parse tentatively so that we can back up if we don't find a
13319          class-specifier.  */
13320       cp_parser_parse_tentatively (parser);
13321       /* Look for the class-specifier.  */
13322       type_spec = cp_parser_class_specifier (parser);
13323       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13324       /* If that worked, we're done.  */
13325       if (cp_parser_parse_definitely (parser))
13326         {
13327           if (declares_class_or_enum)
13328             *declares_class_or_enum = 2;
13329           if (decl_specs)
13330             cp_parser_set_decl_spec_type (decl_specs,
13331                                           type_spec,
13332                                           token->location,
13333                                           /*type_definition_p=*/true);
13334           return type_spec;
13335         }
13336
13337       /* Fall through.  */
13338     elaborated_type_specifier:
13339       /* We're declaring (not defining) a class or enum.  */
13340       if (declares_class_or_enum)
13341         *declares_class_or_enum = 1;
13342
13343       /* Fall through.  */
13344     case RID_TYPENAME:
13345       /* Look for an elaborated-type-specifier.  */
13346       type_spec
13347         = (cp_parser_elaborated_type_specifier
13348            (parser,
13349             decl_specs && decl_specs->specs[(int) ds_friend],
13350             is_declaration));
13351       if (decl_specs)
13352         cp_parser_set_decl_spec_type (decl_specs,
13353                                       type_spec,
13354                                       token->location,
13355                                       /*type_definition_p=*/false);
13356       return type_spec;
13357
13358     case RID_CONST:
13359       ds = ds_const;
13360       if (is_cv_qualifier)
13361         *is_cv_qualifier = true;
13362       break;
13363
13364     case RID_VOLATILE:
13365       ds = ds_volatile;
13366       if (is_cv_qualifier)
13367         *is_cv_qualifier = true;
13368       break;
13369
13370     case RID_RESTRICT:
13371       ds = ds_restrict;
13372       if (is_cv_qualifier)
13373         *is_cv_qualifier = true;
13374       break;
13375
13376     case RID_COMPLEX:
13377       /* The `__complex__' keyword is a GNU extension.  */
13378       ds = ds_complex;
13379       break;
13380
13381     default:
13382       break;
13383     }
13384
13385   /* Handle simple keywords.  */
13386   if (ds != ds_last)
13387     {
13388       if (decl_specs)
13389         {
13390           ++decl_specs->specs[(int)ds];
13391           decl_specs->any_specifiers_p = true;
13392         }
13393       return cp_lexer_consume_token (parser->lexer)->u.value;
13394     }
13395
13396   /* If we do not already have a type-specifier, assume we are looking
13397      at a simple-type-specifier.  */
13398   type_spec = cp_parser_simple_type_specifier (parser,
13399                                                decl_specs,
13400                                                flags);
13401
13402   /* If we didn't find a type-specifier, and a type-specifier was not
13403      optional in this context, issue an error message.  */
13404   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13405     {
13406       cp_parser_error (parser, "expected type specifier");
13407       return error_mark_node;
13408     }
13409
13410   return type_spec;
13411 }
13412
13413 /* Parse a simple-type-specifier.
13414
13415    simple-type-specifier:
13416      :: [opt] nested-name-specifier [opt] type-name
13417      :: [opt] nested-name-specifier template template-id
13418      char
13419      wchar_t
13420      bool
13421      short
13422      int
13423      long
13424      signed
13425      unsigned
13426      float
13427      double
13428      void
13429
13430    C++0x Extension:
13431
13432    simple-type-specifier:
13433      auto
13434      decltype ( expression )   
13435      char16_t
13436      char32_t
13437      __underlying_type ( type-id )
13438
13439    GNU Extension:
13440
13441    simple-type-specifier:
13442      __int128
13443      __typeof__ unary-expression
13444      __typeof__ ( type-id )
13445
13446    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13447    appropriately updated.  */
13448
13449 static tree
13450 cp_parser_simple_type_specifier (cp_parser* parser,
13451                                  cp_decl_specifier_seq *decl_specs,
13452                                  cp_parser_flags flags)
13453 {
13454   tree type = NULL_TREE;
13455   cp_token *token;
13456
13457   /* Peek at the next token.  */
13458   token = cp_lexer_peek_token (parser->lexer);
13459
13460   /* If we're looking at a keyword, things are easy.  */
13461   switch (token->keyword)
13462     {
13463     case RID_CHAR:
13464       if (decl_specs)
13465         decl_specs->explicit_char_p = true;
13466       type = char_type_node;
13467       break;
13468     case RID_CHAR16:
13469       type = char16_type_node;
13470       break;
13471     case RID_CHAR32:
13472       type = char32_type_node;
13473       break;
13474     case RID_WCHAR:
13475       type = wchar_type_node;
13476       break;
13477     case RID_BOOL:
13478       type = boolean_type_node;
13479       break;
13480     case RID_SHORT:
13481       if (decl_specs)
13482         ++decl_specs->specs[(int) ds_short];
13483       type = short_integer_type_node;
13484       break;
13485     case RID_INT:
13486       if (decl_specs)
13487         decl_specs->explicit_int_p = true;
13488       type = integer_type_node;
13489       break;
13490     case RID_INT128:
13491       if (!int128_integer_type_node)
13492         break;
13493       if (decl_specs)
13494         decl_specs->explicit_int128_p = true;
13495       type = int128_integer_type_node;
13496       break;
13497     case RID_LONG:
13498       if (decl_specs)
13499         ++decl_specs->specs[(int) ds_long];
13500       type = long_integer_type_node;
13501       break;
13502     case RID_SIGNED:
13503       if (decl_specs)
13504         ++decl_specs->specs[(int) ds_signed];
13505       type = integer_type_node;
13506       break;
13507     case RID_UNSIGNED:
13508       if (decl_specs)
13509         ++decl_specs->specs[(int) ds_unsigned];
13510       type = unsigned_type_node;
13511       break;
13512     case RID_FLOAT:
13513       type = float_type_node;
13514       break;
13515     case RID_DOUBLE:
13516       type = double_type_node;
13517       break;
13518     case RID_VOID:
13519       type = void_type_node;
13520       break;
13521       
13522     case RID_AUTO:
13523       maybe_warn_cpp0x (CPP0X_AUTO);
13524       type = make_auto ();
13525       break;
13526
13527     case RID_DECLTYPE:
13528       /* Since DR 743, decltype can either be a simple-type-specifier by
13529          itself or begin a nested-name-specifier.  Parsing it will replace
13530          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13531          handling below decide what to do.  */
13532       cp_parser_decltype (parser);
13533       cp_lexer_set_token_position (parser->lexer, token);
13534       break;
13535
13536     case RID_TYPEOF:
13537       /* Consume the `typeof' token.  */
13538       cp_lexer_consume_token (parser->lexer);
13539       /* Parse the operand to `typeof'.  */
13540       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13541       /* If it is not already a TYPE, take its type.  */
13542       if (!TYPE_P (type))
13543         type = finish_typeof (type);
13544
13545       if (decl_specs)
13546         cp_parser_set_decl_spec_type (decl_specs, type,
13547                                       token->location,
13548                                       /*type_definition_p=*/false);
13549
13550       return type;
13551
13552     case RID_UNDERLYING_TYPE:
13553       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13554       if (decl_specs)
13555         cp_parser_set_decl_spec_type (decl_specs, type,
13556                                       token->location,
13557                                       /*type_definition_p=*/false);
13558
13559       return type;
13560
13561     case RID_BASES:
13562     case RID_DIRECT_BASES:
13563       type = cp_parser_trait_expr (parser, token->keyword);
13564       if (decl_specs)
13565        cp_parser_set_decl_spec_type (decl_specs, type,
13566                                      token->location,
13567                                      /*type_definition_p=*/false);
13568       return type;
13569     default:
13570       break;
13571     }
13572
13573   /* If token is an already-parsed decltype not followed by ::,
13574      it's a simple-type-specifier.  */
13575   if (token->type == CPP_DECLTYPE
13576       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13577     {
13578       type = token->u.value;
13579       if (decl_specs)
13580         cp_parser_set_decl_spec_type (decl_specs, type,
13581                                       token->location,
13582                                       /*type_definition_p=*/false);
13583       cp_lexer_consume_token (parser->lexer);
13584       return type;
13585     }
13586
13587   /* If the type-specifier was for a built-in type, we're done.  */
13588   if (type)
13589     {
13590       /* Record the type.  */
13591       if (decl_specs
13592           && (token->keyword != RID_SIGNED
13593               && token->keyword != RID_UNSIGNED
13594               && token->keyword != RID_SHORT
13595               && token->keyword != RID_LONG))
13596         cp_parser_set_decl_spec_type (decl_specs,
13597                                       type,
13598                                       token->location,
13599                                       /*type_definition_p=*/false);
13600       if (decl_specs)
13601         decl_specs->any_specifiers_p = true;
13602
13603       /* Consume the token.  */
13604       cp_lexer_consume_token (parser->lexer);
13605
13606       /* There is no valid C++ program where a non-template type is
13607          followed by a "<".  That usually indicates that the user thought
13608          that the type was a template.  */
13609       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13610
13611       return TYPE_NAME (type);
13612     }
13613
13614   /* The type-specifier must be a user-defined type.  */
13615   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13616     {
13617       bool qualified_p;
13618       bool global_p;
13619
13620       /* Don't gobble tokens or issue error messages if this is an
13621          optional type-specifier.  */
13622       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13623         cp_parser_parse_tentatively (parser);
13624
13625       /* Look for the optional `::' operator.  */
13626       global_p
13627         = (cp_parser_global_scope_opt (parser,
13628                                        /*current_scope_valid_p=*/false)
13629            != NULL_TREE);
13630       /* Look for the nested-name specifier.  */
13631       qualified_p
13632         = (cp_parser_nested_name_specifier_opt (parser,
13633                                                 /*typename_keyword_p=*/false,
13634                                                 /*check_dependency_p=*/true,
13635                                                 /*type_p=*/false,
13636                                                 /*is_declaration=*/false)
13637            != NULL_TREE);
13638       token = cp_lexer_peek_token (parser->lexer);
13639       /* If we have seen a nested-name-specifier, and the next token
13640          is `template', then we are using the template-id production.  */
13641       if (parser->scope
13642           && cp_parser_optional_template_keyword (parser))
13643         {
13644           /* Look for the template-id.  */
13645           type = cp_parser_template_id (parser,
13646                                         /*template_keyword_p=*/true,
13647                                         /*check_dependency_p=*/true,
13648                                         /*is_declaration=*/false);
13649           /* If the template-id did not name a type, we are out of
13650              luck.  */
13651           if (TREE_CODE (type) != TYPE_DECL)
13652             {
13653               cp_parser_error (parser, "expected template-id for type");
13654               type = NULL_TREE;
13655             }
13656         }
13657       /* Otherwise, look for a type-name.  */
13658       else
13659         type = cp_parser_type_name (parser);
13660       /* Keep track of all name-lookups performed in class scopes.  */
13661       if (type
13662           && !global_p
13663           && !qualified_p
13664           && TREE_CODE (type) == TYPE_DECL
13665           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13666         maybe_note_name_used_in_class (DECL_NAME (type), type);
13667       /* If it didn't work out, we don't have a TYPE.  */
13668       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13669           && !cp_parser_parse_definitely (parser))
13670         type = NULL_TREE;
13671       if (type && decl_specs)
13672         cp_parser_set_decl_spec_type (decl_specs, type,
13673                                       token->location,
13674                                       /*type_definition_p=*/false);
13675     }
13676
13677   /* If we didn't get a type-name, issue an error message.  */
13678   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13679     {
13680       cp_parser_error (parser, "expected type-name");
13681       return error_mark_node;
13682     }
13683
13684   if (type && type != error_mark_node)
13685     {
13686       /* See if TYPE is an Objective-C type, and if so, parse and
13687          accept any protocol references following it.  Do this before
13688          the cp_parser_check_for_invalid_template_id() call, because
13689          Objective-C types can be followed by '<...>' which would
13690          enclose protocol names rather than template arguments, and so
13691          everything is fine.  */
13692       if (c_dialect_objc () && !parser->scope
13693           && (objc_is_id (type) || objc_is_class_name (type)))
13694         {
13695           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13696           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13697
13698           /* Clobber the "unqualified" type previously entered into
13699              DECL_SPECS with the new, improved protocol-qualified version.  */
13700           if (decl_specs)
13701             decl_specs->type = qual_type;
13702
13703           return qual_type;
13704         }
13705
13706       /* There is no valid C++ program where a non-template type is
13707          followed by a "<".  That usually indicates that the user
13708          thought that the type was a template.  */
13709       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13710                                                token->location);
13711     }
13712
13713   return type;
13714 }
13715
13716 /* Parse a type-name.
13717
13718    type-name:
13719      class-name
13720      enum-name
13721      typedef-name
13722      simple-template-id [in c++0x]
13723
13724    enum-name:
13725      identifier
13726
13727    typedef-name:
13728      identifier
13729
13730    Returns a TYPE_DECL for the type.  */
13731
13732 static tree
13733 cp_parser_type_name (cp_parser* parser)
13734 {
13735   tree type_decl;
13736
13737   /* We can't know yet whether it is a class-name or not.  */
13738   cp_parser_parse_tentatively (parser);
13739   /* Try a class-name.  */
13740   type_decl = cp_parser_class_name (parser,
13741                                     /*typename_keyword_p=*/false,
13742                                     /*template_keyword_p=*/false,
13743                                     none_type,
13744                                     /*check_dependency_p=*/true,
13745                                     /*class_head_p=*/false,
13746                                     /*is_declaration=*/false);
13747   /* If it's not a class-name, keep looking.  */
13748   if (!cp_parser_parse_definitely (parser))
13749     {
13750       if (cxx_dialect < cxx0x)
13751         /* It must be a typedef-name or an enum-name.  */
13752         return cp_parser_nonclass_name (parser);
13753
13754       cp_parser_parse_tentatively (parser);
13755       /* It is either a simple-template-id representing an
13756          instantiation of an alias template...  */
13757       type_decl = cp_parser_template_id (parser,
13758                                          /*template_keyword_p=*/false,
13759                                          /*check_dependency_p=*/false,
13760                                          /*is_declaration=*/false);
13761       /* Note that this must be an instantiation of an alias template
13762          because [temp.names]/6 says:
13763          
13764              A template-id that names an alias template specialization
13765              is a type-name.
13766
13767          Whereas [temp.names]/7 says:
13768          
13769              A simple-template-id that names a class template
13770              specialization is a class-name.  */
13771       if (type_decl != NULL_TREE
13772           && TREE_CODE (type_decl) == TYPE_DECL
13773           && TYPE_DECL_ALIAS_P (type_decl))
13774         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13775       else
13776         cp_parser_simulate_error (parser);
13777
13778       if (!cp_parser_parse_definitely (parser))
13779         /* ... Or a typedef-name or an enum-name.  */
13780         return cp_parser_nonclass_name (parser);
13781     }
13782
13783   return type_decl;
13784 }
13785
13786 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13787
13788    enum-name:
13789      identifier
13790
13791    typedef-name:
13792      identifier
13793
13794    Returns a TYPE_DECL for the type.  */
13795
13796 static tree
13797 cp_parser_nonclass_name (cp_parser* parser)
13798 {
13799   tree type_decl;
13800   tree identifier;
13801
13802   cp_token *token = cp_lexer_peek_token (parser->lexer);
13803   identifier = cp_parser_identifier (parser);
13804   if (identifier == error_mark_node)
13805     return error_mark_node;
13806
13807   /* Look up the type-name.  */
13808   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13809
13810   /* If it is a using decl, use its underlying decl.  */
13811   type_decl = strip_using_decl (type_decl);
13812
13813   if (TREE_CODE (type_decl) != TYPE_DECL
13814       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13815     {
13816       /* See if this is an Objective-C type.  */
13817       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13818       tree type = objc_get_protocol_qualified_type (identifier, protos);
13819       if (type)
13820         type_decl = TYPE_NAME (type);
13821     }
13822
13823   /* Issue an error if we did not find a type-name.  */
13824   if (TREE_CODE (type_decl) != TYPE_DECL
13825       /* In Objective-C, we have the complication that class names are
13826          normally type names and start declarations (eg, the
13827          "NSObject" in "NSObject *object;"), but can be used in an
13828          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13829          is an expression.  So, a classname followed by a dot is not a
13830          valid type-name.  */
13831       || (objc_is_class_name (TREE_TYPE (type_decl))
13832           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13833     {
13834       if (!cp_parser_simulate_error (parser))
13835         cp_parser_name_lookup_error (parser, identifier, type_decl,
13836                                      NLE_TYPE, token->location);
13837       return error_mark_node;
13838     }
13839   /* Remember that the name was used in the definition of the
13840      current class so that we can check later to see if the
13841      meaning would have been different after the class was
13842      entirely defined.  */
13843   else if (type_decl != error_mark_node
13844            && !parser->scope)
13845     maybe_note_name_used_in_class (identifier, type_decl);
13846   
13847   return type_decl;
13848 }
13849
13850 /* Parse an elaborated-type-specifier.  Note that the grammar given
13851    here incorporates the resolution to DR68.
13852
13853    elaborated-type-specifier:
13854      class-key :: [opt] nested-name-specifier [opt] identifier
13855      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13856      enum-key :: [opt] nested-name-specifier [opt] identifier
13857      typename :: [opt] nested-name-specifier identifier
13858      typename :: [opt] nested-name-specifier template [opt]
13859        template-id
13860
13861    GNU extension:
13862
13863    elaborated-type-specifier:
13864      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13865      class-key attributes :: [opt] nested-name-specifier [opt]
13866                template [opt] template-id
13867      enum attributes :: [opt] nested-name-specifier [opt] identifier
13868
13869    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13870    declared `friend'.  If IS_DECLARATION is TRUE, then this
13871    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13872    something is being declared.
13873
13874    Returns the TYPE specified.  */
13875
13876 static tree
13877 cp_parser_elaborated_type_specifier (cp_parser* parser,
13878                                      bool is_friend,
13879                                      bool is_declaration)
13880 {
13881   enum tag_types tag_type;
13882   tree identifier;
13883   tree type = NULL_TREE;
13884   tree attributes = NULL_TREE;
13885   tree globalscope;
13886   cp_token *token = NULL;
13887
13888   /* See if we're looking at the `enum' keyword.  */
13889   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13890     {
13891       /* Consume the `enum' token.  */
13892       cp_lexer_consume_token (parser->lexer);
13893       /* Remember that it's an enumeration type.  */
13894       tag_type = enum_type;
13895       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13896          enums) is used here.  */
13897       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13898           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13899         {
13900             pedwarn (input_location, 0, "elaborated-type-specifier "
13901                       "for a scoped enum must not use the %<%D%> keyword",
13902                       cp_lexer_peek_token (parser->lexer)->u.value);
13903           /* Consume the `struct' or `class' and parse it anyway.  */
13904           cp_lexer_consume_token (parser->lexer);
13905         }
13906       /* Parse the attributes.  */
13907       attributes = cp_parser_attributes_opt (parser);
13908     }
13909   /* Or, it might be `typename'.  */
13910   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13911                                            RID_TYPENAME))
13912     {
13913       /* Consume the `typename' token.  */
13914       cp_lexer_consume_token (parser->lexer);
13915       /* Remember that it's a `typename' type.  */
13916       tag_type = typename_type;
13917     }
13918   /* Otherwise it must be a class-key.  */
13919   else
13920     {
13921       tag_type = cp_parser_class_key (parser);
13922       if (tag_type == none_type)
13923         return error_mark_node;
13924       /* Parse the attributes.  */
13925       attributes = cp_parser_attributes_opt (parser);
13926     }
13927
13928   /* Look for the `::' operator.  */
13929   globalscope =  cp_parser_global_scope_opt (parser,
13930                                              /*current_scope_valid_p=*/false);
13931   /* Look for the nested-name-specifier.  */
13932   if (tag_type == typename_type && !globalscope)
13933     {
13934       if (!cp_parser_nested_name_specifier (parser,
13935                                            /*typename_keyword_p=*/true,
13936                                            /*check_dependency_p=*/true,
13937                                            /*type_p=*/true,
13938                                             is_declaration))
13939         return error_mark_node;
13940     }
13941   else
13942     /* Even though `typename' is not present, the proposed resolution
13943        to Core Issue 180 says that in `class A<T>::B', `B' should be
13944        considered a type-name, even if `A<T>' is dependent.  */
13945     cp_parser_nested_name_specifier_opt (parser,
13946                                          /*typename_keyword_p=*/true,
13947                                          /*check_dependency_p=*/true,
13948                                          /*type_p=*/true,
13949                                          is_declaration);
13950  /* For everything but enumeration types, consider a template-id.
13951     For an enumeration type, consider only a plain identifier.  */
13952   if (tag_type != enum_type)
13953     {
13954       bool template_p = false;
13955       tree decl;
13956
13957       /* Allow the `template' keyword.  */
13958       template_p = cp_parser_optional_template_keyword (parser);
13959       /* If we didn't see `template', we don't know if there's a
13960          template-id or not.  */
13961       if (!template_p)
13962         cp_parser_parse_tentatively (parser);
13963       /* Parse the template-id.  */
13964       token = cp_lexer_peek_token (parser->lexer);
13965       decl = cp_parser_template_id (parser, template_p,
13966                                     /*check_dependency_p=*/true,
13967                                     is_declaration);
13968       /* If we didn't find a template-id, look for an ordinary
13969          identifier.  */
13970       if (!template_p && !cp_parser_parse_definitely (parser))
13971         ;
13972       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13973          in effect, then we must assume that, upon instantiation, the
13974          template will correspond to a class.  */
13975       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13976                && tag_type == typename_type)
13977         type = make_typename_type (parser->scope, decl,
13978                                    typename_type,
13979                                    /*complain=*/tf_error);
13980       /* If the `typename' keyword is in effect and DECL is not a type
13981          decl. Then type is non existant.   */
13982       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13983         type = NULL_TREE; 
13984       else 
13985         type = check_elaborated_type_specifier (tag_type, decl,
13986                                                 /*allow_template_p=*/true);
13987     }
13988
13989   if (!type)
13990     {
13991       token = cp_lexer_peek_token (parser->lexer);
13992       identifier = cp_parser_identifier (parser);
13993
13994       if (identifier == error_mark_node)
13995         {
13996           parser->scope = NULL_TREE;
13997           return error_mark_node;
13998         }
13999
14000       /* For a `typename', we needn't call xref_tag.  */
14001       if (tag_type == typename_type
14002           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14003         return cp_parser_make_typename_type (parser, parser->scope,
14004                                              identifier,
14005                                              token->location);
14006       /* Look up a qualified name in the usual way.  */
14007       if (parser->scope)
14008         {
14009           tree decl;
14010           tree ambiguous_decls;
14011
14012           decl = cp_parser_lookup_name (parser, identifier,
14013                                         tag_type,
14014                                         /*is_template=*/false,
14015                                         /*is_namespace=*/false,
14016                                         /*check_dependency=*/true,
14017                                         &ambiguous_decls,
14018                                         token->location);
14019
14020           /* If the lookup was ambiguous, an error will already have been
14021              issued.  */
14022           if (ambiguous_decls)
14023             return error_mark_node;
14024
14025           /* If we are parsing friend declaration, DECL may be a
14026              TEMPLATE_DECL tree node here.  However, we need to check
14027              whether this TEMPLATE_DECL results in valid code.  Consider
14028              the following example:
14029
14030                namespace N {
14031                  template <class T> class C {};
14032                }
14033                class X {
14034                  template <class T> friend class N::C; // #1, valid code
14035                };
14036                template <class T> class Y {
14037                  friend class N::C;                    // #2, invalid code
14038                };
14039
14040              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14041              name lookup of `N::C'.  We see that friend declaration must
14042              be template for the code to be valid.  Note that
14043              processing_template_decl does not work here since it is
14044              always 1 for the above two cases.  */
14045
14046           decl = (cp_parser_maybe_treat_template_as_class
14047                   (decl, /*tag_name_p=*/is_friend
14048                          && parser->num_template_parameter_lists));
14049
14050           if (TREE_CODE (decl) != TYPE_DECL)
14051             {
14052               cp_parser_diagnose_invalid_type_name (parser,
14053                                                     parser->scope,
14054                                                     identifier,
14055                                                     token->location);
14056               return error_mark_node;
14057             }
14058
14059           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14060             {
14061               bool allow_template = (parser->num_template_parameter_lists
14062                                       || DECL_SELF_REFERENCE_P (decl));
14063               type = check_elaborated_type_specifier (tag_type, decl, 
14064                                                       allow_template);
14065
14066               if (type == error_mark_node)
14067                 return error_mark_node;
14068             }
14069
14070           /* Forward declarations of nested types, such as
14071
14072                class C1::C2;
14073                class C1::C2::C3;
14074
14075              are invalid unless all components preceding the final '::'
14076              are complete.  If all enclosing types are complete, these
14077              declarations become merely pointless.
14078
14079              Invalid forward declarations of nested types are errors
14080              caught elsewhere in parsing.  Those that are pointless arrive
14081              here.  */
14082
14083           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14084               && !is_friend && !processing_explicit_instantiation)
14085             warning (0, "declaration %qD does not declare anything", decl);
14086
14087           type = TREE_TYPE (decl);
14088         }
14089       else
14090         {
14091           /* An elaborated-type-specifier sometimes introduces a new type and
14092              sometimes names an existing type.  Normally, the rule is that it
14093              introduces a new type only if there is not an existing type of
14094              the same name already in scope.  For example, given:
14095
14096                struct S {};
14097                void f() { struct S s; }
14098
14099              the `struct S' in the body of `f' is the same `struct S' as in
14100              the global scope; the existing definition is used.  However, if
14101              there were no global declaration, this would introduce a new
14102              local class named `S'.
14103
14104              An exception to this rule applies to the following code:
14105
14106                namespace N { struct S; }
14107
14108              Here, the elaborated-type-specifier names a new type
14109              unconditionally; even if there is already an `S' in the
14110              containing scope this declaration names a new type.
14111              This exception only applies if the elaborated-type-specifier
14112              forms the complete declaration:
14113
14114                [class.name]
14115
14116                A declaration consisting solely of `class-key identifier ;' is
14117                either a redeclaration of the name in the current scope or a
14118                forward declaration of the identifier as a class name.  It
14119                introduces the name into the current scope.
14120
14121              We are in this situation precisely when the next token is a `;'.
14122
14123              An exception to the exception is that a `friend' declaration does
14124              *not* name a new type; i.e., given:
14125
14126                struct S { friend struct T; };
14127
14128              `T' is not a new type in the scope of `S'.
14129
14130              Also, `new struct S' or `sizeof (struct S)' never results in the
14131              definition of a new type; a new type can only be declared in a
14132              declaration context.  */
14133
14134           tag_scope ts;
14135           bool template_p;
14136
14137           if (is_friend)
14138             /* Friends have special name lookup rules.  */
14139             ts = ts_within_enclosing_non_class;
14140           else if (is_declaration
14141                    && cp_lexer_next_token_is (parser->lexer,
14142                                               CPP_SEMICOLON))
14143             /* This is a `class-key identifier ;' */
14144             ts = ts_current;
14145           else
14146             ts = ts_global;
14147
14148           template_p =
14149             (parser->num_template_parameter_lists
14150              && (cp_parser_next_token_starts_class_definition_p (parser)
14151                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14152           /* An unqualified name was used to reference this type, so
14153              there were no qualifying templates.  */
14154           if (!cp_parser_check_template_parameters (parser,
14155                                                     /*num_templates=*/0,
14156                                                     token->location,
14157                                                     /*declarator=*/NULL))
14158             return error_mark_node;
14159           type = xref_tag (tag_type, identifier, ts, template_p);
14160         }
14161     }
14162
14163   if (type == error_mark_node)
14164     return error_mark_node;
14165
14166   /* Allow attributes on forward declarations of classes.  */
14167   if (attributes)
14168     {
14169       if (TREE_CODE (type) == TYPENAME_TYPE)
14170         warning (OPT_Wattributes,
14171                  "attributes ignored on uninstantiated type");
14172       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14173                && ! processing_explicit_instantiation)
14174         warning (OPT_Wattributes,
14175                  "attributes ignored on template instantiation");
14176       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14177         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14178       else
14179         warning (OPT_Wattributes,
14180                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14181     }
14182
14183   if (tag_type != enum_type)
14184     {
14185       /* Indicate whether this class was declared as a `class' or as a
14186          `struct'.  */
14187       if (TREE_CODE (type) == RECORD_TYPE)
14188         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14189       cp_parser_check_class_key (tag_type, type);
14190     }
14191
14192   /* A "<" cannot follow an elaborated type specifier.  If that
14193      happens, the user was probably trying to form a template-id.  */
14194   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14195
14196   return type;
14197 }
14198
14199 /* Parse an enum-specifier.
14200
14201    enum-specifier:
14202      enum-head { enumerator-list [opt] }
14203      enum-head { enumerator-list , } [C++0x]
14204
14205    enum-head:
14206      enum-key identifier [opt] enum-base [opt]
14207      enum-key nested-name-specifier identifier enum-base [opt]
14208
14209    enum-key:
14210      enum
14211      enum class   [C++0x]
14212      enum struct  [C++0x]
14213
14214    enum-base:   [C++0x]
14215      : type-specifier-seq
14216
14217    opaque-enum-specifier:
14218      enum-key identifier enum-base [opt] ;
14219
14220    GNU Extensions:
14221      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14222        { enumerator-list [opt] }attributes[opt]
14223      enum-key attributes[opt] identifier [opt] enum-base [opt]
14224        { enumerator-list, }attributes[opt] [C++0x]
14225
14226    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14227    if the token stream isn't an enum-specifier after all.  */
14228
14229 static tree
14230 cp_parser_enum_specifier (cp_parser* parser)
14231 {
14232   tree identifier;
14233   tree type = NULL_TREE;
14234   tree prev_scope;
14235   tree nested_name_specifier = NULL_TREE;
14236   tree attributes;
14237   bool scoped_enum_p = false;
14238   bool has_underlying_type = false;
14239   bool nested_being_defined = false;
14240   bool new_value_list = false;
14241   bool is_new_type = false;
14242   bool is_anonymous = false;
14243   tree underlying_type = NULL_TREE;
14244   cp_token *type_start_token = NULL;
14245   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14246
14247   parser->colon_corrects_to_scope_p = false;
14248
14249   /* Parse tentatively so that we can back up if we don't find a
14250      enum-specifier.  */
14251   cp_parser_parse_tentatively (parser);
14252
14253   /* Caller guarantees that the current token is 'enum', an identifier
14254      possibly follows, and the token after that is an opening brace.
14255      If we don't have an identifier, fabricate an anonymous name for
14256      the enumeration being defined.  */
14257   cp_lexer_consume_token (parser->lexer);
14258
14259   /* Parse the "class" or "struct", which indicates a scoped
14260      enumeration type in C++0x.  */
14261   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14262       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14263     {
14264       if (cxx_dialect < cxx0x)
14265         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14266
14267       /* Consume the `struct' or `class' token.  */
14268       cp_lexer_consume_token (parser->lexer);
14269
14270       scoped_enum_p = true;
14271     }
14272
14273   attributes = cp_parser_attributes_opt (parser);
14274
14275   /* Clear the qualification.  */
14276   parser->scope = NULL_TREE;
14277   parser->qualifying_scope = NULL_TREE;
14278   parser->object_scope = NULL_TREE;
14279
14280   /* Figure out in what scope the declaration is being placed.  */
14281   prev_scope = current_scope ();
14282
14283   type_start_token = cp_lexer_peek_token (parser->lexer);
14284
14285   push_deferring_access_checks (dk_no_check);
14286   nested_name_specifier
14287       = cp_parser_nested_name_specifier_opt (parser,
14288                                              /*typename_keyword_p=*/true,
14289                                              /*check_dependency_p=*/false,
14290                                              /*type_p=*/false,
14291                                              /*is_declaration=*/false);
14292
14293   if (nested_name_specifier)
14294     {
14295       tree name;
14296
14297       identifier = cp_parser_identifier (parser);
14298       name =  cp_parser_lookup_name (parser, identifier,
14299                                      enum_type,
14300                                      /*is_template=*/false,
14301                                      /*is_namespace=*/false,
14302                                      /*check_dependency=*/true,
14303                                      /*ambiguous_decls=*/NULL,
14304                                      input_location);
14305       if (name)
14306         {
14307           type = TREE_TYPE (name);
14308           if (TREE_CODE (type) == TYPENAME_TYPE)
14309             {
14310               /* Are template enums allowed in ISO? */
14311               if (template_parm_scope_p ())
14312                 pedwarn (type_start_token->location, OPT_pedantic,
14313                          "%qD is an enumeration template", name);
14314               /* ignore a typename reference, for it will be solved by name
14315                  in start_enum.  */
14316               type = NULL_TREE;
14317             }
14318         }
14319       else
14320         error_at (type_start_token->location,
14321                   "%qD is not an enumerator-name", identifier);
14322     }
14323   else
14324     {
14325       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14326         identifier = cp_parser_identifier (parser);
14327       else
14328         {
14329           identifier = make_anon_name ();
14330           is_anonymous = true;
14331         }
14332     }
14333   pop_deferring_access_checks ();
14334
14335   /* Check for the `:' that denotes a specified underlying type in C++0x.
14336      Note that a ':' could also indicate a bitfield width, however.  */
14337   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14338     {
14339       cp_decl_specifier_seq type_specifiers;
14340
14341       /* Consume the `:'.  */
14342       cp_lexer_consume_token (parser->lexer);
14343
14344       /* Parse the type-specifier-seq.  */
14345       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14346                                     /*is_trailing_return=*/false,
14347                                     &type_specifiers);
14348
14349       /* At this point this is surely not elaborated type specifier.  */
14350       if (!cp_parser_parse_definitely (parser))
14351         return NULL_TREE;
14352
14353       if (cxx_dialect < cxx0x)
14354         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14355
14356       has_underlying_type = true;
14357
14358       /* If that didn't work, stop.  */
14359       if (type_specifiers.type != error_mark_node)
14360         {
14361           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14362                                             /*initialized=*/0, NULL);
14363           if (underlying_type == error_mark_node)
14364             underlying_type = NULL_TREE;
14365         }
14366     }
14367
14368   /* Look for the `{' but don't consume it yet.  */
14369   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14370     {
14371       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14372         {
14373           cp_parser_error (parser, "expected %<{%>");
14374           if (has_underlying_type)
14375             {
14376               type = NULL_TREE;
14377               goto out;
14378             }
14379         }
14380       /* An opaque-enum-specifier must have a ';' here.  */
14381       if ((scoped_enum_p || underlying_type)
14382           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14383         {
14384           cp_parser_error (parser, "expected %<;%> or %<{%>");
14385           if (has_underlying_type)
14386             {
14387               type = NULL_TREE;
14388               goto out;
14389             }
14390         }
14391     }
14392
14393   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14394     return NULL_TREE;
14395
14396   if (nested_name_specifier)
14397     {
14398       if (CLASS_TYPE_P (nested_name_specifier))
14399         {
14400           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14401           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14402           push_scope (nested_name_specifier);
14403         }
14404       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14405         {
14406           push_nested_namespace (nested_name_specifier);
14407         }
14408     }
14409
14410   /* Issue an error message if type-definitions are forbidden here.  */
14411   if (!cp_parser_check_type_definition (parser))
14412     type = error_mark_node;
14413   else
14414     /* Create the new type.  We do this before consuming the opening
14415        brace so the enum will be recorded as being on the line of its
14416        tag (or the 'enum' keyword, if there is no tag).  */
14417     type = start_enum (identifier, type, underlying_type,
14418                        scoped_enum_p, &is_new_type);
14419
14420   /* If the next token is not '{' it is an opaque-enum-specifier or an
14421      elaborated-type-specifier.  */
14422   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14423     {
14424       timevar_push (TV_PARSE_ENUM);
14425       if (nested_name_specifier)
14426         {
14427           /* The following catches invalid code such as:
14428              enum class S<int>::E { A, B, C }; */
14429           if (!processing_specialization
14430               && CLASS_TYPE_P (nested_name_specifier)
14431               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14432             error_at (type_start_token->location, "cannot add an enumerator "
14433                       "list to a template instantiation");
14434
14435           /* If that scope does not contain the scope in which the
14436              class was originally declared, the program is invalid.  */
14437           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14438             {
14439               if (at_namespace_scope_p ())
14440                 error_at (type_start_token->location,
14441                           "declaration of %qD in namespace %qD which does not "
14442                           "enclose %qD",
14443                           type, prev_scope, nested_name_specifier);
14444               else
14445                 error_at (type_start_token->location,
14446                           "declaration of %qD in %qD which does not enclose %qD",
14447                           type, prev_scope, nested_name_specifier);
14448               type = error_mark_node;
14449             }
14450         }
14451
14452       if (scoped_enum_p)
14453         begin_scope (sk_scoped_enum, type);
14454
14455       /* Consume the opening brace.  */
14456       cp_lexer_consume_token (parser->lexer);
14457
14458       if (type == error_mark_node)
14459         ; /* Nothing to add */
14460       else if (OPAQUE_ENUM_P (type)
14461                || (cxx_dialect > cxx98 && processing_specialization))
14462         {
14463           new_value_list = true;
14464           SET_OPAQUE_ENUM_P (type, false);
14465           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14466         }
14467       else
14468         {
14469           error_at (type_start_token->location, "multiple definition of %q#T", type);
14470           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14471                     "previous definition here");
14472           type = error_mark_node;
14473         }
14474
14475       if (type == error_mark_node)
14476         cp_parser_skip_to_end_of_block_or_statement (parser);
14477       /* If the next token is not '}', then there are some enumerators.  */
14478       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14479         cp_parser_enumerator_list (parser, type);
14480
14481       /* Consume the final '}'.  */
14482       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14483
14484       if (scoped_enum_p)
14485         finish_scope ();
14486       timevar_pop (TV_PARSE_ENUM);
14487     }
14488   else
14489     {
14490       /* If a ';' follows, then it is an opaque-enum-specifier
14491         and additional restrictions apply.  */
14492       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14493         {
14494           if (is_anonymous)
14495             error_at (type_start_token->location,
14496                       "opaque-enum-specifier without name");
14497           else if (nested_name_specifier)
14498             error_at (type_start_token->location,
14499                       "opaque-enum-specifier must use a simple identifier");
14500         }
14501     }
14502
14503   /* Look for trailing attributes to apply to this enumeration, and
14504      apply them if appropriate.  */
14505   if (cp_parser_allow_gnu_extensions_p (parser))
14506     {
14507       tree trailing_attr = cp_parser_attributes_opt (parser);
14508       trailing_attr = chainon (trailing_attr, attributes);
14509       cplus_decl_attributes (&type,
14510                              trailing_attr,
14511                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14512     }
14513
14514   /* Finish up the enumeration.  */
14515   if (type != error_mark_node)
14516     {
14517       if (new_value_list)
14518         finish_enum_value_list (type);
14519       if (is_new_type)
14520         finish_enum (type);
14521     }
14522
14523   if (nested_name_specifier)
14524     {
14525       if (CLASS_TYPE_P (nested_name_specifier))
14526         {
14527           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14528           pop_scope (nested_name_specifier);
14529         }
14530       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14531         {
14532           pop_nested_namespace (nested_name_specifier);
14533         }
14534     }
14535  out:
14536   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14537   return type;
14538 }
14539
14540 /* Parse an enumerator-list.  The enumerators all have the indicated
14541    TYPE.
14542
14543    enumerator-list:
14544      enumerator-definition
14545      enumerator-list , enumerator-definition  */
14546
14547 static void
14548 cp_parser_enumerator_list (cp_parser* parser, tree type)
14549 {
14550   while (true)
14551     {
14552       /* Parse an enumerator-definition.  */
14553       cp_parser_enumerator_definition (parser, type);
14554
14555       /* If the next token is not a ',', we've reached the end of
14556          the list.  */
14557       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14558         break;
14559       /* Otherwise, consume the `,' and keep going.  */
14560       cp_lexer_consume_token (parser->lexer);
14561       /* If the next token is a `}', there is a trailing comma.  */
14562       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14563         {
14564           if (cxx_dialect < cxx0x && !in_system_header)
14565             pedwarn (input_location, OPT_pedantic,
14566                      "comma at end of enumerator list");
14567           break;
14568         }
14569     }
14570 }
14571
14572 /* Parse an enumerator-definition.  The enumerator has the indicated
14573    TYPE.
14574
14575    enumerator-definition:
14576      enumerator
14577      enumerator = constant-expression
14578
14579    enumerator:
14580      identifier  */
14581
14582 static void
14583 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14584 {
14585   tree identifier;
14586   tree value;
14587   location_t loc;
14588
14589   /* Save the input location because we are interested in the location
14590      of the identifier and not the location of the explicit value.  */
14591   loc = cp_lexer_peek_token (parser->lexer)->location;
14592
14593   /* Look for the identifier.  */
14594   identifier = cp_parser_identifier (parser);
14595   if (identifier == error_mark_node)
14596     return;
14597
14598   /* If the next token is an '=', then there is an explicit value.  */
14599   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14600     {
14601       /* Consume the `=' token.  */
14602       cp_lexer_consume_token (parser->lexer);
14603       /* Parse the value.  */
14604       value = cp_parser_constant_expression (parser,
14605                                              /*allow_non_constant_p=*/false,
14606                                              NULL);
14607     }
14608   else
14609     value = NULL_TREE;
14610
14611   /* If we are processing a template, make sure the initializer of the
14612      enumerator doesn't contain any bare template parameter pack.  */
14613   if (check_for_bare_parameter_packs (value))
14614     value = error_mark_node;
14615
14616   /* integral_constant_value will pull out this expression, so make sure
14617      it's folded as appropriate.  */
14618   value = fold_non_dependent_expr (value);
14619
14620   /* Create the enumerator.  */
14621   build_enumerator (identifier, value, type, loc);
14622 }
14623
14624 /* Parse a namespace-name.
14625
14626    namespace-name:
14627      original-namespace-name
14628      namespace-alias
14629
14630    Returns the NAMESPACE_DECL for the namespace.  */
14631
14632 static tree
14633 cp_parser_namespace_name (cp_parser* parser)
14634 {
14635   tree identifier;
14636   tree namespace_decl;
14637
14638   cp_token *token = cp_lexer_peek_token (parser->lexer);
14639
14640   /* Get the name of the namespace.  */
14641   identifier = cp_parser_identifier (parser);
14642   if (identifier == error_mark_node)
14643     return error_mark_node;
14644
14645   /* Look up the identifier in the currently active scope.  Look only
14646      for namespaces, due to:
14647
14648        [basic.lookup.udir]
14649
14650        When looking up a namespace-name in a using-directive or alias
14651        definition, only namespace names are considered.
14652
14653      And:
14654
14655        [basic.lookup.qual]
14656
14657        During the lookup of a name preceding the :: scope resolution
14658        operator, object, function, and enumerator names are ignored.
14659
14660      (Note that cp_parser_qualifying_entity only calls this
14661      function if the token after the name is the scope resolution
14662      operator.)  */
14663   namespace_decl = cp_parser_lookup_name (parser, identifier,
14664                                           none_type,
14665                                           /*is_template=*/false,
14666                                           /*is_namespace=*/true,
14667                                           /*check_dependency=*/true,
14668                                           /*ambiguous_decls=*/NULL,
14669                                           token->location);
14670   /* If it's not a namespace, issue an error.  */
14671   if (namespace_decl == error_mark_node
14672       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14673     {
14674       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14675         error_at (token->location, "%qD is not a namespace-name", identifier);
14676       cp_parser_error (parser, "expected namespace-name");
14677       namespace_decl = error_mark_node;
14678     }
14679
14680   return namespace_decl;
14681 }
14682
14683 /* Parse a namespace-definition.
14684
14685    namespace-definition:
14686      named-namespace-definition
14687      unnamed-namespace-definition
14688
14689    named-namespace-definition:
14690      original-namespace-definition
14691      extension-namespace-definition
14692
14693    original-namespace-definition:
14694      namespace identifier { namespace-body }
14695
14696    extension-namespace-definition:
14697      namespace original-namespace-name { namespace-body }
14698
14699    unnamed-namespace-definition:
14700      namespace { namespace-body } */
14701
14702 static void
14703 cp_parser_namespace_definition (cp_parser* parser)
14704 {
14705   tree identifier, attribs;
14706   bool has_visibility;
14707   bool is_inline;
14708
14709   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14710     {
14711       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14712       is_inline = true;
14713       cp_lexer_consume_token (parser->lexer);
14714     }
14715   else
14716     is_inline = false;
14717
14718   /* Look for the `namespace' keyword.  */
14719   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14720
14721   /* Get the name of the namespace.  We do not attempt to distinguish
14722      between an original-namespace-definition and an
14723      extension-namespace-definition at this point.  The semantic
14724      analysis routines are responsible for that.  */
14725   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14726     identifier = cp_parser_identifier (parser);
14727   else
14728     identifier = NULL_TREE;
14729
14730   /* Parse any specified attributes.  */
14731   attribs = cp_parser_attributes_opt (parser);
14732
14733   /* Look for the `{' to start the namespace.  */
14734   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14735   /* Start the namespace.  */
14736   push_namespace (identifier);
14737
14738   /* "inline namespace" is equivalent to a stub namespace definition
14739      followed by a strong using directive.  */
14740   if (is_inline)
14741     {
14742       tree name_space = current_namespace;
14743       /* Set up namespace association.  */
14744       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14745         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14746                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14747       /* Import the contents of the inline namespace.  */
14748       pop_namespace ();
14749       do_using_directive (name_space);
14750       push_namespace (identifier);
14751     }
14752
14753   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14754
14755   /* Parse the body of the namespace.  */
14756   cp_parser_namespace_body (parser);
14757
14758   if (has_visibility)
14759     pop_visibility (1);
14760
14761   /* Finish the namespace.  */
14762   pop_namespace ();
14763   /* Look for the final `}'.  */
14764   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14765 }
14766
14767 /* Parse a namespace-body.
14768
14769    namespace-body:
14770      declaration-seq [opt]  */
14771
14772 static void
14773 cp_parser_namespace_body (cp_parser* parser)
14774 {
14775   cp_parser_declaration_seq_opt (parser);
14776 }
14777
14778 /* Parse a namespace-alias-definition.
14779
14780    namespace-alias-definition:
14781      namespace identifier = qualified-namespace-specifier ;  */
14782
14783 static void
14784 cp_parser_namespace_alias_definition (cp_parser* parser)
14785 {
14786   tree identifier;
14787   tree namespace_specifier;
14788
14789   cp_token *token = cp_lexer_peek_token (parser->lexer);
14790
14791   /* Look for the `namespace' keyword.  */
14792   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14793   /* Look for the identifier.  */
14794   identifier = cp_parser_identifier (parser);
14795   if (identifier == error_mark_node)
14796     return;
14797   /* Look for the `=' token.  */
14798   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14799       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14800     {
14801       error_at (token->location, "%<namespace%> definition is not allowed here");
14802       /* Skip the definition.  */
14803       cp_lexer_consume_token (parser->lexer);
14804       if (cp_parser_skip_to_closing_brace (parser))
14805         cp_lexer_consume_token (parser->lexer);
14806       return;
14807     }
14808   cp_parser_require (parser, CPP_EQ, RT_EQ);
14809   /* Look for the qualified-namespace-specifier.  */
14810   namespace_specifier
14811     = cp_parser_qualified_namespace_specifier (parser);
14812   /* Look for the `;' token.  */
14813   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14814
14815   /* Register the alias in the symbol table.  */
14816   do_namespace_alias (identifier, namespace_specifier);
14817 }
14818
14819 /* Parse a qualified-namespace-specifier.
14820
14821    qualified-namespace-specifier:
14822      :: [opt] nested-name-specifier [opt] namespace-name
14823
14824    Returns a NAMESPACE_DECL corresponding to the specified
14825    namespace.  */
14826
14827 static tree
14828 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14829 {
14830   /* Look for the optional `::'.  */
14831   cp_parser_global_scope_opt (parser,
14832                               /*current_scope_valid_p=*/false);
14833
14834   /* Look for the optional nested-name-specifier.  */
14835   cp_parser_nested_name_specifier_opt (parser,
14836                                        /*typename_keyword_p=*/false,
14837                                        /*check_dependency_p=*/true,
14838                                        /*type_p=*/false,
14839                                        /*is_declaration=*/true);
14840
14841   return cp_parser_namespace_name (parser);
14842 }
14843
14844 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14845    access declaration.
14846
14847    using-declaration:
14848      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14849      using :: unqualified-id ;  
14850
14851    access-declaration:
14852      qualified-id ;  
14853
14854    */
14855
14856 static bool
14857 cp_parser_using_declaration (cp_parser* parser, 
14858                              bool access_declaration_p)
14859 {
14860   cp_token *token;
14861   bool typename_p = false;
14862   bool global_scope_p;
14863   tree decl;
14864   tree identifier;
14865   tree qscope;
14866
14867   if (access_declaration_p)
14868     cp_parser_parse_tentatively (parser);
14869   else
14870     {
14871       /* Look for the `using' keyword.  */
14872       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14873       
14874       /* Peek at the next token.  */
14875       token = cp_lexer_peek_token (parser->lexer);
14876       /* See if it's `typename'.  */
14877       if (token->keyword == RID_TYPENAME)
14878         {
14879           /* Remember that we've seen it.  */
14880           typename_p = true;
14881           /* Consume the `typename' token.  */
14882           cp_lexer_consume_token (parser->lexer);
14883         }
14884     }
14885
14886   /* Look for the optional global scope qualification.  */
14887   global_scope_p
14888     = (cp_parser_global_scope_opt (parser,
14889                                    /*current_scope_valid_p=*/false)
14890        != NULL_TREE);
14891
14892   /* If we saw `typename', or didn't see `::', then there must be a
14893      nested-name-specifier present.  */
14894   if (typename_p || !global_scope_p)
14895     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14896                                               /*check_dependency_p=*/true,
14897                                               /*type_p=*/false,
14898                                               /*is_declaration=*/true);
14899   /* Otherwise, we could be in either of the two productions.  In that
14900      case, treat the nested-name-specifier as optional.  */
14901   else
14902     qscope = cp_parser_nested_name_specifier_opt (parser,
14903                                                   /*typename_keyword_p=*/false,
14904                                                   /*check_dependency_p=*/true,
14905                                                   /*type_p=*/false,
14906                                                   /*is_declaration=*/true);
14907   if (!qscope)
14908     qscope = global_namespace;
14909
14910   if (access_declaration_p && cp_parser_error_occurred (parser))
14911     /* Something has already gone wrong; there's no need to parse
14912        further.  Since an error has occurred, the return value of
14913        cp_parser_parse_definitely will be false, as required.  */
14914     return cp_parser_parse_definitely (parser);
14915
14916   token = cp_lexer_peek_token (parser->lexer);
14917   /* Parse the unqualified-id.  */
14918   identifier = cp_parser_unqualified_id (parser,
14919                                          /*template_keyword_p=*/false,
14920                                          /*check_dependency_p=*/true,
14921                                          /*declarator_p=*/true,
14922                                          /*optional_p=*/false);
14923
14924   if (access_declaration_p)
14925     {
14926       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14927         cp_parser_simulate_error (parser);
14928       if (!cp_parser_parse_definitely (parser))
14929         return false;
14930     }
14931
14932   /* The function we call to handle a using-declaration is different
14933      depending on what scope we are in.  */
14934   if (qscope == error_mark_node || identifier == error_mark_node)
14935     ;
14936   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14937            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14938     /* [namespace.udecl]
14939
14940        A using declaration shall not name a template-id.  */
14941     error_at (token->location,
14942               "a template-id may not appear in a using-declaration");
14943   else
14944     {
14945       if (at_class_scope_p ())
14946         {
14947           /* Create the USING_DECL.  */
14948           decl = do_class_using_decl (parser->scope, identifier);
14949
14950           if (check_for_bare_parameter_packs (decl))
14951             return false;
14952           else
14953             /* Add it to the list of members in this class.  */
14954             finish_member_declaration (decl);
14955         }
14956       else
14957         {
14958           decl = cp_parser_lookup_name_simple (parser,
14959                                                identifier,
14960                                                token->location);
14961           if (decl == error_mark_node)
14962             cp_parser_name_lookup_error (parser, identifier,
14963                                          decl, NLE_NULL,
14964                                          token->location);
14965           else if (check_for_bare_parameter_packs (decl))
14966             return false;
14967           else if (!at_namespace_scope_p ())
14968             do_local_using_decl (decl, qscope, identifier);
14969           else
14970             do_toplevel_using_decl (decl, qscope, identifier);
14971         }
14972     }
14973
14974   /* Look for the final `;'.  */
14975   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14976   
14977   return true;
14978 }
14979
14980 /* Parse an alias-declaration.
14981
14982    alias-declaration:
14983      using identifier attribute-specifier-seq [opt] = type-id  */
14984
14985 static tree
14986 cp_parser_alias_declaration (cp_parser* parser)
14987 {
14988   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
14989   location_t id_location;
14990   cp_declarator *declarator;
14991   cp_decl_specifier_seq decl_specs;
14992   bool member_p;
14993   const char *saved_message = NULL;
14994
14995   /* Look for the `using' keyword.  */
14996   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14997   id_location = cp_lexer_peek_token (parser->lexer)->location;
14998   id = cp_parser_identifier (parser);
14999   attributes = cp_parser_attributes_opt (parser);
15000   cp_parser_require (parser, CPP_EQ, RT_EQ);
15001
15002   /* Now we are going to parse the type-id of the declaration.  */
15003
15004   /*
15005     [dcl.type]/3 says:
15006
15007         "A type-specifier-seq shall not define a class or enumeration
15008          unless it appears in the type-id of an alias-declaration (7.1.3) that
15009          is not the declaration of a template-declaration."
15010
15011     In other words, if we currently are in an alias template, the
15012     type-id should not define a type.
15013
15014     So let's set parser->type_definition_forbidden_message in that
15015     case; cp_parser_check_type_definition (called by
15016     cp_parser_class_specifier) will then emit an error if a type is
15017     defined in the type-id.  */
15018   if (parser->num_template_parameter_lists)
15019     {
15020       saved_message = parser->type_definition_forbidden_message;
15021       parser->type_definition_forbidden_message =
15022         G_("types may not be defined in alias template declarations");
15023     }
15024
15025   type = cp_parser_type_id (parser);
15026
15027   /* Restore the error message if need be.  */
15028   if (parser->num_template_parameter_lists)
15029     parser->type_definition_forbidden_message = saved_message;
15030
15031   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15032
15033   if (cp_parser_error_occurred (parser))
15034     return error_mark_node;
15035
15036   /* A typedef-name can also be introduced by an alias-declaration. The
15037      identifier following the using keyword becomes a typedef-name. It has
15038      the same semantics as if it were introduced by the typedef
15039      specifier. In particular, it does not define a new type and it shall
15040      not appear in the type-id.  */
15041
15042   clear_decl_specs (&decl_specs);
15043   decl_specs.type = type;
15044   decl_specs.attributes = attributes;
15045   ++decl_specs.specs[(int) ds_typedef];
15046   ++decl_specs.specs[(int) ds_alias];
15047
15048   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15049   declarator->id_loc = id_location;
15050
15051   member_p = at_class_scope_p ();
15052   if (member_p)
15053     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15054                       NULL_TREE, attributes);
15055   else
15056     decl = start_decl (declarator, &decl_specs, 0,
15057                        attributes, NULL_TREE, &pushed_scope);
15058   if (decl == error_mark_node)
15059     return decl;
15060
15061   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15062
15063   if (pushed_scope)
15064     pop_scope (pushed_scope);
15065
15066   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15067      added into the symbol table; otherwise, return the TYPE_DECL.  */
15068   if (DECL_LANG_SPECIFIC (decl)
15069       && DECL_TEMPLATE_INFO (decl)
15070       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15071     {
15072       decl = DECL_TI_TEMPLATE (decl);
15073       if (member_p)
15074         check_member_template (decl);
15075     }
15076
15077   return decl;
15078 }
15079
15080 /* Parse a using-directive.
15081
15082    using-directive:
15083      using namespace :: [opt] nested-name-specifier [opt]
15084        namespace-name ;  */
15085
15086 static void
15087 cp_parser_using_directive (cp_parser* parser)
15088 {
15089   tree namespace_decl;
15090   tree attribs;
15091
15092   /* Look for the `using' keyword.  */
15093   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15094   /* And the `namespace' keyword.  */
15095   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15096   /* Look for the optional `::' operator.  */
15097   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15098   /* And the optional nested-name-specifier.  */
15099   cp_parser_nested_name_specifier_opt (parser,
15100                                        /*typename_keyword_p=*/false,
15101                                        /*check_dependency_p=*/true,
15102                                        /*type_p=*/false,
15103                                        /*is_declaration=*/true);
15104   /* Get the namespace being used.  */
15105   namespace_decl = cp_parser_namespace_name (parser);
15106   /* And any specified attributes.  */
15107   attribs = cp_parser_attributes_opt (parser);
15108   /* Update the symbol table.  */
15109   parse_using_directive (namespace_decl, attribs);
15110   /* Look for the final `;'.  */
15111   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15112 }
15113
15114 /* Parse an asm-definition.
15115
15116    asm-definition:
15117      asm ( string-literal ) ;
15118
15119    GNU Extension:
15120
15121    asm-definition:
15122      asm volatile [opt] ( string-literal ) ;
15123      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15124      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15125                           : asm-operand-list [opt] ) ;
15126      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15127                           : asm-operand-list [opt]
15128                           : asm-clobber-list [opt] ) ;
15129      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15130                                : asm-clobber-list [opt]
15131                                : asm-goto-list ) ;  */
15132
15133 static void
15134 cp_parser_asm_definition (cp_parser* parser)
15135 {
15136   tree string;
15137   tree outputs = NULL_TREE;
15138   tree inputs = NULL_TREE;
15139   tree clobbers = NULL_TREE;
15140   tree labels = NULL_TREE;
15141   tree asm_stmt;
15142   bool volatile_p = false;
15143   bool extended_p = false;
15144   bool invalid_inputs_p = false;
15145   bool invalid_outputs_p = false;
15146   bool goto_p = false;
15147   required_token missing = RT_NONE;
15148
15149   /* Look for the `asm' keyword.  */
15150   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15151   /* See if the next token is `volatile'.  */
15152   if (cp_parser_allow_gnu_extensions_p (parser)
15153       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15154     {
15155       /* Remember that we saw the `volatile' keyword.  */
15156       volatile_p = true;
15157       /* Consume the token.  */
15158       cp_lexer_consume_token (parser->lexer);
15159     }
15160   if (cp_parser_allow_gnu_extensions_p (parser)
15161       && parser->in_function_body
15162       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15163     {
15164       /* Remember that we saw the `goto' keyword.  */
15165       goto_p = true;
15166       /* Consume the token.  */
15167       cp_lexer_consume_token (parser->lexer);
15168     }
15169   /* Look for the opening `('.  */
15170   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15171     return;
15172   /* Look for the string.  */
15173   string = cp_parser_string_literal (parser, false, false);
15174   if (string == error_mark_node)
15175     {
15176       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15177                                              /*consume_paren=*/true);
15178       return;
15179     }
15180
15181   /* If we're allowing GNU extensions, check for the extended assembly
15182      syntax.  Unfortunately, the `:' tokens need not be separated by
15183      a space in C, and so, for compatibility, we tolerate that here
15184      too.  Doing that means that we have to treat the `::' operator as
15185      two `:' tokens.  */
15186   if (cp_parser_allow_gnu_extensions_p (parser)
15187       && parser->in_function_body
15188       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15189           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15190     {
15191       bool inputs_p = false;
15192       bool clobbers_p = false;
15193       bool labels_p = false;
15194
15195       /* The extended syntax was used.  */
15196       extended_p = true;
15197
15198       /* Look for outputs.  */
15199       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15200         {
15201           /* Consume the `:'.  */
15202           cp_lexer_consume_token (parser->lexer);
15203           /* Parse the output-operands.  */
15204           if (cp_lexer_next_token_is_not (parser->lexer,
15205                                           CPP_COLON)
15206               && cp_lexer_next_token_is_not (parser->lexer,
15207                                              CPP_SCOPE)
15208               && cp_lexer_next_token_is_not (parser->lexer,
15209                                              CPP_CLOSE_PAREN)
15210               && !goto_p)
15211             outputs = cp_parser_asm_operand_list (parser);
15212
15213             if (outputs == error_mark_node)
15214               invalid_outputs_p = true;
15215         }
15216       /* If the next token is `::', there are no outputs, and the
15217          next token is the beginning of the inputs.  */
15218       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15219         /* The inputs are coming next.  */
15220         inputs_p = true;
15221
15222       /* Look for inputs.  */
15223       if (inputs_p
15224           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15225         {
15226           /* Consume the `:' or `::'.  */
15227           cp_lexer_consume_token (parser->lexer);
15228           /* Parse the output-operands.  */
15229           if (cp_lexer_next_token_is_not (parser->lexer,
15230                                           CPP_COLON)
15231               && cp_lexer_next_token_is_not (parser->lexer,
15232                                              CPP_SCOPE)
15233               && cp_lexer_next_token_is_not (parser->lexer,
15234                                              CPP_CLOSE_PAREN))
15235             inputs = cp_parser_asm_operand_list (parser);
15236
15237             if (inputs == error_mark_node)
15238               invalid_inputs_p = true;
15239         }
15240       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15241         /* The clobbers are coming next.  */
15242         clobbers_p = true;
15243
15244       /* Look for clobbers.  */
15245       if (clobbers_p
15246           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15247         {
15248           clobbers_p = true;
15249           /* Consume the `:' or `::'.  */
15250           cp_lexer_consume_token (parser->lexer);
15251           /* Parse the clobbers.  */
15252           if (cp_lexer_next_token_is_not (parser->lexer,
15253                                           CPP_COLON)
15254               && cp_lexer_next_token_is_not (parser->lexer,
15255                                              CPP_CLOSE_PAREN))
15256             clobbers = cp_parser_asm_clobber_list (parser);
15257         }
15258       else if (goto_p
15259                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15260         /* The labels are coming next.  */
15261         labels_p = true;
15262
15263       /* Look for labels.  */
15264       if (labels_p
15265           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15266         {
15267           labels_p = true;
15268           /* Consume the `:' or `::'.  */
15269           cp_lexer_consume_token (parser->lexer);
15270           /* Parse the labels.  */
15271           labels = cp_parser_asm_label_list (parser);
15272         }
15273
15274       if (goto_p && !labels_p)
15275         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15276     }
15277   else if (goto_p)
15278     missing = RT_COLON_SCOPE;
15279
15280   /* Look for the closing `)'.  */
15281   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15282                           missing ? missing : RT_CLOSE_PAREN))
15283     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15284                                            /*consume_paren=*/true);
15285   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15286
15287   if (!invalid_inputs_p && !invalid_outputs_p)
15288     {
15289       /* Create the ASM_EXPR.  */
15290       if (parser->in_function_body)
15291         {
15292           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15293                                       inputs, clobbers, labels);
15294           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15295           if (!extended_p)
15296             {
15297               tree temp = asm_stmt;
15298               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15299                 temp = TREE_OPERAND (temp, 0);
15300
15301               ASM_INPUT_P (temp) = 1;
15302             }
15303         }
15304       else
15305         cgraph_add_asm_node (string);
15306     }
15307 }
15308
15309 /* Declarators [gram.dcl.decl] */
15310
15311 /* Parse an init-declarator.
15312
15313    init-declarator:
15314      declarator initializer [opt]
15315
15316    GNU Extension:
15317
15318    init-declarator:
15319      declarator asm-specification [opt] attributes [opt] initializer [opt]
15320
15321    function-definition:
15322      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15323        function-body
15324      decl-specifier-seq [opt] declarator function-try-block
15325
15326    GNU Extension:
15327
15328    function-definition:
15329      __extension__ function-definition
15330
15331    TM Extension:
15332
15333    function-definition:
15334      decl-specifier-seq [opt] declarator function-transaction-block
15335
15336    The DECL_SPECIFIERS apply to this declarator.  Returns a
15337    representation of the entity declared.  If MEMBER_P is TRUE, then
15338    this declarator appears in a class scope.  The new DECL created by
15339    this declarator is returned.
15340
15341    The CHECKS are access checks that should be performed once we know
15342    what entity is being declared (and, therefore, what classes have
15343    befriended it).
15344
15345    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15346    for a function-definition here as well.  If the declarator is a
15347    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15348    be TRUE upon return.  By that point, the function-definition will
15349    have been completely parsed.
15350
15351    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15352    is FALSE.
15353
15354    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15355    parsed declaration if it is an uninitialized single declarator not followed
15356    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15357    if present, will not be consumed.  If returned, this declarator will be
15358    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15359
15360 static tree
15361 cp_parser_init_declarator (cp_parser* parser,
15362                            cp_decl_specifier_seq *decl_specifiers,
15363                            VEC (deferred_access_check,gc)* checks,
15364                            bool function_definition_allowed_p,
15365                            bool member_p,
15366                            int declares_class_or_enum,
15367                            bool* function_definition_p,
15368                            tree* maybe_range_for_decl)
15369 {
15370   cp_token *token = NULL, *asm_spec_start_token = NULL,
15371            *attributes_start_token = NULL;
15372   cp_declarator *declarator;
15373   tree prefix_attributes;
15374   tree attributes;
15375   tree asm_specification;
15376   tree initializer;
15377   tree decl = NULL_TREE;
15378   tree scope;
15379   int is_initialized;
15380   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15381      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15382      "(...)".  */
15383   enum cpp_ttype initialization_kind;
15384   bool is_direct_init = false;
15385   bool is_non_constant_init;
15386   int ctor_dtor_or_conv_p;
15387   bool friend_p;
15388   tree pushed_scope = NULL_TREE;
15389   bool range_for_decl_p = false;
15390
15391   /* Gather the attributes that were provided with the
15392      decl-specifiers.  */
15393   prefix_attributes = decl_specifiers->attributes;
15394
15395   /* Assume that this is not the declarator for a function
15396      definition.  */
15397   if (function_definition_p)
15398     *function_definition_p = false;
15399
15400   /* Defer access checks while parsing the declarator; we cannot know
15401      what names are accessible until we know what is being
15402      declared.  */
15403   resume_deferring_access_checks ();
15404
15405   /* Parse the declarator.  */
15406   token = cp_lexer_peek_token (parser->lexer);
15407   declarator
15408     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15409                             &ctor_dtor_or_conv_p,
15410                             /*parenthesized_p=*/NULL,
15411                             member_p);
15412   /* Gather up the deferred checks.  */
15413   stop_deferring_access_checks ();
15414
15415   /* If the DECLARATOR was erroneous, there's no need to go
15416      further.  */
15417   if (declarator == cp_error_declarator)
15418     return error_mark_node;
15419
15420   /* Check that the number of template-parameter-lists is OK.  */
15421   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15422                                                        token->location))
15423     return error_mark_node;
15424
15425   if (declares_class_or_enum & 2)
15426     cp_parser_check_for_definition_in_return_type (declarator,
15427                                                    decl_specifiers->type,
15428                                                    decl_specifiers->type_location);
15429
15430   /* Figure out what scope the entity declared by the DECLARATOR is
15431      located in.  `grokdeclarator' sometimes changes the scope, so
15432      we compute it now.  */
15433   scope = get_scope_of_declarator (declarator);
15434
15435   /* Perform any lookups in the declared type which were thought to be
15436      dependent, but are not in the scope of the declarator.  */
15437   decl_specifiers->type
15438     = maybe_update_decl_type (decl_specifiers->type, scope);
15439
15440   /* If we're allowing GNU extensions, look for an asm-specification
15441      and attributes.  */
15442   if (cp_parser_allow_gnu_extensions_p (parser))
15443     {
15444       /* Look for an asm-specification.  */
15445       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15446       asm_specification = cp_parser_asm_specification_opt (parser);
15447       /* And attributes.  */
15448       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15449       attributes = cp_parser_attributes_opt (parser);
15450     }
15451   else
15452     {
15453       asm_specification = NULL_TREE;
15454       attributes = NULL_TREE;
15455     }
15456
15457   /* Peek at the next token.  */
15458   token = cp_lexer_peek_token (parser->lexer);
15459   /* Check to see if the token indicates the start of a
15460      function-definition.  */
15461   if (function_declarator_p (declarator)
15462       && cp_parser_token_starts_function_definition_p (token))
15463     {
15464       if (!function_definition_allowed_p)
15465         {
15466           /* If a function-definition should not appear here, issue an
15467              error message.  */
15468           cp_parser_error (parser,
15469                            "a function-definition is not allowed here");
15470           return error_mark_node;
15471         }
15472       else
15473         {
15474           location_t func_brace_location
15475             = cp_lexer_peek_token (parser->lexer)->location;
15476
15477           /* Neither attributes nor an asm-specification are allowed
15478              on a function-definition.  */
15479           if (asm_specification)
15480             error_at (asm_spec_start_token->location,
15481                       "an asm-specification is not allowed "
15482                       "on a function-definition");
15483           if (attributes)
15484             error_at (attributes_start_token->location,
15485                       "attributes are not allowed on a function-definition");
15486           /* This is a function-definition.  */
15487           *function_definition_p = true;
15488
15489           /* Parse the function definition.  */
15490           if (member_p)
15491             decl = cp_parser_save_member_function_body (parser,
15492                                                         decl_specifiers,
15493                                                         declarator,
15494                                                         prefix_attributes);
15495           else
15496             decl
15497               = (cp_parser_function_definition_from_specifiers_and_declarator
15498                  (parser, decl_specifiers, prefix_attributes, declarator));
15499
15500           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15501             {
15502               /* This is where the prologue starts...  */
15503               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15504                 = func_brace_location;
15505             }
15506
15507           return decl;
15508         }
15509     }
15510
15511   /* [dcl.dcl]
15512
15513      Only in function declarations for constructors, destructors, and
15514      type conversions can the decl-specifier-seq be omitted.
15515
15516      We explicitly postpone this check past the point where we handle
15517      function-definitions because we tolerate function-definitions
15518      that are missing their return types in some modes.  */
15519   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15520     {
15521       cp_parser_error (parser,
15522                        "expected constructor, destructor, or type conversion");
15523       return error_mark_node;
15524     }
15525
15526   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15527   if (token->type == CPP_EQ
15528       || token->type == CPP_OPEN_PAREN
15529       || token->type == CPP_OPEN_BRACE)
15530     {
15531       is_initialized = SD_INITIALIZED;
15532       initialization_kind = token->type;
15533       if (maybe_range_for_decl)
15534         *maybe_range_for_decl = error_mark_node;
15535
15536       if (token->type == CPP_EQ
15537           && function_declarator_p (declarator))
15538         {
15539           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15540           if (t2->keyword == RID_DEFAULT)
15541             is_initialized = SD_DEFAULTED;
15542           else if (t2->keyword == RID_DELETE)
15543             is_initialized = SD_DELETED;
15544         }
15545     }
15546   else
15547     {
15548       /* If the init-declarator isn't initialized and isn't followed by a
15549          `,' or `;', it's not a valid init-declarator.  */
15550       if (token->type != CPP_COMMA
15551           && token->type != CPP_SEMICOLON)
15552         {
15553           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15554             range_for_decl_p = true;
15555           else
15556             {
15557               cp_parser_error (parser, "expected initializer");
15558               return error_mark_node;
15559             }
15560         }
15561       is_initialized = SD_UNINITIALIZED;
15562       initialization_kind = CPP_EOF;
15563     }
15564
15565   /* Because start_decl has side-effects, we should only call it if we
15566      know we're going ahead.  By this point, we know that we cannot
15567      possibly be looking at any other construct.  */
15568   cp_parser_commit_to_tentative_parse (parser);
15569
15570   /* If the decl specifiers were bad, issue an error now that we're
15571      sure this was intended to be a declarator.  Then continue
15572      declaring the variable(s), as int, to try to cut down on further
15573      errors.  */
15574   if (decl_specifiers->any_specifiers_p
15575       && decl_specifiers->type == error_mark_node)
15576     {
15577       cp_parser_error (parser, "invalid type in declaration");
15578       decl_specifiers->type = integer_type_node;
15579     }
15580
15581   /* Check to see whether or not this declaration is a friend.  */
15582   friend_p = cp_parser_friend_p (decl_specifiers);
15583
15584   /* Enter the newly declared entry in the symbol table.  If we're
15585      processing a declaration in a class-specifier, we wait until
15586      after processing the initializer.  */
15587   if (!member_p)
15588     {
15589       if (parser->in_unbraced_linkage_specification_p)
15590         decl_specifiers->storage_class = sc_extern;
15591       decl = start_decl (declarator, decl_specifiers,
15592                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15593                          attributes, prefix_attributes,
15594                          &pushed_scope);
15595       /* Adjust location of decl if declarator->id_loc is more appropriate:
15596          set, and decl wasn't merged with another decl, in which case its
15597          location would be different from input_location, and more accurate.  */
15598       if (DECL_P (decl)
15599           && declarator->id_loc != UNKNOWN_LOCATION
15600           && DECL_SOURCE_LOCATION (decl) == input_location)
15601         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15602     }
15603   else if (scope)
15604     /* Enter the SCOPE.  That way unqualified names appearing in the
15605        initializer will be looked up in SCOPE.  */
15606     pushed_scope = push_scope (scope);
15607
15608   /* Perform deferred access control checks, now that we know in which
15609      SCOPE the declared entity resides.  */
15610   if (!member_p && decl)
15611     {
15612       tree saved_current_function_decl = NULL_TREE;
15613
15614       /* If the entity being declared is a function, pretend that we
15615          are in its scope.  If it is a `friend', it may have access to
15616          things that would not otherwise be accessible.  */
15617       if (TREE_CODE (decl) == FUNCTION_DECL)
15618         {
15619           saved_current_function_decl = current_function_decl;
15620           current_function_decl = decl;
15621         }
15622
15623       /* Perform access checks for template parameters.  */
15624       cp_parser_perform_template_parameter_access_checks (checks);
15625
15626       /* Perform the access control checks for the declarator and the
15627          decl-specifiers.  */
15628       perform_deferred_access_checks ();
15629
15630       /* Restore the saved value.  */
15631       if (TREE_CODE (decl) == FUNCTION_DECL)
15632         current_function_decl = saved_current_function_decl;
15633     }
15634
15635   /* Parse the initializer.  */
15636   initializer = NULL_TREE;
15637   is_direct_init = false;
15638   is_non_constant_init = true;
15639   if (is_initialized)
15640     {
15641       if (function_declarator_p (declarator))
15642         {
15643           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15644            if (initialization_kind == CPP_EQ)
15645              initializer = cp_parser_pure_specifier (parser);
15646            else
15647              {
15648                /* If the declaration was erroneous, we don't really
15649                   know what the user intended, so just silently
15650                   consume the initializer.  */
15651                if (decl != error_mark_node)
15652                  error_at (initializer_start_token->location,
15653                            "initializer provided for function");
15654                cp_parser_skip_to_closing_parenthesis (parser,
15655                                                       /*recovering=*/true,
15656                                                       /*or_comma=*/false,
15657                                                       /*consume_paren=*/true);
15658              }
15659         }
15660       else
15661         {
15662           /* We want to record the extra mangling scope for in-class
15663              initializers of class members and initializers of static data
15664              member templates.  The former is a C++0x feature which isn't
15665              implemented yet, and I expect it will involve deferring
15666              parsing of the initializer until end of class as with default
15667              arguments.  So right here we only handle the latter.  */
15668           if (!member_p && processing_template_decl)
15669             start_lambda_scope (decl);
15670           initializer = cp_parser_initializer (parser,
15671                                                &is_direct_init,
15672                                                &is_non_constant_init);
15673           if (!member_p && processing_template_decl)
15674             finish_lambda_scope ();
15675         }
15676     }
15677
15678   /* The old parser allows attributes to appear after a parenthesized
15679      initializer.  Mark Mitchell proposed removing this functionality
15680      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15681      attributes -- but ignores them.  */
15682   if (cp_parser_allow_gnu_extensions_p (parser)
15683       && initialization_kind == CPP_OPEN_PAREN)
15684     if (cp_parser_attributes_opt (parser))
15685       warning (OPT_Wattributes,
15686                "attributes after parenthesized initializer ignored");
15687
15688   /* For an in-class declaration, use `grokfield' to create the
15689      declaration.  */
15690   if (member_p)
15691     {
15692       if (pushed_scope)
15693         {
15694           pop_scope (pushed_scope);
15695           pushed_scope = NULL_TREE;
15696         }
15697       decl = grokfield (declarator, decl_specifiers,
15698                         initializer, !is_non_constant_init,
15699                         /*asmspec=*/NULL_TREE,
15700                         prefix_attributes);
15701       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15702         cp_parser_save_default_args (parser, decl);
15703     }
15704
15705   /* Finish processing the declaration.  But, skip member
15706      declarations.  */
15707   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15708     {
15709       cp_finish_decl (decl,
15710                       initializer, !is_non_constant_init,
15711                       asm_specification,
15712                       /* If the initializer is in parentheses, then this is
15713                          a direct-initialization, which means that an
15714                          `explicit' constructor is OK.  Otherwise, an
15715                          `explicit' constructor cannot be used.  */
15716                       ((is_direct_init || !is_initialized)
15717                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15718     }
15719   else if ((cxx_dialect != cxx98) && friend_p
15720            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15721     /* Core issue #226 (C++0x only): A default template-argument
15722        shall not be specified in a friend class template
15723        declaration. */
15724     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15725                              /*is_partial=*/0, /*is_friend_decl=*/1);
15726
15727   if (!friend_p && pushed_scope)
15728     pop_scope (pushed_scope);
15729
15730   return decl;
15731 }
15732
15733 /* Parse a declarator.
15734
15735    declarator:
15736      direct-declarator
15737      ptr-operator declarator
15738
15739    abstract-declarator:
15740      ptr-operator abstract-declarator [opt]
15741      direct-abstract-declarator
15742
15743    GNU Extensions:
15744
15745    declarator:
15746      attributes [opt] direct-declarator
15747      attributes [opt] ptr-operator declarator
15748
15749    abstract-declarator:
15750      attributes [opt] ptr-operator abstract-declarator [opt]
15751      attributes [opt] direct-abstract-declarator
15752
15753    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15754    detect constructor, destructor or conversion operators. It is set
15755    to -1 if the declarator is a name, and +1 if it is a
15756    function. Otherwise it is set to zero. Usually you just want to
15757    test for >0, but internally the negative value is used.
15758
15759    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15760    a decl-specifier-seq unless it declares a constructor, destructor,
15761    or conversion.  It might seem that we could check this condition in
15762    semantic analysis, rather than parsing, but that makes it difficult
15763    to handle something like `f()'.  We want to notice that there are
15764    no decl-specifiers, and therefore realize that this is an
15765    expression, not a declaration.)
15766
15767    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15768    the declarator is a direct-declarator of the form "(...)".
15769
15770    MEMBER_P is true iff this declarator is a member-declarator.  */
15771
15772 static cp_declarator *
15773 cp_parser_declarator (cp_parser* parser,
15774                       cp_parser_declarator_kind dcl_kind,
15775                       int* ctor_dtor_or_conv_p,
15776                       bool* parenthesized_p,
15777                       bool member_p)
15778 {
15779   cp_declarator *declarator;
15780   enum tree_code code;
15781   cp_cv_quals cv_quals;
15782   tree class_type;
15783   tree attributes = NULL_TREE;
15784
15785   /* Assume this is not a constructor, destructor, or type-conversion
15786      operator.  */
15787   if (ctor_dtor_or_conv_p)
15788     *ctor_dtor_or_conv_p = 0;
15789
15790   if (cp_parser_allow_gnu_extensions_p (parser))
15791     attributes = cp_parser_attributes_opt (parser);
15792
15793   /* Check for the ptr-operator production.  */
15794   cp_parser_parse_tentatively (parser);
15795   /* Parse the ptr-operator.  */
15796   code = cp_parser_ptr_operator (parser,
15797                                  &class_type,
15798                                  &cv_quals);
15799   /* If that worked, then we have a ptr-operator.  */
15800   if (cp_parser_parse_definitely (parser))
15801     {
15802       /* If a ptr-operator was found, then this declarator was not
15803          parenthesized.  */
15804       if (parenthesized_p)
15805         *parenthesized_p = true;
15806       /* The dependent declarator is optional if we are parsing an
15807          abstract-declarator.  */
15808       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15809         cp_parser_parse_tentatively (parser);
15810
15811       /* Parse the dependent declarator.  */
15812       declarator = cp_parser_declarator (parser, dcl_kind,
15813                                          /*ctor_dtor_or_conv_p=*/NULL,
15814                                          /*parenthesized_p=*/NULL,
15815                                          /*member_p=*/false);
15816
15817       /* If we are parsing an abstract-declarator, we must handle the
15818          case where the dependent declarator is absent.  */
15819       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15820           && !cp_parser_parse_definitely (parser))
15821         declarator = NULL;
15822
15823       declarator = cp_parser_make_indirect_declarator
15824         (code, class_type, cv_quals, declarator);
15825     }
15826   /* Everything else is a direct-declarator.  */
15827   else
15828     {
15829       if (parenthesized_p)
15830         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15831                                                    CPP_OPEN_PAREN);
15832       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15833                                                 ctor_dtor_or_conv_p,
15834                                                 member_p);
15835     }
15836
15837   if (attributes && declarator && declarator != cp_error_declarator)
15838     declarator->attributes = attributes;
15839
15840   return declarator;
15841 }
15842
15843 /* Parse a direct-declarator or direct-abstract-declarator.
15844
15845    direct-declarator:
15846      declarator-id
15847      direct-declarator ( parameter-declaration-clause )
15848        cv-qualifier-seq [opt]
15849        exception-specification [opt]
15850      direct-declarator [ constant-expression [opt] ]
15851      ( declarator )
15852
15853    direct-abstract-declarator:
15854      direct-abstract-declarator [opt]
15855        ( parameter-declaration-clause )
15856        cv-qualifier-seq [opt]
15857        exception-specification [opt]
15858      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15859      ( abstract-declarator )
15860
15861    Returns a representation of the declarator.  DCL_KIND is
15862    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15863    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15864    we are parsing a direct-declarator.  It is
15865    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15866    of ambiguity we prefer an abstract declarator, as per
15867    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15868    cp_parser_declarator.  */
15869
15870 static cp_declarator *
15871 cp_parser_direct_declarator (cp_parser* parser,
15872                              cp_parser_declarator_kind dcl_kind,
15873                              int* ctor_dtor_or_conv_p,
15874                              bool member_p)
15875 {
15876   cp_token *token;
15877   cp_declarator *declarator = NULL;
15878   tree scope = NULL_TREE;
15879   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15880   bool saved_in_declarator_p = parser->in_declarator_p;
15881   bool first = true;
15882   tree pushed_scope = NULL_TREE;
15883
15884   while (true)
15885     {
15886       /* Peek at the next token.  */
15887       token = cp_lexer_peek_token (parser->lexer);
15888       if (token->type == CPP_OPEN_PAREN)
15889         {
15890           /* This is either a parameter-declaration-clause, or a
15891              parenthesized declarator. When we know we are parsing a
15892              named declarator, it must be a parenthesized declarator
15893              if FIRST is true. For instance, `(int)' is a
15894              parameter-declaration-clause, with an omitted
15895              direct-abstract-declarator. But `((*))', is a
15896              parenthesized abstract declarator. Finally, when T is a
15897              template parameter `(T)' is a
15898              parameter-declaration-clause, and not a parenthesized
15899              named declarator.
15900
15901              We first try and parse a parameter-declaration-clause,
15902              and then try a nested declarator (if FIRST is true).
15903
15904              It is not an error for it not to be a
15905              parameter-declaration-clause, even when FIRST is
15906              false. Consider,
15907
15908                int i (int);
15909                int i (3);
15910
15911              The first is the declaration of a function while the
15912              second is the definition of a variable, including its
15913              initializer.
15914
15915              Having seen only the parenthesis, we cannot know which of
15916              these two alternatives should be selected.  Even more
15917              complex are examples like:
15918
15919                int i (int (a));
15920                int i (int (3));
15921
15922              The former is a function-declaration; the latter is a
15923              variable initialization.
15924
15925              Thus again, we try a parameter-declaration-clause, and if
15926              that fails, we back out and return.  */
15927
15928           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15929             {
15930               tree params;
15931               unsigned saved_num_template_parameter_lists;
15932               bool is_declarator = false;
15933               tree t;
15934
15935               /* In a member-declarator, the only valid interpretation
15936                  of a parenthesis is the start of a
15937                  parameter-declaration-clause.  (It is invalid to
15938                  initialize a static data member with a parenthesized
15939                  initializer; only the "=" form of initialization is
15940                  permitted.)  */
15941               if (!member_p)
15942                 cp_parser_parse_tentatively (parser);
15943
15944               /* Consume the `('.  */
15945               cp_lexer_consume_token (parser->lexer);
15946               if (first)
15947                 {
15948                   /* If this is going to be an abstract declarator, we're
15949                      in a declarator and we can't have default args.  */
15950                   parser->default_arg_ok_p = false;
15951                   parser->in_declarator_p = true;
15952                 }
15953
15954               /* Inside the function parameter list, surrounding
15955                  template-parameter-lists do not apply.  */
15956               saved_num_template_parameter_lists
15957                 = parser->num_template_parameter_lists;
15958               parser->num_template_parameter_lists = 0;
15959
15960               begin_scope (sk_function_parms, NULL_TREE);
15961
15962               /* Parse the parameter-declaration-clause.  */
15963               params = cp_parser_parameter_declaration_clause (parser);
15964
15965               parser->num_template_parameter_lists
15966                 = saved_num_template_parameter_lists;
15967
15968               /* Consume the `)'.  */
15969               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15970
15971               /* If all went well, parse the cv-qualifier-seq and the
15972                  exception-specification.  */
15973               if (member_p || cp_parser_parse_definitely (parser))
15974                 {
15975                   cp_cv_quals cv_quals;
15976                   cp_virt_specifiers virt_specifiers;
15977                   tree exception_specification;
15978                   tree late_return;
15979
15980                   is_declarator = true;
15981
15982                   if (ctor_dtor_or_conv_p)
15983                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15984                   first = false;
15985
15986                   /* Parse the cv-qualifier-seq.  */
15987                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15988                   /* And the exception-specification.  */
15989                   exception_specification
15990                     = cp_parser_exception_specification_opt (parser);
15991                   /* Parse the virt-specifier-seq.  */
15992                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15993
15994                   late_return = (cp_parser_late_return_type_opt
15995                                  (parser, member_p ? cv_quals : -1));
15996
15997                   /* Create the function-declarator.  */
15998                   declarator = make_call_declarator (declarator,
15999                                                      params,
16000                                                      cv_quals,
16001                                                      virt_specifiers,
16002                                                      exception_specification,
16003                                                      late_return);
16004                   /* Any subsequent parameter lists are to do with
16005                      return type, so are not those of the declared
16006                      function.  */
16007                   parser->default_arg_ok_p = false;
16008                 }
16009
16010               /* Remove the function parms from scope.  */
16011               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16012                 pop_binding (DECL_NAME (t), t);
16013               leave_scope();
16014
16015               if (is_declarator)
16016                 /* Repeat the main loop.  */
16017                 continue;
16018             }
16019
16020           /* If this is the first, we can try a parenthesized
16021              declarator.  */
16022           if (first)
16023             {
16024               bool saved_in_type_id_in_expr_p;
16025
16026               parser->default_arg_ok_p = saved_default_arg_ok_p;
16027               parser->in_declarator_p = saved_in_declarator_p;
16028
16029               /* Consume the `('.  */
16030               cp_lexer_consume_token (parser->lexer);
16031               /* Parse the nested declarator.  */
16032               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16033               parser->in_type_id_in_expr_p = true;
16034               declarator
16035                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16036                                         /*parenthesized_p=*/NULL,
16037                                         member_p);
16038               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16039               first = false;
16040               /* Expect a `)'.  */
16041               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16042                 declarator = cp_error_declarator;
16043               if (declarator == cp_error_declarator)
16044                 break;
16045
16046               goto handle_declarator;
16047             }
16048           /* Otherwise, we must be done.  */
16049           else
16050             break;
16051         }
16052       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16053                && token->type == CPP_OPEN_SQUARE)
16054         {
16055           /* Parse an array-declarator.  */
16056           tree bounds;
16057
16058           if (ctor_dtor_or_conv_p)
16059             *ctor_dtor_or_conv_p = 0;
16060
16061           first = false;
16062           parser->default_arg_ok_p = false;
16063           parser->in_declarator_p = true;
16064           /* Consume the `['.  */
16065           cp_lexer_consume_token (parser->lexer);
16066           /* Peek at the next token.  */
16067           token = cp_lexer_peek_token (parser->lexer);
16068           /* If the next token is `]', then there is no
16069              constant-expression.  */
16070           if (token->type != CPP_CLOSE_SQUARE)
16071             {
16072               bool non_constant_p;
16073
16074               bounds
16075                 = cp_parser_constant_expression (parser,
16076                                                  /*allow_non_constant=*/true,
16077                                                  &non_constant_p);
16078               if (!non_constant_p)
16079                 /* OK */;
16080               else if (error_operand_p (bounds))
16081                 /* Already gave an error.  */;
16082               else if (!parser->in_function_body
16083                        || current_binding_level->kind == sk_function_parms)
16084                 {
16085                   /* Normally, the array bound must be an integral constant
16086                      expression.  However, as an extension, we allow VLAs
16087                      in function scopes as long as they aren't part of a
16088                      parameter declaration.  */
16089                   cp_parser_error (parser,
16090                                    "array bound is not an integer constant");
16091                   bounds = error_mark_node;
16092                 }
16093               else if (processing_template_decl)
16094                 {
16095                   /* Remember this wasn't a constant-expression.  */
16096                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16097                   TREE_SIDE_EFFECTS (bounds) = 1;
16098                 }
16099             }
16100           else
16101             bounds = NULL_TREE;
16102           /* Look for the closing `]'.  */
16103           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16104             {
16105               declarator = cp_error_declarator;
16106               break;
16107             }
16108
16109           declarator = make_array_declarator (declarator, bounds);
16110         }
16111       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16112         {
16113           {
16114             tree qualifying_scope;
16115             tree unqualified_name;
16116             special_function_kind sfk;
16117             bool abstract_ok;
16118             bool pack_expansion_p = false;
16119             cp_token *declarator_id_start_token;
16120
16121             /* Parse a declarator-id */
16122             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16123             if (abstract_ok)
16124               {
16125                 cp_parser_parse_tentatively (parser);
16126
16127                 /* If we see an ellipsis, we should be looking at a
16128                    parameter pack. */
16129                 if (token->type == CPP_ELLIPSIS)
16130                   {
16131                     /* Consume the `...' */
16132                     cp_lexer_consume_token (parser->lexer);
16133
16134                     pack_expansion_p = true;
16135                   }
16136               }
16137
16138             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16139             unqualified_name
16140               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16141             qualifying_scope = parser->scope;
16142             if (abstract_ok)
16143               {
16144                 bool okay = false;
16145
16146                 if (!unqualified_name && pack_expansion_p)
16147                   {
16148                     /* Check whether an error occurred. */
16149                     okay = !cp_parser_error_occurred (parser);
16150
16151                     /* We already consumed the ellipsis to mark a
16152                        parameter pack, but we have no way to report it,
16153                        so abort the tentative parse. We will be exiting
16154                        immediately anyway. */
16155                     cp_parser_abort_tentative_parse (parser);
16156                   }
16157                 else
16158                   okay = cp_parser_parse_definitely (parser);
16159
16160                 if (!okay)
16161                   unqualified_name = error_mark_node;
16162                 else if (unqualified_name
16163                          && (qualifying_scope
16164                              || (TREE_CODE (unqualified_name)
16165                                  != IDENTIFIER_NODE)))
16166                   {
16167                     cp_parser_error (parser, "expected unqualified-id");
16168                     unqualified_name = error_mark_node;
16169                   }
16170               }
16171
16172             if (!unqualified_name)
16173               return NULL;
16174             if (unqualified_name == error_mark_node)
16175               {
16176                 declarator = cp_error_declarator;
16177                 pack_expansion_p = false;
16178                 declarator->parameter_pack_p = false;
16179                 break;
16180               }
16181
16182             if (qualifying_scope && at_namespace_scope_p ()
16183                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16184               {
16185                 /* In the declaration of a member of a template class
16186                    outside of the class itself, the SCOPE will sometimes
16187                    be a TYPENAME_TYPE.  For example, given:
16188
16189                    template <typename T>
16190                    int S<T>::R::i = 3;
16191
16192                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16193                    this context, we must resolve S<T>::R to an ordinary
16194                    type, rather than a typename type.
16195
16196                    The reason we normally avoid resolving TYPENAME_TYPEs
16197                    is that a specialization of `S' might render
16198                    `S<T>::R' not a type.  However, if `S' is
16199                    specialized, then this `i' will not be used, so there
16200                    is no harm in resolving the types here.  */
16201                 tree type;
16202
16203                 /* Resolve the TYPENAME_TYPE.  */
16204                 type = resolve_typename_type (qualifying_scope,
16205                                               /*only_current_p=*/false);
16206                 /* If that failed, the declarator is invalid.  */
16207                 if (TREE_CODE (type) == TYPENAME_TYPE)
16208                   {
16209                     if (typedef_variant_p (type))
16210                       error_at (declarator_id_start_token->location,
16211                                 "cannot define member of dependent typedef "
16212                                 "%qT", type);
16213                     else
16214                       error_at (declarator_id_start_token->location,
16215                                 "%<%T::%E%> is not a type",
16216                                 TYPE_CONTEXT (qualifying_scope),
16217                                 TYPE_IDENTIFIER (qualifying_scope));
16218                   }
16219                 qualifying_scope = type;
16220               }
16221
16222             sfk = sfk_none;
16223
16224             if (unqualified_name)
16225               {
16226                 tree class_type;
16227
16228                 if (qualifying_scope
16229                     && CLASS_TYPE_P (qualifying_scope))
16230                   class_type = qualifying_scope;
16231                 else
16232                   class_type = current_class_type;
16233
16234                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16235                   {
16236                     tree name_type = TREE_TYPE (unqualified_name);
16237                     if (class_type && same_type_p (name_type, class_type))
16238                       {
16239                         if (qualifying_scope
16240                             && CLASSTYPE_USE_TEMPLATE (name_type))
16241                           {
16242                             error_at (declarator_id_start_token->location,
16243                                       "invalid use of constructor as a template");
16244                             inform (declarator_id_start_token->location,
16245                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16246                                     "name the constructor in a qualified name",
16247                                     class_type,
16248                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16249                                     class_type, name_type);
16250                             declarator = cp_error_declarator;
16251                             break;
16252                           }
16253                         else
16254                           unqualified_name = constructor_name (class_type);
16255                       }
16256                     else
16257                       {
16258                         /* We do not attempt to print the declarator
16259                            here because we do not have enough
16260                            information about its original syntactic
16261                            form.  */
16262                         cp_parser_error (parser, "invalid declarator");
16263                         declarator = cp_error_declarator;
16264                         break;
16265                       }
16266                   }
16267
16268                 if (class_type)
16269                   {
16270                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16271                       sfk = sfk_destructor;
16272                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16273                       sfk = sfk_conversion;
16274                     else if (/* There's no way to declare a constructor
16275                                 for an anonymous type, even if the type
16276                                 got a name for linkage purposes.  */
16277                              !TYPE_WAS_ANONYMOUS (class_type)
16278                              && constructor_name_p (unqualified_name,
16279                                                     class_type))
16280                       {
16281                         unqualified_name = constructor_name (class_type);
16282                         sfk = sfk_constructor;
16283                       }
16284                     else if (is_overloaded_fn (unqualified_name)
16285                              && DECL_CONSTRUCTOR_P (get_first_fn
16286                                                     (unqualified_name)))
16287                       sfk = sfk_constructor;
16288
16289                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16290                       *ctor_dtor_or_conv_p = -1;
16291                   }
16292               }
16293             declarator = make_id_declarator (qualifying_scope,
16294                                              unqualified_name,
16295                                              sfk);
16296             declarator->id_loc = token->location;
16297             declarator->parameter_pack_p = pack_expansion_p;
16298
16299             if (pack_expansion_p)
16300               maybe_warn_variadic_templates ();
16301           }
16302
16303         handle_declarator:;
16304           scope = get_scope_of_declarator (declarator);
16305           if (scope)
16306             /* Any names that appear after the declarator-id for a
16307                member are looked up in the containing scope.  */
16308             pushed_scope = push_scope (scope);
16309           parser->in_declarator_p = true;
16310           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16311               || (declarator && declarator->kind == cdk_id))
16312             /* Default args are only allowed on function
16313                declarations.  */
16314             parser->default_arg_ok_p = saved_default_arg_ok_p;
16315           else
16316             parser->default_arg_ok_p = false;
16317
16318           first = false;
16319         }
16320       /* We're done.  */
16321       else
16322         break;
16323     }
16324
16325   /* For an abstract declarator, we might wind up with nothing at this
16326      point.  That's an error; the declarator is not optional.  */
16327   if (!declarator)
16328     cp_parser_error (parser, "expected declarator");
16329
16330   /* If we entered a scope, we must exit it now.  */
16331   if (pushed_scope)
16332     pop_scope (pushed_scope);
16333
16334   parser->default_arg_ok_p = saved_default_arg_ok_p;
16335   parser->in_declarator_p = saved_in_declarator_p;
16336
16337   return declarator;
16338 }
16339
16340 /* Parse a ptr-operator.
16341
16342    ptr-operator:
16343      * cv-qualifier-seq [opt]
16344      &
16345      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16346
16347    GNU Extension:
16348
16349    ptr-operator:
16350      & cv-qualifier-seq [opt]
16351
16352    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16353    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16354    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16355    filled in with the TYPE containing the member.  *CV_QUALS is
16356    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16357    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16358    Note that the tree codes returned by this function have nothing
16359    to do with the types of trees that will be eventually be created
16360    to represent the pointer or reference type being parsed. They are
16361    just constants with suggestive names. */
16362 static enum tree_code
16363 cp_parser_ptr_operator (cp_parser* parser,
16364                         tree* type,
16365                         cp_cv_quals *cv_quals)
16366 {
16367   enum tree_code code = ERROR_MARK;
16368   cp_token *token;
16369
16370   /* Assume that it's not a pointer-to-member.  */
16371   *type = NULL_TREE;
16372   /* And that there are no cv-qualifiers.  */
16373   *cv_quals = TYPE_UNQUALIFIED;
16374
16375   /* Peek at the next token.  */
16376   token = cp_lexer_peek_token (parser->lexer);
16377
16378   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16379   if (token->type == CPP_MULT)
16380     code = INDIRECT_REF;
16381   else if (token->type == CPP_AND)
16382     code = ADDR_EXPR;
16383   else if ((cxx_dialect != cxx98) &&
16384            token->type == CPP_AND_AND) /* C++0x only */
16385     code = NON_LVALUE_EXPR;
16386
16387   if (code != ERROR_MARK)
16388     {
16389       /* Consume the `*', `&' or `&&'.  */
16390       cp_lexer_consume_token (parser->lexer);
16391
16392       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16393          `&', if we are allowing GNU extensions.  (The only qualifier
16394          that can legally appear after `&' is `restrict', but that is
16395          enforced during semantic analysis.  */
16396       if (code == INDIRECT_REF
16397           || cp_parser_allow_gnu_extensions_p (parser))
16398         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16399     }
16400   else
16401     {
16402       /* Try the pointer-to-member case.  */
16403       cp_parser_parse_tentatively (parser);
16404       /* Look for the optional `::' operator.  */
16405       cp_parser_global_scope_opt (parser,
16406                                   /*current_scope_valid_p=*/false);
16407       /* Look for the nested-name specifier.  */
16408       token = cp_lexer_peek_token (parser->lexer);
16409       cp_parser_nested_name_specifier (parser,
16410                                        /*typename_keyword_p=*/false,
16411                                        /*check_dependency_p=*/true,
16412                                        /*type_p=*/false,
16413                                        /*is_declaration=*/false);
16414       /* If we found it, and the next token is a `*', then we are
16415          indeed looking at a pointer-to-member operator.  */
16416       if (!cp_parser_error_occurred (parser)
16417           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16418         {
16419           /* Indicate that the `*' operator was used.  */
16420           code = INDIRECT_REF;
16421
16422           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16423             error_at (token->location, "%qD is a namespace", parser->scope);
16424           else
16425             {
16426               /* The type of which the member is a member is given by the
16427                  current SCOPE.  */
16428               *type = parser->scope;
16429               /* The next name will not be qualified.  */
16430               parser->scope = NULL_TREE;
16431               parser->qualifying_scope = NULL_TREE;
16432               parser->object_scope = NULL_TREE;
16433               /* Look for the optional cv-qualifier-seq.  */
16434               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16435             }
16436         }
16437       /* If that didn't work we don't have a ptr-operator.  */
16438       if (!cp_parser_parse_definitely (parser))
16439         cp_parser_error (parser, "expected ptr-operator");
16440     }
16441
16442   return code;
16443 }
16444
16445 /* Parse an (optional) cv-qualifier-seq.
16446
16447    cv-qualifier-seq:
16448      cv-qualifier cv-qualifier-seq [opt]
16449
16450    cv-qualifier:
16451      const
16452      volatile
16453
16454    GNU Extension:
16455
16456    cv-qualifier:
16457      __restrict__
16458
16459    Returns a bitmask representing the cv-qualifiers.  */
16460
16461 static cp_cv_quals
16462 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16463 {
16464   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16465
16466   while (true)
16467     {
16468       cp_token *token;
16469       cp_cv_quals cv_qualifier;
16470
16471       /* Peek at the next token.  */
16472       token = cp_lexer_peek_token (parser->lexer);
16473       /* See if it's a cv-qualifier.  */
16474       switch (token->keyword)
16475         {
16476         case RID_CONST:
16477           cv_qualifier = TYPE_QUAL_CONST;
16478           break;
16479
16480         case RID_VOLATILE:
16481           cv_qualifier = TYPE_QUAL_VOLATILE;
16482           break;
16483
16484         case RID_RESTRICT:
16485           cv_qualifier = TYPE_QUAL_RESTRICT;
16486           break;
16487
16488         default:
16489           cv_qualifier = TYPE_UNQUALIFIED;
16490           break;
16491         }
16492
16493       if (!cv_qualifier)
16494         break;
16495
16496       if (cv_quals & cv_qualifier)
16497         {
16498           error_at (token->location, "duplicate cv-qualifier");
16499           cp_lexer_purge_token (parser->lexer);
16500         }
16501       else
16502         {
16503           cp_lexer_consume_token (parser->lexer);
16504           cv_quals |= cv_qualifier;
16505         }
16506     }
16507
16508   return cv_quals;
16509 }
16510
16511 /* Parse an (optional) virt-specifier-seq.
16512
16513    virt-specifier-seq:
16514      virt-specifier virt-specifier-seq [opt]
16515
16516    virt-specifier:
16517      override
16518      final
16519
16520    Returns a bitmask representing the virt-specifiers.  */
16521
16522 static cp_virt_specifiers
16523 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16524 {
16525   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16526
16527   while (true)
16528     {
16529       cp_token *token;
16530       cp_virt_specifiers virt_specifier;
16531
16532       /* Peek at the next token.  */
16533       token = cp_lexer_peek_token (parser->lexer);
16534       /* See if it's a virt-specifier-qualifier.  */
16535       if (token->type != CPP_NAME)
16536         break;
16537       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16538         {
16539           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16540           virt_specifier = VIRT_SPEC_OVERRIDE;
16541         }
16542       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16543         {
16544           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16545           virt_specifier = VIRT_SPEC_FINAL;
16546         }
16547       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16548         {
16549           virt_specifier = VIRT_SPEC_FINAL;
16550         }
16551       else
16552         break;
16553
16554       if (virt_specifiers & virt_specifier)
16555         {
16556           error_at (token->location, "duplicate virt-specifier");
16557           cp_lexer_purge_token (parser->lexer);
16558         }
16559       else
16560         {
16561           cp_lexer_consume_token (parser->lexer);
16562           virt_specifiers |= virt_specifier;
16563         }
16564     }
16565   return virt_specifiers;
16566 }
16567
16568 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16569    is in scope even though it isn't real.  */
16570
16571 static void
16572 inject_this_parameter (tree ctype, cp_cv_quals quals)
16573 {
16574   tree this_parm;
16575
16576   if (current_class_ptr)
16577     {
16578       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16579       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16580       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16581           && cp_type_quals (type) == quals)
16582         return;
16583     }
16584
16585   this_parm = build_this_parm (ctype, quals);
16586   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16587   current_class_ptr = NULL_TREE;
16588   current_class_ref
16589     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16590   current_class_ptr = this_parm;
16591 }
16592
16593 /* Parse a late-specified return type, if any.  This is not a separate
16594    non-terminal, but part of a function declarator, which looks like
16595
16596    -> trailing-type-specifier-seq abstract-declarator(opt)
16597
16598    Returns the type indicated by the type-id.
16599
16600    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16601    function.  */
16602
16603 static tree
16604 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16605 {
16606   cp_token *token;
16607   tree type;
16608
16609   /* Peek at the next token.  */
16610   token = cp_lexer_peek_token (parser->lexer);
16611   /* A late-specified return type is indicated by an initial '->'. */
16612   if (token->type != CPP_DEREF)
16613     return NULL_TREE;
16614
16615   /* Consume the ->.  */
16616   cp_lexer_consume_token (parser->lexer);
16617
16618   if (quals >= 0)
16619     {
16620       /* DR 1207: 'this' is in scope in the trailing return type.  */
16621       gcc_assert (current_class_ptr == NULL_TREE);
16622       inject_this_parameter (current_class_type, quals);
16623     }
16624
16625   type = cp_parser_trailing_type_id (parser);
16626
16627   if (quals >= 0)
16628     current_class_ptr = current_class_ref = NULL_TREE;
16629
16630   return type;
16631 }
16632
16633 /* Parse a declarator-id.
16634
16635    declarator-id:
16636      id-expression
16637      :: [opt] nested-name-specifier [opt] type-name
16638
16639    In the `id-expression' case, the value returned is as for
16640    cp_parser_id_expression if the id-expression was an unqualified-id.
16641    If the id-expression was a qualified-id, then a SCOPE_REF is
16642    returned.  The first operand is the scope (either a NAMESPACE_DECL
16643    or TREE_TYPE), but the second is still just a representation of an
16644    unqualified-id.  */
16645
16646 static tree
16647 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16648 {
16649   tree id;
16650   /* The expression must be an id-expression.  Assume that qualified
16651      names are the names of types so that:
16652
16653        template <class T>
16654        int S<T>::R::i = 3;
16655
16656      will work; we must treat `S<T>::R' as the name of a type.
16657      Similarly, assume that qualified names are templates, where
16658      required, so that:
16659
16660        template <class T>
16661        int S<T>::R<T>::i = 3;
16662
16663      will work, too.  */
16664   id = cp_parser_id_expression (parser,
16665                                 /*template_keyword_p=*/false,
16666                                 /*check_dependency_p=*/false,
16667                                 /*template_p=*/NULL,
16668                                 /*declarator_p=*/true,
16669                                 optional_p);
16670   if (id && BASELINK_P (id))
16671     id = BASELINK_FUNCTIONS (id);
16672   return id;
16673 }
16674
16675 /* Parse a type-id.
16676
16677    type-id:
16678      type-specifier-seq abstract-declarator [opt]
16679
16680    Returns the TYPE specified.  */
16681
16682 static tree
16683 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16684                      bool is_trailing_return)
16685 {
16686   cp_decl_specifier_seq type_specifier_seq;
16687   cp_declarator *abstract_declarator;
16688
16689   /* Parse the type-specifier-seq.  */
16690   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16691                                 is_trailing_return,
16692                                 &type_specifier_seq);
16693   if (type_specifier_seq.type == error_mark_node)
16694     return error_mark_node;
16695
16696   /* There might or might not be an abstract declarator.  */
16697   cp_parser_parse_tentatively (parser);
16698   /* Look for the declarator.  */
16699   abstract_declarator
16700     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16701                             /*parenthesized_p=*/NULL,
16702                             /*member_p=*/false);
16703   /* Check to see if there really was a declarator.  */
16704   if (!cp_parser_parse_definitely (parser))
16705     abstract_declarator = NULL;
16706
16707   if (type_specifier_seq.type
16708       && type_uses_auto (type_specifier_seq.type))
16709     {
16710       /* A type-id with type 'auto' is only ok if the abstract declarator
16711          is a function declarator with a late-specified return type.  */
16712       if (abstract_declarator
16713           && abstract_declarator->kind == cdk_function
16714           && abstract_declarator->u.function.late_return_type)
16715         /* OK */;
16716       else
16717         {
16718           error ("invalid use of %<auto%>");
16719           return error_mark_node;
16720         }
16721     }
16722   
16723   return groktypename (&type_specifier_seq, abstract_declarator,
16724                        is_template_arg);
16725 }
16726
16727 static tree cp_parser_type_id (cp_parser *parser)
16728 {
16729   return cp_parser_type_id_1 (parser, false, false);
16730 }
16731
16732 static tree cp_parser_template_type_arg (cp_parser *parser)
16733 {
16734   tree r;
16735   const char *saved_message = parser->type_definition_forbidden_message;
16736   parser->type_definition_forbidden_message
16737     = G_("types may not be defined in template arguments");
16738   r = cp_parser_type_id_1 (parser, true, false);
16739   parser->type_definition_forbidden_message = saved_message;
16740   return r;
16741 }
16742
16743 static tree cp_parser_trailing_type_id (cp_parser *parser)
16744 {
16745   return cp_parser_type_id_1 (parser, false, true);
16746 }
16747
16748 /* Parse a type-specifier-seq.
16749
16750    type-specifier-seq:
16751      type-specifier type-specifier-seq [opt]
16752
16753    GNU extension:
16754
16755    type-specifier-seq:
16756      attributes type-specifier-seq [opt]
16757
16758    If IS_DECLARATION is true, we are at the start of a "condition" or
16759    exception-declaration, so we might be followed by a declarator-id.
16760
16761    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16762    i.e. we've just seen "->".
16763
16764    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16765
16766 static void
16767 cp_parser_type_specifier_seq (cp_parser* parser,
16768                               bool is_declaration,
16769                               bool is_trailing_return,
16770                               cp_decl_specifier_seq *type_specifier_seq)
16771 {
16772   bool seen_type_specifier = false;
16773   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16774   cp_token *start_token = NULL;
16775
16776   /* Clear the TYPE_SPECIFIER_SEQ.  */
16777   clear_decl_specs (type_specifier_seq);
16778
16779   /* In the context of a trailing return type, enum E { } is an
16780      elaborated-type-specifier followed by a function-body, not an
16781      enum-specifier.  */
16782   if (is_trailing_return)
16783     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16784
16785   /* Parse the type-specifiers and attributes.  */
16786   while (true)
16787     {
16788       tree type_specifier;
16789       bool is_cv_qualifier;
16790
16791       /* Check for attributes first.  */
16792       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16793         {
16794           type_specifier_seq->attributes =
16795             chainon (type_specifier_seq->attributes,
16796                      cp_parser_attributes_opt (parser));
16797           continue;
16798         }
16799
16800       /* record the token of the beginning of the type specifier seq,
16801          for error reporting purposes*/
16802      if (!start_token)
16803        start_token = cp_lexer_peek_token (parser->lexer);
16804
16805       /* Look for the type-specifier.  */
16806       type_specifier = cp_parser_type_specifier (parser,
16807                                                  flags,
16808                                                  type_specifier_seq,
16809                                                  /*is_declaration=*/false,
16810                                                  NULL,
16811                                                  &is_cv_qualifier);
16812       if (!type_specifier)
16813         {
16814           /* If the first type-specifier could not be found, this is not a
16815              type-specifier-seq at all.  */
16816           if (!seen_type_specifier)
16817             {
16818               cp_parser_error (parser, "expected type-specifier");
16819               type_specifier_seq->type = error_mark_node;
16820               return;
16821             }
16822           /* If subsequent type-specifiers could not be found, the
16823              type-specifier-seq is complete.  */
16824           break;
16825         }
16826
16827       seen_type_specifier = true;
16828       /* The standard says that a condition can be:
16829
16830             type-specifier-seq declarator = assignment-expression
16831
16832          However, given:
16833
16834            struct S {};
16835            if (int S = ...)
16836
16837          we should treat the "S" as a declarator, not as a
16838          type-specifier.  The standard doesn't say that explicitly for
16839          type-specifier-seq, but it does say that for
16840          decl-specifier-seq in an ordinary declaration.  Perhaps it
16841          would be clearer just to allow a decl-specifier-seq here, and
16842          then add a semantic restriction that if any decl-specifiers
16843          that are not type-specifiers appear, the program is invalid.  */
16844       if (is_declaration && !is_cv_qualifier)
16845         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16846     }
16847
16848   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16849 }
16850
16851 /* Parse a parameter-declaration-clause.
16852
16853    parameter-declaration-clause:
16854      parameter-declaration-list [opt] ... [opt]
16855      parameter-declaration-list , ...
16856
16857    Returns a representation for the parameter declarations.  A return
16858    value of NULL indicates a parameter-declaration-clause consisting
16859    only of an ellipsis.  */
16860
16861 static tree
16862 cp_parser_parameter_declaration_clause (cp_parser* parser)
16863 {
16864   tree parameters;
16865   cp_token *token;
16866   bool ellipsis_p;
16867   bool is_error;
16868
16869   /* Peek at the next token.  */
16870   token = cp_lexer_peek_token (parser->lexer);
16871   /* Check for trivial parameter-declaration-clauses.  */
16872   if (token->type == CPP_ELLIPSIS)
16873     {
16874       /* Consume the `...' token.  */
16875       cp_lexer_consume_token (parser->lexer);
16876       return NULL_TREE;
16877     }
16878   else if (token->type == CPP_CLOSE_PAREN)
16879     /* There are no parameters.  */
16880     {
16881 #ifndef NO_IMPLICIT_EXTERN_C
16882       if (in_system_header && current_class_type == NULL
16883           && current_lang_name == lang_name_c)
16884         return NULL_TREE;
16885       else
16886 #endif
16887         return void_list_node;
16888     }
16889   /* Check for `(void)', too, which is a special case.  */
16890   else if (token->keyword == RID_VOID
16891            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16892                == CPP_CLOSE_PAREN))
16893     {
16894       /* Consume the `void' token.  */
16895       cp_lexer_consume_token (parser->lexer);
16896       /* There are no parameters.  */
16897       return void_list_node;
16898     }
16899
16900   /* Parse the parameter-declaration-list.  */
16901   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16902   /* If a parse error occurred while parsing the
16903      parameter-declaration-list, then the entire
16904      parameter-declaration-clause is erroneous.  */
16905   if (is_error)
16906     return NULL;
16907
16908   /* Peek at the next token.  */
16909   token = cp_lexer_peek_token (parser->lexer);
16910   /* If it's a `,', the clause should terminate with an ellipsis.  */
16911   if (token->type == CPP_COMMA)
16912     {
16913       /* Consume the `,'.  */
16914       cp_lexer_consume_token (parser->lexer);
16915       /* Expect an ellipsis.  */
16916       ellipsis_p
16917         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16918     }
16919   /* It might also be `...' if the optional trailing `,' was
16920      omitted.  */
16921   else if (token->type == CPP_ELLIPSIS)
16922     {
16923       /* Consume the `...' token.  */
16924       cp_lexer_consume_token (parser->lexer);
16925       /* And remember that we saw it.  */
16926       ellipsis_p = true;
16927     }
16928   else
16929     ellipsis_p = false;
16930
16931   /* Finish the parameter list.  */
16932   if (!ellipsis_p)
16933     parameters = chainon (parameters, void_list_node);
16934
16935   return parameters;
16936 }
16937
16938 /* Parse a parameter-declaration-list.
16939
16940    parameter-declaration-list:
16941      parameter-declaration
16942      parameter-declaration-list , parameter-declaration
16943
16944    Returns a representation of the parameter-declaration-list, as for
16945    cp_parser_parameter_declaration_clause.  However, the
16946    `void_list_node' is never appended to the list.  Upon return,
16947    *IS_ERROR will be true iff an error occurred.  */
16948
16949 static tree
16950 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16951 {
16952   tree parameters = NULL_TREE;
16953   tree *tail = &parameters; 
16954   bool saved_in_unbraced_linkage_specification_p;
16955   int index = 0;
16956
16957   /* Assume all will go well.  */
16958   *is_error = false;
16959   /* The special considerations that apply to a function within an
16960      unbraced linkage specifications do not apply to the parameters
16961      to the function.  */
16962   saved_in_unbraced_linkage_specification_p 
16963     = parser->in_unbraced_linkage_specification_p;
16964   parser->in_unbraced_linkage_specification_p = false;
16965
16966   /* Look for more parameters.  */
16967   while (true)
16968     {
16969       cp_parameter_declarator *parameter;
16970       tree decl = error_mark_node;
16971       bool parenthesized_p = false;
16972       /* Parse the parameter.  */
16973       parameter
16974         = cp_parser_parameter_declaration (parser,
16975                                            /*template_parm_p=*/false,
16976                                            &parenthesized_p);
16977
16978       /* We don't know yet if the enclosing context is deprecated, so wait
16979          and warn in grokparms if appropriate.  */
16980       deprecated_state = DEPRECATED_SUPPRESS;
16981
16982       if (parameter)
16983         decl = grokdeclarator (parameter->declarator,
16984                                &parameter->decl_specifiers,
16985                                PARM,
16986                                parameter->default_argument != NULL_TREE,
16987                                &parameter->decl_specifiers.attributes);
16988
16989       deprecated_state = DEPRECATED_NORMAL;
16990
16991       /* If a parse error occurred parsing the parameter declaration,
16992          then the entire parameter-declaration-list is erroneous.  */
16993       if (decl == error_mark_node)
16994         {
16995           *is_error = true;
16996           parameters = error_mark_node;
16997           break;
16998         }
16999
17000       if (parameter->decl_specifiers.attributes)
17001         cplus_decl_attributes (&decl,
17002                                parameter->decl_specifiers.attributes,
17003                                0);
17004       if (DECL_NAME (decl))
17005         decl = pushdecl (decl);
17006
17007       if (decl != error_mark_node)
17008         {
17009           retrofit_lang_decl (decl);
17010           DECL_PARM_INDEX (decl) = ++index;
17011           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17012         }
17013
17014       /* Add the new parameter to the list.  */
17015       *tail = build_tree_list (parameter->default_argument, decl);
17016       tail = &TREE_CHAIN (*tail);
17017
17018       /* Peek at the next token.  */
17019       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17020           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17021           /* These are for Objective-C++ */
17022           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17023           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17024         /* The parameter-declaration-list is complete.  */
17025         break;
17026       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17027         {
17028           cp_token *token;
17029
17030           /* Peek at the next token.  */
17031           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17032           /* If it's an ellipsis, then the list is complete.  */
17033           if (token->type == CPP_ELLIPSIS)
17034             break;
17035           /* Otherwise, there must be more parameters.  Consume the
17036              `,'.  */
17037           cp_lexer_consume_token (parser->lexer);
17038           /* When parsing something like:
17039
17040                 int i(float f, double d)
17041
17042              we can tell after seeing the declaration for "f" that we
17043              are not looking at an initialization of a variable "i",
17044              but rather at the declaration of a function "i".
17045
17046              Due to the fact that the parsing of template arguments
17047              (as specified to a template-id) requires backtracking we
17048              cannot use this technique when inside a template argument
17049              list.  */
17050           if (!parser->in_template_argument_list_p
17051               && !parser->in_type_id_in_expr_p
17052               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17053               /* However, a parameter-declaration of the form
17054                  "foat(f)" (which is a valid declaration of a
17055                  parameter "f") can also be interpreted as an
17056                  expression (the conversion of "f" to "float").  */
17057               && !parenthesized_p)
17058             cp_parser_commit_to_tentative_parse (parser);
17059         }
17060       else
17061         {
17062           cp_parser_error (parser, "expected %<,%> or %<...%>");
17063           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17064             cp_parser_skip_to_closing_parenthesis (parser,
17065                                                    /*recovering=*/true,
17066                                                    /*or_comma=*/false,
17067                                                    /*consume_paren=*/false);
17068           break;
17069         }
17070     }
17071
17072   parser->in_unbraced_linkage_specification_p
17073     = saved_in_unbraced_linkage_specification_p;
17074
17075   return parameters;
17076 }
17077
17078 /* Parse a parameter declaration.
17079
17080    parameter-declaration:
17081      decl-specifier-seq ... [opt] declarator
17082      decl-specifier-seq declarator = assignment-expression
17083      decl-specifier-seq ... [opt] abstract-declarator [opt]
17084      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17085
17086    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17087    declares a template parameter.  (In that case, a non-nested `>'
17088    token encountered during the parsing of the assignment-expression
17089    is not interpreted as a greater-than operator.)
17090
17091    Returns a representation of the parameter, or NULL if an error
17092    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17093    true iff the declarator is of the form "(p)".  */
17094
17095 static cp_parameter_declarator *
17096 cp_parser_parameter_declaration (cp_parser *parser,
17097                                  bool template_parm_p,
17098                                  bool *parenthesized_p)
17099 {
17100   int declares_class_or_enum;
17101   cp_decl_specifier_seq decl_specifiers;
17102   cp_declarator *declarator;
17103   tree default_argument;
17104   cp_token *token = NULL, *declarator_token_start = NULL;
17105   const char *saved_message;
17106
17107   /* In a template parameter, `>' is not an operator.
17108
17109      [temp.param]
17110
17111      When parsing a default template-argument for a non-type
17112      template-parameter, the first non-nested `>' is taken as the end
17113      of the template parameter-list rather than a greater-than
17114      operator.  */
17115
17116   /* Type definitions may not appear in parameter types.  */
17117   saved_message = parser->type_definition_forbidden_message;
17118   parser->type_definition_forbidden_message
17119     = G_("types may not be defined in parameter types");
17120
17121   /* Parse the declaration-specifiers.  */
17122   cp_parser_decl_specifier_seq (parser,
17123                                 CP_PARSER_FLAGS_NONE,
17124                                 &decl_specifiers,
17125                                 &declares_class_or_enum);
17126
17127   /* Complain about missing 'typename' or other invalid type names.  */
17128   if (!decl_specifiers.any_type_specifiers_p)
17129     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17130
17131   /* If an error occurred, there's no reason to attempt to parse the
17132      rest of the declaration.  */
17133   if (cp_parser_error_occurred (parser))
17134     {
17135       parser->type_definition_forbidden_message = saved_message;
17136       return NULL;
17137     }
17138
17139   /* Peek at the next token.  */
17140   token = cp_lexer_peek_token (parser->lexer);
17141
17142   /* If the next token is a `)', `,', `=', `>', or `...', then there
17143      is no declarator. However, when variadic templates are enabled,
17144      there may be a declarator following `...'.  */
17145   if (token->type == CPP_CLOSE_PAREN
17146       || token->type == CPP_COMMA
17147       || token->type == CPP_EQ
17148       || token->type == CPP_GREATER)
17149     {
17150       declarator = NULL;
17151       if (parenthesized_p)
17152         *parenthesized_p = false;
17153     }
17154   /* Otherwise, there should be a declarator.  */
17155   else
17156     {
17157       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17158       parser->default_arg_ok_p = false;
17159
17160       /* After seeing a decl-specifier-seq, if the next token is not a
17161          "(", there is no possibility that the code is a valid
17162          expression.  Therefore, if parsing tentatively, we commit at
17163          this point.  */
17164       if (!parser->in_template_argument_list_p
17165           /* In an expression context, having seen:
17166
17167                (int((char ...
17168
17169              we cannot be sure whether we are looking at a
17170              function-type (taking a "char" as a parameter) or a cast
17171              of some object of type "char" to "int".  */
17172           && !parser->in_type_id_in_expr_p
17173           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17174           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17175           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17176         cp_parser_commit_to_tentative_parse (parser);
17177       /* Parse the declarator.  */
17178       declarator_token_start = token;
17179       declarator = cp_parser_declarator (parser,
17180                                          CP_PARSER_DECLARATOR_EITHER,
17181                                          /*ctor_dtor_or_conv_p=*/NULL,
17182                                          parenthesized_p,
17183                                          /*member_p=*/false);
17184       parser->default_arg_ok_p = saved_default_arg_ok_p;
17185       /* After the declarator, allow more attributes.  */
17186       decl_specifiers.attributes
17187         = chainon (decl_specifiers.attributes,
17188                    cp_parser_attributes_opt (parser));
17189     }
17190
17191   /* If the next token is an ellipsis, and we have not seen a
17192      declarator name, and the type of the declarator contains parameter
17193      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17194      a parameter pack expansion expression. Otherwise, leave the
17195      ellipsis for a C-style variadic function. */
17196   token = cp_lexer_peek_token (parser->lexer);
17197   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17198     {
17199       tree type = decl_specifiers.type;
17200
17201       if (type && DECL_P (type))
17202         type = TREE_TYPE (type);
17203
17204       if (type
17205           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17206           && declarator_can_be_parameter_pack (declarator)
17207           && (!declarator || !declarator->parameter_pack_p)
17208           && uses_parameter_packs (type))
17209         {
17210           /* Consume the `...'. */
17211           cp_lexer_consume_token (parser->lexer);
17212           maybe_warn_variadic_templates ();
17213           
17214           /* Build a pack expansion type */
17215           if (declarator)
17216             declarator->parameter_pack_p = true;
17217           else
17218             decl_specifiers.type = make_pack_expansion (type);
17219         }
17220     }
17221
17222   /* The restriction on defining new types applies only to the type
17223      of the parameter, not to the default argument.  */
17224   parser->type_definition_forbidden_message = saved_message;
17225
17226   /* If the next token is `=', then process a default argument.  */
17227   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17228     {
17229       /* If we are defining a class, then the tokens that make up the
17230          default argument must be saved and processed later.  */
17231       if (!template_parm_p && at_class_scope_p ()
17232           && TYPE_BEING_DEFINED (current_class_type)
17233           && !LAMBDA_TYPE_P (current_class_type))
17234         {
17235           unsigned depth = 0;
17236           int maybe_template_id = 0;
17237           cp_token *first_token;
17238           cp_token *token;
17239
17240           /* Add tokens until we have processed the entire default
17241              argument.  We add the range [first_token, token).  */
17242           first_token = cp_lexer_peek_token (parser->lexer);
17243           while (true)
17244             {
17245               bool done = false;
17246
17247               /* Peek at the next token.  */
17248               token = cp_lexer_peek_token (parser->lexer);
17249               /* What we do depends on what token we have.  */
17250               switch (token->type)
17251                 {
17252                   /* In valid code, a default argument must be
17253                      immediately followed by a `,' `)', or `...'.  */
17254                 case CPP_COMMA:
17255                   if (depth == 0 && maybe_template_id)
17256                     {
17257                       /* If we've seen a '<', we might be in a
17258                          template-argument-list.  Until Core issue 325 is
17259                          resolved, we don't know how this situation ought
17260                          to be handled, so try to DTRT.  We check whether
17261                          what comes after the comma is a valid parameter
17262                          declaration list.  If it is, then the comma ends
17263                          the default argument; otherwise the default
17264                          argument continues.  */
17265                       bool error = false;
17266                       tree t;
17267
17268                       /* Set ITALP so cp_parser_parameter_declaration_list
17269                          doesn't decide to commit to this parse.  */
17270                       bool saved_italp = parser->in_template_argument_list_p;
17271                       parser->in_template_argument_list_p = true;
17272
17273                       cp_parser_parse_tentatively (parser);
17274                       cp_lexer_consume_token (parser->lexer);
17275                       begin_scope (sk_function_parms, NULL_TREE);
17276                       cp_parser_parameter_declaration_list (parser, &error);
17277                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17278                         pop_binding (DECL_NAME (t), t);
17279                       leave_scope ();
17280                       if (!cp_parser_error_occurred (parser) && !error)
17281                         done = true;
17282                       cp_parser_abort_tentative_parse (parser);
17283
17284                       parser->in_template_argument_list_p = saved_italp;
17285                       break;
17286                     }
17287                 case CPP_CLOSE_PAREN:
17288                 case CPP_ELLIPSIS:
17289                   /* If we run into a non-nested `;', `}', or `]',
17290                      then the code is invalid -- but the default
17291                      argument is certainly over.  */
17292                 case CPP_SEMICOLON:
17293                 case CPP_CLOSE_BRACE:
17294                 case CPP_CLOSE_SQUARE:
17295                   if (depth == 0)
17296                     done = true;
17297                   /* Update DEPTH, if necessary.  */
17298                   else if (token->type == CPP_CLOSE_PAREN
17299                            || token->type == CPP_CLOSE_BRACE
17300                            || token->type == CPP_CLOSE_SQUARE)
17301                     --depth;
17302                   break;
17303
17304                 case CPP_OPEN_PAREN:
17305                 case CPP_OPEN_SQUARE:
17306                 case CPP_OPEN_BRACE:
17307                   ++depth;
17308                   break;
17309
17310                 case CPP_LESS:
17311                   if (depth == 0)
17312                     /* This might be the comparison operator, or it might
17313                        start a template argument list.  */
17314                     ++maybe_template_id;
17315                   break;
17316
17317                 case CPP_RSHIFT:
17318                   if (cxx_dialect == cxx98)
17319                     break;
17320                   /* Fall through for C++0x, which treats the `>>'
17321                      operator like two `>' tokens in certain
17322                      cases.  */
17323
17324                 case CPP_GREATER:
17325                   if (depth == 0)
17326                     {
17327                       /* This might be an operator, or it might close a
17328                          template argument list.  But if a previous '<'
17329                          started a template argument list, this will have
17330                          closed it, so we can't be in one anymore.  */
17331                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17332                       if (maybe_template_id < 0)
17333                         maybe_template_id = 0;
17334                     }
17335                   break;
17336
17337                   /* If we run out of tokens, issue an error message.  */
17338                 case CPP_EOF:
17339                 case CPP_PRAGMA_EOL:
17340                   error_at (token->location, "file ends in default argument");
17341                   done = true;
17342                   break;
17343
17344                 case CPP_NAME:
17345                 case CPP_SCOPE:
17346                   /* In these cases, we should look for template-ids.
17347                      For example, if the default argument is
17348                      `X<int, double>()', we need to do name lookup to
17349                      figure out whether or not `X' is a template; if
17350                      so, the `,' does not end the default argument.
17351
17352                      That is not yet done.  */
17353                   break;
17354
17355                 default:
17356                   break;
17357                 }
17358
17359               /* If we've reached the end, stop.  */
17360               if (done)
17361                 break;
17362
17363               /* Add the token to the token block.  */
17364               token = cp_lexer_consume_token (parser->lexer);
17365             }
17366
17367           /* Create a DEFAULT_ARG to represent the unparsed default
17368              argument.  */
17369           default_argument = make_node (DEFAULT_ARG);
17370           DEFARG_TOKENS (default_argument)
17371             = cp_token_cache_new (first_token, token);
17372           DEFARG_INSTANTIATIONS (default_argument) = NULL;
17373         }
17374       /* Outside of a class definition, we can just parse the
17375          assignment-expression.  */
17376       else
17377         {
17378           token = cp_lexer_peek_token (parser->lexer);
17379           default_argument 
17380             = cp_parser_default_argument (parser, template_parm_p);
17381         }
17382
17383       if (!parser->default_arg_ok_p)
17384         {
17385           if (flag_permissive)
17386             warning (0, "deprecated use of default argument for parameter of non-function");
17387           else
17388             {
17389               error_at (token->location,
17390                         "default arguments are only "
17391                         "permitted for function parameters");
17392               default_argument = NULL_TREE;
17393             }
17394         }
17395       else if ((declarator && declarator->parameter_pack_p)
17396                || (decl_specifiers.type
17397                    && PACK_EXPANSION_P (decl_specifiers.type)))
17398         {
17399           /* Find the name of the parameter pack.  */     
17400           cp_declarator *id_declarator = declarator;
17401           while (id_declarator && id_declarator->kind != cdk_id)
17402             id_declarator = id_declarator->declarator;
17403           
17404           if (id_declarator && id_declarator->kind == cdk_id)
17405             error_at (declarator_token_start->location,
17406                       template_parm_p
17407                       ? G_("template parameter pack %qD "
17408                            "cannot have a default argument")
17409                       : G_("parameter pack %qD cannot have "
17410                            "a default argument"),
17411                       id_declarator->u.id.unqualified_name);
17412           else
17413             error_at (declarator_token_start->location,
17414                       template_parm_p
17415                       ? G_("template parameter pack cannot have "
17416                            "a default argument")
17417                       : G_("parameter pack cannot have a "
17418                            "default argument"));
17419
17420           default_argument = NULL_TREE;
17421         }
17422     }
17423   else
17424     default_argument = NULL_TREE;
17425
17426   return make_parameter_declarator (&decl_specifiers,
17427                                     declarator,
17428                                     default_argument);
17429 }
17430
17431 /* Parse a default argument and return it.
17432
17433    TEMPLATE_PARM_P is true if this is a default argument for a
17434    non-type template parameter.  */
17435 static tree
17436 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17437 {
17438   tree default_argument = NULL_TREE;
17439   bool saved_greater_than_is_operator_p;
17440   bool saved_local_variables_forbidden_p;
17441   bool non_constant_p, is_direct_init;
17442
17443   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17444      set correctly.  */
17445   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17446   parser->greater_than_is_operator_p = !template_parm_p;
17447   /* Local variable names (and the `this' keyword) may not
17448      appear in a default argument.  */
17449   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17450   parser->local_variables_forbidden_p = true;
17451   /* Parse the assignment-expression.  */
17452   if (template_parm_p)
17453     push_deferring_access_checks (dk_no_deferred);
17454   default_argument
17455     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17456   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17457     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17458   if (template_parm_p)
17459     pop_deferring_access_checks ();
17460   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17461   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17462
17463   return default_argument;
17464 }
17465
17466 /* Parse a function-body.
17467
17468    function-body:
17469      compound_statement  */
17470
17471 static void
17472 cp_parser_function_body (cp_parser *parser)
17473 {
17474   cp_parser_compound_statement (parser, NULL, false, true);
17475 }
17476
17477 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17478    true if a ctor-initializer was present.  */
17479
17480 static bool
17481 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17482 {
17483   tree body, list;
17484   bool ctor_initializer_p;
17485   const bool check_body_p =
17486      DECL_CONSTRUCTOR_P (current_function_decl)
17487      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17488   tree last = NULL;
17489
17490   /* Begin the function body.  */
17491   body = begin_function_body ();
17492   /* Parse the optional ctor-initializer.  */
17493   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17494
17495   /* If we're parsing a constexpr constructor definition, we need
17496      to check that the constructor body is indeed empty.  However,
17497      before we get to cp_parser_function_body lot of junk has been
17498      generated, so we can't just check that we have an empty block.
17499      Rather we take a snapshot of the outermost block, and check whether
17500      cp_parser_function_body changed its state.  */
17501   if (check_body_p)
17502     {
17503       list = body;
17504       if (TREE_CODE (list) == BIND_EXPR)
17505         list = BIND_EXPR_BODY (list);
17506       if (TREE_CODE (list) == STATEMENT_LIST
17507           && STATEMENT_LIST_TAIL (list) != NULL)
17508         last = STATEMENT_LIST_TAIL (list)->stmt;
17509     }
17510   /* Parse the function-body.  */
17511   cp_parser_function_body (parser);
17512   if (check_body_p)
17513     check_constexpr_ctor_body (last, list);
17514   /* Finish the function body.  */
17515   finish_function_body (body);
17516
17517   return ctor_initializer_p;
17518 }
17519
17520 /* Parse an initializer.
17521
17522    initializer:
17523      = initializer-clause
17524      ( expression-list )
17525
17526    Returns an expression representing the initializer.  If no
17527    initializer is present, NULL_TREE is returned.
17528
17529    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17530    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17531    set to TRUE if there is no initializer present.  If there is an
17532    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17533    is set to true; otherwise it is set to false.  */
17534
17535 static tree
17536 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17537                        bool* non_constant_p)
17538 {
17539   cp_token *token;
17540   tree init;
17541
17542   /* Peek at the next token.  */
17543   token = cp_lexer_peek_token (parser->lexer);
17544
17545   /* Let our caller know whether or not this initializer was
17546      parenthesized.  */
17547   *is_direct_init = (token->type != CPP_EQ);
17548   /* Assume that the initializer is constant.  */
17549   *non_constant_p = false;
17550
17551   if (token->type == CPP_EQ)
17552     {
17553       /* Consume the `='.  */
17554       cp_lexer_consume_token (parser->lexer);
17555       /* Parse the initializer-clause.  */
17556       init = cp_parser_initializer_clause (parser, non_constant_p);
17557     }
17558   else if (token->type == CPP_OPEN_PAREN)
17559     {
17560       VEC(tree,gc) *vec;
17561       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17562                                                      /*cast_p=*/false,
17563                                                      /*allow_expansion_p=*/true,
17564                                                      non_constant_p);
17565       if (vec == NULL)
17566         return error_mark_node;
17567       init = build_tree_list_vec (vec);
17568       release_tree_vector (vec);
17569     }
17570   else if (token->type == CPP_OPEN_BRACE)
17571     {
17572       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17573       init = cp_parser_braced_list (parser, non_constant_p);
17574       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17575     }
17576   else
17577     {
17578       /* Anything else is an error.  */
17579       cp_parser_error (parser, "expected initializer");
17580       init = error_mark_node;
17581     }
17582
17583   return init;
17584 }
17585
17586 /* Parse an initializer-clause.
17587
17588    initializer-clause:
17589      assignment-expression
17590      braced-init-list
17591
17592    Returns an expression representing the initializer.
17593
17594    If the `assignment-expression' production is used the value
17595    returned is simply a representation for the expression.
17596
17597    Otherwise, calls cp_parser_braced_list.  */
17598
17599 static tree
17600 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17601 {
17602   tree initializer;
17603
17604   /* Assume the expression is constant.  */
17605   *non_constant_p = false;
17606
17607   /* If it is not a `{', then we are looking at an
17608      assignment-expression.  */
17609   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17610     {
17611       initializer
17612         = cp_parser_constant_expression (parser,
17613                                         /*allow_non_constant_p=*/true,
17614                                         non_constant_p);
17615     }
17616   else
17617     initializer = cp_parser_braced_list (parser, non_constant_p);
17618
17619   return initializer;
17620 }
17621
17622 /* Parse a brace-enclosed initializer list.
17623
17624    braced-init-list:
17625      { initializer-list , [opt] }
17626      { }
17627
17628    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17629    the elements of the initializer-list (or NULL, if the last
17630    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17631    NULL_TREE.  There is no way to detect whether or not the optional
17632    trailing `,' was provided.  NON_CONSTANT_P is as for
17633    cp_parser_initializer.  */     
17634
17635 static tree
17636 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17637 {
17638   tree initializer;
17639
17640   /* Consume the `{' token.  */
17641   cp_lexer_consume_token (parser->lexer);
17642   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17643   initializer = make_node (CONSTRUCTOR);
17644   /* If it's not a `}', then there is a non-trivial initializer.  */
17645   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17646     {
17647       /* Parse the initializer list.  */
17648       CONSTRUCTOR_ELTS (initializer)
17649         = cp_parser_initializer_list (parser, non_constant_p);
17650       /* A trailing `,' token is allowed.  */
17651       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17652         cp_lexer_consume_token (parser->lexer);
17653     }
17654   /* Now, there should be a trailing `}'.  */
17655   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17656   TREE_TYPE (initializer) = init_list_type_node;
17657   return initializer;
17658 }
17659
17660 /* Parse an initializer-list.
17661
17662    initializer-list:
17663      initializer-clause ... [opt]
17664      initializer-list , initializer-clause ... [opt]
17665
17666    GNU Extension:
17667
17668    initializer-list:
17669      designation initializer-clause ...[opt]
17670      initializer-list , designation initializer-clause ...[opt]
17671
17672    designation:
17673      . identifier =
17674      identifier :
17675      [ constant-expression ] =
17676
17677    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17678    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17679    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17680    as for cp_parser_initializer.  */
17681
17682 static VEC(constructor_elt,gc) *
17683 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17684 {
17685   VEC(constructor_elt,gc) *v = NULL;
17686
17687   /* Assume all of the expressions are constant.  */
17688   *non_constant_p = false;
17689
17690   /* Parse the rest of the list.  */
17691   while (true)
17692     {
17693       cp_token *token;
17694       tree designator;
17695       tree initializer;
17696       bool clause_non_constant_p;
17697
17698       /* If the next token is an identifier and the following one is a
17699          colon, we are looking at the GNU designated-initializer
17700          syntax.  */
17701       if (cp_parser_allow_gnu_extensions_p (parser)
17702           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17703           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17704         {
17705           /* Warn the user that they are using an extension.  */
17706           pedwarn (input_location, OPT_pedantic, 
17707                    "ISO C++ does not allow designated initializers");
17708           /* Consume the identifier.  */
17709           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17710           /* Consume the `:'.  */
17711           cp_lexer_consume_token (parser->lexer);
17712         }
17713       /* Also handle the C99 syntax, '. id ='.  */
17714       else if (cp_parser_allow_gnu_extensions_p (parser)
17715                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17716                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17717                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17718         {
17719           /* Warn the user that they are using an extension.  */
17720           pedwarn (input_location, OPT_pedantic,
17721                    "ISO C++ does not allow C99 designated initializers");
17722           /* Consume the `.'.  */
17723           cp_lexer_consume_token (parser->lexer);
17724           /* Consume the identifier.  */
17725           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17726           /* Consume the `='.  */
17727           cp_lexer_consume_token (parser->lexer);
17728         }
17729       /* Also handle C99 array designators, '[ const ] ='.  */
17730       else if (cp_parser_allow_gnu_extensions_p (parser)
17731                && !c_dialect_objc ()
17732                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17733         {
17734           /* In C++11, [ could start a lambda-introducer.  */
17735           cp_parser_parse_tentatively (parser);
17736           cp_lexer_consume_token (parser->lexer);
17737           designator = cp_parser_constant_expression (parser, false, NULL);
17738           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17739           cp_parser_require (parser, CPP_EQ, RT_EQ);
17740           if (!cp_parser_parse_definitely (parser))
17741             designator = NULL_TREE;
17742         }
17743       else
17744         designator = NULL_TREE;
17745
17746       /* Parse the initializer.  */
17747       initializer = cp_parser_initializer_clause (parser,
17748                                                   &clause_non_constant_p);
17749       /* If any clause is non-constant, so is the entire initializer.  */
17750       if (clause_non_constant_p)
17751         *non_constant_p = true;
17752
17753       /* If we have an ellipsis, this is an initializer pack
17754          expansion.  */
17755       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17756         {
17757           /* Consume the `...'.  */
17758           cp_lexer_consume_token (parser->lexer);
17759
17760           /* Turn the initializer into an initializer expansion.  */
17761           initializer = make_pack_expansion (initializer);
17762         }
17763
17764       /* Add it to the vector.  */
17765       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17766
17767       /* If the next token is not a comma, we have reached the end of
17768          the list.  */
17769       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17770         break;
17771
17772       /* Peek at the next token.  */
17773       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17774       /* If the next token is a `}', then we're still done.  An
17775          initializer-clause can have a trailing `,' after the
17776          initializer-list and before the closing `}'.  */
17777       if (token->type == CPP_CLOSE_BRACE)
17778         break;
17779
17780       /* Consume the `,' token.  */
17781       cp_lexer_consume_token (parser->lexer);
17782     }
17783
17784   return v;
17785 }
17786
17787 /* Classes [gram.class] */
17788
17789 /* Parse a class-name.
17790
17791    class-name:
17792      identifier
17793      template-id
17794
17795    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17796    to indicate that names looked up in dependent types should be
17797    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17798    keyword has been used to indicate that the name that appears next
17799    is a template.  TAG_TYPE indicates the explicit tag given before
17800    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17801    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17802    is the class being defined in a class-head.
17803
17804    Returns the TYPE_DECL representing the class.  */
17805
17806 static tree
17807 cp_parser_class_name (cp_parser *parser,
17808                       bool typename_keyword_p,
17809                       bool template_keyword_p,
17810                       enum tag_types tag_type,
17811                       bool check_dependency_p,
17812                       bool class_head_p,
17813                       bool is_declaration)
17814 {
17815   tree decl;
17816   tree scope;
17817   bool typename_p;
17818   cp_token *token;
17819   tree identifier = NULL_TREE;
17820
17821   /* All class-names start with an identifier.  */
17822   token = cp_lexer_peek_token (parser->lexer);
17823   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17824     {
17825       cp_parser_error (parser, "expected class-name");
17826       return error_mark_node;
17827     }
17828
17829   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17830      to a template-id, so we save it here.  */
17831   scope = parser->scope;
17832   if (scope == error_mark_node)
17833     return error_mark_node;
17834
17835   /* Any name names a type if we're following the `typename' keyword
17836      in a qualified name where the enclosing scope is type-dependent.  */
17837   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17838                 && dependent_type_p (scope));
17839   /* Handle the common case (an identifier, but not a template-id)
17840      efficiently.  */
17841   if (token->type == CPP_NAME
17842       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17843     {
17844       cp_token *identifier_token;
17845       bool ambiguous_p;
17846
17847       /* Look for the identifier.  */
17848       identifier_token = cp_lexer_peek_token (parser->lexer);
17849       ambiguous_p = identifier_token->ambiguous_p;
17850       identifier = cp_parser_identifier (parser);
17851       /* If the next token isn't an identifier, we are certainly not
17852          looking at a class-name.  */
17853       if (identifier == error_mark_node)
17854         decl = error_mark_node;
17855       /* If we know this is a type-name, there's no need to look it
17856          up.  */
17857       else if (typename_p)
17858         decl = identifier;
17859       else
17860         {
17861           tree ambiguous_decls;
17862           /* If we already know that this lookup is ambiguous, then
17863              we've already issued an error message; there's no reason
17864              to check again.  */
17865           if (ambiguous_p)
17866             {
17867               cp_parser_simulate_error (parser);
17868               return error_mark_node;
17869             }
17870           /* If the next token is a `::', then the name must be a type
17871              name.
17872
17873              [basic.lookup.qual]
17874
17875              During the lookup for a name preceding the :: scope
17876              resolution operator, object, function, and enumerator
17877              names are ignored.  */
17878           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17879             tag_type = typename_type;
17880           /* Look up the name.  */
17881           decl = cp_parser_lookup_name (parser, identifier,
17882                                         tag_type,
17883                                         /*is_template=*/false,
17884                                         /*is_namespace=*/false,
17885                                         check_dependency_p,
17886                                         &ambiguous_decls,
17887                                         identifier_token->location);
17888           if (ambiguous_decls)
17889             {
17890               if (cp_parser_parsing_tentatively (parser))
17891                 cp_parser_simulate_error (parser);
17892               return error_mark_node;
17893             }
17894         }
17895     }
17896   else
17897     {
17898       /* Try a template-id.  */
17899       decl = cp_parser_template_id (parser, template_keyword_p,
17900                                     check_dependency_p,
17901                                     is_declaration);
17902       if (decl == error_mark_node)
17903         return error_mark_node;
17904     }
17905
17906   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17907
17908   /* If this is a typename, create a TYPENAME_TYPE.  */
17909   if (typename_p && decl != error_mark_node)
17910     {
17911       decl = make_typename_type (scope, decl, typename_type,
17912                                  /*complain=*/tf_error);
17913       if (decl != error_mark_node)
17914         decl = TYPE_NAME (decl);
17915     }
17916
17917   /* Check to see that it is really the name of a class.  */
17918   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17919       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17920       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17921     /* Situations like this:
17922
17923          template <typename T> struct A {
17924            typename T::template X<int>::I i;
17925          };
17926
17927        are problematic.  Is `T::template X<int>' a class-name?  The
17928        standard does not seem to be definitive, but there is no other
17929        valid interpretation of the following `::'.  Therefore, those
17930        names are considered class-names.  */
17931     {
17932       decl = make_typename_type (scope, decl, tag_type, tf_error);
17933       if (decl != error_mark_node)
17934         decl = TYPE_NAME (decl);
17935     }
17936   else if (TREE_CODE (decl) != TYPE_DECL
17937            || TREE_TYPE (decl) == error_mark_node
17938            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17939            /* In Objective-C 2.0, a classname followed by '.' starts a
17940               dot-syntax expression, and it's not a type-name.  */
17941            || (c_dialect_objc ()
17942                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17943                && objc_is_class_name (decl)))
17944     decl = error_mark_node;
17945
17946   if (decl == error_mark_node)
17947     cp_parser_error (parser, "expected class-name");
17948   else if (identifier && !parser->scope)
17949     maybe_note_name_used_in_class (identifier, decl);
17950
17951   return decl;
17952 }
17953
17954 /* Parse a class-specifier.
17955
17956    class-specifier:
17957      class-head { member-specification [opt] }
17958
17959    Returns the TREE_TYPE representing the class.  */
17960
17961 static tree
17962 cp_parser_class_specifier_1 (cp_parser* parser)
17963 {
17964   tree type;
17965   tree attributes = NULL_TREE;
17966   bool nested_name_specifier_p;
17967   unsigned saved_num_template_parameter_lists;
17968   bool saved_in_function_body;
17969   unsigned char in_statement;
17970   bool in_switch_statement_p;
17971   bool saved_in_unbraced_linkage_specification_p;
17972   tree old_scope = NULL_TREE;
17973   tree scope = NULL_TREE;
17974   tree bases;
17975   cp_token *closing_brace;
17976
17977   push_deferring_access_checks (dk_no_deferred);
17978
17979   /* Parse the class-head.  */
17980   type = cp_parser_class_head (parser,
17981                                &nested_name_specifier_p,
17982                                &attributes,
17983                                &bases);
17984   /* If the class-head was a semantic disaster, skip the entire body
17985      of the class.  */
17986   if (!type)
17987     {
17988       cp_parser_skip_to_end_of_block_or_statement (parser);
17989       pop_deferring_access_checks ();
17990       return error_mark_node;
17991     }
17992
17993   /* Look for the `{'.  */
17994   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17995     {
17996       pop_deferring_access_checks ();
17997       return error_mark_node;
17998     }
17999
18000   /* Process the base classes. If they're invalid, skip the 
18001      entire class body.  */
18002   if (!xref_basetypes (type, bases))
18003     {
18004       /* Consuming the closing brace yields better error messages
18005          later on.  */
18006       if (cp_parser_skip_to_closing_brace (parser))
18007         cp_lexer_consume_token (parser->lexer);
18008       pop_deferring_access_checks ();
18009       return error_mark_node;
18010     }
18011
18012   /* Issue an error message if type-definitions are forbidden here.  */
18013   cp_parser_check_type_definition (parser);
18014   /* Remember that we are defining one more class.  */
18015   ++parser->num_classes_being_defined;
18016   /* Inside the class, surrounding template-parameter-lists do not
18017      apply.  */
18018   saved_num_template_parameter_lists
18019     = parser->num_template_parameter_lists;
18020   parser->num_template_parameter_lists = 0;
18021   /* We are not in a function body.  */
18022   saved_in_function_body = parser->in_function_body;
18023   parser->in_function_body = false;
18024   /* Or in a loop.  */
18025   in_statement = parser->in_statement;
18026   parser->in_statement = 0;
18027   /* Or in a switch.  */
18028   in_switch_statement_p = parser->in_switch_statement_p;
18029   parser->in_switch_statement_p = false;
18030   /* We are not immediately inside an extern "lang" block.  */
18031   saved_in_unbraced_linkage_specification_p
18032     = parser->in_unbraced_linkage_specification_p;
18033   parser->in_unbraced_linkage_specification_p = false;
18034
18035   /* Start the class.  */
18036   if (nested_name_specifier_p)
18037     {
18038       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18039       old_scope = push_inner_scope (scope);
18040     }
18041   type = begin_class_definition (type, attributes);
18042
18043   if (type == error_mark_node)
18044     /* If the type is erroneous, skip the entire body of the class.  */
18045     cp_parser_skip_to_closing_brace (parser);
18046   else
18047     /* Parse the member-specification.  */
18048     cp_parser_member_specification_opt (parser);
18049
18050   /* Look for the trailing `}'.  */
18051   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18052   /* Look for trailing attributes to apply to this class.  */
18053   if (cp_parser_allow_gnu_extensions_p (parser))
18054     attributes = cp_parser_attributes_opt (parser);
18055   if (type != error_mark_node)
18056     type = finish_struct (type, attributes);
18057   if (nested_name_specifier_p)
18058     pop_inner_scope (old_scope, scope);
18059
18060   /* We've finished a type definition.  Check for the common syntax
18061      error of forgetting a semicolon after the definition.  We need to
18062      be careful, as we can't just check for not-a-semicolon and be done
18063      with it; the user might have typed:
18064
18065      class X { } c = ...;
18066      class X { } *p = ...;
18067
18068      and so forth.  Instead, enumerate all the possible tokens that
18069      might follow this production; if we don't see one of them, then
18070      complain and silently insert the semicolon.  */
18071   {
18072     cp_token *token = cp_lexer_peek_token (parser->lexer);
18073     bool want_semicolon = true;
18074
18075     switch (token->type)
18076       {
18077       case CPP_NAME:
18078       case CPP_SEMICOLON:
18079       case CPP_MULT:
18080       case CPP_AND:
18081       case CPP_OPEN_PAREN:
18082       case CPP_CLOSE_PAREN:
18083       case CPP_COMMA:
18084         want_semicolon = false;
18085         break;
18086
18087         /* While it's legal for type qualifiers and storage class
18088            specifiers to follow type definitions in the grammar, only
18089            compiler testsuites contain code like that.  Assume that if
18090            we see such code, then what we're really seeing is a case
18091            like:
18092
18093            class X { }
18094            const <type> var = ...;
18095
18096            or
18097
18098            class Y { }
18099            static <type> func (...) ...
18100
18101            i.e. the qualifier or specifier applies to the next
18102            declaration.  To do so, however, we need to look ahead one
18103            more token to see if *that* token is a type specifier.
18104
18105            This code could be improved to handle:
18106
18107            class Z { }
18108            static const <type> var = ...;  */
18109       case CPP_KEYWORD:
18110         if (keyword_is_decl_specifier (token->keyword))
18111           {
18112             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18113
18114             /* Handling user-defined types here would be nice, but very
18115                tricky.  */
18116             want_semicolon
18117               = (lookahead->type == CPP_KEYWORD
18118                  && keyword_begins_type_specifier (lookahead->keyword));
18119           }
18120         break;
18121       default:
18122         break;
18123       }
18124
18125     /* If we don't have a type, then something is very wrong and we
18126        shouldn't try to do anything clever.  Likewise for not seeing the
18127        closing brace.  */
18128     if (closing_brace && TYPE_P (type) && want_semicolon)
18129       {
18130         cp_token_position prev
18131           = cp_lexer_previous_token_position (parser->lexer);
18132         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18133         location_t loc = prev_token->location;
18134
18135         if (CLASSTYPE_DECLARED_CLASS (type))
18136           error_at (loc, "expected %<;%> after class definition");
18137         else if (TREE_CODE (type) == RECORD_TYPE)
18138           error_at (loc, "expected %<;%> after struct definition");
18139         else if (TREE_CODE (type) == UNION_TYPE)
18140           error_at (loc, "expected %<;%> after union definition");
18141         else
18142           gcc_unreachable ();
18143
18144         /* Unget one token and smash it to look as though we encountered
18145            a semicolon in the input stream.  */
18146         cp_lexer_set_token_position (parser->lexer, prev);
18147         token = cp_lexer_peek_token (parser->lexer);
18148         token->type = CPP_SEMICOLON;
18149         token->keyword = RID_MAX;
18150       }
18151   }
18152
18153   /* If this class is not itself within the scope of another class,
18154      then we need to parse the bodies of all of the queued function
18155      definitions.  Note that the queued functions defined in a class
18156      are not always processed immediately following the
18157      class-specifier for that class.  Consider:
18158
18159        struct A {
18160          struct B { void f() { sizeof (A); } };
18161        };
18162
18163      If `f' were processed before the processing of `A' were
18164      completed, there would be no way to compute the size of `A'.
18165      Note that the nesting we are interested in here is lexical --
18166      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18167      for:
18168
18169        struct A { struct B; };
18170        struct A::B { void f() { } };
18171
18172      there is no need to delay the parsing of `A::B::f'.  */
18173   if (--parser->num_classes_being_defined == 0)
18174     {
18175       tree decl;
18176       tree class_type = NULL_TREE;
18177       tree pushed_scope = NULL_TREE;
18178       unsigned ix;
18179       cp_default_arg_entry *e;
18180       tree save_ccp, save_ccr;
18181
18182       /* In a first pass, parse default arguments to the functions.
18183          Then, in a second pass, parse the bodies of the functions.
18184          This two-phased approach handles cases like:
18185
18186             struct S {
18187               void f() { g(); }
18188               void g(int i = 3);
18189             };
18190
18191          */
18192       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18193                         ix, e)
18194         {
18195           decl = e->decl;
18196           /* If there are default arguments that have not yet been processed,
18197              take care of them now.  */
18198           if (class_type != e->class_type)
18199             {
18200               if (pushed_scope)
18201                 pop_scope (pushed_scope);
18202               class_type = e->class_type;
18203               pushed_scope = push_scope (class_type);
18204             }
18205           /* Make sure that any template parameters are in scope.  */
18206           maybe_begin_member_template_processing (decl);
18207           /* Parse the default argument expressions.  */
18208           cp_parser_late_parsing_default_args (parser, decl);
18209           /* Remove any template parameters from the symbol table.  */
18210           maybe_end_member_template_processing ();
18211         }
18212       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18213       /* Now parse any NSDMIs.  */
18214       save_ccp = current_class_ptr;
18215       save_ccr = current_class_ref;
18216       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18217         {
18218           if (class_type != DECL_CONTEXT (decl))
18219             {
18220               if (pushed_scope)
18221                 pop_scope (pushed_scope);
18222               class_type = DECL_CONTEXT (decl);
18223               pushed_scope = push_scope (class_type);
18224             }
18225           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18226           cp_parser_late_parsing_nsdmi (parser, decl);
18227         }
18228       VEC_truncate (tree, unparsed_nsdmis, 0);
18229       current_class_ptr = save_ccp;
18230       current_class_ref = save_ccr;
18231       if (pushed_scope)
18232         pop_scope (pushed_scope);
18233       /* Now parse the body of the functions.  */
18234       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18235         cp_parser_late_parsing_for_member (parser, decl);
18236       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18237     }
18238
18239   /* Put back any saved access checks.  */
18240   pop_deferring_access_checks ();
18241
18242   /* Restore saved state.  */
18243   parser->in_switch_statement_p = in_switch_statement_p;
18244   parser->in_statement = in_statement;
18245   parser->in_function_body = saved_in_function_body;
18246   parser->num_template_parameter_lists
18247     = saved_num_template_parameter_lists;
18248   parser->in_unbraced_linkage_specification_p
18249     = saved_in_unbraced_linkage_specification_p;
18250
18251   return type;
18252 }
18253
18254 static tree
18255 cp_parser_class_specifier (cp_parser* parser)
18256 {
18257   tree ret;
18258   timevar_push (TV_PARSE_STRUCT);
18259   ret = cp_parser_class_specifier_1 (parser);
18260   timevar_pop (TV_PARSE_STRUCT);
18261   return ret;
18262 }
18263
18264 /* Parse a class-head.
18265
18266    class-head:
18267      class-key identifier [opt] base-clause [opt]
18268      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18269      class-key nested-name-specifier [opt] template-id
18270        base-clause [opt]
18271
18272    class-virt-specifier:
18273      final
18274
18275    GNU Extensions:
18276      class-key attributes identifier [opt] base-clause [opt]
18277      class-key attributes nested-name-specifier identifier base-clause [opt]
18278      class-key attributes nested-name-specifier [opt] template-id
18279        base-clause [opt]
18280
18281    Upon return BASES is initialized to the list of base classes (or
18282    NULL, if there are none) in the same form returned by
18283    cp_parser_base_clause.
18284
18285    Returns the TYPE of the indicated class.  Sets
18286    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18287    involving a nested-name-specifier was used, and FALSE otherwise.
18288
18289    Returns error_mark_node if this is not a class-head.
18290
18291    Returns NULL_TREE if the class-head is syntactically valid, but
18292    semantically invalid in a way that means we should skip the entire
18293    body of the class.  */
18294
18295 static tree
18296 cp_parser_class_head (cp_parser* parser,
18297                       bool* nested_name_specifier_p,
18298                       tree *attributes_p,
18299                       tree *bases)
18300 {
18301   tree nested_name_specifier;
18302   enum tag_types class_key;
18303   tree id = NULL_TREE;
18304   tree type = NULL_TREE;
18305   tree attributes;
18306   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18307   bool template_id_p = false;
18308   bool qualified_p = false;
18309   bool invalid_nested_name_p = false;
18310   bool invalid_explicit_specialization_p = false;
18311   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18312   tree pushed_scope = NULL_TREE;
18313   unsigned num_templates;
18314   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18315   /* Assume no nested-name-specifier will be present.  */
18316   *nested_name_specifier_p = false;
18317   /* Assume no template parameter lists will be used in defining the
18318      type.  */
18319   num_templates = 0;
18320   parser->colon_corrects_to_scope_p = false;
18321
18322   *bases = NULL_TREE;
18323
18324   /* Look for the class-key.  */
18325   class_key = cp_parser_class_key (parser);
18326   if (class_key == none_type)
18327     return error_mark_node;
18328
18329   /* Parse the attributes.  */
18330   attributes = cp_parser_attributes_opt (parser);
18331
18332   /* If the next token is `::', that is invalid -- but sometimes
18333      people do try to write:
18334
18335        struct ::S {};
18336
18337      Handle this gracefully by accepting the extra qualifier, and then
18338      issuing an error about it later if this really is a
18339      class-head.  If it turns out just to be an elaborated type
18340      specifier, remain silent.  */
18341   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18342     qualified_p = true;
18343
18344   push_deferring_access_checks (dk_no_check);
18345
18346   /* Determine the name of the class.  Begin by looking for an
18347      optional nested-name-specifier.  */
18348   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18349   nested_name_specifier
18350     = cp_parser_nested_name_specifier_opt (parser,
18351                                            /*typename_keyword_p=*/false,
18352                                            /*check_dependency_p=*/false,
18353                                            /*type_p=*/false,
18354                                            /*is_declaration=*/false);
18355   /* If there was a nested-name-specifier, then there *must* be an
18356      identifier.  */
18357   if (nested_name_specifier)
18358     {
18359       type_start_token = cp_lexer_peek_token (parser->lexer);
18360       /* Although the grammar says `identifier', it really means
18361          `class-name' or `template-name'.  You are only allowed to
18362          define a class that has already been declared with this
18363          syntax.
18364
18365          The proposed resolution for Core Issue 180 says that wherever
18366          you see `class T::X' you should treat `X' as a type-name.
18367
18368          It is OK to define an inaccessible class; for example:
18369
18370            class A { class B; };
18371            class A::B {};
18372
18373          We do not know if we will see a class-name, or a
18374          template-name.  We look for a class-name first, in case the
18375          class-name is a template-id; if we looked for the
18376          template-name first we would stop after the template-name.  */
18377       cp_parser_parse_tentatively (parser);
18378       type = cp_parser_class_name (parser,
18379                                    /*typename_keyword_p=*/false,
18380                                    /*template_keyword_p=*/false,
18381                                    class_type,
18382                                    /*check_dependency_p=*/false,
18383                                    /*class_head_p=*/true,
18384                                    /*is_declaration=*/false);
18385       /* If that didn't work, ignore the nested-name-specifier.  */
18386       if (!cp_parser_parse_definitely (parser))
18387         {
18388           invalid_nested_name_p = true;
18389           type_start_token = cp_lexer_peek_token (parser->lexer);
18390           id = cp_parser_identifier (parser);
18391           if (id == error_mark_node)
18392             id = NULL_TREE;
18393         }
18394       /* If we could not find a corresponding TYPE, treat this
18395          declaration like an unqualified declaration.  */
18396       if (type == error_mark_node)
18397         nested_name_specifier = NULL_TREE;
18398       /* Otherwise, count the number of templates used in TYPE and its
18399          containing scopes.  */
18400       else
18401         {
18402           tree scope;
18403
18404           for (scope = TREE_TYPE (type);
18405                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18406                scope = (TYPE_P (scope)
18407                         ? TYPE_CONTEXT (scope)
18408                         : DECL_CONTEXT (scope)))
18409             if (TYPE_P (scope)
18410                 && CLASS_TYPE_P (scope)
18411                 && CLASSTYPE_TEMPLATE_INFO (scope)
18412                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18413                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18414               ++num_templates;
18415         }
18416     }
18417   /* Otherwise, the identifier is optional.  */
18418   else
18419     {
18420       /* We don't know whether what comes next is a template-id,
18421          an identifier, or nothing at all.  */
18422       cp_parser_parse_tentatively (parser);
18423       /* Check for a template-id.  */
18424       type_start_token = cp_lexer_peek_token (parser->lexer);
18425       id = cp_parser_template_id (parser,
18426                                   /*template_keyword_p=*/false,
18427                                   /*check_dependency_p=*/true,
18428                                   /*is_declaration=*/true);
18429       /* If that didn't work, it could still be an identifier.  */
18430       if (!cp_parser_parse_definitely (parser))
18431         {
18432           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18433             {
18434               type_start_token = cp_lexer_peek_token (parser->lexer);
18435               id = cp_parser_identifier (parser);
18436             }
18437           else
18438             id = NULL_TREE;
18439         }
18440       else
18441         {
18442           template_id_p = true;
18443           ++num_templates;
18444         }
18445     }
18446
18447   pop_deferring_access_checks ();
18448
18449   if (id)
18450     {
18451       cp_parser_check_for_invalid_template_id (parser, id,
18452                                                type_start_token->location);
18453     }
18454   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18455
18456   /* If it's not a `:' or a `{' then we can't really be looking at a
18457      class-head, since a class-head only appears as part of a
18458      class-specifier.  We have to detect this situation before calling
18459      xref_tag, since that has irreversible side-effects.  */
18460   if (!cp_parser_next_token_starts_class_definition_p (parser))
18461     {
18462       cp_parser_error (parser, "expected %<{%> or %<:%>");
18463       type = error_mark_node;
18464       goto out;
18465     }
18466
18467   /* At this point, we're going ahead with the class-specifier, even
18468      if some other problem occurs.  */
18469   cp_parser_commit_to_tentative_parse (parser);
18470   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18471     {
18472       cp_parser_error (parser,
18473                        "cannot specify %<override%> for a class");
18474       type = error_mark_node;
18475       goto out;
18476     }
18477   /* Issue the error about the overly-qualified name now.  */
18478   if (qualified_p)
18479     {
18480       cp_parser_error (parser,
18481                        "global qualification of class name is invalid");
18482       type = error_mark_node;
18483       goto out;
18484     }
18485   else if (invalid_nested_name_p)
18486     {
18487       cp_parser_error (parser,
18488                        "qualified name does not name a class");
18489       type = error_mark_node;
18490       goto out;
18491     }
18492   else if (nested_name_specifier)
18493     {
18494       tree scope;
18495
18496       /* Reject typedef-names in class heads.  */
18497       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18498         {
18499           error_at (type_start_token->location,
18500                     "invalid class name in declaration of %qD",
18501                     type);
18502           type = NULL_TREE;
18503           goto done;
18504         }
18505
18506       /* Figure out in what scope the declaration is being placed.  */
18507       scope = current_scope ();
18508       /* If that scope does not contain the scope in which the
18509          class was originally declared, the program is invalid.  */
18510       if (scope && !is_ancestor (scope, nested_name_specifier))
18511         {
18512           if (at_namespace_scope_p ())
18513             error_at (type_start_token->location,
18514                       "declaration of %qD in namespace %qD which does not "
18515                       "enclose %qD",
18516                       type, scope, nested_name_specifier);
18517           else
18518             error_at (type_start_token->location,
18519                       "declaration of %qD in %qD which does not enclose %qD",
18520                       type, scope, nested_name_specifier);
18521           type = NULL_TREE;
18522           goto done;
18523         }
18524       /* [dcl.meaning]
18525
18526          A declarator-id shall not be qualified except for the
18527          definition of a ... nested class outside of its class
18528          ... [or] the definition or explicit instantiation of a
18529          class member of a namespace outside of its namespace.  */
18530       if (scope == nested_name_specifier)
18531         {
18532           permerror (nested_name_specifier_token_start->location,
18533                      "extra qualification not allowed");
18534           nested_name_specifier = NULL_TREE;
18535           num_templates = 0;
18536         }
18537     }
18538   /* An explicit-specialization must be preceded by "template <>".  If
18539      it is not, try to recover gracefully.  */
18540   if (at_namespace_scope_p ()
18541       && parser->num_template_parameter_lists == 0
18542       && template_id_p)
18543     {
18544       error_at (type_start_token->location,
18545                 "an explicit specialization must be preceded by %<template <>%>");
18546       invalid_explicit_specialization_p = true;
18547       /* Take the same action that would have been taken by
18548          cp_parser_explicit_specialization.  */
18549       ++parser->num_template_parameter_lists;
18550       begin_specialization ();
18551     }
18552   /* There must be no "return" statements between this point and the
18553      end of this function; set "type "to the correct return value and
18554      use "goto done;" to return.  */
18555   /* Make sure that the right number of template parameters were
18556      present.  */
18557   if (!cp_parser_check_template_parameters (parser, num_templates,
18558                                             type_start_token->location,
18559                                             /*declarator=*/NULL))
18560     {
18561       /* If something went wrong, there is no point in even trying to
18562          process the class-definition.  */
18563       type = NULL_TREE;
18564       goto done;
18565     }
18566
18567   /* Look up the type.  */
18568   if (template_id_p)
18569     {
18570       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18571           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18572               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18573         {
18574           error_at (type_start_token->location,
18575                     "function template %qD redeclared as a class template", id);
18576           type = error_mark_node;
18577         }
18578       else
18579         {
18580           type = TREE_TYPE (id);
18581           type = maybe_process_partial_specialization (type);
18582         }
18583       if (nested_name_specifier)
18584         pushed_scope = push_scope (nested_name_specifier);
18585     }
18586   else if (nested_name_specifier)
18587     {
18588       tree class_type;
18589
18590       /* Given:
18591
18592             template <typename T> struct S { struct T };
18593             template <typename T> struct S<T>::T { };
18594
18595          we will get a TYPENAME_TYPE when processing the definition of
18596          `S::T'.  We need to resolve it to the actual type before we
18597          try to define it.  */
18598       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18599         {
18600           class_type = resolve_typename_type (TREE_TYPE (type),
18601                                               /*only_current_p=*/false);
18602           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18603             type = TYPE_NAME (class_type);
18604           else
18605             {
18606               cp_parser_error (parser, "could not resolve typename type");
18607               type = error_mark_node;
18608             }
18609         }
18610
18611       if (maybe_process_partial_specialization (TREE_TYPE (type))
18612           == error_mark_node)
18613         {
18614           type = NULL_TREE;
18615           goto done;
18616         }
18617
18618       class_type = current_class_type;
18619       /* Enter the scope indicated by the nested-name-specifier.  */
18620       pushed_scope = push_scope (nested_name_specifier);
18621       /* Get the canonical version of this type.  */
18622       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18623       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18624           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18625         {
18626           type = push_template_decl (type);
18627           if (type == error_mark_node)
18628             {
18629               type = NULL_TREE;
18630               goto done;
18631             }
18632         }
18633
18634       type = TREE_TYPE (type);
18635       *nested_name_specifier_p = true;
18636     }
18637   else      /* The name is not a nested name.  */
18638     {
18639       /* If the class was unnamed, create a dummy name.  */
18640       if (!id)
18641         id = make_anon_name ();
18642       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18643                        parser->num_template_parameter_lists);
18644     }
18645
18646   /* Indicate whether this class was declared as a `class' or as a
18647      `struct'.  */
18648   if (TREE_CODE (type) == RECORD_TYPE)
18649     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18650   cp_parser_check_class_key (class_key, type);
18651
18652   /* If this type was already complete, and we see another definition,
18653      that's an error.  */
18654   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18655     {
18656       error_at (type_start_token->location, "redefinition of %q#T",
18657                 type);
18658       error_at (type_start_token->location, "previous definition of %q+#T",
18659                 type);
18660       type = NULL_TREE;
18661       goto done;
18662     }
18663   else if (type == error_mark_node)
18664     type = NULL_TREE;
18665
18666   /* We will have entered the scope containing the class; the names of
18667      base classes should be looked up in that context.  For example:
18668
18669        struct A { struct B {}; struct C; };
18670        struct A::C : B {};
18671
18672      is valid.  */
18673
18674   /* Get the list of base-classes, if there is one.  */
18675   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18676     *bases = cp_parser_base_clause (parser);
18677
18678  done:
18679   /* Leave the scope given by the nested-name-specifier.  We will
18680      enter the class scope itself while processing the members.  */
18681   if (pushed_scope)
18682     pop_scope (pushed_scope);
18683
18684   if (invalid_explicit_specialization_p)
18685     {
18686       end_specialization ();
18687       --parser->num_template_parameter_lists;
18688     }
18689
18690   if (type)
18691     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18692   *attributes_p = attributes;
18693   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18694     CLASSTYPE_FINAL (type) = 1;
18695  out:
18696   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18697   return type;
18698 }
18699
18700 /* Parse a class-key.
18701
18702    class-key:
18703      class
18704      struct
18705      union
18706
18707    Returns the kind of class-key specified, or none_type to indicate
18708    error.  */
18709
18710 static enum tag_types
18711 cp_parser_class_key (cp_parser* parser)
18712 {
18713   cp_token *token;
18714   enum tag_types tag_type;
18715
18716   /* Look for the class-key.  */
18717   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18718   if (!token)
18719     return none_type;
18720
18721   /* Check to see if the TOKEN is a class-key.  */
18722   tag_type = cp_parser_token_is_class_key (token);
18723   if (!tag_type)
18724     cp_parser_error (parser, "expected class-key");
18725   return tag_type;
18726 }
18727
18728 /* Parse an (optional) member-specification.
18729
18730    member-specification:
18731      member-declaration member-specification [opt]
18732      access-specifier : member-specification [opt]  */
18733
18734 static void
18735 cp_parser_member_specification_opt (cp_parser* parser)
18736 {
18737   while (true)
18738     {
18739       cp_token *token;
18740       enum rid keyword;
18741
18742       /* Peek at the next token.  */
18743       token = cp_lexer_peek_token (parser->lexer);
18744       /* If it's a `}', or EOF then we've seen all the members.  */
18745       if (token->type == CPP_CLOSE_BRACE
18746           || token->type == CPP_EOF
18747           || token->type == CPP_PRAGMA_EOL)
18748         break;
18749
18750       /* See if this token is a keyword.  */
18751       keyword = token->keyword;
18752       switch (keyword)
18753         {
18754         case RID_PUBLIC:
18755         case RID_PROTECTED:
18756         case RID_PRIVATE:
18757           /* Consume the access-specifier.  */
18758           cp_lexer_consume_token (parser->lexer);
18759           /* Remember which access-specifier is active.  */
18760           current_access_specifier = token->u.value;
18761           /* Look for the `:'.  */
18762           cp_parser_require (parser, CPP_COLON, RT_COLON);
18763           break;
18764
18765         default:
18766           /* Accept #pragmas at class scope.  */
18767           if (token->type == CPP_PRAGMA)
18768             {
18769               cp_parser_pragma (parser, pragma_external);
18770               break;
18771             }
18772
18773           /* Otherwise, the next construction must be a
18774              member-declaration.  */
18775           cp_parser_member_declaration (parser);
18776         }
18777     }
18778 }
18779
18780 /* Parse a member-declaration.
18781
18782    member-declaration:
18783      decl-specifier-seq [opt] member-declarator-list [opt] ;
18784      function-definition ; [opt]
18785      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18786      using-declaration
18787      template-declaration
18788      alias-declaration
18789
18790    member-declarator-list:
18791      member-declarator
18792      member-declarator-list , member-declarator
18793
18794    member-declarator:
18795      declarator pure-specifier [opt]
18796      declarator constant-initializer [opt]
18797      identifier [opt] : constant-expression
18798
18799    GNU Extensions:
18800
18801    member-declaration:
18802      __extension__ member-declaration
18803
18804    member-declarator:
18805      declarator attributes [opt] pure-specifier [opt]
18806      declarator attributes [opt] constant-initializer [opt]
18807      identifier [opt] attributes [opt] : constant-expression  
18808
18809    C++0x Extensions:
18810
18811    member-declaration:
18812      static_assert-declaration  */
18813
18814 static void
18815 cp_parser_member_declaration (cp_parser* parser)
18816 {
18817   cp_decl_specifier_seq decl_specifiers;
18818   tree prefix_attributes;
18819   tree decl;
18820   int declares_class_or_enum;
18821   bool friend_p;
18822   cp_token *token = NULL;
18823   cp_token *decl_spec_token_start = NULL;
18824   cp_token *initializer_token_start = NULL;
18825   int saved_pedantic;
18826   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18827
18828   /* Check for the `__extension__' keyword.  */
18829   if (cp_parser_extension_opt (parser, &saved_pedantic))
18830     {
18831       /* Recurse.  */
18832       cp_parser_member_declaration (parser);
18833       /* Restore the old value of the PEDANTIC flag.  */
18834       pedantic = saved_pedantic;
18835
18836       return;
18837     }
18838
18839   /* Check for a template-declaration.  */
18840   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18841     {
18842       /* An explicit specialization here is an error condition, and we
18843          expect the specialization handler to detect and report this.  */
18844       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18845           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18846         cp_parser_explicit_specialization (parser);
18847       else
18848         cp_parser_template_declaration (parser, /*member_p=*/true);
18849
18850       return;
18851     }
18852
18853   /* Check for a using-declaration.  */
18854   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18855     {
18856       if (cxx_dialect < cxx0x)
18857         {
18858           /* Parse the using-declaration.  */
18859           cp_parser_using_declaration (parser,
18860                                        /*access_declaration_p=*/false);
18861           return;
18862         }
18863       else
18864         {
18865           tree decl;
18866           cp_parser_parse_tentatively (parser);
18867           decl = cp_parser_alias_declaration (parser);
18868           if (cp_parser_parse_definitely (parser))
18869             finish_member_declaration (decl);
18870           else
18871             cp_parser_using_declaration (parser,
18872                                          /*access_declaration_p=*/false);
18873           return;
18874         }
18875     }
18876
18877   /* Check for @defs.  */
18878   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18879     {
18880       tree ivar, member;
18881       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18882       ivar = ivar_chains;
18883       while (ivar)
18884         {
18885           member = ivar;
18886           ivar = TREE_CHAIN (member);
18887           TREE_CHAIN (member) = NULL_TREE;
18888           finish_member_declaration (member);
18889         }
18890       return;
18891     }
18892
18893   /* If the next token is `static_assert' we have a static assertion.  */
18894   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18895     {
18896       cp_parser_static_assert (parser, /*member_p=*/true);
18897       return;
18898     }
18899
18900   parser->colon_corrects_to_scope_p = false;
18901
18902   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18903     goto out;
18904
18905   /* Parse the decl-specifier-seq.  */
18906   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18907   cp_parser_decl_specifier_seq (parser,
18908                                 CP_PARSER_FLAGS_OPTIONAL,
18909                                 &decl_specifiers,
18910                                 &declares_class_or_enum);
18911   prefix_attributes = decl_specifiers.attributes;
18912   decl_specifiers.attributes = NULL_TREE;
18913   /* Check for an invalid type-name.  */
18914   if (!decl_specifiers.any_type_specifiers_p
18915       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18916     goto out;
18917   /* If there is no declarator, then the decl-specifier-seq should
18918      specify a type.  */
18919   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18920     {
18921       /* If there was no decl-specifier-seq, and the next token is a
18922          `;', then we have something like:
18923
18924            struct S { ; };
18925
18926          [class.mem]
18927
18928          Each member-declaration shall declare at least one member
18929          name of the class.  */
18930       if (!decl_specifiers.any_specifiers_p)
18931         {
18932           cp_token *token = cp_lexer_peek_token (parser->lexer);
18933           if (!in_system_header_at (token->location))
18934             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18935         }
18936       else
18937         {
18938           tree type;
18939
18940           /* See if this declaration is a friend.  */
18941           friend_p = cp_parser_friend_p (&decl_specifiers);
18942           /* If there were decl-specifiers, check to see if there was
18943              a class-declaration.  */
18944           type = check_tag_decl (&decl_specifiers);
18945           /* Nested classes have already been added to the class, but
18946              a `friend' needs to be explicitly registered.  */
18947           if (friend_p)
18948             {
18949               /* If the `friend' keyword was present, the friend must
18950                  be introduced with a class-key.  */
18951                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18952                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18953                           "in C++03 a class-key must be used "
18954                           "when declaring a friend");
18955                /* In this case:
18956
18957                     template <typename T> struct A {
18958                       friend struct A<T>::B;
18959                     };
18960
18961                   A<T>::B will be represented by a TYPENAME_TYPE, and
18962                   therefore not recognized by check_tag_decl.  */
18963                if (!type)
18964                  {
18965                    type = decl_specifiers.type;
18966                    if (type && TREE_CODE (type) == TYPE_DECL)
18967                      type = TREE_TYPE (type);
18968                  }
18969                if (!type || !TYPE_P (type))
18970                  error_at (decl_spec_token_start->location,
18971                            "friend declaration does not name a class or "
18972                            "function");
18973                else
18974                  make_friend_class (current_class_type, type,
18975                                     /*complain=*/true);
18976             }
18977           /* If there is no TYPE, an error message will already have
18978              been issued.  */
18979           else if (!type || type == error_mark_node)
18980             ;
18981           /* An anonymous aggregate has to be handled specially; such
18982              a declaration really declares a data member (with a
18983              particular type), as opposed to a nested class.  */
18984           else if (ANON_AGGR_TYPE_P (type))
18985             {
18986               /* Remove constructors and such from TYPE, now that we
18987                  know it is an anonymous aggregate.  */
18988               fixup_anonymous_aggr (type);
18989               /* And make the corresponding data member.  */
18990               decl = build_decl (decl_spec_token_start->location,
18991                                  FIELD_DECL, NULL_TREE, type);
18992               /* Add it to the class.  */
18993               finish_member_declaration (decl);
18994             }
18995           else
18996             cp_parser_check_access_in_redeclaration
18997                                               (TYPE_NAME (type),
18998                                                decl_spec_token_start->location);
18999         }
19000     }
19001   else
19002     {
19003       bool assume_semicolon = false;
19004
19005       /* See if these declarations will be friends.  */
19006       friend_p = cp_parser_friend_p (&decl_specifiers);
19007
19008       /* Keep going until we hit the `;' at the end of the
19009          declaration.  */
19010       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19011         {
19012           tree attributes = NULL_TREE;
19013           tree first_attribute;
19014
19015           /* Peek at the next token.  */
19016           token = cp_lexer_peek_token (parser->lexer);
19017
19018           /* Check for a bitfield declaration.  */
19019           if (token->type == CPP_COLON
19020               || (token->type == CPP_NAME
19021                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19022                   == CPP_COLON))
19023             {
19024               tree identifier;
19025               tree width;
19026
19027               /* Get the name of the bitfield.  Note that we cannot just
19028                  check TOKEN here because it may have been invalidated by
19029                  the call to cp_lexer_peek_nth_token above.  */
19030               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19031                 identifier = cp_parser_identifier (parser);
19032               else
19033                 identifier = NULL_TREE;
19034
19035               /* Consume the `:' token.  */
19036               cp_lexer_consume_token (parser->lexer);
19037               /* Get the width of the bitfield.  */
19038               width
19039                 = cp_parser_constant_expression (parser,
19040                                                  /*allow_non_constant=*/false,
19041                                                  NULL);
19042
19043               /* Look for attributes that apply to the bitfield.  */
19044               attributes = cp_parser_attributes_opt (parser);
19045               /* Remember which attributes are prefix attributes and
19046                  which are not.  */
19047               first_attribute = attributes;
19048               /* Combine the attributes.  */
19049               attributes = chainon (prefix_attributes, attributes);
19050
19051               /* Create the bitfield declaration.  */
19052               decl = grokbitfield (identifier
19053                                    ? make_id_declarator (NULL_TREE,
19054                                                          identifier,
19055                                                          sfk_none)
19056                                    : NULL,
19057                                    &decl_specifiers,
19058                                    width,
19059                                    attributes);
19060             }
19061           else
19062             {
19063               cp_declarator *declarator;
19064               tree initializer;
19065               tree asm_specification;
19066               int ctor_dtor_or_conv_p;
19067
19068               /* Parse the declarator.  */
19069               declarator
19070                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19071                                         &ctor_dtor_or_conv_p,
19072                                         /*parenthesized_p=*/NULL,
19073                                         /*member_p=*/true);
19074
19075               /* If something went wrong parsing the declarator, make sure
19076                  that we at least consume some tokens.  */
19077               if (declarator == cp_error_declarator)
19078                 {
19079                   /* Skip to the end of the statement.  */
19080                   cp_parser_skip_to_end_of_statement (parser);
19081                   /* If the next token is not a semicolon, that is
19082                      probably because we just skipped over the body of
19083                      a function.  So, we consume a semicolon if
19084                      present, but do not issue an error message if it
19085                      is not present.  */
19086                   if (cp_lexer_next_token_is (parser->lexer,
19087                                               CPP_SEMICOLON))
19088                     cp_lexer_consume_token (parser->lexer);
19089                   goto out;
19090                 }
19091
19092               if (declares_class_or_enum & 2)
19093                 cp_parser_check_for_definition_in_return_type
19094                                             (declarator, decl_specifiers.type,
19095                                              decl_specifiers.type_location);
19096
19097               /* Look for an asm-specification.  */
19098               asm_specification = cp_parser_asm_specification_opt (parser);
19099               /* Look for attributes that apply to the declaration.  */
19100               attributes = cp_parser_attributes_opt (parser);
19101               /* Remember which attributes are prefix attributes and
19102                  which are not.  */
19103               first_attribute = attributes;
19104               /* Combine the attributes.  */
19105               attributes = chainon (prefix_attributes, attributes);
19106
19107               /* If it's an `=', then we have a constant-initializer or a
19108                  pure-specifier.  It is not correct to parse the
19109                  initializer before registering the member declaration
19110                  since the member declaration should be in scope while
19111                  its initializer is processed.  However, the rest of the
19112                  front end does not yet provide an interface that allows
19113                  us to handle this correctly.  */
19114               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19115                 {
19116                   /* In [class.mem]:
19117
19118                      A pure-specifier shall be used only in the declaration of
19119                      a virtual function.
19120
19121                      A member-declarator can contain a constant-initializer
19122                      only if it declares a static member of integral or
19123                      enumeration type.
19124
19125                      Therefore, if the DECLARATOR is for a function, we look
19126                      for a pure-specifier; otherwise, we look for a
19127                      constant-initializer.  When we call `grokfield', it will
19128                      perform more stringent semantics checks.  */
19129                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19130                   if (function_declarator_p (declarator)
19131                       || (decl_specifiers.type
19132                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19133                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19134                               == FUNCTION_TYPE)))
19135                     initializer = cp_parser_pure_specifier (parser);
19136                   else if (decl_specifiers.storage_class != sc_static)
19137                     initializer = cp_parser_save_nsdmi (parser);
19138                   else if (cxx_dialect >= cxx0x)
19139                     {
19140                       bool nonconst;
19141                       /* Don't require a constant rvalue in C++11, since we
19142                          might want a reference constant.  We'll enforce
19143                          constancy later.  */
19144                       cp_lexer_consume_token (parser->lexer);
19145                       /* Parse the initializer.  */
19146                       initializer = cp_parser_initializer_clause (parser,
19147                                                                   &nonconst);
19148                     }
19149                   else
19150                     /* Parse the initializer.  */
19151                     initializer = cp_parser_constant_initializer (parser);
19152                 }
19153               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19154                        && !function_declarator_p (declarator))
19155                 {
19156                   bool x;
19157                   if (decl_specifiers.storage_class != sc_static)
19158                     initializer = cp_parser_save_nsdmi (parser);
19159                   else
19160                     initializer = cp_parser_initializer (parser, &x, &x);
19161                 }
19162               /* Otherwise, there is no initializer.  */
19163               else
19164                 initializer = NULL_TREE;
19165
19166               /* See if we are probably looking at a function
19167                  definition.  We are certainly not looking at a
19168                  member-declarator.  Calling `grokfield' has
19169                  side-effects, so we must not do it unless we are sure
19170                  that we are looking at a member-declarator.  */
19171               if (cp_parser_token_starts_function_definition_p
19172                   (cp_lexer_peek_token (parser->lexer)))
19173                 {
19174                   /* The grammar does not allow a pure-specifier to be
19175                      used when a member function is defined.  (It is
19176                      possible that this fact is an oversight in the
19177                      standard, since a pure function may be defined
19178                      outside of the class-specifier.  */
19179                   if (initializer)
19180                     error_at (initializer_token_start->location,
19181                               "pure-specifier on function-definition");
19182                   decl = cp_parser_save_member_function_body (parser,
19183                                                               &decl_specifiers,
19184                                                               declarator,
19185                                                               attributes);
19186                   /* If the member was not a friend, declare it here.  */
19187                   if (!friend_p)
19188                     finish_member_declaration (decl);
19189                   /* Peek at the next token.  */
19190                   token = cp_lexer_peek_token (parser->lexer);
19191                   /* If the next token is a semicolon, consume it.  */
19192                   if (token->type == CPP_SEMICOLON)
19193                     cp_lexer_consume_token (parser->lexer);
19194                   goto out;
19195                 }
19196               else
19197                 if (declarator->kind == cdk_function)
19198                   declarator->id_loc = token->location;
19199                 /* Create the declaration.  */
19200                 decl = grokfield (declarator, &decl_specifiers,
19201                                   initializer, /*init_const_expr_p=*/true,
19202                                   asm_specification,
19203                                   attributes);
19204             }
19205
19206           /* Reset PREFIX_ATTRIBUTES.  */
19207           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19208             attributes = TREE_CHAIN (attributes);
19209           if (attributes)
19210             TREE_CHAIN (attributes) = NULL_TREE;
19211
19212           /* If there is any qualification still in effect, clear it
19213              now; we will be starting fresh with the next declarator.  */
19214           parser->scope = NULL_TREE;
19215           parser->qualifying_scope = NULL_TREE;
19216           parser->object_scope = NULL_TREE;
19217           /* If it's a `,', then there are more declarators.  */
19218           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19219             cp_lexer_consume_token (parser->lexer);
19220           /* If the next token isn't a `;', then we have a parse error.  */
19221           else if (cp_lexer_next_token_is_not (parser->lexer,
19222                                                CPP_SEMICOLON))
19223             {
19224               /* The next token might be a ways away from where the
19225                  actual semicolon is missing.  Find the previous token
19226                  and use that for our error position.  */
19227               cp_token *token = cp_lexer_previous_token (parser->lexer);
19228               error_at (token->location,
19229                         "expected %<;%> at end of member declaration");
19230
19231               /* Assume that the user meant to provide a semicolon.  If
19232                  we were to cp_parser_skip_to_end_of_statement, we might
19233                  skip to a semicolon inside a member function definition
19234                  and issue nonsensical error messages.  */
19235               assume_semicolon = true;
19236             }
19237
19238           if (decl)
19239             {
19240               /* Add DECL to the list of members.  */
19241               if (!friend_p)
19242                 finish_member_declaration (decl);
19243
19244               if (TREE_CODE (decl) == FUNCTION_DECL)
19245                 cp_parser_save_default_args (parser, decl);
19246               else if (TREE_CODE (decl) == FIELD_DECL
19247                        && !DECL_C_BIT_FIELD (decl)
19248                        && DECL_INITIAL (decl))
19249                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19250                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19251             }
19252
19253           if (assume_semicolon)
19254             goto out;
19255         }
19256     }
19257
19258   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19259  out:
19260   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19261 }
19262
19263 /* Parse a pure-specifier.
19264
19265    pure-specifier:
19266      = 0
19267
19268    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19269    Otherwise, ERROR_MARK_NODE is returned.  */
19270
19271 static tree
19272 cp_parser_pure_specifier (cp_parser* parser)
19273 {
19274   cp_token *token;
19275
19276   /* Look for the `=' token.  */
19277   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19278     return error_mark_node;
19279   /* Look for the `0' token.  */
19280   token = cp_lexer_peek_token (parser->lexer);
19281
19282   if (token->type == CPP_EOF
19283       || token->type == CPP_PRAGMA_EOL)
19284     return error_mark_node;
19285
19286   cp_lexer_consume_token (parser->lexer);
19287
19288   /* Accept = default or = delete in c++0x mode.  */
19289   if (token->keyword == RID_DEFAULT
19290       || token->keyword == RID_DELETE)
19291     {
19292       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19293       return token->u.value;
19294     }
19295
19296   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19297   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19298     {
19299       cp_parser_error (parser,
19300                        "invalid pure specifier (only %<= 0%> is allowed)");
19301       cp_parser_skip_to_end_of_statement (parser);
19302       return error_mark_node;
19303     }
19304   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19305     {
19306       error_at (token->location, "templates may not be %<virtual%>");
19307       return error_mark_node;
19308     }
19309
19310   return integer_zero_node;
19311 }
19312
19313 /* Parse a constant-initializer.
19314
19315    constant-initializer:
19316      = constant-expression
19317
19318    Returns a representation of the constant-expression.  */
19319
19320 static tree
19321 cp_parser_constant_initializer (cp_parser* parser)
19322 {
19323   /* Look for the `=' token.  */
19324   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19325     return error_mark_node;
19326
19327   /* It is invalid to write:
19328
19329        struct S { static const int i = { 7 }; };
19330
19331      */
19332   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19333     {
19334       cp_parser_error (parser,
19335                        "a brace-enclosed initializer is not allowed here");
19336       /* Consume the opening brace.  */
19337       cp_lexer_consume_token (parser->lexer);
19338       /* Skip the initializer.  */
19339       cp_parser_skip_to_closing_brace (parser);
19340       /* Look for the trailing `}'.  */
19341       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19342
19343       return error_mark_node;
19344     }
19345
19346   return cp_parser_constant_expression (parser,
19347                                         /*allow_non_constant=*/false,
19348                                         NULL);
19349 }
19350
19351 /* Derived classes [gram.class.derived] */
19352
19353 /* Parse a base-clause.
19354
19355    base-clause:
19356      : base-specifier-list
19357
19358    base-specifier-list:
19359      base-specifier ... [opt]
19360      base-specifier-list , base-specifier ... [opt]
19361
19362    Returns a TREE_LIST representing the base-classes, in the order in
19363    which they were declared.  The representation of each node is as
19364    described by cp_parser_base_specifier.
19365
19366    In the case that no bases are specified, this function will return
19367    NULL_TREE, not ERROR_MARK_NODE.  */
19368
19369 static tree
19370 cp_parser_base_clause (cp_parser* parser)
19371 {
19372   tree bases = NULL_TREE;
19373
19374   /* Look for the `:' that begins the list.  */
19375   cp_parser_require (parser, CPP_COLON, RT_COLON);
19376
19377   /* Scan the base-specifier-list.  */
19378   while (true)
19379     {
19380       cp_token *token;
19381       tree base;
19382       bool pack_expansion_p = false;
19383
19384       /* Look for the base-specifier.  */
19385       base = cp_parser_base_specifier (parser);
19386       /* Look for the (optional) ellipsis. */
19387       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19388         {
19389           /* Consume the `...'. */
19390           cp_lexer_consume_token (parser->lexer);
19391
19392           pack_expansion_p = true;
19393         }
19394
19395       /* Add BASE to the front of the list.  */
19396       if (base && base != error_mark_node)
19397         {
19398           if (pack_expansion_p)
19399             /* Make this a pack expansion type. */
19400             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19401
19402           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19403             {
19404               TREE_CHAIN (base) = bases;
19405               bases = base;
19406             }
19407         }
19408       /* Peek at the next token.  */
19409       token = cp_lexer_peek_token (parser->lexer);
19410       /* If it's not a comma, then the list is complete.  */
19411       if (token->type != CPP_COMMA)
19412         break;
19413       /* Consume the `,'.  */
19414       cp_lexer_consume_token (parser->lexer);
19415     }
19416
19417   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19418      base class had a qualified name.  However, the next name that
19419      appears is certainly not qualified.  */
19420   parser->scope = NULL_TREE;
19421   parser->qualifying_scope = NULL_TREE;
19422   parser->object_scope = NULL_TREE;
19423
19424   return nreverse (bases);
19425 }
19426
19427 /* Parse a base-specifier.
19428
19429    base-specifier:
19430      :: [opt] nested-name-specifier [opt] class-name
19431      virtual access-specifier [opt] :: [opt] nested-name-specifier
19432        [opt] class-name
19433      access-specifier virtual [opt] :: [opt] nested-name-specifier
19434        [opt] class-name
19435
19436    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19437    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19438    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19439    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19440
19441 static tree
19442 cp_parser_base_specifier (cp_parser* parser)
19443 {
19444   cp_token *token;
19445   bool done = false;
19446   bool virtual_p = false;
19447   bool duplicate_virtual_error_issued_p = false;
19448   bool duplicate_access_error_issued_p = false;
19449   bool class_scope_p, template_p;
19450   tree access = access_default_node;
19451   tree type;
19452
19453   /* Process the optional `virtual' and `access-specifier'.  */
19454   while (!done)
19455     {
19456       /* Peek at the next token.  */
19457       token = cp_lexer_peek_token (parser->lexer);
19458       /* Process `virtual'.  */
19459       switch (token->keyword)
19460         {
19461         case RID_VIRTUAL:
19462           /* If `virtual' appears more than once, issue an error.  */
19463           if (virtual_p && !duplicate_virtual_error_issued_p)
19464             {
19465               cp_parser_error (parser,
19466                                "%<virtual%> specified more than once in base-specified");
19467               duplicate_virtual_error_issued_p = true;
19468             }
19469
19470           virtual_p = true;
19471
19472           /* Consume the `virtual' token.  */
19473           cp_lexer_consume_token (parser->lexer);
19474
19475           break;
19476
19477         case RID_PUBLIC:
19478         case RID_PROTECTED:
19479         case RID_PRIVATE:
19480           /* If more than one access specifier appears, issue an
19481              error.  */
19482           if (access != access_default_node
19483               && !duplicate_access_error_issued_p)
19484             {
19485               cp_parser_error (parser,
19486                                "more than one access specifier in base-specified");
19487               duplicate_access_error_issued_p = true;
19488             }
19489
19490           access = ridpointers[(int) token->keyword];
19491
19492           /* Consume the access-specifier.  */
19493           cp_lexer_consume_token (parser->lexer);
19494
19495           break;
19496
19497         default:
19498           done = true;
19499           break;
19500         }
19501     }
19502   /* It is not uncommon to see programs mechanically, erroneously, use
19503      the 'typename' keyword to denote (dependent) qualified types
19504      as base classes.  */
19505   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19506     {
19507       token = cp_lexer_peek_token (parser->lexer);
19508       if (!processing_template_decl)
19509         error_at (token->location,
19510                   "keyword %<typename%> not allowed outside of templates");
19511       else
19512         error_at (token->location,
19513                   "keyword %<typename%> not allowed in this context "
19514                   "(the base class is implicitly a type)");
19515       cp_lexer_consume_token (parser->lexer);
19516     }
19517
19518   /* Look for the optional `::' operator.  */
19519   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19520   /* Look for the nested-name-specifier.  The simplest way to
19521      implement:
19522
19523        [temp.res]
19524
19525        The keyword `typename' is not permitted in a base-specifier or
19526        mem-initializer; in these contexts a qualified name that
19527        depends on a template-parameter is implicitly assumed to be a
19528        type name.
19529
19530      is to pretend that we have seen the `typename' keyword at this
19531      point.  */
19532   cp_parser_nested_name_specifier_opt (parser,
19533                                        /*typename_keyword_p=*/true,
19534                                        /*check_dependency_p=*/true,
19535                                        typename_type,
19536                                        /*is_declaration=*/true);
19537   /* If the base class is given by a qualified name, assume that names
19538      we see are type names or templates, as appropriate.  */
19539   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19540   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19541
19542   if (!parser->scope
19543       && cp_lexer_next_token_is_decltype (parser->lexer))
19544     /* DR 950 allows decltype as a base-specifier.  */
19545     type = cp_parser_decltype (parser);
19546   else
19547     {
19548       /* Otherwise, look for the class-name.  */
19549       type = cp_parser_class_name (parser,
19550                                    class_scope_p,
19551                                    template_p,
19552                                    typename_type,
19553                                    /*check_dependency_p=*/true,
19554                                    /*class_head_p=*/false,
19555                                    /*is_declaration=*/true);
19556       type = TREE_TYPE (type);
19557     }
19558
19559   if (type == error_mark_node)
19560     return error_mark_node;
19561
19562   return finish_base_specifier (type, access, virtual_p);
19563 }
19564
19565 /* Exception handling [gram.exception] */
19566
19567 /* Parse an (optional) noexcept-specification.
19568
19569    noexcept-specification:
19570      noexcept ( constant-expression ) [opt]
19571
19572    If no noexcept-specification is present, returns NULL_TREE.
19573    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19574    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19575    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19576    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19577    in which case a boolean condition is returned instead.  */
19578
19579 static tree
19580 cp_parser_noexcept_specification_opt (cp_parser* parser,
19581                                       bool require_constexpr,
19582                                       bool* consumed_expr,
19583                                       bool return_cond)
19584 {
19585   cp_token *token;
19586   const char *saved_message;
19587
19588   /* Peek at the next token.  */
19589   token = cp_lexer_peek_token (parser->lexer);
19590
19591   /* Is it a noexcept-specification?  */
19592   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19593     {
19594       tree expr;
19595       cp_lexer_consume_token (parser->lexer);
19596
19597       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19598         {
19599           cp_lexer_consume_token (parser->lexer);
19600
19601           if (require_constexpr)
19602             {
19603               /* Types may not be defined in an exception-specification.  */
19604               saved_message = parser->type_definition_forbidden_message;
19605               parser->type_definition_forbidden_message
19606               = G_("types may not be defined in an exception-specification");
19607
19608               expr = cp_parser_constant_expression (parser, false, NULL);
19609
19610               /* Restore the saved message.  */
19611               parser->type_definition_forbidden_message = saved_message;
19612             }
19613           else
19614             {
19615               expr = cp_parser_expression (parser, false, NULL);
19616               *consumed_expr = true;
19617             }
19618
19619           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19620         }
19621       else
19622         {
19623           expr = boolean_true_node;
19624           if (!require_constexpr)
19625             *consumed_expr = false;
19626         }
19627
19628       /* We cannot build a noexcept-spec right away because this will check
19629          that expr is a constexpr.  */
19630       if (!return_cond)
19631         return build_noexcept_spec (expr, tf_warning_or_error);
19632       else
19633         return expr;
19634     }
19635   else
19636     return NULL_TREE;
19637 }
19638
19639 /* Parse an (optional) exception-specification.
19640
19641    exception-specification:
19642      throw ( type-id-list [opt] )
19643
19644    Returns a TREE_LIST representing the exception-specification.  The
19645    TREE_VALUE of each node is a type.  */
19646
19647 static tree
19648 cp_parser_exception_specification_opt (cp_parser* parser)
19649 {
19650   cp_token *token;
19651   tree type_id_list;
19652   const char *saved_message;
19653
19654   /* Peek at the next token.  */
19655   token = cp_lexer_peek_token (parser->lexer);
19656
19657   /* Is it a noexcept-specification?  */
19658   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19659                                                       false);
19660   if (type_id_list != NULL_TREE)
19661     return type_id_list;
19662
19663   /* If it's not `throw', then there's no exception-specification.  */
19664   if (!cp_parser_is_keyword (token, RID_THROW))
19665     return NULL_TREE;
19666
19667 #if 0
19668   /* Enable this once a lot of code has transitioned to noexcept?  */
19669   if (cxx_dialect == cxx0x && !in_system_header)
19670     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19671              "deprecated in C++0x; use %<noexcept%> instead");
19672 #endif
19673
19674   /* Consume the `throw'.  */
19675   cp_lexer_consume_token (parser->lexer);
19676
19677   /* Look for the `('.  */
19678   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19679
19680   /* Peek at the next token.  */
19681   token = cp_lexer_peek_token (parser->lexer);
19682   /* If it's not a `)', then there is a type-id-list.  */
19683   if (token->type != CPP_CLOSE_PAREN)
19684     {
19685       /* Types may not be defined in an exception-specification.  */
19686       saved_message = parser->type_definition_forbidden_message;
19687       parser->type_definition_forbidden_message
19688         = G_("types may not be defined in an exception-specification");
19689       /* Parse the type-id-list.  */
19690       type_id_list = cp_parser_type_id_list (parser);
19691       /* Restore the saved message.  */
19692       parser->type_definition_forbidden_message = saved_message;
19693     }
19694   else
19695     type_id_list = empty_except_spec;
19696
19697   /* Look for the `)'.  */
19698   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19699
19700   return type_id_list;
19701 }
19702
19703 /* Parse an (optional) type-id-list.
19704
19705    type-id-list:
19706      type-id ... [opt]
19707      type-id-list , type-id ... [opt]
19708
19709    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19710    in the order that the types were presented.  */
19711
19712 static tree
19713 cp_parser_type_id_list (cp_parser* parser)
19714 {
19715   tree types = NULL_TREE;
19716
19717   while (true)
19718     {
19719       cp_token *token;
19720       tree type;
19721
19722       /* Get the next type-id.  */
19723       type = cp_parser_type_id (parser);
19724       /* Parse the optional ellipsis. */
19725       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19726         {
19727           /* Consume the `...'. */
19728           cp_lexer_consume_token (parser->lexer);
19729
19730           /* Turn the type into a pack expansion expression. */
19731           type = make_pack_expansion (type);
19732         }
19733       /* Add it to the list.  */
19734       types = add_exception_specifier (types, type, /*complain=*/1);
19735       /* Peek at the next token.  */
19736       token = cp_lexer_peek_token (parser->lexer);
19737       /* If it is not a `,', we are done.  */
19738       if (token->type != CPP_COMMA)
19739         break;
19740       /* Consume the `,'.  */
19741       cp_lexer_consume_token (parser->lexer);
19742     }
19743
19744   return nreverse (types);
19745 }
19746
19747 /* Parse a try-block.
19748
19749    try-block:
19750      try compound-statement handler-seq  */
19751
19752 static tree
19753 cp_parser_try_block (cp_parser* parser)
19754 {
19755   tree try_block;
19756
19757   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19758   try_block = begin_try_block ();
19759   cp_parser_compound_statement (parser, NULL, true, false);
19760   finish_try_block (try_block);
19761   cp_parser_handler_seq (parser);
19762   finish_handler_sequence (try_block);
19763
19764   return try_block;
19765 }
19766
19767 /* Parse a function-try-block.
19768
19769    function-try-block:
19770      try ctor-initializer [opt] function-body handler-seq  */
19771
19772 static bool
19773 cp_parser_function_try_block (cp_parser* parser)
19774 {
19775   tree compound_stmt;
19776   tree try_block;
19777   bool ctor_initializer_p;
19778
19779   /* Look for the `try' keyword.  */
19780   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19781     return false;
19782   /* Let the rest of the front end know where we are.  */
19783   try_block = begin_function_try_block (&compound_stmt);
19784   /* Parse the function-body.  */
19785   ctor_initializer_p
19786     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19787   /* We're done with the `try' part.  */
19788   finish_function_try_block (try_block);
19789   /* Parse the handlers.  */
19790   cp_parser_handler_seq (parser);
19791   /* We're done with the handlers.  */
19792   finish_function_handler_sequence (try_block, compound_stmt);
19793
19794   return ctor_initializer_p;
19795 }
19796
19797 /* Parse a handler-seq.
19798
19799    handler-seq:
19800      handler handler-seq [opt]  */
19801
19802 static void
19803 cp_parser_handler_seq (cp_parser* parser)
19804 {
19805   while (true)
19806     {
19807       cp_token *token;
19808
19809       /* Parse the handler.  */
19810       cp_parser_handler (parser);
19811       /* Peek at the next token.  */
19812       token = cp_lexer_peek_token (parser->lexer);
19813       /* If it's not `catch' then there are no more handlers.  */
19814       if (!cp_parser_is_keyword (token, RID_CATCH))
19815         break;
19816     }
19817 }
19818
19819 /* Parse a handler.
19820
19821    handler:
19822      catch ( exception-declaration ) compound-statement  */
19823
19824 static void
19825 cp_parser_handler (cp_parser* parser)
19826 {
19827   tree handler;
19828   tree declaration;
19829
19830   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19831   handler = begin_handler ();
19832   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19833   declaration = cp_parser_exception_declaration (parser);
19834   finish_handler_parms (declaration, handler);
19835   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19836   cp_parser_compound_statement (parser, NULL, false, false);
19837   finish_handler (handler);
19838 }
19839
19840 /* Parse an exception-declaration.
19841
19842    exception-declaration:
19843      type-specifier-seq declarator
19844      type-specifier-seq abstract-declarator
19845      type-specifier-seq
19846      ...
19847
19848    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19849    ellipsis variant is used.  */
19850
19851 static tree
19852 cp_parser_exception_declaration (cp_parser* parser)
19853 {
19854   cp_decl_specifier_seq type_specifiers;
19855   cp_declarator *declarator;
19856   const char *saved_message;
19857
19858   /* If it's an ellipsis, it's easy to handle.  */
19859   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19860     {
19861       /* Consume the `...' token.  */
19862       cp_lexer_consume_token (parser->lexer);
19863       return NULL_TREE;
19864     }
19865
19866   /* Types may not be defined in exception-declarations.  */
19867   saved_message = parser->type_definition_forbidden_message;
19868   parser->type_definition_forbidden_message
19869     = G_("types may not be defined in exception-declarations");
19870
19871   /* Parse the type-specifier-seq.  */
19872   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19873                                 /*is_trailing_return=*/false,
19874                                 &type_specifiers);
19875   /* If it's a `)', then there is no declarator.  */
19876   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19877     declarator = NULL;
19878   else
19879     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19880                                        /*ctor_dtor_or_conv_p=*/NULL,
19881                                        /*parenthesized_p=*/NULL,
19882                                        /*member_p=*/false);
19883
19884   /* Restore the saved message.  */
19885   parser->type_definition_forbidden_message = saved_message;
19886
19887   if (!type_specifiers.any_specifiers_p)
19888     return error_mark_node;
19889
19890   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19891 }
19892
19893 /* Parse a throw-expression.
19894
19895    throw-expression:
19896      throw assignment-expression [opt]
19897
19898    Returns a THROW_EXPR representing the throw-expression.  */
19899
19900 static tree
19901 cp_parser_throw_expression (cp_parser* parser)
19902 {
19903   tree expression;
19904   cp_token* token;
19905
19906   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19907   token = cp_lexer_peek_token (parser->lexer);
19908   /* Figure out whether or not there is an assignment-expression
19909      following the "throw" keyword.  */
19910   if (token->type == CPP_COMMA
19911       || token->type == CPP_SEMICOLON
19912       || token->type == CPP_CLOSE_PAREN
19913       || token->type == CPP_CLOSE_SQUARE
19914       || token->type == CPP_CLOSE_BRACE
19915       || token->type == CPP_COLON)
19916     expression = NULL_TREE;
19917   else
19918     expression = cp_parser_assignment_expression (parser,
19919                                                   /*cast_p=*/false, NULL);
19920
19921   return build_throw (expression);
19922 }
19923
19924 /* GNU Extensions */
19925
19926 /* Parse an (optional) asm-specification.
19927
19928    asm-specification:
19929      asm ( string-literal )
19930
19931    If the asm-specification is present, returns a STRING_CST
19932    corresponding to the string-literal.  Otherwise, returns
19933    NULL_TREE.  */
19934
19935 static tree
19936 cp_parser_asm_specification_opt (cp_parser* parser)
19937 {
19938   cp_token *token;
19939   tree asm_specification;
19940
19941   /* Peek at the next token.  */
19942   token = cp_lexer_peek_token (parser->lexer);
19943   /* If the next token isn't the `asm' keyword, then there's no
19944      asm-specification.  */
19945   if (!cp_parser_is_keyword (token, RID_ASM))
19946     return NULL_TREE;
19947
19948   /* Consume the `asm' token.  */
19949   cp_lexer_consume_token (parser->lexer);
19950   /* Look for the `('.  */
19951   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19952
19953   /* Look for the string-literal.  */
19954   asm_specification = cp_parser_string_literal (parser, false, false);
19955
19956   /* Look for the `)'.  */
19957   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19958
19959   return asm_specification;
19960 }
19961
19962 /* Parse an asm-operand-list.
19963
19964    asm-operand-list:
19965      asm-operand
19966      asm-operand-list , asm-operand
19967
19968    asm-operand:
19969      string-literal ( expression )
19970      [ string-literal ] string-literal ( expression )
19971
19972    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19973    each node is the expression.  The TREE_PURPOSE is itself a
19974    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19975    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19976    is a STRING_CST for the string literal before the parenthesis. Returns
19977    ERROR_MARK_NODE if any of the operands are invalid.  */
19978
19979 static tree
19980 cp_parser_asm_operand_list (cp_parser* parser)
19981 {
19982   tree asm_operands = NULL_TREE;
19983   bool invalid_operands = false;
19984
19985   while (true)
19986     {
19987       tree string_literal;
19988       tree expression;
19989       tree name;
19990
19991       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19992         {
19993           /* Consume the `[' token.  */
19994           cp_lexer_consume_token (parser->lexer);
19995           /* Read the operand name.  */
19996           name = cp_parser_identifier (parser);
19997           if (name != error_mark_node)
19998             name = build_string (IDENTIFIER_LENGTH (name),
19999                                  IDENTIFIER_POINTER (name));
20000           /* Look for the closing `]'.  */
20001           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20002         }
20003       else
20004         name = NULL_TREE;
20005       /* Look for the string-literal.  */
20006       string_literal = cp_parser_string_literal (parser, false, false);
20007
20008       /* Look for the `('.  */
20009       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20010       /* Parse the expression.  */
20011       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20012       /* Look for the `)'.  */
20013       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20014
20015       if (name == error_mark_node 
20016           || string_literal == error_mark_node 
20017           || expression == error_mark_node)
20018         invalid_operands = true;
20019
20020       /* Add this operand to the list.  */
20021       asm_operands = tree_cons (build_tree_list (name, string_literal),
20022                                 expression,
20023                                 asm_operands);
20024       /* If the next token is not a `,', there are no more
20025          operands.  */
20026       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20027         break;
20028       /* Consume the `,'.  */
20029       cp_lexer_consume_token (parser->lexer);
20030     }
20031
20032   return invalid_operands ? error_mark_node : nreverse (asm_operands);
20033 }
20034
20035 /* Parse an asm-clobber-list.
20036
20037    asm-clobber-list:
20038      string-literal
20039      asm-clobber-list , string-literal
20040
20041    Returns a TREE_LIST, indicating the clobbers in the order that they
20042    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
20043
20044 static tree
20045 cp_parser_asm_clobber_list (cp_parser* parser)
20046 {
20047   tree clobbers = NULL_TREE;
20048
20049   while (true)
20050     {
20051       tree string_literal;
20052
20053       /* Look for the string literal.  */
20054       string_literal = cp_parser_string_literal (parser, false, false);
20055       /* Add it to the list.  */
20056       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20057       /* If the next token is not a `,', then the list is
20058          complete.  */
20059       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20060         break;
20061       /* Consume the `,' token.  */
20062       cp_lexer_consume_token (parser->lexer);
20063     }
20064
20065   return clobbers;
20066 }
20067
20068 /* Parse an asm-label-list.
20069
20070    asm-label-list:
20071      identifier
20072      asm-label-list , identifier
20073
20074    Returns a TREE_LIST, indicating the labels in the order that they
20075    appeared.  The TREE_VALUE of each node is a label.  */
20076
20077 static tree
20078 cp_parser_asm_label_list (cp_parser* parser)
20079 {
20080   tree labels = NULL_TREE;
20081
20082   while (true)
20083     {
20084       tree identifier, label, name;
20085
20086       /* Look for the identifier.  */
20087       identifier = cp_parser_identifier (parser);
20088       if (!error_operand_p (identifier))
20089         {
20090           label = lookup_label (identifier);
20091           if (TREE_CODE (label) == LABEL_DECL)
20092             {
20093               TREE_USED (label) = 1;
20094               check_goto (label);
20095               name = build_string (IDENTIFIER_LENGTH (identifier),
20096                                    IDENTIFIER_POINTER (identifier));
20097               labels = tree_cons (name, label, labels);
20098             }
20099         }
20100       /* If the next token is not a `,', then the list is
20101          complete.  */
20102       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20103         break;
20104       /* Consume the `,' token.  */
20105       cp_lexer_consume_token (parser->lexer);
20106     }
20107
20108   return nreverse (labels);
20109 }
20110
20111 /* Parse an (optional) series of attributes.
20112
20113    attributes:
20114      attributes attribute
20115
20116    attribute:
20117      __attribute__ (( attribute-list [opt] ))
20118
20119    The return value is as for cp_parser_attribute_list.  */
20120
20121 static tree
20122 cp_parser_attributes_opt (cp_parser* parser)
20123 {
20124   tree attributes = NULL_TREE;
20125
20126   while (true)
20127     {
20128       cp_token *token;
20129       tree attribute_list;
20130
20131       /* Peek at the next token.  */
20132       token = cp_lexer_peek_token (parser->lexer);
20133       /* If it's not `__attribute__', then we're done.  */
20134       if (token->keyword != RID_ATTRIBUTE)
20135         break;
20136
20137       /* Consume the `__attribute__' keyword.  */
20138       cp_lexer_consume_token (parser->lexer);
20139       /* Look for the two `(' tokens.  */
20140       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20141       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20142
20143       /* Peek at the next token.  */
20144       token = cp_lexer_peek_token (parser->lexer);
20145       if (token->type != CPP_CLOSE_PAREN)
20146         /* Parse the attribute-list.  */
20147         attribute_list = cp_parser_attribute_list (parser);
20148       else
20149         /* If the next token is a `)', then there is no attribute
20150            list.  */
20151         attribute_list = NULL;
20152
20153       /* Look for the two `)' tokens.  */
20154       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20155       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20156
20157       /* Add these new attributes to the list.  */
20158       attributes = chainon (attributes, attribute_list);
20159     }
20160
20161   return attributes;
20162 }
20163
20164 /* Parse an attribute-list.
20165
20166    attribute-list:
20167      attribute
20168      attribute-list , attribute
20169
20170    attribute:
20171      identifier
20172      identifier ( identifier )
20173      identifier ( identifier , expression-list )
20174      identifier ( expression-list )
20175
20176    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20177    to an attribute.  The TREE_PURPOSE of each node is the identifier
20178    indicating which attribute is in use.  The TREE_VALUE represents
20179    the arguments, if any.  */
20180
20181 static tree
20182 cp_parser_attribute_list (cp_parser* parser)
20183 {
20184   tree attribute_list = NULL_TREE;
20185   bool save_translate_strings_p = parser->translate_strings_p;
20186
20187   parser->translate_strings_p = false;
20188   while (true)
20189     {
20190       cp_token *token;
20191       tree identifier;
20192       tree attribute;
20193
20194       /* Look for the identifier.  We also allow keywords here; for
20195          example `__attribute__ ((const))' is legal.  */
20196       token = cp_lexer_peek_token (parser->lexer);
20197       if (token->type == CPP_NAME
20198           || token->type == CPP_KEYWORD)
20199         {
20200           tree arguments = NULL_TREE;
20201
20202           /* Consume the token.  */
20203           token = cp_lexer_consume_token (parser->lexer);
20204
20205           /* Save away the identifier that indicates which attribute
20206              this is.  */
20207           identifier = (token->type == CPP_KEYWORD) 
20208             /* For keywords, use the canonical spelling, not the
20209                parsed identifier.  */
20210             ? ridpointers[(int) token->keyword]
20211             : token->u.value;
20212           
20213           attribute = build_tree_list (identifier, NULL_TREE);
20214
20215           /* Peek at the next token.  */
20216           token = cp_lexer_peek_token (parser->lexer);
20217           /* If it's an `(', then parse the attribute arguments.  */
20218           if (token->type == CPP_OPEN_PAREN)
20219             {
20220               VEC(tree,gc) *vec;
20221               int attr_flag = (attribute_takes_identifier_p (identifier)
20222                                ? id_attr : normal_attr);
20223               vec = cp_parser_parenthesized_expression_list
20224                     (parser, attr_flag, /*cast_p=*/false,
20225                      /*allow_expansion_p=*/false,
20226                      /*non_constant_p=*/NULL);
20227               if (vec == NULL)
20228                 arguments = error_mark_node;
20229               else
20230                 {
20231                   arguments = build_tree_list_vec (vec);
20232                   release_tree_vector (vec);
20233                 }
20234               /* Save the arguments away.  */
20235               TREE_VALUE (attribute) = arguments;
20236             }
20237
20238           if (arguments != error_mark_node)
20239             {
20240               /* Add this attribute to the list.  */
20241               TREE_CHAIN (attribute) = attribute_list;
20242               attribute_list = attribute;
20243             }
20244
20245           token = cp_lexer_peek_token (parser->lexer);
20246         }
20247       /* Now, look for more attributes.  If the next token isn't a
20248          `,', we're done.  */
20249       if (token->type != CPP_COMMA)
20250         break;
20251
20252       /* Consume the comma and keep going.  */
20253       cp_lexer_consume_token (parser->lexer);
20254     }
20255   parser->translate_strings_p = save_translate_strings_p;
20256
20257   /* We built up the list in reverse order.  */
20258   return nreverse (attribute_list);
20259 }
20260
20261 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20262    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20263    current value of the PEDANTIC flag, regardless of whether or not
20264    the `__extension__' keyword is present.  The caller is responsible
20265    for restoring the value of the PEDANTIC flag.  */
20266
20267 static bool
20268 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20269 {
20270   /* Save the old value of the PEDANTIC flag.  */
20271   *saved_pedantic = pedantic;
20272
20273   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20274     {
20275       /* Consume the `__extension__' token.  */
20276       cp_lexer_consume_token (parser->lexer);
20277       /* We're not being pedantic while the `__extension__' keyword is
20278          in effect.  */
20279       pedantic = 0;
20280
20281       return true;
20282     }
20283
20284   return false;
20285 }
20286
20287 /* Parse a label declaration.
20288
20289    label-declaration:
20290      __label__ label-declarator-seq ;
20291
20292    label-declarator-seq:
20293      identifier , label-declarator-seq
20294      identifier  */
20295
20296 static void
20297 cp_parser_label_declaration (cp_parser* parser)
20298 {
20299   /* Look for the `__label__' keyword.  */
20300   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20301
20302   while (true)
20303     {
20304       tree identifier;
20305
20306       /* Look for an identifier.  */
20307       identifier = cp_parser_identifier (parser);
20308       /* If we failed, stop.  */
20309       if (identifier == error_mark_node)
20310         break;
20311       /* Declare it as a label.  */
20312       finish_label_decl (identifier);
20313       /* If the next token is a `;', stop.  */
20314       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20315         break;
20316       /* Look for the `,' separating the label declarations.  */
20317       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20318     }
20319
20320   /* Look for the final `;'.  */
20321   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20322 }
20323
20324 /* Support Functions */
20325
20326 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20327    NAME should have one of the representations used for an
20328    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20329    is returned.  If PARSER->SCOPE is a dependent type, then a
20330    SCOPE_REF is returned.
20331
20332    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20333    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20334    was formed.  Abstractly, such entities should not be passed to this
20335    function, because they do not need to be looked up, but it is
20336    simpler to check for this special case here, rather than at the
20337    call-sites.
20338
20339    In cases not explicitly covered above, this function returns a
20340    DECL, OVERLOAD, or baselink representing the result of the lookup.
20341    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20342    is returned.
20343
20344    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20345    (e.g., "struct") that was used.  In that case bindings that do not
20346    refer to types are ignored.
20347
20348    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20349    ignored.
20350
20351    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20352    are ignored.
20353
20354    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20355    types.
20356
20357    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20358    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20359    NULL_TREE otherwise.  */
20360
20361 static tree
20362 cp_parser_lookup_name (cp_parser *parser, tree name,
20363                        enum tag_types tag_type,
20364                        bool is_template,
20365                        bool is_namespace,
20366                        bool check_dependency,
20367                        tree *ambiguous_decls,
20368                        location_t name_location)
20369 {
20370   int flags = 0;
20371   tree decl;
20372   tree object_type = parser->context->object_type;
20373
20374   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20375     flags |= LOOKUP_COMPLAIN;
20376
20377   /* Assume that the lookup will be unambiguous.  */
20378   if (ambiguous_decls)
20379     *ambiguous_decls = NULL_TREE;
20380
20381   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20382      no longer valid.  Note that if we are parsing tentatively, and
20383      the parse fails, OBJECT_TYPE will be automatically restored.  */
20384   parser->context->object_type = NULL_TREE;
20385
20386   if (name == error_mark_node)
20387     return error_mark_node;
20388
20389   /* A template-id has already been resolved; there is no lookup to
20390      do.  */
20391   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20392     return name;
20393   if (BASELINK_P (name))
20394     {
20395       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20396                   == TEMPLATE_ID_EXPR);
20397       return name;
20398     }
20399
20400   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20401      it should already have been checked to make sure that the name
20402      used matches the type being destroyed.  */
20403   if (TREE_CODE (name) == BIT_NOT_EXPR)
20404     {
20405       tree type;
20406
20407       /* Figure out to which type this destructor applies.  */
20408       if (parser->scope)
20409         type = parser->scope;
20410       else if (object_type)
20411         type = object_type;
20412       else
20413         type = current_class_type;
20414       /* If that's not a class type, there is no destructor.  */
20415       if (!type || !CLASS_TYPE_P (type))
20416         return error_mark_node;
20417       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20418         lazily_declare_fn (sfk_destructor, type);
20419       if (!CLASSTYPE_DESTRUCTORS (type))
20420           return error_mark_node;
20421       /* If it was a class type, return the destructor.  */
20422       return CLASSTYPE_DESTRUCTORS (type);
20423     }
20424
20425   /* By this point, the NAME should be an ordinary identifier.  If
20426      the id-expression was a qualified name, the qualifying scope is
20427      stored in PARSER->SCOPE at this point.  */
20428   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20429
20430   /* Perform the lookup.  */
20431   if (parser->scope)
20432     {
20433       bool dependent_p;
20434
20435       if (parser->scope == error_mark_node)
20436         return error_mark_node;
20437
20438       /* If the SCOPE is dependent, the lookup must be deferred until
20439          the template is instantiated -- unless we are explicitly
20440          looking up names in uninstantiated templates.  Even then, we
20441          cannot look up the name if the scope is not a class type; it
20442          might, for example, be a template type parameter.  */
20443       dependent_p = (TYPE_P (parser->scope)
20444                      && dependent_scope_p (parser->scope));
20445       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20446           && dependent_p)
20447         /* Defer lookup.  */
20448         decl = error_mark_node;
20449       else
20450         {
20451           tree pushed_scope = NULL_TREE;
20452
20453           /* If PARSER->SCOPE is a dependent type, then it must be a
20454              class type, and we must not be checking dependencies;
20455              otherwise, we would have processed this lookup above.  So
20456              that PARSER->SCOPE is not considered a dependent base by
20457              lookup_member, we must enter the scope here.  */
20458           if (dependent_p)
20459             pushed_scope = push_scope (parser->scope);
20460
20461           /* If the PARSER->SCOPE is a template specialization, it
20462              may be instantiated during name lookup.  In that case,
20463              errors may be issued.  Even if we rollback the current
20464              tentative parse, those errors are valid.  */
20465           decl = lookup_qualified_name (parser->scope, name,
20466                                         tag_type != none_type,
20467                                         /*complain=*/true);
20468
20469           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20470              lookup result and the nested-name-specifier nominates a class C:
20471                * if the name specified after the nested-name-specifier, when
20472                looked up in C, is the injected-class-name of C (Clause 9), or
20473                * if the name specified after the nested-name-specifier is the
20474                same as the identifier or the simple-template-id's template-
20475                name in the last component of the nested-name-specifier,
20476              the name is instead considered to name the constructor of
20477              class C. [ Note: for example, the constructor is not an
20478              acceptable lookup result in an elaborated-type-specifier so
20479              the constructor would not be used in place of the
20480              injected-class-name. --end note ] Such a constructor name
20481              shall be used only in the declarator-id of a declaration that
20482              names a constructor or in a using-declaration.  */
20483           if (tag_type == none_type
20484               && DECL_SELF_REFERENCE_P (decl)
20485               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20486             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20487                                           tag_type != none_type,
20488                                           /*complain=*/true);
20489
20490           /* If we have a single function from a using decl, pull it out.  */
20491           if (TREE_CODE (decl) == OVERLOAD
20492               && !really_overloaded_fn (decl))
20493             decl = OVL_FUNCTION (decl);
20494
20495           if (pushed_scope)
20496             pop_scope (pushed_scope);
20497         }
20498
20499       /* If the scope is a dependent type and either we deferred lookup or
20500          we did lookup but didn't find the name, rememeber the name.  */
20501       if (decl == error_mark_node && TYPE_P (parser->scope)
20502           && dependent_type_p (parser->scope))
20503         {
20504           if (tag_type)
20505             {
20506               tree type;
20507
20508               /* The resolution to Core Issue 180 says that `struct
20509                  A::B' should be considered a type-name, even if `A'
20510                  is dependent.  */
20511               type = make_typename_type (parser->scope, name, tag_type,
20512                                          /*complain=*/tf_error);
20513               decl = TYPE_NAME (type);
20514             }
20515           else if (is_template
20516                    && (cp_parser_next_token_ends_template_argument_p (parser)
20517                        || cp_lexer_next_token_is (parser->lexer,
20518                                                   CPP_CLOSE_PAREN)))
20519             decl = make_unbound_class_template (parser->scope,
20520                                                 name, NULL_TREE,
20521                                                 /*complain=*/tf_error);
20522           else
20523             decl = build_qualified_name (/*type=*/NULL_TREE,
20524                                          parser->scope, name,
20525                                          is_template);
20526         }
20527       parser->qualifying_scope = parser->scope;
20528       parser->object_scope = NULL_TREE;
20529     }
20530   else if (object_type)
20531     {
20532       tree object_decl = NULL_TREE;
20533       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20534          OBJECT_TYPE is not a class.  */
20535       if (CLASS_TYPE_P (object_type))
20536         /* If the OBJECT_TYPE is a template specialization, it may
20537            be instantiated during name lookup.  In that case, errors
20538            may be issued.  Even if we rollback the current tentative
20539            parse, those errors are valid.  */
20540         object_decl = lookup_member (object_type,
20541                                      name,
20542                                      /*protect=*/0,
20543                                      tag_type != none_type,
20544                                      tf_warning_or_error);
20545       /* Look it up in the enclosing context, too.  */
20546       decl = lookup_name_real (name, tag_type != none_type,
20547                                /*nonclass=*/0,
20548                                /*block_p=*/true, is_namespace, flags);
20549       parser->object_scope = object_type;
20550       parser->qualifying_scope = NULL_TREE;
20551       if (object_decl)
20552         decl = object_decl;
20553     }
20554   else
20555     {
20556       decl = lookup_name_real (name, tag_type != none_type,
20557                                /*nonclass=*/0,
20558                                /*block_p=*/true, is_namespace, flags);
20559       parser->qualifying_scope = NULL_TREE;
20560       parser->object_scope = NULL_TREE;
20561     }
20562
20563   /* If the lookup failed, let our caller know.  */
20564   if (!decl || decl == error_mark_node)
20565     return error_mark_node;
20566
20567   /* Pull out the template from an injected-class-name (or multiple).  */
20568   if (is_template)
20569     decl = maybe_get_template_decl_from_type_decl (decl);
20570
20571   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20572   if (TREE_CODE (decl) == TREE_LIST)
20573     {
20574       if (ambiguous_decls)
20575         *ambiguous_decls = decl;
20576       /* The error message we have to print is too complicated for
20577          cp_parser_error, so we incorporate its actions directly.  */
20578       if (!cp_parser_simulate_error (parser))
20579         {
20580           error_at (name_location, "reference to %qD is ambiguous",
20581                     name);
20582           print_candidates (decl);
20583         }
20584       return error_mark_node;
20585     }
20586
20587   gcc_assert (DECL_P (decl)
20588               || TREE_CODE (decl) == OVERLOAD
20589               || TREE_CODE (decl) == SCOPE_REF
20590               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20591               || BASELINK_P (decl));
20592
20593   /* If we have resolved the name of a member declaration, check to
20594      see if the declaration is accessible.  When the name resolves to
20595      set of overloaded functions, accessibility is checked when
20596      overload resolution is done.
20597
20598      During an explicit instantiation, access is not checked at all,
20599      as per [temp.explicit].  */
20600   if (DECL_P (decl))
20601     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20602
20603   maybe_record_typedef_use (decl);
20604
20605   return decl;
20606 }
20607
20608 /* Like cp_parser_lookup_name, but for use in the typical case where
20609    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20610    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20611
20612 static tree
20613 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20614 {
20615   return cp_parser_lookup_name (parser, name,
20616                                 none_type,
20617                                 /*is_template=*/false,
20618                                 /*is_namespace=*/false,
20619                                 /*check_dependency=*/true,
20620                                 /*ambiguous_decls=*/NULL,
20621                                 location);
20622 }
20623
20624 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20625    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20626    true, the DECL indicates the class being defined in a class-head,
20627    or declared in an elaborated-type-specifier.
20628
20629    Otherwise, return DECL.  */
20630
20631 static tree
20632 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20633 {
20634   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20635      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20636
20637        struct A {
20638          template <typename T> struct B;
20639        };
20640
20641        template <typename T> struct A::B {};
20642
20643      Similarly, in an elaborated-type-specifier:
20644
20645        namespace N { struct X{}; }
20646
20647        struct A {
20648          template <typename T> friend struct N::X;
20649        };
20650
20651      However, if the DECL refers to a class type, and we are in
20652      the scope of the class, then the name lookup automatically
20653      finds the TYPE_DECL created by build_self_reference rather
20654      than a TEMPLATE_DECL.  For example, in:
20655
20656        template <class T> struct S {
20657          S s;
20658        };
20659
20660      there is no need to handle such case.  */
20661
20662   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20663     return DECL_TEMPLATE_RESULT (decl);
20664
20665   return decl;
20666 }
20667
20668 /* If too many, or too few, template-parameter lists apply to the
20669    declarator, issue an error message.  Returns TRUE if all went well,
20670    and FALSE otherwise.  */
20671
20672 static bool
20673 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20674                                                 cp_declarator *declarator,
20675                                                 location_t declarator_location)
20676 {
20677   unsigned num_templates;
20678
20679   /* We haven't seen any classes that involve template parameters yet.  */
20680   num_templates = 0;
20681
20682   switch (declarator->kind)
20683     {
20684     case cdk_id:
20685       if (declarator->u.id.qualifying_scope)
20686         {
20687           tree scope;
20688
20689           scope = declarator->u.id.qualifying_scope;
20690
20691           while (scope && CLASS_TYPE_P (scope))
20692             {
20693               /* You're supposed to have one `template <...>'
20694                  for every template class, but you don't need one
20695                  for a full specialization.  For example:
20696
20697                  template <class T> struct S{};
20698                  template <> struct S<int> { void f(); };
20699                  void S<int>::f () {}
20700
20701                  is correct; there shouldn't be a `template <>' for
20702                  the definition of `S<int>::f'.  */
20703               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20704                 /* If SCOPE does not have template information of any
20705                    kind, then it is not a template, nor is it nested
20706                    within a template.  */
20707                 break;
20708               if (explicit_class_specialization_p (scope))
20709                 break;
20710               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20711                 ++num_templates;
20712
20713               scope = TYPE_CONTEXT (scope);
20714             }
20715         }
20716       else if (TREE_CODE (declarator->u.id.unqualified_name)
20717                == TEMPLATE_ID_EXPR)
20718         /* If the DECLARATOR has the form `X<y>' then it uses one
20719            additional level of template parameters.  */
20720         ++num_templates;
20721
20722       return cp_parser_check_template_parameters 
20723         (parser, num_templates, declarator_location, declarator);
20724
20725
20726     case cdk_function:
20727     case cdk_array:
20728     case cdk_pointer:
20729     case cdk_reference:
20730     case cdk_ptrmem:
20731       return (cp_parser_check_declarator_template_parameters
20732               (parser, declarator->declarator, declarator_location));
20733
20734     case cdk_error:
20735       return true;
20736
20737     default:
20738       gcc_unreachable ();
20739     }
20740   return false;
20741 }
20742
20743 /* NUM_TEMPLATES were used in the current declaration.  If that is
20744    invalid, return FALSE and issue an error messages.  Otherwise,
20745    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20746    declarator and we can print more accurate diagnostics.  */
20747
20748 static bool
20749 cp_parser_check_template_parameters (cp_parser* parser,
20750                                      unsigned num_templates,
20751                                      location_t location,
20752                                      cp_declarator *declarator)
20753 {
20754   /* If there are the same number of template classes and parameter
20755      lists, that's OK.  */
20756   if (parser->num_template_parameter_lists == num_templates)
20757     return true;
20758   /* If there are more, but only one more, then we are referring to a
20759      member template.  That's OK too.  */
20760   if (parser->num_template_parameter_lists == num_templates + 1)
20761     return true;
20762   /* If there are more template classes than parameter lists, we have
20763      something like:
20764
20765        template <class T> void S<T>::R<T>::f ();  */
20766   if (parser->num_template_parameter_lists < num_templates)
20767     {
20768       if (declarator && !current_function_decl)
20769         error_at (location, "specializing member %<%T::%E%> "
20770                   "requires %<template<>%> syntax", 
20771                   declarator->u.id.qualifying_scope,
20772                   declarator->u.id.unqualified_name);
20773       else if (declarator)
20774         error_at (location, "invalid declaration of %<%T::%E%>",
20775                   declarator->u.id.qualifying_scope,
20776                   declarator->u.id.unqualified_name);
20777       else 
20778         error_at (location, "too few template-parameter-lists");
20779       return false;
20780     }
20781   /* Otherwise, there are too many template parameter lists.  We have
20782      something like:
20783
20784      template <class T> template <class U> void S::f();  */
20785   error_at (location, "too many template-parameter-lists");
20786   return false;
20787 }
20788
20789 /* Parse an optional `::' token indicating that the following name is
20790    from the global namespace.  If so, PARSER->SCOPE is set to the
20791    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20792    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20793    Returns the new value of PARSER->SCOPE, if the `::' token is
20794    present, and NULL_TREE otherwise.  */
20795
20796 static tree
20797 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20798 {
20799   cp_token *token;
20800
20801   /* Peek at the next token.  */
20802   token = cp_lexer_peek_token (parser->lexer);
20803   /* If we're looking at a `::' token then we're starting from the
20804      global namespace, not our current location.  */
20805   if (token->type == CPP_SCOPE)
20806     {
20807       /* Consume the `::' token.  */
20808       cp_lexer_consume_token (parser->lexer);
20809       /* Set the SCOPE so that we know where to start the lookup.  */
20810       parser->scope = global_namespace;
20811       parser->qualifying_scope = global_namespace;
20812       parser->object_scope = NULL_TREE;
20813
20814       return parser->scope;
20815     }
20816   else if (!current_scope_valid_p)
20817     {
20818       parser->scope = NULL_TREE;
20819       parser->qualifying_scope = NULL_TREE;
20820       parser->object_scope = NULL_TREE;
20821     }
20822
20823   return NULL_TREE;
20824 }
20825
20826 /* Returns TRUE if the upcoming token sequence is the start of a
20827    constructor declarator.  If FRIEND_P is true, the declarator is
20828    preceded by the `friend' specifier.  */
20829
20830 static bool
20831 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20832 {
20833   bool constructor_p;
20834   tree nested_name_specifier;
20835   cp_token *next_token;
20836
20837   /* The common case is that this is not a constructor declarator, so
20838      try to avoid doing lots of work if at all possible.  It's not
20839      valid declare a constructor at function scope.  */
20840   if (parser->in_function_body)
20841     return false;
20842   /* And only certain tokens can begin a constructor declarator.  */
20843   next_token = cp_lexer_peek_token (parser->lexer);
20844   if (next_token->type != CPP_NAME
20845       && next_token->type != CPP_SCOPE
20846       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20847       && next_token->type != CPP_TEMPLATE_ID)
20848     return false;
20849
20850   /* Parse tentatively; we are going to roll back all of the tokens
20851      consumed here.  */
20852   cp_parser_parse_tentatively (parser);
20853   /* Assume that we are looking at a constructor declarator.  */
20854   constructor_p = true;
20855
20856   /* Look for the optional `::' operator.  */
20857   cp_parser_global_scope_opt (parser,
20858                               /*current_scope_valid_p=*/false);
20859   /* Look for the nested-name-specifier.  */
20860   nested_name_specifier
20861     = (cp_parser_nested_name_specifier_opt (parser,
20862                                             /*typename_keyword_p=*/false,
20863                                             /*check_dependency_p=*/false,
20864                                             /*type_p=*/false,
20865                                             /*is_declaration=*/false));
20866   /* Outside of a class-specifier, there must be a
20867      nested-name-specifier.  */
20868   if (!nested_name_specifier &&
20869       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20870        || friend_p))
20871     constructor_p = false;
20872   else if (nested_name_specifier == error_mark_node)
20873     constructor_p = false;
20874
20875   /* If we have a class scope, this is easy; DR 147 says that S::S always
20876      names the constructor, and no other qualified name could.  */
20877   if (constructor_p && nested_name_specifier
20878       && CLASS_TYPE_P (nested_name_specifier))
20879     {
20880       tree id = cp_parser_unqualified_id (parser,
20881                                           /*template_keyword_p=*/false,
20882                                           /*check_dependency_p=*/false,
20883                                           /*declarator_p=*/true,
20884                                           /*optional_p=*/false);
20885       if (is_overloaded_fn (id))
20886         id = DECL_NAME (get_first_fn (id));
20887       if (!constructor_name_p (id, nested_name_specifier))
20888         constructor_p = false;
20889     }
20890   /* If we still think that this might be a constructor-declarator,
20891      look for a class-name.  */
20892   else if (constructor_p)
20893     {
20894       /* If we have:
20895
20896            template <typename T> struct S {
20897              S();
20898            };
20899
20900          we must recognize that the nested `S' names a class.  */
20901       tree type_decl;
20902       type_decl = cp_parser_class_name (parser,
20903                                         /*typename_keyword_p=*/false,
20904                                         /*template_keyword_p=*/false,
20905                                         none_type,
20906                                         /*check_dependency_p=*/false,
20907                                         /*class_head_p=*/false,
20908                                         /*is_declaration=*/false);
20909       /* If there was no class-name, then this is not a constructor.  */
20910       constructor_p = !cp_parser_error_occurred (parser);
20911
20912       /* If we're still considering a constructor, we have to see a `(',
20913          to begin the parameter-declaration-clause, followed by either a
20914          `)', an `...', or a decl-specifier.  We need to check for a
20915          type-specifier to avoid being fooled into thinking that:
20916
20917            S (f) (int);
20918
20919          is a constructor.  (It is actually a function named `f' that
20920          takes one parameter (of type `int') and returns a value of type
20921          `S'.  */
20922       if (constructor_p
20923           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20924         constructor_p = false;
20925
20926       if (constructor_p
20927           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20928           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20929           /* A parameter declaration begins with a decl-specifier,
20930              which is either the "attribute" keyword, a storage class
20931              specifier, or (usually) a type-specifier.  */
20932           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20933         {
20934           tree type;
20935           tree pushed_scope = NULL_TREE;
20936           unsigned saved_num_template_parameter_lists;
20937
20938           /* Names appearing in the type-specifier should be looked up
20939              in the scope of the class.  */
20940           if (current_class_type)
20941             type = NULL_TREE;
20942           else
20943             {
20944               type = TREE_TYPE (type_decl);
20945               if (TREE_CODE (type) == TYPENAME_TYPE)
20946                 {
20947                   type = resolve_typename_type (type,
20948                                                 /*only_current_p=*/false);
20949                   if (TREE_CODE (type) == TYPENAME_TYPE)
20950                     {
20951                       cp_parser_abort_tentative_parse (parser);
20952                       return false;
20953                     }
20954                 }
20955               pushed_scope = push_scope (type);
20956             }
20957
20958           /* Inside the constructor parameter list, surrounding
20959              template-parameter-lists do not apply.  */
20960           saved_num_template_parameter_lists
20961             = parser->num_template_parameter_lists;
20962           parser->num_template_parameter_lists = 0;
20963
20964           /* Look for the type-specifier.  */
20965           cp_parser_type_specifier (parser,
20966                                     CP_PARSER_FLAGS_NONE,
20967                                     /*decl_specs=*/NULL,
20968                                     /*is_declarator=*/true,
20969                                     /*declares_class_or_enum=*/NULL,
20970                                     /*is_cv_qualifier=*/NULL);
20971
20972           parser->num_template_parameter_lists
20973             = saved_num_template_parameter_lists;
20974
20975           /* Leave the scope of the class.  */
20976           if (pushed_scope)
20977             pop_scope (pushed_scope);
20978
20979           constructor_p = !cp_parser_error_occurred (parser);
20980         }
20981     }
20982
20983   /* We did not really want to consume any tokens.  */
20984   cp_parser_abort_tentative_parse (parser);
20985
20986   return constructor_p;
20987 }
20988
20989 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20990    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20991    they must be performed once we are in the scope of the function.
20992
20993    Returns the function defined.  */
20994
20995 static tree
20996 cp_parser_function_definition_from_specifiers_and_declarator
20997   (cp_parser* parser,
20998    cp_decl_specifier_seq *decl_specifiers,
20999    tree attributes,
21000    const cp_declarator *declarator)
21001 {
21002   tree fn;
21003   bool success_p;
21004
21005   /* Begin the function-definition.  */
21006   success_p = start_function (decl_specifiers, declarator, attributes);
21007
21008   /* The things we're about to see are not directly qualified by any
21009      template headers we've seen thus far.  */
21010   reset_specialization ();
21011
21012   /* If there were names looked up in the decl-specifier-seq that we
21013      did not check, check them now.  We must wait until we are in the
21014      scope of the function to perform the checks, since the function
21015      might be a friend.  */
21016   perform_deferred_access_checks ();
21017
21018   if (!success_p)
21019     {
21020       /* Skip the entire function.  */
21021       cp_parser_skip_to_end_of_block_or_statement (parser);
21022       fn = error_mark_node;
21023     }
21024   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21025     {
21026       /* Seen already, skip it.  An error message has already been output.  */
21027       cp_parser_skip_to_end_of_block_or_statement (parser);
21028       fn = current_function_decl;
21029       current_function_decl = NULL_TREE;
21030       /* If this is a function from a class, pop the nested class.  */
21031       if (current_class_name)
21032         pop_nested_class ();
21033     }
21034   else
21035     {
21036       timevar_id_t tv;
21037       if (DECL_DECLARED_INLINE_P (current_function_decl))
21038         tv = TV_PARSE_INLINE;
21039       else
21040         tv = TV_PARSE_FUNC;
21041       timevar_push (tv);
21042       fn = cp_parser_function_definition_after_declarator (parser,
21043                                                          /*inline_p=*/false);
21044       timevar_pop (tv);
21045     }
21046
21047   return fn;
21048 }
21049
21050 /* Parse the part of a function-definition that follows the
21051    declarator.  INLINE_P is TRUE iff this function is an inline
21052    function defined within a class-specifier.
21053
21054    Returns the function defined.  */
21055
21056 static tree
21057 cp_parser_function_definition_after_declarator (cp_parser* parser,
21058                                                 bool inline_p)
21059 {
21060   tree fn;
21061   bool ctor_initializer_p = false;
21062   bool saved_in_unbraced_linkage_specification_p;
21063   bool saved_in_function_body;
21064   unsigned saved_num_template_parameter_lists;
21065   cp_token *token;
21066
21067   saved_in_function_body = parser->in_function_body;
21068   parser->in_function_body = true;
21069   /* If the next token is `return', then the code may be trying to
21070      make use of the "named return value" extension that G++ used to
21071      support.  */
21072   token = cp_lexer_peek_token (parser->lexer);
21073   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21074     {
21075       /* Consume the `return' keyword.  */
21076       cp_lexer_consume_token (parser->lexer);
21077       /* Look for the identifier that indicates what value is to be
21078          returned.  */
21079       cp_parser_identifier (parser);
21080       /* Issue an error message.  */
21081       error_at (token->location,
21082                 "named return values are no longer supported");
21083       /* Skip tokens until we reach the start of the function body.  */
21084       while (true)
21085         {
21086           cp_token *token = cp_lexer_peek_token (parser->lexer);
21087           if (token->type == CPP_OPEN_BRACE
21088               || token->type == CPP_EOF
21089               || token->type == CPP_PRAGMA_EOL)
21090             break;
21091           cp_lexer_consume_token (parser->lexer);
21092         }
21093     }
21094   /* The `extern' in `extern "C" void f () { ... }' does not apply to
21095      anything declared inside `f'.  */
21096   saved_in_unbraced_linkage_specification_p
21097     = parser->in_unbraced_linkage_specification_p;
21098   parser->in_unbraced_linkage_specification_p = false;
21099   /* Inside the function, surrounding template-parameter-lists do not
21100      apply.  */
21101   saved_num_template_parameter_lists
21102     = parser->num_template_parameter_lists;
21103   parser->num_template_parameter_lists = 0;
21104
21105   start_lambda_scope (current_function_decl);
21106
21107   /* If the next token is `try', `__transaction_atomic', or
21108      `__transaction_relaxed`, then we are looking at either function-try-block
21109      or function-transaction-block.  Note that all of these include the
21110      function-body.  */
21111   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21112     ctor_initializer_p = cp_parser_function_transaction (parser,
21113         RID_TRANSACTION_ATOMIC);
21114   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21115       RID_TRANSACTION_RELAXED))
21116     ctor_initializer_p = cp_parser_function_transaction (parser,
21117         RID_TRANSACTION_RELAXED);
21118   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21119     ctor_initializer_p = cp_parser_function_try_block (parser);
21120   else
21121     ctor_initializer_p
21122       = cp_parser_ctor_initializer_opt_and_function_body (parser);
21123
21124   finish_lambda_scope ();
21125
21126   /* Finish the function.  */
21127   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21128                         (inline_p ? 2 : 0));
21129   /* Generate code for it, if necessary.  */
21130   expand_or_defer_fn (fn);
21131   /* Restore the saved values.  */
21132   parser->in_unbraced_linkage_specification_p
21133     = saved_in_unbraced_linkage_specification_p;
21134   parser->num_template_parameter_lists
21135     = saved_num_template_parameter_lists;
21136   parser->in_function_body = saved_in_function_body;
21137
21138   return fn;
21139 }
21140
21141 /* Parse a template-declaration, assuming that the `export' (and
21142    `extern') keywords, if present, has already been scanned.  MEMBER_P
21143    is as for cp_parser_template_declaration.  */
21144
21145 static void
21146 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21147 {
21148   tree decl = NULL_TREE;
21149   VEC (deferred_access_check,gc) *checks;
21150   tree parameter_list;
21151   bool friend_p = false;
21152   bool need_lang_pop;
21153   cp_token *token;
21154
21155   /* Look for the `template' keyword.  */
21156   token = cp_lexer_peek_token (parser->lexer);
21157   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21158     return;
21159
21160   /* And the `<'.  */
21161   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21162     return;
21163   if (at_class_scope_p () && current_function_decl)
21164     {
21165       /* 14.5.2.2 [temp.mem]
21166
21167          A local class shall not have member templates.  */
21168       error_at (token->location,
21169                 "invalid declaration of member template in local class");
21170       cp_parser_skip_to_end_of_block_or_statement (parser);
21171       return;
21172     }
21173   /* [temp]
21174
21175      A template ... shall not have C linkage.  */
21176   if (current_lang_name == lang_name_c)
21177     {
21178       error_at (token->location, "template with C linkage");
21179       /* Give it C++ linkage to avoid confusing other parts of the
21180          front end.  */
21181       push_lang_context (lang_name_cplusplus);
21182       need_lang_pop = true;
21183     }
21184   else
21185     need_lang_pop = false;
21186
21187   /* We cannot perform access checks on the template parameter
21188      declarations until we know what is being declared, just as we
21189      cannot check the decl-specifier list.  */
21190   push_deferring_access_checks (dk_deferred);
21191
21192   /* If the next token is `>', then we have an invalid
21193      specialization.  Rather than complain about an invalid template
21194      parameter, issue an error message here.  */
21195   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21196     {
21197       cp_parser_error (parser, "invalid explicit specialization");
21198       begin_specialization ();
21199       parameter_list = NULL_TREE;
21200     }
21201   else
21202     {
21203       /* Parse the template parameters.  */
21204       parameter_list = cp_parser_template_parameter_list (parser);
21205       fixup_template_parms ();
21206     }
21207
21208   /* Get the deferred access checks from the parameter list.  These
21209      will be checked once we know what is being declared, as for a
21210      member template the checks must be performed in the scope of the
21211      class containing the member.  */
21212   checks = get_deferred_access_checks ();
21213
21214   /* Look for the `>'.  */
21215   cp_parser_skip_to_end_of_template_parameter_list (parser);
21216   /* We just processed one more parameter list.  */
21217   ++parser->num_template_parameter_lists;
21218   /* If the next token is `template', there are more template
21219      parameters.  */
21220   if (cp_lexer_next_token_is_keyword (parser->lexer,
21221                                       RID_TEMPLATE))
21222     cp_parser_template_declaration_after_export (parser, member_p);
21223   else if (cxx_dialect >= cxx0x
21224            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21225     decl = cp_parser_alias_declaration (parser);
21226   else
21227     {
21228       /* There are no access checks when parsing a template, as we do not
21229          know if a specialization will be a friend.  */
21230       push_deferring_access_checks (dk_no_check);
21231       token = cp_lexer_peek_token (parser->lexer);
21232       decl = cp_parser_single_declaration (parser,
21233                                            checks,
21234                                            member_p,
21235                                            /*explicit_specialization_p=*/false,
21236                                            &friend_p);
21237       pop_deferring_access_checks ();
21238
21239       /* If this is a member template declaration, let the front
21240          end know.  */
21241       if (member_p && !friend_p && decl)
21242         {
21243           if (TREE_CODE (decl) == TYPE_DECL)
21244             cp_parser_check_access_in_redeclaration (decl, token->location);
21245
21246           decl = finish_member_template_decl (decl);
21247         }
21248       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21249         make_friend_class (current_class_type, TREE_TYPE (decl),
21250                            /*complain=*/true);
21251     }
21252   /* We are done with the current parameter list.  */
21253   --parser->num_template_parameter_lists;
21254
21255   pop_deferring_access_checks ();
21256
21257   /* Finish up.  */
21258   finish_template_decl (parameter_list);
21259
21260   /* Check the template arguments for a literal operator template.  */
21261   if (decl
21262       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21263       && UDLIT_OPER_P (DECL_NAME (decl)))
21264     {
21265       bool ok = true;
21266       if (parameter_list == NULL_TREE)
21267         ok = false;
21268       else
21269         {
21270           int num_parms = TREE_VEC_LENGTH (parameter_list);
21271           if (num_parms != 1)
21272             ok = false;
21273           else
21274             {
21275               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21276               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21277               if (TREE_TYPE (parm) != char_type_node
21278                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21279                 ok = false;
21280             }
21281         }
21282       if (!ok)
21283         error ("literal operator template %qD has invalid parameter list."
21284                "  Expected non-type template argument pack <char...>",
21285                decl);
21286     }
21287   /* Register member declarations.  */
21288   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21289     finish_member_declaration (decl);
21290   /* For the erroneous case of a template with C linkage, we pushed an
21291      implicit C++ linkage scope; exit that scope now.  */
21292   if (need_lang_pop)
21293     pop_lang_context ();
21294   /* If DECL is a function template, we must return to parse it later.
21295      (Even though there is no definition, there might be default
21296      arguments that need handling.)  */
21297   if (member_p && decl
21298       && (TREE_CODE (decl) == FUNCTION_DECL
21299           || DECL_FUNCTION_TEMPLATE_P (decl)))
21300     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21301 }
21302
21303 /* Perform the deferred access checks from a template-parameter-list.
21304    CHECKS is a TREE_LIST of access checks, as returned by
21305    get_deferred_access_checks.  */
21306
21307 static void
21308 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21309 {
21310   ++processing_template_parmlist;
21311   perform_access_checks (checks);
21312   --processing_template_parmlist;
21313 }
21314
21315 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21316    `function-definition' sequence.  MEMBER_P is true, this declaration
21317    appears in a class scope.
21318
21319    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21320    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21321
21322 static tree
21323 cp_parser_single_declaration (cp_parser* parser,
21324                               VEC (deferred_access_check,gc)* checks,
21325                               bool member_p,
21326                               bool explicit_specialization_p,
21327                               bool* friend_p)
21328 {
21329   int declares_class_or_enum;
21330   tree decl = NULL_TREE;
21331   cp_decl_specifier_seq decl_specifiers;
21332   bool function_definition_p = false;
21333   cp_token *decl_spec_token_start;
21334
21335   /* This function is only used when processing a template
21336      declaration.  */
21337   gcc_assert (innermost_scope_kind () == sk_template_parms
21338               || innermost_scope_kind () == sk_template_spec);
21339
21340   /* Defer access checks until we know what is being declared.  */
21341   push_deferring_access_checks (dk_deferred);
21342
21343   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21344      alternative.  */
21345   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21346   cp_parser_decl_specifier_seq (parser,
21347                                 CP_PARSER_FLAGS_OPTIONAL,
21348                                 &decl_specifiers,
21349                                 &declares_class_or_enum);
21350   if (friend_p)
21351     *friend_p = cp_parser_friend_p (&decl_specifiers);
21352
21353   /* There are no template typedefs.  */
21354   if (decl_specifiers.specs[(int) ds_typedef])
21355     {
21356       error_at (decl_spec_token_start->location,
21357                 "template declaration of %<typedef%>");
21358       decl = error_mark_node;
21359     }
21360
21361   /* Gather up the access checks that occurred the
21362      decl-specifier-seq.  */
21363   stop_deferring_access_checks ();
21364
21365   /* Check for the declaration of a template class.  */
21366   if (declares_class_or_enum)
21367     {
21368       if (cp_parser_declares_only_class_p (parser))
21369         {
21370           decl = shadow_tag (&decl_specifiers);
21371
21372           /* In this case:
21373
21374                struct C {
21375                  friend template <typename T> struct A<T>::B;
21376                };
21377
21378              A<T>::B will be represented by a TYPENAME_TYPE, and
21379              therefore not recognized by shadow_tag.  */
21380           if (friend_p && *friend_p
21381               && !decl
21382               && decl_specifiers.type
21383               && TYPE_P (decl_specifiers.type))
21384             decl = decl_specifiers.type;
21385
21386           if (decl && decl != error_mark_node)
21387             decl = TYPE_NAME (decl);
21388           else
21389             decl = error_mark_node;
21390
21391           /* Perform access checks for template parameters.  */
21392           cp_parser_perform_template_parameter_access_checks (checks);
21393         }
21394     }
21395
21396   /* Complain about missing 'typename' or other invalid type names.  */
21397   if (!decl_specifiers.any_type_specifiers_p
21398       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21399     {
21400       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21401          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21402          the rest of this declaration.  */
21403       decl = error_mark_node;
21404       goto out;
21405     }
21406
21407   /* If it's not a template class, try for a template function.  If
21408      the next token is a `;', then this declaration does not declare
21409      anything.  But, if there were errors in the decl-specifiers, then
21410      the error might well have come from an attempted class-specifier.
21411      In that case, there's no need to warn about a missing declarator.  */
21412   if (!decl
21413       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21414           || decl_specifiers.type != error_mark_node))
21415     {
21416       decl = cp_parser_init_declarator (parser,
21417                                         &decl_specifiers,
21418                                         checks,
21419                                         /*function_definition_allowed_p=*/true,
21420                                         member_p,
21421                                         declares_class_or_enum,
21422                                         &function_definition_p,
21423                                         NULL);
21424
21425     /* 7.1.1-1 [dcl.stc]
21426
21427        A storage-class-specifier shall not be specified in an explicit
21428        specialization...  */
21429     if (decl
21430         && explicit_specialization_p
21431         && decl_specifiers.storage_class != sc_none)
21432       {
21433         error_at (decl_spec_token_start->location,
21434                   "explicit template specialization cannot have a storage class");
21435         decl = error_mark_node;
21436       }
21437     }
21438
21439   /* Look for a trailing `;' after the declaration.  */
21440   if (!function_definition_p
21441       && (decl == error_mark_node
21442           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21443     cp_parser_skip_to_end_of_block_or_statement (parser);
21444
21445  out:
21446   pop_deferring_access_checks ();
21447
21448   /* Clear any current qualification; whatever comes next is the start
21449      of something new.  */
21450   parser->scope = NULL_TREE;
21451   parser->qualifying_scope = NULL_TREE;
21452   parser->object_scope = NULL_TREE;
21453
21454   return decl;
21455 }
21456
21457 /* Parse a cast-expression that is not the operand of a unary "&".  */
21458
21459 static tree
21460 cp_parser_simple_cast_expression (cp_parser *parser)
21461 {
21462   return cp_parser_cast_expression (parser, /*address_p=*/false,
21463                                     /*cast_p=*/false, NULL);
21464 }
21465
21466 /* Parse a functional cast to TYPE.  Returns an expression
21467    representing the cast.  */
21468
21469 static tree
21470 cp_parser_functional_cast (cp_parser* parser, tree type)
21471 {
21472   VEC(tree,gc) *vec;
21473   tree expression_list;
21474   tree cast;
21475   bool nonconst_p;
21476
21477   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21478     {
21479       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21480       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21481       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21482       if (TREE_CODE (type) == TYPE_DECL)
21483         type = TREE_TYPE (type);
21484       return finish_compound_literal (type, expression_list,
21485                                       tf_warning_or_error);
21486     }
21487
21488
21489   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21490                                                  /*cast_p=*/true,
21491                                                  /*allow_expansion_p=*/true,
21492                                                  /*non_constant_p=*/NULL);
21493   if (vec == NULL)
21494     expression_list = error_mark_node;
21495   else
21496     {
21497       expression_list = build_tree_list_vec (vec);
21498       release_tree_vector (vec);
21499     }
21500
21501   cast = build_functional_cast (type, expression_list,
21502                                 tf_warning_or_error);
21503   /* [expr.const]/1: In an integral constant expression "only type
21504      conversions to integral or enumeration type can be used".  */
21505   if (TREE_CODE (type) == TYPE_DECL)
21506     type = TREE_TYPE (type);
21507   if (cast != error_mark_node
21508       && !cast_valid_in_integral_constant_expression_p (type)
21509       && cp_parser_non_integral_constant_expression (parser,
21510                                                      NIC_CONSTRUCTOR))
21511     return error_mark_node;
21512   return cast;
21513 }
21514
21515 /* Save the tokens that make up the body of a member function defined
21516    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21517    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21518    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21519    for the member function.  */
21520
21521 static tree
21522 cp_parser_save_member_function_body (cp_parser* parser,
21523                                      cp_decl_specifier_seq *decl_specifiers,
21524                                      cp_declarator *declarator,
21525                                      tree attributes)
21526 {
21527   cp_token *first;
21528   cp_token *last;
21529   tree fn;
21530
21531   /* Create the FUNCTION_DECL.  */
21532   fn = grokmethod (decl_specifiers, declarator, attributes);
21533   /* If something went badly wrong, bail out now.  */
21534   if (fn == error_mark_node)
21535     {
21536       /* If there's a function-body, skip it.  */
21537       if (cp_parser_token_starts_function_definition_p
21538           (cp_lexer_peek_token (parser->lexer)))
21539         cp_parser_skip_to_end_of_block_or_statement (parser);
21540       return error_mark_node;
21541     }
21542
21543   /* Remember it, if there default args to post process.  */
21544   cp_parser_save_default_args (parser, fn);
21545
21546   /* Save away the tokens that make up the body of the
21547      function.  */
21548   first = parser->lexer->next_token;
21549   /* We can have braced-init-list mem-initializers before the fn body.  */
21550   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21551     {
21552       cp_lexer_consume_token (parser->lexer);
21553       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21554              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21555         {
21556           /* cache_group will stop after an un-nested { } pair, too.  */
21557           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21558             break;
21559
21560           /* variadic mem-inits have ... after the ')'.  */
21561           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21562             cp_lexer_consume_token (parser->lexer);
21563         }
21564     }
21565   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21566   /* Handle function try blocks.  */
21567   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21568     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21569   last = parser->lexer->next_token;
21570
21571   /* Save away the inline definition; we will process it when the
21572      class is complete.  */
21573   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21574   DECL_PENDING_INLINE_P (fn) = 1;
21575
21576   /* We need to know that this was defined in the class, so that
21577      friend templates are handled correctly.  */
21578   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21579
21580   /* Add FN to the queue of functions to be parsed later.  */
21581   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21582
21583   return fn;
21584 }
21585
21586 /* Save the tokens that make up the in-class initializer for a non-static
21587    data member.  Returns a DEFAULT_ARG.  */
21588
21589 static tree
21590 cp_parser_save_nsdmi (cp_parser* parser)
21591 {
21592   /* Save away the tokens that make up the body of the
21593      function.  */
21594   cp_token *first = parser->lexer->next_token;
21595   cp_token *last;
21596   tree node;
21597
21598   /* Save tokens until the next comma or semicolon.  */
21599   cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21600
21601   last = parser->lexer->next_token;
21602
21603   node = make_node (DEFAULT_ARG);
21604   DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21605   DEFARG_INSTANTIATIONS (node) = NULL;
21606
21607   return node;
21608 }
21609
21610
21611 /* Parse a template-argument-list, as well as the trailing ">" (but
21612    not the opening "<").  See cp_parser_template_argument_list for the
21613    return value.  */
21614
21615 static tree
21616 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21617 {
21618   tree arguments;
21619   tree saved_scope;
21620   tree saved_qualifying_scope;
21621   tree saved_object_scope;
21622   bool saved_greater_than_is_operator_p;
21623   int saved_unevaluated_operand;
21624   int saved_inhibit_evaluation_warnings;
21625
21626   /* [temp.names]
21627
21628      When parsing a template-id, the first non-nested `>' is taken as
21629      the end of the template-argument-list rather than a greater-than
21630      operator.  */
21631   saved_greater_than_is_operator_p
21632     = parser->greater_than_is_operator_p;
21633   parser->greater_than_is_operator_p = false;
21634   /* Parsing the argument list may modify SCOPE, so we save it
21635      here.  */
21636   saved_scope = parser->scope;
21637   saved_qualifying_scope = parser->qualifying_scope;
21638   saved_object_scope = parser->object_scope;
21639   /* We need to evaluate the template arguments, even though this
21640      template-id may be nested within a "sizeof".  */
21641   saved_unevaluated_operand = cp_unevaluated_operand;
21642   cp_unevaluated_operand = 0;
21643   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21644   c_inhibit_evaluation_warnings = 0;
21645   /* Parse the template-argument-list itself.  */
21646   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21647       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21648     arguments = NULL_TREE;
21649   else
21650     arguments = cp_parser_template_argument_list (parser);
21651   /* Look for the `>' that ends the template-argument-list. If we find
21652      a '>>' instead, it's probably just a typo.  */
21653   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21654     {
21655       if (cxx_dialect != cxx98)
21656         {
21657           /* In C++0x, a `>>' in a template argument list or cast
21658              expression is considered to be two separate `>'
21659              tokens. So, change the current token to a `>', but don't
21660              consume it: it will be consumed later when the outer
21661              template argument list (or cast expression) is parsed.
21662              Note that this replacement of `>' for `>>' is necessary
21663              even if we are parsing tentatively: in the tentative
21664              case, after calling
21665              cp_parser_enclosed_template_argument_list we will always
21666              throw away all of the template arguments and the first
21667              closing `>', either because the template argument list
21668              was erroneous or because we are replacing those tokens
21669              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21670              not have been thrown away) is needed either to close an
21671              outer template argument list or to complete a new-style
21672              cast.  */
21673           cp_token *token = cp_lexer_peek_token (parser->lexer);
21674           token->type = CPP_GREATER;
21675         }
21676       else if (!saved_greater_than_is_operator_p)
21677         {
21678           /* If we're in a nested template argument list, the '>>' has
21679             to be a typo for '> >'. We emit the error message, but we
21680             continue parsing and we push a '>' as next token, so that
21681             the argument list will be parsed correctly.  Note that the
21682             global source location is still on the token before the
21683             '>>', so we need to say explicitly where we want it.  */
21684           cp_token *token = cp_lexer_peek_token (parser->lexer);
21685           error_at (token->location, "%<>>%> should be %<> >%> "
21686                     "within a nested template argument list");
21687
21688           token->type = CPP_GREATER;
21689         }
21690       else
21691         {
21692           /* If this is not a nested template argument list, the '>>'
21693             is a typo for '>'. Emit an error message and continue.
21694             Same deal about the token location, but here we can get it
21695             right by consuming the '>>' before issuing the diagnostic.  */
21696           cp_token *token = cp_lexer_consume_token (parser->lexer);
21697           error_at (token->location,
21698                     "spurious %<>>%>, use %<>%> to terminate "
21699                     "a template argument list");
21700         }
21701     }
21702   else
21703     cp_parser_skip_to_end_of_template_parameter_list (parser);
21704   /* The `>' token might be a greater-than operator again now.  */
21705   parser->greater_than_is_operator_p
21706     = saved_greater_than_is_operator_p;
21707   /* Restore the SAVED_SCOPE.  */
21708   parser->scope = saved_scope;
21709   parser->qualifying_scope = saved_qualifying_scope;
21710   parser->object_scope = saved_object_scope;
21711   cp_unevaluated_operand = saved_unevaluated_operand;
21712   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21713
21714   return arguments;
21715 }
21716
21717 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21718    arguments, or the body of the function have not yet been parsed,
21719    parse them now.  */
21720
21721 static void
21722 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21723 {
21724   timevar_push (TV_PARSE_INMETH);
21725   /* If this member is a template, get the underlying
21726      FUNCTION_DECL.  */
21727   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21728     member_function = DECL_TEMPLATE_RESULT (member_function);
21729
21730   /* There should not be any class definitions in progress at this
21731      point; the bodies of members are only parsed outside of all class
21732      definitions.  */
21733   gcc_assert (parser->num_classes_being_defined == 0);
21734   /* While we're parsing the member functions we might encounter more
21735      classes.  We want to handle them right away, but we don't want
21736      them getting mixed up with functions that are currently in the
21737      queue.  */
21738   push_unparsed_function_queues (parser);
21739
21740   /* Make sure that any template parameters are in scope.  */
21741   maybe_begin_member_template_processing (member_function);
21742
21743   /* If the body of the function has not yet been parsed, parse it
21744      now.  */
21745   if (DECL_PENDING_INLINE_P (member_function))
21746     {
21747       tree function_scope;
21748       cp_token_cache *tokens;
21749
21750       /* The function is no longer pending; we are processing it.  */
21751       tokens = DECL_PENDING_INLINE_INFO (member_function);
21752       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21753       DECL_PENDING_INLINE_P (member_function) = 0;
21754
21755       /* If this is a local class, enter the scope of the containing
21756          function.  */
21757       function_scope = current_function_decl;
21758       if (function_scope)
21759         push_function_context ();
21760
21761       /* Push the body of the function onto the lexer stack.  */
21762       cp_parser_push_lexer_for_tokens (parser, tokens);
21763
21764       /* Let the front end know that we going to be defining this
21765          function.  */
21766       start_preparsed_function (member_function, NULL_TREE,
21767                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21768
21769       /* Don't do access checking if it is a templated function.  */
21770       if (processing_template_decl)
21771         push_deferring_access_checks (dk_no_check);
21772
21773       /* Now, parse the body of the function.  */
21774       cp_parser_function_definition_after_declarator (parser,
21775                                                       /*inline_p=*/true);
21776
21777       if (processing_template_decl)
21778         pop_deferring_access_checks ();
21779
21780       /* Leave the scope of the containing function.  */
21781       if (function_scope)
21782         pop_function_context ();
21783       cp_parser_pop_lexer (parser);
21784     }
21785
21786   /* Remove any template parameters from the symbol table.  */
21787   maybe_end_member_template_processing ();
21788
21789   /* Restore the queue.  */
21790   pop_unparsed_function_queues (parser);
21791   timevar_pop (TV_PARSE_INMETH);
21792 }
21793
21794 /* If DECL contains any default args, remember it on the unparsed
21795    functions queue.  */
21796
21797 static void
21798 cp_parser_save_default_args (cp_parser* parser, tree decl)
21799 {
21800   tree probe;
21801
21802   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21803        probe;
21804        probe = TREE_CHAIN (probe))
21805     if (TREE_PURPOSE (probe))
21806       {
21807         cp_default_arg_entry *entry
21808           = VEC_safe_push (cp_default_arg_entry, gc,
21809                            unparsed_funs_with_default_args, NULL);
21810         entry->class_type = current_class_type;
21811         entry->decl = decl;
21812         break;
21813       }
21814 }
21815
21816 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21817    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21818    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21819    from the parameter-type-list.  */
21820
21821 static tree
21822 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21823                                       tree default_arg, tree parmtype)
21824 {
21825   cp_token_cache *tokens;
21826   tree parsed_arg;
21827   bool dummy;
21828
21829   /* Push the saved tokens for the default argument onto the parser's
21830      lexer stack.  */
21831   tokens = DEFARG_TOKENS (default_arg);
21832   cp_parser_push_lexer_for_tokens (parser, tokens);
21833
21834   start_lambda_scope (decl);
21835
21836   /* Parse the default argument.  */
21837   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21838   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21839     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21840
21841   finish_lambda_scope ();
21842
21843   if (!processing_template_decl)
21844     {
21845       /* In a non-template class, check conversions now.  In a template,
21846          we'll wait and instantiate these as needed.  */
21847       if (TREE_CODE (decl) == PARM_DECL)
21848         parsed_arg = check_default_argument (parmtype, parsed_arg);
21849       else
21850         {
21851           int flags = LOOKUP_IMPLICIT;
21852           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21853               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21854             flags = LOOKUP_NORMAL;
21855           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21856         }
21857     }
21858
21859   /* If the token stream has not been completely used up, then
21860      there was extra junk after the end of the default
21861      argument.  */
21862   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21863     {
21864       if (TREE_CODE (decl) == PARM_DECL)
21865         cp_parser_error (parser, "expected %<,%>");
21866       else
21867         cp_parser_error (parser, "expected %<;%>");
21868     }
21869
21870   /* Revert to the main lexer.  */
21871   cp_parser_pop_lexer (parser);
21872
21873   return parsed_arg;
21874 }
21875
21876 /* FIELD is a non-static data member with an initializer which we saved for
21877    later; parse it now.  */
21878
21879 static void
21880 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21881 {
21882   tree def;
21883
21884   push_unparsed_function_queues (parser);
21885   def = cp_parser_late_parse_one_default_arg (parser, field,
21886                                               DECL_INITIAL (field),
21887                                               NULL_TREE);
21888   pop_unparsed_function_queues (parser);
21889
21890   DECL_INITIAL (field) = def;
21891 }
21892
21893 /* FN is a FUNCTION_DECL which may contains a parameter with an
21894    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21895    assumes that the current scope is the scope in which the default
21896    argument should be processed.  */
21897
21898 static void
21899 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21900 {
21901   bool saved_local_variables_forbidden_p;
21902   tree parm, parmdecl;
21903
21904   /* While we're parsing the default args, we might (due to the
21905      statement expression extension) encounter more classes.  We want
21906      to handle them right away, but we don't want them getting mixed
21907      up with default args that are currently in the queue.  */
21908   push_unparsed_function_queues (parser);
21909
21910   /* Local variable names (and the `this' keyword) may not appear
21911      in a default argument.  */
21912   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21913   parser->local_variables_forbidden_p = true;
21914
21915   push_defarg_context (fn);
21916
21917   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21918          parmdecl = DECL_ARGUMENTS (fn);
21919        parm && parm != void_list_node;
21920        parm = TREE_CHAIN (parm),
21921          parmdecl = DECL_CHAIN (parmdecl))
21922     {
21923       tree default_arg = TREE_PURPOSE (parm);
21924       tree parsed_arg;
21925       VEC(tree,gc) *insts;
21926       tree copy;
21927       unsigned ix;
21928
21929       if (!default_arg)
21930         continue;
21931
21932       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21933         /* This can happen for a friend declaration for a function
21934            already declared with default arguments.  */
21935         continue;
21936
21937       parsed_arg
21938         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21939                                                 default_arg,
21940                                                 TREE_VALUE (parm));
21941       if (parsed_arg == error_mark_node)
21942         {
21943           continue;
21944         }
21945
21946       TREE_PURPOSE (parm) = parsed_arg;
21947
21948       /* Update any instantiations we've already created.  */
21949       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21950            VEC_iterate (tree, insts, ix, copy); ix++)
21951         TREE_PURPOSE (copy) = parsed_arg;
21952     }
21953
21954   pop_defarg_context ();
21955
21956   /* Make sure no default arg is missing.  */
21957   check_default_args (fn);
21958
21959   /* Restore the state of local_variables_forbidden_p.  */
21960   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21961
21962   /* Restore the queue.  */
21963   pop_unparsed_function_queues (parser);
21964 }
21965
21966 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21967    either a TYPE or an expression, depending on the form of the
21968    input.  The KEYWORD indicates which kind of expression we have
21969    encountered.  */
21970
21971 static tree
21972 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21973 {
21974   tree expr = NULL_TREE;
21975   const char *saved_message;
21976   char *tmp;
21977   bool saved_integral_constant_expression_p;
21978   bool saved_non_integral_constant_expression_p;
21979   bool pack_expansion_p = false;
21980
21981   /* Types cannot be defined in a `sizeof' expression.  Save away the
21982      old message.  */
21983   saved_message = parser->type_definition_forbidden_message;
21984   /* And create the new one.  */
21985   tmp = concat ("types may not be defined in %<",
21986                 IDENTIFIER_POINTER (ridpointers[keyword]),
21987                 "%> expressions", NULL);
21988   parser->type_definition_forbidden_message = tmp;
21989
21990   /* The restrictions on constant-expressions do not apply inside
21991      sizeof expressions.  */
21992   saved_integral_constant_expression_p
21993     = parser->integral_constant_expression_p;
21994   saved_non_integral_constant_expression_p
21995     = parser->non_integral_constant_expression_p;
21996   parser->integral_constant_expression_p = false;
21997
21998   /* If it's a `...', then we are computing the length of a parameter
21999      pack.  */
22000   if (keyword == RID_SIZEOF
22001       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22002     {
22003       /* Consume the `...'.  */
22004       cp_lexer_consume_token (parser->lexer);
22005       maybe_warn_variadic_templates ();
22006
22007       /* Note that this is an expansion.  */
22008       pack_expansion_p = true;
22009     }
22010
22011   /* Do not actually evaluate the expression.  */
22012   ++cp_unevaluated_operand;
22013   ++c_inhibit_evaluation_warnings;
22014   /* If it's a `(', then we might be looking at the type-id
22015      construction.  */
22016   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22017     {
22018       tree type;
22019       bool saved_in_type_id_in_expr_p;
22020
22021       /* We can't be sure yet whether we're looking at a type-id or an
22022          expression.  */
22023       cp_parser_parse_tentatively (parser);
22024       /* Consume the `('.  */
22025       cp_lexer_consume_token (parser->lexer);
22026       /* Parse the type-id.  */
22027       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22028       parser->in_type_id_in_expr_p = true;
22029       type = cp_parser_type_id (parser);
22030       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22031       /* Now, look for the trailing `)'.  */
22032       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22033       /* If all went well, then we're done.  */
22034       if (cp_parser_parse_definitely (parser))
22035         {
22036           cp_decl_specifier_seq decl_specs;
22037
22038           /* Build a trivial decl-specifier-seq.  */
22039           clear_decl_specs (&decl_specs);
22040           decl_specs.type = type;
22041
22042           /* Call grokdeclarator to figure out what type this is.  */
22043           expr = grokdeclarator (NULL,
22044                                  &decl_specs,
22045                                  TYPENAME,
22046                                  /*initialized=*/0,
22047                                  /*attrlist=*/NULL);
22048         }
22049     }
22050
22051   /* If the type-id production did not work out, then we must be
22052      looking at the unary-expression production.  */
22053   if (!expr)
22054     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22055                                        /*cast_p=*/false, NULL);
22056
22057   if (pack_expansion_p)
22058     /* Build a pack expansion. */
22059     expr = make_pack_expansion (expr);
22060
22061   /* Go back to evaluating expressions.  */
22062   --cp_unevaluated_operand;
22063   --c_inhibit_evaluation_warnings;
22064
22065   /* Free the message we created.  */
22066   free (tmp);
22067   /* And restore the old one.  */
22068   parser->type_definition_forbidden_message = saved_message;
22069   parser->integral_constant_expression_p
22070     = saved_integral_constant_expression_p;
22071   parser->non_integral_constant_expression_p
22072     = saved_non_integral_constant_expression_p;
22073
22074   return expr;
22075 }
22076
22077 /* If the current declaration has no declarator, return true.  */
22078
22079 static bool
22080 cp_parser_declares_only_class_p (cp_parser *parser)
22081 {
22082   /* If the next token is a `;' or a `,' then there is no
22083      declarator.  */
22084   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22085           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22086 }
22087
22088 /* Update the DECL_SPECS to reflect the storage class indicated by
22089    KEYWORD.  */
22090
22091 static void
22092 cp_parser_set_storage_class (cp_parser *parser,
22093                              cp_decl_specifier_seq *decl_specs,
22094                              enum rid keyword,
22095                              location_t location)
22096 {
22097   cp_storage_class storage_class;
22098
22099   if (parser->in_unbraced_linkage_specification_p)
22100     {
22101       error_at (location, "invalid use of %qD in linkage specification",
22102                 ridpointers[keyword]);
22103       return;
22104     }
22105   else if (decl_specs->storage_class != sc_none)
22106     {
22107       decl_specs->conflicting_specifiers_p = true;
22108       return;
22109     }
22110
22111   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22112       && decl_specs->specs[(int) ds_thread])
22113     {
22114       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22115       decl_specs->specs[(int) ds_thread] = 0;
22116     }
22117
22118   switch (keyword)
22119     {
22120     case RID_AUTO:
22121       storage_class = sc_auto;
22122       break;
22123     case RID_REGISTER:
22124       storage_class = sc_register;
22125       break;
22126     case RID_STATIC:
22127       storage_class = sc_static;
22128       break;
22129     case RID_EXTERN:
22130       storage_class = sc_extern;
22131       break;
22132     case RID_MUTABLE:
22133       storage_class = sc_mutable;
22134       break;
22135     default:
22136       gcc_unreachable ();
22137     }
22138   decl_specs->storage_class = storage_class;
22139
22140   /* A storage class specifier cannot be applied alongside a typedef 
22141      specifier. If there is a typedef specifier present then set 
22142      conflicting_specifiers_p which will trigger an error later
22143      on in grokdeclarator. */
22144   if (decl_specs->specs[(int)ds_typedef])
22145     decl_specs->conflicting_specifiers_p = true;
22146 }
22147
22148 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22149    is true, the type is a class or enum definition.  */
22150
22151 static void
22152 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22153                               tree type_spec,
22154                               location_t location,
22155                               bool type_definition_p)
22156 {
22157   decl_specs->any_specifiers_p = true;
22158
22159   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22160      (with, for example, in "typedef int wchar_t;") we remember that
22161      this is what happened.  In system headers, we ignore these
22162      declarations so that G++ can work with system headers that are not
22163      C++-safe.  */
22164   if (decl_specs->specs[(int) ds_typedef]
22165       && !type_definition_p
22166       && (type_spec == boolean_type_node
22167           || type_spec == char16_type_node
22168           || type_spec == char32_type_node
22169           || type_spec == wchar_type_node)
22170       && (decl_specs->type
22171           || decl_specs->specs[(int) ds_long]
22172           || decl_specs->specs[(int) ds_short]
22173           || decl_specs->specs[(int) ds_unsigned]
22174           || decl_specs->specs[(int) ds_signed]))
22175     {
22176       decl_specs->redefined_builtin_type = type_spec;
22177       if (!decl_specs->type)
22178         {
22179           decl_specs->type = type_spec;
22180           decl_specs->type_definition_p = false;
22181           decl_specs->type_location = location;
22182         }
22183     }
22184   else if (decl_specs->type)
22185     decl_specs->multiple_types_p = true;
22186   else
22187     {
22188       decl_specs->type = type_spec;
22189       decl_specs->type_definition_p = type_definition_p;
22190       decl_specs->redefined_builtin_type = NULL_TREE;
22191       decl_specs->type_location = location;
22192     }
22193 }
22194
22195 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22196    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22197
22198 static bool
22199 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22200 {
22201   return decl_specifiers->specs[(int) ds_friend] != 0;
22202 }
22203
22204 /* Issue an error message indicating that TOKEN_DESC was expected.
22205    If KEYWORD is true, it indicated this function is called by
22206    cp_parser_require_keword and the required token can only be
22207    a indicated keyword. */
22208
22209 static void
22210 cp_parser_required_error (cp_parser *parser,
22211                           required_token token_desc,
22212                           bool keyword)
22213 {
22214   switch (token_desc)
22215     {
22216       case RT_NEW:
22217         cp_parser_error (parser, "expected %<new%>");
22218         return;
22219       case RT_DELETE:
22220         cp_parser_error (parser, "expected %<delete%>");
22221         return;
22222       case RT_RETURN:
22223         cp_parser_error (parser, "expected %<return%>");
22224         return;
22225       case RT_WHILE:
22226         cp_parser_error (parser, "expected %<while%>");
22227         return;
22228       case RT_EXTERN:
22229         cp_parser_error (parser, "expected %<extern%>");
22230         return;
22231       case RT_STATIC_ASSERT:
22232         cp_parser_error (parser, "expected %<static_assert%>");
22233         return;
22234       case RT_DECLTYPE:
22235         cp_parser_error (parser, "expected %<decltype%>");
22236         return;
22237       case RT_OPERATOR:
22238         cp_parser_error (parser, "expected %<operator%>");
22239         return;
22240       case RT_CLASS:
22241         cp_parser_error (parser, "expected %<class%>");
22242         return;
22243       case RT_TEMPLATE:
22244         cp_parser_error (parser, "expected %<template%>");
22245         return;
22246       case RT_NAMESPACE:
22247         cp_parser_error (parser, "expected %<namespace%>");
22248         return;
22249       case RT_USING:
22250         cp_parser_error (parser, "expected %<using%>");
22251         return;
22252       case RT_ASM:
22253         cp_parser_error (parser, "expected %<asm%>");
22254         return;
22255       case RT_TRY:
22256         cp_parser_error (parser, "expected %<try%>");
22257         return;
22258       case RT_CATCH:
22259         cp_parser_error (parser, "expected %<catch%>");
22260         return;
22261       case RT_THROW:
22262         cp_parser_error (parser, "expected %<throw%>");
22263         return;
22264       case RT_LABEL:
22265         cp_parser_error (parser, "expected %<__label__%>");
22266         return;
22267       case RT_AT_TRY:
22268         cp_parser_error (parser, "expected %<@try%>");
22269         return;
22270       case RT_AT_SYNCHRONIZED:
22271         cp_parser_error (parser, "expected %<@synchronized%>");
22272         return;
22273       case RT_AT_THROW:
22274         cp_parser_error (parser, "expected %<@throw%>");
22275         return;
22276       case RT_TRANSACTION_ATOMIC:
22277         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22278         return;
22279       case RT_TRANSACTION_RELAXED:
22280         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22281         return;
22282       default:
22283         break;
22284     }
22285   if (!keyword)
22286     {
22287       switch (token_desc)
22288         {
22289           case RT_SEMICOLON:
22290             cp_parser_error (parser, "expected %<;%>");
22291             return;
22292           case RT_OPEN_PAREN:
22293             cp_parser_error (parser, "expected %<(%>");
22294             return;
22295           case RT_CLOSE_BRACE:
22296             cp_parser_error (parser, "expected %<}%>");
22297             return;
22298           case RT_OPEN_BRACE:
22299             cp_parser_error (parser, "expected %<{%>");
22300             return;
22301           case RT_CLOSE_SQUARE:
22302             cp_parser_error (parser, "expected %<]%>");
22303             return;
22304           case RT_OPEN_SQUARE:
22305             cp_parser_error (parser, "expected %<[%>");
22306             return;
22307           case RT_COMMA:
22308             cp_parser_error (parser, "expected %<,%>");
22309             return;
22310           case RT_SCOPE:
22311             cp_parser_error (parser, "expected %<::%>");
22312             return;
22313           case RT_LESS:
22314             cp_parser_error (parser, "expected %<<%>");
22315             return;
22316           case RT_GREATER:
22317             cp_parser_error (parser, "expected %<>%>");
22318             return;
22319           case RT_EQ:
22320             cp_parser_error (parser, "expected %<=%>");
22321             return;
22322           case RT_ELLIPSIS:
22323             cp_parser_error (parser, "expected %<...%>");
22324             return;
22325           case RT_MULT:
22326             cp_parser_error (parser, "expected %<*%>");
22327             return;
22328           case RT_COMPL:
22329             cp_parser_error (parser, "expected %<~%>");
22330             return;
22331           case RT_COLON:
22332             cp_parser_error (parser, "expected %<:%>");
22333             return;
22334           case RT_COLON_SCOPE:
22335             cp_parser_error (parser, "expected %<:%> or %<::%>");
22336             return;
22337           case RT_CLOSE_PAREN:
22338             cp_parser_error (parser, "expected %<)%>");
22339             return;
22340           case RT_COMMA_CLOSE_PAREN:
22341             cp_parser_error (parser, "expected %<,%> or %<)%>");
22342             return;
22343           case RT_PRAGMA_EOL:
22344             cp_parser_error (parser, "expected end of line");
22345             return;
22346           case RT_NAME:
22347             cp_parser_error (parser, "expected identifier");
22348             return;
22349           case RT_SELECT:
22350             cp_parser_error (parser, "expected selection-statement");
22351             return;
22352           case RT_INTERATION:
22353             cp_parser_error (parser, "expected iteration-statement");
22354             return;
22355           case RT_JUMP:
22356             cp_parser_error (parser, "expected jump-statement");
22357             return;
22358           case RT_CLASS_KEY:
22359             cp_parser_error (parser, "expected class-key");
22360             return;
22361           case RT_CLASS_TYPENAME_TEMPLATE:
22362             cp_parser_error (parser,
22363                  "expected %<class%>, %<typename%>, or %<template%>");
22364             return;
22365           default:
22366             gcc_unreachable ();
22367         }
22368     }
22369   else
22370     gcc_unreachable ();
22371 }
22372
22373
22374
22375 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22376    issue an error message indicating that TOKEN_DESC was expected.
22377
22378    Returns the token consumed, if the token had the appropriate type.
22379    Otherwise, returns NULL.  */
22380
22381 static cp_token *
22382 cp_parser_require (cp_parser* parser,
22383                    enum cpp_ttype type,
22384                    required_token token_desc)
22385 {
22386   if (cp_lexer_next_token_is (parser->lexer, type))
22387     return cp_lexer_consume_token (parser->lexer);
22388   else
22389     {
22390       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22391       if (!cp_parser_simulate_error (parser))
22392         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22393       return NULL;
22394     }
22395 }
22396
22397 /* An error message is produced if the next token is not '>'.
22398    All further tokens are skipped until the desired token is
22399    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22400
22401 static void
22402 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22403 {
22404   /* Current level of '< ... >'.  */
22405   unsigned level = 0;
22406   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22407   unsigned nesting_depth = 0;
22408
22409   /* Are we ready, yet?  If not, issue error message.  */
22410   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22411     return;
22412
22413   /* Skip tokens until the desired token is found.  */
22414   while (true)
22415     {
22416       /* Peek at the next token.  */
22417       switch (cp_lexer_peek_token (parser->lexer)->type)
22418         {
22419         case CPP_LESS:
22420           if (!nesting_depth)
22421             ++level;
22422           break;
22423
22424         case CPP_RSHIFT:
22425           if (cxx_dialect == cxx98)
22426             /* C++0x views the `>>' operator as two `>' tokens, but
22427                C++98 does not. */
22428             break;
22429           else if (!nesting_depth && level-- == 0)
22430             {
22431               /* We've hit a `>>' where the first `>' closes the
22432                  template argument list, and the second `>' is
22433                  spurious.  Just consume the `>>' and stop; we've
22434                  already produced at least one error.  */
22435               cp_lexer_consume_token (parser->lexer);
22436               return;
22437             }
22438           /* Fall through for C++0x, so we handle the second `>' in
22439              the `>>'.  */
22440
22441         case CPP_GREATER:
22442           if (!nesting_depth && level-- == 0)
22443             {
22444               /* We've reached the token we want, consume it and stop.  */
22445               cp_lexer_consume_token (parser->lexer);
22446               return;
22447             }
22448           break;
22449
22450         case CPP_OPEN_PAREN:
22451         case CPP_OPEN_SQUARE:
22452           ++nesting_depth;
22453           break;
22454
22455         case CPP_CLOSE_PAREN:
22456         case CPP_CLOSE_SQUARE:
22457           if (nesting_depth-- == 0)
22458             return;
22459           break;
22460
22461         case CPP_EOF:
22462         case CPP_PRAGMA_EOL:
22463         case CPP_SEMICOLON:
22464         case CPP_OPEN_BRACE:
22465         case CPP_CLOSE_BRACE:
22466           /* The '>' was probably forgotten, don't look further.  */
22467           return;
22468
22469         default:
22470           break;
22471         }
22472
22473       /* Consume this token.  */
22474       cp_lexer_consume_token (parser->lexer);
22475     }
22476 }
22477
22478 /* If the next token is the indicated keyword, consume it.  Otherwise,
22479    issue an error message indicating that TOKEN_DESC was expected.
22480
22481    Returns the token consumed, if the token had the appropriate type.
22482    Otherwise, returns NULL.  */
22483
22484 static cp_token *
22485 cp_parser_require_keyword (cp_parser* parser,
22486                            enum rid keyword,
22487                            required_token token_desc)
22488 {
22489   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22490
22491   if (token && token->keyword != keyword)
22492     {
22493       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22494       return NULL;
22495     }
22496
22497   return token;
22498 }
22499
22500 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22501    function-definition.  */
22502
22503 static bool
22504 cp_parser_token_starts_function_definition_p (cp_token* token)
22505 {
22506   return (/* An ordinary function-body begins with an `{'.  */
22507           token->type == CPP_OPEN_BRACE
22508           /* A ctor-initializer begins with a `:'.  */
22509           || token->type == CPP_COLON
22510           /* A function-try-block begins with `try'.  */
22511           || token->keyword == RID_TRY
22512           /* A function-transaction-block begins with `__transaction_atomic'
22513              or `__transaction_relaxed'.  */
22514           || token->keyword == RID_TRANSACTION_ATOMIC
22515           || token->keyword == RID_TRANSACTION_RELAXED
22516           /* The named return value extension begins with `return'.  */
22517           || token->keyword == RID_RETURN);
22518 }
22519
22520 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22521    definition.  */
22522
22523 static bool
22524 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22525 {
22526   cp_token *token;
22527
22528   token = cp_lexer_peek_token (parser->lexer);
22529   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22530 }
22531
22532 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22533    C++0x) ending a template-argument.  */
22534
22535 static bool
22536 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22537 {
22538   cp_token *token;
22539
22540   token = cp_lexer_peek_token (parser->lexer);
22541   return (token->type == CPP_COMMA 
22542           || token->type == CPP_GREATER
22543           || token->type == CPP_ELLIPSIS
22544           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22545 }
22546
22547 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22548    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22549
22550 static bool
22551 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22552                                                      size_t n)
22553 {
22554   cp_token *token;
22555
22556   token = cp_lexer_peek_nth_token (parser->lexer, n);
22557   if (token->type == CPP_LESS)
22558     return true;
22559   /* Check for the sequence `<::' in the original code. It would be lexed as
22560      `[:', where `[' is a digraph, and there is no whitespace before
22561      `:'.  */
22562   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22563     {
22564       cp_token *token2;
22565       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22566       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22567         return true;
22568     }
22569   return false;
22570 }
22571
22572 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22573    or none_type otherwise.  */
22574
22575 static enum tag_types
22576 cp_parser_token_is_class_key (cp_token* token)
22577 {
22578   switch (token->keyword)
22579     {
22580     case RID_CLASS:
22581       return class_type;
22582     case RID_STRUCT:
22583       return record_type;
22584     case RID_UNION:
22585       return union_type;
22586
22587     default:
22588       return none_type;
22589     }
22590 }
22591
22592 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22593
22594 static void
22595 cp_parser_check_class_key (enum tag_types class_key, tree type)
22596 {
22597   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22598     {
22599       permerror (input_location, "%qs tag used in naming %q#T",
22600                  class_key == union_type ? "union"
22601                  : class_key == record_type ? "struct" : "class",
22602                  type);
22603       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22604               "%q#T was previously declared here", type);
22605     }
22606 }
22607
22608 /* Issue an error message if DECL is redeclared with different
22609    access than its original declaration [class.access.spec/3].
22610    This applies to nested classes and nested class templates.
22611    [class.mem/1].  */
22612
22613 static void
22614 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22615 {
22616   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22617     return;
22618
22619   if ((TREE_PRIVATE (decl)
22620        != (current_access_specifier == access_private_node))
22621       || (TREE_PROTECTED (decl)
22622           != (current_access_specifier == access_protected_node)))
22623     error_at (location, "%qD redeclared with different access", decl);
22624 }
22625
22626 /* Look for the `template' keyword, as a syntactic disambiguator.
22627    Return TRUE iff it is present, in which case it will be
22628    consumed.  */
22629
22630 static bool
22631 cp_parser_optional_template_keyword (cp_parser *parser)
22632 {
22633   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22634     {
22635       /* The `template' keyword can only be used within templates;
22636          outside templates the parser can always figure out what is a
22637          template and what is not.  */
22638       if (!processing_template_decl)
22639         {
22640           cp_token *token = cp_lexer_peek_token (parser->lexer);
22641           error_at (token->location,
22642                     "%<template%> (as a disambiguator) is only allowed "
22643                     "within templates");
22644           /* If this part of the token stream is rescanned, the same
22645              error message would be generated.  So, we purge the token
22646              from the stream.  */
22647           cp_lexer_purge_token (parser->lexer);
22648           return false;
22649         }
22650       else
22651         {
22652           /* Consume the `template' keyword.  */
22653           cp_lexer_consume_token (parser->lexer);
22654           return true;
22655         }
22656     }
22657
22658   return false;
22659 }
22660
22661 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22662    set PARSER->SCOPE, and perform other related actions.  */
22663
22664 static void
22665 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22666 {
22667   int i;
22668   struct tree_check *check_value;
22669   deferred_access_check *chk;
22670   VEC (deferred_access_check,gc) *checks;
22671
22672   /* Get the stored value.  */
22673   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22674   /* Perform any access checks that were deferred.  */
22675   checks = check_value->checks;
22676   if (checks)
22677     {
22678       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22679         perform_or_defer_access_check (chk->binfo,
22680                                        chk->decl,
22681                                        chk->diag_decl);
22682     }
22683   /* Set the scope from the stored value.  */
22684   parser->scope = check_value->value;
22685   parser->qualifying_scope = check_value->qualifying_scope;
22686   parser->object_scope = NULL_TREE;
22687 }
22688
22689 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22690    encounter the end of a block before what we were looking for.  */
22691
22692 static bool
22693 cp_parser_cache_group (cp_parser *parser,
22694                        enum cpp_ttype end,
22695                        unsigned depth)
22696 {
22697   while (true)
22698     {
22699       cp_token *token = cp_lexer_peek_token (parser->lexer);
22700
22701       /* Abort a parenthesized expression if we encounter a semicolon.  */
22702       if ((end == CPP_CLOSE_PAREN || depth == 0)
22703           && token->type == CPP_SEMICOLON)
22704         return true;
22705       /* If we've reached the end of the file, stop.  */
22706       if (token->type == CPP_EOF
22707           || (end != CPP_PRAGMA_EOL
22708               && token->type == CPP_PRAGMA_EOL))
22709         return true;
22710       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22711         /* We've hit the end of an enclosing block, so there's been some
22712            kind of syntax error.  */
22713         return true;
22714
22715       /* If we're caching something finished by a comma (or semicolon),
22716          such as an NSDMI, don't consume the comma.  */
22717       if (end == CPP_COMMA
22718           && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22719         return false;
22720
22721       /* Consume the token.  */
22722       cp_lexer_consume_token (parser->lexer);
22723       /* See if it starts a new group.  */
22724       if (token->type == CPP_OPEN_BRACE)
22725         {
22726           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22727           /* In theory this should probably check end == '}', but
22728              cp_parser_save_member_function_body needs it to exit
22729              after either '}' or ')' when called with ')'.  */
22730           if (depth == 0)
22731             return false;
22732         }
22733       else if (token->type == CPP_OPEN_PAREN)
22734         {
22735           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22736           if (depth == 0 && end == CPP_CLOSE_PAREN)
22737             return false;
22738         }
22739       else if (token->type == CPP_PRAGMA)
22740         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22741       else if (token->type == end)
22742         return false;
22743     }
22744 }
22745
22746 /* Begin parsing tentatively.  We always save tokens while parsing
22747    tentatively so that if the tentative parsing fails we can restore the
22748    tokens.  */
22749
22750 static void
22751 cp_parser_parse_tentatively (cp_parser* parser)
22752 {
22753   /* Enter a new parsing context.  */
22754   parser->context = cp_parser_context_new (parser->context);
22755   /* Begin saving tokens.  */
22756   cp_lexer_save_tokens (parser->lexer);
22757   /* In order to avoid repetitive access control error messages,
22758      access checks are queued up until we are no longer parsing
22759      tentatively.  */
22760   push_deferring_access_checks (dk_deferred);
22761 }
22762
22763 /* Commit to the currently active tentative parse.  */
22764
22765 static void
22766 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22767 {
22768   cp_parser_context *context;
22769   cp_lexer *lexer;
22770
22771   /* Mark all of the levels as committed.  */
22772   lexer = parser->lexer;
22773   for (context = parser->context; context->next; context = context->next)
22774     {
22775       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22776         break;
22777       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22778       while (!cp_lexer_saving_tokens (lexer))
22779         lexer = lexer->next;
22780       cp_lexer_commit_tokens (lexer);
22781     }
22782 }
22783
22784 /* Abort the currently active tentative parse.  All consumed tokens
22785    will be rolled back, and no diagnostics will be issued.  */
22786
22787 static void
22788 cp_parser_abort_tentative_parse (cp_parser* parser)
22789 {
22790   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22791               || errorcount > 0);
22792   cp_parser_simulate_error (parser);
22793   /* Now, pretend that we want to see if the construct was
22794      successfully parsed.  */
22795   cp_parser_parse_definitely (parser);
22796 }
22797
22798 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22799    token stream.  Otherwise, commit to the tokens we have consumed.
22800    Returns true if no error occurred; false otherwise.  */
22801
22802 static bool
22803 cp_parser_parse_definitely (cp_parser* parser)
22804 {
22805   bool error_occurred;
22806   cp_parser_context *context;
22807
22808   /* Remember whether or not an error occurred, since we are about to
22809      destroy that information.  */
22810   error_occurred = cp_parser_error_occurred (parser);
22811   /* Remove the topmost context from the stack.  */
22812   context = parser->context;
22813   parser->context = context->next;
22814   /* If no parse errors occurred, commit to the tentative parse.  */
22815   if (!error_occurred)
22816     {
22817       /* Commit to the tokens read tentatively, unless that was
22818          already done.  */
22819       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22820         cp_lexer_commit_tokens (parser->lexer);
22821
22822       pop_to_parent_deferring_access_checks ();
22823     }
22824   /* Otherwise, if errors occurred, roll back our state so that things
22825      are just as they were before we began the tentative parse.  */
22826   else
22827     {
22828       cp_lexer_rollback_tokens (parser->lexer);
22829       pop_deferring_access_checks ();
22830     }
22831   /* Add the context to the front of the free list.  */
22832   context->next = cp_parser_context_free_list;
22833   cp_parser_context_free_list = context;
22834
22835   return !error_occurred;
22836 }
22837
22838 /* Returns true if we are parsing tentatively and are not committed to
22839    this tentative parse.  */
22840
22841 static bool
22842 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22843 {
22844   return (cp_parser_parsing_tentatively (parser)
22845           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22846 }
22847
22848 /* Returns nonzero iff an error has occurred during the most recent
22849    tentative parse.  */
22850
22851 static bool
22852 cp_parser_error_occurred (cp_parser* parser)
22853 {
22854   return (cp_parser_parsing_tentatively (parser)
22855           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22856 }
22857
22858 /* Returns nonzero if GNU extensions are allowed.  */
22859
22860 static bool
22861 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22862 {
22863   return parser->allow_gnu_extensions_p;
22864 }
22865 \f
22866 /* Objective-C++ Productions */
22867
22868
22869 /* Parse an Objective-C expression, which feeds into a primary-expression
22870    above.
22871
22872    objc-expression:
22873      objc-message-expression
22874      objc-string-literal
22875      objc-encode-expression
22876      objc-protocol-expression
22877      objc-selector-expression
22878
22879   Returns a tree representation of the expression.  */
22880
22881 static tree
22882 cp_parser_objc_expression (cp_parser* parser)
22883 {
22884   /* Try to figure out what kind of declaration is present.  */
22885   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22886
22887   switch (kwd->type)
22888     {
22889     case CPP_OPEN_SQUARE:
22890       return cp_parser_objc_message_expression (parser);
22891
22892     case CPP_OBJC_STRING:
22893       kwd = cp_lexer_consume_token (parser->lexer);
22894       return objc_build_string_object (kwd->u.value);
22895
22896     case CPP_KEYWORD:
22897       switch (kwd->keyword)
22898         {
22899         case RID_AT_ENCODE:
22900           return cp_parser_objc_encode_expression (parser);
22901
22902         case RID_AT_PROTOCOL:
22903           return cp_parser_objc_protocol_expression (parser);
22904
22905         case RID_AT_SELECTOR:
22906           return cp_parser_objc_selector_expression (parser);
22907
22908         default:
22909           break;
22910         }
22911     default:
22912       error_at (kwd->location,
22913                 "misplaced %<@%D%> Objective-C++ construct",
22914                 kwd->u.value);
22915       cp_parser_skip_to_end_of_block_or_statement (parser);
22916     }
22917
22918   return error_mark_node;
22919 }
22920
22921 /* Parse an Objective-C message expression.
22922
22923    objc-message-expression:
22924      [ objc-message-receiver objc-message-args ]
22925
22926    Returns a representation of an Objective-C message.  */
22927
22928 static tree
22929 cp_parser_objc_message_expression (cp_parser* parser)
22930 {
22931   tree receiver, messageargs;
22932
22933   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
22934   receiver = cp_parser_objc_message_receiver (parser);
22935   messageargs = cp_parser_objc_message_args (parser);
22936   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22937
22938   return objc_build_message_expr (receiver, messageargs);
22939 }
22940
22941 /* Parse an objc-message-receiver.
22942
22943    objc-message-receiver:
22944      expression
22945      simple-type-specifier
22946
22947   Returns a representation of the type or expression.  */
22948
22949 static tree
22950 cp_parser_objc_message_receiver (cp_parser* parser)
22951 {
22952   tree rcv;
22953
22954   /* An Objective-C message receiver may be either (1) a type
22955      or (2) an expression.  */
22956   cp_parser_parse_tentatively (parser);
22957   rcv = cp_parser_expression (parser, false, NULL);
22958
22959   if (cp_parser_parse_definitely (parser))
22960     return rcv;
22961
22962   rcv = cp_parser_simple_type_specifier (parser,
22963                                          /*decl_specs=*/NULL,
22964                                          CP_PARSER_FLAGS_NONE);
22965
22966   return objc_get_class_reference (rcv);
22967 }
22968
22969 /* Parse the arguments and selectors comprising an Objective-C message.
22970
22971    objc-message-args:
22972      objc-selector
22973      objc-selector-args
22974      objc-selector-args , objc-comma-args
22975
22976    objc-selector-args:
22977      objc-selector [opt] : assignment-expression
22978      objc-selector-args objc-selector [opt] : assignment-expression
22979
22980    objc-comma-args:
22981      assignment-expression
22982      objc-comma-args , assignment-expression
22983
22984    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22985    selector arguments and TREE_VALUE containing a list of comma
22986    arguments.  */
22987
22988 static tree
22989 cp_parser_objc_message_args (cp_parser* parser)
22990 {
22991   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22992   bool maybe_unary_selector_p = true;
22993   cp_token *token = cp_lexer_peek_token (parser->lexer);
22994
22995   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22996     {
22997       tree selector = NULL_TREE, arg;
22998
22999       if (token->type != CPP_COLON)
23000         selector = cp_parser_objc_selector (parser);
23001
23002       /* Detect if we have a unary selector.  */
23003       if (maybe_unary_selector_p
23004           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23005         return build_tree_list (selector, NULL_TREE);
23006
23007       maybe_unary_selector_p = false;
23008       cp_parser_require (parser, CPP_COLON, RT_COLON);
23009       arg = cp_parser_assignment_expression (parser, false, NULL);
23010
23011       sel_args
23012         = chainon (sel_args,
23013                    build_tree_list (selector, arg));
23014
23015       token = cp_lexer_peek_token (parser->lexer);
23016     }
23017
23018   /* Handle non-selector arguments, if any. */
23019   while (token->type == CPP_COMMA)
23020     {
23021       tree arg;
23022
23023       cp_lexer_consume_token (parser->lexer);
23024       arg = cp_parser_assignment_expression (parser, false, NULL);
23025
23026       addl_args
23027         = chainon (addl_args,
23028                    build_tree_list (NULL_TREE, arg));
23029
23030       token = cp_lexer_peek_token (parser->lexer);
23031     }
23032
23033   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23034     {
23035       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23036       return build_tree_list (error_mark_node, error_mark_node);
23037     }
23038
23039   return build_tree_list (sel_args, addl_args);
23040 }
23041
23042 /* Parse an Objective-C encode expression.
23043
23044    objc-encode-expression:
23045      @encode objc-typename
23046
23047    Returns an encoded representation of the type argument.  */
23048
23049 static tree
23050 cp_parser_objc_encode_expression (cp_parser* parser)
23051 {
23052   tree type;
23053   cp_token *token;
23054
23055   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
23056   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23057   token = cp_lexer_peek_token (parser->lexer);
23058   type = complete_type (cp_parser_type_id (parser));
23059   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23060
23061   if (!type)
23062     {
23063       error_at (token->location, 
23064                 "%<@encode%> must specify a type as an argument");
23065       return error_mark_node;
23066     }
23067
23068   /* This happens if we find @encode(T) (where T is a template
23069      typename or something dependent on a template typename) when
23070      parsing a template.  In that case, we can't compile it
23071      immediately, but we rather create an AT_ENCODE_EXPR which will
23072      need to be instantiated when the template is used.
23073   */
23074   if (dependent_type_p (type))
23075     {
23076       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23077       TREE_READONLY (value) = 1;
23078       return value;
23079     }
23080
23081   return objc_build_encode_expr (type);
23082 }
23083
23084 /* Parse an Objective-C @defs expression.  */
23085
23086 static tree
23087 cp_parser_objc_defs_expression (cp_parser *parser)
23088 {
23089   tree name;
23090
23091   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
23092   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23093   name = cp_parser_identifier (parser);
23094   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23095
23096   return objc_get_class_ivars (name);
23097 }
23098
23099 /* Parse an Objective-C protocol expression.
23100
23101   objc-protocol-expression:
23102     @protocol ( identifier )
23103
23104   Returns a representation of the protocol expression.  */
23105
23106 static tree
23107 cp_parser_objc_protocol_expression (cp_parser* parser)
23108 {
23109   tree proto;
23110
23111   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23112   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23113   proto = cp_parser_identifier (parser);
23114   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23115
23116   return objc_build_protocol_expr (proto);
23117 }
23118
23119 /* Parse an Objective-C selector expression.
23120
23121    objc-selector-expression:
23122      @selector ( objc-method-signature )
23123
23124    objc-method-signature:
23125      objc-selector
23126      objc-selector-seq
23127
23128    objc-selector-seq:
23129      objc-selector :
23130      objc-selector-seq objc-selector :
23131
23132   Returns a representation of the method selector.  */
23133
23134 static tree
23135 cp_parser_objc_selector_expression (cp_parser* parser)
23136 {
23137   tree sel_seq = NULL_TREE;
23138   bool maybe_unary_selector_p = true;
23139   cp_token *token;
23140   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23141
23142   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23143   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23144   token = cp_lexer_peek_token (parser->lexer);
23145
23146   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23147          || token->type == CPP_SCOPE)
23148     {
23149       tree selector = NULL_TREE;
23150
23151       if (token->type != CPP_COLON
23152           || token->type == CPP_SCOPE)
23153         selector = cp_parser_objc_selector (parser);
23154
23155       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23156           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23157         {
23158           /* Detect if we have a unary selector.  */
23159           if (maybe_unary_selector_p)
23160             {
23161               sel_seq = selector;
23162               goto finish_selector;
23163             }
23164           else
23165             {
23166               cp_parser_error (parser, "expected %<:%>");
23167             }
23168         }
23169       maybe_unary_selector_p = false;
23170       token = cp_lexer_consume_token (parser->lexer);
23171
23172       if (token->type == CPP_SCOPE)
23173         {
23174           sel_seq
23175             = chainon (sel_seq,
23176                        build_tree_list (selector, NULL_TREE));
23177           sel_seq
23178             = chainon (sel_seq,
23179                        build_tree_list (NULL_TREE, NULL_TREE));
23180         }
23181       else
23182         sel_seq
23183           = chainon (sel_seq,
23184                      build_tree_list (selector, NULL_TREE));
23185
23186       token = cp_lexer_peek_token (parser->lexer);
23187     }
23188
23189  finish_selector:
23190   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23191
23192   return objc_build_selector_expr (loc, sel_seq);
23193 }
23194
23195 /* Parse a list of identifiers.
23196
23197    objc-identifier-list:
23198      identifier
23199      objc-identifier-list , identifier
23200
23201    Returns a TREE_LIST of identifier nodes.  */
23202
23203 static tree
23204 cp_parser_objc_identifier_list (cp_parser* parser)
23205 {
23206   tree identifier;
23207   tree list;
23208   cp_token *sep;
23209
23210   identifier = cp_parser_identifier (parser);
23211   if (identifier == error_mark_node)
23212     return error_mark_node;      
23213
23214   list = build_tree_list (NULL_TREE, identifier);
23215   sep = cp_lexer_peek_token (parser->lexer);
23216
23217   while (sep->type == CPP_COMMA)
23218     {
23219       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23220       identifier = cp_parser_identifier (parser);
23221       if (identifier == error_mark_node)
23222         return list;
23223
23224       list = chainon (list, build_tree_list (NULL_TREE,
23225                                              identifier));
23226       sep = cp_lexer_peek_token (parser->lexer);
23227     }
23228   
23229   return list;
23230 }
23231
23232 /* Parse an Objective-C alias declaration.
23233
23234    objc-alias-declaration:
23235      @compatibility_alias identifier identifier ;
23236
23237    This function registers the alias mapping with the Objective-C front end.
23238    It returns nothing.  */
23239
23240 static void
23241 cp_parser_objc_alias_declaration (cp_parser* parser)
23242 {
23243   tree alias, orig;
23244
23245   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23246   alias = cp_parser_identifier (parser);
23247   orig = cp_parser_identifier (parser);
23248   objc_declare_alias (alias, orig);
23249   cp_parser_consume_semicolon_at_end_of_statement (parser);
23250 }
23251
23252 /* Parse an Objective-C class forward-declaration.
23253
23254    objc-class-declaration:
23255      @class objc-identifier-list ;
23256
23257    The function registers the forward declarations with the Objective-C
23258    front end.  It returns nothing.  */
23259
23260 static void
23261 cp_parser_objc_class_declaration (cp_parser* parser)
23262 {
23263   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23264   while (true)
23265     {
23266       tree id;
23267       
23268       id = cp_parser_identifier (parser);
23269       if (id == error_mark_node)
23270         break;
23271       
23272       objc_declare_class (id);
23273
23274       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23275         cp_lexer_consume_token (parser->lexer);
23276       else
23277         break;
23278     }
23279   cp_parser_consume_semicolon_at_end_of_statement (parser);
23280 }
23281
23282 /* Parse a list of Objective-C protocol references.
23283
23284    objc-protocol-refs-opt:
23285      objc-protocol-refs [opt]
23286
23287    objc-protocol-refs:
23288      < objc-identifier-list >
23289
23290    Returns a TREE_LIST of identifiers, if any.  */
23291
23292 static tree
23293 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23294 {
23295   tree protorefs = NULL_TREE;
23296
23297   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23298     {
23299       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23300       protorefs = cp_parser_objc_identifier_list (parser);
23301       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23302     }
23303
23304   return protorefs;
23305 }
23306
23307 /* Parse a Objective-C visibility specification.  */
23308
23309 static void
23310 cp_parser_objc_visibility_spec (cp_parser* parser)
23311 {
23312   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23313
23314   switch (vis->keyword)
23315     {
23316     case RID_AT_PRIVATE:
23317       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23318       break;
23319     case RID_AT_PROTECTED:
23320       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23321       break;
23322     case RID_AT_PUBLIC:
23323       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23324       break;
23325     case RID_AT_PACKAGE:
23326       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23327       break;
23328     default:
23329       return;
23330     }
23331
23332   /* Eat '@private'/'@protected'/'@public'.  */
23333   cp_lexer_consume_token (parser->lexer);
23334 }
23335
23336 /* Parse an Objective-C method type.  Return 'true' if it is a class
23337    (+) method, and 'false' if it is an instance (-) method.  */
23338
23339 static inline bool
23340 cp_parser_objc_method_type (cp_parser* parser)
23341 {
23342   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23343     return true;
23344   else
23345     return false;
23346 }
23347
23348 /* Parse an Objective-C protocol qualifier.  */
23349
23350 static tree
23351 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23352 {
23353   tree quals = NULL_TREE, node;
23354   cp_token *token = cp_lexer_peek_token (parser->lexer);
23355
23356   node = token->u.value;
23357
23358   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23359          && (node == ridpointers [(int) RID_IN]
23360              || node == ridpointers [(int) RID_OUT]
23361              || node == ridpointers [(int) RID_INOUT]
23362              || node == ridpointers [(int) RID_BYCOPY]
23363              || node == ridpointers [(int) RID_BYREF]
23364              || node == ridpointers [(int) RID_ONEWAY]))
23365     {
23366       quals = tree_cons (NULL_TREE, node, quals);
23367       cp_lexer_consume_token (parser->lexer);
23368       token = cp_lexer_peek_token (parser->lexer);
23369       node = token->u.value;
23370     }
23371
23372   return quals;
23373 }
23374
23375 /* Parse an Objective-C typename.  */
23376
23377 static tree
23378 cp_parser_objc_typename (cp_parser* parser)
23379 {
23380   tree type_name = NULL_TREE;
23381
23382   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23383     {
23384       tree proto_quals, cp_type = NULL_TREE;
23385
23386       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23387       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23388
23389       /* An ObjC type name may consist of just protocol qualifiers, in which
23390          case the type shall default to 'id'.  */
23391       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23392         {
23393           cp_type = cp_parser_type_id (parser);
23394           
23395           /* If the type could not be parsed, an error has already
23396              been produced.  For error recovery, behave as if it had
23397              not been specified, which will use the default type
23398              'id'.  */
23399           if (cp_type == error_mark_node)
23400             {
23401               cp_type = NULL_TREE;
23402               /* We need to skip to the closing parenthesis as
23403                  cp_parser_type_id() does not seem to do it for
23404                  us.  */
23405               cp_parser_skip_to_closing_parenthesis (parser,
23406                                                      /*recovering=*/true,
23407                                                      /*or_comma=*/false,
23408                                                      /*consume_paren=*/false);
23409             }
23410         }
23411
23412       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23413       type_name = build_tree_list (proto_quals, cp_type);
23414     }
23415
23416   return type_name;
23417 }
23418
23419 /* Check to see if TYPE refers to an Objective-C selector name.  */
23420
23421 static bool
23422 cp_parser_objc_selector_p (enum cpp_ttype type)
23423 {
23424   return (type == CPP_NAME || type == CPP_KEYWORD
23425           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23426           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23427           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23428           || type == CPP_XOR || type == CPP_XOR_EQ);
23429 }
23430
23431 /* Parse an Objective-C selector.  */
23432
23433 static tree
23434 cp_parser_objc_selector (cp_parser* parser)
23435 {
23436   cp_token *token = cp_lexer_consume_token (parser->lexer);
23437
23438   if (!cp_parser_objc_selector_p (token->type))
23439     {
23440       error_at (token->location, "invalid Objective-C++ selector name");
23441       return error_mark_node;
23442     }
23443
23444   /* C++ operator names are allowed to appear in ObjC selectors.  */
23445   switch (token->type)
23446     {
23447     case CPP_AND_AND: return get_identifier ("and");
23448     case CPP_AND_EQ: return get_identifier ("and_eq");
23449     case CPP_AND: return get_identifier ("bitand");
23450     case CPP_OR: return get_identifier ("bitor");
23451     case CPP_COMPL: return get_identifier ("compl");
23452     case CPP_NOT: return get_identifier ("not");
23453     case CPP_NOT_EQ: return get_identifier ("not_eq");
23454     case CPP_OR_OR: return get_identifier ("or");
23455     case CPP_OR_EQ: return get_identifier ("or_eq");
23456     case CPP_XOR: return get_identifier ("xor");
23457     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23458     default: return token->u.value;
23459     }
23460 }
23461
23462 /* Parse an Objective-C params list.  */
23463
23464 static tree
23465 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23466 {
23467   tree params = NULL_TREE;
23468   bool maybe_unary_selector_p = true;
23469   cp_token *token = cp_lexer_peek_token (parser->lexer);
23470
23471   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23472     {
23473       tree selector = NULL_TREE, type_name, identifier;
23474       tree parm_attr = NULL_TREE;
23475
23476       if (token->keyword == RID_ATTRIBUTE)
23477         break;
23478
23479       if (token->type != CPP_COLON)
23480         selector = cp_parser_objc_selector (parser);
23481
23482       /* Detect if we have a unary selector.  */
23483       if (maybe_unary_selector_p
23484           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23485         {
23486           params = selector; /* Might be followed by attributes.  */
23487           break;
23488         }
23489
23490       maybe_unary_selector_p = false;
23491       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23492         {
23493           /* Something went quite wrong.  There should be a colon
23494              here, but there is not.  Stop parsing parameters.  */
23495           break;
23496         }
23497       type_name = cp_parser_objc_typename (parser);
23498       /* New ObjC allows attributes on parameters too.  */
23499       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23500         parm_attr = cp_parser_attributes_opt (parser);
23501       identifier = cp_parser_identifier (parser);
23502
23503       params
23504         = chainon (params,
23505                    objc_build_keyword_decl (selector,
23506                                             type_name,
23507                                             identifier,
23508                                             parm_attr));
23509
23510       token = cp_lexer_peek_token (parser->lexer);
23511     }
23512
23513   if (params == NULL_TREE)
23514     {
23515       cp_parser_error (parser, "objective-c++ method declaration is expected");
23516       return error_mark_node;
23517     }
23518
23519   /* We allow tail attributes for the method.  */
23520   if (token->keyword == RID_ATTRIBUTE)
23521     {
23522       *attributes = cp_parser_attributes_opt (parser);
23523       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23524           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23525         return params;
23526       cp_parser_error (parser, 
23527                        "method attributes must be specified at the end");
23528       return error_mark_node;
23529     }
23530
23531   if (params == NULL_TREE)
23532     {
23533       cp_parser_error (parser, "objective-c++ method declaration is expected");
23534       return error_mark_node;
23535     }
23536   return params;
23537 }
23538
23539 /* Parse the non-keyword Objective-C params.  */
23540
23541 static tree
23542 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23543                                        tree* attributes)
23544 {
23545   tree params = make_node (TREE_LIST);
23546   cp_token *token = cp_lexer_peek_token (parser->lexer);
23547   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23548
23549   while (token->type == CPP_COMMA)
23550     {
23551       cp_parameter_declarator *parmdecl;
23552       tree parm;
23553
23554       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23555       token = cp_lexer_peek_token (parser->lexer);
23556
23557       if (token->type == CPP_ELLIPSIS)
23558         {
23559           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23560           *ellipsisp = true;
23561           token = cp_lexer_peek_token (parser->lexer);
23562           break;
23563         }
23564
23565       /* TODO: parse attributes for tail parameters.  */
23566       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23567       parm = grokdeclarator (parmdecl->declarator,
23568                              &parmdecl->decl_specifiers,
23569                              PARM, /*initialized=*/0,
23570                              /*attrlist=*/NULL);
23571
23572       chainon (params, build_tree_list (NULL_TREE, parm));
23573       token = cp_lexer_peek_token (parser->lexer);
23574     }
23575
23576   /* We allow tail attributes for the method.  */
23577   if (token->keyword == RID_ATTRIBUTE)
23578     {
23579       if (*attributes == NULL_TREE)
23580         {
23581           *attributes = cp_parser_attributes_opt (parser);
23582           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23583               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23584             return params;
23585         }
23586       else        
23587         /* We have an error, but parse the attributes, so that we can 
23588            carry on.  */
23589         *attributes = cp_parser_attributes_opt (parser);
23590
23591       cp_parser_error (parser, 
23592                        "method attributes must be specified at the end");
23593       return error_mark_node;
23594     }
23595
23596   return params;
23597 }
23598
23599 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23600
23601 static void
23602 cp_parser_objc_interstitial_code (cp_parser* parser)
23603 {
23604   cp_token *token = cp_lexer_peek_token (parser->lexer);
23605
23606   /* If the next token is `extern' and the following token is a string
23607      literal, then we have a linkage specification.  */
23608   if (token->keyword == RID_EXTERN
23609       && cp_parser_is_pure_string_literal
23610          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23611     cp_parser_linkage_specification (parser);
23612   /* Handle #pragma, if any.  */
23613   else if (token->type == CPP_PRAGMA)
23614     cp_parser_pragma (parser, pragma_external);
23615   /* Allow stray semicolons.  */
23616   else if (token->type == CPP_SEMICOLON)
23617     cp_lexer_consume_token (parser->lexer);
23618   /* Mark methods as optional or required, when building protocols.  */
23619   else if (token->keyword == RID_AT_OPTIONAL)
23620     {
23621       cp_lexer_consume_token (parser->lexer);
23622       objc_set_method_opt (true);
23623     }
23624   else if (token->keyword == RID_AT_REQUIRED)
23625     {
23626       cp_lexer_consume_token (parser->lexer);
23627       objc_set_method_opt (false);
23628     }
23629   else if (token->keyword == RID_NAMESPACE)
23630     cp_parser_namespace_definition (parser);
23631   /* Other stray characters must generate errors.  */
23632   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23633     {
23634       cp_lexer_consume_token (parser->lexer);
23635       error ("stray %qs between Objective-C++ methods",
23636              token->type == CPP_OPEN_BRACE ? "{" : "}");
23637     }
23638   /* Finally, try to parse a block-declaration, or a function-definition.  */
23639   else
23640     cp_parser_block_declaration (parser, /*statement_p=*/false);
23641 }
23642
23643 /* Parse a method signature.  */
23644
23645 static tree
23646 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23647 {
23648   tree rettype, kwdparms, optparms;
23649   bool ellipsis = false;
23650   bool is_class_method;
23651
23652   is_class_method = cp_parser_objc_method_type (parser);
23653   rettype = cp_parser_objc_typename (parser);
23654   *attributes = NULL_TREE;
23655   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23656   if (kwdparms == error_mark_node)
23657     return error_mark_node;
23658   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23659   if (optparms == error_mark_node)
23660     return error_mark_node;
23661
23662   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23663 }
23664
23665 static bool
23666 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23667 {
23668   tree tattr;  
23669   cp_lexer_save_tokens (parser->lexer);
23670   tattr = cp_parser_attributes_opt (parser);
23671   gcc_assert (tattr) ;
23672   
23673   /* If the attributes are followed by a method introducer, this is not allowed.
23674      Dump the attributes and flag the situation.  */
23675   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23676       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23677     return true;
23678
23679   /* Otherwise, the attributes introduce some interstitial code, possibly so
23680      rewind to allow that check.  */
23681   cp_lexer_rollback_tokens (parser->lexer);
23682   return false;  
23683 }
23684
23685 /* Parse an Objective-C method prototype list.  */
23686
23687 static void
23688 cp_parser_objc_method_prototype_list (cp_parser* parser)
23689 {
23690   cp_token *token = cp_lexer_peek_token (parser->lexer);
23691
23692   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23693     {
23694       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23695         {
23696           tree attributes, sig;
23697           bool is_class_method;
23698           if (token->type == CPP_PLUS)
23699             is_class_method = true;
23700           else
23701             is_class_method = false;
23702           sig = cp_parser_objc_method_signature (parser, &attributes);
23703           if (sig == error_mark_node)
23704             {
23705               cp_parser_skip_to_end_of_block_or_statement (parser);
23706               token = cp_lexer_peek_token (parser->lexer);
23707               continue;
23708             }
23709           objc_add_method_declaration (is_class_method, sig, attributes);
23710           cp_parser_consume_semicolon_at_end_of_statement (parser);
23711         }
23712       else if (token->keyword == RID_AT_PROPERTY)
23713         cp_parser_objc_at_property_declaration (parser);
23714       else if (token->keyword == RID_ATTRIBUTE 
23715                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23716         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23717                     OPT_Wattributes, 
23718                     "prefix attributes are ignored for methods");
23719       else
23720         /* Allow for interspersed non-ObjC++ code.  */
23721         cp_parser_objc_interstitial_code (parser);
23722
23723       token = cp_lexer_peek_token (parser->lexer);
23724     }
23725
23726   if (token->type != CPP_EOF)
23727     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23728   else
23729     cp_parser_error (parser, "expected %<@end%>");
23730
23731   objc_finish_interface ();
23732 }
23733
23734 /* Parse an Objective-C method definition list.  */
23735
23736 static void
23737 cp_parser_objc_method_definition_list (cp_parser* parser)
23738 {
23739   cp_token *token = cp_lexer_peek_token (parser->lexer);
23740
23741   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23742     {
23743       tree meth;
23744
23745       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23746         {
23747           cp_token *ptk;
23748           tree sig, attribute;
23749           bool is_class_method;
23750           if (token->type == CPP_PLUS)
23751             is_class_method = true;
23752           else
23753             is_class_method = false;
23754           push_deferring_access_checks (dk_deferred);
23755           sig = cp_parser_objc_method_signature (parser, &attribute);
23756           if (sig == error_mark_node)
23757             {
23758               cp_parser_skip_to_end_of_block_or_statement (parser);
23759               token = cp_lexer_peek_token (parser->lexer);
23760               continue;
23761             }
23762           objc_start_method_definition (is_class_method, sig, attribute,
23763                                         NULL_TREE);
23764
23765           /* For historical reasons, we accept an optional semicolon.  */
23766           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23767             cp_lexer_consume_token (parser->lexer);
23768
23769           ptk = cp_lexer_peek_token (parser->lexer);
23770           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23771                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23772             {
23773               perform_deferred_access_checks ();
23774               stop_deferring_access_checks ();
23775               meth = cp_parser_function_definition_after_declarator (parser,
23776                                                                      false);
23777               pop_deferring_access_checks ();
23778               objc_finish_method_definition (meth);
23779             }
23780         }
23781       /* The following case will be removed once @synthesize is
23782          completely implemented.  */
23783       else if (token->keyword == RID_AT_PROPERTY)
23784         cp_parser_objc_at_property_declaration (parser);
23785       else if (token->keyword == RID_AT_SYNTHESIZE)
23786         cp_parser_objc_at_synthesize_declaration (parser);
23787       else if (token->keyword == RID_AT_DYNAMIC)
23788         cp_parser_objc_at_dynamic_declaration (parser);
23789       else if (token->keyword == RID_ATTRIBUTE 
23790                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23791         warning_at (token->location, OPT_Wattributes,
23792                     "prefix attributes are ignored for methods");
23793       else
23794         /* Allow for interspersed non-ObjC++ code.  */
23795         cp_parser_objc_interstitial_code (parser);
23796
23797       token = cp_lexer_peek_token (parser->lexer);
23798     }
23799
23800   if (token->type != CPP_EOF)
23801     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23802   else
23803     cp_parser_error (parser, "expected %<@end%>");
23804
23805   objc_finish_implementation ();
23806 }
23807
23808 /* Parse Objective-C ivars.  */
23809
23810 static void
23811 cp_parser_objc_class_ivars (cp_parser* parser)
23812 {
23813   cp_token *token = cp_lexer_peek_token (parser->lexer);
23814
23815   if (token->type != CPP_OPEN_BRACE)
23816     return;     /* No ivars specified.  */
23817
23818   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23819   token = cp_lexer_peek_token (parser->lexer);
23820
23821   while (token->type != CPP_CLOSE_BRACE 
23822         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23823     {
23824       cp_decl_specifier_seq declspecs;
23825       int decl_class_or_enum_p;
23826       tree prefix_attributes;
23827
23828       cp_parser_objc_visibility_spec (parser);
23829
23830       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23831         break;
23832
23833       cp_parser_decl_specifier_seq (parser,
23834                                     CP_PARSER_FLAGS_OPTIONAL,
23835                                     &declspecs,
23836                                     &decl_class_or_enum_p);
23837
23838       /* auto, register, static, extern, mutable.  */
23839       if (declspecs.storage_class != sc_none)
23840         {
23841           cp_parser_error (parser, "invalid type for instance variable");         
23842           declspecs.storage_class = sc_none;
23843         }
23844
23845       /* __thread.  */
23846       if (declspecs.specs[(int) ds_thread])
23847         {
23848           cp_parser_error (parser, "invalid type for instance variable");
23849           declspecs.specs[(int) ds_thread] = 0;
23850         }
23851       
23852       /* typedef.  */
23853       if (declspecs.specs[(int) ds_typedef])
23854         {
23855           cp_parser_error (parser, "invalid type for instance variable");
23856           declspecs.specs[(int) ds_typedef] = 0;
23857         }
23858
23859       prefix_attributes = declspecs.attributes;
23860       declspecs.attributes = NULL_TREE;
23861
23862       /* Keep going until we hit the `;' at the end of the
23863          declaration.  */
23864       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23865         {
23866           tree width = NULL_TREE, attributes, first_attribute, decl;
23867           cp_declarator *declarator = NULL;
23868           int ctor_dtor_or_conv_p;
23869
23870           /* Check for a (possibly unnamed) bitfield declaration.  */
23871           token = cp_lexer_peek_token (parser->lexer);
23872           if (token->type == CPP_COLON)
23873             goto eat_colon;
23874
23875           if (token->type == CPP_NAME
23876               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23877                   == CPP_COLON))
23878             {
23879               /* Get the name of the bitfield.  */
23880               declarator = make_id_declarator (NULL_TREE,
23881                                                cp_parser_identifier (parser),
23882                                                sfk_none);
23883
23884              eat_colon:
23885               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23886               /* Get the width of the bitfield.  */
23887               width
23888                 = cp_parser_constant_expression (parser,
23889                                                  /*allow_non_constant=*/false,
23890                                                  NULL);
23891             }
23892           else
23893             {
23894               /* Parse the declarator.  */
23895               declarator
23896                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23897                                         &ctor_dtor_or_conv_p,
23898                                         /*parenthesized_p=*/NULL,
23899                                         /*member_p=*/false);
23900             }
23901
23902           /* Look for attributes that apply to the ivar.  */
23903           attributes = cp_parser_attributes_opt (parser);
23904           /* Remember which attributes are prefix attributes and
23905              which are not.  */
23906           first_attribute = attributes;
23907           /* Combine the attributes.  */
23908           attributes = chainon (prefix_attributes, attributes);
23909
23910           if (width)
23911               /* Create the bitfield declaration.  */
23912               decl = grokbitfield (declarator, &declspecs,
23913                                    width,
23914                                    attributes);
23915           else
23916             decl = grokfield (declarator, &declspecs,
23917                               NULL_TREE, /*init_const_expr_p=*/false,
23918                               NULL_TREE, attributes);
23919
23920           /* Add the instance variable.  */
23921           if (decl != error_mark_node && decl != NULL_TREE)
23922             objc_add_instance_variable (decl);
23923
23924           /* Reset PREFIX_ATTRIBUTES.  */
23925           while (attributes && TREE_CHAIN (attributes) != first_attribute)
23926             attributes = TREE_CHAIN (attributes);
23927           if (attributes)
23928             TREE_CHAIN (attributes) = NULL_TREE;
23929
23930           token = cp_lexer_peek_token (parser->lexer);
23931
23932           if (token->type == CPP_COMMA)
23933             {
23934               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23935               continue;
23936             }
23937           break;
23938         }
23939
23940       cp_parser_consume_semicolon_at_end_of_statement (parser);
23941       token = cp_lexer_peek_token (parser->lexer);
23942     }
23943
23944   if (token->keyword == RID_AT_END)
23945     cp_parser_error (parser, "expected %<}%>");
23946
23947   /* Do not consume the RID_AT_END, so it will be read again as terminating
23948      the @interface of @implementation.  */ 
23949   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23950     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
23951     
23952   /* For historical reasons, we accept an optional semicolon.  */
23953   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23954     cp_lexer_consume_token (parser->lexer);
23955 }
23956
23957 /* Parse an Objective-C protocol declaration.  */
23958
23959 static void
23960 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23961 {
23962   tree proto, protorefs;
23963   cp_token *tok;
23964
23965   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23966   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23967     {
23968       tok = cp_lexer_peek_token (parser->lexer);
23969       error_at (tok->location, "identifier expected after %<@protocol%>");
23970       cp_parser_consume_semicolon_at_end_of_statement (parser);
23971       return;
23972     }
23973
23974   /* See if we have a forward declaration or a definition.  */
23975   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23976
23977   /* Try a forward declaration first.  */
23978   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23979     {
23980       while (true)
23981         {
23982           tree id;
23983           
23984           id = cp_parser_identifier (parser);
23985           if (id == error_mark_node)
23986             break;
23987           
23988           objc_declare_protocol (id, attributes);
23989           
23990           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23991             cp_lexer_consume_token (parser->lexer);
23992           else
23993             break;
23994         }
23995       cp_parser_consume_semicolon_at_end_of_statement (parser);
23996     }
23997
23998   /* Ok, we got a full-fledged definition (or at least should).  */
23999   else
24000     {
24001       proto = cp_parser_identifier (parser);
24002       protorefs = cp_parser_objc_protocol_refs_opt (parser);
24003       objc_start_protocol (proto, protorefs, attributes);
24004       cp_parser_objc_method_prototype_list (parser);
24005     }
24006 }
24007
24008 /* Parse an Objective-C superclass or category.  */
24009
24010 static void
24011 cp_parser_objc_superclass_or_category (cp_parser *parser, 
24012                                        bool iface_p,
24013                                        tree *super,
24014                                        tree *categ, bool *is_class_extension)
24015 {
24016   cp_token *next = cp_lexer_peek_token (parser->lexer);
24017
24018   *super = *categ = NULL_TREE;
24019   *is_class_extension = false;
24020   if (next->type == CPP_COLON)
24021     {
24022       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24023       *super = cp_parser_identifier (parser);
24024     }
24025   else if (next->type == CPP_OPEN_PAREN)
24026     {
24027       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24028
24029       /* If there is no category name, and this is an @interface, we
24030          have a class extension.  */
24031       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24032         {
24033           *categ = NULL_TREE;
24034           *is_class_extension = true;
24035         }
24036       else
24037         *categ = cp_parser_identifier (parser);
24038
24039       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24040     }
24041 }
24042
24043 /* Parse an Objective-C class interface.  */
24044
24045 static void
24046 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24047 {
24048   tree name, super, categ, protos;
24049   bool is_class_extension;
24050
24051   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
24052   name = cp_parser_identifier (parser);
24053   if (name == error_mark_node)
24054     {
24055       /* It's hard to recover because even if valid @interface stuff
24056          is to follow, we can't compile it (or validate it) if we
24057          don't even know which class it refers to.  Let's assume this
24058          was a stray '@interface' token in the stream and skip it.
24059       */
24060       return;
24061     }
24062   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24063                                          &is_class_extension);
24064   protos = cp_parser_objc_protocol_refs_opt (parser);
24065
24066   /* We have either a class or a category on our hands.  */
24067   if (categ || is_class_extension)
24068     objc_start_category_interface (name, categ, protos, attributes);
24069   else
24070     {
24071       objc_start_class_interface (name, super, protos, attributes);
24072       /* Handle instance variable declarations, if any.  */
24073       cp_parser_objc_class_ivars (parser);
24074       objc_continue_interface ();
24075     }
24076
24077   cp_parser_objc_method_prototype_list (parser);
24078 }
24079
24080 /* Parse an Objective-C class implementation.  */
24081
24082 static void
24083 cp_parser_objc_class_implementation (cp_parser* parser)
24084 {
24085   tree name, super, categ;
24086   bool is_class_extension;
24087
24088   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
24089   name = cp_parser_identifier (parser);
24090   if (name == error_mark_node)
24091     {
24092       /* It's hard to recover because even if valid @implementation
24093          stuff is to follow, we can't compile it (or validate it) if
24094          we don't even know which class it refers to.  Let's assume
24095          this was a stray '@implementation' token in the stream and
24096          skip it.
24097       */
24098       return;
24099     }
24100   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24101                                          &is_class_extension);
24102
24103   /* We have either a class or a category on our hands.  */
24104   if (categ)
24105     objc_start_category_implementation (name, categ);
24106   else
24107     {
24108       objc_start_class_implementation (name, super);
24109       /* Handle instance variable declarations, if any.  */
24110       cp_parser_objc_class_ivars (parser);
24111       objc_continue_implementation ();
24112     }
24113
24114   cp_parser_objc_method_definition_list (parser);
24115 }
24116
24117 /* Consume the @end token and finish off the implementation.  */
24118
24119 static void
24120 cp_parser_objc_end_implementation (cp_parser* parser)
24121 {
24122   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24123   objc_finish_implementation ();
24124 }
24125
24126 /* Parse an Objective-C declaration.  */
24127
24128 static void
24129 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24130 {
24131   /* Try to figure out what kind of declaration is present.  */
24132   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24133
24134   if (attributes)
24135     switch (kwd->keyword)
24136       {
24137         case RID_AT_ALIAS:
24138         case RID_AT_CLASS:
24139         case RID_AT_END:
24140           error_at (kwd->location, "attributes may not be specified before"
24141                     " the %<@%D%> Objective-C++ keyword",
24142                     kwd->u.value);
24143           attributes = NULL;
24144           break;
24145         case RID_AT_IMPLEMENTATION:
24146           warning_at (kwd->location, OPT_Wattributes,
24147                       "prefix attributes are ignored before %<@%D%>",
24148                       kwd->u.value);
24149           attributes = NULL;
24150         default:
24151           break;
24152       }
24153
24154   switch (kwd->keyword)
24155     {
24156     case RID_AT_ALIAS:
24157       cp_parser_objc_alias_declaration (parser);
24158       break;
24159     case RID_AT_CLASS:
24160       cp_parser_objc_class_declaration (parser);
24161       break;
24162     case RID_AT_PROTOCOL:
24163       cp_parser_objc_protocol_declaration (parser, attributes);
24164       break;
24165     case RID_AT_INTERFACE:
24166       cp_parser_objc_class_interface (parser, attributes);
24167       break;
24168     case RID_AT_IMPLEMENTATION:
24169       cp_parser_objc_class_implementation (parser);
24170       break;
24171     case RID_AT_END:
24172       cp_parser_objc_end_implementation (parser);
24173       break;
24174     default:
24175       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24176                 kwd->u.value);
24177       cp_parser_skip_to_end_of_block_or_statement (parser);
24178     }
24179 }
24180
24181 /* Parse an Objective-C try-catch-finally statement.
24182
24183    objc-try-catch-finally-stmt:
24184      @try compound-statement objc-catch-clause-seq [opt]
24185        objc-finally-clause [opt]
24186
24187    objc-catch-clause-seq:
24188      objc-catch-clause objc-catch-clause-seq [opt]
24189
24190    objc-catch-clause:
24191      @catch ( objc-exception-declaration ) compound-statement
24192
24193    objc-finally-clause:
24194      @finally compound-statement
24195
24196    objc-exception-declaration:
24197      parameter-declaration
24198      '...'
24199
24200    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24201
24202    Returns NULL_TREE.
24203
24204    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24205    for C.  Keep them in sync.  */   
24206
24207 static tree
24208 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24209 {
24210   location_t location;
24211   tree stmt;
24212
24213   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24214   location = cp_lexer_peek_token (parser->lexer)->location;
24215   objc_maybe_warn_exceptions (location);
24216   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24217      node, lest it get absorbed into the surrounding block.  */
24218   stmt = push_stmt_list ();
24219   cp_parser_compound_statement (parser, NULL, false, false);
24220   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24221
24222   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24223     {
24224       cp_parameter_declarator *parm;
24225       tree parameter_declaration = error_mark_node;
24226       bool seen_open_paren = false;
24227
24228       cp_lexer_consume_token (parser->lexer);
24229       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24230         seen_open_paren = true;
24231       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24232         {
24233           /* We have "@catch (...)" (where the '...' are literally
24234              what is in the code).  Skip the '...'.
24235              parameter_declaration is set to NULL_TREE, and
24236              objc_being_catch_clauses() knows that that means
24237              '...'.  */
24238           cp_lexer_consume_token (parser->lexer);
24239           parameter_declaration = NULL_TREE;
24240         }
24241       else
24242         {
24243           /* We have "@catch (NSException *exception)" or something
24244              like that.  Parse the parameter declaration.  */
24245           parm = cp_parser_parameter_declaration (parser, false, NULL);
24246           if (parm == NULL)
24247             parameter_declaration = error_mark_node;
24248           else
24249             parameter_declaration = grokdeclarator (parm->declarator,
24250                                                     &parm->decl_specifiers,
24251                                                     PARM, /*initialized=*/0,
24252                                                     /*attrlist=*/NULL);
24253         }
24254       if (seen_open_paren)
24255         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24256       else
24257         {
24258           /* If there was no open parenthesis, we are recovering from
24259              an error, and we are trying to figure out what mistake
24260              the user has made.  */
24261
24262           /* If there is an immediate closing parenthesis, the user
24263              probably forgot the opening one (ie, they typed "@catch
24264              NSException *e)".  Parse the closing parenthesis and keep
24265              going.  */
24266           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24267             cp_lexer_consume_token (parser->lexer);
24268           
24269           /* If these is no immediate closing parenthesis, the user
24270              probably doesn't know that parenthesis are required at
24271              all (ie, they typed "@catch NSException *e").  So, just
24272              forget about the closing parenthesis and keep going.  */
24273         }
24274       objc_begin_catch_clause (parameter_declaration);
24275       cp_parser_compound_statement (parser, NULL, false, false);
24276       objc_finish_catch_clause ();
24277     }
24278   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24279     {
24280       cp_lexer_consume_token (parser->lexer);
24281       location = cp_lexer_peek_token (parser->lexer)->location;
24282       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24283          node, lest it get absorbed into the surrounding block.  */
24284       stmt = push_stmt_list ();
24285       cp_parser_compound_statement (parser, NULL, false, false);
24286       objc_build_finally_clause (location, pop_stmt_list (stmt));
24287     }
24288
24289   return objc_finish_try_stmt ();
24290 }
24291
24292 /* Parse an Objective-C synchronized statement.
24293
24294    objc-synchronized-stmt:
24295      @synchronized ( expression ) compound-statement
24296
24297    Returns NULL_TREE.  */
24298
24299 static tree
24300 cp_parser_objc_synchronized_statement (cp_parser *parser)
24301 {
24302   location_t location;
24303   tree lock, stmt;
24304
24305   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24306
24307   location = cp_lexer_peek_token (parser->lexer)->location;
24308   objc_maybe_warn_exceptions (location);
24309   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24310   lock = cp_parser_expression (parser, false, NULL);
24311   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24312
24313   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24314      node, lest it get absorbed into the surrounding block.  */
24315   stmt = push_stmt_list ();
24316   cp_parser_compound_statement (parser, NULL, false, false);
24317
24318   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24319 }
24320
24321 /* Parse an Objective-C throw statement.
24322
24323    objc-throw-stmt:
24324      @throw assignment-expression [opt] ;
24325
24326    Returns a constructed '@throw' statement.  */
24327
24328 static tree
24329 cp_parser_objc_throw_statement (cp_parser *parser)
24330 {
24331   tree expr = NULL_TREE;
24332   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24333
24334   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24335
24336   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24337     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24338
24339   cp_parser_consume_semicolon_at_end_of_statement (parser);
24340
24341   return objc_build_throw_stmt (loc, expr);
24342 }
24343
24344 /* Parse an Objective-C statement.  */
24345
24346 static tree
24347 cp_parser_objc_statement (cp_parser * parser)
24348 {
24349   /* Try to figure out what kind of declaration is present.  */
24350   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24351
24352   switch (kwd->keyword)
24353     {
24354     case RID_AT_TRY:
24355       return cp_parser_objc_try_catch_finally_statement (parser);
24356     case RID_AT_SYNCHRONIZED:
24357       return cp_parser_objc_synchronized_statement (parser);
24358     case RID_AT_THROW:
24359       return cp_parser_objc_throw_statement (parser);
24360     default:
24361       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24362                kwd->u.value);
24363       cp_parser_skip_to_end_of_block_or_statement (parser);
24364     }
24365
24366   return error_mark_node;
24367 }
24368
24369 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24370    look ahead to see if an objc keyword follows the attributes.  This
24371    is to detect the use of prefix attributes on ObjC @interface and 
24372    @protocol.  */
24373
24374 static bool
24375 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24376 {
24377   cp_lexer_save_tokens (parser->lexer);
24378   *attrib = cp_parser_attributes_opt (parser);
24379   gcc_assert (*attrib);
24380   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24381     {
24382       cp_lexer_commit_tokens (parser->lexer);
24383       return true;
24384     }
24385   cp_lexer_rollback_tokens (parser->lexer);
24386   return false;  
24387 }
24388
24389 /* This routine is a minimal replacement for
24390    c_parser_struct_declaration () used when parsing the list of
24391    types/names or ObjC++ properties.  For example, when parsing the
24392    code
24393
24394    @property (readonly) int a, b, c;
24395
24396    this function is responsible for parsing "int a, int b, int c" and
24397    returning the declarations as CHAIN of DECLs.
24398
24399    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24400    similar parsing.  */
24401 static tree
24402 cp_parser_objc_struct_declaration (cp_parser *parser)
24403 {
24404   tree decls = NULL_TREE;
24405   cp_decl_specifier_seq declspecs;
24406   int decl_class_or_enum_p;
24407   tree prefix_attributes;
24408
24409   cp_parser_decl_specifier_seq (parser,
24410                                 CP_PARSER_FLAGS_NONE,
24411                                 &declspecs,
24412                                 &decl_class_or_enum_p);
24413
24414   if (declspecs.type == error_mark_node)
24415     return error_mark_node;
24416
24417   /* auto, register, static, extern, mutable.  */
24418   if (declspecs.storage_class != sc_none)
24419     {
24420       cp_parser_error (parser, "invalid type for property");
24421       declspecs.storage_class = sc_none;
24422     }
24423   
24424   /* __thread.  */
24425   if (declspecs.specs[(int) ds_thread])
24426     {
24427       cp_parser_error (parser, "invalid type for property");
24428       declspecs.specs[(int) ds_thread] = 0;
24429     }
24430   
24431   /* typedef.  */
24432   if (declspecs.specs[(int) ds_typedef])
24433     {
24434       cp_parser_error (parser, "invalid type for property");
24435       declspecs.specs[(int) ds_typedef] = 0;
24436     }
24437
24438   prefix_attributes = declspecs.attributes;
24439   declspecs.attributes = NULL_TREE;
24440
24441   /* Keep going until we hit the `;' at the end of the declaration. */
24442   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24443     {
24444       tree attributes, first_attribute, decl;
24445       cp_declarator *declarator;
24446       cp_token *token;
24447
24448       /* Parse the declarator.  */
24449       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24450                                          NULL, NULL, false);
24451
24452       /* Look for attributes that apply to the ivar.  */
24453       attributes = cp_parser_attributes_opt (parser);
24454       /* Remember which attributes are prefix attributes and
24455          which are not.  */
24456       first_attribute = attributes;
24457       /* Combine the attributes.  */
24458       attributes = chainon (prefix_attributes, attributes);
24459       
24460       decl = grokfield (declarator, &declspecs,
24461                         NULL_TREE, /*init_const_expr_p=*/false,
24462                         NULL_TREE, attributes);
24463
24464       if (decl == error_mark_node || decl == NULL_TREE)
24465         return error_mark_node;
24466       
24467       /* Reset PREFIX_ATTRIBUTES.  */
24468       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24469         attributes = TREE_CHAIN (attributes);
24470       if (attributes)
24471         TREE_CHAIN (attributes) = NULL_TREE;
24472
24473       DECL_CHAIN (decl) = decls;
24474       decls = decl;
24475
24476       token = cp_lexer_peek_token (parser->lexer);
24477       if (token->type == CPP_COMMA)
24478         {
24479           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24480           continue;
24481         }
24482       else
24483         break;
24484     }
24485   return decls;
24486 }
24487
24488 /* Parse an Objective-C @property declaration.  The syntax is:
24489
24490    objc-property-declaration:
24491      '@property' objc-property-attributes[opt] struct-declaration ;
24492
24493    objc-property-attributes:
24494     '(' objc-property-attribute-list ')'
24495
24496    objc-property-attribute-list:
24497      objc-property-attribute
24498      objc-property-attribute-list, objc-property-attribute
24499
24500    objc-property-attribute
24501      'getter' = identifier
24502      'setter' = identifier
24503      'readonly'
24504      'readwrite'
24505      'assign'
24506      'retain'
24507      'copy'
24508      'nonatomic'
24509
24510   For example:
24511     @property NSString *name;
24512     @property (readonly) id object;
24513     @property (retain, nonatomic, getter=getTheName) id name;
24514     @property int a, b, c;
24515
24516    PS: This function is identical to
24517    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24518 static void 
24519 cp_parser_objc_at_property_declaration (cp_parser *parser)
24520 {
24521   /* The following variables hold the attributes of the properties as
24522      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24523      seen.  When we see an attribute, we set them to 'true' (if they
24524      are boolean properties) or to the identifier (if they have an
24525      argument, ie, for getter and setter).  Note that here we only
24526      parse the list of attributes, check the syntax and accumulate the
24527      attributes that we find.  objc_add_property_declaration() will
24528      then process the information.  */
24529   bool property_assign = false;
24530   bool property_copy = false;
24531   tree property_getter_ident = NULL_TREE;
24532   bool property_nonatomic = false;
24533   bool property_readonly = false;
24534   bool property_readwrite = false;
24535   bool property_retain = false;
24536   tree property_setter_ident = NULL_TREE;
24537
24538   /* 'properties' is the list of properties that we read.  Usually a
24539      single one, but maybe more (eg, in "@property int a, b, c;" there
24540      are three).  */
24541   tree properties;
24542   location_t loc;
24543
24544   loc = cp_lexer_peek_token (parser->lexer)->location;
24545
24546   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24547
24548   /* Parse the optional attribute list...  */
24549   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24550     {
24551       /* Eat the '('.  */
24552       cp_lexer_consume_token (parser->lexer);
24553
24554       while (true)
24555         {
24556           bool syntax_error = false;
24557           cp_token *token = cp_lexer_peek_token (parser->lexer);
24558           enum rid keyword;
24559
24560           if (token->type != CPP_NAME)
24561             {
24562               cp_parser_error (parser, "expected identifier");
24563               break;
24564             }
24565           keyword = C_RID_CODE (token->u.value);
24566           cp_lexer_consume_token (parser->lexer);
24567           switch (keyword)
24568             {
24569             case RID_ASSIGN:    property_assign = true;    break;
24570             case RID_COPY:      property_copy = true;      break;
24571             case RID_NONATOMIC: property_nonatomic = true; break;
24572             case RID_READONLY:  property_readonly = true;  break;
24573             case RID_READWRITE: property_readwrite = true; break;
24574             case RID_RETAIN:    property_retain = true;    break;
24575
24576             case RID_GETTER:
24577             case RID_SETTER:
24578               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24579                 {
24580                   if (keyword == RID_GETTER)
24581                     cp_parser_error (parser,
24582                                      "missing %<=%> (after %<getter%> attribute)");
24583                   else
24584                     cp_parser_error (parser,
24585                                      "missing %<=%> (after %<setter%> attribute)");
24586                   syntax_error = true;
24587                   break;
24588                 }
24589               cp_lexer_consume_token (parser->lexer); /* eat the = */
24590               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24591                 {
24592                   cp_parser_error (parser, "expected identifier");
24593                   syntax_error = true;
24594                   break;
24595                 }
24596               if (keyword == RID_SETTER)
24597                 {
24598                   if (property_setter_ident != NULL_TREE)
24599                     {
24600                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24601                       cp_lexer_consume_token (parser->lexer);
24602                     }
24603                   else
24604                     property_setter_ident = cp_parser_objc_selector (parser);
24605                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24606                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24607                   else
24608                     cp_lexer_consume_token (parser->lexer);
24609                 }
24610               else
24611                 {
24612                   if (property_getter_ident != NULL_TREE)
24613                     {
24614                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24615                       cp_lexer_consume_token (parser->lexer);
24616                     }
24617                   else
24618                     property_getter_ident = cp_parser_objc_selector (parser);
24619                 }
24620               break;
24621             default:
24622               cp_parser_error (parser, "unknown property attribute");
24623               syntax_error = true;
24624               break;
24625             }
24626
24627           if (syntax_error)
24628             break;
24629
24630           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24631             cp_lexer_consume_token (parser->lexer);
24632           else
24633             break;
24634         }
24635
24636       /* FIXME: "@property (setter, assign);" will generate a spurious
24637          "error: expected â€˜)’ before â€˜,’ token".  This is because
24638          cp_parser_require, unlike the C counterpart, will produce an
24639          error even if we are in error recovery.  */
24640       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24641         {
24642           cp_parser_skip_to_closing_parenthesis (parser,
24643                                                  /*recovering=*/true,
24644                                                  /*or_comma=*/false,
24645                                                  /*consume_paren=*/true);
24646         }
24647     }
24648
24649   /* ... and the property declaration(s).  */
24650   properties = cp_parser_objc_struct_declaration (parser);
24651
24652   if (properties == error_mark_node)
24653     {
24654       cp_parser_skip_to_end_of_statement (parser);
24655       /* If the next token is now a `;', consume it.  */
24656       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24657         cp_lexer_consume_token (parser->lexer);
24658       return;
24659     }
24660
24661   if (properties == NULL_TREE)
24662     cp_parser_error (parser, "expected identifier");
24663   else
24664     {
24665       /* Comma-separated properties are chained together in
24666          reverse order; add them one by one.  */
24667       properties = nreverse (properties);
24668       
24669       for (; properties; properties = TREE_CHAIN (properties))
24670         objc_add_property_declaration (loc, copy_node (properties),
24671                                        property_readonly, property_readwrite,
24672                                        property_assign, property_retain,
24673                                        property_copy, property_nonatomic,
24674                                        property_getter_ident, property_setter_ident);
24675     }
24676   
24677   cp_parser_consume_semicolon_at_end_of_statement (parser);
24678 }
24679
24680 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24681
24682    objc-synthesize-declaration:
24683      @synthesize objc-synthesize-identifier-list ;
24684
24685    objc-synthesize-identifier-list:
24686      objc-synthesize-identifier
24687      objc-synthesize-identifier-list, objc-synthesize-identifier
24688
24689    objc-synthesize-identifier
24690      identifier
24691      identifier = identifier
24692
24693   For example:
24694     @synthesize MyProperty;
24695     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24696
24697   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24698   for C.  Keep them in sync.
24699 */
24700 static void 
24701 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24702 {
24703   tree list = NULL_TREE;
24704   location_t loc;
24705   loc = cp_lexer_peek_token (parser->lexer)->location;
24706
24707   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24708   while (true)
24709     {
24710       tree property, ivar;
24711       property = cp_parser_identifier (parser);
24712       if (property == error_mark_node)
24713         {
24714           cp_parser_consume_semicolon_at_end_of_statement (parser);
24715           return;
24716         }
24717       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24718         {
24719           cp_lexer_consume_token (parser->lexer);
24720           ivar = cp_parser_identifier (parser);
24721           if (ivar == error_mark_node)
24722             {
24723               cp_parser_consume_semicolon_at_end_of_statement (parser);
24724               return;
24725             }
24726         }
24727       else
24728         ivar = NULL_TREE;
24729       list = chainon (list, build_tree_list (ivar, property));
24730       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24731         cp_lexer_consume_token (parser->lexer);
24732       else
24733         break;
24734     }
24735   cp_parser_consume_semicolon_at_end_of_statement (parser);
24736   objc_add_synthesize_declaration (loc, list);
24737 }
24738
24739 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24740
24741    objc-dynamic-declaration:
24742      @dynamic identifier-list ;
24743
24744    For example:
24745      @dynamic MyProperty;
24746      @dynamic MyProperty, AnotherProperty;
24747
24748   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24749   for C.  Keep them in sync.
24750 */
24751 static void 
24752 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24753 {
24754   tree list = NULL_TREE;
24755   location_t loc;
24756   loc = cp_lexer_peek_token (parser->lexer)->location;
24757
24758   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24759   while (true)
24760     {
24761       tree property;
24762       property = cp_parser_identifier (parser);
24763       if (property == error_mark_node)
24764         {
24765           cp_parser_consume_semicolon_at_end_of_statement (parser);
24766           return;
24767         }
24768       list = chainon (list, build_tree_list (NULL, property));
24769       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24770         cp_lexer_consume_token (parser->lexer);
24771       else
24772         break;
24773     }
24774   cp_parser_consume_semicolon_at_end_of_statement (parser);
24775   objc_add_dynamic_declaration (loc, list);
24776 }
24777
24778 \f
24779 /* OpenMP 2.5 parsing routines.  */
24780
24781 /* Returns name of the next clause.
24782    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24783    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24784    returned and the token is consumed.  */
24785
24786 static pragma_omp_clause
24787 cp_parser_omp_clause_name (cp_parser *parser)
24788 {
24789   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24790
24791   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24792     result = PRAGMA_OMP_CLAUSE_IF;
24793   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24794     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24795   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24796     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24797   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24798     {
24799       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24800       const char *p = IDENTIFIER_POINTER (id);
24801
24802       switch (p[0])
24803         {
24804         case 'c':
24805           if (!strcmp ("collapse", p))
24806             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24807           else if (!strcmp ("copyin", p))
24808             result = PRAGMA_OMP_CLAUSE_COPYIN;
24809           else if (!strcmp ("copyprivate", p))
24810             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24811           break;
24812         case 'f':
24813           if (!strcmp ("final", p))
24814             result = PRAGMA_OMP_CLAUSE_FINAL;
24815           else if (!strcmp ("firstprivate", p))
24816             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24817           break;
24818         case 'l':
24819           if (!strcmp ("lastprivate", p))
24820             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24821           break;
24822         case 'm':
24823           if (!strcmp ("mergeable", p))
24824             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24825           break;
24826         case 'n':
24827           if (!strcmp ("nowait", p))
24828             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24829           else if (!strcmp ("num_threads", p))
24830             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24831           break;
24832         case 'o':
24833           if (!strcmp ("ordered", p))
24834             result = PRAGMA_OMP_CLAUSE_ORDERED;
24835           break;
24836         case 'r':
24837           if (!strcmp ("reduction", p))
24838             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24839           break;
24840         case 's':
24841           if (!strcmp ("schedule", p))
24842             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24843           else if (!strcmp ("shared", p))
24844             result = PRAGMA_OMP_CLAUSE_SHARED;
24845           break;
24846         case 'u':
24847           if (!strcmp ("untied", p))
24848             result = PRAGMA_OMP_CLAUSE_UNTIED;
24849           break;
24850         }
24851     }
24852
24853   if (result != PRAGMA_OMP_CLAUSE_NONE)
24854     cp_lexer_consume_token (parser->lexer);
24855
24856   return result;
24857 }
24858
24859 /* Validate that a clause of the given type does not already exist.  */
24860
24861 static void
24862 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24863                            const char *name, location_t location)
24864 {
24865   tree c;
24866
24867   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24868     if (OMP_CLAUSE_CODE (c) == code)
24869       {
24870         error_at (location, "too many %qs clauses", name);
24871         break;
24872       }
24873 }
24874
24875 /* OpenMP 2.5:
24876    variable-list:
24877      identifier
24878      variable-list , identifier
24879
24880    In addition, we match a closing parenthesis.  An opening parenthesis
24881    will have been consumed by the caller.
24882
24883    If KIND is nonzero, create the appropriate node and install the decl
24884    in OMP_CLAUSE_DECL and add the node to the head of the list.
24885
24886    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24887    return the list created.  */
24888
24889 static tree
24890 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24891                                 tree list)
24892 {
24893   cp_token *token;
24894   while (1)
24895     {
24896       tree name, decl;
24897
24898       token = cp_lexer_peek_token (parser->lexer);
24899       name = cp_parser_id_expression (parser, /*template_p=*/false,
24900                                       /*check_dependency_p=*/true,
24901                                       /*template_p=*/NULL,
24902                                       /*declarator_p=*/false,
24903                                       /*optional_p=*/false);
24904       if (name == error_mark_node)
24905         goto skip_comma;
24906
24907       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24908       if (decl == error_mark_node)
24909         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24910                                      token->location);
24911       else if (kind != 0)
24912         {
24913           tree u = build_omp_clause (token->location, kind);
24914           OMP_CLAUSE_DECL (u) = decl;
24915           OMP_CLAUSE_CHAIN (u) = list;
24916           list = u;
24917         }
24918       else
24919         list = tree_cons (decl, NULL_TREE, list);
24920
24921     get_comma:
24922       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24923         break;
24924       cp_lexer_consume_token (parser->lexer);
24925     }
24926
24927   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24928     {
24929       int ending;
24930
24931       /* Try to resync to an unnested comma.  Copied from
24932          cp_parser_parenthesized_expression_list.  */
24933     skip_comma:
24934       ending = cp_parser_skip_to_closing_parenthesis (parser,
24935                                                       /*recovering=*/true,
24936                                                       /*or_comma=*/true,
24937                                                       /*consume_paren=*/true);
24938       if (ending < 0)
24939         goto get_comma;
24940     }
24941
24942   return list;
24943 }
24944
24945 /* Similarly, but expect leading and trailing parenthesis.  This is a very
24946    common case for omp clauses.  */
24947
24948 static tree
24949 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24950 {
24951   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24952     return cp_parser_omp_var_list_no_open (parser, kind, list);
24953   return list;
24954 }
24955
24956 /* OpenMP 3.0:
24957    collapse ( constant-expression ) */
24958
24959 static tree
24960 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24961 {
24962   tree c, num;
24963   location_t loc;
24964   HOST_WIDE_INT n;
24965
24966   loc = cp_lexer_peek_token (parser->lexer)->location;
24967   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24968     return list;
24969
24970   num = cp_parser_constant_expression (parser, false, NULL);
24971
24972   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24973     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24974                                            /*or_comma=*/false,
24975                                            /*consume_paren=*/true);
24976
24977   if (num == error_mark_node)
24978     return list;
24979   num = fold_non_dependent_expr (num);
24980   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24981       || !host_integerp (num, 0)
24982       || (n = tree_low_cst (num, 0)) <= 0
24983       || (int) n != n)
24984     {
24985       error_at (loc, "collapse argument needs positive constant integer expression");
24986       return list;
24987     }
24988
24989   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24990   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24991   OMP_CLAUSE_CHAIN (c) = list;
24992   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24993
24994   return c;
24995 }
24996
24997 /* OpenMP 2.5:
24998    default ( shared | none ) */
24999
25000 static tree
25001 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25002 {
25003   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25004   tree c;
25005
25006   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25007     return list;
25008   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25009     {
25010       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25011       const char *p = IDENTIFIER_POINTER (id);
25012
25013       switch (p[0])
25014         {
25015         case 'n':
25016           if (strcmp ("none", p) != 0)
25017             goto invalid_kind;
25018           kind = OMP_CLAUSE_DEFAULT_NONE;
25019           break;
25020
25021         case 's':
25022           if (strcmp ("shared", p) != 0)
25023             goto invalid_kind;
25024           kind = OMP_CLAUSE_DEFAULT_SHARED;
25025           break;
25026
25027         default:
25028           goto invalid_kind;
25029         }
25030
25031       cp_lexer_consume_token (parser->lexer);
25032     }
25033   else
25034     {
25035     invalid_kind:
25036       cp_parser_error (parser, "expected %<none%> or %<shared%>");
25037     }
25038
25039   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25040     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25041                                            /*or_comma=*/false,
25042                                            /*consume_paren=*/true);
25043
25044   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25045     return list;
25046
25047   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25048   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25049   OMP_CLAUSE_CHAIN (c) = list;
25050   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25051
25052   return c;
25053 }
25054
25055 /* OpenMP 3.1:
25056    final ( expression ) */
25057
25058 static tree
25059 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25060 {
25061   tree t, c;
25062
25063   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25064     return list;
25065
25066   t = cp_parser_condition (parser);
25067
25068   if (t == error_mark_node
25069       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25070     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25071                                            /*or_comma=*/false,
25072                                            /*consume_paren=*/true);
25073
25074   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25075
25076   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25077   OMP_CLAUSE_FINAL_EXPR (c) = t;
25078   OMP_CLAUSE_CHAIN (c) = list;
25079
25080   return c;
25081 }
25082
25083 /* OpenMP 2.5:
25084    if ( expression ) */
25085
25086 static tree
25087 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25088 {
25089   tree t, c;
25090
25091   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25092     return list;
25093
25094   t = cp_parser_condition (parser);
25095
25096   if (t == error_mark_node
25097       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25098     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25099                                            /*or_comma=*/false,
25100                                            /*consume_paren=*/true);
25101
25102   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25103
25104   c = build_omp_clause (location, OMP_CLAUSE_IF);
25105   OMP_CLAUSE_IF_EXPR (c) = t;
25106   OMP_CLAUSE_CHAIN (c) = list;
25107
25108   return c;
25109 }
25110
25111 /* OpenMP 3.1:
25112    mergeable */
25113
25114 static tree
25115 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25116                                 tree list, location_t location)
25117 {
25118   tree c;
25119
25120   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25121                              location);
25122
25123   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25124   OMP_CLAUSE_CHAIN (c) = list;
25125   return c;
25126 }
25127
25128 /* OpenMP 2.5:
25129    nowait */
25130
25131 static tree
25132 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25133                              tree list, location_t location)
25134 {
25135   tree c;
25136
25137   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25138
25139   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25140   OMP_CLAUSE_CHAIN (c) = list;
25141   return c;
25142 }
25143
25144 /* OpenMP 2.5:
25145    num_threads ( expression ) */
25146
25147 static tree
25148 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25149                                   location_t location)
25150 {
25151   tree t, c;
25152
25153   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25154     return list;
25155
25156   t = cp_parser_expression (parser, false, NULL);
25157
25158   if (t == error_mark_node
25159       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25160     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25161                                            /*or_comma=*/false,
25162                                            /*consume_paren=*/true);
25163
25164   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25165                              "num_threads", location);
25166
25167   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25168   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25169   OMP_CLAUSE_CHAIN (c) = list;
25170
25171   return c;
25172 }
25173
25174 /* OpenMP 2.5:
25175    ordered */
25176
25177 static tree
25178 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25179                               tree list, location_t location)
25180 {
25181   tree c;
25182
25183   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25184                              "ordered", location);
25185
25186   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25187   OMP_CLAUSE_CHAIN (c) = list;
25188   return c;
25189 }
25190
25191 /* OpenMP 2.5:
25192    reduction ( reduction-operator : variable-list )
25193
25194    reduction-operator:
25195      One of: + * - & ^ | && ||
25196
25197    OpenMP 3.1:
25198
25199    reduction-operator:
25200      One of: + * - & ^ | && || min max  */
25201
25202 static tree
25203 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25204 {
25205   enum tree_code code;
25206   tree nlist, c;
25207
25208   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25209     return list;
25210
25211   switch (cp_lexer_peek_token (parser->lexer)->type)
25212     {
25213     case CPP_PLUS:
25214       code = PLUS_EXPR;
25215       break;
25216     case CPP_MULT:
25217       code = MULT_EXPR;
25218       break;
25219     case CPP_MINUS:
25220       code = MINUS_EXPR;
25221       break;
25222     case CPP_AND:
25223       code = BIT_AND_EXPR;
25224       break;
25225     case CPP_XOR:
25226       code = BIT_XOR_EXPR;
25227       break;
25228     case CPP_OR:
25229       code = BIT_IOR_EXPR;
25230       break;
25231     case CPP_AND_AND:
25232       code = TRUTH_ANDIF_EXPR;
25233       break;
25234     case CPP_OR_OR:
25235       code = TRUTH_ORIF_EXPR;
25236       break;
25237     case CPP_NAME:
25238       {
25239         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25240         const char *p = IDENTIFIER_POINTER (id);
25241
25242         if (strcmp (p, "min") == 0)
25243           {
25244             code = MIN_EXPR;
25245             break;
25246           }
25247         if (strcmp (p, "max") == 0)
25248           {
25249             code = MAX_EXPR;
25250             break;
25251           }
25252       }
25253       /* FALLTHROUGH */
25254     default:
25255       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25256                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25257     resync_fail:
25258       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25259                                              /*or_comma=*/false,
25260                                              /*consume_paren=*/true);
25261       return list;
25262     }
25263   cp_lexer_consume_token (parser->lexer);
25264
25265   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25266     goto resync_fail;
25267
25268   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25269   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25270     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25271
25272   return nlist;
25273 }
25274
25275 /* OpenMP 2.5:
25276    schedule ( schedule-kind )
25277    schedule ( schedule-kind , expression )
25278
25279    schedule-kind:
25280      static | dynamic | guided | runtime | auto  */
25281
25282 static tree
25283 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25284 {
25285   tree c, t;
25286
25287   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25288     return list;
25289
25290   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25291
25292   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25293     {
25294       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25295       const char *p = IDENTIFIER_POINTER (id);
25296
25297       switch (p[0])
25298         {
25299         case 'd':
25300           if (strcmp ("dynamic", p) != 0)
25301             goto invalid_kind;
25302           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25303           break;
25304
25305         case 'g':
25306           if (strcmp ("guided", p) != 0)
25307             goto invalid_kind;
25308           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25309           break;
25310
25311         case 'r':
25312           if (strcmp ("runtime", p) != 0)
25313             goto invalid_kind;
25314           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25315           break;
25316
25317         default:
25318           goto invalid_kind;
25319         }
25320     }
25321   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25322     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25323   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25324     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25325   else
25326     goto invalid_kind;
25327   cp_lexer_consume_token (parser->lexer);
25328
25329   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25330     {
25331       cp_token *token;
25332       cp_lexer_consume_token (parser->lexer);
25333
25334       token = cp_lexer_peek_token (parser->lexer);
25335       t = cp_parser_assignment_expression (parser, false, NULL);
25336
25337       if (t == error_mark_node)
25338         goto resync_fail;
25339       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25340         error_at (token->location, "schedule %<runtime%> does not take "
25341                   "a %<chunk_size%> parameter");
25342       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25343         error_at (token->location, "schedule %<auto%> does not take "
25344                   "a %<chunk_size%> parameter");
25345       else
25346         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25347
25348       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25349         goto resync_fail;
25350     }
25351   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25352     goto resync_fail;
25353
25354   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25355   OMP_CLAUSE_CHAIN (c) = list;
25356   return c;
25357
25358  invalid_kind:
25359   cp_parser_error (parser, "invalid schedule kind");
25360  resync_fail:
25361   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25362                                          /*or_comma=*/false,
25363                                          /*consume_paren=*/true);
25364   return list;
25365 }
25366
25367 /* OpenMP 3.0:
25368    untied */
25369
25370 static tree
25371 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25372                              tree list, location_t location)
25373 {
25374   tree c;
25375
25376   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25377
25378   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25379   OMP_CLAUSE_CHAIN (c) = list;
25380   return c;
25381 }
25382
25383 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25384    is a bitmask in MASK.  Return the list of clauses found; the result
25385    of clause default goes in *pdefault.  */
25386
25387 static tree
25388 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25389                            const char *where, cp_token *pragma_tok)
25390 {
25391   tree clauses = NULL;
25392   bool first = true;
25393   cp_token *token = NULL;
25394
25395   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25396     {
25397       pragma_omp_clause c_kind;
25398       const char *c_name;
25399       tree prev = clauses;
25400
25401       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25402         cp_lexer_consume_token (parser->lexer);
25403
25404       token = cp_lexer_peek_token (parser->lexer);
25405       c_kind = cp_parser_omp_clause_name (parser);
25406       first = false;
25407
25408       switch (c_kind)
25409         {
25410         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25411           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25412                                                    token->location);
25413           c_name = "collapse";
25414           break;
25415         case PRAGMA_OMP_CLAUSE_COPYIN:
25416           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25417           c_name = "copyin";
25418           break;
25419         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25420           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25421                                             clauses);
25422           c_name = "copyprivate";
25423           break;
25424         case PRAGMA_OMP_CLAUSE_DEFAULT:
25425           clauses = cp_parser_omp_clause_default (parser, clauses,
25426                                                   token->location);
25427           c_name = "default";
25428           break;
25429         case PRAGMA_OMP_CLAUSE_FINAL:
25430           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25431           c_name = "final";
25432           break;
25433         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25434           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25435                                             clauses);
25436           c_name = "firstprivate";
25437           break;
25438         case PRAGMA_OMP_CLAUSE_IF:
25439           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25440           c_name = "if";
25441           break;
25442         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25443           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25444                                             clauses);
25445           c_name = "lastprivate";
25446           break;
25447         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25448           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25449                                                     token->location);
25450           c_name = "mergeable";
25451           break;
25452         case PRAGMA_OMP_CLAUSE_NOWAIT:
25453           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25454           c_name = "nowait";
25455           break;
25456         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25457           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25458                                                       token->location);
25459           c_name = "num_threads";
25460           break;
25461         case PRAGMA_OMP_CLAUSE_ORDERED:
25462           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25463                                                   token->location);
25464           c_name = "ordered";
25465           break;
25466         case PRAGMA_OMP_CLAUSE_PRIVATE:
25467           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25468                                             clauses);
25469           c_name = "private";
25470           break;
25471         case PRAGMA_OMP_CLAUSE_REDUCTION:
25472           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25473           c_name = "reduction";
25474           break;
25475         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25476           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25477                                                    token->location);
25478           c_name = "schedule";
25479           break;
25480         case PRAGMA_OMP_CLAUSE_SHARED:
25481           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25482                                             clauses);
25483           c_name = "shared";
25484           break;
25485         case PRAGMA_OMP_CLAUSE_UNTIED:
25486           clauses = cp_parser_omp_clause_untied (parser, clauses,
25487                                                  token->location);
25488           c_name = "nowait";
25489           break;
25490         default:
25491           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25492           goto saw_error;
25493         }
25494
25495       if (((mask >> c_kind) & 1) == 0)
25496         {
25497           /* Remove the invalid clause(s) from the list to avoid
25498              confusing the rest of the compiler.  */
25499           clauses = prev;
25500           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25501         }
25502     }
25503  saw_error:
25504   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25505   return finish_omp_clauses (clauses);
25506 }
25507
25508 /* OpenMP 2.5:
25509    structured-block:
25510      statement
25511
25512    In practice, we're also interested in adding the statement to an
25513    outer node.  So it is convenient if we work around the fact that
25514    cp_parser_statement calls add_stmt.  */
25515
25516 static unsigned
25517 cp_parser_begin_omp_structured_block (cp_parser *parser)
25518 {
25519   unsigned save = parser->in_statement;
25520
25521   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25522      This preserves the "not within loop or switch" style error messages
25523      for nonsense cases like
25524         void foo() {
25525         #pragma omp single
25526           break;
25527         }
25528   */
25529   if (parser->in_statement)
25530     parser->in_statement = IN_OMP_BLOCK;
25531
25532   return save;
25533 }
25534
25535 static void
25536 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25537 {
25538   parser->in_statement = save;
25539 }
25540
25541 static tree
25542 cp_parser_omp_structured_block (cp_parser *parser)
25543 {
25544   tree stmt = begin_omp_structured_block ();
25545   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25546
25547   cp_parser_statement (parser, NULL_TREE, false, NULL);
25548
25549   cp_parser_end_omp_structured_block (parser, save);
25550   return finish_omp_structured_block (stmt);
25551 }
25552
25553 /* OpenMP 2.5:
25554    # pragma omp atomic new-line
25555      expression-stmt
25556
25557    expression-stmt:
25558      x binop= expr | x++ | ++x | x-- | --x
25559    binop:
25560      +, *, -, /, &, ^, |, <<, >>
25561
25562   where x is an lvalue expression with scalar type.
25563
25564    OpenMP 3.1:
25565    # pragma omp atomic new-line
25566      update-stmt
25567
25568    # pragma omp atomic read new-line
25569      read-stmt
25570
25571    # pragma omp atomic write new-line
25572      write-stmt
25573
25574    # pragma omp atomic update new-line
25575      update-stmt
25576
25577    # pragma omp atomic capture new-line
25578      capture-stmt
25579
25580    # pragma omp atomic capture new-line
25581      capture-block
25582
25583    read-stmt:
25584      v = x
25585    write-stmt:
25586      x = expr
25587    update-stmt:
25588      expression-stmt | x = x binop expr
25589    capture-stmt:
25590      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25591    capture-block:
25592      { v = x; update-stmt; } | { update-stmt; v = x; }
25593
25594   where x and v are lvalue expressions with scalar type.  */
25595
25596 static void
25597 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25598 {
25599   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25600   tree rhs1 = NULL_TREE, orig_lhs;
25601   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25602   bool structured_block = false;
25603
25604   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25605     {
25606       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25607       const char *p = IDENTIFIER_POINTER (id);
25608
25609       if (!strcmp (p, "read"))
25610         code = OMP_ATOMIC_READ;
25611       else if (!strcmp (p, "write"))
25612         code = NOP_EXPR;
25613       else if (!strcmp (p, "update"))
25614         code = OMP_ATOMIC;
25615       else if (!strcmp (p, "capture"))
25616         code = OMP_ATOMIC_CAPTURE_NEW;
25617       else
25618         p = NULL;
25619       if (p)
25620         cp_lexer_consume_token (parser->lexer);
25621     }
25622   cp_parser_require_pragma_eol (parser, pragma_tok);
25623
25624   switch (code)
25625     {
25626     case OMP_ATOMIC_READ:
25627     case NOP_EXPR: /* atomic write */
25628       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25629                                       /*cast_p=*/false, NULL);
25630       if (v == error_mark_node)
25631         goto saw_error;
25632       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25633         goto saw_error;
25634       if (code == NOP_EXPR)
25635         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25636       else
25637         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25638                                           /*cast_p=*/false, NULL);
25639       if (lhs == error_mark_node)
25640         goto saw_error;
25641       if (code == NOP_EXPR)
25642         {
25643           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25644              opcode.  */
25645           code = OMP_ATOMIC;
25646           rhs = lhs;
25647           lhs = v;
25648           v = NULL_TREE;
25649         }
25650       goto done;
25651     case OMP_ATOMIC_CAPTURE_NEW:
25652       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25653         {
25654           cp_lexer_consume_token (parser->lexer);
25655           structured_block = true;
25656         }
25657       else
25658         {
25659           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25660                                           /*cast_p=*/false, NULL);
25661           if (v == error_mark_node)
25662             goto saw_error;
25663           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25664             goto saw_error;
25665         }
25666     default:
25667       break;
25668     }
25669
25670 restart:
25671   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25672                                     /*cast_p=*/false, NULL);
25673   orig_lhs = lhs;
25674   switch (TREE_CODE (lhs))
25675     {
25676     case ERROR_MARK:
25677       goto saw_error;
25678
25679     case POSTINCREMENT_EXPR:
25680       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25681         code = OMP_ATOMIC_CAPTURE_OLD;
25682       /* FALLTHROUGH */
25683     case PREINCREMENT_EXPR:
25684       lhs = TREE_OPERAND (lhs, 0);
25685       opcode = PLUS_EXPR;
25686       rhs = integer_one_node;
25687       break;
25688
25689     case POSTDECREMENT_EXPR:
25690       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25691         code = OMP_ATOMIC_CAPTURE_OLD;
25692       /* FALLTHROUGH */
25693     case PREDECREMENT_EXPR:
25694       lhs = TREE_OPERAND (lhs, 0);
25695       opcode = MINUS_EXPR;
25696       rhs = integer_one_node;
25697       break;
25698
25699     case COMPOUND_EXPR:
25700       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25701          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25702          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25703          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25704          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25705                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25706             == BOOLEAN_TYPE)
25707        /* Undo effects of boolean_increment for post {in,de}crement.  */
25708        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25709       /* FALLTHRU */
25710     case MODIFY_EXPR:
25711       if (TREE_CODE (lhs) == MODIFY_EXPR
25712          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25713         {
25714           /* Undo effects of boolean_increment.  */
25715           if (integer_onep (TREE_OPERAND (lhs, 1)))
25716             {
25717               /* This is pre or post increment.  */
25718               rhs = TREE_OPERAND (lhs, 1);
25719               lhs = TREE_OPERAND (lhs, 0);
25720               opcode = NOP_EXPR;
25721               if (code == OMP_ATOMIC_CAPTURE_NEW
25722                   && !structured_block
25723                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25724                 code = OMP_ATOMIC_CAPTURE_OLD;
25725               break;
25726             }
25727         }
25728       /* FALLTHRU */
25729     default:
25730       switch (cp_lexer_peek_token (parser->lexer)->type)
25731         {
25732         case CPP_MULT_EQ:
25733           opcode = MULT_EXPR;
25734           break;
25735         case CPP_DIV_EQ:
25736           opcode = TRUNC_DIV_EXPR;
25737           break;
25738         case CPP_PLUS_EQ:
25739           opcode = PLUS_EXPR;
25740           break;
25741         case CPP_MINUS_EQ:
25742           opcode = MINUS_EXPR;
25743           break;
25744         case CPP_LSHIFT_EQ:
25745           opcode = LSHIFT_EXPR;
25746           break;
25747         case CPP_RSHIFT_EQ:
25748           opcode = RSHIFT_EXPR;
25749           break;
25750         case CPP_AND_EQ:
25751           opcode = BIT_AND_EXPR;
25752           break;
25753         case CPP_OR_EQ:
25754           opcode = BIT_IOR_EXPR;
25755           break;
25756         case CPP_XOR_EQ:
25757           opcode = BIT_XOR_EXPR;
25758           break;
25759         case CPP_EQ:
25760           if (structured_block || code == OMP_ATOMIC)
25761             {
25762               enum cp_parser_prec oprec;
25763               cp_token *token;
25764               cp_lexer_consume_token (parser->lexer);
25765               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25766                                                  /*cast_p=*/false, NULL);
25767               if (rhs1 == error_mark_node)
25768                 goto saw_error;
25769               token = cp_lexer_peek_token (parser->lexer);
25770               switch (token->type)
25771                 {
25772                 case CPP_SEMICOLON:
25773                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25774                     {
25775                       code = OMP_ATOMIC_CAPTURE_OLD;
25776                       v = lhs;
25777                       lhs = NULL_TREE;
25778                       lhs1 = rhs1;
25779                       rhs1 = NULL_TREE;
25780                       cp_lexer_consume_token (parser->lexer);
25781                       goto restart;
25782                     }
25783                   cp_parser_error (parser,
25784                                    "invalid form of %<#pragma omp atomic%>");
25785                   goto saw_error;
25786                 case CPP_MULT:
25787                   opcode = MULT_EXPR;
25788                   break;
25789                 case CPP_DIV:
25790                   opcode = TRUNC_DIV_EXPR;
25791                   break;
25792                 case CPP_PLUS:
25793                   opcode = PLUS_EXPR;
25794                   break;
25795                 case CPP_MINUS:
25796                   opcode = MINUS_EXPR;
25797                   break;
25798                 case CPP_LSHIFT:
25799                   opcode = LSHIFT_EXPR;
25800                   break;
25801                 case CPP_RSHIFT:
25802                   opcode = RSHIFT_EXPR;
25803                   break;
25804                 case CPP_AND:
25805                   opcode = BIT_AND_EXPR;
25806                   break;
25807                 case CPP_OR:
25808                   opcode = BIT_IOR_EXPR;
25809                   break;
25810                 case CPP_XOR:
25811                   opcode = BIT_XOR_EXPR;
25812                   break;
25813                 default:
25814                   cp_parser_error (parser,
25815                                    "invalid operator for %<#pragma omp atomic%>");
25816                   goto saw_error;
25817                 }
25818               oprec = TOKEN_PRECEDENCE (token);
25819               gcc_assert (oprec != PREC_NOT_OPERATOR);
25820               if (commutative_tree_code (opcode))
25821                 oprec = (enum cp_parser_prec) (oprec - 1);
25822               cp_lexer_consume_token (parser->lexer);
25823               rhs = cp_parser_binary_expression (parser, false, false,
25824                                                  oprec, NULL);
25825               if (rhs == error_mark_node)
25826                 goto saw_error;
25827               goto stmt_done;
25828             }
25829           /* FALLTHROUGH */
25830         default:
25831           cp_parser_error (parser,
25832                            "invalid operator for %<#pragma omp atomic%>");
25833           goto saw_error;
25834         }
25835       cp_lexer_consume_token (parser->lexer);
25836
25837       rhs = cp_parser_expression (parser, false, NULL);
25838       if (rhs == error_mark_node)
25839         goto saw_error;
25840       break;
25841     }
25842 stmt_done:
25843   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25844     {
25845       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25846         goto saw_error;
25847       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25848                                       /*cast_p=*/false, NULL);
25849       if (v == error_mark_node)
25850         goto saw_error;
25851       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25852         goto saw_error;
25853       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25854                                          /*cast_p=*/false, NULL);
25855       if (lhs1 == error_mark_node)
25856         goto saw_error;
25857     }
25858   if (structured_block)
25859     {
25860       cp_parser_consume_semicolon_at_end_of_statement (parser);
25861       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25862     }
25863 done:
25864   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25865   if (!structured_block)
25866     cp_parser_consume_semicolon_at_end_of_statement (parser);
25867   return;
25868
25869  saw_error:
25870   cp_parser_skip_to_end_of_block_or_statement (parser);
25871   if (structured_block)
25872     {
25873       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25874         cp_lexer_consume_token (parser->lexer);
25875       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25876         {
25877           cp_parser_skip_to_end_of_block_or_statement (parser);
25878           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25879             cp_lexer_consume_token (parser->lexer);
25880         }
25881     }
25882 }
25883
25884
25885 /* OpenMP 2.5:
25886    # pragma omp barrier new-line  */
25887
25888 static void
25889 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25890 {
25891   cp_parser_require_pragma_eol (parser, pragma_tok);
25892   finish_omp_barrier ();
25893 }
25894
25895 /* OpenMP 2.5:
25896    # pragma omp critical [(name)] new-line
25897      structured-block  */
25898
25899 static tree
25900 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25901 {
25902   tree stmt, name = NULL;
25903
25904   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25905     {
25906       cp_lexer_consume_token (parser->lexer);
25907
25908       name = cp_parser_identifier (parser);
25909
25910       if (name == error_mark_node
25911           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25912         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25913                                                /*or_comma=*/false,
25914                                                /*consume_paren=*/true);
25915       if (name == error_mark_node)
25916         name = NULL;
25917     }
25918   cp_parser_require_pragma_eol (parser, pragma_tok);
25919
25920   stmt = cp_parser_omp_structured_block (parser);
25921   return c_finish_omp_critical (input_location, stmt, name);
25922 }
25923
25924 /* OpenMP 2.5:
25925    # pragma omp flush flush-vars[opt] new-line
25926
25927    flush-vars:
25928      ( variable-list ) */
25929
25930 static void
25931 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25932 {
25933   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25934     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25935   cp_parser_require_pragma_eol (parser, pragma_tok);
25936
25937   finish_omp_flush ();
25938 }
25939
25940 /* Helper function, to parse omp for increment expression.  */
25941
25942 static tree
25943 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25944 {
25945   tree cond = cp_parser_binary_expression (parser, false, true,
25946                                            PREC_NOT_OPERATOR, NULL);
25947   if (cond == error_mark_node
25948       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25949     {
25950       cp_parser_skip_to_end_of_statement (parser);
25951       return error_mark_node;
25952     }
25953
25954   switch (TREE_CODE (cond))
25955     {
25956     case GT_EXPR:
25957     case GE_EXPR:
25958     case LT_EXPR:
25959     case LE_EXPR:
25960       break;
25961     default:
25962       return error_mark_node;
25963     }
25964
25965   /* If decl is an iterator, preserve LHS and RHS of the relational
25966      expr until finish_omp_for.  */
25967   if (decl
25968       && (type_dependent_expression_p (decl)
25969           || CLASS_TYPE_P (TREE_TYPE (decl))))
25970     return cond;
25971
25972   return build_x_binary_op (TREE_CODE (cond),
25973                             TREE_OPERAND (cond, 0), ERROR_MARK,
25974                             TREE_OPERAND (cond, 1), ERROR_MARK,
25975                             /*overload=*/NULL, tf_warning_or_error);
25976 }
25977
25978 /* Helper function, to parse omp for increment expression.  */
25979
25980 static tree
25981 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25982 {
25983   cp_token *token = cp_lexer_peek_token (parser->lexer);
25984   enum tree_code op;
25985   tree lhs, rhs;
25986   cp_id_kind idk;
25987   bool decl_first;
25988
25989   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25990     {
25991       op = (token->type == CPP_PLUS_PLUS
25992             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25993       cp_lexer_consume_token (parser->lexer);
25994       lhs = cp_parser_cast_expression (parser, false, false, NULL);
25995       if (lhs != decl)
25996         return error_mark_node;
25997       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25998     }
25999
26000   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26001   if (lhs != decl)
26002     return error_mark_node;
26003
26004   token = cp_lexer_peek_token (parser->lexer);
26005   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26006     {
26007       op = (token->type == CPP_PLUS_PLUS
26008             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26009       cp_lexer_consume_token (parser->lexer);
26010       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26011     }
26012
26013   op = cp_parser_assignment_operator_opt (parser);
26014   if (op == ERROR_MARK)
26015     return error_mark_node;
26016
26017   if (op != NOP_EXPR)
26018     {
26019       rhs = cp_parser_assignment_expression (parser, false, NULL);
26020       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26021       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26022     }
26023
26024   lhs = cp_parser_binary_expression (parser, false, false,
26025                                      PREC_ADDITIVE_EXPRESSION, NULL);
26026   token = cp_lexer_peek_token (parser->lexer);
26027   decl_first = lhs == decl;
26028   if (decl_first)
26029     lhs = NULL_TREE;
26030   if (token->type != CPP_PLUS
26031       && token->type != CPP_MINUS)
26032     return error_mark_node;
26033
26034   do
26035     {
26036       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26037       cp_lexer_consume_token (parser->lexer);
26038       rhs = cp_parser_binary_expression (parser, false, false,
26039                                          PREC_ADDITIVE_EXPRESSION, NULL);
26040       token = cp_lexer_peek_token (parser->lexer);
26041       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26042         {
26043           if (lhs == NULL_TREE)
26044             {
26045               if (op == PLUS_EXPR)
26046                 lhs = rhs;
26047               else
26048                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
26049             }
26050           else
26051             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
26052                                      NULL, tf_warning_or_error);
26053         }
26054     }
26055   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26056
26057   if (!decl_first)
26058     {
26059       if (rhs != decl || op == MINUS_EXPR)
26060         return error_mark_node;
26061       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26062     }
26063   else
26064     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26065
26066   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26067 }
26068
26069 /* Parse the restricted form of the for statement allowed by OpenMP.  */
26070
26071 static tree
26072 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26073 {
26074   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26075   tree real_decl, initv, condv, incrv, declv;
26076   tree this_pre_body, cl;
26077   location_t loc_first;
26078   bool collapse_err = false;
26079   int i, collapse = 1, nbraces = 0;
26080   VEC(tree,gc) *for_block = make_tree_vector ();
26081
26082   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26083     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26084       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26085
26086   gcc_assert (collapse >= 1);
26087
26088   declv = make_tree_vec (collapse);
26089   initv = make_tree_vec (collapse);
26090   condv = make_tree_vec (collapse);
26091   incrv = make_tree_vec (collapse);
26092
26093   loc_first = cp_lexer_peek_token (parser->lexer)->location;
26094
26095   for (i = 0; i < collapse; i++)
26096     {
26097       int bracecount = 0;
26098       bool add_private_clause = false;
26099       location_t loc;
26100
26101       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26102         {
26103           cp_parser_error (parser, "for statement expected");
26104           return NULL;
26105         }
26106       loc = cp_lexer_consume_token (parser->lexer)->location;
26107
26108       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26109         return NULL;
26110
26111       init = decl = real_decl = NULL;
26112       this_pre_body = push_stmt_list ();
26113       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26114         {
26115           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26116
26117              init-expr:
26118                        var = lb
26119                        integer-type var = lb
26120                        random-access-iterator-type var = lb
26121                        pointer-type var = lb
26122           */
26123           cp_decl_specifier_seq type_specifiers;
26124
26125           /* First, try to parse as an initialized declaration.  See
26126              cp_parser_condition, from whence the bulk of this is copied.  */
26127
26128           cp_parser_parse_tentatively (parser);
26129           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26130                                         /*is_trailing_return=*/false,
26131                                         &type_specifiers);
26132           if (cp_parser_parse_definitely (parser))
26133             {
26134               /* If parsing a type specifier seq succeeded, then this
26135                  MUST be a initialized declaration.  */
26136               tree asm_specification, attributes;
26137               cp_declarator *declarator;
26138
26139               declarator = cp_parser_declarator (parser,
26140                                                  CP_PARSER_DECLARATOR_NAMED,
26141                                                  /*ctor_dtor_or_conv_p=*/NULL,
26142                                                  /*parenthesized_p=*/NULL,
26143                                                  /*member_p=*/false);
26144               attributes = cp_parser_attributes_opt (parser);
26145               asm_specification = cp_parser_asm_specification_opt (parser);
26146
26147               if (declarator == cp_error_declarator) 
26148                 cp_parser_skip_to_end_of_statement (parser);
26149
26150               else 
26151                 {
26152                   tree pushed_scope, auto_node;
26153
26154                   decl = start_decl (declarator, &type_specifiers,
26155                                      SD_INITIALIZED, attributes,
26156                                      /*prefix_attributes=*/NULL_TREE,
26157                                      &pushed_scope);
26158
26159                   auto_node = type_uses_auto (TREE_TYPE (decl));
26160                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26161                     {
26162                       if (cp_lexer_next_token_is (parser->lexer, 
26163                                                   CPP_OPEN_PAREN))
26164                         error ("parenthesized initialization is not allowed in "
26165                                "OpenMP %<for%> loop");
26166                       else
26167                         /* Trigger an error.  */
26168                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26169
26170                       init = error_mark_node;
26171                       cp_parser_skip_to_end_of_statement (parser);
26172                     }
26173                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26174                            || type_dependent_expression_p (decl)
26175                            || auto_node)
26176                     {
26177                       bool is_direct_init, is_non_constant_init;
26178
26179                       init = cp_parser_initializer (parser,
26180                                                     &is_direct_init,
26181                                                     &is_non_constant_init);
26182
26183                       if (auto_node)
26184                         {
26185                           TREE_TYPE (decl)
26186                             = do_auto_deduction (TREE_TYPE (decl), init,
26187                                                  auto_node);
26188
26189                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26190                               && !type_dependent_expression_p (decl))
26191                             goto non_class;
26192                         }
26193                       
26194                       cp_finish_decl (decl, init, !is_non_constant_init,
26195                                       asm_specification,
26196                                       LOOKUP_ONLYCONVERTING);
26197                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26198                         {
26199                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26200                           init = NULL_TREE;
26201                         }
26202                       else
26203                         init = pop_stmt_list (this_pre_body);
26204                       this_pre_body = NULL_TREE;
26205                     }
26206                   else
26207                     {
26208                       /* Consume '='.  */
26209                       cp_lexer_consume_token (parser->lexer);
26210                       init = cp_parser_assignment_expression (parser, false, NULL);
26211
26212                     non_class:
26213                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26214                         init = error_mark_node;
26215                       else
26216                         cp_finish_decl (decl, NULL_TREE,
26217                                         /*init_const_expr_p=*/false,
26218                                         asm_specification,
26219                                         LOOKUP_ONLYCONVERTING);
26220                     }
26221
26222                   if (pushed_scope)
26223                     pop_scope (pushed_scope);
26224                 }
26225             }
26226           else 
26227             {
26228               cp_id_kind idk;
26229               /* If parsing a type specifier sequence failed, then
26230                  this MUST be a simple expression.  */
26231               cp_parser_parse_tentatively (parser);
26232               decl = cp_parser_primary_expression (parser, false, false,
26233                                                    false, &idk);
26234               if (!cp_parser_error_occurred (parser)
26235                   && decl
26236                   && DECL_P (decl)
26237                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26238                 {
26239                   tree rhs;
26240
26241                   cp_parser_parse_definitely (parser);
26242                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26243                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26244                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26245                                                          rhs,
26246                                                          tf_warning_or_error));
26247                   add_private_clause = true;
26248                 }
26249               else
26250                 {
26251                   decl = NULL;
26252                   cp_parser_abort_tentative_parse (parser);
26253                   init = cp_parser_expression (parser, false, NULL);
26254                   if (init)
26255                     {
26256                       if (TREE_CODE (init) == MODIFY_EXPR
26257                           || TREE_CODE (init) == MODOP_EXPR)
26258                         real_decl = TREE_OPERAND (init, 0);
26259                     }
26260                 }
26261             }
26262         }
26263       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26264       if (this_pre_body)
26265         {
26266           this_pre_body = pop_stmt_list (this_pre_body);
26267           if (pre_body)
26268             {
26269               tree t = pre_body;
26270               pre_body = push_stmt_list ();
26271               add_stmt (t);
26272               add_stmt (this_pre_body);
26273               pre_body = pop_stmt_list (pre_body);
26274             }
26275           else
26276             pre_body = this_pre_body;
26277         }
26278
26279       if (decl)
26280         real_decl = decl;
26281       if (par_clauses != NULL && real_decl != NULL_TREE)
26282         {
26283           tree *c;
26284           for (c = par_clauses; *c ; )
26285             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26286                 && OMP_CLAUSE_DECL (*c) == real_decl)
26287               {
26288                 error_at (loc, "iteration variable %qD"
26289                           " should not be firstprivate", real_decl);
26290                 *c = OMP_CLAUSE_CHAIN (*c);
26291               }
26292             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26293                      && OMP_CLAUSE_DECL (*c) == real_decl)
26294               {
26295                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26296                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26297                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26298                 OMP_CLAUSE_DECL (l) = real_decl;
26299                 OMP_CLAUSE_CHAIN (l) = clauses;
26300                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26301                 clauses = l;
26302                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26303                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26304                 add_private_clause = false;
26305               }
26306             else
26307               {
26308                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26309                     && OMP_CLAUSE_DECL (*c) == real_decl)
26310                   add_private_clause = false;
26311                 c = &OMP_CLAUSE_CHAIN (*c);
26312               }
26313         }
26314
26315       if (add_private_clause)
26316         {
26317           tree c;
26318           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26319             {
26320               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26321                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26322                   && OMP_CLAUSE_DECL (c) == decl)
26323                 break;
26324               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26325                        && OMP_CLAUSE_DECL (c) == decl)
26326                 error_at (loc, "iteration variable %qD "
26327                           "should not be firstprivate",
26328                           decl);
26329               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26330                        && OMP_CLAUSE_DECL (c) == decl)
26331                 error_at (loc, "iteration variable %qD should not be reduction",
26332                           decl);
26333             }
26334           if (c == NULL)
26335             {
26336               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26337               OMP_CLAUSE_DECL (c) = decl;
26338               c = finish_omp_clauses (c);
26339               if (c)
26340                 {
26341                   OMP_CLAUSE_CHAIN (c) = clauses;
26342                   clauses = c;
26343                 }
26344             }
26345         }
26346
26347       cond = NULL;
26348       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26349         cond = cp_parser_omp_for_cond (parser, decl);
26350       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26351
26352       incr = NULL;
26353       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26354         {
26355           /* If decl is an iterator, preserve the operator on decl
26356              until finish_omp_for.  */
26357           if (real_decl
26358               && ((processing_template_decl
26359                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26360                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26361             incr = cp_parser_omp_for_incr (parser, real_decl);
26362           else
26363             incr = cp_parser_expression (parser, false, NULL);
26364         }
26365
26366       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26367         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26368                                                /*or_comma=*/false,
26369                                                /*consume_paren=*/true);
26370
26371       TREE_VEC_ELT (declv, i) = decl;
26372       TREE_VEC_ELT (initv, i) = init;
26373       TREE_VEC_ELT (condv, i) = cond;
26374       TREE_VEC_ELT (incrv, i) = incr;
26375
26376       if (i == collapse - 1)
26377         break;
26378
26379       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26380          in between the collapsed for loops to be still considered perfectly
26381          nested.  Hopefully the final version clarifies this.
26382          For now handle (multiple) {'s and empty statements.  */
26383       cp_parser_parse_tentatively (parser);
26384       do
26385         {
26386           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26387             break;
26388           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26389             {
26390               cp_lexer_consume_token (parser->lexer);
26391               bracecount++;
26392             }
26393           else if (bracecount
26394                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26395             cp_lexer_consume_token (parser->lexer);
26396           else
26397             {
26398               loc = cp_lexer_peek_token (parser->lexer)->location;
26399               error_at (loc, "not enough collapsed for loops");
26400               collapse_err = true;
26401               cp_parser_abort_tentative_parse (parser);
26402               declv = NULL_TREE;
26403               break;
26404             }
26405         }
26406       while (1);
26407
26408       if (declv)
26409         {
26410           cp_parser_parse_definitely (parser);
26411           nbraces += bracecount;
26412         }
26413     }
26414
26415   /* Note that we saved the original contents of this flag when we entered
26416      the structured block, and so we don't need to re-save it here.  */
26417   parser->in_statement = IN_OMP_FOR;
26418
26419   /* Note that the grammar doesn't call for a structured block here,
26420      though the loop as a whole is a structured block.  */
26421   body = push_stmt_list ();
26422   cp_parser_statement (parser, NULL_TREE, false, NULL);
26423   body = pop_stmt_list (body);
26424
26425   if (declv == NULL_TREE)
26426     ret = NULL_TREE;
26427   else
26428     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26429                           pre_body, clauses);
26430
26431   while (nbraces)
26432     {
26433       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26434         {
26435           cp_lexer_consume_token (parser->lexer);
26436           nbraces--;
26437         }
26438       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26439         cp_lexer_consume_token (parser->lexer);
26440       else
26441         {
26442           if (!collapse_err)
26443             {
26444               error_at (cp_lexer_peek_token (parser->lexer)->location,
26445                         "collapsed loops not perfectly nested");
26446             }
26447           collapse_err = true;
26448           cp_parser_statement_seq_opt (parser, NULL);
26449           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26450             break;
26451         }
26452     }
26453
26454   while (!VEC_empty (tree, for_block))
26455     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26456   release_tree_vector (for_block);
26457
26458   return ret;
26459 }
26460
26461 /* OpenMP 2.5:
26462    #pragma omp for for-clause[optseq] new-line
26463      for-loop  */
26464
26465 #define OMP_FOR_CLAUSE_MASK                             \
26466         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26467         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26468         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26469         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26470         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26471         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26472         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26473         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26474
26475 static tree
26476 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26477 {
26478   tree clauses, sb, ret;
26479   unsigned int save;
26480
26481   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26482                                        "#pragma omp for", pragma_tok);
26483
26484   sb = begin_omp_structured_block ();
26485   save = cp_parser_begin_omp_structured_block (parser);
26486
26487   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26488
26489   cp_parser_end_omp_structured_block (parser, save);
26490   add_stmt (finish_omp_structured_block (sb));
26491
26492   return ret;
26493 }
26494
26495 /* OpenMP 2.5:
26496    # pragma omp master new-line
26497      structured-block  */
26498
26499 static tree
26500 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26501 {
26502   cp_parser_require_pragma_eol (parser, pragma_tok);
26503   return c_finish_omp_master (input_location,
26504                               cp_parser_omp_structured_block (parser));
26505 }
26506
26507 /* OpenMP 2.5:
26508    # pragma omp ordered new-line
26509      structured-block  */
26510
26511 static tree
26512 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26513 {
26514   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26515   cp_parser_require_pragma_eol (parser, pragma_tok);
26516   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26517 }
26518
26519 /* OpenMP 2.5:
26520
26521    section-scope:
26522      { section-sequence }
26523
26524    section-sequence:
26525      section-directive[opt] structured-block
26526      section-sequence section-directive structured-block  */
26527
26528 static tree
26529 cp_parser_omp_sections_scope (cp_parser *parser)
26530 {
26531   tree stmt, substmt;
26532   bool error_suppress = false;
26533   cp_token *tok;
26534
26535   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26536     return NULL_TREE;
26537
26538   stmt = push_stmt_list ();
26539
26540   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26541     {
26542       unsigned save;
26543
26544       substmt = begin_omp_structured_block ();
26545       save = cp_parser_begin_omp_structured_block (parser);
26546
26547       while (1)
26548         {
26549           cp_parser_statement (parser, NULL_TREE, false, NULL);
26550
26551           tok = cp_lexer_peek_token (parser->lexer);
26552           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26553             break;
26554           if (tok->type == CPP_CLOSE_BRACE)
26555             break;
26556           if (tok->type == CPP_EOF)
26557             break;
26558         }
26559
26560       cp_parser_end_omp_structured_block (parser, save);
26561       substmt = finish_omp_structured_block (substmt);
26562       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26563       add_stmt (substmt);
26564     }
26565
26566   while (1)
26567     {
26568       tok = cp_lexer_peek_token (parser->lexer);
26569       if (tok->type == CPP_CLOSE_BRACE)
26570         break;
26571       if (tok->type == CPP_EOF)
26572         break;
26573
26574       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26575         {
26576           cp_lexer_consume_token (parser->lexer);
26577           cp_parser_require_pragma_eol (parser, tok);
26578           error_suppress = false;
26579         }
26580       else if (!error_suppress)
26581         {
26582           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26583           error_suppress = true;
26584         }
26585
26586       substmt = cp_parser_omp_structured_block (parser);
26587       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26588       add_stmt (substmt);
26589     }
26590   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26591
26592   substmt = pop_stmt_list (stmt);
26593
26594   stmt = make_node (OMP_SECTIONS);
26595   TREE_TYPE (stmt) = void_type_node;
26596   OMP_SECTIONS_BODY (stmt) = substmt;
26597
26598   add_stmt (stmt);
26599   return stmt;
26600 }
26601
26602 /* OpenMP 2.5:
26603    # pragma omp sections sections-clause[optseq] newline
26604      sections-scope  */
26605
26606 #define OMP_SECTIONS_CLAUSE_MASK                        \
26607         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26608         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26609         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26610         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26611         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26612
26613 static tree
26614 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26615 {
26616   tree clauses, ret;
26617
26618   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26619                                        "#pragma omp sections", pragma_tok);
26620
26621   ret = cp_parser_omp_sections_scope (parser);
26622   if (ret)
26623     OMP_SECTIONS_CLAUSES (ret) = clauses;
26624
26625   return ret;
26626 }
26627
26628 /* OpenMP 2.5:
26629    # pragma parallel parallel-clause new-line
26630    # pragma parallel for parallel-for-clause new-line
26631    # pragma parallel sections parallel-sections-clause new-line  */
26632
26633 #define OMP_PARALLEL_CLAUSE_MASK                        \
26634         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26635         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26636         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26637         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26638         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26639         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26640         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26641         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26642
26643 static tree
26644 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26645 {
26646   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26647   const char *p_name = "#pragma omp parallel";
26648   tree stmt, clauses, par_clause, ws_clause, block;
26649   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26650   unsigned int save;
26651   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26652
26653   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26654     {
26655       cp_lexer_consume_token (parser->lexer);
26656       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26657       p_name = "#pragma omp parallel for";
26658       mask |= OMP_FOR_CLAUSE_MASK;
26659       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26660     }
26661   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26662     {
26663       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26664       const char *p = IDENTIFIER_POINTER (id);
26665       if (strcmp (p, "sections") == 0)
26666         {
26667           cp_lexer_consume_token (parser->lexer);
26668           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26669           p_name = "#pragma omp parallel sections";
26670           mask |= OMP_SECTIONS_CLAUSE_MASK;
26671           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26672         }
26673     }
26674
26675   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26676   block = begin_omp_parallel ();
26677   save = cp_parser_begin_omp_structured_block (parser);
26678
26679   switch (p_kind)
26680     {
26681     case PRAGMA_OMP_PARALLEL:
26682       cp_parser_statement (parser, NULL_TREE, false, NULL);
26683       par_clause = clauses;
26684       break;
26685
26686     case PRAGMA_OMP_PARALLEL_FOR:
26687       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26688       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26689       break;
26690
26691     case PRAGMA_OMP_PARALLEL_SECTIONS:
26692       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26693       stmt = cp_parser_omp_sections_scope (parser);
26694       if (stmt)
26695         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26696       break;
26697
26698     default:
26699       gcc_unreachable ();
26700     }
26701
26702   cp_parser_end_omp_structured_block (parser, save);
26703   stmt = finish_omp_parallel (par_clause, block);
26704   if (p_kind != PRAGMA_OMP_PARALLEL)
26705     OMP_PARALLEL_COMBINED (stmt) = 1;
26706   return stmt;
26707 }
26708
26709 /* OpenMP 2.5:
26710    # pragma omp single single-clause[optseq] new-line
26711      structured-block  */
26712
26713 #define OMP_SINGLE_CLAUSE_MASK                          \
26714         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26715         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26716         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26717         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26718
26719 static tree
26720 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26721 {
26722   tree stmt = make_node (OMP_SINGLE);
26723   TREE_TYPE (stmt) = void_type_node;
26724
26725   OMP_SINGLE_CLAUSES (stmt)
26726     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26727                                  "#pragma omp single", pragma_tok);
26728   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26729
26730   return add_stmt (stmt);
26731 }
26732
26733 /* OpenMP 3.0:
26734    # pragma omp task task-clause[optseq] new-line
26735      structured-block  */
26736
26737 #define OMP_TASK_CLAUSE_MASK                            \
26738         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26739         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26740         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26741         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26742         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26743         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26744         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26745         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26746
26747 static tree
26748 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26749 {
26750   tree clauses, block;
26751   unsigned int save;
26752
26753   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26754                                        "#pragma omp task", pragma_tok);
26755   block = begin_omp_task ();
26756   save = cp_parser_begin_omp_structured_block (parser);
26757   cp_parser_statement (parser, NULL_TREE, false, NULL);
26758   cp_parser_end_omp_structured_block (parser, save);
26759   return finish_omp_task (clauses, block);
26760 }
26761
26762 /* OpenMP 3.0:
26763    # pragma omp taskwait new-line  */
26764
26765 static void
26766 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26767 {
26768   cp_parser_require_pragma_eol (parser, pragma_tok);
26769   finish_omp_taskwait ();
26770 }
26771
26772 /* OpenMP 3.1:
26773    # pragma omp taskyield new-line  */
26774
26775 static void
26776 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26777 {
26778   cp_parser_require_pragma_eol (parser, pragma_tok);
26779   finish_omp_taskyield ();
26780 }
26781
26782 /* OpenMP 2.5:
26783    # pragma omp threadprivate (variable-list) */
26784
26785 static void
26786 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26787 {
26788   tree vars;
26789
26790   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26791   cp_parser_require_pragma_eol (parser, pragma_tok);
26792
26793   finish_omp_threadprivate (vars);
26794 }
26795
26796 /* Main entry point to OpenMP statement pragmas.  */
26797
26798 static void
26799 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26800 {
26801   tree stmt;
26802
26803   switch (pragma_tok->pragma_kind)
26804     {
26805     case PRAGMA_OMP_ATOMIC:
26806       cp_parser_omp_atomic (parser, pragma_tok);
26807       return;
26808     case PRAGMA_OMP_CRITICAL:
26809       stmt = cp_parser_omp_critical (parser, pragma_tok);
26810       break;
26811     case PRAGMA_OMP_FOR:
26812       stmt = cp_parser_omp_for (parser, pragma_tok);
26813       break;
26814     case PRAGMA_OMP_MASTER:
26815       stmt = cp_parser_omp_master (parser, pragma_tok);
26816       break;
26817     case PRAGMA_OMP_ORDERED:
26818       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26819       break;
26820     case PRAGMA_OMP_PARALLEL:
26821       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26822       break;
26823     case PRAGMA_OMP_SECTIONS:
26824       stmt = cp_parser_omp_sections (parser, pragma_tok);
26825       break;
26826     case PRAGMA_OMP_SINGLE:
26827       stmt = cp_parser_omp_single (parser, pragma_tok);
26828       break;
26829     case PRAGMA_OMP_TASK:
26830       stmt = cp_parser_omp_task (parser, pragma_tok);
26831       break;
26832     default:
26833       gcc_unreachable ();
26834     }
26835
26836   if (stmt)
26837     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26838 }
26839 \f
26840 /* Transactional Memory parsing routines.  */
26841
26842 /* Parse a transaction attribute.
26843
26844    txn-attribute:
26845         attribute
26846         [ [ identifier ] ]
26847
26848    ??? Simplify this when C++0x bracket attributes are
26849    implemented properly.  */
26850
26851 static tree
26852 cp_parser_txn_attribute_opt (cp_parser *parser)
26853 {
26854   cp_token *token;
26855   tree attr_name, attr = NULL;
26856
26857   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26858     return cp_parser_attributes_opt (parser);
26859
26860   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26861     return NULL_TREE;
26862   cp_lexer_consume_token (parser->lexer);
26863   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26864     goto error1;
26865
26866   token = cp_lexer_peek_token (parser->lexer);
26867   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26868     {
26869       token = cp_lexer_consume_token (parser->lexer);
26870
26871       attr_name = (token->type == CPP_KEYWORD
26872                    /* For keywords, use the canonical spelling,
26873                       not the parsed identifier.  */
26874                    ? ridpointers[(int) token->keyword]
26875                    : token->u.value);
26876       attr = build_tree_list (attr_name, NULL_TREE);
26877     }
26878   else
26879     cp_parser_error (parser, "expected identifier");
26880
26881   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26882  error1:
26883   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26884   return attr;
26885 }
26886
26887 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26888
26889    transaction-statement:
26890      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
26891        compound-statement
26892      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
26893 */
26894
26895 static tree
26896 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26897 {
26898   unsigned char old_in = parser->in_transaction;
26899   unsigned char this_in = 1, new_in;
26900   cp_token *token;
26901   tree stmt, attrs, noex;
26902
26903   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26904       || keyword == RID_TRANSACTION_RELAXED);
26905   token = cp_parser_require_keyword (parser, keyword,
26906       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26907           : RT_TRANSACTION_RELAXED));
26908   gcc_assert (token != NULL);
26909
26910   if (keyword == RID_TRANSACTION_RELAXED)
26911     this_in |= TM_STMT_ATTR_RELAXED;
26912   else
26913     {
26914       attrs = cp_parser_txn_attribute_opt (parser);
26915       if (attrs)
26916         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26917     }
26918
26919   /* Parse a noexcept specification.  */
26920   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
26921
26922   /* Keep track if we're in the lexical scope of an outer transaction.  */
26923   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
26924
26925   stmt = begin_transaction_stmt (token->location, NULL, this_in);
26926
26927   parser->in_transaction = new_in;
26928   cp_parser_compound_statement (parser, NULL, false, false);
26929   parser->in_transaction = old_in;
26930
26931   finish_transaction_stmt (stmt, NULL, this_in, noex);
26932
26933   return stmt;
26934 }
26935
26936 /* Parse a __transaction_atomic or __transaction_relaxed expression.
26937
26938    transaction-expression:
26939      __transaction_atomic txn-noexcept-spec[opt] ( expression )
26940      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
26941 */
26942
26943 static tree
26944 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
26945 {
26946   unsigned char old_in = parser->in_transaction;
26947   unsigned char this_in = 1;
26948   cp_token *token;
26949   tree expr, noex;
26950   bool noex_expr;
26951
26952   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26953       || keyword == RID_TRANSACTION_RELAXED);
26954
26955   if (!flag_tm)
26956     error (keyword == RID_TRANSACTION_RELAXED
26957            ? G_("%<__transaction_relaxed%> without transactional memory "
26958                 "support enabled")
26959            : G_("%<__transaction_atomic%> without transactional memory "
26960                 "support enabled"));
26961
26962   token = cp_parser_require_keyword (parser, keyword,
26963       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26964           : RT_TRANSACTION_RELAXED));
26965   gcc_assert (token != NULL);
26966
26967   if (keyword == RID_TRANSACTION_RELAXED)
26968     this_in |= TM_STMT_ATTR_RELAXED;
26969
26970   /* Set this early.  This might mean that we allow transaction_cancel in
26971      an expression that we find out later actually has to be a constexpr.
26972      However, we expect that cxx_constant_value will be able to deal with
26973      this; also, if the noexcept has no constexpr, then what we parse next
26974      really is a transaction's body.  */
26975   parser->in_transaction = this_in;
26976
26977   /* Parse a noexcept specification.  */
26978   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
26979                                                true);
26980
26981   if (!noex || !noex_expr
26982       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
26983     {
26984       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
26985
26986       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26987       finish_parenthesized_expr (expr);
26988
26989       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
26990     }
26991   else
26992     {
26993       /* The only expression that is available got parsed for the noexcept
26994          already.  noexcept is true then.  */
26995       expr = noex;
26996       noex = boolean_true_node;
26997     }
26998
26999   expr = build_transaction_expr (token->location, expr, this_in, noex);
27000   parser->in_transaction = old_in;
27001
27002   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27003     return error_mark_node;
27004
27005   return (flag_tm ? expr : error_mark_node);
27006 }
27007
27008 /* Parse a function-transaction-block.
27009
27010    function-transaction-block:
27011      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27012          function-body
27013      __transaction_atomic txn-attribute[opt] function-try-block
27014      __transaction_relaxed ctor-initializer[opt] function-body
27015      __transaction_relaxed function-try-block
27016 */
27017
27018 static bool
27019 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27020 {
27021   unsigned char old_in = parser->in_transaction;
27022   unsigned char new_in = 1;
27023   tree compound_stmt, stmt, attrs;
27024   bool ctor_initializer_p;
27025   cp_token *token;
27026
27027   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27028       || keyword == RID_TRANSACTION_RELAXED);
27029   token = cp_parser_require_keyword (parser, keyword,
27030       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27031           : RT_TRANSACTION_RELAXED));
27032   gcc_assert (token != NULL);
27033
27034   if (keyword == RID_TRANSACTION_RELAXED)
27035     new_in |= TM_STMT_ATTR_RELAXED;
27036   else
27037     {
27038       attrs = cp_parser_txn_attribute_opt (parser);
27039       if (attrs)
27040         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27041     }
27042
27043   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27044
27045   parser->in_transaction = new_in;
27046
27047   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27048     ctor_initializer_p = cp_parser_function_try_block (parser);
27049   else
27050     ctor_initializer_p
27051       = cp_parser_ctor_initializer_opt_and_function_body (parser);
27052
27053   parser->in_transaction = old_in;
27054
27055   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27056
27057   return ctor_initializer_p;
27058 }
27059
27060 /* Parse a __transaction_cancel statement.
27061
27062    cancel-statement:
27063      __transaction_cancel txn-attribute[opt] ;
27064      __transaction_cancel txn-attribute[opt] throw-expression ;
27065
27066    ??? Cancel and throw is not yet implemented.  */
27067
27068 static tree
27069 cp_parser_transaction_cancel (cp_parser *parser)
27070 {
27071   cp_token *token;
27072   bool is_outer = false;
27073   tree stmt, attrs;
27074
27075   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27076                                      RT_TRANSACTION_CANCEL);
27077   gcc_assert (token != NULL);
27078
27079   attrs = cp_parser_txn_attribute_opt (parser);
27080   if (attrs)
27081     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27082
27083   /* ??? Parse cancel-and-throw here.  */
27084
27085   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27086
27087   if (!flag_tm)
27088     {
27089       error_at (token->location, "%<__transaction_cancel%> without "
27090                 "transactional memory support enabled");
27091       return error_mark_node;
27092     }
27093   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27094     {
27095       error_at (token->location, "%<__transaction_cancel%> within a "
27096                 "%<__transaction_relaxed%>");
27097       return error_mark_node;
27098     }
27099   else if (is_outer)
27100     {
27101       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27102           && !is_tm_may_cancel_outer (current_function_decl))
27103         {
27104           error_at (token->location, "outer %<__transaction_cancel%> not "
27105                     "within outer %<__transaction_atomic%>");
27106           error_at (token->location,
27107                     "  or a %<transaction_may_cancel_outer%> function");
27108           return error_mark_node;
27109         }
27110     }
27111   else if (parser->in_transaction == 0)
27112     {
27113       error_at (token->location, "%<__transaction_cancel%> not within "
27114                 "%<__transaction_atomic%>");
27115       return error_mark_node;
27116     }
27117
27118   stmt = build_tm_abort_call (token->location, is_outer);
27119   add_stmt (stmt);
27120   finish_stmt ();
27121
27122   return stmt;
27123 }
27124 \f
27125 /* The parser.  */
27126
27127 static GTY (()) cp_parser *the_parser;
27128
27129 \f
27130 /* Special handling for the first token or line in the file.  The first
27131    thing in the file might be #pragma GCC pch_preprocess, which loads a
27132    PCH file, which is a GC collection point.  So we need to handle this
27133    first pragma without benefit of an existing lexer structure.
27134
27135    Always returns one token to the caller in *FIRST_TOKEN.  This is
27136    either the true first token of the file, or the first token after
27137    the initial pragma.  */
27138
27139 static void
27140 cp_parser_initial_pragma (cp_token *first_token)
27141 {
27142   tree name = NULL;
27143
27144   cp_lexer_get_preprocessor_token (NULL, first_token);
27145   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27146     return;
27147
27148   cp_lexer_get_preprocessor_token (NULL, first_token);
27149   if (first_token->type == CPP_STRING)
27150     {
27151       name = first_token->u.value;
27152
27153       cp_lexer_get_preprocessor_token (NULL, first_token);
27154       if (first_token->type != CPP_PRAGMA_EOL)
27155         error_at (first_token->location,
27156                   "junk at end of %<#pragma GCC pch_preprocess%>");
27157     }
27158   else
27159     error_at (first_token->location, "expected string literal");
27160
27161   /* Skip to the end of the pragma.  */
27162   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27163     cp_lexer_get_preprocessor_token (NULL, first_token);
27164
27165   /* Now actually load the PCH file.  */
27166   if (name)
27167     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27168
27169   /* Read one more token to return to our caller.  We have to do this
27170      after reading the PCH file in, since its pointers have to be
27171      live.  */
27172   cp_lexer_get_preprocessor_token (NULL, first_token);
27173 }
27174
27175 /* Normal parsing of a pragma token.  Here we can (and must) use the
27176    regular lexer.  */
27177
27178 static bool
27179 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27180 {
27181   cp_token *pragma_tok;
27182   unsigned int id;
27183
27184   pragma_tok = cp_lexer_consume_token (parser->lexer);
27185   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27186   parser->lexer->in_pragma = true;
27187
27188   id = pragma_tok->pragma_kind;
27189   switch (id)
27190     {
27191     case PRAGMA_GCC_PCH_PREPROCESS:
27192       error_at (pragma_tok->location,
27193                 "%<#pragma GCC pch_preprocess%> must be first");
27194       break;
27195
27196     case PRAGMA_OMP_BARRIER:
27197       switch (context)
27198         {
27199         case pragma_compound:
27200           cp_parser_omp_barrier (parser, pragma_tok);
27201           return false;
27202         case pragma_stmt:
27203           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27204                     "used in compound statements");
27205           break;
27206         default:
27207           goto bad_stmt;
27208         }
27209       break;
27210
27211     case PRAGMA_OMP_FLUSH:
27212       switch (context)
27213         {
27214         case pragma_compound:
27215           cp_parser_omp_flush (parser, pragma_tok);
27216           return false;
27217         case pragma_stmt:
27218           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27219                     "used in compound statements");
27220           break;
27221         default:
27222           goto bad_stmt;
27223         }
27224       break;
27225
27226     case PRAGMA_OMP_TASKWAIT:
27227       switch (context)
27228         {
27229         case pragma_compound:
27230           cp_parser_omp_taskwait (parser, pragma_tok);
27231           return false;
27232         case pragma_stmt:
27233           error_at (pragma_tok->location,
27234                     "%<#pragma omp taskwait%> may only be "
27235                     "used in compound statements");
27236           break;
27237         default:
27238           goto bad_stmt;
27239         }
27240       break;
27241
27242     case PRAGMA_OMP_TASKYIELD:
27243       switch (context)
27244         {
27245         case pragma_compound:
27246           cp_parser_omp_taskyield (parser, pragma_tok);
27247           return false;
27248         case pragma_stmt:
27249           error_at (pragma_tok->location,
27250                     "%<#pragma omp taskyield%> may only be "
27251                     "used in compound statements");
27252           break;
27253         default:
27254           goto bad_stmt;
27255         }
27256       break;
27257
27258     case PRAGMA_OMP_THREADPRIVATE:
27259       cp_parser_omp_threadprivate (parser, pragma_tok);
27260       return false;
27261
27262     case PRAGMA_OMP_ATOMIC:
27263     case PRAGMA_OMP_CRITICAL:
27264     case PRAGMA_OMP_FOR:
27265     case PRAGMA_OMP_MASTER:
27266     case PRAGMA_OMP_ORDERED:
27267     case PRAGMA_OMP_PARALLEL:
27268     case PRAGMA_OMP_SECTIONS:
27269     case PRAGMA_OMP_SINGLE:
27270     case PRAGMA_OMP_TASK:
27271       if (context == pragma_external)
27272         goto bad_stmt;
27273       cp_parser_omp_construct (parser, pragma_tok);
27274       return true;
27275
27276     case PRAGMA_OMP_SECTION:
27277       error_at (pragma_tok->location, 
27278                 "%<#pragma omp section%> may only be used in "
27279                 "%<#pragma omp sections%> construct");
27280       break;
27281
27282     default:
27283       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27284       c_invoke_pragma_handler (id);
27285       break;
27286
27287     bad_stmt:
27288       cp_parser_error (parser, "expected declaration specifiers");
27289       break;
27290     }
27291
27292   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27293   return false;
27294 }
27295
27296 /* The interface the pragma parsers have to the lexer.  */
27297
27298 enum cpp_ttype
27299 pragma_lex (tree *value)
27300 {
27301   cp_token *tok;
27302   enum cpp_ttype ret;
27303
27304   tok = cp_lexer_peek_token (the_parser->lexer);
27305
27306   ret = tok->type;
27307   *value = tok->u.value;
27308
27309   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27310     ret = CPP_EOF;
27311   else if (ret == CPP_STRING)
27312     *value = cp_parser_string_literal (the_parser, false, false);
27313   else
27314     {
27315       cp_lexer_consume_token (the_parser->lexer);
27316       if (ret == CPP_KEYWORD)
27317         ret = CPP_NAME;
27318     }
27319
27320   return ret;
27321 }
27322
27323 \f
27324 /* External interface.  */
27325
27326 /* Parse one entire translation unit.  */
27327
27328 void
27329 c_parse_file (void)
27330 {
27331   static bool already_called = false;
27332
27333   if (already_called)
27334     {
27335       sorry ("inter-module optimizations not implemented for C++");
27336       return;
27337     }
27338   already_called = true;
27339
27340   the_parser = cp_parser_new ();
27341   push_deferring_access_checks (flag_access_control
27342                                 ? dk_no_deferred : dk_no_check);
27343   cp_parser_translation_unit (the_parser);
27344   the_parser = NULL;
27345 }
27346
27347 #include "gt-cp-parser.h"