OSDN Git Service

PR c++/6936
[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 /* Parse a user-defined char constant.  Returns a call to a user-defined
3551    literal operator taking the character as an argument.  */
3552
3553 static tree
3554 cp_parser_userdef_char_literal (cp_parser *parser)
3555 {
3556   cp_token *token = NULL;
3557   tree literal, suffix_id, value;
3558   tree name, decl;
3559   tree result;
3560   VEC(tree,gc) *vec;
3561
3562   token = cp_lexer_consume_token (parser->lexer);
3563   literal = token->u.value;
3564   suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3565   value = USERDEF_LITERAL_VALUE (literal);
3566   name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3567
3568   /* Build up a call to the user-defined operator  */
3569   /* Lookup the name we got back from the id-expression.  */
3570   vec = make_tree_vector ();
3571   VEC_safe_push (tree, gc, vec, value);
3572   decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3573   if (!decl || decl == error_mark_node)
3574     {
3575       error ("unable to find user-defined character literal operator %qD",
3576              name);
3577       release_tree_vector (vec);
3578       return error_mark_node;
3579     }
3580   result = finish_call_expr (decl, &vec, false, true, tf_warning_or_error);
3581   release_tree_vector (vec);
3582
3583   return result;
3584 }
3585
3586 /* A subroutine of cp_parser_userdef_numeric_literal to
3587    create a char... template parameter pack from a string node.  */
3588
3589 static tree
3590 make_char_string_pack (tree value)
3591 {
3592   tree charvec;
3593   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3594   const char *str = TREE_STRING_POINTER (value);
3595   int i, len = TREE_STRING_LENGTH (value) - 1;
3596   tree argvec = make_tree_vec (1);
3597
3598   /* Fill in CHARVEC with all of the parameters.  */
3599   charvec = make_tree_vec (len);
3600   for (i = 0; i < len; ++i)
3601     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3602
3603   /* Build the argument packs.  */
3604   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3605   TREE_TYPE (argpack) = char_type_node;
3606
3607   TREE_VEC_ELT (argvec, 0) = argpack;
3608
3609   return argvec;
3610 }
3611
3612 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3613    literal operator.  */
3614
3615 static tree
3616 cp_parser_userdef_numeric_literal (cp_parser *parser)
3617 {
3618   cp_token *token = NULL;
3619   tree literal, suffix_id, value, num_string;
3620   tree name, decl;
3621   tree result = error_mark_node;
3622   VEC(tree,gc) *args;
3623
3624   token = cp_lexer_consume_token (parser->lexer);
3625   literal = token->u.value;
3626   suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3627   value = USERDEF_LITERAL_VALUE (literal);
3628   num_string = USERDEF_LITERAL_NUM_STRING (literal);
3629   name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3630
3631   /* Build up a call to the user-defined operator  */
3632   /* Lookup the name we got back from the id-expression.  */
3633   /* Try to find the literal operator by finishing the call expression
3634      with the numeric argument.  */
3635   args = make_tree_vector ();
3636   VEC_safe_push (tree, gc, args, value);
3637   decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3638   if (decl && decl != error_mark_node)
3639     {
3640       result = finish_call_expr (decl, &args, false, true, tf_none);
3641       if (result != error_mark_node)
3642         {
3643           release_tree_vector (args);
3644           return result;
3645         }
3646     }
3647   release_tree_vector (args);
3648
3649   /* If the numeric argument didn't work, look for a raw literal
3650      operator taking a const char* argument consisting of the number
3651      in string format.  */
3652   args = make_tree_vector ();
3653   VEC_safe_push (tree, gc, args, num_string);
3654   decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3655   if (decl && decl != error_mark_node)
3656     {
3657       result = finish_call_expr (decl, &args, false, true, tf_none);
3658       if (result != error_mark_node)
3659         {
3660           release_tree_vector (args);
3661           return result;
3662         }
3663     }
3664   release_tree_vector (args);
3665
3666   /* If the raw literal didn't work, look for a non-type template
3667      function with parameter pack char....  Call the function with
3668      template parameter characters representing the number.  */
3669   args = make_tree_vector ();
3670   decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3671   if (decl && decl != error_mark_node)
3672     {
3673       tree tmpl_args = make_char_string_pack (num_string);
3674       decl = lookup_template_function (decl, tmpl_args);
3675       result = finish_call_expr (decl, &args, false, true, tf_none);
3676       if (result != error_mark_node)
3677         {
3678           release_tree_vector (args);
3679           return result;
3680         }
3681     }
3682   release_tree_vector (args);
3683
3684   if (result == error_mark_node)
3685     error ("unable to find user-defined numeric literal operator %qD", name);
3686
3687   return result;
3688 }
3689
3690 /* Parse a user-defined string constant.  Returns a call to a user-defined
3691    literal operator taking a character pointer and the length of the string
3692    as arguments.  */
3693
3694 static tree
3695 cp_parser_userdef_string_literal (cp_token *token)
3696 {
3697   tree literal, suffix_id, value;
3698   tree name, decl;
3699   tree result;
3700   VEC(tree,gc) *vec;
3701   int len;
3702
3703   literal = token->u.value;
3704   suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3705   name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3706   value = USERDEF_LITERAL_VALUE (literal);
3707   len = TREE_STRING_LENGTH (value)
3708         / TREE_INT_CST_LOW (TYPE_SIZE_UNIT (TREE_TYPE (TREE_TYPE (value)))) - 1;
3709   /* Build up a call to the user-defined operator  */
3710   /* Lookup the name we got back from the id-expression.  */
3711   vec = make_tree_vector ();
3712   VEC_safe_push (tree, gc, vec, value);
3713   VEC_safe_push (tree, gc, vec, build_int_cst (size_type_node, len));
3714   decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3715   if (!decl || decl == error_mark_node)
3716     {
3717       error ("unable to find user-defined string literal operator %qD", name);
3718       release_tree_vector (vec);
3719       return error_mark_node;
3720     }
3721   result = finish_call_expr (decl, &vec, false, true, tf_none);
3722   if (result == error_mark_node)
3723     error ("unable to find valid user-defined string literal operator %qD."
3724            "  Possible missing length argument in string literal operator.",
3725            name);
3726   release_tree_vector (vec);
3727
3728   return result;
3729 }
3730
3731
3732 /* Basic concepts [gram.basic]  */
3733
3734 /* Parse a translation-unit.
3735
3736    translation-unit:
3737      declaration-seq [opt]
3738
3739    Returns TRUE if all went well.  */
3740
3741 static bool
3742 cp_parser_translation_unit (cp_parser* parser)
3743 {
3744   /* The address of the first non-permanent object on the declarator
3745      obstack.  */
3746   static void *declarator_obstack_base;
3747
3748   bool success;
3749
3750   /* Create the declarator obstack, if necessary.  */
3751   if (!cp_error_declarator)
3752     {
3753       gcc_obstack_init (&declarator_obstack);
3754       /* Create the error declarator.  */
3755       cp_error_declarator = make_declarator (cdk_error);
3756       /* Create the empty parameter list.  */
3757       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3758       /* Remember where the base of the declarator obstack lies.  */
3759       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3760     }
3761
3762   cp_parser_declaration_seq_opt (parser);
3763
3764   /* If there are no tokens left then all went well.  */
3765   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3766     {
3767       /* Get rid of the token array; we don't need it any more.  */
3768       cp_lexer_destroy (parser->lexer);
3769       parser->lexer = NULL;
3770
3771       /* This file might have been a context that's implicitly extern
3772          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3773       if (parser->implicit_extern_c)
3774         {
3775           pop_lang_context ();
3776           parser->implicit_extern_c = false;
3777         }
3778
3779       /* Finish up.  */
3780       finish_translation_unit ();
3781
3782       success = true;
3783     }
3784   else
3785     {
3786       cp_parser_error (parser, "expected declaration");
3787       success = false;
3788     }
3789
3790   /* Make sure the declarator obstack was fully cleaned up.  */
3791   gcc_assert (obstack_next_free (&declarator_obstack)
3792               == declarator_obstack_base);
3793
3794   /* All went well.  */
3795   return success;
3796 }
3797
3798 /* Expressions [gram.expr] */
3799
3800 /* Parse a primary-expression.
3801
3802    primary-expression:
3803      literal
3804      this
3805      ( expression )
3806      id-expression
3807
3808    GNU Extensions:
3809
3810    primary-expression:
3811      ( compound-statement )
3812      __builtin_va_arg ( assignment-expression , type-id )
3813      __builtin_offsetof ( type-id , offsetof-expression )
3814
3815    C++ Extensions:
3816      __has_nothrow_assign ( type-id )   
3817      __has_nothrow_constructor ( type-id )
3818      __has_nothrow_copy ( type-id )
3819      __has_trivial_assign ( type-id )   
3820      __has_trivial_constructor ( type-id )
3821      __has_trivial_copy ( type-id )
3822      __has_trivial_destructor ( type-id )
3823      __has_virtual_destructor ( type-id )     
3824      __is_abstract ( type-id )
3825      __is_base_of ( type-id , type-id )
3826      __is_class ( type-id )
3827      __is_convertible_to ( type-id , type-id )     
3828      __is_empty ( type-id )
3829      __is_enum ( type-id )
3830      __is_literal_type ( type-id )
3831      __is_pod ( type-id )
3832      __is_polymorphic ( type-id )
3833      __is_std_layout ( type-id )
3834      __is_trivial ( type-id )
3835      __is_union ( type-id )
3836
3837    Objective-C++ Extension:
3838
3839    primary-expression:
3840      objc-expression
3841
3842    literal:
3843      __null
3844
3845    ADDRESS_P is true iff this expression was immediately preceded by
3846    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3847    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3848    true iff this expression is a template argument.
3849
3850    Returns a representation of the expression.  Upon return, *IDK
3851    indicates what kind of id-expression (if any) was present.  */
3852
3853 static tree
3854 cp_parser_primary_expression (cp_parser *parser,
3855                               bool address_p,
3856                               bool cast_p,
3857                               bool template_arg_p,
3858                               cp_id_kind *idk)
3859 {
3860   cp_token *token = NULL;
3861
3862   /* Assume the primary expression is not an id-expression.  */
3863   *idk = CP_ID_KIND_NONE;
3864
3865   /* Peek at the next token.  */
3866   token = cp_lexer_peek_token (parser->lexer);
3867   switch (token->type)
3868     {
3869       /* literal:
3870            integer-literal
3871            character-literal
3872            floating-literal
3873            string-literal
3874            boolean-literal
3875            pointer-literal
3876            user-defined-literal  */
3877     case CPP_CHAR:
3878     case CPP_CHAR16:
3879     case CPP_CHAR32:
3880     case CPP_WCHAR:
3881     case CPP_NUMBER:
3882       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3883         return cp_parser_userdef_numeric_literal (parser);
3884       token = cp_lexer_consume_token (parser->lexer);
3885       if (TREE_CODE (token->u.value) == FIXED_CST)
3886         {
3887           error_at (token->location,
3888                     "fixed-point types not supported in C++");
3889           return error_mark_node;
3890         }
3891       /* Floating-point literals are only allowed in an integral
3892          constant expression if they are cast to an integral or
3893          enumeration type.  */
3894       if (TREE_CODE (token->u.value) == REAL_CST
3895           && parser->integral_constant_expression_p
3896           && pedantic)
3897         {
3898           /* CAST_P will be set even in invalid code like "int(2.7 +
3899              ...)".   Therefore, we have to check that the next token
3900              is sure to end the cast.  */
3901           if (cast_p)
3902             {
3903               cp_token *next_token;
3904
3905               next_token = cp_lexer_peek_token (parser->lexer);
3906               if (/* The comma at the end of an
3907                      enumerator-definition.  */
3908                   next_token->type != CPP_COMMA
3909                   /* The curly brace at the end of an enum-specifier.  */
3910                   && next_token->type != CPP_CLOSE_BRACE
3911                   /* The end of a statement.  */
3912                   && next_token->type != CPP_SEMICOLON
3913                   /* The end of the cast-expression.  */
3914                   && next_token->type != CPP_CLOSE_PAREN
3915                   /* The end of an array bound.  */
3916                   && next_token->type != CPP_CLOSE_SQUARE
3917                   /* The closing ">" in a template-argument-list.  */
3918                   && (next_token->type != CPP_GREATER
3919                       || parser->greater_than_is_operator_p)
3920                   /* C++0x only: A ">>" treated like two ">" tokens,
3921                      in a template-argument-list.  */
3922                   && (next_token->type != CPP_RSHIFT
3923                       || (cxx_dialect == cxx98)
3924                       || parser->greater_than_is_operator_p))
3925                 cast_p = false;
3926             }
3927
3928           /* If we are within a cast, then the constraint that the
3929              cast is to an integral or enumeration type will be
3930              checked at that point.  If we are not within a cast, then
3931              this code is invalid.  */
3932           if (!cast_p)
3933             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3934         }
3935       return token->u.value;
3936
3937     case CPP_CHAR_USERDEF:
3938     case CPP_CHAR16_USERDEF:
3939     case CPP_CHAR32_USERDEF:
3940     case CPP_WCHAR_USERDEF:
3941       return cp_parser_userdef_char_literal (parser);
3942
3943     case CPP_STRING:
3944     case CPP_STRING16:
3945     case CPP_STRING32:
3946     case CPP_WSTRING:
3947     case CPP_UTF8STRING:
3948     case CPP_STRING_USERDEF:
3949     case CPP_STRING16_USERDEF:
3950     case CPP_STRING32_USERDEF:
3951     case CPP_WSTRING_USERDEF:
3952     case CPP_UTF8STRING_USERDEF:
3953       /* ??? Should wide strings be allowed when parser->translate_strings_p
3954          is false (i.e. in attributes)?  If not, we can kill the third
3955          argument to cp_parser_string_literal.  */
3956       return cp_parser_string_literal (parser,
3957                                        parser->translate_strings_p,
3958                                        true);
3959
3960     case CPP_OPEN_PAREN:
3961       {
3962         tree expr;
3963         bool saved_greater_than_is_operator_p;
3964
3965         /* Consume the `('.  */
3966         cp_lexer_consume_token (parser->lexer);
3967         /* Within a parenthesized expression, a `>' token is always
3968            the greater-than operator.  */
3969         saved_greater_than_is_operator_p
3970           = parser->greater_than_is_operator_p;
3971         parser->greater_than_is_operator_p = true;
3972         /* If we see `( { ' then we are looking at the beginning of
3973            a GNU statement-expression.  */
3974         if (cp_parser_allow_gnu_extensions_p (parser)
3975             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3976           {
3977             /* Statement-expressions are not allowed by the standard.  */
3978             pedwarn (token->location, OPT_pedantic, 
3979                      "ISO C++ forbids braced-groups within expressions");
3980
3981             /* And they're not allowed outside of a function-body; you
3982                cannot, for example, write:
3983
3984                  int i = ({ int j = 3; j + 1; });
3985
3986                at class or namespace scope.  */
3987             if (!parser->in_function_body
3988                 || parser->in_template_argument_list_p)
3989               {
3990                 error_at (token->location,
3991                           "statement-expressions are not allowed outside "
3992                           "functions nor in template-argument lists");
3993                 cp_parser_skip_to_end_of_block_or_statement (parser);
3994                 expr = error_mark_node;
3995               }
3996             else
3997               {
3998                 /* Start the statement-expression.  */
3999                 expr = begin_stmt_expr ();
4000                 /* Parse the compound-statement.  */
4001                 cp_parser_compound_statement (parser, expr, false, false);
4002                 /* Finish up.  */
4003                 expr = finish_stmt_expr (expr, false);
4004               }
4005           }
4006         else
4007           {
4008             /* Parse the parenthesized expression.  */
4009             expr = cp_parser_expression (parser, cast_p, idk);
4010             /* Let the front end know that this expression was
4011                enclosed in parentheses. This matters in case, for
4012                example, the expression is of the form `A::B', since
4013                `&A::B' might be a pointer-to-member, but `&(A::B)' is
4014                not.  */
4015             finish_parenthesized_expr (expr);
4016             /* DR 705: Wrapping an unqualified name in parentheses
4017                suppresses arg-dependent lookup.  We want to pass back
4018                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
4019                (c++/37862), but none of the others.  */
4020             if (*idk != CP_ID_KIND_QUALIFIED)
4021               *idk = CP_ID_KIND_NONE;
4022           }
4023         /* The `>' token might be the end of a template-id or
4024            template-parameter-list now.  */
4025         parser->greater_than_is_operator_p
4026           = saved_greater_than_is_operator_p;
4027         /* Consume the `)'.  */
4028         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4029           cp_parser_skip_to_end_of_statement (parser);
4030
4031         return expr;
4032       }
4033
4034     case CPP_OPEN_SQUARE:
4035       if (c_dialect_objc ())
4036         /* We have an Objective-C++ message. */
4037         return cp_parser_objc_expression (parser);
4038       {
4039         tree lam = cp_parser_lambda_expression (parser);
4040         /* Don't warn about a failed tentative parse.  */
4041         if (cp_parser_error_occurred (parser))
4042           return error_mark_node;
4043         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4044         return lam;
4045       }
4046
4047     case CPP_OBJC_STRING:
4048       if (c_dialect_objc ())
4049         /* We have an Objective-C++ string literal. */
4050         return cp_parser_objc_expression (parser);
4051       cp_parser_error (parser, "expected primary-expression");
4052       return error_mark_node;
4053
4054     case CPP_KEYWORD:
4055       switch (token->keyword)
4056         {
4057           /* These two are the boolean literals.  */
4058         case RID_TRUE:
4059           cp_lexer_consume_token (parser->lexer);
4060           return boolean_true_node;
4061         case RID_FALSE:
4062           cp_lexer_consume_token (parser->lexer);
4063           return boolean_false_node;
4064
4065           /* The `__null' literal.  */
4066         case RID_NULL:
4067           cp_lexer_consume_token (parser->lexer);
4068           return null_node;
4069
4070           /* The `nullptr' literal.  */
4071         case RID_NULLPTR:
4072           cp_lexer_consume_token (parser->lexer);
4073           return nullptr_node;
4074
4075           /* Recognize the `this' keyword.  */
4076         case RID_THIS:
4077           cp_lexer_consume_token (parser->lexer);
4078           if (parser->local_variables_forbidden_p)
4079             {
4080               error_at (token->location,
4081                         "%<this%> may not be used in this context");
4082               return error_mark_node;
4083             }
4084           /* Pointers cannot appear in constant-expressions.  */
4085           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4086             return error_mark_node;
4087           return finish_this_expr ();
4088
4089           /* The `operator' keyword can be the beginning of an
4090              id-expression.  */
4091         case RID_OPERATOR:
4092           goto id_expression;
4093
4094         case RID_FUNCTION_NAME:
4095         case RID_PRETTY_FUNCTION_NAME:
4096         case RID_C99_FUNCTION_NAME:
4097           {
4098             non_integral_constant name;
4099
4100             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4101                __func__ are the names of variables -- but they are
4102                treated specially.  Therefore, they are handled here,
4103                rather than relying on the generic id-expression logic
4104                below.  Grammatically, these names are id-expressions.
4105
4106                Consume the token.  */
4107             token = cp_lexer_consume_token (parser->lexer);
4108
4109             switch (token->keyword)
4110               {
4111               case RID_FUNCTION_NAME:
4112                 name = NIC_FUNC_NAME;
4113                 break;
4114               case RID_PRETTY_FUNCTION_NAME:
4115                 name = NIC_PRETTY_FUNC;
4116                 break;
4117               case RID_C99_FUNCTION_NAME:
4118                 name = NIC_C99_FUNC;
4119                 break;
4120               default:
4121                 gcc_unreachable ();
4122               }
4123
4124             if (cp_parser_non_integral_constant_expression (parser, name))
4125               return error_mark_node;
4126
4127             /* Look up the name.  */
4128             return finish_fname (token->u.value);
4129           }
4130
4131         case RID_VA_ARG:
4132           {
4133             tree expression;
4134             tree type;
4135
4136             /* The `__builtin_va_arg' construct is used to handle
4137                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4138             cp_lexer_consume_token (parser->lexer);
4139             /* Look for the opening `('.  */
4140             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4141             /* Now, parse the assignment-expression.  */
4142             expression = cp_parser_assignment_expression (parser,
4143                                                           /*cast_p=*/false, NULL);
4144             /* Look for the `,'.  */
4145             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4146             /* Parse the type-id.  */
4147             type = cp_parser_type_id (parser);
4148             /* Look for the closing `)'.  */
4149             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4150             /* Using `va_arg' in a constant-expression is not
4151                allowed.  */
4152             if (cp_parser_non_integral_constant_expression (parser,
4153                                                             NIC_VA_ARG))
4154               return error_mark_node;
4155             return build_x_va_arg (expression, type);
4156           }
4157
4158         case RID_OFFSETOF:
4159           return cp_parser_builtin_offsetof (parser);
4160
4161         case RID_HAS_NOTHROW_ASSIGN:
4162         case RID_HAS_NOTHROW_CONSTRUCTOR:
4163         case RID_HAS_NOTHROW_COPY:        
4164         case RID_HAS_TRIVIAL_ASSIGN:
4165         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4166         case RID_HAS_TRIVIAL_COPY:        
4167         case RID_HAS_TRIVIAL_DESTRUCTOR:
4168         case RID_HAS_VIRTUAL_DESTRUCTOR:
4169         case RID_IS_ABSTRACT:
4170         case RID_IS_BASE_OF:
4171         case RID_IS_CLASS:
4172         case RID_IS_CONVERTIBLE_TO:
4173         case RID_IS_EMPTY:
4174         case RID_IS_ENUM:
4175         case RID_IS_LITERAL_TYPE:
4176         case RID_IS_POD:
4177         case RID_IS_POLYMORPHIC:
4178         case RID_IS_STD_LAYOUT:
4179         case RID_IS_TRIVIAL:
4180         case RID_IS_UNION:
4181           return cp_parser_trait_expr (parser, token->keyword);
4182
4183         /* Objective-C++ expressions.  */
4184         case RID_AT_ENCODE:
4185         case RID_AT_PROTOCOL:
4186         case RID_AT_SELECTOR:
4187           return cp_parser_objc_expression (parser);
4188
4189         case RID_TEMPLATE:
4190           if (parser->in_function_body
4191               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4192                   == CPP_LESS))
4193             {
4194               error_at (token->location,
4195                         "a template declaration cannot appear at block scope");
4196               cp_parser_skip_to_end_of_block_or_statement (parser);
4197               return error_mark_node;
4198             }
4199         default:
4200           cp_parser_error (parser, "expected primary-expression");
4201           return error_mark_node;
4202         }
4203
4204       /* An id-expression can start with either an identifier, a
4205          `::' as the beginning of a qualified-id, or the "operator"
4206          keyword.  */
4207     case CPP_NAME:
4208     case CPP_SCOPE:
4209     case CPP_TEMPLATE_ID:
4210     case CPP_NESTED_NAME_SPECIFIER:
4211       {
4212         tree id_expression;
4213         tree decl;
4214         const char *error_msg;
4215         bool template_p;
4216         bool done;
4217         cp_token *id_expr_token;
4218
4219       id_expression:
4220         /* Parse the id-expression.  */
4221         id_expression
4222           = cp_parser_id_expression (parser,
4223                                      /*template_keyword_p=*/false,
4224                                      /*check_dependency_p=*/true,
4225                                      &template_p,
4226                                      /*declarator_p=*/false,
4227                                      /*optional_p=*/false);
4228         if (id_expression == error_mark_node)
4229           return error_mark_node;
4230         id_expr_token = token;
4231         token = cp_lexer_peek_token (parser->lexer);
4232         done = (token->type != CPP_OPEN_SQUARE
4233                 && token->type != CPP_OPEN_PAREN
4234                 && token->type != CPP_DOT
4235                 && token->type != CPP_DEREF
4236                 && token->type != CPP_PLUS_PLUS
4237                 && token->type != CPP_MINUS_MINUS);
4238         /* If we have a template-id, then no further lookup is
4239            required.  If the template-id was for a template-class, we
4240            will sometimes have a TYPE_DECL at this point.  */
4241         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4242                  || TREE_CODE (id_expression) == TYPE_DECL)
4243           decl = id_expression;
4244         /* Look up the name.  */
4245         else
4246           {
4247             tree ambiguous_decls;
4248
4249             /* If we already know that this lookup is ambiguous, then
4250                we've already issued an error message; there's no reason
4251                to check again.  */
4252             if (id_expr_token->type == CPP_NAME
4253                 && id_expr_token->ambiguous_p)
4254               {
4255                 cp_parser_simulate_error (parser);
4256                 return error_mark_node;
4257               }
4258
4259             decl = cp_parser_lookup_name (parser, id_expression,
4260                                           none_type,
4261                                           template_p,
4262                                           /*is_namespace=*/false,
4263                                           /*check_dependency=*/true,
4264                                           &ambiguous_decls,
4265                                           id_expr_token->location);
4266             /* If the lookup was ambiguous, an error will already have
4267                been issued.  */
4268             if (ambiguous_decls)
4269               return error_mark_node;
4270
4271             /* In Objective-C++, we may have an Objective-C 2.0
4272                dot-syntax for classes here.  */
4273             if (c_dialect_objc ()
4274                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4275                 && TREE_CODE (decl) == TYPE_DECL
4276                 && objc_is_class_name (decl))
4277               {
4278                 tree component;
4279                 cp_lexer_consume_token (parser->lexer);
4280                 component = cp_parser_identifier (parser);
4281                 if (component == error_mark_node)
4282                   return error_mark_node;
4283
4284                 return objc_build_class_component_ref (id_expression, component);
4285               }
4286
4287             /* In Objective-C++, an instance variable (ivar) may be preferred
4288                to whatever cp_parser_lookup_name() found.  */
4289             decl = objc_lookup_ivar (decl, id_expression);
4290
4291             /* If name lookup gives us a SCOPE_REF, then the
4292                qualifying scope was dependent.  */
4293             if (TREE_CODE (decl) == SCOPE_REF)
4294               {
4295                 /* At this point, we do not know if DECL is a valid
4296                    integral constant expression.  We assume that it is
4297                    in fact such an expression, so that code like:
4298
4299                       template <int N> struct A {
4300                         int a[B<N>::i];
4301                       };
4302                      
4303                    is accepted.  At template-instantiation time, we
4304                    will check that B<N>::i is actually a constant.  */
4305                 return decl;
4306               }
4307             /* Check to see if DECL is a local variable in a context
4308                where that is forbidden.  */
4309             if (parser->local_variables_forbidden_p
4310                 && local_variable_p (decl))
4311               {
4312                 /* It might be that we only found DECL because we are
4313                    trying to be generous with pre-ISO scoping rules.
4314                    For example, consider:
4315
4316                      int i;
4317                      void g() {
4318                        for (int i = 0; i < 10; ++i) {}
4319                        extern void f(int j = i);
4320                      }
4321
4322                    Here, name look up will originally find the out
4323                    of scope `i'.  We need to issue a warning message,
4324                    but then use the global `i'.  */
4325                 decl = check_for_out_of_scope_variable (decl);
4326                 if (local_variable_p (decl))
4327                   {
4328                     error_at (id_expr_token->location,
4329                               "local variable %qD may not appear in this context",
4330                               decl);
4331                     return error_mark_node;
4332                   }
4333               }
4334           }
4335
4336         decl = (finish_id_expression
4337                 (id_expression, decl, parser->scope,
4338                  idk,
4339                  parser->integral_constant_expression_p,
4340                  parser->allow_non_integral_constant_expression_p,
4341                  &parser->non_integral_constant_expression_p,
4342                  template_p, done, address_p,
4343                  template_arg_p,
4344                  &error_msg,
4345                  id_expr_token->location));
4346         if (error_msg)
4347           cp_parser_error (parser, error_msg);
4348         return decl;
4349       }
4350
4351       /* Anything else is an error.  */
4352     default:
4353       cp_parser_error (parser, "expected primary-expression");
4354       return error_mark_node;
4355     }
4356 }
4357
4358 /* Parse an id-expression.
4359
4360    id-expression:
4361      unqualified-id
4362      qualified-id
4363
4364    qualified-id:
4365      :: [opt] nested-name-specifier template [opt] unqualified-id
4366      :: identifier
4367      :: operator-function-id
4368      :: template-id
4369
4370    Return a representation of the unqualified portion of the
4371    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4372    a `::' or nested-name-specifier.
4373
4374    Often, if the id-expression was a qualified-id, the caller will
4375    want to make a SCOPE_REF to represent the qualified-id.  This
4376    function does not do this in order to avoid wastefully creating
4377    SCOPE_REFs when they are not required.
4378
4379    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4380    `template' keyword.
4381
4382    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4383    uninstantiated templates.
4384
4385    If *TEMPLATE_P is non-NULL, it is set to true iff the
4386    `template' keyword is used to explicitly indicate that the entity
4387    named is a template.
4388
4389    If DECLARATOR_P is true, the id-expression is appearing as part of
4390    a declarator, rather than as part of an expression.  */
4391
4392 static tree
4393 cp_parser_id_expression (cp_parser *parser,
4394                          bool template_keyword_p,
4395                          bool check_dependency_p,
4396                          bool *template_p,
4397                          bool declarator_p,
4398                          bool optional_p)
4399 {
4400   bool global_scope_p;
4401   bool nested_name_specifier_p;
4402
4403   /* Assume the `template' keyword was not used.  */
4404   if (template_p)
4405     *template_p = template_keyword_p;
4406
4407   /* Look for the optional `::' operator.  */
4408   global_scope_p
4409     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4410        != NULL_TREE);
4411   /* Look for the optional nested-name-specifier.  */
4412   nested_name_specifier_p
4413     = (cp_parser_nested_name_specifier_opt (parser,
4414                                             /*typename_keyword_p=*/false,
4415                                             check_dependency_p,
4416                                             /*type_p=*/false,
4417                                             declarator_p)
4418        != NULL_TREE);
4419   /* If there is a nested-name-specifier, then we are looking at
4420      the first qualified-id production.  */
4421   if (nested_name_specifier_p)
4422     {
4423       tree saved_scope;
4424       tree saved_object_scope;
4425       tree saved_qualifying_scope;
4426       tree unqualified_id;
4427       bool is_template;
4428
4429       /* See if the next token is the `template' keyword.  */
4430       if (!template_p)
4431         template_p = &is_template;
4432       *template_p = cp_parser_optional_template_keyword (parser);
4433       /* Name lookup we do during the processing of the
4434          unqualified-id might obliterate SCOPE.  */
4435       saved_scope = parser->scope;
4436       saved_object_scope = parser->object_scope;
4437       saved_qualifying_scope = parser->qualifying_scope;
4438       /* Process the final unqualified-id.  */
4439       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4440                                                  check_dependency_p,
4441                                                  declarator_p,
4442                                                  /*optional_p=*/false);
4443       /* Restore the SAVED_SCOPE for our caller.  */
4444       parser->scope = saved_scope;
4445       parser->object_scope = saved_object_scope;
4446       parser->qualifying_scope = saved_qualifying_scope;
4447
4448       return unqualified_id;
4449     }
4450   /* Otherwise, if we are in global scope, then we are looking at one
4451      of the other qualified-id productions.  */
4452   else if (global_scope_p)
4453     {
4454       cp_token *token;
4455       tree id;
4456
4457       /* Peek at the next token.  */
4458       token = cp_lexer_peek_token (parser->lexer);
4459
4460       /* If it's an identifier, and the next token is not a "<", then
4461          we can avoid the template-id case.  This is an optimization
4462          for this common case.  */
4463       if (token->type == CPP_NAME
4464           && !cp_parser_nth_token_starts_template_argument_list_p
4465                (parser, 2))
4466         return cp_parser_identifier (parser);
4467
4468       cp_parser_parse_tentatively (parser);
4469       /* Try a template-id.  */
4470       id = cp_parser_template_id (parser,
4471                                   /*template_keyword_p=*/false,
4472                                   /*check_dependency_p=*/true,
4473                                   declarator_p);
4474       /* If that worked, we're done.  */
4475       if (cp_parser_parse_definitely (parser))
4476         return id;
4477
4478       /* Peek at the next token.  (Changes in the token buffer may
4479          have invalidated the pointer obtained above.)  */
4480       token = cp_lexer_peek_token (parser->lexer);
4481
4482       switch (token->type)
4483         {
4484         case CPP_NAME:
4485           return cp_parser_identifier (parser);
4486
4487         case CPP_KEYWORD:
4488           if (token->keyword == RID_OPERATOR)
4489             return cp_parser_operator_function_id (parser);
4490           /* Fall through.  */
4491
4492         default:
4493           cp_parser_error (parser, "expected id-expression");
4494           return error_mark_node;
4495         }
4496     }
4497   else
4498     return cp_parser_unqualified_id (parser, template_keyword_p,
4499                                      /*check_dependency_p=*/true,
4500                                      declarator_p,
4501                                      optional_p);
4502 }
4503
4504 /* Parse an unqualified-id.
4505
4506    unqualified-id:
4507      identifier
4508      operator-function-id
4509      conversion-function-id
4510      ~ class-name
4511      template-id
4512
4513    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4514    keyword, in a construct like `A::template ...'.
4515
4516    Returns a representation of unqualified-id.  For the `identifier'
4517    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4518    production a BIT_NOT_EXPR is returned; the operand of the
4519    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4520    other productions, see the documentation accompanying the
4521    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4522    names are looked up in uninstantiated templates.  If DECLARATOR_P
4523    is true, the unqualified-id is appearing as part of a declarator,
4524    rather than as part of an expression.  */
4525
4526 static tree
4527 cp_parser_unqualified_id (cp_parser* parser,
4528                           bool template_keyword_p,
4529                           bool check_dependency_p,
4530                           bool declarator_p,
4531                           bool optional_p)
4532 {
4533   cp_token *token;
4534
4535   /* Peek at the next token.  */
4536   token = cp_lexer_peek_token (parser->lexer);
4537
4538   switch (token->type)
4539     {
4540     case CPP_NAME:
4541       {
4542         tree id;
4543
4544         /* We don't know yet whether or not this will be a
4545            template-id.  */
4546         cp_parser_parse_tentatively (parser);
4547         /* Try a template-id.  */
4548         id = cp_parser_template_id (parser, template_keyword_p,
4549                                     check_dependency_p,
4550                                     declarator_p);
4551         /* If it worked, we're done.  */
4552         if (cp_parser_parse_definitely (parser))
4553           return id;
4554         /* Otherwise, it's an ordinary identifier.  */
4555         return cp_parser_identifier (parser);
4556       }
4557
4558     case CPP_TEMPLATE_ID:
4559       return cp_parser_template_id (parser, template_keyword_p,
4560                                     check_dependency_p,
4561                                     declarator_p);
4562
4563     case CPP_COMPL:
4564       {
4565         tree type_decl;
4566         tree qualifying_scope;
4567         tree object_scope;
4568         tree scope;
4569         bool done;
4570
4571         /* Consume the `~' token.  */
4572         cp_lexer_consume_token (parser->lexer);
4573         /* Parse the class-name.  The standard, as written, seems to
4574            say that:
4575
4576              template <typename T> struct S { ~S (); };
4577              template <typename T> S<T>::~S() {}
4578
4579            is invalid, since `~' must be followed by a class-name, but
4580            `S<T>' is dependent, and so not known to be a class.
4581            That's not right; we need to look in uninstantiated
4582            templates.  A further complication arises from:
4583
4584              template <typename T> void f(T t) {
4585                t.T::~T();
4586              }
4587
4588            Here, it is not possible to look up `T' in the scope of `T'
4589            itself.  We must look in both the current scope, and the
4590            scope of the containing complete expression.
4591
4592            Yet another issue is:
4593
4594              struct S {
4595                int S;
4596                ~S();
4597              };
4598
4599              S::~S() {}
4600
4601            The standard does not seem to say that the `S' in `~S'
4602            should refer to the type `S' and not the data member
4603            `S::S'.  */
4604
4605         /* DR 244 says that we look up the name after the "~" in the
4606            same scope as we looked up the qualifying name.  That idea
4607            isn't fully worked out; it's more complicated than that.  */
4608         scope = parser->scope;
4609         object_scope = parser->object_scope;
4610         qualifying_scope = parser->qualifying_scope;
4611
4612         /* Check for invalid scopes.  */
4613         if (scope == error_mark_node)
4614           {
4615             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4616               cp_lexer_consume_token (parser->lexer);
4617             return error_mark_node;
4618           }
4619         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4620           {
4621             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4622               error_at (token->location,
4623                         "scope %qT before %<~%> is not a class-name",
4624                         scope);
4625             cp_parser_simulate_error (parser);
4626             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4627               cp_lexer_consume_token (parser->lexer);
4628             return error_mark_node;
4629           }
4630         gcc_assert (!scope || TYPE_P (scope));
4631
4632         /* If the name is of the form "X::~X" it's OK even if X is a
4633            typedef.  */
4634         token = cp_lexer_peek_token (parser->lexer);
4635         if (scope
4636             && token->type == CPP_NAME
4637             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4638                 != CPP_LESS)
4639             && (token->u.value == TYPE_IDENTIFIER (scope)
4640                 || (CLASS_TYPE_P (scope)
4641                     && constructor_name_p (token->u.value, scope))))
4642           {
4643             cp_lexer_consume_token (parser->lexer);
4644             return build_nt (BIT_NOT_EXPR, scope);
4645           }
4646
4647         /* If there was an explicit qualification (S::~T), first look
4648            in the scope given by the qualification (i.e., S).
4649
4650            Note: in the calls to cp_parser_class_name below we pass
4651            typename_type so that lookup finds the injected-class-name
4652            rather than the constructor.  */
4653         done = false;
4654         type_decl = NULL_TREE;
4655         if (scope)
4656           {
4657             cp_parser_parse_tentatively (parser);
4658             type_decl = cp_parser_class_name (parser,
4659                                               /*typename_keyword_p=*/false,
4660                                               /*template_keyword_p=*/false,
4661                                               typename_type,
4662                                               /*check_dependency=*/false,
4663                                               /*class_head_p=*/false,
4664                                               declarator_p);
4665             if (cp_parser_parse_definitely (parser))
4666               done = true;
4667           }
4668         /* In "N::S::~S", look in "N" as well.  */
4669         if (!done && scope && qualifying_scope)
4670           {
4671             cp_parser_parse_tentatively (parser);
4672             parser->scope = qualifying_scope;
4673             parser->object_scope = NULL_TREE;
4674             parser->qualifying_scope = NULL_TREE;
4675             type_decl
4676               = cp_parser_class_name (parser,
4677                                       /*typename_keyword_p=*/false,
4678                                       /*template_keyword_p=*/false,
4679                                       typename_type,
4680                                       /*check_dependency=*/false,
4681                                       /*class_head_p=*/false,
4682                                       declarator_p);
4683             if (cp_parser_parse_definitely (parser))
4684               done = true;
4685           }
4686         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4687         else if (!done && object_scope)
4688           {
4689             cp_parser_parse_tentatively (parser);
4690             parser->scope = object_scope;
4691             parser->object_scope = NULL_TREE;
4692             parser->qualifying_scope = NULL_TREE;
4693             type_decl
4694               = cp_parser_class_name (parser,
4695                                       /*typename_keyword_p=*/false,
4696                                       /*template_keyword_p=*/false,
4697                                       typename_type,
4698                                       /*check_dependency=*/false,
4699                                       /*class_head_p=*/false,
4700                                       declarator_p);
4701             if (cp_parser_parse_definitely (parser))
4702               done = true;
4703           }
4704         /* Look in the surrounding context.  */
4705         if (!done)
4706           {
4707             parser->scope = NULL_TREE;
4708             parser->object_scope = NULL_TREE;
4709             parser->qualifying_scope = NULL_TREE;
4710             if (processing_template_decl)
4711               cp_parser_parse_tentatively (parser);
4712             type_decl
4713               = cp_parser_class_name (parser,
4714                                       /*typename_keyword_p=*/false,
4715                                       /*template_keyword_p=*/false,
4716                                       typename_type,
4717                                       /*check_dependency=*/false,
4718                                       /*class_head_p=*/false,
4719                                       declarator_p);
4720             if (processing_template_decl
4721                 && ! cp_parser_parse_definitely (parser))
4722               {
4723                 /* We couldn't find a type with this name, so just accept
4724                    it and check for a match at instantiation time.  */
4725                 type_decl = cp_parser_identifier (parser);
4726                 if (type_decl != error_mark_node)
4727                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4728                 return type_decl;
4729               }
4730           }
4731         /* If an error occurred, assume that the name of the
4732            destructor is the same as the name of the qualifying
4733            class.  That allows us to keep parsing after running
4734            into ill-formed destructor names.  */
4735         if (type_decl == error_mark_node && scope)
4736           return build_nt (BIT_NOT_EXPR, scope);
4737         else if (type_decl == error_mark_node)
4738           return error_mark_node;
4739
4740         /* Check that destructor name and scope match.  */
4741         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4742           {
4743             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4744               error_at (token->location,
4745                         "declaration of %<~%T%> as member of %qT",
4746                         type_decl, scope);
4747             cp_parser_simulate_error (parser);
4748             return error_mark_node;
4749           }
4750
4751         /* [class.dtor]
4752
4753            A typedef-name that names a class shall not be used as the
4754            identifier in the declarator for a destructor declaration.  */
4755         if (declarator_p
4756             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4757             && !DECL_SELF_REFERENCE_P (type_decl)
4758             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4759           error_at (token->location,
4760                     "typedef-name %qD used as destructor declarator",
4761                     type_decl);
4762
4763         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4764       }
4765
4766     case CPP_KEYWORD:
4767       if (token->keyword == RID_OPERATOR)
4768         {
4769           tree id;
4770
4771           /* This could be a template-id, so we try that first.  */
4772           cp_parser_parse_tentatively (parser);
4773           /* Try a template-id.  */
4774           id = cp_parser_template_id (parser, template_keyword_p,
4775                                       /*check_dependency_p=*/true,
4776                                       declarator_p);
4777           /* If that worked, we're done.  */
4778           if (cp_parser_parse_definitely (parser))
4779             return id;
4780           /* We still don't know whether we're looking at an
4781              operator-function-id or a conversion-function-id.  */
4782           cp_parser_parse_tentatively (parser);
4783           /* Try an operator-function-id.  */
4784           id = cp_parser_operator_function_id (parser);
4785           /* If that didn't work, try a conversion-function-id.  */
4786           if (!cp_parser_parse_definitely (parser))
4787             id = cp_parser_conversion_function_id (parser);
4788           else if (UDLIT_OPER_P (id))
4789             {
4790               /* 17.6.3.3.5  */
4791               const char *name = UDLIT_OP_SUFFIX (id);
4792               if (name[0] != '_' && !in_system_header)
4793                 warning (0, "literal operator suffixes not preceded by %<_%>"
4794                             " are reserved for future standardization");
4795             }
4796
4797           return id;
4798         }
4799       /* Fall through.  */
4800
4801     default:
4802       if (optional_p)
4803         return NULL_TREE;
4804       cp_parser_error (parser, "expected unqualified-id");
4805       return error_mark_node;
4806     }
4807 }
4808
4809 /* Parse an (optional) nested-name-specifier.
4810
4811    nested-name-specifier: [C++98]
4812      class-or-namespace-name :: nested-name-specifier [opt]
4813      class-or-namespace-name :: template nested-name-specifier [opt]
4814
4815    nested-name-specifier: [C++0x]
4816      type-name ::
4817      namespace-name ::
4818      nested-name-specifier identifier ::
4819      nested-name-specifier template [opt] simple-template-id ::
4820
4821    PARSER->SCOPE should be set appropriately before this function is
4822    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4823    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4824    in name lookups.
4825
4826    Sets PARSER->SCOPE to the class (TYPE) or namespace
4827    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4828    it unchanged if there is no nested-name-specifier.  Returns the new
4829    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4830
4831    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4832    part of a declaration and/or decl-specifier.  */
4833
4834 static tree
4835 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4836                                      bool typename_keyword_p,
4837                                      bool check_dependency_p,
4838                                      bool type_p,
4839                                      bool is_declaration)
4840 {
4841   bool success = false;
4842   cp_token_position start = 0;
4843   cp_token *token;
4844
4845   /* Remember where the nested-name-specifier starts.  */
4846   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4847     {
4848       start = cp_lexer_token_position (parser->lexer, false);
4849       push_deferring_access_checks (dk_deferred);
4850     }
4851
4852   while (true)
4853     {
4854       tree new_scope;
4855       tree old_scope;
4856       tree saved_qualifying_scope;
4857       bool template_keyword_p;
4858
4859       /* Spot cases that cannot be the beginning of a
4860          nested-name-specifier.  */
4861       token = cp_lexer_peek_token (parser->lexer);
4862
4863       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4864          the already parsed nested-name-specifier.  */
4865       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4866         {
4867           /* Grab the nested-name-specifier and continue the loop.  */
4868           cp_parser_pre_parsed_nested_name_specifier (parser);
4869           /* If we originally encountered this nested-name-specifier
4870              with IS_DECLARATION set to false, we will not have
4871              resolved TYPENAME_TYPEs, so we must do so here.  */
4872           if (is_declaration
4873               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4874             {
4875               new_scope = resolve_typename_type (parser->scope,
4876                                                  /*only_current_p=*/false);
4877               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4878                 parser->scope = new_scope;
4879             }
4880           success = true;
4881           continue;
4882         }
4883
4884       /* Spot cases that cannot be the beginning of a
4885          nested-name-specifier.  On the second and subsequent times
4886          through the loop, we look for the `template' keyword.  */
4887       if (success && token->keyword == RID_TEMPLATE)
4888         ;
4889       /* A template-id can start a nested-name-specifier.  */
4890       else if (token->type == CPP_TEMPLATE_ID)
4891         ;
4892       /* DR 743: decltype can be used in a nested-name-specifier.  */
4893       else if (token_is_decltype (token))
4894         ;
4895       else
4896         {
4897           /* If the next token is not an identifier, then it is
4898              definitely not a type-name or namespace-name.  */
4899           if (token->type != CPP_NAME)
4900             break;
4901           /* If the following token is neither a `<' (to begin a
4902              template-id), nor a `::', then we are not looking at a
4903              nested-name-specifier.  */
4904           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4905
4906           if (token->type == CPP_COLON
4907               && parser->colon_corrects_to_scope_p
4908               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4909             {
4910               error_at (token->location,
4911                         "found %<:%> in nested-name-specifier, expected %<::%>");
4912               token->type = CPP_SCOPE;
4913             }
4914
4915           if (token->type != CPP_SCOPE
4916               && !cp_parser_nth_token_starts_template_argument_list_p
4917                   (parser, 2))
4918             break;
4919         }
4920
4921       /* The nested-name-specifier is optional, so we parse
4922          tentatively.  */
4923       cp_parser_parse_tentatively (parser);
4924
4925       /* Look for the optional `template' keyword, if this isn't the
4926          first time through the loop.  */
4927       if (success)
4928         template_keyword_p = cp_parser_optional_template_keyword (parser);
4929       else
4930         template_keyword_p = false;
4931
4932       /* Save the old scope since the name lookup we are about to do
4933          might destroy it.  */
4934       old_scope = parser->scope;
4935       saved_qualifying_scope = parser->qualifying_scope;
4936       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4937          look up names in "X<T>::I" in order to determine that "Y" is
4938          a template.  So, if we have a typename at this point, we make
4939          an effort to look through it.  */
4940       if (is_declaration
4941           && !typename_keyword_p
4942           && parser->scope
4943           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4944         parser->scope = resolve_typename_type (parser->scope,
4945                                                /*only_current_p=*/false);
4946       /* Parse the qualifying entity.  */
4947       new_scope
4948         = cp_parser_qualifying_entity (parser,
4949                                        typename_keyword_p,
4950                                        template_keyword_p,
4951                                        check_dependency_p,
4952                                        type_p,
4953                                        is_declaration);
4954       /* Look for the `::' token.  */
4955       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4956
4957       /* If we found what we wanted, we keep going; otherwise, we're
4958          done.  */
4959       if (!cp_parser_parse_definitely (parser))
4960         {
4961           bool error_p = false;
4962
4963           /* Restore the OLD_SCOPE since it was valid before the
4964              failed attempt at finding the last
4965              class-or-namespace-name.  */
4966           parser->scope = old_scope;
4967           parser->qualifying_scope = saved_qualifying_scope;
4968
4969           /* If the next token is a decltype, and the one after that is a
4970              `::', then the decltype has failed to resolve to a class or
4971              enumeration type.  Give this error even when parsing
4972              tentatively since it can't possibly be valid--and we're going
4973              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4974              won't get another chance.*/
4975           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4976               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4977                   == CPP_SCOPE))
4978             {
4979               token = cp_lexer_consume_token (parser->lexer);
4980               error_at (token->location, "decltype evaluates to %qT, "
4981                         "which is not a class or enumeration type",
4982                         token->u.value);
4983               parser->scope = error_mark_node;
4984               error_p = true;
4985               /* As below.  */
4986               success = true;
4987               cp_lexer_consume_token (parser->lexer);
4988             }
4989
4990           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4991             break;
4992           /* If the next token is an identifier, and the one after
4993              that is a `::', then any valid interpretation would have
4994              found a class-or-namespace-name.  */
4995           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4996                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4997                      == CPP_SCOPE)
4998                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4999                      != CPP_COMPL))
5000             {
5001               token = cp_lexer_consume_token (parser->lexer);
5002               if (!error_p)
5003                 {
5004                   if (!token->ambiguous_p)
5005                     {
5006                       tree decl;
5007                       tree ambiguous_decls;
5008
5009                       decl = cp_parser_lookup_name (parser, token->u.value,
5010                                                     none_type,
5011                                                     /*is_template=*/false,
5012                                                     /*is_namespace=*/false,
5013                                                     /*check_dependency=*/true,
5014                                                     &ambiguous_decls,
5015                                                     token->location);
5016                       if (TREE_CODE (decl) == TEMPLATE_DECL)
5017                         error_at (token->location,
5018                                   "%qD used without template parameters",
5019                                   decl);
5020                       else if (ambiguous_decls)
5021                         {
5022                           error_at (token->location,
5023                                     "reference to %qD is ambiguous",
5024                                     token->u.value);
5025                           print_candidates (ambiguous_decls);
5026                           decl = error_mark_node;
5027                         }
5028                       else
5029                         {
5030                           if (cxx_dialect != cxx98)
5031                             cp_parser_name_lookup_error
5032                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5033                              token->location);
5034                           else
5035                             cp_parser_name_lookup_error
5036                             (parser, token->u.value, decl, NLE_CXX98,
5037                              token->location);
5038                         }
5039                     }
5040                   parser->scope = error_mark_node;
5041                   error_p = true;
5042                   /* Treat this as a successful nested-name-specifier
5043                      due to:
5044
5045                      [basic.lookup.qual]
5046
5047                      If the name found is not a class-name (clause
5048                      _class_) or namespace-name (_namespace.def_), the
5049                      program is ill-formed.  */
5050                   success = true;
5051                 }
5052               cp_lexer_consume_token (parser->lexer);
5053             }
5054           break;
5055         }
5056       /* We've found one valid nested-name-specifier.  */
5057       success = true;
5058       /* Name lookup always gives us a DECL.  */
5059       if (TREE_CODE (new_scope) == TYPE_DECL)
5060         new_scope = TREE_TYPE (new_scope);
5061       /* Uses of "template" must be followed by actual templates.  */
5062       if (template_keyword_p
5063           && !(CLASS_TYPE_P (new_scope)
5064                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5065                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5066                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5067           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5068                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5069                    == TEMPLATE_ID_EXPR)))
5070         permerror (input_location, TYPE_P (new_scope)
5071                    ? G_("%qT is not a template")
5072                    : G_("%qD is not a template"),
5073                    new_scope);
5074       /* If it is a class scope, try to complete it; we are about to
5075          be looking up names inside the class.  */
5076       if (TYPE_P (new_scope)
5077           /* Since checking types for dependency can be expensive,
5078              avoid doing it if the type is already complete.  */
5079           && !COMPLETE_TYPE_P (new_scope)
5080           /* Do not try to complete dependent types.  */
5081           && !dependent_type_p (new_scope))
5082         {
5083           new_scope = complete_type (new_scope);
5084           /* If it is a typedef to current class, use the current
5085              class instead, as the typedef won't have any names inside
5086              it yet.  */
5087           if (!COMPLETE_TYPE_P (new_scope)
5088               && currently_open_class (new_scope))
5089             new_scope = TYPE_MAIN_VARIANT (new_scope);
5090         }
5091       /* Make sure we look in the right scope the next time through
5092          the loop.  */
5093       parser->scope = new_scope;
5094     }
5095
5096   /* If parsing tentatively, replace the sequence of tokens that makes
5097      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5098      token.  That way, should we re-parse the token stream, we will
5099      not have to repeat the effort required to do the parse, nor will
5100      we issue duplicate error messages.  */
5101   if (success && start)
5102     {
5103       cp_token *token;
5104
5105       token = cp_lexer_token_at (parser->lexer, start);
5106       /* Reset the contents of the START token.  */
5107       token->type = CPP_NESTED_NAME_SPECIFIER;
5108       /* Retrieve any deferred checks.  Do not pop this access checks yet
5109          so the memory will not be reclaimed during token replacing below.  */
5110       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5111       token->u.tree_check_value->value = parser->scope;
5112       token->u.tree_check_value->checks = get_deferred_access_checks ();
5113       token->u.tree_check_value->qualifying_scope =
5114         parser->qualifying_scope;
5115       token->keyword = RID_MAX;
5116
5117       /* Purge all subsequent tokens.  */
5118       cp_lexer_purge_tokens_after (parser->lexer, start);
5119     }
5120
5121   if (start)
5122     pop_to_parent_deferring_access_checks ();
5123
5124   return success ? parser->scope : NULL_TREE;
5125 }
5126
5127 /* Parse a nested-name-specifier.  See
5128    cp_parser_nested_name_specifier_opt for details.  This function
5129    behaves identically, except that it will an issue an error if no
5130    nested-name-specifier is present.  */
5131
5132 static tree
5133 cp_parser_nested_name_specifier (cp_parser *parser,
5134                                  bool typename_keyword_p,
5135                                  bool check_dependency_p,
5136                                  bool type_p,
5137                                  bool is_declaration)
5138 {
5139   tree scope;
5140
5141   /* Look for the nested-name-specifier.  */
5142   scope = cp_parser_nested_name_specifier_opt (parser,
5143                                                typename_keyword_p,
5144                                                check_dependency_p,
5145                                                type_p,
5146                                                is_declaration);
5147   /* If it was not present, issue an error message.  */
5148   if (!scope)
5149     {
5150       cp_parser_error (parser, "expected nested-name-specifier");
5151       parser->scope = NULL_TREE;
5152     }
5153
5154   return scope;
5155 }
5156
5157 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5158    this is either a class-name or a namespace-name (which corresponds
5159    to the class-or-namespace-name production in the grammar). For
5160    C++0x, it can also be a type-name that refers to an enumeration
5161    type or a simple-template-id.
5162
5163    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5164    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5165    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5166    TYPE_P is TRUE iff the next name should be taken as a class-name,
5167    even the same name is declared to be another entity in the same
5168    scope.
5169
5170    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5171    specified by the class-or-namespace-name.  If neither is found the
5172    ERROR_MARK_NODE is returned.  */
5173
5174 static tree
5175 cp_parser_qualifying_entity (cp_parser *parser,
5176                              bool typename_keyword_p,
5177                              bool template_keyword_p,
5178                              bool check_dependency_p,
5179                              bool type_p,
5180                              bool is_declaration)
5181 {
5182   tree saved_scope;
5183   tree saved_qualifying_scope;
5184   tree saved_object_scope;
5185   tree scope;
5186   bool only_class_p;
5187   bool successful_parse_p;
5188
5189   /* DR 743: decltype can appear in a nested-name-specifier.  */
5190   if (cp_lexer_next_token_is_decltype (parser->lexer))
5191     {
5192       scope = cp_parser_decltype (parser);
5193       if (TREE_CODE (scope) != ENUMERAL_TYPE
5194           && !MAYBE_CLASS_TYPE_P (scope))
5195         {
5196           cp_parser_simulate_error (parser);
5197           return error_mark_node;
5198         }
5199       if (TYPE_NAME (scope))
5200         scope = TYPE_NAME (scope);
5201       return scope;
5202     }
5203
5204   /* Before we try to parse the class-name, we must save away the
5205      current PARSER->SCOPE since cp_parser_class_name will destroy
5206      it.  */
5207   saved_scope = parser->scope;
5208   saved_qualifying_scope = parser->qualifying_scope;
5209   saved_object_scope = parser->object_scope;
5210   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5211      there is no need to look for a namespace-name.  */
5212   only_class_p = template_keyword_p 
5213     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5214   if (!only_class_p)
5215     cp_parser_parse_tentatively (parser);
5216   scope = cp_parser_class_name (parser,
5217                                 typename_keyword_p,
5218                                 template_keyword_p,
5219                                 type_p ? class_type : none_type,
5220                                 check_dependency_p,
5221                                 /*class_head_p=*/false,
5222                                 is_declaration);
5223   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5224   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5225   if (!only_class_p 
5226       && cxx_dialect != cxx98
5227       && !successful_parse_p)
5228     {
5229       /* Restore the saved scope.  */
5230       parser->scope = saved_scope;
5231       parser->qualifying_scope = saved_qualifying_scope;
5232       parser->object_scope = saved_object_scope;
5233
5234       /* Parse tentatively.  */
5235       cp_parser_parse_tentatively (parser);
5236      
5237       /* Parse a type-name  */
5238       scope = cp_parser_type_name (parser);
5239
5240       /* "If the name found does not designate a namespace or a class,
5241          enumeration, or dependent type, the program is ill-formed."
5242
5243          We cover classes and dependent types above and namespaces below,
5244          so this code is only looking for enums.  */
5245       if (!scope || TREE_CODE (scope) != TYPE_DECL
5246           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5247         cp_parser_simulate_error (parser);
5248
5249       successful_parse_p = cp_parser_parse_definitely (parser);
5250     }
5251   /* If that didn't work, try for a namespace-name.  */
5252   if (!only_class_p && !successful_parse_p)
5253     {
5254       /* Restore the saved scope.  */
5255       parser->scope = saved_scope;
5256       parser->qualifying_scope = saved_qualifying_scope;
5257       parser->object_scope = saved_object_scope;
5258       /* If we are not looking at an identifier followed by the scope
5259          resolution operator, then this is not part of a
5260          nested-name-specifier.  (Note that this function is only used
5261          to parse the components of a nested-name-specifier.)  */
5262       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5263           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5264         return error_mark_node;
5265       scope = cp_parser_namespace_name (parser);
5266     }
5267
5268   return scope;
5269 }
5270
5271 /* Parse a postfix-expression.
5272
5273    postfix-expression:
5274      primary-expression
5275      postfix-expression [ expression ]
5276      postfix-expression ( expression-list [opt] )
5277      simple-type-specifier ( expression-list [opt] )
5278      typename :: [opt] nested-name-specifier identifier
5279        ( expression-list [opt] )
5280      typename :: [opt] nested-name-specifier template [opt] template-id
5281        ( expression-list [opt] )
5282      postfix-expression . template [opt] id-expression
5283      postfix-expression -> template [opt] id-expression
5284      postfix-expression . pseudo-destructor-name
5285      postfix-expression -> pseudo-destructor-name
5286      postfix-expression ++
5287      postfix-expression --
5288      dynamic_cast < type-id > ( expression )
5289      static_cast < type-id > ( expression )
5290      reinterpret_cast < type-id > ( expression )
5291      const_cast < type-id > ( expression )
5292      typeid ( expression )
5293      typeid ( type-id )
5294
5295    GNU Extension:
5296
5297    postfix-expression:
5298      ( type-id ) { initializer-list , [opt] }
5299
5300    This extension is a GNU version of the C99 compound-literal
5301    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5302    but they are essentially the same concept.)
5303
5304    If ADDRESS_P is true, the postfix expression is the operand of the
5305    `&' operator.  CAST_P is true if this expression is the target of a
5306    cast.
5307
5308    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5309    class member access expressions [expr.ref].
5310
5311    Returns a representation of the expression.  */
5312
5313 static tree
5314 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5315                               bool member_access_only_p,
5316                               cp_id_kind * pidk_return)
5317 {
5318   cp_token *token;
5319   enum rid keyword;
5320   cp_id_kind idk = CP_ID_KIND_NONE;
5321   tree postfix_expression = NULL_TREE;
5322   bool is_member_access = false;
5323
5324   /* Peek at the next token.  */
5325   token = cp_lexer_peek_token (parser->lexer);
5326   /* Some of the productions are determined by keywords.  */
5327   keyword = token->keyword;
5328   switch (keyword)
5329     {
5330     case RID_DYNCAST:
5331     case RID_STATCAST:
5332     case RID_REINTCAST:
5333     case RID_CONSTCAST:
5334       {
5335         tree type;
5336         tree expression;
5337         const char *saved_message;
5338
5339         /* All of these can be handled in the same way from the point
5340            of view of parsing.  Begin by consuming the token
5341            identifying the cast.  */
5342         cp_lexer_consume_token (parser->lexer);
5343
5344         /* New types cannot be defined in the cast.  */
5345         saved_message = parser->type_definition_forbidden_message;
5346         parser->type_definition_forbidden_message
5347           = G_("types may not be defined in casts");
5348
5349         /* Look for the opening `<'.  */
5350         cp_parser_require (parser, CPP_LESS, RT_LESS);
5351         /* Parse the type to which we are casting.  */
5352         type = cp_parser_type_id (parser);
5353         /* Look for the closing `>'.  */
5354         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5355         /* Restore the old message.  */
5356         parser->type_definition_forbidden_message = saved_message;
5357
5358         /* And the expression which is being cast.  */
5359         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5360         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5361         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5362
5363         /* Only type conversions to integral or enumeration types
5364            can be used in constant-expressions.  */
5365         if (!cast_valid_in_integral_constant_expression_p (type)
5366             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5367           return error_mark_node;
5368
5369         switch (keyword)
5370           {
5371           case RID_DYNCAST:
5372             postfix_expression
5373               = build_dynamic_cast (type, expression, tf_warning_or_error);
5374             break;
5375           case RID_STATCAST:
5376             postfix_expression
5377               = build_static_cast (type, expression, tf_warning_or_error);
5378             break;
5379           case RID_REINTCAST:
5380             postfix_expression
5381               = build_reinterpret_cast (type, expression, 
5382                                         tf_warning_or_error);
5383             break;
5384           case RID_CONSTCAST:
5385             postfix_expression
5386               = build_const_cast (type, expression, tf_warning_or_error);
5387             break;
5388           default:
5389             gcc_unreachable ();
5390           }
5391       }
5392       break;
5393
5394     case RID_TYPEID:
5395       {
5396         tree type;
5397         const char *saved_message;
5398         bool saved_in_type_id_in_expr_p;
5399
5400         /* Consume the `typeid' token.  */
5401         cp_lexer_consume_token (parser->lexer);
5402         /* Look for the `(' token.  */
5403         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5404         /* Types cannot be defined in a `typeid' expression.  */
5405         saved_message = parser->type_definition_forbidden_message;
5406         parser->type_definition_forbidden_message
5407           = G_("types may not be defined in a %<typeid%> expression");
5408         /* We can't be sure yet whether we're looking at a type-id or an
5409            expression.  */
5410         cp_parser_parse_tentatively (parser);
5411         /* Try a type-id first.  */
5412         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5413         parser->in_type_id_in_expr_p = true;
5414         type = cp_parser_type_id (parser);
5415         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5416         /* Look for the `)' token.  Otherwise, we can't be sure that
5417            we're not looking at an expression: consider `typeid (int
5418            (3))', for example.  */
5419         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5420         /* If all went well, simply lookup the type-id.  */
5421         if (cp_parser_parse_definitely (parser))
5422           postfix_expression = get_typeid (type);
5423         /* Otherwise, fall back to the expression variant.  */
5424         else
5425           {
5426             tree expression;
5427
5428             /* Look for an expression.  */
5429             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5430             /* Compute its typeid.  */
5431             postfix_expression = build_typeid (expression);
5432             /* Look for the `)' token.  */
5433             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5434           }
5435         /* Restore the saved message.  */
5436         parser->type_definition_forbidden_message = saved_message;
5437         /* `typeid' may not appear in an integral constant expression.  */
5438         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5439           return error_mark_node;
5440       }
5441       break;
5442
5443     case RID_TYPENAME:
5444       {
5445         tree type;
5446         /* The syntax permitted here is the same permitted for an
5447            elaborated-type-specifier.  */
5448         type = cp_parser_elaborated_type_specifier (parser,
5449                                                     /*is_friend=*/false,
5450                                                     /*is_declaration=*/false);
5451         postfix_expression = cp_parser_functional_cast (parser, type);
5452       }
5453       break;
5454
5455     default:
5456       {
5457         tree type;
5458
5459         /* If the next thing is a simple-type-specifier, we may be
5460            looking at a functional cast.  We could also be looking at
5461            an id-expression.  So, we try the functional cast, and if
5462            that doesn't work we fall back to the primary-expression.  */
5463         cp_parser_parse_tentatively (parser);
5464         /* Look for the simple-type-specifier.  */
5465         type = cp_parser_simple_type_specifier (parser,
5466                                                 /*decl_specs=*/NULL,
5467                                                 CP_PARSER_FLAGS_NONE);
5468         /* Parse the cast itself.  */
5469         if (!cp_parser_error_occurred (parser))
5470           postfix_expression
5471             = cp_parser_functional_cast (parser, type);
5472         /* If that worked, we're done.  */
5473         if (cp_parser_parse_definitely (parser))
5474           break;
5475
5476         /* If the functional-cast didn't work out, try a
5477            compound-literal.  */
5478         if (cp_parser_allow_gnu_extensions_p (parser)
5479             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5480           {
5481             VEC(constructor_elt,gc) *initializer_list = NULL;
5482             bool saved_in_type_id_in_expr_p;
5483
5484             cp_parser_parse_tentatively (parser);
5485             /* Consume the `('.  */
5486             cp_lexer_consume_token (parser->lexer);
5487             /* Parse the type.  */
5488             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5489             parser->in_type_id_in_expr_p = true;
5490             type = cp_parser_type_id (parser);
5491             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5492             /* Look for the `)'.  */
5493             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5494             /* Look for the `{'.  */
5495             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5496             /* If things aren't going well, there's no need to
5497                keep going.  */
5498             if (!cp_parser_error_occurred (parser))
5499               {
5500                 bool non_constant_p;
5501                 /* Parse the initializer-list.  */
5502                 initializer_list
5503                   = cp_parser_initializer_list (parser, &non_constant_p);
5504                 /* Allow a trailing `,'.  */
5505                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5506                   cp_lexer_consume_token (parser->lexer);
5507                 /* Look for the final `}'.  */
5508                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5509               }
5510             /* If that worked, we're definitely looking at a
5511                compound-literal expression.  */
5512             if (cp_parser_parse_definitely (parser))
5513               {
5514                 /* Warn the user that a compound literal is not
5515                    allowed in standard C++.  */
5516                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5517                 /* For simplicity, we disallow compound literals in
5518                    constant-expressions.  We could
5519                    allow compound literals of integer type, whose
5520                    initializer was a constant, in constant
5521                    expressions.  Permitting that usage, as a further
5522                    extension, would not change the meaning of any
5523                    currently accepted programs.  (Of course, as
5524                    compound literals are not part of ISO C++, the
5525                    standard has nothing to say.)  */
5526                 if (cp_parser_non_integral_constant_expression (parser,
5527                                                                 NIC_NCC))
5528                   {
5529                     postfix_expression = error_mark_node;
5530                     break;
5531                   }
5532                 /* Form the representation of the compound-literal.  */
5533                 postfix_expression
5534                   = (finish_compound_literal
5535                      (type, build_constructor (init_list_type_node,
5536                                                initializer_list),
5537                       tf_warning_or_error));
5538                 break;
5539               }
5540           }
5541
5542         /* It must be a primary-expression.  */
5543         postfix_expression
5544           = cp_parser_primary_expression (parser, address_p, cast_p,
5545                                           /*template_arg_p=*/false,
5546                                           &idk);
5547       }
5548       break;
5549     }
5550
5551   /* Keep looping until the postfix-expression is complete.  */
5552   while (true)
5553     {
5554       if (idk == CP_ID_KIND_UNQUALIFIED
5555           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5556           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5557         /* It is not a Koenig lookup function call.  */
5558         postfix_expression
5559           = unqualified_name_lookup_error (postfix_expression);
5560
5561       /* Peek at the next token.  */
5562       token = cp_lexer_peek_token (parser->lexer);
5563
5564       switch (token->type)
5565         {
5566         case CPP_OPEN_SQUARE:
5567           postfix_expression
5568             = cp_parser_postfix_open_square_expression (parser,
5569                                                         postfix_expression,
5570                                                         false);
5571           idk = CP_ID_KIND_NONE;
5572           is_member_access = false;
5573           break;
5574
5575         case CPP_OPEN_PAREN:
5576           /* postfix-expression ( expression-list [opt] ) */
5577           {
5578             bool koenig_p;
5579             bool is_builtin_constant_p;
5580             bool saved_integral_constant_expression_p = false;
5581             bool saved_non_integral_constant_expression_p = false;
5582             VEC(tree,gc) *args;
5583
5584             is_member_access = false;
5585
5586             is_builtin_constant_p
5587               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5588             if (is_builtin_constant_p)
5589               {
5590                 /* The whole point of __builtin_constant_p is to allow
5591                    non-constant expressions to appear as arguments.  */
5592                 saved_integral_constant_expression_p
5593                   = parser->integral_constant_expression_p;
5594                 saved_non_integral_constant_expression_p
5595                   = parser->non_integral_constant_expression_p;
5596                 parser->integral_constant_expression_p = false;
5597               }
5598             args = (cp_parser_parenthesized_expression_list
5599                     (parser, non_attr,
5600                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5601                      /*non_constant_p=*/NULL));
5602             if (is_builtin_constant_p)
5603               {
5604                 parser->integral_constant_expression_p
5605                   = saved_integral_constant_expression_p;
5606                 parser->non_integral_constant_expression_p
5607                   = saved_non_integral_constant_expression_p;
5608               }
5609
5610             if (args == NULL)
5611               {
5612                 postfix_expression = error_mark_node;
5613                 break;
5614               }
5615
5616             /* Function calls are not permitted in
5617                constant-expressions.  */
5618             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5619                 && cp_parser_non_integral_constant_expression (parser,
5620                                                                NIC_FUNC_CALL))
5621               {
5622                 postfix_expression = error_mark_node;
5623                 release_tree_vector (args);
5624                 break;
5625               }
5626
5627             koenig_p = false;
5628             if (idk == CP_ID_KIND_UNQUALIFIED
5629                 || idk == CP_ID_KIND_TEMPLATE_ID)
5630               {
5631                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5632                   {
5633                     if (!VEC_empty (tree, args))
5634                       {
5635                         koenig_p = true;
5636                         if (!any_type_dependent_arguments_p (args))
5637                           postfix_expression
5638                             = perform_koenig_lookup (postfix_expression, args,
5639                                                      /*include_std=*/false,
5640                                                      tf_warning_or_error);
5641                       }
5642                     else
5643                       postfix_expression
5644                         = unqualified_fn_lookup_error (postfix_expression);
5645                   }
5646                 /* We do not perform argument-dependent lookup if
5647                    normal lookup finds a non-function, in accordance
5648                    with the expected resolution of DR 218.  */
5649                 else if (!VEC_empty (tree, args)
5650                          && is_overloaded_fn (postfix_expression))
5651                   {
5652                     tree fn = get_first_fn (postfix_expression);
5653                     fn = STRIP_TEMPLATE (fn);
5654
5655                     /* Do not do argument dependent lookup if regular
5656                        lookup finds a member function or a block-scope
5657                        function declaration.  [basic.lookup.argdep]/3  */
5658                     if (!DECL_FUNCTION_MEMBER_P (fn)
5659                         && !DECL_LOCAL_FUNCTION_P (fn))
5660                       {
5661                         koenig_p = true;
5662                         if (!any_type_dependent_arguments_p (args))
5663                           postfix_expression
5664                             = perform_koenig_lookup (postfix_expression, args,
5665                                                      /*include_std=*/false,
5666                                                      tf_warning_or_error);
5667                       }
5668                   }
5669               }
5670
5671             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5672               {
5673                 tree instance = TREE_OPERAND (postfix_expression, 0);
5674                 tree fn = TREE_OPERAND (postfix_expression, 1);
5675
5676                 if (processing_template_decl
5677                     && (type_dependent_expression_p (instance)
5678                         || (!BASELINK_P (fn)
5679                             && TREE_CODE (fn) != FIELD_DECL)
5680                         || type_dependent_expression_p (fn)
5681                         || any_type_dependent_arguments_p (args)))
5682                   {
5683                     postfix_expression
5684                       = build_nt_call_vec (postfix_expression, args);
5685                     release_tree_vector (args);
5686                     break;
5687                   }
5688
5689                 if (BASELINK_P (fn))
5690                   {
5691                   postfix_expression
5692                     = (build_new_method_call
5693                        (instance, fn, &args, NULL_TREE,
5694                         (idk == CP_ID_KIND_QUALIFIED
5695                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5696                          : LOOKUP_NORMAL),
5697                         /*fn_p=*/NULL,
5698                         tf_warning_or_error));
5699                   }
5700                 else
5701                   postfix_expression
5702                     = finish_call_expr (postfix_expression, &args,
5703                                         /*disallow_virtual=*/false,
5704                                         /*koenig_p=*/false,
5705                                         tf_warning_or_error);
5706               }
5707             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5708                      || TREE_CODE (postfix_expression) == MEMBER_REF
5709                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5710               postfix_expression = (build_offset_ref_call_from_tree
5711                                     (postfix_expression, &args));
5712             else if (idk == CP_ID_KIND_QUALIFIED)
5713               /* A call to a static class member, or a namespace-scope
5714                  function.  */
5715               postfix_expression
5716                 = finish_call_expr (postfix_expression, &args,
5717                                     /*disallow_virtual=*/true,
5718                                     koenig_p,
5719                                     tf_warning_or_error);
5720             else
5721               /* All other function calls.  */
5722               postfix_expression
5723                 = finish_call_expr (postfix_expression, &args,
5724                                     /*disallow_virtual=*/false,
5725                                     koenig_p,
5726                                     tf_warning_or_error);
5727
5728             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5729             idk = CP_ID_KIND_NONE;
5730
5731             release_tree_vector (args);
5732           }
5733           break;
5734
5735         case CPP_DOT:
5736         case CPP_DEREF:
5737           /* postfix-expression . template [opt] id-expression
5738              postfix-expression . pseudo-destructor-name
5739              postfix-expression -> template [opt] id-expression
5740              postfix-expression -> pseudo-destructor-name */
5741
5742           /* Consume the `.' or `->' operator.  */
5743           cp_lexer_consume_token (parser->lexer);
5744
5745           postfix_expression
5746             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5747                                                       postfix_expression,
5748                                                       false, &idk,
5749                                                       token->location);
5750
5751           is_member_access = true;
5752           break;
5753
5754         case CPP_PLUS_PLUS:
5755           /* postfix-expression ++  */
5756           /* Consume the `++' token.  */
5757           cp_lexer_consume_token (parser->lexer);
5758           /* Generate a representation for the complete expression.  */
5759           postfix_expression
5760             = finish_increment_expr (postfix_expression,
5761                                      POSTINCREMENT_EXPR);
5762           /* Increments may not appear in constant-expressions.  */
5763           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5764             postfix_expression = error_mark_node;
5765           idk = CP_ID_KIND_NONE;
5766           is_member_access = false;
5767           break;
5768
5769         case CPP_MINUS_MINUS:
5770           /* postfix-expression -- */
5771           /* Consume the `--' token.  */
5772           cp_lexer_consume_token (parser->lexer);
5773           /* Generate a representation for the complete expression.  */
5774           postfix_expression
5775             = finish_increment_expr (postfix_expression,
5776                                      POSTDECREMENT_EXPR);
5777           /* Decrements may not appear in constant-expressions.  */
5778           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5779             postfix_expression = error_mark_node;
5780           idk = CP_ID_KIND_NONE;
5781           is_member_access = false;
5782           break;
5783
5784         default:
5785           if (pidk_return != NULL)
5786             * pidk_return = idk;
5787           if (member_access_only_p)
5788             return is_member_access? postfix_expression : error_mark_node;
5789           else
5790             return postfix_expression;
5791         }
5792     }
5793
5794   /* We should never get here.  */
5795   gcc_unreachable ();
5796   return error_mark_node;
5797 }
5798
5799 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5800    by cp_parser_builtin_offsetof.  We're looking for
5801
5802      postfix-expression [ expression ]
5803
5804    FOR_OFFSETOF is set if we're being called in that context, which
5805    changes how we deal with integer constant expressions.  */
5806
5807 static tree
5808 cp_parser_postfix_open_square_expression (cp_parser *parser,
5809                                           tree postfix_expression,
5810                                           bool for_offsetof)
5811 {
5812   tree index;
5813
5814   /* Consume the `[' token.  */
5815   cp_lexer_consume_token (parser->lexer);
5816
5817   /* Parse the index expression.  */
5818   /* ??? For offsetof, there is a question of what to allow here.  If
5819      offsetof is not being used in an integral constant expression context,
5820      then we *could* get the right answer by computing the value at runtime.
5821      If we are in an integral constant expression context, then we might
5822      could accept any constant expression; hard to say without analysis.
5823      Rather than open the barn door too wide right away, allow only integer
5824      constant expressions here.  */
5825   if (for_offsetof)
5826     index = cp_parser_constant_expression (parser, false, NULL);
5827   else
5828     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5829
5830   /* Look for the closing `]'.  */
5831   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5832
5833   /* Build the ARRAY_REF.  */
5834   postfix_expression = grok_array_decl (postfix_expression, index);
5835
5836   /* When not doing offsetof, array references are not permitted in
5837      constant-expressions.  */
5838   if (!for_offsetof
5839       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5840     postfix_expression = error_mark_node;
5841
5842   return postfix_expression;
5843 }
5844
5845 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5846    by cp_parser_builtin_offsetof.  We're looking for
5847
5848      postfix-expression . template [opt] id-expression
5849      postfix-expression . pseudo-destructor-name
5850      postfix-expression -> template [opt] id-expression
5851      postfix-expression -> pseudo-destructor-name
5852
5853    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5854    limits what of the above we'll actually accept, but nevermind.
5855    TOKEN_TYPE is the "." or "->" token, which will already have been
5856    removed from the stream.  */
5857
5858 static tree
5859 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5860                                         enum cpp_ttype token_type,
5861                                         tree postfix_expression,
5862                                         bool for_offsetof, cp_id_kind *idk,
5863                                         location_t location)
5864 {
5865   tree name;
5866   bool dependent_p;
5867   bool pseudo_destructor_p;
5868   tree scope = NULL_TREE;
5869
5870   /* If this is a `->' operator, dereference the pointer.  */
5871   if (token_type == CPP_DEREF)
5872     postfix_expression = build_x_arrow (postfix_expression);
5873   /* Check to see whether or not the expression is type-dependent.  */
5874   dependent_p = type_dependent_expression_p (postfix_expression);
5875   /* The identifier following the `->' or `.' is not qualified.  */
5876   parser->scope = NULL_TREE;
5877   parser->qualifying_scope = NULL_TREE;
5878   parser->object_scope = NULL_TREE;
5879   *idk = CP_ID_KIND_NONE;
5880
5881   /* Enter the scope corresponding to the type of the object
5882      given by the POSTFIX_EXPRESSION.  */
5883   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5884     {
5885       scope = TREE_TYPE (postfix_expression);
5886       /* According to the standard, no expression should ever have
5887          reference type.  Unfortunately, we do not currently match
5888          the standard in this respect in that our internal representation
5889          of an expression may have reference type even when the standard
5890          says it does not.  Therefore, we have to manually obtain the
5891          underlying type here.  */
5892       scope = non_reference (scope);
5893       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5894       if (scope == unknown_type_node)
5895         {
5896           error_at (location, "%qE does not have class type",
5897                     postfix_expression);
5898           scope = NULL_TREE;
5899         }
5900       /* Unlike the object expression in other contexts, *this is not
5901          required to be of complete type for purposes of class member
5902          access (5.2.5) outside the member function body.  */
5903       else if (scope != current_class_ref
5904                && !(processing_template_decl && scope == current_class_type))
5905         scope = complete_type_or_else (scope, NULL_TREE);
5906       /* Let the name lookup machinery know that we are processing a
5907          class member access expression.  */
5908       parser->context->object_type = scope;
5909       /* If something went wrong, we want to be able to discern that case,
5910          as opposed to the case where there was no SCOPE due to the type
5911          of expression being dependent.  */
5912       if (!scope)
5913         scope = error_mark_node;
5914       /* If the SCOPE was erroneous, make the various semantic analysis
5915          functions exit quickly -- and without issuing additional error
5916          messages.  */
5917       if (scope == error_mark_node)
5918         postfix_expression = error_mark_node;
5919     }
5920
5921   /* Assume this expression is not a pseudo-destructor access.  */
5922   pseudo_destructor_p = false;
5923
5924   /* If the SCOPE is a scalar type, then, if this is a valid program,
5925      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5926      is type dependent, it can be pseudo-destructor-name or something else.
5927      Try to parse it as pseudo-destructor-name first.  */
5928   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5929     {
5930       tree s;
5931       tree type;
5932
5933       cp_parser_parse_tentatively (parser);
5934       /* Parse the pseudo-destructor-name.  */
5935       s = NULL_TREE;
5936       cp_parser_pseudo_destructor_name (parser, &s, &type);
5937       if (dependent_p
5938           && (cp_parser_error_occurred (parser)
5939               || TREE_CODE (type) != TYPE_DECL
5940               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5941         cp_parser_abort_tentative_parse (parser);
5942       else if (cp_parser_parse_definitely (parser))
5943         {
5944           pseudo_destructor_p = true;
5945           postfix_expression
5946             = finish_pseudo_destructor_expr (postfix_expression,
5947                                              s, TREE_TYPE (type));
5948         }
5949     }
5950
5951   if (!pseudo_destructor_p)
5952     {
5953       /* If the SCOPE is not a scalar type, we are looking at an
5954          ordinary class member access expression, rather than a
5955          pseudo-destructor-name.  */
5956       bool template_p;
5957       cp_token *token = cp_lexer_peek_token (parser->lexer);
5958       /* Parse the id-expression.  */
5959       name = (cp_parser_id_expression
5960               (parser,
5961                cp_parser_optional_template_keyword (parser),
5962                /*check_dependency_p=*/true,
5963                &template_p,
5964                /*declarator_p=*/false,
5965                /*optional_p=*/false));
5966       /* In general, build a SCOPE_REF if the member name is qualified.
5967          However, if the name was not dependent and has already been
5968          resolved; there is no need to build the SCOPE_REF.  For example;
5969
5970              struct X { void f(); };
5971              template <typename T> void f(T* t) { t->X::f(); }
5972
5973          Even though "t" is dependent, "X::f" is not and has been resolved
5974          to a BASELINK; there is no need to include scope information.  */
5975
5976       /* But we do need to remember that there was an explicit scope for
5977          virtual function calls.  */
5978       if (parser->scope)
5979         *idk = CP_ID_KIND_QUALIFIED;
5980
5981       /* If the name is a template-id that names a type, we will get a
5982          TYPE_DECL here.  That is invalid code.  */
5983       if (TREE_CODE (name) == TYPE_DECL)
5984         {
5985           error_at (token->location, "invalid use of %qD", name);
5986           postfix_expression = error_mark_node;
5987         }
5988       else
5989         {
5990           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5991             {
5992               if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
5993                 {
5994                   error_at (token->location, "%<%D::%D%> is not a class member",
5995                             parser->scope, name);
5996                   postfix_expression = error_mark_node;
5997                 }
5998               else
5999                 name = build_qualified_name (/*type=*/NULL_TREE,
6000                                              parser->scope,
6001                                              name,
6002                                              template_p);
6003               parser->scope = NULL_TREE;
6004               parser->qualifying_scope = NULL_TREE;
6005               parser->object_scope = NULL_TREE;
6006             }
6007           if (scope && name && BASELINK_P (name))
6008             adjust_result_of_qualified_name_lookup
6009               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
6010           postfix_expression
6011             = finish_class_member_access_expr (postfix_expression, name,
6012                                                template_p, 
6013                                                tf_warning_or_error);
6014         }
6015     }
6016
6017   /* We no longer need to look up names in the scope of the object on
6018      the left-hand side of the `.' or `->' operator.  */
6019   parser->context->object_type = NULL_TREE;
6020
6021   /* Outside of offsetof, these operators may not appear in
6022      constant-expressions.  */
6023   if (!for_offsetof
6024       && (cp_parser_non_integral_constant_expression
6025           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
6026     postfix_expression = error_mark_node;
6027
6028   return postfix_expression;
6029 }
6030
6031 /* Parse a parenthesized expression-list.
6032
6033    expression-list:
6034      assignment-expression
6035      expression-list, assignment-expression
6036
6037    attribute-list:
6038      expression-list
6039      identifier
6040      identifier, expression-list
6041
6042    CAST_P is true if this expression is the target of a cast.
6043
6044    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6045    argument pack.
6046
6047    Returns a vector of trees.  Each element is a representation of an
6048    assignment-expression.  NULL is returned if the ( and or ) are
6049    missing.  An empty, but allocated, vector is returned on no
6050    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6051    if we are parsing an attribute list for an attribute that wants a
6052    plain identifier argument, normal_attr for an attribute that wants
6053    an expression, or non_attr if we aren't parsing an attribute list.  If
6054    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6055    not all of the expressions in the list were constant.  */
6056
6057 static VEC(tree,gc) *
6058 cp_parser_parenthesized_expression_list (cp_parser* parser,
6059                                          int is_attribute_list,
6060                                          bool cast_p,
6061                                          bool allow_expansion_p,
6062                                          bool *non_constant_p)
6063 {
6064   VEC(tree,gc) *expression_list;
6065   bool fold_expr_p = is_attribute_list != non_attr;
6066   tree identifier = NULL_TREE;
6067   bool saved_greater_than_is_operator_p;
6068
6069   /* Assume all the expressions will be constant.  */
6070   if (non_constant_p)
6071     *non_constant_p = false;
6072
6073   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6074     return NULL;
6075
6076   expression_list = make_tree_vector ();
6077
6078   /* Within a parenthesized expression, a `>' token is always
6079      the greater-than operator.  */
6080   saved_greater_than_is_operator_p
6081     = parser->greater_than_is_operator_p;
6082   parser->greater_than_is_operator_p = true;
6083
6084   /* Consume expressions until there are no more.  */
6085   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6086     while (true)
6087       {
6088         tree expr;
6089
6090         /* At the beginning of attribute lists, check to see if the
6091            next token is an identifier.  */
6092         if (is_attribute_list == id_attr
6093             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6094           {
6095             cp_token *token;
6096
6097             /* Consume the identifier.  */
6098             token = cp_lexer_consume_token (parser->lexer);
6099             /* Save the identifier.  */
6100             identifier = token->u.value;
6101           }
6102         else
6103           {
6104             bool expr_non_constant_p;
6105
6106             /* Parse the next assignment-expression.  */
6107             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6108               {
6109                 /* A braced-init-list.  */
6110                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6111                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6112                 if (non_constant_p && expr_non_constant_p)
6113                   *non_constant_p = true;
6114               }
6115             else if (non_constant_p)
6116               {
6117                 expr = (cp_parser_constant_expression
6118                         (parser, /*allow_non_constant_p=*/true,
6119                          &expr_non_constant_p));
6120                 if (expr_non_constant_p)
6121                   *non_constant_p = true;
6122               }
6123             else
6124               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6125
6126             if (fold_expr_p)
6127               expr = fold_non_dependent_expr (expr);
6128
6129             /* If we have an ellipsis, then this is an expression
6130                expansion.  */
6131             if (allow_expansion_p
6132                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6133               {
6134                 /* Consume the `...'.  */
6135                 cp_lexer_consume_token (parser->lexer);
6136
6137                 /* Build the argument pack.  */
6138                 expr = make_pack_expansion (expr);
6139               }
6140
6141              /* Add it to the list.  We add error_mark_node
6142                 expressions to the list, so that we can still tell if
6143                 the correct form for a parenthesized expression-list
6144                 is found. That gives better errors.  */
6145             VEC_safe_push (tree, gc, expression_list, expr);
6146
6147             if (expr == error_mark_node)
6148               goto skip_comma;
6149           }
6150
6151         /* After the first item, attribute lists look the same as
6152            expression lists.  */
6153         is_attribute_list = non_attr;
6154
6155       get_comma:;
6156         /* If the next token isn't a `,', then we are done.  */
6157         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6158           break;
6159
6160         /* Otherwise, consume the `,' and keep going.  */
6161         cp_lexer_consume_token (parser->lexer);
6162       }
6163
6164   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6165     {
6166       int ending;
6167
6168     skip_comma:;
6169       /* We try and resync to an unnested comma, as that will give the
6170          user better diagnostics.  */
6171       ending = cp_parser_skip_to_closing_parenthesis (parser,
6172                                                       /*recovering=*/true,
6173                                                       /*or_comma=*/true,
6174                                                       /*consume_paren=*/true);
6175       if (ending < 0)
6176         goto get_comma;
6177       if (!ending)
6178         {
6179           parser->greater_than_is_operator_p
6180             = saved_greater_than_is_operator_p;
6181           return NULL;
6182         }
6183     }
6184
6185   parser->greater_than_is_operator_p
6186     = saved_greater_than_is_operator_p;
6187
6188   if (identifier)
6189     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6190
6191   return expression_list;
6192 }
6193
6194 /* Parse a pseudo-destructor-name.
6195
6196    pseudo-destructor-name:
6197      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6198      :: [opt] nested-name-specifier template template-id :: ~ type-name
6199      :: [opt] nested-name-specifier [opt] ~ type-name
6200
6201    If either of the first two productions is used, sets *SCOPE to the
6202    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6203    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6204    or ERROR_MARK_NODE if the parse fails.  */
6205
6206 static void
6207 cp_parser_pseudo_destructor_name (cp_parser* parser,
6208                                   tree* scope,
6209                                   tree* type)
6210 {
6211   bool nested_name_specifier_p;
6212
6213   /* Assume that things will not work out.  */
6214   *type = error_mark_node;
6215
6216   /* Look for the optional `::' operator.  */
6217   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6218   /* Look for the optional nested-name-specifier.  */
6219   nested_name_specifier_p
6220     = (cp_parser_nested_name_specifier_opt (parser,
6221                                             /*typename_keyword_p=*/false,
6222                                             /*check_dependency_p=*/true,
6223                                             /*type_p=*/false,
6224                                             /*is_declaration=*/false)
6225        != NULL_TREE);
6226   /* Now, if we saw a nested-name-specifier, we might be doing the
6227      second production.  */
6228   if (nested_name_specifier_p
6229       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6230     {
6231       /* Consume the `template' keyword.  */
6232       cp_lexer_consume_token (parser->lexer);
6233       /* Parse the template-id.  */
6234       cp_parser_template_id (parser,
6235                              /*template_keyword_p=*/true,
6236                              /*check_dependency_p=*/false,
6237                              /*is_declaration=*/true);
6238       /* Look for the `::' token.  */
6239       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6240     }
6241   /* If the next token is not a `~', then there might be some
6242      additional qualification.  */
6243   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6244     {
6245       /* At this point, we're looking for "type-name :: ~".  The type-name
6246          must not be a class-name, since this is a pseudo-destructor.  So,
6247          it must be either an enum-name, or a typedef-name -- both of which
6248          are just identifiers.  So, we peek ahead to check that the "::"
6249          and "~" tokens are present; if they are not, then we can avoid
6250          calling type_name.  */
6251       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6252           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6253           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6254         {
6255           cp_parser_error (parser, "non-scalar type");
6256           return;
6257         }
6258
6259       /* Look for the type-name.  */
6260       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6261       if (*scope == error_mark_node)
6262         return;
6263
6264       /* Look for the `::' token.  */
6265       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6266     }
6267   else
6268     *scope = NULL_TREE;
6269
6270   /* Look for the `~'.  */
6271   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6272
6273   /* Once we see the ~, this has to be a pseudo-destructor.  */
6274   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6275     cp_parser_commit_to_tentative_parse (parser);
6276
6277   /* Look for the type-name again.  We are not responsible for
6278      checking that it matches the first type-name.  */
6279   *type = cp_parser_nonclass_name (parser);
6280 }
6281
6282 /* Parse a unary-expression.
6283
6284    unary-expression:
6285      postfix-expression
6286      ++ cast-expression
6287      -- cast-expression
6288      unary-operator cast-expression
6289      sizeof unary-expression
6290      sizeof ( type-id )
6291      alignof ( type-id )  [C++0x]
6292      new-expression
6293      delete-expression
6294
6295    GNU Extensions:
6296
6297    unary-expression:
6298      __extension__ cast-expression
6299      __alignof__ unary-expression
6300      __alignof__ ( type-id )
6301      alignof unary-expression  [C++0x]
6302      __real__ cast-expression
6303      __imag__ cast-expression
6304      && identifier
6305
6306    ADDRESS_P is true iff the unary-expression is appearing as the
6307    operand of the `&' operator.   CAST_P is true if this expression is
6308    the target of a cast.
6309
6310    Returns a representation of the expression.  */
6311
6312 static tree
6313 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6314                             cp_id_kind * pidk)
6315 {
6316   cp_token *token;
6317   enum tree_code unary_operator;
6318
6319   /* Peek at the next token.  */
6320   token = cp_lexer_peek_token (parser->lexer);
6321   /* Some keywords give away the kind of expression.  */
6322   if (token->type == CPP_KEYWORD)
6323     {
6324       enum rid keyword = token->keyword;
6325
6326       switch (keyword)
6327         {
6328         case RID_ALIGNOF:
6329         case RID_SIZEOF:
6330           {
6331             tree operand;
6332             enum tree_code op;
6333
6334             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6335             /* Consume the token.  */
6336             cp_lexer_consume_token (parser->lexer);
6337             /* Parse the operand.  */
6338             operand = cp_parser_sizeof_operand (parser, keyword);
6339
6340             if (TYPE_P (operand))
6341               return cxx_sizeof_or_alignof_type (operand, op, true);
6342             else
6343               {
6344                 /* ISO C++ defines alignof only with types, not with
6345                    expressions. So pedwarn if alignof is used with a non-
6346                    type expression. However, __alignof__ is ok.  */
6347                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6348                   pedwarn (token->location, OPT_pedantic,
6349                            "ISO C++ does not allow %<alignof%> "
6350                            "with a non-type");
6351
6352                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6353               }
6354           }
6355
6356         case RID_NEW:
6357           return cp_parser_new_expression (parser);
6358
6359         case RID_DELETE:
6360           return cp_parser_delete_expression (parser);
6361
6362         case RID_EXTENSION:
6363           {
6364             /* The saved value of the PEDANTIC flag.  */
6365             int saved_pedantic;
6366             tree expr;
6367
6368             /* Save away the PEDANTIC flag.  */
6369             cp_parser_extension_opt (parser, &saved_pedantic);
6370             /* Parse the cast-expression.  */
6371             expr = cp_parser_simple_cast_expression (parser);
6372             /* Restore the PEDANTIC flag.  */
6373             pedantic = saved_pedantic;
6374
6375             return expr;
6376           }
6377
6378         case RID_REALPART:
6379         case RID_IMAGPART:
6380           {
6381             tree expression;
6382
6383             /* Consume the `__real__' or `__imag__' token.  */
6384             cp_lexer_consume_token (parser->lexer);
6385             /* Parse the cast-expression.  */
6386             expression = cp_parser_simple_cast_expression (parser);
6387             /* Create the complete representation.  */
6388             return build_x_unary_op ((keyword == RID_REALPART
6389                                       ? REALPART_EXPR : IMAGPART_EXPR),
6390                                      expression,
6391                                      tf_warning_or_error);
6392           }
6393           break;
6394
6395         case RID_TRANSACTION_ATOMIC:
6396         case RID_TRANSACTION_RELAXED:
6397           return cp_parser_transaction_expression (parser, keyword);
6398
6399         case RID_NOEXCEPT:
6400           {
6401             tree expr;
6402             const char *saved_message;
6403             bool saved_integral_constant_expression_p;
6404             bool saved_non_integral_constant_expression_p;
6405             bool saved_greater_than_is_operator_p;
6406
6407             cp_lexer_consume_token (parser->lexer);
6408             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6409
6410             saved_message = parser->type_definition_forbidden_message;
6411             parser->type_definition_forbidden_message
6412               = G_("types may not be defined in %<noexcept%> expressions");
6413
6414             saved_integral_constant_expression_p
6415               = parser->integral_constant_expression_p;
6416             saved_non_integral_constant_expression_p
6417               = parser->non_integral_constant_expression_p;
6418             parser->integral_constant_expression_p = false;
6419
6420             saved_greater_than_is_operator_p
6421               = parser->greater_than_is_operator_p;
6422             parser->greater_than_is_operator_p = true;
6423
6424             ++cp_unevaluated_operand;
6425             ++c_inhibit_evaluation_warnings;
6426             expr = cp_parser_expression (parser, false, NULL);
6427             --c_inhibit_evaluation_warnings;
6428             --cp_unevaluated_operand;
6429
6430             parser->greater_than_is_operator_p
6431               = saved_greater_than_is_operator_p;
6432
6433             parser->integral_constant_expression_p
6434               = saved_integral_constant_expression_p;
6435             parser->non_integral_constant_expression_p
6436               = saved_non_integral_constant_expression_p;
6437
6438             parser->type_definition_forbidden_message = saved_message;
6439
6440             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6441             return finish_noexcept_expr (expr, tf_warning_or_error);
6442           }
6443
6444         default:
6445           break;
6446         }
6447     }
6448
6449   /* Look for the `:: new' and `:: delete', which also signal the
6450      beginning of a new-expression, or delete-expression,
6451      respectively.  If the next token is `::', then it might be one of
6452      these.  */
6453   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6454     {
6455       enum rid keyword;
6456
6457       /* See if the token after the `::' is one of the keywords in
6458          which we're interested.  */
6459       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6460       /* If it's `new', we have a new-expression.  */
6461       if (keyword == RID_NEW)
6462         return cp_parser_new_expression (parser);
6463       /* Similarly, for `delete'.  */
6464       else if (keyword == RID_DELETE)
6465         return cp_parser_delete_expression (parser);
6466     }
6467
6468   /* Look for a unary operator.  */
6469   unary_operator = cp_parser_unary_operator (token);
6470   /* The `++' and `--' operators can be handled similarly, even though
6471      they are not technically unary-operators in the grammar.  */
6472   if (unary_operator == ERROR_MARK)
6473     {
6474       if (token->type == CPP_PLUS_PLUS)
6475         unary_operator = PREINCREMENT_EXPR;
6476       else if (token->type == CPP_MINUS_MINUS)
6477         unary_operator = PREDECREMENT_EXPR;
6478       /* Handle the GNU address-of-label extension.  */
6479       else if (cp_parser_allow_gnu_extensions_p (parser)
6480                && token->type == CPP_AND_AND)
6481         {
6482           tree identifier;
6483           tree expression;
6484           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6485
6486           /* Consume the '&&' token.  */
6487           cp_lexer_consume_token (parser->lexer);
6488           /* Look for the identifier.  */
6489           identifier = cp_parser_identifier (parser);
6490           /* Create an expression representing the address.  */
6491           expression = finish_label_address_expr (identifier, loc);
6492           if (cp_parser_non_integral_constant_expression (parser,
6493                                                           NIC_ADDR_LABEL))
6494             expression = error_mark_node;
6495           return expression;
6496         }
6497     }
6498   if (unary_operator != ERROR_MARK)
6499     {
6500       tree cast_expression;
6501       tree expression = error_mark_node;
6502       non_integral_constant non_constant_p = NIC_NONE;
6503
6504       /* Consume the operator token.  */
6505       token = cp_lexer_consume_token (parser->lexer);
6506       /* Parse the cast-expression.  */
6507       cast_expression
6508         = cp_parser_cast_expression (parser,
6509                                      unary_operator == ADDR_EXPR,
6510                                      /*cast_p=*/false, pidk);
6511       /* Now, build an appropriate representation.  */
6512       switch (unary_operator)
6513         {
6514         case INDIRECT_REF:
6515           non_constant_p = NIC_STAR;
6516           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6517                                              tf_warning_or_error);
6518           break;
6519
6520         case ADDR_EXPR:
6521            non_constant_p = NIC_ADDR;
6522           /* Fall through.  */
6523         case BIT_NOT_EXPR:
6524           expression = build_x_unary_op (unary_operator, cast_expression,
6525                                          tf_warning_or_error);
6526           break;
6527
6528         case PREINCREMENT_EXPR:
6529         case PREDECREMENT_EXPR:
6530           non_constant_p = unary_operator == PREINCREMENT_EXPR
6531                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6532           /* Fall through.  */
6533         case UNARY_PLUS_EXPR:
6534         case NEGATE_EXPR:
6535         case TRUTH_NOT_EXPR:
6536           expression = finish_unary_op_expr (unary_operator, cast_expression);
6537           break;
6538
6539         default:
6540           gcc_unreachable ();
6541         }
6542
6543       if (non_constant_p != NIC_NONE
6544           && cp_parser_non_integral_constant_expression (parser,
6545                                                          non_constant_p))
6546         expression = error_mark_node;
6547
6548       return expression;
6549     }
6550
6551   return cp_parser_postfix_expression (parser, address_p, cast_p,
6552                                        /*member_access_only_p=*/false,
6553                                        pidk);
6554 }
6555
6556 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6557    unary-operator, the corresponding tree code is returned.  */
6558
6559 static enum tree_code
6560 cp_parser_unary_operator (cp_token* token)
6561 {
6562   switch (token->type)
6563     {
6564     case CPP_MULT:
6565       return INDIRECT_REF;
6566
6567     case CPP_AND:
6568       return ADDR_EXPR;
6569
6570     case CPP_PLUS:
6571       return UNARY_PLUS_EXPR;
6572
6573     case CPP_MINUS:
6574       return NEGATE_EXPR;
6575
6576     case CPP_NOT:
6577       return TRUTH_NOT_EXPR;
6578
6579     case CPP_COMPL:
6580       return BIT_NOT_EXPR;
6581
6582     default:
6583       return ERROR_MARK;
6584     }
6585 }
6586
6587 /* Parse a new-expression.
6588
6589    new-expression:
6590      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6591      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6592
6593    Returns a representation of the expression.  */
6594
6595 static tree
6596 cp_parser_new_expression (cp_parser* parser)
6597 {
6598   bool global_scope_p;
6599   VEC(tree,gc) *placement;
6600   tree type;
6601   VEC(tree,gc) *initializer;
6602   tree nelts;
6603   tree ret;
6604
6605   /* Look for the optional `::' operator.  */
6606   global_scope_p
6607     = (cp_parser_global_scope_opt (parser,
6608                                    /*current_scope_valid_p=*/false)
6609        != NULL_TREE);
6610   /* Look for the `new' operator.  */
6611   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6612   /* There's no easy way to tell a new-placement from the
6613      `( type-id )' construct.  */
6614   cp_parser_parse_tentatively (parser);
6615   /* Look for a new-placement.  */
6616   placement = cp_parser_new_placement (parser);
6617   /* If that didn't work out, there's no new-placement.  */
6618   if (!cp_parser_parse_definitely (parser))
6619     {
6620       if (placement != NULL)
6621         release_tree_vector (placement);
6622       placement = NULL;
6623     }
6624
6625   /* If the next token is a `(', then we have a parenthesized
6626      type-id.  */
6627   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6628     {
6629       cp_token *token;
6630       /* Consume the `('.  */
6631       cp_lexer_consume_token (parser->lexer);
6632       /* Parse the type-id.  */
6633       type = cp_parser_type_id (parser);
6634       /* Look for the closing `)'.  */
6635       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6636       token = cp_lexer_peek_token (parser->lexer);
6637       /* There should not be a direct-new-declarator in this production,
6638          but GCC used to allowed this, so we check and emit a sensible error
6639          message for this case.  */
6640       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6641         {
6642           error_at (token->location,
6643                     "array bound forbidden after parenthesized type-id");
6644           inform (token->location, 
6645                   "try removing the parentheses around the type-id");
6646           cp_parser_direct_new_declarator (parser);
6647         }
6648       nelts = NULL_TREE;
6649     }
6650   /* Otherwise, there must be a new-type-id.  */
6651   else
6652     type = cp_parser_new_type_id (parser, &nelts);
6653
6654   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6655   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6656       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6657     initializer = cp_parser_new_initializer (parser);
6658   else
6659     initializer = NULL;
6660
6661   /* A new-expression may not appear in an integral constant
6662      expression.  */
6663   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6664     ret = error_mark_node;
6665   else
6666     {
6667       /* Create a representation of the new-expression.  */
6668       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6669                        tf_warning_or_error);
6670     }
6671
6672   if (placement != NULL)
6673     release_tree_vector (placement);
6674   if (initializer != NULL)
6675     release_tree_vector (initializer);
6676
6677   return ret;
6678 }
6679
6680 /* Parse a new-placement.
6681
6682    new-placement:
6683      ( expression-list )
6684
6685    Returns the same representation as for an expression-list.  */
6686
6687 static VEC(tree,gc) *
6688 cp_parser_new_placement (cp_parser* parser)
6689 {
6690   VEC(tree,gc) *expression_list;
6691
6692   /* Parse the expression-list.  */
6693   expression_list = (cp_parser_parenthesized_expression_list
6694                      (parser, non_attr, /*cast_p=*/false,
6695                       /*allow_expansion_p=*/true,
6696                       /*non_constant_p=*/NULL));
6697
6698   return expression_list;
6699 }
6700
6701 /* Parse a new-type-id.
6702
6703    new-type-id:
6704      type-specifier-seq new-declarator [opt]
6705
6706    Returns the TYPE allocated.  If the new-type-id indicates an array
6707    type, *NELTS is set to the number of elements in the last array
6708    bound; the TYPE will not include the last array bound.  */
6709
6710 static tree
6711 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6712 {
6713   cp_decl_specifier_seq type_specifier_seq;
6714   cp_declarator *new_declarator;
6715   cp_declarator *declarator;
6716   cp_declarator *outer_declarator;
6717   const char *saved_message;
6718   tree type;
6719
6720   /* The type-specifier sequence must not contain type definitions.
6721      (It cannot contain declarations of new types either, but if they
6722      are not definitions we will catch that because they are not
6723      complete.)  */
6724   saved_message = parser->type_definition_forbidden_message;
6725   parser->type_definition_forbidden_message
6726     = G_("types may not be defined in a new-type-id");
6727   /* Parse the type-specifier-seq.  */
6728   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6729                                 /*is_trailing_return=*/false,
6730                                 &type_specifier_seq);
6731   /* Restore the old message.  */
6732   parser->type_definition_forbidden_message = saved_message;
6733   /* Parse the new-declarator.  */
6734   new_declarator = cp_parser_new_declarator_opt (parser);
6735
6736   /* Determine the number of elements in the last array dimension, if
6737      any.  */
6738   *nelts = NULL_TREE;
6739   /* Skip down to the last array dimension.  */
6740   declarator = new_declarator;
6741   outer_declarator = NULL;
6742   while (declarator && (declarator->kind == cdk_pointer
6743                         || declarator->kind == cdk_ptrmem))
6744     {
6745       outer_declarator = declarator;
6746       declarator = declarator->declarator;
6747     }
6748   while (declarator
6749          && declarator->kind == cdk_array
6750          && declarator->declarator
6751          && declarator->declarator->kind == cdk_array)
6752     {
6753       outer_declarator = declarator;
6754       declarator = declarator->declarator;
6755     }
6756
6757   if (declarator && declarator->kind == cdk_array)
6758     {
6759       *nelts = declarator->u.array.bounds;
6760       if (*nelts == error_mark_node)
6761         *nelts = integer_one_node;
6762
6763       if (outer_declarator)
6764         outer_declarator->declarator = declarator->declarator;
6765       else
6766         new_declarator = NULL;
6767     }
6768
6769   type = groktypename (&type_specifier_seq, new_declarator, false);
6770   return type;
6771 }
6772
6773 /* Parse an (optional) new-declarator.
6774
6775    new-declarator:
6776      ptr-operator new-declarator [opt]
6777      direct-new-declarator
6778
6779    Returns the declarator.  */
6780
6781 static cp_declarator *
6782 cp_parser_new_declarator_opt (cp_parser* parser)
6783 {
6784   enum tree_code code;
6785   tree type;
6786   cp_cv_quals cv_quals;
6787
6788   /* We don't know if there's a ptr-operator next, or not.  */
6789   cp_parser_parse_tentatively (parser);
6790   /* Look for a ptr-operator.  */
6791   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6792   /* If that worked, look for more new-declarators.  */
6793   if (cp_parser_parse_definitely (parser))
6794     {
6795       cp_declarator *declarator;
6796
6797       /* Parse another optional declarator.  */
6798       declarator = cp_parser_new_declarator_opt (parser);
6799
6800       return cp_parser_make_indirect_declarator
6801         (code, type, cv_quals, declarator);
6802     }
6803
6804   /* If the next token is a `[', there is a direct-new-declarator.  */
6805   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6806     return cp_parser_direct_new_declarator (parser);
6807
6808   return NULL;
6809 }
6810
6811 /* Parse a direct-new-declarator.
6812
6813    direct-new-declarator:
6814      [ expression ]
6815      direct-new-declarator [constant-expression]
6816
6817    */
6818
6819 static cp_declarator *
6820 cp_parser_direct_new_declarator (cp_parser* parser)
6821 {
6822   cp_declarator *declarator = NULL;
6823
6824   while (true)
6825     {
6826       tree expression;
6827
6828       /* Look for the opening `['.  */
6829       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6830       /* The first expression is not required to be constant.  */
6831       if (!declarator)
6832         {
6833           cp_token *token = cp_lexer_peek_token (parser->lexer);
6834           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6835           /* The standard requires that the expression have integral
6836              type.  DR 74 adds enumeration types.  We believe that the
6837              real intent is that these expressions be handled like the
6838              expression in a `switch' condition, which also allows
6839              classes with a single conversion to integral or
6840              enumeration type.  */
6841           if (!processing_template_decl)
6842             {
6843               expression
6844                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6845                                               expression,
6846                                               /*complain=*/true);
6847               if (!expression)
6848                 {
6849                   error_at (token->location,
6850                             "expression in new-declarator must have integral "
6851                             "or enumeration type");
6852                   expression = error_mark_node;
6853                 }
6854             }
6855         }
6856       /* But all the other expressions must be.  */
6857       else
6858         expression
6859           = cp_parser_constant_expression (parser,
6860                                            /*allow_non_constant=*/false,
6861                                            NULL);
6862       /* Look for the closing `]'.  */
6863       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6864
6865       /* Add this bound to the declarator.  */
6866       declarator = make_array_declarator (declarator, expression);
6867
6868       /* If the next token is not a `[', then there are no more
6869          bounds.  */
6870       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6871         break;
6872     }
6873
6874   return declarator;
6875 }
6876
6877 /* Parse a new-initializer.
6878
6879    new-initializer:
6880      ( expression-list [opt] )
6881      braced-init-list
6882
6883    Returns a representation of the expression-list.  */
6884
6885 static VEC(tree,gc) *
6886 cp_parser_new_initializer (cp_parser* parser)
6887 {
6888   VEC(tree,gc) *expression_list;
6889
6890   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6891     {
6892       tree t;
6893       bool expr_non_constant_p;
6894       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6895       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6896       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6897       expression_list = make_tree_vector_single (t);
6898     }
6899   else
6900     expression_list = (cp_parser_parenthesized_expression_list
6901                        (parser, non_attr, /*cast_p=*/false,
6902                         /*allow_expansion_p=*/true,
6903                         /*non_constant_p=*/NULL));
6904
6905   return expression_list;
6906 }
6907
6908 /* Parse a delete-expression.
6909
6910    delete-expression:
6911      :: [opt] delete cast-expression
6912      :: [opt] delete [ ] cast-expression
6913
6914    Returns a representation of the expression.  */
6915
6916 static tree
6917 cp_parser_delete_expression (cp_parser* parser)
6918 {
6919   bool global_scope_p;
6920   bool array_p;
6921   tree expression;
6922
6923   /* Look for the optional `::' operator.  */
6924   global_scope_p
6925     = (cp_parser_global_scope_opt (parser,
6926                                    /*current_scope_valid_p=*/false)
6927        != NULL_TREE);
6928   /* Look for the `delete' keyword.  */
6929   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6930   /* See if the array syntax is in use.  */
6931   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6932     {
6933       /* Consume the `[' token.  */
6934       cp_lexer_consume_token (parser->lexer);
6935       /* Look for the `]' token.  */
6936       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6937       /* Remember that this is the `[]' construct.  */
6938       array_p = true;
6939     }
6940   else
6941     array_p = false;
6942
6943   /* Parse the cast-expression.  */
6944   expression = cp_parser_simple_cast_expression (parser);
6945
6946   /* A delete-expression may not appear in an integral constant
6947      expression.  */
6948   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6949     return error_mark_node;
6950
6951   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6952                         tf_warning_or_error);
6953 }
6954
6955 /* Returns true if TOKEN may start a cast-expression and false
6956    otherwise.  */
6957
6958 static bool
6959 cp_parser_token_starts_cast_expression (cp_token *token)
6960 {
6961   switch (token->type)
6962     {
6963     case CPP_COMMA:
6964     case CPP_SEMICOLON:
6965     case CPP_QUERY:
6966     case CPP_COLON:
6967     case CPP_CLOSE_SQUARE:
6968     case CPP_CLOSE_PAREN:
6969     case CPP_CLOSE_BRACE:
6970     case CPP_DOT:
6971     case CPP_DOT_STAR:
6972     case CPP_DEREF:
6973     case CPP_DEREF_STAR:
6974     case CPP_DIV:
6975     case CPP_MOD:
6976     case CPP_LSHIFT:
6977     case CPP_RSHIFT:
6978     case CPP_LESS:
6979     case CPP_GREATER:
6980     case CPP_LESS_EQ:
6981     case CPP_GREATER_EQ:
6982     case CPP_EQ_EQ:
6983     case CPP_NOT_EQ:
6984     case CPP_EQ:
6985     case CPP_MULT_EQ:
6986     case CPP_DIV_EQ:
6987     case CPP_MOD_EQ:
6988     case CPP_PLUS_EQ:
6989     case CPP_MINUS_EQ:
6990     case CPP_RSHIFT_EQ:
6991     case CPP_LSHIFT_EQ:
6992     case CPP_AND_EQ:
6993     case CPP_XOR_EQ:
6994     case CPP_OR_EQ:
6995     case CPP_XOR:
6996     case CPP_OR:
6997     case CPP_OR_OR:
6998     case CPP_EOF:
6999       return false;
7000
7001       /* '[' may start a primary-expression in obj-c++.  */
7002     case CPP_OPEN_SQUARE:
7003       return c_dialect_objc ();
7004
7005     default:
7006       return true;
7007     }
7008 }
7009
7010 /* Parse a cast-expression.
7011
7012    cast-expression:
7013      unary-expression
7014      ( type-id ) cast-expression
7015
7016    ADDRESS_P is true iff the unary-expression is appearing as the
7017    operand of the `&' operator.   CAST_P is true if this expression is
7018    the target of a cast.
7019
7020    Returns a representation of the expression.  */
7021
7022 static tree
7023 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
7024                            cp_id_kind * pidk)
7025 {
7026   /* If it's a `(', then we might be looking at a cast.  */
7027   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
7028     {
7029       tree type = NULL_TREE;
7030       tree expr = NULL_TREE;
7031       bool compound_literal_p;
7032       const char *saved_message;
7033
7034       /* There's no way to know yet whether or not this is a cast.
7035          For example, `(int (3))' is a unary-expression, while `(int)
7036          3' is a cast.  So, we resort to parsing tentatively.  */
7037       cp_parser_parse_tentatively (parser);
7038       /* Types may not be defined in a cast.  */
7039       saved_message = parser->type_definition_forbidden_message;
7040       parser->type_definition_forbidden_message
7041         = G_("types may not be defined in casts");
7042       /* Consume the `('.  */
7043       cp_lexer_consume_token (parser->lexer);
7044       /* A very tricky bit is that `(struct S) { 3 }' is a
7045          compound-literal (which we permit in C++ as an extension).
7046          But, that construct is not a cast-expression -- it is a
7047          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7048          is legal; if the compound-literal were a cast-expression,
7049          you'd need an extra set of parentheses.)  But, if we parse
7050          the type-id, and it happens to be a class-specifier, then we
7051          will commit to the parse at that point, because we cannot
7052          undo the action that is done when creating a new class.  So,
7053          then we cannot back up and do a postfix-expression.
7054
7055          Therefore, we scan ahead to the closing `)', and check to see
7056          if the token after the `)' is a `{'.  If so, we are not
7057          looking at a cast-expression.
7058
7059          Save tokens so that we can put them back.  */
7060       cp_lexer_save_tokens (parser->lexer);
7061       /* Skip tokens until the next token is a closing parenthesis.
7062          If we find the closing `)', and the next token is a `{', then
7063          we are looking at a compound-literal.  */
7064       compound_literal_p
7065         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7066                                                   /*consume_paren=*/true)
7067            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7068       /* Roll back the tokens we skipped.  */
7069       cp_lexer_rollback_tokens (parser->lexer);
7070       /* If we were looking at a compound-literal, simulate an error
7071          so that the call to cp_parser_parse_definitely below will
7072          fail.  */
7073       if (compound_literal_p)
7074         cp_parser_simulate_error (parser);
7075       else
7076         {
7077           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7078           parser->in_type_id_in_expr_p = true;
7079           /* Look for the type-id.  */
7080           type = cp_parser_type_id (parser);
7081           /* Look for the closing `)'.  */
7082           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7083           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7084         }
7085
7086       /* Restore the saved message.  */
7087       parser->type_definition_forbidden_message = saved_message;
7088
7089       /* At this point this can only be either a cast or a
7090          parenthesized ctor such as `(T ())' that looks like a cast to
7091          function returning T.  */
7092       if (!cp_parser_error_occurred (parser)
7093           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7094                                                      (parser->lexer)))
7095         {
7096           cp_parser_parse_definitely (parser);
7097           expr = cp_parser_cast_expression (parser,
7098                                             /*address_p=*/false,
7099                                             /*cast_p=*/true, pidk);
7100
7101           /* Warn about old-style casts, if so requested.  */
7102           if (warn_old_style_cast
7103               && !in_system_header
7104               && !VOID_TYPE_P (type)
7105               && current_lang_name != lang_name_c)
7106             warning (OPT_Wold_style_cast, "use of old-style cast");
7107
7108           /* Only type conversions to integral or enumeration types
7109              can be used in constant-expressions.  */
7110           if (!cast_valid_in_integral_constant_expression_p (type)
7111               && cp_parser_non_integral_constant_expression (parser,
7112                                                              NIC_CAST))
7113             return error_mark_node;
7114
7115           /* Perform the cast.  */
7116           expr = build_c_cast (input_location, type, expr);
7117           return expr;
7118         }
7119       else 
7120         cp_parser_abort_tentative_parse (parser);
7121     }
7122
7123   /* If we get here, then it's not a cast, so it must be a
7124      unary-expression.  */
7125   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7126 }
7127
7128 /* Parse a binary expression of the general form:
7129
7130    pm-expression:
7131      cast-expression
7132      pm-expression .* cast-expression
7133      pm-expression ->* cast-expression
7134
7135    multiplicative-expression:
7136      pm-expression
7137      multiplicative-expression * pm-expression
7138      multiplicative-expression / pm-expression
7139      multiplicative-expression % pm-expression
7140
7141    additive-expression:
7142      multiplicative-expression
7143      additive-expression + multiplicative-expression
7144      additive-expression - multiplicative-expression
7145
7146    shift-expression:
7147      additive-expression
7148      shift-expression << additive-expression
7149      shift-expression >> additive-expression
7150
7151    relational-expression:
7152      shift-expression
7153      relational-expression < shift-expression
7154      relational-expression > shift-expression
7155      relational-expression <= shift-expression
7156      relational-expression >= shift-expression
7157
7158   GNU Extension:
7159
7160    relational-expression:
7161      relational-expression <? shift-expression
7162      relational-expression >? shift-expression
7163
7164    equality-expression:
7165      relational-expression
7166      equality-expression == relational-expression
7167      equality-expression != relational-expression
7168
7169    and-expression:
7170      equality-expression
7171      and-expression & equality-expression
7172
7173    exclusive-or-expression:
7174      and-expression
7175      exclusive-or-expression ^ and-expression
7176
7177    inclusive-or-expression:
7178      exclusive-or-expression
7179      inclusive-or-expression | exclusive-or-expression
7180
7181    logical-and-expression:
7182      inclusive-or-expression
7183      logical-and-expression && inclusive-or-expression
7184
7185    logical-or-expression:
7186      logical-and-expression
7187      logical-or-expression || logical-and-expression
7188
7189    All these are implemented with a single function like:
7190
7191    binary-expression:
7192      simple-cast-expression
7193      binary-expression <token> binary-expression
7194
7195    CAST_P is true if this expression is the target of a cast.
7196
7197    The binops_by_token map is used to get the tree codes for each <token> type.
7198    binary-expressions are associated according to a precedence table.  */
7199
7200 #define TOKEN_PRECEDENCE(token)                              \
7201 (((token->type == CPP_GREATER                                \
7202    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7203   && !parser->greater_than_is_operator_p)                    \
7204  ? PREC_NOT_OPERATOR                                         \
7205  : binops_by_token[token->type].prec)
7206
7207 static tree
7208 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7209                              bool no_toplevel_fold_p,
7210                              enum cp_parser_prec prec,
7211                              cp_id_kind * pidk)
7212 {
7213   cp_parser_expression_stack stack;
7214   cp_parser_expression_stack_entry *sp = &stack[0];
7215   tree lhs, rhs;
7216   cp_token *token;
7217   enum tree_code tree_type, lhs_type, rhs_type;
7218   enum cp_parser_prec new_prec, lookahead_prec;
7219   tree overload;
7220
7221   /* Parse the first expression.  */
7222   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7223   lhs_type = ERROR_MARK;
7224
7225   for (;;)
7226     {
7227       /* Get an operator token.  */
7228       token = cp_lexer_peek_token (parser->lexer);
7229
7230       if (warn_cxx0x_compat
7231           && token->type == CPP_RSHIFT
7232           && !parser->greater_than_is_operator_p)
7233         {
7234           if (warning_at (token->location, OPT_Wc__0x_compat, 
7235                           "%<>>%> operator is treated as"
7236                           " two right angle brackets in C++11"))
7237             inform (token->location,
7238                     "suggest parentheses around %<>>%> expression");
7239         }
7240
7241       new_prec = TOKEN_PRECEDENCE (token);
7242
7243       /* Popping an entry off the stack means we completed a subexpression:
7244          - either we found a token which is not an operator (`>' where it is not
7245            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7246            will happen repeatedly;
7247          - or, we found an operator which has lower priority.  This is the case
7248            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7249            parsing `3 * 4'.  */
7250       if (new_prec <= prec)
7251         {
7252           if (sp == stack)
7253             break;
7254           else
7255             goto pop;
7256         }
7257
7258      get_rhs:
7259       tree_type = binops_by_token[token->type].tree_type;
7260
7261       /* We used the operator token.  */
7262       cp_lexer_consume_token (parser->lexer);
7263
7264       /* For "false && x" or "true || x", x will never be executed;
7265          disable warnings while evaluating it.  */
7266       if (tree_type == TRUTH_ANDIF_EXPR)
7267         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7268       else if (tree_type == TRUTH_ORIF_EXPR)
7269         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7270
7271       /* Extract another operand.  It may be the RHS of this expression
7272          or the LHS of a new, higher priority expression.  */
7273       rhs = cp_parser_simple_cast_expression (parser);
7274       rhs_type = ERROR_MARK;
7275
7276       /* Get another operator token.  Look up its precedence to avoid
7277          building a useless (immediately popped) stack entry for common
7278          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7279       token = cp_lexer_peek_token (parser->lexer);
7280       lookahead_prec = TOKEN_PRECEDENCE (token);
7281       if (lookahead_prec > new_prec)
7282         {
7283           /* ... and prepare to parse the RHS of the new, higher priority
7284              expression.  Since precedence levels on the stack are
7285              monotonically increasing, we do not have to care about
7286              stack overflows.  */
7287           sp->prec = prec;
7288           sp->tree_type = tree_type;
7289           sp->lhs = lhs;
7290           sp->lhs_type = lhs_type;
7291           sp++;
7292           lhs = rhs;
7293           lhs_type = rhs_type;
7294           prec = new_prec;
7295           new_prec = lookahead_prec;
7296           goto get_rhs;
7297
7298          pop:
7299           lookahead_prec = new_prec;
7300           /* If the stack is not empty, we have parsed into LHS the right side
7301              (`4' in the example above) of an expression we had suspended.
7302              We can use the information on the stack to recover the LHS (`3')
7303              from the stack together with the tree code (`MULT_EXPR'), and
7304              the precedence of the higher level subexpression
7305              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7306              which will be used to actually build the additive expression.  */
7307           --sp;
7308           prec = sp->prec;
7309           tree_type = sp->tree_type;
7310           rhs = lhs;
7311           rhs_type = lhs_type;
7312           lhs = sp->lhs;
7313           lhs_type = sp->lhs_type;
7314         }
7315
7316       /* Undo the disabling of warnings done above.  */
7317       if (tree_type == TRUTH_ANDIF_EXPR)
7318         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7319       else if (tree_type == TRUTH_ORIF_EXPR)
7320         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7321
7322       overload = NULL;
7323       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7324          ERROR_MARK for everything that is not a binary expression.
7325          This makes warn_about_parentheses miss some warnings that
7326          involve unary operators.  For unary expressions we should
7327          pass the correct tree_code unless the unary expression was
7328          surrounded by parentheses.
7329       */
7330       if (no_toplevel_fold_p
7331           && lookahead_prec <= prec
7332           && sp == stack
7333           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7334         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7335       else
7336         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7337                                  &overload, tf_warning_or_error);
7338       lhs_type = tree_type;
7339
7340       /* If the binary operator required the use of an overloaded operator,
7341          then this expression cannot be an integral constant-expression.
7342          An overloaded operator can be used even if both operands are
7343          otherwise permissible in an integral constant-expression if at
7344          least one of the operands is of enumeration type.  */
7345
7346       if (overload
7347           && cp_parser_non_integral_constant_expression (parser,
7348                                                          NIC_OVERLOADED))
7349         return error_mark_node;
7350     }
7351
7352   return lhs;
7353 }
7354
7355
7356 /* Parse the `? expression : assignment-expression' part of a
7357    conditional-expression.  The LOGICAL_OR_EXPR is the
7358    logical-or-expression that started the conditional-expression.
7359    Returns a representation of the entire conditional-expression.
7360
7361    This routine is used by cp_parser_assignment_expression.
7362
7363      ? expression : assignment-expression
7364
7365    GNU Extensions:
7366
7367      ? : assignment-expression */
7368
7369 static tree
7370 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7371 {
7372   tree expr;
7373   tree assignment_expr;
7374   struct cp_token *token;
7375
7376   /* Consume the `?' token.  */
7377   cp_lexer_consume_token (parser->lexer);
7378   token = cp_lexer_peek_token (parser->lexer);
7379   if (cp_parser_allow_gnu_extensions_p (parser)
7380       && token->type == CPP_COLON)
7381     {
7382       pedwarn (token->location, OPT_pedantic, 
7383                "ISO C++ does not allow ?: with omitted middle operand");
7384       /* Implicit true clause.  */
7385       expr = NULL_TREE;
7386       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7387       warn_for_omitted_condop (token->location, logical_or_expr);
7388     }
7389   else
7390     {
7391       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7392       parser->colon_corrects_to_scope_p = false;
7393       /* Parse the expression.  */
7394       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7395       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7396       c_inhibit_evaluation_warnings +=
7397         ((logical_or_expr == truthvalue_true_node)
7398          - (logical_or_expr == truthvalue_false_node));
7399       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7400     }
7401
7402   /* The next token should be a `:'.  */
7403   cp_parser_require (parser, CPP_COLON, RT_COLON);
7404   /* Parse the assignment-expression.  */
7405   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7406   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7407
7408   /* Build the conditional-expression.  */
7409   return build_x_conditional_expr (logical_or_expr,
7410                                    expr,
7411                                    assignment_expr,
7412                                    tf_warning_or_error);
7413 }
7414
7415 /* Parse an assignment-expression.
7416
7417    assignment-expression:
7418      conditional-expression
7419      logical-or-expression assignment-operator assignment_expression
7420      throw-expression
7421
7422    CAST_P is true if this expression is the target of a cast.
7423
7424    Returns a representation for the expression.  */
7425
7426 static tree
7427 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7428                                  cp_id_kind * pidk)
7429 {
7430   tree expr;
7431
7432   /* If the next token is the `throw' keyword, then we're looking at
7433      a throw-expression.  */
7434   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7435     expr = cp_parser_throw_expression (parser);
7436   /* Otherwise, it must be that we are looking at a
7437      logical-or-expression.  */
7438   else
7439     {
7440       /* Parse the binary expressions (logical-or-expression).  */
7441       expr = cp_parser_binary_expression (parser, cast_p, false,
7442                                           PREC_NOT_OPERATOR, pidk);
7443       /* If the next token is a `?' then we're actually looking at a
7444          conditional-expression.  */
7445       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7446         return cp_parser_question_colon_clause (parser, expr);
7447       else
7448         {
7449           enum tree_code assignment_operator;
7450
7451           /* If it's an assignment-operator, we're using the second
7452              production.  */
7453           assignment_operator
7454             = cp_parser_assignment_operator_opt (parser);
7455           if (assignment_operator != ERROR_MARK)
7456             {
7457               bool non_constant_p;
7458
7459               /* Parse the right-hand side of the assignment.  */
7460               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7461
7462               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7463                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7464
7465               /* An assignment may not appear in a
7466                  constant-expression.  */
7467               if (cp_parser_non_integral_constant_expression (parser,
7468                                                               NIC_ASSIGNMENT))
7469                 return error_mark_node;
7470               /* Build the assignment expression.  */
7471               expr = build_x_modify_expr (expr,
7472                                           assignment_operator,
7473                                           rhs,
7474                                           tf_warning_or_error);
7475             }
7476         }
7477     }
7478
7479   return expr;
7480 }
7481
7482 /* Parse an (optional) assignment-operator.
7483
7484    assignment-operator: one of
7485      = *= /= %= += -= >>= <<= &= ^= |=
7486
7487    GNU Extension:
7488
7489    assignment-operator: one of
7490      <?= >?=
7491
7492    If the next token is an assignment operator, the corresponding tree
7493    code is returned, and the token is consumed.  For example, for
7494    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7495    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7496    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7497    operator, ERROR_MARK is returned.  */
7498
7499 static enum tree_code
7500 cp_parser_assignment_operator_opt (cp_parser* parser)
7501 {
7502   enum tree_code op;
7503   cp_token *token;
7504
7505   /* Peek at the next token.  */
7506   token = cp_lexer_peek_token (parser->lexer);
7507
7508   switch (token->type)
7509     {
7510     case CPP_EQ:
7511       op = NOP_EXPR;
7512       break;
7513
7514     case CPP_MULT_EQ:
7515       op = MULT_EXPR;
7516       break;
7517
7518     case CPP_DIV_EQ:
7519       op = TRUNC_DIV_EXPR;
7520       break;
7521
7522     case CPP_MOD_EQ:
7523       op = TRUNC_MOD_EXPR;
7524       break;
7525
7526     case CPP_PLUS_EQ:
7527       op = PLUS_EXPR;
7528       break;
7529
7530     case CPP_MINUS_EQ:
7531       op = MINUS_EXPR;
7532       break;
7533
7534     case CPP_RSHIFT_EQ:
7535       op = RSHIFT_EXPR;
7536       break;
7537
7538     case CPP_LSHIFT_EQ:
7539       op = LSHIFT_EXPR;
7540       break;
7541
7542     case CPP_AND_EQ:
7543       op = BIT_AND_EXPR;
7544       break;
7545
7546     case CPP_XOR_EQ:
7547       op = BIT_XOR_EXPR;
7548       break;
7549
7550     case CPP_OR_EQ:
7551       op = BIT_IOR_EXPR;
7552       break;
7553
7554     default:
7555       /* Nothing else is an assignment operator.  */
7556       op = ERROR_MARK;
7557     }
7558
7559   /* If it was an assignment operator, consume it.  */
7560   if (op != ERROR_MARK)
7561     cp_lexer_consume_token (parser->lexer);
7562
7563   return op;
7564 }
7565
7566 /* Parse an expression.
7567
7568    expression:
7569      assignment-expression
7570      expression , assignment-expression
7571
7572    CAST_P is true if this expression is the target of a cast.
7573
7574    Returns a representation of the expression.  */
7575
7576 static tree
7577 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7578 {
7579   tree expression = NULL_TREE;
7580
7581   while (true)
7582     {
7583       tree assignment_expression;
7584
7585       /* Parse the next assignment-expression.  */
7586       assignment_expression
7587         = cp_parser_assignment_expression (parser, cast_p, pidk);
7588       /* If this is the first assignment-expression, we can just
7589          save it away.  */
7590       if (!expression)
7591         expression = assignment_expression;
7592       else
7593         expression = build_x_compound_expr (expression,
7594                                             assignment_expression,
7595                                             tf_warning_or_error);
7596       /* If the next token is not a comma, then we are done with the
7597          expression.  */
7598       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7599         break;
7600       /* Consume the `,'.  */
7601       cp_lexer_consume_token (parser->lexer);
7602       /* A comma operator cannot appear in a constant-expression.  */
7603       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7604         expression = error_mark_node;
7605     }
7606
7607   return expression;
7608 }
7609
7610 /* Parse a constant-expression.
7611
7612    constant-expression:
7613      conditional-expression
7614
7615   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7616   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7617   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7618   is false, NON_CONSTANT_P should be NULL.  */
7619
7620 static tree
7621 cp_parser_constant_expression (cp_parser* parser,
7622                                bool allow_non_constant_p,
7623                                bool *non_constant_p)
7624 {
7625   bool saved_integral_constant_expression_p;
7626   bool saved_allow_non_integral_constant_expression_p;
7627   bool saved_non_integral_constant_expression_p;
7628   tree expression;
7629
7630   /* It might seem that we could simply parse the
7631      conditional-expression, and then check to see if it were
7632      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7633      one that the compiler can figure out is constant, possibly after
7634      doing some simplifications or optimizations.  The standard has a
7635      precise definition of constant-expression, and we must honor
7636      that, even though it is somewhat more restrictive.
7637
7638      For example:
7639
7640        int i[(2, 3)];
7641
7642      is not a legal declaration, because `(2, 3)' is not a
7643      constant-expression.  The `,' operator is forbidden in a
7644      constant-expression.  However, GCC's constant-folding machinery
7645      will fold this operation to an INTEGER_CST for `3'.  */
7646
7647   /* Save the old settings.  */
7648   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7649   saved_allow_non_integral_constant_expression_p
7650     = parser->allow_non_integral_constant_expression_p;
7651   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7652   /* We are now parsing a constant-expression.  */
7653   parser->integral_constant_expression_p = true;
7654   parser->allow_non_integral_constant_expression_p
7655     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7656   parser->non_integral_constant_expression_p = false;
7657   /* Although the grammar says "conditional-expression", we parse an
7658      "assignment-expression", which also permits "throw-expression"
7659      and the use of assignment operators.  In the case that
7660      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7661      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7662      actually essential that we look for an assignment-expression.
7663      For example, cp_parser_initializer_clauses uses this function to
7664      determine whether a particular assignment-expression is in fact
7665      constant.  */
7666   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7667   /* Restore the old settings.  */
7668   parser->integral_constant_expression_p
7669     = saved_integral_constant_expression_p;
7670   parser->allow_non_integral_constant_expression_p
7671     = saved_allow_non_integral_constant_expression_p;
7672   if (cxx_dialect >= cxx0x)
7673     {
7674       /* Require an rvalue constant expression here; that's what our
7675          callers expect.  Reference constant expressions are handled
7676          separately in e.g. cp_parser_template_argument.  */
7677       bool is_const = potential_rvalue_constant_expression (expression);
7678       parser->non_integral_constant_expression_p = !is_const;
7679       if (!is_const && !allow_non_constant_p)
7680         require_potential_rvalue_constant_expression (expression);
7681     }
7682   if (allow_non_constant_p)
7683     *non_constant_p = parser->non_integral_constant_expression_p;
7684   parser->non_integral_constant_expression_p
7685     = saved_non_integral_constant_expression_p;
7686
7687   return expression;
7688 }
7689
7690 /* Parse __builtin_offsetof.
7691
7692    offsetof-expression:
7693      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7694
7695    offsetof-member-designator:
7696      id-expression
7697      | offsetof-member-designator "." id-expression
7698      | offsetof-member-designator "[" expression "]"
7699      | offsetof-member-designator "->" id-expression  */
7700
7701 static tree
7702 cp_parser_builtin_offsetof (cp_parser *parser)
7703 {
7704   int save_ice_p, save_non_ice_p;
7705   tree type, expr;
7706   cp_id_kind dummy;
7707   cp_token *token;
7708
7709   /* We're about to accept non-integral-constant things, but will
7710      definitely yield an integral constant expression.  Save and
7711      restore these values around our local parsing.  */
7712   save_ice_p = parser->integral_constant_expression_p;
7713   save_non_ice_p = parser->non_integral_constant_expression_p;
7714
7715   /* Consume the "__builtin_offsetof" token.  */
7716   cp_lexer_consume_token (parser->lexer);
7717   /* Consume the opening `('.  */
7718   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7719   /* Parse the type-id.  */
7720   type = cp_parser_type_id (parser);
7721   /* Look for the `,'.  */
7722   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7723   token = cp_lexer_peek_token (parser->lexer);
7724
7725   /* Build the (type *)null that begins the traditional offsetof macro.  */
7726   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7727                             tf_warning_or_error);
7728
7729   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7730   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7731                                                  true, &dummy, token->location);
7732   while (true)
7733     {
7734       token = cp_lexer_peek_token (parser->lexer);
7735       switch (token->type)
7736         {
7737         case CPP_OPEN_SQUARE:
7738           /* offsetof-member-designator "[" expression "]" */
7739           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7740           break;
7741
7742         case CPP_DEREF:
7743           /* offsetof-member-designator "->" identifier */
7744           expr = grok_array_decl (expr, integer_zero_node);
7745           /* FALLTHRU */
7746
7747         case CPP_DOT:
7748           /* offsetof-member-designator "." identifier */
7749           cp_lexer_consume_token (parser->lexer);
7750           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7751                                                          expr, true, &dummy,
7752                                                          token->location);
7753           break;
7754
7755         case CPP_CLOSE_PAREN:
7756           /* Consume the ")" token.  */
7757           cp_lexer_consume_token (parser->lexer);
7758           goto success;
7759
7760         default:
7761           /* Error.  We know the following require will fail, but
7762              that gives the proper error message.  */
7763           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7764           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7765           expr = error_mark_node;
7766           goto failure;
7767         }
7768     }
7769
7770  success:
7771   /* If we're processing a template, we can't finish the semantics yet.
7772      Otherwise we can fold the entire expression now.  */
7773   if (processing_template_decl)
7774     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7775   else
7776     expr = finish_offsetof (expr);
7777
7778  failure:
7779   parser->integral_constant_expression_p = save_ice_p;
7780   parser->non_integral_constant_expression_p = save_non_ice_p;
7781
7782   return expr;
7783 }
7784
7785 /* Parse a trait expression.
7786
7787    Returns a representation of the expression, the underlying type
7788    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7789
7790 static tree
7791 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7792 {
7793   cp_trait_kind kind;
7794   tree type1, type2 = NULL_TREE;
7795   bool binary = false;
7796   cp_decl_specifier_seq decl_specs;
7797
7798   switch (keyword)
7799     {
7800     case RID_HAS_NOTHROW_ASSIGN:
7801       kind = CPTK_HAS_NOTHROW_ASSIGN;
7802       break;
7803     case RID_HAS_NOTHROW_CONSTRUCTOR:
7804       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7805       break;
7806     case RID_HAS_NOTHROW_COPY:
7807       kind = CPTK_HAS_NOTHROW_COPY;
7808       break;
7809     case RID_HAS_TRIVIAL_ASSIGN:
7810       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7811       break;
7812     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7813       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7814       break;
7815     case RID_HAS_TRIVIAL_COPY:
7816       kind = CPTK_HAS_TRIVIAL_COPY;
7817       break;
7818     case RID_HAS_TRIVIAL_DESTRUCTOR:
7819       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7820       break;
7821     case RID_HAS_VIRTUAL_DESTRUCTOR:
7822       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7823       break;
7824     case RID_IS_ABSTRACT:
7825       kind = CPTK_IS_ABSTRACT;
7826       break;
7827     case RID_IS_BASE_OF:
7828       kind = CPTK_IS_BASE_OF;
7829       binary = true;
7830       break;
7831     case RID_IS_CLASS:
7832       kind = CPTK_IS_CLASS;
7833       break;
7834     case RID_IS_CONVERTIBLE_TO:
7835       kind = CPTK_IS_CONVERTIBLE_TO;
7836       binary = true;
7837       break;
7838     case RID_IS_EMPTY:
7839       kind = CPTK_IS_EMPTY;
7840       break;
7841     case RID_IS_ENUM:
7842       kind = CPTK_IS_ENUM;
7843       break;
7844     case RID_IS_LITERAL_TYPE:
7845       kind = CPTK_IS_LITERAL_TYPE;
7846       break;
7847     case RID_IS_POD:
7848       kind = CPTK_IS_POD;
7849       break;
7850     case RID_IS_POLYMORPHIC:
7851       kind = CPTK_IS_POLYMORPHIC;
7852       break;
7853     case RID_IS_STD_LAYOUT:
7854       kind = CPTK_IS_STD_LAYOUT;
7855       break;
7856     case RID_IS_TRIVIAL:
7857       kind = CPTK_IS_TRIVIAL;
7858       break;
7859     case RID_IS_UNION:
7860       kind = CPTK_IS_UNION;
7861       break;
7862     case RID_UNDERLYING_TYPE:
7863       kind = CPTK_UNDERLYING_TYPE;
7864       break;
7865     case RID_BASES:
7866       kind = CPTK_BASES;
7867       break;
7868     case RID_DIRECT_BASES:
7869       kind = CPTK_DIRECT_BASES;
7870       break;
7871     default:
7872       gcc_unreachable ();
7873     }
7874
7875   /* Consume the token.  */
7876   cp_lexer_consume_token (parser->lexer);
7877
7878   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7879
7880   type1 = cp_parser_type_id (parser);
7881
7882   if (type1 == error_mark_node)
7883     return error_mark_node;
7884
7885   /* Build a trivial decl-specifier-seq.  */
7886   clear_decl_specs (&decl_specs);
7887   decl_specs.type = type1;
7888
7889   /* Call grokdeclarator to figure out what type this is.  */
7890   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7891                           /*initialized=*/0, /*attrlist=*/NULL);
7892
7893   if (binary)
7894     {
7895       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7896  
7897       type2 = cp_parser_type_id (parser);
7898
7899       if (type2 == error_mark_node)
7900         return error_mark_node;
7901
7902       /* Build a trivial decl-specifier-seq.  */
7903       clear_decl_specs (&decl_specs);
7904       decl_specs.type = type2;
7905
7906       /* Call grokdeclarator to figure out what type this is.  */
7907       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7908                               /*initialized=*/0, /*attrlist=*/NULL);
7909     }
7910
7911   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7912
7913   /* Complete the trait expression, which may mean either processing
7914      the trait expr now or saving it for template instantiation.  */
7915   switch(kind)
7916     {
7917     case CPTK_UNDERLYING_TYPE:
7918       return finish_underlying_type (type1);
7919     case CPTK_BASES:
7920       return finish_bases (type1, false);
7921     case CPTK_DIRECT_BASES:
7922       return finish_bases (type1, true);
7923     default:
7924       return finish_trait_expr (kind, type1, type2);
7925     }
7926 }
7927
7928 /* Lambdas that appear in variable initializer or default argument scope
7929    get that in their mangling, so we need to record it.  We might as well
7930    use the count for function and namespace scopes as well.  */
7931 static GTY(()) tree lambda_scope;
7932 static GTY(()) int lambda_count;
7933 typedef struct GTY(()) tree_int
7934 {
7935   tree t;
7936   int i;
7937 } tree_int;
7938 DEF_VEC_O(tree_int);
7939 DEF_VEC_ALLOC_O(tree_int,gc);
7940 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7941
7942 static void
7943 start_lambda_scope (tree decl)
7944 {
7945   tree_int ti;
7946   gcc_assert (decl);
7947   /* Once we're inside a function, we ignore other scopes and just push
7948      the function again so that popping works properly.  */
7949   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7950     decl = current_function_decl;
7951   ti.t = lambda_scope;
7952   ti.i = lambda_count;
7953   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7954   if (lambda_scope != decl)
7955     {
7956       /* Don't reset the count if we're still in the same function.  */
7957       lambda_scope = decl;
7958       lambda_count = 0;
7959     }
7960 }
7961
7962 static void
7963 record_lambda_scope (tree lambda)
7964 {
7965   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7966   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7967 }
7968
7969 static void
7970 finish_lambda_scope (void)
7971 {
7972   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7973   if (lambda_scope != p->t)
7974     {
7975       lambda_scope = p->t;
7976       lambda_count = p->i;
7977     }
7978   VEC_pop (tree_int, lambda_scope_stack);
7979 }
7980
7981 /* Parse a lambda expression.
7982
7983    lambda-expression:
7984      lambda-introducer lambda-declarator [opt] compound-statement
7985
7986    Returns a representation of the expression.  */
7987
7988 static tree
7989 cp_parser_lambda_expression (cp_parser* parser)
7990 {
7991   tree lambda_expr = build_lambda_expr ();
7992   tree type;
7993   bool ok;
7994
7995   LAMBDA_EXPR_LOCATION (lambda_expr)
7996     = cp_lexer_peek_token (parser->lexer)->location;
7997
7998   if (cp_unevaluated_operand)
7999     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
8000               "lambda-expression in unevaluated context");
8001
8002   /* We may be in the middle of deferred access check.  Disable
8003      it now.  */
8004   push_deferring_access_checks (dk_no_deferred);
8005
8006   cp_parser_lambda_introducer (parser, lambda_expr);
8007
8008   type = begin_lambda_type (lambda_expr);
8009
8010   record_lambda_scope (lambda_expr);
8011
8012   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
8013   determine_visibility (TYPE_NAME (type));
8014
8015   /* Now that we've started the type, add the capture fields for any
8016      explicit captures.  */
8017   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
8018
8019   {
8020     /* Inside the class, surrounding template-parameter-lists do not apply.  */
8021     unsigned int saved_num_template_parameter_lists
8022         = parser->num_template_parameter_lists;
8023     unsigned char in_statement = parser->in_statement;
8024     bool in_switch_statement_p = parser->in_switch_statement_p;
8025
8026     parser->num_template_parameter_lists = 0;
8027     parser->in_statement = 0;
8028     parser->in_switch_statement_p = false;
8029
8030     /* By virtue of defining a local class, a lambda expression has access to
8031        the private variables of enclosing classes.  */
8032
8033     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8034
8035     if (ok)
8036       cp_parser_lambda_body (parser, lambda_expr);
8037     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8038       cp_parser_skip_to_end_of_block_or_statement (parser);
8039
8040     /* The capture list was built up in reverse order; fix that now.  */
8041     {
8042       tree newlist = NULL_TREE;
8043       tree elt, next;
8044
8045       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8046            elt; elt = next)
8047         {
8048           next = TREE_CHAIN (elt);
8049           TREE_CHAIN (elt) = newlist;
8050           newlist = elt;
8051         }
8052       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8053     }
8054
8055     if (ok)
8056       maybe_add_lambda_conv_op (type);
8057
8058     type = finish_struct (type, /*attributes=*/NULL_TREE);
8059
8060     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8061     parser->in_statement = in_statement;
8062     parser->in_switch_statement_p = in_switch_statement_p;
8063   }
8064
8065   pop_deferring_access_checks ();
8066
8067   /* This field is only used during parsing of the lambda.  */
8068   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8069
8070   /* This lambda shouldn't have any proxies left at this point.  */
8071   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8072   /* And now that we're done, push proxies for an enclosing lambda.  */
8073   insert_pending_capture_proxies ();
8074
8075   if (ok)
8076     return build_lambda_object (lambda_expr);
8077   else
8078     return error_mark_node;
8079 }
8080
8081 /* Parse the beginning of a lambda expression.
8082
8083    lambda-introducer:
8084      [ lambda-capture [opt] ]
8085
8086    LAMBDA_EXPR is the current representation of the lambda expression.  */
8087
8088 static void
8089 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8090 {
8091   /* Need commas after the first capture.  */
8092   bool first = true;
8093
8094   /* Eat the leading `['.  */
8095   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8096
8097   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8098   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8099       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8100     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8101   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8102     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8103
8104   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8105     {
8106       cp_lexer_consume_token (parser->lexer);
8107       first = false;
8108     }
8109
8110   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8111     {
8112       cp_token* capture_token;
8113       tree capture_id;
8114       tree capture_init_expr;
8115       cp_id_kind idk = CP_ID_KIND_NONE;
8116       bool explicit_init_p = false;
8117
8118       enum capture_kind_type
8119       {
8120         BY_COPY,
8121         BY_REFERENCE
8122       };
8123       enum capture_kind_type capture_kind = BY_COPY;
8124
8125       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8126         {
8127           error ("expected end of capture-list");
8128           return;
8129         }
8130
8131       if (first)
8132         first = false;
8133       else
8134         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8135
8136       /* Possibly capture `this'.  */
8137       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8138         {
8139           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8140           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8141             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8142                      "with by-copy capture default");
8143           cp_lexer_consume_token (parser->lexer);
8144           add_capture (lambda_expr,
8145                        /*id=*/this_identifier,
8146                        /*initializer=*/finish_this_expr(),
8147                        /*by_reference_p=*/false,
8148                        explicit_init_p);
8149           continue;
8150         }
8151
8152       /* Remember whether we want to capture as a reference or not.  */
8153       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8154         {
8155           capture_kind = BY_REFERENCE;
8156           cp_lexer_consume_token (parser->lexer);
8157         }
8158
8159       /* Get the identifier.  */
8160       capture_token = cp_lexer_peek_token (parser->lexer);
8161       capture_id = cp_parser_identifier (parser);
8162
8163       if (capture_id == error_mark_node)
8164         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8165            delimiters, but I modified this to stop on unnested ']' as well.  It
8166            was already changed to stop on unnested '}', so the
8167            "closing_parenthesis" name is no more misleading with my change.  */
8168         {
8169           cp_parser_skip_to_closing_parenthesis (parser,
8170                                                  /*recovering=*/true,
8171                                                  /*or_comma=*/true,
8172                                                  /*consume_paren=*/true);
8173           break;
8174         }
8175
8176       /* Find the initializer for this capture.  */
8177       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8178         {
8179           /* An explicit expression exists.  */
8180           cp_lexer_consume_token (parser->lexer);
8181           pedwarn (input_location, OPT_pedantic,
8182                    "ISO C++ does not allow initializers "
8183                    "in lambda expression capture lists");
8184           capture_init_expr = cp_parser_assignment_expression (parser,
8185                                                                /*cast_p=*/true,
8186                                                                &idk);
8187           explicit_init_p = true;
8188         }
8189       else
8190         {
8191           const char* error_msg;
8192
8193           /* Turn the identifier into an id-expression.  */
8194           capture_init_expr
8195             = cp_parser_lookup_name
8196                 (parser,
8197                  capture_id,
8198                  none_type,
8199                  /*is_template=*/false,
8200                  /*is_namespace=*/false,
8201                  /*check_dependency=*/true,
8202                  /*ambiguous_decls=*/NULL,
8203                  capture_token->location);
8204
8205           if (capture_init_expr == error_mark_node)
8206             {
8207               unqualified_name_lookup_error (capture_id);
8208               continue;
8209             }
8210           else if (DECL_P (capture_init_expr)
8211                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8212                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8213             {
8214               error_at (capture_token->location,
8215                         "capture of non-variable %qD ",
8216                         capture_init_expr);
8217               inform (0, "%q+#D declared here", capture_init_expr);
8218               continue;
8219             }
8220           if (TREE_CODE (capture_init_expr) == VAR_DECL
8221               && decl_storage_duration (capture_init_expr) != dk_auto)
8222             {
8223               pedwarn (capture_token->location, 0, "capture of variable "
8224                        "%qD with non-automatic storage duration",
8225                        capture_init_expr);
8226               inform (0, "%q+#D declared here", capture_init_expr);
8227               continue;
8228             }
8229
8230           capture_init_expr
8231             = finish_id_expression
8232                 (capture_id,
8233                  capture_init_expr,
8234                  parser->scope,
8235                  &idk,
8236                  /*integral_constant_expression_p=*/false,
8237                  /*allow_non_integral_constant_expression_p=*/false,
8238                  /*non_integral_constant_expression_p=*/NULL,
8239                  /*template_p=*/false,
8240                  /*done=*/true,
8241                  /*address_p=*/false,
8242                  /*template_arg_p=*/false,
8243                  &error_msg,
8244                  capture_token->location);
8245         }
8246
8247       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8248           && !explicit_init_p)
8249         {
8250           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8251               && capture_kind == BY_COPY)
8252             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8253                      "of %qD redundant with by-copy capture default",
8254                      capture_id);
8255           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8256               && capture_kind == BY_REFERENCE)
8257             pedwarn (capture_token->location, 0, "explicit by-reference "
8258                      "capture of %qD redundant with by-reference capture "
8259                      "default", capture_id);
8260         }
8261
8262       add_capture (lambda_expr,
8263                    capture_id,
8264                    capture_init_expr,
8265                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8266                    explicit_init_p);
8267     }
8268
8269   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8270 }
8271
8272 /* Parse the (optional) middle of a lambda expression.
8273
8274    lambda-declarator:
8275      ( parameter-declaration-clause [opt] )
8276        attribute-specifier [opt]
8277        mutable [opt]
8278        exception-specification [opt]
8279        lambda-return-type-clause [opt]
8280
8281    LAMBDA_EXPR is the current representation of the lambda expression.  */
8282
8283 static bool
8284 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8285 {
8286   /* 5.1.1.4 of the standard says:
8287        If a lambda-expression does not include a lambda-declarator, it is as if
8288        the lambda-declarator were ().
8289      This means an empty parameter list, no attributes, and no exception
8290      specification.  */
8291   tree param_list = void_list_node;
8292   tree attributes = NULL_TREE;
8293   tree exception_spec = NULL_TREE;
8294   tree t;
8295
8296   /* The lambda-declarator is optional, but must begin with an opening
8297      parenthesis if present.  */
8298   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8299     {
8300       cp_lexer_consume_token (parser->lexer);
8301
8302       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8303
8304       /* Parse parameters.  */
8305       param_list = cp_parser_parameter_declaration_clause (parser);
8306
8307       /* Default arguments shall not be specified in the
8308          parameter-declaration-clause of a lambda-declarator.  */
8309       for (t = param_list; t; t = TREE_CHAIN (t))
8310         if (TREE_PURPOSE (t))
8311           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8312                    "default argument specified for lambda parameter");
8313
8314       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8315
8316       attributes = cp_parser_attributes_opt (parser);
8317
8318       /* Parse optional `mutable' keyword.  */
8319       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8320         {
8321           cp_lexer_consume_token (parser->lexer);
8322           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8323         }
8324
8325       /* Parse optional exception specification.  */
8326       exception_spec = cp_parser_exception_specification_opt (parser);
8327
8328       /* Parse optional trailing return type.  */
8329       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8330         {
8331           cp_lexer_consume_token (parser->lexer);
8332           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8333         }
8334
8335       /* The function parameters must be in scope all the way until after the
8336          trailing-return-type in case of decltype.  */
8337       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8338         pop_binding (DECL_NAME (t), t);
8339
8340       leave_scope ();
8341     }
8342
8343   /* Create the function call operator.
8344
8345      Messing with declarators like this is no uglier than building up the
8346      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8347      other code.  */
8348   {
8349     cp_decl_specifier_seq return_type_specs;
8350     cp_declarator* declarator;
8351     tree fco;
8352     int quals;
8353     void *p;
8354
8355     clear_decl_specs (&return_type_specs);
8356     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8357       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8358     else
8359       /* Maybe we will deduce the return type later, but we can use void
8360          as a placeholder return type anyways.  */
8361       return_type_specs.type = void_type_node;
8362
8363     p = obstack_alloc (&declarator_obstack, 0);
8364
8365     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8366                                      sfk_none);
8367
8368     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8369              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8370     declarator = make_call_declarator (declarator, param_list, quals,
8371                                        VIRT_SPEC_UNSPECIFIED,
8372                                        exception_spec,
8373                                        /*late_return_type=*/NULL_TREE);
8374     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8375
8376     fco = grokmethod (&return_type_specs,
8377                       declarator,
8378                       attributes);
8379     if (fco != error_mark_node)
8380       {
8381         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8382         DECL_ARTIFICIAL (fco) = 1;
8383         /* Give the object parameter a different name.  */
8384         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8385       }
8386
8387     finish_member_declaration (fco);
8388
8389     obstack_free (&declarator_obstack, p);
8390
8391     return (fco != error_mark_node);
8392   }
8393 }
8394
8395 /* Parse the body of a lambda expression, which is simply
8396
8397    compound-statement
8398
8399    but which requires special handling.
8400    LAMBDA_EXPR is the current representation of the lambda expression.  */
8401
8402 static void
8403 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8404 {
8405   bool nested = (current_function_decl != NULL_TREE);
8406   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8407   if (nested)
8408     push_function_context ();
8409   else
8410     /* Still increment function_depth so that we don't GC in the
8411        middle of an expression.  */
8412     ++function_depth;
8413   /* Clear this in case we're in the middle of a default argument.  */
8414   parser->local_variables_forbidden_p = false;
8415
8416   /* Finish the function call operator
8417      - class_specifier
8418      + late_parsing_for_member
8419      + function_definition_after_declarator
8420      + ctor_initializer_opt_and_function_body  */
8421   {
8422     tree fco = lambda_function (lambda_expr);
8423     tree body;
8424     bool done = false;
8425     tree compound_stmt;
8426     tree cap;
8427
8428     /* Let the front end know that we are going to be defining this
8429        function.  */
8430     start_preparsed_function (fco,
8431                               NULL_TREE,
8432                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8433
8434     start_lambda_scope (fco);
8435     body = begin_function_body ();
8436
8437     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8438       goto out;
8439
8440     /* Push the proxies for any explicit captures.  */
8441     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8442          cap = TREE_CHAIN (cap))
8443       build_capture_proxy (TREE_PURPOSE (cap));
8444
8445     compound_stmt = begin_compound_stmt (0);
8446
8447     /* 5.1.1.4 of the standard says:
8448          If a lambda-expression does not include a trailing-return-type, it
8449          is as if the trailing-return-type denotes the following type:
8450           * if the compound-statement is of the form
8451                { return attribute-specifier [opt] expression ; }
8452              the type of the returned expression after lvalue-to-rvalue
8453              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8454              (_conv.array_ 4.2), and function-to-pointer conversion
8455              (_conv.func_ 4.3);
8456           * otherwise, void.  */
8457
8458     /* In a lambda that has neither a lambda-return-type-clause
8459        nor a deducible form, errors should be reported for return statements
8460        in the body.  Since we used void as the placeholder return type, parsing
8461        the body as usual will give such desired behavior.  */
8462     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8463         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8464         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8465       {
8466         tree expr = NULL_TREE;
8467         cp_id_kind idk = CP_ID_KIND_NONE;
8468
8469         /* Parse tentatively in case there's more after the initial return
8470            statement.  */
8471         cp_parser_parse_tentatively (parser);
8472
8473         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8474
8475         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8476
8477         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8478         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8479
8480         if (cp_parser_parse_definitely (parser))
8481           {
8482             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8483
8484             /* Will get error here if type not deduced yet.  */
8485             finish_return_stmt (expr);
8486
8487             done = true;
8488           }
8489       }
8490
8491     if (!done)
8492       {
8493         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8494           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8495         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8496           cp_parser_label_declaration (parser);
8497         cp_parser_statement_seq_opt (parser, NULL_TREE);
8498         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8499         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8500       }
8501
8502     finish_compound_stmt (compound_stmt);
8503
8504   out:
8505     finish_function_body (body);
8506     finish_lambda_scope ();
8507
8508     /* Finish the function and generate code for it if necessary.  */
8509     expand_or_defer_fn (finish_function (/*inline*/2));
8510   }
8511
8512   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8513   if (nested)
8514     pop_function_context();
8515   else
8516     --function_depth;
8517 }
8518
8519 /* Statements [gram.stmt.stmt]  */
8520
8521 /* Parse a statement.
8522
8523    statement:
8524      labeled-statement
8525      expression-statement
8526      compound-statement
8527      selection-statement
8528      iteration-statement
8529      jump-statement
8530      declaration-statement
8531      try-block
8532
8533   TM Extension:
8534
8535    statement:
8536      atomic-statement
8537
8538   IN_COMPOUND is true when the statement is nested inside a
8539   cp_parser_compound_statement; this matters for certain pragmas.
8540
8541   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8542   is a (possibly labeled) if statement which is not enclosed in braces
8543   and has an else clause.  This is used to implement -Wparentheses.  */
8544
8545 static void
8546 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8547                      bool in_compound, bool *if_p)
8548 {
8549   tree statement;
8550   cp_token *token;
8551   location_t statement_location;
8552
8553  restart:
8554   if (if_p != NULL)
8555     *if_p = false;
8556   /* There is no statement yet.  */
8557   statement = NULL_TREE;
8558   /* Peek at the next token.  */
8559   token = cp_lexer_peek_token (parser->lexer);
8560   /* Remember the location of the first token in the statement.  */
8561   statement_location = token->location;
8562   /* If this is a keyword, then that will often determine what kind of
8563      statement we have.  */
8564   if (token->type == CPP_KEYWORD)
8565     {
8566       enum rid keyword = token->keyword;
8567
8568       switch (keyword)
8569         {
8570         case RID_CASE:
8571         case RID_DEFAULT:
8572           /* Looks like a labeled-statement with a case label.
8573              Parse the label, and then use tail recursion to parse
8574              the statement.  */
8575           cp_parser_label_for_labeled_statement (parser);
8576           goto restart;
8577
8578         case RID_IF:
8579         case RID_SWITCH:
8580           statement = cp_parser_selection_statement (parser, if_p);
8581           break;
8582
8583         case RID_WHILE:
8584         case RID_DO:
8585         case RID_FOR:
8586           statement = cp_parser_iteration_statement (parser);
8587           break;
8588
8589         case RID_BREAK:
8590         case RID_CONTINUE:
8591         case RID_RETURN:
8592         case RID_GOTO:
8593           statement = cp_parser_jump_statement (parser);
8594           break;
8595
8596           /* Objective-C++ exception-handling constructs.  */
8597         case RID_AT_TRY:
8598         case RID_AT_CATCH:
8599         case RID_AT_FINALLY:
8600         case RID_AT_SYNCHRONIZED:
8601         case RID_AT_THROW:
8602           statement = cp_parser_objc_statement (parser);
8603           break;
8604
8605         case RID_TRY:
8606           statement = cp_parser_try_block (parser);
8607           break;
8608
8609         case RID_NAMESPACE:
8610           /* This must be a namespace alias definition.  */
8611           cp_parser_declaration_statement (parser);
8612           return;
8613           
8614         case RID_TRANSACTION_ATOMIC:
8615         case RID_TRANSACTION_RELAXED:
8616           statement = cp_parser_transaction (parser, keyword);
8617           break;
8618         case RID_TRANSACTION_CANCEL:
8619           statement = cp_parser_transaction_cancel (parser);
8620           break;
8621
8622         default:
8623           /* It might be a keyword like `int' that can start a
8624              declaration-statement.  */
8625           break;
8626         }
8627     }
8628   else if (token->type == CPP_NAME)
8629     {
8630       /* If the next token is a `:', then we are looking at a
8631          labeled-statement.  */
8632       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8633       if (token->type == CPP_COLON)
8634         {
8635           /* Looks like a labeled-statement with an ordinary label.
8636              Parse the label, and then use tail recursion to parse
8637              the statement.  */
8638           cp_parser_label_for_labeled_statement (parser);
8639           goto restart;
8640         }
8641     }
8642   /* Anything that starts with a `{' must be a compound-statement.  */
8643   else if (token->type == CPP_OPEN_BRACE)
8644     statement = cp_parser_compound_statement (parser, NULL, false, false);
8645   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8646      a statement all its own.  */
8647   else if (token->type == CPP_PRAGMA)
8648     {
8649       /* Only certain OpenMP pragmas are attached to statements, and thus
8650          are considered statements themselves.  All others are not.  In
8651          the context of a compound, accept the pragma as a "statement" and
8652          return so that we can check for a close brace.  Otherwise we
8653          require a real statement and must go back and read one.  */
8654       if (in_compound)
8655         cp_parser_pragma (parser, pragma_compound);
8656       else if (!cp_parser_pragma (parser, pragma_stmt))
8657         goto restart;
8658       return;
8659     }
8660   else if (token->type == CPP_EOF)
8661     {
8662       cp_parser_error (parser, "expected statement");
8663       return;
8664     }
8665
8666   /* Everything else must be a declaration-statement or an
8667      expression-statement.  Try for the declaration-statement
8668      first, unless we are looking at a `;', in which case we know that
8669      we have an expression-statement.  */
8670   if (!statement)
8671     {
8672       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8673         {
8674           cp_parser_parse_tentatively (parser);
8675           /* Try to parse the declaration-statement.  */
8676           cp_parser_declaration_statement (parser);
8677           /* If that worked, we're done.  */
8678           if (cp_parser_parse_definitely (parser))
8679             return;
8680         }
8681       /* Look for an expression-statement instead.  */
8682       statement = cp_parser_expression_statement (parser, in_statement_expr);
8683     }
8684
8685   /* Set the line number for the statement.  */
8686   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8687     SET_EXPR_LOCATION (statement, statement_location);
8688 }
8689
8690 /* Parse the label for a labeled-statement, i.e.
8691
8692    identifier :
8693    case constant-expression :
8694    default :
8695
8696    GNU Extension:
8697    case constant-expression ... constant-expression : statement
8698
8699    When a label is parsed without errors, the label is added to the
8700    parse tree by the finish_* functions, so this function doesn't
8701    have to return the label.  */
8702
8703 static void
8704 cp_parser_label_for_labeled_statement (cp_parser* parser)
8705 {
8706   cp_token *token;
8707   tree label = NULL_TREE;
8708   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8709
8710   /* The next token should be an identifier.  */
8711   token = cp_lexer_peek_token (parser->lexer);
8712   if (token->type != CPP_NAME
8713       && token->type != CPP_KEYWORD)
8714     {
8715       cp_parser_error (parser, "expected labeled-statement");
8716       return;
8717     }
8718
8719   parser->colon_corrects_to_scope_p = false;
8720   switch (token->keyword)
8721     {
8722     case RID_CASE:
8723       {
8724         tree expr, expr_hi;
8725         cp_token *ellipsis;
8726
8727         /* Consume the `case' token.  */
8728         cp_lexer_consume_token (parser->lexer);
8729         /* Parse the constant-expression.  */
8730         expr = cp_parser_constant_expression (parser,
8731                                               /*allow_non_constant_p=*/false,
8732                                               NULL);
8733
8734         ellipsis = cp_lexer_peek_token (parser->lexer);
8735         if (ellipsis->type == CPP_ELLIPSIS)
8736           {
8737             /* Consume the `...' token.  */
8738             cp_lexer_consume_token (parser->lexer);
8739             expr_hi =
8740               cp_parser_constant_expression (parser,
8741                                              /*allow_non_constant_p=*/false,
8742                                              NULL);
8743             /* We don't need to emit warnings here, as the common code
8744                will do this for us.  */
8745           }
8746         else
8747           expr_hi = NULL_TREE;
8748
8749         if (parser->in_switch_statement_p)
8750           finish_case_label (token->location, expr, expr_hi);
8751         else
8752           error_at (token->location,
8753                     "case label %qE not within a switch statement",
8754                     expr);
8755       }
8756       break;
8757
8758     case RID_DEFAULT:
8759       /* Consume the `default' token.  */
8760       cp_lexer_consume_token (parser->lexer);
8761
8762       if (parser->in_switch_statement_p)
8763         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8764       else
8765         error_at (token->location, "case label not within a switch statement");
8766       break;
8767
8768     default:
8769       /* Anything else must be an ordinary label.  */
8770       label = finish_label_stmt (cp_parser_identifier (parser));
8771       break;
8772     }
8773
8774   /* Require the `:' token.  */
8775   cp_parser_require (parser, CPP_COLON, RT_COLON);
8776
8777   /* An ordinary label may optionally be followed by attributes.
8778      However, this is only permitted if the attributes are then
8779      followed by a semicolon.  This is because, for backward
8780      compatibility, when parsing
8781        lab: __attribute__ ((unused)) int i;
8782      we want the attribute to attach to "i", not "lab".  */
8783   if (label != NULL_TREE
8784       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8785     {
8786       tree attrs;
8787
8788       cp_parser_parse_tentatively (parser);
8789       attrs = cp_parser_attributes_opt (parser);
8790       if (attrs == NULL_TREE
8791           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8792         cp_parser_abort_tentative_parse (parser);
8793       else if (!cp_parser_parse_definitely (parser))
8794         ;
8795       else
8796         cplus_decl_attributes (&label, attrs, 0);
8797     }
8798
8799   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8800 }
8801
8802 /* Parse an expression-statement.
8803
8804    expression-statement:
8805      expression [opt] ;
8806
8807    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8808    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8809    indicates whether this expression-statement is part of an
8810    expression statement.  */
8811
8812 static tree
8813 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8814 {
8815   tree statement = NULL_TREE;
8816   cp_token *token = cp_lexer_peek_token (parser->lexer);
8817
8818   /* If the next token is a ';', then there is no expression
8819      statement.  */
8820   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8821     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8822
8823   /* Give a helpful message for "A<T>::type t;" and the like.  */
8824   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8825       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8826     {
8827       if (TREE_CODE (statement) == SCOPE_REF)
8828         error_at (token->location, "need %<typename%> before %qE because "
8829                   "%qT is a dependent scope",
8830                   statement, TREE_OPERAND (statement, 0));
8831       else if (is_overloaded_fn (statement)
8832                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8833         {
8834           /* A::A a; */
8835           tree fn = get_first_fn (statement);
8836           error_at (token->location,
8837                     "%<%T::%D%> names the constructor, not the type",
8838                     DECL_CONTEXT (fn), DECL_NAME (fn));
8839         }
8840     }
8841
8842   /* Consume the final `;'.  */
8843   cp_parser_consume_semicolon_at_end_of_statement (parser);
8844
8845   if (in_statement_expr
8846       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8847     /* This is the final expression statement of a statement
8848        expression.  */
8849     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8850   else if (statement)
8851     statement = finish_expr_stmt (statement);
8852   else
8853     finish_stmt ();
8854
8855   return statement;
8856 }
8857
8858 /* Parse a compound-statement.
8859
8860    compound-statement:
8861      { statement-seq [opt] }
8862
8863    GNU extension:
8864
8865    compound-statement:
8866      { label-declaration-seq [opt] statement-seq [opt] }
8867
8868    label-declaration-seq:
8869      label-declaration
8870      label-declaration-seq label-declaration
8871
8872    Returns a tree representing the statement.  */
8873
8874 static tree
8875 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8876                               bool in_try, bool function_body)
8877 {
8878   tree compound_stmt;
8879
8880   /* Consume the `{'.  */
8881   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8882     return error_mark_node;
8883   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8884       && !function_body)
8885     pedwarn (input_location, OPT_pedantic,
8886              "compound-statement in constexpr function");
8887   /* Begin the compound-statement.  */
8888   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8889   /* If the next keyword is `__label__' we have a label declaration.  */
8890   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8891     cp_parser_label_declaration (parser);
8892   /* Parse an (optional) statement-seq.  */
8893   cp_parser_statement_seq_opt (parser, in_statement_expr);
8894   /* Finish the compound-statement.  */
8895   finish_compound_stmt (compound_stmt);
8896   /* Consume the `}'.  */
8897   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8898
8899   return compound_stmt;
8900 }
8901
8902 /* Parse an (optional) statement-seq.
8903
8904    statement-seq:
8905      statement
8906      statement-seq [opt] statement  */
8907
8908 static void
8909 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8910 {
8911   /* Scan statements until there aren't any more.  */
8912   while (true)
8913     {
8914       cp_token *token = cp_lexer_peek_token (parser->lexer);
8915
8916       /* If we are looking at a `}', then we have run out of
8917          statements; the same is true if we have reached the end
8918          of file, or have stumbled upon a stray '@end'.  */
8919       if (token->type == CPP_CLOSE_BRACE
8920           || token->type == CPP_EOF
8921           || token->type == CPP_PRAGMA_EOL
8922           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8923         break;
8924       
8925       /* If we are in a compound statement and find 'else' then
8926          something went wrong.  */
8927       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8928         {
8929           if (parser->in_statement & IN_IF_STMT) 
8930             break;
8931           else
8932             {
8933               token = cp_lexer_consume_token (parser->lexer);
8934               error_at (token->location, "%<else%> without a previous %<if%>");
8935             }
8936         }
8937
8938       /* Parse the statement.  */
8939       cp_parser_statement (parser, in_statement_expr, true, NULL);
8940     }
8941 }
8942
8943 /* Parse a selection-statement.
8944
8945    selection-statement:
8946      if ( condition ) statement
8947      if ( condition ) statement else statement
8948      switch ( condition ) statement
8949
8950    Returns the new IF_STMT or SWITCH_STMT.
8951
8952    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8953    is a (possibly labeled) if statement which is not enclosed in
8954    braces and has an else clause.  This is used to implement
8955    -Wparentheses.  */
8956
8957 static tree
8958 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8959 {
8960   cp_token *token;
8961   enum rid keyword;
8962
8963   if (if_p != NULL)
8964     *if_p = false;
8965
8966   /* Peek at the next token.  */
8967   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8968
8969   /* See what kind of keyword it is.  */
8970   keyword = token->keyword;
8971   switch (keyword)
8972     {
8973     case RID_IF:
8974     case RID_SWITCH:
8975       {
8976         tree statement;
8977         tree condition;
8978
8979         /* Look for the `('.  */
8980         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8981           {
8982             cp_parser_skip_to_end_of_statement (parser);
8983             return error_mark_node;
8984           }
8985
8986         /* Begin the selection-statement.  */
8987         if (keyword == RID_IF)
8988           statement = begin_if_stmt ();
8989         else
8990           statement = begin_switch_stmt ();
8991
8992         /* Parse the condition.  */
8993         condition = cp_parser_condition (parser);
8994         /* Look for the `)'.  */
8995         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8996           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8997                                                  /*consume_paren=*/true);
8998
8999         if (keyword == RID_IF)
9000           {
9001             bool nested_if;
9002             unsigned char in_statement;
9003
9004             /* Add the condition.  */
9005             finish_if_stmt_cond (condition, statement);
9006
9007             /* Parse the then-clause.  */
9008             in_statement = parser->in_statement;
9009             parser->in_statement |= IN_IF_STMT;
9010             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9011               {
9012                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9013                 add_stmt (build_empty_stmt (loc));
9014                 cp_lexer_consume_token (parser->lexer);
9015                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
9016                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
9017                               "empty body in an %<if%> statement");
9018                 nested_if = false;
9019               }
9020             else
9021               cp_parser_implicitly_scoped_statement (parser, &nested_if);
9022             parser->in_statement = in_statement;
9023
9024             finish_then_clause (statement);
9025
9026             /* If the next token is `else', parse the else-clause.  */
9027             if (cp_lexer_next_token_is_keyword (parser->lexer,
9028                                                 RID_ELSE))
9029               {
9030                 /* Consume the `else' keyword.  */
9031                 cp_lexer_consume_token (parser->lexer);
9032                 begin_else_clause (statement);
9033                 /* Parse the else-clause.  */
9034                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9035                   {
9036                     location_t loc;
9037                     loc = cp_lexer_peek_token (parser->lexer)->location;
9038                     warning_at (loc,
9039                                 OPT_Wempty_body, "suggest braces around "
9040                                 "empty body in an %<else%> statement");
9041                     add_stmt (build_empty_stmt (loc));
9042                     cp_lexer_consume_token (parser->lexer);
9043                   }
9044                 else
9045                   cp_parser_implicitly_scoped_statement (parser, NULL);
9046
9047                 finish_else_clause (statement);
9048
9049                 /* If we are currently parsing a then-clause, then
9050                    IF_P will not be NULL.  We set it to true to
9051                    indicate that this if statement has an else clause.
9052                    This may trigger the Wparentheses warning below
9053                    when we get back up to the parent if statement.  */
9054                 if (if_p != NULL)
9055                   *if_p = true;
9056               }
9057             else
9058               {
9059                 /* This if statement does not have an else clause.  If
9060                    NESTED_IF is true, then the then-clause is an if
9061                    statement which does have an else clause.  We warn
9062                    about the potential ambiguity.  */
9063                 if (nested_if)
9064                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9065                               "suggest explicit braces to avoid ambiguous"
9066                               " %<else%>");
9067               }
9068
9069             /* Now we're all done with the if-statement.  */
9070             finish_if_stmt (statement);
9071           }
9072         else
9073           {
9074             bool in_switch_statement_p;
9075             unsigned char in_statement;
9076
9077             /* Add the condition.  */
9078             finish_switch_cond (condition, statement);
9079
9080             /* Parse the body of the switch-statement.  */
9081             in_switch_statement_p = parser->in_switch_statement_p;
9082             in_statement = parser->in_statement;
9083             parser->in_switch_statement_p = true;
9084             parser->in_statement |= IN_SWITCH_STMT;
9085             cp_parser_implicitly_scoped_statement (parser, NULL);
9086             parser->in_switch_statement_p = in_switch_statement_p;
9087             parser->in_statement = in_statement;
9088
9089             /* Now we're all done with the switch-statement.  */
9090             finish_switch_stmt (statement);
9091           }
9092
9093         return statement;
9094       }
9095       break;
9096
9097     default:
9098       cp_parser_error (parser, "expected selection-statement");
9099       return error_mark_node;
9100     }
9101 }
9102
9103 /* Parse a condition.
9104
9105    condition:
9106      expression
9107      type-specifier-seq declarator = initializer-clause
9108      type-specifier-seq declarator braced-init-list
9109
9110    GNU Extension:
9111
9112    condition:
9113      type-specifier-seq declarator asm-specification [opt]
9114        attributes [opt] = assignment-expression
9115
9116    Returns the expression that should be tested.  */
9117
9118 static tree
9119 cp_parser_condition (cp_parser* parser)
9120 {
9121   cp_decl_specifier_seq type_specifiers;
9122   const char *saved_message;
9123   int declares_class_or_enum;
9124
9125   /* Try the declaration first.  */
9126   cp_parser_parse_tentatively (parser);
9127   /* New types are not allowed in the type-specifier-seq for a
9128      condition.  */
9129   saved_message = parser->type_definition_forbidden_message;
9130   parser->type_definition_forbidden_message
9131     = G_("types may not be defined in conditions");
9132   /* Parse the type-specifier-seq.  */
9133   cp_parser_decl_specifier_seq (parser,
9134                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9135                                 &type_specifiers,
9136                                 &declares_class_or_enum);
9137   /* Restore the saved message.  */
9138   parser->type_definition_forbidden_message = saved_message;
9139   /* If all is well, we might be looking at a declaration.  */
9140   if (!cp_parser_error_occurred (parser))
9141     {
9142       tree decl;
9143       tree asm_specification;
9144       tree attributes;
9145       cp_declarator *declarator;
9146       tree initializer = NULL_TREE;
9147
9148       /* Parse the declarator.  */
9149       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9150                                          /*ctor_dtor_or_conv_p=*/NULL,
9151                                          /*parenthesized_p=*/NULL,
9152                                          /*member_p=*/false);
9153       /* Parse the attributes.  */
9154       attributes = cp_parser_attributes_opt (parser);
9155       /* Parse the asm-specification.  */
9156       asm_specification = cp_parser_asm_specification_opt (parser);
9157       /* If the next token is not an `=' or '{', then we might still be
9158          looking at an expression.  For example:
9159
9160            if (A(a).x)
9161
9162          looks like a decl-specifier-seq and a declarator -- but then
9163          there is no `=', so this is an expression.  */
9164       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9165           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9166         cp_parser_simulate_error (parser);
9167         
9168       /* If we did see an `=' or '{', then we are looking at a declaration
9169          for sure.  */
9170       if (cp_parser_parse_definitely (parser))
9171         {
9172           tree pushed_scope;
9173           bool non_constant_p;
9174           bool flags = LOOKUP_ONLYCONVERTING;
9175
9176           /* Create the declaration.  */
9177           decl = start_decl (declarator, &type_specifiers,
9178                              /*initialized_p=*/true,
9179                              attributes, /*prefix_attributes=*/NULL_TREE,
9180                              &pushed_scope);
9181
9182           /* Parse the initializer.  */
9183           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9184             {
9185               initializer = cp_parser_braced_list (parser, &non_constant_p);
9186               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9187               flags = 0;
9188             }
9189           else
9190             {
9191               /* Consume the `='.  */
9192               cp_parser_require (parser, CPP_EQ, RT_EQ);
9193               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9194             }
9195           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9196             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9197
9198           /* Process the initializer.  */
9199           cp_finish_decl (decl,
9200                           initializer, !non_constant_p,
9201                           asm_specification,
9202                           flags);
9203
9204           if (pushed_scope)
9205             pop_scope (pushed_scope);
9206
9207           return convert_from_reference (decl);
9208         }
9209     }
9210   /* If we didn't even get past the declarator successfully, we are
9211      definitely not looking at a declaration.  */
9212   else
9213     cp_parser_abort_tentative_parse (parser);
9214
9215   /* Otherwise, we are looking at an expression.  */
9216   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9217 }
9218
9219 /* Parses a for-statement or range-for-statement until the closing ')',
9220    not included. */
9221
9222 static tree
9223 cp_parser_for (cp_parser *parser)
9224 {
9225   tree init, scope, decl;
9226   bool is_range_for;
9227
9228   /* Begin the for-statement.  */
9229   scope = begin_for_scope (&init);
9230
9231   /* Parse the initialization.  */
9232   is_range_for = cp_parser_for_init_statement (parser, &decl);
9233
9234   if (is_range_for)
9235     return cp_parser_range_for (parser, scope, init, decl);
9236   else
9237     return cp_parser_c_for (parser, scope, init);
9238 }
9239
9240 static tree
9241 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9242 {
9243   /* Normal for loop */
9244   tree condition = NULL_TREE;
9245   tree expression = NULL_TREE;
9246   tree stmt;
9247
9248   stmt = begin_for_stmt (scope, init);
9249   /* The for-init-statement has already been parsed in
9250      cp_parser_for_init_statement, so no work is needed here.  */
9251   finish_for_init_stmt (stmt);
9252
9253   /* If there's a condition, process it.  */
9254   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9255     condition = cp_parser_condition (parser);
9256   finish_for_cond (condition, stmt);
9257   /* Look for the `;'.  */
9258   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9259
9260   /* If there's an expression, process it.  */
9261   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9262     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9263   finish_for_expr (expression, stmt);
9264
9265   return stmt;
9266 }
9267
9268 /* Tries to parse a range-based for-statement:
9269
9270   range-based-for:
9271     decl-specifier-seq declarator : expression
9272
9273   The decl-specifier-seq declarator and the `:' are already parsed by
9274   cp_parser_for_init_statement. If processing_template_decl it returns a
9275   newly created RANGE_FOR_STMT; if not, it is converted to a
9276   regular FOR_STMT.  */
9277
9278 static tree
9279 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9280 {
9281   tree stmt, range_expr;
9282
9283   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9284     {
9285       bool expr_non_constant_p;
9286       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9287     }
9288   else
9289     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9290
9291   /* If in template, STMT is converted to a normal for-statement
9292      at instantiation. If not, it is done just ahead. */
9293   if (processing_template_decl)
9294     {
9295       if (check_for_bare_parameter_packs (range_expr))
9296         range_expr = error_mark_node;
9297       stmt = begin_range_for_stmt (scope, init);
9298       finish_range_for_decl (stmt, range_decl, range_expr);
9299       if (!type_dependent_expression_p (range_expr)
9300           /* do_auto_deduction doesn't mess with template init-lists.  */
9301           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9302         do_range_for_auto_deduction (range_decl, range_expr);
9303     }
9304   else
9305     {
9306       stmt = begin_for_stmt (scope, init);
9307       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9308     }
9309   return stmt;
9310 }
9311
9312 /* Subroutine of cp_convert_range_for: given the initializer expression,
9313    builds up the range temporary.  */
9314
9315 static tree
9316 build_range_temp (tree range_expr)
9317 {
9318   tree range_type, range_temp;
9319
9320   /* Find out the type deduced by the declaration
9321      `auto &&__range = range_expr'.  */
9322   range_type = cp_build_reference_type (make_auto (), true);
9323   range_type = do_auto_deduction (range_type, range_expr,
9324                                   type_uses_auto (range_type));
9325
9326   /* Create the __range variable.  */
9327   range_temp = build_decl (input_location, VAR_DECL,
9328                            get_identifier ("__for_range"), range_type);
9329   TREE_USED (range_temp) = 1;
9330   DECL_ARTIFICIAL (range_temp) = 1;
9331
9332   return range_temp;
9333 }
9334
9335 /* Used by cp_parser_range_for in template context: we aren't going to
9336    do a full conversion yet, but we still need to resolve auto in the
9337    type of the for-range-declaration if present.  This is basically
9338    a shortcut version of cp_convert_range_for.  */
9339
9340 static void
9341 do_range_for_auto_deduction (tree decl, tree range_expr)
9342 {
9343   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9344   if (auto_node)
9345     {
9346       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9347       range_temp = convert_from_reference (build_range_temp (range_expr));
9348       iter_type = (cp_parser_perform_range_for_lookup
9349                    (range_temp, &begin_dummy, &end_dummy));
9350       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9351       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9352                                         tf_warning_or_error);
9353       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9354                                             iter_decl, auto_node);
9355     }
9356 }
9357
9358 /* Converts a range-based for-statement into a normal
9359    for-statement, as per the definition.
9360
9361       for (RANGE_DECL : RANGE_EXPR)
9362         BLOCK
9363
9364    should be equivalent to:
9365
9366       {
9367         auto &&__range = RANGE_EXPR;
9368         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9369               __begin != __end;
9370               ++__begin)
9371           {
9372               RANGE_DECL = *__begin;
9373               BLOCK
9374           }
9375       }
9376
9377    If RANGE_EXPR is an array:
9378         BEGIN_EXPR = __range
9379         END_EXPR = __range + ARRAY_SIZE(__range)
9380    Else if RANGE_EXPR has a member 'begin' or 'end':
9381         BEGIN_EXPR = __range.begin()
9382         END_EXPR = __range.end()
9383    Else:
9384         BEGIN_EXPR = begin(__range)
9385         END_EXPR = end(__range);
9386
9387    If __range has a member 'begin' but not 'end', or vice versa, we must
9388    still use the second alternative (it will surely fail, however).
9389    When calling begin()/end() in the third alternative we must use
9390    argument dependent lookup, but always considering 'std' as an associated
9391    namespace.  */
9392
9393 tree
9394 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9395 {
9396   tree begin, end;
9397   tree iter_type, begin_expr, end_expr;
9398   tree condition, expression;
9399
9400   if (range_decl == error_mark_node || range_expr == error_mark_node)
9401     /* If an error happened previously do nothing or else a lot of
9402        unhelpful errors would be issued.  */
9403     begin_expr = end_expr = iter_type = error_mark_node;
9404   else
9405     {
9406       tree range_temp = build_range_temp (range_expr);
9407       pushdecl (range_temp);
9408       cp_finish_decl (range_temp, range_expr,
9409                       /*is_constant_init*/false, NULL_TREE,
9410                       LOOKUP_ONLYCONVERTING);
9411
9412       range_temp = convert_from_reference (range_temp);
9413       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9414                                                       &begin_expr, &end_expr);
9415     }
9416
9417   /* The new for initialization statement.  */
9418   begin = build_decl (input_location, VAR_DECL,
9419                       get_identifier ("__for_begin"), iter_type);
9420   TREE_USED (begin) = 1;
9421   DECL_ARTIFICIAL (begin) = 1;
9422   pushdecl (begin);
9423   cp_finish_decl (begin, begin_expr,
9424                   /*is_constant_init*/false, NULL_TREE,
9425                   LOOKUP_ONLYCONVERTING);
9426
9427   end = build_decl (input_location, VAR_DECL,
9428                     get_identifier ("__for_end"), iter_type);
9429   TREE_USED (end) = 1;
9430   DECL_ARTIFICIAL (end) = 1;
9431   pushdecl (end);
9432   cp_finish_decl (end, end_expr,
9433                   /*is_constant_init*/false, NULL_TREE,
9434                   LOOKUP_ONLYCONVERTING);
9435
9436   finish_for_init_stmt (statement);
9437
9438   /* The new for condition.  */
9439   condition = build_x_binary_op (NE_EXPR,
9440                                  begin, ERROR_MARK,
9441                                  end, ERROR_MARK,
9442                                  NULL, tf_warning_or_error);
9443   finish_for_cond (condition, statement);
9444
9445   /* The new increment expression.  */
9446   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9447   finish_for_expr (expression, statement);
9448
9449   /* The declaration is initialized with *__begin inside the loop body.  */
9450   cp_finish_decl (range_decl,
9451                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9452                   /*is_constant_init*/false, NULL_TREE,
9453                   LOOKUP_ONLYCONVERTING);
9454
9455   return statement;
9456 }
9457
9458 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9459    We need to solve both at the same time because the method used
9460    depends on the existence of members begin or end.
9461    Returns the type deduced for the iterator expression.  */
9462
9463 static tree
9464 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9465 {
9466   if (error_operand_p (range))
9467     {
9468       *begin = *end = error_mark_node;
9469       return error_mark_node;
9470     }
9471
9472   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9473     {
9474       error ("range-based %<for%> expression of type %qT "
9475              "has incomplete type", TREE_TYPE (range));
9476       *begin = *end = error_mark_node;
9477       return error_mark_node;
9478     }
9479   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9480     {
9481       /* If RANGE is an array, we will use pointer arithmetic.  */
9482       *begin = range;
9483       *end = build_binary_op (input_location, PLUS_EXPR,
9484                               range,
9485                               array_type_nelts_top (TREE_TYPE (range)),
9486                               0);
9487       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9488     }
9489   else
9490     {
9491       /* If it is not an array, we must do a bit of magic.  */
9492       tree id_begin, id_end;
9493       tree member_begin, member_end;
9494
9495       *begin = *end = error_mark_node;
9496
9497       id_begin = get_identifier ("begin");
9498       id_end = get_identifier ("end");
9499       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9500                                     /*protect=*/2, /*want_type=*/false,
9501                                     tf_warning_or_error);
9502       member_end = lookup_member (TREE_TYPE (range), id_end,
9503                                   /*protect=*/2, /*want_type=*/false,
9504                                   tf_warning_or_error);
9505
9506       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9507         {
9508           /* Use the member functions.  */
9509           if (member_begin != NULL_TREE)
9510             *begin = cp_parser_range_for_member_function (range, id_begin);
9511           else
9512             error ("range-based %<for%> expression of type %qT has an "
9513                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9514
9515           if (member_end != NULL_TREE)
9516             *end = cp_parser_range_for_member_function (range, id_end);
9517           else
9518             error ("range-based %<for%> expression of type %qT has a "
9519                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9520         }
9521       else
9522         {
9523           /* Use global functions with ADL.  */
9524           VEC(tree,gc) *vec;
9525           vec = make_tree_vector ();
9526
9527           VEC_safe_push (tree, gc, vec, range);
9528
9529           member_begin = perform_koenig_lookup (id_begin, vec,
9530                                                 /*include_std=*/true,
9531                                                 tf_warning_or_error);
9532           *begin = finish_call_expr (member_begin, &vec, false, true,
9533                                      tf_warning_or_error);
9534           member_end = perform_koenig_lookup (id_end, vec,
9535                                               /*include_std=*/true,
9536                                               tf_warning_or_error);
9537           *end = finish_call_expr (member_end, &vec, false, true,
9538                                    tf_warning_or_error);
9539
9540           release_tree_vector (vec);
9541         }
9542
9543       /* Last common checks.  */
9544       if (*begin == error_mark_node || *end == error_mark_node)
9545         {
9546           /* If one of the expressions is an error do no more checks.  */
9547           *begin = *end = error_mark_node;
9548           return error_mark_node;
9549         }
9550       else
9551         {
9552           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9553           /* The unqualified type of the __begin and __end temporaries should
9554              be the same, as required by the multiple auto declaration.  */
9555           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9556             error ("inconsistent begin/end types in range-based %<for%> "
9557                    "statement: %qT and %qT",
9558                    TREE_TYPE (*begin), TREE_TYPE (*end));
9559           return iter_type;
9560         }
9561     }
9562 }
9563
9564 /* Helper function for cp_parser_perform_range_for_lookup.
9565    Builds a tree for RANGE.IDENTIFIER().  */
9566
9567 static tree
9568 cp_parser_range_for_member_function (tree range, tree identifier)
9569 {
9570   tree member, res;
9571   VEC(tree,gc) *vec;
9572
9573   member = finish_class_member_access_expr (range, identifier,
9574                                             false, tf_warning_or_error);
9575   if (member == error_mark_node)
9576     return error_mark_node;
9577
9578   vec = make_tree_vector ();
9579   res = finish_call_expr (member, &vec,
9580                           /*disallow_virtual=*/false,
9581                           /*koenig_p=*/false,
9582                           tf_warning_or_error);
9583   release_tree_vector (vec);
9584   return res;
9585 }
9586
9587 /* Parse an iteration-statement.
9588
9589    iteration-statement:
9590      while ( condition ) statement
9591      do statement while ( expression ) ;
9592      for ( for-init-statement condition [opt] ; expression [opt] )
9593        statement
9594
9595    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9596
9597 static tree
9598 cp_parser_iteration_statement (cp_parser* parser)
9599 {
9600   cp_token *token;
9601   enum rid keyword;
9602   tree statement;
9603   unsigned char in_statement;
9604
9605   /* Peek at the next token.  */
9606   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9607   if (!token)
9608     return error_mark_node;
9609
9610   /* Remember whether or not we are already within an iteration
9611      statement.  */
9612   in_statement = parser->in_statement;
9613
9614   /* See what kind of keyword it is.  */
9615   keyword = token->keyword;
9616   switch (keyword)
9617     {
9618     case RID_WHILE:
9619       {
9620         tree condition;
9621
9622         /* Begin the while-statement.  */
9623         statement = begin_while_stmt ();
9624         /* Look for the `('.  */
9625         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9626         /* Parse the condition.  */
9627         condition = cp_parser_condition (parser);
9628         finish_while_stmt_cond (condition, statement);
9629         /* Look for the `)'.  */
9630         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9631         /* Parse the dependent statement.  */
9632         parser->in_statement = IN_ITERATION_STMT;
9633         cp_parser_already_scoped_statement (parser);
9634         parser->in_statement = in_statement;
9635         /* We're done with the while-statement.  */
9636         finish_while_stmt (statement);
9637       }
9638       break;
9639
9640     case RID_DO:
9641       {
9642         tree expression;
9643
9644         /* Begin the do-statement.  */
9645         statement = begin_do_stmt ();
9646         /* Parse the body of the do-statement.  */
9647         parser->in_statement = IN_ITERATION_STMT;
9648         cp_parser_implicitly_scoped_statement (parser, NULL);
9649         parser->in_statement = in_statement;
9650         finish_do_body (statement);
9651         /* Look for the `while' keyword.  */
9652         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9653         /* Look for the `('.  */
9654         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9655         /* Parse the expression.  */
9656         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9657         /* We're done with the do-statement.  */
9658         finish_do_stmt (expression, statement);
9659         /* Look for the `)'.  */
9660         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9661         /* Look for the `;'.  */
9662         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9663       }
9664       break;
9665
9666     case RID_FOR:
9667       {
9668         /* Look for the `('.  */
9669         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9670
9671         statement = cp_parser_for (parser);
9672
9673         /* Look for the `)'.  */
9674         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9675
9676         /* Parse the body of the for-statement.  */
9677         parser->in_statement = IN_ITERATION_STMT;
9678         cp_parser_already_scoped_statement (parser);
9679         parser->in_statement = in_statement;
9680
9681         /* We're done with the for-statement.  */
9682         finish_for_stmt (statement);
9683       }
9684       break;
9685
9686     default:
9687       cp_parser_error (parser, "expected iteration-statement");
9688       statement = error_mark_node;
9689       break;
9690     }
9691
9692   return statement;
9693 }
9694
9695 /* Parse a for-init-statement or the declarator of a range-based-for.
9696    Returns true if a range-based-for declaration is seen.
9697
9698    for-init-statement:
9699      expression-statement
9700      simple-declaration  */
9701
9702 static bool
9703 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9704 {
9705   /* If the next token is a `;', then we have an empty
9706      expression-statement.  Grammatically, this is also a
9707      simple-declaration, but an invalid one, because it does not
9708      declare anything.  Therefore, if we did not handle this case
9709      specially, we would issue an error message about an invalid
9710      declaration.  */
9711   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9712     {
9713       bool is_range_for = false;
9714       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9715
9716       parser->colon_corrects_to_scope_p = false;
9717
9718       /* We're going to speculatively look for a declaration, falling back
9719          to an expression, if necessary.  */
9720       cp_parser_parse_tentatively (parser);
9721       /* Parse the declaration.  */
9722       cp_parser_simple_declaration (parser,
9723                                     /*function_definition_allowed_p=*/false,
9724                                     decl);
9725       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9726       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9727         {
9728           /* It is a range-for, consume the ':' */
9729           cp_lexer_consume_token (parser->lexer);
9730           is_range_for = true;
9731           if (cxx_dialect < cxx0x)
9732             {
9733               error_at (cp_lexer_peek_token (parser->lexer)->location,
9734                         "range-based %<for%> loops are not allowed "
9735                         "in C++98 mode");
9736               *decl = error_mark_node;
9737             }
9738         }
9739       else
9740           /* The ';' is not consumed yet because we told
9741              cp_parser_simple_declaration not to.  */
9742           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9743
9744       if (cp_parser_parse_definitely (parser))
9745         return is_range_for;
9746       /* If the tentative parse failed, then we shall need to look for an
9747          expression-statement.  */
9748     }
9749   /* If we are here, it is an expression-statement.  */
9750   cp_parser_expression_statement (parser, NULL_TREE);
9751   return false;
9752 }
9753
9754 /* Parse a jump-statement.
9755
9756    jump-statement:
9757      break ;
9758      continue ;
9759      return expression [opt] ;
9760      return braced-init-list ;
9761      goto identifier ;
9762
9763    GNU extension:
9764
9765    jump-statement:
9766      goto * expression ;
9767
9768    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9769
9770 static tree
9771 cp_parser_jump_statement (cp_parser* parser)
9772 {
9773   tree statement = error_mark_node;
9774   cp_token *token;
9775   enum rid keyword;
9776   unsigned char in_statement;
9777
9778   /* Peek at the next token.  */
9779   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9780   if (!token)
9781     return error_mark_node;
9782
9783   /* See what kind of keyword it is.  */
9784   keyword = token->keyword;
9785   switch (keyword)
9786     {
9787     case RID_BREAK:
9788       in_statement = parser->in_statement & ~IN_IF_STMT;      
9789       switch (in_statement)
9790         {
9791         case 0:
9792           error_at (token->location, "break statement not within loop or switch");
9793           break;
9794         default:
9795           gcc_assert ((in_statement & IN_SWITCH_STMT)
9796                       || in_statement == IN_ITERATION_STMT);
9797           statement = finish_break_stmt ();
9798           break;
9799         case IN_OMP_BLOCK:
9800           error_at (token->location, "invalid exit from OpenMP structured block");
9801           break;
9802         case IN_OMP_FOR:
9803           error_at (token->location, "break statement used with OpenMP for loop");
9804           break;
9805         }
9806       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9807       break;
9808
9809     case RID_CONTINUE:
9810       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9811         {
9812         case 0:
9813           error_at (token->location, "continue statement not within a loop");
9814           break;
9815         case IN_ITERATION_STMT:
9816         case IN_OMP_FOR:
9817           statement = finish_continue_stmt ();
9818           break;
9819         case IN_OMP_BLOCK:
9820           error_at (token->location, "invalid exit from OpenMP structured block");
9821           break;
9822         default:
9823           gcc_unreachable ();
9824         }
9825       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9826       break;
9827
9828     case RID_RETURN:
9829       {
9830         tree expr;
9831         bool expr_non_constant_p;
9832
9833         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9834           {
9835             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9836             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9837           }
9838         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9839           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9840         else
9841           /* If the next token is a `;', then there is no
9842              expression.  */
9843           expr = NULL_TREE;
9844         /* Build the return-statement.  */
9845         statement = finish_return_stmt (expr);
9846         /* Look for the final `;'.  */
9847         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9848       }
9849       break;
9850
9851     case RID_GOTO:
9852       /* Create the goto-statement.  */
9853       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9854         {
9855           /* Issue a warning about this use of a GNU extension.  */
9856           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9857           /* Consume the '*' token.  */
9858           cp_lexer_consume_token (parser->lexer);
9859           /* Parse the dependent expression.  */
9860           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9861         }
9862       else
9863         finish_goto_stmt (cp_parser_identifier (parser));
9864       /* Look for the final `;'.  */
9865       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9866       break;
9867
9868     default:
9869       cp_parser_error (parser, "expected jump-statement");
9870       break;
9871     }
9872
9873   return statement;
9874 }
9875
9876 /* Parse a declaration-statement.
9877
9878    declaration-statement:
9879      block-declaration  */
9880
9881 static void
9882 cp_parser_declaration_statement (cp_parser* parser)
9883 {
9884   void *p;
9885
9886   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9887   p = obstack_alloc (&declarator_obstack, 0);
9888
9889  /* Parse the block-declaration.  */
9890   cp_parser_block_declaration (parser, /*statement_p=*/true);
9891
9892   /* Free any declarators allocated.  */
9893   obstack_free (&declarator_obstack, p);
9894
9895   /* Finish off the statement.  */
9896   finish_stmt ();
9897 }
9898
9899 /* Some dependent statements (like `if (cond) statement'), are
9900    implicitly in their own scope.  In other words, if the statement is
9901    a single statement (as opposed to a compound-statement), it is
9902    none-the-less treated as if it were enclosed in braces.  Any
9903    declarations appearing in the dependent statement are out of scope
9904    after control passes that point.  This function parses a statement,
9905    but ensures that is in its own scope, even if it is not a
9906    compound-statement.
9907
9908    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9909    is a (possibly labeled) if statement which is not enclosed in
9910    braces and has an else clause.  This is used to implement
9911    -Wparentheses.
9912
9913    Returns the new statement.  */
9914
9915 static tree
9916 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9917 {
9918   tree statement;
9919
9920   if (if_p != NULL)
9921     *if_p = false;
9922
9923   /* Mark if () ; with a special NOP_EXPR.  */
9924   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9925     {
9926       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9927       cp_lexer_consume_token (parser->lexer);
9928       statement = add_stmt (build_empty_stmt (loc));
9929     }
9930   /* if a compound is opened, we simply parse the statement directly.  */
9931   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9932     statement = cp_parser_compound_statement (parser, NULL, false, false);
9933   /* If the token is not a `{', then we must take special action.  */
9934   else
9935     {
9936       /* Create a compound-statement.  */
9937       statement = begin_compound_stmt (0);
9938       /* Parse the dependent-statement.  */
9939       cp_parser_statement (parser, NULL_TREE, false, if_p);
9940       /* Finish the dummy compound-statement.  */
9941       finish_compound_stmt (statement);
9942     }
9943
9944   /* Return the statement.  */
9945   return statement;
9946 }
9947
9948 /* For some dependent statements (like `while (cond) statement'), we
9949    have already created a scope.  Therefore, even if the dependent
9950    statement is a compound-statement, we do not want to create another
9951    scope.  */
9952
9953 static void
9954 cp_parser_already_scoped_statement (cp_parser* parser)
9955 {
9956   /* If the token is a `{', then we must take special action.  */
9957   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9958     cp_parser_statement (parser, NULL_TREE, false, NULL);
9959   else
9960     {
9961       /* Avoid calling cp_parser_compound_statement, so that we
9962          don't create a new scope.  Do everything else by hand.  */
9963       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9964       /* If the next keyword is `__label__' we have a label declaration.  */
9965       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9966         cp_parser_label_declaration (parser);
9967       /* Parse an (optional) statement-seq.  */
9968       cp_parser_statement_seq_opt (parser, NULL_TREE);
9969       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9970     }
9971 }
9972
9973 /* Declarations [gram.dcl.dcl] */
9974
9975 /* Parse an optional declaration-sequence.
9976
9977    declaration-seq:
9978      declaration
9979      declaration-seq declaration  */
9980
9981 static void
9982 cp_parser_declaration_seq_opt (cp_parser* parser)
9983 {
9984   while (true)
9985     {
9986       cp_token *token;
9987
9988       token = cp_lexer_peek_token (parser->lexer);
9989
9990       if (token->type == CPP_CLOSE_BRACE
9991           || token->type == CPP_EOF
9992           || token->type == CPP_PRAGMA_EOL)
9993         break;
9994
9995       if (token->type == CPP_SEMICOLON)
9996         {
9997           /* A declaration consisting of a single semicolon is
9998              invalid.  Allow it unless we're being pedantic.  */
9999           cp_lexer_consume_token (parser->lexer);
10000           if (!in_system_header)
10001             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
10002           continue;
10003         }
10004
10005       /* If we're entering or exiting a region that's implicitly
10006          extern "C", modify the lang context appropriately.  */
10007       if (!parser->implicit_extern_c && token->implicit_extern_c)
10008         {
10009           push_lang_context (lang_name_c);
10010           parser->implicit_extern_c = true;
10011         }
10012       else if (parser->implicit_extern_c && !token->implicit_extern_c)
10013         {
10014           pop_lang_context ();
10015           parser->implicit_extern_c = false;
10016         }
10017
10018       if (token->type == CPP_PRAGMA)
10019         {
10020           /* A top-level declaration can consist solely of a #pragma.
10021              A nested declaration cannot, so this is done here and not
10022              in cp_parser_declaration.  (A #pragma at block scope is
10023              handled in cp_parser_statement.)  */
10024           cp_parser_pragma (parser, pragma_external);
10025           continue;
10026         }
10027
10028       /* Parse the declaration itself.  */
10029       cp_parser_declaration (parser);
10030     }
10031 }
10032
10033 /* Parse a declaration.
10034
10035    declaration:
10036      block-declaration
10037      function-definition
10038      template-declaration
10039      explicit-instantiation
10040      explicit-specialization
10041      linkage-specification
10042      namespace-definition
10043
10044    GNU extension:
10045
10046    declaration:
10047       __extension__ declaration */
10048
10049 static void
10050 cp_parser_declaration (cp_parser* parser)
10051 {
10052   cp_token token1;
10053   cp_token token2;
10054   int saved_pedantic;
10055   void *p;
10056   tree attributes = NULL_TREE;
10057
10058   /* Check for the `__extension__' keyword.  */
10059   if (cp_parser_extension_opt (parser, &saved_pedantic))
10060     {
10061       /* Parse the qualified declaration.  */
10062       cp_parser_declaration (parser);
10063       /* Restore the PEDANTIC flag.  */
10064       pedantic = saved_pedantic;
10065
10066       return;
10067     }
10068
10069   /* Try to figure out what kind of declaration is present.  */
10070   token1 = *cp_lexer_peek_token (parser->lexer);
10071
10072   if (token1.type != CPP_EOF)
10073     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10074   else
10075     {
10076       token2.type = CPP_EOF;
10077       token2.keyword = RID_MAX;
10078     }
10079
10080   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10081   p = obstack_alloc (&declarator_obstack, 0);
10082
10083   /* If the next token is `extern' and the following token is a string
10084      literal, then we have a linkage specification.  */
10085   if (token1.keyword == RID_EXTERN
10086       && cp_parser_is_pure_string_literal (&token2))
10087     cp_parser_linkage_specification (parser);
10088   /* If the next token is `template', then we have either a template
10089      declaration, an explicit instantiation, or an explicit
10090      specialization.  */
10091   else if (token1.keyword == RID_TEMPLATE)
10092     {
10093       /* `template <>' indicates a template specialization.  */
10094       if (token2.type == CPP_LESS
10095           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10096         cp_parser_explicit_specialization (parser);
10097       /* `template <' indicates a template declaration.  */
10098       else if (token2.type == CPP_LESS)
10099         cp_parser_template_declaration (parser, /*member_p=*/false);
10100       /* Anything else must be an explicit instantiation.  */
10101       else
10102         cp_parser_explicit_instantiation (parser);
10103     }
10104   /* If the next token is `export', then we have a template
10105      declaration.  */
10106   else if (token1.keyword == RID_EXPORT)
10107     cp_parser_template_declaration (parser, /*member_p=*/false);
10108   /* If the next token is `extern', 'static' or 'inline' and the one
10109      after that is `template', we have a GNU extended explicit
10110      instantiation directive.  */
10111   else if (cp_parser_allow_gnu_extensions_p (parser)
10112            && (token1.keyword == RID_EXTERN
10113                || token1.keyword == RID_STATIC
10114                || token1.keyword == RID_INLINE)
10115            && token2.keyword == RID_TEMPLATE)
10116     cp_parser_explicit_instantiation (parser);
10117   /* If the next token is `namespace', check for a named or unnamed
10118      namespace definition.  */
10119   else if (token1.keyword == RID_NAMESPACE
10120            && (/* A named namespace definition.  */
10121                (token2.type == CPP_NAME
10122                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10123                     != CPP_EQ))
10124                /* An unnamed namespace definition.  */
10125                || token2.type == CPP_OPEN_BRACE
10126                || token2.keyword == RID_ATTRIBUTE))
10127     cp_parser_namespace_definition (parser);
10128   /* An inline (associated) namespace definition.  */
10129   else if (token1.keyword == RID_INLINE
10130            && token2.keyword == RID_NAMESPACE)
10131     cp_parser_namespace_definition (parser);
10132   /* Objective-C++ declaration/definition.  */
10133   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10134     cp_parser_objc_declaration (parser, NULL_TREE);
10135   else if (c_dialect_objc ()
10136            && token1.keyword == RID_ATTRIBUTE
10137            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10138     cp_parser_objc_declaration (parser, attributes);
10139   /* We must have either a block declaration or a function
10140      definition.  */
10141   else
10142     /* Try to parse a block-declaration, or a function-definition.  */
10143     cp_parser_block_declaration (parser, /*statement_p=*/false);
10144
10145   /* Free any declarators allocated.  */
10146   obstack_free (&declarator_obstack, p);
10147 }
10148
10149 /* Parse a block-declaration.
10150
10151    block-declaration:
10152      simple-declaration
10153      asm-definition
10154      namespace-alias-definition
10155      using-declaration
10156      using-directive
10157
10158    GNU Extension:
10159
10160    block-declaration:
10161      __extension__ block-declaration
10162
10163    C++0x Extension:
10164
10165    block-declaration:
10166      static_assert-declaration
10167
10168    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10169    part of a declaration-statement.  */
10170
10171 static void
10172 cp_parser_block_declaration (cp_parser *parser,
10173                              bool      statement_p)
10174 {
10175   cp_token *token1;
10176   int saved_pedantic;
10177
10178   /* Check for the `__extension__' keyword.  */
10179   if (cp_parser_extension_opt (parser, &saved_pedantic))
10180     {
10181       /* Parse the qualified declaration.  */
10182       cp_parser_block_declaration (parser, statement_p);
10183       /* Restore the PEDANTIC flag.  */
10184       pedantic = saved_pedantic;
10185
10186       return;
10187     }
10188
10189   /* Peek at the next token to figure out which kind of declaration is
10190      present.  */
10191   token1 = cp_lexer_peek_token (parser->lexer);
10192
10193   /* If the next keyword is `asm', we have an asm-definition.  */
10194   if (token1->keyword == RID_ASM)
10195     {
10196       if (statement_p)
10197         cp_parser_commit_to_tentative_parse (parser);
10198       cp_parser_asm_definition (parser);
10199     }
10200   /* If the next keyword is `namespace', we have a
10201      namespace-alias-definition.  */
10202   else if (token1->keyword == RID_NAMESPACE)
10203     cp_parser_namespace_alias_definition (parser);
10204   /* If the next keyword is `using', we have a
10205      using-declaration, a using-directive, or an alias-declaration.  */
10206   else if (token1->keyword == RID_USING)
10207     {
10208       cp_token *token2;
10209
10210       if (statement_p)
10211         cp_parser_commit_to_tentative_parse (parser);
10212       /* If the token after `using' is `namespace', then we have a
10213          using-directive.  */
10214       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10215       if (token2->keyword == RID_NAMESPACE)
10216         cp_parser_using_directive (parser);
10217       /* If the second token after 'using' is '=', then we have an
10218          alias-declaration.  */
10219       else if (cxx_dialect >= cxx0x
10220                && token2->type == CPP_NAME
10221                && ((cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
10222                    || (cp_lexer_peek_nth_token (parser->lexer, 3)->keyword
10223                        == RID_ATTRIBUTE)))
10224         cp_parser_alias_declaration (parser);
10225       /* Otherwise, it's a using-declaration.  */
10226       else
10227         cp_parser_using_declaration (parser,
10228                                      /*access_declaration_p=*/false);
10229     }
10230   /* If the next keyword is `__label__' we have a misplaced label
10231      declaration.  */
10232   else if (token1->keyword == RID_LABEL)
10233     {
10234       cp_lexer_consume_token (parser->lexer);
10235       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10236       cp_parser_skip_to_end_of_statement (parser);
10237       /* If the next token is now a `;', consume it.  */
10238       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10239         cp_lexer_consume_token (parser->lexer);
10240     }
10241   /* If the next token is `static_assert' we have a static assertion.  */
10242   else if (token1->keyword == RID_STATIC_ASSERT)
10243     cp_parser_static_assert (parser, /*member_p=*/false);
10244   /* Anything else must be a simple-declaration.  */
10245   else
10246     cp_parser_simple_declaration (parser, !statement_p,
10247                                   /*maybe_range_for_decl*/NULL);
10248 }
10249
10250 /* Parse a simple-declaration.
10251
10252    simple-declaration:
10253      decl-specifier-seq [opt] init-declarator-list [opt] ;
10254
10255    init-declarator-list:
10256      init-declarator
10257      init-declarator-list , init-declarator
10258
10259    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10260    function-definition as a simple-declaration.
10261
10262    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10263    parsed declaration if it is an uninitialized single declarator not followed
10264    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10265    if present, will not be consumed.  */
10266
10267 static void
10268 cp_parser_simple_declaration (cp_parser* parser,
10269                               bool function_definition_allowed_p,
10270                               tree *maybe_range_for_decl)
10271 {
10272   cp_decl_specifier_seq decl_specifiers;
10273   int declares_class_or_enum;
10274   bool saw_declarator;
10275
10276   if (maybe_range_for_decl)
10277     *maybe_range_for_decl = NULL_TREE;
10278
10279   /* Defer access checks until we know what is being declared; the
10280      checks for names appearing in the decl-specifier-seq should be
10281      done as if we were in the scope of the thing being declared.  */
10282   push_deferring_access_checks (dk_deferred);
10283
10284   /* Parse the decl-specifier-seq.  We have to keep track of whether
10285      or not the decl-specifier-seq declares a named class or
10286      enumeration type, since that is the only case in which the
10287      init-declarator-list is allowed to be empty.
10288
10289      [dcl.dcl]
10290
10291      In a simple-declaration, the optional init-declarator-list can be
10292      omitted only when declaring a class or enumeration, that is when
10293      the decl-specifier-seq contains either a class-specifier, an
10294      elaborated-type-specifier, or an enum-specifier.  */
10295   cp_parser_decl_specifier_seq (parser,
10296                                 CP_PARSER_FLAGS_OPTIONAL,
10297                                 &decl_specifiers,
10298                                 &declares_class_or_enum);
10299   /* We no longer need to defer access checks.  */
10300   stop_deferring_access_checks ();
10301
10302   /* In a block scope, a valid declaration must always have a
10303      decl-specifier-seq.  By not trying to parse declarators, we can
10304      resolve the declaration/expression ambiguity more quickly.  */
10305   if (!function_definition_allowed_p
10306       && !decl_specifiers.any_specifiers_p)
10307     {
10308       cp_parser_error (parser, "expected declaration");
10309       goto done;
10310     }
10311
10312   /* If the next two tokens are both identifiers, the code is
10313      erroneous. The usual cause of this situation is code like:
10314
10315        T t;
10316
10317      where "T" should name a type -- but does not.  */
10318   if (!decl_specifiers.any_type_specifiers_p
10319       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10320     {
10321       /* If parsing tentatively, we should commit; we really are
10322          looking at a declaration.  */
10323       cp_parser_commit_to_tentative_parse (parser);
10324       /* Give up.  */
10325       goto done;
10326     }
10327
10328   /* If we have seen at least one decl-specifier, and the next token
10329      is not a parenthesis, then we must be looking at a declaration.
10330      (After "int (" we might be looking at a functional cast.)  */
10331   if (decl_specifiers.any_specifiers_p
10332       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10333       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10334       && !cp_parser_error_occurred (parser))
10335     cp_parser_commit_to_tentative_parse (parser);
10336
10337   /* Keep going until we hit the `;' at the end of the simple
10338      declaration.  */
10339   saw_declarator = false;
10340   while (cp_lexer_next_token_is_not (parser->lexer,
10341                                      CPP_SEMICOLON))
10342     {
10343       cp_token *token;
10344       bool function_definition_p;
10345       tree decl;
10346
10347       if (saw_declarator)
10348         {
10349           /* If we are processing next declarator, coma is expected */
10350           token = cp_lexer_peek_token (parser->lexer);
10351           gcc_assert (token->type == CPP_COMMA);
10352           cp_lexer_consume_token (parser->lexer);
10353           if (maybe_range_for_decl)
10354             *maybe_range_for_decl = error_mark_node;
10355         }
10356       else
10357         saw_declarator = true;
10358
10359       /* Parse the init-declarator.  */
10360       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10361                                         /*checks=*/NULL,
10362                                         function_definition_allowed_p,
10363                                         /*member_p=*/false,
10364                                         declares_class_or_enum,
10365                                         &function_definition_p,
10366                                         maybe_range_for_decl);
10367       /* If an error occurred while parsing tentatively, exit quickly.
10368          (That usually happens when in the body of a function; each
10369          statement is treated as a declaration-statement until proven
10370          otherwise.)  */
10371       if (cp_parser_error_occurred (parser))
10372         goto done;
10373       /* Handle function definitions specially.  */
10374       if (function_definition_p)
10375         {
10376           /* If the next token is a `,', then we are probably
10377              processing something like:
10378
10379                void f() {}, *p;
10380
10381              which is erroneous.  */
10382           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10383             {
10384               cp_token *token = cp_lexer_peek_token (parser->lexer);
10385               error_at (token->location,
10386                         "mixing"
10387                         " declarations and function-definitions is forbidden");
10388             }
10389           /* Otherwise, we're done with the list of declarators.  */
10390           else
10391             {
10392               pop_deferring_access_checks ();
10393               return;
10394             }
10395         }
10396       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10397         *maybe_range_for_decl = decl;
10398       /* The next token should be either a `,' or a `;'.  */
10399       token = cp_lexer_peek_token (parser->lexer);
10400       /* If it's a `,', there are more declarators to come.  */
10401       if (token->type == CPP_COMMA)
10402         /* will be consumed next time around */;
10403       /* If it's a `;', we are done.  */
10404       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10405         break;
10406       /* Anything else is an error.  */
10407       else
10408         {
10409           /* If we have already issued an error message we don't need
10410              to issue another one.  */
10411           if (decl != error_mark_node
10412               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10413             cp_parser_error (parser, "expected %<,%> or %<;%>");
10414           /* Skip tokens until we reach the end of the statement.  */
10415           cp_parser_skip_to_end_of_statement (parser);
10416           /* If the next token is now a `;', consume it.  */
10417           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10418             cp_lexer_consume_token (parser->lexer);
10419           goto done;
10420         }
10421       /* After the first time around, a function-definition is not
10422          allowed -- even if it was OK at first.  For example:
10423
10424            int i, f() {}
10425
10426          is not valid.  */
10427       function_definition_allowed_p = false;
10428     }
10429
10430   /* Issue an error message if no declarators are present, and the
10431      decl-specifier-seq does not itself declare a class or
10432      enumeration.  */
10433   if (!saw_declarator)
10434     {
10435       if (cp_parser_declares_only_class_p (parser))
10436         shadow_tag (&decl_specifiers);
10437       /* Perform any deferred access checks.  */
10438       perform_deferred_access_checks ();
10439     }
10440
10441   /* Consume the `;'.  */
10442   if (!maybe_range_for_decl)
10443       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10444
10445  done:
10446   pop_deferring_access_checks ();
10447 }
10448
10449 /* Parse a decl-specifier-seq.
10450
10451    decl-specifier-seq:
10452      decl-specifier-seq [opt] decl-specifier
10453
10454    decl-specifier:
10455      storage-class-specifier
10456      type-specifier
10457      function-specifier
10458      friend
10459      typedef
10460
10461    GNU Extension:
10462
10463    decl-specifier:
10464      attributes
10465
10466    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10467
10468    The parser flags FLAGS is used to control type-specifier parsing.
10469
10470    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10471    flags:
10472
10473      1: one of the decl-specifiers is an elaborated-type-specifier
10474         (i.e., a type declaration)
10475      2: one of the decl-specifiers is an enum-specifier or a
10476         class-specifier (i.e., a type definition)
10477
10478    */
10479
10480 static void
10481 cp_parser_decl_specifier_seq (cp_parser* parser,
10482                               cp_parser_flags flags,
10483                               cp_decl_specifier_seq *decl_specs,
10484                               int* declares_class_or_enum)
10485 {
10486   bool constructor_possible_p = !parser->in_declarator_p;
10487   cp_token *start_token = NULL;
10488
10489   /* Clear DECL_SPECS.  */
10490   clear_decl_specs (decl_specs);
10491
10492   /* Assume no class or enumeration type is declared.  */
10493   *declares_class_or_enum = 0;
10494
10495   /* Keep reading specifiers until there are no more to read.  */
10496   while (true)
10497     {
10498       bool constructor_p;
10499       bool found_decl_spec;
10500       cp_token *token;
10501
10502       /* Peek at the next token.  */
10503       token = cp_lexer_peek_token (parser->lexer);
10504
10505       /* Save the first token of the decl spec list for error
10506          reporting.  */
10507       if (!start_token)
10508         start_token = token;
10509       /* Handle attributes.  */
10510       if (token->keyword == RID_ATTRIBUTE)
10511         {
10512           /* Parse the attributes.  */
10513           decl_specs->attributes
10514             = chainon (decl_specs->attributes,
10515                        cp_parser_attributes_opt (parser));
10516           continue;
10517         }
10518       /* Assume we will find a decl-specifier keyword.  */
10519       found_decl_spec = true;
10520       /* If the next token is an appropriate keyword, we can simply
10521          add it to the list.  */
10522       switch (token->keyword)
10523         {
10524           /* decl-specifier:
10525                friend
10526                constexpr */
10527         case RID_FRIEND:
10528           if (!at_class_scope_p ())
10529             {
10530               error_at (token->location, "%<friend%> used outside of class");
10531               cp_lexer_purge_token (parser->lexer);
10532             }
10533           else
10534             {
10535               ++decl_specs->specs[(int) ds_friend];
10536               /* Consume the token.  */
10537               cp_lexer_consume_token (parser->lexer);
10538             }
10539           break;
10540
10541         case RID_CONSTEXPR:
10542           ++decl_specs->specs[(int) ds_constexpr];
10543           cp_lexer_consume_token (parser->lexer);
10544           break;
10545
10546           /* function-specifier:
10547                inline
10548                virtual
10549                explicit  */
10550         case RID_INLINE:
10551         case RID_VIRTUAL:
10552         case RID_EXPLICIT:
10553           cp_parser_function_specifier_opt (parser, decl_specs);
10554           break;
10555
10556           /* decl-specifier:
10557                typedef  */
10558         case RID_TYPEDEF:
10559           ++decl_specs->specs[(int) ds_typedef];
10560           /* Consume the token.  */
10561           cp_lexer_consume_token (parser->lexer);
10562           /* A constructor declarator cannot appear in a typedef.  */
10563           constructor_possible_p = false;
10564           /* The "typedef" keyword can only occur in a declaration; we
10565              may as well commit at this point.  */
10566           cp_parser_commit_to_tentative_parse (parser);
10567
10568           if (decl_specs->storage_class != sc_none)
10569             decl_specs->conflicting_specifiers_p = true;
10570           break;
10571
10572           /* storage-class-specifier:
10573                auto
10574                register
10575                static
10576                extern
10577                mutable
10578
10579              GNU Extension:
10580                thread  */
10581         case RID_AUTO:
10582           if (cxx_dialect == cxx98) 
10583             {
10584               /* Consume the token.  */
10585               cp_lexer_consume_token (parser->lexer);
10586
10587               /* Complain about `auto' as a storage specifier, if
10588                  we're complaining about C++0x compatibility.  */
10589               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10590                           " changes meaning in C++11; please remove it");
10591
10592               /* Set the storage class anyway.  */
10593               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10594                                            token->location);
10595             }
10596           else
10597             /* C++0x auto type-specifier.  */
10598             found_decl_spec = false;
10599           break;
10600
10601         case RID_REGISTER:
10602         case RID_STATIC:
10603         case RID_EXTERN:
10604         case RID_MUTABLE:
10605           /* Consume the token.  */
10606           cp_lexer_consume_token (parser->lexer);
10607           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10608                                        token->location);
10609           break;
10610         case RID_THREAD:
10611           /* Consume the token.  */
10612           cp_lexer_consume_token (parser->lexer);
10613           ++decl_specs->specs[(int) ds_thread];
10614           break;
10615
10616         default:
10617           /* We did not yet find a decl-specifier yet.  */
10618           found_decl_spec = false;
10619           break;
10620         }
10621
10622       if (found_decl_spec
10623           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10624           && token->keyword != RID_CONSTEXPR)
10625         error ("decl-specifier invalid in condition");
10626
10627       /* Constructors are a special case.  The `S' in `S()' is not a
10628          decl-specifier; it is the beginning of the declarator.  */
10629       constructor_p
10630         = (!found_decl_spec
10631            && constructor_possible_p
10632            && (cp_parser_constructor_declarator_p
10633                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10634
10635       /* If we don't have a DECL_SPEC yet, then we must be looking at
10636          a type-specifier.  */
10637       if (!found_decl_spec && !constructor_p)
10638         {
10639           int decl_spec_declares_class_or_enum;
10640           bool is_cv_qualifier;
10641           tree type_spec;
10642
10643           type_spec
10644             = cp_parser_type_specifier (parser, flags,
10645                                         decl_specs,
10646                                         /*is_declaration=*/true,
10647                                         &decl_spec_declares_class_or_enum,
10648                                         &is_cv_qualifier);
10649           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10650
10651           /* If this type-specifier referenced a user-defined type
10652              (a typedef, class-name, etc.), then we can't allow any
10653              more such type-specifiers henceforth.
10654
10655              [dcl.spec]
10656
10657              The longest sequence of decl-specifiers that could
10658              possibly be a type name is taken as the
10659              decl-specifier-seq of a declaration.  The sequence shall
10660              be self-consistent as described below.
10661
10662              [dcl.type]
10663
10664              As a general rule, at most one type-specifier is allowed
10665              in the complete decl-specifier-seq of a declaration.  The
10666              only exceptions are the following:
10667
10668              -- const or volatile can be combined with any other
10669                 type-specifier.
10670
10671              -- signed or unsigned can be combined with char, long,
10672                 short, or int.
10673
10674              -- ..
10675
10676              Example:
10677
10678                typedef char* Pc;
10679                void g (const int Pc);
10680
10681              Here, Pc is *not* part of the decl-specifier seq; it's
10682              the declarator.  Therefore, once we see a type-specifier
10683              (other than a cv-qualifier), we forbid any additional
10684              user-defined types.  We *do* still allow things like `int
10685              int' to be considered a decl-specifier-seq, and issue the
10686              error message later.  */
10687           if (type_spec && !is_cv_qualifier)
10688             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10689           /* A constructor declarator cannot follow a type-specifier.  */
10690           if (type_spec)
10691             {
10692               constructor_possible_p = false;
10693               found_decl_spec = true;
10694               if (!is_cv_qualifier)
10695                 decl_specs->any_type_specifiers_p = true;
10696             }
10697         }
10698
10699       /* If we still do not have a DECL_SPEC, then there are no more
10700          decl-specifiers.  */
10701       if (!found_decl_spec)
10702         break;
10703
10704       decl_specs->any_specifiers_p = true;
10705       /* After we see one decl-specifier, further decl-specifiers are
10706          always optional.  */
10707       flags |= CP_PARSER_FLAGS_OPTIONAL;
10708     }
10709
10710   cp_parser_check_decl_spec (decl_specs, start_token->location);
10711
10712   /* Don't allow a friend specifier with a class definition.  */
10713   if (decl_specs->specs[(int) ds_friend] != 0
10714       && (*declares_class_or_enum & 2))
10715     error_at (start_token->location,
10716               "class definition may not be declared a friend");
10717 }
10718
10719 /* Parse an (optional) storage-class-specifier.
10720
10721    storage-class-specifier:
10722      auto
10723      register
10724      static
10725      extern
10726      mutable
10727
10728    GNU Extension:
10729
10730    storage-class-specifier:
10731      thread
10732
10733    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10734
10735 static tree
10736 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10737 {
10738   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10739     {
10740     case RID_AUTO:
10741       if (cxx_dialect != cxx98)
10742         return NULL_TREE;
10743       /* Fall through for C++98.  */
10744
10745     case RID_REGISTER:
10746     case RID_STATIC:
10747     case RID_EXTERN:
10748     case RID_MUTABLE:
10749     case RID_THREAD:
10750       /* Consume the token.  */
10751       return cp_lexer_consume_token (parser->lexer)->u.value;
10752
10753     default:
10754       return NULL_TREE;
10755     }
10756 }
10757
10758 /* Parse an (optional) function-specifier.
10759
10760    function-specifier:
10761      inline
10762      virtual
10763      explicit
10764
10765    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10766    Updates DECL_SPECS, if it is non-NULL.  */
10767
10768 static tree
10769 cp_parser_function_specifier_opt (cp_parser* parser,
10770                                   cp_decl_specifier_seq *decl_specs)
10771 {
10772   cp_token *token = cp_lexer_peek_token (parser->lexer);
10773   switch (token->keyword)
10774     {
10775     case RID_INLINE:
10776       if (decl_specs)
10777         ++decl_specs->specs[(int) ds_inline];
10778       break;
10779
10780     case RID_VIRTUAL:
10781       /* 14.5.2.3 [temp.mem]
10782
10783          A member function template shall not be virtual.  */
10784       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10785         error_at (token->location, "templates may not be %<virtual%>");
10786       else if (decl_specs)
10787         ++decl_specs->specs[(int) ds_virtual];
10788       break;
10789
10790     case RID_EXPLICIT:
10791       if (decl_specs)
10792         ++decl_specs->specs[(int) ds_explicit];
10793       break;
10794
10795     default:
10796       return NULL_TREE;
10797     }
10798
10799   /* Consume the token.  */
10800   return cp_lexer_consume_token (parser->lexer)->u.value;
10801 }
10802
10803 /* Parse a linkage-specification.
10804
10805    linkage-specification:
10806      extern string-literal { declaration-seq [opt] }
10807      extern string-literal declaration  */
10808
10809 static void
10810 cp_parser_linkage_specification (cp_parser* parser)
10811 {
10812   tree linkage;
10813
10814   /* Look for the `extern' keyword.  */
10815   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10816
10817   /* Look for the string-literal.  */
10818   linkage = cp_parser_string_literal (parser, false, false);
10819
10820   /* Transform the literal into an identifier.  If the literal is a
10821      wide-character string, or contains embedded NULs, then we can't
10822      handle it as the user wants.  */
10823   if (strlen (TREE_STRING_POINTER (linkage))
10824       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10825     {
10826       cp_parser_error (parser, "invalid linkage-specification");
10827       /* Assume C++ linkage.  */
10828       linkage = lang_name_cplusplus;
10829     }
10830   else
10831     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10832
10833   /* We're now using the new linkage.  */
10834   push_lang_context (linkage);
10835
10836   /* If the next token is a `{', then we're using the first
10837      production.  */
10838   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10839     {
10840       /* Consume the `{' token.  */
10841       cp_lexer_consume_token (parser->lexer);
10842       /* Parse the declarations.  */
10843       cp_parser_declaration_seq_opt (parser);
10844       /* Look for the closing `}'.  */
10845       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10846     }
10847   /* Otherwise, there's just one declaration.  */
10848   else
10849     {
10850       bool saved_in_unbraced_linkage_specification_p;
10851
10852       saved_in_unbraced_linkage_specification_p
10853         = parser->in_unbraced_linkage_specification_p;
10854       parser->in_unbraced_linkage_specification_p = true;
10855       cp_parser_declaration (parser);
10856       parser->in_unbraced_linkage_specification_p
10857         = saved_in_unbraced_linkage_specification_p;
10858     }
10859
10860   /* We're done with the linkage-specification.  */
10861   pop_lang_context ();
10862 }
10863
10864 /* Parse a static_assert-declaration.
10865
10866    static_assert-declaration:
10867      static_assert ( constant-expression , string-literal ) ; 
10868
10869    If MEMBER_P, this static_assert is a class member.  */
10870
10871 static void 
10872 cp_parser_static_assert(cp_parser *parser, bool member_p)
10873 {
10874   tree condition;
10875   tree message;
10876   cp_token *token;
10877   location_t saved_loc;
10878   bool dummy;
10879
10880   /* Peek at the `static_assert' token so we can keep track of exactly
10881      where the static assertion started.  */
10882   token = cp_lexer_peek_token (parser->lexer);
10883   saved_loc = token->location;
10884
10885   /* Look for the `static_assert' keyword.  */
10886   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10887                                   RT_STATIC_ASSERT))
10888     return;
10889
10890   /*  We know we are in a static assertion; commit to any tentative
10891       parse.  */
10892   if (cp_parser_parsing_tentatively (parser))
10893     cp_parser_commit_to_tentative_parse (parser);
10894
10895   /* Parse the `(' starting the static assertion condition.  */
10896   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10897
10898   /* Parse the constant-expression.  Allow a non-constant expression
10899      here in order to give better diagnostics in finish_static_assert.  */
10900   condition = 
10901     cp_parser_constant_expression (parser,
10902                                    /*allow_non_constant_p=*/true,
10903                                    /*non_constant_p=*/&dummy);
10904
10905   /* Parse the separating `,'.  */
10906   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10907
10908   /* Parse the string-literal message.  */
10909   message = cp_parser_string_literal (parser, 
10910                                       /*translate=*/false,
10911                                       /*wide_ok=*/true);
10912
10913   /* A `)' completes the static assertion.  */
10914   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10915     cp_parser_skip_to_closing_parenthesis (parser, 
10916                                            /*recovering=*/true, 
10917                                            /*or_comma=*/false,
10918                                            /*consume_paren=*/true);
10919
10920   /* A semicolon terminates the declaration.  */
10921   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10922
10923   /* Complete the static assertion, which may mean either processing 
10924      the static assert now or saving it for template instantiation.  */
10925   finish_static_assert (condition, message, saved_loc, member_p);
10926 }
10927
10928 /* Parse a `decltype' type. Returns the type. 
10929
10930    simple-type-specifier:
10931      decltype ( expression )  */
10932
10933 static tree
10934 cp_parser_decltype (cp_parser *parser)
10935 {
10936   tree expr;
10937   bool id_expression_or_member_access_p = false;
10938   const char *saved_message;
10939   bool saved_integral_constant_expression_p;
10940   bool saved_non_integral_constant_expression_p;
10941   cp_token *id_expr_start_token;
10942   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10943
10944   if (start_token->type == CPP_DECLTYPE)
10945     {
10946       /* Already parsed.  */
10947       cp_lexer_consume_token (parser->lexer);
10948       return start_token->u.value;
10949     }
10950
10951   /* Look for the `decltype' token.  */
10952   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10953     return error_mark_node;
10954
10955   /* Types cannot be defined in a `decltype' expression.  Save away the
10956      old message.  */
10957   saved_message = parser->type_definition_forbidden_message;
10958
10959   /* And create the new one.  */
10960   parser->type_definition_forbidden_message
10961     = G_("types may not be defined in %<decltype%> expressions");
10962
10963   /* The restrictions on constant-expressions do not apply inside
10964      decltype expressions.  */
10965   saved_integral_constant_expression_p
10966     = parser->integral_constant_expression_p;
10967   saved_non_integral_constant_expression_p
10968     = parser->non_integral_constant_expression_p;
10969   parser->integral_constant_expression_p = false;
10970
10971   /* Do not actually evaluate the expression.  */
10972   ++cp_unevaluated_operand;
10973
10974   /* Do not warn about problems with the expression.  */
10975   ++c_inhibit_evaluation_warnings;
10976
10977   /* Parse the opening `('.  */
10978   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10979     return error_mark_node;
10980   
10981   /* First, try parsing an id-expression.  */
10982   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10983   cp_parser_parse_tentatively (parser);
10984   expr = cp_parser_id_expression (parser,
10985                                   /*template_keyword_p=*/false,
10986                                   /*check_dependency_p=*/true,
10987                                   /*template_p=*/NULL,
10988                                   /*declarator_p=*/false,
10989                                   /*optional_p=*/false);
10990
10991   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10992     {
10993       bool non_integral_constant_expression_p = false;
10994       tree id_expression = expr;
10995       cp_id_kind idk;
10996       const char *error_msg;
10997
10998       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10999         /* Lookup the name we got back from the id-expression.  */
11000         expr = cp_parser_lookup_name (parser, expr,
11001                                       none_type,
11002                                       /*is_template=*/false,
11003                                       /*is_namespace=*/false,
11004                                       /*check_dependency=*/true,
11005                                       /*ambiguous_decls=*/NULL,
11006                                       id_expr_start_token->location);
11007
11008       if (expr
11009           && expr != error_mark_node
11010           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
11011           && TREE_CODE (expr) != TYPE_DECL
11012           && (TREE_CODE (expr) != BIT_NOT_EXPR
11013               || !TYPE_P (TREE_OPERAND (expr, 0)))
11014           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11015         {
11016           /* Complete lookup of the id-expression.  */
11017           expr = (finish_id_expression
11018                   (id_expression, expr, parser->scope, &idk,
11019                    /*integral_constant_expression_p=*/false,
11020                    /*allow_non_integral_constant_expression_p=*/true,
11021                    &non_integral_constant_expression_p,
11022                    /*template_p=*/false,
11023                    /*done=*/true,
11024                    /*address_p=*/false,
11025                    /*template_arg_p=*/false,
11026                    &error_msg,
11027                    id_expr_start_token->location));
11028
11029           if (expr == error_mark_node)
11030             /* We found an id-expression, but it was something that we
11031                should not have found. This is an error, not something
11032                we can recover from, so note that we found an
11033                id-expression and we'll recover as gracefully as
11034                possible.  */
11035             id_expression_or_member_access_p = true;
11036         }
11037
11038       if (expr 
11039           && expr != error_mark_node
11040           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11041         /* We have an id-expression.  */
11042         id_expression_or_member_access_p = true;
11043     }
11044
11045   if (!id_expression_or_member_access_p)
11046     {
11047       /* Abort the id-expression parse.  */
11048       cp_parser_abort_tentative_parse (parser);
11049
11050       /* Parsing tentatively, again.  */
11051       cp_parser_parse_tentatively (parser);
11052
11053       /* Parse a class member access.  */
11054       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
11055                                            /*cast_p=*/false,
11056                                            /*member_access_only_p=*/true, NULL);
11057
11058       if (expr 
11059           && expr != error_mark_node
11060           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11061         /* We have an id-expression.  */
11062         id_expression_or_member_access_p = true;
11063     }
11064
11065   if (id_expression_or_member_access_p)
11066     /* We have parsed the complete id-expression or member access.  */
11067     cp_parser_parse_definitely (parser);
11068   else
11069     {
11070       bool saved_greater_than_is_operator_p;
11071
11072       /* Abort our attempt to parse an id-expression or member access
11073          expression.  */
11074       cp_parser_abort_tentative_parse (parser);
11075
11076       /* Within a parenthesized expression, a `>' token is always
11077          the greater-than operator.  */
11078       saved_greater_than_is_operator_p
11079         = parser->greater_than_is_operator_p;
11080       parser->greater_than_is_operator_p = true;
11081
11082       /* Parse a full expression.  */
11083       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11084
11085       /* The `>' token might be the end of a template-id or
11086          template-parameter-list now.  */
11087       parser->greater_than_is_operator_p
11088         = saved_greater_than_is_operator_p;
11089     }
11090
11091   /* Go back to evaluating expressions.  */
11092   --cp_unevaluated_operand;
11093   --c_inhibit_evaluation_warnings;
11094
11095   /* Restore the old message and the integral constant expression
11096      flags.  */
11097   parser->type_definition_forbidden_message = saved_message;
11098   parser->integral_constant_expression_p
11099     = saved_integral_constant_expression_p;
11100   parser->non_integral_constant_expression_p
11101     = saved_non_integral_constant_expression_p;
11102
11103   /* Parse to the closing `)'.  */
11104   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11105     {
11106       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11107                                              /*consume_paren=*/true);
11108       return error_mark_node;
11109     }
11110
11111   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11112                                tf_warning_or_error);
11113
11114   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11115      it again.  */
11116   start_token->type = CPP_DECLTYPE;
11117   start_token->u.value = expr;
11118   start_token->keyword = RID_MAX;
11119   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11120
11121   return expr;
11122 }
11123
11124 /* Special member functions [gram.special] */
11125
11126 /* Parse a conversion-function-id.
11127
11128    conversion-function-id:
11129      operator conversion-type-id
11130
11131    Returns an IDENTIFIER_NODE representing the operator.  */
11132
11133 static tree
11134 cp_parser_conversion_function_id (cp_parser* parser)
11135 {
11136   tree type;
11137   tree saved_scope;
11138   tree saved_qualifying_scope;
11139   tree saved_object_scope;
11140   tree pushed_scope = NULL_TREE;
11141
11142   /* Look for the `operator' token.  */
11143   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11144     return error_mark_node;
11145   /* When we parse the conversion-type-id, the current scope will be
11146      reset.  However, we need that information in able to look up the
11147      conversion function later, so we save it here.  */
11148   saved_scope = parser->scope;
11149   saved_qualifying_scope = parser->qualifying_scope;
11150   saved_object_scope = parser->object_scope;
11151   /* We must enter the scope of the class so that the names of
11152      entities declared within the class are available in the
11153      conversion-type-id.  For example, consider:
11154
11155        struct S {
11156          typedef int I;
11157          operator I();
11158        };
11159
11160        S::operator I() { ... }
11161
11162      In order to see that `I' is a type-name in the definition, we
11163      must be in the scope of `S'.  */
11164   if (saved_scope)
11165     pushed_scope = push_scope (saved_scope);
11166   /* Parse the conversion-type-id.  */
11167   type = cp_parser_conversion_type_id (parser);
11168   /* Leave the scope of the class, if any.  */
11169   if (pushed_scope)
11170     pop_scope (pushed_scope);
11171   /* Restore the saved scope.  */
11172   parser->scope = saved_scope;
11173   parser->qualifying_scope = saved_qualifying_scope;
11174   parser->object_scope = saved_object_scope;
11175   /* If the TYPE is invalid, indicate failure.  */
11176   if (type == error_mark_node)
11177     return error_mark_node;
11178   return mangle_conv_op_name_for_type (type);
11179 }
11180
11181 /* Parse a conversion-type-id:
11182
11183    conversion-type-id:
11184      type-specifier-seq conversion-declarator [opt]
11185
11186    Returns the TYPE specified.  */
11187
11188 static tree
11189 cp_parser_conversion_type_id (cp_parser* parser)
11190 {
11191   tree attributes;
11192   cp_decl_specifier_seq type_specifiers;
11193   cp_declarator *declarator;
11194   tree type_specified;
11195
11196   /* Parse the attributes.  */
11197   attributes = cp_parser_attributes_opt (parser);
11198   /* Parse the type-specifiers.  */
11199   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11200                                 /*is_trailing_return=*/false,
11201                                 &type_specifiers);
11202   /* If that didn't work, stop.  */
11203   if (type_specifiers.type == error_mark_node)
11204     return error_mark_node;
11205   /* Parse the conversion-declarator.  */
11206   declarator = cp_parser_conversion_declarator_opt (parser);
11207
11208   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11209                                     /*initialized=*/0, &attributes);
11210   if (attributes)
11211     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11212
11213   /* Don't give this error when parsing tentatively.  This happens to
11214      work because we always parse this definitively once.  */
11215   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11216       && type_uses_auto (type_specified))
11217     {
11218       error ("invalid use of %<auto%> in conversion operator");
11219       return error_mark_node;
11220     }
11221
11222   return type_specified;
11223 }
11224
11225 /* Parse an (optional) conversion-declarator.
11226
11227    conversion-declarator:
11228      ptr-operator conversion-declarator [opt]
11229
11230    */
11231
11232 static cp_declarator *
11233 cp_parser_conversion_declarator_opt (cp_parser* parser)
11234 {
11235   enum tree_code code;
11236   tree class_type;
11237   cp_cv_quals cv_quals;
11238
11239   /* We don't know if there's a ptr-operator next, or not.  */
11240   cp_parser_parse_tentatively (parser);
11241   /* Try the ptr-operator.  */
11242   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11243   /* If it worked, look for more conversion-declarators.  */
11244   if (cp_parser_parse_definitely (parser))
11245     {
11246       cp_declarator *declarator;
11247
11248       /* Parse another optional declarator.  */
11249       declarator = cp_parser_conversion_declarator_opt (parser);
11250
11251       return cp_parser_make_indirect_declarator
11252         (code, class_type, cv_quals, declarator);
11253    }
11254
11255   return NULL;
11256 }
11257
11258 /* Parse an (optional) ctor-initializer.
11259
11260    ctor-initializer:
11261      : mem-initializer-list
11262
11263    Returns TRUE iff the ctor-initializer was actually present.  */
11264
11265 static bool
11266 cp_parser_ctor_initializer_opt (cp_parser* parser)
11267 {
11268   /* If the next token is not a `:', then there is no
11269      ctor-initializer.  */
11270   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11271     {
11272       /* Do default initialization of any bases and members.  */
11273       if (DECL_CONSTRUCTOR_P (current_function_decl))
11274         finish_mem_initializers (NULL_TREE);
11275
11276       return false;
11277     }
11278
11279   /* Consume the `:' token.  */
11280   cp_lexer_consume_token (parser->lexer);
11281   /* And the mem-initializer-list.  */
11282   cp_parser_mem_initializer_list (parser);
11283
11284   return true;
11285 }
11286
11287 /* Parse a mem-initializer-list.
11288
11289    mem-initializer-list:
11290      mem-initializer ... [opt]
11291      mem-initializer ... [opt] , mem-initializer-list  */
11292
11293 static void
11294 cp_parser_mem_initializer_list (cp_parser* parser)
11295 {
11296   tree mem_initializer_list = NULL_TREE;
11297   cp_token *token = cp_lexer_peek_token (parser->lexer);
11298
11299   /* Let the semantic analysis code know that we are starting the
11300      mem-initializer-list.  */
11301   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11302     error_at (token->location,
11303               "only constructors take member initializers");
11304
11305   /* Loop through the list.  */
11306   while (true)
11307     {
11308       tree mem_initializer;
11309
11310       token = cp_lexer_peek_token (parser->lexer);
11311       /* Parse the mem-initializer.  */
11312       mem_initializer = cp_parser_mem_initializer (parser);
11313       /* If the next token is a `...', we're expanding member initializers. */
11314       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11315         {
11316           /* Consume the `...'. */
11317           cp_lexer_consume_token (parser->lexer);
11318
11319           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11320              can be expanded but members cannot. */
11321           if (mem_initializer != error_mark_node
11322               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11323             {
11324               error_at (token->location,
11325                         "cannot expand initializer for member %<%D%>",
11326                         TREE_PURPOSE (mem_initializer));
11327               mem_initializer = error_mark_node;
11328             }
11329
11330           /* Construct the pack expansion type. */
11331           if (mem_initializer != error_mark_node)
11332             mem_initializer = make_pack_expansion (mem_initializer);
11333         }
11334       /* Add it to the list, unless it was erroneous.  */
11335       if (mem_initializer != error_mark_node)
11336         {
11337           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11338           mem_initializer_list = mem_initializer;
11339         }
11340       /* If the next token is not a `,', we're done.  */
11341       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11342         break;
11343       /* Consume the `,' token.  */
11344       cp_lexer_consume_token (parser->lexer);
11345     }
11346
11347   /* Perform semantic analysis.  */
11348   if (DECL_CONSTRUCTOR_P (current_function_decl))
11349     finish_mem_initializers (mem_initializer_list);
11350 }
11351
11352 /* Parse a mem-initializer.
11353
11354    mem-initializer:
11355      mem-initializer-id ( expression-list [opt] )
11356      mem-initializer-id braced-init-list
11357
11358    GNU extension:
11359
11360    mem-initializer:
11361      ( expression-list [opt] )
11362
11363    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11364    class) or FIELD_DECL (for a non-static data member) to initialize;
11365    the TREE_VALUE is the expression-list.  An empty initialization
11366    list is represented by void_list_node.  */
11367
11368 static tree
11369 cp_parser_mem_initializer (cp_parser* parser)
11370 {
11371   tree mem_initializer_id;
11372   tree expression_list;
11373   tree member;
11374   cp_token *token = cp_lexer_peek_token (parser->lexer);
11375
11376   /* Find out what is being initialized.  */
11377   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11378     {
11379       permerror (token->location,
11380                  "anachronistic old-style base class initializer");
11381       mem_initializer_id = NULL_TREE;
11382     }
11383   else
11384     {
11385       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11386       if (mem_initializer_id == error_mark_node)
11387         return mem_initializer_id;
11388     }
11389   member = expand_member_init (mem_initializer_id);
11390   if (member && !DECL_P (member))
11391     in_base_initializer = 1;
11392
11393   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11394     {
11395       bool expr_non_constant_p;
11396       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11397       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11398       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11399       expression_list = build_tree_list (NULL_TREE, expression_list);
11400     }
11401   else
11402     {
11403       VEC(tree,gc)* vec;
11404       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11405                                                      /*cast_p=*/false,
11406                                                      /*allow_expansion_p=*/true,
11407                                                      /*non_constant_p=*/NULL);
11408       if (vec == NULL)
11409         return error_mark_node;
11410       expression_list = build_tree_list_vec (vec);
11411       release_tree_vector (vec);
11412     }
11413
11414   if (expression_list == error_mark_node)
11415     return error_mark_node;
11416   if (!expression_list)
11417     expression_list = void_type_node;
11418
11419   in_base_initializer = 0;
11420
11421   return member ? build_tree_list (member, expression_list) : error_mark_node;
11422 }
11423
11424 /* Parse a mem-initializer-id.
11425
11426    mem-initializer-id:
11427      :: [opt] nested-name-specifier [opt] class-name
11428      identifier
11429
11430    Returns a TYPE indicating the class to be initializer for the first
11431    production.  Returns an IDENTIFIER_NODE indicating the data member
11432    to be initialized for the second production.  */
11433
11434 static tree
11435 cp_parser_mem_initializer_id (cp_parser* parser)
11436 {
11437   bool global_scope_p;
11438   bool nested_name_specifier_p;
11439   bool template_p = false;
11440   tree id;
11441
11442   cp_token *token = cp_lexer_peek_token (parser->lexer);
11443
11444   /* `typename' is not allowed in this context ([temp.res]).  */
11445   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11446     {
11447       error_at (token->location, 
11448                 "keyword %<typename%> not allowed in this context (a qualified "
11449                 "member initializer is implicitly a type)");
11450       cp_lexer_consume_token (parser->lexer);
11451     }
11452   /* Look for the optional `::' operator.  */
11453   global_scope_p
11454     = (cp_parser_global_scope_opt (parser,
11455                                    /*current_scope_valid_p=*/false)
11456        != NULL_TREE);
11457   /* Look for the optional nested-name-specifier.  The simplest way to
11458      implement:
11459
11460        [temp.res]
11461
11462        The keyword `typename' is not permitted in a base-specifier or
11463        mem-initializer; in these contexts a qualified name that
11464        depends on a template-parameter is implicitly assumed to be a
11465        type name.
11466
11467      is to assume that we have seen the `typename' keyword at this
11468      point.  */
11469   nested_name_specifier_p
11470     = (cp_parser_nested_name_specifier_opt (parser,
11471                                             /*typename_keyword_p=*/true,
11472                                             /*check_dependency_p=*/true,
11473                                             /*type_p=*/true,
11474                                             /*is_declaration=*/true)
11475        != NULL_TREE);
11476   if (nested_name_specifier_p)
11477     template_p = cp_parser_optional_template_keyword (parser);
11478   /* If there is a `::' operator or a nested-name-specifier, then we
11479      are definitely looking for a class-name.  */
11480   if (global_scope_p || nested_name_specifier_p)
11481     return cp_parser_class_name (parser,
11482                                  /*typename_keyword_p=*/true,
11483                                  /*template_keyword_p=*/template_p,
11484                                  typename_type,
11485                                  /*check_dependency_p=*/true,
11486                                  /*class_head_p=*/false,
11487                                  /*is_declaration=*/true);
11488   /* Otherwise, we could also be looking for an ordinary identifier.  */
11489   cp_parser_parse_tentatively (parser);
11490   /* Try a class-name.  */
11491   id = cp_parser_class_name (parser,
11492                              /*typename_keyword_p=*/true,
11493                              /*template_keyword_p=*/false,
11494                              none_type,
11495                              /*check_dependency_p=*/true,
11496                              /*class_head_p=*/false,
11497                              /*is_declaration=*/true);
11498   /* If we found one, we're done.  */
11499   if (cp_parser_parse_definitely (parser))
11500     return id;
11501   /* Otherwise, look for an ordinary identifier.  */
11502   return cp_parser_identifier (parser);
11503 }
11504
11505 /* Overloading [gram.over] */
11506
11507 /* Parse an operator-function-id.
11508
11509    operator-function-id:
11510      operator operator
11511
11512    Returns an IDENTIFIER_NODE for the operator which is a
11513    human-readable spelling of the identifier, e.g., `operator +'.  */
11514
11515 static tree
11516 cp_parser_operator_function_id (cp_parser* parser)
11517 {
11518   /* Look for the `operator' keyword.  */
11519   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11520     return error_mark_node;
11521   /* And then the name of the operator itself.  */
11522   return cp_parser_operator (parser);
11523 }
11524
11525 /* Return an identifier node for a user-defined literal operator.
11526    The suffix identifier is chained to the operator name identifier.  */
11527
11528 static tree
11529 cp_literal_operator_id (const char* name)
11530 {
11531   tree identifier;
11532   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11533                               + strlen (name) + 10);
11534   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11535   identifier = get_identifier (buffer);
11536   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11537
11538   return identifier;
11539 }
11540
11541 /* Parse an operator.
11542
11543    operator:
11544      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11545      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11546      || ++ -- , ->* -> () []
11547
11548    GNU Extensions:
11549
11550    operator:
11551      <? >? <?= >?=
11552
11553    Returns an IDENTIFIER_NODE for the operator which is a
11554    human-readable spelling of the identifier, e.g., `operator +'.  */
11555
11556 static tree
11557 cp_parser_operator (cp_parser* parser)
11558 {
11559   tree id = NULL_TREE;
11560   cp_token *token;
11561
11562   /* Peek at the next token.  */
11563   token = cp_lexer_peek_token (parser->lexer);
11564   /* Figure out which operator we have.  */
11565   switch (token->type)
11566     {
11567     case CPP_KEYWORD:
11568       {
11569         enum tree_code op;
11570
11571         /* The keyword should be either `new' or `delete'.  */
11572         if (token->keyword == RID_NEW)
11573           op = NEW_EXPR;
11574         else if (token->keyword == RID_DELETE)
11575           op = DELETE_EXPR;
11576         else
11577           break;
11578
11579         /* Consume the `new' or `delete' token.  */
11580         cp_lexer_consume_token (parser->lexer);
11581
11582         /* Peek at the next token.  */
11583         token = cp_lexer_peek_token (parser->lexer);
11584         /* If it's a `[' token then this is the array variant of the
11585            operator.  */
11586         if (token->type == CPP_OPEN_SQUARE)
11587           {
11588             /* Consume the `[' token.  */
11589             cp_lexer_consume_token (parser->lexer);
11590             /* Look for the `]' token.  */
11591             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11592             id = ansi_opname (op == NEW_EXPR
11593                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11594           }
11595         /* Otherwise, we have the non-array variant.  */
11596         else
11597           id = ansi_opname (op);
11598
11599         return id;
11600       }
11601
11602     case CPP_PLUS:
11603       id = ansi_opname (PLUS_EXPR);
11604       break;
11605
11606     case CPP_MINUS:
11607       id = ansi_opname (MINUS_EXPR);
11608       break;
11609
11610     case CPP_MULT:
11611       id = ansi_opname (MULT_EXPR);
11612       break;
11613
11614     case CPP_DIV:
11615       id = ansi_opname (TRUNC_DIV_EXPR);
11616       break;
11617
11618     case CPP_MOD:
11619       id = ansi_opname (TRUNC_MOD_EXPR);
11620       break;
11621
11622     case CPP_XOR:
11623       id = ansi_opname (BIT_XOR_EXPR);
11624       break;
11625
11626     case CPP_AND:
11627       id = ansi_opname (BIT_AND_EXPR);
11628       break;
11629
11630     case CPP_OR:
11631       id = ansi_opname (BIT_IOR_EXPR);
11632       break;
11633
11634     case CPP_COMPL:
11635       id = ansi_opname (BIT_NOT_EXPR);
11636       break;
11637
11638     case CPP_NOT:
11639       id = ansi_opname (TRUTH_NOT_EXPR);
11640       break;
11641
11642     case CPP_EQ:
11643       id = ansi_assopname (NOP_EXPR);
11644       break;
11645
11646     case CPP_LESS:
11647       id = ansi_opname (LT_EXPR);
11648       break;
11649
11650     case CPP_GREATER:
11651       id = ansi_opname (GT_EXPR);
11652       break;
11653
11654     case CPP_PLUS_EQ:
11655       id = ansi_assopname (PLUS_EXPR);
11656       break;
11657
11658     case CPP_MINUS_EQ:
11659       id = ansi_assopname (MINUS_EXPR);
11660       break;
11661
11662     case CPP_MULT_EQ:
11663       id = ansi_assopname (MULT_EXPR);
11664       break;
11665
11666     case CPP_DIV_EQ:
11667       id = ansi_assopname (TRUNC_DIV_EXPR);
11668       break;
11669
11670     case CPP_MOD_EQ:
11671       id = ansi_assopname (TRUNC_MOD_EXPR);
11672       break;
11673
11674     case CPP_XOR_EQ:
11675       id = ansi_assopname (BIT_XOR_EXPR);
11676       break;
11677
11678     case CPP_AND_EQ:
11679       id = ansi_assopname (BIT_AND_EXPR);
11680       break;
11681
11682     case CPP_OR_EQ:
11683       id = ansi_assopname (BIT_IOR_EXPR);
11684       break;
11685
11686     case CPP_LSHIFT:
11687       id = ansi_opname (LSHIFT_EXPR);
11688       break;
11689
11690     case CPP_RSHIFT:
11691       id = ansi_opname (RSHIFT_EXPR);
11692       break;
11693
11694     case CPP_LSHIFT_EQ:
11695       id = ansi_assopname (LSHIFT_EXPR);
11696       break;
11697
11698     case CPP_RSHIFT_EQ:
11699       id = ansi_assopname (RSHIFT_EXPR);
11700       break;
11701
11702     case CPP_EQ_EQ:
11703       id = ansi_opname (EQ_EXPR);
11704       break;
11705
11706     case CPP_NOT_EQ:
11707       id = ansi_opname (NE_EXPR);
11708       break;
11709
11710     case CPP_LESS_EQ:
11711       id = ansi_opname (LE_EXPR);
11712       break;
11713
11714     case CPP_GREATER_EQ:
11715       id = ansi_opname (GE_EXPR);
11716       break;
11717
11718     case CPP_AND_AND:
11719       id = ansi_opname (TRUTH_ANDIF_EXPR);
11720       break;
11721
11722     case CPP_OR_OR:
11723       id = ansi_opname (TRUTH_ORIF_EXPR);
11724       break;
11725
11726     case CPP_PLUS_PLUS:
11727       id = ansi_opname (POSTINCREMENT_EXPR);
11728       break;
11729
11730     case CPP_MINUS_MINUS:
11731       id = ansi_opname (PREDECREMENT_EXPR);
11732       break;
11733
11734     case CPP_COMMA:
11735       id = ansi_opname (COMPOUND_EXPR);
11736       break;
11737
11738     case CPP_DEREF_STAR:
11739       id = ansi_opname (MEMBER_REF);
11740       break;
11741
11742     case CPP_DEREF:
11743       id = ansi_opname (COMPONENT_REF);
11744       break;
11745
11746     case CPP_OPEN_PAREN:
11747       /* Consume the `('.  */
11748       cp_lexer_consume_token (parser->lexer);
11749       /* Look for the matching `)'.  */
11750       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11751       return ansi_opname (CALL_EXPR);
11752
11753     case CPP_OPEN_SQUARE:
11754       /* Consume the `['.  */
11755       cp_lexer_consume_token (parser->lexer);
11756       /* Look for the matching `]'.  */
11757       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11758       return ansi_opname (ARRAY_REF);
11759
11760     case CPP_STRING:
11761       if (cxx_dialect == cxx98)
11762         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11763       if (TREE_STRING_LENGTH (token->u.value) > 2)
11764         {
11765           error ("expected empty string after %<operator%> keyword");
11766           return error_mark_node;
11767         }
11768       /* Consume the string.  */
11769       cp_lexer_consume_token (parser->lexer);
11770       /* Look for the suffix identifier.  */
11771       token = cp_lexer_peek_token (parser->lexer);
11772       if (token->type == CPP_NAME)
11773         {
11774           id = cp_parser_identifier (parser);
11775           if (id != error_mark_node)
11776             {
11777               const char *name = IDENTIFIER_POINTER (id);
11778               return cp_literal_operator_id (name);
11779             }
11780         }
11781       else
11782         {
11783           error ("expected suffix identifier");
11784           return error_mark_node;
11785         }
11786
11787     case CPP_STRING_USERDEF:
11788       error ("missing space between %<\"\"%> and suffix identifier");
11789       return error_mark_node;
11790
11791     default:
11792       /* Anything else is an error.  */
11793       break;
11794     }
11795
11796   /* If we have selected an identifier, we need to consume the
11797      operator token.  */
11798   if (id)
11799     cp_lexer_consume_token (parser->lexer);
11800   /* Otherwise, no valid operator name was present.  */
11801   else
11802     {
11803       cp_parser_error (parser, "expected operator");
11804       id = error_mark_node;
11805     }
11806
11807   return id;
11808 }
11809
11810 /* Parse a template-declaration.
11811
11812    template-declaration:
11813      export [opt] template < template-parameter-list > declaration
11814
11815    If MEMBER_P is TRUE, this template-declaration occurs within a
11816    class-specifier.
11817
11818    The grammar rule given by the standard isn't correct.  What
11819    is really meant is:
11820
11821    template-declaration:
11822      export [opt] template-parameter-list-seq
11823        decl-specifier-seq [opt] init-declarator [opt] ;
11824      export [opt] template-parameter-list-seq
11825        function-definition
11826
11827    template-parameter-list-seq:
11828      template-parameter-list-seq [opt]
11829      template < template-parameter-list >  */
11830
11831 static void
11832 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11833 {
11834   /* Check for `export'.  */
11835   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11836     {
11837       /* Consume the `export' token.  */
11838       cp_lexer_consume_token (parser->lexer);
11839       /* Warn that we do not support `export'.  */
11840       warning (0, "keyword %<export%> not implemented, and will be ignored");
11841     }
11842
11843   cp_parser_template_declaration_after_export (parser, member_p);
11844 }
11845
11846 /* Parse a template-parameter-list.
11847
11848    template-parameter-list:
11849      template-parameter
11850      template-parameter-list , template-parameter
11851
11852    Returns a TREE_LIST.  Each node represents a template parameter.
11853    The nodes are connected via their TREE_CHAINs.  */
11854
11855 static tree
11856 cp_parser_template_parameter_list (cp_parser* parser)
11857 {
11858   tree parameter_list = NULL_TREE;
11859
11860   begin_template_parm_list ();
11861
11862   /* The loop below parses the template parms.  We first need to know
11863      the total number of template parms to be able to compute proper
11864      canonical types of each dependent type. So after the loop, when
11865      we know the total number of template parms,
11866      end_template_parm_list computes the proper canonical types and
11867      fixes up the dependent types accordingly.  */
11868   while (true)
11869     {
11870       tree parameter;
11871       bool is_non_type;
11872       bool is_parameter_pack;
11873       location_t parm_loc;
11874
11875       /* Parse the template-parameter.  */
11876       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11877       parameter = cp_parser_template_parameter (parser, 
11878                                                 &is_non_type,
11879                                                 &is_parameter_pack);
11880       /* Add it to the list.  */
11881       if (parameter != error_mark_node)
11882         parameter_list = process_template_parm (parameter_list,
11883                                                 parm_loc,
11884                                                 parameter,
11885                                                 is_non_type,
11886                                                 is_parameter_pack,
11887                                                 0);
11888       else
11889        {
11890          tree err_parm = build_tree_list (parameter, parameter);
11891          parameter_list = chainon (parameter_list, err_parm);
11892        }
11893
11894       /* If the next token is not a `,', we're done.  */
11895       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11896         break;
11897       /* Otherwise, consume the `,' token.  */
11898       cp_lexer_consume_token (parser->lexer);
11899     }
11900
11901   return end_template_parm_list (parameter_list);
11902 }
11903
11904 /* Parse a template-parameter.
11905
11906    template-parameter:
11907      type-parameter
11908      parameter-declaration
11909
11910    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11911    the parameter.  The TREE_PURPOSE is the default value, if any.
11912    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11913    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11914    set to true iff this parameter is a parameter pack. */
11915
11916 static tree
11917 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11918                               bool *is_parameter_pack)
11919 {
11920   cp_token *token;
11921   cp_parameter_declarator *parameter_declarator;
11922   cp_declarator *id_declarator;
11923   tree parm;
11924
11925   /* Assume it is a type parameter or a template parameter.  */
11926   *is_non_type = false;
11927   /* Assume it not a parameter pack. */
11928   *is_parameter_pack = false;
11929   /* Peek at the next token.  */
11930   token = cp_lexer_peek_token (parser->lexer);
11931   /* If it is `class' or `template', we have a type-parameter.  */
11932   if (token->keyword == RID_TEMPLATE)
11933     return cp_parser_type_parameter (parser, is_parameter_pack);
11934   /* If it is `class' or `typename' we do not know yet whether it is a
11935      type parameter or a non-type parameter.  Consider:
11936
11937        template <typename T, typename T::X X> ...
11938
11939      or:
11940
11941        template <class C, class D*> ...
11942
11943      Here, the first parameter is a type parameter, and the second is
11944      a non-type parameter.  We can tell by looking at the token after
11945      the identifier -- if it is a `,', `=', or `>' then we have a type
11946      parameter.  */
11947   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11948     {
11949       /* Peek at the token after `class' or `typename'.  */
11950       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11951       /* If it's an ellipsis, we have a template type parameter
11952          pack. */
11953       if (token->type == CPP_ELLIPSIS)
11954         return cp_parser_type_parameter (parser, is_parameter_pack);
11955       /* If it's an identifier, skip it.  */
11956       if (token->type == CPP_NAME)
11957         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11958       /* Now, see if the token looks like the end of a template
11959          parameter.  */
11960       if (token->type == CPP_COMMA
11961           || token->type == CPP_EQ
11962           || token->type == CPP_GREATER)
11963         return cp_parser_type_parameter (parser, is_parameter_pack);
11964     }
11965
11966   /* Otherwise, it is a non-type parameter.
11967
11968      [temp.param]
11969
11970      When parsing a default template-argument for a non-type
11971      template-parameter, the first non-nested `>' is taken as the end
11972      of the template parameter-list rather than a greater-than
11973      operator.  */
11974   *is_non_type = true;
11975   parameter_declarator
11976      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11977                                         /*parenthesized_p=*/NULL);
11978
11979   /* If the parameter declaration is marked as a parameter pack, set
11980      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11981      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11982      grokdeclarator. */
11983   if (parameter_declarator
11984       && parameter_declarator->declarator
11985       && parameter_declarator->declarator->parameter_pack_p)
11986     {
11987       *is_parameter_pack = true;
11988       parameter_declarator->declarator->parameter_pack_p = false;
11989     }
11990
11991   /* If the next token is an ellipsis, and we don't already have it
11992      marked as a parameter pack, then we have a parameter pack (that
11993      has no declarator).  */
11994   if (!*is_parameter_pack
11995       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11996       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11997     {
11998       /* Consume the `...'.  */
11999       cp_lexer_consume_token (parser->lexer);
12000       maybe_warn_variadic_templates ();
12001       
12002       *is_parameter_pack = true;
12003     }
12004   /* We might end up with a pack expansion as the type of the non-type
12005      template parameter, in which case this is a non-type template
12006      parameter pack.  */
12007   else if (parameter_declarator
12008            && parameter_declarator->decl_specifiers.type
12009            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
12010     {
12011       *is_parameter_pack = true;
12012       parameter_declarator->decl_specifiers.type = 
12013         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
12014     }
12015
12016   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12017     {
12018       /* Parameter packs cannot have default arguments.  However, a
12019          user may try to do so, so we'll parse them and give an
12020          appropriate diagnostic here.  */
12021
12022       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
12023       
12024       /* Find the name of the parameter pack.  */     
12025       id_declarator = parameter_declarator->declarator;
12026       while (id_declarator && id_declarator->kind != cdk_id)
12027         id_declarator = id_declarator->declarator;
12028       
12029       if (id_declarator && id_declarator->kind == cdk_id)
12030         error_at (start_token->location,
12031                   "template parameter pack %qD cannot have a default argument",
12032                   id_declarator->u.id.unqualified_name);
12033       else
12034         error_at (start_token->location,
12035                   "template parameter pack cannot have a default argument");
12036       
12037       /* Parse the default argument, but throw away the result.  */
12038       cp_parser_default_argument (parser, /*template_parm_p=*/true);
12039     }
12040
12041   parm = grokdeclarator (parameter_declarator->declarator,
12042                          &parameter_declarator->decl_specifiers,
12043                          TPARM, /*initialized=*/0,
12044                          /*attrlist=*/NULL);
12045   if (parm == error_mark_node)
12046     return error_mark_node;
12047
12048   return build_tree_list (parameter_declarator->default_argument, parm);
12049 }
12050
12051 /* Parse a type-parameter.
12052
12053    type-parameter:
12054      class identifier [opt]
12055      class identifier [opt] = type-id
12056      typename identifier [opt]
12057      typename identifier [opt] = type-id
12058      template < template-parameter-list > class identifier [opt]
12059      template < template-parameter-list > class identifier [opt]
12060        = id-expression
12061
12062    GNU Extension (variadic templates):
12063
12064    type-parameter:
12065      class ... identifier [opt]
12066      typename ... identifier [opt]
12067
12068    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12069    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12070    the declaration of the parameter.
12071
12072    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12073
12074 static tree
12075 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12076 {
12077   cp_token *token;
12078   tree parameter;
12079
12080   /* Look for a keyword to tell us what kind of parameter this is.  */
12081   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12082   if (!token)
12083     return error_mark_node;
12084
12085   switch (token->keyword)
12086     {
12087     case RID_CLASS:
12088     case RID_TYPENAME:
12089       {
12090         tree identifier;
12091         tree default_argument;
12092
12093         /* If the next token is an ellipsis, we have a template
12094            argument pack. */
12095         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12096           {
12097             /* Consume the `...' token. */
12098             cp_lexer_consume_token (parser->lexer);
12099             maybe_warn_variadic_templates ();
12100
12101             *is_parameter_pack = true;
12102           }
12103
12104         /* If the next token is an identifier, then it names the
12105            parameter.  */
12106         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12107           identifier = cp_parser_identifier (parser);
12108         else
12109           identifier = NULL_TREE;
12110
12111         /* Create the parameter.  */
12112         parameter = finish_template_type_parm (class_type_node, identifier);
12113
12114         /* If the next token is an `=', we have a default argument.  */
12115         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12116           {
12117             /* Consume the `=' token.  */
12118             cp_lexer_consume_token (parser->lexer);
12119             /* Parse the default-argument.  */
12120             push_deferring_access_checks (dk_no_deferred);
12121             default_argument = cp_parser_type_id (parser);
12122
12123             /* Template parameter packs cannot have default
12124                arguments. */
12125             if (*is_parameter_pack)
12126               {
12127                 if (identifier)
12128                   error_at (token->location,
12129                             "template parameter pack %qD cannot have a "
12130                             "default argument", identifier);
12131                 else
12132                   error_at (token->location,
12133                             "template parameter packs cannot have "
12134                             "default arguments");
12135                 default_argument = NULL_TREE;
12136               }
12137             pop_deferring_access_checks ();
12138           }
12139         else
12140           default_argument = NULL_TREE;
12141
12142         /* Create the combined representation of the parameter and the
12143            default argument.  */
12144         parameter = build_tree_list (default_argument, parameter);
12145       }
12146       break;
12147
12148     case RID_TEMPLATE:
12149       {
12150         tree identifier;
12151         tree default_argument;
12152
12153         /* Look for the `<'.  */
12154         cp_parser_require (parser, CPP_LESS, RT_LESS);
12155         /* Parse the template-parameter-list.  */
12156         cp_parser_template_parameter_list (parser);
12157         /* Look for the `>'.  */
12158         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12159         /* Look for the `class' keyword.  */
12160         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12161         /* If the next token is an ellipsis, we have a template
12162            argument pack. */
12163         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12164           {
12165             /* Consume the `...' token. */
12166             cp_lexer_consume_token (parser->lexer);
12167             maybe_warn_variadic_templates ();
12168
12169             *is_parameter_pack = true;
12170           }
12171         /* If the next token is an `=', then there is a
12172            default-argument.  If the next token is a `>', we are at
12173            the end of the parameter-list.  If the next token is a `,',
12174            then we are at the end of this parameter.  */
12175         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12176             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12177             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12178           {
12179             identifier = cp_parser_identifier (parser);
12180             /* Treat invalid names as if the parameter were nameless.  */
12181             if (identifier == error_mark_node)
12182               identifier = NULL_TREE;
12183           }
12184         else
12185           identifier = NULL_TREE;
12186
12187         /* Create the template parameter.  */
12188         parameter = finish_template_template_parm (class_type_node,
12189                                                    identifier);
12190
12191         /* If the next token is an `=', then there is a
12192            default-argument.  */
12193         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12194           {
12195             bool is_template;
12196
12197             /* Consume the `='.  */
12198             cp_lexer_consume_token (parser->lexer);
12199             /* Parse the id-expression.  */
12200             push_deferring_access_checks (dk_no_deferred);
12201             /* save token before parsing the id-expression, for error
12202                reporting */
12203             token = cp_lexer_peek_token (parser->lexer);
12204             default_argument
12205               = cp_parser_id_expression (parser,
12206                                          /*template_keyword_p=*/false,
12207                                          /*check_dependency_p=*/true,
12208                                          /*template_p=*/&is_template,
12209                                          /*declarator_p=*/false,
12210                                          /*optional_p=*/false);
12211             if (TREE_CODE (default_argument) == TYPE_DECL)
12212               /* If the id-expression was a template-id that refers to
12213                  a template-class, we already have the declaration here,
12214                  so no further lookup is needed.  */
12215                  ;
12216             else
12217               /* Look up the name.  */
12218               default_argument
12219                 = cp_parser_lookup_name (parser, default_argument,
12220                                          none_type,
12221                                          /*is_template=*/is_template,
12222                                          /*is_namespace=*/false,
12223                                          /*check_dependency=*/true,
12224                                          /*ambiguous_decls=*/NULL,
12225                                          token->location);
12226             /* See if the default argument is valid.  */
12227             default_argument
12228               = check_template_template_default_arg (default_argument);
12229
12230             /* Template parameter packs cannot have default
12231                arguments. */
12232             if (*is_parameter_pack)
12233               {
12234                 if (identifier)
12235                   error_at (token->location,
12236                             "template parameter pack %qD cannot "
12237                             "have a default argument",
12238                             identifier);
12239                 else
12240                   error_at (token->location, "template parameter packs cannot "
12241                             "have default arguments");
12242                 default_argument = NULL_TREE;
12243               }
12244             pop_deferring_access_checks ();
12245           }
12246         else
12247           default_argument = NULL_TREE;
12248
12249         /* Create the combined representation of the parameter and the
12250            default argument.  */
12251         parameter = build_tree_list (default_argument, parameter);
12252       }
12253       break;
12254
12255     default:
12256       gcc_unreachable ();
12257       break;
12258     }
12259
12260   return parameter;
12261 }
12262
12263 /* Parse a template-id.
12264
12265    template-id:
12266      template-name < template-argument-list [opt] >
12267
12268    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12269    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12270    returned.  Otherwise, if the template-name names a function, or set
12271    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12272    names a class, returns a TYPE_DECL for the specialization.
12273
12274    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12275    uninstantiated templates.  */
12276
12277 static tree
12278 cp_parser_template_id (cp_parser *parser,
12279                        bool template_keyword_p,
12280                        bool check_dependency_p,
12281                        bool is_declaration)
12282 {
12283   int i;
12284   tree templ;
12285   tree arguments;
12286   tree template_id;
12287   cp_token_position start_of_id = 0;
12288   deferred_access_check *chk;
12289   VEC (deferred_access_check,gc) *access_check;
12290   cp_token *next_token = NULL, *next_token_2 = NULL;
12291   bool is_identifier;
12292
12293   /* If the next token corresponds to a template-id, there is no need
12294      to reparse it.  */
12295   next_token = cp_lexer_peek_token (parser->lexer);
12296   if (next_token->type == CPP_TEMPLATE_ID)
12297     {
12298       struct tree_check *check_value;
12299
12300       /* Get the stored value.  */
12301       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12302       /* Perform any access checks that were deferred.  */
12303       access_check = check_value->checks;
12304       if (access_check)
12305         {
12306           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12307             perform_or_defer_access_check (chk->binfo,
12308                                            chk->decl,
12309                                            chk->diag_decl);
12310         }
12311       /* Return the stored value.  */
12312       return check_value->value;
12313     }
12314
12315   /* Avoid performing name lookup if there is no possibility of
12316      finding a template-id.  */
12317   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12318       || (next_token->type == CPP_NAME
12319           && !cp_parser_nth_token_starts_template_argument_list_p
12320                (parser, 2)))
12321     {
12322       cp_parser_error (parser, "expected template-id");
12323       return error_mark_node;
12324     }
12325
12326   /* Remember where the template-id starts.  */
12327   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12328     start_of_id = cp_lexer_token_position (parser->lexer, false);
12329
12330   push_deferring_access_checks (dk_deferred);
12331
12332   /* Parse the template-name.  */
12333   is_identifier = false;
12334   templ = cp_parser_template_name (parser, template_keyword_p,
12335                                    check_dependency_p,
12336                                    is_declaration,
12337                                    &is_identifier);
12338   if (templ == error_mark_node || is_identifier)
12339     {
12340       pop_deferring_access_checks ();
12341       return templ;
12342     }
12343
12344   /* If we find the sequence `[:' after a template-name, it's probably
12345      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12346      parse correctly the argument list.  */
12347   next_token = cp_lexer_peek_token (parser->lexer);
12348   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12349   if (next_token->type == CPP_OPEN_SQUARE
12350       && next_token->flags & DIGRAPH
12351       && next_token_2->type == CPP_COLON
12352       && !(next_token_2->flags & PREV_WHITE))
12353     {
12354       cp_parser_parse_tentatively (parser);
12355       /* Change `:' into `::'.  */
12356       next_token_2->type = CPP_SCOPE;
12357       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12358          CPP_LESS.  */
12359       cp_lexer_consume_token (parser->lexer);
12360
12361       /* Parse the arguments.  */
12362       arguments = cp_parser_enclosed_template_argument_list (parser);
12363       if (!cp_parser_parse_definitely (parser))
12364         {
12365           /* If we couldn't parse an argument list, then we revert our changes
12366              and return simply an error. Maybe this is not a template-id
12367              after all.  */
12368           next_token_2->type = CPP_COLON;
12369           cp_parser_error (parser, "expected %<<%>");
12370           pop_deferring_access_checks ();
12371           return error_mark_node;
12372         }
12373       /* Otherwise, emit an error about the invalid digraph, but continue
12374          parsing because we got our argument list.  */
12375       if (permerror (next_token->location,
12376                      "%<<::%> cannot begin a template-argument list"))
12377         {
12378           static bool hint = false;
12379           inform (next_token->location,
12380                   "%<<:%> is an alternate spelling for %<[%>."
12381                   " Insert whitespace between %<<%> and %<::%>");
12382           if (!hint && !flag_permissive)
12383             {
12384               inform (next_token->location, "(if you use %<-fpermissive%>"
12385                       " G++ will accept your code)");
12386               hint = true;
12387             }
12388         }
12389     }
12390   else
12391     {
12392       /* Look for the `<' that starts the template-argument-list.  */
12393       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12394         {
12395           pop_deferring_access_checks ();
12396           return error_mark_node;
12397         }
12398       /* Parse the arguments.  */
12399       arguments = cp_parser_enclosed_template_argument_list (parser);
12400     }
12401
12402   /* Build a representation of the specialization.  */
12403   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12404     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12405   else if (DECL_TYPE_TEMPLATE_P (templ)
12406            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12407     {
12408       bool entering_scope;
12409       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12410          template (rather than some instantiation thereof) only if
12411          is not nested within some other construct.  For example, in
12412          "template <typename T> void f(T) { A<T>::", A<T> is just an
12413          instantiation of A.  */
12414       entering_scope = (template_parm_scope_p ()
12415                         && cp_lexer_next_token_is (parser->lexer,
12416                                                    CPP_SCOPE));
12417       template_id
12418         = finish_template_type (templ, arguments, entering_scope);
12419     }
12420   else
12421     {
12422       /* If it's not a class-template or a template-template, it should be
12423          a function-template.  */
12424       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12425                    || TREE_CODE (templ) == OVERLOAD
12426                    || BASELINK_P (templ)));
12427
12428       template_id = lookup_template_function (templ, arguments);
12429     }
12430
12431   /* If parsing tentatively, replace the sequence of tokens that makes
12432      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12433      should we re-parse the token stream, we will not have to repeat
12434      the effort required to do the parse, nor will we issue duplicate
12435      error messages about problems during instantiation of the
12436      template.  */
12437   if (start_of_id)
12438     {
12439       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12440
12441       /* Reset the contents of the START_OF_ID token.  */
12442       token->type = CPP_TEMPLATE_ID;
12443       /* Retrieve any deferred checks.  Do not pop this access checks yet
12444          so the memory will not be reclaimed during token replacing below.  */
12445       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12446       token->u.tree_check_value->value = template_id;
12447       token->u.tree_check_value->checks = get_deferred_access_checks ();
12448       token->keyword = RID_MAX;
12449
12450       /* Purge all subsequent tokens.  */
12451       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12452
12453       /* ??? Can we actually assume that, if template_id ==
12454          error_mark_node, we will have issued a diagnostic to the
12455          user, as opposed to simply marking the tentative parse as
12456          failed?  */
12457       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12458         error_at (token->location, "parse error in template argument list");
12459     }
12460
12461   pop_deferring_access_checks ();
12462   return template_id;
12463 }
12464
12465 /* Parse a template-name.
12466
12467    template-name:
12468      identifier
12469
12470    The standard should actually say:
12471
12472    template-name:
12473      identifier
12474      operator-function-id
12475
12476    A defect report has been filed about this issue.
12477
12478    A conversion-function-id cannot be a template name because they cannot
12479    be part of a template-id. In fact, looking at this code:
12480
12481    a.operator K<int>()
12482
12483    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12484    It is impossible to call a templated conversion-function-id with an
12485    explicit argument list, since the only allowed template parameter is
12486    the type to which it is converting.
12487
12488    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12489    `template' keyword, in a construction like:
12490
12491      T::template f<3>()
12492
12493    In that case `f' is taken to be a template-name, even though there
12494    is no way of knowing for sure.
12495
12496    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12497    name refers to a set of overloaded functions, at least one of which
12498    is a template, or an IDENTIFIER_NODE with the name of the template,
12499    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12500    names are looked up inside uninstantiated templates.  */
12501
12502 static tree
12503 cp_parser_template_name (cp_parser* parser,
12504                          bool template_keyword_p,
12505                          bool check_dependency_p,
12506                          bool is_declaration,
12507                          bool *is_identifier)
12508 {
12509   tree identifier;
12510   tree decl;
12511   tree fns;
12512   cp_token *token = cp_lexer_peek_token (parser->lexer);
12513
12514   /* If the next token is `operator', then we have either an
12515      operator-function-id or a conversion-function-id.  */
12516   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12517     {
12518       /* We don't know whether we're looking at an
12519          operator-function-id or a conversion-function-id.  */
12520       cp_parser_parse_tentatively (parser);
12521       /* Try an operator-function-id.  */
12522       identifier = cp_parser_operator_function_id (parser);
12523       /* If that didn't work, try a conversion-function-id.  */
12524       if (!cp_parser_parse_definitely (parser))
12525         {
12526           cp_parser_error (parser, "expected template-name");
12527           return error_mark_node;
12528         }
12529     }
12530   /* Look for the identifier.  */
12531   else
12532     identifier = cp_parser_identifier (parser);
12533
12534   /* If we didn't find an identifier, we don't have a template-id.  */
12535   if (identifier == error_mark_node)
12536     return error_mark_node;
12537
12538   /* If the name immediately followed the `template' keyword, then it
12539      is a template-name.  However, if the next token is not `<', then
12540      we do not treat it as a template-name, since it is not being used
12541      as part of a template-id.  This enables us to handle constructs
12542      like:
12543
12544        template <typename T> struct S { S(); };
12545        template <typename T> S<T>::S();
12546
12547      correctly.  We would treat `S' as a template -- if it were `S<T>'
12548      -- but we do not if there is no `<'.  */
12549
12550   if (processing_template_decl
12551       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12552     {
12553       /* In a declaration, in a dependent context, we pretend that the
12554          "template" keyword was present in order to improve error
12555          recovery.  For example, given:
12556
12557            template <typename T> void f(T::X<int>);
12558
12559          we want to treat "X<int>" as a template-id.  */
12560       if (is_declaration
12561           && !template_keyword_p
12562           && parser->scope && TYPE_P (parser->scope)
12563           && check_dependency_p
12564           && dependent_scope_p (parser->scope)
12565           /* Do not do this for dtors (or ctors), since they never
12566              need the template keyword before their name.  */
12567           && !constructor_name_p (identifier, parser->scope))
12568         {
12569           cp_token_position start = 0;
12570
12571           /* Explain what went wrong.  */
12572           error_at (token->location, "non-template %qD used as template",
12573                     identifier);
12574           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12575                   parser->scope, identifier);
12576           /* If parsing tentatively, find the location of the "<" token.  */
12577           if (cp_parser_simulate_error (parser))
12578             start = cp_lexer_token_position (parser->lexer, true);
12579           /* Parse the template arguments so that we can issue error
12580              messages about them.  */
12581           cp_lexer_consume_token (parser->lexer);
12582           cp_parser_enclosed_template_argument_list (parser);
12583           /* Skip tokens until we find a good place from which to
12584              continue parsing.  */
12585           cp_parser_skip_to_closing_parenthesis (parser,
12586                                                  /*recovering=*/true,
12587                                                  /*or_comma=*/true,
12588                                                  /*consume_paren=*/false);
12589           /* If parsing tentatively, permanently remove the
12590              template argument list.  That will prevent duplicate
12591              error messages from being issued about the missing
12592              "template" keyword.  */
12593           if (start)
12594             cp_lexer_purge_tokens_after (parser->lexer, start);
12595           if (is_identifier)
12596             *is_identifier = true;
12597           return identifier;
12598         }
12599
12600       /* If the "template" keyword is present, then there is generally
12601          no point in doing name-lookup, so we just return IDENTIFIER.
12602          But, if the qualifying scope is non-dependent then we can
12603          (and must) do name-lookup normally.  */
12604       if (template_keyword_p
12605           && (!parser->scope
12606               || (TYPE_P (parser->scope)
12607                   && dependent_type_p (parser->scope))))
12608         return identifier;
12609     }
12610
12611   /* Look up the name.  */
12612   decl = cp_parser_lookup_name (parser, identifier,
12613                                 none_type,
12614                                 /*is_template=*/true,
12615                                 /*is_namespace=*/false,
12616                                 check_dependency_p,
12617                                 /*ambiguous_decls=*/NULL,
12618                                 token->location);
12619
12620   /* If DECL is a template, then the name was a template-name.  */
12621   if (TREE_CODE (decl) == TEMPLATE_DECL)
12622     ;
12623   else
12624     {
12625       tree fn = NULL_TREE;
12626
12627       /* The standard does not explicitly indicate whether a name that
12628          names a set of overloaded declarations, some of which are
12629          templates, is a template-name.  However, such a name should
12630          be a template-name; otherwise, there is no way to form a
12631          template-id for the overloaded templates.  */
12632       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12633       if (TREE_CODE (fns) == OVERLOAD)
12634         for (fn = fns; fn; fn = OVL_NEXT (fn))
12635           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12636             break;
12637
12638       if (!fn)
12639         {
12640           /* The name does not name a template.  */
12641           cp_parser_error (parser, "expected template-name");
12642           return error_mark_node;
12643         }
12644     }
12645
12646   /* If DECL is dependent, and refers to a function, then just return
12647      its name; we will look it up again during template instantiation.  */
12648   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12649     {
12650       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12651       if (TYPE_P (scope) && dependent_type_p (scope))
12652         return identifier;
12653     }
12654
12655   return decl;
12656 }
12657
12658 /* Parse a template-argument-list.
12659
12660    template-argument-list:
12661      template-argument ... [opt]
12662      template-argument-list , template-argument ... [opt]
12663
12664    Returns a TREE_VEC containing the arguments.  */
12665
12666 static tree
12667 cp_parser_template_argument_list (cp_parser* parser)
12668 {
12669   tree fixed_args[10];
12670   unsigned n_args = 0;
12671   unsigned alloced = 10;
12672   tree *arg_ary = fixed_args;
12673   tree vec;
12674   bool saved_in_template_argument_list_p;
12675   bool saved_ice_p;
12676   bool saved_non_ice_p;
12677
12678   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12679   parser->in_template_argument_list_p = true;
12680   /* Even if the template-id appears in an integral
12681      constant-expression, the contents of the argument list do
12682      not.  */
12683   saved_ice_p = parser->integral_constant_expression_p;
12684   parser->integral_constant_expression_p = false;
12685   saved_non_ice_p = parser->non_integral_constant_expression_p;
12686   parser->non_integral_constant_expression_p = false;
12687
12688   /* Parse the arguments.  */
12689   do
12690     {
12691       tree argument;
12692
12693       if (n_args)
12694         /* Consume the comma.  */
12695         cp_lexer_consume_token (parser->lexer);
12696
12697       /* Parse the template-argument.  */
12698       argument = cp_parser_template_argument (parser);
12699
12700       /* If the next token is an ellipsis, we're expanding a template
12701          argument pack. */
12702       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12703         {
12704           if (argument == error_mark_node)
12705             {
12706               cp_token *token = cp_lexer_peek_token (parser->lexer);
12707               error_at (token->location,
12708                         "expected parameter pack before %<...%>");
12709             }
12710           /* Consume the `...' token. */
12711           cp_lexer_consume_token (parser->lexer);
12712
12713           /* Make the argument into a TYPE_PACK_EXPANSION or
12714              EXPR_PACK_EXPANSION. */
12715           argument = make_pack_expansion (argument);
12716         }
12717
12718       if (n_args == alloced)
12719         {
12720           alloced *= 2;
12721
12722           if (arg_ary == fixed_args)
12723             {
12724               arg_ary = XNEWVEC (tree, alloced);
12725               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12726             }
12727           else
12728             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12729         }
12730       arg_ary[n_args++] = argument;
12731     }
12732   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12733
12734   vec = make_tree_vec (n_args);
12735
12736   while (n_args--)
12737     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12738
12739   if (arg_ary != fixed_args)
12740     free (arg_ary);
12741   parser->non_integral_constant_expression_p = saved_non_ice_p;
12742   parser->integral_constant_expression_p = saved_ice_p;
12743   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12744 #ifdef ENABLE_CHECKING
12745   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12746 #endif
12747   return vec;
12748 }
12749
12750 /* Parse a template-argument.
12751
12752    template-argument:
12753      assignment-expression
12754      type-id
12755      id-expression
12756
12757    The representation is that of an assignment-expression, type-id, or
12758    id-expression -- except that the qualified id-expression is
12759    evaluated, so that the value returned is either a DECL or an
12760    OVERLOAD.
12761
12762    Although the standard says "assignment-expression", it forbids
12763    throw-expressions or assignments in the template argument.
12764    Therefore, we use "conditional-expression" instead.  */
12765
12766 static tree
12767 cp_parser_template_argument (cp_parser* parser)
12768 {
12769   tree argument;
12770   bool template_p;
12771   bool address_p;
12772   bool maybe_type_id = false;
12773   cp_token *token = NULL, *argument_start_token = NULL;
12774   cp_id_kind idk;
12775
12776   /* There's really no way to know what we're looking at, so we just
12777      try each alternative in order.
12778
12779        [temp.arg]
12780
12781        In a template-argument, an ambiguity between a type-id and an
12782        expression is resolved to a type-id, regardless of the form of
12783        the corresponding template-parameter.
12784
12785      Therefore, we try a type-id first.  */
12786   cp_parser_parse_tentatively (parser);
12787   argument = cp_parser_template_type_arg (parser);
12788   /* If there was no error parsing the type-id but the next token is a
12789      '>>', our behavior depends on which dialect of C++ we're
12790      parsing. In C++98, we probably found a typo for '> >'. But there
12791      are type-id which are also valid expressions. For instance:
12792
12793      struct X { int operator >> (int); };
12794      template <int V> struct Foo {};
12795      Foo<X () >> 5> r;
12796
12797      Here 'X()' is a valid type-id of a function type, but the user just
12798      wanted to write the expression "X() >> 5". Thus, we remember that we
12799      found a valid type-id, but we still try to parse the argument as an
12800      expression to see what happens. 
12801
12802      In C++0x, the '>>' will be considered two separate '>'
12803      tokens.  */
12804   if (!cp_parser_error_occurred (parser)
12805       && cxx_dialect == cxx98
12806       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12807     {
12808       maybe_type_id = true;
12809       cp_parser_abort_tentative_parse (parser);
12810     }
12811   else
12812     {
12813       /* If the next token isn't a `,' or a `>', then this argument wasn't
12814       really finished. This means that the argument is not a valid
12815       type-id.  */
12816       if (!cp_parser_next_token_ends_template_argument_p (parser))
12817         cp_parser_error (parser, "expected template-argument");
12818       /* If that worked, we're done.  */
12819       if (cp_parser_parse_definitely (parser))
12820         return argument;
12821     }
12822   /* We're still not sure what the argument will be.  */
12823   cp_parser_parse_tentatively (parser);
12824   /* Try a template.  */
12825   argument_start_token = cp_lexer_peek_token (parser->lexer);
12826   argument = cp_parser_id_expression (parser,
12827                                       /*template_keyword_p=*/false,
12828                                       /*check_dependency_p=*/true,
12829                                       &template_p,
12830                                       /*declarator_p=*/false,
12831                                       /*optional_p=*/false);
12832   /* If the next token isn't a `,' or a `>', then this argument wasn't
12833      really finished.  */
12834   if (!cp_parser_next_token_ends_template_argument_p (parser))
12835     cp_parser_error (parser, "expected template-argument");
12836   if (!cp_parser_error_occurred (parser))
12837     {
12838       /* Figure out what is being referred to.  If the id-expression
12839          was for a class template specialization, then we will have a
12840          TYPE_DECL at this point.  There is no need to do name lookup
12841          at this point in that case.  */
12842       if (TREE_CODE (argument) != TYPE_DECL)
12843         argument = cp_parser_lookup_name (parser, argument,
12844                                           none_type,
12845                                           /*is_template=*/template_p,
12846                                           /*is_namespace=*/false,
12847                                           /*check_dependency=*/true,
12848                                           /*ambiguous_decls=*/NULL,
12849                                           argument_start_token->location);
12850       if (TREE_CODE (argument) != TEMPLATE_DECL
12851           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12852         cp_parser_error (parser, "expected template-name");
12853     }
12854   if (cp_parser_parse_definitely (parser))
12855     return argument;
12856   /* It must be a non-type argument.  There permitted cases are given
12857      in [temp.arg.nontype]:
12858
12859      -- an integral constant-expression of integral or enumeration
12860         type; or
12861
12862      -- the name of a non-type template-parameter; or
12863
12864      -- the name of an object or function with external linkage...
12865
12866      -- the address of an object or function with external linkage...
12867
12868      -- a pointer to member...  */
12869   /* Look for a non-type template parameter.  */
12870   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12871     {
12872       cp_parser_parse_tentatively (parser);
12873       argument = cp_parser_primary_expression (parser,
12874                                                /*address_p=*/false,
12875                                                /*cast_p=*/false,
12876                                                /*template_arg_p=*/true,
12877                                                &idk);
12878       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12879           || !cp_parser_next_token_ends_template_argument_p (parser))
12880         cp_parser_simulate_error (parser);
12881       if (cp_parser_parse_definitely (parser))
12882         return argument;
12883     }
12884
12885   /* If the next token is "&", the argument must be the address of an
12886      object or function with external linkage.  */
12887   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12888   if (address_p)
12889     cp_lexer_consume_token (parser->lexer);
12890   /* See if we might have an id-expression.  */
12891   token = cp_lexer_peek_token (parser->lexer);
12892   if (token->type == CPP_NAME
12893       || token->keyword == RID_OPERATOR
12894       || token->type == CPP_SCOPE
12895       || token->type == CPP_TEMPLATE_ID
12896       || token->type == CPP_NESTED_NAME_SPECIFIER)
12897     {
12898       cp_parser_parse_tentatively (parser);
12899       argument = cp_parser_primary_expression (parser,
12900                                                address_p,
12901                                                /*cast_p=*/false,
12902                                                /*template_arg_p=*/true,
12903                                                &idk);
12904       if (cp_parser_error_occurred (parser)
12905           || !cp_parser_next_token_ends_template_argument_p (parser))
12906         cp_parser_abort_tentative_parse (parser);
12907       else
12908         {
12909           tree probe;
12910
12911           if (TREE_CODE (argument) == INDIRECT_REF)
12912             {
12913               gcc_assert (REFERENCE_REF_P (argument));
12914               argument = TREE_OPERAND (argument, 0);
12915             }
12916
12917           /* If we're in a template, we represent a qualified-id referring
12918              to a static data member as a SCOPE_REF even if the scope isn't
12919              dependent so that we can check access control later.  */
12920           probe = argument;
12921           if (TREE_CODE (probe) == SCOPE_REF)
12922             probe = TREE_OPERAND (probe, 1);
12923           if (TREE_CODE (probe) == VAR_DECL)
12924             {
12925               /* A variable without external linkage might still be a
12926                  valid constant-expression, so no error is issued here
12927                  if the external-linkage check fails.  */
12928               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12929                 cp_parser_simulate_error (parser);
12930             }
12931           else if (is_overloaded_fn (argument))
12932             /* All overloaded functions are allowed; if the external
12933                linkage test does not pass, an error will be issued
12934                later.  */
12935             ;
12936           else if (address_p
12937                    && (TREE_CODE (argument) == OFFSET_REF
12938                        || TREE_CODE (argument) == SCOPE_REF))
12939             /* A pointer-to-member.  */
12940             ;
12941           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12942             ;
12943           else
12944             cp_parser_simulate_error (parser);
12945
12946           if (cp_parser_parse_definitely (parser))
12947             {
12948               if (address_p)
12949                 argument = build_x_unary_op (ADDR_EXPR, argument,
12950                                              tf_warning_or_error);
12951               return argument;
12952             }
12953         }
12954     }
12955   /* If the argument started with "&", there are no other valid
12956      alternatives at this point.  */
12957   if (address_p)
12958     {
12959       cp_parser_error (parser, "invalid non-type template argument");
12960       return error_mark_node;
12961     }
12962
12963   /* If the argument wasn't successfully parsed as a type-id followed
12964      by '>>', the argument can only be a constant expression now.
12965      Otherwise, we try parsing the constant-expression tentatively,
12966      because the argument could really be a type-id.  */
12967   if (maybe_type_id)
12968     cp_parser_parse_tentatively (parser);
12969   argument = cp_parser_constant_expression (parser,
12970                                             /*allow_non_constant_p=*/false,
12971                                             /*non_constant_p=*/NULL);
12972   argument = fold_non_dependent_expr (argument);
12973   if (!maybe_type_id)
12974     return argument;
12975   if (!cp_parser_next_token_ends_template_argument_p (parser))
12976     cp_parser_error (parser, "expected template-argument");
12977   if (cp_parser_parse_definitely (parser))
12978     return argument;
12979   /* We did our best to parse the argument as a non type-id, but that
12980      was the only alternative that matched (albeit with a '>' after
12981      it). We can assume it's just a typo from the user, and a
12982      diagnostic will then be issued.  */
12983   return cp_parser_template_type_arg (parser);
12984 }
12985
12986 /* Parse an explicit-instantiation.
12987
12988    explicit-instantiation:
12989      template declaration
12990
12991    Although the standard says `declaration', what it really means is:
12992
12993    explicit-instantiation:
12994      template decl-specifier-seq [opt] declarator [opt] ;
12995
12996    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12997    supposed to be allowed.  A defect report has been filed about this
12998    issue.
12999
13000    GNU Extension:
13001
13002    explicit-instantiation:
13003      storage-class-specifier template
13004        decl-specifier-seq [opt] declarator [opt] ;
13005      function-specifier template
13006        decl-specifier-seq [opt] declarator [opt] ;  */
13007
13008 static void
13009 cp_parser_explicit_instantiation (cp_parser* parser)
13010 {
13011   int declares_class_or_enum;
13012   cp_decl_specifier_seq decl_specifiers;
13013   tree extension_specifier = NULL_TREE;
13014
13015   timevar_push (TV_TEMPLATE_INST);
13016
13017   /* Look for an (optional) storage-class-specifier or
13018      function-specifier.  */
13019   if (cp_parser_allow_gnu_extensions_p (parser))
13020     {
13021       extension_specifier
13022         = cp_parser_storage_class_specifier_opt (parser);
13023       if (!extension_specifier)
13024         extension_specifier
13025           = cp_parser_function_specifier_opt (parser,
13026                                               /*decl_specs=*/NULL);
13027     }
13028
13029   /* Look for the `template' keyword.  */
13030   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13031   /* Let the front end know that we are processing an explicit
13032      instantiation.  */
13033   begin_explicit_instantiation ();
13034   /* [temp.explicit] says that we are supposed to ignore access
13035      control while processing explicit instantiation directives.  */
13036   push_deferring_access_checks (dk_no_check);
13037   /* Parse a decl-specifier-seq.  */
13038   cp_parser_decl_specifier_seq (parser,
13039                                 CP_PARSER_FLAGS_OPTIONAL,
13040                                 &decl_specifiers,
13041                                 &declares_class_or_enum);
13042   /* If there was exactly one decl-specifier, and it declared a class,
13043      and there's no declarator, then we have an explicit type
13044      instantiation.  */
13045   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
13046     {
13047       tree type;
13048
13049       type = check_tag_decl (&decl_specifiers);
13050       /* Turn access control back on for names used during
13051          template instantiation.  */
13052       pop_deferring_access_checks ();
13053       if (type)
13054         do_type_instantiation (type, extension_specifier,
13055                                /*complain=*/tf_error);
13056     }
13057   else
13058     {
13059       cp_declarator *declarator;
13060       tree decl;
13061
13062       /* Parse the declarator.  */
13063       declarator
13064         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13065                                 /*ctor_dtor_or_conv_p=*/NULL,
13066                                 /*parenthesized_p=*/NULL,
13067                                 /*member_p=*/false);
13068       if (declares_class_or_enum & 2)
13069         cp_parser_check_for_definition_in_return_type (declarator,
13070                                                        decl_specifiers.type,
13071                                                        decl_specifiers.type_location);
13072       if (declarator != cp_error_declarator)
13073         {
13074           if (decl_specifiers.specs[(int)ds_inline])
13075             permerror (input_location, "explicit instantiation shall not use"
13076                        " %<inline%> specifier");
13077           if (decl_specifiers.specs[(int)ds_constexpr])
13078             permerror (input_location, "explicit instantiation shall not use"
13079                        " %<constexpr%> specifier");
13080
13081           decl = grokdeclarator (declarator, &decl_specifiers,
13082                                  NORMAL, 0, &decl_specifiers.attributes);
13083           /* Turn access control back on for names used during
13084              template instantiation.  */
13085           pop_deferring_access_checks ();
13086           /* Do the explicit instantiation.  */
13087           do_decl_instantiation (decl, extension_specifier);
13088         }
13089       else
13090         {
13091           pop_deferring_access_checks ();
13092           /* Skip the body of the explicit instantiation.  */
13093           cp_parser_skip_to_end_of_statement (parser);
13094         }
13095     }
13096   /* We're done with the instantiation.  */
13097   end_explicit_instantiation ();
13098
13099   cp_parser_consume_semicolon_at_end_of_statement (parser);
13100
13101   timevar_pop (TV_TEMPLATE_INST);
13102 }
13103
13104 /* Parse an explicit-specialization.
13105
13106    explicit-specialization:
13107      template < > declaration
13108
13109    Although the standard says `declaration', what it really means is:
13110
13111    explicit-specialization:
13112      template <> decl-specifier [opt] init-declarator [opt] ;
13113      template <> function-definition
13114      template <> explicit-specialization
13115      template <> template-declaration  */
13116
13117 static void
13118 cp_parser_explicit_specialization (cp_parser* parser)
13119 {
13120   bool need_lang_pop;
13121   cp_token *token = cp_lexer_peek_token (parser->lexer);
13122
13123   /* Look for the `template' keyword.  */
13124   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13125   /* Look for the `<'.  */
13126   cp_parser_require (parser, CPP_LESS, RT_LESS);
13127   /* Look for the `>'.  */
13128   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13129   /* We have processed another parameter list.  */
13130   ++parser->num_template_parameter_lists;
13131   /* [temp]
13132
13133      A template ... explicit specialization ... shall not have C
13134      linkage.  */
13135   if (current_lang_name == lang_name_c)
13136     {
13137       error_at (token->location, "template specialization with C linkage");
13138       /* Give it C++ linkage to avoid confusing other parts of the
13139          front end.  */
13140       push_lang_context (lang_name_cplusplus);
13141       need_lang_pop = true;
13142     }
13143   else
13144     need_lang_pop = false;
13145   /* Let the front end know that we are beginning a specialization.  */
13146   if (!begin_specialization ())
13147     {
13148       end_specialization ();
13149       return;
13150     }
13151
13152   /* If the next keyword is `template', we need to figure out whether
13153      or not we're looking a template-declaration.  */
13154   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13155     {
13156       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13157           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13158         cp_parser_template_declaration_after_export (parser,
13159                                                      /*member_p=*/false);
13160       else
13161         cp_parser_explicit_specialization (parser);
13162     }
13163   else
13164     /* Parse the dependent declaration.  */
13165     cp_parser_single_declaration (parser,
13166                                   /*checks=*/NULL,
13167                                   /*member_p=*/false,
13168                                   /*explicit_specialization_p=*/true,
13169                                   /*friend_p=*/NULL);
13170   /* We're done with the specialization.  */
13171   end_specialization ();
13172   /* For the erroneous case of a template with C linkage, we pushed an
13173      implicit C++ linkage scope; exit that scope now.  */
13174   if (need_lang_pop)
13175     pop_lang_context ();
13176   /* We're done with this parameter list.  */
13177   --parser->num_template_parameter_lists;
13178 }
13179
13180 /* Parse a type-specifier.
13181
13182    type-specifier:
13183      simple-type-specifier
13184      class-specifier
13185      enum-specifier
13186      elaborated-type-specifier
13187      cv-qualifier
13188
13189    GNU Extension:
13190
13191    type-specifier:
13192      __complex__
13193
13194    Returns a representation of the type-specifier.  For a
13195    class-specifier, enum-specifier, or elaborated-type-specifier, a
13196    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13197
13198    The parser flags FLAGS is used to control type-specifier parsing.
13199
13200    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13201    in a decl-specifier-seq.
13202
13203    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13204    class-specifier, enum-specifier, or elaborated-type-specifier, then
13205    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13206    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13207    zero.
13208
13209    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13210    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13211    is set to FALSE.  */
13212
13213 static tree
13214 cp_parser_type_specifier (cp_parser* parser,
13215                           cp_parser_flags flags,
13216                           cp_decl_specifier_seq *decl_specs,
13217                           bool is_declaration,
13218                           int* declares_class_or_enum,
13219                           bool* is_cv_qualifier)
13220 {
13221   tree type_spec = NULL_TREE;
13222   cp_token *token;
13223   enum rid keyword;
13224   cp_decl_spec ds = ds_last;
13225
13226   /* Assume this type-specifier does not declare a new type.  */
13227   if (declares_class_or_enum)
13228     *declares_class_or_enum = 0;
13229   /* And that it does not specify a cv-qualifier.  */
13230   if (is_cv_qualifier)
13231     *is_cv_qualifier = false;
13232   /* Peek at the next token.  */
13233   token = cp_lexer_peek_token (parser->lexer);
13234
13235   /* If we're looking at a keyword, we can use that to guide the
13236      production we choose.  */
13237   keyword = token->keyword;
13238   switch (keyword)
13239     {
13240     case RID_ENUM:
13241       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13242         goto elaborated_type_specifier;
13243
13244       /* Look for the enum-specifier.  */
13245       type_spec = cp_parser_enum_specifier (parser);
13246       /* If that worked, we're done.  */
13247       if (type_spec)
13248         {
13249           if (declares_class_or_enum)
13250             *declares_class_or_enum = 2;
13251           if (decl_specs)
13252             cp_parser_set_decl_spec_type (decl_specs,
13253                                           type_spec,
13254                                           token->location,
13255                                           /*type_definition_p=*/true);
13256           return type_spec;
13257         }
13258       else
13259         goto elaborated_type_specifier;
13260
13261       /* Any of these indicate either a class-specifier, or an
13262          elaborated-type-specifier.  */
13263     case RID_CLASS:
13264     case RID_STRUCT:
13265     case RID_UNION:
13266       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13267         goto elaborated_type_specifier;
13268
13269       /* Parse tentatively so that we can back up if we don't find a
13270          class-specifier.  */
13271       cp_parser_parse_tentatively (parser);
13272       /* Look for the class-specifier.  */
13273       type_spec = cp_parser_class_specifier (parser);
13274       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13275       /* If that worked, we're done.  */
13276       if (cp_parser_parse_definitely (parser))
13277         {
13278           if (declares_class_or_enum)
13279             *declares_class_or_enum = 2;
13280           if (decl_specs)
13281             cp_parser_set_decl_spec_type (decl_specs,
13282                                           type_spec,
13283                                           token->location,
13284                                           /*type_definition_p=*/true);
13285           return type_spec;
13286         }
13287
13288       /* Fall through.  */
13289     elaborated_type_specifier:
13290       /* We're declaring (not defining) a class or enum.  */
13291       if (declares_class_or_enum)
13292         *declares_class_or_enum = 1;
13293
13294       /* Fall through.  */
13295     case RID_TYPENAME:
13296       /* Look for an elaborated-type-specifier.  */
13297       type_spec
13298         = (cp_parser_elaborated_type_specifier
13299            (parser,
13300             decl_specs && decl_specs->specs[(int) ds_friend],
13301             is_declaration));
13302       if (decl_specs)
13303         cp_parser_set_decl_spec_type (decl_specs,
13304                                       type_spec,
13305                                       token->location,
13306                                       /*type_definition_p=*/false);
13307       return type_spec;
13308
13309     case RID_CONST:
13310       ds = ds_const;
13311       if (is_cv_qualifier)
13312         *is_cv_qualifier = true;
13313       break;
13314
13315     case RID_VOLATILE:
13316       ds = ds_volatile;
13317       if (is_cv_qualifier)
13318         *is_cv_qualifier = true;
13319       break;
13320
13321     case RID_RESTRICT:
13322       ds = ds_restrict;
13323       if (is_cv_qualifier)
13324         *is_cv_qualifier = true;
13325       break;
13326
13327     case RID_COMPLEX:
13328       /* The `__complex__' keyword is a GNU extension.  */
13329       ds = ds_complex;
13330       break;
13331
13332     default:
13333       break;
13334     }
13335
13336   /* Handle simple keywords.  */
13337   if (ds != ds_last)
13338     {
13339       if (decl_specs)
13340         {
13341           ++decl_specs->specs[(int)ds];
13342           decl_specs->any_specifiers_p = true;
13343         }
13344       return cp_lexer_consume_token (parser->lexer)->u.value;
13345     }
13346
13347   /* If we do not already have a type-specifier, assume we are looking
13348      at a simple-type-specifier.  */
13349   type_spec = cp_parser_simple_type_specifier (parser,
13350                                                decl_specs,
13351                                                flags);
13352
13353   /* If we didn't find a type-specifier, and a type-specifier was not
13354      optional in this context, issue an error message.  */
13355   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13356     {
13357       cp_parser_error (parser, "expected type specifier");
13358       return error_mark_node;
13359     }
13360
13361   return type_spec;
13362 }
13363
13364 /* Parse a simple-type-specifier.
13365
13366    simple-type-specifier:
13367      :: [opt] nested-name-specifier [opt] type-name
13368      :: [opt] nested-name-specifier template template-id
13369      char
13370      wchar_t
13371      bool
13372      short
13373      int
13374      long
13375      signed
13376      unsigned
13377      float
13378      double
13379      void
13380
13381    C++0x Extension:
13382
13383    simple-type-specifier:
13384      auto
13385      decltype ( expression )   
13386      char16_t
13387      char32_t
13388      __underlying_type ( type-id )
13389
13390    GNU Extension:
13391
13392    simple-type-specifier:
13393      __int128
13394      __typeof__ unary-expression
13395      __typeof__ ( type-id )
13396
13397    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13398    appropriately updated.  */
13399
13400 static tree
13401 cp_parser_simple_type_specifier (cp_parser* parser,
13402                                  cp_decl_specifier_seq *decl_specs,
13403                                  cp_parser_flags flags)
13404 {
13405   tree type = NULL_TREE;
13406   cp_token *token;
13407
13408   /* Peek at the next token.  */
13409   token = cp_lexer_peek_token (parser->lexer);
13410
13411   /* If we're looking at a keyword, things are easy.  */
13412   switch (token->keyword)
13413     {
13414     case RID_CHAR:
13415       if (decl_specs)
13416         decl_specs->explicit_char_p = true;
13417       type = char_type_node;
13418       break;
13419     case RID_CHAR16:
13420       type = char16_type_node;
13421       break;
13422     case RID_CHAR32:
13423       type = char32_type_node;
13424       break;
13425     case RID_WCHAR:
13426       type = wchar_type_node;
13427       break;
13428     case RID_BOOL:
13429       type = boolean_type_node;
13430       break;
13431     case RID_SHORT:
13432       if (decl_specs)
13433         ++decl_specs->specs[(int) ds_short];
13434       type = short_integer_type_node;
13435       break;
13436     case RID_INT:
13437       if (decl_specs)
13438         decl_specs->explicit_int_p = true;
13439       type = integer_type_node;
13440       break;
13441     case RID_INT128:
13442       if (!int128_integer_type_node)
13443         break;
13444       if (decl_specs)
13445         decl_specs->explicit_int128_p = true;
13446       type = int128_integer_type_node;
13447       break;
13448     case RID_LONG:
13449       if (decl_specs)
13450         ++decl_specs->specs[(int) ds_long];
13451       type = long_integer_type_node;
13452       break;
13453     case RID_SIGNED:
13454       if (decl_specs)
13455         ++decl_specs->specs[(int) ds_signed];
13456       type = integer_type_node;
13457       break;
13458     case RID_UNSIGNED:
13459       if (decl_specs)
13460         ++decl_specs->specs[(int) ds_unsigned];
13461       type = unsigned_type_node;
13462       break;
13463     case RID_FLOAT:
13464       type = float_type_node;
13465       break;
13466     case RID_DOUBLE:
13467       type = double_type_node;
13468       break;
13469     case RID_VOID:
13470       type = void_type_node;
13471       break;
13472       
13473     case RID_AUTO:
13474       maybe_warn_cpp0x (CPP0X_AUTO);
13475       type = make_auto ();
13476       break;
13477
13478     case RID_DECLTYPE:
13479       /* Since DR 743, decltype can either be a simple-type-specifier by
13480          itself or begin a nested-name-specifier.  Parsing it will replace
13481          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13482          handling below decide what to do.  */
13483       cp_parser_decltype (parser);
13484       cp_lexer_set_token_position (parser->lexer, token);
13485       break;
13486
13487     case RID_TYPEOF:
13488       /* Consume the `typeof' token.  */
13489       cp_lexer_consume_token (parser->lexer);
13490       /* Parse the operand to `typeof'.  */
13491       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13492       /* If it is not already a TYPE, take its type.  */
13493       if (!TYPE_P (type))
13494         type = finish_typeof (type);
13495
13496       if (decl_specs)
13497         cp_parser_set_decl_spec_type (decl_specs, type,
13498                                       token->location,
13499                                       /*type_definition_p=*/false);
13500
13501       return type;
13502
13503     case RID_UNDERLYING_TYPE:
13504       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13505       if (decl_specs)
13506         cp_parser_set_decl_spec_type (decl_specs, type,
13507                                       token->location,
13508                                       /*type_definition_p=*/false);
13509
13510       return type;
13511
13512     case RID_BASES:
13513     case RID_DIRECT_BASES:
13514       type = cp_parser_trait_expr (parser, token->keyword);
13515       if (decl_specs)
13516        cp_parser_set_decl_spec_type (decl_specs, type,
13517                                      token->location,
13518                                      /*type_definition_p=*/false);
13519       return type;
13520     default:
13521       break;
13522     }
13523
13524   /* If token is an already-parsed decltype not followed by ::,
13525      it's a simple-type-specifier.  */
13526   if (token->type == CPP_DECLTYPE
13527       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13528     {
13529       type = token->u.value;
13530       if (decl_specs)
13531         cp_parser_set_decl_spec_type (decl_specs, type,
13532                                       token->location,
13533                                       /*type_definition_p=*/false);
13534       cp_lexer_consume_token (parser->lexer);
13535       return type;
13536     }
13537
13538   /* If the type-specifier was for a built-in type, we're done.  */
13539   if (type)
13540     {
13541       /* Record the type.  */
13542       if (decl_specs
13543           && (token->keyword != RID_SIGNED
13544               && token->keyword != RID_UNSIGNED
13545               && token->keyword != RID_SHORT
13546               && token->keyword != RID_LONG))
13547         cp_parser_set_decl_spec_type (decl_specs,
13548                                       type,
13549                                       token->location,
13550                                       /*type_definition_p=*/false);
13551       if (decl_specs)
13552         decl_specs->any_specifiers_p = true;
13553
13554       /* Consume the token.  */
13555       cp_lexer_consume_token (parser->lexer);
13556
13557       /* There is no valid C++ program where a non-template type is
13558          followed by a "<".  That usually indicates that the user thought
13559          that the type was a template.  */
13560       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13561
13562       return TYPE_NAME (type);
13563     }
13564
13565   /* The type-specifier must be a user-defined type.  */
13566   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13567     {
13568       bool qualified_p;
13569       bool global_p;
13570
13571       /* Don't gobble tokens or issue error messages if this is an
13572          optional type-specifier.  */
13573       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13574         cp_parser_parse_tentatively (parser);
13575
13576       /* Look for the optional `::' operator.  */
13577       global_p
13578         = (cp_parser_global_scope_opt (parser,
13579                                        /*current_scope_valid_p=*/false)
13580            != NULL_TREE);
13581       /* Look for the nested-name specifier.  */
13582       qualified_p
13583         = (cp_parser_nested_name_specifier_opt (parser,
13584                                                 /*typename_keyword_p=*/false,
13585                                                 /*check_dependency_p=*/true,
13586                                                 /*type_p=*/false,
13587                                                 /*is_declaration=*/false)
13588            != NULL_TREE);
13589       token = cp_lexer_peek_token (parser->lexer);
13590       /* If we have seen a nested-name-specifier, and the next token
13591          is `template', then we are using the template-id production.  */
13592       if (parser->scope
13593           && cp_parser_optional_template_keyword (parser))
13594         {
13595           /* Look for the template-id.  */
13596           type = cp_parser_template_id (parser,
13597                                         /*template_keyword_p=*/true,
13598                                         /*check_dependency_p=*/true,
13599                                         /*is_declaration=*/false);
13600           /* If the template-id did not name a type, we are out of
13601              luck.  */
13602           if (TREE_CODE (type) != TYPE_DECL)
13603             {
13604               cp_parser_error (parser, "expected template-id for type");
13605               type = NULL_TREE;
13606             }
13607         }
13608       /* Otherwise, look for a type-name.  */
13609       else
13610         type = cp_parser_type_name (parser);
13611       /* Keep track of all name-lookups performed in class scopes.  */
13612       if (type
13613           && !global_p
13614           && !qualified_p
13615           && TREE_CODE (type) == TYPE_DECL
13616           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13617         maybe_note_name_used_in_class (DECL_NAME (type), type);
13618       /* If it didn't work out, we don't have a TYPE.  */
13619       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13620           && !cp_parser_parse_definitely (parser))
13621         type = NULL_TREE;
13622       if (type && decl_specs)
13623         cp_parser_set_decl_spec_type (decl_specs, type,
13624                                       token->location,
13625                                       /*type_definition_p=*/false);
13626     }
13627
13628   /* If we didn't get a type-name, issue an error message.  */
13629   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13630     {
13631       cp_parser_error (parser, "expected type-name");
13632       return error_mark_node;
13633     }
13634
13635   if (type && type != error_mark_node)
13636     {
13637       /* See if TYPE is an Objective-C type, and if so, parse and
13638          accept any protocol references following it.  Do this before
13639          the cp_parser_check_for_invalid_template_id() call, because
13640          Objective-C types can be followed by '<...>' which would
13641          enclose protocol names rather than template arguments, and so
13642          everything is fine.  */
13643       if (c_dialect_objc () && !parser->scope
13644           && (objc_is_id (type) || objc_is_class_name (type)))
13645         {
13646           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13647           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13648
13649           /* Clobber the "unqualified" type previously entered into
13650              DECL_SPECS with the new, improved protocol-qualified version.  */
13651           if (decl_specs)
13652             decl_specs->type = qual_type;
13653
13654           return qual_type;
13655         }
13656
13657       /* There is no valid C++ program where a non-template type is
13658          followed by a "<".  That usually indicates that the user
13659          thought that the type was a template.  */
13660       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13661                                                token->location);
13662     }
13663
13664   return type;
13665 }
13666
13667 /* Parse a type-name.
13668
13669    type-name:
13670      class-name
13671      enum-name
13672      typedef-name
13673      simple-template-id [in c++0x]
13674
13675    enum-name:
13676      identifier
13677
13678    typedef-name:
13679      identifier
13680
13681    Returns a TYPE_DECL for the type.  */
13682
13683 static tree
13684 cp_parser_type_name (cp_parser* parser)
13685 {
13686   tree type_decl;
13687
13688   /* We can't know yet whether it is a class-name or not.  */
13689   cp_parser_parse_tentatively (parser);
13690   /* Try a class-name.  */
13691   type_decl = cp_parser_class_name (parser,
13692                                     /*typename_keyword_p=*/false,
13693                                     /*template_keyword_p=*/false,
13694                                     none_type,
13695                                     /*check_dependency_p=*/true,
13696                                     /*class_head_p=*/false,
13697                                     /*is_declaration=*/false);
13698   /* If it's not a class-name, keep looking.  */
13699   if (!cp_parser_parse_definitely (parser))
13700     {
13701       if (cxx_dialect < cxx0x)
13702         /* It must be a typedef-name or an enum-name.  */
13703         return cp_parser_nonclass_name (parser);
13704
13705       cp_parser_parse_tentatively (parser);
13706       /* It is either a simple-template-id representing an
13707          instantiation of an alias template...  */
13708       type_decl = cp_parser_template_id (parser,
13709                                          /*template_keyword_p=*/false,
13710                                          /*check_dependency_p=*/false,
13711                                          /*is_declaration=*/false);
13712       /* Note that this must be an instantiation of an alias template
13713          because [temp.names]/6 says:
13714          
13715              A template-id that names an alias template specialization
13716              is a type-name.
13717
13718          Whereas [temp.names]/7 says:
13719          
13720              A simple-template-id that names a class template
13721              specialization is a class-name.  */
13722       if (type_decl != NULL_TREE
13723           && TREE_CODE (type_decl) == TYPE_DECL
13724           && TYPE_DECL_ALIAS_P (type_decl))
13725         gcc_assert (DECL_TEMPLATE_INSTANTIATION (type_decl));
13726       else
13727         cp_parser_simulate_error (parser);
13728
13729       if (!cp_parser_parse_definitely (parser))
13730         /* ... Or a typedef-name or an enum-name.  */
13731         return cp_parser_nonclass_name (parser);
13732     }
13733
13734   return type_decl;
13735 }
13736
13737 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13738
13739    enum-name:
13740      identifier
13741
13742    typedef-name:
13743      identifier
13744
13745    Returns a TYPE_DECL for the type.  */
13746
13747 static tree
13748 cp_parser_nonclass_name (cp_parser* parser)
13749 {
13750   tree type_decl;
13751   tree identifier;
13752
13753   cp_token *token = cp_lexer_peek_token (parser->lexer);
13754   identifier = cp_parser_identifier (parser);
13755   if (identifier == error_mark_node)
13756     return error_mark_node;
13757
13758   /* Look up the type-name.  */
13759   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13760
13761   /* If it is a using decl, use its underlying decl.  */
13762   type_decl = strip_using_decl (type_decl);
13763
13764   if (TREE_CODE (type_decl) != TYPE_DECL
13765       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13766     {
13767       /* See if this is an Objective-C type.  */
13768       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13769       tree type = objc_get_protocol_qualified_type (identifier, protos);
13770       if (type)
13771         type_decl = TYPE_NAME (type);
13772     }
13773
13774   /* Issue an error if we did not find a type-name.  */
13775   if (TREE_CODE (type_decl) != TYPE_DECL
13776       /* In Objective-C, we have the complication that class names are
13777          normally type names and start declarations (eg, the
13778          "NSObject" in "NSObject *object;"), but can be used in an
13779          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13780          is an expression.  So, a classname followed by a dot is not a
13781          valid type-name.  */
13782       || (objc_is_class_name (TREE_TYPE (type_decl))
13783           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13784     {
13785       if (!cp_parser_simulate_error (parser))
13786         cp_parser_name_lookup_error (parser, identifier, type_decl,
13787                                      NLE_TYPE, token->location);
13788       return error_mark_node;
13789     }
13790   /* Remember that the name was used in the definition of the
13791      current class so that we can check later to see if the
13792      meaning would have been different after the class was
13793      entirely defined.  */
13794   else if (type_decl != error_mark_node
13795            && !parser->scope)
13796     maybe_note_name_used_in_class (identifier, type_decl);
13797   
13798   return type_decl;
13799 }
13800
13801 /* Parse an elaborated-type-specifier.  Note that the grammar given
13802    here incorporates the resolution to DR68.
13803
13804    elaborated-type-specifier:
13805      class-key :: [opt] nested-name-specifier [opt] identifier
13806      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13807      enum-key :: [opt] nested-name-specifier [opt] identifier
13808      typename :: [opt] nested-name-specifier identifier
13809      typename :: [opt] nested-name-specifier template [opt]
13810        template-id
13811
13812    GNU extension:
13813
13814    elaborated-type-specifier:
13815      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13816      class-key attributes :: [opt] nested-name-specifier [opt]
13817                template [opt] template-id
13818      enum attributes :: [opt] nested-name-specifier [opt] identifier
13819
13820    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13821    declared `friend'.  If IS_DECLARATION is TRUE, then this
13822    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13823    something is being declared.
13824
13825    Returns the TYPE specified.  */
13826
13827 static tree
13828 cp_parser_elaborated_type_specifier (cp_parser* parser,
13829                                      bool is_friend,
13830                                      bool is_declaration)
13831 {
13832   enum tag_types tag_type;
13833   tree identifier;
13834   tree type = NULL_TREE;
13835   tree attributes = NULL_TREE;
13836   tree globalscope;
13837   cp_token *token = NULL;
13838
13839   /* See if we're looking at the `enum' keyword.  */
13840   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13841     {
13842       /* Consume the `enum' token.  */
13843       cp_lexer_consume_token (parser->lexer);
13844       /* Remember that it's an enumeration type.  */
13845       tag_type = enum_type;
13846       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13847          enums) is used here.  */
13848       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13849           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13850         {
13851             pedwarn (input_location, 0, "elaborated-type-specifier "
13852                       "for a scoped enum must not use the %<%D%> keyword",
13853                       cp_lexer_peek_token (parser->lexer)->u.value);
13854           /* Consume the `struct' or `class' and parse it anyway.  */
13855           cp_lexer_consume_token (parser->lexer);
13856         }
13857       /* Parse the attributes.  */
13858       attributes = cp_parser_attributes_opt (parser);
13859     }
13860   /* Or, it might be `typename'.  */
13861   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13862                                            RID_TYPENAME))
13863     {
13864       /* Consume the `typename' token.  */
13865       cp_lexer_consume_token (parser->lexer);
13866       /* Remember that it's a `typename' type.  */
13867       tag_type = typename_type;
13868     }
13869   /* Otherwise it must be a class-key.  */
13870   else
13871     {
13872       tag_type = cp_parser_class_key (parser);
13873       if (tag_type == none_type)
13874         return error_mark_node;
13875       /* Parse the attributes.  */
13876       attributes = cp_parser_attributes_opt (parser);
13877     }
13878
13879   /* Look for the `::' operator.  */
13880   globalscope =  cp_parser_global_scope_opt (parser,
13881                                              /*current_scope_valid_p=*/false);
13882   /* Look for the nested-name-specifier.  */
13883   if (tag_type == typename_type && !globalscope)
13884     {
13885       if (!cp_parser_nested_name_specifier (parser,
13886                                            /*typename_keyword_p=*/true,
13887                                            /*check_dependency_p=*/true,
13888                                            /*type_p=*/true,
13889                                             is_declaration))
13890         return error_mark_node;
13891     }
13892   else
13893     /* Even though `typename' is not present, the proposed resolution
13894        to Core Issue 180 says that in `class A<T>::B', `B' should be
13895        considered a type-name, even if `A<T>' is dependent.  */
13896     cp_parser_nested_name_specifier_opt (parser,
13897                                          /*typename_keyword_p=*/true,
13898                                          /*check_dependency_p=*/true,
13899                                          /*type_p=*/true,
13900                                          is_declaration);
13901  /* For everything but enumeration types, consider a template-id.
13902     For an enumeration type, consider only a plain identifier.  */
13903   if (tag_type != enum_type)
13904     {
13905       bool template_p = false;
13906       tree decl;
13907
13908       /* Allow the `template' keyword.  */
13909       template_p = cp_parser_optional_template_keyword (parser);
13910       /* If we didn't see `template', we don't know if there's a
13911          template-id or not.  */
13912       if (!template_p)
13913         cp_parser_parse_tentatively (parser);
13914       /* Parse the template-id.  */
13915       token = cp_lexer_peek_token (parser->lexer);
13916       decl = cp_parser_template_id (parser, template_p,
13917                                     /*check_dependency_p=*/true,
13918                                     is_declaration);
13919       /* If we didn't find a template-id, look for an ordinary
13920          identifier.  */
13921       if (!template_p && !cp_parser_parse_definitely (parser))
13922         ;
13923       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13924          in effect, then we must assume that, upon instantiation, the
13925          template will correspond to a class.  */
13926       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13927                && tag_type == typename_type)
13928         type = make_typename_type (parser->scope, decl,
13929                                    typename_type,
13930                                    /*complain=*/tf_error);
13931       /* If the `typename' keyword is in effect and DECL is not a type
13932          decl. Then type is non existant.   */
13933       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13934         type = NULL_TREE; 
13935       else 
13936         type = TREE_TYPE (decl);
13937     }
13938
13939   if (!type)
13940     {
13941       token = cp_lexer_peek_token (parser->lexer);
13942       identifier = cp_parser_identifier (parser);
13943
13944       if (identifier == error_mark_node)
13945         {
13946           parser->scope = NULL_TREE;
13947           return error_mark_node;
13948         }
13949
13950       /* For a `typename', we needn't call xref_tag.  */
13951       if (tag_type == typename_type
13952           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13953         return cp_parser_make_typename_type (parser, parser->scope,
13954                                              identifier,
13955                                              token->location);
13956       /* Look up a qualified name in the usual way.  */
13957       if (parser->scope)
13958         {
13959           tree decl;
13960           tree ambiguous_decls;
13961
13962           decl = cp_parser_lookup_name (parser, identifier,
13963                                         tag_type,
13964                                         /*is_template=*/false,
13965                                         /*is_namespace=*/false,
13966                                         /*check_dependency=*/true,
13967                                         &ambiguous_decls,
13968                                         token->location);
13969
13970           /* If the lookup was ambiguous, an error will already have been
13971              issued.  */
13972           if (ambiguous_decls)
13973             return error_mark_node;
13974
13975           /* If we are parsing friend declaration, DECL may be a
13976              TEMPLATE_DECL tree node here.  However, we need to check
13977              whether this TEMPLATE_DECL results in valid code.  Consider
13978              the following example:
13979
13980                namespace N {
13981                  template <class T> class C {};
13982                }
13983                class X {
13984                  template <class T> friend class N::C; // #1, valid code
13985                };
13986                template <class T> class Y {
13987                  friend class N::C;                    // #2, invalid code
13988                };
13989
13990              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13991              name lookup of `N::C'.  We see that friend declaration must
13992              be template for the code to be valid.  Note that
13993              processing_template_decl does not work here since it is
13994              always 1 for the above two cases.  */
13995
13996           decl = (cp_parser_maybe_treat_template_as_class
13997                   (decl, /*tag_name_p=*/is_friend
13998                          && parser->num_template_parameter_lists));
13999
14000           if (TREE_CODE (decl) != TYPE_DECL)
14001             {
14002               cp_parser_diagnose_invalid_type_name (parser,
14003                                                     parser->scope,
14004                                                     identifier,
14005                                                     token->location);
14006               return error_mark_node;
14007             }
14008
14009           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
14010             {
14011               bool allow_template = (parser->num_template_parameter_lists
14012                                       || DECL_SELF_REFERENCE_P (decl));
14013               type = check_elaborated_type_specifier (tag_type, decl, 
14014                                                       allow_template);
14015
14016               if (type == error_mark_node)
14017                 return error_mark_node;
14018             }
14019
14020           /* Forward declarations of nested types, such as
14021
14022                class C1::C2;
14023                class C1::C2::C3;
14024
14025              are invalid unless all components preceding the final '::'
14026              are complete.  If all enclosing types are complete, these
14027              declarations become merely pointless.
14028
14029              Invalid forward declarations of nested types are errors
14030              caught elsewhere in parsing.  Those that are pointless arrive
14031              here.  */
14032
14033           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
14034               && !is_friend && !processing_explicit_instantiation)
14035             warning (0, "declaration %qD does not declare anything", decl);
14036
14037           type = TREE_TYPE (decl);
14038         }
14039       else
14040         {
14041           /* An elaborated-type-specifier sometimes introduces a new type and
14042              sometimes names an existing type.  Normally, the rule is that it
14043              introduces a new type only if there is not an existing type of
14044              the same name already in scope.  For example, given:
14045
14046                struct S {};
14047                void f() { struct S s; }
14048
14049              the `struct S' in the body of `f' is the same `struct S' as in
14050              the global scope; the existing definition is used.  However, if
14051              there were no global declaration, this would introduce a new
14052              local class named `S'.
14053
14054              An exception to this rule applies to the following code:
14055
14056                namespace N { struct S; }
14057
14058              Here, the elaborated-type-specifier names a new type
14059              unconditionally; even if there is already an `S' in the
14060              containing scope this declaration names a new type.
14061              This exception only applies if the elaborated-type-specifier
14062              forms the complete declaration:
14063
14064                [class.name]
14065
14066                A declaration consisting solely of `class-key identifier ;' is
14067                either a redeclaration of the name in the current scope or a
14068                forward declaration of the identifier as a class name.  It
14069                introduces the name into the current scope.
14070
14071              We are in this situation precisely when the next token is a `;'.
14072
14073              An exception to the exception is that a `friend' declaration does
14074              *not* name a new type; i.e., given:
14075
14076                struct S { friend struct T; };
14077
14078              `T' is not a new type in the scope of `S'.
14079
14080              Also, `new struct S' or `sizeof (struct S)' never results in the
14081              definition of a new type; a new type can only be declared in a
14082              declaration context.  */
14083
14084           tag_scope ts;
14085           bool template_p;
14086
14087           if (is_friend)
14088             /* Friends have special name lookup rules.  */
14089             ts = ts_within_enclosing_non_class;
14090           else if (is_declaration
14091                    && cp_lexer_next_token_is (parser->lexer,
14092                                               CPP_SEMICOLON))
14093             /* This is a `class-key identifier ;' */
14094             ts = ts_current;
14095           else
14096             ts = ts_global;
14097
14098           template_p =
14099             (parser->num_template_parameter_lists
14100              && (cp_parser_next_token_starts_class_definition_p (parser)
14101                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14102           /* An unqualified name was used to reference this type, so
14103              there were no qualifying templates.  */
14104           if (!cp_parser_check_template_parameters (parser,
14105                                                     /*num_templates=*/0,
14106                                                     token->location,
14107                                                     /*declarator=*/NULL))
14108             return error_mark_node;
14109           type = xref_tag (tag_type, identifier, ts, template_p);
14110         }
14111     }
14112
14113   if (type == error_mark_node)
14114     return error_mark_node;
14115
14116   /* Allow attributes on forward declarations of classes.  */
14117   if (attributes)
14118     {
14119       if (TREE_CODE (type) == TYPENAME_TYPE)
14120         warning (OPT_Wattributes,
14121                  "attributes ignored on uninstantiated type");
14122       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14123                && ! processing_explicit_instantiation)
14124         warning (OPT_Wattributes,
14125                  "attributes ignored on template instantiation");
14126       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14127         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14128       else
14129         warning (OPT_Wattributes,
14130                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14131     }
14132
14133   if (tag_type != enum_type)
14134     {
14135       /* Indicate whether this class was declared as a `class' or as a
14136          `struct'.  */
14137       if (TREE_CODE (type) == RECORD_TYPE)
14138         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14139       cp_parser_check_class_key (tag_type, type);
14140     }
14141
14142   /* A "<" cannot follow an elaborated type specifier.  If that
14143      happens, the user was probably trying to form a template-id.  */
14144   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14145
14146   return type;
14147 }
14148
14149 /* Parse an enum-specifier.
14150
14151    enum-specifier:
14152      enum-head { enumerator-list [opt] }
14153      enum-head { enumerator-list , } [C++0x]
14154
14155    enum-head:
14156      enum-key identifier [opt] enum-base [opt]
14157      enum-key nested-name-specifier identifier enum-base [opt]
14158
14159    enum-key:
14160      enum
14161      enum class   [C++0x]
14162      enum struct  [C++0x]
14163
14164    enum-base:   [C++0x]
14165      : type-specifier-seq
14166
14167    opaque-enum-specifier:
14168      enum-key identifier enum-base [opt] ;
14169
14170    GNU Extensions:
14171      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14172        { enumerator-list [opt] }attributes[opt]
14173      enum-key attributes[opt] identifier [opt] enum-base [opt]
14174        { enumerator-list, }attributes[opt] [C++0x]
14175
14176    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14177    if the token stream isn't an enum-specifier after all.  */
14178
14179 static tree
14180 cp_parser_enum_specifier (cp_parser* parser)
14181 {
14182   tree identifier;
14183   tree type = NULL_TREE;
14184   tree prev_scope;
14185   tree nested_name_specifier = NULL_TREE;
14186   tree attributes;
14187   bool scoped_enum_p = false;
14188   bool has_underlying_type = false;
14189   bool nested_being_defined = false;
14190   bool new_value_list = false;
14191   bool is_new_type = false;
14192   bool is_anonymous = false;
14193   tree underlying_type = NULL_TREE;
14194   cp_token *type_start_token = NULL;
14195   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14196
14197   parser->colon_corrects_to_scope_p = false;
14198
14199   /* Parse tentatively so that we can back up if we don't find a
14200      enum-specifier.  */
14201   cp_parser_parse_tentatively (parser);
14202
14203   /* Caller guarantees that the current token is 'enum', an identifier
14204      possibly follows, and the token after that is an opening brace.
14205      If we don't have an identifier, fabricate an anonymous name for
14206      the enumeration being defined.  */
14207   cp_lexer_consume_token (parser->lexer);
14208
14209   /* Parse the "class" or "struct", which indicates a scoped
14210      enumeration type in C++0x.  */
14211   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14212       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14213     {
14214       if (cxx_dialect < cxx0x)
14215         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14216
14217       /* Consume the `struct' or `class' token.  */
14218       cp_lexer_consume_token (parser->lexer);
14219
14220       scoped_enum_p = true;
14221     }
14222
14223   attributes = cp_parser_attributes_opt (parser);
14224
14225   /* Clear the qualification.  */
14226   parser->scope = NULL_TREE;
14227   parser->qualifying_scope = NULL_TREE;
14228   parser->object_scope = NULL_TREE;
14229
14230   /* Figure out in what scope the declaration is being placed.  */
14231   prev_scope = current_scope ();
14232
14233   type_start_token = cp_lexer_peek_token (parser->lexer);
14234
14235   push_deferring_access_checks (dk_no_check);
14236   nested_name_specifier
14237       = cp_parser_nested_name_specifier_opt (parser,
14238                                              /*typename_keyword_p=*/true,
14239                                              /*check_dependency_p=*/false,
14240                                              /*type_p=*/false,
14241                                              /*is_declaration=*/false);
14242
14243   if (nested_name_specifier)
14244     {
14245       tree name;
14246
14247       identifier = cp_parser_identifier (parser);
14248       name =  cp_parser_lookup_name (parser, identifier,
14249                                      enum_type,
14250                                      /*is_template=*/false,
14251                                      /*is_namespace=*/false,
14252                                      /*check_dependency=*/true,
14253                                      /*ambiguous_decls=*/NULL,
14254                                      input_location);
14255       if (name)
14256         {
14257           type = TREE_TYPE (name);
14258           if (TREE_CODE (type) == TYPENAME_TYPE)
14259             {
14260               /* Are template enums allowed in ISO? */
14261               if (template_parm_scope_p ())
14262                 pedwarn (type_start_token->location, OPT_pedantic,
14263                          "%qD is an enumeration template", name);
14264               /* ignore a typename reference, for it will be solved by name
14265                  in start_enum.  */
14266               type = NULL_TREE;
14267             }
14268         }
14269       else
14270         error_at (type_start_token->location,
14271                   "%qD is not an enumerator-name", identifier);
14272     }
14273   else
14274     {
14275       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14276         identifier = cp_parser_identifier (parser);
14277       else
14278         {
14279           identifier = make_anon_name ();
14280           is_anonymous = true;
14281         }
14282     }
14283   pop_deferring_access_checks ();
14284
14285   /* Check for the `:' that denotes a specified underlying type in C++0x.
14286      Note that a ':' could also indicate a bitfield width, however.  */
14287   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14288     {
14289       cp_decl_specifier_seq type_specifiers;
14290
14291       /* Consume the `:'.  */
14292       cp_lexer_consume_token (parser->lexer);
14293
14294       /* Parse the type-specifier-seq.  */
14295       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14296                                     /*is_trailing_return=*/false,
14297                                     &type_specifiers);
14298
14299       /* At this point this is surely not elaborated type specifier.  */
14300       if (!cp_parser_parse_definitely (parser))
14301         return NULL_TREE;
14302
14303       if (cxx_dialect < cxx0x)
14304         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14305
14306       has_underlying_type = true;
14307
14308       /* If that didn't work, stop.  */
14309       if (type_specifiers.type != error_mark_node)
14310         {
14311           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14312                                             /*initialized=*/0, NULL);
14313           if (underlying_type == error_mark_node)
14314             underlying_type = NULL_TREE;
14315         }
14316     }
14317
14318   /* Look for the `{' but don't consume it yet.  */
14319   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14320     {
14321       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14322         {
14323           cp_parser_error (parser, "expected %<{%>");
14324           if (has_underlying_type)
14325             {
14326               type = NULL_TREE;
14327               goto out;
14328             }
14329         }
14330       /* An opaque-enum-specifier must have a ';' here.  */
14331       if ((scoped_enum_p || underlying_type)
14332           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14333         {
14334           cp_parser_error (parser, "expected %<;%> or %<{%>");
14335           if (has_underlying_type)
14336             {
14337               type = NULL_TREE;
14338               goto out;
14339             }
14340         }
14341     }
14342
14343   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14344     return NULL_TREE;
14345
14346   if (nested_name_specifier)
14347     {
14348       if (CLASS_TYPE_P (nested_name_specifier))
14349         {
14350           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14351           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14352           push_scope (nested_name_specifier);
14353         }
14354       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14355         {
14356           push_nested_namespace (nested_name_specifier);
14357         }
14358     }
14359
14360   /* Issue an error message if type-definitions are forbidden here.  */
14361   if (!cp_parser_check_type_definition (parser))
14362     type = error_mark_node;
14363   else
14364     /* Create the new type.  We do this before consuming the opening
14365        brace so the enum will be recorded as being on the line of its
14366        tag (or the 'enum' keyword, if there is no tag).  */
14367     type = start_enum (identifier, type, underlying_type,
14368                        scoped_enum_p, &is_new_type);
14369
14370   /* If the next token is not '{' it is an opaque-enum-specifier or an
14371      elaborated-type-specifier.  */
14372   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14373     {
14374       timevar_push (TV_PARSE_ENUM);
14375       if (nested_name_specifier)
14376         {
14377           /* The following catches invalid code such as:
14378              enum class S<int>::E { A, B, C }; */
14379           if (!processing_specialization
14380               && CLASS_TYPE_P (nested_name_specifier)
14381               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14382             error_at (type_start_token->location, "cannot add an enumerator "
14383                       "list to a template instantiation");
14384
14385           /* If that scope does not contain the scope in which the
14386              class was originally declared, the program is invalid.  */
14387           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14388             {
14389               if (at_namespace_scope_p ())
14390                 error_at (type_start_token->location,
14391                           "declaration of %qD in namespace %qD which does not "
14392                           "enclose %qD",
14393                           type, prev_scope, nested_name_specifier);
14394               else
14395                 error_at (type_start_token->location,
14396                           "declaration of %qD in %qD which does not enclose %qD",
14397                           type, prev_scope, nested_name_specifier);
14398               type = error_mark_node;
14399             }
14400         }
14401
14402       if (scoped_enum_p)
14403         begin_scope (sk_scoped_enum, type);
14404
14405       /* Consume the opening brace.  */
14406       cp_lexer_consume_token (parser->lexer);
14407
14408       if (type == error_mark_node)
14409         ; /* Nothing to add */
14410       else if (OPAQUE_ENUM_P (type)
14411                || (cxx_dialect > cxx98 && processing_specialization))
14412         {
14413           new_value_list = true;
14414           SET_OPAQUE_ENUM_P (type, false);
14415           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14416         }
14417       else
14418         {
14419           error_at (type_start_token->location, "multiple definition of %q#T", type);
14420           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14421                     "previous definition here");
14422           type = error_mark_node;
14423         }
14424
14425       if (type == error_mark_node)
14426         cp_parser_skip_to_end_of_block_or_statement (parser);
14427       /* If the next token is not '}', then there are some enumerators.  */
14428       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14429         cp_parser_enumerator_list (parser, type);
14430
14431       /* Consume the final '}'.  */
14432       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14433
14434       if (scoped_enum_p)
14435         finish_scope ();
14436       timevar_pop (TV_PARSE_ENUM);
14437     }
14438   else
14439     {
14440       /* If a ';' follows, then it is an opaque-enum-specifier
14441         and additional restrictions apply.  */
14442       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14443         {
14444           if (is_anonymous)
14445             error_at (type_start_token->location,
14446                       "opaque-enum-specifier without name");
14447           else if (nested_name_specifier)
14448             error_at (type_start_token->location,
14449                       "opaque-enum-specifier must use a simple identifier");
14450         }
14451     }
14452
14453   /* Look for trailing attributes to apply to this enumeration, and
14454      apply them if appropriate.  */
14455   if (cp_parser_allow_gnu_extensions_p (parser))
14456     {
14457       tree trailing_attr = cp_parser_attributes_opt (parser);
14458       trailing_attr = chainon (trailing_attr, attributes);
14459       cplus_decl_attributes (&type,
14460                              trailing_attr,
14461                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14462     }
14463
14464   /* Finish up the enumeration.  */
14465   if (type != error_mark_node)
14466     {
14467       if (new_value_list)
14468         finish_enum_value_list (type);
14469       if (is_new_type)
14470         finish_enum (type);
14471     }
14472
14473   if (nested_name_specifier)
14474     {
14475       if (CLASS_TYPE_P (nested_name_specifier))
14476         {
14477           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14478           pop_scope (nested_name_specifier);
14479         }
14480       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14481         {
14482           pop_nested_namespace (nested_name_specifier);
14483         }
14484     }
14485  out:
14486   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14487   return type;
14488 }
14489
14490 /* Parse an enumerator-list.  The enumerators all have the indicated
14491    TYPE.
14492
14493    enumerator-list:
14494      enumerator-definition
14495      enumerator-list , enumerator-definition  */
14496
14497 static void
14498 cp_parser_enumerator_list (cp_parser* parser, tree type)
14499 {
14500   while (true)
14501     {
14502       /* Parse an enumerator-definition.  */
14503       cp_parser_enumerator_definition (parser, type);
14504
14505       /* If the next token is not a ',', we've reached the end of
14506          the list.  */
14507       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14508         break;
14509       /* Otherwise, consume the `,' and keep going.  */
14510       cp_lexer_consume_token (parser->lexer);
14511       /* If the next token is a `}', there is a trailing comma.  */
14512       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14513         {
14514           if (cxx_dialect < cxx0x && !in_system_header)
14515             pedwarn (input_location, OPT_pedantic,
14516                      "comma at end of enumerator list");
14517           break;
14518         }
14519     }
14520 }
14521
14522 /* Parse an enumerator-definition.  The enumerator has the indicated
14523    TYPE.
14524
14525    enumerator-definition:
14526      enumerator
14527      enumerator = constant-expression
14528
14529    enumerator:
14530      identifier  */
14531
14532 static void
14533 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14534 {
14535   tree identifier;
14536   tree value;
14537   location_t loc;
14538
14539   /* Save the input location because we are interested in the location
14540      of the identifier and not the location of the explicit value.  */
14541   loc = cp_lexer_peek_token (parser->lexer)->location;
14542
14543   /* Look for the identifier.  */
14544   identifier = cp_parser_identifier (parser);
14545   if (identifier == error_mark_node)
14546     return;
14547
14548   /* If the next token is an '=', then there is an explicit value.  */
14549   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14550     {
14551       /* Consume the `=' token.  */
14552       cp_lexer_consume_token (parser->lexer);
14553       /* Parse the value.  */
14554       value = cp_parser_constant_expression (parser,
14555                                              /*allow_non_constant_p=*/false,
14556                                              NULL);
14557     }
14558   else
14559     value = NULL_TREE;
14560
14561   /* If we are processing a template, make sure the initializer of the
14562      enumerator doesn't contain any bare template parameter pack.  */
14563   if (check_for_bare_parameter_packs (value))
14564     value = error_mark_node;
14565
14566   /* integral_constant_value will pull out this expression, so make sure
14567      it's folded as appropriate.  */
14568   value = fold_non_dependent_expr (value);
14569
14570   /* Create the enumerator.  */
14571   build_enumerator (identifier, value, type, loc);
14572 }
14573
14574 /* Parse a namespace-name.
14575
14576    namespace-name:
14577      original-namespace-name
14578      namespace-alias
14579
14580    Returns the NAMESPACE_DECL for the namespace.  */
14581
14582 static tree
14583 cp_parser_namespace_name (cp_parser* parser)
14584 {
14585   tree identifier;
14586   tree namespace_decl;
14587
14588   cp_token *token = cp_lexer_peek_token (parser->lexer);
14589
14590   /* Get the name of the namespace.  */
14591   identifier = cp_parser_identifier (parser);
14592   if (identifier == error_mark_node)
14593     return error_mark_node;
14594
14595   /* Look up the identifier in the currently active scope.  Look only
14596      for namespaces, due to:
14597
14598        [basic.lookup.udir]
14599
14600        When looking up a namespace-name in a using-directive or alias
14601        definition, only namespace names are considered.
14602
14603      And:
14604
14605        [basic.lookup.qual]
14606
14607        During the lookup of a name preceding the :: scope resolution
14608        operator, object, function, and enumerator names are ignored.
14609
14610      (Note that cp_parser_qualifying_entity only calls this
14611      function if the token after the name is the scope resolution
14612      operator.)  */
14613   namespace_decl = cp_parser_lookup_name (parser, identifier,
14614                                           none_type,
14615                                           /*is_template=*/false,
14616                                           /*is_namespace=*/true,
14617                                           /*check_dependency=*/true,
14618                                           /*ambiguous_decls=*/NULL,
14619                                           token->location);
14620   /* If it's not a namespace, issue an error.  */
14621   if (namespace_decl == error_mark_node
14622       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14623     {
14624       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14625         error_at (token->location, "%qD is not a namespace-name", identifier);
14626       cp_parser_error (parser, "expected namespace-name");
14627       namespace_decl = error_mark_node;
14628     }
14629
14630   return namespace_decl;
14631 }
14632
14633 /* Parse a namespace-definition.
14634
14635    namespace-definition:
14636      named-namespace-definition
14637      unnamed-namespace-definition
14638
14639    named-namespace-definition:
14640      original-namespace-definition
14641      extension-namespace-definition
14642
14643    original-namespace-definition:
14644      namespace identifier { namespace-body }
14645
14646    extension-namespace-definition:
14647      namespace original-namespace-name { namespace-body }
14648
14649    unnamed-namespace-definition:
14650      namespace { namespace-body } */
14651
14652 static void
14653 cp_parser_namespace_definition (cp_parser* parser)
14654 {
14655   tree identifier, attribs;
14656   bool has_visibility;
14657   bool is_inline;
14658
14659   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14660     {
14661       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14662       is_inline = true;
14663       cp_lexer_consume_token (parser->lexer);
14664     }
14665   else
14666     is_inline = false;
14667
14668   /* Look for the `namespace' keyword.  */
14669   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14670
14671   /* Get the name of the namespace.  We do not attempt to distinguish
14672      between an original-namespace-definition and an
14673      extension-namespace-definition at this point.  The semantic
14674      analysis routines are responsible for that.  */
14675   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14676     identifier = cp_parser_identifier (parser);
14677   else
14678     identifier = NULL_TREE;
14679
14680   /* Parse any specified attributes.  */
14681   attribs = cp_parser_attributes_opt (parser);
14682
14683   /* Look for the `{' to start the namespace.  */
14684   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14685   /* Start the namespace.  */
14686   push_namespace (identifier);
14687
14688   /* "inline namespace" is equivalent to a stub namespace definition
14689      followed by a strong using directive.  */
14690   if (is_inline)
14691     {
14692       tree name_space = current_namespace;
14693       /* Set up namespace association.  */
14694       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14695         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14696                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14697       /* Import the contents of the inline namespace.  */
14698       pop_namespace ();
14699       do_using_directive (name_space);
14700       push_namespace (identifier);
14701     }
14702
14703   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14704
14705   /* Parse the body of the namespace.  */
14706   cp_parser_namespace_body (parser);
14707
14708   if (has_visibility)
14709     pop_visibility (1);
14710
14711   /* Finish the namespace.  */
14712   pop_namespace ();
14713   /* Look for the final `}'.  */
14714   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14715 }
14716
14717 /* Parse a namespace-body.
14718
14719    namespace-body:
14720      declaration-seq [opt]  */
14721
14722 static void
14723 cp_parser_namespace_body (cp_parser* parser)
14724 {
14725   cp_parser_declaration_seq_opt (parser);
14726 }
14727
14728 /* Parse a namespace-alias-definition.
14729
14730    namespace-alias-definition:
14731      namespace identifier = qualified-namespace-specifier ;  */
14732
14733 static void
14734 cp_parser_namespace_alias_definition (cp_parser* parser)
14735 {
14736   tree identifier;
14737   tree namespace_specifier;
14738
14739   cp_token *token = cp_lexer_peek_token (parser->lexer);
14740
14741   /* Look for the `namespace' keyword.  */
14742   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14743   /* Look for the identifier.  */
14744   identifier = cp_parser_identifier (parser);
14745   if (identifier == error_mark_node)
14746     return;
14747   /* Look for the `=' token.  */
14748   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14749       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14750     {
14751       error_at (token->location, "%<namespace%> definition is not allowed here");
14752       /* Skip the definition.  */
14753       cp_lexer_consume_token (parser->lexer);
14754       if (cp_parser_skip_to_closing_brace (parser))
14755         cp_lexer_consume_token (parser->lexer);
14756       return;
14757     }
14758   cp_parser_require (parser, CPP_EQ, RT_EQ);
14759   /* Look for the qualified-namespace-specifier.  */
14760   namespace_specifier
14761     = cp_parser_qualified_namespace_specifier (parser);
14762   /* Look for the `;' token.  */
14763   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14764
14765   /* Register the alias in the symbol table.  */
14766   do_namespace_alias (identifier, namespace_specifier);
14767 }
14768
14769 /* Parse a qualified-namespace-specifier.
14770
14771    qualified-namespace-specifier:
14772      :: [opt] nested-name-specifier [opt] namespace-name
14773
14774    Returns a NAMESPACE_DECL corresponding to the specified
14775    namespace.  */
14776
14777 static tree
14778 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14779 {
14780   /* Look for the optional `::'.  */
14781   cp_parser_global_scope_opt (parser,
14782                               /*current_scope_valid_p=*/false);
14783
14784   /* Look for the optional nested-name-specifier.  */
14785   cp_parser_nested_name_specifier_opt (parser,
14786                                        /*typename_keyword_p=*/false,
14787                                        /*check_dependency_p=*/true,
14788                                        /*type_p=*/false,
14789                                        /*is_declaration=*/true);
14790
14791   return cp_parser_namespace_name (parser);
14792 }
14793
14794 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14795    access declaration.
14796
14797    using-declaration:
14798      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14799      using :: unqualified-id ;  
14800
14801    access-declaration:
14802      qualified-id ;  
14803
14804    */
14805
14806 static bool
14807 cp_parser_using_declaration (cp_parser* parser, 
14808                              bool access_declaration_p)
14809 {
14810   cp_token *token;
14811   bool typename_p = false;
14812   bool global_scope_p;
14813   tree decl;
14814   tree identifier;
14815   tree qscope;
14816
14817   if (access_declaration_p)
14818     cp_parser_parse_tentatively (parser);
14819   else
14820     {
14821       /* Look for the `using' keyword.  */
14822       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14823       
14824       /* Peek at the next token.  */
14825       token = cp_lexer_peek_token (parser->lexer);
14826       /* See if it's `typename'.  */
14827       if (token->keyword == RID_TYPENAME)
14828         {
14829           /* Remember that we've seen it.  */
14830           typename_p = true;
14831           /* Consume the `typename' token.  */
14832           cp_lexer_consume_token (parser->lexer);
14833         }
14834     }
14835
14836   /* Look for the optional global scope qualification.  */
14837   global_scope_p
14838     = (cp_parser_global_scope_opt (parser,
14839                                    /*current_scope_valid_p=*/false)
14840        != NULL_TREE);
14841
14842   /* If we saw `typename', or didn't see `::', then there must be a
14843      nested-name-specifier present.  */
14844   if (typename_p || !global_scope_p)
14845     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14846                                               /*check_dependency_p=*/true,
14847                                               /*type_p=*/false,
14848                                               /*is_declaration=*/true);
14849   /* Otherwise, we could be in either of the two productions.  In that
14850      case, treat the nested-name-specifier as optional.  */
14851   else
14852     qscope = cp_parser_nested_name_specifier_opt (parser,
14853                                                   /*typename_keyword_p=*/false,
14854                                                   /*check_dependency_p=*/true,
14855                                                   /*type_p=*/false,
14856                                                   /*is_declaration=*/true);
14857   if (!qscope)
14858     qscope = global_namespace;
14859
14860   if (access_declaration_p && cp_parser_error_occurred (parser))
14861     /* Something has already gone wrong; there's no need to parse
14862        further.  Since an error has occurred, the return value of
14863        cp_parser_parse_definitely will be false, as required.  */
14864     return cp_parser_parse_definitely (parser);
14865
14866   token = cp_lexer_peek_token (parser->lexer);
14867   /* Parse the unqualified-id.  */
14868   identifier = cp_parser_unqualified_id (parser,
14869                                          /*template_keyword_p=*/false,
14870                                          /*check_dependency_p=*/true,
14871                                          /*declarator_p=*/true,
14872                                          /*optional_p=*/false);
14873
14874   if (access_declaration_p)
14875     {
14876       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14877         cp_parser_simulate_error (parser);
14878       if (!cp_parser_parse_definitely (parser))
14879         return false;
14880     }
14881
14882   /* The function we call to handle a using-declaration is different
14883      depending on what scope we are in.  */
14884   if (qscope == error_mark_node || identifier == error_mark_node)
14885     ;
14886   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14887            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14888     /* [namespace.udecl]
14889
14890        A using declaration shall not name a template-id.  */
14891     error_at (token->location,
14892               "a template-id may not appear in a using-declaration");
14893   else
14894     {
14895       if (at_class_scope_p ())
14896         {
14897           /* Create the USING_DECL.  */
14898           decl = do_class_using_decl (parser->scope, identifier);
14899
14900           if (check_for_bare_parameter_packs (decl))
14901             return false;
14902           else
14903             /* Add it to the list of members in this class.  */
14904             finish_member_declaration (decl);
14905         }
14906       else
14907         {
14908           decl = cp_parser_lookup_name_simple (parser,
14909                                                identifier,
14910                                                token->location);
14911           if (decl == error_mark_node)
14912             cp_parser_name_lookup_error (parser, identifier,
14913                                          decl, NLE_NULL,
14914                                          token->location);
14915           else if (check_for_bare_parameter_packs (decl))
14916             return false;
14917           else if (!at_namespace_scope_p ())
14918             do_local_using_decl (decl, qscope, identifier);
14919           else
14920             do_toplevel_using_decl (decl, qscope, identifier);
14921         }
14922     }
14923
14924   /* Look for the final `;'.  */
14925   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14926   
14927   return true;
14928 }
14929
14930 /* Parse an alias-declaration.
14931
14932    alias-declaration:
14933      using identifier attribute-specifier-seq [opt] = type-id  */
14934
14935 static tree
14936 cp_parser_alias_declaration (cp_parser* parser)
14937 {
14938   tree id, type, decl, pushed_scope = NULL_TREE, attributes;
14939   location_t id_location;
14940   cp_declarator *declarator;
14941   cp_decl_specifier_seq decl_specs;
14942   bool member_p;
14943
14944   /* Look for the `using' keyword.  */
14945   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14946   id_location = cp_lexer_peek_token (parser->lexer)->location;
14947   id = cp_parser_identifier (parser);
14948   attributes = cp_parser_attributes_opt (parser);
14949   cp_parser_require (parser, CPP_EQ, RT_EQ);
14950
14951   type = cp_parser_type_id (parser);
14952   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14953
14954   if (cp_parser_error_occurred (parser))
14955     return error_mark_node;
14956
14957   /* A typedef-name can also be introduced by an alias-declaration. The
14958      identifier following the using keyword becomes a typedef-name. It has
14959      the same semantics as if it were introduced by the typedef
14960      specifier. In particular, it does not define a new type and it shall
14961      not appear in the type-id.  */
14962
14963   clear_decl_specs (&decl_specs);
14964   decl_specs.type = type;
14965   decl_specs.attributes = attributes;
14966   ++decl_specs.specs[(int) ds_typedef];
14967   ++decl_specs.specs[(int) ds_alias];
14968
14969   declarator = make_id_declarator (NULL_TREE, id, sfk_none);
14970   declarator->id_loc = id_location;
14971
14972   member_p = at_class_scope_p ();
14973   if (member_p)
14974     decl = grokfield (declarator, &decl_specs, NULL_TREE, false,
14975                       NULL_TREE, attributes);
14976   else
14977     decl = start_decl (declarator, &decl_specs, 0,
14978                        attributes, NULL_TREE, &pushed_scope);
14979   if (decl == error_mark_node)
14980     return decl;
14981
14982   cp_finish_decl (decl, NULL_TREE, 0, NULL_TREE, 0);
14983
14984   if (pushed_scope)
14985     pop_scope (pushed_scope);
14986
14987   /* If decl is a template, return its TEMPLATE_DECL so that it gets
14988      added into the symbol table; otherwise, return the TYPE_DECL.  */
14989   if (DECL_LANG_SPECIFIC (decl)
14990       && DECL_TEMPLATE_INFO (decl)
14991       && PRIMARY_TEMPLATE_P (DECL_TI_TEMPLATE (decl)))
14992     {
14993       decl = DECL_TI_TEMPLATE (decl);
14994       if (member_p)
14995         check_member_template (decl);
14996     }
14997
14998   return decl;
14999 }
15000
15001 /* Parse a using-directive.
15002
15003    using-directive:
15004      using namespace :: [opt] nested-name-specifier [opt]
15005        namespace-name ;  */
15006
15007 static void
15008 cp_parser_using_directive (cp_parser* parser)
15009 {
15010   tree namespace_decl;
15011   tree attribs;
15012
15013   /* Look for the `using' keyword.  */
15014   cp_parser_require_keyword (parser, RID_USING, RT_USING);
15015   /* And the `namespace' keyword.  */
15016   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
15017   /* Look for the optional `::' operator.  */
15018   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
15019   /* And the optional nested-name-specifier.  */
15020   cp_parser_nested_name_specifier_opt (parser,
15021                                        /*typename_keyword_p=*/false,
15022                                        /*check_dependency_p=*/true,
15023                                        /*type_p=*/false,
15024                                        /*is_declaration=*/true);
15025   /* Get the namespace being used.  */
15026   namespace_decl = cp_parser_namespace_name (parser);
15027   /* And any specified attributes.  */
15028   attribs = cp_parser_attributes_opt (parser);
15029   /* Update the symbol table.  */
15030   parse_using_directive (namespace_decl, attribs);
15031   /* Look for the final `;'.  */
15032   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15033 }
15034
15035 /* Parse an asm-definition.
15036
15037    asm-definition:
15038      asm ( string-literal ) ;
15039
15040    GNU Extension:
15041
15042    asm-definition:
15043      asm volatile [opt] ( string-literal ) ;
15044      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
15045      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15046                           : asm-operand-list [opt] ) ;
15047      asm volatile [opt] ( string-literal : asm-operand-list [opt]
15048                           : asm-operand-list [opt]
15049                           : asm-clobber-list [opt] ) ;
15050      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
15051                                : asm-clobber-list [opt]
15052                                : asm-goto-list ) ;  */
15053
15054 static void
15055 cp_parser_asm_definition (cp_parser* parser)
15056 {
15057   tree string;
15058   tree outputs = NULL_TREE;
15059   tree inputs = NULL_TREE;
15060   tree clobbers = NULL_TREE;
15061   tree labels = NULL_TREE;
15062   tree asm_stmt;
15063   bool volatile_p = false;
15064   bool extended_p = false;
15065   bool invalid_inputs_p = false;
15066   bool invalid_outputs_p = false;
15067   bool goto_p = false;
15068   required_token missing = RT_NONE;
15069
15070   /* Look for the `asm' keyword.  */
15071   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
15072   /* See if the next token is `volatile'.  */
15073   if (cp_parser_allow_gnu_extensions_p (parser)
15074       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
15075     {
15076       /* Remember that we saw the `volatile' keyword.  */
15077       volatile_p = true;
15078       /* Consume the token.  */
15079       cp_lexer_consume_token (parser->lexer);
15080     }
15081   if (cp_parser_allow_gnu_extensions_p (parser)
15082       && parser->in_function_body
15083       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
15084     {
15085       /* Remember that we saw the `goto' keyword.  */
15086       goto_p = true;
15087       /* Consume the token.  */
15088       cp_lexer_consume_token (parser->lexer);
15089     }
15090   /* Look for the opening `('.  */
15091   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
15092     return;
15093   /* Look for the string.  */
15094   string = cp_parser_string_literal (parser, false, false);
15095   if (string == error_mark_node)
15096     {
15097       cp_parser_skip_to_closing_parenthesis (parser, true, false,
15098                                              /*consume_paren=*/true);
15099       return;
15100     }
15101
15102   /* If we're allowing GNU extensions, check for the extended assembly
15103      syntax.  Unfortunately, the `:' tokens need not be separated by
15104      a space in C, and so, for compatibility, we tolerate that here
15105      too.  Doing that means that we have to treat the `::' operator as
15106      two `:' tokens.  */
15107   if (cp_parser_allow_gnu_extensions_p (parser)
15108       && parser->in_function_body
15109       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
15110           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
15111     {
15112       bool inputs_p = false;
15113       bool clobbers_p = false;
15114       bool labels_p = false;
15115
15116       /* The extended syntax was used.  */
15117       extended_p = true;
15118
15119       /* Look for outputs.  */
15120       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15121         {
15122           /* Consume the `:'.  */
15123           cp_lexer_consume_token (parser->lexer);
15124           /* Parse the output-operands.  */
15125           if (cp_lexer_next_token_is_not (parser->lexer,
15126                                           CPP_COLON)
15127               && cp_lexer_next_token_is_not (parser->lexer,
15128                                              CPP_SCOPE)
15129               && cp_lexer_next_token_is_not (parser->lexer,
15130                                              CPP_CLOSE_PAREN)
15131               && !goto_p)
15132             outputs = cp_parser_asm_operand_list (parser);
15133
15134             if (outputs == error_mark_node)
15135               invalid_outputs_p = true;
15136         }
15137       /* If the next token is `::', there are no outputs, and the
15138          next token is the beginning of the inputs.  */
15139       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15140         /* The inputs are coming next.  */
15141         inputs_p = true;
15142
15143       /* Look for inputs.  */
15144       if (inputs_p
15145           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15146         {
15147           /* Consume the `:' or `::'.  */
15148           cp_lexer_consume_token (parser->lexer);
15149           /* Parse the output-operands.  */
15150           if (cp_lexer_next_token_is_not (parser->lexer,
15151                                           CPP_COLON)
15152               && cp_lexer_next_token_is_not (parser->lexer,
15153                                              CPP_SCOPE)
15154               && cp_lexer_next_token_is_not (parser->lexer,
15155                                              CPP_CLOSE_PAREN))
15156             inputs = cp_parser_asm_operand_list (parser);
15157
15158             if (inputs == error_mark_node)
15159               invalid_inputs_p = true;
15160         }
15161       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15162         /* The clobbers are coming next.  */
15163         clobbers_p = true;
15164
15165       /* Look for clobbers.  */
15166       if (clobbers_p
15167           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15168         {
15169           clobbers_p = true;
15170           /* Consume the `:' or `::'.  */
15171           cp_lexer_consume_token (parser->lexer);
15172           /* Parse the clobbers.  */
15173           if (cp_lexer_next_token_is_not (parser->lexer,
15174                                           CPP_COLON)
15175               && cp_lexer_next_token_is_not (parser->lexer,
15176                                              CPP_CLOSE_PAREN))
15177             clobbers = cp_parser_asm_clobber_list (parser);
15178         }
15179       else if (goto_p
15180                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15181         /* The labels are coming next.  */
15182         labels_p = true;
15183
15184       /* Look for labels.  */
15185       if (labels_p
15186           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15187         {
15188           labels_p = true;
15189           /* Consume the `:' or `::'.  */
15190           cp_lexer_consume_token (parser->lexer);
15191           /* Parse the labels.  */
15192           labels = cp_parser_asm_label_list (parser);
15193         }
15194
15195       if (goto_p && !labels_p)
15196         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15197     }
15198   else if (goto_p)
15199     missing = RT_COLON_SCOPE;
15200
15201   /* Look for the closing `)'.  */
15202   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15203                           missing ? missing : RT_CLOSE_PAREN))
15204     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15205                                            /*consume_paren=*/true);
15206   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15207
15208   if (!invalid_inputs_p && !invalid_outputs_p)
15209     {
15210       /* Create the ASM_EXPR.  */
15211       if (parser->in_function_body)
15212         {
15213           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15214                                       inputs, clobbers, labels);
15215           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15216           if (!extended_p)
15217             {
15218               tree temp = asm_stmt;
15219               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15220                 temp = TREE_OPERAND (temp, 0);
15221
15222               ASM_INPUT_P (temp) = 1;
15223             }
15224         }
15225       else
15226         cgraph_add_asm_node (string);
15227     }
15228 }
15229
15230 /* Declarators [gram.dcl.decl] */
15231
15232 /* Parse an init-declarator.
15233
15234    init-declarator:
15235      declarator initializer [opt]
15236
15237    GNU Extension:
15238
15239    init-declarator:
15240      declarator asm-specification [opt] attributes [opt] initializer [opt]
15241
15242    function-definition:
15243      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15244        function-body
15245      decl-specifier-seq [opt] declarator function-try-block
15246
15247    GNU Extension:
15248
15249    function-definition:
15250      __extension__ function-definition
15251
15252    TM Extension:
15253
15254    function-definition:
15255      decl-specifier-seq [opt] declarator function-transaction-block
15256
15257    The DECL_SPECIFIERS apply to this declarator.  Returns a
15258    representation of the entity declared.  If MEMBER_P is TRUE, then
15259    this declarator appears in a class scope.  The new DECL created by
15260    this declarator is returned.
15261
15262    The CHECKS are access checks that should be performed once we know
15263    what entity is being declared (and, therefore, what classes have
15264    befriended it).
15265
15266    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15267    for a function-definition here as well.  If the declarator is a
15268    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15269    be TRUE upon return.  By that point, the function-definition will
15270    have been completely parsed.
15271
15272    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15273    is FALSE.
15274
15275    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15276    parsed declaration if it is an uninitialized single declarator not followed
15277    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15278    if present, will not be consumed.  If returned, this declarator will be
15279    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15280
15281 static tree
15282 cp_parser_init_declarator (cp_parser* parser,
15283                            cp_decl_specifier_seq *decl_specifiers,
15284                            VEC (deferred_access_check,gc)* checks,
15285                            bool function_definition_allowed_p,
15286                            bool member_p,
15287                            int declares_class_or_enum,
15288                            bool* function_definition_p,
15289                            tree* maybe_range_for_decl)
15290 {
15291   cp_token *token = NULL, *asm_spec_start_token = NULL,
15292            *attributes_start_token = NULL;
15293   cp_declarator *declarator;
15294   tree prefix_attributes;
15295   tree attributes;
15296   tree asm_specification;
15297   tree initializer;
15298   tree decl = NULL_TREE;
15299   tree scope;
15300   int is_initialized;
15301   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15302      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15303      "(...)".  */
15304   enum cpp_ttype initialization_kind;
15305   bool is_direct_init = false;
15306   bool is_non_constant_init;
15307   int ctor_dtor_or_conv_p;
15308   bool friend_p;
15309   tree pushed_scope = NULL_TREE;
15310   bool range_for_decl_p = false;
15311
15312   /* Gather the attributes that were provided with the
15313      decl-specifiers.  */
15314   prefix_attributes = decl_specifiers->attributes;
15315
15316   /* Assume that this is not the declarator for a function
15317      definition.  */
15318   if (function_definition_p)
15319     *function_definition_p = false;
15320
15321   /* Defer access checks while parsing the declarator; we cannot know
15322      what names are accessible until we know what is being
15323      declared.  */
15324   resume_deferring_access_checks ();
15325
15326   /* Parse the declarator.  */
15327   token = cp_lexer_peek_token (parser->lexer);
15328   declarator
15329     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15330                             &ctor_dtor_or_conv_p,
15331                             /*parenthesized_p=*/NULL,
15332                             member_p);
15333   /* Gather up the deferred checks.  */
15334   stop_deferring_access_checks ();
15335
15336   /* If the DECLARATOR was erroneous, there's no need to go
15337      further.  */
15338   if (declarator == cp_error_declarator)
15339     return error_mark_node;
15340
15341   /* Check that the number of template-parameter-lists is OK.  */
15342   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15343                                                        token->location))
15344     return error_mark_node;
15345
15346   if (declares_class_or_enum & 2)
15347     cp_parser_check_for_definition_in_return_type (declarator,
15348                                                    decl_specifiers->type,
15349                                                    decl_specifiers->type_location);
15350
15351   /* Figure out what scope the entity declared by the DECLARATOR is
15352      located in.  `grokdeclarator' sometimes changes the scope, so
15353      we compute it now.  */
15354   scope = get_scope_of_declarator (declarator);
15355
15356   /* Perform any lookups in the declared type which were thought to be
15357      dependent, but are not in the scope of the declarator.  */
15358   decl_specifiers->type
15359     = maybe_update_decl_type (decl_specifiers->type, scope);
15360
15361   /* If we're allowing GNU extensions, look for an asm-specification
15362      and attributes.  */
15363   if (cp_parser_allow_gnu_extensions_p (parser))
15364     {
15365       /* Look for an asm-specification.  */
15366       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15367       asm_specification = cp_parser_asm_specification_opt (parser);
15368       /* And attributes.  */
15369       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15370       attributes = cp_parser_attributes_opt (parser);
15371     }
15372   else
15373     {
15374       asm_specification = NULL_TREE;
15375       attributes = NULL_TREE;
15376     }
15377
15378   /* Peek at the next token.  */
15379   token = cp_lexer_peek_token (parser->lexer);
15380   /* Check to see if the token indicates the start of a
15381      function-definition.  */
15382   if (function_declarator_p (declarator)
15383       && cp_parser_token_starts_function_definition_p (token))
15384     {
15385       if (!function_definition_allowed_p)
15386         {
15387           /* If a function-definition should not appear here, issue an
15388              error message.  */
15389           cp_parser_error (parser,
15390                            "a function-definition is not allowed here");
15391           return error_mark_node;
15392         }
15393       else
15394         {
15395           location_t func_brace_location
15396             = cp_lexer_peek_token (parser->lexer)->location;
15397
15398           /* Neither attributes nor an asm-specification are allowed
15399              on a function-definition.  */
15400           if (asm_specification)
15401             error_at (asm_spec_start_token->location,
15402                       "an asm-specification is not allowed "
15403                       "on a function-definition");
15404           if (attributes)
15405             error_at (attributes_start_token->location,
15406                       "attributes are not allowed on a function-definition");
15407           /* This is a function-definition.  */
15408           *function_definition_p = true;
15409
15410           /* Parse the function definition.  */
15411           if (member_p)
15412             decl = cp_parser_save_member_function_body (parser,
15413                                                         decl_specifiers,
15414                                                         declarator,
15415                                                         prefix_attributes);
15416           else
15417             decl
15418               = (cp_parser_function_definition_from_specifiers_and_declarator
15419                  (parser, decl_specifiers, prefix_attributes, declarator));
15420
15421           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15422             {
15423               /* This is where the prologue starts...  */
15424               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15425                 = func_brace_location;
15426             }
15427
15428           return decl;
15429         }
15430     }
15431
15432   /* [dcl.dcl]
15433
15434      Only in function declarations for constructors, destructors, and
15435      type conversions can the decl-specifier-seq be omitted.
15436
15437      We explicitly postpone this check past the point where we handle
15438      function-definitions because we tolerate function-definitions
15439      that are missing their return types in some modes.  */
15440   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15441     {
15442       cp_parser_error (parser,
15443                        "expected constructor, destructor, or type conversion");
15444       return error_mark_node;
15445     }
15446
15447   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15448   if (token->type == CPP_EQ
15449       || token->type == CPP_OPEN_PAREN
15450       || token->type == CPP_OPEN_BRACE)
15451     {
15452       is_initialized = SD_INITIALIZED;
15453       initialization_kind = token->type;
15454       if (maybe_range_for_decl)
15455         *maybe_range_for_decl = error_mark_node;
15456
15457       if (token->type == CPP_EQ
15458           && function_declarator_p (declarator))
15459         {
15460           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15461           if (t2->keyword == RID_DEFAULT)
15462             is_initialized = SD_DEFAULTED;
15463           else if (t2->keyword == RID_DELETE)
15464             is_initialized = SD_DELETED;
15465         }
15466     }
15467   else
15468     {
15469       /* If the init-declarator isn't initialized and isn't followed by a
15470          `,' or `;', it's not a valid init-declarator.  */
15471       if (token->type != CPP_COMMA
15472           && token->type != CPP_SEMICOLON)
15473         {
15474           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15475             range_for_decl_p = true;
15476           else
15477             {
15478               cp_parser_error (parser, "expected initializer");
15479               return error_mark_node;
15480             }
15481         }
15482       is_initialized = SD_UNINITIALIZED;
15483       initialization_kind = CPP_EOF;
15484     }
15485
15486   /* Because start_decl has side-effects, we should only call it if we
15487      know we're going ahead.  By this point, we know that we cannot
15488      possibly be looking at any other construct.  */
15489   cp_parser_commit_to_tentative_parse (parser);
15490
15491   /* If the decl specifiers were bad, issue an error now that we're
15492      sure this was intended to be a declarator.  Then continue
15493      declaring the variable(s), as int, to try to cut down on further
15494      errors.  */
15495   if (decl_specifiers->any_specifiers_p
15496       && decl_specifiers->type == error_mark_node)
15497     {
15498       cp_parser_error (parser, "invalid type in declaration");
15499       decl_specifiers->type = integer_type_node;
15500     }
15501
15502   /* Check to see whether or not this declaration is a friend.  */
15503   friend_p = cp_parser_friend_p (decl_specifiers);
15504
15505   /* Enter the newly declared entry in the symbol table.  If we're
15506      processing a declaration in a class-specifier, we wait until
15507      after processing the initializer.  */
15508   if (!member_p)
15509     {
15510       if (parser->in_unbraced_linkage_specification_p)
15511         decl_specifiers->storage_class = sc_extern;
15512       decl = start_decl (declarator, decl_specifiers,
15513                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15514                          attributes, prefix_attributes,
15515                          &pushed_scope);
15516       /* Adjust location of decl if declarator->id_loc is more appropriate:
15517          set, and decl wasn't merged with another decl, in which case its
15518          location would be different from input_location, and more accurate.  */
15519       if (DECL_P (decl)
15520           && declarator->id_loc != UNKNOWN_LOCATION
15521           && DECL_SOURCE_LOCATION (decl) == input_location)
15522         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15523     }
15524   else if (scope)
15525     /* Enter the SCOPE.  That way unqualified names appearing in the
15526        initializer will be looked up in SCOPE.  */
15527     pushed_scope = push_scope (scope);
15528
15529   /* Perform deferred access control checks, now that we know in which
15530      SCOPE the declared entity resides.  */
15531   if (!member_p && decl)
15532     {
15533       tree saved_current_function_decl = NULL_TREE;
15534
15535       /* If the entity being declared is a function, pretend that we
15536          are in its scope.  If it is a `friend', it may have access to
15537          things that would not otherwise be accessible.  */
15538       if (TREE_CODE (decl) == FUNCTION_DECL)
15539         {
15540           saved_current_function_decl = current_function_decl;
15541           current_function_decl = decl;
15542         }
15543
15544       /* Perform access checks for template parameters.  */
15545       cp_parser_perform_template_parameter_access_checks (checks);
15546
15547       /* Perform the access control checks for the declarator and the
15548          decl-specifiers.  */
15549       perform_deferred_access_checks ();
15550
15551       /* Restore the saved value.  */
15552       if (TREE_CODE (decl) == FUNCTION_DECL)
15553         current_function_decl = saved_current_function_decl;
15554     }
15555
15556   /* Parse the initializer.  */
15557   initializer = NULL_TREE;
15558   is_direct_init = false;
15559   is_non_constant_init = true;
15560   if (is_initialized)
15561     {
15562       if (function_declarator_p (declarator))
15563         {
15564           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15565            if (initialization_kind == CPP_EQ)
15566              initializer = cp_parser_pure_specifier (parser);
15567            else
15568              {
15569                /* If the declaration was erroneous, we don't really
15570                   know what the user intended, so just silently
15571                   consume the initializer.  */
15572                if (decl != error_mark_node)
15573                  error_at (initializer_start_token->location,
15574                            "initializer provided for function");
15575                cp_parser_skip_to_closing_parenthesis (parser,
15576                                                       /*recovering=*/true,
15577                                                       /*or_comma=*/false,
15578                                                       /*consume_paren=*/true);
15579              }
15580         }
15581       else
15582         {
15583           /* We want to record the extra mangling scope for in-class
15584              initializers of class members and initializers of static data
15585              member templates.  The former is a C++0x feature which isn't
15586              implemented yet, and I expect it will involve deferring
15587              parsing of the initializer until end of class as with default
15588              arguments.  So right here we only handle the latter.  */
15589           if (!member_p && processing_template_decl)
15590             start_lambda_scope (decl);
15591           initializer = cp_parser_initializer (parser,
15592                                                &is_direct_init,
15593                                                &is_non_constant_init);
15594           if (!member_p && processing_template_decl)
15595             finish_lambda_scope ();
15596         }
15597     }
15598
15599   /* The old parser allows attributes to appear after a parenthesized
15600      initializer.  Mark Mitchell proposed removing this functionality
15601      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15602      attributes -- but ignores them.  */
15603   if (cp_parser_allow_gnu_extensions_p (parser)
15604       && initialization_kind == CPP_OPEN_PAREN)
15605     if (cp_parser_attributes_opt (parser))
15606       warning (OPT_Wattributes,
15607                "attributes after parenthesized initializer ignored");
15608
15609   /* For an in-class declaration, use `grokfield' to create the
15610      declaration.  */
15611   if (member_p)
15612     {
15613       if (pushed_scope)
15614         {
15615           pop_scope (pushed_scope);
15616           pushed_scope = NULL_TREE;
15617         }
15618       decl = grokfield (declarator, decl_specifiers,
15619                         initializer, !is_non_constant_init,
15620                         /*asmspec=*/NULL_TREE,
15621                         prefix_attributes);
15622       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15623         cp_parser_save_default_args (parser, decl);
15624     }
15625
15626   /* Finish processing the declaration.  But, skip member
15627      declarations.  */
15628   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15629     {
15630       cp_finish_decl (decl,
15631                       initializer, !is_non_constant_init,
15632                       asm_specification,
15633                       /* If the initializer is in parentheses, then this is
15634                          a direct-initialization, which means that an
15635                          `explicit' constructor is OK.  Otherwise, an
15636                          `explicit' constructor cannot be used.  */
15637                       ((is_direct_init || !is_initialized)
15638                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15639     }
15640   else if ((cxx_dialect != cxx98) && friend_p
15641            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15642     /* Core issue #226 (C++0x only): A default template-argument
15643        shall not be specified in a friend class template
15644        declaration. */
15645     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15646                              /*is_partial=*/0, /*is_friend_decl=*/1);
15647
15648   if (!friend_p && pushed_scope)
15649     pop_scope (pushed_scope);
15650
15651   return decl;
15652 }
15653
15654 /* Parse a declarator.
15655
15656    declarator:
15657      direct-declarator
15658      ptr-operator declarator
15659
15660    abstract-declarator:
15661      ptr-operator abstract-declarator [opt]
15662      direct-abstract-declarator
15663
15664    GNU Extensions:
15665
15666    declarator:
15667      attributes [opt] direct-declarator
15668      attributes [opt] ptr-operator declarator
15669
15670    abstract-declarator:
15671      attributes [opt] ptr-operator abstract-declarator [opt]
15672      attributes [opt] direct-abstract-declarator
15673
15674    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15675    detect constructor, destructor or conversion operators. It is set
15676    to -1 if the declarator is a name, and +1 if it is a
15677    function. Otherwise it is set to zero. Usually you just want to
15678    test for >0, but internally the negative value is used.
15679
15680    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15681    a decl-specifier-seq unless it declares a constructor, destructor,
15682    or conversion.  It might seem that we could check this condition in
15683    semantic analysis, rather than parsing, but that makes it difficult
15684    to handle something like `f()'.  We want to notice that there are
15685    no decl-specifiers, and therefore realize that this is an
15686    expression, not a declaration.)
15687
15688    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15689    the declarator is a direct-declarator of the form "(...)".
15690
15691    MEMBER_P is true iff this declarator is a member-declarator.  */
15692
15693 static cp_declarator *
15694 cp_parser_declarator (cp_parser* parser,
15695                       cp_parser_declarator_kind dcl_kind,
15696                       int* ctor_dtor_or_conv_p,
15697                       bool* parenthesized_p,
15698                       bool member_p)
15699 {
15700   cp_declarator *declarator;
15701   enum tree_code code;
15702   cp_cv_quals cv_quals;
15703   tree class_type;
15704   tree attributes = NULL_TREE;
15705
15706   /* Assume this is not a constructor, destructor, or type-conversion
15707      operator.  */
15708   if (ctor_dtor_or_conv_p)
15709     *ctor_dtor_or_conv_p = 0;
15710
15711   if (cp_parser_allow_gnu_extensions_p (parser))
15712     attributes = cp_parser_attributes_opt (parser);
15713
15714   /* Check for the ptr-operator production.  */
15715   cp_parser_parse_tentatively (parser);
15716   /* Parse the ptr-operator.  */
15717   code = cp_parser_ptr_operator (parser,
15718                                  &class_type,
15719                                  &cv_quals);
15720   /* If that worked, then we have a ptr-operator.  */
15721   if (cp_parser_parse_definitely (parser))
15722     {
15723       /* If a ptr-operator was found, then this declarator was not
15724          parenthesized.  */
15725       if (parenthesized_p)
15726         *parenthesized_p = true;
15727       /* The dependent declarator is optional if we are parsing an
15728          abstract-declarator.  */
15729       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15730         cp_parser_parse_tentatively (parser);
15731
15732       /* Parse the dependent declarator.  */
15733       declarator = cp_parser_declarator (parser, dcl_kind,
15734                                          /*ctor_dtor_or_conv_p=*/NULL,
15735                                          /*parenthesized_p=*/NULL,
15736                                          /*member_p=*/false);
15737
15738       /* If we are parsing an abstract-declarator, we must handle the
15739          case where the dependent declarator is absent.  */
15740       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15741           && !cp_parser_parse_definitely (parser))
15742         declarator = NULL;
15743
15744       declarator = cp_parser_make_indirect_declarator
15745         (code, class_type, cv_quals, declarator);
15746     }
15747   /* Everything else is a direct-declarator.  */
15748   else
15749     {
15750       if (parenthesized_p)
15751         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15752                                                    CPP_OPEN_PAREN);
15753       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15754                                                 ctor_dtor_or_conv_p,
15755                                                 member_p);
15756     }
15757
15758   if (attributes && declarator && declarator != cp_error_declarator)
15759     declarator->attributes = attributes;
15760
15761   return declarator;
15762 }
15763
15764 /* Parse a direct-declarator or direct-abstract-declarator.
15765
15766    direct-declarator:
15767      declarator-id
15768      direct-declarator ( parameter-declaration-clause )
15769        cv-qualifier-seq [opt]
15770        exception-specification [opt]
15771      direct-declarator [ constant-expression [opt] ]
15772      ( declarator )
15773
15774    direct-abstract-declarator:
15775      direct-abstract-declarator [opt]
15776        ( parameter-declaration-clause )
15777        cv-qualifier-seq [opt]
15778        exception-specification [opt]
15779      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15780      ( abstract-declarator )
15781
15782    Returns a representation of the declarator.  DCL_KIND is
15783    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15784    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15785    we are parsing a direct-declarator.  It is
15786    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15787    of ambiguity we prefer an abstract declarator, as per
15788    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15789    cp_parser_declarator.  */
15790
15791 static cp_declarator *
15792 cp_parser_direct_declarator (cp_parser* parser,
15793                              cp_parser_declarator_kind dcl_kind,
15794                              int* ctor_dtor_or_conv_p,
15795                              bool member_p)
15796 {
15797   cp_token *token;
15798   cp_declarator *declarator = NULL;
15799   tree scope = NULL_TREE;
15800   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15801   bool saved_in_declarator_p = parser->in_declarator_p;
15802   bool first = true;
15803   tree pushed_scope = NULL_TREE;
15804
15805   while (true)
15806     {
15807       /* Peek at the next token.  */
15808       token = cp_lexer_peek_token (parser->lexer);
15809       if (token->type == CPP_OPEN_PAREN)
15810         {
15811           /* This is either a parameter-declaration-clause, or a
15812              parenthesized declarator. When we know we are parsing a
15813              named declarator, it must be a parenthesized declarator
15814              if FIRST is true. For instance, `(int)' is a
15815              parameter-declaration-clause, with an omitted
15816              direct-abstract-declarator. But `((*))', is a
15817              parenthesized abstract declarator. Finally, when T is a
15818              template parameter `(T)' is a
15819              parameter-declaration-clause, and not a parenthesized
15820              named declarator.
15821
15822              We first try and parse a parameter-declaration-clause,
15823              and then try a nested declarator (if FIRST is true).
15824
15825              It is not an error for it not to be a
15826              parameter-declaration-clause, even when FIRST is
15827              false. Consider,
15828
15829                int i (int);
15830                int i (3);
15831
15832              The first is the declaration of a function while the
15833              second is the definition of a variable, including its
15834              initializer.
15835
15836              Having seen only the parenthesis, we cannot know which of
15837              these two alternatives should be selected.  Even more
15838              complex are examples like:
15839
15840                int i (int (a));
15841                int i (int (3));
15842
15843              The former is a function-declaration; the latter is a
15844              variable initialization.
15845
15846              Thus again, we try a parameter-declaration-clause, and if
15847              that fails, we back out and return.  */
15848
15849           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15850             {
15851               tree params;
15852               unsigned saved_num_template_parameter_lists;
15853               bool is_declarator = false;
15854               tree t;
15855
15856               /* In a member-declarator, the only valid interpretation
15857                  of a parenthesis is the start of a
15858                  parameter-declaration-clause.  (It is invalid to
15859                  initialize a static data member with a parenthesized
15860                  initializer; only the "=" form of initialization is
15861                  permitted.)  */
15862               if (!member_p)
15863                 cp_parser_parse_tentatively (parser);
15864
15865               /* Consume the `('.  */
15866               cp_lexer_consume_token (parser->lexer);
15867               if (first)
15868                 {
15869                   /* If this is going to be an abstract declarator, we're
15870                      in a declarator and we can't have default args.  */
15871                   parser->default_arg_ok_p = false;
15872                   parser->in_declarator_p = true;
15873                 }
15874
15875               /* Inside the function parameter list, surrounding
15876                  template-parameter-lists do not apply.  */
15877               saved_num_template_parameter_lists
15878                 = parser->num_template_parameter_lists;
15879               parser->num_template_parameter_lists = 0;
15880
15881               begin_scope (sk_function_parms, NULL_TREE);
15882
15883               /* Parse the parameter-declaration-clause.  */
15884               params = cp_parser_parameter_declaration_clause (parser);
15885
15886               parser->num_template_parameter_lists
15887                 = saved_num_template_parameter_lists;
15888
15889               /* Consume the `)'.  */
15890               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15891
15892               /* If all went well, parse the cv-qualifier-seq and the
15893                  exception-specification.  */
15894               if (member_p || cp_parser_parse_definitely (parser))
15895                 {
15896                   cp_cv_quals cv_quals;
15897                   cp_virt_specifiers virt_specifiers;
15898                   tree exception_specification;
15899                   tree late_return;
15900
15901                   is_declarator = true;
15902
15903                   if (ctor_dtor_or_conv_p)
15904                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15905                   first = false;
15906
15907                   /* Parse the cv-qualifier-seq.  */
15908                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15909                   /* And the exception-specification.  */
15910                   exception_specification
15911                     = cp_parser_exception_specification_opt (parser);
15912                   /* Parse the virt-specifier-seq.  */
15913                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15914
15915                   late_return = (cp_parser_late_return_type_opt
15916                                  (parser, member_p ? cv_quals : -1));
15917
15918                   /* Create the function-declarator.  */
15919                   declarator = make_call_declarator (declarator,
15920                                                      params,
15921                                                      cv_quals,
15922                                                      virt_specifiers,
15923                                                      exception_specification,
15924                                                      late_return);
15925                   /* Any subsequent parameter lists are to do with
15926                      return type, so are not those of the declared
15927                      function.  */
15928                   parser->default_arg_ok_p = false;
15929                 }
15930
15931               /* Remove the function parms from scope.  */
15932               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15933                 pop_binding (DECL_NAME (t), t);
15934               leave_scope();
15935
15936               if (is_declarator)
15937                 /* Repeat the main loop.  */
15938                 continue;
15939             }
15940
15941           /* If this is the first, we can try a parenthesized
15942              declarator.  */
15943           if (first)
15944             {
15945               bool saved_in_type_id_in_expr_p;
15946
15947               parser->default_arg_ok_p = saved_default_arg_ok_p;
15948               parser->in_declarator_p = saved_in_declarator_p;
15949
15950               /* Consume the `('.  */
15951               cp_lexer_consume_token (parser->lexer);
15952               /* Parse the nested declarator.  */
15953               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15954               parser->in_type_id_in_expr_p = true;
15955               declarator
15956                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15957                                         /*parenthesized_p=*/NULL,
15958                                         member_p);
15959               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15960               first = false;
15961               /* Expect a `)'.  */
15962               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15963                 declarator = cp_error_declarator;
15964               if (declarator == cp_error_declarator)
15965                 break;
15966
15967               goto handle_declarator;
15968             }
15969           /* Otherwise, we must be done.  */
15970           else
15971             break;
15972         }
15973       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15974                && token->type == CPP_OPEN_SQUARE)
15975         {
15976           /* Parse an array-declarator.  */
15977           tree bounds;
15978
15979           if (ctor_dtor_or_conv_p)
15980             *ctor_dtor_or_conv_p = 0;
15981
15982           first = false;
15983           parser->default_arg_ok_p = false;
15984           parser->in_declarator_p = true;
15985           /* Consume the `['.  */
15986           cp_lexer_consume_token (parser->lexer);
15987           /* Peek at the next token.  */
15988           token = cp_lexer_peek_token (parser->lexer);
15989           /* If the next token is `]', then there is no
15990              constant-expression.  */
15991           if (token->type != CPP_CLOSE_SQUARE)
15992             {
15993               bool non_constant_p;
15994
15995               bounds
15996                 = cp_parser_constant_expression (parser,
15997                                                  /*allow_non_constant=*/true,
15998                                                  &non_constant_p);
15999               if (!non_constant_p)
16000                 /* OK */;
16001               /* Normally, the array bound must be an integral constant
16002                  expression.  However, as an extension, we allow VLAs
16003                  in function scopes as long as they aren't part of a
16004                  parameter declaration.  */
16005               else if (!parser->in_function_body
16006                        || current_binding_level->kind == sk_function_parms)
16007                 {
16008                   cp_parser_error (parser,
16009                                    "array bound is not an integer constant");
16010                   bounds = error_mark_node;
16011                 }
16012               else if (processing_template_decl && !error_operand_p (bounds))
16013                 {
16014                   /* Remember this wasn't a constant-expression.  */
16015                   bounds = build_nop (TREE_TYPE (bounds), bounds);
16016                   TREE_SIDE_EFFECTS (bounds) = 1;
16017                 }
16018             }
16019           else
16020             bounds = NULL_TREE;
16021           /* Look for the closing `]'.  */
16022           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
16023             {
16024               declarator = cp_error_declarator;
16025               break;
16026             }
16027
16028           declarator = make_array_declarator (declarator, bounds);
16029         }
16030       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
16031         {
16032           {
16033             tree qualifying_scope;
16034             tree unqualified_name;
16035             special_function_kind sfk;
16036             bool abstract_ok;
16037             bool pack_expansion_p = false;
16038             cp_token *declarator_id_start_token;
16039
16040             /* Parse a declarator-id */
16041             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
16042             if (abstract_ok)
16043               {
16044                 cp_parser_parse_tentatively (parser);
16045
16046                 /* If we see an ellipsis, we should be looking at a
16047                    parameter pack. */
16048                 if (token->type == CPP_ELLIPSIS)
16049                   {
16050                     /* Consume the `...' */
16051                     cp_lexer_consume_token (parser->lexer);
16052
16053                     pack_expansion_p = true;
16054                   }
16055               }
16056
16057             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
16058             unqualified_name
16059               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
16060             qualifying_scope = parser->scope;
16061             if (abstract_ok)
16062               {
16063                 bool okay = false;
16064
16065                 if (!unqualified_name && pack_expansion_p)
16066                   {
16067                     /* Check whether an error occurred. */
16068                     okay = !cp_parser_error_occurred (parser);
16069
16070                     /* We already consumed the ellipsis to mark a
16071                        parameter pack, but we have no way to report it,
16072                        so abort the tentative parse. We will be exiting
16073                        immediately anyway. */
16074                     cp_parser_abort_tentative_parse (parser);
16075                   }
16076                 else
16077                   okay = cp_parser_parse_definitely (parser);
16078
16079                 if (!okay)
16080                   unqualified_name = error_mark_node;
16081                 else if (unqualified_name
16082                          && (qualifying_scope
16083                              || (TREE_CODE (unqualified_name)
16084                                  != IDENTIFIER_NODE)))
16085                   {
16086                     cp_parser_error (parser, "expected unqualified-id");
16087                     unqualified_name = error_mark_node;
16088                   }
16089               }
16090
16091             if (!unqualified_name)
16092               return NULL;
16093             if (unqualified_name == error_mark_node)
16094               {
16095                 declarator = cp_error_declarator;
16096                 pack_expansion_p = false;
16097                 declarator->parameter_pack_p = false;
16098                 break;
16099               }
16100
16101             if (qualifying_scope && at_namespace_scope_p ()
16102                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
16103               {
16104                 /* In the declaration of a member of a template class
16105                    outside of the class itself, the SCOPE will sometimes
16106                    be a TYPENAME_TYPE.  For example, given:
16107
16108                    template <typename T>
16109                    int S<T>::R::i = 3;
16110
16111                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
16112                    this context, we must resolve S<T>::R to an ordinary
16113                    type, rather than a typename type.
16114
16115                    The reason we normally avoid resolving TYPENAME_TYPEs
16116                    is that a specialization of `S' might render
16117                    `S<T>::R' not a type.  However, if `S' is
16118                    specialized, then this `i' will not be used, so there
16119                    is no harm in resolving the types here.  */
16120                 tree type;
16121
16122                 /* Resolve the TYPENAME_TYPE.  */
16123                 type = resolve_typename_type (qualifying_scope,
16124                                               /*only_current_p=*/false);
16125                 /* If that failed, the declarator is invalid.  */
16126                 if (TREE_CODE (type) == TYPENAME_TYPE)
16127                   {
16128                     if (typedef_variant_p (type))
16129                       error_at (declarator_id_start_token->location,
16130                                 "cannot define member of dependent typedef "
16131                                 "%qT", type);
16132                     else
16133                       error_at (declarator_id_start_token->location,
16134                                 "%<%T::%E%> is not a type",
16135                                 TYPE_CONTEXT (qualifying_scope),
16136                                 TYPE_IDENTIFIER (qualifying_scope));
16137                   }
16138                 qualifying_scope = type;
16139               }
16140
16141             sfk = sfk_none;
16142
16143             if (unqualified_name)
16144               {
16145                 tree class_type;
16146
16147                 if (qualifying_scope
16148                     && CLASS_TYPE_P (qualifying_scope))
16149                   class_type = qualifying_scope;
16150                 else
16151                   class_type = current_class_type;
16152
16153                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
16154                   {
16155                     tree name_type = TREE_TYPE (unqualified_name);
16156                     if (class_type && same_type_p (name_type, class_type))
16157                       {
16158                         if (qualifying_scope
16159                             && CLASSTYPE_USE_TEMPLATE (name_type))
16160                           {
16161                             error_at (declarator_id_start_token->location,
16162                                       "invalid use of constructor as a template");
16163                             inform (declarator_id_start_token->location,
16164                                     "use %<%T::%D%> instead of %<%T::%D%> to "
16165                                     "name the constructor in a qualified name",
16166                                     class_type,
16167                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
16168                                     class_type, name_type);
16169                             declarator = cp_error_declarator;
16170                             break;
16171                           }
16172                         else
16173                           unqualified_name = constructor_name (class_type);
16174                       }
16175                     else
16176                       {
16177                         /* We do not attempt to print the declarator
16178                            here because we do not have enough
16179                            information about its original syntactic
16180                            form.  */
16181                         cp_parser_error (parser, "invalid declarator");
16182                         declarator = cp_error_declarator;
16183                         break;
16184                       }
16185                   }
16186
16187                 if (class_type)
16188                   {
16189                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16190                       sfk = sfk_destructor;
16191                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16192                       sfk = sfk_conversion;
16193                     else if (/* There's no way to declare a constructor
16194                                 for an anonymous type, even if the type
16195                                 got a name for linkage purposes.  */
16196                              !TYPE_WAS_ANONYMOUS (class_type)
16197                              && constructor_name_p (unqualified_name,
16198                                                     class_type))
16199                       {
16200                         unqualified_name = constructor_name (class_type);
16201                         sfk = sfk_constructor;
16202                       }
16203                     else if (is_overloaded_fn (unqualified_name)
16204                              && DECL_CONSTRUCTOR_P (get_first_fn
16205                                                     (unqualified_name)))
16206                       sfk = sfk_constructor;
16207
16208                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16209                       *ctor_dtor_or_conv_p = -1;
16210                   }
16211               }
16212             declarator = make_id_declarator (qualifying_scope,
16213                                              unqualified_name,
16214                                              sfk);
16215             declarator->id_loc = token->location;
16216             declarator->parameter_pack_p = pack_expansion_p;
16217
16218             if (pack_expansion_p)
16219               maybe_warn_variadic_templates ();
16220           }
16221
16222         handle_declarator:;
16223           scope = get_scope_of_declarator (declarator);
16224           if (scope)
16225             /* Any names that appear after the declarator-id for a
16226                member are looked up in the containing scope.  */
16227             pushed_scope = push_scope (scope);
16228           parser->in_declarator_p = true;
16229           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16230               || (declarator && declarator->kind == cdk_id))
16231             /* Default args are only allowed on function
16232                declarations.  */
16233             parser->default_arg_ok_p = saved_default_arg_ok_p;
16234           else
16235             parser->default_arg_ok_p = false;
16236
16237           first = false;
16238         }
16239       /* We're done.  */
16240       else
16241         break;
16242     }
16243
16244   /* For an abstract declarator, we might wind up with nothing at this
16245      point.  That's an error; the declarator is not optional.  */
16246   if (!declarator)
16247     cp_parser_error (parser, "expected declarator");
16248
16249   /* If we entered a scope, we must exit it now.  */
16250   if (pushed_scope)
16251     pop_scope (pushed_scope);
16252
16253   parser->default_arg_ok_p = saved_default_arg_ok_p;
16254   parser->in_declarator_p = saved_in_declarator_p;
16255
16256   return declarator;
16257 }
16258
16259 /* Parse a ptr-operator.
16260
16261    ptr-operator:
16262      * cv-qualifier-seq [opt]
16263      &
16264      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16265
16266    GNU Extension:
16267
16268    ptr-operator:
16269      & cv-qualifier-seq [opt]
16270
16271    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16272    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16273    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16274    filled in with the TYPE containing the member.  *CV_QUALS is
16275    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16276    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16277    Note that the tree codes returned by this function have nothing
16278    to do with the types of trees that will be eventually be created
16279    to represent the pointer or reference type being parsed. They are
16280    just constants with suggestive names. */
16281 static enum tree_code
16282 cp_parser_ptr_operator (cp_parser* parser,
16283                         tree* type,
16284                         cp_cv_quals *cv_quals)
16285 {
16286   enum tree_code code = ERROR_MARK;
16287   cp_token *token;
16288
16289   /* Assume that it's not a pointer-to-member.  */
16290   *type = NULL_TREE;
16291   /* And that there are no cv-qualifiers.  */
16292   *cv_quals = TYPE_UNQUALIFIED;
16293
16294   /* Peek at the next token.  */
16295   token = cp_lexer_peek_token (parser->lexer);
16296
16297   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16298   if (token->type == CPP_MULT)
16299     code = INDIRECT_REF;
16300   else if (token->type == CPP_AND)
16301     code = ADDR_EXPR;
16302   else if ((cxx_dialect != cxx98) &&
16303            token->type == CPP_AND_AND) /* C++0x only */
16304     code = NON_LVALUE_EXPR;
16305
16306   if (code != ERROR_MARK)
16307     {
16308       /* Consume the `*', `&' or `&&'.  */
16309       cp_lexer_consume_token (parser->lexer);
16310
16311       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16312          `&', if we are allowing GNU extensions.  (The only qualifier
16313          that can legally appear after `&' is `restrict', but that is
16314          enforced during semantic analysis.  */
16315       if (code == INDIRECT_REF
16316           || cp_parser_allow_gnu_extensions_p (parser))
16317         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16318     }
16319   else
16320     {
16321       /* Try the pointer-to-member case.  */
16322       cp_parser_parse_tentatively (parser);
16323       /* Look for the optional `::' operator.  */
16324       cp_parser_global_scope_opt (parser,
16325                                   /*current_scope_valid_p=*/false);
16326       /* Look for the nested-name specifier.  */
16327       token = cp_lexer_peek_token (parser->lexer);
16328       cp_parser_nested_name_specifier (parser,
16329                                        /*typename_keyword_p=*/false,
16330                                        /*check_dependency_p=*/true,
16331                                        /*type_p=*/false,
16332                                        /*is_declaration=*/false);
16333       /* If we found it, and the next token is a `*', then we are
16334          indeed looking at a pointer-to-member operator.  */
16335       if (!cp_parser_error_occurred (parser)
16336           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16337         {
16338           /* Indicate that the `*' operator was used.  */
16339           code = INDIRECT_REF;
16340
16341           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16342             error_at (token->location, "%qD is a namespace", parser->scope);
16343           else
16344             {
16345               /* The type of which the member is a member is given by the
16346                  current SCOPE.  */
16347               *type = parser->scope;
16348               /* The next name will not be qualified.  */
16349               parser->scope = NULL_TREE;
16350               parser->qualifying_scope = NULL_TREE;
16351               parser->object_scope = NULL_TREE;
16352               /* Look for the optional cv-qualifier-seq.  */
16353               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16354             }
16355         }
16356       /* If that didn't work we don't have a ptr-operator.  */
16357       if (!cp_parser_parse_definitely (parser))
16358         cp_parser_error (parser, "expected ptr-operator");
16359     }
16360
16361   return code;
16362 }
16363
16364 /* Parse an (optional) cv-qualifier-seq.
16365
16366    cv-qualifier-seq:
16367      cv-qualifier cv-qualifier-seq [opt]
16368
16369    cv-qualifier:
16370      const
16371      volatile
16372
16373    GNU Extension:
16374
16375    cv-qualifier:
16376      __restrict__
16377
16378    Returns a bitmask representing the cv-qualifiers.  */
16379
16380 static cp_cv_quals
16381 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16382 {
16383   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16384
16385   while (true)
16386     {
16387       cp_token *token;
16388       cp_cv_quals cv_qualifier;
16389
16390       /* Peek at the next token.  */
16391       token = cp_lexer_peek_token (parser->lexer);
16392       /* See if it's a cv-qualifier.  */
16393       switch (token->keyword)
16394         {
16395         case RID_CONST:
16396           cv_qualifier = TYPE_QUAL_CONST;
16397           break;
16398
16399         case RID_VOLATILE:
16400           cv_qualifier = TYPE_QUAL_VOLATILE;
16401           break;
16402
16403         case RID_RESTRICT:
16404           cv_qualifier = TYPE_QUAL_RESTRICT;
16405           break;
16406
16407         default:
16408           cv_qualifier = TYPE_UNQUALIFIED;
16409           break;
16410         }
16411
16412       if (!cv_qualifier)
16413         break;
16414
16415       if (cv_quals & cv_qualifier)
16416         {
16417           error_at (token->location, "duplicate cv-qualifier");
16418           cp_lexer_purge_token (parser->lexer);
16419         }
16420       else
16421         {
16422           cp_lexer_consume_token (parser->lexer);
16423           cv_quals |= cv_qualifier;
16424         }
16425     }
16426
16427   return cv_quals;
16428 }
16429
16430 /* Parse an (optional) virt-specifier-seq.
16431
16432    virt-specifier-seq:
16433      virt-specifier virt-specifier-seq [opt]
16434
16435    virt-specifier:
16436      override
16437      final
16438
16439    Returns a bitmask representing the virt-specifiers.  */
16440
16441 static cp_virt_specifiers
16442 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16443 {
16444   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16445
16446   while (true)
16447     {
16448       cp_token *token;
16449       cp_virt_specifiers virt_specifier;
16450
16451       /* Peek at the next token.  */
16452       token = cp_lexer_peek_token (parser->lexer);
16453       /* See if it's a virt-specifier-qualifier.  */
16454       if (token->type != CPP_NAME)
16455         break;
16456       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16457         {
16458           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16459           virt_specifier = VIRT_SPEC_OVERRIDE;
16460         }
16461       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16462         {
16463           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16464           virt_specifier = VIRT_SPEC_FINAL;
16465         }
16466       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16467         {
16468           virt_specifier = VIRT_SPEC_FINAL;
16469         }
16470       else
16471         break;
16472
16473       if (virt_specifiers & virt_specifier)
16474         {
16475           error_at (token->location, "duplicate virt-specifier");
16476           cp_lexer_purge_token (parser->lexer);
16477         }
16478       else
16479         {
16480           cp_lexer_consume_token (parser->lexer);
16481           virt_specifiers |= virt_specifier;
16482         }
16483     }
16484   return virt_specifiers;
16485 }
16486
16487 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16488    is in scope even though it isn't real.  */
16489
16490 static void
16491 inject_this_parameter (tree ctype, cp_cv_quals quals)
16492 {
16493   tree this_parm;
16494
16495   if (current_class_ptr)
16496     {
16497       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16498       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16499       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16500           && cp_type_quals (type) == quals)
16501         return;
16502     }
16503
16504   this_parm = build_this_parm (ctype, quals);
16505   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16506   current_class_ptr = NULL_TREE;
16507   current_class_ref
16508     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16509   current_class_ptr = this_parm;
16510 }
16511
16512 /* Parse a late-specified return type, if any.  This is not a separate
16513    non-terminal, but part of a function declarator, which looks like
16514
16515    -> trailing-type-specifier-seq abstract-declarator(opt)
16516
16517    Returns the type indicated by the type-id.
16518
16519    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16520    function.  */
16521
16522 static tree
16523 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16524 {
16525   cp_token *token;
16526   tree type;
16527
16528   /* Peek at the next token.  */
16529   token = cp_lexer_peek_token (parser->lexer);
16530   /* A late-specified return type is indicated by an initial '->'. */
16531   if (token->type != CPP_DEREF)
16532     return NULL_TREE;
16533
16534   /* Consume the ->.  */
16535   cp_lexer_consume_token (parser->lexer);
16536
16537   if (quals >= 0)
16538     {
16539       /* DR 1207: 'this' is in scope in the trailing return type.  */
16540       gcc_assert (current_class_ptr == NULL_TREE);
16541       inject_this_parameter (current_class_type, quals);
16542     }
16543
16544   type = cp_parser_trailing_type_id (parser);
16545
16546   if (quals >= 0)
16547     current_class_ptr = current_class_ref = NULL_TREE;
16548
16549   return type;
16550 }
16551
16552 /* Parse a declarator-id.
16553
16554    declarator-id:
16555      id-expression
16556      :: [opt] nested-name-specifier [opt] type-name
16557
16558    In the `id-expression' case, the value returned is as for
16559    cp_parser_id_expression if the id-expression was an unqualified-id.
16560    If the id-expression was a qualified-id, then a SCOPE_REF is
16561    returned.  The first operand is the scope (either a NAMESPACE_DECL
16562    or TREE_TYPE), but the second is still just a representation of an
16563    unqualified-id.  */
16564
16565 static tree
16566 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16567 {
16568   tree id;
16569   /* The expression must be an id-expression.  Assume that qualified
16570      names are the names of types so that:
16571
16572        template <class T>
16573        int S<T>::R::i = 3;
16574
16575      will work; we must treat `S<T>::R' as the name of a type.
16576      Similarly, assume that qualified names are templates, where
16577      required, so that:
16578
16579        template <class T>
16580        int S<T>::R<T>::i = 3;
16581
16582      will work, too.  */
16583   id = cp_parser_id_expression (parser,
16584                                 /*template_keyword_p=*/false,
16585                                 /*check_dependency_p=*/false,
16586                                 /*template_p=*/NULL,
16587                                 /*declarator_p=*/true,
16588                                 optional_p);
16589   if (id && BASELINK_P (id))
16590     id = BASELINK_FUNCTIONS (id);
16591   return id;
16592 }
16593
16594 /* Parse a type-id.
16595
16596    type-id:
16597      type-specifier-seq abstract-declarator [opt]
16598
16599    Returns the TYPE specified.  */
16600
16601 static tree
16602 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16603                      bool is_trailing_return)
16604 {
16605   cp_decl_specifier_seq type_specifier_seq;
16606   cp_declarator *abstract_declarator;
16607
16608   /* Parse the type-specifier-seq.  */
16609   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16610                                 is_trailing_return,
16611                                 &type_specifier_seq);
16612   if (type_specifier_seq.type == error_mark_node)
16613     return error_mark_node;
16614
16615   /* There might or might not be an abstract declarator.  */
16616   cp_parser_parse_tentatively (parser);
16617   /* Look for the declarator.  */
16618   abstract_declarator
16619     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16620                             /*parenthesized_p=*/NULL,
16621                             /*member_p=*/false);
16622   /* Check to see if there really was a declarator.  */
16623   if (!cp_parser_parse_definitely (parser))
16624     abstract_declarator = NULL;
16625
16626   if (type_specifier_seq.type
16627       && type_uses_auto (type_specifier_seq.type))
16628     {
16629       /* A type-id with type 'auto' is only ok if the abstract declarator
16630          is a function declarator with a late-specified return type.  */
16631       if (abstract_declarator
16632           && abstract_declarator->kind == cdk_function
16633           && abstract_declarator->u.function.late_return_type)
16634         /* OK */;
16635       else
16636         {
16637           error ("invalid use of %<auto%>");
16638           return error_mark_node;
16639         }
16640     }
16641   
16642   return groktypename (&type_specifier_seq, abstract_declarator,
16643                        is_template_arg);
16644 }
16645
16646 static tree cp_parser_type_id (cp_parser *parser)
16647 {
16648   return cp_parser_type_id_1 (parser, false, false);
16649 }
16650
16651 static tree cp_parser_template_type_arg (cp_parser *parser)
16652 {
16653   tree r;
16654   const char *saved_message = parser->type_definition_forbidden_message;
16655   parser->type_definition_forbidden_message
16656     = G_("types may not be defined in template arguments");
16657   r = cp_parser_type_id_1 (parser, true, false);
16658   parser->type_definition_forbidden_message = saved_message;
16659   return r;
16660 }
16661
16662 static tree cp_parser_trailing_type_id (cp_parser *parser)
16663 {
16664   return cp_parser_type_id_1 (parser, false, true);
16665 }
16666
16667 /* Parse a type-specifier-seq.
16668
16669    type-specifier-seq:
16670      type-specifier type-specifier-seq [opt]
16671
16672    GNU extension:
16673
16674    type-specifier-seq:
16675      attributes type-specifier-seq [opt]
16676
16677    If IS_DECLARATION is true, we are at the start of a "condition" or
16678    exception-declaration, so we might be followed by a declarator-id.
16679
16680    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16681    i.e. we've just seen "->".
16682
16683    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16684
16685 static void
16686 cp_parser_type_specifier_seq (cp_parser* parser,
16687                               bool is_declaration,
16688                               bool is_trailing_return,
16689                               cp_decl_specifier_seq *type_specifier_seq)
16690 {
16691   bool seen_type_specifier = false;
16692   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16693   cp_token *start_token = NULL;
16694
16695   /* Clear the TYPE_SPECIFIER_SEQ.  */
16696   clear_decl_specs (type_specifier_seq);
16697
16698   /* In the context of a trailing return type, enum E { } is an
16699      elaborated-type-specifier followed by a function-body, not an
16700      enum-specifier.  */
16701   if (is_trailing_return)
16702     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16703
16704   /* Parse the type-specifiers and attributes.  */
16705   while (true)
16706     {
16707       tree type_specifier;
16708       bool is_cv_qualifier;
16709
16710       /* Check for attributes first.  */
16711       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16712         {
16713           type_specifier_seq->attributes =
16714             chainon (type_specifier_seq->attributes,
16715                      cp_parser_attributes_opt (parser));
16716           continue;
16717         }
16718
16719       /* record the token of the beginning of the type specifier seq,
16720          for error reporting purposes*/
16721      if (!start_token)
16722        start_token = cp_lexer_peek_token (parser->lexer);
16723
16724       /* Look for the type-specifier.  */
16725       type_specifier = cp_parser_type_specifier (parser,
16726                                                  flags,
16727                                                  type_specifier_seq,
16728                                                  /*is_declaration=*/false,
16729                                                  NULL,
16730                                                  &is_cv_qualifier);
16731       if (!type_specifier)
16732         {
16733           /* If the first type-specifier could not be found, this is not a
16734              type-specifier-seq at all.  */
16735           if (!seen_type_specifier)
16736             {
16737               cp_parser_error (parser, "expected type-specifier");
16738               type_specifier_seq->type = error_mark_node;
16739               return;
16740             }
16741           /* If subsequent type-specifiers could not be found, the
16742              type-specifier-seq is complete.  */
16743           break;
16744         }
16745
16746       seen_type_specifier = true;
16747       /* The standard says that a condition can be:
16748
16749             type-specifier-seq declarator = assignment-expression
16750
16751          However, given:
16752
16753            struct S {};
16754            if (int S = ...)
16755
16756          we should treat the "S" as a declarator, not as a
16757          type-specifier.  The standard doesn't say that explicitly for
16758          type-specifier-seq, but it does say that for
16759          decl-specifier-seq in an ordinary declaration.  Perhaps it
16760          would be clearer just to allow a decl-specifier-seq here, and
16761          then add a semantic restriction that if any decl-specifiers
16762          that are not type-specifiers appear, the program is invalid.  */
16763       if (is_declaration && !is_cv_qualifier)
16764         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16765     }
16766
16767   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16768 }
16769
16770 /* Parse a parameter-declaration-clause.
16771
16772    parameter-declaration-clause:
16773      parameter-declaration-list [opt] ... [opt]
16774      parameter-declaration-list , ...
16775
16776    Returns a representation for the parameter declarations.  A return
16777    value of NULL indicates a parameter-declaration-clause consisting
16778    only of an ellipsis.  */
16779
16780 static tree
16781 cp_parser_parameter_declaration_clause (cp_parser* parser)
16782 {
16783   tree parameters;
16784   cp_token *token;
16785   bool ellipsis_p;
16786   bool is_error;
16787
16788   /* Peek at the next token.  */
16789   token = cp_lexer_peek_token (parser->lexer);
16790   /* Check for trivial parameter-declaration-clauses.  */
16791   if (token->type == CPP_ELLIPSIS)
16792     {
16793       /* Consume the `...' token.  */
16794       cp_lexer_consume_token (parser->lexer);
16795       return NULL_TREE;
16796     }
16797   else if (token->type == CPP_CLOSE_PAREN)
16798     /* There are no parameters.  */
16799     {
16800 #ifndef NO_IMPLICIT_EXTERN_C
16801       if (in_system_header && current_class_type == NULL
16802           && current_lang_name == lang_name_c)
16803         return NULL_TREE;
16804       else
16805 #endif
16806         return void_list_node;
16807     }
16808   /* Check for `(void)', too, which is a special case.  */
16809   else if (token->keyword == RID_VOID
16810            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16811                == CPP_CLOSE_PAREN))
16812     {
16813       /* Consume the `void' token.  */
16814       cp_lexer_consume_token (parser->lexer);
16815       /* There are no parameters.  */
16816       return void_list_node;
16817     }
16818
16819   /* Parse the parameter-declaration-list.  */
16820   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16821   /* If a parse error occurred while parsing the
16822      parameter-declaration-list, then the entire
16823      parameter-declaration-clause is erroneous.  */
16824   if (is_error)
16825     return NULL;
16826
16827   /* Peek at the next token.  */
16828   token = cp_lexer_peek_token (parser->lexer);
16829   /* If it's a `,', the clause should terminate with an ellipsis.  */
16830   if (token->type == CPP_COMMA)
16831     {
16832       /* Consume the `,'.  */
16833       cp_lexer_consume_token (parser->lexer);
16834       /* Expect an ellipsis.  */
16835       ellipsis_p
16836         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16837     }
16838   /* It might also be `...' if the optional trailing `,' was
16839      omitted.  */
16840   else if (token->type == CPP_ELLIPSIS)
16841     {
16842       /* Consume the `...' token.  */
16843       cp_lexer_consume_token (parser->lexer);
16844       /* And remember that we saw it.  */
16845       ellipsis_p = true;
16846     }
16847   else
16848     ellipsis_p = false;
16849
16850   /* Finish the parameter list.  */
16851   if (!ellipsis_p)
16852     parameters = chainon (parameters, void_list_node);
16853
16854   return parameters;
16855 }
16856
16857 /* Parse a parameter-declaration-list.
16858
16859    parameter-declaration-list:
16860      parameter-declaration
16861      parameter-declaration-list , parameter-declaration
16862
16863    Returns a representation of the parameter-declaration-list, as for
16864    cp_parser_parameter_declaration_clause.  However, the
16865    `void_list_node' is never appended to the list.  Upon return,
16866    *IS_ERROR will be true iff an error occurred.  */
16867
16868 static tree
16869 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16870 {
16871   tree parameters = NULL_TREE;
16872   tree *tail = &parameters; 
16873   bool saved_in_unbraced_linkage_specification_p;
16874   int index = 0;
16875
16876   /* Assume all will go well.  */
16877   *is_error = false;
16878   /* The special considerations that apply to a function within an
16879      unbraced linkage specifications do not apply to the parameters
16880      to the function.  */
16881   saved_in_unbraced_linkage_specification_p 
16882     = parser->in_unbraced_linkage_specification_p;
16883   parser->in_unbraced_linkage_specification_p = false;
16884
16885   /* Look for more parameters.  */
16886   while (true)
16887     {
16888       cp_parameter_declarator *parameter;
16889       tree decl = error_mark_node;
16890       bool parenthesized_p = false;
16891       /* Parse the parameter.  */
16892       parameter
16893         = cp_parser_parameter_declaration (parser,
16894                                            /*template_parm_p=*/false,
16895                                            &parenthesized_p);
16896
16897       /* We don't know yet if the enclosing context is deprecated, so wait
16898          and warn in grokparms if appropriate.  */
16899       deprecated_state = DEPRECATED_SUPPRESS;
16900
16901       if (parameter)
16902         decl = grokdeclarator (parameter->declarator,
16903                                &parameter->decl_specifiers,
16904                                PARM,
16905                                parameter->default_argument != NULL_TREE,
16906                                &parameter->decl_specifiers.attributes);
16907
16908       deprecated_state = DEPRECATED_NORMAL;
16909
16910       /* If a parse error occurred parsing the parameter declaration,
16911          then the entire parameter-declaration-list is erroneous.  */
16912       if (decl == error_mark_node)
16913         {
16914           *is_error = true;
16915           parameters = error_mark_node;
16916           break;
16917         }
16918
16919       if (parameter->decl_specifiers.attributes)
16920         cplus_decl_attributes (&decl,
16921                                parameter->decl_specifiers.attributes,
16922                                0);
16923       if (DECL_NAME (decl))
16924         decl = pushdecl (decl);
16925
16926       if (decl != error_mark_node)
16927         {
16928           retrofit_lang_decl (decl);
16929           DECL_PARM_INDEX (decl) = ++index;
16930           DECL_PARM_LEVEL (decl) = function_parm_depth ();
16931         }
16932
16933       /* Add the new parameter to the list.  */
16934       *tail = build_tree_list (parameter->default_argument, decl);
16935       tail = &TREE_CHAIN (*tail);
16936
16937       /* Peek at the next token.  */
16938       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16939           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16940           /* These are for Objective-C++ */
16941           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16942           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16943         /* The parameter-declaration-list is complete.  */
16944         break;
16945       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16946         {
16947           cp_token *token;
16948
16949           /* Peek at the next token.  */
16950           token = cp_lexer_peek_nth_token (parser->lexer, 2);
16951           /* If it's an ellipsis, then the list is complete.  */
16952           if (token->type == CPP_ELLIPSIS)
16953             break;
16954           /* Otherwise, there must be more parameters.  Consume the
16955              `,'.  */
16956           cp_lexer_consume_token (parser->lexer);
16957           /* When parsing something like:
16958
16959                 int i(float f, double d)
16960
16961              we can tell after seeing the declaration for "f" that we
16962              are not looking at an initialization of a variable "i",
16963              but rather at the declaration of a function "i".
16964
16965              Due to the fact that the parsing of template arguments
16966              (as specified to a template-id) requires backtracking we
16967              cannot use this technique when inside a template argument
16968              list.  */
16969           if (!parser->in_template_argument_list_p
16970               && !parser->in_type_id_in_expr_p
16971               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16972               /* However, a parameter-declaration of the form
16973                  "foat(f)" (which is a valid declaration of a
16974                  parameter "f") can also be interpreted as an
16975                  expression (the conversion of "f" to "float").  */
16976               && !parenthesized_p)
16977             cp_parser_commit_to_tentative_parse (parser);
16978         }
16979       else
16980         {
16981           cp_parser_error (parser, "expected %<,%> or %<...%>");
16982           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16983             cp_parser_skip_to_closing_parenthesis (parser,
16984                                                    /*recovering=*/true,
16985                                                    /*or_comma=*/false,
16986                                                    /*consume_paren=*/false);
16987           break;
16988         }
16989     }
16990
16991   parser->in_unbraced_linkage_specification_p
16992     = saved_in_unbraced_linkage_specification_p;
16993
16994   return parameters;
16995 }
16996
16997 /* Parse a parameter declaration.
16998
16999    parameter-declaration:
17000      decl-specifier-seq ... [opt] declarator
17001      decl-specifier-seq declarator = assignment-expression
17002      decl-specifier-seq ... [opt] abstract-declarator [opt]
17003      decl-specifier-seq abstract-declarator [opt] = assignment-expression
17004
17005    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
17006    declares a template parameter.  (In that case, a non-nested `>'
17007    token encountered during the parsing of the assignment-expression
17008    is not interpreted as a greater-than operator.)
17009
17010    Returns a representation of the parameter, or NULL if an error
17011    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
17012    true iff the declarator is of the form "(p)".  */
17013
17014 static cp_parameter_declarator *
17015 cp_parser_parameter_declaration (cp_parser *parser,
17016                                  bool template_parm_p,
17017                                  bool *parenthesized_p)
17018 {
17019   int declares_class_or_enum;
17020   cp_decl_specifier_seq decl_specifiers;
17021   cp_declarator *declarator;
17022   tree default_argument;
17023   cp_token *token = NULL, *declarator_token_start = NULL;
17024   const char *saved_message;
17025
17026   /* In a template parameter, `>' is not an operator.
17027
17028      [temp.param]
17029
17030      When parsing a default template-argument for a non-type
17031      template-parameter, the first non-nested `>' is taken as the end
17032      of the template parameter-list rather than a greater-than
17033      operator.  */
17034
17035   /* Type definitions may not appear in parameter types.  */
17036   saved_message = parser->type_definition_forbidden_message;
17037   parser->type_definition_forbidden_message
17038     = G_("types may not be defined in parameter types");
17039
17040   /* Parse the declaration-specifiers.  */
17041   cp_parser_decl_specifier_seq (parser,
17042                                 CP_PARSER_FLAGS_NONE,
17043                                 &decl_specifiers,
17044                                 &declares_class_or_enum);
17045
17046   /* Complain about missing 'typename' or other invalid type names.  */
17047   if (!decl_specifiers.any_type_specifiers_p)
17048     cp_parser_parse_and_diagnose_invalid_type_name (parser);
17049
17050   /* If an error occurred, there's no reason to attempt to parse the
17051      rest of the declaration.  */
17052   if (cp_parser_error_occurred (parser))
17053     {
17054       parser->type_definition_forbidden_message = saved_message;
17055       return NULL;
17056     }
17057
17058   /* Peek at the next token.  */
17059   token = cp_lexer_peek_token (parser->lexer);
17060
17061   /* If the next token is a `)', `,', `=', `>', or `...', then there
17062      is no declarator. However, when variadic templates are enabled,
17063      there may be a declarator following `...'.  */
17064   if (token->type == CPP_CLOSE_PAREN
17065       || token->type == CPP_COMMA
17066       || token->type == CPP_EQ
17067       || token->type == CPP_GREATER)
17068     {
17069       declarator = NULL;
17070       if (parenthesized_p)
17071         *parenthesized_p = false;
17072     }
17073   /* Otherwise, there should be a declarator.  */
17074   else
17075     {
17076       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
17077       parser->default_arg_ok_p = false;
17078
17079       /* After seeing a decl-specifier-seq, if the next token is not a
17080          "(", there is no possibility that the code is a valid
17081          expression.  Therefore, if parsing tentatively, we commit at
17082          this point.  */
17083       if (!parser->in_template_argument_list_p
17084           /* In an expression context, having seen:
17085
17086                (int((char ...
17087
17088              we cannot be sure whether we are looking at a
17089              function-type (taking a "char" as a parameter) or a cast
17090              of some object of type "char" to "int".  */
17091           && !parser->in_type_id_in_expr_p
17092           && cp_parser_uncommitted_to_tentative_parse_p (parser)
17093           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
17094           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
17095         cp_parser_commit_to_tentative_parse (parser);
17096       /* Parse the declarator.  */
17097       declarator_token_start = token;
17098       declarator = cp_parser_declarator (parser,
17099                                          CP_PARSER_DECLARATOR_EITHER,
17100                                          /*ctor_dtor_or_conv_p=*/NULL,
17101                                          parenthesized_p,
17102                                          /*member_p=*/false);
17103       parser->default_arg_ok_p = saved_default_arg_ok_p;
17104       /* After the declarator, allow more attributes.  */
17105       decl_specifiers.attributes
17106         = chainon (decl_specifiers.attributes,
17107                    cp_parser_attributes_opt (parser));
17108     }
17109
17110   /* If the next token is an ellipsis, and we have not seen a
17111      declarator name, and the type of the declarator contains parameter
17112      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
17113      a parameter pack expansion expression. Otherwise, leave the
17114      ellipsis for a C-style variadic function. */
17115   token = cp_lexer_peek_token (parser->lexer);
17116   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17117     {
17118       tree type = decl_specifiers.type;
17119
17120       if (type && DECL_P (type))
17121         type = TREE_TYPE (type);
17122
17123       if (type
17124           && TREE_CODE (type) != TYPE_PACK_EXPANSION
17125           && declarator_can_be_parameter_pack (declarator)
17126           && (!declarator || !declarator->parameter_pack_p)
17127           && uses_parameter_packs (type))
17128         {
17129           /* Consume the `...'. */
17130           cp_lexer_consume_token (parser->lexer);
17131           maybe_warn_variadic_templates ();
17132           
17133           /* Build a pack expansion type */
17134           if (declarator)
17135             declarator->parameter_pack_p = true;
17136           else
17137             decl_specifiers.type = make_pack_expansion (type);
17138         }
17139     }
17140
17141   /* The restriction on defining new types applies only to the type
17142      of the parameter, not to the default argument.  */
17143   parser->type_definition_forbidden_message = saved_message;
17144
17145   /* If the next token is `=', then process a default argument.  */
17146   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
17147     {
17148       /* If we are defining a class, then the tokens that make up the
17149          default argument must be saved and processed later.  */
17150       if (!template_parm_p && at_class_scope_p ()
17151           && TYPE_BEING_DEFINED (current_class_type)
17152           && !LAMBDA_TYPE_P (current_class_type))
17153         {
17154           unsigned depth = 0;
17155           int maybe_template_id = 0;
17156           cp_token *first_token;
17157           cp_token *token;
17158
17159           /* Add tokens until we have processed the entire default
17160              argument.  We add the range [first_token, token).  */
17161           first_token = cp_lexer_peek_token (parser->lexer);
17162           while (true)
17163             {
17164               bool done = false;
17165
17166               /* Peek at the next token.  */
17167               token = cp_lexer_peek_token (parser->lexer);
17168               /* What we do depends on what token we have.  */
17169               switch (token->type)
17170                 {
17171                   /* In valid code, a default argument must be
17172                      immediately followed by a `,' `)', or `...'.  */
17173                 case CPP_COMMA:
17174                   if (depth == 0 && maybe_template_id)
17175                     {
17176                       /* If we've seen a '<', we might be in a
17177                          template-argument-list.  Until Core issue 325 is
17178                          resolved, we don't know how this situation ought
17179                          to be handled, so try to DTRT.  We check whether
17180                          what comes after the comma is a valid parameter
17181                          declaration list.  If it is, then the comma ends
17182                          the default argument; otherwise the default
17183                          argument continues.  */
17184                       bool error = false;
17185                       tree t;
17186
17187                       /* Set ITALP so cp_parser_parameter_declaration_list
17188                          doesn't decide to commit to this parse.  */
17189                       bool saved_italp = parser->in_template_argument_list_p;
17190                       parser->in_template_argument_list_p = true;
17191
17192                       cp_parser_parse_tentatively (parser);
17193                       cp_lexer_consume_token (parser->lexer);
17194                       begin_scope (sk_function_parms, NULL_TREE);
17195                       cp_parser_parameter_declaration_list (parser, &error);
17196                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17197                         pop_binding (DECL_NAME (t), t);
17198                       leave_scope ();
17199                       if (!cp_parser_error_occurred (parser) && !error)
17200                         done = true;
17201                       cp_parser_abort_tentative_parse (parser);
17202
17203                       parser->in_template_argument_list_p = saved_italp;
17204                       break;
17205                     }
17206                 case CPP_CLOSE_PAREN:
17207                 case CPP_ELLIPSIS:
17208                   /* If we run into a non-nested `;', `}', or `]',
17209                      then the code is invalid -- but the default
17210                      argument is certainly over.  */
17211                 case CPP_SEMICOLON:
17212                 case CPP_CLOSE_BRACE:
17213                 case CPP_CLOSE_SQUARE:
17214                   if (depth == 0)
17215                     done = true;
17216                   /* Update DEPTH, if necessary.  */
17217                   else if (token->type == CPP_CLOSE_PAREN
17218                            || token->type == CPP_CLOSE_BRACE
17219                            || token->type == CPP_CLOSE_SQUARE)
17220                     --depth;
17221                   break;
17222
17223                 case CPP_OPEN_PAREN:
17224                 case CPP_OPEN_SQUARE:
17225                 case CPP_OPEN_BRACE:
17226                   ++depth;
17227                   break;
17228
17229                 case CPP_LESS:
17230                   if (depth == 0)
17231                     /* This might be the comparison operator, or it might
17232                        start a template argument list.  */
17233                     ++maybe_template_id;
17234                   break;
17235
17236                 case CPP_RSHIFT:
17237                   if (cxx_dialect == cxx98)
17238                     break;
17239                   /* Fall through for C++0x, which treats the `>>'
17240                      operator like two `>' tokens in certain
17241                      cases.  */
17242
17243                 case CPP_GREATER:
17244                   if (depth == 0)
17245                     {
17246                       /* This might be an operator, or it might close a
17247                          template argument list.  But if a previous '<'
17248                          started a template argument list, this will have
17249                          closed it, so we can't be in one anymore.  */
17250                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17251                       if (maybe_template_id < 0)
17252                         maybe_template_id = 0;
17253                     }
17254                   break;
17255
17256                   /* If we run out of tokens, issue an error message.  */
17257                 case CPP_EOF:
17258                 case CPP_PRAGMA_EOL:
17259                   error_at (token->location, "file ends in default argument");
17260                   done = true;
17261                   break;
17262
17263                 case CPP_NAME:
17264                 case CPP_SCOPE:
17265                   /* In these cases, we should look for template-ids.
17266                      For example, if the default argument is
17267                      `X<int, double>()', we need to do name lookup to
17268                      figure out whether or not `X' is a template; if
17269                      so, the `,' does not end the default argument.
17270
17271                      That is not yet done.  */
17272                   break;
17273
17274                 default:
17275                   break;
17276                 }
17277
17278               /* If we've reached the end, stop.  */
17279               if (done)
17280                 break;
17281
17282               /* Add the token to the token block.  */
17283               token = cp_lexer_consume_token (parser->lexer);
17284             }
17285
17286           /* Create a DEFAULT_ARG to represent the unparsed default
17287              argument.  */
17288           default_argument = make_node (DEFAULT_ARG);
17289           DEFARG_TOKENS (default_argument)
17290             = cp_token_cache_new (first_token, token);
17291           DEFARG_INSTANTIATIONS (default_argument) = NULL;
17292         }
17293       /* Outside of a class definition, we can just parse the
17294          assignment-expression.  */
17295       else
17296         {
17297           token = cp_lexer_peek_token (parser->lexer);
17298           default_argument 
17299             = cp_parser_default_argument (parser, template_parm_p);
17300         }
17301
17302       if (!parser->default_arg_ok_p)
17303         {
17304           if (flag_permissive)
17305             warning (0, "deprecated use of default argument for parameter of non-function");
17306           else
17307             {
17308               error_at (token->location,
17309                         "default arguments are only "
17310                         "permitted for function parameters");
17311               default_argument = NULL_TREE;
17312             }
17313         }
17314       else if ((declarator && declarator->parameter_pack_p)
17315                || (decl_specifiers.type
17316                    && PACK_EXPANSION_P (decl_specifiers.type)))
17317         {
17318           /* Find the name of the parameter pack.  */     
17319           cp_declarator *id_declarator = declarator;
17320           while (id_declarator && id_declarator->kind != cdk_id)
17321             id_declarator = id_declarator->declarator;
17322           
17323           if (id_declarator && id_declarator->kind == cdk_id)
17324             error_at (declarator_token_start->location,
17325                       template_parm_p
17326                       ? G_("template parameter pack %qD "
17327                            "cannot have a default argument")
17328                       : G_("parameter pack %qD cannot have "
17329                            "a default argument"),
17330                       id_declarator->u.id.unqualified_name);
17331           else
17332             error_at (declarator_token_start->location,
17333                       template_parm_p
17334                       ? G_("template parameter pack cannot have "
17335                            "a default argument")
17336                       : G_("parameter pack cannot have a "
17337                            "default argument"));
17338
17339           default_argument = NULL_TREE;
17340         }
17341     }
17342   else
17343     default_argument = NULL_TREE;
17344
17345   return make_parameter_declarator (&decl_specifiers,
17346                                     declarator,
17347                                     default_argument);
17348 }
17349
17350 /* Parse a default argument and return it.
17351
17352    TEMPLATE_PARM_P is true if this is a default argument for a
17353    non-type template parameter.  */
17354 static tree
17355 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17356 {
17357   tree default_argument = NULL_TREE;
17358   bool saved_greater_than_is_operator_p;
17359   bool saved_local_variables_forbidden_p;
17360   bool non_constant_p, is_direct_init;
17361
17362   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17363      set correctly.  */
17364   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17365   parser->greater_than_is_operator_p = !template_parm_p;
17366   /* Local variable names (and the `this' keyword) may not
17367      appear in a default argument.  */
17368   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17369   parser->local_variables_forbidden_p = true;
17370   /* Parse the assignment-expression.  */
17371   if (template_parm_p)
17372     push_deferring_access_checks (dk_no_deferred);
17373   default_argument
17374     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17375   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17376     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17377   if (template_parm_p)
17378     pop_deferring_access_checks ();
17379   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17380   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17381
17382   return default_argument;
17383 }
17384
17385 /* Parse a function-body.
17386
17387    function-body:
17388      compound_statement  */
17389
17390 static void
17391 cp_parser_function_body (cp_parser *parser)
17392 {
17393   cp_parser_compound_statement (parser, NULL, false, true);
17394 }
17395
17396 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17397    true if a ctor-initializer was present.  */
17398
17399 static bool
17400 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17401 {
17402   tree body, list;
17403   bool ctor_initializer_p;
17404   const bool check_body_p =
17405      DECL_CONSTRUCTOR_P (current_function_decl)
17406      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17407   tree last = NULL;
17408
17409   /* Begin the function body.  */
17410   body = begin_function_body ();
17411   /* Parse the optional ctor-initializer.  */
17412   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17413
17414   /* If we're parsing a constexpr constructor definition, we need
17415      to check that the constructor body is indeed empty.  However,
17416      before we get to cp_parser_function_body lot of junk has been
17417      generated, so we can't just check that we have an empty block.
17418      Rather we take a snapshot of the outermost block, and check whether
17419      cp_parser_function_body changed its state.  */
17420   if (check_body_p)
17421     {
17422       list = body;
17423       if (TREE_CODE (list) == BIND_EXPR)
17424         list = BIND_EXPR_BODY (list);
17425       if (TREE_CODE (list) == STATEMENT_LIST
17426           && STATEMENT_LIST_TAIL (list) != NULL)
17427         last = STATEMENT_LIST_TAIL (list)->stmt;
17428     }
17429   /* Parse the function-body.  */
17430   cp_parser_function_body (parser);
17431   if (check_body_p)
17432     check_constexpr_ctor_body (last, list);
17433   /* Finish the function body.  */
17434   finish_function_body (body);
17435
17436   return ctor_initializer_p;
17437 }
17438
17439 /* Parse an initializer.
17440
17441    initializer:
17442      = initializer-clause
17443      ( expression-list )
17444
17445    Returns an expression representing the initializer.  If no
17446    initializer is present, NULL_TREE is returned.
17447
17448    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17449    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17450    set to TRUE if there is no initializer present.  If there is an
17451    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17452    is set to true; otherwise it is set to false.  */
17453
17454 static tree
17455 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17456                        bool* non_constant_p)
17457 {
17458   cp_token *token;
17459   tree init;
17460
17461   /* Peek at the next token.  */
17462   token = cp_lexer_peek_token (parser->lexer);
17463
17464   /* Let our caller know whether or not this initializer was
17465      parenthesized.  */
17466   *is_direct_init = (token->type != CPP_EQ);
17467   /* Assume that the initializer is constant.  */
17468   *non_constant_p = false;
17469
17470   if (token->type == CPP_EQ)
17471     {
17472       /* Consume the `='.  */
17473       cp_lexer_consume_token (parser->lexer);
17474       /* Parse the initializer-clause.  */
17475       init = cp_parser_initializer_clause (parser, non_constant_p);
17476     }
17477   else if (token->type == CPP_OPEN_PAREN)
17478     {
17479       VEC(tree,gc) *vec;
17480       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17481                                                      /*cast_p=*/false,
17482                                                      /*allow_expansion_p=*/true,
17483                                                      non_constant_p);
17484       if (vec == NULL)
17485         return error_mark_node;
17486       init = build_tree_list_vec (vec);
17487       release_tree_vector (vec);
17488     }
17489   else if (token->type == CPP_OPEN_BRACE)
17490     {
17491       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17492       init = cp_parser_braced_list (parser, non_constant_p);
17493       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17494     }
17495   else
17496     {
17497       /* Anything else is an error.  */
17498       cp_parser_error (parser, "expected initializer");
17499       init = error_mark_node;
17500     }
17501
17502   return init;
17503 }
17504
17505 /* Parse an initializer-clause.
17506
17507    initializer-clause:
17508      assignment-expression
17509      braced-init-list
17510
17511    Returns an expression representing the initializer.
17512
17513    If the `assignment-expression' production is used the value
17514    returned is simply a representation for the expression.
17515
17516    Otherwise, calls cp_parser_braced_list.  */
17517
17518 static tree
17519 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17520 {
17521   tree initializer;
17522
17523   /* Assume the expression is constant.  */
17524   *non_constant_p = false;
17525
17526   /* If it is not a `{', then we are looking at an
17527      assignment-expression.  */
17528   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17529     {
17530       initializer
17531         = cp_parser_constant_expression (parser,
17532                                         /*allow_non_constant_p=*/true,
17533                                         non_constant_p);
17534     }
17535   else
17536     initializer = cp_parser_braced_list (parser, non_constant_p);
17537
17538   return initializer;
17539 }
17540
17541 /* Parse a brace-enclosed initializer list.
17542
17543    braced-init-list:
17544      { initializer-list , [opt] }
17545      { }
17546
17547    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17548    the elements of the initializer-list (or NULL, if the last
17549    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17550    NULL_TREE.  There is no way to detect whether or not the optional
17551    trailing `,' was provided.  NON_CONSTANT_P is as for
17552    cp_parser_initializer.  */     
17553
17554 static tree
17555 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17556 {
17557   tree initializer;
17558
17559   /* Consume the `{' token.  */
17560   cp_lexer_consume_token (parser->lexer);
17561   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17562   initializer = make_node (CONSTRUCTOR);
17563   /* If it's not a `}', then there is a non-trivial initializer.  */
17564   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17565     {
17566       /* Parse the initializer list.  */
17567       CONSTRUCTOR_ELTS (initializer)
17568         = cp_parser_initializer_list (parser, non_constant_p);
17569       /* A trailing `,' token is allowed.  */
17570       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17571         cp_lexer_consume_token (parser->lexer);
17572     }
17573   /* Now, there should be a trailing `}'.  */
17574   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17575   TREE_TYPE (initializer) = init_list_type_node;
17576   return initializer;
17577 }
17578
17579 /* Parse an initializer-list.
17580
17581    initializer-list:
17582      initializer-clause ... [opt]
17583      initializer-list , initializer-clause ... [opt]
17584
17585    GNU Extension:
17586
17587    initializer-list:
17588      designation initializer-clause ...[opt]
17589      initializer-list , designation initializer-clause ...[opt]
17590
17591    designation:
17592      . identifier =
17593      identifier :
17594      [ constant-expression ] =
17595
17596    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17597    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17598    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17599    as for cp_parser_initializer.  */
17600
17601 static VEC(constructor_elt,gc) *
17602 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17603 {
17604   VEC(constructor_elt,gc) *v = NULL;
17605
17606   /* Assume all of the expressions are constant.  */
17607   *non_constant_p = false;
17608
17609   /* Parse the rest of the list.  */
17610   while (true)
17611     {
17612       cp_token *token;
17613       tree designator;
17614       tree initializer;
17615       bool clause_non_constant_p;
17616
17617       /* If the next token is an identifier and the following one is a
17618          colon, we are looking at the GNU designated-initializer
17619          syntax.  */
17620       if (cp_parser_allow_gnu_extensions_p (parser)
17621           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17622           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17623         {
17624           /* Warn the user that they are using an extension.  */
17625           pedwarn (input_location, OPT_pedantic, 
17626                    "ISO C++ does not allow designated initializers");
17627           /* Consume the identifier.  */
17628           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17629           /* Consume the `:'.  */
17630           cp_lexer_consume_token (parser->lexer);
17631         }
17632       /* Also handle the C99 syntax, '. id ='.  */
17633       else if (cp_parser_allow_gnu_extensions_p (parser)
17634                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17635                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17636                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17637         {
17638           /* Warn the user that they are using an extension.  */
17639           pedwarn (input_location, OPT_pedantic,
17640                    "ISO C++ does not allow C99 designated initializers");
17641           /* Consume the `.'.  */
17642           cp_lexer_consume_token (parser->lexer);
17643           /* Consume the identifier.  */
17644           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17645           /* Consume the `='.  */
17646           cp_lexer_consume_token (parser->lexer);
17647         }
17648       /* Also handle C99 array designators, '[ const ] ='.  */
17649       else if (cp_parser_allow_gnu_extensions_p (parser)
17650                && !c_dialect_objc ()
17651                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17652         {
17653           /* In C++11, [ could start a lambda-introducer.  */
17654           cp_parser_parse_tentatively (parser);
17655           cp_lexer_consume_token (parser->lexer);
17656           designator = cp_parser_constant_expression (parser, false, NULL);
17657           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17658           cp_parser_require (parser, CPP_EQ, RT_EQ);
17659           cp_parser_parse_definitely (parser);
17660         }
17661       else
17662         designator = NULL_TREE;
17663
17664       /* Parse the initializer.  */
17665       initializer = cp_parser_initializer_clause (parser,
17666                                                   &clause_non_constant_p);
17667       /* If any clause is non-constant, so is the entire initializer.  */
17668       if (clause_non_constant_p)
17669         *non_constant_p = true;
17670
17671       /* If we have an ellipsis, this is an initializer pack
17672          expansion.  */
17673       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17674         {
17675           /* Consume the `...'.  */
17676           cp_lexer_consume_token (parser->lexer);
17677
17678           /* Turn the initializer into an initializer expansion.  */
17679           initializer = make_pack_expansion (initializer);
17680         }
17681
17682       /* Add it to the vector.  */
17683       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17684
17685       /* If the next token is not a comma, we have reached the end of
17686          the list.  */
17687       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17688         break;
17689
17690       /* Peek at the next token.  */
17691       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17692       /* If the next token is a `}', then we're still done.  An
17693          initializer-clause can have a trailing `,' after the
17694          initializer-list and before the closing `}'.  */
17695       if (token->type == CPP_CLOSE_BRACE)
17696         break;
17697
17698       /* Consume the `,' token.  */
17699       cp_lexer_consume_token (parser->lexer);
17700     }
17701
17702   return v;
17703 }
17704
17705 /* Classes [gram.class] */
17706
17707 /* Parse a class-name.
17708
17709    class-name:
17710      identifier
17711      template-id
17712
17713    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17714    to indicate that names looked up in dependent types should be
17715    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17716    keyword has been used to indicate that the name that appears next
17717    is a template.  TAG_TYPE indicates the explicit tag given before
17718    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17719    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17720    is the class being defined in a class-head.
17721
17722    Returns the TYPE_DECL representing the class.  */
17723
17724 static tree
17725 cp_parser_class_name (cp_parser *parser,
17726                       bool typename_keyword_p,
17727                       bool template_keyword_p,
17728                       enum tag_types tag_type,
17729                       bool check_dependency_p,
17730                       bool class_head_p,
17731                       bool is_declaration)
17732 {
17733   tree decl;
17734   tree scope;
17735   bool typename_p;
17736   cp_token *token;
17737   tree identifier = NULL_TREE;
17738
17739   /* All class-names start with an identifier.  */
17740   token = cp_lexer_peek_token (parser->lexer);
17741   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17742     {
17743       cp_parser_error (parser, "expected class-name");
17744       return error_mark_node;
17745     }
17746
17747   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17748      to a template-id, so we save it here.  */
17749   scope = parser->scope;
17750   if (scope == error_mark_node)
17751     return error_mark_node;
17752
17753   /* Any name names a type if we're following the `typename' keyword
17754      in a qualified name where the enclosing scope is type-dependent.  */
17755   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17756                 && dependent_type_p (scope));
17757   /* Handle the common case (an identifier, but not a template-id)
17758      efficiently.  */
17759   if (token->type == CPP_NAME
17760       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17761     {
17762       cp_token *identifier_token;
17763       bool ambiguous_p;
17764
17765       /* Look for the identifier.  */
17766       identifier_token = cp_lexer_peek_token (parser->lexer);
17767       ambiguous_p = identifier_token->ambiguous_p;
17768       identifier = cp_parser_identifier (parser);
17769       /* If the next token isn't an identifier, we are certainly not
17770          looking at a class-name.  */
17771       if (identifier == error_mark_node)
17772         decl = error_mark_node;
17773       /* If we know this is a type-name, there's no need to look it
17774          up.  */
17775       else if (typename_p)
17776         decl = identifier;
17777       else
17778         {
17779           tree ambiguous_decls;
17780           /* If we already know that this lookup is ambiguous, then
17781              we've already issued an error message; there's no reason
17782              to check again.  */
17783           if (ambiguous_p)
17784             {
17785               cp_parser_simulate_error (parser);
17786               return error_mark_node;
17787             }
17788           /* If the next token is a `::', then the name must be a type
17789              name.
17790
17791              [basic.lookup.qual]
17792
17793              During the lookup for a name preceding the :: scope
17794              resolution operator, object, function, and enumerator
17795              names are ignored.  */
17796           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17797             tag_type = typename_type;
17798           /* Look up the name.  */
17799           decl = cp_parser_lookup_name (parser, identifier,
17800                                         tag_type,
17801                                         /*is_template=*/false,
17802                                         /*is_namespace=*/false,
17803                                         check_dependency_p,
17804                                         &ambiguous_decls,
17805                                         identifier_token->location);
17806           if (ambiguous_decls)
17807             {
17808               if (cp_parser_parsing_tentatively (parser))
17809                 cp_parser_simulate_error (parser);
17810               return error_mark_node;
17811             }
17812         }
17813     }
17814   else
17815     {
17816       /* Try a template-id.  */
17817       decl = cp_parser_template_id (parser, template_keyword_p,
17818                                     check_dependency_p,
17819                                     is_declaration);
17820       if (decl == error_mark_node)
17821         return error_mark_node;
17822     }
17823
17824   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17825
17826   /* If this is a typename, create a TYPENAME_TYPE.  */
17827   if (typename_p && decl != error_mark_node)
17828     {
17829       decl = make_typename_type (scope, decl, typename_type,
17830                                  /*complain=*/tf_error);
17831       if (decl != error_mark_node)
17832         decl = TYPE_NAME (decl);
17833     }
17834
17835   /* Check to see that it is really the name of a class.  */
17836   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17837       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17838       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17839     /* Situations like this:
17840
17841          template <typename T> struct A {
17842            typename T::template X<int>::I i;
17843          };
17844
17845        are problematic.  Is `T::template X<int>' a class-name?  The
17846        standard does not seem to be definitive, but there is no other
17847        valid interpretation of the following `::'.  Therefore, those
17848        names are considered class-names.  */
17849     {
17850       decl = make_typename_type (scope, decl, tag_type, tf_error);
17851       if (decl != error_mark_node)
17852         decl = TYPE_NAME (decl);
17853     }
17854   else if (TREE_CODE (decl) != TYPE_DECL
17855            || TREE_TYPE (decl) == error_mark_node
17856            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17857            /* In Objective-C 2.0, a classname followed by '.' starts a
17858               dot-syntax expression, and it's not a type-name.  */
17859            || (c_dialect_objc ()
17860                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17861                && objc_is_class_name (decl)))
17862     decl = error_mark_node;
17863
17864   if (decl == error_mark_node)
17865     cp_parser_error (parser, "expected class-name");
17866   else if (identifier && !parser->scope)
17867     maybe_note_name_used_in_class (identifier, decl);
17868
17869   return decl;
17870 }
17871
17872 /* Parse a class-specifier.
17873
17874    class-specifier:
17875      class-head { member-specification [opt] }
17876
17877    Returns the TREE_TYPE representing the class.  */
17878
17879 static tree
17880 cp_parser_class_specifier_1 (cp_parser* parser)
17881 {
17882   tree type;
17883   tree attributes = NULL_TREE;
17884   bool nested_name_specifier_p;
17885   unsigned saved_num_template_parameter_lists;
17886   bool saved_in_function_body;
17887   unsigned char in_statement;
17888   bool in_switch_statement_p;
17889   bool saved_in_unbraced_linkage_specification_p;
17890   tree old_scope = NULL_TREE;
17891   tree scope = NULL_TREE;
17892   tree bases;
17893   cp_token *closing_brace;
17894
17895   push_deferring_access_checks (dk_no_deferred);
17896
17897   /* Parse the class-head.  */
17898   type = cp_parser_class_head (parser,
17899                                &nested_name_specifier_p,
17900                                &attributes,
17901                                &bases);
17902   /* If the class-head was a semantic disaster, skip the entire body
17903      of the class.  */
17904   if (!type)
17905     {
17906       cp_parser_skip_to_end_of_block_or_statement (parser);
17907       pop_deferring_access_checks ();
17908       return error_mark_node;
17909     }
17910
17911   /* Look for the `{'.  */
17912   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17913     {
17914       pop_deferring_access_checks ();
17915       return error_mark_node;
17916     }
17917
17918   /* Process the base classes. If they're invalid, skip the 
17919      entire class body.  */
17920   if (!xref_basetypes (type, bases))
17921     {
17922       /* Consuming the closing brace yields better error messages
17923          later on.  */
17924       if (cp_parser_skip_to_closing_brace (parser))
17925         cp_lexer_consume_token (parser->lexer);
17926       pop_deferring_access_checks ();
17927       return error_mark_node;
17928     }
17929
17930   /* Issue an error message if type-definitions are forbidden here.  */
17931   cp_parser_check_type_definition (parser);
17932   /* Remember that we are defining one more class.  */
17933   ++parser->num_classes_being_defined;
17934   /* Inside the class, surrounding template-parameter-lists do not
17935      apply.  */
17936   saved_num_template_parameter_lists
17937     = parser->num_template_parameter_lists;
17938   parser->num_template_parameter_lists = 0;
17939   /* We are not in a function body.  */
17940   saved_in_function_body = parser->in_function_body;
17941   parser->in_function_body = false;
17942   /* Or in a loop.  */
17943   in_statement = parser->in_statement;
17944   parser->in_statement = 0;
17945   /* Or in a switch.  */
17946   in_switch_statement_p = parser->in_switch_statement_p;
17947   parser->in_switch_statement_p = false;
17948   /* We are not immediately inside an extern "lang" block.  */
17949   saved_in_unbraced_linkage_specification_p
17950     = parser->in_unbraced_linkage_specification_p;
17951   parser->in_unbraced_linkage_specification_p = false;
17952
17953   /* Start the class.  */
17954   if (nested_name_specifier_p)
17955     {
17956       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17957       old_scope = push_inner_scope (scope);
17958     }
17959   type = begin_class_definition (type, attributes);
17960
17961   if (type == error_mark_node)
17962     /* If the type is erroneous, skip the entire body of the class.  */
17963     cp_parser_skip_to_closing_brace (parser);
17964   else
17965     /* Parse the member-specification.  */
17966     cp_parser_member_specification_opt (parser);
17967
17968   /* Look for the trailing `}'.  */
17969   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17970   /* Look for trailing attributes to apply to this class.  */
17971   if (cp_parser_allow_gnu_extensions_p (parser))
17972     attributes = cp_parser_attributes_opt (parser);
17973   if (type != error_mark_node)
17974     type = finish_struct (type, attributes);
17975   if (nested_name_specifier_p)
17976     pop_inner_scope (old_scope, scope);
17977
17978   /* We've finished a type definition.  Check for the common syntax
17979      error of forgetting a semicolon after the definition.  We need to
17980      be careful, as we can't just check for not-a-semicolon and be done
17981      with it; the user might have typed:
17982
17983      class X { } c = ...;
17984      class X { } *p = ...;
17985
17986      and so forth.  Instead, enumerate all the possible tokens that
17987      might follow this production; if we don't see one of them, then
17988      complain and silently insert the semicolon.  */
17989   {
17990     cp_token *token = cp_lexer_peek_token (parser->lexer);
17991     bool want_semicolon = true;
17992
17993     switch (token->type)
17994       {
17995       case CPP_NAME:
17996       case CPP_SEMICOLON:
17997       case CPP_MULT:
17998       case CPP_AND:
17999       case CPP_OPEN_PAREN:
18000       case CPP_CLOSE_PAREN:
18001       case CPP_COMMA:
18002         want_semicolon = false;
18003         break;
18004
18005         /* While it's legal for type qualifiers and storage class
18006            specifiers to follow type definitions in the grammar, only
18007            compiler testsuites contain code like that.  Assume that if
18008            we see such code, then what we're really seeing is a case
18009            like:
18010
18011            class X { }
18012            const <type> var = ...;
18013
18014            or
18015
18016            class Y { }
18017            static <type> func (...) ...
18018
18019            i.e. the qualifier or specifier applies to the next
18020            declaration.  To do so, however, we need to look ahead one
18021            more token to see if *that* token is a type specifier.
18022
18023            This code could be improved to handle:
18024
18025            class Z { }
18026            static const <type> var = ...;  */
18027       case CPP_KEYWORD:
18028         if (keyword_is_decl_specifier (token->keyword))
18029           {
18030             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
18031
18032             /* Handling user-defined types here would be nice, but very
18033                tricky.  */
18034             want_semicolon
18035               = (lookahead->type == CPP_KEYWORD
18036                  && keyword_begins_type_specifier (lookahead->keyword));
18037           }
18038         break;
18039       default:
18040         break;
18041       }
18042
18043     /* If we don't have a type, then something is very wrong and we
18044        shouldn't try to do anything clever.  Likewise for not seeing the
18045        closing brace.  */
18046     if (closing_brace && TYPE_P (type) && want_semicolon)
18047       {
18048         cp_token_position prev
18049           = cp_lexer_previous_token_position (parser->lexer);
18050         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
18051         location_t loc = prev_token->location;
18052
18053         if (CLASSTYPE_DECLARED_CLASS (type))
18054           error_at (loc, "expected %<;%> after class definition");
18055         else if (TREE_CODE (type) == RECORD_TYPE)
18056           error_at (loc, "expected %<;%> after struct definition");
18057         else if (TREE_CODE (type) == UNION_TYPE)
18058           error_at (loc, "expected %<;%> after union definition");
18059         else
18060           gcc_unreachable ();
18061
18062         /* Unget one token and smash it to look as though we encountered
18063            a semicolon in the input stream.  */
18064         cp_lexer_set_token_position (parser->lexer, prev);
18065         token = cp_lexer_peek_token (parser->lexer);
18066         token->type = CPP_SEMICOLON;
18067         token->keyword = RID_MAX;
18068       }
18069   }
18070
18071   /* If this class is not itself within the scope of another class,
18072      then we need to parse the bodies of all of the queued function
18073      definitions.  Note that the queued functions defined in a class
18074      are not always processed immediately following the
18075      class-specifier for that class.  Consider:
18076
18077        struct A {
18078          struct B { void f() { sizeof (A); } };
18079        };
18080
18081      If `f' were processed before the processing of `A' were
18082      completed, there would be no way to compute the size of `A'.
18083      Note that the nesting we are interested in here is lexical --
18084      not the semantic nesting given by TYPE_CONTEXT.  In particular,
18085      for:
18086
18087        struct A { struct B; };
18088        struct A::B { void f() { } };
18089
18090      there is no need to delay the parsing of `A::B::f'.  */
18091   if (--parser->num_classes_being_defined == 0)
18092     {
18093       tree decl;
18094       tree class_type = NULL_TREE;
18095       tree pushed_scope = NULL_TREE;
18096       unsigned ix;
18097       cp_default_arg_entry *e;
18098       tree save_ccp, save_ccr;
18099
18100       /* In a first pass, parse default arguments to the functions.
18101          Then, in a second pass, parse the bodies of the functions.
18102          This two-phased approach handles cases like:
18103
18104             struct S {
18105               void f() { g(); }
18106               void g(int i = 3);
18107             };
18108
18109          */
18110       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
18111                         ix, e)
18112         {
18113           decl = e->decl;
18114           /* If there are default arguments that have not yet been processed,
18115              take care of them now.  */
18116           if (class_type != e->class_type)
18117             {
18118               if (pushed_scope)
18119                 pop_scope (pushed_scope);
18120               class_type = e->class_type;
18121               pushed_scope = push_scope (class_type);
18122             }
18123           /* Make sure that any template parameters are in scope.  */
18124           maybe_begin_member_template_processing (decl);
18125           /* Parse the default argument expressions.  */
18126           cp_parser_late_parsing_default_args (parser, decl);
18127           /* Remove any template parameters from the symbol table.  */
18128           maybe_end_member_template_processing ();
18129         }
18130       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
18131       /* Now parse any NSDMIs.  */
18132       save_ccp = current_class_ptr;
18133       save_ccr = current_class_ref;
18134       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
18135         {
18136           if (class_type != DECL_CONTEXT (decl))
18137             {
18138               if (pushed_scope)
18139                 pop_scope (pushed_scope);
18140               class_type = DECL_CONTEXT (decl);
18141               pushed_scope = push_scope (class_type);
18142             }
18143           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
18144           cp_parser_late_parsing_nsdmi (parser, decl);
18145         }
18146       VEC_truncate (tree, unparsed_nsdmis, 0);
18147       current_class_ptr = save_ccp;
18148       current_class_ref = save_ccr;
18149       if (pushed_scope)
18150         pop_scope (pushed_scope);
18151       /* Now parse the body of the functions.  */
18152       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
18153         cp_parser_late_parsing_for_member (parser, decl);
18154       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
18155     }
18156
18157   /* Put back any saved access checks.  */
18158   pop_deferring_access_checks ();
18159
18160   /* Restore saved state.  */
18161   parser->in_switch_statement_p = in_switch_statement_p;
18162   parser->in_statement = in_statement;
18163   parser->in_function_body = saved_in_function_body;
18164   parser->num_template_parameter_lists
18165     = saved_num_template_parameter_lists;
18166   parser->in_unbraced_linkage_specification_p
18167     = saved_in_unbraced_linkage_specification_p;
18168
18169   return type;
18170 }
18171
18172 static tree
18173 cp_parser_class_specifier (cp_parser* parser)
18174 {
18175   tree ret;
18176   timevar_push (TV_PARSE_STRUCT);
18177   ret = cp_parser_class_specifier_1 (parser);
18178   timevar_pop (TV_PARSE_STRUCT);
18179   return ret;
18180 }
18181
18182 /* Parse a class-head.
18183
18184    class-head:
18185      class-key identifier [opt] base-clause [opt]
18186      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18187      class-key nested-name-specifier [opt] template-id
18188        base-clause [opt]
18189
18190    class-virt-specifier:
18191      final
18192
18193    GNU Extensions:
18194      class-key attributes identifier [opt] base-clause [opt]
18195      class-key attributes nested-name-specifier identifier base-clause [opt]
18196      class-key attributes nested-name-specifier [opt] template-id
18197        base-clause [opt]
18198
18199    Upon return BASES is initialized to the list of base classes (or
18200    NULL, if there are none) in the same form returned by
18201    cp_parser_base_clause.
18202
18203    Returns the TYPE of the indicated class.  Sets
18204    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18205    involving a nested-name-specifier was used, and FALSE otherwise.
18206
18207    Returns error_mark_node if this is not a class-head.
18208
18209    Returns NULL_TREE if the class-head is syntactically valid, but
18210    semantically invalid in a way that means we should skip the entire
18211    body of the class.  */
18212
18213 static tree
18214 cp_parser_class_head (cp_parser* parser,
18215                       bool* nested_name_specifier_p,
18216                       tree *attributes_p,
18217                       tree *bases)
18218 {
18219   tree nested_name_specifier;
18220   enum tag_types class_key;
18221   tree id = NULL_TREE;
18222   tree type = NULL_TREE;
18223   tree attributes;
18224   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18225   bool template_id_p = false;
18226   bool qualified_p = false;
18227   bool invalid_nested_name_p = false;
18228   bool invalid_explicit_specialization_p = false;
18229   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18230   tree pushed_scope = NULL_TREE;
18231   unsigned num_templates;
18232   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18233   /* Assume no nested-name-specifier will be present.  */
18234   *nested_name_specifier_p = false;
18235   /* Assume no template parameter lists will be used in defining the
18236      type.  */
18237   num_templates = 0;
18238   parser->colon_corrects_to_scope_p = false;
18239
18240   *bases = NULL_TREE;
18241
18242   /* Look for the class-key.  */
18243   class_key = cp_parser_class_key (parser);
18244   if (class_key == none_type)
18245     return error_mark_node;
18246
18247   /* Parse the attributes.  */
18248   attributes = cp_parser_attributes_opt (parser);
18249
18250   /* If the next token is `::', that is invalid -- but sometimes
18251      people do try to write:
18252
18253        struct ::S {};
18254
18255      Handle this gracefully by accepting the extra qualifier, and then
18256      issuing an error about it later if this really is a
18257      class-head.  If it turns out just to be an elaborated type
18258      specifier, remain silent.  */
18259   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18260     qualified_p = true;
18261
18262   push_deferring_access_checks (dk_no_check);
18263
18264   /* Determine the name of the class.  Begin by looking for an
18265      optional nested-name-specifier.  */
18266   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18267   nested_name_specifier
18268     = cp_parser_nested_name_specifier_opt (parser,
18269                                            /*typename_keyword_p=*/false,
18270                                            /*check_dependency_p=*/false,
18271                                            /*type_p=*/false,
18272                                            /*is_declaration=*/false);
18273   /* If there was a nested-name-specifier, then there *must* be an
18274      identifier.  */
18275   if (nested_name_specifier)
18276     {
18277       type_start_token = cp_lexer_peek_token (parser->lexer);
18278       /* Although the grammar says `identifier', it really means
18279          `class-name' or `template-name'.  You are only allowed to
18280          define a class that has already been declared with this
18281          syntax.
18282
18283          The proposed resolution for Core Issue 180 says that wherever
18284          you see `class T::X' you should treat `X' as a type-name.
18285
18286          It is OK to define an inaccessible class; for example:
18287
18288            class A { class B; };
18289            class A::B {};
18290
18291          We do not know if we will see a class-name, or a
18292          template-name.  We look for a class-name first, in case the
18293          class-name is a template-id; if we looked for the
18294          template-name first we would stop after the template-name.  */
18295       cp_parser_parse_tentatively (parser);
18296       type = cp_parser_class_name (parser,
18297                                    /*typename_keyword_p=*/false,
18298                                    /*template_keyword_p=*/false,
18299                                    class_type,
18300                                    /*check_dependency_p=*/false,
18301                                    /*class_head_p=*/true,
18302                                    /*is_declaration=*/false);
18303       /* If that didn't work, ignore the nested-name-specifier.  */
18304       if (!cp_parser_parse_definitely (parser))
18305         {
18306           invalid_nested_name_p = true;
18307           type_start_token = cp_lexer_peek_token (parser->lexer);
18308           id = cp_parser_identifier (parser);
18309           if (id == error_mark_node)
18310             id = NULL_TREE;
18311         }
18312       /* If we could not find a corresponding TYPE, treat this
18313          declaration like an unqualified declaration.  */
18314       if (type == error_mark_node)
18315         nested_name_specifier = NULL_TREE;
18316       /* Otherwise, count the number of templates used in TYPE and its
18317          containing scopes.  */
18318       else
18319         {
18320           tree scope;
18321
18322           for (scope = TREE_TYPE (type);
18323                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18324                scope = (TYPE_P (scope)
18325                         ? TYPE_CONTEXT (scope)
18326                         : DECL_CONTEXT (scope)))
18327             if (TYPE_P (scope)
18328                 && CLASS_TYPE_P (scope)
18329                 && CLASSTYPE_TEMPLATE_INFO (scope)
18330                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18331                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18332               ++num_templates;
18333         }
18334     }
18335   /* Otherwise, the identifier is optional.  */
18336   else
18337     {
18338       /* We don't know whether what comes next is a template-id,
18339          an identifier, or nothing at all.  */
18340       cp_parser_parse_tentatively (parser);
18341       /* Check for a template-id.  */
18342       type_start_token = cp_lexer_peek_token (parser->lexer);
18343       id = cp_parser_template_id (parser,
18344                                   /*template_keyword_p=*/false,
18345                                   /*check_dependency_p=*/true,
18346                                   /*is_declaration=*/true);
18347       /* If that didn't work, it could still be an identifier.  */
18348       if (!cp_parser_parse_definitely (parser))
18349         {
18350           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18351             {
18352               type_start_token = cp_lexer_peek_token (parser->lexer);
18353               id = cp_parser_identifier (parser);
18354             }
18355           else
18356             id = NULL_TREE;
18357         }
18358       else
18359         {
18360           template_id_p = true;
18361           ++num_templates;
18362         }
18363     }
18364
18365   pop_deferring_access_checks ();
18366
18367   if (id)
18368     {
18369       cp_parser_check_for_invalid_template_id (parser, id,
18370                                                type_start_token->location);
18371     }
18372   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18373
18374   /* If it's not a `:' or a `{' then we can't really be looking at a
18375      class-head, since a class-head only appears as part of a
18376      class-specifier.  We have to detect this situation before calling
18377      xref_tag, since that has irreversible side-effects.  */
18378   if (!cp_parser_next_token_starts_class_definition_p (parser))
18379     {
18380       cp_parser_error (parser, "expected %<{%> or %<:%>");
18381       type = error_mark_node;
18382       goto out;
18383     }
18384
18385   /* At this point, we're going ahead with the class-specifier, even
18386      if some other problem occurs.  */
18387   cp_parser_commit_to_tentative_parse (parser);
18388   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18389     {
18390       cp_parser_error (parser,
18391                        "cannot specify %<override%> for a class");
18392       type = error_mark_node;
18393       goto out;
18394     }
18395   /* Issue the error about the overly-qualified name now.  */
18396   if (qualified_p)
18397     {
18398       cp_parser_error (parser,
18399                        "global qualification of class name is invalid");
18400       type = error_mark_node;
18401       goto out;
18402     }
18403   else if (invalid_nested_name_p)
18404     {
18405       cp_parser_error (parser,
18406                        "qualified name does not name a class");
18407       type = error_mark_node;
18408       goto out;
18409     }
18410   else if (nested_name_specifier)
18411     {
18412       tree scope;
18413
18414       /* Reject typedef-names in class heads.  */
18415       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18416         {
18417           error_at (type_start_token->location,
18418                     "invalid class name in declaration of %qD",
18419                     type);
18420           type = NULL_TREE;
18421           goto done;
18422         }
18423
18424       /* Figure out in what scope the declaration is being placed.  */
18425       scope = current_scope ();
18426       /* If that scope does not contain the scope in which the
18427          class was originally declared, the program is invalid.  */
18428       if (scope && !is_ancestor (scope, nested_name_specifier))
18429         {
18430           if (at_namespace_scope_p ())
18431             error_at (type_start_token->location,
18432                       "declaration of %qD in namespace %qD which does not "
18433                       "enclose %qD",
18434                       type, scope, nested_name_specifier);
18435           else
18436             error_at (type_start_token->location,
18437                       "declaration of %qD in %qD which does not enclose %qD",
18438                       type, scope, nested_name_specifier);
18439           type = NULL_TREE;
18440           goto done;
18441         }
18442       /* [dcl.meaning]
18443
18444          A declarator-id shall not be qualified except for the
18445          definition of a ... nested class outside of its class
18446          ... [or] the definition or explicit instantiation of a
18447          class member of a namespace outside of its namespace.  */
18448       if (scope == nested_name_specifier)
18449         {
18450           permerror (nested_name_specifier_token_start->location,
18451                      "extra qualification not allowed");
18452           nested_name_specifier = NULL_TREE;
18453           num_templates = 0;
18454         }
18455     }
18456   /* An explicit-specialization must be preceded by "template <>".  If
18457      it is not, try to recover gracefully.  */
18458   if (at_namespace_scope_p ()
18459       && parser->num_template_parameter_lists == 0
18460       && template_id_p)
18461     {
18462       error_at (type_start_token->location,
18463                 "an explicit specialization must be preceded by %<template <>%>");
18464       invalid_explicit_specialization_p = true;
18465       /* Take the same action that would have been taken by
18466          cp_parser_explicit_specialization.  */
18467       ++parser->num_template_parameter_lists;
18468       begin_specialization ();
18469     }
18470   /* There must be no "return" statements between this point and the
18471      end of this function; set "type "to the correct return value and
18472      use "goto done;" to return.  */
18473   /* Make sure that the right number of template parameters were
18474      present.  */
18475   if (!cp_parser_check_template_parameters (parser, num_templates,
18476                                             type_start_token->location,
18477                                             /*declarator=*/NULL))
18478     {
18479       /* If something went wrong, there is no point in even trying to
18480          process the class-definition.  */
18481       type = NULL_TREE;
18482       goto done;
18483     }
18484
18485   /* Look up the type.  */
18486   if (template_id_p)
18487     {
18488       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18489           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18490               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18491         {
18492           error_at (type_start_token->location,
18493                     "function template %qD redeclared as a class template", id);
18494           type = error_mark_node;
18495         }
18496       else
18497         {
18498           type = TREE_TYPE (id);
18499           type = maybe_process_partial_specialization (type);
18500         }
18501       if (nested_name_specifier)
18502         pushed_scope = push_scope (nested_name_specifier);
18503     }
18504   else if (nested_name_specifier)
18505     {
18506       tree class_type;
18507
18508       /* Given:
18509
18510             template <typename T> struct S { struct T };
18511             template <typename T> struct S<T>::T { };
18512
18513          we will get a TYPENAME_TYPE when processing the definition of
18514          `S::T'.  We need to resolve it to the actual type before we
18515          try to define it.  */
18516       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18517         {
18518           class_type = resolve_typename_type (TREE_TYPE (type),
18519                                               /*only_current_p=*/false);
18520           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18521             type = TYPE_NAME (class_type);
18522           else
18523             {
18524               cp_parser_error (parser, "could not resolve typename type");
18525               type = error_mark_node;
18526             }
18527         }
18528
18529       if (maybe_process_partial_specialization (TREE_TYPE (type))
18530           == error_mark_node)
18531         {
18532           type = NULL_TREE;
18533           goto done;
18534         }
18535
18536       class_type = current_class_type;
18537       /* Enter the scope indicated by the nested-name-specifier.  */
18538       pushed_scope = push_scope (nested_name_specifier);
18539       /* Get the canonical version of this type.  */
18540       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18541       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18542           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18543         {
18544           type = push_template_decl (type);
18545           if (type == error_mark_node)
18546             {
18547               type = NULL_TREE;
18548               goto done;
18549             }
18550         }
18551
18552       type = TREE_TYPE (type);
18553       *nested_name_specifier_p = true;
18554     }
18555   else      /* The name is not a nested name.  */
18556     {
18557       /* If the class was unnamed, create a dummy name.  */
18558       if (!id)
18559         id = make_anon_name ();
18560       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18561                        parser->num_template_parameter_lists);
18562     }
18563
18564   /* Indicate whether this class was declared as a `class' or as a
18565      `struct'.  */
18566   if (TREE_CODE (type) == RECORD_TYPE)
18567     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18568   cp_parser_check_class_key (class_key, type);
18569
18570   /* If this type was already complete, and we see another definition,
18571      that's an error.  */
18572   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18573     {
18574       error_at (type_start_token->location, "redefinition of %q#T",
18575                 type);
18576       error_at (type_start_token->location, "previous definition of %q+#T",
18577                 type);
18578       type = NULL_TREE;
18579       goto done;
18580     }
18581   else if (type == error_mark_node)
18582     type = NULL_TREE;
18583
18584   /* We will have entered the scope containing the class; the names of
18585      base classes should be looked up in that context.  For example:
18586
18587        struct A { struct B {}; struct C; };
18588        struct A::C : B {};
18589
18590      is valid.  */
18591
18592   /* Get the list of base-classes, if there is one.  */
18593   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18594     *bases = cp_parser_base_clause (parser);
18595
18596  done:
18597   /* Leave the scope given by the nested-name-specifier.  We will
18598      enter the class scope itself while processing the members.  */
18599   if (pushed_scope)
18600     pop_scope (pushed_scope);
18601
18602   if (invalid_explicit_specialization_p)
18603     {
18604       end_specialization ();
18605       --parser->num_template_parameter_lists;
18606     }
18607
18608   if (type)
18609     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18610   *attributes_p = attributes;
18611   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18612     CLASSTYPE_FINAL (type) = 1;
18613  out:
18614   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18615   return type;
18616 }
18617
18618 /* Parse a class-key.
18619
18620    class-key:
18621      class
18622      struct
18623      union
18624
18625    Returns the kind of class-key specified, or none_type to indicate
18626    error.  */
18627
18628 static enum tag_types
18629 cp_parser_class_key (cp_parser* parser)
18630 {
18631   cp_token *token;
18632   enum tag_types tag_type;
18633
18634   /* Look for the class-key.  */
18635   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18636   if (!token)
18637     return none_type;
18638
18639   /* Check to see if the TOKEN is a class-key.  */
18640   tag_type = cp_parser_token_is_class_key (token);
18641   if (!tag_type)
18642     cp_parser_error (parser, "expected class-key");
18643   return tag_type;
18644 }
18645
18646 /* Parse an (optional) member-specification.
18647
18648    member-specification:
18649      member-declaration member-specification [opt]
18650      access-specifier : member-specification [opt]  */
18651
18652 static void
18653 cp_parser_member_specification_opt (cp_parser* parser)
18654 {
18655   while (true)
18656     {
18657       cp_token *token;
18658       enum rid keyword;
18659
18660       /* Peek at the next token.  */
18661       token = cp_lexer_peek_token (parser->lexer);
18662       /* If it's a `}', or EOF then we've seen all the members.  */
18663       if (token->type == CPP_CLOSE_BRACE
18664           || token->type == CPP_EOF
18665           || token->type == CPP_PRAGMA_EOL)
18666         break;
18667
18668       /* See if this token is a keyword.  */
18669       keyword = token->keyword;
18670       switch (keyword)
18671         {
18672         case RID_PUBLIC:
18673         case RID_PROTECTED:
18674         case RID_PRIVATE:
18675           /* Consume the access-specifier.  */
18676           cp_lexer_consume_token (parser->lexer);
18677           /* Remember which access-specifier is active.  */
18678           current_access_specifier = token->u.value;
18679           /* Look for the `:'.  */
18680           cp_parser_require (parser, CPP_COLON, RT_COLON);
18681           break;
18682
18683         default:
18684           /* Accept #pragmas at class scope.  */
18685           if (token->type == CPP_PRAGMA)
18686             {
18687               cp_parser_pragma (parser, pragma_external);
18688               break;
18689             }
18690
18691           /* Otherwise, the next construction must be a
18692              member-declaration.  */
18693           cp_parser_member_declaration (parser);
18694         }
18695     }
18696 }
18697
18698 /* Parse a member-declaration.
18699
18700    member-declaration:
18701      decl-specifier-seq [opt] member-declarator-list [opt] ;
18702      function-definition ; [opt]
18703      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18704      using-declaration
18705      template-declaration
18706      alias-declaration
18707
18708    member-declarator-list:
18709      member-declarator
18710      member-declarator-list , member-declarator
18711
18712    member-declarator:
18713      declarator pure-specifier [opt]
18714      declarator constant-initializer [opt]
18715      identifier [opt] : constant-expression
18716
18717    GNU Extensions:
18718
18719    member-declaration:
18720      __extension__ member-declaration
18721
18722    member-declarator:
18723      declarator attributes [opt] pure-specifier [opt]
18724      declarator attributes [opt] constant-initializer [opt]
18725      identifier [opt] attributes [opt] : constant-expression  
18726
18727    C++0x Extensions:
18728
18729    member-declaration:
18730      static_assert-declaration  */
18731
18732 static void
18733 cp_parser_member_declaration (cp_parser* parser)
18734 {
18735   cp_decl_specifier_seq decl_specifiers;
18736   tree prefix_attributes;
18737   tree decl;
18738   int declares_class_or_enum;
18739   bool friend_p;
18740   cp_token *token = NULL;
18741   cp_token *decl_spec_token_start = NULL;
18742   cp_token *initializer_token_start = NULL;
18743   int saved_pedantic;
18744   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18745
18746   /* Check for the `__extension__' keyword.  */
18747   if (cp_parser_extension_opt (parser, &saved_pedantic))
18748     {
18749       /* Recurse.  */
18750       cp_parser_member_declaration (parser);
18751       /* Restore the old value of the PEDANTIC flag.  */
18752       pedantic = saved_pedantic;
18753
18754       return;
18755     }
18756
18757   /* Check for a template-declaration.  */
18758   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18759     {
18760       /* An explicit specialization here is an error condition, and we
18761          expect the specialization handler to detect and report this.  */
18762       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18763           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18764         cp_parser_explicit_specialization (parser);
18765       else
18766         cp_parser_template_declaration (parser, /*member_p=*/true);
18767
18768       return;
18769     }
18770
18771   /* Check for a using-declaration.  */
18772   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18773     {
18774       if (cxx_dialect < cxx0x)
18775         {
18776           /* Parse the using-declaration.  */
18777           cp_parser_using_declaration (parser,
18778                                        /*access_declaration_p=*/false);
18779           return;
18780         }
18781       else
18782         {
18783           tree decl;
18784           cp_parser_parse_tentatively (parser);
18785           decl = cp_parser_alias_declaration (parser);
18786           if (cp_parser_parse_definitely (parser))
18787             finish_member_declaration (decl);
18788           else
18789             cp_parser_using_declaration (parser,
18790                                          /*access_declaration_p=*/false);
18791           return;
18792         }
18793     }
18794
18795   /* Check for @defs.  */
18796   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18797     {
18798       tree ivar, member;
18799       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18800       ivar = ivar_chains;
18801       while (ivar)
18802         {
18803           member = ivar;
18804           ivar = TREE_CHAIN (member);
18805           TREE_CHAIN (member) = NULL_TREE;
18806           finish_member_declaration (member);
18807         }
18808       return;
18809     }
18810
18811   /* If the next token is `static_assert' we have a static assertion.  */
18812   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18813     {
18814       cp_parser_static_assert (parser, /*member_p=*/true);
18815       return;
18816     }
18817
18818   parser->colon_corrects_to_scope_p = false;
18819
18820   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18821     goto out;
18822
18823   /* Parse the decl-specifier-seq.  */
18824   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18825   cp_parser_decl_specifier_seq (parser,
18826                                 CP_PARSER_FLAGS_OPTIONAL,
18827                                 &decl_specifiers,
18828                                 &declares_class_or_enum);
18829   prefix_attributes = decl_specifiers.attributes;
18830   decl_specifiers.attributes = NULL_TREE;
18831   /* Check for an invalid type-name.  */
18832   if (!decl_specifiers.any_type_specifiers_p
18833       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18834     goto out;
18835   /* If there is no declarator, then the decl-specifier-seq should
18836      specify a type.  */
18837   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18838     {
18839       /* If there was no decl-specifier-seq, and the next token is a
18840          `;', then we have something like:
18841
18842            struct S { ; };
18843
18844          [class.mem]
18845
18846          Each member-declaration shall declare at least one member
18847          name of the class.  */
18848       if (!decl_specifiers.any_specifiers_p)
18849         {
18850           cp_token *token = cp_lexer_peek_token (parser->lexer);
18851           if (!in_system_header_at (token->location))
18852             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18853         }
18854       else
18855         {
18856           tree type;
18857
18858           /* See if this declaration is a friend.  */
18859           friend_p = cp_parser_friend_p (&decl_specifiers);
18860           /* If there were decl-specifiers, check to see if there was
18861              a class-declaration.  */
18862           type = check_tag_decl (&decl_specifiers);
18863           /* Nested classes have already been added to the class, but
18864              a `friend' needs to be explicitly registered.  */
18865           if (friend_p)
18866             {
18867               /* If the `friend' keyword was present, the friend must
18868                  be introduced with a class-key.  */
18869                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18870                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18871                           "in C++03 a class-key must be used "
18872                           "when declaring a friend");
18873                /* In this case:
18874
18875                     template <typename T> struct A {
18876                       friend struct A<T>::B;
18877                     };
18878
18879                   A<T>::B will be represented by a TYPENAME_TYPE, and
18880                   therefore not recognized by check_tag_decl.  */
18881                if (!type)
18882                  {
18883                    type = decl_specifiers.type;
18884                    if (type && TREE_CODE (type) == TYPE_DECL)
18885                      type = TREE_TYPE (type);
18886                  }
18887                if (!type || !TYPE_P (type))
18888                  error_at (decl_spec_token_start->location,
18889                            "friend declaration does not name a class or "
18890                            "function");
18891                else
18892                  make_friend_class (current_class_type, type,
18893                                     /*complain=*/true);
18894             }
18895           /* If there is no TYPE, an error message will already have
18896              been issued.  */
18897           else if (!type || type == error_mark_node)
18898             ;
18899           /* An anonymous aggregate has to be handled specially; such
18900              a declaration really declares a data member (with a
18901              particular type), as opposed to a nested class.  */
18902           else if (ANON_AGGR_TYPE_P (type))
18903             {
18904               /* Remove constructors and such from TYPE, now that we
18905                  know it is an anonymous aggregate.  */
18906               fixup_anonymous_aggr (type);
18907               /* And make the corresponding data member.  */
18908               decl = build_decl (decl_spec_token_start->location,
18909                                  FIELD_DECL, NULL_TREE, type);
18910               /* Add it to the class.  */
18911               finish_member_declaration (decl);
18912             }
18913           else
18914             cp_parser_check_access_in_redeclaration
18915                                               (TYPE_NAME (type),
18916                                                decl_spec_token_start->location);
18917         }
18918     }
18919   else
18920     {
18921       bool assume_semicolon = false;
18922
18923       /* See if these declarations will be friends.  */
18924       friend_p = cp_parser_friend_p (&decl_specifiers);
18925
18926       /* Keep going until we hit the `;' at the end of the
18927          declaration.  */
18928       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18929         {
18930           tree attributes = NULL_TREE;
18931           tree first_attribute;
18932
18933           /* Peek at the next token.  */
18934           token = cp_lexer_peek_token (parser->lexer);
18935
18936           /* Check for a bitfield declaration.  */
18937           if (token->type == CPP_COLON
18938               || (token->type == CPP_NAME
18939                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18940                   == CPP_COLON))
18941             {
18942               tree identifier;
18943               tree width;
18944
18945               /* Get the name of the bitfield.  Note that we cannot just
18946                  check TOKEN here because it may have been invalidated by
18947                  the call to cp_lexer_peek_nth_token above.  */
18948               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18949                 identifier = cp_parser_identifier (parser);
18950               else
18951                 identifier = NULL_TREE;
18952
18953               /* Consume the `:' token.  */
18954               cp_lexer_consume_token (parser->lexer);
18955               /* Get the width of the bitfield.  */
18956               width
18957                 = cp_parser_constant_expression (parser,
18958                                                  /*allow_non_constant=*/false,
18959                                                  NULL);
18960
18961               /* Look for attributes that apply to the bitfield.  */
18962               attributes = cp_parser_attributes_opt (parser);
18963               /* Remember which attributes are prefix attributes and
18964                  which are not.  */
18965               first_attribute = attributes;
18966               /* Combine the attributes.  */
18967               attributes = chainon (prefix_attributes, attributes);
18968
18969               /* Create the bitfield declaration.  */
18970               decl = grokbitfield (identifier
18971                                    ? make_id_declarator (NULL_TREE,
18972                                                          identifier,
18973                                                          sfk_none)
18974                                    : NULL,
18975                                    &decl_specifiers,
18976                                    width,
18977                                    attributes);
18978             }
18979           else
18980             {
18981               cp_declarator *declarator;
18982               tree initializer;
18983               tree asm_specification;
18984               int ctor_dtor_or_conv_p;
18985
18986               /* Parse the declarator.  */
18987               declarator
18988                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18989                                         &ctor_dtor_or_conv_p,
18990                                         /*parenthesized_p=*/NULL,
18991                                         /*member_p=*/true);
18992
18993               /* If something went wrong parsing the declarator, make sure
18994                  that we at least consume some tokens.  */
18995               if (declarator == cp_error_declarator)
18996                 {
18997                   /* Skip to the end of the statement.  */
18998                   cp_parser_skip_to_end_of_statement (parser);
18999                   /* If the next token is not a semicolon, that is
19000                      probably because we just skipped over the body of
19001                      a function.  So, we consume a semicolon if
19002                      present, but do not issue an error message if it
19003                      is not present.  */
19004                   if (cp_lexer_next_token_is (parser->lexer,
19005                                               CPP_SEMICOLON))
19006                     cp_lexer_consume_token (parser->lexer);
19007                   goto out;
19008                 }
19009
19010               if (declares_class_or_enum & 2)
19011                 cp_parser_check_for_definition_in_return_type
19012                                             (declarator, decl_specifiers.type,
19013                                              decl_specifiers.type_location);
19014
19015               /* Look for an asm-specification.  */
19016               asm_specification = cp_parser_asm_specification_opt (parser);
19017               /* Look for attributes that apply to the declaration.  */
19018               attributes = cp_parser_attributes_opt (parser);
19019               /* Remember which attributes are prefix attributes and
19020                  which are not.  */
19021               first_attribute = attributes;
19022               /* Combine the attributes.  */
19023               attributes = chainon (prefix_attributes, attributes);
19024
19025               /* If it's an `=', then we have a constant-initializer or a
19026                  pure-specifier.  It is not correct to parse the
19027                  initializer before registering the member declaration
19028                  since the member declaration should be in scope while
19029                  its initializer is processed.  However, the rest of the
19030                  front end does not yet provide an interface that allows
19031                  us to handle this correctly.  */
19032               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
19033                 {
19034                   /* In [class.mem]:
19035
19036                      A pure-specifier shall be used only in the declaration of
19037                      a virtual function.
19038
19039                      A member-declarator can contain a constant-initializer
19040                      only if it declares a static member of integral or
19041                      enumeration type.
19042
19043                      Therefore, if the DECLARATOR is for a function, we look
19044                      for a pure-specifier; otherwise, we look for a
19045                      constant-initializer.  When we call `grokfield', it will
19046                      perform more stringent semantics checks.  */
19047                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
19048                   if (function_declarator_p (declarator)
19049                       || (decl_specifiers.type
19050                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
19051                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
19052                               == FUNCTION_TYPE)))
19053                     initializer = cp_parser_pure_specifier (parser);
19054                   else if (decl_specifiers.storage_class != sc_static)
19055                     initializer = cp_parser_save_nsdmi (parser);
19056                   else if (cxx_dialect >= cxx0x)
19057                     {
19058                       bool nonconst;
19059                       /* Don't require a constant rvalue in C++11, since we
19060                          might want a reference constant.  We'll enforce
19061                          constancy later.  */
19062                       cp_lexer_consume_token (parser->lexer);
19063                       /* Parse the initializer.  */
19064                       initializer = cp_parser_initializer_clause (parser,
19065                                                                   &nonconst);
19066                     }
19067                   else
19068                     /* Parse the initializer.  */
19069                     initializer = cp_parser_constant_initializer (parser);
19070                 }
19071               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
19072                        && !function_declarator_p (declarator))
19073                 {
19074                   bool x;
19075                   if (decl_specifiers.storage_class != sc_static)
19076                     initializer = cp_parser_save_nsdmi (parser);
19077                   else
19078                     initializer = cp_parser_initializer (parser, &x, &x);
19079                 }
19080               /* Otherwise, there is no initializer.  */
19081               else
19082                 initializer = NULL_TREE;
19083
19084               /* See if we are probably looking at a function
19085                  definition.  We are certainly not looking at a
19086                  member-declarator.  Calling `grokfield' has
19087                  side-effects, so we must not do it unless we are sure
19088                  that we are looking at a member-declarator.  */
19089               if (cp_parser_token_starts_function_definition_p
19090                   (cp_lexer_peek_token (parser->lexer)))
19091                 {
19092                   /* The grammar does not allow a pure-specifier to be
19093                      used when a member function is defined.  (It is
19094                      possible that this fact is an oversight in the
19095                      standard, since a pure function may be defined
19096                      outside of the class-specifier.  */
19097                   if (initializer)
19098                     error_at (initializer_token_start->location,
19099                               "pure-specifier on function-definition");
19100                   decl = cp_parser_save_member_function_body (parser,
19101                                                               &decl_specifiers,
19102                                                               declarator,
19103                                                               attributes);
19104                   /* If the member was not a friend, declare it here.  */
19105                   if (!friend_p)
19106                     finish_member_declaration (decl);
19107                   /* Peek at the next token.  */
19108                   token = cp_lexer_peek_token (parser->lexer);
19109                   /* If the next token is a semicolon, consume it.  */
19110                   if (token->type == CPP_SEMICOLON)
19111                     cp_lexer_consume_token (parser->lexer);
19112                   goto out;
19113                 }
19114               else
19115                 if (declarator->kind == cdk_function)
19116                   declarator->id_loc = token->location;
19117                 /* Create the declaration.  */
19118                 decl = grokfield (declarator, &decl_specifiers,
19119                                   initializer, /*init_const_expr_p=*/true,
19120                                   asm_specification,
19121                                   attributes);
19122             }
19123
19124           /* Reset PREFIX_ATTRIBUTES.  */
19125           while (attributes && TREE_CHAIN (attributes) != first_attribute)
19126             attributes = TREE_CHAIN (attributes);
19127           if (attributes)
19128             TREE_CHAIN (attributes) = NULL_TREE;
19129
19130           /* If there is any qualification still in effect, clear it
19131              now; we will be starting fresh with the next declarator.  */
19132           parser->scope = NULL_TREE;
19133           parser->qualifying_scope = NULL_TREE;
19134           parser->object_scope = NULL_TREE;
19135           /* If it's a `,', then there are more declarators.  */
19136           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
19137             cp_lexer_consume_token (parser->lexer);
19138           /* If the next token isn't a `;', then we have a parse error.  */
19139           else if (cp_lexer_next_token_is_not (parser->lexer,
19140                                                CPP_SEMICOLON))
19141             {
19142               /* The next token might be a ways away from where the
19143                  actual semicolon is missing.  Find the previous token
19144                  and use that for our error position.  */
19145               cp_token *token = cp_lexer_previous_token (parser->lexer);
19146               error_at (token->location,
19147                         "expected %<;%> at end of member declaration");
19148
19149               /* Assume that the user meant to provide a semicolon.  If
19150                  we were to cp_parser_skip_to_end_of_statement, we might
19151                  skip to a semicolon inside a member function definition
19152                  and issue nonsensical error messages.  */
19153               assume_semicolon = true;
19154             }
19155
19156           if (decl)
19157             {
19158               /* Add DECL to the list of members.  */
19159               if (!friend_p)
19160                 finish_member_declaration (decl);
19161
19162               if (TREE_CODE (decl) == FUNCTION_DECL)
19163                 cp_parser_save_default_args (parser, decl);
19164               else if (TREE_CODE (decl) == FIELD_DECL
19165                        && !DECL_C_BIT_FIELD (decl)
19166                        && DECL_INITIAL (decl))
19167                 /* Add DECL to the queue of NSDMI to be parsed later.  */
19168                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
19169             }
19170
19171           if (assume_semicolon)
19172             goto out;
19173         }
19174     }
19175
19176   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19177  out:
19178   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
19179 }
19180
19181 /* Parse a pure-specifier.
19182
19183    pure-specifier:
19184      = 0
19185
19186    Returns INTEGER_ZERO_NODE if a pure specifier is found.
19187    Otherwise, ERROR_MARK_NODE is returned.  */
19188
19189 static tree
19190 cp_parser_pure_specifier (cp_parser* parser)
19191 {
19192   cp_token *token;
19193
19194   /* Look for the `=' token.  */
19195   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19196     return error_mark_node;
19197   /* Look for the `0' token.  */
19198   token = cp_lexer_peek_token (parser->lexer);
19199
19200   if (token->type == CPP_EOF
19201       || token->type == CPP_PRAGMA_EOL)
19202     return error_mark_node;
19203
19204   cp_lexer_consume_token (parser->lexer);
19205
19206   /* Accept = default or = delete in c++0x mode.  */
19207   if (token->keyword == RID_DEFAULT
19208       || token->keyword == RID_DELETE)
19209     {
19210       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19211       return token->u.value;
19212     }
19213
19214   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19215   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19216     {
19217       cp_parser_error (parser,
19218                        "invalid pure specifier (only %<= 0%> is allowed)");
19219       cp_parser_skip_to_end_of_statement (parser);
19220       return error_mark_node;
19221     }
19222   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19223     {
19224       error_at (token->location, "templates may not be %<virtual%>");
19225       return error_mark_node;
19226     }
19227
19228   return integer_zero_node;
19229 }
19230
19231 /* Parse a constant-initializer.
19232
19233    constant-initializer:
19234      = constant-expression
19235
19236    Returns a representation of the constant-expression.  */
19237
19238 static tree
19239 cp_parser_constant_initializer (cp_parser* parser)
19240 {
19241   /* Look for the `=' token.  */
19242   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19243     return error_mark_node;
19244
19245   /* It is invalid to write:
19246
19247        struct S { static const int i = { 7 }; };
19248
19249      */
19250   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19251     {
19252       cp_parser_error (parser,
19253                        "a brace-enclosed initializer is not allowed here");
19254       /* Consume the opening brace.  */
19255       cp_lexer_consume_token (parser->lexer);
19256       /* Skip the initializer.  */
19257       cp_parser_skip_to_closing_brace (parser);
19258       /* Look for the trailing `}'.  */
19259       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19260
19261       return error_mark_node;
19262     }
19263
19264   return cp_parser_constant_expression (parser,
19265                                         /*allow_non_constant=*/false,
19266                                         NULL);
19267 }
19268
19269 /* Derived classes [gram.class.derived] */
19270
19271 /* Parse a base-clause.
19272
19273    base-clause:
19274      : base-specifier-list
19275
19276    base-specifier-list:
19277      base-specifier ... [opt]
19278      base-specifier-list , base-specifier ... [opt]
19279
19280    Returns a TREE_LIST representing the base-classes, in the order in
19281    which they were declared.  The representation of each node is as
19282    described by cp_parser_base_specifier.
19283
19284    In the case that no bases are specified, this function will return
19285    NULL_TREE, not ERROR_MARK_NODE.  */
19286
19287 static tree
19288 cp_parser_base_clause (cp_parser* parser)
19289 {
19290   tree bases = NULL_TREE;
19291
19292   /* Look for the `:' that begins the list.  */
19293   cp_parser_require (parser, CPP_COLON, RT_COLON);
19294
19295   /* Scan the base-specifier-list.  */
19296   while (true)
19297     {
19298       cp_token *token;
19299       tree base;
19300       bool pack_expansion_p = false;
19301
19302       /* Look for the base-specifier.  */
19303       base = cp_parser_base_specifier (parser);
19304       /* Look for the (optional) ellipsis. */
19305       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19306         {
19307           /* Consume the `...'. */
19308           cp_lexer_consume_token (parser->lexer);
19309
19310           pack_expansion_p = true;
19311         }
19312
19313       /* Add BASE to the front of the list.  */
19314       if (base && base != error_mark_node)
19315         {
19316           if (pack_expansion_p)
19317             /* Make this a pack expansion type. */
19318             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19319
19320           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19321             {
19322               TREE_CHAIN (base) = bases;
19323               bases = base;
19324             }
19325         }
19326       /* Peek at the next token.  */
19327       token = cp_lexer_peek_token (parser->lexer);
19328       /* If it's not a comma, then the list is complete.  */
19329       if (token->type != CPP_COMMA)
19330         break;
19331       /* Consume the `,'.  */
19332       cp_lexer_consume_token (parser->lexer);
19333     }
19334
19335   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19336      base class had a qualified name.  However, the next name that
19337      appears is certainly not qualified.  */
19338   parser->scope = NULL_TREE;
19339   parser->qualifying_scope = NULL_TREE;
19340   parser->object_scope = NULL_TREE;
19341
19342   return nreverse (bases);
19343 }
19344
19345 /* Parse a base-specifier.
19346
19347    base-specifier:
19348      :: [opt] nested-name-specifier [opt] class-name
19349      virtual access-specifier [opt] :: [opt] nested-name-specifier
19350        [opt] class-name
19351      access-specifier virtual [opt] :: [opt] nested-name-specifier
19352        [opt] class-name
19353
19354    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19355    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19356    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19357    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19358
19359 static tree
19360 cp_parser_base_specifier (cp_parser* parser)
19361 {
19362   cp_token *token;
19363   bool done = false;
19364   bool virtual_p = false;
19365   bool duplicate_virtual_error_issued_p = false;
19366   bool duplicate_access_error_issued_p = false;
19367   bool class_scope_p, template_p;
19368   tree access = access_default_node;
19369   tree type;
19370
19371   /* Process the optional `virtual' and `access-specifier'.  */
19372   while (!done)
19373     {
19374       /* Peek at the next token.  */
19375       token = cp_lexer_peek_token (parser->lexer);
19376       /* Process `virtual'.  */
19377       switch (token->keyword)
19378         {
19379         case RID_VIRTUAL:
19380           /* If `virtual' appears more than once, issue an error.  */
19381           if (virtual_p && !duplicate_virtual_error_issued_p)
19382             {
19383               cp_parser_error (parser,
19384                                "%<virtual%> specified more than once in base-specified");
19385               duplicate_virtual_error_issued_p = true;
19386             }
19387
19388           virtual_p = true;
19389
19390           /* Consume the `virtual' token.  */
19391           cp_lexer_consume_token (parser->lexer);
19392
19393           break;
19394
19395         case RID_PUBLIC:
19396         case RID_PROTECTED:
19397         case RID_PRIVATE:
19398           /* If more than one access specifier appears, issue an
19399              error.  */
19400           if (access != access_default_node
19401               && !duplicate_access_error_issued_p)
19402             {
19403               cp_parser_error (parser,
19404                                "more than one access specifier in base-specified");
19405               duplicate_access_error_issued_p = true;
19406             }
19407
19408           access = ridpointers[(int) token->keyword];
19409
19410           /* Consume the access-specifier.  */
19411           cp_lexer_consume_token (parser->lexer);
19412
19413           break;
19414
19415         default:
19416           done = true;
19417           break;
19418         }
19419     }
19420   /* It is not uncommon to see programs mechanically, erroneously, use
19421      the 'typename' keyword to denote (dependent) qualified types
19422      as base classes.  */
19423   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19424     {
19425       token = cp_lexer_peek_token (parser->lexer);
19426       if (!processing_template_decl)
19427         error_at (token->location,
19428                   "keyword %<typename%> not allowed outside of templates");
19429       else
19430         error_at (token->location,
19431                   "keyword %<typename%> not allowed in this context "
19432                   "(the base class is implicitly a type)");
19433       cp_lexer_consume_token (parser->lexer);
19434     }
19435
19436   /* Look for the optional `::' operator.  */
19437   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19438   /* Look for the nested-name-specifier.  The simplest way to
19439      implement:
19440
19441        [temp.res]
19442
19443        The keyword `typename' is not permitted in a base-specifier or
19444        mem-initializer; in these contexts a qualified name that
19445        depends on a template-parameter is implicitly assumed to be a
19446        type name.
19447
19448      is to pretend that we have seen the `typename' keyword at this
19449      point.  */
19450   cp_parser_nested_name_specifier_opt (parser,
19451                                        /*typename_keyword_p=*/true,
19452                                        /*check_dependency_p=*/true,
19453                                        typename_type,
19454                                        /*is_declaration=*/true);
19455   /* If the base class is given by a qualified name, assume that names
19456      we see are type names or templates, as appropriate.  */
19457   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19458   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19459
19460   if (!parser->scope
19461       && cp_lexer_next_token_is_decltype (parser->lexer))
19462     /* DR 950 allows decltype as a base-specifier.  */
19463     type = cp_parser_decltype (parser);
19464   else
19465     {
19466       /* Otherwise, look for the class-name.  */
19467       type = cp_parser_class_name (parser,
19468                                    class_scope_p,
19469                                    template_p,
19470                                    typename_type,
19471                                    /*check_dependency_p=*/true,
19472                                    /*class_head_p=*/false,
19473                                    /*is_declaration=*/true);
19474       type = TREE_TYPE (type);
19475     }
19476
19477   if (type == error_mark_node)
19478     return error_mark_node;
19479
19480   return finish_base_specifier (type, access, virtual_p);
19481 }
19482
19483 /* Exception handling [gram.exception] */
19484
19485 /* Parse an (optional) exception-specification.
19486
19487    exception-specification:
19488      throw ( type-id-list [opt] )
19489
19490    Returns a TREE_LIST representing the exception-specification.  The
19491    TREE_VALUE of each node is a type.  */
19492
19493 static tree
19494 cp_parser_exception_specification_opt (cp_parser* parser)
19495 {
19496   cp_token *token;
19497   tree type_id_list;
19498   const char *saved_message;
19499
19500   /* Peek at the next token.  */
19501   token = cp_lexer_peek_token (parser->lexer);
19502
19503   /* Is it a noexcept-specification?  */
19504   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19505     {
19506       tree expr;
19507       cp_lexer_consume_token (parser->lexer);
19508
19509       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19510         {
19511           cp_lexer_consume_token (parser->lexer);
19512
19513           /* Types may not be defined in an exception-specification.  */
19514           saved_message = parser->type_definition_forbidden_message;
19515           parser->type_definition_forbidden_message
19516             = G_("types may not be defined in an exception-specification");
19517
19518           expr = cp_parser_constant_expression (parser, false, NULL);
19519
19520           /* Restore the saved message.  */
19521           parser->type_definition_forbidden_message = saved_message;
19522
19523           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19524         }
19525       else
19526         expr = boolean_true_node;
19527
19528       return build_noexcept_spec (expr, tf_warning_or_error);
19529     }
19530
19531   /* If it's not `throw', then there's no exception-specification.  */
19532   if (!cp_parser_is_keyword (token, RID_THROW))
19533     return NULL_TREE;
19534
19535 #if 0
19536   /* Enable this once a lot of code has transitioned to noexcept?  */
19537   if (cxx_dialect == cxx0x && !in_system_header)
19538     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19539              "deprecated in C++0x; use %<noexcept%> instead");
19540 #endif
19541
19542   /* Consume the `throw'.  */
19543   cp_lexer_consume_token (parser->lexer);
19544
19545   /* Look for the `('.  */
19546   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19547
19548   /* Peek at the next token.  */
19549   token = cp_lexer_peek_token (parser->lexer);
19550   /* If it's not a `)', then there is a type-id-list.  */
19551   if (token->type != CPP_CLOSE_PAREN)
19552     {
19553       /* Types may not be defined in an exception-specification.  */
19554       saved_message = parser->type_definition_forbidden_message;
19555       parser->type_definition_forbidden_message
19556         = G_("types may not be defined in an exception-specification");
19557       /* Parse the type-id-list.  */
19558       type_id_list = cp_parser_type_id_list (parser);
19559       /* Restore the saved message.  */
19560       parser->type_definition_forbidden_message = saved_message;
19561     }
19562   else
19563     type_id_list = empty_except_spec;
19564
19565   /* Look for the `)'.  */
19566   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19567
19568   return type_id_list;
19569 }
19570
19571 /* Parse an (optional) type-id-list.
19572
19573    type-id-list:
19574      type-id ... [opt]
19575      type-id-list , type-id ... [opt]
19576
19577    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19578    in the order that the types were presented.  */
19579
19580 static tree
19581 cp_parser_type_id_list (cp_parser* parser)
19582 {
19583   tree types = NULL_TREE;
19584
19585   while (true)
19586     {
19587       cp_token *token;
19588       tree type;
19589
19590       /* Get the next type-id.  */
19591       type = cp_parser_type_id (parser);
19592       /* Parse the optional ellipsis. */
19593       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19594         {
19595           /* Consume the `...'. */
19596           cp_lexer_consume_token (parser->lexer);
19597
19598           /* Turn the type into a pack expansion expression. */
19599           type = make_pack_expansion (type);
19600         }
19601       /* Add it to the list.  */
19602       types = add_exception_specifier (types, type, /*complain=*/1);
19603       /* Peek at the next token.  */
19604       token = cp_lexer_peek_token (parser->lexer);
19605       /* If it is not a `,', we are done.  */
19606       if (token->type != CPP_COMMA)
19607         break;
19608       /* Consume the `,'.  */
19609       cp_lexer_consume_token (parser->lexer);
19610     }
19611
19612   return nreverse (types);
19613 }
19614
19615 /* Parse a try-block.
19616
19617    try-block:
19618      try compound-statement handler-seq  */
19619
19620 static tree
19621 cp_parser_try_block (cp_parser* parser)
19622 {
19623   tree try_block;
19624
19625   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19626   try_block = begin_try_block ();
19627   cp_parser_compound_statement (parser, NULL, true, false);
19628   finish_try_block (try_block);
19629   cp_parser_handler_seq (parser);
19630   finish_handler_sequence (try_block);
19631
19632   return try_block;
19633 }
19634
19635 /* Parse a function-try-block.
19636
19637    function-try-block:
19638      try ctor-initializer [opt] function-body handler-seq  */
19639
19640 static bool
19641 cp_parser_function_try_block (cp_parser* parser)
19642 {
19643   tree compound_stmt;
19644   tree try_block;
19645   bool ctor_initializer_p;
19646
19647   /* Look for the `try' keyword.  */
19648   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19649     return false;
19650   /* Let the rest of the front end know where we are.  */
19651   try_block = begin_function_try_block (&compound_stmt);
19652   /* Parse the function-body.  */
19653   ctor_initializer_p
19654     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19655   /* We're done with the `try' part.  */
19656   finish_function_try_block (try_block);
19657   /* Parse the handlers.  */
19658   cp_parser_handler_seq (parser);
19659   /* We're done with the handlers.  */
19660   finish_function_handler_sequence (try_block, compound_stmt);
19661
19662   return ctor_initializer_p;
19663 }
19664
19665 /* Parse a handler-seq.
19666
19667    handler-seq:
19668      handler handler-seq [opt]  */
19669
19670 static void
19671 cp_parser_handler_seq (cp_parser* parser)
19672 {
19673   while (true)
19674     {
19675       cp_token *token;
19676
19677       /* Parse the handler.  */
19678       cp_parser_handler (parser);
19679       /* Peek at the next token.  */
19680       token = cp_lexer_peek_token (parser->lexer);
19681       /* If it's not `catch' then there are no more handlers.  */
19682       if (!cp_parser_is_keyword (token, RID_CATCH))
19683         break;
19684     }
19685 }
19686
19687 /* Parse a handler.
19688
19689    handler:
19690      catch ( exception-declaration ) compound-statement  */
19691
19692 static void
19693 cp_parser_handler (cp_parser* parser)
19694 {
19695   tree handler;
19696   tree declaration;
19697
19698   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19699   handler = begin_handler ();
19700   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19701   declaration = cp_parser_exception_declaration (parser);
19702   finish_handler_parms (declaration, handler);
19703   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19704   cp_parser_compound_statement (parser, NULL, false, false);
19705   finish_handler (handler);
19706 }
19707
19708 /* Parse an exception-declaration.
19709
19710    exception-declaration:
19711      type-specifier-seq declarator
19712      type-specifier-seq abstract-declarator
19713      type-specifier-seq
19714      ...
19715
19716    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19717    ellipsis variant is used.  */
19718
19719 static tree
19720 cp_parser_exception_declaration (cp_parser* parser)
19721 {
19722   cp_decl_specifier_seq type_specifiers;
19723   cp_declarator *declarator;
19724   const char *saved_message;
19725
19726   /* If it's an ellipsis, it's easy to handle.  */
19727   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19728     {
19729       /* Consume the `...' token.  */
19730       cp_lexer_consume_token (parser->lexer);
19731       return NULL_TREE;
19732     }
19733
19734   /* Types may not be defined in exception-declarations.  */
19735   saved_message = parser->type_definition_forbidden_message;
19736   parser->type_definition_forbidden_message
19737     = G_("types may not be defined in exception-declarations");
19738
19739   /* Parse the type-specifier-seq.  */
19740   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19741                                 /*is_trailing_return=*/false,
19742                                 &type_specifiers);
19743   /* If it's a `)', then there is no declarator.  */
19744   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19745     declarator = NULL;
19746   else
19747     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19748                                        /*ctor_dtor_or_conv_p=*/NULL,
19749                                        /*parenthesized_p=*/NULL,
19750                                        /*member_p=*/false);
19751
19752   /* Restore the saved message.  */
19753   parser->type_definition_forbidden_message = saved_message;
19754
19755   if (!type_specifiers.any_specifiers_p)
19756     return error_mark_node;
19757
19758   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19759 }
19760
19761 /* Parse a throw-expression.
19762
19763    throw-expression:
19764      throw assignment-expression [opt]
19765
19766    Returns a THROW_EXPR representing the throw-expression.  */
19767
19768 static tree
19769 cp_parser_throw_expression (cp_parser* parser)
19770 {
19771   tree expression;
19772   cp_token* token;
19773
19774   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19775   token = cp_lexer_peek_token (parser->lexer);
19776   /* Figure out whether or not there is an assignment-expression
19777      following the "throw" keyword.  */
19778   if (token->type == CPP_COMMA
19779       || token->type == CPP_SEMICOLON
19780       || token->type == CPP_CLOSE_PAREN
19781       || token->type == CPP_CLOSE_SQUARE
19782       || token->type == CPP_CLOSE_BRACE
19783       || token->type == CPP_COLON)
19784     expression = NULL_TREE;
19785   else
19786     expression = cp_parser_assignment_expression (parser,
19787                                                   /*cast_p=*/false, NULL);
19788
19789   return build_throw (expression);
19790 }
19791
19792 /* GNU Extensions */
19793
19794 /* Parse an (optional) asm-specification.
19795
19796    asm-specification:
19797      asm ( string-literal )
19798
19799    If the asm-specification is present, returns a STRING_CST
19800    corresponding to the string-literal.  Otherwise, returns
19801    NULL_TREE.  */
19802
19803 static tree
19804 cp_parser_asm_specification_opt (cp_parser* parser)
19805 {
19806   cp_token *token;
19807   tree asm_specification;
19808
19809   /* Peek at the next token.  */
19810   token = cp_lexer_peek_token (parser->lexer);
19811   /* If the next token isn't the `asm' keyword, then there's no
19812      asm-specification.  */
19813   if (!cp_parser_is_keyword (token, RID_ASM))
19814     return NULL_TREE;
19815
19816   /* Consume the `asm' token.  */
19817   cp_lexer_consume_token (parser->lexer);
19818   /* Look for the `('.  */
19819   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19820
19821   /* Look for the string-literal.  */
19822   asm_specification = cp_parser_string_literal (parser, false, false);
19823
19824   /* Look for the `)'.  */
19825   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19826
19827   return asm_specification;
19828 }
19829
19830 /* Parse an asm-operand-list.
19831
19832    asm-operand-list:
19833      asm-operand
19834      asm-operand-list , asm-operand
19835
19836    asm-operand:
19837      string-literal ( expression )
19838      [ string-literal ] string-literal ( expression )
19839
19840    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19841    each node is the expression.  The TREE_PURPOSE is itself a
19842    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19843    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19844    is a STRING_CST for the string literal before the parenthesis. Returns
19845    ERROR_MARK_NODE if any of the operands are invalid.  */
19846
19847 static tree
19848 cp_parser_asm_operand_list (cp_parser* parser)
19849 {
19850   tree asm_operands = NULL_TREE;
19851   bool invalid_operands = false;
19852
19853   while (true)
19854     {
19855       tree string_literal;
19856       tree expression;
19857       tree name;
19858
19859       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19860         {
19861           /* Consume the `[' token.  */
19862           cp_lexer_consume_token (parser->lexer);
19863           /* Read the operand name.  */
19864           name = cp_parser_identifier (parser);
19865           if (name != error_mark_node)
19866             name = build_string (IDENTIFIER_LENGTH (name),
19867                                  IDENTIFIER_POINTER (name));
19868           /* Look for the closing `]'.  */
19869           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19870         }
19871       else
19872         name = NULL_TREE;
19873       /* Look for the string-literal.  */
19874       string_literal = cp_parser_string_literal (parser, false, false);
19875
19876       /* Look for the `('.  */
19877       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19878       /* Parse the expression.  */
19879       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19880       /* Look for the `)'.  */
19881       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19882
19883       if (name == error_mark_node 
19884           || string_literal == error_mark_node 
19885           || expression == error_mark_node)
19886         invalid_operands = true;
19887
19888       /* Add this operand to the list.  */
19889       asm_operands = tree_cons (build_tree_list (name, string_literal),
19890                                 expression,
19891                                 asm_operands);
19892       /* If the next token is not a `,', there are no more
19893          operands.  */
19894       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19895         break;
19896       /* Consume the `,'.  */
19897       cp_lexer_consume_token (parser->lexer);
19898     }
19899
19900   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19901 }
19902
19903 /* Parse an asm-clobber-list.
19904
19905    asm-clobber-list:
19906      string-literal
19907      asm-clobber-list , string-literal
19908
19909    Returns a TREE_LIST, indicating the clobbers in the order that they
19910    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19911
19912 static tree
19913 cp_parser_asm_clobber_list (cp_parser* parser)
19914 {
19915   tree clobbers = NULL_TREE;
19916
19917   while (true)
19918     {
19919       tree string_literal;
19920
19921       /* Look for the string literal.  */
19922       string_literal = cp_parser_string_literal (parser, false, false);
19923       /* Add it to the list.  */
19924       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19925       /* If the next token is not a `,', then the list is
19926          complete.  */
19927       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19928         break;
19929       /* Consume the `,' token.  */
19930       cp_lexer_consume_token (parser->lexer);
19931     }
19932
19933   return clobbers;
19934 }
19935
19936 /* Parse an asm-label-list.
19937
19938    asm-label-list:
19939      identifier
19940      asm-label-list , identifier
19941
19942    Returns a TREE_LIST, indicating the labels in the order that they
19943    appeared.  The TREE_VALUE of each node is a label.  */
19944
19945 static tree
19946 cp_parser_asm_label_list (cp_parser* parser)
19947 {
19948   tree labels = NULL_TREE;
19949
19950   while (true)
19951     {
19952       tree identifier, label, name;
19953
19954       /* Look for the identifier.  */
19955       identifier = cp_parser_identifier (parser);
19956       if (!error_operand_p (identifier))
19957         {
19958           label = lookup_label (identifier);
19959           if (TREE_CODE (label) == LABEL_DECL)
19960             {
19961               TREE_USED (label) = 1;
19962               check_goto (label);
19963               name = build_string (IDENTIFIER_LENGTH (identifier),
19964                                    IDENTIFIER_POINTER (identifier));
19965               labels = tree_cons (name, label, labels);
19966             }
19967         }
19968       /* If the next token is not a `,', then the list is
19969          complete.  */
19970       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19971         break;
19972       /* Consume the `,' token.  */
19973       cp_lexer_consume_token (parser->lexer);
19974     }
19975
19976   return nreverse (labels);
19977 }
19978
19979 /* Parse an (optional) series of attributes.
19980
19981    attributes:
19982      attributes attribute
19983
19984    attribute:
19985      __attribute__ (( attribute-list [opt] ))
19986
19987    The return value is as for cp_parser_attribute_list.  */
19988
19989 static tree
19990 cp_parser_attributes_opt (cp_parser* parser)
19991 {
19992   tree attributes = NULL_TREE;
19993
19994   while (true)
19995     {
19996       cp_token *token;
19997       tree attribute_list;
19998
19999       /* Peek at the next token.  */
20000       token = cp_lexer_peek_token (parser->lexer);
20001       /* If it's not `__attribute__', then we're done.  */
20002       if (token->keyword != RID_ATTRIBUTE)
20003         break;
20004
20005       /* Consume the `__attribute__' keyword.  */
20006       cp_lexer_consume_token (parser->lexer);
20007       /* Look for the two `(' tokens.  */
20008       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20009       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
20010
20011       /* Peek at the next token.  */
20012       token = cp_lexer_peek_token (parser->lexer);
20013       if (token->type != CPP_CLOSE_PAREN)
20014         /* Parse the attribute-list.  */
20015         attribute_list = cp_parser_attribute_list (parser);
20016       else
20017         /* If the next token is a `)', then there is no attribute
20018            list.  */
20019         attribute_list = NULL;
20020
20021       /* Look for the two `)' tokens.  */
20022       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20023       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
20024
20025       /* Add these new attributes to the list.  */
20026       attributes = chainon (attributes, attribute_list);
20027     }
20028
20029   return attributes;
20030 }
20031
20032 /* Parse an attribute-list.
20033
20034    attribute-list:
20035      attribute
20036      attribute-list , attribute
20037
20038    attribute:
20039      identifier
20040      identifier ( identifier )
20041      identifier ( identifier , expression-list )
20042      identifier ( expression-list )
20043
20044    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
20045    to an attribute.  The TREE_PURPOSE of each node is the identifier
20046    indicating which attribute is in use.  The TREE_VALUE represents
20047    the arguments, if any.  */
20048
20049 static tree
20050 cp_parser_attribute_list (cp_parser* parser)
20051 {
20052   tree attribute_list = NULL_TREE;
20053   bool save_translate_strings_p = parser->translate_strings_p;
20054
20055   parser->translate_strings_p = false;
20056   while (true)
20057     {
20058       cp_token *token;
20059       tree identifier;
20060       tree attribute;
20061
20062       /* Look for the identifier.  We also allow keywords here; for
20063          example `__attribute__ ((const))' is legal.  */
20064       token = cp_lexer_peek_token (parser->lexer);
20065       if (token->type == CPP_NAME
20066           || token->type == CPP_KEYWORD)
20067         {
20068           tree arguments = NULL_TREE;
20069
20070           /* Consume the token.  */
20071           token = cp_lexer_consume_token (parser->lexer);
20072
20073           /* Save away the identifier that indicates which attribute
20074              this is.  */
20075           identifier = (token->type == CPP_KEYWORD) 
20076             /* For keywords, use the canonical spelling, not the
20077                parsed identifier.  */
20078             ? ridpointers[(int) token->keyword]
20079             : token->u.value;
20080           
20081           attribute = build_tree_list (identifier, NULL_TREE);
20082
20083           /* Peek at the next token.  */
20084           token = cp_lexer_peek_token (parser->lexer);
20085           /* If it's an `(', then parse the attribute arguments.  */
20086           if (token->type == CPP_OPEN_PAREN)
20087             {
20088               VEC(tree,gc) *vec;
20089               int attr_flag = (attribute_takes_identifier_p (identifier)
20090                                ? id_attr : normal_attr);
20091               vec = cp_parser_parenthesized_expression_list
20092                     (parser, attr_flag, /*cast_p=*/false,
20093                      /*allow_expansion_p=*/false,
20094                      /*non_constant_p=*/NULL);
20095               if (vec == NULL)
20096                 arguments = error_mark_node;
20097               else
20098                 {
20099                   arguments = build_tree_list_vec (vec);
20100                   release_tree_vector (vec);
20101                 }
20102               /* Save the arguments away.  */
20103               TREE_VALUE (attribute) = arguments;
20104             }
20105
20106           if (arguments != error_mark_node)
20107             {
20108               /* Add this attribute to the list.  */
20109               TREE_CHAIN (attribute) = attribute_list;
20110               attribute_list = attribute;
20111             }
20112
20113           token = cp_lexer_peek_token (parser->lexer);
20114         }
20115       /* Now, look for more attributes.  If the next token isn't a
20116          `,', we're done.  */
20117       if (token->type != CPP_COMMA)
20118         break;
20119
20120       /* Consume the comma and keep going.  */
20121       cp_lexer_consume_token (parser->lexer);
20122     }
20123   parser->translate_strings_p = save_translate_strings_p;
20124
20125   /* We built up the list in reverse order.  */
20126   return nreverse (attribute_list);
20127 }
20128
20129 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
20130    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
20131    current value of the PEDANTIC flag, regardless of whether or not
20132    the `__extension__' keyword is present.  The caller is responsible
20133    for restoring the value of the PEDANTIC flag.  */
20134
20135 static bool
20136 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
20137 {
20138   /* Save the old value of the PEDANTIC flag.  */
20139   *saved_pedantic = pedantic;
20140
20141   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
20142     {
20143       /* Consume the `__extension__' token.  */
20144       cp_lexer_consume_token (parser->lexer);
20145       /* We're not being pedantic while the `__extension__' keyword is
20146          in effect.  */
20147       pedantic = 0;
20148
20149       return true;
20150     }
20151
20152   return false;
20153 }
20154
20155 /* Parse a label declaration.
20156
20157    label-declaration:
20158      __label__ label-declarator-seq ;
20159
20160    label-declarator-seq:
20161      identifier , label-declarator-seq
20162      identifier  */
20163
20164 static void
20165 cp_parser_label_declaration (cp_parser* parser)
20166 {
20167   /* Look for the `__label__' keyword.  */
20168   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
20169
20170   while (true)
20171     {
20172       tree identifier;
20173
20174       /* Look for an identifier.  */
20175       identifier = cp_parser_identifier (parser);
20176       /* If we failed, stop.  */
20177       if (identifier == error_mark_node)
20178         break;
20179       /* Declare it as a label.  */
20180       finish_label_decl (identifier);
20181       /* If the next token is a `;', stop.  */
20182       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
20183         break;
20184       /* Look for the `,' separating the label declarations.  */
20185       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
20186     }
20187
20188   /* Look for the final `;'.  */
20189   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
20190 }
20191
20192 /* Support Functions */
20193
20194 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20195    NAME should have one of the representations used for an
20196    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20197    is returned.  If PARSER->SCOPE is a dependent type, then a
20198    SCOPE_REF is returned.
20199
20200    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20201    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20202    was formed.  Abstractly, such entities should not be passed to this
20203    function, because they do not need to be looked up, but it is
20204    simpler to check for this special case here, rather than at the
20205    call-sites.
20206
20207    In cases not explicitly covered above, this function returns a
20208    DECL, OVERLOAD, or baselink representing the result of the lookup.
20209    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20210    is returned.
20211
20212    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20213    (e.g., "struct") that was used.  In that case bindings that do not
20214    refer to types are ignored.
20215
20216    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20217    ignored.
20218
20219    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20220    are ignored.
20221
20222    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20223    types.
20224
20225    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20226    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20227    NULL_TREE otherwise.  */
20228
20229 static tree
20230 cp_parser_lookup_name (cp_parser *parser, tree name,
20231                        enum tag_types tag_type,
20232                        bool is_template,
20233                        bool is_namespace,
20234                        bool check_dependency,
20235                        tree *ambiguous_decls,
20236                        location_t name_location)
20237 {
20238   int flags = 0;
20239   tree decl;
20240   tree object_type = parser->context->object_type;
20241
20242   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20243     flags |= LOOKUP_COMPLAIN;
20244
20245   /* Assume that the lookup will be unambiguous.  */
20246   if (ambiguous_decls)
20247     *ambiguous_decls = NULL_TREE;
20248
20249   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20250      no longer valid.  Note that if we are parsing tentatively, and
20251      the parse fails, OBJECT_TYPE will be automatically restored.  */
20252   parser->context->object_type = NULL_TREE;
20253
20254   if (name == error_mark_node)
20255     return error_mark_node;
20256
20257   /* A template-id has already been resolved; there is no lookup to
20258      do.  */
20259   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20260     return name;
20261   if (BASELINK_P (name))
20262     {
20263       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20264                   == TEMPLATE_ID_EXPR);
20265       return name;
20266     }
20267
20268   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20269      it should already have been checked to make sure that the name
20270      used matches the type being destroyed.  */
20271   if (TREE_CODE (name) == BIT_NOT_EXPR)
20272     {
20273       tree type;
20274
20275       /* Figure out to which type this destructor applies.  */
20276       if (parser->scope)
20277         type = parser->scope;
20278       else if (object_type)
20279         type = object_type;
20280       else
20281         type = current_class_type;
20282       /* If that's not a class type, there is no destructor.  */
20283       if (!type || !CLASS_TYPE_P (type))
20284         return error_mark_node;
20285       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20286         lazily_declare_fn (sfk_destructor, type);
20287       if (!CLASSTYPE_DESTRUCTORS (type))
20288           return error_mark_node;
20289       /* If it was a class type, return the destructor.  */
20290       return CLASSTYPE_DESTRUCTORS (type);
20291     }
20292
20293   /* By this point, the NAME should be an ordinary identifier.  If
20294      the id-expression was a qualified name, the qualifying scope is
20295      stored in PARSER->SCOPE at this point.  */
20296   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20297
20298   /* Perform the lookup.  */
20299   if (parser->scope)
20300     {
20301       bool dependent_p;
20302
20303       if (parser->scope == error_mark_node)
20304         return error_mark_node;
20305
20306       /* If the SCOPE is dependent, the lookup must be deferred until
20307          the template is instantiated -- unless we are explicitly
20308          looking up names in uninstantiated templates.  Even then, we
20309          cannot look up the name if the scope is not a class type; it
20310          might, for example, be a template type parameter.  */
20311       dependent_p = (TYPE_P (parser->scope)
20312                      && dependent_scope_p (parser->scope));
20313       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20314           && dependent_p)
20315         /* Defer lookup.  */
20316         decl = error_mark_node;
20317       else
20318         {
20319           tree pushed_scope = NULL_TREE;
20320
20321           /* If PARSER->SCOPE is a dependent type, then it must be a
20322              class type, and we must not be checking dependencies;
20323              otherwise, we would have processed this lookup above.  So
20324              that PARSER->SCOPE is not considered a dependent base by
20325              lookup_member, we must enter the scope here.  */
20326           if (dependent_p)
20327             pushed_scope = push_scope (parser->scope);
20328
20329           /* If the PARSER->SCOPE is a template specialization, it
20330              may be instantiated during name lookup.  In that case,
20331              errors may be issued.  Even if we rollback the current
20332              tentative parse, those errors are valid.  */
20333           decl = lookup_qualified_name (parser->scope, name,
20334                                         tag_type != none_type,
20335                                         /*complain=*/true);
20336
20337           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20338              lookup result and the nested-name-specifier nominates a class C:
20339                * if the name specified after the nested-name-specifier, when
20340                looked up in C, is the injected-class-name of C (Clause 9), or
20341                * if the name specified after the nested-name-specifier is the
20342                same as the identifier or the simple-template-id's template-
20343                name in the last component of the nested-name-specifier,
20344              the name is instead considered to name the constructor of
20345              class C. [ Note: for example, the constructor is not an
20346              acceptable lookup result in an elaborated-type-specifier so
20347              the constructor would not be used in place of the
20348              injected-class-name. --end note ] Such a constructor name
20349              shall be used only in the declarator-id of a declaration that
20350              names a constructor or in a using-declaration.  */
20351           if (tag_type == none_type
20352               && DECL_SELF_REFERENCE_P (decl)
20353               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20354             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20355                                           tag_type != none_type,
20356                                           /*complain=*/true);
20357
20358           /* If we have a single function from a using decl, pull it out.  */
20359           if (TREE_CODE (decl) == OVERLOAD
20360               && !really_overloaded_fn (decl))
20361             decl = OVL_FUNCTION (decl);
20362
20363           if (pushed_scope)
20364             pop_scope (pushed_scope);
20365         }
20366
20367       /* If the scope is a dependent type and either we deferred lookup or
20368          we did lookup but didn't find the name, rememeber the name.  */
20369       if (decl == error_mark_node && TYPE_P (parser->scope)
20370           && dependent_type_p (parser->scope))
20371         {
20372           if (tag_type)
20373             {
20374               tree type;
20375
20376               /* The resolution to Core Issue 180 says that `struct
20377                  A::B' should be considered a type-name, even if `A'
20378                  is dependent.  */
20379               type = make_typename_type (parser->scope, name, tag_type,
20380                                          /*complain=*/tf_error);
20381               decl = TYPE_NAME (type);
20382             }
20383           else if (is_template
20384                    && (cp_parser_next_token_ends_template_argument_p (parser)
20385                        || cp_lexer_next_token_is (parser->lexer,
20386                                                   CPP_CLOSE_PAREN)))
20387             decl = make_unbound_class_template (parser->scope,
20388                                                 name, NULL_TREE,
20389                                                 /*complain=*/tf_error);
20390           else
20391             decl = build_qualified_name (/*type=*/NULL_TREE,
20392                                          parser->scope, name,
20393                                          is_template);
20394         }
20395       parser->qualifying_scope = parser->scope;
20396       parser->object_scope = NULL_TREE;
20397     }
20398   else if (object_type)
20399     {
20400       tree object_decl = NULL_TREE;
20401       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20402          OBJECT_TYPE is not a class.  */
20403       if (CLASS_TYPE_P (object_type))
20404         /* If the OBJECT_TYPE is a template specialization, it may
20405            be instantiated during name lookup.  In that case, errors
20406            may be issued.  Even if we rollback the current tentative
20407            parse, those errors are valid.  */
20408         object_decl = lookup_member (object_type,
20409                                      name,
20410                                      /*protect=*/0,
20411                                      tag_type != none_type,
20412                                      tf_warning_or_error);
20413       /* Look it up in the enclosing context, too.  */
20414       decl = lookup_name_real (name, tag_type != none_type,
20415                                /*nonclass=*/0,
20416                                /*block_p=*/true, is_namespace, flags);
20417       parser->object_scope = object_type;
20418       parser->qualifying_scope = NULL_TREE;
20419       if (object_decl)
20420         decl = object_decl;
20421     }
20422   else
20423     {
20424       decl = lookup_name_real (name, tag_type != none_type,
20425                                /*nonclass=*/0,
20426                                /*block_p=*/true, is_namespace, flags);
20427       parser->qualifying_scope = NULL_TREE;
20428       parser->object_scope = NULL_TREE;
20429     }
20430
20431   /* If the lookup failed, let our caller know.  */
20432   if (!decl || decl == error_mark_node)
20433     return error_mark_node;
20434
20435   /* Pull out the template from an injected-class-name (or multiple).  */
20436   if (is_template)
20437     decl = maybe_get_template_decl_from_type_decl (decl);
20438
20439   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20440   if (TREE_CODE (decl) == TREE_LIST)
20441     {
20442       if (ambiguous_decls)
20443         *ambiguous_decls = decl;
20444       /* The error message we have to print is too complicated for
20445          cp_parser_error, so we incorporate its actions directly.  */
20446       if (!cp_parser_simulate_error (parser))
20447         {
20448           error_at (name_location, "reference to %qD is ambiguous",
20449                     name);
20450           print_candidates (decl);
20451         }
20452       return error_mark_node;
20453     }
20454
20455   gcc_assert (DECL_P (decl)
20456               || TREE_CODE (decl) == OVERLOAD
20457               || TREE_CODE (decl) == SCOPE_REF
20458               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20459               || BASELINK_P (decl));
20460
20461   /* If we have resolved the name of a member declaration, check to
20462      see if the declaration is accessible.  When the name resolves to
20463      set of overloaded functions, accessibility is checked when
20464      overload resolution is done.
20465
20466      During an explicit instantiation, access is not checked at all,
20467      as per [temp.explicit].  */
20468   if (DECL_P (decl))
20469     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20470
20471   maybe_record_typedef_use (decl);
20472
20473   return decl;
20474 }
20475
20476 /* Like cp_parser_lookup_name, but for use in the typical case where
20477    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20478    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20479
20480 static tree
20481 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20482 {
20483   return cp_parser_lookup_name (parser, name,
20484                                 none_type,
20485                                 /*is_template=*/false,
20486                                 /*is_namespace=*/false,
20487                                 /*check_dependency=*/true,
20488                                 /*ambiguous_decls=*/NULL,
20489                                 location);
20490 }
20491
20492 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20493    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20494    true, the DECL indicates the class being defined in a class-head,
20495    or declared in an elaborated-type-specifier.
20496
20497    Otherwise, return DECL.  */
20498
20499 static tree
20500 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20501 {
20502   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20503      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20504
20505        struct A {
20506          template <typename T> struct B;
20507        };
20508
20509        template <typename T> struct A::B {};
20510
20511      Similarly, in an elaborated-type-specifier:
20512
20513        namespace N { struct X{}; }
20514
20515        struct A {
20516          template <typename T> friend struct N::X;
20517        };
20518
20519      However, if the DECL refers to a class type, and we are in
20520      the scope of the class, then the name lookup automatically
20521      finds the TYPE_DECL created by build_self_reference rather
20522      than a TEMPLATE_DECL.  For example, in:
20523
20524        template <class T> struct S {
20525          S s;
20526        };
20527
20528      there is no need to handle such case.  */
20529
20530   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20531     return DECL_TEMPLATE_RESULT (decl);
20532
20533   return decl;
20534 }
20535
20536 /* If too many, or too few, template-parameter lists apply to the
20537    declarator, issue an error message.  Returns TRUE if all went well,
20538    and FALSE otherwise.  */
20539
20540 static bool
20541 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20542                                                 cp_declarator *declarator,
20543                                                 location_t declarator_location)
20544 {
20545   unsigned num_templates;
20546
20547   /* We haven't seen any classes that involve template parameters yet.  */
20548   num_templates = 0;
20549
20550   switch (declarator->kind)
20551     {
20552     case cdk_id:
20553       if (declarator->u.id.qualifying_scope)
20554         {
20555           tree scope;
20556
20557           scope = declarator->u.id.qualifying_scope;
20558
20559           while (scope && CLASS_TYPE_P (scope))
20560             {
20561               /* You're supposed to have one `template <...>'
20562                  for every template class, but you don't need one
20563                  for a full specialization.  For example:
20564
20565                  template <class T> struct S{};
20566                  template <> struct S<int> { void f(); };
20567                  void S<int>::f () {}
20568
20569                  is correct; there shouldn't be a `template <>' for
20570                  the definition of `S<int>::f'.  */
20571               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20572                 /* If SCOPE does not have template information of any
20573                    kind, then it is not a template, nor is it nested
20574                    within a template.  */
20575                 break;
20576               if (explicit_class_specialization_p (scope))
20577                 break;
20578               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20579                 ++num_templates;
20580
20581               scope = TYPE_CONTEXT (scope);
20582             }
20583         }
20584       else if (TREE_CODE (declarator->u.id.unqualified_name)
20585                == TEMPLATE_ID_EXPR)
20586         /* If the DECLARATOR has the form `X<y>' then it uses one
20587            additional level of template parameters.  */
20588         ++num_templates;
20589
20590       return cp_parser_check_template_parameters 
20591         (parser, num_templates, declarator_location, declarator);
20592
20593
20594     case cdk_function:
20595     case cdk_array:
20596     case cdk_pointer:
20597     case cdk_reference:
20598     case cdk_ptrmem:
20599       return (cp_parser_check_declarator_template_parameters
20600               (parser, declarator->declarator, declarator_location));
20601
20602     case cdk_error:
20603       return true;
20604
20605     default:
20606       gcc_unreachable ();
20607     }
20608   return false;
20609 }
20610
20611 /* NUM_TEMPLATES were used in the current declaration.  If that is
20612    invalid, return FALSE and issue an error messages.  Otherwise,
20613    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20614    declarator and we can print more accurate diagnostics.  */
20615
20616 static bool
20617 cp_parser_check_template_parameters (cp_parser* parser,
20618                                      unsigned num_templates,
20619                                      location_t location,
20620                                      cp_declarator *declarator)
20621 {
20622   /* If there are the same number of template classes and parameter
20623      lists, that's OK.  */
20624   if (parser->num_template_parameter_lists == num_templates)
20625     return true;
20626   /* If there are more, but only one more, then we are referring to a
20627      member template.  That's OK too.  */
20628   if (parser->num_template_parameter_lists == num_templates + 1)
20629     return true;
20630   /* If there are more template classes than parameter lists, we have
20631      something like:
20632
20633        template <class T> void S<T>::R<T>::f ();  */
20634   if (parser->num_template_parameter_lists < num_templates)
20635     {
20636       if (declarator && !current_function_decl)
20637         error_at (location, "specializing member %<%T::%E%> "
20638                   "requires %<template<>%> syntax", 
20639                   declarator->u.id.qualifying_scope,
20640                   declarator->u.id.unqualified_name);
20641       else if (declarator)
20642         error_at (location, "invalid declaration of %<%T::%E%>",
20643                   declarator->u.id.qualifying_scope,
20644                   declarator->u.id.unqualified_name);
20645       else 
20646         error_at (location, "too few template-parameter-lists");
20647       return false;
20648     }
20649   /* Otherwise, there are too many template parameter lists.  We have
20650      something like:
20651
20652      template <class T> template <class U> void S::f();  */
20653   error_at (location, "too many template-parameter-lists");
20654   return false;
20655 }
20656
20657 /* Parse an optional `::' token indicating that the following name is
20658    from the global namespace.  If so, PARSER->SCOPE is set to the
20659    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20660    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20661    Returns the new value of PARSER->SCOPE, if the `::' token is
20662    present, and NULL_TREE otherwise.  */
20663
20664 static tree
20665 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20666 {
20667   cp_token *token;
20668
20669   /* Peek at the next token.  */
20670   token = cp_lexer_peek_token (parser->lexer);
20671   /* If we're looking at a `::' token then we're starting from the
20672      global namespace, not our current location.  */
20673   if (token->type == CPP_SCOPE)
20674     {
20675       /* Consume the `::' token.  */
20676       cp_lexer_consume_token (parser->lexer);
20677       /* Set the SCOPE so that we know where to start the lookup.  */
20678       parser->scope = global_namespace;
20679       parser->qualifying_scope = global_namespace;
20680       parser->object_scope = NULL_TREE;
20681
20682       return parser->scope;
20683     }
20684   else if (!current_scope_valid_p)
20685     {
20686       parser->scope = NULL_TREE;
20687       parser->qualifying_scope = NULL_TREE;
20688       parser->object_scope = NULL_TREE;
20689     }
20690
20691   return NULL_TREE;
20692 }
20693
20694 /* Returns TRUE if the upcoming token sequence is the start of a
20695    constructor declarator.  If FRIEND_P is true, the declarator is
20696    preceded by the `friend' specifier.  */
20697
20698 static bool
20699 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20700 {
20701   bool constructor_p;
20702   tree nested_name_specifier;
20703   cp_token *next_token;
20704
20705   /* The common case is that this is not a constructor declarator, so
20706      try to avoid doing lots of work if at all possible.  It's not
20707      valid declare a constructor at function scope.  */
20708   if (parser->in_function_body)
20709     return false;
20710   /* And only certain tokens can begin a constructor declarator.  */
20711   next_token = cp_lexer_peek_token (parser->lexer);
20712   if (next_token->type != CPP_NAME
20713       && next_token->type != CPP_SCOPE
20714       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20715       && next_token->type != CPP_TEMPLATE_ID)
20716     return false;
20717
20718   /* Parse tentatively; we are going to roll back all of the tokens
20719      consumed here.  */
20720   cp_parser_parse_tentatively (parser);
20721   /* Assume that we are looking at a constructor declarator.  */
20722   constructor_p = true;
20723
20724   /* Look for the optional `::' operator.  */
20725   cp_parser_global_scope_opt (parser,
20726                               /*current_scope_valid_p=*/false);
20727   /* Look for the nested-name-specifier.  */
20728   nested_name_specifier
20729     = (cp_parser_nested_name_specifier_opt (parser,
20730                                             /*typename_keyword_p=*/false,
20731                                             /*check_dependency_p=*/false,
20732                                             /*type_p=*/false,
20733                                             /*is_declaration=*/false));
20734   /* Outside of a class-specifier, there must be a
20735      nested-name-specifier.  */
20736   if (!nested_name_specifier &&
20737       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20738        || friend_p))
20739     constructor_p = false;
20740   else if (nested_name_specifier == error_mark_node)
20741     constructor_p = false;
20742
20743   /* If we have a class scope, this is easy; DR 147 says that S::S always
20744      names the constructor, and no other qualified name could.  */
20745   if (constructor_p && nested_name_specifier
20746       && CLASS_TYPE_P (nested_name_specifier))
20747     {
20748       tree id = cp_parser_unqualified_id (parser,
20749                                           /*template_keyword_p=*/false,
20750                                           /*check_dependency_p=*/false,
20751                                           /*declarator_p=*/true,
20752                                           /*optional_p=*/false);
20753       if (is_overloaded_fn (id))
20754         id = DECL_NAME (get_first_fn (id));
20755       if (!constructor_name_p (id, nested_name_specifier))
20756         constructor_p = false;
20757     }
20758   /* If we still think that this might be a constructor-declarator,
20759      look for a class-name.  */
20760   else if (constructor_p)
20761     {
20762       /* If we have:
20763
20764            template <typename T> struct S {
20765              S();
20766            };
20767
20768          we must recognize that the nested `S' names a class.  */
20769       tree type_decl;
20770       type_decl = cp_parser_class_name (parser,
20771                                         /*typename_keyword_p=*/false,
20772                                         /*template_keyword_p=*/false,
20773                                         none_type,
20774                                         /*check_dependency_p=*/false,
20775                                         /*class_head_p=*/false,
20776                                         /*is_declaration=*/false);
20777       /* If there was no class-name, then this is not a constructor.  */
20778       constructor_p = !cp_parser_error_occurred (parser);
20779
20780       /* If we're still considering a constructor, we have to see a `(',
20781          to begin the parameter-declaration-clause, followed by either a
20782          `)', an `...', or a decl-specifier.  We need to check for a
20783          type-specifier to avoid being fooled into thinking that:
20784
20785            S (f) (int);
20786
20787          is a constructor.  (It is actually a function named `f' that
20788          takes one parameter (of type `int') and returns a value of type
20789          `S'.  */
20790       if (constructor_p
20791           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20792         constructor_p = false;
20793
20794       if (constructor_p
20795           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20796           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20797           /* A parameter declaration begins with a decl-specifier,
20798              which is either the "attribute" keyword, a storage class
20799              specifier, or (usually) a type-specifier.  */
20800           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20801         {
20802           tree type;
20803           tree pushed_scope = NULL_TREE;
20804           unsigned saved_num_template_parameter_lists;
20805
20806           /* Names appearing in the type-specifier should be looked up
20807              in the scope of the class.  */
20808           if (current_class_type)
20809             type = NULL_TREE;
20810           else
20811             {
20812               type = TREE_TYPE (type_decl);
20813               if (TREE_CODE (type) == TYPENAME_TYPE)
20814                 {
20815                   type = resolve_typename_type (type,
20816                                                 /*only_current_p=*/false);
20817                   if (TREE_CODE (type) == TYPENAME_TYPE)
20818                     {
20819                       cp_parser_abort_tentative_parse (parser);
20820                       return false;
20821                     }
20822                 }
20823               pushed_scope = push_scope (type);
20824             }
20825
20826           /* Inside the constructor parameter list, surrounding
20827              template-parameter-lists do not apply.  */
20828           saved_num_template_parameter_lists
20829             = parser->num_template_parameter_lists;
20830           parser->num_template_parameter_lists = 0;
20831
20832           /* Look for the type-specifier.  */
20833           cp_parser_type_specifier (parser,
20834                                     CP_PARSER_FLAGS_NONE,
20835                                     /*decl_specs=*/NULL,
20836                                     /*is_declarator=*/true,
20837                                     /*declares_class_or_enum=*/NULL,
20838                                     /*is_cv_qualifier=*/NULL);
20839
20840           parser->num_template_parameter_lists
20841             = saved_num_template_parameter_lists;
20842
20843           /* Leave the scope of the class.  */
20844           if (pushed_scope)
20845             pop_scope (pushed_scope);
20846
20847           constructor_p = !cp_parser_error_occurred (parser);
20848         }
20849     }
20850
20851   /* We did not really want to consume any tokens.  */
20852   cp_parser_abort_tentative_parse (parser);
20853
20854   return constructor_p;
20855 }
20856
20857 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20858    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20859    they must be performed once we are in the scope of the function.
20860
20861    Returns the function defined.  */
20862
20863 static tree
20864 cp_parser_function_definition_from_specifiers_and_declarator
20865   (cp_parser* parser,
20866    cp_decl_specifier_seq *decl_specifiers,
20867    tree attributes,
20868    const cp_declarator *declarator)
20869 {
20870   tree fn;
20871   bool success_p;
20872
20873   /* Begin the function-definition.  */
20874   success_p = start_function (decl_specifiers, declarator, attributes);
20875
20876   /* The things we're about to see are not directly qualified by any
20877      template headers we've seen thus far.  */
20878   reset_specialization ();
20879
20880   /* If there were names looked up in the decl-specifier-seq that we
20881      did not check, check them now.  We must wait until we are in the
20882      scope of the function to perform the checks, since the function
20883      might be a friend.  */
20884   perform_deferred_access_checks ();
20885
20886   if (!success_p)
20887     {
20888       /* Skip the entire function.  */
20889       cp_parser_skip_to_end_of_block_or_statement (parser);
20890       fn = error_mark_node;
20891     }
20892   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20893     {
20894       /* Seen already, skip it.  An error message has already been output.  */
20895       cp_parser_skip_to_end_of_block_or_statement (parser);
20896       fn = current_function_decl;
20897       current_function_decl = NULL_TREE;
20898       /* If this is a function from a class, pop the nested class.  */
20899       if (current_class_name)
20900         pop_nested_class ();
20901     }
20902   else
20903     {
20904       timevar_id_t tv;
20905       if (DECL_DECLARED_INLINE_P (current_function_decl))
20906         tv = TV_PARSE_INLINE;
20907       else
20908         tv = TV_PARSE_FUNC;
20909       timevar_push (tv);
20910       fn = cp_parser_function_definition_after_declarator (parser,
20911                                                          /*inline_p=*/false);
20912       timevar_pop (tv);
20913     }
20914
20915   return fn;
20916 }
20917
20918 /* Parse the part of a function-definition that follows the
20919    declarator.  INLINE_P is TRUE iff this function is an inline
20920    function defined within a class-specifier.
20921
20922    Returns the function defined.  */
20923
20924 static tree
20925 cp_parser_function_definition_after_declarator (cp_parser* parser,
20926                                                 bool inline_p)
20927 {
20928   tree fn;
20929   bool ctor_initializer_p = false;
20930   bool saved_in_unbraced_linkage_specification_p;
20931   bool saved_in_function_body;
20932   unsigned saved_num_template_parameter_lists;
20933   cp_token *token;
20934
20935   saved_in_function_body = parser->in_function_body;
20936   parser->in_function_body = true;
20937   /* If the next token is `return', then the code may be trying to
20938      make use of the "named return value" extension that G++ used to
20939      support.  */
20940   token = cp_lexer_peek_token (parser->lexer);
20941   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20942     {
20943       /* Consume the `return' keyword.  */
20944       cp_lexer_consume_token (parser->lexer);
20945       /* Look for the identifier that indicates what value is to be
20946          returned.  */
20947       cp_parser_identifier (parser);
20948       /* Issue an error message.  */
20949       error_at (token->location,
20950                 "named return values are no longer supported");
20951       /* Skip tokens until we reach the start of the function body.  */
20952       while (true)
20953         {
20954           cp_token *token = cp_lexer_peek_token (parser->lexer);
20955           if (token->type == CPP_OPEN_BRACE
20956               || token->type == CPP_EOF
20957               || token->type == CPP_PRAGMA_EOL)
20958             break;
20959           cp_lexer_consume_token (parser->lexer);
20960         }
20961     }
20962   /* The `extern' in `extern "C" void f () { ... }' does not apply to
20963      anything declared inside `f'.  */
20964   saved_in_unbraced_linkage_specification_p
20965     = parser->in_unbraced_linkage_specification_p;
20966   parser->in_unbraced_linkage_specification_p = false;
20967   /* Inside the function, surrounding template-parameter-lists do not
20968      apply.  */
20969   saved_num_template_parameter_lists
20970     = parser->num_template_parameter_lists;
20971   parser->num_template_parameter_lists = 0;
20972
20973   start_lambda_scope (current_function_decl);
20974
20975   /* If the next token is `try', `__transaction_atomic', or
20976      `__transaction_relaxed`, then we are looking at either function-try-block
20977      or function-transaction-block.  Note that all of these include the
20978      function-body.  */
20979   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRANSACTION_ATOMIC))
20980     ctor_initializer_p = cp_parser_function_transaction (parser,
20981         RID_TRANSACTION_ATOMIC);
20982   else if (cp_lexer_next_token_is_keyword (parser->lexer,
20983       RID_TRANSACTION_RELAXED))
20984     ctor_initializer_p = cp_parser_function_transaction (parser,
20985         RID_TRANSACTION_RELAXED);
20986   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20987     ctor_initializer_p = cp_parser_function_try_block (parser);
20988   else
20989     ctor_initializer_p
20990       = cp_parser_ctor_initializer_opt_and_function_body (parser);
20991
20992   finish_lambda_scope ();
20993
20994   /* Finish the function.  */
20995   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20996                         (inline_p ? 2 : 0));
20997   /* Generate code for it, if necessary.  */
20998   expand_or_defer_fn (fn);
20999   /* Restore the saved values.  */
21000   parser->in_unbraced_linkage_specification_p
21001     = saved_in_unbraced_linkage_specification_p;
21002   parser->num_template_parameter_lists
21003     = saved_num_template_parameter_lists;
21004   parser->in_function_body = saved_in_function_body;
21005
21006   return fn;
21007 }
21008
21009 /* Parse a template-declaration, assuming that the `export' (and
21010    `extern') keywords, if present, has already been scanned.  MEMBER_P
21011    is as for cp_parser_template_declaration.  */
21012
21013 static void
21014 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
21015 {
21016   tree decl = NULL_TREE;
21017   VEC (deferred_access_check,gc) *checks;
21018   tree parameter_list;
21019   bool friend_p = false;
21020   bool need_lang_pop;
21021   cp_token *token;
21022
21023   /* Look for the `template' keyword.  */
21024   token = cp_lexer_peek_token (parser->lexer);
21025   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
21026     return;
21027
21028   /* And the `<'.  */
21029   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
21030     return;
21031   if (at_class_scope_p () && current_function_decl)
21032     {
21033       /* 14.5.2.2 [temp.mem]
21034
21035          A local class shall not have member templates.  */
21036       error_at (token->location,
21037                 "invalid declaration of member template in local class");
21038       cp_parser_skip_to_end_of_block_or_statement (parser);
21039       return;
21040     }
21041   /* [temp]
21042
21043      A template ... shall not have C linkage.  */
21044   if (current_lang_name == lang_name_c)
21045     {
21046       error_at (token->location, "template with C linkage");
21047       /* Give it C++ linkage to avoid confusing other parts of the
21048          front end.  */
21049       push_lang_context (lang_name_cplusplus);
21050       need_lang_pop = true;
21051     }
21052   else
21053     need_lang_pop = false;
21054
21055   /* We cannot perform access checks on the template parameter
21056      declarations until we know what is being declared, just as we
21057      cannot check the decl-specifier list.  */
21058   push_deferring_access_checks (dk_deferred);
21059
21060   /* If the next token is `>', then we have an invalid
21061      specialization.  Rather than complain about an invalid template
21062      parameter, issue an error message here.  */
21063   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
21064     {
21065       cp_parser_error (parser, "invalid explicit specialization");
21066       begin_specialization ();
21067       parameter_list = NULL_TREE;
21068     }
21069   else
21070     {
21071       /* Parse the template parameters.  */
21072       parameter_list = cp_parser_template_parameter_list (parser);
21073       fixup_template_parms ();
21074     }
21075
21076   /* Get the deferred access checks from the parameter list.  These
21077      will be checked once we know what is being declared, as for a
21078      member template the checks must be performed in the scope of the
21079      class containing the member.  */
21080   checks = get_deferred_access_checks ();
21081
21082   /* Look for the `>'.  */
21083   cp_parser_skip_to_end_of_template_parameter_list (parser);
21084   /* We just processed one more parameter list.  */
21085   ++parser->num_template_parameter_lists;
21086   /* If the next token is `template', there are more template
21087      parameters.  */
21088   if (cp_lexer_next_token_is_keyword (parser->lexer,
21089                                       RID_TEMPLATE))
21090     cp_parser_template_declaration_after_export (parser, member_p);
21091   else if (cxx_dialect >= cxx0x
21092            && cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
21093     decl = cp_parser_alias_declaration (parser);
21094   else
21095     {
21096       /* There are no access checks when parsing a template, as we do not
21097          know if a specialization will be a friend.  */
21098       push_deferring_access_checks (dk_no_check);
21099       token = cp_lexer_peek_token (parser->lexer);
21100       decl = cp_parser_single_declaration (parser,
21101                                            checks,
21102                                            member_p,
21103                                            /*explicit_specialization_p=*/false,
21104                                            &friend_p);
21105       pop_deferring_access_checks ();
21106
21107       /* If this is a member template declaration, let the front
21108          end know.  */
21109       if (member_p && !friend_p && decl)
21110         {
21111           if (TREE_CODE (decl) == TYPE_DECL)
21112             cp_parser_check_access_in_redeclaration (decl, token->location);
21113
21114           decl = finish_member_template_decl (decl);
21115         }
21116       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
21117         make_friend_class (current_class_type, TREE_TYPE (decl),
21118                            /*complain=*/true);
21119     }
21120   /* We are done with the current parameter list.  */
21121   --parser->num_template_parameter_lists;
21122
21123   pop_deferring_access_checks ();
21124
21125   /* Finish up.  */
21126   finish_template_decl (parameter_list);
21127
21128   /* Check the template arguments for a literal operator template.  */
21129   if (decl
21130       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
21131       && UDLIT_OPER_P (DECL_NAME (decl)))
21132     {
21133       bool ok = true;
21134       if (parameter_list == NULL_TREE)
21135         ok = false;
21136       else
21137         {
21138           int num_parms = TREE_VEC_LENGTH (parameter_list);
21139           if (num_parms != 1)
21140             ok = false;
21141           else
21142             {
21143               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
21144               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
21145               if (TREE_TYPE (parm) != char_type_node
21146                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
21147                 ok = false;
21148             }
21149         }
21150       if (!ok)
21151         error ("literal operator template %qD has invalid parameter list."
21152                "  Expected non-type template argument pack <char...>",
21153                decl);
21154     }
21155   /* Register member declarations.  */
21156   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
21157     finish_member_declaration (decl);
21158   /* For the erroneous case of a template with C linkage, we pushed an
21159      implicit C++ linkage scope; exit that scope now.  */
21160   if (need_lang_pop)
21161     pop_lang_context ();
21162   /* If DECL is a function template, we must return to parse it later.
21163      (Even though there is no definition, there might be default
21164      arguments that need handling.)  */
21165   if (member_p && decl
21166       && (TREE_CODE (decl) == FUNCTION_DECL
21167           || DECL_FUNCTION_TEMPLATE_P (decl)))
21168     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
21169 }
21170
21171 /* Perform the deferred access checks from a template-parameter-list.
21172    CHECKS is a TREE_LIST of access checks, as returned by
21173    get_deferred_access_checks.  */
21174
21175 static void
21176 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
21177 {
21178   ++processing_template_parmlist;
21179   perform_access_checks (checks);
21180   --processing_template_parmlist;
21181 }
21182
21183 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
21184    `function-definition' sequence.  MEMBER_P is true, this declaration
21185    appears in a class scope.
21186
21187    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
21188    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
21189
21190 static tree
21191 cp_parser_single_declaration (cp_parser* parser,
21192                               VEC (deferred_access_check,gc)* checks,
21193                               bool member_p,
21194                               bool explicit_specialization_p,
21195                               bool* friend_p)
21196 {
21197   int declares_class_or_enum;
21198   tree decl = NULL_TREE;
21199   cp_decl_specifier_seq decl_specifiers;
21200   bool function_definition_p = false;
21201   cp_token *decl_spec_token_start;
21202
21203   /* This function is only used when processing a template
21204      declaration.  */
21205   gcc_assert (innermost_scope_kind () == sk_template_parms
21206               || innermost_scope_kind () == sk_template_spec);
21207
21208   /* Defer access checks until we know what is being declared.  */
21209   push_deferring_access_checks (dk_deferred);
21210
21211   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21212      alternative.  */
21213   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21214   cp_parser_decl_specifier_seq (parser,
21215                                 CP_PARSER_FLAGS_OPTIONAL,
21216                                 &decl_specifiers,
21217                                 &declares_class_or_enum);
21218   if (friend_p)
21219     *friend_p = cp_parser_friend_p (&decl_specifiers);
21220
21221   /* There are no template typedefs.  */
21222   if (decl_specifiers.specs[(int) ds_typedef])
21223     {
21224       error_at (decl_spec_token_start->location,
21225                 "template declaration of %<typedef%>");
21226       decl = error_mark_node;
21227     }
21228
21229   /* Gather up the access checks that occurred the
21230      decl-specifier-seq.  */
21231   stop_deferring_access_checks ();
21232
21233   /* Check for the declaration of a template class.  */
21234   if (declares_class_or_enum)
21235     {
21236       if (cp_parser_declares_only_class_p (parser))
21237         {
21238           decl = shadow_tag (&decl_specifiers);
21239
21240           /* In this case:
21241
21242                struct C {
21243                  friend template <typename T> struct A<T>::B;
21244                };
21245
21246              A<T>::B will be represented by a TYPENAME_TYPE, and
21247              therefore not recognized by shadow_tag.  */
21248           if (friend_p && *friend_p
21249               && !decl
21250               && decl_specifiers.type
21251               && TYPE_P (decl_specifiers.type))
21252             decl = decl_specifiers.type;
21253
21254           if (decl && decl != error_mark_node)
21255             decl = TYPE_NAME (decl);
21256           else
21257             decl = error_mark_node;
21258
21259           /* Perform access checks for template parameters.  */
21260           cp_parser_perform_template_parameter_access_checks (checks);
21261         }
21262     }
21263
21264   /* Complain about missing 'typename' or other invalid type names.  */
21265   if (!decl_specifiers.any_type_specifiers_p
21266       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21267     {
21268       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21269          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21270          the rest of this declaration.  */
21271       decl = error_mark_node;
21272       goto out;
21273     }
21274
21275   /* If it's not a template class, try for a template function.  If
21276      the next token is a `;', then this declaration does not declare
21277      anything.  But, if there were errors in the decl-specifiers, then
21278      the error might well have come from an attempted class-specifier.
21279      In that case, there's no need to warn about a missing declarator.  */
21280   if (!decl
21281       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21282           || decl_specifiers.type != error_mark_node))
21283     {
21284       decl = cp_parser_init_declarator (parser,
21285                                         &decl_specifiers,
21286                                         checks,
21287                                         /*function_definition_allowed_p=*/true,
21288                                         member_p,
21289                                         declares_class_or_enum,
21290                                         &function_definition_p,
21291                                         NULL);
21292
21293     /* 7.1.1-1 [dcl.stc]
21294
21295        A storage-class-specifier shall not be specified in an explicit
21296        specialization...  */
21297     if (decl
21298         && explicit_specialization_p
21299         && decl_specifiers.storage_class != sc_none)
21300       {
21301         error_at (decl_spec_token_start->location,
21302                   "explicit template specialization cannot have a storage class");
21303         decl = error_mark_node;
21304       }
21305     }
21306
21307   /* Look for a trailing `;' after the declaration.  */
21308   if (!function_definition_p
21309       && (decl == error_mark_node
21310           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21311     cp_parser_skip_to_end_of_block_or_statement (parser);
21312
21313  out:
21314   pop_deferring_access_checks ();
21315
21316   /* Clear any current qualification; whatever comes next is the start
21317      of something new.  */
21318   parser->scope = NULL_TREE;
21319   parser->qualifying_scope = NULL_TREE;
21320   parser->object_scope = NULL_TREE;
21321
21322   return decl;
21323 }
21324
21325 /* Parse a cast-expression that is not the operand of a unary "&".  */
21326
21327 static tree
21328 cp_parser_simple_cast_expression (cp_parser *parser)
21329 {
21330   return cp_parser_cast_expression (parser, /*address_p=*/false,
21331                                     /*cast_p=*/false, NULL);
21332 }
21333
21334 /* Parse a functional cast to TYPE.  Returns an expression
21335    representing the cast.  */
21336
21337 static tree
21338 cp_parser_functional_cast (cp_parser* parser, tree type)
21339 {
21340   VEC(tree,gc) *vec;
21341   tree expression_list;
21342   tree cast;
21343   bool nonconst_p;
21344
21345   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21346     {
21347       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21348       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21349       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21350       if (TREE_CODE (type) == TYPE_DECL)
21351         type = TREE_TYPE (type);
21352       return finish_compound_literal (type, expression_list,
21353                                       tf_warning_or_error);
21354     }
21355
21356
21357   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21358                                                  /*cast_p=*/true,
21359                                                  /*allow_expansion_p=*/true,
21360                                                  /*non_constant_p=*/NULL);
21361   if (vec == NULL)
21362     expression_list = error_mark_node;
21363   else
21364     {
21365       expression_list = build_tree_list_vec (vec);
21366       release_tree_vector (vec);
21367     }
21368
21369   cast = build_functional_cast (type, expression_list,
21370                                 tf_warning_or_error);
21371   /* [expr.const]/1: In an integral constant expression "only type
21372      conversions to integral or enumeration type can be used".  */
21373   if (TREE_CODE (type) == TYPE_DECL)
21374     type = TREE_TYPE (type);
21375   if (cast != error_mark_node
21376       && !cast_valid_in_integral_constant_expression_p (type)
21377       && cp_parser_non_integral_constant_expression (parser,
21378                                                      NIC_CONSTRUCTOR))
21379     return error_mark_node;
21380   return cast;
21381 }
21382
21383 /* Save the tokens that make up the body of a member function defined
21384    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21385    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21386    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21387    for the member function.  */
21388
21389 static tree
21390 cp_parser_save_member_function_body (cp_parser* parser,
21391                                      cp_decl_specifier_seq *decl_specifiers,
21392                                      cp_declarator *declarator,
21393                                      tree attributes)
21394 {
21395   cp_token *first;
21396   cp_token *last;
21397   tree fn;
21398
21399   /* Create the FUNCTION_DECL.  */
21400   fn = grokmethod (decl_specifiers, declarator, attributes);
21401   /* If something went badly wrong, bail out now.  */
21402   if (fn == error_mark_node)
21403     {
21404       /* If there's a function-body, skip it.  */
21405       if (cp_parser_token_starts_function_definition_p
21406           (cp_lexer_peek_token (parser->lexer)))
21407         cp_parser_skip_to_end_of_block_or_statement (parser);
21408       return error_mark_node;
21409     }
21410
21411   /* Remember it, if there default args to post process.  */
21412   cp_parser_save_default_args (parser, fn);
21413
21414   /* Save away the tokens that make up the body of the
21415      function.  */
21416   first = parser->lexer->next_token;
21417   /* We can have braced-init-list mem-initializers before the fn body.  */
21418   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21419     {
21420       cp_lexer_consume_token (parser->lexer);
21421       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21422              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21423         {
21424           /* cache_group will stop after an un-nested { } pair, too.  */
21425           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21426             break;
21427
21428           /* variadic mem-inits have ... after the ')'.  */
21429           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21430             cp_lexer_consume_token (parser->lexer);
21431         }
21432     }
21433   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21434   /* Handle function try blocks.  */
21435   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21436     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21437   last = parser->lexer->next_token;
21438
21439   /* Save away the inline definition; we will process it when the
21440      class is complete.  */
21441   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21442   DECL_PENDING_INLINE_P (fn) = 1;
21443
21444   /* We need to know that this was defined in the class, so that
21445      friend templates are handled correctly.  */
21446   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21447
21448   /* Add FN to the queue of functions to be parsed later.  */
21449   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21450
21451   return fn;
21452 }
21453
21454 /* Save the tokens that make up the in-class initializer for a non-static
21455    data member.  Returns a DEFAULT_ARG.  */
21456
21457 static tree
21458 cp_parser_save_nsdmi (cp_parser* parser)
21459 {
21460   /* Save away the tokens that make up the body of the
21461      function.  */
21462   cp_token *first = parser->lexer->next_token;
21463   cp_token *last;
21464   tree node;
21465
21466   /* Save tokens until the next comma or semicolon.  */
21467   cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21468
21469   last = parser->lexer->next_token;
21470
21471   node = make_node (DEFAULT_ARG);
21472   DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21473   DEFARG_INSTANTIATIONS (node) = NULL;
21474
21475   return node;
21476 }
21477
21478
21479 /* Parse a template-argument-list, as well as the trailing ">" (but
21480    not the opening "<").  See cp_parser_template_argument_list for the
21481    return value.  */
21482
21483 static tree
21484 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21485 {
21486   tree arguments;
21487   tree saved_scope;
21488   tree saved_qualifying_scope;
21489   tree saved_object_scope;
21490   bool saved_greater_than_is_operator_p;
21491   int saved_unevaluated_operand;
21492   int saved_inhibit_evaluation_warnings;
21493
21494   /* [temp.names]
21495
21496      When parsing a template-id, the first non-nested `>' is taken as
21497      the end of the template-argument-list rather than a greater-than
21498      operator.  */
21499   saved_greater_than_is_operator_p
21500     = parser->greater_than_is_operator_p;
21501   parser->greater_than_is_operator_p = false;
21502   /* Parsing the argument list may modify SCOPE, so we save it
21503      here.  */
21504   saved_scope = parser->scope;
21505   saved_qualifying_scope = parser->qualifying_scope;
21506   saved_object_scope = parser->object_scope;
21507   /* We need to evaluate the template arguments, even though this
21508      template-id may be nested within a "sizeof".  */
21509   saved_unevaluated_operand = cp_unevaluated_operand;
21510   cp_unevaluated_operand = 0;
21511   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21512   c_inhibit_evaluation_warnings = 0;
21513   /* Parse the template-argument-list itself.  */
21514   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21515       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21516     arguments = NULL_TREE;
21517   else
21518     arguments = cp_parser_template_argument_list (parser);
21519   /* Look for the `>' that ends the template-argument-list. If we find
21520      a '>>' instead, it's probably just a typo.  */
21521   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21522     {
21523       if (cxx_dialect != cxx98)
21524         {
21525           /* In C++0x, a `>>' in a template argument list or cast
21526              expression is considered to be two separate `>'
21527              tokens. So, change the current token to a `>', but don't
21528              consume it: it will be consumed later when the outer
21529              template argument list (or cast expression) is parsed.
21530              Note that this replacement of `>' for `>>' is necessary
21531              even if we are parsing tentatively: in the tentative
21532              case, after calling
21533              cp_parser_enclosed_template_argument_list we will always
21534              throw away all of the template arguments and the first
21535              closing `>', either because the template argument list
21536              was erroneous or because we are replacing those tokens
21537              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21538              not have been thrown away) is needed either to close an
21539              outer template argument list or to complete a new-style
21540              cast.  */
21541           cp_token *token = cp_lexer_peek_token (parser->lexer);
21542           token->type = CPP_GREATER;
21543         }
21544       else if (!saved_greater_than_is_operator_p)
21545         {
21546           /* If we're in a nested template argument list, the '>>' has
21547             to be a typo for '> >'. We emit the error message, but we
21548             continue parsing and we push a '>' as next token, so that
21549             the argument list will be parsed correctly.  Note that the
21550             global source location is still on the token before the
21551             '>>', so we need to say explicitly where we want it.  */
21552           cp_token *token = cp_lexer_peek_token (parser->lexer);
21553           error_at (token->location, "%<>>%> should be %<> >%> "
21554                     "within a nested template argument list");
21555
21556           token->type = CPP_GREATER;
21557         }
21558       else
21559         {
21560           /* If this is not a nested template argument list, the '>>'
21561             is a typo for '>'. Emit an error message and continue.
21562             Same deal about the token location, but here we can get it
21563             right by consuming the '>>' before issuing the diagnostic.  */
21564           cp_token *token = cp_lexer_consume_token (parser->lexer);
21565           error_at (token->location,
21566                     "spurious %<>>%>, use %<>%> to terminate "
21567                     "a template argument list");
21568         }
21569     }
21570   else
21571     cp_parser_skip_to_end_of_template_parameter_list (parser);
21572   /* The `>' token might be a greater-than operator again now.  */
21573   parser->greater_than_is_operator_p
21574     = saved_greater_than_is_operator_p;
21575   /* Restore the SAVED_SCOPE.  */
21576   parser->scope = saved_scope;
21577   parser->qualifying_scope = saved_qualifying_scope;
21578   parser->object_scope = saved_object_scope;
21579   cp_unevaluated_operand = saved_unevaluated_operand;
21580   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21581
21582   return arguments;
21583 }
21584
21585 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21586    arguments, or the body of the function have not yet been parsed,
21587    parse them now.  */
21588
21589 static void
21590 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21591 {
21592   timevar_push (TV_PARSE_INMETH);
21593   /* If this member is a template, get the underlying
21594      FUNCTION_DECL.  */
21595   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21596     member_function = DECL_TEMPLATE_RESULT (member_function);
21597
21598   /* There should not be any class definitions in progress at this
21599      point; the bodies of members are only parsed outside of all class
21600      definitions.  */
21601   gcc_assert (parser->num_classes_being_defined == 0);
21602   /* While we're parsing the member functions we might encounter more
21603      classes.  We want to handle them right away, but we don't want
21604      them getting mixed up with functions that are currently in the
21605      queue.  */
21606   push_unparsed_function_queues (parser);
21607
21608   /* Make sure that any template parameters are in scope.  */
21609   maybe_begin_member_template_processing (member_function);
21610
21611   /* If the body of the function has not yet been parsed, parse it
21612      now.  */
21613   if (DECL_PENDING_INLINE_P (member_function))
21614     {
21615       tree function_scope;
21616       cp_token_cache *tokens;
21617
21618       /* The function is no longer pending; we are processing it.  */
21619       tokens = DECL_PENDING_INLINE_INFO (member_function);
21620       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21621       DECL_PENDING_INLINE_P (member_function) = 0;
21622
21623       /* If this is a local class, enter the scope of the containing
21624          function.  */
21625       function_scope = current_function_decl;
21626       if (function_scope)
21627         push_function_context ();
21628
21629       /* Push the body of the function onto the lexer stack.  */
21630       cp_parser_push_lexer_for_tokens (parser, tokens);
21631
21632       /* Let the front end know that we going to be defining this
21633          function.  */
21634       start_preparsed_function (member_function, NULL_TREE,
21635                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21636
21637       /* Don't do access checking if it is a templated function.  */
21638       if (processing_template_decl)
21639         push_deferring_access_checks (dk_no_check);
21640
21641       /* Now, parse the body of the function.  */
21642       cp_parser_function_definition_after_declarator (parser,
21643                                                       /*inline_p=*/true);
21644
21645       if (processing_template_decl)
21646         pop_deferring_access_checks ();
21647
21648       /* Leave the scope of the containing function.  */
21649       if (function_scope)
21650         pop_function_context ();
21651       cp_parser_pop_lexer (parser);
21652     }
21653
21654   /* Remove any template parameters from the symbol table.  */
21655   maybe_end_member_template_processing ();
21656
21657   /* Restore the queue.  */
21658   pop_unparsed_function_queues (parser);
21659   timevar_pop (TV_PARSE_INMETH);
21660 }
21661
21662 /* If DECL contains any default args, remember it on the unparsed
21663    functions queue.  */
21664
21665 static void
21666 cp_parser_save_default_args (cp_parser* parser, tree decl)
21667 {
21668   tree probe;
21669
21670   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21671        probe;
21672        probe = TREE_CHAIN (probe))
21673     if (TREE_PURPOSE (probe))
21674       {
21675         cp_default_arg_entry *entry
21676           = VEC_safe_push (cp_default_arg_entry, gc,
21677                            unparsed_funs_with_default_args, NULL);
21678         entry->class_type = current_class_type;
21679         entry->decl = decl;
21680         break;
21681       }
21682 }
21683
21684 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21685    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21686    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21687    from the parameter-type-list.  */
21688
21689 static tree
21690 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21691                                       tree default_arg, tree parmtype)
21692 {
21693   cp_token_cache *tokens;
21694   tree parsed_arg;
21695   bool dummy;
21696
21697   /* Push the saved tokens for the default argument onto the parser's
21698      lexer stack.  */
21699   tokens = DEFARG_TOKENS (default_arg);
21700   cp_parser_push_lexer_for_tokens (parser, tokens);
21701
21702   start_lambda_scope (decl);
21703
21704   /* Parse the default argument.  */
21705   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21706   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21707     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21708
21709   finish_lambda_scope ();
21710
21711   if (!processing_template_decl)
21712     {
21713       /* In a non-template class, check conversions now.  In a template,
21714          we'll wait and instantiate these as needed.  */
21715       if (TREE_CODE (decl) == PARM_DECL)
21716         parsed_arg = check_default_argument (parmtype, parsed_arg);
21717       else
21718         {
21719           int flags = LOOKUP_IMPLICIT;
21720           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21721               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21722             flags = LOOKUP_NORMAL;
21723           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21724         }
21725     }
21726
21727   /* If the token stream has not been completely used up, then
21728      there was extra junk after the end of the default
21729      argument.  */
21730   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21731     {
21732       if (TREE_CODE (decl) == PARM_DECL)
21733         cp_parser_error (parser, "expected %<,%>");
21734       else
21735         cp_parser_error (parser, "expected %<;%>");
21736     }
21737
21738   /* Revert to the main lexer.  */
21739   cp_parser_pop_lexer (parser);
21740
21741   return parsed_arg;
21742 }
21743
21744 /* FIELD is a non-static data member with an initializer which we saved for
21745    later; parse it now.  */
21746
21747 static void
21748 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21749 {
21750   tree def;
21751
21752   push_unparsed_function_queues (parser);
21753   def = cp_parser_late_parse_one_default_arg (parser, field,
21754                                               DECL_INITIAL (field),
21755                                               NULL_TREE);
21756   pop_unparsed_function_queues (parser);
21757
21758   DECL_INITIAL (field) = def;
21759 }
21760
21761 /* FN is a FUNCTION_DECL which may contains a parameter with an
21762    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21763    assumes that the current scope is the scope in which the default
21764    argument should be processed.  */
21765
21766 static void
21767 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21768 {
21769   bool saved_local_variables_forbidden_p;
21770   tree parm, parmdecl;
21771
21772   /* While we're parsing the default args, we might (due to the
21773      statement expression extension) encounter more classes.  We want
21774      to handle them right away, but we don't want them getting mixed
21775      up with default args that are currently in the queue.  */
21776   push_unparsed_function_queues (parser);
21777
21778   /* Local variable names (and the `this' keyword) may not appear
21779      in a default argument.  */
21780   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21781   parser->local_variables_forbidden_p = true;
21782
21783   push_defarg_context (fn);
21784
21785   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21786          parmdecl = DECL_ARGUMENTS (fn);
21787        parm && parm != void_list_node;
21788        parm = TREE_CHAIN (parm),
21789          parmdecl = DECL_CHAIN (parmdecl))
21790     {
21791       tree default_arg = TREE_PURPOSE (parm);
21792       tree parsed_arg;
21793       VEC(tree,gc) *insts;
21794       tree copy;
21795       unsigned ix;
21796
21797       if (!default_arg)
21798         continue;
21799
21800       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21801         /* This can happen for a friend declaration for a function
21802            already declared with default arguments.  */
21803         continue;
21804
21805       parsed_arg
21806         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21807                                                 default_arg,
21808                                                 TREE_VALUE (parm));
21809       if (parsed_arg == error_mark_node)
21810         {
21811           continue;
21812         }
21813
21814       TREE_PURPOSE (parm) = parsed_arg;
21815
21816       /* Update any instantiations we've already created.  */
21817       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21818            VEC_iterate (tree, insts, ix, copy); ix++)
21819         TREE_PURPOSE (copy) = parsed_arg;
21820     }
21821
21822   pop_defarg_context ();
21823
21824   /* Make sure no default arg is missing.  */
21825   check_default_args (fn);
21826
21827   /* Restore the state of local_variables_forbidden_p.  */
21828   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21829
21830   /* Restore the queue.  */
21831   pop_unparsed_function_queues (parser);
21832 }
21833
21834 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21835    either a TYPE or an expression, depending on the form of the
21836    input.  The KEYWORD indicates which kind of expression we have
21837    encountered.  */
21838
21839 static tree
21840 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21841 {
21842   tree expr = NULL_TREE;
21843   const char *saved_message;
21844   char *tmp;
21845   bool saved_integral_constant_expression_p;
21846   bool saved_non_integral_constant_expression_p;
21847   bool pack_expansion_p = false;
21848
21849   /* Types cannot be defined in a `sizeof' expression.  Save away the
21850      old message.  */
21851   saved_message = parser->type_definition_forbidden_message;
21852   /* And create the new one.  */
21853   tmp = concat ("types may not be defined in %<",
21854                 IDENTIFIER_POINTER (ridpointers[keyword]),
21855                 "%> expressions", NULL);
21856   parser->type_definition_forbidden_message = tmp;
21857
21858   /* The restrictions on constant-expressions do not apply inside
21859      sizeof expressions.  */
21860   saved_integral_constant_expression_p
21861     = parser->integral_constant_expression_p;
21862   saved_non_integral_constant_expression_p
21863     = parser->non_integral_constant_expression_p;
21864   parser->integral_constant_expression_p = false;
21865
21866   /* If it's a `...', then we are computing the length of a parameter
21867      pack.  */
21868   if (keyword == RID_SIZEOF
21869       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21870     {
21871       /* Consume the `...'.  */
21872       cp_lexer_consume_token (parser->lexer);
21873       maybe_warn_variadic_templates ();
21874
21875       /* Note that this is an expansion.  */
21876       pack_expansion_p = true;
21877     }
21878
21879   /* Do not actually evaluate the expression.  */
21880   ++cp_unevaluated_operand;
21881   ++c_inhibit_evaluation_warnings;
21882   /* If it's a `(', then we might be looking at the type-id
21883      construction.  */
21884   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21885     {
21886       tree type;
21887       bool saved_in_type_id_in_expr_p;
21888
21889       /* We can't be sure yet whether we're looking at a type-id or an
21890          expression.  */
21891       cp_parser_parse_tentatively (parser);
21892       /* Consume the `('.  */
21893       cp_lexer_consume_token (parser->lexer);
21894       /* Parse the type-id.  */
21895       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21896       parser->in_type_id_in_expr_p = true;
21897       type = cp_parser_type_id (parser);
21898       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21899       /* Now, look for the trailing `)'.  */
21900       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21901       /* If all went well, then we're done.  */
21902       if (cp_parser_parse_definitely (parser))
21903         {
21904           cp_decl_specifier_seq decl_specs;
21905
21906           /* Build a trivial decl-specifier-seq.  */
21907           clear_decl_specs (&decl_specs);
21908           decl_specs.type = type;
21909
21910           /* Call grokdeclarator to figure out what type this is.  */
21911           expr = grokdeclarator (NULL,
21912                                  &decl_specs,
21913                                  TYPENAME,
21914                                  /*initialized=*/0,
21915                                  /*attrlist=*/NULL);
21916         }
21917     }
21918
21919   /* If the type-id production did not work out, then we must be
21920      looking at the unary-expression production.  */
21921   if (!expr)
21922     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21923                                        /*cast_p=*/false, NULL);
21924
21925   if (pack_expansion_p)
21926     /* Build a pack expansion. */
21927     expr = make_pack_expansion (expr);
21928
21929   /* Go back to evaluating expressions.  */
21930   --cp_unevaluated_operand;
21931   --c_inhibit_evaluation_warnings;
21932
21933   /* Free the message we created.  */
21934   free (tmp);
21935   /* And restore the old one.  */
21936   parser->type_definition_forbidden_message = saved_message;
21937   parser->integral_constant_expression_p
21938     = saved_integral_constant_expression_p;
21939   parser->non_integral_constant_expression_p
21940     = saved_non_integral_constant_expression_p;
21941
21942   return expr;
21943 }
21944
21945 /* If the current declaration has no declarator, return true.  */
21946
21947 static bool
21948 cp_parser_declares_only_class_p (cp_parser *parser)
21949 {
21950   /* If the next token is a `;' or a `,' then there is no
21951      declarator.  */
21952   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21953           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21954 }
21955
21956 /* Update the DECL_SPECS to reflect the storage class indicated by
21957    KEYWORD.  */
21958
21959 static void
21960 cp_parser_set_storage_class (cp_parser *parser,
21961                              cp_decl_specifier_seq *decl_specs,
21962                              enum rid keyword,
21963                              location_t location)
21964 {
21965   cp_storage_class storage_class;
21966
21967   if (parser->in_unbraced_linkage_specification_p)
21968     {
21969       error_at (location, "invalid use of %qD in linkage specification",
21970                 ridpointers[keyword]);
21971       return;
21972     }
21973   else if (decl_specs->storage_class != sc_none)
21974     {
21975       decl_specs->conflicting_specifiers_p = true;
21976       return;
21977     }
21978
21979   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21980       && decl_specs->specs[(int) ds_thread])
21981     {
21982       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21983       decl_specs->specs[(int) ds_thread] = 0;
21984     }
21985
21986   switch (keyword)
21987     {
21988     case RID_AUTO:
21989       storage_class = sc_auto;
21990       break;
21991     case RID_REGISTER:
21992       storage_class = sc_register;
21993       break;
21994     case RID_STATIC:
21995       storage_class = sc_static;
21996       break;
21997     case RID_EXTERN:
21998       storage_class = sc_extern;
21999       break;
22000     case RID_MUTABLE:
22001       storage_class = sc_mutable;
22002       break;
22003     default:
22004       gcc_unreachable ();
22005     }
22006   decl_specs->storage_class = storage_class;
22007
22008   /* A storage class specifier cannot be applied alongside a typedef 
22009      specifier. If there is a typedef specifier present then set 
22010      conflicting_specifiers_p which will trigger an error later
22011      on in grokdeclarator. */
22012   if (decl_specs->specs[(int)ds_typedef])
22013     decl_specs->conflicting_specifiers_p = true;
22014 }
22015
22016 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
22017    is true, the type is a class or enum definition.  */
22018
22019 static void
22020 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
22021                               tree type_spec,
22022                               location_t location,
22023                               bool type_definition_p)
22024 {
22025   decl_specs->any_specifiers_p = true;
22026
22027   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
22028      (with, for example, in "typedef int wchar_t;") we remember that
22029      this is what happened.  In system headers, we ignore these
22030      declarations so that G++ can work with system headers that are not
22031      C++-safe.  */
22032   if (decl_specs->specs[(int) ds_typedef]
22033       && !type_definition_p
22034       && (type_spec == boolean_type_node
22035           || type_spec == char16_type_node
22036           || type_spec == char32_type_node
22037           || type_spec == wchar_type_node)
22038       && (decl_specs->type
22039           || decl_specs->specs[(int) ds_long]
22040           || decl_specs->specs[(int) ds_short]
22041           || decl_specs->specs[(int) ds_unsigned]
22042           || decl_specs->specs[(int) ds_signed]))
22043     {
22044       decl_specs->redefined_builtin_type = type_spec;
22045       if (!decl_specs->type)
22046         {
22047           decl_specs->type = type_spec;
22048           decl_specs->type_definition_p = false;
22049           decl_specs->type_location = location;
22050         }
22051     }
22052   else if (decl_specs->type)
22053     decl_specs->multiple_types_p = true;
22054   else
22055     {
22056       decl_specs->type = type_spec;
22057       decl_specs->type_definition_p = type_definition_p;
22058       decl_specs->redefined_builtin_type = NULL_TREE;
22059       decl_specs->type_location = location;
22060     }
22061 }
22062
22063 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
22064    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
22065
22066 static bool
22067 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
22068 {
22069   return decl_specifiers->specs[(int) ds_friend] != 0;
22070 }
22071
22072 /* Issue an error message indicating that TOKEN_DESC was expected.
22073    If KEYWORD is true, it indicated this function is called by
22074    cp_parser_require_keword and the required token can only be
22075    a indicated keyword. */
22076
22077 static void
22078 cp_parser_required_error (cp_parser *parser,
22079                           required_token token_desc,
22080                           bool keyword)
22081 {
22082   switch (token_desc)
22083     {
22084       case RT_NEW:
22085         cp_parser_error (parser, "expected %<new%>");
22086         return;
22087       case RT_DELETE:
22088         cp_parser_error (parser, "expected %<delete%>");
22089         return;
22090       case RT_RETURN:
22091         cp_parser_error (parser, "expected %<return%>");
22092         return;
22093       case RT_WHILE:
22094         cp_parser_error (parser, "expected %<while%>");
22095         return;
22096       case RT_EXTERN:
22097         cp_parser_error (parser, "expected %<extern%>");
22098         return;
22099       case RT_STATIC_ASSERT:
22100         cp_parser_error (parser, "expected %<static_assert%>");
22101         return;
22102       case RT_DECLTYPE:
22103         cp_parser_error (parser, "expected %<decltype%>");
22104         return;
22105       case RT_OPERATOR:
22106         cp_parser_error (parser, "expected %<operator%>");
22107         return;
22108       case RT_CLASS:
22109         cp_parser_error (parser, "expected %<class%>");
22110         return;
22111       case RT_TEMPLATE:
22112         cp_parser_error (parser, "expected %<template%>");
22113         return;
22114       case RT_NAMESPACE:
22115         cp_parser_error (parser, "expected %<namespace%>");
22116         return;
22117       case RT_USING:
22118         cp_parser_error (parser, "expected %<using%>");
22119         return;
22120       case RT_ASM:
22121         cp_parser_error (parser, "expected %<asm%>");
22122         return;
22123       case RT_TRY:
22124         cp_parser_error (parser, "expected %<try%>");
22125         return;
22126       case RT_CATCH:
22127         cp_parser_error (parser, "expected %<catch%>");
22128         return;
22129       case RT_THROW:
22130         cp_parser_error (parser, "expected %<throw%>");
22131         return;
22132       case RT_LABEL:
22133         cp_parser_error (parser, "expected %<__label__%>");
22134         return;
22135       case RT_AT_TRY:
22136         cp_parser_error (parser, "expected %<@try%>");
22137         return;
22138       case RT_AT_SYNCHRONIZED:
22139         cp_parser_error (parser, "expected %<@synchronized%>");
22140         return;
22141       case RT_AT_THROW:
22142         cp_parser_error (parser, "expected %<@throw%>");
22143         return;
22144       case RT_TRANSACTION_ATOMIC:
22145         cp_parser_error (parser, "expected %<__transaction_atomic%>");
22146         return;
22147       case RT_TRANSACTION_RELAXED:
22148         cp_parser_error (parser, "expected %<__transaction_relaxed%>");
22149         return;
22150       default:
22151         break;
22152     }
22153   if (!keyword)
22154     {
22155       switch (token_desc)
22156         {
22157           case RT_SEMICOLON:
22158             cp_parser_error (parser, "expected %<;%>");
22159             return;
22160           case RT_OPEN_PAREN:
22161             cp_parser_error (parser, "expected %<(%>");
22162             return;
22163           case RT_CLOSE_BRACE:
22164             cp_parser_error (parser, "expected %<}%>");
22165             return;
22166           case RT_OPEN_BRACE:
22167             cp_parser_error (parser, "expected %<{%>");
22168             return;
22169           case RT_CLOSE_SQUARE:
22170             cp_parser_error (parser, "expected %<]%>");
22171             return;
22172           case RT_OPEN_SQUARE:
22173             cp_parser_error (parser, "expected %<[%>");
22174             return;
22175           case RT_COMMA:
22176             cp_parser_error (parser, "expected %<,%>");
22177             return;
22178           case RT_SCOPE:
22179             cp_parser_error (parser, "expected %<::%>");
22180             return;
22181           case RT_LESS:
22182             cp_parser_error (parser, "expected %<<%>");
22183             return;
22184           case RT_GREATER:
22185             cp_parser_error (parser, "expected %<>%>");
22186             return;
22187           case RT_EQ:
22188             cp_parser_error (parser, "expected %<=%>");
22189             return;
22190           case RT_ELLIPSIS:
22191             cp_parser_error (parser, "expected %<...%>");
22192             return;
22193           case RT_MULT:
22194             cp_parser_error (parser, "expected %<*%>");
22195             return;
22196           case RT_COMPL:
22197             cp_parser_error (parser, "expected %<~%>");
22198             return;
22199           case RT_COLON:
22200             cp_parser_error (parser, "expected %<:%>");
22201             return;
22202           case RT_COLON_SCOPE:
22203             cp_parser_error (parser, "expected %<:%> or %<::%>");
22204             return;
22205           case RT_CLOSE_PAREN:
22206             cp_parser_error (parser, "expected %<)%>");
22207             return;
22208           case RT_COMMA_CLOSE_PAREN:
22209             cp_parser_error (parser, "expected %<,%> or %<)%>");
22210             return;
22211           case RT_PRAGMA_EOL:
22212             cp_parser_error (parser, "expected end of line");
22213             return;
22214           case RT_NAME:
22215             cp_parser_error (parser, "expected identifier");
22216             return;
22217           case RT_SELECT:
22218             cp_parser_error (parser, "expected selection-statement");
22219             return;
22220           case RT_INTERATION:
22221             cp_parser_error (parser, "expected iteration-statement");
22222             return;
22223           case RT_JUMP:
22224             cp_parser_error (parser, "expected jump-statement");
22225             return;
22226           case RT_CLASS_KEY:
22227             cp_parser_error (parser, "expected class-key");
22228             return;
22229           case RT_CLASS_TYPENAME_TEMPLATE:
22230             cp_parser_error (parser,
22231                  "expected %<class%>, %<typename%>, or %<template%>");
22232             return;
22233           default:
22234             gcc_unreachable ();
22235         }
22236     }
22237   else
22238     gcc_unreachable ();
22239 }
22240
22241
22242
22243 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22244    issue an error message indicating that TOKEN_DESC was expected.
22245
22246    Returns the token consumed, if the token had the appropriate type.
22247    Otherwise, returns NULL.  */
22248
22249 static cp_token *
22250 cp_parser_require (cp_parser* parser,
22251                    enum cpp_ttype type,
22252                    required_token token_desc)
22253 {
22254   if (cp_lexer_next_token_is (parser->lexer, type))
22255     return cp_lexer_consume_token (parser->lexer);
22256   else
22257     {
22258       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22259       if (!cp_parser_simulate_error (parser))
22260         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22261       return NULL;
22262     }
22263 }
22264
22265 /* An error message is produced if the next token is not '>'.
22266    All further tokens are skipped until the desired token is
22267    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22268
22269 static void
22270 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22271 {
22272   /* Current level of '< ... >'.  */
22273   unsigned level = 0;
22274   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22275   unsigned nesting_depth = 0;
22276
22277   /* Are we ready, yet?  If not, issue error message.  */
22278   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22279     return;
22280
22281   /* Skip tokens until the desired token is found.  */
22282   while (true)
22283     {
22284       /* Peek at the next token.  */
22285       switch (cp_lexer_peek_token (parser->lexer)->type)
22286         {
22287         case CPP_LESS:
22288           if (!nesting_depth)
22289             ++level;
22290           break;
22291
22292         case CPP_RSHIFT:
22293           if (cxx_dialect == cxx98)
22294             /* C++0x views the `>>' operator as two `>' tokens, but
22295                C++98 does not. */
22296             break;
22297           else if (!nesting_depth && level-- == 0)
22298             {
22299               /* We've hit a `>>' where the first `>' closes the
22300                  template argument list, and the second `>' is
22301                  spurious.  Just consume the `>>' and stop; we've
22302                  already produced at least one error.  */
22303               cp_lexer_consume_token (parser->lexer);
22304               return;
22305             }
22306           /* Fall through for C++0x, so we handle the second `>' in
22307              the `>>'.  */
22308
22309         case CPP_GREATER:
22310           if (!nesting_depth && level-- == 0)
22311             {
22312               /* We've reached the token we want, consume it and stop.  */
22313               cp_lexer_consume_token (parser->lexer);
22314               return;
22315             }
22316           break;
22317
22318         case CPP_OPEN_PAREN:
22319         case CPP_OPEN_SQUARE:
22320           ++nesting_depth;
22321           break;
22322
22323         case CPP_CLOSE_PAREN:
22324         case CPP_CLOSE_SQUARE:
22325           if (nesting_depth-- == 0)
22326             return;
22327           break;
22328
22329         case CPP_EOF:
22330         case CPP_PRAGMA_EOL:
22331         case CPP_SEMICOLON:
22332         case CPP_OPEN_BRACE:
22333         case CPP_CLOSE_BRACE:
22334           /* The '>' was probably forgotten, don't look further.  */
22335           return;
22336
22337         default:
22338           break;
22339         }
22340
22341       /* Consume this token.  */
22342       cp_lexer_consume_token (parser->lexer);
22343     }
22344 }
22345
22346 /* If the next token is the indicated keyword, consume it.  Otherwise,
22347    issue an error message indicating that TOKEN_DESC was expected.
22348
22349    Returns the token consumed, if the token had the appropriate type.
22350    Otherwise, returns NULL.  */
22351
22352 static cp_token *
22353 cp_parser_require_keyword (cp_parser* parser,
22354                            enum rid keyword,
22355                            required_token token_desc)
22356 {
22357   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22358
22359   if (token && token->keyword != keyword)
22360     {
22361       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22362       return NULL;
22363     }
22364
22365   return token;
22366 }
22367
22368 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22369    function-definition.  */
22370
22371 static bool
22372 cp_parser_token_starts_function_definition_p (cp_token* token)
22373 {
22374   return (/* An ordinary function-body begins with an `{'.  */
22375           token->type == CPP_OPEN_BRACE
22376           /* A ctor-initializer begins with a `:'.  */
22377           || token->type == CPP_COLON
22378           /* A function-try-block begins with `try'.  */
22379           || token->keyword == RID_TRY
22380           /* A function-transaction-block begins with `__transaction_atomic'
22381              or `__transaction_relaxed'.  */
22382           || token->keyword == RID_TRANSACTION_ATOMIC
22383           || token->keyword == RID_TRANSACTION_RELAXED
22384           /* The named return value extension begins with `return'.  */
22385           || token->keyword == RID_RETURN);
22386 }
22387
22388 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22389    definition.  */
22390
22391 static bool
22392 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22393 {
22394   cp_token *token;
22395
22396   token = cp_lexer_peek_token (parser->lexer);
22397   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22398 }
22399
22400 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22401    C++0x) ending a template-argument.  */
22402
22403 static bool
22404 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22405 {
22406   cp_token *token;
22407
22408   token = cp_lexer_peek_token (parser->lexer);
22409   return (token->type == CPP_COMMA 
22410           || token->type == CPP_GREATER
22411           || token->type == CPP_ELLIPSIS
22412           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22413 }
22414
22415 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22416    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22417
22418 static bool
22419 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22420                                                      size_t n)
22421 {
22422   cp_token *token;
22423
22424   token = cp_lexer_peek_nth_token (parser->lexer, n);
22425   if (token->type == CPP_LESS)
22426     return true;
22427   /* Check for the sequence `<::' in the original code. It would be lexed as
22428      `[:', where `[' is a digraph, and there is no whitespace before
22429      `:'.  */
22430   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22431     {
22432       cp_token *token2;
22433       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22434       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22435         return true;
22436     }
22437   return false;
22438 }
22439
22440 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22441    or none_type otherwise.  */
22442
22443 static enum tag_types
22444 cp_parser_token_is_class_key (cp_token* token)
22445 {
22446   switch (token->keyword)
22447     {
22448     case RID_CLASS:
22449       return class_type;
22450     case RID_STRUCT:
22451       return record_type;
22452     case RID_UNION:
22453       return union_type;
22454
22455     default:
22456       return none_type;
22457     }
22458 }
22459
22460 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22461
22462 static void
22463 cp_parser_check_class_key (enum tag_types class_key, tree type)
22464 {
22465   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22466     permerror (input_location, "%qs tag used in naming %q#T",
22467             class_key == union_type ? "union"
22468              : class_key == record_type ? "struct" : "class",
22469              type);
22470 }
22471
22472 /* Issue an error message if DECL is redeclared with different
22473    access than its original declaration [class.access.spec/3].
22474    This applies to nested classes and nested class templates.
22475    [class.mem/1].  */
22476
22477 static void
22478 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22479 {
22480   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22481     return;
22482
22483   if ((TREE_PRIVATE (decl)
22484        != (current_access_specifier == access_private_node))
22485       || (TREE_PROTECTED (decl)
22486           != (current_access_specifier == access_protected_node)))
22487     error_at (location, "%qD redeclared with different access", decl);
22488 }
22489
22490 /* Look for the `template' keyword, as a syntactic disambiguator.
22491    Return TRUE iff it is present, in which case it will be
22492    consumed.  */
22493
22494 static bool
22495 cp_parser_optional_template_keyword (cp_parser *parser)
22496 {
22497   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22498     {
22499       /* The `template' keyword can only be used within templates;
22500          outside templates the parser can always figure out what is a
22501          template and what is not.  */
22502       if (!processing_template_decl)
22503         {
22504           cp_token *token = cp_lexer_peek_token (parser->lexer);
22505           error_at (token->location,
22506                     "%<template%> (as a disambiguator) is only allowed "
22507                     "within templates");
22508           /* If this part of the token stream is rescanned, the same
22509              error message would be generated.  So, we purge the token
22510              from the stream.  */
22511           cp_lexer_purge_token (parser->lexer);
22512           return false;
22513         }
22514       else
22515         {
22516           /* Consume the `template' keyword.  */
22517           cp_lexer_consume_token (parser->lexer);
22518           return true;
22519         }
22520     }
22521
22522   return false;
22523 }
22524
22525 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22526    set PARSER->SCOPE, and perform other related actions.  */
22527
22528 static void
22529 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22530 {
22531   int i;
22532   struct tree_check *check_value;
22533   deferred_access_check *chk;
22534   VEC (deferred_access_check,gc) *checks;
22535
22536   /* Get the stored value.  */
22537   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22538   /* Perform any access checks that were deferred.  */
22539   checks = check_value->checks;
22540   if (checks)
22541     {
22542       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22543         perform_or_defer_access_check (chk->binfo,
22544                                        chk->decl,
22545                                        chk->diag_decl);
22546     }
22547   /* Set the scope from the stored value.  */
22548   parser->scope = check_value->value;
22549   parser->qualifying_scope = check_value->qualifying_scope;
22550   parser->object_scope = NULL_TREE;
22551 }
22552
22553 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22554    encounter the end of a block before what we were looking for.  */
22555
22556 static bool
22557 cp_parser_cache_group (cp_parser *parser,
22558                        enum cpp_ttype end,
22559                        unsigned depth)
22560 {
22561   while (true)
22562     {
22563       cp_token *token = cp_lexer_peek_token (parser->lexer);
22564
22565       /* Abort a parenthesized expression if we encounter a semicolon.  */
22566       if ((end == CPP_CLOSE_PAREN || depth == 0)
22567           && token->type == CPP_SEMICOLON)
22568         return true;
22569       /* If we've reached the end of the file, stop.  */
22570       if (token->type == CPP_EOF
22571           || (end != CPP_PRAGMA_EOL
22572               && token->type == CPP_PRAGMA_EOL))
22573         return true;
22574       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22575         /* We've hit the end of an enclosing block, so there's been some
22576            kind of syntax error.  */
22577         return true;
22578
22579       /* If we're caching something finished by a comma (or semicolon),
22580          such as an NSDMI, don't consume the comma.  */
22581       if (end == CPP_COMMA
22582           && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22583         return false;
22584
22585       /* Consume the token.  */
22586       cp_lexer_consume_token (parser->lexer);
22587       /* See if it starts a new group.  */
22588       if (token->type == CPP_OPEN_BRACE)
22589         {
22590           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22591           /* In theory this should probably check end == '}', but
22592              cp_parser_save_member_function_body needs it to exit
22593              after either '}' or ')' when called with ')'.  */
22594           if (depth == 0)
22595             return false;
22596         }
22597       else if (token->type == CPP_OPEN_PAREN)
22598         {
22599           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22600           if (depth == 0 && end == CPP_CLOSE_PAREN)
22601             return false;
22602         }
22603       else if (token->type == CPP_PRAGMA)
22604         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22605       else if (token->type == end)
22606         return false;
22607     }
22608 }
22609
22610 /* Begin parsing tentatively.  We always save tokens while parsing
22611    tentatively so that if the tentative parsing fails we can restore the
22612    tokens.  */
22613
22614 static void
22615 cp_parser_parse_tentatively (cp_parser* parser)
22616 {
22617   /* Enter a new parsing context.  */
22618   parser->context = cp_parser_context_new (parser->context);
22619   /* Begin saving tokens.  */
22620   cp_lexer_save_tokens (parser->lexer);
22621   /* In order to avoid repetitive access control error messages,
22622      access checks are queued up until we are no longer parsing
22623      tentatively.  */
22624   push_deferring_access_checks (dk_deferred);
22625 }
22626
22627 /* Commit to the currently active tentative parse.  */
22628
22629 static void
22630 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22631 {
22632   cp_parser_context *context;
22633   cp_lexer *lexer;
22634
22635   /* Mark all of the levels as committed.  */
22636   lexer = parser->lexer;
22637   for (context = parser->context; context->next; context = context->next)
22638     {
22639       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22640         break;
22641       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22642       while (!cp_lexer_saving_tokens (lexer))
22643         lexer = lexer->next;
22644       cp_lexer_commit_tokens (lexer);
22645     }
22646 }
22647
22648 /* Abort the currently active tentative parse.  All consumed tokens
22649    will be rolled back, and no diagnostics will be issued.  */
22650
22651 static void
22652 cp_parser_abort_tentative_parse (cp_parser* parser)
22653 {
22654   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22655               || errorcount > 0);
22656   cp_parser_simulate_error (parser);
22657   /* Now, pretend that we want to see if the construct was
22658      successfully parsed.  */
22659   cp_parser_parse_definitely (parser);
22660 }
22661
22662 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22663    token stream.  Otherwise, commit to the tokens we have consumed.
22664    Returns true if no error occurred; false otherwise.  */
22665
22666 static bool
22667 cp_parser_parse_definitely (cp_parser* parser)
22668 {
22669   bool error_occurred;
22670   cp_parser_context *context;
22671
22672   /* Remember whether or not an error occurred, since we are about to
22673      destroy that information.  */
22674   error_occurred = cp_parser_error_occurred (parser);
22675   /* Remove the topmost context from the stack.  */
22676   context = parser->context;
22677   parser->context = context->next;
22678   /* If no parse errors occurred, commit to the tentative parse.  */
22679   if (!error_occurred)
22680     {
22681       /* Commit to the tokens read tentatively, unless that was
22682          already done.  */
22683       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22684         cp_lexer_commit_tokens (parser->lexer);
22685
22686       pop_to_parent_deferring_access_checks ();
22687     }
22688   /* Otherwise, if errors occurred, roll back our state so that things
22689      are just as they were before we began the tentative parse.  */
22690   else
22691     {
22692       cp_lexer_rollback_tokens (parser->lexer);
22693       pop_deferring_access_checks ();
22694     }
22695   /* Add the context to the front of the free list.  */
22696   context->next = cp_parser_context_free_list;
22697   cp_parser_context_free_list = context;
22698
22699   return !error_occurred;
22700 }
22701
22702 /* Returns true if we are parsing tentatively and are not committed to
22703    this tentative parse.  */
22704
22705 static bool
22706 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22707 {
22708   return (cp_parser_parsing_tentatively (parser)
22709           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22710 }
22711
22712 /* Returns nonzero iff an error has occurred during the most recent
22713    tentative parse.  */
22714
22715 static bool
22716 cp_parser_error_occurred (cp_parser* parser)
22717 {
22718   return (cp_parser_parsing_tentatively (parser)
22719           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22720 }
22721
22722 /* Returns nonzero if GNU extensions are allowed.  */
22723
22724 static bool
22725 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22726 {
22727   return parser->allow_gnu_extensions_p;
22728 }
22729 \f
22730 /* Objective-C++ Productions */
22731
22732
22733 /* Parse an Objective-C expression, which feeds into a primary-expression
22734    above.
22735
22736    objc-expression:
22737      objc-message-expression
22738      objc-string-literal
22739      objc-encode-expression
22740      objc-protocol-expression
22741      objc-selector-expression
22742
22743   Returns a tree representation of the expression.  */
22744
22745 static tree
22746 cp_parser_objc_expression (cp_parser* parser)
22747 {
22748   /* Try to figure out what kind of declaration is present.  */
22749   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22750
22751   switch (kwd->type)
22752     {
22753     case CPP_OPEN_SQUARE:
22754       return cp_parser_objc_message_expression (parser);
22755
22756     case CPP_OBJC_STRING:
22757       kwd = cp_lexer_consume_token (parser->lexer);
22758       return objc_build_string_object (kwd->u.value);
22759
22760     case CPP_KEYWORD:
22761       switch (kwd->keyword)
22762         {
22763         case RID_AT_ENCODE:
22764           return cp_parser_objc_encode_expression (parser);
22765
22766         case RID_AT_PROTOCOL:
22767           return cp_parser_objc_protocol_expression (parser);
22768
22769         case RID_AT_SELECTOR:
22770           return cp_parser_objc_selector_expression (parser);
22771
22772         default:
22773           break;
22774         }
22775     default:
22776       error_at (kwd->location,
22777                 "misplaced %<@%D%> Objective-C++ construct",
22778                 kwd->u.value);
22779       cp_parser_skip_to_end_of_block_or_statement (parser);
22780     }
22781
22782   return error_mark_node;
22783 }
22784
22785 /* Parse an Objective-C message expression.
22786
22787    objc-message-expression:
22788      [ objc-message-receiver objc-message-args ]
22789
22790    Returns a representation of an Objective-C message.  */
22791
22792 static tree
22793 cp_parser_objc_message_expression (cp_parser* parser)
22794 {
22795   tree receiver, messageargs;
22796
22797   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
22798   receiver = cp_parser_objc_message_receiver (parser);
22799   messageargs = cp_parser_objc_message_args (parser);
22800   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22801
22802   return objc_build_message_expr (receiver, messageargs);
22803 }
22804
22805 /* Parse an objc-message-receiver.
22806
22807    objc-message-receiver:
22808      expression
22809      simple-type-specifier
22810
22811   Returns a representation of the type or expression.  */
22812
22813 static tree
22814 cp_parser_objc_message_receiver (cp_parser* parser)
22815 {
22816   tree rcv;
22817
22818   /* An Objective-C message receiver may be either (1) a type
22819      or (2) an expression.  */
22820   cp_parser_parse_tentatively (parser);
22821   rcv = cp_parser_expression (parser, false, NULL);
22822
22823   if (cp_parser_parse_definitely (parser))
22824     return rcv;
22825
22826   rcv = cp_parser_simple_type_specifier (parser,
22827                                          /*decl_specs=*/NULL,
22828                                          CP_PARSER_FLAGS_NONE);
22829
22830   return objc_get_class_reference (rcv);
22831 }
22832
22833 /* Parse the arguments and selectors comprising an Objective-C message.
22834
22835    objc-message-args:
22836      objc-selector
22837      objc-selector-args
22838      objc-selector-args , objc-comma-args
22839
22840    objc-selector-args:
22841      objc-selector [opt] : assignment-expression
22842      objc-selector-args objc-selector [opt] : assignment-expression
22843
22844    objc-comma-args:
22845      assignment-expression
22846      objc-comma-args , assignment-expression
22847
22848    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22849    selector arguments and TREE_VALUE containing a list of comma
22850    arguments.  */
22851
22852 static tree
22853 cp_parser_objc_message_args (cp_parser* parser)
22854 {
22855   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22856   bool maybe_unary_selector_p = true;
22857   cp_token *token = cp_lexer_peek_token (parser->lexer);
22858
22859   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22860     {
22861       tree selector = NULL_TREE, arg;
22862
22863       if (token->type != CPP_COLON)
22864         selector = cp_parser_objc_selector (parser);
22865
22866       /* Detect if we have a unary selector.  */
22867       if (maybe_unary_selector_p
22868           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22869         return build_tree_list (selector, NULL_TREE);
22870
22871       maybe_unary_selector_p = false;
22872       cp_parser_require (parser, CPP_COLON, RT_COLON);
22873       arg = cp_parser_assignment_expression (parser, false, NULL);
22874
22875       sel_args
22876         = chainon (sel_args,
22877                    build_tree_list (selector, arg));
22878
22879       token = cp_lexer_peek_token (parser->lexer);
22880     }
22881
22882   /* Handle non-selector arguments, if any. */
22883   while (token->type == CPP_COMMA)
22884     {
22885       tree arg;
22886
22887       cp_lexer_consume_token (parser->lexer);
22888       arg = cp_parser_assignment_expression (parser, false, NULL);
22889
22890       addl_args
22891         = chainon (addl_args,
22892                    build_tree_list (NULL_TREE, arg));
22893
22894       token = cp_lexer_peek_token (parser->lexer);
22895     }
22896
22897   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22898     {
22899       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22900       return build_tree_list (error_mark_node, error_mark_node);
22901     }
22902
22903   return build_tree_list (sel_args, addl_args);
22904 }
22905
22906 /* Parse an Objective-C encode expression.
22907
22908    objc-encode-expression:
22909      @encode objc-typename
22910
22911    Returns an encoded representation of the type argument.  */
22912
22913 static tree
22914 cp_parser_objc_encode_expression (cp_parser* parser)
22915 {
22916   tree type;
22917   cp_token *token;
22918
22919   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
22920   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22921   token = cp_lexer_peek_token (parser->lexer);
22922   type = complete_type (cp_parser_type_id (parser));
22923   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22924
22925   if (!type)
22926     {
22927       error_at (token->location, 
22928                 "%<@encode%> must specify a type as an argument");
22929       return error_mark_node;
22930     }
22931
22932   /* This happens if we find @encode(T) (where T is a template
22933      typename or something dependent on a template typename) when
22934      parsing a template.  In that case, we can't compile it
22935      immediately, but we rather create an AT_ENCODE_EXPR which will
22936      need to be instantiated when the template is used.
22937   */
22938   if (dependent_type_p (type))
22939     {
22940       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22941       TREE_READONLY (value) = 1;
22942       return value;
22943     }
22944
22945   return objc_build_encode_expr (type);
22946 }
22947
22948 /* Parse an Objective-C @defs expression.  */
22949
22950 static tree
22951 cp_parser_objc_defs_expression (cp_parser *parser)
22952 {
22953   tree name;
22954
22955   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
22956   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22957   name = cp_parser_identifier (parser);
22958   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22959
22960   return objc_get_class_ivars (name);
22961 }
22962
22963 /* Parse an Objective-C protocol expression.
22964
22965   objc-protocol-expression:
22966     @protocol ( identifier )
22967
22968   Returns a representation of the protocol expression.  */
22969
22970 static tree
22971 cp_parser_objc_protocol_expression (cp_parser* parser)
22972 {
22973   tree proto;
22974
22975   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22976   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22977   proto = cp_parser_identifier (parser);
22978   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22979
22980   return objc_build_protocol_expr (proto);
22981 }
22982
22983 /* Parse an Objective-C selector expression.
22984
22985    objc-selector-expression:
22986      @selector ( objc-method-signature )
22987
22988    objc-method-signature:
22989      objc-selector
22990      objc-selector-seq
22991
22992    objc-selector-seq:
22993      objc-selector :
22994      objc-selector-seq objc-selector :
22995
22996   Returns a representation of the method selector.  */
22997
22998 static tree
22999 cp_parser_objc_selector_expression (cp_parser* parser)
23000 {
23001   tree sel_seq = NULL_TREE;
23002   bool maybe_unary_selector_p = true;
23003   cp_token *token;
23004   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23005
23006   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
23007   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23008   token = cp_lexer_peek_token (parser->lexer);
23009
23010   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
23011          || token->type == CPP_SCOPE)
23012     {
23013       tree selector = NULL_TREE;
23014
23015       if (token->type != CPP_COLON
23016           || token->type == CPP_SCOPE)
23017         selector = cp_parser_objc_selector (parser);
23018
23019       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
23020           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
23021         {
23022           /* Detect if we have a unary selector.  */
23023           if (maybe_unary_selector_p)
23024             {
23025               sel_seq = selector;
23026               goto finish_selector;
23027             }
23028           else
23029             {
23030               cp_parser_error (parser, "expected %<:%>");
23031             }
23032         }
23033       maybe_unary_selector_p = false;
23034       token = cp_lexer_consume_token (parser->lexer);
23035
23036       if (token->type == CPP_SCOPE)
23037         {
23038           sel_seq
23039             = chainon (sel_seq,
23040                        build_tree_list (selector, NULL_TREE));
23041           sel_seq
23042             = chainon (sel_seq,
23043                        build_tree_list (NULL_TREE, NULL_TREE));
23044         }
23045       else
23046         sel_seq
23047           = chainon (sel_seq,
23048                      build_tree_list (selector, NULL_TREE));
23049
23050       token = cp_lexer_peek_token (parser->lexer);
23051     }
23052
23053  finish_selector:
23054   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23055
23056   return objc_build_selector_expr (loc, sel_seq);
23057 }
23058
23059 /* Parse a list of identifiers.
23060
23061    objc-identifier-list:
23062      identifier
23063      objc-identifier-list , identifier
23064
23065    Returns a TREE_LIST of identifier nodes.  */
23066
23067 static tree
23068 cp_parser_objc_identifier_list (cp_parser* parser)
23069 {
23070   tree identifier;
23071   tree list;
23072   cp_token *sep;
23073
23074   identifier = cp_parser_identifier (parser);
23075   if (identifier == error_mark_node)
23076     return error_mark_node;      
23077
23078   list = build_tree_list (NULL_TREE, identifier);
23079   sep = cp_lexer_peek_token (parser->lexer);
23080
23081   while (sep->type == CPP_COMMA)
23082     {
23083       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23084       identifier = cp_parser_identifier (parser);
23085       if (identifier == error_mark_node)
23086         return list;
23087
23088       list = chainon (list, build_tree_list (NULL_TREE,
23089                                              identifier));
23090       sep = cp_lexer_peek_token (parser->lexer);
23091     }
23092   
23093   return list;
23094 }
23095
23096 /* Parse an Objective-C alias declaration.
23097
23098    objc-alias-declaration:
23099      @compatibility_alias identifier identifier ;
23100
23101    This function registers the alias mapping with the Objective-C front end.
23102    It returns nothing.  */
23103
23104 static void
23105 cp_parser_objc_alias_declaration (cp_parser* parser)
23106 {
23107   tree alias, orig;
23108
23109   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
23110   alias = cp_parser_identifier (parser);
23111   orig = cp_parser_identifier (parser);
23112   objc_declare_alias (alias, orig);
23113   cp_parser_consume_semicolon_at_end_of_statement (parser);
23114 }
23115
23116 /* Parse an Objective-C class forward-declaration.
23117
23118    objc-class-declaration:
23119      @class objc-identifier-list ;
23120
23121    The function registers the forward declarations with the Objective-C
23122    front end.  It returns nothing.  */
23123
23124 static void
23125 cp_parser_objc_class_declaration (cp_parser* parser)
23126 {
23127   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
23128   while (true)
23129     {
23130       tree id;
23131       
23132       id = cp_parser_identifier (parser);
23133       if (id == error_mark_node)
23134         break;
23135       
23136       objc_declare_class (id);
23137
23138       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23139         cp_lexer_consume_token (parser->lexer);
23140       else
23141         break;
23142     }
23143   cp_parser_consume_semicolon_at_end_of_statement (parser);
23144 }
23145
23146 /* Parse a list of Objective-C protocol references.
23147
23148    objc-protocol-refs-opt:
23149      objc-protocol-refs [opt]
23150
23151    objc-protocol-refs:
23152      < objc-identifier-list >
23153
23154    Returns a TREE_LIST of identifiers, if any.  */
23155
23156 static tree
23157 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
23158 {
23159   tree protorefs = NULL_TREE;
23160
23161   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
23162     {
23163       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
23164       protorefs = cp_parser_objc_identifier_list (parser);
23165       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
23166     }
23167
23168   return protorefs;
23169 }
23170
23171 /* Parse a Objective-C visibility specification.  */
23172
23173 static void
23174 cp_parser_objc_visibility_spec (cp_parser* parser)
23175 {
23176   cp_token *vis = cp_lexer_peek_token (parser->lexer);
23177
23178   switch (vis->keyword)
23179     {
23180     case RID_AT_PRIVATE:
23181       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
23182       break;
23183     case RID_AT_PROTECTED:
23184       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
23185       break;
23186     case RID_AT_PUBLIC:
23187       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
23188       break;
23189     case RID_AT_PACKAGE:
23190       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
23191       break;
23192     default:
23193       return;
23194     }
23195
23196   /* Eat '@private'/'@protected'/'@public'.  */
23197   cp_lexer_consume_token (parser->lexer);
23198 }
23199
23200 /* Parse an Objective-C method type.  Return 'true' if it is a class
23201    (+) method, and 'false' if it is an instance (-) method.  */
23202
23203 static inline bool
23204 cp_parser_objc_method_type (cp_parser* parser)
23205 {
23206   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
23207     return true;
23208   else
23209     return false;
23210 }
23211
23212 /* Parse an Objective-C protocol qualifier.  */
23213
23214 static tree
23215 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23216 {
23217   tree quals = NULL_TREE, node;
23218   cp_token *token = cp_lexer_peek_token (parser->lexer);
23219
23220   node = token->u.value;
23221
23222   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23223          && (node == ridpointers [(int) RID_IN]
23224              || node == ridpointers [(int) RID_OUT]
23225              || node == ridpointers [(int) RID_INOUT]
23226              || node == ridpointers [(int) RID_BYCOPY]
23227              || node == ridpointers [(int) RID_BYREF]
23228              || node == ridpointers [(int) RID_ONEWAY]))
23229     {
23230       quals = tree_cons (NULL_TREE, node, quals);
23231       cp_lexer_consume_token (parser->lexer);
23232       token = cp_lexer_peek_token (parser->lexer);
23233       node = token->u.value;
23234     }
23235
23236   return quals;
23237 }
23238
23239 /* Parse an Objective-C typename.  */
23240
23241 static tree
23242 cp_parser_objc_typename (cp_parser* parser)
23243 {
23244   tree type_name = NULL_TREE;
23245
23246   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23247     {
23248       tree proto_quals, cp_type = NULL_TREE;
23249
23250       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23251       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23252
23253       /* An ObjC type name may consist of just protocol qualifiers, in which
23254          case the type shall default to 'id'.  */
23255       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23256         {
23257           cp_type = cp_parser_type_id (parser);
23258           
23259           /* If the type could not be parsed, an error has already
23260              been produced.  For error recovery, behave as if it had
23261              not been specified, which will use the default type
23262              'id'.  */
23263           if (cp_type == error_mark_node)
23264             {
23265               cp_type = NULL_TREE;
23266               /* We need to skip to the closing parenthesis as
23267                  cp_parser_type_id() does not seem to do it for
23268                  us.  */
23269               cp_parser_skip_to_closing_parenthesis (parser,
23270                                                      /*recovering=*/true,
23271                                                      /*or_comma=*/false,
23272                                                      /*consume_paren=*/false);
23273             }
23274         }
23275
23276       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23277       type_name = build_tree_list (proto_quals, cp_type);
23278     }
23279
23280   return type_name;
23281 }
23282
23283 /* Check to see if TYPE refers to an Objective-C selector name.  */
23284
23285 static bool
23286 cp_parser_objc_selector_p (enum cpp_ttype type)
23287 {
23288   return (type == CPP_NAME || type == CPP_KEYWORD
23289           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23290           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23291           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23292           || type == CPP_XOR || type == CPP_XOR_EQ);
23293 }
23294
23295 /* Parse an Objective-C selector.  */
23296
23297 static tree
23298 cp_parser_objc_selector (cp_parser* parser)
23299 {
23300   cp_token *token = cp_lexer_consume_token (parser->lexer);
23301
23302   if (!cp_parser_objc_selector_p (token->type))
23303     {
23304       error_at (token->location, "invalid Objective-C++ selector name");
23305       return error_mark_node;
23306     }
23307
23308   /* C++ operator names are allowed to appear in ObjC selectors.  */
23309   switch (token->type)
23310     {
23311     case CPP_AND_AND: return get_identifier ("and");
23312     case CPP_AND_EQ: return get_identifier ("and_eq");
23313     case CPP_AND: return get_identifier ("bitand");
23314     case CPP_OR: return get_identifier ("bitor");
23315     case CPP_COMPL: return get_identifier ("compl");
23316     case CPP_NOT: return get_identifier ("not");
23317     case CPP_NOT_EQ: return get_identifier ("not_eq");
23318     case CPP_OR_OR: return get_identifier ("or");
23319     case CPP_OR_EQ: return get_identifier ("or_eq");
23320     case CPP_XOR: return get_identifier ("xor");
23321     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23322     default: return token->u.value;
23323     }
23324 }
23325
23326 /* Parse an Objective-C params list.  */
23327
23328 static tree
23329 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23330 {
23331   tree params = NULL_TREE;
23332   bool maybe_unary_selector_p = true;
23333   cp_token *token = cp_lexer_peek_token (parser->lexer);
23334
23335   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23336     {
23337       tree selector = NULL_TREE, type_name, identifier;
23338       tree parm_attr = NULL_TREE;
23339
23340       if (token->keyword == RID_ATTRIBUTE)
23341         break;
23342
23343       if (token->type != CPP_COLON)
23344         selector = cp_parser_objc_selector (parser);
23345
23346       /* Detect if we have a unary selector.  */
23347       if (maybe_unary_selector_p
23348           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23349         {
23350           params = selector; /* Might be followed by attributes.  */
23351           break;
23352         }
23353
23354       maybe_unary_selector_p = false;
23355       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23356         {
23357           /* Something went quite wrong.  There should be a colon
23358              here, but there is not.  Stop parsing parameters.  */
23359           break;
23360         }
23361       type_name = cp_parser_objc_typename (parser);
23362       /* New ObjC allows attributes on parameters too.  */
23363       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23364         parm_attr = cp_parser_attributes_opt (parser);
23365       identifier = cp_parser_identifier (parser);
23366
23367       params
23368         = chainon (params,
23369                    objc_build_keyword_decl (selector,
23370                                             type_name,
23371                                             identifier,
23372                                             parm_attr));
23373
23374       token = cp_lexer_peek_token (parser->lexer);
23375     }
23376
23377   if (params == NULL_TREE)
23378     {
23379       cp_parser_error (parser, "objective-c++ method declaration is expected");
23380       return error_mark_node;
23381     }
23382
23383   /* We allow tail attributes for the method.  */
23384   if (token->keyword == RID_ATTRIBUTE)
23385     {
23386       *attributes = cp_parser_attributes_opt (parser);
23387       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23388           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23389         return params;
23390       cp_parser_error (parser, 
23391                        "method attributes must be specified at the end");
23392       return error_mark_node;
23393     }
23394
23395   if (params == NULL_TREE)
23396     {
23397       cp_parser_error (parser, "objective-c++ method declaration is expected");
23398       return error_mark_node;
23399     }
23400   return params;
23401 }
23402
23403 /* Parse the non-keyword Objective-C params.  */
23404
23405 static tree
23406 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23407                                        tree* attributes)
23408 {
23409   tree params = make_node (TREE_LIST);
23410   cp_token *token = cp_lexer_peek_token (parser->lexer);
23411   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23412
23413   while (token->type == CPP_COMMA)
23414     {
23415       cp_parameter_declarator *parmdecl;
23416       tree parm;
23417
23418       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23419       token = cp_lexer_peek_token (parser->lexer);
23420
23421       if (token->type == CPP_ELLIPSIS)
23422         {
23423           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23424           *ellipsisp = true;
23425           token = cp_lexer_peek_token (parser->lexer);
23426           break;
23427         }
23428
23429       /* TODO: parse attributes for tail parameters.  */
23430       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23431       parm = grokdeclarator (parmdecl->declarator,
23432                              &parmdecl->decl_specifiers,
23433                              PARM, /*initialized=*/0,
23434                              /*attrlist=*/NULL);
23435
23436       chainon (params, build_tree_list (NULL_TREE, parm));
23437       token = cp_lexer_peek_token (parser->lexer);
23438     }
23439
23440   /* We allow tail attributes for the method.  */
23441   if (token->keyword == RID_ATTRIBUTE)
23442     {
23443       if (*attributes == NULL_TREE)
23444         {
23445           *attributes = cp_parser_attributes_opt (parser);
23446           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23447               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23448             return params;
23449         }
23450       else        
23451         /* We have an error, but parse the attributes, so that we can 
23452            carry on.  */
23453         *attributes = cp_parser_attributes_opt (parser);
23454
23455       cp_parser_error (parser, 
23456                        "method attributes must be specified at the end");
23457       return error_mark_node;
23458     }
23459
23460   return params;
23461 }
23462
23463 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23464
23465 static void
23466 cp_parser_objc_interstitial_code (cp_parser* parser)
23467 {
23468   cp_token *token = cp_lexer_peek_token (parser->lexer);
23469
23470   /* If the next token is `extern' and the following token is a string
23471      literal, then we have a linkage specification.  */
23472   if (token->keyword == RID_EXTERN
23473       && cp_parser_is_pure_string_literal
23474          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23475     cp_parser_linkage_specification (parser);
23476   /* Handle #pragma, if any.  */
23477   else if (token->type == CPP_PRAGMA)
23478     cp_parser_pragma (parser, pragma_external);
23479   /* Allow stray semicolons.  */
23480   else if (token->type == CPP_SEMICOLON)
23481     cp_lexer_consume_token (parser->lexer);
23482   /* Mark methods as optional or required, when building protocols.  */
23483   else if (token->keyword == RID_AT_OPTIONAL)
23484     {
23485       cp_lexer_consume_token (parser->lexer);
23486       objc_set_method_opt (true);
23487     }
23488   else if (token->keyword == RID_AT_REQUIRED)
23489     {
23490       cp_lexer_consume_token (parser->lexer);
23491       objc_set_method_opt (false);
23492     }
23493   else if (token->keyword == RID_NAMESPACE)
23494     cp_parser_namespace_definition (parser);
23495   /* Other stray characters must generate errors.  */
23496   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23497     {
23498       cp_lexer_consume_token (parser->lexer);
23499       error ("stray %qs between Objective-C++ methods",
23500              token->type == CPP_OPEN_BRACE ? "{" : "}");
23501     }
23502   /* Finally, try to parse a block-declaration, or a function-definition.  */
23503   else
23504     cp_parser_block_declaration (parser, /*statement_p=*/false);
23505 }
23506
23507 /* Parse a method signature.  */
23508
23509 static tree
23510 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23511 {
23512   tree rettype, kwdparms, optparms;
23513   bool ellipsis = false;
23514   bool is_class_method;
23515
23516   is_class_method = cp_parser_objc_method_type (parser);
23517   rettype = cp_parser_objc_typename (parser);
23518   *attributes = NULL_TREE;
23519   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23520   if (kwdparms == error_mark_node)
23521     return error_mark_node;
23522   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23523   if (optparms == error_mark_node)
23524     return error_mark_node;
23525
23526   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23527 }
23528
23529 static bool
23530 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23531 {
23532   tree tattr;  
23533   cp_lexer_save_tokens (parser->lexer);
23534   tattr = cp_parser_attributes_opt (parser);
23535   gcc_assert (tattr) ;
23536   
23537   /* If the attributes are followed by a method introducer, this is not allowed.
23538      Dump the attributes and flag the situation.  */
23539   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23540       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23541     return true;
23542
23543   /* Otherwise, the attributes introduce some interstitial code, possibly so
23544      rewind to allow that check.  */
23545   cp_lexer_rollback_tokens (parser->lexer);
23546   return false;  
23547 }
23548
23549 /* Parse an Objective-C method prototype list.  */
23550
23551 static void
23552 cp_parser_objc_method_prototype_list (cp_parser* parser)
23553 {
23554   cp_token *token = cp_lexer_peek_token (parser->lexer);
23555
23556   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23557     {
23558       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23559         {
23560           tree attributes, sig;
23561           bool is_class_method;
23562           if (token->type == CPP_PLUS)
23563             is_class_method = true;
23564           else
23565             is_class_method = false;
23566           sig = cp_parser_objc_method_signature (parser, &attributes);
23567           if (sig == error_mark_node)
23568             {
23569               cp_parser_skip_to_end_of_block_or_statement (parser);
23570               token = cp_lexer_peek_token (parser->lexer);
23571               continue;
23572             }
23573           objc_add_method_declaration (is_class_method, sig, attributes);
23574           cp_parser_consume_semicolon_at_end_of_statement (parser);
23575         }
23576       else if (token->keyword == RID_AT_PROPERTY)
23577         cp_parser_objc_at_property_declaration (parser);
23578       else if (token->keyword == RID_ATTRIBUTE 
23579                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23580         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23581                     OPT_Wattributes, 
23582                     "prefix attributes are ignored for methods");
23583       else
23584         /* Allow for interspersed non-ObjC++ code.  */
23585         cp_parser_objc_interstitial_code (parser);
23586
23587       token = cp_lexer_peek_token (parser->lexer);
23588     }
23589
23590   if (token->type != CPP_EOF)
23591     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23592   else
23593     cp_parser_error (parser, "expected %<@end%>");
23594
23595   objc_finish_interface ();
23596 }
23597
23598 /* Parse an Objective-C method definition list.  */
23599
23600 static void
23601 cp_parser_objc_method_definition_list (cp_parser* parser)
23602 {
23603   cp_token *token = cp_lexer_peek_token (parser->lexer);
23604
23605   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23606     {
23607       tree meth;
23608
23609       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23610         {
23611           cp_token *ptk;
23612           tree sig, attribute;
23613           bool is_class_method;
23614           if (token->type == CPP_PLUS)
23615             is_class_method = true;
23616           else
23617             is_class_method = false;
23618           push_deferring_access_checks (dk_deferred);
23619           sig = cp_parser_objc_method_signature (parser, &attribute);
23620           if (sig == error_mark_node)
23621             {
23622               cp_parser_skip_to_end_of_block_or_statement (parser);
23623               token = cp_lexer_peek_token (parser->lexer);
23624               continue;
23625             }
23626           objc_start_method_definition (is_class_method, sig, attribute,
23627                                         NULL_TREE);
23628
23629           /* For historical reasons, we accept an optional semicolon.  */
23630           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23631             cp_lexer_consume_token (parser->lexer);
23632
23633           ptk = cp_lexer_peek_token (parser->lexer);
23634           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23635                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23636             {
23637               perform_deferred_access_checks ();
23638               stop_deferring_access_checks ();
23639               meth = cp_parser_function_definition_after_declarator (parser,
23640                                                                      false);
23641               pop_deferring_access_checks ();
23642               objc_finish_method_definition (meth);
23643             }
23644         }
23645       /* The following case will be removed once @synthesize is
23646          completely implemented.  */
23647       else if (token->keyword == RID_AT_PROPERTY)
23648         cp_parser_objc_at_property_declaration (parser);
23649       else if (token->keyword == RID_AT_SYNTHESIZE)
23650         cp_parser_objc_at_synthesize_declaration (parser);
23651       else if (token->keyword == RID_AT_DYNAMIC)
23652         cp_parser_objc_at_dynamic_declaration (parser);
23653       else if (token->keyword == RID_ATTRIBUTE 
23654                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23655         warning_at (token->location, OPT_Wattributes,
23656                     "prefix attributes are ignored for methods");
23657       else
23658         /* Allow for interspersed non-ObjC++ code.  */
23659         cp_parser_objc_interstitial_code (parser);
23660
23661       token = cp_lexer_peek_token (parser->lexer);
23662     }
23663
23664   if (token->type != CPP_EOF)
23665     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23666   else
23667     cp_parser_error (parser, "expected %<@end%>");
23668
23669   objc_finish_implementation ();
23670 }
23671
23672 /* Parse Objective-C ivars.  */
23673
23674 static void
23675 cp_parser_objc_class_ivars (cp_parser* parser)
23676 {
23677   cp_token *token = cp_lexer_peek_token (parser->lexer);
23678
23679   if (token->type != CPP_OPEN_BRACE)
23680     return;     /* No ivars specified.  */
23681
23682   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23683   token = cp_lexer_peek_token (parser->lexer);
23684
23685   while (token->type != CPP_CLOSE_BRACE 
23686         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23687     {
23688       cp_decl_specifier_seq declspecs;
23689       int decl_class_or_enum_p;
23690       tree prefix_attributes;
23691
23692       cp_parser_objc_visibility_spec (parser);
23693
23694       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23695         break;
23696
23697       cp_parser_decl_specifier_seq (parser,
23698                                     CP_PARSER_FLAGS_OPTIONAL,
23699                                     &declspecs,
23700                                     &decl_class_or_enum_p);
23701
23702       /* auto, register, static, extern, mutable.  */
23703       if (declspecs.storage_class != sc_none)
23704         {
23705           cp_parser_error (parser, "invalid type for instance variable");         
23706           declspecs.storage_class = sc_none;
23707         }
23708
23709       /* __thread.  */
23710       if (declspecs.specs[(int) ds_thread])
23711         {
23712           cp_parser_error (parser, "invalid type for instance variable");
23713           declspecs.specs[(int) ds_thread] = 0;
23714         }
23715       
23716       /* typedef.  */
23717       if (declspecs.specs[(int) ds_typedef])
23718         {
23719           cp_parser_error (parser, "invalid type for instance variable");
23720           declspecs.specs[(int) ds_typedef] = 0;
23721         }
23722
23723       prefix_attributes = declspecs.attributes;
23724       declspecs.attributes = NULL_TREE;
23725
23726       /* Keep going until we hit the `;' at the end of the
23727          declaration.  */
23728       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23729         {
23730           tree width = NULL_TREE, attributes, first_attribute, decl;
23731           cp_declarator *declarator = NULL;
23732           int ctor_dtor_or_conv_p;
23733
23734           /* Check for a (possibly unnamed) bitfield declaration.  */
23735           token = cp_lexer_peek_token (parser->lexer);
23736           if (token->type == CPP_COLON)
23737             goto eat_colon;
23738
23739           if (token->type == CPP_NAME
23740               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23741                   == CPP_COLON))
23742             {
23743               /* Get the name of the bitfield.  */
23744               declarator = make_id_declarator (NULL_TREE,
23745                                                cp_parser_identifier (parser),
23746                                                sfk_none);
23747
23748              eat_colon:
23749               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23750               /* Get the width of the bitfield.  */
23751               width
23752                 = cp_parser_constant_expression (parser,
23753                                                  /*allow_non_constant=*/false,
23754                                                  NULL);
23755             }
23756           else
23757             {
23758               /* Parse the declarator.  */
23759               declarator
23760                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23761                                         &ctor_dtor_or_conv_p,
23762                                         /*parenthesized_p=*/NULL,
23763                                         /*member_p=*/false);
23764             }
23765
23766           /* Look for attributes that apply to the ivar.  */
23767           attributes = cp_parser_attributes_opt (parser);
23768           /* Remember which attributes are prefix attributes and
23769              which are not.  */
23770           first_attribute = attributes;
23771           /* Combine the attributes.  */
23772           attributes = chainon (prefix_attributes, attributes);
23773
23774           if (width)
23775               /* Create the bitfield declaration.  */
23776               decl = grokbitfield (declarator, &declspecs,
23777                                    width,
23778                                    attributes);
23779           else
23780             decl = grokfield (declarator, &declspecs,
23781                               NULL_TREE, /*init_const_expr_p=*/false,
23782                               NULL_TREE, attributes);
23783
23784           /* Add the instance variable.  */
23785           if (decl != error_mark_node && decl != NULL_TREE)
23786             objc_add_instance_variable (decl);
23787
23788           /* Reset PREFIX_ATTRIBUTES.  */
23789           while (attributes && TREE_CHAIN (attributes) != first_attribute)
23790             attributes = TREE_CHAIN (attributes);
23791           if (attributes)
23792             TREE_CHAIN (attributes) = NULL_TREE;
23793
23794           token = cp_lexer_peek_token (parser->lexer);
23795
23796           if (token->type == CPP_COMMA)
23797             {
23798               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23799               continue;
23800             }
23801           break;
23802         }
23803
23804       cp_parser_consume_semicolon_at_end_of_statement (parser);
23805       token = cp_lexer_peek_token (parser->lexer);
23806     }
23807
23808   if (token->keyword == RID_AT_END)
23809     cp_parser_error (parser, "expected %<}%>");
23810
23811   /* Do not consume the RID_AT_END, so it will be read again as terminating
23812      the @interface of @implementation.  */ 
23813   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23814     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
23815     
23816   /* For historical reasons, we accept an optional semicolon.  */
23817   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23818     cp_lexer_consume_token (parser->lexer);
23819 }
23820
23821 /* Parse an Objective-C protocol declaration.  */
23822
23823 static void
23824 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23825 {
23826   tree proto, protorefs;
23827   cp_token *tok;
23828
23829   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23830   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23831     {
23832       tok = cp_lexer_peek_token (parser->lexer);
23833       error_at (tok->location, "identifier expected after %<@protocol%>");
23834       cp_parser_consume_semicolon_at_end_of_statement (parser);
23835       return;
23836     }
23837
23838   /* See if we have a forward declaration or a definition.  */
23839   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23840
23841   /* Try a forward declaration first.  */
23842   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23843     {
23844       while (true)
23845         {
23846           tree id;
23847           
23848           id = cp_parser_identifier (parser);
23849           if (id == error_mark_node)
23850             break;
23851           
23852           objc_declare_protocol (id, attributes);
23853           
23854           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23855             cp_lexer_consume_token (parser->lexer);
23856           else
23857             break;
23858         }
23859       cp_parser_consume_semicolon_at_end_of_statement (parser);
23860     }
23861
23862   /* Ok, we got a full-fledged definition (or at least should).  */
23863   else
23864     {
23865       proto = cp_parser_identifier (parser);
23866       protorefs = cp_parser_objc_protocol_refs_opt (parser);
23867       objc_start_protocol (proto, protorefs, attributes);
23868       cp_parser_objc_method_prototype_list (parser);
23869     }
23870 }
23871
23872 /* Parse an Objective-C superclass or category.  */
23873
23874 static void
23875 cp_parser_objc_superclass_or_category (cp_parser *parser, 
23876                                        bool iface_p,
23877                                        tree *super,
23878                                        tree *categ, bool *is_class_extension)
23879 {
23880   cp_token *next = cp_lexer_peek_token (parser->lexer);
23881
23882   *super = *categ = NULL_TREE;
23883   *is_class_extension = false;
23884   if (next->type == CPP_COLON)
23885     {
23886       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23887       *super = cp_parser_identifier (parser);
23888     }
23889   else if (next->type == CPP_OPEN_PAREN)
23890     {
23891       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23892
23893       /* If there is no category name, and this is an @interface, we
23894          have a class extension.  */
23895       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23896         {
23897           *categ = NULL_TREE;
23898           *is_class_extension = true;
23899         }
23900       else
23901         *categ = cp_parser_identifier (parser);
23902
23903       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23904     }
23905 }
23906
23907 /* Parse an Objective-C class interface.  */
23908
23909 static void
23910 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23911 {
23912   tree name, super, categ, protos;
23913   bool is_class_extension;
23914
23915   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
23916   name = cp_parser_identifier (parser);
23917   if (name == error_mark_node)
23918     {
23919       /* It's hard to recover because even if valid @interface stuff
23920          is to follow, we can't compile it (or validate it) if we
23921          don't even know which class it refers to.  Let's assume this
23922          was a stray '@interface' token in the stream and skip it.
23923       */
23924       return;
23925     }
23926   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23927                                          &is_class_extension);
23928   protos = cp_parser_objc_protocol_refs_opt (parser);
23929
23930   /* We have either a class or a category on our hands.  */
23931   if (categ || is_class_extension)
23932     objc_start_category_interface (name, categ, protos, attributes);
23933   else
23934     {
23935       objc_start_class_interface (name, super, protos, attributes);
23936       /* Handle instance variable declarations, if any.  */
23937       cp_parser_objc_class_ivars (parser);
23938       objc_continue_interface ();
23939     }
23940
23941   cp_parser_objc_method_prototype_list (parser);
23942 }
23943
23944 /* Parse an Objective-C class implementation.  */
23945
23946 static void
23947 cp_parser_objc_class_implementation (cp_parser* parser)
23948 {
23949   tree name, super, categ;
23950   bool is_class_extension;
23951
23952   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
23953   name = cp_parser_identifier (parser);
23954   if (name == error_mark_node)
23955     {
23956       /* It's hard to recover because even if valid @implementation
23957          stuff is to follow, we can't compile it (or validate it) if
23958          we don't even know which class it refers to.  Let's assume
23959          this was a stray '@implementation' token in the stream and
23960          skip it.
23961       */
23962       return;
23963     }
23964   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23965                                          &is_class_extension);
23966
23967   /* We have either a class or a category on our hands.  */
23968   if (categ)
23969     objc_start_category_implementation (name, categ);
23970   else
23971     {
23972       objc_start_class_implementation (name, super);
23973       /* Handle instance variable declarations, if any.  */
23974       cp_parser_objc_class_ivars (parser);
23975       objc_continue_implementation ();
23976     }
23977
23978   cp_parser_objc_method_definition_list (parser);
23979 }
23980
23981 /* Consume the @end token and finish off the implementation.  */
23982
23983 static void
23984 cp_parser_objc_end_implementation (cp_parser* parser)
23985 {
23986   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23987   objc_finish_implementation ();
23988 }
23989
23990 /* Parse an Objective-C declaration.  */
23991
23992 static void
23993 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23994 {
23995   /* Try to figure out what kind of declaration is present.  */
23996   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23997
23998   if (attributes)
23999     switch (kwd->keyword)
24000       {
24001         case RID_AT_ALIAS:
24002         case RID_AT_CLASS:
24003         case RID_AT_END:
24004           error_at (kwd->location, "attributes may not be specified before"
24005                     " the %<@%D%> Objective-C++ keyword",
24006                     kwd->u.value);
24007           attributes = NULL;
24008           break;
24009         case RID_AT_IMPLEMENTATION:
24010           warning_at (kwd->location, OPT_Wattributes,
24011                       "prefix attributes are ignored before %<@%D%>",
24012                       kwd->u.value);
24013           attributes = NULL;
24014         default:
24015           break;
24016       }
24017
24018   switch (kwd->keyword)
24019     {
24020     case RID_AT_ALIAS:
24021       cp_parser_objc_alias_declaration (parser);
24022       break;
24023     case RID_AT_CLASS:
24024       cp_parser_objc_class_declaration (parser);
24025       break;
24026     case RID_AT_PROTOCOL:
24027       cp_parser_objc_protocol_declaration (parser, attributes);
24028       break;
24029     case RID_AT_INTERFACE:
24030       cp_parser_objc_class_interface (parser, attributes);
24031       break;
24032     case RID_AT_IMPLEMENTATION:
24033       cp_parser_objc_class_implementation (parser);
24034       break;
24035     case RID_AT_END:
24036       cp_parser_objc_end_implementation (parser);
24037       break;
24038     default:
24039       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24040                 kwd->u.value);
24041       cp_parser_skip_to_end_of_block_or_statement (parser);
24042     }
24043 }
24044
24045 /* Parse an Objective-C try-catch-finally statement.
24046
24047    objc-try-catch-finally-stmt:
24048      @try compound-statement objc-catch-clause-seq [opt]
24049        objc-finally-clause [opt]
24050
24051    objc-catch-clause-seq:
24052      objc-catch-clause objc-catch-clause-seq [opt]
24053
24054    objc-catch-clause:
24055      @catch ( objc-exception-declaration ) compound-statement
24056
24057    objc-finally-clause:
24058      @finally compound-statement
24059
24060    objc-exception-declaration:
24061      parameter-declaration
24062      '...'
24063
24064    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
24065
24066    Returns NULL_TREE.
24067
24068    PS: This function is identical to c_parser_objc_try_catch_finally_statement
24069    for C.  Keep them in sync.  */   
24070
24071 static tree
24072 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
24073 {
24074   location_t location;
24075   tree stmt;
24076
24077   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
24078   location = cp_lexer_peek_token (parser->lexer)->location;
24079   objc_maybe_warn_exceptions (location);
24080   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
24081      node, lest it get absorbed into the surrounding block.  */
24082   stmt = push_stmt_list ();
24083   cp_parser_compound_statement (parser, NULL, false, false);
24084   objc_begin_try_stmt (location, pop_stmt_list (stmt));
24085
24086   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
24087     {
24088       cp_parameter_declarator *parm;
24089       tree parameter_declaration = error_mark_node;
24090       bool seen_open_paren = false;
24091
24092       cp_lexer_consume_token (parser->lexer);
24093       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24094         seen_open_paren = true;
24095       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
24096         {
24097           /* We have "@catch (...)" (where the '...' are literally
24098              what is in the code).  Skip the '...'.
24099              parameter_declaration is set to NULL_TREE, and
24100              objc_being_catch_clauses() knows that that means
24101              '...'.  */
24102           cp_lexer_consume_token (parser->lexer);
24103           parameter_declaration = NULL_TREE;
24104         }
24105       else
24106         {
24107           /* We have "@catch (NSException *exception)" or something
24108              like that.  Parse the parameter declaration.  */
24109           parm = cp_parser_parameter_declaration (parser, false, NULL);
24110           if (parm == NULL)
24111             parameter_declaration = error_mark_node;
24112           else
24113             parameter_declaration = grokdeclarator (parm->declarator,
24114                                                     &parm->decl_specifiers,
24115                                                     PARM, /*initialized=*/0,
24116                                                     /*attrlist=*/NULL);
24117         }
24118       if (seen_open_paren)
24119         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24120       else
24121         {
24122           /* If there was no open parenthesis, we are recovering from
24123              an error, and we are trying to figure out what mistake
24124              the user has made.  */
24125
24126           /* If there is an immediate closing parenthesis, the user
24127              probably forgot the opening one (ie, they typed "@catch
24128              NSException *e)".  Parse the closing parenthesis and keep
24129              going.  */
24130           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
24131             cp_lexer_consume_token (parser->lexer);
24132           
24133           /* If these is no immediate closing parenthesis, the user
24134              probably doesn't know that parenthesis are required at
24135              all (ie, they typed "@catch NSException *e").  So, just
24136              forget about the closing parenthesis and keep going.  */
24137         }
24138       objc_begin_catch_clause (parameter_declaration);
24139       cp_parser_compound_statement (parser, NULL, false, false);
24140       objc_finish_catch_clause ();
24141     }
24142   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
24143     {
24144       cp_lexer_consume_token (parser->lexer);
24145       location = cp_lexer_peek_token (parser->lexer)->location;
24146       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
24147          node, lest it get absorbed into the surrounding block.  */
24148       stmt = push_stmt_list ();
24149       cp_parser_compound_statement (parser, NULL, false, false);
24150       objc_build_finally_clause (location, pop_stmt_list (stmt));
24151     }
24152
24153   return objc_finish_try_stmt ();
24154 }
24155
24156 /* Parse an Objective-C synchronized statement.
24157
24158    objc-synchronized-stmt:
24159      @synchronized ( expression ) compound-statement
24160
24161    Returns NULL_TREE.  */
24162
24163 static tree
24164 cp_parser_objc_synchronized_statement (cp_parser *parser)
24165 {
24166   location_t location;
24167   tree lock, stmt;
24168
24169   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
24170
24171   location = cp_lexer_peek_token (parser->lexer)->location;
24172   objc_maybe_warn_exceptions (location);
24173   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
24174   lock = cp_parser_expression (parser, false, NULL);
24175   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
24176
24177   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
24178      node, lest it get absorbed into the surrounding block.  */
24179   stmt = push_stmt_list ();
24180   cp_parser_compound_statement (parser, NULL, false, false);
24181
24182   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
24183 }
24184
24185 /* Parse an Objective-C throw statement.
24186
24187    objc-throw-stmt:
24188      @throw assignment-expression [opt] ;
24189
24190    Returns a constructed '@throw' statement.  */
24191
24192 static tree
24193 cp_parser_objc_throw_statement (cp_parser *parser)
24194 {
24195   tree expr = NULL_TREE;
24196   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
24197
24198   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
24199
24200   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24201     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
24202
24203   cp_parser_consume_semicolon_at_end_of_statement (parser);
24204
24205   return objc_build_throw_stmt (loc, expr);
24206 }
24207
24208 /* Parse an Objective-C statement.  */
24209
24210 static tree
24211 cp_parser_objc_statement (cp_parser * parser)
24212 {
24213   /* Try to figure out what kind of declaration is present.  */
24214   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24215
24216   switch (kwd->keyword)
24217     {
24218     case RID_AT_TRY:
24219       return cp_parser_objc_try_catch_finally_statement (parser);
24220     case RID_AT_SYNCHRONIZED:
24221       return cp_parser_objc_synchronized_statement (parser);
24222     case RID_AT_THROW:
24223       return cp_parser_objc_throw_statement (parser);
24224     default:
24225       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24226                kwd->u.value);
24227       cp_parser_skip_to_end_of_block_or_statement (parser);
24228     }
24229
24230   return error_mark_node;
24231 }
24232
24233 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24234    look ahead to see if an objc keyword follows the attributes.  This
24235    is to detect the use of prefix attributes on ObjC @interface and 
24236    @protocol.  */
24237
24238 static bool
24239 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24240 {
24241   cp_lexer_save_tokens (parser->lexer);
24242   *attrib = cp_parser_attributes_opt (parser);
24243   gcc_assert (*attrib);
24244   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24245     {
24246       cp_lexer_commit_tokens (parser->lexer);
24247       return true;
24248     }
24249   cp_lexer_rollback_tokens (parser->lexer);
24250   return false;  
24251 }
24252
24253 /* This routine is a minimal replacement for
24254    c_parser_struct_declaration () used when parsing the list of
24255    types/names or ObjC++ properties.  For example, when parsing the
24256    code
24257
24258    @property (readonly) int a, b, c;
24259
24260    this function is responsible for parsing "int a, int b, int c" and
24261    returning the declarations as CHAIN of DECLs.
24262
24263    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24264    similar parsing.  */
24265 static tree
24266 cp_parser_objc_struct_declaration (cp_parser *parser)
24267 {
24268   tree decls = NULL_TREE;
24269   cp_decl_specifier_seq declspecs;
24270   int decl_class_or_enum_p;
24271   tree prefix_attributes;
24272
24273   cp_parser_decl_specifier_seq (parser,
24274                                 CP_PARSER_FLAGS_NONE,
24275                                 &declspecs,
24276                                 &decl_class_or_enum_p);
24277
24278   if (declspecs.type == error_mark_node)
24279     return error_mark_node;
24280
24281   /* auto, register, static, extern, mutable.  */
24282   if (declspecs.storage_class != sc_none)
24283     {
24284       cp_parser_error (parser, "invalid type for property");
24285       declspecs.storage_class = sc_none;
24286     }
24287   
24288   /* __thread.  */
24289   if (declspecs.specs[(int) ds_thread])
24290     {
24291       cp_parser_error (parser, "invalid type for property");
24292       declspecs.specs[(int) ds_thread] = 0;
24293     }
24294   
24295   /* typedef.  */
24296   if (declspecs.specs[(int) ds_typedef])
24297     {
24298       cp_parser_error (parser, "invalid type for property");
24299       declspecs.specs[(int) ds_typedef] = 0;
24300     }
24301
24302   prefix_attributes = declspecs.attributes;
24303   declspecs.attributes = NULL_TREE;
24304
24305   /* Keep going until we hit the `;' at the end of the declaration. */
24306   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24307     {
24308       tree attributes, first_attribute, decl;
24309       cp_declarator *declarator;
24310       cp_token *token;
24311
24312       /* Parse the declarator.  */
24313       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24314                                          NULL, NULL, false);
24315
24316       /* Look for attributes that apply to the ivar.  */
24317       attributes = cp_parser_attributes_opt (parser);
24318       /* Remember which attributes are prefix attributes and
24319          which are not.  */
24320       first_attribute = attributes;
24321       /* Combine the attributes.  */
24322       attributes = chainon (prefix_attributes, attributes);
24323       
24324       decl = grokfield (declarator, &declspecs,
24325                         NULL_TREE, /*init_const_expr_p=*/false,
24326                         NULL_TREE, attributes);
24327
24328       if (decl == error_mark_node || decl == NULL_TREE)
24329         return error_mark_node;
24330       
24331       /* Reset PREFIX_ATTRIBUTES.  */
24332       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24333         attributes = TREE_CHAIN (attributes);
24334       if (attributes)
24335         TREE_CHAIN (attributes) = NULL_TREE;
24336
24337       DECL_CHAIN (decl) = decls;
24338       decls = decl;
24339
24340       token = cp_lexer_peek_token (parser->lexer);
24341       if (token->type == CPP_COMMA)
24342         {
24343           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24344           continue;
24345         }
24346       else
24347         break;
24348     }
24349   return decls;
24350 }
24351
24352 /* Parse an Objective-C @property declaration.  The syntax is:
24353
24354    objc-property-declaration:
24355      '@property' objc-property-attributes[opt] struct-declaration ;
24356
24357    objc-property-attributes:
24358     '(' objc-property-attribute-list ')'
24359
24360    objc-property-attribute-list:
24361      objc-property-attribute
24362      objc-property-attribute-list, objc-property-attribute
24363
24364    objc-property-attribute
24365      'getter' = identifier
24366      'setter' = identifier
24367      'readonly'
24368      'readwrite'
24369      'assign'
24370      'retain'
24371      'copy'
24372      'nonatomic'
24373
24374   For example:
24375     @property NSString *name;
24376     @property (readonly) id object;
24377     @property (retain, nonatomic, getter=getTheName) id name;
24378     @property int a, b, c;
24379
24380    PS: This function is identical to
24381    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24382 static void 
24383 cp_parser_objc_at_property_declaration (cp_parser *parser)
24384 {
24385   /* The following variables hold the attributes of the properties as
24386      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24387      seen.  When we see an attribute, we set them to 'true' (if they
24388      are boolean properties) or to the identifier (if they have an
24389      argument, ie, for getter and setter).  Note that here we only
24390      parse the list of attributes, check the syntax and accumulate the
24391      attributes that we find.  objc_add_property_declaration() will
24392      then process the information.  */
24393   bool property_assign = false;
24394   bool property_copy = false;
24395   tree property_getter_ident = NULL_TREE;
24396   bool property_nonatomic = false;
24397   bool property_readonly = false;
24398   bool property_readwrite = false;
24399   bool property_retain = false;
24400   tree property_setter_ident = NULL_TREE;
24401
24402   /* 'properties' is the list of properties that we read.  Usually a
24403      single one, but maybe more (eg, in "@property int a, b, c;" there
24404      are three).  */
24405   tree properties;
24406   location_t loc;
24407
24408   loc = cp_lexer_peek_token (parser->lexer)->location;
24409
24410   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24411
24412   /* Parse the optional attribute list...  */
24413   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24414     {
24415       /* Eat the '('.  */
24416       cp_lexer_consume_token (parser->lexer);
24417
24418       while (true)
24419         {
24420           bool syntax_error = false;
24421           cp_token *token = cp_lexer_peek_token (parser->lexer);
24422           enum rid keyword;
24423
24424           if (token->type != CPP_NAME)
24425             {
24426               cp_parser_error (parser, "expected identifier");
24427               break;
24428             }
24429           keyword = C_RID_CODE (token->u.value);
24430           cp_lexer_consume_token (parser->lexer);
24431           switch (keyword)
24432             {
24433             case RID_ASSIGN:    property_assign = true;    break;
24434             case RID_COPY:      property_copy = true;      break;
24435             case RID_NONATOMIC: property_nonatomic = true; break;
24436             case RID_READONLY:  property_readonly = true;  break;
24437             case RID_READWRITE: property_readwrite = true; break;
24438             case RID_RETAIN:    property_retain = true;    break;
24439
24440             case RID_GETTER:
24441             case RID_SETTER:
24442               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24443                 {
24444                   if (keyword == RID_GETTER)
24445                     cp_parser_error (parser,
24446                                      "missing %<=%> (after %<getter%> attribute)");
24447                   else
24448                     cp_parser_error (parser,
24449                                      "missing %<=%> (after %<setter%> attribute)");
24450                   syntax_error = true;
24451                   break;
24452                 }
24453               cp_lexer_consume_token (parser->lexer); /* eat the = */
24454               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24455                 {
24456                   cp_parser_error (parser, "expected identifier");
24457                   syntax_error = true;
24458                   break;
24459                 }
24460               if (keyword == RID_SETTER)
24461                 {
24462                   if (property_setter_ident != NULL_TREE)
24463                     {
24464                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24465                       cp_lexer_consume_token (parser->lexer);
24466                     }
24467                   else
24468                     property_setter_ident = cp_parser_objc_selector (parser);
24469                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24470                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24471                   else
24472                     cp_lexer_consume_token (parser->lexer);
24473                 }
24474               else
24475                 {
24476                   if (property_getter_ident != NULL_TREE)
24477                     {
24478                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24479                       cp_lexer_consume_token (parser->lexer);
24480                     }
24481                   else
24482                     property_getter_ident = cp_parser_objc_selector (parser);
24483                 }
24484               break;
24485             default:
24486               cp_parser_error (parser, "unknown property attribute");
24487               syntax_error = true;
24488               break;
24489             }
24490
24491           if (syntax_error)
24492             break;
24493
24494           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24495             cp_lexer_consume_token (parser->lexer);
24496           else
24497             break;
24498         }
24499
24500       /* FIXME: "@property (setter, assign);" will generate a spurious
24501          "error: expected â€˜)’ before â€˜,’ token".  This is because
24502          cp_parser_require, unlike the C counterpart, will produce an
24503          error even if we are in error recovery.  */
24504       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24505         {
24506           cp_parser_skip_to_closing_parenthesis (parser,
24507                                                  /*recovering=*/true,
24508                                                  /*or_comma=*/false,
24509                                                  /*consume_paren=*/true);
24510         }
24511     }
24512
24513   /* ... and the property declaration(s).  */
24514   properties = cp_parser_objc_struct_declaration (parser);
24515
24516   if (properties == error_mark_node)
24517     {
24518       cp_parser_skip_to_end_of_statement (parser);
24519       /* If the next token is now a `;', consume it.  */
24520       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24521         cp_lexer_consume_token (parser->lexer);
24522       return;
24523     }
24524
24525   if (properties == NULL_TREE)
24526     cp_parser_error (parser, "expected identifier");
24527   else
24528     {
24529       /* Comma-separated properties are chained together in
24530          reverse order; add them one by one.  */
24531       properties = nreverse (properties);
24532       
24533       for (; properties; properties = TREE_CHAIN (properties))
24534         objc_add_property_declaration (loc, copy_node (properties),
24535                                        property_readonly, property_readwrite,
24536                                        property_assign, property_retain,
24537                                        property_copy, property_nonatomic,
24538                                        property_getter_ident, property_setter_ident);
24539     }
24540   
24541   cp_parser_consume_semicolon_at_end_of_statement (parser);
24542 }
24543
24544 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24545
24546    objc-synthesize-declaration:
24547      @synthesize objc-synthesize-identifier-list ;
24548
24549    objc-synthesize-identifier-list:
24550      objc-synthesize-identifier
24551      objc-synthesize-identifier-list, objc-synthesize-identifier
24552
24553    objc-synthesize-identifier
24554      identifier
24555      identifier = identifier
24556
24557   For example:
24558     @synthesize MyProperty;
24559     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24560
24561   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24562   for C.  Keep them in sync.
24563 */
24564 static void 
24565 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24566 {
24567   tree list = NULL_TREE;
24568   location_t loc;
24569   loc = cp_lexer_peek_token (parser->lexer)->location;
24570
24571   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24572   while (true)
24573     {
24574       tree property, ivar;
24575       property = cp_parser_identifier (parser);
24576       if (property == error_mark_node)
24577         {
24578           cp_parser_consume_semicolon_at_end_of_statement (parser);
24579           return;
24580         }
24581       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24582         {
24583           cp_lexer_consume_token (parser->lexer);
24584           ivar = cp_parser_identifier (parser);
24585           if (ivar == error_mark_node)
24586             {
24587               cp_parser_consume_semicolon_at_end_of_statement (parser);
24588               return;
24589             }
24590         }
24591       else
24592         ivar = NULL_TREE;
24593       list = chainon (list, build_tree_list (ivar, property));
24594       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24595         cp_lexer_consume_token (parser->lexer);
24596       else
24597         break;
24598     }
24599   cp_parser_consume_semicolon_at_end_of_statement (parser);
24600   objc_add_synthesize_declaration (loc, list);
24601 }
24602
24603 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24604
24605    objc-dynamic-declaration:
24606      @dynamic identifier-list ;
24607
24608    For example:
24609      @dynamic MyProperty;
24610      @dynamic MyProperty, AnotherProperty;
24611
24612   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24613   for C.  Keep them in sync.
24614 */
24615 static void 
24616 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24617 {
24618   tree list = NULL_TREE;
24619   location_t loc;
24620   loc = cp_lexer_peek_token (parser->lexer)->location;
24621
24622   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24623   while (true)
24624     {
24625       tree property;
24626       property = cp_parser_identifier (parser);
24627       if (property == error_mark_node)
24628         {
24629           cp_parser_consume_semicolon_at_end_of_statement (parser);
24630           return;
24631         }
24632       list = chainon (list, build_tree_list (NULL, property));
24633       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24634         cp_lexer_consume_token (parser->lexer);
24635       else
24636         break;
24637     }
24638   cp_parser_consume_semicolon_at_end_of_statement (parser);
24639   objc_add_dynamic_declaration (loc, list);
24640 }
24641
24642 \f
24643 /* OpenMP 2.5 parsing routines.  */
24644
24645 /* Returns name of the next clause.
24646    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24647    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24648    returned and the token is consumed.  */
24649
24650 static pragma_omp_clause
24651 cp_parser_omp_clause_name (cp_parser *parser)
24652 {
24653   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24654
24655   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24656     result = PRAGMA_OMP_CLAUSE_IF;
24657   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24658     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24659   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24660     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24661   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24662     {
24663       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24664       const char *p = IDENTIFIER_POINTER (id);
24665
24666       switch (p[0])
24667         {
24668         case 'c':
24669           if (!strcmp ("collapse", p))
24670             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24671           else if (!strcmp ("copyin", p))
24672             result = PRAGMA_OMP_CLAUSE_COPYIN;
24673           else if (!strcmp ("copyprivate", p))
24674             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24675           break;
24676         case 'f':
24677           if (!strcmp ("final", p))
24678             result = PRAGMA_OMP_CLAUSE_FINAL;
24679           else if (!strcmp ("firstprivate", p))
24680             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24681           break;
24682         case 'l':
24683           if (!strcmp ("lastprivate", p))
24684             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24685           break;
24686         case 'm':
24687           if (!strcmp ("mergeable", p))
24688             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24689           break;
24690         case 'n':
24691           if (!strcmp ("nowait", p))
24692             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24693           else if (!strcmp ("num_threads", p))
24694             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24695           break;
24696         case 'o':
24697           if (!strcmp ("ordered", p))
24698             result = PRAGMA_OMP_CLAUSE_ORDERED;
24699           break;
24700         case 'r':
24701           if (!strcmp ("reduction", p))
24702             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24703           break;
24704         case 's':
24705           if (!strcmp ("schedule", p))
24706             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24707           else if (!strcmp ("shared", p))
24708             result = PRAGMA_OMP_CLAUSE_SHARED;
24709           break;
24710         case 'u':
24711           if (!strcmp ("untied", p))
24712             result = PRAGMA_OMP_CLAUSE_UNTIED;
24713           break;
24714         }
24715     }
24716
24717   if (result != PRAGMA_OMP_CLAUSE_NONE)
24718     cp_lexer_consume_token (parser->lexer);
24719
24720   return result;
24721 }
24722
24723 /* Validate that a clause of the given type does not already exist.  */
24724
24725 static void
24726 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24727                            const char *name, location_t location)
24728 {
24729   tree c;
24730
24731   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24732     if (OMP_CLAUSE_CODE (c) == code)
24733       {
24734         error_at (location, "too many %qs clauses", name);
24735         break;
24736       }
24737 }
24738
24739 /* OpenMP 2.5:
24740    variable-list:
24741      identifier
24742      variable-list , identifier
24743
24744    In addition, we match a closing parenthesis.  An opening parenthesis
24745    will have been consumed by the caller.
24746
24747    If KIND is nonzero, create the appropriate node and install the decl
24748    in OMP_CLAUSE_DECL and add the node to the head of the list.
24749
24750    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24751    return the list created.  */
24752
24753 static tree
24754 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24755                                 tree list)
24756 {
24757   cp_token *token;
24758   while (1)
24759     {
24760       tree name, decl;
24761
24762       token = cp_lexer_peek_token (parser->lexer);
24763       name = cp_parser_id_expression (parser, /*template_p=*/false,
24764                                       /*check_dependency_p=*/true,
24765                                       /*template_p=*/NULL,
24766                                       /*declarator_p=*/false,
24767                                       /*optional_p=*/false);
24768       if (name == error_mark_node)
24769         goto skip_comma;
24770
24771       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24772       if (decl == error_mark_node)
24773         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24774                                      token->location);
24775       else if (kind != 0)
24776         {
24777           tree u = build_omp_clause (token->location, kind);
24778           OMP_CLAUSE_DECL (u) = decl;
24779           OMP_CLAUSE_CHAIN (u) = list;
24780           list = u;
24781         }
24782       else
24783         list = tree_cons (decl, NULL_TREE, list);
24784
24785     get_comma:
24786       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24787         break;
24788       cp_lexer_consume_token (parser->lexer);
24789     }
24790
24791   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24792     {
24793       int ending;
24794
24795       /* Try to resync to an unnested comma.  Copied from
24796          cp_parser_parenthesized_expression_list.  */
24797     skip_comma:
24798       ending = cp_parser_skip_to_closing_parenthesis (parser,
24799                                                       /*recovering=*/true,
24800                                                       /*or_comma=*/true,
24801                                                       /*consume_paren=*/true);
24802       if (ending < 0)
24803         goto get_comma;
24804     }
24805
24806   return list;
24807 }
24808
24809 /* Similarly, but expect leading and trailing parenthesis.  This is a very
24810    common case for omp clauses.  */
24811
24812 static tree
24813 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24814 {
24815   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24816     return cp_parser_omp_var_list_no_open (parser, kind, list);
24817   return list;
24818 }
24819
24820 /* OpenMP 3.0:
24821    collapse ( constant-expression ) */
24822
24823 static tree
24824 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24825 {
24826   tree c, num;
24827   location_t loc;
24828   HOST_WIDE_INT n;
24829
24830   loc = cp_lexer_peek_token (parser->lexer)->location;
24831   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24832     return list;
24833
24834   num = cp_parser_constant_expression (parser, false, NULL);
24835
24836   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24837     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24838                                            /*or_comma=*/false,
24839                                            /*consume_paren=*/true);
24840
24841   if (num == error_mark_node)
24842     return list;
24843   num = fold_non_dependent_expr (num);
24844   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24845       || !host_integerp (num, 0)
24846       || (n = tree_low_cst (num, 0)) <= 0
24847       || (int) n != n)
24848     {
24849       error_at (loc, "collapse argument needs positive constant integer expression");
24850       return list;
24851     }
24852
24853   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24854   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24855   OMP_CLAUSE_CHAIN (c) = list;
24856   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24857
24858   return c;
24859 }
24860
24861 /* OpenMP 2.5:
24862    default ( shared | none ) */
24863
24864 static tree
24865 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24866 {
24867   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24868   tree c;
24869
24870   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24871     return list;
24872   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24873     {
24874       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24875       const char *p = IDENTIFIER_POINTER (id);
24876
24877       switch (p[0])
24878         {
24879         case 'n':
24880           if (strcmp ("none", p) != 0)
24881             goto invalid_kind;
24882           kind = OMP_CLAUSE_DEFAULT_NONE;
24883           break;
24884
24885         case 's':
24886           if (strcmp ("shared", p) != 0)
24887             goto invalid_kind;
24888           kind = OMP_CLAUSE_DEFAULT_SHARED;
24889           break;
24890
24891         default:
24892           goto invalid_kind;
24893         }
24894
24895       cp_lexer_consume_token (parser->lexer);
24896     }
24897   else
24898     {
24899     invalid_kind:
24900       cp_parser_error (parser, "expected %<none%> or %<shared%>");
24901     }
24902
24903   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24904     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24905                                            /*or_comma=*/false,
24906                                            /*consume_paren=*/true);
24907
24908   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24909     return list;
24910
24911   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24912   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24913   OMP_CLAUSE_CHAIN (c) = list;
24914   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24915
24916   return c;
24917 }
24918
24919 /* OpenMP 3.1:
24920    final ( expression ) */
24921
24922 static tree
24923 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24924 {
24925   tree t, c;
24926
24927   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24928     return list;
24929
24930   t = cp_parser_condition (parser);
24931
24932   if (t == error_mark_node
24933       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24934     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24935                                            /*or_comma=*/false,
24936                                            /*consume_paren=*/true);
24937
24938   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24939
24940   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24941   OMP_CLAUSE_FINAL_EXPR (c) = t;
24942   OMP_CLAUSE_CHAIN (c) = list;
24943
24944   return c;
24945 }
24946
24947 /* OpenMP 2.5:
24948    if ( expression ) */
24949
24950 static tree
24951 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24952 {
24953   tree t, c;
24954
24955   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24956     return list;
24957
24958   t = cp_parser_condition (parser);
24959
24960   if (t == error_mark_node
24961       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24962     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24963                                            /*or_comma=*/false,
24964                                            /*consume_paren=*/true);
24965
24966   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24967
24968   c = build_omp_clause (location, OMP_CLAUSE_IF);
24969   OMP_CLAUSE_IF_EXPR (c) = t;
24970   OMP_CLAUSE_CHAIN (c) = list;
24971
24972   return c;
24973 }
24974
24975 /* OpenMP 3.1:
24976    mergeable */
24977
24978 static tree
24979 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24980                                 tree list, location_t location)
24981 {
24982   tree c;
24983
24984   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24985                              location);
24986
24987   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24988   OMP_CLAUSE_CHAIN (c) = list;
24989   return c;
24990 }
24991
24992 /* OpenMP 2.5:
24993    nowait */
24994
24995 static tree
24996 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24997                              tree list, location_t location)
24998 {
24999   tree c;
25000
25001   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
25002
25003   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
25004   OMP_CLAUSE_CHAIN (c) = list;
25005   return c;
25006 }
25007
25008 /* OpenMP 2.5:
25009    num_threads ( expression ) */
25010
25011 static tree
25012 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
25013                                   location_t location)
25014 {
25015   tree t, c;
25016
25017   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25018     return list;
25019
25020   t = cp_parser_expression (parser, false, NULL);
25021
25022   if (t == error_mark_node
25023       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25024     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25025                                            /*or_comma=*/false,
25026                                            /*consume_paren=*/true);
25027
25028   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
25029                              "num_threads", location);
25030
25031   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
25032   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
25033   OMP_CLAUSE_CHAIN (c) = list;
25034
25035   return c;
25036 }
25037
25038 /* OpenMP 2.5:
25039    ordered */
25040
25041 static tree
25042 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
25043                               tree list, location_t location)
25044 {
25045   tree c;
25046
25047   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
25048                              "ordered", location);
25049
25050   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
25051   OMP_CLAUSE_CHAIN (c) = list;
25052   return c;
25053 }
25054
25055 /* OpenMP 2.5:
25056    reduction ( reduction-operator : variable-list )
25057
25058    reduction-operator:
25059      One of: + * - & ^ | && ||
25060
25061    OpenMP 3.1:
25062
25063    reduction-operator:
25064      One of: + * - & ^ | && || min max  */
25065
25066 static tree
25067 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
25068 {
25069   enum tree_code code;
25070   tree nlist, c;
25071
25072   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25073     return list;
25074
25075   switch (cp_lexer_peek_token (parser->lexer)->type)
25076     {
25077     case CPP_PLUS:
25078       code = PLUS_EXPR;
25079       break;
25080     case CPP_MULT:
25081       code = MULT_EXPR;
25082       break;
25083     case CPP_MINUS:
25084       code = MINUS_EXPR;
25085       break;
25086     case CPP_AND:
25087       code = BIT_AND_EXPR;
25088       break;
25089     case CPP_XOR:
25090       code = BIT_XOR_EXPR;
25091       break;
25092     case CPP_OR:
25093       code = BIT_IOR_EXPR;
25094       break;
25095     case CPP_AND_AND:
25096       code = TRUTH_ANDIF_EXPR;
25097       break;
25098     case CPP_OR_OR:
25099       code = TRUTH_ORIF_EXPR;
25100       break;
25101     case CPP_NAME:
25102       {
25103         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25104         const char *p = IDENTIFIER_POINTER (id);
25105
25106         if (strcmp (p, "min") == 0)
25107           {
25108             code = MIN_EXPR;
25109             break;
25110           }
25111         if (strcmp (p, "max") == 0)
25112           {
25113             code = MAX_EXPR;
25114             break;
25115           }
25116       }
25117       /* FALLTHROUGH */
25118     default:
25119       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
25120                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
25121     resync_fail:
25122       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25123                                              /*or_comma=*/false,
25124                                              /*consume_paren=*/true);
25125       return list;
25126     }
25127   cp_lexer_consume_token (parser->lexer);
25128
25129   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
25130     goto resync_fail;
25131
25132   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
25133   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
25134     OMP_CLAUSE_REDUCTION_CODE (c) = code;
25135
25136   return nlist;
25137 }
25138
25139 /* OpenMP 2.5:
25140    schedule ( schedule-kind )
25141    schedule ( schedule-kind , expression )
25142
25143    schedule-kind:
25144      static | dynamic | guided | runtime | auto  */
25145
25146 static tree
25147 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
25148 {
25149   tree c, t;
25150
25151   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25152     return list;
25153
25154   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
25155
25156   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25157     {
25158       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25159       const char *p = IDENTIFIER_POINTER (id);
25160
25161       switch (p[0])
25162         {
25163         case 'd':
25164           if (strcmp ("dynamic", p) != 0)
25165             goto invalid_kind;
25166           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
25167           break;
25168
25169         case 'g':
25170           if (strcmp ("guided", p) != 0)
25171             goto invalid_kind;
25172           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
25173           break;
25174
25175         case 'r':
25176           if (strcmp ("runtime", p) != 0)
25177             goto invalid_kind;
25178           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
25179           break;
25180
25181         default:
25182           goto invalid_kind;
25183         }
25184     }
25185   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
25186     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
25187   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
25188     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
25189   else
25190     goto invalid_kind;
25191   cp_lexer_consume_token (parser->lexer);
25192
25193   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25194     {
25195       cp_token *token;
25196       cp_lexer_consume_token (parser->lexer);
25197
25198       token = cp_lexer_peek_token (parser->lexer);
25199       t = cp_parser_assignment_expression (parser, false, NULL);
25200
25201       if (t == error_mark_node)
25202         goto resync_fail;
25203       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
25204         error_at (token->location, "schedule %<runtime%> does not take "
25205                   "a %<chunk_size%> parameter");
25206       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
25207         error_at (token->location, "schedule %<auto%> does not take "
25208                   "a %<chunk_size%> parameter");
25209       else
25210         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
25211
25212       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25213         goto resync_fail;
25214     }
25215   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25216     goto resync_fail;
25217
25218   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25219   OMP_CLAUSE_CHAIN (c) = list;
25220   return c;
25221
25222  invalid_kind:
25223   cp_parser_error (parser, "invalid schedule kind");
25224  resync_fail:
25225   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25226                                          /*or_comma=*/false,
25227                                          /*consume_paren=*/true);
25228   return list;
25229 }
25230
25231 /* OpenMP 3.0:
25232    untied */
25233
25234 static tree
25235 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25236                              tree list, location_t location)
25237 {
25238   tree c;
25239
25240   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25241
25242   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25243   OMP_CLAUSE_CHAIN (c) = list;
25244   return c;
25245 }
25246
25247 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25248    is a bitmask in MASK.  Return the list of clauses found; the result
25249    of clause default goes in *pdefault.  */
25250
25251 static tree
25252 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25253                            const char *where, cp_token *pragma_tok)
25254 {
25255   tree clauses = NULL;
25256   bool first = true;
25257   cp_token *token = NULL;
25258
25259   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25260     {
25261       pragma_omp_clause c_kind;
25262       const char *c_name;
25263       tree prev = clauses;
25264
25265       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25266         cp_lexer_consume_token (parser->lexer);
25267
25268       token = cp_lexer_peek_token (parser->lexer);
25269       c_kind = cp_parser_omp_clause_name (parser);
25270       first = false;
25271
25272       switch (c_kind)
25273         {
25274         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25275           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25276                                                    token->location);
25277           c_name = "collapse";
25278           break;
25279         case PRAGMA_OMP_CLAUSE_COPYIN:
25280           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25281           c_name = "copyin";
25282           break;
25283         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25284           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25285                                             clauses);
25286           c_name = "copyprivate";
25287           break;
25288         case PRAGMA_OMP_CLAUSE_DEFAULT:
25289           clauses = cp_parser_omp_clause_default (parser, clauses,
25290                                                   token->location);
25291           c_name = "default";
25292           break;
25293         case PRAGMA_OMP_CLAUSE_FINAL:
25294           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25295           c_name = "final";
25296           break;
25297         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25298           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25299                                             clauses);
25300           c_name = "firstprivate";
25301           break;
25302         case PRAGMA_OMP_CLAUSE_IF:
25303           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25304           c_name = "if";
25305           break;
25306         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25307           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25308                                             clauses);
25309           c_name = "lastprivate";
25310           break;
25311         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25312           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25313                                                     token->location);
25314           c_name = "mergeable";
25315           break;
25316         case PRAGMA_OMP_CLAUSE_NOWAIT:
25317           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25318           c_name = "nowait";
25319           break;
25320         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25321           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25322                                                       token->location);
25323           c_name = "num_threads";
25324           break;
25325         case PRAGMA_OMP_CLAUSE_ORDERED:
25326           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25327                                                   token->location);
25328           c_name = "ordered";
25329           break;
25330         case PRAGMA_OMP_CLAUSE_PRIVATE:
25331           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25332                                             clauses);
25333           c_name = "private";
25334           break;
25335         case PRAGMA_OMP_CLAUSE_REDUCTION:
25336           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25337           c_name = "reduction";
25338           break;
25339         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25340           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25341                                                    token->location);
25342           c_name = "schedule";
25343           break;
25344         case PRAGMA_OMP_CLAUSE_SHARED:
25345           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25346                                             clauses);
25347           c_name = "shared";
25348           break;
25349         case PRAGMA_OMP_CLAUSE_UNTIED:
25350           clauses = cp_parser_omp_clause_untied (parser, clauses,
25351                                                  token->location);
25352           c_name = "nowait";
25353           break;
25354         default:
25355           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25356           goto saw_error;
25357         }
25358
25359       if (((mask >> c_kind) & 1) == 0)
25360         {
25361           /* Remove the invalid clause(s) from the list to avoid
25362              confusing the rest of the compiler.  */
25363           clauses = prev;
25364           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25365         }
25366     }
25367  saw_error:
25368   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25369   return finish_omp_clauses (clauses);
25370 }
25371
25372 /* OpenMP 2.5:
25373    structured-block:
25374      statement
25375
25376    In practice, we're also interested in adding the statement to an
25377    outer node.  So it is convenient if we work around the fact that
25378    cp_parser_statement calls add_stmt.  */
25379
25380 static unsigned
25381 cp_parser_begin_omp_structured_block (cp_parser *parser)
25382 {
25383   unsigned save = parser->in_statement;
25384
25385   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25386      This preserves the "not within loop or switch" style error messages
25387      for nonsense cases like
25388         void foo() {
25389         #pragma omp single
25390           break;
25391         }
25392   */
25393   if (parser->in_statement)
25394     parser->in_statement = IN_OMP_BLOCK;
25395
25396   return save;
25397 }
25398
25399 static void
25400 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25401 {
25402   parser->in_statement = save;
25403 }
25404
25405 static tree
25406 cp_parser_omp_structured_block (cp_parser *parser)
25407 {
25408   tree stmt = begin_omp_structured_block ();
25409   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25410
25411   cp_parser_statement (parser, NULL_TREE, false, NULL);
25412
25413   cp_parser_end_omp_structured_block (parser, save);
25414   return finish_omp_structured_block (stmt);
25415 }
25416
25417 /* OpenMP 2.5:
25418    # pragma omp atomic new-line
25419      expression-stmt
25420
25421    expression-stmt:
25422      x binop= expr | x++ | ++x | x-- | --x
25423    binop:
25424      +, *, -, /, &, ^, |, <<, >>
25425
25426   where x is an lvalue expression with scalar type.
25427
25428    OpenMP 3.1:
25429    # pragma omp atomic new-line
25430      update-stmt
25431
25432    # pragma omp atomic read new-line
25433      read-stmt
25434
25435    # pragma omp atomic write new-line
25436      write-stmt
25437
25438    # pragma omp atomic update new-line
25439      update-stmt
25440
25441    # pragma omp atomic capture new-line
25442      capture-stmt
25443
25444    # pragma omp atomic capture new-line
25445      capture-block
25446
25447    read-stmt:
25448      v = x
25449    write-stmt:
25450      x = expr
25451    update-stmt:
25452      expression-stmt | x = x binop expr
25453    capture-stmt:
25454      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25455    capture-block:
25456      { v = x; update-stmt; } | { update-stmt; v = x; }
25457
25458   where x and v are lvalue expressions with scalar type.  */
25459
25460 static void
25461 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25462 {
25463   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25464   tree rhs1 = NULL_TREE, orig_lhs;
25465   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25466   bool structured_block = false;
25467
25468   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25469     {
25470       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25471       const char *p = IDENTIFIER_POINTER (id);
25472
25473       if (!strcmp (p, "read"))
25474         code = OMP_ATOMIC_READ;
25475       else if (!strcmp (p, "write"))
25476         code = NOP_EXPR;
25477       else if (!strcmp (p, "update"))
25478         code = OMP_ATOMIC;
25479       else if (!strcmp (p, "capture"))
25480         code = OMP_ATOMIC_CAPTURE_NEW;
25481       else
25482         p = NULL;
25483       if (p)
25484         cp_lexer_consume_token (parser->lexer);
25485     }
25486   cp_parser_require_pragma_eol (parser, pragma_tok);
25487
25488   switch (code)
25489     {
25490     case OMP_ATOMIC_READ:
25491     case NOP_EXPR: /* atomic write */
25492       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25493                                       /*cast_p=*/false, NULL);
25494       if (v == error_mark_node)
25495         goto saw_error;
25496       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25497         goto saw_error;
25498       if (code == NOP_EXPR)
25499         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25500       else
25501         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25502                                           /*cast_p=*/false, NULL);
25503       if (lhs == error_mark_node)
25504         goto saw_error;
25505       if (code == NOP_EXPR)
25506         {
25507           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25508              opcode.  */
25509           code = OMP_ATOMIC;
25510           rhs = lhs;
25511           lhs = v;
25512           v = NULL_TREE;
25513         }
25514       goto done;
25515     case OMP_ATOMIC_CAPTURE_NEW:
25516       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25517         {
25518           cp_lexer_consume_token (parser->lexer);
25519           structured_block = true;
25520         }
25521       else
25522         {
25523           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25524                                           /*cast_p=*/false, NULL);
25525           if (v == error_mark_node)
25526             goto saw_error;
25527           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25528             goto saw_error;
25529         }
25530     default:
25531       break;
25532     }
25533
25534 restart:
25535   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25536                                     /*cast_p=*/false, NULL);
25537   orig_lhs = lhs;
25538   switch (TREE_CODE (lhs))
25539     {
25540     case ERROR_MARK:
25541       goto saw_error;
25542
25543     case POSTINCREMENT_EXPR:
25544       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25545         code = OMP_ATOMIC_CAPTURE_OLD;
25546       /* FALLTHROUGH */
25547     case PREINCREMENT_EXPR:
25548       lhs = TREE_OPERAND (lhs, 0);
25549       opcode = PLUS_EXPR;
25550       rhs = integer_one_node;
25551       break;
25552
25553     case POSTDECREMENT_EXPR:
25554       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25555         code = OMP_ATOMIC_CAPTURE_OLD;
25556       /* FALLTHROUGH */
25557     case PREDECREMENT_EXPR:
25558       lhs = TREE_OPERAND (lhs, 0);
25559       opcode = MINUS_EXPR;
25560       rhs = integer_one_node;
25561       break;
25562
25563     case COMPOUND_EXPR:
25564       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25565          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25566          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25567          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25568          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25569                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25570             == BOOLEAN_TYPE)
25571        /* Undo effects of boolean_increment for post {in,de}crement.  */
25572        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25573       /* FALLTHRU */
25574     case MODIFY_EXPR:
25575       if (TREE_CODE (lhs) == MODIFY_EXPR
25576          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25577         {
25578           /* Undo effects of boolean_increment.  */
25579           if (integer_onep (TREE_OPERAND (lhs, 1)))
25580             {
25581               /* This is pre or post increment.  */
25582               rhs = TREE_OPERAND (lhs, 1);
25583               lhs = TREE_OPERAND (lhs, 0);
25584               opcode = NOP_EXPR;
25585               if (code == OMP_ATOMIC_CAPTURE_NEW
25586                   && !structured_block
25587                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25588                 code = OMP_ATOMIC_CAPTURE_OLD;
25589               break;
25590             }
25591         }
25592       /* FALLTHRU */
25593     default:
25594       switch (cp_lexer_peek_token (parser->lexer)->type)
25595         {
25596         case CPP_MULT_EQ:
25597           opcode = MULT_EXPR;
25598           break;
25599         case CPP_DIV_EQ:
25600           opcode = TRUNC_DIV_EXPR;
25601           break;
25602         case CPP_PLUS_EQ:
25603           opcode = PLUS_EXPR;
25604           break;
25605         case CPP_MINUS_EQ:
25606           opcode = MINUS_EXPR;
25607           break;
25608         case CPP_LSHIFT_EQ:
25609           opcode = LSHIFT_EXPR;
25610           break;
25611         case CPP_RSHIFT_EQ:
25612           opcode = RSHIFT_EXPR;
25613           break;
25614         case CPP_AND_EQ:
25615           opcode = BIT_AND_EXPR;
25616           break;
25617         case CPP_OR_EQ:
25618           opcode = BIT_IOR_EXPR;
25619           break;
25620         case CPP_XOR_EQ:
25621           opcode = BIT_XOR_EXPR;
25622           break;
25623         case CPP_EQ:
25624           if (structured_block || code == OMP_ATOMIC)
25625             {
25626               enum cp_parser_prec oprec;
25627               cp_token *token;
25628               cp_lexer_consume_token (parser->lexer);
25629               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25630                                                  /*cast_p=*/false, NULL);
25631               if (rhs1 == error_mark_node)
25632                 goto saw_error;
25633               token = cp_lexer_peek_token (parser->lexer);
25634               switch (token->type)
25635                 {
25636                 case CPP_SEMICOLON:
25637                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25638                     {
25639                       code = OMP_ATOMIC_CAPTURE_OLD;
25640                       v = lhs;
25641                       lhs = NULL_TREE;
25642                       lhs1 = rhs1;
25643                       rhs1 = NULL_TREE;
25644                       cp_lexer_consume_token (parser->lexer);
25645                       goto restart;
25646                     }
25647                   cp_parser_error (parser,
25648                                    "invalid form of %<#pragma omp atomic%>");
25649                   goto saw_error;
25650                 case CPP_MULT:
25651                   opcode = MULT_EXPR;
25652                   break;
25653                 case CPP_DIV:
25654                   opcode = TRUNC_DIV_EXPR;
25655                   break;
25656                 case CPP_PLUS:
25657                   opcode = PLUS_EXPR;
25658                   break;
25659                 case CPP_MINUS:
25660                   opcode = MINUS_EXPR;
25661                   break;
25662                 case CPP_LSHIFT:
25663                   opcode = LSHIFT_EXPR;
25664                   break;
25665                 case CPP_RSHIFT:
25666                   opcode = RSHIFT_EXPR;
25667                   break;
25668                 case CPP_AND:
25669                   opcode = BIT_AND_EXPR;
25670                   break;
25671                 case CPP_OR:
25672                   opcode = BIT_IOR_EXPR;
25673                   break;
25674                 case CPP_XOR:
25675                   opcode = BIT_XOR_EXPR;
25676                   break;
25677                 default:
25678                   cp_parser_error (parser,
25679                                    "invalid operator for %<#pragma omp atomic%>");
25680                   goto saw_error;
25681                 }
25682               oprec = TOKEN_PRECEDENCE (token);
25683               gcc_assert (oprec != PREC_NOT_OPERATOR);
25684               if (commutative_tree_code (opcode))
25685                 oprec = (enum cp_parser_prec) (oprec - 1);
25686               cp_lexer_consume_token (parser->lexer);
25687               rhs = cp_parser_binary_expression (parser, false, false,
25688                                                  oprec, NULL);
25689               if (rhs == error_mark_node)
25690                 goto saw_error;
25691               goto stmt_done;
25692             }
25693           /* FALLTHROUGH */
25694         default:
25695           cp_parser_error (parser,
25696                            "invalid operator for %<#pragma omp atomic%>");
25697           goto saw_error;
25698         }
25699       cp_lexer_consume_token (parser->lexer);
25700
25701       rhs = cp_parser_expression (parser, false, NULL);
25702       if (rhs == error_mark_node)
25703         goto saw_error;
25704       break;
25705     }
25706 stmt_done:
25707   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25708     {
25709       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25710         goto saw_error;
25711       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25712                                       /*cast_p=*/false, NULL);
25713       if (v == error_mark_node)
25714         goto saw_error;
25715       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25716         goto saw_error;
25717       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25718                                          /*cast_p=*/false, NULL);
25719       if (lhs1 == error_mark_node)
25720         goto saw_error;
25721     }
25722   if (structured_block)
25723     {
25724       cp_parser_consume_semicolon_at_end_of_statement (parser);
25725       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25726     }
25727 done:
25728   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25729   if (!structured_block)
25730     cp_parser_consume_semicolon_at_end_of_statement (parser);
25731   return;
25732
25733  saw_error:
25734   cp_parser_skip_to_end_of_block_or_statement (parser);
25735   if (structured_block)
25736     {
25737       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25738         cp_lexer_consume_token (parser->lexer);
25739       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25740         {
25741           cp_parser_skip_to_end_of_block_or_statement (parser);
25742           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25743             cp_lexer_consume_token (parser->lexer);
25744         }
25745     }
25746 }
25747
25748
25749 /* OpenMP 2.5:
25750    # pragma omp barrier new-line  */
25751
25752 static void
25753 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25754 {
25755   cp_parser_require_pragma_eol (parser, pragma_tok);
25756   finish_omp_barrier ();
25757 }
25758
25759 /* OpenMP 2.5:
25760    # pragma omp critical [(name)] new-line
25761      structured-block  */
25762
25763 static tree
25764 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25765 {
25766   tree stmt, name = NULL;
25767
25768   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25769     {
25770       cp_lexer_consume_token (parser->lexer);
25771
25772       name = cp_parser_identifier (parser);
25773
25774       if (name == error_mark_node
25775           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25776         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25777                                                /*or_comma=*/false,
25778                                                /*consume_paren=*/true);
25779       if (name == error_mark_node)
25780         name = NULL;
25781     }
25782   cp_parser_require_pragma_eol (parser, pragma_tok);
25783
25784   stmt = cp_parser_omp_structured_block (parser);
25785   return c_finish_omp_critical (input_location, stmt, name);
25786 }
25787
25788 /* OpenMP 2.5:
25789    # pragma omp flush flush-vars[opt] new-line
25790
25791    flush-vars:
25792      ( variable-list ) */
25793
25794 static void
25795 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25796 {
25797   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25798     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25799   cp_parser_require_pragma_eol (parser, pragma_tok);
25800
25801   finish_omp_flush ();
25802 }
25803
25804 /* Helper function, to parse omp for increment expression.  */
25805
25806 static tree
25807 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25808 {
25809   tree cond = cp_parser_binary_expression (parser, false, true,
25810                                            PREC_NOT_OPERATOR, NULL);
25811   if (cond == error_mark_node
25812       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25813     {
25814       cp_parser_skip_to_end_of_statement (parser);
25815       return error_mark_node;
25816     }
25817
25818   switch (TREE_CODE (cond))
25819     {
25820     case GT_EXPR:
25821     case GE_EXPR:
25822     case LT_EXPR:
25823     case LE_EXPR:
25824       break;
25825     default:
25826       return error_mark_node;
25827     }
25828
25829   /* If decl is an iterator, preserve LHS and RHS of the relational
25830      expr until finish_omp_for.  */
25831   if (decl
25832       && (type_dependent_expression_p (decl)
25833           || CLASS_TYPE_P (TREE_TYPE (decl))))
25834     return cond;
25835
25836   return build_x_binary_op (TREE_CODE (cond),
25837                             TREE_OPERAND (cond, 0), ERROR_MARK,
25838                             TREE_OPERAND (cond, 1), ERROR_MARK,
25839                             /*overload=*/NULL, tf_warning_or_error);
25840 }
25841
25842 /* Helper function, to parse omp for increment expression.  */
25843
25844 static tree
25845 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25846 {
25847   cp_token *token = cp_lexer_peek_token (parser->lexer);
25848   enum tree_code op;
25849   tree lhs, rhs;
25850   cp_id_kind idk;
25851   bool decl_first;
25852
25853   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25854     {
25855       op = (token->type == CPP_PLUS_PLUS
25856             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25857       cp_lexer_consume_token (parser->lexer);
25858       lhs = cp_parser_cast_expression (parser, false, false, NULL);
25859       if (lhs != decl)
25860         return error_mark_node;
25861       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25862     }
25863
25864   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25865   if (lhs != decl)
25866     return error_mark_node;
25867
25868   token = cp_lexer_peek_token (parser->lexer);
25869   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25870     {
25871       op = (token->type == CPP_PLUS_PLUS
25872             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25873       cp_lexer_consume_token (parser->lexer);
25874       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25875     }
25876
25877   op = cp_parser_assignment_operator_opt (parser);
25878   if (op == ERROR_MARK)
25879     return error_mark_node;
25880
25881   if (op != NOP_EXPR)
25882     {
25883       rhs = cp_parser_assignment_expression (parser, false, NULL);
25884       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25885       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25886     }
25887
25888   lhs = cp_parser_binary_expression (parser, false, false,
25889                                      PREC_ADDITIVE_EXPRESSION, NULL);
25890   token = cp_lexer_peek_token (parser->lexer);
25891   decl_first = lhs == decl;
25892   if (decl_first)
25893     lhs = NULL_TREE;
25894   if (token->type != CPP_PLUS
25895       && token->type != CPP_MINUS)
25896     return error_mark_node;
25897
25898   do
25899     {
25900       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25901       cp_lexer_consume_token (parser->lexer);
25902       rhs = cp_parser_binary_expression (parser, false, false,
25903                                          PREC_ADDITIVE_EXPRESSION, NULL);
25904       token = cp_lexer_peek_token (parser->lexer);
25905       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25906         {
25907           if (lhs == NULL_TREE)
25908             {
25909               if (op == PLUS_EXPR)
25910                 lhs = rhs;
25911               else
25912                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25913             }
25914           else
25915             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25916                                      NULL, tf_warning_or_error);
25917         }
25918     }
25919   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25920
25921   if (!decl_first)
25922     {
25923       if (rhs != decl || op == MINUS_EXPR)
25924         return error_mark_node;
25925       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25926     }
25927   else
25928     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25929
25930   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25931 }
25932
25933 /* Parse the restricted form of the for statement allowed by OpenMP.  */
25934
25935 static tree
25936 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25937 {
25938   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25939   tree real_decl, initv, condv, incrv, declv;
25940   tree this_pre_body, cl;
25941   location_t loc_first;
25942   bool collapse_err = false;
25943   int i, collapse = 1, nbraces = 0;
25944   VEC(tree,gc) *for_block = make_tree_vector ();
25945
25946   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25947     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25948       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25949
25950   gcc_assert (collapse >= 1);
25951
25952   declv = make_tree_vec (collapse);
25953   initv = make_tree_vec (collapse);
25954   condv = make_tree_vec (collapse);
25955   incrv = make_tree_vec (collapse);
25956
25957   loc_first = cp_lexer_peek_token (parser->lexer)->location;
25958
25959   for (i = 0; i < collapse; i++)
25960     {
25961       int bracecount = 0;
25962       bool add_private_clause = false;
25963       location_t loc;
25964
25965       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25966         {
25967           cp_parser_error (parser, "for statement expected");
25968           return NULL;
25969         }
25970       loc = cp_lexer_consume_token (parser->lexer)->location;
25971
25972       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25973         return NULL;
25974
25975       init = decl = real_decl = NULL;
25976       this_pre_body = push_stmt_list ();
25977       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25978         {
25979           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25980
25981              init-expr:
25982                        var = lb
25983                        integer-type var = lb
25984                        random-access-iterator-type var = lb
25985                        pointer-type var = lb
25986           */
25987           cp_decl_specifier_seq type_specifiers;
25988
25989           /* First, try to parse as an initialized declaration.  See
25990              cp_parser_condition, from whence the bulk of this is copied.  */
25991
25992           cp_parser_parse_tentatively (parser);
25993           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25994                                         /*is_trailing_return=*/false,
25995                                         &type_specifiers);
25996           if (cp_parser_parse_definitely (parser))
25997             {
25998               /* If parsing a type specifier seq succeeded, then this
25999                  MUST be a initialized declaration.  */
26000               tree asm_specification, attributes;
26001               cp_declarator *declarator;
26002
26003               declarator = cp_parser_declarator (parser,
26004                                                  CP_PARSER_DECLARATOR_NAMED,
26005                                                  /*ctor_dtor_or_conv_p=*/NULL,
26006                                                  /*parenthesized_p=*/NULL,
26007                                                  /*member_p=*/false);
26008               attributes = cp_parser_attributes_opt (parser);
26009               asm_specification = cp_parser_asm_specification_opt (parser);
26010
26011               if (declarator == cp_error_declarator) 
26012                 cp_parser_skip_to_end_of_statement (parser);
26013
26014               else 
26015                 {
26016                   tree pushed_scope, auto_node;
26017
26018                   decl = start_decl (declarator, &type_specifiers,
26019                                      SD_INITIALIZED, attributes,
26020                                      /*prefix_attributes=*/NULL_TREE,
26021                                      &pushed_scope);
26022
26023                   auto_node = type_uses_auto (TREE_TYPE (decl));
26024                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
26025                     {
26026                       if (cp_lexer_next_token_is (parser->lexer, 
26027                                                   CPP_OPEN_PAREN))
26028                         error ("parenthesized initialization is not allowed in "
26029                                "OpenMP %<for%> loop");
26030                       else
26031                         /* Trigger an error.  */
26032                         cp_parser_require (parser, CPP_EQ, RT_EQ);
26033
26034                       init = error_mark_node;
26035                       cp_parser_skip_to_end_of_statement (parser);
26036                     }
26037                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
26038                            || type_dependent_expression_p (decl)
26039                            || auto_node)
26040                     {
26041                       bool is_direct_init, is_non_constant_init;
26042
26043                       init = cp_parser_initializer (parser,
26044                                                     &is_direct_init,
26045                                                     &is_non_constant_init);
26046
26047                       if (auto_node)
26048                         {
26049                           TREE_TYPE (decl)
26050                             = do_auto_deduction (TREE_TYPE (decl), init,
26051                                                  auto_node);
26052
26053                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
26054                               && !type_dependent_expression_p (decl))
26055                             goto non_class;
26056                         }
26057                       
26058                       cp_finish_decl (decl, init, !is_non_constant_init,
26059                                       asm_specification,
26060                                       LOOKUP_ONLYCONVERTING);
26061                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
26062                         {
26063                           VEC_safe_push (tree, gc, for_block, this_pre_body);
26064                           init = NULL_TREE;
26065                         }
26066                       else
26067                         init = pop_stmt_list (this_pre_body);
26068                       this_pre_body = NULL_TREE;
26069                     }
26070                   else
26071                     {
26072                       /* Consume '='.  */
26073                       cp_lexer_consume_token (parser->lexer);
26074                       init = cp_parser_assignment_expression (parser, false, NULL);
26075
26076                     non_class:
26077                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
26078                         init = error_mark_node;
26079                       else
26080                         cp_finish_decl (decl, NULL_TREE,
26081                                         /*init_const_expr_p=*/false,
26082                                         asm_specification,
26083                                         LOOKUP_ONLYCONVERTING);
26084                     }
26085
26086                   if (pushed_scope)
26087                     pop_scope (pushed_scope);
26088                 }
26089             }
26090           else 
26091             {
26092               cp_id_kind idk;
26093               /* If parsing a type specifier sequence failed, then
26094                  this MUST be a simple expression.  */
26095               cp_parser_parse_tentatively (parser);
26096               decl = cp_parser_primary_expression (parser, false, false,
26097                                                    false, &idk);
26098               if (!cp_parser_error_occurred (parser)
26099                   && decl
26100                   && DECL_P (decl)
26101                   && CLASS_TYPE_P (TREE_TYPE (decl)))
26102                 {
26103                   tree rhs;
26104
26105                   cp_parser_parse_definitely (parser);
26106                   cp_parser_require (parser, CPP_EQ, RT_EQ);
26107                   rhs = cp_parser_assignment_expression (parser, false, NULL);
26108                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
26109                                                          rhs,
26110                                                          tf_warning_or_error));
26111                   add_private_clause = true;
26112                 }
26113               else
26114                 {
26115                   decl = NULL;
26116                   cp_parser_abort_tentative_parse (parser);
26117                   init = cp_parser_expression (parser, false, NULL);
26118                   if (init)
26119                     {
26120                       if (TREE_CODE (init) == MODIFY_EXPR
26121                           || TREE_CODE (init) == MODOP_EXPR)
26122                         real_decl = TREE_OPERAND (init, 0);
26123                     }
26124                 }
26125             }
26126         }
26127       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26128       if (this_pre_body)
26129         {
26130           this_pre_body = pop_stmt_list (this_pre_body);
26131           if (pre_body)
26132             {
26133               tree t = pre_body;
26134               pre_body = push_stmt_list ();
26135               add_stmt (t);
26136               add_stmt (this_pre_body);
26137               pre_body = pop_stmt_list (pre_body);
26138             }
26139           else
26140             pre_body = this_pre_body;
26141         }
26142
26143       if (decl)
26144         real_decl = decl;
26145       if (par_clauses != NULL && real_decl != NULL_TREE)
26146         {
26147           tree *c;
26148           for (c = par_clauses; *c ; )
26149             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
26150                 && OMP_CLAUSE_DECL (*c) == real_decl)
26151               {
26152                 error_at (loc, "iteration variable %qD"
26153                           " should not be firstprivate", real_decl);
26154                 *c = OMP_CLAUSE_CHAIN (*c);
26155               }
26156             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
26157                      && OMP_CLAUSE_DECL (*c) == real_decl)
26158               {
26159                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
26160                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
26161                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
26162                 OMP_CLAUSE_DECL (l) = real_decl;
26163                 OMP_CLAUSE_CHAIN (l) = clauses;
26164                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
26165                 clauses = l;
26166                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
26167                 CP_OMP_CLAUSE_INFO (*c) = NULL;
26168                 add_private_clause = false;
26169               }
26170             else
26171               {
26172                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
26173                     && OMP_CLAUSE_DECL (*c) == real_decl)
26174                   add_private_clause = false;
26175                 c = &OMP_CLAUSE_CHAIN (*c);
26176               }
26177         }
26178
26179       if (add_private_clause)
26180         {
26181           tree c;
26182           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
26183             {
26184               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
26185                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
26186                   && OMP_CLAUSE_DECL (c) == decl)
26187                 break;
26188               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
26189                        && OMP_CLAUSE_DECL (c) == decl)
26190                 error_at (loc, "iteration variable %qD "
26191                           "should not be firstprivate",
26192                           decl);
26193               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
26194                        && OMP_CLAUSE_DECL (c) == decl)
26195                 error_at (loc, "iteration variable %qD should not be reduction",
26196                           decl);
26197             }
26198           if (c == NULL)
26199             {
26200               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
26201               OMP_CLAUSE_DECL (c) = decl;
26202               c = finish_omp_clauses (c);
26203               if (c)
26204                 {
26205                   OMP_CLAUSE_CHAIN (c) = clauses;
26206                   clauses = c;
26207                 }
26208             }
26209         }
26210
26211       cond = NULL;
26212       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26213         cond = cp_parser_omp_for_cond (parser, decl);
26214       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26215
26216       incr = NULL;
26217       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26218         {
26219           /* If decl is an iterator, preserve the operator on decl
26220              until finish_omp_for.  */
26221           if (decl
26222               && ((type_dependent_expression_p (decl)
26223                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
26224                   || CLASS_TYPE_P (TREE_TYPE (decl))))
26225             incr = cp_parser_omp_for_incr (parser, decl);
26226           else
26227             incr = cp_parser_expression (parser, false, NULL);
26228         }
26229
26230       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26231         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26232                                                /*or_comma=*/false,
26233                                                /*consume_paren=*/true);
26234
26235       TREE_VEC_ELT (declv, i) = decl;
26236       TREE_VEC_ELT (initv, i) = init;
26237       TREE_VEC_ELT (condv, i) = cond;
26238       TREE_VEC_ELT (incrv, i) = incr;
26239
26240       if (i == collapse - 1)
26241         break;
26242
26243       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26244          in between the collapsed for loops to be still considered perfectly
26245          nested.  Hopefully the final version clarifies this.
26246          For now handle (multiple) {'s and empty statements.  */
26247       cp_parser_parse_tentatively (parser);
26248       do
26249         {
26250           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26251             break;
26252           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26253             {
26254               cp_lexer_consume_token (parser->lexer);
26255               bracecount++;
26256             }
26257           else if (bracecount
26258                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26259             cp_lexer_consume_token (parser->lexer);
26260           else
26261             {
26262               loc = cp_lexer_peek_token (parser->lexer)->location;
26263               error_at (loc, "not enough collapsed for loops");
26264               collapse_err = true;
26265               cp_parser_abort_tentative_parse (parser);
26266               declv = NULL_TREE;
26267               break;
26268             }
26269         }
26270       while (1);
26271
26272       if (declv)
26273         {
26274           cp_parser_parse_definitely (parser);
26275           nbraces += bracecount;
26276         }
26277     }
26278
26279   /* Note that we saved the original contents of this flag when we entered
26280      the structured block, and so we don't need to re-save it here.  */
26281   parser->in_statement = IN_OMP_FOR;
26282
26283   /* Note that the grammar doesn't call for a structured block here,
26284      though the loop as a whole is a structured block.  */
26285   body = push_stmt_list ();
26286   cp_parser_statement (parser, NULL_TREE, false, NULL);
26287   body = pop_stmt_list (body);
26288
26289   if (declv == NULL_TREE)
26290     ret = NULL_TREE;
26291   else
26292     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26293                           pre_body, clauses);
26294
26295   while (nbraces)
26296     {
26297       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26298         {
26299           cp_lexer_consume_token (parser->lexer);
26300           nbraces--;
26301         }
26302       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26303         cp_lexer_consume_token (parser->lexer);
26304       else
26305         {
26306           if (!collapse_err)
26307             {
26308               error_at (cp_lexer_peek_token (parser->lexer)->location,
26309                         "collapsed loops not perfectly nested");
26310             }
26311           collapse_err = true;
26312           cp_parser_statement_seq_opt (parser, NULL);
26313           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26314             break;
26315         }
26316     }
26317
26318   while (!VEC_empty (tree, for_block))
26319     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26320   release_tree_vector (for_block);
26321
26322   return ret;
26323 }
26324
26325 /* OpenMP 2.5:
26326    #pragma omp for for-clause[optseq] new-line
26327      for-loop  */
26328
26329 #define OMP_FOR_CLAUSE_MASK                             \
26330         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26331         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26332         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26333         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26334         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26335         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26336         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26337         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26338
26339 static tree
26340 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26341 {
26342   tree clauses, sb, ret;
26343   unsigned int save;
26344
26345   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26346                                        "#pragma omp for", pragma_tok);
26347
26348   sb = begin_omp_structured_block ();
26349   save = cp_parser_begin_omp_structured_block (parser);
26350
26351   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26352
26353   cp_parser_end_omp_structured_block (parser, save);
26354   add_stmt (finish_omp_structured_block (sb));
26355
26356   return ret;
26357 }
26358
26359 /* OpenMP 2.5:
26360    # pragma omp master new-line
26361      structured-block  */
26362
26363 static tree
26364 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26365 {
26366   cp_parser_require_pragma_eol (parser, pragma_tok);
26367   return c_finish_omp_master (input_location,
26368                               cp_parser_omp_structured_block (parser));
26369 }
26370
26371 /* OpenMP 2.5:
26372    # pragma omp ordered new-line
26373      structured-block  */
26374
26375 static tree
26376 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26377 {
26378   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26379   cp_parser_require_pragma_eol (parser, pragma_tok);
26380   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26381 }
26382
26383 /* OpenMP 2.5:
26384
26385    section-scope:
26386      { section-sequence }
26387
26388    section-sequence:
26389      section-directive[opt] structured-block
26390      section-sequence section-directive structured-block  */
26391
26392 static tree
26393 cp_parser_omp_sections_scope (cp_parser *parser)
26394 {
26395   tree stmt, substmt;
26396   bool error_suppress = false;
26397   cp_token *tok;
26398
26399   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26400     return NULL_TREE;
26401
26402   stmt = push_stmt_list ();
26403
26404   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26405     {
26406       unsigned save;
26407
26408       substmt = begin_omp_structured_block ();
26409       save = cp_parser_begin_omp_structured_block (parser);
26410
26411       while (1)
26412         {
26413           cp_parser_statement (parser, NULL_TREE, false, NULL);
26414
26415           tok = cp_lexer_peek_token (parser->lexer);
26416           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26417             break;
26418           if (tok->type == CPP_CLOSE_BRACE)
26419             break;
26420           if (tok->type == CPP_EOF)
26421             break;
26422         }
26423
26424       cp_parser_end_omp_structured_block (parser, save);
26425       substmt = finish_omp_structured_block (substmt);
26426       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26427       add_stmt (substmt);
26428     }
26429
26430   while (1)
26431     {
26432       tok = cp_lexer_peek_token (parser->lexer);
26433       if (tok->type == CPP_CLOSE_BRACE)
26434         break;
26435       if (tok->type == CPP_EOF)
26436         break;
26437
26438       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26439         {
26440           cp_lexer_consume_token (parser->lexer);
26441           cp_parser_require_pragma_eol (parser, tok);
26442           error_suppress = false;
26443         }
26444       else if (!error_suppress)
26445         {
26446           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26447           error_suppress = true;
26448         }
26449
26450       substmt = cp_parser_omp_structured_block (parser);
26451       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26452       add_stmt (substmt);
26453     }
26454   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26455
26456   substmt = pop_stmt_list (stmt);
26457
26458   stmt = make_node (OMP_SECTIONS);
26459   TREE_TYPE (stmt) = void_type_node;
26460   OMP_SECTIONS_BODY (stmt) = substmt;
26461
26462   add_stmt (stmt);
26463   return stmt;
26464 }
26465
26466 /* OpenMP 2.5:
26467    # pragma omp sections sections-clause[optseq] newline
26468      sections-scope  */
26469
26470 #define OMP_SECTIONS_CLAUSE_MASK                        \
26471         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26472         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26473         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26474         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26475         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26476
26477 static tree
26478 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26479 {
26480   tree clauses, ret;
26481
26482   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26483                                        "#pragma omp sections", pragma_tok);
26484
26485   ret = cp_parser_omp_sections_scope (parser);
26486   if (ret)
26487     OMP_SECTIONS_CLAUSES (ret) = clauses;
26488
26489   return ret;
26490 }
26491
26492 /* OpenMP 2.5:
26493    # pragma parallel parallel-clause new-line
26494    # pragma parallel for parallel-for-clause new-line
26495    # pragma parallel sections parallel-sections-clause new-line  */
26496
26497 #define OMP_PARALLEL_CLAUSE_MASK                        \
26498         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26499         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26500         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26501         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26502         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26503         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26504         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26505         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26506
26507 static tree
26508 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26509 {
26510   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26511   const char *p_name = "#pragma omp parallel";
26512   tree stmt, clauses, par_clause, ws_clause, block;
26513   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26514   unsigned int save;
26515   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26516
26517   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26518     {
26519       cp_lexer_consume_token (parser->lexer);
26520       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26521       p_name = "#pragma omp parallel for";
26522       mask |= OMP_FOR_CLAUSE_MASK;
26523       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26524     }
26525   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26526     {
26527       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26528       const char *p = IDENTIFIER_POINTER (id);
26529       if (strcmp (p, "sections") == 0)
26530         {
26531           cp_lexer_consume_token (parser->lexer);
26532           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26533           p_name = "#pragma omp parallel sections";
26534           mask |= OMP_SECTIONS_CLAUSE_MASK;
26535           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26536         }
26537     }
26538
26539   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26540   block = begin_omp_parallel ();
26541   save = cp_parser_begin_omp_structured_block (parser);
26542
26543   switch (p_kind)
26544     {
26545     case PRAGMA_OMP_PARALLEL:
26546       cp_parser_statement (parser, NULL_TREE, false, NULL);
26547       par_clause = clauses;
26548       break;
26549
26550     case PRAGMA_OMP_PARALLEL_FOR:
26551       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26552       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26553       break;
26554
26555     case PRAGMA_OMP_PARALLEL_SECTIONS:
26556       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26557       stmt = cp_parser_omp_sections_scope (parser);
26558       if (stmt)
26559         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26560       break;
26561
26562     default:
26563       gcc_unreachable ();
26564     }
26565
26566   cp_parser_end_omp_structured_block (parser, save);
26567   stmt = finish_omp_parallel (par_clause, block);
26568   if (p_kind != PRAGMA_OMP_PARALLEL)
26569     OMP_PARALLEL_COMBINED (stmt) = 1;
26570   return stmt;
26571 }
26572
26573 /* OpenMP 2.5:
26574    # pragma omp single single-clause[optseq] new-line
26575      structured-block  */
26576
26577 #define OMP_SINGLE_CLAUSE_MASK                          \
26578         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26579         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26580         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26581         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26582
26583 static tree
26584 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26585 {
26586   tree stmt = make_node (OMP_SINGLE);
26587   TREE_TYPE (stmt) = void_type_node;
26588
26589   OMP_SINGLE_CLAUSES (stmt)
26590     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26591                                  "#pragma omp single", pragma_tok);
26592   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26593
26594   return add_stmt (stmt);
26595 }
26596
26597 /* OpenMP 3.0:
26598    # pragma omp task task-clause[optseq] new-line
26599      structured-block  */
26600
26601 #define OMP_TASK_CLAUSE_MASK                            \
26602         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26603         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26604         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26605         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26606         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26607         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26608         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26609         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26610
26611 static tree
26612 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26613 {
26614   tree clauses, block;
26615   unsigned int save;
26616
26617   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26618                                        "#pragma omp task", pragma_tok);
26619   block = begin_omp_task ();
26620   save = cp_parser_begin_omp_structured_block (parser);
26621   cp_parser_statement (parser, NULL_TREE, false, NULL);
26622   cp_parser_end_omp_structured_block (parser, save);
26623   return finish_omp_task (clauses, block);
26624 }
26625
26626 /* OpenMP 3.0:
26627    # pragma omp taskwait new-line  */
26628
26629 static void
26630 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26631 {
26632   cp_parser_require_pragma_eol (parser, pragma_tok);
26633   finish_omp_taskwait ();
26634 }
26635
26636 /* OpenMP 3.1:
26637    # pragma omp taskyield new-line  */
26638
26639 static void
26640 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26641 {
26642   cp_parser_require_pragma_eol (parser, pragma_tok);
26643   finish_omp_taskyield ();
26644 }
26645
26646 /* OpenMP 2.5:
26647    # pragma omp threadprivate (variable-list) */
26648
26649 static void
26650 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26651 {
26652   tree vars;
26653
26654   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26655   cp_parser_require_pragma_eol (parser, pragma_tok);
26656
26657   finish_omp_threadprivate (vars);
26658 }
26659
26660 /* Main entry point to OpenMP statement pragmas.  */
26661
26662 static void
26663 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26664 {
26665   tree stmt;
26666
26667   switch (pragma_tok->pragma_kind)
26668     {
26669     case PRAGMA_OMP_ATOMIC:
26670       cp_parser_omp_atomic (parser, pragma_tok);
26671       return;
26672     case PRAGMA_OMP_CRITICAL:
26673       stmt = cp_parser_omp_critical (parser, pragma_tok);
26674       break;
26675     case PRAGMA_OMP_FOR:
26676       stmt = cp_parser_omp_for (parser, pragma_tok);
26677       break;
26678     case PRAGMA_OMP_MASTER:
26679       stmt = cp_parser_omp_master (parser, pragma_tok);
26680       break;
26681     case PRAGMA_OMP_ORDERED:
26682       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26683       break;
26684     case PRAGMA_OMP_PARALLEL:
26685       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26686       break;
26687     case PRAGMA_OMP_SECTIONS:
26688       stmt = cp_parser_omp_sections (parser, pragma_tok);
26689       break;
26690     case PRAGMA_OMP_SINGLE:
26691       stmt = cp_parser_omp_single (parser, pragma_tok);
26692       break;
26693     case PRAGMA_OMP_TASK:
26694       stmt = cp_parser_omp_task (parser, pragma_tok);
26695       break;
26696     default:
26697       gcc_unreachable ();
26698     }
26699
26700   if (stmt)
26701     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26702 }
26703 \f
26704 /* Transactional Memory parsing routines.  */
26705
26706 /* Parse a transaction attribute.
26707
26708    txn-attribute:
26709         attribute
26710         [ [ identifier ] ]
26711
26712    ??? Simplify this when C++0x bracket attributes are
26713    implemented properly.  */
26714
26715 static tree
26716 cp_parser_txn_attribute_opt (cp_parser *parser)
26717 {
26718   cp_token *token;
26719   tree attr_name, attr = NULL;
26720
26721   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
26722     return cp_parser_attributes_opt (parser);
26723
26724   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
26725     return NULL_TREE;
26726   cp_lexer_consume_token (parser->lexer);
26727   if (!cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE))
26728     goto error1;
26729
26730   token = cp_lexer_peek_token (parser->lexer);
26731   if (token->type == CPP_NAME || token->type == CPP_KEYWORD)
26732     {
26733       token = cp_lexer_consume_token (parser->lexer);
26734
26735       attr_name = (token->type == CPP_KEYWORD
26736                    /* For keywords, use the canonical spelling,
26737                       not the parsed identifier.  */
26738                    ? ridpointers[(int) token->keyword]
26739                    : token->u.value);
26740       attr = build_tree_list (attr_name, NULL_TREE);
26741     }
26742   else
26743     cp_parser_error (parser, "expected identifier");
26744
26745   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26746  error1:
26747   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
26748   return attr;
26749 }
26750
26751 /* Parse a __transaction_atomic or __transaction_relaxed statement.
26752
26753    transaction-statement:
26754      __transaction_atomic txn-attribute[opt] txn-exception-spec[opt]
26755        compound-statement
26756      __transaction_relaxed txn-exception-spec[opt] compound-statement
26757
26758    ??? The exception specification is not yet implemented.
26759 */
26760
26761 static tree
26762 cp_parser_transaction (cp_parser *parser, enum rid keyword)
26763 {
26764   unsigned char old_in = parser->in_transaction;
26765   unsigned char this_in = 1, new_in;
26766   cp_token *token;
26767   tree stmt, attrs;
26768
26769   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26770       || keyword == RID_TRANSACTION_RELAXED);
26771   token = cp_parser_require_keyword (parser, keyword,
26772       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26773           : RT_TRANSACTION_RELAXED));
26774   gcc_assert (token != NULL);
26775
26776   if (keyword == RID_TRANSACTION_RELAXED)
26777     this_in |= TM_STMT_ATTR_RELAXED;
26778   else
26779     {
26780       attrs = cp_parser_txn_attribute_opt (parser);
26781       if (attrs)
26782         this_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26783     }
26784
26785   /* Keep track if we're in the lexical scope of an outer transaction.  */
26786   new_in = this_in | (old_in & TM_STMT_ATTR_OUTER);
26787
26788   stmt = begin_transaction_stmt (token->location, NULL, this_in);
26789
26790   parser->in_transaction = new_in;
26791   cp_parser_compound_statement (parser, NULL, false, false);
26792   parser->in_transaction = old_in;
26793
26794   finish_transaction_stmt (stmt, NULL, this_in);
26795
26796   return stmt;
26797 }
26798
26799 /* Parse a __transaction_atomic or __transaction_relaxed expression.
26800
26801    transaction-expression:
26802      __transaction_atomic txn-exception-spec[opt] ( expression )
26803      __transaction_relaxed txn-exception-spec[opt] ( expression )
26804
26805    ??? The exception specification is not yet implemented.
26806 */
26807
26808 static tree
26809 cp_parser_transaction_expression (cp_parser *parser, enum rid keyword)
26810 {
26811   unsigned char old_in = parser->in_transaction;
26812   unsigned char this_in = 1;
26813   cp_token *token;
26814   tree ret;
26815
26816   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26817       || keyword == RID_TRANSACTION_RELAXED);
26818
26819   if (!flag_tm)
26820     error (keyword == RID_TRANSACTION_RELAXED
26821            ? G_("%<__transaction_relaxed%> without transactional memory "
26822                 "support enabled")
26823            : G_("%<__transaction_atomic%> without transactional memory "
26824                 "support enabled"));
26825
26826   token = cp_parser_require_keyword (parser, keyword,
26827       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26828           : RT_TRANSACTION_RELAXED));
26829   gcc_assert (token != NULL);
26830
26831   if (keyword == RID_TRANSACTION_RELAXED)
26832     this_in |= TM_STMT_ATTR_RELAXED;
26833
26834   parser->in_transaction = this_in;
26835   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
26836     {
26837       tree expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
26838       ret = build_transaction_expr (token->location, expr, this_in);
26839     }
26840   else
26841     {
26842       cp_parser_error (parser, "expected %<(%>");
26843       ret = error_mark_node;
26844     }
26845   parser->in_transaction = old_in;
26846
26847   if (cp_parser_non_integral_constant_expression (parser, NIC_TRANSACTION))
26848     return error_mark_node;
26849
26850   return (flag_tm ? ret : error_mark_node);
26851 }
26852
26853 /* Parse a function-transaction-block.
26854
26855    function-transaction-block:
26856      __transaction_atomic txn-attribute[opt] ctor-initializer[opt]
26857          function-body
26858      __transaction_atomic txn-attribute[opt] function-try-block
26859      __transaction_relaxed ctor-initializer[opt] function-body
26860      __transaction_relaxed function-try-block
26861 */
26862
26863 static bool
26864 cp_parser_function_transaction (cp_parser *parser, enum rid keyword)
26865 {
26866   unsigned char old_in = parser->in_transaction;
26867   unsigned char new_in = 1;
26868   tree compound_stmt, stmt, attrs;
26869   bool ctor_initializer_p;
26870   cp_token *token;
26871
26872   gcc_assert (keyword == RID_TRANSACTION_ATOMIC
26873       || keyword == RID_TRANSACTION_RELAXED);
26874   token = cp_parser_require_keyword (parser, keyword,
26875       (keyword == RID_TRANSACTION_ATOMIC ? RT_TRANSACTION_ATOMIC
26876           : RT_TRANSACTION_RELAXED));
26877   gcc_assert (token != NULL);
26878
26879   if (keyword == RID_TRANSACTION_RELAXED)
26880     new_in |= TM_STMT_ATTR_RELAXED;
26881   else
26882     {
26883       attrs = cp_parser_txn_attribute_opt (parser);
26884       if (attrs)
26885         new_in |= parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER);
26886     }
26887
26888   stmt = begin_transaction_stmt (token->location, &compound_stmt, new_in);
26889
26890   parser->in_transaction = new_in;
26891
26892   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
26893     ctor_initializer_p = cp_parser_function_try_block (parser);
26894   else
26895     ctor_initializer_p
26896       = cp_parser_ctor_initializer_opt_and_function_body (parser);
26897
26898   parser->in_transaction = old_in;
26899
26900   finish_transaction_stmt (stmt, compound_stmt, new_in);
26901
26902   return ctor_initializer_p;
26903 }
26904
26905 /* Parse a __transaction_cancel statement.
26906
26907    cancel-statement:
26908      __transaction_cancel txn-attribute[opt] ;
26909      __transaction_cancel txn-attribute[opt] throw-expression ;
26910
26911    ??? Cancel and throw is not yet implemented.  */
26912
26913 static tree
26914 cp_parser_transaction_cancel (cp_parser *parser)
26915 {
26916   cp_token *token;
26917   bool is_outer = false;
26918   tree stmt, attrs;
26919
26920   token = cp_parser_require_keyword (parser, RID_TRANSACTION_CANCEL,
26921                                      RT_TRANSACTION_CANCEL);
26922   gcc_assert (token != NULL);
26923
26924   attrs = cp_parser_txn_attribute_opt (parser);
26925   if (attrs)
26926     is_outer = (parse_tm_stmt_attr (attrs, TM_STMT_ATTR_OUTER) != 0);
26927
26928   /* ??? Parse cancel-and-throw here.  */
26929
26930   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26931
26932   if (!flag_tm)
26933     {
26934       error_at (token->location, "%<__transaction_cancel%> without "
26935                 "transactional memory support enabled");
26936       return error_mark_node;
26937     }
26938   else if (parser->in_transaction & TM_STMT_ATTR_RELAXED)
26939     {
26940       error_at (token->location, "%<__transaction_cancel%> within a "
26941                 "%<__transaction_relaxed%>");
26942       return error_mark_node;
26943     }
26944   else if (is_outer)
26945     {
26946       if ((parser->in_transaction & TM_STMT_ATTR_OUTER) == 0
26947           && !is_tm_may_cancel_outer (current_function_decl))
26948         {
26949           error_at (token->location, "outer %<__transaction_cancel%> not "
26950                     "within outer %<__transaction_atomic%>");
26951           error_at (token->location,
26952                     "  or a %<transaction_may_cancel_outer%> function");
26953           return error_mark_node;
26954         }
26955     }
26956   else if (parser->in_transaction == 0)
26957     {
26958       error_at (token->location, "%<__transaction_cancel%> not within "
26959                 "%<__transaction_atomic%>");
26960       return error_mark_node;
26961     }
26962
26963   stmt = build_tm_abort_call (token->location, is_outer);
26964   add_stmt (stmt);
26965   finish_stmt ();
26966
26967   return stmt;
26968 }
26969 \f
26970 /* The parser.  */
26971
26972 static GTY (()) cp_parser *the_parser;
26973
26974 \f
26975 /* Special handling for the first token or line in the file.  The first
26976    thing in the file might be #pragma GCC pch_preprocess, which loads a
26977    PCH file, which is a GC collection point.  So we need to handle this
26978    first pragma without benefit of an existing lexer structure.
26979
26980    Always returns one token to the caller in *FIRST_TOKEN.  This is
26981    either the true first token of the file, or the first token after
26982    the initial pragma.  */
26983
26984 static void
26985 cp_parser_initial_pragma (cp_token *first_token)
26986 {
26987   tree name = NULL;
26988
26989   cp_lexer_get_preprocessor_token (NULL, first_token);
26990   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
26991     return;
26992
26993   cp_lexer_get_preprocessor_token (NULL, first_token);
26994   if (first_token->type == CPP_STRING)
26995     {
26996       name = first_token->u.value;
26997
26998       cp_lexer_get_preprocessor_token (NULL, first_token);
26999       if (first_token->type != CPP_PRAGMA_EOL)
27000         error_at (first_token->location,
27001                   "junk at end of %<#pragma GCC pch_preprocess%>");
27002     }
27003   else
27004     error_at (first_token->location, "expected string literal");
27005
27006   /* Skip to the end of the pragma.  */
27007   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
27008     cp_lexer_get_preprocessor_token (NULL, first_token);
27009
27010   /* Now actually load the PCH file.  */
27011   if (name)
27012     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
27013
27014   /* Read one more token to return to our caller.  We have to do this
27015      after reading the PCH file in, since its pointers have to be
27016      live.  */
27017   cp_lexer_get_preprocessor_token (NULL, first_token);
27018 }
27019
27020 /* Normal parsing of a pragma token.  Here we can (and must) use the
27021    regular lexer.  */
27022
27023 static bool
27024 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
27025 {
27026   cp_token *pragma_tok;
27027   unsigned int id;
27028
27029   pragma_tok = cp_lexer_consume_token (parser->lexer);
27030   gcc_assert (pragma_tok->type == CPP_PRAGMA);
27031   parser->lexer->in_pragma = true;
27032
27033   id = pragma_tok->pragma_kind;
27034   switch (id)
27035     {
27036     case PRAGMA_GCC_PCH_PREPROCESS:
27037       error_at (pragma_tok->location,
27038                 "%<#pragma GCC pch_preprocess%> must be first");
27039       break;
27040
27041     case PRAGMA_OMP_BARRIER:
27042       switch (context)
27043         {
27044         case pragma_compound:
27045           cp_parser_omp_barrier (parser, pragma_tok);
27046           return false;
27047         case pragma_stmt:
27048           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
27049                     "used in compound statements");
27050           break;
27051         default:
27052           goto bad_stmt;
27053         }
27054       break;
27055
27056     case PRAGMA_OMP_FLUSH:
27057       switch (context)
27058         {
27059         case pragma_compound:
27060           cp_parser_omp_flush (parser, pragma_tok);
27061           return false;
27062         case pragma_stmt:
27063           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
27064                     "used in compound statements");
27065           break;
27066         default:
27067           goto bad_stmt;
27068         }
27069       break;
27070
27071     case PRAGMA_OMP_TASKWAIT:
27072       switch (context)
27073         {
27074         case pragma_compound:
27075           cp_parser_omp_taskwait (parser, pragma_tok);
27076           return false;
27077         case pragma_stmt:
27078           error_at (pragma_tok->location,
27079                     "%<#pragma omp taskwait%> may only be "
27080                     "used in compound statements");
27081           break;
27082         default:
27083           goto bad_stmt;
27084         }
27085       break;
27086
27087     case PRAGMA_OMP_TASKYIELD:
27088       switch (context)
27089         {
27090         case pragma_compound:
27091           cp_parser_omp_taskyield (parser, pragma_tok);
27092           return false;
27093         case pragma_stmt:
27094           error_at (pragma_tok->location,
27095                     "%<#pragma omp taskyield%> may only be "
27096                     "used in compound statements");
27097           break;
27098         default:
27099           goto bad_stmt;
27100         }
27101       break;
27102
27103     case PRAGMA_OMP_THREADPRIVATE:
27104       cp_parser_omp_threadprivate (parser, pragma_tok);
27105       return false;
27106
27107     case PRAGMA_OMP_ATOMIC:
27108     case PRAGMA_OMP_CRITICAL:
27109     case PRAGMA_OMP_FOR:
27110     case PRAGMA_OMP_MASTER:
27111     case PRAGMA_OMP_ORDERED:
27112     case PRAGMA_OMP_PARALLEL:
27113     case PRAGMA_OMP_SECTIONS:
27114     case PRAGMA_OMP_SINGLE:
27115     case PRAGMA_OMP_TASK:
27116       if (context == pragma_external)
27117         goto bad_stmt;
27118       cp_parser_omp_construct (parser, pragma_tok);
27119       return true;
27120
27121     case PRAGMA_OMP_SECTION:
27122       error_at (pragma_tok->location, 
27123                 "%<#pragma omp section%> may only be used in "
27124                 "%<#pragma omp sections%> construct");
27125       break;
27126
27127     default:
27128       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
27129       c_invoke_pragma_handler (id);
27130       break;
27131
27132     bad_stmt:
27133       cp_parser_error (parser, "expected declaration specifiers");
27134       break;
27135     }
27136
27137   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
27138   return false;
27139 }
27140
27141 /* The interface the pragma parsers have to the lexer.  */
27142
27143 enum cpp_ttype
27144 pragma_lex (tree *value)
27145 {
27146   cp_token *tok;
27147   enum cpp_ttype ret;
27148
27149   tok = cp_lexer_peek_token (the_parser->lexer);
27150
27151   ret = tok->type;
27152   *value = tok->u.value;
27153
27154   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
27155     ret = CPP_EOF;
27156   else if (ret == CPP_STRING)
27157     *value = cp_parser_string_literal (the_parser, false, false);
27158   else
27159     {
27160       cp_lexer_consume_token (the_parser->lexer);
27161       if (ret == CPP_KEYWORD)
27162         ret = CPP_NAME;
27163     }
27164
27165   return ret;
27166 }
27167
27168 \f
27169 /* External interface.  */
27170
27171 /* Parse one entire translation unit.  */
27172
27173 void
27174 c_parse_file (void)
27175 {
27176   static bool already_called = false;
27177
27178   if (already_called)
27179     {
27180       sorry ("inter-module optimizations not implemented for C++");
27181       return;
27182     }
27183   already_called = true;
27184
27185   the_parser = cp_parser_new ();
27186   push_deferring_access_checks (flag_access_control
27187                                 ? dk_no_deferred : dk_no_check);
27188   cp_parser_translation_unit (the_parser);
27189   the_parser = NULL;
27190 }
27191
27192 #include "gt-cp-parser.h"