OSDN Git Service

PR c++/51541 - ICE with invalid identifier in alias-declaration
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR,
110   /* a transaction expression */
111   NIC_TRANSACTION
112 } non_integral_constant;
113
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error {
116   /* NULL */
117   NLE_NULL,
118   /* is not a type */
119   NLE_TYPE,
120   /* is not a class or namespace */
121   NLE_CXX98,
122   /* is not a class, namespace, or enumeration */
123   NLE_NOT_CXX98
124 } name_lookup_error;
125
126 /* The various kinds of required token */
127 typedef enum required_token {
128   RT_NONE,
129   RT_SEMICOLON,  /* ';' */
130   RT_OPEN_PAREN, /* '(' */
131   RT_CLOSE_BRACE, /* '}' */
132   RT_OPEN_BRACE,  /* '{' */
133   RT_CLOSE_SQUARE, /* ']' */
134   RT_OPEN_SQUARE,  /* '[' */
135   RT_COMMA, /* ',' */
136   RT_SCOPE, /* '::' */
137   RT_LESS, /* '<' */
138   RT_GREATER, /* '>' */
139   RT_EQ, /* '=' */
140   RT_ELLIPSIS, /* '...' */
141   RT_MULT, /* '*' */
142   RT_COMPL, /* '~' */
143   RT_COLON, /* ':' */
144   RT_COLON_SCOPE, /* ':' or '::' */
145   RT_CLOSE_PAREN, /* ')' */
146   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147   RT_PRAGMA_EOL, /* end of line */
148   RT_NAME, /* identifier */
149
150   /* The type is CPP_KEYWORD */
151   RT_NEW, /* new */
152   RT_DELETE, /* delete */
153   RT_RETURN, /* return */
154   RT_WHILE, /* while */
155   RT_EXTERN, /* extern */
156   RT_STATIC_ASSERT, /* static_assert */
157   RT_DECLTYPE, /* decltype */
158   RT_OPERATOR, /* operator */
159   RT_CLASS, /* class */
160   RT_TEMPLATE, /* template */
161   RT_NAMESPACE, /* namespace */
162   RT_USING, /* using */
163   RT_ASM, /* asm */
164   RT_TRY, /* try */
165   RT_CATCH, /* catch */
166   RT_THROW, /* throw */
167   RT_LABEL, /* __label__ */
168   RT_AT_TRY, /* @try */
169   RT_AT_SYNCHRONIZED, /* @synchronized */
170   RT_AT_THROW, /* @throw */
171
172   RT_SELECT,  /* selection-statement */
173   RT_INTERATION, /* iteration-statement */
174   RT_JUMP, /* jump-statement */
175   RT_CLASS_KEY, /* class-key */
176   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179   RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 } required_token;
181
182 /* Prototypes.  */
183
184 static cp_lexer *cp_lexer_new_main
185   (void);
186 static cp_lexer *cp_lexer_new_from_tokens
187   (cp_token_cache *tokens);
188 static void cp_lexer_destroy
189   (cp_lexer *);
190 static int cp_lexer_saving_tokens
191   (const cp_lexer *);
192 static cp_token *cp_lexer_token_at
193   (cp_lexer *, cp_token_position);
194 static void cp_lexer_get_preprocessor_token
195   (cp_lexer *, cp_token *);
196 static inline cp_token *cp_lexer_peek_token
197   (cp_lexer *);
198 static cp_token *cp_lexer_peek_nth_token
199   (cp_lexer *, size_t);
200 static inline bool cp_lexer_next_token_is
201   (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_not
203   (cp_lexer *, enum cpp_ttype);
204 static bool cp_lexer_next_token_is_keyword
205   (cp_lexer *, enum rid);
206 static cp_token *cp_lexer_consume_token
207   (cp_lexer *);
208 static void cp_lexer_purge_token
209   (cp_lexer *);
210 static void cp_lexer_purge_tokens_after
211   (cp_lexer *, cp_token_position);
212 static void cp_lexer_save_tokens
213   (cp_lexer *);
214 static void cp_lexer_commit_tokens
215   (cp_lexer *);
216 static void cp_lexer_rollback_tokens
217   (cp_lexer *);
218 static void cp_lexer_print_token
219   (FILE *, cp_token *);
220 static inline bool cp_lexer_debugging_p
221   (cp_lexer *);
222 static void cp_lexer_start_debugging
223   (cp_lexer *) ATTRIBUTE_UNUSED;
224 static void cp_lexer_stop_debugging
225   (cp_lexer *) ATTRIBUTE_UNUSED;
226
227 static cp_token_cache *cp_token_cache_new
228   (cp_token *, cp_token *);
229
230 static void cp_parser_initial_pragma
231   (cp_token *);
232
233 static tree cp_literal_operator_id
234   (const char *);
235
236 /* Manifest constants.  */
237 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
238 #define CP_SAVED_TOKEN_STACK 5
239
240 /* Variables.  */
241
242 /* The stream to which debugging output should be written.  */
243 static FILE *cp_lexer_debug_stream;
244
245 /* Nonzero if we are parsing an unevaluated operand: an operand to
246    sizeof, typeof, or alignof.  */
247 int cp_unevaluated_operand;
248
249 /* Dump up to NUM tokens in BUFFER to FILE starting with token
250    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
251    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
252    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
253    highlighted by surrounding it in [[ ]].  */
254
255 static void
256 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
257                       cp_token *start_token, unsigned num,
258                       cp_token *curr_token)
259 {
260   unsigned i, nprinted;
261   cp_token *token;
262   bool do_print;
263
264   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265
266   if (buffer == NULL)
267     return;
268
269   if (num == 0)
270     num = VEC_length (cp_token, buffer);
271
272   if (start_token == NULL)
273     start_token = VEC_address (cp_token, buffer);
274
275   if (start_token > VEC_address (cp_token, buffer))
276     {
277       cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
278       fprintf (file, " ... ");
279     }
280
281   do_print = false;
282   nprinted = 0;
283   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284     {
285       if (token == start_token)
286         do_print = true;
287
288       if (!do_print)
289         continue;
290
291       nprinted++;
292       if (token == curr_token)
293         fprintf (file, "[[");
294
295       cp_lexer_print_token (file, token);
296
297       if (token == curr_token)
298         fprintf (file, "]]");
299
300       switch (token->type)
301         {
302           case CPP_SEMICOLON:
303           case CPP_OPEN_BRACE:
304           case CPP_CLOSE_BRACE:
305           case CPP_EOF:
306             fputc ('\n', file);
307             break;
308
309           default:
310             fputc (' ', file);
311         }
312     }
313
314   if (i == num && i < VEC_length (cp_token, buffer))
315     {
316       fprintf (file, " ... ");
317       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
318                             VEC_length (cp_token, buffer) - 1));
319     }
320
321   fprintf (file, "\n");
322 }
323
324
325 /* Dump all tokens in BUFFER to stderr.  */
326
327 void
328 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
329 {
330   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 }
332
333
334 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
335    description for T.  */
336
337 static void
338 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
339 {
340   if (t)
341     {
342       fprintf (file, "%s: ", desc);
343       print_node_brief (file, "", t, 0);
344     }
345 }
346
347
348 /* Dump parser context C to FILE.  */
349
350 static void
351 cp_debug_print_context (FILE *file, cp_parser_context *c)
352 {
353   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
354   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
355   print_node_brief (file, "", c->object_type, 0);
356   fprintf (file, "}\n");
357 }
358
359
360 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
361
362 static void
363 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
364 {
365   unsigned i;
366   cp_parser_context *c;
367
368   fprintf (file, "Parsing context stack:\n");
369   for (i = 0, c = first; c; c = c->next, i++)
370     {
371       fprintf (file, "\t#%u: ", i);
372       cp_debug_print_context (file, c);
373     }
374 }
375
376
377 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
378
379 static void
380 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
381 {
382   if (flag)
383     fprintf (file, "%s: true\n", desc);
384 }
385
386
387 /* Print an unparsed function entry UF to FILE.  */
388
389 static void
390 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
391 {
392   unsigned i;
393   cp_default_arg_entry *default_arg_fn;
394   tree fn;
395
396   fprintf (file, "\tFunctions with default args:\n");
397   for (i = 0;
398        VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
399                     default_arg_fn);
400        i++)
401     {
402       fprintf (file, "\t\tClass type: ");
403       print_node_brief (file, "", default_arg_fn->class_type, 0);
404       fprintf (file, "\t\tDeclaration: ");
405       print_node_brief (file, "", default_arg_fn->decl, 0);
406       fprintf (file, "\n");
407     }
408
409   fprintf (file, "\n\tFunctions with definitions that require "
410            "post-processing\n\t\t");
411   for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
412     {
413       print_node_brief (file, "", fn, 0);
414       fprintf (file, " ");
415     }
416   fprintf (file, "\n");
417
418   fprintf (file, "\n\tNon-static data members with initializers that require "
419            "post-processing\n\t\t");
420   for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
421     {
422       print_node_brief (file, "", fn, 0);
423       fprintf (file, " ");
424     }
425   fprintf (file, "\n");
426 }
427
428
429 /* Print the stack of unparsed member functions S to FILE.  */
430
431 static void
432 cp_debug_print_unparsed_queues (FILE *file,
433                                 VEC(cp_unparsed_functions_entry, gc) *s)
434 {
435   unsigned i;
436   cp_unparsed_functions_entry *uf;
437
438   fprintf (file, "Unparsed functions\n");
439   for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
440     {
441       fprintf (file, "#%u:\n", i);
442       cp_debug_print_unparsed_function (file, uf);
443     }
444 }
445
446
447 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
448    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
449
450 static void
451 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
452 {
453   cp_token *next_token, *first_token, *start_token;
454
455   if (file == NULL)
456     file = stderr;
457
458   next_token = parser->lexer->next_token;
459   first_token = VEC_address (cp_token, parser->lexer->buffer);
460   start_token = (next_token > first_token + window_size / 2)
461                 ? next_token - window_size / 2
462                 : first_token;
463   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
464                         next_token);
465 }
466
467
468 /* Dump debugging information for the given PARSER.  If FILE is NULL,
469    the output is printed on stderr.  */
470
471 void
472 cp_debug_parser (FILE *file, cp_parser *parser)
473 {
474   const size_t window_size = 20;
475   cp_token *token;
476   expanded_location eloc;
477
478   if (file == NULL)
479     file = stderr;
480
481   fprintf (file, "Parser state\n\n");
482   fprintf (file, "Number of tokens: %u\n",
483            VEC_length (cp_token, parser->lexer->buffer));
484   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
485   cp_debug_print_tree_if_set (file, "Object scope",
486                                      parser->object_scope);
487   cp_debug_print_tree_if_set (file, "Qualifying scope",
488                                      parser->qualifying_scope);
489   cp_debug_print_context_stack (file, parser->context);
490   cp_debug_print_flag (file, "Allow GNU extensions",
491                               parser->allow_gnu_extensions_p);
492   cp_debug_print_flag (file, "'>' token is greater-than",
493                               parser->greater_than_is_operator_p);
494   cp_debug_print_flag (file, "Default args allowed in current "
495                               "parameter list", parser->default_arg_ok_p);
496   cp_debug_print_flag (file, "Parsing integral constant-expression",
497                               parser->integral_constant_expression_p);
498   cp_debug_print_flag (file, "Allow non-constant expression in current "
499                               "constant-expression",
500                               parser->allow_non_integral_constant_expression_p);
501   cp_debug_print_flag (file, "Seen non-constant expression",
502                               parser->non_integral_constant_expression_p);
503   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
504                               "current context",
505                               parser->local_variables_forbidden_p);
506   cp_debug_print_flag (file, "In unbraced linkage specification",
507                               parser->in_unbraced_linkage_specification_p);
508   cp_debug_print_flag (file, "Parsing a declarator",
509                               parser->in_declarator_p);
510   cp_debug_print_flag (file, "In template argument list",
511                               parser->in_template_argument_list_p);
512   cp_debug_print_flag (file, "Parsing an iteration statement",
513                               parser->in_statement & IN_ITERATION_STMT);
514   cp_debug_print_flag (file, "Parsing a switch statement",
515                               parser->in_statement & IN_SWITCH_STMT);
516   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
517                               parser->in_statement & IN_OMP_BLOCK);
518   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
519                               parser->in_statement & IN_OMP_FOR);
520   cp_debug_print_flag (file, "Parsing an if statement",
521                               parser->in_statement & IN_IF_STMT);
522   cp_debug_print_flag (file, "Parsing a type-id in an expression "
523                               "context", parser->in_type_id_in_expr_p);
524   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
525                               parser->implicit_extern_c);
526   cp_debug_print_flag (file, "String expressions should be translated "
527                               "to execution character set",
528                               parser->translate_strings_p);
529   cp_debug_print_flag (file, "Parsing function body outside of a "
530                               "local class", parser->in_function_body);
531   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
532                               parser->colon_corrects_to_scope_p);
533   if (parser->type_definition_forbidden_message)
534     fprintf (file, "Error message for forbidden type definitions: %s\n",
535              parser->type_definition_forbidden_message);
536   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
537   fprintf (file, "Number of class definitions in progress: %u\n",
538            parser->num_classes_being_defined);
539   fprintf (file, "Number of template parameter lists for the current "
540            "declaration: %u\n", parser->num_template_parameter_lists);
541   cp_debug_parser_tokens (file, parser, window_size);
542   token = parser->lexer->next_token;
543   fprintf (file, "Next token to parse:\n");
544   fprintf (file, "\tToken:  ");
545   cp_lexer_print_token (file, token);
546   eloc = expand_location (token->location);
547   fprintf (file, "\n\tFile:   %s\n", eloc.file);
548   fprintf (file, "\tLine:   %d\n", eloc.line);
549   fprintf (file, "\tColumn: %d\n", eloc.column);
550 }
551
552
553 /* Allocate memory for a new lexer object and return it.  */
554
555 static cp_lexer *
556 cp_lexer_alloc (void)
557 {
558   cp_lexer *lexer;
559
560   c_common_no_more_pch ();
561
562   /* Allocate the memory.  */
563   lexer = ggc_alloc_cleared_cp_lexer ();
564
565   /* Initially we are not debugging.  */
566   lexer->debugging_p = false;
567
568   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
569                                    CP_SAVED_TOKEN_STACK);
570
571   /* Create the buffer.  */
572   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
573
574   return lexer;
575 }
576
577
578 /* Create a new main C++ lexer, the lexer that gets tokens from the
579    preprocessor.  */
580
581 static cp_lexer *
582 cp_lexer_new_main (void)
583 {
584   cp_lexer *lexer;
585   cp_token token;
586
587   /* It's possible that parsing the first pragma will load a PCH file,
588      which is a GC collection point.  So we have to do that before
589      allocating any memory.  */
590   cp_parser_initial_pragma (&token);
591
592   lexer = cp_lexer_alloc ();
593
594   /* Put the first token in the buffer.  */
595   VEC_quick_push (cp_token, lexer->buffer, &token);
596
597   /* Get the remaining tokens from the preprocessor.  */
598   while (token.type != CPP_EOF)
599     {
600       cp_lexer_get_preprocessor_token (lexer, &token);
601       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
602     }
603
604   lexer->last_token = VEC_address (cp_token, lexer->buffer)
605                       + VEC_length (cp_token, lexer->buffer)
606                       - 1;
607   lexer->next_token = VEC_length (cp_token, lexer->buffer)
608                       ? VEC_address (cp_token, lexer->buffer)
609                       : &eof_token;
610
611   /* Subsequent preprocessor diagnostics should use compiler
612      diagnostic functions to get the compiler source location.  */
613   done_lexing = true;
614
615   gcc_assert (!lexer->next_token->purged_p);
616   return lexer;
617 }
618
619 /* Create a new lexer whose token stream is primed with the tokens in
620    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
621
622 static cp_lexer *
623 cp_lexer_new_from_tokens (cp_token_cache *cache)
624 {
625   cp_token *first = cache->first;
626   cp_token *last = cache->last;
627   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
628
629   /* We do not own the buffer.  */
630   lexer->buffer = NULL;
631   lexer->next_token = first == last ? &eof_token : first;
632   lexer->last_token = last;
633
634   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
635                                    CP_SAVED_TOKEN_STACK);
636
637   /* Initially we are not debugging.  */
638   lexer->debugging_p = false;
639
640   gcc_assert (!lexer->next_token->purged_p);
641   return lexer;
642 }
643
644 /* Frees all resources associated with LEXER.  */
645
646 static void
647 cp_lexer_destroy (cp_lexer *lexer)
648 {
649   VEC_free (cp_token, gc, lexer->buffer);
650   VEC_free (cp_token_position, heap, lexer->saved_tokens);
651   ggc_free (lexer);
652 }
653
654 /* Returns nonzero if debugging information should be output.  */
655
656 static inline bool
657 cp_lexer_debugging_p (cp_lexer *lexer)
658 {
659   return lexer->debugging_p;
660 }
661
662
663 static inline cp_token_position
664 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
665 {
666   gcc_assert (!previous_p || lexer->next_token != &eof_token);
667
668   return lexer->next_token - previous_p;
669 }
670
671 static inline cp_token *
672 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
673 {
674   return pos;
675 }
676
677 static inline void
678 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
679 {
680   lexer->next_token = cp_lexer_token_at (lexer, pos);
681 }
682
683 static inline cp_token_position
684 cp_lexer_previous_token_position (cp_lexer *lexer)
685 {
686   if (lexer->next_token == &eof_token)
687     return lexer->last_token - 1;
688   else
689     return cp_lexer_token_position (lexer, true);
690 }
691
692 static inline cp_token *
693 cp_lexer_previous_token (cp_lexer *lexer)
694 {
695   cp_token_position tp = cp_lexer_previous_token_position (lexer);
696
697   return cp_lexer_token_at (lexer, tp);
698 }
699
700 /* nonzero if we are presently saving tokens.  */
701
702 static inline int
703 cp_lexer_saving_tokens (const cp_lexer* lexer)
704 {
705   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
706 }
707
708 /* Store the next token from the preprocessor in *TOKEN.  Return true
709    if we reach EOF.  If LEXER is NULL, assume we are handling an
710    initial #pragma pch_preprocess, and thus want the lexer to return
711    processed strings.  */
712
713 static void
714 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
715 {
716   static int is_extern_c = 0;
717
718    /* Get a new token from the preprocessor.  */
719   token->type
720     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
721                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
722   token->keyword = RID_MAX;
723   token->pragma_kind = PRAGMA_NONE;
724   token->purged_p = false;
725
726   /* On some systems, some header files are surrounded by an
727      implicit extern "C" block.  Set a flag in the token if it
728      comes from such a header.  */
729   is_extern_c += pending_lang_change;
730   pending_lang_change = 0;
731   token->implicit_extern_c = is_extern_c > 0;
732
733   /* Check to see if this token is a keyword.  */
734   if (token->type == CPP_NAME)
735     {
736       if (C_IS_RESERVED_WORD (token->u.value))
737         {
738           /* Mark this token as a keyword.  */
739           token->type = CPP_KEYWORD;
740           /* Record which keyword.  */
741           token->keyword = C_RID_CODE (token->u.value);
742         }
743       else
744         {
745           if (warn_cxx0x_compat
746               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
747               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
748             {
749               /* Warn about the C++0x keyword (but still treat it as
750                  an identifier).  */
751               warning (OPT_Wc__0x_compat, 
752                        "identifier %qE is a keyword in C++11",
753                        token->u.value);
754
755               /* Clear out the C_RID_CODE so we don't warn about this
756                  particular identifier-turned-keyword again.  */
757               C_SET_RID_CODE (token->u.value, RID_MAX);
758             }
759
760           token->ambiguous_p = false;
761           token->keyword = RID_MAX;
762         }
763     }
764   else if (token->type == CPP_AT_NAME)
765     {
766       /* This only happens in Objective-C++; it must be a keyword.  */
767       token->type = CPP_KEYWORD;
768       switch (C_RID_CODE (token->u.value))
769         {
770           /* Replace 'class' with '@class', 'private' with '@private',
771              etc.  This prevents confusion with the C++ keyword
772              'class', and makes the tokens consistent with other
773              Objective-C 'AT' keywords.  For example '@class' is
774              reported as RID_AT_CLASS which is consistent with
775              '@synchronized', which is reported as
776              RID_AT_SYNCHRONIZED.
777           */
778         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
779         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
780         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
781         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
782         case RID_THROW:     token->keyword = RID_AT_THROW; break;
783         case RID_TRY:       token->keyword = RID_AT_TRY; break;
784         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
785         default:            token->keyword = C_RID_CODE (token->u.value);
786         }
787     }
788   else if (token->type == CPP_PRAGMA)
789     {
790       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
791       token->pragma_kind = ((enum pragma_kind)
792                             TREE_INT_CST_LOW (token->u.value));
793       token->u.value = NULL_TREE;
794     }
795 }
796
797 /* Update the globals input_location and the input file stack from TOKEN.  */
798 static inline void
799 cp_lexer_set_source_position_from_token (cp_token *token)
800 {
801   if (token->type != CPP_EOF)
802     {
803       input_location = token->location;
804     }
805 }
806
807 /* Return a pointer to the next token in the token stream, but do not
808    consume it.  */
809
810 static inline cp_token *
811 cp_lexer_peek_token (cp_lexer *lexer)
812 {
813   if (cp_lexer_debugging_p (lexer))
814     {
815       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
816       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
817       putc ('\n', cp_lexer_debug_stream);
818     }
819   return lexer->next_token;
820 }
821
822 /* Return true if the next token has the indicated TYPE.  */
823
824 static inline bool
825 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
826 {
827   return cp_lexer_peek_token (lexer)->type == type;
828 }
829
830 /* Return true if the next token does not have the indicated TYPE.  */
831
832 static inline bool
833 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
834 {
835   return !cp_lexer_next_token_is (lexer, type);
836 }
837
838 /* Return true if the next token is the indicated KEYWORD.  */
839
840 static inline bool
841 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
842 {
843   return cp_lexer_peek_token (lexer)->keyword == keyword;
844 }
845
846 /* Return true if the next token is not the indicated KEYWORD.  */
847
848 static inline bool
849 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
850 {
851   return cp_lexer_peek_token (lexer)->keyword != keyword;
852 }
853
854 /* Return true if the next token is a keyword for a decl-specifier.  */
855
856 static bool
857 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
858 {
859   cp_token *token;
860
861   token = cp_lexer_peek_token (lexer);
862   switch (token->keyword) 
863     {
864       /* auto specifier: storage-class-specifier in C++,
865          simple-type-specifier in C++0x.  */
866     case RID_AUTO:
867       /* Storage classes.  */
868     case RID_REGISTER:
869     case RID_STATIC:
870     case RID_EXTERN:
871     case RID_MUTABLE:
872     case RID_THREAD:
873       /* Elaborated type specifiers.  */
874     case RID_ENUM:
875     case RID_CLASS:
876     case RID_STRUCT:
877     case RID_UNION:
878     case RID_TYPENAME:
879       /* Simple type specifiers.  */
880     case RID_CHAR:
881     case RID_CHAR16:
882     case RID_CHAR32:
883     case RID_WCHAR:
884     case RID_BOOL:
885     case RID_SHORT:
886     case RID_INT:
887     case RID_LONG:
888     case RID_INT128:
889     case RID_SIGNED:
890     case RID_UNSIGNED:
891     case RID_FLOAT:
892     case RID_DOUBLE:
893     case RID_VOID:
894       /* GNU extensions.  */ 
895     case RID_ATTRIBUTE:
896     case RID_TYPEOF:
897       /* C++0x extensions.  */
898     case RID_DECLTYPE:
899     case RID_UNDERLYING_TYPE:
900       return true;
901
902     default:
903       return false;
904     }
905 }
906
907 /* Returns TRUE iff the token T begins a decltype type.  */
908
909 static bool
910 token_is_decltype (cp_token *t)
911 {
912   return (t->keyword == RID_DECLTYPE
913           || t->type == CPP_DECLTYPE);
914 }
915
916 /* Returns TRUE iff the next token begins a decltype type.  */
917
918 static bool
919 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
920 {
921   cp_token *t = cp_lexer_peek_token (lexer);
922   return token_is_decltype (t);
923 }
924
925 /* Return a pointer to the Nth token in the token stream.  If N is 1,
926    then this is precisely equivalent to cp_lexer_peek_token (except
927    that it is not inline).  One would like to disallow that case, but
928    there is one case (cp_parser_nth_token_starts_template_id) where
929    the caller passes a variable for N and it might be 1.  */
930
931 static cp_token *
932 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
933 {
934   cp_token *token;
935
936   /* N is 1-based, not zero-based.  */
937   gcc_assert (n > 0);
938
939   if (cp_lexer_debugging_p (lexer))
940     fprintf (cp_lexer_debug_stream,
941              "cp_lexer: peeking ahead %ld at token: ", (long)n);
942
943   --n;
944   token = lexer->next_token;
945   gcc_assert (!n || token != &eof_token);
946   while (n != 0)
947     {
948       ++token;
949       if (token == lexer->last_token)
950         {
951           token = &eof_token;
952           break;
953         }
954
955       if (!token->purged_p)
956         --n;
957     }
958
959   if (cp_lexer_debugging_p (lexer))
960     {
961       cp_lexer_print_token (cp_lexer_debug_stream, token);
962       putc ('\n', cp_lexer_debug_stream);
963     }
964
965   return token;
966 }
967
968 /* Return the next token, and advance the lexer's next_token pointer
969    to point to the next non-purged token.  */
970
971 static cp_token *
972 cp_lexer_consume_token (cp_lexer* lexer)
973 {
974   cp_token *token = lexer->next_token;
975
976   gcc_assert (token != &eof_token);
977   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
978
979   do
980     {
981       lexer->next_token++;
982       if (lexer->next_token == lexer->last_token)
983         {
984           lexer->next_token = &eof_token;
985           break;
986         }
987
988     }
989   while (lexer->next_token->purged_p);
990
991   cp_lexer_set_source_position_from_token (token);
992
993   /* Provide debugging output.  */
994   if (cp_lexer_debugging_p (lexer))
995     {
996       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
997       cp_lexer_print_token (cp_lexer_debug_stream, token);
998       putc ('\n', cp_lexer_debug_stream);
999     }
1000
1001   return token;
1002 }
1003
1004 /* Permanently remove the next token from the token stream, and
1005    advance the next_token pointer to refer to the next non-purged
1006    token.  */
1007
1008 static void
1009 cp_lexer_purge_token (cp_lexer *lexer)
1010 {
1011   cp_token *tok = lexer->next_token;
1012
1013   gcc_assert (tok != &eof_token);
1014   tok->purged_p = true;
1015   tok->location = UNKNOWN_LOCATION;
1016   tok->u.value = NULL_TREE;
1017   tok->keyword = RID_MAX;
1018
1019   do
1020     {
1021       tok++;
1022       if (tok == lexer->last_token)
1023         {
1024           tok = &eof_token;
1025           break;
1026         }
1027     }
1028   while (tok->purged_p);
1029   lexer->next_token = tok;
1030 }
1031
1032 /* Permanently remove all tokens after TOK, up to, but not
1033    including, the token that will be returned next by
1034    cp_lexer_peek_token.  */
1035
1036 static void
1037 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1038 {
1039   cp_token *peek = lexer->next_token;
1040
1041   if (peek == &eof_token)
1042     peek = lexer->last_token;
1043
1044   gcc_assert (tok < peek);
1045
1046   for ( tok += 1; tok != peek; tok += 1)
1047     {
1048       tok->purged_p = true;
1049       tok->location = UNKNOWN_LOCATION;
1050       tok->u.value = NULL_TREE;
1051       tok->keyword = RID_MAX;
1052     }
1053 }
1054
1055 /* Begin saving tokens.  All tokens consumed after this point will be
1056    preserved.  */
1057
1058 static void
1059 cp_lexer_save_tokens (cp_lexer* lexer)
1060 {
1061   /* Provide debugging output.  */
1062   if (cp_lexer_debugging_p (lexer))
1063     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1064
1065   VEC_safe_push (cp_token_position, heap,
1066                  lexer->saved_tokens, lexer->next_token);
1067 }
1068
1069 /* Commit to the portion of the token stream most recently saved.  */
1070
1071 static void
1072 cp_lexer_commit_tokens (cp_lexer* lexer)
1073 {
1074   /* Provide debugging output.  */
1075   if (cp_lexer_debugging_p (lexer))
1076     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1077
1078   VEC_pop (cp_token_position, lexer->saved_tokens);
1079 }
1080
1081 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1082    to the token stream.  Stop saving tokens.  */
1083
1084 static void
1085 cp_lexer_rollback_tokens (cp_lexer* lexer)
1086 {
1087   /* Provide debugging output.  */
1088   if (cp_lexer_debugging_p (lexer))
1089     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1090
1091   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1092 }
1093
1094 /* Print a representation of the TOKEN on the STREAM.  */
1095
1096 static void
1097 cp_lexer_print_token (FILE * stream, cp_token *token)
1098 {
1099   /* We don't use cpp_type2name here because the parser defines
1100      a few tokens of its own.  */
1101   static const char *const token_names[] = {
1102     /* cpplib-defined token types */
1103 #define OP(e, s) #e,
1104 #define TK(e, s) #e,
1105     TTYPE_TABLE
1106 #undef OP
1107 #undef TK
1108     /* C++ parser token types - see "Manifest constants", above.  */
1109     "KEYWORD",
1110     "TEMPLATE_ID",
1111     "NESTED_NAME_SPECIFIER",
1112   };
1113
1114   /* For some tokens, print the associated data.  */
1115   switch (token->type)
1116     {
1117     case CPP_KEYWORD:
1118       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1119          For example, `struct' is mapped to an INTEGER_CST.  */
1120       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1121         break;
1122       /* else fall through */
1123     case CPP_NAME:
1124       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1125       break;
1126
1127     case CPP_STRING:
1128     case CPP_STRING16:
1129     case CPP_STRING32:
1130     case CPP_WSTRING:
1131     case CPP_UTF8STRING:
1132       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1133       break;
1134
1135     case CPP_NUMBER:
1136       print_generic_expr (stream, token->u.value, 0);
1137       break;
1138
1139     default:
1140       /* If we have a name for the token, print it out.  Otherwise, we
1141          simply give the numeric code.  */
1142       if (token->type < ARRAY_SIZE(token_names))
1143         fputs (token_names[token->type], stream);
1144       else
1145         fprintf (stream, "[%d]", token->type);
1146       break;
1147     }
1148 }
1149
1150 /* Start emitting debugging information.  */
1151
1152 static void
1153 cp_lexer_start_debugging (cp_lexer* lexer)
1154 {
1155   lexer->debugging_p = true;
1156   cp_lexer_debug_stream = stderr;
1157 }
1158
1159 /* Stop emitting debugging information.  */
1160
1161 static void
1162 cp_lexer_stop_debugging (cp_lexer* lexer)
1163 {
1164   lexer->debugging_p = false;
1165   cp_lexer_debug_stream = NULL;
1166 }
1167
1168 /* Create a new cp_token_cache, representing a range of tokens.  */
1169
1170 static cp_token_cache *
1171 cp_token_cache_new (cp_token *first, cp_token *last)
1172 {
1173   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1174   cache->first = first;
1175   cache->last = last;
1176   return cache;
1177 }
1178
1179 \f
1180 /* Decl-specifiers.  */
1181
1182 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1183
1184 static void
1185 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1186 {
1187   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1188 }
1189
1190 /* Declarators.  */
1191
1192 /* Nothing other than the parser should be creating declarators;
1193    declarators are a semi-syntactic representation of C++ entities.
1194    Other parts of the front end that need to create entities (like
1195    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1196
1197 static cp_declarator *make_call_declarator
1198   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1199 static cp_declarator *make_array_declarator
1200   (cp_declarator *, tree);
1201 static cp_declarator *make_pointer_declarator
1202   (cp_cv_quals, cp_declarator *);
1203 static cp_declarator *make_reference_declarator
1204   (cp_cv_quals, cp_declarator *, bool);
1205 static cp_parameter_declarator *make_parameter_declarator
1206   (cp_decl_specifier_seq *, cp_declarator *, tree);
1207 static cp_declarator *make_ptrmem_declarator
1208   (cp_cv_quals, tree, cp_declarator *);
1209
1210 /* An erroneous declarator.  */
1211 static cp_declarator *cp_error_declarator;
1212
1213 /* The obstack on which declarators and related data structures are
1214    allocated.  */
1215 static struct obstack declarator_obstack;
1216
1217 /* Alloc BYTES from the declarator memory pool.  */
1218
1219 static inline void *
1220 alloc_declarator (size_t bytes)
1221 {
1222   return obstack_alloc (&declarator_obstack, bytes);
1223 }
1224
1225 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1226    common to all declarators.  */
1227
1228 static cp_declarator *
1229 make_declarator (cp_declarator_kind kind)
1230 {
1231   cp_declarator *declarator;
1232
1233   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1234   declarator->kind = kind;
1235   declarator->attributes = NULL_TREE;
1236   declarator->declarator = NULL;
1237   declarator->parameter_pack_p = false;
1238   declarator->id_loc = UNKNOWN_LOCATION;
1239
1240   return declarator;
1241 }
1242
1243 /* Make a declarator for a generalized identifier.  If
1244    QUALIFYING_SCOPE is non-NULL, the identifier is
1245    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1246    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1247    is, if any.   */
1248
1249 static cp_declarator *
1250 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1251                     special_function_kind sfk)
1252 {
1253   cp_declarator *declarator;
1254
1255   /* It is valid to write:
1256
1257        class C { void f(); };
1258        typedef C D;
1259        void D::f();
1260
1261      The standard is not clear about whether `typedef const C D' is
1262      legal; as of 2002-09-15 the committee is considering that
1263      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1264      well.  */
1265   if (qualifying_scope && TYPE_P (qualifying_scope))
1266     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267
1268   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1269               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1270               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271
1272   declarator = make_declarator (cdk_id);
1273   declarator->u.id.qualifying_scope = qualifying_scope;
1274   declarator->u.id.unqualified_name = unqualified_name;
1275   declarator->u.id.sfk = sfk;
1276   
1277   return declarator;
1278 }
1279
1280 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1281    of modifiers such as const or volatile to apply to the pointer
1282    type, represented as identifiers.  */
1283
1284 cp_declarator *
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1286 {
1287   cp_declarator *declarator;
1288
1289   declarator = make_declarator (cdk_pointer);
1290   declarator->declarator = target;
1291   declarator->u.pointer.qualifiers = cv_qualifiers;
1292   declarator->u.pointer.class_type = NULL_TREE;
1293   if (target)
1294     {
1295       declarator->id_loc = target->id_loc;
1296       declarator->parameter_pack_p = target->parameter_pack_p;
1297       target->parameter_pack_p = false;
1298     }
1299   else
1300     declarator->parameter_pack_p = false;
1301
1302   return declarator;
1303 }
1304
1305 /* Like make_pointer_declarator -- but for references.  */
1306
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309                            bool rvalue_ref)
1310 {
1311   cp_declarator *declarator;
1312
1313   declarator = make_declarator (cdk_reference);
1314   declarator->declarator = target;
1315   declarator->u.reference.qualifiers = cv_qualifiers;
1316   declarator->u.reference.rvalue_ref = rvalue_ref;
1317   if (target)
1318     {
1319       declarator->id_loc = target->id_loc;
1320       declarator->parameter_pack_p = target->parameter_pack_p;
1321       target->parameter_pack_p = false;
1322     }
1323   else
1324     declarator->parameter_pack_p = false;
1325
1326   return declarator;
1327 }
1328
1329 /* Like make_pointer_declarator -- but for a pointer to a non-static
1330    member of CLASS_TYPE.  */
1331
1332 cp_declarator *
1333 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1334                         cp_declarator *pointee)
1335 {
1336   cp_declarator *declarator;
1337
1338   declarator = make_declarator (cdk_ptrmem);
1339   declarator->declarator = pointee;
1340   declarator->u.pointer.qualifiers = cv_qualifiers;
1341   declarator->u.pointer.class_type = class_type;
1342
1343   if (pointee)
1344     {
1345       declarator->parameter_pack_p = pointee->parameter_pack_p;
1346       pointee->parameter_pack_p = false;
1347     }
1348   else
1349     declarator->parameter_pack_p = false;
1350
1351   return declarator;
1352 }
1353
1354 /* Make a declarator for the function given by TARGET, with the
1355    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1356    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1357    indicates what exceptions can be thrown.  */
1358
1359 cp_declarator *
1360 make_call_declarator (cp_declarator *target,
1361                       tree parms,
1362                       cp_cv_quals cv_qualifiers,
1363                       cp_virt_specifiers virt_specifiers,
1364                       tree exception_specification,
1365                       tree late_return_type)
1366 {
1367   cp_declarator *declarator;
1368
1369   declarator = make_declarator (cdk_function);
1370   declarator->declarator = target;
1371   declarator->u.function.parameters = parms;
1372   declarator->u.function.qualifiers = cv_qualifiers;
1373   declarator->u.function.virt_specifiers = virt_specifiers;
1374   declarator->u.function.exception_specification = exception_specification;
1375   declarator->u.function.late_return_type = late_return_type;
1376   if (target)
1377     {
1378       declarator->id_loc = target->id_loc;
1379       declarator->parameter_pack_p = target->parameter_pack_p;
1380       target->parameter_pack_p = false;
1381     }
1382   else
1383     declarator->parameter_pack_p = false;
1384
1385   return declarator;
1386 }
1387
1388 /* Make a declarator for an array of BOUNDS elements, each of which is
1389    defined by ELEMENT.  */
1390
1391 cp_declarator *
1392 make_array_declarator (cp_declarator *element, tree bounds)
1393 {
1394   cp_declarator *declarator;
1395
1396   declarator = make_declarator (cdk_array);
1397   declarator->declarator = element;
1398   declarator->u.array.bounds = bounds;
1399   if (element)
1400     {
1401       declarator->id_loc = element->id_loc;
1402       declarator->parameter_pack_p = element->parameter_pack_p;
1403       element->parameter_pack_p = false;
1404     }
1405   else
1406     declarator->parameter_pack_p = false;
1407
1408   return declarator;
1409 }
1410
1411 /* Determine whether the declarator we've seen so far can be a
1412    parameter pack, when followed by an ellipsis.  */
1413 static bool 
1414 declarator_can_be_parameter_pack (cp_declarator *declarator)
1415 {
1416   /* Search for a declarator name, or any other declarator that goes
1417      after the point where the ellipsis could appear in a parameter
1418      pack. If we find any of these, then this declarator can not be
1419      made into a parameter pack.  */
1420   bool found = false;
1421   while (declarator && !found)
1422     {
1423       switch ((int)declarator->kind)
1424         {
1425         case cdk_id:
1426         case cdk_array:
1427           found = true;
1428           break;
1429
1430         case cdk_error:
1431           return true;
1432
1433         default:
1434           declarator = declarator->declarator;
1435           break;
1436         }
1437     }
1438
1439   return !found;
1440 }
1441
1442 cp_parameter_declarator *no_parameters;
1443
1444 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1445    DECLARATOR and DEFAULT_ARGUMENT.  */
1446
1447 cp_parameter_declarator *
1448 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1449                            cp_declarator *declarator,
1450                            tree default_argument)
1451 {
1452   cp_parameter_declarator *parameter;
1453
1454   parameter = ((cp_parameter_declarator *)
1455                alloc_declarator (sizeof (cp_parameter_declarator)));
1456   parameter->next = NULL;
1457   if (decl_specifiers)
1458     parameter->decl_specifiers = *decl_specifiers;
1459   else
1460     clear_decl_specs (&parameter->decl_specifiers);
1461   parameter->declarator = declarator;
1462   parameter->default_argument = default_argument;
1463   parameter->ellipsis_p = false;
1464
1465   return parameter;
1466 }
1467
1468 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1469
1470 static bool
1471 function_declarator_p (const cp_declarator *declarator)
1472 {
1473   while (declarator)
1474     {
1475       if (declarator->kind == cdk_function
1476           && declarator->declarator->kind == cdk_id)
1477         return true;
1478       if (declarator->kind == cdk_id
1479           || declarator->kind == cdk_error)
1480         return false;
1481       declarator = declarator->declarator;
1482     }
1483   return false;
1484 }
1485  
1486 /* The parser.  */
1487
1488 /* Overview
1489    --------
1490
1491    A cp_parser parses the token stream as specified by the C++
1492    grammar.  Its job is purely parsing, not semantic analysis.  For
1493    example, the parser breaks the token stream into declarators,
1494    expressions, statements, and other similar syntactic constructs.
1495    It does not check that the types of the expressions on either side
1496    of an assignment-statement are compatible, or that a function is
1497    not declared with a parameter of type `void'.
1498
1499    The parser invokes routines elsewhere in the compiler to perform
1500    semantic analysis and to build up the abstract syntax tree for the
1501    code processed.
1502
1503    The parser (and the template instantiation code, which is, in a
1504    way, a close relative of parsing) are the only parts of the
1505    compiler that should be calling push_scope and pop_scope, or
1506    related functions.  The parser (and template instantiation code)
1507    keeps track of what scope is presently active; everything else
1508    should simply honor that.  (The code that generates static
1509    initializers may also need to set the scope, in order to check
1510    access control correctly when emitting the initializers.)
1511
1512    Methodology
1513    -----------
1514
1515    The parser is of the standard recursive-descent variety.  Upcoming
1516    tokens in the token stream are examined in order to determine which
1517    production to use when parsing a non-terminal.  Some C++ constructs
1518    require arbitrary look ahead to disambiguate.  For example, it is
1519    impossible, in the general case, to tell whether a statement is an
1520    expression or declaration without scanning the entire statement.
1521    Therefore, the parser is capable of "parsing tentatively."  When the
1522    parser is not sure what construct comes next, it enters this mode.
1523    Then, while we attempt to parse the construct, the parser queues up
1524    error messages, rather than issuing them immediately, and saves the
1525    tokens it consumes.  If the construct is parsed successfully, the
1526    parser "commits", i.e., it issues any queued error messages and
1527    the tokens that were being preserved are permanently discarded.
1528    If, however, the construct is not parsed successfully, the parser
1529    rolls back its state completely so that it can resume parsing using
1530    a different alternative.
1531
1532    Future Improvements
1533    -------------------
1534
1535    The performance of the parser could probably be improved substantially.
1536    We could often eliminate the need to parse tentatively by looking ahead
1537    a little bit.  In some places, this approach might not entirely eliminate
1538    the need to parse tentatively, but it might still speed up the average
1539    case.  */
1540
1541 /* Flags that are passed to some parsing functions.  These values can
1542    be bitwise-ored together.  */
1543
1544 enum
1545 {
1546   /* No flags.  */
1547   CP_PARSER_FLAGS_NONE = 0x0,
1548   /* The construct is optional.  If it is not present, then no error
1549      should be issued.  */
1550   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1551   /* When parsing a type-specifier, treat user-defined type-names
1552      as non-type identifiers.  */
1553   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1554   /* When parsing a type-specifier, do not try to parse a class-specifier
1555      or enum-specifier.  */
1556   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1557   /* When parsing a decl-specifier-seq, only allow type-specifier or
1558      constexpr.  */
1559   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1560 };
1561
1562 /* This type is used for parameters and variables which hold
1563    combinations of the above flags.  */
1564 typedef int cp_parser_flags;
1565
1566 /* The different kinds of declarators we want to parse.  */
1567
1568 typedef enum cp_parser_declarator_kind
1569 {
1570   /* We want an abstract declarator.  */
1571   CP_PARSER_DECLARATOR_ABSTRACT,
1572   /* We want a named declarator.  */
1573   CP_PARSER_DECLARATOR_NAMED,
1574   /* We don't mind, but the name must be an unqualified-id.  */
1575   CP_PARSER_DECLARATOR_EITHER
1576 } cp_parser_declarator_kind;
1577
1578 /* The precedence values used to parse binary expressions.  The minimum value
1579    of PREC must be 1, because zero is reserved to quickly discriminate
1580    binary operators from other tokens.  */
1581
1582 enum cp_parser_prec
1583 {
1584   PREC_NOT_OPERATOR,
1585   PREC_LOGICAL_OR_EXPRESSION,
1586   PREC_LOGICAL_AND_EXPRESSION,
1587   PREC_INCLUSIVE_OR_EXPRESSION,
1588   PREC_EXCLUSIVE_OR_EXPRESSION,
1589   PREC_AND_EXPRESSION,
1590   PREC_EQUALITY_EXPRESSION,
1591   PREC_RELATIONAL_EXPRESSION,
1592   PREC_SHIFT_EXPRESSION,
1593   PREC_ADDITIVE_EXPRESSION,
1594   PREC_MULTIPLICATIVE_EXPRESSION,
1595   PREC_PM_EXPRESSION,
1596   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1597 };
1598
1599 /* A mapping from a token type to a corresponding tree node type, with a
1600    precedence value.  */
1601
1602 typedef struct cp_parser_binary_operations_map_node
1603 {
1604   /* The token type.  */
1605   enum cpp_ttype token_type;
1606   /* The corresponding tree code.  */
1607   enum tree_code tree_type;
1608   /* The precedence of this operator.  */
1609   enum cp_parser_prec prec;
1610 } cp_parser_binary_operations_map_node;
1611
1612 typedef struct cp_parser_expression_stack_entry
1613 {
1614   /* Left hand side of the binary operation we are currently
1615      parsing.  */
1616   tree lhs;
1617   /* Original tree code for left hand side, if it was a binary
1618      expression itself (used for -Wparentheses).  */
1619   enum tree_code lhs_type;
1620   /* Tree code for the binary operation we are parsing.  */
1621   enum tree_code tree_type;
1622   /* Precedence of the binary operation we are parsing.  */
1623   enum cp_parser_prec prec;
1624 } cp_parser_expression_stack_entry;
1625
1626 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1627    entries because precedence levels on the stack are monotonically
1628    increasing.  */
1629 typedef struct cp_parser_expression_stack_entry
1630   cp_parser_expression_stack[NUM_PREC_VALUES];
1631
1632 /* Prototypes.  */
1633
1634 /* Constructors and destructors.  */
1635
1636 static cp_parser_context *cp_parser_context_new
1637   (cp_parser_context *);
1638
1639 /* Class variables.  */
1640
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1642
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644    Transformed into an associative array (binops_by_token) by
1645    cp_parser_new.  */
1646
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1650
1651   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654
1655   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657
1658   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660
1661   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665
1666   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1668
1669   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1670
1671   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1672
1673   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1674
1675   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1676
1677   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1678 };
1679
1680 /* The same as binops, but initialized by cp_parser_new so that
1681    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1682    for speed.  */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1684
1685 /* Constructors and destructors.  */
1686
1687 /* Construct a new context.  The context below this one on the stack
1688    is given by NEXT.  */
1689
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1692 {
1693   cp_parser_context *context;
1694
1695   /* Allocate the storage.  */
1696   if (cp_parser_context_free_list != NULL)
1697     {
1698       /* Pull the first entry from the free list.  */
1699       context = cp_parser_context_free_list;
1700       cp_parser_context_free_list = context->next;
1701       memset (context, 0, sizeof (*context));
1702     }
1703   else
1704     context = ggc_alloc_cleared_cp_parser_context ();
1705
1706   /* No errors have occurred yet in this context.  */
1707   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708   /* If this is not the bottommost context, copy information that we
1709      need from the previous context.  */
1710   if (next)
1711     {
1712       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713          expression, then we are parsing one in this context, too.  */
1714       context->object_type = next->object_type;
1715       /* Thread the stack.  */
1716       context->next = next;
1717     }
1718
1719   return context;
1720 }
1721
1722 /* Managing the unparsed function queues.  */
1723
1724 #define unparsed_funs_with_default_args \
1725   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728 #define unparsed_nsdmis \
1729   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1730
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1733 {
1734   VEC_safe_push (cp_unparsed_functions_entry, gc,
1735                  parser->unparsed_queues, NULL);
1736   unparsed_funs_with_default_args = NULL;
1737   unparsed_funs_with_definitions = make_tree_vector ();
1738   unparsed_nsdmis = NULL;
1739 }
1740
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1743 {
1744   release_tree_vector (unparsed_funs_with_definitions);
1745   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1746 }
1747
1748 /* Prototypes.  */
1749
1750 /* Constructors and destructors.  */
1751
1752 static cp_parser *cp_parser_new
1753   (void);
1754
1755 /* Routines to parse various constructs.
1756
1757    Those that return `tree' will return the error_mark_node (rather
1758    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759    Sometimes, they will return an ordinary node if error-recovery was
1760    attempted, even though a parse error occurred.  So, to check
1761    whether or not a parse error occurred, you should always use
1762    cp_parser_error_occurred.  If the construct is optional (indicated
1763    either by an `_opt' in the name of the function that does the
1764    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765    the construct is not present.  */
1766
1767 /* Lexical conventions [gram.lex]  */
1768
1769 static tree cp_parser_identifier
1770   (cp_parser *);
1771 static tree cp_parser_string_literal
1772   (cp_parser *, bool, bool);
1773 static tree cp_parser_userdef_char_literal
1774   (cp_parser *);
1775 static tree cp_parser_userdef_string_literal
1776   (cp_token *);
1777 static tree cp_parser_userdef_numeric_literal
1778   (cp_parser *);
1779
1780 /* Basic concepts [gram.basic]  */
1781
1782 static bool cp_parser_translation_unit
1783   (cp_parser *);
1784
1785 /* Expressions [gram.expr]  */
1786
1787 static tree cp_parser_primary_expression
1788   (cp_parser *, bool, bool, bool, cp_id_kind *);
1789 static tree cp_parser_id_expression
1790   (cp_parser *, bool, bool, bool *, bool, bool);
1791 static tree cp_parser_unqualified_id
1792   (cp_parser *, bool, bool, bool, bool);
1793 static tree cp_parser_nested_name_specifier_opt
1794   (cp_parser *, bool, bool, bool, bool);
1795 static tree cp_parser_nested_name_specifier
1796   (cp_parser *, bool, bool, bool, bool);
1797 static tree cp_parser_qualifying_entity
1798   (cp_parser *, bool, bool, bool, bool, bool);
1799 static tree cp_parser_postfix_expression
1800   (cp_parser *, bool, bool, bool, cp_id_kind *);
1801 static tree cp_parser_postfix_open_square_expression
1802   (cp_parser *, tree, bool);
1803 static tree cp_parser_postfix_dot_deref_expression
1804   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1806   (cp_parser *, int, bool, bool, bool *);
1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1809 static void cp_parser_pseudo_destructor_name
1810   (cp_parser *, tree *, tree *);
1811 static tree cp_parser_unary_expression
1812   (cp_parser *, bool, bool, cp_id_kind *);
1813 static enum tree_code cp_parser_unary_operator
1814   (cp_token *);
1815 static tree cp_parser_new_expression
1816   (cp_parser *);
1817 static VEC(tree,gc) *cp_parser_new_placement
1818   (cp_parser *);
1819 static tree cp_parser_new_type_id
1820   (cp_parser *, tree *);
1821 static cp_declarator *cp_parser_new_declarator_opt
1822   (cp_parser *);
1823 static cp_declarator *cp_parser_direct_new_declarator
1824   (cp_parser *);
1825 static VEC(tree,gc) *cp_parser_new_initializer
1826   (cp_parser *);
1827 static tree cp_parser_delete_expression
1828   (cp_parser *);
1829 static tree cp_parser_cast_expression
1830   (cp_parser *, bool, bool, cp_id_kind *);
1831 static tree cp_parser_binary_expression
1832   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1833 static tree cp_parser_question_colon_clause
1834   (cp_parser *, tree);
1835 static tree cp_parser_assignment_expression
1836   (cp_parser *, bool, cp_id_kind *);
1837 static enum tree_code cp_parser_assignment_operator_opt
1838   (cp_parser *);
1839 static tree cp_parser_expression
1840   (cp_parser *, bool, cp_id_kind *);
1841 static tree cp_parser_constant_expression
1842   (cp_parser *, bool, bool *);
1843 static tree cp_parser_builtin_offsetof
1844   (cp_parser *);
1845 static tree cp_parser_lambda_expression
1846   (cp_parser *);
1847 static void cp_parser_lambda_introducer
1848   (cp_parser *, tree);
1849 static bool cp_parser_lambda_declarator_opt
1850   (cp_parser *, tree);
1851 static void cp_parser_lambda_body
1852   (cp_parser *, tree);
1853
1854 /* Statements [gram.stmt.stmt]  */
1855
1856 static void cp_parser_statement
1857   (cp_parser *, tree, bool, bool *);
1858 static void cp_parser_label_for_labeled_statement
1859   (cp_parser *);
1860 static tree cp_parser_expression_statement
1861   (cp_parser *, tree);
1862 static tree cp_parser_compound_statement
1863   (cp_parser *, tree, bool, bool);
1864 static void cp_parser_statement_seq_opt
1865   (cp_parser *, tree);
1866 static tree cp_parser_selection_statement
1867   (cp_parser *, bool *);
1868 static tree cp_parser_condition
1869   (cp_parser *);
1870 static tree cp_parser_iteration_statement
1871   (cp_parser *);
1872 static bool cp_parser_for_init_statement
1873   (cp_parser *, tree *decl);
1874 static tree cp_parser_for
1875   (cp_parser *);
1876 static tree cp_parser_c_for
1877   (cp_parser *, tree, tree);
1878 static tree cp_parser_range_for
1879   (cp_parser *, tree, tree, tree);
1880 static void do_range_for_auto_deduction
1881   (tree, tree);
1882 static tree cp_parser_perform_range_for_lookup
1883   (tree, tree *, tree *);
1884 static tree cp_parser_range_for_member_function
1885   (tree, tree);
1886 static tree cp_parser_jump_statement
1887   (cp_parser *);
1888 static void cp_parser_declaration_statement
1889   (cp_parser *);
1890
1891 static tree cp_parser_implicitly_scoped_statement
1892   (cp_parser *, bool *);
1893 static void cp_parser_already_scoped_statement
1894   (cp_parser *);
1895
1896 /* Declarations [gram.dcl.dcl] */
1897
1898 static void cp_parser_declaration_seq_opt
1899   (cp_parser *);
1900 static void cp_parser_declaration
1901   (cp_parser *);
1902 static void cp_parser_block_declaration
1903   (cp_parser *, bool);
1904 static void cp_parser_simple_declaration
1905   (cp_parser *, bool, tree *);
1906 static void cp_parser_decl_specifier_seq
1907   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1908 static tree cp_parser_storage_class_specifier_opt
1909   (cp_parser *);
1910 static tree cp_parser_function_specifier_opt
1911   (cp_parser *, cp_decl_specifier_seq *);
1912 static tree cp_parser_type_specifier
1913   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1914    int *, bool *);
1915 static tree cp_parser_simple_type_specifier
1916   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1917 static tree cp_parser_type_name
1918   (cp_parser *);
1919 static tree cp_parser_nonclass_name 
1920   (cp_parser* parser);
1921 static tree cp_parser_elaborated_type_specifier
1922   (cp_parser *, bool, bool);
1923 static tree cp_parser_enum_specifier
1924   (cp_parser *);
1925 static void cp_parser_enumerator_list
1926   (cp_parser *, tree);
1927 static void cp_parser_enumerator_definition
1928   (cp_parser *, tree);
1929 static tree cp_parser_namespace_name
1930   (cp_parser *);
1931 static void cp_parser_namespace_definition
1932   (cp_parser *);
1933 static void cp_parser_namespace_body
1934   (cp_parser *);
1935 static tree cp_parser_qualified_namespace_specifier
1936   (cp_parser *);
1937 static void cp_parser_namespace_alias_definition
1938   (cp_parser *);
1939 static bool cp_parser_using_declaration
1940   (cp_parser *, bool);
1941 static void cp_parser_using_directive
1942   (cp_parser *);
1943 static tree cp_parser_alias_declaration
1944   (cp_parser *);
1945 static void cp_parser_asm_definition
1946   (cp_parser *);
1947 static void cp_parser_linkage_specification
1948   (cp_parser *);
1949 static void cp_parser_static_assert
1950   (cp_parser *, bool);
1951 static tree cp_parser_decltype
1952   (cp_parser *);
1953
1954 /* Declarators [gram.dcl.decl] */
1955
1956 static tree cp_parser_init_declarator
1957   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1958 static cp_declarator *cp_parser_declarator
1959   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1960 static cp_declarator *cp_parser_direct_declarator
1961   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1962 static enum tree_code cp_parser_ptr_operator
1963   (cp_parser *, tree *, cp_cv_quals *);
1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1965   (cp_parser *);
1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1967   (cp_parser *);
1968 static tree cp_parser_late_return_type_opt
1969   (cp_parser *, cp_cv_quals);
1970 static tree cp_parser_declarator_id
1971   (cp_parser *, bool);
1972 static tree cp_parser_type_id
1973   (cp_parser *);
1974 static tree cp_parser_template_type_arg
1975   (cp_parser *);
1976 static tree cp_parser_trailing_type_id (cp_parser *);
1977 static tree cp_parser_type_id_1
1978   (cp_parser *, bool, bool);
1979 static void cp_parser_type_specifier_seq
1980   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1981 static tree cp_parser_parameter_declaration_clause
1982   (cp_parser *);
1983 static tree cp_parser_parameter_declaration_list
1984   (cp_parser *, bool *);
1985 static cp_parameter_declarator *cp_parser_parameter_declaration
1986   (cp_parser *, bool, bool *);
1987 static tree cp_parser_default_argument 
1988   (cp_parser *, bool);
1989 static void cp_parser_function_body
1990   (cp_parser *);
1991 static tree cp_parser_initializer
1992   (cp_parser *, bool *, bool *);
1993 static tree cp_parser_initializer_clause
1994   (cp_parser *, bool *);
1995 static tree cp_parser_braced_list
1996   (cp_parser*, bool*);
1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1998   (cp_parser *, bool *);
1999
2000 static bool cp_parser_ctor_initializer_opt_and_function_body
2001   (cp_parser *);
2002
2003 /* Classes [gram.class] */
2004
2005 static tree cp_parser_class_name
2006   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2007 static tree cp_parser_class_specifier
2008   (cp_parser *);
2009 static tree cp_parser_class_head
2010   (cp_parser *, bool *, tree *, tree *);
2011 static enum tag_types cp_parser_class_key
2012   (cp_parser *);
2013 static void cp_parser_member_specification_opt
2014   (cp_parser *);
2015 static void cp_parser_member_declaration
2016   (cp_parser *);
2017 static tree cp_parser_pure_specifier
2018   (cp_parser *);
2019 static tree cp_parser_constant_initializer
2020   (cp_parser *);
2021
2022 /* Derived classes [gram.class.derived] */
2023
2024 static tree cp_parser_base_clause
2025   (cp_parser *);
2026 static tree cp_parser_base_specifier
2027   (cp_parser *);
2028
2029 /* Special member functions [gram.special] */
2030
2031 static tree cp_parser_conversion_function_id
2032   (cp_parser *);
2033 static tree cp_parser_conversion_type_id
2034   (cp_parser *);
2035 static cp_declarator *cp_parser_conversion_declarator_opt
2036   (cp_parser *);
2037 static bool cp_parser_ctor_initializer_opt
2038   (cp_parser *);
2039 static void cp_parser_mem_initializer_list
2040   (cp_parser *);
2041 static tree cp_parser_mem_initializer
2042   (cp_parser *);
2043 static tree cp_parser_mem_initializer_id
2044   (cp_parser *);
2045
2046 /* Overloading [gram.over] */
2047
2048 static tree cp_parser_operator_function_id
2049   (cp_parser *);
2050 static tree cp_parser_operator
2051   (cp_parser *);
2052
2053 /* Templates [gram.temp] */
2054
2055 static void cp_parser_template_declaration
2056   (cp_parser *, bool);
2057 static tree cp_parser_template_parameter_list
2058   (cp_parser *);
2059 static tree cp_parser_template_parameter
2060   (cp_parser *, bool *, bool *);
2061 static tree cp_parser_type_parameter
2062   (cp_parser *, bool *);
2063 static tree cp_parser_template_id
2064   (cp_parser *, bool, bool, bool);
2065 static tree cp_parser_template_name
2066   (cp_parser *, bool, bool, bool, bool *);
2067 static tree cp_parser_template_argument_list
2068   (cp_parser *);
2069 static tree cp_parser_template_argument
2070   (cp_parser *);
2071 static void cp_parser_explicit_instantiation
2072   (cp_parser *);
2073 static void cp_parser_explicit_specialization
2074   (cp_parser *);
2075
2076 /* Exception handling [gram.exception] */
2077
2078 static tree cp_parser_try_block
2079   (cp_parser *);
2080 static bool cp_parser_function_try_block
2081   (cp_parser *);
2082 static void cp_parser_handler_seq
2083   (cp_parser *);
2084 static void cp_parser_handler
2085   (cp_parser *);
2086 static tree cp_parser_exception_declaration
2087   (cp_parser *);
2088 static tree cp_parser_throw_expression
2089   (cp_parser *);
2090 static tree cp_parser_exception_specification_opt
2091   (cp_parser *);
2092 static tree cp_parser_type_id_list
2093   (cp_parser *);
2094
2095 /* GNU Extensions */
2096
2097 static tree cp_parser_asm_specification_opt
2098   (cp_parser *);
2099 static tree cp_parser_asm_operand_list
2100   (cp_parser *);
2101 static tree cp_parser_asm_clobber_list
2102   (cp_parser *);
2103 static tree cp_parser_asm_label_list
2104   (cp_parser *);
2105 static tree cp_parser_attributes_opt
2106   (cp_parser *);
2107 static tree cp_parser_attribute_list
2108   (cp_parser *);
2109 static bool cp_parser_extension_opt
2110   (cp_parser *, int *);
2111 static void cp_parser_label_declaration
2112   (cp_parser *);
2113
2114 /* Transactional Memory Extensions */
2115
2116 static tree cp_parser_transaction
2117   (cp_parser *, enum rid);
2118 static tree cp_parser_transaction_expression
2119   (cp_parser *, enum rid);
2120 static bool cp_parser_function_transaction
2121   (cp_parser *, enum rid);
2122 static tree cp_parser_transaction_cancel
2123   (cp_parser *);
2124
2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2126 static bool cp_parser_pragma
2127   (cp_parser *, enum pragma_context);
2128
2129 /* Objective-C++ Productions */
2130
2131 static tree cp_parser_objc_message_receiver
2132   (cp_parser *);
2133 static tree cp_parser_objc_message_args
2134   (cp_parser *);
2135 static tree cp_parser_objc_message_expression
2136   (cp_parser *);
2137 static tree cp_parser_objc_encode_expression
2138   (cp_parser *);
2139 static tree cp_parser_objc_defs_expression
2140   (cp_parser *);
2141 static tree cp_parser_objc_protocol_expression
2142   (cp_parser *);
2143 static tree cp_parser_objc_selector_expression
2144   (cp_parser *);
2145 static tree cp_parser_objc_expression
2146   (cp_parser *);
2147 static bool cp_parser_objc_selector_p
2148   (enum cpp_ttype);
2149 static tree cp_parser_objc_selector
2150   (cp_parser *);
2151 static tree cp_parser_objc_protocol_refs_opt
2152   (cp_parser *);
2153 static void cp_parser_objc_declaration
2154   (cp_parser *, tree);
2155 static tree cp_parser_objc_statement
2156   (cp_parser *);
2157 static bool cp_parser_objc_valid_prefix_attributes
2158   (cp_parser *, tree *);
2159 static void cp_parser_objc_at_property_declaration 
2160   (cp_parser *) ;
2161 static void cp_parser_objc_at_synthesize_declaration 
2162   (cp_parser *) ;
2163 static void cp_parser_objc_at_dynamic_declaration
2164   (cp_parser *) ;
2165 static tree cp_parser_objc_struct_declaration
2166   (cp_parser *) ;
2167
2168 /* Utility Routines */
2169
2170 static tree cp_parser_lookup_name
2171   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2172 static tree cp_parser_lookup_name_simple
2173   (cp_parser *, tree, location_t);
2174 static tree cp_parser_maybe_treat_template_as_class
2175   (tree, bool);
2176 static bool cp_parser_check_declarator_template_parameters
2177   (cp_parser *, cp_declarator *, location_t);
2178 static bool cp_parser_check_template_parameters
2179   (cp_parser *, unsigned, location_t, cp_declarator *);
2180 static tree cp_parser_simple_cast_expression
2181   (cp_parser *);
2182 static tree cp_parser_global_scope_opt
2183   (cp_parser *, bool);
2184 static bool cp_parser_constructor_declarator_p
2185   (cp_parser *, bool);
2186 static tree cp_parser_function_definition_from_specifiers_and_declarator
2187   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2188 static tree cp_parser_function_definition_after_declarator
2189   (cp_parser *, bool);
2190 static void cp_parser_template_declaration_after_export
2191   (cp_parser *, bool);
2192 static void cp_parser_perform_template_parameter_access_checks
2193   (VEC (deferred_access_check,gc)*);
2194 static tree cp_parser_single_declaration
2195   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2196 static tree cp_parser_functional_cast
2197   (cp_parser *, tree);
2198 static tree cp_parser_save_member_function_body
2199   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2200 static tree cp_parser_save_nsdmi
2201   (cp_parser *);
2202 static tree cp_parser_enclosed_template_argument_list
2203   (cp_parser *);
2204 static void cp_parser_save_default_args
2205   (cp_parser *, tree);
2206 static void cp_parser_late_parsing_for_member
2207   (cp_parser *, tree);
2208 static tree cp_parser_late_parse_one_default_arg
2209   (cp_parser *, tree, tree, tree);
2210 static void cp_parser_late_parsing_nsdmi
2211   (cp_parser *, tree);
2212 static void cp_parser_late_parsing_default_args
2213   (cp_parser *, tree);
2214 static tree cp_parser_sizeof_operand
2215   (cp_parser *, enum rid);
2216 static tree cp_parser_trait_expr
2217   (cp_parser *, enum rid);
2218 static bool cp_parser_declares_only_class_p
2219   (cp_parser *);
2220 static void cp_parser_set_storage_class
2221   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2222 static void cp_parser_set_decl_spec_type
2223   (cp_decl_specifier_seq *, tree, location_t, bool);
2224 static bool cp_parser_friend_p
2225   (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227   (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229   (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231   (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2233   (cp_token *);
2234 static bool cp_parser_next_token_starts_class_definition_p
2235   (cp_parser *);
2236 static bool cp_parser_next_token_ends_template_argument_p
2237   (cp_parser *);
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239   (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2241   (cp_token *);
2242 static void cp_parser_check_class_key
2243   (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245   (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2247   (cp_parser *);
2248 static void cp_parser_pre_parsed_nested_name_specifier
2249   (cp_parser *);
2250 static bool cp_parser_cache_group
2251   (cp_parser *, enum cpp_ttype, unsigned);
2252 static tree cp_parser_cache_defarg
2253   (cp_parser *parser, bool nsdmi);
2254 static void cp_parser_parse_tentatively
2255   (cp_parser *);
2256 static void cp_parser_commit_to_tentative_parse
2257   (cp_parser *);
2258 static void cp_parser_abort_tentative_parse
2259   (cp_parser *);
2260 static bool cp_parser_parse_definitely
2261   (cp_parser *);
2262 static inline bool cp_parser_parsing_tentatively
2263   (cp_parser *);
2264 static bool cp_parser_uncommitted_to_tentative_parse_p
2265   (cp_parser *);
2266 static void cp_parser_error
2267   (cp_parser *, const char *);
2268 static void cp_parser_name_lookup_error
2269   (cp_parser *, tree, tree, name_lookup_error, location_t);
2270 static bool cp_parser_simulate_error
2271   (cp_parser *);
2272 static bool cp_parser_check_type_definition
2273   (cp_parser *);
2274 static void cp_parser_check_for_definition_in_return_type
2275   (cp_declarator *, tree, location_t type_location);
2276 static void cp_parser_check_for_invalid_template_id
2277   (cp_parser *, tree, location_t location);
2278 static bool cp_parser_non_integral_constant_expression
2279   (cp_parser *, non_integral_constant);
2280 static void cp_parser_diagnose_invalid_type_name
2281   (cp_parser *, tree, tree, location_t);
2282 static bool cp_parser_parse_and_diagnose_invalid_type_name
2283   (cp_parser *);
2284 static int cp_parser_skip_to_closing_parenthesis
2285   (cp_parser *, bool, bool, bool);
2286 static void cp_parser_skip_to_end_of_statement
2287   (cp_parser *);
2288 static void cp_parser_consume_semicolon_at_end_of_statement
2289   (cp_parser *);
2290 static void cp_parser_skip_to_end_of_block_or_statement
2291   (cp_parser *);
2292 static bool cp_parser_skip_to_closing_brace
2293   (cp_parser *);
2294 static void cp_parser_skip_to_end_of_template_parameter_list
2295   (cp_parser *);
2296 static void cp_parser_skip_to_pragma_eol
2297   (cp_parser*, cp_token *);
2298 static bool cp_parser_error_occurred
2299   (cp_parser *);
2300 static bool cp_parser_allow_gnu_extensions_p
2301   (cp_parser *);
2302 static bool cp_parser_is_pure_string_literal
2303   (cp_token *);
2304 static bool cp_parser_is_string_literal
2305   (cp_token *);
2306 static bool cp_parser_is_keyword
2307   (cp_token *, enum rid);
2308 static tree cp_parser_make_typename_type
2309   (cp_parser *, tree, tree, location_t location);
2310 static cp_declarator * cp_parser_make_indirect_declarator
2311   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2312
2313 /* Returns nonzero if we are parsing tentatively.  */
2314
2315 static inline bool
2316 cp_parser_parsing_tentatively (cp_parser* parser)
2317 {
2318   return parser->context->next != NULL;
2319 }
2320
2321 /* Returns nonzero if TOKEN is a string literal.  */
2322
2323 static bool
2324 cp_parser_is_pure_string_literal (cp_token* token)
2325 {
2326   return (token->type == CPP_STRING ||
2327           token->type == CPP_STRING16 ||
2328           token->type == CPP_STRING32 ||
2329           token->type == CPP_WSTRING ||
2330           token->type == CPP_UTF8STRING);
2331 }
2332
2333 /* Returns nonzero if TOKEN is a string literal
2334    of a user-defined string literal.  */
2335
2336 static bool
2337 cp_parser_is_string_literal (cp_token* token)
2338 {
2339   return (cp_parser_is_pure_string_literal (token) ||
2340           token->type == CPP_STRING_USERDEF ||
2341           token->type == CPP_STRING16_USERDEF ||
2342           token->type == CPP_STRING32_USERDEF ||
2343           token->type == CPP_WSTRING_USERDEF ||
2344           token->type == CPP_UTF8STRING_USERDEF);
2345 }
2346
2347 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2348
2349 static bool
2350 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2351 {
2352   return token->keyword == keyword;
2353 }
2354
2355 /* If not parsing tentatively, issue a diagnostic of the form
2356       FILE:LINE: MESSAGE before TOKEN
2357    where TOKEN is the next token in the input stream.  MESSAGE
2358    (specified by the caller) is usually of the form "expected
2359    OTHER-TOKEN".  */
2360
2361 static void
2362 cp_parser_error (cp_parser* parser, const char* gmsgid)
2363 {
2364   if (!cp_parser_simulate_error (parser))
2365     {
2366       cp_token *token = cp_lexer_peek_token (parser->lexer);
2367       /* This diagnostic makes more sense if it is tagged to the line
2368          of the token we just peeked at.  */
2369       cp_lexer_set_source_position_from_token (token);
2370
2371       if (token->type == CPP_PRAGMA)
2372         {
2373           error_at (token->location,
2374                     "%<#pragma%> is not allowed here");
2375           cp_parser_skip_to_pragma_eol (parser, token);
2376           return;
2377         }
2378
2379       c_parse_error (gmsgid,
2380                      /* Because c_parser_error does not understand
2381                         CPP_KEYWORD, keywords are treated like
2382                         identifiers.  */
2383                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2384                      token->u.value, token->flags);
2385     }
2386 }
2387
2388 /* Issue an error about name-lookup failing.  NAME is the
2389    IDENTIFIER_NODE DECL is the result of
2390    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2391    the thing that we hoped to find.  */
2392
2393 static void
2394 cp_parser_name_lookup_error (cp_parser* parser,
2395                              tree name,
2396                              tree decl,
2397                              name_lookup_error desired,
2398                              location_t location)
2399 {
2400   /* If name lookup completely failed, tell the user that NAME was not
2401      declared.  */
2402   if (decl == error_mark_node)
2403     {
2404       if (parser->scope && parser->scope != global_namespace)
2405         error_at (location, "%<%E::%E%> has not been declared",
2406                   parser->scope, name);
2407       else if (parser->scope == global_namespace)
2408         error_at (location, "%<::%E%> has not been declared", name);
2409       else if (parser->object_scope
2410                && !CLASS_TYPE_P (parser->object_scope))
2411         error_at (location, "request for member %qE in non-class type %qT",
2412                   name, parser->object_scope);
2413       else if (parser->object_scope)
2414         error_at (location, "%<%T::%E%> has not been declared",
2415                   parser->object_scope, name);
2416       else
2417         error_at (location, "%qE has not been declared", name);
2418     }
2419   else if (parser->scope && parser->scope != global_namespace)
2420     {
2421       switch (desired)
2422         {
2423           case NLE_TYPE:
2424             error_at (location, "%<%E::%E%> is not a type",
2425                                 parser->scope, name);
2426             break;
2427           case NLE_CXX98:
2428             error_at (location, "%<%E::%E%> is not a class or namespace",
2429                                 parser->scope, name);
2430             break;
2431           case NLE_NOT_CXX98:
2432             error_at (location,
2433                       "%<%E::%E%> is not a class, namespace, or enumeration",
2434                       parser->scope, name);
2435             break;
2436           default:
2437             gcc_unreachable ();
2438             
2439         }
2440     }
2441   else if (parser->scope == global_namespace)
2442     {
2443       switch (desired)
2444         {
2445           case NLE_TYPE:
2446             error_at (location, "%<::%E%> is not a type", name);
2447             break;
2448           case NLE_CXX98:
2449             error_at (location, "%<::%E%> is not a class or namespace", name);
2450             break;
2451           case NLE_NOT_CXX98:
2452             error_at (location,
2453                       "%<::%E%> is not a class, namespace, or enumeration",
2454                       name);
2455             break;
2456           default:
2457             gcc_unreachable ();
2458         }
2459     }
2460   else
2461     {
2462       switch (desired)
2463         {
2464           case NLE_TYPE:
2465             error_at (location, "%qE is not a type", name);
2466             break;
2467           case NLE_CXX98:
2468             error_at (location, "%qE is not a class or namespace", name);
2469             break;
2470           case NLE_NOT_CXX98:
2471             error_at (location,
2472                       "%qE is not a class, namespace, or enumeration", name);
2473             break;
2474           default:
2475             gcc_unreachable ();
2476         }
2477     }
2478 }
2479
2480 /* If we are parsing tentatively, remember that an error has occurred
2481    during this tentative parse.  Returns true if the error was
2482    simulated; false if a message should be issued by the caller.  */
2483
2484 static bool
2485 cp_parser_simulate_error (cp_parser* parser)
2486 {
2487   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2488     {
2489       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2490       return true;
2491     }
2492   return false;
2493 }
2494
2495 /* Check for repeated decl-specifiers.  */
2496
2497 static void
2498 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2499                            location_t location)
2500 {
2501   int ds;
2502
2503   for (ds = ds_first; ds != ds_last; ++ds)
2504     {
2505       unsigned count = decl_specs->specs[ds];
2506       if (count < 2)
2507         continue;
2508       /* The "long" specifier is a special case because of "long long".  */
2509       if (ds == ds_long)
2510         {
2511           if (count > 2)
2512             error_at (location, "%<long long long%> is too long for GCC");
2513           else 
2514             pedwarn_cxx98 (location, OPT_Wlong_long, 
2515                            "ISO C++ 1998 does not support %<long long%>");
2516         }
2517       else if (count > 1)
2518         {
2519           static const char *const decl_spec_names[] = {
2520             "signed",
2521             "unsigned",
2522             "short",
2523             "long",
2524             "const",
2525             "volatile",
2526             "restrict",
2527             "inline",
2528             "virtual",
2529             "explicit",
2530             "friend",
2531             "typedef",
2532             "using",
2533             "constexpr",
2534             "__complex",
2535             "__thread"
2536           };
2537           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2538         }
2539     }
2540 }
2541
2542 /* This function is called when a type is defined.  If type
2543    definitions are forbidden at this point, an error message is
2544    issued.  */
2545
2546 static bool
2547 cp_parser_check_type_definition (cp_parser* parser)
2548 {
2549   /* If types are forbidden here, issue a message.  */
2550   if (parser->type_definition_forbidden_message)
2551     {
2552       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2553          in the message need to be interpreted.  */
2554       error (parser->type_definition_forbidden_message);
2555       return false;
2556     }
2557   return true;
2558 }
2559
2560 /* This function is called when the DECLARATOR is processed.  The TYPE
2561    was a type defined in the decl-specifiers.  If it is invalid to
2562    define a type in the decl-specifiers for DECLARATOR, an error is
2563    issued. TYPE_LOCATION is the location of TYPE and is used
2564    for error reporting.  */
2565
2566 static void
2567 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2568                                                tree type, location_t type_location)
2569 {
2570   /* [dcl.fct] forbids type definitions in return types.
2571      Unfortunately, it's not easy to know whether or not we are
2572      processing a return type until after the fact.  */
2573   while (declarator
2574          && (declarator->kind == cdk_pointer
2575              || declarator->kind == cdk_reference
2576              || declarator->kind == cdk_ptrmem))
2577     declarator = declarator->declarator;
2578   if (declarator
2579       && declarator->kind == cdk_function)
2580     {
2581       error_at (type_location,
2582                 "new types may not be defined in a return type");
2583       inform (type_location, 
2584               "(perhaps a semicolon is missing after the definition of %qT)",
2585               type);
2586     }
2587 }
2588
2589 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2590    "<" in any valid C++ program.  If the next token is indeed "<",
2591    issue a message warning the user about what appears to be an
2592    invalid attempt to form a template-id. LOCATION is the location
2593    of the type-specifier (TYPE) */
2594
2595 static void
2596 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2597                                          tree type, location_t location)
2598 {
2599   cp_token_position start = 0;
2600
2601   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2602     {
2603       if (TYPE_P (type))
2604         error_at (location, "%qT is not a template", type);
2605       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2606         error_at (location, "%qE is not a template", type);
2607       else
2608         error_at (location, "invalid template-id");
2609       /* Remember the location of the invalid "<".  */
2610       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2611         start = cp_lexer_token_position (parser->lexer, true);
2612       /* Consume the "<".  */
2613       cp_lexer_consume_token (parser->lexer);
2614       /* Parse the template arguments.  */
2615       cp_parser_enclosed_template_argument_list (parser);
2616       /* Permanently remove the invalid template arguments so that
2617          this error message is not issued again.  */
2618       if (start)
2619         cp_lexer_purge_tokens_after (parser->lexer, start);
2620     }
2621 }
2622
2623 /* If parsing an integral constant-expression, issue an error message
2624    about the fact that THING appeared and return true.  Otherwise,
2625    return false.  In either case, set
2626    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2627
2628 static bool
2629 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2630                                             non_integral_constant thing)
2631 {
2632   parser->non_integral_constant_expression_p = true;
2633   if (parser->integral_constant_expression_p)
2634     {
2635       if (!parser->allow_non_integral_constant_expression_p)
2636         {
2637           const char *msg = NULL;
2638           switch (thing)
2639             {
2640               case NIC_FLOAT:
2641                 error ("floating-point literal "
2642                        "cannot appear in a constant-expression");
2643                 return true;
2644               case NIC_CAST:
2645                 error ("a cast to a type other than an integral or "
2646                        "enumeration type cannot appear in a "
2647                        "constant-expression");
2648                 return true;
2649               case NIC_TYPEID:
2650                 error ("%<typeid%> operator "
2651                        "cannot appear in a constant-expression");
2652                 return true;
2653               case NIC_NCC:
2654                 error ("non-constant compound literals "
2655                        "cannot appear in a constant-expression");
2656                 return true;
2657               case NIC_FUNC_CALL:
2658                 error ("a function call "
2659                        "cannot appear in a constant-expression");
2660                 return true;
2661               case NIC_INC:
2662                 error ("an increment "
2663                        "cannot appear in a constant-expression");
2664                 return true;
2665               case NIC_DEC:
2666                 error ("an decrement "
2667                        "cannot appear in a constant-expression");
2668                 return true;
2669               case NIC_ARRAY_REF:
2670                 error ("an array reference "
2671                        "cannot appear in a constant-expression");
2672                 return true;
2673               case NIC_ADDR_LABEL:
2674                 error ("the address of a label "
2675                        "cannot appear in a constant-expression");
2676                 return true;
2677               case NIC_OVERLOADED:
2678                 error ("calls to overloaded operators "
2679                        "cannot appear in a constant-expression");
2680                 return true;
2681               case NIC_ASSIGNMENT:
2682                 error ("an assignment cannot appear in a constant-expression");
2683                 return true;
2684               case NIC_COMMA:
2685                 error ("a comma operator "
2686                        "cannot appear in a constant-expression");
2687                 return true;
2688               case NIC_CONSTRUCTOR:
2689                 error ("a call to a constructor "
2690                        "cannot appear in a constant-expression");
2691                 return true;
2692               case NIC_TRANSACTION:
2693                 error ("a transaction expression "
2694                        "cannot appear in a constant-expression");
2695                 return true;
2696               case NIC_THIS:
2697                 msg = "this";
2698                 break;
2699               case NIC_FUNC_NAME:
2700                 msg = "__FUNCTION__";
2701                 break;
2702               case NIC_PRETTY_FUNC:
2703                 msg = "__PRETTY_FUNCTION__";
2704                 break;
2705               case NIC_C99_FUNC:
2706                 msg = "__func__";
2707                 break;
2708               case NIC_VA_ARG:
2709                 msg = "va_arg";
2710                 break;
2711               case NIC_ARROW:
2712                 msg = "->";
2713                 break;
2714               case NIC_POINT:
2715                 msg = ".";
2716                 break;
2717               case NIC_STAR:
2718                 msg = "*";
2719                 break;
2720               case NIC_ADDR:
2721                 msg = "&";
2722                 break;
2723               case NIC_PREINCREMENT:
2724                 msg = "++";
2725                 break;
2726               case NIC_PREDECREMENT:
2727                 msg = "--";
2728                 break;
2729               case NIC_NEW:
2730                 msg = "new";
2731                 break;
2732               case NIC_DEL:
2733                 msg = "delete";
2734                 break;
2735               default:
2736                 gcc_unreachable ();
2737             }
2738           if (msg)
2739             error ("%qs cannot appear in a constant-expression", msg);
2740           return true;
2741         }
2742     }
2743   return false;
2744 }
2745
2746 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2747    qualifying scope (or NULL, if none) for ID.  This function commits
2748    to the current active tentative parse, if any.  (Otherwise, the
2749    problematic construct might be encountered again later, resulting
2750    in duplicate error messages.) LOCATION is the location of ID.  */
2751
2752 static void
2753 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2754                                       tree scope, tree id,
2755                                       location_t location)
2756 {
2757   tree decl, old_scope;
2758   cp_parser_commit_to_tentative_parse (parser);
2759   /* Try to lookup the identifier.  */
2760   old_scope = parser->scope;
2761   parser->scope = scope;
2762   decl = cp_parser_lookup_name_simple (parser, id, location);
2763   parser->scope = old_scope;
2764   /* If the lookup found a template-name, it means that the user forgot
2765   to specify an argument list. Emit a useful error message.  */
2766   if (TREE_CODE (decl) == TEMPLATE_DECL)
2767     error_at (location,
2768               "invalid use of template-name %qE without an argument list",
2769               decl);
2770   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2771     error_at (location, "invalid use of destructor %qD as a type", id);
2772   else if (TREE_CODE (decl) == TYPE_DECL)
2773     /* Something like 'unsigned A a;'  */
2774     error_at (location, "invalid combination of multiple type-specifiers");
2775   else if (!parser->scope)
2776     {
2777       /* Issue an error message.  */
2778       error_at (location, "%qE does not name a type", id);
2779       /* If we're in a template class, it's possible that the user was
2780          referring to a type from a base class.  For example:
2781
2782            template <typename T> struct A { typedef T X; };
2783            template <typename T> struct B : public A<T> { X x; };
2784
2785          The user should have said "typename A<T>::X".  */
2786       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2787         inform (location, "C++11 %<constexpr%> only available with "
2788                 "-std=c++11 or -std=gnu++11");
2789       else if (processing_template_decl && current_class_type
2790                && TYPE_BINFO (current_class_type))
2791         {
2792           tree b;
2793
2794           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2795                b;
2796                b = TREE_CHAIN (b))
2797             {
2798               tree base_type = BINFO_TYPE (b);
2799               if (CLASS_TYPE_P (base_type)
2800                   && dependent_type_p (base_type))
2801                 {
2802                   tree field;
2803                   /* Go from a particular instantiation of the
2804                      template (which will have an empty TYPE_FIELDs),
2805                      to the main version.  */
2806                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2807                   for (field = TYPE_FIELDS (base_type);
2808                        field;
2809                        field = DECL_CHAIN (field))
2810                     if (TREE_CODE (field) == TYPE_DECL
2811                         && DECL_NAME (field) == id)
2812                       {
2813                         inform (location, 
2814                                 "(perhaps %<typename %T::%E%> was intended)",
2815                                 BINFO_TYPE (b), id);
2816                         break;
2817                       }
2818                   if (field)
2819                     break;
2820                 }
2821             }
2822         }
2823     }
2824   /* Here we diagnose qualified-ids where the scope is actually correct,
2825      but the identifier does not resolve to a valid type name.  */
2826   else if (parser->scope != error_mark_node)
2827     {
2828       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2829         error_at (location, "%qE in namespace %qE does not name a type",
2830                   id, parser->scope);
2831       else if (CLASS_TYPE_P (parser->scope)
2832                && constructor_name_p (id, parser->scope))
2833         {
2834           /* A<T>::A<T>() */
2835           error_at (location, "%<%T::%E%> names the constructor, not"
2836                     " the type", parser->scope, id);
2837           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2838             error_at (location, "and %qT has no template constructors",
2839                       parser->scope);
2840         }
2841       else if (TYPE_P (parser->scope)
2842                && dependent_scope_p (parser->scope))
2843         error_at (location, "need %<typename%> before %<%T::%E%> because "
2844                   "%qT is a dependent scope",
2845                   parser->scope, id, parser->scope);
2846       else if (TYPE_P (parser->scope))
2847         error_at (location, "%qE in %q#T does not name a type",
2848                   id, parser->scope);
2849       else
2850         gcc_unreachable ();
2851     }
2852 }
2853
2854 /* Check for a common situation where a type-name should be present,
2855    but is not, and issue a sensible error message.  Returns true if an
2856    invalid type-name was detected.
2857
2858    The situation handled by this function are variable declarations of the
2859    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2860    Usually, `ID' should name a type, but if we got here it means that it
2861    does not. We try to emit the best possible error message depending on
2862    how exactly the id-expression looks like.  */
2863
2864 static bool
2865 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2866 {
2867   tree id;
2868   cp_token *token = cp_lexer_peek_token (parser->lexer);
2869
2870   /* Avoid duplicate error about ambiguous lookup.  */
2871   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2872     {
2873       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2874       if (next->type == CPP_NAME && next->ambiguous_p)
2875         goto out;
2876     }
2877
2878   cp_parser_parse_tentatively (parser);
2879   id = cp_parser_id_expression (parser,
2880                                 /*template_keyword_p=*/false,
2881                                 /*check_dependency_p=*/true,
2882                                 /*template_p=*/NULL,
2883                                 /*declarator_p=*/true,
2884                                 /*optional_p=*/false);
2885   /* If the next token is a (, this is a function with no explicit return
2886      type, i.e. constructor, destructor or conversion op.  */
2887   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2888       || TREE_CODE (id) == TYPE_DECL)
2889     {
2890       cp_parser_abort_tentative_parse (parser);
2891       return false;
2892     }
2893   if (!cp_parser_parse_definitely (parser))
2894     return false;
2895
2896   /* Emit a diagnostic for the invalid type.  */
2897   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2898                                         id, token->location);
2899  out:
2900   /* If we aren't in the middle of a declarator (i.e. in a
2901      parameter-declaration-clause), skip to the end of the declaration;
2902      there's no point in trying to process it.  */
2903   if (!parser->in_declarator_p)
2904     cp_parser_skip_to_end_of_block_or_statement (parser);
2905   return true;
2906 }
2907
2908 /* Consume tokens up to, and including, the next non-nested closing `)'.
2909    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2910    are doing error recovery. Returns -1 if OR_COMMA is true and we
2911    found an unnested comma.  */
2912
2913 static int
2914 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2915                                        bool recovering,
2916                                        bool or_comma,
2917                                        bool consume_paren)
2918 {
2919   unsigned paren_depth = 0;
2920   unsigned brace_depth = 0;
2921   unsigned square_depth = 0;
2922
2923   if (recovering && !or_comma
2924       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2925     return 0;
2926
2927   while (true)
2928     {
2929       cp_token * token = cp_lexer_peek_token (parser->lexer);
2930
2931       switch (token->type)
2932         {
2933         case CPP_EOF:
2934         case CPP_PRAGMA_EOL:
2935           /* If we've run out of tokens, then there is no closing `)'.  */
2936           return 0;
2937
2938         /* This is good for lambda expression capture-lists.  */
2939         case CPP_OPEN_SQUARE:
2940           ++square_depth;
2941           break;
2942         case CPP_CLOSE_SQUARE:
2943           if (!square_depth--)
2944             return 0;
2945           break;
2946
2947         case CPP_SEMICOLON:
2948           /* This matches the processing in skip_to_end_of_statement.  */
2949           if (!brace_depth)
2950             return 0;
2951           break;
2952
2953         case CPP_OPEN_BRACE:
2954           ++brace_depth;
2955           break;
2956         case CPP_CLOSE_BRACE:
2957           if (!brace_depth--)
2958             return 0;
2959           break;
2960
2961         case CPP_COMMA:
2962           if (recovering && or_comma && !brace_depth && !paren_depth
2963               && !square_depth)
2964             return -1;
2965           break;
2966
2967         case CPP_OPEN_PAREN:
2968           if (!brace_depth)
2969             ++paren_depth;
2970           break;
2971
2972         case CPP_CLOSE_PAREN:
2973           if (!brace_depth && !paren_depth--)
2974             {
2975               if (consume_paren)
2976                 cp_lexer_consume_token (parser->lexer);
2977               return 1;
2978             }
2979           break;
2980
2981         default:
2982           break;
2983         }
2984
2985       /* Consume the token.  */
2986       cp_lexer_consume_token (parser->lexer);
2987     }
2988 }
2989
2990 /* Consume tokens until we reach the end of the current statement.
2991    Normally, that will be just before consuming a `;'.  However, if a
2992    non-nested `}' comes first, then we stop before consuming that.  */
2993
2994 static void
2995 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2996 {
2997   unsigned nesting_depth = 0;
2998
2999   while (true)
3000     {
3001       cp_token *token = cp_lexer_peek_token (parser->lexer);
3002
3003       switch (token->type)
3004         {
3005         case CPP_EOF:
3006         case CPP_PRAGMA_EOL:
3007           /* If we've run out of tokens, stop.  */
3008           return;
3009
3010         case CPP_SEMICOLON:
3011           /* If the next token is a `;', we have reached the end of the
3012              statement.  */
3013           if (!nesting_depth)
3014             return;
3015           break;
3016
3017         case CPP_CLOSE_BRACE:
3018           /* If this is a non-nested '}', stop before consuming it.
3019              That way, when confronted with something like:
3020
3021                { 3 + }
3022
3023              we stop before consuming the closing '}', even though we
3024              have not yet reached a `;'.  */
3025           if (nesting_depth == 0)
3026             return;
3027
3028           /* If it is the closing '}' for a block that we have
3029              scanned, stop -- but only after consuming the token.
3030              That way given:
3031
3032                 void f g () { ... }
3033                 typedef int I;
3034
3035              we will stop after the body of the erroneously declared
3036              function, but before consuming the following `typedef'
3037              declaration.  */
3038           if (--nesting_depth == 0)
3039             {
3040               cp_lexer_consume_token (parser->lexer);
3041               return;
3042             }
3043
3044         case CPP_OPEN_BRACE:
3045           ++nesting_depth;
3046           break;
3047
3048         default:
3049           break;
3050         }
3051
3052       /* Consume the token.  */
3053       cp_lexer_consume_token (parser->lexer);
3054     }
3055 }
3056
3057 /* This function is called at the end of a statement or declaration.
3058    If the next token is a semicolon, it is consumed; otherwise, error
3059    recovery is attempted.  */
3060
3061 static void
3062 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3063 {
3064   /* Look for the trailing `;'.  */
3065   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3066     {
3067       /* If there is additional (erroneous) input, skip to the end of
3068          the statement.  */
3069       cp_parser_skip_to_end_of_statement (parser);
3070       /* If the next token is now a `;', consume it.  */
3071       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3072         cp_lexer_consume_token (parser->lexer);
3073     }
3074 }
3075
3076 /* Skip tokens until we have consumed an entire block, or until we
3077    have consumed a non-nested `;'.  */
3078
3079 static void
3080 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3081 {
3082   int nesting_depth = 0;
3083
3084   while (nesting_depth >= 0)
3085     {
3086       cp_token *token = cp_lexer_peek_token (parser->lexer);
3087
3088       switch (token->type)
3089         {
3090         case CPP_EOF:
3091         case CPP_PRAGMA_EOL:
3092           /* If we've run out of tokens, stop.  */
3093           return;
3094
3095         case CPP_SEMICOLON:
3096           /* Stop if this is an unnested ';'. */
3097           if (!nesting_depth)
3098             nesting_depth = -1;
3099           break;
3100
3101         case CPP_CLOSE_BRACE:
3102           /* Stop if this is an unnested '}', or closes the outermost
3103              nesting level.  */
3104           nesting_depth--;
3105           if (nesting_depth < 0)
3106             return;
3107           if (!nesting_depth)
3108             nesting_depth = -1;
3109           break;
3110
3111         case CPP_OPEN_BRACE:
3112           /* Nest. */
3113           nesting_depth++;
3114           break;
3115
3116         default:
3117           break;
3118         }
3119
3120       /* Consume the token.  */
3121       cp_lexer_consume_token (parser->lexer);
3122     }
3123 }
3124
3125 /* Skip tokens until a non-nested closing curly brace is the next
3126    token, or there are no more tokens. Return true in the first case,
3127    false otherwise.  */
3128
3129 static bool
3130 cp_parser_skip_to_closing_brace (cp_parser *parser)
3131 {
3132   unsigned nesting_depth = 0;
3133
3134   while (true)
3135     {
3136       cp_token *token = cp_lexer_peek_token (parser->lexer);
3137
3138       switch (token->type)
3139         {
3140         case CPP_EOF:
3141         case CPP_PRAGMA_EOL:
3142           /* If we've run out of tokens, stop.  */
3143           return false;
3144
3145         case CPP_CLOSE_BRACE:
3146           /* If the next token is a non-nested `}', then we have reached
3147              the end of the current block.  */
3148           if (nesting_depth-- == 0)
3149             return true;
3150           break;
3151
3152         case CPP_OPEN_BRACE:
3153           /* If it the next token is a `{', then we are entering a new
3154              block.  Consume the entire block.  */
3155           ++nesting_depth;
3156           break;
3157
3158         default:
3159           break;
3160         }
3161
3162       /* Consume the token.  */
3163       cp_lexer_consume_token (parser->lexer);
3164     }
3165 }
3166
3167 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3168    parameter is the PRAGMA token, allowing us to purge the entire pragma
3169    sequence.  */
3170
3171 static void
3172 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3173 {
3174   cp_token *token;
3175
3176   parser->lexer->in_pragma = false;
3177
3178   do
3179     token = cp_lexer_consume_token (parser->lexer);
3180   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3181
3182   /* Ensure that the pragma is not parsed again.  */
3183   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3184 }
3185
3186 /* Require pragma end of line, resyncing with it as necessary.  The
3187    arguments are as for cp_parser_skip_to_pragma_eol.  */
3188
3189 static void
3190 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3191 {
3192   parser->lexer->in_pragma = false;
3193   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3194     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3195 }
3196
3197 /* This is a simple wrapper around make_typename_type. When the id is
3198    an unresolved identifier node, we can provide a superior diagnostic
3199    using cp_parser_diagnose_invalid_type_name.  */
3200
3201 static tree
3202 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3203                               tree id, location_t id_location)
3204 {
3205   tree result;
3206   if (TREE_CODE (id) == IDENTIFIER_NODE)
3207     {
3208       result = make_typename_type (scope, id, typename_type,
3209                                    /*complain=*/tf_none);
3210       if (result == error_mark_node)
3211         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3212       return result;
3213     }
3214   return make_typename_type (scope, id, typename_type, tf_error);
3215 }
3216
3217 /* This is a wrapper around the
3218    make_{pointer,ptrmem,reference}_declarator functions that decides
3219    which one to call based on the CODE and CLASS_TYPE arguments. The
3220    CODE argument should be one of the values returned by
3221    cp_parser_ptr_operator. */
3222 static cp_declarator *
3223 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3224                                     cp_cv_quals cv_qualifiers,
3225                                     cp_declarator *target)
3226 {
3227   if (code == ERROR_MARK)
3228     return cp_error_declarator;
3229
3230   if (code == INDIRECT_REF)
3231     if (class_type == NULL_TREE)
3232       return make_pointer_declarator (cv_qualifiers, target);
3233     else
3234       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3235   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3236     return make_reference_declarator (cv_qualifiers, target, false);
3237   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3238     return make_reference_declarator (cv_qualifiers, target, true);
3239   gcc_unreachable ();
3240 }
3241
3242 /* Create a new C++ parser.  */
3243
3244 static cp_parser *
3245 cp_parser_new (void)
3246 {
3247   cp_parser *parser;
3248   cp_lexer *lexer;
3249   unsigned i;
3250
3251   /* cp_lexer_new_main is called before doing GC allocation because
3252      cp_lexer_new_main might load a PCH file.  */
3253   lexer = cp_lexer_new_main ();
3254
3255   /* Initialize the binops_by_token so that we can get the tree
3256      directly from the token.  */
3257   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3258     binops_by_token[binops[i].token_type] = binops[i];
3259
3260   parser = ggc_alloc_cleared_cp_parser ();
3261   parser->lexer = lexer;
3262   parser->context = cp_parser_context_new (NULL);
3263
3264   /* For now, we always accept GNU extensions.  */
3265   parser->allow_gnu_extensions_p = 1;
3266
3267   /* The `>' token is a greater-than operator, not the end of a
3268      template-id.  */
3269   parser->greater_than_is_operator_p = true;
3270
3271   parser->default_arg_ok_p = true;
3272
3273   /* We are not parsing a constant-expression.  */
3274   parser->integral_constant_expression_p = false;
3275   parser->allow_non_integral_constant_expression_p = false;
3276   parser->non_integral_constant_expression_p = false;
3277
3278   /* Local variable names are not forbidden.  */
3279   parser->local_variables_forbidden_p = false;
3280
3281   /* We are not processing an `extern "C"' declaration.  */
3282   parser->in_unbraced_linkage_specification_p = false;
3283
3284   /* We are not processing a declarator.  */
3285   parser->in_declarator_p = false;
3286
3287   /* We are not processing a template-argument-list.  */
3288   parser->in_template_argument_list_p = false;
3289
3290   /* We are not in an iteration statement.  */
3291   parser->in_statement = 0;
3292
3293   /* We are not in a switch statement.  */
3294   parser->in_switch_statement_p = false;
3295
3296   /* We are not parsing a type-id inside an expression.  */
3297   parser->in_type_id_in_expr_p = false;
3298
3299   /* Declarations aren't implicitly extern "C".  */
3300   parser->implicit_extern_c = false;
3301
3302   /* String literals should be translated to the execution character set.  */
3303   parser->translate_strings_p = true;
3304
3305   /* We are not parsing a function body.  */
3306   parser->in_function_body = false;
3307
3308   /* We can correct until told otherwise.  */
3309   parser->colon_corrects_to_scope_p = true;
3310
3311   /* The unparsed function queue is empty.  */
3312   push_unparsed_function_queues (parser);
3313
3314   /* There are no classes being defined.  */
3315   parser->num_classes_being_defined = 0;
3316
3317   /* No template parameters apply.  */
3318   parser->num_template_parameter_lists = 0;
3319
3320   return parser;
3321 }
3322
3323 /* Create a cp_lexer structure which will emit the tokens in CACHE
3324    and push it onto the parser's lexer stack.  This is used for delayed
3325    parsing of in-class method bodies and default arguments, and should
3326    not be confused with tentative parsing.  */
3327 static void
3328 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3329 {
3330   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3331   lexer->next = parser->lexer;
3332   parser->lexer = lexer;
3333
3334   /* Move the current source position to that of the first token in the
3335      new lexer.  */
3336   cp_lexer_set_source_position_from_token (lexer->next_token);
3337 }
3338
3339 /* Pop the top lexer off the parser stack.  This is never used for the
3340    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3341 static void
3342 cp_parser_pop_lexer (cp_parser *parser)
3343 {
3344   cp_lexer *lexer = parser->lexer;
3345   parser->lexer = lexer->next;
3346   cp_lexer_destroy (lexer);
3347
3348   /* Put the current source position back where it was before this
3349      lexer was pushed.  */
3350   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3351 }
3352
3353 /* Lexical conventions [gram.lex]  */
3354
3355 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3356    identifier.  */
3357
3358 static tree
3359 cp_parser_identifier (cp_parser* parser)
3360 {
3361   cp_token *token;
3362
3363   /* Look for the identifier.  */
3364   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3365   /* Return the value.  */
3366   return token ? token->u.value : error_mark_node;
3367 }
3368
3369 /* Parse a sequence of adjacent string constants.  Returns a
3370    TREE_STRING representing the combined, nul-terminated string
3371    constant.  If TRANSLATE is true, translate the string to the
3372    execution character set.  If WIDE_OK is true, a wide string is
3373    invalid here.
3374
3375    C++98 [lex.string] says that if a narrow string literal token is
3376    adjacent to a wide string literal token, the behavior is undefined.
3377    However, C99 6.4.5p4 says that this results in a wide string literal.
3378    We follow C99 here, for consistency with the C front end.
3379
3380    This code is largely lifted from lex_string() in c-lex.c.
3381
3382    FUTURE: ObjC++ will need to handle @-strings here.  */
3383 static tree
3384 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3385 {
3386   tree value;
3387   size_t count;
3388   struct obstack str_ob;
3389   cpp_string str, istr, *strs;
3390   cp_token *tok;
3391   enum cpp_ttype type, curr_type;
3392   int have_suffix_p = 0;
3393   tree string_tree;
3394   tree suffix_id = NULL_TREE;
3395   bool curr_tok_is_userdef_p = false;
3396
3397   tok = cp_lexer_peek_token (parser->lexer);
3398   if (!cp_parser_is_string_literal (tok))
3399     {
3400       cp_parser_error (parser, "expected string-literal");
3401       return error_mark_node;
3402     }
3403
3404   if (cpp_userdef_string_p (tok->type))
3405     {
3406       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3407       curr_type = cpp_userdef_string_remove_type (tok->type);
3408       curr_tok_is_userdef_p = true;
3409     }
3410   else
3411     {
3412       string_tree = tok->u.value;
3413       curr_type = tok->type;
3414     }
3415   type = curr_type;
3416
3417   /* Try to avoid the overhead of creating and destroying an obstack
3418      for the common case of just one string.  */
3419   if (!cp_parser_is_string_literal
3420       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3421     {
3422       cp_lexer_consume_token (parser->lexer);
3423
3424       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3425       str.len = TREE_STRING_LENGTH (string_tree);
3426       count = 1;
3427
3428       if (curr_tok_is_userdef_p)
3429         {
3430           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3431           have_suffix_p = 1;
3432           curr_type = cpp_userdef_string_remove_type (tok->type);
3433         }
3434       else
3435         curr_type = tok->type;
3436
3437       strs = &str;
3438     }
3439   else
3440     {
3441       gcc_obstack_init (&str_ob);
3442       count = 0;
3443
3444       do
3445         {
3446           cp_lexer_consume_token (parser->lexer);
3447           count++;
3448           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3449           str.len = TREE_STRING_LENGTH (string_tree);
3450
3451           if (curr_tok_is_userdef_p)
3452             {
3453               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3454               if (have_suffix_p == 0)
3455                 {
3456                   suffix_id = curr_suffix_id;
3457                   have_suffix_p = 1;
3458                 }
3459               else if (have_suffix_p == 1
3460                        && curr_suffix_id != suffix_id)
3461                 {
3462                   error ("inconsistent user-defined literal suffixes"
3463                          " %qD and %qD in string literal",
3464                          suffix_id, curr_suffix_id);
3465                   have_suffix_p = -1;
3466                 }
3467               curr_type = cpp_userdef_string_remove_type (tok->type);
3468             }
3469           else
3470             curr_type = tok->type;
3471
3472           if (type != curr_type)
3473             {
3474               if (type == CPP_STRING)
3475                 type = curr_type;
3476               else if (curr_type != CPP_STRING)
3477                 error_at (tok->location,
3478                           "unsupported non-standard concatenation "
3479                           "of string literals");
3480             }
3481
3482           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3483
3484           tok = cp_lexer_peek_token (parser->lexer);
3485           if (cpp_userdef_string_p (tok->type))
3486             {
3487               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3488               curr_type = cpp_userdef_string_remove_type (tok->type);
3489               curr_tok_is_userdef_p = true;
3490             }
3491           else
3492             {
3493               string_tree = tok->u.value;
3494               curr_type = tok->type;
3495               curr_tok_is_userdef_p = false;
3496             }
3497         }
3498       while (cp_parser_is_string_literal (tok));
3499
3500       strs = (cpp_string *) obstack_finish (&str_ob);
3501     }
3502
3503   if (type != CPP_STRING && !wide_ok)
3504     {
3505       cp_parser_error (parser, "a wide string is invalid in this context");
3506       type = CPP_STRING;
3507     }
3508
3509   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3510       (parse_in, strs, count, &istr, type))
3511     {
3512       value = build_string (istr.len, (const char *)istr.text);
3513       free (CONST_CAST (unsigned char *, istr.text));
3514
3515       switch (type)
3516         {
3517         default:
3518         case CPP_STRING:
3519         case CPP_UTF8STRING:
3520           TREE_TYPE (value) = char_array_type_node;
3521           break;
3522         case CPP_STRING16:
3523           TREE_TYPE (value) = char16_array_type_node;
3524           break;
3525         case CPP_STRING32:
3526           TREE_TYPE (value) = char32_array_type_node;
3527           break;
3528         case CPP_WSTRING:
3529           TREE_TYPE (value) = wchar_array_type_node;
3530           break;
3531         }
3532
3533       value = fix_string_type (value);
3534
3535       if (have_suffix_p)
3536         {
3537           tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3538           tok->u.value = literal;
3539           return cp_parser_userdef_string_literal (tok);
3540         }
3541     }
3542   else
3543     /* cpp_interpret_string has issued an error.  */
3544     value = error_mark_node;
3545
3546   if (count > 1)
3547     obstack_free (&str_ob, 0);
3548
3549   return value;
3550 }
3551
3552 /* Look up a literal operator with the name and the exact arguments.  */
3553
3554 static tree
3555 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3556 {
3557   tree decl, fns;
3558   decl = lookup_name (name);
3559   if (!decl || !is_overloaded_fn (decl))
3560     return error_mark_node;
3561
3562   for (fns = decl; fns; fns = OVL_NEXT (fns))
3563     {
3564       unsigned int ix;
3565       bool found = true;
3566       tree fn = OVL_CURRENT (fns);
3567       tree argtypes = NULL_TREE;
3568       argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3569       if (argtypes != NULL_TREE)
3570         {
3571           for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3572                ++ix, argtypes = TREE_CHAIN (argtypes))
3573             {
3574               tree targ = TREE_VALUE (argtypes);
3575               tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3576               bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3577               bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3578               if ((ptr || arr || !same_type_p (targ, tparm))
3579                   && (!ptr || !arr
3580                       || !same_type_p (TREE_TYPE (targ),
3581                                        TREE_TYPE (tparm))))
3582                 found = false;
3583             }
3584           if (found)
3585             return fn;
3586         }
3587     }
3588
3589   return error_mark_node;
3590 }
3591
3592 /* Parse a user-defined char constant.  Returns a call to a user-defined
3593    literal operator taking the character as an argument.  */
3594
3595 static tree
3596 cp_parser_userdef_char_literal (cp_parser *parser)
3597 {
3598   cp_token *token = cp_lexer_consume_token (parser->lexer);
3599   tree literal = token->u.value;
3600   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3601   tree value = USERDEF_LITERAL_VALUE (literal);
3602   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3603   tree decl, result;
3604
3605   /* Build up a call to the user-defined operator  */
3606   /* Lookup the name we got back from the id-expression.  */
3607   VEC(tree,gc) *args = make_tree_vector ();
3608   VEC_safe_push (tree, gc, args, value);
3609   decl = lookup_literal_operator (name, args);
3610   if (!decl || decl == error_mark_node)
3611     {
3612       error ("unable to find character literal operator %qD with %qT argument",
3613              name, TREE_TYPE (value));
3614       release_tree_vector (args);
3615       return error_mark_node;
3616     }
3617   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3618   release_tree_vector (args);
3619   if (result != error_mark_node)
3620     return result;
3621
3622   error ("unable to find character literal operator %qD with %qT argument",
3623          name, TREE_TYPE (value));
3624   return error_mark_node;
3625 }
3626
3627 /* A subroutine of cp_parser_userdef_numeric_literal to
3628    create a char... template parameter pack from a string node.  */
3629
3630 static tree
3631 make_char_string_pack (tree value)
3632 {
3633   tree charvec;
3634   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3635   const char *str = TREE_STRING_POINTER (value);
3636   int i, len = TREE_STRING_LENGTH (value) - 1;
3637   tree argvec = make_tree_vec (1);
3638
3639   /* Fill in CHARVEC with all of the parameters.  */
3640   charvec = make_tree_vec (len);
3641   for (i = 0; i < len; ++i)
3642     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3643
3644   /* Build the argument packs.  */
3645   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3646   TREE_TYPE (argpack) = char_type_node;
3647
3648   TREE_VEC_ELT (argvec, 0) = argpack;
3649
3650   return argvec;
3651 }
3652
3653 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3654    literal operator.  */
3655
3656 static tree
3657 cp_parser_userdef_numeric_literal (cp_parser *parser)
3658 {
3659   cp_token *token = cp_lexer_consume_token (parser->lexer);
3660   tree literal = token->u.value;
3661   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3662   tree value = USERDEF_LITERAL_VALUE (literal);
3663   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3664   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3665   tree decl, result;
3666   VEC(tree,gc) *args;
3667
3668   /* Look for a literal operator taking the exact type of numeric argument
3669      as the literal value.  */
3670   args = make_tree_vector ();
3671   VEC_safe_push (tree, gc, args, value);
3672   decl = lookup_literal_operator (name, args);
3673   if (decl && decl != error_mark_node)
3674     {
3675       result = finish_call_expr (decl, &args, false, true, tf_none);
3676       if (result != error_mark_node)
3677         {
3678           release_tree_vector (args);
3679           return result;
3680         }
3681     }
3682   release_tree_vector (args);
3683
3684   /* If the numeric argument didn't work, look for a raw literal
3685      operator taking a const char* argument consisting of the number
3686      in string format.  */
3687   args = make_tree_vector ();
3688   VEC_safe_push (tree, gc, args, num_string);
3689   decl = lookup_literal_operator (name, args);
3690   if (decl && decl != error_mark_node)
3691     {
3692       result = finish_call_expr (decl, &args, false, true, tf_none);
3693       if (result != error_mark_node)
3694         {
3695           release_tree_vector (args);
3696           return result;
3697         }
3698     }
3699   release_tree_vector (args);
3700
3701   /* If the raw literal didn't work, look for a non-type template
3702      function with parameter pack char....  Call the function with
3703      template parameter characters representing the number.  */
3704   args = make_tree_vector ();
3705   decl = lookup_literal_operator (name, args);
3706   if (decl && decl != error_mark_node)
3707     {
3708       tree tmpl_args = make_char_string_pack (num_string);
3709       decl = lookup_template_function (decl, tmpl_args);
3710       result = finish_call_expr (decl, &args, false, true, tf_none);
3711       if (result != error_mark_node)
3712         {
3713           release_tree_vector (args);
3714           return result;
3715         }
3716     }
3717   release_tree_vector (args);
3718
3719   error ("unable to find numeric literal operator %qD", name);
3720   return error_mark_node;
3721 }
3722
3723 /* Parse a user-defined string constant.  Returns a call to a user-defined
3724    literal operator taking a character pointer and the length of the string
3725    as arguments.  */
3726
3727 static tree
3728 cp_parser_userdef_string_literal (cp_token *token)
3729 {
3730   tree literal = token->u.value;
3731   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3732   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3733   tree value = USERDEF_LITERAL_VALUE (literal);
3734   int len = TREE_STRING_LENGTH (value)
3735         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3736   tree decl, result;
3737
3738   /* Build up a call to the user-defined operator  */
3739   /* Lookup the name we got back from the id-expression.  */
3740   VEC(tree,gc) *args = make_tree_vector ();
3741   VEC_safe_push (tree, gc, args, value);
3742   VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3743   decl = lookup_name (name);
3744   if (!decl || decl == error_mark_node)
3745     {
3746       error ("unable to find string literal operator %qD", name);
3747       release_tree_vector (args);
3748       return error_mark_node;
3749     }
3750   result = finish_call_expr (decl, &args, false, true, tf_none);
3751   release_tree_vector (args);
3752   if (result != error_mark_node)
3753     return result;
3754
3755   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3756          name, TREE_TYPE (value), size_type_node);
3757   return error_mark_node;
3758 }
3759
3760
3761 /* Basic concepts [gram.basic]  */
3762
3763 /* Parse a translation-unit.
3764
3765    translation-unit:
3766      declaration-seq [opt]
3767
3768    Returns TRUE if all went well.  */
3769
3770 static bool
3771 cp_parser_translation_unit (cp_parser* parser)
3772 {
3773   /* The address of the first non-permanent object on the declarator
3774      obstack.  */
3775   static void *declarator_obstack_base;
3776
3777   bool success;
3778
3779   /* Create the declarator obstack, if necessary.  */
3780   if (!cp_error_declarator)
3781     {
3782       gcc_obstack_init (&declarator_obstack);
3783       /* Create the error declarator.  */
3784       cp_error_declarator = make_declarator (cdk_error);
3785       /* Create the empty parameter list.  */
3786       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3787       /* Remember where the base of the declarator obstack lies.  */
3788       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3789     }
3790
3791   cp_parser_declaration_seq_opt (parser);
3792
3793   /* If there are no tokens left then all went well.  */
3794   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3795     {
3796       /* Get rid of the token array; we don't need it any more.  */
3797       cp_lexer_destroy (parser->lexer);
3798       parser->lexer = NULL;
3799
3800       /* This file might have been a context that's implicitly extern
3801          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3802       if (parser->implicit_extern_c)
3803         {
3804           pop_lang_context ();
3805           parser->implicit_extern_c = false;
3806         }
3807
3808       /* Finish up.  */
3809       finish_translation_unit ();
3810
3811       success = true;
3812     }
3813   else
3814     {
3815       cp_parser_error (parser, "expected declaration");
3816       success = false;
3817     }
3818
3819   /* Make sure the declarator obstack was fully cleaned up.  */
3820   gcc_assert (obstack_next_free (&declarator_obstack)
3821               == declarator_obstack_base);
3822
3823   /* All went well.  */
3824   return success;
3825 }
3826
3827 /* Expressions [gram.expr] */
3828
3829 /* Parse a primary-expression.
3830
3831    primary-expression:
3832      literal
3833      this
3834      ( expression )
3835      id-expression
3836
3837    GNU Extensions:
3838
3839    primary-expression:
3840      ( compound-statement )
3841      __builtin_va_arg ( assignment-expression , type-id )
3842      __builtin_offsetof ( type-id , offsetof-expression )
3843
3844    C++ Extensions:
3845      __has_nothrow_assign ( type-id )   
3846      __has_nothrow_constructor ( type-id )
3847      __has_nothrow_copy ( type-id )
3848      __has_trivial_assign ( type-id )   
3849      __has_trivial_constructor ( type-id )
3850      __has_trivial_copy ( type-id )
3851      __has_trivial_destructor ( type-id )
3852      __has_virtual_destructor ( type-id )     
3853      __is_abstract ( type-id )
3854      __is_base_of ( type-id , type-id )
3855      __is_class ( type-id )
3856      __is_convertible_to ( type-id , type-id )     
3857      __is_empty ( type-id )
3858      __is_enum ( type-id )
3859      __is_final ( type-id )
3860      __is_literal_type ( type-id )
3861      __is_pod ( type-id )
3862      __is_polymorphic ( type-id )
3863      __is_std_layout ( type-id )
3864      __is_trivial ( type-id )
3865      __is_union ( type-id )
3866
3867    Objective-C++ Extension:
3868
3869    primary-expression:
3870      objc-expression
3871
3872    literal:
3873      __null
3874
3875    ADDRESS_P is true iff this expression was immediately preceded by
3876    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3877    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3878    true iff this expression is a template argument.
3879
3880    Returns a representation of the expression.  Upon return, *IDK
3881    indicates what kind of id-expression (if any) was present.  */
3882
3883 static tree
3884 cp_parser_primary_expression (cp_parser *parser,
3885                               bool address_p,
3886                               bool cast_p,
3887                               bool template_arg_p,
3888                               cp_id_kind *idk)
3889 {
3890   cp_token *token = NULL;
3891
3892   /* Assume the primary expression is not an id-expression.  */
3893   *idk = CP_ID_KIND_NONE;
3894
3895   /* Peek at the next token.  */
3896   token = cp_lexer_peek_token (parser->lexer);
3897   switch (token->type)
3898     {
3899       /* literal:
3900            integer-literal
3901            character-literal
3902            floating-literal
3903            string-literal
3904            boolean-literal
3905            pointer-literal
3906            user-defined-literal  */
3907     case CPP_CHAR:
3908     case CPP_CHAR16:
3909     case CPP_CHAR32:
3910     case CPP_WCHAR:
3911     case CPP_NUMBER:
3912       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3913         return cp_parser_userdef_numeric_literal (parser);
3914       token = cp_lexer_consume_token (parser->lexer);
3915       if (TREE_CODE (token->u.value) == FIXED_CST)
3916         {
3917           error_at (token->location,
3918                     "fixed-point types not supported in C++");
3919           return error_mark_node;
3920         }
3921       /* Floating-point literals are only allowed in an integral
3922          constant expression if they are cast to an integral or
3923          enumeration type.  */
3924       if (TREE_CODE (token->u.value) == REAL_CST
3925           && parser->integral_constant_expression_p
3926           && pedantic)
3927         {
3928           /* CAST_P will be set even in invalid code like "int(2.7 +
3929              ...)".   Therefore, we have to check that the next token
3930              is sure to end the cast.  */
3931           if (cast_p)
3932             {
3933               cp_token *next_token;
3934
3935               next_token = cp_lexer_peek_token (parser->lexer);
3936               if (/* The comma at the end of an
3937                      enumerator-definition.  */
3938                   next_token->type != CPP_COMMA
3939                   /* The curly brace at the end of an enum-specifier.  */
3940                   && next_token->type != CPP_CLOSE_BRACE
3941                   /* The end of a statement.  */
3942                   && next_token->type != CPP_SEMICOLON
3943                   /* The end of the cast-expression.  */
3944                   && next_token->type != CPP_CLOSE_PAREN
3945                   /* The end of an array bound.  */
3946                   && next_token->type != CPP_CLOSE_SQUARE
3947                   /* The closing ">" in a template-argument-list.  */
3948                   && (next_token->type != CPP_GREATER
3949                       || parser->greater_than_is_operator_p)
3950                   /* C++0x only: A ">>" treated like two ">" tokens,
3951                      in a template-argument-list.  */
3952                   && (next_token->type != CPP_RSHIFT
3953                       || (cxx_dialect == cxx98)
3954                       || parser->greater_than_is_operator_p))
3955                 cast_p = false;
3956             }
3957
3958           /* If we are within a cast, then the constraint that the
3959              cast is to an integral or enumeration type will be
3960              checked at that point.  If we are not within a cast, then
3961              this code is invalid.  */
3962           if (!cast_p)
3963             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3964         }
3965       return token->u.value;
3966
3967     case CPP_CHAR_USERDEF:
3968     case CPP_CHAR16_USERDEF:
3969     case CPP_CHAR32_USERDEF:
3970     case CPP_WCHAR_USERDEF:
3971       return cp_parser_userdef_char_literal (parser);
3972
3973     case CPP_STRING:
3974     case CPP_STRING16:
3975     case CPP_STRING32:
3976     case CPP_WSTRING:
3977     case CPP_UTF8STRING:
3978     case CPP_STRING_USERDEF:
3979     case CPP_STRING16_USERDEF:
3980     case CPP_STRING32_USERDEF:
3981     case CPP_WSTRING_USERDEF:
3982     case CPP_UTF8STRING_USERDEF:
3983       /* ??? Should wide strings be allowed when parser->translate_strings_p
3984          is false (i.e. in attributes)?  If not, we can kill the third
3985          argument to cp_parser_string_literal.  */
3986       return cp_parser_string_literal (parser,
3987                                        parser->translate_strings_p,
3988                                        true);
3989
3990     case CPP_OPEN_PAREN:
3991       {
3992         tree expr;
3993         bool saved_greater_than_is_operator_p;
3994
3995         /* Consume the `('.  */
3996         cp_lexer_consume_token (parser->lexer);
3997         /* Within a parenthesized expression, a `>' token is always
3998            the greater-than operator.  */
3999         saved_greater_than_is_operator_p
4000           = parser->greater_than_is_operator_p;
4001         parser->greater_than_is_operator_p = true;
4002         /* If we see `( { ' then we are looking at the beginning of
4003            a GNU statement-expression.  */
4004         if (cp_parser_allow_gnu_extensions_p (parser)
4005             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4006           {
4007             /* Statement-expressions are not allowed by the standard.  */
4008             pedwarn (token->location, OPT_pedantic, 
4009                      "ISO C++ forbids braced-groups within expressions");
4010
4011             /* And they're not allowed outside of a function-body; you
4012                cannot, for example, write:
4013
4014                  int i = ({ int j = 3; j + 1; });
4015
4016                at class or namespace scope.  */
4017             if (!parser->in_function_body
4018                 || parser->in_template_argument_list_p)
4019               {
4020                 error_at (token->location,
4021                           "statement-expressions are not allowed outside "
4022                           "functions nor in template-argument lists");
4023                 cp_parser_skip_to_end_of_block_or_statement (parser);
4024                 expr = error_mark_node;
4025               }
4026             else
4027               {
4028                 /* Start the statement-expression.  */
4029                 expr = begin_stmt_expr ();
4030                 /* Parse the compound-statement.  */
4031                 cp_parser_compound_statement (parser, expr, false, false);
4032                 /* Finish up.  */
4033                 expr = finish_stmt_expr (expr, false);
4034               }
4035           }
4036         else
4037           {
4038             /* Parse the parenthesized expression.  */
4039             expr = cp_parser_expression (parser, cast_p, idk);
4040             /* Let the front end know that this expression was
4041                enclosed in parentheses. This matters in case, for
4042                example, the expression is of the form `A::B', since
4043                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4044                not.  */
4045             finish_parenthesized_expr (expr);
4046             /* DR 705: Wrapping an unqualified name in parentheses
4047                suppresses arg-dependent lookup.  We want to pass back
4048                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4049                (c++/37862), but none of the others.  */
4050             if (*idk != CP_ID_KIND_QUALIFIED)
4051               *idk = CP_ID_KIND_NONE;
4052           }
4053         /* The `>' token might be the end of a template-id or
4054            template-parameter-list now.  */
4055         parser->greater_than_is_operator_p
4056           = saved_greater_than_is_operator_p;
4057         /* Consume the `)'.  */
4058         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4059           cp_parser_skip_to_end_of_statement (parser);
4060
4061         return expr;
4062       }
4063
4064     case CPP_OPEN_SQUARE:
4065       if (c_dialect_objc ())
4066         /* We have an Objective-C++ message. */
4067         return cp_parser_objc_expression (parser);
4068       {
4069         tree lam = cp_parser_lambda_expression (parser);
4070         /* Don't warn about a failed tentative parse.  */
4071         if (cp_parser_error_occurred (parser))
4072           return error_mark_node;
4073         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4074         return lam;
4075       }
4076
4077     case CPP_OBJC_STRING:
4078       if (c_dialect_objc ())
4079         /* We have an Objective-C++ string literal. */
4080         return cp_parser_objc_expression (parser);
4081       cp_parser_error (parser, "expected primary-expression");
4082       return error_mark_node;
4083
4084     case CPP_KEYWORD:
4085       switch (token->keyword)
4086         {
4087           /* These two are the boolean literals.  */
4088         case RID_TRUE:
4089           cp_lexer_consume_token (parser->lexer);
4090           return boolean_true_node;
4091         case RID_FALSE:
4092           cp_lexer_consume_token (parser->lexer);
4093           return boolean_false_node;
4094
4095           /* The `__null' literal.  */
4096         case RID_NULL:
4097           cp_lexer_consume_token (parser->lexer);
4098           return null_node;
4099
4100           /* The `nullptr' literal.  */
4101         case RID_NULLPTR:
4102           cp_lexer_consume_token (parser->lexer);
4103           return nullptr_node;
4104
4105           /* Recognize the `this' keyword.  */
4106         case RID_THIS:
4107           cp_lexer_consume_token (parser->lexer);
4108           if (parser->local_variables_forbidden_p)
4109             {
4110               error_at (token->location,
4111                         "%<this%> may not be used in this context");
4112               return error_mark_node;
4113             }
4114           /* Pointers cannot appear in constant-expressions.  */
4115           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4116             return error_mark_node;
4117           return finish_this_expr ();
4118
4119           /* The `operator' keyword can be the beginning of an
4120              id-expression.  */
4121         case RID_OPERATOR:
4122           goto id_expression;
4123
4124         case RID_FUNCTION_NAME:
4125         case RID_PRETTY_FUNCTION_NAME:
4126         case RID_C99_FUNCTION_NAME:
4127           {
4128             non_integral_constant name;
4129
4130             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4131                __func__ are the names of variables -- but they are
4132                treated specially.  Therefore, they are handled here,
4133                rather than relying on the generic id-expression logic
4134                below.  Grammatically, these names are id-expressions.
4135
4136                Consume the token.  */
4137             token = cp_lexer_consume_token (parser->lexer);
4138
4139             switch (token->keyword)
4140               {
4141               case RID_FUNCTION_NAME:
4142                 name = NIC_FUNC_NAME;
4143                 break;
4144               case RID_PRETTY_FUNCTION_NAME:
4145                 name = NIC_PRETTY_FUNC;
4146                 break;
4147               case RID_C99_FUNCTION_NAME:
4148                 name = NIC_C99_FUNC;
4149                 break;
4150               default:
4151                 gcc_unreachable ();
4152               }
4153
4154             if (cp_parser_non_integral_constant_expression (parser, name))
4155               return error_mark_node;
4156
4157             /* Look up the name.  */
4158             return finish_fname (token->u.value);
4159           }
4160
4161         case RID_VA_ARG:
4162           {
4163             tree expression;
4164             tree type;
4165
4166             /* The `__builtin_va_arg' construct is used to handle
4167                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4168             cp_lexer_consume_token (parser->lexer);
4169             /* Look for the opening `('.  */
4170             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4171             /* Now, parse the assignment-expression.  */
4172             expression = cp_parser_assignment_expression (parser,
4173                                                           /*cast_p=*/false, NULL);
4174             /* Look for the `,'.  */
4175             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4176             /* Parse the type-id.  */
4177             type = cp_parser_type_id (parser);
4178             /* Look for the closing `)'.  */
4179             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4180             /* Using `va_arg' in a constant-expression is not
4181                allowed.  */
4182             if (cp_parser_non_integral_constant_expression (parser,
4183                                                             NIC_VA_ARG))
4184               return error_mark_node;
4185             return build_x_va_arg (expression, type);
4186           }
4187
4188         case RID_OFFSETOF:
4189           return cp_parser_builtin_offsetof (parser);
4190
4191         case RID_HAS_NOTHROW_ASSIGN:
4192         case RID_HAS_NOTHROW_CONSTRUCTOR:
4193         case RID_HAS_NOTHROW_COPY:        
4194         case RID_HAS_TRIVIAL_ASSIGN:
4195         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4196         case RID_HAS_TRIVIAL_COPY:        
4197         case RID_HAS_TRIVIAL_DESTRUCTOR:
4198         case RID_HAS_VIRTUAL_DESTRUCTOR:
4199         case RID_IS_ABSTRACT:
4200         case RID_IS_BASE_OF:
4201         case RID_IS_CLASS:
4202         case RID_IS_CONVERTIBLE_TO:
4203         case RID_IS_EMPTY:
4204         case RID_IS_ENUM:
4205         case RID_IS_FINAL:
4206         case RID_IS_LITERAL_TYPE:
4207         case RID_IS_POD:
4208         case RID_IS_POLYMORPHIC:
4209         case RID_IS_STD_LAYOUT:
4210         case RID_IS_TRIVIAL:
4211         case RID_IS_UNION:
4212           return cp_parser_trait_expr (parser, token->keyword);
4213
4214         /* Objective-C++ expressions.  */
4215         case RID_AT_ENCODE:
4216         case RID_AT_PROTOCOL:
4217         case RID_AT_SELECTOR:
4218           return cp_parser_objc_expression (parser);
4219
4220         case RID_TEMPLATE:
4221           if (parser->in_function_body
4222               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4223                   == CPP_LESS))
4224             {
4225               error_at (token->location,
4226                         "a template declaration cannot appear at block scope");
4227               cp_parser_skip_to_end_of_block_or_statement (parser);
4228               return error_mark_node;
4229             }
4230         default:
4231           cp_parser_error (parser, "expected primary-expression");
4232           return error_mark_node;
4233         }
4234
4235       /* An id-expression can start with either an identifier, a
4236          `::' as the beginning of a qualified-id, or the "operator"
4237          keyword.  */
4238     case CPP_NAME:
4239     case CPP_SCOPE:
4240     case CPP_TEMPLATE_ID:
4241     case CPP_NESTED_NAME_SPECIFIER:
4242       {
4243         tree id_expression;
4244         tree decl;
4245         const char *error_msg;
4246         bool template_p;
4247         bool done;
4248         cp_token *id_expr_token;
4249
4250       id_expression:
4251         /* Parse the id-expression.  */
4252         id_expression
4253           = cp_parser_id_expression (parser,
4254                                      /*template_keyword_p=*/false,
4255                                      /*check_dependency_p=*/true,
4256                                      &template_p,
4257                                      /*declarator_p=*/false,
4258                                      /*optional_p=*/false);
4259         if (id_expression == error_mark_node)
4260           return error_mark_node;
4261         id_expr_token = token;
4262         token = cp_lexer_peek_token (parser->lexer);
4263         done = (token->type != CPP_OPEN_SQUARE
4264                 && token->type != CPP_OPEN_PAREN
4265                 && token->type != CPP_DOT
4266                 && token->type != CPP_DEREF
4267                 && token->type != CPP_PLUS_PLUS
4268                 && token->type != CPP_MINUS_MINUS);
4269         /* If we have a template-id, then no further lookup is
4270            required.  If the template-id was for a template-class, we
4271            will sometimes have a TYPE_DECL at this point.  */
4272         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4273                  || TREE_CODE (id_expression) == TYPE_DECL)
4274           decl = id_expression;
4275         /* Look up the name.  */
4276         else
4277           {
4278             tree ambiguous_decls;
4279
4280             /* If we already know that this lookup is ambiguous, then
4281                we've already issued an error message; there's no reason
4282                to check again.  */
4283             if (id_expr_token->type == CPP_NAME
4284                 && id_expr_token->ambiguous_p)
4285               {
4286                 cp_parser_simulate_error (parser);
4287                 return error_mark_node;
4288               }
4289
4290             decl = cp_parser_lookup_name (parser, id_expression,
4291                                           none_type,
4292                                           template_p,
4293                                           /*is_namespace=*/false,
4294                                           /*check_dependency=*/true,
4295                                           &ambiguous_decls,
4296                                           id_expr_token->location);
4297             /* If the lookup was ambiguous, an error will already have
4298                been issued.  */
4299             if (ambiguous_decls)
4300               return error_mark_node;
4301
4302             /* In Objective-C++, we may have an Objective-C 2.0
4303                dot-syntax for classes here.  */
4304             if (c_dialect_objc ()
4305                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4306                 && TREE_CODE (decl) == TYPE_DECL
4307                 && objc_is_class_name (decl))
4308               {
4309                 tree component;
4310                 cp_lexer_consume_token (parser->lexer);
4311                 component = cp_parser_identifier (parser);
4312                 if (component == error_mark_node)
4313                   return error_mark_node;
4314
4315                 return objc_build_class_component_ref (id_expression, component);
4316               }
4317
4318             /* In Objective-C++, an instance variable (ivar) may be preferred
4319                to whatever cp_parser_lookup_name() found.  */
4320             decl = objc_lookup_ivar (decl, id_expression);
4321
4322             /* If name lookup gives us a SCOPE_REF, then the
4323                qualifying scope was dependent.  */
4324             if (TREE_CODE (decl) == SCOPE_REF)
4325               {
4326                 /* At this point, we do not know if DECL is a valid
4327                    integral constant expression.  We assume that it is
4328                    in fact such an expression, so that code like:
4329
4330                       template <int N> struct A {
4331                         int a[B<N>::i];
4332                       };
4333                      
4334                    is accepted.  At template-instantiation time, we
4335                    will check that B<N>::i is actually a constant.  */
4336                 return decl;
4337               }
4338             /* Check to see if DECL is a local variable in a context
4339                where that is forbidden.  */
4340             if (parser->local_variables_forbidden_p
4341                 && local_variable_p (decl))
4342               {
4343                 /* It might be that we only found DECL because we are
4344                    trying to be generous with pre-ISO scoping rules.
4345                    For example, consider:
4346
4347                      int i;
4348                      void g() {
4349                        for (int i = 0; i < 10; ++i) {}
4350                        extern void f(int j = i);
4351                      }
4352
4353                    Here, name look up will originally find the out
4354                    of scope `i'.  We need to issue a warning message,
4355                    but then use the global `i'.  */
4356                 decl = check_for_out_of_scope_variable (decl);
4357                 if (local_variable_p (decl))
4358                   {
4359                     error_at (id_expr_token->location,
4360                               "local variable %qD may not appear in this context",
4361                               decl);
4362                     return error_mark_node;
4363                   }
4364               }
4365           }
4366
4367         decl = (finish_id_expression
4368                 (id_expression, decl, parser->scope,
4369                  idk,
4370                  parser->integral_constant_expression_p,
4371                  parser->allow_non_integral_constant_expression_p,
4372                  &parser->non_integral_constant_expression_p,
4373                  template_p, done, address_p,
4374                  template_arg_p,
4375                  &error_msg,
4376                  id_expr_token->location));
4377         if (error_msg)
4378           cp_parser_error (parser, error_msg);
4379         return decl;
4380       }
4381
4382       /* Anything else is an error.  */
4383     default:
4384       cp_parser_error (parser, "expected primary-expression");
4385       return error_mark_node;
4386     }
4387 }
4388
4389 /* Parse an id-expression.
4390
4391    id-expression:
4392      unqualified-id
4393      qualified-id
4394
4395    qualified-id:
4396      :: [opt] nested-name-specifier template [opt] unqualified-id
4397      :: identifier
4398      :: operator-function-id
4399      :: template-id
4400
4401    Return a representation of the unqualified portion of the
4402    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4403    a `::' or nested-name-specifier.
4404
4405    Often, if the id-expression was a qualified-id, the caller will
4406    want to make a SCOPE_REF to represent the qualified-id.  This
4407    function does not do this in order to avoid wastefully creating
4408    SCOPE_REFs when they are not required.
4409
4410    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4411    `template' keyword.
4412
4413    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4414    uninstantiated templates.
4415
4416    If *TEMPLATE_P is non-NULL, it is set to true iff the
4417    `template' keyword is used to explicitly indicate that the entity
4418    named is a template.
4419
4420    If DECLARATOR_P is true, the id-expression is appearing as part of
4421    a declarator, rather than as part of an expression.  */
4422
4423 static tree
4424 cp_parser_id_expression (cp_parser *parser,
4425                          bool template_keyword_p,
4426                          bool check_dependency_p,
4427                          bool *template_p,
4428                          bool declarator_p,
4429                          bool optional_p)
4430 {
4431   bool global_scope_p;
4432   bool nested_name_specifier_p;
4433
4434   /* Assume the `template' keyword was not used.  */
4435   if (template_p)
4436     *template_p = template_keyword_p;
4437
4438   /* Look for the optional `::' operator.  */
4439   global_scope_p
4440     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4441        != NULL_TREE);
4442   /* Look for the optional nested-name-specifier.  */
4443   nested_name_specifier_p
4444     = (cp_parser_nested_name_specifier_opt (parser,
4445                                             /*typename_keyword_p=*/false,
4446                                             check_dependency_p,
4447                                             /*type_p=*/false,
4448                                             declarator_p)
4449        != NULL_TREE);
4450   /* If there is a nested-name-specifier, then we are looking at
4451      the first qualified-id production.  */
4452   if (nested_name_specifier_p)
4453     {
4454       tree saved_scope;
4455       tree saved_object_scope;
4456       tree saved_qualifying_scope;
4457       tree unqualified_id;
4458       bool is_template;
4459
4460       /* See if the next token is the `template' keyword.  */
4461       if (!template_p)
4462         template_p = &is_template;
4463       *template_p = cp_parser_optional_template_keyword (parser);
4464       /* Name lookup we do during the processing of the
4465          unqualified-id might obliterate SCOPE.  */
4466       saved_scope = parser->scope;
4467       saved_object_scope = parser->object_scope;
4468       saved_qualifying_scope = parser->qualifying_scope;
4469       /* Process the final unqualified-id.  */
4470       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4471                                                  check_dependency_p,
4472                                                  declarator_p,
4473                                                  /*optional_p=*/false);
4474       /* Restore the SAVED_SCOPE for our caller.  */
4475       parser->scope = saved_scope;
4476       parser->object_scope = saved_object_scope;
4477       parser->qualifying_scope = saved_qualifying_scope;
4478
4479       return unqualified_id;
4480     }
4481   /* Otherwise, if we are in global scope, then we are looking at one
4482      of the other qualified-id productions.  */
4483   else if (global_scope_p)
4484     {
4485       cp_token *token;
4486       tree id;
4487
4488       /* Peek at the next token.  */
4489       token = cp_lexer_peek_token (parser->lexer);
4490
4491       /* If it's an identifier, and the next token is not a "<", then
4492          we can avoid the template-id case.  This is an optimization
4493          for this common case.  */
4494       if (token->type == CPP_NAME
4495           && !cp_parser_nth_token_starts_template_argument_list_p
4496                (parser, 2))
4497         return cp_parser_identifier (parser);
4498
4499       cp_parser_parse_tentatively (parser);
4500       /* Try a template-id.  */
4501       id = cp_parser_template_id (parser,
4502                                   /*template_keyword_p=*/false,
4503                                   /*check_dependency_p=*/true,
4504                                   declarator_p);
4505       /* If that worked, we're done.  */
4506       if (cp_parser_parse_definitely (parser))
4507         return id;
4508
4509       /* Peek at the next token.  (Changes in the token buffer may
4510          have invalidated the pointer obtained above.)  */
4511       token = cp_lexer_peek_token (parser->lexer);
4512
4513       switch (token->type)
4514         {
4515         case CPP_NAME:
4516           return cp_parser_identifier (parser);
4517
4518         case CPP_KEYWORD:
4519           if (token->keyword == RID_OPERATOR)
4520             return cp_parser_operator_function_id (parser);
4521           /* Fall through.  */
4522
4523         default:
4524           cp_parser_error (parser, "expected id-expression");
4525           return error_mark_node;
4526         }
4527     }
4528   else
4529     return cp_parser_unqualified_id (parser, template_keyword_p,
4530                                      /*check_dependency_p=*/true,
4531                                      declarator_p,
4532                                      optional_p);
4533 }
4534
4535 /* Parse an unqualified-id.
4536
4537    unqualified-id:
4538      identifier
4539      operator-function-id
4540      conversion-function-id
4541      ~ class-name
4542      template-id
4543
4544    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4545    keyword, in a construct like `A::template ...'.
4546
4547    Returns a representation of unqualified-id.  For the `identifier'
4548    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4549    production a BIT_NOT_EXPR is returned; the operand of the
4550    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4551    other productions, see the documentation accompanying the
4552    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4553    names are looked up in uninstantiated templates.  If DECLARATOR_P
4554    is true, the unqualified-id is appearing as part of a declarator,
4555    rather than as part of an expression.  */
4556
4557 static tree
4558 cp_parser_unqualified_id (cp_parser* parser,
4559                           bool template_keyword_p,
4560                           bool check_dependency_p,
4561                           bool declarator_p,
4562                           bool optional_p)
4563 {
4564   cp_token *token;
4565
4566   /* Peek at the next token.  */
4567   token = cp_lexer_peek_token (parser->lexer);
4568
4569   switch (token->type)
4570     {
4571     case CPP_NAME:
4572       {
4573         tree id;
4574
4575         /* We don't know yet whether or not this will be a
4576            template-id.  */
4577         cp_parser_parse_tentatively (parser);
4578         /* Try a template-id.  */
4579         id = cp_parser_template_id (parser, template_keyword_p,
4580                                     check_dependency_p,
4581                                     declarator_p);
4582         /* If it worked, we're done.  */
4583         if (cp_parser_parse_definitely (parser))
4584           return id;
4585         /* Otherwise, it's an ordinary identifier.  */
4586         return cp_parser_identifier (parser);
4587       }
4588
4589     case CPP_TEMPLATE_ID:
4590       return cp_parser_template_id (parser, template_keyword_p,
4591                                     check_dependency_p,
4592                                     declarator_p);
4593
4594     case CPP_COMPL:
4595       {
4596         tree type_decl;
4597         tree qualifying_scope;
4598         tree object_scope;
4599         tree scope;
4600         bool done;
4601
4602         /* Consume the `~' token.  */
4603         cp_lexer_consume_token (parser->lexer);
4604         /* Parse the class-name.  The standard, as written, seems to
4605            say that:
4606
4607              template <typename T> struct S { ~S (); };
4608              template <typename T> S<T>::~S() {}
4609
4610            is invalid, since `~' must be followed by a class-name, but
4611            `S<T>' is dependent, and so not known to be a class.
4612            That's not right; we need to look in uninstantiated
4613            templates.  A further complication arises from:
4614
4615              template <typename T> void f(T t) {
4616                t.T::~T();
4617              }
4618
4619            Here, it is not possible to look up `T' in the scope of `T'
4620            itself.  We must look in both the current scope, and the
4621            scope of the containing complete expression.
4622
4623            Yet another issue is:
4624
4625              struct S {
4626                int S;
4627                ~S();
4628              };
4629
4630              S::~S() {}
4631
4632            The standard does not seem to say that the `S' in `~S'
4633            should refer to the type `S' and not the data member
4634            `S::S'.  */
4635
4636         /* DR 244 says that we look up the name after the "~" in the
4637            same scope as we looked up the qualifying name.  That idea
4638            isn't fully worked out; it's more complicated than that.  */
4639         scope = parser->scope;
4640         object_scope = parser->object_scope;
4641         qualifying_scope = parser->qualifying_scope;
4642
4643         /* Check for invalid scopes.  */
4644         if (scope == error_mark_node)
4645           {
4646             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4647               cp_lexer_consume_token (parser->lexer);
4648             return error_mark_node;
4649           }
4650         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4651           {
4652             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4653               error_at (token->location,
4654                         "scope %qT before %<~%> is not a class-name",
4655                         scope);
4656             cp_parser_simulate_error (parser);
4657             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4658               cp_lexer_consume_token (parser->lexer);
4659             return error_mark_node;
4660           }
4661         gcc_assert (!scope || TYPE_P (scope));
4662
4663         /* If the name is of the form "X::~X" it's OK even if X is a
4664            typedef.  */
4665         token = cp_lexer_peek_token (parser->lexer);
4666         if (scope
4667             && token->type == CPP_NAME
4668             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4669                 != CPP_LESS)
4670             && (token->u.value == TYPE_IDENTIFIER (scope)
4671                 || (CLASS_TYPE_P (scope)
4672                     && constructor_name_p (token->u.value, scope))))
4673           {
4674             cp_lexer_consume_token (parser->lexer);
4675             return build_nt (BIT_NOT_EXPR, scope);
4676           }
4677
4678         /* If there was an explicit qualification (S::~T), first look
4679            in the scope given by the qualification (i.e., S).
4680
4681            Note: in the calls to cp_parser_class_name below we pass
4682            typename_type so that lookup finds the injected-class-name
4683            rather than the constructor.  */
4684         done = false;
4685         type_decl = NULL_TREE;
4686         if (scope)
4687           {
4688             cp_parser_parse_tentatively (parser);
4689             type_decl = cp_parser_class_name (parser,
4690                                               /*typename_keyword_p=*/false,
4691                                               /*template_keyword_p=*/false,
4692                                               typename_type,
4693                                               /*check_dependency=*/false,
4694                                               /*class_head_p=*/false,
4695                                               declarator_p);
4696             if (cp_parser_parse_definitely (parser))
4697               done = true;
4698           }
4699         /* In "N::S::~S", look in "N" as well.  */
4700         if (!done && scope && qualifying_scope)
4701           {
4702             cp_parser_parse_tentatively (parser);
4703             parser->scope = qualifying_scope;
4704             parser->object_scope = NULL_TREE;
4705             parser->qualifying_scope = NULL_TREE;
4706             type_decl
4707               = cp_parser_class_name (parser,
4708                                       /*typename_keyword_p=*/false,
4709                                       /*template_keyword_p=*/false,
4710                                       typename_type,
4711                                       /*check_dependency=*/false,
4712                                       /*class_head_p=*/false,
4713                                       declarator_p);
4714             if (cp_parser_parse_definitely (parser))
4715               done = true;
4716           }
4717         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4718         else if (!done && object_scope)
4719           {
4720             cp_parser_parse_tentatively (parser);
4721             parser->scope = object_scope;
4722             parser->object_scope = NULL_TREE;
4723             parser->qualifying_scope = NULL_TREE;
4724             type_decl
4725               = cp_parser_class_name (parser,
4726                                       /*typename_keyword_p=*/false,
4727                                       /*template_keyword_p=*/false,
4728                                       typename_type,
4729                                       /*check_dependency=*/false,
4730                                       /*class_head_p=*/false,
4731                                       declarator_p);
4732             if (cp_parser_parse_definitely (parser))
4733               done = true;
4734           }
4735         /* Look in the surrounding context.  */
4736         if (!done)
4737           {
4738             parser->scope = NULL_TREE;
4739             parser->object_scope = NULL_TREE;
4740             parser->qualifying_scope = NULL_TREE;
4741             if (processing_template_decl)
4742               cp_parser_parse_tentatively (parser);
4743             type_decl
4744               = cp_parser_class_name (parser,
4745                                       /*typename_keyword_p=*/false,
4746                                       /*template_keyword_p=*/false,
4747                                       typename_type,
4748                                       /*check_dependency=*/false,
4749                                       /*class_head_p=*/false,
4750                                       declarator_p);
4751             if (processing_template_decl
4752                 && ! cp_parser_parse_definitely (parser))
4753               {
4754                 /* We couldn't find a type with this name, so just accept
4755                    it and check for a match at instantiation time.  */
4756                 type_decl = cp_parser_identifier (parser);
4757                 if (type_decl != error_mark_node)
4758                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4759                 return type_decl;
4760               }
4761           }
4762         /* If an error occurred, assume that the name of the
4763            destructor is the same as the name of the qualifying
4764            class.  That allows us to keep parsing after running
4765            into ill-formed destructor names.  */
4766         if (type_decl == error_mark_node && scope)
4767           return build_nt (BIT_NOT_EXPR, scope);
4768         else if (type_decl == error_mark_node)
4769           return error_mark_node;
4770
4771         /* Check that destructor name and scope match.  */
4772         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4773           {
4774             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4775               error_at (token->location,
4776                         "declaration of %<~%T%> as member of %qT",
4777                         type_decl, scope);
4778             cp_parser_simulate_error (parser);
4779             return error_mark_node;
4780           }
4781
4782         /* [class.dtor]
4783
4784            A typedef-name that names a class shall not be used as the
4785            identifier in the declarator for a destructor declaration.  */
4786         if (declarator_p
4787             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4788             && !DECL_SELF_REFERENCE_P (type_decl)
4789             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4790           error_at (token->location,
4791                     "typedef-name %qD used as destructor declarator",
4792                     type_decl);
4793
4794         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4795       }
4796
4797     case CPP_KEYWORD:
4798       if (token->keyword == RID_OPERATOR)
4799         {
4800           tree id;
4801
4802           /* This could be a template-id, so we try that first.  */
4803           cp_parser_parse_tentatively (parser);
4804           /* Try a template-id.  */
4805           id = cp_parser_template_id (parser, template_keyword_p,
4806                                       /*check_dependency_p=*/true,
4807                                       declarator_p);
4808           /* If that worked, we're done.  */
4809           if (cp_parser_parse_definitely (parser))
4810             return id;
4811           /* We still don't know whether we're looking at an
4812              operator-function-id or a conversion-function-id.  */
4813           cp_parser_parse_tentatively (parser);
4814           /* Try an operator-function-id.  */
4815           id = cp_parser_operator_function_id (parser);
4816           /* If that didn't work, try a conversion-function-id.  */
4817           if (!cp_parser_parse_definitely (parser))
4818             id = cp_parser_conversion_function_id (parser);
4819           else if (UDLIT_OPER_P (id))
4820             {
4821               /* 17.6.3.3.5  */
4822               const char *name = UDLIT_OP_SUFFIX (id);
4823               if (name[0] != '_' && !in_system_header)
4824                 warning (0, "literal operator suffixes not preceded by %<_%>"
4825                             " are reserved for future standardization");
4826             }
4827
4828           return id;
4829         }
4830       /* Fall through.  */
4831
4832     default:
4833       if (optional_p)
4834         return NULL_TREE;
4835       cp_parser_error (parser, "expected unqualified-id");
4836       return error_mark_node;
4837     }
4838 }
4839
4840 /* Parse an (optional) nested-name-specifier.
4841
4842    nested-name-specifier: [C++98]
4843      class-or-namespace-name :: nested-name-specifier [opt]
4844      class-or-namespace-name :: template nested-name-specifier [opt]
4845
4846    nested-name-specifier: [C++0x]
4847      type-name ::
4848      namespace-name ::
4849      nested-name-specifier identifier ::
4850      nested-name-specifier template [opt] simple-template-id ::
4851
4852    PARSER->SCOPE should be set appropriately before this function is
4853    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4854    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4855    in name lookups.
4856
4857    Sets PARSER->SCOPE to the class (TYPE) or namespace
4858    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4859    it unchanged if there is no nested-name-specifier.  Returns the new
4860    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4861
4862    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4863    part of a declaration and/or decl-specifier.  */
4864
4865 static tree
4866 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4867                                      bool typename_keyword_p,
4868                                      bool check_dependency_p,
4869                                      bool type_p,
4870                                      bool is_declaration)
4871 {
4872   bool success = false;
4873   cp_token_position start = 0;
4874   cp_token *token;
4875
4876   /* Remember where the nested-name-specifier starts.  */
4877   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4878     {
4879       start = cp_lexer_token_position (parser->lexer, false);
4880       push_deferring_access_checks (dk_deferred);
4881     }
4882
4883   while (true)
4884     {
4885       tree new_scope;
4886       tree old_scope;
4887       tree saved_qualifying_scope;
4888       bool template_keyword_p;
4889
4890       /* Spot cases that cannot be the beginning of a
4891          nested-name-specifier.  */
4892       token = cp_lexer_peek_token (parser->lexer);
4893
4894       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4895          the already parsed nested-name-specifier.  */
4896       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4897         {
4898           /* Grab the nested-name-specifier and continue the loop.  */
4899           cp_parser_pre_parsed_nested_name_specifier (parser);
4900           /* If we originally encountered this nested-name-specifier
4901              with IS_DECLARATION set to false, we will not have
4902              resolved TYPENAME_TYPEs, so we must do so here.  */
4903           if (is_declaration
4904               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4905             {
4906               new_scope = resolve_typename_type (parser->scope,
4907                                                  /*only_current_p=*/false);
4908               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4909                 parser->scope = new_scope;
4910             }
4911           success = true;
4912           continue;
4913         }
4914
4915       /* Spot cases that cannot be the beginning of a
4916          nested-name-specifier.  On the second and subsequent times
4917          through the loop, we look for the `template' keyword.  */
4918       if (success && token->keyword == RID_TEMPLATE)
4919         ;
4920       /* A template-id can start a nested-name-specifier.  */
4921       else if (token->type == CPP_TEMPLATE_ID)
4922         ;
4923       /* DR 743: decltype can be used in a nested-name-specifier.  */
4924       else if (token_is_decltype (token))
4925         ;
4926       else
4927         {
4928           /* If the next token is not an identifier, then it is
4929              definitely not a type-name or namespace-name.  */
4930           if (token->type != CPP_NAME)
4931             break;
4932           /* If the following token is neither a `<' (to begin a
4933              template-id), nor a `::', then we are not looking at a
4934              nested-name-specifier.  */
4935           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4936
4937           if (token->type == CPP_COLON
4938               && parser->colon_corrects_to_scope_p
4939               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4940             {
4941               error_at (token->location,
4942                         "found %<:%> in nested-name-specifier, expected %<::%>");
4943               token->type = CPP_SCOPE;
4944             }
4945
4946           if (token->type != CPP_SCOPE
4947               && !cp_parser_nth_token_starts_template_argument_list_p
4948                   (parser, 2))
4949             break;
4950         }
4951
4952       /* The nested-name-specifier is optional, so we parse
4953          tentatively.  */
4954       cp_parser_parse_tentatively (parser);
4955
4956       /* Look for the optional `template' keyword, if this isn't the
4957          first time through the loop.  */
4958       if (success)
4959         template_keyword_p = cp_parser_optional_template_keyword (parser);
4960       else
4961         template_keyword_p = false;
4962
4963       /* Save the old scope since the name lookup we are about to do
4964          might destroy it.  */
4965       old_scope = parser->scope;
4966       saved_qualifying_scope = parser->qualifying_scope;
4967       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4968          look up names in "X<T>::I" in order to determine that "Y" is
4969          a template.  So, if we have a typename at this point, we make
4970          an effort to look through it.  */
4971       if (is_declaration
4972           && !typename_keyword_p
4973           && parser->scope
4974           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4975         parser->scope = resolve_typename_type (parser->scope,
4976                                                /*only_current_p=*/false);
4977       /* Parse the qualifying entity.  */
4978       new_scope
4979         = cp_parser_qualifying_entity (parser,
4980                                        typename_keyword_p,
4981                                        template_keyword_p,
4982                                        check_dependency_p,
4983                                        type_p,
4984                                        is_declaration);
4985       /* Look for the `::' token.  */
4986       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4987
4988       /* If we found what we wanted, we keep going; otherwise, we're
4989          done.  */
4990       if (!cp_parser_parse_definitely (parser))
4991         {
4992           bool error_p = false;
4993
4994           /* Restore the OLD_SCOPE since it was valid before the
4995              failed attempt at finding the last
4996              class-or-namespace-name.  */
4997           parser->scope = old_scope;
4998           parser->qualifying_scope = saved_qualifying_scope;
4999
5000           /* If the next token is a decltype, and the one after that is a
5001              `::', then the decltype has failed to resolve to a class or
5002              enumeration type.  Give this error even when parsing
5003              tentatively since it can't possibly be valid--and we're going
5004              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5005              won't get another chance.*/
5006           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5007               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5008                   == CPP_SCOPE))
5009             {
5010               token = cp_lexer_consume_token (parser->lexer);
5011               error_at (token->location, "decltype evaluates to %qT, "
5012                         "which is not a class or enumeration type",
5013                         token->u.value);
5014               parser->scope = error_mark_node;
5015               error_p = true;
5016               /* As below.  */
5017               success = true;
5018               cp_lexer_consume_token (parser->lexer);
5019             }
5020
5021           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5022             break;
5023           /* If the next token is an identifier, and the one after
5024              that is a `::', then any valid interpretation would have
5025              found a class-or-namespace-name.  */
5026           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5027                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5028                      == CPP_SCOPE)
5029                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5030                      != CPP_COMPL))
5031             {
5032               token = cp_lexer_consume_token (parser->lexer);
5033               if (!error_p)
5034                 {
5035                   if (!token->ambiguous_p)
5036                     {
5037                       tree decl;
5038                       tree ambiguous_decls;
5039
5040                       decl = cp_parser_lookup_name (parser, token->u.value,
5041                                                     none_type,
5042                                                     /*is_template=*/false,
5043                                                     /*is_namespace=*/false,
5044                                                     /*check_dependency=*/true,
5045                                                     &ambiguous_decls,
5046                                                     token->location);
5047                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5048                         error_at (token->location,
5049                                   "%qD used without template parameters",
5050                                   decl);
5051                       else if (ambiguous_decls)
5052                         {
5053                           error_at (token->location,
5054                                     "reference to %qD is ambiguous",
5055                                     token->u.value);
5056                           print_candidates (ambiguous_decls);
5057                           decl = error_mark_node;
5058                         }
5059                       else
5060                         {
5061                           if (cxx_dialect != cxx98)
5062                             cp_parser_name_lookup_error
5063                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5064                              token->location);
5065                           else
5066                             cp_parser_name_lookup_error
5067                             (parser, token->u.value, decl, NLE_CXX98,
5068                              token->location);
5069                         }
5070                     }
5071                   parser->scope = error_mark_node;
5072                   error_p = true;
5073                   /* Treat this as a successful nested-name-specifier
5074                      due to:
5075
5076                      [basic.lookup.qual]
5077
5078                      If the name found is not a class-name (clause
5079                      _class_) or namespace-name (_namespace.def_), the
5080                      program is ill-formed.  */
5081                   success = true;
5082                 }
5083               cp_lexer_consume_token (parser->lexer);
5084             }
5085           break;
5086         }
5087       /* We've found one valid nested-name-specifier.  */
5088       success = true;
5089       /* Name lookup always gives us a DECL.  */
5090       if (TREE_CODE (new_scope) == TYPE_DECL)
5091         new_scope = TREE_TYPE (new_scope);
5092       /* Uses of "template" must be followed by actual templates.  */
5093       if (template_keyword_p
5094           && !(CLASS_TYPE_P (new_scope)
5095                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5096                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5097                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5098           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5099                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5100                    == TEMPLATE_ID_EXPR)))
5101         permerror (input_location, TYPE_P (new_scope)
5102                    ? G_("%qT is not a template")
5103                    : G_("%qD is not a template"),
5104                    new_scope);
5105       /* If it is a class scope, try to complete it; we are about to
5106          be looking up names inside the class.  */
5107       if (TYPE_P (new_scope)
5108           /* Since checking types for dependency can be expensive,
5109              avoid doing it if the type is already complete.  */
5110           && !COMPLETE_TYPE_P (new_scope)
5111           /* Do not try to complete dependent types.  */
5112           && !dependent_type_p (new_scope))
5113         {
5114           new_scope = complete_type (new_scope);
5115           /* If it is a typedef to current class, use the current
5116              class instead, as the typedef won't have any names inside
5117              it yet.  */
5118           if (!COMPLETE_TYPE_P (new_scope)
5119               && currently_open_class (new_scope))
5120             new_scope = TYPE_MAIN_VARIANT (new_scope);
5121         }
5122       /* Make sure we look in the right scope the next time through
5123          the loop.  */
5124       parser->scope = new_scope;
5125     }
5126
5127   /* If parsing tentatively, replace the sequence of tokens that makes
5128      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5129      token.  That way, should we re-parse the token stream, we will
5130      not have to repeat the effort required to do the parse, nor will
5131      we issue duplicate error messages.  */
5132   if (success && start)
5133     {
5134       cp_token *token;
5135
5136       token = cp_lexer_token_at (parser->lexer, start);
5137       /* Reset the contents of the START token.  */
5138       token->type = CPP_NESTED_NAME_SPECIFIER;
5139       /* Retrieve any deferred checks.  Do not pop this access checks yet
5140          so the memory will not be reclaimed during token replacing below.  */
5141       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5142       token->u.tree_check_value->value = parser->scope;
5143       token->u.tree_check_value->checks = get_deferred_access_checks ();
5144       token->u.tree_check_value->qualifying_scope =
5145         parser->qualifying_scope;
5146       token->keyword = RID_MAX;
5147
5148       /* Purge all subsequent tokens.  */
5149       cp_lexer_purge_tokens_after (parser->lexer, start);
5150     }
5151
5152   if (start)
5153     pop_to_parent_deferring_access_checks ();
5154
5155   return success ? parser->scope : NULL_TREE;
5156 }
5157
5158 /* Parse a nested-name-specifier.  See
5159    cp_parser_nested_name_specifier_opt for details.  This function
5160    behaves identically, except that it will an issue an error if no
5161    nested-name-specifier is present.  */
5162
5163 static tree
5164 cp_parser_nested_name_specifier (cp_parser *parser,
5165                                  bool typename_keyword_p,
5166                                  bool check_dependency_p,
5167                                  bool type_p,
5168                                  bool is_declaration)
5169 {
5170   tree scope;
5171
5172   /* Look for the nested-name-specifier.  */
5173   scope = cp_parser_nested_name_specifier_opt (parser,
5174                                                typename_keyword_p,
5175                                                check_dependency_p,
5176                                                type_p,
5177                                                is_declaration);
5178   /* If it was not present, issue an error message.  */
5179   if (!scope)
5180     {
5181       cp_parser_error (parser, "expected nested-name-specifier");
5182       parser->scope = NULL_TREE;
5183     }
5184
5185   return scope;
5186 }
5187
5188 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5189    this is either a class-name or a namespace-name (which corresponds
5190    to the class-or-namespace-name production in the grammar). For
5191    C++0x, it can also be a type-name that refers to an enumeration
5192    type or a simple-template-id.
5193
5194    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5195    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5196    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5197    TYPE_P is TRUE iff the next name should be taken as a class-name,
5198    even the same name is declared to be another entity in the same
5199    scope.
5200
5201    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5202    specified by the class-or-namespace-name.  If neither is found the
5203    ERROR_MARK_NODE is returned.  */
5204
5205 static tree
5206 cp_parser_qualifying_entity (cp_parser *parser,
5207                              bool typename_keyword_p,
5208                              bool template_keyword_p,
5209                              bool check_dependency_p,
5210                              bool type_p,
5211                              bool is_declaration)
5212 {
5213   tree saved_scope;
5214   tree saved_qualifying_scope;
5215   tree saved_object_scope;
5216   tree scope;
5217   bool only_class_p;
5218   bool successful_parse_p;
5219
5220   /* DR 743: decltype can appear in a nested-name-specifier.  */
5221   if (cp_lexer_next_token_is_decltype (parser->lexer))
5222     {
5223       scope = cp_parser_decltype (parser);
5224       if (TREE_CODE (scope) != ENUMERAL_TYPE
5225           && !MAYBE_CLASS_TYPE_P (scope))
5226         {
5227           cp_parser_simulate_error (parser);
5228           return error_mark_node;
5229         }
5230       if (TYPE_NAME (scope))
5231         scope = TYPE_NAME (scope);
5232       return scope;
5233     }
5234
5235   /* Before we try to parse the class-name, we must save away the
5236      current PARSER->SCOPE since cp_parser_class_name will destroy
5237      it.  */
5238   saved_scope = parser->scope;
5239   saved_qualifying_scope = parser->qualifying_scope;
5240   saved_object_scope = parser->object_scope;
5241   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5242      there is no need to look for a namespace-name.  */
5243   only_class_p = template_keyword_p 
5244     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5245   if (!only_class_p)
5246     cp_parser_parse_tentatively (parser);
5247   scope = cp_parser_class_name (parser,
5248                                 typename_keyword_p,
5249                                 template_keyword_p,
5250                                 type_p ? class_type : none_type,
5251                                 check_dependency_p,
5252                                 /*class_head_p=*/false,
5253                                 is_declaration);
5254   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5255   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5256   if (!only_class_p 
5257       && cxx_dialect != cxx98
5258       && !successful_parse_p)
5259     {
5260       /* Restore the saved scope.  */
5261       parser->scope = saved_scope;
5262       parser->qualifying_scope = saved_qualifying_scope;
5263       parser->object_scope = saved_object_scope;
5264
5265       /* Parse tentatively.  */
5266       cp_parser_parse_tentatively (parser);
5267      
5268       /* Parse a type-name  */
5269       scope = cp_parser_type_name (parser);
5270
5271       /* "If the name found does not designate a namespace or a class,
5272          enumeration, or dependent type, the program is ill-formed."
5273
5274          We cover classes and dependent types above and namespaces below,
5275          so this code is only looking for enums.  */
5276       if (!scope || TREE_CODE (scope) != TYPE_DECL
5277           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5278         cp_parser_simulate_error (parser);
5279
5280       successful_parse_p = cp_parser_parse_definitely (parser);
5281     }
5282   /* If that didn't work, try for a namespace-name.  */
5283   if (!only_class_p && !successful_parse_p)
5284     {
5285       /* Restore the saved scope.  */
5286       parser->scope = saved_scope;
5287       parser->qualifying_scope = saved_qualifying_scope;
5288       parser->object_scope = saved_object_scope;
5289       /* If we are not looking at an identifier followed by the scope
5290          resolution operator, then this is not part of a
5291          nested-name-specifier.  (Note that this function is only used
5292          to parse the components of a nested-name-specifier.)  */
5293       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5294           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5295         return error_mark_node;
5296       scope = cp_parser_namespace_name (parser);
5297     }
5298
5299   return scope;
5300 }
5301
5302 /* Parse a postfix-expression.
5303
5304    postfix-expression:
5305      primary-expression
5306      postfix-expression [ expression ]
5307      postfix-expression ( expression-list [opt] )
5308      simple-type-specifier ( expression-list [opt] )
5309      typename :: [opt] nested-name-specifier identifier
5310        ( expression-list [opt] )
5311      typename :: [opt] nested-name-specifier template [opt] template-id
5312        ( expression-list [opt] )
5313      postfix-expression . template [opt] id-expression
5314      postfix-expression -> template [opt] id-expression
5315      postfix-expression . pseudo-destructor-name
5316      postfix-expression -> pseudo-destructor-name
5317      postfix-expression ++
5318      postfix-expression --
5319      dynamic_cast < type-id > ( expression )
5320      static_cast < type-id > ( expression )
5321      reinterpret_cast < type-id > ( expression )
5322      const_cast < type-id > ( expression )
5323      typeid ( expression )
5324      typeid ( type-id )
5325
5326    GNU Extension:
5327
5328    postfix-expression:
5329      ( type-id ) { initializer-list , [opt] }
5330
5331    This extension is a GNU version of the C99 compound-literal
5332    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5333    but they are essentially the same concept.)
5334
5335    If ADDRESS_P is true, the postfix expression is the operand of the
5336    `&' operator.  CAST_P is true if this expression is the target of a
5337    cast.
5338
5339    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5340    class member access expressions [expr.ref].
5341
5342    Returns a representation of the expression.  */
5343
5344 static tree
5345 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5346                               bool member_access_only_p,
5347                               cp_id_kind * pidk_return)
5348 {
5349   cp_token *token;
5350   enum rid keyword;
5351   cp_id_kind idk = CP_ID_KIND_NONE;
5352   tree postfix_expression = NULL_TREE;
5353   bool is_member_access = false;
5354
5355   /* Peek at the next token.  */
5356   token = cp_lexer_peek_token (parser->lexer);
5357   /* Some of the productions are determined by keywords.  */
5358   keyword = token->keyword;
5359   switch (keyword)
5360     {
5361     case RID_DYNCAST:
5362     case RID_STATCAST:
5363     case RID_REINTCAST:
5364     case RID_CONSTCAST:
5365       {
5366         tree type;
5367         tree expression;
5368         const char *saved_message;
5369
5370         /* All of these can be handled in the same way from the point
5371            of view of parsing.  Begin by consuming the token
5372            identifying the cast.  */
5373         cp_lexer_consume_token (parser->lexer);
5374
5375         /* New types cannot be defined in the cast.  */
5376         saved_message = parser->type_definition_forbidden_message;
5377         parser->type_definition_forbidden_message
5378           = G_("types may not be defined in casts");
5379
5380         /* Look for the opening `<'.  */
5381         cp_parser_require (parser, CPP_LESS, RT_LESS);
5382         /* Parse the type to which we are casting.  */
5383         type = cp_parser_type_id (parser);
5384         /* Look for the closing `>'.  */
5385         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5386         /* Restore the old message.  */
5387         parser->type_definition_forbidden_message = saved_message;
5388
5389         /* And the expression which is being cast.  */
5390         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5391         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5392         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5393
5394         /* Only type conversions to integral or enumeration types
5395            can be used in constant-expressions.  */
5396         if (!cast_valid_in_integral_constant_expression_p (type)
5397             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5398           return error_mark_node;
5399
5400         switch (keyword)
5401           {
5402           case RID_DYNCAST:
5403             postfix_expression
5404               = build_dynamic_cast (type, expression, tf_warning_or_error);
5405             break;
5406           case RID_STATCAST:
5407             postfix_expression
5408               = build_static_cast (type, expression, tf_warning_or_error);
5409             break;
5410           case RID_REINTCAST:
5411             postfix_expression
5412               = build_reinterpret_cast (type, expression, 
5413                                         tf_warning_or_error);
5414             break;
5415           case RID_CONSTCAST:
5416             postfix_expression
5417               = build_const_cast (type, expression, tf_warning_or_error);
5418             break;
5419           default:
5420             gcc_unreachable ();
5421           }
5422       }
5423       break;
5424
5425     case RID_TYPEID:
5426       {
5427         tree type;
5428         const char *saved_message;
5429         bool saved_in_type_id_in_expr_p;
5430
5431         /* Consume the `typeid' token.  */
5432         cp_lexer_consume_token (parser->lexer);
5433         /* Look for the `(' token.  */
5434         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5435         /* Types cannot be defined in a `typeid' expression.  */
5436         saved_message = parser->type_definition_forbidden_message;
5437         parser->type_definition_forbidden_message
5438           = G_("types may not be defined in a %<typeid%> expression");
5439         /* We can't be sure yet whether we're looking at a type-id or an
5440            expression.  */
5441         cp_parser_parse_tentatively (parser);
5442         /* Try a type-id first.  */
5443         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5444         parser->in_type_id_in_expr_p = true;
5445         type = cp_parser_type_id (parser);
5446         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5447         /* Look for the `)' token.  Otherwise, we can't be sure that
5448            we're not looking at an expression: consider `typeid (int
5449            (3))', for example.  */
5450         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5451         /* If all went well, simply lookup the type-id.  */
5452         if (cp_parser_parse_definitely (parser))
5453           postfix_expression = get_typeid (type);
5454         /* Otherwise, fall back to the expression variant.  */
5455         else
5456           {
5457             tree expression;
5458
5459             /* Look for an expression.  */
5460             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5461             /* Compute its typeid.  */
5462             postfix_expression = build_typeid (expression);
5463             /* Look for the `)' token.  */
5464             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5465           }
5466         /* Restore the saved message.  */
5467         parser->type_definition_forbidden_message = saved_message;
5468         /* `typeid' may not appear in an integral constant expression.  */
5469         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5470           return error_mark_node;
5471       }
5472       break;
5473
5474     case RID_TYPENAME:
5475       {
5476         tree type;
5477         /* The syntax permitted here is the same permitted for an
5478            elaborated-type-specifier.  */
5479         type = cp_parser_elaborated_type_specifier (parser,
5480                                                     /*is_friend=*/false,
5481                                                     /*is_declaration=*/false);
5482         postfix_expression = cp_parser_functional_cast (parser, type);
5483       }
5484       break;
5485
5486     default:
5487       {
5488         tree type;
5489
5490         /* If the next thing is a simple-type-specifier, we may be
5491            looking at a functional cast.  We could also be looking at
5492            an id-expression.  So, we try the functional cast, and if
5493            that doesn't work we fall back to the primary-expression.  */
5494         cp_parser_parse_tentatively (parser);
5495         /* Look for the simple-type-specifier.  */
5496         type = cp_parser_simple_type_specifier (parser,
5497                                                 /*decl_specs=*/NULL,
5498                                                 CP_PARSER_FLAGS_NONE);
5499         /* Parse the cast itself.  */
5500         if (!cp_parser_error_occurred (parser))
5501           postfix_expression
5502             = cp_parser_functional_cast (parser, type);
5503         /* If that worked, we're done.  */
5504         if (cp_parser_parse_definitely (parser))
5505           break;
5506
5507         /* If the functional-cast didn't work out, try a
5508            compound-literal.  */
5509         if (cp_parser_allow_gnu_extensions_p (parser)
5510             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5511           {
5512             VEC(constructor_elt,gc) *initializer_list = NULL;
5513             bool saved_in_type_id_in_expr_p;
5514
5515             cp_parser_parse_tentatively (parser);
5516             /* Consume the `('.  */
5517             cp_lexer_consume_token (parser->lexer);
5518             /* Parse the type.  */
5519             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5520             parser->in_type_id_in_expr_p = true;
5521             type = cp_parser_type_id (parser);
5522             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5523             /* Look for the `)'.  */
5524             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5525             /* Look for the `{'.  */
5526             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5527             /* If things aren't going well, there's no need to
5528                keep going.  */
5529             if (!cp_parser_error_occurred (parser))
5530               {
5531                 bool non_constant_p;
5532                 /* Parse the initializer-list.  */
5533                 initializer_list
5534                   = cp_parser_initializer_list (parser, &non_constant_p);
5535                 /* Allow a trailing `,'.  */
5536                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5537                   cp_lexer_consume_token (parser->lexer);
5538                 /* Look for the final `}'.  */
5539                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5540               }
5541             /* If that worked, we're definitely looking at a
5542                compound-literal expression.  */
5543             if (cp_parser_parse_definitely (parser))
5544               {
5545                 /* Warn the user that a compound literal is not
5546                    allowed in standard C++.  */
5547                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5548                 /* For simplicity, we disallow compound literals in
5549                    constant-expressions.  We could
5550                    allow compound literals of integer type, whose
5551                    initializer was a constant, in constant
5552                    expressions.  Permitting that usage, as a further
5553                    extension, would not change the meaning of any
5554                    currently accepted programs.  (Of course, as
5555                    compound literals are not part of ISO C++, the
5556                    standard has nothing to say.)  */
5557                 if (cp_parser_non_integral_constant_expression (parser,
5558                                                                 NIC_NCC))
5559                   {
5560                     postfix_expression = error_mark_node;
5561                     break;
5562                   }
5563                 /* Form the representation of the compound-literal.  */
5564                 postfix_expression
5565                   = (finish_compound_literal
5566                      (type, build_constructor (init_list_type_node,
5567                                                initializer_list),
5568                       tf_warning_or_error));
5569                 break;
5570               }
5571           }
5572
5573         /* It must be a primary-expression.  */
5574         postfix_expression
5575           = cp_parser_primary_expression (parser, address_p, cast_p,
5576                                           /*template_arg_p=*/false,
5577                                           &idk);
5578       }
5579       break;
5580     }
5581
5582   /* Keep looping until the postfix-expression is complete.  */
5583   while (true)
5584     {
5585       if (idk == CP_ID_KIND_UNQUALIFIED
5586           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5587           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5588         /* It is not a Koenig lookup function call.  */
5589         postfix_expression
5590           = unqualified_name_lookup_error (postfix_expression);
5591
5592       /* Peek at the next token.  */
5593       token = cp_lexer_peek_token (parser->lexer);
5594
5595       switch (token->type)
5596         {
5597         case CPP_OPEN_SQUARE:
5598           postfix_expression
5599             = cp_parser_postfix_open_square_expression (parser,
5600                                                         postfix_expression,
5601                                                         false);
5602           idk = CP_ID_KIND_NONE;
5603           is_member_access = false;
5604           break;
5605
5606         case CPP_OPEN_PAREN:
5607           /* postfix-expression ( expression-list [opt] ) */
5608           {
5609             bool koenig_p;
5610             bool is_builtin_constant_p;
5611             bool saved_integral_constant_expression_p = false;
5612             bool saved_non_integral_constant_expression_p = false;
5613             VEC(tree,gc) *args;
5614
5615             is_member_access = false;
5616
5617             is_builtin_constant_p
5618               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5619             if (is_builtin_constant_p)
5620               {
5621                 /* The whole point of __builtin_constant_p is to allow
5622                    non-constant expressions to appear as arguments.  */
5623                 saved_integral_constant_expression_p
5624                   = parser->integral_constant_expression_p;
5625                 saved_non_integral_constant_expression_p
5626                   = parser->non_integral_constant_expression_p;
5627                 parser->integral_constant_expression_p = false;
5628               }
5629             args = (cp_parser_parenthesized_expression_list
5630                     (parser, non_attr,
5631                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5632                      /*non_constant_p=*/NULL));
5633             if (is_builtin_constant_p)
5634               {
5635                 parser->integral_constant_expression_p
5636                   = saved_integral_constant_expression_p;
5637                 parser->non_integral_constant_expression_p
5638                   = saved_non_integral_constant_expression_p;
5639               }
5640
5641             if (args == NULL)
5642               {
5643                 postfix_expression = error_mark_node;
5644                 break;
5645               }
5646
5647             /* Function calls are not permitted in
5648                constant-expressions.  */
5649             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5650                 && cp_parser_non_integral_constant_expression (parser,
5651                                                                NIC_FUNC_CALL))
5652               {
5653                 postfix_expression = error_mark_node;
5654                 release_tree_vector (args);
5655                 break;
5656               }
5657
5658             koenig_p = false;
5659             if (idk == CP_ID_KIND_UNQUALIFIED
5660                 || idk == CP_ID_KIND_TEMPLATE_ID)
5661               {
5662                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5663                   {
5664                     if (!VEC_empty (tree, args))
5665                       {
5666                         koenig_p = true;
5667                         if (!any_type_dependent_arguments_p (args))
5668                           postfix_expression
5669                             = perform_koenig_lookup (postfix_expression, args,
5670                                                      /*include_std=*/false,
5671                                                      tf_warning_or_error);
5672                       }
5673                     else
5674                       postfix_expression
5675                         = unqualified_fn_lookup_error (postfix_expression);
5676                   }
5677                 /* We do not perform argument-dependent lookup if
5678                    normal lookup finds a non-function, in accordance
5679                    with the expected resolution of DR 218.  */
5680                 else if (!VEC_empty (tree, args)
5681                          && is_overloaded_fn (postfix_expression))
5682                   {
5683                     tree fn = get_first_fn (postfix_expression);
5684                     fn = STRIP_TEMPLATE (fn);
5685
5686                     /* Do not do argument dependent lookup if regular
5687                        lookup finds a member function or a block-scope
5688                        function declaration.  [basic.lookup.argdep]/3  */
5689                     if (!DECL_FUNCTION_MEMBER_P (fn)
5690                         && !DECL_LOCAL_FUNCTION_P (fn))
5691                       {
5692                         koenig_p = true;
5693                         if (!any_type_dependent_arguments_p (args))
5694                           postfix_expression
5695                             = perform_koenig_lookup (postfix_expression, args,
5696                                                      /*include_std=*/false,
5697                                                      tf_warning_or_error);
5698                       }
5699                   }
5700               }
5701
5702             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5703               {
5704                 tree instance = TREE_OPERAND (postfix_expression, 0);
5705                 tree fn = TREE_OPERAND (postfix_expression, 1);
5706
5707                 if (processing_template_decl
5708                     && (type_dependent_expression_p (instance)
5709                         || (!BASELINK_P (fn)
5710                             && TREE_CODE (fn) != FIELD_DECL)
5711                         || type_dependent_expression_p (fn)
5712                         || any_type_dependent_arguments_p (args)))
5713                   {
5714                     postfix_expression
5715                       = build_nt_call_vec (postfix_expression, args);
5716                     release_tree_vector (args);
5717                     break;
5718                   }
5719
5720                 if (BASELINK_P (fn))
5721                   {
5722                   postfix_expression
5723                     = (build_new_method_call
5724                        (instance, fn, &args, NULL_TREE,
5725                         (idk == CP_ID_KIND_QUALIFIED
5726                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5727                          : LOOKUP_NORMAL),
5728                         /*fn_p=*/NULL,
5729                         tf_warning_or_error));
5730                   }
5731                 else
5732                   postfix_expression
5733                     = finish_call_expr (postfix_expression, &args,
5734                                         /*disallow_virtual=*/false,
5735                                         /*koenig_p=*/false,
5736                                         tf_warning_or_error);
5737               }
5738             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5739                      || TREE_CODE (postfix_expression) == MEMBER_REF
5740                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5741               postfix_expression = (build_offset_ref_call_from_tree
5742                                     (postfix_expression, &args));
5743             else if (idk == CP_ID_KIND_QUALIFIED)
5744               /* A call to a static class member, or a namespace-scope
5745                  function.  */
5746               postfix_expression
5747                 = finish_call_expr (postfix_expression, &args,
5748                                     /*disallow_virtual=*/true,
5749                                     koenig_p,
5750                                     tf_warning_or_error);
5751             else
5752               /* All other function calls.  */
5753               postfix_expression
5754                 = finish_call_expr (postfix_expression, &args,
5755                                     /*disallow_virtual=*/false,
5756                                     koenig_p,
5757                                     tf_warning_or_error);
5758
5759             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5760             idk = CP_ID_KIND_NONE;
5761
5762             release_tree_vector (args);
5763           }
5764           break;
5765
5766         case CPP_DOT:
5767         case CPP_DEREF:
5768           /* postfix-expression . template [opt] id-expression
5769              postfix-expression . pseudo-destructor-name
5770              postfix-expression -> template [opt] id-expression
5771              postfix-expression -> pseudo-destructor-name */
5772
5773           /* Consume the `.' or `->' operator.  */
5774           cp_lexer_consume_token (parser->lexer);
5775
5776           postfix_expression
5777             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5778                                                       postfix_expression,
5779                                                       false, &idk,
5780                                                       token->location);
5781
5782           is_member_access = true;
5783           break;
5784
5785         case CPP_PLUS_PLUS:
5786           /* postfix-expression ++  */
5787           /* Consume the `++' token.  */
5788           cp_lexer_consume_token (parser->lexer);
5789           /* Generate a representation for the complete expression.  */
5790           postfix_expression
5791             = finish_increment_expr (postfix_expression,
5792                                      POSTINCREMENT_EXPR);
5793           /* Increments may not appear in constant-expressions.  */
5794           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5795             postfix_expression = error_mark_node;
5796           idk = CP_ID_KIND_NONE;
5797           is_member_access = false;
5798           break;
5799
5800         case CPP_MINUS_MINUS:
5801           /* postfix-expression -- */
5802           /* Consume the `--' token.  */
5803           cp_lexer_consume_token (parser->lexer);
5804           /* Generate a representation for the complete expression.  */
5805           postfix_expression
5806             = finish_increment_expr (postfix_expression,
5807                                      POSTDECREMENT_EXPR);
5808           /* Decrements may not appear in constant-expressions.  */
5809           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5810             postfix_expression = error_mark_node;
5811           idk = CP_ID_KIND_NONE;
5812           is_member_access = false;
5813           break;
5814
5815         default:
5816           if (pidk_return != NULL)
5817             * pidk_return = idk;
5818           if (member_access_only_p)
5819             return is_member_access? postfix_expression : error_mark_node;
5820           else
5821             return postfix_expression;
5822         }
5823     }
5824
5825   /* We should never get here.  */
5826   gcc_unreachable ();
5827   return error_mark_node;
5828 }
5829
5830 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5831    by cp_parser_builtin_offsetof.  We're looking for
5832
5833      postfix-expression [ expression ]
5834      postfix-expression [ braced-init-list ] (C++11)
5835
5836    FOR_OFFSETOF is set if we're being called in that context, which
5837    changes how we deal with integer constant expressions.  */
5838
5839 static tree
5840 cp_parser_postfix_open_square_expression (cp_parser *parser,
5841                                           tree postfix_expression,
5842                                           bool for_offsetof)
5843 {
5844   tree index;
5845
5846   /* Consume the `[' token.  */
5847   cp_lexer_consume_token (parser->lexer);
5848
5849   /* Parse the index expression.  */
5850   /* ??? For offsetof, there is a question of what to allow here.  If
5851      offsetof is not being used in an integral constant expression context,
5852      then we *could* get the right answer by computing the value at runtime.
5853      If we are in an integral constant expression context, then we might
5854      could accept any constant expression; hard to say without analysis.
5855      Rather than open the barn door too wide right away, allow only integer
5856      constant expressions here.  */
5857   if (for_offsetof)
5858     index = cp_parser_constant_expression (parser, false, NULL);
5859   else
5860     {
5861       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
5862         {
5863           bool expr_nonconst_p;
5864           maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
5865           index = cp_parser_braced_list (parser, &expr_nonconst_p);
5866         }
5867       else
5868         index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5869     }
5870
5871   /* Look for the closing `]'.  */
5872   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5873
5874   /* Build the ARRAY_REF.  */
5875   postfix_expression = grok_array_decl (postfix_expression, index);
5876
5877   /* When not doing offsetof, array references are not permitted in
5878      constant-expressions.  */
5879   if (!for_offsetof
5880       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5881     postfix_expression = error_mark_node;
5882
5883   return postfix_expression;
5884 }
5885
5886 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5887    by cp_parser_builtin_offsetof.  We're looking for
5888
5889      postfix-expression . template [opt] id-expression
5890      postfix-expression . pseudo-destructor-name
5891      postfix-expression -> template [opt] id-expression
5892      postfix-expression -> pseudo-destructor-name
5893
5894    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5895    limits what of the above we'll actually accept, but nevermind.
5896    TOKEN_TYPE is the "." or "->" token, which will already have been
5897    removed from the stream.  */
5898
5899 static tree
5900 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5901                                         enum cpp_ttype token_type,
5902                                         tree postfix_expression,
5903                                         bool for_offsetof, cp_id_kind *idk,
5904                                         location_t location)
5905 {
5906   tree name;
5907   bool dependent_p;
5908   bool pseudo_destructor_p;
5909   tree scope = NULL_TREE;
5910
5911   /* If this is a `->' operator, dereference the pointer.  */
5912   if (token_type == CPP_DEREF)
5913     postfix_expression = build_x_arrow (postfix_expression);
5914   /* Check to see whether or not the expression is type-dependent.  */
5915   dependent_p = type_dependent_expression_p (postfix_expression);
5916   /* The identifier following the `->' or `.' is not qualified.  */
5917   parser->scope = NULL_TREE;
5918   parser->qualifying_scope = NULL_TREE;
5919   parser->object_scope = NULL_TREE;
5920   *idk = CP_ID_KIND_NONE;
5921
5922   /* Enter the scope corresponding to the type of the object
5923      given by the POSTFIX_EXPRESSION.  */
5924   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5925     {
5926       scope = TREE_TYPE (postfix_expression);
5927       /* According to the standard, no expression should ever have
5928          reference type.  Unfortunately, we do not currently match
5929          the standard in this respect in that our internal representation
5930          of an expression may have reference type even when the standard
5931          says it does not.  Therefore, we have to manually obtain the
5932          underlying type here.  */
5933       scope = non_reference (scope);
5934       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5935       if (scope == unknown_type_node)
5936         {
5937           error_at (location, "%qE does not have class type",
5938                     postfix_expression);
5939           scope = NULL_TREE;
5940         }
5941       /* Unlike the object expression in other contexts, *this is not
5942          required to be of complete type for purposes of class member
5943          access (5.2.5) outside the member function body.  */
5944       else if (scope != current_class_ref
5945                && !(processing_template_decl && scope == current_class_type))
5946         scope = complete_type_or_else (scope, NULL_TREE);
5947       /* Let the name lookup machinery know that we are processing a
5948          class member access expression.  */
5949       parser->context->object_type = scope;
5950       /* If something went wrong, we want to be able to discern that case,
5951          as opposed to the case where there was no SCOPE due to the type
5952          of expression being dependent.  */
5953       if (!scope)
5954         scope = error_mark_node;
5955       /* If the SCOPE was erroneous, make the various semantic analysis
5956          functions exit quickly -- and without issuing additional error
5957          messages.  */
5958       if (scope == error_mark_node)
5959         postfix_expression = error_mark_node;
5960     }
5961
5962   /* Assume this expression is not a pseudo-destructor access.  */
5963   pseudo_destructor_p = false;
5964
5965   /* If the SCOPE is a scalar type, then, if this is a valid program,
5966      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5967      is type dependent, it can be pseudo-destructor-name or something else.
5968      Try to parse it as pseudo-destructor-name first.  */
5969   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5970     {
5971       tree s;
5972       tree type;
5973
5974       cp_parser_parse_tentatively (parser);
5975       /* Parse the pseudo-destructor-name.  */
5976       s = NULL_TREE;
5977       cp_parser_pseudo_destructor_name (parser, &s, &type);
5978       if (dependent_p
5979           && (cp_parser_error_occurred (parser)
5980               || TREE_CODE (type) != TYPE_DECL
5981               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5982         cp_parser_abort_tentative_parse (parser);
5983       else if (cp_parser_parse_definitely (parser))
5984         {
5985           pseudo_destructor_p = true;
5986           postfix_expression
5987             = finish_pseudo_destructor_expr (postfix_expression,
5988                                              s, TREE_TYPE (type));
5989         }
5990     }
5991
5992   if (!pseudo_destructor_p)
5993     {
5994       /* If the SCOPE is not a scalar type, we are looking at an
5995          ordinary class member access expression, rather than a
5996          pseudo-destructor-name.  */
5997       bool template_p;
5998       cp_token *token = cp_lexer_peek_token (parser->lexer);
5999       /* Parse the id-expression.  */
6000       name = (cp_parser_id_expression
6001               (parser,
6002                cp_parser_optional_template_keyword (parser),
6003                /*check_dependency_p=*/true,
6004                &template_p,
6005                /*declarator_p=*/false,
6006                /*optional_p=*/false));
6007       /* In general, build a SCOPE_REF if the member name is qualified.
6008          However, if the name was not dependent and has already been
6009          resolved; there is no need to build the SCOPE_REF.  For example;
6010
6011              struct X { void f(); };
6012              template <typename T> void f(T* t) { t->X::f(); }
6013
6014          Even though "t" is dependent, "X::f" is not and has been resolved
6015          to a BASELINK; there is no need to include scope information.  */
6016
6017       /* But we do need to remember that there was an explicit scope for
6018          virtual function calls.  */
6019       if (parser->scope)
6020         *idk = CP_ID_KIND_QUALIFIED;
6021
6022       /* If the name is a template-id that names a type, we will get a
6023          TYPE_DECL here.  That is invalid code.  */
6024       if (TREE_CODE (name) == TYPE_DECL)
6025         {
6026           error_at (token->location, "invalid use of %qD", name);
6027           postfix_expression = error_mark_node;
6028         }
6029       else
6030         {
6031           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6032             {
6033               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6034                 {
6035                   error_at (token->location, "%<%D::%D%> is not a class member",
6036                             parser->scope, name);
6037                   postfix_expression = error_mark_node;
6038                 }
6039               else
6040                 name = build_qualified_name (/*type=*/NULL_TREE,
6041                                              parser->scope,
6042                                              name,
6043                                              template_p);
6044               parser->scope = NULL_TREE;
6045               parser->qualifying_scope = NULL_TREE;
6046               parser->object_scope = NULL_TREE;
6047             }
6048           if (scope && name && BASELINK_P (name))
6049             adjust_result_of_qualified_name_lookup
6050               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6051           postfix_expression
6052             = finish_class_member_access_expr (postfix_expression, name,
6053                                                template_p, 
6054                                                tf_warning_or_error);
6055         }
6056     }
6057
6058   /* We no longer need to look up names in the scope of the object on
6059      the left-hand side of the `.' or `->' operator.  */
6060   parser->context->object_type = NULL_TREE;
6061
6062   /* Outside of offsetof, these operators may not appear in
6063      constant-expressions.  */
6064   if (!for_offsetof
6065       && (cp_parser_non_integral_constant_expression
6066           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6067     postfix_expression = error_mark_node;
6068
6069   return postfix_expression;
6070 }
6071
6072 /* Parse a parenthesized expression-list.
6073
6074    expression-list:
6075      assignment-expression
6076      expression-list, assignment-expression
6077
6078    attribute-list:
6079      expression-list
6080      identifier
6081      identifier, expression-list
6082
6083    CAST_P is true if this expression is the target of a cast.
6084
6085    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6086    argument pack.
6087
6088    Returns a vector of trees.  Each element is a representation of an
6089    assignment-expression.  NULL is returned if the ( and or ) are
6090    missing.  An empty, but allocated, vector is returned on no
6091    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6092    if we are parsing an attribute list for an attribute that wants a
6093    plain identifier argument, normal_attr for an attribute that wants
6094    an expression, or non_attr if we aren't parsing an attribute list.  If
6095    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6096    not all of the expressions in the list were constant.  */
6097
6098 static VEC(tree,gc) *
6099 cp_parser_parenthesized_expression_list (cp_parser* parser,
6100                                          int is_attribute_list,
6101                                          bool cast_p,
6102                                          bool allow_expansion_p,
6103                                          bool *non_constant_p)
6104 {
6105   VEC(tree,gc) *expression_list;
6106   bool fold_expr_p = is_attribute_list != non_attr;
6107   tree identifier = NULL_TREE;
6108   bool saved_greater_than_is_operator_p;
6109
6110   /* Assume all the expressions will be constant.  */
6111   if (non_constant_p)
6112     *non_constant_p = false;
6113
6114   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6115     return NULL;
6116
6117   expression_list = make_tree_vector ();
6118
6119   /* Within a parenthesized expression, a `>' token is always
6120      the greater-than operator.  */
6121   saved_greater_than_is_operator_p
6122     = parser->greater_than_is_operator_p;
6123   parser->greater_than_is_operator_p = true;
6124
6125   /* Consume expressions until there are no more.  */
6126   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6127     while (true)
6128       {
6129         tree expr;
6130
6131         /* At the beginning of attribute lists, check to see if the
6132            next token is an identifier.  */
6133         if (is_attribute_list == id_attr
6134             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6135           {
6136             cp_token *token;
6137
6138             /* Consume the identifier.  */
6139             token = cp_lexer_consume_token (parser->lexer);
6140             /* Save the identifier.  */
6141             identifier = token->u.value;
6142           }
6143         else
6144           {
6145             bool expr_non_constant_p;
6146
6147             /* Parse the next assignment-expression.  */
6148             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6149               {
6150                 /* A braced-init-list.  */
6151                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6152                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6153                 if (non_constant_p && expr_non_constant_p)
6154                   *non_constant_p = true;
6155               }
6156             else if (non_constant_p)
6157               {
6158                 expr = (cp_parser_constant_expression
6159                         (parser, /*allow_non_constant_p=*/true,
6160                          &expr_non_constant_p));
6161                 if (expr_non_constant_p)
6162                   *non_constant_p = true;
6163               }
6164             else
6165               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6166
6167             if (fold_expr_p)
6168               expr = fold_non_dependent_expr (expr);
6169
6170             /* If we have an ellipsis, then this is an expression
6171                expansion.  */
6172             if (allow_expansion_p
6173                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6174               {
6175                 /* Consume the `...'.  */
6176                 cp_lexer_consume_token (parser->lexer);
6177
6178                 /* Build the argument pack.  */
6179                 expr = make_pack_expansion (expr);
6180               }
6181
6182              /* Add it to the list.  We add error_mark_node
6183                 expressions to the list, so that we can still tell if
6184                 the correct form for a parenthesized expression-list
6185                 is found. That gives better errors.  */
6186             VEC_safe_push (tree, gc, expression_list, expr);
6187
6188             if (expr == error_mark_node)
6189               goto skip_comma;
6190           }
6191
6192         /* After the first item, attribute lists look the same as
6193            expression lists.  */
6194         is_attribute_list = non_attr;
6195
6196       get_comma:;
6197         /* If the next token isn't a `,', then we are done.  */
6198         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6199           break;
6200
6201         /* Otherwise, consume the `,' and keep going.  */
6202         cp_lexer_consume_token (parser->lexer);
6203       }
6204
6205   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6206     {
6207       int ending;
6208
6209     skip_comma:;
6210       /* We try and resync to an unnested comma, as that will give the
6211          user better diagnostics.  */
6212       ending = cp_parser_skip_to_closing_parenthesis (parser,
6213                                                       /*recovering=*/true,
6214                                                       /*or_comma=*/true,
6215                                                       /*consume_paren=*/true);
6216       if (ending < 0)
6217         goto get_comma;
6218       if (!ending)
6219         {
6220           parser->greater_than_is_operator_p
6221             = saved_greater_than_is_operator_p;
6222           return NULL;
6223         }
6224     }
6225
6226   parser->greater_than_is_operator_p
6227     = saved_greater_than_is_operator_p;
6228
6229   if (identifier)
6230     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6231
6232   return expression_list;
6233 }
6234
6235 /* Parse a pseudo-destructor-name.
6236
6237    pseudo-destructor-name:
6238      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6239      :: [opt] nested-name-specifier template template-id :: ~ type-name
6240      :: [opt] nested-name-specifier [opt] ~ type-name
6241
6242    If either of the first two productions is used, sets *SCOPE to the
6243    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6244    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6245    or ERROR_MARK_NODE if the parse fails.  */
6246
6247 static void
6248 cp_parser_pseudo_destructor_name (cp_parser* parser,
6249                                   tree* scope,
6250                                   tree* type)
6251 {
6252   bool nested_name_specifier_p;
6253
6254   /* Assume that things will not work out.  */
6255   *type = error_mark_node;
6256
6257   /* Look for the optional `::' operator.  */
6258   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6259   /* Look for the optional nested-name-specifier.  */
6260   nested_name_specifier_p
6261     = (cp_parser_nested_name_specifier_opt (parser,
6262                                             /*typename_keyword_p=*/false,
6263                                             /*check_dependency_p=*/true,
6264                                             /*type_p=*/false,
6265                                             /*is_declaration=*/false)
6266        != NULL_TREE);
6267   /* Now, if we saw a nested-name-specifier, we might be doing the
6268      second production.  */
6269   if (nested_name_specifier_p
6270       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6271     {
6272       /* Consume the `template' keyword.  */
6273       cp_lexer_consume_token (parser->lexer);
6274       /* Parse the template-id.  */
6275       cp_parser_template_id (parser,
6276                              /*template_keyword_p=*/true,
6277                              /*check_dependency_p=*/false,
6278                              /*is_declaration=*/true);
6279       /* Look for the `::' token.  */
6280       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6281     }
6282   /* If the next token is not a `~', then there might be some
6283      additional qualification.  */
6284   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6285     {
6286       /* At this point, we're looking for "type-name :: ~".  The type-name
6287          must not be a class-name, since this is a pseudo-destructor.  So,
6288          it must be either an enum-name, or a typedef-name -- both of which
6289          are just identifiers.  So, we peek ahead to check that the "::"
6290          and "~" tokens are present; if they are not, then we can avoid
6291          calling type_name.  */
6292       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6293           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6294           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6295         {
6296           cp_parser_error (parser, "non-scalar type");
6297           return;
6298         }
6299
6300       /* Look for the type-name.  */
6301       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6302       if (*scope == error_mark_node)
6303         return;
6304
6305       /* Look for the `::' token.  */
6306       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6307     }
6308   else
6309     *scope = NULL_TREE;
6310
6311   /* Look for the `~'.  */
6312   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6313
6314   /* Once we see the ~, this has to be a pseudo-destructor.  */
6315   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6316     cp_parser_commit_to_tentative_parse (parser);
6317
6318   /* Look for the type-name again.  We are not responsible for
6319      checking that it matches the first type-name.  */
6320   *type = cp_parser_nonclass_name (parser);
6321 }
6322
6323 /* Parse a unary-expression.
6324
6325    unary-expression:
6326      postfix-expression
6327      ++ cast-expression
6328      -- cast-expression
6329      unary-operator cast-expression
6330      sizeof unary-expression
6331      sizeof ( type-id )
6332      alignof ( type-id )  [C++0x]
6333      new-expression
6334      delete-expression
6335
6336    GNU Extensions:
6337
6338    unary-expression:
6339      __extension__ cast-expression
6340      __alignof__ unary-expression
6341      __alignof__ ( type-id )
6342      alignof unary-expression  [C++0x]
6343      __real__ cast-expression
6344      __imag__ cast-expression
6345      && identifier
6346
6347    ADDRESS_P is true iff the unary-expression is appearing as the
6348    operand of the `&' operator.   CAST_P is true if this expression is
6349    the target of a cast.
6350
6351    Returns a representation of the expression.  */
6352
6353 static tree
6354 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6355                             cp_id_kind * pidk)
6356 {
6357   cp_token *token;
6358   enum tree_code unary_operator;
6359
6360   /* Peek at the next token.  */
6361   token = cp_lexer_peek_token (parser->lexer);
6362   /* Some keywords give away the kind of expression.  */
6363   if (token->type == CPP_KEYWORD)
6364     {
6365       enum rid keyword = token->keyword;
6366
6367       switch (keyword)
6368         {
6369         case RID_ALIGNOF:
6370         case RID_SIZEOF:
6371           {
6372             tree operand;
6373             enum tree_code op;
6374
6375             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6376             /* Consume the token.  */
6377             cp_lexer_consume_token (parser->lexer);
6378             /* Parse the operand.  */
6379             operand = cp_parser_sizeof_operand (parser, keyword);
6380
6381             if (TYPE_P (operand))
6382               return cxx_sizeof_or_alignof_type (operand, op, true);
6383             else
6384               {
6385                 /* ISO C++ defines alignof only with types, not with
6386                    expressions. So pedwarn if alignof is used with a non-
6387                    type expression. However, __alignof__ is ok.  */
6388                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6389                   pedwarn (token->location, OPT_pedantic,
6390                            "ISO C++ does not allow %<alignof%> "
6391                            "with a non-type");
6392
6393                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6394               }
6395           }
6396
6397         case RID_NEW:
6398           return cp_parser_new_expression (parser);
6399
6400         case RID_DELETE:
6401           return cp_parser_delete_expression (parser);
6402
6403         case RID_EXTENSION:
6404           {
6405             /* The saved value of the PEDANTIC flag.  */
6406             int saved_pedantic;
6407             tree expr;
6408
6409             /* Save away the PEDANTIC flag.  */
6410             cp_parser_extension_opt (parser, &saved_pedantic);
6411             /* Parse the cast-expression.  */
6412             expr = cp_parser_simple_cast_expression (parser);
6413             /* Restore the PEDANTIC flag.  */
6414             pedantic = saved_pedantic;
6415
6416             return expr;
6417           }
6418
6419         case RID_REALPART:
6420         case RID_IMAGPART:
6421           {
6422             tree expression;
6423
6424             /* Consume the `__real__' or `__imag__' token.  */
6425             cp_lexer_consume_token (parser->lexer);
6426             /* Parse the cast-expression.  */
6427             expression = cp_parser_simple_cast_expression (parser);
6428             /* Create the complete representation.  */
6429             return build_x_unary_op ((keyword == RID_REALPART
6430                                       ? REALPART_EXPR : IMAGPART_EXPR),
6431                                      expression,
6432                                      tf_warning_or_error);
6433           }
6434           break;
6435
6436         case RID_TRANSACTION_ATOMIC:
6437         case RID_TRANSACTION_RELAXED:
6438           return cp_parser_transaction_expression (parser, keyword);
6439
6440         case RID_NOEXCEPT:
6441           {
6442             tree expr;
6443             const char *saved_message;
6444             bool saved_integral_constant_expression_p;
6445             bool saved_non_integral_constant_expression_p;
6446             bool saved_greater_than_is_operator_p;
6447
6448             cp_lexer_consume_token (parser->lexer);
6449             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6450
6451             saved_message = parser->type_definition_forbidden_message;
6452             parser->type_definition_forbidden_message
6453               = G_("types may not be defined in %<noexcept%> expressions");
6454
6455             saved_integral_constant_expression_p
6456               = parser->integral_constant_expression_p;
6457             saved_non_integral_constant_expression_p
6458               = parser->non_integral_constant_expression_p;
6459             parser->integral_constant_expression_p = false;
6460
6461             saved_greater_than_is_operator_p
6462               = parser->greater_than_is_operator_p;
6463             parser->greater_than_is_operator_p = true;
6464
6465             ++cp_unevaluated_operand;
6466             ++c_inhibit_evaluation_warnings;
6467             expr = cp_parser_expression (parser, false, NULL);
6468             --c_inhibit_evaluation_warnings;
6469             --cp_unevaluated_operand;
6470
6471             parser->greater_than_is_operator_p
6472               = saved_greater_than_is_operator_p;
6473
6474             parser->integral_constant_expression_p
6475               = saved_integral_constant_expression_p;
6476             parser->non_integral_constant_expression_p
6477               = saved_non_integral_constant_expression_p;
6478
6479             parser->type_definition_forbidden_message = saved_message;
6480
6481             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6482             return finish_noexcept_expr (expr, tf_warning_or_error);
6483           }
6484
6485         default:
6486           break;
6487         }
6488     }
6489
6490   /* Look for the `:: new' and `:: delete', which also signal the
6491      beginning of a new-expression, or delete-expression,
6492      respectively.  If the next token is `::', then it might be one of
6493      these.  */
6494   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6495     {
6496       enum rid keyword;
6497
6498       /* See if the token after the `::' is one of the keywords in
6499          which we're interested.  */
6500       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6501       /* If it's `new', we have a new-expression.  */
6502       if (keyword == RID_NEW)
6503         return cp_parser_new_expression (parser);
6504       /* Similarly, for `delete'.  */
6505       else if (keyword == RID_DELETE)
6506         return cp_parser_delete_expression (parser);
6507     }
6508
6509   /* Look for a unary operator.  */
6510   unary_operator = cp_parser_unary_operator (token);
6511   /* The `++' and `--' operators can be handled similarly, even though
6512      they are not technically unary-operators in the grammar.  */
6513   if (unary_operator == ERROR_MARK)
6514     {
6515       if (token->type == CPP_PLUS_PLUS)
6516         unary_operator = PREINCREMENT_EXPR;
6517       else if (token->type == CPP_MINUS_MINUS)
6518         unary_operator = PREDECREMENT_EXPR;
6519       /* Handle the GNU address-of-label extension.  */
6520       else if (cp_parser_allow_gnu_extensions_p (parser)
6521                && token->type == CPP_AND_AND)
6522         {
6523           tree identifier;
6524           tree expression;
6525           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6526
6527           /* Consume the '&&' token.  */
6528           cp_lexer_consume_token (parser->lexer);
6529           /* Look for the identifier.  */
6530           identifier = cp_parser_identifier (parser);
6531           /* Create an expression representing the address.  */
6532           expression = finish_label_address_expr (identifier, loc);
6533           if (cp_parser_non_integral_constant_expression (parser,
6534                                                           NIC_ADDR_LABEL))
6535             expression = error_mark_node;
6536           return expression;
6537         }
6538     }
6539   if (unary_operator != ERROR_MARK)
6540     {
6541       tree cast_expression;
6542       tree expression = error_mark_node;
6543       non_integral_constant non_constant_p = NIC_NONE;
6544
6545       /* Consume the operator token.  */
6546       token = cp_lexer_consume_token (parser->lexer);
6547       /* Parse the cast-expression.  */
6548       cast_expression
6549         = cp_parser_cast_expression (parser,
6550                                      unary_operator == ADDR_EXPR,
6551                                      /*cast_p=*/false, pidk);
6552       /* Now, build an appropriate representation.  */
6553       switch (unary_operator)
6554         {
6555         case INDIRECT_REF:
6556           non_constant_p = NIC_STAR;
6557           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6558                                              tf_warning_or_error);
6559           break;
6560
6561         case ADDR_EXPR:
6562            non_constant_p = NIC_ADDR;
6563           /* Fall through.  */
6564         case BIT_NOT_EXPR:
6565           expression = build_x_unary_op (unary_operator, cast_expression,
6566                                          tf_warning_or_error);
6567           break;
6568
6569         case PREINCREMENT_EXPR:
6570         case PREDECREMENT_EXPR:
6571           non_constant_p = unary_operator == PREINCREMENT_EXPR
6572                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6573           /* Fall through.  */
6574         case UNARY_PLUS_EXPR:
6575         case NEGATE_EXPR:
6576         case TRUTH_NOT_EXPR:
6577           expression = finish_unary_op_expr (unary_operator, cast_expression);
6578           break;
6579
6580         default:
6581           gcc_unreachable ();
6582         }
6583
6584       if (non_constant_p != NIC_NONE
6585           && cp_parser_non_integral_constant_expression (parser,
6586                                                          non_constant_p))
6587         expression = error_mark_node;
6588
6589       return expression;
6590     }
6591
6592   return cp_parser_postfix_expression (parser, address_p, cast_p,
6593                                        /*member_access_only_p=*/false,
6594                                        pidk);
6595 }
6596
6597 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6598    unary-operator, the corresponding tree code is returned.  */
6599
6600 static enum tree_code
6601 cp_parser_unary_operator (cp_token* token)
6602 {
6603   switch (token->type)
6604     {
6605     case CPP_MULT:
6606       return INDIRECT_REF;
6607
6608     case CPP_AND:
6609       return ADDR_EXPR;
6610
6611     case CPP_PLUS:
6612       return UNARY_PLUS_EXPR;
6613
6614     case CPP_MINUS:
6615       return NEGATE_EXPR;
6616
6617     case CPP_NOT:
6618       return TRUTH_NOT_EXPR;
6619
6620     case CPP_COMPL:
6621       return BIT_NOT_EXPR;
6622
6623     default:
6624       return ERROR_MARK;
6625     }
6626 }
6627
6628 /* Parse a new-expression.
6629
6630    new-expression:
6631      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6632      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6633
6634    Returns a representation of the expression.  */
6635
6636 static tree
6637 cp_parser_new_expression (cp_parser* parser)
6638 {
6639   bool global_scope_p;
6640   VEC(tree,gc) *placement;
6641   tree type;
6642   VEC(tree,gc) *initializer;
6643   tree nelts;
6644   tree ret;
6645
6646   /* Look for the optional `::' operator.  */
6647   global_scope_p
6648     = (cp_parser_global_scope_opt (parser,
6649                                    /*current_scope_valid_p=*/false)
6650        != NULL_TREE);
6651   /* Look for the `new' operator.  */
6652   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6653   /* There's no easy way to tell a new-placement from the
6654      `( type-id )' construct.  */
6655   cp_parser_parse_tentatively (parser);
6656   /* Look for a new-placement.  */
6657   placement = cp_parser_new_placement (parser);
6658   /* If that didn't work out, there's no new-placement.  */
6659   if (!cp_parser_parse_definitely (parser))
6660     {
6661       if (placement != NULL)
6662         release_tree_vector (placement);
6663       placement = NULL;
6664     }
6665
6666   /* If the next token is a `(', then we have a parenthesized
6667      type-id.  */
6668   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6669     {
6670       cp_token *token;
6671       /* Consume the `('.  */
6672       cp_lexer_consume_token (parser->lexer);
6673       /* Parse the type-id.  */
6674       type = cp_parser_type_id (parser);
6675       /* Look for the closing `)'.  */
6676       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6677       token = cp_lexer_peek_token (parser->lexer);
6678       /* There should not be a direct-new-declarator in this production,
6679          but GCC used to allowed this, so we check and emit a sensible error
6680          message for this case.  */
6681       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6682         {
6683           error_at (token->location,
6684                     "array bound forbidden after parenthesized type-id");
6685           inform (token->location, 
6686                   "try removing the parentheses around the type-id");
6687           cp_parser_direct_new_declarator (parser);
6688         }
6689       nelts = NULL_TREE;
6690     }
6691   /* Otherwise, there must be a new-type-id.  */
6692   else
6693     type = cp_parser_new_type_id (parser, &nelts);
6694
6695   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6696   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6697       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6698     initializer = cp_parser_new_initializer (parser);
6699   else
6700     initializer = NULL;
6701
6702   /* A new-expression may not appear in an integral constant
6703      expression.  */
6704   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6705     ret = error_mark_node;
6706   else
6707     {
6708       /* Create a representation of the new-expression.  */
6709       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6710                        tf_warning_or_error);
6711     }
6712
6713   if (placement != NULL)
6714     release_tree_vector (placement);
6715   if (initializer != NULL)
6716     release_tree_vector (initializer);
6717
6718   return ret;
6719 }
6720
6721 /* Parse a new-placement.
6722
6723    new-placement:
6724      ( expression-list )
6725
6726    Returns the same representation as for an expression-list.  */
6727
6728 static VEC(tree,gc) *
6729 cp_parser_new_placement (cp_parser* parser)
6730 {
6731   VEC(tree,gc) *expression_list;
6732
6733   /* Parse the expression-list.  */
6734   expression_list = (cp_parser_parenthesized_expression_list
6735                      (parser, non_attr, /*cast_p=*/false,
6736                       /*allow_expansion_p=*/true,
6737                       /*non_constant_p=*/NULL));
6738
6739   return expression_list;
6740 }
6741
6742 /* Parse a new-type-id.
6743
6744    new-type-id:
6745      type-specifier-seq new-declarator [opt]
6746
6747    Returns the TYPE allocated.  If the new-type-id indicates an array
6748    type, *NELTS is set to the number of elements in the last array
6749    bound; the TYPE will not include the last array bound.  */
6750
6751 static tree
6752 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6753 {
6754   cp_decl_specifier_seq type_specifier_seq;
6755   cp_declarator *new_declarator;
6756   cp_declarator *declarator;
6757   cp_declarator *outer_declarator;
6758   const char *saved_message;
6759   tree type;
6760
6761   /* The type-specifier sequence must not contain type definitions.
6762      (It cannot contain declarations of new types either, but if they
6763      are not definitions we will catch that because they are not
6764      complete.)  */
6765   saved_message = parser->type_definition_forbidden_message;
6766   parser->type_definition_forbidden_message
6767     = G_("types may not be defined in a new-type-id");
6768   /* Parse the type-specifier-seq.  */
6769   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6770                                 /*is_trailing_return=*/false,
6771                                 &type_specifier_seq);
6772   /* Restore the old message.  */
6773   parser->type_definition_forbidden_message = saved_message;
6774   /* Parse the new-declarator.  */
6775   new_declarator = cp_parser_new_declarator_opt (parser);
6776
6777   /* Determine the number of elements in the last array dimension, if
6778      any.  */
6779   *nelts = NULL_TREE;
6780   /* Skip down to the last array dimension.  */
6781   declarator = new_declarator;
6782   outer_declarator = NULL;
6783   while (declarator && (declarator->kind == cdk_pointer
6784                         || declarator->kind == cdk_ptrmem))
6785     {
6786       outer_declarator = declarator;
6787       declarator = declarator->declarator;
6788     }
6789   while (declarator
6790          && declarator->kind == cdk_array
6791          && declarator->declarator
6792          && declarator->declarator->kind == cdk_array)
6793     {
6794       outer_declarator = declarator;
6795       declarator = declarator->declarator;
6796     }
6797
6798   if (declarator && declarator->kind == cdk_array)
6799     {
6800       *nelts = declarator->u.array.bounds;
6801       if (*nelts == error_mark_node)
6802         *nelts = integer_one_node;
6803
6804       if (outer_declarator)
6805         outer_declarator->declarator = declarator->declarator;
6806       else
6807         new_declarator = NULL;
6808     }
6809
6810   type = groktypename (&type_specifier_seq, new_declarator, false);
6811   return type;
6812 }
6813
6814 /* Parse an (optional) new-declarator.
6815
6816    new-declarator:
6817      ptr-operator new-declarator [opt]
6818      direct-new-declarator
6819
6820    Returns the declarator.  */
6821
6822 static cp_declarator *
6823 cp_parser_new_declarator_opt (cp_parser* parser)
6824 {
6825   enum tree_code code;
6826   tree type;
6827   cp_cv_quals cv_quals;
6828
6829   /* We don't know if there's a ptr-operator next, or not.  */
6830   cp_parser_parse_tentatively (parser);
6831   /* Look for a ptr-operator.  */
6832   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6833   /* If that worked, look for more new-declarators.  */
6834   if (cp_parser_parse_definitely (parser))
6835     {
6836       cp_declarator *declarator;
6837
6838       /* Parse another optional declarator.  */
6839       declarator = cp_parser_new_declarator_opt (parser);
6840
6841       return cp_parser_make_indirect_declarator
6842         (code, type, cv_quals, declarator);
6843     }
6844
6845   /* If the next token is a `[', there is a direct-new-declarator.  */
6846   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6847     return cp_parser_direct_new_declarator (parser);
6848
6849   return NULL;
6850 }
6851
6852 /* Parse a direct-new-declarator.
6853
6854    direct-new-declarator:
6855      [ expression ]
6856      direct-new-declarator [constant-expression]
6857
6858    */
6859
6860 static cp_declarator *
6861 cp_parser_direct_new_declarator (cp_parser* parser)
6862 {
6863   cp_declarator *declarator = NULL;
6864
6865   while (true)
6866     {
6867       tree expression;
6868
6869       /* Look for the opening `['.  */
6870       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6871       /* The first expression is not required to be constant.  */
6872       if (!declarator)
6873         {
6874           cp_token *token = cp_lexer_peek_token (parser->lexer);
6875           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6876           /* The standard requires that the expression have integral
6877              type.  DR 74 adds enumeration types.  We believe that the
6878              real intent is that these expressions be handled like the
6879              expression in a `switch' condition, which also allows
6880              classes with a single conversion to integral or
6881              enumeration type.  */
6882           if (!processing_template_decl)
6883             {
6884               expression
6885                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6886                                               expression,
6887                                               /*complain=*/true);
6888               if (!expression)
6889                 {
6890                   error_at (token->location,
6891                             "expression in new-declarator must have integral "
6892                             "or enumeration type");
6893                   expression = error_mark_node;
6894                 }
6895             }
6896         }
6897       /* But all the other expressions must be.  */
6898       else
6899         expression
6900           = cp_parser_constant_expression (parser,
6901                                            /*allow_non_constant=*/false,
6902                                            NULL);
6903       /* Look for the closing `]'.  */
6904       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6905
6906       /* Add this bound to the declarator.  */
6907       declarator = make_array_declarator (declarator, expression);
6908
6909       /* If the next token is not a `[', then there are no more
6910          bounds.  */
6911       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6912         break;
6913     }
6914
6915   return declarator;
6916 }
6917
6918 /* Parse a new-initializer.
6919
6920    new-initializer:
6921      ( expression-list [opt] )
6922      braced-init-list
6923
6924    Returns a representation of the expression-list.  */
6925
6926 static VEC(tree,gc) *
6927 cp_parser_new_initializer (cp_parser* parser)
6928 {
6929   VEC(tree,gc) *expression_list;
6930
6931   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6932     {
6933       tree t;
6934       bool expr_non_constant_p;
6935       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6936       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6937       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6938       expression_list = make_tree_vector_single (t);
6939     }
6940   else
6941     expression_list = (cp_parser_parenthesized_expression_list
6942                        (parser, non_attr, /*cast_p=*/false,
6943                         /*allow_expansion_p=*/true,
6944                         /*non_constant_p=*/NULL));
6945
6946   return expression_list;
6947 }
6948
6949 /* Parse a delete-expression.
6950
6951    delete-expression:
6952      :: [opt] delete cast-expression
6953      :: [opt] delete [ ] cast-expression
6954
6955    Returns a representation of the expression.  */
6956
6957 static tree
6958 cp_parser_delete_expression (cp_parser* parser)
6959 {
6960   bool global_scope_p;
6961   bool array_p;
6962   tree expression;
6963
6964   /* Look for the optional `::' operator.  */
6965   global_scope_p
6966     = (cp_parser_global_scope_opt (parser,
6967                                    /*current_scope_valid_p=*/false)
6968        != NULL_TREE);
6969   /* Look for the `delete' keyword.  */
6970   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6971   /* See if the array syntax is in use.  */
6972   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6973     {
6974       /* Consume the `[' token.  */
6975       cp_lexer_consume_token (parser->lexer);
6976       /* Look for the `]' token.  */
6977       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6978       /* Remember that this is the `[]' construct.  */
6979       array_p = true;
6980     }
6981   else
6982     array_p = false;
6983
6984   /* Parse the cast-expression.  */
6985   expression = cp_parser_simple_cast_expression (parser);
6986
6987   /* A delete-expression may not appear in an integral constant
6988      expression.  */
6989   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6990     return error_mark_node;
6991
6992   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6993                         tf_warning_or_error);
6994 }
6995
6996 /* Returns true if TOKEN may start a cast-expression and false
6997    otherwise.  */
6998
6999 static bool
7000 cp_parser_token_starts_cast_expression (cp_token *token)
7001 {
7002   switch (token->type)
7003     {
7004     case CPP_COMMA:
7005     case CPP_SEMICOLON:
7006     case CPP_QUERY:
7007     case CPP_COLON:
7008     case CPP_CLOSE_SQUARE:
7009     case CPP_CLOSE_PAREN:
7010     case CPP_CLOSE_BRACE:
7011     case CPP_DOT:
7012     case CPP_DOT_STAR:
7013     case CPP_DEREF:
7014     case CPP_DEREF_STAR:
7015     case CPP_DIV:
7016     case CPP_MOD:
7017     case CPP_LSHIFT:
7018     case CPP_RSHIFT:
7019     case CPP_LESS:
7020     case CPP_GREATER:
7021     case CPP_LESS_EQ:
7022     case CPP_GREATER_EQ:
7023     case CPP_EQ_EQ:
7024     case CPP_NOT_EQ:
7025     case CPP_EQ:
7026     case CPP_MULT_EQ:
7027     case CPP_DIV_EQ:
7028     case CPP_MOD_EQ:
7029     case CPP_PLUS_EQ:
7030     case CPP_MINUS_EQ:
7031     case CPP_RSHIFT_EQ:
7032     case CPP_LSHIFT_EQ:
7033     case CPP_AND_EQ:
7034     case CPP_XOR_EQ:
7035     case CPP_OR_EQ:
7036     case CPP_XOR:
7037     case CPP_OR:
7038     case CPP_OR_OR:
7039     case CPP_EOF:
7040       return false;
7041
7042       /* '[' may start a primary-expression in obj-c++.  */
7043     case CPP_OPEN_SQUARE:
7044       return c_dialect_objc ();
7045
7046     default:
7047       return true;
7048     }
7049 }
7050
7051 /* Parse a cast-expression.
7052
7053    cast-expression:
7054      unary-expression
7055      ( type-id ) cast-expression
7056
7057    ADDRESS_P is true iff the unary-expression is appearing as the
7058    operand of the `&' operator.   CAST_P is true if this expression is
7059    the target of a cast.
7060
7061    Returns a representation of the expression.  */
7062
7063 static tree
7064 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7065                            cp_id_kind * pidk)
7066 {
7067   /* If it's a `(', then we might be looking at a cast.  */
7068   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7069     {
7070       tree type = NULL_TREE;
7071       tree expr = NULL_TREE;
7072       bool compound_literal_p;
7073       const char *saved_message;
7074
7075       /* There's no way to know yet whether or not this is a cast.
7076          For example, `(int (3))' is a unary-expression, while `(int)
7077          3' is a cast.  So, we resort to parsing tentatively.  */
7078       cp_parser_parse_tentatively (parser);
7079       /* Types may not be defined in a cast.  */
7080       saved_message = parser->type_definition_forbidden_message;
7081       parser->type_definition_forbidden_message
7082         = G_("types may not be defined in casts");
7083       /* Consume the `('.  */
7084       cp_lexer_consume_token (parser->lexer);
7085       /* A very tricky bit is that `(struct S) { 3 }' is a
7086          compound-literal (which we permit in C++ as an extension).
7087          But, that construct is not a cast-expression -- it is a
7088          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7089          is legal; if the compound-literal were a cast-expression,
7090          you'd need an extra set of parentheses.)  But, if we parse
7091          the type-id, and it happens to be a class-specifier, then we
7092          will commit to the parse at that point, because we cannot
7093          undo the action that is done when creating a new class.  So,
7094          then we cannot back up and do a postfix-expression.
7095
7096          Therefore, we scan ahead to the closing `)', and check to see
7097          if the token after the `)' is a `{'.  If so, we are not
7098          looking at a cast-expression.
7099
7100          Save tokens so that we can put them back.  */
7101       cp_lexer_save_tokens (parser->lexer);
7102       /* Skip tokens until the next token is a closing parenthesis.
7103          If we find the closing `)', and the next token is a `{', then
7104          we are looking at a compound-literal.  */
7105       compound_literal_p
7106         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7107                                                   /*consume_paren=*/true)
7108            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7109       /* Roll back the tokens we skipped.  */
7110       cp_lexer_rollback_tokens (parser->lexer);
7111       /* If we were looking at a compound-literal, simulate an error
7112          so that the call to cp_parser_parse_definitely below will
7113          fail.  */
7114       if (compound_literal_p)
7115         cp_parser_simulate_error (parser);
7116       else
7117         {
7118           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7119           parser->in_type_id_in_expr_p = true;
7120           /* Look for the type-id.  */
7121           type = cp_parser_type_id (parser);
7122           /* Look for the closing `)'.  */
7123           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7124           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7125         }
7126
7127       /* Restore the saved message.  */
7128       parser->type_definition_forbidden_message = saved_message;
7129
7130       /* At this point this can only be either a cast or a
7131          parenthesized ctor such as `(T ())' that looks like a cast to
7132          function returning T.  */
7133       if (!cp_parser_error_occurred (parser)
7134           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7135                                                      (parser->lexer)))
7136         {
7137           cp_parser_parse_definitely (parser);
7138           expr = cp_parser_cast_expression (parser,
7139                                             /*address_p=*/false,
7140                                             /*cast_p=*/true, pidk);
7141
7142           /* Warn about old-style casts, if so requested.  */
7143           if (warn_old_style_cast
7144               && !in_system_header
7145               && !VOID_TYPE_P (type)
7146               && current_lang_name != lang_name_c)
7147             warning (OPT_Wold_style_cast, "use of old-style cast");
7148
7149           /* Only type conversions to integral or enumeration types
7150              can be used in constant-expressions.  */
7151           if (!cast_valid_in_integral_constant_expression_p (type)
7152               && cp_parser_non_integral_constant_expression (parser,
7153                                                              NIC_CAST))
7154             return error_mark_node;
7155
7156           /* Perform the cast.  */
7157           expr = build_c_cast (input_location, type, expr);
7158           return expr;
7159         }
7160       else 
7161         cp_parser_abort_tentative_parse (parser);
7162     }
7163
7164   /* If we get here, then it's not a cast, so it must be a
7165      unary-expression.  */
7166   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7167 }
7168
7169 /* Parse a binary expression of the general form:
7170
7171    pm-expression:
7172      cast-expression
7173      pm-expression .* cast-expression
7174      pm-expression ->* cast-expression
7175
7176    multiplicative-expression:
7177      pm-expression
7178      multiplicative-expression * pm-expression
7179      multiplicative-expression / pm-expression
7180      multiplicative-expression % pm-expression
7181
7182    additive-expression:
7183      multiplicative-expression
7184      additive-expression + multiplicative-expression
7185      additive-expression - multiplicative-expression
7186
7187    shift-expression:
7188      additive-expression
7189      shift-expression << additive-expression
7190      shift-expression >> additive-expression
7191
7192    relational-expression:
7193      shift-expression
7194      relational-expression < shift-expression
7195      relational-expression > shift-expression
7196      relational-expression <= shift-expression
7197      relational-expression >= shift-expression
7198
7199   GNU Extension:
7200
7201    relational-expression:
7202      relational-expression <? shift-expression
7203      relational-expression >? shift-expression
7204
7205    equality-expression:
7206      relational-expression
7207      equality-expression == relational-expression
7208      equality-expression != relational-expression
7209
7210    and-expression:
7211      equality-expression
7212      and-expression & equality-expression
7213
7214    exclusive-or-expression:
7215      and-expression
7216      exclusive-or-expression ^ and-expression
7217
7218    inclusive-or-expression:
7219      exclusive-or-expression
7220      inclusive-or-expression | exclusive-or-expression
7221
7222    logical-and-expression:
7223      inclusive-or-expression
7224      logical-and-expression && inclusive-or-expression
7225
7226    logical-or-expression:
7227      logical-and-expression
7228      logical-or-expression || logical-and-expression
7229
7230    All these are implemented with a single function like:
7231
7232    binary-expression:
7233      simple-cast-expression
7234      binary-expression <token> binary-expression
7235
7236    CAST_P is true if this expression is the target of a cast.
7237
7238    The binops_by_token map is used to get the tree codes for each <token> type.
7239    binary-expressions are associated according to a precedence table.  */
7240
7241 #define TOKEN_PRECEDENCE(token)                              \
7242 (((token->type == CPP_GREATER                                \
7243    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7244   && !parser->greater_than_is_operator_p)                    \
7245  ? PREC_NOT_OPERATOR                                         \
7246  : binops_by_token[token->type].prec)
7247
7248 static tree
7249 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7250                              bool no_toplevel_fold_p,
7251                              enum cp_parser_prec prec,
7252                              cp_id_kind * pidk)
7253 {
7254   cp_parser_expression_stack stack;
7255   cp_parser_expression_stack_entry *sp = &stack[0];
7256   tree lhs, rhs;
7257   cp_token *token;
7258   enum tree_code tree_type, lhs_type, rhs_type;
7259   enum cp_parser_prec new_prec, lookahead_prec;
7260   tree overload;
7261
7262   /* Parse the first expression.  */
7263   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7264   lhs_type = ERROR_MARK;
7265
7266   for (;;)
7267     {
7268       /* Get an operator token.  */
7269       token = cp_lexer_peek_token (parser->lexer);
7270
7271       if (warn_cxx0x_compat
7272           && token->type == CPP_RSHIFT
7273           && !parser->greater_than_is_operator_p)
7274         {
7275           if (warning_at (token->location, OPT_Wc__0x_compat, 
7276                           "%<>>%> operator is treated as"
7277                           " two right angle brackets in C++11"))
7278             inform (token->location,
7279                     "suggest parentheses around %<>>%> expression");
7280         }
7281
7282       new_prec = TOKEN_PRECEDENCE (token);
7283
7284       /* Popping an entry off the stack means we completed a subexpression:
7285          - either we found a token which is not an operator (`>' where it is not
7286            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7287            will happen repeatedly;
7288          - or, we found an operator which has lower priority.  This is the case
7289            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7290            parsing `3 * 4'.  */
7291       if (new_prec <= prec)
7292         {
7293           if (sp == stack)
7294             break;
7295           else
7296             goto pop;
7297         }
7298
7299      get_rhs:
7300       tree_type = binops_by_token[token->type].tree_type;
7301
7302       /* We used the operator token.  */
7303       cp_lexer_consume_token (parser->lexer);
7304
7305       /* For "false && x" or "true || x", x will never be executed;
7306          disable warnings while evaluating it.  */
7307       if (tree_type == TRUTH_ANDIF_EXPR)
7308         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7309       else if (tree_type == TRUTH_ORIF_EXPR)
7310         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7311
7312       /* Extract another operand.  It may be the RHS of this expression
7313          or the LHS of a new, higher priority expression.  */
7314       rhs = cp_parser_simple_cast_expression (parser);
7315       rhs_type = ERROR_MARK;
7316
7317       /* Get another operator token.  Look up its precedence to avoid
7318          building a useless (immediately popped) stack entry for common
7319          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7320       token = cp_lexer_peek_token (parser->lexer);
7321       lookahead_prec = TOKEN_PRECEDENCE (token);
7322       if (lookahead_prec > new_prec)
7323         {
7324           /* ... and prepare to parse the RHS of the new, higher priority
7325              expression.  Since precedence levels on the stack are
7326              monotonically increasing, we do not have to care about
7327              stack overflows.  */
7328           sp->prec = prec;
7329           sp->tree_type = tree_type;
7330           sp->lhs = lhs;
7331           sp->lhs_type = lhs_type;
7332           sp++;
7333           lhs = rhs;
7334           lhs_type = rhs_type;
7335           prec = new_prec;
7336           new_prec = lookahead_prec;
7337           goto get_rhs;
7338
7339          pop:
7340           lookahead_prec = new_prec;
7341           /* If the stack is not empty, we have parsed into LHS the right side
7342              (`4' in the example above) of an expression we had suspended.
7343              We can use the information on the stack to recover the LHS (`3')
7344              from the stack together with the tree code (`MULT_EXPR'), and
7345              the precedence of the higher level subexpression
7346              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7347              which will be used to actually build the additive expression.  */
7348           --sp;
7349           prec = sp->prec;
7350           tree_type = sp->tree_type;
7351           rhs = lhs;
7352           rhs_type = lhs_type;
7353           lhs = sp->lhs;
7354           lhs_type = sp->lhs_type;
7355         }
7356
7357       /* Undo the disabling of warnings done above.  */
7358       if (tree_type == TRUTH_ANDIF_EXPR)
7359         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7360       else if (tree_type == TRUTH_ORIF_EXPR)
7361         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7362
7363       overload = NULL;
7364       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7365          ERROR_MARK for everything that is not a binary expression.
7366          This makes warn_about_parentheses miss some warnings that
7367          involve unary operators.  For unary expressions we should
7368          pass the correct tree_code unless the unary expression was
7369          surrounded by parentheses.
7370       */
7371       if (no_toplevel_fold_p
7372           && lookahead_prec <= prec
7373           && sp == stack
7374           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7375         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7376       else
7377         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7378                                  &overload, tf_warning_or_error);
7379       lhs_type = tree_type;
7380
7381       /* If the binary operator required the use of an overloaded operator,
7382          then this expression cannot be an integral constant-expression.
7383          An overloaded operator can be used even if both operands are
7384          otherwise permissible in an integral constant-expression if at
7385          least one of the operands is of enumeration type.  */
7386
7387       if (overload
7388           && cp_parser_non_integral_constant_expression (parser,
7389                                                          NIC_OVERLOADED))
7390         return error_mark_node;
7391     }
7392
7393   return lhs;
7394 }
7395
7396
7397 /* Parse the `? expression : assignment-expression' part of a
7398    conditional-expression.  The LOGICAL_OR_EXPR is the
7399    logical-or-expression that started the conditional-expression.
7400    Returns a representation of the entire conditional-expression.
7401
7402    This routine is used by cp_parser_assignment_expression.
7403
7404      ? expression : assignment-expression
7405
7406    GNU Extensions:
7407
7408      ? : assignment-expression */
7409
7410 static tree
7411 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7412 {
7413   tree expr;
7414   tree assignment_expr;
7415   struct cp_token *token;
7416
7417   /* Consume the `?' token.  */
7418   cp_lexer_consume_token (parser->lexer);
7419   token = cp_lexer_peek_token (parser->lexer);
7420   if (cp_parser_allow_gnu_extensions_p (parser)
7421       && token->type == CPP_COLON)
7422     {
7423       pedwarn (token->location, OPT_pedantic, 
7424                "ISO C++ does not allow ?: with omitted middle operand");
7425       /* Implicit true clause.  */
7426       expr = NULL_TREE;
7427       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7428       warn_for_omitted_condop (token->location, logical_or_expr);
7429     }
7430   else
7431     {
7432       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7433       parser->colon_corrects_to_scope_p = false;
7434       /* Parse the expression.  */
7435       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7436       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7437       c_inhibit_evaluation_warnings +=
7438         ((logical_or_expr == truthvalue_true_node)
7439          - (logical_or_expr == truthvalue_false_node));
7440       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7441     }
7442
7443   /* The next token should be a `:'.  */
7444   cp_parser_require (parser, CPP_COLON, RT_COLON);
7445   /* Parse the assignment-expression.  */
7446   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7447   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7448
7449   /* Build the conditional-expression.  */
7450   return build_x_conditional_expr (logical_or_expr,
7451                                    expr,
7452                                    assignment_expr,
7453                                    tf_warning_or_error);
7454 }
7455
7456 /* Parse an assignment-expression.
7457
7458    assignment-expression:
7459      conditional-expression
7460      logical-or-expression assignment-operator assignment_expression
7461      throw-expression
7462
7463    CAST_P is true if this expression is the target of a cast.
7464
7465    Returns a representation for the expression.  */
7466
7467 static tree
7468 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7469                                  cp_id_kind * pidk)
7470 {
7471   tree expr;
7472
7473   /* If the next token is the `throw' keyword, then we're looking at
7474      a throw-expression.  */
7475   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7476     expr = cp_parser_throw_expression (parser);
7477   /* Otherwise, it must be that we are looking at a
7478      logical-or-expression.  */
7479   else
7480     {
7481       /* Parse the binary expressions (logical-or-expression).  */
7482       expr = cp_parser_binary_expression (parser, cast_p, false,
7483                                           PREC_NOT_OPERATOR, pidk);
7484       /* If the next token is a `?' then we're actually looking at a
7485          conditional-expression.  */
7486       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7487         return cp_parser_question_colon_clause (parser, expr);
7488       else
7489         {
7490           enum tree_code assignment_operator;
7491
7492           /* If it's an assignment-operator, we're using the second
7493              production.  */
7494           assignment_operator
7495             = cp_parser_assignment_operator_opt (parser);
7496           if (assignment_operator != ERROR_MARK)
7497             {
7498               bool non_constant_p;
7499
7500               /* Parse the right-hand side of the assignment.  */
7501               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7502
7503               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7504                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7505
7506               /* An assignment may not appear in a
7507                  constant-expression.  */
7508               if (cp_parser_non_integral_constant_expression (parser,
7509                                                               NIC_ASSIGNMENT))
7510                 return error_mark_node;
7511               /* Build the assignment expression.  */
7512               expr = build_x_modify_expr (expr,
7513                                           assignment_operator,
7514                                           rhs,
7515                                           tf_warning_or_error);
7516             }
7517         }
7518     }
7519
7520   return expr;
7521 }
7522
7523 /* Parse an (optional) assignment-operator.
7524
7525    assignment-operator: one of
7526      = *= /= %= += -= >>= <<= &= ^= |=
7527
7528    GNU Extension:
7529
7530    assignment-operator: one of
7531      <?= >?=
7532
7533    If the next token is an assignment operator, the corresponding tree
7534    code is returned, and the token is consumed.  For example, for
7535    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7536    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7537    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7538    operator, ERROR_MARK is returned.  */
7539
7540 static enum tree_code
7541 cp_parser_assignment_operator_opt (cp_parser* parser)
7542 {
7543   enum tree_code op;
7544   cp_token *token;
7545
7546   /* Peek at the next token.  */
7547   token = cp_lexer_peek_token (parser->lexer);
7548
7549   switch (token->type)
7550     {
7551     case CPP_EQ:
7552       op = NOP_EXPR;
7553       break;
7554
7555     case CPP_MULT_EQ:
7556       op = MULT_EXPR;
7557       break;
7558
7559     case CPP_DIV_EQ:
7560       op = TRUNC_DIV_EXPR;
7561       break;
7562
7563     case CPP_MOD_EQ:
7564       op = TRUNC_MOD_EXPR;
7565       break;
7566
7567     case CPP_PLUS_EQ:
7568       op = PLUS_EXPR;
7569       break;
7570
7571     case CPP_MINUS_EQ:
7572       op = MINUS_EXPR;
7573       break;
7574
7575     case CPP_RSHIFT_EQ:
7576       op = RSHIFT_EXPR;
7577       break;
7578
7579     case CPP_LSHIFT_EQ:
7580       op = LSHIFT_EXPR;
7581       break;
7582
7583     case CPP_AND_EQ:
7584       op = BIT_AND_EXPR;
7585       break;
7586
7587     case CPP_XOR_EQ:
7588       op = BIT_XOR_EXPR;
7589       break;
7590
7591     case CPP_OR_EQ:
7592       op = BIT_IOR_EXPR;
7593       break;
7594
7595     default:
7596       /* Nothing else is an assignment operator.  */
7597       op = ERROR_MARK;
7598     }
7599
7600   /* If it was an assignment operator, consume it.  */
7601   if (op != ERROR_MARK)
7602     cp_lexer_consume_token (parser->lexer);
7603
7604   return op;
7605 }
7606
7607 /* Parse an expression.
7608
7609    expression:
7610      assignment-expression
7611      expression , assignment-expression
7612
7613    CAST_P is true if this expression is the target of a cast.
7614
7615    Returns a representation of the expression.  */
7616
7617 static tree
7618 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7619 {
7620   tree expression = NULL_TREE;
7621
7622   while (true)
7623     {
7624       tree assignment_expression;
7625
7626       /* Parse the next assignment-expression.  */
7627       assignment_expression
7628         = cp_parser_assignment_expression (parser, cast_p, pidk);
7629       /* If this is the first assignment-expression, we can just
7630          save it away.  */
7631       if (!expression)
7632         expression = assignment_expression;
7633       else
7634         expression = build_x_compound_expr (expression,
7635                                             assignment_expression,
7636                                             tf_warning_or_error);
7637       /* If the next token is not a comma, then we are done with the
7638          expression.  */
7639       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7640         break;
7641       /* Consume the `,'.  */
7642       cp_lexer_consume_token (parser->lexer);
7643       /* A comma operator cannot appear in a constant-expression.  */
7644       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7645         expression = error_mark_node;
7646     }
7647
7648   return expression;
7649 }
7650
7651 /* Parse a constant-expression.
7652
7653    constant-expression:
7654      conditional-expression
7655
7656   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7657   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7658   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7659   is false, NON_CONSTANT_P should be NULL.  */
7660
7661 static tree
7662 cp_parser_constant_expression (cp_parser* parser,
7663                                bool allow_non_constant_p,
7664                                bool *non_constant_p)
7665 {
7666   bool saved_integral_constant_expression_p;
7667   bool saved_allow_non_integral_constant_expression_p;
7668   bool saved_non_integral_constant_expression_p;
7669   tree expression;
7670
7671   /* It might seem that we could simply parse the
7672      conditional-expression, and then check to see if it were
7673      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7674      one that the compiler can figure out is constant, possibly after
7675      doing some simplifications or optimizations.  The standard has a
7676      precise definition of constant-expression, and we must honor
7677      that, even though it is somewhat more restrictive.
7678
7679      For example:
7680
7681        int i[(2, 3)];
7682
7683      is not a legal declaration, because `(2, 3)' is not a
7684      constant-expression.  The `,' operator is forbidden in a
7685      constant-expression.  However, GCC's constant-folding machinery
7686      will fold this operation to an INTEGER_CST for `3'.  */
7687
7688   /* Save the old settings.  */
7689   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7690   saved_allow_non_integral_constant_expression_p
7691     = parser->allow_non_integral_constant_expression_p;
7692   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7693   /* We are now parsing a constant-expression.  */
7694   parser->integral_constant_expression_p = true;
7695   parser->allow_non_integral_constant_expression_p
7696     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7697   parser->non_integral_constant_expression_p = false;
7698   /* Although the grammar says "conditional-expression", we parse an
7699      "assignment-expression", which also permits "throw-expression"
7700      and the use of assignment operators.  In the case that
7701      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7702      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7703      actually essential that we look for an assignment-expression.
7704      For example, cp_parser_initializer_clauses uses this function to
7705      determine whether a particular assignment-expression is in fact
7706      constant.  */
7707   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7708   /* Restore the old settings.  */
7709   parser->integral_constant_expression_p
7710     = saved_integral_constant_expression_p;
7711   parser->allow_non_integral_constant_expression_p
7712     = saved_allow_non_integral_constant_expression_p;
7713   if (cxx_dialect >= cxx0x)
7714     {
7715       /* Require an rvalue constant expression here; that's what our
7716          callers expect.  Reference constant expressions are handled
7717          separately in e.g. cp_parser_template_argument.  */
7718       bool is_const = potential_rvalue_constant_expression (expression);
7719       parser->non_integral_constant_expression_p = !is_const;
7720       if (!is_const && !allow_non_constant_p)
7721         require_potential_rvalue_constant_expression (expression);
7722     }
7723   if (allow_non_constant_p)
7724     *non_constant_p = parser->non_integral_constant_expression_p;
7725   parser->non_integral_constant_expression_p
7726     = saved_non_integral_constant_expression_p;
7727
7728   return expression;
7729 }
7730
7731 /* Parse __builtin_offsetof.
7732
7733    offsetof-expression:
7734      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7735
7736    offsetof-member-designator:
7737      id-expression
7738      | offsetof-member-designator "." id-expression
7739      | offsetof-member-designator "[" expression "]"
7740      | offsetof-member-designator "->" id-expression  */
7741
7742 static tree
7743 cp_parser_builtin_offsetof (cp_parser *parser)
7744 {
7745   int save_ice_p, save_non_ice_p;
7746   tree type, expr;
7747   cp_id_kind dummy;
7748   cp_token *token;
7749
7750   /* We're about to accept non-integral-constant things, but will
7751      definitely yield an integral constant expression.  Save and
7752      restore these values around our local parsing.  */
7753   save_ice_p = parser->integral_constant_expression_p;
7754   save_non_ice_p = parser->non_integral_constant_expression_p;
7755
7756   /* Consume the "__builtin_offsetof" token.  */
7757   cp_lexer_consume_token (parser->lexer);
7758   /* Consume the opening `('.  */
7759   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7760   /* Parse the type-id.  */
7761   type = cp_parser_type_id (parser);
7762   /* Look for the `,'.  */
7763   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7764   token = cp_lexer_peek_token (parser->lexer);
7765
7766   /* Build the (type *)null that begins the traditional offsetof macro.  */
7767   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7768                             tf_warning_or_error);
7769
7770   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7771   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7772                                                  true, &dummy, token->location);
7773   while (true)
7774     {
7775       token = cp_lexer_peek_token (parser->lexer);
7776       switch (token->type)
7777         {
7778         case CPP_OPEN_SQUARE:
7779           /* offsetof-member-designator "[" expression "]" */
7780           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7781           break;
7782
7783         case CPP_DEREF:
7784           /* offsetof-member-designator "->" identifier */
7785           expr = grok_array_decl (expr, integer_zero_node);
7786           /* FALLTHRU */
7787
7788         case CPP_DOT:
7789           /* offsetof-member-designator "." identifier */
7790           cp_lexer_consume_token (parser->lexer);
7791           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7792                                                          expr, true, &dummy,
7793                                                          token->location);
7794           break;
7795
7796         case CPP_CLOSE_PAREN:
7797           /* Consume the ")" token.  */
7798           cp_lexer_consume_token (parser->lexer);
7799           goto success;
7800
7801         default:
7802           /* Error.  We know the following require will fail, but
7803              that gives the proper error message.  */
7804           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7805           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7806           expr = error_mark_node;
7807           goto failure;
7808         }
7809     }
7810
7811  success:
7812   /* If we're processing a template, we can't finish the semantics yet.
7813      Otherwise we can fold the entire expression now.  */
7814   if (processing_template_decl)
7815     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7816   else
7817     expr = finish_offsetof (expr);
7818
7819  failure:
7820   parser->integral_constant_expression_p = save_ice_p;
7821   parser->non_integral_constant_expression_p = save_non_ice_p;
7822
7823   return expr;
7824 }
7825
7826 /* Parse a trait expression.
7827
7828    Returns a representation of the expression, the underlying type
7829    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7830
7831 static tree
7832 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7833 {
7834   cp_trait_kind kind;
7835   tree type1, type2 = NULL_TREE;
7836   bool binary = false;
7837   cp_decl_specifier_seq decl_specs;
7838
7839   switch (keyword)
7840     {
7841     case RID_HAS_NOTHROW_ASSIGN:
7842       kind = CPTK_HAS_NOTHROW_ASSIGN;
7843       break;
7844     case RID_HAS_NOTHROW_CONSTRUCTOR:
7845       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7846       break;
7847     case RID_HAS_NOTHROW_COPY:
7848       kind = CPTK_HAS_NOTHROW_COPY;
7849       break;
7850     case RID_HAS_TRIVIAL_ASSIGN:
7851       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7852       break;
7853     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7854       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7855       break;
7856     case RID_HAS_TRIVIAL_COPY:
7857       kind = CPTK_HAS_TRIVIAL_COPY;
7858       break;
7859     case RID_HAS_TRIVIAL_DESTRUCTOR:
7860       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7861       break;
7862     case RID_HAS_VIRTUAL_DESTRUCTOR:
7863       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7864       break;
7865     case RID_IS_ABSTRACT:
7866       kind = CPTK_IS_ABSTRACT;
7867       break;
7868     case RID_IS_BASE_OF:
7869       kind = CPTK_IS_BASE_OF;
7870       binary = true;
7871       break;
7872     case RID_IS_CLASS:
7873       kind = CPTK_IS_CLASS;
7874       break;
7875     case RID_IS_CONVERTIBLE_TO:
7876       kind = CPTK_IS_CONVERTIBLE_TO;
7877       binary = true;
7878       break;
7879     case RID_IS_EMPTY:
7880       kind = CPTK_IS_EMPTY;
7881       break;
7882     case RID_IS_ENUM:
7883       kind = CPTK_IS_ENUM;
7884       break;
7885     case RID_IS_FINAL:
7886       kind = CPTK_IS_FINAL;
7887       break;
7888     case RID_IS_LITERAL_TYPE:
7889       kind = CPTK_IS_LITERAL_TYPE;
7890       break;
7891     case RID_IS_POD:
7892       kind = CPTK_IS_POD;
7893       break;
7894     case RID_IS_POLYMORPHIC:
7895       kind = CPTK_IS_POLYMORPHIC;
7896       break;
7897     case RID_IS_STD_LAYOUT:
7898       kind = CPTK_IS_STD_LAYOUT;
7899       break;
7900     case RID_IS_TRIVIAL:
7901       kind = CPTK_IS_TRIVIAL;
7902       break;
7903     case RID_IS_UNION:
7904       kind = CPTK_IS_UNION;
7905       break;
7906     case RID_UNDERLYING_TYPE:
7907       kind = CPTK_UNDERLYING_TYPE;
7908       break;
7909     case RID_BASES:
7910       kind = CPTK_BASES;
7911       break;
7912     case RID_DIRECT_BASES:
7913       kind = CPTK_DIRECT_BASES;
7914       break;
7915     default:
7916       gcc_unreachable ();
7917     }
7918
7919   /* Consume the token.  */
7920   cp_lexer_consume_token (parser->lexer);
7921
7922   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7923
7924   type1 = cp_parser_type_id (parser);
7925
7926   if (type1 == 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 = type1;
7932
7933   /* Call grokdeclarator to figure out what type this is.  */
7934   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7935                           /*initialized=*/0, /*attrlist=*/NULL);
7936
7937   if (binary)
7938     {
7939       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7940  
7941       type2 = cp_parser_type_id (parser);
7942
7943       if (type2 == error_mark_node)
7944         return error_mark_node;
7945
7946       /* Build a trivial decl-specifier-seq.  */
7947       clear_decl_specs (&decl_specs);
7948       decl_specs.type = type2;
7949
7950       /* Call grokdeclarator to figure out what type this is.  */
7951       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7952                               /*initialized=*/0, /*attrlist=*/NULL);
7953     }
7954
7955   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7956
7957   /* Complete the trait expression, which may mean either processing
7958      the trait expr now or saving it for template instantiation.  */
7959   switch(kind)
7960     {
7961     case CPTK_UNDERLYING_TYPE:
7962       return finish_underlying_type (type1);
7963     case CPTK_BASES:
7964       return finish_bases (type1, false);
7965     case CPTK_DIRECT_BASES:
7966       return finish_bases (type1, true);
7967     default:
7968       return finish_trait_expr (kind, type1, type2);
7969     }
7970 }
7971
7972 /* Lambdas that appear in variable initializer or default argument scope
7973    get that in their mangling, so we need to record it.  We might as well
7974    use the count for function and namespace scopes as well.  */
7975 static GTY(()) tree lambda_scope;
7976 static GTY(()) int lambda_count;
7977 typedef struct GTY(()) tree_int
7978 {
7979   tree t;
7980   int i;
7981 } tree_int;
7982 DEF_VEC_O(tree_int);
7983 DEF_VEC_ALLOC_O(tree_int,gc);
7984 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7985
7986 static void
7987 start_lambda_scope (tree decl)
7988 {
7989   tree_int ti;
7990   gcc_assert (decl);
7991   /* Once we're inside a function, we ignore other scopes and just push
7992      the function again so that popping works properly.  */
7993   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7994     decl = current_function_decl;
7995   ti.t = lambda_scope;
7996   ti.i = lambda_count;
7997   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7998   if (lambda_scope != decl)
7999     {
8000       /* Don't reset the count if we're still in the same function.  */
8001       lambda_scope = decl;
8002       lambda_count = 0;
8003     }
8004 }
8005
8006 static void
8007 record_lambda_scope (tree lambda)
8008 {
8009   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
8010   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
8011 }
8012
8013 static void
8014 finish_lambda_scope (void)
8015 {
8016   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8017   if (lambda_scope != p->t)
8018     {
8019       lambda_scope = p->t;
8020       lambda_count = p->i;
8021     }
8022   VEC_pop (tree_int, lambda_scope_stack);
8023 }
8024
8025 /* Parse a lambda expression.
8026
8027    lambda-expression:
8028      lambda-introducer lambda-declarator [opt] compound-statement
8029
8030    Returns a representation of the expression.  */
8031
8032 static tree
8033 cp_parser_lambda_expression (cp_parser* parser)
8034 {
8035   tree lambda_expr = build_lambda_expr ();
8036   tree type;
8037   bool ok;
8038
8039   LAMBDA_EXPR_LOCATION (lambda_expr)
8040     = cp_lexer_peek_token (parser->lexer)->location;
8041
8042   if (cp_unevaluated_operand)
8043     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8044               "lambda-expression in unevaluated context");
8045
8046   /* We may be in the middle of deferred access check.  Disable
8047      it now.  */
8048   push_deferring_access_checks (dk_no_deferred);
8049
8050   cp_parser_lambda_introducer (parser, lambda_expr);
8051
8052   type = begin_lambda_type (lambda_expr);
8053   if (type == error_mark_node)
8054     return error_mark_node;
8055
8056   record_lambda_scope (lambda_expr);
8057
8058   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8059   determine_visibility (TYPE_NAME (type));
8060
8061   /* Now that we've started the type, add the capture fields for any
8062      explicit captures.  */
8063   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8064
8065   {
8066     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8067     unsigned int saved_num_template_parameter_lists
8068         = parser->num_template_parameter_lists;
8069     unsigned char in_statement = parser->in_statement;
8070     bool in_switch_statement_p = parser->in_switch_statement_p;
8071
8072     parser->num_template_parameter_lists = 0;
8073     parser->in_statement = 0;
8074     parser->in_switch_statement_p = false;
8075
8076     /* By virtue of defining a local class, a lambda expression has access to
8077        the private variables of enclosing classes.  */
8078
8079     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8080
8081     if (ok)
8082       cp_parser_lambda_body (parser, lambda_expr);
8083     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8084       cp_parser_skip_to_end_of_block_or_statement (parser);
8085
8086     /* The capture list was built up in reverse order; fix that now.  */
8087     {
8088       tree newlist = NULL_TREE;
8089       tree elt, next;
8090
8091       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8092            elt; elt = next)
8093         {
8094           next = TREE_CHAIN (elt);
8095           TREE_CHAIN (elt) = newlist;
8096           newlist = elt;
8097         }
8098       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8099     }
8100
8101     if (ok)
8102       maybe_add_lambda_conv_op (type);
8103
8104     type = finish_struct (type, /*attributes=*/NULL_TREE);
8105
8106     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8107     parser->in_statement = in_statement;
8108     parser->in_switch_statement_p = in_switch_statement_p;
8109   }
8110
8111   pop_deferring_access_checks ();
8112
8113   /* This field is only used during parsing of the lambda.  */
8114   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8115
8116   /* This lambda shouldn't have any proxies left at this point.  */
8117   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8118   /* And now that we're done, push proxies for an enclosing lambda.  */
8119   insert_pending_capture_proxies ();
8120
8121   if (ok)
8122     return build_lambda_object (lambda_expr);
8123   else
8124     return error_mark_node;
8125 }
8126
8127 /* Parse the beginning of a lambda expression.
8128
8129    lambda-introducer:
8130      [ lambda-capture [opt] ]
8131
8132    LAMBDA_EXPR is the current representation of the lambda expression.  */
8133
8134 static void
8135 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8136 {
8137   /* Need commas after the first capture.  */
8138   bool first = true;
8139
8140   /* Eat the leading `['.  */
8141   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8142
8143   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8144   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8145       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8146     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8147   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8148     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8149
8150   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8151     {
8152       cp_lexer_consume_token (parser->lexer);
8153       first = false;
8154     }
8155
8156   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8157     {
8158       cp_token* capture_token;
8159       tree capture_id;
8160       tree capture_init_expr;
8161       cp_id_kind idk = CP_ID_KIND_NONE;
8162       bool explicit_init_p = false;
8163
8164       enum capture_kind_type
8165       {
8166         BY_COPY,
8167         BY_REFERENCE
8168       };
8169       enum capture_kind_type capture_kind = BY_COPY;
8170
8171       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8172         {
8173           error ("expected end of capture-list");
8174           return;
8175         }
8176
8177       if (first)
8178         first = false;
8179       else
8180         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8181
8182       /* Possibly capture `this'.  */
8183       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8184         {
8185           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8186           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8187             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8188                      "with by-copy capture default");
8189           cp_lexer_consume_token (parser->lexer);
8190           add_capture (lambda_expr,
8191                        /*id=*/this_identifier,
8192                        /*initializer=*/finish_this_expr(),
8193                        /*by_reference_p=*/false,
8194                        explicit_init_p);
8195           continue;
8196         }
8197
8198       /* Remember whether we want to capture as a reference or not.  */
8199       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8200         {
8201           capture_kind = BY_REFERENCE;
8202           cp_lexer_consume_token (parser->lexer);
8203         }
8204
8205       /* Get the identifier.  */
8206       capture_token = cp_lexer_peek_token (parser->lexer);
8207       capture_id = cp_parser_identifier (parser);
8208
8209       if (capture_id == error_mark_node)
8210         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8211            delimiters, but I modified this to stop on unnested ']' as well.  It
8212            was already changed to stop on unnested '}', so the
8213            "closing_parenthesis" name is no more misleading with my change.  */
8214         {
8215           cp_parser_skip_to_closing_parenthesis (parser,
8216                                                  /*recovering=*/true,
8217                                                  /*or_comma=*/true,
8218                                                  /*consume_paren=*/true);
8219           break;
8220         }
8221
8222       /* Find the initializer for this capture.  */
8223       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8224         {
8225           /* An explicit expression exists.  */
8226           cp_lexer_consume_token (parser->lexer);
8227           pedwarn (input_location, OPT_pedantic,
8228                    "ISO C++ does not allow initializers "
8229                    "in lambda expression capture lists");
8230           capture_init_expr = cp_parser_assignment_expression (parser,
8231                                                                /*cast_p=*/true,
8232                                                                &idk);
8233           explicit_init_p = true;
8234         }
8235       else
8236         {
8237           const char* error_msg;
8238
8239           /* Turn the identifier into an id-expression.  */
8240           capture_init_expr
8241             = cp_parser_lookup_name
8242                 (parser,
8243                  capture_id,
8244                  none_type,
8245                  /*is_template=*/false,
8246                  /*is_namespace=*/false,
8247                  /*check_dependency=*/true,
8248                  /*ambiguous_decls=*/NULL,
8249                  capture_token->location);
8250
8251           if (capture_init_expr == error_mark_node)
8252             {
8253               unqualified_name_lookup_error (capture_id);
8254               continue;
8255             }
8256           else if (DECL_P (capture_init_expr)
8257                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8258                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8259             {
8260               error_at (capture_token->location,
8261                         "capture of non-variable %qD ",
8262                         capture_init_expr);
8263               inform (0, "%q+#D declared here", capture_init_expr);
8264               continue;
8265             }
8266           if (TREE_CODE (capture_init_expr) == VAR_DECL
8267               && decl_storage_duration (capture_init_expr) != dk_auto)
8268             {
8269               pedwarn (capture_token->location, 0, "capture of variable "
8270                        "%qD with non-automatic storage duration",
8271                        capture_init_expr);
8272               inform (0, "%q+#D declared here", capture_init_expr);
8273               continue;
8274             }
8275
8276           capture_init_expr
8277             = finish_id_expression
8278                 (capture_id,
8279                  capture_init_expr,
8280                  parser->scope,
8281                  &idk,
8282                  /*integral_constant_expression_p=*/false,
8283                  /*allow_non_integral_constant_expression_p=*/false,
8284                  /*non_integral_constant_expression_p=*/NULL,
8285                  /*template_p=*/false,
8286                  /*done=*/true,
8287                  /*address_p=*/false,
8288                  /*template_arg_p=*/false,
8289                  &error_msg,
8290                  capture_token->location);
8291         }
8292
8293       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8294           && !explicit_init_p)
8295         {
8296           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8297               && capture_kind == BY_COPY)
8298             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8299                      "of %qD redundant with by-copy capture default",
8300                      capture_id);
8301           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8302               && capture_kind == BY_REFERENCE)
8303             pedwarn (capture_token->location, 0, "explicit by-reference "
8304                      "capture of %qD redundant with by-reference capture "
8305                      "default", capture_id);
8306         }
8307
8308       add_capture (lambda_expr,
8309                    capture_id,
8310                    capture_init_expr,
8311                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8312                    explicit_init_p);
8313     }
8314
8315   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8316 }
8317
8318 /* Parse the (optional) middle of a lambda expression.
8319
8320    lambda-declarator:
8321      ( parameter-declaration-clause [opt] )
8322        attribute-specifier [opt]
8323        mutable [opt]
8324        exception-specification [opt]
8325        lambda-return-type-clause [opt]
8326
8327    LAMBDA_EXPR is the current representation of the lambda expression.  */
8328
8329 static bool
8330 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8331 {
8332   /* 5.1.1.4 of the standard says:
8333        If a lambda-expression does not include a lambda-declarator, it is as if
8334        the lambda-declarator were ().
8335      This means an empty parameter list, no attributes, and no exception
8336      specification.  */
8337   tree param_list = void_list_node;
8338   tree attributes = NULL_TREE;
8339   tree exception_spec = NULL_TREE;
8340   tree t;
8341
8342   /* The lambda-declarator is optional, but must begin with an opening
8343      parenthesis if present.  */
8344   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8345     {
8346       cp_lexer_consume_token (parser->lexer);
8347
8348       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8349
8350       /* Parse parameters.  */
8351       param_list = cp_parser_parameter_declaration_clause (parser);
8352
8353       /* Default arguments shall not be specified in the
8354          parameter-declaration-clause of a lambda-declarator.  */
8355       for (t = param_list; t; t = TREE_CHAIN (t))
8356         if (TREE_PURPOSE (t))
8357           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8358                    "default argument specified for lambda parameter");
8359
8360       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8361
8362       attributes = cp_parser_attributes_opt (parser);
8363
8364       /* Parse optional `mutable' keyword.  */
8365       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8366         {
8367           cp_lexer_consume_token (parser->lexer);
8368           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8369         }
8370
8371       /* Parse optional exception specification.  */
8372       exception_spec = cp_parser_exception_specification_opt (parser);
8373
8374       /* Parse optional trailing return type.  */
8375       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8376         {
8377           cp_lexer_consume_token (parser->lexer);
8378           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8379         }
8380
8381       /* The function parameters must be in scope all the way until after the
8382          trailing-return-type in case of decltype.  */
8383       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8384         pop_binding (DECL_NAME (t), t);
8385
8386       leave_scope ();
8387     }
8388
8389   /* Create the function call operator.
8390
8391      Messing with declarators like this is no uglier than building up the
8392      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8393      other code.  */
8394   {
8395     cp_decl_specifier_seq return_type_specs;
8396     cp_declarator* declarator;
8397     tree fco;
8398     int quals;
8399     void *p;
8400
8401     clear_decl_specs (&return_type_specs);
8402     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8403       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8404     else
8405       /* Maybe we will deduce the return type later, but we can use void
8406          as a placeholder return type anyways.  */
8407       return_type_specs.type = void_type_node;
8408
8409     p = obstack_alloc (&declarator_obstack, 0);
8410
8411     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8412                                      sfk_none);
8413
8414     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8415              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8416     declarator = make_call_declarator (declarator, param_list, quals,
8417                                        VIRT_SPEC_UNSPECIFIED,
8418                                        exception_spec,
8419                                        /*late_return_type=*/NULL_TREE);
8420     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8421
8422     fco = grokmethod (&return_type_specs,
8423                       declarator,
8424                       attributes);
8425     if (fco != error_mark_node)
8426       {
8427         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8428         DECL_ARTIFICIAL (fco) = 1;
8429         /* Give the object parameter a different name.  */
8430         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8431       }
8432
8433     finish_member_declaration (fco);
8434
8435     obstack_free (&declarator_obstack, p);
8436
8437     return (fco != error_mark_node);
8438   }
8439 }
8440
8441 /* Parse the body of a lambda expression, which is simply
8442
8443    compound-statement
8444
8445    but which requires special handling.
8446    LAMBDA_EXPR is the current representation of the lambda expression.  */
8447
8448 static void
8449 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8450 {
8451   bool nested = (current_function_decl != NULL_TREE);
8452   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8453   if (nested)
8454     push_function_context ();
8455   else
8456     /* Still increment function_depth so that we don't GC in the
8457        middle of an expression.  */
8458     ++function_depth;
8459   /* Clear this in case we're in the middle of a default argument.  */
8460   parser->local_variables_forbidden_p = false;
8461
8462   /* Finish the function call operator
8463      - class_specifier
8464      + late_parsing_for_member
8465      + function_definition_after_declarator
8466      + ctor_initializer_opt_and_function_body  */
8467   {
8468     tree fco = lambda_function (lambda_expr);
8469     tree body;
8470     bool done = false;
8471     tree compound_stmt;
8472     tree cap;
8473
8474     /* Let the front end know that we are going to be defining this
8475        function.  */
8476     start_preparsed_function (fco,
8477                               NULL_TREE,
8478                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8479
8480     start_lambda_scope (fco);
8481     body = begin_function_body ();
8482
8483     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8484       goto out;
8485
8486     /* Push the proxies for any explicit captures.  */
8487     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8488          cap = TREE_CHAIN (cap))
8489       build_capture_proxy (TREE_PURPOSE (cap));
8490
8491     compound_stmt = begin_compound_stmt (0);
8492
8493     /* 5.1.1.4 of the standard says:
8494          If a lambda-expression does not include a trailing-return-type, it
8495          is as if the trailing-return-type denotes the following type:
8496           * if the compound-statement is of the form
8497                { return attribute-specifier [opt] expression ; }
8498              the type of the returned expression after lvalue-to-rvalue
8499              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8500              (_conv.array_ 4.2), and function-to-pointer conversion
8501              (_conv.func_ 4.3);
8502           * otherwise, void.  */
8503
8504     /* In a lambda that has neither a lambda-return-type-clause
8505        nor a deducible form, errors should be reported for return statements
8506        in the body.  Since we used void as the placeholder return type, parsing
8507        the body as usual will give such desired behavior.  */
8508     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8509         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8510         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8511       {
8512         tree expr = NULL_TREE;
8513         cp_id_kind idk = CP_ID_KIND_NONE;
8514
8515         /* Parse tentatively in case there's more after the initial return
8516            statement.  */
8517         cp_parser_parse_tentatively (parser);
8518
8519         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8520
8521         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8522
8523         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8524         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8525
8526         if (cp_parser_parse_definitely (parser))
8527           {
8528             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8529
8530             /* Will get error here if type not deduced yet.  */
8531             finish_return_stmt (expr);
8532
8533             done = true;
8534           }
8535       }
8536
8537     if (!done)
8538       {
8539         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8540           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8541         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8542           cp_parser_label_declaration (parser);
8543         cp_parser_statement_seq_opt (parser, NULL_TREE);
8544         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8545         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8546       }
8547
8548     finish_compound_stmt (compound_stmt);
8549
8550   out:
8551     finish_function_body (body);
8552     finish_lambda_scope ();
8553
8554     /* Finish the function and generate code for it if necessary.  */
8555     expand_or_defer_fn (finish_function (/*inline*/2));
8556   }
8557
8558   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8559   if (nested)
8560     pop_function_context();
8561   else
8562     --function_depth;
8563 }
8564
8565 /* Statements [gram.stmt.stmt]  */
8566
8567 /* Parse a statement.
8568
8569    statement:
8570      labeled-statement
8571      expression-statement
8572      compound-statement
8573      selection-statement
8574      iteration-statement
8575      jump-statement
8576      declaration-statement
8577      try-block
8578
8579   TM Extension:
8580
8581    statement:
8582      atomic-statement
8583
8584   IN_COMPOUND is true when the statement is nested inside a
8585   cp_parser_compound_statement; this matters for certain pragmas.
8586
8587   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8588   is a (possibly labeled) if statement which is not enclosed in braces
8589   and has an else clause.  This is used to implement -Wparentheses.  */
8590
8591 static void
8592 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8593                      bool in_compound, bool *if_p)
8594 {
8595   tree statement;
8596   cp_token *token;
8597   location_t statement_location;
8598
8599  restart:
8600   if (if_p != NULL)
8601     *if_p = false;
8602   /* There is no statement yet.  */
8603   statement = NULL_TREE;
8604   /* Peek at the next token.  */
8605   token = cp_lexer_peek_token (parser->lexer);
8606   /* Remember the location of the first token in the statement.  */
8607   statement_location = token->location;
8608   /* If this is a keyword, then that will often determine what kind of
8609      statement we have.  */
8610   if (token->type == CPP_KEYWORD)
8611     {
8612       enum rid keyword = token->keyword;
8613
8614       switch (keyword)
8615         {
8616         case RID_CASE:
8617         case RID_DEFAULT:
8618           /* Looks like a labeled-statement with a case label.
8619              Parse the label, and then use tail recursion to parse
8620              the statement.  */
8621           cp_parser_label_for_labeled_statement (parser);
8622           goto restart;
8623
8624         case RID_IF:
8625         case RID_SWITCH:
8626           statement = cp_parser_selection_statement (parser, if_p);
8627           break;
8628
8629         case RID_WHILE:
8630         case RID_DO:
8631         case RID_FOR:
8632           statement = cp_parser_iteration_statement (parser);
8633           break;
8634
8635         case RID_BREAK:
8636         case RID_CONTINUE:
8637         case RID_RETURN:
8638         case RID_GOTO:
8639           statement = cp_parser_jump_statement (parser);
8640           break;
8641
8642           /* Objective-C++ exception-handling constructs.  */
8643         case RID_AT_TRY:
8644         case RID_AT_CATCH:
8645         case RID_AT_FINALLY:
8646         case RID_AT_SYNCHRONIZED:
8647         case RID_AT_THROW:
8648           statement = cp_parser_objc_statement (parser);
8649           break;
8650
8651         case RID_TRY:
8652           statement = cp_parser_try_block (parser);
8653           break;
8654
8655         case RID_NAMESPACE:
8656           /* This must be a namespace alias definition.  */
8657           cp_parser_declaration_statement (parser);
8658           return;
8659           
8660         case RID_TRANSACTION_ATOMIC:
8661         case RID_TRANSACTION_RELAXED:
8662           statement = cp_parser_transaction (parser, keyword);
8663           break;
8664         case RID_TRANSACTION_CANCEL:
8665           statement = cp_parser_transaction_cancel (parser);
8666           break;
8667
8668         default:
8669           /* It might be a keyword like `int' that can start a
8670              declaration-statement.  */
8671           break;
8672         }
8673     }
8674   else if (token->type == CPP_NAME)
8675     {
8676       /* If the next token is a `:', then we are looking at a
8677          labeled-statement.  */
8678       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8679       if (token->type == CPP_COLON)
8680         {
8681           /* Looks like a labeled-statement with an ordinary label.
8682              Parse the label, and then use tail recursion to parse
8683              the statement.  */
8684           cp_parser_label_for_labeled_statement (parser);
8685           goto restart;
8686         }
8687     }
8688   /* Anything that starts with a `{' must be a compound-statement.  */
8689   else if (token->type == CPP_OPEN_BRACE)
8690     statement = cp_parser_compound_statement (parser, NULL, false, false);
8691   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8692      a statement all its own.  */
8693   else if (token->type == CPP_PRAGMA)
8694     {
8695       /* Only certain OpenMP pragmas are attached to statements, and thus
8696          are considered statements themselves.  All others are not.  In
8697          the context of a compound, accept the pragma as a "statement" and
8698          return so that we can check for a close brace.  Otherwise we
8699          require a real statement and must go back and read one.  */
8700       if (in_compound)
8701         cp_parser_pragma (parser, pragma_compound);
8702       else if (!cp_parser_pragma (parser, pragma_stmt))
8703         goto restart;
8704       return;
8705     }
8706   else if (token->type == CPP_EOF)
8707     {
8708       cp_parser_error (parser, "expected statement");
8709       return;
8710     }
8711
8712   /* Everything else must be a declaration-statement or an
8713      expression-statement.  Try for the declaration-statement
8714      first, unless we are looking at a `;', in which case we know that
8715      we have an expression-statement.  */
8716   if (!statement)
8717     {
8718       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8719         {
8720           cp_parser_parse_tentatively (parser);
8721           /* Try to parse the declaration-statement.  */
8722           cp_parser_declaration_statement (parser);
8723           /* If that worked, we're done.  */
8724           if (cp_parser_parse_definitely (parser))
8725             return;
8726         }
8727       /* Look for an expression-statement instead.  */
8728       statement = cp_parser_expression_statement (parser, in_statement_expr);
8729     }
8730
8731   /* Set the line number for the statement.  */
8732   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8733     SET_EXPR_LOCATION (statement, statement_location);
8734 }
8735
8736 /* Parse the label for a labeled-statement, i.e.
8737
8738    identifier :
8739    case constant-expression :
8740    default :
8741
8742    GNU Extension:
8743    case constant-expression ... constant-expression : statement
8744
8745    When a label is parsed without errors, the label is added to the
8746    parse tree by the finish_* functions, so this function doesn't
8747    have to return the label.  */
8748
8749 static void
8750 cp_parser_label_for_labeled_statement (cp_parser* parser)
8751 {
8752   cp_token *token;
8753   tree label = NULL_TREE;
8754   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8755
8756   /* The next token should be an identifier.  */
8757   token = cp_lexer_peek_token (parser->lexer);
8758   if (token->type != CPP_NAME
8759       && token->type != CPP_KEYWORD)
8760     {
8761       cp_parser_error (parser, "expected labeled-statement");
8762       return;
8763     }
8764
8765   parser->colon_corrects_to_scope_p = false;
8766   switch (token->keyword)
8767     {
8768     case RID_CASE:
8769       {
8770         tree expr, expr_hi;
8771         cp_token *ellipsis;
8772
8773         /* Consume the `case' token.  */
8774         cp_lexer_consume_token (parser->lexer);
8775         /* Parse the constant-expression.  */
8776         expr = cp_parser_constant_expression (parser,
8777                                               /*allow_non_constant_p=*/false,
8778                                               NULL);
8779
8780         ellipsis = cp_lexer_peek_token (parser->lexer);
8781         if (ellipsis->type == CPP_ELLIPSIS)
8782           {
8783             /* Consume the `...' token.  */
8784             cp_lexer_consume_token (parser->lexer);
8785             expr_hi =
8786               cp_parser_constant_expression (parser,
8787                                              /*allow_non_constant_p=*/false,
8788                                              NULL);
8789             /* We don't need to emit warnings here, as the common code
8790                will do this for us.  */
8791           }
8792         else
8793           expr_hi = NULL_TREE;
8794
8795         if (parser->in_switch_statement_p)
8796           finish_case_label (token->location, expr, expr_hi);
8797         else
8798           error_at (token->location,
8799                     "case label %qE not within a switch statement",
8800                     expr);
8801       }
8802       break;
8803
8804     case RID_DEFAULT:
8805       /* Consume the `default' token.  */
8806       cp_lexer_consume_token (parser->lexer);
8807
8808       if (parser->in_switch_statement_p)
8809         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8810       else
8811         error_at (token->location, "case label not within a switch statement");
8812       break;
8813
8814     default:
8815       /* Anything else must be an ordinary label.  */
8816       label = finish_label_stmt (cp_parser_identifier (parser));
8817       break;
8818     }
8819
8820   /* Require the `:' token.  */
8821   cp_parser_require (parser, CPP_COLON, RT_COLON);
8822
8823   /* An ordinary label may optionally be followed by attributes.
8824      However, this is only permitted if the attributes are then
8825      followed by a semicolon.  This is because, for backward
8826      compatibility, when parsing
8827        lab: __attribute__ ((unused)) int i;
8828      we want the attribute to attach to "i", not "lab".  */
8829   if (label != NULL_TREE
8830       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8831     {
8832       tree attrs;
8833
8834       cp_parser_parse_tentatively (parser);
8835       attrs = cp_parser_attributes_opt (parser);
8836       if (attrs == NULL_TREE
8837           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8838         cp_parser_abort_tentative_parse (parser);
8839       else if (!cp_parser_parse_definitely (parser))
8840         ;
8841       else
8842         cplus_decl_attributes (&label, attrs, 0);
8843     }
8844
8845   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8846 }
8847
8848 /* Parse an expression-statement.
8849
8850    expression-statement:
8851      expression [opt] ;
8852
8853    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8854    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8855    indicates whether this expression-statement is part of an
8856    expression statement.  */
8857
8858 static tree
8859 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8860 {
8861   tree statement = NULL_TREE;
8862   cp_token *token = cp_lexer_peek_token (parser->lexer);
8863
8864   /* If the next token is a ';', then there is no expression
8865      statement.  */
8866   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8867     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8868
8869   /* Give a helpful message for "A<T>::type t;" and the like.  */
8870   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8871       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8872     {
8873       if (TREE_CODE (statement) == SCOPE_REF)
8874         error_at (token->location, "need %<typename%> before %qE because "
8875                   "%qT is a dependent scope",
8876                   statement, TREE_OPERAND (statement, 0));
8877       else if (is_overloaded_fn (statement)
8878                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8879         {
8880           /* A::A a; */
8881           tree fn = get_first_fn (statement);
8882           error_at (token->location,
8883                     "%<%T::%D%> names the constructor, not the type",
8884                     DECL_CONTEXT (fn), DECL_NAME (fn));
8885         }
8886     }
8887
8888   /* Consume the final `;'.  */
8889   cp_parser_consume_semicolon_at_end_of_statement (parser);
8890
8891   if (in_statement_expr
8892       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8893     /* This is the final expression statement of a statement
8894        expression.  */
8895     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8896   else if (statement)
8897     statement = finish_expr_stmt (statement);
8898   else
8899     finish_stmt ();
8900
8901   return statement;
8902 }
8903
8904 /* Parse a compound-statement.
8905
8906    compound-statement:
8907      { statement-seq [opt] }
8908
8909    GNU extension:
8910
8911    compound-statement:
8912      { label-declaration-seq [opt] statement-seq [opt] }
8913
8914    label-declaration-seq:
8915      label-declaration
8916      label-declaration-seq label-declaration
8917
8918    Returns a tree representing the statement.  */
8919
8920 static tree
8921 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8922                               bool in_try, bool function_body)
8923 {
8924   tree compound_stmt;
8925
8926   /* Consume the `{'.  */
8927   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8928     return error_mark_node;
8929   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8930       && !function_body)
8931     pedwarn (input_location, OPT_pedantic,
8932              "compound-statement in constexpr function");
8933   /* Begin the compound-statement.  */
8934   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8935   /* If the next keyword is `__label__' we have a label declaration.  */
8936   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8937     cp_parser_label_declaration (parser);
8938   /* Parse an (optional) statement-seq.  */
8939   cp_parser_statement_seq_opt (parser, in_statement_expr);
8940   /* Finish the compound-statement.  */
8941   finish_compound_stmt (compound_stmt);
8942   /* Consume the `}'.  */
8943   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8944
8945   return compound_stmt;
8946 }
8947
8948 /* Parse an (optional) statement-seq.
8949
8950    statement-seq:
8951      statement
8952      statement-seq [opt] statement  */
8953
8954 static void
8955 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8956 {
8957   /* Scan statements until there aren't any more.  */
8958   while (true)
8959     {
8960       cp_token *token = cp_lexer_peek_token (parser->lexer);
8961
8962       /* If we are looking at a `}', then we have run out of
8963          statements; the same is true if we have reached the end
8964          of file, or have stumbled upon a stray '@end'.  */
8965       if (token->type == CPP_CLOSE_BRACE
8966           || token->type == CPP_EOF
8967           || token->type == CPP_PRAGMA_EOL
8968           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8969         break;
8970       
8971       /* If we are in a compound statement and find 'else' then
8972          something went wrong.  */
8973       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8974         {
8975           if (parser->in_statement & IN_IF_STMT) 
8976             break;
8977           else
8978             {
8979               token = cp_lexer_consume_token (parser->lexer);
8980               error_at (token->location, "%<else%> without a previous %<if%>");
8981             }
8982         }
8983
8984       /* Parse the statement.  */
8985       cp_parser_statement (parser, in_statement_expr, true, NULL);
8986     }
8987 }
8988
8989 /* Parse a selection-statement.
8990
8991    selection-statement:
8992      if ( condition ) statement
8993      if ( condition ) statement else statement
8994      switch ( condition ) statement
8995
8996    Returns the new IF_STMT or SWITCH_STMT.
8997
8998    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8999    is a (possibly labeled) if statement which is not enclosed in
9000    braces and has an else clause.  This is used to implement
9001    -Wparentheses.  */
9002
9003 static tree
9004 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
9005 {
9006   cp_token *token;
9007   enum rid keyword;
9008
9009   if (if_p != NULL)
9010     *if_p = false;
9011
9012   /* Peek at the next token.  */
9013   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9014
9015   /* See what kind of keyword it is.  */
9016   keyword = token->keyword;
9017   switch (keyword)
9018     {
9019     case RID_IF:
9020     case RID_SWITCH:
9021       {
9022         tree statement;
9023         tree condition;
9024
9025         /* Look for the `('.  */
9026         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9027           {
9028             cp_parser_skip_to_end_of_statement (parser);
9029             return error_mark_node;
9030           }
9031
9032         /* Begin the selection-statement.  */
9033         if (keyword == RID_IF)
9034           statement = begin_if_stmt ();
9035         else
9036           statement = begin_switch_stmt ();
9037
9038         /* Parse the condition.  */
9039         condition = cp_parser_condition (parser);
9040         /* Look for the `)'.  */
9041         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9042           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9043                                                  /*consume_paren=*/true);
9044
9045         if (keyword == RID_IF)
9046           {
9047             bool nested_if;
9048             unsigned char in_statement;
9049
9050             /* Add the condition.  */
9051             finish_if_stmt_cond (condition, statement);
9052
9053             /* Parse the then-clause.  */
9054             in_statement = parser->in_statement;
9055             parser->in_statement |= IN_IF_STMT;
9056             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9057               {
9058                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9059                 add_stmt (build_empty_stmt (loc));
9060                 cp_lexer_consume_token (parser->lexer);
9061                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9062                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9063                               "empty body in an %<if%> statement");
9064                 nested_if = false;
9065               }
9066             else
9067               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9068             parser->in_statement = in_statement;
9069
9070             finish_then_clause (statement);
9071
9072             /* If the next token is `else', parse the else-clause.  */
9073             if (cp_lexer_next_token_is_keyword (parser->lexer,
9074                                                 RID_ELSE))
9075               {
9076                 /* Consume the `else' keyword.  */
9077                 cp_lexer_consume_token (parser->lexer);
9078                 begin_else_clause (statement);
9079                 /* Parse the else-clause.  */
9080                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9081                   {
9082                     location_t loc;
9083                     loc = cp_lexer_peek_token (parser->lexer)->location;
9084                     warning_at (loc,
9085                                 OPT_Wempty_body, "suggest braces around "
9086                                 "empty body in an %<else%> statement");
9087                     add_stmt (build_empty_stmt (loc));
9088                     cp_lexer_consume_token (parser->lexer);
9089                   }
9090                 else
9091                   cp_parser_implicitly_scoped_statement (parser, NULL);
9092
9093                 finish_else_clause (statement);
9094
9095                 /* If we are currently parsing a then-clause, then
9096                    IF_P will not be NULL.  We set it to true to
9097                    indicate that this if statement has an else clause.
9098                    This may trigger the Wparentheses warning below
9099                    when we get back up to the parent if statement.  */
9100                 if (if_p != NULL)
9101                   *if_p = true;
9102               }
9103             else
9104               {
9105                 /* This if statement does not have an else clause.  If
9106                    NESTED_IF is true, then the then-clause is an if
9107                    statement which does have an else clause.  We warn
9108                    about the potential ambiguity.  */
9109                 if (nested_if)
9110                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9111                               "suggest explicit braces to avoid ambiguous"
9112                               " %<else%>");
9113               }
9114
9115             /* Now we're all done with the if-statement.  */
9116             finish_if_stmt (statement);
9117           }
9118         else
9119           {
9120             bool in_switch_statement_p;
9121             unsigned char in_statement;
9122
9123             /* Add the condition.  */
9124             finish_switch_cond (condition, statement);
9125
9126             /* Parse the body of the switch-statement.  */
9127             in_switch_statement_p = parser->in_switch_statement_p;
9128             in_statement = parser->in_statement;
9129             parser->in_switch_statement_p = true;
9130             parser->in_statement |= IN_SWITCH_STMT;
9131             cp_parser_implicitly_scoped_statement (parser, NULL);
9132             parser->in_switch_statement_p = in_switch_statement_p;
9133             parser->in_statement = in_statement;
9134
9135             /* Now we're all done with the switch-statement.  */
9136             finish_switch_stmt (statement);
9137           }
9138
9139         return statement;
9140       }
9141       break;
9142
9143     default:
9144       cp_parser_error (parser, "expected selection-statement");
9145       return error_mark_node;
9146     }
9147 }
9148
9149 /* Parse a condition.
9150
9151    condition:
9152      expression
9153      type-specifier-seq declarator = initializer-clause
9154      type-specifier-seq declarator braced-init-list
9155
9156    GNU Extension:
9157
9158    condition:
9159      type-specifier-seq declarator asm-specification [opt]
9160        attributes [opt] = assignment-expression
9161
9162    Returns the expression that should be tested.  */
9163
9164 static tree
9165 cp_parser_condition (cp_parser* parser)
9166 {
9167   cp_decl_specifier_seq type_specifiers;
9168   const char *saved_message;
9169   int declares_class_or_enum;
9170
9171   /* Try the declaration first.  */
9172   cp_parser_parse_tentatively (parser);
9173   /* New types are not allowed in the type-specifier-seq for a
9174      condition.  */
9175   saved_message = parser->type_definition_forbidden_message;
9176   parser->type_definition_forbidden_message
9177     = G_("types may not be defined in conditions");
9178   /* Parse the type-specifier-seq.  */
9179   cp_parser_decl_specifier_seq (parser,
9180                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9181                                 &type_specifiers,
9182                                 &declares_class_or_enum);
9183   /* Restore the saved message.  */
9184   parser->type_definition_forbidden_message = saved_message;
9185   /* If all is well, we might be looking at a declaration.  */
9186   if (!cp_parser_error_occurred (parser))
9187     {
9188       tree decl;
9189       tree asm_specification;
9190       tree attributes;
9191       cp_declarator *declarator;
9192       tree initializer = NULL_TREE;
9193
9194       /* Parse the declarator.  */
9195       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9196                                          /*ctor_dtor_or_conv_p=*/NULL,
9197                                          /*parenthesized_p=*/NULL,
9198                                          /*member_p=*/false);
9199       /* Parse the attributes.  */
9200       attributes = cp_parser_attributes_opt (parser);
9201       /* Parse the asm-specification.  */
9202       asm_specification = cp_parser_asm_specification_opt (parser);
9203       /* If the next token is not an `=' or '{', then we might still be
9204          looking at an expression.  For example:
9205
9206            if (A(a).x)
9207
9208          looks like a decl-specifier-seq and a declarator -- but then
9209          there is no `=', so this is an expression.  */
9210       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9211           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9212         cp_parser_simulate_error (parser);
9213         
9214       /* If we did see an `=' or '{', then we are looking at a declaration
9215          for sure.  */
9216       if (cp_parser_parse_definitely (parser))
9217         {
9218           tree pushed_scope;
9219           bool non_constant_p;
9220           bool flags = LOOKUP_ONLYCONVERTING;
9221
9222           /* Create the declaration.  */
9223           decl = start_decl (declarator, &type_specifiers,
9224                              /*initialized_p=*/true,
9225                              attributes, /*prefix_attributes=*/NULL_TREE,
9226                              &pushed_scope);
9227
9228           /* Parse the initializer.  */
9229           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9230             {
9231               initializer = cp_parser_braced_list (parser, &non_constant_p);
9232               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9233               flags = 0;
9234             }
9235           else
9236             {
9237               /* Consume the `='.  */
9238               cp_parser_require (parser, CPP_EQ, RT_EQ);
9239               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9240             }
9241           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9242             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9243
9244           /* Process the initializer.  */
9245           cp_finish_decl (decl,
9246                           initializer, !non_constant_p,
9247                           asm_specification,
9248                           flags);
9249
9250           if (pushed_scope)
9251             pop_scope (pushed_scope);
9252
9253           return convert_from_reference (decl);
9254         }
9255     }
9256   /* If we didn't even get past the declarator successfully, we are
9257      definitely not looking at a declaration.  */
9258   else
9259     cp_parser_abort_tentative_parse (parser);
9260
9261   /* Otherwise, we are looking at an expression.  */
9262   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9263 }
9264
9265 /* Parses a for-statement or range-for-statement until the closing ')',
9266    not included. */
9267
9268 static tree
9269 cp_parser_for (cp_parser *parser)
9270 {
9271   tree init, scope, decl;
9272   bool is_range_for;
9273
9274   /* Begin the for-statement.  */
9275   scope = begin_for_scope (&init);
9276
9277   /* Parse the initialization.  */
9278   is_range_for = cp_parser_for_init_statement (parser, &decl);
9279
9280   if (is_range_for)
9281     return cp_parser_range_for (parser, scope, init, decl);
9282   else
9283     return cp_parser_c_for (parser, scope, init);
9284 }
9285
9286 static tree
9287 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9288 {
9289   /* Normal for loop */
9290   tree condition = NULL_TREE;
9291   tree expression = NULL_TREE;
9292   tree stmt;
9293
9294   stmt = begin_for_stmt (scope, init);
9295   /* The for-init-statement has already been parsed in
9296      cp_parser_for_init_statement, so no work is needed here.  */
9297   finish_for_init_stmt (stmt);
9298
9299   /* If there's a condition, process it.  */
9300   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9301     condition = cp_parser_condition (parser);
9302   finish_for_cond (condition, stmt);
9303   /* Look for the `;'.  */
9304   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9305
9306   /* If there's an expression, process it.  */
9307   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9308     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9309   finish_for_expr (expression, stmt);
9310
9311   return stmt;
9312 }
9313
9314 /* Tries to parse a range-based for-statement:
9315
9316   range-based-for:
9317     decl-specifier-seq declarator : expression
9318
9319   The decl-specifier-seq declarator and the `:' are already parsed by
9320   cp_parser_for_init_statement. If processing_template_decl it returns a
9321   newly created RANGE_FOR_STMT; if not, it is converted to a
9322   regular FOR_STMT.  */
9323
9324 static tree
9325 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9326 {
9327   tree stmt, range_expr;
9328
9329   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9330     {
9331       bool expr_non_constant_p;
9332       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9333     }
9334   else
9335     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9336
9337   /* If in template, STMT is converted to a normal for-statement
9338      at instantiation. If not, it is done just ahead. */
9339   if (processing_template_decl)
9340     {
9341       if (check_for_bare_parameter_packs (range_expr))
9342         range_expr = error_mark_node;
9343       stmt = begin_range_for_stmt (scope, init);
9344       finish_range_for_decl (stmt, range_decl, range_expr);
9345       if (!type_dependent_expression_p (range_expr)
9346           /* do_auto_deduction doesn't mess with template init-lists.  */
9347           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9348         do_range_for_auto_deduction (range_decl, range_expr);
9349     }
9350   else
9351     {
9352       stmt = begin_for_stmt (scope, init);
9353       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9354     }
9355   return stmt;
9356 }
9357
9358 /* Subroutine of cp_convert_range_for: given the initializer expression,
9359    builds up the range temporary.  */
9360
9361 static tree
9362 build_range_temp (tree range_expr)
9363 {
9364   tree range_type, range_temp;
9365
9366   /* Find out the type deduced by the declaration
9367      `auto &&__range = range_expr'.  */
9368   range_type = cp_build_reference_type (make_auto (), true);
9369   range_type = do_auto_deduction (range_type, range_expr,
9370                                   type_uses_auto (range_type));
9371
9372   /* Create the __range variable.  */
9373   range_temp = build_decl (input_location, VAR_DECL,
9374                            get_identifier ("__for_range"), range_type);
9375   TREE_USED (range_temp) = 1;
9376   DECL_ARTIFICIAL (range_temp) = 1;
9377
9378   return range_temp;
9379 }
9380
9381 /* Used by cp_parser_range_for in template context: we aren't going to
9382    do a full conversion yet, but we still need to resolve auto in the
9383    type of the for-range-declaration if present.  This is basically
9384    a shortcut version of cp_convert_range_for.  */
9385
9386 static void
9387 do_range_for_auto_deduction (tree decl, tree range_expr)
9388 {
9389   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9390   if (auto_node)
9391     {
9392       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9393       range_temp = convert_from_reference (build_range_temp (range_expr));
9394       iter_type = (cp_parser_perform_range_for_lookup
9395                    (range_temp, &begin_dummy, &end_dummy));
9396       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9397       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9398                                         tf_warning_or_error);
9399       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9400                                             iter_decl, auto_node);
9401     }
9402 }
9403
9404 /* Converts a range-based for-statement into a normal
9405    for-statement, as per the definition.
9406
9407       for (RANGE_DECL : RANGE_EXPR)
9408         BLOCK
9409
9410    should be equivalent to:
9411
9412       {
9413         auto &&__range = RANGE_EXPR;
9414         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9415               __begin != __end;
9416               ++__begin)
9417           {
9418               RANGE_DECL = *__begin;
9419               BLOCK
9420           }
9421       }
9422
9423    If RANGE_EXPR is an array:
9424         BEGIN_EXPR = __range
9425         END_EXPR = __range + ARRAY_SIZE(__range)
9426    Else if RANGE_EXPR has a member 'begin' or 'end':
9427         BEGIN_EXPR = __range.begin()
9428         END_EXPR = __range.end()
9429    Else:
9430         BEGIN_EXPR = begin(__range)
9431         END_EXPR = end(__range);
9432
9433    If __range has a member 'begin' but not 'end', or vice versa, we must
9434    still use the second alternative (it will surely fail, however).
9435    When calling begin()/end() in the third alternative we must use
9436    argument dependent lookup, but always considering 'std' as an associated
9437    namespace.  */
9438
9439 tree
9440 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9441 {
9442   tree begin, end;
9443   tree iter_type, begin_expr, end_expr;
9444   tree condition, expression;
9445
9446   if (range_decl == error_mark_node || range_expr == error_mark_node)
9447     /* If an error happened previously do nothing or else a lot of
9448        unhelpful errors would be issued.  */
9449     begin_expr = end_expr = iter_type = error_mark_node;
9450   else
9451     {
9452       tree range_temp = build_range_temp (range_expr);
9453       pushdecl (range_temp);
9454       cp_finish_decl (range_temp, range_expr,
9455                       /*is_constant_init*/false, NULL_TREE,
9456                       LOOKUP_ONLYCONVERTING);
9457
9458       range_temp = convert_from_reference (range_temp);
9459       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9460                                                       &begin_expr, &end_expr);
9461     }
9462
9463   /* The new for initialization statement.  */
9464   begin = build_decl (input_location, VAR_DECL,
9465                       get_identifier ("__for_begin"), iter_type);
9466   TREE_USED (begin) = 1;
9467   DECL_ARTIFICIAL (begin) = 1;
9468   pushdecl (begin);
9469   cp_finish_decl (begin, begin_expr,
9470                   /*is_constant_init*/false, NULL_TREE,
9471                   LOOKUP_ONLYCONVERTING);
9472
9473   end = build_decl (input_location, VAR_DECL,
9474                     get_identifier ("__for_end"), iter_type);
9475   TREE_USED (end) = 1;
9476   DECL_ARTIFICIAL (end) = 1;
9477   pushdecl (end);
9478   cp_finish_decl (end, end_expr,
9479                   /*is_constant_init*/false, NULL_TREE,
9480                   LOOKUP_ONLYCONVERTING);
9481
9482   finish_for_init_stmt (statement);
9483
9484   /* The new for condition.  */
9485   condition = build_x_binary_op (NE_EXPR,
9486                                  begin, ERROR_MARK,
9487                                  end, ERROR_MARK,
9488                                  NULL, tf_warning_or_error);
9489   finish_for_cond (condition, statement);
9490
9491   /* The new increment expression.  */
9492   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9493   finish_for_expr (expression, statement);
9494
9495   /* The declaration is initialized with *__begin inside the loop body.  */
9496   cp_finish_decl (range_decl,
9497                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9498                   /*is_constant_init*/false, NULL_TREE,
9499                   LOOKUP_ONLYCONVERTING);
9500
9501   return statement;
9502 }
9503
9504 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9505    We need to solve both at the same time because the method used
9506    depends on the existence of members begin or end.
9507    Returns the type deduced for the iterator expression.  */
9508
9509 static tree
9510 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9511 {
9512   if (error_operand_p (range))
9513     {
9514       *begin = *end = error_mark_node;
9515       return error_mark_node;
9516     }
9517
9518   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9519     {
9520       error ("range-based %<for%> expression of type %qT "
9521              "has incomplete type", TREE_TYPE (range));
9522       *begin = *end = error_mark_node;
9523       return error_mark_node;
9524     }
9525   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9526     {
9527       /* If RANGE is an array, we will use pointer arithmetic.  */
9528       *begin = range;
9529       *end = build_binary_op (input_location, PLUS_EXPR,
9530                               range,
9531                               array_type_nelts_top (TREE_TYPE (range)),
9532                               0);
9533       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9534     }
9535   else
9536     {
9537       /* If it is not an array, we must do a bit of magic.  */
9538       tree id_begin, id_end;
9539       tree member_begin, member_end;
9540
9541       *begin = *end = error_mark_node;
9542
9543       id_begin = get_identifier ("begin");
9544       id_end = get_identifier ("end");
9545       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9546                                     /*protect=*/2, /*want_type=*/false,
9547                                     tf_warning_or_error);
9548       member_end = lookup_member (TREE_TYPE (range), id_end,
9549                                   /*protect=*/2, /*want_type=*/false,
9550                                   tf_warning_or_error);
9551
9552       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9553         {
9554           /* Use the member functions.  */
9555           if (member_begin != NULL_TREE)
9556             *begin = cp_parser_range_for_member_function (range, id_begin);
9557           else
9558             error ("range-based %<for%> expression of type %qT has an "
9559                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9560
9561           if (member_end != NULL_TREE)
9562             *end = cp_parser_range_for_member_function (range, id_end);
9563           else
9564             error ("range-based %<for%> expression of type %qT has a "
9565                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9566         }
9567       else
9568         {
9569           /* Use global functions with ADL.  */
9570           VEC(tree,gc) *vec;
9571           vec = make_tree_vector ();
9572
9573           VEC_safe_push (tree, gc, vec, range);
9574
9575           member_begin = perform_koenig_lookup (id_begin, vec,
9576                                                 /*include_std=*/true,
9577                                                 tf_warning_or_error);
9578           *begin = finish_call_expr (member_begin, &vec, false, true,
9579                                      tf_warning_or_error);
9580           member_end = perform_koenig_lookup (id_end, vec,
9581                                               /*include_std=*/true,
9582                                               tf_warning_or_error);
9583           *end = finish_call_expr (member_end, &vec, false, true,
9584                                    tf_warning_or_error);
9585
9586           release_tree_vector (vec);
9587         }
9588
9589       /* Last common checks.  */
9590       if (*begin == error_mark_node || *end == error_mark_node)
9591         {
9592           /* If one of the expressions is an error do no more checks.  */
9593           *begin = *end = error_mark_node;
9594           return error_mark_node;
9595         }
9596       else
9597         {
9598           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9599           /* The unqualified type of the __begin and __end temporaries should
9600              be the same, as required by the multiple auto declaration.  */
9601           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9602             error ("inconsistent begin/end types in range-based %<for%> "
9603                    "statement: %qT and %qT",
9604                    TREE_TYPE (*begin), TREE_TYPE (*end));
9605           return iter_type;
9606         }
9607     }
9608 }
9609
9610 /* Helper function for cp_parser_perform_range_for_lookup.
9611    Builds a tree for RANGE.IDENTIFIER().  */
9612
9613 static tree
9614 cp_parser_range_for_member_function (tree range, tree identifier)
9615 {
9616   tree member, res;
9617   VEC(tree,gc) *vec;
9618
9619   member = finish_class_member_access_expr (range, identifier,
9620                                             false, tf_warning_or_error);
9621   if (member == error_mark_node)
9622     return error_mark_node;
9623
9624   vec = make_tree_vector ();
9625   res = finish_call_expr (member, &vec,
9626                           /*disallow_virtual=*/false,
9627                           /*koenig_p=*/false,
9628                           tf_warning_or_error);
9629   release_tree_vector (vec);
9630   return res;
9631 }
9632
9633 /* Parse an iteration-statement.
9634
9635    iteration-statement:
9636      while ( condition ) statement
9637      do statement while ( expression ) ;
9638      for ( for-init-statement condition [opt] ; expression [opt] )
9639        statement
9640
9641    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9642
9643 static tree
9644 cp_parser_iteration_statement (cp_parser* parser)
9645 {
9646   cp_token *token;
9647   enum rid keyword;
9648   tree statement;
9649   unsigned char in_statement;
9650
9651   /* Peek at the next token.  */
9652   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9653   if (!token)
9654     return error_mark_node;
9655
9656   /* Remember whether or not we are already within an iteration
9657      statement.  */
9658   in_statement = parser->in_statement;
9659
9660   /* See what kind of keyword it is.  */
9661   keyword = token->keyword;
9662   switch (keyword)
9663     {
9664     case RID_WHILE:
9665       {
9666         tree condition;
9667
9668         /* Begin the while-statement.  */
9669         statement = begin_while_stmt ();
9670         /* Look for the `('.  */
9671         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9672         /* Parse the condition.  */
9673         condition = cp_parser_condition (parser);
9674         finish_while_stmt_cond (condition, statement);
9675         /* Look for the `)'.  */
9676         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9677         /* Parse the dependent statement.  */
9678         parser->in_statement = IN_ITERATION_STMT;
9679         cp_parser_already_scoped_statement (parser);
9680         parser->in_statement = in_statement;
9681         /* We're done with the while-statement.  */
9682         finish_while_stmt (statement);
9683       }
9684       break;
9685
9686     case RID_DO:
9687       {
9688         tree expression;
9689
9690         /* Begin the do-statement.  */
9691         statement = begin_do_stmt ();
9692         /* Parse the body of the do-statement.  */
9693         parser->in_statement = IN_ITERATION_STMT;
9694         cp_parser_implicitly_scoped_statement (parser, NULL);
9695         parser->in_statement = in_statement;
9696         finish_do_body (statement);
9697         /* Look for the `while' keyword.  */
9698         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9699         /* Look for the `('.  */
9700         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9701         /* Parse the expression.  */
9702         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9703         /* We're done with the do-statement.  */
9704         finish_do_stmt (expression, statement);
9705         /* Look for the `)'.  */
9706         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9707         /* Look for the `;'.  */
9708         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9709       }
9710       break;
9711
9712     case RID_FOR:
9713       {
9714         /* Look for the `('.  */
9715         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9716
9717         statement = cp_parser_for (parser);
9718
9719         /* Look for the `)'.  */
9720         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9721
9722         /* Parse the body of the for-statement.  */
9723         parser->in_statement = IN_ITERATION_STMT;
9724         cp_parser_already_scoped_statement (parser);
9725         parser->in_statement = in_statement;
9726
9727         /* We're done with the for-statement.  */
9728         finish_for_stmt (statement);
9729       }
9730       break;
9731
9732     default:
9733       cp_parser_error (parser, "expected iteration-statement");
9734       statement = error_mark_node;
9735       break;
9736     }
9737
9738   return statement;
9739 }
9740
9741 /* Parse a for-init-statement or the declarator of a range-based-for.
9742    Returns true if a range-based-for declaration is seen.
9743
9744    for-init-statement:
9745      expression-statement
9746      simple-declaration  */
9747
9748 static bool
9749 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9750 {
9751   /* If the next token is a `;', then we have an empty
9752      expression-statement.  Grammatically, this is also a
9753      simple-declaration, but an invalid one, because it does not
9754      declare anything.  Therefore, if we did not handle this case
9755      specially, we would issue an error message about an invalid
9756      declaration.  */
9757   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9758     {
9759       bool is_range_for = false;
9760       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9761
9762       parser->colon_corrects_to_scope_p = false;
9763
9764       /* We're going to speculatively look for a declaration, falling back
9765          to an expression, if necessary.  */
9766       cp_parser_parse_tentatively (parser);
9767       /* Parse the declaration.  */
9768       cp_parser_simple_declaration (parser,
9769                                     /*function_definition_allowed_p=*/false,
9770                                     decl);
9771       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9772       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9773         {
9774           /* It is a range-for, consume the ':' */
9775           cp_lexer_consume_token (parser->lexer);
9776           is_range_for = true;
9777           if (cxx_dialect < cxx0x)
9778             {
9779               error_at (cp_lexer_peek_token (parser->lexer)->location,
9780                         "range-based %<for%> loops are not allowed "
9781                         "in C++98 mode");
9782               *decl = error_mark_node;
9783             }
9784         }
9785       else
9786           /* The ';' is not consumed yet because we told
9787              cp_parser_simple_declaration not to.  */
9788           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9789
9790       if (cp_parser_parse_definitely (parser))
9791         return is_range_for;
9792       /* If the tentative parse failed, then we shall need to look for an
9793          expression-statement.  */
9794     }
9795   /* If we are here, it is an expression-statement.  */
9796   cp_parser_expression_statement (parser, NULL_TREE);
9797   return false;
9798 }
9799
9800 /* Parse a jump-statement.
9801
9802    jump-statement:
9803      break ;
9804      continue ;
9805      return expression [opt] ;
9806      return braced-init-list ;
9807      goto identifier ;
9808
9809    GNU extension:
9810
9811    jump-statement:
9812      goto * expression ;
9813
9814    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9815
9816 static tree
9817 cp_parser_jump_statement (cp_parser* parser)
9818 {
9819   tree statement = error_mark_node;
9820   cp_token *token;
9821   enum rid keyword;
9822   unsigned char in_statement;
9823
9824   /* Peek at the next token.  */
9825   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9826   if (!token)
9827     return error_mark_node;
9828
9829   /* See what kind of keyword it is.  */
9830   keyword = token->keyword;
9831   switch (keyword)
9832     {
9833     case RID_BREAK:
9834       in_statement = parser->in_statement & ~IN_IF_STMT;      
9835       switch (in_statement)
9836         {
9837         case 0:
9838           error_at (token->location, "break statement not within loop or switch");
9839           break;
9840         default:
9841           gcc_assert ((in_statement & IN_SWITCH_STMT)
9842                       || in_statement == IN_ITERATION_STMT);
9843           statement = finish_break_stmt ();
9844           break;
9845         case IN_OMP_BLOCK:
9846           error_at (token->location, "invalid exit from OpenMP structured block");
9847           break;
9848         case IN_OMP_FOR:
9849           error_at (token->location, "break statement used with OpenMP for loop");
9850           break;
9851         }
9852       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9853       break;
9854
9855     case RID_CONTINUE:
9856       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9857         {
9858         case 0:
9859           error_at (token->location, "continue statement not within a loop");
9860           break;
9861         case IN_ITERATION_STMT:
9862         case IN_OMP_FOR:
9863           statement = finish_continue_stmt ();
9864           break;
9865         case IN_OMP_BLOCK:
9866           error_at (token->location, "invalid exit from OpenMP structured block");
9867           break;
9868         default:
9869           gcc_unreachable ();
9870         }
9871       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9872       break;
9873
9874     case RID_RETURN:
9875       {
9876         tree expr;
9877         bool expr_non_constant_p;
9878
9879         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9880           {
9881             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9882             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9883           }
9884         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9885           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9886         else
9887           /* If the next token is a `;', then there is no
9888              expression.  */
9889           expr = NULL_TREE;
9890         /* Build the return-statement.  */
9891         statement = finish_return_stmt (expr);
9892         /* Look for the final `;'.  */
9893         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9894       }
9895       break;
9896
9897     case RID_GOTO:
9898       /* Create the goto-statement.  */
9899       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9900         {
9901           /* Issue a warning about this use of a GNU extension.  */
9902           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9903           /* Consume the '*' token.  */
9904           cp_lexer_consume_token (parser->lexer);
9905           /* Parse the dependent expression.  */
9906           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9907         }
9908       else
9909         finish_goto_stmt (cp_parser_identifier (parser));
9910       /* Look for the final `;'.  */
9911       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9912       break;
9913
9914     default:
9915       cp_parser_error (parser, "expected jump-statement");
9916       break;
9917     }
9918
9919   return statement;
9920 }
9921
9922 /* Parse a declaration-statement.
9923
9924    declaration-statement:
9925      block-declaration  */
9926
9927 static void
9928 cp_parser_declaration_statement (cp_parser* parser)
9929 {
9930   void *p;
9931
9932   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9933   p = obstack_alloc (&declarator_obstack, 0);
9934
9935  /* Parse the block-declaration.  */
9936   cp_parser_block_declaration (parser, /*statement_p=*/true);
9937
9938   /* Free any declarators allocated.  */
9939   obstack_free (&declarator_obstack, p);
9940
9941   /* Finish off the statement.  */
9942   finish_stmt ();
9943 }
9944
9945 /* Some dependent statements (like `if (cond) statement'), are
9946    implicitly in their own scope.  In other words, if the statement is
9947    a single statement (as opposed to a compound-statement), it is
9948    none-the-less treated as if it were enclosed in braces.  Any
9949    declarations appearing in the dependent statement are out of scope
9950    after control passes that point.  This function parses a statement,
9951    but ensures that is in its own scope, even if it is not a
9952    compound-statement.
9953
9954    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9955    is a (possibly labeled) if statement which is not enclosed in
9956    braces and has an else clause.  This is used to implement
9957    -Wparentheses.
9958
9959    Returns the new statement.  */
9960
9961 static tree
9962 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9963 {
9964   tree statement;
9965
9966   if (if_p != NULL)
9967     *if_p = false;
9968
9969   /* Mark if () ; with a special NOP_EXPR.  */
9970   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9971     {
9972       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9973       cp_lexer_consume_token (parser->lexer);
9974       statement = add_stmt (build_empty_stmt (loc));
9975     }
9976   /* if a compound is opened, we simply parse the statement directly.  */
9977   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9978     statement = cp_parser_compound_statement (parser, NULL, false, false);
9979   /* If the token is not a `{', then we must take special action.  */
9980   else
9981     {
9982       /* Create a compound-statement.  */
9983       statement = begin_compound_stmt (0);
9984       /* Parse the dependent-statement.  */
9985       cp_parser_statement (parser, NULL_TREE, false, if_p);
9986       /* Finish the dummy compound-statement.  */
9987       finish_compound_stmt (statement);
9988     }
9989
9990   /* Return the statement.  */
9991   return statement;
9992 }
9993
9994 /* For some dependent statements (like `while (cond) statement'), we
9995    have already created a scope.  Therefore, even if the dependent
9996    statement is a compound-statement, we do not want to create another
9997    scope.  */
9998
9999 static void
10000 cp_parser_already_scoped_statement (cp_parser* parser)
10001 {
10002   /* If the token is a `{', then we must take special action.  */
10003   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
10004     cp_parser_statement (parser, NULL_TREE, false, NULL);
10005   else
10006     {
10007       /* Avoid calling cp_parser_compound_statement, so that we
10008          don't create a new scope.  Do everything else by hand.  */
10009       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
10010       /* If the next keyword is `__label__' we have a label declaration.  */
10011       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10012         cp_parser_label_declaration (parser);
10013       /* Parse an (optional) statement-seq.  */
10014       cp_parser_statement_seq_opt (parser, NULL_TREE);
10015       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10016     }
10017 }
10018
10019 /* Declarations [gram.dcl.dcl] */
10020
10021 /* Parse an optional declaration-sequence.
10022
10023    declaration-seq:
10024      declaration
10025      declaration-seq declaration  */
10026
10027 static void
10028 cp_parser_declaration_seq_opt (cp_parser* parser)
10029 {
10030   while (true)
10031     {
10032       cp_token *token;
10033
10034       token = cp_lexer_peek_token (parser->lexer);
10035
10036       if (token->type == CPP_CLOSE_BRACE
10037           || token->type == CPP_EOF
10038           || token->type == CPP_PRAGMA_EOL)
10039         break;
10040
10041       if (token->type == CPP_SEMICOLON)
10042         {
10043           /* A declaration consisting of a single semicolon is
10044              invalid.  Allow it unless we're being pedantic.  */
10045           cp_lexer_consume_token (parser->lexer);
10046           if (!in_system_header)
10047             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10048           continue;
10049         }
10050
10051       /* If we're entering or exiting a region that's implicitly
10052          extern "C", modify the lang context appropriately.  */
10053       if (!parser->implicit_extern_c && token->implicit_extern_c)
10054         {
10055           push_lang_context (lang_name_c);
10056           parser->implicit_extern_c = true;
10057         }
10058       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10059         {
10060           pop_lang_context ();
10061           parser->implicit_extern_c = false;
10062         }
10063
10064       if (token->type == CPP_PRAGMA)
10065         {
10066           /* A top-level declaration can consist solely of a #pragma.
10067              A nested declaration cannot, so this is done here and not
10068              in cp_parser_declaration.  (A #pragma at block scope is
10069              handled in cp_parser_statement.)  */
10070           cp_parser_pragma (parser, pragma_external);
10071           continue;
10072         }
10073
10074       /* Parse the declaration itself.  */
10075       cp_parser_declaration (parser);
10076     }
10077 }
10078
10079 /* Parse a declaration.
10080
10081    declaration:
10082      block-declaration
10083      function-definition
10084      template-declaration
10085      explicit-instantiation
10086      explicit-specialization
10087      linkage-specification
10088      namespace-definition
10089
10090    GNU extension:
10091
10092    declaration:
10093       __extension__ declaration */
10094
10095 static void
10096 cp_parser_declaration (cp_parser* parser)
10097 {
10098   cp_token token1;
10099   cp_token token2;
10100   int saved_pedantic;
10101   void *p;
10102   tree attributes = NULL_TREE;
10103
10104   /* Check for the `__extension__' keyword.  */
10105   if (cp_parser_extension_opt (parser, &saved_pedantic))
10106     {
10107       /* Parse the qualified declaration.  */
10108       cp_parser_declaration (parser);
10109       /* Restore the PEDANTIC flag.  */
10110       pedantic = saved_pedantic;
10111
10112       return;
10113     }
10114
10115   /* Try to figure out what kind of declaration is present.  */
10116   token1 = *cp_lexer_peek_token (parser->lexer);
10117
10118   if (token1.type != CPP_EOF)
10119     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10120   else
10121     {
10122       token2.type = CPP_EOF;
10123       token2.keyword = RID_MAX;
10124     }
10125
10126   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10127   p = obstack_alloc (&declarator_obstack, 0);
10128
10129   /* If the next token is `extern' and the following token is a string
10130      literal, then we have a linkage specification.  */
10131   if (token1.keyword == RID_EXTERN
10132       && cp_parser_is_pure_string_literal (&token2))
10133     cp_parser_linkage_specification (parser);
10134   /* If the next token is `template', then we have either a template
10135      declaration, an explicit instantiation, or an explicit
10136      specialization.  */
10137   else if (token1.keyword == RID_TEMPLATE)
10138     {
10139       /* `template <>' indicates a template specialization.  */
10140       if (token2.type == CPP_LESS
10141           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10142         cp_parser_explicit_specialization (parser);
10143       /* `template <' indicates a template declaration.  */
10144       else if (token2.type == CPP_LESS)
10145         cp_parser_template_declaration (parser, /*member_p=*/false);
10146       /* Anything else must be an explicit instantiation.  */
10147       else
10148         cp_parser_explicit_instantiation (parser);
10149     }
10150   /* If the next token is `export', then we have a template
10151      declaration.  */
10152   else if (token1.keyword == RID_EXPORT)
10153     cp_parser_template_declaration (parser, /*member_p=*/false);
10154   /* If the next token is `extern', 'static' or 'inline' and the one
10155      after that is `template', we have a GNU extended explicit
10156      instantiation directive.  */
10157   else if (cp_parser_allow_gnu_extensions_p (parser)
10158            && (token1.keyword == RID_EXTERN
10159                || token1.keyword == RID_STATIC
10160                || token1.keyword == RID_INLINE)
10161            && token2.keyword == RID_TEMPLATE)
10162     cp_parser_explicit_instantiation (parser);
10163   /* If the next token is `namespace', check for a named or unnamed
10164      namespace definition.  */
10165   else if (token1.keyword == RID_NAMESPACE
10166            && (/* A named namespace definition.  */
10167                (token2.type == CPP_NAME
10168                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10169                     != CPP_EQ))
10170                /* An unnamed namespace definition.  */
10171                || token2.type == CPP_OPEN_BRACE
10172                || token2.keyword == RID_ATTRIBUTE))
10173     cp_parser_namespace_definition (parser);
10174   /* An inline (associated) namespace definition.  */
10175   else if (token1.keyword == RID_INLINE
10176            && token2.keyword == RID_NAMESPACE)
10177     cp_parser_namespace_definition (parser);
10178   /* Objective-C++ declaration/definition.  */
10179   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10180     cp_parser_objc_declaration (parser, NULL_TREE);
10181   else if (c_dialect_objc ()
10182            && token1.keyword == RID_ATTRIBUTE
10183            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10184     cp_parser_objc_declaration (parser, attributes);
10185   /* We must have either a block declaration or a function
10186      definition.  */
10187   else
10188     /* Try to parse a block-declaration, or a function-definition.  */
10189     cp_parser_block_declaration (parser, /*statement_p=*/false);
10190
10191   /* Free any declarators allocated.  */
10192   obstack_free (&declarator_obstack, p);
10193 }
10194
10195 /* Parse a block-declaration.
10196
10197    block-declaration:
10198      simple-declaration
10199      asm-definition
10200      namespace-alias-definition
10201      using-declaration
10202      using-directive
10203
10204    GNU Extension:
10205
10206    block-declaration:
10207      __extension__ block-declaration
10208
10209    C++0x Extension:
10210
10211    block-declaration:
10212      static_assert-declaration
10213
10214    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10215    part of a declaration-statement.  */
10216
10217 static void
10218 cp_parser_block_declaration (cp_parser *parser,
10219                              bool      statement_p)
10220 {
10221   cp_token *token1;
10222   int saved_pedantic;
10223
10224   /* Check for the `__extension__' keyword.  */
10225   if (cp_parser_extension_opt (parser, &saved_pedantic))
10226     {
10227       /* Parse the qualified declaration.  */
10228       cp_parser_block_declaration (parser, statement_p);
10229       /* Restore the PEDANTIC flag.  */
10230       pedantic = saved_pedantic;
10231
10232       return;
10233     }
10234
10235   /* Peek at the next token to figure out which kind of declaration is
10236      present.  */
10237   token1 = cp_lexer_peek_token (parser->lexer);
10238
10239   /* If the next keyword is `asm', we have an asm-definition.  */
10240   if (token1->keyword == RID_ASM)
10241     {
10242       if (statement_p)
10243         cp_parser_commit_to_tentative_parse (parser);
10244       cp_parser_asm_definition (parser);
10245     }
10246   /* If the next keyword is `namespace', we have a
10247      namespace-alias-definition.  */
10248   else if (token1->keyword == RID_NAMESPACE)
10249     cp_parser_namespace_alias_definition (parser);
10250   /* If the next keyword is `using', we have a
10251      using-declaration, a using-directive, or an alias-declaration.  */
10252   else if (token1->keyword == RID_USING)
10253     {
10254       cp_token *token2;
10255
10256       if (statement_p)
10257         cp_parser_commit_to_tentative_parse (parser);
10258       /* If the token after `using' is `namespace', then we have a
10259          using-directive.  */
10260       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10261       if (token2->keyword == RID_NAMESPACE)
10262         cp_parser_using_directive (parser);
10263       /* If the second token after 'using' is '=', then we have an
10264          alias-declaration.  */
10265       else if (cxx_dialect >= cxx0x
10266                && token2->type == CPP_NAME
10267                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10268                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10269                        == RID_ATTRIBUTE)))
10270         cp_parser_alias_declaration (parser);
10271       /* Otherwise, it's a using-declaration.  */
10272       else
10273         cp_parser_using_declaration (parser,
10274                                      /*access_declaration_p=*/false);
10275     }
10276   /* If the next keyword is `__label__' we have a misplaced label
10277      declaration.  */
10278   else if (token1->keyword == RID_LABEL)
10279     {
10280       cp_lexer_consume_token (parser->lexer);
10281       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10282       cp_parser_skip_to_end_of_statement (parser);
10283       /* If the next token is now a `;', consume it.  */
10284       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10285         cp_lexer_consume_token (parser->lexer);
10286     }
10287   /* If the next token is `static_assert' we have a static assertion.  */
10288   else if (token1->keyword == RID_STATIC_ASSERT)
10289     cp_parser_static_assert (parser, /*member_p=*/false);
10290   /* Anything else must be a simple-declaration.  */
10291   else
10292     cp_parser_simple_declaration (parser, !statement_p,
10293                                   /*maybe_range_for_decl*/NULL);
10294 }
10295
10296 /* Parse a simple-declaration.
10297
10298    simple-declaration:
10299      decl-specifier-seq [opt] init-declarator-list [opt] ;
10300
10301    init-declarator-list:
10302      init-declarator
10303      init-declarator-list , init-declarator
10304
10305    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10306    function-definition as a simple-declaration.
10307
10308    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10309    parsed declaration if it is an uninitialized single declarator not followed
10310    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10311    if present, will not be consumed.  */
10312
10313 static void
10314 cp_parser_simple_declaration (cp_parser* parser,
10315                               bool function_definition_allowed_p,
10316                               tree *maybe_range_for_decl)
10317 {
10318   cp_decl_specifier_seq decl_specifiers;
10319   int declares_class_or_enum;
10320   bool saw_declarator;
10321
10322   if (maybe_range_for_decl)
10323     *maybe_range_for_decl = NULL_TREE;
10324
10325   /* Defer access checks until we know what is being declared; the
10326      checks for names appearing in the decl-specifier-seq should be
10327      done as if we were in the scope of the thing being declared.  */
10328   push_deferring_access_checks (dk_deferred);
10329
10330   /* Parse the decl-specifier-seq.  We have to keep track of whether
10331      or not the decl-specifier-seq declares a named class or
10332      enumeration type, since that is the only case in which the
10333      init-declarator-list is allowed to be empty.
10334
10335      [dcl.dcl]
10336
10337      In a simple-declaration, the optional init-declarator-list can be
10338      omitted only when declaring a class or enumeration, that is when
10339      the decl-specifier-seq contains either a class-specifier, an
10340      elaborated-type-specifier, or an enum-specifier.  */
10341   cp_parser_decl_specifier_seq (parser,
10342                                 CP_PARSER_FLAGS_OPTIONAL,
10343                                 &decl_specifiers,
10344                                 &declares_class_or_enum);
10345   /* We no longer need to defer access checks.  */
10346   stop_deferring_access_checks ();
10347
10348   /* In a block scope, a valid declaration must always have a
10349      decl-specifier-seq.  By not trying to parse declarators, we can
10350      resolve the declaration/expression ambiguity more quickly.  */
10351   if (!function_definition_allowed_p
10352       && !decl_specifiers.any_specifiers_p)
10353     {
10354       cp_parser_error (parser, "expected declaration");
10355       goto done;
10356     }
10357
10358   /* If the next two tokens are both identifiers, the code is
10359      erroneous. The usual cause of this situation is code like:
10360
10361        T t;
10362
10363      where "T" should name a type -- but does not.  */
10364   if (!decl_specifiers.any_type_specifiers_p
10365       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10366     {
10367       /* If parsing tentatively, we should commit; we really are
10368          looking at a declaration.  */
10369       cp_parser_commit_to_tentative_parse (parser);
10370       /* Give up.  */
10371       goto done;
10372     }
10373
10374   /* If we have seen at least one decl-specifier, and the next token
10375      is not a parenthesis, then we must be looking at a declaration.
10376      (After "int (" we might be looking at a functional cast.)  */
10377   if (decl_specifiers.any_specifiers_p
10378       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10379       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10380       && !cp_parser_error_occurred (parser))
10381     cp_parser_commit_to_tentative_parse (parser);
10382
10383   /* Keep going until we hit the `;' at the end of the simple
10384      declaration.  */
10385   saw_declarator = false;
10386   while (cp_lexer_next_token_is_not (parser->lexer,
10387                                      CPP_SEMICOLON))
10388     {
10389       cp_token *token;
10390       bool function_definition_p;
10391       tree decl;
10392
10393       if (saw_declarator)
10394         {
10395           /* If we are processing next declarator, coma is expected */
10396           token = cp_lexer_peek_token (parser->lexer);
10397           gcc_assert (token->type == CPP_COMMA);
10398           cp_lexer_consume_token (parser->lexer);
10399           if (maybe_range_for_decl)
10400             *maybe_range_for_decl = error_mark_node;
10401         }
10402       else
10403         saw_declarator = true;
10404
10405       /* Parse the init-declarator.  */
10406       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10407                                         /*checks=*/NULL,
10408                                         function_definition_allowed_p,
10409                                         /*member_p=*/false,
10410                                         declares_class_or_enum,
10411                                         &function_definition_p,
10412                                         maybe_range_for_decl);
10413       /* If an error occurred while parsing tentatively, exit quickly.
10414          (That usually happens when in the body of a function; each
10415          statement is treated as a declaration-statement until proven
10416          otherwise.)  */
10417       if (cp_parser_error_occurred (parser))
10418         goto done;
10419       /* Handle function definitions specially.  */
10420       if (function_definition_p)
10421         {
10422           /* If the next token is a `,', then we are probably
10423              processing something like:
10424
10425                void f() {}, *p;
10426
10427              which is erroneous.  */
10428           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10429             {
10430               cp_token *token = cp_lexer_peek_token (parser->lexer);
10431               error_at (token->location,
10432                         "mixing"
10433                         " declarations and function-definitions is forbidden");
10434             }
10435           /* Otherwise, we're done with the list of declarators.  */
10436           else
10437             {
10438               pop_deferring_access_checks ();
10439               return;
10440             }
10441         }
10442       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10443         *maybe_range_for_decl = decl;
10444       /* The next token should be either a `,' or a `;'.  */
10445       token = cp_lexer_peek_token (parser->lexer);
10446       /* If it's a `,', there are more declarators to come.  */
10447       if (token->type == CPP_COMMA)
10448         /* will be consumed next time around */;
10449       /* If it's a `;', we are done.  */
10450       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10451         break;
10452       /* Anything else is an error.  */
10453       else
10454         {
10455           /* If we have already issued an error message we don't need
10456              to issue another one.  */
10457           if (decl != error_mark_node
10458               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10459             cp_parser_error (parser, "expected %<,%> or %<;%>");
10460           /* Skip tokens until we reach the end of the statement.  */
10461           cp_parser_skip_to_end_of_statement (parser);
10462           /* If the next token is now a `;', consume it.  */
10463           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10464             cp_lexer_consume_token (parser->lexer);
10465           goto done;
10466         }
10467       /* After the first time around, a function-definition is not
10468          allowed -- even if it was OK at first.  For example:
10469
10470            int i, f() {}
10471
10472          is not valid.  */
10473       function_definition_allowed_p = false;
10474     }
10475
10476   /* Issue an error message if no declarators are present, and the
10477      decl-specifier-seq does not itself declare a class or
10478      enumeration.  */
10479   if (!saw_declarator)
10480     {
10481       if (cp_parser_declares_only_class_p (parser))
10482         shadow_tag (&decl_specifiers);
10483       /* Perform any deferred access checks.  */
10484       perform_deferred_access_checks ();
10485     }
10486
10487   /* Consume the `;'.  */
10488   if (!maybe_range_for_decl)
10489       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10490
10491  done:
10492   pop_deferring_access_checks ();
10493 }
10494
10495 /* Parse a decl-specifier-seq.
10496
10497    decl-specifier-seq:
10498      decl-specifier-seq [opt] decl-specifier
10499
10500    decl-specifier:
10501      storage-class-specifier
10502      type-specifier
10503      function-specifier
10504      friend
10505      typedef
10506
10507    GNU Extension:
10508
10509    decl-specifier:
10510      attributes
10511
10512    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10513
10514    The parser flags FLAGS is used to control type-specifier parsing.
10515
10516    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10517    flags:
10518
10519      1: one of the decl-specifiers is an elaborated-type-specifier
10520         (i.e., a type declaration)
10521      2: one of the decl-specifiers is an enum-specifier or a
10522         class-specifier (i.e., a type definition)
10523
10524    */
10525
10526 static void
10527 cp_parser_decl_specifier_seq (cp_parser* parser,
10528                               cp_parser_flags flags,
10529                               cp_decl_specifier_seq *decl_specs,
10530                               int* declares_class_or_enum)
10531 {
10532   bool constructor_possible_p = !parser->in_declarator_p;
10533   cp_token *start_token = NULL;
10534
10535   /* Clear DECL_SPECS.  */
10536   clear_decl_specs (decl_specs);
10537
10538   /* Assume no class or enumeration type is declared.  */
10539   *declares_class_or_enum = 0;
10540
10541   /* Keep reading specifiers until there are no more to read.  */
10542   while (true)
10543     {
10544       bool constructor_p;
10545       bool found_decl_spec;
10546       cp_token *token;
10547
10548       /* Peek at the next token.  */
10549       token = cp_lexer_peek_token (parser->lexer);
10550
10551       /* Save the first token of the decl spec list for error
10552          reporting.  */
10553       if (!start_token)
10554         start_token = token;
10555       /* Handle attributes.  */
10556       if (token->keyword == RID_ATTRIBUTE)
10557         {
10558           /* Parse the attributes.  */
10559           decl_specs->attributes
10560             = chainon (decl_specs->attributes,
10561                        cp_parser_attributes_opt (parser));
10562           continue;
10563         }
10564       /* Assume we will find a decl-specifier keyword.  */
10565       found_decl_spec = true;
10566       /* If the next token is an appropriate keyword, we can simply
10567          add it to the list.  */
10568       switch (token->keyword)
10569         {
10570           /* decl-specifier:
10571                friend
10572                constexpr */
10573         case RID_FRIEND:
10574           if (!at_class_scope_p ())
10575             {
10576               error_at (token->location, "%<friend%> used outside of class");
10577               cp_lexer_purge_token (parser->lexer);
10578             }
10579           else
10580             {
10581               ++decl_specs->specs[(int) ds_friend];
10582               /* Consume the token.  */
10583               cp_lexer_consume_token (parser->lexer);
10584             }
10585           break;
10586
10587         case RID_CONSTEXPR:
10588           ++decl_specs->specs[(int) ds_constexpr];
10589           cp_lexer_consume_token (parser->lexer);
10590           break;
10591
10592           /* function-specifier:
10593                inline
10594                virtual
10595                explicit  */
10596         case RID_INLINE:
10597         case RID_VIRTUAL:
10598         case RID_EXPLICIT:
10599           cp_parser_function_specifier_opt (parser, decl_specs);
10600           break;
10601
10602           /* decl-specifier:
10603                typedef  */
10604         case RID_TYPEDEF:
10605           ++decl_specs->specs[(int) ds_typedef];
10606           /* Consume the token.  */
10607           cp_lexer_consume_token (parser->lexer);
10608           /* A constructor declarator cannot appear in a typedef.  */
10609           constructor_possible_p = false;
10610           /* The "typedef" keyword can only occur in a declaration; we
10611              may as well commit at this point.  */
10612           cp_parser_commit_to_tentative_parse (parser);
10613
10614           if (decl_specs->storage_class != sc_none)
10615             decl_specs->conflicting_specifiers_p = true;
10616           break;
10617
10618           /* storage-class-specifier:
10619                auto
10620                register
10621                static
10622                extern
10623                mutable
10624
10625              GNU Extension:
10626                thread  */
10627         case RID_AUTO:
10628           if (cxx_dialect == cxx98) 
10629             {
10630               /* Consume the token.  */
10631               cp_lexer_consume_token (parser->lexer);
10632
10633               /* Complain about `auto' as a storage specifier, if
10634                  we're complaining about C++0x compatibility.  */
10635               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10636                           " changes meaning in C++11; please remove it");
10637
10638               /* Set the storage class anyway.  */
10639               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10640                                            token->location);
10641             }
10642           else
10643             /* C++0x auto type-specifier.  */
10644             found_decl_spec = false;
10645           break;
10646
10647         case RID_REGISTER:
10648         case RID_STATIC:
10649         case RID_EXTERN:
10650         case RID_MUTABLE:
10651           /* Consume the token.  */
10652           cp_lexer_consume_token (parser->lexer);
10653           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10654                                        token->location);
10655           break;
10656         case RID_THREAD:
10657           /* Consume the token.  */
10658           cp_lexer_consume_token (parser->lexer);
10659           ++decl_specs->specs[(int) ds_thread];
10660           break;
10661
10662         default:
10663           /* We did not yet find a decl-specifier yet.  */
10664           found_decl_spec = false;
10665           break;
10666         }
10667
10668       if (found_decl_spec
10669           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10670           && token->keyword != RID_CONSTEXPR)
10671         error ("decl-specifier invalid in condition");
10672
10673       /* Constructors are a special case.  The `S' in `S()' is not a
10674          decl-specifier; it is the beginning of the declarator.  */
10675       constructor_p
10676         = (!found_decl_spec
10677            && constructor_possible_p
10678            && (cp_parser_constructor_declarator_p
10679                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10680
10681       /* If we don't have a DECL_SPEC yet, then we must be looking at
10682          a type-specifier.  */
10683       if (!found_decl_spec && !constructor_p)
10684         {
10685           int decl_spec_declares_class_or_enum;
10686           bool is_cv_qualifier;
10687           tree type_spec;
10688
10689           type_spec
10690             = cp_parser_type_specifier (parser, flags,
10691                                         decl_specs,
10692                                         /*is_declaration=*/true,
10693                                         &decl_spec_declares_class_or_enum,
10694                                         &is_cv_qualifier);
10695           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10696
10697           /* If this type-specifier referenced a user-defined type
10698              (a typedef, class-name, etc.), then we can't allow any
10699              more such type-specifiers henceforth.
10700
10701              [dcl.spec]
10702
10703              The longest sequence of decl-specifiers that could
10704              possibly be a type name is taken as the
10705              decl-specifier-seq of a declaration.  The sequence shall
10706              be self-consistent as described below.
10707
10708              [dcl.type]
10709
10710              As a general rule, at most one type-specifier is allowed
10711              in the complete decl-specifier-seq of a declaration.  The
10712              only exceptions are the following:
10713
10714              -- const or volatile can be combined with any other
10715                 type-specifier.
10716
10717              -- signed or unsigned can be combined with char, long,
10718                 short, or int.
10719
10720              -- ..
10721
10722              Example:
10723
10724                typedef char* Pc;
10725                void g (const int Pc);
10726
10727              Here, Pc is *not* part of the decl-specifier seq; it's
10728              the declarator.  Therefore, once we see a type-specifier
10729              (other than a cv-qualifier), we forbid any additional
10730              user-defined types.  We *do* still allow things like `int
10731              int' to be considered a decl-specifier-seq, and issue the
10732              error message later.  */
10733           if (type_spec && !is_cv_qualifier)
10734             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10735           /* A constructor declarator cannot follow a type-specifier.  */
10736           if (type_spec)
10737             {
10738               constructor_possible_p = false;
10739               found_decl_spec = true;
10740               if (!is_cv_qualifier)
10741                 decl_specs->any_type_specifiers_p = true;
10742             }
10743         }
10744
10745       /* If we still do not have a DECL_SPEC, then there are no more
10746          decl-specifiers.  */
10747       if (!found_decl_spec)
10748         break;
10749
10750       decl_specs->any_specifiers_p = true;
10751       /* After we see one decl-specifier, further decl-specifiers are
10752          always optional.  */
10753       flags |= CP_PARSER_FLAGS_OPTIONAL;
10754     }
10755
10756   cp_parser_check_decl_spec (decl_specs, start_token->location);
10757
10758   /* Don't allow a friend specifier with a class definition.  */
10759   if (decl_specs->specs[(int) ds_friend] != 0
10760       && (*declares_class_or_enum & 2))
10761     error_at (start_token->location,
10762               "class definition may not be declared a friend");
10763 }
10764
10765 /* Parse an (optional) storage-class-specifier.
10766
10767    storage-class-specifier:
10768      auto
10769      register
10770      static
10771      extern
10772      mutable
10773
10774    GNU Extension:
10775
10776    storage-class-specifier:
10777      thread
10778
10779    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10780
10781 static tree
10782 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10783 {
10784   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10785     {
10786     case RID_AUTO:
10787       if (cxx_dialect != cxx98)
10788         return NULL_TREE;
10789       /* Fall through for C++98.  */
10790
10791     case RID_REGISTER:
10792     case RID_STATIC:
10793     case RID_EXTERN:
10794     case RID_MUTABLE:
10795     case RID_THREAD:
10796       /* Consume the token.  */
10797       return cp_lexer_consume_token (parser->lexer)->u.value;
10798
10799     default:
10800       return NULL_TREE;
10801     }
10802 }
10803
10804 /* Parse an (optional) function-specifier.
10805
10806    function-specifier:
10807      inline
10808      virtual
10809      explicit
10810
10811    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10812    Updates DECL_SPECS, if it is non-NULL.  */
10813
10814 static tree
10815 cp_parser_function_specifier_opt (cp_parser* parser,
10816                                   cp_decl_specifier_seq *decl_specs)
10817 {
10818   cp_token *token = cp_lexer_peek_token (parser->lexer);
10819   switch (token->keyword)
10820     {
10821     case RID_INLINE:
10822       if (decl_specs)
10823         ++decl_specs->specs[(int) ds_inline];
10824       break;
10825
10826     case RID_VIRTUAL:
10827       /* 14.5.2.3 [temp.mem]
10828
10829          A member function template shall not be virtual.  */
10830       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10831         error_at (token->location, "templates may not be %<virtual%>");
10832       else if (decl_specs)
10833         ++decl_specs->specs[(int) ds_virtual];
10834       break;
10835
10836     case RID_EXPLICIT:
10837       if (decl_specs)
10838         ++decl_specs->specs[(int) ds_explicit];
10839       break;
10840
10841     default:
10842       return NULL_TREE;
10843     }
10844
10845   /* Consume the token.  */
10846   return cp_lexer_consume_token (parser->lexer)->u.value;
10847 }
10848
10849 /* Parse a linkage-specification.
10850
10851    linkage-specification:
10852      extern string-literal { declaration-seq [opt] }
10853      extern string-literal declaration  */
10854
10855 static void
10856 cp_parser_linkage_specification (cp_parser* parser)
10857 {
10858   tree linkage;
10859
10860   /* Look for the `extern' keyword.  */
10861   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10862
10863   /* Look for the string-literal.  */
10864   linkage = cp_parser_string_literal (parser, false, false);
10865
10866   /* Transform the literal into an identifier.  If the literal is a
10867      wide-character string, or contains embedded NULs, then we can't
10868      handle it as the user wants.  */
10869   if (strlen (TREE_STRING_POINTER (linkage))
10870       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10871     {
10872       cp_parser_error (parser, "invalid linkage-specification");
10873       /* Assume C++ linkage.  */
10874       linkage = lang_name_cplusplus;
10875     }
10876   else
10877     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10878
10879   /* We're now using the new linkage.  */
10880   push_lang_context (linkage);
10881
10882   /* If the next token is a `{', then we're using the first
10883      production.  */
10884   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10885     {
10886       /* Consume the `{' token.  */
10887       cp_lexer_consume_token (parser->lexer);
10888       /* Parse the declarations.  */
10889       cp_parser_declaration_seq_opt (parser);
10890       /* Look for the closing `}'.  */
10891       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10892     }
10893   /* Otherwise, there's just one declaration.  */
10894   else
10895     {
10896       bool saved_in_unbraced_linkage_specification_p;
10897
10898       saved_in_unbraced_linkage_specification_p
10899         = parser->in_unbraced_linkage_specification_p;
10900       parser->in_unbraced_linkage_specification_p = true;
10901       cp_parser_declaration (parser);
10902       parser->in_unbraced_linkage_specification_p
10903         = saved_in_unbraced_linkage_specification_p;
10904     }
10905
10906   /* We're done with the linkage-specification.  */
10907   pop_lang_context ();
10908 }
10909
10910 /* Parse a static_assert-declaration.
10911
10912    static_assert-declaration:
10913      static_assert ( constant-expression , string-literal ) ; 
10914
10915    If MEMBER_P, this static_assert is a class member.  */
10916
10917 static void 
10918 cp_parser_static_assert(cp_parser *parser, bool member_p)
10919 {
10920   tree condition;
10921   tree message;
10922   cp_token *token;
10923   location_t saved_loc;
10924   bool dummy;
10925
10926   /* Peek at the `static_assert' token so we can keep track of exactly
10927      where the static assertion started.  */
10928   token = cp_lexer_peek_token (parser->lexer);
10929   saved_loc = token->location;
10930
10931   /* Look for the `static_assert' keyword.  */
10932   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10933                                   RT_STATIC_ASSERT))
10934     return;
10935
10936   /*  We know we are in a static assertion; commit to any tentative
10937       parse.  */
10938   if (cp_parser_parsing_tentatively (parser))
10939     cp_parser_commit_to_tentative_parse (parser);
10940
10941   /* Parse the `(' starting the static assertion condition.  */
10942   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10943
10944   /* Parse the constant-expression.  Allow a non-constant expression
10945      here in order to give better diagnostics in finish_static_assert.  */
10946   condition = 
10947     cp_parser_constant_expression (parser,
10948                                    /*allow_non_constant_p=*/true,
10949                                    /*non_constant_p=*/&dummy);
10950
10951   /* Parse the separating `,'.  */
10952   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10953
10954   /* Parse the string-literal message.  */
10955   message = cp_parser_string_literal (parser, 
10956                                       /*translate=*/false,
10957                                       /*wide_ok=*/true);
10958
10959   /* A `)' completes the static assertion.  */
10960   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10961     cp_parser_skip_to_closing_parenthesis (parser, 
10962                                            /*recovering=*/true, 
10963                                            /*or_comma=*/false,
10964                                            /*consume_paren=*/true);
10965
10966   /* A semicolon terminates the declaration.  */
10967   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10968
10969   /* Complete the static assertion, which may mean either processing 
10970      the static assert now or saving it for template instantiation.  */
10971   finish_static_assert (condition, message, saved_loc, member_p);
10972 }
10973
10974 /* Parse a `decltype' type. Returns the type. 
10975
10976    simple-type-specifier:
10977      decltype ( expression )  */
10978
10979 static tree
10980 cp_parser_decltype (cp_parser *parser)
10981 {
10982   tree expr;
10983   bool id_expression_or_member_access_p = false;
10984   const char *saved_message;
10985   bool saved_integral_constant_expression_p;
10986   bool saved_non_integral_constant_expression_p;
10987   cp_token *id_expr_start_token;
10988   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10989
10990   if (start_token->type == CPP_DECLTYPE)
10991     {
10992       /* Already parsed.  */
10993       cp_lexer_consume_token (parser->lexer);
10994       return start_token->u.value;
10995     }
10996
10997   /* Look for the `decltype' token.  */
10998   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10999     return error_mark_node;
11000
11001   /* Types cannot be defined in a `decltype' expression.  Save away the
11002      old message.  */
11003   saved_message = parser->type_definition_forbidden_message;
11004
11005   /* And create the new one.  */
11006   parser->type_definition_forbidden_message
11007     = G_("types may not be defined in %<decltype%> expressions");
11008
11009   /* The restrictions on constant-expressions do not apply inside
11010      decltype expressions.  */
11011   saved_integral_constant_expression_p
11012     = parser->integral_constant_expression_p;
11013   saved_non_integral_constant_expression_p
11014     = parser->non_integral_constant_expression_p;
11015   parser->integral_constant_expression_p = false;
11016
11017   /* Do not actually evaluate the expression.  */
11018   ++cp_unevaluated_operand;
11019
11020   /* Do not warn about problems with the expression.  */
11021   ++c_inhibit_evaluation_warnings;
11022
11023   /* Parse the opening `('.  */
11024   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11025     return error_mark_node;
11026   
11027   /* First, try parsing an id-expression.  */
11028   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11029   cp_parser_parse_tentatively (parser);
11030   expr = cp_parser_id_expression (parser,
11031                                   /*template_keyword_p=*/false,
11032                                   /*check_dependency_p=*/true,
11033                                   /*template_p=*/NULL,
11034                                   /*declarator_p=*/false,
11035                                   /*optional_p=*/false);
11036
11037   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11038     {
11039       bool non_integral_constant_expression_p = false;
11040       tree id_expression = expr;
11041       cp_id_kind idk;
11042       const char *error_msg;
11043
11044       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11045         /* Lookup the name we got back from the id-expression.  */
11046         expr = cp_parser_lookup_name (parser, expr,
11047                                       none_type,
11048                                       /*is_template=*/false,
11049                                       /*is_namespace=*/false,
11050                                       /*check_dependency=*/true,
11051                                       /*ambiguous_decls=*/NULL,
11052                                       id_expr_start_token->location);
11053
11054       if (expr
11055           && expr != error_mark_node
11056           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11057           && TREE_CODE (expr) != TYPE_DECL
11058           && (TREE_CODE (expr) != BIT_NOT_EXPR
11059               || !TYPE_P (TREE_OPERAND (expr, 0)))
11060           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11061         {
11062           /* Complete lookup of the id-expression.  */
11063           expr = (finish_id_expression
11064                   (id_expression, expr, parser->scope, &idk,
11065                    /*integral_constant_expression_p=*/false,
11066                    /*allow_non_integral_constant_expression_p=*/true,
11067                    &non_integral_constant_expression_p,
11068                    /*template_p=*/false,
11069                    /*done=*/true,
11070                    /*address_p=*/false,
11071                    /*template_arg_p=*/false,
11072                    &error_msg,
11073                    id_expr_start_token->location));
11074
11075           if (expr == error_mark_node)
11076             /* We found an id-expression, but it was something that we
11077                should not have found. This is an error, not something
11078                we can recover from, so note that we found an
11079                id-expression and we'll recover as gracefully as
11080                possible.  */
11081             id_expression_or_member_access_p = true;
11082         }
11083
11084       if (expr 
11085           && expr != error_mark_node
11086           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11087         /* We have an id-expression.  */
11088         id_expression_or_member_access_p = true;
11089     }
11090
11091   if (!id_expression_or_member_access_p)
11092     {
11093       /* Abort the id-expression parse.  */
11094       cp_parser_abort_tentative_parse (parser);
11095
11096       /* Parsing tentatively, again.  */
11097       cp_parser_parse_tentatively (parser);
11098
11099       /* Parse a class member access.  */
11100       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11101                                            /*cast_p=*/false,
11102                                            /*member_access_only_p=*/true, NULL);
11103
11104       if (expr 
11105           && expr != error_mark_node
11106           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11107         /* We have an id-expression.  */
11108         id_expression_or_member_access_p = true;
11109     }
11110
11111   if (id_expression_or_member_access_p)
11112     /* We have parsed the complete id-expression or member access.  */
11113     cp_parser_parse_definitely (parser);
11114   else
11115     {
11116       bool saved_greater_than_is_operator_p;
11117
11118       /* Abort our attempt to parse an id-expression or member access
11119          expression.  */
11120       cp_parser_abort_tentative_parse (parser);
11121
11122       /* Within a parenthesized expression, a `>' token is always
11123          the greater-than operator.  */
11124       saved_greater_than_is_operator_p
11125         = parser->greater_than_is_operator_p;
11126       parser->greater_than_is_operator_p = true;
11127
11128       /* Parse a full expression.  */
11129       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11130
11131       /* The `>' token might be the end of a template-id or
11132          template-parameter-list now.  */
11133       parser->greater_than_is_operator_p
11134         = saved_greater_than_is_operator_p;
11135     }
11136
11137   /* Go back to evaluating expressions.  */
11138   --cp_unevaluated_operand;
11139   --c_inhibit_evaluation_warnings;
11140
11141   /* Restore the old message and the integral constant expression
11142      flags.  */
11143   parser->type_definition_forbidden_message = saved_message;
11144   parser->integral_constant_expression_p
11145     = saved_integral_constant_expression_p;
11146   parser->non_integral_constant_expression_p
11147     = saved_non_integral_constant_expression_p;
11148
11149   /* Parse to the closing `)'.  */
11150   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11151     {
11152       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11153                                              /*consume_paren=*/true);
11154       return error_mark_node;
11155     }
11156
11157   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11158                                tf_warning_or_error);
11159
11160   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11161      it again.  */
11162   start_token->type = CPP_DECLTYPE;
11163   start_token->u.value = expr;
11164   start_token->keyword = RID_MAX;
11165   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11166
11167   return expr;
11168 }
11169
11170 /* Special member functions [gram.special] */
11171
11172 /* Parse a conversion-function-id.
11173
11174    conversion-function-id:
11175      operator conversion-type-id
11176
11177    Returns an IDENTIFIER_NODE representing the operator.  */
11178
11179 static tree
11180 cp_parser_conversion_function_id (cp_parser* parser)
11181 {
11182   tree type;
11183   tree saved_scope;
11184   tree saved_qualifying_scope;
11185   tree saved_object_scope;
11186   tree pushed_scope = NULL_TREE;
11187
11188   /* Look for the `operator' token.  */
11189   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11190     return error_mark_node;
11191   /* When we parse the conversion-type-id, the current scope will be
11192      reset.  However, we need that information in able to look up the
11193      conversion function later, so we save it here.  */
11194   saved_scope = parser->scope;
11195   saved_qualifying_scope = parser->qualifying_scope;
11196   saved_object_scope = parser->object_scope;
11197   /* We must enter the scope of the class so that the names of
11198      entities declared within the class are available in the
11199      conversion-type-id.  For example, consider:
11200
11201        struct S {
11202          typedef int I;
11203          operator I();
11204        };
11205
11206        S::operator I() { ... }
11207
11208      In order to see that `I' is a type-name in the definition, we
11209      must be in the scope of `S'.  */
11210   if (saved_scope)
11211     pushed_scope = push_scope (saved_scope);
11212   /* Parse the conversion-type-id.  */
11213   type = cp_parser_conversion_type_id (parser);
11214   /* Leave the scope of the class, if any.  */
11215   if (pushed_scope)
11216     pop_scope (pushed_scope);
11217   /* Restore the saved scope.  */
11218   parser->scope = saved_scope;
11219   parser->qualifying_scope = saved_qualifying_scope;
11220   parser->object_scope = saved_object_scope;
11221   /* If the TYPE is invalid, indicate failure.  */
11222   if (type == error_mark_node)
11223     return error_mark_node;
11224   return mangle_conv_op_name_for_type (type);
11225 }
11226
11227 /* Parse a conversion-type-id:
11228
11229    conversion-type-id:
11230      type-specifier-seq conversion-declarator [opt]
11231
11232    Returns the TYPE specified.  */
11233
11234 static tree
11235 cp_parser_conversion_type_id (cp_parser* parser)
11236 {
11237   tree attributes;
11238   cp_decl_specifier_seq type_specifiers;
11239   cp_declarator *declarator;
11240   tree type_specified;
11241
11242   /* Parse the attributes.  */
11243   attributes = cp_parser_attributes_opt (parser);
11244   /* Parse the type-specifiers.  */
11245   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11246                                 /*is_trailing_return=*/false,
11247                                 &type_specifiers);
11248   /* If that didn't work, stop.  */
11249   if (type_specifiers.type == error_mark_node)
11250     return error_mark_node;
11251   /* Parse the conversion-declarator.  */
11252   declarator = cp_parser_conversion_declarator_opt (parser);
11253
11254   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11255                                     /*initialized=*/0, &attributes);
11256   if (attributes)
11257     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11258
11259   /* Don't give this error when parsing tentatively.  This happens to
11260      work because we always parse this definitively once.  */
11261   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11262       && type_uses_auto (type_specified))
11263     {
11264       error ("invalid use of %<auto%> in conversion operator");
11265       return error_mark_node;
11266     }
11267
11268   return type_specified;
11269 }
11270
11271 /* Parse an (optional) conversion-declarator.
11272
11273    conversion-declarator:
11274      ptr-operator conversion-declarator [opt]
11275
11276    */
11277
11278 static cp_declarator *
11279 cp_parser_conversion_declarator_opt (cp_parser* parser)
11280 {
11281   enum tree_code code;
11282   tree class_type;
11283   cp_cv_quals cv_quals;
11284
11285   /* We don't know if there's a ptr-operator next, or not.  */
11286   cp_parser_parse_tentatively (parser);
11287   /* Try the ptr-operator.  */
11288   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11289   /* If it worked, look for more conversion-declarators.  */
11290   if (cp_parser_parse_definitely (parser))
11291     {
11292       cp_declarator *declarator;
11293
11294       /* Parse another optional declarator.  */
11295       declarator = cp_parser_conversion_declarator_opt (parser);
11296
11297       return cp_parser_make_indirect_declarator
11298         (code, class_type, cv_quals, declarator);
11299    }
11300
11301   return NULL;
11302 }
11303
11304 /* Parse an (optional) ctor-initializer.
11305
11306    ctor-initializer:
11307      : mem-initializer-list
11308
11309    Returns TRUE iff the ctor-initializer was actually present.  */
11310
11311 static bool
11312 cp_parser_ctor_initializer_opt (cp_parser* parser)
11313 {
11314   /* If the next token is not a `:', then there is no
11315      ctor-initializer.  */
11316   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11317     {
11318       /* Do default initialization of any bases and members.  */
11319       if (DECL_CONSTRUCTOR_P (current_function_decl))
11320         finish_mem_initializers (NULL_TREE);
11321
11322       return false;
11323     }
11324
11325   /* Consume the `:' token.  */
11326   cp_lexer_consume_token (parser->lexer);
11327   /* And the mem-initializer-list.  */
11328   cp_parser_mem_initializer_list (parser);
11329
11330   return true;
11331 }
11332
11333 /* Parse a mem-initializer-list.
11334
11335    mem-initializer-list:
11336      mem-initializer ... [opt]
11337      mem-initializer ... [opt] , mem-initializer-list  */
11338
11339 static void
11340 cp_parser_mem_initializer_list (cp_parser* parser)
11341 {
11342   tree mem_initializer_list = NULL_TREE;
11343   tree target_ctor = error_mark_node;
11344   cp_token *token = cp_lexer_peek_token (parser->lexer);
11345
11346   /* Let the semantic analysis code know that we are starting the
11347      mem-initializer-list.  */
11348   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11349     error_at (token->location,
11350               "only constructors take member initializers");
11351
11352   /* Loop through the list.  */
11353   while (true)
11354     {
11355       tree mem_initializer;
11356
11357       token = cp_lexer_peek_token (parser->lexer);
11358       /* Parse the mem-initializer.  */
11359       mem_initializer = cp_parser_mem_initializer (parser);
11360       /* If the next token is a `...', we're expanding member initializers. */
11361       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11362         {
11363           /* Consume the `...'. */
11364           cp_lexer_consume_token (parser->lexer);
11365
11366           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11367              can be expanded but members cannot. */
11368           if (mem_initializer != error_mark_node
11369               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11370             {
11371               error_at (token->location,
11372                         "cannot expand initializer for member %<%D%>",
11373                         TREE_PURPOSE (mem_initializer));
11374               mem_initializer = error_mark_node;
11375             }
11376
11377           /* Construct the pack expansion type. */
11378           if (mem_initializer != error_mark_node)
11379             mem_initializer = make_pack_expansion (mem_initializer);
11380         }
11381       if (target_ctor != error_mark_node
11382           && mem_initializer != error_mark_node)
11383         {
11384           error ("mem-initializer for %qD follows constructor delegation",
11385                  TREE_PURPOSE (mem_initializer));
11386           mem_initializer = error_mark_node;
11387         }
11388       /* Look for a target constructor. */
11389       if (mem_initializer != error_mark_node
11390           && TYPE_P (TREE_PURPOSE (mem_initializer))
11391           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11392         {
11393           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11394           if (mem_initializer_list)
11395             {
11396               error ("constructor delegation follows mem-initializer for %qD",
11397                      TREE_PURPOSE (mem_initializer_list));
11398               mem_initializer = error_mark_node;
11399             }
11400           target_ctor = mem_initializer;
11401         }
11402       /* Add it to the list, unless it was erroneous.  */
11403       if (mem_initializer != error_mark_node)
11404         {
11405           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11406           mem_initializer_list = mem_initializer;
11407         }
11408       /* If the next token is not a `,', we're done.  */
11409       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11410         break;
11411       /* Consume the `,' token.  */
11412       cp_lexer_consume_token (parser->lexer);
11413     }
11414
11415   /* Perform semantic analysis.  */
11416   if (DECL_CONSTRUCTOR_P (current_function_decl))
11417     finish_mem_initializers (mem_initializer_list);
11418 }
11419
11420 /* Parse a mem-initializer.
11421
11422    mem-initializer:
11423      mem-initializer-id ( expression-list [opt] )
11424      mem-initializer-id braced-init-list
11425
11426    GNU extension:
11427
11428    mem-initializer:
11429      ( expression-list [opt] )
11430
11431    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11432    class) or FIELD_DECL (for a non-static data member) to initialize;
11433    the TREE_VALUE is the expression-list.  An empty initialization
11434    list is represented by void_list_node.  */
11435
11436 static tree
11437 cp_parser_mem_initializer (cp_parser* parser)
11438 {
11439   tree mem_initializer_id;
11440   tree expression_list;
11441   tree member;
11442   cp_token *token = cp_lexer_peek_token (parser->lexer);
11443
11444   /* Find out what is being initialized.  */
11445   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11446     {
11447       permerror (token->location,
11448                  "anachronistic old-style base class initializer");
11449       mem_initializer_id = NULL_TREE;
11450     }
11451   else
11452     {
11453       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11454       if (mem_initializer_id == error_mark_node)
11455         return mem_initializer_id;
11456     }
11457   member = expand_member_init (mem_initializer_id);
11458   if (member && !DECL_P (member))
11459     in_base_initializer = 1;
11460
11461   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11462     {
11463       bool expr_non_constant_p;
11464       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11465       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11466       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11467       expression_list = build_tree_list (NULL_TREE, expression_list);
11468     }
11469   else
11470     {
11471       VEC(tree,gc)* vec;
11472       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11473                                                      /*cast_p=*/false,
11474                                                      /*allow_expansion_p=*/true,
11475                                                      /*non_constant_p=*/NULL);
11476       if (vec == NULL)
11477         return error_mark_node;
11478       expression_list = build_tree_list_vec (vec);
11479       release_tree_vector (vec);
11480     }
11481
11482   if (expression_list == error_mark_node)
11483     return error_mark_node;
11484   if (!expression_list)
11485     expression_list = void_type_node;
11486
11487   in_base_initializer = 0;
11488
11489   return member ? build_tree_list (member, expression_list) : error_mark_node;
11490 }
11491
11492 /* Parse a mem-initializer-id.
11493
11494    mem-initializer-id:
11495      :: [opt] nested-name-specifier [opt] class-name
11496      identifier
11497
11498    Returns a TYPE indicating the class to be initializer for the first
11499    production.  Returns an IDENTIFIER_NODE indicating the data member
11500    to be initialized for the second production.  */
11501
11502 static tree
11503 cp_parser_mem_initializer_id (cp_parser* parser)
11504 {
11505   bool global_scope_p;
11506   bool nested_name_specifier_p;
11507   bool template_p = false;
11508   tree id;
11509
11510   cp_token *token = cp_lexer_peek_token (parser->lexer);
11511
11512   /* `typename' is not allowed in this context ([temp.res]).  */
11513   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11514     {
11515       error_at (token->location, 
11516                 "keyword %<typename%> not allowed in this context (a qualified "
11517                 "member initializer is implicitly a type)");
11518       cp_lexer_consume_token (parser->lexer);
11519     }
11520   /* Look for the optional `::' operator.  */
11521   global_scope_p
11522     = (cp_parser_global_scope_opt (parser,
11523                                    /*current_scope_valid_p=*/false)
11524        != NULL_TREE);
11525   /* Look for the optional nested-name-specifier.  The simplest way to
11526      implement:
11527
11528        [temp.res]
11529
11530        The keyword `typename' is not permitted in a base-specifier or
11531        mem-initializer; in these contexts a qualified name that
11532        depends on a template-parameter is implicitly assumed to be a
11533        type name.
11534
11535      is to assume that we have seen the `typename' keyword at this
11536      point.  */
11537   nested_name_specifier_p
11538     = (cp_parser_nested_name_specifier_opt (parser,
11539                                             /*typename_keyword_p=*/true,
11540                                             /*check_dependency_p=*/true,
11541                                             /*type_p=*/true,
11542                                             /*is_declaration=*/true)
11543        != NULL_TREE);
11544   if (nested_name_specifier_p)
11545     template_p = cp_parser_optional_template_keyword (parser);
11546   /* If there is a `::' operator or a nested-name-specifier, then we
11547      are definitely looking for a class-name.  */
11548   if (global_scope_p || nested_name_specifier_p)
11549     return cp_parser_class_name (parser,
11550                                  /*typename_keyword_p=*/true,
11551                                  /*template_keyword_p=*/template_p,
11552                                  typename_type,
11553                                  /*check_dependency_p=*/true,
11554                                  /*class_head_p=*/false,
11555                                  /*is_declaration=*/true);
11556   /* Otherwise, we could also be looking for an ordinary identifier.  */
11557   cp_parser_parse_tentatively (parser);
11558   /* Try a class-name.  */
11559   id = cp_parser_class_name (parser,
11560                              /*typename_keyword_p=*/true,
11561                              /*template_keyword_p=*/false,
11562                              none_type,
11563                              /*check_dependency_p=*/true,
11564                              /*class_head_p=*/false,
11565                              /*is_declaration=*/true);
11566   /* If we found one, we're done.  */
11567   if (cp_parser_parse_definitely (parser))
11568     return id;
11569   /* Otherwise, look for an ordinary identifier.  */
11570   return cp_parser_identifier (parser);
11571 }
11572
11573 /* Overloading [gram.over] */
11574
11575 /* Parse an operator-function-id.
11576
11577    operator-function-id:
11578      operator operator
11579
11580    Returns an IDENTIFIER_NODE for the operator which is a
11581    human-readable spelling of the identifier, e.g., `operator +'.  */
11582
11583 static tree
11584 cp_parser_operator_function_id (cp_parser* parser)
11585 {
11586   /* Look for the `operator' keyword.  */
11587   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11588     return error_mark_node;
11589   /* And then the name of the operator itself.  */
11590   return cp_parser_operator (parser);
11591 }
11592
11593 /* Return an identifier node for a user-defined literal operator.
11594    The suffix identifier is chained to the operator name identifier.  */
11595
11596 static tree
11597 cp_literal_operator_id (const char* name)
11598 {
11599   tree identifier;
11600   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11601                               + strlen (name) + 10);
11602   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11603   identifier = get_identifier (buffer);
11604   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11605
11606   return identifier;
11607 }
11608
11609 /* Parse an operator.
11610
11611    operator:
11612      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11613      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11614      || ++ -- , ->* -> () []
11615
11616    GNU Extensions:
11617
11618    operator:
11619      <? >? <?= >?=
11620
11621    Returns an IDENTIFIER_NODE for the operator which is a
11622    human-readable spelling of the identifier, e.g., `operator +'.  */
11623
11624 static tree
11625 cp_parser_operator (cp_parser* parser)
11626 {
11627   tree id = NULL_TREE;
11628   cp_token *token;
11629
11630   /* Peek at the next token.  */
11631   token = cp_lexer_peek_token (parser->lexer);
11632   /* Figure out which operator we have.  */
11633   switch (token->type)
11634     {
11635     case CPP_KEYWORD:
11636       {
11637         enum tree_code op;
11638
11639         /* The keyword should be either `new' or `delete'.  */
11640         if (token->keyword == RID_NEW)
11641           op = NEW_EXPR;
11642         else if (token->keyword == RID_DELETE)
11643           op = DELETE_EXPR;
11644         else
11645           break;
11646
11647         /* Consume the `new' or `delete' token.  */
11648         cp_lexer_consume_token (parser->lexer);
11649
11650         /* Peek at the next token.  */
11651         token = cp_lexer_peek_token (parser->lexer);
11652         /* If it's a `[' token then this is the array variant of the
11653            operator.  */
11654         if (token->type == CPP_OPEN_SQUARE)
11655           {
11656             /* Consume the `[' token.  */
11657             cp_lexer_consume_token (parser->lexer);
11658             /* Look for the `]' token.  */
11659             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11660             id = ansi_opname (op == NEW_EXPR
11661                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11662           }
11663         /* Otherwise, we have the non-array variant.  */
11664         else
11665           id = ansi_opname (op);
11666
11667         return id;
11668       }
11669
11670     case CPP_PLUS:
11671       id = ansi_opname (PLUS_EXPR);
11672       break;
11673
11674     case CPP_MINUS:
11675       id = ansi_opname (MINUS_EXPR);
11676       break;
11677
11678     case CPP_MULT:
11679       id = ansi_opname (MULT_EXPR);
11680       break;
11681
11682     case CPP_DIV:
11683       id = ansi_opname (TRUNC_DIV_EXPR);
11684       break;
11685
11686     case CPP_MOD:
11687       id = ansi_opname (TRUNC_MOD_EXPR);
11688       break;
11689
11690     case CPP_XOR:
11691       id = ansi_opname (BIT_XOR_EXPR);
11692       break;
11693
11694     case CPP_AND:
11695       id = ansi_opname (BIT_AND_EXPR);
11696       break;
11697
11698     case CPP_OR:
11699       id = ansi_opname (BIT_IOR_EXPR);
11700       break;
11701
11702     case CPP_COMPL:
11703       id = ansi_opname (BIT_NOT_EXPR);
11704       break;
11705
11706     case CPP_NOT:
11707       id = ansi_opname (TRUTH_NOT_EXPR);
11708       break;
11709
11710     case CPP_EQ:
11711       id = ansi_assopname (NOP_EXPR);
11712       break;
11713
11714     case CPP_LESS:
11715       id = ansi_opname (LT_EXPR);
11716       break;
11717
11718     case CPP_GREATER:
11719       id = ansi_opname (GT_EXPR);
11720       break;
11721
11722     case CPP_PLUS_EQ:
11723       id = ansi_assopname (PLUS_EXPR);
11724       break;
11725
11726     case CPP_MINUS_EQ:
11727       id = ansi_assopname (MINUS_EXPR);
11728       break;
11729
11730     case CPP_MULT_EQ:
11731       id = ansi_assopname (MULT_EXPR);
11732       break;
11733
11734     case CPP_DIV_EQ:
11735       id = ansi_assopname (TRUNC_DIV_EXPR);
11736       break;
11737
11738     case CPP_MOD_EQ:
11739       id = ansi_assopname (TRUNC_MOD_EXPR);
11740       break;
11741
11742     case CPP_XOR_EQ:
11743       id = ansi_assopname (BIT_XOR_EXPR);
11744       break;
11745
11746     case CPP_AND_EQ:
11747       id = ansi_assopname (BIT_AND_EXPR);
11748       break;
11749
11750     case CPP_OR_EQ:
11751       id = ansi_assopname (BIT_IOR_EXPR);
11752       break;
11753
11754     case CPP_LSHIFT:
11755       id = ansi_opname (LSHIFT_EXPR);
11756       break;
11757
11758     case CPP_RSHIFT:
11759       id = ansi_opname (RSHIFT_EXPR);
11760       break;
11761
11762     case CPP_LSHIFT_EQ:
11763       id = ansi_assopname (LSHIFT_EXPR);
11764       break;
11765
11766     case CPP_RSHIFT_EQ:
11767       id = ansi_assopname (RSHIFT_EXPR);
11768       break;
11769
11770     case CPP_EQ_EQ:
11771       id = ansi_opname (EQ_EXPR);
11772       break;
11773
11774     case CPP_NOT_EQ:
11775       id = ansi_opname (NE_EXPR);
11776       break;
11777
11778     case CPP_LESS_EQ:
11779       id = ansi_opname (LE_EXPR);
11780       break;
11781
11782     case CPP_GREATER_EQ:
11783       id = ansi_opname (GE_EXPR);
11784       break;
11785
11786     case CPP_AND_AND:
11787       id = ansi_opname (TRUTH_ANDIF_EXPR);
11788       break;
11789
11790     case CPP_OR_OR:
11791       id = ansi_opname (TRUTH_ORIF_EXPR);
11792       break;
11793
11794     case CPP_PLUS_PLUS:
11795       id = ansi_opname (POSTINCREMENT_EXPR);
11796       break;
11797
11798     case CPP_MINUS_MINUS:
11799       id = ansi_opname (PREDECREMENT_EXPR);
11800       break;
11801
11802     case CPP_COMMA:
11803       id = ansi_opname (COMPOUND_EXPR);
11804       break;
11805
11806     case CPP_DEREF_STAR:
11807       id = ansi_opname (MEMBER_REF);
11808       break;
11809
11810     case CPP_DEREF:
11811       id = ansi_opname (COMPONENT_REF);
11812       break;
11813
11814     case CPP_OPEN_PAREN:
11815       /* Consume the `('.  */
11816       cp_lexer_consume_token (parser->lexer);
11817       /* Look for the matching `)'.  */
11818       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11819       return ansi_opname (CALL_EXPR);
11820
11821     case CPP_OPEN_SQUARE:
11822       /* Consume the `['.  */
11823       cp_lexer_consume_token (parser->lexer);
11824       /* Look for the matching `]'.  */
11825       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11826       return ansi_opname (ARRAY_REF);
11827
11828     case CPP_STRING:
11829       if (cxx_dialect == cxx98)
11830         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11831       if (TREE_STRING_LENGTH (token->u.value) > 2)
11832         {
11833           error ("expected empty string after %<operator%> keyword");
11834           return error_mark_node;
11835         }
11836       /* Consume the string.  */
11837       cp_lexer_consume_token (parser->lexer);
11838       /* Look for the suffix identifier.  */
11839       token = cp_lexer_peek_token (parser->lexer);
11840       if (token->type == CPP_NAME)
11841         {
11842           id = cp_parser_identifier (parser);
11843           if (id != error_mark_node)
11844             {
11845               const char *name = IDENTIFIER_POINTER (id);
11846               return cp_literal_operator_id (name);
11847             }
11848         }
11849       else
11850         {
11851           error ("expected suffix identifier");
11852           return error_mark_node;
11853         }
11854
11855     case CPP_STRING_USERDEF:
11856       error ("missing space between %<\"\"%> and suffix identifier");
11857       return error_mark_node;
11858
11859     default:
11860       /* Anything else is an error.  */
11861       break;
11862     }
11863
11864   /* If we have selected an identifier, we need to consume the
11865      operator token.  */
11866   if (id)
11867     cp_lexer_consume_token (parser->lexer);
11868   /* Otherwise, no valid operator name was present.  */
11869   else
11870     {
11871       cp_parser_error (parser, "expected operator");
11872       id = error_mark_node;
11873     }
11874
11875   return id;
11876 }
11877
11878 /* Parse a template-declaration.
11879
11880    template-declaration:
11881      export [opt] template < template-parameter-list > declaration
11882
11883    If MEMBER_P is TRUE, this template-declaration occurs within a
11884    class-specifier.
11885
11886    The grammar rule given by the standard isn't correct.  What
11887    is really meant is:
11888
11889    template-declaration:
11890      export [opt] template-parameter-list-seq
11891        decl-specifier-seq [opt] init-declarator [opt] ;
11892      export [opt] template-parameter-list-seq
11893        function-definition
11894
11895    template-parameter-list-seq:
11896      template-parameter-list-seq [opt]
11897      template < template-parameter-list >  */
11898
11899 static void
11900 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11901 {
11902   /* Check for `export'.  */
11903   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11904     {
11905       /* Consume the `export' token.  */
11906       cp_lexer_consume_token (parser->lexer);
11907       /* Warn that we do not support `export'.  */
11908       warning (0, "keyword %<export%> not implemented, and will be ignored");
11909     }
11910
11911   cp_parser_template_declaration_after_export (parser, member_p);
11912 }
11913
11914 /* Parse a template-parameter-list.
11915
11916    template-parameter-list:
11917      template-parameter
11918      template-parameter-list , template-parameter
11919
11920    Returns a TREE_LIST.  Each node represents a template parameter.
11921    The nodes are connected via their TREE_CHAINs.  */
11922
11923 static tree
11924 cp_parser_template_parameter_list (cp_parser* parser)
11925 {
11926   tree parameter_list = NULL_TREE;
11927
11928   begin_template_parm_list ();
11929
11930   /* The loop below parses the template parms.  We first need to know
11931      the total number of template parms to be able to compute proper
11932      canonical types of each dependent type. So after the loop, when
11933      we know the total number of template parms,
11934      end_template_parm_list computes the proper canonical types and
11935      fixes up the dependent types accordingly.  */
11936   while (true)
11937     {
11938       tree parameter;
11939       bool is_non_type;
11940       bool is_parameter_pack;
11941       location_t parm_loc;
11942
11943       /* Parse the template-parameter.  */
11944       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11945       parameter = cp_parser_template_parameter (parser, 
11946                                                 &is_non_type,
11947                                                 &is_parameter_pack);
11948       /* Add it to the list.  */
11949       if (parameter != error_mark_node)
11950         parameter_list = process_template_parm (parameter_list,
11951                                                 parm_loc,
11952                                                 parameter,
11953                                                 is_non_type,
11954                                                 is_parameter_pack,
11955                                                 0);
11956       else
11957        {
11958          tree err_parm = build_tree_list (parameter, parameter);
11959          parameter_list = chainon (parameter_list, err_parm);
11960        }
11961
11962       /* If the next token is not a `,', we're done.  */
11963       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11964         break;
11965       /* Otherwise, consume the `,' token.  */
11966       cp_lexer_consume_token (parser->lexer);
11967     }
11968
11969   return end_template_parm_list (parameter_list);
11970 }
11971
11972 /* Parse a template-parameter.
11973
11974    template-parameter:
11975      type-parameter
11976      parameter-declaration
11977
11978    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11979    the parameter.  The TREE_PURPOSE is the default value, if any.
11980    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11981    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11982    set to true iff this parameter is a parameter pack. */
11983
11984 static tree
11985 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11986                               bool *is_parameter_pack)
11987 {
11988   cp_token *token;
11989   cp_parameter_declarator *parameter_declarator;
11990   cp_declarator *id_declarator;
11991   tree parm;
11992
11993   /* Assume it is a type parameter or a template parameter.  */
11994   *is_non_type = false;
11995   /* Assume it not a parameter pack. */
11996   *is_parameter_pack = false;
11997   /* Peek at the next token.  */
11998   token = cp_lexer_peek_token (parser->lexer);
11999   /* If it is `class' or `template', we have a type-parameter.  */
12000   if (token->keyword == RID_TEMPLATE)
12001     return cp_parser_type_parameter (parser, is_parameter_pack);
12002   /* If it is `class' or `typename' we do not know yet whether it is a
12003      type parameter or a non-type parameter.  Consider:
12004
12005        template <typename T, typename T::X X> ...
12006
12007      or:
12008
12009        template <class C, class D*> ...
12010
12011      Here, the first parameter is a type parameter, and the second is
12012      a non-type parameter.  We can tell by looking at the token after
12013      the identifier -- if it is a `,', `=', or `>' then we have a type
12014      parameter.  */
12015   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12016     {
12017       /* Peek at the token after `class' or `typename'.  */
12018       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12019       /* If it's an ellipsis, we have a template type parameter
12020          pack. */
12021       if (token->type == CPP_ELLIPSIS)
12022         return cp_parser_type_parameter (parser, is_parameter_pack);
12023       /* If it's an identifier, skip it.  */
12024       if (token->type == CPP_NAME)
12025         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12026       /* Now, see if the token looks like the end of a template
12027          parameter.  */
12028       if (token->type == CPP_COMMA
12029           || token->type == CPP_EQ
12030           || token->type == CPP_GREATER)
12031         return cp_parser_type_parameter (parser, is_parameter_pack);
12032     }
12033
12034   /* Otherwise, it is a non-type parameter.
12035
12036      [temp.param]
12037
12038      When parsing a default template-argument for a non-type
12039      template-parameter, the first non-nested `>' is taken as the end
12040      of the template parameter-list rather than a greater-than
12041      operator.  */
12042   *is_non_type = true;
12043   parameter_declarator
12044      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12045                                         /*parenthesized_p=*/NULL);
12046
12047   /* If the parameter declaration is marked as a parameter pack, set
12048      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12049      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12050      grokdeclarator. */
12051   if (parameter_declarator
12052       && parameter_declarator->declarator
12053       && parameter_declarator->declarator->parameter_pack_p)
12054     {
12055       *is_parameter_pack = true;
12056       parameter_declarator->declarator->parameter_pack_p = false;
12057     }
12058
12059   /* If the next token is an ellipsis, and we don't already have it
12060      marked as a parameter pack, then we have a parameter pack (that
12061      has no declarator).  */
12062   if (!*is_parameter_pack
12063       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12064       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12065     {
12066       /* Consume the `...'.  */
12067       cp_lexer_consume_token (parser->lexer);
12068       maybe_warn_variadic_templates ();
12069       
12070       *is_parameter_pack = true;
12071     }
12072   /* We might end up with a pack expansion as the type of the non-type
12073      template parameter, in which case this is a non-type template
12074      parameter pack.  */
12075   else if (parameter_declarator
12076            && parameter_declarator->decl_specifiers.type
12077            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12078     {
12079       *is_parameter_pack = true;
12080       parameter_declarator->decl_specifiers.type = 
12081         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12082     }
12083
12084   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12085     {
12086       /* Parameter packs cannot have default arguments.  However, a
12087          user may try to do so, so we'll parse them and give an
12088          appropriate diagnostic here.  */
12089
12090       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12091       
12092       /* Find the name of the parameter pack.  */     
12093       id_declarator = parameter_declarator->declarator;
12094       while (id_declarator && id_declarator->kind != cdk_id)
12095         id_declarator = id_declarator->declarator;
12096       
12097       if (id_declarator && id_declarator->kind == cdk_id)
12098         error_at (start_token->location,
12099                   "template parameter pack %qD cannot have a default argument",
12100                   id_declarator->u.id.unqualified_name);
12101       else
12102         error_at (start_token->location,
12103                   "template parameter pack cannot have a default argument");
12104       
12105       /* Parse the default argument, but throw away the result.  */
12106       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12107     }
12108
12109   parm = grokdeclarator (parameter_declarator->declarator,
12110                          &parameter_declarator->decl_specifiers,
12111                          TPARM, /*initialized=*/0,
12112                          /*attrlist=*/NULL);
12113   if (parm == error_mark_node)
12114     return error_mark_node;
12115
12116   return build_tree_list (parameter_declarator->default_argument, parm);
12117 }
12118
12119 /* Parse a type-parameter.
12120
12121    type-parameter:
12122      class identifier [opt]
12123      class identifier [opt] = type-id
12124      typename identifier [opt]
12125      typename identifier [opt] = type-id
12126      template < template-parameter-list > class identifier [opt]
12127      template < template-parameter-list > class identifier [opt]
12128        = id-expression
12129
12130    GNU Extension (variadic templates):
12131
12132    type-parameter:
12133      class ... identifier [opt]
12134      typename ... identifier [opt]
12135
12136    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12137    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12138    the declaration of the parameter.
12139
12140    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12141
12142 static tree
12143 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12144 {
12145   cp_token *token;
12146   tree parameter;
12147
12148   /* Look for a keyword to tell us what kind of parameter this is.  */
12149   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12150   if (!token)
12151     return error_mark_node;
12152
12153   switch (token->keyword)
12154     {
12155     case RID_CLASS:
12156     case RID_TYPENAME:
12157       {
12158         tree identifier;
12159         tree default_argument;
12160
12161         /* If the next token is an ellipsis, we have a template
12162            argument pack. */
12163         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12164           {
12165             /* Consume the `...' token. */
12166             cp_lexer_consume_token (parser->lexer);
12167             maybe_warn_variadic_templates ();
12168
12169             *is_parameter_pack = true;
12170           }
12171
12172         /* If the next token is an identifier, then it names the
12173            parameter.  */
12174         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12175           identifier = cp_parser_identifier (parser);
12176         else
12177           identifier = NULL_TREE;
12178
12179         /* Create the parameter.  */
12180         parameter = finish_template_type_parm (class_type_node, identifier);
12181
12182         /* If the next token is an `=', we have a default argument.  */
12183         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12184           {
12185             /* Consume the `=' token.  */
12186             cp_lexer_consume_token (parser->lexer);
12187             /* Parse the default-argument.  */
12188             push_deferring_access_checks (dk_no_deferred);
12189             default_argument = cp_parser_type_id (parser);
12190
12191             /* Template parameter packs cannot have default
12192                arguments. */
12193             if (*is_parameter_pack)
12194               {
12195                 if (identifier)
12196                   error_at (token->location,
12197                             "template parameter pack %qD cannot have a "
12198                             "default argument", identifier);
12199                 else
12200                   error_at (token->location,
12201                             "template parameter packs cannot have "
12202                             "default arguments");
12203                 default_argument = NULL_TREE;
12204               }
12205             pop_deferring_access_checks ();
12206           }
12207         else
12208           default_argument = NULL_TREE;
12209
12210         /* Create the combined representation of the parameter and the
12211            default argument.  */
12212         parameter = build_tree_list (default_argument, parameter);
12213       }
12214       break;
12215
12216     case RID_TEMPLATE:
12217       {
12218         tree identifier;
12219         tree default_argument;
12220
12221         /* Look for the `<'.  */
12222         cp_parser_require (parser, CPP_LESS, RT_LESS);
12223         /* Parse the template-parameter-list.  */
12224         cp_parser_template_parameter_list (parser);
12225         /* Look for the `>'.  */
12226         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12227         /* Look for the `class' keyword.  */
12228         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12229         /* If the next token is an ellipsis, we have a template
12230            argument pack. */
12231         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12232           {
12233             /* Consume the `...' token. */
12234             cp_lexer_consume_token (parser->lexer);
12235             maybe_warn_variadic_templates ();
12236
12237             *is_parameter_pack = true;
12238           }
12239         /* If the next token is an `=', then there is a
12240            default-argument.  If the next token is a `>', we are at
12241            the end of the parameter-list.  If the next token is a `,',
12242            then we are at the end of this parameter.  */
12243         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12244             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12245             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12246           {
12247             identifier = cp_parser_identifier (parser);
12248             /* Treat invalid names as if the parameter were nameless.  */
12249             if (identifier == error_mark_node)
12250               identifier = NULL_TREE;
12251           }
12252         else
12253           identifier = NULL_TREE;
12254
12255         /* Create the template parameter.  */
12256         parameter = finish_template_template_parm (class_type_node,
12257                                                    identifier);
12258
12259         /* If the next token is an `=', then there is a
12260            default-argument.  */
12261         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12262           {
12263             bool is_template;
12264
12265             /* Consume the `='.  */
12266             cp_lexer_consume_token (parser->lexer);
12267             /* Parse the id-expression.  */
12268             push_deferring_access_checks (dk_no_deferred);
12269             /* save token before parsing the id-expression, for error
12270                reporting */
12271             token = cp_lexer_peek_token (parser->lexer);
12272             default_argument
12273               = cp_parser_id_expression (parser,
12274                                          /*template_keyword_p=*/false,
12275                                          /*check_dependency_p=*/true,
12276                                          /*template_p=*/&is_template,
12277                                          /*declarator_p=*/false,
12278                                          /*optional_p=*/false);
12279             if (TREE_CODE (default_argument) == TYPE_DECL)
12280               /* If the id-expression was a template-id that refers to
12281                  a template-class, we already have the declaration here,
12282                  so no further lookup is needed.  */
12283                  ;
12284             else
12285               /* Look up the name.  */
12286               default_argument
12287                 = cp_parser_lookup_name (parser, default_argument,
12288                                          none_type,
12289                                          /*is_template=*/is_template,
12290                                          /*is_namespace=*/false,
12291                                          /*check_dependency=*/true,
12292                                          /*ambiguous_decls=*/NULL,
12293                                          token->location);
12294             /* See if the default argument is valid.  */
12295             default_argument
12296               = check_template_template_default_arg (default_argument);
12297
12298             /* Template parameter packs cannot have default
12299                arguments. */
12300             if (*is_parameter_pack)
12301               {
12302                 if (identifier)
12303                   error_at (token->location,
12304                             "template parameter pack %qD cannot "
12305                             "have a default argument",
12306                             identifier);
12307                 else
12308                   error_at (token->location, "template parameter packs cannot "
12309                             "have default arguments");
12310                 default_argument = NULL_TREE;
12311               }
12312             pop_deferring_access_checks ();
12313           }
12314         else
12315           default_argument = NULL_TREE;
12316
12317         /* Create the combined representation of the parameter and the
12318            default argument.  */
12319         parameter = build_tree_list (default_argument, parameter);
12320       }
12321       break;
12322
12323     default:
12324       gcc_unreachable ();
12325       break;
12326     }
12327
12328   return parameter;
12329 }
12330
12331 /* Parse a template-id.
12332
12333    template-id:
12334      template-name < template-argument-list [opt] >
12335
12336    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12337    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12338    returned.  Otherwise, if the template-name names a function, or set
12339    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12340    names a class, returns a TYPE_DECL for the specialization.
12341
12342    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12343    uninstantiated templates.  */
12344
12345 static tree
12346 cp_parser_template_id (cp_parser *parser,
12347                        bool template_keyword_p,
12348                        bool check_dependency_p,
12349                        bool is_declaration)
12350 {
12351   int i;
12352   tree templ;
12353   tree arguments;
12354   tree template_id;
12355   cp_token_position start_of_id = 0;
12356   deferred_access_check *chk;
12357   VEC (deferred_access_check,gc) *access_check;
12358   cp_token *next_token = NULL, *next_token_2 = NULL;
12359   bool is_identifier;
12360
12361   /* If the next token corresponds to a template-id, there is no need
12362      to reparse it.  */
12363   next_token = cp_lexer_peek_token (parser->lexer);
12364   if (next_token->type == CPP_TEMPLATE_ID)
12365     {
12366       struct tree_check *check_value;
12367
12368       /* Get the stored value.  */
12369       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12370       /* Perform any access checks that were deferred.  */
12371       access_check = check_value->checks;
12372       if (access_check)
12373         {
12374           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12375             perform_or_defer_access_check (chk->binfo,
12376                                            chk->decl,
12377                                            chk->diag_decl);
12378         }
12379       /* Return the stored value.  */
12380       return check_value->value;
12381     }
12382
12383   /* Avoid performing name lookup if there is no possibility of
12384      finding a template-id.  */
12385   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12386       || (next_token->type == CPP_NAME
12387           && !cp_parser_nth_token_starts_template_argument_list_p
12388                (parser, 2)))
12389     {
12390       cp_parser_error (parser, "expected template-id");
12391       return error_mark_node;
12392     }
12393
12394   /* Remember where the template-id starts.  */
12395   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12396     start_of_id = cp_lexer_token_position (parser->lexer, false);
12397
12398   push_deferring_access_checks (dk_deferred);
12399
12400   /* Parse the template-name.  */
12401   is_identifier = false;
12402   templ = cp_parser_template_name (parser, template_keyword_p,
12403                                    check_dependency_p,
12404                                    is_declaration,
12405                                    &is_identifier);
12406   if (templ == error_mark_node || is_identifier)
12407     {
12408       pop_deferring_access_checks ();
12409       return templ;
12410     }
12411
12412   /* If we find the sequence `[:' after a template-name, it's probably
12413      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12414      parse correctly the argument list.  */
12415   next_token = cp_lexer_peek_token (parser->lexer);
12416   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12417   if (next_token->type == CPP_OPEN_SQUARE
12418       && next_token->flags & DIGRAPH
12419       && next_token_2->type == CPP_COLON
12420       && !(next_token_2->flags & PREV_WHITE))
12421     {
12422       cp_parser_parse_tentatively (parser);
12423       /* Change `:' into `::'.  */
12424       next_token_2->type = CPP_SCOPE;
12425       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12426          CPP_LESS.  */
12427       cp_lexer_consume_token (parser->lexer);
12428
12429       /* Parse the arguments.  */
12430       arguments = cp_parser_enclosed_template_argument_list (parser);
12431       if (!cp_parser_parse_definitely (parser))
12432         {
12433           /* If we couldn't parse an argument list, then we revert our changes
12434              and return simply an error. Maybe this is not a template-id
12435              after all.  */
12436           next_token_2->type = CPP_COLON;
12437           cp_parser_error (parser, "expected %<<%>");
12438           pop_deferring_access_checks ();
12439           return error_mark_node;
12440         }
12441       /* Otherwise, emit an error about the invalid digraph, but continue
12442          parsing because we got our argument list.  */
12443       if (permerror (next_token->location,
12444                      "%<<::%> cannot begin a template-argument list"))
12445         {
12446           static bool hint = false;
12447           inform (next_token->location,
12448                   "%<<:%> is an alternate spelling for %<[%>."
12449                   " Insert whitespace between %<<%> and %<::%>");
12450           if (!hint && !flag_permissive)
12451             {
12452               inform (next_token->location, "(if you use %<-fpermissive%>"
12453                       " G++ will accept your code)");
12454               hint = true;
12455             }
12456         }
12457     }
12458   else
12459     {
12460       /* Look for the `<' that starts the template-argument-list.  */
12461       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12462         {
12463           pop_deferring_access_checks ();
12464           return error_mark_node;
12465         }
12466       /* Parse the arguments.  */
12467       arguments = cp_parser_enclosed_template_argument_list (parser);
12468     }
12469
12470   /* Build a representation of the specialization.  */
12471   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12472     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12473   else if (DECL_TYPE_TEMPLATE_P (templ)
12474            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12475     {
12476       bool entering_scope;
12477       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12478          template (rather than some instantiation thereof) only if
12479          is not nested within some other construct.  For example, in
12480          "template <typename T> void f(T) { A<T>::", A<T> is just an
12481          instantiation of A.  */
12482       entering_scope = (template_parm_scope_p ()
12483                         && cp_lexer_next_token_is (parser->lexer,
12484                                                    CPP_SCOPE));
12485       template_id
12486         = finish_template_type (templ, arguments, entering_scope);
12487     }
12488   else
12489     {
12490       /* If it's not a class-template or a template-template, it should be
12491          a function-template.  */
12492       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12493                    || TREE_CODE (templ) == OVERLOAD
12494                    || BASELINK_P (templ)));
12495
12496       template_id = lookup_template_function (templ, arguments);
12497     }
12498
12499   /* If parsing tentatively, replace the sequence of tokens that makes
12500      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12501      should we re-parse the token stream, we will not have to repeat
12502      the effort required to do the parse, nor will we issue duplicate
12503      error messages about problems during instantiation of the
12504      template.  */
12505   if (start_of_id)
12506     {
12507       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12508
12509       /* Reset the contents of the START_OF_ID token.  */
12510       token->type = CPP_TEMPLATE_ID;
12511       /* Retrieve any deferred checks.  Do not pop this access checks yet
12512          so the memory will not be reclaimed during token replacing below.  */
12513       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12514       token->u.tree_check_value->value = template_id;
12515       token->u.tree_check_value->checks = get_deferred_access_checks ();
12516       token->keyword = RID_MAX;
12517
12518       /* Purge all subsequent tokens.  */
12519       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12520
12521       /* ??? Can we actually assume that, if template_id ==
12522          error_mark_node, we will have issued a diagnostic to the
12523          user, as opposed to simply marking the tentative parse as
12524          failed?  */
12525       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12526         error_at (token->location, "parse error in template argument list");
12527     }
12528
12529   pop_deferring_access_checks ();
12530   return template_id;
12531 }
12532
12533 /* Parse a template-name.
12534
12535    template-name:
12536      identifier
12537
12538    The standard should actually say:
12539
12540    template-name:
12541      identifier
12542      operator-function-id
12543
12544    A defect report has been filed about this issue.
12545
12546    A conversion-function-id cannot be a template name because they cannot
12547    be part of a template-id. In fact, looking at this code:
12548
12549    a.operator K<int>()
12550
12551    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12552    It is impossible to call a templated conversion-function-id with an
12553    explicit argument list, since the only allowed template parameter is
12554    the type to which it is converting.
12555
12556    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12557    `template' keyword, in a construction like:
12558
12559      T::template f<3>()
12560
12561    In that case `f' is taken to be a template-name, even though there
12562    is no way of knowing for sure.
12563
12564    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12565    name refers to a set of overloaded functions, at least one of which
12566    is a template, or an IDENTIFIER_NODE with the name of the template,
12567    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12568    names are looked up inside uninstantiated templates.  */
12569
12570 static tree
12571 cp_parser_template_name (cp_parser* parser,
12572                          bool template_keyword_p,
12573                          bool check_dependency_p,
12574                          bool is_declaration,
12575                          bool *is_identifier)
12576 {
12577   tree identifier;
12578   tree decl;
12579   tree fns;
12580   cp_token *token = cp_lexer_peek_token (parser->lexer);
12581
12582   /* If the next token is `operator', then we have either an
12583      operator-function-id or a conversion-function-id.  */
12584   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12585     {
12586       /* We don't know whether we're looking at an
12587          operator-function-id or a conversion-function-id.  */
12588       cp_parser_parse_tentatively (parser);
12589       /* Try an operator-function-id.  */
12590       identifier = cp_parser_operator_function_id (parser);
12591       /* If that didn't work, try a conversion-function-id.  */
12592       if (!cp_parser_parse_definitely (parser))
12593         {
12594           cp_parser_error (parser, "expected template-name");
12595           return error_mark_node;
12596         }
12597     }
12598   /* Look for the identifier.  */
12599   else
12600     identifier = cp_parser_identifier (parser);
12601
12602   /* If we didn't find an identifier, we don't have a template-id.  */
12603   if (identifier == error_mark_node)
12604     return error_mark_node;
12605
12606   /* If the name immediately followed the `template' keyword, then it
12607      is a template-name.  However, if the next token is not `<', then
12608      we do not treat it as a template-name, since it is not being used
12609      as part of a template-id.  This enables us to handle constructs
12610      like:
12611
12612        template <typename T> struct S { S(); };
12613        template <typename T> S<T>::S();
12614
12615      correctly.  We would treat `S' as a template -- if it were `S<T>'
12616      -- but we do not if there is no `<'.  */
12617
12618   if (processing_template_decl
12619       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12620     {
12621       /* In a declaration, in a dependent context, we pretend that the
12622          "template" keyword was present in order to improve error
12623          recovery.  For example, given:
12624
12625            template <typename T> void f(T::X<int>);
12626
12627          we want to treat "X<int>" as a template-id.  */
12628       if (is_declaration
12629           && !template_keyword_p
12630           && parser->scope && TYPE_P (parser->scope)
12631           && check_dependency_p
12632           && dependent_scope_p (parser->scope)
12633           /* Do not do this for dtors (or ctors), since they never
12634              need the template keyword before their name.  */
12635           && !constructor_name_p (identifier, parser->scope))
12636         {
12637           cp_token_position start = 0;
12638
12639           /* Explain what went wrong.  */
12640           error_at (token->location, "non-template %qD used as template",
12641                     identifier);
12642           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12643                   parser->scope, identifier);
12644           /* If parsing tentatively, find the location of the "<" token.  */
12645           if (cp_parser_simulate_error (parser))
12646             start = cp_lexer_token_position (parser->lexer, true);
12647           /* Parse the template arguments so that we can issue error
12648              messages about them.  */
12649           cp_lexer_consume_token (parser->lexer);
12650           cp_parser_enclosed_template_argument_list (parser);
12651           /* Skip tokens until we find a good place from which to
12652              continue parsing.  */
12653           cp_parser_skip_to_closing_parenthesis (parser,
12654                                                  /*recovering=*/true,
12655                                                  /*or_comma=*/true,
12656                                                  /*consume_paren=*/false);
12657           /* If parsing tentatively, permanently remove the
12658              template argument list.  That will prevent duplicate
12659              error messages from being issued about the missing
12660              "template" keyword.  */
12661           if (start)
12662             cp_lexer_purge_tokens_after (parser->lexer, start);
12663           if (is_identifier)
12664             *is_identifier = true;
12665           return identifier;
12666         }
12667
12668       /* If the "template" keyword is present, then there is generally
12669          no point in doing name-lookup, so we just return IDENTIFIER.
12670          But, if the qualifying scope is non-dependent then we can
12671          (and must) do name-lookup normally.  */
12672       if (template_keyword_p
12673           && (!parser->scope
12674               || (TYPE_P (parser->scope)
12675                   && dependent_type_p (parser->scope))))
12676         return identifier;
12677     }
12678
12679   /* Look up the name.  */
12680   decl = cp_parser_lookup_name (parser, identifier,
12681                                 none_type,
12682                                 /*is_template=*/true,
12683                                 /*is_namespace=*/false,
12684                                 check_dependency_p,
12685                                 /*ambiguous_decls=*/NULL,
12686                                 token->location);
12687
12688   /* If DECL is a template, then the name was a template-name.  */
12689   if (TREE_CODE (decl) == TEMPLATE_DECL)
12690     ;
12691   else
12692     {
12693       tree fn = NULL_TREE;
12694
12695       /* The standard does not explicitly indicate whether a name that
12696          names a set of overloaded declarations, some of which are
12697          templates, is a template-name.  However, such a name should
12698          be a template-name; otherwise, there is no way to form a
12699          template-id for the overloaded templates.  */
12700       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12701       if (TREE_CODE (fns) == OVERLOAD)
12702         for (fn = fns; fn; fn = OVL_NEXT (fn))
12703           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12704             break;
12705
12706       if (!fn)
12707         {
12708           /* The name does not name a template.  */
12709           cp_parser_error (parser, "expected template-name");
12710           return error_mark_node;
12711         }
12712     }
12713
12714   /* If DECL is dependent, and refers to a function, then just return
12715      its name; we will look it up again during template instantiation.  */
12716   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12717     {
12718       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12719       if (TYPE_P (scope) && dependent_type_p (scope))
12720         return identifier;
12721     }
12722
12723   return decl;
12724 }
12725
12726 /* Parse a template-argument-list.
12727
12728    template-argument-list:
12729      template-argument ... [opt]
12730      template-argument-list , template-argument ... [opt]
12731
12732    Returns a TREE_VEC containing the arguments.  */
12733
12734 static tree
12735 cp_parser_template_argument_list (cp_parser* parser)
12736 {
12737   tree fixed_args[10];
12738   unsigned n_args = 0;
12739   unsigned alloced = 10;
12740   tree *arg_ary = fixed_args;
12741   tree vec;
12742   bool saved_in_template_argument_list_p;
12743   bool saved_ice_p;
12744   bool saved_non_ice_p;
12745
12746   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12747   parser->in_template_argument_list_p = true;
12748   /* Even if the template-id appears in an integral
12749      constant-expression, the contents of the argument list do
12750      not.  */
12751   saved_ice_p = parser->integral_constant_expression_p;
12752   parser->integral_constant_expression_p = false;
12753   saved_non_ice_p = parser->non_integral_constant_expression_p;
12754   parser->non_integral_constant_expression_p = false;
12755
12756   /* Parse the arguments.  */
12757   do
12758     {
12759       tree argument;
12760
12761       if (n_args)
12762         /* Consume the comma.  */
12763         cp_lexer_consume_token (parser->lexer);
12764
12765       /* Parse the template-argument.  */
12766       argument = cp_parser_template_argument (parser);
12767
12768       /* If the next token is an ellipsis, we're expanding a template
12769          argument pack. */
12770       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12771         {
12772           if (argument == error_mark_node)
12773             {
12774               cp_token *token = cp_lexer_peek_token (parser->lexer);
12775               error_at (token->location,
12776                         "expected parameter pack before %<...%>");
12777             }
12778           /* Consume the `...' token. */
12779           cp_lexer_consume_token (parser->lexer);
12780
12781           /* Make the argument into a TYPE_PACK_EXPANSION or
12782              EXPR_PACK_EXPANSION. */
12783           argument = make_pack_expansion (argument);
12784         }
12785
12786       if (n_args == alloced)
12787         {
12788           alloced *= 2;
12789
12790           if (arg_ary == fixed_args)
12791             {
12792               arg_ary = XNEWVEC (tree, alloced);
12793               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12794             }
12795           else
12796             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12797         }
12798       arg_ary[n_args++] = argument;
12799     }
12800   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12801
12802   vec = make_tree_vec (n_args);
12803
12804   while (n_args--)
12805     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12806
12807   if (arg_ary != fixed_args)
12808     free (arg_ary);
12809   parser->non_integral_constant_expression_p = saved_non_ice_p;
12810   parser->integral_constant_expression_p = saved_ice_p;
12811   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12812 #ifdef ENABLE_CHECKING
12813   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12814 #endif
12815   return vec;
12816 }
12817
12818 /* Parse a template-argument.
12819
12820    template-argument:
12821      assignment-expression
12822      type-id
12823      id-expression
12824
12825    The representation is that of an assignment-expression, type-id, or
12826    id-expression -- except that the qualified id-expression is
12827    evaluated, so that the value returned is either a DECL or an
12828    OVERLOAD.
12829
12830    Although the standard says "assignment-expression", it forbids
12831    throw-expressions or assignments in the template argument.
12832    Therefore, we use "conditional-expression" instead.  */
12833
12834 static tree
12835 cp_parser_template_argument (cp_parser* parser)
12836 {
12837   tree argument;
12838   bool template_p;
12839   bool address_p;
12840   bool maybe_type_id = false;
12841   cp_token *token = NULL, *argument_start_token = NULL;
12842   cp_id_kind idk;
12843
12844   /* There's really no way to know what we're looking at, so we just
12845      try each alternative in order.
12846
12847        [temp.arg]
12848
12849        In a template-argument, an ambiguity between a type-id and an
12850        expression is resolved to a type-id, regardless of the form of
12851        the corresponding template-parameter.
12852
12853      Therefore, we try a type-id first.  */
12854   cp_parser_parse_tentatively (parser);
12855   argument = cp_parser_template_type_arg (parser);
12856   /* If there was no error parsing the type-id but the next token is a
12857      '>>', our behavior depends on which dialect of C++ we're
12858      parsing. In C++98, we probably found a typo for '> >'. But there
12859      are type-id which are also valid expressions. For instance:
12860
12861      struct X { int operator >> (int); };
12862      template <int V> struct Foo {};
12863      Foo<X () >> 5> r;
12864
12865      Here 'X()' is a valid type-id of a function type, but the user just
12866      wanted to write the expression "X() >> 5". Thus, we remember that we
12867      found a valid type-id, but we still try to parse the argument as an
12868      expression to see what happens. 
12869
12870      In C++0x, the '>>' will be considered two separate '>'
12871      tokens.  */
12872   if (!cp_parser_error_occurred (parser)
12873       && cxx_dialect == cxx98
12874       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12875     {
12876       maybe_type_id = true;
12877       cp_parser_abort_tentative_parse (parser);
12878     }
12879   else
12880     {
12881       /* If the next token isn't a `,' or a `>', then this argument wasn't
12882       really finished. This means that the argument is not a valid
12883       type-id.  */
12884       if (!cp_parser_next_token_ends_template_argument_p (parser))
12885         cp_parser_error (parser, "expected template-argument");
12886       /* If that worked, we're done.  */
12887       if (cp_parser_parse_definitely (parser))
12888         return argument;
12889     }
12890   /* We're still not sure what the argument will be.  */
12891   cp_parser_parse_tentatively (parser);
12892   /* Try a template.  */
12893   argument_start_token = cp_lexer_peek_token (parser->lexer);
12894   argument = cp_parser_id_expression (parser,
12895                                       /*template_keyword_p=*/false,
12896                                       /*check_dependency_p=*/true,
12897                                       &template_p,
12898                                       /*declarator_p=*/false,
12899                                       /*optional_p=*/false);
12900   /* If the next token isn't a `,' or a `>', then this argument wasn't
12901      really finished.  */
12902   if (!cp_parser_next_token_ends_template_argument_p (parser))
12903     cp_parser_error (parser, "expected template-argument");
12904   if (!cp_parser_error_occurred (parser))
12905     {
12906       /* Figure out what is being referred to.  If the id-expression
12907          was for a class template specialization, then we will have a
12908          TYPE_DECL at this point.  There is no need to do name lookup
12909          at this point in that case.  */
12910       if (TREE_CODE (argument) != TYPE_DECL)
12911         argument = cp_parser_lookup_name (parser, argument,
12912                                           none_type,
12913                                           /*is_template=*/template_p,
12914                                           /*is_namespace=*/false,
12915                                           /*check_dependency=*/true,
12916                                           /*ambiguous_decls=*/NULL,
12917                                           argument_start_token->location);
12918       if (TREE_CODE (argument) != TEMPLATE_DECL
12919           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12920         cp_parser_error (parser, "expected template-name");
12921     }
12922   if (cp_parser_parse_definitely (parser))
12923     return argument;
12924   /* It must be a non-type argument.  There permitted cases are given
12925      in [temp.arg.nontype]:
12926
12927      -- an integral constant-expression of integral or enumeration
12928         type; or
12929
12930      -- the name of a non-type template-parameter; or
12931
12932      -- the name of an object or function with external linkage...
12933
12934      -- the address of an object or function with external linkage...
12935
12936      -- a pointer to member...  */
12937   /* Look for a non-type template parameter.  */
12938   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12939     {
12940       cp_parser_parse_tentatively (parser);
12941       argument = cp_parser_primary_expression (parser,
12942                                                /*address_p=*/false,
12943                                                /*cast_p=*/false,
12944                                                /*template_arg_p=*/true,
12945                                                &idk);
12946       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12947           || !cp_parser_next_token_ends_template_argument_p (parser))
12948         cp_parser_simulate_error (parser);
12949       if (cp_parser_parse_definitely (parser))
12950         return argument;
12951     }
12952
12953   /* If the next token is "&", the argument must be the address of an
12954      object or function with external linkage.  */
12955   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12956   if (address_p)
12957     cp_lexer_consume_token (parser->lexer);
12958   /* See if we might have an id-expression.  */
12959   token = cp_lexer_peek_token (parser->lexer);
12960   if (token->type == CPP_NAME
12961       || token->keyword == RID_OPERATOR
12962       || token->type == CPP_SCOPE
12963       || token->type == CPP_TEMPLATE_ID
12964       || token->type == CPP_NESTED_NAME_SPECIFIER)
12965     {
12966       cp_parser_parse_tentatively (parser);
12967       argument = cp_parser_primary_expression (parser,
12968                                                address_p,
12969                                                /*cast_p=*/false,
12970                                                /*template_arg_p=*/true,
12971                                                &idk);
12972       if (cp_parser_error_occurred (parser)
12973           || !cp_parser_next_token_ends_template_argument_p (parser))
12974         cp_parser_abort_tentative_parse (parser);
12975       else
12976         {
12977           tree probe;
12978
12979           if (TREE_CODE (argument) == INDIRECT_REF)
12980             {
12981               gcc_assert (REFERENCE_REF_P (argument));
12982               argument = TREE_OPERAND (argument, 0);
12983             }
12984
12985           /* If we're in a template, we represent a qualified-id referring
12986              to a static data member as a SCOPE_REF even if the scope isn't
12987              dependent so that we can check access control later.  */
12988           probe = argument;
12989           if (TREE_CODE (probe) == SCOPE_REF)
12990             probe = TREE_OPERAND (probe, 1);
12991           if (TREE_CODE (probe) == VAR_DECL)
12992             {
12993               /* A variable without external linkage might still be a
12994                  valid constant-expression, so no error is issued here
12995                  if the external-linkage check fails.  */
12996               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12997                 cp_parser_simulate_error (parser);
12998             }
12999           else if (is_overloaded_fn (argument))
13000             /* All overloaded functions are allowed; if the external
13001                linkage test does not pass, an error will be issued
13002                later.  */
13003             ;
13004           else if (address_p
13005                    && (TREE_CODE (argument) == OFFSET_REF
13006                        || TREE_CODE (argument) == SCOPE_REF))
13007             /* A pointer-to-member.  */
13008             ;
13009           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
13010             ;
13011           else
13012             cp_parser_simulate_error (parser);
13013
13014           if (cp_parser_parse_definitely (parser))
13015             {
13016               if (address_p)
13017                 argument = build_x_unary_op (ADDR_EXPR, argument,
13018                                              tf_warning_or_error);
13019               return argument;
13020             }
13021         }
13022     }
13023   /* If the argument started with "&", there are no other valid
13024      alternatives at this point.  */
13025   if (address_p)
13026     {
13027       cp_parser_error (parser, "invalid non-type template argument");
13028       return error_mark_node;
13029     }
13030
13031   /* If the argument wasn't successfully parsed as a type-id followed
13032      by '>>', the argument can only be a constant expression now.
13033      Otherwise, we try parsing the constant-expression tentatively,
13034      because the argument could really be a type-id.  */
13035   if (maybe_type_id)
13036     cp_parser_parse_tentatively (parser);
13037   argument = cp_parser_constant_expression (parser,
13038                                             /*allow_non_constant_p=*/false,
13039                                             /*non_constant_p=*/NULL);
13040   argument = fold_non_dependent_expr (argument);
13041   if (!maybe_type_id)
13042     return argument;
13043   if (!cp_parser_next_token_ends_template_argument_p (parser))
13044     cp_parser_error (parser, "expected template-argument");
13045   if (cp_parser_parse_definitely (parser))
13046     return argument;
13047   /* We did our best to parse the argument as a non type-id, but that
13048      was the only alternative that matched (albeit with a '>' after
13049      it). We can assume it's just a typo from the user, and a
13050      diagnostic will then be issued.  */
13051   return cp_parser_template_type_arg (parser);
13052 }
13053
13054 /* Parse an explicit-instantiation.
13055
13056    explicit-instantiation:
13057      template declaration
13058
13059    Although the standard says `declaration', what it really means is:
13060
13061    explicit-instantiation:
13062      template decl-specifier-seq [opt] declarator [opt] ;
13063
13064    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13065    supposed to be allowed.  A defect report has been filed about this
13066    issue.
13067
13068    GNU Extension:
13069
13070    explicit-instantiation:
13071      storage-class-specifier template
13072        decl-specifier-seq [opt] declarator [opt] ;
13073      function-specifier template
13074        decl-specifier-seq [opt] declarator [opt] ;  */
13075
13076 static void
13077 cp_parser_explicit_instantiation (cp_parser* parser)
13078 {
13079   int declares_class_or_enum;
13080   cp_decl_specifier_seq decl_specifiers;
13081   tree extension_specifier = NULL_TREE;
13082
13083   timevar_push (TV_TEMPLATE_INST);
13084
13085   /* Look for an (optional) storage-class-specifier or
13086      function-specifier.  */
13087   if (cp_parser_allow_gnu_extensions_p (parser))
13088     {
13089       extension_specifier
13090         = cp_parser_storage_class_specifier_opt (parser);
13091       if (!extension_specifier)
13092         extension_specifier
13093           = cp_parser_function_specifier_opt (parser,
13094                                               /*decl_specs=*/NULL);
13095     }
13096
13097   /* Look for the `template' keyword.  */
13098   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13099   /* Let the front end know that we are processing an explicit
13100      instantiation.  */
13101   begin_explicit_instantiation ();
13102   /* [temp.explicit] says that we are supposed to ignore access
13103      control while processing explicit instantiation directives.  */
13104   push_deferring_access_checks (dk_no_check);
13105   /* Parse a decl-specifier-seq.  */
13106   cp_parser_decl_specifier_seq (parser,
13107                                 CP_PARSER_FLAGS_OPTIONAL,
13108                                 &decl_specifiers,
13109                                 &declares_class_or_enum);
13110   /* If there was exactly one decl-specifier, and it declared a class,
13111      and there's no declarator, then we have an explicit type
13112      instantiation.  */
13113   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13114     {
13115       tree type;
13116
13117       type = check_tag_decl (&decl_specifiers);
13118       /* Turn access control back on for names used during
13119          template instantiation.  */
13120       pop_deferring_access_checks ();
13121       if (type)
13122         do_type_instantiation (type, extension_specifier,
13123                                /*complain=*/tf_error);
13124     }
13125   else
13126     {
13127       cp_declarator *declarator;
13128       tree decl;
13129
13130       /* Parse the declarator.  */
13131       declarator
13132         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13133                                 /*ctor_dtor_or_conv_p=*/NULL,
13134                                 /*parenthesized_p=*/NULL,
13135                                 /*member_p=*/false);
13136       if (declares_class_or_enum & 2)
13137         cp_parser_check_for_definition_in_return_type (declarator,
13138                                                        decl_specifiers.type,
13139                                                        decl_specifiers.type_location);
13140       if (declarator != cp_error_declarator)
13141         {
13142           if (decl_specifiers.specs[(int)ds_inline])
13143             permerror (input_location, "explicit instantiation shall not use"
13144                        " %<inline%> specifier");
13145           if (decl_specifiers.specs[(int)ds_constexpr])
13146             permerror (input_location, "explicit instantiation shall not use"
13147                        " %<constexpr%> specifier");
13148
13149           decl = grokdeclarator (declarator, &decl_specifiers,
13150                                  NORMAL, 0, &decl_specifiers.attributes);
13151           /* Turn access control back on for names used during
13152              template instantiation.  */
13153           pop_deferring_access_checks ();
13154           /* Do the explicit instantiation.  */
13155           do_decl_instantiation (decl, extension_specifier);
13156         }
13157       else
13158         {
13159           pop_deferring_access_checks ();
13160           /* Skip the body of the explicit instantiation.  */
13161           cp_parser_skip_to_end_of_statement (parser);
13162         }
13163     }
13164   /* We're done with the instantiation.  */
13165   end_explicit_instantiation ();
13166
13167   cp_parser_consume_semicolon_at_end_of_statement (parser);
13168
13169   timevar_pop (TV_TEMPLATE_INST);
13170 }
13171
13172 /* Parse an explicit-specialization.
13173
13174    explicit-specialization:
13175      template < > declaration
13176
13177    Although the standard says `declaration', what it really means is:
13178
13179    explicit-specialization:
13180      template <> decl-specifier [opt] init-declarator [opt] ;
13181      template <> function-definition
13182      template <> explicit-specialization
13183      template <> template-declaration  */
13184
13185 static void
13186 cp_parser_explicit_specialization (cp_parser* parser)
13187 {
13188   bool need_lang_pop;
13189   cp_token *token = cp_lexer_peek_token (parser->lexer);
13190
13191   /* Look for the `template' keyword.  */
13192   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13193   /* Look for the `<'.  */
13194   cp_parser_require (parser, CPP_LESS, RT_LESS);
13195   /* Look for the `>'.  */
13196   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13197   /* We have processed another parameter list.  */
13198   ++parser->num_template_parameter_lists;
13199   /* [temp]
13200
13201      A template ... explicit specialization ... shall not have C
13202      linkage.  */
13203   if (current_lang_name == lang_name_c)
13204     {
13205       error_at (token->location, "template specialization with C linkage");
13206       /* Give it C++ linkage to avoid confusing other parts of the
13207          front end.  */
13208       push_lang_context (lang_name_cplusplus);
13209       need_lang_pop = true;
13210     }
13211   else
13212     need_lang_pop = false;
13213   /* Let the front end know that we are beginning a specialization.  */
13214   if (!begin_specialization ())
13215     {
13216       end_specialization ();
13217       return;
13218     }
13219
13220   /* If the next keyword is `template', we need to figure out whether
13221      or not we're looking a template-declaration.  */
13222   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13223     {
13224       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13225           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13226         cp_parser_template_declaration_after_export (parser,
13227                                                      /*member_p=*/false);
13228       else
13229         cp_parser_explicit_specialization (parser);
13230     }
13231   else
13232     /* Parse the dependent declaration.  */
13233     cp_parser_single_declaration (parser,
13234                                   /*checks=*/NULL,
13235                                   /*member_p=*/false,
13236                                   /*explicit_specialization_p=*/true,
13237                                   /*friend_p=*/NULL);
13238   /* We're done with the specialization.  */
13239   end_specialization ();
13240   /* For the erroneous case of a template with C linkage, we pushed an
13241      implicit C++ linkage scope; exit that scope now.  */
13242   if (need_lang_pop)
13243     pop_lang_context ();
13244   /* We're done with this parameter list.  */
13245   --parser->num_template_parameter_lists;
13246 }
13247
13248 /* Parse a type-specifier.
13249
13250    type-specifier:
13251      simple-type-specifier
13252      class-specifier
13253      enum-specifier
13254      elaborated-type-specifier
13255      cv-qualifier
13256
13257    GNU Extension:
13258
13259    type-specifier:
13260      __complex__
13261
13262    Returns a representation of the type-specifier.  For a
13263    class-specifier, enum-specifier, or elaborated-type-specifier, a
13264    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13265
13266    The parser flags FLAGS is used to control type-specifier parsing.
13267
13268    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13269    in a decl-specifier-seq.
13270
13271    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13272    class-specifier, enum-specifier, or elaborated-type-specifier, then
13273    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13274    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13275    zero.
13276
13277    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13278    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13279    is set to FALSE.  */
13280
13281 static tree
13282 cp_parser_type_specifier (cp_parser* parser,
13283                           cp_parser_flags flags,
13284                           cp_decl_specifier_seq *decl_specs,
13285                           bool is_declaration,
13286                           int* declares_class_or_enum,
13287                           bool* is_cv_qualifier)
13288 {
13289   tree type_spec = NULL_TREE;
13290   cp_token *token;
13291   enum rid keyword;
13292   cp_decl_spec ds = ds_last;
13293
13294   /* Assume this type-specifier does not declare a new type.  */
13295   if (declares_class_or_enum)
13296     *declares_class_or_enum = 0;
13297   /* And that it does not specify a cv-qualifier.  */
13298   if (is_cv_qualifier)
13299     *is_cv_qualifier = false;
13300   /* Peek at the next token.  */
13301   token = cp_lexer_peek_token (parser->lexer);
13302
13303   /* If we're looking at a keyword, we can use that to guide the
13304      production we choose.  */
13305   keyword = token->keyword;
13306   switch (keyword)
13307     {
13308     case RID_ENUM:
13309       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13310         goto elaborated_type_specifier;
13311
13312       /* Look for the enum-specifier.  */
13313       type_spec = cp_parser_enum_specifier (parser);
13314       /* If that worked, we're done.  */
13315       if (type_spec)
13316         {
13317           if (declares_class_or_enum)
13318             *declares_class_or_enum = 2;
13319           if (decl_specs)
13320             cp_parser_set_decl_spec_type (decl_specs,
13321                                           type_spec,
13322                                           token->location,
13323                                           /*type_definition_p=*/true);
13324           return type_spec;
13325         }
13326       else
13327         goto elaborated_type_specifier;
13328
13329       /* Any of these indicate either a class-specifier, or an
13330          elaborated-type-specifier.  */
13331     case RID_CLASS:
13332     case RID_STRUCT:
13333     case RID_UNION:
13334       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13335         goto elaborated_type_specifier;
13336
13337       /* Parse tentatively so that we can back up if we don't find a
13338          class-specifier.  */
13339       cp_parser_parse_tentatively (parser);
13340       /* Look for the class-specifier.  */
13341       type_spec = cp_parser_class_specifier (parser);
13342       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13343       /* If that worked, we're done.  */
13344       if (cp_parser_parse_definitely (parser))
13345         {
13346           if (declares_class_or_enum)
13347             *declares_class_or_enum = 2;
13348           if (decl_specs)
13349             cp_parser_set_decl_spec_type (decl_specs,
13350                                           type_spec,
13351                                           token->location,
13352                                           /*type_definition_p=*/true);
13353           return type_spec;
13354         }
13355
13356       /* Fall through.  */
13357     elaborated_type_specifier:
13358       /* We're declaring (not defining) a class or enum.  */
13359       if (declares_class_or_enum)
13360         *declares_class_or_enum = 1;
13361
13362       /* Fall through.  */
13363     case RID_TYPENAME:
13364       /* Look for an elaborated-type-specifier.  */
13365       type_spec
13366         = (cp_parser_elaborated_type_specifier
13367            (parser,
13368             decl_specs && decl_specs->specs[(int) ds_friend],
13369             is_declaration));
13370       if (decl_specs)
13371         cp_parser_set_decl_spec_type (decl_specs,
13372                                       type_spec,
13373                                       token->location,
13374                                       /*type_definition_p=*/false);
13375       return type_spec;
13376
13377     case RID_CONST:
13378       ds = ds_const;
13379       if (is_cv_qualifier)
13380         *is_cv_qualifier = true;
13381       break;
13382
13383     case RID_VOLATILE:
13384       ds = ds_volatile;
13385       if (is_cv_qualifier)
13386         *is_cv_qualifier = true;
13387       break;
13388
13389     case RID_RESTRICT:
13390       ds = ds_restrict;
13391       if (is_cv_qualifier)
13392         *is_cv_qualifier = true;
13393       break;
13394
13395     case RID_COMPLEX:
13396       /* The `__complex__' keyword is a GNU extension.  */
13397       ds = ds_complex;
13398       break;
13399
13400     default:
13401       break;
13402     }
13403
13404   /* Handle simple keywords.  */
13405   if (ds != ds_last)
13406     {
13407       if (decl_specs)
13408         {
13409           ++decl_specs->specs[(int)ds];
13410           decl_specs->any_specifiers_p = true;
13411         }
13412       return cp_lexer_consume_token (parser->lexer)->u.value;
13413     }
13414
13415   /* If we do not already have a type-specifier, assume we are looking
13416      at a simple-type-specifier.  */
13417   type_spec = cp_parser_simple_type_specifier (parser,
13418                                                decl_specs,
13419                                                flags);
13420
13421   /* If we didn't find a type-specifier, and a type-specifier was not
13422      optional in this context, issue an error message.  */
13423   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13424     {
13425       cp_parser_error (parser, "expected type specifier");
13426       return error_mark_node;
13427     }
13428
13429   return type_spec;
13430 }
13431
13432 /* Parse a simple-type-specifier.
13433
13434    simple-type-specifier:
13435      :: [opt] nested-name-specifier [opt] type-name
13436      :: [opt] nested-name-specifier template template-id
13437      char
13438      wchar_t
13439      bool
13440      short
13441      int
13442      long
13443      signed
13444      unsigned
13445      float
13446      double
13447      void
13448
13449    C++0x Extension:
13450
13451    simple-type-specifier:
13452      auto
13453      decltype ( expression )   
13454      char16_t
13455      char32_t
13456      __underlying_type ( type-id )
13457
13458    GNU Extension:
13459
13460    simple-type-specifier:
13461      __int128
13462      __typeof__ unary-expression
13463      __typeof__ ( type-id )
13464
13465    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13466    appropriately updated.  */
13467
13468 static tree
13469 cp_parser_simple_type_specifier (cp_parser* parser,
13470                                  cp_decl_specifier_seq *decl_specs,
13471                                  cp_parser_flags flags)
13472 {
13473   tree type = NULL_TREE;
13474   cp_token *token;
13475
13476   /* Peek at the next token.  */
13477   token = cp_lexer_peek_token (parser->lexer);
13478
13479   /* If we're looking at a keyword, things are easy.  */
13480   switch (token->keyword)
13481     {
13482     case RID_CHAR:
13483       if (decl_specs)
13484         decl_specs->explicit_char_p = true;
13485       type = char_type_node;
13486       break;
13487     case RID_CHAR16:
13488       type = char16_type_node;
13489       break;
13490     case RID_CHAR32:
13491       type = char32_type_node;
13492       break;
13493     case RID_WCHAR:
13494       type = wchar_type_node;
13495       break;
13496     case RID_BOOL:
13497       type = boolean_type_node;
13498       break;
13499     case RID_SHORT:
13500       if (decl_specs)
13501         ++decl_specs->specs[(int) ds_short];
13502       type = short_integer_type_node;
13503       break;
13504     case RID_INT:
13505       if (decl_specs)
13506         decl_specs->explicit_int_p = true;
13507       type = integer_type_node;
13508       break;
13509     case RID_INT128:
13510       if (!int128_integer_type_node)
13511         break;
13512       if (decl_specs)
13513         decl_specs->explicit_int128_p = true;
13514       type = int128_integer_type_node;
13515       break;
13516     case RID_LONG:
13517       if (decl_specs)
13518         ++decl_specs->specs[(int) ds_long];
13519       type = long_integer_type_node;
13520       break;
13521     case RID_SIGNED:
13522       if (decl_specs)
13523         ++decl_specs->specs[(int) ds_signed];
13524       type = integer_type_node;
13525       break;
13526     case RID_UNSIGNED:
13527       if (decl_specs)
13528         ++decl_specs->specs[(int) ds_unsigned];
13529       type = unsigned_type_node;
13530       break;
13531     case RID_FLOAT:
13532       type = float_type_node;
13533       break;
13534     case RID_DOUBLE:
13535       type = double_type_node;
13536       break;
13537     case RID_VOID:
13538       type = void_type_node;
13539       break;
13540       
13541     case RID_AUTO:
13542       maybe_warn_cpp0x (CPP0X_AUTO);
13543       type = make_auto ();
13544       break;
13545
13546     case RID_DECLTYPE:
13547       /* Since DR 743, decltype can either be a simple-type-specifier by
13548          itself or begin a nested-name-specifier.  Parsing it will replace
13549          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13550          handling below decide what to do.  */
13551       cp_parser_decltype (parser);
13552       cp_lexer_set_token_position (parser->lexer, token);
13553       break;
13554
13555     case RID_TYPEOF:
13556       /* Consume the `typeof' token.  */
13557       cp_lexer_consume_token (parser->lexer);
13558       /* Parse the operand to `typeof'.  */
13559       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13560       /* If it is not already a TYPE, take its type.  */
13561       if (!TYPE_P (type))
13562         type = finish_typeof (type);
13563
13564       if (decl_specs)
13565         cp_parser_set_decl_spec_type (decl_specs, type,
13566                                       token->location,
13567                                       /*type_definition_p=*/false);
13568
13569       return type;
13570
13571     case RID_UNDERLYING_TYPE:
13572       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13573       if (decl_specs)
13574         cp_parser_set_decl_spec_type (decl_specs, type,
13575                                       token->location,
13576                                       /*type_definition_p=*/false);
13577
13578       return type;
13579
13580     case RID_BASES:
13581     case RID_DIRECT_BASES:
13582       type = cp_parser_trait_expr (parser, token->keyword);
13583       if (decl_specs)
13584        cp_parser_set_decl_spec_type (decl_specs, type,
13585                                      token->location,
13586                                      /*type_definition_p=*/false);
13587       return type;
13588     default:
13589       break;
13590     }
13591
13592   /* If token is an already-parsed decltype not followed by ::,
13593      it's a simple-type-specifier.  */
13594   if (token->type == CPP_DECLTYPE
13595       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13596     {
13597       type = token->u.value;
13598       if (decl_specs)
13599         cp_parser_set_decl_spec_type (decl_specs, type,
13600                                       token->location,
13601                                       /*type_definition_p=*/false);
13602       cp_lexer_consume_token (parser->lexer);
13603       return type;
13604     }
13605
13606   /* If the type-specifier was for a built-in type, we're done.  */
13607   if (type)
13608     {
13609       /* Record the type.  */
13610       if (decl_specs
13611           && (token->keyword != RID_SIGNED
13612               && token->keyword != RID_UNSIGNED
13613               && token->keyword != RID_SHORT
13614               && token->keyword != RID_LONG))
13615         cp_parser_set_decl_spec_type (decl_specs,
13616                                       type,
13617                                       token->location,
13618                                       /*type_definition_p=*/false);
13619       if (decl_specs)
13620         decl_specs->any_specifiers_p = true;
13621
13622       /* Consume the token.  */
13623       cp_lexer_consume_token (parser->lexer);
13624
13625       /* There is no valid C++ program where a non-template type is
13626          followed by a "<".  That usually indicates that the user thought
13627          that the type was a template.  */
13628       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13629
13630       return TYPE_NAME (type);
13631     }
13632
13633   /* The type-specifier must be a user-defined type.  */
13634   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13635     {
13636       bool qualified_p;
13637       bool global_p;
13638
13639       /* Don't gobble tokens or issue error messages if this is an
13640          optional type-specifier.  */
13641       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13642         cp_parser_parse_tentatively (parser);
13643
13644       /* Look for the optional `::' operator.  */
13645       global_p
13646         = (cp_parser_global_scope_opt (parser,
13647                                        /*current_scope_valid_p=*/false)
13648            != NULL_TREE);
13649       /* Look for the nested-name specifier.  */
13650       qualified_p
13651         = (cp_parser_nested_name_specifier_opt (parser,
13652                                                 /*typename_keyword_p=*/false,
13653                                                 /*check_dependency_p=*/true,
13654                                                 /*type_p=*/false,
13655                                                 /*is_declaration=*/false)
13656            != NULL_TREE);
13657       token = cp_lexer_peek_token (parser->lexer);
13658       /* If we have seen a nested-name-specifier, and the next token
13659          is `template', then we are using the template-id production.  */
13660       if (parser->scope
13661           && cp_parser_optional_template_keyword (parser))
13662         {
13663           /* Look for the template-id.  */
13664           type = cp_parser_template_id (parser,
13665                                         /*template_keyword_p=*/true,
13666                                         /*check_dependency_p=*/true,
13667                                         /*is_declaration=*/false);
13668           /* If the template-id did not name a type, we are out of
13669              luck.  */
13670           if (TREE_CODE (type) != TYPE_DECL)
13671             {
13672               cp_parser_error (parser, "expected template-id for type");
13673               type = NULL_TREE;
13674             }
13675         }
13676       /* Otherwise, look for a type-name.  */
13677       else
13678         type = cp_parser_type_name (parser);
13679       /* Keep track of all name-lookups performed in class scopes.  */
13680       if (type
13681           && !global_p
13682           && !qualified_p
13683           && TREE_CODE (type) == TYPE_DECL
13684           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13685         maybe_note_name_used_in_class (DECL_NAME (type), type);
13686       /* If it didn't work out, we don't have a TYPE.  */
13687       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13688           && !cp_parser_parse_definitely (parser))
13689         type = NULL_TREE;
13690       if (type && decl_specs)
13691         cp_parser_set_decl_spec_type (decl_specs, type,
13692                                       token->location,
13693                                       /*type_definition_p=*/false);
13694     }
13695
13696   /* If we didn't get a type-name, issue an error message.  */
13697   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13698     {
13699       cp_parser_error (parser, "expected type-name");
13700       return error_mark_node;
13701     }
13702
13703   if (type && type != error_mark_node)
13704     {
13705       /* See if TYPE is an Objective-C type, and if so, parse and
13706          accept any protocol references following it.  Do this before
13707          the cp_parser_check_for_invalid_template_id() call, because
13708          Objective-C types can be followed by '<...>' which would
13709          enclose protocol names rather than template arguments, and so
13710          everything is fine.  */
13711       if (c_dialect_objc () && !parser->scope
13712           && (objc_is_id (type) || objc_is_class_name (type)))
13713         {
13714           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13715           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13716
13717           /* Clobber the "unqualified" type previously entered into
13718              DECL_SPECS with the new, improved protocol-qualified version.  */
13719           if (decl_specs)
13720             decl_specs->type = qual_type;
13721
13722           return qual_type;
13723         }
13724
13725       /* There is no valid C++ program where a non-template type is
13726          followed by a "<".  That usually indicates that the user
13727          thought that the type was a template.  */
13728       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13729                                                token->location);
13730     }
13731
13732   return type;
13733 }
13734
13735 /* Parse a type-name.
13736
13737    type-name:
13738      class-name
13739      enum-name
13740      typedef-name
13741      simple-template-id [in c++0x]
13742
13743    enum-name:
13744      identifier
13745
13746    typedef-name:
13747      identifier
13748
13749    Returns a TYPE_DECL for the type.  */
13750
13751 static tree
13752 cp_parser_type_name (cp_parser* parser)
13753 {
13754   tree type_decl;
13755
13756   /* We can't know yet whether it is a class-name or not.  */
13757   cp_parser_parse_tentatively (parser);
13758   /* Try a class-name.  */
13759   type_decl = cp_parser_class_name (parser,
13760                                     /*typename_keyword_p=*/false,
13761                                     /*template_keyword_p=*/false,
13762                                     none_type,
13763                                     /*check_dependency_p=*/true,
13764                                     /*class_head_p=*/false,
13765                                     /*is_declaration=*/false);
13766   /* If it's not a class-name, keep looking.  */
13767   if (!cp_parser_parse_definitely (parser))
13768     {
13769       if (cxx_dialect < cxx0x)
13770         /* It must be a typedef-name or an enum-name.  */
13771         return cp_parser_nonclass_name (parser);
13772
13773       cp_parser_parse_tentatively (parser);
13774       /* It is either a simple-template-id representing an
13775          instantiation of an alias template...  */
13776       type_decl = cp_parser_template_id (parser,
13777                                          /*template_keyword_p=*/false,
13778                                          /*check_dependency_p=*/false,
13779                                          /*is_declaration=*/false);
13780       /* Note that this must be an instantiation of an alias template
13781          because [temp.names]/6 says:
13782          
13783              A template-id that names an alias template specialization
13784              is a type-name.
13785
13786          Whereas [temp.names]/7 says:
13787          
13788              A simple-template-id that names a class template
13789              specialization is a class-name.  */
13790       if (type_decl != NULL_TREE
13791           && TREE_CODE (type_decl) == TYPE_DECL
13792           && TYPE_DECL_ALIAS_P (type_decl))
13793         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13794       else
13795         cp_parser_simulate_error (parser);
13796
13797       if (!cp_parser_parse_definitely (parser))
13798         /* ... Or a typedef-name or an enum-name.  */
13799         return cp_parser_nonclass_name (parser);
13800     }
13801
13802   return type_decl;
13803 }
13804
13805 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13806
13807    enum-name:
13808      identifier
13809
13810    typedef-name:
13811      identifier
13812
13813    Returns a TYPE_DECL for the type.  */
13814
13815 static tree
13816 cp_parser_nonclass_name (cp_parser* parser)
13817 {
13818   tree type_decl;
13819   tree identifier;
13820
13821   cp_token *token = cp_lexer_peek_token (parser->lexer);
13822   identifier = cp_parser_identifier (parser);
13823   if (identifier == error_mark_node)
13824     return error_mark_node;
13825
13826   /* Look up the type-name.  */
13827   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13828
13829   if (TREE_CODE (type_decl) == USING_DECL)
13830     {
13831       if (!DECL_DEPENDENT_P (type_decl))
13832         type_decl = strip_using_decl (type_decl);
13833       else if (USING_DECL_TYPENAME_P (type_decl))
13834         {
13835           /* We have found a type introduced by a using
13836              declaration at class scope that refers to a dependent
13837              type.
13838              
13839              using typename :: [opt] nested-name-specifier unqualified-id ;
13840           */
13841           type_decl = make_typename_type (TREE_TYPE (type_decl),
13842                                           DECL_NAME (type_decl),
13843                                           typename_type, tf_error);
13844           if (type_decl != error_mark_node)
13845             type_decl = TYPE_NAME (type_decl);
13846         }
13847     }
13848   
13849   if (TREE_CODE (type_decl) != TYPE_DECL
13850       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13851     {
13852       /* See if this is an Objective-C type.  */
13853       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13854       tree type = objc_get_protocol_qualified_type (identifier, protos);
13855       if (type)
13856         type_decl = TYPE_NAME (type);
13857     }
13858
13859   /* Issue an error if we did not find a type-name.  */
13860   if (TREE_CODE (type_decl) != TYPE_DECL
13861       /* In Objective-C, we have the complication that class names are
13862          normally type names and start declarations (eg, the
13863          "NSObject" in "NSObject *object;"), but can be used in an
13864          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13865          is an expression.  So, a classname followed by a dot is not a
13866          valid type-name.  */
13867       || (objc_is_class_name (TREE_TYPE (type_decl))
13868           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13869     {
13870       if (!cp_parser_simulate_error (parser))
13871         cp_parser_name_lookup_error (parser, identifier, type_decl,
13872                                      NLE_TYPE, token->location);
13873       return error_mark_node;
13874     }
13875   /* Remember that the name was used in the definition of the
13876      current class so that we can check later to see if the
13877      meaning would have been different after the class was
13878      entirely defined.  */
13879   else if (type_decl != error_mark_node
13880            && !parser->scope)
13881     maybe_note_name_used_in_class (identifier, type_decl);
13882   
13883   return type_decl;
13884 }
13885
13886 /* Parse an elaborated-type-specifier.  Note that the grammar given
13887    here incorporates the resolution to DR68.
13888
13889    elaborated-type-specifier:
13890      class-key :: [opt] nested-name-specifier [opt] identifier
13891      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13892      enum-key :: [opt] nested-name-specifier [opt] identifier
13893      typename :: [opt] nested-name-specifier identifier
13894      typename :: [opt] nested-name-specifier template [opt]
13895        template-id
13896
13897    GNU extension:
13898
13899    elaborated-type-specifier:
13900      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13901      class-key attributes :: [opt] nested-name-specifier [opt]
13902                template [opt] template-id
13903      enum attributes :: [opt] nested-name-specifier [opt] identifier
13904
13905    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13906    declared `friend'.  If IS_DECLARATION is TRUE, then this
13907    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13908    something is being declared.
13909
13910    Returns the TYPE specified.  */
13911
13912 static tree
13913 cp_parser_elaborated_type_specifier (cp_parser* parser,
13914                                      bool is_friend,
13915                                      bool is_declaration)
13916 {
13917   enum tag_types tag_type;
13918   tree identifier;
13919   tree type = NULL_TREE;
13920   tree attributes = NULL_TREE;
13921   tree globalscope;
13922   cp_token *token = NULL;
13923
13924   /* See if we're looking at the `enum' keyword.  */
13925   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13926     {
13927       /* Consume the `enum' token.  */
13928       cp_lexer_consume_token (parser->lexer);
13929       /* Remember that it's an enumeration type.  */
13930       tag_type = enum_type;
13931       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13932          enums) is used here.  */
13933       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13934           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13935         {
13936             pedwarn (input_location, 0, "elaborated-type-specifier "
13937                       "for a scoped enum must not use the %<%D%> keyword",
13938                       cp_lexer_peek_token (parser->lexer)->u.value);
13939           /* Consume the `struct' or `class' and parse it anyway.  */
13940           cp_lexer_consume_token (parser->lexer);
13941         }
13942       /* Parse the attributes.  */
13943       attributes = cp_parser_attributes_opt (parser);
13944     }
13945   /* Or, it might be `typename'.  */
13946   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13947                                            RID_TYPENAME))
13948     {
13949       /* Consume the `typename' token.  */
13950       cp_lexer_consume_token (parser->lexer);
13951       /* Remember that it's a `typename' type.  */
13952       tag_type = typename_type;
13953     }
13954   /* Otherwise it must be a class-key.  */
13955   else
13956     {
13957       tag_type = cp_parser_class_key (parser);
13958       if (tag_type == none_type)
13959         return error_mark_node;
13960       /* Parse the attributes.  */
13961       attributes = cp_parser_attributes_opt (parser);
13962     }
13963
13964   /* Look for the `::' operator.  */
13965   globalscope =  cp_parser_global_scope_opt (parser,
13966                                              /*current_scope_valid_p=*/false);
13967   /* Look for the nested-name-specifier.  */
13968   if (tag_type == typename_type && !globalscope)
13969     {
13970       if (!cp_parser_nested_name_specifier (parser,
13971                                            /*typename_keyword_p=*/true,
13972                                            /*check_dependency_p=*/true,
13973                                            /*type_p=*/true,
13974                                             is_declaration))
13975         return error_mark_node;
13976     }
13977   else
13978     /* Even though `typename' is not present, the proposed resolution
13979        to Core Issue 180 says that in `class A<T>::B', `B' should be
13980        considered a type-name, even if `A<T>' is dependent.  */
13981     cp_parser_nested_name_specifier_opt (parser,
13982                                          /*typename_keyword_p=*/true,
13983                                          /*check_dependency_p=*/true,
13984                                          /*type_p=*/true,
13985                                          is_declaration);
13986  /* For everything but enumeration types, consider a template-id.
13987     For an enumeration type, consider only a plain identifier.  */
13988   if (tag_type != enum_type)
13989     {
13990       bool template_p = false;
13991       tree decl;
13992
13993       /* Allow the `template' keyword.  */
13994       template_p = cp_parser_optional_template_keyword (parser);
13995       /* If we didn't see `template', we don't know if there's a
13996          template-id or not.  */
13997       if (!template_p)
13998         cp_parser_parse_tentatively (parser);
13999       /* Parse the template-id.  */
14000       token = cp_lexer_peek_token (parser->lexer);
14001       decl = cp_parser_template_id (parser, template_p,
14002                                     /*check_dependency_p=*/true,
14003                                     is_declaration);
14004       /* If we didn't find a template-id, look for an ordinary
14005          identifier.  */
14006       if (!template_p && !cp_parser_parse_definitely (parser))
14007         ;
14008       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
14009          in effect, then we must assume that, upon instantiation, the
14010          template will correspond to a class.  */
14011       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14012                && tag_type == typename_type)
14013         type = make_typename_type (parser->scope, decl,
14014                                    typename_type,
14015                                    /*complain=*/tf_error);
14016       /* If the `typename' keyword is in effect and DECL is not a type
14017          decl. Then type is non existant.   */
14018       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14019         type = NULL_TREE; 
14020       else 
14021         type = check_elaborated_type_specifier (tag_type, decl,
14022                                                 /*allow_template_p=*/true);
14023     }
14024
14025   if (!type)
14026     {
14027       token = cp_lexer_peek_token (parser->lexer);
14028       identifier = cp_parser_identifier (parser);
14029
14030       if (identifier == error_mark_node)
14031         {
14032           parser->scope = NULL_TREE;
14033           return error_mark_node;
14034         }
14035
14036       /* For a `typename', we needn't call xref_tag.  */
14037       if (tag_type == typename_type
14038           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14039         return cp_parser_make_typename_type (parser, parser->scope,
14040                                              identifier,
14041                                              token->location);
14042       /* Look up a qualified name in the usual way.  */
14043       if (parser->scope)
14044         {
14045           tree decl;
14046           tree ambiguous_decls;
14047
14048           decl = cp_parser_lookup_name (parser, identifier,
14049                                         tag_type,
14050                                         /*is_template=*/false,
14051                                         /*is_namespace=*/false,
14052                                         /*check_dependency=*/true,
14053                                         &ambiguous_decls,
14054                                         token->location);
14055
14056           /* If the lookup was ambiguous, an error will already have been
14057              issued.  */
14058           if (ambiguous_decls)
14059             return error_mark_node;
14060
14061           /* If we are parsing friend declaration, DECL may be a
14062              TEMPLATE_DECL tree node here.  However, we need to check
14063              whether this TEMPLATE_DECL results in valid code.  Consider
14064              the following example:
14065
14066                namespace N {
14067                  template <class T> class C {};
14068                }
14069                class X {
14070                  template <class T> friend class N::C; // #1, valid code
14071                };
14072                template <class T> class Y {
14073                  friend class N::C;                    // #2, invalid code
14074                };
14075
14076              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14077              name lookup of `N::C'.  We see that friend declaration must
14078              be template for the code to be valid.  Note that
14079              processing_template_decl does not work here since it is
14080              always 1 for the above two cases.  */
14081
14082           decl = (cp_parser_maybe_treat_template_as_class
14083                   (decl, /*tag_name_p=*/is_friend
14084                          && parser->num_template_parameter_lists));
14085
14086           if (TREE_CODE (decl) != TYPE_DECL)
14087             {
14088               cp_parser_diagnose_invalid_type_name (parser,
14089                                                     parser->scope,
14090                                                     identifier,
14091                                                     token->location);
14092               return error_mark_node;
14093             }
14094
14095           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14096             {
14097               bool allow_template = (parser->num_template_parameter_lists
14098                                       || DECL_SELF_REFERENCE_P (decl));
14099               type = check_elaborated_type_specifier (tag_type, decl, 
14100                                                       allow_template);
14101
14102               if (type == error_mark_node)
14103                 return error_mark_node;
14104             }
14105
14106           /* Forward declarations of nested types, such as
14107
14108                class C1::C2;
14109                class C1::C2::C3;
14110
14111              are invalid unless all components preceding the final '::'
14112              are complete.  If all enclosing types are complete, these
14113              declarations become merely pointless.
14114
14115              Invalid forward declarations of nested types are errors
14116              caught elsewhere in parsing.  Those that are pointless arrive
14117              here.  */
14118
14119           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14120               && !is_friend && !processing_explicit_instantiation)
14121             warning (0, "declaration %qD does not declare anything", decl);
14122
14123           type = TREE_TYPE (decl);
14124         }
14125       else
14126         {
14127           /* An elaborated-type-specifier sometimes introduces a new type and
14128              sometimes names an existing type.  Normally, the rule is that it
14129              introduces a new type only if there is not an existing type of
14130              the same name already in scope.  For example, given:
14131
14132                struct S {};
14133                void f() { struct S s; }
14134
14135              the `struct S' in the body of `f' is the same `struct S' as in
14136              the global scope; the existing definition is used.  However, if
14137              there were no global declaration, this would introduce a new
14138              local class named `S'.
14139
14140              An exception to this rule applies to the following code:
14141
14142                namespace N { struct S; }
14143
14144              Here, the elaborated-type-specifier names a new type
14145              unconditionally; even if there is already an `S' in the
14146              containing scope this declaration names a new type.
14147              This exception only applies if the elaborated-type-specifier
14148              forms the complete declaration:
14149
14150                [class.name]
14151
14152                A declaration consisting solely of `class-key identifier ;' is
14153                either a redeclaration of the name in the current scope or a
14154                forward declaration of the identifier as a class name.  It
14155                introduces the name into the current scope.
14156
14157              We are in this situation precisely when the next token is a `;'.
14158
14159              An exception to the exception is that a `friend' declaration does
14160              *not* name a new type; i.e., given:
14161
14162                struct S { friend struct T; };
14163
14164              `T' is not a new type in the scope of `S'.
14165
14166              Also, `new struct S' or `sizeof (struct S)' never results in the
14167              definition of a new type; a new type can only be declared in a
14168              declaration context.  */
14169
14170           tag_scope ts;
14171           bool template_p;
14172
14173           if (is_friend)
14174             /* Friends have special name lookup rules.  */
14175             ts = ts_within_enclosing_non_class;
14176           else if (is_declaration
14177                    && cp_lexer_next_token_is (parser->lexer,
14178                                               CPP_SEMICOLON))
14179             /* This is a `class-key identifier ;' */
14180             ts = ts_current;
14181           else
14182             ts = ts_global;
14183
14184           template_p =
14185             (parser->num_template_parameter_lists
14186              && (cp_parser_next_token_starts_class_definition_p (parser)
14187                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14188           /* An unqualified name was used to reference this type, so
14189              there were no qualifying templates.  */
14190           if (!cp_parser_check_template_parameters (parser,
14191                                                     /*num_templates=*/0,
14192                                                     token->location,
14193                                                     /*declarator=*/NULL))
14194             return error_mark_node;
14195           type = xref_tag (tag_type, identifier, ts, template_p);
14196         }
14197     }
14198
14199   if (type == error_mark_node)
14200     return error_mark_node;
14201
14202   /* Allow attributes on forward declarations of classes.  */
14203   if (attributes)
14204     {
14205       if (TREE_CODE (type) == TYPENAME_TYPE)
14206         warning (OPT_Wattributes,
14207                  "attributes ignored on uninstantiated type");
14208       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14209                && ! processing_explicit_instantiation)
14210         warning (OPT_Wattributes,
14211                  "attributes ignored on template instantiation");
14212       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14213         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14214       else
14215         warning (OPT_Wattributes,
14216                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14217     }
14218
14219   if (tag_type != enum_type)
14220     {
14221       /* Indicate whether this class was declared as a `class' or as a
14222          `struct'.  */
14223       if (TREE_CODE (type) == RECORD_TYPE)
14224         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14225       cp_parser_check_class_key (tag_type, type);
14226     }
14227
14228   /* A "<" cannot follow an elaborated type specifier.  If that
14229      happens, the user was probably trying to form a template-id.  */
14230   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14231
14232   return type;
14233 }
14234
14235 /* Parse an enum-specifier.
14236
14237    enum-specifier:
14238      enum-head { enumerator-list [opt] }
14239      enum-head { enumerator-list , } [C++0x]
14240
14241    enum-head:
14242      enum-key identifier [opt] enum-base [opt]
14243      enum-key nested-name-specifier identifier enum-base [opt]
14244
14245    enum-key:
14246      enum
14247      enum class   [C++0x]
14248      enum struct  [C++0x]
14249
14250    enum-base:   [C++0x]
14251      : type-specifier-seq
14252
14253    opaque-enum-specifier:
14254      enum-key identifier enum-base [opt] ;
14255
14256    GNU Extensions:
14257      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14258        { enumerator-list [opt] }attributes[opt]
14259      enum-key attributes[opt] identifier [opt] enum-base [opt]
14260        { enumerator-list, }attributes[opt] [C++0x]
14261
14262    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14263    if the token stream isn't an enum-specifier after all.  */
14264
14265 static tree
14266 cp_parser_enum_specifier (cp_parser* parser)
14267 {
14268   tree identifier;
14269   tree type = NULL_TREE;
14270   tree prev_scope;
14271   tree nested_name_specifier = NULL_TREE;
14272   tree attributes;
14273   bool scoped_enum_p = false;
14274   bool has_underlying_type = false;
14275   bool nested_being_defined = false;
14276   bool new_value_list = false;
14277   bool is_new_type = false;
14278   bool is_anonymous = false;
14279   tree underlying_type = NULL_TREE;
14280   cp_token *type_start_token = NULL;
14281   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14282
14283   parser->colon_corrects_to_scope_p = false;
14284
14285   /* Parse tentatively so that we can back up if we don't find a
14286      enum-specifier.  */
14287   cp_parser_parse_tentatively (parser);
14288
14289   /* Caller guarantees that the current token is 'enum', an identifier
14290      possibly follows, and the token after that is an opening brace.
14291      If we don't have an identifier, fabricate an anonymous name for
14292      the enumeration being defined.  */
14293   cp_lexer_consume_token (parser->lexer);
14294
14295   /* Parse the "class" or "struct", which indicates a scoped
14296      enumeration type in C++0x.  */
14297   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14298       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14299     {
14300       if (cxx_dialect < cxx0x)
14301         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14302
14303       /* Consume the `struct' or `class' token.  */
14304       cp_lexer_consume_token (parser->lexer);
14305
14306       scoped_enum_p = true;
14307     }
14308
14309   attributes = cp_parser_attributes_opt (parser);
14310
14311   /* Clear the qualification.  */
14312   parser->scope = NULL_TREE;
14313   parser->qualifying_scope = NULL_TREE;
14314   parser->object_scope = NULL_TREE;
14315
14316   /* Figure out in what scope the declaration is being placed.  */
14317   prev_scope = current_scope ();
14318
14319   type_start_token = cp_lexer_peek_token (parser->lexer);
14320
14321   push_deferring_access_checks (dk_no_check);
14322   nested_name_specifier
14323       = cp_parser_nested_name_specifier_opt (parser,
14324                                              /*typename_keyword_p=*/true,
14325                                              /*check_dependency_p=*/false,
14326                                              /*type_p=*/false,
14327                                              /*is_declaration=*/false);
14328
14329   if (nested_name_specifier)
14330     {
14331       tree name;
14332
14333       identifier = cp_parser_identifier (parser);
14334       name =  cp_parser_lookup_name (parser, identifier,
14335                                      enum_type,
14336                                      /*is_template=*/false,
14337                                      /*is_namespace=*/false,
14338                                      /*check_dependency=*/true,
14339                                      /*ambiguous_decls=*/NULL,
14340                                      input_location);
14341       if (name)
14342         {
14343           type = TREE_TYPE (name);
14344           if (TREE_CODE (type) == TYPENAME_TYPE)
14345             {
14346               /* Are template enums allowed in ISO? */
14347               if (template_parm_scope_p ())
14348                 pedwarn (type_start_token->location, OPT_pedantic,
14349                          "%qD is an enumeration template", name);
14350               /* ignore a typename reference, for it will be solved by name
14351                  in start_enum.  */
14352               type = NULL_TREE;
14353             }
14354         }
14355       else
14356         error_at (type_start_token->location,
14357                   "%qD is not an enumerator-name", identifier);
14358     }
14359   else
14360     {
14361       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14362         identifier = cp_parser_identifier (parser);
14363       else
14364         {
14365           identifier = make_anon_name ();
14366           is_anonymous = true;
14367         }
14368     }
14369   pop_deferring_access_checks ();
14370
14371   /* Check for the `:' that denotes a specified underlying type in C++0x.
14372      Note that a ':' could also indicate a bitfield width, however.  */
14373   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14374     {
14375       cp_decl_specifier_seq type_specifiers;
14376
14377       /* Consume the `:'.  */
14378       cp_lexer_consume_token (parser->lexer);
14379
14380       /* Parse the type-specifier-seq.  */
14381       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14382                                     /*is_trailing_return=*/false,
14383                                     &type_specifiers);
14384
14385       /* At this point this is surely not elaborated type specifier.  */
14386       if (!cp_parser_parse_definitely (parser))
14387         return NULL_TREE;
14388
14389       if (cxx_dialect < cxx0x)
14390         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14391
14392       has_underlying_type = true;
14393
14394       /* If that didn't work, stop.  */
14395       if (type_specifiers.type != error_mark_node)
14396         {
14397           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14398                                             /*initialized=*/0, NULL);
14399           if (underlying_type == error_mark_node)
14400             underlying_type = NULL_TREE;
14401         }
14402     }
14403
14404   /* Look for the `{' but don't consume it yet.  */
14405   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14406     {
14407       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14408         {
14409           cp_parser_error (parser, "expected %<{%>");
14410           if (has_underlying_type)
14411             {
14412               type = NULL_TREE;
14413               goto out;
14414             }
14415         }
14416       /* An opaque-enum-specifier must have a ';' here.  */
14417       if ((scoped_enum_p || underlying_type)
14418           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14419         {
14420           cp_parser_error (parser, "expected %<;%> or %<{%>");
14421           if (has_underlying_type)
14422             {
14423               type = NULL_TREE;
14424               goto out;
14425             }
14426         }
14427     }
14428
14429   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14430     return NULL_TREE;
14431
14432   if (nested_name_specifier)
14433     {
14434       if (CLASS_TYPE_P (nested_name_specifier))
14435         {
14436           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14437           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14438           push_scope (nested_name_specifier);
14439         }
14440       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14441         {
14442           push_nested_namespace (nested_name_specifier);
14443         }
14444     }
14445
14446   /* Issue an error message if type-definitions are forbidden here.  */
14447   if (!cp_parser_check_type_definition (parser))
14448     type = error_mark_node;
14449   else
14450     /* Create the new type.  We do this before consuming the opening
14451        brace so the enum will be recorded as being on the line of its
14452        tag (or the 'enum' keyword, if there is no tag).  */
14453     type = start_enum (identifier, type, underlying_type,
14454                        scoped_enum_p, &is_new_type);
14455
14456   /* If the next token is not '{' it is an opaque-enum-specifier or an
14457      elaborated-type-specifier.  */
14458   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14459     {
14460       timevar_push (TV_PARSE_ENUM);
14461       if (nested_name_specifier)
14462         {
14463           /* The following catches invalid code such as:
14464              enum class S<int>::E { A, B, C }; */
14465           if (!processing_specialization
14466               && CLASS_TYPE_P (nested_name_specifier)
14467               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14468             error_at (type_start_token->location, "cannot add an enumerator "
14469                       "list to a template instantiation");
14470
14471           /* If that scope does not contain the scope in which the
14472              class was originally declared, the program is invalid.  */
14473           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14474             {
14475               if (at_namespace_scope_p ())
14476                 error_at (type_start_token->location,
14477                           "declaration of %qD in namespace %qD which does not "
14478                           "enclose %qD",
14479                           type, prev_scope, nested_name_specifier);
14480               else
14481                 error_at (type_start_token->location,
14482                           "declaration of %qD in %qD which does not enclose %qD",
14483                           type, prev_scope, nested_name_specifier);
14484               type = error_mark_node;
14485             }
14486         }
14487
14488       if (scoped_enum_p)
14489         begin_scope (sk_scoped_enum, type);
14490
14491       /* Consume the opening brace.  */
14492       cp_lexer_consume_token (parser->lexer);
14493
14494       if (type == error_mark_node)
14495         ; /* Nothing to add */
14496       else if (OPAQUE_ENUM_P (type)
14497                || (cxx_dialect > cxx98 && processing_specialization))
14498         {
14499           new_value_list = true;
14500           SET_OPAQUE_ENUM_P (type, false);
14501           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14502         }
14503       else
14504         {
14505           error_at (type_start_token->location, "multiple definition of %q#T", type);
14506           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14507                     "previous definition here");
14508           type = error_mark_node;
14509         }
14510
14511       if (type == error_mark_node)
14512         cp_parser_skip_to_end_of_block_or_statement (parser);
14513       /* If the next token is not '}', then there are some enumerators.  */
14514       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14515         cp_parser_enumerator_list (parser, type);
14516
14517       /* Consume the final '}'.  */
14518       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14519
14520       if (scoped_enum_p)
14521         finish_scope ();
14522       timevar_pop (TV_PARSE_ENUM);
14523     }
14524   else
14525     {
14526       /* If a ';' follows, then it is an opaque-enum-specifier
14527         and additional restrictions apply.  */
14528       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14529         {
14530           if (is_anonymous)
14531             error_at (type_start_token->location,
14532                       "opaque-enum-specifier without name");
14533           else if (nested_name_specifier)
14534             error_at (type_start_token->location,
14535                       "opaque-enum-specifier must use a simple identifier");
14536         }
14537     }
14538
14539   /* Look for trailing attributes to apply to this enumeration, and
14540      apply them if appropriate.  */
14541   if (cp_parser_allow_gnu_extensions_p (parser))
14542     {
14543       tree trailing_attr = cp_parser_attributes_opt (parser);
14544       trailing_attr = chainon (trailing_attr, attributes);
14545       cplus_decl_attributes (&type,
14546                              trailing_attr,
14547                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14548     }
14549
14550   /* Finish up the enumeration.  */
14551   if (type != error_mark_node)
14552     {
14553       if (new_value_list)
14554         finish_enum_value_list (type);
14555       if (is_new_type)
14556         finish_enum (type);
14557     }
14558
14559   if (nested_name_specifier)
14560     {
14561       if (CLASS_TYPE_P (nested_name_specifier))
14562         {
14563           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14564           pop_scope (nested_name_specifier);
14565         }
14566       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14567         {
14568           pop_nested_namespace (nested_name_specifier);
14569         }
14570     }
14571  out:
14572   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14573   return type;
14574 }
14575
14576 /* Parse an enumerator-list.  The enumerators all have the indicated
14577    TYPE.
14578
14579    enumerator-list:
14580      enumerator-definition
14581      enumerator-list , enumerator-definition  */
14582
14583 static void
14584 cp_parser_enumerator_list (cp_parser* parser, tree type)
14585 {
14586   while (true)
14587     {
14588       /* Parse an enumerator-definition.  */
14589       cp_parser_enumerator_definition (parser, type);
14590
14591       /* If the next token is not a ',', we've reached the end of
14592          the list.  */
14593       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14594         break;
14595       /* Otherwise, consume the `,' and keep going.  */
14596       cp_lexer_consume_token (parser->lexer);
14597       /* If the next token is a `}', there is a trailing comma.  */
14598       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14599         {
14600           if (cxx_dialect < cxx0x && !in_system_header)
14601             pedwarn (input_location, OPT_pedantic,
14602                      "comma at end of enumerator list");
14603           break;
14604         }
14605     }
14606 }
14607
14608 /* Parse an enumerator-definition.  The enumerator has the indicated
14609    TYPE.
14610
14611    enumerator-definition:
14612      enumerator
14613      enumerator = constant-expression
14614
14615    enumerator:
14616      identifier  */
14617
14618 static void
14619 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14620 {
14621   tree identifier;
14622   tree value;
14623   location_t loc;
14624
14625   /* Save the input location because we are interested in the location
14626      of the identifier and not the location of the explicit value.  */
14627   loc = cp_lexer_peek_token (parser->lexer)->location;
14628
14629   /* Look for the identifier.  */
14630   identifier = cp_parser_identifier (parser);
14631   if (identifier == error_mark_node)
14632     return;
14633
14634   /* If the next token is an '=', then there is an explicit value.  */
14635   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14636     {
14637       /* Consume the `=' token.  */
14638       cp_lexer_consume_token (parser->lexer);
14639       /* Parse the value.  */
14640       value = cp_parser_constant_expression (parser,
14641                                              /*allow_non_constant_p=*/false,
14642                                              NULL);
14643     }
14644   else
14645     value = NULL_TREE;
14646
14647   /* If we are processing a template, make sure the initializer of the
14648      enumerator doesn't contain any bare template parameter pack.  */
14649   if (check_for_bare_parameter_packs (value))
14650     value = error_mark_node;
14651
14652   /* integral_constant_value will pull out this expression, so make sure
14653      it's folded as appropriate.  */
14654   value = fold_non_dependent_expr (value);
14655
14656   /* Create the enumerator.  */
14657   build_enumerator (identifier, value, type, loc);
14658 }
14659
14660 /* Parse a namespace-name.
14661
14662    namespace-name:
14663      original-namespace-name
14664      namespace-alias
14665
14666    Returns the NAMESPACE_DECL for the namespace.  */
14667
14668 static tree
14669 cp_parser_namespace_name (cp_parser* parser)
14670 {
14671   tree identifier;
14672   tree namespace_decl;
14673
14674   cp_token *token = cp_lexer_peek_token (parser->lexer);
14675
14676   /* Get the name of the namespace.  */
14677   identifier = cp_parser_identifier (parser);
14678   if (identifier == error_mark_node)
14679     return error_mark_node;
14680
14681   /* Look up the identifier in the currently active scope.  Look only
14682      for namespaces, due to:
14683
14684        [basic.lookup.udir]
14685
14686        When looking up a namespace-name in a using-directive or alias
14687        definition, only namespace names are considered.
14688
14689      And:
14690
14691        [basic.lookup.qual]
14692
14693        During the lookup of a name preceding the :: scope resolution
14694        operator, object, function, and enumerator names are ignored.
14695
14696      (Note that cp_parser_qualifying_entity only calls this
14697      function if the token after the name is the scope resolution
14698      operator.)  */
14699   namespace_decl = cp_parser_lookup_name (parser, identifier,
14700                                           none_type,
14701                                           /*is_template=*/false,
14702                                           /*is_namespace=*/true,
14703                                           /*check_dependency=*/true,
14704                                           /*ambiguous_decls=*/NULL,
14705                                           token->location);
14706   /* If it's not a namespace, issue an error.  */
14707   if (namespace_decl == error_mark_node
14708       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14709     {
14710       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14711         error_at (token->location, "%qD is not a namespace-name", identifier);
14712       cp_parser_error (parser, "expected namespace-name");
14713       namespace_decl = error_mark_node;
14714     }
14715
14716   return namespace_decl;
14717 }
14718
14719 /* Parse a namespace-definition.
14720
14721    namespace-definition:
14722      named-namespace-definition
14723      unnamed-namespace-definition
14724
14725    named-namespace-definition:
14726      original-namespace-definition
14727      extension-namespace-definition
14728
14729    original-namespace-definition:
14730      namespace identifier { namespace-body }
14731
14732    extension-namespace-definition:
14733      namespace original-namespace-name { namespace-body }
14734
14735    unnamed-namespace-definition:
14736      namespace { namespace-body } */
14737
14738 static void
14739 cp_parser_namespace_definition (cp_parser* parser)
14740 {
14741   tree identifier, attribs;
14742   bool has_visibility;
14743   bool is_inline;
14744
14745   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14746     {
14747       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14748       is_inline = true;
14749       cp_lexer_consume_token (parser->lexer);
14750     }
14751   else
14752     is_inline = false;
14753
14754   /* Look for the `namespace' keyword.  */
14755   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14756
14757   /* Get the name of the namespace.  We do not attempt to distinguish
14758      between an original-namespace-definition and an
14759      extension-namespace-definition at this point.  The semantic
14760      analysis routines are responsible for that.  */
14761   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14762     identifier = cp_parser_identifier (parser);
14763   else
14764     identifier = NULL_TREE;
14765
14766   /* Parse any specified attributes.  */
14767   attribs = cp_parser_attributes_opt (parser);
14768
14769   /* Look for the `{' to start the namespace.  */
14770   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14771   /* Start the namespace.  */
14772   push_namespace (identifier);
14773
14774   /* "inline namespace" is equivalent to a stub namespace definition
14775      followed by a strong using directive.  */
14776   if (is_inline)
14777     {
14778       tree name_space = current_namespace;
14779       /* Set up namespace association.  */
14780       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14781         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14782                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14783       /* Import the contents of the inline namespace.  */
14784       pop_namespace ();
14785       do_using_directive (name_space);
14786       push_namespace (identifier);
14787     }
14788
14789   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14790
14791   /* Parse the body of the namespace.  */
14792   cp_parser_namespace_body (parser);
14793
14794   if (has_visibility)
14795     pop_visibility (1);
14796
14797   /* Finish the namespace.  */
14798   pop_namespace ();
14799   /* Look for the final `}'.  */
14800   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14801 }
14802
14803 /* Parse a namespace-body.
14804
14805    namespace-body:
14806      declaration-seq [opt]  */
14807
14808 static void
14809 cp_parser_namespace_body (cp_parser* parser)
14810 {
14811   cp_parser_declaration_seq_opt (parser);
14812 }
14813
14814 /* Parse a namespace-alias-definition.
14815
14816    namespace-alias-definition:
14817      namespace identifier = qualified-namespace-specifier ;  */
14818
14819 static void
14820 cp_parser_namespace_alias_definition (cp_parser* parser)
14821 {
14822   tree identifier;
14823   tree namespace_specifier;
14824
14825   cp_token *token = cp_lexer_peek_token (parser->lexer);
14826
14827   /* Look for the `namespace' keyword.  */
14828   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14829   /* Look for the identifier.  */
14830   identifier = cp_parser_identifier (parser);
14831   if (identifier == error_mark_node)
14832     return;
14833   /* Look for the `=' token.  */
14834   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14835       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14836     {
14837       error_at (token->location, "%<namespace%> definition is not allowed here");
14838       /* Skip the definition.  */
14839       cp_lexer_consume_token (parser->lexer);
14840       if (cp_parser_skip_to_closing_brace (parser))
14841         cp_lexer_consume_token (parser->lexer);
14842       return;
14843     }
14844   cp_parser_require (parser, CPP_EQ, RT_EQ);
14845   /* Look for the qualified-namespace-specifier.  */
14846   namespace_specifier
14847     = cp_parser_qualified_namespace_specifier (parser);
14848   /* Look for the `;' token.  */
14849   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14850
14851   /* Register the alias in the symbol table.  */
14852   do_namespace_alias (identifier, namespace_specifier);
14853 }
14854
14855 /* Parse a qualified-namespace-specifier.
14856
14857    qualified-namespace-specifier:
14858      :: [opt] nested-name-specifier [opt] namespace-name
14859
14860    Returns a NAMESPACE_DECL corresponding to the specified
14861    namespace.  */
14862
14863 static tree
14864 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14865 {
14866   /* Look for the optional `::'.  */
14867   cp_parser_global_scope_opt (parser,
14868                               /*current_scope_valid_p=*/false);
14869
14870   /* Look for the optional nested-name-specifier.  */
14871   cp_parser_nested_name_specifier_opt (parser,
14872                                        /*typename_keyword_p=*/false,
14873                                        /*check_dependency_p=*/true,
14874                                        /*type_p=*/false,
14875                                        /*is_declaration=*/true);
14876
14877   return cp_parser_namespace_name (parser);
14878 }
14879
14880 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14881    access declaration.
14882
14883    using-declaration:
14884      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14885      using :: unqualified-id ;  
14886
14887    access-declaration:
14888      qualified-id ;  
14889
14890    */
14891
14892 static bool
14893 cp_parser_using_declaration (cp_parser* parser, 
14894                              bool access_declaration_p)
14895 {
14896   cp_token *token;
14897   bool typename_p = false;
14898   bool global_scope_p;
14899   tree decl;
14900   tree identifier;
14901   tree qscope;
14902   int oldcount = errorcount;
14903   cp_token *diag_token = NULL;
14904
14905   if (access_declaration_p)
14906     {
14907       diag_token = cp_lexer_peek_token (parser->lexer);
14908       cp_parser_parse_tentatively (parser);
14909     }
14910   else
14911     {
14912       /* Look for the `using' keyword.  */
14913       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14914       
14915       /* Peek at the next token.  */
14916       token = cp_lexer_peek_token (parser->lexer);
14917       /* See if it's `typename'.  */
14918       if (token->keyword == RID_TYPENAME)
14919         {
14920           /* Remember that we've seen it.  */
14921           typename_p = true;
14922           /* Consume the `typename' token.  */
14923           cp_lexer_consume_token (parser->lexer);
14924         }
14925     }
14926
14927   /* Look for the optional global scope qualification.  */
14928   global_scope_p
14929     = (cp_parser_global_scope_opt (parser,
14930                                    /*current_scope_valid_p=*/false)
14931        != NULL_TREE);
14932
14933   /* If we saw `typename', or didn't see `::', then there must be a
14934      nested-name-specifier present.  */
14935   if (typename_p || !global_scope_p)
14936     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14937                                               /*check_dependency_p=*/true,
14938                                               /*type_p=*/false,
14939                                               /*is_declaration=*/true);
14940   /* Otherwise, we could be in either of the two productions.  In that
14941      case, treat the nested-name-specifier as optional.  */
14942   else
14943     qscope = cp_parser_nested_name_specifier_opt (parser,
14944                                                   /*typename_keyword_p=*/false,
14945                                                   /*check_dependency_p=*/true,
14946                                                   /*type_p=*/false,
14947                                                   /*is_declaration=*/true);
14948   if (!qscope)
14949     qscope = global_namespace;
14950
14951   if (access_declaration_p && cp_parser_error_occurred (parser))
14952     /* Something has already gone wrong; there's no need to parse
14953        further.  Since an error has occurred, the return value of
14954        cp_parser_parse_definitely will be false, as required.  */
14955     return cp_parser_parse_definitely (parser);
14956
14957   token = cp_lexer_peek_token (parser->lexer);
14958   /* Parse the unqualified-id.  */
14959   identifier = cp_parser_unqualified_id (parser,
14960                                          /*template_keyword_p=*/false,
14961                                          /*check_dependency_p=*/true,
14962                                          /*declarator_p=*/true,
14963                                          /*optional_p=*/false);
14964
14965   if (access_declaration_p)
14966     {
14967       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14968         cp_parser_simulate_error (parser);
14969       if (!cp_parser_parse_definitely (parser))
14970         return false;
14971     }
14972
14973   /* The function we call to handle a using-declaration is different
14974      depending on what scope we are in.  */
14975   if (qscope == error_mark_node || identifier == error_mark_node)
14976     ;
14977   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14978            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14979     /* [namespace.udecl]
14980
14981        A using declaration shall not name a template-id.  */
14982     error_at (token->location,
14983               "a template-id may not appear in a using-declaration");
14984   else
14985     {
14986       if (at_class_scope_p ())
14987         {
14988           /* Create the USING_DECL.  */
14989           decl = do_class_using_decl (parser->scope, identifier);
14990
14991           if (decl && typename_p)
14992             USING_DECL_TYPENAME_P (decl) = 1;
14993
14994           if (check_for_bare_parameter_packs (decl))
14995             return false;
14996           else
14997             /* Add it to the list of members in this class.  */
14998             finish_member_declaration (decl);
14999         }
15000       else
15001         {
15002           decl = cp_parser_lookup_name_simple (parser,
15003                                                identifier,
15004                                                token->location);
15005           if (decl == error_mark_node)
15006             cp_parser_name_lookup_error (parser, identifier,
15007                                          decl, NLE_NULL,
15008                                          token->location);
15009           else if (check_for_bare_parameter_packs (decl))
15010             return false;
15011           else if (!at_namespace_scope_p ())
15012             do_local_using_decl (decl, qscope, identifier);
15013           else
15014             do_toplevel_using_decl (decl, qscope, identifier);
15015         }
15016     }
15017
15018   /* Look for the final `;'.  */
15019   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15020
15021   if (access_declaration_p && errorcount == oldcount)
15022     warning_at (diag_token->location, OPT_Wdeprecated,
15023                 "access declarations are deprecated "
15024                 "in favour of using-declarations; "
15025                 "suggestion: add the %<using%> keyword");
15026
15027   return true;
15028 }
15029
15030 /* Parse an alias-declaration.
15031
15032    alias-declaration:
15033      using identifier attribute-specifier-seq [opt] = type-id  */
15034
15035 static tree
15036 cp_parser_alias_declaration (cp_parser* parser)
15037 {
15038   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15039   location_t id_location;
15040   cp_declarator *declarator;
15041   cp_decl_specifier_seq decl_specs;
15042   bool member_p;
15043   const char *saved_message = NULL;
15044
15045   /* Look for the `using' keyword.  */
15046   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15047   id_location = cp_lexer_peek_token (parser->lexer)->location;
15048   id = cp_parser_identifier (parser);
15049   if (id == error_mark_node)
15050     return error_mark_node;
15051
15052   attributes = cp_parser_attributes_opt (parser);
15053   if (attributes == error_mark_node)
15054     return error_mark_node;
15055
15056   cp_parser_require (parser, CPP_EQ, RT_EQ);
15057
15058   /* Now we are going to parse the type-id of the declaration.  */
15059
15060   /*
15061     [dcl.type]/3 says:
15062
15063         "A type-specifier-seq shall not define a class or enumeration
15064          unless it appears in the type-id of an alias-declaration (7.1.3) that
15065          is not the declaration of a template-declaration."
15066
15067     In other words, if we currently are in an alias template, the
15068     type-id should not define a type.
15069
15070     So let's set parser->type_definition_forbidden_message in that
15071     case; cp_parser_check_type_definition (called by
15072     cp_parser_class_specifier) will then emit an error if a type is
15073     defined in the type-id.  */
15074   if (parser->num_template_parameter_lists)
15075     {
15076       saved_message = parser->type_definition_forbidden_message;
15077       parser->type_definition_forbidden_message =
15078         G_("types may not be defined in alias template declarations");
15079     }
15080
15081   type = cp_parser_type_id (parser);
15082
15083   /* Restore the error message if need be.  */
15084   if (parser->num_template_parameter_lists)
15085     parser->type_definition_forbidden_message = saved_message;
15086
15087   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15088
15089   if (cp_parser_error_occurred (parser))
15090     return error_mark_node;
15091
15092   /* A typedef-name can also be introduced by an alias-declaration. The
15093      identifier following the using keyword becomes a typedef-name. It has
15094      the same semantics as if it were introduced by the typedef
15095      specifier. In particular, it does not define a new type and it shall
15096      not appear in the type-id.  */
15097
15098   clear_decl_specs (&decl_specs);
15099   decl_specs.type = type;
15100   decl_specs.attributes = attributes;
15101   ++decl_specs.specs[(int) ds_typedef];
15102   ++decl_specs.specs[(int) ds_alias];
15103
15104   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15105   declarator->id_loc = id_location;
15106
15107   member_p = at_class_scope_p ();
15108   if (member_p)
15109     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15110                       NULL_TREE, attributes);
15111   else
15112     decl = start_decl (declarator, &decl_specs, 0,
15113                        attributes, NULL_TREE, &pushed_scope);
15114   if (decl == error_mark_node)
15115     return decl;
15116
15117   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15118
15119   if (pushed_scope)
15120     pop_scope (pushed_scope);
15121
15122   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15123      added into the symbol table; otherwise, return the TYPE_DECL.  */
15124   if (DECL_LANG_SPECIFIC (decl)
15125       && DECL_TEMPLATE_INFO (decl)
15126       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15127     {
15128       decl = DECL_TI_TEMPLATE (decl);
15129       if (member_p)
15130         check_member_template (decl);
15131     }
15132
15133   return decl;
15134 }
15135
15136 /* Parse a using-directive.
15137
15138    using-directive:
15139      using namespace :: [opt] nested-name-specifier [opt]
15140        namespace-name ;  */
15141
15142 static void
15143 cp_parser_using_directive (cp_parser* parser)
15144 {
15145   tree namespace_decl;
15146   tree attribs;
15147
15148   /* Look for the `using' keyword.  */
15149   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15150   /* And the `namespace' keyword.  */
15151   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15152   /* Look for the optional `::' operator.  */
15153   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15154   /* And the optional nested-name-specifier.  */
15155   cp_parser_nested_name_specifier_opt (parser,
15156                                        /*typename_keyword_p=*/false,
15157                                        /*check_dependency_p=*/true,
15158                                        /*type_p=*/false,
15159                                        /*is_declaration=*/true);
15160   /* Get the namespace being used.  */
15161   namespace_decl = cp_parser_namespace_name (parser);
15162   /* And any specified attributes.  */
15163   attribs = cp_parser_attributes_opt (parser);
15164   /* Update the symbol table.  */
15165   parse_using_directive (namespace_decl, attribs);
15166   /* Look for the final `;'.  */
15167   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15168 }
15169
15170 /* Parse an asm-definition.
15171
15172    asm-definition:
15173      asm ( string-literal ) ;
15174
15175    GNU Extension:
15176
15177    asm-definition:
15178      asm volatile [opt] ( string-literal ) ;
15179      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15180      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15181                           : asm-operand-list [opt] ) ;
15182      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15183                           : asm-operand-list [opt]
15184                           : asm-clobber-list [opt] ) ;
15185      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15186                                : asm-clobber-list [opt]
15187                                : asm-goto-list ) ;  */
15188
15189 static void
15190 cp_parser_asm_definition (cp_parser* parser)
15191 {
15192   tree string;
15193   tree outputs = NULL_TREE;
15194   tree inputs = NULL_TREE;
15195   tree clobbers = NULL_TREE;
15196   tree labels = NULL_TREE;
15197   tree asm_stmt;
15198   bool volatile_p = false;
15199   bool extended_p = false;
15200   bool invalid_inputs_p = false;
15201   bool invalid_outputs_p = false;
15202   bool goto_p = false;
15203   required_token missing = RT_NONE;
15204
15205   /* Look for the `asm' keyword.  */
15206   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15207   /* See if the next token is `volatile'.  */
15208   if (cp_parser_allow_gnu_extensions_p (parser)
15209       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15210     {
15211       /* Remember that we saw the `volatile' keyword.  */
15212       volatile_p = true;
15213       /* Consume the token.  */
15214       cp_lexer_consume_token (parser->lexer);
15215     }
15216   if (cp_parser_allow_gnu_extensions_p (parser)
15217       && parser->in_function_body
15218       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15219     {
15220       /* Remember that we saw the `goto' keyword.  */
15221       goto_p = true;
15222       /* Consume the token.  */
15223       cp_lexer_consume_token (parser->lexer);
15224     }
15225   /* Look for the opening `('.  */
15226   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15227     return;
15228   /* Look for the string.  */
15229   string = cp_parser_string_literal (parser, false, false);
15230   if (string == error_mark_node)
15231     {
15232       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15233                                              /*consume_paren=*/true);
15234       return;
15235     }
15236
15237   /* If we're allowing GNU extensions, check for the extended assembly
15238      syntax.  Unfortunately, the `:' tokens need not be separated by
15239      a space in C, and so, for compatibility, we tolerate that here
15240      too.  Doing that means that we have to treat the `::' operator as
15241      two `:' tokens.  */
15242   if (cp_parser_allow_gnu_extensions_p (parser)
15243       && parser->in_function_body
15244       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15245           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15246     {
15247       bool inputs_p = false;
15248       bool clobbers_p = false;
15249       bool labels_p = false;
15250
15251       /* The extended syntax was used.  */
15252       extended_p = true;
15253
15254       /* Look for outputs.  */
15255       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15256         {
15257           /* Consume the `:'.  */
15258           cp_lexer_consume_token (parser->lexer);
15259           /* Parse the output-operands.  */
15260           if (cp_lexer_next_token_is_not (parser->lexer,
15261                                           CPP_COLON)
15262               && cp_lexer_next_token_is_not (parser->lexer,
15263                                              CPP_SCOPE)
15264               && cp_lexer_next_token_is_not (parser->lexer,
15265                                              CPP_CLOSE_PAREN)
15266               && !goto_p)
15267             outputs = cp_parser_asm_operand_list (parser);
15268
15269             if (outputs == error_mark_node)
15270               invalid_outputs_p = true;
15271         }
15272       /* If the next token is `::', there are no outputs, and the
15273          next token is the beginning of the inputs.  */
15274       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15275         /* The inputs are coming next.  */
15276         inputs_p = true;
15277
15278       /* Look for inputs.  */
15279       if (inputs_p
15280           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15281         {
15282           /* Consume the `:' or `::'.  */
15283           cp_lexer_consume_token (parser->lexer);
15284           /* Parse the output-operands.  */
15285           if (cp_lexer_next_token_is_not (parser->lexer,
15286                                           CPP_COLON)
15287               && cp_lexer_next_token_is_not (parser->lexer,
15288                                              CPP_SCOPE)
15289               && cp_lexer_next_token_is_not (parser->lexer,
15290                                              CPP_CLOSE_PAREN))
15291             inputs = cp_parser_asm_operand_list (parser);
15292
15293             if (inputs == error_mark_node)
15294               invalid_inputs_p = true;
15295         }
15296       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15297         /* The clobbers are coming next.  */
15298         clobbers_p = true;
15299
15300       /* Look for clobbers.  */
15301       if (clobbers_p
15302           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15303         {
15304           clobbers_p = true;
15305           /* Consume the `:' or `::'.  */
15306           cp_lexer_consume_token (parser->lexer);
15307           /* Parse the clobbers.  */
15308           if (cp_lexer_next_token_is_not (parser->lexer,
15309                                           CPP_COLON)
15310               && cp_lexer_next_token_is_not (parser->lexer,
15311                                              CPP_CLOSE_PAREN))
15312             clobbers = cp_parser_asm_clobber_list (parser);
15313         }
15314       else if (goto_p
15315                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15316         /* The labels are coming next.  */
15317         labels_p = true;
15318
15319       /* Look for labels.  */
15320       if (labels_p
15321           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15322         {
15323           labels_p = true;
15324           /* Consume the `:' or `::'.  */
15325           cp_lexer_consume_token (parser->lexer);
15326           /* Parse the labels.  */
15327           labels = cp_parser_asm_label_list (parser);
15328         }
15329
15330       if (goto_p && !labels_p)
15331         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15332     }
15333   else if (goto_p)
15334     missing = RT_COLON_SCOPE;
15335
15336   /* Look for the closing `)'.  */
15337   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15338                           missing ? missing : RT_CLOSE_PAREN))
15339     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15340                                            /*consume_paren=*/true);
15341   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15342
15343   if (!invalid_inputs_p && !invalid_outputs_p)
15344     {
15345       /* Create the ASM_EXPR.  */
15346       if (parser->in_function_body)
15347         {
15348           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15349                                       inputs, clobbers, labels);
15350           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15351           if (!extended_p)
15352             {
15353               tree temp = asm_stmt;
15354               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15355                 temp = TREE_OPERAND (temp, 0);
15356
15357               ASM_INPUT_P (temp) = 1;
15358             }
15359         }
15360       else
15361         cgraph_add_asm_node (string);
15362     }
15363 }
15364
15365 /* Declarators [gram.dcl.decl] */
15366
15367 /* Parse an init-declarator.
15368
15369    init-declarator:
15370      declarator initializer [opt]
15371
15372    GNU Extension:
15373
15374    init-declarator:
15375      declarator asm-specification [opt] attributes [opt] initializer [opt]
15376
15377    function-definition:
15378      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15379        function-body
15380      decl-specifier-seq [opt] declarator function-try-block
15381
15382    GNU Extension:
15383
15384    function-definition:
15385      __extension__ function-definition
15386
15387    TM Extension:
15388
15389    function-definition:
15390      decl-specifier-seq [opt] declarator function-transaction-block
15391
15392    The DECL_SPECIFIERS apply to this declarator.  Returns a
15393    representation of the entity declared.  If MEMBER_P is TRUE, then
15394    this declarator appears in a class scope.  The new DECL created by
15395    this declarator is returned.
15396
15397    The CHECKS are access checks that should be performed once we know
15398    what entity is being declared (and, therefore, what classes have
15399    befriended it).
15400
15401    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15402    for a function-definition here as well.  If the declarator is a
15403    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15404    be TRUE upon return.  By that point, the function-definition will
15405    have been completely parsed.
15406
15407    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15408    is FALSE.
15409
15410    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15411    parsed declaration if it is an uninitialized single declarator not followed
15412    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15413    if present, will not be consumed.  If returned, this declarator will be
15414    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15415
15416 static tree
15417 cp_parser_init_declarator (cp_parser* parser,
15418                            cp_decl_specifier_seq *decl_specifiers,
15419                            VEC (deferred_access_check,gc)* checks,
15420                            bool function_definition_allowed_p,
15421                            bool member_p,
15422                            int declares_class_or_enum,
15423                            bool* function_definition_p,
15424                            tree* maybe_range_for_decl)
15425 {
15426   cp_token *token = NULL, *asm_spec_start_token = NULL,
15427            *attributes_start_token = NULL;
15428   cp_declarator *declarator;
15429   tree prefix_attributes;
15430   tree attributes;
15431   tree asm_specification;
15432   tree initializer;
15433   tree decl = NULL_TREE;
15434   tree scope;
15435   int is_initialized;
15436   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15437      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15438      "(...)".  */
15439   enum cpp_ttype initialization_kind;
15440   bool is_direct_init = false;
15441   bool is_non_constant_init;
15442   int ctor_dtor_or_conv_p;
15443   bool friend_p;
15444   tree pushed_scope = NULL_TREE;
15445   bool range_for_decl_p = false;
15446
15447   /* Gather the attributes that were provided with the
15448      decl-specifiers.  */
15449   prefix_attributes = decl_specifiers->attributes;
15450
15451   /* Assume that this is not the declarator for a function
15452      definition.  */
15453   if (function_definition_p)
15454     *function_definition_p = false;
15455
15456   /* Defer access checks while parsing the declarator; we cannot know
15457      what names are accessible until we know what is being
15458      declared.  */
15459   resume_deferring_access_checks ();
15460
15461   /* Parse the declarator.  */
15462   token = cp_lexer_peek_token (parser->lexer);
15463   declarator
15464     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15465                             &ctor_dtor_or_conv_p,
15466                             /*parenthesized_p=*/NULL,
15467                             member_p);
15468   /* Gather up the deferred checks.  */
15469   stop_deferring_access_checks ();
15470
15471   /* If the DECLARATOR was erroneous, there's no need to go
15472      further.  */
15473   if (declarator == cp_error_declarator)
15474     return error_mark_node;
15475
15476   /* Check that the number of template-parameter-lists is OK.  */
15477   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15478                                                        token->location))
15479     return error_mark_node;
15480
15481   if (declares_class_or_enum & 2)
15482     cp_parser_check_for_definition_in_return_type (declarator,
15483                                                    decl_specifiers->type,
15484                                                    decl_specifiers->type_location);
15485
15486   /* Figure out what scope the entity declared by the DECLARATOR is
15487      located in.  `grokdeclarator' sometimes changes the scope, so
15488      we compute it now.  */
15489   scope = get_scope_of_declarator (declarator);
15490
15491   /* Perform any lookups in the declared type which were thought to be
15492      dependent, but are not in the scope of the declarator.  */
15493   decl_specifiers->type
15494     = maybe_update_decl_type (decl_specifiers->type, scope);
15495
15496   /* If we're allowing GNU extensions, look for an asm-specification
15497      and attributes.  */
15498   if (cp_parser_allow_gnu_extensions_p (parser))
15499     {
15500       /* Look for an asm-specification.  */
15501       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15502       asm_specification = cp_parser_asm_specification_opt (parser);
15503       /* And attributes.  */
15504       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15505       attributes = cp_parser_attributes_opt (parser);
15506     }
15507   else
15508     {
15509       asm_specification = NULL_TREE;
15510       attributes = NULL_TREE;
15511     }
15512
15513   /* Peek at the next token.  */
15514   token = cp_lexer_peek_token (parser->lexer);
15515   /* Check to see if the token indicates the start of a
15516      function-definition.  */
15517   if (function_declarator_p (declarator)
15518       && cp_parser_token_starts_function_definition_p (token))
15519     {
15520       if (!function_definition_allowed_p)
15521         {
15522           /* If a function-definition should not appear here, issue an
15523              error message.  */
15524           cp_parser_error (parser,
15525                            "a function-definition is not allowed here");
15526           return error_mark_node;
15527         }
15528       else
15529         {
15530           location_t func_brace_location
15531             = cp_lexer_peek_token (parser->lexer)->location;
15532
15533           /* Neither attributes nor an asm-specification are allowed
15534              on a function-definition.  */
15535           if (asm_specification)
15536             error_at (asm_spec_start_token->location,
15537                       "an asm-specification is not allowed "
15538                       "on a function-definition");
15539           if (attributes)
15540             error_at (attributes_start_token->location,
15541                       "attributes are not allowed on a function-definition");
15542           /* This is a function-definition.  */
15543           *function_definition_p = true;
15544
15545           /* Parse the function definition.  */
15546           if (member_p)
15547             decl = cp_parser_save_member_function_body (parser,
15548                                                         decl_specifiers,
15549                                                         declarator,
15550                                                         prefix_attributes);
15551           else
15552             decl
15553               = (cp_parser_function_definition_from_specifiers_and_declarator
15554                  (parser, decl_specifiers, prefix_attributes, declarator));
15555
15556           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15557             {
15558               /* This is where the prologue starts...  */
15559               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15560                 = func_brace_location;
15561             }
15562
15563           return decl;
15564         }
15565     }
15566
15567   /* [dcl.dcl]
15568
15569      Only in function declarations for constructors, destructors, and
15570      type conversions can the decl-specifier-seq be omitted.
15571
15572      We explicitly postpone this check past the point where we handle
15573      function-definitions because we tolerate function-definitions
15574      that are missing their return types in some modes.  */
15575   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15576     {
15577       cp_parser_error (parser,
15578                        "expected constructor, destructor, or type conversion");
15579       return error_mark_node;
15580     }
15581
15582   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15583   if (token->type == CPP_EQ
15584       || token->type == CPP_OPEN_PAREN
15585       || token->type == CPP_OPEN_BRACE)
15586     {
15587       is_initialized = SD_INITIALIZED;
15588       initialization_kind = token->type;
15589       if (maybe_range_for_decl)
15590         *maybe_range_for_decl = error_mark_node;
15591
15592       if (token->type == CPP_EQ
15593           && function_declarator_p (declarator))
15594         {
15595           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15596           if (t2->keyword == RID_DEFAULT)
15597             is_initialized = SD_DEFAULTED;
15598           else if (t2->keyword == RID_DELETE)
15599             is_initialized = SD_DELETED;
15600         }
15601     }
15602   else
15603     {
15604       /* If the init-declarator isn't initialized and isn't followed by a
15605          `,' or `;', it's not a valid init-declarator.  */
15606       if (token->type != CPP_COMMA
15607           && token->type != CPP_SEMICOLON)
15608         {
15609           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15610             range_for_decl_p = true;
15611           else
15612             {
15613               cp_parser_error (parser, "expected initializer");
15614               return error_mark_node;
15615             }
15616         }
15617       is_initialized = SD_UNINITIALIZED;
15618       initialization_kind = CPP_EOF;
15619     }
15620
15621   /* Because start_decl has side-effects, we should only call it if we
15622      know we're going ahead.  By this point, we know that we cannot
15623      possibly be looking at any other construct.  */
15624   cp_parser_commit_to_tentative_parse (parser);
15625
15626   /* If the decl specifiers were bad, issue an error now that we're
15627      sure this was intended to be a declarator.  Then continue
15628      declaring the variable(s), as int, to try to cut down on further
15629      errors.  */
15630   if (decl_specifiers->any_specifiers_p
15631       && decl_specifiers->type == error_mark_node)
15632     {
15633       cp_parser_error (parser, "invalid type in declaration");
15634       decl_specifiers->type = integer_type_node;
15635     }
15636
15637   /* Check to see whether or not this declaration is a friend.  */
15638   friend_p = cp_parser_friend_p (decl_specifiers);
15639
15640   /* Enter the newly declared entry in the symbol table.  If we're
15641      processing a declaration in a class-specifier, we wait until
15642      after processing the initializer.  */
15643   if (!member_p)
15644     {
15645       if (parser->in_unbraced_linkage_specification_p)
15646         decl_specifiers->storage_class = sc_extern;
15647       decl = start_decl (declarator, decl_specifiers,
15648                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15649                          attributes, prefix_attributes,
15650                          &pushed_scope);
15651       /* Adjust location of decl if declarator->id_loc is more appropriate:
15652          set, and decl wasn't merged with another decl, in which case its
15653          location would be different from input_location, and more accurate.  */
15654       if (DECL_P (decl)
15655           && declarator->id_loc != UNKNOWN_LOCATION
15656           && DECL_SOURCE_LOCATION (decl) == input_location)
15657         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15658     }
15659   else if (scope)
15660     /* Enter the SCOPE.  That way unqualified names appearing in the
15661        initializer will be looked up in SCOPE.  */
15662     pushed_scope = push_scope (scope);
15663
15664   /* Perform deferred access control checks, now that we know in which
15665      SCOPE the declared entity resides.  */
15666   if (!member_p && decl)
15667     {
15668       tree saved_current_function_decl = NULL_TREE;
15669
15670       /* If the entity being declared is a function, pretend that we
15671          are in its scope.  If it is a `friend', it may have access to
15672          things that would not otherwise be accessible.  */
15673       if (TREE_CODE (decl) == FUNCTION_DECL)
15674         {
15675           saved_current_function_decl = current_function_decl;
15676           current_function_decl = decl;
15677         }
15678
15679       /* Perform access checks for template parameters.  */
15680       cp_parser_perform_template_parameter_access_checks (checks);
15681
15682       /* Perform the access control checks for the declarator and the
15683          decl-specifiers.  */
15684       perform_deferred_access_checks ();
15685
15686       /* Restore the saved value.  */
15687       if (TREE_CODE (decl) == FUNCTION_DECL)
15688         current_function_decl = saved_current_function_decl;
15689     }
15690
15691   /* Parse the initializer.  */
15692   initializer = NULL_TREE;
15693   is_direct_init = false;
15694   is_non_constant_init = true;
15695   if (is_initialized)
15696     {
15697       if (function_declarator_p (declarator))
15698         {
15699           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15700            if (initialization_kind == CPP_EQ)
15701              initializer = cp_parser_pure_specifier (parser);
15702            else
15703              {
15704                /* If the declaration was erroneous, we don't really
15705                   know what the user intended, so just silently
15706                   consume the initializer.  */
15707                if (decl != error_mark_node)
15708                  error_at (initializer_start_token->location,
15709                            "initializer provided for function");
15710                cp_parser_skip_to_closing_parenthesis (parser,
15711                                                       /*recovering=*/true,
15712                                                       /*or_comma=*/false,
15713                                                       /*consume_paren=*/true);
15714              }
15715         }
15716       else
15717         {
15718           /* We want to record the extra mangling scope for in-class
15719              initializers of class members and initializers of static data
15720              member templates.  The former is a C++0x feature which isn't
15721              implemented yet, and I expect it will involve deferring
15722              parsing of the initializer until end of class as with default
15723              arguments.  So right here we only handle the latter.  */
15724           if (!member_p && processing_template_decl)
15725             start_lambda_scope (decl);
15726           initializer = cp_parser_initializer (parser,
15727                                                &is_direct_init,
15728                                                &is_non_constant_init);
15729           if (!member_p && processing_template_decl)
15730             finish_lambda_scope ();
15731         }
15732     }
15733
15734   /* The old parser allows attributes to appear after a parenthesized
15735      initializer.  Mark Mitchell proposed removing this functionality
15736      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15737      attributes -- but ignores them.  */
15738   if (cp_parser_allow_gnu_extensions_p (parser)
15739       && initialization_kind == CPP_OPEN_PAREN)
15740     if (cp_parser_attributes_opt (parser))
15741       warning (OPT_Wattributes,
15742                "attributes after parenthesized initializer ignored");
15743
15744   /* For an in-class declaration, use `grokfield' to create the
15745      declaration.  */
15746   if (member_p)
15747     {
15748       if (pushed_scope)
15749         {
15750           pop_scope (pushed_scope);
15751           pushed_scope = NULL_TREE;
15752         }
15753       decl = grokfield (declarator, decl_specifiers,
15754                         initializer, !is_non_constant_init,
15755                         /*asmspec=*/NULL_TREE,
15756                         prefix_attributes);
15757       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15758         cp_parser_save_default_args (parser, decl);
15759     }
15760
15761   /* Finish processing the declaration.  But, skip member
15762      declarations.  */
15763   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15764     {
15765       cp_finish_decl (decl,
15766                       initializer, !is_non_constant_init,
15767                       asm_specification,
15768                       /* If the initializer is in parentheses, then this is
15769                          a direct-initialization, which means that an
15770                          `explicit' constructor is OK.  Otherwise, an
15771                          `explicit' constructor cannot be used.  */
15772                       ((is_direct_init || !is_initialized)
15773                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15774     }
15775   else if ((cxx_dialect != cxx98) && friend_p
15776            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15777     /* Core issue #226 (C++0x only): A default template-argument
15778        shall not be specified in a friend class template
15779        declaration. */
15780     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15781                              /*is_partial=*/0, /*is_friend_decl=*/1);
15782
15783   if (!friend_p && pushed_scope)
15784     pop_scope (pushed_scope);
15785
15786   return decl;
15787 }
15788
15789 /* Parse a declarator.
15790
15791    declarator:
15792      direct-declarator
15793      ptr-operator declarator
15794
15795    abstract-declarator:
15796      ptr-operator abstract-declarator [opt]
15797      direct-abstract-declarator
15798
15799    GNU Extensions:
15800
15801    declarator:
15802      attributes [opt] direct-declarator
15803      attributes [opt] ptr-operator declarator
15804
15805    abstract-declarator:
15806      attributes [opt] ptr-operator abstract-declarator [opt]
15807      attributes [opt] direct-abstract-declarator
15808
15809    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15810    detect constructor, destructor or conversion operators. It is set
15811    to -1 if the declarator is a name, and +1 if it is a
15812    function. Otherwise it is set to zero. Usually you just want to
15813    test for >0, but internally the negative value is used.
15814
15815    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15816    a decl-specifier-seq unless it declares a constructor, destructor,
15817    or conversion.  It might seem that we could check this condition in
15818    semantic analysis, rather than parsing, but that makes it difficult
15819    to handle something like `f()'.  We want to notice that there are
15820    no decl-specifiers, and therefore realize that this is an
15821    expression, not a declaration.)
15822
15823    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15824    the declarator is a direct-declarator of the form "(...)".
15825
15826    MEMBER_P is true iff this declarator is a member-declarator.  */
15827
15828 static cp_declarator *
15829 cp_parser_declarator (cp_parser* parser,
15830                       cp_parser_declarator_kind dcl_kind,
15831                       int* ctor_dtor_or_conv_p,
15832                       bool* parenthesized_p,
15833                       bool member_p)
15834 {
15835   cp_declarator *declarator;
15836   enum tree_code code;
15837   cp_cv_quals cv_quals;
15838   tree class_type;
15839   tree attributes = NULL_TREE;
15840
15841   /* Assume this is not a constructor, destructor, or type-conversion
15842      operator.  */
15843   if (ctor_dtor_or_conv_p)
15844     *ctor_dtor_or_conv_p = 0;
15845
15846   if (cp_parser_allow_gnu_extensions_p (parser))
15847     attributes = cp_parser_attributes_opt (parser);
15848
15849   /* Check for the ptr-operator production.  */
15850   cp_parser_parse_tentatively (parser);
15851   /* Parse the ptr-operator.  */
15852   code = cp_parser_ptr_operator (parser,
15853                                  &class_type,
15854                                  &cv_quals);
15855   /* If that worked, then we have a ptr-operator.  */
15856   if (cp_parser_parse_definitely (parser))
15857     {
15858       /* If a ptr-operator was found, then this declarator was not
15859          parenthesized.  */
15860       if (parenthesized_p)
15861         *parenthesized_p = true;
15862       /* The dependent declarator is optional if we are parsing an
15863          abstract-declarator.  */
15864       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15865         cp_parser_parse_tentatively (parser);
15866
15867       /* Parse the dependent declarator.  */
15868       declarator = cp_parser_declarator (parser, dcl_kind,
15869                                          /*ctor_dtor_or_conv_p=*/NULL,
15870                                          /*parenthesized_p=*/NULL,
15871                                          /*member_p=*/false);
15872
15873       /* If we are parsing an abstract-declarator, we must handle the
15874          case where the dependent declarator is absent.  */
15875       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15876           && !cp_parser_parse_definitely (parser))
15877         declarator = NULL;
15878
15879       declarator = cp_parser_make_indirect_declarator
15880         (code, class_type, cv_quals, declarator);
15881     }
15882   /* Everything else is a direct-declarator.  */
15883   else
15884     {
15885       if (parenthesized_p)
15886         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15887                                                    CPP_OPEN_PAREN);
15888       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15889                                                 ctor_dtor_or_conv_p,
15890                                                 member_p);
15891     }
15892
15893   if (attributes && declarator && declarator != cp_error_declarator)
15894     declarator->attributes = attributes;
15895
15896   return declarator;
15897 }
15898
15899 /* Parse a direct-declarator or direct-abstract-declarator.
15900
15901    direct-declarator:
15902      declarator-id
15903      direct-declarator ( parameter-declaration-clause )
15904        cv-qualifier-seq [opt]
15905        exception-specification [opt]
15906      direct-declarator [ constant-expression [opt] ]
15907      ( declarator )
15908
15909    direct-abstract-declarator:
15910      direct-abstract-declarator [opt]
15911        ( parameter-declaration-clause )
15912        cv-qualifier-seq [opt]
15913        exception-specification [opt]
15914      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15915      ( abstract-declarator )
15916
15917    Returns a representation of the declarator.  DCL_KIND is
15918    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15919    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15920    we are parsing a direct-declarator.  It is
15921    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15922    of ambiguity we prefer an abstract declarator, as per
15923    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15924    cp_parser_declarator.  */
15925
15926 static cp_declarator *
15927 cp_parser_direct_declarator (cp_parser* parser,
15928                              cp_parser_declarator_kind dcl_kind,
15929                              int* ctor_dtor_or_conv_p,
15930                              bool member_p)
15931 {
15932   cp_token *token;
15933   cp_declarator *declarator = NULL;
15934   tree scope = NULL_TREE;
15935   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15936   bool saved_in_declarator_p = parser->in_declarator_p;
15937   bool first = true;
15938   tree pushed_scope = NULL_TREE;
15939
15940   while (true)
15941     {
15942       /* Peek at the next token.  */
15943       token = cp_lexer_peek_token (parser->lexer);
15944       if (token->type == CPP_OPEN_PAREN)
15945         {
15946           /* This is either a parameter-declaration-clause, or a
15947              parenthesized declarator. When we know we are parsing a
15948              named declarator, it must be a parenthesized declarator
15949              if FIRST is true. For instance, `(int)' is a
15950              parameter-declaration-clause, with an omitted
15951              direct-abstract-declarator. But `((*))', is a
15952              parenthesized abstract declarator. Finally, when T is a
15953              template parameter `(T)' is a
15954              parameter-declaration-clause, and not a parenthesized
15955              named declarator.
15956
15957              We first try and parse a parameter-declaration-clause,
15958              and then try a nested declarator (if FIRST is true).
15959
15960              It is not an error for it not to be a
15961              parameter-declaration-clause, even when FIRST is
15962              false. Consider,
15963
15964                int i (int);
15965                int i (3);
15966
15967              The first is the declaration of a function while the
15968              second is the definition of a variable, including its
15969              initializer.
15970
15971              Having seen only the parenthesis, we cannot know which of
15972              these two alternatives should be selected.  Even more
15973              complex are examples like:
15974
15975                int i (int (a));
15976                int i (int (3));
15977
15978              The former is a function-declaration; the latter is a
15979              variable initialization.
15980
15981              Thus again, we try a parameter-declaration-clause, and if
15982              that fails, we back out and return.  */
15983
15984           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15985             {
15986               tree params;
15987               unsigned saved_num_template_parameter_lists;
15988               bool is_declarator = false;
15989               tree t;
15990
15991               /* In a member-declarator, the only valid interpretation
15992                  of a parenthesis is the start of a
15993                  parameter-declaration-clause.  (It is invalid to
15994                  initialize a static data member with a parenthesized
15995                  initializer; only the "=" form of initialization is
15996                  permitted.)  */
15997               if (!member_p)
15998                 cp_parser_parse_tentatively (parser);
15999
16000               /* Consume the `('.  */
16001               cp_lexer_consume_token (parser->lexer);
16002               if (first)
16003                 {
16004                   /* If this is going to be an abstract declarator, we're
16005                      in a declarator and we can't have default args.  */
16006                   parser->default_arg_ok_p = false;
16007                   parser->in_declarator_p = true;
16008                 }
16009
16010               /* Inside the function parameter list, surrounding
16011                  template-parameter-lists do not apply.  */
16012               saved_num_template_parameter_lists
16013                 = parser->num_template_parameter_lists;
16014               parser->num_template_parameter_lists = 0;
16015
16016               begin_scope (sk_function_parms, NULL_TREE);
16017
16018               /* Parse the parameter-declaration-clause.  */
16019               params = cp_parser_parameter_declaration_clause (parser);
16020
16021               parser->num_template_parameter_lists
16022                 = saved_num_template_parameter_lists;
16023
16024               /* Consume the `)'.  */
16025               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
16026
16027               /* If all went well, parse the cv-qualifier-seq and the
16028                  exception-specification.  */
16029               if (member_p || cp_parser_parse_definitely (parser))
16030                 {
16031                   cp_cv_quals cv_quals;
16032                   cp_virt_specifiers virt_specifiers;
16033                   tree exception_specification;
16034                   tree late_return;
16035
16036                   is_declarator = true;
16037
16038                   if (ctor_dtor_or_conv_p)
16039                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16040                   first = false;
16041
16042                   /* Parse the cv-qualifier-seq.  */
16043                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16044                   /* And the exception-specification.  */
16045                   exception_specification
16046                     = cp_parser_exception_specification_opt (parser);
16047                   /* Parse the virt-specifier-seq.  */
16048                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16049
16050                   late_return = (cp_parser_late_return_type_opt
16051                                  (parser, member_p ? cv_quals : -1));
16052
16053                   /* Create the function-declarator.  */
16054                   declarator = make_call_declarator (declarator,
16055                                                      params,
16056                                                      cv_quals,
16057                                                      virt_specifiers,
16058                                                      exception_specification,
16059                                                      late_return);
16060                   /* Any subsequent parameter lists are to do with
16061                      return type, so are not those of the declared
16062                      function.  */
16063                   parser->default_arg_ok_p = false;
16064                 }
16065
16066               /* Remove the function parms from scope.  */
16067               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16068                 pop_binding (DECL_NAME (t), t);
16069               leave_scope();
16070
16071               if (is_declarator)
16072                 /* Repeat the main loop.  */
16073                 continue;
16074             }
16075
16076           /* If this is the first, we can try a parenthesized
16077              declarator.  */
16078           if (first)
16079             {
16080               bool saved_in_type_id_in_expr_p;
16081
16082               parser->default_arg_ok_p = saved_default_arg_ok_p;
16083               parser->in_declarator_p = saved_in_declarator_p;
16084
16085               /* Consume the `('.  */
16086               cp_lexer_consume_token (parser->lexer);
16087               /* Parse the nested declarator.  */
16088               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16089               parser->in_type_id_in_expr_p = true;
16090               declarator
16091                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16092                                         /*parenthesized_p=*/NULL,
16093                                         member_p);
16094               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16095               first = false;
16096               /* Expect a `)'.  */
16097               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16098                 declarator = cp_error_declarator;
16099               if (declarator == cp_error_declarator)
16100                 break;
16101
16102               goto handle_declarator;
16103             }
16104           /* Otherwise, we must be done.  */
16105           else
16106             break;
16107         }
16108       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16109                && token->type == CPP_OPEN_SQUARE)
16110         {
16111           /* Parse an array-declarator.  */
16112           tree bounds;
16113
16114           if (ctor_dtor_or_conv_p)
16115             *ctor_dtor_or_conv_p = 0;
16116
16117           first = false;
16118           parser->default_arg_ok_p = false;
16119           parser->in_declarator_p = true;
16120           /* Consume the `['.  */
16121           cp_lexer_consume_token (parser->lexer);
16122           /* Peek at the next token.  */
16123           token = cp_lexer_peek_token (parser->lexer);
16124           /* If the next token is `]', then there is no
16125              constant-expression.  */
16126           if (token->type != CPP_CLOSE_SQUARE)
16127             {
16128               bool non_constant_p;
16129
16130               bounds
16131                 = cp_parser_constant_expression (parser,
16132                                                  /*allow_non_constant=*/true,
16133                                                  &non_constant_p);
16134               if (!non_constant_p)
16135                 /* OK */;
16136               else if (error_operand_p (bounds))
16137                 /* Already gave an error.  */;
16138               else if (!parser->in_function_body
16139                        || current_binding_level->kind == sk_function_parms)
16140                 {
16141                   /* Normally, the array bound must be an integral constant
16142                      expression.  However, as an extension, we allow VLAs
16143                      in function scopes as long as they aren't part of a
16144                      parameter declaration.  */
16145                   cp_parser_error (parser,
16146                                    "array bound is not an integer constant");
16147                   bounds = error_mark_node;
16148                 }
16149               else if (processing_template_decl)
16150                 {
16151                   /* Remember this wasn't a constant-expression.  */
16152                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16153                   TREE_SIDE_EFFECTS (bounds) = 1;
16154                 }
16155             }
16156           else
16157             bounds = NULL_TREE;
16158           /* Look for the closing `]'.  */
16159           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16160             {
16161               declarator = cp_error_declarator;
16162               break;
16163             }
16164
16165           declarator = make_array_declarator (declarator, bounds);
16166         }
16167       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16168         {
16169           {
16170             tree qualifying_scope;
16171             tree unqualified_name;
16172             special_function_kind sfk;
16173             bool abstract_ok;
16174             bool pack_expansion_p = false;
16175             cp_token *declarator_id_start_token;
16176
16177             /* Parse a declarator-id */
16178             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16179             if (abstract_ok)
16180               {
16181                 cp_parser_parse_tentatively (parser);
16182
16183                 /* If we see an ellipsis, we should be looking at a
16184                    parameter pack. */
16185                 if (token->type == CPP_ELLIPSIS)
16186                   {
16187                     /* Consume the `...' */
16188                     cp_lexer_consume_token (parser->lexer);
16189
16190                     pack_expansion_p = true;
16191                   }
16192               }
16193
16194             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16195             unqualified_name
16196               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16197             qualifying_scope = parser->scope;
16198             if (abstract_ok)
16199               {
16200                 bool okay = false;
16201
16202                 if (!unqualified_name && pack_expansion_p)
16203                   {
16204                     /* Check whether an error occurred. */
16205                     okay = !cp_parser_error_occurred (parser);
16206
16207                     /* We already consumed the ellipsis to mark a
16208                        parameter pack, but we have no way to report it,
16209                        so abort the tentative parse. We will be exiting
16210                        immediately anyway. */
16211                     cp_parser_abort_tentative_parse (parser);
16212                   }
16213                 else
16214                   okay = cp_parser_parse_definitely (parser);
16215
16216                 if (!okay)
16217                   unqualified_name = error_mark_node;
16218                 else if (unqualified_name
16219                          && (qualifying_scope
16220                              || (TREE_CODE (unqualified_name)
16221                                  != IDENTIFIER_NODE)))
16222                   {
16223                     cp_parser_error (parser, "expected unqualified-id");
16224                     unqualified_name = error_mark_node;
16225                   }
16226               }
16227
16228             if (!unqualified_name)
16229               return NULL;
16230             if (unqualified_name == error_mark_node)
16231               {
16232                 declarator = cp_error_declarator;
16233                 pack_expansion_p = false;
16234                 declarator->parameter_pack_p = false;
16235                 break;
16236               }
16237
16238             if (qualifying_scope && at_namespace_scope_p ()
16239                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16240               {
16241                 /* In the declaration of a member of a template class
16242                    outside of the class itself, the SCOPE will sometimes
16243                    be a TYPENAME_TYPE.  For example, given:
16244
16245                    template <typename T>
16246                    int S<T>::R::i = 3;
16247
16248                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16249                    this context, we must resolve S<T>::R to an ordinary
16250                    type, rather than a typename type.
16251
16252                    The reason we normally avoid resolving TYPENAME_TYPEs
16253                    is that a specialization of `S' might render
16254                    `S<T>::R' not a type.  However, if `S' is
16255                    specialized, then this `i' will not be used, so there
16256                    is no harm in resolving the types here.  */
16257                 tree type;
16258
16259                 /* Resolve the TYPENAME_TYPE.  */
16260                 type = resolve_typename_type (qualifying_scope,
16261                                               /*only_current_p=*/false);
16262                 /* If that failed, the declarator is invalid.  */
16263                 if (TREE_CODE (type) == TYPENAME_TYPE)
16264                   {
16265                     if (typedef_variant_p (type))
16266                       error_at (declarator_id_start_token->location,
16267                                 "cannot define member of dependent typedef "
16268                                 "%qT", type);
16269                     else
16270                       error_at (declarator_id_start_token->location,
16271                                 "%<%T::%E%> is not a type",
16272                                 TYPE_CONTEXT (qualifying_scope),
16273                                 TYPE_IDENTIFIER (qualifying_scope));
16274                   }
16275                 qualifying_scope = type;
16276               }
16277
16278             sfk = sfk_none;
16279
16280             if (unqualified_name)
16281               {
16282                 tree class_type;
16283
16284                 if (qualifying_scope
16285                     && CLASS_TYPE_P (qualifying_scope))
16286                   class_type = qualifying_scope;
16287                 else
16288                   class_type = current_class_type;
16289
16290                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16291                   {
16292                     tree name_type = TREE_TYPE (unqualified_name);
16293                     if (class_type && same_type_p (name_type, class_type))
16294                       {
16295                         if (qualifying_scope
16296                             && CLASSTYPE_USE_TEMPLATE (name_type))
16297                           {
16298                             error_at (declarator_id_start_token->location,
16299                                       "invalid use of constructor as a template");
16300                             inform (declarator_id_start_token->location,
16301                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16302                                     "name the constructor in a qualified name",
16303                                     class_type,
16304                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16305                                     class_type, name_type);
16306                             declarator = cp_error_declarator;
16307                             break;
16308                           }
16309                         else
16310                           unqualified_name = constructor_name (class_type);
16311                       }
16312                     else
16313                       {
16314                         /* We do not attempt to print the declarator
16315                            here because we do not have enough
16316                            information about its original syntactic
16317                            form.  */
16318                         cp_parser_error (parser, "invalid declarator");
16319                         declarator = cp_error_declarator;
16320                         break;
16321                       }
16322                   }
16323
16324                 if (class_type)
16325                   {
16326                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16327                       sfk = sfk_destructor;
16328                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16329                       sfk = sfk_conversion;
16330                     else if (/* There's no way to declare a constructor
16331                                 for an anonymous type, even if the type
16332                                 got a name for linkage purposes.  */
16333                              !TYPE_WAS_ANONYMOUS (class_type)
16334                              && constructor_name_p (unqualified_name,
16335                                                     class_type))
16336                       {
16337                         unqualified_name = constructor_name (class_type);
16338                         sfk = sfk_constructor;
16339                       }
16340                     else if (is_overloaded_fn (unqualified_name)
16341                              && DECL_CONSTRUCTOR_P (get_first_fn
16342                                                     (unqualified_name)))
16343                       sfk = sfk_constructor;
16344
16345                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16346                       *ctor_dtor_or_conv_p = -1;
16347                   }
16348               }
16349             declarator = make_id_declarator (qualifying_scope,
16350                                              unqualified_name,
16351                                              sfk);
16352             declarator->id_loc = token->location;
16353             declarator->parameter_pack_p = pack_expansion_p;
16354
16355             if (pack_expansion_p)
16356               maybe_warn_variadic_templates ();
16357           }
16358
16359         handle_declarator:;
16360           scope = get_scope_of_declarator (declarator);
16361           if (scope)
16362             /* Any names that appear after the declarator-id for a
16363                member are looked up in the containing scope.  */
16364             pushed_scope = push_scope (scope);
16365           parser->in_declarator_p = true;
16366           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16367               || (declarator && declarator->kind == cdk_id))
16368             /* Default args are only allowed on function
16369                declarations.  */
16370             parser->default_arg_ok_p = saved_default_arg_ok_p;
16371           else
16372             parser->default_arg_ok_p = false;
16373
16374           first = false;
16375         }
16376       /* We're done.  */
16377       else
16378         break;
16379     }
16380
16381   /* For an abstract declarator, we might wind up with nothing at this
16382      point.  That's an error; the declarator is not optional.  */
16383   if (!declarator)
16384     cp_parser_error (parser, "expected declarator");
16385
16386   /* If we entered a scope, we must exit it now.  */
16387   if (pushed_scope)
16388     pop_scope (pushed_scope);
16389
16390   parser->default_arg_ok_p = saved_default_arg_ok_p;
16391   parser->in_declarator_p = saved_in_declarator_p;
16392
16393   return declarator;
16394 }
16395
16396 /* Parse a ptr-operator.
16397
16398    ptr-operator:
16399      * cv-qualifier-seq [opt]
16400      &
16401      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16402
16403    GNU Extension:
16404
16405    ptr-operator:
16406      & cv-qualifier-seq [opt]
16407
16408    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16409    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16410    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16411    filled in with the TYPE containing the member.  *CV_QUALS is
16412    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16413    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16414    Note that the tree codes returned by this function have nothing
16415    to do with the types of trees that will be eventually be created
16416    to represent the pointer or reference type being parsed. They are
16417    just constants with suggestive names. */
16418 static enum tree_code
16419 cp_parser_ptr_operator (cp_parser* parser,
16420                         tree* type,
16421                         cp_cv_quals *cv_quals)
16422 {
16423   enum tree_code code = ERROR_MARK;
16424   cp_token *token;
16425
16426   /* Assume that it's not a pointer-to-member.  */
16427   *type = NULL_TREE;
16428   /* And that there are no cv-qualifiers.  */
16429   *cv_quals = TYPE_UNQUALIFIED;
16430
16431   /* Peek at the next token.  */
16432   token = cp_lexer_peek_token (parser->lexer);
16433
16434   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16435   if (token->type == CPP_MULT)
16436     code = INDIRECT_REF;
16437   else if (token->type == CPP_AND)
16438     code = ADDR_EXPR;
16439   else if ((cxx_dialect != cxx98) &&
16440            token->type == CPP_AND_AND) /* C++0x only */
16441     code = NON_LVALUE_EXPR;
16442
16443   if (code != ERROR_MARK)
16444     {
16445       /* Consume the `*', `&' or `&&'.  */
16446       cp_lexer_consume_token (parser->lexer);
16447
16448       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16449          `&', if we are allowing GNU extensions.  (The only qualifier
16450          that can legally appear after `&' is `restrict', but that is
16451          enforced during semantic analysis.  */
16452       if (code == INDIRECT_REF
16453           || cp_parser_allow_gnu_extensions_p (parser))
16454         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16455     }
16456   else
16457     {
16458       /* Try the pointer-to-member case.  */
16459       cp_parser_parse_tentatively (parser);
16460       /* Look for the optional `::' operator.  */
16461       cp_parser_global_scope_opt (parser,
16462                                   /*current_scope_valid_p=*/false);
16463       /* Look for the nested-name specifier.  */
16464       token = cp_lexer_peek_token (parser->lexer);
16465       cp_parser_nested_name_specifier (parser,
16466                                        /*typename_keyword_p=*/false,
16467                                        /*check_dependency_p=*/true,
16468                                        /*type_p=*/false,
16469                                        /*is_declaration=*/false);
16470       /* If we found it, and the next token is a `*', then we are
16471          indeed looking at a pointer-to-member operator.  */
16472       if (!cp_parser_error_occurred (parser)
16473           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16474         {
16475           /* Indicate that the `*' operator was used.  */
16476           code = INDIRECT_REF;
16477
16478           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16479             error_at (token->location, "%qD is a namespace", parser->scope);
16480           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16481             error_at (token->location, "cannot form pointer to member of "
16482                       "non-class %q#T", parser->scope);
16483           else
16484             {
16485               /* The type of which the member is a member is given by the
16486                  current SCOPE.  */
16487               *type = parser->scope;
16488               /* The next name will not be qualified.  */
16489               parser->scope = NULL_TREE;
16490               parser->qualifying_scope = NULL_TREE;
16491               parser->object_scope = NULL_TREE;
16492               /* Look for the optional cv-qualifier-seq.  */
16493               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16494             }
16495         }
16496       /* If that didn't work we don't have a ptr-operator.  */
16497       if (!cp_parser_parse_definitely (parser))
16498         cp_parser_error (parser, "expected ptr-operator");
16499     }
16500
16501   return code;
16502 }
16503
16504 /* Parse an (optional) cv-qualifier-seq.
16505
16506    cv-qualifier-seq:
16507      cv-qualifier cv-qualifier-seq [opt]
16508
16509    cv-qualifier:
16510      const
16511      volatile
16512
16513    GNU Extension:
16514
16515    cv-qualifier:
16516      __restrict__
16517
16518    Returns a bitmask representing the cv-qualifiers.  */
16519
16520 static cp_cv_quals
16521 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16522 {
16523   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16524
16525   while (true)
16526     {
16527       cp_token *token;
16528       cp_cv_quals cv_qualifier;
16529
16530       /* Peek at the next token.  */
16531       token = cp_lexer_peek_token (parser->lexer);
16532       /* See if it's a cv-qualifier.  */
16533       switch (token->keyword)
16534         {
16535         case RID_CONST:
16536           cv_qualifier = TYPE_QUAL_CONST;
16537           break;
16538
16539         case RID_VOLATILE:
16540           cv_qualifier = TYPE_QUAL_VOLATILE;
16541           break;
16542
16543         case RID_RESTRICT:
16544           cv_qualifier = TYPE_QUAL_RESTRICT;
16545           break;
16546
16547         default:
16548           cv_qualifier = TYPE_UNQUALIFIED;
16549           break;
16550         }
16551
16552       if (!cv_qualifier)
16553         break;
16554
16555       if (cv_quals & cv_qualifier)
16556         {
16557           error_at (token->location, "duplicate cv-qualifier");
16558           cp_lexer_purge_token (parser->lexer);
16559         }
16560       else
16561         {
16562           cp_lexer_consume_token (parser->lexer);
16563           cv_quals |= cv_qualifier;
16564         }
16565     }
16566
16567   return cv_quals;
16568 }
16569
16570 /* Parse an (optional) virt-specifier-seq.
16571
16572    virt-specifier-seq:
16573      virt-specifier virt-specifier-seq [opt]
16574
16575    virt-specifier:
16576      override
16577      final
16578
16579    Returns a bitmask representing the virt-specifiers.  */
16580
16581 static cp_virt_specifiers
16582 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16583 {
16584   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16585
16586   while (true)
16587     {
16588       cp_token *token;
16589       cp_virt_specifiers virt_specifier;
16590
16591       /* Peek at the next token.  */
16592       token = cp_lexer_peek_token (parser->lexer);
16593       /* See if it's a virt-specifier-qualifier.  */
16594       if (token->type != CPP_NAME)
16595         break;
16596       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16597         {
16598           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16599           virt_specifier = VIRT_SPEC_OVERRIDE;
16600         }
16601       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16602         {
16603           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16604           virt_specifier = VIRT_SPEC_FINAL;
16605         }
16606       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16607         {
16608           virt_specifier = VIRT_SPEC_FINAL;
16609         }
16610       else
16611         break;
16612
16613       if (virt_specifiers & virt_specifier)
16614         {
16615           error_at (token->location, "duplicate virt-specifier");
16616           cp_lexer_purge_token (parser->lexer);
16617         }
16618       else
16619         {
16620           cp_lexer_consume_token (parser->lexer);
16621           virt_specifiers |= virt_specifier;
16622         }
16623     }
16624   return virt_specifiers;
16625 }
16626
16627 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16628    is in scope even though it isn't real.  */
16629
16630 static void
16631 inject_this_parameter (tree ctype, cp_cv_quals quals)
16632 {
16633   tree this_parm;
16634
16635   if (current_class_ptr)
16636     {
16637       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16638       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16639       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16640           && cp_type_quals (type) == quals)
16641         return;
16642     }
16643
16644   this_parm = build_this_parm (ctype, quals);
16645   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16646   current_class_ptr = NULL_TREE;
16647   current_class_ref
16648     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16649   current_class_ptr = this_parm;
16650 }
16651
16652 /* Parse a late-specified return type, if any.  This is not a separate
16653    non-terminal, but part of a function declarator, which looks like
16654
16655    -> trailing-type-specifier-seq abstract-declarator(opt)
16656
16657    Returns the type indicated by the type-id.
16658
16659    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16660    function.  */
16661
16662 static tree
16663 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16664 {
16665   cp_token *token;
16666   tree type;
16667
16668   /* Peek at the next token.  */
16669   token = cp_lexer_peek_token (parser->lexer);
16670   /* A late-specified return type is indicated by an initial '->'. */
16671   if (token->type != CPP_DEREF)
16672     return NULL_TREE;
16673
16674   /* Consume the ->.  */
16675   cp_lexer_consume_token (parser->lexer);
16676
16677   if (quals >= 0)
16678     {
16679       /* DR 1207: 'this' is in scope in the trailing return type.  */
16680       gcc_assert (current_class_ptr == NULL_TREE);
16681       inject_this_parameter (current_class_type, quals);
16682     }
16683
16684   type = cp_parser_trailing_type_id (parser);
16685
16686   if (quals >= 0)
16687     current_class_ptr = current_class_ref = NULL_TREE;
16688
16689   return type;
16690 }
16691
16692 /* Parse a declarator-id.
16693
16694    declarator-id:
16695      id-expression
16696      :: [opt] nested-name-specifier [opt] type-name
16697
16698    In the `id-expression' case, the value returned is as for
16699    cp_parser_id_expression if the id-expression was an unqualified-id.
16700    If the id-expression was a qualified-id, then a SCOPE_REF is
16701    returned.  The first operand is the scope (either a NAMESPACE_DECL
16702    or TREE_TYPE), but the second is still just a representation of an
16703    unqualified-id.  */
16704
16705 static tree
16706 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16707 {
16708   tree id;
16709   /* The expression must be an id-expression.  Assume that qualified
16710      names are the names of types so that:
16711
16712        template <class T>
16713        int S<T>::R::i = 3;
16714
16715      will work; we must treat `S<T>::R' as the name of a type.
16716      Similarly, assume that qualified names are templates, where
16717      required, so that:
16718
16719        template <class T>
16720        int S<T>::R<T>::i = 3;
16721
16722      will work, too.  */
16723   id = cp_parser_id_expression (parser,
16724                                 /*template_keyword_p=*/false,
16725                                 /*check_dependency_p=*/false,
16726                                 /*template_p=*/NULL,
16727                                 /*declarator_p=*/true,
16728                                 optional_p);
16729   if (id && BASELINK_P (id))
16730     id = BASELINK_FUNCTIONS (id);
16731   return id;
16732 }
16733
16734 /* Parse a type-id.
16735
16736    type-id:
16737      type-specifier-seq abstract-declarator [opt]
16738
16739    Returns the TYPE specified.  */
16740
16741 static tree
16742 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16743                      bool is_trailing_return)
16744 {
16745   cp_decl_specifier_seq type_specifier_seq;
16746   cp_declarator *abstract_declarator;
16747
16748   /* Parse the type-specifier-seq.  */
16749   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16750                                 is_trailing_return,
16751                                 &type_specifier_seq);
16752   if (type_specifier_seq.type == error_mark_node)
16753     return error_mark_node;
16754
16755   /* There might or might not be an abstract declarator.  */
16756   cp_parser_parse_tentatively (parser);
16757   /* Look for the declarator.  */
16758   abstract_declarator
16759     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16760                             /*parenthesized_p=*/NULL,
16761                             /*member_p=*/false);
16762   /* Check to see if there really was a declarator.  */
16763   if (!cp_parser_parse_definitely (parser))
16764     abstract_declarator = NULL;
16765
16766   if (type_specifier_seq.type
16767       && type_uses_auto (type_specifier_seq.type))
16768     {
16769       /* A type-id with type 'auto' is only ok if the abstract declarator
16770          is a function declarator with a late-specified return type.  */
16771       if (abstract_declarator
16772           && abstract_declarator->kind == cdk_function
16773           && abstract_declarator->u.function.late_return_type)
16774         /* OK */;
16775       else
16776         {
16777           error ("invalid use of %<auto%>");
16778           return error_mark_node;
16779         }
16780     }
16781   
16782   return groktypename (&type_specifier_seq, abstract_declarator,
16783                        is_template_arg);
16784 }
16785
16786 static tree cp_parser_type_id (cp_parser *parser)
16787 {
16788   return cp_parser_type_id_1 (parser, false, false);
16789 }
16790
16791 static tree cp_parser_template_type_arg (cp_parser *parser)
16792 {
16793   tree r;
16794   const char *saved_message = parser->type_definition_forbidden_message;
16795   parser->type_definition_forbidden_message
16796     = G_("types may not be defined in template arguments");
16797   r = cp_parser_type_id_1 (parser, true, false);
16798   parser->type_definition_forbidden_message = saved_message;
16799   return r;
16800 }
16801
16802 static tree cp_parser_trailing_type_id (cp_parser *parser)
16803 {
16804   return cp_parser_type_id_1 (parser, false, true);
16805 }
16806
16807 /* Parse a type-specifier-seq.
16808
16809    type-specifier-seq:
16810      type-specifier type-specifier-seq [opt]
16811
16812    GNU extension:
16813
16814    type-specifier-seq:
16815      attributes type-specifier-seq [opt]
16816
16817    If IS_DECLARATION is true, we are at the start of a "condition" or
16818    exception-declaration, so we might be followed by a declarator-id.
16819
16820    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16821    i.e. we've just seen "->".
16822
16823    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16824
16825 static void
16826 cp_parser_type_specifier_seq (cp_parser* parser,
16827                               bool is_declaration,
16828                               bool is_trailing_return,
16829                               cp_decl_specifier_seq *type_specifier_seq)
16830 {
16831   bool seen_type_specifier = false;
16832   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16833   cp_token *start_token = NULL;
16834
16835   /* Clear the TYPE_SPECIFIER_SEQ.  */
16836   clear_decl_specs (type_specifier_seq);
16837
16838   /* In the context of a trailing return type, enum E { } is an
16839      elaborated-type-specifier followed by a function-body, not an
16840      enum-specifier.  */
16841   if (is_trailing_return)
16842     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16843
16844   /* Parse the type-specifiers and attributes.  */
16845   while (true)
16846     {
16847       tree type_specifier;
16848       bool is_cv_qualifier;
16849
16850       /* Check for attributes first.  */
16851       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16852         {
16853           type_specifier_seq->attributes =
16854             chainon (type_specifier_seq->attributes,
16855                      cp_parser_attributes_opt (parser));
16856           continue;
16857         }
16858
16859       /* record the token of the beginning of the type specifier seq,
16860          for error reporting purposes*/
16861      if (!start_token)
16862        start_token = cp_lexer_peek_token (parser->lexer);
16863
16864       /* Look for the type-specifier.  */
16865       type_specifier = cp_parser_type_specifier (parser,
16866                                                  flags,
16867                                                  type_specifier_seq,
16868                                                  /*is_declaration=*/false,
16869                                                  NULL,
16870                                                  &is_cv_qualifier);
16871       if (!type_specifier)
16872         {
16873           /* If the first type-specifier could not be found, this is not a
16874              type-specifier-seq at all.  */
16875           if (!seen_type_specifier)
16876             {
16877               cp_parser_error (parser, "expected type-specifier");
16878               type_specifier_seq->type = error_mark_node;
16879               return;
16880             }
16881           /* If subsequent type-specifiers could not be found, the
16882              type-specifier-seq is complete.  */
16883           break;
16884         }
16885
16886       seen_type_specifier = true;
16887       /* The standard says that a condition can be:
16888
16889             type-specifier-seq declarator = assignment-expression
16890
16891          However, given:
16892
16893            struct S {};
16894            if (int S = ...)
16895
16896          we should treat the "S" as a declarator, not as a
16897          type-specifier.  The standard doesn't say that explicitly for
16898          type-specifier-seq, but it does say that for
16899          decl-specifier-seq in an ordinary declaration.  Perhaps it
16900          would be clearer just to allow a decl-specifier-seq here, and
16901          then add a semantic restriction that if any decl-specifiers
16902          that are not type-specifiers appear, the program is invalid.  */
16903       if (is_declaration && !is_cv_qualifier)
16904         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16905     }
16906
16907   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16908 }
16909
16910 /* Parse a parameter-declaration-clause.
16911
16912    parameter-declaration-clause:
16913      parameter-declaration-list [opt] ... [opt]
16914      parameter-declaration-list , ...
16915
16916    Returns a representation for the parameter declarations.  A return
16917    value of NULL indicates a parameter-declaration-clause consisting
16918    only of an ellipsis.  */
16919
16920 static tree
16921 cp_parser_parameter_declaration_clause (cp_parser* parser)
16922 {
16923   tree parameters;
16924   cp_token *token;
16925   bool ellipsis_p;
16926   bool is_error;
16927
16928   /* Peek at the next token.  */
16929   token = cp_lexer_peek_token (parser->lexer);
16930   /* Check for trivial parameter-declaration-clauses.  */
16931   if (token->type == CPP_ELLIPSIS)
16932     {
16933       /* Consume the `...' token.  */
16934       cp_lexer_consume_token (parser->lexer);
16935       return NULL_TREE;
16936     }
16937   else if (token->type == CPP_CLOSE_PAREN)
16938     /* There are no parameters.  */
16939     {
16940 #ifndef NO_IMPLICIT_EXTERN_C
16941       if (in_system_header && current_class_type == NULL
16942           && current_lang_name == lang_name_c)
16943         return NULL_TREE;
16944       else
16945 #endif
16946         return void_list_node;
16947     }
16948   /* Check for `(void)', too, which is a special case.  */
16949   else if (token->keyword == RID_VOID
16950            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16951                == CPP_CLOSE_PAREN))
16952     {
16953       /* Consume the `void' token.  */
16954       cp_lexer_consume_token (parser->lexer);
16955       /* There are no parameters.  */
16956       return void_list_node;
16957     }
16958
16959   /* Parse the parameter-declaration-list.  */
16960   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16961   /* If a parse error occurred while parsing the
16962      parameter-declaration-list, then the entire
16963      parameter-declaration-clause is erroneous.  */
16964   if (is_error)
16965     return NULL;
16966
16967   /* Peek at the next token.  */
16968   token = cp_lexer_peek_token (parser->lexer);
16969   /* If it's a `,', the clause should terminate with an ellipsis.  */
16970   if (token->type == CPP_COMMA)
16971     {
16972       /* Consume the `,'.  */
16973       cp_lexer_consume_token (parser->lexer);
16974       /* Expect an ellipsis.  */
16975       ellipsis_p
16976         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16977     }
16978   /* It might also be `...' if the optional trailing `,' was
16979      omitted.  */
16980   else if (token->type == CPP_ELLIPSIS)
16981     {
16982       /* Consume the `...' token.  */
16983       cp_lexer_consume_token (parser->lexer);
16984       /* And remember that we saw it.  */
16985       ellipsis_p = true;
16986     }
16987   else
16988     ellipsis_p = false;
16989
16990   /* Finish the parameter list.  */
16991   if (!ellipsis_p)
16992     parameters = chainon (parameters, void_list_node);
16993
16994   return parameters;
16995 }
16996
16997 /* Parse a parameter-declaration-list.
16998
16999    parameter-declaration-list:
17000      parameter-declaration
17001      parameter-declaration-list , parameter-declaration
17002
17003    Returns a representation of the parameter-declaration-list, as for
17004    cp_parser_parameter_declaration_clause.  However, the
17005    `void_list_node' is never appended to the list.  Upon return,
17006    *IS_ERROR will be true iff an error occurred.  */
17007
17008 static tree
17009 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
17010 {
17011   tree parameters = NULL_TREE;
17012   tree *tail = &parameters; 
17013   bool saved_in_unbraced_linkage_specification_p;
17014   int index = 0;
17015
17016   /* Assume all will go well.  */
17017   *is_error = false;
17018   /* The special considerations that apply to a function within an
17019      unbraced linkage specifications do not apply to the parameters
17020      to the function.  */
17021   saved_in_unbraced_linkage_specification_p 
17022     = parser->in_unbraced_linkage_specification_p;
17023   parser->in_unbraced_linkage_specification_p = false;
17024
17025   /* Look for more parameters.  */
17026   while (true)
17027     {
17028       cp_parameter_declarator *parameter;
17029       tree decl = error_mark_node;
17030       bool parenthesized_p = false;
17031       /* Parse the parameter.  */
17032       parameter
17033         = cp_parser_parameter_declaration (parser,
17034                                            /*template_parm_p=*/false,
17035                                            &parenthesized_p);
17036
17037       /* We don't know yet if the enclosing context is deprecated, so wait
17038          and warn in grokparms if appropriate.  */
17039       deprecated_state = DEPRECATED_SUPPRESS;
17040
17041       if (parameter)
17042         decl = grokdeclarator (parameter->declarator,
17043                                &parameter->decl_specifiers,
17044                                PARM,
17045                                parameter->default_argument != NULL_TREE,
17046                                &parameter->decl_specifiers.attributes);
17047
17048       deprecated_state = DEPRECATED_NORMAL;
17049
17050       /* If a parse error occurred parsing the parameter declaration,
17051          then the entire parameter-declaration-list is erroneous.  */
17052       if (decl == error_mark_node)
17053         {
17054           *is_error = true;
17055           parameters = error_mark_node;
17056           break;
17057         }
17058
17059       if (parameter->decl_specifiers.attributes)
17060         cplus_decl_attributes (&decl,
17061                                parameter->decl_specifiers.attributes,
17062                                0);
17063       if (DECL_NAME (decl))
17064         decl = pushdecl (decl);
17065
17066       if (decl != error_mark_node)
17067         {
17068           retrofit_lang_decl (decl);
17069           DECL_PARM_INDEX (decl) = ++index;
17070           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17071         }
17072
17073       /* Add the new parameter to the list.  */
17074       *tail = build_tree_list (parameter->default_argument, decl);
17075       tail = &TREE_CHAIN (*tail);
17076
17077       /* Peek at the next token.  */
17078       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17079           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17080           /* These are for Objective-C++ */
17081           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17082           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17083         /* The parameter-declaration-list is complete.  */
17084         break;
17085       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17086         {
17087           cp_token *token;
17088
17089           /* Peek at the next token.  */
17090           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17091           /* If it's an ellipsis, then the list is complete.  */
17092           if (token->type == CPP_ELLIPSIS)
17093             break;
17094           /* Otherwise, there must be more parameters.  Consume the
17095              `,'.  */
17096           cp_lexer_consume_token (parser->lexer);
17097           /* When parsing something like:
17098
17099                 int i(float f, double d)
17100
17101              we can tell after seeing the declaration for "f" that we
17102              are not looking at an initialization of a variable "i",
17103              but rather at the declaration of a function "i".
17104
17105              Due to the fact that the parsing of template arguments
17106              (as specified to a template-id) requires backtracking we
17107              cannot use this technique when inside a template argument
17108              list.  */
17109           if (!parser->in_template_argument_list_p
17110               && !parser->in_type_id_in_expr_p
17111               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17112               /* However, a parameter-declaration of the form
17113                  "foat(f)" (which is a valid declaration of a
17114                  parameter "f") can also be interpreted as an
17115                  expression (the conversion of "f" to "float").  */
17116               && !parenthesized_p)
17117             cp_parser_commit_to_tentative_parse (parser);
17118         }
17119       else
17120         {
17121           cp_parser_error (parser, "expected %<,%> or %<...%>");
17122           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17123             cp_parser_skip_to_closing_parenthesis (parser,
17124                                                    /*recovering=*/true,
17125                                                    /*or_comma=*/false,
17126                                                    /*consume_paren=*/false);
17127           break;
17128         }
17129     }
17130
17131   parser->in_unbraced_linkage_specification_p
17132     = saved_in_unbraced_linkage_specification_p;
17133
17134   return parameters;
17135 }
17136
17137 /* Parse a parameter declaration.
17138
17139    parameter-declaration:
17140      decl-specifier-seq ... [opt] declarator
17141      decl-specifier-seq declarator = assignment-expression
17142      decl-specifier-seq ... [opt] abstract-declarator [opt]
17143      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17144
17145    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17146    declares a template parameter.  (In that case, a non-nested `>'
17147    token encountered during the parsing of the assignment-expression
17148    is not interpreted as a greater-than operator.)
17149
17150    Returns a representation of the parameter, or NULL if an error
17151    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17152    true iff the declarator is of the form "(p)".  */
17153
17154 static cp_parameter_declarator *
17155 cp_parser_parameter_declaration (cp_parser *parser,
17156                                  bool template_parm_p,
17157                                  bool *parenthesized_p)
17158 {
17159   int declares_class_or_enum;
17160   cp_decl_specifier_seq decl_specifiers;
17161   cp_declarator *declarator;
17162   tree default_argument;
17163   cp_token *token = NULL, *declarator_token_start = NULL;
17164   const char *saved_message;
17165
17166   /* In a template parameter, `>' is not an operator.
17167
17168      [temp.param]
17169
17170      When parsing a default template-argument for a non-type
17171      template-parameter, the first non-nested `>' is taken as the end
17172      of the template parameter-list rather than a greater-than
17173      operator.  */
17174
17175   /* Type definitions may not appear in parameter types.  */
17176   saved_message = parser->type_definition_forbidden_message;
17177   parser->type_definition_forbidden_message
17178     = G_("types may not be defined in parameter types");
17179
17180   /* Parse the declaration-specifiers.  */
17181   cp_parser_decl_specifier_seq (parser,
17182                                 CP_PARSER_FLAGS_NONE,
17183                                 &decl_specifiers,
17184                                 &declares_class_or_enum);
17185
17186   /* Complain about missing 'typename' or other invalid type names.  */
17187   if (!decl_specifiers.any_type_specifiers_p)
17188     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17189
17190   /* If an error occurred, there's no reason to attempt to parse the
17191      rest of the declaration.  */
17192   if (cp_parser_error_occurred (parser))
17193     {
17194       parser->type_definition_forbidden_message = saved_message;
17195       return NULL;
17196     }
17197
17198   /* Peek at the next token.  */
17199   token = cp_lexer_peek_token (parser->lexer);
17200
17201   /* If the next token is a `)', `,', `=', `>', or `...', then there
17202      is no declarator. However, when variadic templates are enabled,
17203      there may be a declarator following `...'.  */
17204   if (token->type == CPP_CLOSE_PAREN
17205       || token->type == CPP_COMMA
17206       || token->type == CPP_EQ
17207       || token->type == CPP_GREATER)
17208     {
17209       declarator = NULL;
17210       if (parenthesized_p)
17211         *parenthesized_p = false;
17212     }
17213   /* Otherwise, there should be a declarator.  */
17214   else
17215     {
17216       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17217       parser->default_arg_ok_p = false;
17218
17219       /* After seeing a decl-specifier-seq, if the next token is not a
17220          "(", there is no possibility that the code is a valid
17221          expression.  Therefore, if parsing tentatively, we commit at
17222          this point.  */
17223       if (!parser->in_template_argument_list_p
17224           /* In an expression context, having seen:
17225
17226                (int((char ...
17227
17228              we cannot be sure whether we are looking at a
17229              function-type (taking a "char" as a parameter) or a cast
17230              of some object of type "char" to "int".  */
17231           && !parser->in_type_id_in_expr_p
17232           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17233           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17234           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17235         cp_parser_commit_to_tentative_parse (parser);
17236       /* Parse the declarator.  */
17237       declarator_token_start = token;
17238       declarator = cp_parser_declarator (parser,
17239                                          CP_PARSER_DECLARATOR_EITHER,
17240                                          /*ctor_dtor_or_conv_p=*/NULL,
17241                                          parenthesized_p,
17242                                          /*member_p=*/false);
17243       parser->default_arg_ok_p = saved_default_arg_ok_p;
17244       /* After the declarator, allow more attributes.  */
17245       decl_specifiers.attributes
17246         = chainon (decl_specifiers.attributes,
17247                    cp_parser_attributes_opt (parser));
17248     }
17249
17250   /* If the next token is an ellipsis, and we have not seen a
17251      declarator name, and the type of the declarator contains parameter
17252      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17253      a parameter pack expansion expression. Otherwise, leave the
17254      ellipsis for a C-style variadic function. */
17255   token = cp_lexer_peek_token (parser->lexer);
17256   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17257     {
17258       tree type = decl_specifiers.type;
17259
17260       if (type && DECL_P (type))
17261         type = TREE_TYPE (type);
17262
17263       if (type
17264           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17265           && declarator_can_be_parameter_pack (declarator)
17266           && (!declarator || !declarator->parameter_pack_p)
17267           && uses_parameter_packs (type))
17268         {
17269           /* Consume the `...'. */
17270           cp_lexer_consume_token (parser->lexer);
17271           maybe_warn_variadic_templates ();
17272           
17273           /* Build a pack expansion type */
17274           if (declarator)
17275             declarator->parameter_pack_p = true;
17276           else
17277             decl_specifiers.type = make_pack_expansion (type);
17278         }
17279     }
17280
17281   /* The restriction on defining new types applies only to the type
17282      of the parameter, not to the default argument.  */
17283   parser->type_definition_forbidden_message = saved_message;
17284
17285   /* If the next token is `=', then process a default argument.  */
17286   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17287     {
17288       token = cp_lexer_peek_token (parser->lexer);
17289       /* If we are defining a class, then the tokens that make up the
17290          default argument must be saved and processed later.  */
17291       if (!template_parm_p && at_class_scope_p ()
17292           && TYPE_BEING_DEFINED (current_class_type)
17293           && !LAMBDA_TYPE_P (current_class_type))
17294         default_argument = cp_parser_cache_defarg (parser, /*nsdmi=*/false);
17295       /* Outside of a class definition, we can just parse the
17296          assignment-expression.  */
17297       else
17298         default_argument
17299           = cp_parser_default_argument (parser, template_parm_p);
17300
17301       if (!parser->default_arg_ok_p)
17302         {
17303           if (flag_permissive)
17304             warning (0, "deprecated use of default argument for parameter of non-function");
17305           else
17306             {
17307               error_at (token->location,
17308                         "default arguments are only "
17309                         "permitted for function parameters");
17310               default_argument = NULL_TREE;
17311             }
17312         }
17313       else if ((declarator && declarator->parameter_pack_p)
17314                || (decl_specifiers.type
17315                    && PACK_EXPANSION_P (decl_specifiers.type)))
17316         {
17317           /* Find the name of the parameter pack.  */     
17318           cp_declarator *id_declarator = declarator;
17319           while (id_declarator && id_declarator->kind != cdk_id)
17320             id_declarator = id_declarator->declarator;
17321           
17322           if (id_declarator && id_declarator->kind == cdk_id)
17323             error_at (declarator_token_start->location,
17324                       template_parm_p
17325                       ? G_("template parameter pack %qD "
17326                            "cannot have a default argument")
17327                       : G_("parameter pack %qD cannot have "
17328                            "a default argument"),
17329                       id_declarator->u.id.unqualified_name);
17330           else
17331             error_at (declarator_token_start->location,
17332                       template_parm_p
17333                       ? G_("template parameter pack cannot have "
17334                            "a default argument")
17335                       : G_("parameter pack cannot have a "
17336                            "default argument"));
17337
17338           default_argument = NULL_TREE;
17339         }
17340     }
17341   else
17342     default_argument = NULL_TREE;
17343
17344   return make_parameter_declarator (&decl_specifiers,
17345                                     declarator,
17346                                     default_argument);
17347 }
17348
17349 /* Parse a default argument and return it.
17350
17351    TEMPLATE_PARM_P is true if this is a default argument for a
17352    non-type template parameter.  */
17353 static tree
17354 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17355 {
17356   tree default_argument = NULL_TREE;
17357   bool saved_greater_than_is_operator_p;
17358   bool saved_local_variables_forbidden_p;
17359   bool non_constant_p, is_direct_init;
17360
17361   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17362      set correctly.  */
17363   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17364   parser->greater_than_is_operator_p = !template_parm_p;
17365   /* Local variable names (and the `this' keyword) may not
17366      appear in a default argument.  */
17367   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17368   parser->local_variables_forbidden_p = true;
17369   /* Parse the assignment-expression.  */
17370   if (template_parm_p)
17371     push_deferring_access_checks (dk_no_deferred);
17372   default_argument
17373     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17374   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17375     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17376   if (template_parm_p)
17377     pop_deferring_access_checks ();
17378   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17379   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17380
17381   return default_argument;
17382 }
17383
17384 /* Parse a function-body.
17385
17386    function-body:
17387      compound_statement  */
17388
17389 static void
17390 cp_parser_function_body (cp_parser *parser)
17391 {
17392   cp_parser_compound_statement (parser, NULL, false, true);
17393 }
17394
17395 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17396    true if a ctor-initializer was present.  */
17397
17398 static bool
17399 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17400 {
17401   tree body, list;
17402   bool ctor_initializer_p;
17403   const bool check_body_p =
17404      DECL_CONSTRUCTOR_P (current_function_decl)
17405      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17406   tree last = NULL;
17407
17408   /* Begin the function body.  */
17409   body = begin_function_body ();
17410   /* Parse the optional ctor-initializer.  */
17411   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17412
17413   /* If we're parsing a constexpr constructor definition, we need
17414      to check that the constructor body is indeed empty.  However,
17415      before we get to cp_parser_function_body lot of junk has been
17416      generated, so we can't just check that we have an empty block.
17417      Rather we take a snapshot of the outermost block, and check whether
17418      cp_parser_function_body changed its state.  */
17419   if (check_body_p)
17420     {
17421       list = body;
17422       if (TREE_CODE (list) == BIND_EXPR)
17423         list = BIND_EXPR_BODY (list);
17424       if (TREE_CODE (list) == STATEMENT_LIST
17425           && STATEMENT_LIST_TAIL (list) != NULL)
17426         last = STATEMENT_LIST_TAIL (list)->stmt;
17427     }
17428   /* Parse the function-body.  */
17429   cp_parser_function_body (parser);
17430   if (check_body_p)
17431     check_constexpr_ctor_body (last, list);
17432   /* Finish the function body.  */
17433   finish_function_body (body);
17434
17435   return ctor_initializer_p;
17436 }
17437
17438 /* Parse an initializer.
17439
17440    initializer:
17441      = initializer-clause
17442      ( expression-list )
17443
17444    Returns an expression representing the initializer.  If no
17445    initializer is present, NULL_TREE is returned.
17446
17447    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17448    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17449    set to TRUE if there is no initializer present.  If there is an
17450    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17451    is set to true; otherwise it is set to false.  */
17452
17453 static tree
17454 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17455                        bool* non_constant_p)
17456 {
17457   cp_token *token;
17458   tree init;
17459
17460   /* Peek at the next token.  */
17461   token = cp_lexer_peek_token (parser->lexer);
17462
17463   /* Let our caller know whether or not this initializer was
17464      parenthesized.  */
17465   *is_direct_init = (token->type != CPP_EQ);
17466   /* Assume that the initializer is constant.  */
17467   *non_constant_p = false;
17468
17469   if (token->type == CPP_EQ)
17470     {
17471       /* Consume the `='.  */
17472       cp_lexer_consume_token (parser->lexer);
17473       /* Parse the initializer-clause.  */
17474       init = cp_parser_initializer_clause (parser, non_constant_p);
17475     }
17476   else if (token->type == CPP_OPEN_PAREN)
17477     {
17478       VEC(tree,gc) *vec;
17479       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17480                                                      /*cast_p=*/false,
17481                                                      /*allow_expansion_p=*/true,
17482                                                      non_constant_p);
17483       if (vec == NULL)
17484         return error_mark_node;
17485       init = build_tree_list_vec (vec);
17486       release_tree_vector (vec);
17487     }
17488   else if (token->type == CPP_OPEN_BRACE)
17489     {
17490       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17491       init = cp_parser_braced_list (parser, non_constant_p);
17492       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17493     }
17494   else
17495     {
17496       /* Anything else is an error.  */
17497       cp_parser_error (parser, "expected initializer");
17498       init = error_mark_node;
17499     }
17500
17501   return init;
17502 }
17503
17504 /* Parse an initializer-clause.
17505
17506    initializer-clause:
17507      assignment-expression
17508      braced-init-list
17509
17510    Returns an expression representing the initializer.
17511
17512    If the `assignment-expression' production is used the value
17513    returned is simply a representation for the expression.
17514
17515    Otherwise, calls cp_parser_braced_list.  */
17516
17517 static tree
17518 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17519 {
17520   tree initializer;
17521
17522   /* Assume the expression is constant.  */
17523   *non_constant_p = false;
17524
17525   /* If it is not a `{', then we are looking at an
17526      assignment-expression.  */
17527   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17528     {
17529       initializer
17530         = cp_parser_constant_expression (parser,
17531                                         /*allow_non_constant_p=*/true,
17532                                         non_constant_p);
17533     }
17534   else
17535     initializer = cp_parser_braced_list (parser, non_constant_p);
17536
17537   return initializer;
17538 }
17539
17540 /* Parse a brace-enclosed initializer list.
17541
17542    braced-init-list:
17543      { initializer-list , [opt] }
17544      { }
17545
17546    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17547    the elements of the initializer-list (or NULL, if the last
17548    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17549    NULL_TREE.  There is no way to detect whether or not the optional
17550    trailing `,' was provided.  NON_CONSTANT_P is as for
17551    cp_parser_initializer.  */     
17552
17553 static tree
17554 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17555 {
17556   tree initializer;
17557
17558   /* Consume the `{' token.  */
17559   cp_lexer_consume_token (parser->lexer);
17560   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17561   initializer = make_node (CONSTRUCTOR);
17562   /* If it's not a `}', then there is a non-trivial initializer.  */
17563   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17564     {
17565       /* Parse the initializer list.  */
17566       CONSTRUCTOR_ELTS (initializer)
17567         = cp_parser_initializer_list (parser, non_constant_p);
17568       /* A trailing `,' token is allowed.  */
17569       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17570         cp_lexer_consume_token (parser->lexer);
17571     }
17572   /* Now, there should be a trailing `}'.  */
17573   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17574   TREE_TYPE (initializer) = init_list_type_node;
17575   return initializer;
17576 }
17577
17578 /* Parse an initializer-list.
17579
17580    initializer-list:
17581      initializer-clause ... [opt]
17582      initializer-list , initializer-clause ... [opt]
17583
17584    GNU Extension:
17585
17586    initializer-list:
17587      designation initializer-clause ...[opt]
17588      initializer-list , designation initializer-clause ...[opt]
17589
17590    designation:
17591      . identifier =
17592      identifier :
17593      [ constant-expression ] =
17594
17595    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17596    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17597    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17598    as for cp_parser_initializer.  */
17599
17600 static VEC(constructor_elt,gc) *
17601 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17602 {
17603   VEC(constructor_elt,gc) *v = NULL;
17604
17605   /* Assume all of the expressions are constant.  */
17606   *non_constant_p = false;
17607
17608   /* Parse the rest of the list.  */
17609   while (true)
17610     {
17611       cp_token *token;
17612       tree designator;
17613       tree initializer;
17614       bool clause_non_constant_p;
17615
17616       /* If the next token is an identifier and the following one is a
17617          colon, we are looking at the GNU designated-initializer
17618          syntax.  */
17619       if (cp_parser_allow_gnu_extensions_p (parser)
17620           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17621           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17622         {
17623           /* Warn the user that they are using an extension.  */
17624           pedwarn (input_location, OPT_pedantic, 
17625                    "ISO C++ does not allow designated initializers");
17626           /* Consume the identifier.  */
17627           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17628           /* Consume the `:'.  */
17629           cp_lexer_consume_token (parser->lexer);
17630         }
17631       /* Also handle the C99 syntax, '. id ='.  */
17632       else if (cp_parser_allow_gnu_extensions_p (parser)
17633                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17634                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17635                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17636         {
17637           /* Warn the user that they are using an extension.  */
17638           pedwarn (input_location, OPT_pedantic,
17639                    "ISO C++ does not allow C99 designated initializers");
17640           /* Consume the `.'.  */
17641           cp_lexer_consume_token (parser->lexer);
17642           /* Consume the identifier.  */
17643           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17644           /* Consume the `='.  */
17645           cp_lexer_consume_token (parser->lexer);
17646         }
17647       /* Also handle C99 array designators, '[ const ] ='.  */
17648       else if (cp_parser_allow_gnu_extensions_p (parser)
17649                && !c_dialect_objc ()
17650                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17651         {
17652           /* In C++11, [ could start a lambda-introducer.  */
17653           cp_parser_parse_tentatively (parser);
17654           cp_lexer_consume_token (parser->lexer);
17655           designator = cp_parser_constant_expression (parser, false, NULL);
17656           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17657           cp_parser_require (parser, CPP_EQ, RT_EQ);
17658           if (!cp_parser_parse_definitely (parser))
17659             designator = NULL_TREE;
17660         }
17661       else
17662         designator = NULL_TREE;
17663
17664       /* Parse the initializer.  */
17665       initializer = cp_parser_initializer_clause (parser,
17666                                                   &clause_non_constant_p);
17667       /* If any clause is non-constant, so is the entire initializer.  */
17668       if (clause_non_constant_p)
17669         *non_constant_p = true;
17670
17671       /* If we have an ellipsis, this is an initializer pack
17672          expansion.  */
17673       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17674         {
17675           /* Consume the `...'.  */
17676           cp_lexer_consume_token (parser->lexer);
17677
17678           /* Turn the initializer into an initializer expansion.  */
17679           initializer = make_pack_expansion (initializer);
17680         }
17681
17682       /* Add it to the vector.  */
17683       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17684
17685       /* If the next token is not a comma, we have reached the end of
17686          the list.  */
17687       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17688         break;
17689
17690       /* Peek at the next token.  */
17691       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17692       /* If the next token is a `}', then we're still done.  An
17693          initializer-clause can have a trailing `,' after the
17694          initializer-list and before the closing `}'.  */
17695       if (token->type == CPP_CLOSE_BRACE)
17696         break;
17697
17698       /* Consume the `,' token.  */
17699       cp_lexer_consume_token (parser->lexer);
17700     }
17701
17702   return v;
17703 }
17704
17705 /* Classes [gram.class] */
17706
17707 /* Parse a class-name.
17708
17709    class-name:
17710      identifier
17711      template-id
17712
17713    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17714    to indicate that names looked up in dependent types should be
17715    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17716    keyword has been used to indicate that the name that appears next
17717    is a template.  TAG_TYPE indicates the explicit tag given before
17718    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17719    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17720    is the class being defined in a class-head.
17721
17722    Returns the TYPE_DECL representing the class.  */
17723
17724 static tree
17725 cp_parser_class_name (cp_parser *parser,
17726                       bool typename_keyword_p,
17727                       bool template_keyword_p,
17728                       enum tag_types tag_type,
17729                       bool check_dependency_p,
17730                       bool class_head_p,
17731                       bool is_declaration)
17732 {
17733   tree decl;
17734   tree scope;
17735   bool typename_p;
17736   cp_token *token;
17737   tree identifier = NULL_TREE;
17738
17739   /* All class-names start with an identifier.  */
17740   token = cp_lexer_peek_token (parser->lexer);
17741   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17742     {
17743       cp_parser_error (parser, "expected class-name");
17744       return error_mark_node;
17745     }
17746
17747   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17748      to a template-id, so we save it here.  */
17749   scope = parser->scope;
17750   if (scope == error_mark_node)
17751     return error_mark_node;
17752
17753   /* Any name names a type if we're following the `typename' keyword
17754      in a qualified name where the enclosing scope is type-dependent.  */
17755   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17756                 && dependent_type_p (scope));
17757   /* Handle the common case (an identifier, but not a template-id)
17758      efficiently.  */
17759   if (token->type == CPP_NAME
17760       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17761     {
17762       cp_token *identifier_token;
17763       bool ambiguous_p;
17764
17765       /* Look for the identifier.  */
17766       identifier_token = cp_lexer_peek_token (parser->lexer);
17767       ambiguous_p = identifier_token->ambiguous_p;
17768       identifier = cp_parser_identifier (parser);
17769       /* If the next token isn't an identifier, we are certainly not
17770          looking at a class-name.  */
17771       if (identifier == error_mark_node)
17772         decl = error_mark_node;
17773       /* If we know this is a type-name, there's no need to look it
17774          up.  */
17775       else if (typename_p)
17776         decl = identifier;
17777       else
17778         {
17779           tree ambiguous_decls;
17780           /* If we already know that this lookup is ambiguous, then
17781              we've already issued an error message; there's no reason
17782              to check again.  */
17783           if (ambiguous_p)
17784             {
17785               cp_parser_simulate_error (parser);
17786               return error_mark_node;
17787             }
17788           /* If the next token is a `::', then the name must be a type
17789              name.
17790
17791              [basic.lookup.qual]
17792
17793              During the lookup for a name preceding the :: scope
17794              resolution operator, object, function, and enumerator
17795              names are ignored.  */
17796           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17797             tag_type = typename_type;
17798           /* Look up the name.  */
17799           decl = cp_parser_lookup_name (parser, identifier,
17800                                         tag_type,
17801                                         /*is_template=*/false,
17802                                         /*is_namespace=*/false,
17803                                         check_dependency_p,
17804                                         &ambiguous_decls,
17805                                         identifier_token->location);
17806           if (ambiguous_decls)
17807             {
17808               if (cp_parser_parsing_tentatively (parser))
17809                 cp_parser_simulate_error (parser);
17810               return error_mark_node;
17811             }
17812         }
17813     }
17814   else
17815     {
17816       /* Try a template-id.  */
17817       decl = cp_parser_template_id (parser, template_keyword_p,
17818                                     check_dependency_p,
17819                                     is_declaration);
17820       if (decl == error_mark_node)
17821         return error_mark_node;
17822     }
17823
17824   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17825
17826   /* If this is a typename, create a TYPENAME_TYPE.  */
17827   if (typename_p && decl != error_mark_node)
17828     {
17829       decl = make_typename_type (scope, decl, typename_type,
17830                                  /*complain=*/tf_error);
17831       if (decl != error_mark_node)
17832         decl = TYPE_NAME (decl);
17833     }
17834
17835   /* Check to see that it is really the name of a class.  */
17836   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17837       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17838       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17839     /* Situations like this:
17840
17841          template <typename T> struct A {
17842            typename T::template X<int>::I i;
17843          };
17844
17845        are problematic.  Is `T::template X<int>' a class-name?  The
17846        standard does not seem to be definitive, but there is no other
17847        valid interpretation of the following `::'.  Therefore, those
17848        names are considered class-names.  */
17849     {
17850       decl = make_typename_type (scope, decl, tag_type, tf_error);
17851       if (decl != error_mark_node)
17852         decl = TYPE_NAME (decl);
17853     }
17854   else if (TREE_CODE (decl) != TYPE_DECL
17855            || TREE_TYPE (decl) == error_mark_node
17856            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17857            /* In Objective-C 2.0, a classname followed by '.' starts a
17858               dot-syntax expression, and it's not a type-name.  */
17859            || (c_dialect_objc ()
17860                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17861                && objc_is_class_name (decl)))
17862     decl = error_mark_node;
17863
17864   if (decl == error_mark_node)
17865     cp_parser_error (parser, "expected class-name");
17866   else if (identifier && !parser->scope)
17867     maybe_note_name_used_in_class (identifier, decl);
17868
17869   return decl;
17870 }
17871
17872 /* Parse a class-specifier.
17873
17874    class-specifier:
17875      class-head { member-specification [opt] }
17876
17877    Returns the TREE_TYPE representing the class.  */
17878
17879 static tree
17880 cp_parser_class_specifier_1 (cp_parser* parser)
17881 {
17882   tree type;
17883   tree attributes = NULL_TREE;
17884   bool nested_name_specifier_p;
17885   unsigned saved_num_template_parameter_lists;
17886   bool saved_in_function_body;
17887   unsigned char in_statement;
17888   bool in_switch_statement_p;
17889   bool saved_in_unbraced_linkage_specification_p;
17890   tree old_scope = NULL_TREE;
17891   tree scope = NULL_TREE;
17892   tree bases;
17893   cp_token *closing_brace;
17894
17895   push_deferring_access_checks (dk_no_deferred);
17896
17897   /* Parse the class-head.  */
17898   type = cp_parser_class_head (parser,
17899                                &nested_name_specifier_p,
17900                                &attributes,
17901                                &bases);
17902   /* If the class-head was a semantic disaster, skip the entire body
17903      of the class.  */
17904   if (!type)
17905     {
17906       cp_parser_skip_to_end_of_block_or_statement (parser);
17907       pop_deferring_access_checks ();
17908       return error_mark_node;
17909     }
17910
17911   /* Look for the `{'.  */
17912   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17913     {
17914       pop_deferring_access_checks ();
17915       return error_mark_node;
17916     }
17917
17918   /* Process the base classes. If they're invalid, skip the 
17919      entire class body.  */
17920   if (!xref_basetypes (type, bases))
17921     {
17922       /* Consuming the closing brace yields better error messages
17923          later on.  */
17924       if (cp_parser_skip_to_closing_brace (parser))
17925         cp_lexer_consume_token (parser->lexer);
17926       pop_deferring_access_checks ();
17927       return error_mark_node;
17928     }
17929
17930   /* Issue an error message if type-definitions are forbidden here.  */
17931   cp_parser_check_type_definition (parser);
17932   /* Remember that we are defining one more class.  */
17933   ++parser->num_classes_being_defined;
17934   /* Inside the class, surrounding template-parameter-lists do not
17935      apply.  */
17936   saved_num_template_parameter_lists
17937     = parser->num_template_parameter_lists;
17938   parser->num_template_parameter_lists = 0;
17939   /* We are not in a function body.  */
17940   saved_in_function_body = parser->in_function_body;
17941   parser->in_function_body = false;
17942   /* Or in a loop.  */
17943   in_statement = parser->in_statement;
17944   parser->in_statement = 0;
17945   /* Or in a switch.  */
17946   in_switch_statement_p = parser->in_switch_statement_p;
17947   parser->in_switch_statement_p = false;
17948   /* We are not immediately inside an extern "lang" block.  */
17949   saved_in_unbraced_linkage_specification_p
17950     = parser->in_unbraced_linkage_specification_p;
17951   parser->in_unbraced_linkage_specification_p = false;
17952
17953   /* Start the class.  */
17954   if (nested_name_specifier_p)
17955     {
17956       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17957       old_scope = push_inner_scope (scope);
17958     }
17959   type = begin_class_definition (type, attributes);
17960
17961   if (type == error_mark_node)
17962     /* If the type is erroneous, skip the entire body of the class.  */
17963     cp_parser_skip_to_closing_brace (parser);
17964   else
17965     /* Parse the member-specification.  */
17966     cp_parser_member_specification_opt (parser);
17967
17968   /* Look for the trailing `}'.  */
17969   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17970   /* Look for trailing attributes to apply to this class.  */
17971   if (cp_parser_allow_gnu_extensions_p (parser))
17972     attributes = cp_parser_attributes_opt (parser);
17973   if (type != error_mark_node)
17974     type = finish_struct (type, attributes);
17975   if (nested_name_specifier_p)
17976     pop_inner_scope (old_scope, scope);
17977
17978   /* We've finished a type definition.  Check for the common syntax
17979      error of forgetting a semicolon after the definition.  We need to
17980      be careful, as we can't just check for not-a-semicolon and be done
17981      with it; the user might have typed:
17982
17983      class X { } c = ...;
17984      class X { } *p = ...;
17985
17986      and so forth.  Instead, enumerate all the possible tokens that
17987      might follow this production; if we don't see one of them, then
17988      complain and silently insert the semicolon.  */
17989   {
17990     cp_token *token = cp_lexer_peek_token (parser->lexer);
17991     bool want_semicolon = true;
17992
17993     switch (token->type)
17994       {
17995       case CPP_NAME:
17996       case CPP_SEMICOLON:
17997       case CPP_MULT:
17998       case CPP_AND:
17999       case CPP_OPEN_PAREN:
18000       case CPP_CLOSE_PAREN:
18001       case CPP_COMMA:
18002         want_semicolon = false;
18003         break;
18004
18005         /* While it's legal for type qualifiers and storage class
18006            specifiers to follow type definitions in the grammar, only
18007            compiler testsuites contain code like that.  Assume that if
18008            we see such code, then what we're really seeing is a case
18009            like:
18010
18011            class X { }
18012            const <type> var = ...;
18013
18014            or
18015
18016            class Y { }
18017            static <type> func (...) ...
18018
18019            i.e. the qualifier or specifier applies to the next
18020            declaration.  To do so, however, we need to look ahead one
18021            more token to see if *that* token is a type specifier.
18022
18023            This code could be improved to handle:
18024
18025            class Z { }
18026            static const <type> var = ...;  */
18027       case CPP_KEYWORD:
18028         if (keyword_is_decl_specifier (token->keyword))
18029           {
18030             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18031
18032             /* Handling user-defined types here would be nice, but very
18033                tricky.  */
18034             want_semicolon
18035               = (lookahead->type == CPP_KEYWORD
18036                  && keyword_begins_type_specifier (lookahead->keyword));
18037           }
18038         break;
18039       default:
18040         break;
18041       }
18042
18043     /* If we don't have a type, then something is very wrong and we
18044        shouldn't try to do anything clever.  Likewise for not seeing the
18045        closing brace.  */
18046     if (closing_brace && TYPE_P (type) && want_semicolon)
18047       {
18048         cp_token_position prev
18049           = cp_lexer_previous_token_position (parser->lexer);
18050         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18051         location_t loc = prev_token->location;
18052
18053         if (CLASSTYPE_DECLARED_CLASS (type))
18054           error_at (loc, "expected %<;%> after class definition");
18055         else if (TREE_CODE (type) == RECORD_TYPE)
18056           error_at (loc, "expected %<;%> after struct definition");
18057         else if (TREE_CODE (type) == UNION_TYPE)
18058           error_at (loc, "expected %<;%> after union definition");
18059         else
18060           gcc_unreachable ();
18061
18062         /* Unget one token and smash it to look as though we encountered
18063            a semicolon in the input stream.  */
18064         cp_lexer_set_token_position (parser->lexer, prev);
18065         token = cp_lexer_peek_token (parser->lexer);
18066         token->type = CPP_SEMICOLON;
18067         token->keyword = RID_MAX;
18068       }
18069   }
18070
18071   /* If this class is not itself within the scope of another class,
18072      then we need to parse the bodies of all of the queued function
18073      definitions.  Note that the queued functions defined in a class
18074      are not always processed immediately following the
18075      class-specifier for that class.  Consider:
18076
18077        struct A {
18078          struct B { void f() { sizeof (A); } };
18079        };
18080
18081      If `f' were processed before the processing of `A' were
18082      completed, there would be no way to compute the size of `A'.
18083      Note that the nesting we are interested in here is lexical --
18084      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18085      for:
18086
18087        struct A { struct B; };
18088        struct A::B { void f() { } };
18089
18090      there is no need to delay the parsing of `A::B::f'.  */
18091   if (--parser->num_classes_being_defined == 0)
18092     {
18093       tree decl;
18094       tree class_type = NULL_TREE;
18095       tree pushed_scope = NULL_TREE;
18096       unsigned ix;
18097       cp_default_arg_entry *e;
18098       tree save_ccp, save_ccr;
18099
18100       /* In a first pass, parse default arguments to the functions.
18101          Then, in a second pass, parse the bodies of the functions.
18102          This two-phased approach handles cases like:
18103
18104             struct S {
18105               void f() { g(); }
18106               void g(int i = 3);
18107             };
18108
18109          */
18110       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18111                         ix, e)
18112         {
18113           decl = e->decl;
18114           /* If there are default arguments that have not yet been processed,
18115              take care of them now.  */
18116           if (class_type != e->class_type)
18117             {
18118               if (pushed_scope)
18119                 pop_scope (pushed_scope);
18120               class_type = e->class_type;
18121               pushed_scope = push_scope (class_type);
18122             }
18123           /* Make sure that any template parameters are in scope.  */
18124           maybe_begin_member_template_processing (decl);
18125           /* Parse the default argument expressions.  */
18126           cp_parser_late_parsing_default_args (parser, decl);
18127           /* Remove any template parameters from the symbol table.  */
18128           maybe_end_member_template_processing ();
18129         }
18130       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18131       /* Now parse any NSDMIs.  */
18132       save_ccp = current_class_ptr;
18133       save_ccr = current_class_ref;
18134       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18135         {
18136           if (class_type != DECL_CONTEXT (decl))
18137             {
18138               if (pushed_scope)
18139                 pop_scope (pushed_scope);
18140               class_type = DECL_CONTEXT (decl);
18141               pushed_scope = push_scope (class_type);
18142             }
18143           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18144           cp_parser_late_parsing_nsdmi (parser, decl);
18145         }
18146       VEC_truncate (tree, unparsed_nsdmis, 0);
18147       current_class_ptr = save_ccp;
18148       current_class_ref = save_ccr;
18149       if (pushed_scope)
18150         pop_scope (pushed_scope);
18151       /* Now parse the body of the functions.  */
18152       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18153         cp_parser_late_parsing_for_member (parser, decl);
18154       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18155     }
18156
18157   /* Put back any saved access checks.  */
18158   pop_deferring_access_checks ();
18159
18160   /* Restore saved state.  */
18161   parser->in_switch_statement_p = in_switch_statement_p;
18162   parser->in_statement = in_statement;
18163   parser->in_function_body = saved_in_function_body;
18164   parser->num_template_parameter_lists
18165     = saved_num_template_parameter_lists;
18166   parser->in_unbraced_linkage_specification_p
18167     = saved_in_unbraced_linkage_specification_p;
18168
18169   return type;
18170 }
18171
18172 static tree
18173 cp_parser_class_specifier (cp_parser* parser)
18174 {
18175   tree ret;
18176   timevar_push (TV_PARSE_STRUCT);
18177   ret = cp_parser_class_specifier_1 (parser);
18178   timevar_pop (TV_PARSE_STRUCT);
18179   return ret;
18180 }
18181
18182 /* Parse a class-head.
18183
18184    class-head:
18185      class-key identifier [opt] base-clause [opt]
18186      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18187      class-key nested-name-specifier [opt] template-id
18188        base-clause [opt]
18189
18190    class-virt-specifier:
18191      final
18192
18193    GNU Extensions:
18194      class-key attributes identifier [opt] base-clause [opt]
18195      class-key attributes nested-name-specifier identifier base-clause [opt]
18196      class-key attributes nested-name-specifier [opt] template-id
18197        base-clause [opt]
18198
18199    Upon return BASES is initialized to the list of base classes (or
18200    NULL, if there are none) in the same form returned by
18201    cp_parser_base_clause.
18202
18203    Returns the TYPE of the indicated class.  Sets
18204    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18205    involving a nested-name-specifier was used, and FALSE otherwise.
18206
18207    Returns error_mark_node if this is not a class-head.
18208
18209    Returns NULL_TREE if the class-head is syntactically valid, but
18210    semantically invalid in a way that means we should skip the entire
18211    body of the class.  */
18212
18213 static tree
18214 cp_parser_class_head (cp_parser* parser,
18215                       bool* nested_name_specifier_p,
18216                       tree *attributes_p,
18217                       tree *bases)
18218 {
18219   tree nested_name_specifier;
18220   enum tag_types class_key;
18221   tree id = NULL_TREE;
18222   tree type = NULL_TREE;
18223   tree attributes;
18224   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18225   bool template_id_p = false;
18226   bool qualified_p = false;
18227   bool invalid_nested_name_p = false;
18228   bool invalid_explicit_specialization_p = false;
18229   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18230   tree pushed_scope = NULL_TREE;
18231   unsigned num_templates;
18232   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18233   /* Assume no nested-name-specifier will be present.  */
18234   *nested_name_specifier_p = false;
18235   /* Assume no template parameter lists will be used in defining the
18236      type.  */
18237   num_templates = 0;
18238   parser->colon_corrects_to_scope_p = false;
18239
18240   *bases = NULL_TREE;
18241
18242   /* Look for the class-key.  */
18243   class_key = cp_parser_class_key (parser);
18244   if (class_key == none_type)
18245     return error_mark_node;
18246
18247   /* Parse the attributes.  */
18248   attributes = cp_parser_attributes_opt (parser);
18249
18250   /* If the next token is `::', that is invalid -- but sometimes
18251      people do try to write:
18252
18253        struct ::S {};
18254
18255      Handle this gracefully by accepting the extra qualifier, and then
18256      issuing an error about it later if this really is a
18257      class-head.  If it turns out just to be an elaborated type
18258      specifier, remain silent.  */
18259   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18260     qualified_p = true;
18261
18262   push_deferring_access_checks (dk_no_check);
18263
18264   /* Determine the name of the class.  Begin by looking for an
18265      optional nested-name-specifier.  */
18266   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18267   nested_name_specifier
18268     = cp_parser_nested_name_specifier_opt (parser,
18269                                            /*typename_keyword_p=*/false,
18270                                            /*check_dependency_p=*/false,
18271                                            /*type_p=*/false,
18272                                            /*is_declaration=*/false);
18273   /* If there was a nested-name-specifier, then there *must* be an
18274      identifier.  */
18275   if (nested_name_specifier)
18276     {
18277       type_start_token = cp_lexer_peek_token (parser->lexer);
18278       /* Although the grammar says `identifier', it really means
18279          `class-name' or `template-name'.  You are only allowed to
18280          define a class that has already been declared with this
18281          syntax.
18282
18283          The proposed resolution for Core Issue 180 says that wherever
18284          you see `class T::X' you should treat `X' as a type-name.
18285
18286          It is OK to define an inaccessible class; for example:
18287
18288            class A { class B; };
18289            class A::B {};
18290
18291          We do not know if we will see a class-name, or a
18292          template-name.  We look for a class-name first, in case the
18293          class-name is a template-id; if we looked for the
18294          template-name first we would stop after the template-name.  */
18295       cp_parser_parse_tentatively (parser);
18296       type = cp_parser_class_name (parser,
18297                                    /*typename_keyword_p=*/false,
18298                                    /*template_keyword_p=*/false,
18299                                    class_type,
18300                                    /*check_dependency_p=*/false,
18301                                    /*class_head_p=*/true,
18302                                    /*is_declaration=*/false);
18303       /* If that didn't work, ignore the nested-name-specifier.  */
18304       if (!cp_parser_parse_definitely (parser))
18305         {
18306           invalid_nested_name_p = true;
18307           type_start_token = cp_lexer_peek_token (parser->lexer);
18308           id = cp_parser_identifier (parser);
18309           if (id == error_mark_node)
18310             id = NULL_TREE;
18311         }
18312       /* If we could not find a corresponding TYPE, treat this
18313          declaration like an unqualified declaration.  */
18314       if (type == error_mark_node)
18315         nested_name_specifier = NULL_TREE;
18316       /* Otherwise, count the number of templates used in TYPE and its
18317          containing scopes.  */
18318       else
18319         {
18320           tree scope;
18321
18322           for (scope = TREE_TYPE (type);
18323                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18324                scope = (TYPE_P (scope)
18325                         ? TYPE_CONTEXT (scope)
18326                         : DECL_CONTEXT (scope)))
18327             if (TYPE_P (scope)
18328                 && CLASS_TYPE_P (scope)
18329                 && CLASSTYPE_TEMPLATE_INFO (scope)
18330                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18331                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18332               ++num_templates;
18333         }
18334     }
18335   /* Otherwise, the identifier is optional.  */
18336   else
18337     {
18338       /* We don't know whether what comes next is a template-id,
18339          an identifier, or nothing at all.  */
18340       cp_parser_parse_tentatively (parser);
18341       /* Check for a template-id.  */
18342       type_start_token = cp_lexer_peek_token (parser->lexer);
18343       id = cp_parser_template_id (parser,
18344                                   /*template_keyword_p=*/false,
18345                                   /*check_dependency_p=*/true,
18346                                   /*is_declaration=*/true);
18347       /* If that didn't work, it could still be an identifier.  */
18348       if (!cp_parser_parse_definitely (parser))
18349         {
18350           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18351             {
18352               type_start_token = cp_lexer_peek_token (parser->lexer);
18353               id = cp_parser_identifier (parser);
18354             }
18355           else
18356             id = NULL_TREE;
18357         }
18358       else
18359         {
18360           template_id_p = true;
18361           ++num_templates;
18362         }
18363     }
18364
18365   pop_deferring_access_checks ();
18366
18367   if (id)
18368     {
18369       cp_parser_check_for_invalid_template_id (parser, id,
18370                                                type_start_token->location);
18371     }
18372   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18373
18374   /* If it's not a `:' or a `{' then we can't really be looking at a
18375      class-head, since a class-head only appears as part of a
18376      class-specifier.  We have to detect this situation before calling
18377      xref_tag, since that has irreversible side-effects.  */
18378   if (!cp_parser_next_token_starts_class_definition_p (parser))
18379     {
18380       cp_parser_error (parser, "expected %<{%> or %<:%>");
18381       type = error_mark_node;
18382       goto out;
18383     }
18384
18385   /* At this point, we're going ahead with the class-specifier, even
18386      if some other problem occurs.  */
18387   cp_parser_commit_to_tentative_parse (parser);
18388   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18389     {
18390       cp_parser_error (parser,
18391                        "cannot specify %<override%> for a class");
18392       type = error_mark_node;
18393       goto out;
18394     }
18395   /* Issue the error about the overly-qualified name now.  */
18396   if (qualified_p)
18397     {
18398       cp_parser_error (parser,
18399                        "global qualification of class name is invalid");
18400       type = error_mark_node;
18401       goto out;
18402     }
18403   else if (invalid_nested_name_p)
18404     {
18405       cp_parser_error (parser,
18406                        "qualified name does not name a class");
18407       type = error_mark_node;
18408       goto out;
18409     }
18410   else if (nested_name_specifier)
18411     {
18412       tree scope;
18413
18414       /* Reject typedef-names in class heads.  */
18415       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18416         {
18417           error_at (type_start_token->location,
18418                     "invalid class name in declaration of %qD",
18419                     type);
18420           type = NULL_TREE;
18421           goto done;
18422         }
18423
18424       /* Figure out in what scope the declaration is being placed.  */
18425       scope = current_scope ();
18426       /* If that scope does not contain the scope in which the
18427          class was originally declared, the program is invalid.  */
18428       if (scope && !is_ancestor (scope, nested_name_specifier))
18429         {
18430           if (at_namespace_scope_p ())
18431             error_at (type_start_token->location,
18432                       "declaration of %qD in namespace %qD which does not "
18433                       "enclose %qD",
18434                       type, scope, nested_name_specifier);
18435           else
18436             error_at (type_start_token->location,
18437                       "declaration of %qD in %qD which does not enclose %qD",
18438                       type, scope, nested_name_specifier);
18439           type = NULL_TREE;
18440           goto done;
18441         }
18442       /* [dcl.meaning]
18443
18444          A declarator-id shall not be qualified except for the
18445          definition of a ... nested class outside of its class
18446          ... [or] the definition or explicit instantiation of a
18447          class member of a namespace outside of its namespace.  */
18448       if (scope == nested_name_specifier)
18449         {
18450           permerror (nested_name_specifier_token_start->location,
18451                      "extra qualification not allowed");
18452           nested_name_specifier = NULL_TREE;
18453           num_templates = 0;
18454         }
18455     }
18456   /* An explicit-specialization must be preceded by "template <>".  If
18457      it is not, try to recover gracefully.  */
18458   if (at_namespace_scope_p ()
18459       && parser->num_template_parameter_lists == 0
18460       && template_id_p)
18461     {
18462       error_at (type_start_token->location,
18463                 "an explicit specialization must be preceded by %<template <>%>");
18464       invalid_explicit_specialization_p = true;
18465       /* Take the same action that would have been taken by
18466          cp_parser_explicit_specialization.  */
18467       ++parser->num_template_parameter_lists;
18468       begin_specialization ();
18469     }
18470   /* There must be no "return" statements between this point and the
18471      end of this function; set "type "to the correct return value and
18472      use "goto done;" to return.  */
18473   /* Make sure that the right number of template parameters were
18474      present.  */
18475   if (!cp_parser_check_template_parameters (parser, num_templates,
18476                                             type_start_token->location,
18477                                             /*declarator=*/NULL))
18478     {
18479       /* If something went wrong, there is no point in even trying to
18480          process the class-definition.  */
18481       type = NULL_TREE;
18482       goto done;
18483     }
18484
18485   /* Look up the type.  */
18486   if (template_id_p)
18487     {
18488       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18489           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18490               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18491         {
18492           error_at (type_start_token->location,
18493                     "function template %qD redeclared as a class template", id);
18494           type = error_mark_node;
18495         }
18496       else
18497         {
18498           type = TREE_TYPE (id);
18499           type = maybe_process_partial_specialization (type);
18500         }
18501       if (nested_name_specifier)
18502         pushed_scope = push_scope (nested_name_specifier);
18503     }
18504   else if (nested_name_specifier)
18505     {
18506       tree class_type;
18507
18508       /* Given:
18509
18510             template <typename T> struct S { struct T };
18511             template <typename T> struct S<T>::T { };
18512
18513          we will get a TYPENAME_TYPE when processing the definition of
18514          `S::T'.  We need to resolve it to the actual type before we
18515          try to define it.  */
18516       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18517         {
18518           class_type = resolve_typename_type (TREE_TYPE (type),
18519                                               /*only_current_p=*/false);
18520           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18521             type = TYPE_NAME (class_type);
18522           else
18523             {
18524               cp_parser_error (parser, "could not resolve typename type");
18525               type = error_mark_node;
18526             }
18527         }
18528
18529       if (maybe_process_partial_specialization (TREE_TYPE (type))
18530           == error_mark_node)
18531         {
18532           type = NULL_TREE;
18533           goto done;
18534         }
18535
18536       class_type = current_class_type;
18537       /* Enter the scope indicated by the nested-name-specifier.  */
18538       pushed_scope = push_scope (nested_name_specifier);
18539       /* Get the canonical version of this type.  */
18540       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18541       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18542           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18543         {
18544           type = push_template_decl (type);
18545           if (type == error_mark_node)
18546             {
18547               type = NULL_TREE;
18548               goto done;
18549             }
18550         }
18551
18552       type = TREE_TYPE (type);
18553       *nested_name_specifier_p = true;
18554     }
18555   else      /* The name is not a nested name.  */
18556     {
18557       /* If the class was unnamed, create a dummy name.  */
18558       if (!id)
18559         id = make_anon_name ();
18560       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18561                        parser->num_template_parameter_lists);
18562     }
18563
18564   /* Indicate whether this class was declared as a `class' or as a
18565      `struct'.  */
18566   if (TREE_CODE (type) == RECORD_TYPE)
18567     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18568   cp_parser_check_class_key (class_key, type);
18569
18570   /* If this type was already complete, and we see another definition,
18571      that's an error.  */
18572   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18573     {
18574       error_at (type_start_token->location, "redefinition of %q#T",
18575                 type);
18576       error_at (type_start_token->location, "previous definition of %q+#T",
18577                 type);
18578       type = NULL_TREE;
18579       goto done;
18580     }
18581   else if (type == error_mark_node)
18582     type = NULL_TREE;
18583
18584   /* We will have entered the scope containing the class; the names of
18585      base classes should be looked up in that context.  For example:
18586
18587        struct A { struct B {}; struct C; };
18588        struct A::C : B {};
18589
18590      is valid.  */
18591
18592   /* Get the list of base-classes, if there is one.  */
18593   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18594     *bases = cp_parser_base_clause (parser);
18595
18596  done:
18597   /* Leave the scope given by the nested-name-specifier.  We will
18598      enter the class scope itself while processing the members.  */
18599   if (pushed_scope)
18600     pop_scope (pushed_scope);
18601
18602   if (invalid_explicit_specialization_p)
18603     {
18604       end_specialization ();
18605       --parser->num_template_parameter_lists;
18606     }
18607
18608   if (type)
18609     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18610   *attributes_p = attributes;
18611   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18612     CLASSTYPE_FINAL (type) = 1;
18613  out:
18614   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18615   return type;
18616 }
18617
18618 /* Parse a class-key.
18619
18620    class-key:
18621      class
18622      struct
18623      union
18624
18625    Returns the kind of class-key specified, or none_type to indicate
18626    error.  */
18627
18628 static enum tag_types
18629 cp_parser_class_key (cp_parser* parser)
18630 {
18631   cp_token *token;
18632   enum tag_types tag_type;
18633
18634   /* Look for the class-key.  */
18635   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18636   if (!token)
18637     return none_type;
18638
18639   /* Check to see if the TOKEN is a class-key.  */
18640   tag_type = cp_parser_token_is_class_key (token);
18641   if (!tag_type)
18642     cp_parser_error (parser, "expected class-key");
18643   return tag_type;
18644 }
18645
18646 /* Parse an (optional) member-specification.
18647
18648    member-specification:
18649      member-declaration member-specification [opt]
18650      access-specifier : member-specification [opt]  */
18651
18652 static void
18653 cp_parser_member_specification_opt (cp_parser* parser)
18654 {
18655   while (true)
18656     {
18657       cp_token *token;
18658       enum rid keyword;
18659
18660       /* Peek at the next token.  */
18661       token = cp_lexer_peek_token (parser->lexer);
18662       /* If it's a `}', or EOF then we've seen all the members.  */
18663       if (token->type == CPP_CLOSE_BRACE
18664           || token->type == CPP_EOF
18665           || token->type == CPP_PRAGMA_EOL)
18666         break;
18667
18668       /* See if this token is a keyword.  */
18669       keyword = token->keyword;
18670       switch (keyword)
18671         {
18672         case RID_PUBLIC:
18673         case RID_PROTECTED:
18674         case RID_PRIVATE:
18675           /* Consume the access-specifier.  */
18676           cp_lexer_consume_token (parser->lexer);
18677           /* Remember which access-specifier is active.  */
18678           current_access_specifier = token->u.value;
18679           /* Look for the `:'.  */
18680           cp_parser_require (parser, CPP_COLON, RT_COLON);
18681           break;
18682
18683         default:
18684           /* Accept #pragmas at class scope.  */
18685           if (token->type == CPP_PRAGMA)
18686             {
18687               cp_parser_pragma (parser, pragma_external);
18688               break;
18689             }
18690
18691           /* Otherwise, the next construction must be a
18692              member-declaration.  */
18693           cp_parser_member_declaration (parser);
18694         }
18695     }
18696 }
18697
18698 /* Parse a member-declaration.
18699
18700    member-declaration:
18701      decl-specifier-seq [opt] member-declarator-list [opt] ;
18702      function-definition ; [opt]
18703      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18704      using-declaration
18705      template-declaration
18706      alias-declaration
18707
18708    member-declarator-list:
18709      member-declarator
18710      member-declarator-list , member-declarator
18711
18712    member-declarator:
18713      declarator pure-specifier [opt]
18714      declarator constant-initializer [opt]
18715      identifier [opt] : constant-expression
18716
18717    GNU Extensions:
18718
18719    member-declaration:
18720      __extension__ member-declaration
18721
18722    member-declarator:
18723      declarator attributes [opt] pure-specifier [opt]
18724      declarator attributes [opt] constant-initializer [opt]
18725      identifier [opt] attributes [opt] : constant-expression  
18726
18727    C++0x Extensions:
18728
18729    member-declaration:
18730      static_assert-declaration  */
18731
18732 static void
18733 cp_parser_member_declaration (cp_parser* parser)
18734 {
18735   cp_decl_specifier_seq decl_specifiers;
18736   tree prefix_attributes;
18737   tree decl;
18738   int declares_class_or_enum;
18739   bool friend_p;
18740   cp_token *token = NULL;
18741   cp_token *decl_spec_token_start = NULL;
18742   cp_token *initializer_token_start = NULL;
18743   int saved_pedantic;
18744   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18745
18746   /* Check for the `__extension__' keyword.  */
18747   if (cp_parser_extension_opt (parser, &saved_pedantic))
18748     {
18749       /* Recurse.  */
18750       cp_parser_member_declaration (parser);
18751       /* Restore the old value of the PEDANTIC flag.  */
18752       pedantic = saved_pedantic;
18753
18754       return;
18755     }
18756
18757   /* Check for a template-declaration.  */
18758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18759     {
18760       /* An explicit specialization here is an error condition, and we
18761          expect the specialization handler to detect and report this.  */
18762       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18763           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18764         cp_parser_explicit_specialization (parser);
18765       else
18766         cp_parser_template_declaration (parser, /*member_p=*/true);
18767
18768       return;
18769     }
18770
18771   /* Check for a using-declaration.  */
18772   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18773     {
18774       if (cxx_dialect < cxx0x)
18775         {
18776           /* Parse the using-declaration.  */
18777           cp_parser_using_declaration (parser,
18778                                        /*access_declaration_p=*/false);
18779           return;
18780         }
18781       else
18782         {
18783           tree decl;
18784           cp_parser_parse_tentatively (parser);
18785           decl = cp_parser_alias_declaration (parser);
18786           if (cp_parser_parse_definitely (parser))
18787             finish_member_declaration (decl);
18788           else
18789             cp_parser_using_declaration (parser,
18790                                          /*access_declaration_p=*/false);
18791           return;
18792         }
18793     }
18794
18795   /* Check for @defs.  */
18796   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18797     {
18798       tree ivar, member;
18799       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18800       ivar = ivar_chains;
18801       while (ivar)
18802         {
18803           member = ivar;
18804           ivar = TREE_CHAIN (member);
18805           TREE_CHAIN (member) = NULL_TREE;
18806           finish_member_declaration (member);
18807         }
18808       return;
18809     }
18810
18811   /* If the next token is `static_assert' we have a static assertion.  */
18812   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18813     {
18814       cp_parser_static_assert (parser, /*member_p=*/true);
18815       return;
18816     }
18817
18818   parser->colon_corrects_to_scope_p = false;
18819
18820   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18821       goto out;
18822
18823   /* Parse the decl-specifier-seq.  */
18824   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18825   cp_parser_decl_specifier_seq (parser,
18826                                 CP_PARSER_FLAGS_OPTIONAL,
18827                                 &decl_specifiers,
18828                                 &declares_class_or_enum);
18829   prefix_attributes = decl_specifiers.attributes;
18830   decl_specifiers.attributes = NULL_TREE;
18831   /* Check for an invalid type-name.  */
18832   if (!decl_specifiers.any_type_specifiers_p
18833       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18834     goto out;
18835   /* If there is no declarator, then the decl-specifier-seq should
18836      specify a type.  */
18837   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18838     {
18839       /* If there was no decl-specifier-seq, and the next token is a
18840          `;', then we have something like:
18841
18842            struct S { ; };
18843
18844          [class.mem]
18845
18846          Each member-declaration shall declare at least one member
18847          name of the class.  */
18848       if (!decl_specifiers.any_specifiers_p)
18849         {
18850           cp_token *token = cp_lexer_peek_token (parser->lexer);
18851           if (!in_system_header_at (token->location))
18852             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18853         }
18854       else
18855         {
18856           tree type;
18857
18858           /* See if this declaration is a friend.  */
18859           friend_p = cp_parser_friend_p (&decl_specifiers);
18860           /* If there were decl-specifiers, check to see if there was
18861              a class-declaration.  */
18862           type = check_tag_decl (&decl_specifiers);
18863           /* Nested classes have already been added to the class, but
18864              a `friend' needs to be explicitly registered.  */
18865           if (friend_p)
18866             {
18867               /* If the `friend' keyword was present, the friend must
18868                  be introduced with a class-key.  */
18869                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18870                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18871                           "in C++03 a class-key must be used "
18872                           "when declaring a friend");
18873                /* In this case:
18874
18875                     template <typename T> struct A {
18876                       friend struct A<T>::B;
18877                     };
18878
18879                   A<T>::B will be represented by a TYPENAME_TYPE, and
18880                   therefore not recognized by check_tag_decl.  */
18881                if (!type)
18882                  {
18883                    type = decl_specifiers.type;
18884                    if (type && TREE_CODE (type) == TYPE_DECL)
18885                      type = TREE_TYPE (type);
18886                  }
18887                if (!type || !TYPE_P (type))
18888                  error_at (decl_spec_token_start->location,
18889                            "friend declaration does not name a class or "
18890                            "function");
18891                else
18892                  make_friend_class (current_class_type, type,
18893                                     /*complain=*/true);
18894             }
18895           /* If there is no TYPE, an error message will already have
18896              been issued.  */
18897           else if (!type || type == error_mark_node)
18898             ;
18899           /* An anonymous aggregate has to be handled specially; such
18900              a declaration really declares a data member (with a
18901              particular type), as opposed to a nested class.  */
18902           else if (ANON_AGGR_TYPE_P (type))
18903             {
18904               /* Remove constructors and such from TYPE, now that we
18905                  know it is an anonymous aggregate.  */
18906               fixup_anonymous_aggr (type);
18907               /* And make the corresponding data member.  */
18908               decl = build_decl (decl_spec_token_start->location,
18909                                  FIELD_DECL, NULL_TREE, type);
18910               /* Add it to the class.  */
18911               finish_member_declaration (decl);
18912             }
18913           else
18914             cp_parser_check_access_in_redeclaration
18915                                               (TYPE_NAME (type),
18916                                                decl_spec_token_start->location);
18917         }
18918     }
18919   else
18920     {
18921       bool assume_semicolon = false;
18922
18923       /* See if these declarations will be friends.  */
18924       friend_p = cp_parser_friend_p (&decl_specifiers);
18925
18926       /* Keep going until we hit the `;' at the end of the
18927          declaration.  */
18928       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18929         {
18930           tree attributes = NULL_TREE;
18931           tree first_attribute;
18932
18933           /* Peek at the next token.  */
18934           token = cp_lexer_peek_token (parser->lexer);
18935
18936           /* Check for a bitfield declaration.  */
18937           if (token->type == CPP_COLON
18938               || (token->type == CPP_NAME
18939                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18940                   == CPP_COLON))
18941             {
18942               tree identifier;
18943               tree width;
18944
18945               /* Get the name of the bitfield.  Note that we cannot just
18946                  check TOKEN here because it may have been invalidated by
18947                  the call to cp_lexer_peek_nth_token above.  */
18948               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18949                 identifier = cp_parser_identifier (parser);
18950               else
18951                 identifier = NULL_TREE;
18952
18953               /* Consume the `:' token.  */
18954               cp_lexer_consume_token (parser->lexer);
18955               /* Get the width of the bitfield.  */
18956               width
18957                 = cp_parser_constant_expression (parser,
18958                                                  /*allow_non_constant=*/false,
18959                                                  NULL);
18960
18961               /* Look for attributes that apply to the bitfield.  */
18962               attributes = cp_parser_attributes_opt (parser);
18963               /* Remember which attributes are prefix attributes and
18964                  which are not.  */
18965               first_attribute = attributes;
18966               /* Combine the attributes.  */
18967               attributes = chainon (prefix_attributes, attributes);
18968
18969               /* Create the bitfield declaration.  */
18970               decl = grokbitfield (identifier
18971                                    ? make_id_declarator (NULL_TREE,
18972                                                          identifier,
18973                                                          sfk_none)
18974                                    : NULL,
18975                                    &decl_specifiers,
18976                                    width,
18977                                    attributes);
18978             }
18979           else
18980             {
18981               cp_declarator *declarator;
18982               tree initializer;
18983               tree asm_specification;
18984               int ctor_dtor_or_conv_p;
18985
18986               /* Parse the declarator.  */
18987               declarator
18988                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18989                                         &ctor_dtor_or_conv_p,
18990                                         /*parenthesized_p=*/NULL,
18991                                         /*member_p=*/true);
18992
18993               /* If something went wrong parsing the declarator, make sure
18994                  that we at least consume some tokens.  */
18995               if (declarator == cp_error_declarator)
18996                 {
18997                   /* Skip to the end of the statement.  */
18998                   cp_parser_skip_to_end_of_statement (parser);
18999                   /* If the next token is not a semicolon, that is
19000                      probably because we just skipped over the body of
19001                      a function.  So, we consume a semicolon if
19002                      present, but do not issue an error message if it
19003                      is not present.  */
19004                   if (cp_lexer_next_token_is (parser->lexer,
19005                                               CPP_SEMICOLON))
19006                     cp_lexer_consume_token (parser->lexer);
19007                   goto out;
19008                 }
19009
19010               if (declares_class_or_enum & 2)
19011                 cp_parser_check_for_definition_in_return_type
19012                                             (declarator, decl_specifiers.type,
19013                                              decl_specifiers.type_location);
19014
19015               /* Look for an asm-specification.  */
19016               asm_specification = cp_parser_asm_specification_opt (parser);
19017               /* Look for attributes that apply to the declaration.  */
19018               attributes = cp_parser_attributes_opt (parser);
19019               /* Remember which attributes are prefix attributes and
19020                  which are not.  */
19021               first_attribute = attributes;
19022               /* Combine the attributes.  */
19023               attributes = chainon (prefix_attributes, attributes);
19024
19025               /* If it's an `=', then we have a constant-initializer or a
19026                  pure-specifier.  It is not correct to parse the
19027                  initializer before registering the member declaration
19028                  since the member declaration should be in scope while
19029                  its initializer is processed.  However, the rest of the
19030                  front end does not yet provide an interface that allows
19031                  us to handle this correctly.  */
19032               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19033                 {
19034                   /* In [class.mem]:
19035
19036                      A pure-specifier shall be used only in the declaration of
19037                      a virtual function.
19038
19039                      A member-declarator can contain a constant-initializer
19040                      only if it declares a static member of integral or
19041                      enumeration type.
19042
19043                      Therefore, if the DECLARATOR is for a function, we look
19044                      for a pure-specifier; otherwise, we look for a
19045                      constant-initializer.  When we call `grokfield', it will
19046                      perform more stringent semantics checks.  */
19047                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19048                   if (function_declarator_p (declarator)
19049                       || (decl_specifiers.type
19050                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19051                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19052                               == FUNCTION_TYPE)))
19053                     initializer = cp_parser_pure_specifier (parser);
19054                   else if (decl_specifiers.storage_class != sc_static)
19055                     initializer = cp_parser_save_nsdmi (parser);
19056                   else if (cxx_dialect >= cxx0x)
19057                     {
19058                       bool nonconst;
19059                       /* Don't require a constant rvalue in C++11, since we
19060                          might want a reference constant.  We'll enforce
19061                          constancy later.  */
19062                       cp_lexer_consume_token (parser->lexer);
19063                       /* Parse the initializer.  */
19064                       initializer = cp_parser_initializer_clause (parser,
19065                                                                   &nonconst);
19066                     }
19067                   else
19068                     /* Parse the initializer.  */
19069                     initializer = cp_parser_constant_initializer (parser);
19070                 }
19071               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19072                        && !function_declarator_p (declarator))
19073                 {
19074                   bool x;
19075                   if (decl_specifiers.storage_class != sc_static)
19076                     initializer = cp_parser_save_nsdmi (parser);
19077                   else
19078                     initializer = cp_parser_initializer (parser, &x, &x);
19079                 }
19080               /* Otherwise, there is no initializer.  */
19081               else
19082                 initializer = NULL_TREE;
19083
19084               /* See if we are probably looking at a function
19085                  definition.  We are certainly not looking at a
19086                  member-declarator.  Calling `grokfield' has
19087                  side-effects, so we must not do it unless we are sure
19088                  that we are looking at a member-declarator.  */
19089               if (cp_parser_token_starts_function_definition_p
19090                   (cp_lexer_peek_token (parser->lexer)))
19091                 {
19092                   /* The grammar does not allow a pure-specifier to be
19093                      used when a member function is defined.  (It is
19094                      possible that this fact is an oversight in the
19095                      standard, since a pure function may be defined
19096                      outside of the class-specifier.  */
19097                   if (initializer)
19098                     error_at (initializer_token_start->location,
19099                               "pure-specifier on function-definition");
19100                   decl = cp_parser_save_member_function_body (parser,
19101                                                               &decl_specifiers,
19102                                                               declarator,
19103                                                               attributes);
19104                   /* If the member was not a friend, declare it here.  */
19105                   if (!friend_p)
19106                     finish_member_declaration (decl);
19107                   /* Peek at the next token.  */
19108                   token = cp_lexer_peek_token (parser->lexer);
19109                   /* If the next token is a semicolon, consume it.  */
19110                   if (token->type == CPP_SEMICOLON)
19111                     cp_lexer_consume_token (parser->lexer);
19112                   goto out;
19113                 }
19114               else
19115                 if (declarator->kind == cdk_function)
19116                   declarator->id_loc = token->location;
19117                 /* Create the declaration.  */
19118                 decl = grokfield (declarator, &decl_specifiers,
19119                                   initializer, /*init_const_expr_p=*/true,
19120                                   asm_specification,
19121                                   attributes);
19122             }
19123
19124           /* Reset PREFIX_ATTRIBUTES.  */
19125           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19126             attributes = TREE_CHAIN (attributes);
19127           if (attributes)
19128             TREE_CHAIN (attributes) = NULL_TREE;
19129
19130           /* If there is any qualification still in effect, clear it
19131              now; we will be starting fresh with the next declarator.  */
19132           parser->scope = NULL_TREE;
19133           parser->qualifying_scope = NULL_TREE;
19134           parser->object_scope = NULL_TREE;
19135           /* If it's a `,', then there are more declarators.  */
19136           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19137             cp_lexer_consume_token (parser->lexer);
19138           /* If the next token isn't a `;', then we have a parse error.  */
19139           else if (cp_lexer_next_token_is_not (parser->lexer,
19140                                                CPP_SEMICOLON))
19141             {
19142               /* The next token might be a ways away from where the
19143                  actual semicolon is missing.  Find the previous token
19144                  and use that for our error position.  */
19145               cp_token *token = cp_lexer_previous_token (parser->lexer);
19146               error_at (token->location,
19147                         "expected %<;%> at end of member declaration");
19148
19149               /* Assume that the user meant to provide a semicolon.  If
19150                  we were to cp_parser_skip_to_end_of_statement, we might
19151                  skip to a semicolon inside a member function definition
19152                  and issue nonsensical error messages.  */
19153               assume_semicolon = true;
19154             }
19155
19156           if (decl)
19157             {
19158               /* Add DECL to the list of members.  */
19159               if (!friend_p)
19160                 finish_member_declaration (decl);
19161
19162               if (TREE_CODE (decl) == FUNCTION_DECL)
19163                 cp_parser_save_default_args (parser, decl);
19164               else if (TREE_CODE (decl) == FIELD_DECL
19165                        && !DECL_C_BIT_FIELD (decl)
19166                        && DECL_INITIAL (decl))
19167                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19168                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19169             }
19170
19171           if (assume_semicolon)
19172             goto out;
19173         }
19174     }
19175
19176   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19177  out:
19178   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19179 }
19180
19181 /* Parse a pure-specifier.
19182
19183    pure-specifier:
19184      = 0
19185
19186    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19187    Otherwise, ERROR_MARK_NODE is returned.  */
19188
19189 static tree
19190 cp_parser_pure_specifier (cp_parser* parser)
19191 {
19192   cp_token *token;
19193
19194   /* Look for the `=' token.  */
19195   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19196     return error_mark_node;
19197   /* Look for the `0' token.  */
19198   token = cp_lexer_peek_token (parser->lexer);
19199
19200   if (token->type == CPP_EOF
19201       || token->type == CPP_PRAGMA_EOL)
19202     return error_mark_node;
19203
19204   cp_lexer_consume_token (parser->lexer);
19205
19206   /* Accept = default or = delete in c++0x mode.  */
19207   if (token->keyword == RID_DEFAULT
19208       || token->keyword == RID_DELETE)
19209     {
19210       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19211       return token->u.value;
19212     }
19213
19214   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19215   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19216     {
19217       cp_parser_error (parser,
19218                        "invalid pure specifier (only %<= 0%> is allowed)");
19219       cp_parser_skip_to_end_of_statement (parser);
19220       return error_mark_node;
19221     }
19222   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19223     {
19224       error_at (token->location, "templates may not be %<virtual%>");
19225       return error_mark_node;
19226     }
19227
19228   return integer_zero_node;
19229 }
19230
19231 /* Parse a constant-initializer.
19232
19233    constant-initializer:
19234      = constant-expression
19235
19236    Returns a representation of the constant-expression.  */
19237
19238 static tree
19239 cp_parser_constant_initializer (cp_parser* parser)
19240 {
19241   /* Look for the `=' token.  */
19242   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19243     return error_mark_node;
19244
19245   /* It is invalid to write:
19246
19247        struct S { static const int i = { 7 }; };
19248
19249      */
19250   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19251     {
19252       cp_parser_error (parser,
19253                        "a brace-enclosed initializer is not allowed here");
19254       /* Consume the opening brace.  */
19255       cp_lexer_consume_token (parser->lexer);
19256       /* Skip the initializer.  */
19257       cp_parser_skip_to_closing_brace (parser);
19258       /* Look for the trailing `}'.  */
19259       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19260
19261       return error_mark_node;
19262     }
19263
19264   return cp_parser_constant_expression (parser,
19265                                         /*allow_non_constant=*/false,
19266                                         NULL);
19267 }
19268
19269 /* Derived classes [gram.class.derived] */
19270
19271 /* Parse a base-clause.
19272
19273    base-clause:
19274      : base-specifier-list
19275
19276    base-specifier-list:
19277      base-specifier ... [opt]
19278      base-specifier-list , base-specifier ... [opt]
19279
19280    Returns a TREE_LIST representing the base-classes, in the order in
19281    which they were declared.  The representation of each node is as
19282    described by cp_parser_base_specifier.
19283
19284    In the case that no bases are specified, this function will return
19285    NULL_TREE, not ERROR_MARK_NODE.  */
19286
19287 static tree
19288 cp_parser_base_clause (cp_parser* parser)
19289 {
19290   tree bases = NULL_TREE;
19291
19292   /* Look for the `:' that begins the list.  */
19293   cp_parser_require (parser, CPP_COLON, RT_COLON);
19294
19295   /* Scan the base-specifier-list.  */
19296   while (true)
19297     {
19298       cp_token *token;
19299       tree base;
19300       bool pack_expansion_p = false;
19301
19302       /* Look for the base-specifier.  */
19303       base = cp_parser_base_specifier (parser);
19304       /* Look for the (optional) ellipsis. */
19305       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19306         {
19307           /* Consume the `...'. */
19308           cp_lexer_consume_token (parser->lexer);
19309
19310           pack_expansion_p = true;
19311         }
19312
19313       /* Add BASE to the front of the list.  */
19314       if (base && base != error_mark_node)
19315         {
19316           if (pack_expansion_p)
19317             /* Make this a pack expansion type. */
19318             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19319
19320           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19321             {
19322               TREE_CHAIN (base) = bases;
19323               bases = base;
19324             }
19325         }
19326       /* Peek at the next token.  */
19327       token = cp_lexer_peek_token (parser->lexer);
19328       /* If it's not a comma, then the list is complete.  */
19329       if (token->type != CPP_COMMA)
19330         break;
19331       /* Consume the `,'.  */
19332       cp_lexer_consume_token (parser->lexer);
19333     }
19334
19335   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19336      base class had a qualified name.  However, the next name that
19337      appears is certainly not qualified.  */
19338   parser->scope = NULL_TREE;
19339   parser->qualifying_scope = NULL_TREE;
19340   parser->object_scope = NULL_TREE;
19341
19342   return nreverse (bases);
19343 }
19344
19345 /* Parse a base-specifier.
19346
19347    base-specifier:
19348      :: [opt] nested-name-specifier [opt] class-name
19349      virtual access-specifier [opt] :: [opt] nested-name-specifier
19350        [opt] class-name
19351      access-specifier virtual [opt] :: [opt] nested-name-specifier
19352        [opt] class-name
19353
19354    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19355    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19356    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19357    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19358
19359 static tree
19360 cp_parser_base_specifier (cp_parser* parser)
19361 {
19362   cp_token *token;
19363   bool done = false;
19364   bool virtual_p = false;
19365   bool duplicate_virtual_error_issued_p = false;
19366   bool duplicate_access_error_issued_p = false;
19367   bool class_scope_p, template_p;
19368   tree access = access_default_node;
19369   tree type;
19370
19371   /* Process the optional `virtual' and `access-specifier'.  */
19372   while (!done)
19373     {
19374       /* Peek at the next token.  */
19375       token = cp_lexer_peek_token (parser->lexer);
19376       /* Process `virtual'.  */
19377       switch (token->keyword)
19378         {
19379         case RID_VIRTUAL:
19380           /* If `virtual' appears more than once, issue an error.  */
19381           if (virtual_p && !duplicate_virtual_error_issued_p)
19382             {
19383               cp_parser_error (parser,
19384                                "%<virtual%> specified more than once in base-specified");
19385               duplicate_virtual_error_issued_p = true;
19386             }
19387
19388           virtual_p = true;
19389
19390           /* Consume the `virtual' token.  */
19391           cp_lexer_consume_token (parser->lexer);
19392
19393           break;
19394
19395         case RID_PUBLIC:
19396         case RID_PROTECTED:
19397         case RID_PRIVATE:
19398           /* If more than one access specifier appears, issue an
19399              error.  */
19400           if (access != access_default_node
19401               && !duplicate_access_error_issued_p)
19402             {
19403               cp_parser_error (parser,
19404                                "more than one access specifier in base-specified");
19405               duplicate_access_error_issued_p = true;
19406             }
19407
19408           access = ridpointers[(int) token->keyword];
19409
19410           /* Consume the access-specifier.  */
19411           cp_lexer_consume_token (parser->lexer);
19412
19413           break;
19414
19415         default:
19416           done = true;
19417           break;
19418         }
19419     }
19420   /* It is not uncommon to see programs mechanically, erroneously, use
19421      the 'typename' keyword to denote (dependent) qualified types
19422      as base classes.  */
19423   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19424     {
19425       token = cp_lexer_peek_token (parser->lexer);
19426       if (!processing_template_decl)
19427         error_at (token->location,
19428                   "keyword %<typename%> not allowed outside of templates");
19429       else
19430         error_at (token->location,
19431                   "keyword %<typename%> not allowed in this context "
19432                   "(the base class is implicitly a type)");
19433       cp_lexer_consume_token (parser->lexer);
19434     }
19435
19436   /* Look for the optional `::' operator.  */
19437   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19438   /* Look for the nested-name-specifier.  The simplest way to
19439      implement:
19440
19441        [temp.res]
19442
19443        The keyword `typename' is not permitted in a base-specifier or
19444        mem-initializer; in these contexts a qualified name that
19445        depends on a template-parameter is implicitly assumed to be a
19446        type name.
19447
19448      is to pretend that we have seen the `typename' keyword at this
19449      point.  */
19450   cp_parser_nested_name_specifier_opt (parser,
19451                                        /*typename_keyword_p=*/true,
19452                                        /*check_dependency_p=*/true,
19453                                        typename_type,
19454                                        /*is_declaration=*/true);
19455   /* If the base class is given by a qualified name, assume that names
19456      we see are type names or templates, as appropriate.  */
19457   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19458   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19459
19460   if (!parser->scope
19461       && cp_lexer_next_token_is_decltype (parser->lexer))
19462     /* DR 950 allows decltype as a base-specifier.  */
19463     type = cp_parser_decltype (parser);
19464   else
19465     {
19466       /* Otherwise, look for the class-name.  */
19467       type = cp_parser_class_name (parser,
19468                                    class_scope_p,
19469                                    template_p,
19470                                    typename_type,
19471                                    /*check_dependency_p=*/true,
19472                                    /*class_head_p=*/false,
19473                                    /*is_declaration=*/true);
19474       type = TREE_TYPE (type);
19475     }
19476
19477   if (type == error_mark_node)
19478     return error_mark_node;
19479
19480   return finish_base_specifier (type, access, virtual_p);
19481 }
19482
19483 /* Exception handling [gram.exception] */
19484
19485 /* Parse an (optional) noexcept-specification.
19486
19487    noexcept-specification:
19488      noexcept ( constant-expression ) [opt]
19489
19490    If no noexcept-specification is present, returns NULL_TREE.
19491    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19492    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19493    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19494    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19495    in which case a boolean condition is returned instead.  */
19496
19497 static tree
19498 cp_parser_noexcept_specification_opt (cp_parser* parser,
19499                                       bool require_constexpr,
19500                                       bool* consumed_expr,
19501                                       bool return_cond)
19502 {
19503   cp_token *token;
19504   const char *saved_message;
19505
19506   /* Peek at the next token.  */
19507   token = cp_lexer_peek_token (parser->lexer);
19508
19509   /* Is it a noexcept-specification?  */
19510   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19511     {
19512       tree expr;
19513       cp_lexer_consume_token (parser->lexer);
19514
19515       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19516         {
19517           cp_lexer_consume_token (parser->lexer);
19518
19519           if (require_constexpr)
19520             {
19521               /* Types may not be defined in an exception-specification.  */
19522               saved_message = parser->type_definition_forbidden_message;
19523               parser->type_definition_forbidden_message
19524               = G_("types may not be defined in an exception-specification");
19525
19526               expr = cp_parser_constant_expression (parser, false, NULL);
19527
19528               /* Restore the saved message.  */
19529               parser->type_definition_forbidden_message = saved_message;
19530             }
19531           else
19532             {
19533               expr = cp_parser_expression (parser, false, NULL);
19534               *consumed_expr = true;
19535             }
19536
19537           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19538         }
19539       else
19540         {
19541           expr = boolean_true_node;
19542           if (!require_constexpr)
19543             *consumed_expr = false;
19544         }
19545
19546       /* We cannot build a noexcept-spec right away because this will check
19547          that expr is a constexpr.  */
19548       if (!return_cond)
19549         return build_noexcept_spec (expr, tf_warning_or_error);
19550       else
19551         return expr;
19552     }
19553   else
19554     return NULL_TREE;
19555 }
19556
19557 /* Parse an (optional) exception-specification.
19558
19559    exception-specification:
19560      throw ( type-id-list [opt] )
19561
19562    Returns a TREE_LIST representing the exception-specification.  The
19563    TREE_VALUE of each node is a type.  */
19564
19565 static tree
19566 cp_parser_exception_specification_opt (cp_parser* parser)
19567 {
19568   cp_token *token;
19569   tree type_id_list;
19570   const char *saved_message;
19571
19572   /* Peek at the next token.  */
19573   token = cp_lexer_peek_token (parser->lexer);
19574
19575   /* Is it a noexcept-specification?  */
19576   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19577                                                       false);
19578   if (type_id_list != NULL_TREE)
19579     return type_id_list;
19580
19581   /* If it's not `throw', then there's no exception-specification.  */
19582   if (!cp_parser_is_keyword (token, RID_THROW))
19583     return NULL_TREE;
19584
19585 #if 0
19586   /* Enable this once a lot of code has transitioned to noexcept?  */
19587   if (cxx_dialect == cxx0x && !in_system_header)
19588     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19589              "deprecated in C++0x; use %<noexcept%> instead");
19590 #endif
19591
19592   /* Consume the `throw'.  */
19593   cp_lexer_consume_token (parser->lexer);
19594
19595   /* Look for the `('.  */
19596   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19597
19598   /* Peek at the next token.  */
19599   token = cp_lexer_peek_token (parser->lexer);
19600   /* If it's not a `)', then there is a type-id-list.  */
19601   if (token->type != CPP_CLOSE_PAREN)
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       /* Parse the type-id-list.  */
19608       type_id_list = cp_parser_type_id_list (parser);
19609       /* Restore the saved message.  */
19610       parser->type_definition_forbidden_message = saved_message;
19611     }
19612   else
19613     type_id_list = empty_except_spec;
19614
19615   /* Look for the `)'.  */
19616   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19617
19618   return type_id_list;
19619 }
19620
19621 /* Parse an (optional) type-id-list.
19622
19623    type-id-list:
19624      type-id ... [opt]
19625      type-id-list , type-id ... [opt]
19626
19627    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19628    in the order that the types were presented.  */
19629
19630 static tree
19631 cp_parser_type_id_list (cp_parser* parser)
19632 {
19633   tree types = NULL_TREE;
19634
19635   while (true)
19636     {
19637       cp_token *token;
19638       tree type;
19639
19640       /* Get the next type-id.  */
19641       type = cp_parser_type_id (parser);
19642       /* Parse the optional ellipsis. */
19643       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19644         {
19645           /* Consume the `...'. */
19646           cp_lexer_consume_token (parser->lexer);
19647
19648           /* Turn the type into a pack expansion expression. */
19649           type = make_pack_expansion (type);
19650         }
19651       /* Add it to the list.  */
19652       types = add_exception_specifier (types, type, /*complain=*/1);
19653       /* Peek at the next token.  */
19654       token = cp_lexer_peek_token (parser->lexer);
19655       /* If it is not a `,', we are done.  */
19656       if (token->type != CPP_COMMA)
19657         break;
19658       /* Consume the `,'.  */
19659       cp_lexer_consume_token (parser->lexer);
19660     }
19661
19662   return nreverse (types);
19663 }
19664
19665 /* Parse a try-block.
19666
19667    try-block:
19668      try compound-statement handler-seq  */
19669
19670 static tree
19671 cp_parser_try_block (cp_parser* parser)
19672 {
19673   tree try_block;
19674
19675   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19676   try_block = begin_try_block ();
19677   cp_parser_compound_statement (parser, NULL, true, false);
19678   finish_try_block (try_block);
19679   cp_parser_handler_seq (parser);
19680   finish_handler_sequence (try_block);
19681
19682   return try_block;
19683 }
19684
19685 /* Parse a function-try-block.
19686
19687    function-try-block:
19688      try ctor-initializer [opt] function-body handler-seq  */
19689
19690 static bool
19691 cp_parser_function_try_block (cp_parser* parser)
19692 {
19693   tree compound_stmt;
19694   tree try_block;
19695   bool ctor_initializer_p;
19696
19697   /* Look for the `try' keyword.  */
19698   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19699     return false;
19700   /* Let the rest of the front end know where we are.  */
19701   try_block = begin_function_try_block (&compound_stmt);
19702   /* Parse the function-body.  */
19703   ctor_initializer_p
19704     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19705   /* We're done with the `try' part.  */
19706   finish_function_try_block (try_block);
19707   /* Parse the handlers.  */
19708   cp_parser_handler_seq (parser);
19709   /* We're done with the handlers.  */
19710   finish_function_handler_sequence (try_block, compound_stmt);
19711
19712   return ctor_initializer_p;
19713 }
19714
19715 /* Parse a handler-seq.
19716
19717    handler-seq:
19718      handler handler-seq [opt]  */
19719
19720 static void
19721 cp_parser_handler_seq (cp_parser* parser)
19722 {
19723   while (true)
19724     {
19725       cp_token *token;
19726
19727       /* Parse the handler.  */
19728       cp_parser_handler (parser);
19729       /* Peek at the next token.  */
19730       token = cp_lexer_peek_token (parser->lexer);
19731       /* If it's not `catch' then there are no more handlers.  */
19732       if (!cp_parser_is_keyword (token, RID_CATCH))
19733         break;
19734     }
19735 }
19736
19737 /* Parse a handler.
19738
19739    handler:
19740      catch ( exception-declaration ) compound-statement  */
19741
19742 static void
19743 cp_parser_handler (cp_parser* parser)
19744 {
19745   tree handler;
19746   tree declaration;
19747
19748   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19749   handler = begin_handler ();
19750   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19751   declaration = cp_parser_exception_declaration (parser);
19752   finish_handler_parms (declaration, handler);
19753   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19754   cp_parser_compound_statement (parser, NULL, false, false);
19755   finish_handler (handler);
19756 }
19757
19758 /* Parse an exception-declaration.
19759
19760    exception-declaration:
19761      type-specifier-seq declarator
19762      type-specifier-seq abstract-declarator
19763      type-specifier-seq
19764      ...
19765
19766    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19767    ellipsis variant is used.  */
19768
19769 static tree
19770 cp_parser_exception_declaration (cp_parser* parser)
19771 {
19772   cp_decl_specifier_seq type_specifiers;
19773   cp_declarator *declarator;
19774   const char *saved_message;
19775
19776   /* If it's an ellipsis, it's easy to handle.  */
19777   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19778     {
19779       /* Consume the `...' token.  */
19780       cp_lexer_consume_token (parser->lexer);
19781       return NULL_TREE;
19782     }
19783
19784   /* Types may not be defined in exception-declarations.  */
19785   saved_message = parser->type_definition_forbidden_message;
19786   parser->type_definition_forbidden_message
19787     = G_("types may not be defined in exception-declarations");
19788
19789   /* Parse the type-specifier-seq.  */
19790   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19791                                 /*is_trailing_return=*/false,
19792                                 &type_specifiers);
19793   /* If it's a `)', then there is no declarator.  */
19794   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19795     declarator = NULL;
19796   else
19797     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19798                                        /*ctor_dtor_or_conv_p=*/NULL,
19799                                        /*parenthesized_p=*/NULL,
19800                                        /*member_p=*/false);
19801
19802   /* Restore the saved message.  */
19803   parser->type_definition_forbidden_message = saved_message;
19804
19805   if (!type_specifiers.any_specifiers_p)
19806     return error_mark_node;
19807
19808   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19809 }
19810
19811 /* Parse a throw-expression.
19812
19813    throw-expression:
19814      throw assignment-expression [opt]
19815
19816    Returns a THROW_EXPR representing the throw-expression.  */
19817
19818 static tree
19819 cp_parser_throw_expression (cp_parser* parser)
19820 {
19821   tree expression;
19822   cp_token* token;
19823
19824   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19825   token = cp_lexer_peek_token (parser->lexer);
19826   /* Figure out whether or not there is an assignment-expression
19827      following the "throw" keyword.  */
19828   if (token->type == CPP_COMMA
19829       || token->type == CPP_SEMICOLON
19830       || token->type == CPP_CLOSE_PAREN
19831       || token->type == CPP_CLOSE_SQUARE
19832       || token->type == CPP_CLOSE_BRACE
19833       || token->type == CPP_COLON)
19834     expression = NULL_TREE;
19835   else
19836     expression = cp_parser_assignment_expression (parser,
19837                                                   /*cast_p=*/false, NULL);
19838
19839   return build_throw (expression);
19840 }
19841
19842 /* GNU Extensions */
19843
19844 /* Parse an (optional) asm-specification.
19845
19846    asm-specification:
19847      asm ( string-literal )
19848
19849    If the asm-specification is present, returns a STRING_CST
19850    corresponding to the string-literal.  Otherwise, returns
19851    NULL_TREE.  */
19852
19853 static tree
19854 cp_parser_asm_specification_opt (cp_parser* parser)
19855 {
19856   cp_token *token;
19857   tree asm_specification;
19858
19859   /* Peek at the next token.  */
19860   token = cp_lexer_peek_token (parser->lexer);
19861   /* If the next token isn't the `asm' keyword, then there's no
19862      asm-specification.  */
19863   if (!cp_parser_is_keyword (token, RID_ASM))
19864     return NULL_TREE;
19865
19866   /* Consume the `asm' token.  */
19867   cp_lexer_consume_token (parser->lexer);
19868   /* Look for the `('.  */
19869   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19870
19871   /* Look for the string-literal.  */
19872   asm_specification = cp_parser_string_literal (parser, false, false);
19873
19874   /* Look for the `)'.  */
19875   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19876
19877   return asm_specification;
19878 }
19879
19880 /* Parse an asm-operand-list.
19881
19882    asm-operand-list:
19883      asm-operand
19884      asm-operand-list , asm-operand
19885
19886    asm-operand:
19887      string-literal ( expression )
19888      [ string-literal ] string-literal ( expression )
19889
19890    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19891    each node is the expression.  The TREE_PURPOSE is itself a
19892    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19893    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19894    is a STRING_CST for the string literal before the parenthesis. Returns
19895    ERROR_MARK_NODE if any of the operands are invalid.  */
19896
19897 static tree
19898 cp_parser_asm_operand_list (cp_parser* parser)
19899 {
19900   tree asm_operands = NULL_TREE;
19901   bool invalid_operands = false;
19902
19903   while (true)
19904     {
19905       tree string_literal;
19906       tree expression;
19907       tree name;
19908
19909       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19910         {
19911           /* Consume the `[' token.  */
19912           cp_lexer_consume_token (parser->lexer);
19913           /* Read the operand name.  */
19914           name = cp_parser_identifier (parser);
19915           if (name != error_mark_node)
19916             name = build_string (IDENTIFIER_LENGTH (name),
19917                                  IDENTIFIER_POINTER (name));
19918           /* Look for the closing `]'.  */
19919           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19920         }
19921       else
19922         name = NULL_TREE;
19923       /* Look for the string-literal.  */
19924       string_literal = cp_parser_string_literal (parser, false, false);
19925
19926       /* Look for the `('.  */
19927       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19928       /* Parse the expression.  */
19929       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19930       /* Look for the `)'.  */
19931       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19932
19933       if (name == error_mark_node 
19934           || string_literal == error_mark_node 
19935           || expression == error_mark_node)
19936         invalid_operands = true;
19937
19938       /* Add this operand to the list.  */
19939       asm_operands = tree_cons (build_tree_list (name, string_literal),
19940                                 expression,
19941                                 asm_operands);
19942       /* If the next token is not a `,', there are no more
19943          operands.  */
19944       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19945         break;
19946       /* Consume the `,'.  */
19947       cp_lexer_consume_token (parser->lexer);
19948     }
19949
19950   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19951 }
19952
19953 /* Parse an asm-clobber-list.
19954
19955    asm-clobber-list:
19956      string-literal
19957      asm-clobber-list , string-literal
19958
19959    Returns a TREE_LIST, indicating the clobbers in the order that they
19960    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19961
19962 static tree
19963 cp_parser_asm_clobber_list (cp_parser* parser)
19964 {
19965   tree clobbers = NULL_TREE;
19966
19967   while (true)
19968     {
19969       tree string_literal;
19970
19971       /* Look for the string literal.  */
19972       string_literal = cp_parser_string_literal (parser, false, false);
19973       /* Add it to the list.  */
19974       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19975       /* If the next token is not a `,', then the list is
19976          complete.  */
19977       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19978         break;
19979       /* Consume the `,' token.  */
19980       cp_lexer_consume_token (parser->lexer);
19981     }
19982
19983   return clobbers;
19984 }
19985
19986 /* Parse an asm-label-list.
19987
19988    asm-label-list:
19989      identifier
19990      asm-label-list , identifier
19991
19992    Returns a TREE_LIST, indicating the labels in the order that they
19993    appeared.  The TREE_VALUE of each node is a label.  */
19994
19995 static tree
19996 cp_parser_asm_label_list (cp_parser* parser)
19997 {
19998   tree labels = NULL_TREE;
19999
20000   while (true)
20001     {
20002       tree identifier, label, name;
20003
20004       /* Look for the identifier.  */
20005       identifier = cp_parser_identifier (parser);
20006       if (!error_operand_p (identifier))
20007         {
20008           label = lookup_label (identifier);
20009           if (TREE_CODE (label) == LABEL_DECL)
20010             {
20011               TREE_USED (label) = 1;
20012               check_goto (label);
20013               name = build_string (IDENTIFIER_LENGTH (identifier),
20014                                    IDENTIFIER_POINTER (identifier));
20015               labels = tree_cons (name, label, labels);
20016             }
20017         }
20018       /* If the next token is not a `,', then the list is
20019          complete.  */
20020       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20021         break;
20022       /* Consume the `,' token.  */
20023       cp_lexer_consume_token (parser->lexer);
20024     }
20025
20026   return nreverse (labels);
20027 }
20028
20029 /* Parse an (optional) series of attributes.
20030
20031    attributes:
20032      attributes attribute
20033
20034    attribute:
20035      __attribute__ (( attribute-list [opt] ))
20036
20037    The return value is as for cp_parser_attribute_list.  */
20038
20039 static tree
20040 cp_parser_attributes_opt (cp_parser* parser)
20041 {
20042   tree attributes = NULL_TREE;
20043
20044   while (true)
20045     {
20046       cp_token *token;
20047       tree attribute_list;
20048
20049       /* Peek at the next token.  */
20050       token = cp_lexer_peek_token (parser->lexer);
20051       /* If it's not `__attribute__', then we're done.  */
20052       if (token->keyword != RID_ATTRIBUTE)
20053         break;
20054
20055       /* Consume the `__attribute__' keyword.  */
20056       cp_lexer_consume_token (parser->lexer);
20057       /* Look for the two `(' tokens.  */
20058       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20059       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20060
20061       /* Peek at the next token.  */
20062       token = cp_lexer_peek_token (parser->lexer);
20063       if (token->type != CPP_CLOSE_PAREN)
20064         /* Parse the attribute-list.  */
20065         attribute_list = cp_parser_attribute_list (parser);
20066       else
20067         /* If the next token is a `)', then there is no attribute
20068            list.  */
20069         attribute_list = NULL;
20070
20071       /* Look for the two `)' tokens.  */
20072       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20073       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20074
20075       /* Add these new attributes to the list.  */
20076       attributes = chainon (attributes, attribute_list);
20077     }
20078
20079   return attributes;
20080 }
20081
20082 /* Parse an attribute-list.
20083
20084    attribute-list:
20085      attribute
20086      attribute-list , attribute
20087
20088    attribute:
20089      identifier
20090      identifier ( identifier )
20091      identifier ( identifier , expression-list )
20092      identifier ( expression-list )
20093
20094    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20095    to an attribute.  The TREE_PURPOSE of each node is the identifier
20096    indicating which attribute is in use.  The TREE_VALUE represents
20097    the arguments, if any.  */
20098
20099 static tree
20100 cp_parser_attribute_list (cp_parser* parser)
20101 {
20102   tree attribute_list = NULL_TREE;
20103   bool save_translate_strings_p = parser->translate_strings_p;
20104
20105   parser->translate_strings_p = false;
20106   while (true)
20107     {
20108       cp_token *token;
20109       tree identifier;
20110       tree attribute;
20111
20112       /* Look for the identifier.  We also allow keywords here; for
20113          example `__attribute__ ((const))' is legal.  */
20114       token = cp_lexer_peek_token (parser->lexer);
20115       if (token->type == CPP_NAME
20116           || token->type == CPP_KEYWORD)
20117         {
20118           tree arguments = NULL_TREE;
20119
20120           /* Consume the token.  */
20121           token = cp_lexer_consume_token (parser->lexer);
20122
20123           /* Save away the identifier that indicates which attribute
20124              this is.  */
20125           identifier = (token->type == CPP_KEYWORD) 
20126             /* For keywords, use the canonical spelling, not the
20127                parsed identifier.  */
20128             ? ridpointers[(int) token->keyword]
20129             : token->u.value;
20130           
20131           attribute = build_tree_list (identifier, NULL_TREE);
20132
20133           /* Peek at the next token.  */
20134           token = cp_lexer_peek_token (parser->lexer);
20135           /* If it's an `(', then parse the attribute arguments.  */
20136           if (token->type == CPP_OPEN_PAREN)
20137             {
20138               VEC(tree,gc) *vec;
20139               int attr_flag = (attribute_takes_identifier_p (identifier)
20140                                ? id_attr : normal_attr);
20141               vec = cp_parser_parenthesized_expression_list
20142                     (parser, attr_flag, /*cast_p=*/false,
20143                      /*allow_expansion_p=*/false,
20144                      /*non_constant_p=*/NULL);
20145               if (vec == NULL)
20146                 arguments = error_mark_node;
20147               else
20148                 {
20149                   arguments = build_tree_list_vec (vec);
20150                   release_tree_vector (vec);
20151                 }
20152               /* Save the arguments away.  */
20153               TREE_VALUE (attribute) = arguments;
20154             }
20155
20156           if (arguments != error_mark_node)
20157             {
20158               /* Add this attribute to the list.  */
20159               TREE_CHAIN (attribute) = attribute_list;
20160               attribute_list = attribute;
20161             }
20162
20163           token = cp_lexer_peek_token (parser->lexer);
20164         }
20165       /* Now, look for more attributes.  If the next token isn't a
20166          `,', we're done.  */
20167       if (token->type != CPP_COMMA)
20168         break;
20169
20170       /* Consume the comma and keep going.  */
20171       cp_lexer_consume_token (parser->lexer);
20172     }
20173   parser->translate_strings_p = save_translate_strings_p;
20174
20175   /* We built up the list in reverse order.  */
20176   return nreverse (attribute_list);
20177 }
20178
20179 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20180    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20181    current value of the PEDANTIC flag, regardless of whether or not
20182    the `__extension__' keyword is present.  The caller is responsible
20183    for restoring the value of the PEDANTIC flag.  */
20184
20185 static bool
20186 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20187 {
20188   /* Save the old value of the PEDANTIC flag.  */
20189   *saved_pedantic = pedantic;
20190
20191   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20192     {
20193       /* Consume the `__extension__' token.  */
20194       cp_lexer_consume_token (parser->lexer);
20195       /* We're not being pedantic while the `__extension__' keyword is
20196          in effect.  */
20197       pedantic = 0;
20198
20199       return true;
20200     }
20201
20202   return false;
20203 }
20204
20205 /* Parse a label declaration.
20206
20207    label-declaration:
20208      __label__ label-declarator-seq ;
20209
20210    label-declarator-seq:
20211      identifier , label-declarator-seq
20212      identifier  */
20213
20214 static void
20215 cp_parser_label_declaration (cp_parser* parser)
20216 {
20217   /* Look for the `__label__' keyword.  */
20218   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20219
20220   while (true)
20221     {
20222       tree identifier;
20223
20224       /* Look for an identifier.  */
20225       identifier = cp_parser_identifier (parser);
20226       /* If we failed, stop.  */
20227       if (identifier == error_mark_node)
20228         break;
20229       /* Declare it as a label.  */
20230       finish_label_decl (identifier);
20231       /* If the next token is a `;', stop.  */
20232       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20233         break;
20234       /* Look for the `,' separating the label declarations.  */
20235       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20236     }
20237
20238   /* Look for the final `;'.  */
20239   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20240 }
20241
20242 /* Support Functions */
20243
20244 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20245    NAME should have one of the representations used for an
20246    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20247    is returned.  If PARSER->SCOPE is a dependent type, then a
20248    SCOPE_REF is returned.
20249
20250    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20251    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20252    was formed.  Abstractly, such entities should not be passed to this
20253    function, because they do not need to be looked up, but it is
20254    simpler to check for this special case here, rather than at the
20255    call-sites.
20256
20257    In cases not explicitly covered above, this function returns a
20258    DECL, OVERLOAD, or baselink representing the result of the lookup.
20259    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20260    is returned.
20261
20262    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20263    (e.g., "struct") that was used.  In that case bindings that do not
20264    refer to types are ignored.
20265
20266    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20267    ignored.
20268
20269    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20270    are ignored.
20271
20272    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20273    types.
20274
20275    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20276    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20277    NULL_TREE otherwise.  */
20278
20279 static tree
20280 cp_parser_lookup_name (cp_parser *parser, tree name,
20281                        enum tag_types tag_type,
20282                        bool is_template,
20283                        bool is_namespace,
20284                        bool check_dependency,
20285                        tree *ambiguous_decls,
20286                        location_t name_location)
20287 {
20288   int flags = 0;
20289   tree decl;
20290   tree object_type = parser->context->object_type;
20291
20292   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20293     flags |= LOOKUP_COMPLAIN;
20294
20295   /* Assume that the lookup will be unambiguous.  */
20296   if (ambiguous_decls)
20297     *ambiguous_decls = NULL_TREE;
20298
20299   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20300      no longer valid.  Note that if we are parsing tentatively, and
20301      the parse fails, OBJECT_TYPE will be automatically restored.  */
20302   parser->context->object_type = NULL_TREE;
20303
20304   if (name == error_mark_node)
20305     return error_mark_node;
20306
20307   /* A template-id has already been resolved; there is no lookup to
20308      do.  */
20309   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20310     return name;
20311   if (BASELINK_P (name))
20312     {
20313       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20314                   == TEMPLATE_ID_EXPR);
20315       return name;
20316     }
20317
20318   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20319      it should already have been checked to make sure that the name
20320      used matches the type being destroyed.  */
20321   if (TREE_CODE (name) == BIT_NOT_EXPR)
20322     {
20323       tree type;
20324
20325       /* Figure out to which type this destructor applies.  */
20326       if (parser->scope)
20327         type = parser->scope;
20328       else if (object_type)
20329         type = object_type;
20330       else
20331         type = current_class_type;
20332       /* If that's not a class type, there is no destructor.  */
20333       if (!type || !CLASS_TYPE_P (type))
20334         return error_mark_node;
20335       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20336         lazily_declare_fn (sfk_destructor, type);
20337       if (!CLASSTYPE_DESTRUCTORS (type))
20338           return error_mark_node;
20339       /* If it was a class type, return the destructor.  */
20340       return CLASSTYPE_DESTRUCTORS (type);
20341     }
20342
20343   /* By this point, the NAME should be an ordinary identifier.  If
20344      the id-expression was a qualified name, the qualifying scope is
20345      stored in PARSER->SCOPE at this point.  */
20346   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20347
20348   /* Perform the lookup.  */
20349   if (parser->scope)
20350     {
20351       bool dependent_p;
20352
20353       if (parser->scope == error_mark_node)
20354         return error_mark_node;
20355
20356       /* If the SCOPE is dependent, the lookup must be deferred until
20357          the template is instantiated -- unless we are explicitly
20358          looking up names in uninstantiated templates.  Even then, we
20359          cannot look up the name if the scope is not a class type; it
20360          might, for example, be a template type parameter.  */
20361       dependent_p = (TYPE_P (parser->scope)
20362                      && dependent_scope_p (parser->scope));
20363       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20364           && dependent_p)
20365         /* Defer lookup.  */
20366         decl = error_mark_node;
20367       else
20368         {
20369           tree pushed_scope = NULL_TREE;
20370
20371           /* If PARSER->SCOPE is a dependent type, then it must be a
20372              class type, and we must not be checking dependencies;
20373              otherwise, we would have processed this lookup above.  So
20374              that PARSER->SCOPE is not considered a dependent base by
20375              lookup_member, we must enter the scope here.  */
20376           if (dependent_p)
20377             pushed_scope = push_scope (parser->scope);
20378
20379           /* If the PARSER->SCOPE is a template specialization, it
20380              may be instantiated during name lookup.  In that case,
20381              errors may be issued.  Even if we rollback the current
20382              tentative parse, those errors are valid.  */
20383           decl = lookup_qualified_name (parser->scope, name,
20384                                         tag_type != none_type,
20385                                         /*complain=*/true);
20386
20387           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20388              lookup result and the nested-name-specifier nominates a class C:
20389                * if the name specified after the nested-name-specifier, when
20390                looked up in C, is the injected-class-name of C (Clause 9), or
20391                * if the name specified after the nested-name-specifier is the
20392                same as the identifier or the simple-template-id's template-
20393                name in the last component of the nested-name-specifier,
20394              the name is instead considered to name the constructor of
20395              class C. [ Note: for example, the constructor is not an
20396              acceptable lookup result in an elaborated-type-specifier so
20397              the constructor would not be used in place of the
20398              injected-class-name. --end note ] Such a constructor name
20399              shall be used only in the declarator-id of a declaration that
20400              names a constructor or in a using-declaration.  */
20401           if (tag_type == none_type
20402               && DECL_SELF_REFERENCE_P (decl)
20403               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20404             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20405                                           tag_type != none_type,
20406                                           /*complain=*/true);
20407
20408           /* If we have a single function from a using decl, pull it out.  */
20409           if (TREE_CODE (decl) == OVERLOAD
20410               && !really_overloaded_fn (decl))
20411             decl = OVL_FUNCTION (decl);
20412
20413           if (pushed_scope)
20414             pop_scope (pushed_scope);
20415         }
20416
20417       /* If the scope is a dependent type and either we deferred lookup or
20418          we did lookup but didn't find the name, rememeber the name.  */
20419       if (decl == error_mark_node && TYPE_P (parser->scope)
20420           && dependent_type_p (parser->scope))
20421         {
20422           if (tag_type)
20423             {
20424               tree type;
20425
20426               /* The resolution to Core Issue 180 says that `struct
20427                  A::B' should be considered a type-name, even if `A'
20428                  is dependent.  */
20429               type = make_typename_type (parser->scope, name, tag_type,
20430                                          /*complain=*/tf_error);
20431               decl = TYPE_NAME (type);
20432             }
20433           else if (is_template
20434                    && (cp_parser_next_token_ends_template_argument_p (parser)
20435                        || cp_lexer_next_token_is (parser->lexer,
20436                                                   CPP_CLOSE_PAREN)))
20437             decl = make_unbound_class_template (parser->scope,
20438                                                 name, NULL_TREE,
20439                                                 /*complain=*/tf_error);
20440           else
20441             decl = build_qualified_name (/*type=*/NULL_TREE,
20442                                          parser->scope, name,
20443                                          is_template);
20444         }
20445       parser->qualifying_scope = parser->scope;
20446       parser->object_scope = NULL_TREE;
20447     }
20448   else if (object_type)
20449     {
20450       tree object_decl = NULL_TREE;
20451       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20452          OBJECT_TYPE is not a class.  */
20453       if (CLASS_TYPE_P (object_type))
20454         /* If the OBJECT_TYPE is a template specialization, it may
20455            be instantiated during name lookup.  In that case, errors
20456            may be issued.  Even if we rollback the current tentative
20457            parse, those errors are valid.  */
20458         object_decl = lookup_member (object_type,
20459                                      name,
20460                                      /*protect=*/0,
20461                                      tag_type != none_type,
20462                                      tf_warning_or_error);
20463       /* Look it up in the enclosing context, too.  */
20464       decl = lookup_name_real (name, tag_type != none_type,
20465                                /*nonclass=*/0,
20466                                /*block_p=*/true, is_namespace, flags);
20467       parser->object_scope = object_type;
20468       parser->qualifying_scope = NULL_TREE;
20469       if (object_decl)
20470         decl = object_decl;
20471     }
20472   else
20473     {
20474       decl = lookup_name_real (name, tag_type != none_type,
20475                                /*nonclass=*/0,
20476                                /*block_p=*/true, is_namespace, flags);
20477       parser->qualifying_scope = NULL_TREE;
20478       parser->object_scope = NULL_TREE;
20479     }
20480
20481   /* If the lookup failed, let our caller know.  */
20482   if (!decl || decl == error_mark_node)
20483     return error_mark_node;
20484
20485   /* Pull out the template from an injected-class-name (or multiple).  */
20486   if (is_template)
20487     decl = maybe_get_template_decl_from_type_decl (decl);
20488
20489   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20490   if (TREE_CODE (decl) == TREE_LIST)
20491     {
20492       if (ambiguous_decls)
20493         *ambiguous_decls = decl;
20494       /* The error message we have to print is too complicated for
20495          cp_parser_error, so we incorporate its actions directly.  */
20496       if (!cp_parser_simulate_error (parser))
20497         {
20498           error_at (name_location, "reference to %qD is ambiguous",
20499                     name);
20500           print_candidates (decl);
20501         }
20502       return error_mark_node;
20503     }
20504
20505   gcc_assert (DECL_P (decl)
20506               || TREE_CODE (decl) == OVERLOAD
20507               || TREE_CODE (decl) == SCOPE_REF
20508               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20509               || BASELINK_P (decl));
20510
20511   /* If we have resolved the name of a member declaration, check to
20512      see if the declaration is accessible.  When the name resolves to
20513      set of overloaded functions, accessibility is checked when
20514      overload resolution is done.
20515
20516      During an explicit instantiation, access is not checked at all,
20517      as per [temp.explicit].  */
20518   if (DECL_P (decl))
20519     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20520
20521   maybe_record_typedef_use (decl);
20522
20523   return decl;
20524 }
20525
20526 /* Like cp_parser_lookup_name, but for use in the typical case where
20527    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20528    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20529
20530 static tree
20531 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20532 {
20533   return cp_parser_lookup_name (parser, name,
20534                                 none_type,
20535                                 /*is_template=*/false,
20536                                 /*is_namespace=*/false,
20537                                 /*check_dependency=*/true,
20538                                 /*ambiguous_decls=*/NULL,
20539                                 location);
20540 }
20541
20542 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20543    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20544    true, the DECL indicates the class being defined in a class-head,
20545    or declared in an elaborated-type-specifier.
20546
20547    Otherwise, return DECL.  */
20548
20549 static tree
20550 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20551 {
20552   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20553      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20554
20555        struct A {
20556          template <typename T> struct B;
20557        };
20558
20559        template <typename T> struct A::B {};
20560
20561      Similarly, in an elaborated-type-specifier:
20562
20563        namespace N { struct X{}; }
20564
20565        struct A {
20566          template <typename T> friend struct N::X;
20567        };
20568
20569      However, if the DECL refers to a class type, and we are in
20570      the scope of the class, then the name lookup automatically
20571      finds the TYPE_DECL created by build_self_reference rather
20572      than a TEMPLATE_DECL.  For example, in:
20573
20574        template <class T> struct S {
20575          S s;
20576        };
20577
20578      there is no need to handle such case.  */
20579
20580   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20581     return DECL_TEMPLATE_RESULT (decl);
20582
20583   return decl;
20584 }
20585
20586 /* If too many, or too few, template-parameter lists apply to the
20587    declarator, issue an error message.  Returns TRUE if all went well,
20588    and FALSE otherwise.  */
20589
20590 static bool
20591 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20592                                                 cp_declarator *declarator,
20593                                                 location_t declarator_location)
20594 {
20595   unsigned num_templates;
20596
20597   /* We haven't seen any classes that involve template parameters yet.  */
20598   num_templates = 0;
20599
20600   switch (declarator->kind)
20601     {
20602     case cdk_id:
20603       if (declarator->u.id.qualifying_scope)
20604         {
20605           tree scope;
20606
20607           scope = declarator->u.id.qualifying_scope;
20608
20609           while (scope && CLASS_TYPE_P (scope))
20610             {
20611               /* You're supposed to have one `template <...>'
20612                  for every template class, but you don't need one
20613                  for a full specialization.  For example:
20614
20615                  template <class T> struct S{};
20616                  template <> struct S<int> { void f(); };
20617                  void S<int>::f () {}
20618
20619                  is correct; there shouldn't be a `template <>' for
20620                  the definition of `S<int>::f'.  */
20621               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20622                 /* If SCOPE does not have template information of any
20623                    kind, then it is not a template, nor is it nested
20624                    within a template.  */
20625                 break;
20626               if (explicit_class_specialization_p (scope))
20627                 break;
20628               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20629                 ++num_templates;
20630
20631               scope = TYPE_CONTEXT (scope);
20632             }
20633         }
20634       else if (TREE_CODE (declarator->u.id.unqualified_name)
20635                == TEMPLATE_ID_EXPR)
20636         /* If the DECLARATOR has the form `X<y>' then it uses one
20637            additional level of template parameters.  */
20638         ++num_templates;
20639
20640       return cp_parser_check_template_parameters 
20641         (parser, num_templates, declarator_location, declarator);
20642
20643
20644     case cdk_function:
20645     case cdk_array:
20646     case cdk_pointer:
20647     case cdk_reference:
20648     case cdk_ptrmem:
20649       return (cp_parser_check_declarator_template_parameters
20650               (parser, declarator->declarator, declarator_location));
20651
20652     case cdk_error:
20653       return true;
20654
20655     default:
20656       gcc_unreachable ();
20657     }
20658   return false;
20659 }
20660
20661 /* NUM_TEMPLATES were used in the current declaration.  If that is
20662    invalid, return FALSE and issue an error messages.  Otherwise,
20663    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20664    declarator and we can print more accurate diagnostics.  */
20665
20666 static bool
20667 cp_parser_check_template_parameters (cp_parser* parser,
20668                                      unsigned num_templates,
20669                                      location_t location,
20670                                      cp_declarator *declarator)
20671 {
20672   /* If there are the same number of template classes and parameter
20673      lists, that's OK.  */
20674   if (parser->num_template_parameter_lists == num_templates)
20675     return true;
20676   /* If there are more, but only one more, then we are referring to a
20677      member template.  That's OK too.  */
20678   if (parser->num_template_parameter_lists == num_templates + 1)
20679     return true;
20680   /* If there are more template classes than parameter lists, we have
20681      something like:
20682
20683        template <class T> void S<T>::R<T>::f ();  */
20684   if (parser->num_template_parameter_lists < num_templates)
20685     {
20686       if (declarator && !current_function_decl)
20687         error_at (location, "specializing member %<%T::%E%> "
20688                   "requires %<template<>%> syntax", 
20689                   declarator->u.id.qualifying_scope,
20690                   declarator->u.id.unqualified_name);
20691       else if (declarator)
20692         error_at (location, "invalid declaration of %<%T::%E%>",
20693                   declarator->u.id.qualifying_scope,
20694                   declarator->u.id.unqualified_name);
20695       else 
20696         error_at (location, "too few template-parameter-lists");
20697       return false;
20698     }
20699   /* Otherwise, there are too many template parameter lists.  We have
20700      something like:
20701
20702      template <class T> template <class U> void S::f();  */
20703   error_at (location, "too many template-parameter-lists");
20704   return false;
20705 }
20706
20707 /* Parse an optional `::' token indicating that the following name is
20708    from the global namespace.  If so, PARSER->SCOPE is set to the
20709    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20710    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20711    Returns the new value of PARSER->SCOPE, if the `::' token is
20712    present, and NULL_TREE otherwise.  */
20713
20714 static tree
20715 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20716 {
20717   cp_token *token;
20718
20719   /* Peek at the next token.  */
20720   token = cp_lexer_peek_token (parser->lexer);
20721   /* If we're looking at a `::' token then we're starting from the
20722      global namespace, not our current location.  */
20723   if (token->type == CPP_SCOPE)
20724     {
20725       /* Consume the `::' token.  */
20726       cp_lexer_consume_token (parser->lexer);
20727       /* Set the SCOPE so that we know where to start the lookup.  */
20728       parser->scope = global_namespace;
20729       parser->qualifying_scope = global_namespace;
20730       parser->object_scope = NULL_TREE;
20731
20732       return parser->scope;
20733     }
20734   else if (!current_scope_valid_p)
20735     {
20736       parser->scope = NULL_TREE;
20737       parser->qualifying_scope = NULL_TREE;
20738       parser->object_scope = NULL_TREE;
20739     }
20740
20741   return NULL_TREE;
20742 }
20743
20744 /* Returns TRUE if the upcoming token sequence is the start of a
20745    constructor declarator.  If FRIEND_P is true, the declarator is
20746    preceded by the `friend' specifier.  */
20747
20748 static bool
20749 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20750 {
20751   bool constructor_p;
20752   tree nested_name_specifier;
20753   cp_token *next_token;
20754
20755   /* The common case is that this is not a constructor declarator, so
20756      try to avoid doing lots of work if at all possible.  It's not
20757      valid declare a constructor at function scope.  */
20758   if (parser->in_function_body)
20759     return false;
20760   /* And only certain tokens can begin a constructor declarator.  */
20761   next_token = cp_lexer_peek_token (parser->lexer);
20762   if (next_token->type != CPP_NAME
20763       && next_token->type != CPP_SCOPE
20764       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20765       && next_token->type != CPP_TEMPLATE_ID)
20766     return false;
20767
20768   /* Parse tentatively; we are going to roll back all of the tokens
20769      consumed here.  */
20770   cp_parser_parse_tentatively (parser);
20771   /* Assume that we are looking at a constructor declarator.  */
20772   constructor_p = true;
20773
20774   /* Look for the optional `::' operator.  */
20775   cp_parser_global_scope_opt (parser,
20776                               /*current_scope_valid_p=*/false);
20777   /* Look for the nested-name-specifier.  */
20778   nested_name_specifier
20779     = (cp_parser_nested_name_specifier_opt (parser,
20780                                             /*typename_keyword_p=*/false,
20781                                             /*check_dependency_p=*/false,
20782                                             /*type_p=*/false,
20783                                             /*is_declaration=*/false));
20784   /* Outside of a class-specifier, there must be a
20785      nested-name-specifier.  */
20786   if (!nested_name_specifier &&
20787       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20788        || friend_p))
20789     constructor_p = false;
20790   else if (nested_name_specifier == error_mark_node)
20791     constructor_p = false;
20792
20793   /* If we have a class scope, this is easy; DR 147 says that S::S always
20794      names the constructor, and no other qualified name could.  */
20795   if (constructor_p && nested_name_specifier
20796       && CLASS_TYPE_P (nested_name_specifier))
20797     {
20798       tree id = cp_parser_unqualified_id (parser,
20799                                           /*template_keyword_p=*/false,
20800                                           /*check_dependency_p=*/false,
20801                                           /*declarator_p=*/true,
20802                                           /*optional_p=*/false);
20803       if (is_overloaded_fn (id))
20804         id = DECL_NAME (get_first_fn (id));
20805       if (!constructor_name_p (id, nested_name_specifier))
20806         constructor_p = false;
20807     }
20808   /* If we still think that this might be a constructor-declarator,
20809      look for a class-name.  */
20810   else if (constructor_p)
20811     {
20812       /* If we have:
20813
20814            template <typename T> struct S {
20815              S();
20816            };
20817
20818          we must recognize that the nested `S' names a class.  */
20819       tree type_decl;
20820       type_decl = cp_parser_class_name (parser,
20821                                         /*typename_keyword_p=*/false,
20822                                         /*template_keyword_p=*/false,
20823                                         none_type,
20824                                         /*check_dependency_p=*/false,
20825                                         /*class_head_p=*/false,
20826                                         /*is_declaration=*/false);
20827       /* If there was no class-name, then this is not a constructor.  */
20828       constructor_p = !cp_parser_error_occurred (parser);
20829
20830       /* If we're still considering a constructor, we have to see a `(',
20831          to begin the parameter-declaration-clause, followed by either a
20832          `)', an `...', or a decl-specifier.  We need to check for a
20833          type-specifier to avoid being fooled into thinking that:
20834
20835            S (f) (int);
20836
20837          is a constructor.  (It is actually a function named `f' that
20838          takes one parameter (of type `int') and returns a value of type
20839          `S'.  */
20840       if (constructor_p
20841           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20842         constructor_p = false;
20843
20844       if (constructor_p
20845           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20846           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20847           /* A parameter declaration begins with a decl-specifier,
20848              which is either the "attribute" keyword, a storage class
20849              specifier, or (usually) a type-specifier.  */
20850           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20851         {
20852           tree type;
20853           tree pushed_scope = NULL_TREE;
20854           unsigned saved_num_template_parameter_lists;
20855
20856           /* Names appearing in the type-specifier should be looked up
20857              in the scope of the class.  */
20858           if (current_class_type)
20859             type = NULL_TREE;
20860           else
20861             {
20862               type = TREE_TYPE (type_decl);
20863               if (TREE_CODE (type) == TYPENAME_TYPE)
20864                 {
20865                   type = resolve_typename_type (type,
20866                                                 /*only_current_p=*/false);
20867                   if (TREE_CODE (type) == TYPENAME_TYPE)
20868                     {
20869                       cp_parser_abort_tentative_parse (parser);
20870                       return false;
20871                     }
20872                 }
20873               pushed_scope = push_scope (type);
20874             }
20875
20876           /* Inside the constructor parameter list, surrounding
20877              template-parameter-lists do not apply.  */
20878           saved_num_template_parameter_lists
20879             = parser->num_template_parameter_lists;
20880           parser->num_template_parameter_lists = 0;
20881
20882           /* Look for the type-specifier.  */
20883           cp_parser_type_specifier (parser,
20884                                     CP_PARSER_FLAGS_NONE,
20885                                     /*decl_specs=*/NULL,
20886                                     /*is_declarator=*/true,
20887                                     /*declares_class_or_enum=*/NULL,
20888                                     /*is_cv_qualifier=*/NULL);
20889
20890           parser->num_template_parameter_lists
20891             = saved_num_template_parameter_lists;
20892
20893           /* Leave the scope of the class.  */
20894           if (pushed_scope)
20895             pop_scope (pushed_scope);
20896
20897           constructor_p = !cp_parser_error_occurred (parser);
20898         }
20899     }
20900
20901   /* We did not really want to consume any tokens.  */
20902   cp_parser_abort_tentative_parse (parser);
20903
20904   return constructor_p;
20905 }
20906
20907 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20908    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20909    they must be performed once we are in the scope of the function.
20910
20911    Returns the function defined.  */
20912
20913 static tree
20914 cp_parser_function_definition_from_specifiers_and_declarator
20915   (cp_parser* parser,
20916    cp_decl_specifier_seq *decl_specifiers,
20917    tree attributes,
20918    const cp_declarator *declarator)
20919 {
20920   tree fn;
20921   bool success_p;
20922
20923   /* Begin the function-definition.  */
20924   success_p = start_function (decl_specifiers, declarator, attributes);
20925
20926   /* The things we're about to see are not directly qualified by any
20927      template headers we've seen thus far.  */
20928   reset_specialization ();
20929
20930   /* If there were names looked up in the decl-specifier-seq that we
20931      did not check, check them now.  We must wait until we are in the
20932      scope of the function to perform the checks, since the function
20933      might be a friend.  */
20934   perform_deferred_access_checks ();
20935
20936   if (!success_p)
20937     {
20938       /* Skip the entire function.  */
20939       cp_parser_skip_to_end_of_block_or_statement (parser);
20940       fn = error_mark_node;
20941     }
20942   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20943     {
20944       /* Seen already, skip it.  An error message has already been output.  */
20945       cp_parser_skip_to_end_of_block_or_statement (parser);
20946       fn = current_function_decl;
20947       current_function_decl = NULL_TREE;
20948       /* If this is a function from a class, pop the nested class.  */
20949       if (current_class_name)
20950         pop_nested_class ();
20951     }
20952   else
20953     {
20954       timevar_id_t tv;
20955       if (DECL_DECLARED_INLINE_P (current_function_decl))
20956         tv = TV_PARSE_INLINE;
20957       else
20958         tv = TV_PARSE_FUNC;
20959       timevar_push (tv);
20960       fn = cp_parser_function_definition_after_declarator (parser,
20961                                                          /*inline_p=*/false);
20962       timevar_pop (tv);
20963     }
20964
20965   return fn;
20966 }
20967
20968 /* Parse the part of a function-definition that follows the
20969    declarator.  INLINE_P is TRUE iff this function is an inline
20970    function defined within a class-specifier.
20971
20972    Returns the function defined.  */
20973
20974 static tree
20975 cp_parser_function_definition_after_declarator (cp_parser* parser,
20976                                                 bool inline_p)
20977 {
20978   tree fn;
20979   bool ctor_initializer_p = false;
20980   bool saved_in_unbraced_linkage_specification_p;
20981   bool saved_in_function_body;
20982   unsigned saved_num_template_parameter_lists;
20983   cp_token *token;
20984
20985   saved_in_function_body = parser->in_function_body;
20986   parser->in_function_body = true;
20987   /* If the next token is `return', then the code may be trying to
20988      make use of the "named return value" extension that G++ used to
20989      support.  */
20990   token = cp_lexer_peek_token (parser->lexer);
20991   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20992     {
20993       /* Consume the `return' keyword.  */
20994       cp_lexer_consume_token (parser->lexer);
20995       /* Look for the identifier that indicates what value is to be
20996          returned.  */
20997       cp_parser_identifier (parser);
20998       /* Issue an error message.  */
20999       error_at (token->location,
21000                 "named return values are no longer supported");
21001       /* Skip tokens until we reach the start of the function body.  */
21002       while (true)
21003         {
21004           cp_token *token = cp_lexer_peek_token (parser->lexer);
21005           if (token->type == CPP_OPEN_BRACE
21006               || token->type == CPP_EOF
21007               || token->type == CPP_PRAGMA_EOL)
21008             break;
21009           cp_lexer_consume_token (parser->lexer);
21010         }
21011     }
21012   /* The `extern' in `extern "C" void f () { ... }' does not apply to
21013      anything declared inside `f'.  */
21014   saved_in_unbraced_linkage_specification_p
21015     = parser->in_unbraced_linkage_specification_p;
21016   parser->in_unbraced_linkage_specification_p = false;
21017   /* Inside the function, surrounding template-parameter-lists do not
21018      apply.  */
21019   saved_num_template_parameter_lists
21020     = parser->num_template_parameter_lists;
21021   parser->num_template_parameter_lists = 0;
21022
21023   start_lambda_scope (current_function_decl);
21024
21025   /* If the next token is `try', `__transaction_atomic', or
21026      `__transaction_relaxed`, then we are looking at either function-try-block
21027      or function-transaction-block.  Note that all of these include the
21028      function-body.  */
21029   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21030     ctor_initializer_p = cp_parser_function_transaction (parser,
21031         RID_TRANSACTION_ATOMIC);
21032   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21033       RID_TRANSACTION_RELAXED))
21034     ctor_initializer_p = cp_parser_function_transaction (parser,
21035         RID_TRANSACTION_RELAXED);
21036   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21037     ctor_initializer_p = cp_parser_function_try_block (parser);
21038   else
21039     ctor_initializer_p
21040       = cp_parser_ctor_initializer_opt_and_function_body (parser);
21041
21042   finish_lambda_scope ();
21043
21044   /* Finish the function.  */
21045   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21046                         (inline_p ? 2 : 0));
21047   /* Generate code for it, if necessary.  */
21048   expand_or_defer_fn (fn);
21049   /* Restore the saved values.  */
21050   parser->in_unbraced_linkage_specification_p
21051     = saved_in_unbraced_linkage_specification_p;
21052   parser->num_template_parameter_lists
21053     = saved_num_template_parameter_lists;
21054   parser->in_function_body = saved_in_function_body;
21055
21056   return fn;
21057 }
21058
21059 /* Parse a template-declaration, assuming that the `export' (and
21060    `extern') keywords, if present, has already been scanned.  MEMBER_P
21061    is as for cp_parser_template_declaration.  */
21062
21063 static void
21064 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21065 {
21066   tree decl = NULL_TREE;
21067   VEC (deferred_access_check,gc) *checks;
21068   tree parameter_list;
21069   bool friend_p = false;
21070   bool need_lang_pop;
21071   cp_token *token;
21072
21073   /* Look for the `template' keyword.  */
21074   token = cp_lexer_peek_token (parser->lexer);
21075   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21076     return;
21077
21078   /* And the `<'.  */
21079   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21080     return;
21081   if (at_class_scope_p () && current_function_decl)
21082     {
21083       /* 14.5.2.2 [temp.mem]
21084
21085          A local class shall not have member templates.  */
21086       error_at (token->location,
21087                 "invalid declaration of member template in local class");
21088       cp_parser_skip_to_end_of_block_or_statement (parser);
21089       return;
21090     }
21091   /* [temp]
21092
21093      A template ... shall not have C linkage.  */
21094   if (current_lang_name == lang_name_c)
21095     {
21096       error_at (token->location, "template with C linkage");
21097       /* Give it C++ linkage to avoid confusing other parts of the
21098          front end.  */
21099       push_lang_context (lang_name_cplusplus);
21100       need_lang_pop = true;
21101     }
21102   else
21103     need_lang_pop = false;
21104
21105   /* We cannot perform access checks on the template parameter
21106      declarations until we know what is being declared, just as we
21107      cannot check the decl-specifier list.  */
21108   push_deferring_access_checks (dk_deferred);
21109
21110   /* If the next token is `>', then we have an invalid
21111      specialization.  Rather than complain about an invalid template
21112      parameter, issue an error message here.  */
21113   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21114     {
21115       cp_parser_error (parser, "invalid explicit specialization");
21116       begin_specialization ();
21117       parameter_list = NULL_TREE;
21118     }
21119   else
21120     {
21121       /* Parse the template parameters.  */
21122       parameter_list = cp_parser_template_parameter_list (parser);
21123       fixup_template_parms ();
21124     }
21125
21126   /* Get the deferred access checks from the parameter list.  These
21127      will be checked once we know what is being declared, as for a
21128      member template the checks must be performed in the scope of the
21129      class containing the member.  */
21130   checks = get_deferred_access_checks ();
21131
21132   /* Look for the `>'.  */
21133   cp_parser_skip_to_end_of_template_parameter_list (parser);
21134   /* We just processed one more parameter list.  */
21135   ++parser->num_template_parameter_lists;
21136   /* If the next token is `template', there are more template
21137      parameters.  */
21138   if (cp_lexer_next_token_is_keyword (parser->lexer,
21139                                       RID_TEMPLATE))
21140     cp_parser_template_declaration_after_export (parser, member_p);
21141   else if (cxx_dialect >= cxx0x
21142            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21143     decl = cp_parser_alias_declaration (parser);
21144   else
21145     {
21146       /* There are no access checks when parsing a template, as we do not
21147          know if a specialization will be a friend.  */
21148       push_deferring_access_checks (dk_no_check);
21149       token = cp_lexer_peek_token (parser->lexer);
21150       decl = cp_parser_single_declaration (parser,
21151                                            checks,
21152                                            member_p,
21153                                            /*explicit_specialization_p=*/false,
21154                                            &friend_p);
21155       pop_deferring_access_checks ();
21156
21157       /* If this is a member template declaration, let the front
21158          end know.  */
21159       if (member_p && !friend_p && decl)
21160         {
21161           if (TREE_CODE (decl) == TYPE_DECL)
21162             cp_parser_check_access_in_redeclaration (decl, token->location);
21163
21164           decl = finish_member_template_decl (decl);
21165         }
21166       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21167         make_friend_class (current_class_type, TREE_TYPE (decl),
21168                            /*complain=*/true);
21169     }
21170   /* We are done with the current parameter list.  */
21171   --parser->num_template_parameter_lists;
21172
21173   pop_deferring_access_checks ();
21174
21175   /* Finish up.  */
21176   finish_template_decl (parameter_list);
21177
21178   /* Check the template arguments for a literal operator template.  */
21179   if (decl
21180       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21181       && UDLIT_OPER_P (DECL_NAME (decl)))
21182     {
21183       bool ok = true;
21184       if (parameter_list == NULL_TREE)
21185         ok = false;
21186       else
21187         {
21188           int num_parms = TREE_VEC_LENGTH (parameter_list);
21189           if (num_parms != 1)
21190             ok = false;
21191           else
21192             {
21193               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21194               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21195               if (TREE_TYPE (parm) != char_type_node
21196                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21197                 ok = false;
21198             }
21199         }
21200       if (!ok)
21201         error ("literal operator template %qD has invalid parameter list."
21202                "  Expected non-type template argument pack <char...>",
21203                decl);
21204     }
21205   /* Register member declarations.  */
21206   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21207     finish_member_declaration (decl);
21208   /* For the erroneous case of a template with C linkage, we pushed an
21209      implicit C++ linkage scope; exit that scope now.  */
21210   if (need_lang_pop)
21211     pop_lang_context ();
21212   /* If DECL is a function template, we must return to parse it later.
21213      (Even though there is no definition, there might be default
21214      arguments that need handling.)  */
21215   if (member_p && decl
21216       && (TREE_CODE (decl) == FUNCTION_DECL
21217           || DECL_FUNCTION_TEMPLATE_P (decl)))
21218     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21219 }
21220
21221 /* Perform the deferred access checks from a template-parameter-list.
21222    CHECKS is a TREE_LIST of access checks, as returned by
21223    get_deferred_access_checks.  */
21224
21225 static void
21226 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21227 {
21228   ++processing_template_parmlist;
21229   perform_access_checks (checks);
21230   --processing_template_parmlist;
21231 }
21232
21233 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21234    `function-definition' sequence.  MEMBER_P is true, this declaration
21235    appears in a class scope.
21236
21237    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21238    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21239
21240 static tree
21241 cp_parser_single_declaration (cp_parser* parser,
21242                               VEC (deferred_access_check,gc)* checks,
21243                               bool member_p,
21244                               bool explicit_specialization_p,
21245                               bool* friend_p)
21246 {
21247   int declares_class_or_enum;
21248   tree decl = NULL_TREE;
21249   cp_decl_specifier_seq decl_specifiers;
21250   bool function_definition_p = false;
21251   cp_token *decl_spec_token_start;
21252
21253   /* This function is only used when processing a template
21254      declaration.  */
21255   gcc_assert (innermost_scope_kind () == sk_template_parms
21256               || innermost_scope_kind () == sk_template_spec);
21257
21258   /* Defer access checks until we know what is being declared.  */
21259   push_deferring_access_checks (dk_deferred);
21260
21261   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21262      alternative.  */
21263   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21264   cp_parser_decl_specifier_seq (parser,
21265                                 CP_PARSER_FLAGS_OPTIONAL,
21266                                 &decl_specifiers,
21267                                 &declares_class_or_enum);
21268   if (friend_p)
21269     *friend_p = cp_parser_friend_p (&decl_specifiers);
21270
21271   /* There are no template typedefs.  */
21272   if (decl_specifiers.specs[(int) ds_typedef])
21273     {
21274       error_at (decl_spec_token_start->location,
21275                 "template declaration of %<typedef%>");
21276       decl = error_mark_node;
21277     }
21278
21279   /* Gather up the access checks that occurred the
21280      decl-specifier-seq.  */
21281   stop_deferring_access_checks ();
21282
21283   /* Check for the declaration of a template class.  */
21284   if (declares_class_or_enum)
21285     {
21286       if (cp_parser_declares_only_class_p (parser))
21287         {
21288           decl = shadow_tag (&decl_specifiers);
21289
21290           /* In this case:
21291
21292                struct C {
21293                  friend template <typename T> struct A<T>::B;
21294                };
21295
21296              A<T>::B will be represented by a TYPENAME_TYPE, and
21297              therefore not recognized by shadow_tag.  */
21298           if (friend_p && *friend_p
21299               && !decl
21300               && decl_specifiers.type
21301               && TYPE_P (decl_specifiers.type))
21302             decl = decl_specifiers.type;
21303
21304           if (decl && decl != error_mark_node)
21305             decl = TYPE_NAME (decl);
21306           else
21307             decl = error_mark_node;
21308
21309           /* Perform access checks for template parameters.  */
21310           cp_parser_perform_template_parameter_access_checks (checks);
21311         }
21312     }
21313
21314   /* Complain about missing 'typename' or other invalid type names.  */
21315   if (!decl_specifiers.any_type_specifiers_p
21316       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21317     {
21318       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21319          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21320          the rest of this declaration.  */
21321       decl = error_mark_node;
21322       goto out;
21323     }
21324
21325   /* If it's not a template class, try for a template function.  If
21326      the next token is a `;', then this declaration does not declare
21327      anything.  But, if there were errors in the decl-specifiers, then
21328      the error might well have come from an attempted class-specifier.
21329      In that case, there's no need to warn about a missing declarator.  */
21330   if (!decl
21331       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21332           || decl_specifiers.type != error_mark_node))
21333     {
21334       decl = cp_parser_init_declarator (parser,
21335                                         &decl_specifiers,
21336                                         checks,
21337                                         /*function_definition_allowed_p=*/true,
21338                                         member_p,
21339                                         declares_class_or_enum,
21340                                         &function_definition_p,
21341                                         NULL);
21342
21343     /* 7.1.1-1 [dcl.stc]
21344
21345        A storage-class-specifier shall not be specified in an explicit
21346        specialization...  */
21347     if (decl
21348         && explicit_specialization_p
21349         && decl_specifiers.storage_class != sc_none)
21350       {
21351         error_at (decl_spec_token_start->location,
21352                   "explicit template specialization cannot have a storage class");
21353         decl = error_mark_node;
21354       }
21355     }
21356
21357   /* Look for a trailing `;' after the declaration.  */
21358   if (!function_definition_p
21359       && (decl == error_mark_node
21360           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21361     cp_parser_skip_to_end_of_block_or_statement (parser);
21362
21363  out:
21364   pop_deferring_access_checks ();
21365
21366   /* Clear any current qualification; whatever comes next is the start
21367      of something new.  */
21368   parser->scope = NULL_TREE;
21369   parser->qualifying_scope = NULL_TREE;
21370   parser->object_scope = NULL_TREE;
21371
21372   return decl;
21373 }
21374
21375 /* Parse a cast-expression that is not the operand of a unary "&".  */
21376
21377 static tree
21378 cp_parser_simple_cast_expression (cp_parser *parser)
21379 {
21380   return cp_parser_cast_expression (parser, /*address_p=*/false,
21381                                     /*cast_p=*/false, NULL);
21382 }
21383
21384 /* Parse a functional cast to TYPE.  Returns an expression
21385    representing the cast.  */
21386
21387 static tree
21388 cp_parser_functional_cast (cp_parser* parser, tree type)
21389 {
21390   VEC(tree,gc) *vec;
21391   tree expression_list;
21392   tree cast;
21393   bool nonconst_p;
21394
21395   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21396     {
21397       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21398       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21399       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21400       if (TREE_CODE (type) == TYPE_DECL)
21401         type = TREE_TYPE (type);
21402       return finish_compound_literal (type, expression_list,
21403                                       tf_warning_or_error);
21404     }
21405
21406
21407   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21408                                                  /*cast_p=*/true,
21409                                                  /*allow_expansion_p=*/true,
21410                                                  /*non_constant_p=*/NULL);
21411   if (vec == NULL)
21412     expression_list = error_mark_node;
21413   else
21414     {
21415       expression_list = build_tree_list_vec (vec);
21416       release_tree_vector (vec);
21417     }
21418
21419   cast = build_functional_cast (type, expression_list,
21420                                 tf_warning_or_error);
21421   /* [expr.const]/1: In an integral constant expression "only type
21422      conversions to integral or enumeration type can be used".  */
21423   if (TREE_CODE (type) == TYPE_DECL)
21424     type = TREE_TYPE (type);
21425   if (cast != error_mark_node
21426       && !cast_valid_in_integral_constant_expression_p (type)
21427       && cp_parser_non_integral_constant_expression (parser,
21428                                                      NIC_CONSTRUCTOR))
21429     return error_mark_node;
21430   return cast;
21431 }
21432
21433 /* Save the tokens that make up the body of a member function defined
21434    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21435    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21436    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21437    for the member function.  */
21438
21439 static tree
21440 cp_parser_save_member_function_body (cp_parser* parser,
21441                                      cp_decl_specifier_seq *decl_specifiers,
21442                                      cp_declarator *declarator,
21443                                      tree attributes)
21444 {
21445   cp_token *first;
21446   cp_token *last;
21447   tree fn;
21448
21449   /* Create the FUNCTION_DECL.  */
21450   fn = grokmethod (decl_specifiers, declarator, attributes);
21451   /* If something went badly wrong, bail out now.  */
21452   if (fn == error_mark_node)
21453     {
21454       /* If there's a function-body, skip it.  */
21455       if (cp_parser_token_starts_function_definition_p
21456           (cp_lexer_peek_token (parser->lexer)))
21457         cp_parser_skip_to_end_of_block_or_statement (parser);
21458       return error_mark_node;
21459     }
21460
21461   /* Remember it, if there default args to post process.  */
21462   cp_parser_save_default_args (parser, fn);
21463
21464   /* Save away the tokens that make up the body of the
21465      function.  */
21466   first = parser->lexer->next_token;
21467   /* We can have braced-init-list mem-initializers before the fn body.  */
21468   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21469     {
21470       cp_lexer_consume_token (parser->lexer);
21471       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21472              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21473         {
21474           /* cache_group will stop after an un-nested { } pair, too.  */
21475           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21476             break;
21477
21478           /* variadic mem-inits have ... after the ')'.  */
21479           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21480             cp_lexer_consume_token (parser->lexer);
21481         }
21482     }
21483   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21484   /* Handle function try blocks.  */
21485   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21486     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21487   last = parser->lexer->next_token;
21488
21489   /* Save away the inline definition; we will process it when the
21490      class is complete.  */
21491   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21492   DECL_PENDING_INLINE_P (fn) = 1;
21493
21494   /* We need to know that this was defined in the class, so that
21495      friend templates are handled correctly.  */
21496   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21497
21498   /* Add FN to the queue of functions to be parsed later.  */
21499   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21500
21501   return fn;
21502 }
21503
21504 /* Save the tokens that make up the in-class initializer for a non-static
21505    data member.  Returns a DEFAULT_ARG.  */
21506
21507 static tree
21508 cp_parser_save_nsdmi (cp_parser* parser)
21509 {
21510   return cp_parser_cache_defarg (parser, /*nsdmi=*/true);
21511 }
21512
21513 /* Parse a template-argument-list, as well as the trailing ">" (but
21514    not the opening "<").  See cp_parser_template_argument_list for the
21515    return value.  */
21516
21517 static tree
21518 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21519 {
21520   tree arguments;
21521   tree saved_scope;
21522   tree saved_qualifying_scope;
21523   tree saved_object_scope;
21524   bool saved_greater_than_is_operator_p;
21525   int saved_unevaluated_operand;
21526   int saved_inhibit_evaluation_warnings;
21527
21528   /* [temp.names]
21529
21530      When parsing a template-id, the first non-nested `>' is taken as
21531      the end of the template-argument-list rather than a greater-than
21532      operator.  */
21533   saved_greater_than_is_operator_p
21534     = parser->greater_than_is_operator_p;
21535   parser->greater_than_is_operator_p = false;
21536   /* Parsing the argument list may modify SCOPE, so we save it
21537      here.  */
21538   saved_scope = parser->scope;
21539   saved_qualifying_scope = parser->qualifying_scope;
21540   saved_object_scope = parser->object_scope;
21541   /* We need to evaluate the template arguments, even though this
21542      template-id may be nested within a "sizeof".  */
21543   saved_unevaluated_operand = cp_unevaluated_operand;
21544   cp_unevaluated_operand = 0;
21545   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21546   c_inhibit_evaluation_warnings = 0;
21547   /* Parse the template-argument-list itself.  */
21548   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21549       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21550     arguments = NULL_TREE;
21551   else
21552     arguments = cp_parser_template_argument_list (parser);
21553   /* Look for the `>' that ends the template-argument-list. If we find
21554      a '>>' instead, it's probably just a typo.  */
21555   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21556     {
21557       if (cxx_dialect != cxx98)
21558         {
21559           /* In C++0x, a `>>' in a template argument list or cast
21560              expression is considered to be two separate `>'
21561              tokens. So, change the current token to a `>', but don't
21562              consume it: it will be consumed later when the outer
21563              template argument list (or cast expression) is parsed.
21564              Note that this replacement of `>' for `>>' is necessary
21565              even if we are parsing tentatively: in the tentative
21566              case, after calling
21567              cp_parser_enclosed_template_argument_list we will always
21568              throw away all of the template arguments and the first
21569              closing `>', either because the template argument list
21570              was erroneous or because we are replacing those tokens
21571              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21572              not have been thrown away) is needed either to close an
21573              outer template argument list or to complete a new-style
21574              cast.  */
21575           cp_token *token = cp_lexer_peek_token (parser->lexer);
21576           token->type = CPP_GREATER;
21577         }
21578       else if (!saved_greater_than_is_operator_p)
21579         {
21580           /* If we're in a nested template argument list, the '>>' has
21581             to be a typo for '> >'. We emit the error message, but we
21582             continue parsing and we push a '>' as next token, so that
21583             the argument list will be parsed correctly.  Note that the
21584             global source location is still on the token before the
21585             '>>', so we need to say explicitly where we want it.  */
21586           cp_token *token = cp_lexer_peek_token (parser->lexer);
21587           error_at (token->location, "%<>>%> should be %<> >%> "
21588                     "within a nested template argument list");
21589
21590           token->type = CPP_GREATER;
21591         }
21592       else
21593         {
21594           /* If this is not a nested template argument list, the '>>'
21595             is a typo for '>'. Emit an error message and continue.
21596             Same deal about the token location, but here we can get it
21597             right by consuming the '>>' before issuing the diagnostic.  */
21598           cp_token *token = cp_lexer_consume_token (parser->lexer);
21599           error_at (token->location,
21600                     "spurious %<>>%>, use %<>%> to terminate "
21601                     "a template argument list");
21602         }
21603     }
21604   else
21605     cp_parser_skip_to_end_of_template_parameter_list (parser);
21606   /* The `>' token might be a greater-than operator again now.  */
21607   parser->greater_than_is_operator_p
21608     = saved_greater_than_is_operator_p;
21609   /* Restore the SAVED_SCOPE.  */
21610   parser->scope = saved_scope;
21611   parser->qualifying_scope = saved_qualifying_scope;
21612   parser->object_scope = saved_object_scope;
21613   cp_unevaluated_operand = saved_unevaluated_operand;
21614   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21615
21616   return arguments;
21617 }
21618
21619 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21620    arguments, or the body of the function have not yet been parsed,
21621    parse them now.  */
21622
21623 static void
21624 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21625 {
21626   timevar_push (TV_PARSE_INMETH);
21627   /* If this member is a template, get the underlying
21628      FUNCTION_DECL.  */
21629   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21630     member_function = DECL_TEMPLATE_RESULT (member_function);
21631
21632   /* There should not be any class definitions in progress at this
21633      point; the bodies of members are only parsed outside of all class
21634      definitions.  */
21635   gcc_assert (parser->num_classes_being_defined == 0);
21636   /* While we're parsing the member functions we might encounter more
21637      classes.  We want to handle them right away, but we don't want
21638      them getting mixed up with functions that are currently in the
21639      queue.  */
21640   push_unparsed_function_queues (parser);
21641
21642   /* Make sure that any template parameters are in scope.  */
21643   maybe_begin_member_template_processing (member_function);
21644
21645   /* If the body of the function has not yet been parsed, parse it
21646      now.  */
21647   if (DECL_PENDING_INLINE_P (member_function))
21648     {
21649       tree function_scope;
21650       cp_token_cache *tokens;
21651
21652       /* The function is no longer pending; we are processing it.  */
21653       tokens = DECL_PENDING_INLINE_INFO (member_function);
21654       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21655       DECL_PENDING_INLINE_P (member_function) = 0;
21656
21657       /* If this is a local class, enter the scope of the containing
21658          function.  */
21659       function_scope = current_function_decl;
21660       if (function_scope)
21661         push_function_context ();
21662
21663       /* Push the body of the function onto the lexer stack.  */
21664       cp_parser_push_lexer_for_tokens (parser, tokens);
21665
21666       /* Let the front end know that we going to be defining this
21667          function.  */
21668       start_preparsed_function (member_function, NULL_TREE,
21669                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21670
21671       /* Don't do access checking if it is a templated function.  */
21672       if (processing_template_decl)
21673         push_deferring_access_checks (dk_no_check);
21674
21675       /* Now, parse the body of the function.  */
21676       cp_parser_function_definition_after_declarator (parser,
21677                                                       /*inline_p=*/true);
21678
21679       if (processing_template_decl)
21680         pop_deferring_access_checks ();
21681
21682       /* Leave the scope of the containing function.  */
21683       if (function_scope)
21684         pop_function_context ();
21685       cp_parser_pop_lexer (parser);
21686     }
21687
21688   /* Remove any template parameters from the symbol table.  */
21689   maybe_end_member_template_processing ();
21690
21691   /* Restore the queue.  */
21692   pop_unparsed_function_queues (parser);
21693   timevar_pop (TV_PARSE_INMETH);
21694 }
21695
21696 /* If DECL contains any default args, remember it on the unparsed
21697    functions queue.  */
21698
21699 static void
21700 cp_parser_save_default_args (cp_parser* parser, tree decl)
21701 {
21702   tree probe;
21703
21704   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21705        probe;
21706        probe = TREE_CHAIN (probe))
21707     if (TREE_PURPOSE (probe))
21708       {
21709         cp_default_arg_entry *entry
21710           = VEC_safe_push (cp_default_arg_entry, gc,
21711                            unparsed_funs_with_default_args, NULL);
21712         entry->class_type = current_class_type;
21713         entry->decl = decl;
21714         break;
21715       }
21716 }
21717
21718 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21719    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21720    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21721    from the parameter-type-list.  */
21722
21723 static tree
21724 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21725                                       tree default_arg, tree parmtype)
21726 {
21727   cp_token_cache *tokens;
21728   tree parsed_arg;
21729   bool dummy;
21730
21731   if (default_arg == error_mark_node)
21732     return error_mark_node;
21733
21734   /* Push the saved tokens for the default argument onto the parser's
21735      lexer stack.  */
21736   tokens = DEFARG_TOKENS (default_arg);
21737   cp_parser_push_lexer_for_tokens (parser, tokens);
21738
21739   start_lambda_scope (decl);
21740
21741   /* Parse the default argument.  */
21742   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21743   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21744     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21745
21746   finish_lambda_scope ();
21747
21748   if (!processing_template_decl)
21749     {
21750       /* In a non-template class, check conversions now.  In a template,
21751          we'll wait and instantiate these as needed.  */
21752       if (TREE_CODE (decl) == PARM_DECL)
21753         parsed_arg = check_default_argument (parmtype, parsed_arg);
21754       else
21755         {
21756           int flags = LOOKUP_IMPLICIT;
21757           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21758               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21759             flags = LOOKUP_NORMAL;
21760           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21761         }
21762     }
21763
21764   /* If the token stream has not been completely used up, then
21765      there was extra junk after the end of the default
21766      argument.  */
21767   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21768     {
21769       if (TREE_CODE (decl) == PARM_DECL)
21770         cp_parser_error (parser, "expected %<,%>");
21771       else
21772         cp_parser_error (parser, "expected %<;%>");
21773     }
21774
21775   /* Revert to the main lexer.  */
21776   cp_parser_pop_lexer (parser);
21777
21778   return parsed_arg;
21779 }
21780
21781 /* FIELD is a non-static data member with an initializer which we saved for
21782    later; parse it now.  */
21783
21784 static void
21785 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21786 {
21787   tree def;
21788
21789   push_unparsed_function_queues (parser);
21790   def = cp_parser_late_parse_one_default_arg (parser, field,
21791                                               DECL_INITIAL (field),
21792                                               NULL_TREE);
21793   pop_unparsed_function_queues (parser);
21794
21795   DECL_INITIAL (field) = def;
21796 }
21797
21798 /* FN is a FUNCTION_DECL which may contains a parameter with an
21799    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21800    assumes that the current scope is the scope in which the default
21801    argument should be processed.  */
21802
21803 static void
21804 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21805 {
21806   bool saved_local_variables_forbidden_p;
21807   tree parm, parmdecl;
21808
21809   /* While we're parsing the default args, we might (due to the
21810      statement expression extension) encounter more classes.  We want
21811      to handle them right away, but we don't want them getting mixed
21812      up with default args that are currently in the queue.  */
21813   push_unparsed_function_queues (parser);
21814
21815   /* Local variable names (and the `this' keyword) may not appear
21816      in a default argument.  */
21817   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21818   parser->local_variables_forbidden_p = true;
21819
21820   push_defarg_context (fn);
21821
21822   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21823          parmdecl = DECL_ARGUMENTS (fn);
21824        parm && parm != void_list_node;
21825        parm = TREE_CHAIN (parm),
21826          parmdecl = DECL_CHAIN (parmdecl))
21827     {
21828       tree default_arg = TREE_PURPOSE (parm);
21829       tree parsed_arg;
21830       VEC(tree,gc) *insts;
21831       tree copy;
21832       unsigned ix;
21833
21834       if (!default_arg)
21835         continue;
21836
21837       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21838         /* This can happen for a friend declaration for a function
21839            already declared with default arguments.  */
21840         continue;
21841
21842       parsed_arg
21843         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21844                                                 default_arg,
21845                                                 TREE_VALUE (parm));
21846       if (parsed_arg == error_mark_node)
21847         {
21848           continue;
21849         }
21850
21851       TREE_PURPOSE (parm) = parsed_arg;
21852
21853       /* Update any instantiations we've already created.  */
21854       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21855            VEC_iterate (tree, insts, ix, copy); ix++)
21856         TREE_PURPOSE (copy) = parsed_arg;
21857     }
21858
21859   pop_defarg_context ();
21860
21861   /* Make sure no default arg is missing.  */
21862   check_default_args (fn);
21863
21864   /* Restore the state of local_variables_forbidden_p.  */
21865   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21866
21867   /* Restore the queue.  */
21868   pop_unparsed_function_queues (parser);
21869 }
21870
21871 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21872    either a TYPE or an expression, depending on the form of the
21873    input.  The KEYWORD indicates which kind of expression we have
21874    encountered.  */
21875
21876 static tree
21877 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21878 {
21879   tree expr = NULL_TREE;
21880   const char *saved_message;
21881   char *tmp;
21882   bool saved_integral_constant_expression_p;
21883   bool saved_non_integral_constant_expression_p;
21884   bool pack_expansion_p = false;
21885
21886   /* Types cannot be defined in a `sizeof' expression.  Save away the
21887      old message.  */
21888   saved_message = parser->type_definition_forbidden_message;
21889   /* And create the new one.  */
21890   tmp = concat ("types may not be defined in %<",
21891                 IDENTIFIER_POINTER (ridpointers[keyword]),
21892                 "%> expressions", NULL);
21893   parser->type_definition_forbidden_message = tmp;
21894
21895   /* The restrictions on constant-expressions do not apply inside
21896      sizeof expressions.  */
21897   saved_integral_constant_expression_p
21898     = parser->integral_constant_expression_p;
21899   saved_non_integral_constant_expression_p
21900     = parser->non_integral_constant_expression_p;
21901   parser->integral_constant_expression_p = false;
21902
21903   /* If it's a `...', then we are computing the length of a parameter
21904      pack.  */
21905   if (keyword == RID_SIZEOF
21906       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21907     {
21908       /* Consume the `...'.  */
21909       cp_lexer_consume_token (parser->lexer);
21910       maybe_warn_variadic_templates ();
21911
21912       /* Note that this is an expansion.  */
21913       pack_expansion_p = true;
21914     }
21915
21916   /* Do not actually evaluate the expression.  */
21917   ++cp_unevaluated_operand;
21918   ++c_inhibit_evaluation_warnings;
21919   /* If it's a `(', then we might be looking at the type-id
21920      construction.  */
21921   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21922     {
21923       tree type;
21924       bool saved_in_type_id_in_expr_p;
21925
21926       /* We can't be sure yet whether we're looking at a type-id or an
21927          expression.  */
21928       cp_parser_parse_tentatively (parser);
21929       /* Consume the `('.  */
21930       cp_lexer_consume_token (parser->lexer);
21931       /* Parse the type-id.  */
21932       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21933       parser->in_type_id_in_expr_p = true;
21934       type = cp_parser_type_id (parser);
21935       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21936       /* Now, look for the trailing `)'.  */
21937       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21938       /* If all went well, then we're done.  */
21939       if (cp_parser_parse_definitely (parser))
21940         {
21941           cp_decl_specifier_seq decl_specs;
21942
21943           /* Build a trivial decl-specifier-seq.  */
21944           clear_decl_specs (&decl_specs);
21945           decl_specs.type = type;
21946
21947           /* Call grokdeclarator to figure out what type this is.  */
21948           expr = grokdeclarator (NULL,
21949                                  &decl_specs,
21950                                  TYPENAME,
21951                                  /*initialized=*/0,
21952                                  /*attrlist=*/NULL);
21953         }
21954     }
21955
21956   /* If the type-id production did not work out, then we must be
21957      looking at the unary-expression production.  */
21958   if (!expr)
21959     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21960                                        /*cast_p=*/false, NULL);
21961
21962   if (pack_expansion_p)
21963     /* Build a pack expansion. */
21964     expr = make_pack_expansion (expr);
21965
21966   /* Go back to evaluating expressions.  */
21967   --cp_unevaluated_operand;
21968   --c_inhibit_evaluation_warnings;
21969
21970   /* Free the message we created.  */
21971   free (tmp);
21972   /* And restore the old one.  */
21973   parser->type_definition_forbidden_message = saved_message;
21974   parser->integral_constant_expression_p
21975     = saved_integral_constant_expression_p;
21976   parser->non_integral_constant_expression_p
21977     = saved_non_integral_constant_expression_p;
21978
21979   return expr;
21980 }
21981
21982 /* If the current declaration has no declarator, return true.  */
21983
21984 static bool
21985 cp_parser_declares_only_class_p (cp_parser *parser)
21986 {
21987   /* If the next token is a `;' or a `,' then there is no
21988      declarator.  */
21989   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21990           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21991 }
21992
21993 /* Update the DECL_SPECS to reflect the storage class indicated by
21994    KEYWORD.  */
21995
21996 static void
21997 cp_parser_set_storage_class (cp_parser *parser,
21998                              cp_decl_specifier_seq *decl_specs,
21999                              enum rid keyword,
22000                              location_t location)
22001 {
22002   cp_storage_class storage_class;
22003
22004   if (parser->in_unbraced_linkage_specification_p)
22005     {
22006       error_at (location, "invalid use of %qD in linkage specification",
22007                 ridpointers[keyword]);
22008       return;
22009     }
22010   else if (decl_specs->storage_class != sc_none)
22011     {
22012       decl_specs->conflicting_specifiers_p = true;
22013       return;
22014     }
22015
22016   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22017       && decl_specs->specs[(int) ds_thread])
22018     {
22019       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22020       decl_specs->specs[(int) ds_thread] = 0;
22021     }
22022
22023   switch (keyword)
22024     {
22025     case RID_AUTO:
22026       storage_class = sc_auto;
22027       break;
22028     case RID_REGISTER:
22029       storage_class = sc_register;
22030       break;
22031     case RID_STATIC:
22032       storage_class = sc_static;
22033       break;
22034     case RID_EXTERN:
22035       storage_class = sc_extern;
22036       break;
22037     case RID_MUTABLE:
22038       storage_class = sc_mutable;
22039       break;
22040     default:
22041       gcc_unreachable ();
22042     }
22043   decl_specs->storage_class = storage_class;
22044
22045   /* A storage class specifier cannot be applied alongside a typedef 
22046      specifier. If there is a typedef specifier present then set 
22047      conflicting_specifiers_p which will trigger an error later
22048      on in grokdeclarator. */
22049   if (decl_specs->specs[(int)ds_typedef])
22050     decl_specs->conflicting_specifiers_p = true;
22051 }
22052
22053 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22054    is true, the type is a class or enum definition.  */
22055
22056 static void
22057 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22058                               tree type_spec,
22059                               location_t location,
22060                               bool type_definition_p)
22061 {
22062   decl_specs->any_specifiers_p = true;
22063
22064   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22065      (with, for example, in "typedef int wchar_t;") we remember that
22066      this is what happened.  In system headers, we ignore these
22067      declarations so that G++ can work with system headers that are not
22068      C++-safe.  */
22069   if (decl_specs->specs[(int) ds_typedef]
22070       && !type_definition_p
22071       && (type_spec == boolean_type_node
22072           || type_spec == char16_type_node
22073           || type_spec == char32_type_node
22074           || type_spec == wchar_type_node)
22075       && (decl_specs->type
22076           || decl_specs->specs[(int) ds_long]
22077           || decl_specs->specs[(int) ds_short]
22078           || decl_specs->specs[(int) ds_unsigned]
22079           || decl_specs->specs[(int) ds_signed]))
22080     {
22081       decl_specs->redefined_builtin_type = type_spec;
22082       if (!decl_specs->type)
22083         {
22084           decl_specs->type = type_spec;
22085           decl_specs->type_definition_p = false;
22086           decl_specs->type_location = location;
22087         }
22088     }
22089   else if (decl_specs->type)
22090     decl_specs->multiple_types_p = true;
22091   else
22092     {
22093       decl_specs->type = type_spec;
22094       decl_specs->type_definition_p = type_definition_p;
22095       decl_specs->redefined_builtin_type = NULL_TREE;
22096       decl_specs->type_location = location;
22097     }
22098 }
22099
22100 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22101    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22102
22103 static bool
22104 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22105 {
22106   return decl_specifiers->specs[(int) ds_friend] != 0;
22107 }
22108
22109 /* Issue an error message indicating that TOKEN_DESC was expected.
22110    If KEYWORD is true, it indicated this function is called by
22111    cp_parser_require_keword and the required token can only be
22112    a indicated keyword. */
22113
22114 static void
22115 cp_parser_required_error (cp_parser *parser,
22116                           required_token token_desc,
22117                           bool keyword)
22118 {
22119   switch (token_desc)
22120     {
22121       case RT_NEW:
22122         cp_parser_error (parser, "expected %<new%>");
22123         return;
22124       case RT_DELETE:
22125         cp_parser_error (parser, "expected %<delete%>");
22126         return;
22127       case RT_RETURN:
22128         cp_parser_error (parser, "expected %<return%>");
22129         return;
22130       case RT_WHILE:
22131         cp_parser_error (parser, "expected %<while%>");
22132         return;
22133       case RT_EXTERN:
22134         cp_parser_error (parser, "expected %<extern%>");
22135         return;
22136       case RT_STATIC_ASSERT:
22137         cp_parser_error (parser, "expected %<static_assert%>");
22138         return;
22139       case RT_DECLTYPE:
22140         cp_parser_error (parser, "expected %<decltype%>");
22141         return;
22142       case RT_OPERATOR:
22143         cp_parser_error (parser, "expected %<operator%>");
22144         return;
22145       case RT_CLASS:
22146         cp_parser_error (parser, "expected %<class%>");
22147         return;
22148       case RT_TEMPLATE:
22149         cp_parser_error (parser, "expected %<template%>");
22150         return;
22151       case RT_NAMESPACE:
22152         cp_parser_error (parser, "expected %<namespace%>");
22153         return;
22154       case RT_USING:
22155         cp_parser_error (parser, "expected %<using%>");
22156         return;
22157       case RT_ASM:
22158         cp_parser_error (parser, "expected %<asm%>");
22159         return;
22160       case RT_TRY:
22161         cp_parser_error (parser, "expected %<try%>");
22162         return;
22163       case RT_CATCH:
22164         cp_parser_error (parser, "expected %<catch%>");
22165         return;
22166       case RT_THROW:
22167         cp_parser_error (parser, "expected %<throw%>");
22168         return;
22169       case RT_LABEL:
22170         cp_parser_error (parser, "expected %<__label__%>");
22171         return;
22172       case RT_AT_TRY:
22173         cp_parser_error (parser, "expected %<@try%>");
22174         return;
22175       case RT_AT_SYNCHRONIZED:
22176         cp_parser_error (parser, "expected %<@synchronized%>");
22177         return;
22178       case RT_AT_THROW:
22179         cp_parser_error (parser, "expected %<@throw%>");
22180         return;
22181       case RT_TRANSACTION_ATOMIC:
22182         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22183         return;
22184       case RT_TRANSACTION_RELAXED:
22185         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22186         return;
22187       default:
22188         break;
22189     }
22190   if (!keyword)
22191     {
22192       switch (token_desc)
22193         {
22194           case RT_SEMICOLON:
22195             cp_parser_error (parser, "expected %<;%>");
22196             return;
22197           case RT_OPEN_PAREN:
22198             cp_parser_error (parser, "expected %<(%>");
22199             return;
22200           case RT_CLOSE_BRACE:
22201             cp_parser_error (parser, "expected %<}%>");
22202             return;
22203           case RT_OPEN_BRACE:
22204             cp_parser_error (parser, "expected %<{%>");
22205             return;
22206           case RT_CLOSE_SQUARE:
22207             cp_parser_error (parser, "expected %<]%>");
22208             return;
22209           case RT_OPEN_SQUARE:
22210             cp_parser_error (parser, "expected %<[%>");
22211             return;
22212           case RT_COMMA:
22213             cp_parser_error (parser, "expected %<,%>");
22214             return;
22215           case RT_SCOPE:
22216             cp_parser_error (parser, "expected %<::%>");
22217             return;
22218           case RT_LESS:
22219             cp_parser_error (parser, "expected %<<%>");
22220             return;
22221           case RT_GREATER:
22222             cp_parser_error (parser, "expected %<>%>");
22223             return;
22224           case RT_EQ:
22225             cp_parser_error (parser, "expected %<=%>");
22226             return;
22227           case RT_ELLIPSIS:
22228             cp_parser_error (parser, "expected %<...%>");
22229             return;
22230           case RT_MULT:
22231             cp_parser_error (parser, "expected %<*%>");
22232             return;
22233           case RT_COMPL:
22234             cp_parser_error (parser, "expected %<~%>");
22235             return;
22236           case RT_COLON:
22237             cp_parser_error (parser, "expected %<:%>");
22238             return;
22239           case RT_COLON_SCOPE:
22240             cp_parser_error (parser, "expected %<:%> or %<::%>");
22241             return;
22242           case RT_CLOSE_PAREN:
22243             cp_parser_error (parser, "expected %<)%>");
22244             return;
22245           case RT_COMMA_CLOSE_PAREN:
22246             cp_parser_error (parser, "expected %<,%> or %<)%>");
22247             return;
22248           case RT_PRAGMA_EOL:
22249             cp_parser_error (parser, "expected end of line");
22250             return;
22251           case RT_NAME:
22252             cp_parser_error (parser, "expected identifier");
22253             return;
22254           case RT_SELECT:
22255             cp_parser_error (parser, "expected selection-statement");
22256             return;
22257           case RT_INTERATION:
22258             cp_parser_error (parser, "expected iteration-statement");
22259             return;
22260           case RT_JUMP:
22261             cp_parser_error (parser, "expected jump-statement");
22262             return;
22263           case RT_CLASS_KEY:
22264             cp_parser_error (parser, "expected class-key");
22265             return;
22266           case RT_CLASS_TYPENAME_TEMPLATE:
22267             cp_parser_error (parser,
22268                  "expected %<class%>, %<typename%>, or %<template%>");
22269             return;
22270           default:
22271             gcc_unreachable ();
22272         }
22273     }
22274   else
22275     gcc_unreachable ();
22276 }
22277
22278
22279
22280 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22281    issue an error message indicating that TOKEN_DESC was expected.
22282
22283    Returns the token consumed, if the token had the appropriate type.
22284    Otherwise, returns NULL.  */
22285
22286 static cp_token *
22287 cp_parser_require (cp_parser* parser,
22288                    enum cpp_ttype type,
22289                    required_token token_desc)
22290 {
22291   if (cp_lexer_next_token_is (parser->lexer, type))
22292     return cp_lexer_consume_token (parser->lexer);
22293   else
22294     {
22295       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22296       if (!cp_parser_simulate_error (parser))
22297         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22298       return NULL;
22299     }
22300 }
22301
22302 /* An error message is produced if the next token is not '>'.
22303    All further tokens are skipped until the desired token is
22304    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22305
22306 static void
22307 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22308 {
22309   /* Current level of '< ... >'.  */
22310   unsigned level = 0;
22311   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22312   unsigned nesting_depth = 0;
22313
22314   /* Are we ready, yet?  If not, issue error message.  */
22315   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22316     return;
22317
22318   /* Skip tokens until the desired token is found.  */
22319   while (true)
22320     {
22321       /* Peek at the next token.  */
22322       switch (cp_lexer_peek_token (parser->lexer)->type)
22323         {
22324         case CPP_LESS:
22325           if (!nesting_depth)
22326             ++level;
22327           break;
22328
22329         case CPP_RSHIFT:
22330           if (cxx_dialect == cxx98)
22331             /* C++0x views the `>>' operator as two `>' tokens, but
22332                C++98 does not. */
22333             break;
22334           else if (!nesting_depth && level-- == 0)
22335             {
22336               /* We've hit a `>>' where the first `>' closes the
22337                  template argument list, and the second `>' is
22338                  spurious.  Just consume the `>>' and stop; we've
22339                  already produced at least one error.  */
22340               cp_lexer_consume_token (parser->lexer);
22341               return;
22342             }
22343           /* Fall through for C++0x, so we handle the second `>' in
22344              the `>>'.  */
22345
22346         case CPP_GREATER:
22347           if (!nesting_depth && level-- == 0)
22348             {
22349               /* We've reached the token we want, consume it and stop.  */
22350               cp_lexer_consume_token (parser->lexer);
22351               return;
22352             }
22353           break;
22354
22355         case CPP_OPEN_PAREN:
22356         case CPP_OPEN_SQUARE:
22357           ++nesting_depth;
22358           break;
22359
22360         case CPP_CLOSE_PAREN:
22361         case CPP_CLOSE_SQUARE:
22362           if (nesting_depth-- == 0)
22363             return;
22364           break;
22365
22366         case CPP_EOF:
22367         case CPP_PRAGMA_EOL:
22368         case CPP_SEMICOLON:
22369         case CPP_OPEN_BRACE:
22370         case CPP_CLOSE_BRACE:
22371           /* The '>' was probably forgotten, don't look further.  */
22372           return;
22373
22374         default:
22375           break;
22376         }
22377
22378       /* Consume this token.  */
22379       cp_lexer_consume_token (parser->lexer);
22380     }
22381 }
22382
22383 /* If the next token is the indicated keyword, consume it.  Otherwise,
22384    issue an error message indicating that TOKEN_DESC was expected.
22385
22386    Returns the token consumed, if the token had the appropriate type.
22387    Otherwise, returns NULL.  */
22388
22389 static cp_token *
22390 cp_parser_require_keyword (cp_parser* parser,
22391                            enum rid keyword,
22392                            required_token token_desc)
22393 {
22394   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22395
22396   if (token && token->keyword != keyword)
22397     {
22398       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22399       return NULL;
22400     }
22401
22402   return token;
22403 }
22404
22405 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22406    function-definition.  */
22407
22408 static bool
22409 cp_parser_token_starts_function_definition_p (cp_token* token)
22410 {
22411   return (/* An ordinary function-body begins with an `{'.  */
22412           token->type == CPP_OPEN_BRACE
22413           /* A ctor-initializer begins with a `:'.  */
22414           || token->type == CPP_COLON
22415           /* A function-try-block begins with `try'.  */
22416           || token->keyword == RID_TRY
22417           /* A function-transaction-block begins with `__transaction_atomic'
22418              or `__transaction_relaxed'.  */
22419           || token->keyword == RID_TRANSACTION_ATOMIC
22420           || token->keyword == RID_TRANSACTION_RELAXED
22421           /* The named return value extension begins with `return'.  */
22422           || token->keyword == RID_RETURN);
22423 }
22424
22425 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22426    definition.  */
22427
22428 static bool
22429 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22430 {
22431   cp_token *token;
22432
22433   token = cp_lexer_peek_token (parser->lexer);
22434   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22435 }
22436
22437 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22438    C++0x) ending a template-argument.  */
22439
22440 static bool
22441 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22442 {
22443   cp_token *token;
22444
22445   token = cp_lexer_peek_token (parser->lexer);
22446   return (token->type == CPP_COMMA 
22447           || token->type == CPP_GREATER
22448           || token->type == CPP_ELLIPSIS
22449           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22450 }
22451
22452 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22453    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22454
22455 static bool
22456 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22457                                                      size_t n)
22458 {
22459   cp_token *token;
22460
22461   token = cp_lexer_peek_nth_token (parser->lexer, n);
22462   if (token->type == CPP_LESS)
22463     return true;
22464   /* Check for the sequence `<::' in the original code. It would be lexed as
22465      `[:', where `[' is a digraph, and there is no whitespace before
22466      `:'.  */
22467   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22468     {
22469       cp_token *token2;
22470       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22471       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22472         return true;
22473     }
22474   return false;
22475 }
22476
22477 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22478    or none_type otherwise.  */
22479
22480 static enum tag_types
22481 cp_parser_token_is_class_key (cp_token* token)
22482 {
22483   switch (token->keyword)
22484     {
22485     case RID_CLASS:
22486       return class_type;
22487     case RID_STRUCT:
22488       return record_type;
22489     case RID_UNION:
22490       return union_type;
22491
22492     default:
22493       return none_type;
22494     }
22495 }
22496
22497 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22498
22499 static void
22500 cp_parser_check_class_key (enum tag_types class_key, tree type)
22501 {
22502   if (type == error_mark_node)
22503     return;
22504   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22505     {
22506       permerror (input_location, "%qs tag used in naming %q#T",
22507                  class_key == union_type ? "union"
22508                  : class_key == record_type ? "struct" : "class",
22509                  type);
22510       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22511               "%q#T was previously declared here", type);
22512     }
22513 }
22514
22515 /* Issue an error message if DECL is redeclared with different
22516    access than its original declaration [class.access.spec/3].
22517    This applies to nested classes and nested class templates.
22518    [class.mem/1].  */
22519
22520 static void
22521 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22522 {
22523   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22524     return;
22525
22526   if ((TREE_PRIVATE (decl)
22527        != (current_access_specifier == access_private_node))
22528       || (TREE_PROTECTED (decl)
22529           != (current_access_specifier == access_protected_node)))
22530     error_at (location, "%qD redeclared with different access", decl);
22531 }
22532
22533 /* Look for the `template' keyword, as a syntactic disambiguator.
22534    Return TRUE iff it is present, in which case it will be
22535    consumed.  */
22536
22537 static bool
22538 cp_parser_optional_template_keyword (cp_parser *parser)
22539 {
22540   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22541     {
22542       /* The `template' keyword can only be used within templates;
22543          outside templates the parser can always figure out what is a
22544          template and what is not.  */
22545       if (!processing_template_decl)
22546         {
22547           cp_token *token = cp_lexer_peek_token (parser->lexer);
22548           error_at (token->location,
22549                     "%<template%> (as a disambiguator) is only allowed "
22550                     "within templates");
22551           /* If this part of the token stream is rescanned, the same
22552              error message would be generated.  So, we purge the token
22553              from the stream.  */
22554           cp_lexer_purge_token (parser->lexer);
22555           return false;
22556         }
22557       else
22558         {
22559           /* Consume the `template' keyword.  */
22560           cp_lexer_consume_token (parser->lexer);
22561           return true;
22562         }
22563     }
22564
22565   return false;
22566 }
22567
22568 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22569    set PARSER->SCOPE, and perform other related actions.  */
22570
22571 static void
22572 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22573 {
22574   int i;
22575   struct tree_check *check_value;
22576   deferred_access_check *chk;
22577   VEC (deferred_access_check,gc) *checks;
22578
22579   /* Get the stored value.  */
22580   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22581   /* Perform any access checks that were deferred.  */
22582   checks = check_value->checks;
22583   if (checks)
22584     {
22585       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22586         perform_or_defer_access_check (chk->binfo,
22587                                        chk->decl,
22588                                        chk->diag_decl);
22589     }
22590   /* Set the scope from the stored value.  */
22591   parser->scope = check_value->value;
22592   parser->qualifying_scope = check_value->qualifying_scope;
22593   parser->object_scope = NULL_TREE;
22594 }
22595
22596 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22597    encounter the end of a block before what we were looking for.  */
22598
22599 static bool
22600 cp_parser_cache_group (cp_parser *parser,
22601                        enum cpp_ttype end,
22602                        unsigned depth)
22603 {
22604   while (true)
22605     {
22606       cp_token *token = cp_lexer_peek_token (parser->lexer);
22607
22608       /* Abort a parenthesized expression if we encounter a semicolon.  */
22609       if ((end == CPP_CLOSE_PAREN || depth == 0)
22610           && token->type == CPP_SEMICOLON)
22611         return true;
22612       /* If we've reached the end of the file, stop.  */
22613       if (token->type == CPP_EOF
22614           || (end != CPP_PRAGMA_EOL
22615               && token->type == CPP_PRAGMA_EOL))
22616         return true;
22617       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22618         /* We've hit the end of an enclosing block, so there's been some
22619            kind of syntax error.  */
22620         return true;
22621
22622       /* Consume the token.  */
22623       cp_lexer_consume_token (parser->lexer);
22624       /* See if it starts a new group.  */
22625       if (token->type == CPP_OPEN_BRACE)
22626         {
22627           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22628           /* In theory this should probably check end == '}', but
22629              cp_parser_save_member_function_body needs it to exit
22630              after either '}' or ')' when called with ')'.  */
22631           if (depth == 0)
22632             return false;
22633         }
22634       else if (token->type == CPP_OPEN_PAREN)
22635         {
22636           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22637           if (depth == 0 && end == CPP_CLOSE_PAREN)
22638             return false;
22639         }
22640       else if (token->type == CPP_PRAGMA)
22641         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22642       else if (token->type == end)
22643         return false;
22644     }
22645 }
22646
22647 /* Like above, for caching a default argument or NSDMI.  Both of these are
22648    terminated by a non-nested comma, but it can be unclear whether or not a
22649    comma is nested in a template argument list unless we do more parsing.
22650    In order to handle this ambiguity, when we encounter a ',' after a '<'
22651    we try to parse what follows as a parameter-declaration-list (in the
22652    case of a default argument) or a member-declarator (in the case of an
22653    NSDMI).  If that succeeds, then we stop caching.  */
22654
22655 static tree
22656 cp_parser_cache_defarg (cp_parser *parser, bool nsdmi)
22657 {
22658   unsigned depth = 0;
22659   int maybe_template_id = 0;
22660   cp_token *first_token;
22661   cp_token *token;
22662   tree default_argument;
22663
22664   /* Add tokens until we have processed the entire default
22665      argument.  We add the range [first_token, token).  */
22666   first_token = cp_lexer_peek_token (parser->lexer);
22667   if (first_token->type == CPP_OPEN_BRACE)
22668     {
22669       /* For list-initialization, this is straightforward.  */
22670       cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
22671       token = cp_lexer_peek_token (parser->lexer);
22672     }
22673   else while (true)
22674     {
22675       bool done = false;
22676
22677       /* Peek at the next token.  */
22678       token = cp_lexer_peek_token (parser->lexer);
22679       /* What we do depends on what token we have.  */
22680       switch (token->type)
22681         {
22682           /* In valid code, a default argument must be
22683              immediately followed by a `,' `)', or `...'.  */
22684         case CPP_COMMA:
22685           if (depth == 0 && maybe_template_id)
22686             {
22687               /* If we've seen a '<', we might be in a
22688                  template-argument-list.  Until Core issue 325 is
22689                  resolved, we don't know how this situation ought
22690                  to be handled, so try to DTRT.  We check whether
22691                  what comes after the comma is a valid parameter
22692                  declaration list.  If it is, then the comma ends
22693                  the default argument; otherwise the default
22694                  argument continues.  */
22695               bool error = false;
22696               tree t;
22697
22698               /* Set ITALP so cp_parser_parameter_declaration_list
22699                  doesn't decide to commit to this parse.  */
22700               bool saved_italp = parser->in_template_argument_list_p;
22701               parser->in_template_argument_list_p = true;
22702
22703               cp_parser_parse_tentatively (parser);
22704               cp_lexer_consume_token (parser->lexer);
22705
22706               if (nsdmi)
22707                 {
22708                   int ctor_dtor_or_conv_p;
22709                   cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
22710                                         &ctor_dtor_or_conv_p,
22711                                         /*parenthesized_p=*/NULL,
22712                                         /*member_p=*/true);
22713                 }
22714               else
22715                 {
22716                   begin_scope (sk_function_parms, NULL_TREE);
22717                   cp_parser_parameter_declaration_list (parser, &error);
22718                   for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
22719                     pop_binding (DECL_NAME (t), t);
22720                   leave_scope ();
22721                 }
22722               if (!cp_parser_error_occurred (parser) && !error)
22723                 done = true;
22724               cp_parser_abort_tentative_parse (parser);
22725
22726               parser->in_template_argument_list_p = saved_italp;
22727               break;
22728             }
22729         case CPP_CLOSE_PAREN:
22730         case CPP_ELLIPSIS:
22731           /* If we run into a non-nested `;', `}', or `]',
22732              then the code is invalid -- but the default
22733              argument is certainly over.  */
22734         case CPP_SEMICOLON:
22735         case CPP_CLOSE_BRACE:
22736         case CPP_CLOSE_SQUARE:
22737           if (depth == 0)
22738             done = true;
22739           /* Update DEPTH, if necessary.  */
22740           else if (token->type == CPP_CLOSE_PAREN
22741                    || token->type == CPP_CLOSE_BRACE
22742                    || token->type == CPP_CLOSE_SQUARE)
22743             --depth;
22744           break;
22745
22746         case CPP_OPEN_PAREN:
22747         case CPP_OPEN_SQUARE:
22748         case CPP_OPEN_BRACE:
22749           ++depth;
22750           break;
22751
22752         case CPP_LESS:
22753           if (depth == 0)
22754             /* This might be the comparison operator, or it might
22755                start a template argument list.  */
22756             ++maybe_template_id;
22757           break;
22758
22759         case CPP_RSHIFT:
22760           if (cxx_dialect == cxx98)
22761             break;
22762           /* Fall through for C++0x, which treats the `>>'
22763              operator like two `>' tokens in certain
22764              cases.  */
22765
22766         case CPP_GREATER:
22767           if (depth == 0)
22768             {
22769               /* This might be an operator, or it might close a
22770                  template argument list.  But if a previous '<'
22771                  started a template argument list, this will have
22772                  closed it, so we can't be in one anymore.  */
22773               maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
22774               if (maybe_template_id < 0)
22775                 maybe_template_id = 0;
22776             }
22777           break;
22778
22779           /* If we run out of tokens, issue an error message.  */
22780         case CPP_EOF:
22781         case CPP_PRAGMA_EOL:
22782           error_at (token->location, "file ends in default argument");
22783           done = true;
22784           break;
22785
22786         case CPP_NAME:
22787         case CPP_SCOPE:
22788           /* In these cases, we should look for template-ids.
22789              For example, if the default argument is
22790              `X<int, double>()', we need to do name lookup to
22791              figure out whether or not `X' is a template; if
22792              so, the `,' does not end the default argument.
22793
22794              That is not yet done.  */
22795           break;
22796
22797         default:
22798           break;
22799         }
22800
22801       /* If we've reached the end, stop.  */
22802       if (done)
22803         break;
22804
22805       /* Add the token to the token block.  */
22806       token = cp_lexer_consume_token (parser->lexer);
22807     }
22808
22809   /* Create a DEFAULT_ARG to represent the unparsed default
22810      argument.  */
22811   default_argument = make_node (DEFAULT_ARG);
22812   DEFARG_TOKENS (default_argument)
22813     = cp_token_cache_new (first_token, token);
22814   DEFARG_INSTANTIATIONS (default_argument) = NULL;
22815
22816   return default_argument;
22817 }
22818
22819 /* Begin parsing tentatively.  We always save tokens while parsing
22820    tentatively so that if the tentative parsing fails we can restore the
22821    tokens.  */
22822
22823 static void
22824 cp_parser_parse_tentatively (cp_parser* parser)
22825 {
22826   /* Enter a new parsing context.  */
22827   parser->context = cp_parser_context_new (parser->context);
22828   /* Begin saving tokens.  */
22829   cp_lexer_save_tokens (parser->lexer);
22830   /* In order to avoid repetitive access control error messages,
22831      access checks are queued up until we are no longer parsing
22832      tentatively.  */
22833   push_deferring_access_checks (dk_deferred);
22834 }
22835
22836 /* Commit to the currently active tentative parse.  */
22837
22838 static void
22839 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22840 {
22841   cp_parser_context *context;
22842   cp_lexer *lexer;
22843
22844   /* Mark all of the levels as committed.  */
22845   lexer = parser->lexer;
22846   for (context = parser->context; context->next; context = context->next)
22847     {
22848       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22849         break;
22850       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22851       while (!cp_lexer_saving_tokens (lexer))
22852         lexer = lexer->next;
22853       cp_lexer_commit_tokens (lexer);
22854     }
22855 }
22856
22857 /* Abort the currently active tentative parse.  All consumed tokens
22858    will be rolled back, and no diagnostics will be issued.  */
22859
22860 static void
22861 cp_parser_abort_tentative_parse (cp_parser* parser)
22862 {
22863   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22864               || errorcount > 0);
22865   cp_parser_simulate_error (parser);
22866   /* Now, pretend that we want to see if the construct was
22867      successfully parsed.  */
22868   cp_parser_parse_definitely (parser);
22869 }
22870
22871 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22872    token stream.  Otherwise, commit to the tokens we have consumed.
22873    Returns true if no error occurred; false otherwise.  */
22874
22875 static bool
22876 cp_parser_parse_definitely (cp_parser* parser)
22877 {
22878   bool error_occurred;
22879   cp_parser_context *context;
22880
22881   /* Remember whether or not an error occurred, since we are about to
22882      destroy that information.  */
22883   error_occurred = cp_parser_error_occurred (parser);
22884   /* Remove the topmost context from the stack.  */
22885   context = parser->context;
22886   parser->context = context->next;
22887   /* If no parse errors occurred, commit to the tentative parse.  */
22888   if (!error_occurred)
22889     {
22890       /* Commit to the tokens read tentatively, unless that was
22891          already done.  */
22892       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22893         cp_lexer_commit_tokens (parser->lexer);
22894
22895       pop_to_parent_deferring_access_checks ();
22896     }
22897   /* Otherwise, if errors occurred, roll back our state so that things
22898      are just as they were before we began the tentative parse.  */
22899   else
22900     {
22901       cp_lexer_rollback_tokens (parser->lexer);
22902       pop_deferring_access_checks ();
22903     }
22904   /* Add the context to the front of the free list.  */
22905   context->next = cp_parser_context_free_list;
22906   cp_parser_context_free_list = context;
22907
22908   return !error_occurred;
22909 }
22910
22911 /* Returns true if we are parsing tentatively and are not committed to
22912    this tentative parse.  */
22913
22914 static bool
22915 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22916 {
22917   return (cp_parser_parsing_tentatively (parser)
22918           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22919 }
22920
22921 /* Returns nonzero iff an error has occurred during the most recent
22922    tentative parse.  */
22923
22924 static bool
22925 cp_parser_error_occurred (cp_parser* parser)
22926 {
22927   return (cp_parser_parsing_tentatively (parser)
22928           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22929 }
22930
22931 /* Returns nonzero if GNU extensions are allowed.  */
22932
22933 static bool
22934 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22935 {
22936   return parser->allow_gnu_extensions_p;
22937 }
22938 \f
22939 /* Objective-C++ Productions */
22940
22941
22942 /* Parse an Objective-C expression, which feeds into a primary-expression
22943    above.
22944
22945    objc-expression:
22946      objc-message-expression
22947      objc-string-literal
22948      objc-encode-expression
22949      objc-protocol-expression
22950      objc-selector-expression
22951
22952   Returns a tree representation of the expression.  */
22953
22954 static tree
22955 cp_parser_objc_expression (cp_parser* parser)
22956 {
22957   /* Try to figure out what kind of declaration is present.  */
22958   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22959
22960   switch (kwd->type)
22961     {
22962     case CPP_OPEN_SQUARE:
22963       return cp_parser_objc_message_expression (parser);
22964
22965     case CPP_OBJC_STRING:
22966       kwd = cp_lexer_consume_token (parser->lexer);
22967       return objc_build_string_object (kwd->u.value);
22968
22969     case CPP_KEYWORD:
22970       switch (kwd->keyword)
22971         {
22972         case RID_AT_ENCODE:
22973           return cp_parser_objc_encode_expression (parser);
22974
22975         case RID_AT_PROTOCOL:
22976           return cp_parser_objc_protocol_expression (parser);
22977
22978         case RID_AT_SELECTOR:
22979           return cp_parser_objc_selector_expression (parser);
22980
22981         default:
22982           break;
22983         }
22984     default:
22985       error_at (kwd->location,
22986                 "misplaced %<@%D%> Objective-C++ construct",
22987                 kwd->u.value);
22988       cp_parser_skip_to_end_of_block_or_statement (parser);
22989     }
22990
22991   return error_mark_node;
22992 }
22993
22994 /* Parse an Objective-C message expression.
22995
22996    objc-message-expression:
22997      [ objc-message-receiver objc-message-args ]
22998
22999    Returns a representation of an Objective-C message.  */
23000
23001 static tree
23002 cp_parser_objc_message_expression (cp_parser* parser)
23003 {
23004   tree receiver, messageargs;
23005
23006   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
23007   receiver = cp_parser_objc_message_receiver (parser);
23008   messageargs = cp_parser_objc_message_args (parser);
23009   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
23010
23011   return objc_build_message_expr (receiver, messageargs);
23012 }
23013
23014 /* Parse an objc-message-receiver.
23015
23016    objc-message-receiver:
23017      expression
23018      simple-type-specifier
23019
23020   Returns a representation of the type or expression.  */
23021
23022 static tree
23023 cp_parser_objc_message_receiver (cp_parser* parser)
23024 {
23025   tree rcv;
23026
23027   /* An Objective-C message receiver may be either (1) a type
23028      or (2) an expression.  */
23029   cp_parser_parse_tentatively (parser);
23030   rcv = cp_parser_expression (parser, false, NULL);
23031
23032   if (cp_parser_parse_definitely (parser))
23033     return rcv;
23034
23035   rcv = cp_parser_simple_type_specifier (parser,
23036                                          /*decl_specs=*/NULL,
23037                                          CP_PARSER_FLAGS_NONE);
23038
23039   return objc_get_class_reference (rcv);
23040 }
23041
23042 /* Parse the arguments and selectors comprising an Objective-C message.
23043
23044    objc-message-args:
23045      objc-selector
23046      objc-selector-args
23047      objc-selector-args , objc-comma-args
23048
23049    objc-selector-args:
23050      objc-selector [opt] : assignment-expression
23051      objc-selector-args objc-selector [opt] : assignment-expression
23052
23053    objc-comma-args:
23054      assignment-expression
23055      objc-comma-args , assignment-expression
23056
23057    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23058    selector arguments and TREE_VALUE containing a list of comma
23059    arguments.  */
23060
23061 static tree
23062 cp_parser_objc_message_args (cp_parser* parser)
23063 {
23064   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23065   bool maybe_unary_selector_p = true;
23066   cp_token *token = cp_lexer_peek_token (parser->lexer);
23067
23068   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23069     {
23070       tree selector = NULL_TREE, arg;
23071
23072       if (token->type != CPP_COLON)
23073         selector = cp_parser_objc_selector (parser);
23074
23075       /* Detect if we have a unary selector.  */
23076       if (maybe_unary_selector_p
23077           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23078         return build_tree_list (selector, NULL_TREE);
23079
23080       maybe_unary_selector_p = false;
23081       cp_parser_require (parser, CPP_COLON, RT_COLON);
23082       arg = cp_parser_assignment_expression (parser, false, NULL);
23083
23084       sel_args
23085         = chainon (sel_args,
23086                    build_tree_list (selector, arg));
23087
23088       token = cp_lexer_peek_token (parser->lexer);
23089     }
23090
23091   /* Handle non-selector arguments, if any. */
23092   while (token->type == CPP_COMMA)
23093     {
23094       tree arg;
23095
23096       cp_lexer_consume_token (parser->lexer);
23097       arg = cp_parser_assignment_expression (parser, false, NULL);
23098
23099       addl_args
23100         = chainon (addl_args,
23101                    build_tree_list (NULL_TREE, arg));
23102
23103       token = cp_lexer_peek_token (parser->lexer);
23104     }
23105
23106   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23107     {
23108       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23109       return build_tree_list (error_mark_node, error_mark_node);
23110     }
23111
23112   return build_tree_list (sel_args, addl_args);
23113 }
23114
23115 /* Parse an Objective-C encode expression.
23116
23117    objc-encode-expression:
23118      @encode objc-typename
23119
23120    Returns an encoded representation of the type argument.  */
23121
23122 static tree
23123 cp_parser_objc_encode_expression (cp_parser* parser)
23124 {
23125   tree type;
23126   cp_token *token;
23127
23128   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
23129   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23130   token = cp_lexer_peek_token (parser->lexer);
23131   type = complete_type (cp_parser_type_id (parser));
23132   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23133
23134   if (!type)
23135     {
23136       error_at (token->location, 
23137                 "%<@encode%> must specify a type as an argument");
23138       return error_mark_node;
23139     }
23140
23141   /* This happens if we find @encode(T) (where T is a template
23142      typename or something dependent on a template typename) when
23143      parsing a template.  In that case, we can't compile it
23144      immediately, but we rather create an AT_ENCODE_EXPR which will
23145      need to be instantiated when the template is used.
23146   */
23147   if (dependent_type_p (type))
23148     {
23149       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23150       TREE_READONLY (value) = 1;
23151       return value;
23152     }
23153
23154   return objc_build_encode_expr (type);
23155 }
23156
23157 /* Parse an Objective-C @defs expression.  */
23158
23159 static tree
23160 cp_parser_objc_defs_expression (cp_parser *parser)
23161 {
23162   tree name;
23163
23164   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
23165   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23166   name = cp_parser_identifier (parser);
23167   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23168
23169   return objc_get_class_ivars (name);
23170 }
23171
23172 /* Parse an Objective-C protocol expression.
23173
23174   objc-protocol-expression:
23175     @protocol ( identifier )
23176
23177   Returns a representation of the protocol expression.  */
23178
23179 static tree
23180 cp_parser_objc_protocol_expression (cp_parser* parser)
23181 {
23182   tree proto;
23183
23184   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23185   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23186   proto = cp_parser_identifier (parser);
23187   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23188
23189   return objc_build_protocol_expr (proto);
23190 }
23191
23192 /* Parse an Objective-C selector expression.
23193
23194    objc-selector-expression:
23195      @selector ( objc-method-signature )
23196
23197    objc-method-signature:
23198      objc-selector
23199      objc-selector-seq
23200
23201    objc-selector-seq:
23202      objc-selector :
23203      objc-selector-seq objc-selector :
23204
23205   Returns a representation of the method selector.  */
23206
23207 static tree
23208 cp_parser_objc_selector_expression (cp_parser* parser)
23209 {
23210   tree sel_seq = NULL_TREE;
23211   bool maybe_unary_selector_p = true;
23212   cp_token *token;
23213   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23214
23215   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23216   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23217   token = cp_lexer_peek_token (parser->lexer);
23218
23219   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23220          || token->type == CPP_SCOPE)
23221     {
23222       tree selector = NULL_TREE;
23223
23224       if (token->type != CPP_COLON
23225           || token->type == CPP_SCOPE)
23226         selector = cp_parser_objc_selector (parser);
23227
23228       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23229           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23230         {
23231           /* Detect if we have a unary selector.  */
23232           if (maybe_unary_selector_p)
23233             {
23234               sel_seq = selector;
23235               goto finish_selector;
23236             }
23237           else
23238             {
23239               cp_parser_error (parser, "expected %<:%>");
23240             }
23241         }
23242       maybe_unary_selector_p = false;
23243       token = cp_lexer_consume_token (parser->lexer);
23244
23245       if (token->type == CPP_SCOPE)
23246         {
23247           sel_seq
23248             = chainon (sel_seq,
23249                        build_tree_list (selector, NULL_TREE));
23250           sel_seq
23251             = chainon (sel_seq,
23252                        build_tree_list (NULL_TREE, NULL_TREE));
23253         }
23254       else
23255         sel_seq
23256           = chainon (sel_seq,
23257                      build_tree_list (selector, NULL_TREE));
23258
23259       token = cp_lexer_peek_token (parser->lexer);
23260     }
23261
23262  finish_selector:
23263   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23264
23265   return objc_build_selector_expr (loc, sel_seq);
23266 }
23267
23268 /* Parse a list of identifiers.
23269
23270    objc-identifier-list:
23271      identifier
23272      objc-identifier-list , identifier
23273
23274    Returns a TREE_LIST of identifier nodes.  */
23275
23276 static tree
23277 cp_parser_objc_identifier_list (cp_parser* parser)
23278 {
23279   tree identifier;
23280   tree list;
23281   cp_token *sep;
23282
23283   identifier = cp_parser_identifier (parser);
23284   if (identifier == error_mark_node)
23285     return error_mark_node;      
23286
23287   list = build_tree_list (NULL_TREE, identifier);
23288   sep = cp_lexer_peek_token (parser->lexer);
23289
23290   while (sep->type == CPP_COMMA)
23291     {
23292       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23293       identifier = cp_parser_identifier (parser);
23294       if (identifier == error_mark_node)
23295         return list;
23296
23297       list = chainon (list, build_tree_list (NULL_TREE,
23298                                              identifier));
23299       sep = cp_lexer_peek_token (parser->lexer);
23300     }
23301   
23302   return list;
23303 }
23304
23305 /* Parse an Objective-C alias declaration.
23306
23307    objc-alias-declaration:
23308      @compatibility_alias identifier identifier ;
23309
23310    This function registers the alias mapping with the Objective-C front end.
23311    It returns nothing.  */
23312
23313 static void
23314 cp_parser_objc_alias_declaration (cp_parser* parser)
23315 {
23316   tree alias, orig;
23317
23318   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23319   alias = cp_parser_identifier (parser);
23320   orig = cp_parser_identifier (parser);
23321   objc_declare_alias (alias, orig);
23322   cp_parser_consume_semicolon_at_end_of_statement (parser);
23323 }
23324
23325 /* Parse an Objective-C class forward-declaration.
23326
23327    objc-class-declaration:
23328      @class objc-identifier-list ;
23329
23330    The function registers the forward declarations with the Objective-C
23331    front end.  It returns nothing.  */
23332
23333 static void
23334 cp_parser_objc_class_declaration (cp_parser* parser)
23335 {
23336   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23337   while (true)
23338     {
23339       tree id;
23340       
23341       id = cp_parser_identifier (parser);
23342       if (id == error_mark_node)
23343         break;
23344       
23345       objc_declare_class (id);
23346
23347       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23348         cp_lexer_consume_token (parser->lexer);
23349       else
23350         break;
23351     }
23352   cp_parser_consume_semicolon_at_end_of_statement (parser);
23353 }
23354
23355 /* Parse a list of Objective-C protocol references.
23356
23357    objc-protocol-refs-opt:
23358      objc-protocol-refs [opt]
23359
23360    objc-protocol-refs:
23361      < objc-identifier-list >
23362
23363    Returns a TREE_LIST of identifiers, if any.  */
23364
23365 static tree
23366 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23367 {
23368   tree protorefs = NULL_TREE;
23369
23370   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23371     {
23372       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23373       protorefs = cp_parser_objc_identifier_list (parser);
23374       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23375     }
23376
23377   return protorefs;
23378 }
23379
23380 /* Parse a Objective-C visibility specification.  */
23381
23382 static void
23383 cp_parser_objc_visibility_spec (cp_parser* parser)
23384 {
23385   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23386
23387   switch (vis->keyword)
23388     {
23389     case RID_AT_PRIVATE:
23390       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23391       break;
23392     case RID_AT_PROTECTED:
23393       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23394       break;
23395     case RID_AT_PUBLIC:
23396       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23397       break;
23398     case RID_AT_PACKAGE:
23399       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23400       break;
23401     default:
23402       return;
23403     }
23404
23405   /* Eat '@private'/'@protected'/'@public'.  */
23406   cp_lexer_consume_token (parser->lexer);
23407 }
23408
23409 /* Parse an Objective-C method type.  Return 'true' if it is a class
23410    (+) method, and 'false' if it is an instance (-) method.  */
23411
23412 static inline bool
23413 cp_parser_objc_method_type (cp_parser* parser)
23414 {
23415   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23416     return true;
23417   else
23418     return false;
23419 }
23420
23421 /* Parse an Objective-C protocol qualifier.  */
23422
23423 static tree
23424 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23425 {
23426   tree quals = NULL_TREE, node;
23427   cp_token *token = cp_lexer_peek_token (parser->lexer);
23428
23429   node = token->u.value;
23430
23431   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23432          && (node == ridpointers [(int) RID_IN]
23433              || node == ridpointers [(int) RID_OUT]
23434              || node == ridpointers [(int) RID_INOUT]
23435              || node == ridpointers [(int) RID_BYCOPY]
23436              || node == ridpointers [(int) RID_BYREF]
23437              || node == ridpointers [(int) RID_ONEWAY]))
23438     {
23439       quals = tree_cons (NULL_TREE, node, quals);
23440       cp_lexer_consume_token (parser->lexer);
23441       token = cp_lexer_peek_token (parser->lexer);
23442       node = token->u.value;
23443     }
23444
23445   return quals;
23446 }
23447
23448 /* Parse an Objective-C typename.  */
23449
23450 static tree
23451 cp_parser_objc_typename (cp_parser* parser)
23452 {
23453   tree type_name = NULL_TREE;
23454
23455   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23456     {
23457       tree proto_quals, cp_type = NULL_TREE;
23458
23459       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23460       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23461
23462       /* An ObjC type name may consist of just protocol qualifiers, in which
23463          case the type shall default to 'id'.  */
23464       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23465         {
23466           cp_type = cp_parser_type_id (parser);
23467           
23468           /* If the type could not be parsed, an error has already
23469              been produced.  For error recovery, behave as if it had
23470              not been specified, which will use the default type
23471              'id'.  */
23472           if (cp_type == error_mark_node)
23473             {
23474               cp_type = NULL_TREE;
23475               /* We need to skip to the closing parenthesis as
23476                  cp_parser_type_id() does not seem to do it for
23477                  us.  */
23478               cp_parser_skip_to_closing_parenthesis (parser,
23479                                                      /*recovering=*/true,
23480                                                      /*or_comma=*/false,
23481                                                      /*consume_paren=*/false);
23482             }
23483         }
23484
23485       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23486       type_name = build_tree_list (proto_quals, cp_type);
23487     }
23488
23489   return type_name;
23490 }
23491
23492 /* Check to see if TYPE refers to an Objective-C selector name.  */
23493
23494 static bool
23495 cp_parser_objc_selector_p (enum cpp_ttype type)
23496 {
23497   return (type == CPP_NAME || type == CPP_KEYWORD
23498           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23499           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23500           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23501           || type == CPP_XOR || type == CPP_XOR_EQ);
23502 }
23503
23504 /* Parse an Objective-C selector.  */
23505
23506 static tree
23507 cp_parser_objc_selector (cp_parser* parser)
23508 {
23509   cp_token *token = cp_lexer_consume_token (parser->lexer);
23510
23511   if (!cp_parser_objc_selector_p (token->type))
23512     {
23513       error_at (token->location, "invalid Objective-C++ selector name");
23514       return error_mark_node;
23515     }
23516
23517   /* C++ operator names are allowed to appear in ObjC selectors.  */
23518   switch (token->type)
23519     {
23520     case CPP_AND_AND: return get_identifier ("and");
23521     case CPP_AND_EQ: return get_identifier ("and_eq");
23522     case CPP_AND: return get_identifier ("bitand");
23523     case CPP_OR: return get_identifier ("bitor");
23524     case CPP_COMPL: return get_identifier ("compl");
23525     case CPP_NOT: return get_identifier ("not");
23526     case CPP_NOT_EQ: return get_identifier ("not_eq");
23527     case CPP_OR_OR: return get_identifier ("or");
23528     case CPP_OR_EQ: return get_identifier ("or_eq");
23529     case CPP_XOR: return get_identifier ("xor");
23530     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23531     default: return token->u.value;
23532     }
23533 }
23534
23535 /* Parse an Objective-C params list.  */
23536
23537 static tree
23538 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23539 {
23540   tree params = NULL_TREE;
23541   bool maybe_unary_selector_p = true;
23542   cp_token *token = cp_lexer_peek_token (parser->lexer);
23543
23544   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23545     {
23546       tree selector = NULL_TREE, type_name, identifier;
23547       tree parm_attr = NULL_TREE;
23548
23549       if (token->keyword == RID_ATTRIBUTE)
23550         break;
23551
23552       if (token->type != CPP_COLON)
23553         selector = cp_parser_objc_selector (parser);
23554
23555       /* Detect if we have a unary selector.  */
23556       if (maybe_unary_selector_p
23557           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23558         {
23559           params = selector; /* Might be followed by attributes.  */
23560           break;
23561         }
23562
23563       maybe_unary_selector_p = false;
23564       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23565         {
23566           /* Something went quite wrong.  There should be a colon
23567              here, but there is not.  Stop parsing parameters.  */
23568           break;
23569         }
23570       type_name = cp_parser_objc_typename (parser);
23571       /* New ObjC allows attributes on parameters too.  */
23572       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23573         parm_attr = cp_parser_attributes_opt (parser);
23574       identifier = cp_parser_identifier (parser);
23575
23576       params
23577         = chainon (params,
23578                    objc_build_keyword_decl (selector,
23579                                             type_name,
23580                                             identifier,
23581                                             parm_attr));
23582
23583       token = cp_lexer_peek_token (parser->lexer);
23584     }
23585
23586   if (params == NULL_TREE)
23587     {
23588       cp_parser_error (parser, "objective-c++ method declaration is expected");
23589       return error_mark_node;
23590     }
23591
23592   /* We allow tail attributes for the method.  */
23593   if (token->keyword == RID_ATTRIBUTE)
23594     {
23595       *attributes = cp_parser_attributes_opt (parser);
23596       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23597           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23598         return params;
23599       cp_parser_error (parser, 
23600                        "method attributes must be specified at the end");
23601       return error_mark_node;
23602     }
23603
23604   if (params == NULL_TREE)
23605     {
23606       cp_parser_error (parser, "objective-c++ method declaration is expected");
23607       return error_mark_node;
23608     }
23609   return params;
23610 }
23611
23612 /* Parse the non-keyword Objective-C params.  */
23613
23614 static tree
23615 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23616                                        tree* attributes)
23617 {
23618   tree params = make_node (TREE_LIST);
23619   cp_token *token = cp_lexer_peek_token (parser->lexer);
23620   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23621
23622   while (token->type == CPP_COMMA)
23623     {
23624       cp_parameter_declarator *parmdecl;
23625       tree parm;
23626
23627       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23628       token = cp_lexer_peek_token (parser->lexer);
23629
23630       if (token->type == CPP_ELLIPSIS)
23631         {
23632           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23633           *ellipsisp = true;
23634           token = cp_lexer_peek_token (parser->lexer);
23635           break;
23636         }
23637
23638       /* TODO: parse attributes for tail parameters.  */
23639       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23640       parm = grokdeclarator (parmdecl->declarator,
23641                              &parmdecl->decl_specifiers,
23642                              PARM, /*initialized=*/0,
23643                              /*attrlist=*/NULL);
23644
23645       chainon (params, build_tree_list (NULL_TREE, parm));
23646       token = cp_lexer_peek_token (parser->lexer);
23647     }
23648
23649   /* We allow tail attributes for the method.  */
23650   if (token->keyword == RID_ATTRIBUTE)
23651     {
23652       if (*attributes == NULL_TREE)
23653         {
23654           *attributes = cp_parser_attributes_opt (parser);
23655           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23656               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23657             return params;
23658         }
23659       else        
23660         /* We have an error, but parse the attributes, so that we can 
23661            carry on.  */
23662         *attributes = cp_parser_attributes_opt (parser);
23663
23664       cp_parser_error (parser, 
23665                        "method attributes must be specified at the end");
23666       return error_mark_node;
23667     }
23668
23669   return params;
23670 }
23671
23672 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23673
23674 static void
23675 cp_parser_objc_interstitial_code (cp_parser* parser)
23676 {
23677   cp_token *token = cp_lexer_peek_token (parser->lexer);
23678
23679   /* If the next token is `extern' and the following token is a string
23680      literal, then we have a linkage specification.  */
23681   if (token->keyword == RID_EXTERN
23682       && cp_parser_is_pure_string_literal
23683          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23684     cp_parser_linkage_specification (parser);
23685   /* Handle #pragma, if any.  */
23686   else if (token->type == CPP_PRAGMA)
23687     cp_parser_pragma (parser, pragma_external);
23688   /* Allow stray semicolons.  */
23689   else if (token->type == CPP_SEMICOLON)
23690     cp_lexer_consume_token (parser->lexer);
23691   /* Mark methods as optional or required, when building protocols.  */
23692   else if (token->keyword == RID_AT_OPTIONAL)
23693     {
23694       cp_lexer_consume_token (parser->lexer);
23695       objc_set_method_opt (true);
23696     }
23697   else if (token->keyword == RID_AT_REQUIRED)
23698     {
23699       cp_lexer_consume_token (parser->lexer);
23700       objc_set_method_opt (false);
23701     }
23702   else if (token->keyword == RID_NAMESPACE)
23703     cp_parser_namespace_definition (parser);
23704   /* Other stray characters must generate errors.  */
23705   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23706     {
23707       cp_lexer_consume_token (parser->lexer);
23708       error ("stray %qs between Objective-C++ methods",
23709              token->type == CPP_OPEN_BRACE ? "{" : "}");
23710     }
23711   /* Finally, try to parse a block-declaration, or a function-definition.  */
23712   else
23713     cp_parser_block_declaration (parser, /*statement_p=*/false);
23714 }
23715
23716 /* Parse a method signature.  */
23717
23718 static tree
23719 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23720 {
23721   tree rettype, kwdparms, optparms;
23722   bool ellipsis = false;
23723   bool is_class_method;
23724
23725   is_class_method = cp_parser_objc_method_type (parser);
23726   rettype = cp_parser_objc_typename (parser);
23727   *attributes = NULL_TREE;
23728   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23729   if (kwdparms == error_mark_node)
23730     return error_mark_node;
23731   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23732   if (optparms == error_mark_node)
23733     return error_mark_node;
23734
23735   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23736 }
23737
23738 static bool
23739 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23740 {
23741   tree tattr;  
23742   cp_lexer_save_tokens (parser->lexer);
23743   tattr = cp_parser_attributes_opt (parser);
23744   gcc_assert (tattr) ;
23745   
23746   /* If the attributes are followed by a method introducer, this is not allowed.
23747      Dump the attributes and flag the situation.  */
23748   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23749       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23750     return true;
23751
23752   /* Otherwise, the attributes introduce some interstitial code, possibly so
23753      rewind to allow that check.  */
23754   cp_lexer_rollback_tokens (parser->lexer);
23755   return false;  
23756 }
23757
23758 /* Parse an Objective-C method prototype list.  */
23759
23760 static void
23761 cp_parser_objc_method_prototype_list (cp_parser* parser)
23762 {
23763   cp_token *token = cp_lexer_peek_token (parser->lexer);
23764
23765   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23766     {
23767       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23768         {
23769           tree attributes, sig;
23770           bool is_class_method;
23771           if (token->type == CPP_PLUS)
23772             is_class_method = true;
23773           else
23774             is_class_method = false;
23775           sig = cp_parser_objc_method_signature (parser, &attributes);
23776           if (sig == error_mark_node)
23777             {
23778               cp_parser_skip_to_end_of_block_or_statement (parser);
23779               token = cp_lexer_peek_token (parser->lexer);
23780               continue;
23781             }
23782           objc_add_method_declaration (is_class_method, sig, attributes);
23783           cp_parser_consume_semicolon_at_end_of_statement (parser);
23784         }
23785       else if (token->keyword == RID_AT_PROPERTY)
23786         cp_parser_objc_at_property_declaration (parser);
23787       else if (token->keyword == RID_ATTRIBUTE 
23788                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23789         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23790                     OPT_Wattributes, 
23791                     "prefix attributes are ignored for methods");
23792       else
23793         /* Allow for interspersed non-ObjC++ code.  */
23794         cp_parser_objc_interstitial_code (parser);
23795
23796       token = cp_lexer_peek_token (parser->lexer);
23797     }
23798
23799   if (token->type != CPP_EOF)
23800     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23801   else
23802     cp_parser_error (parser, "expected %<@end%>");
23803
23804   objc_finish_interface ();
23805 }
23806
23807 /* Parse an Objective-C method definition list.  */
23808
23809 static void
23810 cp_parser_objc_method_definition_list (cp_parser* parser)
23811 {
23812   cp_token *token = cp_lexer_peek_token (parser->lexer);
23813
23814   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23815     {
23816       tree meth;
23817
23818       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23819         {
23820           cp_token *ptk;
23821           tree sig, attribute;
23822           bool is_class_method;
23823           if (token->type == CPP_PLUS)
23824             is_class_method = true;
23825           else
23826             is_class_method = false;
23827           push_deferring_access_checks (dk_deferred);
23828           sig = cp_parser_objc_method_signature (parser, &attribute);
23829           if (sig == error_mark_node)
23830             {
23831               cp_parser_skip_to_end_of_block_or_statement (parser);
23832               token = cp_lexer_peek_token (parser->lexer);
23833               continue;
23834             }
23835           objc_start_method_definition (is_class_method, sig, attribute,
23836                                         NULL_TREE);
23837
23838           /* For historical reasons, we accept an optional semicolon.  */
23839           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23840             cp_lexer_consume_token (parser->lexer);
23841
23842           ptk = cp_lexer_peek_token (parser->lexer);
23843           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23844                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23845             {
23846               perform_deferred_access_checks ();
23847               stop_deferring_access_checks ();
23848               meth = cp_parser_function_definition_after_declarator (parser,
23849                                                                      false);
23850               pop_deferring_access_checks ();
23851               objc_finish_method_definition (meth);
23852             }
23853         }
23854       /* The following case will be removed once @synthesize is
23855          completely implemented.  */
23856       else if (token->keyword == RID_AT_PROPERTY)
23857         cp_parser_objc_at_property_declaration (parser);
23858       else if (token->keyword == RID_AT_SYNTHESIZE)
23859         cp_parser_objc_at_synthesize_declaration (parser);
23860       else if (token->keyword == RID_AT_DYNAMIC)
23861         cp_parser_objc_at_dynamic_declaration (parser);
23862       else if (token->keyword == RID_ATTRIBUTE 
23863                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23864         warning_at (token->location, OPT_Wattributes,
23865                     "prefix attributes are ignored for methods");
23866       else
23867         /* Allow for interspersed non-ObjC++ code.  */
23868         cp_parser_objc_interstitial_code (parser);
23869
23870       token = cp_lexer_peek_token (parser->lexer);
23871     }
23872
23873   if (token->type != CPP_EOF)
23874     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23875   else
23876     cp_parser_error (parser, "expected %<@end%>");
23877
23878   objc_finish_implementation ();
23879 }
23880
23881 /* Parse Objective-C ivars.  */
23882
23883 static void
23884 cp_parser_objc_class_ivars (cp_parser* parser)
23885 {
23886   cp_token *token = cp_lexer_peek_token (parser->lexer);
23887
23888   if (token->type != CPP_OPEN_BRACE)
23889     return;     /* No ivars specified.  */
23890
23891   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23892   token = cp_lexer_peek_token (parser->lexer);
23893
23894   while (token->type != CPP_CLOSE_BRACE 
23895         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23896     {
23897       cp_decl_specifier_seq declspecs;
23898       int decl_class_or_enum_p;
23899       tree prefix_attributes;
23900
23901       cp_parser_objc_visibility_spec (parser);
23902
23903       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23904         break;
23905
23906       cp_parser_decl_specifier_seq (parser,
23907                                     CP_PARSER_FLAGS_OPTIONAL,
23908                                     &declspecs,
23909                                     &decl_class_or_enum_p);
23910
23911       /* auto, register, static, extern, mutable.  */
23912       if (declspecs.storage_class != sc_none)
23913         {
23914           cp_parser_error (parser, "invalid type for instance variable");         
23915           declspecs.storage_class = sc_none;
23916         }
23917
23918       /* __thread.  */
23919       if (declspecs.specs[(int) ds_thread])
23920         {
23921           cp_parser_error (parser, "invalid type for instance variable");
23922           declspecs.specs[(int) ds_thread] = 0;
23923         }
23924       
23925       /* typedef.  */
23926       if (declspecs.specs[(int) ds_typedef])
23927         {
23928           cp_parser_error (parser, "invalid type for instance variable");
23929           declspecs.specs[(int) ds_typedef] = 0;
23930         }
23931
23932       prefix_attributes = declspecs.attributes;
23933       declspecs.attributes = NULL_TREE;
23934
23935       /* Keep going until we hit the `;' at the end of the
23936          declaration.  */
23937       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23938         {
23939           tree width = NULL_TREE, attributes, first_attribute, decl;
23940           cp_declarator *declarator = NULL;
23941           int ctor_dtor_or_conv_p;
23942
23943           /* Check for a (possibly unnamed) bitfield declaration.  */
23944           token = cp_lexer_peek_token (parser->lexer);
23945           if (token->type == CPP_COLON)
23946             goto eat_colon;
23947
23948           if (token->type == CPP_NAME
23949               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23950                   == CPP_COLON))
23951             {
23952               /* Get the name of the bitfield.  */
23953               declarator = make_id_declarator (NULL_TREE,
23954                                                cp_parser_identifier (parser),
23955                                                sfk_none);
23956
23957              eat_colon:
23958               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23959               /* Get the width of the bitfield.  */
23960               width
23961                 = cp_parser_constant_expression (parser,
23962                                                  /*allow_non_constant=*/false,
23963                                                  NULL);
23964             }
23965           else
23966             {
23967               /* Parse the declarator.  */
23968               declarator
23969                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23970                                         &ctor_dtor_or_conv_p,
23971                                         /*parenthesized_p=*/NULL,
23972                                         /*member_p=*/false);
23973             }
23974
23975           /* Look for attributes that apply to the ivar.  */
23976           attributes = cp_parser_attributes_opt (parser);
23977           /* Remember which attributes are prefix attributes and
23978              which are not.  */
23979           first_attribute = attributes;
23980           /* Combine the attributes.  */
23981           attributes = chainon (prefix_attributes, attributes);
23982
23983           if (width)
23984               /* Create the bitfield declaration.  */
23985               decl = grokbitfield (declarator, &declspecs,
23986                                    width,
23987                                    attributes);
23988           else
23989             decl = grokfield (declarator, &declspecs,
23990                               NULL_TREE, /*init_const_expr_p=*/false,
23991                               NULL_TREE, attributes);
23992
23993           /* Add the instance variable.  */
23994           if (decl != error_mark_node && decl != NULL_TREE)
23995             objc_add_instance_variable (decl);
23996
23997           /* Reset PREFIX_ATTRIBUTES.  */
23998           while (attributes && TREE_CHAIN (attributes) != first_attribute)
23999             attributes = TREE_CHAIN (attributes);
24000           if (attributes)
24001             TREE_CHAIN (attributes) = NULL_TREE;
24002
24003           token = cp_lexer_peek_token (parser->lexer);
24004
24005           if (token->type == CPP_COMMA)
24006             {
24007               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24008               continue;
24009             }
24010           break;
24011         }
24012
24013       cp_parser_consume_semicolon_at_end_of_statement (parser);
24014       token = cp_lexer_peek_token (parser->lexer);
24015     }
24016
24017   if (token->keyword == RID_AT_END)
24018     cp_parser_error (parser, "expected %<}%>");
24019
24020   /* Do not consume the RID_AT_END, so it will be read again as terminating
24021      the @interface of @implementation.  */ 
24022   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
24023     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
24024     
24025   /* For historical reasons, we accept an optional semicolon.  */
24026   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24027     cp_lexer_consume_token (parser->lexer);
24028 }
24029
24030 /* Parse an Objective-C protocol declaration.  */
24031
24032 static void
24033 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
24034 {
24035   tree proto, protorefs;
24036   cp_token *tok;
24037
24038   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24039   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24040     {
24041       tok = cp_lexer_peek_token (parser->lexer);
24042       error_at (tok->location, "identifier expected after %<@protocol%>");
24043       cp_parser_consume_semicolon_at_end_of_statement (parser);
24044       return;
24045     }
24046
24047   /* See if we have a forward declaration or a definition.  */
24048   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24049
24050   /* Try a forward declaration first.  */
24051   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24052     {
24053       while (true)
24054         {
24055           tree id;
24056           
24057           id = cp_parser_identifier (parser);
24058           if (id == error_mark_node)
24059             break;
24060           
24061           objc_declare_protocol (id, attributes);
24062           
24063           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24064             cp_lexer_consume_token (parser->lexer);
24065           else
24066             break;
24067         }
24068       cp_parser_consume_semicolon_at_end_of_statement (parser);
24069     }
24070
24071   /* Ok, we got a full-fledged definition (or at least should).  */
24072   else
24073     {
24074       proto = cp_parser_identifier (parser);
24075       protorefs = cp_parser_objc_protocol_refs_opt (parser);
24076       objc_start_protocol (proto, protorefs, attributes);
24077       cp_parser_objc_method_prototype_list (parser);
24078     }
24079 }
24080
24081 /* Parse an Objective-C superclass or category.  */
24082
24083 static void
24084 cp_parser_objc_superclass_or_category (cp_parser *parser, 
24085                                        bool iface_p,
24086                                        tree *super,
24087                                        tree *categ, bool *is_class_extension)
24088 {
24089   cp_token *next = cp_lexer_peek_token (parser->lexer);
24090
24091   *super = *categ = NULL_TREE;
24092   *is_class_extension = false;
24093   if (next->type == CPP_COLON)
24094     {
24095       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24096       *super = cp_parser_identifier (parser);
24097     }
24098   else if (next->type == CPP_OPEN_PAREN)
24099     {
24100       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24101
24102       /* If there is no category name, and this is an @interface, we
24103          have a class extension.  */
24104       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24105         {
24106           *categ = NULL_TREE;
24107           *is_class_extension = true;
24108         }
24109       else
24110         *categ = cp_parser_identifier (parser);
24111
24112       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24113     }
24114 }
24115
24116 /* Parse an Objective-C class interface.  */
24117
24118 static void
24119 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24120 {
24121   tree name, super, categ, protos;
24122   bool is_class_extension;
24123
24124   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
24125   name = cp_parser_identifier (parser);
24126   if (name == error_mark_node)
24127     {
24128       /* It's hard to recover because even if valid @interface stuff
24129          is to follow, we can't compile it (or validate it) if we
24130          don't even know which class it refers to.  Let's assume this
24131          was a stray '@interface' token in the stream and skip it.
24132       */
24133       return;
24134     }
24135   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24136                                          &is_class_extension);
24137   protos = cp_parser_objc_protocol_refs_opt (parser);
24138
24139   /* We have either a class or a category on our hands.  */
24140   if (categ || is_class_extension)
24141     objc_start_category_interface (name, categ, protos, attributes);
24142   else
24143     {
24144       objc_start_class_interface (name, super, protos, attributes);
24145       /* Handle instance variable declarations, if any.  */
24146       cp_parser_objc_class_ivars (parser);
24147       objc_continue_interface ();
24148     }
24149
24150   cp_parser_objc_method_prototype_list (parser);
24151 }
24152
24153 /* Parse an Objective-C class implementation.  */
24154
24155 static void
24156 cp_parser_objc_class_implementation (cp_parser* parser)
24157 {
24158   tree name, super, categ;
24159   bool is_class_extension;
24160
24161   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
24162   name = cp_parser_identifier (parser);
24163   if (name == error_mark_node)
24164     {
24165       /* It's hard to recover because even if valid @implementation
24166          stuff is to follow, we can't compile it (or validate it) if
24167          we don't even know which class it refers to.  Let's assume
24168          this was a stray '@implementation' token in the stream and
24169          skip it.
24170       */
24171       return;
24172     }
24173   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24174                                          &is_class_extension);
24175
24176   /* We have either a class or a category on our hands.  */
24177   if (categ)
24178     objc_start_category_implementation (name, categ);
24179   else
24180     {
24181       objc_start_class_implementation (name, super);
24182       /* Handle instance variable declarations, if any.  */
24183       cp_parser_objc_class_ivars (parser);
24184       objc_continue_implementation ();
24185     }
24186
24187   cp_parser_objc_method_definition_list (parser);
24188 }
24189
24190 /* Consume the @end token and finish off the implementation.  */
24191
24192 static void
24193 cp_parser_objc_end_implementation (cp_parser* parser)
24194 {
24195   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24196   objc_finish_implementation ();
24197 }
24198
24199 /* Parse an Objective-C declaration.  */
24200
24201 static void
24202 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24203 {
24204   /* Try to figure out what kind of declaration is present.  */
24205   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24206
24207   if (attributes)
24208     switch (kwd->keyword)
24209       {
24210         case RID_AT_ALIAS:
24211         case RID_AT_CLASS:
24212         case RID_AT_END:
24213           error_at (kwd->location, "attributes may not be specified before"
24214                     " the %<@%D%> Objective-C++ keyword",
24215                     kwd->u.value);
24216           attributes = NULL;
24217           break;
24218         case RID_AT_IMPLEMENTATION:
24219           warning_at (kwd->location, OPT_Wattributes,
24220                       "prefix attributes are ignored before %<@%D%>",
24221                       kwd->u.value);
24222           attributes = NULL;
24223         default:
24224           break;
24225       }
24226
24227   switch (kwd->keyword)
24228     {
24229     case RID_AT_ALIAS:
24230       cp_parser_objc_alias_declaration (parser);
24231       break;
24232     case RID_AT_CLASS:
24233       cp_parser_objc_class_declaration (parser);
24234       break;
24235     case RID_AT_PROTOCOL:
24236       cp_parser_objc_protocol_declaration (parser, attributes);
24237       break;
24238     case RID_AT_INTERFACE:
24239       cp_parser_objc_class_interface (parser, attributes);
24240       break;
24241     case RID_AT_IMPLEMENTATION:
24242       cp_parser_objc_class_implementation (parser);
24243       break;
24244     case RID_AT_END:
24245       cp_parser_objc_end_implementation (parser);
24246       break;
24247     default:
24248       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24249                 kwd->u.value);
24250       cp_parser_skip_to_end_of_block_or_statement (parser);
24251     }
24252 }
24253
24254 /* Parse an Objective-C try-catch-finally statement.
24255
24256    objc-try-catch-finally-stmt:
24257      @try compound-statement objc-catch-clause-seq [opt]
24258        objc-finally-clause [opt]
24259
24260    objc-catch-clause-seq:
24261      objc-catch-clause objc-catch-clause-seq [opt]
24262
24263    objc-catch-clause:
24264      @catch ( objc-exception-declaration ) compound-statement
24265
24266    objc-finally-clause:
24267      @finally compound-statement
24268
24269    objc-exception-declaration:
24270      parameter-declaration
24271      '...'
24272
24273    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24274
24275    Returns NULL_TREE.
24276
24277    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24278    for C.  Keep them in sync.  */   
24279
24280 static tree
24281 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24282 {
24283   location_t location;
24284   tree stmt;
24285
24286   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24287   location = cp_lexer_peek_token (parser->lexer)->location;
24288   objc_maybe_warn_exceptions (location);
24289   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24290      node, lest it get absorbed into the surrounding block.  */
24291   stmt = push_stmt_list ();
24292   cp_parser_compound_statement (parser, NULL, false, false);
24293   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24294
24295   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24296     {
24297       cp_parameter_declarator *parm;
24298       tree parameter_declaration = error_mark_node;
24299       bool seen_open_paren = false;
24300
24301       cp_lexer_consume_token (parser->lexer);
24302       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24303         seen_open_paren = true;
24304       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24305         {
24306           /* We have "@catch (...)" (where the '...' are literally
24307              what is in the code).  Skip the '...'.
24308              parameter_declaration is set to NULL_TREE, and
24309              objc_being_catch_clauses() knows that that means
24310              '...'.  */
24311           cp_lexer_consume_token (parser->lexer);
24312           parameter_declaration = NULL_TREE;
24313         }
24314       else
24315         {
24316           /* We have "@catch (NSException *exception)" or something
24317              like that.  Parse the parameter declaration.  */
24318           parm = cp_parser_parameter_declaration (parser, false, NULL);
24319           if (parm == NULL)
24320             parameter_declaration = error_mark_node;
24321           else
24322             parameter_declaration = grokdeclarator (parm->declarator,
24323                                                     &parm->decl_specifiers,
24324                                                     PARM, /*initialized=*/0,
24325                                                     /*attrlist=*/NULL);
24326         }
24327       if (seen_open_paren)
24328         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24329       else
24330         {
24331           /* If there was no open parenthesis, we are recovering from
24332              an error, and we are trying to figure out what mistake
24333              the user has made.  */
24334
24335           /* If there is an immediate closing parenthesis, the user
24336              probably forgot the opening one (ie, they typed "@catch
24337              NSException *e)".  Parse the closing parenthesis and keep
24338              going.  */
24339           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24340             cp_lexer_consume_token (parser->lexer);
24341           
24342           /* If these is no immediate closing parenthesis, the user
24343              probably doesn't know that parenthesis are required at
24344              all (ie, they typed "@catch NSException *e").  So, just
24345              forget about the closing parenthesis and keep going.  */
24346         }
24347       objc_begin_catch_clause (parameter_declaration);
24348       cp_parser_compound_statement (parser, NULL, false, false);
24349       objc_finish_catch_clause ();
24350     }
24351   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24352     {
24353       cp_lexer_consume_token (parser->lexer);
24354       location = cp_lexer_peek_token (parser->lexer)->location;
24355       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24356          node, lest it get absorbed into the surrounding block.  */
24357       stmt = push_stmt_list ();
24358       cp_parser_compound_statement (parser, NULL, false, false);
24359       objc_build_finally_clause (location, pop_stmt_list (stmt));
24360     }
24361
24362   return objc_finish_try_stmt ();
24363 }
24364
24365 /* Parse an Objective-C synchronized statement.
24366
24367    objc-synchronized-stmt:
24368      @synchronized ( expression ) compound-statement
24369
24370    Returns NULL_TREE.  */
24371
24372 static tree
24373 cp_parser_objc_synchronized_statement (cp_parser *parser)
24374 {
24375   location_t location;
24376   tree lock, stmt;
24377
24378   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24379
24380   location = cp_lexer_peek_token (parser->lexer)->location;
24381   objc_maybe_warn_exceptions (location);
24382   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24383   lock = cp_parser_expression (parser, false, NULL);
24384   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24385
24386   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24387      node, lest it get absorbed into the surrounding block.  */
24388   stmt = push_stmt_list ();
24389   cp_parser_compound_statement (parser, NULL, false, false);
24390
24391   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24392 }
24393
24394 /* Parse an Objective-C throw statement.
24395
24396    objc-throw-stmt:
24397      @throw assignment-expression [opt] ;
24398
24399    Returns a constructed '@throw' statement.  */
24400
24401 static tree
24402 cp_parser_objc_throw_statement (cp_parser *parser)
24403 {
24404   tree expr = NULL_TREE;
24405   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24406
24407   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24408
24409   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24410     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24411
24412   cp_parser_consume_semicolon_at_end_of_statement (parser);
24413
24414   return objc_build_throw_stmt (loc, expr);
24415 }
24416
24417 /* Parse an Objective-C statement.  */
24418
24419 static tree
24420 cp_parser_objc_statement (cp_parser * parser)
24421 {
24422   /* Try to figure out what kind of declaration is present.  */
24423   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24424
24425   switch (kwd->keyword)
24426     {
24427     case RID_AT_TRY:
24428       return cp_parser_objc_try_catch_finally_statement (parser);
24429     case RID_AT_SYNCHRONIZED:
24430       return cp_parser_objc_synchronized_statement (parser);
24431     case RID_AT_THROW:
24432       return cp_parser_objc_throw_statement (parser);
24433     default:
24434       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24435                kwd->u.value);
24436       cp_parser_skip_to_end_of_block_or_statement (parser);
24437     }
24438
24439   return error_mark_node;
24440 }
24441
24442 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24443    look ahead to see if an objc keyword follows the attributes.  This
24444    is to detect the use of prefix attributes on ObjC @interface and 
24445    @protocol.  */
24446
24447 static bool
24448 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24449 {
24450   cp_lexer_save_tokens (parser->lexer);
24451   *attrib = cp_parser_attributes_opt (parser);
24452   gcc_assert (*attrib);
24453   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24454     {
24455       cp_lexer_commit_tokens (parser->lexer);
24456       return true;
24457     }
24458   cp_lexer_rollback_tokens (parser->lexer);
24459   return false;  
24460 }
24461
24462 /* This routine is a minimal replacement for
24463    c_parser_struct_declaration () used when parsing the list of
24464    types/names or ObjC++ properties.  For example, when parsing the
24465    code
24466
24467    @property (readonly) int a, b, c;
24468
24469    this function is responsible for parsing "int a, int b, int c" and
24470    returning the declarations as CHAIN of DECLs.
24471
24472    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24473    similar parsing.  */
24474 static tree
24475 cp_parser_objc_struct_declaration (cp_parser *parser)
24476 {
24477   tree decls = NULL_TREE;
24478   cp_decl_specifier_seq declspecs;
24479   int decl_class_or_enum_p;
24480   tree prefix_attributes;
24481
24482   cp_parser_decl_specifier_seq (parser,
24483                                 CP_PARSER_FLAGS_NONE,
24484                                 &declspecs,
24485                                 &decl_class_or_enum_p);
24486
24487   if (declspecs.type == error_mark_node)
24488     return error_mark_node;
24489
24490   /* auto, register, static, extern, mutable.  */
24491   if (declspecs.storage_class != sc_none)
24492     {
24493       cp_parser_error (parser, "invalid type for property");
24494       declspecs.storage_class = sc_none;
24495     }
24496   
24497   /* __thread.  */
24498   if (declspecs.specs[(int) ds_thread])
24499     {
24500       cp_parser_error (parser, "invalid type for property");
24501       declspecs.specs[(int) ds_thread] = 0;
24502     }
24503   
24504   /* typedef.  */
24505   if (declspecs.specs[(int) ds_typedef])
24506     {
24507       cp_parser_error (parser, "invalid type for property");
24508       declspecs.specs[(int) ds_typedef] = 0;
24509     }
24510
24511   prefix_attributes = declspecs.attributes;
24512   declspecs.attributes = NULL_TREE;
24513
24514   /* Keep going until we hit the `;' at the end of the declaration. */
24515   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24516     {
24517       tree attributes, first_attribute, decl;
24518       cp_declarator *declarator;
24519       cp_token *token;
24520
24521       /* Parse the declarator.  */
24522       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24523                                          NULL, NULL, false);
24524
24525       /* Look for attributes that apply to the ivar.  */
24526       attributes = cp_parser_attributes_opt (parser);
24527       /* Remember which attributes are prefix attributes and
24528          which are not.  */
24529       first_attribute = attributes;
24530       /* Combine the attributes.  */
24531       attributes = chainon (prefix_attributes, attributes);
24532       
24533       decl = grokfield (declarator, &declspecs,
24534                         NULL_TREE, /*init_const_expr_p=*/false,
24535                         NULL_TREE, attributes);
24536
24537       if (decl == error_mark_node || decl == NULL_TREE)
24538         return error_mark_node;
24539       
24540       /* Reset PREFIX_ATTRIBUTES.  */
24541       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24542         attributes = TREE_CHAIN (attributes);
24543       if (attributes)
24544         TREE_CHAIN (attributes) = NULL_TREE;
24545
24546       DECL_CHAIN (decl) = decls;
24547       decls = decl;
24548
24549       token = cp_lexer_peek_token (parser->lexer);
24550       if (token->type == CPP_COMMA)
24551         {
24552           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24553           continue;
24554         }
24555       else
24556         break;
24557     }
24558   return decls;
24559 }
24560
24561 /* Parse an Objective-C @property declaration.  The syntax is:
24562
24563    objc-property-declaration:
24564      '@property' objc-property-attributes[opt] struct-declaration ;
24565
24566    objc-property-attributes:
24567     '(' objc-property-attribute-list ')'
24568
24569    objc-property-attribute-list:
24570      objc-property-attribute
24571      objc-property-attribute-list, objc-property-attribute
24572
24573    objc-property-attribute
24574      'getter' = identifier
24575      'setter' = identifier
24576      'readonly'
24577      'readwrite'
24578      'assign'
24579      'retain'
24580      'copy'
24581      'nonatomic'
24582
24583   For example:
24584     @property NSString *name;
24585     @property (readonly) id object;
24586     @property (retain, nonatomic, getter=getTheName) id name;
24587     @property int a, b, c;
24588
24589    PS: This function is identical to
24590    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24591 static void 
24592 cp_parser_objc_at_property_declaration (cp_parser *parser)
24593 {
24594   /* The following variables hold the attributes of the properties as
24595      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24596      seen.  When we see an attribute, we set them to 'true' (if they
24597      are boolean properties) or to the identifier (if they have an
24598      argument, ie, for getter and setter).  Note that here we only
24599      parse the list of attributes, check the syntax and accumulate the
24600      attributes that we find.  objc_add_property_declaration() will
24601      then process the information.  */
24602   bool property_assign = false;
24603   bool property_copy = false;
24604   tree property_getter_ident = NULL_TREE;
24605   bool property_nonatomic = false;
24606   bool property_readonly = false;
24607   bool property_readwrite = false;
24608   bool property_retain = false;
24609   tree property_setter_ident = NULL_TREE;
24610
24611   /* 'properties' is the list of properties that we read.  Usually a
24612      single one, but maybe more (eg, in "@property int a, b, c;" there
24613      are three).  */
24614   tree properties;
24615   location_t loc;
24616
24617   loc = cp_lexer_peek_token (parser->lexer)->location;
24618
24619   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24620
24621   /* Parse the optional attribute list...  */
24622   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24623     {
24624       /* Eat the '('.  */
24625       cp_lexer_consume_token (parser->lexer);
24626
24627       while (true)
24628         {
24629           bool syntax_error = false;
24630           cp_token *token = cp_lexer_peek_token (parser->lexer);
24631           enum rid keyword;
24632
24633           if (token->type != CPP_NAME)
24634             {
24635               cp_parser_error (parser, "expected identifier");
24636               break;
24637             }
24638           keyword = C_RID_CODE (token->u.value);
24639           cp_lexer_consume_token (parser->lexer);
24640           switch (keyword)
24641             {
24642             case RID_ASSIGN:    property_assign = true;    break;
24643             case RID_COPY:      property_copy = true;      break;
24644             case RID_NONATOMIC: property_nonatomic = true; break;
24645             case RID_READONLY:  property_readonly = true;  break;
24646             case RID_READWRITE: property_readwrite = true; break;
24647             case RID_RETAIN:    property_retain = true;    break;
24648
24649             case RID_GETTER:
24650             case RID_SETTER:
24651               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24652                 {
24653                   if (keyword == RID_GETTER)
24654                     cp_parser_error (parser,
24655                                      "missing %<=%> (after %<getter%> attribute)");
24656                   else
24657                     cp_parser_error (parser,
24658                                      "missing %<=%> (after %<setter%> attribute)");
24659                   syntax_error = true;
24660                   break;
24661                 }
24662               cp_lexer_consume_token (parser->lexer); /* eat the = */
24663               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24664                 {
24665                   cp_parser_error (parser, "expected identifier");
24666                   syntax_error = true;
24667                   break;
24668                 }
24669               if (keyword == RID_SETTER)
24670                 {
24671                   if (property_setter_ident != NULL_TREE)
24672                     {
24673                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24674                       cp_lexer_consume_token (parser->lexer);
24675                     }
24676                   else
24677                     property_setter_ident = cp_parser_objc_selector (parser);
24678                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24679                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24680                   else
24681                     cp_lexer_consume_token (parser->lexer);
24682                 }
24683               else
24684                 {
24685                   if (property_getter_ident != NULL_TREE)
24686                     {
24687                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24688                       cp_lexer_consume_token (parser->lexer);
24689                     }
24690                   else
24691                     property_getter_ident = cp_parser_objc_selector (parser);
24692                 }
24693               break;
24694             default:
24695               cp_parser_error (parser, "unknown property attribute");
24696               syntax_error = true;
24697               break;
24698             }
24699
24700           if (syntax_error)
24701             break;
24702
24703           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24704             cp_lexer_consume_token (parser->lexer);
24705           else
24706             break;
24707         }
24708
24709       /* FIXME: "@property (setter, assign);" will generate a spurious
24710          "error: expected â€˜)’ before â€˜,’ token".  This is because
24711          cp_parser_require, unlike the C counterpart, will produce an
24712          error even if we are in error recovery.  */
24713       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24714         {
24715           cp_parser_skip_to_closing_parenthesis (parser,
24716                                                  /*recovering=*/true,
24717                                                  /*or_comma=*/false,
24718                                                  /*consume_paren=*/true);
24719         }
24720     }
24721
24722   /* ... and the property declaration(s).  */
24723   properties = cp_parser_objc_struct_declaration (parser);
24724
24725   if (properties == error_mark_node)
24726     {
24727       cp_parser_skip_to_end_of_statement (parser);
24728       /* If the next token is now a `;', consume it.  */
24729       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24730         cp_lexer_consume_token (parser->lexer);
24731       return;
24732     }
24733
24734   if (properties == NULL_TREE)
24735     cp_parser_error (parser, "expected identifier");
24736   else
24737     {
24738       /* Comma-separated properties are chained together in
24739          reverse order; add them one by one.  */
24740       properties = nreverse (properties);
24741       
24742       for (; properties; properties = TREE_CHAIN (properties))
24743         objc_add_property_declaration (loc, copy_node (properties),
24744                                        property_readonly, property_readwrite,
24745                                        property_assign, property_retain,
24746                                        property_copy, property_nonatomic,
24747                                        property_getter_ident, property_setter_ident);
24748     }
24749   
24750   cp_parser_consume_semicolon_at_end_of_statement (parser);
24751 }
24752
24753 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24754
24755    objc-synthesize-declaration:
24756      @synthesize objc-synthesize-identifier-list ;
24757
24758    objc-synthesize-identifier-list:
24759      objc-synthesize-identifier
24760      objc-synthesize-identifier-list, objc-synthesize-identifier
24761
24762    objc-synthesize-identifier
24763      identifier
24764      identifier = identifier
24765
24766   For example:
24767     @synthesize MyProperty;
24768     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24769
24770   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24771   for C.  Keep them in sync.
24772 */
24773 static void 
24774 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24775 {
24776   tree list = NULL_TREE;
24777   location_t loc;
24778   loc = cp_lexer_peek_token (parser->lexer)->location;
24779
24780   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24781   while (true)
24782     {
24783       tree property, ivar;
24784       property = cp_parser_identifier (parser);
24785       if (property == error_mark_node)
24786         {
24787           cp_parser_consume_semicolon_at_end_of_statement (parser);
24788           return;
24789         }
24790       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24791         {
24792           cp_lexer_consume_token (parser->lexer);
24793           ivar = cp_parser_identifier (parser);
24794           if (ivar == error_mark_node)
24795             {
24796               cp_parser_consume_semicolon_at_end_of_statement (parser);
24797               return;
24798             }
24799         }
24800       else
24801         ivar = NULL_TREE;
24802       list = chainon (list, build_tree_list (ivar, property));
24803       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24804         cp_lexer_consume_token (parser->lexer);
24805       else
24806         break;
24807     }
24808   cp_parser_consume_semicolon_at_end_of_statement (parser);
24809   objc_add_synthesize_declaration (loc, list);
24810 }
24811
24812 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24813
24814    objc-dynamic-declaration:
24815      @dynamic identifier-list ;
24816
24817    For example:
24818      @dynamic MyProperty;
24819      @dynamic MyProperty, AnotherProperty;
24820
24821   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24822   for C.  Keep them in sync.
24823 */
24824 static void 
24825 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24826 {
24827   tree list = NULL_TREE;
24828   location_t loc;
24829   loc = cp_lexer_peek_token (parser->lexer)->location;
24830
24831   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24832   while (true)
24833     {
24834       tree property;
24835       property = cp_parser_identifier (parser);
24836       if (property == error_mark_node)
24837         {
24838           cp_parser_consume_semicolon_at_end_of_statement (parser);
24839           return;
24840         }
24841       list = chainon (list, build_tree_list (NULL, property));
24842       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24843         cp_lexer_consume_token (parser->lexer);
24844       else
24845         break;
24846     }
24847   cp_parser_consume_semicolon_at_end_of_statement (parser);
24848   objc_add_dynamic_declaration (loc, list);
24849 }
24850
24851 \f
24852 /* OpenMP 2.5 parsing routines.  */
24853
24854 /* Returns name of the next clause.
24855    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24856    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24857    returned and the token is consumed.  */
24858
24859 static pragma_omp_clause
24860 cp_parser_omp_clause_name (cp_parser *parser)
24861 {
24862   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24863
24864   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24865     result = PRAGMA_OMP_CLAUSE_IF;
24866   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24867     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24868   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24869     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24870   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24871     {
24872       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24873       const char *p = IDENTIFIER_POINTER (id);
24874
24875       switch (p[0])
24876         {
24877         case 'c':
24878           if (!strcmp ("collapse", p))
24879             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24880           else if (!strcmp ("copyin", p))
24881             result = PRAGMA_OMP_CLAUSE_COPYIN;
24882           else if (!strcmp ("copyprivate", p))
24883             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24884           break;
24885         case 'f':
24886           if (!strcmp ("final", p))
24887             result = PRAGMA_OMP_CLAUSE_FINAL;
24888           else if (!strcmp ("firstprivate", p))
24889             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24890           break;
24891         case 'l':
24892           if (!strcmp ("lastprivate", p))
24893             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24894           break;
24895         case 'm':
24896           if (!strcmp ("mergeable", p))
24897             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24898           break;
24899         case 'n':
24900           if (!strcmp ("nowait", p))
24901             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24902           else if (!strcmp ("num_threads", p))
24903             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24904           break;
24905         case 'o':
24906           if (!strcmp ("ordered", p))
24907             result = PRAGMA_OMP_CLAUSE_ORDERED;
24908           break;
24909         case 'r':
24910           if (!strcmp ("reduction", p))
24911             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24912           break;
24913         case 's':
24914           if (!strcmp ("schedule", p))
24915             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24916           else if (!strcmp ("shared", p))
24917             result = PRAGMA_OMP_CLAUSE_SHARED;
24918           break;
24919         case 'u':
24920           if (!strcmp ("untied", p))
24921             result = PRAGMA_OMP_CLAUSE_UNTIED;
24922           break;
24923         }
24924     }
24925
24926   if (result != PRAGMA_OMP_CLAUSE_NONE)
24927     cp_lexer_consume_token (parser->lexer);
24928
24929   return result;
24930 }
24931
24932 /* Validate that a clause of the given type does not already exist.  */
24933
24934 static void
24935 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24936                            const char *name, location_t location)
24937 {
24938   tree c;
24939
24940   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24941     if (OMP_CLAUSE_CODE (c) == code)
24942       {
24943         error_at (location, "too many %qs clauses", name);
24944         break;
24945       }
24946 }
24947
24948 /* OpenMP 2.5:
24949    variable-list:
24950      identifier
24951      variable-list , identifier
24952
24953    In addition, we match a closing parenthesis.  An opening parenthesis
24954    will have been consumed by the caller.
24955
24956    If KIND is nonzero, create the appropriate node and install the decl
24957    in OMP_CLAUSE_DECL and add the node to the head of the list.
24958
24959    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24960    return the list created.  */
24961
24962 static tree
24963 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24964                                 tree list)
24965 {
24966   cp_token *token;
24967   while (1)
24968     {
24969       tree name, decl;
24970
24971       token = cp_lexer_peek_token (parser->lexer);
24972       name = cp_parser_id_expression (parser, /*template_p=*/false,
24973                                       /*check_dependency_p=*/true,
24974                                       /*template_p=*/NULL,
24975                                       /*declarator_p=*/false,
24976                                       /*optional_p=*/false);
24977       if (name == error_mark_node)
24978         goto skip_comma;
24979
24980       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24981       if (decl == error_mark_node)
24982         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24983                                      token->location);
24984       else if (kind != 0)
24985         {
24986           tree u = build_omp_clause (token->location, kind);
24987           OMP_CLAUSE_DECL (u) = decl;
24988           OMP_CLAUSE_CHAIN (u) = list;
24989           list = u;
24990         }
24991       else
24992         list = tree_cons (decl, NULL_TREE, list);
24993
24994     get_comma:
24995       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24996         break;
24997       cp_lexer_consume_token (parser->lexer);
24998     }
24999
25000   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25001     {
25002       int ending;
25003
25004       /* Try to resync to an unnested comma.  Copied from
25005          cp_parser_parenthesized_expression_list.  */
25006     skip_comma:
25007       ending = cp_parser_skip_to_closing_parenthesis (parser,
25008                                                       /*recovering=*/true,
25009                                                       /*or_comma=*/true,
25010                                                       /*consume_paren=*/true);
25011       if (ending < 0)
25012         goto get_comma;
25013     }
25014
25015   return list;
25016 }
25017
25018 /* Similarly, but expect leading and trailing parenthesis.  This is a very
25019    common case for omp clauses.  */
25020
25021 static tree
25022 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
25023 {
25024   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25025     return cp_parser_omp_var_list_no_open (parser, kind, list);
25026   return list;
25027 }
25028
25029 /* OpenMP 3.0:
25030    collapse ( constant-expression ) */
25031
25032 static tree
25033 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
25034 {
25035   tree c, num;
25036   location_t loc;
25037   HOST_WIDE_INT n;
25038
25039   loc = cp_lexer_peek_token (parser->lexer)->location;
25040   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25041     return list;
25042
25043   num = cp_parser_constant_expression (parser, false, NULL);
25044
25045   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25046     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25047                                            /*or_comma=*/false,
25048                                            /*consume_paren=*/true);
25049
25050   if (num == error_mark_node)
25051     return list;
25052   num = fold_non_dependent_expr (num);
25053   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25054       || !host_integerp (num, 0)
25055       || (n = tree_low_cst (num, 0)) <= 0
25056       || (int) n != n)
25057     {
25058       error_at (loc, "collapse argument needs positive constant integer expression");
25059       return list;
25060     }
25061
25062   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25063   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25064   OMP_CLAUSE_CHAIN (c) = list;
25065   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25066
25067   return c;
25068 }
25069
25070 /* OpenMP 2.5:
25071    default ( shared | none ) */
25072
25073 static tree
25074 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25075 {
25076   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25077   tree c;
25078
25079   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25080     return list;
25081   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25082     {
25083       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25084       const char *p = IDENTIFIER_POINTER (id);
25085
25086       switch (p[0])
25087         {
25088         case 'n':
25089           if (strcmp ("none", p) != 0)
25090             goto invalid_kind;
25091           kind = OMP_CLAUSE_DEFAULT_NONE;
25092           break;
25093
25094         case 's':
25095           if (strcmp ("shared", p) != 0)
25096             goto invalid_kind;
25097           kind = OMP_CLAUSE_DEFAULT_SHARED;
25098           break;
25099
25100         default:
25101           goto invalid_kind;
25102         }
25103
25104       cp_lexer_consume_token (parser->lexer);
25105     }
25106   else
25107     {
25108     invalid_kind:
25109       cp_parser_error (parser, "expected %<none%> or %<shared%>");
25110     }
25111
25112   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25113     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25114                                            /*or_comma=*/false,
25115                                            /*consume_paren=*/true);
25116
25117   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25118     return list;
25119
25120   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25121   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25122   OMP_CLAUSE_CHAIN (c) = list;
25123   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25124
25125   return c;
25126 }
25127
25128 /* OpenMP 3.1:
25129    final ( expression ) */
25130
25131 static tree
25132 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25133 {
25134   tree t, c;
25135
25136   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25137     return list;
25138
25139   t = cp_parser_condition (parser);
25140
25141   if (t == error_mark_node
25142       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25143     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25144                                            /*or_comma=*/false,
25145                                            /*consume_paren=*/true);
25146
25147   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25148
25149   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25150   OMP_CLAUSE_FINAL_EXPR (c) = t;
25151   OMP_CLAUSE_CHAIN (c) = list;
25152
25153   return c;
25154 }
25155
25156 /* OpenMP 2.5:
25157    if ( expression ) */
25158
25159 static tree
25160 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25161 {
25162   tree t, c;
25163
25164   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25165     return list;
25166
25167   t = cp_parser_condition (parser);
25168
25169   if (t == error_mark_node
25170       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25171     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25172                                            /*or_comma=*/false,
25173                                            /*consume_paren=*/true);
25174
25175   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25176
25177   c = build_omp_clause (location, OMP_CLAUSE_IF);
25178   OMP_CLAUSE_IF_EXPR (c) = t;
25179   OMP_CLAUSE_CHAIN (c) = list;
25180
25181   return c;
25182 }
25183
25184 /* OpenMP 3.1:
25185    mergeable */
25186
25187 static tree
25188 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25189                                 tree list, location_t location)
25190 {
25191   tree c;
25192
25193   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25194                              location);
25195
25196   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25197   OMP_CLAUSE_CHAIN (c) = list;
25198   return c;
25199 }
25200
25201 /* OpenMP 2.5:
25202    nowait */
25203
25204 static tree
25205 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25206                              tree list, location_t location)
25207 {
25208   tree c;
25209
25210   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25211
25212   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25213   OMP_CLAUSE_CHAIN (c) = list;
25214   return c;
25215 }
25216
25217 /* OpenMP 2.5:
25218    num_threads ( expression ) */
25219
25220 static tree
25221 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25222                                   location_t location)
25223 {
25224   tree t, c;
25225
25226   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25227     return list;
25228
25229   t = cp_parser_expression (parser, false, NULL);
25230
25231   if (t == error_mark_node
25232       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25233     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25234                                            /*or_comma=*/false,
25235                                            /*consume_paren=*/true);
25236
25237   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25238                              "num_threads", location);
25239
25240   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25241   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25242   OMP_CLAUSE_CHAIN (c) = list;
25243
25244   return c;
25245 }
25246
25247 /* OpenMP 2.5:
25248    ordered */
25249
25250 static tree
25251 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25252                               tree list, location_t location)
25253 {
25254   tree c;
25255
25256   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25257                              "ordered", location);
25258
25259   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25260   OMP_CLAUSE_CHAIN (c) = list;
25261   return c;
25262 }
25263
25264 /* OpenMP 2.5:
25265    reduction ( reduction-operator : variable-list )
25266
25267    reduction-operator:
25268      One of: + * - & ^ | && ||
25269
25270    OpenMP 3.1:
25271
25272    reduction-operator:
25273      One of: + * - & ^ | && || min max  */
25274
25275 static tree
25276 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25277 {
25278   enum tree_code code;
25279   tree nlist, c;
25280
25281   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25282     return list;
25283
25284   switch (cp_lexer_peek_token (parser->lexer)->type)
25285     {
25286     case CPP_PLUS:
25287       code = PLUS_EXPR;
25288       break;
25289     case CPP_MULT:
25290       code = MULT_EXPR;
25291       break;
25292     case CPP_MINUS:
25293       code = MINUS_EXPR;
25294       break;
25295     case CPP_AND:
25296       code = BIT_AND_EXPR;
25297       break;
25298     case CPP_XOR:
25299       code = BIT_XOR_EXPR;
25300       break;
25301     case CPP_OR:
25302       code = BIT_IOR_EXPR;
25303       break;
25304     case CPP_AND_AND:
25305       code = TRUTH_ANDIF_EXPR;
25306       break;
25307     case CPP_OR_OR:
25308       code = TRUTH_ORIF_EXPR;
25309       break;
25310     case CPP_NAME:
25311       {
25312         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25313         const char *p = IDENTIFIER_POINTER (id);
25314
25315         if (strcmp (p, "min") == 0)
25316           {
25317             code = MIN_EXPR;
25318             break;
25319           }
25320         if (strcmp (p, "max") == 0)
25321           {
25322             code = MAX_EXPR;
25323             break;
25324           }
25325       }
25326       /* FALLTHROUGH */
25327     default:
25328       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25329                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25330     resync_fail:
25331       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25332                                              /*or_comma=*/false,
25333                                              /*consume_paren=*/true);
25334       return list;
25335     }
25336   cp_lexer_consume_token (parser->lexer);
25337
25338   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25339     goto resync_fail;
25340
25341   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25342   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25343     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25344
25345   return nlist;
25346 }
25347
25348 /* OpenMP 2.5:
25349    schedule ( schedule-kind )
25350    schedule ( schedule-kind , expression )
25351
25352    schedule-kind:
25353      static | dynamic | guided | runtime | auto  */
25354
25355 static tree
25356 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25357 {
25358   tree c, t;
25359
25360   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25361     return list;
25362
25363   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25364
25365   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25366     {
25367       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25368       const char *p = IDENTIFIER_POINTER (id);
25369
25370       switch (p[0])
25371         {
25372         case 'd':
25373           if (strcmp ("dynamic", p) != 0)
25374             goto invalid_kind;
25375           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25376           break;
25377
25378         case 'g':
25379           if (strcmp ("guided", p) != 0)
25380             goto invalid_kind;
25381           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25382           break;
25383
25384         case 'r':
25385           if (strcmp ("runtime", p) != 0)
25386             goto invalid_kind;
25387           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25388           break;
25389
25390         default:
25391           goto invalid_kind;
25392         }
25393     }
25394   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25395     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25396   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25397     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25398   else
25399     goto invalid_kind;
25400   cp_lexer_consume_token (parser->lexer);
25401
25402   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25403     {
25404       cp_token *token;
25405       cp_lexer_consume_token (parser->lexer);
25406
25407       token = cp_lexer_peek_token (parser->lexer);
25408       t = cp_parser_assignment_expression (parser, false, NULL);
25409
25410       if (t == error_mark_node)
25411         goto resync_fail;
25412       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25413         error_at (token->location, "schedule %<runtime%> does not take "
25414                   "a %<chunk_size%> parameter");
25415       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25416         error_at (token->location, "schedule %<auto%> does not take "
25417                   "a %<chunk_size%> parameter");
25418       else
25419         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25420
25421       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25422         goto resync_fail;
25423     }
25424   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25425     goto resync_fail;
25426
25427   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25428   OMP_CLAUSE_CHAIN (c) = list;
25429   return c;
25430
25431  invalid_kind:
25432   cp_parser_error (parser, "invalid schedule kind");
25433  resync_fail:
25434   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25435                                          /*or_comma=*/false,
25436                                          /*consume_paren=*/true);
25437   return list;
25438 }
25439
25440 /* OpenMP 3.0:
25441    untied */
25442
25443 static tree
25444 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25445                              tree list, location_t location)
25446 {
25447   tree c;
25448
25449   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25450
25451   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25452   OMP_CLAUSE_CHAIN (c) = list;
25453   return c;
25454 }
25455
25456 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25457    is a bitmask in MASK.  Return the list of clauses found; the result
25458    of clause default goes in *pdefault.  */
25459
25460 static tree
25461 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25462                            const char *where, cp_token *pragma_tok)
25463 {
25464   tree clauses = NULL;
25465   bool first = true;
25466   cp_token *token = NULL;
25467
25468   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25469     {
25470       pragma_omp_clause c_kind;
25471       const char *c_name;
25472       tree prev = clauses;
25473
25474       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25475         cp_lexer_consume_token (parser->lexer);
25476
25477       token = cp_lexer_peek_token (parser->lexer);
25478       c_kind = cp_parser_omp_clause_name (parser);
25479       first = false;
25480
25481       switch (c_kind)
25482         {
25483         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25484           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25485                                                    token->location);
25486           c_name = "collapse";
25487           break;
25488         case PRAGMA_OMP_CLAUSE_COPYIN:
25489           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25490           c_name = "copyin";
25491           break;
25492         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25493           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25494                                             clauses);
25495           c_name = "copyprivate";
25496           break;
25497         case PRAGMA_OMP_CLAUSE_DEFAULT:
25498           clauses = cp_parser_omp_clause_default (parser, clauses,
25499                                                   token->location);
25500           c_name = "default";
25501           break;
25502         case PRAGMA_OMP_CLAUSE_FINAL:
25503           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25504           c_name = "final";
25505           break;
25506         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25507           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25508                                             clauses);
25509           c_name = "firstprivate";
25510           break;
25511         case PRAGMA_OMP_CLAUSE_IF:
25512           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25513           c_name = "if";
25514           break;
25515         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25516           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25517                                             clauses);
25518           c_name = "lastprivate";
25519           break;
25520         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25521           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25522                                                     token->location);
25523           c_name = "mergeable";
25524           break;
25525         case PRAGMA_OMP_CLAUSE_NOWAIT:
25526           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25527           c_name = "nowait";
25528           break;
25529         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25530           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25531                                                       token->location);
25532           c_name = "num_threads";
25533           break;
25534         case PRAGMA_OMP_CLAUSE_ORDERED:
25535           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25536                                                   token->location);
25537           c_name = "ordered";
25538           break;
25539         case PRAGMA_OMP_CLAUSE_PRIVATE:
25540           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25541                                             clauses);
25542           c_name = "private";
25543           break;
25544         case PRAGMA_OMP_CLAUSE_REDUCTION:
25545           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25546           c_name = "reduction";
25547           break;
25548         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25549           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25550                                                    token->location);
25551           c_name = "schedule";
25552           break;
25553         case PRAGMA_OMP_CLAUSE_SHARED:
25554           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25555                                             clauses);
25556           c_name = "shared";
25557           break;
25558         case PRAGMA_OMP_CLAUSE_UNTIED:
25559           clauses = cp_parser_omp_clause_untied (parser, clauses,
25560                                                  token->location);
25561           c_name = "nowait";
25562           break;
25563         default:
25564           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25565           goto saw_error;
25566         }
25567
25568       if (((mask >> c_kind) & 1) == 0)
25569         {
25570           /* Remove the invalid clause(s) from the list to avoid
25571              confusing the rest of the compiler.  */
25572           clauses = prev;
25573           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25574         }
25575     }
25576  saw_error:
25577   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25578   return finish_omp_clauses (clauses);
25579 }
25580
25581 /* OpenMP 2.5:
25582    structured-block:
25583      statement
25584
25585    In practice, we're also interested in adding the statement to an
25586    outer node.  So it is convenient if we work around the fact that
25587    cp_parser_statement calls add_stmt.  */
25588
25589 static unsigned
25590 cp_parser_begin_omp_structured_block (cp_parser *parser)
25591 {
25592   unsigned save = parser->in_statement;
25593
25594   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25595      This preserves the "not within loop or switch" style error messages
25596      for nonsense cases like
25597         void foo() {
25598         #pragma omp single
25599           break;
25600         }
25601   */
25602   if (parser->in_statement)
25603     parser->in_statement = IN_OMP_BLOCK;
25604
25605   return save;
25606 }
25607
25608 static void
25609 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25610 {
25611   parser->in_statement = save;
25612 }
25613
25614 static tree
25615 cp_parser_omp_structured_block (cp_parser *parser)
25616 {
25617   tree stmt = begin_omp_structured_block ();
25618   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25619
25620   cp_parser_statement (parser, NULL_TREE, false, NULL);
25621
25622   cp_parser_end_omp_structured_block (parser, save);
25623   return finish_omp_structured_block (stmt);
25624 }
25625
25626 /* OpenMP 2.5:
25627    # pragma omp atomic new-line
25628      expression-stmt
25629
25630    expression-stmt:
25631      x binop= expr | x++ | ++x | x-- | --x
25632    binop:
25633      +, *, -, /, &, ^, |, <<, >>
25634
25635   where x is an lvalue expression with scalar type.
25636
25637    OpenMP 3.1:
25638    # pragma omp atomic new-line
25639      update-stmt
25640
25641    # pragma omp atomic read new-line
25642      read-stmt
25643
25644    # pragma omp atomic write new-line
25645      write-stmt
25646
25647    # pragma omp atomic update new-line
25648      update-stmt
25649
25650    # pragma omp atomic capture new-line
25651      capture-stmt
25652
25653    # pragma omp atomic capture new-line
25654      capture-block
25655
25656    read-stmt:
25657      v = x
25658    write-stmt:
25659      x = expr
25660    update-stmt:
25661      expression-stmt | x = x binop expr
25662    capture-stmt:
25663      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25664    capture-block:
25665      { v = x; update-stmt; } | { update-stmt; v = x; }
25666
25667   where x and v are lvalue expressions with scalar type.  */
25668
25669 static void
25670 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25671 {
25672   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25673   tree rhs1 = NULL_TREE, orig_lhs;
25674   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25675   bool structured_block = false;
25676
25677   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25678     {
25679       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25680       const char *p = IDENTIFIER_POINTER (id);
25681
25682       if (!strcmp (p, "read"))
25683         code = OMP_ATOMIC_READ;
25684       else if (!strcmp (p, "write"))
25685         code = NOP_EXPR;
25686       else if (!strcmp (p, "update"))
25687         code = OMP_ATOMIC;
25688       else if (!strcmp (p, "capture"))
25689         code = OMP_ATOMIC_CAPTURE_NEW;
25690       else
25691         p = NULL;
25692       if (p)
25693         cp_lexer_consume_token (parser->lexer);
25694     }
25695   cp_parser_require_pragma_eol (parser, pragma_tok);
25696
25697   switch (code)
25698     {
25699     case OMP_ATOMIC_READ:
25700     case NOP_EXPR: /* atomic write */
25701       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25702                                       /*cast_p=*/false, NULL);
25703       if (v == error_mark_node)
25704         goto saw_error;
25705       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25706         goto saw_error;
25707       if (code == NOP_EXPR)
25708         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25709       else
25710         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25711                                           /*cast_p=*/false, NULL);
25712       if (lhs == error_mark_node)
25713         goto saw_error;
25714       if (code == NOP_EXPR)
25715         {
25716           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25717              opcode.  */
25718           code = OMP_ATOMIC;
25719           rhs = lhs;
25720           lhs = v;
25721           v = NULL_TREE;
25722         }
25723       goto done;
25724     case OMP_ATOMIC_CAPTURE_NEW:
25725       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25726         {
25727           cp_lexer_consume_token (parser->lexer);
25728           structured_block = true;
25729         }
25730       else
25731         {
25732           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25733                                           /*cast_p=*/false, NULL);
25734           if (v == error_mark_node)
25735             goto saw_error;
25736           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25737             goto saw_error;
25738         }
25739     default:
25740       break;
25741     }
25742
25743 restart:
25744   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25745                                     /*cast_p=*/false, NULL);
25746   orig_lhs = lhs;
25747   switch (TREE_CODE (lhs))
25748     {
25749     case ERROR_MARK:
25750       goto saw_error;
25751
25752     case POSTINCREMENT_EXPR:
25753       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25754         code = OMP_ATOMIC_CAPTURE_OLD;
25755       /* FALLTHROUGH */
25756     case PREINCREMENT_EXPR:
25757       lhs = TREE_OPERAND (lhs, 0);
25758       opcode = PLUS_EXPR;
25759       rhs = integer_one_node;
25760       break;
25761
25762     case POSTDECREMENT_EXPR:
25763       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25764         code = OMP_ATOMIC_CAPTURE_OLD;
25765       /* FALLTHROUGH */
25766     case PREDECREMENT_EXPR:
25767       lhs = TREE_OPERAND (lhs, 0);
25768       opcode = MINUS_EXPR;
25769       rhs = integer_one_node;
25770       break;
25771
25772     case COMPOUND_EXPR:
25773       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25774          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25775          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25776          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25777          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25778                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25779             == BOOLEAN_TYPE)
25780        /* Undo effects of boolean_increment for post {in,de}crement.  */
25781        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25782       /* FALLTHRU */
25783     case MODIFY_EXPR:
25784       if (TREE_CODE (lhs) == MODIFY_EXPR
25785          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25786         {
25787           /* Undo effects of boolean_increment.  */
25788           if (integer_onep (TREE_OPERAND (lhs, 1)))
25789             {
25790               /* This is pre or post increment.  */
25791               rhs = TREE_OPERAND (lhs, 1);
25792               lhs = TREE_OPERAND (lhs, 0);
25793               opcode = NOP_EXPR;
25794               if (code == OMP_ATOMIC_CAPTURE_NEW
25795                   && !structured_block
25796                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25797                 code = OMP_ATOMIC_CAPTURE_OLD;
25798               break;
25799             }
25800         }
25801       /* FALLTHRU */
25802     default:
25803       switch (cp_lexer_peek_token (parser->lexer)->type)
25804         {
25805         case CPP_MULT_EQ:
25806           opcode = MULT_EXPR;
25807           break;
25808         case CPP_DIV_EQ:
25809           opcode = TRUNC_DIV_EXPR;
25810           break;
25811         case CPP_PLUS_EQ:
25812           opcode = PLUS_EXPR;
25813           break;
25814         case CPP_MINUS_EQ:
25815           opcode = MINUS_EXPR;
25816           break;
25817         case CPP_LSHIFT_EQ:
25818           opcode = LSHIFT_EXPR;
25819           break;
25820         case CPP_RSHIFT_EQ:
25821           opcode = RSHIFT_EXPR;
25822           break;
25823         case CPP_AND_EQ:
25824           opcode = BIT_AND_EXPR;
25825           break;
25826         case CPP_OR_EQ:
25827           opcode = BIT_IOR_EXPR;
25828           break;
25829         case CPP_XOR_EQ:
25830           opcode = BIT_XOR_EXPR;
25831           break;
25832         case CPP_EQ:
25833           if (structured_block || code == OMP_ATOMIC)
25834             {
25835               enum cp_parser_prec oprec;
25836               cp_token *token;
25837               cp_lexer_consume_token (parser->lexer);
25838               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25839                                                  /*cast_p=*/false, NULL);
25840               if (rhs1 == error_mark_node)
25841                 goto saw_error;
25842               token = cp_lexer_peek_token (parser->lexer);
25843               switch (token->type)
25844                 {
25845                 case CPP_SEMICOLON:
25846                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25847                     {
25848                       code = OMP_ATOMIC_CAPTURE_OLD;
25849                       v = lhs;
25850                       lhs = NULL_TREE;
25851                       lhs1 = rhs1;
25852                       rhs1 = NULL_TREE;
25853                       cp_lexer_consume_token (parser->lexer);
25854                       goto restart;
25855                     }
25856                   cp_parser_error (parser,
25857                                    "invalid form of %<#pragma omp atomic%>");
25858                   goto saw_error;
25859                 case CPP_MULT:
25860                   opcode = MULT_EXPR;
25861                   break;
25862                 case CPP_DIV:
25863                   opcode = TRUNC_DIV_EXPR;
25864                   break;
25865                 case CPP_PLUS:
25866                   opcode = PLUS_EXPR;
25867                   break;
25868                 case CPP_MINUS:
25869                   opcode = MINUS_EXPR;
25870                   break;
25871                 case CPP_LSHIFT:
25872                   opcode = LSHIFT_EXPR;
25873                   break;
25874                 case CPP_RSHIFT:
25875                   opcode = RSHIFT_EXPR;
25876                   break;
25877                 case CPP_AND:
25878                   opcode = BIT_AND_EXPR;
25879                   break;
25880                 case CPP_OR:
25881                   opcode = BIT_IOR_EXPR;
25882                   break;
25883                 case CPP_XOR:
25884                   opcode = BIT_XOR_EXPR;
25885                   break;
25886                 default:
25887                   cp_parser_error (parser,
25888                                    "invalid operator for %<#pragma omp atomic%>");
25889                   goto saw_error;
25890                 }
25891               oprec = TOKEN_PRECEDENCE (token);
25892               gcc_assert (oprec != PREC_NOT_OPERATOR);
25893               if (commutative_tree_code (opcode))
25894                 oprec = (enum cp_parser_prec) (oprec - 1);
25895               cp_lexer_consume_token (parser->lexer);
25896               rhs = cp_parser_binary_expression (parser, false, false,
25897                                                  oprec, NULL);
25898               if (rhs == error_mark_node)
25899                 goto saw_error;
25900               goto stmt_done;
25901             }
25902           /* FALLTHROUGH */
25903         default:
25904           cp_parser_error (parser,
25905                            "invalid operator for %<#pragma omp atomic%>");
25906           goto saw_error;
25907         }
25908       cp_lexer_consume_token (parser->lexer);
25909
25910       rhs = cp_parser_expression (parser, false, NULL);
25911       if (rhs == error_mark_node)
25912         goto saw_error;
25913       break;
25914     }
25915 stmt_done:
25916   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25917     {
25918       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25919         goto saw_error;
25920       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25921                                       /*cast_p=*/false, NULL);
25922       if (v == error_mark_node)
25923         goto saw_error;
25924       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25925         goto saw_error;
25926       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25927                                          /*cast_p=*/false, NULL);
25928       if (lhs1 == error_mark_node)
25929         goto saw_error;
25930     }
25931   if (structured_block)
25932     {
25933       cp_parser_consume_semicolon_at_end_of_statement (parser);
25934       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25935     }
25936 done:
25937   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25938   if (!structured_block)
25939     cp_parser_consume_semicolon_at_end_of_statement (parser);
25940   return;
25941
25942  saw_error:
25943   cp_parser_skip_to_end_of_block_or_statement (parser);
25944   if (structured_block)
25945     {
25946       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25947         cp_lexer_consume_token (parser->lexer);
25948       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25949         {
25950           cp_parser_skip_to_end_of_block_or_statement (parser);
25951           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25952             cp_lexer_consume_token (parser->lexer);
25953         }
25954     }
25955 }
25956
25957
25958 /* OpenMP 2.5:
25959    # pragma omp barrier new-line  */
25960
25961 static void
25962 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25963 {
25964   cp_parser_require_pragma_eol (parser, pragma_tok);
25965   finish_omp_barrier ();
25966 }
25967
25968 /* OpenMP 2.5:
25969    # pragma omp critical [(name)] new-line
25970      structured-block  */
25971
25972 static tree
25973 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25974 {
25975   tree stmt, name = NULL;
25976
25977   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25978     {
25979       cp_lexer_consume_token (parser->lexer);
25980
25981       name = cp_parser_identifier (parser);
25982
25983       if (name == error_mark_node
25984           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25985         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25986                                                /*or_comma=*/false,
25987                                                /*consume_paren=*/true);
25988       if (name == error_mark_node)
25989         name = NULL;
25990     }
25991   cp_parser_require_pragma_eol (parser, pragma_tok);
25992
25993   stmt = cp_parser_omp_structured_block (parser);
25994   return c_finish_omp_critical (input_location, stmt, name);
25995 }
25996
25997 /* OpenMP 2.5:
25998    # pragma omp flush flush-vars[opt] new-line
25999
26000    flush-vars:
26001      ( variable-list ) */
26002
26003 static void
26004 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
26005 {
26006   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26007     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26008   cp_parser_require_pragma_eol (parser, pragma_tok);
26009
26010   finish_omp_flush ();
26011 }
26012
26013 /* Helper function, to parse omp for increment expression.  */
26014
26015 static tree
26016 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
26017 {
26018   tree cond = cp_parser_binary_expression (parser, false, true,
26019                                            PREC_NOT_OPERATOR, NULL);
26020   if (cond == error_mark_node
26021       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26022     {
26023       cp_parser_skip_to_end_of_statement (parser);
26024       return error_mark_node;
26025     }
26026
26027   switch (TREE_CODE (cond))
26028     {
26029     case GT_EXPR:
26030     case GE_EXPR:
26031     case LT_EXPR:
26032     case LE_EXPR:
26033       break;
26034     default:
26035       return error_mark_node;
26036     }
26037
26038   /* If decl is an iterator, preserve LHS and RHS of the relational
26039      expr until finish_omp_for.  */
26040   if (decl
26041       && (type_dependent_expression_p (decl)
26042           || CLASS_TYPE_P (TREE_TYPE (decl))))
26043     return cond;
26044
26045   return build_x_binary_op (TREE_CODE (cond),
26046                             TREE_OPERAND (cond, 0), ERROR_MARK,
26047                             TREE_OPERAND (cond, 1), ERROR_MARK,
26048                             /*overload=*/NULL, tf_warning_or_error);
26049 }
26050
26051 /* Helper function, to parse omp for increment expression.  */
26052
26053 static tree
26054 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26055 {
26056   cp_token *token = cp_lexer_peek_token (parser->lexer);
26057   enum tree_code op;
26058   tree lhs, rhs;
26059   cp_id_kind idk;
26060   bool decl_first;
26061
26062   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26063     {
26064       op = (token->type == CPP_PLUS_PLUS
26065             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26066       cp_lexer_consume_token (parser->lexer);
26067       lhs = cp_parser_cast_expression (parser, false, false, NULL);
26068       if (lhs != decl)
26069         return error_mark_node;
26070       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26071     }
26072
26073   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26074   if (lhs != decl)
26075     return error_mark_node;
26076
26077   token = cp_lexer_peek_token (parser->lexer);
26078   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26079     {
26080       op = (token->type == CPP_PLUS_PLUS
26081             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26082       cp_lexer_consume_token (parser->lexer);
26083       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26084     }
26085
26086   op = cp_parser_assignment_operator_opt (parser);
26087   if (op == ERROR_MARK)
26088     return error_mark_node;
26089
26090   if (op != NOP_EXPR)
26091     {
26092       rhs = cp_parser_assignment_expression (parser, false, NULL);
26093       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26094       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26095     }
26096
26097   lhs = cp_parser_binary_expression (parser, false, false,
26098                                      PREC_ADDITIVE_EXPRESSION, NULL);
26099   token = cp_lexer_peek_token (parser->lexer);
26100   decl_first = lhs == decl;
26101   if (decl_first)
26102     lhs = NULL_TREE;
26103   if (token->type != CPP_PLUS
26104       && token->type != CPP_MINUS)
26105     return error_mark_node;
26106
26107   do
26108     {
26109       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26110       cp_lexer_consume_token (parser->lexer);
26111       rhs = cp_parser_binary_expression (parser, false, false,
26112                                          PREC_ADDITIVE_EXPRESSION, NULL);
26113       token = cp_lexer_peek_token (parser->lexer);
26114       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26115         {
26116           if (lhs == NULL_TREE)
26117             {
26118               if (op == PLUS_EXPR)
26119                 lhs = rhs;
26120               else
26121                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
26122             }
26123           else
26124             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
26125                                      NULL, tf_warning_or_error);
26126         }
26127     }
26128   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26129
26130   if (!decl_first)
26131     {
26132       if (rhs != decl || op == MINUS_EXPR)
26133         return error_mark_node;
26134       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26135     }
26136   else
26137     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26138
26139   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26140 }
26141
26142 /* Parse the restricted form of the for statement allowed by OpenMP.  */
26143
26144 static tree
26145 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26146 {
26147   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26148   tree real_decl, initv, condv, incrv, declv;
26149   tree this_pre_body, cl;
26150   location_t loc_first;
26151   bool collapse_err = false;
26152   int i, collapse = 1, nbraces = 0;
26153   VEC(tree,gc) *for_block = make_tree_vector ();
26154
26155   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26156     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26157       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26158
26159   gcc_assert (collapse >= 1);
26160
26161   declv = make_tree_vec (collapse);
26162   initv = make_tree_vec (collapse);
26163   condv = make_tree_vec (collapse);
26164   incrv = make_tree_vec (collapse);
26165
26166   loc_first = cp_lexer_peek_token (parser->lexer)->location;
26167
26168   for (i = 0; i < collapse; i++)
26169     {
26170       int bracecount = 0;
26171       bool add_private_clause = false;
26172       location_t loc;
26173
26174       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26175         {
26176           cp_parser_error (parser, "for statement expected");
26177           return NULL;
26178         }
26179       loc = cp_lexer_consume_token (parser->lexer)->location;
26180
26181       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26182         return NULL;
26183
26184       init = decl = real_decl = NULL;
26185       this_pre_body = push_stmt_list ();
26186       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26187         {
26188           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26189
26190              init-expr:
26191                        var = lb
26192                        integer-type var = lb
26193                        random-access-iterator-type var = lb
26194                        pointer-type var = lb
26195           */
26196           cp_decl_specifier_seq type_specifiers;
26197
26198           /* First, try to parse as an initialized declaration.  See
26199              cp_parser_condition, from whence the bulk of this is copied.  */
26200
26201           cp_parser_parse_tentatively (parser);
26202           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26203                                         /*is_trailing_return=*/false,
26204                                         &type_specifiers);
26205           if (cp_parser_parse_definitely (parser))
26206             {
26207               /* If parsing a type specifier seq succeeded, then this
26208                  MUST be a initialized declaration.  */
26209               tree asm_specification, attributes;
26210               cp_declarator *declarator;
26211
26212               declarator = cp_parser_declarator (parser,
26213                                                  CP_PARSER_DECLARATOR_NAMED,
26214                                                  /*ctor_dtor_or_conv_p=*/NULL,
26215                                                  /*parenthesized_p=*/NULL,
26216                                                  /*member_p=*/false);
26217               attributes = cp_parser_attributes_opt (parser);
26218               asm_specification = cp_parser_asm_specification_opt (parser);
26219
26220               if (declarator == cp_error_declarator) 
26221                 cp_parser_skip_to_end_of_statement (parser);
26222
26223               else 
26224                 {
26225                   tree pushed_scope, auto_node;
26226
26227                   decl = start_decl (declarator, &type_specifiers,
26228                                      SD_INITIALIZED, attributes,
26229                                      /*prefix_attributes=*/NULL_TREE,
26230                                      &pushed_scope);
26231
26232                   auto_node = type_uses_auto (TREE_TYPE (decl));
26233                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26234                     {
26235                       if (cp_lexer_next_token_is (parser->lexer, 
26236                                                   CPP_OPEN_PAREN))
26237                         error ("parenthesized initialization is not allowed in "
26238                                "OpenMP %<for%> loop");
26239                       else
26240                         /* Trigger an error.  */
26241                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26242
26243                       init = error_mark_node;
26244                       cp_parser_skip_to_end_of_statement (parser);
26245                     }
26246                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26247                            || type_dependent_expression_p (decl)
26248                            || auto_node)
26249                     {
26250                       bool is_direct_init, is_non_constant_init;
26251
26252                       init = cp_parser_initializer (parser,
26253                                                     &is_direct_init,
26254                                                     &is_non_constant_init);
26255
26256                       if (auto_node)
26257                         {
26258                           TREE_TYPE (decl)
26259                             = do_auto_deduction (TREE_TYPE (decl), init,
26260                                                  auto_node);
26261
26262                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26263                               && !type_dependent_expression_p (decl))
26264                             goto non_class;
26265                         }
26266                       
26267                       cp_finish_decl (decl, init, !is_non_constant_init,
26268                                       asm_specification,
26269                                       LOOKUP_ONLYCONVERTING);
26270                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26271                         {
26272                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26273                           init = NULL_TREE;
26274                         }
26275                       else
26276                         init = pop_stmt_list (this_pre_body);
26277                       this_pre_body = NULL_TREE;
26278                     }
26279                   else
26280                     {
26281                       /* Consume '='.  */
26282                       cp_lexer_consume_token (parser->lexer);
26283                       init = cp_parser_assignment_expression (parser, false, NULL);
26284
26285                     non_class:
26286                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26287                         init = error_mark_node;
26288                       else
26289                         cp_finish_decl (decl, NULL_TREE,
26290                                         /*init_const_expr_p=*/false,
26291                                         asm_specification,
26292                                         LOOKUP_ONLYCONVERTING);
26293                     }
26294
26295                   if (pushed_scope)
26296                     pop_scope (pushed_scope);
26297                 }
26298             }
26299           else 
26300             {
26301               cp_id_kind idk;
26302               /* If parsing a type specifier sequence failed, then
26303                  this MUST be a simple expression.  */
26304               cp_parser_parse_tentatively (parser);
26305               decl = cp_parser_primary_expression (parser, false, false,
26306                                                    false, &idk);
26307               if (!cp_parser_error_occurred (parser)
26308                   && decl
26309                   && DECL_P (decl)
26310                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26311                 {
26312                   tree rhs;
26313
26314                   cp_parser_parse_definitely (parser);
26315                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26316                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26317                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26318                                                          rhs,
26319                                                          tf_warning_or_error));
26320                   add_private_clause = true;
26321                 }
26322               else
26323                 {
26324                   decl = NULL;
26325                   cp_parser_abort_tentative_parse (parser);
26326                   init = cp_parser_expression (parser, false, NULL);
26327                   if (init)
26328                     {
26329                       if (TREE_CODE (init) == MODIFY_EXPR
26330                           || TREE_CODE (init) == MODOP_EXPR)
26331                         real_decl = TREE_OPERAND (init, 0);
26332                     }
26333                 }
26334             }
26335         }
26336       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26337       if (this_pre_body)
26338         {
26339           this_pre_body = pop_stmt_list (this_pre_body);
26340           if (pre_body)
26341             {
26342               tree t = pre_body;
26343               pre_body = push_stmt_list ();
26344               add_stmt (t);
26345               add_stmt (this_pre_body);
26346               pre_body = pop_stmt_list (pre_body);
26347             }
26348           else
26349             pre_body = this_pre_body;
26350         }
26351
26352       if (decl)
26353         real_decl = decl;
26354       if (par_clauses != NULL && real_decl != NULL_TREE)
26355         {
26356           tree *c;
26357           for (c = par_clauses; *c ; )
26358             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26359                 && OMP_CLAUSE_DECL (*c) == real_decl)
26360               {
26361                 error_at (loc, "iteration variable %qD"
26362                           " should not be firstprivate", real_decl);
26363                 *c = OMP_CLAUSE_CHAIN (*c);
26364               }
26365             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26366                      && OMP_CLAUSE_DECL (*c) == real_decl)
26367               {
26368                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26369                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26370                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26371                 OMP_CLAUSE_DECL (l) = real_decl;
26372                 OMP_CLAUSE_CHAIN (l) = clauses;
26373                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26374                 clauses = l;
26375                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26376                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26377                 add_private_clause = false;
26378               }
26379             else
26380               {
26381                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26382                     && OMP_CLAUSE_DECL (*c) == real_decl)
26383                   add_private_clause = false;
26384                 c = &OMP_CLAUSE_CHAIN (*c);
26385               }
26386         }
26387
26388       if (add_private_clause)
26389         {
26390           tree c;
26391           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26392             {
26393               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26394                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26395                   && OMP_CLAUSE_DECL (c) == decl)
26396                 break;
26397               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26398                        && OMP_CLAUSE_DECL (c) == decl)
26399                 error_at (loc, "iteration variable %qD "
26400                           "should not be firstprivate",
26401                           decl);
26402               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26403                        && OMP_CLAUSE_DECL (c) == decl)
26404                 error_at (loc, "iteration variable %qD should not be reduction",
26405                           decl);
26406             }
26407           if (c == NULL)
26408             {
26409               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26410               OMP_CLAUSE_DECL (c) = decl;
26411               c = finish_omp_clauses (c);
26412               if (c)
26413                 {
26414                   OMP_CLAUSE_CHAIN (c) = clauses;
26415                   clauses = c;
26416                 }
26417             }
26418         }
26419
26420       cond = NULL;
26421       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26422         cond = cp_parser_omp_for_cond (parser, decl);
26423       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26424
26425       incr = NULL;
26426       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26427         {
26428           /* If decl is an iterator, preserve the operator on decl
26429              until finish_omp_for.  */
26430           if (real_decl
26431               && ((processing_template_decl
26432                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26433                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26434             incr = cp_parser_omp_for_incr (parser, real_decl);
26435           else
26436             incr = cp_parser_expression (parser, false, NULL);
26437         }
26438
26439       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26440         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26441                                                /*or_comma=*/false,
26442                                                /*consume_paren=*/true);
26443
26444       TREE_VEC_ELT (declv, i) = decl;
26445       TREE_VEC_ELT (initv, i) = init;
26446       TREE_VEC_ELT (condv, i) = cond;
26447       TREE_VEC_ELT (incrv, i) = incr;
26448
26449       if (i == collapse - 1)
26450         break;
26451
26452       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26453          in between the collapsed for loops to be still considered perfectly
26454          nested.  Hopefully the final version clarifies this.
26455          For now handle (multiple) {'s and empty statements.  */
26456       cp_parser_parse_tentatively (parser);
26457       do
26458         {
26459           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26460             break;
26461           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26462             {
26463               cp_lexer_consume_token (parser->lexer);
26464               bracecount++;
26465             }
26466           else if (bracecount
26467                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26468             cp_lexer_consume_token (parser->lexer);
26469           else
26470             {
26471               loc = cp_lexer_peek_token (parser->lexer)->location;
26472               error_at (loc, "not enough collapsed for loops");
26473               collapse_err = true;
26474               cp_parser_abort_tentative_parse (parser);
26475               declv = NULL_TREE;
26476               break;
26477             }
26478         }
26479       while (1);
26480
26481       if (declv)
26482         {
26483           cp_parser_parse_definitely (parser);
26484           nbraces += bracecount;
26485         }
26486     }
26487
26488   /* Note that we saved the original contents of this flag when we entered
26489      the structured block, and so we don't need to re-save it here.  */
26490   parser->in_statement = IN_OMP_FOR;
26491
26492   /* Note that the grammar doesn't call for a structured block here,
26493      though the loop as a whole is a structured block.  */
26494   body = push_stmt_list ();
26495   cp_parser_statement (parser, NULL_TREE, false, NULL);
26496   body = pop_stmt_list (body);
26497
26498   if (declv == NULL_TREE)
26499     ret = NULL_TREE;
26500   else
26501     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26502                           pre_body, clauses);
26503
26504   while (nbraces)
26505     {
26506       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26507         {
26508           cp_lexer_consume_token (parser->lexer);
26509           nbraces--;
26510         }
26511       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26512         cp_lexer_consume_token (parser->lexer);
26513       else
26514         {
26515           if (!collapse_err)
26516             {
26517               error_at (cp_lexer_peek_token (parser->lexer)->location,
26518                         "collapsed loops not perfectly nested");
26519             }
26520           collapse_err = true;
26521           cp_parser_statement_seq_opt (parser, NULL);
26522           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26523             break;
26524         }
26525     }
26526
26527   while (!VEC_empty (tree, for_block))
26528     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26529   release_tree_vector (for_block);
26530
26531   return ret;
26532 }
26533
26534 /* OpenMP 2.5:
26535    #pragma omp for for-clause[optseq] new-line
26536      for-loop  */
26537
26538 #define OMP_FOR_CLAUSE_MASK                             \
26539         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26540         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26541         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26542         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26543         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26544         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26545         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26546         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26547
26548 static tree
26549 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26550 {
26551   tree clauses, sb, ret;
26552   unsigned int save;
26553
26554   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26555                                        "#pragma omp for", pragma_tok);
26556
26557   sb = begin_omp_structured_block ();
26558   save = cp_parser_begin_omp_structured_block (parser);
26559
26560   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26561
26562   cp_parser_end_omp_structured_block (parser, save);
26563   add_stmt (finish_omp_structured_block (sb));
26564
26565   return ret;
26566 }
26567
26568 /* OpenMP 2.5:
26569    # pragma omp master new-line
26570      structured-block  */
26571
26572 static tree
26573 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26574 {
26575   cp_parser_require_pragma_eol (parser, pragma_tok);
26576   return c_finish_omp_master (input_location,
26577                               cp_parser_omp_structured_block (parser));
26578 }
26579
26580 /* OpenMP 2.5:
26581    # pragma omp ordered new-line
26582      structured-block  */
26583
26584 static tree
26585 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26586 {
26587   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26588   cp_parser_require_pragma_eol (parser, pragma_tok);
26589   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26590 }
26591
26592 /* OpenMP 2.5:
26593
26594    section-scope:
26595      { section-sequence }
26596
26597    section-sequence:
26598      section-directive[opt] structured-block
26599      section-sequence section-directive structured-block  */
26600
26601 static tree
26602 cp_parser_omp_sections_scope (cp_parser *parser)
26603 {
26604   tree stmt, substmt;
26605   bool error_suppress = false;
26606   cp_token *tok;
26607
26608   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26609     return NULL_TREE;
26610
26611   stmt = push_stmt_list ();
26612
26613   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26614     {
26615       unsigned save;
26616
26617       substmt = begin_omp_structured_block ();
26618       save = cp_parser_begin_omp_structured_block (parser);
26619
26620       while (1)
26621         {
26622           cp_parser_statement (parser, NULL_TREE, false, NULL);
26623
26624           tok = cp_lexer_peek_token (parser->lexer);
26625           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26626             break;
26627           if (tok->type == CPP_CLOSE_BRACE)
26628             break;
26629           if (tok->type == CPP_EOF)
26630             break;
26631         }
26632
26633       cp_parser_end_omp_structured_block (parser, save);
26634       substmt = finish_omp_structured_block (substmt);
26635       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26636       add_stmt (substmt);
26637     }
26638
26639   while (1)
26640     {
26641       tok = cp_lexer_peek_token (parser->lexer);
26642       if (tok->type == CPP_CLOSE_BRACE)
26643         break;
26644       if (tok->type == CPP_EOF)
26645         break;
26646
26647       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26648         {
26649           cp_lexer_consume_token (parser->lexer);
26650           cp_parser_require_pragma_eol (parser, tok);
26651           error_suppress = false;
26652         }
26653       else if (!error_suppress)
26654         {
26655           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26656           error_suppress = true;
26657         }
26658
26659       substmt = cp_parser_omp_structured_block (parser);
26660       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26661       add_stmt (substmt);
26662     }
26663   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26664
26665   substmt = pop_stmt_list (stmt);
26666
26667   stmt = make_node (OMP_SECTIONS);
26668   TREE_TYPE (stmt) = void_type_node;
26669   OMP_SECTIONS_BODY (stmt) = substmt;
26670
26671   add_stmt (stmt);
26672   return stmt;
26673 }
26674
26675 /* OpenMP 2.5:
26676    # pragma omp sections sections-clause[optseq] newline
26677      sections-scope  */
26678
26679 #define OMP_SECTIONS_CLAUSE_MASK                        \
26680         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26681         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26682         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26683         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26684         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26685
26686 static tree
26687 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26688 {
26689   tree clauses, ret;
26690
26691   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26692                                        "#pragma omp sections", pragma_tok);
26693
26694   ret = cp_parser_omp_sections_scope (parser);
26695   if (ret)
26696     OMP_SECTIONS_CLAUSES (ret) = clauses;
26697
26698   return ret;
26699 }
26700
26701 /* OpenMP 2.5:
26702    # pragma parallel parallel-clause new-line
26703    # pragma parallel for parallel-for-clause new-line
26704    # pragma parallel sections parallel-sections-clause new-line  */
26705
26706 #define OMP_PARALLEL_CLAUSE_MASK                        \
26707         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26708         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26709         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26710         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26711         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26712         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26713         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26714         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26715
26716 static tree
26717 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26718 {
26719   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26720   const char *p_name = "#pragma omp parallel";
26721   tree stmt, clauses, par_clause, ws_clause, block;
26722   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26723   unsigned int save;
26724   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26725
26726   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26727     {
26728       cp_lexer_consume_token (parser->lexer);
26729       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26730       p_name = "#pragma omp parallel for";
26731       mask |= OMP_FOR_CLAUSE_MASK;
26732       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26733     }
26734   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26735     {
26736       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26737       const char *p = IDENTIFIER_POINTER (id);
26738       if (strcmp (p, "sections") == 0)
26739         {
26740           cp_lexer_consume_token (parser->lexer);
26741           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26742           p_name = "#pragma omp parallel sections";
26743           mask |= OMP_SECTIONS_CLAUSE_MASK;
26744           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26745         }
26746     }
26747
26748   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26749   block = begin_omp_parallel ();
26750   save = cp_parser_begin_omp_structured_block (parser);
26751
26752   switch (p_kind)
26753     {
26754     case PRAGMA_OMP_PARALLEL:
26755       cp_parser_statement (parser, NULL_TREE, false, NULL);
26756       par_clause = clauses;
26757       break;
26758
26759     case PRAGMA_OMP_PARALLEL_FOR:
26760       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26761       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26762       break;
26763
26764     case PRAGMA_OMP_PARALLEL_SECTIONS:
26765       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26766       stmt = cp_parser_omp_sections_scope (parser);
26767       if (stmt)
26768         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26769       break;
26770
26771     default:
26772       gcc_unreachable ();
26773     }
26774
26775   cp_parser_end_omp_structured_block (parser, save);
26776   stmt = finish_omp_parallel (par_clause, block);
26777   if (p_kind != PRAGMA_OMP_PARALLEL)
26778     OMP_PARALLEL_COMBINED (stmt) = 1;
26779   return stmt;
26780 }
26781
26782 /* OpenMP 2.5:
26783    # pragma omp single single-clause[optseq] new-line
26784      structured-block  */
26785
26786 #define OMP_SINGLE_CLAUSE_MASK                          \
26787         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26788         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26789         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26790         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26791
26792 static tree
26793 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26794 {
26795   tree stmt = make_node (OMP_SINGLE);
26796   TREE_TYPE (stmt) = void_type_node;
26797
26798   OMP_SINGLE_CLAUSES (stmt)
26799     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26800                                  "#pragma omp single", pragma_tok);
26801   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26802
26803   return add_stmt (stmt);
26804 }
26805
26806 /* OpenMP 3.0:
26807    # pragma omp task task-clause[optseq] new-line
26808      structured-block  */
26809
26810 #define OMP_TASK_CLAUSE_MASK                            \
26811         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26812         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26813         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26814         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26815         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26816         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26817         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26818         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26819
26820 static tree
26821 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26822 {
26823   tree clauses, block;
26824   unsigned int save;
26825
26826   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26827                                        "#pragma omp task", pragma_tok);
26828   block = begin_omp_task ();
26829   save = cp_parser_begin_omp_structured_block (parser);
26830   cp_parser_statement (parser, NULL_TREE, false, NULL);
26831   cp_parser_end_omp_structured_block (parser, save);
26832   return finish_omp_task (clauses, block);
26833 }
26834
26835 /* OpenMP 3.0:
26836    # pragma omp taskwait new-line  */
26837
26838 static void
26839 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26840 {
26841   cp_parser_require_pragma_eol (parser, pragma_tok);
26842   finish_omp_taskwait ();
26843 }
26844
26845 /* OpenMP 3.1:
26846    # pragma omp taskyield new-line  */
26847
26848 static void
26849 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26850 {
26851   cp_parser_require_pragma_eol (parser, pragma_tok);
26852   finish_omp_taskyield ();
26853 }
26854
26855 /* OpenMP 2.5:
26856    # pragma omp threadprivate (variable-list) */
26857
26858 static void
26859 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26860 {
26861   tree vars;
26862
26863   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26864   cp_parser_require_pragma_eol (parser, pragma_tok);
26865
26866   finish_omp_threadprivate (vars);
26867 }
26868
26869 /* Main entry point to OpenMP statement pragmas.  */
26870
26871 static void
26872 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26873 {
26874   tree stmt;
26875
26876   switch (pragma_tok->pragma_kind)
26877     {
26878     case PRAGMA_OMP_ATOMIC:
26879       cp_parser_omp_atomic (parser, pragma_tok);
26880       return;
26881     case PRAGMA_OMP_CRITICAL:
26882       stmt = cp_parser_omp_critical (parser, pragma_tok);
26883       break;
26884     case PRAGMA_OMP_FOR:
26885       stmt = cp_parser_omp_for (parser, pragma_tok);
26886       break;
26887     case PRAGMA_OMP_MASTER:
26888       stmt = cp_parser_omp_master (parser, pragma_tok);
26889       break;
26890     case PRAGMA_OMP_ORDERED:
26891       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26892       break;
26893     case PRAGMA_OMP_PARALLEL:
26894       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26895       break;
26896     case PRAGMA_OMP_SECTIONS:
26897       stmt = cp_parser_omp_sections (parser, pragma_tok);
26898       break;
26899     case PRAGMA_OMP_SINGLE:
26900       stmt = cp_parser_omp_single (parser, pragma_tok);
26901       break;
26902     case PRAGMA_OMP_TASK:
26903       stmt = cp_parser_omp_task (parser, pragma_tok);
26904       break;
26905     default:
26906       gcc_unreachable ();
26907     }
26908
26909   if (stmt)
26910     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26911 }
26912 \f
26913 /* Transactional Memory parsing routines.  */
26914
26915 /* Parse a transaction attribute.
26916
26917    txn-attribute:
26918         attribute
26919         [ [ identifier ] ]
26920
26921    ??? Simplify this when C++0x bracket attributes are
26922    implemented properly.  */
26923
26924 static tree
26925 cp_parser_txn_attribute_opt (cp_parser *parser)
26926 {
26927   cp_token *token;
26928   tree attr_name, attr = NULL;
26929
26930   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26931     return cp_parser_attributes_opt (parser);
26932
26933   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26934     return NULL_TREE;
26935   cp_lexer_consume_token (parser->lexer);
26936   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26937     goto error1;
26938
26939   token = cp_lexer_peek_token (parser->lexer);
26940   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26941     {
26942       token = cp_lexer_consume_token (parser->lexer);
26943
26944       attr_name = (token->type == CPP_KEYWORD
26945                    /* For keywords, use the canonical spelling,
26946                       not the parsed identifier.  */
26947                    ? ridpointers[(int) token->keyword]
26948                    : token->u.value);
26949       attr = build_tree_list (attr_name, NULL_TREE);
26950     }
26951   else
26952     cp_parser_error (parser, "expected identifier");
26953
26954   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26955  error1:
26956   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26957   return attr;
26958 }
26959
26960 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26961
26962    transaction-statement:
26963      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
26964        compound-statement
26965      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
26966 */
26967
26968 static tree
26969 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26970 {
26971   unsigned char old_in = parser->in_transaction;
26972   unsigned char this_in = 1, new_in;
26973   cp_token *token;
26974   tree stmt, attrs, noex;
26975
26976   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26977       || keyword == RID_TRANSACTION_RELAXED);
26978   token = cp_parser_require_keyword (parser, keyword,
26979       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26980           : RT_TRANSACTION_RELAXED));
26981   gcc_assert (token != NULL);
26982
26983   if (keyword == RID_TRANSACTION_RELAXED)
26984     this_in |= TM_STMT_ATTR_RELAXED;
26985   else
26986     {
26987       attrs = cp_parser_txn_attribute_opt (parser);
26988       if (attrs)
26989         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26990     }
26991
26992   /* Parse a noexcept specification.  */
26993   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
26994
26995   /* Keep track if we're in the lexical scope of an outer transaction.  */
26996   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
26997
26998   stmt = begin_transaction_stmt (token->location, NULL, this_in);
26999
27000   parser->in_transaction = new_in;
27001   cp_parser_compound_statement (parser, NULL, false, false);
27002   parser->in_transaction = old_in;
27003
27004   finish_transaction_stmt (stmt, NULL, this_in, noex);
27005
27006   return stmt;
27007 }
27008
27009 /* Parse a __transaction_atomic or __transaction_relaxed expression.
27010
27011    transaction-expression:
27012      __transaction_atomic txn-noexcept-spec[opt] ( expression )
27013      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
27014 */
27015
27016 static tree
27017 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
27018 {
27019   unsigned char old_in = parser->in_transaction;
27020   unsigned char this_in = 1;
27021   cp_token *token;
27022   tree expr, noex;
27023   bool noex_expr;
27024
27025   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27026       || keyword == RID_TRANSACTION_RELAXED);
27027
27028   if (!flag_tm)
27029     error (keyword == RID_TRANSACTION_RELAXED
27030            ? G_("%<__transaction_relaxed%> without transactional memory "
27031                 "support enabled")
27032            : G_("%<__transaction_atomic%> without transactional memory "
27033                 "support enabled"));
27034
27035   token = cp_parser_require_keyword (parser, keyword,
27036       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27037           : RT_TRANSACTION_RELAXED));
27038   gcc_assert (token != NULL);
27039
27040   if (keyword == RID_TRANSACTION_RELAXED)
27041     this_in |= TM_STMT_ATTR_RELAXED;
27042
27043   /* Set this early.  This might mean that we allow transaction_cancel in
27044      an expression that we find out later actually has to be a constexpr.
27045      However, we expect that cxx_constant_value will be able to deal with
27046      this; also, if the noexcept has no constexpr, then what we parse next
27047      really is a transaction's body.  */
27048   parser->in_transaction = this_in;
27049
27050   /* Parse a noexcept specification.  */
27051   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27052                                                true);
27053
27054   if (!noex || !noex_expr
27055       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27056     {
27057       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27058
27059       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27060       finish_parenthesized_expr (expr);
27061
27062       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27063     }
27064   else
27065     {
27066       /* The only expression that is available got parsed for the noexcept
27067          already.  noexcept is true then.  */
27068       expr = noex;
27069       noex = boolean_true_node;
27070     }
27071
27072   expr = build_transaction_expr (token->location, expr, this_in, noex);
27073   parser->in_transaction = old_in;
27074
27075   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27076     return error_mark_node;
27077
27078   return (flag_tm ? expr : error_mark_node);
27079 }
27080
27081 /* Parse a function-transaction-block.
27082
27083    function-transaction-block:
27084      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27085          function-body
27086      __transaction_atomic txn-attribute[opt] function-try-block
27087      __transaction_relaxed ctor-initializer[opt] function-body
27088      __transaction_relaxed function-try-block
27089 */
27090
27091 static bool
27092 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27093 {
27094   unsigned char old_in = parser->in_transaction;
27095   unsigned char new_in = 1;
27096   tree compound_stmt, stmt, attrs;
27097   bool ctor_initializer_p;
27098   cp_token *token;
27099
27100   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27101       || keyword == RID_TRANSACTION_RELAXED);
27102   token = cp_parser_require_keyword (parser, keyword,
27103       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27104           : RT_TRANSACTION_RELAXED));
27105   gcc_assert (token != NULL);
27106
27107   if (keyword == RID_TRANSACTION_RELAXED)
27108     new_in |= TM_STMT_ATTR_RELAXED;
27109   else
27110     {
27111       attrs = cp_parser_txn_attribute_opt (parser);
27112       if (attrs)
27113         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27114     }
27115
27116   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27117
27118   parser->in_transaction = new_in;
27119
27120   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27121     ctor_initializer_p = cp_parser_function_try_block (parser);
27122   else
27123     ctor_initializer_p
27124       = cp_parser_ctor_initializer_opt_and_function_body (parser);
27125
27126   parser->in_transaction = old_in;
27127
27128   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27129
27130   return ctor_initializer_p;
27131 }
27132
27133 /* Parse a __transaction_cancel statement.
27134
27135    cancel-statement:
27136      __transaction_cancel txn-attribute[opt] ;
27137      __transaction_cancel txn-attribute[opt] throw-expression ;
27138
27139    ??? Cancel and throw is not yet implemented.  */
27140
27141 static tree
27142 cp_parser_transaction_cancel (cp_parser *parser)
27143 {
27144   cp_token *token;
27145   bool is_outer = false;
27146   tree stmt, attrs;
27147
27148   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27149                                      RT_TRANSACTION_CANCEL);
27150   gcc_assert (token != NULL);
27151
27152   attrs = cp_parser_txn_attribute_opt (parser);
27153   if (attrs)
27154     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27155
27156   /* ??? Parse cancel-and-throw here.  */
27157
27158   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27159
27160   if (!flag_tm)
27161     {
27162       error_at (token->location, "%<__transaction_cancel%> without "
27163                 "transactional memory support enabled");
27164       return error_mark_node;
27165     }
27166   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27167     {
27168       error_at (token->location, "%<__transaction_cancel%> within a "
27169                 "%<__transaction_relaxed%>");
27170       return error_mark_node;
27171     }
27172   else if (is_outer)
27173     {
27174       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27175           && !is_tm_may_cancel_outer (current_function_decl))
27176         {
27177           error_at (token->location, "outer %<__transaction_cancel%> not "
27178                     "within outer %<__transaction_atomic%>");
27179           error_at (token->location,
27180                     "  or a %<transaction_may_cancel_outer%> function");
27181           return error_mark_node;
27182         }
27183     }
27184   else if (parser->in_transaction == 0)
27185     {
27186       error_at (token->location, "%<__transaction_cancel%> not within "
27187                 "%<__transaction_atomic%>");
27188       return error_mark_node;
27189     }
27190
27191   stmt = build_tm_abort_call (token->location, is_outer);
27192   add_stmt (stmt);
27193   finish_stmt ();
27194
27195   return stmt;
27196 }
27197 \f
27198 /* The parser.  */
27199
27200 static GTY (()) cp_parser *the_parser;
27201
27202 \f
27203 /* Special handling for the first token or line in the file.  The first
27204    thing in the file might be #pragma GCC pch_preprocess, which loads a
27205    PCH file, which is a GC collection point.  So we need to handle this
27206    first pragma without benefit of an existing lexer structure.
27207
27208    Always returns one token to the caller in *FIRST_TOKEN.  This is
27209    either the true first token of the file, or the first token after
27210    the initial pragma.  */
27211
27212 static void
27213 cp_parser_initial_pragma (cp_token *first_token)
27214 {
27215   tree name = NULL;
27216
27217   cp_lexer_get_preprocessor_token (NULL, first_token);
27218   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27219     return;
27220
27221   cp_lexer_get_preprocessor_token (NULL, first_token);
27222   if (first_token->type == CPP_STRING)
27223     {
27224       name = first_token->u.value;
27225
27226       cp_lexer_get_preprocessor_token (NULL, first_token);
27227       if (first_token->type != CPP_PRAGMA_EOL)
27228         error_at (first_token->location,
27229                   "junk at end of %<#pragma GCC pch_preprocess%>");
27230     }
27231   else
27232     error_at (first_token->location, "expected string literal");
27233
27234   /* Skip to the end of the pragma.  */
27235   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27236     cp_lexer_get_preprocessor_token (NULL, first_token);
27237
27238   /* Now actually load the PCH file.  */
27239   if (name)
27240     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27241
27242   /* Read one more token to return to our caller.  We have to do this
27243      after reading the PCH file in, since its pointers have to be
27244      live.  */
27245   cp_lexer_get_preprocessor_token (NULL, first_token);
27246 }
27247
27248 /* Normal parsing of a pragma token.  Here we can (and must) use the
27249    regular lexer.  */
27250
27251 static bool
27252 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27253 {
27254   cp_token *pragma_tok;
27255   unsigned int id;
27256
27257   pragma_tok = cp_lexer_consume_token (parser->lexer);
27258   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27259   parser->lexer->in_pragma = true;
27260
27261   id = pragma_tok->pragma_kind;
27262   switch (id)
27263     {
27264     case PRAGMA_GCC_PCH_PREPROCESS:
27265       error_at (pragma_tok->location,
27266                 "%<#pragma GCC pch_preprocess%> must be first");
27267       break;
27268
27269     case PRAGMA_OMP_BARRIER:
27270       switch (context)
27271         {
27272         case pragma_compound:
27273           cp_parser_omp_barrier (parser, pragma_tok);
27274           return false;
27275         case pragma_stmt:
27276           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27277                     "used in compound statements");
27278           break;
27279         default:
27280           goto bad_stmt;
27281         }
27282       break;
27283
27284     case PRAGMA_OMP_FLUSH:
27285       switch (context)
27286         {
27287         case pragma_compound:
27288           cp_parser_omp_flush (parser, pragma_tok);
27289           return false;
27290         case pragma_stmt:
27291           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27292                     "used in compound statements");
27293           break;
27294         default:
27295           goto bad_stmt;
27296         }
27297       break;
27298
27299     case PRAGMA_OMP_TASKWAIT:
27300       switch (context)
27301         {
27302         case pragma_compound:
27303           cp_parser_omp_taskwait (parser, pragma_tok);
27304           return false;
27305         case pragma_stmt:
27306           error_at (pragma_tok->location,
27307                     "%<#pragma omp taskwait%> may only be "
27308                     "used in compound statements");
27309           break;
27310         default:
27311           goto bad_stmt;
27312         }
27313       break;
27314
27315     case PRAGMA_OMP_TASKYIELD:
27316       switch (context)
27317         {
27318         case pragma_compound:
27319           cp_parser_omp_taskyield (parser, pragma_tok);
27320           return false;
27321         case pragma_stmt:
27322           error_at (pragma_tok->location,
27323                     "%<#pragma omp taskyield%> may only be "
27324                     "used in compound statements");
27325           break;
27326         default:
27327           goto bad_stmt;
27328         }
27329       break;
27330
27331     case PRAGMA_OMP_THREADPRIVATE:
27332       cp_parser_omp_threadprivate (parser, pragma_tok);
27333       return false;
27334
27335     case PRAGMA_OMP_ATOMIC:
27336     case PRAGMA_OMP_CRITICAL:
27337     case PRAGMA_OMP_FOR:
27338     case PRAGMA_OMP_MASTER:
27339     case PRAGMA_OMP_ORDERED:
27340     case PRAGMA_OMP_PARALLEL:
27341     case PRAGMA_OMP_SECTIONS:
27342     case PRAGMA_OMP_SINGLE:
27343     case PRAGMA_OMP_TASK:
27344       if (context == pragma_external)
27345         goto bad_stmt;
27346       cp_parser_omp_construct (parser, pragma_tok);
27347       return true;
27348
27349     case PRAGMA_OMP_SECTION:
27350       error_at (pragma_tok->location, 
27351                 "%<#pragma omp section%> may only be used in "
27352                 "%<#pragma omp sections%> construct");
27353       break;
27354
27355     default:
27356       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27357       c_invoke_pragma_handler (id);
27358       break;
27359
27360     bad_stmt:
27361       cp_parser_error (parser, "expected declaration specifiers");
27362       break;
27363     }
27364
27365   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27366   return false;
27367 }
27368
27369 /* The interface the pragma parsers have to the lexer.  */
27370
27371 enum cpp_ttype
27372 pragma_lex (tree *value)
27373 {
27374   cp_token *tok;
27375   enum cpp_ttype ret;
27376
27377   tok = cp_lexer_peek_token (the_parser->lexer);
27378
27379   ret = tok->type;
27380   *value = tok->u.value;
27381
27382   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27383     ret = CPP_EOF;
27384   else if (ret == CPP_STRING)
27385     *value = cp_parser_string_literal (the_parser, false, false);
27386   else
27387     {
27388       cp_lexer_consume_token (the_parser->lexer);
27389       if (ret == CPP_KEYWORD)
27390         ret = CPP_NAME;
27391     }
27392
27393   return ret;
27394 }
27395
27396 \f
27397 /* External interface.  */
27398
27399 /* Parse one entire translation unit.  */
27400
27401 void
27402 c_parse_file (void)
27403 {
27404   static bool already_called = false;
27405
27406   if (already_called)
27407     {
27408       sorry ("inter-module optimizations not implemented for C++");
27409       return;
27410     }
27411   already_called = true;
27412
27413   the_parser = cp_parser_new ();
27414   push_deferring_access_checks (flag_access_control
27415                                 ? dk_no_deferred : dk_no_check);
27416   cp_parser_translation_unit (the_parser);
27417   the_parser = NULL;
27418 }
27419
27420 #include "gt-cp-parser.h"