OSDN Git Service

d4947e7c5d4513d056f7b6f58c80909e3cfd966f
[pf3gnuchains/gcc-fork.git] / gcc / cp / parser.c
1 /* C++ Parser.
2    Copyright (C) 2000, 2001, 2002, 2003, 2004,
3    2005, 2007, 2008, 2009, 2010, 2011  Free Software Foundation, Inc.
4    Written by Mark Mitchell <mark@codesourcery.com>.
5
6    This file is part of GCC.
7
8    GCC is free software; you can redistribute it and/or modify it
9    under the terms of the GNU General Public License as published by
10    the Free Software Foundation; either version 3, or (at your option)
11    any later version.
12
13    GCC is distributed in the hope that it will be useful, but
14    WITHOUT ANY WARRANTY; without even the implied warranty of
15    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
16    General Public License for more details.
17
18 You should have received a copy of the GNU General Public License
19 along with GCC; see the file COPYING3.  If not see
20 <http://www.gnu.org/licenses/>.  */
21
22 #include "config.h"
23 #include "system.h"
24 #include "coretypes.h"
25 #include "tm.h"
26 #include "timevar.h"
27 #include "cpplib.h"
28 #include "tree.h"
29 #include "cp-tree.h"
30 #include "intl.h"
31 #include "c-family/c-pragma.h"
32 #include "decl.h"
33 #include "flags.h"
34 #include "diagnostic-core.h"
35 #include "output.h"
36 #include "target.h"
37 #include "cgraph.h"
38 #include "c-family/c-common.h"
39 #include "c-family/c-objc.h"
40 #include "plugin.h"
41 #include "tree-pretty-print.h"
42 #include "parser.h"
43
44 \f
45 /* The lexer.  */
46
47 /* The cp_lexer_* routines mediate between the lexer proper (in libcpp
48    and c-lex.c) and the C++ parser.  */
49
50 static cp_token eof_token =
51 {
52   CPP_EOF, RID_MAX, 0, PRAGMA_NONE, false, false, false, 0, { NULL }
53 };
54
55 /* The various kinds of non integral constant we encounter. */
56 typedef enum non_integral_constant {
57   NIC_NONE,
58   /* floating-point literal */
59   NIC_FLOAT,
60   /* %<this%> */
61   NIC_THIS,
62   /* %<__FUNCTION__%> */
63   NIC_FUNC_NAME,
64   /* %<__PRETTY_FUNCTION__%> */
65   NIC_PRETTY_FUNC,
66   /* %<__func__%> */
67   NIC_C99_FUNC,
68   /* "%<va_arg%> */
69   NIC_VA_ARG,
70   /* a cast */
71   NIC_CAST,
72   /* %<typeid%> operator */
73   NIC_TYPEID,
74   /* non-constant compound literals */
75   NIC_NCC,
76   /* a function call */
77   NIC_FUNC_CALL,
78   /* an increment */
79   NIC_INC,
80   /* an decrement */
81   NIC_DEC,
82   /* an array reference */
83   NIC_ARRAY_REF,
84   /* %<->%> */
85   NIC_ARROW,
86   /* %<.%> */
87   NIC_POINT,
88   /* the address of a label */
89   NIC_ADDR_LABEL,
90   /* %<*%> */
91   NIC_STAR,
92   /* %<&%> */
93   NIC_ADDR,
94   /* %<++%> */
95   NIC_PREINCREMENT,
96   /* %<--%> */
97   NIC_PREDECREMENT,
98   /* %<new%> */
99   NIC_NEW,
100   /* %<delete%> */
101   NIC_DEL,
102   /* calls to overloaded operators */
103   NIC_OVERLOADED,
104   /* an assignment */
105   NIC_ASSIGNMENT,
106   /* a comma operator */
107   NIC_COMMA,
108   /* a call to a constructor */
109   NIC_CONSTRUCTOR,
110   /* a transaction expression */
111   NIC_TRANSACTION
112 } non_integral_constant;
113
114 /* The various kinds of errors about name-lookup failing. */
115 typedef enum name_lookup_error {
116   /* NULL */
117   NLE_NULL,
118   /* is not a type */
119   NLE_TYPE,
120   /* is not a class or namespace */
121   NLE_CXX98,
122   /* is not a class, namespace, or enumeration */
123   NLE_NOT_CXX98
124 } name_lookup_error;
125
126 /* The various kinds of required token */
127 typedef enum required_token {
128   RT_NONE,
129   RT_SEMICOLON,  /* ';' */
130   RT_OPEN_PAREN, /* '(' */
131   RT_CLOSE_BRACE, /* '}' */
132   RT_OPEN_BRACE,  /* '{' */
133   RT_CLOSE_SQUARE, /* ']' */
134   RT_OPEN_SQUARE,  /* '[' */
135   RT_COMMA, /* ',' */
136   RT_SCOPE, /* '::' */
137   RT_LESS, /* '<' */
138   RT_GREATER, /* '>' */
139   RT_EQ, /* '=' */
140   RT_ELLIPSIS, /* '...' */
141   RT_MULT, /* '*' */
142   RT_COMPL, /* '~' */
143   RT_COLON, /* ':' */
144   RT_COLON_SCOPE, /* ':' or '::' */
145   RT_CLOSE_PAREN, /* ')' */
146   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
147   RT_PRAGMA_EOL, /* end of line */
148   RT_NAME, /* identifier */
149
150   /* The type is CPP_KEYWORD */
151   RT_NEW, /* new */
152   RT_DELETE, /* delete */
153   RT_RETURN, /* return */
154   RT_WHILE, /* while */
155   RT_EXTERN, /* extern */
156   RT_STATIC_ASSERT, /* static_assert */
157   RT_DECLTYPE, /* decltype */
158   RT_OPERATOR, /* operator */
159   RT_CLASS, /* class */
160   RT_TEMPLATE, /* template */
161   RT_NAMESPACE, /* namespace */
162   RT_USING, /* using */
163   RT_ASM, /* asm */
164   RT_TRY, /* try */
165   RT_CATCH, /* catch */
166   RT_THROW, /* throw */
167   RT_LABEL, /* __label__ */
168   RT_AT_TRY, /* @try */
169   RT_AT_SYNCHRONIZED, /* @synchronized */
170   RT_AT_THROW, /* @throw */
171
172   RT_SELECT,  /* selection-statement */
173   RT_INTERATION, /* iteration-statement */
174   RT_JUMP, /* jump-statement */
175   RT_CLASS_KEY, /* class-key */
176   RT_CLASS_TYPENAME_TEMPLATE, /* class, typename, or template */
177   RT_TRANSACTION_ATOMIC, /* __transaction_atomic */
178   RT_TRANSACTION_RELAXED, /* __transaction_relaxed */
179   RT_TRANSACTION_CANCEL /* __transaction_cancel */
180 } required_token;
181
182 /* Prototypes.  */
183
184 static cp_lexer *cp_lexer_new_main
185   (void);
186 static cp_lexer *cp_lexer_new_from_tokens
187   (cp_token_cache *tokens);
188 static void cp_lexer_destroy
189   (cp_lexer *);
190 static int cp_lexer_saving_tokens
191   (const cp_lexer *);
192 static cp_token *cp_lexer_token_at
193   (cp_lexer *, cp_token_position);
194 static void cp_lexer_get_preprocessor_token
195   (cp_lexer *, cp_token *);
196 static inline cp_token *cp_lexer_peek_token
197   (cp_lexer *);
198 static cp_token *cp_lexer_peek_nth_token
199   (cp_lexer *, size_t);
200 static inline bool cp_lexer_next_token_is
201   (cp_lexer *, enum cpp_ttype);
202 static bool cp_lexer_next_token_is_not
203   (cp_lexer *, enum cpp_ttype);
204 static bool cp_lexer_next_token_is_keyword
205   (cp_lexer *, enum rid);
206 static cp_token *cp_lexer_consume_token
207   (cp_lexer *);
208 static void cp_lexer_purge_token
209   (cp_lexer *);
210 static void cp_lexer_purge_tokens_after
211   (cp_lexer *, cp_token_position);
212 static void cp_lexer_save_tokens
213   (cp_lexer *);
214 static void cp_lexer_commit_tokens
215   (cp_lexer *);
216 static void cp_lexer_rollback_tokens
217   (cp_lexer *);
218 static void cp_lexer_print_token
219   (FILE *, cp_token *);
220 static inline bool cp_lexer_debugging_p
221   (cp_lexer *);
222 static void cp_lexer_start_debugging
223   (cp_lexer *) ATTRIBUTE_UNUSED;
224 static void cp_lexer_stop_debugging
225   (cp_lexer *) ATTRIBUTE_UNUSED;
226
227 static cp_token_cache *cp_token_cache_new
228   (cp_token *, cp_token *);
229
230 static void cp_parser_initial_pragma
231   (cp_token *);
232
233 static tree cp_literal_operator_id
234   (const char *);
235
236 /* Manifest constants.  */
237 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
238 #define CP_SAVED_TOKEN_STACK 5
239
240 /* Variables.  */
241
242 /* The stream to which debugging output should be written.  */
243 static FILE *cp_lexer_debug_stream;
244
245 /* Nonzero if we are parsing an unevaluated operand: an operand to
246    sizeof, typeof, or alignof.  */
247 int cp_unevaluated_operand;
248
249 /* Dump up to NUM tokens in BUFFER to FILE starting with token
250    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
251    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
252    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
253    highlighted by surrounding it in [[ ]].  */
254
255 static void
256 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
257                       cp_token *start_token, unsigned num,
258                       cp_token *curr_token)
259 {
260   unsigned i, nprinted;
261   cp_token *token;
262   bool do_print;
263
264   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
265
266   if (buffer == NULL)
267     return;
268
269   if (num == 0)
270     num = VEC_length (cp_token, buffer);
271
272   if (start_token == NULL)
273     start_token = VEC_address (cp_token, buffer);
274
275   if (start_token > VEC_address (cp_token, buffer))
276     {
277       cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
278       fprintf (file, " ... ");
279     }
280
281   do_print = false;
282   nprinted = 0;
283   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
284     {
285       if (token == start_token)
286         do_print = true;
287
288       if (!do_print)
289         continue;
290
291       nprinted++;
292       if (token == curr_token)
293         fprintf (file, "[[");
294
295       cp_lexer_print_token (file, token);
296
297       if (token == curr_token)
298         fprintf (file, "]]");
299
300       switch (token->type)
301         {
302           case CPP_SEMICOLON:
303           case CPP_OPEN_BRACE:
304           case CPP_CLOSE_BRACE:
305           case CPP_EOF:
306             fputc ('\n', file);
307             break;
308
309           default:
310             fputc (' ', file);
311         }
312     }
313
314   if (i == num && i < VEC_length (cp_token, buffer))
315     {
316       fprintf (file, " ... ");
317       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
318                             VEC_length (cp_token, buffer) - 1));
319     }
320
321   fprintf (file, "\n");
322 }
323
324
325 /* Dump all tokens in BUFFER to stderr.  */
326
327 void
328 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
329 {
330   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
331 }
332
333
334 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
335    description for T.  */
336
337 static void
338 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
339 {
340   if (t)
341     {
342       fprintf (file, "%s: ", desc);
343       print_node_brief (file, "", t, 0);
344     }
345 }
346
347
348 /* Dump parser context C to FILE.  */
349
350 static void
351 cp_debug_print_context (FILE *file, cp_parser_context *c)
352 {
353   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
354   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
355   print_node_brief (file, "", c->object_type, 0);
356   fprintf (file, "}\n");
357 }
358
359
360 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
361
362 static void
363 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
364 {
365   unsigned i;
366   cp_parser_context *c;
367
368   fprintf (file, "Parsing context stack:\n");
369   for (i = 0, c = first; c; c = c->next, i++)
370     {
371       fprintf (file, "\t#%u: ", i);
372       cp_debug_print_context (file, c);
373     }
374 }
375
376
377 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
378
379 static void
380 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
381 {
382   if (flag)
383     fprintf (file, "%s: true\n", desc);
384 }
385
386
387 /* Print an unparsed function entry UF to FILE.  */
388
389 static void
390 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
391 {
392   unsigned i;
393   cp_default_arg_entry *default_arg_fn;
394   tree fn;
395
396   fprintf (file, "\tFunctions with default args:\n");
397   for (i = 0;
398        VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
399                     default_arg_fn);
400        i++)
401     {
402       fprintf (file, "\t\tClass type: ");
403       print_node_brief (file, "", default_arg_fn->class_type, 0);
404       fprintf (file, "\t\tDeclaration: ");
405       print_node_brief (file, "", default_arg_fn->decl, 0);
406       fprintf (file, "\n");
407     }
408
409   fprintf (file, "\n\tFunctions with definitions that require "
410            "post-processing\n\t\t");
411   for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
412     {
413       print_node_brief (file, "", fn, 0);
414       fprintf (file, " ");
415     }
416   fprintf (file, "\n");
417
418   fprintf (file, "\n\tNon-static data members with initializers that require "
419            "post-processing\n\t\t");
420   for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
421     {
422       print_node_brief (file, "", fn, 0);
423       fprintf (file, " ");
424     }
425   fprintf (file, "\n");
426 }
427
428
429 /* Print the stack of unparsed member functions S to FILE.  */
430
431 static void
432 cp_debug_print_unparsed_queues (FILE *file,
433                                 VEC(cp_unparsed_functions_entry, gc) *s)
434 {
435   unsigned i;
436   cp_unparsed_functions_entry *uf;
437
438   fprintf (file, "Unparsed functions\n");
439   for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
440     {
441       fprintf (file, "#%u:\n", i);
442       cp_debug_print_unparsed_function (file, uf);
443     }
444 }
445
446
447 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
448    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
449
450 static void
451 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
452 {
453   cp_token *next_token, *first_token, *start_token;
454
455   if (file == NULL)
456     file = stderr;
457
458   next_token = parser->lexer->next_token;
459   first_token = VEC_address (cp_token, parser->lexer->buffer);
460   start_token = (next_token > first_token + window_size / 2)
461                 ? next_token - window_size / 2
462                 : first_token;
463   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
464                         next_token);
465 }
466
467
468 /* Dump debugging information for the given PARSER.  If FILE is NULL,
469    the output is printed on stderr.  */
470
471 void
472 cp_debug_parser (FILE *file, cp_parser *parser)
473 {
474   const size_t window_size = 20;
475   cp_token *token;
476   expanded_location eloc;
477
478   if (file == NULL)
479     file = stderr;
480
481   fprintf (file, "Parser state\n\n");
482   fprintf (file, "Number of tokens: %u\n",
483            VEC_length (cp_token, parser->lexer->buffer));
484   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
485   cp_debug_print_tree_if_set (file, "Object scope",
486                                      parser->object_scope);
487   cp_debug_print_tree_if_set (file, "Qualifying scope",
488                                      parser->qualifying_scope);
489   cp_debug_print_context_stack (file, parser->context);
490   cp_debug_print_flag (file, "Allow GNU extensions",
491                               parser->allow_gnu_extensions_p);
492   cp_debug_print_flag (file, "'>' token is greater-than",
493                               parser->greater_than_is_operator_p);
494   cp_debug_print_flag (file, "Default args allowed in current "
495                               "parameter list", parser->default_arg_ok_p);
496   cp_debug_print_flag (file, "Parsing integral constant-expression",
497                               parser->integral_constant_expression_p);
498   cp_debug_print_flag (file, "Allow non-constant expression in current "
499                               "constant-expression",
500                               parser->allow_non_integral_constant_expression_p);
501   cp_debug_print_flag (file, "Seen non-constant expression",
502                               parser->non_integral_constant_expression_p);
503   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
504                               "current context",
505                               parser->local_variables_forbidden_p);
506   cp_debug_print_flag (file, "In unbraced linkage specification",
507                               parser->in_unbraced_linkage_specification_p);
508   cp_debug_print_flag (file, "Parsing a declarator",
509                               parser->in_declarator_p);
510   cp_debug_print_flag (file, "In template argument list",
511                               parser->in_template_argument_list_p);
512   cp_debug_print_flag (file, "Parsing an iteration statement",
513                               parser->in_statement & IN_ITERATION_STMT);
514   cp_debug_print_flag (file, "Parsing a switch statement",
515                               parser->in_statement & IN_SWITCH_STMT);
516   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
517                               parser->in_statement & IN_OMP_BLOCK);
518   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
519                               parser->in_statement & IN_OMP_FOR);
520   cp_debug_print_flag (file, "Parsing an if statement",
521                               parser->in_statement & IN_IF_STMT);
522   cp_debug_print_flag (file, "Parsing a type-id in an expression "
523                               "context", parser->in_type_id_in_expr_p);
524   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
525                               parser->implicit_extern_c);
526   cp_debug_print_flag (file, "String expressions should be translated "
527                               "to execution character set",
528                               parser->translate_strings_p);
529   cp_debug_print_flag (file, "Parsing function body outside of a "
530                               "local class", parser->in_function_body);
531   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
532                               parser->colon_corrects_to_scope_p);
533   if (parser->type_definition_forbidden_message)
534     fprintf (file, "Error message for forbidden type definitions: %s\n",
535              parser->type_definition_forbidden_message);
536   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
537   fprintf (file, "Number of class definitions in progress: %u\n",
538            parser->num_classes_being_defined);
539   fprintf (file, "Number of template parameter lists for the current "
540            "declaration: %u\n", parser->num_template_parameter_lists);
541   cp_debug_parser_tokens (file, parser, window_size);
542   token = parser->lexer->next_token;
543   fprintf (file, "Next token to parse:\n");
544   fprintf (file, "\tToken:  ");
545   cp_lexer_print_token (file, token);
546   eloc = expand_location (token->location);
547   fprintf (file, "\n\tFile:   %s\n", eloc.file);
548   fprintf (file, "\tLine:   %d\n", eloc.line);
549   fprintf (file, "\tColumn: %d\n", eloc.column);
550 }
551
552
553 /* Allocate memory for a new lexer object and return it.  */
554
555 static cp_lexer *
556 cp_lexer_alloc (void)
557 {
558   cp_lexer *lexer;
559
560   c_common_no_more_pch ();
561
562   /* Allocate the memory.  */
563   lexer = ggc_alloc_cleared_cp_lexer ();
564
565   /* Initially we are not debugging.  */
566   lexer->debugging_p = false;
567
568   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
569                                    CP_SAVED_TOKEN_STACK);
570
571   /* Create the buffer.  */
572   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
573
574   return lexer;
575 }
576
577
578 /* Create a new main C++ lexer, the lexer that gets tokens from the
579    preprocessor.  */
580
581 static cp_lexer *
582 cp_lexer_new_main (void)
583 {
584   cp_lexer *lexer;
585   cp_token token;
586
587   /* It's possible that parsing the first pragma will load a PCH file,
588      which is a GC collection point.  So we have to do that before
589      allocating any memory.  */
590   cp_parser_initial_pragma (&token);
591
592   lexer = cp_lexer_alloc ();
593
594   /* Put the first token in the buffer.  */
595   VEC_quick_push (cp_token, lexer->buffer, &token);
596
597   /* Get the remaining tokens from the preprocessor.  */
598   while (token.type != CPP_EOF)
599     {
600       cp_lexer_get_preprocessor_token (lexer, &token);
601       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
602     }
603
604   lexer->last_token = VEC_address (cp_token, lexer->buffer)
605                       + VEC_length (cp_token, lexer->buffer)
606                       - 1;
607   lexer->next_token = VEC_length (cp_token, lexer->buffer)
608                       ? VEC_address (cp_token, lexer->buffer)
609                       : &eof_token;
610
611   /* Subsequent preprocessor diagnostics should use compiler
612      diagnostic functions to get the compiler source location.  */
613   done_lexing = true;
614
615   gcc_assert (!lexer->next_token->purged_p);
616   return lexer;
617 }
618
619 /* Create a new lexer whose token stream is primed with the tokens in
620    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
621
622 static cp_lexer *
623 cp_lexer_new_from_tokens (cp_token_cache *cache)
624 {
625   cp_token *first = cache->first;
626   cp_token *last = cache->last;
627   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
628
629   /* We do not own the buffer.  */
630   lexer->buffer = NULL;
631   lexer->next_token = first == last ? &eof_token : first;
632   lexer->last_token = last;
633
634   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
635                                    CP_SAVED_TOKEN_STACK);
636
637   /* Initially we are not debugging.  */
638   lexer->debugging_p = false;
639
640   gcc_assert (!lexer->next_token->purged_p);
641   return lexer;
642 }
643
644 /* Frees all resources associated with LEXER.  */
645
646 static void
647 cp_lexer_destroy (cp_lexer *lexer)
648 {
649   VEC_free (cp_token, gc, lexer->buffer);
650   VEC_free (cp_token_position, heap, lexer->saved_tokens);
651   ggc_free (lexer);
652 }
653
654 /* Returns nonzero if debugging information should be output.  */
655
656 static inline bool
657 cp_lexer_debugging_p (cp_lexer *lexer)
658 {
659   return lexer->debugging_p;
660 }
661
662
663 static inline cp_token_position
664 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
665 {
666   gcc_assert (!previous_p || lexer->next_token != &eof_token);
667
668   return lexer->next_token - previous_p;
669 }
670
671 static inline cp_token *
672 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
673 {
674   return pos;
675 }
676
677 static inline void
678 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
679 {
680   lexer->next_token = cp_lexer_token_at (lexer, pos);
681 }
682
683 static inline cp_token_position
684 cp_lexer_previous_token_position (cp_lexer *lexer)
685 {
686   if (lexer->next_token == &eof_token)
687     return lexer->last_token - 1;
688   else
689     return cp_lexer_token_position (lexer, true);
690 }
691
692 static inline cp_token *
693 cp_lexer_previous_token (cp_lexer *lexer)
694 {
695   cp_token_position tp = cp_lexer_previous_token_position (lexer);
696
697   return cp_lexer_token_at (lexer, tp);
698 }
699
700 /* nonzero if we are presently saving tokens.  */
701
702 static inline int
703 cp_lexer_saving_tokens (const cp_lexer* lexer)
704 {
705   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
706 }
707
708 /* Store the next token from the preprocessor in *TOKEN.  Return true
709    if we reach EOF.  If LEXER is NULL, assume we are handling an
710    initial #pragma pch_preprocess, and thus want the lexer to return
711    processed strings.  */
712
713 static void
714 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
715 {
716   static int is_extern_c = 0;
717
718    /* Get a new token from the preprocessor.  */
719   token->type
720     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
721                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
722   token->keyword = RID_MAX;
723   token->pragma_kind = PRAGMA_NONE;
724   token->purged_p = false;
725
726   /* On some systems, some header files are surrounded by an
727      implicit extern "C" block.  Set a flag in the token if it
728      comes from such a header.  */
729   is_extern_c += pending_lang_change;
730   pending_lang_change = 0;
731   token->implicit_extern_c = is_extern_c > 0;
732
733   /* Check to see if this token is a keyword.  */
734   if (token->type == CPP_NAME)
735     {
736       if (C_IS_RESERVED_WORD (token->u.value))
737         {
738           /* Mark this token as a keyword.  */
739           token->type = CPP_KEYWORD;
740           /* Record which keyword.  */
741           token->keyword = C_RID_CODE (token->u.value);
742         }
743       else
744         {
745           if (warn_cxx0x_compat
746               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
747               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
748             {
749               /* Warn about the C++0x keyword (but still treat it as
750                  an identifier).  */
751               warning (OPT_Wc__0x_compat, 
752                        "identifier %qE is a keyword in C++11",
753                        token->u.value);
754
755               /* Clear out the C_RID_CODE so we don't warn about this
756                  particular identifier-turned-keyword again.  */
757               C_SET_RID_CODE (token->u.value, RID_MAX);
758             }
759
760           token->ambiguous_p = false;
761           token->keyword = RID_MAX;
762         }
763     }
764   else if (token->type == CPP_AT_NAME)
765     {
766       /* This only happens in Objective-C++; it must be a keyword.  */
767       token->type = CPP_KEYWORD;
768       switch (C_RID_CODE (token->u.value))
769         {
770           /* Replace 'class' with '@class', 'private' with '@private',
771              etc.  This prevents confusion with the C++ keyword
772              'class', and makes the tokens consistent with other
773              Objective-C 'AT' keywords.  For example '@class' is
774              reported as RID_AT_CLASS which is consistent with
775              '@synchronized', which is reported as
776              RID_AT_SYNCHRONIZED.
777           */
778         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
779         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
780         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
781         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
782         case RID_THROW:     token->keyword = RID_AT_THROW; break;
783         case RID_TRY:       token->keyword = RID_AT_TRY; break;
784         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
785         default:            token->keyword = C_RID_CODE (token->u.value);
786         }
787     }
788   else if (token->type == CPP_PRAGMA)
789     {
790       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
791       token->pragma_kind = ((enum pragma_kind)
792                             TREE_INT_CST_LOW (token->u.value));
793       token->u.value = NULL_TREE;
794     }
795 }
796
797 /* Update the globals input_location and the input file stack from TOKEN.  */
798 static inline void
799 cp_lexer_set_source_position_from_token (cp_token *token)
800 {
801   if (token->type != CPP_EOF)
802     {
803       input_location = token->location;
804     }
805 }
806
807 /* Return a pointer to the next token in the token stream, but do not
808    consume it.  */
809
810 static inline cp_token *
811 cp_lexer_peek_token (cp_lexer *lexer)
812 {
813   if (cp_lexer_debugging_p (lexer))
814     {
815       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
816       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
817       putc ('\n', cp_lexer_debug_stream);
818     }
819   return lexer->next_token;
820 }
821
822 /* Return true if the next token has the indicated TYPE.  */
823
824 static inline bool
825 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
826 {
827   return cp_lexer_peek_token (lexer)->type == type;
828 }
829
830 /* Return true if the next token does not have the indicated TYPE.  */
831
832 static inline bool
833 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
834 {
835   return !cp_lexer_next_token_is (lexer, type);
836 }
837
838 /* Return true if the next token is the indicated KEYWORD.  */
839
840 static inline bool
841 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
842 {
843   return cp_lexer_peek_token (lexer)->keyword == keyword;
844 }
845
846 /* Return true if the next token is not the indicated KEYWORD.  */
847
848 static inline bool
849 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
850 {
851   return cp_lexer_peek_token (lexer)->keyword != keyword;
852 }
853
854 /* Return true if the next token is a keyword for a decl-specifier.  */
855
856 static bool
857 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
858 {
859   cp_token *token;
860
861   token = cp_lexer_peek_token (lexer);
862   switch (token->keyword) 
863     {
864       /* auto specifier: storage-class-specifier in C++,
865          simple-type-specifier in C++0x.  */
866     case RID_AUTO:
867       /* Storage classes.  */
868     case RID_REGISTER:
869     case RID_STATIC:
870     case RID_EXTERN:
871     case RID_MUTABLE:
872     case RID_THREAD:
873       /* Elaborated type specifiers.  */
874     case RID_ENUM:
875     case RID_CLASS:
876     case RID_STRUCT:
877     case RID_UNION:
878     case RID_TYPENAME:
879       /* Simple type specifiers.  */
880     case RID_CHAR:
881     case RID_CHAR16:
882     case RID_CHAR32:
883     case RID_WCHAR:
884     case RID_BOOL:
885     case RID_SHORT:
886     case RID_INT:
887     case RID_LONG:
888     case RID_INT128:
889     case RID_SIGNED:
890     case RID_UNSIGNED:
891     case RID_FLOAT:
892     case RID_DOUBLE:
893     case RID_VOID:
894       /* GNU extensions.  */ 
895     case RID_ATTRIBUTE:
896     case RID_TYPEOF:
897       /* C++0x extensions.  */
898     case RID_DECLTYPE:
899     case RID_UNDERLYING_TYPE:
900       return true;
901
902     default:
903       return false;
904     }
905 }
906
907 /* Returns TRUE iff the token T begins a decltype type.  */
908
909 static bool
910 token_is_decltype (cp_token *t)
911 {
912   return (t->keyword == RID_DECLTYPE
913           || t->type == CPP_DECLTYPE);
914 }
915
916 /* Returns TRUE iff the next token begins a decltype type.  */
917
918 static bool
919 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
920 {
921   cp_token *t = cp_lexer_peek_token (lexer);
922   return token_is_decltype (t);
923 }
924
925 /* Return a pointer to the Nth token in the token stream.  If N is 1,
926    then this is precisely equivalent to cp_lexer_peek_token (except
927    that it is not inline).  One would like to disallow that case, but
928    there is one case (cp_parser_nth_token_starts_template_id) where
929    the caller passes a variable for N and it might be 1.  */
930
931 static cp_token *
932 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
933 {
934   cp_token *token;
935
936   /* N is 1-based, not zero-based.  */
937   gcc_assert (n > 0);
938
939   if (cp_lexer_debugging_p (lexer))
940     fprintf (cp_lexer_debug_stream,
941              "cp_lexer: peeking ahead %ld at token: ", (long)n);
942
943   --n;
944   token = lexer->next_token;
945   gcc_assert (!n || token != &eof_token);
946   while (n != 0)
947     {
948       ++token;
949       if (token == lexer->last_token)
950         {
951           token = &eof_token;
952           break;
953         }
954
955       if (!token->purged_p)
956         --n;
957     }
958
959   if (cp_lexer_debugging_p (lexer))
960     {
961       cp_lexer_print_token (cp_lexer_debug_stream, token);
962       putc ('\n', cp_lexer_debug_stream);
963     }
964
965   return token;
966 }
967
968 /* Return the next token, and advance the lexer's next_token pointer
969    to point to the next non-purged token.  */
970
971 static cp_token *
972 cp_lexer_consume_token (cp_lexer* lexer)
973 {
974   cp_token *token = lexer->next_token;
975
976   gcc_assert (token != &eof_token);
977   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
978
979   do
980     {
981       lexer->next_token++;
982       if (lexer->next_token == lexer->last_token)
983         {
984           lexer->next_token = &eof_token;
985           break;
986         }
987
988     }
989   while (lexer->next_token->purged_p);
990
991   cp_lexer_set_source_position_from_token (token);
992
993   /* Provide debugging output.  */
994   if (cp_lexer_debugging_p (lexer))
995     {
996       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
997       cp_lexer_print_token (cp_lexer_debug_stream, token);
998       putc ('\n', cp_lexer_debug_stream);
999     }
1000
1001   return token;
1002 }
1003
1004 /* Permanently remove the next token from the token stream, and
1005    advance the next_token pointer to refer to the next non-purged
1006    token.  */
1007
1008 static void
1009 cp_lexer_purge_token (cp_lexer *lexer)
1010 {
1011   cp_token *tok = lexer->next_token;
1012
1013   gcc_assert (tok != &eof_token);
1014   tok->purged_p = true;
1015   tok->location = UNKNOWN_LOCATION;
1016   tok->u.value = NULL_TREE;
1017   tok->keyword = RID_MAX;
1018
1019   do
1020     {
1021       tok++;
1022       if (tok == lexer->last_token)
1023         {
1024           tok = &eof_token;
1025           break;
1026         }
1027     }
1028   while (tok->purged_p);
1029   lexer->next_token = tok;
1030 }
1031
1032 /* Permanently remove all tokens after TOK, up to, but not
1033    including, the token that will be returned next by
1034    cp_lexer_peek_token.  */
1035
1036 static void
1037 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1038 {
1039   cp_token *peek = lexer->next_token;
1040
1041   if (peek == &eof_token)
1042     peek = lexer->last_token;
1043
1044   gcc_assert (tok < peek);
1045
1046   for ( tok += 1; tok != peek; tok += 1)
1047     {
1048       tok->purged_p = true;
1049       tok->location = UNKNOWN_LOCATION;
1050       tok->u.value = NULL_TREE;
1051       tok->keyword = RID_MAX;
1052     }
1053 }
1054
1055 /* Begin saving tokens.  All tokens consumed after this point will be
1056    preserved.  */
1057
1058 static void
1059 cp_lexer_save_tokens (cp_lexer* lexer)
1060 {
1061   /* Provide debugging output.  */
1062   if (cp_lexer_debugging_p (lexer))
1063     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1064
1065   VEC_safe_push (cp_token_position, heap,
1066                  lexer->saved_tokens, lexer->next_token);
1067 }
1068
1069 /* Commit to the portion of the token stream most recently saved.  */
1070
1071 static void
1072 cp_lexer_commit_tokens (cp_lexer* lexer)
1073 {
1074   /* Provide debugging output.  */
1075   if (cp_lexer_debugging_p (lexer))
1076     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1077
1078   VEC_pop (cp_token_position, lexer->saved_tokens);
1079 }
1080
1081 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1082    to the token stream.  Stop saving tokens.  */
1083
1084 static void
1085 cp_lexer_rollback_tokens (cp_lexer* lexer)
1086 {
1087   /* Provide debugging output.  */
1088   if (cp_lexer_debugging_p (lexer))
1089     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1090
1091   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1092 }
1093
1094 /* Print a representation of the TOKEN on the STREAM.  */
1095
1096 static void
1097 cp_lexer_print_token (FILE * stream, cp_token *token)
1098 {
1099   /* We don't use cpp_type2name here because the parser defines
1100      a few tokens of its own.  */
1101   static const char *const token_names[] = {
1102     /* cpplib-defined token types */
1103 #define OP(e, s) #e,
1104 #define TK(e, s) #e,
1105     TTYPE_TABLE
1106 #undef OP
1107 #undef TK
1108     /* C++ parser token types - see "Manifest constants", above.  */
1109     "KEYWORD",
1110     "TEMPLATE_ID",
1111     "NESTED_NAME_SPECIFIER",
1112   };
1113
1114   /* For some tokens, print the associated data.  */
1115   switch (token->type)
1116     {
1117     case CPP_KEYWORD:
1118       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1119          For example, `struct' is mapped to an INTEGER_CST.  */
1120       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1121         break;
1122       /* else fall through */
1123     case CPP_NAME:
1124       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1125       break;
1126
1127     case CPP_STRING:
1128     case CPP_STRING16:
1129     case CPP_STRING32:
1130     case CPP_WSTRING:
1131     case CPP_UTF8STRING:
1132       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1133       break;
1134
1135     case CPP_NUMBER:
1136       print_generic_expr (stream, token->u.value, 0);
1137       break;
1138
1139     default:
1140       /* If we have a name for the token, print it out.  Otherwise, we
1141          simply give the numeric code.  */
1142       if (token->type < ARRAY_SIZE(token_names))
1143         fputs (token_names[token->type], stream);
1144       else
1145         fprintf (stream, "[%d]", token->type);
1146       break;
1147     }
1148 }
1149
1150 /* Start emitting debugging information.  */
1151
1152 static void
1153 cp_lexer_start_debugging (cp_lexer* lexer)
1154 {
1155   lexer->debugging_p = true;
1156   cp_lexer_debug_stream = stderr;
1157 }
1158
1159 /* Stop emitting debugging information.  */
1160
1161 static void
1162 cp_lexer_stop_debugging (cp_lexer* lexer)
1163 {
1164   lexer->debugging_p = false;
1165   cp_lexer_debug_stream = NULL;
1166 }
1167
1168 /* Create a new cp_token_cache, representing a range of tokens.  */
1169
1170 static cp_token_cache *
1171 cp_token_cache_new (cp_token *first, cp_token *last)
1172 {
1173   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1174   cache->first = first;
1175   cache->last = last;
1176   return cache;
1177 }
1178
1179 \f
1180 /* Decl-specifiers.  */
1181
1182 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1183
1184 static void
1185 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1186 {
1187   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1188 }
1189
1190 /* Declarators.  */
1191
1192 /* Nothing other than the parser should be creating declarators;
1193    declarators are a semi-syntactic representation of C++ entities.
1194    Other parts of the front end that need to create entities (like
1195    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1196
1197 static cp_declarator *make_call_declarator
1198   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1199 static cp_declarator *make_array_declarator
1200   (cp_declarator *, tree);
1201 static cp_declarator *make_pointer_declarator
1202   (cp_cv_quals, cp_declarator *);
1203 static cp_declarator *make_reference_declarator
1204   (cp_cv_quals, cp_declarator *, bool);
1205 static cp_parameter_declarator *make_parameter_declarator
1206   (cp_decl_specifier_seq *, cp_declarator *, tree);
1207 static cp_declarator *make_ptrmem_declarator
1208   (cp_cv_quals, tree, cp_declarator *);
1209
1210 /* An erroneous declarator.  */
1211 static cp_declarator *cp_error_declarator;
1212
1213 /* The obstack on which declarators and related data structures are
1214    allocated.  */
1215 static struct obstack declarator_obstack;
1216
1217 /* Alloc BYTES from the declarator memory pool.  */
1218
1219 static inline void *
1220 alloc_declarator (size_t bytes)
1221 {
1222   return obstack_alloc (&declarator_obstack, bytes);
1223 }
1224
1225 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1226    common to all declarators.  */
1227
1228 static cp_declarator *
1229 make_declarator (cp_declarator_kind kind)
1230 {
1231   cp_declarator *declarator;
1232
1233   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1234   declarator->kind = kind;
1235   declarator->attributes = NULL_TREE;
1236   declarator->declarator = NULL;
1237   declarator->parameter_pack_p = false;
1238   declarator->id_loc = UNKNOWN_LOCATION;
1239
1240   return declarator;
1241 }
1242
1243 /* Make a declarator for a generalized identifier.  If
1244    QUALIFYING_SCOPE is non-NULL, the identifier is
1245    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1246    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1247    is, if any.   */
1248
1249 static cp_declarator *
1250 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1251                     special_function_kind sfk)
1252 {
1253   cp_declarator *declarator;
1254
1255   /* It is valid to write:
1256
1257        class C { void f(); };
1258        typedef C D;
1259        void D::f();
1260
1261      The standard is not clear about whether `typedef const C D' is
1262      legal; as of 2002-09-15 the committee is considering that
1263      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1264      well.  */
1265   if (qualifying_scope && TYPE_P (qualifying_scope))
1266     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1267
1268   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1269               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1270               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1271
1272   declarator = make_declarator (cdk_id);
1273   declarator->u.id.qualifying_scope = qualifying_scope;
1274   declarator->u.id.unqualified_name = unqualified_name;
1275   declarator->u.id.sfk = sfk;
1276   
1277   return declarator;
1278 }
1279
1280 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1281    of modifiers such as const or volatile to apply to the pointer
1282    type, represented as identifiers.  */
1283
1284 cp_declarator *
1285 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1286 {
1287   cp_declarator *declarator;
1288
1289   declarator = make_declarator (cdk_pointer);
1290   declarator->declarator = target;
1291   declarator->u.pointer.qualifiers = cv_qualifiers;
1292   declarator->u.pointer.class_type = NULL_TREE;
1293   if (target)
1294     {
1295       declarator->id_loc = target->id_loc;
1296       declarator->parameter_pack_p = target->parameter_pack_p;
1297       target->parameter_pack_p = false;
1298     }
1299   else
1300     declarator->parameter_pack_p = false;
1301
1302   return declarator;
1303 }
1304
1305 /* Like make_pointer_declarator -- but for references.  */
1306
1307 cp_declarator *
1308 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1309                            bool rvalue_ref)
1310 {
1311   cp_declarator *declarator;
1312
1313   declarator = make_declarator (cdk_reference);
1314   declarator->declarator = target;
1315   declarator->u.reference.qualifiers = cv_qualifiers;
1316   declarator->u.reference.rvalue_ref = rvalue_ref;
1317   if (target)
1318     {
1319       declarator->id_loc = target->id_loc;
1320       declarator->parameter_pack_p = target->parameter_pack_p;
1321       target->parameter_pack_p = false;
1322     }
1323   else
1324     declarator->parameter_pack_p = false;
1325
1326   return declarator;
1327 }
1328
1329 /* Like make_pointer_declarator -- but for a pointer to a non-static
1330    member of CLASS_TYPE.  */
1331
1332 cp_declarator *
1333 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1334                         cp_declarator *pointee)
1335 {
1336   cp_declarator *declarator;
1337
1338   declarator = make_declarator (cdk_ptrmem);
1339   declarator->declarator = pointee;
1340   declarator->u.pointer.qualifiers = cv_qualifiers;
1341   declarator->u.pointer.class_type = class_type;
1342
1343   if (pointee)
1344     {
1345       declarator->parameter_pack_p = pointee->parameter_pack_p;
1346       pointee->parameter_pack_p = false;
1347     }
1348   else
1349     declarator->parameter_pack_p = false;
1350
1351   return declarator;
1352 }
1353
1354 /* Make a declarator for the function given by TARGET, with the
1355    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1356    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1357    indicates what exceptions can be thrown.  */
1358
1359 cp_declarator *
1360 make_call_declarator (cp_declarator *target,
1361                       tree parms,
1362                       cp_cv_quals cv_qualifiers,
1363                       cp_virt_specifiers virt_specifiers,
1364                       tree exception_specification,
1365                       tree late_return_type)
1366 {
1367   cp_declarator *declarator;
1368
1369   declarator = make_declarator (cdk_function);
1370   declarator->declarator = target;
1371   declarator->u.function.parameters = parms;
1372   declarator->u.function.qualifiers = cv_qualifiers;
1373   declarator->u.function.virt_specifiers = virt_specifiers;
1374   declarator->u.function.exception_specification = exception_specification;
1375   declarator->u.function.late_return_type = late_return_type;
1376   if (target)
1377     {
1378       declarator->id_loc = target->id_loc;
1379       declarator->parameter_pack_p = target->parameter_pack_p;
1380       target->parameter_pack_p = false;
1381     }
1382   else
1383     declarator->parameter_pack_p = false;
1384
1385   return declarator;
1386 }
1387
1388 /* Make a declarator for an array of BOUNDS elements, each of which is
1389    defined by ELEMENT.  */
1390
1391 cp_declarator *
1392 make_array_declarator (cp_declarator *element, tree bounds)
1393 {
1394   cp_declarator *declarator;
1395
1396   declarator = make_declarator (cdk_array);
1397   declarator->declarator = element;
1398   declarator->u.array.bounds = bounds;
1399   if (element)
1400     {
1401       declarator->id_loc = element->id_loc;
1402       declarator->parameter_pack_p = element->parameter_pack_p;
1403       element->parameter_pack_p = false;
1404     }
1405   else
1406     declarator->parameter_pack_p = false;
1407
1408   return declarator;
1409 }
1410
1411 /* Determine whether the declarator we've seen so far can be a
1412    parameter pack, when followed by an ellipsis.  */
1413 static bool 
1414 declarator_can_be_parameter_pack (cp_declarator *declarator)
1415 {
1416   /* Search for a declarator name, or any other declarator that goes
1417      after the point where the ellipsis could appear in a parameter
1418      pack. If we find any of these, then this declarator can not be
1419      made into a parameter pack.  */
1420   bool found = false;
1421   while (declarator && !found)
1422     {
1423       switch ((int)declarator->kind)
1424         {
1425         case cdk_id:
1426         case cdk_array:
1427           found = true;
1428           break;
1429
1430         case cdk_error:
1431           return true;
1432
1433         default:
1434           declarator = declarator->declarator;
1435           break;
1436         }
1437     }
1438
1439   return !found;
1440 }
1441
1442 cp_parameter_declarator *no_parameters;
1443
1444 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1445    DECLARATOR and DEFAULT_ARGUMENT.  */
1446
1447 cp_parameter_declarator *
1448 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1449                            cp_declarator *declarator,
1450                            tree default_argument)
1451 {
1452   cp_parameter_declarator *parameter;
1453
1454   parameter = ((cp_parameter_declarator *)
1455                alloc_declarator (sizeof (cp_parameter_declarator)));
1456   parameter->next = NULL;
1457   if (decl_specifiers)
1458     parameter->decl_specifiers = *decl_specifiers;
1459   else
1460     clear_decl_specs (&parameter->decl_specifiers);
1461   parameter->declarator = declarator;
1462   parameter->default_argument = default_argument;
1463   parameter->ellipsis_p = false;
1464
1465   return parameter;
1466 }
1467
1468 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1469
1470 static bool
1471 function_declarator_p (const cp_declarator *declarator)
1472 {
1473   while (declarator)
1474     {
1475       if (declarator->kind == cdk_function
1476           && declarator->declarator->kind == cdk_id)
1477         return true;
1478       if (declarator->kind == cdk_id
1479           || declarator->kind == cdk_error)
1480         return false;
1481       declarator = declarator->declarator;
1482     }
1483   return false;
1484 }
1485  
1486 /* The parser.  */
1487
1488 /* Overview
1489    --------
1490
1491    A cp_parser parses the token stream as specified by the C++
1492    grammar.  Its job is purely parsing, not semantic analysis.  For
1493    example, the parser breaks the token stream into declarators,
1494    expressions, statements, and other similar syntactic constructs.
1495    It does not check that the types of the expressions on either side
1496    of an assignment-statement are compatible, or that a function is
1497    not declared with a parameter of type `void'.
1498
1499    The parser invokes routines elsewhere in the compiler to perform
1500    semantic analysis and to build up the abstract syntax tree for the
1501    code processed.
1502
1503    The parser (and the template instantiation code, which is, in a
1504    way, a close relative of parsing) are the only parts of the
1505    compiler that should be calling push_scope and pop_scope, or
1506    related functions.  The parser (and template instantiation code)
1507    keeps track of what scope is presently active; everything else
1508    should simply honor that.  (The code that generates static
1509    initializers may also need to set the scope, in order to check
1510    access control correctly when emitting the initializers.)
1511
1512    Methodology
1513    -----------
1514
1515    The parser is of the standard recursive-descent variety.  Upcoming
1516    tokens in the token stream are examined in order to determine which
1517    production to use when parsing a non-terminal.  Some C++ constructs
1518    require arbitrary look ahead to disambiguate.  For example, it is
1519    impossible, in the general case, to tell whether a statement is an
1520    expression or declaration without scanning the entire statement.
1521    Therefore, the parser is capable of "parsing tentatively."  When the
1522    parser is not sure what construct comes next, it enters this mode.
1523    Then, while we attempt to parse the construct, the parser queues up
1524    error messages, rather than issuing them immediately, and saves the
1525    tokens it consumes.  If the construct is parsed successfully, the
1526    parser "commits", i.e., it issues any queued error messages and
1527    the tokens that were being preserved are permanently discarded.
1528    If, however, the construct is not parsed successfully, the parser
1529    rolls back its state completely so that it can resume parsing using
1530    a different alternative.
1531
1532    Future Improvements
1533    -------------------
1534
1535    The performance of the parser could probably be improved substantially.
1536    We could often eliminate the need to parse tentatively by looking ahead
1537    a little bit.  In some places, this approach might not entirely eliminate
1538    the need to parse tentatively, but it might still speed up the average
1539    case.  */
1540
1541 /* Flags that are passed to some parsing functions.  These values can
1542    be bitwise-ored together.  */
1543
1544 enum
1545 {
1546   /* No flags.  */
1547   CP_PARSER_FLAGS_NONE = 0x0,
1548   /* The construct is optional.  If it is not present, then no error
1549      should be issued.  */
1550   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1551   /* When parsing a type-specifier, treat user-defined type-names
1552      as non-type identifiers.  */
1553   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1554   /* When parsing a type-specifier, do not try to parse a class-specifier
1555      or enum-specifier.  */
1556   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1557   /* When parsing a decl-specifier-seq, only allow type-specifier or
1558      constexpr.  */
1559   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1560 };
1561
1562 /* This type is used for parameters and variables which hold
1563    combinations of the above flags.  */
1564 typedef int cp_parser_flags;
1565
1566 /* The different kinds of declarators we want to parse.  */
1567
1568 typedef enum cp_parser_declarator_kind
1569 {
1570   /* We want an abstract declarator.  */
1571   CP_PARSER_DECLARATOR_ABSTRACT,
1572   /* We want a named declarator.  */
1573   CP_PARSER_DECLARATOR_NAMED,
1574   /* We don't mind, but the name must be an unqualified-id.  */
1575   CP_PARSER_DECLARATOR_EITHER
1576 } cp_parser_declarator_kind;
1577
1578 /* The precedence values used to parse binary expressions.  The minimum value
1579    of PREC must be 1, because zero is reserved to quickly discriminate
1580    binary operators from other tokens.  */
1581
1582 enum cp_parser_prec
1583 {
1584   PREC_NOT_OPERATOR,
1585   PREC_LOGICAL_OR_EXPRESSION,
1586   PREC_LOGICAL_AND_EXPRESSION,
1587   PREC_INCLUSIVE_OR_EXPRESSION,
1588   PREC_EXCLUSIVE_OR_EXPRESSION,
1589   PREC_AND_EXPRESSION,
1590   PREC_EQUALITY_EXPRESSION,
1591   PREC_RELATIONAL_EXPRESSION,
1592   PREC_SHIFT_EXPRESSION,
1593   PREC_ADDITIVE_EXPRESSION,
1594   PREC_MULTIPLICATIVE_EXPRESSION,
1595   PREC_PM_EXPRESSION,
1596   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1597 };
1598
1599 /* A mapping from a token type to a corresponding tree node type, with a
1600    precedence value.  */
1601
1602 typedef struct cp_parser_binary_operations_map_node
1603 {
1604   /* The token type.  */
1605   enum cpp_ttype token_type;
1606   /* The corresponding tree code.  */
1607   enum tree_code tree_type;
1608   /* The precedence of this operator.  */
1609   enum cp_parser_prec prec;
1610 } cp_parser_binary_operations_map_node;
1611
1612 typedef struct cp_parser_expression_stack_entry
1613 {
1614   /* Left hand side of the binary operation we are currently
1615      parsing.  */
1616   tree lhs;
1617   /* Original tree code for left hand side, if it was a binary
1618      expression itself (used for -Wparentheses).  */
1619   enum tree_code lhs_type;
1620   /* Tree code for the binary operation we are parsing.  */
1621   enum tree_code tree_type;
1622   /* Precedence of the binary operation we are parsing.  */
1623   enum cp_parser_prec prec;
1624 } cp_parser_expression_stack_entry;
1625
1626 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1627    entries because precedence levels on the stack are monotonically
1628    increasing.  */
1629 typedef struct cp_parser_expression_stack_entry
1630   cp_parser_expression_stack[NUM_PREC_VALUES];
1631
1632 /* Prototypes.  */
1633
1634 /* Constructors and destructors.  */
1635
1636 static cp_parser_context *cp_parser_context_new
1637   (cp_parser_context *);
1638
1639 /* Class variables.  */
1640
1641 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1642
1643 /* The operator-precedence table used by cp_parser_binary_expression.
1644    Transformed into an associative array (binops_by_token) by
1645    cp_parser_new.  */
1646
1647 static const cp_parser_binary_operations_map_node binops[] = {
1648   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1649   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1650
1651   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1652   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1653   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1654
1655   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1656   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1657
1658   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1659   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1660
1661   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1662   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1663   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1664   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1665
1666   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1667   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1668
1669   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1670
1671   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1672
1673   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1674
1675   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1676
1677   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1678 };
1679
1680 /* The same as binops, but initialized by cp_parser_new so that
1681    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1682    for speed.  */
1683 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1684
1685 /* Constructors and destructors.  */
1686
1687 /* Construct a new context.  The context below this one on the stack
1688    is given by NEXT.  */
1689
1690 static cp_parser_context *
1691 cp_parser_context_new (cp_parser_context* next)
1692 {
1693   cp_parser_context *context;
1694
1695   /* Allocate the storage.  */
1696   if (cp_parser_context_free_list != NULL)
1697     {
1698       /* Pull the first entry from the free list.  */
1699       context = cp_parser_context_free_list;
1700       cp_parser_context_free_list = context->next;
1701       memset (context, 0, sizeof (*context));
1702     }
1703   else
1704     context = ggc_alloc_cleared_cp_parser_context ();
1705
1706   /* No errors have occurred yet in this context.  */
1707   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1708   /* If this is not the bottommost context, copy information that we
1709      need from the previous context.  */
1710   if (next)
1711     {
1712       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1713          expression, then we are parsing one in this context, too.  */
1714       context->object_type = next->object_type;
1715       /* Thread the stack.  */
1716       context->next = next;
1717     }
1718
1719   return context;
1720 }
1721
1722 /* Managing the unparsed function queues.  */
1723
1724 #define unparsed_funs_with_default_args \
1725   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1726 #define unparsed_funs_with_definitions \
1727   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1728 #define unparsed_nsdmis \
1729   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1730
1731 static void
1732 push_unparsed_function_queues (cp_parser *parser)
1733 {
1734   VEC_safe_push (cp_unparsed_functions_entry, gc,
1735                  parser->unparsed_queues, NULL);
1736   unparsed_funs_with_default_args = NULL;
1737   unparsed_funs_with_definitions = make_tree_vector ();
1738   unparsed_nsdmis = NULL;
1739 }
1740
1741 static void
1742 pop_unparsed_function_queues (cp_parser *parser)
1743 {
1744   release_tree_vector (unparsed_funs_with_definitions);
1745   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1746 }
1747
1748 /* Prototypes.  */
1749
1750 /* Constructors and destructors.  */
1751
1752 static cp_parser *cp_parser_new
1753   (void);
1754
1755 /* Routines to parse various constructs.
1756
1757    Those that return `tree' will return the error_mark_node (rather
1758    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1759    Sometimes, they will return an ordinary node if error-recovery was
1760    attempted, even though a parse error occurred.  So, to check
1761    whether or not a parse error occurred, you should always use
1762    cp_parser_error_occurred.  If the construct is optional (indicated
1763    either by an `_opt' in the name of the function that does the
1764    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1765    the construct is not present.  */
1766
1767 /* Lexical conventions [gram.lex]  */
1768
1769 static tree cp_parser_identifier
1770   (cp_parser *);
1771 static tree cp_parser_string_literal
1772   (cp_parser *, bool, bool);
1773 static tree cp_parser_userdef_char_literal
1774   (cp_parser *);
1775 static tree cp_parser_userdef_string_literal
1776   (cp_token *);
1777 static tree cp_parser_userdef_numeric_literal
1778   (cp_parser *);
1779
1780 /* Basic concepts [gram.basic]  */
1781
1782 static bool cp_parser_translation_unit
1783   (cp_parser *);
1784
1785 /* Expressions [gram.expr]  */
1786
1787 static tree cp_parser_primary_expression
1788   (cp_parser *, bool, bool, bool, cp_id_kind *);
1789 static tree cp_parser_id_expression
1790   (cp_parser *, bool, bool, bool *, bool, bool);
1791 static tree cp_parser_unqualified_id
1792   (cp_parser *, bool, bool, bool, bool);
1793 static tree cp_parser_nested_name_specifier_opt
1794   (cp_parser *, bool, bool, bool, bool);
1795 static tree cp_parser_nested_name_specifier
1796   (cp_parser *, bool, bool, bool, bool);
1797 static tree cp_parser_qualifying_entity
1798   (cp_parser *, bool, bool, bool, bool, bool);
1799 static tree cp_parser_postfix_expression
1800   (cp_parser *, bool, bool, bool, cp_id_kind *);
1801 static tree cp_parser_postfix_open_square_expression
1802   (cp_parser *, tree, bool);
1803 static tree cp_parser_postfix_dot_deref_expression
1804   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1805 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1806   (cp_parser *, int, bool, bool, bool *);
1807 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1808 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1809 static void cp_parser_pseudo_destructor_name
1810   (cp_parser *, tree *, tree *);
1811 static tree cp_parser_unary_expression
1812   (cp_parser *, bool, bool, cp_id_kind *);
1813 static enum tree_code cp_parser_unary_operator
1814   (cp_token *);
1815 static tree cp_parser_new_expression
1816   (cp_parser *);
1817 static VEC(tree,gc) *cp_parser_new_placement
1818   (cp_parser *);
1819 static tree cp_parser_new_type_id
1820   (cp_parser *, tree *);
1821 static cp_declarator *cp_parser_new_declarator_opt
1822   (cp_parser *);
1823 static cp_declarator *cp_parser_direct_new_declarator
1824   (cp_parser *);
1825 static VEC(tree,gc) *cp_parser_new_initializer
1826   (cp_parser *);
1827 static tree cp_parser_delete_expression
1828   (cp_parser *);
1829 static tree cp_parser_cast_expression
1830   (cp_parser *, bool, bool, cp_id_kind *);
1831 static tree cp_parser_binary_expression
1832   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1833 static tree cp_parser_question_colon_clause
1834   (cp_parser *, tree);
1835 static tree cp_parser_assignment_expression
1836   (cp_parser *, bool, cp_id_kind *);
1837 static enum tree_code cp_parser_assignment_operator_opt
1838   (cp_parser *);
1839 static tree cp_parser_expression
1840   (cp_parser *, bool, cp_id_kind *);
1841 static tree cp_parser_constant_expression
1842   (cp_parser *, bool, bool *);
1843 static tree cp_parser_builtin_offsetof
1844   (cp_parser *);
1845 static tree cp_parser_lambda_expression
1846   (cp_parser *);
1847 static void cp_parser_lambda_introducer
1848   (cp_parser *, tree);
1849 static bool cp_parser_lambda_declarator_opt
1850   (cp_parser *, tree);
1851 static void cp_parser_lambda_body
1852   (cp_parser *, tree);
1853
1854 /* Statements [gram.stmt.stmt]  */
1855
1856 static void cp_parser_statement
1857   (cp_parser *, tree, bool, bool *);
1858 static void cp_parser_label_for_labeled_statement
1859   (cp_parser *);
1860 static tree cp_parser_expression_statement
1861   (cp_parser *, tree);
1862 static tree cp_parser_compound_statement
1863   (cp_parser *, tree, bool, bool);
1864 static void cp_parser_statement_seq_opt
1865   (cp_parser *, tree);
1866 static tree cp_parser_selection_statement
1867   (cp_parser *, bool *);
1868 static tree cp_parser_condition
1869   (cp_parser *);
1870 static tree cp_parser_iteration_statement
1871   (cp_parser *);
1872 static bool cp_parser_for_init_statement
1873   (cp_parser *, tree *decl);
1874 static tree cp_parser_for
1875   (cp_parser *);
1876 static tree cp_parser_c_for
1877   (cp_parser *, tree, tree);
1878 static tree cp_parser_range_for
1879   (cp_parser *, tree, tree, tree);
1880 static void do_range_for_auto_deduction
1881   (tree, tree);
1882 static tree cp_parser_perform_range_for_lookup
1883   (tree, tree *, tree *);
1884 static tree cp_parser_range_for_member_function
1885   (tree, tree);
1886 static tree cp_parser_jump_statement
1887   (cp_parser *);
1888 static void cp_parser_declaration_statement
1889   (cp_parser *);
1890
1891 static tree cp_parser_implicitly_scoped_statement
1892   (cp_parser *, bool *);
1893 static void cp_parser_already_scoped_statement
1894   (cp_parser *);
1895
1896 /* Declarations [gram.dcl.dcl] */
1897
1898 static void cp_parser_declaration_seq_opt
1899   (cp_parser *);
1900 static void cp_parser_declaration
1901   (cp_parser *);
1902 static void cp_parser_block_declaration
1903   (cp_parser *, bool);
1904 static void cp_parser_simple_declaration
1905   (cp_parser *, bool, tree *);
1906 static void cp_parser_decl_specifier_seq
1907   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1908 static tree cp_parser_storage_class_specifier_opt
1909   (cp_parser *);
1910 static tree cp_parser_function_specifier_opt
1911   (cp_parser *, cp_decl_specifier_seq *);
1912 static tree cp_parser_type_specifier
1913   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1914    int *, bool *);
1915 static tree cp_parser_simple_type_specifier
1916   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1917 static tree cp_parser_type_name
1918   (cp_parser *);
1919 static tree cp_parser_nonclass_name 
1920   (cp_parser* parser);
1921 static tree cp_parser_elaborated_type_specifier
1922   (cp_parser *, bool, bool);
1923 static tree cp_parser_enum_specifier
1924   (cp_parser *);
1925 static void cp_parser_enumerator_list
1926   (cp_parser *, tree);
1927 static void cp_parser_enumerator_definition
1928   (cp_parser *, tree);
1929 static tree cp_parser_namespace_name
1930   (cp_parser *);
1931 static void cp_parser_namespace_definition
1932   (cp_parser *);
1933 static void cp_parser_namespace_body
1934   (cp_parser *);
1935 static tree cp_parser_qualified_namespace_specifier
1936   (cp_parser *);
1937 static void cp_parser_namespace_alias_definition
1938   (cp_parser *);
1939 static bool cp_parser_using_declaration
1940   (cp_parser *, bool);
1941 static void cp_parser_using_directive
1942   (cp_parser *);
1943 static tree cp_parser_alias_declaration
1944   (cp_parser *);
1945 static void cp_parser_asm_definition
1946   (cp_parser *);
1947 static void cp_parser_linkage_specification
1948   (cp_parser *);
1949 static void cp_parser_static_assert
1950   (cp_parser *, bool);
1951 static tree cp_parser_decltype
1952   (cp_parser *);
1953
1954 /* Declarators [gram.dcl.decl] */
1955
1956 static tree cp_parser_init_declarator
1957   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1958 static cp_declarator *cp_parser_declarator
1959   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1960 static cp_declarator *cp_parser_direct_declarator
1961   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1962 static enum tree_code cp_parser_ptr_operator
1963   (cp_parser *, tree *, cp_cv_quals *);
1964 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1965   (cp_parser *);
1966 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1967   (cp_parser *);
1968 static tree cp_parser_late_return_type_opt
1969   (cp_parser *, cp_cv_quals);
1970 static tree cp_parser_declarator_id
1971   (cp_parser *, bool);
1972 static tree cp_parser_type_id
1973   (cp_parser *);
1974 static tree cp_parser_template_type_arg
1975   (cp_parser *);
1976 static tree cp_parser_trailing_type_id (cp_parser *);
1977 static tree cp_parser_type_id_1
1978   (cp_parser *, bool, bool);
1979 static void cp_parser_type_specifier_seq
1980   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1981 static tree cp_parser_parameter_declaration_clause
1982   (cp_parser *);
1983 static tree cp_parser_parameter_declaration_list
1984   (cp_parser *, bool *);
1985 static cp_parameter_declarator *cp_parser_parameter_declaration
1986   (cp_parser *, bool, bool *);
1987 static tree cp_parser_default_argument 
1988   (cp_parser *, bool);
1989 static void cp_parser_function_body
1990   (cp_parser *);
1991 static tree cp_parser_initializer
1992   (cp_parser *, bool *, bool *);
1993 static tree cp_parser_initializer_clause
1994   (cp_parser *, bool *);
1995 static tree cp_parser_braced_list
1996   (cp_parser*, bool*);
1997 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1998   (cp_parser *, bool *);
1999
2000 static bool cp_parser_ctor_initializer_opt_and_function_body
2001   (cp_parser *);
2002
2003 /* Classes [gram.class] */
2004
2005 static tree cp_parser_class_name
2006   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2007 static tree cp_parser_class_specifier
2008   (cp_parser *);
2009 static tree cp_parser_class_head
2010   (cp_parser *, bool *, tree *, tree *);
2011 static enum tag_types cp_parser_class_key
2012   (cp_parser *);
2013 static void cp_parser_member_specification_opt
2014   (cp_parser *);
2015 static void cp_parser_member_declaration
2016   (cp_parser *);
2017 static tree cp_parser_pure_specifier
2018   (cp_parser *);
2019 static tree cp_parser_constant_initializer
2020   (cp_parser *);
2021
2022 /* Derived classes [gram.class.derived] */
2023
2024 static tree cp_parser_base_clause
2025   (cp_parser *);
2026 static tree cp_parser_base_specifier
2027   (cp_parser *);
2028
2029 /* Special member functions [gram.special] */
2030
2031 static tree cp_parser_conversion_function_id
2032   (cp_parser *);
2033 static tree cp_parser_conversion_type_id
2034   (cp_parser *);
2035 static cp_declarator *cp_parser_conversion_declarator_opt
2036   (cp_parser *);
2037 static bool cp_parser_ctor_initializer_opt
2038   (cp_parser *);
2039 static void cp_parser_mem_initializer_list
2040   (cp_parser *);
2041 static tree cp_parser_mem_initializer
2042   (cp_parser *);
2043 static tree cp_parser_mem_initializer_id
2044   (cp_parser *);
2045
2046 /* Overloading [gram.over] */
2047
2048 static tree cp_parser_operator_function_id
2049   (cp_parser *);
2050 static tree cp_parser_operator
2051   (cp_parser *);
2052
2053 /* Templates [gram.temp] */
2054
2055 static void cp_parser_template_declaration
2056   (cp_parser *, bool);
2057 static tree cp_parser_template_parameter_list
2058   (cp_parser *);
2059 static tree cp_parser_template_parameter
2060   (cp_parser *, bool *, bool *);
2061 static tree cp_parser_type_parameter
2062   (cp_parser *, bool *);
2063 static tree cp_parser_template_id
2064   (cp_parser *, bool, bool, bool);
2065 static tree cp_parser_template_name
2066   (cp_parser *, bool, bool, bool, bool *);
2067 static tree cp_parser_template_argument_list
2068   (cp_parser *);
2069 static tree cp_parser_template_argument
2070   (cp_parser *);
2071 static void cp_parser_explicit_instantiation
2072   (cp_parser *);
2073 static void cp_parser_explicit_specialization
2074   (cp_parser *);
2075
2076 /* Exception handling [gram.exception] */
2077
2078 static tree cp_parser_try_block
2079   (cp_parser *);
2080 static bool cp_parser_function_try_block
2081   (cp_parser *);
2082 static void cp_parser_handler_seq
2083   (cp_parser *);
2084 static void cp_parser_handler
2085   (cp_parser *);
2086 static tree cp_parser_exception_declaration
2087   (cp_parser *);
2088 static tree cp_parser_throw_expression
2089   (cp_parser *);
2090 static tree cp_parser_exception_specification_opt
2091   (cp_parser *);
2092 static tree cp_parser_type_id_list
2093   (cp_parser *);
2094
2095 /* GNU Extensions */
2096
2097 static tree cp_parser_asm_specification_opt
2098   (cp_parser *);
2099 static tree cp_parser_asm_operand_list
2100   (cp_parser *);
2101 static tree cp_parser_asm_clobber_list
2102   (cp_parser *);
2103 static tree cp_parser_asm_label_list
2104   (cp_parser *);
2105 static tree cp_parser_attributes_opt
2106   (cp_parser *);
2107 static tree cp_parser_attribute_list
2108   (cp_parser *);
2109 static bool cp_parser_extension_opt
2110   (cp_parser *, int *);
2111 static void cp_parser_label_declaration
2112   (cp_parser *);
2113
2114 /* Transactional Memory Extensions */
2115
2116 static tree cp_parser_transaction
2117   (cp_parser *, enum rid);
2118 static tree cp_parser_transaction_expression
2119   (cp_parser *, enum rid);
2120 static bool cp_parser_function_transaction
2121   (cp_parser *, enum rid);
2122 static tree cp_parser_transaction_cancel
2123   (cp_parser *);
2124
2125 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2126 static bool cp_parser_pragma
2127   (cp_parser *, enum pragma_context);
2128
2129 /* Objective-C++ Productions */
2130
2131 static tree cp_parser_objc_message_receiver
2132   (cp_parser *);
2133 static tree cp_parser_objc_message_args
2134   (cp_parser *);
2135 static tree cp_parser_objc_message_expression
2136   (cp_parser *);
2137 static tree cp_parser_objc_encode_expression
2138   (cp_parser *);
2139 static tree cp_parser_objc_defs_expression
2140   (cp_parser *);
2141 static tree cp_parser_objc_protocol_expression
2142   (cp_parser *);
2143 static tree cp_parser_objc_selector_expression
2144   (cp_parser *);
2145 static tree cp_parser_objc_expression
2146   (cp_parser *);
2147 static bool cp_parser_objc_selector_p
2148   (enum cpp_ttype);
2149 static tree cp_parser_objc_selector
2150   (cp_parser *);
2151 static tree cp_parser_objc_protocol_refs_opt
2152   (cp_parser *);
2153 static void cp_parser_objc_declaration
2154   (cp_parser *, tree);
2155 static tree cp_parser_objc_statement
2156   (cp_parser *);
2157 static bool cp_parser_objc_valid_prefix_attributes
2158   (cp_parser *, tree *);
2159 static void cp_parser_objc_at_property_declaration 
2160   (cp_parser *) ;
2161 static void cp_parser_objc_at_synthesize_declaration 
2162   (cp_parser *) ;
2163 static void cp_parser_objc_at_dynamic_declaration
2164   (cp_parser *) ;
2165 static tree cp_parser_objc_struct_declaration
2166   (cp_parser *) ;
2167
2168 /* Utility Routines */
2169
2170 static tree cp_parser_lookup_name
2171   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2172 static tree cp_parser_lookup_name_simple
2173   (cp_parser *, tree, location_t);
2174 static tree cp_parser_maybe_treat_template_as_class
2175   (tree, bool);
2176 static bool cp_parser_check_declarator_template_parameters
2177   (cp_parser *, cp_declarator *, location_t);
2178 static bool cp_parser_check_template_parameters
2179   (cp_parser *, unsigned, location_t, cp_declarator *);
2180 static tree cp_parser_simple_cast_expression
2181   (cp_parser *);
2182 static tree cp_parser_global_scope_opt
2183   (cp_parser *, bool);
2184 static bool cp_parser_constructor_declarator_p
2185   (cp_parser *, bool);
2186 static tree cp_parser_function_definition_from_specifiers_and_declarator
2187   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2188 static tree cp_parser_function_definition_after_declarator
2189   (cp_parser *, bool);
2190 static void cp_parser_template_declaration_after_export
2191   (cp_parser *, bool);
2192 static void cp_parser_perform_template_parameter_access_checks
2193   (VEC (deferred_access_check,gc)*);
2194 static tree cp_parser_single_declaration
2195   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2196 static tree cp_parser_functional_cast
2197   (cp_parser *, tree);
2198 static tree cp_parser_save_member_function_body
2199   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2200 static tree cp_parser_save_nsdmi
2201   (cp_parser *);
2202 static tree cp_parser_enclosed_template_argument_list
2203   (cp_parser *);
2204 static void cp_parser_save_default_args
2205   (cp_parser *, tree);
2206 static void cp_parser_late_parsing_for_member
2207   (cp_parser *, tree);
2208 static tree cp_parser_late_parse_one_default_arg
2209   (cp_parser *, tree, tree, tree);
2210 static void cp_parser_late_parsing_nsdmi
2211   (cp_parser *, tree);
2212 static void cp_parser_late_parsing_default_args
2213   (cp_parser *, tree);
2214 static tree cp_parser_sizeof_operand
2215   (cp_parser *, enum rid);
2216 static tree cp_parser_trait_expr
2217   (cp_parser *, enum rid);
2218 static bool cp_parser_declares_only_class_p
2219   (cp_parser *);
2220 static void cp_parser_set_storage_class
2221   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2222 static void cp_parser_set_decl_spec_type
2223   (cp_decl_specifier_seq *, tree, location_t, bool);
2224 static bool cp_parser_friend_p
2225   (const cp_decl_specifier_seq *);
2226 static void cp_parser_required_error
2227   (cp_parser *, required_token, bool);
2228 static cp_token *cp_parser_require
2229   (cp_parser *, enum cpp_ttype, required_token);
2230 static cp_token *cp_parser_require_keyword
2231   (cp_parser *, enum rid, required_token);
2232 static bool cp_parser_token_starts_function_definition_p
2233   (cp_token *);
2234 static bool cp_parser_next_token_starts_class_definition_p
2235   (cp_parser *);
2236 static bool cp_parser_next_token_ends_template_argument_p
2237   (cp_parser *);
2238 static bool cp_parser_nth_token_starts_template_argument_list_p
2239   (cp_parser *, size_t);
2240 static enum tag_types cp_parser_token_is_class_key
2241   (cp_token *);
2242 static void cp_parser_check_class_key
2243   (enum tag_types, tree type);
2244 static void cp_parser_check_access_in_redeclaration
2245   (tree type, location_t location);
2246 static bool cp_parser_optional_template_keyword
2247   (cp_parser *);
2248 static void cp_parser_pre_parsed_nested_name_specifier
2249   (cp_parser *);
2250 static bool cp_parser_cache_group
2251   (cp_parser *, enum cpp_ttype, unsigned);
2252 static void cp_parser_parse_tentatively
2253   (cp_parser *);
2254 static void cp_parser_commit_to_tentative_parse
2255   (cp_parser *);
2256 static void cp_parser_abort_tentative_parse
2257   (cp_parser *);
2258 static bool cp_parser_parse_definitely
2259   (cp_parser *);
2260 static inline bool cp_parser_parsing_tentatively
2261   (cp_parser *);
2262 static bool cp_parser_uncommitted_to_tentative_parse_p
2263   (cp_parser *);
2264 static void cp_parser_error
2265   (cp_parser *, const char *);
2266 static void cp_parser_name_lookup_error
2267   (cp_parser *, tree, tree, name_lookup_error, location_t);
2268 static bool cp_parser_simulate_error
2269   (cp_parser *);
2270 static bool cp_parser_check_type_definition
2271   (cp_parser *);
2272 static void cp_parser_check_for_definition_in_return_type
2273   (cp_declarator *, tree, location_t type_location);
2274 static void cp_parser_check_for_invalid_template_id
2275   (cp_parser *, tree, location_t location);
2276 static bool cp_parser_non_integral_constant_expression
2277   (cp_parser *, non_integral_constant);
2278 static void cp_parser_diagnose_invalid_type_name
2279   (cp_parser *, tree, tree, location_t);
2280 static bool cp_parser_parse_and_diagnose_invalid_type_name
2281   (cp_parser *);
2282 static int cp_parser_skip_to_closing_parenthesis
2283   (cp_parser *, bool, bool, bool);
2284 static void cp_parser_skip_to_end_of_statement
2285   (cp_parser *);
2286 static void cp_parser_consume_semicolon_at_end_of_statement
2287   (cp_parser *);
2288 static void cp_parser_skip_to_end_of_block_or_statement
2289   (cp_parser *);
2290 static bool cp_parser_skip_to_closing_brace
2291   (cp_parser *);
2292 static void cp_parser_skip_to_end_of_template_parameter_list
2293   (cp_parser *);
2294 static void cp_parser_skip_to_pragma_eol
2295   (cp_parser*, cp_token *);
2296 static bool cp_parser_error_occurred
2297   (cp_parser *);
2298 static bool cp_parser_allow_gnu_extensions_p
2299   (cp_parser *);
2300 static bool cp_parser_is_pure_string_literal
2301   (cp_token *);
2302 static bool cp_parser_is_string_literal
2303   (cp_token *);
2304 static bool cp_parser_is_keyword
2305   (cp_token *, enum rid);
2306 static tree cp_parser_make_typename_type
2307   (cp_parser *, tree, tree, location_t location);
2308 static cp_declarator * cp_parser_make_indirect_declarator
2309   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2310
2311 /* Returns nonzero if we are parsing tentatively.  */
2312
2313 static inline bool
2314 cp_parser_parsing_tentatively (cp_parser* parser)
2315 {
2316   return parser->context->next != NULL;
2317 }
2318
2319 /* Returns nonzero if TOKEN is a string literal.  */
2320
2321 static bool
2322 cp_parser_is_pure_string_literal (cp_token* token)
2323 {
2324   return (token->type == CPP_STRING ||
2325           token->type == CPP_STRING16 ||
2326           token->type == CPP_STRING32 ||
2327           token->type == CPP_WSTRING ||
2328           token->type == CPP_UTF8STRING);
2329 }
2330
2331 /* Returns nonzero if TOKEN is a string literal
2332    of a user-defined string literal.  */
2333
2334 static bool
2335 cp_parser_is_string_literal (cp_token* token)
2336 {
2337   return (cp_parser_is_pure_string_literal (token) ||
2338           token->type == CPP_STRING_USERDEF ||
2339           token->type == CPP_STRING16_USERDEF ||
2340           token->type == CPP_STRING32_USERDEF ||
2341           token->type == CPP_WSTRING_USERDEF ||
2342           token->type == CPP_UTF8STRING_USERDEF);
2343 }
2344
2345 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2346
2347 static bool
2348 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2349 {
2350   return token->keyword == keyword;
2351 }
2352
2353 /* If not parsing tentatively, issue a diagnostic of the form
2354       FILE:LINE: MESSAGE before TOKEN
2355    where TOKEN is the next token in the input stream.  MESSAGE
2356    (specified by the caller) is usually of the form "expected
2357    OTHER-TOKEN".  */
2358
2359 static void
2360 cp_parser_error (cp_parser* parser, const char* gmsgid)
2361 {
2362   if (!cp_parser_simulate_error (parser))
2363     {
2364       cp_token *token = cp_lexer_peek_token (parser->lexer);
2365       /* This diagnostic makes more sense if it is tagged to the line
2366          of the token we just peeked at.  */
2367       cp_lexer_set_source_position_from_token (token);
2368
2369       if (token->type == CPP_PRAGMA)
2370         {
2371           error_at (token->location,
2372                     "%<#pragma%> is not allowed here");
2373           cp_parser_skip_to_pragma_eol (parser, token);
2374           return;
2375         }
2376
2377       c_parse_error (gmsgid,
2378                      /* Because c_parser_error does not understand
2379                         CPP_KEYWORD, keywords are treated like
2380                         identifiers.  */
2381                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2382                      token->u.value, token->flags);
2383     }
2384 }
2385
2386 /* Issue an error about name-lookup failing.  NAME is the
2387    IDENTIFIER_NODE DECL is the result of
2388    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2389    the thing that we hoped to find.  */
2390
2391 static void
2392 cp_parser_name_lookup_error (cp_parser* parser,
2393                              tree name,
2394                              tree decl,
2395                              name_lookup_error desired,
2396                              location_t location)
2397 {
2398   /* If name lookup completely failed, tell the user that NAME was not
2399      declared.  */
2400   if (decl == error_mark_node)
2401     {
2402       if (parser->scope && parser->scope != global_namespace)
2403         error_at (location, "%<%E::%E%> has not been declared",
2404                   parser->scope, name);
2405       else if (parser->scope == global_namespace)
2406         error_at (location, "%<::%E%> has not been declared", name);
2407       else if (parser->object_scope
2408                && !CLASS_TYPE_P (parser->object_scope))
2409         error_at (location, "request for member %qE in non-class type %qT",
2410                   name, parser->object_scope);
2411       else if (parser->object_scope)
2412         error_at (location, "%<%T::%E%> has not been declared",
2413                   parser->object_scope, name);
2414       else
2415         error_at (location, "%qE has not been declared", name);
2416     }
2417   else if (parser->scope && parser->scope != global_namespace)
2418     {
2419       switch (desired)
2420         {
2421           case NLE_TYPE:
2422             error_at (location, "%<%E::%E%> is not a type",
2423                                 parser->scope, name);
2424             break;
2425           case NLE_CXX98:
2426             error_at (location, "%<%E::%E%> is not a class or namespace",
2427                                 parser->scope, name);
2428             break;
2429           case NLE_NOT_CXX98:
2430             error_at (location,
2431                       "%<%E::%E%> is not a class, namespace, or enumeration",
2432                       parser->scope, name);
2433             break;
2434           default:
2435             gcc_unreachable ();
2436             
2437         }
2438     }
2439   else if (parser->scope == global_namespace)
2440     {
2441       switch (desired)
2442         {
2443           case NLE_TYPE:
2444             error_at (location, "%<::%E%> is not a type", name);
2445             break;
2446           case NLE_CXX98:
2447             error_at (location, "%<::%E%> is not a class or namespace", name);
2448             break;
2449           case NLE_NOT_CXX98:
2450             error_at (location,
2451                       "%<::%E%> is not a class, namespace, or enumeration",
2452                       name);
2453             break;
2454           default:
2455             gcc_unreachable ();
2456         }
2457     }
2458   else
2459     {
2460       switch (desired)
2461         {
2462           case NLE_TYPE:
2463             error_at (location, "%qE is not a type", name);
2464             break;
2465           case NLE_CXX98:
2466             error_at (location, "%qE is not a class or namespace", name);
2467             break;
2468           case NLE_NOT_CXX98:
2469             error_at (location,
2470                       "%qE is not a class, namespace, or enumeration", name);
2471             break;
2472           default:
2473             gcc_unreachable ();
2474         }
2475     }
2476 }
2477
2478 /* If we are parsing tentatively, remember that an error has occurred
2479    during this tentative parse.  Returns true if the error was
2480    simulated; false if a message should be issued by the caller.  */
2481
2482 static bool
2483 cp_parser_simulate_error (cp_parser* parser)
2484 {
2485   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2486     {
2487       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2488       return true;
2489     }
2490   return false;
2491 }
2492
2493 /* Check for repeated decl-specifiers.  */
2494
2495 static void
2496 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2497                            location_t location)
2498 {
2499   int ds;
2500
2501   for (ds = ds_first; ds != ds_last; ++ds)
2502     {
2503       unsigned count = decl_specs->specs[ds];
2504       if (count < 2)
2505         continue;
2506       /* The "long" specifier is a special case because of "long long".  */
2507       if (ds == ds_long)
2508         {
2509           if (count > 2)
2510             error_at (location, "%<long long long%> is too long for GCC");
2511           else 
2512             pedwarn_cxx98 (location, OPT_Wlong_long, 
2513                            "ISO C++ 1998 does not support %<long long%>");
2514         }
2515       else if (count > 1)
2516         {
2517           static const char *const decl_spec_names[] = {
2518             "signed",
2519             "unsigned",
2520             "short",
2521             "long",
2522             "const",
2523             "volatile",
2524             "restrict",
2525             "inline",
2526             "virtual",
2527             "explicit",
2528             "friend",
2529             "typedef",
2530             "using",
2531             "constexpr",
2532             "__complex",
2533             "__thread"
2534           };
2535           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2536         }
2537     }
2538 }
2539
2540 /* This function is called when a type is defined.  If type
2541    definitions are forbidden at this point, an error message is
2542    issued.  */
2543
2544 static bool
2545 cp_parser_check_type_definition (cp_parser* parser)
2546 {
2547   /* If types are forbidden here, issue a message.  */
2548   if (parser->type_definition_forbidden_message)
2549     {
2550       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2551          in the message need to be interpreted.  */
2552       error (parser->type_definition_forbidden_message);
2553       return false;
2554     }
2555   return true;
2556 }
2557
2558 /* This function is called when the DECLARATOR is processed.  The TYPE
2559    was a type defined in the decl-specifiers.  If it is invalid to
2560    define a type in the decl-specifiers for DECLARATOR, an error is
2561    issued. TYPE_LOCATION is the location of TYPE and is used
2562    for error reporting.  */
2563
2564 static void
2565 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2566                                                tree type, location_t type_location)
2567 {
2568   /* [dcl.fct] forbids type definitions in return types.
2569      Unfortunately, it's not easy to know whether or not we are
2570      processing a return type until after the fact.  */
2571   while (declarator
2572          && (declarator->kind == cdk_pointer
2573              || declarator->kind == cdk_reference
2574              || declarator->kind == cdk_ptrmem))
2575     declarator = declarator->declarator;
2576   if (declarator
2577       && declarator->kind == cdk_function)
2578     {
2579       error_at (type_location,
2580                 "new types may not be defined in a return type");
2581       inform (type_location, 
2582               "(perhaps a semicolon is missing after the definition of %qT)",
2583               type);
2584     }
2585 }
2586
2587 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2588    "<" in any valid C++ program.  If the next token is indeed "<",
2589    issue a message warning the user about what appears to be an
2590    invalid attempt to form a template-id. LOCATION is the location
2591    of the type-specifier (TYPE) */
2592
2593 static void
2594 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2595                                          tree type, location_t location)
2596 {
2597   cp_token_position start = 0;
2598
2599   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2600     {
2601       if (TYPE_P (type))
2602         error_at (location, "%qT is not a template", type);
2603       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2604         error_at (location, "%qE is not a template", type);
2605       else
2606         error_at (location, "invalid template-id");
2607       /* Remember the location of the invalid "<".  */
2608       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2609         start = cp_lexer_token_position (parser->lexer, true);
2610       /* Consume the "<".  */
2611       cp_lexer_consume_token (parser->lexer);
2612       /* Parse the template arguments.  */
2613       cp_parser_enclosed_template_argument_list (parser);
2614       /* Permanently remove the invalid template arguments so that
2615          this error message is not issued again.  */
2616       if (start)
2617         cp_lexer_purge_tokens_after (parser->lexer, start);
2618     }
2619 }
2620
2621 /* If parsing an integral constant-expression, issue an error message
2622    about the fact that THING appeared and return true.  Otherwise,
2623    return false.  In either case, set
2624    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2625
2626 static bool
2627 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2628                                             non_integral_constant thing)
2629 {
2630   parser->non_integral_constant_expression_p = true;
2631   if (parser->integral_constant_expression_p)
2632     {
2633       if (!parser->allow_non_integral_constant_expression_p)
2634         {
2635           const char *msg = NULL;
2636           switch (thing)
2637             {
2638               case NIC_FLOAT:
2639                 error ("floating-point literal "
2640                        "cannot appear in a constant-expression");
2641                 return true;
2642               case NIC_CAST:
2643                 error ("a cast to a type other than an integral or "
2644                        "enumeration type cannot appear in a "
2645                        "constant-expression");
2646                 return true;
2647               case NIC_TYPEID:
2648                 error ("%<typeid%> operator "
2649                        "cannot appear in a constant-expression");
2650                 return true;
2651               case NIC_NCC:
2652                 error ("non-constant compound literals "
2653                        "cannot appear in a constant-expression");
2654                 return true;
2655               case NIC_FUNC_CALL:
2656                 error ("a function call "
2657                        "cannot appear in a constant-expression");
2658                 return true;
2659               case NIC_INC:
2660                 error ("an increment "
2661                        "cannot appear in a constant-expression");
2662                 return true;
2663               case NIC_DEC:
2664                 error ("an decrement "
2665                        "cannot appear in a constant-expression");
2666                 return true;
2667               case NIC_ARRAY_REF:
2668                 error ("an array reference "
2669                        "cannot appear in a constant-expression");
2670                 return true;
2671               case NIC_ADDR_LABEL:
2672                 error ("the address of a label "
2673                        "cannot appear in a constant-expression");
2674                 return true;
2675               case NIC_OVERLOADED:
2676                 error ("calls to overloaded operators "
2677                        "cannot appear in a constant-expression");
2678                 return true;
2679               case NIC_ASSIGNMENT:
2680                 error ("an assignment cannot appear in a constant-expression");
2681                 return true;
2682               case NIC_COMMA:
2683                 error ("a comma operator "
2684                        "cannot appear in a constant-expression");
2685                 return true;
2686               case NIC_CONSTRUCTOR:
2687                 error ("a call to a constructor "
2688                        "cannot appear in a constant-expression");
2689                 return true;
2690               case NIC_TRANSACTION:
2691                 error ("a transaction expression "
2692                        "cannot appear in a constant-expression");
2693                 return true;
2694               case NIC_THIS:
2695                 msg = "this";
2696                 break;
2697               case NIC_FUNC_NAME:
2698                 msg = "__FUNCTION__";
2699                 break;
2700               case NIC_PRETTY_FUNC:
2701                 msg = "__PRETTY_FUNCTION__";
2702                 break;
2703               case NIC_C99_FUNC:
2704                 msg = "__func__";
2705                 break;
2706               case NIC_VA_ARG:
2707                 msg = "va_arg";
2708                 break;
2709               case NIC_ARROW:
2710                 msg = "->";
2711                 break;
2712               case NIC_POINT:
2713                 msg = ".";
2714                 break;
2715               case NIC_STAR:
2716                 msg = "*";
2717                 break;
2718               case NIC_ADDR:
2719                 msg = "&";
2720                 break;
2721               case NIC_PREINCREMENT:
2722                 msg = "++";
2723                 break;
2724               case NIC_PREDECREMENT:
2725                 msg = "--";
2726                 break;
2727               case NIC_NEW:
2728                 msg = "new";
2729                 break;
2730               case NIC_DEL:
2731                 msg = "delete";
2732                 break;
2733               default:
2734                 gcc_unreachable ();
2735             }
2736           if (msg)
2737             error ("%qs cannot appear in a constant-expression", msg);
2738           return true;
2739         }
2740     }
2741   return false;
2742 }
2743
2744 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2745    qualifying scope (or NULL, if none) for ID.  This function commits
2746    to the current active tentative parse, if any.  (Otherwise, the
2747    problematic construct might be encountered again later, resulting
2748    in duplicate error messages.) LOCATION is the location of ID.  */
2749
2750 static void
2751 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2752                                       tree scope, tree id,
2753                                       location_t location)
2754 {
2755   tree decl, old_scope;
2756   cp_parser_commit_to_tentative_parse (parser);
2757   /* Try to lookup the identifier.  */
2758   old_scope = parser->scope;
2759   parser->scope = scope;
2760   decl = cp_parser_lookup_name_simple (parser, id, location);
2761   parser->scope = old_scope;
2762   /* If the lookup found a template-name, it means that the user forgot
2763   to specify an argument list. Emit a useful error message.  */
2764   if (TREE_CODE (decl) == TEMPLATE_DECL)
2765     error_at (location,
2766               "invalid use of template-name %qE without an argument list",
2767               decl);
2768   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2769     error_at (location, "invalid use of destructor %qD as a type", id);
2770   else if (TREE_CODE (decl) == TYPE_DECL)
2771     /* Something like 'unsigned A a;'  */
2772     error_at (location, "invalid combination of multiple type-specifiers");
2773   else if (!parser->scope)
2774     {
2775       /* Issue an error message.  */
2776       error_at (location, "%qE does not name a type", id);
2777       /* If we're in a template class, it's possible that the user was
2778          referring to a type from a base class.  For example:
2779
2780            template <typename T> struct A { typedef T X; };
2781            template <typename T> struct B : public A<T> { X x; };
2782
2783          The user should have said "typename A<T>::X".  */
2784       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2785         inform (location, "C++11 %<constexpr%> only available with "
2786                 "-std=c++11 or -std=gnu++11");
2787       else if (processing_template_decl && current_class_type
2788                && TYPE_BINFO (current_class_type))
2789         {
2790           tree b;
2791
2792           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2793                b;
2794                b = TREE_CHAIN (b))
2795             {
2796               tree base_type = BINFO_TYPE (b);
2797               if (CLASS_TYPE_P (base_type)
2798                   && dependent_type_p (base_type))
2799                 {
2800                   tree field;
2801                   /* Go from a particular instantiation of the
2802                      template (which will have an empty TYPE_FIELDs),
2803                      to the main version.  */
2804                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2805                   for (field = TYPE_FIELDS (base_type);
2806                        field;
2807                        field = DECL_CHAIN (field))
2808                     if (TREE_CODE (field) == TYPE_DECL
2809                         && DECL_NAME (field) == id)
2810                       {
2811                         inform (location, 
2812                                 "(perhaps %<typename %T::%E%> was intended)",
2813                                 BINFO_TYPE (b), id);
2814                         break;
2815                       }
2816                   if (field)
2817                     break;
2818                 }
2819             }
2820         }
2821     }
2822   /* Here we diagnose qualified-ids where the scope is actually correct,
2823      but the identifier does not resolve to a valid type name.  */
2824   else if (parser->scope != error_mark_node)
2825     {
2826       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2827         error_at (location, "%qE in namespace %qE does not name a type",
2828                   id, parser->scope);
2829       else if (CLASS_TYPE_P (parser->scope)
2830                && constructor_name_p (id, parser->scope))
2831         {
2832           /* A<T>::A<T>() */
2833           error_at (location, "%<%T::%E%> names the constructor, not"
2834                     " the type", parser->scope, id);
2835           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2836             error_at (location, "and %qT has no template constructors",
2837                       parser->scope);
2838         }
2839       else if (TYPE_P (parser->scope)
2840                && dependent_scope_p (parser->scope))
2841         error_at (location, "need %<typename%> before %<%T::%E%> because "
2842                   "%qT is a dependent scope",
2843                   parser->scope, id, parser->scope);
2844       else if (TYPE_P (parser->scope))
2845         error_at (location, "%qE in %q#T does not name a type",
2846                   id, parser->scope);
2847       else
2848         gcc_unreachable ();
2849     }
2850 }
2851
2852 /* Check for a common situation where a type-name should be present,
2853    but is not, and issue a sensible error message.  Returns true if an
2854    invalid type-name was detected.
2855
2856    The situation handled by this function are variable declarations of the
2857    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2858    Usually, `ID' should name a type, but if we got here it means that it
2859    does not. We try to emit the best possible error message depending on
2860    how exactly the id-expression looks like.  */
2861
2862 static bool
2863 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2864 {
2865   tree id;
2866   cp_token *token = cp_lexer_peek_token (parser->lexer);
2867
2868   /* Avoid duplicate error about ambiguous lookup.  */
2869   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2870     {
2871       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2872       if (next->type == CPP_NAME && next->ambiguous_p)
2873         goto out;
2874     }
2875
2876   cp_parser_parse_tentatively (parser);
2877   id = cp_parser_id_expression (parser,
2878                                 /*template_keyword_p=*/false,
2879                                 /*check_dependency_p=*/true,
2880                                 /*template_p=*/NULL,
2881                                 /*declarator_p=*/true,
2882                                 /*optional_p=*/false);
2883   /* If the next token is a (, this is a function with no explicit return
2884      type, i.e. constructor, destructor or conversion op.  */
2885   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2886       || TREE_CODE (id) == TYPE_DECL)
2887     {
2888       cp_parser_abort_tentative_parse (parser);
2889       return false;
2890     }
2891   if (!cp_parser_parse_definitely (parser))
2892     return false;
2893
2894   /* Emit a diagnostic for the invalid type.  */
2895   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2896                                         id, token->location);
2897  out:
2898   /* If we aren't in the middle of a declarator (i.e. in a
2899      parameter-declaration-clause), skip to the end of the declaration;
2900      there's no point in trying to process it.  */
2901   if (!parser->in_declarator_p)
2902     cp_parser_skip_to_end_of_block_or_statement (parser);
2903   return true;
2904 }
2905
2906 /* Consume tokens up to, and including, the next non-nested closing `)'.
2907    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2908    are doing error recovery. Returns -1 if OR_COMMA is true and we
2909    found an unnested comma.  */
2910
2911 static int
2912 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2913                                        bool recovering,
2914                                        bool or_comma,
2915                                        bool consume_paren)
2916 {
2917   unsigned paren_depth = 0;
2918   unsigned brace_depth = 0;
2919   unsigned square_depth = 0;
2920
2921   if (recovering && !or_comma
2922       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2923     return 0;
2924
2925   while (true)
2926     {
2927       cp_token * token = cp_lexer_peek_token (parser->lexer);
2928
2929       switch (token->type)
2930         {
2931         case CPP_EOF:
2932         case CPP_PRAGMA_EOL:
2933           /* If we've run out of tokens, then there is no closing `)'.  */
2934           return 0;
2935
2936         /* This is good for lambda expression capture-lists.  */
2937         case CPP_OPEN_SQUARE:
2938           ++square_depth;
2939           break;
2940         case CPP_CLOSE_SQUARE:
2941           if (!square_depth--)
2942             return 0;
2943           break;
2944
2945         case CPP_SEMICOLON:
2946           /* This matches the processing in skip_to_end_of_statement.  */
2947           if (!brace_depth)
2948             return 0;
2949           break;
2950
2951         case CPP_OPEN_BRACE:
2952           ++brace_depth;
2953           break;
2954         case CPP_CLOSE_BRACE:
2955           if (!brace_depth--)
2956             return 0;
2957           break;
2958
2959         case CPP_COMMA:
2960           if (recovering && or_comma && !brace_depth && !paren_depth
2961               && !square_depth)
2962             return -1;
2963           break;
2964
2965         case CPP_OPEN_PAREN:
2966           if (!brace_depth)
2967             ++paren_depth;
2968           break;
2969
2970         case CPP_CLOSE_PAREN:
2971           if (!brace_depth && !paren_depth--)
2972             {
2973               if (consume_paren)
2974                 cp_lexer_consume_token (parser->lexer);
2975               return 1;
2976             }
2977           break;
2978
2979         default:
2980           break;
2981         }
2982
2983       /* Consume the token.  */
2984       cp_lexer_consume_token (parser->lexer);
2985     }
2986 }
2987
2988 /* Consume tokens until we reach the end of the current statement.
2989    Normally, that will be just before consuming a `;'.  However, if a
2990    non-nested `}' comes first, then we stop before consuming that.  */
2991
2992 static void
2993 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2994 {
2995   unsigned nesting_depth = 0;
2996
2997   while (true)
2998     {
2999       cp_token *token = cp_lexer_peek_token (parser->lexer);
3000
3001       switch (token->type)
3002         {
3003         case CPP_EOF:
3004         case CPP_PRAGMA_EOL:
3005           /* If we've run out of tokens, stop.  */
3006           return;
3007
3008         case CPP_SEMICOLON:
3009           /* If the next token is a `;', we have reached the end of the
3010              statement.  */
3011           if (!nesting_depth)
3012             return;
3013           break;
3014
3015         case CPP_CLOSE_BRACE:
3016           /* If this is a non-nested '}', stop before consuming it.
3017              That way, when confronted with something like:
3018
3019                { 3 + }
3020
3021              we stop before consuming the closing '}', even though we
3022              have not yet reached a `;'.  */
3023           if (nesting_depth == 0)
3024             return;
3025
3026           /* If it is the closing '}' for a block that we have
3027              scanned, stop -- but only after consuming the token.
3028              That way given:
3029
3030                 void f g () { ... }
3031                 typedef int I;
3032
3033              we will stop after the body of the erroneously declared
3034              function, but before consuming the following `typedef'
3035              declaration.  */
3036           if (--nesting_depth == 0)
3037             {
3038               cp_lexer_consume_token (parser->lexer);
3039               return;
3040             }
3041
3042         case CPP_OPEN_BRACE:
3043           ++nesting_depth;
3044           break;
3045
3046         default:
3047           break;
3048         }
3049
3050       /* Consume the token.  */
3051       cp_lexer_consume_token (parser->lexer);
3052     }
3053 }
3054
3055 /* This function is called at the end of a statement or declaration.
3056    If the next token is a semicolon, it is consumed; otherwise, error
3057    recovery is attempted.  */
3058
3059 static void
3060 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3061 {
3062   /* Look for the trailing `;'.  */
3063   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3064     {
3065       /* If there is additional (erroneous) input, skip to the end of
3066          the statement.  */
3067       cp_parser_skip_to_end_of_statement (parser);
3068       /* If the next token is now a `;', consume it.  */
3069       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3070         cp_lexer_consume_token (parser->lexer);
3071     }
3072 }
3073
3074 /* Skip tokens until we have consumed an entire block, or until we
3075    have consumed a non-nested `;'.  */
3076
3077 static void
3078 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3079 {
3080   int nesting_depth = 0;
3081
3082   while (nesting_depth >= 0)
3083     {
3084       cp_token *token = cp_lexer_peek_token (parser->lexer);
3085
3086       switch (token->type)
3087         {
3088         case CPP_EOF:
3089         case CPP_PRAGMA_EOL:
3090           /* If we've run out of tokens, stop.  */
3091           return;
3092
3093         case CPP_SEMICOLON:
3094           /* Stop if this is an unnested ';'. */
3095           if (!nesting_depth)
3096             nesting_depth = -1;
3097           break;
3098
3099         case CPP_CLOSE_BRACE:
3100           /* Stop if this is an unnested '}', or closes the outermost
3101              nesting level.  */
3102           nesting_depth--;
3103           if (nesting_depth < 0)
3104             return;
3105           if (!nesting_depth)
3106             nesting_depth = -1;
3107           break;
3108
3109         case CPP_OPEN_BRACE:
3110           /* Nest. */
3111           nesting_depth++;
3112           break;
3113
3114         default:
3115           break;
3116         }
3117
3118       /* Consume the token.  */
3119       cp_lexer_consume_token (parser->lexer);
3120     }
3121 }
3122
3123 /* Skip tokens until a non-nested closing curly brace is the next
3124    token, or there are no more tokens. Return true in the first case,
3125    false otherwise.  */
3126
3127 static bool
3128 cp_parser_skip_to_closing_brace (cp_parser *parser)
3129 {
3130   unsigned nesting_depth = 0;
3131
3132   while (true)
3133     {
3134       cp_token *token = cp_lexer_peek_token (parser->lexer);
3135
3136       switch (token->type)
3137         {
3138         case CPP_EOF:
3139         case CPP_PRAGMA_EOL:
3140           /* If we've run out of tokens, stop.  */
3141           return false;
3142
3143         case CPP_CLOSE_BRACE:
3144           /* If the next token is a non-nested `}', then we have reached
3145              the end of the current block.  */
3146           if (nesting_depth-- == 0)
3147             return true;
3148           break;
3149
3150         case CPP_OPEN_BRACE:
3151           /* If it the next token is a `{', then we are entering a new
3152              block.  Consume the entire block.  */
3153           ++nesting_depth;
3154           break;
3155
3156         default:
3157           break;
3158         }
3159
3160       /* Consume the token.  */
3161       cp_lexer_consume_token (parser->lexer);
3162     }
3163 }
3164
3165 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3166    parameter is the PRAGMA token, allowing us to purge the entire pragma
3167    sequence.  */
3168
3169 static void
3170 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3171 {
3172   cp_token *token;
3173
3174   parser->lexer->in_pragma = false;
3175
3176   do
3177     token = cp_lexer_consume_token (parser->lexer);
3178   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3179
3180   /* Ensure that the pragma is not parsed again.  */
3181   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3182 }
3183
3184 /* Require pragma end of line, resyncing with it as necessary.  The
3185    arguments are as for cp_parser_skip_to_pragma_eol.  */
3186
3187 static void
3188 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3189 {
3190   parser->lexer->in_pragma = false;
3191   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3192     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3193 }
3194
3195 /* This is a simple wrapper around make_typename_type. When the id is
3196    an unresolved identifier node, we can provide a superior diagnostic
3197    using cp_parser_diagnose_invalid_type_name.  */
3198
3199 static tree
3200 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3201                               tree id, location_t id_location)
3202 {
3203   tree result;
3204   if (TREE_CODE (id) == IDENTIFIER_NODE)
3205     {
3206       result = make_typename_type (scope, id, typename_type,
3207                                    /*complain=*/tf_none);
3208       if (result == error_mark_node)
3209         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3210       return result;
3211     }
3212   return make_typename_type (scope, id, typename_type, tf_error);
3213 }
3214
3215 /* This is a wrapper around the
3216    make_{pointer,ptrmem,reference}_declarator functions that decides
3217    which one to call based on the CODE and CLASS_TYPE arguments. The
3218    CODE argument should be one of the values returned by
3219    cp_parser_ptr_operator. */
3220 static cp_declarator *
3221 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3222                                     cp_cv_quals cv_qualifiers,
3223                                     cp_declarator *target)
3224 {
3225   if (code == ERROR_MARK)
3226     return cp_error_declarator;
3227
3228   if (code == INDIRECT_REF)
3229     if (class_type == NULL_TREE)
3230       return make_pointer_declarator (cv_qualifiers, target);
3231     else
3232       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3233   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3234     return make_reference_declarator (cv_qualifiers, target, false);
3235   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3236     return make_reference_declarator (cv_qualifiers, target, true);
3237   gcc_unreachable ();
3238 }
3239
3240 /* Create a new C++ parser.  */
3241
3242 static cp_parser *
3243 cp_parser_new (void)
3244 {
3245   cp_parser *parser;
3246   cp_lexer *lexer;
3247   unsigned i;
3248
3249   /* cp_lexer_new_main is called before doing GC allocation because
3250      cp_lexer_new_main might load a PCH file.  */
3251   lexer = cp_lexer_new_main ();
3252
3253   /* Initialize the binops_by_token so that we can get the tree
3254      directly from the token.  */
3255   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3256     binops_by_token[binops[i].token_type] = binops[i];
3257
3258   parser = ggc_alloc_cleared_cp_parser ();
3259   parser->lexer = lexer;
3260   parser->context = cp_parser_context_new (NULL);
3261
3262   /* For now, we always accept GNU extensions.  */
3263   parser->allow_gnu_extensions_p = 1;
3264
3265   /* The `>' token is a greater-than operator, not the end of a
3266      template-id.  */
3267   parser->greater_than_is_operator_p = true;
3268
3269   parser->default_arg_ok_p = true;
3270
3271   /* We are not parsing a constant-expression.  */
3272   parser->integral_constant_expression_p = false;
3273   parser->allow_non_integral_constant_expression_p = false;
3274   parser->non_integral_constant_expression_p = false;
3275
3276   /* Local variable names are not forbidden.  */
3277   parser->local_variables_forbidden_p = false;
3278
3279   /* We are not processing an `extern "C"' declaration.  */
3280   parser->in_unbraced_linkage_specification_p = false;
3281
3282   /* We are not processing a declarator.  */
3283   parser->in_declarator_p = false;
3284
3285   /* We are not processing a template-argument-list.  */
3286   parser->in_template_argument_list_p = false;
3287
3288   /* We are not in an iteration statement.  */
3289   parser->in_statement = 0;
3290
3291   /* We are not in a switch statement.  */
3292   parser->in_switch_statement_p = false;
3293
3294   /* We are not parsing a type-id inside an expression.  */
3295   parser->in_type_id_in_expr_p = false;
3296
3297   /* Declarations aren't implicitly extern "C".  */
3298   parser->implicit_extern_c = false;
3299
3300   /* String literals should be translated to the execution character set.  */
3301   parser->translate_strings_p = true;
3302
3303   /* We are not parsing a function body.  */
3304   parser->in_function_body = false;
3305
3306   /* We can correct until told otherwise.  */
3307   parser->colon_corrects_to_scope_p = true;
3308
3309   /* The unparsed function queue is empty.  */
3310   push_unparsed_function_queues (parser);
3311
3312   /* There are no classes being defined.  */
3313   parser->num_classes_being_defined = 0;
3314
3315   /* No template parameters apply.  */
3316   parser->num_template_parameter_lists = 0;
3317
3318   return parser;
3319 }
3320
3321 /* Create a cp_lexer structure which will emit the tokens in CACHE
3322    and push it onto the parser's lexer stack.  This is used for delayed
3323    parsing of in-class method bodies and default arguments, and should
3324    not be confused with tentative parsing.  */
3325 static void
3326 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3327 {
3328   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3329   lexer->next = parser->lexer;
3330   parser->lexer = lexer;
3331
3332   /* Move the current source position to that of the first token in the
3333      new lexer.  */
3334   cp_lexer_set_source_position_from_token (lexer->next_token);
3335 }
3336
3337 /* Pop the top lexer off the parser stack.  This is never used for the
3338    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3339 static void
3340 cp_parser_pop_lexer (cp_parser *parser)
3341 {
3342   cp_lexer *lexer = parser->lexer;
3343   parser->lexer = lexer->next;
3344   cp_lexer_destroy (lexer);
3345
3346   /* Put the current source position back where it was before this
3347      lexer was pushed.  */
3348   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3349 }
3350
3351 /* Lexical conventions [gram.lex]  */
3352
3353 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3354    identifier.  */
3355
3356 static tree
3357 cp_parser_identifier (cp_parser* parser)
3358 {
3359   cp_token *token;
3360
3361   /* Look for the identifier.  */
3362   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3363   /* Return the value.  */
3364   return token ? token->u.value : error_mark_node;
3365 }
3366
3367 /* Parse a sequence of adjacent string constants.  Returns a
3368    TREE_STRING representing the combined, nul-terminated string
3369    constant.  If TRANSLATE is true, translate the string to the
3370    execution character set.  If WIDE_OK is true, a wide string is
3371    invalid here.
3372
3373    C++98 [lex.string] says that if a narrow string literal token is
3374    adjacent to a wide string literal token, the behavior is undefined.
3375    However, C99 6.4.5p4 says that this results in a wide string literal.
3376    We follow C99 here, for consistency with the C front end.
3377
3378    This code is largely lifted from lex_string() in c-lex.c.
3379
3380    FUTURE: ObjC++ will need to handle @-strings here.  */
3381 static tree
3382 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3383 {
3384   tree value;
3385   size_t count;
3386   struct obstack str_ob;
3387   cpp_string str, istr, *strs;
3388   cp_token *tok;
3389   enum cpp_ttype type, curr_type;
3390   int have_suffix_p = 0;
3391   tree string_tree;
3392   tree suffix_id = NULL_TREE;
3393   bool curr_tok_is_userdef_p = false;
3394
3395   tok = cp_lexer_peek_token (parser->lexer);
3396   if (!cp_parser_is_string_literal (tok))
3397     {
3398       cp_parser_error (parser, "expected string-literal");
3399       return error_mark_node;
3400     }
3401
3402   if (cpp_userdef_string_p (tok->type))
3403     {
3404       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3405       curr_type = cpp_userdef_string_remove_type (tok->type);
3406       curr_tok_is_userdef_p = true;
3407     }
3408   else
3409     {
3410       string_tree = tok->u.value;
3411       curr_type = tok->type;
3412     }
3413   type = curr_type;
3414
3415   /* Try to avoid the overhead of creating and destroying an obstack
3416      for the common case of just one string.  */
3417   if (!cp_parser_is_string_literal
3418       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3419     {
3420       cp_lexer_consume_token (parser->lexer);
3421
3422       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3423       str.len = TREE_STRING_LENGTH (string_tree);
3424       count = 1;
3425
3426       if (curr_tok_is_userdef_p)
3427         {
3428           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3429           have_suffix_p = 1;
3430           curr_type = cpp_userdef_string_remove_type (tok->type);
3431         }
3432       else
3433         curr_type = tok->type;
3434
3435       strs = &str;
3436     }
3437   else
3438     {
3439       gcc_obstack_init (&str_ob);
3440       count = 0;
3441
3442       do
3443         {
3444           cp_lexer_consume_token (parser->lexer);
3445           count++;
3446           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3447           str.len = TREE_STRING_LENGTH (string_tree);
3448
3449           if (curr_tok_is_userdef_p)
3450             {
3451               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3452               if (have_suffix_p == 0)
3453                 {
3454                   suffix_id = curr_suffix_id;
3455                   have_suffix_p = 1;
3456                 }
3457               else if (have_suffix_p == 1
3458                        && curr_suffix_id != suffix_id)
3459                 {
3460                   error ("inconsistent user-defined literal suffixes"
3461                          " %qD and %qD in string literal",
3462                          suffix_id, curr_suffix_id);
3463                   have_suffix_p = -1;
3464                 }
3465               curr_type = cpp_userdef_string_remove_type (tok->type);
3466             }
3467           else
3468             curr_type = tok->type;
3469
3470           if (type != curr_type)
3471             {
3472               if (type == CPP_STRING)
3473                 type = curr_type;
3474               else if (curr_type != CPP_STRING)
3475                 error_at (tok->location,
3476                           "unsupported non-standard concatenation "
3477                           "of string literals");
3478             }
3479
3480           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3481
3482           tok = cp_lexer_peek_token (parser->lexer);
3483           if (cpp_userdef_string_p (tok->type))
3484             {
3485               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3486               curr_type = cpp_userdef_string_remove_type (tok->type);
3487               curr_tok_is_userdef_p = true;
3488             }
3489           else
3490             {
3491               string_tree = tok->u.value;
3492               curr_type = tok->type;
3493               curr_tok_is_userdef_p = false;
3494             }
3495         }
3496       while (cp_parser_is_string_literal (tok));
3497
3498       strs = (cpp_string *) obstack_finish (&str_ob);
3499     }
3500
3501   if (type != CPP_STRING && !wide_ok)
3502     {
3503       cp_parser_error (parser, "a wide string is invalid in this context");
3504       type = CPP_STRING;
3505     }
3506
3507   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3508       (parse_in, strs, count, &istr, type))
3509     {
3510       value = build_string (istr.len, (const char *)istr.text);
3511       free (CONST_CAST (unsigned char *, istr.text));
3512
3513       switch (type)
3514         {
3515         default:
3516         case CPP_STRING:
3517         case CPP_UTF8STRING:
3518           TREE_TYPE (value) = char_array_type_node;
3519           break;
3520         case CPP_STRING16:
3521           TREE_TYPE (value) = char16_array_type_node;
3522           break;
3523         case CPP_STRING32:
3524           TREE_TYPE (value) = char32_array_type_node;
3525           break;
3526         case CPP_WSTRING:
3527           TREE_TYPE (value) = wchar_array_type_node;
3528           break;
3529         }
3530
3531       value = fix_string_type (value);
3532
3533       if (have_suffix_p)
3534         {
3535           tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3536           tok->u.value = literal;
3537           return cp_parser_userdef_string_literal (tok);
3538         }
3539     }
3540   else
3541     /* cpp_interpret_string has issued an error.  */
3542     value = error_mark_node;
3543
3544   if (count > 1)
3545     obstack_free (&str_ob, 0);
3546
3547   return value;
3548 }
3549
3550 /* Look up a literal operator with the name and the exact arguments.  */
3551
3552 static tree
3553 lookup_literal_operator (tree name, VEC(tree,gc) *args)
3554 {
3555   tree decl, fns;
3556   decl = lookup_name (name);
3557   if (!decl || !is_overloaded_fn (decl))
3558     return error_mark_node;
3559
3560   for (fns = decl; fns; fns = OVL_NEXT (fns))
3561     {
3562       unsigned int ix;
3563       bool found = true;
3564       tree fn = OVL_CURRENT (fns);
3565       tree argtypes = NULL_TREE;
3566       argtypes = TYPE_ARG_TYPES (TREE_TYPE (fn));
3567       if (argtypes != NULL_TREE)
3568         {
3569           for (ix = 0; ix < VEC_length (tree, args) && argtypes != NULL_TREE;
3570                ++ix, argtypes = TREE_CHAIN (argtypes))
3571             {
3572               tree targ = TREE_VALUE (argtypes);
3573               tree tparm = TREE_TYPE (VEC_index (tree, args, ix));
3574               bool ptr = TREE_CODE (targ) == POINTER_TYPE;
3575               bool arr = TREE_CODE (tparm) == ARRAY_TYPE;
3576               if ((ptr || arr || !same_type_p (targ, tparm))
3577                   && (!ptr || !arr
3578                       || !same_type_p (TREE_TYPE (targ),
3579                                        TREE_TYPE (tparm))))
3580                 found = false;
3581             }
3582           if (found)
3583             return fn;
3584         }
3585     }
3586
3587   return error_mark_node;
3588 }
3589
3590 /* Parse a user-defined char constant.  Returns a call to a user-defined
3591    literal operator taking the character as an argument.  */
3592
3593 static tree
3594 cp_parser_userdef_char_literal (cp_parser *parser)
3595 {
3596   cp_token *token = cp_lexer_consume_token (parser->lexer);
3597   tree literal = token->u.value;
3598   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3599   tree value = USERDEF_LITERAL_VALUE (literal);
3600   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3601   tree decl, result;
3602
3603   /* Build up a call to the user-defined operator  */
3604   /* Lookup the name we got back from the id-expression.  */
3605   VEC(tree,gc) *args = make_tree_vector ();
3606   VEC_safe_push (tree, gc, args, value);
3607   decl = lookup_literal_operator (name, args);
3608   if (!decl || decl == error_mark_node)
3609     {
3610       error ("unable to find character literal operator %qD with %qT argument",
3611              name, TREE_TYPE (value));
3612       release_tree_vector (args);
3613       return error_mark_node;
3614     }
3615   result = finish_call_expr (decl, &args, false, true, tf_warning_or_error);
3616   release_tree_vector (args);
3617   if (result != error_mark_node)
3618     return result;
3619
3620   error ("unable to find character literal operator %qD with %qT argument",
3621          name, TREE_TYPE (value));
3622   return error_mark_node;
3623 }
3624
3625 /* A subroutine of cp_parser_userdef_numeric_literal to
3626    create a char... template parameter pack from a string node.  */
3627
3628 static tree
3629 make_char_string_pack (tree value)
3630 {
3631   tree charvec;
3632   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3633   const char *str = TREE_STRING_POINTER (value);
3634   int i, len = TREE_STRING_LENGTH (value) - 1;
3635   tree argvec = make_tree_vec (1);
3636
3637   /* Fill in CHARVEC with all of the parameters.  */
3638   charvec = make_tree_vec (len);
3639   for (i = 0; i < len; ++i)
3640     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3641
3642   /* Build the argument packs.  */
3643   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3644   TREE_TYPE (argpack) = char_type_node;
3645
3646   TREE_VEC_ELT (argvec, 0) = argpack;
3647
3648   return argvec;
3649 }
3650
3651 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3652    literal operator.  */
3653
3654 static tree
3655 cp_parser_userdef_numeric_literal (cp_parser *parser)
3656 {
3657   cp_token *token = cp_lexer_consume_token (parser->lexer);
3658   tree literal = token->u.value;
3659   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3660   tree value = USERDEF_LITERAL_VALUE (literal);
3661   tree num_string = USERDEF_LITERAL_NUM_STRING (literal);
3662   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3663   tree decl, result;
3664   VEC(tree,gc) *args;
3665
3666   /* Look for a literal operator taking the exact type of numeric argument
3667      as the literal value.  */
3668   args = make_tree_vector ();
3669   VEC_safe_push (tree, gc, args, value);
3670   decl = lookup_literal_operator (name, args);
3671   if (decl && decl != error_mark_node)
3672     {
3673       result = finish_call_expr (decl, &args, false, true, tf_none);
3674       if (result != error_mark_node)
3675         {
3676           release_tree_vector (args);
3677           return result;
3678         }
3679     }
3680   release_tree_vector (args);
3681
3682   /* If the numeric argument didn't work, look for a raw literal
3683      operator taking a const char* argument consisting of the number
3684      in string format.  */
3685   args = make_tree_vector ();
3686   VEC_safe_push (tree, gc, args, num_string);
3687   decl = lookup_literal_operator (name, args);
3688   if (decl && decl != error_mark_node)
3689     {
3690       result = finish_call_expr (decl, &args, false, true, tf_none);
3691       if (result != error_mark_node)
3692         {
3693           release_tree_vector (args);
3694           return result;
3695         }
3696     }
3697   release_tree_vector (args);
3698
3699   /* If the raw literal didn't work, look for a non-type template
3700      function with parameter pack char....  Call the function with
3701      template parameter characters representing the number.  */
3702   args = make_tree_vector ();
3703   decl = lookup_literal_operator (name, args);
3704   if (decl && decl != error_mark_node)
3705     {
3706       tree tmpl_args = make_char_string_pack (num_string);
3707       decl = lookup_template_function (decl, tmpl_args);
3708       result = finish_call_expr (decl, &args, false, true, tf_none);
3709       if (result != error_mark_node)
3710         {
3711           release_tree_vector (args);
3712           return result;
3713         }
3714     }
3715   release_tree_vector (args);
3716
3717   error ("unable to find numeric literal operator %qD", name);
3718   return error_mark_node;
3719 }
3720
3721 /* Parse a user-defined string constant.  Returns a call to a user-defined
3722    literal operator taking a character pointer and the length of the string
3723    as arguments.  */
3724
3725 static tree
3726 cp_parser_userdef_string_literal (cp_token *token)
3727 {
3728   tree literal = token->u.value;
3729   tree suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3730   tree name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3731   tree value = USERDEF_LITERAL_VALUE (literal);
3732   int len = TREE_STRING_LENGTH (value)
3733         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3734   tree decl, result;
3735
3736   /* Build up a call to the user-defined operator  */
3737   /* Lookup the name we got back from the id-expression.  */
3738   VEC(tree,gc) *args = make_tree_vector ();
3739   VEC_safe_push (tree, gc, args, value);
3740   VEC_safe_push (tree, gc, args, build_int_cst (size_type_node, len));
3741   decl = lookup_name (name);
3742   if (!decl || decl == error_mark_node)
3743     {
3744       error ("unable to find string literal operator %qD", name);
3745       release_tree_vector (args);
3746       return error_mark_node;
3747     }
3748   result = finish_call_expr (decl, &args, false, true, tf_none);
3749   release_tree_vector (args);
3750   if (result != error_mark_node)
3751     return result;
3752
3753   error ("unable to find string literal operator %qD with %qT, %qT arguments",
3754          name, TREE_TYPE (value), size_type_node);
3755   return error_mark_node;
3756 }
3757
3758
3759 /* Basic concepts [gram.basic]  */
3760
3761 /* Parse a translation-unit.
3762
3763    translation-unit:
3764      declaration-seq [opt]
3765
3766    Returns TRUE if all went well.  */
3767
3768 static bool
3769 cp_parser_translation_unit (cp_parser* parser)
3770 {
3771   /* The address of the first non-permanent object on the declarator
3772      obstack.  */
3773   static void *declarator_obstack_base;
3774
3775   bool success;
3776
3777   /* Create the declarator obstack, if necessary.  */
3778   if (!cp_error_declarator)
3779     {
3780       gcc_obstack_init (&declarator_obstack);
3781       /* Create the error declarator.  */
3782       cp_error_declarator = make_declarator (cdk_error);
3783       /* Create the empty parameter list.  */
3784       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3785       /* Remember where the base of the declarator obstack lies.  */
3786       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3787     }
3788
3789   cp_parser_declaration_seq_opt (parser);
3790
3791   /* If there are no tokens left then all went well.  */
3792   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3793     {
3794       /* Get rid of the token array; we don't need it any more.  */
3795       cp_lexer_destroy (parser->lexer);
3796       parser->lexer = NULL;
3797
3798       /* This file might have been a context that's implicitly extern
3799          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3800       if (parser->implicit_extern_c)
3801         {
3802           pop_lang_context ();
3803           parser->implicit_extern_c = false;
3804         }
3805
3806       /* Finish up.  */
3807       finish_translation_unit ();
3808
3809       success = true;
3810     }
3811   else
3812     {
3813       cp_parser_error (parser, "expected declaration");
3814       success = false;
3815     }
3816
3817   /* Make sure the declarator obstack was fully cleaned up.  */
3818   gcc_assert (obstack_next_free (&declarator_obstack)
3819               == declarator_obstack_base);
3820
3821   /* All went well.  */
3822   return success;
3823 }
3824
3825 /* Expressions [gram.expr] */
3826
3827 /* Parse a primary-expression.
3828
3829    primary-expression:
3830      literal
3831      this
3832      ( expression )
3833      id-expression
3834
3835    GNU Extensions:
3836
3837    primary-expression:
3838      ( compound-statement )
3839      __builtin_va_arg ( assignment-expression , type-id )
3840      __builtin_offsetof ( type-id , offsetof-expression )
3841
3842    C++ Extensions:
3843      __has_nothrow_assign ( type-id )   
3844      __has_nothrow_constructor ( type-id )
3845      __has_nothrow_copy ( type-id )
3846      __has_trivial_assign ( type-id )   
3847      __has_trivial_constructor ( type-id )
3848      __has_trivial_copy ( type-id )
3849      __has_trivial_destructor ( type-id )
3850      __has_virtual_destructor ( type-id )     
3851      __is_abstract ( type-id )
3852      __is_base_of ( type-id , type-id )
3853      __is_class ( type-id )
3854      __is_convertible_to ( type-id , type-id )     
3855      __is_empty ( type-id )
3856      __is_enum ( type-id )
3857      __is_final ( type-id )
3858      __is_literal_type ( type-id )
3859      __is_pod ( type-id )
3860      __is_polymorphic ( type-id )
3861      __is_std_layout ( type-id )
3862      __is_trivial ( type-id )
3863      __is_union ( type-id )
3864
3865    Objective-C++ Extension:
3866
3867    primary-expression:
3868      objc-expression
3869
3870    literal:
3871      __null
3872
3873    ADDRESS_P is true iff this expression was immediately preceded by
3874    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3875    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3876    true iff this expression is a template argument.
3877
3878    Returns a representation of the expression.  Upon return, *IDK
3879    indicates what kind of id-expression (if any) was present.  */
3880
3881 static tree
3882 cp_parser_primary_expression (cp_parser *parser,
3883                               bool address_p,
3884                               bool cast_p,
3885                               bool template_arg_p,
3886                               cp_id_kind *idk)
3887 {
3888   cp_token *token = NULL;
3889
3890   /* Assume the primary expression is not an id-expression.  */
3891   *idk = CP_ID_KIND_NONE;
3892
3893   /* Peek at the next token.  */
3894   token = cp_lexer_peek_token (parser->lexer);
3895   switch (token->type)
3896     {
3897       /* literal:
3898            integer-literal
3899            character-literal
3900            floating-literal
3901            string-literal
3902            boolean-literal
3903            pointer-literal
3904            user-defined-literal  */
3905     case CPP_CHAR:
3906     case CPP_CHAR16:
3907     case CPP_CHAR32:
3908     case CPP_WCHAR:
3909     case CPP_NUMBER:
3910       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3911         return cp_parser_userdef_numeric_literal (parser);
3912       token = cp_lexer_consume_token (parser->lexer);
3913       if (TREE_CODE (token->u.value) == FIXED_CST)
3914         {
3915           error_at (token->location,
3916                     "fixed-point types not supported in C++");
3917           return error_mark_node;
3918         }
3919       /* Floating-point literals are only allowed in an integral
3920          constant expression if they are cast to an integral or
3921          enumeration type.  */
3922       if (TREE_CODE (token->u.value) == REAL_CST
3923           && parser->integral_constant_expression_p
3924           && pedantic)
3925         {
3926           /* CAST_P will be set even in invalid code like "int(2.7 +
3927              ...)".   Therefore, we have to check that the next token
3928              is sure to end the cast.  */
3929           if (cast_p)
3930             {
3931               cp_token *next_token;
3932
3933               next_token = cp_lexer_peek_token (parser->lexer);
3934               if (/* The comma at the end of an
3935                      enumerator-definition.  */
3936                   next_token->type != CPP_COMMA
3937                   /* The curly brace at the end of an enum-specifier.  */
3938                   && next_token->type != CPP_CLOSE_BRACE
3939                   /* The end of a statement.  */
3940                   && next_token->type != CPP_SEMICOLON
3941                   /* The end of the cast-expression.  */
3942                   && next_token->type != CPP_CLOSE_PAREN
3943                   /* The end of an array bound.  */
3944                   && next_token->type != CPP_CLOSE_SQUARE
3945                   /* The closing ">" in a template-argument-list.  */
3946                   && (next_token->type != CPP_GREATER
3947                       || parser->greater_than_is_operator_p)
3948                   /* C++0x only: A ">>" treated like two ">" tokens,
3949                      in a template-argument-list.  */
3950                   && (next_token->type != CPP_RSHIFT
3951                       || (cxx_dialect == cxx98)
3952                       || parser->greater_than_is_operator_p))
3953                 cast_p = false;
3954             }
3955
3956           /* If we are within a cast, then the constraint that the
3957              cast is to an integral or enumeration type will be
3958              checked at that point.  If we are not within a cast, then
3959              this code is invalid.  */
3960           if (!cast_p)
3961             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3962         }
3963       return token->u.value;
3964
3965     case CPP_CHAR_USERDEF:
3966     case CPP_CHAR16_USERDEF:
3967     case CPP_CHAR32_USERDEF:
3968     case CPP_WCHAR_USERDEF:
3969       return cp_parser_userdef_char_literal (parser);
3970
3971     case CPP_STRING:
3972     case CPP_STRING16:
3973     case CPP_STRING32:
3974     case CPP_WSTRING:
3975     case CPP_UTF8STRING:
3976     case CPP_STRING_USERDEF:
3977     case CPP_STRING16_USERDEF:
3978     case CPP_STRING32_USERDEF:
3979     case CPP_WSTRING_USERDEF:
3980     case CPP_UTF8STRING_USERDEF:
3981       /* ??? Should wide strings be allowed when parser->translate_strings_p
3982          is false (i.e. in attributes)?  If not, we can kill the third
3983          argument to cp_parser_string_literal.  */
3984       return cp_parser_string_literal (parser,
3985                                        parser->translate_strings_p,
3986                                        true);
3987
3988     case CPP_OPEN_PAREN:
3989       {
3990         tree expr;
3991         bool saved_greater_than_is_operator_p;
3992
3993         /* Consume the `('.  */
3994         cp_lexer_consume_token (parser->lexer);
3995         /* Within a parenthesized expression, a `>' token is always
3996            the greater-than operator.  */
3997         saved_greater_than_is_operator_p
3998           = parser->greater_than_is_operator_p;
3999         parser->greater_than_is_operator_p = true;
4000         /* If we see `( { ' then we are looking at the beginning of
4001            a GNU statement-expression.  */
4002         if (cp_parser_allow_gnu_extensions_p (parser)
4003             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
4004           {
4005             /* Statement-expressions are not allowed by the standard.  */
4006             pedwarn (token->location, OPT_pedantic, 
4007                      "ISO C++ forbids braced-groups within expressions");
4008
4009             /* And they're not allowed outside of a function-body; you
4010                cannot, for example, write:
4011
4012                  int i = ({ int j = 3; j + 1; });
4013
4014                at class or namespace scope.  */
4015             if (!parser->in_function_body
4016                 || parser->in_template_argument_list_p)
4017               {
4018                 error_at (token->location,
4019                           "statement-expressions are not allowed outside "
4020                           "functions nor in template-argument lists");
4021                 cp_parser_skip_to_end_of_block_or_statement (parser);
4022                 expr = error_mark_node;
4023               }
4024             else
4025               {
4026                 /* Start the statement-expression.  */
4027                 expr = begin_stmt_expr ();
4028                 /* Parse the compound-statement.  */
4029                 cp_parser_compound_statement (parser, expr, false, false);
4030                 /* Finish up.  */
4031                 expr = finish_stmt_expr (expr, false);
4032               }
4033           }
4034         else
4035           {
4036             /* Parse the parenthesized expression.  */
4037             expr = cp_parser_expression (parser, cast_p, idk);
4038             /* Let the front end know that this expression was
4039                enclosed in parentheses. This matters in case, for
4040                example, the expression is of the form `A::B', since
4041                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4042                not.  */
4043             finish_parenthesized_expr (expr);
4044             /* DR 705: Wrapping an unqualified name in parentheses
4045                suppresses arg-dependent lookup.  We want to pass back
4046                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4047                (c++/37862), but none of the others.  */
4048             if (*idk != CP_ID_KIND_QUALIFIED)
4049               *idk = CP_ID_KIND_NONE;
4050           }
4051         /* The `>' token might be the end of a template-id or
4052            template-parameter-list now.  */
4053         parser->greater_than_is_operator_p
4054           = saved_greater_than_is_operator_p;
4055         /* Consume the `)'.  */
4056         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4057           cp_parser_skip_to_end_of_statement (parser);
4058
4059         return expr;
4060       }
4061
4062     case CPP_OPEN_SQUARE:
4063       if (c_dialect_objc ())
4064         /* We have an Objective-C++ message. */
4065         return cp_parser_objc_expression (parser);
4066       {
4067         tree lam = cp_parser_lambda_expression (parser);
4068         /* Don't warn about a failed tentative parse.  */
4069         if (cp_parser_error_occurred (parser))
4070           return error_mark_node;
4071         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4072         return lam;
4073       }
4074
4075     case CPP_OBJC_STRING:
4076       if (c_dialect_objc ())
4077         /* We have an Objective-C++ string literal. */
4078         return cp_parser_objc_expression (parser);
4079       cp_parser_error (parser, "expected primary-expression");
4080       return error_mark_node;
4081
4082     case CPP_KEYWORD:
4083       switch (token->keyword)
4084         {
4085           /* These two are the boolean literals.  */
4086         case RID_TRUE:
4087           cp_lexer_consume_token (parser->lexer);
4088           return boolean_true_node;
4089         case RID_FALSE:
4090           cp_lexer_consume_token (parser->lexer);
4091           return boolean_false_node;
4092
4093           /* The `__null' literal.  */
4094         case RID_NULL:
4095           cp_lexer_consume_token (parser->lexer);
4096           return null_node;
4097
4098           /* The `nullptr' literal.  */
4099         case RID_NULLPTR:
4100           cp_lexer_consume_token (parser->lexer);
4101           return nullptr_node;
4102
4103           /* Recognize the `this' keyword.  */
4104         case RID_THIS:
4105           cp_lexer_consume_token (parser->lexer);
4106           if (parser->local_variables_forbidden_p)
4107             {
4108               error_at (token->location,
4109                         "%<this%> may not be used in this context");
4110               return error_mark_node;
4111             }
4112           /* Pointers cannot appear in constant-expressions.  */
4113           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4114             return error_mark_node;
4115           return finish_this_expr ();
4116
4117           /* The `operator' keyword can be the beginning of an
4118              id-expression.  */
4119         case RID_OPERATOR:
4120           goto id_expression;
4121
4122         case RID_FUNCTION_NAME:
4123         case RID_PRETTY_FUNCTION_NAME:
4124         case RID_C99_FUNCTION_NAME:
4125           {
4126             non_integral_constant name;
4127
4128             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4129                __func__ are the names of variables -- but they are
4130                treated specially.  Therefore, they are handled here,
4131                rather than relying on the generic id-expression logic
4132                below.  Grammatically, these names are id-expressions.
4133
4134                Consume the token.  */
4135             token = cp_lexer_consume_token (parser->lexer);
4136
4137             switch (token->keyword)
4138               {
4139               case RID_FUNCTION_NAME:
4140                 name = NIC_FUNC_NAME;
4141                 break;
4142               case RID_PRETTY_FUNCTION_NAME:
4143                 name = NIC_PRETTY_FUNC;
4144                 break;
4145               case RID_C99_FUNCTION_NAME:
4146                 name = NIC_C99_FUNC;
4147                 break;
4148               default:
4149                 gcc_unreachable ();
4150               }
4151
4152             if (cp_parser_non_integral_constant_expression (parser, name))
4153               return error_mark_node;
4154
4155             /* Look up the name.  */
4156             return finish_fname (token->u.value);
4157           }
4158
4159         case RID_VA_ARG:
4160           {
4161             tree expression;
4162             tree type;
4163
4164             /* The `__builtin_va_arg' construct is used to handle
4165                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4166             cp_lexer_consume_token (parser->lexer);
4167             /* Look for the opening `('.  */
4168             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4169             /* Now, parse the assignment-expression.  */
4170             expression = cp_parser_assignment_expression (parser,
4171                                                           /*cast_p=*/false, NULL);
4172             /* Look for the `,'.  */
4173             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4174             /* Parse the type-id.  */
4175             type = cp_parser_type_id (parser);
4176             /* Look for the closing `)'.  */
4177             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4178             /* Using `va_arg' in a constant-expression is not
4179                allowed.  */
4180             if (cp_parser_non_integral_constant_expression (parser,
4181                                                             NIC_VA_ARG))
4182               return error_mark_node;
4183             return build_x_va_arg (expression, type);
4184           }
4185
4186         case RID_OFFSETOF:
4187           return cp_parser_builtin_offsetof (parser);
4188
4189         case RID_HAS_NOTHROW_ASSIGN:
4190         case RID_HAS_NOTHROW_CONSTRUCTOR:
4191         case RID_HAS_NOTHROW_COPY:        
4192         case RID_HAS_TRIVIAL_ASSIGN:
4193         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4194         case RID_HAS_TRIVIAL_COPY:        
4195         case RID_HAS_TRIVIAL_DESTRUCTOR:
4196         case RID_HAS_VIRTUAL_DESTRUCTOR:
4197         case RID_IS_ABSTRACT:
4198         case RID_IS_BASE_OF:
4199         case RID_IS_CLASS:
4200         case RID_IS_CONVERTIBLE_TO:
4201         case RID_IS_EMPTY:
4202         case RID_IS_ENUM:
4203         case RID_IS_FINAL:
4204         case RID_IS_LITERAL_TYPE:
4205         case RID_IS_POD:
4206         case RID_IS_POLYMORPHIC:
4207         case RID_IS_STD_LAYOUT:
4208         case RID_IS_TRIVIAL:
4209         case RID_IS_UNION:
4210           return cp_parser_trait_expr (parser, token->keyword);
4211
4212         /* Objective-C++ expressions.  */
4213         case RID_AT_ENCODE:
4214         case RID_AT_PROTOCOL:
4215         case RID_AT_SELECTOR:
4216           return cp_parser_objc_expression (parser);
4217
4218         case RID_TEMPLATE:
4219           if (parser->in_function_body
4220               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4221                   == CPP_LESS))
4222             {
4223               error_at (token->location,
4224                         "a template declaration cannot appear at block scope");
4225               cp_parser_skip_to_end_of_block_or_statement (parser);
4226               return error_mark_node;
4227             }
4228         default:
4229           cp_parser_error (parser, "expected primary-expression");
4230           return error_mark_node;
4231         }
4232
4233       /* An id-expression can start with either an identifier, a
4234          `::' as the beginning of a qualified-id, or the "operator"
4235          keyword.  */
4236     case CPP_NAME:
4237     case CPP_SCOPE:
4238     case CPP_TEMPLATE_ID:
4239     case CPP_NESTED_NAME_SPECIFIER:
4240       {
4241         tree id_expression;
4242         tree decl;
4243         const char *error_msg;
4244         bool template_p;
4245         bool done;
4246         cp_token *id_expr_token;
4247
4248       id_expression:
4249         /* Parse the id-expression.  */
4250         id_expression
4251           = cp_parser_id_expression (parser,
4252                                      /*template_keyword_p=*/false,
4253                                      /*check_dependency_p=*/true,
4254                                      &template_p,
4255                                      /*declarator_p=*/false,
4256                                      /*optional_p=*/false);
4257         if (id_expression == error_mark_node)
4258           return error_mark_node;
4259         id_expr_token = token;
4260         token = cp_lexer_peek_token (parser->lexer);
4261         done = (token->type != CPP_OPEN_SQUARE
4262                 && token->type != CPP_OPEN_PAREN
4263                 && token->type != CPP_DOT
4264                 && token->type != CPP_DEREF
4265                 && token->type != CPP_PLUS_PLUS
4266                 && token->type != CPP_MINUS_MINUS);
4267         /* If we have a template-id, then no further lookup is
4268            required.  If the template-id was for a template-class, we
4269            will sometimes have a TYPE_DECL at this point.  */
4270         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4271                  || TREE_CODE (id_expression) == TYPE_DECL)
4272           decl = id_expression;
4273         /* Look up the name.  */
4274         else
4275           {
4276             tree ambiguous_decls;
4277
4278             /* If we already know that this lookup is ambiguous, then
4279                we've already issued an error message; there's no reason
4280                to check again.  */
4281             if (id_expr_token->type == CPP_NAME
4282                 && id_expr_token->ambiguous_p)
4283               {
4284                 cp_parser_simulate_error (parser);
4285                 return error_mark_node;
4286               }
4287
4288             decl = cp_parser_lookup_name (parser, id_expression,
4289                                           none_type,
4290                                           template_p,
4291                                           /*is_namespace=*/false,
4292                                           /*check_dependency=*/true,
4293                                           &ambiguous_decls,
4294                                           id_expr_token->location);
4295             /* If the lookup was ambiguous, an error will already have
4296                been issued.  */
4297             if (ambiguous_decls)
4298               return error_mark_node;
4299
4300             /* In Objective-C++, we may have an Objective-C 2.0
4301                dot-syntax for classes here.  */
4302             if (c_dialect_objc ()
4303                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4304                 && TREE_CODE (decl) == TYPE_DECL
4305                 && objc_is_class_name (decl))
4306               {
4307                 tree component;
4308                 cp_lexer_consume_token (parser->lexer);
4309                 component = cp_parser_identifier (parser);
4310                 if (component == error_mark_node)
4311                   return error_mark_node;
4312
4313                 return objc_build_class_component_ref (id_expression, component);
4314               }
4315
4316             /* In Objective-C++, an instance variable (ivar) may be preferred
4317                to whatever cp_parser_lookup_name() found.  */
4318             decl = objc_lookup_ivar (decl, id_expression);
4319
4320             /* If name lookup gives us a SCOPE_REF, then the
4321                qualifying scope was dependent.  */
4322             if (TREE_CODE (decl) == SCOPE_REF)
4323               {
4324                 /* At this point, we do not know if DECL is a valid
4325                    integral constant expression.  We assume that it is
4326                    in fact such an expression, so that code like:
4327
4328                       template <int N> struct A {
4329                         int a[B<N>::i];
4330                       };
4331                      
4332                    is accepted.  At template-instantiation time, we
4333                    will check that B<N>::i is actually a constant.  */
4334                 return decl;
4335               }
4336             /* Check to see if DECL is a local variable in a context
4337                where that is forbidden.  */
4338             if (parser->local_variables_forbidden_p
4339                 && local_variable_p (decl))
4340               {
4341                 /* It might be that we only found DECL because we are
4342                    trying to be generous with pre-ISO scoping rules.
4343                    For example, consider:
4344
4345                      int i;
4346                      void g() {
4347                        for (int i = 0; i < 10; ++i) {}
4348                        extern void f(int j = i);
4349                      }
4350
4351                    Here, name look up will originally find the out
4352                    of scope `i'.  We need to issue a warning message,
4353                    but then use the global `i'.  */
4354                 decl = check_for_out_of_scope_variable (decl);
4355                 if (local_variable_p (decl))
4356                   {
4357                     error_at (id_expr_token->location,
4358                               "local variable %qD may not appear in this context",
4359                               decl);
4360                     return error_mark_node;
4361                   }
4362               }
4363           }
4364
4365         decl = (finish_id_expression
4366                 (id_expression, decl, parser->scope,
4367                  idk,
4368                  parser->integral_constant_expression_p,
4369                  parser->allow_non_integral_constant_expression_p,
4370                  &parser->non_integral_constant_expression_p,
4371                  template_p, done, address_p,
4372                  template_arg_p,
4373                  &error_msg,
4374                  id_expr_token->location));
4375         if (error_msg)
4376           cp_parser_error (parser, error_msg);
4377         return decl;
4378       }
4379
4380       /* Anything else is an error.  */
4381     default:
4382       cp_parser_error (parser, "expected primary-expression");
4383       return error_mark_node;
4384     }
4385 }
4386
4387 /* Parse an id-expression.
4388
4389    id-expression:
4390      unqualified-id
4391      qualified-id
4392
4393    qualified-id:
4394      :: [opt] nested-name-specifier template [opt] unqualified-id
4395      :: identifier
4396      :: operator-function-id
4397      :: template-id
4398
4399    Return a representation of the unqualified portion of the
4400    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4401    a `::' or nested-name-specifier.
4402
4403    Often, if the id-expression was a qualified-id, the caller will
4404    want to make a SCOPE_REF to represent the qualified-id.  This
4405    function does not do this in order to avoid wastefully creating
4406    SCOPE_REFs when they are not required.
4407
4408    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4409    `template' keyword.
4410
4411    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4412    uninstantiated templates.
4413
4414    If *TEMPLATE_P is non-NULL, it is set to true iff the
4415    `template' keyword is used to explicitly indicate that the entity
4416    named is a template.
4417
4418    If DECLARATOR_P is true, the id-expression is appearing as part of
4419    a declarator, rather than as part of an expression.  */
4420
4421 static tree
4422 cp_parser_id_expression (cp_parser *parser,
4423                          bool template_keyword_p,
4424                          bool check_dependency_p,
4425                          bool *template_p,
4426                          bool declarator_p,
4427                          bool optional_p)
4428 {
4429   bool global_scope_p;
4430   bool nested_name_specifier_p;
4431
4432   /* Assume the `template' keyword was not used.  */
4433   if (template_p)
4434     *template_p = template_keyword_p;
4435
4436   /* Look for the optional `::' operator.  */
4437   global_scope_p
4438     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4439        != NULL_TREE);
4440   /* Look for the optional nested-name-specifier.  */
4441   nested_name_specifier_p
4442     = (cp_parser_nested_name_specifier_opt (parser,
4443                                             /*typename_keyword_p=*/false,
4444                                             check_dependency_p,
4445                                             /*type_p=*/false,
4446                                             declarator_p)
4447        != NULL_TREE);
4448   /* If there is a nested-name-specifier, then we are looking at
4449      the first qualified-id production.  */
4450   if (nested_name_specifier_p)
4451     {
4452       tree saved_scope;
4453       tree saved_object_scope;
4454       tree saved_qualifying_scope;
4455       tree unqualified_id;
4456       bool is_template;
4457
4458       /* See if the next token is the `template' keyword.  */
4459       if (!template_p)
4460         template_p = &is_template;
4461       *template_p = cp_parser_optional_template_keyword (parser);
4462       /* Name lookup we do during the processing of the
4463          unqualified-id might obliterate SCOPE.  */
4464       saved_scope = parser->scope;
4465       saved_object_scope = parser->object_scope;
4466       saved_qualifying_scope = parser->qualifying_scope;
4467       /* Process the final unqualified-id.  */
4468       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4469                                                  check_dependency_p,
4470                                                  declarator_p,
4471                                                  /*optional_p=*/false);
4472       /* Restore the SAVED_SCOPE for our caller.  */
4473       parser->scope = saved_scope;
4474       parser->object_scope = saved_object_scope;
4475       parser->qualifying_scope = saved_qualifying_scope;
4476
4477       return unqualified_id;
4478     }
4479   /* Otherwise, if we are in global scope, then we are looking at one
4480      of the other qualified-id productions.  */
4481   else if (global_scope_p)
4482     {
4483       cp_token *token;
4484       tree id;
4485
4486       /* Peek at the next token.  */
4487       token = cp_lexer_peek_token (parser->lexer);
4488
4489       /* If it's an identifier, and the next token is not a "<", then
4490          we can avoid the template-id case.  This is an optimization
4491          for this common case.  */
4492       if (token->type == CPP_NAME
4493           && !cp_parser_nth_token_starts_template_argument_list_p
4494                (parser, 2))
4495         return cp_parser_identifier (parser);
4496
4497       cp_parser_parse_tentatively (parser);
4498       /* Try a template-id.  */
4499       id = cp_parser_template_id (parser,
4500                                   /*template_keyword_p=*/false,
4501                                   /*check_dependency_p=*/true,
4502                                   declarator_p);
4503       /* If that worked, we're done.  */
4504       if (cp_parser_parse_definitely (parser))
4505         return id;
4506
4507       /* Peek at the next token.  (Changes in the token buffer may
4508          have invalidated the pointer obtained above.)  */
4509       token = cp_lexer_peek_token (parser->lexer);
4510
4511       switch (token->type)
4512         {
4513         case CPP_NAME:
4514           return cp_parser_identifier (parser);
4515
4516         case CPP_KEYWORD:
4517           if (token->keyword == RID_OPERATOR)
4518             return cp_parser_operator_function_id (parser);
4519           /* Fall through.  */
4520
4521         default:
4522           cp_parser_error (parser, "expected id-expression");
4523           return error_mark_node;
4524         }
4525     }
4526   else
4527     return cp_parser_unqualified_id (parser, template_keyword_p,
4528                                      /*check_dependency_p=*/true,
4529                                      declarator_p,
4530                                      optional_p);
4531 }
4532
4533 /* Parse an unqualified-id.
4534
4535    unqualified-id:
4536      identifier
4537      operator-function-id
4538      conversion-function-id
4539      ~ class-name
4540      template-id
4541
4542    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4543    keyword, in a construct like `A::template ...'.
4544
4545    Returns a representation of unqualified-id.  For the `identifier'
4546    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4547    production a BIT_NOT_EXPR is returned; the operand of the
4548    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4549    other productions, see the documentation accompanying the
4550    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4551    names are looked up in uninstantiated templates.  If DECLARATOR_P
4552    is true, the unqualified-id is appearing as part of a declarator,
4553    rather than as part of an expression.  */
4554
4555 static tree
4556 cp_parser_unqualified_id (cp_parser* parser,
4557                           bool template_keyword_p,
4558                           bool check_dependency_p,
4559                           bool declarator_p,
4560                           bool optional_p)
4561 {
4562   cp_token *token;
4563
4564   /* Peek at the next token.  */
4565   token = cp_lexer_peek_token (parser->lexer);
4566
4567   switch (token->type)
4568     {
4569     case CPP_NAME:
4570       {
4571         tree id;
4572
4573         /* We don't know yet whether or not this will be a
4574            template-id.  */
4575         cp_parser_parse_tentatively (parser);
4576         /* Try a template-id.  */
4577         id = cp_parser_template_id (parser, template_keyword_p,
4578                                     check_dependency_p,
4579                                     declarator_p);
4580         /* If it worked, we're done.  */
4581         if (cp_parser_parse_definitely (parser))
4582           return id;
4583         /* Otherwise, it's an ordinary identifier.  */
4584         return cp_parser_identifier (parser);
4585       }
4586
4587     case CPP_TEMPLATE_ID:
4588       return cp_parser_template_id (parser, template_keyword_p,
4589                                     check_dependency_p,
4590                                     declarator_p);
4591
4592     case CPP_COMPL:
4593       {
4594         tree type_decl;
4595         tree qualifying_scope;
4596         tree object_scope;
4597         tree scope;
4598         bool done;
4599
4600         /* Consume the `~' token.  */
4601         cp_lexer_consume_token (parser->lexer);
4602         /* Parse the class-name.  The standard, as written, seems to
4603            say that:
4604
4605              template <typename T> struct S { ~S (); };
4606              template <typename T> S<T>::~S() {}
4607
4608            is invalid, since `~' must be followed by a class-name, but
4609            `S<T>' is dependent, and so not known to be a class.
4610            That's not right; we need to look in uninstantiated
4611            templates.  A further complication arises from:
4612
4613              template <typename T> void f(T t) {
4614                t.T::~T();
4615              }
4616
4617            Here, it is not possible to look up `T' in the scope of `T'
4618            itself.  We must look in both the current scope, and the
4619            scope of the containing complete expression.
4620
4621            Yet another issue is:
4622
4623              struct S {
4624                int S;
4625                ~S();
4626              };
4627
4628              S::~S() {}
4629
4630            The standard does not seem to say that the `S' in `~S'
4631            should refer to the type `S' and not the data member
4632            `S::S'.  */
4633
4634         /* DR 244 says that we look up the name after the "~" in the
4635            same scope as we looked up the qualifying name.  That idea
4636            isn't fully worked out; it's more complicated than that.  */
4637         scope = parser->scope;
4638         object_scope = parser->object_scope;
4639         qualifying_scope = parser->qualifying_scope;
4640
4641         /* Check for invalid scopes.  */
4642         if (scope == error_mark_node)
4643           {
4644             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4645               cp_lexer_consume_token (parser->lexer);
4646             return error_mark_node;
4647           }
4648         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4649           {
4650             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4651               error_at (token->location,
4652                         "scope %qT before %<~%> is not a class-name",
4653                         scope);
4654             cp_parser_simulate_error (parser);
4655             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4656               cp_lexer_consume_token (parser->lexer);
4657             return error_mark_node;
4658           }
4659         gcc_assert (!scope || TYPE_P (scope));
4660
4661         /* If the name is of the form "X::~X" it's OK even if X is a
4662            typedef.  */
4663         token = cp_lexer_peek_token (parser->lexer);
4664         if (scope
4665             && token->type == CPP_NAME
4666             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4667                 != CPP_LESS)
4668             && (token->u.value == TYPE_IDENTIFIER (scope)
4669                 || (CLASS_TYPE_P (scope)
4670                     && constructor_name_p (token->u.value, scope))))
4671           {
4672             cp_lexer_consume_token (parser->lexer);
4673             return build_nt (BIT_NOT_EXPR, scope);
4674           }
4675
4676         /* If there was an explicit qualification (S::~T), first look
4677            in the scope given by the qualification (i.e., S).
4678
4679            Note: in the calls to cp_parser_class_name below we pass
4680            typename_type so that lookup finds the injected-class-name
4681            rather than the constructor.  */
4682         done = false;
4683         type_decl = NULL_TREE;
4684         if (scope)
4685           {
4686             cp_parser_parse_tentatively (parser);
4687             type_decl = cp_parser_class_name (parser,
4688                                               /*typename_keyword_p=*/false,
4689                                               /*template_keyword_p=*/false,
4690                                               typename_type,
4691                                               /*check_dependency=*/false,
4692                                               /*class_head_p=*/false,
4693                                               declarator_p);
4694             if (cp_parser_parse_definitely (parser))
4695               done = true;
4696           }
4697         /* In "N::S::~S", look in "N" as well.  */
4698         if (!done && scope && qualifying_scope)
4699           {
4700             cp_parser_parse_tentatively (parser);
4701             parser->scope = qualifying_scope;
4702             parser->object_scope = NULL_TREE;
4703             parser->qualifying_scope = NULL_TREE;
4704             type_decl
4705               = cp_parser_class_name (parser,
4706                                       /*typename_keyword_p=*/false,
4707                                       /*template_keyword_p=*/false,
4708                                       typename_type,
4709                                       /*check_dependency=*/false,
4710                                       /*class_head_p=*/false,
4711                                       declarator_p);
4712             if (cp_parser_parse_definitely (parser))
4713               done = true;
4714           }
4715         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4716         else if (!done && object_scope)
4717           {
4718             cp_parser_parse_tentatively (parser);
4719             parser->scope = object_scope;
4720             parser->object_scope = NULL_TREE;
4721             parser->qualifying_scope = NULL_TREE;
4722             type_decl
4723               = cp_parser_class_name (parser,
4724                                       /*typename_keyword_p=*/false,
4725                                       /*template_keyword_p=*/false,
4726                                       typename_type,
4727                                       /*check_dependency=*/false,
4728                                       /*class_head_p=*/false,
4729                                       declarator_p);
4730             if (cp_parser_parse_definitely (parser))
4731               done = true;
4732           }
4733         /* Look in the surrounding context.  */
4734         if (!done)
4735           {
4736             parser->scope = NULL_TREE;
4737             parser->object_scope = NULL_TREE;
4738             parser->qualifying_scope = NULL_TREE;
4739             if (processing_template_decl)
4740               cp_parser_parse_tentatively (parser);
4741             type_decl
4742               = cp_parser_class_name (parser,
4743                                       /*typename_keyword_p=*/false,
4744                                       /*template_keyword_p=*/false,
4745                                       typename_type,
4746                                       /*check_dependency=*/false,
4747                                       /*class_head_p=*/false,
4748                                       declarator_p);
4749             if (processing_template_decl
4750                 && ! cp_parser_parse_definitely (parser))
4751               {
4752                 /* We couldn't find a type with this name, so just accept
4753                    it and check for a match at instantiation time.  */
4754                 type_decl = cp_parser_identifier (parser);
4755                 if (type_decl != error_mark_node)
4756                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4757                 return type_decl;
4758               }
4759           }
4760         /* If an error occurred, assume that the name of the
4761            destructor is the same as the name of the qualifying
4762            class.  That allows us to keep parsing after running
4763            into ill-formed destructor names.  */
4764         if (type_decl == error_mark_node && scope)
4765           return build_nt (BIT_NOT_EXPR, scope);
4766         else if (type_decl == error_mark_node)
4767           return error_mark_node;
4768
4769         /* Check that destructor name and scope match.  */
4770         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4771           {
4772             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4773               error_at (token->location,
4774                         "declaration of %<~%T%> as member of %qT",
4775                         type_decl, scope);
4776             cp_parser_simulate_error (parser);
4777             return error_mark_node;
4778           }
4779
4780         /* [class.dtor]
4781
4782            A typedef-name that names a class shall not be used as the
4783            identifier in the declarator for a destructor declaration.  */
4784         if (declarator_p
4785             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4786             && !DECL_SELF_REFERENCE_P (type_decl)
4787             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4788           error_at (token->location,
4789                     "typedef-name %qD used as destructor declarator",
4790                     type_decl);
4791
4792         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4793       }
4794
4795     case CPP_KEYWORD:
4796       if (token->keyword == RID_OPERATOR)
4797         {
4798           tree id;
4799
4800           /* This could be a template-id, so we try that first.  */
4801           cp_parser_parse_tentatively (parser);
4802           /* Try a template-id.  */
4803           id = cp_parser_template_id (parser, template_keyword_p,
4804                                       /*check_dependency_p=*/true,
4805                                       declarator_p);
4806           /* If that worked, we're done.  */
4807           if (cp_parser_parse_definitely (parser))
4808             return id;
4809           /* We still don't know whether we're looking at an
4810              operator-function-id or a conversion-function-id.  */
4811           cp_parser_parse_tentatively (parser);
4812           /* Try an operator-function-id.  */
4813           id = cp_parser_operator_function_id (parser);
4814           /* If that didn't work, try a conversion-function-id.  */
4815           if (!cp_parser_parse_definitely (parser))
4816             id = cp_parser_conversion_function_id (parser);
4817           else if (UDLIT_OPER_P (id))
4818             {
4819               /* 17.6.3.3.5  */
4820               const char *name = UDLIT_OP_SUFFIX (id);
4821               if (name[0] != '_' && !in_system_header)
4822                 warning (0, "literal operator suffixes not preceded by %<_%>"
4823                             " are reserved for future standardization");
4824             }
4825
4826           return id;
4827         }
4828       /* Fall through.  */
4829
4830     default:
4831       if (optional_p)
4832         return NULL_TREE;
4833       cp_parser_error (parser, "expected unqualified-id");
4834       return error_mark_node;
4835     }
4836 }
4837
4838 /* Parse an (optional) nested-name-specifier.
4839
4840    nested-name-specifier: [C++98]
4841      class-or-namespace-name :: nested-name-specifier [opt]
4842      class-or-namespace-name :: template nested-name-specifier [opt]
4843
4844    nested-name-specifier: [C++0x]
4845      type-name ::
4846      namespace-name ::
4847      nested-name-specifier identifier ::
4848      nested-name-specifier template [opt] simple-template-id ::
4849
4850    PARSER->SCOPE should be set appropriately before this function is
4851    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4852    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4853    in name lookups.
4854
4855    Sets PARSER->SCOPE to the class (TYPE) or namespace
4856    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4857    it unchanged if there is no nested-name-specifier.  Returns the new
4858    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4859
4860    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4861    part of a declaration and/or decl-specifier.  */
4862
4863 static tree
4864 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4865                                      bool typename_keyword_p,
4866                                      bool check_dependency_p,
4867                                      bool type_p,
4868                                      bool is_declaration)
4869 {
4870   bool success = false;
4871   cp_token_position start = 0;
4872   cp_token *token;
4873
4874   /* Remember where the nested-name-specifier starts.  */
4875   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4876     {
4877       start = cp_lexer_token_position (parser->lexer, false);
4878       push_deferring_access_checks (dk_deferred);
4879     }
4880
4881   while (true)
4882     {
4883       tree new_scope;
4884       tree old_scope;
4885       tree saved_qualifying_scope;
4886       bool template_keyword_p;
4887
4888       /* Spot cases that cannot be the beginning of a
4889          nested-name-specifier.  */
4890       token = cp_lexer_peek_token (parser->lexer);
4891
4892       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4893          the already parsed nested-name-specifier.  */
4894       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4895         {
4896           /* Grab the nested-name-specifier and continue the loop.  */
4897           cp_parser_pre_parsed_nested_name_specifier (parser);
4898           /* If we originally encountered this nested-name-specifier
4899              with IS_DECLARATION set to false, we will not have
4900              resolved TYPENAME_TYPEs, so we must do so here.  */
4901           if (is_declaration
4902               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4903             {
4904               new_scope = resolve_typename_type (parser->scope,
4905                                                  /*only_current_p=*/false);
4906               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4907                 parser->scope = new_scope;
4908             }
4909           success = true;
4910           continue;
4911         }
4912
4913       /* Spot cases that cannot be the beginning of a
4914          nested-name-specifier.  On the second and subsequent times
4915          through the loop, we look for the `template' keyword.  */
4916       if (success && token->keyword == RID_TEMPLATE)
4917         ;
4918       /* A template-id can start a nested-name-specifier.  */
4919       else if (token->type == CPP_TEMPLATE_ID)
4920         ;
4921       /* DR 743: decltype can be used in a nested-name-specifier.  */
4922       else if (token_is_decltype (token))
4923         ;
4924       else
4925         {
4926           /* If the next token is not an identifier, then it is
4927              definitely not a type-name or namespace-name.  */
4928           if (token->type != CPP_NAME)
4929             break;
4930           /* If the following token is neither a `<' (to begin a
4931              template-id), nor a `::', then we are not looking at a
4932              nested-name-specifier.  */
4933           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4934
4935           if (token->type == CPP_COLON
4936               && parser->colon_corrects_to_scope_p
4937               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4938             {
4939               error_at (token->location,
4940                         "found %<:%> in nested-name-specifier, expected %<::%>");
4941               token->type = CPP_SCOPE;
4942             }
4943
4944           if (token->type != CPP_SCOPE
4945               && !cp_parser_nth_token_starts_template_argument_list_p
4946                   (parser, 2))
4947             break;
4948         }
4949
4950       /* The nested-name-specifier is optional, so we parse
4951          tentatively.  */
4952       cp_parser_parse_tentatively (parser);
4953
4954       /* Look for the optional `template' keyword, if this isn't the
4955          first time through the loop.  */
4956       if (success)
4957         template_keyword_p = cp_parser_optional_template_keyword (parser);
4958       else
4959         template_keyword_p = false;
4960
4961       /* Save the old scope since the name lookup we are about to do
4962          might destroy it.  */
4963       old_scope = parser->scope;
4964       saved_qualifying_scope = parser->qualifying_scope;
4965       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4966          look up names in "X<T>::I" in order to determine that "Y" is
4967          a template.  So, if we have a typename at this point, we make
4968          an effort to look through it.  */
4969       if (is_declaration
4970           && !typename_keyword_p
4971           && parser->scope
4972           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4973         parser->scope = resolve_typename_type (parser->scope,
4974                                                /*only_current_p=*/false);
4975       /* Parse the qualifying entity.  */
4976       new_scope
4977         = cp_parser_qualifying_entity (parser,
4978                                        typename_keyword_p,
4979                                        template_keyword_p,
4980                                        check_dependency_p,
4981                                        type_p,
4982                                        is_declaration);
4983       /* Look for the `::' token.  */
4984       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4985
4986       /* If we found what we wanted, we keep going; otherwise, we're
4987          done.  */
4988       if (!cp_parser_parse_definitely (parser))
4989         {
4990           bool error_p = false;
4991
4992           /* Restore the OLD_SCOPE since it was valid before the
4993              failed attempt at finding the last
4994              class-or-namespace-name.  */
4995           parser->scope = old_scope;
4996           parser->qualifying_scope = saved_qualifying_scope;
4997
4998           /* If the next token is a decltype, and the one after that is a
4999              `::', then the decltype has failed to resolve to a class or
5000              enumeration type.  Give this error even when parsing
5001              tentatively since it can't possibly be valid--and we're going
5002              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
5003              won't get another chance.*/
5004           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
5005               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5006                   == CPP_SCOPE))
5007             {
5008               token = cp_lexer_consume_token (parser->lexer);
5009               error_at (token->location, "decltype evaluates to %qT, "
5010                         "which is not a class or enumeration type",
5011                         token->u.value);
5012               parser->scope = error_mark_node;
5013               error_p = true;
5014               /* As below.  */
5015               success = true;
5016               cp_lexer_consume_token (parser->lexer);
5017             }
5018
5019           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
5020             break;
5021           /* If the next token is an identifier, and the one after
5022              that is a `::', then any valid interpretation would have
5023              found a class-or-namespace-name.  */
5024           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
5025                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
5026                      == CPP_SCOPE)
5027                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
5028                      != CPP_COMPL))
5029             {
5030               token = cp_lexer_consume_token (parser->lexer);
5031               if (!error_p)
5032                 {
5033                   if (!token->ambiguous_p)
5034                     {
5035                       tree decl;
5036                       tree ambiguous_decls;
5037
5038                       decl = cp_parser_lookup_name (parser, token->u.value,
5039                                                     none_type,
5040                                                     /*is_template=*/false,
5041                                                     /*is_namespace=*/false,
5042                                                     /*check_dependency=*/true,
5043                                                     &ambiguous_decls,
5044                                                     token->location);
5045                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5046                         error_at (token->location,
5047                                   "%qD used without template parameters",
5048                                   decl);
5049                       else if (ambiguous_decls)
5050                         {
5051                           error_at (token->location,
5052                                     "reference to %qD is ambiguous",
5053                                     token->u.value);
5054                           print_candidates (ambiguous_decls);
5055                           decl = error_mark_node;
5056                         }
5057                       else
5058                         {
5059                           if (cxx_dialect != cxx98)
5060                             cp_parser_name_lookup_error
5061                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5062                              token->location);
5063                           else
5064                             cp_parser_name_lookup_error
5065                             (parser, token->u.value, decl, NLE_CXX98,
5066                              token->location);
5067                         }
5068                     }
5069                   parser->scope = error_mark_node;
5070                   error_p = true;
5071                   /* Treat this as a successful nested-name-specifier
5072                      due to:
5073
5074                      [basic.lookup.qual]
5075
5076                      If the name found is not a class-name (clause
5077                      _class_) or namespace-name (_namespace.def_), the
5078                      program is ill-formed.  */
5079                   success = true;
5080                 }
5081               cp_lexer_consume_token (parser->lexer);
5082             }
5083           break;
5084         }
5085       /* We've found one valid nested-name-specifier.  */
5086       success = true;
5087       /* Name lookup always gives us a DECL.  */
5088       if (TREE_CODE (new_scope) == TYPE_DECL)
5089         new_scope = TREE_TYPE (new_scope);
5090       /* Uses of "template" must be followed by actual templates.  */
5091       if (template_keyword_p
5092           && !(CLASS_TYPE_P (new_scope)
5093                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5094                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5095                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5096           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5097                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5098                    == TEMPLATE_ID_EXPR)))
5099         permerror (input_location, TYPE_P (new_scope)
5100                    ? G_("%qT is not a template")
5101                    : G_("%qD is not a template"),
5102                    new_scope);
5103       /* If it is a class scope, try to complete it; we are about to
5104          be looking up names inside the class.  */
5105       if (TYPE_P (new_scope)
5106           /* Since checking types for dependency can be expensive,
5107              avoid doing it if the type is already complete.  */
5108           && !COMPLETE_TYPE_P (new_scope)
5109           /* Do not try to complete dependent types.  */
5110           && !dependent_type_p (new_scope))
5111         {
5112           new_scope = complete_type (new_scope);
5113           /* If it is a typedef to current class, use the current
5114              class instead, as the typedef won't have any names inside
5115              it yet.  */
5116           if (!COMPLETE_TYPE_P (new_scope)
5117               && currently_open_class (new_scope))
5118             new_scope = TYPE_MAIN_VARIANT (new_scope);
5119         }
5120       /* Make sure we look in the right scope the next time through
5121          the loop.  */
5122       parser->scope = new_scope;
5123     }
5124
5125   /* If parsing tentatively, replace the sequence of tokens that makes
5126      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5127      token.  That way, should we re-parse the token stream, we will
5128      not have to repeat the effort required to do the parse, nor will
5129      we issue duplicate error messages.  */
5130   if (success && start)
5131     {
5132       cp_token *token;
5133
5134       token = cp_lexer_token_at (parser->lexer, start);
5135       /* Reset the contents of the START token.  */
5136       token->type = CPP_NESTED_NAME_SPECIFIER;
5137       /* Retrieve any deferred checks.  Do not pop this access checks yet
5138          so the memory will not be reclaimed during token replacing below.  */
5139       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5140       token->u.tree_check_value->value = parser->scope;
5141       token->u.tree_check_value->checks = get_deferred_access_checks ();
5142       token->u.tree_check_value->qualifying_scope =
5143         parser->qualifying_scope;
5144       token->keyword = RID_MAX;
5145
5146       /* Purge all subsequent tokens.  */
5147       cp_lexer_purge_tokens_after (parser->lexer, start);
5148     }
5149
5150   if (start)
5151     pop_to_parent_deferring_access_checks ();
5152
5153   return success ? parser->scope : NULL_TREE;
5154 }
5155
5156 /* Parse a nested-name-specifier.  See
5157    cp_parser_nested_name_specifier_opt for details.  This function
5158    behaves identically, except that it will an issue an error if no
5159    nested-name-specifier is present.  */
5160
5161 static tree
5162 cp_parser_nested_name_specifier (cp_parser *parser,
5163                                  bool typename_keyword_p,
5164                                  bool check_dependency_p,
5165                                  bool type_p,
5166                                  bool is_declaration)
5167 {
5168   tree scope;
5169
5170   /* Look for the nested-name-specifier.  */
5171   scope = cp_parser_nested_name_specifier_opt (parser,
5172                                                typename_keyword_p,
5173                                                check_dependency_p,
5174                                                type_p,
5175                                                is_declaration);
5176   /* If it was not present, issue an error message.  */
5177   if (!scope)
5178     {
5179       cp_parser_error (parser, "expected nested-name-specifier");
5180       parser->scope = NULL_TREE;
5181     }
5182
5183   return scope;
5184 }
5185
5186 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5187    this is either a class-name or a namespace-name (which corresponds
5188    to the class-or-namespace-name production in the grammar). For
5189    C++0x, it can also be a type-name that refers to an enumeration
5190    type or a simple-template-id.
5191
5192    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5193    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5194    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5195    TYPE_P is TRUE iff the next name should be taken as a class-name,
5196    even the same name is declared to be another entity in the same
5197    scope.
5198
5199    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5200    specified by the class-or-namespace-name.  If neither is found the
5201    ERROR_MARK_NODE is returned.  */
5202
5203 static tree
5204 cp_parser_qualifying_entity (cp_parser *parser,
5205                              bool typename_keyword_p,
5206                              bool template_keyword_p,
5207                              bool check_dependency_p,
5208                              bool type_p,
5209                              bool is_declaration)
5210 {
5211   tree saved_scope;
5212   tree saved_qualifying_scope;
5213   tree saved_object_scope;
5214   tree scope;
5215   bool only_class_p;
5216   bool successful_parse_p;
5217
5218   /* DR 743: decltype can appear in a nested-name-specifier.  */
5219   if (cp_lexer_next_token_is_decltype (parser->lexer))
5220     {
5221       scope = cp_parser_decltype (parser);
5222       if (TREE_CODE (scope) != ENUMERAL_TYPE
5223           && !MAYBE_CLASS_TYPE_P (scope))
5224         {
5225           cp_parser_simulate_error (parser);
5226           return error_mark_node;
5227         }
5228       if (TYPE_NAME (scope))
5229         scope = TYPE_NAME (scope);
5230       return scope;
5231     }
5232
5233   /* Before we try to parse the class-name, we must save away the
5234      current PARSER->SCOPE since cp_parser_class_name will destroy
5235      it.  */
5236   saved_scope = parser->scope;
5237   saved_qualifying_scope = parser->qualifying_scope;
5238   saved_object_scope = parser->object_scope;
5239   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5240      there is no need to look for a namespace-name.  */
5241   only_class_p = template_keyword_p 
5242     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5243   if (!only_class_p)
5244     cp_parser_parse_tentatively (parser);
5245   scope = cp_parser_class_name (parser,
5246                                 typename_keyword_p,
5247                                 template_keyword_p,
5248                                 type_p ? class_type : none_type,
5249                                 check_dependency_p,
5250                                 /*class_head_p=*/false,
5251                                 is_declaration);
5252   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5253   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5254   if (!only_class_p 
5255       && cxx_dialect != cxx98
5256       && !successful_parse_p)
5257     {
5258       /* Restore the saved scope.  */
5259       parser->scope = saved_scope;
5260       parser->qualifying_scope = saved_qualifying_scope;
5261       parser->object_scope = saved_object_scope;
5262
5263       /* Parse tentatively.  */
5264       cp_parser_parse_tentatively (parser);
5265      
5266       /* Parse a type-name  */
5267       scope = cp_parser_type_name (parser);
5268
5269       /* "If the name found does not designate a namespace or a class,
5270          enumeration, or dependent type, the program is ill-formed."
5271
5272          We cover classes and dependent types above and namespaces below,
5273          so this code is only looking for enums.  */
5274       if (!scope || TREE_CODE (scope) != TYPE_DECL
5275           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5276         cp_parser_simulate_error (parser);
5277
5278       successful_parse_p = cp_parser_parse_definitely (parser);
5279     }
5280   /* If that didn't work, try for a namespace-name.  */
5281   if (!only_class_p && !successful_parse_p)
5282     {
5283       /* Restore the saved scope.  */
5284       parser->scope = saved_scope;
5285       parser->qualifying_scope = saved_qualifying_scope;
5286       parser->object_scope = saved_object_scope;
5287       /* If we are not looking at an identifier followed by the scope
5288          resolution operator, then this is not part of a
5289          nested-name-specifier.  (Note that this function is only used
5290          to parse the components of a nested-name-specifier.)  */
5291       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5292           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5293         return error_mark_node;
5294       scope = cp_parser_namespace_name (parser);
5295     }
5296
5297   return scope;
5298 }
5299
5300 /* Parse a postfix-expression.
5301
5302    postfix-expression:
5303      primary-expression
5304      postfix-expression [ expression ]
5305      postfix-expression ( expression-list [opt] )
5306      simple-type-specifier ( expression-list [opt] )
5307      typename :: [opt] nested-name-specifier identifier
5308        ( expression-list [opt] )
5309      typename :: [opt] nested-name-specifier template [opt] template-id
5310        ( expression-list [opt] )
5311      postfix-expression . template [opt] id-expression
5312      postfix-expression -> template [opt] id-expression
5313      postfix-expression . pseudo-destructor-name
5314      postfix-expression -> pseudo-destructor-name
5315      postfix-expression ++
5316      postfix-expression --
5317      dynamic_cast < type-id > ( expression )
5318      static_cast < type-id > ( expression )
5319      reinterpret_cast < type-id > ( expression )
5320      const_cast < type-id > ( expression )
5321      typeid ( expression )
5322      typeid ( type-id )
5323
5324    GNU Extension:
5325
5326    postfix-expression:
5327      ( type-id ) { initializer-list , [opt] }
5328
5329    This extension is a GNU version of the C99 compound-literal
5330    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5331    but they are essentially the same concept.)
5332
5333    If ADDRESS_P is true, the postfix expression is the operand of the
5334    `&' operator.  CAST_P is true if this expression is the target of a
5335    cast.
5336
5337    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5338    class member access expressions [expr.ref].
5339
5340    Returns a representation of the expression.  */
5341
5342 static tree
5343 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5344                               bool member_access_only_p,
5345                               cp_id_kind * pidk_return)
5346 {
5347   cp_token *token;
5348   enum rid keyword;
5349   cp_id_kind idk = CP_ID_KIND_NONE;
5350   tree postfix_expression = NULL_TREE;
5351   bool is_member_access = false;
5352
5353   /* Peek at the next token.  */
5354   token = cp_lexer_peek_token (parser->lexer);
5355   /* Some of the productions are determined by keywords.  */
5356   keyword = token->keyword;
5357   switch (keyword)
5358     {
5359     case RID_DYNCAST:
5360     case RID_STATCAST:
5361     case RID_REINTCAST:
5362     case RID_CONSTCAST:
5363       {
5364         tree type;
5365         tree expression;
5366         const char *saved_message;
5367
5368         /* All of these can be handled in the same way from the point
5369            of view of parsing.  Begin by consuming the token
5370            identifying the cast.  */
5371         cp_lexer_consume_token (parser->lexer);
5372
5373         /* New types cannot be defined in the cast.  */
5374         saved_message = parser->type_definition_forbidden_message;
5375         parser->type_definition_forbidden_message
5376           = G_("types may not be defined in casts");
5377
5378         /* Look for the opening `<'.  */
5379         cp_parser_require (parser, CPP_LESS, RT_LESS);
5380         /* Parse the type to which we are casting.  */
5381         type = cp_parser_type_id (parser);
5382         /* Look for the closing `>'.  */
5383         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5384         /* Restore the old message.  */
5385         parser->type_definition_forbidden_message = saved_message;
5386
5387         /* And the expression which is being cast.  */
5388         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5389         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5390         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5391
5392         /* Only type conversions to integral or enumeration types
5393            can be used in constant-expressions.  */
5394         if (!cast_valid_in_integral_constant_expression_p (type)
5395             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5396           return error_mark_node;
5397
5398         switch (keyword)
5399           {
5400           case RID_DYNCAST:
5401             postfix_expression
5402               = build_dynamic_cast (type, expression, tf_warning_or_error);
5403             break;
5404           case RID_STATCAST:
5405             postfix_expression
5406               = build_static_cast (type, expression, tf_warning_or_error);
5407             break;
5408           case RID_REINTCAST:
5409             postfix_expression
5410               = build_reinterpret_cast (type, expression, 
5411                                         tf_warning_or_error);
5412             break;
5413           case RID_CONSTCAST:
5414             postfix_expression
5415               = build_const_cast (type, expression, tf_warning_or_error);
5416             break;
5417           default:
5418             gcc_unreachable ();
5419           }
5420       }
5421       break;
5422
5423     case RID_TYPEID:
5424       {
5425         tree type;
5426         const char *saved_message;
5427         bool saved_in_type_id_in_expr_p;
5428
5429         /* Consume the `typeid' token.  */
5430         cp_lexer_consume_token (parser->lexer);
5431         /* Look for the `(' token.  */
5432         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5433         /* Types cannot be defined in a `typeid' expression.  */
5434         saved_message = parser->type_definition_forbidden_message;
5435         parser->type_definition_forbidden_message
5436           = G_("types may not be defined in a %<typeid%> expression");
5437         /* We can't be sure yet whether we're looking at a type-id or an
5438            expression.  */
5439         cp_parser_parse_tentatively (parser);
5440         /* Try a type-id first.  */
5441         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5442         parser->in_type_id_in_expr_p = true;
5443         type = cp_parser_type_id (parser);
5444         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5445         /* Look for the `)' token.  Otherwise, we can't be sure that
5446            we're not looking at an expression: consider `typeid (int
5447            (3))', for example.  */
5448         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5449         /* If all went well, simply lookup the type-id.  */
5450         if (cp_parser_parse_definitely (parser))
5451           postfix_expression = get_typeid (type);
5452         /* Otherwise, fall back to the expression variant.  */
5453         else
5454           {
5455             tree expression;
5456
5457             /* Look for an expression.  */
5458             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5459             /* Compute its typeid.  */
5460             postfix_expression = build_typeid (expression);
5461             /* Look for the `)' token.  */
5462             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5463           }
5464         /* Restore the saved message.  */
5465         parser->type_definition_forbidden_message = saved_message;
5466         /* `typeid' may not appear in an integral constant expression.  */
5467         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5468           return error_mark_node;
5469       }
5470       break;
5471
5472     case RID_TYPENAME:
5473       {
5474         tree type;
5475         /* The syntax permitted here is the same permitted for an
5476            elaborated-type-specifier.  */
5477         type = cp_parser_elaborated_type_specifier (parser,
5478                                                     /*is_friend=*/false,
5479                                                     /*is_declaration=*/false);
5480         postfix_expression = cp_parser_functional_cast (parser, type);
5481       }
5482       break;
5483
5484     default:
5485       {
5486         tree type;
5487
5488         /* If the next thing is a simple-type-specifier, we may be
5489            looking at a functional cast.  We could also be looking at
5490            an id-expression.  So, we try the functional cast, and if
5491            that doesn't work we fall back to the primary-expression.  */
5492         cp_parser_parse_tentatively (parser);
5493         /* Look for the simple-type-specifier.  */
5494         type = cp_parser_simple_type_specifier (parser,
5495                                                 /*decl_specs=*/NULL,
5496                                                 CP_PARSER_FLAGS_NONE);
5497         /* Parse the cast itself.  */
5498         if (!cp_parser_error_occurred (parser))
5499           postfix_expression
5500             = cp_parser_functional_cast (parser, type);
5501         /* If that worked, we're done.  */
5502         if (cp_parser_parse_definitely (parser))
5503           break;
5504
5505         /* If the functional-cast didn't work out, try a
5506            compound-literal.  */
5507         if (cp_parser_allow_gnu_extensions_p (parser)
5508             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5509           {
5510             VEC(constructor_elt,gc) *initializer_list = NULL;
5511             bool saved_in_type_id_in_expr_p;
5512
5513             cp_parser_parse_tentatively (parser);
5514             /* Consume the `('.  */
5515             cp_lexer_consume_token (parser->lexer);
5516             /* Parse the type.  */
5517             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5518             parser->in_type_id_in_expr_p = true;
5519             type = cp_parser_type_id (parser);
5520             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5521             /* Look for the `)'.  */
5522             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5523             /* Look for the `{'.  */
5524             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5525             /* If things aren't going well, there's no need to
5526                keep going.  */
5527             if (!cp_parser_error_occurred (parser))
5528               {
5529                 bool non_constant_p;
5530                 /* Parse the initializer-list.  */
5531                 initializer_list
5532                   = cp_parser_initializer_list (parser, &non_constant_p);
5533                 /* Allow a trailing `,'.  */
5534                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5535                   cp_lexer_consume_token (parser->lexer);
5536                 /* Look for the final `}'.  */
5537                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5538               }
5539             /* If that worked, we're definitely looking at a
5540                compound-literal expression.  */
5541             if (cp_parser_parse_definitely (parser))
5542               {
5543                 /* Warn the user that a compound literal is not
5544                    allowed in standard C++.  */
5545                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5546                 /* For simplicity, we disallow compound literals in
5547                    constant-expressions.  We could
5548                    allow compound literals of integer type, whose
5549                    initializer was a constant, in constant
5550                    expressions.  Permitting that usage, as a further
5551                    extension, would not change the meaning of any
5552                    currently accepted programs.  (Of course, as
5553                    compound literals are not part of ISO C++, the
5554                    standard has nothing to say.)  */
5555                 if (cp_parser_non_integral_constant_expression (parser,
5556                                                                 NIC_NCC))
5557                   {
5558                     postfix_expression = error_mark_node;
5559                     break;
5560                   }
5561                 /* Form the representation of the compound-literal.  */
5562                 postfix_expression
5563                   = (finish_compound_literal
5564                      (type, build_constructor (init_list_type_node,
5565                                                initializer_list),
5566                       tf_warning_or_error));
5567                 break;
5568               }
5569           }
5570
5571         /* It must be a primary-expression.  */
5572         postfix_expression
5573           = cp_parser_primary_expression (parser, address_p, cast_p,
5574                                           /*template_arg_p=*/false,
5575                                           &idk);
5576       }
5577       break;
5578     }
5579
5580   /* Keep looping until the postfix-expression is complete.  */
5581   while (true)
5582     {
5583       if (idk == CP_ID_KIND_UNQUALIFIED
5584           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5585           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5586         /* It is not a Koenig lookup function call.  */
5587         postfix_expression
5588           = unqualified_name_lookup_error (postfix_expression);
5589
5590       /* Peek at the next token.  */
5591       token = cp_lexer_peek_token (parser->lexer);
5592
5593       switch (token->type)
5594         {
5595         case CPP_OPEN_SQUARE:
5596           postfix_expression
5597             = cp_parser_postfix_open_square_expression (parser,
5598                                                         postfix_expression,
5599                                                         false);
5600           idk = CP_ID_KIND_NONE;
5601           is_member_access = false;
5602           break;
5603
5604         case CPP_OPEN_PAREN:
5605           /* postfix-expression ( expression-list [opt] ) */
5606           {
5607             bool koenig_p;
5608             bool is_builtin_constant_p;
5609             bool saved_integral_constant_expression_p = false;
5610             bool saved_non_integral_constant_expression_p = false;
5611             VEC(tree,gc) *args;
5612
5613             is_member_access = false;
5614
5615             is_builtin_constant_p
5616               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5617             if (is_builtin_constant_p)
5618               {
5619                 /* The whole point of __builtin_constant_p is to allow
5620                    non-constant expressions to appear as arguments.  */
5621                 saved_integral_constant_expression_p
5622                   = parser->integral_constant_expression_p;
5623                 saved_non_integral_constant_expression_p
5624                   = parser->non_integral_constant_expression_p;
5625                 parser->integral_constant_expression_p = false;
5626               }
5627             args = (cp_parser_parenthesized_expression_list
5628                     (parser, non_attr,
5629                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5630                      /*non_constant_p=*/NULL));
5631             if (is_builtin_constant_p)
5632               {
5633                 parser->integral_constant_expression_p
5634                   = saved_integral_constant_expression_p;
5635                 parser->non_integral_constant_expression_p
5636                   = saved_non_integral_constant_expression_p;
5637               }
5638
5639             if (args == NULL)
5640               {
5641                 postfix_expression = error_mark_node;
5642                 break;
5643               }
5644
5645             /* Function calls are not permitted in
5646                constant-expressions.  */
5647             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5648                 && cp_parser_non_integral_constant_expression (parser,
5649                                                                NIC_FUNC_CALL))
5650               {
5651                 postfix_expression = error_mark_node;
5652                 release_tree_vector (args);
5653                 break;
5654               }
5655
5656             koenig_p = false;
5657             if (idk == CP_ID_KIND_UNQUALIFIED
5658                 || idk == CP_ID_KIND_TEMPLATE_ID)
5659               {
5660                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5661                   {
5662                     if (!VEC_empty (tree, args))
5663                       {
5664                         koenig_p = true;
5665                         if (!any_type_dependent_arguments_p (args))
5666                           postfix_expression
5667                             = perform_koenig_lookup (postfix_expression, args,
5668                                                      /*include_std=*/false,
5669                                                      tf_warning_or_error);
5670                       }
5671                     else
5672                       postfix_expression
5673                         = unqualified_fn_lookup_error (postfix_expression);
5674                   }
5675                 /* We do not perform argument-dependent lookup if
5676                    normal lookup finds a non-function, in accordance
5677                    with the expected resolution of DR 218.  */
5678                 else if (!VEC_empty (tree, args)
5679                          && is_overloaded_fn (postfix_expression))
5680                   {
5681                     tree fn = get_first_fn (postfix_expression);
5682                     fn = STRIP_TEMPLATE (fn);
5683
5684                     /* Do not do argument dependent lookup if regular
5685                        lookup finds a member function or a block-scope
5686                        function declaration.  [basic.lookup.argdep]/3  */
5687                     if (!DECL_FUNCTION_MEMBER_P (fn)
5688                         && !DECL_LOCAL_FUNCTION_P (fn))
5689                       {
5690                         koenig_p = true;
5691                         if (!any_type_dependent_arguments_p (args))
5692                           postfix_expression
5693                             = perform_koenig_lookup (postfix_expression, args,
5694                                                      /*include_std=*/false,
5695                                                      tf_warning_or_error);
5696                       }
5697                   }
5698               }
5699
5700             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5701               {
5702                 tree instance = TREE_OPERAND (postfix_expression, 0);
5703                 tree fn = TREE_OPERAND (postfix_expression, 1);
5704
5705                 if (processing_template_decl
5706                     && (type_dependent_expression_p (instance)
5707                         || (!BASELINK_P (fn)
5708                             && TREE_CODE (fn) != FIELD_DECL)
5709                         || type_dependent_expression_p (fn)
5710                         || any_type_dependent_arguments_p (args)))
5711                   {
5712                     postfix_expression
5713                       = build_nt_call_vec (postfix_expression, args);
5714                     release_tree_vector (args);
5715                     break;
5716                   }
5717
5718                 if (BASELINK_P (fn))
5719                   {
5720                   postfix_expression
5721                     = (build_new_method_call
5722                        (instance, fn, &args, NULL_TREE,
5723                         (idk == CP_ID_KIND_QUALIFIED
5724                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5725                          : LOOKUP_NORMAL),
5726                         /*fn_p=*/NULL,
5727                         tf_warning_or_error));
5728                   }
5729                 else
5730                   postfix_expression
5731                     = finish_call_expr (postfix_expression, &args,
5732                                         /*disallow_virtual=*/false,
5733                                         /*koenig_p=*/false,
5734                                         tf_warning_or_error);
5735               }
5736             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5737                      || TREE_CODE (postfix_expression) == MEMBER_REF
5738                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5739               postfix_expression = (build_offset_ref_call_from_tree
5740                                     (postfix_expression, &args));
5741             else if (idk == CP_ID_KIND_QUALIFIED)
5742               /* A call to a static class member, or a namespace-scope
5743                  function.  */
5744               postfix_expression
5745                 = finish_call_expr (postfix_expression, &args,
5746                                     /*disallow_virtual=*/true,
5747                                     koenig_p,
5748                                     tf_warning_or_error);
5749             else
5750               /* All other function calls.  */
5751               postfix_expression
5752                 = finish_call_expr (postfix_expression, &args,
5753                                     /*disallow_virtual=*/false,
5754                                     koenig_p,
5755                                     tf_warning_or_error);
5756
5757             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5758             idk = CP_ID_KIND_NONE;
5759
5760             release_tree_vector (args);
5761           }
5762           break;
5763
5764         case CPP_DOT:
5765         case CPP_DEREF:
5766           /* postfix-expression . template [opt] id-expression
5767              postfix-expression . pseudo-destructor-name
5768              postfix-expression -> template [opt] id-expression
5769              postfix-expression -> pseudo-destructor-name */
5770
5771           /* Consume the `.' or `->' operator.  */
5772           cp_lexer_consume_token (parser->lexer);
5773
5774           postfix_expression
5775             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5776                                                       postfix_expression,
5777                                                       false, &idk,
5778                                                       token->location);
5779
5780           is_member_access = true;
5781           break;
5782
5783         case CPP_PLUS_PLUS:
5784           /* postfix-expression ++  */
5785           /* Consume the `++' token.  */
5786           cp_lexer_consume_token (parser->lexer);
5787           /* Generate a representation for the complete expression.  */
5788           postfix_expression
5789             = finish_increment_expr (postfix_expression,
5790                                      POSTINCREMENT_EXPR);
5791           /* Increments may not appear in constant-expressions.  */
5792           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5793             postfix_expression = error_mark_node;
5794           idk = CP_ID_KIND_NONE;
5795           is_member_access = false;
5796           break;
5797
5798         case CPP_MINUS_MINUS:
5799           /* postfix-expression -- */
5800           /* Consume the `--' token.  */
5801           cp_lexer_consume_token (parser->lexer);
5802           /* Generate a representation for the complete expression.  */
5803           postfix_expression
5804             = finish_increment_expr (postfix_expression,
5805                                      POSTDECREMENT_EXPR);
5806           /* Decrements may not appear in constant-expressions.  */
5807           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5808             postfix_expression = error_mark_node;
5809           idk = CP_ID_KIND_NONE;
5810           is_member_access = false;
5811           break;
5812
5813         default:
5814           if (pidk_return != NULL)
5815             * pidk_return = idk;
5816           if (member_access_only_p)
5817             return is_member_access? postfix_expression : error_mark_node;
5818           else
5819             return postfix_expression;
5820         }
5821     }
5822
5823   /* We should never get here.  */
5824   gcc_unreachable ();
5825   return error_mark_node;
5826 }
5827
5828 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5829    by cp_parser_builtin_offsetof.  We're looking for
5830
5831      postfix-expression [ expression ]
5832
5833    FOR_OFFSETOF is set if we're being called in that context, which
5834    changes how we deal with integer constant expressions.  */
5835
5836 static tree
5837 cp_parser_postfix_open_square_expression (cp_parser *parser,
5838                                           tree postfix_expression,
5839                                           bool for_offsetof)
5840 {
5841   tree index;
5842
5843   /* Consume the `[' token.  */
5844   cp_lexer_consume_token (parser->lexer);
5845
5846   /* Parse the index expression.  */
5847   /* ??? For offsetof, there is a question of what to allow here.  If
5848      offsetof is not being used in an integral constant expression context,
5849      then we *could* get the right answer by computing the value at runtime.
5850      If we are in an integral constant expression context, then we might
5851      could accept any constant expression; hard to say without analysis.
5852      Rather than open the barn door too wide right away, allow only integer
5853      constant expressions here.  */
5854   if (for_offsetof)
5855     index = cp_parser_constant_expression (parser, false, NULL);
5856   else
5857     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5858
5859   /* Look for the closing `]'.  */
5860   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5861
5862   /* Build the ARRAY_REF.  */
5863   postfix_expression = grok_array_decl (postfix_expression, index);
5864
5865   /* When not doing offsetof, array references are not permitted in
5866      constant-expressions.  */
5867   if (!for_offsetof
5868       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5869     postfix_expression = error_mark_node;
5870
5871   return postfix_expression;
5872 }
5873
5874 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5875    by cp_parser_builtin_offsetof.  We're looking for
5876
5877      postfix-expression . template [opt] id-expression
5878      postfix-expression . pseudo-destructor-name
5879      postfix-expression -> template [opt] id-expression
5880      postfix-expression -> pseudo-destructor-name
5881
5882    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5883    limits what of the above we'll actually accept, but nevermind.
5884    TOKEN_TYPE is the "." or "->" token, which will already have been
5885    removed from the stream.  */
5886
5887 static tree
5888 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5889                                         enum cpp_ttype token_type,
5890                                         tree postfix_expression,
5891                                         bool for_offsetof, cp_id_kind *idk,
5892                                         location_t location)
5893 {
5894   tree name;
5895   bool dependent_p;
5896   bool pseudo_destructor_p;
5897   tree scope = NULL_TREE;
5898
5899   /* If this is a `->' operator, dereference the pointer.  */
5900   if (token_type == CPP_DEREF)
5901     postfix_expression = build_x_arrow (postfix_expression);
5902   /* Check to see whether or not the expression is type-dependent.  */
5903   dependent_p = type_dependent_expression_p (postfix_expression);
5904   /* The identifier following the `->' or `.' is not qualified.  */
5905   parser->scope = NULL_TREE;
5906   parser->qualifying_scope = NULL_TREE;
5907   parser->object_scope = NULL_TREE;
5908   *idk = CP_ID_KIND_NONE;
5909
5910   /* Enter the scope corresponding to the type of the object
5911      given by the POSTFIX_EXPRESSION.  */
5912   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5913     {
5914       scope = TREE_TYPE (postfix_expression);
5915       /* According to the standard, no expression should ever have
5916          reference type.  Unfortunately, we do not currently match
5917          the standard in this respect in that our internal representation
5918          of an expression may have reference type even when the standard
5919          says it does not.  Therefore, we have to manually obtain the
5920          underlying type here.  */
5921       scope = non_reference (scope);
5922       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5923       if (scope == unknown_type_node)
5924         {
5925           error_at (location, "%qE does not have class type",
5926                     postfix_expression);
5927           scope = NULL_TREE;
5928         }
5929       /* Unlike the object expression in other contexts, *this is not
5930          required to be of complete type for purposes of class member
5931          access (5.2.5) outside the member function body.  */
5932       else if (scope != current_class_ref
5933                && !(processing_template_decl && scope == current_class_type))
5934         scope = complete_type_or_else (scope, NULL_TREE);
5935       /* Let the name lookup machinery know that we are processing a
5936          class member access expression.  */
5937       parser->context->object_type = scope;
5938       /* If something went wrong, we want to be able to discern that case,
5939          as opposed to the case where there was no SCOPE due to the type
5940          of expression being dependent.  */
5941       if (!scope)
5942         scope = error_mark_node;
5943       /* If the SCOPE was erroneous, make the various semantic analysis
5944          functions exit quickly -- and without issuing additional error
5945          messages.  */
5946       if (scope == error_mark_node)
5947         postfix_expression = error_mark_node;
5948     }
5949
5950   /* Assume this expression is not a pseudo-destructor access.  */
5951   pseudo_destructor_p = false;
5952
5953   /* If the SCOPE is a scalar type, then, if this is a valid program,
5954      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5955      is type dependent, it can be pseudo-destructor-name or something else.
5956      Try to parse it as pseudo-destructor-name first.  */
5957   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5958     {
5959       tree s;
5960       tree type;
5961
5962       cp_parser_parse_tentatively (parser);
5963       /* Parse the pseudo-destructor-name.  */
5964       s = NULL_TREE;
5965       cp_parser_pseudo_destructor_name (parser, &s, &type);
5966       if (dependent_p
5967           && (cp_parser_error_occurred (parser)
5968               || TREE_CODE (type) != TYPE_DECL
5969               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5970         cp_parser_abort_tentative_parse (parser);
5971       else if (cp_parser_parse_definitely (parser))
5972         {
5973           pseudo_destructor_p = true;
5974           postfix_expression
5975             = finish_pseudo_destructor_expr (postfix_expression,
5976                                              s, TREE_TYPE (type));
5977         }
5978     }
5979
5980   if (!pseudo_destructor_p)
5981     {
5982       /* If the SCOPE is not a scalar type, we are looking at an
5983          ordinary class member access expression, rather than a
5984          pseudo-destructor-name.  */
5985       bool template_p;
5986       cp_token *token = cp_lexer_peek_token (parser->lexer);
5987       /* Parse the id-expression.  */
5988       name = (cp_parser_id_expression
5989               (parser,
5990                cp_parser_optional_template_keyword (parser),
5991                /*check_dependency_p=*/true,
5992                &template_p,
5993                /*declarator_p=*/false,
5994                /*optional_p=*/false));
5995       /* In general, build a SCOPE_REF if the member name is qualified.
5996          However, if the name was not dependent and has already been
5997          resolved; there is no need to build the SCOPE_REF.  For example;
5998
5999              struct X { void f(); };
6000              template <typename T> void f(T* t) { t->X::f(); }
6001
6002          Even though "t" is dependent, "X::f" is not and has been resolved
6003          to a BASELINK; there is no need to include scope information.  */
6004
6005       /* But we do need to remember that there was an explicit scope for
6006          virtual function calls.  */
6007       if (parser->scope)
6008         *idk = CP_ID_KIND_QUALIFIED;
6009
6010       /* If the name is a template-id that names a type, we will get a
6011          TYPE_DECL here.  That is invalid code.  */
6012       if (TREE_CODE (name) == TYPE_DECL)
6013         {
6014           error_at (token->location, "invalid use of %qD", name);
6015           postfix_expression = error_mark_node;
6016         }
6017       else
6018         {
6019           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
6020             {
6021               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
6022                 {
6023                   error_at (token->location, "%<%D::%D%> is not a class member",
6024                             parser->scope, name);
6025                   postfix_expression = error_mark_node;
6026                 }
6027               else
6028                 name = build_qualified_name (/*type=*/NULL_TREE,
6029                                              parser->scope,
6030                                              name,
6031                                              template_p);
6032               parser->scope = NULL_TREE;
6033               parser->qualifying_scope = NULL_TREE;
6034               parser->object_scope = NULL_TREE;
6035             }
6036           if (scope && name && BASELINK_P (name))
6037             adjust_result_of_qualified_name_lookup
6038               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6039           postfix_expression
6040             = finish_class_member_access_expr (postfix_expression, name,
6041                                                template_p, 
6042                                                tf_warning_or_error);
6043         }
6044     }
6045
6046   /* We no longer need to look up names in the scope of the object on
6047      the left-hand side of the `.' or `->' operator.  */
6048   parser->context->object_type = NULL_TREE;
6049
6050   /* Outside of offsetof, these operators may not appear in
6051      constant-expressions.  */
6052   if (!for_offsetof
6053       && (cp_parser_non_integral_constant_expression
6054           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6055     postfix_expression = error_mark_node;
6056
6057   return postfix_expression;
6058 }
6059
6060 /* Parse a parenthesized expression-list.
6061
6062    expression-list:
6063      assignment-expression
6064      expression-list, assignment-expression
6065
6066    attribute-list:
6067      expression-list
6068      identifier
6069      identifier, expression-list
6070
6071    CAST_P is true if this expression is the target of a cast.
6072
6073    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6074    argument pack.
6075
6076    Returns a vector of trees.  Each element is a representation of an
6077    assignment-expression.  NULL is returned if the ( and or ) are
6078    missing.  An empty, but allocated, vector is returned on no
6079    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6080    if we are parsing an attribute list for an attribute that wants a
6081    plain identifier argument, normal_attr for an attribute that wants
6082    an expression, or non_attr if we aren't parsing an attribute list.  If
6083    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6084    not all of the expressions in the list were constant.  */
6085
6086 static VEC(tree,gc) *
6087 cp_parser_parenthesized_expression_list (cp_parser* parser,
6088                                          int is_attribute_list,
6089                                          bool cast_p,
6090                                          bool allow_expansion_p,
6091                                          bool *non_constant_p)
6092 {
6093   VEC(tree,gc) *expression_list;
6094   bool fold_expr_p = is_attribute_list != non_attr;
6095   tree identifier = NULL_TREE;
6096   bool saved_greater_than_is_operator_p;
6097
6098   /* Assume all the expressions will be constant.  */
6099   if (non_constant_p)
6100     *non_constant_p = false;
6101
6102   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6103     return NULL;
6104
6105   expression_list = make_tree_vector ();
6106
6107   /* Within a parenthesized expression, a `>' token is always
6108      the greater-than operator.  */
6109   saved_greater_than_is_operator_p
6110     = parser->greater_than_is_operator_p;
6111   parser->greater_than_is_operator_p = true;
6112
6113   /* Consume expressions until there are no more.  */
6114   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6115     while (true)
6116       {
6117         tree expr;
6118
6119         /* At the beginning of attribute lists, check to see if the
6120            next token is an identifier.  */
6121         if (is_attribute_list == id_attr
6122             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6123           {
6124             cp_token *token;
6125
6126             /* Consume the identifier.  */
6127             token = cp_lexer_consume_token (parser->lexer);
6128             /* Save the identifier.  */
6129             identifier = token->u.value;
6130           }
6131         else
6132           {
6133             bool expr_non_constant_p;
6134
6135             /* Parse the next assignment-expression.  */
6136             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6137               {
6138                 /* A braced-init-list.  */
6139                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6140                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6141                 if (non_constant_p && expr_non_constant_p)
6142                   *non_constant_p = true;
6143               }
6144             else if (non_constant_p)
6145               {
6146                 expr = (cp_parser_constant_expression
6147                         (parser, /*allow_non_constant_p=*/true,
6148                          &expr_non_constant_p));
6149                 if (expr_non_constant_p)
6150                   *non_constant_p = true;
6151               }
6152             else
6153               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6154
6155             if (fold_expr_p)
6156               expr = fold_non_dependent_expr (expr);
6157
6158             /* If we have an ellipsis, then this is an expression
6159                expansion.  */
6160             if (allow_expansion_p
6161                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6162               {
6163                 /* Consume the `...'.  */
6164                 cp_lexer_consume_token (parser->lexer);
6165
6166                 /* Build the argument pack.  */
6167                 expr = make_pack_expansion (expr);
6168               }
6169
6170              /* Add it to the list.  We add error_mark_node
6171                 expressions to the list, so that we can still tell if
6172                 the correct form for a parenthesized expression-list
6173                 is found. That gives better errors.  */
6174             VEC_safe_push (tree, gc, expression_list, expr);
6175
6176             if (expr == error_mark_node)
6177               goto skip_comma;
6178           }
6179
6180         /* After the first item, attribute lists look the same as
6181            expression lists.  */
6182         is_attribute_list = non_attr;
6183
6184       get_comma:;
6185         /* If the next token isn't a `,', then we are done.  */
6186         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6187           break;
6188
6189         /* Otherwise, consume the `,' and keep going.  */
6190         cp_lexer_consume_token (parser->lexer);
6191       }
6192
6193   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6194     {
6195       int ending;
6196
6197     skip_comma:;
6198       /* We try and resync to an unnested comma, as that will give the
6199          user better diagnostics.  */
6200       ending = cp_parser_skip_to_closing_parenthesis (parser,
6201                                                       /*recovering=*/true,
6202                                                       /*or_comma=*/true,
6203                                                       /*consume_paren=*/true);
6204       if (ending < 0)
6205         goto get_comma;
6206       if (!ending)
6207         {
6208           parser->greater_than_is_operator_p
6209             = saved_greater_than_is_operator_p;
6210           return NULL;
6211         }
6212     }
6213
6214   parser->greater_than_is_operator_p
6215     = saved_greater_than_is_operator_p;
6216
6217   if (identifier)
6218     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6219
6220   return expression_list;
6221 }
6222
6223 /* Parse a pseudo-destructor-name.
6224
6225    pseudo-destructor-name:
6226      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6227      :: [opt] nested-name-specifier template template-id :: ~ type-name
6228      :: [opt] nested-name-specifier [opt] ~ type-name
6229
6230    If either of the first two productions is used, sets *SCOPE to the
6231    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6232    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6233    or ERROR_MARK_NODE if the parse fails.  */
6234
6235 static void
6236 cp_parser_pseudo_destructor_name (cp_parser* parser,
6237                                   tree* scope,
6238                                   tree* type)
6239 {
6240   bool nested_name_specifier_p;
6241
6242   /* Assume that things will not work out.  */
6243   *type = error_mark_node;
6244
6245   /* Look for the optional `::' operator.  */
6246   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6247   /* Look for the optional nested-name-specifier.  */
6248   nested_name_specifier_p
6249     = (cp_parser_nested_name_specifier_opt (parser,
6250                                             /*typename_keyword_p=*/false,
6251                                             /*check_dependency_p=*/true,
6252                                             /*type_p=*/false,
6253                                             /*is_declaration=*/false)
6254        != NULL_TREE);
6255   /* Now, if we saw a nested-name-specifier, we might be doing the
6256      second production.  */
6257   if (nested_name_specifier_p
6258       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6259     {
6260       /* Consume the `template' keyword.  */
6261       cp_lexer_consume_token (parser->lexer);
6262       /* Parse the template-id.  */
6263       cp_parser_template_id (parser,
6264                              /*template_keyword_p=*/true,
6265                              /*check_dependency_p=*/false,
6266                              /*is_declaration=*/true);
6267       /* Look for the `::' token.  */
6268       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6269     }
6270   /* If the next token is not a `~', then there might be some
6271      additional qualification.  */
6272   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6273     {
6274       /* At this point, we're looking for "type-name :: ~".  The type-name
6275          must not be a class-name, since this is a pseudo-destructor.  So,
6276          it must be either an enum-name, or a typedef-name -- both of which
6277          are just identifiers.  So, we peek ahead to check that the "::"
6278          and "~" tokens are present; if they are not, then we can avoid
6279          calling type_name.  */
6280       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6281           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6282           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6283         {
6284           cp_parser_error (parser, "non-scalar type");
6285           return;
6286         }
6287
6288       /* Look for the type-name.  */
6289       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6290       if (*scope == error_mark_node)
6291         return;
6292
6293       /* Look for the `::' token.  */
6294       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6295     }
6296   else
6297     *scope = NULL_TREE;
6298
6299   /* Look for the `~'.  */
6300   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6301
6302   /* Once we see the ~, this has to be a pseudo-destructor.  */
6303   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6304     cp_parser_commit_to_tentative_parse (parser);
6305
6306   /* Look for the type-name again.  We are not responsible for
6307      checking that it matches the first type-name.  */
6308   *type = cp_parser_nonclass_name (parser);
6309 }
6310
6311 /* Parse a unary-expression.
6312
6313    unary-expression:
6314      postfix-expression
6315      ++ cast-expression
6316      -- cast-expression
6317      unary-operator cast-expression
6318      sizeof unary-expression
6319      sizeof ( type-id )
6320      alignof ( type-id )  [C++0x]
6321      new-expression
6322      delete-expression
6323
6324    GNU Extensions:
6325
6326    unary-expression:
6327      __extension__ cast-expression
6328      __alignof__ unary-expression
6329      __alignof__ ( type-id )
6330      alignof unary-expression  [C++0x]
6331      __real__ cast-expression
6332      __imag__ cast-expression
6333      && identifier
6334
6335    ADDRESS_P is true iff the unary-expression is appearing as the
6336    operand of the `&' operator.   CAST_P is true if this expression is
6337    the target of a cast.
6338
6339    Returns a representation of the expression.  */
6340
6341 static tree
6342 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6343                             cp_id_kind * pidk)
6344 {
6345   cp_token *token;
6346   enum tree_code unary_operator;
6347
6348   /* Peek at the next token.  */
6349   token = cp_lexer_peek_token (parser->lexer);
6350   /* Some keywords give away the kind of expression.  */
6351   if (token->type == CPP_KEYWORD)
6352     {
6353       enum rid keyword = token->keyword;
6354
6355       switch (keyword)
6356         {
6357         case RID_ALIGNOF:
6358         case RID_SIZEOF:
6359           {
6360             tree operand;
6361             enum tree_code op;
6362
6363             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6364             /* Consume the token.  */
6365             cp_lexer_consume_token (parser->lexer);
6366             /* Parse the operand.  */
6367             operand = cp_parser_sizeof_operand (parser, keyword);
6368
6369             if (TYPE_P (operand))
6370               return cxx_sizeof_or_alignof_type (operand, op, true);
6371             else
6372               {
6373                 /* ISO C++ defines alignof only with types, not with
6374                    expressions. So pedwarn if alignof is used with a non-
6375                    type expression. However, __alignof__ is ok.  */
6376                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6377                   pedwarn (token->location, OPT_pedantic,
6378                            "ISO C++ does not allow %<alignof%> "
6379                            "with a non-type");
6380
6381                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6382               }
6383           }
6384
6385         case RID_NEW:
6386           return cp_parser_new_expression (parser);
6387
6388         case RID_DELETE:
6389           return cp_parser_delete_expression (parser);
6390
6391         case RID_EXTENSION:
6392           {
6393             /* The saved value of the PEDANTIC flag.  */
6394             int saved_pedantic;
6395             tree expr;
6396
6397             /* Save away the PEDANTIC flag.  */
6398             cp_parser_extension_opt (parser, &saved_pedantic);
6399             /* Parse the cast-expression.  */
6400             expr = cp_parser_simple_cast_expression (parser);
6401             /* Restore the PEDANTIC flag.  */
6402             pedantic = saved_pedantic;
6403
6404             return expr;
6405           }
6406
6407         case RID_REALPART:
6408         case RID_IMAGPART:
6409           {
6410             tree expression;
6411
6412             /* Consume the `__real__' or `__imag__' token.  */
6413             cp_lexer_consume_token (parser->lexer);
6414             /* Parse the cast-expression.  */
6415             expression = cp_parser_simple_cast_expression (parser);
6416             /* Create the complete representation.  */
6417             return build_x_unary_op ((keyword == RID_REALPART
6418                                       ? REALPART_EXPR : IMAGPART_EXPR),
6419                                      expression,
6420                                      tf_warning_or_error);
6421           }
6422           break;
6423
6424         case RID_TRANSACTION_ATOMIC:
6425         case RID_TRANSACTION_RELAXED:
6426           return cp_parser_transaction_expression (parser, keyword);
6427
6428         case RID_NOEXCEPT:
6429           {
6430             tree expr;
6431             const char *saved_message;
6432             bool saved_integral_constant_expression_p;
6433             bool saved_non_integral_constant_expression_p;
6434             bool saved_greater_than_is_operator_p;
6435
6436             cp_lexer_consume_token (parser->lexer);
6437             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6438
6439             saved_message = parser->type_definition_forbidden_message;
6440             parser->type_definition_forbidden_message
6441               = G_("types may not be defined in %<noexcept%> expressions");
6442
6443             saved_integral_constant_expression_p
6444               = parser->integral_constant_expression_p;
6445             saved_non_integral_constant_expression_p
6446               = parser->non_integral_constant_expression_p;
6447             parser->integral_constant_expression_p = false;
6448
6449             saved_greater_than_is_operator_p
6450               = parser->greater_than_is_operator_p;
6451             parser->greater_than_is_operator_p = true;
6452
6453             ++cp_unevaluated_operand;
6454             ++c_inhibit_evaluation_warnings;
6455             expr = cp_parser_expression (parser, false, NULL);
6456             --c_inhibit_evaluation_warnings;
6457             --cp_unevaluated_operand;
6458
6459             parser->greater_than_is_operator_p
6460               = saved_greater_than_is_operator_p;
6461
6462             parser->integral_constant_expression_p
6463               = saved_integral_constant_expression_p;
6464             parser->non_integral_constant_expression_p
6465               = saved_non_integral_constant_expression_p;
6466
6467             parser->type_definition_forbidden_message = saved_message;
6468
6469             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6470             return finish_noexcept_expr (expr, tf_warning_or_error);
6471           }
6472
6473         default:
6474           break;
6475         }
6476     }
6477
6478   /* Look for the `:: new' and `:: delete', which also signal the
6479      beginning of a new-expression, or delete-expression,
6480      respectively.  If the next token is `::', then it might be one of
6481      these.  */
6482   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6483     {
6484       enum rid keyword;
6485
6486       /* See if the token after the `::' is one of the keywords in
6487          which we're interested.  */
6488       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6489       /* If it's `new', we have a new-expression.  */
6490       if (keyword == RID_NEW)
6491         return cp_parser_new_expression (parser);
6492       /* Similarly, for `delete'.  */
6493       else if (keyword == RID_DELETE)
6494         return cp_parser_delete_expression (parser);
6495     }
6496
6497   /* Look for a unary operator.  */
6498   unary_operator = cp_parser_unary_operator (token);
6499   /* The `++' and `--' operators can be handled similarly, even though
6500      they are not technically unary-operators in the grammar.  */
6501   if (unary_operator == ERROR_MARK)
6502     {
6503       if (token->type == CPP_PLUS_PLUS)
6504         unary_operator = PREINCREMENT_EXPR;
6505       else if (token->type == CPP_MINUS_MINUS)
6506         unary_operator = PREDECREMENT_EXPR;
6507       /* Handle the GNU address-of-label extension.  */
6508       else if (cp_parser_allow_gnu_extensions_p (parser)
6509                && token->type == CPP_AND_AND)
6510         {
6511           tree identifier;
6512           tree expression;
6513           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6514
6515           /* Consume the '&&' token.  */
6516           cp_lexer_consume_token (parser->lexer);
6517           /* Look for the identifier.  */
6518           identifier = cp_parser_identifier (parser);
6519           /* Create an expression representing the address.  */
6520           expression = finish_label_address_expr (identifier, loc);
6521           if (cp_parser_non_integral_constant_expression (parser,
6522                                                           NIC_ADDR_LABEL))
6523             expression = error_mark_node;
6524           return expression;
6525         }
6526     }
6527   if (unary_operator != ERROR_MARK)
6528     {
6529       tree cast_expression;
6530       tree expression = error_mark_node;
6531       non_integral_constant non_constant_p = NIC_NONE;
6532
6533       /* Consume the operator token.  */
6534       token = cp_lexer_consume_token (parser->lexer);
6535       /* Parse the cast-expression.  */
6536       cast_expression
6537         = cp_parser_cast_expression (parser,
6538                                      unary_operator == ADDR_EXPR,
6539                                      /*cast_p=*/false, pidk);
6540       /* Now, build an appropriate representation.  */
6541       switch (unary_operator)
6542         {
6543         case INDIRECT_REF:
6544           non_constant_p = NIC_STAR;
6545           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6546                                              tf_warning_or_error);
6547           break;
6548
6549         case ADDR_EXPR:
6550            non_constant_p = NIC_ADDR;
6551           /* Fall through.  */
6552         case BIT_NOT_EXPR:
6553           expression = build_x_unary_op (unary_operator, cast_expression,
6554                                          tf_warning_or_error);
6555           break;
6556
6557         case PREINCREMENT_EXPR:
6558         case PREDECREMENT_EXPR:
6559           non_constant_p = unary_operator == PREINCREMENT_EXPR
6560                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6561           /* Fall through.  */
6562         case UNARY_PLUS_EXPR:
6563         case NEGATE_EXPR:
6564         case TRUTH_NOT_EXPR:
6565           expression = finish_unary_op_expr (unary_operator, cast_expression);
6566           break;
6567
6568         default:
6569           gcc_unreachable ();
6570         }
6571
6572       if (non_constant_p != NIC_NONE
6573           && cp_parser_non_integral_constant_expression (parser,
6574                                                          non_constant_p))
6575         expression = error_mark_node;
6576
6577       return expression;
6578     }
6579
6580   return cp_parser_postfix_expression (parser, address_p, cast_p,
6581                                        /*member_access_only_p=*/false,
6582                                        pidk);
6583 }
6584
6585 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6586    unary-operator, the corresponding tree code is returned.  */
6587
6588 static enum tree_code
6589 cp_parser_unary_operator (cp_token* token)
6590 {
6591   switch (token->type)
6592     {
6593     case CPP_MULT:
6594       return INDIRECT_REF;
6595
6596     case CPP_AND:
6597       return ADDR_EXPR;
6598
6599     case CPP_PLUS:
6600       return UNARY_PLUS_EXPR;
6601
6602     case CPP_MINUS:
6603       return NEGATE_EXPR;
6604
6605     case CPP_NOT:
6606       return TRUTH_NOT_EXPR;
6607
6608     case CPP_COMPL:
6609       return BIT_NOT_EXPR;
6610
6611     default:
6612       return ERROR_MARK;
6613     }
6614 }
6615
6616 /* Parse a new-expression.
6617
6618    new-expression:
6619      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6620      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6621
6622    Returns a representation of the expression.  */
6623
6624 static tree
6625 cp_parser_new_expression (cp_parser* parser)
6626 {
6627   bool global_scope_p;
6628   VEC(tree,gc) *placement;
6629   tree type;
6630   VEC(tree,gc) *initializer;
6631   tree nelts;
6632   tree ret;
6633
6634   /* Look for the optional `::' operator.  */
6635   global_scope_p
6636     = (cp_parser_global_scope_opt (parser,
6637                                    /*current_scope_valid_p=*/false)
6638        != NULL_TREE);
6639   /* Look for the `new' operator.  */
6640   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6641   /* There's no easy way to tell a new-placement from the
6642      `( type-id )' construct.  */
6643   cp_parser_parse_tentatively (parser);
6644   /* Look for a new-placement.  */
6645   placement = cp_parser_new_placement (parser);
6646   /* If that didn't work out, there's no new-placement.  */
6647   if (!cp_parser_parse_definitely (parser))
6648     {
6649       if (placement != NULL)
6650         release_tree_vector (placement);
6651       placement = NULL;
6652     }
6653
6654   /* If the next token is a `(', then we have a parenthesized
6655      type-id.  */
6656   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6657     {
6658       cp_token *token;
6659       /* Consume the `('.  */
6660       cp_lexer_consume_token (parser->lexer);
6661       /* Parse the type-id.  */
6662       type = cp_parser_type_id (parser);
6663       /* Look for the closing `)'.  */
6664       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6665       token = cp_lexer_peek_token (parser->lexer);
6666       /* There should not be a direct-new-declarator in this production,
6667          but GCC used to allowed this, so we check and emit a sensible error
6668          message for this case.  */
6669       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6670         {
6671           error_at (token->location,
6672                     "array bound forbidden after parenthesized type-id");
6673           inform (token->location, 
6674                   "try removing the parentheses around the type-id");
6675           cp_parser_direct_new_declarator (parser);
6676         }
6677       nelts = NULL_TREE;
6678     }
6679   /* Otherwise, there must be a new-type-id.  */
6680   else
6681     type = cp_parser_new_type_id (parser, &nelts);
6682
6683   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6684   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6685       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6686     initializer = cp_parser_new_initializer (parser);
6687   else
6688     initializer = NULL;
6689
6690   /* A new-expression may not appear in an integral constant
6691      expression.  */
6692   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6693     ret = error_mark_node;
6694   else
6695     {
6696       /* Create a representation of the new-expression.  */
6697       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6698                        tf_warning_or_error);
6699     }
6700
6701   if (placement != NULL)
6702     release_tree_vector (placement);
6703   if (initializer != NULL)
6704     release_tree_vector (initializer);
6705
6706   return ret;
6707 }
6708
6709 /* Parse a new-placement.
6710
6711    new-placement:
6712      ( expression-list )
6713
6714    Returns the same representation as for an expression-list.  */
6715
6716 static VEC(tree,gc) *
6717 cp_parser_new_placement (cp_parser* parser)
6718 {
6719   VEC(tree,gc) *expression_list;
6720
6721   /* Parse the expression-list.  */
6722   expression_list = (cp_parser_parenthesized_expression_list
6723                      (parser, non_attr, /*cast_p=*/false,
6724                       /*allow_expansion_p=*/true,
6725                       /*non_constant_p=*/NULL));
6726
6727   return expression_list;
6728 }
6729
6730 /* Parse a new-type-id.
6731
6732    new-type-id:
6733      type-specifier-seq new-declarator [opt]
6734
6735    Returns the TYPE allocated.  If the new-type-id indicates an array
6736    type, *NELTS is set to the number of elements in the last array
6737    bound; the TYPE will not include the last array bound.  */
6738
6739 static tree
6740 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6741 {
6742   cp_decl_specifier_seq type_specifier_seq;
6743   cp_declarator *new_declarator;
6744   cp_declarator *declarator;
6745   cp_declarator *outer_declarator;
6746   const char *saved_message;
6747   tree type;
6748
6749   /* The type-specifier sequence must not contain type definitions.
6750      (It cannot contain declarations of new types either, but if they
6751      are not definitions we will catch that because they are not
6752      complete.)  */
6753   saved_message = parser->type_definition_forbidden_message;
6754   parser->type_definition_forbidden_message
6755     = G_("types may not be defined in a new-type-id");
6756   /* Parse the type-specifier-seq.  */
6757   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6758                                 /*is_trailing_return=*/false,
6759                                 &type_specifier_seq);
6760   /* Restore the old message.  */
6761   parser->type_definition_forbidden_message = saved_message;
6762   /* Parse the new-declarator.  */
6763   new_declarator = cp_parser_new_declarator_opt (parser);
6764
6765   /* Determine the number of elements in the last array dimension, if
6766      any.  */
6767   *nelts = NULL_TREE;
6768   /* Skip down to the last array dimension.  */
6769   declarator = new_declarator;
6770   outer_declarator = NULL;
6771   while (declarator && (declarator->kind == cdk_pointer
6772                         || declarator->kind == cdk_ptrmem))
6773     {
6774       outer_declarator = declarator;
6775       declarator = declarator->declarator;
6776     }
6777   while (declarator
6778          && declarator->kind == cdk_array
6779          && declarator->declarator
6780          && declarator->declarator->kind == cdk_array)
6781     {
6782       outer_declarator = declarator;
6783       declarator = declarator->declarator;
6784     }
6785
6786   if (declarator && declarator->kind == cdk_array)
6787     {
6788       *nelts = declarator->u.array.bounds;
6789       if (*nelts == error_mark_node)
6790         *nelts = integer_one_node;
6791
6792       if (outer_declarator)
6793         outer_declarator->declarator = declarator->declarator;
6794       else
6795         new_declarator = NULL;
6796     }
6797
6798   type = groktypename (&type_specifier_seq, new_declarator, false);
6799   return type;
6800 }
6801
6802 /* Parse an (optional) new-declarator.
6803
6804    new-declarator:
6805      ptr-operator new-declarator [opt]
6806      direct-new-declarator
6807
6808    Returns the declarator.  */
6809
6810 static cp_declarator *
6811 cp_parser_new_declarator_opt (cp_parser* parser)
6812 {
6813   enum tree_code code;
6814   tree type;
6815   cp_cv_quals cv_quals;
6816
6817   /* We don't know if there's a ptr-operator next, or not.  */
6818   cp_parser_parse_tentatively (parser);
6819   /* Look for a ptr-operator.  */
6820   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6821   /* If that worked, look for more new-declarators.  */
6822   if (cp_parser_parse_definitely (parser))
6823     {
6824       cp_declarator *declarator;
6825
6826       /* Parse another optional declarator.  */
6827       declarator = cp_parser_new_declarator_opt (parser);
6828
6829       return cp_parser_make_indirect_declarator
6830         (code, type, cv_quals, declarator);
6831     }
6832
6833   /* If the next token is a `[', there is a direct-new-declarator.  */
6834   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6835     return cp_parser_direct_new_declarator (parser);
6836
6837   return NULL;
6838 }
6839
6840 /* Parse a direct-new-declarator.
6841
6842    direct-new-declarator:
6843      [ expression ]
6844      direct-new-declarator [constant-expression]
6845
6846    */
6847
6848 static cp_declarator *
6849 cp_parser_direct_new_declarator (cp_parser* parser)
6850 {
6851   cp_declarator *declarator = NULL;
6852
6853   while (true)
6854     {
6855       tree expression;
6856
6857       /* Look for the opening `['.  */
6858       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6859       /* The first expression is not required to be constant.  */
6860       if (!declarator)
6861         {
6862           cp_token *token = cp_lexer_peek_token (parser->lexer);
6863           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6864           /* The standard requires that the expression have integral
6865              type.  DR 74 adds enumeration types.  We believe that the
6866              real intent is that these expressions be handled like the
6867              expression in a `switch' condition, which also allows
6868              classes with a single conversion to integral or
6869              enumeration type.  */
6870           if (!processing_template_decl)
6871             {
6872               expression
6873                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6874                                               expression,
6875                                               /*complain=*/true);
6876               if (!expression)
6877                 {
6878                   error_at (token->location,
6879                             "expression in new-declarator must have integral "
6880                             "or enumeration type");
6881                   expression = error_mark_node;
6882                 }
6883             }
6884         }
6885       /* But all the other expressions must be.  */
6886       else
6887         expression
6888           = cp_parser_constant_expression (parser,
6889                                            /*allow_non_constant=*/false,
6890                                            NULL);
6891       /* Look for the closing `]'.  */
6892       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6893
6894       /* Add this bound to the declarator.  */
6895       declarator = make_array_declarator (declarator, expression);
6896
6897       /* If the next token is not a `[', then there are no more
6898          bounds.  */
6899       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6900         break;
6901     }
6902
6903   return declarator;
6904 }
6905
6906 /* Parse a new-initializer.
6907
6908    new-initializer:
6909      ( expression-list [opt] )
6910      braced-init-list
6911
6912    Returns a representation of the expression-list.  */
6913
6914 static VEC(tree,gc) *
6915 cp_parser_new_initializer (cp_parser* parser)
6916 {
6917   VEC(tree,gc) *expression_list;
6918
6919   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6920     {
6921       tree t;
6922       bool expr_non_constant_p;
6923       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6924       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6925       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6926       expression_list = make_tree_vector_single (t);
6927     }
6928   else
6929     expression_list = (cp_parser_parenthesized_expression_list
6930                        (parser, non_attr, /*cast_p=*/false,
6931                         /*allow_expansion_p=*/true,
6932                         /*non_constant_p=*/NULL));
6933
6934   return expression_list;
6935 }
6936
6937 /* Parse a delete-expression.
6938
6939    delete-expression:
6940      :: [opt] delete cast-expression
6941      :: [opt] delete [ ] cast-expression
6942
6943    Returns a representation of the expression.  */
6944
6945 static tree
6946 cp_parser_delete_expression (cp_parser* parser)
6947 {
6948   bool global_scope_p;
6949   bool array_p;
6950   tree expression;
6951
6952   /* Look for the optional `::' operator.  */
6953   global_scope_p
6954     = (cp_parser_global_scope_opt (parser,
6955                                    /*current_scope_valid_p=*/false)
6956        != NULL_TREE);
6957   /* Look for the `delete' keyword.  */
6958   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6959   /* See if the array syntax is in use.  */
6960   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6961     {
6962       /* Consume the `[' token.  */
6963       cp_lexer_consume_token (parser->lexer);
6964       /* Look for the `]' token.  */
6965       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6966       /* Remember that this is the `[]' construct.  */
6967       array_p = true;
6968     }
6969   else
6970     array_p = false;
6971
6972   /* Parse the cast-expression.  */
6973   expression = cp_parser_simple_cast_expression (parser);
6974
6975   /* A delete-expression may not appear in an integral constant
6976      expression.  */
6977   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6978     return error_mark_node;
6979
6980   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6981                         tf_warning_or_error);
6982 }
6983
6984 /* Returns true if TOKEN may start a cast-expression and false
6985    otherwise.  */
6986
6987 static bool
6988 cp_parser_token_starts_cast_expression (cp_token *token)
6989 {
6990   switch (token->type)
6991     {
6992     case CPP_COMMA:
6993     case CPP_SEMICOLON:
6994     case CPP_QUERY:
6995     case CPP_COLON:
6996     case CPP_CLOSE_SQUARE:
6997     case CPP_CLOSE_PAREN:
6998     case CPP_CLOSE_BRACE:
6999     case CPP_DOT:
7000     case CPP_DOT_STAR:
7001     case CPP_DEREF:
7002     case CPP_DEREF_STAR:
7003     case CPP_DIV:
7004     case CPP_MOD:
7005     case CPP_LSHIFT:
7006     case CPP_RSHIFT:
7007     case CPP_LESS:
7008     case CPP_GREATER:
7009     case CPP_LESS_EQ:
7010     case CPP_GREATER_EQ:
7011     case CPP_EQ_EQ:
7012     case CPP_NOT_EQ:
7013     case CPP_EQ:
7014     case CPP_MULT_EQ:
7015     case CPP_DIV_EQ:
7016     case CPP_MOD_EQ:
7017     case CPP_PLUS_EQ:
7018     case CPP_MINUS_EQ:
7019     case CPP_RSHIFT_EQ:
7020     case CPP_LSHIFT_EQ:
7021     case CPP_AND_EQ:
7022     case CPP_XOR_EQ:
7023     case CPP_OR_EQ:
7024     case CPP_XOR:
7025     case CPP_OR:
7026     case CPP_OR_OR:
7027     case CPP_EOF:
7028       return false;
7029
7030       /* '[' may start a primary-expression in obj-c++.  */
7031     case CPP_OPEN_SQUARE:
7032       return c_dialect_objc ();
7033
7034     default:
7035       return true;
7036     }
7037 }
7038
7039 /* Parse a cast-expression.
7040
7041    cast-expression:
7042      unary-expression
7043      ( type-id ) cast-expression
7044
7045    ADDRESS_P is true iff the unary-expression is appearing as the
7046    operand of the `&' operator.   CAST_P is true if this expression is
7047    the target of a cast.
7048
7049    Returns a representation of the expression.  */
7050
7051 static tree
7052 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7053                            cp_id_kind * pidk)
7054 {
7055   /* If it's a `(', then we might be looking at a cast.  */
7056   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7057     {
7058       tree type = NULL_TREE;
7059       tree expr = NULL_TREE;
7060       bool compound_literal_p;
7061       const char *saved_message;
7062
7063       /* There's no way to know yet whether or not this is a cast.
7064          For example, `(int (3))' is a unary-expression, while `(int)
7065          3' is a cast.  So, we resort to parsing tentatively.  */
7066       cp_parser_parse_tentatively (parser);
7067       /* Types may not be defined in a cast.  */
7068       saved_message = parser->type_definition_forbidden_message;
7069       parser->type_definition_forbidden_message
7070         = G_("types may not be defined in casts");
7071       /* Consume the `('.  */
7072       cp_lexer_consume_token (parser->lexer);
7073       /* A very tricky bit is that `(struct S) { 3 }' is a
7074          compound-literal (which we permit in C++ as an extension).
7075          But, that construct is not a cast-expression -- it is a
7076          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7077          is legal; if the compound-literal were a cast-expression,
7078          you'd need an extra set of parentheses.)  But, if we parse
7079          the type-id, and it happens to be a class-specifier, then we
7080          will commit to the parse at that point, because we cannot
7081          undo the action that is done when creating a new class.  So,
7082          then we cannot back up and do a postfix-expression.
7083
7084          Therefore, we scan ahead to the closing `)', and check to see
7085          if the token after the `)' is a `{'.  If so, we are not
7086          looking at a cast-expression.
7087
7088          Save tokens so that we can put them back.  */
7089       cp_lexer_save_tokens (parser->lexer);
7090       /* Skip tokens until the next token is a closing parenthesis.
7091          If we find the closing `)', and the next token is a `{', then
7092          we are looking at a compound-literal.  */
7093       compound_literal_p
7094         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7095                                                   /*consume_paren=*/true)
7096            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7097       /* Roll back the tokens we skipped.  */
7098       cp_lexer_rollback_tokens (parser->lexer);
7099       /* If we were looking at a compound-literal, simulate an error
7100          so that the call to cp_parser_parse_definitely below will
7101          fail.  */
7102       if (compound_literal_p)
7103         cp_parser_simulate_error (parser);
7104       else
7105         {
7106           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7107           parser->in_type_id_in_expr_p = true;
7108           /* Look for the type-id.  */
7109           type = cp_parser_type_id (parser);
7110           /* Look for the closing `)'.  */
7111           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7112           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7113         }
7114
7115       /* Restore the saved message.  */
7116       parser->type_definition_forbidden_message = saved_message;
7117
7118       /* At this point this can only be either a cast or a
7119          parenthesized ctor such as `(T ())' that looks like a cast to
7120          function returning T.  */
7121       if (!cp_parser_error_occurred (parser)
7122           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7123                                                      (parser->lexer)))
7124         {
7125           cp_parser_parse_definitely (parser);
7126           expr = cp_parser_cast_expression (parser,
7127                                             /*address_p=*/false,
7128                                             /*cast_p=*/true, pidk);
7129
7130           /* Warn about old-style casts, if so requested.  */
7131           if (warn_old_style_cast
7132               && !in_system_header
7133               && !VOID_TYPE_P (type)
7134               && current_lang_name != lang_name_c)
7135             warning (OPT_Wold_style_cast, "use of old-style cast");
7136
7137           /* Only type conversions to integral or enumeration types
7138              can be used in constant-expressions.  */
7139           if (!cast_valid_in_integral_constant_expression_p (type)
7140               && cp_parser_non_integral_constant_expression (parser,
7141                                                              NIC_CAST))
7142             return error_mark_node;
7143
7144           /* Perform the cast.  */
7145           expr = build_c_cast (input_location, type, expr);
7146           return expr;
7147         }
7148       else 
7149         cp_parser_abort_tentative_parse (parser);
7150     }
7151
7152   /* If we get here, then it's not a cast, so it must be a
7153      unary-expression.  */
7154   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7155 }
7156
7157 /* Parse a binary expression of the general form:
7158
7159    pm-expression:
7160      cast-expression
7161      pm-expression .* cast-expression
7162      pm-expression ->* cast-expression
7163
7164    multiplicative-expression:
7165      pm-expression
7166      multiplicative-expression * pm-expression
7167      multiplicative-expression / pm-expression
7168      multiplicative-expression % pm-expression
7169
7170    additive-expression:
7171      multiplicative-expression
7172      additive-expression + multiplicative-expression
7173      additive-expression - multiplicative-expression
7174
7175    shift-expression:
7176      additive-expression
7177      shift-expression << additive-expression
7178      shift-expression >> additive-expression
7179
7180    relational-expression:
7181      shift-expression
7182      relational-expression < shift-expression
7183      relational-expression > shift-expression
7184      relational-expression <= shift-expression
7185      relational-expression >= shift-expression
7186
7187   GNU Extension:
7188
7189    relational-expression:
7190      relational-expression <? shift-expression
7191      relational-expression >? shift-expression
7192
7193    equality-expression:
7194      relational-expression
7195      equality-expression == relational-expression
7196      equality-expression != relational-expression
7197
7198    and-expression:
7199      equality-expression
7200      and-expression & equality-expression
7201
7202    exclusive-or-expression:
7203      and-expression
7204      exclusive-or-expression ^ and-expression
7205
7206    inclusive-or-expression:
7207      exclusive-or-expression
7208      inclusive-or-expression | exclusive-or-expression
7209
7210    logical-and-expression:
7211      inclusive-or-expression
7212      logical-and-expression && inclusive-or-expression
7213
7214    logical-or-expression:
7215      logical-and-expression
7216      logical-or-expression || logical-and-expression
7217
7218    All these are implemented with a single function like:
7219
7220    binary-expression:
7221      simple-cast-expression
7222      binary-expression <token> binary-expression
7223
7224    CAST_P is true if this expression is the target of a cast.
7225
7226    The binops_by_token map is used to get the tree codes for each <token> type.
7227    binary-expressions are associated according to a precedence table.  */
7228
7229 #define TOKEN_PRECEDENCE(token)                              \
7230 (((token->type == CPP_GREATER                                \
7231    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7232   && !parser->greater_than_is_operator_p)                    \
7233  ? PREC_NOT_OPERATOR                                         \
7234  : binops_by_token[token->type].prec)
7235
7236 static tree
7237 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7238                              bool no_toplevel_fold_p,
7239                              enum cp_parser_prec prec,
7240                              cp_id_kind * pidk)
7241 {
7242   cp_parser_expression_stack stack;
7243   cp_parser_expression_stack_entry *sp = &stack[0];
7244   tree lhs, rhs;
7245   cp_token *token;
7246   enum tree_code tree_type, lhs_type, rhs_type;
7247   enum cp_parser_prec new_prec, lookahead_prec;
7248   tree overload;
7249
7250   /* Parse the first expression.  */
7251   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7252   lhs_type = ERROR_MARK;
7253
7254   for (;;)
7255     {
7256       /* Get an operator token.  */
7257       token = cp_lexer_peek_token (parser->lexer);
7258
7259       if (warn_cxx0x_compat
7260           && token->type == CPP_RSHIFT
7261           && !parser->greater_than_is_operator_p)
7262         {
7263           if (warning_at (token->location, OPT_Wc__0x_compat, 
7264                           "%<>>%> operator is treated as"
7265                           " two right angle brackets in C++11"))
7266             inform (token->location,
7267                     "suggest parentheses around %<>>%> expression");
7268         }
7269
7270       new_prec = TOKEN_PRECEDENCE (token);
7271
7272       /* Popping an entry off the stack means we completed a subexpression:
7273          - either we found a token which is not an operator (`>' where it is not
7274            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7275            will happen repeatedly;
7276          - or, we found an operator which has lower priority.  This is the case
7277            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7278            parsing `3 * 4'.  */
7279       if (new_prec <= prec)
7280         {
7281           if (sp == stack)
7282             break;
7283           else
7284             goto pop;
7285         }
7286
7287      get_rhs:
7288       tree_type = binops_by_token[token->type].tree_type;
7289
7290       /* We used the operator token.  */
7291       cp_lexer_consume_token (parser->lexer);
7292
7293       /* For "false && x" or "true || x", x will never be executed;
7294          disable warnings while evaluating it.  */
7295       if (tree_type == TRUTH_ANDIF_EXPR)
7296         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7297       else if (tree_type == TRUTH_ORIF_EXPR)
7298         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7299
7300       /* Extract another operand.  It may be the RHS of this expression
7301          or the LHS of a new, higher priority expression.  */
7302       rhs = cp_parser_simple_cast_expression (parser);
7303       rhs_type = ERROR_MARK;
7304
7305       /* Get another operator token.  Look up its precedence to avoid
7306          building a useless (immediately popped) stack entry for common
7307          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7308       token = cp_lexer_peek_token (parser->lexer);
7309       lookahead_prec = TOKEN_PRECEDENCE (token);
7310       if (lookahead_prec > new_prec)
7311         {
7312           /* ... and prepare to parse the RHS of the new, higher priority
7313              expression.  Since precedence levels on the stack are
7314              monotonically increasing, we do not have to care about
7315              stack overflows.  */
7316           sp->prec = prec;
7317           sp->tree_type = tree_type;
7318           sp->lhs = lhs;
7319           sp->lhs_type = lhs_type;
7320           sp++;
7321           lhs = rhs;
7322           lhs_type = rhs_type;
7323           prec = new_prec;
7324           new_prec = lookahead_prec;
7325           goto get_rhs;
7326
7327          pop:
7328           lookahead_prec = new_prec;
7329           /* If the stack is not empty, we have parsed into LHS the right side
7330              (`4' in the example above) of an expression we had suspended.
7331              We can use the information on the stack to recover the LHS (`3')
7332              from the stack together with the tree code (`MULT_EXPR'), and
7333              the precedence of the higher level subexpression
7334              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7335              which will be used to actually build the additive expression.  */
7336           --sp;
7337           prec = sp->prec;
7338           tree_type = sp->tree_type;
7339           rhs = lhs;
7340           rhs_type = lhs_type;
7341           lhs = sp->lhs;
7342           lhs_type = sp->lhs_type;
7343         }
7344
7345       /* Undo the disabling of warnings done above.  */
7346       if (tree_type == TRUTH_ANDIF_EXPR)
7347         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7348       else if (tree_type == TRUTH_ORIF_EXPR)
7349         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7350
7351       overload = NULL;
7352       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7353          ERROR_MARK for everything that is not a binary expression.
7354          This makes warn_about_parentheses miss some warnings that
7355          involve unary operators.  For unary expressions we should
7356          pass the correct tree_code unless the unary expression was
7357          surrounded by parentheses.
7358       */
7359       if (no_toplevel_fold_p
7360           && lookahead_prec <= prec
7361           && sp == stack
7362           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7363         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7364       else
7365         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7366                                  &overload, tf_warning_or_error);
7367       lhs_type = tree_type;
7368
7369       /* If the binary operator required the use of an overloaded operator,
7370          then this expression cannot be an integral constant-expression.
7371          An overloaded operator can be used even if both operands are
7372          otherwise permissible in an integral constant-expression if at
7373          least one of the operands is of enumeration type.  */
7374
7375       if (overload
7376           && cp_parser_non_integral_constant_expression (parser,
7377                                                          NIC_OVERLOADED))
7378         return error_mark_node;
7379     }
7380
7381   return lhs;
7382 }
7383
7384
7385 /* Parse the `? expression : assignment-expression' part of a
7386    conditional-expression.  The LOGICAL_OR_EXPR is the
7387    logical-or-expression that started the conditional-expression.
7388    Returns a representation of the entire conditional-expression.
7389
7390    This routine is used by cp_parser_assignment_expression.
7391
7392      ? expression : assignment-expression
7393
7394    GNU Extensions:
7395
7396      ? : assignment-expression */
7397
7398 static tree
7399 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7400 {
7401   tree expr;
7402   tree assignment_expr;
7403   struct cp_token *token;
7404
7405   /* Consume the `?' token.  */
7406   cp_lexer_consume_token (parser->lexer);
7407   token = cp_lexer_peek_token (parser->lexer);
7408   if (cp_parser_allow_gnu_extensions_p (parser)
7409       && token->type == CPP_COLON)
7410     {
7411       pedwarn (token->location, OPT_pedantic, 
7412                "ISO C++ does not allow ?: with omitted middle operand");
7413       /* Implicit true clause.  */
7414       expr = NULL_TREE;
7415       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7416       warn_for_omitted_condop (token->location, logical_or_expr);
7417     }
7418   else
7419     {
7420       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7421       parser->colon_corrects_to_scope_p = false;
7422       /* Parse the expression.  */
7423       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7424       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7425       c_inhibit_evaluation_warnings +=
7426         ((logical_or_expr == truthvalue_true_node)
7427          - (logical_or_expr == truthvalue_false_node));
7428       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7429     }
7430
7431   /* The next token should be a `:'.  */
7432   cp_parser_require (parser, CPP_COLON, RT_COLON);
7433   /* Parse the assignment-expression.  */
7434   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7435   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7436
7437   /* Build the conditional-expression.  */
7438   return build_x_conditional_expr (logical_or_expr,
7439                                    expr,
7440                                    assignment_expr,
7441                                    tf_warning_or_error);
7442 }
7443
7444 /* Parse an assignment-expression.
7445
7446    assignment-expression:
7447      conditional-expression
7448      logical-or-expression assignment-operator assignment_expression
7449      throw-expression
7450
7451    CAST_P is true if this expression is the target of a cast.
7452
7453    Returns a representation for the expression.  */
7454
7455 static tree
7456 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7457                                  cp_id_kind * pidk)
7458 {
7459   tree expr;
7460
7461   /* If the next token is the `throw' keyword, then we're looking at
7462      a throw-expression.  */
7463   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7464     expr = cp_parser_throw_expression (parser);
7465   /* Otherwise, it must be that we are looking at a
7466      logical-or-expression.  */
7467   else
7468     {
7469       /* Parse the binary expressions (logical-or-expression).  */
7470       expr = cp_parser_binary_expression (parser, cast_p, false,
7471                                           PREC_NOT_OPERATOR, pidk);
7472       /* If the next token is a `?' then we're actually looking at a
7473          conditional-expression.  */
7474       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7475         return cp_parser_question_colon_clause (parser, expr);
7476       else
7477         {
7478           enum tree_code assignment_operator;
7479
7480           /* If it's an assignment-operator, we're using the second
7481              production.  */
7482           assignment_operator
7483             = cp_parser_assignment_operator_opt (parser);
7484           if (assignment_operator != ERROR_MARK)
7485             {
7486               bool non_constant_p;
7487
7488               /* Parse the right-hand side of the assignment.  */
7489               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7490
7491               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7492                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7493
7494               /* An assignment may not appear in a
7495                  constant-expression.  */
7496               if (cp_parser_non_integral_constant_expression (parser,
7497                                                               NIC_ASSIGNMENT))
7498                 return error_mark_node;
7499               /* Build the assignment expression.  */
7500               expr = build_x_modify_expr (expr,
7501                                           assignment_operator,
7502                                           rhs,
7503                                           tf_warning_or_error);
7504             }
7505         }
7506     }
7507
7508   return expr;
7509 }
7510
7511 /* Parse an (optional) assignment-operator.
7512
7513    assignment-operator: one of
7514      = *= /= %= += -= >>= <<= &= ^= |=
7515
7516    GNU Extension:
7517
7518    assignment-operator: one of
7519      <?= >?=
7520
7521    If the next token is an assignment operator, the corresponding tree
7522    code is returned, and the token is consumed.  For example, for
7523    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7524    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7525    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7526    operator, ERROR_MARK is returned.  */
7527
7528 static enum tree_code
7529 cp_parser_assignment_operator_opt (cp_parser* parser)
7530 {
7531   enum tree_code op;
7532   cp_token *token;
7533
7534   /* Peek at the next token.  */
7535   token = cp_lexer_peek_token (parser->lexer);
7536
7537   switch (token->type)
7538     {
7539     case CPP_EQ:
7540       op = NOP_EXPR;
7541       break;
7542
7543     case CPP_MULT_EQ:
7544       op = MULT_EXPR;
7545       break;
7546
7547     case CPP_DIV_EQ:
7548       op = TRUNC_DIV_EXPR;
7549       break;
7550
7551     case CPP_MOD_EQ:
7552       op = TRUNC_MOD_EXPR;
7553       break;
7554
7555     case CPP_PLUS_EQ:
7556       op = PLUS_EXPR;
7557       break;
7558
7559     case CPP_MINUS_EQ:
7560       op = MINUS_EXPR;
7561       break;
7562
7563     case CPP_RSHIFT_EQ:
7564       op = RSHIFT_EXPR;
7565       break;
7566
7567     case CPP_LSHIFT_EQ:
7568       op = LSHIFT_EXPR;
7569       break;
7570
7571     case CPP_AND_EQ:
7572       op = BIT_AND_EXPR;
7573       break;
7574
7575     case CPP_XOR_EQ:
7576       op = BIT_XOR_EXPR;
7577       break;
7578
7579     case CPP_OR_EQ:
7580       op = BIT_IOR_EXPR;
7581       break;
7582
7583     default:
7584       /* Nothing else is an assignment operator.  */
7585       op = ERROR_MARK;
7586     }
7587
7588   /* If it was an assignment operator, consume it.  */
7589   if (op != ERROR_MARK)
7590     cp_lexer_consume_token (parser->lexer);
7591
7592   return op;
7593 }
7594
7595 /* Parse an expression.
7596
7597    expression:
7598      assignment-expression
7599      expression , assignment-expression
7600
7601    CAST_P is true if this expression is the target of a cast.
7602
7603    Returns a representation of the expression.  */
7604
7605 static tree
7606 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7607 {
7608   tree expression = NULL_TREE;
7609
7610   while (true)
7611     {
7612       tree assignment_expression;
7613
7614       /* Parse the next assignment-expression.  */
7615       assignment_expression
7616         = cp_parser_assignment_expression (parser, cast_p, pidk);
7617       /* If this is the first assignment-expression, we can just
7618          save it away.  */
7619       if (!expression)
7620         expression = assignment_expression;
7621       else
7622         expression = build_x_compound_expr (expression,
7623                                             assignment_expression,
7624                                             tf_warning_or_error);
7625       /* If the next token is not a comma, then we are done with the
7626          expression.  */
7627       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7628         break;
7629       /* Consume the `,'.  */
7630       cp_lexer_consume_token (parser->lexer);
7631       /* A comma operator cannot appear in a constant-expression.  */
7632       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7633         expression = error_mark_node;
7634     }
7635
7636   return expression;
7637 }
7638
7639 /* Parse a constant-expression.
7640
7641    constant-expression:
7642      conditional-expression
7643
7644   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7645   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7646   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7647   is false, NON_CONSTANT_P should be NULL.  */
7648
7649 static tree
7650 cp_parser_constant_expression (cp_parser* parser,
7651                                bool allow_non_constant_p,
7652                                bool *non_constant_p)
7653 {
7654   bool saved_integral_constant_expression_p;
7655   bool saved_allow_non_integral_constant_expression_p;
7656   bool saved_non_integral_constant_expression_p;
7657   tree expression;
7658
7659   /* It might seem that we could simply parse the
7660      conditional-expression, and then check to see if it were
7661      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7662      one that the compiler can figure out is constant, possibly after
7663      doing some simplifications or optimizations.  The standard has a
7664      precise definition of constant-expression, and we must honor
7665      that, even though it is somewhat more restrictive.
7666
7667      For example:
7668
7669        int i[(2, 3)];
7670
7671      is not a legal declaration, because `(2, 3)' is not a
7672      constant-expression.  The `,' operator is forbidden in a
7673      constant-expression.  However, GCC's constant-folding machinery
7674      will fold this operation to an INTEGER_CST for `3'.  */
7675
7676   /* Save the old settings.  */
7677   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7678   saved_allow_non_integral_constant_expression_p
7679     = parser->allow_non_integral_constant_expression_p;
7680   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7681   /* We are now parsing a constant-expression.  */
7682   parser->integral_constant_expression_p = true;
7683   parser->allow_non_integral_constant_expression_p
7684     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7685   parser->non_integral_constant_expression_p = false;
7686   /* Although the grammar says "conditional-expression", we parse an
7687      "assignment-expression", which also permits "throw-expression"
7688      and the use of assignment operators.  In the case that
7689      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7690      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7691      actually essential that we look for an assignment-expression.
7692      For example, cp_parser_initializer_clauses uses this function to
7693      determine whether a particular assignment-expression is in fact
7694      constant.  */
7695   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7696   /* Restore the old settings.  */
7697   parser->integral_constant_expression_p
7698     = saved_integral_constant_expression_p;
7699   parser->allow_non_integral_constant_expression_p
7700     = saved_allow_non_integral_constant_expression_p;
7701   if (cxx_dialect >= cxx0x)
7702     {
7703       /* Require an rvalue constant expression here; that's what our
7704          callers expect.  Reference constant expressions are handled
7705          separately in e.g. cp_parser_template_argument.  */
7706       bool is_const = potential_rvalue_constant_expression (expression);
7707       parser->non_integral_constant_expression_p = !is_const;
7708       if (!is_const && !allow_non_constant_p)
7709         require_potential_rvalue_constant_expression (expression);
7710     }
7711   if (allow_non_constant_p)
7712     *non_constant_p = parser->non_integral_constant_expression_p;
7713   parser->non_integral_constant_expression_p
7714     = saved_non_integral_constant_expression_p;
7715
7716   return expression;
7717 }
7718
7719 /* Parse __builtin_offsetof.
7720
7721    offsetof-expression:
7722      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7723
7724    offsetof-member-designator:
7725      id-expression
7726      | offsetof-member-designator "." id-expression
7727      | offsetof-member-designator "[" expression "]"
7728      | offsetof-member-designator "->" id-expression  */
7729
7730 static tree
7731 cp_parser_builtin_offsetof (cp_parser *parser)
7732 {
7733   int save_ice_p, save_non_ice_p;
7734   tree type, expr;
7735   cp_id_kind dummy;
7736   cp_token *token;
7737
7738   /* We're about to accept non-integral-constant things, but will
7739      definitely yield an integral constant expression.  Save and
7740      restore these values around our local parsing.  */
7741   save_ice_p = parser->integral_constant_expression_p;
7742   save_non_ice_p = parser->non_integral_constant_expression_p;
7743
7744   /* Consume the "__builtin_offsetof" token.  */
7745   cp_lexer_consume_token (parser->lexer);
7746   /* Consume the opening `('.  */
7747   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7748   /* Parse the type-id.  */
7749   type = cp_parser_type_id (parser);
7750   /* Look for the `,'.  */
7751   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7752   token = cp_lexer_peek_token (parser->lexer);
7753
7754   /* Build the (type *)null that begins the traditional offsetof macro.  */
7755   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7756                             tf_warning_or_error);
7757
7758   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7759   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7760                                                  true, &dummy, token->location);
7761   while (true)
7762     {
7763       token = cp_lexer_peek_token (parser->lexer);
7764       switch (token->type)
7765         {
7766         case CPP_OPEN_SQUARE:
7767           /* offsetof-member-designator "[" expression "]" */
7768           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7769           break;
7770
7771         case CPP_DEREF:
7772           /* offsetof-member-designator "->" identifier */
7773           expr = grok_array_decl (expr, integer_zero_node);
7774           /* FALLTHRU */
7775
7776         case CPP_DOT:
7777           /* offsetof-member-designator "." identifier */
7778           cp_lexer_consume_token (parser->lexer);
7779           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7780                                                          expr, true, &dummy,
7781                                                          token->location);
7782           break;
7783
7784         case CPP_CLOSE_PAREN:
7785           /* Consume the ")" token.  */
7786           cp_lexer_consume_token (parser->lexer);
7787           goto success;
7788
7789         default:
7790           /* Error.  We know the following require will fail, but
7791              that gives the proper error message.  */
7792           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7793           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7794           expr = error_mark_node;
7795           goto failure;
7796         }
7797     }
7798
7799  success:
7800   /* If we're processing a template, we can't finish the semantics yet.
7801      Otherwise we can fold the entire expression now.  */
7802   if (processing_template_decl)
7803     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7804   else
7805     expr = finish_offsetof (expr);
7806
7807  failure:
7808   parser->integral_constant_expression_p = save_ice_p;
7809   parser->non_integral_constant_expression_p = save_non_ice_p;
7810
7811   return expr;
7812 }
7813
7814 /* Parse a trait expression.
7815
7816    Returns a representation of the expression, the underlying type
7817    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7818
7819 static tree
7820 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7821 {
7822   cp_trait_kind kind;
7823   tree type1, type2 = NULL_TREE;
7824   bool binary = false;
7825   cp_decl_specifier_seq decl_specs;
7826
7827   switch (keyword)
7828     {
7829     case RID_HAS_NOTHROW_ASSIGN:
7830       kind = CPTK_HAS_NOTHROW_ASSIGN;
7831       break;
7832     case RID_HAS_NOTHROW_CONSTRUCTOR:
7833       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7834       break;
7835     case RID_HAS_NOTHROW_COPY:
7836       kind = CPTK_HAS_NOTHROW_COPY;
7837       break;
7838     case RID_HAS_TRIVIAL_ASSIGN:
7839       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7840       break;
7841     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7842       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7843       break;
7844     case RID_HAS_TRIVIAL_COPY:
7845       kind = CPTK_HAS_TRIVIAL_COPY;
7846       break;
7847     case RID_HAS_TRIVIAL_DESTRUCTOR:
7848       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7849       break;
7850     case RID_HAS_VIRTUAL_DESTRUCTOR:
7851       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7852       break;
7853     case RID_IS_ABSTRACT:
7854       kind = CPTK_IS_ABSTRACT;
7855       break;
7856     case RID_IS_BASE_OF:
7857       kind = CPTK_IS_BASE_OF;
7858       binary = true;
7859       break;
7860     case RID_IS_CLASS:
7861       kind = CPTK_IS_CLASS;
7862       break;
7863     case RID_IS_CONVERTIBLE_TO:
7864       kind = CPTK_IS_CONVERTIBLE_TO;
7865       binary = true;
7866       break;
7867     case RID_IS_EMPTY:
7868       kind = CPTK_IS_EMPTY;
7869       break;
7870     case RID_IS_ENUM:
7871       kind = CPTK_IS_ENUM;
7872       break;
7873     case RID_IS_FINAL:
7874       kind = CPTK_IS_FINAL;
7875       break;
7876     case RID_IS_LITERAL_TYPE:
7877       kind = CPTK_IS_LITERAL_TYPE;
7878       break;
7879     case RID_IS_POD:
7880       kind = CPTK_IS_POD;
7881       break;
7882     case RID_IS_POLYMORPHIC:
7883       kind = CPTK_IS_POLYMORPHIC;
7884       break;
7885     case RID_IS_STD_LAYOUT:
7886       kind = CPTK_IS_STD_LAYOUT;
7887       break;
7888     case RID_IS_TRIVIAL:
7889       kind = CPTK_IS_TRIVIAL;
7890       break;
7891     case RID_IS_UNION:
7892       kind = CPTK_IS_UNION;
7893       break;
7894     case RID_UNDERLYING_TYPE:
7895       kind = CPTK_UNDERLYING_TYPE;
7896       break;
7897     case RID_BASES:
7898       kind = CPTK_BASES;
7899       break;
7900     case RID_DIRECT_BASES:
7901       kind = CPTK_DIRECT_BASES;
7902       break;
7903     default:
7904       gcc_unreachable ();
7905     }
7906
7907   /* Consume the token.  */
7908   cp_lexer_consume_token (parser->lexer);
7909
7910   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7911
7912   type1 = cp_parser_type_id (parser);
7913
7914   if (type1 == error_mark_node)
7915     return error_mark_node;
7916
7917   /* Build a trivial decl-specifier-seq.  */
7918   clear_decl_specs (&decl_specs);
7919   decl_specs.type = type1;
7920
7921   /* Call grokdeclarator to figure out what type this is.  */
7922   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7923                           /*initialized=*/0, /*attrlist=*/NULL);
7924
7925   if (binary)
7926     {
7927       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7928  
7929       type2 = cp_parser_type_id (parser);
7930
7931       if (type2 == error_mark_node)
7932         return error_mark_node;
7933
7934       /* Build a trivial decl-specifier-seq.  */
7935       clear_decl_specs (&decl_specs);
7936       decl_specs.type = type2;
7937
7938       /* Call grokdeclarator to figure out what type this is.  */
7939       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7940                               /*initialized=*/0, /*attrlist=*/NULL);
7941     }
7942
7943   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7944
7945   /* Complete the trait expression, which may mean either processing
7946      the trait expr now or saving it for template instantiation.  */
7947   switch(kind)
7948     {
7949     case CPTK_UNDERLYING_TYPE:
7950       return finish_underlying_type (type1);
7951     case CPTK_BASES:
7952       return finish_bases (type1, false);
7953     case CPTK_DIRECT_BASES:
7954       return finish_bases (type1, true);
7955     default:
7956       return finish_trait_expr (kind, type1, type2);
7957     }
7958 }
7959
7960 /* Lambdas that appear in variable initializer or default argument scope
7961    get that in their mangling, so we need to record it.  We might as well
7962    use the count for function and namespace scopes as well.  */
7963 static GTY(()) tree lambda_scope;
7964 static GTY(()) int lambda_count;
7965 typedef struct GTY(()) tree_int
7966 {
7967   tree t;
7968   int i;
7969 } tree_int;
7970 DEF_VEC_O(tree_int);
7971 DEF_VEC_ALLOC_O(tree_int,gc);
7972 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7973
7974 static void
7975 start_lambda_scope (tree decl)
7976 {
7977   tree_int ti;
7978   gcc_assert (decl);
7979   /* Once we're inside a function, we ignore other scopes and just push
7980      the function again so that popping works properly.  */
7981   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7982     decl = current_function_decl;
7983   ti.t = lambda_scope;
7984   ti.i = lambda_count;
7985   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7986   if (lambda_scope != decl)
7987     {
7988       /* Don't reset the count if we're still in the same function.  */
7989       lambda_scope = decl;
7990       lambda_count = 0;
7991     }
7992 }
7993
7994 static void
7995 record_lambda_scope (tree lambda)
7996 {
7997   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7998   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7999 }
8000
8001 static void
8002 finish_lambda_scope (void)
8003 {
8004   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
8005   if (lambda_scope != p->t)
8006     {
8007       lambda_scope = p->t;
8008       lambda_count = p->i;
8009     }
8010   VEC_pop (tree_int, lambda_scope_stack);
8011 }
8012
8013 /* Parse a lambda expression.
8014
8015    lambda-expression:
8016      lambda-introducer lambda-declarator [opt] compound-statement
8017
8018    Returns a representation of the expression.  */
8019
8020 static tree
8021 cp_parser_lambda_expression (cp_parser* parser)
8022 {
8023   tree lambda_expr = build_lambda_expr ();
8024   tree type;
8025   bool ok;
8026
8027   LAMBDA_EXPR_LOCATION (lambda_expr)
8028     = cp_lexer_peek_token (parser->lexer)->location;
8029
8030   if (cp_unevaluated_operand)
8031     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8032               "lambda-expression in unevaluated context");
8033
8034   /* We may be in the middle of deferred access check.  Disable
8035      it now.  */
8036   push_deferring_access_checks (dk_no_deferred);
8037
8038   cp_parser_lambda_introducer (parser, lambda_expr);
8039
8040   type = begin_lambda_type (lambda_expr);
8041   if (type == error_mark_node)
8042     return error_mark_node;
8043
8044   record_lambda_scope (lambda_expr);
8045
8046   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8047   determine_visibility (TYPE_NAME (type));
8048
8049   /* Now that we've started the type, add the capture fields for any
8050      explicit captures.  */
8051   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8052
8053   {
8054     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8055     unsigned int saved_num_template_parameter_lists
8056         = parser->num_template_parameter_lists;
8057     unsigned char in_statement = parser->in_statement;
8058     bool in_switch_statement_p = parser->in_switch_statement_p;
8059
8060     parser->num_template_parameter_lists = 0;
8061     parser->in_statement = 0;
8062     parser->in_switch_statement_p = false;
8063
8064     /* By virtue of defining a local class, a lambda expression has access to
8065        the private variables of enclosing classes.  */
8066
8067     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8068
8069     if (ok)
8070       cp_parser_lambda_body (parser, lambda_expr);
8071     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8072       cp_parser_skip_to_end_of_block_or_statement (parser);
8073
8074     /* The capture list was built up in reverse order; fix that now.  */
8075     {
8076       tree newlist = NULL_TREE;
8077       tree elt, next;
8078
8079       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8080            elt; elt = next)
8081         {
8082           next = TREE_CHAIN (elt);
8083           TREE_CHAIN (elt) = newlist;
8084           newlist = elt;
8085         }
8086       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8087     }
8088
8089     if (ok)
8090       maybe_add_lambda_conv_op (type);
8091
8092     type = finish_struct (type, /*attributes=*/NULL_TREE);
8093
8094     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8095     parser->in_statement = in_statement;
8096     parser->in_switch_statement_p = in_switch_statement_p;
8097   }
8098
8099   pop_deferring_access_checks ();
8100
8101   /* This field is only used during parsing of the lambda.  */
8102   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8103
8104   /* This lambda shouldn't have any proxies left at this point.  */
8105   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8106   /* And now that we're done, push proxies for an enclosing lambda.  */
8107   insert_pending_capture_proxies ();
8108
8109   if (ok)
8110     return build_lambda_object (lambda_expr);
8111   else
8112     return error_mark_node;
8113 }
8114
8115 /* Parse the beginning of a lambda expression.
8116
8117    lambda-introducer:
8118      [ lambda-capture [opt] ]
8119
8120    LAMBDA_EXPR is the current representation of the lambda expression.  */
8121
8122 static void
8123 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8124 {
8125   /* Need commas after the first capture.  */
8126   bool first = true;
8127
8128   /* Eat the leading `['.  */
8129   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8130
8131   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8132   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8133       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8134     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8135   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8136     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8137
8138   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8139     {
8140       cp_lexer_consume_token (parser->lexer);
8141       first = false;
8142     }
8143
8144   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8145     {
8146       cp_token* capture_token;
8147       tree capture_id;
8148       tree capture_init_expr;
8149       cp_id_kind idk = CP_ID_KIND_NONE;
8150       bool explicit_init_p = false;
8151
8152       enum capture_kind_type
8153       {
8154         BY_COPY,
8155         BY_REFERENCE
8156       };
8157       enum capture_kind_type capture_kind = BY_COPY;
8158
8159       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8160         {
8161           error ("expected end of capture-list");
8162           return;
8163         }
8164
8165       if (first)
8166         first = false;
8167       else
8168         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8169
8170       /* Possibly capture `this'.  */
8171       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8172         {
8173           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8174           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8175             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8176                      "with by-copy capture default");
8177           cp_lexer_consume_token (parser->lexer);
8178           add_capture (lambda_expr,
8179                        /*id=*/this_identifier,
8180                        /*initializer=*/finish_this_expr(),
8181                        /*by_reference_p=*/false,
8182                        explicit_init_p);
8183           continue;
8184         }
8185
8186       /* Remember whether we want to capture as a reference or not.  */
8187       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8188         {
8189           capture_kind = BY_REFERENCE;
8190           cp_lexer_consume_token (parser->lexer);
8191         }
8192
8193       /* Get the identifier.  */
8194       capture_token = cp_lexer_peek_token (parser->lexer);
8195       capture_id = cp_parser_identifier (parser);
8196
8197       if (capture_id == error_mark_node)
8198         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8199            delimiters, but I modified this to stop on unnested ']' as well.  It
8200            was already changed to stop on unnested '}', so the
8201            "closing_parenthesis" name is no more misleading with my change.  */
8202         {
8203           cp_parser_skip_to_closing_parenthesis (parser,
8204                                                  /*recovering=*/true,
8205                                                  /*or_comma=*/true,
8206                                                  /*consume_paren=*/true);
8207           break;
8208         }
8209
8210       /* Find the initializer for this capture.  */
8211       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8212         {
8213           /* An explicit expression exists.  */
8214           cp_lexer_consume_token (parser->lexer);
8215           pedwarn (input_location, OPT_pedantic,
8216                    "ISO C++ does not allow initializers "
8217                    "in lambda expression capture lists");
8218           capture_init_expr = cp_parser_assignment_expression (parser,
8219                                                                /*cast_p=*/true,
8220                                                                &idk);
8221           explicit_init_p = true;
8222         }
8223       else
8224         {
8225           const char* error_msg;
8226
8227           /* Turn the identifier into an id-expression.  */
8228           capture_init_expr
8229             = cp_parser_lookup_name
8230                 (parser,
8231                  capture_id,
8232                  none_type,
8233                  /*is_template=*/false,
8234                  /*is_namespace=*/false,
8235                  /*check_dependency=*/true,
8236                  /*ambiguous_decls=*/NULL,
8237                  capture_token->location);
8238
8239           if (capture_init_expr == error_mark_node)
8240             {
8241               unqualified_name_lookup_error (capture_id);
8242               continue;
8243             }
8244           else if (DECL_P (capture_init_expr)
8245                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8246                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8247             {
8248               error_at (capture_token->location,
8249                         "capture of non-variable %qD ",
8250                         capture_init_expr);
8251               inform (0, "%q+#D declared here", capture_init_expr);
8252               continue;
8253             }
8254           if (TREE_CODE (capture_init_expr) == VAR_DECL
8255               && decl_storage_duration (capture_init_expr) != dk_auto)
8256             {
8257               pedwarn (capture_token->location, 0, "capture of variable "
8258                        "%qD with non-automatic storage duration",
8259                        capture_init_expr);
8260               inform (0, "%q+#D declared here", capture_init_expr);
8261               continue;
8262             }
8263
8264           capture_init_expr
8265             = finish_id_expression
8266                 (capture_id,
8267                  capture_init_expr,
8268                  parser->scope,
8269                  &idk,
8270                  /*integral_constant_expression_p=*/false,
8271                  /*allow_non_integral_constant_expression_p=*/false,
8272                  /*non_integral_constant_expression_p=*/NULL,
8273                  /*template_p=*/false,
8274                  /*done=*/true,
8275                  /*address_p=*/false,
8276                  /*template_arg_p=*/false,
8277                  &error_msg,
8278                  capture_token->location);
8279         }
8280
8281       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8282           && !explicit_init_p)
8283         {
8284           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8285               && capture_kind == BY_COPY)
8286             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8287                      "of %qD redundant with by-copy capture default",
8288                      capture_id);
8289           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8290               && capture_kind == BY_REFERENCE)
8291             pedwarn (capture_token->location, 0, "explicit by-reference "
8292                      "capture of %qD redundant with by-reference capture "
8293                      "default", capture_id);
8294         }
8295
8296       add_capture (lambda_expr,
8297                    capture_id,
8298                    capture_init_expr,
8299                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8300                    explicit_init_p);
8301     }
8302
8303   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8304 }
8305
8306 /* Parse the (optional) middle of a lambda expression.
8307
8308    lambda-declarator:
8309      ( parameter-declaration-clause [opt] )
8310        attribute-specifier [opt]
8311        mutable [opt]
8312        exception-specification [opt]
8313        lambda-return-type-clause [opt]
8314
8315    LAMBDA_EXPR is the current representation of the lambda expression.  */
8316
8317 static bool
8318 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8319 {
8320   /* 5.1.1.4 of the standard says:
8321        If a lambda-expression does not include a lambda-declarator, it is as if
8322        the lambda-declarator were ().
8323      This means an empty parameter list, no attributes, and no exception
8324      specification.  */
8325   tree param_list = void_list_node;
8326   tree attributes = NULL_TREE;
8327   tree exception_spec = NULL_TREE;
8328   tree t;
8329
8330   /* The lambda-declarator is optional, but must begin with an opening
8331      parenthesis if present.  */
8332   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8333     {
8334       cp_lexer_consume_token (parser->lexer);
8335
8336       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8337
8338       /* Parse parameters.  */
8339       param_list = cp_parser_parameter_declaration_clause (parser);
8340
8341       /* Default arguments shall not be specified in the
8342          parameter-declaration-clause of a lambda-declarator.  */
8343       for (t = param_list; t; t = TREE_CHAIN (t))
8344         if (TREE_PURPOSE (t))
8345           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8346                    "default argument specified for lambda parameter");
8347
8348       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8349
8350       attributes = cp_parser_attributes_opt (parser);
8351
8352       /* Parse optional `mutable' keyword.  */
8353       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8354         {
8355           cp_lexer_consume_token (parser->lexer);
8356           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8357         }
8358
8359       /* Parse optional exception specification.  */
8360       exception_spec = cp_parser_exception_specification_opt (parser);
8361
8362       /* Parse optional trailing return type.  */
8363       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8364         {
8365           cp_lexer_consume_token (parser->lexer);
8366           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8367         }
8368
8369       /* The function parameters must be in scope all the way until after the
8370          trailing-return-type in case of decltype.  */
8371       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8372         pop_binding (DECL_NAME (t), t);
8373
8374       leave_scope ();
8375     }
8376
8377   /* Create the function call operator.
8378
8379      Messing with declarators like this is no uglier than building up the
8380      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8381      other code.  */
8382   {
8383     cp_decl_specifier_seq return_type_specs;
8384     cp_declarator* declarator;
8385     tree fco;
8386     int quals;
8387     void *p;
8388
8389     clear_decl_specs (&return_type_specs);
8390     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8391       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8392     else
8393       /* Maybe we will deduce the return type later, but we can use void
8394          as a placeholder return type anyways.  */
8395       return_type_specs.type = void_type_node;
8396
8397     p = obstack_alloc (&declarator_obstack, 0);
8398
8399     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8400                                      sfk_none);
8401
8402     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8403              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8404     declarator = make_call_declarator (declarator, param_list, quals,
8405                                        VIRT_SPEC_UNSPECIFIED,
8406                                        exception_spec,
8407                                        /*late_return_type=*/NULL_TREE);
8408     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8409
8410     fco = grokmethod (&return_type_specs,
8411                       declarator,
8412                       attributes);
8413     if (fco != error_mark_node)
8414       {
8415         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8416         DECL_ARTIFICIAL (fco) = 1;
8417         /* Give the object parameter a different name.  */
8418         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8419       }
8420
8421     finish_member_declaration (fco);
8422
8423     obstack_free (&declarator_obstack, p);
8424
8425     return (fco != error_mark_node);
8426   }
8427 }
8428
8429 /* Parse the body of a lambda expression, which is simply
8430
8431    compound-statement
8432
8433    but which requires special handling.
8434    LAMBDA_EXPR is the current representation of the lambda expression.  */
8435
8436 static void
8437 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8438 {
8439   bool nested = (current_function_decl != NULL_TREE);
8440   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8441   if (nested)
8442     push_function_context ();
8443   else
8444     /* Still increment function_depth so that we don't GC in the
8445        middle of an expression.  */
8446     ++function_depth;
8447   /* Clear this in case we're in the middle of a default argument.  */
8448   parser->local_variables_forbidden_p = false;
8449
8450   /* Finish the function call operator
8451      - class_specifier
8452      + late_parsing_for_member
8453      + function_definition_after_declarator
8454      + ctor_initializer_opt_and_function_body  */
8455   {
8456     tree fco = lambda_function (lambda_expr);
8457     tree body;
8458     bool done = false;
8459     tree compound_stmt;
8460     tree cap;
8461
8462     /* Let the front end know that we are going to be defining this
8463        function.  */
8464     start_preparsed_function (fco,
8465                               NULL_TREE,
8466                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8467
8468     start_lambda_scope (fco);
8469     body = begin_function_body ();
8470
8471     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8472       goto out;
8473
8474     /* Push the proxies for any explicit captures.  */
8475     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8476          cap = TREE_CHAIN (cap))
8477       build_capture_proxy (TREE_PURPOSE (cap));
8478
8479     compound_stmt = begin_compound_stmt (0);
8480
8481     /* 5.1.1.4 of the standard says:
8482          If a lambda-expression does not include a trailing-return-type, it
8483          is as if the trailing-return-type denotes the following type:
8484           * if the compound-statement is of the form
8485                { return attribute-specifier [opt] expression ; }
8486              the type of the returned expression after lvalue-to-rvalue
8487              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8488              (_conv.array_ 4.2), and function-to-pointer conversion
8489              (_conv.func_ 4.3);
8490           * otherwise, void.  */
8491
8492     /* In a lambda that has neither a lambda-return-type-clause
8493        nor a deducible form, errors should be reported for return statements
8494        in the body.  Since we used void as the placeholder return type, parsing
8495        the body as usual will give such desired behavior.  */
8496     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8497         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8498         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8499       {
8500         tree expr = NULL_TREE;
8501         cp_id_kind idk = CP_ID_KIND_NONE;
8502
8503         /* Parse tentatively in case there's more after the initial return
8504            statement.  */
8505         cp_parser_parse_tentatively (parser);
8506
8507         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8508
8509         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8510
8511         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8512         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8513
8514         if (cp_parser_parse_definitely (parser))
8515           {
8516             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8517
8518             /* Will get error here if type not deduced yet.  */
8519             finish_return_stmt (expr);
8520
8521             done = true;
8522           }
8523       }
8524
8525     if (!done)
8526       {
8527         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8528           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8529         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8530           cp_parser_label_declaration (parser);
8531         cp_parser_statement_seq_opt (parser, NULL_TREE);
8532         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8533         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8534       }
8535
8536     finish_compound_stmt (compound_stmt);
8537
8538   out:
8539     finish_function_body (body);
8540     finish_lambda_scope ();
8541
8542     /* Finish the function and generate code for it if necessary.  */
8543     expand_or_defer_fn (finish_function (/*inline*/2));
8544   }
8545
8546   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8547   if (nested)
8548     pop_function_context();
8549   else
8550     --function_depth;
8551 }
8552
8553 /* Statements [gram.stmt.stmt]  */
8554
8555 /* Parse a statement.
8556
8557    statement:
8558      labeled-statement
8559      expression-statement
8560      compound-statement
8561      selection-statement
8562      iteration-statement
8563      jump-statement
8564      declaration-statement
8565      try-block
8566
8567   TM Extension:
8568
8569    statement:
8570      atomic-statement
8571
8572   IN_COMPOUND is true when the statement is nested inside a
8573   cp_parser_compound_statement; this matters for certain pragmas.
8574
8575   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8576   is a (possibly labeled) if statement which is not enclosed in braces
8577   and has an else clause.  This is used to implement -Wparentheses.  */
8578
8579 static void
8580 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8581                      bool in_compound, bool *if_p)
8582 {
8583   tree statement;
8584   cp_token *token;
8585   location_t statement_location;
8586
8587  restart:
8588   if (if_p != NULL)
8589     *if_p = false;
8590   /* There is no statement yet.  */
8591   statement = NULL_TREE;
8592   /* Peek at the next token.  */
8593   token = cp_lexer_peek_token (parser->lexer);
8594   /* Remember the location of the first token in the statement.  */
8595   statement_location = token->location;
8596   /* If this is a keyword, then that will often determine what kind of
8597      statement we have.  */
8598   if (token->type == CPP_KEYWORD)
8599     {
8600       enum rid keyword = token->keyword;
8601
8602       switch (keyword)
8603         {
8604         case RID_CASE:
8605         case RID_DEFAULT:
8606           /* Looks like a labeled-statement with a case label.
8607              Parse the label, and then use tail recursion to parse
8608              the statement.  */
8609           cp_parser_label_for_labeled_statement (parser);
8610           goto restart;
8611
8612         case RID_IF:
8613         case RID_SWITCH:
8614           statement = cp_parser_selection_statement (parser, if_p);
8615           break;
8616
8617         case RID_WHILE:
8618         case RID_DO:
8619         case RID_FOR:
8620           statement = cp_parser_iteration_statement (parser);
8621           break;
8622
8623         case RID_BREAK:
8624         case RID_CONTINUE:
8625         case RID_RETURN:
8626         case RID_GOTO:
8627           statement = cp_parser_jump_statement (parser);
8628           break;
8629
8630           /* Objective-C++ exception-handling constructs.  */
8631         case RID_AT_TRY:
8632         case RID_AT_CATCH:
8633         case RID_AT_FINALLY:
8634         case RID_AT_SYNCHRONIZED:
8635         case RID_AT_THROW:
8636           statement = cp_parser_objc_statement (parser);
8637           break;
8638
8639         case RID_TRY:
8640           statement = cp_parser_try_block (parser);
8641           break;
8642
8643         case RID_NAMESPACE:
8644           /* This must be a namespace alias definition.  */
8645           cp_parser_declaration_statement (parser);
8646           return;
8647           
8648         case RID_TRANSACTION_ATOMIC:
8649         case RID_TRANSACTION_RELAXED:
8650           statement = cp_parser_transaction (parser, keyword);
8651           break;
8652         case RID_TRANSACTION_CANCEL:
8653           statement = cp_parser_transaction_cancel (parser);
8654           break;
8655
8656         default:
8657           /* It might be a keyword like `int' that can start a
8658              declaration-statement.  */
8659           break;
8660         }
8661     }
8662   else if (token->type == CPP_NAME)
8663     {
8664       /* If the next token is a `:', then we are looking at a
8665          labeled-statement.  */
8666       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8667       if (token->type == CPP_COLON)
8668         {
8669           /* Looks like a labeled-statement with an ordinary label.
8670              Parse the label, and then use tail recursion to parse
8671              the statement.  */
8672           cp_parser_label_for_labeled_statement (parser);
8673           goto restart;
8674         }
8675     }
8676   /* Anything that starts with a `{' must be a compound-statement.  */
8677   else if (token->type == CPP_OPEN_BRACE)
8678     statement = cp_parser_compound_statement (parser, NULL, false, false);
8679   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8680      a statement all its own.  */
8681   else if (token->type == CPP_PRAGMA)
8682     {
8683       /* Only certain OpenMP pragmas are attached to statements, and thus
8684          are considered statements themselves.  All others are not.  In
8685          the context of a compound, accept the pragma as a "statement" and
8686          return so that we can check for a close brace.  Otherwise we
8687          require a real statement and must go back and read one.  */
8688       if (in_compound)
8689         cp_parser_pragma (parser, pragma_compound);
8690       else if (!cp_parser_pragma (parser, pragma_stmt))
8691         goto restart;
8692       return;
8693     }
8694   else if (token->type == CPP_EOF)
8695     {
8696       cp_parser_error (parser, "expected statement");
8697       return;
8698     }
8699
8700   /* Everything else must be a declaration-statement or an
8701      expression-statement.  Try for the declaration-statement
8702      first, unless we are looking at a `;', in which case we know that
8703      we have an expression-statement.  */
8704   if (!statement)
8705     {
8706       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8707         {
8708           cp_parser_parse_tentatively (parser);
8709           /* Try to parse the declaration-statement.  */
8710           cp_parser_declaration_statement (parser);
8711           /* If that worked, we're done.  */
8712           if (cp_parser_parse_definitely (parser))
8713             return;
8714         }
8715       /* Look for an expression-statement instead.  */
8716       statement = cp_parser_expression_statement (parser, in_statement_expr);
8717     }
8718
8719   /* Set the line number for the statement.  */
8720   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8721     SET_EXPR_LOCATION (statement, statement_location);
8722 }
8723
8724 /* Parse the label for a labeled-statement, i.e.
8725
8726    identifier :
8727    case constant-expression :
8728    default :
8729
8730    GNU Extension:
8731    case constant-expression ... constant-expression : statement
8732
8733    When a label is parsed without errors, the label is added to the
8734    parse tree by the finish_* functions, so this function doesn't
8735    have to return the label.  */
8736
8737 static void
8738 cp_parser_label_for_labeled_statement (cp_parser* parser)
8739 {
8740   cp_token *token;
8741   tree label = NULL_TREE;
8742   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8743
8744   /* The next token should be an identifier.  */
8745   token = cp_lexer_peek_token (parser->lexer);
8746   if (token->type != CPP_NAME
8747       && token->type != CPP_KEYWORD)
8748     {
8749       cp_parser_error (parser, "expected labeled-statement");
8750       return;
8751     }
8752
8753   parser->colon_corrects_to_scope_p = false;
8754   switch (token->keyword)
8755     {
8756     case RID_CASE:
8757       {
8758         tree expr, expr_hi;
8759         cp_token *ellipsis;
8760
8761         /* Consume the `case' token.  */
8762         cp_lexer_consume_token (parser->lexer);
8763         /* Parse the constant-expression.  */
8764         expr = cp_parser_constant_expression (parser,
8765                                               /*allow_non_constant_p=*/false,
8766                                               NULL);
8767
8768         ellipsis = cp_lexer_peek_token (parser->lexer);
8769         if (ellipsis->type == CPP_ELLIPSIS)
8770           {
8771             /* Consume the `...' token.  */
8772             cp_lexer_consume_token (parser->lexer);
8773             expr_hi =
8774               cp_parser_constant_expression (parser,
8775                                              /*allow_non_constant_p=*/false,
8776                                              NULL);
8777             /* We don't need to emit warnings here, as the common code
8778                will do this for us.  */
8779           }
8780         else
8781           expr_hi = NULL_TREE;
8782
8783         if (parser->in_switch_statement_p)
8784           finish_case_label (token->location, expr, expr_hi);
8785         else
8786           error_at (token->location,
8787                     "case label %qE not within a switch statement",
8788                     expr);
8789       }
8790       break;
8791
8792     case RID_DEFAULT:
8793       /* Consume the `default' token.  */
8794       cp_lexer_consume_token (parser->lexer);
8795
8796       if (parser->in_switch_statement_p)
8797         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8798       else
8799         error_at (token->location, "case label not within a switch statement");
8800       break;
8801
8802     default:
8803       /* Anything else must be an ordinary label.  */
8804       label = finish_label_stmt (cp_parser_identifier (parser));
8805       break;
8806     }
8807
8808   /* Require the `:' token.  */
8809   cp_parser_require (parser, CPP_COLON, RT_COLON);
8810
8811   /* An ordinary label may optionally be followed by attributes.
8812      However, this is only permitted if the attributes are then
8813      followed by a semicolon.  This is because, for backward
8814      compatibility, when parsing
8815        lab: __attribute__ ((unused)) int i;
8816      we want the attribute to attach to "i", not "lab".  */
8817   if (label != NULL_TREE
8818       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8819     {
8820       tree attrs;
8821
8822       cp_parser_parse_tentatively (parser);
8823       attrs = cp_parser_attributes_opt (parser);
8824       if (attrs == NULL_TREE
8825           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8826         cp_parser_abort_tentative_parse (parser);
8827       else if (!cp_parser_parse_definitely (parser))
8828         ;
8829       else
8830         cplus_decl_attributes (&label, attrs, 0);
8831     }
8832
8833   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8834 }
8835
8836 /* Parse an expression-statement.
8837
8838    expression-statement:
8839      expression [opt] ;
8840
8841    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8842    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8843    indicates whether this expression-statement is part of an
8844    expression statement.  */
8845
8846 static tree
8847 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8848 {
8849   tree statement = NULL_TREE;
8850   cp_token *token = cp_lexer_peek_token (parser->lexer);
8851
8852   /* If the next token is a ';', then there is no expression
8853      statement.  */
8854   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8855     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8856
8857   /* Give a helpful message for "A<T>::type t;" and the like.  */
8858   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8859       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8860     {
8861       if (TREE_CODE (statement) == SCOPE_REF)
8862         error_at (token->location, "need %<typename%> before %qE because "
8863                   "%qT is a dependent scope",
8864                   statement, TREE_OPERAND (statement, 0));
8865       else if (is_overloaded_fn (statement)
8866                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8867         {
8868           /* A::A a; */
8869           tree fn = get_first_fn (statement);
8870           error_at (token->location,
8871                     "%<%T::%D%> names the constructor, not the type",
8872                     DECL_CONTEXT (fn), DECL_NAME (fn));
8873         }
8874     }
8875
8876   /* Consume the final `;'.  */
8877   cp_parser_consume_semicolon_at_end_of_statement (parser);
8878
8879   if (in_statement_expr
8880       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8881     /* This is the final expression statement of a statement
8882        expression.  */
8883     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8884   else if (statement)
8885     statement = finish_expr_stmt (statement);
8886   else
8887     finish_stmt ();
8888
8889   return statement;
8890 }
8891
8892 /* Parse a compound-statement.
8893
8894    compound-statement:
8895      { statement-seq [opt] }
8896
8897    GNU extension:
8898
8899    compound-statement:
8900      { label-declaration-seq [opt] statement-seq [opt] }
8901
8902    label-declaration-seq:
8903      label-declaration
8904      label-declaration-seq label-declaration
8905
8906    Returns a tree representing the statement.  */
8907
8908 static tree
8909 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8910                               bool in_try, bool function_body)
8911 {
8912   tree compound_stmt;
8913
8914   /* Consume the `{'.  */
8915   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8916     return error_mark_node;
8917   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8918       && !function_body)
8919     pedwarn (input_location, OPT_pedantic,
8920              "compound-statement in constexpr function");
8921   /* Begin the compound-statement.  */
8922   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8923   /* If the next keyword is `__label__' we have a label declaration.  */
8924   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8925     cp_parser_label_declaration (parser);
8926   /* Parse an (optional) statement-seq.  */
8927   cp_parser_statement_seq_opt (parser, in_statement_expr);
8928   /* Finish the compound-statement.  */
8929   finish_compound_stmt (compound_stmt);
8930   /* Consume the `}'.  */
8931   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8932
8933   return compound_stmt;
8934 }
8935
8936 /* Parse an (optional) statement-seq.
8937
8938    statement-seq:
8939      statement
8940      statement-seq [opt] statement  */
8941
8942 static void
8943 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8944 {
8945   /* Scan statements until there aren't any more.  */
8946   while (true)
8947     {
8948       cp_token *token = cp_lexer_peek_token (parser->lexer);
8949
8950       /* If we are looking at a `}', then we have run out of
8951          statements; the same is true if we have reached the end
8952          of file, or have stumbled upon a stray '@end'.  */
8953       if (token->type == CPP_CLOSE_BRACE
8954           || token->type == CPP_EOF
8955           || token->type == CPP_PRAGMA_EOL
8956           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8957         break;
8958       
8959       /* If we are in a compound statement and find 'else' then
8960          something went wrong.  */
8961       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8962         {
8963           if (parser->in_statement & IN_IF_STMT) 
8964             break;
8965           else
8966             {
8967               token = cp_lexer_consume_token (parser->lexer);
8968               error_at (token->location, "%<else%> without a previous %<if%>");
8969             }
8970         }
8971
8972       /* Parse the statement.  */
8973       cp_parser_statement (parser, in_statement_expr, true, NULL);
8974     }
8975 }
8976
8977 /* Parse a selection-statement.
8978
8979    selection-statement:
8980      if ( condition ) statement
8981      if ( condition ) statement else statement
8982      switch ( condition ) statement
8983
8984    Returns the new IF_STMT or SWITCH_STMT.
8985
8986    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8987    is a (possibly labeled) if statement which is not enclosed in
8988    braces and has an else clause.  This is used to implement
8989    -Wparentheses.  */
8990
8991 static tree
8992 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8993 {
8994   cp_token *token;
8995   enum rid keyword;
8996
8997   if (if_p != NULL)
8998     *if_p = false;
8999
9000   /* Peek at the next token.  */
9001   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
9002
9003   /* See what kind of keyword it is.  */
9004   keyword = token->keyword;
9005   switch (keyword)
9006     {
9007     case RID_IF:
9008     case RID_SWITCH:
9009       {
9010         tree statement;
9011         tree condition;
9012
9013         /* Look for the `('.  */
9014         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
9015           {
9016             cp_parser_skip_to_end_of_statement (parser);
9017             return error_mark_node;
9018           }
9019
9020         /* Begin the selection-statement.  */
9021         if (keyword == RID_IF)
9022           statement = begin_if_stmt ();
9023         else
9024           statement = begin_switch_stmt ();
9025
9026         /* Parse the condition.  */
9027         condition = cp_parser_condition (parser);
9028         /* Look for the `)'.  */
9029         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
9030           cp_parser_skip_to_closing_parenthesis (parser, true, false,
9031                                                  /*consume_paren=*/true);
9032
9033         if (keyword == RID_IF)
9034           {
9035             bool nested_if;
9036             unsigned char in_statement;
9037
9038             /* Add the condition.  */
9039             finish_if_stmt_cond (condition, statement);
9040
9041             /* Parse the then-clause.  */
9042             in_statement = parser->in_statement;
9043             parser->in_statement |= IN_IF_STMT;
9044             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9045               {
9046                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9047                 add_stmt (build_empty_stmt (loc));
9048                 cp_lexer_consume_token (parser->lexer);
9049                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9050                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9051                               "empty body in an %<if%> statement");
9052                 nested_if = false;
9053               }
9054             else
9055               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9056             parser->in_statement = in_statement;
9057
9058             finish_then_clause (statement);
9059
9060             /* If the next token is `else', parse the else-clause.  */
9061             if (cp_lexer_next_token_is_keyword (parser->lexer,
9062                                                 RID_ELSE))
9063               {
9064                 /* Consume the `else' keyword.  */
9065                 cp_lexer_consume_token (parser->lexer);
9066                 begin_else_clause (statement);
9067                 /* Parse the else-clause.  */
9068                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9069                   {
9070                     location_t loc;
9071                     loc = cp_lexer_peek_token (parser->lexer)->location;
9072                     warning_at (loc,
9073                                 OPT_Wempty_body, "suggest braces around "
9074                                 "empty body in an %<else%> statement");
9075                     add_stmt (build_empty_stmt (loc));
9076                     cp_lexer_consume_token (parser->lexer);
9077                   }
9078                 else
9079                   cp_parser_implicitly_scoped_statement (parser, NULL);
9080
9081                 finish_else_clause (statement);
9082
9083                 /* If we are currently parsing a then-clause, then
9084                    IF_P will not be NULL.  We set it to true to
9085                    indicate that this if statement has an else clause.
9086                    This may trigger the Wparentheses warning below
9087                    when we get back up to the parent if statement.  */
9088                 if (if_p != NULL)
9089                   *if_p = true;
9090               }
9091             else
9092               {
9093                 /* This if statement does not have an else clause.  If
9094                    NESTED_IF is true, then the then-clause is an if
9095                    statement which does have an else clause.  We warn
9096                    about the potential ambiguity.  */
9097                 if (nested_if)
9098                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9099                               "suggest explicit braces to avoid ambiguous"
9100                               " %<else%>");
9101               }
9102
9103             /* Now we're all done with the if-statement.  */
9104             finish_if_stmt (statement);
9105           }
9106         else
9107           {
9108             bool in_switch_statement_p;
9109             unsigned char in_statement;
9110
9111             /* Add the condition.  */
9112             finish_switch_cond (condition, statement);
9113
9114             /* Parse the body of the switch-statement.  */
9115             in_switch_statement_p = parser->in_switch_statement_p;
9116             in_statement = parser->in_statement;
9117             parser->in_switch_statement_p = true;
9118             parser->in_statement |= IN_SWITCH_STMT;
9119             cp_parser_implicitly_scoped_statement (parser, NULL);
9120             parser->in_switch_statement_p = in_switch_statement_p;
9121             parser->in_statement = in_statement;
9122
9123             /* Now we're all done with the switch-statement.  */
9124             finish_switch_stmt (statement);
9125           }
9126
9127         return statement;
9128       }
9129       break;
9130
9131     default:
9132       cp_parser_error (parser, "expected selection-statement");
9133       return error_mark_node;
9134     }
9135 }
9136
9137 /* Parse a condition.
9138
9139    condition:
9140      expression
9141      type-specifier-seq declarator = initializer-clause
9142      type-specifier-seq declarator braced-init-list
9143
9144    GNU Extension:
9145
9146    condition:
9147      type-specifier-seq declarator asm-specification [opt]
9148        attributes [opt] = assignment-expression
9149
9150    Returns the expression that should be tested.  */
9151
9152 static tree
9153 cp_parser_condition (cp_parser* parser)
9154 {
9155   cp_decl_specifier_seq type_specifiers;
9156   const char *saved_message;
9157   int declares_class_or_enum;
9158
9159   /* Try the declaration first.  */
9160   cp_parser_parse_tentatively (parser);
9161   /* New types are not allowed in the type-specifier-seq for a
9162      condition.  */
9163   saved_message = parser->type_definition_forbidden_message;
9164   parser->type_definition_forbidden_message
9165     = G_("types may not be defined in conditions");
9166   /* Parse the type-specifier-seq.  */
9167   cp_parser_decl_specifier_seq (parser,
9168                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9169                                 &type_specifiers,
9170                                 &declares_class_or_enum);
9171   /* Restore the saved message.  */
9172   parser->type_definition_forbidden_message = saved_message;
9173   /* If all is well, we might be looking at a declaration.  */
9174   if (!cp_parser_error_occurred (parser))
9175     {
9176       tree decl;
9177       tree asm_specification;
9178       tree attributes;
9179       cp_declarator *declarator;
9180       tree initializer = NULL_TREE;
9181
9182       /* Parse the declarator.  */
9183       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9184                                          /*ctor_dtor_or_conv_p=*/NULL,
9185                                          /*parenthesized_p=*/NULL,
9186                                          /*member_p=*/false);
9187       /* Parse the attributes.  */
9188       attributes = cp_parser_attributes_opt (parser);
9189       /* Parse the asm-specification.  */
9190       asm_specification = cp_parser_asm_specification_opt (parser);
9191       /* If the next token is not an `=' or '{', then we might still be
9192          looking at an expression.  For example:
9193
9194            if (A(a).x)
9195
9196          looks like a decl-specifier-seq and a declarator -- but then
9197          there is no `=', so this is an expression.  */
9198       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9199           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9200         cp_parser_simulate_error (parser);
9201         
9202       /* If we did see an `=' or '{', then we are looking at a declaration
9203          for sure.  */
9204       if (cp_parser_parse_definitely (parser))
9205         {
9206           tree pushed_scope;
9207           bool non_constant_p;
9208           bool flags = LOOKUP_ONLYCONVERTING;
9209
9210           /* Create the declaration.  */
9211           decl = start_decl (declarator, &type_specifiers,
9212                              /*initialized_p=*/true,
9213                              attributes, /*prefix_attributes=*/NULL_TREE,
9214                              &pushed_scope);
9215
9216           /* Parse the initializer.  */
9217           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9218             {
9219               initializer = cp_parser_braced_list (parser, &non_constant_p);
9220               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9221               flags = 0;
9222             }
9223           else
9224             {
9225               /* Consume the `='.  */
9226               cp_parser_require (parser, CPP_EQ, RT_EQ);
9227               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9228             }
9229           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9230             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9231
9232           /* Process the initializer.  */
9233           cp_finish_decl (decl,
9234                           initializer, !non_constant_p,
9235                           asm_specification,
9236                           flags);
9237
9238           if (pushed_scope)
9239             pop_scope (pushed_scope);
9240
9241           return convert_from_reference (decl);
9242         }
9243     }
9244   /* If we didn't even get past the declarator successfully, we are
9245      definitely not looking at a declaration.  */
9246   else
9247     cp_parser_abort_tentative_parse (parser);
9248
9249   /* Otherwise, we are looking at an expression.  */
9250   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9251 }
9252
9253 /* Parses a for-statement or range-for-statement until the closing ')',
9254    not included. */
9255
9256 static tree
9257 cp_parser_for (cp_parser *parser)
9258 {
9259   tree init, scope, decl;
9260   bool is_range_for;
9261
9262   /* Begin the for-statement.  */
9263   scope = begin_for_scope (&init);
9264
9265   /* Parse the initialization.  */
9266   is_range_for = cp_parser_for_init_statement (parser, &decl);
9267
9268   if (is_range_for)
9269     return cp_parser_range_for (parser, scope, init, decl);
9270   else
9271     return cp_parser_c_for (parser, scope, init);
9272 }
9273
9274 static tree
9275 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9276 {
9277   /* Normal for loop */
9278   tree condition = NULL_TREE;
9279   tree expression = NULL_TREE;
9280   tree stmt;
9281
9282   stmt = begin_for_stmt (scope, init);
9283   /* The for-init-statement has already been parsed in
9284      cp_parser_for_init_statement, so no work is needed here.  */
9285   finish_for_init_stmt (stmt);
9286
9287   /* If there's a condition, process it.  */
9288   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9289     condition = cp_parser_condition (parser);
9290   finish_for_cond (condition, stmt);
9291   /* Look for the `;'.  */
9292   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9293
9294   /* If there's an expression, process it.  */
9295   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9296     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9297   finish_for_expr (expression, stmt);
9298
9299   return stmt;
9300 }
9301
9302 /* Tries to parse a range-based for-statement:
9303
9304   range-based-for:
9305     decl-specifier-seq declarator : expression
9306
9307   The decl-specifier-seq declarator and the `:' are already parsed by
9308   cp_parser_for_init_statement. If processing_template_decl it returns a
9309   newly created RANGE_FOR_STMT; if not, it is converted to a
9310   regular FOR_STMT.  */
9311
9312 static tree
9313 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9314 {
9315   tree stmt, range_expr;
9316
9317   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9318     {
9319       bool expr_non_constant_p;
9320       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9321     }
9322   else
9323     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9324
9325   /* If in template, STMT is converted to a normal for-statement
9326      at instantiation. If not, it is done just ahead. */
9327   if (processing_template_decl)
9328     {
9329       if (check_for_bare_parameter_packs (range_expr))
9330         range_expr = error_mark_node;
9331       stmt = begin_range_for_stmt (scope, init);
9332       finish_range_for_decl (stmt, range_decl, range_expr);
9333       if (!type_dependent_expression_p (range_expr)
9334           /* do_auto_deduction doesn't mess with template init-lists.  */
9335           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9336         do_range_for_auto_deduction (range_decl, range_expr);
9337     }
9338   else
9339     {
9340       stmt = begin_for_stmt (scope, init);
9341       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9342     }
9343   return stmt;
9344 }
9345
9346 /* Subroutine of cp_convert_range_for: given the initializer expression,
9347    builds up the range temporary.  */
9348
9349 static tree
9350 build_range_temp (tree range_expr)
9351 {
9352   tree range_type, range_temp;
9353
9354   /* Find out the type deduced by the declaration
9355      `auto &&__range = range_expr'.  */
9356   range_type = cp_build_reference_type (make_auto (), true);
9357   range_type = do_auto_deduction (range_type, range_expr,
9358                                   type_uses_auto (range_type));
9359
9360   /* Create the __range variable.  */
9361   range_temp = build_decl (input_location, VAR_DECL,
9362                            get_identifier ("__for_range"), range_type);
9363   TREE_USED (range_temp) = 1;
9364   DECL_ARTIFICIAL (range_temp) = 1;
9365
9366   return range_temp;
9367 }
9368
9369 /* Used by cp_parser_range_for in template context: we aren't going to
9370    do a full conversion yet, but we still need to resolve auto in the
9371    type of the for-range-declaration if present.  This is basically
9372    a shortcut version of cp_convert_range_for.  */
9373
9374 static void
9375 do_range_for_auto_deduction (tree decl, tree range_expr)
9376 {
9377   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9378   if (auto_node)
9379     {
9380       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9381       range_temp = convert_from_reference (build_range_temp (range_expr));
9382       iter_type = (cp_parser_perform_range_for_lookup
9383                    (range_temp, &begin_dummy, &end_dummy));
9384       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9385       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9386                                         tf_warning_or_error);
9387       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9388                                             iter_decl, auto_node);
9389     }
9390 }
9391
9392 /* Converts a range-based for-statement into a normal
9393    for-statement, as per the definition.
9394
9395       for (RANGE_DECL : RANGE_EXPR)
9396         BLOCK
9397
9398    should be equivalent to:
9399
9400       {
9401         auto &&__range = RANGE_EXPR;
9402         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9403               __begin != __end;
9404               ++__begin)
9405           {
9406               RANGE_DECL = *__begin;
9407               BLOCK
9408           }
9409       }
9410
9411    If RANGE_EXPR is an array:
9412         BEGIN_EXPR = __range
9413         END_EXPR = __range + ARRAY_SIZE(__range)
9414    Else if RANGE_EXPR has a member 'begin' or 'end':
9415         BEGIN_EXPR = __range.begin()
9416         END_EXPR = __range.end()
9417    Else:
9418         BEGIN_EXPR = begin(__range)
9419         END_EXPR = end(__range);
9420
9421    If __range has a member 'begin' but not 'end', or vice versa, we must
9422    still use the second alternative (it will surely fail, however).
9423    When calling begin()/end() in the third alternative we must use
9424    argument dependent lookup, but always considering 'std' as an associated
9425    namespace.  */
9426
9427 tree
9428 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9429 {
9430   tree begin, end;
9431   tree iter_type, begin_expr, end_expr;
9432   tree condition, expression;
9433
9434   if (range_decl == error_mark_node || range_expr == error_mark_node)
9435     /* If an error happened previously do nothing or else a lot of
9436        unhelpful errors would be issued.  */
9437     begin_expr = end_expr = iter_type = error_mark_node;
9438   else
9439     {
9440       tree range_temp = build_range_temp (range_expr);
9441       pushdecl (range_temp);
9442       cp_finish_decl (range_temp, range_expr,
9443                       /*is_constant_init*/false, NULL_TREE,
9444                       LOOKUP_ONLYCONVERTING);
9445
9446       range_temp = convert_from_reference (range_temp);
9447       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9448                                                       &begin_expr, &end_expr);
9449     }
9450
9451   /* The new for initialization statement.  */
9452   begin = build_decl (input_location, VAR_DECL,
9453                       get_identifier ("__for_begin"), iter_type);
9454   TREE_USED (begin) = 1;
9455   DECL_ARTIFICIAL (begin) = 1;
9456   pushdecl (begin);
9457   cp_finish_decl (begin, begin_expr,
9458                   /*is_constant_init*/false, NULL_TREE,
9459                   LOOKUP_ONLYCONVERTING);
9460
9461   end = build_decl (input_location, VAR_DECL,
9462                     get_identifier ("__for_end"), iter_type);
9463   TREE_USED (end) = 1;
9464   DECL_ARTIFICIAL (end) = 1;
9465   pushdecl (end);
9466   cp_finish_decl (end, end_expr,
9467                   /*is_constant_init*/false, NULL_TREE,
9468                   LOOKUP_ONLYCONVERTING);
9469
9470   finish_for_init_stmt (statement);
9471
9472   /* The new for condition.  */
9473   condition = build_x_binary_op (NE_EXPR,
9474                                  begin, ERROR_MARK,
9475                                  end, ERROR_MARK,
9476                                  NULL, tf_warning_or_error);
9477   finish_for_cond (condition, statement);
9478
9479   /* The new increment expression.  */
9480   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9481   finish_for_expr (expression, statement);
9482
9483   /* The declaration is initialized with *__begin inside the loop body.  */
9484   cp_finish_decl (range_decl,
9485                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9486                   /*is_constant_init*/false, NULL_TREE,
9487                   LOOKUP_ONLYCONVERTING);
9488
9489   return statement;
9490 }
9491
9492 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9493    We need to solve both at the same time because the method used
9494    depends on the existence of members begin or end.
9495    Returns the type deduced for the iterator expression.  */
9496
9497 static tree
9498 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9499 {
9500   if (error_operand_p (range))
9501     {
9502       *begin = *end = error_mark_node;
9503       return error_mark_node;
9504     }
9505
9506   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9507     {
9508       error ("range-based %<for%> expression of type %qT "
9509              "has incomplete type", TREE_TYPE (range));
9510       *begin = *end = error_mark_node;
9511       return error_mark_node;
9512     }
9513   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9514     {
9515       /* If RANGE is an array, we will use pointer arithmetic.  */
9516       *begin = range;
9517       *end = build_binary_op (input_location, PLUS_EXPR,
9518                               range,
9519                               array_type_nelts_top (TREE_TYPE (range)),
9520                               0);
9521       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9522     }
9523   else
9524     {
9525       /* If it is not an array, we must do a bit of magic.  */
9526       tree id_begin, id_end;
9527       tree member_begin, member_end;
9528
9529       *begin = *end = error_mark_node;
9530
9531       id_begin = get_identifier ("begin");
9532       id_end = get_identifier ("end");
9533       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9534                                     /*protect=*/2, /*want_type=*/false,
9535                                     tf_warning_or_error);
9536       member_end = lookup_member (TREE_TYPE (range), id_end,
9537                                   /*protect=*/2, /*want_type=*/false,
9538                                   tf_warning_or_error);
9539
9540       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9541         {
9542           /* Use the member functions.  */
9543           if (member_begin != NULL_TREE)
9544             *begin = cp_parser_range_for_member_function (range, id_begin);
9545           else
9546             error ("range-based %<for%> expression of type %qT has an "
9547                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9548
9549           if (member_end != NULL_TREE)
9550             *end = cp_parser_range_for_member_function (range, id_end);
9551           else
9552             error ("range-based %<for%> expression of type %qT has a "
9553                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9554         }
9555       else
9556         {
9557           /* Use global functions with ADL.  */
9558           VEC(tree,gc) *vec;
9559           vec = make_tree_vector ();
9560
9561           VEC_safe_push (tree, gc, vec, range);
9562
9563           member_begin = perform_koenig_lookup (id_begin, vec,
9564                                                 /*include_std=*/true,
9565                                                 tf_warning_or_error);
9566           *begin = finish_call_expr (member_begin, &vec, false, true,
9567                                      tf_warning_or_error);
9568           member_end = perform_koenig_lookup (id_end, vec,
9569                                               /*include_std=*/true,
9570                                               tf_warning_or_error);
9571           *end = finish_call_expr (member_end, &vec, false, true,
9572                                    tf_warning_or_error);
9573
9574           release_tree_vector (vec);
9575         }
9576
9577       /* Last common checks.  */
9578       if (*begin == error_mark_node || *end == error_mark_node)
9579         {
9580           /* If one of the expressions is an error do no more checks.  */
9581           *begin = *end = error_mark_node;
9582           return error_mark_node;
9583         }
9584       else
9585         {
9586           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9587           /* The unqualified type of the __begin and __end temporaries should
9588              be the same, as required by the multiple auto declaration.  */
9589           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9590             error ("inconsistent begin/end types in range-based %<for%> "
9591                    "statement: %qT and %qT",
9592                    TREE_TYPE (*begin), TREE_TYPE (*end));
9593           return iter_type;
9594         }
9595     }
9596 }
9597
9598 /* Helper function for cp_parser_perform_range_for_lookup.
9599    Builds a tree for RANGE.IDENTIFIER().  */
9600
9601 static tree
9602 cp_parser_range_for_member_function (tree range, tree identifier)
9603 {
9604   tree member, res;
9605   VEC(tree,gc) *vec;
9606
9607   member = finish_class_member_access_expr (range, identifier,
9608                                             false, tf_warning_or_error);
9609   if (member == error_mark_node)
9610     return error_mark_node;
9611
9612   vec = make_tree_vector ();
9613   res = finish_call_expr (member, &vec,
9614                           /*disallow_virtual=*/false,
9615                           /*koenig_p=*/false,
9616                           tf_warning_or_error);
9617   release_tree_vector (vec);
9618   return res;
9619 }
9620
9621 /* Parse an iteration-statement.
9622
9623    iteration-statement:
9624      while ( condition ) statement
9625      do statement while ( expression ) ;
9626      for ( for-init-statement condition [opt] ; expression [opt] )
9627        statement
9628
9629    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9630
9631 static tree
9632 cp_parser_iteration_statement (cp_parser* parser)
9633 {
9634   cp_token *token;
9635   enum rid keyword;
9636   tree statement;
9637   unsigned char in_statement;
9638
9639   /* Peek at the next token.  */
9640   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9641   if (!token)
9642     return error_mark_node;
9643
9644   /* Remember whether or not we are already within an iteration
9645      statement.  */
9646   in_statement = parser->in_statement;
9647
9648   /* See what kind of keyword it is.  */
9649   keyword = token->keyword;
9650   switch (keyword)
9651     {
9652     case RID_WHILE:
9653       {
9654         tree condition;
9655
9656         /* Begin the while-statement.  */
9657         statement = begin_while_stmt ();
9658         /* Look for the `('.  */
9659         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9660         /* Parse the condition.  */
9661         condition = cp_parser_condition (parser);
9662         finish_while_stmt_cond (condition, statement);
9663         /* Look for the `)'.  */
9664         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9665         /* Parse the dependent statement.  */
9666         parser->in_statement = IN_ITERATION_STMT;
9667         cp_parser_already_scoped_statement (parser);
9668         parser->in_statement = in_statement;
9669         /* We're done with the while-statement.  */
9670         finish_while_stmt (statement);
9671       }
9672       break;
9673
9674     case RID_DO:
9675       {
9676         tree expression;
9677
9678         /* Begin the do-statement.  */
9679         statement = begin_do_stmt ();
9680         /* Parse the body of the do-statement.  */
9681         parser->in_statement = IN_ITERATION_STMT;
9682         cp_parser_implicitly_scoped_statement (parser, NULL);
9683         parser->in_statement = in_statement;
9684         finish_do_body (statement);
9685         /* Look for the `while' keyword.  */
9686         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9687         /* Look for the `('.  */
9688         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9689         /* Parse the expression.  */
9690         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9691         /* We're done with the do-statement.  */
9692         finish_do_stmt (expression, statement);
9693         /* Look for the `)'.  */
9694         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9695         /* Look for the `;'.  */
9696         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9697       }
9698       break;
9699
9700     case RID_FOR:
9701       {
9702         /* Look for the `('.  */
9703         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9704
9705         statement = cp_parser_for (parser);
9706
9707         /* Look for the `)'.  */
9708         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9709
9710         /* Parse the body of the for-statement.  */
9711         parser->in_statement = IN_ITERATION_STMT;
9712         cp_parser_already_scoped_statement (parser);
9713         parser->in_statement = in_statement;
9714
9715         /* We're done with the for-statement.  */
9716         finish_for_stmt (statement);
9717       }
9718       break;
9719
9720     default:
9721       cp_parser_error (parser, "expected iteration-statement");
9722       statement = error_mark_node;
9723       break;
9724     }
9725
9726   return statement;
9727 }
9728
9729 /* Parse a for-init-statement or the declarator of a range-based-for.
9730    Returns true if a range-based-for declaration is seen.
9731
9732    for-init-statement:
9733      expression-statement
9734      simple-declaration  */
9735
9736 static bool
9737 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9738 {
9739   /* If the next token is a `;', then we have an empty
9740      expression-statement.  Grammatically, this is also a
9741      simple-declaration, but an invalid one, because it does not
9742      declare anything.  Therefore, if we did not handle this case
9743      specially, we would issue an error message about an invalid
9744      declaration.  */
9745   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9746     {
9747       bool is_range_for = false;
9748       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9749
9750       parser->colon_corrects_to_scope_p = false;
9751
9752       /* We're going to speculatively look for a declaration, falling back
9753          to an expression, if necessary.  */
9754       cp_parser_parse_tentatively (parser);
9755       /* Parse the declaration.  */
9756       cp_parser_simple_declaration (parser,
9757                                     /*function_definition_allowed_p=*/false,
9758                                     decl);
9759       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9760       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9761         {
9762           /* It is a range-for, consume the ':' */
9763           cp_lexer_consume_token (parser->lexer);
9764           is_range_for = true;
9765           if (cxx_dialect < cxx0x)
9766             {
9767               error_at (cp_lexer_peek_token (parser->lexer)->location,
9768                         "range-based %<for%> loops are not allowed "
9769                         "in C++98 mode");
9770               *decl = error_mark_node;
9771             }
9772         }
9773       else
9774           /* The ';' is not consumed yet because we told
9775              cp_parser_simple_declaration not to.  */
9776           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9777
9778       if (cp_parser_parse_definitely (parser))
9779         return is_range_for;
9780       /* If the tentative parse failed, then we shall need to look for an
9781          expression-statement.  */
9782     }
9783   /* If we are here, it is an expression-statement.  */
9784   cp_parser_expression_statement (parser, NULL_TREE);
9785   return false;
9786 }
9787
9788 /* Parse a jump-statement.
9789
9790    jump-statement:
9791      break ;
9792      continue ;
9793      return expression [opt] ;
9794      return braced-init-list ;
9795      goto identifier ;
9796
9797    GNU extension:
9798
9799    jump-statement:
9800      goto * expression ;
9801
9802    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9803
9804 static tree
9805 cp_parser_jump_statement (cp_parser* parser)
9806 {
9807   tree statement = error_mark_node;
9808   cp_token *token;
9809   enum rid keyword;
9810   unsigned char in_statement;
9811
9812   /* Peek at the next token.  */
9813   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9814   if (!token)
9815     return error_mark_node;
9816
9817   /* See what kind of keyword it is.  */
9818   keyword = token->keyword;
9819   switch (keyword)
9820     {
9821     case RID_BREAK:
9822       in_statement = parser->in_statement & ~IN_IF_STMT;      
9823       switch (in_statement)
9824         {
9825         case 0:
9826           error_at (token->location, "break statement not within loop or switch");
9827           break;
9828         default:
9829           gcc_assert ((in_statement & IN_SWITCH_STMT)
9830                       || in_statement == IN_ITERATION_STMT);
9831           statement = finish_break_stmt ();
9832           break;
9833         case IN_OMP_BLOCK:
9834           error_at (token->location, "invalid exit from OpenMP structured block");
9835           break;
9836         case IN_OMP_FOR:
9837           error_at (token->location, "break statement used with OpenMP for loop");
9838           break;
9839         }
9840       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9841       break;
9842
9843     case RID_CONTINUE:
9844       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9845         {
9846         case 0:
9847           error_at (token->location, "continue statement not within a loop");
9848           break;
9849         case IN_ITERATION_STMT:
9850         case IN_OMP_FOR:
9851           statement = finish_continue_stmt ();
9852           break;
9853         case IN_OMP_BLOCK:
9854           error_at (token->location, "invalid exit from OpenMP structured block");
9855           break;
9856         default:
9857           gcc_unreachable ();
9858         }
9859       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9860       break;
9861
9862     case RID_RETURN:
9863       {
9864         tree expr;
9865         bool expr_non_constant_p;
9866
9867         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9868           {
9869             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9870             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9871           }
9872         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9873           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9874         else
9875           /* If the next token is a `;', then there is no
9876              expression.  */
9877           expr = NULL_TREE;
9878         /* Build the return-statement.  */
9879         statement = finish_return_stmt (expr);
9880         /* Look for the final `;'.  */
9881         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9882       }
9883       break;
9884
9885     case RID_GOTO:
9886       /* Create the goto-statement.  */
9887       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9888         {
9889           /* Issue a warning about this use of a GNU extension.  */
9890           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9891           /* Consume the '*' token.  */
9892           cp_lexer_consume_token (parser->lexer);
9893           /* Parse the dependent expression.  */
9894           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9895         }
9896       else
9897         finish_goto_stmt (cp_parser_identifier (parser));
9898       /* Look for the final `;'.  */
9899       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9900       break;
9901
9902     default:
9903       cp_parser_error (parser, "expected jump-statement");
9904       break;
9905     }
9906
9907   return statement;
9908 }
9909
9910 /* Parse a declaration-statement.
9911
9912    declaration-statement:
9913      block-declaration  */
9914
9915 static void
9916 cp_parser_declaration_statement (cp_parser* parser)
9917 {
9918   void *p;
9919
9920   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9921   p = obstack_alloc (&declarator_obstack, 0);
9922
9923  /* Parse the block-declaration.  */
9924   cp_parser_block_declaration (parser, /*statement_p=*/true);
9925
9926   /* Free any declarators allocated.  */
9927   obstack_free (&declarator_obstack, p);
9928
9929   /* Finish off the statement.  */
9930   finish_stmt ();
9931 }
9932
9933 /* Some dependent statements (like `if (cond) statement'), are
9934    implicitly in their own scope.  In other words, if the statement is
9935    a single statement (as opposed to a compound-statement), it is
9936    none-the-less treated as if it were enclosed in braces.  Any
9937    declarations appearing in the dependent statement are out of scope
9938    after control passes that point.  This function parses a statement,
9939    but ensures that is in its own scope, even if it is not a
9940    compound-statement.
9941
9942    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9943    is a (possibly labeled) if statement which is not enclosed in
9944    braces and has an else clause.  This is used to implement
9945    -Wparentheses.
9946
9947    Returns the new statement.  */
9948
9949 static tree
9950 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9951 {
9952   tree statement;
9953
9954   if (if_p != NULL)
9955     *if_p = false;
9956
9957   /* Mark if () ; with a special NOP_EXPR.  */
9958   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9959     {
9960       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9961       cp_lexer_consume_token (parser->lexer);
9962       statement = add_stmt (build_empty_stmt (loc));
9963     }
9964   /* if a compound is opened, we simply parse the statement directly.  */
9965   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9966     statement = cp_parser_compound_statement (parser, NULL, false, false);
9967   /* If the token is not a `{', then we must take special action.  */
9968   else
9969     {
9970       /* Create a compound-statement.  */
9971       statement = begin_compound_stmt (0);
9972       /* Parse the dependent-statement.  */
9973       cp_parser_statement (parser, NULL_TREE, false, if_p);
9974       /* Finish the dummy compound-statement.  */
9975       finish_compound_stmt (statement);
9976     }
9977
9978   /* Return the statement.  */
9979   return statement;
9980 }
9981
9982 /* For some dependent statements (like `while (cond) statement'), we
9983    have already created a scope.  Therefore, even if the dependent
9984    statement is a compound-statement, we do not want to create another
9985    scope.  */
9986
9987 static void
9988 cp_parser_already_scoped_statement (cp_parser* parser)
9989 {
9990   /* If the token is a `{', then we must take special action.  */
9991   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9992     cp_parser_statement (parser, NULL_TREE, false, NULL);
9993   else
9994     {
9995       /* Avoid calling cp_parser_compound_statement, so that we
9996          don't create a new scope.  Do everything else by hand.  */
9997       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9998       /* If the next keyword is `__label__' we have a label declaration.  */
9999       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
10000         cp_parser_label_declaration (parser);
10001       /* Parse an (optional) statement-seq.  */
10002       cp_parser_statement_seq_opt (parser, NULL_TREE);
10003       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10004     }
10005 }
10006
10007 /* Declarations [gram.dcl.dcl] */
10008
10009 /* Parse an optional declaration-sequence.
10010
10011    declaration-seq:
10012      declaration
10013      declaration-seq declaration  */
10014
10015 static void
10016 cp_parser_declaration_seq_opt (cp_parser* parser)
10017 {
10018   while (true)
10019     {
10020       cp_token *token;
10021
10022       token = cp_lexer_peek_token (parser->lexer);
10023
10024       if (token->type == CPP_CLOSE_BRACE
10025           || token->type == CPP_EOF
10026           || token->type == CPP_PRAGMA_EOL)
10027         break;
10028
10029       if (token->type == CPP_SEMICOLON)
10030         {
10031           /* A declaration consisting of a single semicolon is
10032              invalid.  Allow it unless we're being pedantic.  */
10033           cp_lexer_consume_token (parser->lexer);
10034           if (!in_system_header)
10035             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10036           continue;
10037         }
10038
10039       /* If we're entering or exiting a region that's implicitly
10040          extern "C", modify the lang context appropriately.  */
10041       if (!parser->implicit_extern_c && token->implicit_extern_c)
10042         {
10043           push_lang_context (lang_name_c);
10044           parser->implicit_extern_c = true;
10045         }
10046       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10047         {
10048           pop_lang_context ();
10049           parser->implicit_extern_c = false;
10050         }
10051
10052       if (token->type == CPP_PRAGMA)
10053         {
10054           /* A top-level declaration can consist solely of a #pragma.
10055              A nested declaration cannot, so this is done here and not
10056              in cp_parser_declaration.  (A #pragma at block scope is
10057              handled in cp_parser_statement.)  */
10058           cp_parser_pragma (parser, pragma_external);
10059           continue;
10060         }
10061
10062       /* Parse the declaration itself.  */
10063       cp_parser_declaration (parser);
10064     }
10065 }
10066
10067 /* Parse a declaration.
10068
10069    declaration:
10070      block-declaration
10071      function-definition
10072      template-declaration
10073      explicit-instantiation
10074      explicit-specialization
10075      linkage-specification
10076      namespace-definition
10077
10078    GNU extension:
10079
10080    declaration:
10081       __extension__ declaration */
10082
10083 static void
10084 cp_parser_declaration (cp_parser* parser)
10085 {
10086   cp_token token1;
10087   cp_token token2;
10088   int saved_pedantic;
10089   void *p;
10090   tree attributes = NULL_TREE;
10091
10092   /* Check for the `__extension__' keyword.  */
10093   if (cp_parser_extension_opt (parser, &saved_pedantic))
10094     {
10095       /* Parse the qualified declaration.  */
10096       cp_parser_declaration (parser);
10097       /* Restore the PEDANTIC flag.  */
10098       pedantic = saved_pedantic;
10099
10100       return;
10101     }
10102
10103   /* Try to figure out what kind of declaration is present.  */
10104   token1 = *cp_lexer_peek_token (parser->lexer);
10105
10106   if (token1.type != CPP_EOF)
10107     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10108   else
10109     {
10110       token2.type = CPP_EOF;
10111       token2.keyword = RID_MAX;
10112     }
10113
10114   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10115   p = obstack_alloc (&declarator_obstack, 0);
10116
10117   /* If the next token is `extern' and the following token is a string
10118      literal, then we have a linkage specification.  */
10119   if (token1.keyword == RID_EXTERN
10120       && cp_parser_is_pure_string_literal (&token2))
10121     cp_parser_linkage_specification (parser);
10122   /* If the next token is `template', then we have either a template
10123      declaration, an explicit instantiation, or an explicit
10124      specialization.  */
10125   else if (token1.keyword == RID_TEMPLATE)
10126     {
10127       /* `template <>' indicates a template specialization.  */
10128       if (token2.type == CPP_LESS
10129           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10130         cp_parser_explicit_specialization (parser);
10131       /* `template <' indicates a template declaration.  */
10132       else if (token2.type == CPP_LESS)
10133         cp_parser_template_declaration (parser, /*member_p=*/false);
10134       /* Anything else must be an explicit instantiation.  */
10135       else
10136         cp_parser_explicit_instantiation (parser);
10137     }
10138   /* If the next token is `export', then we have a template
10139      declaration.  */
10140   else if (token1.keyword == RID_EXPORT)
10141     cp_parser_template_declaration (parser, /*member_p=*/false);
10142   /* If the next token is `extern', 'static' or 'inline' and the one
10143      after that is `template', we have a GNU extended explicit
10144      instantiation directive.  */
10145   else if (cp_parser_allow_gnu_extensions_p (parser)
10146            && (token1.keyword == RID_EXTERN
10147                || token1.keyword == RID_STATIC
10148                || token1.keyword == RID_INLINE)
10149            && token2.keyword == RID_TEMPLATE)
10150     cp_parser_explicit_instantiation (parser);
10151   /* If the next token is `namespace', check for a named or unnamed
10152      namespace definition.  */
10153   else if (token1.keyword == RID_NAMESPACE
10154            && (/* A named namespace definition.  */
10155                (token2.type == CPP_NAME
10156                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10157                     != CPP_EQ))
10158                /* An unnamed namespace definition.  */
10159                || token2.type == CPP_OPEN_BRACE
10160                || token2.keyword == RID_ATTRIBUTE))
10161     cp_parser_namespace_definition (parser);
10162   /* An inline (associated) namespace definition.  */
10163   else if (token1.keyword == RID_INLINE
10164            && token2.keyword == RID_NAMESPACE)
10165     cp_parser_namespace_definition (parser);
10166   /* Objective-C++ declaration/definition.  */
10167   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10168     cp_parser_objc_declaration (parser, NULL_TREE);
10169   else if (c_dialect_objc ()
10170            && token1.keyword == RID_ATTRIBUTE
10171            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10172     cp_parser_objc_declaration (parser, attributes);
10173   /* We must have either a block declaration or a function
10174      definition.  */
10175   else
10176     /* Try to parse a block-declaration, or a function-definition.  */
10177     cp_parser_block_declaration (parser, /*statement_p=*/false);
10178
10179   /* Free any declarators allocated.  */
10180   obstack_free (&declarator_obstack, p);
10181 }
10182
10183 /* Parse a block-declaration.
10184
10185    block-declaration:
10186      simple-declaration
10187      asm-definition
10188      namespace-alias-definition
10189      using-declaration
10190      using-directive
10191
10192    GNU Extension:
10193
10194    block-declaration:
10195      __extension__ block-declaration
10196
10197    C++0x Extension:
10198
10199    block-declaration:
10200      static_assert-declaration
10201
10202    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10203    part of a declaration-statement.  */
10204
10205 static void
10206 cp_parser_block_declaration (cp_parser *parser,
10207                              bool      statement_p)
10208 {
10209   cp_token *token1;
10210   int saved_pedantic;
10211
10212   /* Check for the `__extension__' keyword.  */
10213   if (cp_parser_extension_opt (parser, &saved_pedantic))
10214     {
10215       /* Parse the qualified declaration.  */
10216       cp_parser_block_declaration (parser, statement_p);
10217       /* Restore the PEDANTIC flag.  */
10218       pedantic = saved_pedantic;
10219
10220       return;
10221     }
10222
10223   /* Peek at the next token to figure out which kind of declaration is
10224      present.  */
10225   token1 = cp_lexer_peek_token (parser->lexer);
10226
10227   /* If the next keyword is `asm', we have an asm-definition.  */
10228   if (token1->keyword == RID_ASM)
10229     {
10230       if (statement_p)
10231         cp_parser_commit_to_tentative_parse (parser);
10232       cp_parser_asm_definition (parser);
10233     }
10234   /* If the next keyword is `namespace', we have a
10235      namespace-alias-definition.  */
10236   else if (token1->keyword == RID_NAMESPACE)
10237     cp_parser_namespace_alias_definition (parser);
10238   /* If the next keyword is `using', we have a
10239      using-declaration, a using-directive, or an alias-declaration.  */
10240   else if (token1->keyword == RID_USING)
10241     {
10242       cp_token *token2;
10243
10244       if (statement_p)
10245         cp_parser_commit_to_tentative_parse (parser);
10246       /* If the token after `using' is `namespace', then we have a
10247          using-directive.  */
10248       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10249       if (token2->keyword == RID_NAMESPACE)
10250         cp_parser_using_directive (parser);
10251       /* If the second token after 'using' is '=', then we have an
10252          alias-declaration.  */
10253       else if (cxx_dialect >= cxx0x
10254                && token2->type == CPP_NAME
10255                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10256                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10257                        == RID_ATTRIBUTE)))
10258         cp_parser_alias_declaration (parser);
10259       /* Otherwise, it's a using-declaration.  */
10260       else
10261         cp_parser_using_declaration (parser,
10262                                      /*access_declaration_p=*/false);
10263     }
10264   /* If the next keyword is `__label__' we have a misplaced label
10265      declaration.  */
10266   else if (token1->keyword == RID_LABEL)
10267     {
10268       cp_lexer_consume_token (parser->lexer);
10269       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10270       cp_parser_skip_to_end_of_statement (parser);
10271       /* If the next token is now a `;', consume it.  */
10272       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10273         cp_lexer_consume_token (parser->lexer);
10274     }
10275   /* If the next token is `static_assert' we have a static assertion.  */
10276   else if (token1->keyword == RID_STATIC_ASSERT)
10277     cp_parser_static_assert (parser, /*member_p=*/false);
10278   /* Anything else must be a simple-declaration.  */
10279   else
10280     cp_parser_simple_declaration (parser, !statement_p,
10281                                   /*maybe_range_for_decl*/NULL);
10282 }
10283
10284 /* Parse a simple-declaration.
10285
10286    simple-declaration:
10287      decl-specifier-seq [opt] init-declarator-list [opt] ;
10288
10289    init-declarator-list:
10290      init-declarator
10291      init-declarator-list , init-declarator
10292
10293    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10294    function-definition as a simple-declaration.
10295
10296    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10297    parsed declaration if it is an uninitialized single declarator not followed
10298    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10299    if present, will not be consumed.  */
10300
10301 static void
10302 cp_parser_simple_declaration (cp_parser* parser,
10303                               bool function_definition_allowed_p,
10304                               tree *maybe_range_for_decl)
10305 {
10306   cp_decl_specifier_seq decl_specifiers;
10307   int declares_class_or_enum;
10308   bool saw_declarator;
10309
10310   if (maybe_range_for_decl)
10311     *maybe_range_for_decl = NULL_TREE;
10312
10313   /* Defer access checks until we know what is being declared; the
10314      checks for names appearing in the decl-specifier-seq should be
10315      done as if we were in the scope of the thing being declared.  */
10316   push_deferring_access_checks (dk_deferred);
10317
10318   /* Parse the decl-specifier-seq.  We have to keep track of whether
10319      or not the decl-specifier-seq declares a named class or
10320      enumeration type, since that is the only case in which the
10321      init-declarator-list is allowed to be empty.
10322
10323      [dcl.dcl]
10324
10325      In a simple-declaration, the optional init-declarator-list can be
10326      omitted only when declaring a class or enumeration, that is when
10327      the decl-specifier-seq contains either a class-specifier, an
10328      elaborated-type-specifier, or an enum-specifier.  */
10329   cp_parser_decl_specifier_seq (parser,
10330                                 CP_PARSER_FLAGS_OPTIONAL,
10331                                 &decl_specifiers,
10332                                 &declares_class_or_enum);
10333   /* We no longer need to defer access checks.  */
10334   stop_deferring_access_checks ();
10335
10336   /* In a block scope, a valid declaration must always have a
10337      decl-specifier-seq.  By not trying to parse declarators, we can
10338      resolve the declaration/expression ambiguity more quickly.  */
10339   if (!function_definition_allowed_p
10340       && !decl_specifiers.any_specifiers_p)
10341     {
10342       cp_parser_error (parser, "expected declaration");
10343       goto done;
10344     }
10345
10346   /* If the next two tokens are both identifiers, the code is
10347      erroneous. The usual cause of this situation is code like:
10348
10349        T t;
10350
10351      where "T" should name a type -- but does not.  */
10352   if (!decl_specifiers.any_type_specifiers_p
10353       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10354     {
10355       /* If parsing tentatively, we should commit; we really are
10356          looking at a declaration.  */
10357       cp_parser_commit_to_tentative_parse (parser);
10358       /* Give up.  */
10359       goto done;
10360     }
10361
10362   /* If we have seen at least one decl-specifier, and the next token
10363      is not a parenthesis, then we must be looking at a declaration.
10364      (After "int (" we might be looking at a functional cast.)  */
10365   if (decl_specifiers.any_specifiers_p
10366       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10367       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10368       && !cp_parser_error_occurred (parser))
10369     cp_parser_commit_to_tentative_parse (parser);
10370
10371   /* Keep going until we hit the `;' at the end of the simple
10372      declaration.  */
10373   saw_declarator = false;
10374   while (cp_lexer_next_token_is_not (parser->lexer,
10375                                      CPP_SEMICOLON))
10376     {
10377       cp_token *token;
10378       bool function_definition_p;
10379       tree decl;
10380
10381       if (saw_declarator)
10382         {
10383           /* If we are processing next declarator, coma is expected */
10384           token = cp_lexer_peek_token (parser->lexer);
10385           gcc_assert (token->type == CPP_COMMA);
10386           cp_lexer_consume_token (parser->lexer);
10387           if (maybe_range_for_decl)
10388             *maybe_range_for_decl = error_mark_node;
10389         }
10390       else
10391         saw_declarator = true;
10392
10393       /* Parse the init-declarator.  */
10394       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10395                                         /*checks=*/NULL,
10396                                         function_definition_allowed_p,
10397                                         /*member_p=*/false,
10398                                         declares_class_or_enum,
10399                                         &function_definition_p,
10400                                         maybe_range_for_decl);
10401       /* If an error occurred while parsing tentatively, exit quickly.
10402          (That usually happens when in the body of a function; each
10403          statement is treated as a declaration-statement until proven
10404          otherwise.)  */
10405       if (cp_parser_error_occurred (parser))
10406         goto done;
10407       /* Handle function definitions specially.  */
10408       if (function_definition_p)
10409         {
10410           /* If the next token is a `,', then we are probably
10411              processing something like:
10412
10413                void f() {}, *p;
10414
10415              which is erroneous.  */
10416           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10417             {
10418               cp_token *token = cp_lexer_peek_token (parser->lexer);
10419               error_at (token->location,
10420                         "mixing"
10421                         " declarations and function-definitions is forbidden");
10422             }
10423           /* Otherwise, we're done with the list of declarators.  */
10424           else
10425             {
10426               pop_deferring_access_checks ();
10427               return;
10428             }
10429         }
10430       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10431         *maybe_range_for_decl = decl;
10432       /* The next token should be either a `,' or a `;'.  */
10433       token = cp_lexer_peek_token (parser->lexer);
10434       /* If it's a `,', there are more declarators to come.  */
10435       if (token->type == CPP_COMMA)
10436         /* will be consumed next time around */;
10437       /* If it's a `;', we are done.  */
10438       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10439         break;
10440       /* Anything else is an error.  */
10441       else
10442         {
10443           /* If we have already issued an error message we don't need
10444              to issue another one.  */
10445           if (decl != error_mark_node
10446               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10447             cp_parser_error (parser, "expected %<,%> or %<;%>");
10448           /* Skip tokens until we reach the end of the statement.  */
10449           cp_parser_skip_to_end_of_statement (parser);
10450           /* If the next token is now a `;', consume it.  */
10451           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10452             cp_lexer_consume_token (parser->lexer);
10453           goto done;
10454         }
10455       /* After the first time around, a function-definition is not
10456          allowed -- even if it was OK at first.  For example:
10457
10458            int i, f() {}
10459
10460          is not valid.  */
10461       function_definition_allowed_p = false;
10462     }
10463
10464   /* Issue an error message if no declarators are present, and the
10465      decl-specifier-seq does not itself declare a class or
10466      enumeration.  */
10467   if (!saw_declarator)
10468     {
10469       if (cp_parser_declares_only_class_p (parser))
10470         shadow_tag (&decl_specifiers);
10471       /* Perform any deferred access checks.  */
10472       perform_deferred_access_checks ();
10473     }
10474
10475   /* Consume the `;'.  */
10476   if (!maybe_range_for_decl)
10477       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10478
10479  done:
10480   pop_deferring_access_checks ();
10481 }
10482
10483 /* Parse a decl-specifier-seq.
10484
10485    decl-specifier-seq:
10486      decl-specifier-seq [opt] decl-specifier
10487
10488    decl-specifier:
10489      storage-class-specifier
10490      type-specifier
10491      function-specifier
10492      friend
10493      typedef
10494
10495    GNU Extension:
10496
10497    decl-specifier:
10498      attributes
10499
10500    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10501
10502    The parser flags FLAGS is used to control type-specifier parsing.
10503
10504    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10505    flags:
10506
10507      1: one of the decl-specifiers is an elaborated-type-specifier
10508         (i.e., a type declaration)
10509      2: one of the decl-specifiers is an enum-specifier or a
10510         class-specifier (i.e., a type definition)
10511
10512    */
10513
10514 static void
10515 cp_parser_decl_specifier_seq (cp_parser* parser,
10516                               cp_parser_flags flags,
10517                               cp_decl_specifier_seq *decl_specs,
10518                               int* declares_class_or_enum)
10519 {
10520   bool constructor_possible_p = !parser->in_declarator_p;
10521   cp_token *start_token = NULL;
10522
10523   /* Clear DECL_SPECS.  */
10524   clear_decl_specs (decl_specs);
10525
10526   /* Assume no class or enumeration type is declared.  */
10527   *declares_class_or_enum = 0;
10528
10529   /* Keep reading specifiers until there are no more to read.  */
10530   while (true)
10531     {
10532       bool constructor_p;
10533       bool found_decl_spec;
10534       cp_token *token;
10535
10536       /* Peek at the next token.  */
10537       token = cp_lexer_peek_token (parser->lexer);
10538
10539       /* Save the first token of the decl spec list for error
10540          reporting.  */
10541       if (!start_token)
10542         start_token = token;
10543       /* Handle attributes.  */
10544       if (token->keyword == RID_ATTRIBUTE)
10545         {
10546           /* Parse the attributes.  */
10547           decl_specs->attributes
10548             = chainon (decl_specs->attributes,
10549                        cp_parser_attributes_opt (parser));
10550           continue;
10551         }
10552       /* Assume we will find a decl-specifier keyword.  */
10553       found_decl_spec = true;
10554       /* If the next token is an appropriate keyword, we can simply
10555          add it to the list.  */
10556       switch (token->keyword)
10557         {
10558           /* decl-specifier:
10559                friend
10560                constexpr */
10561         case RID_FRIEND:
10562           if (!at_class_scope_p ())
10563             {
10564               error_at (token->location, "%<friend%> used outside of class");
10565               cp_lexer_purge_token (parser->lexer);
10566             }
10567           else
10568             {
10569               ++decl_specs->specs[(int) ds_friend];
10570               /* Consume the token.  */
10571               cp_lexer_consume_token (parser->lexer);
10572             }
10573           break;
10574
10575         case RID_CONSTEXPR:
10576           ++decl_specs->specs[(int) ds_constexpr];
10577           cp_lexer_consume_token (parser->lexer);
10578           break;
10579
10580           /* function-specifier:
10581                inline
10582                virtual
10583                explicit  */
10584         case RID_INLINE:
10585         case RID_VIRTUAL:
10586         case RID_EXPLICIT:
10587           cp_parser_function_specifier_opt (parser, decl_specs);
10588           break;
10589
10590           /* decl-specifier:
10591                typedef  */
10592         case RID_TYPEDEF:
10593           ++decl_specs->specs[(int) ds_typedef];
10594           /* Consume the token.  */
10595           cp_lexer_consume_token (parser->lexer);
10596           /* A constructor declarator cannot appear in a typedef.  */
10597           constructor_possible_p = false;
10598           /* The "typedef" keyword can only occur in a declaration; we
10599              may as well commit at this point.  */
10600           cp_parser_commit_to_tentative_parse (parser);
10601
10602           if (decl_specs->storage_class != sc_none)
10603             decl_specs->conflicting_specifiers_p = true;
10604           break;
10605
10606           /* storage-class-specifier:
10607                auto
10608                register
10609                static
10610                extern
10611                mutable
10612
10613              GNU Extension:
10614                thread  */
10615         case RID_AUTO:
10616           if (cxx_dialect == cxx98) 
10617             {
10618               /* Consume the token.  */
10619               cp_lexer_consume_token (parser->lexer);
10620
10621               /* Complain about `auto' as a storage specifier, if
10622                  we're complaining about C++0x compatibility.  */
10623               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10624                           " changes meaning in C++11; please remove it");
10625
10626               /* Set the storage class anyway.  */
10627               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10628                                            token->location);
10629             }
10630           else
10631             /* C++0x auto type-specifier.  */
10632             found_decl_spec = false;
10633           break;
10634
10635         case RID_REGISTER:
10636         case RID_STATIC:
10637         case RID_EXTERN:
10638         case RID_MUTABLE:
10639           /* Consume the token.  */
10640           cp_lexer_consume_token (parser->lexer);
10641           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10642                                        token->location);
10643           break;
10644         case RID_THREAD:
10645           /* Consume the token.  */
10646           cp_lexer_consume_token (parser->lexer);
10647           ++decl_specs->specs[(int) ds_thread];
10648           break;
10649
10650         default:
10651           /* We did not yet find a decl-specifier yet.  */
10652           found_decl_spec = false;
10653           break;
10654         }
10655
10656       if (found_decl_spec
10657           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10658           && token->keyword != RID_CONSTEXPR)
10659         error ("decl-specifier invalid in condition");
10660
10661       /* Constructors are a special case.  The `S' in `S()' is not a
10662          decl-specifier; it is the beginning of the declarator.  */
10663       constructor_p
10664         = (!found_decl_spec
10665            && constructor_possible_p
10666            && (cp_parser_constructor_declarator_p
10667                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10668
10669       /* If we don't have a DECL_SPEC yet, then we must be looking at
10670          a type-specifier.  */
10671       if (!found_decl_spec && !constructor_p)
10672         {
10673           int decl_spec_declares_class_or_enum;
10674           bool is_cv_qualifier;
10675           tree type_spec;
10676
10677           type_spec
10678             = cp_parser_type_specifier (parser, flags,
10679                                         decl_specs,
10680                                         /*is_declaration=*/true,
10681                                         &decl_spec_declares_class_or_enum,
10682                                         &is_cv_qualifier);
10683           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10684
10685           /* If this type-specifier referenced a user-defined type
10686              (a typedef, class-name, etc.), then we can't allow any
10687              more such type-specifiers henceforth.
10688
10689              [dcl.spec]
10690
10691              The longest sequence of decl-specifiers that could
10692              possibly be a type name is taken as the
10693              decl-specifier-seq of a declaration.  The sequence shall
10694              be self-consistent as described below.
10695
10696              [dcl.type]
10697
10698              As a general rule, at most one type-specifier is allowed
10699              in the complete decl-specifier-seq of a declaration.  The
10700              only exceptions are the following:
10701
10702              -- const or volatile can be combined with any other
10703                 type-specifier.
10704
10705              -- signed or unsigned can be combined with char, long,
10706                 short, or int.
10707
10708              -- ..
10709
10710              Example:
10711
10712                typedef char* Pc;
10713                void g (const int Pc);
10714
10715              Here, Pc is *not* part of the decl-specifier seq; it's
10716              the declarator.  Therefore, once we see a type-specifier
10717              (other than a cv-qualifier), we forbid any additional
10718              user-defined types.  We *do* still allow things like `int
10719              int' to be considered a decl-specifier-seq, and issue the
10720              error message later.  */
10721           if (type_spec && !is_cv_qualifier)
10722             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10723           /* A constructor declarator cannot follow a type-specifier.  */
10724           if (type_spec)
10725             {
10726               constructor_possible_p = false;
10727               found_decl_spec = true;
10728               if (!is_cv_qualifier)
10729                 decl_specs->any_type_specifiers_p = true;
10730             }
10731         }
10732
10733       /* If we still do not have a DECL_SPEC, then there are no more
10734          decl-specifiers.  */
10735       if (!found_decl_spec)
10736         break;
10737
10738       decl_specs->any_specifiers_p = true;
10739       /* After we see one decl-specifier, further decl-specifiers are
10740          always optional.  */
10741       flags |= CP_PARSER_FLAGS_OPTIONAL;
10742     }
10743
10744   cp_parser_check_decl_spec (decl_specs, start_token->location);
10745
10746   /* Don't allow a friend specifier with a class definition.  */
10747   if (decl_specs->specs[(int) ds_friend] != 0
10748       && (*declares_class_or_enum & 2))
10749     error_at (start_token->location,
10750               "class definition may not be declared a friend");
10751 }
10752
10753 /* Parse an (optional) storage-class-specifier.
10754
10755    storage-class-specifier:
10756      auto
10757      register
10758      static
10759      extern
10760      mutable
10761
10762    GNU Extension:
10763
10764    storage-class-specifier:
10765      thread
10766
10767    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10768
10769 static tree
10770 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10771 {
10772   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10773     {
10774     case RID_AUTO:
10775       if (cxx_dialect != cxx98)
10776         return NULL_TREE;
10777       /* Fall through for C++98.  */
10778
10779     case RID_REGISTER:
10780     case RID_STATIC:
10781     case RID_EXTERN:
10782     case RID_MUTABLE:
10783     case RID_THREAD:
10784       /* Consume the token.  */
10785       return cp_lexer_consume_token (parser->lexer)->u.value;
10786
10787     default:
10788       return NULL_TREE;
10789     }
10790 }
10791
10792 /* Parse an (optional) function-specifier.
10793
10794    function-specifier:
10795      inline
10796      virtual
10797      explicit
10798
10799    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10800    Updates DECL_SPECS, if it is non-NULL.  */
10801
10802 static tree
10803 cp_parser_function_specifier_opt (cp_parser* parser,
10804                                   cp_decl_specifier_seq *decl_specs)
10805 {
10806   cp_token *token = cp_lexer_peek_token (parser->lexer);
10807   switch (token->keyword)
10808     {
10809     case RID_INLINE:
10810       if (decl_specs)
10811         ++decl_specs->specs[(int) ds_inline];
10812       break;
10813
10814     case RID_VIRTUAL:
10815       /* 14.5.2.3 [temp.mem]
10816
10817          A member function template shall not be virtual.  */
10818       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10819         error_at (token->location, "templates may not be %<virtual%>");
10820       else if (decl_specs)
10821         ++decl_specs->specs[(int) ds_virtual];
10822       break;
10823
10824     case RID_EXPLICIT:
10825       if (decl_specs)
10826         ++decl_specs->specs[(int) ds_explicit];
10827       break;
10828
10829     default:
10830       return NULL_TREE;
10831     }
10832
10833   /* Consume the token.  */
10834   return cp_lexer_consume_token (parser->lexer)->u.value;
10835 }
10836
10837 /* Parse a linkage-specification.
10838
10839    linkage-specification:
10840      extern string-literal { declaration-seq [opt] }
10841      extern string-literal declaration  */
10842
10843 static void
10844 cp_parser_linkage_specification (cp_parser* parser)
10845 {
10846   tree linkage;
10847
10848   /* Look for the `extern' keyword.  */
10849   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10850
10851   /* Look for the string-literal.  */
10852   linkage = cp_parser_string_literal (parser, false, false);
10853
10854   /* Transform the literal into an identifier.  If the literal is a
10855      wide-character string, or contains embedded NULs, then we can't
10856      handle it as the user wants.  */
10857   if (strlen (TREE_STRING_POINTER (linkage))
10858       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10859     {
10860       cp_parser_error (parser, "invalid linkage-specification");
10861       /* Assume C++ linkage.  */
10862       linkage = lang_name_cplusplus;
10863     }
10864   else
10865     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10866
10867   /* We're now using the new linkage.  */
10868   push_lang_context (linkage);
10869
10870   /* If the next token is a `{', then we're using the first
10871      production.  */
10872   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10873     {
10874       /* Consume the `{' token.  */
10875       cp_lexer_consume_token (parser->lexer);
10876       /* Parse the declarations.  */
10877       cp_parser_declaration_seq_opt (parser);
10878       /* Look for the closing `}'.  */
10879       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10880     }
10881   /* Otherwise, there's just one declaration.  */
10882   else
10883     {
10884       bool saved_in_unbraced_linkage_specification_p;
10885
10886       saved_in_unbraced_linkage_specification_p
10887         = parser->in_unbraced_linkage_specification_p;
10888       parser->in_unbraced_linkage_specification_p = true;
10889       cp_parser_declaration (parser);
10890       parser->in_unbraced_linkage_specification_p
10891         = saved_in_unbraced_linkage_specification_p;
10892     }
10893
10894   /* We're done with the linkage-specification.  */
10895   pop_lang_context ();
10896 }
10897
10898 /* Parse a static_assert-declaration.
10899
10900    static_assert-declaration:
10901      static_assert ( constant-expression , string-literal ) ; 
10902
10903    If MEMBER_P, this static_assert is a class member.  */
10904
10905 static void 
10906 cp_parser_static_assert(cp_parser *parser, bool member_p)
10907 {
10908   tree condition;
10909   tree message;
10910   cp_token *token;
10911   location_t saved_loc;
10912   bool dummy;
10913
10914   /* Peek at the `static_assert' token so we can keep track of exactly
10915      where the static assertion started.  */
10916   token = cp_lexer_peek_token (parser->lexer);
10917   saved_loc = token->location;
10918
10919   /* Look for the `static_assert' keyword.  */
10920   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10921                                   RT_STATIC_ASSERT))
10922     return;
10923
10924   /*  We know we are in a static assertion; commit to any tentative
10925       parse.  */
10926   if (cp_parser_parsing_tentatively (parser))
10927     cp_parser_commit_to_tentative_parse (parser);
10928
10929   /* Parse the `(' starting the static assertion condition.  */
10930   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10931
10932   /* Parse the constant-expression.  Allow a non-constant expression
10933      here in order to give better diagnostics in finish_static_assert.  */
10934   condition = 
10935     cp_parser_constant_expression (parser,
10936                                    /*allow_non_constant_p=*/true,
10937                                    /*non_constant_p=*/&dummy);
10938
10939   /* Parse the separating `,'.  */
10940   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10941
10942   /* Parse the string-literal message.  */
10943   message = cp_parser_string_literal (parser, 
10944                                       /*translate=*/false,
10945                                       /*wide_ok=*/true);
10946
10947   /* A `)' completes the static assertion.  */
10948   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10949     cp_parser_skip_to_closing_parenthesis (parser, 
10950                                            /*recovering=*/true, 
10951                                            /*or_comma=*/false,
10952                                            /*consume_paren=*/true);
10953
10954   /* A semicolon terminates the declaration.  */
10955   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10956
10957   /* Complete the static assertion, which may mean either processing 
10958      the static assert now or saving it for template instantiation.  */
10959   finish_static_assert (condition, message, saved_loc, member_p);
10960 }
10961
10962 /* Parse a `decltype' type. Returns the type. 
10963
10964    simple-type-specifier:
10965      decltype ( expression )  */
10966
10967 static tree
10968 cp_parser_decltype (cp_parser *parser)
10969 {
10970   tree expr;
10971   bool id_expression_or_member_access_p = false;
10972   const char *saved_message;
10973   bool saved_integral_constant_expression_p;
10974   bool saved_non_integral_constant_expression_p;
10975   cp_token *id_expr_start_token;
10976   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10977
10978   if (start_token->type == CPP_DECLTYPE)
10979     {
10980       /* Already parsed.  */
10981       cp_lexer_consume_token (parser->lexer);
10982       return start_token->u.value;
10983     }
10984
10985   /* Look for the `decltype' token.  */
10986   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10987     return error_mark_node;
10988
10989   /* Types cannot be defined in a `decltype' expression.  Save away the
10990      old message.  */
10991   saved_message = parser->type_definition_forbidden_message;
10992
10993   /* And create the new one.  */
10994   parser->type_definition_forbidden_message
10995     = G_("types may not be defined in %<decltype%> expressions");
10996
10997   /* The restrictions on constant-expressions do not apply inside
10998      decltype expressions.  */
10999   saved_integral_constant_expression_p
11000     = parser->integral_constant_expression_p;
11001   saved_non_integral_constant_expression_p
11002     = parser->non_integral_constant_expression_p;
11003   parser->integral_constant_expression_p = false;
11004
11005   /* Do not actually evaluate the expression.  */
11006   ++cp_unevaluated_operand;
11007
11008   /* Do not warn about problems with the expression.  */
11009   ++c_inhibit_evaluation_warnings;
11010
11011   /* Parse the opening `('.  */
11012   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
11013     return error_mark_node;
11014   
11015   /* First, try parsing an id-expression.  */
11016   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
11017   cp_parser_parse_tentatively (parser);
11018   expr = cp_parser_id_expression (parser,
11019                                   /*template_keyword_p=*/false,
11020                                   /*check_dependency_p=*/true,
11021                                   /*template_p=*/NULL,
11022                                   /*declarator_p=*/false,
11023                                   /*optional_p=*/false);
11024
11025   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
11026     {
11027       bool non_integral_constant_expression_p = false;
11028       tree id_expression = expr;
11029       cp_id_kind idk;
11030       const char *error_msg;
11031
11032       if (TREE_CODE (expr) == IDENTIFIER_NODE)
11033         /* Lookup the name we got back from the id-expression.  */
11034         expr = cp_parser_lookup_name (parser, expr,
11035                                       none_type,
11036                                       /*is_template=*/false,
11037                                       /*is_namespace=*/false,
11038                                       /*check_dependency=*/true,
11039                                       /*ambiguous_decls=*/NULL,
11040                                       id_expr_start_token->location);
11041
11042       if (expr
11043           && expr != error_mark_node
11044           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11045           && TREE_CODE (expr) != TYPE_DECL
11046           && (TREE_CODE (expr) != BIT_NOT_EXPR
11047               || !TYPE_P (TREE_OPERAND (expr, 0)))
11048           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11049         {
11050           /* Complete lookup of the id-expression.  */
11051           expr = (finish_id_expression
11052                   (id_expression, expr, parser->scope, &idk,
11053                    /*integral_constant_expression_p=*/false,
11054                    /*allow_non_integral_constant_expression_p=*/true,
11055                    &non_integral_constant_expression_p,
11056                    /*template_p=*/false,
11057                    /*done=*/true,
11058                    /*address_p=*/false,
11059                    /*template_arg_p=*/false,
11060                    &error_msg,
11061                    id_expr_start_token->location));
11062
11063           if (expr == error_mark_node)
11064             /* We found an id-expression, but it was something that we
11065                should not have found. This is an error, not something
11066                we can recover from, so note that we found an
11067                id-expression and we'll recover as gracefully as
11068                possible.  */
11069             id_expression_or_member_access_p = true;
11070         }
11071
11072       if (expr 
11073           && expr != error_mark_node
11074           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11075         /* We have an id-expression.  */
11076         id_expression_or_member_access_p = true;
11077     }
11078
11079   if (!id_expression_or_member_access_p)
11080     {
11081       /* Abort the id-expression parse.  */
11082       cp_parser_abort_tentative_parse (parser);
11083
11084       /* Parsing tentatively, again.  */
11085       cp_parser_parse_tentatively (parser);
11086
11087       /* Parse a class member access.  */
11088       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11089                                            /*cast_p=*/false,
11090                                            /*member_access_only_p=*/true, NULL);
11091
11092       if (expr 
11093           && expr != error_mark_node
11094           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11095         /* We have an id-expression.  */
11096         id_expression_or_member_access_p = true;
11097     }
11098
11099   if (id_expression_or_member_access_p)
11100     /* We have parsed the complete id-expression or member access.  */
11101     cp_parser_parse_definitely (parser);
11102   else
11103     {
11104       bool saved_greater_than_is_operator_p;
11105
11106       /* Abort our attempt to parse an id-expression or member access
11107          expression.  */
11108       cp_parser_abort_tentative_parse (parser);
11109
11110       /* Within a parenthesized expression, a `>' token is always
11111          the greater-than operator.  */
11112       saved_greater_than_is_operator_p
11113         = parser->greater_than_is_operator_p;
11114       parser->greater_than_is_operator_p = true;
11115
11116       /* Parse a full expression.  */
11117       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11118
11119       /* The `>' token might be the end of a template-id or
11120          template-parameter-list now.  */
11121       parser->greater_than_is_operator_p
11122         = saved_greater_than_is_operator_p;
11123     }
11124
11125   /* Go back to evaluating expressions.  */
11126   --cp_unevaluated_operand;
11127   --c_inhibit_evaluation_warnings;
11128
11129   /* Restore the old message and the integral constant expression
11130      flags.  */
11131   parser->type_definition_forbidden_message = saved_message;
11132   parser->integral_constant_expression_p
11133     = saved_integral_constant_expression_p;
11134   parser->non_integral_constant_expression_p
11135     = saved_non_integral_constant_expression_p;
11136
11137   /* Parse to the closing `)'.  */
11138   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11139     {
11140       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11141                                              /*consume_paren=*/true);
11142       return error_mark_node;
11143     }
11144
11145   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11146                                tf_warning_or_error);
11147
11148   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11149      it again.  */
11150   start_token->type = CPP_DECLTYPE;
11151   start_token->u.value = expr;
11152   start_token->keyword = RID_MAX;
11153   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11154
11155   return expr;
11156 }
11157
11158 /* Special member functions [gram.special] */
11159
11160 /* Parse a conversion-function-id.
11161
11162    conversion-function-id:
11163      operator conversion-type-id
11164
11165    Returns an IDENTIFIER_NODE representing the operator.  */
11166
11167 static tree
11168 cp_parser_conversion_function_id (cp_parser* parser)
11169 {
11170   tree type;
11171   tree saved_scope;
11172   tree saved_qualifying_scope;
11173   tree saved_object_scope;
11174   tree pushed_scope = NULL_TREE;
11175
11176   /* Look for the `operator' token.  */
11177   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11178     return error_mark_node;
11179   /* When we parse the conversion-type-id, the current scope will be
11180      reset.  However, we need that information in able to look up the
11181      conversion function later, so we save it here.  */
11182   saved_scope = parser->scope;
11183   saved_qualifying_scope = parser->qualifying_scope;
11184   saved_object_scope = parser->object_scope;
11185   /* We must enter the scope of the class so that the names of
11186      entities declared within the class are available in the
11187      conversion-type-id.  For example, consider:
11188
11189        struct S {
11190          typedef int I;
11191          operator I();
11192        };
11193
11194        S::operator I() { ... }
11195
11196      In order to see that `I' is a type-name in the definition, we
11197      must be in the scope of `S'.  */
11198   if (saved_scope)
11199     pushed_scope = push_scope (saved_scope);
11200   /* Parse the conversion-type-id.  */
11201   type = cp_parser_conversion_type_id (parser);
11202   /* Leave the scope of the class, if any.  */
11203   if (pushed_scope)
11204     pop_scope (pushed_scope);
11205   /* Restore the saved scope.  */
11206   parser->scope = saved_scope;
11207   parser->qualifying_scope = saved_qualifying_scope;
11208   parser->object_scope = saved_object_scope;
11209   /* If the TYPE is invalid, indicate failure.  */
11210   if (type == error_mark_node)
11211     return error_mark_node;
11212   return mangle_conv_op_name_for_type (type);
11213 }
11214
11215 /* Parse a conversion-type-id:
11216
11217    conversion-type-id:
11218      type-specifier-seq conversion-declarator [opt]
11219
11220    Returns the TYPE specified.  */
11221
11222 static tree
11223 cp_parser_conversion_type_id (cp_parser* parser)
11224 {
11225   tree attributes;
11226   cp_decl_specifier_seq type_specifiers;
11227   cp_declarator *declarator;
11228   tree type_specified;
11229
11230   /* Parse the attributes.  */
11231   attributes = cp_parser_attributes_opt (parser);
11232   /* Parse the type-specifiers.  */
11233   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11234                                 /*is_trailing_return=*/false,
11235                                 &type_specifiers);
11236   /* If that didn't work, stop.  */
11237   if (type_specifiers.type == error_mark_node)
11238     return error_mark_node;
11239   /* Parse the conversion-declarator.  */
11240   declarator = cp_parser_conversion_declarator_opt (parser);
11241
11242   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11243                                     /*initialized=*/0, &attributes);
11244   if (attributes)
11245     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11246
11247   /* Don't give this error when parsing tentatively.  This happens to
11248      work because we always parse this definitively once.  */
11249   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11250       && type_uses_auto (type_specified))
11251     {
11252       error ("invalid use of %<auto%> in conversion operator");
11253       return error_mark_node;
11254     }
11255
11256   return type_specified;
11257 }
11258
11259 /* Parse an (optional) conversion-declarator.
11260
11261    conversion-declarator:
11262      ptr-operator conversion-declarator [opt]
11263
11264    */
11265
11266 static cp_declarator *
11267 cp_parser_conversion_declarator_opt (cp_parser* parser)
11268 {
11269   enum tree_code code;
11270   tree class_type;
11271   cp_cv_quals cv_quals;
11272
11273   /* We don't know if there's a ptr-operator next, or not.  */
11274   cp_parser_parse_tentatively (parser);
11275   /* Try the ptr-operator.  */
11276   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11277   /* If it worked, look for more conversion-declarators.  */
11278   if (cp_parser_parse_definitely (parser))
11279     {
11280       cp_declarator *declarator;
11281
11282       /* Parse another optional declarator.  */
11283       declarator = cp_parser_conversion_declarator_opt (parser);
11284
11285       return cp_parser_make_indirect_declarator
11286         (code, class_type, cv_quals, declarator);
11287    }
11288
11289   return NULL;
11290 }
11291
11292 /* Parse an (optional) ctor-initializer.
11293
11294    ctor-initializer:
11295      : mem-initializer-list
11296
11297    Returns TRUE iff the ctor-initializer was actually present.  */
11298
11299 static bool
11300 cp_parser_ctor_initializer_opt (cp_parser* parser)
11301 {
11302   /* If the next token is not a `:', then there is no
11303      ctor-initializer.  */
11304   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11305     {
11306       /* Do default initialization of any bases and members.  */
11307       if (DECL_CONSTRUCTOR_P (current_function_decl))
11308         finish_mem_initializers (NULL_TREE);
11309
11310       return false;
11311     }
11312
11313   /* Consume the `:' token.  */
11314   cp_lexer_consume_token (parser->lexer);
11315   /* And the mem-initializer-list.  */
11316   cp_parser_mem_initializer_list (parser);
11317
11318   return true;
11319 }
11320
11321 /* Parse a mem-initializer-list.
11322
11323    mem-initializer-list:
11324      mem-initializer ... [opt]
11325      mem-initializer ... [opt] , mem-initializer-list  */
11326
11327 static void
11328 cp_parser_mem_initializer_list (cp_parser* parser)
11329 {
11330   tree mem_initializer_list = NULL_TREE;
11331   tree target_ctor = error_mark_node;
11332   cp_token *token = cp_lexer_peek_token (parser->lexer);
11333
11334   /* Let the semantic analysis code know that we are starting the
11335      mem-initializer-list.  */
11336   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11337     error_at (token->location,
11338               "only constructors take member initializers");
11339
11340   /* Loop through the list.  */
11341   while (true)
11342     {
11343       tree mem_initializer;
11344
11345       token = cp_lexer_peek_token (parser->lexer);
11346       /* Parse the mem-initializer.  */
11347       mem_initializer = cp_parser_mem_initializer (parser);
11348       /* If the next token is a `...', we're expanding member initializers. */
11349       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11350         {
11351           /* Consume the `...'. */
11352           cp_lexer_consume_token (parser->lexer);
11353
11354           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11355              can be expanded but members cannot. */
11356           if (mem_initializer != error_mark_node
11357               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11358             {
11359               error_at (token->location,
11360                         "cannot expand initializer for member %<%D%>",
11361                         TREE_PURPOSE (mem_initializer));
11362               mem_initializer = error_mark_node;
11363             }
11364
11365           /* Construct the pack expansion type. */
11366           if (mem_initializer != error_mark_node)
11367             mem_initializer = make_pack_expansion (mem_initializer);
11368         }
11369       if (target_ctor != error_mark_node
11370           && mem_initializer != error_mark_node)
11371         {
11372           error ("mem-initializer for %qD follows constructor delegation",
11373                  TREE_PURPOSE (mem_initializer));
11374           mem_initializer = error_mark_node;
11375         }
11376       /* Look for a target constructor. */
11377       if (mem_initializer != error_mark_node
11378           && TYPE_P (TREE_PURPOSE (mem_initializer))
11379           && same_type_p (TREE_PURPOSE (mem_initializer), current_class_type))
11380         {
11381           maybe_warn_cpp0x (CPP0X_DELEGATING_CTORS);
11382           if (mem_initializer_list)
11383             {
11384               error ("constructor delegation follows mem-initializer for %qD",
11385                      TREE_PURPOSE (mem_initializer_list));
11386               mem_initializer = error_mark_node;
11387             }
11388           target_ctor = mem_initializer;
11389         }
11390       /* Add it to the list, unless it was erroneous.  */
11391       if (mem_initializer != error_mark_node)
11392         {
11393           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11394           mem_initializer_list = mem_initializer;
11395         }
11396       /* If the next token is not a `,', we're done.  */
11397       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11398         break;
11399       /* Consume the `,' token.  */
11400       cp_lexer_consume_token (parser->lexer);
11401     }
11402
11403   /* Perform semantic analysis.  */
11404   if (DECL_CONSTRUCTOR_P (current_function_decl))
11405     finish_mem_initializers (mem_initializer_list);
11406 }
11407
11408 /* Parse a mem-initializer.
11409
11410    mem-initializer:
11411      mem-initializer-id ( expression-list [opt] )
11412      mem-initializer-id braced-init-list
11413
11414    GNU extension:
11415
11416    mem-initializer:
11417      ( expression-list [opt] )
11418
11419    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11420    class) or FIELD_DECL (for a non-static data member) to initialize;
11421    the TREE_VALUE is the expression-list.  An empty initialization
11422    list is represented by void_list_node.  */
11423
11424 static tree
11425 cp_parser_mem_initializer (cp_parser* parser)
11426 {
11427   tree mem_initializer_id;
11428   tree expression_list;
11429   tree member;
11430   cp_token *token = cp_lexer_peek_token (parser->lexer);
11431
11432   /* Find out what is being initialized.  */
11433   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11434     {
11435       permerror (token->location,
11436                  "anachronistic old-style base class initializer");
11437       mem_initializer_id = NULL_TREE;
11438     }
11439   else
11440     {
11441       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11442       if (mem_initializer_id == error_mark_node)
11443         return mem_initializer_id;
11444     }
11445   member = expand_member_init (mem_initializer_id);
11446   if (member && !DECL_P (member))
11447     in_base_initializer = 1;
11448
11449   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11450     {
11451       bool expr_non_constant_p;
11452       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11453       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11454       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11455       expression_list = build_tree_list (NULL_TREE, expression_list);
11456     }
11457   else
11458     {
11459       VEC(tree,gc)* vec;
11460       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11461                                                      /*cast_p=*/false,
11462                                                      /*allow_expansion_p=*/true,
11463                                                      /*non_constant_p=*/NULL);
11464       if (vec == NULL)
11465         return error_mark_node;
11466       expression_list = build_tree_list_vec (vec);
11467       release_tree_vector (vec);
11468     }
11469
11470   if (expression_list == error_mark_node)
11471     return error_mark_node;
11472   if (!expression_list)
11473     expression_list = void_type_node;
11474
11475   in_base_initializer = 0;
11476
11477   return member ? build_tree_list (member, expression_list) : error_mark_node;
11478 }
11479
11480 /* Parse a mem-initializer-id.
11481
11482    mem-initializer-id:
11483      :: [opt] nested-name-specifier [opt] class-name
11484      identifier
11485
11486    Returns a TYPE indicating the class to be initializer for the first
11487    production.  Returns an IDENTIFIER_NODE indicating the data member
11488    to be initialized for the second production.  */
11489
11490 static tree
11491 cp_parser_mem_initializer_id (cp_parser* parser)
11492 {
11493   bool global_scope_p;
11494   bool nested_name_specifier_p;
11495   bool template_p = false;
11496   tree id;
11497
11498   cp_token *token = cp_lexer_peek_token (parser->lexer);
11499
11500   /* `typename' is not allowed in this context ([temp.res]).  */
11501   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11502     {
11503       error_at (token->location, 
11504                 "keyword %<typename%> not allowed in this context (a qualified "
11505                 "member initializer is implicitly a type)");
11506       cp_lexer_consume_token (parser->lexer);
11507     }
11508   /* Look for the optional `::' operator.  */
11509   global_scope_p
11510     = (cp_parser_global_scope_opt (parser,
11511                                    /*current_scope_valid_p=*/false)
11512        != NULL_TREE);
11513   /* Look for the optional nested-name-specifier.  The simplest way to
11514      implement:
11515
11516        [temp.res]
11517
11518        The keyword `typename' is not permitted in a base-specifier or
11519        mem-initializer; in these contexts a qualified name that
11520        depends on a template-parameter is implicitly assumed to be a
11521        type name.
11522
11523      is to assume that we have seen the `typename' keyword at this
11524      point.  */
11525   nested_name_specifier_p
11526     = (cp_parser_nested_name_specifier_opt (parser,
11527                                             /*typename_keyword_p=*/true,
11528                                             /*check_dependency_p=*/true,
11529                                             /*type_p=*/true,
11530                                             /*is_declaration=*/true)
11531        != NULL_TREE);
11532   if (nested_name_specifier_p)
11533     template_p = cp_parser_optional_template_keyword (parser);
11534   /* If there is a `::' operator or a nested-name-specifier, then we
11535      are definitely looking for a class-name.  */
11536   if (global_scope_p || nested_name_specifier_p)
11537     return cp_parser_class_name (parser,
11538                                  /*typename_keyword_p=*/true,
11539                                  /*template_keyword_p=*/template_p,
11540                                  typename_type,
11541                                  /*check_dependency_p=*/true,
11542                                  /*class_head_p=*/false,
11543                                  /*is_declaration=*/true);
11544   /* Otherwise, we could also be looking for an ordinary identifier.  */
11545   cp_parser_parse_tentatively (parser);
11546   /* Try a class-name.  */
11547   id = cp_parser_class_name (parser,
11548                              /*typename_keyword_p=*/true,
11549                              /*template_keyword_p=*/false,
11550                              none_type,
11551                              /*check_dependency_p=*/true,
11552                              /*class_head_p=*/false,
11553                              /*is_declaration=*/true);
11554   /* If we found one, we're done.  */
11555   if (cp_parser_parse_definitely (parser))
11556     return id;
11557   /* Otherwise, look for an ordinary identifier.  */
11558   return cp_parser_identifier (parser);
11559 }
11560
11561 /* Overloading [gram.over] */
11562
11563 /* Parse an operator-function-id.
11564
11565    operator-function-id:
11566      operator operator
11567
11568    Returns an IDENTIFIER_NODE for the operator which is a
11569    human-readable spelling of the identifier, e.g., `operator +'.  */
11570
11571 static tree
11572 cp_parser_operator_function_id (cp_parser* parser)
11573 {
11574   /* Look for the `operator' keyword.  */
11575   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11576     return error_mark_node;
11577   /* And then the name of the operator itself.  */
11578   return cp_parser_operator (parser);
11579 }
11580
11581 /* Return an identifier node for a user-defined literal operator.
11582    The suffix identifier is chained to the operator name identifier.  */
11583
11584 static tree
11585 cp_literal_operator_id (const char* name)
11586 {
11587   tree identifier;
11588   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11589                               + strlen (name) + 10);
11590   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11591   identifier = get_identifier (buffer);
11592   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11593
11594   return identifier;
11595 }
11596
11597 /* Parse an operator.
11598
11599    operator:
11600      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11601      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11602      || ++ -- , ->* -> () []
11603
11604    GNU Extensions:
11605
11606    operator:
11607      <? >? <?= >?=
11608
11609    Returns an IDENTIFIER_NODE for the operator which is a
11610    human-readable spelling of the identifier, e.g., `operator +'.  */
11611
11612 static tree
11613 cp_parser_operator (cp_parser* parser)
11614 {
11615   tree id = NULL_TREE;
11616   cp_token *token;
11617
11618   /* Peek at the next token.  */
11619   token = cp_lexer_peek_token (parser->lexer);
11620   /* Figure out which operator we have.  */
11621   switch (token->type)
11622     {
11623     case CPP_KEYWORD:
11624       {
11625         enum tree_code op;
11626
11627         /* The keyword should be either `new' or `delete'.  */
11628         if (token->keyword == RID_NEW)
11629           op = NEW_EXPR;
11630         else if (token->keyword == RID_DELETE)
11631           op = DELETE_EXPR;
11632         else
11633           break;
11634
11635         /* Consume the `new' or `delete' token.  */
11636         cp_lexer_consume_token (parser->lexer);
11637
11638         /* Peek at the next token.  */
11639         token = cp_lexer_peek_token (parser->lexer);
11640         /* If it's a `[' token then this is the array variant of the
11641            operator.  */
11642         if (token->type == CPP_OPEN_SQUARE)
11643           {
11644             /* Consume the `[' token.  */
11645             cp_lexer_consume_token (parser->lexer);
11646             /* Look for the `]' token.  */
11647             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11648             id = ansi_opname (op == NEW_EXPR
11649                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11650           }
11651         /* Otherwise, we have the non-array variant.  */
11652         else
11653           id = ansi_opname (op);
11654
11655         return id;
11656       }
11657
11658     case CPP_PLUS:
11659       id = ansi_opname (PLUS_EXPR);
11660       break;
11661
11662     case CPP_MINUS:
11663       id = ansi_opname (MINUS_EXPR);
11664       break;
11665
11666     case CPP_MULT:
11667       id = ansi_opname (MULT_EXPR);
11668       break;
11669
11670     case CPP_DIV:
11671       id = ansi_opname (TRUNC_DIV_EXPR);
11672       break;
11673
11674     case CPP_MOD:
11675       id = ansi_opname (TRUNC_MOD_EXPR);
11676       break;
11677
11678     case CPP_XOR:
11679       id = ansi_opname (BIT_XOR_EXPR);
11680       break;
11681
11682     case CPP_AND:
11683       id = ansi_opname (BIT_AND_EXPR);
11684       break;
11685
11686     case CPP_OR:
11687       id = ansi_opname (BIT_IOR_EXPR);
11688       break;
11689
11690     case CPP_COMPL:
11691       id = ansi_opname (BIT_NOT_EXPR);
11692       break;
11693
11694     case CPP_NOT:
11695       id = ansi_opname (TRUTH_NOT_EXPR);
11696       break;
11697
11698     case CPP_EQ:
11699       id = ansi_assopname (NOP_EXPR);
11700       break;
11701
11702     case CPP_LESS:
11703       id = ansi_opname (LT_EXPR);
11704       break;
11705
11706     case CPP_GREATER:
11707       id = ansi_opname (GT_EXPR);
11708       break;
11709
11710     case CPP_PLUS_EQ:
11711       id = ansi_assopname (PLUS_EXPR);
11712       break;
11713
11714     case CPP_MINUS_EQ:
11715       id = ansi_assopname (MINUS_EXPR);
11716       break;
11717
11718     case CPP_MULT_EQ:
11719       id = ansi_assopname (MULT_EXPR);
11720       break;
11721
11722     case CPP_DIV_EQ:
11723       id = ansi_assopname (TRUNC_DIV_EXPR);
11724       break;
11725
11726     case CPP_MOD_EQ:
11727       id = ansi_assopname (TRUNC_MOD_EXPR);
11728       break;
11729
11730     case CPP_XOR_EQ:
11731       id = ansi_assopname (BIT_XOR_EXPR);
11732       break;
11733
11734     case CPP_AND_EQ:
11735       id = ansi_assopname (BIT_AND_EXPR);
11736       break;
11737
11738     case CPP_OR_EQ:
11739       id = ansi_assopname (BIT_IOR_EXPR);
11740       break;
11741
11742     case CPP_LSHIFT:
11743       id = ansi_opname (LSHIFT_EXPR);
11744       break;
11745
11746     case CPP_RSHIFT:
11747       id = ansi_opname (RSHIFT_EXPR);
11748       break;
11749
11750     case CPP_LSHIFT_EQ:
11751       id = ansi_assopname (LSHIFT_EXPR);
11752       break;
11753
11754     case CPP_RSHIFT_EQ:
11755       id = ansi_assopname (RSHIFT_EXPR);
11756       break;
11757
11758     case CPP_EQ_EQ:
11759       id = ansi_opname (EQ_EXPR);
11760       break;
11761
11762     case CPP_NOT_EQ:
11763       id = ansi_opname (NE_EXPR);
11764       break;
11765
11766     case CPP_LESS_EQ:
11767       id = ansi_opname (LE_EXPR);
11768       break;
11769
11770     case CPP_GREATER_EQ:
11771       id = ansi_opname (GE_EXPR);
11772       break;
11773
11774     case CPP_AND_AND:
11775       id = ansi_opname (TRUTH_ANDIF_EXPR);
11776       break;
11777
11778     case CPP_OR_OR:
11779       id = ansi_opname (TRUTH_ORIF_EXPR);
11780       break;
11781
11782     case CPP_PLUS_PLUS:
11783       id = ansi_opname (POSTINCREMENT_EXPR);
11784       break;
11785
11786     case CPP_MINUS_MINUS:
11787       id = ansi_opname (PREDECREMENT_EXPR);
11788       break;
11789
11790     case CPP_COMMA:
11791       id = ansi_opname (COMPOUND_EXPR);
11792       break;
11793
11794     case CPP_DEREF_STAR:
11795       id = ansi_opname (MEMBER_REF);
11796       break;
11797
11798     case CPP_DEREF:
11799       id = ansi_opname (COMPONENT_REF);
11800       break;
11801
11802     case CPP_OPEN_PAREN:
11803       /* Consume the `('.  */
11804       cp_lexer_consume_token (parser->lexer);
11805       /* Look for the matching `)'.  */
11806       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11807       return ansi_opname (CALL_EXPR);
11808
11809     case CPP_OPEN_SQUARE:
11810       /* Consume the `['.  */
11811       cp_lexer_consume_token (parser->lexer);
11812       /* Look for the matching `]'.  */
11813       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11814       return ansi_opname (ARRAY_REF);
11815
11816     case CPP_STRING:
11817       if (cxx_dialect == cxx98)
11818         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11819       if (TREE_STRING_LENGTH (token->u.value) > 2)
11820         {
11821           error ("expected empty string after %<operator%> keyword");
11822           return error_mark_node;
11823         }
11824       /* Consume the string.  */
11825       cp_lexer_consume_token (parser->lexer);
11826       /* Look for the suffix identifier.  */
11827       token = cp_lexer_peek_token (parser->lexer);
11828       if (token->type == CPP_NAME)
11829         {
11830           id = cp_parser_identifier (parser);
11831           if (id != error_mark_node)
11832             {
11833               const char *name = IDENTIFIER_POINTER (id);
11834               return cp_literal_operator_id (name);
11835             }
11836         }
11837       else
11838         {
11839           error ("expected suffix identifier");
11840           return error_mark_node;
11841         }
11842
11843     case CPP_STRING_USERDEF:
11844       error ("missing space between %<\"\"%> and suffix identifier");
11845       return error_mark_node;
11846
11847     default:
11848       /* Anything else is an error.  */
11849       break;
11850     }
11851
11852   /* If we have selected an identifier, we need to consume the
11853      operator token.  */
11854   if (id)
11855     cp_lexer_consume_token (parser->lexer);
11856   /* Otherwise, no valid operator name was present.  */
11857   else
11858     {
11859       cp_parser_error (parser, "expected operator");
11860       id = error_mark_node;
11861     }
11862
11863   return id;
11864 }
11865
11866 /* Parse a template-declaration.
11867
11868    template-declaration:
11869      export [opt] template < template-parameter-list > declaration
11870
11871    If MEMBER_P is TRUE, this template-declaration occurs within a
11872    class-specifier.
11873
11874    The grammar rule given by the standard isn't correct.  What
11875    is really meant is:
11876
11877    template-declaration:
11878      export [opt] template-parameter-list-seq
11879        decl-specifier-seq [opt] init-declarator [opt] ;
11880      export [opt] template-parameter-list-seq
11881        function-definition
11882
11883    template-parameter-list-seq:
11884      template-parameter-list-seq [opt]
11885      template < template-parameter-list >  */
11886
11887 static void
11888 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11889 {
11890   /* Check for `export'.  */
11891   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11892     {
11893       /* Consume the `export' token.  */
11894       cp_lexer_consume_token (parser->lexer);
11895       /* Warn that we do not support `export'.  */
11896       warning (0, "keyword %<export%> not implemented, and will be ignored");
11897     }
11898
11899   cp_parser_template_declaration_after_export (parser, member_p);
11900 }
11901
11902 /* Parse a template-parameter-list.
11903
11904    template-parameter-list:
11905      template-parameter
11906      template-parameter-list , template-parameter
11907
11908    Returns a TREE_LIST.  Each node represents a template parameter.
11909    The nodes are connected via their TREE_CHAINs.  */
11910
11911 static tree
11912 cp_parser_template_parameter_list (cp_parser* parser)
11913 {
11914   tree parameter_list = NULL_TREE;
11915
11916   begin_template_parm_list ();
11917
11918   /* The loop below parses the template parms.  We first need to know
11919      the total number of template parms to be able to compute proper
11920      canonical types of each dependent type. So after the loop, when
11921      we know the total number of template parms,
11922      end_template_parm_list computes the proper canonical types and
11923      fixes up the dependent types accordingly.  */
11924   while (true)
11925     {
11926       tree parameter;
11927       bool is_non_type;
11928       bool is_parameter_pack;
11929       location_t parm_loc;
11930
11931       /* Parse the template-parameter.  */
11932       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11933       parameter = cp_parser_template_parameter (parser, 
11934                                                 &is_non_type,
11935                                                 &is_parameter_pack);
11936       /* Add it to the list.  */
11937       if (parameter != error_mark_node)
11938         parameter_list = process_template_parm (parameter_list,
11939                                                 parm_loc,
11940                                                 parameter,
11941                                                 is_non_type,
11942                                                 is_parameter_pack,
11943                                                 0);
11944       else
11945        {
11946          tree err_parm = build_tree_list (parameter, parameter);
11947          parameter_list = chainon (parameter_list, err_parm);
11948        }
11949
11950       /* If the next token is not a `,', we're done.  */
11951       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11952         break;
11953       /* Otherwise, consume the `,' token.  */
11954       cp_lexer_consume_token (parser->lexer);
11955     }
11956
11957   return end_template_parm_list (parameter_list);
11958 }
11959
11960 /* Parse a template-parameter.
11961
11962    template-parameter:
11963      type-parameter
11964      parameter-declaration
11965
11966    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11967    the parameter.  The TREE_PURPOSE is the default value, if any.
11968    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11969    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11970    set to true iff this parameter is a parameter pack. */
11971
11972 static tree
11973 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11974                               bool *is_parameter_pack)
11975 {
11976   cp_token *token;
11977   cp_parameter_declarator *parameter_declarator;
11978   cp_declarator *id_declarator;
11979   tree parm;
11980
11981   /* Assume it is a type parameter or a template parameter.  */
11982   *is_non_type = false;
11983   /* Assume it not a parameter pack. */
11984   *is_parameter_pack = false;
11985   /* Peek at the next token.  */
11986   token = cp_lexer_peek_token (parser->lexer);
11987   /* If it is `class' or `template', we have a type-parameter.  */
11988   if (token->keyword == RID_TEMPLATE)
11989     return cp_parser_type_parameter (parser, is_parameter_pack);
11990   /* If it is `class' or `typename' we do not know yet whether it is a
11991      type parameter or a non-type parameter.  Consider:
11992
11993        template <typename T, typename T::X X> ...
11994
11995      or:
11996
11997        template <class C, class D*> ...
11998
11999      Here, the first parameter is a type parameter, and the second is
12000      a non-type parameter.  We can tell by looking at the token after
12001      the identifier -- if it is a `,', `=', or `>' then we have a type
12002      parameter.  */
12003   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
12004     {
12005       /* Peek at the token after `class' or `typename'.  */
12006       token = cp_lexer_peek_nth_token (parser->lexer, 2);
12007       /* If it's an ellipsis, we have a template type parameter
12008          pack. */
12009       if (token->type == CPP_ELLIPSIS)
12010         return cp_parser_type_parameter (parser, is_parameter_pack);
12011       /* If it's an identifier, skip it.  */
12012       if (token->type == CPP_NAME)
12013         token = cp_lexer_peek_nth_token (parser->lexer, 3);
12014       /* Now, see if the token looks like the end of a template
12015          parameter.  */
12016       if (token->type == CPP_COMMA
12017           || token->type == CPP_EQ
12018           || token->type == CPP_GREATER)
12019         return cp_parser_type_parameter (parser, is_parameter_pack);
12020     }
12021
12022   /* Otherwise, it is a non-type parameter.
12023
12024      [temp.param]
12025
12026      When parsing a default template-argument for a non-type
12027      template-parameter, the first non-nested `>' is taken as the end
12028      of the template parameter-list rather than a greater-than
12029      operator.  */
12030   *is_non_type = true;
12031   parameter_declarator
12032      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
12033                                         /*parenthesized_p=*/NULL);
12034
12035   /* If the parameter declaration is marked as a parameter pack, set
12036      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
12037      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
12038      grokdeclarator. */
12039   if (parameter_declarator
12040       && parameter_declarator->declarator
12041       && parameter_declarator->declarator->parameter_pack_p)
12042     {
12043       *is_parameter_pack = true;
12044       parameter_declarator->declarator->parameter_pack_p = false;
12045     }
12046
12047   /* If the next token is an ellipsis, and we don't already have it
12048      marked as a parameter pack, then we have a parameter pack (that
12049      has no declarator).  */
12050   if (!*is_parameter_pack
12051       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
12052       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
12053     {
12054       /* Consume the `...'.  */
12055       cp_lexer_consume_token (parser->lexer);
12056       maybe_warn_variadic_templates ();
12057       
12058       *is_parameter_pack = true;
12059     }
12060   /* We might end up with a pack expansion as the type of the non-type
12061      template parameter, in which case this is a non-type template
12062      parameter pack.  */
12063   else if (parameter_declarator
12064            && parameter_declarator->decl_specifiers.type
12065            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12066     {
12067       *is_parameter_pack = true;
12068       parameter_declarator->decl_specifiers.type = 
12069         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12070     }
12071
12072   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12073     {
12074       /* Parameter packs cannot have default arguments.  However, a
12075          user may try to do so, so we'll parse them and give an
12076          appropriate diagnostic here.  */
12077
12078       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12079       
12080       /* Find the name of the parameter pack.  */     
12081       id_declarator = parameter_declarator->declarator;
12082       while (id_declarator && id_declarator->kind != cdk_id)
12083         id_declarator = id_declarator->declarator;
12084       
12085       if (id_declarator && id_declarator->kind == cdk_id)
12086         error_at (start_token->location,
12087                   "template parameter pack %qD cannot have a default argument",
12088                   id_declarator->u.id.unqualified_name);
12089       else
12090         error_at (start_token->location,
12091                   "template parameter pack cannot have a default argument");
12092       
12093       /* Parse the default argument, but throw away the result.  */
12094       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12095     }
12096
12097   parm = grokdeclarator (parameter_declarator->declarator,
12098                          &parameter_declarator->decl_specifiers,
12099                          TPARM, /*initialized=*/0,
12100                          /*attrlist=*/NULL);
12101   if (parm == error_mark_node)
12102     return error_mark_node;
12103
12104   return build_tree_list (parameter_declarator->default_argument, parm);
12105 }
12106
12107 /* Parse a type-parameter.
12108
12109    type-parameter:
12110      class identifier [opt]
12111      class identifier [opt] = type-id
12112      typename identifier [opt]
12113      typename identifier [opt] = type-id
12114      template < template-parameter-list > class identifier [opt]
12115      template < template-parameter-list > class identifier [opt]
12116        = id-expression
12117
12118    GNU Extension (variadic templates):
12119
12120    type-parameter:
12121      class ... identifier [opt]
12122      typename ... identifier [opt]
12123
12124    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12125    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12126    the declaration of the parameter.
12127
12128    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12129
12130 static tree
12131 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12132 {
12133   cp_token *token;
12134   tree parameter;
12135
12136   /* Look for a keyword to tell us what kind of parameter this is.  */
12137   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12138   if (!token)
12139     return error_mark_node;
12140
12141   switch (token->keyword)
12142     {
12143     case RID_CLASS:
12144     case RID_TYPENAME:
12145       {
12146         tree identifier;
12147         tree default_argument;
12148
12149         /* If the next token is an ellipsis, we have a template
12150            argument pack. */
12151         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12152           {
12153             /* Consume the `...' token. */
12154             cp_lexer_consume_token (parser->lexer);
12155             maybe_warn_variadic_templates ();
12156
12157             *is_parameter_pack = true;
12158           }
12159
12160         /* If the next token is an identifier, then it names the
12161            parameter.  */
12162         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12163           identifier = cp_parser_identifier (parser);
12164         else
12165           identifier = NULL_TREE;
12166
12167         /* Create the parameter.  */
12168         parameter = finish_template_type_parm (class_type_node, identifier);
12169
12170         /* If the next token is an `=', we have a default argument.  */
12171         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12172           {
12173             /* Consume the `=' token.  */
12174             cp_lexer_consume_token (parser->lexer);
12175             /* Parse the default-argument.  */
12176             push_deferring_access_checks (dk_no_deferred);
12177             default_argument = cp_parser_type_id (parser);
12178
12179             /* Template parameter packs cannot have default
12180                arguments. */
12181             if (*is_parameter_pack)
12182               {
12183                 if (identifier)
12184                   error_at (token->location,
12185                             "template parameter pack %qD cannot have a "
12186                             "default argument", identifier);
12187                 else
12188                   error_at (token->location,
12189                             "template parameter packs cannot have "
12190                             "default arguments");
12191                 default_argument = NULL_TREE;
12192               }
12193             pop_deferring_access_checks ();
12194           }
12195         else
12196           default_argument = NULL_TREE;
12197
12198         /* Create the combined representation of the parameter and the
12199            default argument.  */
12200         parameter = build_tree_list (default_argument, parameter);
12201       }
12202       break;
12203
12204     case RID_TEMPLATE:
12205       {
12206         tree identifier;
12207         tree default_argument;
12208
12209         /* Look for the `<'.  */
12210         cp_parser_require (parser, CPP_LESS, RT_LESS);
12211         /* Parse the template-parameter-list.  */
12212         cp_parser_template_parameter_list (parser);
12213         /* Look for the `>'.  */
12214         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12215         /* Look for the `class' keyword.  */
12216         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12217         /* If the next token is an ellipsis, we have a template
12218            argument pack. */
12219         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12220           {
12221             /* Consume the `...' token. */
12222             cp_lexer_consume_token (parser->lexer);
12223             maybe_warn_variadic_templates ();
12224
12225             *is_parameter_pack = true;
12226           }
12227         /* If the next token is an `=', then there is a
12228            default-argument.  If the next token is a `>', we are at
12229            the end of the parameter-list.  If the next token is a `,',
12230            then we are at the end of this parameter.  */
12231         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12232             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12233             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12234           {
12235             identifier = cp_parser_identifier (parser);
12236             /* Treat invalid names as if the parameter were nameless.  */
12237             if (identifier == error_mark_node)
12238               identifier = NULL_TREE;
12239           }
12240         else
12241           identifier = NULL_TREE;
12242
12243         /* Create the template parameter.  */
12244         parameter = finish_template_template_parm (class_type_node,
12245                                                    identifier);
12246
12247         /* If the next token is an `=', then there is a
12248            default-argument.  */
12249         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12250           {
12251             bool is_template;
12252
12253             /* Consume the `='.  */
12254             cp_lexer_consume_token (parser->lexer);
12255             /* Parse the id-expression.  */
12256             push_deferring_access_checks (dk_no_deferred);
12257             /* save token before parsing the id-expression, for error
12258                reporting */
12259             token = cp_lexer_peek_token (parser->lexer);
12260             default_argument
12261               = cp_parser_id_expression (parser,
12262                                          /*template_keyword_p=*/false,
12263                                          /*check_dependency_p=*/true,
12264                                          /*template_p=*/&is_template,
12265                                          /*declarator_p=*/false,
12266                                          /*optional_p=*/false);
12267             if (TREE_CODE (default_argument) == TYPE_DECL)
12268               /* If the id-expression was a template-id that refers to
12269                  a template-class, we already have the declaration here,
12270                  so no further lookup is needed.  */
12271                  ;
12272             else
12273               /* Look up the name.  */
12274               default_argument
12275                 = cp_parser_lookup_name (parser, default_argument,
12276                                          none_type,
12277                                          /*is_template=*/is_template,
12278                                          /*is_namespace=*/false,
12279                                          /*check_dependency=*/true,
12280                                          /*ambiguous_decls=*/NULL,
12281                                          token->location);
12282             /* See if the default argument is valid.  */
12283             default_argument
12284               = check_template_template_default_arg (default_argument);
12285
12286             /* Template parameter packs cannot have default
12287                arguments. */
12288             if (*is_parameter_pack)
12289               {
12290                 if (identifier)
12291                   error_at (token->location,
12292                             "template parameter pack %qD cannot "
12293                             "have a default argument",
12294                             identifier);
12295                 else
12296                   error_at (token->location, "template parameter packs cannot "
12297                             "have default arguments");
12298                 default_argument = NULL_TREE;
12299               }
12300             pop_deferring_access_checks ();
12301           }
12302         else
12303           default_argument = NULL_TREE;
12304
12305         /* Create the combined representation of the parameter and the
12306            default argument.  */
12307         parameter = build_tree_list (default_argument, parameter);
12308       }
12309       break;
12310
12311     default:
12312       gcc_unreachable ();
12313       break;
12314     }
12315
12316   return parameter;
12317 }
12318
12319 /* Parse a template-id.
12320
12321    template-id:
12322      template-name < template-argument-list [opt] >
12323
12324    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12325    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12326    returned.  Otherwise, if the template-name names a function, or set
12327    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12328    names a class, returns a TYPE_DECL for the specialization.
12329
12330    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12331    uninstantiated templates.  */
12332
12333 static tree
12334 cp_parser_template_id (cp_parser *parser,
12335                        bool template_keyword_p,
12336                        bool check_dependency_p,
12337                        bool is_declaration)
12338 {
12339   int i;
12340   tree templ;
12341   tree arguments;
12342   tree template_id;
12343   cp_token_position start_of_id = 0;
12344   deferred_access_check *chk;
12345   VEC (deferred_access_check,gc) *access_check;
12346   cp_token *next_token = NULL, *next_token_2 = NULL;
12347   bool is_identifier;
12348
12349   /* If the next token corresponds to a template-id, there is no need
12350      to reparse it.  */
12351   next_token = cp_lexer_peek_token (parser->lexer);
12352   if (next_token->type == CPP_TEMPLATE_ID)
12353     {
12354       struct tree_check *check_value;
12355
12356       /* Get the stored value.  */
12357       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12358       /* Perform any access checks that were deferred.  */
12359       access_check = check_value->checks;
12360       if (access_check)
12361         {
12362           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12363             perform_or_defer_access_check (chk->binfo,
12364                                            chk->decl,
12365                                            chk->diag_decl);
12366         }
12367       /* Return the stored value.  */
12368       return check_value->value;
12369     }
12370
12371   /* Avoid performing name lookup if there is no possibility of
12372      finding a template-id.  */
12373   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12374       || (next_token->type == CPP_NAME
12375           && !cp_parser_nth_token_starts_template_argument_list_p
12376                (parser, 2)))
12377     {
12378       cp_parser_error (parser, "expected template-id");
12379       return error_mark_node;
12380     }
12381
12382   /* Remember where the template-id starts.  */
12383   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12384     start_of_id = cp_lexer_token_position (parser->lexer, false);
12385
12386   push_deferring_access_checks (dk_deferred);
12387
12388   /* Parse the template-name.  */
12389   is_identifier = false;
12390   templ = cp_parser_template_name (parser, template_keyword_p,
12391                                    check_dependency_p,
12392                                    is_declaration,
12393                                    &is_identifier);
12394   if (templ == error_mark_node || is_identifier)
12395     {
12396       pop_deferring_access_checks ();
12397       return templ;
12398     }
12399
12400   /* If we find the sequence `[:' after a template-name, it's probably
12401      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12402      parse correctly the argument list.  */
12403   next_token = cp_lexer_peek_token (parser->lexer);
12404   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12405   if (next_token->type == CPP_OPEN_SQUARE
12406       && next_token->flags & DIGRAPH
12407       && next_token_2->type == CPP_COLON
12408       && !(next_token_2->flags & PREV_WHITE))
12409     {
12410       cp_parser_parse_tentatively (parser);
12411       /* Change `:' into `::'.  */
12412       next_token_2->type = CPP_SCOPE;
12413       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12414          CPP_LESS.  */
12415       cp_lexer_consume_token (parser->lexer);
12416
12417       /* Parse the arguments.  */
12418       arguments = cp_parser_enclosed_template_argument_list (parser);
12419       if (!cp_parser_parse_definitely (parser))
12420         {
12421           /* If we couldn't parse an argument list, then we revert our changes
12422              and return simply an error. Maybe this is not a template-id
12423              after all.  */
12424           next_token_2->type = CPP_COLON;
12425           cp_parser_error (parser, "expected %<<%>");
12426           pop_deferring_access_checks ();
12427           return error_mark_node;
12428         }
12429       /* Otherwise, emit an error about the invalid digraph, but continue
12430          parsing because we got our argument list.  */
12431       if (permerror (next_token->location,
12432                      "%<<::%> cannot begin a template-argument list"))
12433         {
12434           static bool hint = false;
12435           inform (next_token->location,
12436                   "%<<:%> is an alternate spelling for %<[%>."
12437                   " Insert whitespace between %<<%> and %<::%>");
12438           if (!hint && !flag_permissive)
12439             {
12440               inform (next_token->location, "(if you use %<-fpermissive%>"
12441                       " G++ will accept your code)");
12442               hint = true;
12443             }
12444         }
12445     }
12446   else
12447     {
12448       /* Look for the `<' that starts the template-argument-list.  */
12449       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12450         {
12451           pop_deferring_access_checks ();
12452           return error_mark_node;
12453         }
12454       /* Parse the arguments.  */
12455       arguments = cp_parser_enclosed_template_argument_list (parser);
12456     }
12457
12458   /* Build a representation of the specialization.  */
12459   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12460     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12461   else if (DECL_TYPE_TEMPLATE_P (templ)
12462            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12463     {
12464       bool entering_scope;
12465       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12466          template (rather than some instantiation thereof) only if
12467          is not nested within some other construct.  For example, in
12468          "template <typename T> void f(T) { A<T>::", A<T> is just an
12469          instantiation of A.  */
12470       entering_scope = (template_parm_scope_p ()
12471                         && cp_lexer_next_token_is (parser->lexer,
12472                                                    CPP_SCOPE));
12473       template_id
12474         = finish_template_type (templ, arguments, entering_scope);
12475     }
12476   else
12477     {
12478       /* If it's not a class-template or a template-template, it should be
12479          a function-template.  */
12480       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12481                    || TREE_CODE (templ) == OVERLOAD
12482                    || BASELINK_P (templ)));
12483
12484       template_id = lookup_template_function (templ, arguments);
12485     }
12486
12487   /* If parsing tentatively, replace the sequence of tokens that makes
12488      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12489      should we re-parse the token stream, we will not have to repeat
12490      the effort required to do the parse, nor will we issue duplicate
12491      error messages about problems during instantiation of the
12492      template.  */
12493   if (start_of_id)
12494     {
12495       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12496
12497       /* Reset the contents of the START_OF_ID token.  */
12498       token->type = CPP_TEMPLATE_ID;
12499       /* Retrieve any deferred checks.  Do not pop this access checks yet
12500          so the memory will not be reclaimed during token replacing below.  */
12501       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12502       token->u.tree_check_value->value = template_id;
12503       token->u.tree_check_value->checks = get_deferred_access_checks ();
12504       token->keyword = RID_MAX;
12505
12506       /* Purge all subsequent tokens.  */
12507       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12508
12509       /* ??? Can we actually assume that, if template_id ==
12510          error_mark_node, we will have issued a diagnostic to the
12511          user, as opposed to simply marking the tentative parse as
12512          failed?  */
12513       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12514         error_at (token->location, "parse error in template argument list");
12515     }
12516
12517   pop_deferring_access_checks ();
12518   return template_id;
12519 }
12520
12521 /* Parse a template-name.
12522
12523    template-name:
12524      identifier
12525
12526    The standard should actually say:
12527
12528    template-name:
12529      identifier
12530      operator-function-id
12531
12532    A defect report has been filed about this issue.
12533
12534    A conversion-function-id cannot be a template name because they cannot
12535    be part of a template-id. In fact, looking at this code:
12536
12537    a.operator K<int>()
12538
12539    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12540    It is impossible to call a templated conversion-function-id with an
12541    explicit argument list, since the only allowed template parameter is
12542    the type to which it is converting.
12543
12544    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12545    `template' keyword, in a construction like:
12546
12547      T::template f<3>()
12548
12549    In that case `f' is taken to be a template-name, even though there
12550    is no way of knowing for sure.
12551
12552    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12553    name refers to a set of overloaded functions, at least one of which
12554    is a template, or an IDENTIFIER_NODE with the name of the template,
12555    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12556    names are looked up inside uninstantiated templates.  */
12557
12558 static tree
12559 cp_parser_template_name (cp_parser* parser,
12560                          bool template_keyword_p,
12561                          bool check_dependency_p,
12562                          bool is_declaration,
12563                          bool *is_identifier)
12564 {
12565   tree identifier;
12566   tree decl;
12567   tree fns;
12568   cp_token *token = cp_lexer_peek_token (parser->lexer);
12569
12570   /* If the next token is `operator', then we have either an
12571      operator-function-id or a conversion-function-id.  */
12572   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12573     {
12574       /* We don't know whether we're looking at an
12575          operator-function-id or a conversion-function-id.  */
12576       cp_parser_parse_tentatively (parser);
12577       /* Try an operator-function-id.  */
12578       identifier = cp_parser_operator_function_id (parser);
12579       /* If that didn't work, try a conversion-function-id.  */
12580       if (!cp_parser_parse_definitely (parser))
12581         {
12582           cp_parser_error (parser, "expected template-name");
12583           return error_mark_node;
12584         }
12585     }
12586   /* Look for the identifier.  */
12587   else
12588     identifier = cp_parser_identifier (parser);
12589
12590   /* If we didn't find an identifier, we don't have a template-id.  */
12591   if (identifier == error_mark_node)
12592     return error_mark_node;
12593
12594   /* If the name immediately followed the `template' keyword, then it
12595      is a template-name.  However, if the next token is not `<', then
12596      we do not treat it as a template-name, since it is not being used
12597      as part of a template-id.  This enables us to handle constructs
12598      like:
12599
12600        template <typename T> struct S { S(); };
12601        template <typename T> S<T>::S();
12602
12603      correctly.  We would treat `S' as a template -- if it were `S<T>'
12604      -- but we do not if there is no `<'.  */
12605
12606   if (processing_template_decl
12607       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12608     {
12609       /* In a declaration, in a dependent context, we pretend that the
12610          "template" keyword was present in order to improve error
12611          recovery.  For example, given:
12612
12613            template <typename T> void f(T::X<int>);
12614
12615          we want to treat "X<int>" as a template-id.  */
12616       if (is_declaration
12617           && !template_keyword_p
12618           && parser->scope && TYPE_P (parser->scope)
12619           && check_dependency_p
12620           && dependent_scope_p (parser->scope)
12621           /* Do not do this for dtors (or ctors), since they never
12622              need the template keyword before their name.  */
12623           && !constructor_name_p (identifier, parser->scope))
12624         {
12625           cp_token_position start = 0;
12626
12627           /* Explain what went wrong.  */
12628           error_at (token->location, "non-template %qD used as template",
12629                     identifier);
12630           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12631                   parser->scope, identifier);
12632           /* If parsing tentatively, find the location of the "<" token.  */
12633           if (cp_parser_simulate_error (parser))
12634             start = cp_lexer_token_position (parser->lexer, true);
12635           /* Parse the template arguments so that we can issue error
12636              messages about them.  */
12637           cp_lexer_consume_token (parser->lexer);
12638           cp_parser_enclosed_template_argument_list (parser);
12639           /* Skip tokens until we find a good place from which to
12640              continue parsing.  */
12641           cp_parser_skip_to_closing_parenthesis (parser,
12642                                                  /*recovering=*/true,
12643                                                  /*or_comma=*/true,
12644                                                  /*consume_paren=*/false);
12645           /* If parsing tentatively, permanently remove the
12646              template argument list.  That will prevent duplicate
12647              error messages from being issued about the missing
12648              "template" keyword.  */
12649           if (start)
12650             cp_lexer_purge_tokens_after (parser->lexer, start);
12651           if (is_identifier)
12652             *is_identifier = true;
12653           return identifier;
12654         }
12655
12656       /* If the "template" keyword is present, then there is generally
12657          no point in doing name-lookup, so we just return IDENTIFIER.
12658          But, if the qualifying scope is non-dependent then we can
12659          (and must) do name-lookup normally.  */
12660       if (template_keyword_p
12661           && (!parser->scope
12662               || (TYPE_P (parser->scope)
12663                   && dependent_type_p (parser->scope))))
12664         return identifier;
12665     }
12666
12667   /* Look up the name.  */
12668   decl = cp_parser_lookup_name (parser, identifier,
12669                                 none_type,
12670                                 /*is_template=*/true,
12671                                 /*is_namespace=*/false,
12672                                 check_dependency_p,
12673                                 /*ambiguous_decls=*/NULL,
12674                                 token->location);
12675
12676   /* If DECL is a template, then the name was a template-name.  */
12677   if (TREE_CODE (decl) == TEMPLATE_DECL)
12678     ;
12679   else
12680     {
12681       tree fn = NULL_TREE;
12682
12683       /* The standard does not explicitly indicate whether a name that
12684          names a set of overloaded declarations, some of which are
12685          templates, is a template-name.  However, such a name should
12686          be a template-name; otherwise, there is no way to form a
12687          template-id for the overloaded templates.  */
12688       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12689       if (TREE_CODE (fns) == OVERLOAD)
12690         for (fn = fns; fn; fn = OVL_NEXT (fn))
12691           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12692             break;
12693
12694       if (!fn)
12695         {
12696           /* The name does not name a template.  */
12697           cp_parser_error (parser, "expected template-name");
12698           return error_mark_node;
12699         }
12700     }
12701
12702   /* If DECL is dependent, and refers to a function, then just return
12703      its name; we will look it up again during template instantiation.  */
12704   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12705     {
12706       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12707       if (TYPE_P (scope) && dependent_type_p (scope))
12708         return identifier;
12709     }
12710
12711   return decl;
12712 }
12713
12714 /* Parse a template-argument-list.
12715
12716    template-argument-list:
12717      template-argument ... [opt]
12718      template-argument-list , template-argument ... [opt]
12719
12720    Returns a TREE_VEC containing the arguments.  */
12721
12722 static tree
12723 cp_parser_template_argument_list (cp_parser* parser)
12724 {
12725   tree fixed_args[10];
12726   unsigned n_args = 0;
12727   unsigned alloced = 10;
12728   tree *arg_ary = fixed_args;
12729   tree vec;
12730   bool saved_in_template_argument_list_p;
12731   bool saved_ice_p;
12732   bool saved_non_ice_p;
12733
12734   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12735   parser->in_template_argument_list_p = true;
12736   /* Even if the template-id appears in an integral
12737      constant-expression, the contents of the argument list do
12738      not.  */
12739   saved_ice_p = parser->integral_constant_expression_p;
12740   parser->integral_constant_expression_p = false;
12741   saved_non_ice_p = parser->non_integral_constant_expression_p;
12742   parser->non_integral_constant_expression_p = false;
12743
12744   /* Parse the arguments.  */
12745   do
12746     {
12747       tree argument;
12748
12749       if (n_args)
12750         /* Consume the comma.  */
12751         cp_lexer_consume_token (parser->lexer);
12752
12753       /* Parse the template-argument.  */
12754       argument = cp_parser_template_argument (parser);
12755
12756       /* If the next token is an ellipsis, we're expanding a template
12757          argument pack. */
12758       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12759         {
12760           if (argument == error_mark_node)
12761             {
12762               cp_token *token = cp_lexer_peek_token (parser->lexer);
12763               error_at (token->location,
12764                         "expected parameter pack before %<...%>");
12765             }
12766           /* Consume the `...' token. */
12767           cp_lexer_consume_token (parser->lexer);
12768
12769           /* Make the argument into a TYPE_PACK_EXPANSION or
12770              EXPR_PACK_EXPANSION. */
12771           argument = make_pack_expansion (argument);
12772         }
12773
12774       if (n_args == alloced)
12775         {
12776           alloced *= 2;
12777
12778           if (arg_ary == fixed_args)
12779             {
12780               arg_ary = XNEWVEC (tree, alloced);
12781               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12782             }
12783           else
12784             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12785         }
12786       arg_ary[n_args++] = argument;
12787     }
12788   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12789
12790   vec = make_tree_vec (n_args);
12791
12792   while (n_args--)
12793     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12794
12795   if (arg_ary != fixed_args)
12796     free (arg_ary);
12797   parser->non_integral_constant_expression_p = saved_non_ice_p;
12798   parser->integral_constant_expression_p = saved_ice_p;
12799   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12800 #ifdef ENABLE_CHECKING
12801   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12802 #endif
12803   return vec;
12804 }
12805
12806 /* Parse a template-argument.
12807
12808    template-argument:
12809      assignment-expression
12810      type-id
12811      id-expression
12812
12813    The representation is that of an assignment-expression, type-id, or
12814    id-expression -- except that the qualified id-expression is
12815    evaluated, so that the value returned is either a DECL or an
12816    OVERLOAD.
12817
12818    Although the standard says "assignment-expression", it forbids
12819    throw-expressions or assignments in the template argument.
12820    Therefore, we use "conditional-expression" instead.  */
12821
12822 static tree
12823 cp_parser_template_argument (cp_parser* parser)
12824 {
12825   tree argument;
12826   bool template_p;
12827   bool address_p;
12828   bool maybe_type_id = false;
12829   cp_token *token = NULL, *argument_start_token = NULL;
12830   cp_id_kind idk;
12831
12832   /* There's really no way to know what we're looking at, so we just
12833      try each alternative in order.
12834
12835        [temp.arg]
12836
12837        In a template-argument, an ambiguity between a type-id and an
12838        expression is resolved to a type-id, regardless of the form of
12839        the corresponding template-parameter.
12840
12841      Therefore, we try a type-id first.  */
12842   cp_parser_parse_tentatively (parser);
12843   argument = cp_parser_template_type_arg (parser);
12844   /* If there was no error parsing the type-id but the next token is a
12845      '>>', our behavior depends on which dialect of C++ we're
12846      parsing. In C++98, we probably found a typo for '> >'. But there
12847      are type-id which are also valid expressions. For instance:
12848
12849      struct X { int operator >> (int); };
12850      template <int V> struct Foo {};
12851      Foo<X () >> 5> r;
12852
12853      Here 'X()' is a valid type-id of a function type, but the user just
12854      wanted to write the expression "X() >> 5". Thus, we remember that we
12855      found a valid type-id, but we still try to parse the argument as an
12856      expression to see what happens. 
12857
12858      In C++0x, the '>>' will be considered two separate '>'
12859      tokens.  */
12860   if (!cp_parser_error_occurred (parser)
12861       && cxx_dialect == cxx98
12862       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12863     {
12864       maybe_type_id = true;
12865       cp_parser_abort_tentative_parse (parser);
12866     }
12867   else
12868     {
12869       /* If the next token isn't a `,' or a `>', then this argument wasn't
12870       really finished. This means that the argument is not a valid
12871       type-id.  */
12872       if (!cp_parser_next_token_ends_template_argument_p (parser))
12873         cp_parser_error (parser, "expected template-argument");
12874       /* If that worked, we're done.  */
12875       if (cp_parser_parse_definitely (parser))
12876         return argument;
12877     }
12878   /* We're still not sure what the argument will be.  */
12879   cp_parser_parse_tentatively (parser);
12880   /* Try a template.  */
12881   argument_start_token = cp_lexer_peek_token (parser->lexer);
12882   argument = cp_parser_id_expression (parser,
12883                                       /*template_keyword_p=*/false,
12884                                       /*check_dependency_p=*/true,
12885                                       &template_p,
12886                                       /*declarator_p=*/false,
12887                                       /*optional_p=*/false);
12888   /* If the next token isn't a `,' or a `>', then this argument wasn't
12889      really finished.  */
12890   if (!cp_parser_next_token_ends_template_argument_p (parser))
12891     cp_parser_error (parser, "expected template-argument");
12892   if (!cp_parser_error_occurred (parser))
12893     {
12894       /* Figure out what is being referred to.  If the id-expression
12895          was for a class template specialization, then we will have a
12896          TYPE_DECL at this point.  There is no need to do name lookup
12897          at this point in that case.  */
12898       if (TREE_CODE (argument) != TYPE_DECL)
12899         argument = cp_parser_lookup_name (parser, argument,
12900                                           none_type,
12901                                           /*is_template=*/template_p,
12902                                           /*is_namespace=*/false,
12903                                           /*check_dependency=*/true,
12904                                           /*ambiguous_decls=*/NULL,
12905                                           argument_start_token->location);
12906       if (TREE_CODE (argument) != TEMPLATE_DECL
12907           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12908         cp_parser_error (parser, "expected template-name");
12909     }
12910   if (cp_parser_parse_definitely (parser))
12911     return argument;
12912   /* It must be a non-type argument.  There permitted cases are given
12913      in [temp.arg.nontype]:
12914
12915      -- an integral constant-expression of integral or enumeration
12916         type; or
12917
12918      -- the name of a non-type template-parameter; or
12919
12920      -- the name of an object or function with external linkage...
12921
12922      -- the address of an object or function with external linkage...
12923
12924      -- a pointer to member...  */
12925   /* Look for a non-type template parameter.  */
12926   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12927     {
12928       cp_parser_parse_tentatively (parser);
12929       argument = cp_parser_primary_expression (parser,
12930                                                /*address_p=*/false,
12931                                                /*cast_p=*/false,
12932                                                /*template_arg_p=*/true,
12933                                                &idk);
12934       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12935           || !cp_parser_next_token_ends_template_argument_p (parser))
12936         cp_parser_simulate_error (parser);
12937       if (cp_parser_parse_definitely (parser))
12938         return argument;
12939     }
12940
12941   /* If the next token is "&", the argument must be the address of an
12942      object or function with external linkage.  */
12943   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12944   if (address_p)
12945     cp_lexer_consume_token (parser->lexer);
12946   /* See if we might have an id-expression.  */
12947   token = cp_lexer_peek_token (parser->lexer);
12948   if (token->type == CPP_NAME
12949       || token->keyword == RID_OPERATOR
12950       || token->type == CPP_SCOPE
12951       || token->type == CPP_TEMPLATE_ID
12952       || token->type == CPP_NESTED_NAME_SPECIFIER)
12953     {
12954       cp_parser_parse_tentatively (parser);
12955       argument = cp_parser_primary_expression (parser,
12956                                                address_p,
12957                                                /*cast_p=*/false,
12958                                                /*template_arg_p=*/true,
12959                                                &idk);
12960       if (cp_parser_error_occurred (parser)
12961           || !cp_parser_next_token_ends_template_argument_p (parser))
12962         cp_parser_abort_tentative_parse (parser);
12963       else
12964         {
12965           tree probe;
12966
12967           if (TREE_CODE (argument) == INDIRECT_REF)
12968             {
12969               gcc_assert (REFERENCE_REF_P (argument));
12970               argument = TREE_OPERAND (argument, 0);
12971             }
12972
12973           /* If we're in a template, we represent a qualified-id referring
12974              to a static data member as a SCOPE_REF even if the scope isn't
12975              dependent so that we can check access control later.  */
12976           probe = argument;
12977           if (TREE_CODE (probe) == SCOPE_REF)
12978             probe = TREE_OPERAND (probe, 1);
12979           if (TREE_CODE (probe) == VAR_DECL)
12980             {
12981               /* A variable without external linkage might still be a
12982                  valid constant-expression, so no error is issued here
12983                  if the external-linkage check fails.  */
12984               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12985                 cp_parser_simulate_error (parser);
12986             }
12987           else if (is_overloaded_fn (argument))
12988             /* All overloaded functions are allowed; if the external
12989                linkage test does not pass, an error will be issued
12990                later.  */
12991             ;
12992           else if (address_p
12993                    && (TREE_CODE (argument) == OFFSET_REF
12994                        || TREE_CODE (argument) == SCOPE_REF))
12995             /* A pointer-to-member.  */
12996             ;
12997           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12998             ;
12999           else
13000             cp_parser_simulate_error (parser);
13001
13002           if (cp_parser_parse_definitely (parser))
13003             {
13004               if (address_p)
13005                 argument = build_x_unary_op (ADDR_EXPR, argument,
13006                                              tf_warning_or_error);
13007               return argument;
13008             }
13009         }
13010     }
13011   /* If the argument started with "&", there are no other valid
13012      alternatives at this point.  */
13013   if (address_p)
13014     {
13015       cp_parser_error (parser, "invalid non-type template argument");
13016       return error_mark_node;
13017     }
13018
13019   /* If the argument wasn't successfully parsed as a type-id followed
13020      by '>>', the argument can only be a constant expression now.
13021      Otherwise, we try parsing the constant-expression tentatively,
13022      because the argument could really be a type-id.  */
13023   if (maybe_type_id)
13024     cp_parser_parse_tentatively (parser);
13025   argument = cp_parser_constant_expression (parser,
13026                                             /*allow_non_constant_p=*/false,
13027                                             /*non_constant_p=*/NULL);
13028   argument = fold_non_dependent_expr (argument);
13029   if (!maybe_type_id)
13030     return argument;
13031   if (!cp_parser_next_token_ends_template_argument_p (parser))
13032     cp_parser_error (parser, "expected template-argument");
13033   if (cp_parser_parse_definitely (parser))
13034     return argument;
13035   /* We did our best to parse the argument as a non type-id, but that
13036      was the only alternative that matched (albeit with a '>' after
13037      it). We can assume it's just a typo from the user, and a
13038      diagnostic will then be issued.  */
13039   return cp_parser_template_type_arg (parser);
13040 }
13041
13042 /* Parse an explicit-instantiation.
13043
13044    explicit-instantiation:
13045      template declaration
13046
13047    Although the standard says `declaration', what it really means is:
13048
13049    explicit-instantiation:
13050      template decl-specifier-seq [opt] declarator [opt] ;
13051
13052    Things like `template int S<int>::i = 5, int S<double>::j;' are not
13053    supposed to be allowed.  A defect report has been filed about this
13054    issue.
13055
13056    GNU Extension:
13057
13058    explicit-instantiation:
13059      storage-class-specifier template
13060        decl-specifier-seq [opt] declarator [opt] ;
13061      function-specifier template
13062        decl-specifier-seq [opt] declarator [opt] ;  */
13063
13064 static void
13065 cp_parser_explicit_instantiation (cp_parser* parser)
13066 {
13067   int declares_class_or_enum;
13068   cp_decl_specifier_seq decl_specifiers;
13069   tree extension_specifier = NULL_TREE;
13070
13071   timevar_push (TV_TEMPLATE_INST);
13072
13073   /* Look for an (optional) storage-class-specifier or
13074      function-specifier.  */
13075   if (cp_parser_allow_gnu_extensions_p (parser))
13076     {
13077       extension_specifier
13078         = cp_parser_storage_class_specifier_opt (parser);
13079       if (!extension_specifier)
13080         extension_specifier
13081           = cp_parser_function_specifier_opt (parser,
13082                                               /*decl_specs=*/NULL);
13083     }
13084
13085   /* Look for the `template' keyword.  */
13086   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13087   /* Let the front end know that we are processing an explicit
13088      instantiation.  */
13089   begin_explicit_instantiation ();
13090   /* [temp.explicit] says that we are supposed to ignore access
13091      control while processing explicit instantiation directives.  */
13092   push_deferring_access_checks (dk_no_check);
13093   /* Parse a decl-specifier-seq.  */
13094   cp_parser_decl_specifier_seq (parser,
13095                                 CP_PARSER_FLAGS_OPTIONAL,
13096                                 &decl_specifiers,
13097                                 &declares_class_or_enum);
13098   /* If there was exactly one decl-specifier, and it declared a class,
13099      and there's no declarator, then we have an explicit type
13100      instantiation.  */
13101   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13102     {
13103       tree type;
13104
13105       type = check_tag_decl (&decl_specifiers);
13106       /* Turn access control back on for names used during
13107          template instantiation.  */
13108       pop_deferring_access_checks ();
13109       if (type)
13110         do_type_instantiation (type, extension_specifier,
13111                                /*complain=*/tf_error);
13112     }
13113   else
13114     {
13115       cp_declarator *declarator;
13116       tree decl;
13117
13118       /* Parse the declarator.  */
13119       declarator
13120         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13121                                 /*ctor_dtor_or_conv_p=*/NULL,
13122                                 /*parenthesized_p=*/NULL,
13123                                 /*member_p=*/false);
13124       if (declares_class_or_enum & 2)
13125         cp_parser_check_for_definition_in_return_type (declarator,
13126                                                        decl_specifiers.type,
13127                                                        decl_specifiers.type_location);
13128       if (declarator != cp_error_declarator)
13129         {
13130           if (decl_specifiers.specs[(int)ds_inline])
13131             permerror (input_location, "explicit instantiation shall not use"
13132                        " %<inline%> specifier");
13133           if (decl_specifiers.specs[(int)ds_constexpr])
13134             permerror (input_location, "explicit instantiation shall not use"
13135                        " %<constexpr%> specifier");
13136
13137           decl = grokdeclarator (declarator, &decl_specifiers,
13138                                  NORMAL, 0, &decl_specifiers.attributes);
13139           /* Turn access control back on for names used during
13140              template instantiation.  */
13141           pop_deferring_access_checks ();
13142           /* Do the explicit instantiation.  */
13143           do_decl_instantiation (decl, extension_specifier);
13144         }
13145       else
13146         {
13147           pop_deferring_access_checks ();
13148           /* Skip the body of the explicit instantiation.  */
13149           cp_parser_skip_to_end_of_statement (parser);
13150         }
13151     }
13152   /* We're done with the instantiation.  */
13153   end_explicit_instantiation ();
13154
13155   cp_parser_consume_semicolon_at_end_of_statement (parser);
13156
13157   timevar_pop (TV_TEMPLATE_INST);
13158 }
13159
13160 /* Parse an explicit-specialization.
13161
13162    explicit-specialization:
13163      template < > declaration
13164
13165    Although the standard says `declaration', what it really means is:
13166
13167    explicit-specialization:
13168      template <> decl-specifier [opt] init-declarator [opt] ;
13169      template <> function-definition
13170      template <> explicit-specialization
13171      template <> template-declaration  */
13172
13173 static void
13174 cp_parser_explicit_specialization (cp_parser* parser)
13175 {
13176   bool need_lang_pop;
13177   cp_token *token = cp_lexer_peek_token (parser->lexer);
13178
13179   /* Look for the `template' keyword.  */
13180   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13181   /* Look for the `<'.  */
13182   cp_parser_require (parser, CPP_LESS, RT_LESS);
13183   /* Look for the `>'.  */
13184   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13185   /* We have processed another parameter list.  */
13186   ++parser->num_template_parameter_lists;
13187   /* [temp]
13188
13189      A template ... explicit specialization ... shall not have C
13190      linkage.  */
13191   if (current_lang_name == lang_name_c)
13192     {
13193       error_at (token->location, "template specialization with C linkage");
13194       /* Give it C++ linkage to avoid confusing other parts of the
13195          front end.  */
13196       push_lang_context (lang_name_cplusplus);
13197       need_lang_pop = true;
13198     }
13199   else
13200     need_lang_pop = false;
13201   /* Let the front end know that we are beginning a specialization.  */
13202   if (!begin_specialization ())
13203     {
13204       end_specialization ();
13205       return;
13206     }
13207
13208   /* If the next keyword is `template', we need to figure out whether
13209      or not we're looking a template-declaration.  */
13210   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13211     {
13212       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13213           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13214         cp_parser_template_declaration_after_export (parser,
13215                                                      /*member_p=*/false);
13216       else
13217         cp_parser_explicit_specialization (parser);
13218     }
13219   else
13220     /* Parse the dependent declaration.  */
13221     cp_parser_single_declaration (parser,
13222                                   /*checks=*/NULL,
13223                                   /*member_p=*/false,
13224                                   /*explicit_specialization_p=*/true,
13225                                   /*friend_p=*/NULL);
13226   /* We're done with the specialization.  */
13227   end_specialization ();
13228   /* For the erroneous case of a template with C linkage, we pushed an
13229      implicit C++ linkage scope; exit that scope now.  */
13230   if (need_lang_pop)
13231     pop_lang_context ();
13232   /* We're done with this parameter list.  */
13233   --parser->num_template_parameter_lists;
13234 }
13235
13236 /* Parse a type-specifier.
13237
13238    type-specifier:
13239      simple-type-specifier
13240      class-specifier
13241      enum-specifier
13242      elaborated-type-specifier
13243      cv-qualifier
13244
13245    GNU Extension:
13246
13247    type-specifier:
13248      __complex__
13249
13250    Returns a representation of the type-specifier.  For a
13251    class-specifier, enum-specifier, or elaborated-type-specifier, a
13252    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13253
13254    The parser flags FLAGS is used to control type-specifier parsing.
13255
13256    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13257    in a decl-specifier-seq.
13258
13259    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13260    class-specifier, enum-specifier, or elaborated-type-specifier, then
13261    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13262    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13263    zero.
13264
13265    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13266    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13267    is set to FALSE.  */
13268
13269 static tree
13270 cp_parser_type_specifier (cp_parser* parser,
13271                           cp_parser_flags flags,
13272                           cp_decl_specifier_seq *decl_specs,
13273                           bool is_declaration,
13274                           int* declares_class_or_enum,
13275                           bool* is_cv_qualifier)
13276 {
13277   tree type_spec = NULL_TREE;
13278   cp_token *token;
13279   enum rid keyword;
13280   cp_decl_spec ds = ds_last;
13281
13282   /* Assume this type-specifier does not declare a new type.  */
13283   if (declares_class_or_enum)
13284     *declares_class_or_enum = 0;
13285   /* And that it does not specify a cv-qualifier.  */
13286   if (is_cv_qualifier)
13287     *is_cv_qualifier = false;
13288   /* Peek at the next token.  */
13289   token = cp_lexer_peek_token (parser->lexer);
13290
13291   /* If we're looking at a keyword, we can use that to guide the
13292      production we choose.  */
13293   keyword = token->keyword;
13294   switch (keyword)
13295     {
13296     case RID_ENUM:
13297       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13298         goto elaborated_type_specifier;
13299
13300       /* Look for the enum-specifier.  */
13301       type_spec = cp_parser_enum_specifier (parser);
13302       /* If that worked, we're done.  */
13303       if (type_spec)
13304         {
13305           if (declares_class_or_enum)
13306             *declares_class_or_enum = 2;
13307           if (decl_specs)
13308             cp_parser_set_decl_spec_type (decl_specs,
13309                                           type_spec,
13310                                           token->location,
13311                                           /*type_definition_p=*/true);
13312           return type_spec;
13313         }
13314       else
13315         goto elaborated_type_specifier;
13316
13317       /* Any of these indicate either a class-specifier, or an
13318          elaborated-type-specifier.  */
13319     case RID_CLASS:
13320     case RID_STRUCT:
13321     case RID_UNION:
13322       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13323         goto elaborated_type_specifier;
13324
13325       /* Parse tentatively so that we can back up if we don't find a
13326          class-specifier.  */
13327       cp_parser_parse_tentatively (parser);
13328       /* Look for the class-specifier.  */
13329       type_spec = cp_parser_class_specifier (parser);
13330       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13331       /* If that worked, we're done.  */
13332       if (cp_parser_parse_definitely (parser))
13333         {
13334           if (declares_class_or_enum)
13335             *declares_class_or_enum = 2;
13336           if (decl_specs)
13337             cp_parser_set_decl_spec_type (decl_specs,
13338                                           type_spec,
13339                                           token->location,
13340                                           /*type_definition_p=*/true);
13341           return type_spec;
13342         }
13343
13344       /* Fall through.  */
13345     elaborated_type_specifier:
13346       /* We're declaring (not defining) a class or enum.  */
13347       if (declares_class_or_enum)
13348         *declares_class_or_enum = 1;
13349
13350       /* Fall through.  */
13351     case RID_TYPENAME:
13352       /* Look for an elaborated-type-specifier.  */
13353       type_spec
13354         = (cp_parser_elaborated_type_specifier
13355            (parser,
13356             decl_specs && decl_specs->specs[(int) ds_friend],
13357             is_declaration));
13358       if (decl_specs)
13359         cp_parser_set_decl_spec_type (decl_specs,
13360                                       type_spec,
13361                                       token->location,
13362                                       /*type_definition_p=*/false);
13363       return type_spec;
13364
13365     case RID_CONST:
13366       ds = ds_const;
13367       if (is_cv_qualifier)
13368         *is_cv_qualifier = true;
13369       break;
13370
13371     case RID_VOLATILE:
13372       ds = ds_volatile;
13373       if (is_cv_qualifier)
13374         *is_cv_qualifier = true;
13375       break;
13376
13377     case RID_RESTRICT:
13378       ds = ds_restrict;
13379       if (is_cv_qualifier)
13380         *is_cv_qualifier = true;
13381       break;
13382
13383     case RID_COMPLEX:
13384       /* The `__complex__' keyword is a GNU extension.  */
13385       ds = ds_complex;
13386       break;
13387
13388     default:
13389       break;
13390     }
13391
13392   /* Handle simple keywords.  */
13393   if (ds != ds_last)
13394     {
13395       if (decl_specs)
13396         {
13397           ++decl_specs->specs[(int)ds];
13398           decl_specs->any_specifiers_p = true;
13399         }
13400       return cp_lexer_consume_token (parser->lexer)->u.value;
13401     }
13402
13403   /* If we do not already have a type-specifier, assume we are looking
13404      at a simple-type-specifier.  */
13405   type_spec = cp_parser_simple_type_specifier (parser,
13406                                                decl_specs,
13407                                                flags);
13408
13409   /* If we didn't find a type-specifier, and a type-specifier was not
13410      optional in this context, issue an error message.  */
13411   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13412     {
13413       cp_parser_error (parser, "expected type specifier");
13414       return error_mark_node;
13415     }
13416
13417   return type_spec;
13418 }
13419
13420 /* Parse a simple-type-specifier.
13421
13422    simple-type-specifier:
13423      :: [opt] nested-name-specifier [opt] type-name
13424      :: [opt] nested-name-specifier template template-id
13425      char
13426      wchar_t
13427      bool
13428      short
13429      int
13430      long
13431      signed
13432      unsigned
13433      float
13434      double
13435      void
13436
13437    C++0x Extension:
13438
13439    simple-type-specifier:
13440      auto
13441      decltype ( expression )   
13442      char16_t
13443      char32_t
13444      __underlying_type ( type-id )
13445
13446    GNU Extension:
13447
13448    simple-type-specifier:
13449      __int128
13450      __typeof__ unary-expression
13451      __typeof__ ( type-id )
13452
13453    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13454    appropriately updated.  */
13455
13456 static tree
13457 cp_parser_simple_type_specifier (cp_parser* parser,
13458                                  cp_decl_specifier_seq *decl_specs,
13459                                  cp_parser_flags flags)
13460 {
13461   tree type = NULL_TREE;
13462   cp_token *token;
13463
13464   /* Peek at the next token.  */
13465   token = cp_lexer_peek_token (parser->lexer);
13466
13467   /* If we're looking at a keyword, things are easy.  */
13468   switch (token->keyword)
13469     {
13470     case RID_CHAR:
13471       if (decl_specs)
13472         decl_specs->explicit_char_p = true;
13473       type = char_type_node;
13474       break;
13475     case RID_CHAR16:
13476       type = char16_type_node;
13477       break;
13478     case RID_CHAR32:
13479       type = char32_type_node;
13480       break;
13481     case RID_WCHAR:
13482       type = wchar_type_node;
13483       break;
13484     case RID_BOOL:
13485       type = boolean_type_node;
13486       break;
13487     case RID_SHORT:
13488       if (decl_specs)
13489         ++decl_specs->specs[(int) ds_short];
13490       type = short_integer_type_node;
13491       break;
13492     case RID_INT:
13493       if (decl_specs)
13494         decl_specs->explicit_int_p = true;
13495       type = integer_type_node;
13496       break;
13497     case RID_INT128:
13498       if (!int128_integer_type_node)
13499         break;
13500       if (decl_specs)
13501         decl_specs->explicit_int128_p = true;
13502       type = int128_integer_type_node;
13503       break;
13504     case RID_LONG:
13505       if (decl_specs)
13506         ++decl_specs->specs[(int) ds_long];
13507       type = long_integer_type_node;
13508       break;
13509     case RID_SIGNED:
13510       if (decl_specs)
13511         ++decl_specs->specs[(int) ds_signed];
13512       type = integer_type_node;
13513       break;
13514     case RID_UNSIGNED:
13515       if (decl_specs)
13516         ++decl_specs->specs[(int) ds_unsigned];
13517       type = unsigned_type_node;
13518       break;
13519     case RID_FLOAT:
13520       type = float_type_node;
13521       break;
13522     case RID_DOUBLE:
13523       type = double_type_node;
13524       break;
13525     case RID_VOID:
13526       type = void_type_node;
13527       break;
13528       
13529     case RID_AUTO:
13530       maybe_warn_cpp0x (CPP0X_AUTO);
13531       type = make_auto ();
13532       break;
13533
13534     case RID_DECLTYPE:
13535       /* Since DR 743, decltype can either be a simple-type-specifier by
13536          itself or begin a nested-name-specifier.  Parsing it will replace
13537          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13538          handling below decide what to do.  */
13539       cp_parser_decltype (parser);
13540       cp_lexer_set_token_position (parser->lexer, token);
13541       break;
13542
13543     case RID_TYPEOF:
13544       /* Consume the `typeof' token.  */
13545       cp_lexer_consume_token (parser->lexer);
13546       /* Parse the operand to `typeof'.  */
13547       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13548       /* If it is not already a TYPE, take its type.  */
13549       if (!TYPE_P (type))
13550         type = finish_typeof (type);
13551
13552       if (decl_specs)
13553         cp_parser_set_decl_spec_type (decl_specs, type,
13554                                       token->location,
13555                                       /*type_definition_p=*/false);
13556
13557       return type;
13558
13559     case RID_UNDERLYING_TYPE:
13560       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13561       if (decl_specs)
13562         cp_parser_set_decl_spec_type (decl_specs, type,
13563                                       token->location,
13564                                       /*type_definition_p=*/false);
13565
13566       return type;
13567
13568     case RID_BASES:
13569     case RID_DIRECT_BASES:
13570       type = cp_parser_trait_expr (parser, token->keyword);
13571       if (decl_specs)
13572        cp_parser_set_decl_spec_type (decl_specs, type,
13573                                      token->location,
13574                                      /*type_definition_p=*/false);
13575       return type;
13576     default:
13577       break;
13578     }
13579
13580   /* If token is an already-parsed decltype not followed by ::,
13581      it's a simple-type-specifier.  */
13582   if (token->type == CPP_DECLTYPE
13583       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13584     {
13585       type = token->u.value;
13586       if (decl_specs)
13587         cp_parser_set_decl_spec_type (decl_specs, type,
13588                                       token->location,
13589                                       /*type_definition_p=*/false);
13590       cp_lexer_consume_token (parser->lexer);
13591       return type;
13592     }
13593
13594   /* If the type-specifier was for a built-in type, we're done.  */
13595   if (type)
13596     {
13597       /* Record the type.  */
13598       if (decl_specs
13599           && (token->keyword != RID_SIGNED
13600               && token->keyword != RID_UNSIGNED
13601               && token->keyword != RID_SHORT
13602               && token->keyword != RID_LONG))
13603         cp_parser_set_decl_spec_type (decl_specs,
13604                                       type,
13605                                       token->location,
13606                                       /*type_definition_p=*/false);
13607       if (decl_specs)
13608         decl_specs->any_specifiers_p = true;
13609
13610       /* Consume the token.  */
13611       cp_lexer_consume_token (parser->lexer);
13612
13613       /* There is no valid C++ program where a non-template type is
13614          followed by a "<".  That usually indicates that the user thought
13615          that the type was a template.  */
13616       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13617
13618       return TYPE_NAME (type);
13619     }
13620
13621   /* The type-specifier must be a user-defined type.  */
13622   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13623     {
13624       bool qualified_p;
13625       bool global_p;
13626
13627       /* Don't gobble tokens or issue error messages if this is an
13628          optional type-specifier.  */
13629       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13630         cp_parser_parse_tentatively (parser);
13631
13632       /* Look for the optional `::' operator.  */
13633       global_p
13634         = (cp_parser_global_scope_opt (parser,
13635                                        /*current_scope_valid_p=*/false)
13636            != NULL_TREE);
13637       /* Look for the nested-name specifier.  */
13638       qualified_p
13639         = (cp_parser_nested_name_specifier_opt (parser,
13640                                                 /*typename_keyword_p=*/false,
13641                                                 /*check_dependency_p=*/true,
13642                                                 /*type_p=*/false,
13643                                                 /*is_declaration=*/false)
13644            != NULL_TREE);
13645       token = cp_lexer_peek_token (parser->lexer);
13646       /* If we have seen a nested-name-specifier, and the next token
13647          is `template', then we are using the template-id production.  */
13648       if (parser->scope
13649           && cp_parser_optional_template_keyword (parser))
13650         {
13651           /* Look for the template-id.  */
13652           type = cp_parser_template_id (parser,
13653                                         /*template_keyword_p=*/true,
13654                                         /*check_dependency_p=*/true,
13655                                         /*is_declaration=*/false);
13656           /* If the template-id did not name a type, we are out of
13657              luck.  */
13658           if (TREE_CODE (type) != TYPE_DECL)
13659             {
13660               cp_parser_error (parser, "expected template-id for type");
13661               type = NULL_TREE;
13662             }
13663         }
13664       /* Otherwise, look for a type-name.  */
13665       else
13666         type = cp_parser_type_name (parser);
13667       /* Keep track of all name-lookups performed in class scopes.  */
13668       if (type
13669           && !global_p
13670           && !qualified_p
13671           && TREE_CODE (type) == TYPE_DECL
13672           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13673         maybe_note_name_used_in_class (DECL_NAME (type), type);
13674       /* If it didn't work out, we don't have a TYPE.  */
13675       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13676           && !cp_parser_parse_definitely (parser))
13677         type = NULL_TREE;
13678       if (type && decl_specs)
13679         cp_parser_set_decl_spec_type (decl_specs, type,
13680                                       token->location,
13681                                       /*type_definition_p=*/false);
13682     }
13683
13684   /* If we didn't get a type-name, issue an error message.  */
13685   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13686     {
13687       cp_parser_error (parser, "expected type-name");
13688       return error_mark_node;
13689     }
13690
13691   if (type && type != error_mark_node)
13692     {
13693       /* See if TYPE is an Objective-C type, and if so, parse and
13694          accept any protocol references following it.  Do this before
13695          the cp_parser_check_for_invalid_template_id() call, because
13696          Objective-C types can be followed by '<...>' which would
13697          enclose protocol names rather than template arguments, and so
13698          everything is fine.  */
13699       if (c_dialect_objc () && !parser->scope
13700           && (objc_is_id (type) || objc_is_class_name (type)))
13701         {
13702           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13703           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13704
13705           /* Clobber the "unqualified" type previously entered into
13706              DECL_SPECS with the new, improved protocol-qualified version.  */
13707           if (decl_specs)
13708             decl_specs->type = qual_type;
13709
13710           return qual_type;
13711         }
13712
13713       /* There is no valid C++ program where a non-template type is
13714          followed by a "<".  That usually indicates that the user
13715          thought that the type was a template.  */
13716       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13717                                                token->location);
13718     }
13719
13720   return type;
13721 }
13722
13723 /* Parse a type-name.
13724
13725    type-name:
13726      class-name
13727      enum-name
13728      typedef-name
13729      simple-template-id [in c++0x]
13730
13731    enum-name:
13732      identifier
13733
13734    typedef-name:
13735      identifier
13736
13737    Returns a TYPE_DECL for the type.  */
13738
13739 static tree
13740 cp_parser_type_name (cp_parser* parser)
13741 {
13742   tree type_decl;
13743
13744   /* We can't know yet whether it is a class-name or not.  */
13745   cp_parser_parse_tentatively (parser);
13746   /* Try a class-name.  */
13747   type_decl = cp_parser_class_name (parser,
13748                                     /*typename_keyword_p=*/false,
13749                                     /*template_keyword_p=*/false,
13750                                     none_type,
13751                                     /*check_dependency_p=*/true,
13752                                     /*class_head_p=*/false,
13753                                     /*is_declaration=*/false);
13754   /* If it's not a class-name, keep looking.  */
13755   if (!cp_parser_parse_definitely (parser))
13756     {
13757       if (cxx_dialect < cxx0x)
13758         /* It must be a typedef-name or an enum-name.  */
13759         return cp_parser_nonclass_name (parser);
13760
13761       cp_parser_parse_tentatively (parser);
13762       /* It is either a simple-template-id representing an
13763          instantiation of an alias template...  */
13764       type_decl = cp_parser_template_id (parser,
13765                                          /*template_keyword_p=*/false,
13766                                          /*check_dependency_p=*/false,
13767                                          /*is_declaration=*/false);
13768       /* Note that this must be an instantiation of an alias template
13769          because [temp.names]/6 says:
13770          
13771              A template-id that names an alias template specialization
13772              is a type-name.
13773
13774          Whereas [temp.names]/7 says:
13775          
13776              A simple-template-id that names a class template
13777              specialization is a class-name.  */
13778       if (type_decl != NULL_TREE
13779           && TREE_CODE (type_decl) == TYPE_DECL
13780           && TYPE_DECL_ALIAS_P (type_decl))
13781         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13782       else
13783         cp_parser_simulate_error (parser);
13784
13785       if (!cp_parser_parse_definitely (parser))
13786         /* ... Or a typedef-name or an enum-name.  */
13787         return cp_parser_nonclass_name (parser);
13788     }
13789
13790   return type_decl;
13791 }
13792
13793 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13794
13795    enum-name:
13796      identifier
13797
13798    typedef-name:
13799      identifier
13800
13801    Returns a TYPE_DECL for the type.  */
13802
13803 static tree
13804 cp_parser_nonclass_name (cp_parser* parser)
13805 {
13806   tree type_decl;
13807   tree identifier;
13808
13809   cp_token *token = cp_lexer_peek_token (parser->lexer);
13810   identifier = cp_parser_identifier (parser);
13811   if (identifier == error_mark_node)
13812     return error_mark_node;
13813
13814   /* Look up the type-name.  */
13815   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13816
13817   if (TREE_CODE (type_decl) == USING_DECL)
13818     {
13819       if (!DECL_DEPENDENT_P (type_decl))
13820         type_decl = strip_using_decl (type_decl);
13821       else if (USING_DECL_TYPENAME_P (type_decl))
13822         {
13823           /* We have found a type introduced by a using
13824              declaration at class scope that refers to a dependent
13825              type.
13826              
13827              using typename :: [opt] nested-name-specifier unqualified-id ;
13828           */
13829           type_decl = make_typename_type (TREE_TYPE (type_decl),
13830                                           DECL_NAME (type_decl),
13831                                           typename_type, tf_error);
13832           if (type_decl != error_mark_node)
13833             type_decl = TYPE_NAME (type_decl);
13834         }
13835     }
13836   
13837   if (TREE_CODE (type_decl) != TYPE_DECL
13838       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13839     {
13840       /* See if this is an Objective-C type.  */
13841       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13842       tree type = objc_get_protocol_qualified_type (identifier, protos);
13843       if (type)
13844         type_decl = TYPE_NAME (type);
13845     }
13846
13847   /* Issue an error if we did not find a type-name.  */
13848   if (TREE_CODE (type_decl) != TYPE_DECL
13849       /* In Objective-C, we have the complication that class names are
13850          normally type names and start declarations (eg, the
13851          "NSObject" in "NSObject *object;"), but can be used in an
13852          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13853          is an expression.  So, a classname followed by a dot is not a
13854          valid type-name.  */
13855       || (objc_is_class_name (TREE_TYPE (type_decl))
13856           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13857     {
13858       if (!cp_parser_simulate_error (parser))
13859         cp_parser_name_lookup_error (parser, identifier, type_decl,
13860                                      NLE_TYPE, token->location);
13861       return error_mark_node;
13862     }
13863   /* Remember that the name was used in the definition of the
13864      current class so that we can check later to see if the
13865      meaning would have been different after the class was
13866      entirely defined.  */
13867   else if (type_decl != error_mark_node
13868            && !parser->scope)
13869     maybe_note_name_used_in_class (identifier, type_decl);
13870   
13871   return type_decl;
13872 }
13873
13874 /* Parse an elaborated-type-specifier.  Note that the grammar given
13875    here incorporates the resolution to DR68.
13876
13877    elaborated-type-specifier:
13878      class-key :: [opt] nested-name-specifier [opt] identifier
13879      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13880      enum-key :: [opt] nested-name-specifier [opt] identifier
13881      typename :: [opt] nested-name-specifier identifier
13882      typename :: [opt] nested-name-specifier template [opt]
13883        template-id
13884
13885    GNU extension:
13886
13887    elaborated-type-specifier:
13888      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13889      class-key attributes :: [opt] nested-name-specifier [opt]
13890                template [opt] template-id
13891      enum attributes :: [opt] nested-name-specifier [opt] identifier
13892
13893    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13894    declared `friend'.  If IS_DECLARATION is TRUE, then this
13895    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13896    something is being declared.
13897
13898    Returns the TYPE specified.  */
13899
13900 static tree
13901 cp_parser_elaborated_type_specifier (cp_parser* parser,
13902                                      bool is_friend,
13903                                      bool is_declaration)
13904 {
13905   enum tag_types tag_type;
13906   tree identifier;
13907   tree type = NULL_TREE;
13908   tree attributes = NULL_TREE;
13909   tree globalscope;
13910   cp_token *token = NULL;
13911
13912   /* See if we're looking at the `enum' keyword.  */
13913   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13914     {
13915       /* Consume the `enum' token.  */
13916       cp_lexer_consume_token (parser->lexer);
13917       /* Remember that it's an enumeration type.  */
13918       tag_type = enum_type;
13919       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13920          enums) is used here.  */
13921       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13922           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13923         {
13924             pedwarn (input_location, 0, "elaborated-type-specifier "
13925                       "for a scoped enum must not use the %<%D%> keyword",
13926                       cp_lexer_peek_token (parser->lexer)->u.value);
13927           /* Consume the `struct' or `class' and parse it anyway.  */
13928           cp_lexer_consume_token (parser->lexer);
13929         }
13930       /* Parse the attributes.  */
13931       attributes = cp_parser_attributes_opt (parser);
13932     }
13933   /* Or, it might be `typename'.  */
13934   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13935                                            RID_TYPENAME))
13936     {
13937       /* Consume the `typename' token.  */
13938       cp_lexer_consume_token (parser->lexer);
13939       /* Remember that it's a `typename' type.  */
13940       tag_type = typename_type;
13941     }
13942   /* Otherwise it must be a class-key.  */
13943   else
13944     {
13945       tag_type = cp_parser_class_key (parser);
13946       if (tag_type == none_type)
13947         return error_mark_node;
13948       /* Parse the attributes.  */
13949       attributes = cp_parser_attributes_opt (parser);
13950     }
13951
13952   /* Look for the `::' operator.  */
13953   globalscope =  cp_parser_global_scope_opt (parser,
13954                                              /*current_scope_valid_p=*/false);
13955   /* Look for the nested-name-specifier.  */
13956   if (tag_type == typename_type && !globalscope)
13957     {
13958       if (!cp_parser_nested_name_specifier (parser,
13959                                            /*typename_keyword_p=*/true,
13960                                            /*check_dependency_p=*/true,
13961                                            /*type_p=*/true,
13962                                             is_declaration))
13963         return error_mark_node;
13964     }
13965   else
13966     /* Even though `typename' is not present, the proposed resolution
13967        to Core Issue 180 says that in `class A<T>::B', `B' should be
13968        considered a type-name, even if `A<T>' is dependent.  */
13969     cp_parser_nested_name_specifier_opt (parser,
13970                                          /*typename_keyword_p=*/true,
13971                                          /*check_dependency_p=*/true,
13972                                          /*type_p=*/true,
13973                                          is_declaration);
13974  /* For everything but enumeration types, consider a template-id.
13975     For an enumeration type, consider only a plain identifier.  */
13976   if (tag_type != enum_type)
13977     {
13978       bool template_p = false;
13979       tree decl;
13980
13981       /* Allow the `template' keyword.  */
13982       template_p = cp_parser_optional_template_keyword (parser);
13983       /* If we didn't see `template', we don't know if there's a
13984          template-id or not.  */
13985       if (!template_p)
13986         cp_parser_parse_tentatively (parser);
13987       /* Parse the template-id.  */
13988       token = cp_lexer_peek_token (parser->lexer);
13989       decl = cp_parser_template_id (parser, template_p,
13990                                     /*check_dependency_p=*/true,
13991                                     is_declaration);
13992       /* If we didn't find a template-id, look for an ordinary
13993          identifier.  */
13994       if (!template_p && !cp_parser_parse_definitely (parser))
13995         ;
13996       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13997          in effect, then we must assume that, upon instantiation, the
13998          template will correspond to a class.  */
13999       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
14000                && tag_type == typename_type)
14001         type = make_typename_type (parser->scope, decl,
14002                                    typename_type,
14003                                    /*complain=*/tf_error);
14004       /* If the `typename' keyword is in effect and DECL is not a type
14005          decl. Then type is non existant.   */
14006       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
14007         type = NULL_TREE; 
14008       else 
14009         type = check_elaborated_type_specifier (tag_type, decl,
14010                                                 /*allow_template_p=*/true);
14011     }
14012
14013   if (!type)
14014     {
14015       token = cp_lexer_peek_token (parser->lexer);
14016       identifier = cp_parser_identifier (parser);
14017
14018       if (identifier == error_mark_node)
14019         {
14020           parser->scope = NULL_TREE;
14021           return error_mark_node;
14022         }
14023
14024       /* For a `typename', we needn't call xref_tag.  */
14025       if (tag_type == typename_type
14026           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
14027         return cp_parser_make_typename_type (parser, parser->scope,
14028                                              identifier,
14029                                              token->location);
14030       /* Look up a qualified name in the usual way.  */
14031       if (parser->scope)
14032         {
14033           tree decl;
14034           tree ambiguous_decls;
14035
14036           decl = cp_parser_lookup_name (parser, identifier,
14037                                         tag_type,
14038                                         /*is_template=*/false,
14039                                         /*is_namespace=*/false,
14040                                         /*check_dependency=*/true,
14041                                         &ambiguous_decls,
14042                                         token->location);
14043
14044           /* If the lookup was ambiguous, an error will already have been
14045              issued.  */
14046           if (ambiguous_decls)
14047             return error_mark_node;
14048
14049           /* If we are parsing friend declaration, DECL may be a
14050              TEMPLATE_DECL tree node here.  However, we need to check
14051              whether this TEMPLATE_DECL results in valid code.  Consider
14052              the following example:
14053
14054                namespace N {
14055                  template <class T> class C {};
14056                }
14057                class X {
14058                  template <class T> friend class N::C; // #1, valid code
14059                };
14060                template <class T> class Y {
14061                  friend class N::C;                    // #2, invalid code
14062                };
14063
14064              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
14065              name lookup of `N::C'.  We see that friend declaration must
14066              be template for the code to be valid.  Note that
14067              processing_template_decl does not work here since it is
14068              always 1 for the above two cases.  */
14069
14070           decl = (cp_parser_maybe_treat_template_as_class
14071                   (decl, /*tag_name_p=*/is_friend
14072                          && parser->num_template_parameter_lists));
14073
14074           if (TREE_CODE (decl) != TYPE_DECL)
14075             {
14076               cp_parser_diagnose_invalid_type_name (parser,
14077                                                     parser->scope,
14078                                                     identifier,
14079                                                     token->location);
14080               return error_mark_node;
14081             }
14082
14083           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14084             {
14085               bool allow_template = (parser->num_template_parameter_lists
14086                                       || DECL_SELF_REFERENCE_P (decl));
14087               type = check_elaborated_type_specifier (tag_type, decl, 
14088                                                       allow_template);
14089
14090               if (type == error_mark_node)
14091                 return error_mark_node;
14092             }
14093
14094           /* Forward declarations of nested types, such as
14095
14096                class C1::C2;
14097                class C1::C2::C3;
14098
14099              are invalid unless all components preceding the final '::'
14100              are complete.  If all enclosing types are complete, these
14101              declarations become merely pointless.
14102
14103              Invalid forward declarations of nested types are errors
14104              caught elsewhere in parsing.  Those that are pointless arrive
14105              here.  */
14106
14107           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14108               && !is_friend && !processing_explicit_instantiation)
14109             warning (0, "declaration %qD does not declare anything", decl);
14110
14111           type = TREE_TYPE (decl);
14112         }
14113       else
14114         {
14115           /* An elaborated-type-specifier sometimes introduces a new type and
14116              sometimes names an existing type.  Normally, the rule is that it
14117              introduces a new type only if there is not an existing type of
14118              the same name already in scope.  For example, given:
14119
14120                struct S {};
14121                void f() { struct S s; }
14122
14123              the `struct S' in the body of `f' is the same `struct S' as in
14124              the global scope; the existing definition is used.  However, if
14125              there were no global declaration, this would introduce a new
14126              local class named `S'.
14127
14128              An exception to this rule applies to the following code:
14129
14130                namespace N { struct S; }
14131
14132              Here, the elaborated-type-specifier names a new type
14133              unconditionally; even if there is already an `S' in the
14134              containing scope this declaration names a new type.
14135              This exception only applies if the elaborated-type-specifier
14136              forms the complete declaration:
14137
14138                [class.name]
14139
14140                A declaration consisting solely of `class-key identifier ;' is
14141                either a redeclaration of the name in the current scope or a
14142                forward declaration of the identifier as a class name.  It
14143                introduces the name into the current scope.
14144
14145              We are in this situation precisely when the next token is a `;'.
14146
14147              An exception to the exception is that a `friend' declaration does
14148              *not* name a new type; i.e., given:
14149
14150                struct S { friend struct T; };
14151
14152              `T' is not a new type in the scope of `S'.
14153
14154              Also, `new struct S' or `sizeof (struct S)' never results in the
14155              definition of a new type; a new type can only be declared in a
14156              declaration context.  */
14157
14158           tag_scope ts;
14159           bool template_p;
14160
14161           if (is_friend)
14162             /* Friends have special name lookup rules.  */
14163             ts = ts_within_enclosing_non_class;
14164           else if (is_declaration
14165                    && cp_lexer_next_token_is (parser->lexer,
14166                                               CPP_SEMICOLON))
14167             /* This is a `class-key identifier ;' */
14168             ts = ts_current;
14169           else
14170             ts = ts_global;
14171
14172           template_p =
14173             (parser->num_template_parameter_lists
14174              && (cp_parser_next_token_starts_class_definition_p (parser)
14175                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14176           /* An unqualified name was used to reference this type, so
14177              there were no qualifying templates.  */
14178           if (!cp_parser_check_template_parameters (parser,
14179                                                     /*num_templates=*/0,
14180                                                     token->location,
14181                                                     /*declarator=*/NULL))
14182             return error_mark_node;
14183           type = xref_tag (tag_type, identifier, ts, template_p);
14184         }
14185     }
14186
14187   if (type == error_mark_node)
14188     return error_mark_node;
14189
14190   /* Allow attributes on forward declarations of classes.  */
14191   if (attributes)
14192     {
14193       if (TREE_CODE (type) == TYPENAME_TYPE)
14194         warning (OPT_Wattributes,
14195                  "attributes ignored on uninstantiated type");
14196       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14197                && ! processing_explicit_instantiation)
14198         warning (OPT_Wattributes,
14199                  "attributes ignored on template instantiation");
14200       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14201         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14202       else
14203         warning (OPT_Wattributes,
14204                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14205     }
14206
14207   if (tag_type != enum_type)
14208     {
14209       /* Indicate whether this class was declared as a `class' or as a
14210          `struct'.  */
14211       if (TREE_CODE (type) == RECORD_TYPE)
14212         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14213       cp_parser_check_class_key (tag_type, type);
14214     }
14215
14216   /* A "<" cannot follow an elaborated type specifier.  If that
14217      happens, the user was probably trying to form a template-id.  */
14218   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14219
14220   return type;
14221 }
14222
14223 /* Parse an enum-specifier.
14224
14225    enum-specifier:
14226      enum-head { enumerator-list [opt] }
14227      enum-head { enumerator-list , } [C++0x]
14228
14229    enum-head:
14230      enum-key identifier [opt] enum-base [opt]
14231      enum-key nested-name-specifier identifier enum-base [opt]
14232
14233    enum-key:
14234      enum
14235      enum class   [C++0x]
14236      enum struct  [C++0x]
14237
14238    enum-base:   [C++0x]
14239      : type-specifier-seq
14240
14241    opaque-enum-specifier:
14242      enum-key identifier enum-base [opt] ;
14243
14244    GNU Extensions:
14245      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14246        { enumerator-list [opt] }attributes[opt]
14247      enum-key attributes[opt] identifier [opt] enum-base [opt]
14248        { enumerator-list, }attributes[opt] [C++0x]
14249
14250    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14251    if the token stream isn't an enum-specifier after all.  */
14252
14253 static tree
14254 cp_parser_enum_specifier (cp_parser* parser)
14255 {
14256   tree identifier;
14257   tree type = NULL_TREE;
14258   tree prev_scope;
14259   tree nested_name_specifier = NULL_TREE;
14260   tree attributes;
14261   bool scoped_enum_p = false;
14262   bool has_underlying_type = false;
14263   bool nested_being_defined = false;
14264   bool new_value_list = false;
14265   bool is_new_type = false;
14266   bool is_anonymous = false;
14267   tree underlying_type = NULL_TREE;
14268   cp_token *type_start_token = NULL;
14269   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14270
14271   parser->colon_corrects_to_scope_p = false;
14272
14273   /* Parse tentatively so that we can back up if we don't find a
14274      enum-specifier.  */
14275   cp_parser_parse_tentatively (parser);
14276
14277   /* Caller guarantees that the current token is 'enum', an identifier
14278      possibly follows, and the token after that is an opening brace.
14279      If we don't have an identifier, fabricate an anonymous name for
14280      the enumeration being defined.  */
14281   cp_lexer_consume_token (parser->lexer);
14282
14283   /* Parse the "class" or "struct", which indicates a scoped
14284      enumeration type in C++0x.  */
14285   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14286       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14287     {
14288       if (cxx_dialect < cxx0x)
14289         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14290
14291       /* Consume the `struct' or `class' token.  */
14292       cp_lexer_consume_token (parser->lexer);
14293
14294       scoped_enum_p = true;
14295     }
14296
14297   attributes = cp_parser_attributes_opt (parser);
14298
14299   /* Clear the qualification.  */
14300   parser->scope = NULL_TREE;
14301   parser->qualifying_scope = NULL_TREE;
14302   parser->object_scope = NULL_TREE;
14303
14304   /* Figure out in what scope the declaration is being placed.  */
14305   prev_scope = current_scope ();
14306
14307   type_start_token = cp_lexer_peek_token (parser->lexer);
14308
14309   push_deferring_access_checks (dk_no_check);
14310   nested_name_specifier
14311       = cp_parser_nested_name_specifier_opt (parser,
14312                                              /*typename_keyword_p=*/true,
14313                                              /*check_dependency_p=*/false,
14314                                              /*type_p=*/false,
14315                                              /*is_declaration=*/false);
14316
14317   if (nested_name_specifier)
14318     {
14319       tree name;
14320
14321       identifier = cp_parser_identifier (parser);
14322       name =  cp_parser_lookup_name (parser, identifier,
14323                                      enum_type,
14324                                      /*is_template=*/false,
14325                                      /*is_namespace=*/false,
14326                                      /*check_dependency=*/true,
14327                                      /*ambiguous_decls=*/NULL,
14328                                      input_location);
14329       if (name)
14330         {
14331           type = TREE_TYPE (name);
14332           if (TREE_CODE (type) == TYPENAME_TYPE)
14333             {
14334               /* Are template enums allowed in ISO? */
14335               if (template_parm_scope_p ())
14336                 pedwarn (type_start_token->location, OPT_pedantic,
14337                          "%qD is an enumeration template", name);
14338               /* ignore a typename reference, for it will be solved by name
14339                  in start_enum.  */
14340               type = NULL_TREE;
14341             }
14342         }
14343       else
14344         error_at (type_start_token->location,
14345                   "%qD is not an enumerator-name", identifier);
14346     }
14347   else
14348     {
14349       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14350         identifier = cp_parser_identifier (parser);
14351       else
14352         {
14353           identifier = make_anon_name ();
14354           is_anonymous = true;
14355         }
14356     }
14357   pop_deferring_access_checks ();
14358
14359   /* Check for the `:' that denotes a specified underlying type in C++0x.
14360      Note that a ':' could also indicate a bitfield width, however.  */
14361   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14362     {
14363       cp_decl_specifier_seq type_specifiers;
14364
14365       /* Consume the `:'.  */
14366       cp_lexer_consume_token (parser->lexer);
14367
14368       /* Parse the type-specifier-seq.  */
14369       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14370                                     /*is_trailing_return=*/false,
14371                                     &type_specifiers);
14372
14373       /* At this point this is surely not elaborated type specifier.  */
14374       if (!cp_parser_parse_definitely (parser))
14375         return NULL_TREE;
14376
14377       if (cxx_dialect < cxx0x)
14378         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14379
14380       has_underlying_type = true;
14381
14382       /* If that didn't work, stop.  */
14383       if (type_specifiers.type != error_mark_node)
14384         {
14385           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14386                                             /*initialized=*/0, NULL);
14387           if (underlying_type == error_mark_node)
14388             underlying_type = NULL_TREE;
14389         }
14390     }
14391
14392   /* Look for the `{' but don't consume it yet.  */
14393   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14394     {
14395       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14396         {
14397           cp_parser_error (parser, "expected %<{%>");
14398           if (has_underlying_type)
14399             {
14400               type = NULL_TREE;
14401               goto out;
14402             }
14403         }
14404       /* An opaque-enum-specifier must have a ';' here.  */
14405       if ((scoped_enum_p || underlying_type)
14406           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14407         {
14408           cp_parser_error (parser, "expected %<;%> or %<{%>");
14409           if (has_underlying_type)
14410             {
14411               type = NULL_TREE;
14412               goto out;
14413             }
14414         }
14415     }
14416
14417   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14418     return NULL_TREE;
14419
14420   if (nested_name_specifier)
14421     {
14422       if (CLASS_TYPE_P (nested_name_specifier))
14423         {
14424           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14425           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14426           push_scope (nested_name_specifier);
14427         }
14428       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14429         {
14430           push_nested_namespace (nested_name_specifier);
14431         }
14432     }
14433
14434   /* Issue an error message if type-definitions are forbidden here.  */
14435   if (!cp_parser_check_type_definition (parser))
14436     type = error_mark_node;
14437   else
14438     /* Create the new type.  We do this before consuming the opening
14439        brace so the enum will be recorded as being on the line of its
14440        tag (or the 'enum' keyword, if there is no tag).  */
14441     type = start_enum (identifier, type, underlying_type,
14442                        scoped_enum_p, &is_new_type);
14443
14444   /* If the next token is not '{' it is an opaque-enum-specifier or an
14445      elaborated-type-specifier.  */
14446   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14447     {
14448       timevar_push (TV_PARSE_ENUM);
14449       if (nested_name_specifier)
14450         {
14451           /* The following catches invalid code such as:
14452              enum class S<int>::E { A, B, C }; */
14453           if (!processing_specialization
14454               && CLASS_TYPE_P (nested_name_specifier)
14455               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14456             error_at (type_start_token->location, "cannot add an enumerator "
14457                       "list to a template instantiation");
14458
14459           /* If that scope does not contain the scope in which the
14460              class was originally declared, the program is invalid.  */
14461           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14462             {
14463               if (at_namespace_scope_p ())
14464                 error_at (type_start_token->location,
14465                           "declaration of %qD in namespace %qD which does not "
14466                           "enclose %qD",
14467                           type, prev_scope, nested_name_specifier);
14468               else
14469                 error_at (type_start_token->location,
14470                           "declaration of %qD in %qD which does not enclose %qD",
14471                           type, prev_scope, nested_name_specifier);
14472               type = error_mark_node;
14473             }
14474         }
14475
14476       if (scoped_enum_p)
14477         begin_scope (sk_scoped_enum, type);
14478
14479       /* Consume the opening brace.  */
14480       cp_lexer_consume_token (parser->lexer);
14481
14482       if (type == error_mark_node)
14483         ; /* Nothing to add */
14484       else if (OPAQUE_ENUM_P (type)
14485                || (cxx_dialect > cxx98 && processing_specialization))
14486         {
14487           new_value_list = true;
14488           SET_OPAQUE_ENUM_P (type, false);
14489           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14490         }
14491       else
14492         {
14493           error_at (type_start_token->location, "multiple definition of %q#T", type);
14494           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14495                     "previous definition here");
14496           type = error_mark_node;
14497         }
14498
14499       if (type == error_mark_node)
14500         cp_parser_skip_to_end_of_block_or_statement (parser);
14501       /* If the next token is not '}', then there are some enumerators.  */
14502       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14503         cp_parser_enumerator_list (parser, type);
14504
14505       /* Consume the final '}'.  */
14506       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14507
14508       if (scoped_enum_p)
14509         finish_scope ();
14510       timevar_pop (TV_PARSE_ENUM);
14511     }
14512   else
14513     {
14514       /* If a ';' follows, then it is an opaque-enum-specifier
14515         and additional restrictions apply.  */
14516       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14517         {
14518           if (is_anonymous)
14519             error_at (type_start_token->location,
14520                       "opaque-enum-specifier without name");
14521           else if (nested_name_specifier)
14522             error_at (type_start_token->location,
14523                       "opaque-enum-specifier must use a simple identifier");
14524         }
14525     }
14526
14527   /* Look for trailing attributes to apply to this enumeration, and
14528      apply them if appropriate.  */
14529   if (cp_parser_allow_gnu_extensions_p (parser))
14530     {
14531       tree trailing_attr = cp_parser_attributes_opt (parser);
14532       trailing_attr = chainon (trailing_attr, attributes);
14533       cplus_decl_attributes (&type,
14534                              trailing_attr,
14535                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14536     }
14537
14538   /* Finish up the enumeration.  */
14539   if (type != error_mark_node)
14540     {
14541       if (new_value_list)
14542         finish_enum_value_list (type);
14543       if (is_new_type)
14544         finish_enum (type);
14545     }
14546
14547   if (nested_name_specifier)
14548     {
14549       if (CLASS_TYPE_P (nested_name_specifier))
14550         {
14551           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14552           pop_scope (nested_name_specifier);
14553         }
14554       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14555         {
14556           pop_nested_namespace (nested_name_specifier);
14557         }
14558     }
14559  out:
14560   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14561   return type;
14562 }
14563
14564 /* Parse an enumerator-list.  The enumerators all have the indicated
14565    TYPE.
14566
14567    enumerator-list:
14568      enumerator-definition
14569      enumerator-list , enumerator-definition  */
14570
14571 static void
14572 cp_parser_enumerator_list (cp_parser* parser, tree type)
14573 {
14574   while (true)
14575     {
14576       /* Parse an enumerator-definition.  */
14577       cp_parser_enumerator_definition (parser, type);
14578
14579       /* If the next token is not a ',', we've reached the end of
14580          the list.  */
14581       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14582         break;
14583       /* Otherwise, consume the `,' and keep going.  */
14584       cp_lexer_consume_token (parser->lexer);
14585       /* If the next token is a `}', there is a trailing comma.  */
14586       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14587         {
14588           if (cxx_dialect < cxx0x && !in_system_header)
14589             pedwarn (input_location, OPT_pedantic,
14590                      "comma at end of enumerator list");
14591           break;
14592         }
14593     }
14594 }
14595
14596 /* Parse an enumerator-definition.  The enumerator has the indicated
14597    TYPE.
14598
14599    enumerator-definition:
14600      enumerator
14601      enumerator = constant-expression
14602
14603    enumerator:
14604      identifier  */
14605
14606 static void
14607 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14608 {
14609   tree identifier;
14610   tree value;
14611   location_t loc;
14612
14613   /* Save the input location because we are interested in the location
14614      of the identifier and not the location of the explicit value.  */
14615   loc = cp_lexer_peek_token (parser->lexer)->location;
14616
14617   /* Look for the identifier.  */
14618   identifier = cp_parser_identifier (parser);
14619   if (identifier == error_mark_node)
14620     return;
14621
14622   /* If the next token is an '=', then there is an explicit value.  */
14623   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14624     {
14625       /* Consume the `=' token.  */
14626       cp_lexer_consume_token (parser->lexer);
14627       /* Parse the value.  */
14628       value = cp_parser_constant_expression (parser,
14629                                              /*allow_non_constant_p=*/false,
14630                                              NULL);
14631     }
14632   else
14633     value = NULL_TREE;
14634
14635   /* If we are processing a template, make sure the initializer of the
14636      enumerator doesn't contain any bare template parameter pack.  */
14637   if (check_for_bare_parameter_packs (value))
14638     value = error_mark_node;
14639
14640   /* integral_constant_value will pull out this expression, so make sure
14641      it's folded as appropriate.  */
14642   value = fold_non_dependent_expr (value);
14643
14644   /* Create the enumerator.  */
14645   build_enumerator (identifier, value, type, loc);
14646 }
14647
14648 /* Parse a namespace-name.
14649
14650    namespace-name:
14651      original-namespace-name
14652      namespace-alias
14653
14654    Returns the NAMESPACE_DECL for the namespace.  */
14655
14656 static tree
14657 cp_parser_namespace_name (cp_parser* parser)
14658 {
14659   tree identifier;
14660   tree namespace_decl;
14661
14662   cp_token *token = cp_lexer_peek_token (parser->lexer);
14663
14664   /* Get the name of the namespace.  */
14665   identifier = cp_parser_identifier (parser);
14666   if (identifier == error_mark_node)
14667     return error_mark_node;
14668
14669   /* Look up the identifier in the currently active scope.  Look only
14670      for namespaces, due to:
14671
14672        [basic.lookup.udir]
14673
14674        When looking up a namespace-name in a using-directive or alias
14675        definition, only namespace names are considered.
14676
14677      And:
14678
14679        [basic.lookup.qual]
14680
14681        During the lookup of a name preceding the :: scope resolution
14682        operator, object, function, and enumerator names are ignored.
14683
14684      (Note that cp_parser_qualifying_entity only calls this
14685      function if the token after the name is the scope resolution
14686      operator.)  */
14687   namespace_decl = cp_parser_lookup_name (parser, identifier,
14688                                           none_type,
14689                                           /*is_template=*/false,
14690                                           /*is_namespace=*/true,
14691                                           /*check_dependency=*/true,
14692                                           /*ambiguous_decls=*/NULL,
14693                                           token->location);
14694   /* If it's not a namespace, issue an error.  */
14695   if (namespace_decl == error_mark_node
14696       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14697     {
14698       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14699         error_at (token->location, "%qD is not a namespace-name", identifier);
14700       cp_parser_error (parser, "expected namespace-name");
14701       namespace_decl = error_mark_node;
14702     }
14703
14704   return namespace_decl;
14705 }
14706
14707 /* Parse a namespace-definition.
14708
14709    namespace-definition:
14710      named-namespace-definition
14711      unnamed-namespace-definition
14712
14713    named-namespace-definition:
14714      original-namespace-definition
14715      extension-namespace-definition
14716
14717    original-namespace-definition:
14718      namespace identifier { namespace-body }
14719
14720    extension-namespace-definition:
14721      namespace original-namespace-name { namespace-body }
14722
14723    unnamed-namespace-definition:
14724      namespace { namespace-body } */
14725
14726 static void
14727 cp_parser_namespace_definition (cp_parser* parser)
14728 {
14729   tree identifier, attribs;
14730   bool has_visibility;
14731   bool is_inline;
14732
14733   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14734     {
14735       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14736       is_inline = true;
14737       cp_lexer_consume_token (parser->lexer);
14738     }
14739   else
14740     is_inline = false;
14741
14742   /* Look for the `namespace' keyword.  */
14743   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14744
14745   /* Get the name of the namespace.  We do not attempt to distinguish
14746      between an original-namespace-definition and an
14747      extension-namespace-definition at this point.  The semantic
14748      analysis routines are responsible for that.  */
14749   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14750     identifier = cp_parser_identifier (parser);
14751   else
14752     identifier = NULL_TREE;
14753
14754   /* Parse any specified attributes.  */
14755   attribs = cp_parser_attributes_opt (parser);
14756
14757   /* Look for the `{' to start the namespace.  */
14758   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14759   /* Start the namespace.  */
14760   push_namespace (identifier);
14761
14762   /* "inline namespace" is equivalent to a stub namespace definition
14763      followed by a strong using directive.  */
14764   if (is_inline)
14765     {
14766       tree name_space = current_namespace;
14767       /* Set up namespace association.  */
14768       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14769         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14770                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14771       /* Import the contents of the inline namespace.  */
14772       pop_namespace ();
14773       do_using_directive (name_space);
14774       push_namespace (identifier);
14775     }
14776
14777   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14778
14779   /* Parse the body of the namespace.  */
14780   cp_parser_namespace_body (parser);
14781
14782   if (has_visibility)
14783     pop_visibility (1);
14784
14785   /* Finish the namespace.  */
14786   pop_namespace ();
14787   /* Look for the final `}'.  */
14788   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14789 }
14790
14791 /* Parse a namespace-body.
14792
14793    namespace-body:
14794      declaration-seq [opt]  */
14795
14796 static void
14797 cp_parser_namespace_body (cp_parser* parser)
14798 {
14799   cp_parser_declaration_seq_opt (parser);
14800 }
14801
14802 /* Parse a namespace-alias-definition.
14803
14804    namespace-alias-definition:
14805      namespace identifier = qualified-namespace-specifier ;  */
14806
14807 static void
14808 cp_parser_namespace_alias_definition (cp_parser* parser)
14809 {
14810   tree identifier;
14811   tree namespace_specifier;
14812
14813   cp_token *token = cp_lexer_peek_token (parser->lexer);
14814
14815   /* Look for the `namespace' keyword.  */
14816   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14817   /* Look for the identifier.  */
14818   identifier = cp_parser_identifier (parser);
14819   if (identifier == error_mark_node)
14820     return;
14821   /* Look for the `=' token.  */
14822   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14823       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14824     {
14825       error_at (token->location, "%<namespace%> definition is not allowed here");
14826       /* Skip the definition.  */
14827       cp_lexer_consume_token (parser->lexer);
14828       if (cp_parser_skip_to_closing_brace (parser))
14829         cp_lexer_consume_token (parser->lexer);
14830       return;
14831     }
14832   cp_parser_require (parser, CPP_EQ, RT_EQ);
14833   /* Look for the qualified-namespace-specifier.  */
14834   namespace_specifier
14835     = cp_parser_qualified_namespace_specifier (parser);
14836   /* Look for the `;' token.  */
14837   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14838
14839   /* Register the alias in the symbol table.  */
14840   do_namespace_alias (identifier, namespace_specifier);
14841 }
14842
14843 /* Parse a qualified-namespace-specifier.
14844
14845    qualified-namespace-specifier:
14846      :: [opt] nested-name-specifier [opt] namespace-name
14847
14848    Returns a NAMESPACE_DECL corresponding to the specified
14849    namespace.  */
14850
14851 static tree
14852 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14853 {
14854   /* Look for the optional `::'.  */
14855   cp_parser_global_scope_opt (parser,
14856                               /*current_scope_valid_p=*/false);
14857
14858   /* Look for the optional nested-name-specifier.  */
14859   cp_parser_nested_name_specifier_opt (parser,
14860                                        /*typename_keyword_p=*/false,
14861                                        /*check_dependency_p=*/true,
14862                                        /*type_p=*/false,
14863                                        /*is_declaration=*/true);
14864
14865   return cp_parser_namespace_name (parser);
14866 }
14867
14868 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14869    access declaration.
14870
14871    using-declaration:
14872      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14873      using :: unqualified-id ;  
14874
14875    access-declaration:
14876      qualified-id ;  
14877
14878    */
14879
14880 static bool
14881 cp_parser_using_declaration (cp_parser* parser, 
14882                              bool access_declaration_p)
14883 {
14884   cp_token *token;
14885   bool typename_p = false;
14886   bool global_scope_p;
14887   tree decl;
14888   tree identifier;
14889   tree qscope;
14890
14891   if (access_declaration_p)
14892     cp_parser_parse_tentatively (parser);
14893   else
14894     {
14895       /* Look for the `using' keyword.  */
14896       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14897       
14898       /* Peek at the next token.  */
14899       token = cp_lexer_peek_token (parser->lexer);
14900       /* See if it's `typename'.  */
14901       if (token->keyword == RID_TYPENAME)
14902         {
14903           /* Remember that we've seen it.  */
14904           typename_p = true;
14905           /* Consume the `typename' token.  */
14906           cp_lexer_consume_token (parser->lexer);
14907         }
14908     }
14909
14910   /* Look for the optional global scope qualification.  */
14911   global_scope_p
14912     = (cp_parser_global_scope_opt (parser,
14913                                    /*current_scope_valid_p=*/false)
14914        != NULL_TREE);
14915
14916   /* If we saw `typename', or didn't see `::', then there must be a
14917      nested-name-specifier present.  */
14918   if (typename_p || !global_scope_p)
14919     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14920                                               /*check_dependency_p=*/true,
14921                                               /*type_p=*/false,
14922                                               /*is_declaration=*/true);
14923   /* Otherwise, we could be in either of the two productions.  In that
14924      case, treat the nested-name-specifier as optional.  */
14925   else
14926     qscope = cp_parser_nested_name_specifier_opt (parser,
14927                                                   /*typename_keyword_p=*/false,
14928                                                   /*check_dependency_p=*/true,
14929                                                   /*type_p=*/false,
14930                                                   /*is_declaration=*/true);
14931   if (!qscope)
14932     qscope = global_namespace;
14933
14934   if (access_declaration_p && cp_parser_error_occurred (parser))
14935     /* Something has already gone wrong; there's no need to parse
14936        further.  Since an error has occurred, the return value of
14937        cp_parser_parse_definitely will be false, as required.  */
14938     return cp_parser_parse_definitely (parser);
14939
14940   token = cp_lexer_peek_token (parser->lexer);
14941   /* Parse the unqualified-id.  */
14942   identifier = cp_parser_unqualified_id (parser,
14943                                          /*template_keyword_p=*/false,
14944                                          /*check_dependency_p=*/true,
14945                                          /*declarator_p=*/true,
14946                                          /*optional_p=*/false);
14947
14948   if (access_declaration_p)
14949     {
14950       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14951         cp_parser_simulate_error (parser);
14952       if (!cp_parser_parse_definitely (parser))
14953         return false;
14954     }
14955
14956   /* The function we call to handle a using-declaration is different
14957      depending on what scope we are in.  */
14958   if (qscope == error_mark_node || identifier == error_mark_node)
14959     ;
14960   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14961            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14962     /* [namespace.udecl]
14963
14964        A using declaration shall not name a template-id.  */
14965     error_at (token->location,
14966               "a template-id may not appear in a using-declaration");
14967   else
14968     {
14969       if (at_class_scope_p ())
14970         {
14971           /* Create the USING_DECL.  */
14972           decl = do_class_using_decl (parser->scope, identifier);
14973
14974           if (decl && typename_p)
14975             USING_DECL_TYPENAME_P (decl) = 1;
14976
14977           if (check_for_bare_parameter_packs (decl))
14978             return false;
14979           else
14980             /* Add it to the list of members in this class.  */
14981             finish_member_declaration (decl);
14982         }
14983       else
14984         {
14985           decl = cp_parser_lookup_name_simple (parser,
14986                                                identifier,
14987                                                token->location);
14988           if (decl == error_mark_node)
14989             cp_parser_name_lookup_error (parser, identifier,
14990                                          decl, NLE_NULL,
14991                                          token->location);
14992           else if (check_for_bare_parameter_packs (decl))
14993             return false;
14994           else if (!at_namespace_scope_p ())
14995             do_local_using_decl (decl, qscope, identifier);
14996           else
14997             do_toplevel_using_decl (decl, qscope, identifier);
14998         }
14999     }
15000
15001   /* Look for the final `;'.  */
15002   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15003   
15004   return true;
15005 }
15006
15007 /* Parse an alias-declaration.
15008
15009    alias-declaration:
15010      using identifier attribute-specifier-seq [opt] = type-id  */
15011
15012 static tree
15013 cp_parser_alias_declaration (cp_parser* parser)
15014 {
15015   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
15016   location_t id_location;
15017   cp_declarator *declarator;
15018   cp_decl_specifier_seq decl_specs;
15019   bool member_p;
15020   const char *saved_message = NULL;
15021
15022   /* Look for the `using' keyword.  */
15023   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15024   id_location = cp_lexer_peek_token (parser->lexer)->location;
15025   id = cp_parser_identifier (parser);
15026   attributes = cp_parser_attributes_opt (parser);
15027   cp_parser_require (parser, CPP_EQ, RT_EQ);
15028
15029   /* Now we are going to parse the type-id of the declaration.  */
15030
15031   /*
15032     [dcl.type]/3 says:
15033
15034         "A type-specifier-seq shall not define a class or enumeration
15035          unless it appears in the type-id of an alias-declaration (7.1.3) that
15036          is not the declaration of a template-declaration."
15037
15038     In other words, if we currently are in an alias template, the
15039     type-id should not define a type.
15040
15041     So let's set parser->type_definition_forbidden_message in that
15042     case; cp_parser_check_type_definition (called by
15043     cp_parser_class_specifier) will then emit an error if a type is
15044     defined in the type-id.  */
15045   if (parser->num_template_parameter_lists)
15046     {
15047       saved_message = parser->type_definition_forbidden_message;
15048       parser->type_definition_forbidden_message =
15049         G_("types may not be defined in alias template declarations");
15050     }
15051
15052   type = cp_parser_type_id (parser);
15053
15054   /* Restore the error message if need be.  */
15055   if (parser->num_template_parameter_lists)
15056     parser->type_definition_forbidden_message = saved_message;
15057
15058   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15059
15060   if (cp_parser_error_occurred (parser))
15061     return error_mark_node;
15062
15063   /* A typedef-name can also be introduced by an alias-declaration. The
15064      identifier following the using keyword becomes a typedef-name. It has
15065      the same semantics as if it were introduced by the typedef
15066      specifier. In particular, it does not define a new type and it shall
15067      not appear in the type-id.  */
15068
15069   clear_decl_specs (&decl_specs);
15070   decl_specs.type = type;
15071   decl_specs.attributes = attributes;
15072   ++decl_specs.specs[(int) ds_typedef];
15073   ++decl_specs.specs[(int) ds_alias];
15074
15075   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
15076   declarator->id_loc = id_location;
15077
15078   member_p = at_class_scope_p ();
15079   if (member_p)
15080     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
15081                       NULL_TREE, attributes);
15082   else
15083     decl = start_decl (declarator, &decl_specs, 0,
15084                        attributes, NULL_TREE, &pushed_scope);
15085   if (decl == error_mark_node)
15086     return decl;
15087
15088   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
15089
15090   if (pushed_scope)
15091     pop_scope (pushed_scope);
15092
15093   /* If decl is a template, return its TEMPLATE_DECL so that it gets
15094      added into the symbol table; otherwise, return the TYPE_DECL.  */
15095   if (DECL_LANG_SPECIFIC (decl)
15096       && DECL_TEMPLATE_INFO (decl)
15097       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
15098     {
15099       decl = DECL_TI_TEMPLATE (decl);
15100       if (member_p)
15101         check_member_template (decl);
15102     }
15103
15104   return decl;
15105 }
15106
15107 /* Parse a using-directive.
15108
15109    using-directive:
15110      using namespace :: [opt] nested-name-specifier [opt]
15111        namespace-name ;  */
15112
15113 static void
15114 cp_parser_using_directive (cp_parser* parser)
15115 {
15116   tree namespace_decl;
15117   tree attribs;
15118
15119   /* Look for the `using' keyword.  */
15120   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15121   /* And the `namespace' keyword.  */
15122   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15123   /* Look for the optional `::' operator.  */
15124   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15125   /* And the optional nested-name-specifier.  */
15126   cp_parser_nested_name_specifier_opt (parser,
15127                                        /*typename_keyword_p=*/false,
15128                                        /*check_dependency_p=*/true,
15129                                        /*type_p=*/false,
15130                                        /*is_declaration=*/true);
15131   /* Get the namespace being used.  */
15132   namespace_decl = cp_parser_namespace_name (parser);
15133   /* And any specified attributes.  */
15134   attribs = cp_parser_attributes_opt (parser);
15135   /* Update the symbol table.  */
15136   parse_using_directive (namespace_decl, attribs);
15137   /* Look for the final `;'.  */
15138   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15139 }
15140
15141 /* Parse an asm-definition.
15142
15143    asm-definition:
15144      asm ( string-literal ) ;
15145
15146    GNU Extension:
15147
15148    asm-definition:
15149      asm volatile [opt] ( string-literal ) ;
15150      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15151      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15152                           : asm-operand-list [opt] ) ;
15153      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15154                           : asm-operand-list [opt]
15155                           : asm-clobber-list [opt] ) ;
15156      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15157                                : asm-clobber-list [opt]
15158                                : asm-goto-list ) ;  */
15159
15160 static void
15161 cp_parser_asm_definition (cp_parser* parser)
15162 {
15163   tree string;
15164   tree outputs = NULL_TREE;
15165   tree inputs = NULL_TREE;
15166   tree clobbers = NULL_TREE;
15167   tree labels = NULL_TREE;
15168   tree asm_stmt;
15169   bool volatile_p = false;
15170   bool extended_p = false;
15171   bool invalid_inputs_p = false;
15172   bool invalid_outputs_p = false;
15173   bool goto_p = false;
15174   required_token missing = RT_NONE;
15175
15176   /* Look for the `asm' keyword.  */
15177   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15178   /* See if the next token is `volatile'.  */
15179   if (cp_parser_allow_gnu_extensions_p (parser)
15180       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15181     {
15182       /* Remember that we saw the `volatile' keyword.  */
15183       volatile_p = true;
15184       /* Consume the token.  */
15185       cp_lexer_consume_token (parser->lexer);
15186     }
15187   if (cp_parser_allow_gnu_extensions_p (parser)
15188       && parser->in_function_body
15189       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15190     {
15191       /* Remember that we saw the `goto' keyword.  */
15192       goto_p = true;
15193       /* Consume the token.  */
15194       cp_lexer_consume_token (parser->lexer);
15195     }
15196   /* Look for the opening `('.  */
15197   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15198     return;
15199   /* Look for the string.  */
15200   string = cp_parser_string_literal (parser, false, false);
15201   if (string == error_mark_node)
15202     {
15203       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15204                                              /*consume_paren=*/true);
15205       return;
15206     }
15207
15208   /* If we're allowing GNU extensions, check for the extended assembly
15209      syntax.  Unfortunately, the `:' tokens need not be separated by
15210      a space in C, and so, for compatibility, we tolerate that here
15211      too.  Doing that means that we have to treat the `::' operator as
15212      two `:' tokens.  */
15213   if (cp_parser_allow_gnu_extensions_p (parser)
15214       && parser->in_function_body
15215       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15216           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15217     {
15218       bool inputs_p = false;
15219       bool clobbers_p = false;
15220       bool labels_p = false;
15221
15222       /* The extended syntax was used.  */
15223       extended_p = true;
15224
15225       /* Look for outputs.  */
15226       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15227         {
15228           /* Consume the `:'.  */
15229           cp_lexer_consume_token (parser->lexer);
15230           /* Parse the output-operands.  */
15231           if (cp_lexer_next_token_is_not (parser->lexer,
15232                                           CPP_COLON)
15233               && cp_lexer_next_token_is_not (parser->lexer,
15234                                              CPP_SCOPE)
15235               && cp_lexer_next_token_is_not (parser->lexer,
15236                                              CPP_CLOSE_PAREN)
15237               && !goto_p)
15238             outputs = cp_parser_asm_operand_list (parser);
15239
15240             if (outputs == error_mark_node)
15241               invalid_outputs_p = true;
15242         }
15243       /* If the next token is `::', there are no outputs, and the
15244          next token is the beginning of the inputs.  */
15245       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15246         /* The inputs are coming next.  */
15247         inputs_p = true;
15248
15249       /* Look for inputs.  */
15250       if (inputs_p
15251           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15252         {
15253           /* Consume the `:' or `::'.  */
15254           cp_lexer_consume_token (parser->lexer);
15255           /* Parse the output-operands.  */
15256           if (cp_lexer_next_token_is_not (parser->lexer,
15257                                           CPP_COLON)
15258               && cp_lexer_next_token_is_not (parser->lexer,
15259                                              CPP_SCOPE)
15260               && cp_lexer_next_token_is_not (parser->lexer,
15261                                              CPP_CLOSE_PAREN))
15262             inputs = cp_parser_asm_operand_list (parser);
15263
15264             if (inputs == error_mark_node)
15265               invalid_inputs_p = true;
15266         }
15267       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15268         /* The clobbers are coming next.  */
15269         clobbers_p = true;
15270
15271       /* Look for clobbers.  */
15272       if (clobbers_p
15273           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15274         {
15275           clobbers_p = true;
15276           /* Consume the `:' or `::'.  */
15277           cp_lexer_consume_token (parser->lexer);
15278           /* Parse the clobbers.  */
15279           if (cp_lexer_next_token_is_not (parser->lexer,
15280                                           CPP_COLON)
15281               && cp_lexer_next_token_is_not (parser->lexer,
15282                                              CPP_CLOSE_PAREN))
15283             clobbers = cp_parser_asm_clobber_list (parser);
15284         }
15285       else if (goto_p
15286                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15287         /* The labels are coming next.  */
15288         labels_p = true;
15289
15290       /* Look for labels.  */
15291       if (labels_p
15292           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15293         {
15294           labels_p = true;
15295           /* Consume the `:' or `::'.  */
15296           cp_lexer_consume_token (parser->lexer);
15297           /* Parse the labels.  */
15298           labels = cp_parser_asm_label_list (parser);
15299         }
15300
15301       if (goto_p && !labels_p)
15302         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15303     }
15304   else if (goto_p)
15305     missing = RT_COLON_SCOPE;
15306
15307   /* Look for the closing `)'.  */
15308   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15309                           missing ? missing : RT_CLOSE_PAREN))
15310     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15311                                            /*consume_paren=*/true);
15312   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15313
15314   if (!invalid_inputs_p && !invalid_outputs_p)
15315     {
15316       /* Create the ASM_EXPR.  */
15317       if (parser->in_function_body)
15318         {
15319           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15320                                       inputs, clobbers, labels);
15321           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15322           if (!extended_p)
15323             {
15324               tree temp = asm_stmt;
15325               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15326                 temp = TREE_OPERAND (temp, 0);
15327
15328               ASM_INPUT_P (temp) = 1;
15329             }
15330         }
15331       else
15332         cgraph_add_asm_node (string);
15333     }
15334 }
15335
15336 /* Declarators [gram.dcl.decl] */
15337
15338 /* Parse an init-declarator.
15339
15340    init-declarator:
15341      declarator initializer [opt]
15342
15343    GNU Extension:
15344
15345    init-declarator:
15346      declarator asm-specification [opt] attributes [opt] initializer [opt]
15347
15348    function-definition:
15349      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15350        function-body
15351      decl-specifier-seq [opt] declarator function-try-block
15352
15353    GNU Extension:
15354
15355    function-definition:
15356      __extension__ function-definition
15357
15358    TM Extension:
15359
15360    function-definition:
15361      decl-specifier-seq [opt] declarator function-transaction-block
15362
15363    The DECL_SPECIFIERS apply to this declarator.  Returns a
15364    representation of the entity declared.  If MEMBER_P is TRUE, then
15365    this declarator appears in a class scope.  The new DECL created by
15366    this declarator is returned.
15367
15368    The CHECKS are access checks that should be performed once we know
15369    what entity is being declared (and, therefore, what classes have
15370    befriended it).
15371
15372    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15373    for a function-definition here as well.  If the declarator is a
15374    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15375    be TRUE upon return.  By that point, the function-definition will
15376    have been completely parsed.
15377
15378    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15379    is FALSE.
15380
15381    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15382    parsed declaration if it is an uninitialized single declarator not followed
15383    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15384    if present, will not be consumed.  If returned, this declarator will be
15385    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15386
15387 static tree
15388 cp_parser_init_declarator (cp_parser* parser,
15389                            cp_decl_specifier_seq *decl_specifiers,
15390                            VEC (deferred_access_check,gc)* checks,
15391                            bool function_definition_allowed_p,
15392                            bool member_p,
15393                            int declares_class_or_enum,
15394                            bool* function_definition_p,
15395                            tree* maybe_range_for_decl)
15396 {
15397   cp_token *token = NULL, *asm_spec_start_token = NULL,
15398            *attributes_start_token = NULL;
15399   cp_declarator *declarator;
15400   tree prefix_attributes;
15401   tree attributes;
15402   tree asm_specification;
15403   tree initializer;
15404   tree decl = NULL_TREE;
15405   tree scope;
15406   int is_initialized;
15407   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15408      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15409      "(...)".  */
15410   enum cpp_ttype initialization_kind;
15411   bool is_direct_init = false;
15412   bool is_non_constant_init;
15413   int ctor_dtor_or_conv_p;
15414   bool friend_p;
15415   tree pushed_scope = NULL_TREE;
15416   bool range_for_decl_p = false;
15417
15418   /* Gather the attributes that were provided with the
15419      decl-specifiers.  */
15420   prefix_attributes = decl_specifiers->attributes;
15421
15422   /* Assume that this is not the declarator for a function
15423      definition.  */
15424   if (function_definition_p)
15425     *function_definition_p = false;
15426
15427   /* Defer access checks while parsing the declarator; we cannot know
15428      what names are accessible until we know what is being
15429      declared.  */
15430   resume_deferring_access_checks ();
15431
15432   /* Parse the declarator.  */
15433   token = cp_lexer_peek_token (parser->lexer);
15434   declarator
15435     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15436                             &ctor_dtor_or_conv_p,
15437                             /*parenthesized_p=*/NULL,
15438                             member_p);
15439   /* Gather up the deferred checks.  */
15440   stop_deferring_access_checks ();
15441
15442   /* If the DECLARATOR was erroneous, there's no need to go
15443      further.  */
15444   if (declarator == cp_error_declarator)
15445     return error_mark_node;
15446
15447   /* Check that the number of template-parameter-lists is OK.  */
15448   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15449                                                        token->location))
15450     return error_mark_node;
15451
15452   if (declares_class_or_enum & 2)
15453     cp_parser_check_for_definition_in_return_type (declarator,
15454                                                    decl_specifiers->type,
15455                                                    decl_specifiers->type_location);
15456
15457   /* Figure out what scope the entity declared by the DECLARATOR is
15458      located in.  `grokdeclarator' sometimes changes the scope, so
15459      we compute it now.  */
15460   scope = get_scope_of_declarator (declarator);
15461
15462   /* Perform any lookups in the declared type which were thought to be
15463      dependent, but are not in the scope of the declarator.  */
15464   decl_specifiers->type
15465     = maybe_update_decl_type (decl_specifiers->type, scope);
15466
15467   /* If we're allowing GNU extensions, look for an asm-specification
15468      and attributes.  */
15469   if (cp_parser_allow_gnu_extensions_p (parser))
15470     {
15471       /* Look for an asm-specification.  */
15472       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15473       asm_specification = cp_parser_asm_specification_opt (parser);
15474       /* And attributes.  */
15475       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15476       attributes = cp_parser_attributes_opt (parser);
15477     }
15478   else
15479     {
15480       asm_specification = NULL_TREE;
15481       attributes = NULL_TREE;
15482     }
15483
15484   /* Peek at the next token.  */
15485   token = cp_lexer_peek_token (parser->lexer);
15486   /* Check to see if the token indicates the start of a
15487      function-definition.  */
15488   if (function_declarator_p (declarator)
15489       && cp_parser_token_starts_function_definition_p (token))
15490     {
15491       if (!function_definition_allowed_p)
15492         {
15493           /* If a function-definition should not appear here, issue an
15494              error message.  */
15495           cp_parser_error (parser,
15496                            "a function-definition is not allowed here");
15497           return error_mark_node;
15498         }
15499       else
15500         {
15501           location_t func_brace_location
15502             = cp_lexer_peek_token (parser->lexer)->location;
15503
15504           /* Neither attributes nor an asm-specification are allowed
15505              on a function-definition.  */
15506           if (asm_specification)
15507             error_at (asm_spec_start_token->location,
15508                       "an asm-specification is not allowed "
15509                       "on a function-definition");
15510           if (attributes)
15511             error_at (attributes_start_token->location,
15512                       "attributes are not allowed on a function-definition");
15513           /* This is a function-definition.  */
15514           *function_definition_p = true;
15515
15516           /* Parse the function definition.  */
15517           if (member_p)
15518             decl = cp_parser_save_member_function_body (parser,
15519                                                         decl_specifiers,
15520                                                         declarator,
15521                                                         prefix_attributes);
15522           else
15523             decl
15524               = (cp_parser_function_definition_from_specifiers_and_declarator
15525                  (parser, decl_specifiers, prefix_attributes, declarator));
15526
15527           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15528             {
15529               /* This is where the prologue starts...  */
15530               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15531                 = func_brace_location;
15532             }
15533
15534           return decl;
15535         }
15536     }
15537
15538   /* [dcl.dcl]
15539
15540      Only in function declarations for constructors, destructors, and
15541      type conversions can the decl-specifier-seq be omitted.
15542
15543      We explicitly postpone this check past the point where we handle
15544      function-definitions because we tolerate function-definitions
15545      that are missing their return types in some modes.  */
15546   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15547     {
15548       cp_parser_error (parser,
15549                        "expected constructor, destructor, or type conversion");
15550       return error_mark_node;
15551     }
15552
15553   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15554   if (token->type == CPP_EQ
15555       || token->type == CPP_OPEN_PAREN
15556       || token->type == CPP_OPEN_BRACE)
15557     {
15558       is_initialized = SD_INITIALIZED;
15559       initialization_kind = token->type;
15560       if (maybe_range_for_decl)
15561         *maybe_range_for_decl = error_mark_node;
15562
15563       if (token->type == CPP_EQ
15564           && function_declarator_p (declarator))
15565         {
15566           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15567           if (t2->keyword == RID_DEFAULT)
15568             is_initialized = SD_DEFAULTED;
15569           else if (t2->keyword == RID_DELETE)
15570             is_initialized = SD_DELETED;
15571         }
15572     }
15573   else
15574     {
15575       /* If the init-declarator isn't initialized and isn't followed by a
15576          `,' or `;', it's not a valid init-declarator.  */
15577       if (token->type != CPP_COMMA
15578           && token->type != CPP_SEMICOLON)
15579         {
15580           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15581             range_for_decl_p = true;
15582           else
15583             {
15584               cp_parser_error (parser, "expected initializer");
15585               return error_mark_node;
15586             }
15587         }
15588       is_initialized = SD_UNINITIALIZED;
15589       initialization_kind = CPP_EOF;
15590     }
15591
15592   /* Because start_decl has side-effects, we should only call it if we
15593      know we're going ahead.  By this point, we know that we cannot
15594      possibly be looking at any other construct.  */
15595   cp_parser_commit_to_tentative_parse (parser);
15596
15597   /* If the decl specifiers were bad, issue an error now that we're
15598      sure this was intended to be a declarator.  Then continue
15599      declaring the variable(s), as int, to try to cut down on further
15600      errors.  */
15601   if (decl_specifiers->any_specifiers_p
15602       && decl_specifiers->type == error_mark_node)
15603     {
15604       cp_parser_error (parser, "invalid type in declaration");
15605       decl_specifiers->type = integer_type_node;
15606     }
15607
15608   /* Check to see whether or not this declaration is a friend.  */
15609   friend_p = cp_parser_friend_p (decl_specifiers);
15610
15611   /* Enter the newly declared entry in the symbol table.  If we're
15612      processing a declaration in a class-specifier, we wait until
15613      after processing the initializer.  */
15614   if (!member_p)
15615     {
15616       if (parser->in_unbraced_linkage_specification_p)
15617         decl_specifiers->storage_class = sc_extern;
15618       decl = start_decl (declarator, decl_specifiers,
15619                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15620                          attributes, prefix_attributes,
15621                          &pushed_scope);
15622       /* Adjust location of decl if declarator->id_loc is more appropriate:
15623          set, and decl wasn't merged with another decl, in which case its
15624          location would be different from input_location, and more accurate.  */
15625       if (DECL_P (decl)
15626           && declarator->id_loc != UNKNOWN_LOCATION
15627           && DECL_SOURCE_LOCATION (decl) == input_location)
15628         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15629     }
15630   else if (scope)
15631     /* Enter the SCOPE.  That way unqualified names appearing in the
15632        initializer will be looked up in SCOPE.  */
15633     pushed_scope = push_scope (scope);
15634
15635   /* Perform deferred access control checks, now that we know in which
15636      SCOPE the declared entity resides.  */
15637   if (!member_p && decl)
15638     {
15639       tree saved_current_function_decl = NULL_TREE;
15640
15641       /* If the entity being declared is a function, pretend that we
15642          are in its scope.  If it is a `friend', it may have access to
15643          things that would not otherwise be accessible.  */
15644       if (TREE_CODE (decl) == FUNCTION_DECL)
15645         {
15646           saved_current_function_decl = current_function_decl;
15647           current_function_decl = decl;
15648         }
15649
15650       /* Perform access checks for template parameters.  */
15651       cp_parser_perform_template_parameter_access_checks (checks);
15652
15653       /* Perform the access control checks for the declarator and the
15654          decl-specifiers.  */
15655       perform_deferred_access_checks ();
15656
15657       /* Restore the saved value.  */
15658       if (TREE_CODE (decl) == FUNCTION_DECL)
15659         current_function_decl = saved_current_function_decl;
15660     }
15661
15662   /* Parse the initializer.  */
15663   initializer = NULL_TREE;
15664   is_direct_init = false;
15665   is_non_constant_init = true;
15666   if (is_initialized)
15667     {
15668       if (function_declarator_p (declarator))
15669         {
15670           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15671            if (initialization_kind == CPP_EQ)
15672              initializer = cp_parser_pure_specifier (parser);
15673            else
15674              {
15675                /* If the declaration was erroneous, we don't really
15676                   know what the user intended, so just silently
15677                   consume the initializer.  */
15678                if (decl != error_mark_node)
15679                  error_at (initializer_start_token->location,
15680                            "initializer provided for function");
15681                cp_parser_skip_to_closing_parenthesis (parser,
15682                                                       /*recovering=*/true,
15683                                                       /*or_comma=*/false,
15684                                                       /*consume_paren=*/true);
15685              }
15686         }
15687       else
15688         {
15689           /* We want to record the extra mangling scope for in-class
15690              initializers of class members and initializers of static data
15691              member templates.  The former is a C++0x feature which isn't
15692              implemented yet, and I expect it will involve deferring
15693              parsing of the initializer until end of class as with default
15694              arguments.  So right here we only handle the latter.  */
15695           if (!member_p && processing_template_decl)
15696             start_lambda_scope (decl);
15697           initializer = cp_parser_initializer (parser,
15698                                                &is_direct_init,
15699                                                &is_non_constant_init);
15700           if (!member_p && processing_template_decl)
15701             finish_lambda_scope ();
15702         }
15703     }
15704
15705   /* The old parser allows attributes to appear after a parenthesized
15706      initializer.  Mark Mitchell proposed removing this functionality
15707      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15708      attributes -- but ignores them.  */
15709   if (cp_parser_allow_gnu_extensions_p (parser)
15710       && initialization_kind == CPP_OPEN_PAREN)
15711     if (cp_parser_attributes_opt (parser))
15712       warning (OPT_Wattributes,
15713                "attributes after parenthesized initializer ignored");
15714
15715   /* For an in-class declaration, use `grokfield' to create the
15716      declaration.  */
15717   if (member_p)
15718     {
15719       if (pushed_scope)
15720         {
15721           pop_scope (pushed_scope);
15722           pushed_scope = NULL_TREE;
15723         }
15724       decl = grokfield (declarator, decl_specifiers,
15725                         initializer, !is_non_constant_init,
15726                         /*asmspec=*/NULL_TREE,
15727                         prefix_attributes);
15728       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15729         cp_parser_save_default_args (parser, decl);
15730     }
15731
15732   /* Finish processing the declaration.  But, skip member
15733      declarations.  */
15734   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15735     {
15736       cp_finish_decl (decl,
15737                       initializer, !is_non_constant_init,
15738                       asm_specification,
15739                       /* If the initializer is in parentheses, then this is
15740                          a direct-initialization, which means that an
15741                          `explicit' constructor is OK.  Otherwise, an
15742                          `explicit' constructor cannot be used.  */
15743                       ((is_direct_init || !is_initialized)
15744                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15745     }
15746   else if ((cxx_dialect != cxx98) && friend_p
15747            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15748     /* Core issue #226 (C++0x only): A default template-argument
15749        shall not be specified in a friend class template
15750        declaration. */
15751     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15752                              /*is_partial=*/0, /*is_friend_decl=*/1);
15753
15754   if (!friend_p && pushed_scope)
15755     pop_scope (pushed_scope);
15756
15757   return decl;
15758 }
15759
15760 /* Parse a declarator.
15761
15762    declarator:
15763      direct-declarator
15764      ptr-operator declarator
15765
15766    abstract-declarator:
15767      ptr-operator abstract-declarator [opt]
15768      direct-abstract-declarator
15769
15770    GNU Extensions:
15771
15772    declarator:
15773      attributes [opt] direct-declarator
15774      attributes [opt] ptr-operator declarator
15775
15776    abstract-declarator:
15777      attributes [opt] ptr-operator abstract-declarator [opt]
15778      attributes [opt] direct-abstract-declarator
15779
15780    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15781    detect constructor, destructor or conversion operators. It is set
15782    to -1 if the declarator is a name, and +1 if it is a
15783    function. Otherwise it is set to zero. Usually you just want to
15784    test for >0, but internally the negative value is used.
15785
15786    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15787    a decl-specifier-seq unless it declares a constructor, destructor,
15788    or conversion.  It might seem that we could check this condition in
15789    semantic analysis, rather than parsing, but that makes it difficult
15790    to handle something like `f()'.  We want to notice that there are
15791    no decl-specifiers, and therefore realize that this is an
15792    expression, not a declaration.)
15793
15794    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15795    the declarator is a direct-declarator of the form "(...)".
15796
15797    MEMBER_P is true iff this declarator is a member-declarator.  */
15798
15799 static cp_declarator *
15800 cp_parser_declarator (cp_parser* parser,
15801                       cp_parser_declarator_kind dcl_kind,
15802                       int* ctor_dtor_or_conv_p,
15803                       bool* parenthesized_p,
15804                       bool member_p)
15805 {
15806   cp_declarator *declarator;
15807   enum tree_code code;
15808   cp_cv_quals cv_quals;
15809   tree class_type;
15810   tree attributes = NULL_TREE;
15811
15812   /* Assume this is not a constructor, destructor, or type-conversion
15813      operator.  */
15814   if (ctor_dtor_or_conv_p)
15815     *ctor_dtor_or_conv_p = 0;
15816
15817   if (cp_parser_allow_gnu_extensions_p (parser))
15818     attributes = cp_parser_attributes_opt (parser);
15819
15820   /* Check for the ptr-operator production.  */
15821   cp_parser_parse_tentatively (parser);
15822   /* Parse the ptr-operator.  */
15823   code = cp_parser_ptr_operator (parser,
15824                                  &class_type,
15825                                  &cv_quals);
15826   /* If that worked, then we have a ptr-operator.  */
15827   if (cp_parser_parse_definitely (parser))
15828     {
15829       /* If a ptr-operator was found, then this declarator was not
15830          parenthesized.  */
15831       if (parenthesized_p)
15832         *parenthesized_p = true;
15833       /* The dependent declarator is optional if we are parsing an
15834          abstract-declarator.  */
15835       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15836         cp_parser_parse_tentatively (parser);
15837
15838       /* Parse the dependent declarator.  */
15839       declarator = cp_parser_declarator (parser, dcl_kind,
15840                                          /*ctor_dtor_or_conv_p=*/NULL,
15841                                          /*parenthesized_p=*/NULL,
15842                                          /*member_p=*/false);
15843
15844       /* If we are parsing an abstract-declarator, we must handle the
15845          case where the dependent declarator is absent.  */
15846       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15847           && !cp_parser_parse_definitely (parser))
15848         declarator = NULL;
15849
15850       declarator = cp_parser_make_indirect_declarator
15851         (code, class_type, cv_quals, declarator);
15852     }
15853   /* Everything else is a direct-declarator.  */
15854   else
15855     {
15856       if (parenthesized_p)
15857         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15858                                                    CPP_OPEN_PAREN);
15859       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15860                                                 ctor_dtor_or_conv_p,
15861                                                 member_p);
15862     }
15863
15864   if (attributes && declarator && declarator != cp_error_declarator)
15865     declarator->attributes = attributes;
15866
15867   return declarator;
15868 }
15869
15870 /* Parse a direct-declarator or direct-abstract-declarator.
15871
15872    direct-declarator:
15873      declarator-id
15874      direct-declarator ( parameter-declaration-clause )
15875        cv-qualifier-seq [opt]
15876        exception-specification [opt]
15877      direct-declarator [ constant-expression [opt] ]
15878      ( declarator )
15879
15880    direct-abstract-declarator:
15881      direct-abstract-declarator [opt]
15882        ( parameter-declaration-clause )
15883        cv-qualifier-seq [opt]
15884        exception-specification [opt]
15885      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15886      ( abstract-declarator )
15887
15888    Returns a representation of the declarator.  DCL_KIND is
15889    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15890    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15891    we are parsing a direct-declarator.  It is
15892    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15893    of ambiguity we prefer an abstract declarator, as per
15894    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15895    cp_parser_declarator.  */
15896
15897 static cp_declarator *
15898 cp_parser_direct_declarator (cp_parser* parser,
15899                              cp_parser_declarator_kind dcl_kind,
15900                              int* ctor_dtor_or_conv_p,
15901                              bool member_p)
15902 {
15903   cp_token *token;
15904   cp_declarator *declarator = NULL;
15905   tree scope = NULL_TREE;
15906   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15907   bool saved_in_declarator_p = parser->in_declarator_p;
15908   bool first = true;
15909   tree pushed_scope = NULL_TREE;
15910
15911   while (true)
15912     {
15913       /* Peek at the next token.  */
15914       token = cp_lexer_peek_token (parser->lexer);
15915       if (token->type == CPP_OPEN_PAREN)
15916         {
15917           /* This is either a parameter-declaration-clause, or a
15918              parenthesized declarator. When we know we are parsing a
15919              named declarator, it must be a parenthesized declarator
15920              if FIRST is true. For instance, `(int)' is a
15921              parameter-declaration-clause, with an omitted
15922              direct-abstract-declarator. But `((*))', is a
15923              parenthesized abstract declarator. Finally, when T is a
15924              template parameter `(T)' is a
15925              parameter-declaration-clause, and not a parenthesized
15926              named declarator.
15927
15928              We first try and parse a parameter-declaration-clause,
15929              and then try a nested declarator (if FIRST is true).
15930
15931              It is not an error for it not to be a
15932              parameter-declaration-clause, even when FIRST is
15933              false. Consider,
15934
15935                int i (int);
15936                int i (3);
15937
15938              The first is the declaration of a function while the
15939              second is the definition of a variable, including its
15940              initializer.
15941
15942              Having seen only the parenthesis, we cannot know which of
15943              these two alternatives should be selected.  Even more
15944              complex are examples like:
15945
15946                int i (int (a));
15947                int i (int (3));
15948
15949              The former is a function-declaration; the latter is a
15950              variable initialization.
15951
15952              Thus again, we try a parameter-declaration-clause, and if
15953              that fails, we back out and return.  */
15954
15955           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15956             {
15957               tree params;
15958               unsigned saved_num_template_parameter_lists;
15959               bool is_declarator = false;
15960               tree t;
15961
15962               /* In a member-declarator, the only valid interpretation
15963                  of a parenthesis is the start of a
15964                  parameter-declaration-clause.  (It is invalid to
15965                  initialize a static data member with a parenthesized
15966                  initializer; only the "=" form of initialization is
15967                  permitted.)  */
15968               if (!member_p)
15969                 cp_parser_parse_tentatively (parser);
15970
15971               /* Consume the `('.  */
15972               cp_lexer_consume_token (parser->lexer);
15973               if (first)
15974                 {
15975                   /* If this is going to be an abstract declarator, we're
15976                      in a declarator and we can't have default args.  */
15977                   parser->default_arg_ok_p = false;
15978                   parser->in_declarator_p = true;
15979                 }
15980
15981               /* Inside the function parameter list, surrounding
15982                  template-parameter-lists do not apply.  */
15983               saved_num_template_parameter_lists
15984                 = parser->num_template_parameter_lists;
15985               parser->num_template_parameter_lists = 0;
15986
15987               begin_scope (sk_function_parms, NULL_TREE);
15988
15989               /* Parse the parameter-declaration-clause.  */
15990               params = cp_parser_parameter_declaration_clause (parser);
15991
15992               parser->num_template_parameter_lists
15993                 = saved_num_template_parameter_lists;
15994
15995               /* Consume the `)'.  */
15996               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15997
15998               /* If all went well, parse the cv-qualifier-seq and the
15999                  exception-specification.  */
16000               if (member_p || cp_parser_parse_definitely (parser))
16001                 {
16002                   cp_cv_quals cv_quals;
16003                   cp_virt_specifiers virt_specifiers;
16004                   tree exception_specification;
16005                   tree late_return;
16006
16007                   is_declarator = true;
16008
16009                   if (ctor_dtor_or_conv_p)
16010                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
16011                   first = false;
16012
16013                   /* Parse the cv-qualifier-seq.  */
16014                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16015                   /* And the exception-specification.  */
16016                   exception_specification
16017                     = cp_parser_exception_specification_opt (parser);
16018                   /* Parse the virt-specifier-seq.  */
16019                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
16020
16021                   late_return = (cp_parser_late_return_type_opt
16022                                  (parser, member_p ? cv_quals : -1));
16023
16024                   /* Create the function-declarator.  */
16025                   declarator = make_call_declarator (declarator,
16026                                                      params,
16027                                                      cv_quals,
16028                                                      virt_specifiers,
16029                                                      exception_specification,
16030                                                      late_return);
16031                   /* Any subsequent parameter lists are to do with
16032                      return type, so are not those of the declared
16033                      function.  */
16034                   parser->default_arg_ok_p = false;
16035                 }
16036
16037               /* Remove the function parms from scope.  */
16038               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
16039                 pop_binding (DECL_NAME (t), t);
16040               leave_scope();
16041
16042               if (is_declarator)
16043                 /* Repeat the main loop.  */
16044                 continue;
16045             }
16046
16047           /* If this is the first, we can try a parenthesized
16048              declarator.  */
16049           if (first)
16050             {
16051               bool saved_in_type_id_in_expr_p;
16052
16053               parser->default_arg_ok_p = saved_default_arg_ok_p;
16054               parser->in_declarator_p = saved_in_declarator_p;
16055
16056               /* Consume the `('.  */
16057               cp_lexer_consume_token (parser->lexer);
16058               /* Parse the nested declarator.  */
16059               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
16060               parser->in_type_id_in_expr_p = true;
16061               declarator
16062                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
16063                                         /*parenthesized_p=*/NULL,
16064                                         member_p);
16065               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
16066               first = false;
16067               /* Expect a `)'.  */
16068               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
16069                 declarator = cp_error_declarator;
16070               if (declarator == cp_error_declarator)
16071                 break;
16072
16073               goto handle_declarator;
16074             }
16075           /* Otherwise, we must be done.  */
16076           else
16077             break;
16078         }
16079       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
16080                && token->type == CPP_OPEN_SQUARE)
16081         {
16082           /* Parse an array-declarator.  */
16083           tree bounds;
16084
16085           if (ctor_dtor_or_conv_p)
16086             *ctor_dtor_or_conv_p = 0;
16087
16088           first = false;
16089           parser->default_arg_ok_p = false;
16090           parser->in_declarator_p = true;
16091           /* Consume the `['.  */
16092           cp_lexer_consume_token (parser->lexer);
16093           /* Peek at the next token.  */
16094           token = cp_lexer_peek_token (parser->lexer);
16095           /* If the next token is `]', then there is no
16096              constant-expression.  */
16097           if (token->type != CPP_CLOSE_SQUARE)
16098             {
16099               bool non_constant_p;
16100
16101               bounds
16102                 = cp_parser_constant_expression (parser,
16103                                                  /*allow_non_constant=*/true,
16104                                                  &non_constant_p);
16105               if (!non_constant_p)
16106                 /* OK */;
16107               else if (error_operand_p (bounds))
16108                 /* Already gave an error.  */;
16109               else if (!parser->in_function_body
16110                        || current_binding_level->kind == sk_function_parms)
16111                 {
16112                   /* Normally, the array bound must be an integral constant
16113                      expression.  However, as an extension, we allow VLAs
16114                      in function scopes as long as they aren't part of a
16115                      parameter declaration.  */
16116                   cp_parser_error (parser,
16117                                    "array bound is not an integer constant");
16118                   bounds = error_mark_node;
16119                 }
16120               else if (processing_template_decl)
16121                 {
16122                   /* Remember this wasn't a constant-expression.  */
16123                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16124                   TREE_SIDE_EFFECTS (bounds) = 1;
16125                 }
16126             }
16127           else
16128             bounds = NULL_TREE;
16129           /* Look for the closing `]'.  */
16130           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16131             {
16132               declarator = cp_error_declarator;
16133               break;
16134             }
16135
16136           declarator = make_array_declarator (declarator, bounds);
16137         }
16138       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16139         {
16140           {
16141             tree qualifying_scope;
16142             tree unqualified_name;
16143             special_function_kind sfk;
16144             bool abstract_ok;
16145             bool pack_expansion_p = false;
16146             cp_token *declarator_id_start_token;
16147
16148             /* Parse a declarator-id */
16149             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16150             if (abstract_ok)
16151               {
16152                 cp_parser_parse_tentatively (parser);
16153
16154                 /* If we see an ellipsis, we should be looking at a
16155                    parameter pack. */
16156                 if (token->type == CPP_ELLIPSIS)
16157                   {
16158                     /* Consume the `...' */
16159                     cp_lexer_consume_token (parser->lexer);
16160
16161                     pack_expansion_p = true;
16162                   }
16163               }
16164
16165             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16166             unqualified_name
16167               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16168             qualifying_scope = parser->scope;
16169             if (abstract_ok)
16170               {
16171                 bool okay = false;
16172
16173                 if (!unqualified_name && pack_expansion_p)
16174                   {
16175                     /* Check whether an error occurred. */
16176                     okay = !cp_parser_error_occurred (parser);
16177
16178                     /* We already consumed the ellipsis to mark a
16179                        parameter pack, but we have no way to report it,
16180                        so abort the tentative parse. We will be exiting
16181                        immediately anyway. */
16182                     cp_parser_abort_tentative_parse (parser);
16183                   }
16184                 else
16185                   okay = cp_parser_parse_definitely (parser);
16186
16187                 if (!okay)
16188                   unqualified_name = error_mark_node;
16189                 else if (unqualified_name
16190                          && (qualifying_scope
16191                              || (TREE_CODE (unqualified_name)
16192                                  != IDENTIFIER_NODE)))
16193                   {
16194                     cp_parser_error (parser, "expected unqualified-id");
16195                     unqualified_name = error_mark_node;
16196                   }
16197               }
16198
16199             if (!unqualified_name)
16200               return NULL;
16201             if (unqualified_name == error_mark_node)
16202               {
16203                 declarator = cp_error_declarator;
16204                 pack_expansion_p = false;
16205                 declarator->parameter_pack_p = false;
16206                 break;
16207               }
16208
16209             if (qualifying_scope && at_namespace_scope_p ()
16210                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16211               {
16212                 /* In the declaration of a member of a template class
16213                    outside of the class itself, the SCOPE will sometimes
16214                    be a TYPENAME_TYPE.  For example, given:
16215
16216                    template <typename T>
16217                    int S<T>::R::i = 3;
16218
16219                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16220                    this context, we must resolve S<T>::R to an ordinary
16221                    type, rather than a typename type.
16222
16223                    The reason we normally avoid resolving TYPENAME_TYPEs
16224                    is that a specialization of `S' might render
16225                    `S<T>::R' not a type.  However, if `S' is
16226                    specialized, then this `i' will not be used, so there
16227                    is no harm in resolving the types here.  */
16228                 tree type;
16229
16230                 /* Resolve the TYPENAME_TYPE.  */
16231                 type = resolve_typename_type (qualifying_scope,
16232                                               /*only_current_p=*/false);
16233                 /* If that failed, the declarator is invalid.  */
16234                 if (TREE_CODE (type) == TYPENAME_TYPE)
16235                   {
16236                     if (typedef_variant_p (type))
16237                       error_at (declarator_id_start_token->location,
16238                                 "cannot define member of dependent typedef "
16239                                 "%qT", type);
16240                     else
16241                       error_at (declarator_id_start_token->location,
16242                                 "%<%T::%E%> is not a type",
16243                                 TYPE_CONTEXT (qualifying_scope),
16244                                 TYPE_IDENTIFIER (qualifying_scope));
16245                   }
16246                 qualifying_scope = type;
16247               }
16248
16249             sfk = sfk_none;
16250
16251             if (unqualified_name)
16252               {
16253                 tree class_type;
16254
16255                 if (qualifying_scope
16256                     && CLASS_TYPE_P (qualifying_scope))
16257                   class_type = qualifying_scope;
16258                 else
16259                   class_type = current_class_type;
16260
16261                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16262                   {
16263                     tree name_type = TREE_TYPE (unqualified_name);
16264                     if (class_type && same_type_p (name_type, class_type))
16265                       {
16266                         if (qualifying_scope
16267                             && CLASSTYPE_USE_TEMPLATE (name_type))
16268                           {
16269                             error_at (declarator_id_start_token->location,
16270                                       "invalid use of constructor as a template");
16271                             inform (declarator_id_start_token->location,
16272                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16273                                     "name the constructor in a qualified name",
16274                                     class_type,
16275                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16276                                     class_type, name_type);
16277                             declarator = cp_error_declarator;
16278                             break;
16279                           }
16280                         else
16281                           unqualified_name = constructor_name (class_type);
16282                       }
16283                     else
16284                       {
16285                         /* We do not attempt to print the declarator
16286                            here because we do not have enough
16287                            information about its original syntactic
16288                            form.  */
16289                         cp_parser_error (parser, "invalid declarator");
16290                         declarator = cp_error_declarator;
16291                         break;
16292                       }
16293                   }
16294
16295                 if (class_type)
16296                   {
16297                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16298                       sfk = sfk_destructor;
16299                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16300                       sfk = sfk_conversion;
16301                     else if (/* There's no way to declare a constructor
16302                                 for an anonymous type, even if the type
16303                                 got a name for linkage purposes.  */
16304                              !TYPE_WAS_ANONYMOUS (class_type)
16305                              && constructor_name_p (unqualified_name,
16306                                                     class_type))
16307                       {
16308                         unqualified_name = constructor_name (class_type);
16309                         sfk = sfk_constructor;
16310                       }
16311                     else if (is_overloaded_fn (unqualified_name)
16312                              && DECL_CONSTRUCTOR_P (get_first_fn
16313                                                     (unqualified_name)))
16314                       sfk = sfk_constructor;
16315
16316                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16317                       *ctor_dtor_or_conv_p = -1;
16318                   }
16319               }
16320             declarator = make_id_declarator (qualifying_scope,
16321                                              unqualified_name,
16322                                              sfk);
16323             declarator->id_loc = token->location;
16324             declarator->parameter_pack_p = pack_expansion_p;
16325
16326             if (pack_expansion_p)
16327               maybe_warn_variadic_templates ();
16328           }
16329
16330         handle_declarator:;
16331           scope = get_scope_of_declarator (declarator);
16332           if (scope)
16333             /* Any names that appear after the declarator-id for a
16334                member are looked up in the containing scope.  */
16335             pushed_scope = push_scope (scope);
16336           parser->in_declarator_p = true;
16337           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16338               || (declarator && declarator->kind == cdk_id))
16339             /* Default args are only allowed on function
16340                declarations.  */
16341             parser->default_arg_ok_p = saved_default_arg_ok_p;
16342           else
16343             parser->default_arg_ok_p = false;
16344
16345           first = false;
16346         }
16347       /* We're done.  */
16348       else
16349         break;
16350     }
16351
16352   /* For an abstract declarator, we might wind up with nothing at this
16353      point.  That's an error; the declarator is not optional.  */
16354   if (!declarator)
16355     cp_parser_error (parser, "expected declarator");
16356
16357   /* If we entered a scope, we must exit it now.  */
16358   if (pushed_scope)
16359     pop_scope (pushed_scope);
16360
16361   parser->default_arg_ok_p = saved_default_arg_ok_p;
16362   parser->in_declarator_p = saved_in_declarator_p;
16363
16364   return declarator;
16365 }
16366
16367 /* Parse a ptr-operator.
16368
16369    ptr-operator:
16370      * cv-qualifier-seq [opt]
16371      &
16372      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16373
16374    GNU Extension:
16375
16376    ptr-operator:
16377      & cv-qualifier-seq [opt]
16378
16379    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16380    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16381    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16382    filled in with the TYPE containing the member.  *CV_QUALS is
16383    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16384    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16385    Note that the tree codes returned by this function have nothing
16386    to do with the types of trees that will be eventually be created
16387    to represent the pointer or reference type being parsed. They are
16388    just constants with suggestive names. */
16389 static enum tree_code
16390 cp_parser_ptr_operator (cp_parser* parser,
16391                         tree* type,
16392                         cp_cv_quals *cv_quals)
16393 {
16394   enum tree_code code = ERROR_MARK;
16395   cp_token *token;
16396
16397   /* Assume that it's not a pointer-to-member.  */
16398   *type = NULL_TREE;
16399   /* And that there are no cv-qualifiers.  */
16400   *cv_quals = TYPE_UNQUALIFIED;
16401
16402   /* Peek at the next token.  */
16403   token = cp_lexer_peek_token (parser->lexer);
16404
16405   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16406   if (token->type == CPP_MULT)
16407     code = INDIRECT_REF;
16408   else if (token->type == CPP_AND)
16409     code = ADDR_EXPR;
16410   else if ((cxx_dialect != cxx98) &&
16411            token->type == CPP_AND_AND) /* C++0x only */
16412     code = NON_LVALUE_EXPR;
16413
16414   if (code != ERROR_MARK)
16415     {
16416       /* Consume the `*', `&' or `&&'.  */
16417       cp_lexer_consume_token (parser->lexer);
16418
16419       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16420          `&', if we are allowing GNU extensions.  (The only qualifier
16421          that can legally appear after `&' is `restrict', but that is
16422          enforced during semantic analysis.  */
16423       if (code == INDIRECT_REF
16424           || cp_parser_allow_gnu_extensions_p (parser))
16425         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16426     }
16427   else
16428     {
16429       /* Try the pointer-to-member case.  */
16430       cp_parser_parse_tentatively (parser);
16431       /* Look for the optional `::' operator.  */
16432       cp_parser_global_scope_opt (parser,
16433                                   /*current_scope_valid_p=*/false);
16434       /* Look for the nested-name specifier.  */
16435       token = cp_lexer_peek_token (parser->lexer);
16436       cp_parser_nested_name_specifier (parser,
16437                                        /*typename_keyword_p=*/false,
16438                                        /*check_dependency_p=*/true,
16439                                        /*type_p=*/false,
16440                                        /*is_declaration=*/false);
16441       /* If we found it, and the next token is a `*', then we are
16442          indeed looking at a pointer-to-member operator.  */
16443       if (!cp_parser_error_occurred (parser)
16444           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16445         {
16446           /* Indicate that the `*' operator was used.  */
16447           code = INDIRECT_REF;
16448
16449           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16450             error_at (token->location, "%qD is a namespace", parser->scope);
16451           else if (TREE_CODE (parser->scope) == ENUMERAL_TYPE)
16452             error_at (token->location, "cannot form pointer to member of "
16453                       "non-class %q#T", parser->scope);
16454           else
16455             {
16456               /* The type of which the member is a member is given by the
16457                  current SCOPE.  */
16458               *type = parser->scope;
16459               /* The next name will not be qualified.  */
16460               parser->scope = NULL_TREE;
16461               parser->qualifying_scope = NULL_TREE;
16462               parser->object_scope = NULL_TREE;
16463               /* Look for the optional cv-qualifier-seq.  */
16464               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16465             }
16466         }
16467       /* If that didn't work we don't have a ptr-operator.  */
16468       if (!cp_parser_parse_definitely (parser))
16469         cp_parser_error (parser, "expected ptr-operator");
16470     }
16471
16472   return code;
16473 }
16474
16475 /* Parse an (optional) cv-qualifier-seq.
16476
16477    cv-qualifier-seq:
16478      cv-qualifier cv-qualifier-seq [opt]
16479
16480    cv-qualifier:
16481      const
16482      volatile
16483
16484    GNU Extension:
16485
16486    cv-qualifier:
16487      __restrict__
16488
16489    Returns a bitmask representing the cv-qualifiers.  */
16490
16491 static cp_cv_quals
16492 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16493 {
16494   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16495
16496   while (true)
16497     {
16498       cp_token *token;
16499       cp_cv_quals cv_qualifier;
16500
16501       /* Peek at the next token.  */
16502       token = cp_lexer_peek_token (parser->lexer);
16503       /* See if it's a cv-qualifier.  */
16504       switch (token->keyword)
16505         {
16506         case RID_CONST:
16507           cv_qualifier = TYPE_QUAL_CONST;
16508           break;
16509
16510         case RID_VOLATILE:
16511           cv_qualifier = TYPE_QUAL_VOLATILE;
16512           break;
16513
16514         case RID_RESTRICT:
16515           cv_qualifier = TYPE_QUAL_RESTRICT;
16516           break;
16517
16518         default:
16519           cv_qualifier = TYPE_UNQUALIFIED;
16520           break;
16521         }
16522
16523       if (!cv_qualifier)
16524         break;
16525
16526       if (cv_quals & cv_qualifier)
16527         {
16528           error_at (token->location, "duplicate cv-qualifier");
16529           cp_lexer_purge_token (parser->lexer);
16530         }
16531       else
16532         {
16533           cp_lexer_consume_token (parser->lexer);
16534           cv_quals |= cv_qualifier;
16535         }
16536     }
16537
16538   return cv_quals;
16539 }
16540
16541 /* Parse an (optional) virt-specifier-seq.
16542
16543    virt-specifier-seq:
16544      virt-specifier virt-specifier-seq [opt]
16545
16546    virt-specifier:
16547      override
16548      final
16549
16550    Returns a bitmask representing the virt-specifiers.  */
16551
16552 static cp_virt_specifiers
16553 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16554 {
16555   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16556
16557   while (true)
16558     {
16559       cp_token *token;
16560       cp_virt_specifiers virt_specifier;
16561
16562       /* Peek at the next token.  */
16563       token = cp_lexer_peek_token (parser->lexer);
16564       /* See if it's a virt-specifier-qualifier.  */
16565       if (token->type != CPP_NAME)
16566         break;
16567       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16568         {
16569           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16570           virt_specifier = VIRT_SPEC_OVERRIDE;
16571         }
16572       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16573         {
16574           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16575           virt_specifier = VIRT_SPEC_FINAL;
16576         }
16577       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16578         {
16579           virt_specifier = VIRT_SPEC_FINAL;
16580         }
16581       else
16582         break;
16583
16584       if (virt_specifiers & virt_specifier)
16585         {
16586           error_at (token->location, "duplicate virt-specifier");
16587           cp_lexer_purge_token (parser->lexer);
16588         }
16589       else
16590         {
16591           cp_lexer_consume_token (parser->lexer);
16592           virt_specifiers |= virt_specifier;
16593         }
16594     }
16595   return virt_specifiers;
16596 }
16597
16598 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16599    is in scope even though it isn't real.  */
16600
16601 static void
16602 inject_this_parameter (tree ctype, cp_cv_quals quals)
16603 {
16604   tree this_parm;
16605
16606   if (current_class_ptr)
16607     {
16608       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16609       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16610       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16611           && cp_type_quals (type) == quals)
16612         return;
16613     }
16614
16615   this_parm = build_this_parm (ctype, quals);
16616   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16617   current_class_ptr = NULL_TREE;
16618   current_class_ref
16619     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16620   current_class_ptr = this_parm;
16621 }
16622
16623 /* Parse a late-specified return type, if any.  This is not a separate
16624    non-terminal, but part of a function declarator, which looks like
16625
16626    -> trailing-type-specifier-seq abstract-declarator(opt)
16627
16628    Returns the type indicated by the type-id.
16629
16630    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16631    function.  */
16632
16633 static tree
16634 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16635 {
16636   cp_token *token;
16637   tree type;
16638
16639   /* Peek at the next token.  */
16640   token = cp_lexer_peek_token (parser->lexer);
16641   /* A late-specified return type is indicated by an initial '->'. */
16642   if (token->type != CPP_DEREF)
16643     return NULL_TREE;
16644
16645   /* Consume the ->.  */
16646   cp_lexer_consume_token (parser->lexer);
16647
16648   if (quals >= 0)
16649     {
16650       /* DR 1207: 'this' is in scope in the trailing return type.  */
16651       gcc_assert (current_class_ptr == NULL_TREE);
16652       inject_this_parameter (current_class_type, quals);
16653     }
16654
16655   type = cp_parser_trailing_type_id (parser);
16656
16657   if (quals >= 0)
16658     current_class_ptr = current_class_ref = NULL_TREE;
16659
16660   return type;
16661 }
16662
16663 /* Parse a declarator-id.
16664
16665    declarator-id:
16666      id-expression
16667      :: [opt] nested-name-specifier [opt] type-name
16668
16669    In the `id-expression' case, the value returned is as for
16670    cp_parser_id_expression if the id-expression was an unqualified-id.
16671    If the id-expression was a qualified-id, then a SCOPE_REF is
16672    returned.  The first operand is the scope (either a NAMESPACE_DECL
16673    or TREE_TYPE), but the second is still just a representation of an
16674    unqualified-id.  */
16675
16676 static tree
16677 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16678 {
16679   tree id;
16680   /* The expression must be an id-expression.  Assume that qualified
16681      names are the names of types so that:
16682
16683        template <class T>
16684        int S<T>::R::i = 3;
16685
16686      will work; we must treat `S<T>::R' as the name of a type.
16687      Similarly, assume that qualified names are templates, where
16688      required, so that:
16689
16690        template <class T>
16691        int S<T>::R<T>::i = 3;
16692
16693      will work, too.  */
16694   id = cp_parser_id_expression (parser,
16695                                 /*template_keyword_p=*/false,
16696                                 /*check_dependency_p=*/false,
16697                                 /*template_p=*/NULL,
16698                                 /*declarator_p=*/true,
16699                                 optional_p);
16700   if (id && BASELINK_P (id))
16701     id = BASELINK_FUNCTIONS (id);
16702   return id;
16703 }
16704
16705 /* Parse a type-id.
16706
16707    type-id:
16708      type-specifier-seq abstract-declarator [opt]
16709
16710    Returns the TYPE specified.  */
16711
16712 static tree
16713 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16714                      bool is_trailing_return)
16715 {
16716   cp_decl_specifier_seq type_specifier_seq;
16717   cp_declarator *abstract_declarator;
16718
16719   /* Parse the type-specifier-seq.  */
16720   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16721                                 is_trailing_return,
16722                                 &type_specifier_seq);
16723   if (type_specifier_seq.type == error_mark_node)
16724     return error_mark_node;
16725
16726   /* There might or might not be an abstract declarator.  */
16727   cp_parser_parse_tentatively (parser);
16728   /* Look for the declarator.  */
16729   abstract_declarator
16730     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16731                             /*parenthesized_p=*/NULL,
16732                             /*member_p=*/false);
16733   /* Check to see if there really was a declarator.  */
16734   if (!cp_parser_parse_definitely (parser))
16735     abstract_declarator = NULL;
16736
16737   if (type_specifier_seq.type
16738       && type_uses_auto (type_specifier_seq.type))
16739     {
16740       /* A type-id with type 'auto' is only ok if the abstract declarator
16741          is a function declarator with a late-specified return type.  */
16742       if (abstract_declarator
16743           && abstract_declarator->kind == cdk_function
16744           && abstract_declarator->u.function.late_return_type)
16745         /* OK */;
16746       else
16747         {
16748           error ("invalid use of %<auto%>");
16749           return error_mark_node;
16750         }
16751     }
16752   
16753   return groktypename (&type_specifier_seq, abstract_declarator,
16754                        is_template_arg);
16755 }
16756
16757 static tree cp_parser_type_id (cp_parser *parser)
16758 {
16759   return cp_parser_type_id_1 (parser, false, false);
16760 }
16761
16762 static tree cp_parser_template_type_arg (cp_parser *parser)
16763 {
16764   tree r;
16765   const char *saved_message = parser->type_definition_forbidden_message;
16766   parser->type_definition_forbidden_message
16767     = G_("types may not be defined in template arguments");
16768   r = cp_parser_type_id_1 (parser, true, false);
16769   parser->type_definition_forbidden_message = saved_message;
16770   return r;
16771 }
16772
16773 static tree cp_parser_trailing_type_id (cp_parser *parser)
16774 {
16775   return cp_parser_type_id_1 (parser, false, true);
16776 }
16777
16778 /* Parse a type-specifier-seq.
16779
16780    type-specifier-seq:
16781      type-specifier type-specifier-seq [opt]
16782
16783    GNU extension:
16784
16785    type-specifier-seq:
16786      attributes type-specifier-seq [opt]
16787
16788    If IS_DECLARATION is true, we are at the start of a "condition" or
16789    exception-declaration, so we might be followed by a declarator-id.
16790
16791    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16792    i.e. we've just seen "->".
16793
16794    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16795
16796 static void
16797 cp_parser_type_specifier_seq (cp_parser* parser,
16798                               bool is_declaration,
16799                               bool is_trailing_return,
16800                               cp_decl_specifier_seq *type_specifier_seq)
16801 {
16802   bool seen_type_specifier = false;
16803   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16804   cp_token *start_token = NULL;
16805
16806   /* Clear the TYPE_SPECIFIER_SEQ.  */
16807   clear_decl_specs (type_specifier_seq);
16808
16809   /* In the context of a trailing return type, enum E { } is an
16810      elaborated-type-specifier followed by a function-body, not an
16811      enum-specifier.  */
16812   if (is_trailing_return)
16813     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16814
16815   /* Parse the type-specifiers and attributes.  */
16816   while (true)
16817     {
16818       tree type_specifier;
16819       bool is_cv_qualifier;
16820
16821       /* Check for attributes first.  */
16822       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16823         {
16824           type_specifier_seq->attributes =
16825             chainon (type_specifier_seq->attributes,
16826                      cp_parser_attributes_opt (parser));
16827           continue;
16828         }
16829
16830       /* record the token of the beginning of the type specifier seq,
16831          for error reporting purposes*/
16832      if (!start_token)
16833        start_token = cp_lexer_peek_token (parser->lexer);
16834
16835       /* Look for the type-specifier.  */
16836       type_specifier = cp_parser_type_specifier (parser,
16837                                                  flags,
16838                                                  type_specifier_seq,
16839                                                  /*is_declaration=*/false,
16840                                                  NULL,
16841                                                  &is_cv_qualifier);
16842       if (!type_specifier)
16843         {
16844           /* If the first type-specifier could not be found, this is not a
16845              type-specifier-seq at all.  */
16846           if (!seen_type_specifier)
16847             {
16848               cp_parser_error (parser, "expected type-specifier");
16849               type_specifier_seq->type = error_mark_node;
16850               return;
16851             }
16852           /* If subsequent type-specifiers could not be found, the
16853              type-specifier-seq is complete.  */
16854           break;
16855         }
16856
16857       seen_type_specifier = true;
16858       /* The standard says that a condition can be:
16859
16860             type-specifier-seq declarator = assignment-expression
16861
16862          However, given:
16863
16864            struct S {};
16865            if (int S = ...)
16866
16867          we should treat the "S" as a declarator, not as a
16868          type-specifier.  The standard doesn't say that explicitly for
16869          type-specifier-seq, but it does say that for
16870          decl-specifier-seq in an ordinary declaration.  Perhaps it
16871          would be clearer just to allow a decl-specifier-seq here, and
16872          then add a semantic restriction that if any decl-specifiers
16873          that are not type-specifiers appear, the program is invalid.  */
16874       if (is_declaration && !is_cv_qualifier)
16875         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16876     }
16877
16878   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16879 }
16880
16881 /* Parse a parameter-declaration-clause.
16882
16883    parameter-declaration-clause:
16884      parameter-declaration-list [opt] ... [opt]
16885      parameter-declaration-list , ...
16886
16887    Returns a representation for the parameter declarations.  A return
16888    value of NULL indicates a parameter-declaration-clause consisting
16889    only of an ellipsis.  */
16890
16891 static tree
16892 cp_parser_parameter_declaration_clause (cp_parser* parser)
16893 {
16894   tree parameters;
16895   cp_token *token;
16896   bool ellipsis_p;
16897   bool is_error;
16898
16899   /* Peek at the next token.  */
16900   token = cp_lexer_peek_token (parser->lexer);
16901   /* Check for trivial parameter-declaration-clauses.  */
16902   if (token->type == CPP_ELLIPSIS)
16903     {
16904       /* Consume the `...' token.  */
16905       cp_lexer_consume_token (parser->lexer);
16906       return NULL_TREE;
16907     }
16908   else if (token->type == CPP_CLOSE_PAREN)
16909     /* There are no parameters.  */
16910     {
16911 #ifndef NO_IMPLICIT_EXTERN_C
16912       if (in_system_header && current_class_type == NULL
16913           && current_lang_name == lang_name_c)
16914         return NULL_TREE;
16915       else
16916 #endif
16917         return void_list_node;
16918     }
16919   /* Check for `(void)', too, which is a special case.  */
16920   else if (token->keyword == RID_VOID
16921            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16922                == CPP_CLOSE_PAREN))
16923     {
16924       /* Consume the `void' token.  */
16925       cp_lexer_consume_token (parser->lexer);
16926       /* There are no parameters.  */
16927       return void_list_node;
16928     }
16929
16930   /* Parse the parameter-declaration-list.  */
16931   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16932   /* If a parse error occurred while parsing the
16933      parameter-declaration-list, then the entire
16934      parameter-declaration-clause is erroneous.  */
16935   if (is_error)
16936     return NULL;
16937
16938   /* Peek at the next token.  */
16939   token = cp_lexer_peek_token (parser->lexer);
16940   /* If it's a `,', the clause should terminate with an ellipsis.  */
16941   if (token->type == CPP_COMMA)
16942     {
16943       /* Consume the `,'.  */
16944       cp_lexer_consume_token (parser->lexer);
16945       /* Expect an ellipsis.  */
16946       ellipsis_p
16947         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16948     }
16949   /* It might also be `...' if the optional trailing `,' was
16950      omitted.  */
16951   else if (token->type == CPP_ELLIPSIS)
16952     {
16953       /* Consume the `...' token.  */
16954       cp_lexer_consume_token (parser->lexer);
16955       /* And remember that we saw it.  */
16956       ellipsis_p = true;
16957     }
16958   else
16959     ellipsis_p = false;
16960
16961   /* Finish the parameter list.  */
16962   if (!ellipsis_p)
16963     parameters = chainon (parameters, void_list_node);
16964
16965   return parameters;
16966 }
16967
16968 /* Parse a parameter-declaration-list.
16969
16970    parameter-declaration-list:
16971      parameter-declaration
16972      parameter-declaration-list , parameter-declaration
16973
16974    Returns a representation of the parameter-declaration-list, as for
16975    cp_parser_parameter_declaration_clause.  However, the
16976    `void_list_node' is never appended to the list.  Upon return,
16977    *IS_ERROR will be true iff an error occurred.  */
16978
16979 static tree
16980 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16981 {
16982   tree parameters = NULL_TREE;
16983   tree *tail = &parameters; 
16984   bool saved_in_unbraced_linkage_specification_p;
16985   int index = 0;
16986
16987   /* Assume all will go well.  */
16988   *is_error = false;
16989   /* The special considerations that apply to a function within an
16990      unbraced linkage specifications do not apply to the parameters
16991      to the function.  */
16992   saved_in_unbraced_linkage_specification_p 
16993     = parser->in_unbraced_linkage_specification_p;
16994   parser->in_unbraced_linkage_specification_p = false;
16995
16996   /* Look for more parameters.  */
16997   while (true)
16998     {
16999       cp_parameter_declarator *parameter;
17000       tree decl = error_mark_node;
17001       bool parenthesized_p = false;
17002       /* Parse the parameter.  */
17003       parameter
17004         = cp_parser_parameter_declaration (parser,
17005                                            /*template_parm_p=*/false,
17006                                            &parenthesized_p);
17007
17008       /* We don't know yet if the enclosing context is deprecated, so wait
17009          and warn in grokparms if appropriate.  */
17010       deprecated_state = DEPRECATED_SUPPRESS;
17011
17012       if (parameter)
17013         decl = grokdeclarator (parameter->declarator,
17014                                &parameter->decl_specifiers,
17015                                PARM,
17016                                parameter->default_argument != NULL_TREE,
17017                                &parameter->decl_specifiers.attributes);
17018
17019       deprecated_state = DEPRECATED_NORMAL;
17020
17021       /* If a parse error occurred parsing the parameter declaration,
17022          then the entire parameter-declaration-list is erroneous.  */
17023       if (decl == error_mark_node)
17024         {
17025           *is_error = true;
17026           parameters = error_mark_node;
17027           break;
17028         }
17029
17030       if (parameter->decl_specifiers.attributes)
17031         cplus_decl_attributes (&decl,
17032                                parameter->decl_specifiers.attributes,
17033                                0);
17034       if (DECL_NAME (decl))
17035         decl = pushdecl (decl);
17036
17037       if (decl != error_mark_node)
17038         {
17039           retrofit_lang_decl (decl);
17040           DECL_PARM_INDEX (decl) = ++index;
17041           DECL_PARM_LEVEL (decl) = function_parm_depth ();
17042         }
17043
17044       /* Add the new parameter to the list.  */
17045       *tail = build_tree_list (parameter->default_argument, decl);
17046       tail = &TREE_CHAIN (*tail);
17047
17048       /* Peek at the next token.  */
17049       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
17050           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
17051           /* These are for Objective-C++ */
17052           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
17053           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
17054         /* The parameter-declaration-list is complete.  */
17055         break;
17056       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17057         {
17058           cp_token *token;
17059
17060           /* Peek at the next token.  */
17061           token = cp_lexer_peek_nth_token (parser->lexer, 2);
17062           /* If it's an ellipsis, then the list is complete.  */
17063           if (token->type == CPP_ELLIPSIS)
17064             break;
17065           /* Otherwise, there must be more parameters.  Consume the
17066              `,'.  */
17067           cp_lexer_consume_token (parser->lexer);
17068           /* When parsing something like:
17069
17070                 int i(float f, double d)
17071
17072              we can tell after seeing the declaration for "f" that we
17073              are not looking at an initialization of a variable "i",
17074              but rather at the declaration of a function "i".
17075
17076              Due to the fact that the parsing of template arguments
17077              (as specified to a template-id) requires backtracking we
17078              cannot use this technique when inside a template argument
17079              list.  */
17080           if (!parser->in_template_argument_list_p
17081               && !parser->in_type_id_in_expr_p
17082               && cp_parser_uncommitted_to_tentative_parse_p (parser)
17083               /* However, a parameter-declaration of the form
17084                  "foat(f)" (which is a valid declaration of a
17085                  parameter "f") can also be interpreted as an
17086                  expression (the conversion of "f" to "float").  */
17087               && !parenthesized_p)
17088             cp_parser_commit_to_tentative_parse (parser);
17089         }
17090       else
17091         {
17092           cp_parser_error (parser, "expected %<,%> or %<...%>");
17093           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
17094             cp_parser_skip_to_closing_parenthesis (parser,
17095                                                    /*recovering=*/true,
17096                                                    /*or_comma=*/false,
17097                                                    /*consume_paren=*/false);
17098           break;
17099         }
17100     }
17101
17102   parser->in_unbraced_linkage_specification_p
17103     = saved_in_unbraced_linkage_specification_p;
17104
17105   return parameters;
17106 }
17107
17108 /* Parse a parameter declaration.
17109
17110    parameter-declaration:
17111      decl-specifier-seq ... [opt] declarator
17112      decl-specifier-seq declarator = assignment-expression
17113      decl-specifier-seq ... [opt] abstract-declarator [opt]
17114      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17115
17116    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17117    declares a template parameter.  (In that case, a non-nested `>'
17118    token encountered during the parsing of the assignment-expression
17119    is not interpreted as a greater-than operator.)
17120
17121    Returns a representation of the parameter, or NULL if an error
17122    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17123    true iff the declarator is of the form "(p)".  */
17124
17125 static cp_parameter_declarator *
17126 cp_parser_parameter_declaration (cp_parser *parser,
17127                                  bool template_parm_p,
17128                                  bool *parenthesized_p)
17129 {
17130   int declares_class_or_enum;
17131   cp_decl_specifier_seq decl_specifiers;
17132   cp_declarator *declarator;
17133   tree default_argument;
17134   cp_token *token = NULL, *declarator_token_start = NULL;
17135   const char *saved_message;
17136
17137   /* In a template parameter, `>' is not an operator.
17138
17139      [temp.param]
17140
17141      When parsing a default template-argument for a non-type
17142      template-parameter, the first non-nested `>' is taken as the end
17143      of the template parameter-list rather than a greater-than
17144      operator.  */
17145
17146   /* Type definitions may not appear in parameter types.  */
17147   saved_message = parser->type_definition_forbidden_message;
17148   parser->type_definition_forbidden_message
17149     = G_("types may not be defined in parameter types");
17150
17151   /* Parse the declaration-specifiers.  */
17152   cp_parser_decl_specifier_seq (parser,
17153                                 CP_PARSER_FLAGS_NONE,
17154                                 &decl_specifiers,
17155                                 &declares_class_or_enum);
17156
17157   /* Complain about missing 'typename' or other invalid type names.  */
17158   if (!decl_specifiers.any_type_specifiers_p)
17159     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17160
17161   /* If an error occurred, there's no reason to attempt to parse the
17162      rest of the declaration.  */
17163   if (cp_parser_error_occurred (parser))
17164     {
17165       parser->type_definition_forbidden_message = saved_message;
17166       return NULL;
17167     }
17168
17169   /* Peek at the next token.  */
17170   token = cp_lexer_peek_token (parser->lexer);
17171
17172   /* If the next token is a `)', `,', `=', `>', or `...', then there
17173      is no declarator. However, when variadic templates are enabled,
17174      there may be a declarator following `...'.  */
17175   if (token->type == CPP_CLOSE_PAREN
17176       || token->type == CPP_COMMA
17177       || token->type == CPP_EQ
17178       || token->type == CPP_GREATER)
17179     {
17180       declarator = NULL;
17181       if (parenthesized_p)
17182         *parenthesized_p = false;
17183     }
17184   /* Otherwise, there should be a declarator.  */
17185   else
17186     {
17187       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17188       parser->default_arg_ok_p = false;
17189
17190       /* After seeing a decl-specifier-seq, if the next token is not a
17191          "(", there is no possibility that the code is a valid
17192          expression.  Therefore, if parsing tentatively, we commit at
17193          this point.  */
17194       if (!parser->in_template_argument_list_p
17195           /* In an expression context, having seen:
17196
17197                (int((char ...
17198
17199              we cannot be sure whether we are looking at a
17200              function-type (taking a "char" as a parameter) or a cast
17201              of some object of type "char" to "int".  */
17202           && !parser->in_type_id_in_expr_p
17203           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17204           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17205           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17206         cp_parser_commit_to_tentative_parse (parser);
17207       /* Parse the declarator.  */
17208       declarator_token_start = token;
17209       declarator = cp_parser_declarator (parser,
17210                                          CP_PARSER_DECLARATOR_EITHER,
17211                                          /*ctor_dtor_or_conv_p=*/NULL,
17212                                          parenthesized_p,
17213                                          /*member_p=*/false);
17214       parser->default_arg_ok_p = saved_default_arg_ok_p;
17215       /* After the declarator, allow more attributes.  */
17216       decl_specifiers.attributes
17217         = chainon (decl_specifiers.attributes,
17218                    cp_parser_attributes_opt (parser));
17219     }
17220
17221   /* If the next token is an ellipsis, and we have not seen a
17222      declarator name, and the type of the declarator contains parameter
17223      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17224      a parameter pack expansion expression. Otherwise, leave the
17225      ellipsis for a C-style variadic function. */
17226   token = cp_lexer_peek_token (parser->lexer);
17227   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17228     {
17229       tree type = decl_specifiers.type;
17230
17231       if (type && DECL_P (type))
17232         type = TREE_TYPE (type);
17233
17234       if (type
17235           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17236           && declarator_can_be_parameter_pack (declarator)
17237           && (!declarator || !declarator->parameter_pack_p)
17238           && uses_parameter_packs (type))
17239         {
17240           /* Consume the `...'. */
17241           cp_lexer_consume_token (parser->lexer);
17242           maybe_warn_variadic_templates ();
17243           
17244           /* Build a pack expansion type */
17245           if (declarator)
17246             declarator->parameter_pack_p = true;
17247           else
17248             decl_specifiers.type = make_pack_expansion (type);
17249         }
17250     }
17251
17252   /* The restriction on defining new types applies only to the type
17253      of the parameter, not to the default argument.  */
17254   parser->type_definition_forbidden_message = saved_message;
17255
17256   /* If the next token is `=', then process a default argument.  */
17257   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17258     {
17259       /* If we are defining a class, then the tokens that make up the
17260          default argument must be saved and processed later.  */
17261       if (!template_parm_p && at_class_scope_p ()
17262           && TYPE_BEING_DEFINED (current_class_type)
17263           && !LAMBDA_TYPE_P (current_class_type))
17264         {
17265           unsigned depth = 0;
17266           int maybe_template_id = 0;
17267           cp_token *first_token;
17268           cp_token *token;
17269
17270           /* Add tokens until we have processed the entire default
17271              argument.  We add the range [first_token, token).  */
17272           first_token = cp_lexer_peek_token (parser->lexer);
17273           while (true)
17274             {
17275               bool done = false;
17276
17277               /* Peek at the next token.  */
17278               token = cp_lexer_peek_token (parser->lexer);
17279               /* What we do depends on what token we have.  */
17280               switch (token->type)
17281                 {
17282                   /* In valid code, a default argument must be
17283                      immediately followed by a `,' `)', or `...'.  */
17284                 case CPP_COMMA:
17285                   if (depth == 0 && maybe_template_id)
17286                     {
17287                       /* If we've seen a '<', we might be in a
17288                          template-argument-list.  Until Core issue 325 is
17289                          resolved, we don't know how this situation ought
17290                          to be handled, so try to DTRT.  We check whether
17291                          what comes after the comma is a valid parameter
17292                          declaration list.  If it is, then the comma ends
17293                          the default argument; otherwise the default
17294                          argument continues.  */
17295                       bool error = false;
17296                       tree t;
17297
17298                       /* Set ITALP so cp_parser_parameter_declaration_list
17299                          doesn't decide to commit to this parse.  */
17300                       bool saved_italp = parser->in_template_argument_list_p;
17301                       parser->in_template_argument_list_p = true;
17302
17303                       cp_parser_parse_tentatively (parser);
17304                       cp_lexer_consume_token (parser->lexer);
17305                       begin_scope (sk_function_parms, NULL_TREE);
17306                       cp_parser_parameter_declaration_list (parser, &error);
17307                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17308                         pop_binding (DECL_NAME (t), t);
17309                       leave_scope ();
17310                       if (!cp_parser_error_occurred (parser) && !error)
17311                         done = true;
17312                       cp_parser_abort_tentative_parse (parser);
17313
17314                       parser->in_template_argument_list_p = saved_italp;
17315                       break;
17316                     }
17317                 case CPP_CLOSE_PAREN:
17318                 case CPP_ELLIPSIS:
17319                   /* If we run into a non-nested `;', `}', or `]',
17320                      then the code is invalid -- but the default
17321                      argument is certainly over.  */
17322                 case CPP_SEMICOLON:
17323                 case CPP_CLOSE_BRACE:
17324                 case CPP_CLOSE_SQUARE:
17325                   if (depth == 0)
17326                     done = true;
17327                   /* Update DEPTH, if necessary.  */
17328                   else if (token->type == CPP_CLOSE_PAREN
17329                            || token->type == CPP_CLOSE_BRACE
17330                            || token->type == CPP_CLOSE_SQUARE)
17331                     --depth;
17332                   break;
17333
17334                 case CPP_OPEN_PAREN:
17335                 case CPP_OPEN_SQUARE:
17336                 case CPP_OPEN_BRACE:
17337                   ++depth;
17338                   break;
17339
17340                 case CPP_LESS:
17341                   if (depth == 0)
17342                     /* This might be the comparison operator, or it might
17343                        start a template argument list.  */
17344                     ++maybe_template_id;
17345                   break;
17346
17347                 case CPP_RSHIFT:
17348                   if (cxx_dialect == cxx98)
17349                     break;
17350                   /* Fall through for C++0x, which treats the `>>'
17351                      operator like two `>' tokens in certain
17352                      cases.  */
17353
17354                 case CPP_GREATER:
17355                   if (depth == 0)
17356                     {
17357                       /* This might be an operator, or it might close a
17358                          template argument list.  But if a previous '<'
17359                          started a template argument list, this will have
17360                          closed it, so we can't be in one anymore.  */
17361                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17362                       if (maybe_template_id < 0)
17363                         maybe_template_id = 0;
17364                     }
17365                   break;
17366
17367                   /* If we run out of tokens, issue an error message.  */
17368                 case CPP_EOF:
17369                 case CPP_PRAGMA_EOL:
17370                   error_at (token->location, "file ends in default argument");
17371                   done = true;
17372                   break;
17373
17374                 case CPP_NAME:
17375                 case CPP_SCOPE:
17376                   /* In these cases, we should look for template-ids.
17377                      For example, if the default argument is
17378                      `X<int, double>()', we need to do name lookup to
17379                      figure out whether or not `X' is a template; if
17380                      so, the `,' does not end the default argument.
17381
17382                      That is not yet done.  */
17383                   break;
17384
17385                 default:
17386                   break;
17387                 }
17388
17389               /* If we've reached the end, stop.  */
17390               if (done)
17391                 break;
17392
17393               /* Add the token to the token block.  */
17394               token = cp_lexer_consume_token (parser->lexer);
17395             }
17396
17397           /* Create a DEFAULT_ARG to represent the unparsed default
17398              argument.  */
17399           default_argument = make_node (DEFAULT_ARG);
17400           DEFARG_TOKENS (default_argument)
17401             = cp_token_cache_new (first_token, token);
17402           DEFARG_INSTANTIATIONS (default_argument) = NULL;
17403         }
17404       /* Outside of a class definition, we can just parse the
17405          assignment-expression.  */
17406       else
17407         {
17408           token = cp_lexer_peek_token (parser->lexer);
17409           default_argument 
17410             = cp_parser_default_argument (parser, template_parm_p);
17411         }
17412
17413       if (!parser->default_arg_ok_p)
17414         {
17415           if (flag_permissive)
17416             warning (0, "deprecated use of default argument for parameter of non-function");
17417           else
17418             {
17419               error_at (token->location,
17420                         "default arguments are only "
17421                         "permitted for function parameters");
17422               default_argument = NULL_TREE;
17423             }
17424         }
17425       else if ((declarator && declarator->parameter_pack_p)
17426                || (decl_specifiers.type
17427                    && PACK_EXPANSION_P (decl_specifiers.type)))
17428         {
17429           /* Find the name of the parameter pack.  */     
17430           cp_declarator *id_declarator = declarator;
17431           while (id_declarator && id_declarator->kind != cdk_id)
17432             id_declarator = id_declarator->declarator;
17433           
17434           if (id_declarator && id_declarator->kind == cdk_id)
17435             error_at (declarator_token_start->location,
17436                       template_parm_p
17437                       ? G_("template parameter pack %qD "
17438                            "cannot have a default argument")
17439                       : G_("parameter pack %qD cannot have "
17440                            "a default argument"),
17441                       id_declarator->u.id.unqualified_name);
17442           else
17443             error_at (declarator_token_start->location,
17444                       template_parm_p
17445                       ? G_("template parameter pack cannot have "
17446                            "a default argument")
17447                       : G_("parameter pack cannot have a "
17448                            "default argument"));
17449
17450           default_argument = NULL_TREE;
17451         }
17452     }
17453   else
17454     default_argument = NULL_TREE;
17455
17456   return make_parameter_declarator (&decl_specifiers,
17457                                     declarator,
17458                                     default_argument);
17459 }
17460
17461 /* Parse a default argument and return it.
17462
17463    TEMPLATE_PARM_P is true if this is a default argument for a
17464    non-type template parameter.  */
17465 static tree
17466 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17467 {
17468   tree default_argument = NULL_TREE;
17469   bool saved_greater_than_is_operator_p;
17470   bool saved_local_variables_forbidden_p;
17471   bool non_constant_p, is_direct_init;
17472
17473   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17474      set correctly.  */
17475   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17476   parser->greater_than_is_operator_p = !template_parm_p;
17477   /* Local variable names (and the `this' keyword) may not
17478      appear in a default argument.  */
17479   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17480   parser->local_variables_forbidden_p = true;
17481   /* Parse the assignment-expression.  */
17482   if (template_parm_p)
17483     push_deferring_access_checks (dk_no_deferred);
17484   default_argument
17485     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17486   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17487     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17488   if (template_parm_p)
17489     pop_deferring_access_checks ();
17490   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17491   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17492
17493   return default_argument;
17494 }
17495
17496 /* Parse a function-body.
17497
17498    function-body:
17499      compound_statement  */
17500
17501 static void
17502 cp_parser_function_body (cp_parser *parser)
17503 {
17504   cp_parser_compound_statement (parser, NULL, false, true);
17505 }
17506
17507 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17508    true if a ctor-initializer was present.  */
17509
17510 static bool
17511 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17512 {
17513   tree body, list;
17514   bool ctor_initializer_p;
17515   const bool check_body_p =
17516      DECL_CONSTRUCTOR_P (current_function_decl)
17517      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17518   tree last = NULL;
17519
17520   /* Begin the function body.  */
17521   body = begin_function_body ();
17522   /* Parse the optional ctor-initializer.  */
17523   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17524
17525   /* If we're parsing a constexpr constructor definition, we need
17526      to check that the constructor body is indeed empty.  However,
17527      before we get to cp_parser_function_body lot of junk has been
17528      generated, so we can't just check that we have an empty block.
17529      Rather we take a snapshot of the outermost block, and check whether
17530      cp_parser_function_body changed its state.  */
17531   if (check_body_p)
17532     {
17533       list = body;
17534       if (TREE_CODE (list) == BIND_EXPR)
17535         list = BIND_EXPR_BODY (list);
17536       if (TREE_CODE (list) == STATEMENT_LIST
17537           && STATEMENT_LIST_TAIL (list) != NULL)
17538         last = STATEMENT_LIST_TAIL (list)->stmt;
17539     }
17540   /* Parse the function-body.  */
17541   cp_parser_function_body (parser);
17542   if (check_body_p)
17543     check_constexpr_ctor_body (last, list);
17544   /* Finish the function body.  */
17545   finish_function_body (body);
17546
17547   return ctor_initializer_p;
17548 }
17549
17550 /* Parse an initializer.
17551
17552    initializer:
17553      = initializer-clause
17554      ( expression-list )
17555
17556    Returns an expression representing the initializer.  If no
17557    initializer is present, NULL_TREE is returned.
17558
17559    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17560    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17561    set to TRUE if there is no initializer present.  If there is an
17562    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17563    is set to true; otherwise it is set to false.  */
17564
17565 static tree
17566 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17567                        bool* non_constant_p)
17568 {
17569   cp_token *token;
17570   tree init;
17571
17572   /* Peek at the next token.  */
17573   token = cp_lexer_peek_token (parser->lexer);
17574
17575   /* Let our caller know whether or not this initializer was
17576      parenthesized.  */
17577   *is_direct_init = (token->type != CPP_EQ);
17578   /* Assume that the initializer is constant.  */
17579   *non_constant_p = false;
17580
17581   if (token->type == CPP_EQ)
17582     {
17583       /* Consume the `='.  */
17584       cp_lexer_consume_token (parser->lexer);
17585       /* Parse the initializer-clause.  */
17586       init = cp_parser_initializer_clause (parser, non_constant_p);
17587     }
17588   else if (token->type == CPP_OPEN_PAREN)
17589     {
17590       VEC(tree,gc) *vec;
17591       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17592                                                      /*cast_p=*/false,
17593                                                      /*allow_expansion_p=*/true,
17594                                                      non_constant_p);
17595       if (vec == NULL)
17596         return error_mark_node;
17597       init = build_tree_list_vec (vec);
17598       release_tree_vector (vec);
17599     }
17600   else if (token->type == CPP_OPEN_BRACE)
17601     {
17602       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17603       init = cp_parser_braced_list (parser, non_constant_p);
17604       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17605     }
17606   else
17607     {
17608       /* Anything else is an error.  */
17609       cp_parser_error (parser, "expected initializer");
17610       init = error_mark_node;
17611     }
17612
17613   return init;
17614 }
17615
17616 /* Parse an initializer-clause.
17617
17618    initializer-clause:
17619      assignment-expression
17620      braced-init-list
17621
17622    Returns an expression representing the initializer.
17623
17624    If the `assignment-expression' production is used the value
17625    returned is simply a representation for the expression.
17626
17627    Otherwise, calls cp_parser_braced_list.  */
17628
17629 static tree
17630 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17631 {
17632   tree initializer;
17633
17634   /* Assume the expression is constant.  */
17635   *non_constant_p = false;
17636
17637   /* If it is not a `{', then we are looking at an
17638      assignment-expression.  */
17639   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17640     {
17641       initializer
17642         = cp_parser_constant_expression (parser,
17643                                         /*allow_non_constant_p=*/true,
17644                                         non_constant_p);
17645     }
17646   else
17647     initializer = cp_parser_braced_list (parser, non_constant_p);
17648
17649   return initializer;
17650 }
17651
17652 /* Parse a brace-enclosed initializer list.
17653
17654    braced-init-list:
17655      { initializer-list , [opt] }
17656      { }
17657
17658    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17659    the elements of the initializer-list (or NULL, if the last
17660    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17661    NULL_TREE.  There is no way to detect whether or not the optional
17662    trailing `,' was provided.  NON_CONSTANT_P is as for
17663    cp_parser_initializer.  */     
17664
17665 static tree
17666 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17667 {
17668   tree initializer;
17669
17670   /* Consume the `{' token.  */
17671   cp_lexer_consume_token (parser->lexer);
17672   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17673   initializer = make_node (CONSTRUCTOR);
17674   /* If it's not a `}', then there is a non-trivial initializer.  */
17675   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17676     {
17677       /* Parse the initializer list.  */
17678       CONSTRUCTOR_ELTS (initializer)
17679         = cp_parser_initializer_list (parser, non_constant_p);
17680       /* A trailing `,' token is allowed.  */
17681       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17682         cp_lexer_consume_token (parser->lexer);
17683     }
17684   /* Now, there should be a trailing `}'.  */
17685   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17686   TREE_TYPE (initializer) = init_list_type_node;
17687   return initializer;
17688 }
17689
17690 /* Parse an initializer-list.
17691
17692    initializer-list:
17693      initializer-clause ... [opt]
17694      initializer-list , initializer-clause ... [opt]
17695
17696    GNU Extension:
17697
17698    initializer-list:
17699      designation initializer-clause ...[opt]
17700      initializer-list , designation initializer-clause ...[opt]
17701
17702    designation:
17703      . identifier =
17704      identifier :
17705      [ constant-expression ] =
17706
17707    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17708    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17709    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17710    as for cp_parser_initializer.  */
17711
17712 static VEC(constructor_elt,gc) *
17713 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17714 {
17715   VEC(constructor_elt,gc) *v = NULL;
17716
17717   /* Assume all of the expressions are constant.  */
17718   *non_constant_p = false;
17719
17720   /* Parse the rest of the list.  */
17721   while (true)
17722     {
17723       cp_token *token;
17724       tree designator;
17725       tree initializer;
17726       bool clause_non_constant_p;
17727
17728       /* If the next token is an identifier and the following one is a
17729          colon, we are looking at the GNU designated-initializer
17730          syntax.  */
17731       if (cp_parser_allow_gnu_extensions_p (parser)
17732           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17733           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17734         {
17735           /* Warn the user that they are using an extension.  */
17736           pedwarn (input_location, OPT_pedantic, 
17737                    "ISO C++ does not allow designated initializers");
17738           /* Consume the identifier.  */
17739           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17740           /* Consume the `:'.  */
17741           cp_lexer_consume_token (parser->lexer);
17742         }
17743       /* Also handle the C99 syntax, '. id ='.  */
17744       else if (cp_parser_allow_gnu_extensions_p (parser)
17745                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17746                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17747                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17748         {
17749           /* Warn the user that they are using an extension.  */
17750           pedwarn (input_location, OPT_pedantic,
17751                    "ISO C++ does not allow C99 designated initializers");
17752           /* Consume the `.'.  */
17753           cp_lexer_consume_token (parser->lexer);
17754           /* Consume the identifier.  */
17755           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17756           /* Consume the `='.  */
17757           cp_lexer_consume_token (parser->lexer);
17758         }
17759       /* Also handle C99 array designators, '[ const ] ='.  */
17760       else if (cp_parser_allow_gnu_extensions_p (parser)
17761                && !c_dialect_objc ()
17762                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17763         {
17764           /* In C++11, [ could start a lambda-introducer.  */
17765           cp_parser_parse_tentatively (parser);
17766           cp_lexer_consume_token (parser->lexer);
17767           designator = cp_parser_constant_expression (parser, false, NULL);
17768           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17769           cp_parser_require (parser, CPP_EQ, RT_EQ);
17770           if (!cp_parser_parse_definitely (parser))
17771             designator = NULL_TREE;
17772         }
17773       else
17774         designator = NULL_TREE;
17775
17776       /* Parse the initializer.  */
17777       initializer = cp_parser_initializer_clause (parser,
17778                                                   &clause_non_constant_p);
17779       /* If any clause is non-constant, so is the entire initializer.  */
17780       if (clause_non_constant_p)
17781         *non_constant_p = true;
17782
17783       /* If we have an ellipsis, this is an initializer pack
17784          expansion.  */
17785       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17786         {
17787           /* Consume the `...'.  */
17788           cp_lexer_consume_token (parser->lexer);
17789
17790           /* Turn the initializer into an initializer expansion.  */
17791           initializer = make_pack_expansion (initializer);
17792         }
17793
17794       /* Add it to the vector.  */
17795       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17796
17797       /* If the next token is not a comma, we have reached the end of
17798          the list.  */
17799       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17800         break;
17801
17802       /* Peek at the next token.  */
17803       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17804       /* If the next token is a `}', then we're still done.  An
17805          initializer-clause can have a trailing `,' after the
17806          initializer-list and before the closing `}'.  */
17807       if (token->type == CPP_CLOSE_BRACE)
17808         break;
17809
17810       /* Consume the `,' token.  */
17811       cp_lexer_consume_token (parser->lexer);
17812     }
17813
17814   return v;
17815 }
17816
17817 /* Classes [gram.class] */
17818
17819 /* Parse a class-name.
17820
17821    class-name:
17822      identifier
17823      template-id
17824
17825    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17826    to indicate that names looked up in dependent types should be
17827    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17828    keyword has been used to indicate that the name that appears next
17829    is a template.  TAG_TYPE indicates the explicit tag given before
17830    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17831    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17832    is the class being defined in a class-head.
17833
17834    Returns the TYPE_DECL representing the class.  */
17835
17836 static tree
17837 cp_parser_class_name (cp_parser *parser,
17838                       bool typename_keyword_p,
17839                       bool template_keyword_p,
17840                       enum tag_types tag_type,
17841                       bool check_dependency_p,
17842                       bool class_head_p,
17843                       bool is_declaration)
17844 {
17845   tree decl;
17846   tree scope;
17847   bool typename_p;
17848   cp_token *token;
17849   tree identifier = NULL_TREE;
17850
17851   /* All class-names start with an identifier.  */
17852   token = cp_lexer_peek_token (parser->lexer);
17853   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17854     {
17855       cp_parser_error (parser, "expected class-name");
17856       return error_mark_node;
17857     }
17858
17859   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17860      to a template-id, so we save it here.  */
17861   scope = parser->scope;
17862   if (scope == error_mark_node)
17863     return error_mark_node;
17864
17865   /* Any name names a type if we're following the `typename' keyword
17866      in a qualified name where the enclosing scope is type-dependent.  */
17867   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17868                 && dependent_type_p (scope));
17869   /* Handle the common case (an identifier, but not a template-id)
17870      efficiently.  */
17871   if (token->type == CPP_NAME
17872       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17873     {
17874       cp_token *identifier_token;
17875       bool ambiguous_p;
17876
17877       /* Look for the identifier.  */
17878       identifier_token = cp_lexer_peek_token (parser->lexer);
17879       ambiguous_p = identifier_token->ambiguous_p;
17880       identifier = cp_parser_identifier (parser);
17881       /* If the next token isn't an identifier, we are certainly not
17882          looking at a class-name.  */
17883       if (identifier == error_mark_node)
17884         decl = error_mark_node;
17885       /* If we know this is a type-name, there's no need to look it
17886          up.  */
17887       else if (typename_p)
17888         decl = identifier;
17889       else
17890         {
17891           tree ambiguous_decls;
17892           /* If we already know that this lookup is ambiguous, then
17893              we've already issued an error message; there's no reason
17894              to check again.  */
17895           if (ambiguous_p)
17896             {
17897               cp_parser_simulate_error (parser);
17898               return error_mark_node;
17899             }
17900           /* If the next token is a `::', then the name must be a type
17901              name.
17902
17903              [basic.lookup.qual]
17904
17905              During the lookup for a name preceding the :: scope
17906              resolution operator, object, function, and enumerator
17907              names are ignored.  */
17908           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17909             tag_type = typename_type;
17910           /* Look up the name.  */
17911           decl = cp_parser_lookup_name (parser, identifier,
17912                                         tag_type,
17913                                         /*is_template=*/false,
17914                                         /*is_namespace=*/false,
17915                                         check_dependency_p,
17916                                         &ambiguous_decls,
17917                                         identifier_token->location);
17918           if (ambiguous_decls)
17919             {
17920               if (cp_parser_parsing_tentatively (parser))
17921                 cp_parser_simulate_error (parser);
17922               return error_mark_node;
17923             }
17924         }
17925     }
17926   else
17927     {
17928       /* Try a template-id.  */
17929       decl = cp_parser_template_id (parser, template_keyword_p,
17930                                     check_dependency_p,
17931                                     is_declaration);
17932       if (decl == error_mark_node)
17933         return error_mark_node;
17934     }
17935
17936   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17937
17938   /* If this is a typename, create a TYPENAME_TYPE.  */
17939   if (typename_p && decl != error_mark_node)
17940     {
17941       decl = make_typename_type (scope, decl, typename_type,
17942                                  /*complain=*/tf_error);
17943       if (decl != error_mark_node)
17944         decl = TYPE_NAME (decl);
17945     }
17946
17947   /* Check to see that it is really the name of a class.  */
17948   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17949       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17950       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17951     /* Situations like this:
17952
17953          template <typename T> struct A {
17954            typename T::template X<int>::I i;
17955          };
17956
17957        are problematic.  Is `T::template X<int>' a class-name?  The
17958        standard does not seem to be definitive, but there is no other
17959        valid interpretation of the following `::'.  Therefore, those
17960        names are considered class-names.  */
17961     {
17962       decl = make_typename_type (scope, decl, tag_type, tf_error);
17963       if (decl != error_mark_node)
17964         decl = TYPE_NAME (decl);
17965     }
17966   else if (TREE_CODE (decl) != TYPE_DECL
17967            || TREE_TYPE (decl) == error_mark_node
17968            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17969            /* In Objective-C 2.0, a classname followed by '.' starts a
17970               dot-syntax expression, and it's not a type-name.  */
17971            || (c_dialect_objc ()
17972                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17973                && objc_is_class_name (decl)))
17974     decl = error_mark_node;
17975
17976   if (decl == error_mark_node)
17977     cp_parser_error (parser, "expected class-name");
17978   else if (identifier && !parser->scope)
17979     maybe_note_name_used_in_class (identifier, decl);
17980
17981   return decl;
17982 }
17983
17984 /* Parse a class-specifier.
17985
17986    class-specifier:
17987      class-head { member-specification [opt] }
17988
17989    Returns the TREE_TYPE representing the class.  */
17990
17991 static tree
17992 cp_parser_class_specifier_1 (cp_parser* parser)
17993 {
17994   tree type;
17995   tree attributes = NULL_TREE;
17996   bool nested_name_specifier_p;
17997   unsigned saved_num_template_parameter_lists;
17998   bool saved_in_function_body;
17999   unsigned char in_statement;
18000   bool in_switch_statement_p;
18001   bool saved_in_unbraced_linkage_specification_p;
18002   tree old_scope = NULL_TREE;
18003   tree scope = NULL_TREE;
18004   tree bases;
18005   cp_token *closing_brace;
18006
18007   push_deferring_access_checks (dk_no_deferred);
18008
18009   /* Parse the class-head.  */
18010   type = cp_parser_class_head (parser,
18011                                &nested_name_specifier_p,
18012                                &attributes,
18013                                &bases);
18014   /* If the class-head was a semantic disaster, skip the entire body
18015      of the class.  */
18016   if (!type)
18017     {
18018       cp_parser_skip_to_end_of_block_or_statement (parser);
18019       pop_deferring_access_checks ();
18020       return error_mark_node;
18021     }
18022
18023   /* Look for the `{'.  */
18024   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
18025     {
18026       pop_deferring_access_checks ();
18027       return error_mark_node;
18028     }
18029
18030   /* Process the base classes. If they're invalid, skip the 
18031      entire class body.  */
18032   if (!xref_basetypes (type, bases))
18033     {
18034       /* Consuming the closing brace yields better error messages
18035          later on.  */
18036       if (cp_parser_skip_to_closing_brace (parser))
18037         cp_lexer_consume_token (parser->lexer);
18038       pop_deferring_access_checks ();
18039       return error_mark_node;
18040     }
18041
18042   /* Issue an error message if type-definitions are forbidden here.  */
18043   cp_parser_check_type_definition (parser);
18044   /* Remember that we are defining one more class.  */
18045   ++parser->num_classes_being_defined;
18046   /* Inside the class, surrounding template-parameter-lists do not
18047      apply.  */
18048   saved_num_template_parameter_lists
18049     = parser->num_template_parameter_lists;
18050   parser->num_template_parameter_lists = 0;
18051   /* We are not in a function body.  */
18052   saved_in_function_body = parser->in_function_body;
18053   parser->in_function_body = false;
18054   /* Or in a loop.  */
18055   in_statement = parser->in_statement;
18056   parser->in_statement = 0;
18057   /* Or in a switch.  */
18058   in_switch_statement_p = parser->in_switch_statement_p;
18059   parser->in_switch_statement_p = false;
18060   /* We are not immediately inside an extern "lang" block.  */
18061   saved_in_unbraced_linkage_specification_p
18062     = parser->in_unbraced_linkage_specification_p;
18063   parser->in_unbraced_linkage_specification_p = false;
18064
18065   /* Start the class.  */
18066   if (nested_name_specifier_p)
18067     {
18068       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
18069       old_scope = push_inner_scope (scope);
18070     }
18071   type = begin_class_definition (type, attributes);
18072
18073   if (type == error_mark_node)
18074     /* If the type is erroneous, skip the entire body of the class.  */
18075     cp_parser_skip_to_closing_brace (parser);
18076   else
18077     /* Parse the member-specification.  */
18078     cp_parser_member_specification_opt (parser);
18079
18080   /* Look for the trailing `}'.  */
18081   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
18082   /* Look for trailing attributes to apply to this class.  */
18083   if (cp_parser_allow_gnu_extensions_p (parser))
18084     attributes = cp_parser_attributes_opt (parser);
18085   if (type != error_mark_node)
18086     type = finish_struct (type, attributes);
18087   if (nested_name_specifier_p)
18088     pop_inner_scope (old_scope, scope);
18089
18090   /* We've finished a type definition.  Check for the common syntax
18091      error of forgetting a semicolon after the definition.  We need to
18092      be careful, as we can't just check for not-a-semicolon and be done
18093      with it; the user might have typed:
18094
18095      class X { } c = ...;
18096      class X { } *p = ...;
18097
18098      and so forth.  Instead, enumerate all the possible tokens that
18099      might follow this production; if we don't see one of them, then
18100      complain and silently insert the semicolon.  */
18101   {
18102     cp_token *token = cp_lexer_peek_token (parser->lexer);
18103     bool want_semicolon = true;
18104
18105     switch (token->type)
18106       {
18107       case CPP_NAME:
18108       case CPP_SEMICOLON:
18109       case CPP_MULT:
18110       case CPP_AND:
18111       case CPP_OPEN_PAREN:
18112       case CPP_CLOSE_PAREN:
18113       case CPP_COMMA:
18114         want_semicolon = false;
18115         break;
18116
18117         /* While it's legal for type qualifiers and storage class
18118            specifiers to follow type definitions in the grammar, only
18119            compiler testsuites contain code like that.  Assume that if
18120            we see such code, then what we're really seeing is a case
18121            like:
18122
18123            class X { }
18124            const <type> var = ...;
18125
18126            or
18127
18128            class Y { }
18129            static <type> func (...) ...
18130
18131            i.e. the qualifier or specifier applies to the next
18132            declaration.  To do so, however, we need to look ahead one
18133            more token to see if *that* token is a type specifier.
18134
18135            This code could be improved to handle:
18136
18137            class Z { }
18138            static const <type> var = ...;  */
18139       case CPP_KEYWORD:
18140         if (keyword_is_decl_specifier (token->keyword))
18141           {
18142             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18143
18144             /* Handling user-defined types here would be nice, but very
18145                tricky.  */
18146             want_semicolon
18147               = (lookahead->type == CPP_KEYWORD
18148                  && keyword_begins_type_specifier (lookahead->keyword));
18149           }
18150         break;
18151       default:
18152         break;
18153       }
18154
18155     /* If we don't have a type, then something is very wrong and we
18156        shouldn't try to do anything clever.  Likewise for not seeing the
18157        closing brace.  */
18158     if (closing_brace && TYPE_P (type) && want_semicolon)
18159       {
18160         cp_token_position prev
18161           = cp_lexer_previous_token_position (parser->lexer);
18162         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18163         location_t loc = prev_token->location;
18164
18165         if (CLASSTYPE_DECLARED_CLASS (type))
18166           error_at (loc, "expected %<;%> after class definition");
18167         else if (TREE_CODE (type) == RECORD_TYPE)
18168           error_at (loc, "expected %<;%> after struct definition");
18169         else if (TREE_CODE (type) == UNION_TYPE)
18170           error_at (loc, "expected %<;%> after union definition");
18171         else
18172           gcc_unreachable ();
18173
18174         /* Unget one token and smash it to look as though we encountered
18175            a semicolon in the input stream.  */
18176         cp_lexer_set_token_position (parser->lexer, prev);
18177         token = cp_lexer_peek_token (parser->lexer);
18178         token->type = CPP_SEMICOLON;
18179         token->keyword = RID_MAX;
18180       }
18181   }
18182
18183   /* If this class is not itself within the scope of another class,
18184      then we need to parse the bodies of all of the queued function
18185      definitions.  Note that the queued functions defined in a class
18186      are not always processed immediately following the
18187      class-specifier for that class.  Consider:
18188
18189        struct A {
18190          struct B { void f() { sizeof (A); } };
18191        };
18192
18193      If `f' were processed before the processing of `A' were
18194      completed, there would be no way to compute the size of `A'.
18195      Note that the nesting we are interested in here is lexical --
18196      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18197      for:
18198
18199        struct A { struct B; };
18200        struct A::B { void f() { } };
18201
18202      there is no need to delay the parsing of `A::B::f'.  */
18203   if (--parser->num_classes_being_defined == 0)
18204     {
18205       tree decl;
18206       tree class_type = NULL_TREE;
18207       tree pushed_scope = NULL_TREE;
18208       unsigned ix;
18209       cp_default_arg_entry *e;
18210       tree save_ccp, save_ccr;
18211
18212       /* In a first pass, parse default arguments to the functions.
18213          Then, in a second pass, parse the bodies of the functions.
18214          This two-phased approach handles cases like:
18215
18216             struct S {
18217               void f() { g(); }
18218               void g(int i = 3);
18219             };
18220
18221          */
18222       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18223                         ix, e)
18224         {
18225           decl = e->decl;
18226           /* If there are default arguments that have not yet been processed,
18227              take care of them now.  */
18228           if (class_type != e->class_type)
18229             {
18230               if (pushed_scope)
18231                 pop_scope (pushed_scope);
18232               class_type = e->class_type;
18233               pushed_scope = push_scope (class_type);
18234             }
18235           /* Make sure that any template parameters are in scope.  */
18236           maybe_begin_member_template_processing (decl);
18237           /* Parse the default argument expressions.  */
18238           cp_parser_late_parsing_default_args (parser, decl);
18239           /* Remove any template parameters from the symbol table.  */
18240           maybe_end_member_template_processing ();
18241         }
18242       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18243       /* Now parse any NSDMIs.  */
18244       save_ccp = current_class_ptr;
18245       save_ccr = current_class_ref;
18246       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18247         {
18248           if (class_type != DECL_CONTEXT (decl))
18249             {
18250               if (pushed_scope)
18251                 pop_scope (pushed_scope);
18252               class_type = DECL_CONTEXT (decl);
18253               pushed_scope = push_scope (class_type);
18254             }
18255           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18256           cp_parser_late_parsing_nsdmi (parser, decl);
18257         }
18258       VEC_truncate (tree, unparsed_nsdmis, 0);
18259       current_class_ptr = save_ccp;
18260       current_class_ref = save_ccr;
18261       if (pushed_scope)
18262         pop_scope (pushed_scope);
18263       /* Now parse the body of the functions.  */
18264       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18265         cp_parser_late_parsing_for_member (parser, decl);
18266       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18267     }
18268
18269   /* Put back any saved access checks.  */
18270   pop_deferring_access_checks ();
18271
18272   /* Restore saved state.  */
18273   parser->in_switch_statement_p = in_switch_statement_p;
18274   parser->in_statement = in_statement;
18275   parser->in_function_body = saved_in_function_body;
18276   parser->num_template_parameter_lists
18277     = saved_num_template_parameter_lists;
18278   parser->in_unbraced_linkage_specification_p
18279     = saved_in_unbraced_linkage_specification_p;
18280
18281   return type;
18282 }
18283
18284 static tree
18285 cp_parser_class_specifier (cp_parser* parser)
18286 {
18287   tree ret;
18288   timevar_push (TV_PARSE_STRUCT);
18289   ret = cp_parser_class_specifier_1 (parser);
18290   timevar_pop (TV_PARSE_STRUCT);
18291   return ret;
18292 }
18293
18294 /* Parse a class-head.
18295
18296    class-head:
18297      class-key identifier [opt] base-clause [opt]
18298      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18299      class-key nested-name-specifier [opt] template-id
18300        base-clause [opt]
18301
18302    class-virt-specifier:
18303      final
18304
18305    GNU Extensions:
18306      class-key attributes identifier [opt] base-clause [opt]
18307      class-key attributes nested-name-specifier identifier base-clause [opt]
18308      class-key attributes nested-name-specifier [opt] template-id
18309        base-clause [opt]
18310
18311    Upon return BASES is initialized to the list of base classes (or
18312    NULL, if there are none) in the same form returned by
18313    cp_parser_base_clause.
18314
18315    Returns the TYPE of the indicated class.  Sets
18316    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18317    involving a nested-name-specifier was used, and FALSE otherwise.
18318
18319    Returns error_mark_node if this is not a class-head.
18320
18321    Returns NULL_TREE if the class-head is syntactically valid, but
18322    semantically invalid in a way that means we should skip the entire
18323    body of the class.  */
18324
18325 static tree
18326 cp_parser_class_head (cp_parser* parser,
18327                       bool* nested_name_specifier_p,
18328                       tree *attributes_p,
18329                       tree *bases)
18330 {
18331   tree nested_name_specifier;
18332   enum tag_types class_key;
18333   tree id = NULL_TREE;
18334   tree type = NULL_TREE;
18335   tree attributes;
18336   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18337   bool template_id_p = false;
18338   bool qualified_p = false;
18339   bool invalid_nested_name_p = false;
18340   bool invalid_explicit_specialization_p = false;
18341   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18342   tree pushed_scope = NULL_TREE;
18343   unsigned num_templates;
18344   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18345   /* Assume no nested-name-specifier will be present.  */
18346   *nested_name_specifier_p = false;
18347   /* Assume no template parameter lists will be used in defining the
18348      type.  */
18349   num_templates = 0;
18350   parser->colon_corrects_to_scope_p = false;
18351
18352   *bases = NULL_TREE;
18353
18354   /* Look for the class-key.  */
18355   class_key = cp_parser_class_key (parser);
18356   if (class_key == none_type)
18357     return error_mark_node;
18358
18359   /* Parse the attributes.  */
18360   attributes = cp_parser_attributes_opt (parser);
18361
18362   /* If the next token is `::', that is invalid -- but sometimes
18363      people do try to write:
18364
18365        struct ::S {};
18366
18367      Handle this gracefully by accepting the extra qualifier, and then
18368      issuing an error about it later if this really is a
18369      class-head.  If it turns out just to be an elaborated type
18370      specifier, remain silent.  */
18371   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18372     qualified_p = true;
18373
18374   push_deferring_access_checks (dk_no_check);
18375
18376   /* Determine the name of the class.  Begin by looking for an
18377      optional nested-name-specifier.  */
18378   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18379   nested_name_specifier
18380     = cp_parser_nested_name_specifier_opt (parser,
18381                                            /*typename_keyword_p=*/false,
18382                                            /*check_dependency_p=*/false,
18383                                            /*type_p=*/false,
18384                                            /*is_declaration=*/false);
18385   /* If there was a nested-name-specifier, then there *must* be an
18386      identifier.  */
18387   if (nested_name_specifier)
18388     {
18389       type_start_token = cp_lexer_peek_token (parser->lexer);
18390       /* Although the grammar says `identifier', it really means
18391          `class-name' or `template-name'.  You are only allowed to
18392          define a class that has already been declared with this
18393          syntax.
18394
18395          The proposed resolution for Core Issue 180 says that wherever
18396          you see `class T::X' you should treat `X' as a type-name.
18397
18398          It is OK to define an inaccessible class; for example:
18399
18400            class A { class B; };
18401            class A::B {};
18402
18403          We do not know if we will see a class-name, or a
18404          template-name.  We look for a class-name first, in case the
18405          class-name is a template-id; if we looked for the
18406          template-name first we would stop after the template-name.  */
18407       cp_parser_parse_tentatively (parser);
18408       type = cp_parser_class_name (parser,
18409                                    /*typename_keyword_p=*/false,
18410                                    /*template_keyword_p=*/false,
18411                                    class_type,
18412                                    /*check_dependency_p=*/false,
18413                                    /*class_head_p=*/true,
18414                                    /*is_declaration=*/false);
18415       /* If that didn't work, ignore the nested-name-specifier.  */
18416       if (!cp_parser_parse_definitely (parser))
18417         {
18418           invalid_nested_name_p = true;
18419           type_start_token = cp_lexer_peek_token (parser->lexer);
18420           id = cp_parser_identifier (parser);
18421           if (id == error_mark_node)
18422             id = NULL_TREE;
18423         }
18424       /* If we could not find a corresponding TYPE, treat this
18425          declaration like an unqualified declaration.  */
18426       if (type == error_mark_node)
18427         nested_name_specifier = NULL_TREE;
18428       /* Otherwise, count the number of templates used in TYPE and its
18429          containing scopes.  */
18430       else
18431         {
18432           tree scope;
18433
18434           for (scope = TREE_TYPE (type);
18435                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18436                scope = (TYPE_P (scope)
18437                         ? TYPE_CONTEXT (scope)
18438                         : DECL_CONTEXT (scope)))
18439             if (TYPE_P (scope)
18440                 && CLASS_TYPE_P (scope)
18441                 && CLASSTYPE_TEMPLATE_INFO (scope)
18442                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18443                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18444               ++num_templates;
18445         }
18446     }
18447   /* Otherwise, the identifier is optional.  */
18448   else
18449     {
18450       /* We don't know whether what comes next is a template-id,
18451          an identifier, or nothing at all.  */
18452       cp_parser_parse_tentatively (parser);
18453       /* Check for a template-id.  */
18454       type_start_token = cp_lexer_peek_token (parser->lexer);
18455       id = cp_parser_template_id (parser,
18456                                   /*template_keyword_p=*/false,
18457                                   /*check_dependency_p=*/true,
18458                                   /*is_declaration=*/true);
18459       /* If that didn't work, it could still be an identifier.  */
18460       if (!cp_parser_parse_definitely (parser))
18461         {
18462           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18463             {
18464               type_start_token = cp_lexer_peek_token (parser->lexer);
18465               id = cp_parser_identifier (parser);
18466             }
18467           else
18468             id = NULL_TREE;
18469         }
18470       else
18471         {
18472           template_id_p = true;
18473           ++num_templates;
18474         }
18475     }
18476
18477   pop_deferring_access_checks ();
18478
18479   if (id)
18480     {
18481       cp_parser_check_for_invalid_template_id (parser, id,
18482                                                type_start_token->location);
18483     }
18484   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18485
18486   /* If it's not a `:' or a `{' then we can't really be looking at a
18487      class-head, since a class-head only appears as part of a
18488      class-specifier.  We have to detect this situation before calling
18489      xref_tag, since that has irreversible side-effects.  */
18490   if (!cp_parser_next_token_starts_class_definition_p (parser))
18491     {
18492       cp_parser_error (parser, "expected %<{%> or %<:%>");
18493       type = error_mark_node;
18494       goto out;
18495     }
18496
18497   /* At this point, we're going ahead with the class-specifier, even
18498      if some other problem occurs.  */
18499   cp_parser_commit_to_tentative_parse (parser);
18500   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18501     {
18502       cp_parser_error (parser,
18503                        "cannot specify %<override%> for a class");
18504       type = error_mark_node;
18505       goto out;
18506     }
18507   /* Issue the error about the overly-qualified name now.  */
18508   if (qualified_p)
18509     {
18510       cp_parser_error (parser,
18511                        "global qualification of class name is invalid");
18512       type = error_mark_node;
18513       goto out;
18514     }
18515   else if (invalid_nested_name_p)
18516     {
18517       cp_parser_error (parser,
18518                        "qualified name does not name a class");
18519       type = error_mark_node;
18520       goto out;
18521     }
18522   else if (nested_name_specifier)
18523     {
18524       tree scope;
18525
18526       /* Reject typedef-names in class heads.  */
18527       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18528         {
18529           error_at (type_start_token->location,
18530                     "invalid class name in declaration of %qD",
18531                     type);
18532           type = NULL_TREE;
18533           goto done;
18534         }
18535
18536       /* Figure out in what scope the declaration is being placed.  */
18537       scope = current_scope ();
18538       /* If that scope does not contain the scope in which the
18539          class was originally declared, the program is invalid.  */
18540       if (scope && !is_ancestor (scope, nested_name_specifier))
18541         {
18542           if (at_namespace_scope_p ())
18543             error_at (type_start_token->location,
18544                       "declaration of %qD in namespace %qD which does not "
18545                       "enclose %qD",
18546                       type, scope, nested_name_specifier);
18547           else
18548             error_at (type_start_token->location,
18549                       "declaration of %qD in %qD which does not enclose %qD",
18550                       type, scope, nested_name_specifier);
18551           type = NULL_TREE;
18552           goto done;
18553         }
18554       /* [dcl.meaning]
18555
18556          A declarator-id shall not be qualified except for the
18557          definition of a ... nested class outside of its class
18558          ... [or] the definition or explicit instantiation of a
18559          class member of a namespace outside of its namespace.  */
18560       if (scope == nested_name_specifier)
18561         {
18562           permerror (nested_name_specifier_token_start->location,
18563                      "extra qualification not allowed");
18564           nested_name_specifier = NULL_TREE;
18565           num_templates = 0;
18566         }
18567     }
18568   /* An explicit-specialization must be preceded by "template <>".  If
18569      it is not, try to recover gracefully.  */
18570   if (at_namespace_scope_p ()
18571       && parser->num_template_parameter_lists == 0
18572       && template_id_p)
18573     {
18574       error_at (type_start_token->location,
18575                 "an explicit specialization must be preceded by %<template <>%>");
18576       invalid_explicit_specialization_p = true;
18577       /* Take the same action that would have been taken by
18578          cp_parser_explicit_specialization.  */
18579       ++parser->num_template_parameter_lists;
18580       begin_specialization ();
18581     }
18582   /* There must be no "return" statements between this point and the
18583      end of this function; set "type "to the correct return value and
18584      use "goto done;" to return.  */
18585   /* Make sure that the right number of template parameters were
18586      present.  */
18587   if (!cp_parser_check_template_parameters (parser, num_templates,
18588                                             type_start_token->location,
18589                                             /*declarator=*/NULL))
18590     {
18591       /* If something went wrong, there is no point in even trying to
18592          process the class-definition.  */
18593       type = NULL_TREE;
18594       goto done;
18595     }
18596
18597   /* Look up the type.  */
18598   if (template_id_p)
18599     {
18600       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18601           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18602               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18603         {
18604           error_at (type_start_token->location,
18605                     "function template %qD redeclared as a class template", id);
18606           type = error_mark_node;
18607         }
18608       else
18609         {
18610           type = TREE_TYPE (id);
18611           type = maybe_process_partial_specialization (type);
18612         }
18613       if (nested_name_specifier)
18614         pushed_scope = push_scope (nested_name_specifier);
18615     }
18616   else if (nested_name_specifier)
18617     {
18618       tree class_type;
18619
18620       /* Given:
18621
18622             template <typename T> struct S { struct T };
18623             template <typename T> struct S<T>::T { };
18624
18625          we will get a TYPENAME_TYPE when processing the definition of
18626          `S::T'.  We need to resolve it to the actual type before we
18627          try to define it.  */
18628       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18629         {
18630           class_type = resolve_typename_type (TREE_TYPE (type),
18631                                               /*only_current_p=*/false);
18632           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18633             type = TYPE_NAME (class_type);
18634           else
18635             {
18636               cp_parser_error (parser, "could not resolve typename type");
18637               type = error_mark_node;
18638             }
18639         }
18640
18641       if (maybe_process_partial_specialization (TREE_TYPE (type))
18642           == error_mark_node)
18643         {
18644           type = NULL_TREE;
18645           goto done;
18646         }
18647
18648       class_type = current_class_type;
18649       /* Enter the scope indicated by the nested-name-specifier.  */
18650       pushed_scope = push_scope (nested_name_specifier);
18651       /* Get the canonical version of this type.  */
18652       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18653       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18654           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18655         {
18656           type = push_template_decl (type);
18657           if (type == error_mark_node)
18658             {
18659               type = NULL_TREE;
18660               goto done;
18661             }
18662         }
18663
18664       type = TREE_TYPE (type);
18665       *nested_name_specifier_p = true;
18666     }
18667   else      /* The name is not a nested name.  */
18668     {
18669       /* If the class was unnamed, create a dummy name.  */
18670       if (!id)
18671         id = make_anon_name ();
18672       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18673                        parser->num_template_parameter_lists);
18674     }
18675
18676   /* Indicate whether this class was declared as a `class' or as a
18677      `struct'.  */
18678   if (TREE_CODE (type) == RECORD_TYPE)
18679     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18680   cp_parser_check_class_key (class_key, type);
18681
18682   /* If this type was already complete, and we see another definition,
18683      that's an error.  */
18684   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18685     {
18686       error_at (type_start_token->location, "redefinition of %q#T",
18687                 type);
18688       error_at (type_start_token->location, "previous definition of %q+#T",
18689                 type);
18690       type = NULL_TREE;
18691       goto done;
18692     }
18693   else if (type == error_mark_node)
18694     type = NULL_TREE;
18695
18696   /* We will have entered the scope containing the class; the names of
18697      base classes should be looked up in that context.  For example:
18698
18699        struct A { struct B {}; struct C; };
18700        struct A::C : B {};
18701
18702      is valid.  */
18703
18704   /* Get the list of base-classes, if there is one.  */
18705   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18706     *bases = cp_parser_base_clause (parser);
18707
18708  done:
18709   /* Leave the scope given by the nested-name-specifier.  We will
18710      enter the class scope itself while processing the members.  */
18711   if (pushed_scope)
18712     pop_scope (pushed_scope);
18713
18714   if (invalid_explicit_specialization_p)
18715     {
18716       end_specialization ();
18717       --parser->num_template_parameter_lists;
18718     }
18719
18720   if (type)
18721     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18722   *attributes_p = attributes;
18723   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18724     CLASSTYPE_FINAL (type) = 1;
18725  out:
18726   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18727   return type;
18728 }
18729
18730 /* Parse a class-key.
18731
18732    class-key:
18733      class
18734      struct
18735      union
18736
18737    Returns the kind of class-key specified, or none_type to indicate
18738    error.  */
18739
18740 static enum tag_types
18741 cp_parser_class_key (cp_parser* parser)
18742 {
18743   cp_token *token;
18744   enum tag_types tag_type;
18745
18746   /* Look for the class-key.  */
18747   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18748   if (!token)
18749     return none_type;
18750
18751   /* Check to see if the TOKEN is a class-key.  */
18752   tag_type = cp_parser_token_is_class_key (token);
18753   if (!tag_type)
18754     cp_parser_error (parser, "expected class-key");
18755   return tag_type;
18756 }
18757
18758 /* Parse an (optional) member-specification.
18759
18760    member-specification:
18761      member-declaration member-specification [opt]
18762      access-specifier : member-specification [opt]  */
18763
18764 static void
18765 cp_parser_member_specification_opt (cp_parser* parser)
18766 {
18767   while (true)
18768     {
18769       cp_token *token;
18770       enum rid keyword;
18771
18772       /* Peek at the next token.  */
18773       token = cp_lexer_peek_token (parser->lexer);
18774       /* If it's a `}', or EOF then we've seen all the members.  */
18775       if (token->type == CPP_CLOSE_BRACE
18776           || token->type == CPP_EOF
18777           || token->type == CPP_PRAGMA_EOL)
18778         break;
18779
18780       /* See if this token is a keyword.  */
18781       keyword = token->keyword;
18782       switch (keyword)
18783         {
18784         case RID_PUBLIC:
18785         case RID_PROTECTED:
18786         case RID_PRIVATE:
18787           /* Consume the access-specifier.  */
18788           cp_lexer_consume_token (parser->lexer);
18789           /* Remember which access-specifier is active.  */
18790           current_access_specifier = token->u.value;
18791           /* Look for the `:'.  */
18792           cp_parser_require (parser, CPP_COLON, RT_COLON);
18793           break;
18794
18795         default:
18796           /* Accept #pragmas at class scope.  */
18797           if (token->type == CPP_PRAGMA)
18798             {
18799               cp_parser_pragma (parser, pragma_external);
18800               break;
18801             }
18802
18803           /* Otherwise, the next construction must be a
18804              member-declaration.  */
18805           cp_parser_member_declaration (parser);
18806         }
18807     }
18808 }
18809
18810 /* Parse a member-declaration.
18811
18812    member-declaration:
18813      decl-specifier-seq [opt] member-declarator-list [opt] ;
18814      function-definition ; [opt]
18815      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18816      using-declaration
18817      template-declaration
18818      alias-declaration
18819
18820    member-declarator-list:
18821      member-declarator
18822      member-declarator-list , member-declarator
18823
18824    member-declarator:
18825      declarator pure-specifier [opt]
18826      declarator constant-initializer [opt]
18827      identifier [opt] : constant-expression
18828
18829    GNU Extensions:
18830
18831    member-declaration:
18832      __extension__ member-declaration
18833
18834    member-declarator:
18835      declarator attributes [opt] pure-specifier [opt]
18836      declarator attributes [opt] constant-initializer [opt]
18837      identifier [opt] attributes [opt] : constant-expression  
18838
18839    C++0x Extensions:
18840
18841    member-declaration:
18842      static_assert-declaration  */
18843
18844 static void
18845 cp_parser_member_declaration (cp_parser* parser)
18846 {
18847   cp_decl_specifier_seq decl_specifiers;
18848   tree prefix_attributes;
18849   tree decl;
18850   int declares_class_or_enum;
18851   bool friend_p;
18852   cp_token *token = NULL;
18853   cp_token *decl_spec_token_start = NULL;
18854   cp_token *initializer_token_start = NULL;
18855   int saved_pedantic;
18856   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18857
18858   /* Check for the `__extension__' keyword.  */
18859   if (cp_parser_extension_opt (parser, &saved_pedantic))
18860     {
18861       /* Recurse.  */
18862       cp_parser_member_declaration (parser);
18863       /* Restore the old value of the PEDANTIC flag.  */
18864       pedantic = saved_pedantic;
18865
18866       return;
18867     }
18868
18869   /* Check for a template-declaration.  */
18870   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18871     {
18872       /* An explicit specialization here is an error condition, and we
18873          expect the specialization handler to detect and report this.  */
18874       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18875           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18876         cp_parser_explicit_specialization (parser);
18877       else
18878         cp_parser_template_declaration (parser, /*member_p=*/true);
18879
18880       return;
18881     }
18882
18883   /* Check for a using-declaration.  */
18884   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18885     {
18886       if (cxx_dialect < cxx0x)
18887         {
18888           /* Parse the using-declaration.  */
18889           cp_parser_using_declaration (parser,
18890                                        /*access_declaration_p=*/false);
18891           return;
18892         }
18893       else
18894         {
18895           tree decl;
18896           cp_parser_parse_tentatively (parser);
18897           decl = cp_parser_alias_declaration (parser);
18898           if (cp_parser_parse_definitely (parser))
18899             finish_member_declaration (decl);
18900           else
18901             cp_parser_using_declaration (parser,
18902                                          /*access_declaration_p=*/false);
18903           return;
18904         }
18905     }
18906
18907   /* Check for @defs.  */
18908   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18909     {
18910       tree ivar, member;
18911       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18912       ivar = ivar_chains;
18913       while (ivar)
18914         {
18915           member = ivar;
18916           ivar = TREE_CHAIN (member);
18917           TREE_CHAIN (member) = NULL_TREE;
18918           finish_member_declaration (member);
18919         }
18920       return;
18921     }
18922
18923   /* If the next token is `static_assert' we have a static assertion.  */
18924   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18925     {
18926       cp_parser_static_assert (parser, /*member_p=*/true);
18927       return;
18928     }
18929
18930   parser->colon_corrects_to_scope_p = false;
18931
18932   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18933       goto out;
18934
18935   /* Parse the decl-specifier-seq.  */
18936   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18937   cp_parser_decl_specifier_seq (parser,
18938                                 CP_PARSER_FLAGS_OPTIONAL,
18939                                 &decl_specifiers,
18940                                 &declares_class_or_enum);
18941   prefix_attributes = decl_specifiers.attributes;
18942   decl_specifiers.attributes = NULL_TREE;
18943   /* Check for an invalid type-name.  */
18944   if (!decl_specifiers.any_type_specifiers_p
18945       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18946     goto out;
18947   /* If there is no declarator, then the decl-specifier-seq should
18948      specify a type.  */
18949   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18950     {
18951       /* If there was no decl-specifier-seq, and the next token is a
18952          `;', then we have something like:
18953
18954            struct S { ; };
18955
18956          [class.mem]
18957
18958          Each member-declaration shall declare at least one member
18959          name of the class.  */
18960       if (!decl_specifiers.any_specifiers_p)
18961         {
18962           cp_token *token = cp_lexer_peek_token (parser->lexer);
18963           if (!in_system_header_at (token->location))
18964             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18965         }
18966       else
18967         {
18968           tree type;
18969
18970           /* See if this declaration is a friend.  */
18971           friend_p = cp_parser_friend_p (&decl_specifiers);
18972           /* If there were decl-specifiers, check to see if there was
18973              a class-declaration.  */
18974           type = check_tag_decl (&decl_specifiers);
18975           /* Nested classes have already been added to the class, but
18976              a `friend' needs to be explicitly registered.  */
18977           if (friend_p)
18978             {
18979               /* If the `friend' keyword was present, the friend must
18980                  be introduced with a class-key.  */
18981                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18982                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18983                           "in C++03 a class-key must be used "
18984                           "when declaring a friend");
18985                /* In this case:
18986
18987                     template <typename T> struct A {
18988                       friend struct A<T>::B;
18989                     };
18990
18991                   A<T>::B will be represented by a TYPENAME_TYPE, and
18992                   therefore not recognized by check_tag_decl.  */
18993                if (!type)
18994                  {
18995                    type = decl_specifiers.type;
18996                    if (type && TREE_CODE (type) == TYPE_DECL)
18997                      type = TREE_TYPE (type);
18998                  }
18999                if (!type || !TYPE_P (type))
19000                  error_at (decl_spec_token_start->location,
19001                            "friend declaration does not name a class or "
19002                            "function");
19003                else
19004                  make_friend_class (current_class_type, type,
19005                                     /*complain=*/true);
19006             }
19007           /* If there is no TYPE, an error message will already have
19008              been issued.  */
19009           else if (!type || type == error_mark_node)
19010             ;
19011           /* An anonymous aggregate has to be handled specially; such
19012              a declaration really declares a data member (with a
19013              particular type), as opposed to a nested class.  */
19014           else if (ANON_AGGR_TYPE_P (type))
19015             {
19016               /* Remove constructors and such from TYPE, now that we
19017                  know it is an anonymous aggregate.  */
19018               fixup_anonymous_aggr (type);
19019               /* And make the corresponding data member.  */
19020               decl = build_decl (decl_spec_token_start->location,
19021                                  FIELD_DECL, NULL_TREE, type);
19022               /* Add it to the class.  */
19023               finish_member_declaration (decl);
19024             }
19025           else
19026             cp_parser_check_access_in_redeclaration
19027                                               (TYPE_NAME (type),
19028                                                decl_spec_token_start->location);
19029         }
19030     }
19031   else
19032     {
19033       bool assume_semicolon = false;
19034
19035       /* See if these declarations will be friends.  */
19036       friend_p = cp_parser_friend_p (&decl_specifiers);
19037
19038       /* Keep going until we hit the `;' at the end of the
19039          declaration.  */
19040       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
19041         {
19042           tree attributes = NULL_TREE;
19043           tree first_attribute;
19044
19045           /* Peek at the next token.  */
19046           token = cp_lexer_peek_token (parser->lexer);
19047
19048           /* Check for a bitfield declaration.  */
19049           if (token->type == CPP_COLON
19050               || (token->type == CPP_NAME
19051                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
19052                   == CPP_COLON))
19053             {
19054               tree identifier;
19055               tree width;
19056
19057               /* Get the name of the bitfield.  Note that we cannot just
19058                  check TOKEN here because it may have been invalidated by
19059                  the call to cp_lexer_peek_nth_token above.  */
19060               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
19061                 identifier = cp_parser_identifier (parser);
19062               else
19063                 identifier = NULL_TREE;
19064
19065               /* Consume the `:' token.  */
19066               cp_lexer_consume_token (parser->lexer);
19067               /* Get the width of the bitfield.  */
19068               width
19069                 = cp_parser_constant_expression (parser,
19070                                                  /*allow_non_constant=*/false,
19071                                                  NULL);
19072
19073               /* Look for attributes that apply to the bitfield.  */
19074               attributes = cp_parser_attributes_opt (parser);
19075               /* Remember which attributes are prefix attributes and
19076                  which are not.  */
19077               first_attribute = attributes;
19078               /* Combine the attributes.  */
19079               attributes = chainon (prefix_attributes, attributes);
19080
19081               /* Create the bitfield declaration.  */
19082               decl = grokbitfield (identifier
19083                                    ? make_id_declarator (NULL_TREE,
19084                                                          identifier,
19085                                                          sfk_none)
19086                                    : NULL,
19087                                    &decl_specifiers,
19088                                    width,
19089                                    attributes);
19090             }
19091           else
19092             {
19093               cp_declarator *declarator;
19094               tree initializer;
19095               tree asm_specification;
19096               int ctor_dtor_or_conv_p;
19097
19098               /* Parse the declarator.  */
19099               declarator
19100                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
19101                                         &ctor_dtor_or_conv_p,
19102                                         /*parenthesized_p=*/NULL,
19103                                         /*member_p=*/true);
19104
19105               /* If something went wrong parsing the declarator, make sure
19106                  that we at least consume some tokens.  */
19107               if (declarator == cp_error_declarator)
19108                 {
19109                   /* Skip to the end of the statement.  */
19110                   cp_parser_skip_to_end_of_statement (parser);
19111                   /* If the next token is not a semicolon, that is
19112                      probably because we just skipped over the body of
19113                      a function.  So, we consume a semicolon if
19114                      present, but do not issue an error message if it
19115                      is not present.  */
19116                   if (cp_lexer_next_token_is (parser->lexer,
19117                                               CPP_SEMICOLON))
19118                     cp_lexer_consume_token (parser->lexer);
19119                   goto out;
19120                 }
19121
19122               if (declares_class_or_enum & 2)
19123                 cp_parser_check_for_definition_in_return_type
19124                                             (declarator, decl_specifiers.type,
19125                                              decl_specifiers.type_location);
19126
19127               /* Look for an asm-specification.  */
19128               asm_specification = cp_parser_asm_specification_opt (parser);
19129               /* Look for attributes that apply to the declaration.  */
19130               attributes = cp_parser_attributes_opt (parser);
19131               /* Remember which attributes are prefix attributes and
19132                  which are not.  */
19133               first_attribute = attributes;
19134               /* Combine the attributes.  */
19135               attributes = chainon (prefix_attributes, attributes);
19136
19137               /* If it's an `=', then we have a constant-initializer or a
19138                  pure-specifier.  It is not correct to parse the
19139                  initializer before registering the member declaration
19140                  since the member declaration should be in scope while
19141                  its initializer is processed.  However, the rest of the
19142                  front end does not yet provide an interface that allows
19143                  us to handle this correctly.  */
19144               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19145                 {
19146                   /* In [class.mem]:
19147
19148                      A pure-specifier shall be used only in the declaration of
19149                      a virtual function.
19150
19151                      A member-declarator can contain a constant-initializer
19152                      only if it declares a static member of integral or
19153                      enumeration type.
19154
19155                      Therefore, if the DECLARATOR is for a function, we look
19156                      for a pure-specifier; otherwise, we look for a
19157                      constant-initializer.  When we call `grokfield', it will
19158                      perform more stringent semantics checks.  */
19159                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19160                   if (function_declarator_p (declarator)
19161                       || (decl_specifiers.type
19162                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19163                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19164                               == FUNCTION_TYPE)))
19165                     initializer = cp_parser_pure_specifier (parser);
19166                   else if (decl_specifiers.storage_class != sc_static)
19167                     initializer = cp_parser_save_nsdmi (parser);
19168                   else if (cxx_dialect >= cxx0x)
19169                     {
19170                       bool nonconst;
19171                       /* Don't require a constant rvalue in C++11, since we
19172                          might want a reference constant.  We'll enforce
19173                          constancy later.  */
19174                       cp_lexer_consume_token (parser->lexer);
19175                       /* Parse the initializer.  */
19176                       initializer = cp_parser_initializer_clause (parser,
19177                                                                   &nonconst);
19178                     }
19179                   else
19180                     /* Parse the initializer.  */
19181                     initializer = cp_parser_constant_initializer (parser);
19182                 }
19183               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19184                        && !function_declarator_p (declarator))
19185                 {
19186                   bool x;
19187                   if (decl_specifiers.storage_class != sc_static)
19188                     initializer = cp_parser_save_nsdmi (parser);
19189                   else
19190                     initializer = cp_parser_initializer (parser, &x, &x);
19191                 }
19192               /* Otherwise, there is no initializer.  */
19193               else
19194                 initializer = NULL_TREE;
19195
19196               /* See if we are probably looking at a function
19197                  definition.  We are certainly not looking at a
19198                  member-declarator.  Calling `grokfield' has
19199                  side-effects, so we must not do it unless we are sure
19200                  that we are looking at a member-declarator.  */
19201               if (cp_parser_token_starts_function_definition_p
19202                   (cp_lexer_peek_token (parser->lexer)))
19203                 {
19204                   /* The grammar does not allow a pure-specifier to be
19205                      used when a member function is defined.  (It is
19206                      possible that this fact is an oversight in the
19207                      standard, since a pure function may be defined
19208                      outside of the class-specifier.  */
19209                   if (initializer)
19210                     error_at (initializer_token_start->location,
19211                               "pure-specifier on function-definition");
19212                   decl = cp_parser_save_member_function_body (parser,
19213                                                               &decl_specifiers,
19214                                                               declarator,
19215                                                               attributes);
19216                   /* If the member was not a friend, declare it here.  */
19217                   if (!friend_p)
19218                     finish_member_declaration (decl);
19219                   /* Peek at the next token.  */
19220                   token = cp_lexer_peek_token (parser->lexer);
19221                   /* If the next token is a semicolon, consume it.  */
19222                   if (token->type == CPP_SEMICOLON)
19223                     cp_lexer_consume_token (parser->lexer);
19224                   goto out;
19225                 }
19226               else
19227                 if (declarator->kind == cdk_function)
19228                   declarator->id_loc = token->location;
19229                 /* Create the declaration.  */
19230                 decl = grokfield (declarator, &decl_specifiers,
19231                                   initializer, /*init_const_expr_p=*/true,
19232                                   asm_specification,
19233                                   attributes);
19234             }
19235
19236           /* Reset PREFIX_ATTRIBUTES.  */
19237           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19238             attributes = TREE_CHAIN (attributes);
19239           if (attributes)
19240             TREE_CHAIN (attributes) = NULL_TREE;
19241
19242           /* If there is any qualification still in effect, clear it
19243              now; we will be starting fresh with the next declarator.  */
19244           parser->scope = NULL_TREE;
19245           parser->qualifying_scope = NULL_TREE;
19246           parser->object_scope = NULL_TREE;
19247           /* If it's a `,', then there are more declarators.  */
19248           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19249             cp_lexer_consume_token (parser->lexer);
19250           /* If the next token isn't a `;', then we have a parse error.  */
19251           else if (cp_lexer_next_token_is_not (parser->lexer,
19252                                                CPP_SEMICOLON))
19253             {
19254               /* The next token might be a ways away from where the
19255                  actual semicolon is missing.  Find the previous token
19256                  and use that for our error position.  */
19257               cp_token *token = cp_lexer_previous_token (parser->lexer);
19258               error_at (token->location,
19259                         "expected %<;%> at end of member declaration");
19260
19261               /* Assume that the user meant to provide a semicolon.  If
19262                  we were to cp_parser_skip_to_end_of_statement, we might
19263                  skip to a semicolon inside a member function definition
19264                  and issue nonsensical error messages.  */
19265               assume_semicolon = true;
19266             }
19267
19268           if (decl)
19269             {
19270               /* Add DECL to the list of members.  */
19271               if (!friend_p)
19272                 finish_member_declaration (decl);
19273
19274               if (TREE_CODE (decl) == FUNCTION_DECL)
19275                 cp_parser_save_default_args (parser, decl);
19276               else if (TREE_CODE (decl) == FIELD_DECL
19277                        && !DECL_C_BIT_FIELD (decl)
19278                        && DECL_INITIAL (decl))
19279                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19280                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19281             }
19282
19283           if (assume_semicolon)
19284             goto out;
19285         }
19286     }
19287
19288   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19289  out:
19290   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19291 }
19292
19293 /* Parse a pure-specifier.
19294
19295    pure-specifier:
19296      = 0
19297
19298    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19299    Otherwise, ERROR_MARK_NODE is returned.  */
19300
19301 static tree
19302 cp_parser_pure_specifier (cp_parser* parser)
19303 {
19304   cp_token *token;
19305
19306   /* Look for the `=' token.  */
19307   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19308     return error_mark_node;
19309   /* Look for the `0' token.  */
19310   token = cp_lexer_peek_token (parser->lexer);
19311
19312   if (token->type == CPP_EOF
19313       || token->type == CPP_PRAGMA_EOL)
19314     return error_mark_node;
19315
19316   cp_lexer_consume_token (parser->lexer);
19317
19318   /* Accept = default or = delete in c++0x mode.  */
19319   if (token->keyword == RID_DEFAULT
19320       || token->keyword == RID_DELETE)
19321     {
19322       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19323       return token->u.value;
19324     }
19325
19326   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19327   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19328     {
19329       cp_parser_error (parser,
19330                        "invalid pure specifier (only %<= 0%> is allowed)");
19331       cp_parser_skip_to_end_of_statement (parser);
19332       return error_mark_node;
19333     }
19334   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19335     {
19336       error_at (token->location, "templates may not be %<virtual%>");
19337       return error_mark_node;
19338     }
19339
19340   return integer_zero_node;
19341 }
19342
19343 /* Parse a constant-initializer.
19344
19345    constant-initializer:
19346      = constant-expression
19347
19348    Returns a representation of the constant-expression.  */
19349
19350 static tree
19351 cp_parser_constant_initializer (cp_parser* parser)
19352 {
19353   /* Look for the `=' token.  */
19354   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19355     return error_mark_node;
19356
19357   /* It is invalid to write:
19358
19359        struct S { static const int i = { 7 }; };
19360
19361      */
19362   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19363     {
19364       cp_parser_error (parser,
19365                        "a brace-enclosed initializer is not allowed here");
19366       /* Consume the opening brace.  */
19367       cp_lexer_consume_token (parser->lexer);
19368       /* Skip the initializer.  */
19369       cp_parser_skip_to_closing_brace (parser);
19370       /* Look for the trailing `}'.  */
19371       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19372
19373       return error_mark_node;
19374     }
19375
19376   return cp_parser_constant_expression (parser,
19377                                         /*allow_non_constant=*/false,
19378                                         NULL);
19379 }
19380
19381 /* Derived classes [gram.class.derived] */
19382
19383 /* Parse a base-clause.
19384
19385    base-clause:
19386      : base-specifier-list
19387
19388    base-specifier-list:
19389      base-specifier ... [opt]
19390      base-specifier-list , base-specifier ... [opt]
19391
19392    Returns a TREE_LIST representing the base-classes, in the order in
19393    which they were declared.  The representation of each node is as
19394    described by cp_parser_base_specifier.
19395
19396    In the case that no bases are specified, this function will return
19397    NULL_TREE, not ERROR_MARK_NODE.  */
19398
19399 static tree
19400 cp_parser_base_clause (cp_parser* parser)
19401 {
19402   tree bases = NULL_TREE;
19403
19404   /* Look for the `:' that begins the list.  */
19405   cp_parser_require (parser, CPP_COLON, RT_COLON);
19406
19407   /* Scan the base-specifier-list.  */
19408   while (true)
19409     {
19410       cp_token *token;
19411       tree base;
19412       bool pack_expansion_p = false;
19413
19414       /* Look for the base-specifier.  */
19415       base = cp_parser_base_specifier (parser);
19416       /* Look for the (optional) ellipsis. */
19417       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19418         {
19419           /* Consume the `...'. */
19420           cp_lexer_consume_token (parser->lexer);
19421
19422           pack_expansion_p = true;
19423         }
19424
19425       /* Add BASE to the front of the list.  */
19426       if (base && base != error_mark_node)
19427         {
19428           if (pack_expansion_p)
19429             /* Make this a pack expansion type. */
19430             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19431
19432           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19433             {
19434               TREE_CHAIN (base) = bases;
19435               bases = base;
19436             }
19437         }
19438       /* Peek at the next token.  */
19439       token = cp_lexer_peek_token (parser->lexer);
19440       /* If it's not a comma, then the list is complete.  */
19441       if (token->type != CPP_COMMA)
19442         break;
19443       /* Consume the `,'.  */
19444       cp_lexer_consume_token (parser->lexer);
19445     }
19446
19447   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19448      base class had a qualified name.  However, the next name that
19449      appears is certainly not qualified.  */
19450   parser->scope = NULL_TREE;
19451   parser->qualifying_scope = NULL_TREE;
19452   parser->object_scope = NULL_TREE;
19453
19454   return nreverse (bases);
19455 }
19456
19457 /* Parse a base-specifier.
19458
19459    base-specifier:
19460      :: [opt] nested-name-specifier [opt] class-name
19461      virtual access-specifier [opt] :: [opt] nested-name-specifier
19462        [opt] class-name
19463      access-specifier virtual [opt] :: [opt] nested-name-specifier
19464        [opt] class-name
19465
19466    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19467    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19468    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19469    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19470
19471 static tree
19472 cp_parser_base_specifier (cp_parser* parser)
19473 {
19474   cp_token *token;
19475   bool done = false;
19476   bool virtual_p = false;
19477   bool duplicate_virtual_error_issued_p = false;
19478   bool duplicate_access_error_issued_p = false;
19479   bool class_scope_p, template_p;
19480   tree access = access_default_node;
19481   tree type;
19482
19483   /* Process the optional `virtual' and `access-specifier'.  */
19484   while (!done)
19485     {
19486       /* Peek at the next token.  */
19487       token = cp_lexer_peek_token (parser->lexer);
19488       /* Process `virtual'.  */
19489       switch (token->keyword)
19490         {
19491         case RID_VIRTUAL:
19492           /* If `virtual' appears more than once, issue an error.  */
19493           if (virtual_p && !duplicate_virtual_error_issued_p)
19494             {
19495               cp_parser_error (parser,
19496                                "%<virtual%> specified more than once in base-specified");
19497               duplicate_virtual_error_issued_p = true;
19498             }
19499
19500           virtual_p = true;
19501
19502           /* Consume the `virtual' token.  */
19503           cp_lexer_consume_token (parser->lexer);
19504
19505           break;
19506
19507         case RID_PUBLIC:
19508         case RID_PROTECTED:
19509         case RID_PRIVATE:
19510           /* If more than one access specifier appears, issue an
19511              error.  */
19512           if (access != access_default_node
19513               && !duplicate_access_error_issued_p)
19514             {
19515               cp_parser_error (parser,
19516                                "more than one access specifier in base-specified");
19517               duplicate_access_error_issued_p = true;
19518             }
19519
19520           access = ridpointers[(int) token->keyword];
19521
19522           /* Consume the access-specifier.  */
19523           cp_lexer_consume_token (parser->lexer);
19524
19525           break;
19526
19527         default:
19528           done = true;
19529           break;
19530         }
19531     }
19532   /* It is not uncommon to see programs mechanically, erroneously, use
19533      the 'typename' keyword to denote (dependent) qualified types
19534      as base classes.  */
19535   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19536     {
19537       token = cp_lexer_peek_token (parser->lexer);
19538       if (!processing_template_decl)
19539         error_at (token->location,
19540                   "keyword %<typename%> not allowed outside of templates");
19541       else
19542         error_at (token->location,
19543                   "keyword %<typename%> not allowed in this context "
19544                   "(the base class is implicitly a type)");
19545       cp_lexer_consume_token (parser->lexer);
19546     }
19547
19548   /* Look for the optional `::' operator.  */
19549   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19550   /* Look for the nested-name-specifier.  The simplest way to
19551      implement:
19552
19553        [temp.res]
19554
19555        The keyword `typename' is not permitted in a base-specifier or
19556        mem-initializer; in these contexts a qualified name that
19557        depends on a template-parameter is implicitly assumed to be a
19558        type name.
19559
19560      is to pretend that we have seen the `typename' keyword at this
19561      point.  */
19562   cp_parser_nested_name_specifier_opt (parser,
19563                                        /*typename_keyword_p=*/true,
19564                                        /*check_dependency_p=*/true,
19565                                        typename_type,
19566                                        /*is_declaration=*/true);
19567   /* If the base class is given by a qualified name, assume that names
19568      we see are type names or templates, as appropriate.  */
19569   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19570   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19571
19572   if (!parser->scope
19573       && cp_lexer_next_token_is_decltype (parser->lexer))
19574     /* DR 950 allows decltype as a base-specifier.  */
19575     type = cp_parser_decltype (parser);
19576   else
19577     {
19578       /* Otherwise, look for the class-name.  */
19579       type = cp_parser_class_name (parser,
19580                                    class_scope_p,
19581                                    template_p,
19582                                    typename_type,
19583                                    /*check_dependency_p=*/true,
19584                                    /*class_head_p=*/false,
19585                                    /*is_declaration=*/true);
19586       type = TREE_TYPE (type);
19587     }
19588
19589   if (type == error_mark_node)
19590     return error_mark_node;
19591
19592   return finish_base_specifier (type, access, virtual_p);
19593 }
19594
19595 /* Exception handling [gram.exception] */
19596
19597 /* Parse an (optional) noexcept-specification.
19598
19599    noexcept-specification:
19600      noexcept ( constant-expression ) [opt]
19601
19602    If no noexcept-specification is present, returns NULL_TREE.
19603    Otherwise, if REQUIRE_CONSTEXPR is false, then either parse and return any
19604    expression if parentheses follow noexcept, or return BOOLEAN_TRUE_NODE if
19605    there are no parentheses.  CONSUMED_EXPR will be set accordingly.
19606    Otherwise, returns a noexcept specification unless RETURN_COND is true,
19607    in which case a boolean condition is returned instead.  */
19608
19609 static tree
19610 cp_parser_noexcept_specification_opt (cp_parser* parser,
19611                                       bool require_constexpr,
19612                                       bool* consumed_expr,
19613                                       bool return_cond)
19614 {
19615   cp_token *token;
19616   const char *saved_message;
19617
19618   /* Peek at the next token.  */
19619   token = cp_lexer_peek_token (parser->lexer);
19620
19621   /* Is it a noexcept-specification?  */
19622   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19623     {
19624       tree expr;
19625       cp_lexer_consume_token (parser->lexer);
19626
19627       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19628         {
19629           cp_lexer_consume_token (parser->lexer);
19630
19631           if (require_constexpr)
19632             {
19633               /* Types may not be defined in an exception-specification.  */
19634               saved_message = parser->type_definition_forbidden_message;
19635               parser->type_definition_forbidden_message
19636               = G_("types may not be defined in an exception-specification");
19637
19638               expr = cp_parser_constant_expression (parser, false, NULL);
19639
19640               /* Restore the saved message.  */
19641               parser->type_definition_forbidden_message = saved_message;
19642             }
19643           else
19644             {
19645               expr = cp_parser_expression (parser, false, NULL);
19646               *consumed_expr = true;
19647             }
19648
19649           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19650         }
19651       else
19652         {
19653           expr = boolean_true_node;
19654           if (!require_constexpr)
19655             *consumed_expr = false;
19656         }
19657
19658       /* We cannot build a noexcept-spec right away because this will check
19659          that expr is a constexpr.  */
19660       if (!return_cond)
19661         return build_noexcept_spec (expr, tf_warning_or_error);
19662       else
19663         return expr;
19664     }
19665   else
19666     return NULL_TREE;
19667 }
19668
19669 /* Parse an (optional) exception-specification.
19670
19671    exception-specification:
19672      throw ( type-id-list [opt] )
19673
19674    Returns a TREE_LIST representing the exception-specification.  The
19675    TREE_VALUE of each node is a type.  */
19676
19677 static tree
19678 cp_parser_exception_specification_opt (cp_parser* parser)
19679 {
19680   cp_token *token;
19681   tree type_id_list;
19682   const char *saved_message;
19683
19684   /* Peek at the next token.  */
19685   token = cp_lexer_peek_token (parser->lexer);
19686
19687   /* Is it a noexcept-specification?  */
19688   type_id_list = cp_parser_noexcept_specification_opt(parser, true, NULL,
19689                                                       false);
19690   if (type_id_list != NULL_TREE)
19691     return type_id_list;
19692
19693   /* If it's not `throw', then there's no exception-specification.  */
19694   if (!cp_parser_is_keyword (token, RID_THROW))
19695     return NULL_TREE;
19696
19697 #if 0
19698   /* Enable this once a lot of code has transitioned to noexcept?  */
19699   if (cxx_dialect == cxx0x && !in_system_header)
19700     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19701              "deprecated in C++0x; use %<noexcept%> instead");
19702 #endif
19703
19704   /* Consume the `throw'.  */
19705   cp_lexer_consume_token (parser->lexer);
19706
19707   /* Look for the `('.  */
19708   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19709
19710   /* Peek at the next token.  */
19711   token = cp_lexer_peek_token (parser->lexer);
19712   /* If it's not a `)', then there is a type-id-list.  */
19713   if (token->type != CPP_CLOSE_PAREN)
19714     {
19715       /* Types may not be defined in an exception-specification.  */
19716       saved_message = parser->type_definition_forbidden_message;
19717       parser->type_definition_forbidden_message
19718         = G_("types may not be defined in an exception-specification");
19719       /* Parse the type-id-list.  */
19720       type_id_list = cp_parser_type_id_list (parser);
19721       /* Restore the saved message.  */
19722       parser->type_definition_forbidden_message = saved_message;
19723     }
19724   else
19725     type_id_list = empty_except_spec;
19726
19727   /* Look for the `)'.  */
19728   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19729
19730   return type_id_list;
19731 }
19732
19733 /* Parse an (optional) type-id-list.
19734
19735    type-id-list:
19736      type-id ... [opt]
19737      type-id-list , type-id ... [opt]
19738
19739    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19740    in the order that the types were presented.  */
19741
19742 static tree
19743 cp_parser_type_id_list (cp_parser* parser)
19744 {
19745   tree types = NULL_TREE;
19746
19747   while (true)
19748     {
19749       cp_token *token;
19750       tree type;
19751
19752       /* Get the next type-id.  */
19753       type = cp_parser_type_id (parser);
19754       /* Parse the optional ellipsis. */
19755       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19756         {
19757           /* Consume the `...'. */
19758           cp_lexer_consume_token (parser->lexer);
19759
19760           /* Turn the type into a pack expansion expression. */
19761           type = make_pack_expansion (type);
19762         }
19763       /* Add it to the list.  */
19764       types = add_exception_specifier (types, type, /*complain=*/1);
19765       /* Peek at the next token.  */
19766       token = cp_lexer_peek_token (parser->lexer);
19767       /* If it is not a `,', we are done.  */
19768       if (token->type != CPP_COMMA)
19769         break;
19770       /* Consume the `,'.  */
19771       cp_lexer_consume_token (parser->lexer);
19772     }
19773
19774   return nreverse (types);
19775 }
19776
19777 /* Parse a try-block.
19778
19779    try-block:
19780      try compound-statement handler-seq  */
19781
19782 static tree
19783 cp_parser_try_block (cp_parser* parser)
19784 {
19785   tree try_block;
19786
19787   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19788   try_block = begin_try_block ();
19789   cp_parser_compound_statement (parser, NULL, true, false);
19790   finish_try_block (try_block);
19791   cp_parser_handler_seq (parser);
19792   finish_handler_sequence (try_block);
19793
19794   return try_block;
19795 }
19796
19797 /* Parse a function-try-block.
19798
19799    function-try-block:
19800      try ctor-initializer [opt] function-body handler-seq  */
19801
19802 static bool
19803 cp_parser_function_try_block (cp_parser* parser)
19804 {
19805   tree compound_stmt;
19806   tree try_block;
19807   bool ctor_initializer_p;
19808
19809   /* Look for the `try' keyword.  */
19810   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19811     return false;
19812   /* Let the rest of the front end know where we are.  */
19813   try_block = begin_function_try_block (&compound_stmt);
19814   /* Parse the function-body.  */
19815   ctor_initializer_p
19816     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19817   /* We're done with the `try' part.  */
19818   finish_function_try_block (try_block);
19819   /* Parse the handlers.  */
19820   cp_parser_handler_seq (parser);
19821   /* We're done with the handlers.  */
19822   finish_function_handler_sequence (try_block, compound_stmt);
19823
19824   return ctor_initializer_p;
19825 }
19826
19827 /* Parse a handler-seq.
19828
19829    handler-seq:
19830      handler handler-seq [opt]  */
19831
19832 static void
19833 cp_parser_handler_seq (cp_parser* parser)
19834 {
19835   while (true)
19836     {
19837       cp_token *token;
19838
19839       /* Parse the handler.  */
19840       cp_parser_handler (parser);
19841       /* Peek at the next token.  */
19842       token = cp_lexer_peek_token (parser->lexer);
19843       /* If it's not `catch' then there are no more handlers.  */
19844       if (!cp_parser_is_keyword (token, RID_CATCH))
19845         break;
19846     }
19847 }
19848
19849 /* Parse a handler.
19850
19851    handler:
19852      catch ( exception-declaration ) compound-statement  */
19853
19854 static void
19855 cp_parser_handler (cp_parser* parser)
19856 {
19857   tree handler;
19858   tree declaration;
19859
19860   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19861   handler = begin_handler ();
19862   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19863   declaration = cp_parser_exception_declaration (parser);
19864   finish_handler_parms (declaration, handler);
19865   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19866   cp_parser_compound_statement (parser, NULL, false, false);
19867   finish_handler (handler);
19868 }
19869
19870 /* Parse an exception-declaration.
19871
19872    exception-declaration:
19873      type-specifier-seq declarator
19874      type-specifier-seq abstract-declarator
19875      type-specifier-seq
19876      ...
19877
19878    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19879    ellipsis variant is used.  */
19880
19881 static tree
19882 cp_parser_exception_declaration (cp_parser* parser)
19883 {
19884   cp_decl_specifier_seq type_specifiers;
19885   cp_declarator *declarator;
19886   const char *saved_message;
19887
19888   /* If it's an ellipsis, it's easy to handle.  */
19889   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19890     {
19891       /* Consume the `...' token.  */
19892       cp_lexer_consume_token (parser->lexer);
19893       return NULL_TREE;
19894     }
19895
19896   /* Types may not be defined in exception-declarations.  */
19897   saved_message = parser->type_definition_forbidden_message;
19898   parser->type_definition_forbidden_message
19899     = G_("types may not be defined in exception-declarations");
19900
19901   /* Parse the type-specifier-seq.  */
19902   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19903                                 /*is_trailing_return=*/false,
19904                                 &type_specifiers);
19905   /* If it's a `)', then there is no declarator.  */
19906   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19907     declarator = NULL;
19908   else
19909     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19910                                        /*ctor_dtor_or_conv_p=*/NULL,
19911                                        /*parenthesized_p=*/NULL,
19912                                        /*member_p=*/false);
19913
19914   /* Restore the saved message.  */
19915   parser->type_definition_forbidden_message = saved_message;
19916
19917   if (!type_specifiers.any_specifiers_p)
19918     return error_mark_node;
19919
19920   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19921 }
19922
19923 /* Parse a throw-expression.
19924
19925    throw-expression:
19926      throw assignment-expression [opt]
19927
19928    Returns a THROW_EXPR representing the throw-expression.  */
19929
19930 static tree
19931 cp_parser_throw_expression (cp_parser* parser)
19932 {
19933   tree expression;
19934   cp_token* token;
19935
19936   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19937   token = cp_lexer_peek_token (parser->lexer);
19938   /* Figure out whether or not there is an assignment-expression
19939      following the "throw" keyword.  */
19940   if (token->type == CPP_COMMA
19941       || token->type == CPP_SEMICOLON
19942       || token->type == CPP_CLOSE_PAREN
19943       || token->type == CPP_CLOSE_SQUARE
19944       || token->type == CPP_CLOSE_BRACE
19945       || token->type == CPP_COLON)
19946     expression = NULL_TREE;
19947   else
19948     expression = cp_parser_assignment_expression (parser,
19949                                                   /*cast_p=*/false, NULL);
19950
19951   return build_throw (expression);
19952 }
19953
19954 /* GNU Extensions */
19955
19956 /* Parse an (optional) asm-specification.
19957
19958    asm-specification:
19959      asm ( string-literal )
19960
19961    If the asm-specification is present, returns a STRING_CST
19962    corresponding to the string-literal.  Otherwise, returns
19963    NULL_TREE.  */
19964
19965 static tree
19966 cp_parser_asm_specification_opt (cp_parser* parser)
19967 {
19968   cp_token *token;
19969   tree asm_specification;
19970
19971   /* Peek at the next token.  */
19972   token = cp_lexer_peek_token (parser->lexer);
19973   /* If the next token isn't the `asm' keyword, then there's no
19974      asm-specification.  */
19975   if (!cp_parser_is_keyword (token, RID_ASM))
19976     return NULL_TREE;
19977
19978   /* Consume the `asm' token.  */
19979   cp_lexer_consume_token (parser->lexer);
19980   /* Look for the `('.  */
19981   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19982
19983   /* Look for the string-literal.  */
19984   asm_specification = cp_parser_string_literal (parser, false, false);
19985
19986   /* Look for the `)'.  */
19987   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19988
19989   return asm_specification;
19990 }
19991
19992 /* Parse an asm-operand-list.
19993
19994    asm-operand-list:
19995      asm-operand
19996      asm-operand-list , asm-operand
19997
19998    asm-operand:
19999      string-literal ( expression )
20000      [ string-literal ] string-literal ( expression )
20001
20002    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
20003    each node is the expression.  The TREE_PURPOSE is itself a
20004    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
20005    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
20006    is a STRING_CST for the string literal before the parenthesis. Returns
20007    ERROR_MARK_NODE if any of the operands are invalid.  */
20008
20009 static tree
20010 cp_parser_asm_operand_list (cp_parser* parser)
20011 {
20012   tree asm_operands = NULL_TREE;
20013   bool invalid_operands = false;
20014
20015   while (true)
20016     {
20017       tree string_literal;
20018       tree expression;
20019       tree name;
20020
20021       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
20022         {
20023           /* Consume the `[' token.  */
20024           cp_lexer_consume_token (parser->lexer);
20025           /* Read the operand name.  */
20026           name = cp_parser_identifier (parser);
20027           if (name != error_mark_node)
20028             name = build_string (IDENTIFIER_LENGTH (name),
20029                                  IDENTIFIER_POINTER (name));
20030           /* Look for the closing `]'.  */
20031           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
20032         }
20033       else
20034         name = NULL_TREE;
20035       /* Look for the string-literal.  */
20036       string_literal = cp_parser_string_literal (parser, false, false);
20037
20038       /* Look for the `('.  */
20039       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20040       /* Parse the expression.  */
20041       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
20042       /* Look for the `)'.  */
20043       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20044
20045       if (name == error_mark_node 
20046           || string_literal == error_mark_node 
20047           || expression == error_mark_node)
20048         invalid_operands = true;
20049
20050       /* Add this operand to the list.  */
20051       asm_operands = tree_cons (build_tree_list (name, string_literal),
20052                                 expression,
20053                                 asm_operands);
20054       /* If the next token is not a `,', there are no more
20055          operands.  */
20056       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20057         break;
20058       /* Consume the `,'.  */
20059       cp_lexer_consume_token (parser->lexer);
20060     }
20061
20062   return invalid_operands ? error_mark_node : nreverse (asm_operands);
20063 }
20064
20065 /* Parse an asm-clobber-list.
20066
20067    asm-clobber-list:
20068      string-literal
20069      asm-clobber-list , string-literal
20070
20071    Returns a TREE_LIST, indicating the clobbers in the order that they
20072    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
20073
20074 static tree
20075 cp_parser_asm_clobber_list (cp_parser* parser)
20076 {
20077   tree clobbers = NULL_TREE;
20078
20079   while (true)
20080     {
20081       tree string_literal;
20082
20083       /* Look for the string literal.  */
20084       string_literal = cp_parser_string_literal (parser, false, false);
20085       /* Add it to the list.  */
20086       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
20087       /* If the next token is not a `,', then the list is
20088          complete.  */
20089       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20090         break;
20091       /* Consume the `,' token.  */
20092       cp_lexer_consume_token (parser->lexer);
20093     }
20094
20095   return clobbers;
20096 }
20097
20098 /* Parse an asm-label-list.
20099
20100    asm-label-list:
20101      identifier
20102      asm-label-list , identifier
20103
20104    Returns a TREE_LIST, indicating the labels in the order that they
20105    appeared.  The TREE_VALUE of each node is a label.  */
20106
20107 static tree
20108 cp_parser_asm_label_list (cp_parser* parser)
20109 {
20110   tree labels = NULL_TREE;
20111
20112   while (true)
20113     {
20114       tree identifier, label, name;
20115
20116       /* Look for the identifier.  */
20117       identifier = cp_parser_identifier (parser);
20118       if (!error_operand_p (identifier))
20119         {
20120           label = lookup_label (identifier);
20121           if (TREE_CODE (label) == LABEL_DECL)
20122             {
20123               TREE_USED (label) = 1;
20124               check_goto (label);
20125               name = build_string (IDENTIFIER_LENGTH (identifier),
20126                                    IDENTIFIER_POINTER (identifier));
20127               labels = tree_cons (name, label, labels);
20128             }
20129         }
20130       /* If the next token is not a `,', then the list is
20131          complete.  */
20132       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
20133         break;
20134       /* Consume the `,' token.  */
20135       cp_lexer_consume_token (parser->lexer);
20136     }
20137
20138   return nreverse (labels);
20139 }
20140
20141 /* Parse an (optional) series of attributes.
20142
20143    attributes:
20144      attributes attribute
20145
20146    attribute:
20147      __attribute__ (( attribute-list [opt] ))
20148
20149    The return value is as for cp_parser_attribute_list.  */
20150
20151 static tree
20152 cp_parser_attributes_opt (cp_parser* parser)
20153 {
20154   tree attributes = NULL_TREE;
20155
20156   while (true)
20157     {
20158       cp_token *token;
20159       tree attribute_list;
20160
20161       /* Peek at the next token.  */
20162       token = cp_lexer_peek_token (parser->lexer);
20163       /* If it's not `__attribute__', then we're done.  */
20164       if (token->keyword != RID_ATTRIBUTE)
20165         break;
20166
20167       /* Consume the `__attribute__' keyword.  */
20168       cp_lexer_consume_token (parser->lexer);
20169       /* Look for the two `(' tokens.  */
20170       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20171       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20172
20173       /* Peek at the next token.  */
20174       token = cp_lexer_peek_token (parser->lexer);
20175       if (token->type != CPP_CLOSE_PAREN)
20176         /* Parse the attribute-list.  */
20177         attribute_list = cp_parser_attribute_list (parser);
20178       else
20179         /* If the next token is a `)', then there is no attribute
20180            list.  */
20181         attribute_list = NULL;
20182
20183       /* Look for the two `)' tokens.  */
20184       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20185       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20186
20187       /* Add these new attributes to the list.  */
20188       attributes = chainon (attributes, attribute_list);
20189     }
20190
20191   return attributes;
20192 }
20193
20194 /* Parse an attribute-list.
20195
20196    attribute-list:
20197      attribute
20198      attribute-list , attribute
20199
20200    attribute:
20201      identifier
20202      identifier ( identifier )
20203      identifier ( identifier , expression-list )
20204      identifier ( expression-list )
20205
20206    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20207    to an attribute.  The TREE_PURPOSE of each node is the identifier
20208    indicating which attribute is in use.  The TREE_VALUE represents
20209    the arguments, if any.  */
20210
20211 static tree
20212 cp_parser_attribute_list (cp_parser* parser)
20213 {
20214   tree attribute_list = NULL_TREE;
20215   bool save_translate_strings_p = parser->translate_strings_p;
20216
20217   parser->translate_strings_p = false;
20218   while (true)
20219     {
20220       cp_token *token;
20221       tree identifier;
20222       tree attribute;
20223
20224       /* Look for the identifier.  We also allow keywords here; for
20225          example `__attribute__ ((const))' is legal.  */
20226       token = cp_lexer_peek_token (parser->lexer);
20227       if (token->type == CPP_NAME
20228           || token->type == CPP_KEYWORD)
20229         {
20230           tree arguments = NULL_TREE;
20231
20232           /* Consume the token.  */
20233           token = cp_lexer_consume_token (parser->lexer);
20234
20235           /* Save away the identifier that indicates which attribute
20236              this is.  */
20237           identifier = (token->type == CPP_KEYWORD) 
20238             /* For keywords, use the canonical spelling, not the
20239                parsed identifier.  */
20240             ? ridpointers[(int) token->keyword]
20241             : token->u.value;
20242           
20243           attribute = build_tree_list (identifier, NULL_TREE);
20244
20245           /* Peek at the next token.  */
20246           token = cp_lexer_peek_token (parser->lexer);
20247           /* If it's an `(', then parse the attribute arguments.  */
20248           if (token->type == CPP_OPEN_PAREN)
20249             {
20250               VEC(tree,gc) *vec;
20251               int attr_flag = (attribute_takes_identifier_p (identifier)
20252                                ? id_attr : normal_attr);
20253               vec = cp_parser_parenthesized_expression_list
20254                     (parser, attr_flag, /*cast_p=*/false,
20255                      /*allow_expansion_p=*/false,
20256                      /*non_constant_p=*/NULL);
20257               if (vec == NULL)
20258                 arguments = error_mark_node;
20259               else
20260                 {
20261                   arguments = build_tree_list_vec (vec);
20262                   release_tree_vector (vec);
20263                 }
20264               /* Save the arguments away.  */
20265               TREE_VALUE (attribute) = arguments;
20266             }
20267
20268           if (arguments != error_mark_node)
20269             {
20270               /* Add this attribute to the list.  */
20271               TREE_CHAIN (attribute) = attribute_list;
20272               attribute_list = attribute;
20273             }
20274
20275           token = cp_lexer_peek_token (parser->lexer);
20276         }
20277       /* Now, look for more attributes.  If the next token isn't a
20278          `,', we're done.  */
20279       if (token->type != CPP_COMMA)
20280         break;
20281
20282       /* Consume the comma and keep going.  */
20283       cp_lexer_consume_token (parser->lexer);
20284     }
20285   parser->translate_strings_p = save_translate_strings_p;
20286
20287   /* We built up the list in reverse order.  */
20288   return nreverse (attribute_list);
20289 }
20290
20291 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20292    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20293    current value of the PEDANTIC flag, regardless of whether or not
20294    the `__extension__' keyword is present.  The caller is responsible
20295    for restoring the value of the PEDANTIC flag.  */
20296
20297 static bool
20298 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20299 {
20300   /* Save the old value of the PEDANTIC flag.  */
20301   *saved_pedantic = pedantic;
20302
20303   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20304     {
20305       /* Consume the `__extension__' token.  */
20306       cp_lexer_consume_token (parser->lexer);
20307       /* We're not being pedantic while the `__extension__' keyword is
20308          in effect.  */
20309       pedantic = 0;
20310
20311       return true;
20312     }
20313
20314   return false;
20315 }
20316
20317 /* Parse a label declaration.
20318
20319    label-declaration:
20320      __label__ label-declarator-seq ;
20321
20322    label-declarator-seq:
20323      identifier , label-declarator-seq
20324      identifier  */
20325
20326 static void
20327 cp_parser_label_declaration (cp_parser* parser)
20328 {
20329   /* Look for the `__label__' keyword.  */
20330   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20331
20332   while (true)
20333     {
20334       tree identifier;
20335
20336       /* Look for an identifier.  */
20337       identifier = cp_parser_identifier (parser);
20338       /* If we failed, stop.  */
20339       if (identifier == error_mark_node)
20340         break;
20341       /* Declare it as a label.  */
20342       finish_label_decl (identifier);
20343       /* If the next token is a `;', stop.  */
20344       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20345         break;
20346       /* Look for the `,' separating the label declarations.  */
20347       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20348     }
20349
20350   /* Look for the final `;'.  */
20351   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20352 }
20353
20354 /* Support Functions */
20355
20356 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20357    NAME should have one of the representations used for an
20358    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20359    is returned.  If PARSER->SCOPE is a dependent type, then a
20360    SCOPE_REF is returned.
20361
20362    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20363    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20364    was formed.  Abstractly, such entities should not be passed to this
20365    function, because they do not need to be looked up, but it is
20366    simpler to check for this special case here, rather than at the
20367    call-sites.
20368
20369    In cases not explicitly covered above, this function returns a
20370    DECL, OVERLOAD, or baselink representing the result of the lookup.
20371    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20372    is returned.
20373
20374    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20375    (e.g., "struct") that was used.  In that case bindings that do not
20376    refer to types are ignored.
20377
20378    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20379    ignored.
20380
20381    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20382    are ignored.
20383
20384    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20385    types.
20386
20387    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20388    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20389    NULL_TREE otherwise.  */
20390
20391 static tree
20392 cp_parser_lookup_name (cp_parser *parser, tree name,
20393                        enum tag_types tag_type,
20394                        bool is_template,
20395                        bool is_namespace,
20396                        bool check_dependency,
20397                        tree *ambiguous_decls,
20398                        location_t name_location)
20399 {
20400   int flags = 0;
20401   tree decl;
20402   tree object_type = parser->context->object_type;
20403
20404   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20405     flags |= LOOKUP_COMPLAIN;
20406
20407   /* Assume that the lookup will be unambiguous.  */
20408   if (ambiguous_decls)
20409     *ambiguous_decls = NULL_TREE;
20410
20411   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20412      no longer valid.  Note that if we are parsing tentatively, and
20413      the parse fails, OBJECT_TYPE will be automatically restored.  */
20414   parser->context->object_type = NULL_TREE;
20415
20416   if (name == error_mark_node)
20417     return error_mark_node;
20418
20419   /* A template-id has already been resolved; there is no lookup to
20420      do.  */
20421   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20422     return name;
20423   if (BASELINK_P (name))
20424     {
20425       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20426                   == TEMPLATE_ID_EXPR);
20427       return name;
20428     }
20429
20430   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20431      it should already have been checked to make sure that the name
20432      used matches the type being destroyed.  */
20433   if (TREE_CODE (name) == BIT_NOT_EXPR)
20434     {
20435       tree type;
20436
20437       /* Figure out to which type this destructor applies.  */
20438       if (parser->scope)
20439         type = parser->scope;
20440       else if (object_type)
20441         type = object_type;
20442       else
20443         type = current_class_type;
20444       /* If that's not a class type, there is no destructor.  */
20445       if (!type || !CLASS_TYPE_P (type))
20446         return error_mark_node;
20447       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20448         lazily_declare_fn (sfk_destructor, type);
20449       if (!CLASSTYPE_DESTRUCTORS (type))
20450           return error_mark_node;
20451       /* If it was a class type, return the destructor.  */
20452       return CLASSTYPE_DESTRUCTORS (type);
20453     }
20454
20455   /* By this point, the NAME should be an ordinary identifier.  If
20456      the id-expression was a qualified name, the qualifying scope is
20457      stored in PARSER->SCOPE at this point.  */
20458   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20459
20460   /* Perform the lookup.  */
20461   if (parser->scope)
20462     {
20463       bool dependent_p;
20464
20465       if (parser->scope == error_mark_node)
20466         return error_mark_node;
20467
20468       /* If the SCOPE is dependent, the lookup must be deferred until
20469          the template is instantiated -- unless we are explicitly
20470          looking up names in uninstantiated templates.  Even then, we
20471          cannot look up the name if the scope is not a class type; it
20472          might, for example, be a template type parameter.  */
20473       dependent_p = (TYPE_P (parser->scope)
20474                      && dependent_scope_p (parser->scope));
20475       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20476           && dependent_p)
20477         /* Defer lookup.  */
20478         decl = error_mark_node;
20479       else
20480         {
20481           tree pushed_scope = NULL_TREE;
20482
20483           /* If PARSER->SCOPE is a dependent type, then it must be a
20484              class type, and we must not be checking dependencies;
20485              otherwise, we would have processed this lookup above.  So
20486              that PARSER->SCOPE is not considered a dependent base by
20487              lookup_member, we must enter the scope here.  */
20488           if (dependent_p)
20489             pushed_scope = push_scope (parser->scope);
20490
20491           /* If the PARSER->SCOPE is a template specialization, it
20492              may be instantiated during name lookup.  In that case,
20493              errors may be issued.  Even if we rollback the current
20494              tentative parse, those errors are valid.  */
20495           decl = lookup_qualified_name (parser->scope, name,
20496                                         tag_type != none_type,
20497                                         /*complain=*/true);
20498
20499           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20500              lookup result and the nested-name-specifier nominates a class C:
20501                * if the name specified after the nested-name-specifier, when
20502                looked up in C, is the injected-class-name of C (Clause 9), or
20503                * if the name specified after the nested-name-specifier is the
20504                same as the identifier or the simple-template-id's template-
20505                name in the last component of the nested-name-specifier,
20506              the name is instead considered to name the constructor of
20507              class C. [ Note: for example, the constructor is not an
20508              acceptable lookup result in an elaborated-type-specifier so
20509              the constructor would not be used in place of the
20510              injected-class-name. --end note ] Such a constructor name
20511              shall be used only in the declarator-id of a declaration that
20512              names a constructor or in a using-declaration.  */
20513           if (tag_type == none_type
20514               && DECL_SELF_REFERENCE_P (decl)
20515               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20516             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20517                                           tag_type != none_type,
20518                                           /*complain=*/true);
20519
20520           /* If we have a single function from a using decl, pull it out.  */
20521           if (TREE_CODE (decl) == OVERLOAD
20522               && !really_overloaded_fn (decl))
20523             decl = OVL_FUNCTION (decl);
20524
20525           if (pushed_scope)
20526             pop_scope (pushed_scope);
20527         }
20528
20529       /* If the scope is a dependent type and either we deferred lookup or
20530          we did lookup but didn't find the name, rememeber the name.  */
20531       if (decl == error_mark_node && TYPE_P (parser->scope)
20532           && dependent_type_p (parser->scope))
20533         {
20534           if (tag_type)
20535             {
20536               tree type;
20537
20538               /* The resolution to Core Issue 180 says that `struct
20539                  A::B' should be considered a type-name, even if `A'
20540                  is dependent.  */
20541               type = make_typename_type (parser->scope, name, tag_type,
20542                                          /*complain=*/tf_error);
20543               decl = TYPE_NAME (type);
20544             }
20545           else if (is_template
20546                    && (cp_parser_next_token_ends_template_argument_p (parser)
20547                        || cp_lexer_next_token_is (parser->lexer,
20548                                                   CPP_CLOSE_PAREN)))
20549             decl = make_unbound_class_template (parser->scope,
20550                                                 name, NULL_TREE,
20551                                                 /*complain=*/tf_error);
20552           else
20553             decl = build_qualified_name (/*type=*/NULL_TREE,
20554                                          parser->scope, name,
20555                                          is_template);
20556         }
20557       parser->qualifying_scope = parser->scope;
20558       parser->object_scope = NULL_TREE;
20559     }
20560   else if (object_type)
20561     {
20562       tree object_decl = NULL_TREE;
20563       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20564          OBJECT_TYPE is not a class.  */
20565       if (CLASS_TYPE_P (object_type))
20566         /* If the OBJECT_TYPE is a template specialization, it may
20567            be instantiated during name lookup.  In that case, errors
20568            may be issued.  Even if we rollback the current tentative
20569            parse, those errors are valid.  */
20570         object_decl = lookup_member (object_type,
20571                                      name,
20572                                      /*protect=*/0,
20573                                      tag_type != none_type,
20574                                      tf_warning_or_error);
20575       /* Look it up in the enclosing context, too.  */
20576       decl = lookup_name_real (name, tag_type != none_type,
20577                                /*nonclass=*/0,
20578                                /*block_p=*/true, is_namespace, flags);
20579       parser->object_scope = object_type;
20580       parser->qualifying_scope = NULL_TREE;
20581       if (object_decl)
20582         decl = object_decl;
20583     }
20584   else
20585     {
20586       decl = lookup_name_real (name, tag_type != none_type,
20587                                /*nonclass=*/0,
20588                                /*block_p=*/true, is_namespace, flags);
20589       parser->qualifying_scope = NULL_TREE;
20590       parser->object_scope = NULL_TREE;
20591     }
20592
20593   /* If the lookup failed, let our caller know.  */
20594   if (!decl || decl == error_mark_node)
20595     return error_mark_node;
20596
20597   /* Pull out the template from an injected-class-name (or multiple).  */
20598   if (is_template)
20599     decl = maybe_get_template_decl_from_type_decl (decl);
20600
20601   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20602   if (TREE_CODE (decl) == TREE_LIST)
20603     {
20604       if (ambiguous_decls)
20605         *ambiguous_decls = decl;
20606       /* The error message we have to print is too complicated for
20607          cp_parser_error, so we incorporate its actions directly.  */
20608       if (!cp_parser_simulate_error (parser))
20609         {
20610           error_at (name_location, "reference to %qD is ambiguous",
20611                     name);
20612           print_candidates (decl);
20613         }
20614       return error_mark_node;
20615     }
20616
20617   gcc_assert (DECL_P (decl)
20618               || TREE_CODE (decl) == OVERLOAD
20619               || TREE_CODE (decl) == SCOPE_REF
20620               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20621               || BASELINK_P (decl));
20622
20623   /* If we have resolved the name of a member declaration, check to
20624      see if the declaration is accessible.  When the name resolves to
20625      set of overloaded functions, accessibility is checked when
20626      overload resolution is done.
20627
20628      During an explicit instantiation, access is not checked at all,
20629      as per [temp.explicit].  */
20630   if (DECL_P (decl))
20631     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20632
20633   maybe_record_typedef_use (decl);
20634
20635   return decl;
20636 }
20637
20638 /* Like cp_parser_lookup_name, but for use in the typical case where
20639    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20640    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20641
20642 static tree
20643 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20644 {
20645   return cp_parser_lookup_name (parser, name,
20646                                 none_type,
20647                                 /*is_template=*/false,
20648                                 /*is_namespace=*/false,
20649                                 /*check_dependency=*/true,
20650                                 /*ambiguous_decls=*/NULL,
20651                                 location);
20652 }
20653
20654 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20655    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20656    true, the DECL indicates the class being defined in a class-head,
20657    or declared in an elaborated-type-specifier.
20658
20659    Otherwise, return DECL.  */
20660
20661 static tree
20662 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20663 {
20664   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20665      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20666
20667        struct A {
20668          template <typename T> struct B;
20669        };
20670
20671        template <typename T> struct A::B {};
20672
20673      Similarly, in an elaborated-type-specifier:
20674
20675        namespace N { struct X{}; }
20676
20677        struct A {
20678          template <typename T> friend struct N::X;
20679        };
20680
20681      However, if the DECL refers to a class type, and we are in
20682      the scope of the class, then the name lookup automatically
20683      finds the TYPE_DECL created by build_self_reference rather
20684      than a TEMPLATE_DECL.  For example, in:
20685
20686        template <class T> struct S {
20687          S s;
20688        };
20689
20690      there is no need to handle such case.  */
20691
20692   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20693     return DECL_TEMPLATE_RESULT (decl);
20694
20695   return decl;
20696 }
20697
20698 /* If too many, or too few, template-parameter lists apply to the
20699    declarator, issue an error message.  Returns TRUE if all went well,
20700    and FALSE otherwise.  */
20701
20702 static bool
20703 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20704                                                 cp_declarator *declarator,
20705                                                 location_t declarator_location)
20706 {
20707   unsigned num_templates;
20708
20709   /* We haven't seen any classes that involve template parameters yet.  */
20710   num_templates = 0;
20711
20712   switch (declarator->kind)
20713     {
20714     case cdk_id:
20715       if (declarator->u.id.qualifying_scope)
20716         {
20717           tree scope;
20718
20719           scope = declarator->u.id.qualifying_scope;
20720
20721           while (scope && CLASS_TYPE_P (scope))
20722             {
20723               /* You're supposed to have one `template <...>'
20724                  for every template class, but you don't need one
20725                  for a full specialization.  For example:
20726
20727                  template <class T> struct S{};
20728                  template <> struct S<int> { void f(); };
20729                  void S<int>::f () {}
20730
20731                  is correct; there shouldn't be a `template <>' for
20732                  the definition of `S<int>::f'.  */
20733               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20734                 /* If SCOPE does not have template information of any
20735                    kind, then it is not a template, nor is it nested
20736                    within a template.  */
20737                 break;
20738               if (explicit_class_specialization_p (scope))
20739                 break;
20740               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20741                 ++num_templates;
20742
20743               scope = TYPE_CONTEXT (scope);
20744             }
20745         }
20746       else if (TREE_CODE (declarator->u.id.unqualified_name)
20747                == TEMPLATE_ID_EXPR)
20748         /* If the DECLARATOR has the form `X<y>' then it uses one
20749            additional level of template parameters.  */
20750         ++num_templates;
20751
20752       return cp_parser_check_template_parameters 
20753         (parser, num_templates, declarator_location, declarator);
20754
20755
20756     case cdk_function:
20757     case cdk_array:
20758     case cdk_pointer:
20759     case cdk_reference:
20760     case cdk_ptrmem:
20761       return (cp_parser_check_declarator_template_parameters
20762               (parser, declarator->declarator, declarator_location));
20763
20764     case cdk_error:
20765       return true;
20766
20767     default:
20768       gcc_unreachable ();
20769     }
20770   return false;
20771 }
20772
20773 /* NUM_TEMPLATES were used in the current declaration.  If that is
20774    invalid, return FALSE and issue an error messages.  Otherwise,
20775    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20776    declarator and we can print more accurate diagnostics.  */
20777
20778 static bool
20779 cp_parser_check_template_parameters (cp_parser* parser,
20780                                      unsigned num_templates,
20781                                      location_t location,
20782                                      cp_declarator *declarator)
20783 {
20784   /* If there are the same number of template classes and parameter
20785      lists, that's OK.  */
20786   if (parser->num_template_parameter_lists == num_templates)
20787     return true;
20788   /* If there are more, but only one more, then we are referring to a
20789      member template.  That's OK too.  */
20790   if (parser->num_template_parameter_lists == num_templates + 1)
20791     return true;
20792   /* If there are more template classes than parameter lists, we have
20793      something like:
20794
20795        template <class T> void S<T>::R<T>::f ();  */
20796   if (parser->num_template_parameter_lists < num_templates)
20797     {
20798       if (declarator && !current_function_decl)
20799         error_at (location, "specializing member %<%T::%E%> "
20800                   "requires %<template<>%> syntax", 
20801                   declarator->u.id.qualifying_scope,
20802                   declarator->u.id.unqualified_name);
20803       else if (declarator)
20804         error_at (location, "invalid declaration of %<%T::%E%>",
20805                   declarator->u.id.qualifying_scope,
20806                   declarator->u.id.unqualified_name);
20807       else 
20808         error_at (location, "too few template-parameter-lists");
20809       return false;
20810     }
20811   /* Otherwise, there are too many template parameter lists.  We have
20812      something like:
20813
20814      template <class T> template <class U> void S::f();  */
20815   error_at (location, "too many template-parameter-lists");
20816   return false;
20817 }
20818
20819 /* Parse an optional `::' token indicating that the following name is
20820    from the global namespace.  If so, PARSER->SCOPE is set to the
20821    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20822    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20823    Returns the new value of PARSER->SCOPE, if the `::' token is
20824    present, and NULL_TREE otherwise.  */
20825
20826 static tree
20827 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20828 {
20829   cp_token *token;
20830
20831   /* Peek at the next token.  */
20832   token = cp_lexer_peek_token (parser->lexer);
20833   /* If we're looking at a `::' token then we're starting from the
20834      global namespace, not our current location.  */
20835   if (token->type == CPP_SCOPE)
20836     {
20837       /* Consume the `::' token.  */
20838       cp_lexer_consume_token (parser->lexer);
20839       /* Set the SCOPE so that we know where to start the lookup.  */
20840       parser->scope = global_namespace;
20841       parser->qualifying_scope = global_namespace;
20842       parser->object_scope = NULL_TREE;
20843
20844       return parser->scope;
20845     }
20846   else if (!current_scope_valid_p)
20847     {
20848       parser->scope = NULL_TREE;
20849       parser->qualifying_scope = NULL_TREE;
20850       parser->object_scope = NULL_TREE;
20851     }
20852
20853   return NULL_TREE;
20854 }
20855
20856 /* Returns TRUE if the upcoming token sequence is the start of a
20857    constructor declarator.  If FRIEND_P is true, the declarator is
20858    preceded by the `friend' specifier.  */
20859
20860 static bool
20861 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20862 {
20863   bool constructor_p;
20864   tree nested_name_specifier;
20865   cp_token *next_token;
20866
20867   /* The common case is that this is not a constructor declarator, so
20868      try to avoid doing lots of work if at all possible.  It's not
20869      valid declare a constructor at function scope.  */
20870   if (parser->in_function_body)
20871     return false;
20872   /* And only certain tokens can begin a constructor declarator.  */
20873   next_token = cp_lexer_peek_token (parser->lexer);
20874   if (next_token->type != CPP_NAME
20875       && next_token->type != CPP_SCOPE
20876       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20877       && next_token->type != CPP_TEMPLATE_ID)
20878     return false;
20879
20880   /* Parse tentatively; we are going to roll back all of the tokens
20881      consumed here.  */
20882   cp_parser_parse_tentatively (parser);
20883   /* Assume that we are looking at a constructor declarator.  */
20884   constructor_p = true;
20885
20886   /* Look for the optional `::' operator.  */
20887   cp_parser_global_scope_opt (parser,
20888                               /*current_scope_valid_p=*/false);
20889   /* Look for the nested-name-specifier.  */
20890   nested_name_specifier
20891     = (cp_parser_nested_name_specifier_opt (parser,
20892                                             /*typename_keyword_p=*/false,
20893                                             /*check_dependency_p=*/false,
20894                                             /*type_p=*/false,
20895                                             /*is_declaration=*/false));
20896   /* Outside of a class-specifier, there must be a
20897      nested-name-specifier.  */
20898   if (!nested_name_specifier &&
20899       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20900        || friend_p))
20901     constructor_p = false;
20902   else if (nested_name_specifier == error_mark_node)
20903     constructor_p = false;
20904
20905   /* If we have a class scope, this is easy; DR 147 says that S::S always
20906      names the constructor, and no other qualified name could.  */
20907   if (constructor_p && nested_name_specifier
20908       && CLASS_TYPE_P (nested_name_specifier))
20909     {
20910       tree id = cp_parser_unqualified_id (parser,
20911                                           /*template_keyword_p=*/false,
20912                                           /*check_dependency_p=*/false,
20913                                           /*declarator_p=*/true,
20914                                           /*optional_p=*/false);
20915       if (is_overloaded_fn (id))
20916         id = DECL_NAME (get_first_fn (id));
20917       if (!constructor_name_p (id, nested_name_specifier))
20918         constructor_p = false;
20919     }
20920   /* If we still think that this might be a constructor-declarator,
20921      look for a class-name.  */
20922   else if (constructor_p)
20923     {
20924       /* If we have:
20925
20926            template <typename T> struct S {
20927              S();
20928            };
20929
20930          we must recognize that the nested `S' names a class.  */
20931       tree type_decl;
20932       type_decl = cp_parser_class_name (parser,
20933                                         /*typename_keyword_p=*/false,
20934                                         /*template_keyword_p=*/false,
20935                                         none_type,
20936                                         /*check_dependency_p=*/false,
20937                                         /*class_head_p=*/false,
20938                                         /*is_declaration=*/false);
20939       /* If there was no class-name, then this is not a constructor.  */
20940       constructor_p = !cp_parser_error_occurred (parser);
20941
20942       /* If we're still considering a constructor, we have to see a `(',
20943          to begin the parameter-declaration-clause, followed by either a
20944          `)', an `...', or a decl-specifier.  We need to check for a
20945          type-specifier to avoid being fooled into thinking that:
20946
20947            S (f) (int);
20948
20949          is a constructor.  (It is actually a function named `f' that
20950          takes one parameter (of type `int') and returns a value of type
20951          `S'.  */
20952       if (constructor_p
20953           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20954         constructor_p = false;
20955
20956       if (constructor_p
20957           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20958           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20959           /* A parameter declaration begins with a decl-specifier,
20960              which is either the "attribute" keyword, a storage class
20961              specifier, or (usually) a type-specifier.  */
20962           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20963         {
20964           tree type;
20965           tree pushed_scope = NULL_TREE;
20966           unsigned saved_num_template_parameter_lists;
20967
20968           /* Names appearing in the type-specifier should be looked up
20969              in the scope of the class.  */
20970           if (current_class_type)
20971             type = NULL_TREE;
20972           else
20973             {
20974               type = TREE_TYPE (type_decl);
20975               if (TREE_CODE (type) == TYPENAME_TYPE)
20976                 {
20977                   type = resolve_typename_type (type,
20978                                                 /*only_current_p=*/false);
20979                   if (TREE_CODE (type) == TYPENAME_TYPE)
20980                     {
20981                       cp_parser_abort_tentative_parse (parser);
20982                       return false;
20983                     }
20984                 }
20985               pushed_scope = push_scope (type);
20986             }
20987
20988           /* Inside the constructor parameter list, surrounding
20989              template-parameter-lists do not apply.  */
20990           saved_num_template_parameter_lists
20991             = parser->num_template_parameter_lists;
20992           parser->num_template_parameter_lists = 0;
20993
20994           /* Look for the type-specifier.  */
20995           cp_parser_type_specifier (parser,
20996                                     CP_PARSER_FLAGS_NONE,
20997                                     /*decl_specs=*/NULL,
20998                                     /*is_declarator=*/true,
20999                                     /*declares_class_or_enum=*/NULL,
21000                                     /*is_cv_qualifier=*/NULL);
21001
21002           parser->num_template_parameter_lists
21003             = saved_num_template_parameter_lists;
21004
21005           /* Leave the scope of the class.  */
21006           if (pushed_scope)
21007             pop_scope (pushed_scope);
21008
21009           constructor_p = !cp_parser_error_occurred (parser);
21010         }
21011     }
21012
21013   /* We did not really want to consume any tokens.  */
21014   cp_parser_abort_tentative_parse (parser);
21015
21016   return constructor_p;
21017 }
21018
21019 /* Parse the definition of the function given by the DECL_SPECIFIERS,
21020    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
21021    they must be performed once we are in the scope of the function.
21022
21023    Returns the function defined.  */
21024
21025 static tree
21026 cp_parser_function_definition_from_specifiers_and_declarator
21027   (cp_parser* parser,
21028    cp_decl_specifier_seq *decl_specifiers,
21029    tree attributes,
21030    const cp_declarator *declarator)
21031 {
21032   tree fn;
21033   bool success_p;
21034
21035   /* Begin the function-definition.  */
21036   success_p = start_function (decl_specifiers, declarator, attributes);
21037
21038   /* The things we're about to see are not directly qualified by any
21039      template headers we've seen thus far.  */
21040   reset_specialization ();
21041
21042   /* If there were names looked up in the decl-specifier-seq that we
21043      did not check, check them now.  We must wait until we are in the
21044      scope of the function to perform the checks, since the function
21045      might be a friend.  */
21046   perform_deferred_access_checks ();
21047
21048   if (!success_p)
21049     {
21050       /* Skip the entire function.  */
21051       cp_parser_skip_to_end_of_block_or_statement (parser);
21052       fn = error_mark_node;
21053     }
21054   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
21055     {
21056       /* Seen already, skip it.  An error message has already been output.  */
21057       cp_parser_skip_to_end_of_block_or_statement (parser);
21058       fn = current_function_decl;
21059       current_function_decl = NULL_TREE;
21060       /* If this is a function from a class, pop the nested class.  */
21061       if (current_class_name)
21062         pop_nested_class ();
21063     }
21064   else
21065     {
21066       timevar_id_t tv;
21067       if (DECL_DECLARED_INLINE_P (current_function_decl))
21068         tv = TV_PARSE_INLINE;
21069       else
21070         tv = TV_PARSE_FUNC;
21071       timevar_push (tv);
21072       fn = cp_parser_function_definition_after_declarator (parser,
21073                                                          /*inline_p=*/false);
21074       timevar_pop (tv);
21075     }
21076
21077   return fn;
21078 }
21079
21080 /* Parse the part of a function-definition that follows the
21081    declarator.  INLINE_P is TRUE iff this function is an inline
21082    function defined within a class-specifier.
21083
21084    Returns the function defined.  */
21085
21086 static tree
21087 cp_parser_function_definition_after_declarator (cp_parser* parser,
21088                                                 bool inline_p)
21089 {
21090   tree fn;
21091   bool ctor_initializer_p = false;
21092   bool saved_in_unbraced_linkage_specification_p;
21093   bool saved_in_function_body;
21094   unsigned saved_num_template_parameter_lists;
21095   cp_token *token;
21096
21097   saved_in_function_body = parser->in_function_body;
21098   parser->in_function_body = true;
21099   /* If the next token is `return', then the code may be trying to
21100      make use of the "named return value" extension that G++ used to
21101      support.  */
21102   token = cp_lexer_peek_token (parser->lexer);
21103   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
21104     {
21105       /* Consume the `return' keyword.  */
21106       cp_lexer_consume_token (parser->lexer);
21107       /* Look for the identifier that indicates what value is to be
21108          returned.  */
21109       cp_parser_identifier (parser);
21110       /* Issue an error message.  */
21111       error_at (token->location,
21112                 "named return values are no longer supported");
21113       /* Skip tokens until we reach the start of the function body.  */
21114       while (true)
21115         {
21116           cp_token *token = cp_lexer_peek_token (parser->lexer);
21117           if (token->type == CPP_OPEN_BRACE
21118               || token->type == CPP_EOF
21119               || token->type == CPP_PRAGMA_EOL)
21120             break;
21121           cp_lexer_consume_token (parser->lexer);
21122         }
21123     }
21124   /* The `extern' in `extern "C" void f () { ... }' does not apply to
21125      anything declared inside `f'.  */
21126   saved_in_unbraced_linkage_specification_p
21127     = parser->in_unbraced_linkage_specification_p;
21128   parser->in_unbraced_linkage_specification_p = false;
21129   /* Inside the function, surrounding template-parameter-lists do not
21130      apply.  */
21131   saved_num_template_parameter_lists
21132     = parser->num_template_parameter_lists;
21133   parser->num_template_parameter_lists = 0;
21134
21135   start_lambda_scope (current_function_decl);
21136
21137   /* If the next token is `try', `__transaction_atomic', or
21138      `__transaction_relaxed`, then we are looking at either function-try-block
21139      or function-transaction-block.  Note that all of these include the
21140      function-body.  */
21141   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
21142     ctor_initializer_p = cp_parser_function_transaction (parser,
21143         RID_TRANSACTION_ATOMIC);
21144   else if (cp_lexer_next_token_is_keyword (parser->lexer,
21145       RID_TRANSACTION_RELAXED))
21146     ctor_initializer_p = cp_parser_function_transaction (parser,
21147         RID_TRANSACTION_RELAXED);
21148   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
21149     ctor_initializer_p = cp_parser_function_try_block (parser);
21150   else
21151     ctor_initializer_p
21152       = cp_parser_ctor_initializer_opt_and_function_body (parser);
21153
21154   finish_lambda_scope ();
21155
21156   /* Finish the function.  */
21157   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
21158                         (inline_p ? 2 : 0));
21159   /* Generate code for it, if necessary.  */
21160   expand_or_defer_fn (fn);
21161   /* Restore the saved values.  */
21162   parser->in_unbraced_linkage_specification_p
21163     = saved_in_unbraced_linkage_specification_p;
21164   parser->num_template_parameter_lists
21165     = saved_num_template_parameter_lists;
21166   parser->in_function_body = saved_in_function_body;
21167
21168   return fn;
21169 }
21170
21171 /* Parse a template-declaration, assuming that the `export' (and
21172    `extern') keywords, if present, has already been scanned.  MEMBER_P
21173    is as for cp_parser_template_declaration.  */
21174
21175 static void
21176 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21177 {
21178   tree decl = NULL_TREE;
21179   VEC (deferred_access_check,gc) *checks;
21180   tree parameter_list;
21181   bool friend_p = false;
21182   bool need_lang_pop;
21183   cp_token *token;
21184
21185   /* Look for the `template' keyword.  */
21186   token = cp_lexer_peek_token (parser->lexer);
21187   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21188     return;
21189
21190   /* And the `<'.  */
21191   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21192     return;
21193   if (at_class_scope_p () && current_function_decl)
21194     {
21195       /* 14.5.2.2 [temp.mem]
21196
21197          A local class shall not have member templates.  */
21198       error_at (token->location,
21199                 "invalid declaration of member template in local class");
21200       cp_parser_skip_to_end_of_block_or_statement (parser);
21201       return;
21202     }
21203   /* [temp]
21204
21205      A template ... shall not have C linkage.  */
21206   if (current_lang_name == lang_name_c)
21207     {
21208       error_at (token->location, "template with C linkage");
21209       /* Give it C++ linkage to avoid confusing other parts of the
21210          front end.  */
21211       push_lang_context (lang_name_cplusplus);
21212       need_lang_pop = true;
21213     }
21214   else
21215     need_lang_pop = false;
21216
21217   /* We cannot perform access checks on the template parameter
21218      declarations until we know what is being declared, just as we
21219      cannot check the decl-specifier list.  */
21220   push_deferring_access_checks (dk_deferred);
21221
21222   /* If the next token is `>', then we have an invalid
21223      specialization.  Rather than complain about an invalid template
21224      parameter, issue an error message here.  */
21225   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21226     {
21227       cp_parser_error (parser, "invalid explicit specialization");
21228       begin_specialization ();
21229       parameter_list = NULL_TREE;
21230     }
21231   else
21232     {
21233       /* Parse the template parameters.  */
21234       parameter_list = cp_parser_template_parameter_list (parser);
21235       fixup_template_parms ();
21236     }
21237
21238   /* Get the deferred access checks from the parameter list.  These
21239      will be checked once we know what is being declared, as for a
21240      member template the checks must be performed in the scope of the
21241      class containing the member.  */
21242   checks = get_deferred_access_checks ();
21243
21244   /* Look for the `>'.  */
21245   cp_parser_skip_to_end_of_template_parameter_list (parser);
21246   /* We just processed one more parameter list.  */
21247   ++parser->num_template_parameter_lists;
21248   /* If the next token is `template', there are more template
21249      parameters.  */
21250   if (cp_lexer_next_token_is_keyword (parser->lexer,
21251                                       RID_TEMPLATE))
21252     cp_parser_template_declaration_after_export (parser, member_p);
21253   else if (cxx_dialect >= cxx0x
21254            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21255     decl = cp_parser_alias_declaration (parser);
21256   else
21257     {
21258       /* There are no access checks when parsing a template, as we do not
21259          know if a specialization will be a friend.  */
21260       push_deferring_access_checks (dk_no_check);
21261       token = cp_lexer_peek_token (parser->lexer);
21262       decl = cp_parser_single_declaration (parser,
21263                                            checks,
21264                                            member_p,
21265                                            /*explicit_specialization_p=*/false,
21266                                            &friend_p);
21267       pop_deferring_access_checks ();
21268
21269       /* If this is a member template declaration, let the front
21270          end know.  */
21271       if (member_p && !friend_p && decl)
21272         {
21273           if (TREE_CODE (decl) == TYPE_DECL)
21274             cp_parser_check_access_in_redeclaration (decl, token->location);
21275
21276           decl = finish_member_template_decl (decl);
21277         }
21278       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21279         make_friend_class (current_class_type, TREE_TYPE (decl),
21280                            /*complain=*/true);
21281     }
21282   /* We are done with the current parameter list.  */
21283   --parser->num_template_parameter_lists;
21284
21285   pop_deferring_access_checks ();
21286
21287   /* Finish up.  */
21288   finish_template_decl (parameter_list);
21289
21290   /* Check the template arguments for a literal operator template.  */
21291   if (decl
21292       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21293       && UDLIT_OPER_P (DECL_NAME (decl)))
21294     {
21295       bool ok = true;
21296       if (parameter_list == NULL_TREE)
21297         ok = false;
21298       else
21299         {
21300           int num_parms = TREE_VEC_LENGTH (parameter_list);
21301           if (num_parms != 1)
21302             ok = false;
21303           else
21304             {
21305               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21306               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21307               if (TREE_TYPE (parm) != char_type_node
21308                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21309                 ok = false;
21310             }
21311         }
21312       if (!ok)
21313         error ("literal operator template %qD has invalid parameter list."
21314                "  Expected non-type template argument pack <char...>",
21315                decl);
21316     }
21317   /* Register member declarations.  */
21318   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21319     finish_member_declaration (decl);
21320   /* For the erroneous case of a template with C linkage, we pushed an
21321      implicit C++ linkage scope; exit that scope now.  */
21322   if (need_lang_pop)
21323     pop_lang_context ();
21324   /* If DECL is a function template, we must return to parse it later.
21325      (Even though there is no definition, there might be default
21326      arguments that need handling.)  */
21327   if (member_p && decl
21328       && (TREE_CODE (decl) == FUNCTION_DECL
21329           || DECL_FUNCTION_TEMPLATE_P (decl)))
21330     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21331 }
21332
21333 /* Perform the deferred access checks from a template-parameter-list.
21334    CHECKS is a TREE_LIST of access checks, as returned by
21335    get_deferred_access_checks.  */
21336
21337 static void
21338 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21339 {
21340   ++processing_template_parmlist;
21341   perform_access_checks (checks);
21342   --processing_template_parmlist;
21343 }
21344
21345 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21346    `function-definition' sequence.  MEMBER_P is true, this declaration
21347    appears in a class scope.
21348
21349    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21350    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21351
21352 static tree
21353 cp_parser_single_declaration (cp_parser* parser,
21354                               VEC (deferred_access_check,gc)* checks,
21355                               bool member_p,
21356                               bool explicit_specialization_p,
21357                               bool* friend_p)
21358 {
21359   int declares_class_or_enum;
21360   tree decl = NULL_TREE;
21361   cp_decl_specifier_seq decl_specifiers;
21362   bool function_definition_p = false;
21363   cp_token *decl_spec_token_start;
21364
21365   /* This function is only used when processing a template
21366      declaration.  */
21367   gcc_assert (innermost_scope_kind () == sk_template_parms
21368               || innermost_scope_kind () == sk_template_spec);
21369
21370   /* Defer access checks until we know what is being declared.  */
21371   push_deferring_access_checks (dk_deferred);
21372
21373   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21374      alternative.  */
21375   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21376   cp_parser_decl_specifier_seq (parser,
21377                                 CP_PARSER_FLAGS_OPTIONAL,
21378                                 &decl_specifiers,
21379                                 &declares_class_or_enum);
21380   if (friend_p)
21381     *friend_p = cp_parser_friend_p (&decl_specifiers);
21382
21383   /* There are no template typedefs.  */
21384   if (decl_specifiers.specs[(int) ds_typedef])
21385     {
21386       error_at (decl_spec_token_start->location,
21387                 "template declaration of %<typedef%>");
21388       decl = error_mark_node;
21389     }
21390
21391   /* Gather up the access checks that occurred the
21392      decl-specifier-seq.  */
21393   stop_deferring_access_checks ();
21394
21395   /* Check for the declaration of a template class.  */
21396   if (declares_class_or_enum)
21397     {
21398       if (cp_parser_declares_only_class_p (parser))
21399         {
21400           decl = shadow_tag (&decl_specifiers);
21401
21402           /* In this case:
21403
21404                struct C {
21405                  friend template <typename T> struct A<T>::B;
21406                };
21407
21408              A<T>::B will be represented by a TYPENAME_TYPE, and
21409              therefore not recognized by shadow_tag.  */
21410           if (friend_p && *friend_p
21411               && !decl
21412               && decl_specifiers.type
21413               && TYPE_P (decl_specifiers.type))
21414             decl = decl_specifiers.type;
21415
21416           if (decl && decl != error_mark_node)
21417             decl = TYPE_NAME (decl);
21418           else
21419             decl = error_mark_node;
21420
21421           /* Perform access checks for template parameters.  */
21422           cp_parser_perform_template_parameter_access_checks (checks);
21423         }
21424     }
21425
21426   /* Complain about missing 'typename' or other invalid type names.  */
21427   if (!decl_specifiers.any_type_specifiers_p
21428       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21429     {
21430       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21431          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21432          the rest of this declaration.  */
21433       decl = error_mark_node;
21434       goto out;
21435     }
21436
21437   /* If it's not a template class, try for a template function.  If
21438      the next token is a `;', then this declaration does not declare
21439      anything.  But, if there were errors in the decl-specifiers, then
21440      the error might well have come from an attempted class-specifier.
21441      In that case, there's no need to warn about a missing declarator.  */
21442   if (!decl
21443       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21444           || decl_specifiers.type != error_mark_node))
21445     {
21446       decl = cp_parser_init_declarator (parser,
21447                                         &decl_specifiers,
21448                                         checks,
21449                                         /*function_definition_allowed_p=*/true,
21450                                         member_p,
21451                                         declares_class_or_enum,
21452                                         &function_definition_p,
21453                                         NULL);
21454
21455     /* 7.1.1-1 [dcl.stc]
21456
21457        A storage-class-specifier shall not be specified in an explicit
21458        specialization...  */
21459     if (decl
21460         && explicit_specialization_p
21461         && decl_specifiers.storage_class != sc_none)
21462       {
21463         error_at (decl_spec_token_start->location,
21464                   "explicit template specialization cannot have a storage class");
21465         decl = error_mark_node;
21466       }
21467     }
21468
21469   /* Look for a trailing `;' after the declaration.  */
21470   if (!function_definition_p
21471       && (decl == error_mark_node
21472           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21473     cp_parser_skip_to_end_of_block_or_statement (parser);
21474
21475  out:
21476   pop_deferring_access_checks ();
21477
21478   /* Clear any current qualification; whatever comes next is the start
21479      of something new.  */
21480   parser->scope = NULL_TREE;
21481   parser->qualifying_scope = NULL_TREE;
21482   parser->object_scope = NULL_TREE;
21483
21484   return decl;
21485 }
21486
21487 /* Parse a cast-expression that is not the operand of a unary "&".  */
21488
21489 static tree
21490 cp_parser_simple_cast_expression (cp_parser *parser)
21491 {
21492   return cp_parser_cast_expression (parser, /*address_p=*/false,
21493                                     /*cast_p=*/false, NULL);
21494 }
21495
21496 /* Parse a functional cast to TYPE.  Returns an expression
21497    representing the cast.  */
21498
21499 static tree
21500 cp_parser_functional_cast (cp_parser* parser, tree type)
21501 {
21502   VEC(tree,gc) *vec;
21503   tree expression_list;
21504   tree cast;
21505   bool nonconst_p;
21506
21507   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21508     {
21509       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21510       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21511       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21512       if (TREE_CODE (type) == TYPE_DECL)
21513         type = TREE_TYPE (type);
21514       return finish_compound_literal (type, expression_list,
21515                                       tf_warning_or_error);
21516     }
21517
21518
21519   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21520                                                  /*cast_p=*/true,
21521                                                  /*allow_expansion_p=*/true,
21522                                                  /*non_constant_p=*/NULL);
21523   if (vec == NULL)
21524     expression_list = error_mark_node;
21525   else
21526     {
21527       expression_list = build_tree_list_vec (vec);
21528       release_tree_vector (vec);
21529     }
21530
21531   cast = build_functional_cast (type, expression_list,
21532                                 tf_warning_or_error);
21533   /* [expr.const]/1: In an integral constant expression "only type
21534      conversions to integral or enumeration type can be used".  */
21535   if (TREE_CODE (type) == TYPE_DECL)
21536     type = TREE_TYPE (type);
21537   if (cast != error_mark_node
21538       && !cast_valid_in_integral_constant_expression_p (type)
21539       && cp_parser_non_integral_constant_expression (parser,
21540                                                      NIC_CONSTRUCTOR))
21541     return error_mark_node;
21542   return cast;
21543 }
21544
21545 /* Save the tokens that make up the body of a member function defined
21546    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21547    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21548    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21549    for the member function.  */
21550
21551 static tree
21552 cp_parser_save_member_function_body (cp_parser* parser,
21553                                      cp_decl_specifier_seq *decl_specifiers,
21554                                      cp_declarator *declarator,
21555                                      tree attributes)
21556 {
21557   cp_token *first;
21558   cp_token *last;
21559   tree fn;
21560
21561   /* Create the FUNCTION_DECL.  */
21562   fn = grokmethod (decl_specifiers, declarator, attributes);
21563   /* If something went badly wrong, bail out now.  */
21564   if (fn == error_mark_node)
21565     {
21566       /* If there's a function-body, skip it.  */
21567       if (cp_parser_token_starts_function_definition_p
21568           (cp_lexer_peek_token (parser->lexer)))
21569         cp_parser_skip_to_end_of_block_or_statement (parser);
21570       return error_mark_node;
21571     }
21572
21573   /* Remember it, if there default args to post process.  */
21574   cp_parser_save_default_args (parser, fn);
21575
21576   /* Save away the tokens that make up the body of the
21577      function.  */
21578   first = parser->lexer->next_token;
21579   /* We can have braced-init-list mem-initializers before the fn body.  */
21580   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21581     {
21582       cp_lexer_consume_token (parser->lexer);
21583       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21584              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21585         {
21586           /* cache_group will stop after an un-nested { } pair, too.  */
21587           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21588             break;
21589
21590           /* variadic mem-inits have ... after the ')'.  */
21591           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21592             cp_lexer_consume_token (parser->lexer);
21593         }
21594     }
21595   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21596   /* Handle function try blocks.  */
21597   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21598     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21599   last = parser->lexer->next_token;
21600
21601   /* Save away the inline definition; we will process it when the
21602      class is complete.  */
21603   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21604   DECL_PENDING_INLINE_P (fn) = 1;
21605
21606   /* We need to know that this was defined in the class, so that
21607      friend templates are handled correctly.  */
21608   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21609
21610   /* Add FN to the queue of functions to be parsed later.  */
21611   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21612
21613   return fn;
21614 }
21615
21616 /* Save the tokens that make up the in-class initializer for a non-static
21617    data member.  Returns a DEFAULT_ARG.  */
21618
21619 static tree
21620 cp_parser_save_nsdmi (cp_parser* parser)
21621 {
21622   /* Save away the tokens that make up the body of the
21623      function.  */
21624   cp_token *first = parser->lexer->next_token;
21625   cp_token *last;
21626   tree node;
21627
21628   /* Save tokens until the next comma or semicolon.  */
21629   cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21630
21631   last = parser->lexer->next_token;
21632
21633   node = make_node (DEFAULT_ARG);
21634   DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21635   DEFARG_INSTANTIATIONS (node) = NULL;
21636
21637   return node;
21638 }
21639
21640
21641 /* Parse a template-argument-list, as well as the trailing ">" (but
21642    not the opening "<").  See cp_parser_template_argument_list for the
21643    return value.  */
21644
21645 static tree
21646 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21647 {
21648   tree arguments;
21649   tree saved_scope;
21650   tree saved_qualifying_scope;
21651   tree saved_object_scope;
21652   bool saved_greater_than_is_operator_p;
21653   int saved_unevaluated_operand;
21654   int saved_inhibit_evaluation_warnings;
21655
21656   /* [temp.names]
21657
21658      When parsing a template-id, the first non-nested `>' is taken as
21659      the end of the template-argument-list rather than a greater-than
21660      operator.  */
21661   saved_greater_than_is_operator_p
21662     = parser->greater_than_is_operator_p;
21663   parser->greater_than_is_operator_p = false;
21664   /* Parsing the argument list may modify SCOPE, so we save it
21665      here.  */
21666   saved_scope = parser->scope;
21667   saved_qualifying_scope = parser->qualifying_scope;
21668   saved_object_scope = parser->object_scope;
21669   /* We need to evaluate the template arguments, even though this
21670      template-id may be nested within a "sizeof".  */
21671   saved_unevaluated_operand = cp_unevaluated_operand;
21672   cp_unevaluated_operand = 0;
21673   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21674   c_inhibit_evaluation_warnings = 0;
21675   /* Parse the template-argument-list itself.  */
21676   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21677       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21678     arguments = NULL_TREE;
21679   else
21680     arguments = cp_parser_template_argument_list (parser);
21681   /* Look for the `>' that ends the template-argument-list. If we find
21682      a '>>' instead, it's probably just a typo.  */
21683   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21684     {
21685       if (cxx_dialect != cxx98)
21686         {
21687           /* In C++0x, a `>>' in a template argument list or cast
21688              expression is considered to be two separate `>'
21689              tokens. So, change the current token to a `>', but don't
21690              consume it: it will be consumed later when the outer
21691              template argument list (or cast expression) is parsed.
21692              Note that this replacement of `>' for `>>' is necessary
21693              even if we are parsing tentatively: in the tentative
21694              case, after calling
21695              cp_parser_enclosed_template_argument_list we will always
21696              throw away all of the template arguments and the first
21697              closing `>', either because the template argument list
21698              was erroneous or because we are replacing those tokens
21699              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21700              not have been thrown away) is needed either to close an
21701              outer template argument list or to complete a new-style
21702              cast.  */
21703           cp_token *token = cp_lexer_peek_token (parser->lexer);
21704           token->type = CPP_GREATER;
21705         }
21706       else if (!saved_greater_than_is_operator_p)
21707         {
21708           /* If we're in a nested template argument list, the '>>' has
21709             to be a typo for '> >'. We emit the error message, but we
21710             continue parsing and we push a '>' as next token, so that
21711             the argument list will be parsed correctly.  Note that the
21712             global source location is still on the token before the
21713             '>>', so we need to say explicitly where we want it.  */
21714           cp_token *token = cp_lexer_peek_token (parser->lexer);
21715           error_at (token->location, "%<>>%> should be %<> >%> "
21716                     "within a nested template argument list");
21717
21718           token->type = CPP_GREATER;
21719         }
21720       else
21721         {
21722           /* If this is not a nested template argument list, the '>>'
21723             is a typo for '>'. Emit an error message and continue.
21724             Same deal about the token location, but here we can get it
21725             right by consuming the '>>' before issuing the diagnostic.  */
21726           cp_token *token = cp_lexer_consume_token (parser->lexer);
21727           error_at (token->location,
21728                     "spurious %<>>%>, use %<>%> to terminate "
21729                     "a template argument list");
21730         }
21731     }
21732   else
21733     cp_parser_skip_to_end_of_template_parameter_list (parser);
21734   /* The `>' token might be a greater-than operator again now.  */
21735   parser->greater_than_is_operator_p
21736     = saved_greater_than_is_operator_p;
21737   /* Restore the SAVED_SCOPE.  */
21738   parser->scope = saved_scope;
21739   parser->qualifying_scope = saved_qualifying_scope;
21740   parser->object_scope = saved_object_scope;
21741   cp_unevaluated_operand = saved_unevaluated_operand;
21742   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21743
21744   return arguments;
21745 }
21746
21747 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21748    arguments, or the body of the function have not yet been parsed,
21749    parse them now.  */
21750
21751 static void
21752 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21753 {
21754   timevar_push (TV_PARSE_INMETH);
21755   /* If this member is a template, get the underlying
21756      FUNCTION_DECL.  */
21757   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21758     member_function = DECL_TEMPLATE_RESULT (member_function);
21759
21760   /* There should not be any class definitions in progress at this
21761      point; the bodies of members are only parsed outside of all class
21762      definitions.  */
21763   gcc_assert (parser->num_classes_being_defined == 0);
21764   /* While we're parsing the member functions we might encounter more
21765      classes.  We want to handle them right away, but we don't want
21766      them getting mixed up with functions that are currently in the
21767      queue.  */
21768   push_unparsed_function_queues (parser);
21769
21770   /* Make sure that any template parameters are in scope.  */
21771   maybe_begin_member_template_processing (member_function);
21772
21773   /* If the body of the function has not yet been parsed, parse it
21774      now.  */
21775   if (DECL_PENDING_INLINE_P (member_function))
21776     {
21777       tree function_scope;
21778       cp_token_cache *tokens;
21779
21780       /* The function is no longer pending; we are processing it.  */
21781       tokens = DECL_PENDING_INLINE_INFO (member_function);
21782       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21783       DECL_PENDING_INLINE_P (member_function) = 0;
21784
21785       /* If this is a local class, enter the scope of the containing
21786          function.  */
21787       function_scope = current_function_decl;
21788       if (function_scope)
21789         push_function_context ();
21790
21791       /* Push the body of the function onto the lexer stack.  */
21792       cp_parser_push_lexer_for_tokens (parser, tokens);
21793
21794       /* Let the front end know that we going to be defining this
21795          function.  */
21796       start_preparsed_function (member_function, NULL_TREE,
21797                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21798
21799       /* Don't do access checking if it is a templated function.  */
21800       if (processing_template_decl)
21801         push_deferring_access_checks (dk_no_check);
21802
21803       /* Now, parse the body of the function.  */
21804       cp_parser_function_definition_after_declarator (parser,
21805                                                       /*inline_p=*/true);
21806
21807       if (processing_template_decl)
21808         pop_deferring_access_checks ();
21809
21810       /* Leave the scope of the containing function.  */
21811       if (function_scope)
21812         pop_function_context ();
21813       cp_parser_pop_lexer (parser);
21814     }
21815
21816   /* Remove any template parameters from the symbol table.  */
21817   maybe_end_member_template_processing ();
21818
21819   /* Restore the queue.  */
21820   pop_unparsed_function_queues (parser);
21821   timevar_pop (TV_PARSE_INMETH);
21822 }
21823
21824 /* If DECL contains any default args, remember it on the unparsed
21825    functions queue.  */
21826
21827 static void
21828 cp_parser_save_default_args (cp_parser* parser, tree decl)
21829 {
21830   tree probe;
21831
21832   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21833        probe;
21834        probe = TREE_CHAIN (probe))
21835     if (TREE_PURPOSE (probe))
21836       {
21837         cp_default_arg_entry *entry
21838           = VEC_safe_push (cp_default_arg_entry, gc,
21839                            unparsed_funs_with_default_args, NULL);
21840         entry->class_type = current_class_type;
21841         entry->decl = decl;
21842         break;
21843       }
21844 }
21845
21846 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21847    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21848    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21849    from the parameter-type-list.  */
21850
21851 static tree
21852 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21853                                       tree default_arg, tree parmtype)
21854 {
21855   cp_token_cache *tokens;
21856   tree parsed_arg;
21857   bool dummy;
21858
21859   if (default_arg == error_mark_node)
21860     return error_mark_node;
21861
21862   /* Push the saved tokens for the default argument onto the parser's
21863      lexer stack.  */
21864   tokens = DEFARG_TOKENS (default_arg);
21865   cp_parser_push_lexer_for_tokens (parser, tokens);
21866
21867   start_lambda_scope (decl);
21868
21869   /* Parse the default argument.  */
21870   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21871   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21872     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21873
21874   finish_lambda_scope ();
21875
21876   if (!processing_template_decl)
21877     {
21878       /* In a non-template class, check conversions now.  In a template,
21879          we'll wait and instantiate these as needed.  */
21880       if (TREE_CODE (decl) == PARM_DECL)
21881         parsed_arg = check_default_argument (parmtype, parsed_arg);
21882       else
21883         {
21884           int flags = LOOKUP_IMPLICIT;
21885           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21886               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21887             flags = LOOKUP_NORMAL;
21888           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21889         }
21890     }
21891
21892   /* If the token stream has not been completely used up, then
21893      there was extra junk after the end of the default
21894      argument.  */
21895   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21896     {
21897       if (TREE_CODE (decl) == PARM_DECL)
21898         cp_parser_error (parser, "expected %<,%>");
21899       else
21900         cp_parser_error (parser, "expected %<;%>");
21901     }
21902
21903   /* Revert to the main lexer.  */
21904   cp_parser_pop_lexer (parser);
21905
21906   return parsed_arg;
21907 }
21908
21909 /* FIELD is a non-static data member with an initializer which we saved for
21910    later; parse it now.  */
21911
21912 static void
21913 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21914 {
21915   tree def;
21916
21917   push_unparsed_function_queues (parser);
21918   def = cp_parser_late_parse_one_default_arg (parser, field,
21919                                               DECL_INITIAL (field),
21920                                               NULL_TREE);
21921   pop_unparsed_function_queues (parser);
21922
21923   DECL_INITIAL (field) = def;
21924 }
21925
21926 /* FN is a FUNCTION_DECL which may contains a parameter with an
21927    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21928    assumes that the current scope is the scope in which the default
21929    argument should be processed.  */
21930
21931 static void
21932 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21933 {
21934   bool saved_local_variables_forbidden_p;
21935   tree parm, parmdecl;
21936
21937   /* While we're parsing the default args, we might (due to the
21938      statement expression extension) encounter more classes.  We want
21939      to handle them right away, but we don't want them getting mixed
21940      up with default args that are currently in the queue.  */
21941   push_unparsed_function_queues (parser);
21942
21943   /* Local variable names (and the `this' keyword) may not appear
21944      in a default argument.  */
21945   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21946   parser->local_variables_forbidden_p = true;
21947
21948   push_defarg_context (fn);
21949
21950   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21951          parmdecl = DECL_ARGUMENTS (fn);
21952        parm && parm != void_list_node;
21953        parm = TREE_CHAIN (parm),
21954          parmdecl = DECL_CHAIN (parmdecl))
21955     {
21956       tree default_arg = TREE_PURPOSE (parm);
21957       tree parsed_arg;
21958       VEC(tree,gc) *insts;
21959       tree copy;
21960       unsigned ix;
21961
21962       if (!default_arg)
21963         continue;
21964
21965       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21966         /* This can happen for a friend declaration for a function
21967            already declared with default arguments.  */
21968         continue;
21969
21970       parsed_arg
21971         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21972                                                 default_arg,
21973                                                 TREE_VALUE (parm));
21974       if (parsed_arg == error_mark_node)
21975         {
21976           continue;
21977         }
21978
21979       TREE_PURPOSE (parm) = parsed_arg;
21980
21981       /* Update any instantiations we've already created.  */
21982       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21983            VEC_iterate (tree, insts, ix, copy); ix++)
21984         TREE_PURPOSE (copy) = parsed_arg;
21985     }
21986
21987   pop_defarg_context ();
21988
21989   /* Make sure no default arg is missing.  */
21990   check_default_args (fn);
21991
21992   /* Restore the state of local_variables_forbidden_p.  */
21993   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21994
21995   /* Restore the queue.  */
21996   pop_unparsed_function_queues (parser);
21997 }
21998
21999 /* Parse the operand of `sizeof' (or a similar operator).  Returns
22000    either a TYPE or an expression, depending on the form of the
22001    input.  The KEYWORD indicates which kind of expression we have
22002    encountered.  */
22003
22004 static tree
22005 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
22006 {
22007   tree expr = NULL_TREE;
22008   const char *saved_message;
22009   char *tmp;
22010   bool saved_integral_constant_expression_p;
22011   bool saved_non_integral_constant_expression_p;
22012   bool pack_expansion_p = false;
22013
22014   /* Types cannot be defined in a `sizeof' expression.  Save away the
22015      old message.  */
22016   saved_message = parser->type_definition_forbidden_message;
22017   /* And create the new one.  */
22018   tmp = concat ("types may not be defined in %<",
22019                 IDENTIFIER_POINTER (ridpointers[keyword]),
22020                 "%> expressions", NULL);
22021   parser->type_definition_forbidden_message = tmp;
22022
22023   /* The restrictions on constant-expressions do not apply inside
22024      sizeof expressions.  */
22025   saved_integral_constant_expression_p
22026     = parser->integral_constant_expression_p;
22027   saved_non_integral_constant_expression_p
22028     = parser->non_integral_constant_expression_p;
22029   parser->integral_constant_expression_p = false;
22030
22031   /* If it's a `...', then we are computing the length of a parameter
22032      pack.  */
22033   if (keyword == RID_SIZEOF
22034       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
22035     {
22036       /* Consume the `...'.  */
22037       cp_lexer_consume_token (parser->lexer);
22038       maybe_warn_variadic_templates ();
22039
22040       /* Note that this is an expansion.  */
22041       pack_expansion_p = true;
22042     }
22043
22044   /* Do not actually evaluate the expression.  */
22045   ++cp_unevaluated_operand;
22046   ++c_inhibit_evaluation_warnings;
22047   /* If it's a `(', then we might be looking at the type-id
22048      construction.  */
22049   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
22050     {
22051       tree type;
22052       bool saved_in_type_id_in_expr_p;
22053
22054       /* We can't be sure yet whether we're looking at a type-id or an
22055          expression.  */
22056       cp_parser_parse_tentatively (parser);
22057       /* Consume the `('.  */
22058       cp_lexer_consume_token (parser->lexer);
22059       /* Parse the type-id.  */
22060       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
22061       parser->in_type_id_in_expr_p = true;
22062       type = cp_parser_type_id (parser);
22063       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
22064       /* Now, look for the trailing `)'.  */
22065       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22066       /* If all went well, then we're done.  */
22067       if (cp_parser_parse_definitely (parser))
22068         {
22069           cp_decl_specifier_seq decl_specs;
22070
22071           /* Build a trivial decl-specifier-seq.  */
22072           clear_decl_specs (&decl_specs);
22073           decl_specs.type = type;
22074
22075           /* Call grokdeclarator to figure out what type this is.  */
22076           expr = grokdeclarator (NULL,
22077                                  &decl_specs,
22078                                  TYPENAME,
22079                                  /*initialized=*/0,
22080                                  /*attrlist=*/NULL);
22081         }
22082     }
22083
22084   /* If the type-id production did not work out, then we must be
22085      looking at the unary-expression production.  */
22086   if (!expr)
22087     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
22088                                        /*cast_p=*/false, NULL);
22089
22090   if (pack_expansion_p)
22091     /* Build a pack expansion. */
22092     expr = make_pack_expansion (expr);
22093
22094   /* Go back to evaluating expressions.  */
22095   --cp_unevaluated_operand;
22096   --c_inhibit_evaluation_warnings;
22097
22098   /* Free the message we created.  */
22099   free (tmp);
22100   /* And restore the old one.  */
22101   parser->type_definition_forbidden_message = saved_message;
22102   parser->integral_constant_expression_p
22103     = saved_integral_constant_expression_p;
22104   parser->non_integral_constant_expression_p
22105     = saved_non_integral_constant_expression_p;
22106
22107   return expr;
22108 }
22109
22110 /* If the current declaration has no declarator, return true.  */
22111
22112 static bool
22113 cp_parser_declares_only_class_p (cp_parser *parser)
22114 {
22115   /* If the next token is a `;' or a `,' then there is no
22116      declarator.  */
22117   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
22118           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
22119 }
22120
22121 /* Update the DECL_SPECS to reflect the storage class indicated by
22122    KEYWORD.  */
22123
22124 static void
22125 cp_parser_set_storage_class (cp_parser *parser,
22126                              cp_decl_specifier_seq *decl_specs,
22127                              enum rid keyword,
22128                              location_t location)
22129 {
22130   cp_storage_class storage_class;
22131
22132   if (parser->in_unbraced_linkage_specification_p)
22133     {
22134       error_at (location, "invalid use of %qD in linkage specification",
22135                 ridpointers[keyword]);
22136       return;
22137     }
22138   else if (decl_specs->storage_class != sc_none)
22139     {
22140       decl_specs->conflicting_specifiers_p = true;
22141       return;
22142     }
22143
22144   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
22145       && decl_specs->specs[(int) ds_thread])
22146     {
22147       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
22148       decl_specs->specs[(int) ds_thread] = 0;
22149     }
22150
22151   switch (keyword)
22152     {
22153     case RID_AUTO:
22154       storage_class = sc_auto;
22155       break;
22156     case RID_REGISTER:
22157       storage_class = sc_register;
22158       break;
22159     case RID_STATIC:
22160       storage_class = sc_static;
22161       break;
22162     case RID_EXTERN:
22163       storage_class = sc_extern;
22164       break;
22165     case RID_MUTABLE:
22166       storage_class = sc_mutable;
22167       break;
22168     default:
22169       gcc_unreachable ();
22170     }
22171   decl_specs->storage_class = storage_class;
22172
22173   /* A storage class specifier cannot be applied alongside a typedef 
22174      specifier. If there is a typedef specifier present then set 
22175      conflicting_specifiers_p which will trigger an error later
22176      on in grokdeclarator. */
22177   if (decl_specs->specs[(int)ds_typedef])
22178     decl_specs->conflicting_specifiers_p = true;
22179 }
22180
22181 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22182    is true, the type is a class or enum definition.  */
22183
22184 static void
22185 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22186                               tree type_spec,
22187                               location_t location,
22188                               bool type_definition_p)
22189 {
22190   decl_specs->any_specifiers_p = true;
22191
22192   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22193      (with, for example, in "typedef int wchar_t;") we remember that
22194      this is what happened.  In system headers, we ignore these
22195      declarations so that G++ can work with system headers that are not
22196      C++-safe.  */
22197   if (decl_specs->specs[(int) ds_typedef]
22198       && !type_definition_p
22199       && (type_spec == boolean_type_node
22200           || type_spec == char16_type_node
22201           || type_spec == char32_type_node
22202           || type_spec == wchar_type_node)
22203       && (decl_specs->type
22204           || decl_specs->specs[(int) ds_long]
22205           || decl_specs->specs[(int) ds_short]
22206           || decl_specs->specs[(int) ds_unsigned]
22207           || decl_specs->specs[(int) ds_signed]))
22208     {
22209       decl_specs->redefined_builtin_type = type_spec;
22210       if (!decl_specs->type)
22211         {
22212           decl_specs->type = type_spec;
22213           decl_specs->type_definition_p = false;
22214           decl_specs->type_location = location;
22215         }
22216     }
22217   else if (decl_specs->type)
22218     decl_specs->multiple_types_p = true;
22219   else
22220     {
22221       decl_specs->type = type_spec;
22222       decl_specs->type_definition_p = type_definition_p;
22223       decl_specs->redefined_builtin_type = NULL_TREE;
22224       decl_specs->type_location = location;
22225     }
22226 }
22227
22228 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22229    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22230
22231 static bool
22232 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22233 {
22234   return decl_specifiers->specs[(int) ds_friend] != 0;
22235 }
22236
22237 /* Issue an error message indicating that TOKEN_DESC was expected.
22238    If KEYWORD is true, it indicated this function is called by
22239    cp_parser_require_keword and the required token can only be
22240    a indicated keyword. */
22241
22242 static void
22243 cp_parser_required_error (cp_parser *parser,
22244                           required_token token_desc,
22245                           bool keyword)
22246 {
22247   switch (token_desc)
22248     {
22249       case RT_NEW:
22250         cp_parser_error (parser, "expected %<new%>");
22251         return;
22252       case RT_DELETE:
22253         cp_parser_error (parser, "expected %<delete%>");
22254         return;
22255       case RT_RETURN:
22256         cp_parser_error (parser, "expected %<return%>");
22257         return;
22258       case RT_WHILE:
22259         cp_parser_error (parser, "expected %<while%>");
22260         return;
22261       case RT_EXTERN:
22262         cp_parser_error (parser, "expected %<extern%>");
22263         return;
22264       case RT_STATIC_ASSERT:
22265         cp_parser_error (parser, "expected %<static_assert%>");
22266         return;
22267       case RT_DECLTYPE:
22268         cp_parser_error (parser, "expected %<decltype%>");
22269         return;
22270       case RT_OPERATOR:
22271         cp_parser_error (parser, "expected %<operator%>");
22272         return;
22273       case RT_CLASS:
22274         cp_parser_error (parser, "expected %<class%>");
22275         return;
22276       case RT_TEMPLATE:
22277         cp_parser_error (parser, "expected %<template%>");
22278         return;
22279       case RT_NAMESPACE:
22280         cp_parser_error (parser, "expected %<namespace%>");
22281         return;
22282       case RT_USING:
22283         cp_parser_error (parser, "expected %<using%>");
22284         return;
22285       case RT_ASM:
22286         cp_parser_error (parser, "expected %<asm%>");
22287         return;
22288       case RT_TRY:
22289         cp_parser_error (parser, "expected %<try%>");
22290         return;
22291       case RT_CATCH:
22292         cp_parser_error (parser, "expected %<catch%>");
22293         return;
22294       case RT_THROW:
22295         cp_parser_error (parser, "expected %<throw%>");
22296         return;
22297       case RT_LABEL:
22298         cp_parser_error (parser, "expected %<__label__%>");
22299         return;
22300       case RT_AT_TRY:
22301         cp_parser_error (parser, "expected %<@try%>");
22302         return;
22303       case RT_AT_SYNCHRONIZED:
22304         cp_parser_error (parser, "expected %<@synchronized%>");
22305         return;
22306       case RT_AT_THROW:
22307         cp_parser_error (parser, "expected %<@throw%>");
22308         return;
22309       case RT_TRANSACTION_ATOMIC:
22310         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22311         return;
22312       case RT_TRANSACTION_RELAXED:
22313         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22314         return;
22315       default:
22316         break;
22317     }
22318   if (!keyword)
22319     {
22320       switch (token_desc)
22321         {
22322           case RT_SEMICOLON:
22323             cp_parser_error (parser, "expected %<;%>");
22324             return;
22325           case RT_OPEN_PAREN:
22326             cp_parser_error (parser, "expected %<(%>");
22327             return;
22328           case RT_CLOSE_BRACE:
22329             cp_parser_error (parser, "expected %<}%>");
22330             return;
22331           case RT_OPEN_BRACE:
22332             cp_parser_error (parser, "expected %<{%>");
22333             return;
22334           case RT_CLOSE_SQUARE:
22335             cp_parser_error (parser, "expected %<]%>");
22336             return;
22337           case RT_OPEN_SQUARE:
22338             cp_parser_error (parser, "expected %<[%>");
22339             return;
22340           case RT_COMMA:
22341             cp_parser_error (parser, "expected %<,%>");
22342             return;
22343           case RT_SCOPE:
22344             cp_parser_error (parser, "expected %<::%>");
22345             return;
22346           case RT_LESS:
22347             cp_parser_error (parser, "expected %<<%>");
22348             return;
22349           case RT_GREATER:
22350             cp_parser_error (parser, "expected %<>%>");
22351             return;
22352           case RT_EQ:
22353             cp_parser_error (parser, "expected %<=%>");
22354             return;
22355           case RT_ELLIPSIS:
22356             cp_parser_error (parser, "expected %<...%>");
22357             return;
22358           case RT_MULT:
22359             cp_parser_error (parser, "expected %<*%>");
22360             return;
22361           case RT_COMPL:
22362             cp_parser_error (parser, "expected %<~%>");
22363             return;
22364           case RT_COLON:
22365             cp_parser_error (parser, "expected %<:%>");
22366             return;
22367           case RT_COLON_SCOPE:
22368             cp_parser_error (parser, "expected %<:%> or %<::%>");
22369             return;
22370           case RT_CLOSE_PAREN:
22371             cp_parser_error (parser, "expected %<)%>");
22372             return;
22373           case RT_COMMA_CLOSE_PAREN:
22374             cp_parser_error (parser, "expected %<,%> or %<)%>");
22375             return;
22376           case RT_PRAGMA_EOL:
22377             cp_parser_error (parser, "expected end of line");
22378             return;
22379           case RT_NAME:
22380             cp_parser_error (parser, "expected identifier");
22381             return;
22382           case RT_SELECT:
22383             cp_parser_error (parser, "expected selection-statement");
22384             return;
22385           case RT_INTERATION:
22386             cp_parser_error (parser, "expected iteration-statement");
22387             return;
22388           case RT_JUMP:
22389             cp_parser_error (parser, "expected jump-statement");
22390             return;
22391           case RT_CLASS_KEY:
22392             cp_parser_error (parser, "expected class-key");
22393             return;
22394           case RT_CLASS_TYPENAME_TEMPLATE:
22395             cp_parser_error (parser,
22396                  "expected %<class%>, %<typename%>, or %<template%>");
22397             return;
22398           default:
22399             gcc_unreachable ();
22400         }
22401     }
22402   else
22403     gcc_unreachable ();
22404 }
22405
22406
22407
22408 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22409    issue an error message indicating that TOKEN_DESC was expected.
22410
22411    Returns the token consumed, if the token had the appropriate type.
22412    Otherwise, returns NULL.  */
22413
22414 static cp_token *
22415 cp_parser_require (cp_parser* parser,
22416                    enum cpp_ttype type,
22417                    required_token token_desc)
22418 {
22419   if (cp_lexer_next_token_is (parser->lexer, type))
22420     return cp_lexer_consume_token (parser->lexer);
22421   else
22422     {
22423       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22424       if (!cp_parser_simulate_error (parser))
22425         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22426       return NULL;
22427     }
22428 }
22429
22430 /* An error message is produced if the next token is not '>'.
22431    All further tokens are skipped until the desired token is
22432    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22433
22434 static void
22435 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22436 {
22437   /* Current level of '< ... >'.  */
22438   unsigned level = 0;
22439   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22440   unsigned nesting_depth = 0;
22441
22442   /* Are we ready, yet?  If not, issue error message.  */
22443   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22444     return;
22445
22446   /* Skip tokens until the desired token is found.  */
22447   while (true)
22448     {
22449       /* Peek at the next token.  */
22450       switch (cp_lexer_peek_token (parser->lexer)->type)
22451         {
22452         case CPP_LESS:
22453           if (!nesting_depth)
22454             ++level;
22455           break;
22456
22457         case CPP_RSHIFT:
22458           if (cxx_dialect == cxx98)
22459             /* C++0x views the `>>' operator as two `>' tokens, but
22460                C++98 does not. */
22461             break;
22462           else if (!nesting_depth && level-- == 0)
22463             {
22464               /* We've hit a `>>' where the first `>' closes the
22465                  template argument list, and the second `>' is
22466                  spurious.  Just consume the `>>' and stop; we've
22467                  already produced at least one error.  */
22468               cp_lexer_consume_token (parser->lexer);
22469               return;
22470             }
22471           /* Fall through for C++0x, so we handle the second `>' in
22472              the `>>'.  */
22473
22474         case CPP_GREATER:
22475           if (!nesting_depth && level-- == 0)
22476             {
22477               /* We've reached the token we want, consume it and stop.  */
22478               cp_lexer_consume_token (parser->lexer);
22479               return;
22480             }
22481           break;
22482
22483         case CPP_OPEN_PAREN:
22484         case CPP_OPEN_SQUARE:
22485           ++nesting_depth;
22486           break;
22487
22488         case CPP_CLOSE_PAREN:
22489         case CPP_CLOSE_SQUARE:
22490           if (nesting_depth-- == 0)
22491             return;
22492           break;
22493
22494         case CPP_EOF:
22495         case CPP_PRAGMA_EOL:
22496         case CPP_SEMICOLON:
22497         case CPP_OPEN_BRACE:
22498         case CPP_CLOSE_BRACE:
22499           /* The '>' was probably forgotten, don't look further.  */
22500           return;
22501
22502         default:
22503           break;
22504         }
22505
22506       /* Consume this token.  */
22507       cp_lexer_consume_token (parser->lexer);
22508     }
22509 }
22510
22511 /* If the next token is the indicated keyword, consume it.  Otherwise,
22512    issue an error message indicating that TOKEN_DESC was expected.
22513
22514    Returns the token consumed, if the token had the appropriate type.
22515    Otherwise, returns NULL.  */
22516
22517 static cp_token *
22518 cp_parser_require_keyword (cp_parser* parser,
22519                            enum rid keyword,
22520                            required_token token_desc)
22521 {
22522   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22523
22524   if (token && token->keyword != keyword)
22525     {
22526       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22527       return NULL;
22528     }
22529
22530   return token;
22531 }
22532
22533 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22534    function-definition.  */
22535
22536 static bool
22537 cp_parser_token_starts_function_definition_p (cp_token* token)
22538 {
22539   return (/* An ordinary function-body begins with an `{'.  */
22540           token->type == CPP_OPEN_BRACE
22541           /* A ctor-initializer begins with a `:'.  */
22542           || token->type == CPP_COLON
22543           /* A function-try-block begins with `try'.  */
22544           || token->keyword == RID_TRY
22545           /* A function-transaction-block begins with `__transaction_atomic'
22546              or `__transaction_relaxed'.  */
22547           || token->keyword == RID_TRANSACTION_ATOMIC
22548           || token->keyword == RID_TRANSACTION_RELAXED
22549           /* The named return value extension begins with `return'.  */
22550           || token->keyword == RID_RETURN);
22551 }
22552
22553 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22554    definition.  */
22555
22556 static bool
22557 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22558 {
22559   cp_token *token;
22560
22561   token = cp_lexer_peek_token (parser->lexer);
22562   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22563 }
22564
22565 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22566    C++0x) ending a template-argument.  */
22567
22568 static bool
22569 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22570 {
22571   cp_token *token;
22572
22573   token = cp_lexer_peek_token (parser->lexer);
22574   return (token->type == CPP_COMMA 
22575           || token->type == CPP_GREATER
22576           || token->type == CPP_ELLIPSIS
22577           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22578 }
22579
22580 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22581    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22582
22583 static bool
22584 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22585                                                      size_t n)
22586 {
22587   cp_token *token;
22588
22589   token = cp_lexer_peek_nth_token (parser->lexer, n);
22590   if (token->type == CPP_LESS)
22591     return true;
22592   /* Check for the sequence `<::' in the original code. It would be lexed as
22593      `[:', where `[' is a digraph, and there is no whitespace before
22594      `:'.  */
22595   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22596     {
22597       cp_token *token2;
22598       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22599       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22600         return true;
22601     }
22602   return false;
22603 }
22604
22605 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22606    or none_type otherwise.  */
22607
22608 static enum tag_types
22609 cp_parser_token_is_class_key (cp_token* token)
22610 {
22611   switch (token->keyword)
22612     {
22613     case RID_CLASS:
22614       return class_type;
22615     case RID_STRUCT:
22616       return record_type;
22617     case RID_UNION:
22618       return union_type;
22619
22620     default:
22621       return none_type;
22622     }
22623 }
22624
22625 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22626
22627 static void
22628 cp_parser_check_class_key (enum tag_types class_key, tree type)
22629 {
22630   if (type == error_mark_node)
22631     return;
22632   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22633     {
22634       permerror (input_location, "%qs tag used in naming %q#T",
22635                  class_key == union_type ? "union"
22636                  : class_key == record_type ? "struct" : "class",
22637                  type);
22638       inform (DECL_SOURCE_LOCATION (TYPE_NAME (type)),
22639               "%q#T was previously declared here", type);
22640     }
22641 }
22642
22643 /* Issue an error message if DECL is redeclared with different
22644    access than its original declaration [class.access.spec/3].
22645    This applies to nested classes and nested class templates.
22646    [class.mem/1].  */
22647
22648 static void
22649 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22650 {
22651   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22652     return;
22653
22654   if ((TREE_PRIVATE (decl)
22655        != (current_access_specifier == access_private_node))
22656       || (TREE_PROTECTED (decl)
22657           != (current_access_specifier == access_protected_node)))
22658     error_at (location, "%qD redeclared with different access", decl);
22659 }
22660
22661 /* Look for the `template' keyword, as a syntactic disambiguator.
22662    Return TRUE iff it is present, in which case it will be
22663    consumed.  */
22664
22665 static bool
22666 cp_parser_optional_template_keyword (cp_parser *parser)
22667 {
22668   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22669     {
22670       /* The `template' keyword can only be used within templates;
22671          outside templates the parser can always figure out what is a
22672          template and what is not.  */
22673       if (!processing_template_decl)
22674         {
22675           cp_token *token = cp_lexer_peek_token (parser->lexer);
22676           error_at (token->location,
22677                     "%<template%> (as a disambiguator) is only allowed "
22678                     "within templates");
22679           /* If this part of the token stream is rescanned, the same
22680              error message would be generated.  So, we purge the token
22681              from the stream.  */
22682           cp_lexer_purge_token (parser->lexer);
22683           return false;
22684         }
22685       else
22686         {
22687           /* Consume the `template' keyword.  */
22688           cp_lexer_consume_token (parser->lexer);
22689           return true;
22690         }
22691     }
22692
22693   return false;
22694 }
22695
22696 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22697    set PARSER->SCOPE, and perform other related actions.  */
22698
22699 static void
22700 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22701 {
22702   int i;
22703   struct tree_check *check_value;
22704   deferred_access_check *chk;
22705   VEC (deferred_access_check,gc) *checks;
22706
22707   /* Get the stored value.  */
22708   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22709   /* Perform any access checks that were deferred.  */
22710   checks = check_value->checks;
22711   if (checks)
22712     {
22713       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22714         perform_or_defer_access_check (chk->binfo,
22715                                        chk->decl,
22716                                        chk->diag_decl);
22717     }
22718   /* Set the scope from the stored value.  */
22719   parser->scope = check_value->value;
22720   parser->qualifying_scope = check_value->qualifying_scope;
22721   parser->object_scope = NULL_TREE;
22722 }
22723
22724 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22725    encounter the end of a block before what we were looking for.  */
22726
22727 static bool
22728 cp_parser_cache_group (cp_parser *parser,
22729                        enum cpp_ttype end,
22730                        unsigned depth)
22731 {
22732   while (true)
22733     {
22734       cp_token *token = cp_lexer_peek_token (parser->lexer);
22735
22736       /* Abort a parenthesized expression if we encounter a semicolon.  */
22737       if ((end == CPP_CLOSE_PAREN || depth == 0)
22738           && token->type == CPP_SEMICOLON)
22739         return true;
22740       /* If we've reached the end of the file, stop.  */
22741       if (token->type == CPP_EOF
22742           || (end != CPP_PRAGMA_EOL
22743               && token->type == CPP_PRAGMA_EOL))
22744         return true;
22745       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22746         /* We've hit the end of an enclosing block, so there's been some
22747            kind of syntax error.  */
22748         return true;
22749
22750       /* If we're caching something finished by a comma (or semicolon),
22751          such as an NSDMI, don't consume the comma.  */
22752       if (end == CPP_COMMA
22753           && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22754         return false;
22755
22756       /* Consume the token.  */
22757       cp_lexer_consume_token (parser->lexer);
22758       /* See if it starts a new group.  */
22759       if (token->type == CPP_OPEN_BRACE)
22760         {
22761           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22762           /* In theory this should probably check end == '}', but
22763              cp_parser_save_member_function_body needs it to exit
22764              after either '}' or ')' when called with ')'.  */
22765           if (depth == 0)
22766             return false;
22767         }
22768       else if (token->type == CPP_OPEN_PAREN)
22769         {
22770           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22771           if (depth == 0 && end == CPP_CLOSE_PAREN)
22772             return false;
22773         }
22774       else if (token->type == CPP_PRAGMA)
22775         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22776       else if (token->type == end)
22777         return false;
22778     }
22779 }
22780
22781 /* Begin parsing tentatively.  We always save tokens while parsing
22782    tentatively so that if the tentative parsing fails we can restore the
22783    tokens.  */
22784
22785 static void
22786 cp_parser_parse_tentatively (cp_parser* parser)
22787 {
22788   /* Enter a new parsing context.  */
22789   parser->context = cp_parser_context_new (parser->context);
22790   /* Begin saving tokens.  */
22791   cp_lexer_save_tokens (parser->lexer);
22792   /* In order to avoid repetitive access control error messages,
22793      access checks are queued up until we are no longer parsing
22794      tentatively.  */
22795   push_deferring_access_checks (dk_deferred);
22796 }
22797
22798 /* Commit to the currently active tentative parse.  */
22799
22800 static void
22801 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22802 {
22803   cp_parser_context *context;
22804   cp_lexer *lexer;
22805
22806   /* Mark all of the levels as committed.  */
22807   lexer = parser->lexer;
22808   for (context = parser->context; context->next; context = context->next)
22809     {
22810       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22811         break;
22812       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22813       while (!cp_lexer_saving_tokens (lexer))
22814         lexer = lexer->next;
22815       cp_lexer_commit_tokens (lexer);
22816     }
22817 }
22818
22819 /* Abort the currently active tentative parse.  All consumed tokens
22820    will be rolled back, and no diagnostics will be issued.  */
22821
22822 static void
22823 cp_parser_abort_tentative_parse (cp_parser* parser)
22824 {
22825   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22826               || errorcount > 0);
22827   cp_parser_simulate_error (parser);
22828   /* Now, pretend that we want to see if the construct was
22829      successfully parsed.  */
22830   cp_parser_parse_definitely (parser);
22831 }
22832
22833 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22834    token stream.  Otherwise, commit to the tokens we have consumed.
22835    Returns true if no error occurred; false otherwise.  */
22836
22837 static bool
22838 cp_parser_parse_definitely (cp_parser* parser)
22839 {
22840   bool error_occurred;
22841   cp_parser_context *context;
22842
22843   /* Remember whether or not an error occurred, since we are about to
22844      destroy that information.  */
22845   error_occurred = cp_parser_error_occurred (parser);
22846   /* Remove the topmost context from the stack.  */
22847   context = parser->context;
22848   parser->context = context->next;
22849   /* If no parse errors occurred, commit to the tentative parse.  */
22850   if (!error_occurred)
22851     {
22852       /* Commit to the tokens read tentatively, unless that was
22853          already done.  */
22854       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22855         cp_lexer_commit_tokens (parser->lexer);
22856
22857       pop_to_parent_deferring_access_checks ();
22858     }
22859   /* Otherwise, if errors occurred, roll back our state so that things
22860      are just as they were before we began the tentative parse.  */
22861   else
22862     {
22863       cp_lexer_rollback_tokens (parser->lexer);
22864       pop_deferring_access_checks ();
22865     }
22866   /* Add the context to the front of the free list.  */
22867   context->next = cp_parser_context_free_list;
22868   cp_parser_context_free_list = context;
22869
22870   return !error_occurred;
22871 }
22872
22873 /* Returns true if we are parsing tentatively and are not committed to
22874    this tentative parse.  */
22875
22876 static bool
22877 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22878 {
22879   return (cp_parser_parsing_tentatively (parser)
22880           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22881 }
22882
22883 /* Returns nonzero iff an error has occurred during the most recent
22884    tentative parse.  */
22885
22886 static bool
22887 cp_parser_error_occurred (cp_parser* parser)
22888 {
22889   return (cp_parser_parsing_tentatively (parser)
22890           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22891 }
22892
22893 /* Returns nonzero if GNU extensions are allowed.  */
22894
22895 static bool
22896 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22897 {
22898   return parser->allow_gnu_extensions_p;
22899 }
22900 \f
22901 /* Objective-C++ Productions */
22902
22903
22904 /* Parse an Objective-C expression, which feeds into a primary-expression
22905    above.
22906
22907    objc-expression:
22908      objc-message-expression
22909      objc-string-literal
22910      objc-encode-expression
22911      objc-protocol-expression
22912      objc-selector-expression
22913
22914   Returns a tree representation of the expression.  */
22915
22916 static tree
22917 cp_parser_objc_expression (cp_parser* parser)
22918 {
22919   /* Try to figure out what kind of declaration is present.  */
22920   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22921
22922   switch (kwd->type)
22923     {
22924     case CPP_OPEN_SQUARE:
22925       return cp_parser_objc_message_expression (parser);
22926
22927     case CPP_OBJC_STRING:
22928       kwd = cp_lexer_consume_token (parser->lexer);
22929       return objc_build_string_object (kwd->u.value);
22930
22931     case CPP_KEYWORD:
22932       switch (kwd->keyword)
22933         {
22934         case RID_AT_ENCODE:
22935           return cp_parser_objc_encode_expression (parser);
22936
22937         case RID_AT_PROTOCOL:
22938           return cp_parser_objc_protocol_expression (parser);
22939
22940         case RID_AT_SELECTOR:
22941           return cp_parser_objc_selector_expression (parser);
22942
22943         default:
22944           break;
22945         }
22946     default:
22947       error_at (kwd->location,
22948                 "misplaced %<@%D%> Objective-C++ construct",
22949                 kwd->u.value);
22950       cp_parser_skip_to_end_of_block_or_statement (parser);
22951     }
22952
22953   return error_mark_node;
22954 }
22955
22956 /* Parse an Objective-C message expression.
22957
22958    objc-message-expression:
22959      [ objc-message-receiver objc-message-args ]
22960
22961    Returns a representation of an Objective-C message.  */
22962
22963 static tree
22964 cp_parser_objc_message_expression (cp_parser* parser)
22965 {
22966   tree receiver, messageargs;
22967
22968   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
22969   receiver = cp_parser_objc_message_receiver (parser);
22970   messageargs = cp_parser_objc_message_args (parser);
22971   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22972
22973   return objc_build_message_expr (receiver, messageargs);
22974 }
22975
22976 /* Parse an objc-message-receiver.
22977
22978    objc-message-receiver:
22979      expression
22980      simple-type-specifier
22981
22982   Returns a representation of the type or expression.  */
22983
22984 static tree
22985 cp_parser_objc_message_receiver (cp_parser* parser)
22986 {
22987   tree rcv;
22988
22989   /* An Objective-C message receiver may be either (1) a type
22990      or (2) an expression.  */
22991   cp_parser_parse_tentatively (parser);
22992   rcv = cp_parser_expression (parser, false, NULL);
22993
22994   if (cp_parser_parse_definitely (parser))
22995     return rcv;
22996
22997   rcv = cp_parser_simple_type_specifier (parser,
22998                                          /*decl_specs=*/NULL,
22999                                          CP_PARSER_FLAGS_NONE);
23000
23001   return objc_get_class_reference (rcv);
23002 }
23003
23004 /* Parse the arguments and selectors comprising an Objective-C message.
23005
23006    objc-message-args:
23007      objc-selector
23008      objc-selector-args
23009      objc-selector-args , objc-comma-args
23010
23011    objc-selector-args:
23012      objc-selector [opt] : assignment-expression
23013      objc-selector-args objc-selector [opt] : assignment-expression
23014
23015    objc-comma-args:
23016      assignment-expression
23017      objc-comma-args , assignment-expression
23018
23019    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
23020    selector arguments and TREE_VALUE containing a list of comma
23021    arguments.  */
23022
23023 static tree
23024 cp_parser_objc_message_args (cp_parser* parser)
23025 {
23026   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
23027   bool maybe_unary_selector_p = true;
23028   cp_token *token = cp_lexer_peek_token (parser->lexer);
23029
23030   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23031     {
23032       tree selector = NULL_TREE, arg;
23033
23034       if (token->type != CPP_COLON)
23035         selector = cp_parser_objc_selector (parser);
23036
23037       /* Detect if we have a unary selector.  */
23038       if (maybe_unary_selector_p
23039           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23040         return build_tree_list (selector, NULL_TREE);
23041
23042       maybe_unary_selector_p = false;
23043       cp_parser_require (parser, CPP_COLON, RT_COLON);
23044       arg = cp_parser_assignment_expression (parser, false, NULL);
23045
23046       sel_args
23047         = chainon (sel_args,
23048                    build_tree_list (selector, arg));
23049
23050       token = cp_lexer_peek_token (parser->lexer);
23051     }
23052
23053   /* Handle non-selector arguments, if any. */
23054   while (token->type == CPP_COMMA)
23055     {
23056       tree arg;
23057
23058       cp_lexer_consume_token (parser->lexer);
23059       arg = cp_parser_assignment_expression (parser, false, NULL);
23060
23061       addl_args
23062         = chainon (addl_args,
23063                    build_tree_list (NULL_TREE, arg));
23064
23065       token = cp_lexer_peek_token (parser->lexer);
23066     }
23067
23068   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
23069     {
23070       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
23071       return build_tree_list (error_mark_node, error_mark_node);
23072     }
23073
23074   return build_tree_list (sel_args, addl_args);
23075 }
23076
23077 /* Parse an Objective-C encode expression.
23078
23079    objc-encode-expression:
23080      @encode objc-typename
23081
23082    Returns an encoded representation of the type argument.  */
23083
23084 static tree
23085 cp_parser_objc_encode_expression (cp_parser* parser)
23086 {
23087   tree type;
23088   cp_token *token;
23089
23090   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
23091   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23092   token = cp_lexer_peek_token (parser->lexer);
23093   type = complete_type (cp_parser_type_id (parser));
23094   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23095
23096   if (!type)
23097     {
23098       error_at (token->location, 
23099                 "%<@encode%> must specify a type as an argument");
23100       return error_mark_node;
23101     }
23102
23103   /* This happens if we find @encode(T) (where T is a template
23104      typename or something dependent on a template typename) when
23105      parsing a template.  In that case, we can't compile it
23106      immediately, but we rather create an AT_ENCODE_EXPR which will
23107      need to be instantiated when the template is used.
23108   */
23109   if (dependent_type_p (type))
23110     {
23111       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
23112       TREE_READONLY (value) = 1;
23113       return value;
23114     }
23115
23116   return objc_build_encode_expr (type);
23117 }
23118
23119 /* Parse an Objective-C @defs expression.  */
23120
23121 static tree
23122 cp_parser_objc_defs_expression (cp_parser *parser)
23123 {
23124   tree name;
23125
23126   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
23127   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23128   name = cp_parser_identifier (parser);
23129   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23130
23131   return objc_get_class_ivars (name);
23132 }
23133
23134 /* Parse an Objective-C protocol expression.
23135
23136   objc-protocol-expression:
23137     @protocol ( identifier )
23138
23139   Returns a representation of the protocol expression.  */
23140
23141 static tree
23142 cp_parser_objc_protocol_expression (cp_parser* parser)
23143 {
23144   tree proto;
23145
23146   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23147   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23148   proto = cp_parser_identifier (parser);
23149   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23150
23151   return objc_build_protocol_expr (proto);
23152 }
23153
23154 /* Parse an Objective-C selector expression.
23155
23156    objc-selector-expression:
23157      @selector ( objc-method-signature )
23158
23159    objc-method-signature:
23160      objc-selector
23161      objc-selector-seq
23162
23163    objc-selector-seq:
23164      objc-selector :
23165      objc-selector-seq objc-selector :
23166
23167   Returns a representation of the method selector.  */
23168
23169 static tree
23170 cp_parser_objc_selector_expression (cp_parser* parser)
23171 {
23172   tree sel_seq = NULL_TREE;
23173   bool maybe_unary_selector_p = true;
23174   cp_token *token;
23175   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23176
23177   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23178   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23179   token = cp_lexer_peek_token (parser->lexer);
23180
23181   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23182          || token->type == CPP_SCOPE)
23183     {
23184       tree selector = NULL_TREE;
23185
23186       if (token->type != CPP_COLON
23187           || token->type == CPP_SCOPE)
23188         selector = cp_parser_objc_selector (parser);
23189
23190       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23191           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23192         {
23193           /* Detect if we have a unary selector.  */
23194           if (maybe_unary_selector_p)
23195             {
23196               sel_seq = selector;
23197               goto finish_selector;
23198             }
23199           else
23200             {
23201               cp_parser_error (parser, "expected %<:%>");
23202             }
23203         }
23204       maybe_unary_selector_p = false;
23205       token = cp_lexer_consume_token (parser->lexer);
23206
23207       if (token->type == CPP_SCOPE)
23208         {
23209           sel_seq
23210             = chainon (sel_seq,
23211                        build_tree_list (selector, NULL_TREE));
23212           sel_seq
23213             = chainon (sel_seq,
23214                        build_tree_list (NULL_TREE, NULL_TREE));
23215         }
23216       else
23217         sel_seq
23218           = chainon (sel_seq,
23219                      build_tree_list (selector, NULL_TREE));
23220
23221       token = cp_lexer_peek_token (parser->lexer);
23222     }
23223
23224  finish_selector:
23225   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23226
23227   return objc_build_selector_expr (loc, sel_seq);
23228 }
23229
23230 /* Parse a list of identifiers.
23231
23232    objc-identifier-list:
23233      identifier
23234      objc-identifier-list , identifier
23235
23236    Returns a TREE_LIST of identifier nodes.  */
23237
23238 static tree
23239 cp_parser_objc_identifier_list (cp_parser* parser)
23240 {
23241   tree identifier;
23242   tree list;
23243   cp_token *sep;
23244
23245   identifier = cp_parser_identifier (parser);
23246   if (identifier == error_mark_node)
23247     return error_mark_node;      
23248
23249   list = build_tree_list (NULL_TREE, identifier);
23250   sep = cp_lexer_peek_token (parser->lexer);
23251
23252   while (sep->type == CPP_COMMA)
23253     {
23254       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23255       identifier = cp_parser_identifier (parser);
23256       if (identifier == error_mark_node)
23257         return list;
23258
23259       list = chainon (list, build_tree_list (NULL_TREE,
23260                                              identifier));
23261       sep = cp_lexer_peek_token (parser->lexer);
23262     }
23263   
23264   return list;
23265 }
23266
23267 /* Parse an Objective-C alias declaration.
23268
23269    objc-alias-declaration:
23270      @compatibility_alias identifier identifier ;
23271
23272    This function registers the alias mapping with the Objective-C front end.
23273    It returns nothing.  */
23274
23275 static void
23276 cp_parser_objc_alias_declaration (cp_parser* parser)
23277 {
23278   tree alias, orig;
23279
23280   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23281   alias = cp_parser_identifier (parser);
23282   orig = cp_parser_identifier (parser);
23283   objc_declare_alias (alias, orig);
23284   cp_parser_consume_semicolon_at_end_of_statement (parser);
23285 }
23286
23287 /* Parse an Objective-C class forward-declaration.
23288
23289    objc-class-declaration:
23290      @class objc-identifier-list ;
23291
23292    The function registers the forward declarations with the Objective-C
23293    front end.  It returns nothing.  */
23294
23295 static void
23296 cp_parser_objc_class_declaration (cp_parser* parser)
23297 {
23298   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23299   while (true)
23300     {
23301       tree id;
23302       
23303       id = cp_parser_identifier (parser);
23304       if (id == error_mark_node)
23305         break;
23306       
23307       objc_declare_class (id);
23308
23309       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23310         cp_lexer_consume_token (parser->lexer);
23311       else
23312         break;
23313     }
23314   cp_parser_consume_semicolon_at_end_of_statement (parser);
23315 }
23316
23317 /* Parse a list of Objective-C protocol references.
23318
23319    objc-protocol-refs-opt:
23320      objc-protocol-refs [opt]
23321
23322    objc-protocol-refs:
23323      < objc-identifier-list >
23324
23325    Returns a TREE_LIST of identifiers, if any.  */
23326
23327 static tree
23328 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23329 {
23330   tree protorefs = NULL_TREE;
23331
23332   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23333     {
23334       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23335       protorefs = cp_parser_objc_identifier_list (parser);
23336       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23337     }
23338
23339   return protorefs;
23340 }
23341
23342 /* Parse a Objective-C visibility specification.  */
23343
23344 static void
23345 cp_parser_objc_visibility_spec (cp_parser* parser)
23346 {
23347   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23348
23349   switch (vis->keyword)
23350     {
23351     case RID_AT_PRIVATE:
23352       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23353       break;
23354     case RID_AT_PROTECTED:
23355       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23356       break;
23357     case RID_AT_PUBLIC:
23358       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23359       break;
23360     case RID_AT_PACKAGE:
23361       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23362       break;
23363     default:
23364       return;
23365     }
23366
23367   /* Eat '@private'/'@protected'/'@public'.  */
23368   cp_lexer_consume_token (parser->lexer);
23369 }
23370
23371 /* Parse an Objective-C method type.  Return 'true' if it is a class
23372    (+) method, and 'false' if it is an instance (-) method.  */
23373
23374 static inline bool
23375 cp_parser_objc_method_type (cp_parser* parser)
23376 {
23377   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23378     return true;
23379   else
23380     return false;
23381 }
23382
23383 /* Parse an Objective-C protocol qualifier.  */
23384
23385 static tree
23386 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23387 {
23388   tree quals = NULL_TREE, node;
23389   cp_token *token = cp_lexer_peek_token (parser->lexer);
23390
23391   node = token->u.value;
23392
23393   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23394          && (node == ridpointers [(int) RID_IN]
23395              || node == ridpointers [(int) RID_OUT]
23396              || node == ridpointers [(int) RID_INOUT]
23397              || node == ridpointers [(int) RID_BYCOPY]
23398              || node == ridpointers [(int) RID_BYREF]
23399              || node == ridpointers [(int) RID_ONEWAY]))
23400     {
23401       quals = tree_cons (NULL_TREE, node, quals);
23402       cp_lexer_consume_token (parser->lexer);
23403       token = cp_lexer_peek_token (parser->lexer);
23404       node = token->u.value;
23405     }
23406
23407   return quals;
23408 }
23409
23410 /* Parse an Objective-C typename.  */
23411
23412 static tree
23413 cp_parser_objc_typename (cp_parser* parser)
23414 {
23415   tree type_name = NULL_TREE;
23416
23417   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23418     {
23419       tree proto_quals, cp_type = NULL_TREE;
23420
23421       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23422       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23423
23424       /* An ObjC type name may consist of just protocol qualifiers, in which
23425          case the type shall default to 'id'.  */
23426       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23427         {
23428           cp_type = cp_parser_type_id (parser);
23429           
23430           /* If the type could not be parsed, an error has already
23431              been produced.  For error recovery, behave as if it had
23432              not been specified, which will use the default type
23433              'id'.  */
23434           if (cp_type == error_mark_node)
23435             {
23436               cp_type = NULL_TREE;
23437               /* We need to skip to the closing parenthesis as
23438                  cp_parser_type_id() does not seem to do it for
23439                  us.  */
23440               cp_parser_skip_to_closing_parenthesis (parser,
23441                                                      /*recovering=*/true,
23442                                                      /*or_comma=*/false,
23443                                                      /*consume_paren=*/false);
23444             }
23445         }
23446
23447       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23448       type_name = build_tree_list (proto_quals, cp_type);
23449     }
23450
23451   return type_name;
23452 }
23453
23454 /* Check to see if TYPE refers to an Objective-C selector name.  */
23455
23456 static bool
23457 cp_parser_objc_selector_p (enum cpp_ttype type)
23458 {
23459   return (type == CPP_NAME || type == CPP_KEYWORD
23460           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23461           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23462           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23463           || type == CPP_XOR || type == CPP_XOR_EQ);
23464 }
23465
23466 /* Parse an Objective-C selector.  */
23467
23468 static tree
23469 cp_parser_objc_selector (cp_parser* parser)
23470 {
23471   cp_token *token = cp_lexer_consume_token (parser->lexer);
23472
23473   if (!cp_parser_objc_selector_p (token->type))
23474     {
23475       error_at (token->location, "invalid Objective-C++ selector name");
23476       return error_mark_node;
23477     }
23478
23479   /* C++ operator names are allowed to appear in ObjC selectors.  */
23480   switch (token->type)
23481     {
23482     case CPP_AND_AND: return get_identifier ("and");
23483     case CPP_AND_EQ: return get_identifier ("and_eq");
23484     case CPP_AND: return get_identifier ("bitand");
23485     case CPP_OR: return get_identifier ("bitor");
23486     case CPP_COMPL: return get_identifier ("compl");
23487     case CPP_NOT: return get_identifier ("not");
23488     case CPP_NOT_EQ: return get_identifier ("not_eq");
23489     case CPP_OR_OR: return get_identifier ("or");
23490     case CPP_OR_EQ: return get_identifier ("or_eq");
23491     case CPP_XOR: return get_identifier ("xor");
23492     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23493     default: return token->u.value;
23494     }
23495 }
23496
23497 /* Parse an Objective-C params list.  */
23498
23499 static tree
23500 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23501 {
23502   tree params = NULL_TREE;
23503   bool maybe_unary_selector_p = true;
23504   cp_token *token = cp_lexer_peek_token (parser->lexer);
23505
23506   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23507     {
23508       tree selector = NULL_TREE, type_name, identifier;
23509       tree parm_attr = NULL_TREE;
23510
23511       if (token->keyword == RID_ATTRIBUTE)
23512         break;
23513
23514       if (token->type != CPP_COLON)
23515         selector = cp_parser_objc_selector (parser);
23516
23517       /* Detect if we have a unary selector.  */
23518       if (maybe_unary_selector_p
23519           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23520         {
23521           params = selector; /* Might be followed by attributes.  */
23522           break;
23523         }
23524
23525       maybe_unary_selector_p = false;
23526       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23527         {
23528           /* Something went quite wrong.  There should be a colon
23529              here, but there is not.  Stop parsing parameters.  */
23530           break;
23531         }
23532       type_name = cp_parser_objc_typename (parser);
23533       /* New ObjC allows attributes on parameters too.  */
23534       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23535         parm_attr = cp_parser_attributes_opt (parser);
23536       identifier = cp_parser_identifier (parser);
23537
23538       params
23539         = chainon (params,
23540                    objc_build_keyword_decl (selector,
23541                                             type_name,
23542                                             identifier,
23543                                             parm_attr));
23544
23545       token = cp_lexer_peek_token (parser->lexer);
23546     }
23547
23548   if (params == NULL_TREE)
23549     {
23550       cp_parser_error (parser, "objective-c++ method declaration is expected");
23551       return error_mark_node;
23552     }
23553
23554   /* We allow tail attributes for the method.  */
23555   if (token->keyword == RID_ATTRIBUTE)
23556     {
23557       *attributes = cp_parser_attributes_opt (parser);
23558       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23559           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23560         return params;
23561       cp_parser_error (parser, 
23562                        "method attributes must be specified at the end");
23563       return error_mark_node;
23564     }
23565
23566   if (params == NULL_TREE)
23567     {
23568       cp_parser_error (parser, "objective-c++ method declaration is expected");
23569       return error_mark_node;
23570     }
23571   return params;
23572 }
23573
23574 /* Parse the non-keyword Objective-C params.  */
23575
23576 static tree
23577 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23578                                        tree* attributes)
23579 {
23580   tree params = make_node (TREE_LIST);
23581   cp_token *token = cp_lexer_peek_token (parser->lexer);
23582   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23583
23584   while (token->type == CPP_COMMA)
23585     {
23586       cp_parameter_declarator *parmdecl;
23587       tree parm;
23588
23589       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23590       token = cp_lexer_peek_token (parser->lexer);
23591
23592       if (token->type == CPP_ELLIPSIS)
23593         {
23594           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23595           *ellipsisp = true;
23596           token = cp_lexer_peek_token (parser->lexer);
23597           break;
23598         }
23599
23600       /* TODO: parse attributes for tail parameters.  */
23601       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23602       parm = grokdeclarator (parmdecl->declarator,
23603                              &parmdecl->decl_specifiers,
23604                              PARM, /*initialized=*/0,
23605                              /*attrlist=*/NULL);
23606
23607       chainon (params, build_tree_list (NULL_TREE, parm));
23608       token = cp_lexer_peek_token (parser->lexer);
23609     }
23610
23611   /* We allow tail attributes for the method.  */
23612   if (token->keyword == RID_ATTRIBUTE)
23613     {
23614       if (*attributes == NULL_TREE)
23615         {
23616           *attributes = cp_parser_attributes_opt (parser);
23617           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23618               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23619             return params;
23620         }
23621       else        
23622         /* We have an error, but parse the attributes, so that we can 
23623            carry on.  */
23624         *attributes = cp_parser_attributes_opt (parser);
23625
23626       cp_parser_error (parser, 
23627                        "method attributes must be specified at the end");
23628       return error_mark_node;
23629     }
23630
23631   return params;
23632 }
23633
23634 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23635
23636 static void
23637 cp_parser_objc_interstitial_code (cp_parser* parser)
23638 {
23639   cp_token *token = cp_lexer_peek_token (parser->lexer);
23640
23641   /* If the next token is `extern' and the following token is a string
23642      literal, then we have a linkage specification.  */
23643   if (token->keyword == RID_EXTERN
23644       && cp_parser_is_pure_string_literal
23645          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23646     cp_parser_linkage_specification (parser);
23647   /* Handle #pragma, if any.  */
23648   else if (token->type == CPP_PRAGMA)
23649     cp_parser_pragma (parser, pragma_external);
23650   /* Allow stray semicolons.  */
23651   else if (token->type == CPP_SEMICOLON)
23652     cp_lexer_consume_token (parser->lexer);
23653   /* Mark methods as optional or required, when building protocols.  */
23654   else if (token->keyword == RID_AT_OPTIONAL)
23655     {
23656       cp_lexer_consume_token (parser->lexer);
23657       objc_set_method_opt (true);
23658     }
23659   else if (token->keyword == RID_AT_REQUIRED)
23660     {
23661       cp_lexer_consume_token (parser->lexer);
23662       objc_set_method_opt (false);
23663     }
23664   else if (token->keyword == RID_NAMESPACE)
23665     cp_parser_namespace_definition (parser);
23666   /* Other stray characters must generate errors.  */
23667   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23668     {
23669       cp_lexer_consume_token (parser->lexer);
23670       error ("stray %qs between Objective-C++ methods",
23671              token->type == CPP_OPEN_BRACE ? "{" : "}");
23672     }
23673   /* Finally, try to parse a block-declaration, or a function-definition.  */
23674   else
23675     cp_parser_block_declaration (parser, /*statement_p=*/false);
23676 }
23677
23678 /* Parse a method signature.  */
23679
23680 static tree
23681 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23682 {
23683   tree rettype, kwdparms, optparms;
23684   bool ellipsis = false;
23685   bool is_class_method;
23686
23687   is_class_method = cp_parser_objc_method_type (parser);
23688   rettype = cp_parser_objc_typename (parser);
23689   *attributes = NULL_TREE;
23690   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23691   if (kwdparms == error_mark_node)
23692     return error_mark_node;
23693   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23694   if (optparms == error_mark_node)
23695     return error_mark_node;
23696
23697   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23698 }
23699
23700 static bool
23701 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23702 {
23703   tree tattr;  
23704   cp_lexer_save_tokens (parser->lexer);
23705   tattr = cp_parser_attributes_opt (parser);
23706   gcc_assert (tattr) ;
23707   
23708   /* If the attributes are followed by a method introducer, this is not allowed.
23709      Dump the attributes and flag the situation.  */
23710   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23711       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23712     return true;
23713
23714   /* Otherwise, the attributes introduce some interstitial code, possibly so
23715      rewind to allow that check.  */
23716   cp_lexer_rollback_tokens (parser->lexer);
23717   return false;  
23718 }
23719
23720 /* Parse an Objective-C method prototype list.  */
23721
23722 static void
23723 cp_parser_objc_method_prototype_list (cp_parser* parser)
23724 {
23725   cp_token *token = cp_lexer_peek_token (parser->lexer);
23726
23727   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23728     {
23729       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23730         {
23731           tree attributes, sig;
23732           bool is_class_method;
23733           if (token->type == CPP_PLUS)
23734             is_class_method = true;
23735           else
23736             is_class_method = false;
23737           sig = cp_parser_objc_method_signature (parser, &attributes);
23738           if (sig == error_mark_node)
23739             {
23740               cp_parser_skip_to_end_of_block_or_statement (parser);
23741               token = cp_lexer_peek_token (parser->lexer);
23742               continue;
23743             }
23744           objc_add_method_declaration (is_class_method, sig, attributes);
23745           cp_parser_consume_semicolon_at_end_of_statement (parser);
23746         }
23747       else if (token->keyword == RID_AT_PROPERTY)
23748         cp_parser_objc_at_property_declaration (parser);
23749       else if (token->keyword == RID_ATTRIBUTE 
23750                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23751         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23752                     OPT_Wattributes, 
23753                     "prefix attributes are ignored for methods");
23754       else
23755         /* Allow for interspersed non-ObjC++ code.  */
23756         cp_parser_objc_interstitial_code (parser);
23757
23758       token = cp_lexer_peek_token (parser->lexer);
23759     }
23760
23761   if (token->type != CPP_EOF)
23762     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23763   else
23764     cp_parser_error (parser, "expected %<@end%>");
23765
23766   objc_finish_interface ();
23767 }
23768
23769 /* Parse an Objective-C method definition list.  */
23770
23771 static void
23772 cp_parser_objc_method_definition_list (cp_parser* parser)
23773 {
23774   cp_token *token = cp_lexer_peek_token (parser->lexer);
23775
23776   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23777     {
23778       tree meth;
23779
23780       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23781         {
23782           cp_token *ptk;
23783           tree sig, attribute;
23784           bool is_class_method;
23785           if (token->type == CPP_PLUS)
23786             is_class_method = true;
23787           else
23788             is_class_method = false;
23789           push_deferring_access_checks (dk_deferred);
23790           sig = cp_parser_objc_method_signature (parser, &attribute);
23791           if (sig == error_mark_node)
23792             {
23793               cp_parser_skip_to_end_of_block_or_statement (parser);
23794               token = cp_lexer_peek_token (parser->lexer);
23795               continue;
23796             }
23797           objc_start_method_definition (is_class_method, sig, attribute,
23798                                         NULL_TREE);
23799
23800           /* For historical reasons, we accept an optional semicolon.  */
23801           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23802             cp_lexer_consume_token (parser->lexer);
23803
23804           ptk = cp_lexer_peek_token (parser->lexer);
23805           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23806                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23807             {
23808               perform_deferred_access_checks ();
23809               stop_deferring_access_checks ();
23810               meth = cp_parser_function_definition_after_declarator (parser,
23811                                                                      false);
23812               pop_deferring_access_checks ();
23813               objc_finish_method_definition (meth);
23814             }
23815         }
23816       /* The following case will be removed once @synthesize is
23817          completely implemented.  */
23818       else if (token->keyword == RID_AT_PROPERTY)
23819         cp_parser_objc_at_property_declaration (parser);
23820       else if (token->keyword == RID_AT_SYNTHESIZE)
23821         cp_parser_objc_at_synthesize_declaration (parser);
23822       else if (token->keyword == RID_AT_DYNAMIC)
23823         cp_parser_objc_at_dynamic_declaration (parser);
23824       else if (token->keyword == RID_ATTRIBUTE 
23825                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23826         warning_at (token->location, OPT_Wattributes,
23827                     "prefix attributes are ignored for methods");
23828       else
23829         /* Allow for interspersed non-ObjC++ code.  */
23830         cp_parser_objc_interstitial_code (parser);
23831
23832       token = cp_lexer_peek_token (parser->lexer);
23833     }
23834
23835   if (token->type != CPP_EOF)
23836     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23837   else
23838     cp_parser_error (parser, "expected %<@end%>");
23839
23840   objc_finish_implementation ();
23841 }
23842
23843 /* Parse Objective-C ivars.  */
23844
23845 static void
23846 cp_parser_objc_class_ivars (cp_parser* parser)
23847 {
23848   cp_token *token = cp_lexer_peek_token (parser->lexer);
23849
23850   if (token->type != CPP_OPEN_BRACE)
23851     return;     /* No ivars specified.  */
23852
23853   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23854   token = cp_lexer_peek_token (parser->lexer);
23855
23856   while (token->type != CPP_CLOSE_BRACE 
23857         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23858     {
23859       cp_decl_specifier_seq declspecs;
23860       int decl_class_or_enum_p;
23861       tree prefix_attributes;
23862
23863       cp_parser_objc_visibility_spec (parser);
23864
23865       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23866         break;
23867
23868       cp_parser_decl_specifier_seq (parser,
23869                                     CP_PARSER_FLAGS_OPTIONAL,
23870                                     &declspecs,
23871                                     &decl_class_or_enum_p);
23872
23873       /* auto, register, static, extern, mutable.  */
23874       if (declspecs.storage_class != sc_none)
23875         {
23876           cp_parser_error (parser, "invalid type for instance variable");         
23877           declspecs.storage_class = sc_none;
23878         }
23879
23880       /* __thread.  */
23881       if (declspecs.specs[(int) ds_thread])
23882         {
23883           cp_parser_error (parser, "invalid type for instance variable");
23884           declspecs.specs[(int) ds_thread] = 0;
23885         }
23886       
23887       /* typedef.  */
23888       if (declspecs.specs[(int) ds_typedef])
23889         {
23890           cp_parser_error (parser, "invalid type for instance variable");
23891           declspecs.specs[(int) ds_typedef] = 0;
23892         }
23893
23894       prefix_attributes = declspecs.attributes;
23895       declspecs.attributes = NULL_TREE;
23896
23897       /* Keep going until we hit the `;' at the end of the
23898          declaration.  */
23899       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23900         {
23901           tree width = NULL_TREE, attributes, first_attribute, decl;
23902           cp_declarator *declarator = NULL;
23903           int ctor_dtor_or_conv_p;
23904
23905           /* Check for a (possibly unnamed) bitfield declaration.  */
23906           token = cp_lexer_peek_token (parser->lexer);
23907           if (token->type == CPP_COLON)
23908             goto eat_colon;
23909
23910           if (token->type == CPP_NAME
23911               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23912                   == CPP_COLON))
23913             {
23914               /* Get the name of the bitfield.  */
23915               declarator = make_id_declarator (NULL_TREE,
23916                                                cp_parser_identifier (parser),
23917                                                sfk_none);
23918
23919              eat_colon:
23920               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23921               /* Get the width of the bitfield.  */
23922               width
23923                 = cp_parser_constant_expression (parser,
23924                                                  /*allow_non_constant=*/false,
23925                                                  NULL);
23926             }
23927           else
23928             {
23929               /* Parse the declarator.  */
23930               declarator
23931                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23932                                         &ctor_dtor_or_conv_p,
23933                                         /*parenthesized_p=*/NULL,
23934                                         /*member_p=*/false);
23935             }
23936
23937           /* Look for attributes that apply to the ivar.  */
23938           attributes = cp_parser_attributes_opt (parser);
23939           /* Remember which attributes are prefix attributes and
23940              which are not.  */
23941           first_attribute = attributes;
23942           /* Combine the attributes.  */
23943           attributes = chainon (prefix_attributes, attributes);
23944
23945           if (width)
23946               /* Create the bitfield declaration.  */
23947               decl = grokbitfield (declarator, &declspecs,
23948                                    width,
23949                                    attributes);
23950           else
23951             decl = grokfield (declarator, &declspecs,
23952                               NULL_TREE, /*init_const_expr_p=*/false,
23953                               NULL_TREE, attributes);
23954
23955           /* Add the instance variable.  */
23956           if (decl != error_mark_node && decl != NULL_TREE)
23957             objc_add_instance_variable (decl);
23958
23959           /* Reset PREFIX_ATTRIBUTES.  */
23960           while (attributes && TREE_CHAIN (attributes) != first_attribute)
23961             attributes = TREE_CHAIN (attributes);
23962           if (attributes)
23963             TREE_CHAIN (attributes) = NULL_TREE;
23964
23965           token = cp_lexer_peek_token (parser->lexer);
23966
23967           if (token->type == CPP_COMMA)
23968             {
23969               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23970               continue;
23971             }
23972           break;
23973         }
23974
23975       cp_parser_consume_semicolon_at_end_of_statement (parser);
23976       token = cp_lexer_peek_token (parser->lexer);
23977     }
23978
23979   if (token->keyword == RID_AT_END)
23980     cp_parser_error (parser, "expected %<}%>");
23981
23982   /* Do not consume the RID_AT_END, so it will be read again as terminating
23983      the @interface of @implementation.  */ 
23984   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23985     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
23986     
23987   /* For historical reasons, we accept an optional semicolon.  */
23988   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23989     cp_lexer_consume_token (parser->lexer);
23990 }
23991
23992 /* Parse an Objective-C protocol declaration.  */
23993
23994 static void
23995 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23996 {
23997   tree proto, protorefs;
23998   cp_token *tok;
23999
24000   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
24001   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
24002     {
24003       tok = cp_lexer_peek_token (parser->lexer);
24004       error_at (tok->location, "identifier expected after %<@protocol%>");
24005       cp_parser_consume_semicolon_at_end_of_statement (parser);
24006       return;
24007     }
24008
24009   /* See if we have a forward declaration or a definition.  */
24010   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
24011
24012   /* Try a forward declaration first.  */
24013   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
24014     {
24015       while (true)
24016         {
24017           tree id;
24018           
24019           id = cp_parser_identifier (parser);
24020           if (id == error_mark_node)
24021             break;
24022           
24023           objc_declare_protocol (id, attributes);
24024           
24025           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24026             cp_lexer_consume_token (parser->lexer);
24027           else
24028             break;
24029         }
24030       cp_parser_consume_semicolon_at_end_of_statement (parser);
24031     }
24032
24033   /* Ok, we got a full-fledged definition (or at least should).  */
24034   else
24035     {
24036       proto = cp_parser_identifier (parser);
24037       protorefs = cp_parser_objc_protocol_refs_opt (parser);
24038       objc_start_protocol (proto, protorefs, attributes);
24039       cp_parser_objc_method_prototype_list (parser);
24040     }
24041 }
24042
24043 /* Parse an Objective-C superclass or category.  */
24044
24045 static void
24046 cp_parser_objc_superclass_or_category (cp_parser *parser, 
24047                                        bool iface_p,
24048                                        tree *super,
24049                                        tree *categ, bool *is_class_extension)
24050 {
24051   cp_token *next = cp_lexer_peek_token (parser->lexer);
24052
24053   *super = *categ = NULL_TREE;
24054   *is_class_extension = false;
24055   if (next->type == CPP_COLON)
24056     {
24057       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
24058       *super = cp_parser_identifier (parser);
24059     }
24060   else if (next->type == CPP_OPEN_PAREN)
24061     {
24062       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
24063
24064       /* If there is no category name, and this is an @interface, we
24065          have a class extension.  */
24066       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24067         {
24068           *categ = NULL_TREE;
24069           *is_class_extension = true;
24070         }
24071       else
24072         *categ = cp_parser_identifier (parser);
24073
24074       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24075     }
24076 }
24077
24078 /* Parse an Objective-C class interface.  */
24079
24080 static void
24081 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
24082 {
24083   tree name, super, categ, protos;
24084   bool is_class_extension;
24085
24086   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
24087   name = cp_parser_identifier (parser);
24088   if (name == error_mark_node)
24089     {
24090       /* It's hard to recover because even if valid @interface stuff
24091          is to follow, we can't compile it (or validate it) if we
24092          don't even know which class it refers to.  Let's assume this
24093          was a stray '@interface' token in the stream and skip it.
24094       */
24095       return;
24096     }
24097   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
24098                                          &is_class_extension);
24099   protos = cp_parser_objc_protocol_refs_opt (parser);
24100
24101   /* We have either a class or a category on our hands.  */
24102   if (categ || is_class_extension)
24103     objc_start_category_interface (name, categ, protos, attributes);
24104   else
24105     {
24106       objc_start_class_interface (name, super, protos, attributes);
24107       /* Handle instance variable declarations, if any.  */
24108       cp_parser_objc_class_ivars (parser);
24109       objc_continue_interface ();
24110     }
24111
24112   cp_parser_objc_method_prototype_list (parser);
24113 }
24114
24115 /* Parse an Objective-C class implementation.  */
24116
24117 static void
24118 cp_parser_objc_class_implementation (cp_parser* parser)
24119 {
24120   tree name, super, categ;
24121   bool is_class_extension;
24122
24123   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
24124   name = cp_parser_identifier (parser);
24125   if (name == error_mark_node)
24126     {
24127       /* It's hard to recover because even if valid @implementation
24128          stuff is to follow, we can't compile it (or validate it) if
24129          we don't even know which class it refers to.  Let's assume
24130          this was a stray '@implementation' token in the stream and
24131          skip it.
24132       */
24133       return;
24134     }
24135   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
24136                                          &is_class_extension);
24137
24138   /* We have either a class or a category on our hands.  */
24139   if (categ)
24140     objc_start_category_implementation (name, categ);
24141   else
24142     {
24143       objc_start_class_implementation (name, super);
24144       /* Handle instance variable declarations, if any.  */
24145       cp_parser_objc_class_ivars (parser);
24146       objc_continue_implementation ();
24147     }
24148
24149   cp_parser_objc_method_definition_list (parser);
24150 }
24151
24152 /* Consume the @end token and finish off the implementation.  */
24153
24154 static void
24155 cp_parser_objc_end_implementation (cp_parser* parser)
24156 {
24157   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
24158   objc_finish_implementation ();
24159 }
24160
24161 /* Parse an Objective-C declaration.  */
24162
24163 static void
24164 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
24165 {
24166   /* Try to figure out what kind of declaration is present.  */
24167   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24168
24169   if (attributes)
24170     switch (kwd->keyword)
24171       {
24172         case RID_AT_ALIAS:
24173         case RID_AT_CLASS:
24174         case RID_AT_END:
24175           error_at (kwd->location, "attributes may not be specified before"
24176                     " the %<@%D%> Objective-C++ keyword",
24177                     kwd->u.value);
24178           attributes = NULL;
24179           break;
24180         case RID_AT_IMPLEMENTATION:
24181           warning_at (kwd->location, OPT_Wattributes,
24182                       "prefix attributes are ignored before %<@%D%>",
24183                       kwd->u.value);
24184           attributes = NULL;
24185         default:
24186           break;
24187       }
24188
24189   switch (kwd->keyword)
24190     {
24191     case RID_AT_ALIAS:
24192       cp_parser_objc_alias_declaration (parser);
24193       break;
24194     case RID_AT_CLASS:
24195       cp_parser_objc_class_declaration (parser);
24196       break;
24197     case RID_AT_PROTOCOL:
24198       cp_parser_objc_protocol_declaration (parser, attributes);
24199       break;
24200     case RID_AT_INTERFACE:
24201       cp_parser_objc_class_interface (parser, attributes);
24202       break;
24203     case RID_AT_IMPLEMENTATION:
24204       cp_parser_objc_class_implementation (parser);
24205       break;
24206     case RID_AT_END:
24207       cp_parser_objc_end_implementation (parser);
24208       break;
24209     default:
24210       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24211                 kwd->u.value);
24212       cp_parser_skip_to_end_of_block_or_statement (parser);
24213     }
24214 }
24215
24216 /* Parse an Objective-C try-catch-finally statement.
24217
24218    objc-try-catch-finally-stmt:
24219      @try compound-statement objc-catch-clause-seq [opt]
24220        objc-finally-clause [opt]
24221
24222    objc-catch-clause-seq:
24223      objc-catch-clause objc-catch-clause-seq [opt]
24224
24225    objc-catch-clause:
24226      @catch ( objc-exception-declaration ) compound-statement
24227
24228    objc-finally-clause:
24229      @finally compound-statement
24230
24231    objc-exception-declaration:
24232      parameter-declaration
24233      '...'
24234
24235    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24236
24237    Returns NULL_TREE.
24238
24239    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24240    for C.  Keep them in sync.  */   
24241
24242 static tree
24243 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24244 {
24245   location_t location;
24246   tree stmt;
24247
24248   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24249   location = cp_lexer_peek_token (parser->lexer)->location;
24250   objc_maybe_warn_exceptions (location);
24251   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24252      node, lest it get absorbed into the surrounding block.  */
24253   stmt = push_stmt_list ();
24254   cp_parser_compound_statement (parser, NULL, false, false);
24255   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24256
24257   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24258     {
24259       cp_parameter_declarator *parm;
24260       tree parameter_declaration = error_mark_node;
24261       bool seen_open_paren = false;
24262
24263       cp_lexer_consume_token (parser->lexer);
24264       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24265         seen_open_paren = true;
24266       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24267         {
24268           /* We have "@catch (...)" (where the '...' are literally
24269              what is in the code).  Skip the '...'.
24270              parameter_declaration is set to NULL_TREE, and
24271              objc_being_catch_clauses() knows that that means
24272              '...'.  */
24273           cp_lexer_consume_token (parser->lexer);
24274           parameter_declaration = NULL_TREE;
24275         }
24276       else
24277         {
24278           /* We have "@catch (NSException *exception)" or something
24279              like that.  Parse the parameter declaration.  */
24280           parm = cp_parser_parameter_declaration (parser, false, NULL);
24281           if (parm == NULL)
24282             parameter_declaration = error_mark_node;
24283           else
24284             parameter_declaration = grokdeclarator (parm->declarator,
24285                                                     &parm->decl_specifiers,
24286                                                     PARM, /*initialized=*/0,
24287                                                     /*attrlist=*/NULL);
24288         }
24289       if (seen_open_paren)
24290         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24291       else
24292         {
24293           /* If there was no open parenthesis, we are recovering from
24294              an error, and we are trying to figure out what mistake
24295              the user has made.  */
24296
24297           /* If there is an immediate closing parenthesis, the user
24298              probably forgot the opening one (ie, they typed "@catch
24299              NSException *e)".  Parse the closing parenthesis and keep
24300              going.  */
24301           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24302             cp_lexer_consume_token (parser->lexer);
24303           
24304           /* If these is no immediate closing parenthesis, the user
24305              probably doesn't know that parenthesis are required at
24306              all (ie, they typed "@catch NSException *e").  So, just
24307              forget about the closing parenthesis and keep going.  */
24308         }
24309       objc_begin_catch_clause (parameter_declaration);
24310       cp_parser_compound_statement (parser, NULL, false, false);
24311       objc_finish_catch_clause ();
24312     }
24313   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24314     {
24315       cp_lexer_consume_token (parser->lexer);
24316       location = cp_lexer_peek_token (parser->lexer)->location;
24317       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24318          node, lest it get absorbed into the surrounding block.  */
24319       stmt = push_stmt_list ();
24320       cp_parser_compound_statement (parser, NULL, false, false);
24321       objc_build_finally_clause (location, pop_stmt_list (stmt));
24322     }
24323
24324   return objc_finish_try_stmt ();
24325 }
24326
24327 /* Parse an Objective-C synchronized statement.
24328
24329    objc-synchronized-stmt:
24330      @synchronized ( expression ) compound-statement
24331
24332    Returns NULL_TREE.  */
24333
24334 static tree
24335 cp_parser_objc_synchronized_statement (cp_parser *parser)
24336 {
24337   location_t location;
24338   tree lock, stmt;
24339
24340   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24341
24342   location = cp_lexer_peek_token (parser->lexer)->location;
24343   objc_maybe_warn_exceptions (location);
24344   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24345   lock = cp_parser_expression (parser, false, NULL);
24346   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24347
24348   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24349      node, lest it get absorbed into the surrounding block.  */
24350   stmt = push_stmt_list ();
24351   cp_parser_compound_statement (parser, NULL, false, false);
24352
24353   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24354 }
24355
24356 /* Parse an Objective-C throw statement.
24357
24358    objc-throw-stmt:
24359      @throw assignment-expression [opt] ;
24360
24361    Returns a constructed '@throw' statement.  */
24362
24363 static tree
24364 cp_parser_objc_throw_statement (cp_parser *parser)
24365 {
24366   tree expr = NULL_TREE;
24367   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24368
24369   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24370
24371   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24372     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24373
24374   cp_parser_consume_semicolon_at_end_of_statement (parser);
24375
24376   return objc_build_throw_stmt (loc, expr);
24377 }
24378
24379 /* Parse an Objective-C statement.  */
24380
24381 static tree
24382 cp_parser_objc_statement (cp_parser * parser)
24383 {
24384   /* Try to figure out what kind of declaration is present.  */
24385   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24386
24387   switch (kwd->keyword)
24388     {
24389     case RID_AT_TRY:
24390       return cp_parser_objc_try_catch_finally_statement (parser);
24391     case RID_AT_SYNCHRONIZED:
24392       return cp_parser_objc_synchronized_statement (parser);
24393     case RID_AT_THROW:
24394       return cp_parser_objc_throw_statement (parser);
24395     default:
24396       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24397                kwd->u.value);
24398       cp_parser_skip_to_end_of_block_or_statement (parser);
24399     }
24400
24401   return error_mark_node;
24402 }
24403
24404 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24405    look ahead to see if an objc keyword follows the attributes.  This
24406    is to detect the use of prefix attributes on ObjC @interface and 
24407    @protocol.  */
24408
24409 static bool
24410 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24411 {
24412   cp_lexer_save_tokens (parser->lexer);
24413   *attrib = cp_parser_attributes_opt (parser);
24414   gcc_assert (*attrib);
24415   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24416     {
24417       cp_lexer_commit_tokens (parser->lexer);
24418       return true;
24419     }
24420   cp_lexer_rollback_tokens (parser->lexer);
24421   return false;  
24422 }
24423
24424 /* This routine is a minimal replacement for
24425    c_parser_struct_declaration () used when parsing the list of
24426    types/names or ObjC++ properties.  For example, when parsing the
24427    code
24428
24429    @property (readonly) int a, b, c;
24430
24431    this function is responsible for parsing "int a, int b, int c" and
24432    returning the declarations as CHAIN of DECLs.
24433
24434    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24435    similar parsing.  */
24436 static tree
24437 cp_parser_objc_struct_declaration (cp_parser *parser)
24438 {
24439   tree decls = NULL_TREE;
24440   cp_decl_specifier_seq declspecs;
24441   int decl_class_or_enum_p;
24442   tree prefix_attributes;
24443
24444   cp_parser_decl_specifier_seq (parser,
24445                                 CP_PARSER_FLAGS_NONE,
24446                                 &declspecs,
24447                                 &decl_class_or_enum_p);
24448
24449   if (declspecs.type == error_mark_node)
24450     return error_mark_node;
24451
24452   /* auto, register, static, extern, mutable.  */
24453   if (declspecs.storage_class != sc_none)
24454     {
24455       cp_parser_error (parser, "invalid type for property");
24456       declspecs.storage_class = sc_none;
24457     }
24458   
24459   /* __thread.  */
24460   if (declspecs.specs[(int) ds_thread])
24461     {
24462       cp_parser_error (parser, "invalid type for property");
24463       declspecs.specs[(int) ds_thread] = 0;
24464     }
24465   
24466   /* typedef.  */
24467   if (declspecs.specs[(int) ds_typedef])
24468     {
24469       cp_parser_error (parser, "invalid type for property");
24470       declspecs.specs[(int) ds_typedef] = 0;
24471     }
24472
24473   prefix_attributes = declspecs.attributes;
24474   declspecs.attributes = NULL_TREE;
24475
24476   /* Keep going until we hit the `;' at the end of the declaration. */
24477   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24478     {
24479       tree attributes, first_attribute, decl;
24480       cp_declarator *declarator;
24481       cp_token *token;
24482
24483       /* Parse the declarator.  */
24484       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24485                                          NULL, NULL, false);
24486
24487       /* Look for attributes that apply to the ivar.  */
24488       attributes = cp_parser_attributes_opt (parser);
24489       /* Remember which attributes are prefix attributes and
24490          which are not.  */
24491       first_attribute = attributes;
24492       /* Combine the attributes.  */
24493       attributes = chainon (prefix_attributes, attributes);
24494       
24495       decl = grokfield (declarator, &declspecs,
24496                         NULL_TREE, /*init_const_expr_p=*/false,
24497                         NULL_TREE, attributes);
24498
24499       if (decl == error_mark_node || decl == NULL_TREE)
24500         return error_mark_node;
24501       
24502       /* Reset PREFIX_ATTRIBUTES.  */
24503       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24504         attributes = TREE_CHAIN (attributes);
24505       if (attributes)
24506         TREE_CHAIN (attributes) = NULL_TREE;
24507
24508       DECL_CHAIN (decl) = decls;
24509       decls = decl;
24510
24511       token = cp_lexer_peek_token (parser->lexer);
24512       if (token->type == CPP_COMMA)
24513         {
24514           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24515           continue;
24516         }
24517       else
24518         break;
24519     }
24520   return decls;
24521 }
24522
24523 /* Parse an Objective-C @property declaration.  The syntax is:
24524
24525    objc-property-declaration:
24526      '@property' objc-property-attributes[opt] struct-declaration ;
24527
24528    objc-property-attributes:
24529     '(' objc-property-attribute-list ')'
24530
24531    objc-property-attribute-list:
24532      objc-property-attribute
24533      objc-property-attribute-list, objc-property-attribute
24534
24535    objc-property-attribute
24536      'getter' = identifier
24537      'setter' = identifier
24538      'readonly'
24539      'readwrite'
24540      'assign'
24541      'retain'
24542      'copy'
24543      'nonatomic'
24544
24545   For example:
24546     @property NSString *name;
24547     @property (readonly) id object;
24548     @property (retain, nonatomic, getter=getTheName) id name;
24549     @property int a, b, c;
24550
24551    PS: This function is identical to
24552    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24553 static void 
24554 cp_parser_objc_at_property_declaration (cp_parser *parser)
24555 {
24556   /* The following variables hold the attributes of the properties as
24557      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24558      seen.  When we see an attribute, we set them to 'true' (if they
24559      are boolean properties) or to the identifier (if they have an
24560      argument, ie, for getter and setter).  Note that here we only
24561      parse the list of attributes, check the syntax and accumulate the
24562      attributes that we find.  objc_add_property_declaration() will
24563      then process the information.  */
24564   bool property_assign = false;
24565   bool property_copy = false;
24566   tree property_getter_ident = NULL_TREE;
24567   bool property_nonatomic = false;
24568   bool property_readonly = false;
24569   bool property_readwrite = false;
24570   bool property_retain = false;
24571   tree property_setter_ident = NULL_TREE;
24572
24573   /* 'properties' is the list of properties that we read.  Usually a
24574      single one, but maybe more (eg, in "@property int a, b, c;" there
24575      are three).  */
24576   tree properties;
24577   location_t loc;
24578
24579   loc = cp_lexer_peek_token (parser->lexer)->location;
24580
24581   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24582
24583   /* Parse the optional attribute list...  */
24584   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24585     {
24586       /* Eat the '('.  */
24587       cp_lexer_consume_token (parser->lexer);
24588
24589       while (true)
24590         {
24591           bool syntax_error = false;
24592           cp_token *token = cp_lexer_peek_token (parser->lexer);
24593           enum rid keyword;
24594
24595           if (token->type != CPP_NAME)
24596             {
24597               cp_parser_error (parser, "expected identifier");
24598               break;
24599             }
24600           keyword = C_RID_CODE (token->u.value);
24601           cp_lexer_consume_token (parser->lexer);
24602           switch (keyword)
24603             {
24604             case RID_ASSIGN:    property_assign = true;    break;
24605             case RID_COPY:      property_copy = true;      break;
24606             case RID_NONATOMIC: property_nonatomic = true; break;
24607             case RID_READONLY:  property_readonly = true;  break;
24608             case RID_READWRITE: property_readwrite = true; break;
24609             case RID_RETAIN:    property_retain = true;    break;
24610
24611             case RID_GETTER:
24612             case RID_SETTER:
24613               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24614                 {
24615                   if (keyword == RID_GETTER)
24616                     cp_parser_error (parser,
24617                                      "missing %<=%> (after %<getter%> attribute)");
24618                   else
24619                     cp_parser_error (parser,
24620                                      "missing %<=%> (after %<setter%> attribute)");
24621                   syntax_error = true;
24622                   break;
24623                 }
24624               cp_lexer_consume_token (parser->lexer); /* eat the = */
24625               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24626                 {
24627                   cp_parser_error (parser, "expected identifier");
24628                   syntax_error = true;
24629                   break;
24630                 }
24631               if (keyword == RID_SETTER)
24632                 {
24633                   if (property_setter_ident != NULL_TREE)
24634                     {
24635                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24636                       cp_lexer_consume_token (parser->lexer);
24637                     }
24638                   else
24639                     property_setter_ident = cp_parser_objc_selector (parser);
24640                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24641                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24642                   else
24643                     cp_lexer_consume_token (parser->lexer);
24644                 }
24645               else
24646                 {
24647                   if (property_getter_ident != NULL_TREE)
24648                     {
24649                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24650                       cp_lexer_consume_token (parser->lexer);
24651                     }
24652                   else
24653                     property_getter_ident = cp_parser_objc_selector (parser);
24654                 }
24655               break;
24656             default:
24657               cp_parser_error (parser, "unknown property attribute");
24658               syntax_error = true;
24659               break;
24660             }
24661
24662           if (syntax_error)
24663             break;
24664
24665           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24666             cp_lexer_consume_token (parser->lexer);
24667           else
24668             break;
24669         }
24670
24671       /* FIXME: "@property (setter, assign);" will generate a spurious
24672          "error: expected â€˜)’ before â€˜,’ token".  This is because
24673          cp_parser_require, unlike the C counterpart, will produce an
24674          error even if we are in error recovery.  */
24675       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24676         {
24677           cp_parser_skip_to_closing_parenthesis (parser,
24678                                                  /*recovering=*/true,
24679                                                  /*or_comma=*/false,
24680                                                  /*consume_paren=*/true);
24681         }
24682     }
24683
24684   /* ... and the property declaration(s).  */
24685   properties = cp_parser_objc_struct_declaration (parser);
24686
24687   if (properties == error_mark_node)
24688     {
24689       cp_parser_skip_to_end_of_statement (parser);
24690       /* If the next token is now a `;', consume it.  */
24691       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24692         cp_lexer_consume_token (parser->lexer);
24693       return;
24694     }
24695
24696   if (properties == NULL_TREE)
24697     cp_parser_error (parser, "expected identifier");
24698   else
24699     {
24700       /* Comma-separated properties are chained together in
24701          reverse order; add them one by one.  */
24702       properties = nreverse (properties);
24703       
24704       for (; properties; properties = TREE_CHAIN (properties))
24705         objc_add_property_declaration (loc, copy_node (properties),
24706                                        property_readonly, property_readwrite,
24707                                        property_assign, property_retain,
24708                                        property_copy, property_nonatomic,
24709                                        property_getter_ident, property_setter_ident);
24710     }
24711   
24712   cp_parser_consume_semicolon_at_end_of_statement (parser);
24713 }
24714
24715 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24716
24717    objc-synthesize-declaration:
24718      @synthesize objc-synthesize-identifier-list ;
24719
24720    objc-synthesize-identifier-list:
24721      objc-synthesize-identifier
24722      objc-synthesize-identifier-list, objc-synthesize-identifier
24723
24724    objc-synthesize-identifier
24725      identifier
24726      identifier = identifier
24727
24728   For example:
24729     @synthesize MyProperty;
24730     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24731
24732   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24733   for C.  Keep them in sync.
24734 */
24735 static void 
24736 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24737 {
24738   tree list = NULL_TREE;
24739   location_t loc;
24740   loc = cp_lexer_peek_token (parser->lexer)->location;
24741
24742   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24743   while (true)
24744     {
24745       tree property, ivar;
24746       property = cp_parser_identifier (parser);
24747       if (property == error_mark_node)
24748         {
24749           cp_parser_consume_semicolon_at_end_of_statement (parser);
24750           return;
24751         }
24752       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24753         {
24754           cp_lexer_consume_token (parser->lexer);
24755           ivar = cp_parser_identifier (parser);
24756           if (ivar == error_mark_node)
24757             {
24758               cp_parser_consume_semicolon_at_end_of_statement (parser);
24759               return;
24760             }
24761         }
24762       else
24763         ivar = NULL_TREE;
24764       list = chainon (list, build_tree_list (ivar, property));
24765       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24766         cp_lexer_consume_token (parser->lexer);
24767       else
24768         break;
24769     }
24770   cp_parser_consume_semicolon_at_end_of_statement (parser);
24771   objc_add_synthesize_declaration (loc, list);
24772 }
24773
24774 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24775
24776    objc-dynamic-declaration:
24777      @dynamic identifier-list ;
24778
24779    For example:
24780      @dynamic MyProperty;
24781      @dynamic MyProperty, AnotherProperty;
24782
24783   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24784   for C.  Keep them in sync.
24785 */
24786 static void 
24787 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24788 {
24789   tree list = NULL_TREE;
24790   location_t loc;
24791   loc = cp_lexer_peek_token (parser->lexer)->location;
24792
24793   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24794   while (true)
24795     {
24796       tree property;
24797       property = cp_parser_identifier (parser);
24798       if (property == error_mark_node)
24799         {
24800           cp_parser_consume_semicolon_at_end_of_statement (parser);
24801           return;
24802         }
24803       list = chainon (list, build_tree_list (NULL, property));
24804       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24805         cp_lexer_consume_token (parser->lexer);
24806       else
24807         break;
24808     }
24809   cp_parser_consume_semicolon_at_end_of_statement (parser);
24810   objc_add_dynamic_declaration (loc, list);
24811 }
24812
24813 \f
24814 /* OpenMP 2.5 parsing routines.  */
24815
24816 /* Returns name of the next clause.
24817    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24818    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24819    returned and the token is consumed.  */
24820
24821 static pragma_omp_clause
24822 cp_parser_omp_clause_name (cp_parser *parser)
24823 {
24824   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24825
24826   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24827     result = PRAGMA_OMP_CLAUSE_IF;
24828   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24829     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24830   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24831     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24832   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24833     {
24834       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24835       const char *p = IDENTIFIER_POINTER (id);
24836
24837       switch (p[0])
24838         {
24839         case 'c':
24840           if (!strcmp ("collapse", p))
24841             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24842           else if (!strcmp ("copyin", p))
24843             result = PRAGMA_OMP_CLAUSE_COPYIN;
24844           else if (!strcmp ("copyprivate", p))
24845             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24846           break;
24847         case 'f':
24848           if (!strcmp ("final", p))
24849             result = PRAGMA_OMP_CLAUSE_FINAL;
24850           else if (!strcmp ("firstprivate", p))
24851             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24852           break;
24853         case 'l':
24854           if (!strcmp ("lastprivate", p))
24855             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24856           break;
24857         case 'm':
24858           if (!strcmp ("mergeable", p))
24859             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24860           break;
24861         case 'n':
24862           if (!strcmp ("nowait", p))
24863             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24864           else if (!strcmp ("num_threads", p))
24865             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24866           break;
24867         case 'o':
24868           if (!strcmp ("ordered", p))
24869             result = PRAGMA_OMP_CLAUSE_ORDERED;
24870           break;
24871         case 'r':
24872           if (!strcmp ("reduction", p))
24873             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24874           break;
24875         case 's':
24876           if (!strcmp ("schedule", p))
24877             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24878           else if (!strcmp ("shared", p))
24879             result = PRAGMA_OMP_CLAUSE_SHARED;
24880           break;
24881         case 'u':
24882           if (!strcmp ("untied", p))
24883             result = PRAGMA_OMP_CLAUSE_UNTIED;
24884           break;
24885         }
24886     }
24887
24888   if (result != PRAGMA_OMP_CLAUSE_NONE)
24889     cp_lexer_consume_token (parser->lexer);
24890
24891   return result;
24892 }
24893
24894 /* Validate that a clause of the given type does not already exist.  */
24895
24896 static void
24897 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24898                            const char *name, location_t location)
24899 {
24900   tree c;
24901
24902   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24903     if (OMP_CLAUSE_CODE (c) == code)
24904       {
24905         error_at (location, "too many %qs clauses", name);
24906         break;
24907       }
24908 }
24909
24910 /* OpenMP 2.5:
24911    variable-list:
24912      identifier
24913      variable-list , identifier
24914
24915    In addition, we match a closing parenthesis.  An opening parenthesis
24916    will have been consumed by the caller.
24917
24918    If KIND is nonzero, create the appropriate node and install the decl
24919    in OMP_CLAUSE_DECL and add the node to the head of the list.
24920
24921    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24922    return the list created.  */
24923
24924 static tree
24925 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24926                                 tree list)
24927 {
24928   cp_token *token;
24929   while (1)
24930     {
24931       tree name, decl;
24932
24933       token = cp_lexer_peek_token (parser->lexer);
24934       name = cp_parser_id_expression (parser, /*template_p=*/false,
24935                                       /*check_dependency_p=*/true,
24936                                       /*template_p=*/NULL,
24937                                       /*declarator_p=*/false,
24938                                       /*optional_p=*/false);
24939       if (name == error_mark_node)
24940         goto skip_comma;
24941
24942       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24943       if (decl == error_mark_node)
24944         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24945                                      token->location);
24946       else if (kind != 0)
24947         {
24948           tree u = build_omp_clause (token->location, kind);
24949           OMP_CLAUSE_DECL (u) = decl;
24950           OMP_CLAUSE_CHAIN (u) = list;
24951           list = u;
24952         }
24953       else
24954         list = tree_cons (decl, NULL_TREE, list);
24955
24956     get_comma:
24957       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24958         break;
24959       cp_lexer_consume_token (parser->lexer);
24960     }
24961
24962   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24963     {
24964       int ending;
24965
24966       /* Try to resync to an unnested comma.  Copied from
24967          cp_parser_parenthesized_expression_list.  */
24968     skip_comma:
24969       ending = cp_parser_skip_to_closing_parenthesis (parser,
24970                                                       /*recovering=*/true,
24971                                                       /*or_comma=*/true,
24972                                                       /*consume_paren=*/true);
24973       if (ending < 0)
24974         goto get_comma;
24975     }
24976
24977   return list;
24978 }
24979
24980 /* Similarly, but expect leading and trailing parenthesis.  This is a very
24981    common case for omp clauses.  */
24982
24983 static tree
24984 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24985 {
24986   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24987     return cp_parser_omp_var_list_no_open (parser, kind, list);
24988   return list;
24989 }
24990
24991 /* OpenMP 3.0:
24992    collapse ( constant-expression ) */
24993
24994 static tree
24995 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24996 {
24997   tree c, num;
24998   location_t loc;
24999   HOST_WIDE_INT n;
25000
25001   loc = cp_lexer_peek_token (parser->lexer)->location;
25002   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25003     return list;
25004
25005   num = cp_parser_constant_expression (parser, false, NULL);
25006
25007   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25008     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25009                                            /*or_comma=*/false,
25010                                            /*consume_paren=*/true);
25011
25012   if (num == error_mark_node)
25013     return list;
25014   num = fold_non_dependent_expr (num);
25015   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
25016       || !host_integerp (num, 0)
25017       || (n = tree_low_cst (num, 0)) <= 0
25018       || (int) n != n)
25019     {
25020       error_at (loc, "collapse argument needs positive constant integer expression");
25021       return list;
25022     }
25023
25024   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
25025   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
25026   OMP_CLAUSE_CHAIN (c) = list;
25027   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
25028
25029   return c;
25030 }
25031
25032 /* OpenMP 2.5:
25033    default ( shared | none ) */
25034
25035 static tree
25036 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
25037 {
25038   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
25039   tree c;
25040
25041   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25042     return list;
25043   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25044     {
25045       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25046       const char *p = IDENTIFIER_POINTER (id);
25047
25048       switch (p[0])
25049         {
25050         case 'n':
25051           if (strcmp ("none", p) != 0)
25052             goto invalid_kind;
25053           kind = OMP_CLAUSE_DEFAULT_NONE;
25054           break;
25055
25056         case 's':
25057           if (strcmp ("shared", p) != 0)
25058             goto invalid_kind;
25059           kind = OMP_CLAUSE_DEFAULT_SHARED;
25060           break;
25061
25062         default:
25063           goto invalid_kind;
25064         }
25065
25066       cp_lexer_consume_token (parser->lexer);
25067     }
25068   else
25069     {
25070     invalid_kind:
25071       cp_parser_error (parser, "expected %<none%> or %<shared%>");
25072     }
25073
25074   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25075     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25076                                            /*or_comma=*/false,
25077                                            /*consume_paren=*/true);
25078
25079   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
25080     return list;
25081
25082   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
25083   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
25084   OMP_CLAUSE_CHAIN (c) = list;
25085   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
25086
25087   return c;
25088 }
25089
25090 /* OpenMP 3.1:
25091    final ( expression ) */
25092
25093 static tree
25094 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
25095 {
25096   tree t, c;
25097
25098   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25099     return list;
25100
25101   t = cp_parser_condition (parser);
25102
25103   if (t == error_mark_node
25104       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25105     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25106                                            /*or_comma=*/false,
25107                                            /*consume_paren=*/true);
25108
25109   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
25110
25111   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
25112   OMP_CLAUSE_FINAL_EXPR (c) = t;
25113   OMP_CLAUSE_CHAIN (c) = list;
25114
25115   return c;
25116 }
25117
25118 /* OpenMP 2.5:
25119    if ( expression ) */
25120
25121 static tree
25122 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
25123 {
25124   tree t, c;
25125
25126   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25127     return list;
25128
25129   t = cp_parser_condition (parser);
25130
25131   if (t == error_mark_node
25132       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25133     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25134                                            /*or_comma=*/false,
25135                                            /*consume_paren=*/true);
25136
25137   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
25138
25139   c = build_omp_clause (location, OMP_CLAUSE_IF);
25140   OMP_CLAUSE_IF_EXPR (c) = t;
25141   OMP_CLAUSE_CHAIN (c) = list;
25142
25143   return c;
25144 }
25145
25146 /* OpenMP 3.1:
25147    mergeable */
25148
25149 static tree
25150 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
25151                                 tree list, location_t location)
25152 {
25153   tree c;
25154
25155   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
25156                              location);
25157
25158   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
25159   OMP_CLAUSE_CHAIN (c) = list;
25160   return c;
25161 }
25162
25163 /* OpenMP 2.5:
25164    nowait */
25165
25166 static tree
25167 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
25168                              tree list, location_t location)
25169 {
25170   tree c;
25171
25172   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25173
25174   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25175   OMP_CLAUSE_CHAIN (c) = list;
25176   return c;
25177 }
25178
25179 /* OpenMP 2.5:
25180    num_threads ( expression ) */
25181
25182 static tree
25183 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25184                                   location_t location)
25185 {
25186   tree t, c;
25187
25188   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25189     return list;
25190
25191   t = cp_parser_expression (parser, false, NULL);
25192
25193   if (t == error_mark_node
25194       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25195     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25196                                            /*or_comma=*/false,
25197                                            /*consume_paren=*/true);
25198
25199   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25200                              "num_threads", location);
25201
25202   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25203   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25204   OMP_CLAUSE_CHAIN (c) = list;
25205
25206   return c;
25207 }
25208
25209 /* OpenMP 2.5:
25210    ordered */
25211
25212 static tree
25213 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25214                               tree list, location_t location)
25215 {
25216   tree c;
25217
25218   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25219                              "ordered", location);
25220
25221   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25222   OMP_CLAUSE_CHAIN (c) = list;
25223   return c;
25224 }
25225
25226 /* OpenMP 2.5:
25227    reduction ( reduction-operator : variable-list )
25228
25229    reduction-operator:
25230      One of: + * - & ^ | && ||
25231
25232    OpenMP 3.1:
25233
25234    reduction-operator:
25235      One of: + * - & ^ | && || min max  */
25236
25237 static tree
25238 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25239 {
25240   enum tree_code code;
25241   tree nlist, c;
25242
25243   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25244     return list;
25245
25246   switch (cp_lexer_peek_token (parser->lexer)->type)
25247     {
25248     case CPP_PLUS:
25249       code = PLUS_EXPR;
25250       break;
25251     case CPP_MULT:
25252       code = MULT_EXPR;
25253       break;
25254     case CPP_MINUS:
25255       code = MINUS_EXPR;
25256       break;
25257     case CPP_AND:
25258       code = BIT_AND_EXPR;
25259       break;
25260     case CPP_XOR:
25261       code = BIT_XOR_EXPR;
25262       break;
25263     case CPP_OR:
25264       code = BIT_IOR_EXPR;
25265       break;
25266     case CPP_AND_AND:
25267       code = TRUTH_ANDIF_EXPR;
25268       break;
25269     case CPP_OR_OR:
25270       code = TRUTH_ORIF_EXPR;
25271       break;
25272     case CPP_NAME:
25273       {
25274         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25275         const char *p = IDENTIFIER_POINTER (id);
25276
25277         if (strcmp (p, "min") == 0)
25278           {
25279             code = MIN_EXPR;
25280             break;
25281           }
25282         if (strcmp (p, "max") == 0)
25283           {
25284             code = MAX_EXPR;
25285             break;
25286           }
25287       }
25288       /* FALLTHROUGH */
25289     default:
25290       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25291                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25292     resync_fail:
25293       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25294                                              /*or_comma=*/false,
25295                                              /*consume_paren=*/true);
25296       return list;
25297     }
25298   cp_lexer_consume_token (parser->lexer);
25299
25300   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25301     goto resync_fail;
25302
25303   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25304   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25305     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25306
25307   return nlist;
25308 }
25309
25310 /* OpenMP 2.5:
25311    schedule ( schedule-kind )
25312    schedule ( schedule-kind , expression )
25313
25314    schedule-kind:
25315      static | dynamic | guided | runtime | auto  */
25316
25317 static tree
25318 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25319 {
25320   tree c, t;
25321
25322   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25323     return list;
25324
25325   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25326
25327   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25328     {
25329       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25330       const char *p = IDENTIFIER_POINTER (id);
25331
25332       switch (p[0])
25333         {
25334         case 'd':
25335           if (strcmp ("dynamic", p) != 0)
25336             goto invalid_kind;
25337           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25338           break;
25339
25340         case 'g':
25341           if (strcmp ("guided", p) != 0)
25342             goto invalid_kind;
25343           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25344           break;
25345
25346         case 'r':
25347           if (strcmp ("runtime", p) != 0)
25348             goto invalid_kind;
25349           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25350           break;
25351
25352         default:
25353           goto invalid_kind;
25354         }
25355     }
25356   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25357     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25358   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25359     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25360   else
25361     goto invalid_kind;
25362   cp_lexer_consume_token (parser->lexer);
25363
25364   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25365     {
25366       cp_token *token;
25367       cp_lexer_consume_token (parser->lexer);
25368
25369       token = cp_lexer_peek_token (parser->lexer);
25370       t = cp_parser_assignment_expression (parser, false, NULL);
25371
25372       if (t == error_mark_node)
25373         goto resync_fail;
25374       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25375         error_at (token->location, "schedule %<runtime%> does not take "
25376                   "a %<chunk_size%> parameter");
25377       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25378         error_at (token->location, "schedule %<auto%> does not take "
25379                   "a %<chunk_size%> parameter");
25380       else
25381         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25382
25383       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25384         goto resync_fail;
25385     }
25386   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25387     goto resync_fail;
25388
25389   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25390   OMP_CLAUSE_CHAIN (c) = list;
25391   return c;
25392
25393  invalid_kind:
25394   cp_parser_error (parser, "invalid schedule kind");
25395  resync_fail:
25396   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25397                                          /*or_comma=*/false,
25398                                          /*consume_paren=*/true);
25399   return list;
25400 }
25401
25402 /* OpenMP 3.0:
25403    untied */
25404
25405 static tree
25406 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25407                              tree list, location_t location)
25408 {
25409   tree c;
25410
25411   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25412
25413   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25414   OMP_CLAUSE_CHAIN (c) = list;
25415   return c;
25416 }
25417
25418 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25419    is a bitmask in MASK.  Return the list of clauses found; the result
25420    of clause default goes in *pdefault.  */
25421
25422 static tree
25423 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25424                            const char *where, cp_token *pragma_tok)
25425 {
25426   tree clauses = NULL;
25427   bool first = true;
25428   cp_token *token = NULL;
25429
25430   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25431     {
25432       pragma_omp_clause c_kind;
25433       const char *c_name;
25434       tree prev = clauses;
25435
25436       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25437         cp_lexer_consume_token (parser->lexer);
25438
25439       token = cp_lexer_peek_token (parser->lexer);
25440       c_kind = cp_parser_omp_clause_name (parser);
25441       first = false;
25442
25443       switch (c_kind)
25444         {
25445         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25446           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25447                                                    token->location);
25448           c_name = "collapse";
25449           break;
25450         case PRAGMA_OMP_CLAUSE_COPYIN:
25451           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25452           c_name = "copyin";
25453           break;
25454         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25455           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25456                                             clauses);
25457           c_name = "copyprivate";
25458           break;
25459         case PRAGMA_OMP_CLAUSE_DEFAULT:
25460           clauses = cp_parser_omp_clause_default (parser, clauses,
25461                                                   token->location);
25462           c_name = "default";
25463           break;
25464         case PRAGMA_OMP_CLAUSE_FINAL:
25465           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25466           c_name = "final";
25467           break;
25468         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25469           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25470                                             clauses);
25471           c_name = "firstprivate";
25472           break;
25473         case PRAGMA_OMP_CLAUSE_IF:
25474           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25475           c_name = "if";
25476           break;
25477         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25478           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25479                                             clauses);
25480           c_name = "lastprivate";
25481           break;
25482         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25483           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25484                                                     token->location);
25485           c_name = "mergeable";
25486           break;
25487         case PRAGMA_OMP_CLAUSE_NOWAIT:
25488           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25489           c_name = "nowait";
25490           break;
25491         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25492           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25493                                                       token->location);
25494           c_name = "num_threads";
25495           break;
25496         case PRAGMA_OMP_CLAUSE_ORDERED:
25497           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25498                                                   token->location);
25499           c_name = "ordered";
25500           break;
25501         case PRAGMA_OMP_CLAUSE_PRIVATE:
25502           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25503                                             clauses);
25504           c_name = "private";
25505           break;
25506         case PRAGMA_OMP_CLAUSE_REDUCTION:
25507           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25508           c_name = "reduction";
25509           break;
25510         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25511           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25512                                                    token->location);
25513           c_name = "schedule";
25514           break;
25515         case PRAGMA_OMP_CLAUSE_SHARED:
25516           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25517                                             clauses);
25518           c_name = "shared";
25519           break;
25520         case PRAGMA_OMP_CLAUSE_UNTIED:
25521           clauses = cp_parser_omp_clause_untied (parser, clauses,
25522                                                  token->location);
25523           c_name = "nowait";
25524           break;
25525         default:
25526           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25527           goto saw_error;
25528         }
25529
25530       if (((mask >> c_kind) & 1) == 0)
25531         {
25532           /* Remove the invalid clause(s) from the list to avoid
25533              confusing the rest of the compiler.  */
25534           clauses = prev;
25535           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25536         }
25537     }
25538  saw_error:
25539   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25540   return finish_omp_clauses (clauses);
25541 }
25542
25543 /* OpenMP 2.5:
25544    structured-block:
25545      statement
25546
25547    In practice, we're also interested in adding the statement to an
25548    outer node.  So it is convenient if we work around the fact that
25549    cp_parser_statement calls add_stmt.  */
25550
25551 static unsigned
25552 cp_parser_begin_omp_structured_block (cp_parser *parser)
25553 {
25554   unsigned save = parser->in_statement;
25555
25556   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25557      This preserves the "not within loop or switch" style error messages
25558      for nonsense cases like
25559         void foo() {
25560         #pragma omp single
25561           break;
25562         }
25563   */
25564   if (parser->in_statement)
25565     parser->in_statement = IN_OMP_BLOCK;
25566
25567   return save;
25568 }
25569
25570 static void
25571 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25572 {
25573   parser->in_statement = save;
25574 }
25575
25576 static tree
25577 cp_parser_omp_structured_block (cp_parser *parser)
25578 {
25579   tree stmt = begin_omp_structured_block ();
25580   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25581
25582   cp_parser_statement (parser, NULL_TREE, false, NULL);
25583
25584   cp_parser_end_omp_structured_block (parser, save);
25585   return finish_omp_structured_block (stmt);
25586 }
25587
25588 /* OpenMP 2.5:
25589    # pragma omp atomic new-line
25590      expression-stmt
25591
25592    expression-stmt:
25593      x binop= expr | x++ | ++x | x-- | --x
25594    binop:
25595      +, *, -, /, &, ^, |, <<, >>
25596
25597   where x is an lvalue expression with scalar type.
25598
25599    OpenMP 3.1:
25600    # pragma omp atomic new-line
25601      update-stmt
25602
25603    # pragma omp atomic read new-line
25604      read-stmt
25605
25606    # pragma omp atomic write new-line
25607      write-stmt
25608
25609    # pragma omp atomic update new-line
25610      update-stmt
25611
25612    # pragma omp atomic capture new-line
25613      capture-stmt
25614
25615    # pragma omp atomic capture new-line
25616      capture-block
25617
25618    read-stmt:
25619      v = x
25620    write-stmt:
25621      x = expr
25622    update-stmt:
25623      expression-stmt | x = x binop expr
25624    capture-stmt:
25625      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25626    capture-block:
25627      { v = x; update-stmt; } | { update-stmt; v = x; }
25628
25629   where x and v are lvalue expressions with scalar type.  */
25630
25631 static void
25632 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25633 {
25634   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25635   tree rhs1 = NULL_TREE, orig_lhs;
25636   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25637   bool structured_block = false;
25638
25639   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25640     {
25641       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25642       const char *p = IDENTIFIER_POINTER (id);
25643
25644       if (!strcmp (p, "read"))
25645         code = OMP_ATOMIC_READ;
25646       else if (!strcmp (p, "write"))
25647         code = NOP_EXPR;
25648       else if (!strcmp (p, "update"))
25649         code = OMP_ATOMIC;
25650       else if (!strcmp (p, "capture"))
25651         code = OMP_ATOMIC_CAPTURE_NEW;
25652       else
25653         p = NULL;
25654       if (p)
25655         cp_lexer_consume_token (parser->lexer);
25656     }
25657   cp_parser_require_pragma_eol (parser, pragma_tok);
25658
25659   switch (code)
25660     {
25661     case OMP_ATOMIC_READ:
25662     case NOP_EXPR: /* atomic write */
25663       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25664                                       /*cast_p=*/false, NULL);
25665       if (v == error_mark_node)
25666         goto saw_error;
25667       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25668         goto saw_error;
25669       if (code == NOP_EXPR)
25670         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25671       else
25672         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25673                                           /*cast_p=*/false, NULL);
25674       if (lhs == error_mark_node)
25675         goto saw_error;
25676       if (code == NOP_EXPR)
25677         {
25678           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25679              opcode.  */
25680           code = OMP_ATOMIC;
25681           rhs = lhs;
25682           lhs = v;
25683           v = NULL_TREE;
25684         }
25685       goto done;
25686     case OMP_ATOMIC_CAPTURE_NEW:
25687       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25688         {
25689           cp_lexer_consume_token (parser->lexer);
25690           structured_block = true;
25691         }
25692       else
25693         {
25694           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25695                                           /*cast_p=*/false, NULL);
25696           if (v == error_mark_node)
25697             goto saw_error;
25698           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25699             goto saw_error;
25700         }
25701     default:
25702       break;
25703     }
25704
25705 restart:
25706   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25707                                     /*cast_p=*/false, NULL);
25708   orig_lhs = lhs;
25709   switch (TREE_CODE (lhs))
25710     {
25711     case ERROR_MARK:
25712       goto saw_error;
25713
25714     case POSTINCREMENT_EXPR:
25715       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25716         code = OMP_ATOMIC_CAPTURE_OLD;
25717       /* FALLTHROUGH */
25718     case PREINCREMENT_EXPR:
25719       lhs = TREE_OPERAND (lhs, 0);
25720       opcode = PLUS_EXPR;
25721       rhs = integer_one_node;
25722       break;
25723
25724     case POSTDECREMENT_EXPR:
25725       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25726         code = OMP_ATOMIC_CAPTURE_OLD;
25727       /* FALLTHROUGH */
25728     case PREDECREMENT_EXPR:
25729       lhs = TREE_OPERAND (lhs, 0);
25730       opcode = MINUS_EXPR;
25731       rhs = integer_one_node;
25732       break;
25733
25734     case COMPOUND_EXPR:
25735       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25736          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25737          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25738          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25739          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25740                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25741             == BOOLEAN_TYPE)
25742        /* Undo effects of boolean_increment for post {in,de}crement.  */
25743        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25744       /* FALLTHRU */
25745     case MODIFY_EXPR:
25746       if (TREE_CODE (lhs) == MODIFY_EXPR
25747          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25748         {
25749           /* Undo effects of boolean_increment.  */
25750           if (integer_onep (TREE_OPERAND (lhs, 1)))
25751             {
25752               /* This is pre or post increment.  */
25753               rhs = TREE_OPERAND (lhs, 1);
25754               lhs = TREE_OPERAND (lhs, 0);
25755               opcode = NOP_EXPR;
25756               if (code == OMP_ATOMIC_CAPTURE_NEW
25757                   && !structured_block
25758                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25759                 code = OMP_ATOMIC_CAPTURE_OLD;
25760               break;
25761             }
25762         }
25763       /* FALLTHRU */
25764     default:
25765       switch (cp_lexer_peek_token (parser->lexer)->type)
25766         {
25767         case CPP_MULT_EQ:
25768           opcode = MULT_EXPR;
25769           break;
25770         case CPP_DIV_EQ:
25771           opcode = TRUNC_DIV_EXPR;
25772           break;
25773         case CPP_PLUS_EQ:
25774           opcode = PLUS_EXPR;
25775           break;
25776         case CPP_MINUS_EQ:
25777           opcode = MINUS_EXPR;
25778           break;
25779         case CPP_LSHIFT_EQ:
25780           opcode = LSHIFT_EXPR;
25781           break;
25782         case CPP_RSHIFT_EQ:
25783           opcode = RSHIFT_EXPR;
25784           break;
25785         case CPP_AND_EQ:
25786           opcode = BIT_AND_EXPR;
25787           break;
25788         case CPP_OR_EQ:
25789           opcode = BIT_IOR_EXPR;
25790           break;
25791         case CPP_XOR_EQ:
25792           opcode = BIT_XOR_EXPR;
25793           break;
25794         case CPP_EQ:
25795           if (structured_block || code == OMP_ATOMIC)
25796             {
25797               enum cp_parser_prec oprec;
25798               cp_token *token;
25799               cp_lexer_consume_token (parser->lexer);
25800               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25801                                                  /*cast_p=*/false, NULL);
25802               if (rhs1 == error_mark_node)
25803                 goto saw_error;
25804               token = cp_lexer_peek_token (parser->lexer);
25805               switch (token->type)
25806                 {
25807                 case CPP_SEMICOLON:
25808                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25809                     {
25810                       code = OMP_ATOMIC_CAPTURE_OLD;
25811                       v = lhs;
25812                       lhs = NULL_TREE;
25813                       lhs1 = rhs1;
25814                       rhs1 = NULL_TREE;
25815                       cp_lexer_consume_token (parser->lexer);
25816                       goto restart;
25817                     }
25818                   cp_parser_error (parser,
25819                                    "invalid form of %<#pragma omp atomic%>");
25820                   goto saw_error;
25821                 case CPP_MULT:
25822                   opcode = MULT_EXPR;
25823                   break;
25824                 case CPP_DIV:
25825                   opcode = TRUNC_DIV_EXPR;
25826                   break;
25827                 case CPP_PLUS:
25828                   opcode = PLUS_EXPR;
25829                   break;
25830                 case CPP_MINUS:
25831                   opcode = MINUS_EXPR;
25832                   break;
25833                 case CPP_LSHIFT:
25834                   opcode = LSHIFT_EXPR;
25835                   break;
25836                 case CPP_RSHIFT:
25837                   opcode = RSHIFT_EXPR;
25838                   break;
25839                 case CPP_AND:
25840                   opcode = BIT_AND_EXPR;
25841                   break;
25842                 case CPP_OR:
25843                   opcode = BIT_IOR_EXPR;
25844                   break;
25845                 case CPP_XOR:
25846                   opcode = BIT_XOR_EXPR;
25847                   break;
25848                 default:
25849                   cp_parser_error (parser,
25850                                    "invalid operator for %<#pragma omp atomic%>");
25851                   goto saw_error;
25852                 }
25853               oprec = TOKEN_PRECEDENCE (token);
25854               gcc_assert (oprec != PREC_NOT_OPERATOR);
25855               if (commutative_tree_code (opcode))
25856                 oprec = (enum cp_parser_prec) (oprec - 1);
25857               cp_lexer_consume_token (parser->lexer);
25858               rhs = cp_parser_binary_expression (parser, false, false,
25859                                                  oprec, NULL);
25860               if (rhs == error_mark_node)
25861                 goto saw_error;
25862               goto stmt_done;
25863             }
25864           /* FALLTHROUGH */
25865         default:
25866           cp_parser_error (parser,
25867                            "invalid operator for %<#pragma omp atomic%>");
25868           goto saw_error;
25869         }
25870       cp_lexer_consume_token (parser->lexer);
25871
25872       rhs = cp_parser_expression (parser, false, NULL);
25873       if (rhs == error_mark_node)
25874         goto saw_error;
25875       break;
25876     }
25877 stmt_done:
25878   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25879     {
25880       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25881         goto saw_error;
25882       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25883                                       /*cast_p=*/false, NULL);
25884       if (v == error_mark_node)
25885         goto saw_error;
25886       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25887         goto saw_error;
25888       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25889                                          /*cast_p=*/false, NULL);
25890       if (lhs1 == error_mark_node)
25891         goto saw_error;
25892     }
25893   if (structured_block)
25894     {
25895       cp_parser_consume_semicolon_at_end_of_statement (parser);
25896       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25897     }
25898 done:
25899   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25900   if (!structured_block)
25901     cp_parser_consume_semicolon_at_end_of_statement (parser);
25902   return;
25903
25904  saw_error:
25905   cp_parser_skip_to_end_of_block_or_statement (parser);
25906   if (structured_block)
25907     {
25908       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25909         cp_lexer_consume_token (parser->lexer);
25910       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25911         {
25912           cp_parser_skip_to_end_of_block_or_statement (parser);
25913           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25914             cp_lexer_consume_token (parser->lexer);
25915         }
25916     }
25917 }
25918
25919
25920 /* OpenMP 2.5:
25921    # pragma omp barrier new-line  */
25922
25923 static void
25924 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25925 {
25926   cp_parser_require_pragma_eol (parser, pragma_tok);
25927   finish_omp_barrier ();
25928 }
25929
25930 /* OpenMP 2.5:
25931    # pragma omp critical [(name)] new-line
25932      structured-block  */
25933
25934 static tree
25935 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25936 {
25937   tree stmt, name = NULL;
25938
25939   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25940     {
25941       cp_lexer_consume_token (parser->lexer);
25942
25943       name = cp_parser_identifier (parser);
25944
25945       if (name == error_mark_node
25946           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25947         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25948                                                /*or_comma=*/false,
25949                                                /*consume_paren=*/true);
25950       if (name == error_mark_node)
25951         name = NULL;
25952     }
25953   cp_parser_require_pragma_eol (parser, pragma_tok);
25954
25955   stmt = cp_parser_omp_structured_block (parser);
25956   return c_finish_omp_critical (input_location, stmt, name);
25957 }
25958
25959 /* OpenMP 2.5:
25960    # pragma omp flush flush-vars[opt] new-line
25961
25962    flush-vars:
25963      ( variable-list ) */
25964
25965 static void
25966 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25967 {
25968   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25969     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25970   cp_parser_require_pragma_eol (parser, pragma_tok);
25971
25972   finish_omp_flush ();
25973 }
25974
25975 /* Helper function, to parse omp for increment expression.  */
25976
25977 static tree
25978 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25979 {
25980   tree cond = cp_parser_binary_expression (parser, false, true,
25981                                            PREC_NOT_OPERATOR, NULL);
25982   if (cond == error_mark_node
25983       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25984     {
25985       cp_parser_skip_to_end_of_statement (parser);
25986       return error_mark_node;
25987     }
25988
25989   switch (TREE_CODE (cond))
25990     {
25991     case GT_EXPR:
25992     case GE_EXPR:
25993     case LT_EXPR:
25994     case LE_EXPR:
25995       break;
25996     default:
25997       return error_mark_node;
25998     }
25999
26000   /* If decl is an iterator, preserve LHS and RHS of the relational
26001      expr until finish_omp_for.  */
26002   if (decl
26003       && (type_dependent_expression_p (decl)
26004           || CLASS_TYPE_P (TREE_TYPE (decl))))
26005     return cond;
26006
26007   return build_x_binary_op (TREE_CODE (cond),
26008                             TREE_OPERAND (cond, 0), ERROR_MARK,
26009                             TREE_OPERAND (cond, 1), ERROR_MARK,
26010                             /*overload=*/NULL, tf_warning_or_error);
26011 }
26012
26013 /* Helper function, to parse omp for increment expression.  */
26014
26015 static tree
26016 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
26017 {
26018   cp_token *token = cp_lexer_peek_token (parser->lexer);
26019   enum tree_code op;
26020   tree lhs, rhs;
26021   cp_id_kind idk;
26022   bool decl_first;
26023
26024   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26025     {
26026       op = (token->type == CPP_PLUS_PLUS
26027             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
26028       cp_lexer_consume_token (parser->lexer);
26029       lhs = cp_parser_cast_expression (parser, false, false, NULL);
26030       if (lhs != decl)
26031         return error_mark_node;
26032       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26033     }
26034
26035   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
26036   if (lhs != decl)
26037     return error_mark_node;
26038
26039   token = cp_lexer_peek_token (parser->lexer);
26040   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
26041     {
26042       op = (token->type == CPP_PLUS_PLUS
26043             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
26044       cp_lexer_consume_token (parser->lexer);
26045       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
26046     }
26047
26048   op = cp_parser_assignment_operator_opt (parser);
26049   if (op == ERROR_MARK)
26050     return error_mark_node;
26051
26052   if (op != NOP_EXPR)
26053     {
26054       rhs = cp_parser_assignment_expression (parser, false, NULL);
26055       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
26056       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26057     }
26058
26059   lhs = cp_parser_binary_expression (parser, false, false,
26060                                      PREC_ADDITIVE_EXPRESSION, NULL);
26061   token = cp_lexer_peek_token (parser->lexer);
26062   decl_first = lhs == decl;
26063   if (decl_first)
26064     lhs = NULL_TREE;
26065   if (token->type != CPP_PLUS
26066       && token->type != CPP_MINUS)
26067     return error_mark_node;
26068
26069   do
26070     {
26071       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
26072       cp_lexer_consume_token (parser->lexer);
26073       rhs = cp_parser_binary_expression (parser, false, false,
26074                                          PREC_ADDITIVE_EXPRESSION, NULL);
26075       token = cp_lexer_peek_token (parser->lexer);
26076       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
26077         {
26078           if (lhs == NULL_TREE)
26079             {
26080               if (op == PLUS_EXPR)
26081                 lhs = rhs;
26082               else
26083                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
26084             }
26085           else
26086             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
26087                                      NULL, tf_warning_or_error);
26088         }
26089     }
26090   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
26091
26092   if (!decl_first)
26093     {
26094       if (rhs != decl || op == MINUS_EXPR)
26095         return error_mark_node;
26096       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
26097     }
26098   else
26099     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
26100
26101   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
26102 }
26103
26104 /* Parse the restricted form of the for statement allowed by OpenMP.  */
26105
26106 static tree
26107 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
26108 {
26109   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
26110   tree real_decl, initv, condv, incrv, declv;
26111   tree this_pre_body, cl;
26112   location_t loc_first;
26113   bool collapse_err = false;
26114   int i, collapse = 1, nbraces = 0;
26115   VEC(tree,gc) *for_block = make_tree_vector ();
26116
26117   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
26118     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
26119       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
26120
26121   gcc_assert (collapse >= 1);
26122
26123   declv = make_tree_vec (collapse);
26124   initv = make_tree_vec (collapse);
26125   condv = make_tree_vec (collapse);
26126   incrv = make_tree_vec (collapse);
26127
26128   loc_first = cp_lexer_peek_token (parser->lexer)->location;
26129
26130   for (i = 0; i < collapse; i++)
26131     {
26132       int bracecount = 0;
26133       bool add_private_clause = false;
26134       location_t loc;
26135
26136       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26137         {
26138           cp_parser_error (parser, "for statement expected");
26139           return NULL;
26140         }
26141       loc = cp_lexer_consume_token (parser->lexer)->location;
26142
26143       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
26144         return NULL;
26145
26146       init = decl = real_decl = NULL;
26147       this_pre_body = push_stmt_list ();
26148       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26149         {
26150           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
26151
26152              init-expr:
26153                        var = lb
26154                        integer-type var = lb
26155                        random-access-iterator-type var = lb
26156                        pointer-type var = lb
26157           */
26158           cp_decl_specifier_seq type_specifiers;
26159
26160           /* First, try to parse as an initialized declaration.  See
26161              cp_parser_condition, from whence the bulk of this is copied.  */
26162
26163           cp_parser_parse_tentatively (parser);
26164           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
26165                                         /*is_trailing_return=*/false,
26166                                         &type_specifiers);
26167           if (cp_parser_parse_definitely (parser))
26168             {
26169               /* If parsing a type specifier seq succeeded, then this
26170                  MUST be a initialized declaration.  */
26171               tree asm_specification, attributes;
26172               cp_declarator *declarator;
26173
26174               declarator = cp_parser_declarator (parser,
26175                                                  CP_PARSER_DECLARATOR_NAMED,
26176                                                  /*ctor_dtor_or_conv_p=*/NULL,
26177                                                  /*parenthesized_p=*/NULL,
26178                                                  /*member_p=*/false);
26179               attributes = cp_parser_attributes_opt (parser);
26180               asm_specification = cp_parser_asm_specification_opt (parser);
26181
26182               if (declarator == cp_error_declarator) 
26183                 cp_parser_skip_to_end_of_statement (parser);
26184
26185               else 
26186                 {
26187                   tree pushed_scope, auto_node;
26188
26189                   decl = start_decl (declarator, &type_specifiers,
26190                                      SD_INITIALIZED, attributes,
26191                                      /*prefix_attributes=*/NULL_TREE,
26192                                      &pushed_scope);
26193
26194                   auto_node = type_uses_auto (TREE_TYPE (decl));
26195                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26196                     {
26197                       if (cp_lexer_next_token_is (parser->lexer, 
26198                                                   CPP_OPEN_PAREN))
26199                         error ("parenthesized initialization is not allowed in "
26200                                "OpenMP %<for%> loop");
26201                       else
26202                         /* Trigger an error.  */
26203                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26204
26205                       init = error_mark_node;
26206                       cp_parser_skip_to_end_of_statement (parser);
26207                     }
26208                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26209                            || type_dependent_expression_p (decl)
26210                            || auto_node)
26211                     {
26212                       bool is_direct_init, is_non_constant_init;
26213
26214                       init = cp_parser_initializer (parser,
26215                                                     &is_direct_init,
26216                                                     &is_non_constant_init);
26217
26218                       if (auto_node)
26219                         {
26220                           TREE_TYPE (decl)
26221                             = do_auto_deduction (TREE_TYPE (decl), init,
26222                                                  auto_node);
26223
26224                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26225                               && !type_dependent_expression_p (decl))
26226                             goto non_class;
26227                         }
26228                       
26229                       cp_finish_decl (decl, init, !is_non_constant_init,
26230                                       asm_specification,
26231                                       LOOKUP_ONLYCONVERTING);
26232                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26233                         {
26234                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26235                           init = NULL_TREE;
26236                         }
26237                       else
26238                         init = pop_stmt_list (this_pre_body);
26239                       this_pre_body = NULL_TREE;
26240                     }
26241                   else
26242                     {
26243                       /* Consume '='.  */
26244                       cp_lexer_consume_token (parser->lexer);
26245                       init = cp_parser_assignment_expression (parser, false, NULL);
26246
26247                     non_class:
26248                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26249                         init = error_mark_node;
26250                       else
26251                         cp_finish_decl (decl, NULL_TREE,
26252                                         /*init_const_expr_p=*/false,
26253                                         asm_specification,
26254                                         LOOKUP_ONLYCONVERTING);
26255                     }
26256
26257                   if (pushed_scope)
26258                     pop_scope (pushed_scope);
26259                 }
26260             }
26261           else 
26262             {
26263               cp_id_kind idk;
26264               /* If parsing a type specifier sequence failed, then
26265                  this MUST be a simple expression.  */
26266               cp_parser_parse_tentatively (parser);
26267               decl = cp_parser_primary_expression (parser, false, false,
26268                                                    false, &idk);
26269               if (!cp_parser_error_occurred (parser)
26270                   && decl
26271                   && DECL_P (decl)
26272                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26273                 {
26274                   tree rhs;
26275
26276                   cp_parser_parse_definitely (parser);
26277                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26278                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26279                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26280                                                          rhs,
26281                                                          tf_warning_or_error));
26282                   add_private_clause = true;
26283                 }
26284               else
26285                 {
26286                   decl = NULL;
26287                   cp_parser_abort_tentative_parse (parser);
26288                   init = cp_parser_expression (parser, false, NULL);
26289                   if (init)
26290                     {
26291                       if (TREE_CODE (init) == MODIFY_EXPR
26292                           || TREE_CODE (init) == MODOP_EXPR)
26293                         real_decl = TREE_OPERAND (init, 0);
26294                     }
26295                 }
26296             }
26297         }
26298       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26299       if (this_pre_body)
26300         {
26301           this_pre_body = pop_stmt_list (this_pre_body);
26302           if (pre_body)
26303             {
26304               tree t = pre_body;
26305               pre_body = push_stmt_list ();
26306               add_stmt (t);
26307               add_stmt (this_pre_body);
26308               pre_body = pop_stmt_list (pre_body);
26309             }
26310           else
26311             pre_body = this_pre_body;
26312         }
26313
26314       if (decl)
26315         real_decl = decl;
26316       if (par_clauses != NULL && real_decl != NULL_TREE)
26317         {
26318           tree *c;
26319           for (c = par_clauses; *c ; )
26320             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26321                 && OMP_CLAUSE_DECL (*c) == real_decl)
26322               {
26323                 error_at (loc, "iteration variable %qD"
26324                           " should not be firstprivate", real_decl);
26325                 *c = OMP_CLAUSE_CHAIN (*c);
26326               }
26327             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26328                      && OMP_CLAUSE_DECL (*c) == real_decl)
26329               {
26330                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26331                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26332                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26333                 OMP_CLAUSE_DECL (l) = real_decl;
26334                 OMP_CLAUSE_CHAIN (l) = clauses;
26335                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26336                 clauses = l;
26337                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26338                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26339                 add_private_clause = false;
26340               }
26341             else
26342               {
26343                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26344                     && OMP_CLAUSE_DECL (*c) == real_decl)
26345                   add_private_clause = false;
26346                 c = &OMP_CLAUSE_CHAIN (*c);
26347               }
26348         }
26349
26350       if (add_private_clause)
26351         {
26352           tree c;
26353           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26354             {
26355               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26356                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26357                   && OMP_CLAUSE_DECL (c) == decl)
26358                 break;
26359               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26360                        && OMP_CLAUSE_DECL (c) == decl)
26361                 error_at (loc, "iteration variable %qD "
26362                           "should not be firstprivate",
26363                           decl);
26364               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26365                        && OMP_CLAUSE_DECL (c) == decl)
26366                 error_at (loc, "iteration variable %qD should not be reduction",
26367                           decl);
26368             }
26369           if (c == NULL)
26370             {
26371               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26372               OMP_CLAUSE_DECL (c) = decl;
26373               c = finish_omp_clauses (c);
26374               if (c)
26375                 {
26376                   OMP_CLAUSE_CHAIN (c) = clauses;
26377                   clauses = c;
26378                 }
26379             }
26380         }
26381
26382       cond = NULL;
26383       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26384         cond = cp_parser_omp_for_cond (parser, decl);
26385       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26386
26387       incr = NULL;
26388       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26389         {
26390           /* If decl is an iterator, preserve the operator on decl
26391              until finish_omp_for.  */
26392           if (real_decl
26393               && ((processing_template_decl
26394                    && !POINTER_TYPE_P (TREE_TYPE (real_decl)))
26395                   || CLASS_TYPE_P (TREE_TYPE (real_decl))))
26396             incr = cp_parser_omp_for_incr (parser, real_decl);
26397           else
26398             incr = cp_parser_expression (parser, false, NULL);
26399         }
26400
26401       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26402         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26403                                                /*or_comma=*/false,
26404                                                /*consume_paren=*/true);
26405
26406       TREE_VEC_ELT (declv, i) = decl;
26407       TREE_VEC_ELT (initv, i) = init;
26408       TREE_VEC_ELT (condv, i) = cond;
26409       TREE_VEC_ELT (incrv, i) = incr;
26410
26411       if (i == collapse - 1)
26412         break;
26413
26414       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26415          in between the collapsed for loops to be still considered perfectly
26416          nested.  Hopefully the final version clarifies this.
26417          For now handle (multiple) {'s and empty statements.  */
26418       cp_parser_parse_tentatively (parser);
26419       do
26420         {
26421           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26422             break;
26423           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26424             {
26425               cp_lexer_consume_token (parser->lexer);
26426               bracecount++;
26427             }
26428           else if (bracecount
26429                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26430             cp_lexer_consume_token (parser->lexer);
26431           else
26432             {
26433               loc = cp_lexer_peek_token (parser->lexer)->location;
26434               error_at (loc, "not enough collapsed for loops");
26435               collapse_err = true;
26436               cp_parser_abort_tentative_parse (parser);
26437               declv = NULL_TREE;
26438               break;
26439             }
26440         }
26441       while (1);
26442
26443       if (declv)
26444         {
26445           cp_parser_parse_definitely (parser);
26446           nbraces += bracecount;
26447         }
26448     }
26449
26450   /* Note that we saved the original contents of this flag when we entered
26451      the structured block, and so we don't need to re-save it here.  */
26452   parser->in_statement = IN_OMP_FOR;
26453
26454   /* Note that the grammar doesn't call for a structured block here,
26455      though the loop as a whole is a structured block.  */
26456   body = push_stmt_list ();
26457   cp_parser_statement (parser, NULL_TREE, false, NULL);
26458   body = pop_stmt_list (body);
26459
26460   if (declv == NULL_TREE)
26461     ret = NULL_TREE;
26462   else
26463     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26464                           pre_body, clauses);
26465
26466   while (nbraces)
26467     {
26468       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26469         {
26470           cp_lexer_consume_token (parser->lexer);
26471           nbraces--;
26472         }
26473       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26474         cp_lexer_consume_token (parser->lexer);
26475       else
26476         {
26477           if (!collapse_err)
26478             {
26479               error_at (cp_lexer_peek_token (parser->lexer)->location,
26480                         "collapsed loops not perfectly nested");
26481             }
26482           collapse_err = true;
26483           cp_parser_statement_seq_opt (parser, NULL);
26484           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26485             break;
26486         }
26487     }
26488
26489   while (!VEC_empty (tree, for_block))
26490     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26491   release_tree_vector (for_block);
26492
26493   return ret;
26494 }
26495
26496 /* OpenMP 2.5:
26497    #pragma omp for for-clause[optseq] new-line
26498      for-loop  */
26499
26500 #define OMP_FOR_CLAUSE_MASK                             \
26501         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26502         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26503         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26504         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26505         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26506         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26507         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26508         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26509
26510 static tree
26511 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26512 {
26513   tree clauses, sb, ret;
26514   unsigned int save;
26515
26516   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26517                                        "#pragma omp for", pragma_tok);
26518
26519   sb = begin_omp_structured_block ();
26520   save = cp_parser_begin_omp_structured_block (parser);
26521
26522   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26523
26524   cp_parser_end_omp_structured_block (parser, save);
26525   add_stmt (finish_omp_structured_block (sb));
26526
26527   return ret;
26528 }
26529
26530 /* OpenMP 2.5:
26531    # pragma omp master new-line
26532      structured-block  */
26533
26534 static tree
26535 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26536 {
26537   cp_parser_require_pragma_eol (parser, pragma_tok);
26538   return c_finish_omp_master (input_location,
26539                               cp_parser_omp_structured_block (parser));
26540 }
26541
26542 /* OpenMP 2.5:
26543    # pragma omp ordered new-line
26544      structured-block  */
26545
26546 static tree
26547 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26548 {
26549   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26550   cp_parser_require_pragma_eol (parser, pragma_tok);
26551   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26552 }
26553
26554 /* OpenMP 2.5:
26555
26556    section-scope:
26557      { section-sequence }
26558
26559    section-sequence:
26560      section-directive[opt] structured-block
26561      section-sequence section-directive structured-block  */
26562
26563 static tree
26564 cp_parser_omp_sections_scope (cp_parser *parser)
26565 {
26566   tree stmt, substmt;
26567   bool error_suppress = false;
26568   cp_token *tok;
26569
26570   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26571     return NULL_TREE;
26572
26573   stmt = push_stmt_list ();
26574
26575   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26576     {
26577       unsigned save;
26578
26579       substmt = begin_omp_structured_block ();
26580       save = cp_parser_begin_omp_structured_block (parser);
26581
26582       while (1)
26583         {
26584           cp_parser_statement (parser, NULL_TREE, false, NULL);
26585
26586           tok = cp_lexer_peek_token (parser->lexer);
26587           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26588             break;
26589           if (tok->type == CPP_CLOSE_BRACE)
26590             break;
26591           if (tok->type == CPP_EOF)
26592             break;
26593         }
26594
26595       cp_parser_end_omp_structured_block (parser, save);
26596       substmt = finish_omp_structured_block (substmt);
26597       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26598       add_stmt (substmt);
26599     }
26600
26601   while (1)
26602     {
26603       tok = cp_lexer_peek_token (parser->lexer);
26604       if (tok->type == CPP_CLOSE_BRACE)
26605         break;
26606       if (tok->type == CPP_EOF)
26607         break;
26608
26609       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26610         {
26611           cp_lexer_consume_token (parser->lexer);
26612           cp_parser_require_pragma_eol (parser, tok);
26613           error_suppress = false;
26614         }
26615       else if (!error_suppress)
26616         {
26617           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26618           error_suppress = true;
26619         }
26620
26621       substmt = cp_parser_omp_structured_block (parser);
26622       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26623       add_stmt (substmt);
26624     }
26625   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26626
26627   substmt = pop_stmt_list (stmt);
26628
26629   stmt = make_node (OMP_SECTIONS);
26630   TREE_TYPE (stmt) = void_type_node;
26631   OMP_SECTIONS_BODY (stmt) = substmt;
26632
26633   add_stmt (stmt);
26634   return stmt;
26635 }
26636
26637 /* OpenMP 2.5:
26638    # pragma omp sections sections-clause[optseq] newline
26639      sections-scope  */
26640
26641 #define OMP_SECTIONS_CLAUSE_MASK                        \
26642         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26643         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26644         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26645         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26646         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26647
26648 static tree
26649 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26650 {
26651   tree clauses, ret;
26652
26653   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26654                                        "#pragma omp sections", pragma_tok);
26655
26656   ret = cp_parser_omp_sections_scope (parser);
26657   if (ret)
26658     OMP_SECTIONS_CLAUSES (ret) = clauses;
26659
26660   return ret;
26661 }
26662
26663 /* OpenMP 2.5:
26664    # pragma parallel parallel-clause new-line
26665    # pragma parallel for parallel-for-clause new-line
26666    # pragma parallel sections parallel-sections-clause new-line  */
26667
26668 #define OMP_PARALLEL_CLAUSE_MASK                        \
26669         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26670         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26671         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26672         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26673         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26674         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26675         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26676         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26677
26678 static tree
26679 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26680 {
26681   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26682   const char *p_name = "#pragma omp parallel";
26683   tree stmt, clauses, par_clause, ws_clause, block;
26684   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26685   unsigned int save;
26686   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26687
26688   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26689     {
26690       cp_lexer_consume_token (parser->lexer);
26691       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26692       p_name = "#pragma omp parallel for";
26693       mask |= OMP_FOR_CLAUSE_MASK;
26694       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26695     }
26696   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26697     {
26698       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26699       const char *p = IDENTIFIER_POINTER (id);
26700       if (strcmp (p, "sections") == 0)
26701         {
26702           cp_lexer_consume_token (parser->lexer);
26703           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26704           p_name = "#pragma omp parallel sections";
26705           mask |= OMP_SECTIONS_CLAUSE_MASK;
26706           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26707         }
26708     }
26709
26710   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26711   block = begin_omp_parallel ();
26712   save = cp_parser_begin_omp_structured_block (parser);
26713
26714   switch (p_kind)
26715     {
26716     case PRAGMA_OMP_PARALLEL:
26717       cp_parser_statement (parser, NULL_TREE, false, NULL);
26718       par_clause = clauses;
26719       break;
26720
26721     case PRAGMA_OMP_PARALLEL_FOR:
26722       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26723       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26724       break;
26725
26726     case PRAGMA_OMP_PARALLEL_SECTIONS:
26727       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26728       stmt = cp_parser_omp_sections_scope (parser);
26729       if (stmt)
26730         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26731       break;
26732
26733     default:
26734       gcc_unreachable ();
26735     }
26736
26737   cp_parser_end_omp_structured_block (parser, save);
26738   stmt = finish_omp_parallel (par_clause, block);
26739   if (p_kind != PRAGMA_OMP_PARALLEL)
26740     OMP_PARALLEL_COMBINED (stmt) = 1;
26741   return stmt;
26742 }
26743
26744 /* OpenMP 2.5:
26745    # pragma omp single single-clause[optseq] new-line
26746      structured-block  */
26747
26748 #define OMP_SINGLE_CLAUSE_MASK                          \
26749         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26750         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26751         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26752         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26753
26754 static tree
26755 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26756 {
26757   tree stmt = make_node (OMP_SINGLE);
26758   TREE_TYPE (stmt) = void_type_node;
26759
26760   OMP_SINGLE_CLAUSES (stmt)
26761     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26762                                  "#pragma omp single", pragma_tok);
26763   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26764
26765   return add_stmt (stmt);
26766 }
26767
26768 /* OpenMP 3.0:
26769    # pragma omp task task-clause[optseq] new-line
26770      structured-block  */
26771
26772 #define OMP_TASK_CLAUSE_MASK                            \
26773         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26774         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26775         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26776         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26777         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26778         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26779         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26780         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26781
26782 static tree
26783 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26784 {
26785   tree clauses, block;
26786   unsigned int save;
26787
26788   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26789                                        "#pragma omp task", pragma_tok);
26790   block = begin_omp_task ();
26791   save = cp_parser_begin_omp_structured_block (parser);
26792   cp_parser_statement (parser, NULL_TREE, false, NULL);
26793   cp_parser_end_omp_structured_block (parser, save);
26794   return finish_omp_task (clauses, block);
26795 }
26796
26797 /* OpenMP 3.0:
26798    # pragma omp taskwait new-line  */
26799
26800 static void
26801 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26802 {
26803   cp_parser_require_pragma_eol (parser, pragma_tok);
26804   finish_omp_taskwait ();
26805 }
26806
26807 /* OpenMP 3.1:
26808    # pragma omp taskyield new-line  */
26809
26810 static void
26811 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26812 {
26813   cp_parser_require_pragma_eol (parser, pragma_tok);
26814   finish_omp_taskyield ();
26815 }
26816
26817 /* OpenMP 2.5:
26818    # pragma omp threadprivate (variable-list) */
26819
26820 static void
26821 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26822 {
26823   tree vars;
26824
26825   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26826   cp_parser_require_pragma_eol (parser, pragma_tok);
26827
26828   finish_omp_threadprivate (vars);
26829 }
26830
26831 /* Main entry point to OpenMP statement pragmas.  */
26832
26833 static void
26834 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26835 {
26836   tree stmt;
26837
26838   switch (pragma_tok->pragma_kind)
26839     {
26840     case PRAGMA_OMP_ATOMIC:
26841       cp_parser_omp_atomic (parser, pragma_tok);
26842       return;
26843     case PRAGMA_OMP_CRITICAL:
26844       stmt = cp_parser_omp_critical (parser, pragma_tok);
26845       break;
26846     case PRAGMA_OMP_FOR:
26847       stmt = cp_parser_omp_for (parser, pragma_tok);
26848       break;
26849     case PRAGMA_OMP_MASTER:
26850       stmt = cp_parser_omp_master (parser, pragma_tok);
26851       break;
26852     case PRAGMA_OMP_ORDERED:
26853       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26854       break;
26855     case PRAGMA_OMP_PARALLEL:
26856       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26857       break;
26858     case PRAGMA_OMP_SECTIONS:
26859       stmt = cp_parser_omp_sections (parser, pragma_tok);
26860       break;
26861     case PRAGMA_OMP_SINGLE:
26862       stmt = cp_parser_omp_single (parser, pragma_tok);
26863       break;
26864     case PRAGMA_OMP_TASK:
26865       stmt = cp_parser_omp_task (parser, pragma_tok);
26866       break;
26867     default:
26868       gcc_unreachable ();
26869     }
26870
26871   if (stmt)
26872     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26873 }
26874 \f
26875 /* Transactional Memory parsing routines.  */
26876
26877 /* Parse a transaction attribute.
26878
26879    txn-attribute:
26880         attribute
26881         [ [ identifier ] ]
26882
26883    ??? Simplify this when C++0x bracket attributes are
26884    implemented properly.  */
26885
26886 static tree
26887 cp_parser_txn_attribute_opt (cp_parser *parser)
26888 {
26889   cp_token *token;
26890   tree attr_name, attr = NULL;
26891
26892   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26893     return cp_parser_attributes_opt (parser);
26894
26895   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26896     return NULL_TREE;
26897   cp_lexer_consume_token (parser->lexer);
26898   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26899     goto error1;
26900
26901   token = cp_lexer_peek_token (parser->lexer);
26902   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26903     {
26904       token = cp_lexer_consume_token (parser->lexer);
26905
26906       attr_name = (token->type == CPP_KEYWORD
26907                    /* For keywords, use the canonical spelling,
26908                       not the parsed identifier.  */
26909                    ? ridpointers[(int) token->keyword]
26910                    : token->u.value);
26911       attr = build_tree_list (attr_name, NULL_TREE);
26912     }
26913   else
26914     cp_parser_error (parser, "expected identifier");
26915
26916   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26917  error1:
26918   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26919   return attr;
26920 }
26921
26922 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26923
26924    transaction-statement:
26925      __transaction_atomic txn-attribute[opt] txn-noexcept-spec[opt]
26926        compound-statement
26927      __transaction_relaxed txn-noexcept-spec[opt] compound-statement
26928 */
26929
26930 static tree
26931 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26932 {
26933   unsigned char old_in = parser->in_transaction;
26934   unsigned char this_in = 1, new_in;
26935   cp_token *token;
26936   tree stmt, attrs, noex;
26937
26938   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26939       || keyword == RID_TRANSACTION_RELAXED);
26940   token = cp_parser_require_keyword (parser, keyword,
26941       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26942           : RT_TRANSACTION_RELAXED));
26943   gcc_assert (token != NULL);
26944
26945   if (keyword == RID_TRANSACTION_RELAXED)
26946     this_in |= TM_STMT_ATTR_RELAXED;
26947   else
26948     {
26949       attrs = cp_parser_txn_attribute_opt (parser);
26950       if (attrs)
26951         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26952     }
26953
26954   /* Parse a noexcept specification.  */
26955   noex = cp_parser_noexcept_specification_opt (parser, true, NULL, true);
26956
26957   /* Keep track if we're in the lexical scope of an outer transaction.  */
26958   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
26959
26960   stmt = begin_transaction_stmt (token->location, NULL, this_in);
26961
26962   parser->in_transaction = new_in;
26963   cp_parser_compound_statement (parser, NULL, false, false);
26964   parser->in_transaction = old_in;
26965
26966   finish_transaction_stmt (stmt, NULL, this_in, noex);
26967
26968   return stmt;
26969 }
26970
26971 /* Parse a __transaction_atomic or __transaction_relaxed expression.
26972
26973    transaction-expression:
26974      __transaction_atomic txn-noexcept-spec[opt] ( expression )
26975      __transaction_relaxed txn-noexcept-spec[opt] ( expression )
26976 */
26977
26978 static tree
26979 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
26980 {
26981   unsigned char old_in = parser->in_transaction;
26982   unsigned char this_in = 1;
26983   cp_token *token;
26984   tree expr, noex;
26985   bool noex_expr;
26986
26987   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26988       || keyword == RID_TRANSACTION_RELAXED);
26989
26990   if (!flag_tm)
26991     error (keyword == RID_TRANSACTION_RELAXED
26992            ? G_("%<__transaction_relaxed%> without transactional memory "
26993                 "support enabled")
26994            : G_("%<__transaction_atomic%> without transactional memory "
26995                 "support enabled"));
26996
26997   token = cp_parser_require_keyword (parser, keyword,
26998       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26999           : RT_TRANSACTION_RELAXED));
27000   gcc_assert (token != NULL);
27001
27002   if (keyword == RID_TRANSACTION_RELAXED)
27003     this_in |= TM_STMT_ATTR_RELAXED;
27004
27005   /* Set this early.  This might mean that we allow transaction_cancel in
27006      an expression that we find out later actually has to be a constexpr.
27007      However, we expect that cxx_constant_value will be able to deal with
27008      this; also, if the noexcept has no constexpr, then what we parse next
27009      really is a transaction's body.  */
27010   parser->in_transaction = this_in;
27011
27012   /* Parse a noexcept specification.  */
27013   noex = cp_parser_noexcept_specification_opt (parser, false, &noex_expr,
27014                                                true);
27015
27016   if (!noex || !noex_expr
27017       || cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
27018     {
27019       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
27020
27021       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
27022       finish_parenthesized_expr (expr);
27023
27024       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
27025     }
27026   else
27027     {
27028       /* The only expression that is available got parsed for the noexcept
27029          already.  noexcept is true then.  */
27030       expr = noex;
27031       noex = boolean_true_node;
27032     }
27033
27034   expr = build_transaction_expr (token->location, expr, this_in, noex);
27035   parser->in_transaction = old_in;
27036
27037   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
27038     return error_mark_node;
27039
27040   return (flag_tm ? expr : error_mark_node);
27041 }
27042
27043 /* Parse a function-transaction-block.
27044
27045    function-transaction-block:
27046      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
27047          function-body
27048      __transaction_atomic txn-attribute[opt] function-try-block
27049      __transaction_relaxed ctor-initializer[opt] function-body
27050      __transaction_relaxed function-try-block
27051 */
27052
27053 static bool
27054 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
27055 {
27056   unsigned char old_in = parser->in_transaction;
27057   unsigned char new_in = 1;
27058   tree compound_stmt, stmt, attrs;
27059   bool ctor_initializer_p;
27060   cp_token *token;
27061
27062   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
27063       || keyword == RID_TRANSACTION_RELAXED);
27064   token = cp_parser_require_keyword (parser, keyword,
27065       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
27066           : RT_TRANSACTION_RELAXED));
27067   gcc_assert (token != NULL);
27068
27069   if (keyword == RID_TRANSACTION_RELAXED)
27070     new_in |= TM_STMT_ATTR_RELAXED;
27071   else
27072     {
27073       attrs = cp_parser_txn_attribute_opt (parser);
27074       if (attrs)
27075         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
27076     }
27077
27078   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
27079
27080   parser->in_transaction = new_in;
27081
27082   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
27083     ctor_initializer_p = cp_parser_function_try_block (parser);
27084   else
27085     ctor_initializer_p
27086       = cp_parser_ctor_initializer_opt_and_function_body (parser);
27087
27088   parser->in_transaction = old_in;
27089
27090   finish_transaction_stmt (stmt, compound_stmt, new_in, NULL_TREE);
27091
27092   return ctor_initializer_p;
27093 }
27094
27095 /* Parse a __transaction_cancel statement.
27096
27097    cancel-statement:
27098      __transaction_cancel txn-attribute[opt] ;
27099      __transaction_cancel txn-attribute[opt] throw-expression ;
27100
27101    ??? Cancel and throw is not yet implemented.  */
27102
27103 static tree
27104 cp_parser_transaction_cancel (cp_parser *parser)
27105 {
27106   cp_token *token;
27107   bool is_outer = false;
27108   tree stmt, attrs;
27109
27110   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
27111                                      RT_TRANSACTION_CANCEL);
27112   gcc_assert (token != NULL);
27113
27114   attrs = cp_parser_txn_attribute_opt (parser);
27115   if (attrs)
27116     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
27117
27118   /* ??? Parse cancel-and-throw here.  */
27119
27120   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
27121
27122   if (!flag_tm)
27123     {
27124       error_at (token->location, "%<__transaction_cancel%> without "
27125                 "transactional memory support enabled");
27126       return error_mark_node;
27127     }
27128   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
27129     {
27130       error_at (token->location, "%<__transaction_cancel%> within a "
27131                 "%<__transaction_relaxed%>");
27132       return error_mark_node;
27133     }
27134   else if (is_outer)
27135     {
27136       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
27137           && !is_tm_may_cancel_outer (current_function_decl))
27138         {
27139           error_at (token->location, "outer %<__transaction_cancel%> not "
27140                     "within outer %<__transaction_atomic%>");
27141           error_at (token->location,
27142                     "  or a %<transaction_may_cancel_outer%> function");
27143           return error_mark_node;
27144         }
27145     }
27146   else if (parser->in_transaction == 0)
27147     {
27148       error_at (token->location, "%<__transaction_cancel%> not within "
27149                 "%<__transaction_atomic%>");
27150       return error_mark_node;
27151     }
27152
27153   stmt = build_tm_abort_call (token->location, is_outer);
27154   add_stmt (stmt);
27155   finish_stmt ();
27156
27157   return stmt;
27158 }
27159 \f
27160 /* The parser.  */
27161
27162 static GTY (()) cp_parser *the_parser;
27163
27164 \f
27165 /* Special handling for the first token or line in the file.  The first
27166    thing in the file might be #pragma GCC pch_preprocess, which loads a
27167    PCH file, which is a GC collection point.  So we need to handle this
27168    first pragma without benefit of an existing lexer structure.
27169
27170    Always returns one token to the caller in *FIRST_TOKEN.  This is
27171    either the true first token of the file, or the first token after
27172    the initial pragma.  */
27173
27174 static void
27175 cp_parser_initial_pragma (cp_token *first_token)
27176 {
27177   tree name = NULL;
27178
27179   cp_lexer_get_preprocessor_token (NULL, first_token);
27180   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
27181     return;
27182
27183   cp_lexer_get_preprocessor_token (NULL, first_token);
27184   if (first_token->type == CPP_STRING)
27185     {
27186       name = first_token->u.value;
27187
27188       cp_lexer_get_preprocessor_token (NULL, first_token);
27189       if (first_token->type != CPP_PRAGMA_EOL)
27190         error_at (first_token->location,
27191                   "junk at end of %<#pragma GCC pch_preprocess%>");
27192     }
27193   else
27194     error_at (first_token->location, "expected string literal");
27195
27196   /* Skip to the end of the pragma.  */
27197   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27198     cp_lexer_get_preprocessor_token (NULL, first_token);
27199
27200   /* Now actually load the PCH file.  */
27201   if (name)
27202     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27203
27204   /* Read one more token to return to our caller.  We have to do this
27205      after reading the PCH file in, since its pointers have to be
27206      live.  */
27207   cp_lexer_get_preprocessor_token (NULL, first_token);
27208 }
27209
27210 /* Normal parsing of a pragma token.  Here we can (and must) use the
27211    regular lexer.  */
27212
27213 static bool
27214 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27215 {
27216   cp_token *pragma_tok;
27217   unsigned int id;
27218
27219   pragma_tok = cp_lexer_consume_token (parser->lexer);
27220   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27221   parser->lexer->in_pragma = true;
27222
27223   id = pragma_tok->pragma_kind;
27224   switch (id)
27225     {
27226     case PRAGMA_GCC_PCH_PREPROCESS:
27227       error_at (pragma_tok->location,
27228                 "%<#pragma GCC pch_preprocess%> must be first");
27229       break;
27230
27231     case PRAGMA_OMP_BARRIER:
27232       switch (context)
27233         {
27234         case pragma_compound:
27235           cp_parser_omp_barrier (parser, pragma_tok);
27236           return false;
27237         case pragma_stmt:
27238           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27239                     "used in compound statements");
27240           break;
27241         default:
27242           goto bad_stmt;
27243         }
27244       break;
27245
27246     case PRAGMA_OMP_FLUSH:
27247       switch (context)
27248         {
27249         case pragma_compound:
27250           cp_parser_omp_flush (parser, pragma_tok);
27251           return false;
27252         case pragma_stmt:
27253           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27254                     "used in compound statements");
27255           break;
27256         default:
27257           goto bad_stmt;
27258         }
27259       break;
27260
27261     case PRAGMA_OMP_TASKWAIT:
27262       switch (context)
27263         {
27264         case pragma_compound:
27265           cp_parser_omp_taskwait (parser, pragma_tok);
27266           return false;
27267         case pragma_stmt:
27268           error_at (pragma_tok->location,
27269                     "%<#pragma omp taskwait%> may only be "
27270                     "used in compound statements");
27271           break;
27272         default:
27273           goto bad_stmt;
27274         }
27275       break;
27276
27277     case PRAGMA_OMP_TASKYIELD:
27278       switch (context)
27279         {
27280         case pragma_compound:
27281           cp_parser_omp_taskyield (parser, pragma_tok);
27282           return false;
27283         case pragma_stmt:
27284           error_at (pragma_tok->location,
27285                     "%<#pragma omp taskyield%> may only be "
27286                     "used in compound statements");
27287           break;
27288         default:
27289           goto bad_stmt;
27290         }
27291       break;
27292
27293     case PRAGMA_OMP_THREADPRIVATE:
27294       cp_parser_omp_threadprivate (parser, pragma_tok);
27295       return false;
27296
27297     case PRAGMA_OMP_ATOMIC:
27298     case PRAGMA_OMP_CRITICAL:
27299     case PRAGMA_OMP_FOR:
27300     case PRAGMA_OMP_MASTER:
27301     case PRAGMA_OMP_ORDERED:
27302     case PRAGMA_OMP_PARALLEL:
27303     case PRAGMA_OMP_SECTIONS:
27304     case PRAGMA_OMP_SINGLE:
27305     case PRAGMA_OMP_TASK:
27306       if (context == pragma_external)
27307         goto bad_stmt;
27308       cp_parser_omp_construct (parser, pragma_tok);
27309       return true;
27310
27311     case PRAGMA_OMP_SECTION:
27312       error_at (pragma_tok->location, 
27313                 "%<#pragma omp section%> may only be used in "
27314                 "%<#pragma omp sections%> construct");
27315       break;
27316
27317     default:
27318       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27319       c_invoke_pragma_handler (id);
27320       break;
27321
27322     bad_stmt:
27323       cp_parser_error (parser, "expected declaration specifiers");
27324       break;
27325     }
27326
27327   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27328   return false;
27329 }
27330
27331 /* The interface the pragma parsers have to the lexer.  */
27332
27333 enum cpp_ttype
27334 pragma_lex (tree *value)
27335 {
27336   cp_token *tok;
27337   enum cpp_ttype ret;
27338
27339   tok = cp_lexer_peek_token (the_parser->lexer);
27340
27341   ret = tok->type;
27342   *value = tok->u.value;
27343
27344   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27345     ret = CPP_EOF;
27346   else if (ret == CPP_STRING)
27347     *value = cp_parser_string_literal (the_parser, false, false);
27348   else
27349     {
27350       cp_lexer_consume_token (the_parser->lexer);
27351       if (ret == CPP_KEYWORD)
27352         ret = CPP_NAME;
27353     }
27354
27355   return ret;
27356 }
27357
27358 \f
27359 /* External interface.  */
27360
27361 /* Parse one entire translation unit.  */
27362
27363 void
27364 c_parse_file (void)
27365 {
27366   static bool already_called = false;
27367
27368   if (already_called)
27369     {
27370       sorry ("inter-module optimizations not implemented for C++");
27371       return;
27372     }
27373   already_called = true;
27374
27375   the_parser = cp_parser_new ();
27376   push_deferring_access_checks (flag_access_control
27377                                 ? dk_no_deferred : dk_no_check);
27378   cp_parser_translation_unit (the_parser);
27379   the_parser = NULL;
27380 }
27381
27382 #include "gt-cp-parser.h"