OSDN Git Service

Implement C++11 user-defined literals.
[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 } non_integral_constant;
111
112 /* The various kinds of errors about name-lookup failing. */
113 typedef enum name_lookup_error {
114   /* NULL */
115   NLE_NULL,
116   /* is not a type */
117   NLE_TYPE,
118   /* is not a class or namespace */
119   NLE_CXX98,
120   /* is not a class, namespace, or enumeration */
121   NLE_NOT_CXX98
122 } name_lookup_error;
123
124 /* The various kinds of required token */
125 typedef enum required_token {
126   RT_NONE,
127   RT_SEMICOLON,  /* ';' */
128   RT_OPEN_PAREN, /* '(' */
129   RT_CLOSE_BRACE, /* '}' */
130   RT_OPEN_BRACE,  /* '{' */
131   RT_CLOSE_SQUARE, /* ']' */
132   RT_OPEN_SQUARE,  /* '[' */
133   RT_COMMA, /* ',' */
134   RT_SCOPE, /* '::' */
135   RT_LESS, /* '<' */
136   RT_GREATER, /* '>' */
137   RT_EQ, /* '=' */
138   RT_ELLIPSIS, /* '...' */
139   RT_MULT, /* '*' */
140   RT_COMPL, /* '~' */
141   RT_COLON, /* ':' */
142   RT_COLON_SCOPE, /* ':' or '::' */
143   RT_CLOSE_PAREN, /* ')' */
144   RT_COMMA_CLOSE_PAREN, /* ',' or ')' */
145   RT_PRAGMA_EOL, /* end of line */
146   RT_NAME, /* identifier */
147
148   /* The type is CPP_KEYWORD */
149   RT_NEW, /* new */
150   RT_DELETE, /* delete */
151   RT_RETURN, /* return */
152   RT_WHILE, /* while */
153   RT_EXTERN, /* extern */
154   RT_STATIC_ASSERT, /* static_assert */
155   RT_DECLTYPE, /* decltype */
156   RT_OPERATOR, /* operator */
157   RT_CLASS, /* class */
158   RT_TEMPLATE, /* template */
159   RT_NAMESPACE, /* namespace */
160   RT_USING, /* using */
161   RT_ASM, /* asm */
162   RT_TRY, /* try */
163   RT_CATCH, /* catch */
164   RT_THROW, /* throw */
165   RT_LABEL, /* __label__ */
166   RT_AT_TRY, /* @try */
167   RT_AT_SYNCHRONIZED, /* @synchronized */
168   RT_AT_THROW, /* @throw */
169
170   RT_SELECT,  /* selection-statement */
171   RT_INTERATION, /* iteration-statement */
172   RT_JUMP, /* jump-statement */
173   RT_CLASS_KEY, /* class-key */
174   RT_CLASS_TYPENAME_TEMPLATE /* class, typename, or template */
175 } required_token;
176
177 /* Prototypes.  */
178
179 static cp_lexer *cp_lexer_new_main
180   (void);
181 static cp_lexer *cp_lexer_new_from_tokens
182   (cp_token_cache *tokens);
183 static void cp_lexer_destroy
184   (cp_lexer *);
185 static int cp_lexer_saving_tokens
186   (const cp_lexer *);
187 static cp_token *cp_lexer_token_at
188   (cp_lexer *, cp_token_position);
189 static void cp_lexer_get_preprocessor_token
190   (cp_lexer *, cp_token *);
191 static inline cp_token *cp_lexer_peek_token
192   (cp_lexer *);
193 static cp_token *cp_lexer_peek_nth_token
194   (cp_lexer *, size_t);
195 static inline bool cp_lexer_next_token_is
196   (cp_lexer *, enum cpp_ttype);
197 static bool cp_lexer_next_token_is_not
198   (cp_lexer *, enum cpp_ttype);
199 static bool cp_lexer_next_token_is_keyword
200   (cp_lexer *, enum rid);
201 static cp_token *cp_lexer_consume_token
202   (cp_lexer *);
203 static void cp_lexer_purge_token
204   (cp_lexer *);
205 static void cp_lexer_purge_tokens_after
206   (cp_lexer *, cp_token_position);
207 static void cp_lexer_save_tokens
208   (cp_lexer *);
209 static void cp_lexer_commit_tokens
210   (cp_lexer *);
211 static void cp_lexer_rollback_tokens
212   (cp_lexer *);
213 static void cp_lexer_print_token
214   (FILE *, cp_token *);
215 static inline bool cp_lexer_debugging_p
216   (cp_lexer *);
217 static void cp_lexer_start_debugging
218   (cp_lexer *) ATTRIBUTE_UNUSED;
219 static void cp_lexer_stop_debugging
220   (cp_lexer *) ATTRIBUTE_UNUSED;
221
222 static cp_token_cache *cp_token_cache_new
223   (cp_token *, cp_token *);
224
225 static void cp_parser_initial_pragma
226   (cp_token *);
227
228 static tree cp_literal_operator_id
229   (const char *);
230
231 /* Manifest constants.  */
232 #define CP_LEXER_BUFFER_SIZE ((256 * 1024) / sizeof (cp_token))
233 #define CP_SAVED_TOKEN_STACK 5
234
235 /* Variables.  */
236
237 /* The stream to which debugging output should be written.  */
238 static FILE *cp_lexer_debug_stream;
239
240 /* Nonzero if we are parsing an unevaluated operand: an operand to
241    sizeof, typeof, or alignof.  */
242 int cp_unevaluated_operand;
243
244 /* Dump up to NUM tokens in BUFFER to FILE starting with token
245    START_TOKEN.  If START_TOKEN is NULL, the dump starts with the
246    first token in BUFFER.  If NUM is 0, dump all the tokens.  If
247    CURR_TOKEN is set and it is one of the tokens in BUFFER, it will be
248    highlighted by surrounding it in [[ ]].  */
249
250 static void
251 cp_lexer_dump_tokens (FILE *file, VEC(cp_token,gc) *buffer,
252                       cp_token *start_token, unsigned num,
253                       cp_token *curr_token)
254 {
255   unsigned i, nprinted;
256   cp_token *token;
257   bool do_print;
258
259   fprintf (file, "%u tokens\n", VEC_length (cp_token, buffer));
260
261   if (buffer == NULL)
262     return;
263
264   if (num == 0)
265     num = VEC_length (cp_token, buffer);
266
267   if (start_token == NULL)
268     start_token = VEC_address (cp_token, buffer);
269
270   if (start_token > VEC_address (cp_token, buffer))
271     {
272       cp_lexer_print_token (file, VEC_index (cp_token, buffer, 0));
273       fprintf (file, " ... ");
274     }
275
276   do_print = false;
277   nprinted = 0;
278   for (i = 0; VEC_iterate (cp_token, buffer, i, token) && nprinted < num; i++)
279     {
280       if (token == start_token)
281         do_print = true;
282
283       if (!do_print)
284         continue;
285
286       nprinted++;
287       if (token == curr_token)
288         fprintf (file, "[[");
289
290       cp_lexer_print_token (file, token);
291
292       if (token == curr_token)
293         fprintf (file, "]]");
294
295       switch (token->type)
296         {
297           case CPP_SEMICOLON:
298           case CPP_OPEN_BRACE:
299           case CPP_CLOSE_BRACE:
300           case CPP_EOF:
301             fputc ('\n', file);
302             break;
303
304           default:
305             fputc (' ', file);
306         }
307     }
308
309   if (i == num && i < VEC_length (cp_token, buffer))
310     {
311       fprintf (file, " ... ");
312       cp_lexer_print_token (file, VEC_index (cp_token, buffer,
313                             VEC_length (cp_token, buffer) - 1));
314     }
315
316   fprintf (file, "\n");
317 }
318
319
320 /* Dump all tokens in BUFFER to stderr.  */
321
322 void
323 cp_lexer_debug_tokens (VEC(cp_token,gc) *buffer)
324 {
325   cp_lexer_dump_tokens (stderr, buffer, NULL, 0, NULL);
326 }
327
328
329 /* Dump the cp_parser tree field T to FILE if T is non-NULL.  DESC is the
330    description for T.  */
331
332 static void
333 cp_debug_print_tree_if_set (FILE *file, const char *desc, tree t)
334 {
335   if (t)
336     {
337       fprintf (file, "%s: ", desc);
338       print_node_brief (file, "", t, 0);
339     }
340 }
341
342
343 /* Dump parser context C to FILE.  */
344
345 static void
346 cp_debug_print_context (FILE *file, cp_parser_context *c)
347 {
348   const char *status_s[] = { "OK", "ERROR", "COMMITTED" };
349   fprintf (file, "{ status = %s, scope = ", status_s[c->status]);
350   print_node_brief (file, "", c->object_type, 0);
351   fprintf (file, "}\n");
352 }
353
354
355 /* Print the stack of parsing contexts to FILE starting with FIRST.  */
356
357 static void
358 cp_debug_print_context_stack (FILE *file, cp_parser_context *first)
359 {
360   unsigned i;
361   cp_parser_context *c;
362
363   fprintf (file, "Parsing context stack:\n");
364   for (i = 0, c = first; c; c = c->next, i++)
365     {
366       fprintf (file, "\t#%u: ", i);
367       cp_debug_print_context (file, c);
368     }
369 }
370
371
372 /* Print the value of FLAG to FILE.  DESC is a string describing the flag.  */
373
374 static void
375 cp_debug_print_flag (FILE *file, const char *desc, bool flag)
376 {
377   if (flag)
378     fprintf (file, "%s: true\n", desc);
379 }
380
381
382 /* Print an unparsed function entry UF to FILE.  */
383
384 static void
385 cp_debug_print_unparsed_function (FILE *file, cp_unparsed_functions_entry *uf)
386 {
387   unsigned i;
388   cp_default_arg_entry *default_arg_fn;
389   tree fn;
390
391   fprintf (file, "\tFunctions with default args:\n");
392   for (i = 0;
393        VEC_iterate (cp_default_arg_entry, uf->funs_with_default_args, i,
394                     default_arg_fn);
395        i++)
396     {
397       fprintf (file, "\t\tClass type: ");
398       print_node_brief (file, "", default_arg_fn->class_type, 0);
399       fprintf (file, "\t\tDeclaration: ");
400       print_node_brief (file, "", default_arg_fn->decl, 0);
401       fprintf (file, "\n");
402     }
403
404   fprintf (file, "\n\tFunctions with definitions that require "
405            "post-processing\n\t\t");
406   for (i = 0; VEC_iterate (tree, uf->funs_with_definitions, i, fn); i++)
407     {
408       print_node_brief (file, "", fn, 0);
409       fprintf (file, " ");
410     }
411   fprintf (file, "\n");
412
413   fprintf (file, "\n\tNon-static data members with initializers that require "
414            "post-processing\n\t\t");
415   for (i = 0; VEC_iterate (tree, uf->nsdmis, i, fn); i++)
416     {
417       print_node_brief (file, "", fn, 0);
418       fprintf (file, " ");
419     }
420   fprintf (file, "\n");
421 }
422
423
424 /* Print the stack of unparsed member functions S to FILE.  */
425
426 static void
427 cp_debug_print_unparsed_queues (FILE *file,
428                                 VEC(cp_unparsed_functions_entry, gc) *s)
429 {
430   unsigned i;
431   cp_unparsed_functions_entry *uf;
432
433   fprintf (file, "Unparsed functions\n");
434   for (i = 0; VEC_iterate (cp_unparsed_functions_entry, s, i, uf); i++)
435     {
436       fprintf (file, "#%u:\n", i);
437       cp_debug_print_unparsed_function (file, uf);
438     }
439 }
440
441
442 /* Dump the tokens in a window of size WINDOW_SIZE around the next_token for
443    the given PARSER.  If FILE is NULL, the output is printed on stderr. */
444
445 static void
446 cp_debug_parser_tokens (FILE *file, cp_parser *parser, int window_size)
447 {
448   cp_token *next_token, *first_token, *start_token;
449
450   if (file == NULL)
451     file = stderr;
452
453   next_token = parser->lexer->next_token;
454   first_token = VEC_address (cp_token, parser->lexer->buffer);
455   start_token = (next_token > first_token + window_size / 2)
456                 ? next_token - window_size / 2
457                 : first_token;
458   cp_lexer_dump_tokens (file, parser->lexer->buffer, start_token, window_size,
459                         next_token);
460 }
461
462
463 /* Dump debugging information for the given PARSER.  If FILE is NULL,
464    the output is printed on stderr.  */
465
466 void
467 cp_debug_parser (FILE *file, cp_parser *parser)
468 {
469   const size_t window_size = 20;
470   cp_token *token;
471   expanded_location eloc;
472
473   if (file == NULL)
474     file = stderr;
475
476   fprintf (file, "Parser state\n\n");
477   fprintf (file, "Number of tokens: %u\n",
478            VEC_length (cp_token, parser->lexer->buffer));
479   cp_debug_print_tree_if_set (file, "Lookup scope", parser->scope);
480   cp_debug_print_tree_if_set (file, "Object scope",
481                                      parser->object_scope);
482   cp_debug_print_tree_if_set (file, "Qualifying scope",
483                                      parser->qualifying_scope);
484   cp_debug_print_context_stack (file, parser->context);
485   cp_debug_print_flag (file, "Allow GNU extensions",
486                               parser->allow_gnu_extensions_p);
487   cp_debug_print_flag (file, "'>' token is greater-than",
488                               parser->greater_than_is_operator_p);
489   cp_debug_print_flag (file, "Default args allowed in current "
490                               "parameter list", parser->default_arg_ok_p);
491   cp_debug_print_flag (file, "Parsing integral constant-expression",
492                               parser->integral_constant_expression_p);
493   cp_debug_print_flag (file, "Allow non-constant expression in current "
494                               "constant-expression",
495                               parser->allow_non_integral_constant_expression_p);
496   cp_debug_print_flag (file, "Seen non-constant expression",
497                               parser->non_integral_constant_expression_p);
498   cp_debug_print_flag (file, "Local names and 'this' forbidden in "
499                               "current context",
500                               parser->local_variables_forbidden_p);
501   cp_debug_print_flag (file, "In unbraced linkage specification",
502                               parser->in_unbraced_linkage_specification_p);
503   cp_debug_print_flag (file, "Parsing a declarator",
504                               parser->in_declarator_p);
505   cp_debug_print_flag (file, "In template argument list",
506                               parser->in_template_argument_list_p);
507   cp_debug_print_flag (file, "Parsing an iteration statement",
508                               parser->in_statement & IN_ITERATION_STMT);
509   cp_debug_print_flag (file, "Parsing a switch statement",
510                               parser->in_statement & IN_SWITCH_STMT);
511   cp_debug_print_flag (file, "Parsing a structured OpenMP block",
512                               parser->in_statement & IN_OMP_BLOCK);
513   cp_debug_print_flag (file, "Parsing a an OpenMP loop",
514                               parser->in_statement & IN_OMP_FOR);
515   cp_debug_print_flag (file, "Parsing an if statement",
516                               parser->in_statement & IN_IF_STMT);
517   cp_debug_print_flag (file, "Parsing a type-id in an expression "
518                               "context", parser->in_type_id_in_expr_p);
519   cp_debug_print_flag (file, "Declarations are implicitly extern \"C\"",
520                               parser->implicit_extern_c);
521   cp_debug_print_flag (file, "String expressions should be translated "
522                               "to execution character set",
523                               parser->translate_strings_p);
524   cp_debug_print_flag (file, "Parsing function body outside of a "
525                               "local class", parser->in_function_body);
526   cp_debug_print_flag (file, "Auto correct a colon to a scope operator",
527                               parser->colon_corrects_to_scope_p);
528   if (parser->type_definition_forbidden_message)
529     fprintf (file, "Error message for forbidden type definitions: %s\n",
530              parser->type_definition_forbidden_message);
531   cp_debug_print_unparsed_queues (file, parser->unparsed_queues);
532   fprintf (file, "Number of class definitions in progress: %u\n",
533            parser->num_classes_being_defined);
534   fprintf (file, "Number of template parameter lists for the current "
535            "declaration: %u\n", parser->num_template_parameter_lists);
536   cp_debug_parser_tokens (file, parser, window_size);
537   token = parser->lexer->next_token;
538   fprintf (file, "Next token to parse:\n");
539   fprintf (file, "\tToken:  ");
540   cp_lexer_print_token (file, token);
541   eloc = expand_location (token->location);
542   fprintf (file, "\n\tFile:   %s\n", eloc.file);
543   fprintf (file, "\tLine:   %d\n", eloc.line);
544   fprintf (file, "\tColumn: %d\n", eloc.column);
545 }
546
547
548 /* Allocate memory for a new lexer object and return it.  */
549
550 static cp_lexer *
551 cp_lexer_alloc (void)
552 {
553   cp_lexer *lexer;
554
555   c_common_no_more_pch ();
556
557   /* Allocate the memory.  */
558   lexer = ggc_alloc_cleared_cp_lexer ();
559
560   /* Initially we are not debugging.  */
561   lexer->debugging_p = false;
562
563   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
564                                    CP_SAVED_TOKEN_STACK);
565
566   /* Create the buffer.  */
567   lexer->buffer = VEC_alloc (cp_token, gc, CP_LEXER_BUFFER_SIZE);
568
569   return lexer;
570 }
571
572
573 /* Create a new main C++ lexer, the lexer that gets tokens from the
574    preprocessor.  */
575
576 static cp_lexer *
577 cp_lexer_new_main (void)
578 {
579   cp_lexer *lexer;
580   cp_token token;
581
582   /* It's possible that parsing the first pragma will load a PCH file,
583      which is a GC collection point.  So we have to do that before
584      allocating any memory.  */
585   cp_parser_initial_pragma (&token);
586
587   lexer = cp_lexer_alloc ();
588
589   /* Put the first token in the buffer.  */
590   VEC_quick_push (cp_token, lexer->buffer, &token);
591
592   /* Get the remaining tokens from the preprocessor.  */
593   while (token.type != CPP_EOF)
594     {
595       cp_lexer_get_preprocessor_token (lexer, &token);
596       VEC_safe_push (cp_token, gc, lexer->buffer, &token);
597     }
598
599   lexer->last_token = VEC_address (cp_token, lexer->buffer)
600                       + VEC_length (cp_token, lexer->buffer)
601                       - 1;
602   lexer->next_token = VEC_length (cp_token, lexer->buffer)
603                       ? VEC_address (cp_token, lexer->buffer)
604                       : &eof_token;
605
606   /* Subsequent preprocessor diagnostics should use compiler
607      diagnostic functions to get the compiler source location.  */
608   done_lexing = true;
609
610   gcc_assert (!lexer->next_token->purged_p);
611   return lexer;
612 }
613
614 /* Create a new lexer whose token stream is primed with the tokens in
615    CACHE.  When these tokens are exhausted, no new tokens will be read.  */
616
617 static cp_lexer *
618 cp_lexer_new_from_tokens (cp_token_cache *cache)
619 {
620   cp_token *first = cache->first;
621   cp_token *last = cache->last;
622   cp_lexer *lexer = ggc_alloc_cleared_cp_lexer ();
623
624   /* We do not own the buffer.  */
625   lexer->buffer = NULL;
626   lexer->next_token = first == last ? &eof_token : first;
627   lexer->last_token = last;
628
629   lexer->saved_tokens = VEC_alloc (cp_token_position, heap,
630                                    CP_SAVED_TOKEN_STACK);
631
632   /* Initially we are not debugging.  */
633   lexer->debugging_p = false;
634
635   gcc_assert (!lexer->next_token->purged_p);
636   return lexer;
637 }
638
639 /* Frees all resources associated with LEXER.  */
640
641 static void
642 cp_lexer_destroy (cp_lexer *lexer)
643 {
644   VEC_free (cp_token, gc, lexer->buffer);
645   VEC_free (cp_token_position, heap, lexer->saved_tokens);
646   ggc_free (lexer);
647 }
648
649 /* Returns nonzero if debugging information should be output.  */
650
651 static inline bool
652 cp_lexer_debugging_p (cp_lexer *lexer)
653 {
654   return lexer->debugging_p;
655 }
656
657
658 static inline cp_token_position
659 cp_lexer_token_position (cp_lexer *lexer, bool previous_p)
660 {
661   gcc_assert (!previous_p || lexer->next_token != &eof_token);
662
663   return lexer->next_token - previous_p;
664 }
665
666 static inline cp_token *
667 cp_lexer_token_at (cp_lexer *lexer ATTRIBUTE_UNUSED, cp_token_position pos)
668 {
669   return pos;
670 }
671
672 static inline void
673 cp_lexer_set_token_position (cp_lexer *lexer, cp_token_position pos)
674 {
675   lexer->next_token = cp_lexer_token_at (lexer, pos);
676 }
677
678 static inline cp_token_position
679 cp_lexer_previous_token_position (cp_lexer *lexer)
680 {
681   if (lexer->next_token == &eof_token)
682     return lexer->last_token - 1;
683   else
684     return cp_lexer_token_position (lexer, true);
685 }
686
687 static inline cp_token *
688 cp_lexer_previous_token (cp_lexer *lexer)
689 {
690   cp_token_position tp = cp_lexer_previous_token_position (lexer);
691
692   return cp_lexer_token_at (lexer, tp);
693 }
694
695 /* nonzero if we are presently saving tokens.  */
696
697 static inline int
698 cp_lexer_saving_tokens (const cp_lexer* lexer)
699 {
700   return VEC_length (cp_token_position, lexer->saved_tokens) != 0;
701 }
702
703 /* Store the next token from the preprocessor in *TOKEN.  Return true
704    if we reach EOF.  If LEXER is NULL, assume we are handling an
705    initial #pragma pch_preprocess, and thus want the lexer to return
706    processed strings.  */
707
708 static void
709 cp_lexer_get_preprocessor_token (cp_lexer *lexer, cp_token *token)
710 {
711   static int is_extern_c = 0;
712
713    /* Get a new token from the preprocessor.  */
714   token->type
715     = c_lex_with_flags (&token->u.value, &token->location, &token->flags,
716                         lexer == NULL ? 0 : C_LEX_STRING_NO_JOIN);
717   token->keyword = RID_MAX;
718   token->pragma_kind = PRAGMA_NONE;
719   token->purged_p = false;
720
721   /* On some systems, some header files are surrounded by an
722      implicit extern "C" block.  Set a flag in the token if it
723      comes from such a header.  */
724   is_extern_c += pending_lang_change;
725   pending_lang_change = 0;
726   token->implicit_extern_c = is_extern_c > 0;
727
728   /* Check to see if this token is a keyword.  */
729   if (token->type == CPP_NAME)
730     {
731       if (C_IS_RESERVED_WORD (token->u.value))
732         {
733           /* Mark this token as a keyword.  */
734           token->type = CPP_KEYWORD;
735           /* Record which keyword.  */
736           token->keyword = C_RID_CODE (token->u.value);
737         }
738       else
739         {
740           if (warn_cxx0x_compat
741               && C_RID_CODE (token->u.value) >= RID_FIRST_CXX0X
742               && C_RID_CODE (token->u.value) <= RID_LAST_CXX0X)
743             {
744               /* Warn about the C++0x keyword (but still treat it as
745                  an identifier).  */
746               warning (OPT_Wc__0x_compat, 
747                        "identifier %qE will become a keyword in C++0x",
748                        token->u.value);
749
750               /* Clear out the C_RID_CODE so we don't warn about this
751                  particular identifier-turned-keyword again.  */
752               C_SET_RID_CODE (token->u.value, RID_MAX);
753             }
754
755           token->ambiguous_p = false;
756           token->keyword = RID_MAX;
757         }
758     }
759   else if (token->type == CPP_AT_NAME)
760     {
761       /* This only happens in Objective-C++; it must be a keyword.  */
762       token->type = CPP_KEYWORD;
763       switch (C_RID_CODE (token->u.value))
764         {
765           /* Replace 'class' with '@class', 'private' with '@private',
766              etc.  This prevents confusion with the C++ keyword
767              'class', and makes the tokens consistent with other
768              Objective-C 'AT' keywords.  For example '@class' is
769              reported as RID_AT_CLASS which is consistent with
770              '@synchronized', which is reported as
771              RID_AT_SYNCHRONIZED.
772           */
773         case RID_CLASS:     token->keyword = RID_AT_CLASS; break;
774         case RID_PRIVATE:   token->keyword = RID_AT_PRIVATE; break;
775         case RID_PROTECTED: token->keyword = RID_AT_PROTECTED; break;
776         case RID_PUBLIC:    token->keyword = RID_AT_PUBLIC; break;
777         case RID_THROW:     token->keyword = RID_AT_THROW; break;
778         case RID_TRY:       token->keyword = RID_AT_TRY; break;
779         case RID_CATCH:     token->keyword = RID_AT_CATCH; break;
780         default:            token->keyword = C_RID_CODE (token->u.value);
781         }
782     }
783   else if (token->type == CPP_PRAGMA)
784     {
785       /* We smuggled the cpp_token->u.pragma value in an INTEGER_CST.  */
786       token->pragma_kind = ((enum pragma_kind)
787                             TREE_INT_CST_LOW (token->u.value));
788       token->u.value = NULL_TREE;
789     }
790 }
791
792 /* Update the globals input_location and the input file stack from TOKEN.  */
793 static inline void
794 cp_lexer_set_source_position_from_token (cp_token *token)
795 {
796   if (token->type != CPP_EOF)
797     {
798       input_location = token->location;
799     }
800 }
801
802 /* Return a pointer to the next token in the token stream, but do not
803    consume it.  */
804
805 static inline cp_token *
806 cp_lexer_peek_token (cp_lexer *lexer)
807 {
808   if (cp_lexer_debugging_p (lexer))
809     {
810       fputs ("cp_lexer: peeking at token: ", cp_lexer_debug_stream);
811       cp_lexer_print_token (cp_lexer_debug_stream, lexer->next_token);
812       putc ('\n', cp_lexer_debug_stream);
813     }
814   return lexer->next_token;
815 }
816
817 /* Return true if the next token has the indicated TYPE.  */
818
819 static inline bool
820 cp_lexer_next_token_is (cp_lexer* lexer, enum cpp_ttype type)
821 {
822   return cp_lexer_peek_token (lexer)->type == type;
823 }
824
825 /* Return true if the next token does not have the indicated TYPE.  */
826
827 static inline bool
828 cp_lexer_next_token_is_not (cp_lexer* lexer, enum cpp_ttype type)
829 {
830   return !cp_lexer_next_token_is (lexer, type);
831 }
832
833 /* Return true if the next token is the indicated KEYWORD.  */
834
835 static inline bool
836 cp_lexer_next_token_is_keyword (cp_lexer* lexer, enum rid keyword)
837 {
838   return cp_lexer_peek_token (lexer)->keyword == keyword;
839 }
840
841 /* Return true if the next token is not the indicated KEYWORD.  */
842
843 static inline bool
844 cp_lexer_next_token_is_not_keyword (cp_lexer* lexer, enum rid keyword)
845 {
846   return cp_lexer_peek_token (lexer)->keyword != keyword;
847 }
848
849 /* Return true if the next token is a keyword for a decl-specifier.  */
850
851 static bool
852 cp_lexer_next_token_is_decl_specifier_keyword (cp_lexer *lexer)
853 {
854   cp_token *token;
855
856   token = cp_lexer_peek_token (lexer);
857   switch (token->keyword) 
858     {
859       /* auto specifier: storage-class-specifier in C++,
860          simple-type-specifier in C++0x.  */
861     case RID_AUTO:
862       /* Storage classes.  */
863     case RID_REGISTER:
864     case RID_STATIC:
865     case RID_EXTERN:
866     case RID_MUTABLE:
867     case RID_THREAD:
868       /* Elaborated type specifiers.  */
869     case RID_ENUM:
870     case RID_CLASS:
871     case RID_STRUCT:
872     case RID_UNION:
873     case RID_TYPENAME:
874       /* Simple type specifiers.  */
875     case RID_CHAR:
876     case RID_CHAR16:
877     case RID_CHAR32:
878     case RID_WCHAR:
879     case RID_BOOL:
880     case RID_SHORT:
881     case RID_INT:
882     case RID_LONG:
883     case RID_INT128:
884     case RID_SIGNED:
885     case RID_UNSIGNED:
886     case RID_FLOAT:
887     case RID_DOUBLE:
888     case RID_VOID:
889       /* GNU extensions.  */ 
890     case RID_ATTRIBUTE:
891     case RID_TYPEOF:
892       /* C++0x extensions.  */
893     case RID_DECLTYPE:
894     case RID_UNDERLYING_TYPE:
895       return true;
896
897     default:
898       return false;
899     }
900 }
901
902 /* Returns TRUE iff the token T begins a decltype type.  */
903
904 static bool
905 token_is_decltype (cp_token *t)
906 {
907   return (t->keyword == RID_DECLTYPE
908           || t->type == CPP_DECLTYPE);
909 }
910
911 /* Returns TRUE iff the next token begins a decltype type.  */
912
913 static bool
914 cp_lexer_next_token_is_decltype (cp_lexer *lexer)
915 {
916   cp_token *t = cp_lexer_peek_token (lexer);
917   return token_is_decltype (t);
918 }
919
920 /* Return a pointer to the Nth token in the token stream.  If N is 1,
921    then this is precisely equivalent to cp_lexer_peek_token (except
922    that it is not inline).  One would like to disallow that case, but
923    there is one case (cp_parser_nth_token_starts_template_id) where
924    the caller passes a variable for N and it might be 1.  */
925
926 static cp_token *
927 cp_lexer_peek_nth_token (cp_lexer* lexer, size_t n)
928 {
929   cp_token *token;
930
931   /* N is 1-based, not zero-based.  */
932   gcc_assert (n > 0);
933
934   if (cp_lexer_debugging_p (lexer))
935     fprintf (cp_lexer_debug_stream,
936              "cp_lexer: peeking ahead %ld at token: ", (long)n);
937
938   --n;
939   token = lexer->next_token;
940   gcc_assert (!n || token != &eof_token);
941   while (n != 0)
942     {
943       ++token;
944       if (token == lexer->last_token)
945         {
946           token = &eof_token;
947           break;
948         }
949
950       if (!token->purged_p)
951         --n;
952     }
953
954   if (cp_lexer_debugging_p (lexer))
955     {
956       cp_lexer_print_token (cp_lexer_debug_stream, token);
957       putc ('\n', cp_lexer_debug_stream);
958     }
959
960   return token;
961 }
962
963 /* Return the next token, and advance the lexer's next_token pointer
964    to point to the next non-purged token.  */
965
966 static cp_token *
967 cp_lexer_consume_token (cp_lexer* lexer)
968 {
969   cp_token *token = lexer->next_token;
970
971   gcc_assert (token != &eof_token);
972   gcc_assert (!lexer->in_pragma || token->type != CPP_PRAGMA_EOL);
973
974   do
975     {
976       lexer->next_token++;
977       if (lexer->next_token == lexer->last_token)
978         {
979           lexer->next_token = &eof_token;
980           break;
981         }
982
983     }
984   while (lexer->next_token->purged_p);
985
986   cp_lexer_set_source_position_from_token (token);
987
988   /* Provide debugging output.  */
989   if (cp_lexer_debugging_p (lexer))
990     {
991       fputs ("cp_lexer: consuming token: ", cp_lexer_debug_stream);
992       cp_lexer_print_token (cp_lexer_debug_stream, token);
993       putc ('\n', cp_lexer_debug_stream);
994     }
995
996   return token;
997 }
998
999 /* Permanently remove the next token from the token stream, and
1000    advance the next_token pointer to refer to the next non-purged
1001    token.  */
1002
1003 static void
1004 cp_lexer_purge_token (cp_lexer *lexer)
1005 {
1006   cp_token *tok = lexer->next_token;
1007
1008   gcc_assert (tok != &eof_token);
1009   tok->purged_p = true;
1010   tok->location = UNKNOWN_LOCATION;
1011   tok->u.value = NULL_TREE;
1012   tok->keyword = RID_MAX;
1013
1014   do
1015     {
1016       tok++;
1017       if (tok == lexer->last_token)
1018         {
1019           tok = &eof_token;
1020           break;
1021         }
1022     }
1023   while (tok->purged_p);
1024   lexer->next_token = tok;
1025 }
1026
1027 /* Permanently remove all tokens after TOK, up to, but not
1028    including, the token that will be returned next by
1029    cp_lexer_peek_token.  */
1030
1031 static void
1032 cp_lexer_purge_tokens_after (cp_lexer *lexer, cp_token *tok)
1033 {
1034   cp_token *peek = lexer->next_token;
1035
1036   if (peek == &eof_token)
1037     peek = lexer->last_token;
1038
1039   gcc_assert (tok < peek);
1040
1041   for ( tok += 1; tok != peek; tok += 1)
1042     {
1043       tok->purged_p = true;
1044       tok->location = UNKNOWN_LOCATION;
1045       tok->u.value = NULL_TREE;
1046       tok->keyword = RID_MAX;
1047     }
1048 }
1049
1050 /* Begin saving tokens.  All tokens consumed after this point will be
1051    preserved.  */
1052
1053 static void
1054 cp_lexer_save_tokens (cp_lexer* lexer)
1055 {
1056   /* Provide debugging output.  */
1057   if (cp_lexer_debugging_p (lexer))
1058     fprintf (cp_lexer_debug_stream, "cp_lexer: saving tokens\n");
1059
1060   VEC_safe_push (cp_token_position, heap,
1061                  lexer->saved_tokens, lexer->next_token);
1062 }
1063
1064 /* Commit to the portion of the token stream most recently saved.  */
1065
1066 static void
1067 cp_lexer_commit_tokens (cp_lexer* lexer)
1068 {
1069   /* Provide debugging output.  */
1070   if (cp_lexer_debugging_p (lexer))
1071     fprintf (cp_lexer_debug_stream, "cp_lexer: committing tokens\n");
1072
1073   VEC_pop (cp_token_position, lexer->saved_tokens);
1074 }
1075
1076 /* Return all tokens saved since the last call to cp_lexer_save_tokens
1077    to the token stream.  Stop saving tokens.  */
1078
1079 static void
1080 cp_lexer_rollback_tokens (cp_lexer* lexer)
1081 {
1082   /* Provide debugging output.  */
1083   if (cp_lexer_debugging_p (lexer))
1084     fprintf (cp_lexer_debug_stream, "cp_lexer: restoring tokens\n");
1085
1086   lexer->next_token = VEC_pop (cp_token_position, lexer->saved_tokens);
1087 }
1088
1089 /* Print a representation of the TOKEN on the STREAM.  */
1090
1091 static void
1092 cp_lexer_print_token (FILE * stream, cp_token *token)
1093 {
1094   /* We don't use cpp_type2name here because the parser defines
1095      a few tokens of its own.  */
1096   static const char *const token_names[] = {
1097     /* cpplib-defined token types */
1098 #define OP(e, s) #e,
1099 #define TK(e, s) #e,
1100     TTYPE_TABLE
1101 #undef OP
1102 #undef TK
1103     /* C++ parser token types - see "Manifest constants", above.  */
1104     "KEYWORD",
1105     "TEMPLATE_ID",
1106     "NESTED_NAME_SPECIFIER",
1107   };
1108
1109   /* For some tokens, print the associated data.  */
1110   switch (token->type)
1111     {
1112     case CPP_KEYWORD:
1113       /* Some keywords have a value that is not an IDENTIFIER_NODE.
1114          For example, `struct' is mapped to an INTEGER_CST.  */
1115       if (TREE_CODE (token->u.value) != IDENTIFIER_NODE)
1116         break;
1117       /* else fall through */
1118     case CPP_NAME:
1119       fputs (IDENTIFIER_POINTER (token->u.value), stream);
1120       break;
1121
1122     case CPP_STRING:
1123     case CPP_STRING16:
1124     case CPP_STRING32:
1125     case CPP_WSTRING:
1126     case CPP_UTF8STRING:
1127       fprintf (stream, " \"%s\"", TREE_STRING_POINTER (token->u.value));
1128       break;
1129
1130     case CPP_NUMBER:
1131       print_generic_expr (stream, token->u.value, 0);
1132       break;
1133
1134     default:
1135       /* If we have a name for the token, print it out.  Otherwise, we
1136          simply give the numeric code.  */
1137       if (token->type < ARRAY_SIZE(token_names))
1138         fputs (token_names[token->type], stream);
1139       else
1140         fprintf (stream, "[%d]", token->type);
1141       break;
1142     }
1143 }
1144
1145 /* Start emitting debugging information.  */
1146
1147 static void
1148 cp_lexer_start_debugging (cp_lexer* lexer)
1149 {
1150   lexer->debugging_p = true;
1151   cp_lexer_debug_stream = stderr;
1152 }
1153
1154 /* Stop emitting debugging information.  */
1155
1156 static void
1157 cp_lexer_stop_debugging (cp_lexer* lexer)
1158 {
1159   lexer->debugging_p = false;
1160   cp_lexer_debug_stream = NULL;
1161 }
1162
1163 /* Create a new cp_token_cache, representing a range of tokens.  */
1164
1165 static cp_token_cache *
1166 cp_token_cache_new (cp_token *first, cp_token *last)
1167 {
1168   cp_token_cache *cache = ggc_alloc_cp_token_cache ();
1169   cache->first = first;
1170   cache->last = last;
1171   return cache;
1172 }
1173
1174 \f
1175 /* Decl-specifiers.  */
1176
1177 /* Set *DECL_SPECS to represent an empty decl-specifier-seq.  */
1178
1179 static void
1180 clear_decl_specs (cp_decl_specifier_seq *decl_specs)
1181 {
1182   memset (decl_specs, 0, sizeof (cp_decl_specifier_seq));
1183 }
1184
1185 /* Declarators.  */
1186
1187 /* Nothing other than the parser should be creating declarators;
1188    declarators are a semi-syntactic representation of C++ entities.
1189    Other parts of the front end that need to create entities (like
1190    VAR_DECLs or FUNCTION_DECLs) should do that directly.  */
1191
1192 static cp_declarator *make_call_declarator
1193   (cp_declarator *, tree, cp_cv_quals, cp_virt_specifiers, tree, tree);
1194 static cp_declarator *make_array_declarator
1195   (cp_declarator *, tree);
1196 static cp_declarator *make_pointer_declarator
1197   (cp_cv_quals, cp_declarator *);
1198 static cp_declarator *make_reference_declarator
1199   (cp_cv_quals, cp_declarator *, bool);
1200 static cp_parameter_declarator *make_parameter_declarator
1201   (cp_decl_specifier_seq *, cp_declarator *, tree);
1202 static cp_declarator *make_ptrmem_declarator
1203   (cp_cv_quals, tree, cp_declarator *);
1204
1205 /* An erroneous declarator.  */
1206 static cp_declarator *cp_error_declarator;
1207
1208 /* The obstack on which declarators and related data structures are
1209    allocated.  */
1210 static struct obstack declarator_obstack;
1211
1212 /* Alloc BYTES from the declarator memory pool.  */
1213
1214 static inline void *
1215 alloc_declarator (size_t bytes)
1216 {
1217   return obstack_alloc (&declarator_obstack, bytes);
1218 }
1219
1220 /* Allocate a declarator of the indicated KIND.  Clear fields that are
1221    common to all declarators.  */
1222
1223 static cp_declarator *
1224 make_declarator (cp_declarator_kind kind)
1225 {
1226   cp_declarator *declarator;
1227
1228   declarator = (cp_declarator *) alloc_declarator (sizeof (cp_declarator));
1229   declarator->kind = kind;
1230   declarator->attributes = NULL_TREE;
1231   declarator->declarator = NULL;
1232   declarator->parameter_pack_p = false;
1233   declarator->id_loc = UNKNOWN_LOCATION;
1234
1235   return declarator;
1236 }
1237
1238 /* Make a declarator for a generalized identifier.  If
1239    QUALIFYING_SCOPE is non-NULL, the identifier is
1240    QUALIFYING_SCOPE::UNQUALIFIED_NAME; otherwise, it is just
1241    UNQUALIFIED_NAME.  SFK indicates the kind of special function this
1242    is, if any.   */
1243
1244 static cp_declarator *
1245 make_id_declarator (tree qualifying_scope, tree unqualified_name,
1246                     special_function_kind sfk)
1247 {
1248   cp_declarator *declarator;
1249
1250   /* It is valid to write:
1251
1252        class C { void f(); };
1253        typedef C D;
1254        void D::f();
1255
1256      The standard is not clear about whether `typedef const C D' is
1257      legal; as of 2002-09-15 the committee is considering that
1258      question.  EDG 3.0 allows that syntax.  Therefore, we do as
1259      well.  */
1260   if (qualifying_scope && TYPE_P (qualifying_scope))
1261     qualifying_scope = TYPE_MAIN_VARIANT (qualifying_scope);
1262
1263   gcc_assert (TREE_CODE (unqualified_name) == IDENTIFIER_NODE
1264               || TREE_CODE (unqualified_name) == BIT_NOT_EXPR
1265               || TREE_CODE (unqualified_name) == TEMPLATE_ID_EXPR);
1266
1267   declarator = make_declarator (cdk_id);
1268   declarator->u.id.qualifying_scope = qualifying_scope;
1269   declarator->u.id.unqualified_name = unqualified_name;
1270   declarator->u.id.sfk = sfk;
1271   
1272   return declarator;
1273 }
1274
1275 /* Make a declarator for a pointer to TARGET.  CV_QUALIFIERS is a list
1276    of modifiers such as const or volatile to apply to the pointer
1277    type, represented as identifiers.  */
1278
1279 cp_declarator *
1280 make_pointer_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target)
1281 {
1282   cp_declarator *declarator;
1283
1284   declarator = make_declarator (cdk_pointer);
1285   declarator->declarator = target;
1286   declarator->u.pointer.qualifiers = cv_qualifiers;
1287   declarator->u.pointer.class_type = NULL_TREE;
1288   if (target)
1289     {
1290       declarator->id_loc = target->id_loc;
1291       declarator->parameter_pack_p = target->parameter_pack_p;
1292       target->parameter_pack_p = false;
1293     }
1294   else
1295     declarator->parameter_pack_p = false;
1296
1297   return declarator;
1298 }
1299
1300 /* Like make_pointer_declarator -- but for references.  */
1301
1302 cp_declarator *
1303 make_reference_declarator (cp_cv_quals cv_qualifiers, cp_declarator *target,
1304                            bool rvalue_ref)
1305 {
1306   cp_declarator *declarator;
1307
1308   declarator = make_declarator (cdk_reference);
1309   declarator->declarator = target;
1310   declarator->u.reference.qualifiers = cv_qualifiers;
1311   declarator->u.reference.rvalue_ref = rvalue_ref;
1312   if (target)
1313     {
1314       declarator->id_loc = target->id_loc;
1315       declarator->parameter_pack_p = target->parameter_pack_p;
1316       target->parameter_pack_p = false;
1317     }
1318   else
1319     declarator->parameter_pack_p = false;
1320
1321   return declarator;
1322 }
1323
1324 /* Like make_pointer_declarator -- but for a pointer to a non-static
1325    member of CLASS_TYPE.  */
1326
1327 cp_declarator *
1328 make_ptrmem_declarator (cp_cv_quals cv_qualifiers, tree class_type,
1329                         cp_declarator *pointee)
1330 {
1331   cp_declarator *declarator;
1332
1333   declarator = make_declarator (cdk_ptrmem);
1334   declarator->declarator = pointee;
1335   declarator->u.pointer.qualifiers = cv_qualifiers;
1336   declarator->u.pointer.class_type = class_type;
1337
1338   if (pointee)
1339     {
1340       declarator->parameter_pack_p = pointee->parameter_pack_p;
1341       pointee->parameter_pack_p = false;
1342     }
1343   else
1344     declarator->parameter_pack_p = false;
1345
1346   return declarator;
1347 }
1348
1349 /* Make a declarator for the function given by TARGET, with the
1350    indicated PARMS.  The CV_QUALIFIERS aply to the function, as in
1351    "const"-qualified member function.  The EXCEPTION_SPECIFICATION
1352    indicates what exceptions can be thrown.  */
1353
1354 cp_declarator *
1355 make_call_declarator (cp_declarator *target,
1356                       tree parms,
1357                       cp_cv_quals cv_qualifiers,
1358                       cp_virt_specifiers virt_specifiers,
1359                       tree exception_specification,
1360                       tree late_return_type)
1361 {
1362   cp_declarator *declarator;
1363
1364   declarator = make_declarator (cdk_function);
1365   declarator->declarator = target;
1366   declarator->u.function.parameters = parms;
1367   declarator->u.function.qualifiers = cv_qualifiers;
1368   declarator->u.function.virt_specifiers = virt_specifiers;
1369   declarator->u.function.exception_specification = exception_specification;
1370   declarator->u.function.late_return_type = late_return_type;
1371   if (target)
1372     {
1373       declarator->id_loc = target->id_loc;
1374       declarator->parameter_pack_p = target->parameter_pack_p;
1375       target->parameter_pack_p = false;
1376     }
1377   else
1378     declarator->parameter_pack_p = false;
1379
1380   return declarator;
1381 }
1382
1383 /* Make a declarator for an array of BOUNDS elements, each of which is
1384    defined by ELEMENT.  */
1385
1386 cp_declarator *
1387 make_array_declarator (cp_declarator *element, tree bounds)
1388 {
1389   cp_declarator *declarator;
1390
1391   declarator = make_declarator (cdk_array);
1392   declarator->declarator = element;
1393   declarator->u.array.bounds = bounds;
1394   if (element)
1395     {
1396       declarator->id_loc = element->id_loc;
1397       declarator->parameter_pack_p = element->parameter_pack_p;
1398       element->parameter_pack_p = false;
1399     }
1400   else
1401     declarator->parameter_pack_p = false;
1402
1403   return declarator;
1404 }
1405
1406 /* Determine whether the declarator we've seen so far can be a
1407    parameter pack, when followed by an ellipsis.  */
1408 static bool 
1409 declarator_can_be_parameter_pack (cp_declarator *declarator)
1410 {
1411   /* Search for a declarator name, or any other declarator that goes
1412      after the point where the ellipsis could appear in a parameter
1413      pack. If we find any of these, then this declarator can not be
1414      made into a parameter pack.  */
1415   bool found = false;
1416   while (declarator && !found)
1417     {
1418       switch ((int)declarator->kind)
1419         {
1420         case cdk_id:
1421         case cdk_array:
1422           found = true;
1423           break;
1424
1425         case cdk_error:
1426           return true;
1427
1428         default:
1429           declarator = declarator->declarator;
1430           break;
1431         }
1432     }
1433
1434   return !found;
1435 }
1436
1437 cp_parameter_declarator *no_parameters;
1438
1439 /* Create a parameter declarator with the indicated DECL_SPECIFIERS,
1440    DECLARATOR and DEFAULT_ARGUMENT.  */
1441
1442 cp_parameter_declarator *
1443 make_parameter_declarator (cp_decl_specifier_seq *decl_specifiers,
1444                            cp_declarator *declarator,
1445                            tree default_argument)
1446 {
1447   cp_parameter_declarator *parameter;
1448
1449   parameter = ((cp_parameter_declarator *)
1450                alloc_declarator (sizeof (cp_parameter_declarator)));
1451   parameter->next = NULL;
1452   if (decl_specifiers)
1453     parameter->decl_specifiers = *decl_specifiers;
1454   else
1455     clear_decl_specs (&parameter->decl_specifiers);
1456   parameter->declarator = declarator;
1457   parameter->default_argument = default_argument;
1458   parameter->ellipsis_p = false;
1459
1460   return parameter;
1461 }
1462
1463 /* Returns true iff DECLARATOR  is a declaration for a function.  */
1464
1465 static bool
1466 function_declarator_p (const cp_declarator *declarator)
1467 {
1468   while (declarator)
1469     {
1470       if (declarator->kind == cdk_function
1471           && declarator->declarator->kind == cdk_id)
1472         return true;
1473       if (declarator->kind == cdk_id
1474           || declarator->kind == cdk_error)
1475         return false;
1476       declarator = declarator->declarator;
1477     }
1478   return false;
1479 }
1480  
1481 /* The parser.  */
1482
1483 /* Overview
1484    --------
1485
1486    A cp_parser parses the token stream as specified by the C++
1487    grammar.  Its job is purely parsing, not semantic analysis.  For
1488    example, the parser breaks the token stream into declarators,
1489    expressions, statements, and other similar syntactic constructs.
1490    It does not check that the types of the expressions on either side
1491    of an assignment-statement are compatible, or that a function is
1492    not declared with a parameter of type `void'.
1493
1494    The parser invokes routines elsewhere in the compiler to perform
1495    semantic analysis and to build up the abstract syntax tree for the
1496    code processed.
1497
1498    The parser (and the template instantiation code, which is, in a
1499    way, a close relative of parsing) are the only parts of the
1500    compiler that should be calling push_scope and pop_scope, or
1501    related functions.  The parser (and template instantiation code)
1502    keeps track of what scope is presently active; everything else
1503    should simply honor that.  (The code that generates static
1504    initializers may also need to set the scope, in order to check
1505    access control correctly when emitting the initializers.)
1506
1507    Methodology
1508    -----------
1509
1510    The parser is of the standard recursive-descent variety.  Upcoming
1511    tokens in the token stream are examined in order to determine which
1512    production to use when parsing a non-terminal.  Some C++ constructs
1513    require arbitrary look ahead to disambiguate.  For example, it is
1514    impossible, in the general case, to tell whether a statement is an
1515    expression or declaration without scanning the entire statement.
1516    Therefore, the parser is capable of "parsing tentatively."  When the
1517    parser is not sure what construct comes next, it enters this mode.
1518    Then, while we attempt to parse the construct, the parser queues up
1519    error messages, rather than issuing them immediately, and saves the
1520    tokens it consumes.  If the construct is parsed successfully, the
1521    parser "commits", i.e., it issues any queued error messages and
1522    the tokens that were being preserved are permanently discarded.
1523    If, however, the construct is not parsed successfully, the parser
1524    rolls back its state completely so that it can resume parsing using
1525    a different alternative.
1526
1527    Future Improvements
1528    -------------------
1529
1530    The performance of the parser could probably be improved substantially.
1531    We could often eliminate the need to parse tentatively by looking ahead
1532    a little bit.  In some places, this approach might not entirely eliminate
1533    the need to parse tentatively, but it might still speed up the average
1534    case.  */
1535
1536 /* Flags that are passed to some parsing functions.  These values can
1537    be bitwise-ored together.  */
1538
1539 enum
1540 {
1541   /* No flags.  */
1542   CP_PARSER_FLAGS_NONE = 0x0,
1543   /* The construct is optional.  If it is not present, then no error
1544      should be issued.  */
1545   CP_PARSER_FLAGS_OPTIONAL = 0x1,
1546   /* When parsing a type-specifier, treat user-defined type-names
1547      as non-type identifiers.  */
1548   CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES = 0x2,
1549   /* When parsing a type-specifier, do not try to parse a class-specifier
1550      or enum-specifier.  */
1551   CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS = 0x4,
1552   /* When parsing a decl-specifier-seq, only allow type-specifier or
1553      constexpr.  */
1554   CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR = 0x8
1555 };
1556
1557 /* This type is used for parameters and variables which hold
1558    combinations of the above flags.  */
1559 typedef int cp_parser_flags;
1560
1561 /* The different kinds of declarators we want to parse.  */
1562
1563 typedef enum cp_parser_declarator_kind
1564 {
1565   /* We want an abstract declarator.  */
1566   CP_PARSER_DECLARATOR_ABSTRACT,
1567   /* We want a named declarator.  */
1568   CP_PARSER_DECLARATOR_NAMED,
1569   /* We don't mind, but the name must be an unqualified-id.  */
1570   CP_PARSER_DECLARATOR_EITHER
1571 } cp_parser_declarator_kind;
1572
1573 /* The precedence values used to parse binary expressions.  The minimum value
1574    of PREC must be 1, because zero is reserved to quickly discriminate
1575    binary operators from other tokens.  */
1576
1577 enum cp_parser_prec
1578 {
1579   PREC_NOT_OPERATOR,
1580   PREC_LOGICAL_OR_EXPRESSION,
1581   PREC_LOGICAL_AND_EXPRESSION,
1582   PREC_INCLUSIVE_OR_EXPRESSION,
1583   PREC_EXCLUSIVE_OR_EXPRESSION,
1584   PREC_AND_EXPRESSION,
1585   PREC_EQUALITY_EXPRESSION,
1586   PREC_RELATIONAL_EXPRESSION,
1587   PREC_SHIFT_EXPRESSION,
1588   PREC_ADDITIVE_EXPRESSION,
1589   PREC_MULTIPLICATIVE_EXPRESSION,
1590   PREC_PM_EXPRESSION,
1591   NUM_PREC_VALUES = PREC_PM_EXPRESSION
1592 };
1593
1594 /* A mapping from a token type to a corresponding tree node type, with a
1595    precedence value.  */
1596
1597 typedef struct cp_parser_binary_operations_map_node
1598 {
1599   /* The token type.  */
1600   enum cpp_ttype token_type;
1601   /* The corresponding tree code.  */
1602   enum tree_code tree_type;
1603   /* The precedence of this operator.  */
1604   enum cp_parser_prec prec;
1605 } cp_parser_binary_operations_map_node;
1606
1607 typedef struct cp_parser_expression_stack_entry
1608 {
1609   /* Left hand side of the binary operation we are currently
1610      parsing.  */
1611   tree lhs;
1612   /* Original tree code for left hand side, if it was a binary
1613      expression itself (used for -Wparentheses).  */
1614   enum tree_code lhs_type;
1615   /* Tree code for the binary operation we are parsing.  */
1616   enum tree_code tree_type;
1617   /* Precedence of the binary operation we are parsing.  */
1618   enum cp_parser_prec prec;
1619 } cp_parser_expression_stack_entry;
1620
1621 /* The stack for storing partial expressions.  We only need NUM_PREC_VALUES
1622    entries because precedence levels on the stack are monotonically
1623    increasing.  */
1624 typedef struct cp_parser_expression_stack_entry
1625   cp_parser_expression_stack[NUM_PREC_VALUES];
1626
1627 /* Prototypes.  */
1628
1629 /* Constructors and destructors.  */
1630
1631 static cp_parser_context *cp_parser_context_new
1632   (cp_parser_context *);
1633
1634 /* Class variables.  */
1635
1636 static GTY((deletable)) cp_parser_context* cp_parser_context_free_list;
1637
1638 /* The operator-precedence table used by cp_parser_binary_expression.
1639    Transformed into an associative array (binops_by_token) by
1640    cp_parser_new.  */
1641
1642 static const cp_parser_binary_operations_map_node binops[] = {
1643   { CPP_DEREF_STAR, MEMBER_REF, PREC_PM_EXPRESSION },
1644   { CPP_DOT_STAR, DOTSTAR_EXPR, PREC_PM_EXPRESSION },
1645
1646   { CPP_MULT, MULT_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1647   { CPP_DIV, TRUNC_DIV_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1648   { CPP_MOD, TRUNC_MOD_EXPR, PREC_MULTIPLICATIVE_EXPRESSION },
1649
1650   { CPP_PLUS, PLUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1651   { CPP_MINUS, MINUS_EXPR, PREC_ADDITIVE_EXPRESSION },
1652
1653   { CPP_LSHIFT, LSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1654   { CPP_RSHIFT, RSHIFT_EXPR, PREC_SHIFT_EXPRESSION },
1655
1656   { CPP_LESS, LT_EXPR, PREC_RELATIONAL_EXPRESSION },
1657   { CPP_GREATER, GT_EXPR, PREC_RELATIONAL_EXPRESSION },
1658   { CPP_LESS_EQ, LE_EXPR, PREC_RELATIONAL_EXPRESSION },
1659   { CPP_GREATER_EQ, GE_EXPR, PREC_RELATIONAL_EXPRESSION },
1660
1661   { CPP_EQ_EQ, EQ_EXPR, PREC_EQUALITY_EXPRESSION },
1662   { CPP_NOT_EQ, NE_EXPR, PREC_EQUALITY_EXPRESSION },
1663
1664   { CPP_AND, BIT_AND_EXPR, PREC_AND_EXPRESSION },
1665
1666   { CPP_XOR, BIT_XOR_EXPR, PREC_EXCLUSIVE_OR_EXPRESSION },
1667
1668   { CPP_OR, BIT_IOR_EXPR, PREC_INCLUSIVE_OR_EXPRESSION },
1669
1670   { CPP_AND_AND, TRUTH_ANDIF_EXPR, PREC_LOGICAL_AND_EXPRESSION },
1671
1672   { CPP_OR_OR, TRUTH_ORIF_EXPR, PREC_LOGICAL_OR_EXPRESSION }
1673 };
1674
1675 /* The same as binops, but initialized by cp_parser_new so that
1676    binops_by_token[N].token_type == N.  Used in cp_parser_binary_expression
1677    for speed.  */
1678 static cp_parser_binary_operations_map_node binops_by_token[N_CP_TTYPES];
1679
1680 /* Constructors and destructors.  */
1681
1682 /* Construct a new context.  The context below this one on the stack
1683    is given by NEXT.  */
1684
1685 static cp_parser_context *
1686 cp_parser_context_new (cp_parser_context* next)
1687 {
1688   cp_parser_context *context;
1689
1690   /* Allocate the storage.  */
1691   if (cp_parser_context_free_list != NULL)
1692     {
1693       /* Pull the first entry from the free list.  */
1694       context = cp_parser_context_free_list;
1695       cp_parser_context_free_list = context->next;
1696       memset (context, 0, sizeof (*context));
1697     }
1698   else
1699     context = ggc_alloc_cleared_cp_parser_context ();
1700
1701   /* No errors have occurred yet in this context.  */
1702   context->status = CP_PARSER_STATUS_KIND_NO_ERROR;
1703   /* If this is not the bottommost context, copy information that we
1704      need from the previous context.  */
1705   if (next)
1706     {
1707       /* If, in the NEXT context, we are parsing an `x->' or `x.'
1708          expression, then we are parsing one in this context, too.  */
1709       context->object_type = next->object_type;
1710       /* Thread the stack.  */
1711       context->next = next;
1712     }
1713
1714   return context;
1715 }
1716
1717 /* Managing the unparsed function queues.  */
1718
1719 #define unparsed_funs_with_default_args \
1720   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_default_args
1721 #define unparsed_funs_with_definitions \
1722   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->funs_with_definitions
1723 #define unparsed_nsdmis \
1724   VEC_last (cp_unparsed_functions_entry, parser->unparsed_queues)->nsdmis
1725
1726 static void
1727 push_unparsed_function_queues (cp_parser *parser)
1728 {
1729   VEC_safe_push (cp_unparsed_functions_entry, gc,
1730                  parser->unparsed_queues, NULL);
1731   unparsed_funs_with_default_args = NULL;
1732   unparsed_funs_with_definitions = make_tree_vector ();
1733   unparsed_nsdmis = NULL;
1734 }
1735
1736 static void
1737 pop_unparsed_function_queues (cp_parser *parser)
1738 {
1739   release_tree_vector (unparsed_funs_with_definitions);
1740   VEC_pop (cp_unparsed_functions_entry, parser->unparsed_queues);
1741 }
1742
1743 /* Prototypes.  */
1744
1745 /* Constructors and destructors.  */
1746
1747 static cp_parser *cp_parser_new
1748   (void);
1749
1750 /* Routines to parse various constructs.
1751
1752    Those that return `tree' will return the error_mark_node (rather
1753    than NULL_TREE) if a parse error occurs, unless otherwise noted.
1754    Sometimes, they will return an ordinary node if error-recovery was
1755    attempted, even though a parse error occurred.  So, to check
1756    whether or not a parse error occurred, you should always use
1757    cp_parser_error_occurred.  If the construct is optional (indicated
1758    either by an `_opt' in the name of the function that does the
1759    parsing or via a FLAGS parameter), then NULL_TREE is returned if
1760    the construct is not present.  */
1761
1762 /* Lexical conventions [gram.lex]  */
1763
1764 static tree cp_parser_identifier
1765   (cp_parser *);
1766 static tree cp_parser_string_literal
1767   (cp_parser *, bool, bool);
1768 static tree cp_parser_userdef_char_literal
1769   (cp_parser *);
1770 static tree cp_parser_userdef_string_literal
1771   (cp_token *);
1772 static tree cp_parser_userdef_numeric_literal
1773   (cp_parser *);
1774
1775 /* Basic concepts [gram.basic]  */
1776
1777 static bool cp_parser_translation_unit
1778   (cp_parser *);
1779
1780 /* Expressions [gram.expr]  */
1781
1782 static tree cp_parser_primary_expression
1783   (cp_parser *, bool, bool, bool, cp_id_kind *);
1784 static tree cp_parser_id_expression
1785   (cp_parser *, bool, bool, bool *, bool, bool);
1786 static tree cp_parser_unqualified_id
1787   (cp_parser *, bool, bool, bool, bool);
1788 static tree cp_parser_nested_name_specifier_opt
1789   (cp_parser *, bool, bool, bool, bool);
1790 static tree cp_parser_nested_name_specifier
1791   (cp_parser *, bool, bool, bool, bool);
1792 static tree cp_parser_qualifying_entity
1793   (cp_parser *, bool, bool, bool, bool, bool);
1794 static tree cp_parser_postfix_expression
1795   (cp_parser *, bool, bool, bool, cp_id_kind *);
1796 static tree cp_parser_postfix_open_square_expression
1797   (cp_parser *, tree, bool);
1798 static tree cp_parser_postfix_dot_deref_expression
1799   (cp_parser *, enum cpp_ttype, tree, bool, cp_id_kind *, location_t);
1800 static VEC(tree,gc) *cp_parser_parenthesized_expression_list
1801   (cp_parser *, int, bool, bool, bool *);
1802 /* Values for the second parameter of cp_parser_parenthesized_expression_list.  */
1803 enum { non_attr = 0, normal_attr = 1, id_attr = 2 };
1804 static void cp_parser_pseudo_destructor_name
1805   (cp_parser *, tree *, tree *);
1806 static tree cp_parser_unary_expression
1807   (cp_parser *, bool, bool, cp_id_kind *);
1808 static enum tree_code cp_parser_unary_operator
1809   (cp_token *);
1810 static tree cp_parser_new_expression
1811   (cp_parser *);
1812 static VEC(tree,gc) *cp_parser_new_placement
1813   (cp_parser *);
1814 static tree cp_parser_new_type_id
1815   (cp_parser *, tree *);
1816 static cp_declarator *cp_parser_new_declarator_opt
1817   (cp_parser *);
1818 static cp_declarator *cp_parser_direct_new_declarator
1819   (cp_parser *);
1820 static VEC(tree,gc) *cp_parser_new_initializer
1821   (cp_parser *);
1822 static tree cp_parser_delete_expression
1823   (cp_parser *);
1824 static tree cp_parser_cast_expression
1825   (cp_parser *, bool, bool, cp_id_kind *);
1826 static tree cp_parser_binary_expression
1827   (cp_parser *, bool, bool, enum cp_parser_prec, cp_id_kind *);
1828 static tree cp_parser_question_colon_clause
1829   (cp_parser *, tree);
1830 static tree cp_parser_assignment_expression
1831   (cp_parser *, bool, cp_id_kind *);
1832 static enum tree_code cp_parser_assignment_operator_opt
1833   (cp_parser *);
1834 static tree cp_parser_expression
1835   (cp_parser *, bool, cp_id_kind *);
1836 static tree cp_parser_constant_expression
1837   (cp_parser *, bool, bool *);
1838 static tree cp_parser_builtin_offsetof
1839   (cp_parser *);
1840 static tree cp_parser_lambda_expression
1841   (cp_parser *);
1842 static void cp_parser_lambda_introducer
1843   (cp_parser *, tree);
1844 static bool cp_parser_lambda_declarator_opt
1845   (cp_parser *, tree);
1846 static void cp_parser_lambda_body
1847   (cp_parser *, tree);
1848
1849 /* Statements [gram.stmt.stmt]  */
1850
1851 static void cp_parser_statement
1852   (cp_parser *, tree, bool, bool *);
1853 static void cp_parser_label_for_labeled_statement
1854   (cp_parser *);
1855 static tree cp_parser_expression_statement
1856   (cp_parser *, tree);
1857 static tree cp_parser_compound_statement
1858   (cp_parser *, tree, bool, bool);
1859 static void cp_parser_statement_seq_opt
1860   (cp_parser *, tree);
1861 static tree cp_parser_selection_statement
1862   (cp_parser *, bool *);
1863 static tree cp_parser_condition
1864   (cp_parser *);
1865 static tree cp_parser_iteration_statement
1866   (cp_parser *);
1867 static bool cp_parser_for_init_statement
1868   (cp_parser *, tree *decl);
1869 static tree cp_parser_for
1870   (cp_parser *);
1871 static tree cp_parser_c_for
1872   (cp_parser *, tree, tree);
1873 static tree cp_parser_range_for
1874   (cp_parser *, tree, tree, tree);
1875 static void do_range_for_auto_deduction
1876   (tree, tree);
1877 static tree cp_parser_perform_range_for_lookup
1878   (tree, tree *, tree *);
1879 static tree cp_parser_range_for_member_function
1880   (tree, tree);
1881 static tree cp_parser_jump_statement
1882   (cp_parser *);
1883 static void cp_parser_declaration_statement
1884   (cp_parser *);
1885
1886 static tree cp_parser_implicitly_scoped_statement
1887   (cp_parser *, bool *);
1888 static void cp_parser_already_scoped_statement
1889   (cp_parser *);
1890
1891 /* Declarations [gram.dcl.dcl] */
1892
1893 static void cp_parser_declaration_seq_opt
1894   (cp_parser *);
1895 static void cp_parser_declaration
1896   (cp_parser *);
1897 static void cp_parser_block_declaration
1898   (cp_parser *, bool);
1899 static void cp_parser_simple_declaration
1900   (cp_parser *, bool, tree *);
1901 static void cp_parser_decl_specifier_seq
1902   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, int *);
1903 static tree cp_parser_storage_class_specifier_opt
1904   (cp_parser *);
1905 static tree cp_parser_function_specifier_opt
1906   (cp_parser *, cp_decl_specifier_seq *);
1907 static tree cp_parser_type_specifier
1908   (cp_parser *, cp_parser_flags, cp_decl_specifier_seq *, bool,
1909    int *, bool *);
1910 static tree cp_parser_simple_type_specifier
1911   (cp_parser *, cp_decl_specifier_seq *, cp_parser_flags);
1912 static tree cp_parser_type_name
1913   (cp_parser *);
1914 static tree cp_parser_nonclass_name 
1915   (cp_parser* parser);
1916 static tree cp_parser_elaborated_type_specifier
1917   (cp_parser *, bool, bool);
1918 static tree cp_parser_enum_specifier
1919   (cp_parser *);
1920 static void cp_parser_enumerator_list
1921   (cp_parser *, tree);
1922 static void cp_parser_enumerator_definition
1923   (cp_parser *, tree);
1924 static tree cp_parser_namespace_name
1925   (cp_parser *);
1926 static void cp_parser_namespace_definition
1927   (cp_parser *);
1928 static void cp_parser_namespace_body
1929   (cp_parser *);
1930 static tree cp_parser_qualified_namespace_specifier
1931   (cp_parser *);
1932 static void cp_parser_namespace_alias_definition
1933   (cp_parser *);
1934 static bool cp_parser_using_declaration
1935   (cp_parser *, bool);
1936 static void cp_parser_using_directive
1937   (cp_parser *);
1938 static void cp_parser_asm_definition
1939   (cp_parser *);
1940 static void cp_parser_linkage_specification
1941   (cp_parser *);
1942 static void cp_parser_static_assert
1943   (cp_parser *, bool);
1944 static tree cp_parser_decltype
1945   (cp_parser *);
1946
1947 /* Declarators [gram.dcl.decl] */
1948
1949 static tree cp_parser_init_declarator
1950   (cp_parser *, cp_decl_specifier_seq *, VEC (deferred_access_check,gc)*, bool, bool, int, bool *, tree *);
1951 static cp_declarator *cp_parser_declarator
1952   (cp_parser *, cp_parser_declarator_kind, int *, bool *, bool);
1953 static cp_declarator *cp_parser_direct_declarator
1954   (cp_parser *, cp_parser_declarator_kind, int *, bool);
1955 static enum tree_code cp_parser_ptr_operator
1956   (cp_parser *, tree *, cp_cv_quals *);
1957 static cp_cv_quals cp_parser_cv_qualifier_seq_opt
1958   (cp_parser *);
1959 static cp_virt_specifiers cp_parser_virt_specifier_seq_opt
1960   (cp_parser *);
1961 static tree cp_parser_late_return_type_opt
1962   (cp_parser *, cp_cv_quals);
1963 static tree cp_parser_declarator_id
1964   (cp_parser *, bool);
1965 static tree cp_parser_type_id
1966   (cp_parser *);
1967 static tree cp_parser_template_type_arg
1968   (cp_parser *);
1969 static tree cp_parser_trailing_type_id (cp_parser *);
1970 static tree cp_parser_type_id_1
1971   (cp_parser *, bool, bool);
1972 static void cp_parser_type_specifier_seq
1973   (cp_parser *, bool, bool, cp_decl_specifier_seq *);
1974 static tree cp_parser_parameter_declaration_clause
1975   (cp_parser *);
1976 static tree cp_parser_parameter_declaration_list
1977   (cp_parser *, bool *);
1978 static cp_parameter_declarator *cp_parser_parameter_declaration
1979   (cp_parser *, bool, bool *);
1980 static tree cp_parser_default_argument 
1981   (cp_parser *, bool);
1982 static void cp_parser_function_body
1983   (cp_parser *);
1984 static tree cp_parser_initializer
1985   (cp_parser *, bool *, bool *);
1986 static tree cp_parser_initializer_clause
1987   (cp_parser *, bool *);
1988 static tree cp_parser_braced_list
1989   (cp_parser*, bool*);
1990 static VEC(constructor_elt,gc) *cp_parser_initializer_list
1991   (cp_parser *, bool *);
1992
1993 static bool cp_parser_ctor_initializer_opt_and_function_body
1994   (cp_parser *);
1995
1996 /* Classes [gram.class] */
1997
1998 static tree cp_parser_class_name
1999   (cp_parser *, bool, bool, enum tag_types, bool, bool, bool);
2000 static tree cp_parser_class_specifier
2001   (cp_parser *);
2002 static tree cp_parser_class_head
2003   (cp_parser *, bool *, tree *, tree *);
2004 static enum tag_types cp_parser_class_key
2005   (cp_parser *);
2006 static void cp_parser_member_specification_opt
2007   (cp_parser *);
2008 static void cp_parser_member_declaration
2009   (cp_parser *);
2010 static tree cp_parser_pure_specifier
2011   (cp_parser *);
2012 static tree cp_parser_constant_initializer
2013   (cp_parser *);
2014
2015 /* Derived classes [gram.class.derived] */
2016
2017 static tree cp_parser_base_clause
2018   (cp_parser *);
2019 static tree cp_parser_base_specifier
2020   (cp_parser *);
2021
2022 /* Special member functions [gram.special] */
2023
2024 static tree cp_parser_conversion_function_id
2025   (cp_parser *);
2026 static tree cp_parser_conversion_type_id
2027   (cp_parser *);
2028 static cp_declarator *cp_parser_conversion_declarator_opt
2029   (cp_parser *);
2030 static bool cp_parser_ctor_initializer_opt
2031   (cp_parser *);
2032 static void cp_parser_mem_initializer_list
2033   (cp_parser *);
2034 static tree cp_parser_mem_initializer
2035   (cp_parser *);
2036 static tree cp_parser_mem_initializer_id
2037   (cp_parser *);
2038
2039 /* Overloading [gram.over] */
2040
2041 static tree cp_parser_operator_function_id
2042   (cp_parser *);
2043 static tree cp_parser_operator
2044   (cp_parser *);
2045
2046 /* Templates [gram.temp] */
2047
2048 static void cp_parser_template_declaration
2049   (cp_parser *, bool);
2050 static tree cp_parser_template_parameter_list
2051   (cp_parser *);
2052 static tree cp_parser_template_parameter
2053   (cp_parser *, bool *, bool *);
2054 static tree cp_parser_type_parameter
2055   (cp_parser *, bool *);
2056 static tree cp_parser_template_id
2057   (cp_parser *, bool, bool, bool);
2058 static tree cp_parser_template_name
2059   (cp_parser *, bool, bool, bool, bool *);
2060 static tree cp_parser_template_argument_list
2061   (cp_parser *);
2062 static tree cp_parser_template_argument
2063   (cp_parser *);
2064 static void cp_parser_explicit_instantiation
2065   (cp_parser *);
2066 static void cp_parser_explicit_specialization
2067   (cp_parser *);
2068
2069 /* Exception handling [gram.exception] */
2070
2071 static tree cp_parser_try_block
2072   (cp_parser *);
2073 static bool cp_parser_function_try_block
2074   (cp_parser *);
2075 static void cp_parser_handler_seq
2076   (cp_parser *);
2077 static void cp_parser_handler
2078   (cp_parser *);
2079 static tree cp_parser_exception_declaration
2080   (cp_parser *);
2081 static tree cp_parser_throw_expression
2082   (cp_parser *);
2083 static tree cp_parser_exception_specification_opt
2084   (cp_parser *);
2085 static tree cp_parser_type_id_list
2086   (cp_parser *);
2087
2088 /* GNU Extensions */
2089
2090 static tree cp_parser_asm_specification_opt
2091   (cp_parser *);
2092 static tree cp_parser_asm_operand_list
2093   (cp_parser *);
2094 static tree cp_parser_asm_clobber_list
2095   (cp_parser *);
2096 static tree cp_parser_asm_label_list
2097   (cp_parser *);
2098 static tree cp_parser_attributes_opt
2099   (cp_parser *);
2100 static tree cp_parser_attribute_list
2101   (cp_parser *);
2102 static bool cp_parser_extension_opt
2103   (cp_parser *, int *);
2104 static void cp_parser_label_declaration
2105   (cp_parser *);
2106
2107 enum pragma_context { pragma_external, pragma_stmt, pragma_compound };
2108 static bool cp_parser_pragma
2109   (cp_parser *, enum pragma_context);
2110
2111 /* Objective-C++ Productions */
2112
2113 static tree cp_parser_objc_message_receiver
2114   (cp_parser *);
2115 static tree cp_parser_objc_message_args
2116   (cp_parser *);
2117 static tree cp_parser_objc_message_expression
2118   (cp_parser *);
2119 static tree cp_parser_objc_encode_expression
2120   (cp_parser *);
2121 static tree cp_parser_objc_defs_expression
2122   (cp_parser *);
2123 static tree cp_parser_objc_protocol_expression
2124   (cp_parser *);
2125 static tree cp_parser_objc_selector_expression
2126   (cp_parser *);
2127 static tree cp_parser_objc_expression
2128   (cp_parser *);
2129 static bool cp_parser_objc_selector_p
2130   (enum cpp_ttype);
2131 static tree cp_parser_objc_selector
2132   (cp_parser *);
2133 static tree cp_parser_objc_protocol_refs_opt
2134   (cp_parser *);
2135 static void cp_parser_objc_declaration
2136   (cp_parser *, tree);
2137 static tree cp_parser_objc_statement
2138   (cp_parser *);
2139 static bool cp_parser_objc_valid_prefix_attributes
2140   (cp_parser *, tree *);
2141 static void cp_parser_objc_at_property_declaration 
2142   (cp_parser *) ;
2143 static void cp_parser_objc_at_synthesize_declaration 
2144   (cp_parser *) ;
2145 static void cp_parser_objc_at_dynamic_declaration
2146   (cp_parser *) ;
2147 static tree cp_parser_objc_struct_declaration
2148   (cp_parser *) ;
2149
2150 /* Utility Routines */
2151
2152 static tree cp_parser_lookup_name
2153   (cp_parser *, tree, enum tag_types, bool, bool, bool, tree *, location_t);
2154 static tree cp_parser_lookup_name_simple
2155   (cp_parser *, tree, location_t);
2156 static tree cp_parser_maybe_treat_template_as_class
2157   (tree, bool);
2158 static bool cp_parser_check_declarator_template_parameters
2159   (cp_parser *, cp_declarator *, location_t);
2160 static bool cp_parser_check_template_parameters
2161   (cp_parser *, unsigned, location_t, cp_declarator *);
2162 static tree cp_parser_simple_cast_expression
2163   (cp_parser *);
2164 static tree cp_parser_global_scope_opt
2165   (cp_parser *, bool);
2166 static bool cp_parser_constructor_declarator_p
2167   (cp_parser *, bool);
2168 static tree cp_parser_function_definition_from_specifiers_and_declarator
2169   (cp_parser *, cp_decl_specifier_seq *, tree, const cp_declarator *);
2170 static tree cp_parser_function_definition_after_declarator
2171   (cp_parser *, bool);
2172 static void cp_parser_template_declaration_after_export
2173   (cp_parser *, bool);
2174 static void cp_parser_perform_template_parameter_access_checks
2175   (VEC (deferred_access_check,gc)*);
2176 static tree cp_parser_single_declaration
2177   (cp_parser *, VEC (deferred_access_check,gc)*, bool, bool, bool *);
2178 static tree cp_parser_functional_cast
2179   (cp_parser *, tree);
2180 static tree cp_parser_save_member_function_body
2181   (cp_parser *, cp_decl_specifier_seq *, cp_declarator *, tree);
2182 static tree cp_parser_save_nsdmi
2183   (cp_parser *);
2184 static tree cp_parser_enclosed_template_argument_list
2185   (cp_parser *);
2186 static void cp_parser_save_default_args
2187   (cp_parser *, tree);
2188 static void cp_parser_late_parsing_for_member
2189   (cp_parser *, tree);
2190 static tree cp_parser_late_parse_one_default_arg
2191   (cp_parser *, tree, tree, tree);
2192 static void cp_parser_late_parsing_nsdmi
2193   (cp_parser *, tree);
2194 static void cp_parser_late_parsing_default_args
2195   (cp_parser *, tree);
2196 static tree cp_parser_sizeof_operand
2197   (cp_parser *, enum rid);
2198 static tree cp_parser_trait_expr
2199   (cp_parser *, enum rid);
2200 static bool cp_parser_declares_only_class_p
2201   (cp_parser *);
2202 static void cp_parser_set_storage_class
2203   (cp_parser *, cp_decl_specifier_seq *, enum rid, location_t);
2204 static void cp_parser_set_decl_spec_type
2205   (cp_decl_specifier_seq *, tree, location_t, bool);
2206 static bool cp_parser_friend_p
2207   (const cp_decl_specifier_seq *);
2208 static void cp_parser_required_error
2209   (cp_parser *, required_token, bool);
2210 static cp_token *cp_parser_require
2211   (cp_parser *, enum cpp_ttype, required_token);
2212 static cp_token *cp_parser_require_keyword
2213   (cp_parser *, enum rid, required_token);
2214 static bool cp_parser_token_starts_function_definition_p
2215   (cp_token *);
2216 static bool cp_parser_next_token_starts_class_definition_p
2217   (cp_parser *);
2218 static bool cp_parser_next_token_ends_template_argument_p
2219   (cp_parser *);
2220 static bool cp_parser_nth_token_starts_template_argument_list_p
2221   (cp_parser *, size_t);
2222 static enum tag_types cp_parser_token_is_class_key
2223   (cp_token *);
2224 static void cp_parser_check_class_key
2225   (enum tag_types, tree type);
2226 static void cp_parser_check_access_in_redeclaration
2227   (tree type, location_t location);
2228 static bool cp_parser_optional_template_keyword
2229   (cp_parser *);
2230 static void cp_parser_pre_parsed_nested_name_specifier
2231   (cp_parser *);
2232 static bool cp_parser_cache_group
2233   (cp_parser *, enum cpp_ttype, unsigned);
2234 static void cp_parser_parse_tentatively
2235   (cp_parser *);
2236 static void cp_parser_commit_to_tentative_parse
2237   (cp_parser *);
2238 static void cp_parser_abort_tentative_parse
2239   (cp_parser *);
2240 static bool cp_parser_parse_definitely
2241   (cp_parser *);
2242 static inline bool cp_parser_parsing_tentatively
2243   (cp_parser *);
2244 static bool cp_parser_uncommitted_to_tentative_parse_p
2245   (cp_parser *);
2246 static void cp_parser_error
2247   (cp_parser *, const char *);
2248 static void cp_parser_name_lookup_error
2249   (cp_parser *, tree, tree, name_lookup_error, location_t);
2250 static bool cp_parser_simulate_error
2251   (cp_parser *);
2252 static bool cp_parser_check_type_definition
2253   (cp_parser *);
2254 static void cp_parser_check_for_definition_in_return_type
2255   (cp_declarator *, tree, location_t type_location);
2256 static void cp_parser_check_for_invalid_template_id
2257   (cp_parser *, tree, location_t location);
2258 static bool cp_parser_non_integral_constant_expression
2259   (cp_parser *, non_integral_constant);
2260 static void cp_parser_diagnose_invalid_type_name
2261   (cp_parser *, tree, tree, location_t);
2262 static bool cp_parser_parse_and_diagnose_invalid_type_name
2263   (cp_parser *);
2264 static int cp_parser_skip_to_closing_parenthesis
2265   (cp_parser *, bool, bool, bool);
2266 static void cp_parser_skip_to_end_of_statement
2267   (cp_parser *);
2268 static void cp_parser_consume_semicolon_at_end_of_statement
2269   (cp_parser *);
2270 static void cp_parser_skip_to_end_of_block_or_statement
2271   (cp_parser *);
2272 static bool cp_parser_skip_to_closing_brace
2273   (cp_parser *);
2274 static void cp_parser_skip_to_end_of_template_parameter_list
2275   (cp_parser *);
2276 static void cp_parser_skip_to_pragma_eol
2277   (cp_parser*, cp_token *);
2278 static bool cp_parser_error_occurred
2279   (cp_parser *);
2280 static bool cp_parser_allow_gnu_extensions_p
2281   (cp_parser *);
2282 static bool cp_parser_is_pure_string_literal
2283   (cp_token *);
2284 static bool cp_parser_is_string_literal
2285   (cp_token *);
2286 static bool cp_parser_is_keyword
2287   (cp_token *, enum rid);
2288 static tree cp_parser_make_typename_type
2289   (cp_parser *, tree, tree, location_t location);
2290 static cp_declarator * cp_parser_make_indirect_declarator
2291   (enum tree_code, tree, cp_cv_quals, cp_declarator *);
2292
2293 /* Returns nonzero if we are parsing tentatively.  */
2294
2295 static inline bool
2296 cp_parser_parsing_tentatively (cp_parser* parser)
2297 {
2298   return parser->context->next != NULL;
2299 }
2300
2301 /* Returns nonzero if TOKEN is a string literal.  */
2302
2303 static bool
2304 cp_parser_is_pure_string_literal (cp_token* token)
2305 {
2306   return (token->type == CPP_STRING ||
2307           token->type == CPP_STRING16 ||
2308           token->type == CPP_STRING32 ||
2309           token->type == CPP_WSTRING ||
2310           token->type == CPP_UTF8STRING);
2311 }
2312
2313 /* Returns nonzero if TOKEN is a string literal
2314    of a user-defined string literal.  */
2315
2316 static bool
2317 cp_parser_is_string_literal (cp_token* token)
2318 {
2319   return (cp_parser_is_pure_string_literal (token) ||
2320           token->type == CPP_STRING_USERDEF ||
2321           token->type == CPP_STRING16_USERDEF ||
2322           token->type == CPP_STRING32_USERDEF ||
2323           token->type == CPP_WSTRING_USERDEF ||
2324           token->type == CPP_UTF8STRING_USERDEF);
2325 }
2326
2327 /* Returns nonzero if TOKEN is the indicated KEYWORD.  */
2328
2329 static bool
2330 cp_parser_is_keyword (cp_token* token, enum rid keyword)
2331 {
2332   return token->keyword == keyword;
2333 }
2334
2335 /* If not parsing tentatively, issue a diagnostic of the form
2336       FILE:LINE: MESSAGE before TOKEN
2337    where TOKEN is the next token in the input stream.  MESSAGE
2338    (specified by the caller) is usually of the form "expected
2339    OTHER-TOKEN".  */
2340
2341 static void
2342 cp_parser_error (cp_parser* parser, const char* gmsgid)
2343 {
2344   if (!cp_parser_simulate_error (parser))
2345     {
2346       cp_token *token = cp_lexer_peek_token (parser->lexer);
2347       /* This diagnostic makes more sense if it is tagged to the line
2348          of the token we just peeked at.  */
2349       cp_lexer_set_source_position_from_token (token);
2350
2351       if (token->type == CPP_PRAGMA)
2352         {
2353           error_at (token->location,
2354                     "%<#pragma%> is not allowed here");
2355           cp_parser_skip_to_pragma_eol (parser, token);
2356           return;
2357         }
2358
2359       c_parse_error (gmsgid,
2360                      /* Because c_parser_error does not understand
2361                         CPP_KEYWORD, keywords are treated like
2362                         identifiers.  */
2363                      (token->type == CPP_KEYWORD ? CPP_NAME : token->type),
2364                      token->u.value, token->flags);
2365     }
2366 }
2367
2368 /* Issue an error about name-lookup failing.  NAME is the
2369    IDENTIFIER_NODE DECL is the result of
2370    the lookup (as returned from cp_parser_lookup_name).  DESIRED is
2371    the thing that we hoped to find.  */
2372
2373 static void
2374 cp_parser_name_lookup_error (cp_parser* parser,
2375                              tree name,
2376                              tree decl,
2377                              name_lookup_error desired,
2378                              location_t location)
2379 {
2380   /* If name lookup completely failed, tell the user that NAME was not
2381      declared.  */
2382   if (decl == error_mark_node)
2383     {
2384       if (parser->scope && parser->scope != global_namespace)
2385         error_at (location, "%<%E::%E%> has not been declared",
2386                   parser->scope, name);
2387       else if (parser->scope == global_namespace)
2388         error_at (location, "%<::%E%> has not been declared", name);
2389       else if (parser->object_scope
2390                && !CLASS_TYPE_P (parser->object_scope))
2391         error_at (location, "request for member %qE in non-class type %qT",
2392                   name, parser->object_scope);
2393       else if (parser->object_scope)
2394         error_at (location, "%<%T::%E%> has not been declared",
2395                   parser->object_scope, name);
2396       else
2397         error_at (location, "%qE has not been declared", name);
2398     }
2399   else if (parser->scope && parser->scope != global_namespace)
2400     {
2401       switch (desired)
2402         {
2403           case NLE_TYPE:
2404             error_at (location, "%<%E::%E%> is not a type",
2405                                 parser->scope, name);
2406             break;
2407           case NLE_CXX98:
2408             error_at (location, "%<%E::%E%> is not a class or namespace",
2409                                 parser->scope, name);
2410             break;
2411           case NLE_NOT_CXX98:
2412             error_at (location,
2413                       "%<%E::%E%> is not a class, namespace, or enumeration",
2414                       parser->scope, name);
2415             break;
2416           default:
2417             gcc_unreachable ();
2418             
2419         }
2420     }
2421   else if (parser->scope == global_namespace)
2422     {
2423       switch (desired)
2424         {
2425           case NLE_TYPE:
2426             error_at (location, "%<::%E%> is not a type", name);
2427             break;
2428           case NLE_CXX98:
2429             error_at (location, "%<::%E%> is not a class or namespace", name);
2430             break;
2431           case NLE_NOT_CXX98:
2432             error_at (location,
2433                       "%<::%E%> is not a class, namespace, or enumeration",
2434                       name);
2435             break;
2436           default:
2437             gcc_unreachable ();
2438         }
2439     }
2440   else
2441     {
2442       switch (desired)
2443         {
2444           case NLE_TYPE:
2445             error_at (location, "%qE is not a type", name);
2446             break;
2447           case NLE_CXX98:
2448             error_at (location, "%qE is not a class or namespace", name);
2449             break;
2450           case NLE_NOT_CXX98:
2451             error_at (location,
2452                       "%qE is not a class, namespace, or enumeration", name);
2453             break;
2454           default:
2455             gcc_unreachable ();
2456         }
2457     }
2458 }
2459
2460 /* If we are parsing tentatively, remember that an error has occurred
2461    during this tentative parse.  Returns true if the error was
2462    simulated; false if a message should be issued by the caller.  */
2463
2464 static bool
2465 cp_parser_simulate_error (cp_parser* parser)
2466 {
2467   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2468     {
2469       parser->context->status = CP_PARSER_STATUS_KIND_ERROR;
2470       return true;
2471     }
2472   return false;
2473 }
2474
2475 /* Check for repeated decl-specifiers.  */
2476
2477 static void
2478 cp_parser_check_decl_spec (cp_decl_specifier_seq *decl_specs,
2479                            location_t location)
2480 {
2481   int ds;
2482
2483   for (ds = ds_first; ds != ds_last; ++ds)
2484     {
2485       unsigned count = decl_specs->specs[ds];
2486       if (count < 2)
2487         continue;
2488       /* The "long" specifier is a special case because of "long long".  */
2489       if (ds == ds_long)
2490         {
2491           if (count > 2)
2492             error_at (location, "%<long long long%> is too long for GCC");
2493           else 
2494             pedwarn_cxx98 (location, OPT_Wlong_long, 
2495                            "ISO C++ 1998 does not support %<long long%>");
2496         }
2497       else if (count > 1)
2498         {
2499           static const char *const decl_spec_names[] = {
2500             "signed",
2501             "unsigned",
2502             "short",
2503             "long",
2504             "const",
2505             "volatile",
2506             "restrict",
2507             "inline",
2508             "virtual",
2509             "explicit",
2510             "friend",
2511             "typedef",
2512             "constexpr",
2513             "__complex",
2514             "__thread"
2515           };
2516           error_at (location, "duplicate %qs", decl_spec_names[ds]);
2517         }
2518     }
2519 }
2520
2521 /* This function is called when a type is defined.  If type
2522    definitions are forbidden at this point, an error message is
2523    issued.  */
2524
2525 static bool
2526 cp_parser_check_type_definition (cp_parser* parser)
2527 {
2528   /* If types are forbidden here, issue a message.  */
2529   if (parser->type_definition_forbidden_message)
2530     {
2531       /* Don't use `%s' to print the string, because quotations (`%<', `%>')
2532          in the message need to be interpreted.  */
2533       error (parser->type_definition_forbidden_message);
2534       return false;
2535     }
2536   return true;
2537 }
2538
2539 /* This function is called when the DECLARATOR is processed.  The TYPE
2540    was a type defined in the decl-specifiers.  If it is invalid to
2541    define a type in the decl-specifiers for DECLARATOR, an error is
2542    issued. TYPE_LOCATION is the location of TYPE and is used
2543    for error reporting.  */
2544
2545 static void
2546 cp_parser_check_for_definition_in_return_type (cp_declarator *declarator,
2547                                                tree type, location_t type_location)
2548 {
2549   /* [dcl.fct] forbids type definitions in return types.
2550      Unfortunately, it's not easy to know whether or not we are
2551      processing a return type until after the fact.  */
2552   while (declarator
2553          && (declarator->kind == cdk_pointer
2554              || declarator->kind == cdk_reference
2555              || declarator->kind == cdk_ptrmem))
2556     declarator = declarator->declarator;
2557   if (declarator
2558       && declarator->kind == cdk_function)
2559     {
2560       error_at (type_location,
2561                 "new types may not be defined in a return type");
2562       inform (type_location, 
2563               "(perhaps a semicolon is missing after the definition of %qT)",
2564               type);
2565     }
2566 }
2567
2568 /* A type-specifier (TYPE) has been parsed which cannot be followed by
2569    "<" in any valid C++ program.  If the next token is indeed "<",
2570    issue a message warning the user about what appears to be an
2571    invalid attempt to form a template-id. LOCATION is the location
2572    of the type-specifier (TYPE) */
2573
2574 static void
2575 cp_parser_check_for_invalid_template_id (cp_parser* parser,
2576                                          tree type, location_t location)
2577 {
2578   cp_token_position start = 0;
2579
2580   if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2581     {
2582       if (TYPE_P (type))
2583         error_at (location, "%qT is not a template", type);
2584       else if (TREE_CODE (type) == IDENTIFIER_NODE)
2585         error_at (location, "%qE is not a template", type);
2586       else
2587         error_at (location, "invalid template-id");
2588       /* Remember the location of the invalid "<".  */
2589       if (cp_parser_uncommitted_to_tentative_parse_p (parser))
2590         start = cp_lexer_token_position (parser->lexer, true);
2591       /* Consume the "<".  */
2592       cp_lexer_consume_token (parser->lexer);
2593       /* Parse the template arguments.  */
2594       cp_parser_enclosed_template_argument_list (parser);
2595       /* Permanently remove the invalid template arguments so that
2596          this error message is not issued again.  */
2597       if (start)
2598         cp_lexer_purge_tokens_after (parser->lexer, start);
2599     }
2600 }
2601
2602 /* If parsing an integral constant-expression, issue an error message
2603    about the fact that THING appeared and return true.  Otherwise,
2604    return false.  In either case, set
2605    PARSER->NON_INTEGRAL_CONSTANT_EXPRESSION_P.  */
2606
2607 static bool
2608 cp_parser_non_integral_constant_expression (cp_parser  *parser,
2609                                             non_integral_constant thing)
2610 {
2611   parser->non_integral_constant_expression_p = true;
2612   if (parser->integral_constant_expression_p)
2613     {
2614       if (!parser->allow_non_integral_constant_expression_p)
2615         {
2616           const char *msg = NULL;
2617           switch (thing)
2618             {
2619               case NIC_FLOAT:
2620                 error ("floating-point literal "
2621                        "cannot appear in a constant-expression");
2622                 return true;
2623               case NIC_CAST:
2624                 error ("a cast to a type other than an integral or "
2625                        "enumeration type cannot appear in a "
2626                        "constant-expression");
2627                 return true;
2628               case NIC_TYPEID:
2629                 error ("%<typeid%> operator "
2630                        "cannot appear in a constant-expression");
2631                 return true;
2632               case NIC_NCC:
2633                 error ("non-constant compound literals "
2634                        "cannot appear in a constant-expression");
2635                 return true;
2636               case NIC_FUNC_CALL:
2637                 error ("a function call "
2638                        "cannot appear in a constant-expression");
2639                 return true;
2640               case NIC_INC:
2641                 error ("an increment "
2642                        "cannot appear in a constant-expression");
2643                 return true;
2644               case NIC_DEC:
2645                 error ("an decrement "
2646                        "cannot appear in a constant-expression");
2647                 return true;
2648               case NIC_ARRAY_REF:
2649                 error ("an array reference "
2650                        "cannot appear in a constant-expression");
2651                 return true;
2652               case NIC_ADDR_LABEL:
2653                 error ("the address of a label "
2654                        "cannot appear in a constant-expression");
2655                 return true;
2656               case NIC_OVERLOADED:
2657                 error ("calls to overloaded operators "
2658                        "cannot appear in a constant-expression");
2659                 return true;
2660               case NIC_ASSIGNMENT:
2661                 error ("an assignment cannot appear in a constant-expression");
2662                 return true;
2663               case NIC_COMMA:
2664                 error ("a comma operator "
2665                        "cannot appear in a constant-expression");
2666                 return true;
2667               case NIC_CONSTRUCTOR:
2668                 error ("a call to a constructor "
2669                        "cannot appear in a constant-expression");
2670                 return true;
2671               case NIC_THIS:
2672                 msg = "this";
2673                 break;
2674               case NIC_FUNC_NAME:
2675                 msg = "__FUNCTION__";
2676                 break;
2677               case NIC_PRETTY_FUNC:
2678                 msg = "__PRETTY_FUNCTION__";
2679                 break;
2680               case NIC_C99_FUNC:
2681                 msg = "__func__";
2682                 break;
2683               case NIC_VA_ARG:
2684                 msg = "va_arg";
2685                 break;
2686               case NIC_ARROW:
2687                 msg = "->";
2688                 break;
2689               case NIC_POINT:
2690                 msg = ".";
2691                 break;
2692               case NIC_STAR:
2693                 msg = "*";
2694                 break;
2695               case NIC_ADDR:
2696                 msg = "&";
2697                 break;
2698               case NIC_PREINCREMENT:
2699                 msg = "++";
2700                 break;
2701               case NIC_PREDECREMENT:
2702                 msg = "--";
2703                 break;
2704               case NIC_NEW:
2705                 msg = "new";
2706                 break;
2707               case NIC_DEL:
2708                 msg = "delete";
2709                 break;
2710               default:
2711                 gcc_unreachable ();
2712             }
2713           if (msg)
2714             error ("%qs cannot appear in a constant-expression", msg);
2715           return true;
2716         }
2717     }
2718   return false;
2719 }
2720
2721 /* Emit a diagnostic for an invalid type name.  SCOPE is the
2722    qualifying scope (or NULL, if none) for ID.  This function commits
2723    to the current active tentative parse, if any.  (Otherwise, the
2724    problematic construct might be encountered again later, resulting
2725    in duplicate error messages.) LOCATION is the location of ID.  */
2726
2727 static void
2728 cp_parser_diagnose_invalid_type_name (cp_parser *parser,
2729                                       tree scope, tree id,
2730                                       location_t location)
2731 {
2732   tree decl, old_scope;
2733   cp_parser_commit_to_tentative_parse (parser);
2734   /* Try to lookup the identifier.  */
2735   old_scope = parser->scope;
2736   parser->scope = scope;
2737   decl = cp_parser_lookup_name_simple (parser, id, location);
2738   parser->scope = old_scope;
2739   /* If the lookup found a template-name, it means that the user forgot
2740   to specify an argument list. Emit a useful error message.  */
2741   if (TREE_CODE (decl) == TEMPLATE_DECL)
2742     error_at (location,
2743               "invalid use of template-name %qE without an argument list",
2744               decl);
2745   else if (TREE_CODE (id) == BIT_NOT_EXPR)
2746     error_at (location, "invalid use of destructor %qD as a type", id);
2747   else if (TREE_CODE (decl) == TYPE_DECL)
2748     /* Something like 'unsigned A a;'  */
2749     error_at (location, "invalid combination of multiple type-specifiers");
2750   else if (!parser->scope)
2751     {
2752       /* Issue an error message.  */
2753       error_at (location, "%qE does not name a type", id);
2754       /* If we're in a template class, it's possible that the user was
2755          referring to a type from a base class.  For example:
2756
2757            template <typename T> struct A { typedef T X; };
2758            template <typename T> struct B : public A<T> { X x; };
2759
2760          The user should have said "typename A<T>::X".  */
2761       if (cxx_dialect < cxx0x && id == ridpointers[(int)RID_CONSTEXPR])
2762         inform (location, "C++0x %<constexpr%> only available with "
2763                 "-std=c++0x or -std=gnu++0x");
2764       else if (processing_template_decl && current_class_type
2765                && TYPE_BINFO (current_class_type))
2766         {
2767           tree b;
2768
2769           for (b = TREE_CHAIN (TYPE_BINFO (current_class_type));
2770                b;
2771                b = TREE_CHAIN (b))
2772             {
2773               tree base_type = BINFO_TYPE (b);
2774               if (CLASS_TYPE_P (base_type)
2775                   && dependent_type_p (base_type))
2776                 {
2777                   tree field;
2778                   /* Go from a particular instantiation of the
2779                      template (which will have an empty TYPE_FIELDs),
2780                      to the main version.  */
2781                   base_type = CLASSTYPE_PRIMARY_TEMPLATE_TYPE (base_type);
2782                   for (field = TYPE_FIELDS (base_type);
2783                        field;
2784                        field = DECL_CHAIN (field))
2785                     if (TREE_CODE (field) == TYPE_DECL
2786                         && DECL_NAME (field) == id)
2787                       {
2788                         inform (location, 
2789                                 "(perhaps %<typename %T::%E%> was intended)",
2790                                 BINFO_TYPE (b), id);
2791                         break;
2792                       }
2793                   if (field)
2794                     break;
2795                 }
2796             }
2797         }
2798     }
2799   /* Here we diagnose qualified-ids where the scope is actually correct,
2800      but the identifier does not resolve to a valid type name.  */
2801   else if (parser->scope != error_mark_node)
2802     {
2803       if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
2804         error_at (location, "%qE in namespace %qE does not name a type",
2805                   id, parser->scope);
2806       else if (CLASS_TYPE_P (parser->scope)
2807                && constructor_name_p (id, parser->scope))
2808         {
2809           /* A<T>::A<T>() */
2810           error_at (location, "%<%T::%E%> names the constructor, not"
2811                     " the type", parser->scope, id);
2812           if (cp_lexer_next_token_is (parser->lexer, CPP_LESS))
2813             error_at (location, "and %qT has no template constructors",
2814                       parser->scope);
2815         }
2816       else if (TYPE_P (parser->scope)
2817                && dependent_scope_p (parser->scope))
2818         error_at (location, "need %<typename%> before %<%T::%E%> because "
2819                   "%qT is a dependent scope",
2820                   parser->scope, id, parser->scope);
2821       else if (TYPE_P (parser->scope))
2822         error_at (location, "%qE in %q#T does not name a type",
2823                   id, parser->scope);
2824       else
2825         gcc_unreachable ();
2826     }
2827 }
2828
2829 /* Check for a common situation where a type-name should be present,
2830    but is not, and issue a sensible error message.  Returns true if an
2831    invalid type-name was detected.
2832
2833    The situation handled by this function are variable declarations of the
2834    form `ID a', where `ID' is an id-expression and `a' is a plain identifier.
2835    Usually, `ID' should name a type, but if we got here it means that it
2836    does not. We try to emit the best possible error message depending on
2837    how exactly the id-expression looks like.  */
2838
2839 static bool
2840 cp_parser_parse_and_diagnose_invalid_type_name (cp_parser *parser)
2841 {
2842   tree id;
2843   cp_token *token = cp_lexer_peek_token (parser->lexer);
2844
2845   /* Avoid duplicate error about ambiguous lookup.  */
2846   if (token->type == CPP_NESTED_NAME_SPECIFIER)
2847     {
2848       cp_token *next = cp_lexer_peek_nth_token (parser->lexer, 2);
2849       if (next->type == CPP_NAME && next->ambiguous_p)
2850         goto out;
2851     }
2852
2853   cp_parser_parse_tentatively (parser);
2854   id = cp_parser_id_expression (parser,
2855                                 /*template_keyword_p=*/false,
2856                                 /*check_dependency_p=*/true,
2857                                 /*template_p=*/NULL,
2858                                 /*declarator_p=*/true,
2859                                 /*optional_p=*/false);
2860   /* If the next token is a (, this is a function with no explicit return
2861      type, i.e. constructor, destructor or conversion op.  */
2862   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
2863       || TREE_CODE (id) == TYPE_DECL)
2864     {
2865       cp_parser_abort_tentative_parse (parser);
2866       return false;
2867     }
2868   if (!cp_parser_parse_definitely (parser))
2869     return false;
2870
2871   /* Emit a diagnostic for the invalid type.  */
2872   cp_parser_diagnose_invalid_type_name (parser, parser->scope,
2873                                         id, token->location);
2874  out:
2875   /* If we aren't in the middle of a declarator (i.e. in a
2876      parameter-declaration-clause), skip to the end of the declaration;
2877      there's no point in trying to process it.  */
2878   if (!parser->in_declarator_p)
2879     cp_parser_skip_to_end_of_block_or_statement (parser);
2880   return true;
2881 }
2882
2883 /* Consume tokens up to, and including, the next non-nested closing `)'.
2884    Returns 1 iff we found a closing `)'.  RECOVERING is true, if we
2885    are doing error recovery. Returns -1 if OR_COMMA is true and we
2886    found an unnested comma.  */
2887
2888 static int
2889 cp_parser_skip_to_closing_parenthesis (cp_parser *parser,
2890                                        bool recovering,
2891                                        bool or_comma,
2892                                        bool consume_paren)
2893 {
2894   unsigned paren_depth = 0;
2895   unsigned brace_depth = 0;
2896   unsigned square_depth = 0;
2897
2898   if (recovering && !or_comma
2899       && cp_parser_uncommitted_to_tentative_parse_p (parser))
2900     return 0;
2901
2902   while (true)
2903     {
2904       cp_token * token = cp_lexer_peek_token (parser->lexer);
2905
2906       switch (token->type)
2907         {
2908         case CPP_EOF:
2909         case CPP_PRAGMA_EOL:
2910           /* If we've run out of tokens, then there is no closing `)'.  */
2911           return 0;
2912
2913         /* This is good for lambda expression capture-lists.  */
2914         case CPP_OPEN_SQUARE:
2915           ++square_depth;
2916           break;
2917         case CPP_CLOSE_SQUARE:
2918           if (!square_depth--)
2919             return 0;
2920           break;
2921
2922         case CPP_SEMICOLON:
2923           /* This matches the processing in skip_to_end_of_statement.  */
2924           if (!brace_depth)
2925             return 0;
2926           break;
2927
2928         case CPP_OPEN_BRACE:
2929           ++brace_depth;
2930           break;
2931         case CPP_CLOSE_BRACE:
2932           if (!brace_depth--)
2933             return 0;
2934           break;
2935
2936         case CPP_COMMA:
2937           if (recovering && or_comma && !brace_depth && !paren_depth
2938               && !square_depth)
2939             return -1;
2940           break;
2941
2942         case CPP_OPEN_PAREN:
2943           if (!brace_depth)
2944             ++paren_depth;
2945           break;
2946
2947         case CPP_CLOSE_PAREN:
2948           if (!brace_depth && !paren_depth--)
2949             {
2950               if (consume_paren)
2951                 cp_lexer_consume_token (parser->lexer);
2952               return 1;
2953             }
2954           break;
2955
2956         default:
2957           break;
2958         }
2959
2960       /* Consume the token.  */
2961       cp_lexer_consume_token (parser->lexer);
2962     }
2963 }
2964
2965 /* Consume tokens until we reach the end of the current statement.
2966    Normally, that will be just before consuming a `;'.  However, if a
2967    non-nested `}' comes first, then we stop before consuming that.  */
2968
2969 static void
2970 cp_parser_skip_to_end_of_statement (cp_parser* parser)
2971 {
2972   unsigned nesting_depth = 0;
2973
2974   while (true)
2975     {
2976       cp_token *token = cp_lexer_peek_token (parser->lexer);
2977
2978       switch (token->type)
2979         {
2980         case CPP_EOF:
2981         case CPP_PRAGMA_EOL:
2982           /* If we've run out of tokens, stop.  */
2983           return;
2984
2985         case CPP_SEMICOLON:
2986           /* If the next token is a `;', we have reached the end of the
2987              statement.  */
2988           if (!nesting_depth)
2989             return;
2990           break;
2991
2992         case CPP_CLOSE_BRACE:
2993           /* If this is a non-nested '}', stop before consuming it.
2994              That way, when confronted with something like:
2995
2996                { 3 + }
2997
2998              we stop before consuming the closing '}', even though we
2999              have not yet reached a `;'.  */
3000           if (nesting_depth == 0)
3001             return;
3002
3003           /* If it is the closing '}' for a block that we have
3004              scanned, stop -- but only after consuming the token.
3005              That way given:
3006
3007                 void f g () { ... }
3008                 typedef int I;
3009
3010              we will stop after the body of the erroneously declared
3011              function, but before consuming the following `typedef'
3012              declaration.  */
3013           if (--nesting_depth == 0)
3014             {
3015               cp_lexer_consume_token (parser->lexer);
3016               return;
3017             }
3018
3019         case CPP_OPEN_BRACE:
3020           ++nesting_depth;
3021           break;
3022
3023         default:
3024           break;
3025         }
3026
3027       /* Consume the token.  */
3028       cp_lexer_consume_token (parser->lexer);
3029     }
3030 }
3031
3032 /* This function is called at the end of a statement or declaration.
3033    If the next token is a semicolon, it is consumed; otherwise, error
3034    recovery is attempted.  */
3035
3036 static void
3037 cp_parser_consume_semicolon_at_end_of_statement (cp_parser *parser)
3038 {
3039   /* Look for the trailing `;'.  */
3040   if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
3041     {
3042       /* If there is additional (erroneous) input, skip to the end of
3043          the statement.  */
3044       cp_parser_skip_to_end_of_statement (parser);
3045       /* If the next token is now a `;', consume it.  */
3046       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
3047         cp_lexer_consume_token (parser->lexer);
3048     }
3049 }
3050
3051 /* Skip tokens until we have consumed an entire block, or until we
3052    have consumed a non-nested `;'.  */
3053
3054 static void
3055 cp_parser_skip_to_end_of_block_or_statement (cp_parser* parser)
3056 {
3057   int nesting_depth = 0;
3058
3059   while (nesting_depth >= 0)
3060     {
3061       cp_token *token = cp_lexer_peek_token (parser->lexer);
3062
3063       switch (token->type)
3064         {
3065         case CPP_EOF:
3066         case CPP_PRAGMA_EOL:
3067           /* If we've run out of tokens, stop.  */
3068           return;
3069
3070         case CPP_SEMICOLON:
3071           /* Stop if this is an unnested ';'. */
3072           if (!nesting_depth)
3073             nesting_depth = -1;
3074           break;
3075
3076         case CPP_CLOSE_BRACE:
3077           /* Stop if this is an unnested '}', or closes the outermost
3078              nesting level.  */
3079           nesting_depth--;
3080           if (nesting_depth < 0)
3081             return;
3082           if (!nesting_depth)
3083             nesting_depth = -1;
3084           break;
3085
3086         case CPP_OPEN_BRACE:
3087           /* Nest. */
3088           nesting_depth++;
3089           break;
3090
3091         default:
3092           break;
3093         }
3094
3095       /* Consume the token.  */
3096       cp_lexer_consume_token (parser->lexer);
3097     }
3098 }
3099
3100 /* Skip tokens until a non-nested closing curly brace is the next
3101    token, or there are no more tokens. Return true in the first case,
3102    false otherwise.  */
3103
3104 static bool
3105 cp_parser_skip_to_closing_brace (cp_parser *parser)
3106 {
3107   unsigned nesting_depth = 0;
3108
3109   while (true)
3110     {
3111       cp_token *token = cp_lexer_peek_token (parser->lexer);
3112
3113       switch (token->type)
3114         {
3115         case CPP_EOF:
3116         case CPP_PRAGMA_EOL:
3117           /* If we've run out of tokens, stop.  */
3118           return false;
3119
3120         case CPP_CLOSE_BRACE:
3121           /* If the next token is a non-nested `}', then we have reached
3122              the end of the current block.  */
3123           if (nesting_depth-- == 0)
3124             return true;
3125           break;
3126
3127         case CPP_OPEN_BRACE:
3128           /* If it the next token is a `{', then we are entering a new
3129              block.  Consume the entire block.  */
3130           ++nesting_depth;
3131           break;
3132
3133         default:
3134           break;
3135         }
3136
3137       /* Consume the token.  */
3138       cp_lexer_consume_token (parser->lexer);
3139     }
3140 }
3141
3142 /* Consume tokens until we reach the end of the pragma.  The PRAGMA_TOK
3143    parameter is the PRAGMA token, allowing us to purge the entire pragma
3144    sequence.  */
3145
3146 static void
3147 cp_parser_skip_to_pragma_eol (cp_parser* parser, cp_token *pragma_tok)
3148 {
3149   cp_token *token;
3150
3151   parser->lexer->in_pragma = false;
3152
3153   do
3154     token = cp_lexer_consume_token (parser->lexer);
3155   while (token->type != CPP_PRAGMA_EOL && token->type != CPP_EOF);
3156
3157   /* Ensure that the pragma is not parsed again.  */
3158   cp_lexer_purge_tokens_after (parser->lexer, pragma_tok);
3159 }
3160
3161 /* Require pragma end of line, resyncing with it as necessary.  The
3162    arguments are as for cp_parser_skip_to_pragma_eol.  */
3163
3164 static void
3165 cp_parser_require_pragma_eol (cp_parser *parser, cp_token *pragma_tok)
3166 {
3167   parser->lexer->in_pragma = false;
3168   if (!cp_parser_require (parser, CPP_PRAGMA_EOL, RT_PRAGMA_EOL))
3169     cp_parser_skip_to_pragma_eol (parser, pragma_tok);
3170 }
3171
3172 /* This is a simple wrapper around make_typename_type. When the id is
3173    an unresolved identifier node, we can provide a superior diagnostic
3174    using cp_parser_diagnose_invalid_type_name.  */
3175
3176 static tree
3177 cp_parser_make_typename_type (cp_parser *parser, tree scope,
3178                               tree id, location_t id_location)
3179 {
3180   tree result;
3181   if (TREE_CODE (id) == IDENTIFIER_NODE)
3182     {
3183       result = make_typename_type (scope, id, typename_type,
3184                                    /*complain=*/tf_none);
3185       if (result == error_mark_node)
3186         cp_parser_diagnose_invalid_type_name (parser, scope, id, id_location);
3187       return result;
3188     }
3189   return make_typename_type (scope, id, typename_type, tf_error);
3190 }
3191
3192 /* This is a wrapper around the
3193    make_{pointer,ptrmem,reference}_declarator functions that decides
3194    which one to call based on the CODE and CLASS_TYPE arguments. The
3195    CODE argument should be one of the values returned by
3196    cp_parser_ptr_operator. */
3197 static cp_declarator *
3198 cp_parser_make_indirect_declarator (enum tree_code code, tree class_type,
3199                                     cp_cv_quals cv_qualifiers,
3200                                     cp_declarator *target)
3201 {
3202   if (code == ERROR_MARK)
3203     return cp_error_declarator;
3204
3205   if (code == INDIRECT_REF)
3206     if (class_type == NULL_TREE)
3207       return make_pointer_declarator (cv_qualifiers, target);
3208     else
3209       return make_ptrmem_declarator (cv_qualifiers, class_type, target);
3210   else if (code == ADDR_EXPR && class_type == NULL_TREE)
3211     return make_reference_declarator (cv_qualifiers, target, false);
3212   else if (code == NON_LVALUE_EXPR && class_type == NULL_TREE)
3213     return make_reference_declarator (cv_qualifiers, target, true);
3214   gcc_unreachable ();
3215 }
3216
3217 /* Create a new C++ parser.  */
3218
3219 static cp_parser *
3220 cp_parser_new (void)
3221 {
3222   cp_parser *parser;
3223   cp_lexer *lexer;
3224   unsigned i;
3225
3226   /* cp_lexer_new_main is called before doing GC allocation because
3227      cp_lexer_new_main might load a PCH file.  */
3228   lexer = cp_lexer_new_main ();
3229
3230   /* Initialize the binops_by_token so that we can get the tree
3231      directly from the token.  */
3232   for (i = 0; i < sizeof (binops) / sizeof (binops[0]); i++)
3233     binops_by_token[binops[i].token_type] = binops[i];
3234
3235   parser = ggc_alloc_cleared_cp_parser ();
3236   parser->lexer = lexer;
3237   parser->context = cp_parser_context_new (NULL);
3238
3239   /* For now, we always accept GNU extensions.  */
3240   parser->allow_gnu_extensions_p = 1;
3241
3242   /* The `>' token is a greater-than operator, not the end of a
3243      template-id.  */
3244   parser->greater_than_is_operator_p = true;
3245
3246   parser->default_arg_ok_p = true;
3247
3248   /* We are not parsing a constant-expression.  */
3249   parser->integral_constant_expression_p = false;
3250   parser->allow_non_integral_constant_expression_p = false;
3251   parser->non_integral_constant_expression_p = false;
3252
3253   /* Local variable names are not forbidden.  */
3254   parser->local_variables_forbidden_p = false;
3255
3256   /* We are not processing an `extern "C"' declaration.  */
3257   parser->in_unbraced_linkage_specification_p = false;
3258
3259   /* We are not processing a declarator.  */
3260   parser->in_declarator_p = false;
3261
3262   /* We are not processing a template-argument-list.  */
3263   parser->in_template_argument_list_p = false;
3264
3265   /* We are not in an iteration statement.  */
3266   parser->in_statement = 0;
3267
3268   /* We are not in a switch statement.  */
3269   parser->in_switch_statement_p = false;
3270
3271   /* We are not parsing a type-id inside an expression.  */
3272   parser->in_type_id_in_expr_p = false;
3273
3274   /* Declarations aren't implicitly extern "C".  */
3275   parser->implicit_extern_c = false;
3276
3277   /* String literals should be translated to the execution character set.  */
3278   parser->translate_strings_p = true;
3279
3280   /* We are not parsing a function body.  */
3281   parser->in_function_body = false;
3282
3283   /* We can correct until told otherwise.  */
3284   parser->colon_corrects_to_scope_p = true;
3285
3286   /* The unparsed function queue is empty.  */
3287   push_unparsed_function_queues (parser);
3288
3289   /* There are no classes being defined.  */
3290   parser->num_classes_being_defined = 0;
3291
3292   /* No template parameters apply.  */
3293   parser->num_template_parameter_lists = 0;
3294
3295   return parser;
3296 }
3297
3298 /* Create a cp_lexer structure which will emit the tokens in CACHE
3299    and push it onto the parser's lexer stack.  This is used for delayed
3300    parsing of in-class method bodies and default arguments, and should
3301    not be confused with tentative parsing.  */
3302 static void
3303 cp_parser_push_lexer_for_tokens (cp_parser *parser, cp_token_cache *cache)
3304 {
3305   cp_lexer *lexer = cp_lexer_new_from_tokens (cache);
3306   lexer->next = parser->lexer;
3307   parser->lexer = lexer;
3308
3309   /* Move the current source position to that of the first token in the
3310      new lexer.  */
3311   cp_lexer_set_source_position_from_token (lexer->next_token);
3312 }
3313
3314 /* Pop the top lexer off the parser stack.  This is never used for the
3315    "main" lexer, only for those pushed by cp_parser_push_lexer_for_tokens.  */
3316 static void
3317 cp_parser_pop_lexer (cp_parser *parser)
3318 {
3319   cp_lexer *lexer = parser->lexer;
3320   parser->lexer = lexer->next;
3321   cp_lexer_destroy (lexer);
3322
3323   /* Put the current source position back where it was before this
3324      lexer was pushed.  */
3325   cp_lexer_set_source_position_from_token (parser->lexer->next_token);
3326 }
3327
3328 /* Lexical conventions [gram.lex]  */
3329
3330 /* Parse an identifier.  Returns an IDENTIFIER_NODE representing the
3331    identifier.  */
3332
3333 static tree
3334 cp_parser_identifier (cp_parser* parser)
3335 {
3336   cp_token *token;
3337
3338   /* Look for the identifier.  */
3339   token = cp_parser_require (parser, CPP_NAME, RT_NAME);
3340   /* Return the value.  */
3341   return token ? token->u.value : error_mark_node;
3342 }
3343
3344 /* Parse a sequence of adjacent string constants.  Returns a
3345    TREE_STRING representing the combined, nul-terminated string
3346    constant.  If TRANSLATE is true, translate the string to the
3347    execution character set.  If WIDE_OK is true, a wide string is
3348    invalid here.
3349
3350    C++98 [lex.string] says that if a narrow string literal token is
3351    adjacent to a wide string literal token, the behavior is undefined.
3352    However, C99 6.4.5p4 says that this results in a wide string literal.
3353    We follow C99 here, for consistency with the C front end.
3354
3355    This code is largely lifted from lex_string() in c-lex.c.
3356
3357    FUTURE: ObjC++ will need to handle @-strings here.  */
3358 static tree
3359 cp_parser_string_literal (cp_parser *parser, bool translate, bool wide_ok)
3360 {
3361   tree value;
3362   size_t count;
3363   struct obstack str_ob;
3364   cpp_string str, istr, *strs;
3365   cp_token *tok;
3366   enum cpp_ttype type, curr_type;
3367   int have_suffix_p = 0;
3368   tree string_tree;
3369   tree suffix_id = NULL_TREE;
3370   bool curr_tok_is_userdef_p = false;
3371
3372   tok = cp_lexer_peek_token (parser->lexer);
3373   if (!cp_parser_is_string_literal (tok))
3374     {
3375       cp_parser_error (parser, "expected string-literal");
3376       return error_mark_node;
3377     }
3378
3379   if (cpp_userdef_string_p (tok->type))
3380     {
3381       string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3382       curr_type = cpp_userdef_string_remove_type (tok->type);
3383       curr_tok_is_userdef_p = true;
3384     }
3385   else
3386     {
3387       string_tree = tok->u.value;
3388       curr_type = tok->type;
3389     }
3390   type = curr_type;
3391
3392   /* Try to avoid the overhead of creating and destroying an obstack
3393      for the common case of just one string.  */
3394   if (!cp_parser_is_string_literal
3395       (cp_lexer_peek_nth_token (parser->lexer, 2)))
3396     {
3397       cp_lexer_consume_token (parser->lexer);
3398
3399       str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3400       str.len = TREE_STRING_LENGTH (string_tree);
3401       count = 1;
3402
3403       if (curr_tok_is_userdef_p)
3404         {
3405           suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3406           have_suffix_p = 1;
3407           curr_type = cpp_userdef_string_remove_type (tok->type);
3408         }
3409       else
3410         curr_type = tok->type;
3411
3412       strs = &str;
3413     }
3414   else
3415     {
3416       gcc_obstack_init (&str_ob);
3417       count = 0;
3418
3419       do
3420         {
3421           cp_lexer_consume_token (parser->lexer);
3422           count++;
3423           str.text = (const unsigned char *)TREE_STRING_POINTER (string_tree);
3424           str.len = TREE_STRING_LENGTH (string_tree);
3425
3426           if (curr_tok_is_userdef_p)
3427             {
3428               tree curr_suffix_id = USERDEF_LITERAL_SUFFIX_ID (tok->u.value);
3429               if (have_suffix_p == 0)
3430                 {
3431                   suffix_id = curr_suffix_id;
3432                   have_suffix_p = 1;
3433                 }
3434               else if (have_suffix_p == 1
3435                        && curr_suffix_id != suffix_id)
3436                 {
3437                   error ("inconsistent user-defined literal suffixes"
3438                          " %qD and %qD in string literal",
3439                          suffix_id, curr_suffix_id);
3440                   have_suffix_p = -1;
3441                 }
3442               curr_type = cpp_userdef_string_remove_type (tok->type);
3443             }
3444           else
3445             curr_type = tok->type;
3446
3447           if (type != curr_type)
3448             {
3449               if (type == CPP_STRING)
3450                 type = curr_type;
3451               else if (curr_type != CPP_STRING)
3452                 error_at (tok->location,
3453                           "unsupported non-standard concatenation "
3454                           "of string literals");
3455             }
3456
3457           obstack_grow (&str_ob, &str, sizeof (cpp_string));
3458
3459           tok = cp_lexer_peek_token (parser->lexer);
3460           if (cpp_userdef_string_p (tok->type))
3461             {
3462               string_tree = USERDEF_LITERAL_VALUE (tok->u.value);
3463               curr_type = cpp_userdef_string_remove_type (tok->type);
3464               curr_tok_is_userdef_p = true;
3465             }
3466           else
3467             {
3468               string_tree = tok->u.value;
3469               curr_type = tok->type;
3470               curr_tok_is_userdef_p = false;
3471             }
3472         }
3473       while (cp_parser_is_string_literal (tok));
3474
3475       strs = (cpp_string *) obstack_finish (&str_ob);
3476     }
3477
3478   if (type != CPP_STRING && !wide_ok)
3479     {
3480       cp_parser_error (parser, "a wide string is invalid in this context");
3481       type = CPP_STRING;
3482     }
3483
3484   if ((translate ? cpp_interpret_string : cpp_interpret_string_notranslate)
3485       (parse_in, strs, count, &istr, type))
3486     {
3487       value = build_string (istr.len, (const char *)istr.text);
3488       free (CONST_CAST (unsigned char *, istr.text));
3489
3490       switch (type)
3491         {
3492         default:
3493         case CPP_STRING:
3494         case CPP_UTF8STRING:
3495           TREE_TYPE (value) = char_array_type_node;
3496           break;
3497         case CPP_STRING16:
3498           TREE_TYPE (value) = char16_array_type_node;
3499           break;
3500         case CPP_STRING32:
3501           TREE_TYPE (value) = char32_array_type_node;
3502           break;
3503         case CPP_WSTRING:
3504           TREE_TYPE (value) = wchar_array_type_node;
3505           break;
3506         }
3507
3508       value = fix_string_type (value);
3509
3510       if (have_suffix_p)
3511         {
3512           tree literal = build_userdef_literal (suffix_id, value, NULL_TREE);
3513           tok->u.value = literal;
3514           return cp_parser_userdef_string_literal (tok);
3515         }
3516     }
3517   else
3518     /* cpp_interpret_string has issued an error.  */
3519     value = error_mark_node;
3520
3521   if (count > 1)
3522     obstack_free (&str_ob, 0);
3523
3524   return value;
3525 }
3526
3527 /* Parse a user-defined char constant.  Returns a call to a user-defined
3528    literal operator taking the character as an argument.  */
3529
3530 static tree
3531 cp_parser_userdef_char_literal (cp_parser *parser)
3532 {
3533   cp_token *token = NULL;
3534   tree literal, suffix_id, value;
3535   tree name, decl;
3536   tree result;
3537   VEC(tree,gc) *vec;
3538
3539   token = cp_lexer_consume_token (parser->lexer);
3540   literal = token->u.value;
3541   suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3542   value = USERDEF_LITERAL_VALUE (literal);
3543   name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3544
3545   /* Build up a call to the user-defined operator  */
3546   /* Lookup the name we got back from the id-expression.  */
3547   vec = make_tree_vector ();
3548   VEC_safe_push (tree, gc, vec, value);
3549   decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3550   if (!decl || decl == error_mark_node)
3551     {
3552       error ("unable to find user-defined character literal operator %qD",
3553              name);
3554       release_tree_vector (vec);
3555       return error_mark_node;
3556     }
3557   result = finish_call_expr (decl, &vec, false, true, tf_warning_or_error);
3558   release_tree_vector (vec);
3559
3560   return result;
3561 }
3562
3563 /* A subroutine of cp_parser_userdef_numeric_literal to
3564    create a char... template parameter pack from a string node.  */
3565
3566 static tree
3567 make_char_string_pack (tree value)
3568 {
3569   tree charvec;
3570   tree argpack = make_node (NONTYPE_ARGUMENT_PACK);
3571   const char *str = TREE_STRING_POINTER (value);
3572   int i, len = TREE_STRING_LENGTH (value) - 1;
3573   tree argvec = make_tree_vec (1);
3574
3575   /* Fill in CHARVEC with all of the parameters.  */
3576   charvec = make_tree_vec (len);
3577   for (i = 0; i < len; ++i)
3578     TREE_VEC_ELT (charvec, i) = build_int_cst (char_type_node, str[i]);
3579
3580   /* Build the argument packs.  */
3581   SET_ARGUMENT_PACK_ARGS (argpack, charvec);
3582   TREE_TYPE (argpack) = char_type_node;
3583
3584   TREE_VEC_ELT (argvec, 0) = argpack;
3585
3586   return argvec;
3587 }
3588
3589 /* Parse a user-defined numeric constant.  returns a call to a user-defined
3590    literal operator.  */
3591
3592 static tree
3593 cp_parser_userdef_numeric_literal (cp_parser *parser)
3594 {
3595   cp_token *token = NULL;
3596   tree literal, suffix_id, value, num_string;
3597   tree name, decl;
3598   tree result = error_mark_node;
3599   VEC(tree,gc) *args;
3600
3601   token = cp_lexer_consume_token (parser->lexer);
3602   literal = token->u.value;
3603   suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3604   value = USERDEF_LITERAL_VALUE (literal);
3605   num_string = USERDEF_LITERAL_NUM_STRING (literal);
3606   name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3607
3608   /* Build up a call to the user-defined operator  */
3609   /* Lookup the name we got back from the id-expression.  */
3610   /* Try to find the literal operator by finishing the call expression
3611      with the numeric argument.  */
3612   args = make_tree_vector ();
3613   VEC_safe_push (tree, gc, args, value);
3614   decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3615   if (decl && decl != error_mark_node)
3616     {
3617       result = finish_call_expr (decl, &args, false, true, tf_none);
3618       if (result != error_mark_node)
3619         {
3620           release_tree_vector (args);
3621           return result;
3622         }
3623     }
3624   release_tree_vector (args);
3625
3626   /* If the numeric argument didn't work, look for a raw literal
3627      operator taking a const char* argument consisting of the number
3628      in string format.  */
3629   args = make_tree_vector ();
3630   VEC_safe_push (tree, gc, args, num_string);
3631   decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3632   if (decl && decl != error_mark_node)
3633     {
3634       result = finish_call_expr (decl, &args, false, true, tf_none);
3635       if (result != error_mark_node)
3636         {
3637           release_tree_vector (args);
3638           return result;
3639         }
3640     }
3641   release_tree_vector (args);
3642
3643   /* If the raw literal didn't work, look for a non-type template
3644      function with parameter pack char....  Call the function with
3645      template parameter characters representing the number.  */
3646   args = make_tree_vector ();
3647   decl = lookup_function_nonclass (name, args, /*block_p=*/false);
3648   if (decl && decl != error_mark_node)
3649     {
3650       tree tmpl_args = make_char_string_pack (num_string);
3651       decl = lookup_template_function (decl, tmpl_args);
3652       result = finish_call_expr (decl, &args, false, true, tf_none);
3653       if (result != error_mark_node)
3654         {
3655           release_tree_vector (args);
3656           return result;
3657         }
3658     }
3659   release_tree_vector (args);
3660
3661   if (result == error_mark_node)
3662     error ("unable to find user-defined numeric literal operator %qD", name);
3663
3664   return result;
3665 }
3666
3667 /* Parse a user-defined string constant.  Returns a call to a user-defined
3668    literal operator taking a character pointer and the length of the string
3669    as arguments.  */
3670 static tree
3671 cp_parser_userdef_string_literal (cp_token *token)
3672 {
3673   tree literal, suffix_id, value;
3674   tree name, decl;
3675   tree result;
3676   VEC(tree,gc) *vec;
3677   int len;
3678
3679   literal = token->u.value;
3680   suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3681   name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3682   value = USERDEF_LITERAL_VALUE (literal);
3683   len = TREE_STRING_LENGTH (value) - 1;
3684
3685   /* Build up a call to the user-defined operator  */
3686   /* Lookup the name we got back from the id-expression.  */
3687   vec = make_tree_vector ();
3688   VEC_safe_push (tree, gc, vec, value);
3689   VEC_safe_push (tree, gc, vec, build_int_cst (size_type_node, len));
3690   decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3691   if (!decl || decl == error_mark_node)
3692     {
3693       error ("unable to find user-defined string literal operator %qD", name);
3694       release_tree_vector (vec);
3695       return error_mark_node;
3696     }
3697   result = finish_call_expr (decl, &vec, false, true, tf_none);
3698   if (result == error_mark_node)
3699     error ("unable to find valid user-defined string literal operator %qD."
3700            "  Possible missing length argument in string literal operator.",
3701            name);
3702   release_tree_vector (vec);
3703
3704   return result;
3705 }
3706
3707
3708 /* Basic concepts [gram.basic]  */
3709
3710 /* Parse a translation-unit.
3711
3712    translation-unit:
3713      declaration-seq [opt]
3714
3715    Returns TRUE if all went well.  */
3716
3717 static bool
3718 cp_parser_translation_unit (cp_parser* parser)
3719 {
3720   /* The address of the first non-permanent object on the declarator
3721      obstack.  */
3722   static void *declarator_obstack_base;
3723
3724   bool success;
3725
3726   /* Create the declarator obstack, if necessary.  */
3727   if (!cp_error_declarator)
3728     {
3729       gcc_obstack_init (&declarator_obstack);
3730       /* Create the error declarator.  */
3731       cp_error_declarator = make_declarator (cdk_error);
3732       /* Create the empty parameter list.  */
3733       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3734       /* Remember where the base of the declarator obstack lies.  */
3735       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3736     }
3737
3738   cp_parser_declaration_seq_opt (parser);
3739
3740   /* If there are no tokens left then all went well.  */
3741   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3742     {
3743       /* Get rid of the token array; we don't need it any more.  */
3744       cp_lexer_destroy (parser->lexer);
3745       parser->lexer = NULL;
3746
3747       /* This file might have been a context that's implicitly extern
3748          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3749       if (parser->implicit_extern_c)
3750         {
3751           pop_lang_context ();
3752           parser->implicit_extern_c = false;
3753         }
3754
3755       /* Finish up.  */
3756       finish_translation_unit ();
3757
3758       success = true;
3759     }
3760   else
3761     {
3762       cp_parser_error (parser, "expected declaration");
3763       success = false;
3764     }
3765
3766   /* Make sure the declarator obstack was fully cleaned up.  */
3767   gcc_assert (obstack_next_free (&declarator_obstack)
3768               == declarator_obstack_base);
3769
3770   /* All went well.  */
3771   return success;
3772 }
3773
3774 /* Expressions [gram.expr] */
3775
3776 /* Parse a primary-expression.
3777
3778    primary-expression:
3779      literal
3780      this
3781      ( expression )
3782      id-expression
3783
3784    GNU Extensions:
3785
3786    primary-expression:
3787      ( compound-statement )
3788      __builtin_va_arg ( assignment-expression , type-id )
3789      __builtin_offsetof ( type-id , offsetof-expression )
3790
3791    C++ Extensions:
3792      __has_nothrow_assign ( type-id )   
3793      __has_nothrow_constructor ( type-id )
3794      __has_nothrow_copy ( type-id )
3795      __has_trivial_assign ( type-id )   
3796      __has_trivial_constructor ( type-id )
3797      __has_trivial_copy ( type-id )
3798      __has_trivial_destructor ( type-id )
3799      __has_virtual_destructor ( type-id )     
3800      __is_abstract ( type-id )
3801      __is_base_of ( type-id , type-id )
3802      __is_class ( type-id )
3803      __is_convertible_to ( type-id , type-id )     
3804      __is_empty ( type-id )
3805      __is_enum ( type-id )
3806      __is_literal_type ( type-id )
3807      __is_pod ( type-id )
3808      __is_polymorphic ( type-id )
3809      __is_std_layout ( type-id )
3810      __is_trivial ( type-id )
3811      __is_union ( type-id )
3812
3813    Objective-C++ Extension:
3814
3815    primary-expression:
3816      objc-expression
3817
3818    literal:
3819      __null
3820
3821    ADDRESS_P is true iff this expression was immediately preceded by
3822    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3823    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3824    true iff this expression is a template argument.
3825
3826    Returns a representation of the expression.  Upon return, *IDK
3827    indicates what kind of id-expression (if any) was present.  */
3828
3829 static tree
3830 cp_parser_primary_expression (cp_parser *parser,
3831                               bool address_p,
3832                               bool cast_p,
3833                               bool template_arg_p,
3834                               cp_id_kind *idk)
3835 {
3836   cp_token *token = NULL;
3837
3838   /* Assume the primary expression is not an id-expression.  */
3839   *idk = CP_ID_KIND_NONE;
3840
3841   /* Peek at the next token.  */
3842   token = cp_lexer_peek_token (parser->lexer);
3843   switch (token->type)
3844     {
3845       /* literal:
3846            integer-literal
3847            character-literal
3848            floating-literal
3849            string-literal
3850            boolean-literal
3851            pointer-literal
3852            user-defined-literal  */
3853     case CPP_CHAR:
3854     case CPP_CHAR16:
3855     case CPP_CHAR32:
3856     case CPP_WCHAR:
3857     case CPP_NUMBER:
3858       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3859         return cp_parser_userdef_numeric_literal (parser);
3860       token = cp_lexer_consume_token (parser->lexer);
3861       if (TREE_CODE (token->u.value) == FIXED_CST)
3862         {
3863           error_at (token->location,
3864                     "fixed-point types not supported in C++");
3865           return error_mark_node;
3866         }
3867       /* Floating-point literals are only allowed in an integral
3868          constant expression if they are cast to an integral or
3869          enumeration type.  */
3870       if (TREE_CODE (token->u.value) == REAL_CST
3871           && parser->integral_constant_expression_p
3872           && pedantic)
3873         {
3874           /* CAST_P will be set even in invalid code like "int(2.7 +
3875              ...)".   Therefore, we have to check that the next token
3876              is sure to end the cast.  */
3877           if (cast_p)
3878             {
3879               cp_token *next_token;
3880
3881               next_token = cp_lexer_peek_token (parser->lexer);
3882               if (/* The comma at the end of an
3883                      enumerator-definition.  */
3884                   next_token->type != CPP_COMMA
3885                   /* The curly brace at the end of an enum-specifier.  */
3886                   && next_token->type != CPP_CLOSE_BRACE
3887                   /* The end of a statement.  */
3888                   && next_token->type != CPP_SEMICOLON
3889                   /* The end of the cast-expression.  */
3890                   && next_token->type != CPP_CLOSE_PAREN
3891                   /* The end of an array bound.  */
3892                   && next_token->type != CPP_CLOSE_SQUARE
3893                   /* The closing ">" in a template-argument-list.  */
3894                   && (next_token->type != CPP_GREATER
3895                       || parser->greater_than_is_operator_p)
3896                   /* C++0x only: A ">>" treated like two ">" tokens,
3897                      in a template-argument-list.  */
3898                   && (next_token->type != CPP_RSHIFT
3899                       || (cxx_dialect == cxx98)
3900                       || parser->greater_than_is_operator_p))
3901                 cast_p = false;
3902             }
3903
3904           /* If we are within a cast, then the constraint that the
3905              cast is to an integral or enumeration type will be
3906              checked at that point.  If we are not within a cast, then
3907              this code is invalid.  */
3908           if (!cast_p)
3909             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3910         }
3911       return token->u.value;
3912
3913     case CPP_CHAR_USERDEF:
3914     case CPP_CHAR16_USERDEF:
3915     case CPP_CHAR32_USERDEF:
3916     case CPP_WCHAR_USERDEF:
3917       return cp_parser_userdef_char_literal (parser);
3918
3919     case CPP_STRING:
3920     case CPP_STRING16:
3921     case CPP_STRING32:
3922     case CPP_WSTRING:
3923     case CPP_UTF8STRING:
3924     case CPP_STRING_USERDEF:
3925     case CPP_STRING16_USERDEF:
3926     case CPP_STRING32_USERDEF:
3927     case CPP_WSTRING_USERDEF:
3928     case CPP_UTF8STRING_USERDEF:
3929       /* ??? Should wide strings be allowed when parser->translate_strings_p
3930          is false (i.e. in attributes)?  If not, we can kill the third
3931          argument to cp_parser_string_literal.  */
3932       return cp_parser_string_literal (parser,
3933                                        parser->translate_strings_p,
3934                                        true);
3935
3936     case CPP_OPEN_PAREN:
3937       {
3938         tree expr;
3939         bool saved_greater_than_is_operator_p;
3940
3941         /* Consume the `('.  */
3942         cp_lexer_consume_token (parser->lexer);
3943         /* Within a parenthesized expression, a `>' token is always
3944            the greater-than operator.  */
3945         saved_greater_than_is_operator_p
3946           = parser->greater_than_is_operator_p;
3947         parser->greater_than_is_operator_p = true;
3948         /* If we see `( { ' then we are looking at the beginning of
3949            a GNU statement-expression.  */
3950         if (cp_parser_allow_gnu_extensions_p (parser)
3951             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3952           {
3953             /* Statement-expressions are not allowed by the standard.  */
3954             pedwarn (token->location, OPT_pedantic, 
3955                      "ISO C++ forbids braced-groups within expressions");
3956
3957             /* And they're not allowed outside of a function-body; you
3958                cannot, for example, write:
3959
3960                  int i = ({ int j = 3; j + 1; });
3961
3962                at class or namespace scope.  */
3963             if (!parser->in_function_body
3964                 || parser->in_template_argument_list_p)
3965               {
3966                 error_at (token->location,
3967                           "statement-expressions are not allowed outside "
3968                           "functions nor in template-argument lists");
3969                 cp_parser_skip_to_end_of_block_or_statement (parser);
3970                 expr = error_mark_node;
3971               }
3972             else
3973               {
3974                 /* Start the statement-expression.  */
3975                 expr = begin_stmt_expr ();
3976                 /* Parse the compound-statement.  */
3977                 cp_parser_compound_statement (parser, expr, false, false);
3978                 /* Finish up.  */
3979                 expr = finish_stmt_expr (expr, false);
3980               }
3981           }
3982         else
3983           {
3984             /* Parse the parenthesized expression.  */
3985             expr = cp_parser_expression (parser, cast_p, idk);
3986             /* Let the front end know that this expression was
3987                enclosed in parentheses. This matters in case, for
3988                example, the expression is of the form `A::B', since
3989                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3990                not.  */
3991             finish_parenthesized_expr (expr);
3992             /* DR 705: Wrapping an unqualified name in parentheses
3993                suppresses arg-dependent lookup.  We want to pass back
3994                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3995                (c++/37862), but none of the others.  */
3996             if (*idk != CP_ID_KIND_QUALIFIED)
3997               *idk = CP_ID_KIND_NONE;
3998           }
3999         /* The `>' token might be the end of a template-id or
4000            template-parameter-list now.  */
4001         parser->greater_than_is_operator_p
4002           = saved_greater_than_is_operator_p;
4003         /* Consume the `)'.  */
4004         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4005           cp_parser_skip_to_end_of_statement (parser);
4006
4007         return expr;
4008       }
4009
4010     case CPP_OPEN_SQUARE:
4011       if (c_dialect_objc ())
4012         /* We have an Objective-C++ message. */
4013         return cp_parser_objc_expression (parser);
4014       {
4015         tree lam = cp_parser_lambda_expression (parser);
4016         /* Don't warn about a failed tentative parse.  */
4017         if (cp_parser_error_occurred (parser))
4018           return error_mark_node;
4019         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4020         return lam;
4021       }
4022
4023     case CPP_OBJC_STRING:
4024       if (c_dialect_objc ())
4025         /* We have an Objective-C++ string literal. */
4026         return cp_parser_objc_expression (parser);
4027       cp_parser_error (parser, "expected primary-expression");
4028       return error_mark_node;
4029
4030     case CPP_KEYWORD:
4031       switch (token->keyword)
4032         {
4033           /* These two are the boolean literals.  */
4034         case RID_TRUE:
4035           cp_lexer_consume_token (parser->lexer);
4036           return boolean_true_node;
4037         case RID_FALSE:
4038           cp_lexer_consume_token (parser->lexer);
4039           return boolean_false_node;
4040
4041           /* The `__null' literal.  */
4042         case RID_NULL:
4043           cp_lexer_consume_token (parser->lexer);
4044           return null_node;
4045
4046           /* The `nullptr' literal.  */
4047         case RID_NULLPTR:
4048           cp_lexer_consume_token (parser->lexer);
4049           return nullptr_node;
4050
4051           /* Recognize the `this' keyword.  */
4052         case RID_THIS:
4053           cp_lexer_consume_token (parser->lexer);
4054           if (parser->local_variables_forbidden_p)
4055             {
4056               error_at (token->location,
4057                         "%<this%> may not be used in this context");
4058               return error_mark_node;
4059             }
4060           /* Pointers cannot appear in constant-expressions.  */
4061           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4062             return error_mark_node;
4063           return finish_this_expr ();
4064
4065           /* The `operator' keyword can be the beginning of an
4066              id-expression.  */
4067         case RID_OPERATOR:
4068           goto id_expression;
4069
4070         case RID_FUNCTION_NAME:
4071         case RID_PRETTY_FUNCTION_NAME:
4072         case RID_C99_FUNCTION_NAME:
4073           {
4074             non_integral_constant name;
4075
4076             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4077                __func__ are the names of variables -- but they are
4078                treated specially.  Therefore, they are handled here,
4079                rather than relying on the generic id-expression logic
4080                below.  Grammatically, these names are id-expressions.
4081
4082                Consume the token.  */
4083             token = cp_lexer_consume_token (parser->lexer);
4084
4085             switch (token->keyword)
4086               {
4087               case RID_FUNCTION_NAME:
4088                 name = NIC_FUNC_NAME;
4089                 break;
4090               case RID_PRETTY_FUNCTION_NAME:
4091                 name = NIC_PRETTY_FUNC;
4092                 break;
4093               case RID_C99_FUNCTION_NAME:
4094                 name = NIC_C99_FUNC;
4095                 break;
4096               default:
4097                 gcc_unreachable ();
4098               }
4099
4100             if (cp_parser_non_integral_constant_expression (parser, name))
4101               return error_mark_node;
4102
4103             /* Look up the name.  */
4104             return finish_fname (token->u.value);
4105           }
4106
4107         case RID_VA_ARG:
4108           {
4109             tree expression;
4110             tree type;
4111
4112             /* The `__builtin_va_arg' construct is used to handle
4113                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4114             cp_lexer_consume_token (parser->lexer);
4115             /* Look for the opening `('.  */
4116             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4117             /* Now, parse the assignment-expression.  */
4118             expression = cp_parser_assignment_expression (parser,
4119                                                           /*cast_p=*/false, NULL);
4120             /* Look for the `,'.  */
4121             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4122             /* Parse the type-id.  */
4123             type = cp_parser_type_id (parser);
4124             /* Look for the closing `)'.  */
4125             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4126             /* Using `va_arg' in a constant-expression is not
4127                allowed.  */
4128             if (cp_parser_non_integral_constant_expression (parser,
4129                                                             NIC_VA_ARG))
4130               return error_mark_node;
4131             return build_x_va_arg (expression, type);
4132           }
4133
4134         case RID_OFFSETOF:
4135           return cp_parser_builtin_offsetof (parser);
4136
4137         case RID_HAS_NOTHROW_ASSIGN:
4138         case RID_HAS_NOTHROW_CONSTRUCTOR:
4139         case RID_HAS_NOTHROW_COPY:        
4140         case RID_HAS_TRIVIAL_ASSIGN:
4141         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4142         case RID_HAS_TRIVIAL_COPY:        
4143         case RID_HAS_TRIVIAL_DESTRUCTOR:
4144         case RID_HAS_VIRTUAL_DESTRUCTOR:
4145         case RID_IS_ABSTRACT:
4146         case RID_IS_BASE_OF:
4147         case RID_IS_CLASS:
4148         case RID_IS_CONVERTIBLE_TO:
4149         case RID_IS_EMPTY:
4150         case RID_IS_ENUM:
4151         case RID_IS_LITERAL_TYPE:
4152         case RID_IS_POD:
4153         case RID_IS_POLYMORPHIC:
4154         case RID_IS_STD_LAYOUT:
4155         case RID_IS_TRIVIAL:
4156         case RID_IS_UNION:
4157           return cp_parser_trait_expr (parser, token->keyword);
4158
4159         /* Objective-C++ expressions.  */
4160         case RID_AT_ENCODE:
4161         case RID_AT_PROTOCOL:
4162         case RID_AT_SELECTOR:
4163           return cp_parser_objc_expression (parser);
4164
4165         case RID_TEMPLATE:
4166           if (parser->in_function_body
4167               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4168                   == CPP_LESS))
4169             {
4170               error_at (token->location,
4171                         "a template declaration cannot appear at block scope");
4172               cp_parser_skip_to_end_of_block_or_statement (parser);
4173               return error_mark_node;
4174             }
4175         default:
4176           cp_parser_error (parser, "expected primary-expression");
4177           return error_mark_node;
4178         }
4179
4180       /* An id-expression can start with either an identifier, a
4181          `::' as the beginning of a qualified-id, or the "operator"
4182          keyword.  */
4183     case CPP_NAME:
4184     case CPP_SCOPE:
4185     case CPP_TEMPLATE_ID:
4186     case CPP_NESTED_NAME_SPECIFIER:
4187       {
4188         tree id_expression;
4189         tree decl;
4190         const char *error_msg;
4191         bool template_p;
4192         bool done;
4193         cp_token *id_expr_token;
4194
4195       id_expression:
4196         /* Parse the id-expression.  */
4197         id_expression
4198           = cp_parser_id_expression (parser,
4199                                      /*template_keyword_p=*/false,
4200                                      /*check_dependency_p=*/true,
4201                                      &template_p,
4202                                      /*declarator_p=*/false,
4203                                      /*optional_p=*/false);
4204         if (id_expression == error_mark_node)
4205           return error_mark_node;
4206         id_expr_token = token;
4207         token = cp_lexer_peek_token (parser->lexer);
4208         done = (token->type != CPP_OPEN_SQUARE
4209                 && token->type != CPP_OPEN_PAREN
4210                 && token->type != CPP_DOT
4211                 && token->type != CPP_DEREF
4212                 && token->type != CPP_PLUS_PLUS
4213                 && token->type != CPP_MINUS_MINUS);
4214         /* If we have a template-id, then no further lookup is
4215            required.  If the template-id was for a template-class, we
4216            will sometimes have a TYPE_DECL at this point.  */
4217         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4218                  || TREE_CODE (id_expression) == TYPE_DECL)
4219           decl = id_expression;
4220         /* Look up the name.  */
4221         else
4222           {
4223             tree ambiguous_decls;
4224
4225             /* If we already know that this lookup is ambiguous, then
4226                we've already issued an error message; there's no reason
4227                to check again.  */
4228             if (id_expr_token->type == CPP_NAME
4229                 && id_expr_token->ambiguous_p)
4230               {
4231                 cp_parser_simulate_error (parser);
4232                 return error_mark_node;
4233               }
4234
4235             decl = cp_parser_lookup_name (parser, id_expression,
4236                                           none_type,
4237                                           template_p,
4238                                           /*is_namespace=*/false,
4239                                           /*check_dependency=*/true,
4240                                           &ambiguous_decls,
4241                                           id_expr_token->location);
4242             /* If the lookup was ambiguous, an error will already have
4243                been issued.  */
4244             if (ambiguous_decls)
4245               return error_mark_node;
4246
4247             /* In Objective-C++, we may have an Objective-C 2.0
4248                dot-syntax for classes here.  */
4249             if (c_dialect_objc ()
4250                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4251                 && TREE_CODE (decl) == TYPE_DECL
4252                 && objc_is_class_name (decl))
4253               {
4254                 tree component;
4255                 cp_lexer_consume_token (parser->lexer);
4256                 component = cp_parser_identifier (parser);
4257                 if (component == error_mark_node)
4258                   return error_mark_node;
4259
4260                 return objc_build_class_component_ref (id_expression, component);
4261               }
4262
4263             /* In Objective-C++, an instance variable (ivar) may be preferred
4264                to whatever cp_parser_lookup_name() found.  */
4265             decl = objc_lookup_ivar (decl, id_expression);
4266
4267             /* If name lookup gives us a SCOPE_REF, then the
4268                qualifying scope was dependent.  */
4269             if (TREE_CODE (decl) == SCOPE_REF)
4270               {
4271                 /* At this point, we do not know if DECL is a valid
4272                    integral constant expression.  We assume that it is
4273                    in fact such an expression, so that code like:
4274
4275                       template <int N> struct A {
4276                         int a[B<N>::i];
4277                       };
4278                      
4279                    is accepted.  At template-instantiation time, we
4280                    will check that B<N>::i is actually a constant.  */
4281                 return decl;
4282               }
4283             /* Check to see if DECL is a local variable in a context
4284                where that is forbidden.  */
4285             if (parser->local_variables_forbidden_p
4286                 && local_variable_p (decl))
4287               {
4288                 /* It might be that we only found DECL because we are
4289                    trying to be generous with pre-ISO scoping rules.
4290                    For example, consider:
4291
4292                      int i;
4293                      void g() {
4294                        for (int i = 0; i < 10; ++i) {}
4295                        extern void f(int j = i);
4296                      }
4297
4298                    Here, name look up will originally find the out
4299                    of scope `i'.  We need to issue a warning message,
4300                    but then use the global `i'.  */
4301                 decl = check_for_out_of_scope_variable (decl);
4302                 if (local_variable_p (decl))
4303                   {
4304                     error_at (id_expr_token->location,
4305                               "local variable %qD may not appear in this context",
4306                               decl);
4307                     return error_mark_node;
4308                   }
4309               }
4310           }
4311
4312         decl = (finish_id_expression
4313                 (id_expression, decl, parser->scope,
4314                  idk,
4315                  parser->integral_constant_expression_p,
4316                  parser->allow_non_integral_constant_expression_p,
4317                  &parser->non_integral_constant_expression_p,
4318                  template_p, done, address_p,
4319                  template_arg_p,
4320                  &error_msg,
4321                  id_expr_token->location));
4322         if (error_msg)
4323           cp_parser_error (parser, error_msg);
4324         return decl;
4325       }
4326
4327       /* Anything else is an error.  */
4328     default:
4329       cp_parser_error (parser, "expected primary-expression");
4330       return error_mark_node;
4331     }
4332 }
4333
4334 /* Parse an id-expression.
4335
4336    id-expression:
4337      unqualified-id
4338      qualified-id
4339
4340    qualified-id:
4341      :: [opt] nested-name-specifier template [opt] unqualified-id
4342      :: identifier
4343      :: operator-function-id
4344      :: template-id
4345
4346    Return a representation of the unqualified portion of the
4347    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4348    a `::' or nested-name-specifier.
4349
4350    Often, if the id-expression was a qualified-id, the caller will
4351    want to make a SCOPE_REF to represent the qualified-id.  This
4352    function does not do this in order to avoid wastefully creating
4353    SCOPE_REFs when they are not required.
4354
4355    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4356    `template' keyword.
4357
4358    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4359    uninstantiated templates.
4360
4361    If *TEMPLATE_P is non-NULL, it is set to true iff the
4362    `template' keyword is used to explicitly indicate that the entity
4363    named is a template.
4364
4365    If DECLARATOR_P is true, the id-expression is appearing as part of
4366    a declarator, rather than as part of an expression.  */
4367
4368 static tree
4369 cp_parser_id_expression (cp_parser *parser,
4370                          bool template_keyword_p,
4371                          bool check_dependency_p,
4372                          bool *template_p,
4373                          bool declarator_p,
4374                          bool optional_p)
4375 {
4376   bool global_scope_p;
4377   bool nested_name_specifier_p;
4378
4379   /* Assume the `template' keyword was not used.  */
4380   if (template_p)
4381     *template_p = template_keyword_p;
4382
4383   /* Look for the optional `::' operator.  */
4384   global_scope_p
4385     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4386        != NULL_TREE);
4387   /* Look for the optional nested-name-specifier.  */
4388   nested_name_specifier_p
4389     = (cp_parser_nested_name_specifier_opt (parser,
4390                                             /*typename_keyword_p=*/false,
4391                                             check_dependency_p,
4392                                             /*type_p=*/false,
4393                                             declarator_p)
4394        != NULL_TREE);
4395   /* If there is a nested-name-specifier, then we are looking at
4396      the first qualified-id production.  */
4397   if (nested_name_specifier_p)
4398     {
4399       tree saved_scope;
4400       tree saved_object_scope;
4401       tree saved_qualifying_scope;
4402       tree unqualified_id;
4403       bool is_template;
4404
4405       /* See if the next token is the `template' keyword.  */
4406       if (!template_p)
4407         template_p = &is_template;
4408       *template_p = cp_parser_optional_template_keyword (parser);
4409       /* Name lookup we do during the processing of the
4410          unqualified-id might obliterate SCOPE.  */
4411       saved_scope = parser->scope;
4412       saved_object_scope = parser->object_scope;
4413       saved_qualifying_scope = parser->qualifying_scope;
4414       /* Process the final unqualified-id.  */
4415       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4416                                                  check_dependency_p,
4417                                                  declarator_p,
4418                                                  /*optional_p=*/false);
4419       /* Restore the SAVED_SCOPE for our caller.  */
4420       parser->scope = saved_scope;
4421       parser->object_scope = saved_object_scope;
4422       parser->qualifying_scope = saved_qualifying_scope;
4423
4424       return unqualified_id;
4425     }
4426   /* Otherwise, if we are in global scope, then we are looking at one
4427      of the other qualified-id productions.  */
4428   else if (global_scope_p)
4429     {
4430       cp_token *token;
4431       tree id;
4432
4433       /* Peek at the next token.  */
4434       token = cp_lexer_peek_token (parser->lexer);
4435
4436       /* If it's an identifier, and the next token is not a "<", then
4437          we can avoid the template-id case.  This is an optimization
4438          for this common case.  */
4439       if (token->type == CPP_NAME
4440           && !cp_parser_nth_token_starts_template_argument_list_p
4441                (parser, 2))
4442         return cp_parser_identifier (parser);
4443
4444       cp_parser_parse_tentatively (parser);
4445       /* Try a template-id.  */
4446       id = cp_parser_template_id (parser,
4447                                   /*template_keyword_p=*/false,
4448                                   /*check_dependency_p=*/true,
4449                                   declarator_p);
4450       /* If that worked, we're done.  */
4451       if (cp_parser_parse_definitely (parser))
4452         return id;
4453
4454       /* Peek at the next token.  (Changes in the token buffer may
4455          have invalidated the pointer obtained above.)  */
4456       token = cp_lexer_peek_token (parser->lexer);
4457
4458       switch (token->type)
4459         {
4460         case CPP_NAME:
4461           return cp_parser_identifier (parser);
4462
4463         case CPP_KEYWORD:
4464           if (token->keyword == RID_OPERATOR)
4465             return cp_parser_operator_function_id (parser);
4466           /* Fall through.  */
4467
4468         default:
4469           cp_parser_error (parser, "expected id-expression");
4470           return error_mark_node;
4471         }
4472     }
4473   else
4474     return cp_parser_unqualified_id (parser, template_keyword_p,
4475                                      /*check_dependency_p=*/true,
4476                                      declarator_p,
4477                                      optional_p);
4478 }
4479
4480 /* Parse an unqualified-id.
4481
4482    unqualified-id:
4483      identifier
4484      operator-function-id
4485      conversion-function-id
4486      ~ class-name
4487      template-id
4488
4489    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4490    keyword, in a construct like `A::template ...'.
4491
4492    Returns a representation of unqualified-id.  For the `identifier'
4493    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4494    production a BIT_NOT_EXPR is returned; the operand of the
4495    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4496    other productions, see the documentation accompanying the
4497    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4498    names are looked up in uninstantiated templates.  If DECLARATOR_P
4499    is true, the unqualified-id is appearing as part of a declarator,
4500    rather than as part of an expression.  */
4501
4502 static tree
4503 cp_parser_unqualified_id (cp_parser* parser,
4504                           bool template_keyword_p,
4505                           bool check_dependency_p,
4506                           bool declarator_p,
4507                           bool optional_p)
4508 {
4509   cp_token *token;
4510
4511   /* Peek at the next token.  */
4512   token = cp_lexer_peek_token (parser->lexer);
4513
4514   switch (token->type)
4515     {
4516     case CPP_NAME:
4517       {
4518         tree id;
4519
4520         /* We don't know yet whether or not this will be a
4521            template-id.  */
4522         cp_parser_parse_tentatively (parser);
4523         /* Try a template-id.  */
4524         id = cp_parser_template_id (parser, template_keyword_p,
4525                                     check_dependency_p,
4526                                     declarator_p);
4527         /* If it worked, we're done.  */
4528         if (cp_parser_parse_definitely (parser))
4529           return id;
4530         /* Otherwise, it's an ordinary identifier.  */
4531         return cp_parser_identifier (parser);
4532       }
4533
4534     case CPP_TEMPLATE_ID:
4535       return cp_parser_template_id (parser, template_keyword_p,
4536                                     check_dependency_p,
4537                                     declarator_p);
4538
4539     case CPP_COMPL:
4540       {
4541         tree type_decl;
4542         tree qualifying_scope;
4543         tree object_scope;
4544         tree scope;
4545         bool done;
4546
4547         /* Consume the `~' token.  */
4548         cp_lexer_consume_token (parser->lexer);
4549         /* Parse the class-name.  The standard, as written, seems to
4550            say that:
4551
4552              template <typename T> struct S { ~S (); };
4553              template <typename T> S<T>::~S() {}
4554
4555            is invalid, since `~' must be followed by a class-name, but
4556            `S<T>' is dependent, and so not known to be a class.
4557            That's not right; we need to look in uninstantiated
4558            templates.  A further complication arises from:
4559
4560              template <typename T> void f(T t) {
4561                t.T::~T();
4562              }
4563
4564            Here, it is not possible to look up `T' in the scope of `T'
4565            itself.  We must look in both the current scope, and the
4566            scope of the containing complete expression.
4567
4568            Yet another issue is:
4569
4570              struct S {
4571                int S;
4572                ~S();
4573              };
4574
4575              S::~S() {}
4576
4577            The standard does not seem to say that the `S' in `~S'
4578            should refer to the type `S' and not the data member
4579            `S::S'.  */
4580
4581         /* DR 244 says that we look up the name after the "~" in the
4582            same scope as we looked up the qualifying name.  That idea
4583            isn't fully worked out; it's more complicated than that.  */
4584         scope = parser->scope;
4585         object_scope = parser->object_scope;
4586         qualifying_scope = parser->qualifying_scope;
4587
4588         /* Check for invalid scopes.  */
4589         if (scope == error_mark_node)
4590           {
4591             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4592               cp_lexer_consume_token (parser->lexer);
4593             return error_mark_node;
4594           }
4595         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4596           {
4597             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4598               error_at (token->location,
4599                         "scope %qT before %<~%> is not a class-name",
4600                         scope);
4601             cp_parser_simulate_error (parser);
4602             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4603               cp_lexer_consume_token (parser->lexer);
4604             return error_mark_node;
4605           }
4606         gcc_assert (!scope || TYPE_P (scope));
4607
4608         /* If the name is of the form "X::~X" it's OK even if X is a
4609            typedef.  */
4610         token = cp_lexer_peek_token (parser->lexer);
4611         if (scope
4612             && token->type == CPP_NAME
4613             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4614                 != CPP_LESS)
4615             && (token->u.value == TYPE_IDENTIFIER (scope)
4616                 || (CLASS_TYPE_P (scope)
4617                     && constructor_name_p (token->u.value, scope))))
4618           {
4619             cp_lexer_consume_token (parser->lexer);
4620             return build_nt (BIT_NOT_EXPR, scope);
4621           }
4622
4623         /* If there was an explicit qualification (S::~T), first look
4624            in the scope given by the qualification (i.e., S).
4625
4626            Note: in the calls to cp_parser_class_name below we pass
4627            typename_type so that lookup finds the injected-class-name
4628            rather than the constructor.  */
4629         done = false;
4630         type_decl = NULL_TREE;
4631         if (scope)
4632           {
4633             cp_parser_parse_tentatively (parser);
4634             type_decl = cp_parser_class_name (parser,
4635                                               /*typename_keyword_p=*/false,
4636                                               /*template_keyword_p=*/false,
4637                                               typename_type,
4638                                               /*check_dependency=*/false,
4639                                               /*class_head_p=*/false,
4640                                               declarator_p);
4641             if (cp_parser_parse_definitely (parser))
4642               done = true;
4643           }
4644         /* In "N::S::~S", look in "N" as well.  */
4645         if (!done && scope && qualifying_scope)
4646           {
4647             cp_parser_parse_tentatively (parser);
4648             parser->scope = qualifying_scope;
4649             parser->object_scope = NULL_TREE;
4650             parser->qualifying_scope = NULL_TREE;
4651             type_decl
4652               = cp_parser_class_name (parser,
4653                                       /*typename_keyword_p=*/false,
4654                                       /*template_keyword_p=*/false,
4655                                       typename_type,
4656                                       /*check_dependency=*/false,
4657                                       /*class_head_p=*/false,
4658                                       declarator_p);
4659             if (cp_parser_parse_definitely (parser))
4660               done = true;
4661           }
4662         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4663         else if (!done && object_scope)
4664           {
4665             cp_parser_parse_tentatively (parser);
4666             parser->scope = object_scope;
4667             parser->object_scope = NULL_TREE;
4668             parser->qualifying_scope = NULL_TREE;
4669             type_decl
4670               = cp_parser_class_name (parser,
4671                                       /*typename_keyword_p=*/false,
4672                                       /*template_keyword_p=*/false,
4673                                       typename_type,
4674                                       /*check_dependency=*/false,
4675                                       /*class_head_p=*/false,
4676                                       declarator_p);
4677             if (cp_parser_parse_definitely (parser))
4678               done = true;
4679           }
4680         /* Look in the surrounding context.  */
4681         if (!done)
4682           {
4683             parser->scope = NULL_TREE;
4684             parser->object_scope = NULL_TREE;
4685             parser->qualifying_scope = NULL_TREE;
4686             if (processing_template_decl)
4687               cp_parser_parse_tentatively (parser);
4688             type_decl
4689               = cp_parser_class_name (parser,
4690                                       /*typename_keyword_p=*/false,
4691                                       /*template_keyword_p=*/false,
4692                                       typename_type,
4693                                       /*check_dependency=*/false,
4694                                       /*class_head_p=*/false,
4695                                       declarator_p);
4696             if (processing_template_decl
4697                 && ! cp_parser_parse_definitely (parser))
4698               {
4699                 /* We couldn't find a type with this name, so just accept
4700                    it and check for a match at instantiation time.  */
4701                 type_decl = cp_parser_identifier (parser);
4702                 if (type_decl != error_mark_node)
4703                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4704                 return type_decl;
4705               }
4706           }
4707         /* If an error occurred, assume that the name of the
4708            destructor is the same as the name of the qualifying
4709            class.  That allows us to keep parsing after running
4710            into ill-formed destructor names.  */
4711         if (type_decl == error_mark_node && scope)
4712           return build_nt (BIT_NOT_EXPR, scope);
4713         else if (type_decl == error_mark_node)
4714           return error_mark_node;
4715
4716         /* Check that destructor name and scope match.  */
4717         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4718           {
4719             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4720               error_at (token->location,
4721                         "declaration of %<~%T%> as member of %qT",
4722                         type_decl, scope);
4723             cp_parser_simulate_error (parser);
4724             return error_mark_node;
4725           }
4726
4727         /* [class.dtor]
4728
4729            A typedef-name that names a class shall not be used as the
4730            identifier in the declarator for a destructor declaration.  */
4731         if (declarator_p
4732             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4733             && !DECL_SELF_REFERENCE_P (type_decl)
4734             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4735           error_at (token->location,
4736                     "typedef-name %qD used as destructor declarator",
4737                     type_decl);
4738
4739         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4740       }
4741
4742     case CPP_KEYWORD:
4743       if (token->keyword == RID_OPERATOR)
4744         {
4745           tree id;
4746
4747           /* This could be a template-id, so we try that first.  */
4748           cp_parser_parse_tentatively (parser);
4749           /* Try a template-id.  */
4750           id = cp_parser_template_id (parser, template_keyword_p,
4751                                       /*check_dependency_p=*/true,
4752                                       declarator_p);
4753           /* If that worked, we're done.  */
4754           if (cp_parser_parse_definitely (parser))
4755             return id;
4756           /* We still don't know whether we're looking at an
4757              operator-function-id or a conversion-function-id.  */
4758           cp_parser_parse_tentatively (parser);
4759           /* Try an operator-function-id.  */
4760           id = cp_parser_operator_function_id (parser);
4761           /* If that didn't work, try a conversion-function-id.  */
4762           if (!cp_parser_parse_definitely (parser))
4763             id = cp_parser_conversion_function_id (parser);
4764           else if (UDLIT_OPER_P (id))
4765             {
4766               /* 17.6.3.3.5  */
4767               const char *name = UDLIT_OP_SUFFIX (id);
4768               if (name[0] != '_' && !in_system_header)
4769                 warning (0, "literal operator suffixes not preceded by %<_%>"
4770                             " are reserved for future standardization");
4771             }
4772
4773           return id;
4774         }
4775       /* Fall through.  */
4776
4777     default:
4778       if (optional_p)
4779         return NULL_TREE;
4780       cp_parser_error (parser, "expected unqualified-id");
4781       return error_mark_node;
4782     }
4783 }
4784
4785 /* Parse an (optional) nested-name-specifier.
4786
4787    nested-name-specifier: [C++98]
4788      class-or-namespace-name :: nested-name-specifier [opt]
4789      class-or-namespace-name :: template nested-name-specifier [opt]
4790
4791    nested-name-specifier: [C++0x]
4792      type-name ::
4793      namespace-name ::
4794      nested-name-specifier identifier ::
4795      nested-name-specifier template [opt] simple-template-id ::
4796
4797    PARSER->SCOPE should be set appropriately before this function is
4798    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4799    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4800    in name lookups.
4801
4802    Sets PARSER->SCOPE to the class (TYPE) or namespace
4803    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4804    it unchanged if there is no nested-name-specifier.  Returns the new
4805    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4806
4807    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4808    part of a declaration and/or decl-specifier.  */
4809
4810 static tree
4811 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4812                                      bool typename_keyword_p,
4813                                      bool check_dependency_p,
4814                                      bool type_p,
4815                                      bool is_declaration)
4816 {
4817   bool success = false;
4818   cp_token_position start = 0;
4819   cp_token *token;
4820
4821   /* Remember where the nested-name-specifier starts.  */
4822   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4823     {
4824       start = cp_lexer_token_position (parser->lexer, false);
4825       push_deferring_access_checks (dk_deferred);
4826     }
4827
4828   while (true)
4829     {
4830       tree new_scope;
4831       tree old_scope;
4832       tree saved_qualifying_scope;
4833       bool template_keyword_p;
4834
4835       /* Spot cases that cannot be the beginning of a
4836          nested-name-specifier.  */
4837       token = cp_lexer_peek_token (parser->lexer);
4838
4839       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4840          the already parsed nested-name-specifier.  */
4841       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4842         {
4843           /* Grab the nested-name-specifier and continue the loop.  */
4844           cp_parser_pre_parsed_nested_name_specifier (parser);
4845           /* If we originally encountered this nested-name-specifier
4846              with IS_DECLARATION set to false, we will not have
4847              resolved TYPENAME_TYPEs, so we must do so here.  */
4848           if (is_declaration
4849               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4850             {
4851               new_scope = resolve_typename_type (parser->scope,
4852                                                  /*only_current_p=*/false);
4853               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4854                 parser->scope = new_scope;
4855             }
4856           success = true;
4857           continue;
4858         }
4859
4860       /* Spot cases that cannot be the beginning of a
4861          nested-name-specifier.  On the second and subsequent times
4862          through the loop, we look for the `template' keyword.  */
4863       if (success && token->keyword == RID_TEMPLATE)
4864         ;
4865       /* A template-id can start a nested-name-specifier.  */
4866       else if (token->type == CPP_TEMPLATE_ID)
4867         ;
4868       /* DR 743: decltype can be used in a nested-name-specifier.  */
4869       else if (token_is_decltype (token))
4870         ;
4871       else
4872         {
4873           /* If the next token is not an identifier, then it is
4874              definitely not a type-name or namespace-name.  */
4875           if (token->type != CPP_NAME)
4876             break;
4877           /* If the following token is neither a `<' (to begin a
4878              template-id), nor a `::', then we are not looking at a
4879              nested-name-specifier.  */
4880           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4881
4882           if (token->type == CPP_COLON
4883               && parser->colon_corrects_to_scope_p
4884               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4885             {
4886               error_at (token->location,
4887                         "found %<:%> in nested-name-specifier, expected %<::%>");
4888               token->type = CPP_SCOPE;
4889             }
4890
4891           if (token->type != CPP_SCOPE
4892               && !cp_parser_nth_token_starts_template_argument_list_p
4893                   (parser, 2))
4894             break;
4895         }
4896
4897       /* The nested-name-specifier is optional, so we parse
4898          tentatively.  */
4899       cp_parser_parse_tentatively (parser);
4900
4901       /* Look for the optional `template' keyword, if this isn't the
4902          first time through the loop.  */
4903       if (success)
4904         template_keyword_p = cp_parser_optional_template_keyword (parser);
4905       else
4906         template_keyword_p = false;
4907
4908       /* Save the old scope since the name lookup we are about to do
4909          might destroy it.  */
4910       old_scope = parser->scope;
4911       saved_qualifying_scope = parser->qualifying_scope;
4912       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4913          look up names in "X<T>::I" in order to determine that "Y" is
4914          a template.  So, if we have a typename at this point, we make
4915          an effort to look through it.  */
4916       if (is_declaration
4917           && !typename_keyword_p
4918           && parser->scope
4919           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4920         parser->scope = resolve_typename_type (parser->scope,
4921                                                /*only_current_p=*/false);
4922       /* Parse the qualifying entity.  */
4923       new_scope
4924         = cp_parser_qualifying_entity (parser,
4925                                        typename_keyword_p,
4926                                        template_keyword_p,
4927                                        check_dependency_p,
4928                                        type_p,
4929                                        is_declaration);
4930       /* Look for the `::' token.  */
4931       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4932
4933       /* If we found what we wanted, we keep going; otherwise, we're
4934          done.  */
4935       if (!cp_parser_parse_definitely (parser))
4936         {
4937           bool error_p = false;
4938
4939           /* Restore the OLD_SCOPE since it was valid before the
4940              failed attempt at finding the last
4941              class-or-namespace-name.  */
4942           parser->scope = old_scope;
4943           parser->qualifying_scope = saved_qualifying_scope;
4944
4945           /* If the next token is a decltype, and the one after that is a
4946              `::', then the decltype has failed to resolve to a class or
4947              enumeration type.  Give this error even when parsing
4948              tentatively since it can't possibly be valid--and we're going
4949              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4950              won't get another chance.*/
4951           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4952               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4953                   == CPP_SCOPE))
4954             {
4955               token = cp_lexer_consume_token (parser->lexer);
4956               error_at (token->location, "decltype evaluates to %qT, "
4957                         "which is not a class or enumeration type",
4958                         token->u.value);
4959               parser->scope = error_mark_node;
4960               error_p = true;
4961               /* As below.  */
4962               success = true;
4963               cp_lexer_consume_token (parser->lexer);
4964             }
4965
4966           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4967             break;
4968           /* If the next token is an identifier, and the one after
4969              that is a `::', then any valid interpretation would have
4970              found a class-or-namespace-name.  */
4971           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4972                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4973                      == CPP_SCOPE)
4974                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4975                      != CPP_COMPL))
4976             {
4977               token = cp_lexer_consume_token (parser->lexer);
4978               if (!error_p)
4979                 {
4980                   if (!token->ambiguous_p)
4981                     {
4982                       tree decl;
4983                       tree ambiguous_decls;
4984
4985                       decl = cp_parser_lookup_name (parser, token->u.value,
4986                                                     none_type,
4987                                                     /*is_template=*/false,
4988                                                     /*is_namespace=*/false,
4989                                                     /*check_dependency=*/true,
4990                                                     &ambiguous_decls,
4991                                                     token->location);
4992                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4993                         error_at (token->location,
4994                                   "%qD used without template parameters",
4995                                   decl);
4996                       else if (ambiguous_decls)
4997                         {
4998                           error_at (token->location,
4999                                     "reference to %qD is ambiguous",
5000                                     token->u.value);
5001                           print_candidates (ambiguous_decls);
5002                           decl = error_mark_node;
5003                         }
5004                       else
5005                         {
5006                           if (cxx_dialect != cxx98)
5007                             cp_parser_name_lookup_error
5008                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5009                              token->location);
5010                           else
5011                             cp_parser_name_lookup_error
5012                             (parser, token->u.value, decl, NLE_CXX98,
5013                              token->location);
5014                         }
5015                     }
5016                   parser->scope = error_mark_node;
5017                   error_p = true;
5018                   /* Treat this as a successful nested-name-specifier
5019                      due to:
5020
5021                      [basic.lookup.qual]
5022
5023                      If the name found is not a class-name (clause
5024                      _class_) or namespace-name (_namespace.def_), the
5025                      program is ill-formed.  */
5026                   success = true;
5027                 }
5028               cp_lexer_consume_token (parser->lexer);
5029             }
5030           break;
5031         }
5032       /* We've found one valid nested-name-specifier.  */
5033       success = true;
5034       /* Name lookup always gives us a DECL.  */
5035       if (TREE_CODE (new_scope) == TYPE_DECL)
5036         new_scope = TREE_TYPE (new_scope);
5037       /* Uses of "template" must be followed by actual templates.  */
5038       if (template_keyword_p
5039           && !(CLASS_TYPE_P (new_scope)
5040                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5041                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5042                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5043           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5044                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5045                    == TEMPLATE_ID_EXPR)))
5046         permerror (input_location, TYPE_P (new_scope)
5047                    ? G_("%qT is not a template")
5048                    : G_("%qD is not a template"),
5049                    new_scope);
5050       /* If it is a class scope, try to complete it; we are about to
5051          be looking up names inside the class.  */
5052       if (TYPE_P (new_scope)
5053           /* Since checking types for dependency can be expensive,
5054              avoid doing it if the type is already complete.  */
5055           && !COMPLETE_TYPE_P (new_scope)
5056           /* Do not try to complete dependent types.  */
5057           && !dependent_type_p (new_scope))
5058         {
5059           new_scope = complete_type (new_scope);
5060           /* If it is a typedef to current class, use the current
5061              class instead, as the typedef won't have any names inside
5062              it yet.  */
5063           if (!COMPLETE_TYPE_P (new_scope)
5064               && currently_open_class (new_scope))
5065             new_scope = TYPE_MAIN_VARIANT (new_scope);
5066         }
5067       /* Make sure we look in the right scope the next time through
5068          the loop.  */
5069       parser->scope = new_scope;
5070     }
5071
5072   /* If parsing tentatively, replace the sequence of tokens that makes
5073      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5074      token.  That way, should we re-parse the token stream, we will
5075      not have to repeat the effort required to do the parse, nor will
5076      we issue duplicate error messages.  */
5077   if (success && start)
5078     {
5079       cp_token *token;
5080
5081       token = cp_lexer_token_at (parser->lexer, start);
5082       /* Reset the contents of the START token.  */
5083       token->type = CPP_NESTED_NAME_SPECIFIER;
5084       /* Retrieve any deferred checks.  Do not pop this access checks yet
5085          so the memory will not be reclaimed during token replacing below.  */
5086       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5087       token->u.tree_check_value->value = parser->scope;
5088       token->u.tree_check_value->checks = get_deferred_access_checks ();
5089       token->u.tree_check_value->qualifying_scope =
5090         parser->qualifying_scope;
5091       token->keyword = RID_MAX;
5092
5093       /* Purge all subsequent tokens.  */
5094       cp_lexer_purge_tokens_after (parser->lexer, start);
5095     }
5096
5097   if (start)
5098     pop_to_parent_deferring_access_checks ();
5099
5100   return success ? parser->scope : NULL_TREE;
5101 }
5102
5103 /* Parse a nested-name-specifier.  See
5104    cp_parser_nested_name_specifier_opt for details.  This function
5105    behaves identically, except that it will an issue an error if no
5106    nested-name-specifier is present.  */
5107
5108 static tree
5109 cp_parser_nested_name_specifier (cp_parser *parser,
5110                                  bool typename_keyword_p,
5111                                  bool check_dependency_p,
5112                                  bool type_p,
5113                                  bool is_declaration)
5114 {
5115   tree scope;
5116
5117   /* Look for the nested-name-specifier.  */
5118   scope = cp_parser_nested_name_specifier_opt (parser,
5119                                                typename_keyword_p,
5120                                                check_dependency_p,
5121                                                type_p,
5122                                                is_declaration);
5123   /* If it was not present, issue an error message.  */
5124   if (!scope)
5125     {
5126       cp_parser_error (parser, "expected nested-name-specifier");
5127       parser->scope = NULL_TREE;
5128     }
5129
5130   return scope;
5131 }
5132
5133 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5134    this is either a class-name or a namespace-name (which corresponds
5135    to the class-or-namespace-name production in the grammar). For
5136    C++0x, it can also be a type-name that refers to an enumeration
5137    type.
5138
5139    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5140    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5141    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5142    TYPE_P is TRUE iff the next name should be taken as a class-name,
5143    even the same name is declared to be another entity in the same
5144    scope.
5145
5146    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5147    specified by the class-or-namespace-name.  If neither is found the
5148    ERROR_MARK_NODE is returned.  */
5149
5150 static tree
5151 cp_parser_qualifying_entity (cp_parser *parser,
5152                              bool typename_keyword_p,
5153                              bool template_keyword_p,
5154                              bool check_dependency_p,
5155                              bool type_p,
5156                              bool is_declaration)
5157 {
5158   tree saved_scope;
5159   tree saved_qualifying_scope;
5160   tree saved_object_scope;
5161   tree scope;
5162   bool only_class_p;
5163   bool successful_parse_p;
5164
5165   /* DR 743: decltype can appear in a nested-name-specifier.  */
5166   if (cp_lexer_next_token_is_decltype (parser->lexer))
5167     {
5168       scope = cp_parser_decltype (parser);
5169       if (TREE_CODE (scope) != ENUMERAL_TYPE
5170           && !MAYBE_CLASS_TYPE_P (scope))
5171         {
5172           cp_parser_simulate_error (parser);
5173           return error_mark_node;
5174         }
5175       if (TYPE_NAME (scope))
5176         scope = TYPE_NAME (scope);
5177       return scope;
5178     }
5179
5180   /* Before we try to parse the class-name, we must save away the
5181      current PARSER->SCOPE since cp_parser_class_name will destroy
5182      it.  */
5183   saved_scope = parser->scope;
5184   saved_qualifying_scope = parser->qualifying_scope;
5185   saved_object_scope = parser->object_scope;
5186   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5187      there is no need to look for a namespace-name.  */
5188   only_class_p = template_keyword_p 
5189     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5190   if (!only_class_p)
5191     cp_parser_parse_tentatively (parser);
5192   scope = cp_parser_class_name (parser,
5193                                 typename_keyword_p,
5194                                 template_keyword_p,
5195                                 type_p ? class_type : none_type,
5196                                 check_dependency_p,
5197                                 /*class_head_p=*/false,
5198                                 is_declaration);
5199   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5200   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5201   if (!only_class_p 
5202       && cxx_dialect != cxx98
5203       && !successful_parse_p)
5204     {
5205       /* Restore the saved scope.  */
5206       parser->scope = saved_scope;
5207       parser->qualifying_scope = saved_qualifying_scope;
5208       parser->object_scope = saved_object_scope;
5209
5210       /* Parse tentatively.  */
5211       cp_parser_parse_tentatively (parser);
5212      
5213       /* Parse a typedef-name or enum-name.  */
5214       scope = cp_parser_nonclass_name (parser);
5215
5216       /* "If the name found does not designate a namespace or a class,
5217          enumeration, or dependent type, the program is ill-formed."
5218
5219          We cover classes and dependent types above and namespaces below,
5220          so this code is only looking for enums.  */
5221       if (!scope || TREE_CODE (scope) != TYPE_DECL
5222           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5223         cp_parser_simulate_error (parser);
5224
5225       successful_parse_p = cp_parser_parse_definitely (parser);
5226     }
5227   /* If that didn't work, try for a namespace-name.  */
5228   if (!only_class_p && !successful_parse_p)
5229     {
5230       /* Restore the saved scope.  */
5231       parser->scope = saved_scope;
5232       parser->qualifying_scope = saved_qualifying_scope;
5233       parser->object_scope = saved_object_scope;
5234       /* If we are not looking at an identifier followed by the scope
5235          resolution operator, then this is not part of a
5236          nested-name-specifier.  (Note that this function is only used
5237          to parse the components of a nested-name-specifier.)  */
5238       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5239           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5240         return error_mark_node;
5241       scope = cp_parser_namespace_name (parser);
5242     }
5243
5244   return scope;
5245 }
5246
5247 /* Parse a postfix-expression.
5248
5249    postfix-expression:
5250      primary-expression
5251      postfix-expression [ expression ]
5252      postfix-expression ( expression-list [opt] )
5253      simple-type-specifier ( expression-list [opt] )
5254      typename :: [opt] nested-name-specifier identifier
5255        ( expression-list [opt] )
5256      typename :: [opt] nested-name-specifier template [opt] template-id
5257        ( expression-list [opt] )
5258      postfix-expression . template [opt] id-expression
5259      postfix-expression -> template [opt] id-expression
5260      postfix-expression . pseudo-destructor-name
5261      postfix-expression -> pseudo-destructor-name
5262      postfix-expression ++
5263      postfix-expression --
5264      dynamic_cast < type-id > ( expression )
5265      static_cast < type-id > ( expression )
5266      reinterpret_cast < type-id > ( expression )
5267      const_cast < type-id > ( expression )
5268      typeid ( expression )
5269      typeid ( type-id )
5270
5271    GNU Extension:
5272
5273    postfix-expression:
5274      ( type-id ) { initializer-list , [opt] }
5275
5276    This extension is a GNU version of the C99 compound-literal
5277    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5278    but they are essentially the same concept.)
5279
5280    If ADDRESS_P is true, the postfix expression is the operand of the
5281    `&' operator.  CAST_P is true if this expression is the target of a
5282    cast.
5283
5284    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5285    class member access expressions [expr.ref].
5286
5287    Returns a representation of the expression.  */
5288
5289 static tree
5290 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5291                               bool member_access_only_p,
5292                               cp_id_kind * pidk_return)
5293 {
5294   cp_token *token;
5295   enum rid keyword;
5296   cp_id_kind idk = CP_ID_KIND_NONE;
5297   tree postfix_expression = NULL_TREE;
5298   bool is_member_access = false;
5299
5300   /* Peek at the next token.  */
5301   token = cp_lexer_peek_token (parser->lexer);
5302   /* Some of the productions are determined by keywords.  */
5303   keyword = token->keyword;
5304   switch (keyword)
5305     {
5306     case RID_DYNCAST:
5307     case RID_STATCAST:
5308     case RID_REINTCAST:
5309     case RID_CONSTCAST:
5310       {
5311         tree type;
5312         tree expression;
5313         const char *saved_message;
5314
5315         /* All of these can be handled in the same way from the point
5316            of view of parsing.  Begin by consuming the token
5317            identifying the cast.  */
5318         cp_lexer_consume_token (parser->lexer);
5319
5320         /* New types cannot be defined in the cast.  */
5321         saved_message = parser->type_definition_forbidden_message;
5322         parser->type_definition_forbidden_message
5323           = G_("types may not be defined in casts");
5324
5325         /* Look for the opening `<'.  */
5326         cp_parser_require (parser, CPP_LESS, RT_LESS);
5327         /* Parse the type to which we are casting.  */
5328         type = cp_parser_type_id (parser);
5329         /* Look for the closing `>'.  */
5330         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5331         /* Restore the old message.  */
5332         parser->type_definition_forbidden_message = saved_message;
5333
5334         /* And the expression which is being cast.  */
5335         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5336         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5337         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5338
5339         /* Only type conversions to integral or enumeration types
5340            can be used in constant-expressions.  */
5341         if (!cast_valid_in_integral_constant_expression_p (type)
5342             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5343           return error_mark_node;
5344
5345         switch (keyword)
5346           {
5347           case RID_DYNCAST:
5348             postfix_expression
5349               = build_dynamic_cast (type, expression, tf_warning_or_error);
5350             break;
5351           case RID_STATCAST:
5352             postfix_expression
5353               = build_static_cast (type, expression, tf_warning_or_error);
5354             break;
5355           case RID_REINTCAST:
5356             postfix_expression
5357               = build_reinterpret_cast (type, expression, 
5358                                         tf_warning_or_error);
5359             break;
5360           case RID_CONSTCAST:
5361             postfix_expression
5362               = build_const_cast (type, expression, tf_warning_or_error);
5363             break;
5364           default:
5365             gcc_unreachable ();
5366           }
5367       }
5368       break;
5369
5370     case RID_TYPEID:
5371       {
5372         tree type;
5373         const char *saved_message;
5374         bool saved_in_type_id_in_expr_p;
5375
5376         /* Consume the `typeid' token.  */
5377         cp_lexer_consume_token (parser->lexer);
5378         /* Look for the `(' token.  */
5379         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5380         /* Types cannot be defined in a `typeid' expression.  */
5381         saved_message = parser->type_definition_forbidden_message;
5382         parser->type_definition_forbidden_message
5383           = G_("types may not be defined in a %<typeid%> expression");
5384         /* We can't be sure yet whether we're looking at a type-id or an
5385            expression.  */
5386         cp_parser_parse_tentatively (parser);
5387         /* Try a type-id first.  */
5388         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5389         parser->in_type_id_in_expr_p = true;
5390         type = cp_parser_type_id (parser);
5391         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5392         /* Look for the `)' token.  Otherwise, we can't be sure that
5393            we're not looking at an expression: consider `typeid (int
5394            (3))', for example.  */
5395         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5396         /* If all went well, simply lookup the type-id.  */
5397         if (cp_parser_parse_definitely (parser))
5398           postfix_expression = get_typeid (type);
5399         /* Otherwise, fall back to the expression variant.  */
5400         else
5401           {
5402             tree expression;
5403
5404             /* Look for an expression.  */
5405             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5406             /* Compute its typeid.  */
5407             postfix_expression = build_typeid (expression);
5408             /* Look for the `)' token.  */
5409             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5410           }
5411         /* Restore the saved message.  */
5412         parser->type_definition_forbidden_message = saved_message;
5413         /* `typeid' may not appear in an integral constant expression.  */
5414         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5415           return error_mark_node;
5416       }
5417       break;
5418
5419     case RID_TYPENAME:
5420       {
5421         tree type;
5422         /* The syntax permitted here is the same permitted for an
5423            elaborated-type-specifier.  */
5424         type = cp_parser_elaborated_type_specifier (parser,
5425                                                     /*is_friend=*/false,
5426                                                     /*is_declaration=*/false);
5427         postfix_expression = cp_parser_functional_cast (parser, type);
5428       }
5429       break;
5430
5431     default:
5432       {
5433         tree type;
5434
5435         /* If the next thing is a simple-type-specifier, we may be
5436            looking at a functional cast.  We could also be looking at
5437            an id-expression.  So, we try the functional cast, and if
5438            that doesn't work we fall back to the primary-expression.  */
5439         cp_parser_parse_tentatively (parser);
5440         /* Look for the simple-type-specifier.  */
5441         type = cp_parser_simple_type_specifier (parser,
5442                                                 /*decl_specs=*/NULL,
5443                                                 CP_PARSER_FLAGS_NONE);
5444         /* Parse the cast itself.  */
5445         if (!cp_parser_error_occurred (parser))
5446           postfix_expression
5447             = cp_parser_functional_cast (parser, type);
5448         /* If that worked, we're done.  */
5449         if (cp_parser_parse_definitely (parser))
5450           break;
5451
5452         /* If the functional-cast didn't work out, try a
5453            compound-literal.  */
5454         if (cp_parser_allow_gnu_extensions_p (parser)
5455             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5456           {
5457             VEC(constructor_elt,gc) *initializer_list = NULL;
5458             bool saved_in_type_id_in_expr_p;
5459
5460             cp_parser_parse_tentatively (parser);
5461             /* Consume the `('.  */
5462             cp_lexer_consume_token (parser->lexer);
5463             /* Parse the type.  */
5464             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5465             parser->in_type_id_in_expr_p = true;
5466             type = cp_parser_type_id (parser);
5467             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5468             /* Look for the `)'.  */
5469             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5470             /* Look for the `{'.  */
5471             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5472             /* If things aren't going well, there's no need to
5473                keep going.  */
5474             if (!cp_parser_error_occurred (parser))
5475               {
5476                 bool non_constant_p;
5477                 /* Parse the initializer-list.  */
5478                 initializer_list
5479                   = cp_parser_initializer_list (parser, &non_constant_p);
5480                 /* Allow a trailing `,'.  */
5481                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5482                   cp_lexer_consume_token (parser->lexer);
5483                 /* Look for the final `}'.  */
5484                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5485               }
5486             /* If that worked, we're definitely looking at a
5487                compound-literal expression.  */
5488             if (cp_parser_parse_definitely (parser))
5489               {
5490                 /* Warn the user that a compound literal is not
5491                    allowed in standard C++.  */
5492                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5493                 /* For simplicity, we disallow compound literals in
5494                    constant-expressions.  We could
5495                    allow compound literals of integer type, whose
5496                    initializer was a constant, in constant
5497                    expressions.  Permitting that usage, as a further
5498                    extension, would not change the meaning of any
5499                    currently accepted programs.  (Of course, as
5500                    compound literals are not part of ISO C++, the
5501                    standard has nothing to say.)  */
5502                 if (cp_parser_non_integral_constant_expression (parser,
5503                                                                 NIC_NCC))
5504                   {
5505                     postfix_expression = error_mark_node;
5506                     break;
5507                   }
5508                 /* Form the representation of the compound-literal.  */
5509                 postfix_expression
5510                   = (finish_compound_literal
5511                      (type, build_constructor (init_list_type_node,
5512                                                initializer_list),
5513                       tf_warning_or_error));
5514                 break;
5515               }
5516           }
5517
5518         /* It must be a primary-expression.  */
5519         postfix_expression
5520           = cp_parser_primary_expression (parser, address_p, cast_p,
5521                                           /*template_arg_p=*/false,
5522                                           &idk);
5523       }
5524       break;
5525     }
5526
5527   /* Keep looping until the postfix-expression is complete.  */
5528   while (true)
5529     {
5530       if (idk == CP_ID_KIND_UNQUALIFIED
5531           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5532           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5533         /* It is not a Koenig lookup function call.  */
5534         postfix_expression
5535           = unqualified_name_lookup_error (postfix_expression);
5536
5537       /* Peek at the next token.  */
5538       token = cp_lexer_peek_token (parser->lexer);
5539
5540       switch (token->type)
5541         {
5542         case CPP_OPEN_SQUARE:
5543           postfix_expression
5544             = cp_parser_postfix_open_square_expression (parser,
5545                                                         postfix_expression,
5546                                                         false);
5547           idk = CP_ID_KIND_NONE;
5548           is_member_access = false;
5549           break;
5550
5551         case CPP_OPEN_PAREN:
5552           /* postfix-expression ( expression-list [opt] ) */
5553           {
5554             bool koenig_p;
5555             bool is_builtin_constant_p;
5556             bool saved_integral_constant_expression_p = false;
5557             bool saved_non_integral_constant_expression_p = false;
5558             VEC(tree,gc) *args;
5559
5560             is_member_access = false;
5561
5562             is_builtin_constant_p
5563               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5564             if (is_builtin_constant_p)
5565               {
5566                 /* The whole point of __builtin_constant_p is to allow
5567                    non-constant expressions to appear as arguments.  */
5568                 saved_integral_constant_expression_p
5569                   = parser->integral_constant_expression_p;
5570                 saved_non_integral_constant_expression_p
5571                   = parser->non_integral_constant_expression_p;
5572                 parser->integral_constant_expression_p = false;
5573               }
5574             args = (cp_parser_parenthesized_expression_list
5575                     (parser, non_attr,
5576                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5577                      /*non_constant_p=*/NULL));
5578             if (is_builtin_constant_p)
5579               {
5580                 parser->integral_constant_expression_p
5581                   = saved_integral_constant_expression_p;
5582                 parser->non_integral_constant_expression_p
5583                   = saved_non_integral_constant_expression_p;
5584               }
5585
5586             if (args == NULL)
5587               {
5588                 postfix_expression = error_mark_node;
5589                 break;
5590               }
5591
5592             /* Function calls are not permitted in
5593                constant-expressions.  */
5594             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5595                 && cp_parser_non_integral_constant_expression (parser,
5596                                                                NIC_FUNC_CALL))
5597               {
5598                 postfix_expression = error_mark_node;
5599                 release_tree_vector (args);
5600                 break;
5601               }
5602
5603             koenig_p = false;
5604             if (idk == CP_ID_KIND_UNQUALIFIED
5605                 || idk == CP_ID_KIND_TEMPLATE_ID)
5606               {
5607                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5608                   {
5609                     if (!VEC_empty (tree, args))
5610                       {
5611                         koenig_p = true;
5612                         if (!any_type_dependent_arguments_p (args))
5613                           postfix_expression
5614                             = perform_koenig_lookup (postfix_expression, args,
5615                                                      /*include_std=*/false,
5616                                                      tf_warning_or_error);
5617                       }
5618                     else
5619                       postfix_expression
5620                         = unqualified_fn_lookup_error (postfix_expression);
5621                   }
5622                 /* We do not perform argument-dependent lookup if
5623                    normal lookup finds a non-function, in accordance
5624                    with the expected resolution of DR 218.  */
5625                 else if (!VEC_empty (tree, args)
5626                          && is_overloaded_fn (postfix_expression))
5627                   {
5628                     tree fn = get_first_fn (postfix_expression);
5629                     fn = STRIP_TEMPLATE (fn);
5630
5631                     /* Do not do argument dependent lookup if regular
5632                        lookup finds a member function or a block-scope
5633                        function declaration.  [basic.lookup.argdep]/3  */
5634                     if (!DECL_FUNCTION_MEMBER_P (fn)
5635                         && !DECL_LOCAL_FUNCTION_P (fn))
5636                       {
5637                         koenig_p = true;
5638                         if (!any_type_dependent_arguments_p (args))
5639                           postfix_expression
5640                             = perform_koenig_lookup (postfix_expression, args,
5641                                                      /*include_std=*/false,
5642                                                      tf_warning_or_error);
5643                       }
5644                   }
5645               }
5646
5647             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5648               {
5649                 tree instance = TREE_OPERAND (postfix_expression, 0);
5650                 tree fn = TREE_OPERAND (postfix_expression, 1);
5651
5652                 if (processing_template_decl
5653                     && (type_dependent_expression_p (instance)
5654                         || (!BASELINK_P (fn)
5655                             && TREE_CODE (fn) != FIELD_DECL)
5656                         || type_dependent_expression_p (fn)
5657                         || any_type_dependent_arguments_p (args)))
5658                   {
5659                     postfix_expression
5660                       = build_nt_call_vec (postfix_expression, args);
5661                     release_tree_vector (args);
5662                     break;
5663                   }
5664
5665                 if (BASELINK_P (fn))
5666                   {
5667                   postfix_expression
5668                     = (build_new_method_call
5669                        (instance, fn, &args, NULL_TREE,
5670                         (idk == CP_ID_KIND_QUALIFIED
5671                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5672                          : LOOKUP_NORMAL),
5673                         /*fn_p=*/NULL,
5674                         tf_warning_or_error));
5675                   }
5676                 else
5677                   postfix_expression
5678                     = finish_call_expr (postfix_expression, &args,
5679                                         /*disallow_virtual=*/false,
5680                                         /*koenig_p=*/false,
5681                                         tf_warning_or_error);
5682               }
5683             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5684                      || TREE_CODE (postfix_expression) == MEMBER_REF
5685                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5686               postfix_expression = (build_offset_ref_call_from_tree
5687                                     (postfix_expression, &args));
5688             else if (idk == CP_ID_KIND_QUALIFIED)
5689               /* A call to a static class member, or a namespace-scope
5690                  function.  */
5691               postfix_expression
5692                 = finish_call_expr (postfix_expression, &args,
5693                                     /*disallow_virtual=*/true,
5694                                     koenig_p,
5695                                     tf_warning_or_error);
5696             else
5697               /* All other function calls.  */
5698               postfix_expression
5699                 = finish_call_expr (postfix_expression, &args,
5700                                     /*disallow_virtual=*/false,
5701                                     koenig_p,
5702                                     tf_warning_or_error);
5703
5704             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5705             idk = CP_ID_KIND_NONE;
5706
5707             release_tree_vector (args);
5708           }
5709           break;
5710
5711         case CPP_DOT:
5712         case CPP_DEREF:
5713           /* postfix-expression . template [opt] id-expression
5714              postfix-expression . pseudo-destructor-name
5715              postfix-expression -> template [opt] id-expression
5716              postfix-expression -> pseudo-destructor-name */
5717
5718           /* Consume the `.' or `->' operator.  */
5719           cp_lexer_consume_token (parser->lexer);
5720
5721           postfix_expression
5722             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5723                                                       postfix_expression,
5724                                                       false, &idk,
5725                                                       token->location);
5726
5727           is_member_access = true;
5728           break;
5729
5730         case CPP_PLUS_PLUS:
5731           /* postfix-expression ++  */
5732           /* Consume the `++' token.  */
5733           cp_lexer_consume_token (parser->lexer);
5734           /* Generate a representation for the complete expression.  */
5735           postfix_expression
5736             = finish_increment_expr (postfix_expression,
5737                                      POSTINCREMENT_EXPR);
5738           /* Increments may not appear in constant-expressions.  */
5739           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5740             postfix_expression = error_mark_node;
5741           idk = CP_ID_KIND_NONE;
5742           is_member_access = false;
5743           break;
5744
5745         case CPP_MINUS_MINUS:
5746           /* postfix-expression -- */
5747           /* Consume the `--' token.  */
5748           cp_lexer_consume_token (parser->lexer);
5749           /* Generate a representation for the complete expression.  */
5750           postfix_expression
5751             = finish_increment_expr (postfix_expression,
5752                                      POSTDECREMENT_EXPR);
5753           /* Decrements may not appear in constant-expressions.  */
5754           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5755             postfix_expression = error_mark_node;
5756           idk = CP_ID_KIND_NONE;
5757           is_member_access = false;
5758           break;
5759
5760         default:
5761           if (pidk_return != NULL)
5762             * pidk_return = idk;
5763           if (member_access_only_p)
5764             return is_member_access? postfix_expression : error_mark_node;
5765           else
5766             return postfix_expression;
5767         }
5768     }
5769
5770   /* We should never get here.  */
5771   gcc_unreachable ();
5772   return error_mark_node;
5773 }
5774
5775 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5776    by cp_parser_builtin_offsetof.  We're looking for
5777
5778      postfix-expression [ expression ]
5779
5780    FOR_OFFSETOF is set if we're being called in that context, which
5781    changes how we deal with integer constant expressions.  */
5782
5783 static tree
5784 cp_parser_postfix_open_square_expression (cp_parser *parser,
5785                                           tree postfix_expression,
5786                                           bool for_offsetof)
5787 {
5788   tree index;
5789
5790   /* Consume the `[' token.  */
5791   cp_lexer_consume_token (parser->lexer);
5792
5793   /* Parse the index expression.  */
5794   /* ??? For offsetof, there is a question of what to allow here.  If
5795      offsetof is not being used in an integral constant expression context,
5796      then we *could* get the right answer by computing the value at runtime.
5797      If we are in an integral constant expression context, then we might
5798      could accept any constant expression; hard to say without analysis.
5799      Rather than open the barn door too wide right away, allow only integer
5800      constant expressions here.  */
5801   if (for_offsetof)
5802     index = cp_parser_constant_expression (parser, false, NULL);
5803   else
5804     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5805
5806   /* Look for the closing `]'.  */
5807   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5808
5809   /* Build the ARRAY_REF.  */
5810   postfix_expression = grok_array_decl (postfix_expression, index);
5811
5812   /* When not doing offsetof, array references are not permitted in
5813      constant-expressions.  */
5814   if (!for_offsetof
5815       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5816     postfix_expression = error_mark_node;
5817
5818   return postfix_expression;
5819 }
5820
5821 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5822    by cp_parser_builtin_offsetof.  We're looking for
5823
5824      postfix-expression . template [opt] id-expression
5825      postfix-expression . pseudo-destructor-name
5826      postfix-expression -> template [opt] id-expression
5827      postfix-expression -> pseudo-destructor-name
5828
5829    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5830    limits what of the above we'll actually accept, but nevermind.
5831    TOKEN_TYPE is the "." or "->" token, which will already have been
5832    removed from the stream.  */
5833
5834 static tree
5835 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5836                                         enum cpp_ttype token_type,
5837                                         tree postfix_expression,
5838                                         bool for_offsetof, cp_id_kind *idk,
5839                                         location_t location)
5840 {
5841   tree name;
5842   bool dependent_p;
5843   bool pseudo_destructor_p;
5844   tree scope = NULL_TREE;
5845
5846   /* If this is a `->' operator, dereference the pointer.  */
5847   if (token_type == CPP_DEREF)
5848     postfix_expression = build_x_arrow (postfix_expression);
5849   /* Check to see whether or not the expression is type-dependent.  */
5850   dependent_p = type_dependent_expression_p (postfix_expression);
5851   /* The identifier following the `->' or `.' is not qualified.  */
5852   parser->scope = NULL_TREE;
5853   parser->qualifying_scope = NULL_TREE;
5854   parser->object_scope = NULL_TREE;
5855   *idk = CP_ID_KIND_NONE;
5856
5857   /* Enter the scope corresponding to the type of the object
5858      given by the POSTFIX_EXPRESSION.  */
5859   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5860     {
5861       scope = TREE_TYPE (postfix_expression);
5862       /* According to the standard, no expression should ever have
5863          reference type.  Unfortunately, we do not currently match
5864          the standard in this respect in that our internal representation
5865          of an expression may have reference type even when the standard
5866          says it does not.  Therefore, we have to manually obtain the
5867          underlying type here.  */
5868       scope = non_reference (scope);
5869       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5870       if (scope == unknown_type_node)
5871         {
5872           error_at (location, "%qE does not have class type",
5873                     postfix_expression);
5874           scope = NULL_TREE;
5875         }
5876       /* Unlike the object expression in other contexts, *this is not
5877          required to be of complete type for purposes of class member
5878          access (5.2.5) outside the member function body.  */
5879       else if (scope != current_class_ref
5880                && !(processing_template_decl && scope == current_class_type))
5881         scope = complete_type_or_else (scope, NULL_TREE);
5882       /* Let the name lookup machinery know that we are processing a
5883          class member access expression.  */
5884       parser->context->object_type = scope;
5885       /* If something went wrong, we want to be able to discern that case,
5886          as opposed to the case where there was no SCOPE due to the type
5887          of expression being dependent.  */
5888       if (!scope)
5889         scope = error_mark_node;
5890       /* If the SCOPE was erroneous, make the various semantic analysis
5891          functions exit quickly -- and without issuing additional error
5892          messages.  */
5893       if (scope == error_mark_node)
5894         postfix_expression = error_mark_node;
5895     }
5896
5897   /* Assume this expression is not a pseudo-destructor access.  */
5898   pseudo_destructor_p = false;
5899
5900   /* If the SCOPE is a scalar type, then, if this is a valid program,
5901      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5902      is type dependent, it can be pseudo-destructor-name or something else.
5903      Try to parse it as pseudo-destructor-name first.  */
5904   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5905     {
5906       tree s;
5907       tree type;
5908
5909       cp_parser_parse_tentatively (parser);
5910       /* Parse the pseudo-destructor-name.  */
5911       s = NULL_TREE;
5912       cp_parser_pseudo_destructor_name (parser, &s, &type);
5913       if (dependent_p
5914           && (cp_parser_error_occurred (parser)
5915               || TREE_CODE (type) != TYPE_DECL
5916               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5917         cp_parser_abort_tentative_parse (parser);
5918       else if (cp_parser_parse_definitely (parser))
5919         {
5920           pseudo_destructor_p = true;
5921           postfix_expression
5922             = finish_pseudo_destructor_expr (postfix_expression,
5923                                              s, TREE_TYPE (type));
5924         }
5925     }
5926
5927   if (!pseudo_destructor_p)
5928     {
5929       /* If the SCOPE is not a scalar type, we are looking at an
5930          ordinary class member access expression, rather than a
5931          pseudo-destructor-name.  */
5932       bool template_p;
5933       cp_token *token = cp_lexer_peek_token (parser->lexer);
5934       /* Parse the id-expression.  */
5935       name = (cp_parser_id_expression
5936               (parser,
5937                cp_parser_optional_template_keyword (parser),
5938                /*check_dependency_p=*/true,
5939                &template_p,
5940                /*declarator_p=*/false,
5941                /*optional_p=*/false));
5942       /* In general, build a SCOPE_REF if the member name is qualified.
5943          However, if the name was not dependent and has already been
5944          resolved; there is no need to build the SCOPE_REF.  For example;
5945
5946              struct X { void f(); };
5947              template <typename T> void f(T* t) { t->X::f(); }
5948
5949          Even though "t" is dependent, "X::f" is not and has been resolved
5950          to a BASELINK; there is no need to include scope information.  */
5951
5952       /* But we do need to remember that there was an explicit scope for
5953          virtual function calls.  */
5954       if (parser->scope)
5955         *idk = CP_ID_KIND_QUALIFIED;
5956
5957       /* If the name is a template-id that names a type, we will get a
5958          TYPE_DECL here.  That is invalid code.  */
5959       if (TREE_CODE (name) == TYPE_DECL)
5960         {
5961           error_at (token->location, "invalid use of %qD", name);
5962           postfix_expression = error_mark_node;
5963         }
5964       else
5965         {
5966           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5967             {
5968               name = build_qualified_name (/*type=*/NULL_TREE,
5969                                            parser->scope,
5970                                            name,
5971                                            template_p);
5972               parser->scope = NULL_TREE;
5973               parser->qualifying_scope = NULL_TREE;
5974               parser->object_scope = NULL_TREE;
5975             }
5976           if (scope && name && BASELINK_P (name))
5977             adjust_result_of_qualified_name_lookup
5978               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5979           postfix_expression
5980             = finish_class_member_access_expr (postfix_expression, name,
5981                                                template_p, 
5982                                                tf_warning_or_error);
5983         }
5984     }
5985
5986   /* We no longer need to look up names in the scope of the object on
5987      the left-hand side of the `.' or `->' operator.  */
5988   parser->context->object_type = NULL_TREE;
5989
5990   /* Outside of offsetof, these operators may not appear in
5991      constant-expressions.  */
5992   if (!for_offsetof
5993       && (cp_parser_non_integral_constant_expression
5994           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5995     postfix_expression = error_mark_node;
5996
5997   return postfix_expression;
5998 }
5999
6000 /* Parse a parenthesized expression-list.
6001
6002    expression-list:
6003      assignment-expression
6004      expression-list, assignment-expression
6005
6006    attribute-list:
6007      expression-list
6008      identifier
6009      identifier, expression-list
6010
6011    CAST_P is true if this expression is the target of a cast.
6012
6013    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6014    argument pack.
6015
6016    Returns a vector of trees.  Each element is a representation of an
6017    assignment-expression.  NULL is returned if the ( and or ) are
6018    missing.  An empty, but allocated, vector is returned on no
6019    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6020    if we are parsing an attribute list for an attribute that wants a
6021    plain identifier argument, normal_attr for an attribute that wants
6022    an expression, or non_attr if we aren't parsing an attribute list.  If
6023    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6024    not all of the expressions in the list were constant.  */
6025
6026 static VEC(tree,gc) *
6027 cp_parser_parenthesized_expression_list (cp_parser* parser,
6028                                          int is_attribute_list,
6029                                          bool cast_p,
6030                                          bool allow_expansion_p,
6031                                          bool *non_constant_p)
6032 {
6033   VEC(tree,gc) *expression_list;
6034   bool fold_expr_p = is_attribute_list != non_attr;
6035   tree identifier = NULL_TREE;
6036   bool saved_greater_than_is_operator_p;
6037
6038   /* Assume all the expressions will be constant.  */
6039   if (non_constant_p)
6040     *non_constant_p = false;
6041
6042   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6043     return NULL;
6044
6045   expression_list = make_tree_vector ();
6046
6047   /* Within a parenthesized expression, a `>' token is always
6048      the greater-than operator.  */
6049   saved_greater_than_is_operator_p
6050     = parser->greater_than_is_operator_p;
6051   parser->greater_than_is_operator_p = true;
6052
6053   /* Consume expressions until there are no more.  */
6054   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6055     while (true)
6056       {
6057         tree expr;
6058
6059         /* At the beginning of attribute lists, check to see if the
6060            next token is an identifier.  */
6061         if (is_attribute_list == id_attr
6062             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6063           {
6064             cp_token *token;
6065
6066             /* Consume the identifier.  */
6067             token = cp_lexer_consume_token (parser->lexer);
6068             /* Save the identifier.  */
6069             identifier = token->u.value;
6070           }
6071         else
6072           {
6073             bool expr_non_constant_p;
6074
6075             /* Parse the next assignment-expression.  */
6076             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6077               {
6078                 /* A braced-init-list.  */
6079                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6080                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6081                 if (non_constant_p && expr_non_constant_p)
6082                   *non_constant_p = true;
6083               }
6084             else if (non_constant_p)
6085               {
6086                 expr = (cp_parser_constant_expression
6087                         (parser, /*allow_non_constant_p=*/true,
6088                          &expr_non_constant_p));
6089                 if (expr_non_constant_p)
6090                   *non_constant_p = true;
6091               }
6092             else
6093               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6094
6095             if (fold_expr_p)
6096               expr = fold_non_dependent_expr (expr);
6097
6098             /* If we have an ellipsis, then this is an expression
6099                expansion.  */
6100             if (allow_expansion_p
6101                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6102               {
6103                 /* Consume the `...'.  */
6104                 cp_lexer_consume_token (parser->lexer);
6105
6106                 /* Build the argument pack.  */
6107                 expr = make_pack_expansion (expr);
6108               }
6109
6110              /* Add it to the list.  We add error_mark_node
6111                 expressions to the list, so that we can still tell if
6112                 the correct form for a parenthesized expression-list
6113                 is found. That gives better errors.  */
6114             VEC_safe_push (tree, gc, expression_list, expr);
6115
6116             if (expr == error_mark_node)
6117               goto skip_comma;
6118           }
6119
6120         /* After the first item, attribute lists look the same as
6121            expression lists.  */
6122         is_attribute_list = non_attr;
6123
6124       get_comma:;
6125         /* If the next token isn't a `,', then we are done.  */
6126         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6127           break;
6128
6129         /* Otherwise, consume the `,' and keep going.  */
6130         cp_lexer_consume_token (parser->lexer);
6131       }
6132
6133   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6134     {
6135       int ending;
6136
6137     skip_comma:;
6138       /* We try and resync to an unnested comma, as that will give the
6139          user better diagnostics.  */
6140       ending = cp_parser_skip_to_closing_parenthesis (parser,
6141                                                       /*recovering=*/true,
6142                                                       /*or_comma=*/true,
6143                                                       /*consume_paren=*/true);
6144       if (ending < 0)
6145         goto get_comma;
6146       if (!ending)
6147         {
6148           parser->greater_than_is_operator_p
6149             = saved_greater_than_is_operator_p;
6150           return NULL;
6151         }
6152     }
6153
6154   parser->greater_than_is_operator_p
6155     = saved_greater_than_is_operator_p;
6156
6157   if (identifier)
6158     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6159
6160   return expression_list;
6161 }
6162
6163 /* Parse a pseudo-destructor-name.
6164
6165    pseudo-destructor-name:
6166      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6167      :: [opt] nested-name-specifier template template-id :: ~ type-name
6168      :: [opt] nested-name-specifier [opt] ~ type-name
6169
6170    If either of the first two productions is used, sets *SCOPE to the
6171    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6172    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6173    or ERROR_MARK_NODE if the parse fails.  */
6174
6175 static void
6176 cp_parser_pseudo_destructor_name (cp_parser* parser,
6177                                   tree* scope,
6178                                   tree* type)
6179 {
6180   bool nested_name_specifier_p;
6181
6182   /* Assume that things will not work out.  */
6183   *type = error_mark_node;
6184
6185   /* Look for the optional `::' operator.  */
6186   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6187   /* Look for the optional nested-name-specifier.  */
6188   nested_name_specifier_p
6189     = (cp_parser_nested_name_specifier_opt (parser,
6190                                             /*typename_keyword_p=*/false,
6191                                             /*check_dependency_p=*/true,
6192                                             /*type_p=*/false,
6193                                             /*is_declaration=*/false)
6194        != NULL_TREE);
6195   /* Now, if we saw a nested-name-specifier, we might be doing the
6196      second production.  */
6197   if (nested_name_specifier_p
6198       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6199     {
6200       /* Consume the `template' keyword.  */
6201       cp_lexer_consume_token (parser->lexer);
6202       /* Parse the template-id.  */
6203       cp_parser_template_id (parser,
6204                              /*template_keyword_p=*/true,
6205                              /*check_dependency_p=*/false,
6206                              /*is_declaration=*/true);
6207       /* Look for the `::' token.  */
6208       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6209     }
6210   /* If the next token is not a `~', then there might be some
6211      additional qualification.  */
6212   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6213     {
6214       /* At this point, we're looking for "type-name :: ~".  The type-name
6215          must not be a class-name, since this is a pseudo-destructor.  So,
6216          it must be either an enum-name, or a typedef-name -- both of which
6217          are just identifiers.  So, we peek ahead to check that the "::"
6218          and "~" tokens are present; if they are not, then we can avoid
6219          calling type_name.  */
6220       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6221           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6222           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6223         {
6224           cp_parser_error (parser, "non-scalar type");
6225           return;
6226         }
6227
6228       /* Look for the type-name.  */
6229       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6230       if (*scope == error_mark_node)
6231         return;
6232
6233       /* Look for the `::' token.  */
6234       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6235     }
6236   else
6237     *scope = NULL_TREE;
6238
6239   /* Look for the `~'.  */
6240   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6241
6242   /* Once we see the ~, this has to be a pseudo-destructor.  */
6243   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6244     cp_parser_commit_to_tentative_parse (parser);
6245
6246   /* Look for the type-name again.  We are not responsible for
6247      checking that it matches the first type-name.  */
6248   *type = cp_parser_nonclass_name (parser);
6249 }
6250
6251 /* Parse a unary-expression.
6252
6253    unary-expression:
6254      postfix-expression
6255      ++ cast-expression
6256      -- cast-expression
6257      unary-operator cast-expression
6258      sizeof unary-expression
6259      sizeof ( type-id )
6260      alignof ( type-id )  [C++0x]
6261      new-expression
6262      delete-expression
6263
6264    GNU Extensions:
6265
6266    unary-expression:
6267      __extension__ cast-expression
6268      __alignof__ unary-expression
6269      __alignof__ ( type-id )
6270      alignof unary-expression  [C++0x]
6271      __real__ cast-expression
6272      __imag__ cast-expression
6273      && identifier
6274
6275    ADDRESS_P is true iff the unary-expression is appearing as the
6276    operand of the `&' operator.   CAST_P is true if this expression is
6277    the target of a cast.
6278
6279    Returns a representation of the expression.  */
6280
6281 static tree
6282 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6283                             cp_id_kind * pidk)
6284 {
6285   cp_token *token;
6286   enum tree_code unary_operator;
6287
6288   /* Peek at the next token.  */
6289   token = cp_lexer_peek_token (parser->lexer);
6290   /* Some keywords give away the kind of expression.  */
6291   if (token->type == CPP_KEYWORD)
6292     {
6293       enum rid keyword = token->keyword;
6294
6295       switch (keyword)
6296         {
6297         case RID_ALIGNOF:
6298         case RID_SIZEOF:
6299           {
6300             tree operand;
6301             enum tree_code op;
6302
6303             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6304             /* Consume the token.  */
6305             cp_lexer_consume_token (parser->lexer);
6306             /* Parse the operand.  */
6307             operand = cp_parser_sizeof_operand (parser, keyword);
6308
6309             if (TYPE_P (operand))
6310               return cxx_sizeof_or_alignof_type (operand, op, true);
6311             else
6312               {
6313                 /* ISO C++ defines alignof only with types, not with
6314                    expressions. So pedwarn if alignof is used with a non-
6315                    type expression. However, __alignof__ is ok.  */
6316                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6317                   pedwarn (token->location, OPT_pedantic,
6318                            "ISO C++ does not allow %<alignof%> "
6319                            "with a non-type");
6320
6321                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6322               }
6323           }
6324
6325         case RID_NEW:
6326           return cp_parser_new_expression (parser);
6327
6328         case RID_DELETE:
6329           return cp_parser_delete_expression (parser);
6330
6331         case RID_EXTENSION:
6332           {
6333             /* The saved value of the PEDANTIC flag.  */
6334             int saved_pedantic;
6335             tree expr;
6336
6337             /* Save away the PEDANTIC flag.  */
6338             cp_parser_extension_opt (parser, &saved_pedantic);
6339             /* Parse the cast-expression.  */
6340             expr = cp_parser_simple_cast_expression (parser);
6341             /* Restore the PEDANTIC flag.  */
6342             pedantic = saved_pedantic;
6343
6344             return expr;
6345           }
6346
6347         case RID_REALPART:
6348         case RID_IMAGPART:
6349           {
6350             tree expression;
6351
6352             /* Consume the `__real__' or `__imag__' token.  */
6353             cp_lexer_consume_token (parser->lexer);
6354             /* Parse the cast-expression.  */
6355             expression = cp_parser_simple_cast_expression (parser);
6356             /* Create the complete representation.  */
6357             return build_x_unary_op ((keyword == RID_REALPART
6358                                       ? REALPART_EXPR : IMAGPART_EXPR),
6359                                      expression,
6360                                      tf_warning_or_error);
6361           }
6362           break;
6363
6364         case RID_NOEXCEPT:
6365           {
6366             tree expr;
6367             const char *saved_message;
6368             bool saved_integral_constant_expression_p;
6369             bool saved_non_integral_constant_expression_p;
6370             bool saved_greater_than_is_operator_p;
6371
6372             cp_lexer_consume_token (parser->lexer);
6373             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6374
6375             saved_message = parser->type_definition_forbidden_message;
6376             parser->type_definition_forbidden_message
6377               = G_("types may not be defined in %<noexcept%> expressions");
6378
6379             saved_integral_constant_expression_p
6380               = parser->integral_constant_expression_p;
6381             saved_non_integral_constant_expression_p
6382               = parser->non_integral_constant_expression_p;
6383             parser->integral_constant_expression_p = false;
6384
6385             saved_greater_than_is_operator_p
6386               = parser->greater_than_is_operator_p;
6387             parser->greater_than_is_operator_p = true;
6388
6389             ++cp_unevaluated_operand;
6390             ++c_inhibit_evaluation_warnings;
6391             expr = cp_parser_expression (parser, false, NULL);
6392             --c_inhibit_evaluation_warnings;
6393             --cp_unevaluated_operand;
6394
6395             parser->greater_than_is_operator_p
6396               = saved_greater_than_is_operator_p;
6397
6398             parser->integral_constant_expression_p
6399               = saved_integral_constant_expression_p;
6400             parser->non_integral_constant_expression_p
6401               = saved_non_integral_constant_expression_p;
6402
6403             parser->type_definition_forbidden_message = saved_message;
6404
6405             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6406             return finish_noexcept_expr (expr, tf_warning_or_error);
6407           }
6408
6409         default:
6410           break;
6411         }
6412     }
6413
6414   /* Look for the `:: new' and `:: delete', which also signal the
6415      beginning of a new-expression, or delete-expression,
6416      respectively.  If the next token is `::', then it might be one of
6417      these.  */
6418   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6419     {
6420       enum rid keyword;
6421
6422       /* See if the token after the `::' is one of the keywords in
6423          which we're interested.  */
6424       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6425       /* If it's `new', we have a new-expression.  */
6426       if (keyword == RID_NEW)
6427         return cp_parser_new_expression (parser);
6428       /* Similarly, for `delete'.  */
6429       else if (keyword == RID_DELETE)
6430         return cp_parser_delete_expression (parser);
6431     }
6432
6433   /* Look for a unary operator.  */
6434   unary_operator = cp_parser_unary_operator (token);
6435   /* The `++' and `--' operators can be handled similarly, even though
6436      they are not technically unary-operators in the grammar.  */
6437   if (unary_operator == ERROR_MARK)
6438     {
6439       if (token->type == CPP_PLUS_PLUS)
6440         unary_operator = PREINCREMENT_EXPR;
6441       else if (token->type == CPP_MINUS_MINUS)
6442         unary_operator = PREDECREMENT_EXPR;
6443       /* Handle the GNU address-of-label extension.  */
6444       else if (cp_parser_allow_gnu_extensions_p (parser)
6445                && token->type == CPP_AND_AND)
6446         {
6447           tree identifier;
6448           tree expression;
6449           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6450
6451           /* Consume the '&&' token.  */
6452           cp_lexer_consume_token (parser->lexer);
6453           /* Look for the identifier.  */
6454           identifier = cp_parser_identifier (parser);
6455           /* Create an expression representing the address.  */
6456           expression = finish_label_address_expr (identifier, loc);
6457           if (cp_parser_non_integral_constant_expression (parser,
6458                                                           NIC_ADDR_LABEL))
6459             expression = error_mark_node;
6460           return expression;
6461         }
6462     }
6463   if (unary_operator != ERROR_MARK)
6464     {
6465       tree cast_expression;
6466       tree expression = error_mark_node;
6467       non_integral_constant non_constant_p = NIC_NONE;
6468
6469       /* Consume the operator token.  */
6470       token = cp_lexer_consume_token (parser->lexer);
6471       /* Parse the cast-expression.  */
6472       cast_expression
6473         = cp_parser_cast_expression (parser,
6474                                      unary_operator == ADDR_EXPR,
6475                                      /*cast_p=*/false, pidk);
6476       /* Now, build an appropriate representation.  */
6477       switch (unary_operator)
6478         {
6479         case INDIRECT_REF:
6480           non_constant_p = NIC_STAR;
6481           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6482                                              tf_warning_or_error);
6483           break;
6484
6485         case ADDR_EXPR:
6486            non_constant_p = NIC_ADDR;
6487           /* Fall through.  */
6488         case BIT_NOT_EXPR:
6489           expression = build_x_unary_op (unary_operator, cast_expression,
6490                                          tf_warning_or_error);
6491           break;
6492
6493         case PREINCREMENT_EXPR:
6494         case PREDECREMENT_EXPR:
6495           non_constant_p = unary_operator == PREINCREMENT_EXPR
6496                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6497           /* Fall through.  */
6498         case UNARY_PLUS_EXPR:
6499         case NEGATE_EXPR:
6500         case TRUTH_NOT_EXPR:
6501           expression = finish_unary_op_expr (unary_operator, cast_expression);
6502           break;
6503
6504         default:
6505           gcc_unreachable ();
6506         }
6507
6508       if (non_constant_p != NIC_NONE
6509           && cp_parser_non_integral_constant_expression (parser,
6510                                                          non_constant_p))
6511         expression = error_mark_node;
6512
6513       return expression;
6514     }
6515
6516   return cp_parser_postfix_expression (parser, address_p, cast_p,
6517                                        /*member_access_only_p=*/false,
6518                                        pidk);
6519 }
6520
6521 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6522    unary-operator, the corresponding tree code is returned.  */
6523
6524 static enum tree_code
6525 cp_parser_unary_operator (cp_token* token)
6526 {
6527   switch (token->type)
6528     {
6529     case CPP_MULT:
6530       return INDIRECT_REF;
6531
6532     case CPP_AND:
6533       return ADDR_EXPR;
6534
6535     case CPP_PLUS:
6536       return UNARY_PLUS_EXPR;
6537
6538     case CPP_MINUS:
6539       return NEGATE_EXPR;
6540
6541     case CPP_NOT:
6542       return TRUTH_NOT_EXPR;
6543
6544     case CPP_COMPL:
6545       return BIT_NOT_EXPR;
6546
6547     default:
6548       return ERROR_MARK;
6549     }
6550 }
6551
6552 /* Parse a new-expression.
6553
6554    new-expression:
6555      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6556      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6557
6558    Returns a representation of the expression.  */
6559
6560 static tree
6561 cp_parser_new_expression (cp_parser* parser)
6562 {
6563   bool global_scope_p;
6564   VEC(tree,gc) *placement;
6565   tree type;
6566   VEC(tree,gc) *initializer;
6567   tree nelts;
6568   tree ret;
6569
6570   /* Look for the optional `::' operator.  */
6571   global_scope_p
6572     = (cp_parser_global_scope_opt (parser,
6573                                    /*current_scope_valid_p=*/false)
6574        != NULL_TREE);
6575   /* Look for the `new' operator.  */
6576   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6577   /* There's no easy way to tell a new-placement from the
6578      `( type-id )' construct.  */
6579   cp_parser_parse_tentatively (parser);
6580   /* Look for a new-placement.  */
6581   placement = cp_parser_new_placement (parser);
6582   /* If that didn't work out, there's no new-placement.  */
6583   if (!cp_parser_parse_definitely (parser))
6584     {
6585       if (placement != NULL)
6586         release_tree_vector (placement);
6587       placement = NULL;
6588     }
6589
6590   /* If the next token is a `(', then we have a parenthesized
6591      type-id.  */
6592   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6593     {
6594       cp_token *token;
6595       /* Consume the `('.  */
6596       cp_lexer_consume_token (parser->lexer);
6597       /* Parse the type-id.  */
6598       type = cp_parser_type_id (parser);
6599       /* Look for the closing `)'.  */
6600       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6601       token = cp_lexer_peek_token (parser->lexer);
6602       /* There should not be a direct-new-declarator in this production,
6603          but GCC used to allowed this, so we check and emit a sensible error
6604          message for this case.  */
6605       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6606         {
6607           error_at (token->location,
6608                     "array bound forbidden after parenthesized type-id");
6609           inform (token->location, 
6610                   "try removing the parentheses around the type-id");
6611           cp_parser_direct_new_declarator (parser);
6612         }
6613       nelts = NULL_TREE;
6614     }
6615   /* Otherwise, there must be a new-type-id.  */
6616   else
6617     type = cp_parser_new_type_id (parser, &nelts);
6618
6619   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6620   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6621       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6622     initializer = cp_parser_new_initializer (parser);
6623   else
6624     initializer = NULL;
6625
6626   /* A new-expression may not appear in an integral constant
6627      expression.  */
6628   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6629     ret = error_mark_node;
6630   else
6631     {
6632       /* Create a representation of the new-expression.  */
6633       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6634                        tf_warning_or_error);
6635     }
6636
6637   if (placement != NULL)
6638     release_tree_vector (placement);
6639   if (initializer != NULL)
6640     release_tree_vector (initializer);
6641
6642   return ret;
6643 }
6644
6645 /* Parse a new-placement.
6646
6647    new-placement:
6648      ( expression-list )
6649
6650    Returns the same representation as for an expression-list.  */
6651
6652 static VEC(tree,gc) *
6653 cp_parser_new_placement (cp_parser* parser)
6654 {
6655   VEC(tree,gc) *expression_list;
6656
6657   /* Parse the expression-list.  */
6658   expression_list = (cp_parser_parenthesized_expression_list
6659                      (parser, non_attr, /*cast_p=*/false,
6660                       /*allow_expansion_p=*/true,
6661                       /*non_constant_p=*/NULL));
6662
6663   return expression_list;
6664 }
6665
6666 /* Parse a new-type-id.
6667
6668    new-type-id:
6669      type-specifier-seq new-declarator [opt]
6670
6671    Returns the TYPE allocated.  If the new-type-id indicates an array
6672    type, *NELTS is set to the number of elements in the last array
6673    bound; the TYPE will not include the last array bound.  */
6674
6675 static tree
6676 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6677 {
6678   cp_decl_specifier_seq type_specifier_seq;
6679   cp_declarator *new_declarator;
6680   cp_declarator *declarator;
6681   cp_declarator *outer_declarator;
6682   const char *saved_message;
6683   tree type;
6684
6685   /* The type-specifier sequence must not contain type definitions.
6686      (It cannot contain declarations of new types either, but if they
6687      are not definitions we will catch that because they are not
6688      complete.)  */
6689   saved_message = parser->type_definition_forbidden_message;
6690   parser->type_definition_forbidden_message
6691     = G_("types may not be defined in a new-type-id");
6692   /* Parse the type-specifier-seq.  */
6693   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6694                                 /*is_trailing_return=*/false,
6695                                 &type_specifier_seq);
6696   /* Restore the old message.  */
6697   parser->type_definition_forbidden_message = saved_message;
6698   /* Parse the new-declarator.  */
6699   new_declarator = cp_parser_new_declarator_opt (parser);
6700
6701   /* Determine the number of elements in the last array dimension, if
6702      any.  */
6703   *nelts = NULL_TREE;
6704   /* Skip down to the last array dimension.  */
6705   declarator = new_declarator;
6706   outer_declarator = NULL;
6707   while (declarator && (declarator->kind == cdk_pointer
6708                         || declarator->kind == cdk_ptrmem))
6709     {
6710       outer_declarator = declarator;
6711       declarator = declarator->declarator;
6712     }
6713   while (declarator
6714          && declarator->kind == cdk_array
6715          && declarator->declarator
6716          && declarator->declarator->kind == cdk_array)
6717     {
6718       outer_declarator = declarator;
6719       declarator = declarator->declarator;
6720     }
6721
6722   if (declarator && declarator->kind == cdk_array)
6723     {
6724       *nelts = declarator->u.array.bounds;
6725       if (*nelts == error_mark_node)
6726         *nelts = integer_one_node;
6727
6728       if (outer_declarator)
6729         outer_declarator->declarator = declarator->declarator;
6730       else
6731         new_declarator = NULL;
6732     }
6733
6734   type = groktypename (&type_specifier_seq, new_declarator, false);
6735   return type;
6736 }
6737
6738 /* Parse an (optional) new-declarator.
6739
6740    new-declarator:
6741      ptr-operator new-declarator [opt]
6742      direct-new-declarator
6743
6744    Returns the declarator.  */
6745
6746 static cp_declarator *
6747 cp_parser_new_declarator_opt (cp_parser* parser)
6748 {
6749   enum tree_code code;
6750   tree type;
6751   cp_cv_quals cv_quals;
6752
6753   /* We don't know if there's a ptr-operator next, or not.  */
6754   cp_parser_parse_tentatively (parser);
6755   /* Look for a ptr-operator.  */
6756   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6757   /* If that worked, look for more new-declarators.  */
6758   if (cp_parser_parse_definitely (parser))
6759     {
6760       cp_declarator *declarator;
6761
6762       /* Parse another optional declarator.  */
6763       declarator = cp_parser_new_declarator_opt (parser);
6764
6765       return cp_parser_make_indirect_declarator
6766         (code, type, cv_quals, declarator);
6767     }
6768
6769   /* If the next token is a `[', there is a direct-new-declarator.  */
6770   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6771     return cp_parser_direct_new_declarator (parser);
6772
6773   return NULL;
6774 }
6775
6776 /* Parse a direct-new-declarator.
6777
6778    direct-new-declarator:
6779      [ expression ]
6780      direct-new-declarator [constant-expression]
6781
6782    */
6783
6784 static cp_declarator *
6785 cp_parser_direct_new_declarator (cp_parser* parser)
6786 {
6787   cp_declarator *declarator = NULL;
6788
6789   while (true)
6790     {
6791       tree expression;
6792
6793       /* Look for the opening `['.  */
6794       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6795       /* The first expression is not required to be constant.  */
6796       if (!declarator)
6797         {
6798           cp_token *token = cp_lexer_peek_token (parser->lexer);
6799           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6800           /* The standard requires that the expression have integral
6801              type.  DR 74 adds enumeration types.  We believe that the
6802              real intent is that these expressions be handled like the
6803              expression in a `switch' condition, which also allows
6804              classes with a single conversion to integral or
6805              enumeration type.  */
6806           if (!processing_template_decl)
6807             {
6808               expression
6809                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6810                                               expression,
6811                                               /*complain=*/true);
6812               if (!expression)
6813                 {
6814                   error_at (token->location,
6815                             "expression in new-declarator must have integral "
6816                             "or enumeration type");
6817                   expression = error_mark_node;
6818                 }
6819             }
6820         }
6821       /* But all the other expressions must be.  */
6822       else
6823         expression
6824           = cp_parser_constant_expression (parser,
6825                                            /*allow_non_constant=*/false,
6826                                            NULL);
6827       /* Look for the closing `]'.  */
6828       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6829
6830       /* Add this bound to the declarator.  */
6831       declarator = make_array_declarator (declarator, expression);
6832
6833       /* If the next token is not a `[', then there are no more
6834          bounds.  */
6835       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6836         break;
6837     }
6838
6839   return declarator;
6840 }
6841
6842 /* Parse a new-initializer.
6843
6844    new-initializer:
6845      ( expression-list [opt] )
6846      braced-init-list
6847
6848    Returns a representation of the expression-list.  */
6849
6850 static VEC(tree,gc) *
6851 cp_parser_new_initializer (cp_parser* parser)
6852 {
6853   VEC(tree,gc) *expression_list;
6854
6855   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6856     {
6857       tree t;
6858       bool expr_non_constant_p;
6859       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6860       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6861       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6862       expression_list = make_tree_vector_single (t);
6863     }
6864   else
6865     expression_list = (cp_parser_parenthesized_expression_list
6866                        (parser, non_attr, /*cast_p=*/false,
6867                         /*allow_expansion_p=*/true,
6868                         /*non_constant_p=*/NULL));
6869
6870   return expression_list;
6871 }
6872
6873 /* Parse a delete-expression.
6874
6875    delete-expression:
6876      :: [opt] delete cast-expression
6877      :: [opt] delete [ ] cast-expression
6878
6879    Returns a representation of the expression.  */
6880
6881 static tree
6882 cp_parser_delete_expression (cp_parser* parser)
6883 {
6884   bool global_scope_p;
6885   bool array_p;
6886   tree expression;
6887
6888   /* Look for the optional `::' operator.  */
6889   global_scope_p
6890     = (cp_parser_global_scope_opt (parser,
6891                                    /*current_scope_valid_p=*/false)
6892        != NULL_TREE);
6893   /* Look for the `delete' keyword.  */
6894   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6895   /* See if the array syntax is in use.  */
6896   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6897     {
6898       /* Consume the `[' token.  */
6899       cp_lexer_consume_token (parser->lexer);
6900       /* Look for the `]' token.  */
6901       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6902       /* Remember that this is the `[]' construct.  */
6903       array_p = true;
6904     }
6905   else
6906     array_p = false;
6907
6908   /* Parse the cast-expression.  */
6909   expression = cp_parser_simple_cast_expression (parser);
6910
6911   /* A delete-expression may not appear in an integral constant
6912      expression.  */
6913   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6914     return error_mark_node;
6915
6916   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6917                         tf_warning_or_error);
6918 }
6919
6920 /* Returns true if TOKEN may start a cast-expression and false
6921    otherwise.  */
6922
6923 static bool
6924 cp_parser_token_starts_cast_expression (cp_token *token)
6925 {
6926   switch (token->type)
6927     {
6928     case CPP_COMMA:
6929     case CPP_SEMICOLON:
6930     case CPP_QUERY:
6931     case CPP_COLON:
6932     case CPP_CLOSE_SQUARE:
6933     case CPP_CLOSE_PAREN:
6934     case CPP_CLOSE_BRACE:
6935     case CPP_DOT:
6936     case CPP_DOT_STAR:
6937     case CPP_DEREF:
6938     case CPP_DEREF_STAR:
6939     case CPP_DIV:
6940     case CPP_MOD:
6941     case CPP_LSHIFT:
6942     case CPP_RSHIFT:
6943     case CPP_LESS:
6944     case CPP_GREATER:
6945     case CPP_LESS_EQ:
6946     case CPP_GREATER_EQ:
6947     case CPP_EQ_EQ:
6948     case CPP_NOT_EQ:
6949     case CPP_EQ:
6950     case CPP_MULT_EQ:
6951     case CPP_DIV_EQ:
6952     case CPP_MOD_EQ:
6953     case CPP_PLUS_EQ:
6954     case CPP_MINUS_EQ:
6955     case CPP_RSHIFT_EQ:
6956     case CPP_LSHIFT_EQ:
6957     case CPP_AND_EQ:
6958     case CPP_XOR_EQ:
6959     case CPP_OR_EQ:
6960     case CPP_XOR:
6961     case CPP_OR:
6962     case CPP_OR_OR:
6963     case CPP_EOF:
6964       return false;
6965
6966       /* '[' may start a primary-expression in obj-c++.  */
6967     case CPP_OPEN_SQUARE:
6968       return c_dialect_objc ();
6969
6970     default:
6971       return true;
6972     }
6973 }
6974
6975 /* Parse a cast-expression.
6976
6977    cast-expression:
6978      unary-expression
6979      ( type-id ) cast-expression
6980
6981    ADDRESS_P is true iff the unary-expression is appearing as the
6982    operand of the `&' operator.   CAST_P is true if this expression is
6983    the target of a cast.
6984
6985    Returns a representation of the expression.  */
6986
6987 static tree
6988 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6989                            cp_id_kind * pidk)
6990 {
6991   /* If it's a `(', then we might be looking at a cast.  */
6992   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6993     {
6994       tree type = NULL_TREE;
6995       tree expr = NULL_TREE;
6996       bool compound_literal_p;
6997       const char *saved_message;
6998
6999       /* There's no way to know yet whether or not this is a cast.
7000          For example, `(int (3))' is a unary-expression, while `(int)
7001          3' is a cast.  So, we resort to parsing tentatively.  */
7002       cp_parser_parse_tentatively (parser);
7003       /* Types may not be defined in a cast.  */
7004       saved_message = parser->type_definition_forbidden_message;
7005       parser->type_definition_forbidden_message
7006         = G_("types may not be defined in casts");
7007       /* Consume the `('.  */
7008       cp_lexer_consume_token (parser->lexer);
7009       /* A very tricky bit is that `(struct S) { 3 }' is a
7010          compound-literal (which we permit in C++ as an extension).
7011          But, that construct is not a cast-expression -- it is a
7012          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7013          is legal; if the compound-literal were a cast-expression,
7014          you'd need an extra set of parentheses.)  But, if we parse
7015          the type-id, and it happens to be a class-specifier, then we
7016          will commit to the parse at that point, because we cannot
7017          undo the action that is done when creating a new class.  So,
7018          then we cannot back up and do a postfix-expression.
7019
7020          Therefore, we scan ahead to the closing `)', and check to see
7021          if the token after the `)' is a `{'.  If so, we are not
7022          looking at a cast-expression.
7023
7024          Save tokens so that we can put them back.  */
7025       cp_lexer_save_tokens (parser->lexer);
7026       /* Skip tokens until the next token is a closing parenthesis.
7027          If we find the closing `)', and the next token is a `{', then
7028          we are looking at a compound-literal.  */
7029       compound_literal_p
7030         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7031                                                   /*consume_paren=*/true)
7032            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7033       /* Roll back the tokens we skipped.  */
7034       cp_lexer_rollback_tokens (parser->lexer);
7035       /* If we were looking at a compound-literal, simulate an error
7036          so that the call to cp_parser_parse_definitely below will
7037          fail.  */
7038       if (compound_literal_p)
7039         cp_parser_simulate_error (parser);
7040       else
7041         {
7042           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7043           parser->in_type_id_in_expr_p = true;
7044           /* Look for the type-id.  */
7045           type = cp_parser_type_id (parser);
7046           /* Look for the closing `)'.  */
7047           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7048           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7049         }
7050
7051       /* Restore the saved message.  */
7052       parser->type_definition_forbidden_message = saved_message;
7053
7054       /* At this point this can only be either a cast or a
7055          parenthesized ctor such as `(T ())' that looks like a cast to
7056          function returning T.  */
7057       if (!cp_parser_error_occurred (parser)
7058           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7059                                                      (parser->lexer)))
7060         {
7061           cp_parser_parse_definitely (parser);
7062           expr = cp_parser_cast_expression (parser,
7063                                             /*address_p=*/false,
7064                                             /*cast_p=*/true, pidk);
7065
7066           /* Warn about old-style casts, if so requested.  */
7067           if (warn_old_style_cast
7068               && !in_system_header
7069               && !VOID_TYPE_P (type)
7070               && current_lang_name != lang_name_c)
7071             warning (OPT_Wold_style_cast, "use of old-style cast");
7072
7073           /* Only type conversions to integral or enumeration types
7074              can be used in constant-expressions.  */
7075           if (!cast_valid_in_integral_constant_expression_p (type)
7076               && cp_parser_non_integral_constant_expression (parser,
7077                                                              NIC_CAST))
7078             return error_mark_node;
7079
7080           /* Perform the cast.  */
7081           expr = build_c_cast (input_location, type, expr);
7082           return expr;
7083         }
7084       else 
7085         cp_parser_abort_tentative_parse (parser);
7086     }
7087
7088   /* If we get here, then it's not a cast, so it must be a
7089      unary-expression.  */
7090   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7091 }
7092
7093 /* Parse a binary expression of the general form:
7094
7095    pm-expression:
7096      cast-expression
7097      pm-expression .* cast-expression
7098      pm-expression ->* cast-expression
7099
7100    multiplicative-expression:
7101      pm-expression
7102      multiplicative-expression * pm-expression
7103      multiplicative-expression / pm-expression
7104      multiplicative-expression % pm-expression
7105
7106    additive-expression:
7107      multiplicative-expression
7108      additive-expression + multiplicative-expression
7109      additive-expression - multiplicative-expression
7110
7111    shift-expression:
7112      additive-expression
7113      shift-expression << additive-expression
7114      shift-expression >> additive-expression
7115
7116    relational-expression:
7117      shift-expression
7118      relational-expression < shift-expression
7119      relational-expression > shift-expression
7120      relational-expression <= shift-expression
7121      relational-expression >= shift-expression
7122
7123   GNU Extension:
7124
7125    relational-expression:
7126      relational-expression <? shift-expression
7127      relational-expression >? shift-expression
7128
7129    equality-expression:
7130      relational-expression
7131      equality-expression == relational-expression
7132      equality-expression != relational-expression
7133
7134    and-expression:
7135      equality-expression
7136      and-expression & equality-expression
7137
7138    exclusive-or-expression:
7139      and-expression
7140      exclusive-or-expression ^ and-expression
7141
7142    inclusive-or-expression:
7143      exclusive-or-expression
7144      inclusive-or-expression | exclusive-or-expression
7145
7146    logical-and-expression:
7147      inclusive-or-expression
7148      logical-and-expression && inclusive-or-expression
7149
7150    logical-or-expression:
7151      logical-and-expression
7152      logical-or-expression || logical-and-expression
7153
7154    All these are implemented with a single function like:
7155
7156    binary-expression:
7157      simple-cast-expression
7158      binary-expression <token> binary-expression
7159
7160    CAST_P is true if this expression is the target of a cast.
7161
7162    The binops_by_token map is used to get the tree codes for each <token> type.
7163    binary-expressions are associated according to a precedence table.  */
7164
7165 #define TOKEN_PRECEDENCE(token)                              \
7166 (((token->type == CPP_GREATER                                \
7167    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7168   && !parser->greater_than_is_operator_p)                    \
7169  ? PREC_NOT_OPERATOR                                         \
7170  : binops_by_token[token->type].prec)
7171
7172 static tree
7173 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7174                              bool no_toplevel_fold_p,
7175                              enum cp_parser_prec prec,
7176                              cp_id_kind * pidk)
7177 {
7178   cp_parser_expression_stack stack;
7179   cp_parser_expression_stack_entry *sp = &stack[0];
7180   tree lhs, rhs;
7181   cp_token *token;
7182   enum tree_code tree_type, lhs_type, rhs_type;
7183   enum cp_parser_prec new_prec, lookahead_prec;
7184   tree overload;
7185
7186   /* Parse the first expression.  */
7187   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7188   lhs_type = ERROR_MARK;
7189
7190   for (;;)
7191     {
7192       /* Get an operator token.  */
7193       token = cp_lexer_peek_token (parser->lexer);
7194
7195       if (warn_cxx0x_compat
7196           && token->type == CPP_RSHIFT
7197           && !parser->greater_than_is_operator_p)
7198         {
7199           if (warning_at (token->location, OPT_Wc__0x_compat, 
7200                           "%<>>%> operator will be treated as"
7201                           " two right angle brackets in C++0x"))
7202             inform (token->location,
7203                     "suggest parentheses around %<>>%> expression");
7204         }
7205
7206       new_prec = TOKEN_PRECEDENCE (token);
7207
7208       /* Popping an entry off the stack means we completed a subexpression:
7209          - either we found a token which is not an operator (`>' where it is not
7210            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7211            will happen repeatedly;
7212          - or, we found an operator which has lower priority.  This is the case
7213            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7214            parsing `3 * 4'.  */
7215       if (new_prec <= prec)
7216         {
7217           if (sp == stack)
7218             break;
7219           else
7220             goto pop;
7221         }
7222
7223      get_rhs:
7224       tree_type = binops_by_token[token->type].tree_type;
7225
7226       /* We used the operator token.  */
7227       cp_lexer_consume_token (parser->lexer);
7228
7229       /* For "false && x" or "true || x", x will never be executed;
7230          disable warnings while evaluating it.  */
7231       if (tree_type == TRUTH_ANDIF_EXPR)
7232         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7233       else if (tree_type == TRUTH_ORIF_EXPR)
7234         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7235
7236       /* Extract another operand.  It may be the RHS of this expression
7237          or the LHS of a new, higher priority expression.  */
7238       rhs = cp_parser_simple_cast_expression (parser);
7239       rhs_type = ERROR_MARK;
7240
7241       /* Get another operator token.  Look up its precedence to avoid
7242          building a useless (immediately popped) stack entry for common
7243          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7244       token = cp_lexer_peek_token (parser->lexer);
7245       lookahead_prec = TOKEN_PRECEDENCE (token);
7246       if (lookahead_prec > new_prec)
7247         {
7248           /* ... and prepare to parse the RHS of the new, higher priority
7249              expression.  Since precedence levels on the stack are
7250              monotonically increasing, we do not have to care about
7251              stack overflows.  */
7252           sp->prec = prec;
7253           sp->tree_type = tree_type;
7254           sp->lhs = lhs;
7255           sp->lhs_type = lhs_type;
7256           sp++;
7257           lhs = rhs;
7258           lhs_type = rhs_type;
7259           prec = new_prec;
7260           new_prec = lookahead_prec;
7261           goto get_rhs;
7262
7263          pop:
7264           lookahead_prec = new_prec;
7265           /* If the stack is not empty, we have parsed into LHS the right side
7266              (`4' in the example above) of an expression we had suspended.
7267              We can use the information on the stack to recover the LHS (`3')
7268              from the stack together with the tree code (`MULT_EXPR'), and
7269              the precedence of the higher level subexpression
7270              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7271              which will be used to actually build the additive expression.  */
7272           --sp;
7273           prec = sp->prec;
7274           tree_type = sp->tree_type;
7275           rhs = lhs;
7276           rhs_type = lhs_type;
7277           lhs = sp->lhs;
7278           lhs_type = sp->lhs_type;
7279         }
7280
7281       /* Undo the disabling of warnings done above.  */
7282       if (tree_type == TRUTH_ANDIF_EXPR)
7283         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7284       else if (tree_type == TRUTH_ORIF_EXPR)
7285         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7286
7287       overload = NULL;
7288       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7289          ERROR_MARK for everything that is not a binary expression.
7290          This makes warn_about_parentheses miss some warnings that
7291          involve unary operators.  For unary expressions we should
7292          pass the correct tree_code unless the unary expression was
7293          surrounded by parentheses.
7294       */
7295       if (no_toplevel_fold_p
7296           && lookahead_prec <= prec
7297           && sp == stack
7298           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7299         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7300       else
7301         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7302                                  &overload, tf_warning_or_error);
7303       lhs_type = tree_type;
7304
7305       /* If the binary operator required the use of an overloaded operator,
7306          then this expression cannot be an integral constant-expression.
7307          An overloaded operator can be used even if both operands are
7308          otherwise permissible in an integral constant-expression if at
7309          least one of the operands is of enumeration type.  */
7310
7311       if (overload
7312           && cp_parser_non_integral_constant_expression (parser,
7313                                                          NIC_OVERLOADED))
7314         return error_mark_node;
7315     }
7316
7317   return lhs;
7318 }
7319
7320
7321 /* Parse the `? expression : assignment-expression' part of a
7322    conditional-expression.  The LOGICAL_OR_EXPR is the
7323    logical-or-expression that started the conditional-expression.
7324    Returns a representation of the entire conditional-expression.
7325
7326    This routine is used by cp_parser_assignment_expression.
7327
7328      ? expression : assignment-expression
7329
7330    GNU Extensions:
7331
7332      ? : assignment-expression */
7333
7334 static tree
7335 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7336 {
7337   tree expr;
7338   tree assignment_expr;
7339   struct cp_token *token;
7340
7341   /* Consume the `?' token.  */
7342   cp_lexer_consume_token (parser->lexer);
7343   token = cp_lexer_peek_token (parser->lexer);
7344   if (cp_parser_allow_gnu_extensions_p (parser)
7345       && token->type == CPP_COLON)
7346     {
7347       pedwarn (token->location, OPT_pedantic, 
7348                "ISO C++ does not allow ?: with omitted middle operand");
7349       /* Implicit true clause.  */
7350       expr = NULL_TREE;
7351       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7352       warn_for_omitted_condop (token->location, logical_or_expr);
7353     }
7354   else
7355     {
7356       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7357       parser->colon_corrects_to_scope_p = false;
7358       /* Parse the expression.  */
7359       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7360       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7361       c_inhibit_evaluation_warnings +=
7362         ((logical_or_expr == truthvalue_true_node)
7363          - (logical_or_expr == truthvalue_false_node));
7364       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7365     }
7366
7367   /* The next token should be a `:'.  */
7368   cp_parser_require (parser, CPP_COLON, RT_COLON);
7369   /* Parse the assignment-expression.  */
7370   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7371   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7372
7373   /* Build the conditional-expression.  */
7374   return build_x_conditional_expr (logical_or_expr,
7375                                    expr,
7376                                    assignment_expr,
7377                                    tf_warning_or_error);
7378 }
7379
7380 /* Parse an assignment-expression.
7381
7382    assignment-expression:
7383      conditional-expression
7384      logical-or-expression assignment-operator assignment_expression
7385      throw-expression
7386
7387    CAST_P is true if this expression is the target of a cast.
7388
7389    Returns a representation for the expression.  */
7390
7391 static tree
7392 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7393                                  cp_id_kind * pidk)
7394 {
7395   tree expr;
7396
7397   /* If the next token is the `throw' keyword, then we're looking at
7398      a throw-expression.  */
7399   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7400     expr = cp_parser_throw_expression (parser);
7401   /* Otherwise, it must be that we are looking at a
7402      logical-or-expression.  */
7403   else
7404     {
7405       /* Parse the binary expressions (logical-or-expression).  */
7406       expr = cp_parser_binary_expression (parser, cast_p, false,
7407                                           PREC_NOT_OPERATOR, pidk);
7408       /* If the next token is a `?' then we're actually looking at a
7409          conditional-expression.  */
7410       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7411         return cp_parser_question_colon_clause (parser, expr);
7412       else
7413         {
7414           enum tree_code assignment_operator;
7415
7416           /* If it's an assignment-operator, we're using the second
7417              production.  */
7418           assignment_operator
7419             = cp_parser_assignment_operator_opt (parser);
7420           if (assignment_operator != ERROR_MARK)
7421             {
7422               bool non_constant_p;
7423
7424               /* Parse the right-hand side of the assignment.  */
7425               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7426
7427               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7428                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7429
7430               /* An assignment may not appear in a
7431                  constant-expression.  */
7432               if (cp_parser_non_integral_constant_expression (parser,
7433                                                               NIC_ASSIGNMENT))
7434                 return error_mark_node;
7435               /* Build the assignment expression.  */
7436               expr = build_x_modify_expr (expr,
7437                                           assignment_operator,
7438                                           rhs,
7439                                           tf_warning_or_error);
7440             }
7441         }
7442     }
7443
7444   return expr;
7445 }
7446
7447 /* Parse an (optional) assignment-operator.
7448
7449    assignment-operator: one of
7450      = *= /= %= += -= >>= <<= &= ^= |=
7451
7452    GNU Extension:
7453
7454    assignment-operator: one of
7455      <?= >?=
7456
7457    If the next token is an assignment operator, the corresponding tree
7458    code is returned, and the token is consumed.  For example, for
7459    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7460    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7461    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7462    operator, ERROR_MARK is returned.  */
7463
7464 static enum tree_code
7465 cp_parser_assignment_operator_opt (cp_parser* parser)
7466 {
7467   enum tree_code op;
7468   cp_token *token;
7469
7470   /* Peek at the next token.  */
7471   token = cp_lexer_peek_token (parser->lexer);
7472
7473   switch (token->type)
7474     {
7475     case CPP_EQ:
7476       op = NOP_EXPR;
7477       break;
7478
7479     case CPP_MULT_EQ:
7480       op = MULT_EXPR;
7481       break;
7482
7483     case CPP_DIV_EQ:
7484       op = TRUNC_DIV_EXPR;
7485       break;
7486
7487     case CPP_MOD_EQ:
7488       op = TRUNC_MOD_EXPR;
7489       break;
7490
7491     case CPP_PLUS_EQ:
7492       op = PLUS_EXPR;
7493       break;
7494
7495     case CPP_MINUS_EQ:
7496       op = MINUS_EXPR;
7497       break;
7498
7499     case CPP_RSHIFT_EQ:
7500       op = RSHIFT_EXPR;
7501       break;
7502
7503     case CPP_LSHIFT_EQ:
7504       op = LSHIFT_EXPR;
7505       break;
7506
7507     case CPP_AND_EQ:
7508       op = BIT_AND_EXPR;
7509       break;
7510
7511     case CPP_XOR_EQ:
7512       op = BIT_XOR_EXPR;
7513       break;
7514
7515     case CPP_OR_EQ:
7516       op = BIT_IOR_EXPR;
7517       break;
7518
7519     default:
7520       /* Nothing else is an assignment operator.  */
7521       op = ERROR_MARK;
7522     }
7523
7524   /* If it was an assignment operator, consume it.  */
7525   if (op != ERROR_MARK)
7526     cp_lexer_consume_token (parser->lexer);
7527
7528   return op;
7529 }
7530
7531 /* Parse an expression.
7532
7533    expression:
7534      assignment-expression
7535      expression , assignment-expression
7536
7537    CAST_P is true if this expression is the target of a cast.
7538
7539    Returns a representation of the expression.  */
7540
7541 static tree
7542 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7543 {
7544   tree expression = NULL_TREE;
7545
7546   while (true)
7547     {
7548       tree assignment_expression;
7549
7550       /* Parse the next assignment-expression.  */
7551       assignment_expression
7552         = cp_parser_assignment_expression (parser, cast_p, pidk);
7553       /* If this is the first assignment-expression, we can just
7554          save it away.  */
7555       if (!expression)
7556         expression = assignment_expression;
7557       else
7558         expression = build_x_compound_expr (expression,
7559                                             assignment_expression,
7560                                             tf_warning_or_error);
7561       /* If the next token is not a comma, then we are done with the
7562          expression.  */
7563       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7564         break;
7565       /* Consume the `,'.  */
7566       cp_lexer_consume_token (parser->lexer);
7567       /* A comma operator cannot appear in a constant-expression.  */
7568       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7569         expression = error_mark_node;
7570     }
7571
7572   return expression;
7573 }
7574
7575 /* Parse a constant-expression.
7576
7577    constant-expression:
7578      conditional-expression
7579
7580   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7581   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7582   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7583   is false, NON_CONSTANT_P should be NULL.  */
7584
7585 static tree
7586 cp_parser_constant_expression (cp_parser* parser,
7587                                bool allow_non_constant_p,
7588                                bool *non_constant_p)
7589 {
7590   bool saved_integral_constant_expression_p;
7591   bool saved_allow_non_integral_constant_expression_p;
7592   bool saved_non_integral_constant_expression_p;
7593   tree expression;
7594
7595   /* It might seem that we could simply parse the
7596      conditional-expression, and then check to see if it were
7597      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7598      one that the compiler can figure out is constant, possibly after
7599      doing some simplifications or optimizations.  The standard has a
7600      precise definition of constant-expression, and we must honor
7601      that, even though it is somewhat more restrictive.
7602
7603      For example:
7604
7605        int i[(2, 3)];
7606
7607      is not a legal declaration, because `(2, 3)' is not a
7608      constant-expression.  The `,' operator is forbidden in a
7609      constant-expression.  However, GCC's constant-folding machinery
7610      will fold this operation to an INTEGER_CST for `3'.  */
7611
7612   /* Save the old settings.  */
7613   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7614   saved_allow_non_integral_constant_expression_p
7615     = parser->allow_non_integral_constant_expression_p;
7616   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7617   /* We are now parsing a constant-expression.  */
7618   parser->integral_constant_expression_p = true;
7619   parser->allow_non_integral_constant_expression_p
7620     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7621   parser->non_integral_constant_expression_p = false;
7622   /* Although the grammar says "conditional-expression", we parse an
7623      "assignment-expression", which also permits "throw-expression"
7624      and the use of assignment operators.  In the case that
7625      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7626      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7627      actually essential that we look for an assignment-expression.
7628      For example, cp_parser_initializer_clauses uses this function to
7629      determine whether a particular assignment-expression is in fact
7630      constant.  */
7631   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7632   /* Restore the old settings.  */
7633   parser->integral_constant_expression_p
7634     = saved_integral_constant_expression_p;
7635   parser->allow_non_integral_constant_expression_p
7636     = saved_allow_non_integral_constant_expression_p;
7637   if (cxx_dialect >= cxx0x)
7638     {
7639       /* Require an rvalue constant expression here; that's what our
7640          callers expect.  Reference constant expressions are handled
7641          separately in e.g. cp_parser_template_argument.  */
7642       bool is_const = potential_rvalue_constant_expression (expression);
7643       parser->non_integral_constant_expression_p = !is_const;
7644       if (!is_const && !allow_non_constant_p)
7645         require_potential_rvalue_constant_expression (expression);
7646     }
7647   if (allow_non_constant_p)
7648     *non_constant_p = parser->non_integral_constant_expression_p;
7649   parser->non_integral_constant_expression_p
7650     = saved_non_integral_constant_expression_p;
7651
7652   return expression;
7653 }
7654
7655 /* Parse __builtin_offsetof.
7656
7657    offsetof-expression:
7658      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7659
7660    offsetof-member-designator:
7661      id-expression
7662      | offsetof-member-designator "." id-expression
7663      | offsetof-member-designator "[" expression "]"
7664      | offsetof-member-designator "->" id-expression  */
7665
7666 static tree
7667 cp_parser_builtin_offsetof (cp_parser *parser)
7668 {
7669   int save_ice_p, save_non_ice_p;
7670   tree type, expr;
7671   cp_id_kind dummy;
7672   cp_token *token;
7673
7674   /* We're about to accept non-integral-constant things, but will
7675      definitely yield an integral constant expression.  Save and
7676      restore these values around our local parsing.  */
7677   save_ice_p = parser->integral_constant_expression_p;
7678   save_non_ice_p = parser->non_integral_constant_expression_p;
7679
7680   /* Consume the "__builtin_offsetof" token.  */
7681   cp_lexer_consume_token (parser->lexer);
7682   /* Consume the opening `('.  */
7683   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7684   /* Parse the type-id.  */
7685   type = cp_parser_type_id (parser);
7686   /* Look for the `,'.  */
7687   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7688   token = cp_lexer_peek_token (parser->lexer);
7689
7690   /* Build the (type *)null that begins the traditional offsetof macro.  */
7691   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7692                             tf_warning_or_error);
7693
7694   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7695   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7696                                                  true, &dummy, token->location);
7697   while (true)
7698     {
7699       token = cp_lexer_peek_token (parser->lexer);
7700       switch (token->type)
7701         {
7702         case CPP_OPEN_SQUARE:
7703           /* offsetof-member-designator "[" expression "]" */
7704           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7705           break;
7706
7707         case CPP_DEREF:
7708           /* offsetof-member-designator "->" identifier */
7709           expr = grok_array_decl (expr, integer_zero_node);
7710           /* FALLTHRU */
7711
7712         case CPP_DOT:
7713           /* offsetof-member-designator "." identifier */
7714           cp_lexer_consume_token (parser->lexer);
7715           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7716                                                          expr, true, &dummy,
7717                                                          token->location);
7718           break;
7719
7720         case CPP_CLOSE_PAREN:
7721           /* Consume the ")" token.  */
7722           cp_lexer_consume_token (parser->lexer);
7723           goto success;
7724
7725         default:
7726           /* Error.  We know the following require will fail, but
7727              that gives the proper error message.  */
7728           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7729           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7730           expr = error_mark_node;
7731           goto failure;
7732         }
7733     }
7734
7735  success:
7736   /* If we're processing a template, we can't finish the semantics yet.
7737      Otherwise we can fold the entire expression now.  */
7738   if (processing_template_decl)
7739     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7740   else
7741     expr = finish_offsetof (expr);
7742
7743  failure:
7744   parser->integral_constant_expression_p = save_ice_p;
7745   parser->non_integral_constant_expression_p = save_non_ice_p;
7746
7747   return expr;
7748 }
7749
7750 /* Parse a trait expression.
7751
7752    Returns a representation of the expression, the underlying type
7753    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7754
7755 static tree
7756 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7757 {
7758   cp_trait_kind kind;
7759   tree type1, type2 = NULL_TREE;
7760   bool binary = false;
7761   cp_decl_specifier_seq decl_specs;
7762
7763   switch (keyword)
7764     {
7765     case RID_HAS_NOTHROW_ASSIGN:
7766       kind = CPTK_HAS_NOTHROW_ASSIGN;
7767       break;
7768     case RID_HAS_NOTHROW_CONSTRUCTOR:
7769       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7770       break;
7771     case RID_HAS_NOTHROW_COPY:
7772       kind = CPTK_HAS_NOTHROW_COPY;
7773       break;
7774     case RID_HAS_TRIVIAL_ASSIGN:
7775       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7776       break;
7777     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7778       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7779       break;
7780     case RID_HAS_TRIVIAL_COPY:
7781       kind = CPTK_HAS_TRIVIAL_COPY;
7782       break;
7783     case RID_HAS_TRIVIAL_DESTRUCTOR:
7784       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7785       break;
7786     case RID_HAS_VIRTUAL_DESTRUCTOR:
7787       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7788       break;
7789     case RID_IS_ABSTRACT:
7790       kind = CPTK_IS_ABSTRACT;
7791       break;
7792     case RID_IS_BASE_OF:
7793       kind = CPTK_IS_BASE_OF;
7794       binary = true;
7795       break;
7796     case RID_IS_CLASS:
7797       kind = CPTK_IS_CLASS;
7798       break;
7799     case RID_IS_CONVERTIBLE_TO:
7800       kind = CPTK_IS_CONVERTIBLE_TO;
7801       binary = true;
7802       break;
7803     case RID_IS_EMPTY:
7804       kind = CPTK_IS_EMPTY;
7805       break;
7806     case RID_IS_ENUM:
7807       kind = CPTK_IS_ENUM;
7808       break;
7809     case RID_IS_LITERAL_TYPE:
7810       kind = CPTK_IS_LITERAL_TYPE;
7811       break;
7812     case RID_IS_POD:
7813       kind = CPTK_IS_POD;
7814       break;
7815     case RID_IS_POLYMORPHIC:
7816       kind = CPTK_IS_POLYMORPHIC;
7817       break;
7818     case RID_IS_STD_LAYOUT:
7819       kind = CPTK_IS_STD_LAYOUT;
7820       break;
7821     case RID_IS_TRIVIAL:
7822       kind = CPTK_IS_TRIVIAL;
7823       break;
7824     case RID_IS_UNION:
7825       kind = CPTK_IS_UNION;
7826       break;
7827     case RID_UNDERLYING_TYPE:
7828       kind = CPTK_UNDERLYING_TYPE;
7829       break;
7830     case RID_BASES:
7831       kind = CPTK_BASES;
7832       break;
7833     case RID_DIRECT_BASES:
7834       kind = CPTK_DIRECT_BASES;
7835       break;
7836     default:
7837       gcc_unreachable ();
7838     }
7839
7840   /* Consume the token.  */
7841   cp_lexer_consume_token (parser->lexer);
7842
7843   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7844
7845   type1 = cp_parser_type_id (parser);
7846
7847   if (type1 == error_mark_node)
7848     return error_mark_node;
7849
7850   /* Build a trivial decl-specifier-seq.  */
7851   clear_decl_specs (&decl_specs);
7852   decl_specs.type = type1;
7853
7854   /* Call grokdeclarator to figure out what type this is.  */
7855   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7856                           /*initialized=*/0, /*attrlist=*/NULL);
7857
7858   if (binary)
7859     {
7860       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7861  
7862       type2 = cp_parser_type_id (parser);
7863
7864       if (type2 == error_mark_node)
7865         return error_mark_node;
7866
7867       /* Build a trivial decl-specifier-seq.  */
7868       clear_decl_specs (&decl_specs);
7869       decl_specs.type = type2;
7870
7871       /* Call grokdeclarator to figure out what type this is.  */
7872       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7873                               /*initialized=*/0, /*attrlist=*/NULL);
7874     }
7875
7876   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7877
7878   /* Complete the trait expression, which may mean either processing
7879      the trait expr now or saving it for template instantiation.  */
7880   switch(kind)
7881     {
7882     case CPTK_UNDERLYING_TYPE:
7883       return finish_underlying_type (type1);
7884     case CPTK_BASES:
7885       return finish_bases (type1, false);
7886     case CPTK_DIRECT_BASES:
7887       return finish_bases (type1, true);
7888     default:
7889       return finish_trait_expr (kind, type1, type2);
7890     }
7891 }
7892
7893 /* Lambdas that appear in variable initializer or default argument scope
7894    get that in their mangling, so we need to record it.  We might as well
7895    use the count for function and namespace scopes as well.  */
7896 static GTY(()) tree lambda_scope;
7897 static GTY(()) int lambda_count;
7898 typedef struct GTY(()) tree_int
7899 {
7900   tree t;
7901   int i;
7902 } tree_int;
7903 DEF_VEC_O(tree_int);
7904 DEF_VEC_ALLOC_O(tree_int,gc);
7905 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7906
7907 static void
7908 start_lambda_scope (tree decl)
7909 {
7910   tree_int ti;
7911   gcc_assert (decl);
7912   /* Once we're inside a function, we ignore other scopes and just push
7913      the function again so that popping works properly.  */
7914   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7915     decl = current_function_decl;
7916   ti.t = lambda_scope;
7917   ti.i = lambda_count;
7918   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7919   if (lambda_scope != decl)
7920     {
7921       /* Don't reset the count if we're still in the same function.  */
7922       lambda_scope = decl;
7923       lambda_count = 0;
7924     }
7925 }
7926
7927 static void
7928 record_lambda_scope (tree lambda)
7929 {
7930   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7931   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7932 }
7933
7934 static void
7935 finish_lambda_scope (void)
7936 {
7937   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7938   if (lambda_scope != p->t)
7939     {
7940       lambda_scope = p->t;
7941       lambda_count = p->i;
7942     }
7943   VEC_pop (tree_int, lambda_scope_stack);
7944 }
7945
7946 /* Parse a lambda expression.
7947
7948    lambda-expression:
7949      lambda-introducer lambda-declarator [opt] compound-statement
7950
7951    Returns a representation of the expression.  */
7952
7953 static tree
7954 cp_parser_lambda_expression (cp_parser* parser)
7955 {
7956   tree lambda_expr = build_lambda_expr ();
7957   tree type;
7958   bool ok;
7959
7960   LAMBDA_EXPR_LOCATION (lambda_expr)
7961     = cp_lexer_peek_token (parser->lexer)->location;
7962
7963   if (cp_unevaluated_operand)
7964     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7965               "lambda-expression in unevaluated context");
7966
7967   /* We may be in the middle of deferred access check.  Disable
7968      it now.  */
7969   push_deferring_access_checks (dk_no_deferred);
7970
7971   cp_parser_lambda_introducer (parser, lambda_expr);
7972
7973   type = begin_lambda_type (lambda_expr);
7974
7975   record_lambda_scope (lambda_expr);
7976
7977   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7978   determine_visibility (TYPE_NAME (type));
7979
7980   /* Now that we've started the type, add the capture fields for any
7981      explicit captures.  */
7982   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7983
7984   {
7985     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7986     unsigned int saved_num_template_parameter_lists
7987         = parser->num_template_parameter_lists;
7988     unsigned char in_statement = parser->in_statement;
7989     bool in_switch_statement_p = parser->in_switch_statement_p;
7990
7991     parser->num_template_parameter_lists = 0;
7992     parser->in_statement = 0;
7993     parser->in_switch_statement_p = false;
7994
7995     /* By virtue of defining a local class, a lambda expression has access to
7996        the private variables of enclosing classes.  */
7997
7998     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
7999
8000     if (ok)
8001       cp_parser_lambda_body (parser, lambda_expr);
8002     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8003       cp_parser_skip_to_end_of_block_or_statement (parser);
8004
8005     /* The capture list was built up in reverse order; fix that now.  */
8006     {
8007       tree newlist = NULL_TREE;
8008       tree elt, next;
8009
8010       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8011            elt; elt = next)
8012         {
8013           next = TREE_CHAIN (elt);
8014           TREE_CHAIN (elt) = newlist;
8015           newlist = elt;
8016         }
8017       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8018     }
8019
8020     if (ok)
8021       maybe_add_lambda_conv_op (type);
8022
8023     type = finish_struct (type, /*attributes=*/NULL_TREE);
8024
8025     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8026     parser->in_statement = in_statement;
8027     parser->in_switch_statement_p = in_switch_statement_p;
8028   }
8029
8030   pop_deferring_access_checks ();
8031
8032   /* This field is only used during parsing of the lambda.  */
8033   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8034
8035   /* This lambda shouldn't have any proxies left at this point.  */
8036   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8037   /* And now that we're done, push proxies for an enclosing lambda.  */
8038   insert_pending_capture_proxies ();
8039
8040   if (ok)
8041     return build_lambda_object (lambda_expr);
8042   else
8043     return error_mark_node;
8044 }
8045
8046 /* Parse the beginning of a lambda expression.
8047
8048    lambda-introducer:
8049      [ lambda-capture [opt] ]
8050
8051    LAMBDA_EXPR is the current representation of the lambda expression.  */
8052
8053 static void
8054 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8055 {
8056   /* Need commas after the first capture.  */
8057   bool first = true;
8058
8059   /* Eat the leading `['.  */
8060   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8061
8062   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8063   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8064       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8065     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8066   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8067     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8068
8069   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8070     {
8071       cp_lexer_consume_token (parser->lexer);
8072       first = false;
8073     }
8074
8075   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8076     {
8077       cp_token* capture_token;
8078       tree capture_id;
8079       tree capture_init_expr;
8080       cp_id_kind idk = CP_ID_KIND_NONE;
8081       bool explicit_init_p = false;
8082
8083       enum capture_kind_type
8084       {
8085         BY_COPY,
8086         BY_REFERENCE
8087       };
8088       enum capture_kind_type capture_kind = BY_COPY;
8089
8090       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8091         {
8092           error ("expected end of capture-list");
8093           return;
8094         }
8095
8096       if (first)
8097         first = false;
8098       else
8099         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8100
8101       /* Possibly capture `this'.  */
8102       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8103         {
8104           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8105           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8106             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8107                      "with by-copy capture default");
8108           cp_lexer_consume_token (parser->lexer);
8109           add_capture (lambda_expr,
8110                        /*id=*/this_identifier,
8111                        /*initializer=*/finish_this_expr(),
8112                        /*by_reference_p=*/false,
8113                        explicit_init_p);
8114           continue;
8115         }
8116
8117       /* Remember whether we want to capture as a reference or not.  */
8118       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8119         {
8120           capture_kind = BY_REFERENCE;
8121           cp_lexer_consume_token (parser->lexer);
8122         }
8123
8124       /* Get the identifier.  */
8125       capture_token = cp_lexer_peek_token (parser->lexer);
8126       capture_id = cp_parser_identifier (parser);
8127
8128       if (capture_id == error_mark_node)
8129         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8130            delimiters, but I modified this to stop on unnested ']' as well.  It
8131            was already changed to stop on unnested '}', so the
8132            "closing_parenthesis" name is no more misleading with my change.  */
8133         {
8134           cp_parser_skip_to_closing_parenthesis (parser,
8135                                                  /*recovering=*/true,
8136                                                  /*or_comma=*/true,
8137                                                  /*consume_paren=*/true);
8138           break;
8139         }
8140
8141       /* Find the initializer for this capture.  */
8142       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8143         {
8144           /* An explicit expression exists.  */
8145           cp_lexer_consume_token (parser->lexer);
8146           pedwarn (input_location, OPT_pedantic,
8147                    "ISO C++ does not allow initializers "
8148                    "in lambda expression capture lists");
8149           capture_init_expr = cp_parser_assignment_expression (parser,
8150                                                                /*cast_p=*/true,
8151                                                                &idk);
8152           explicit_init_p = true;
8153         }
8154       else
8155         {
8156           const char* error_msg;
8157
8158           /* Turn the identifier into an id-expression.  */
8159           capture_init_expr
8160             = cp_parser_lookup_name
8161                 (parser,
8162                  capture_id,
8163                  none_type,
8164                  /*is_template=*/false,
8165                  /*is_namespace=*/false,
8166                  /*check_dependency=*/true,
8167                  /*ambiguous_decls=*/NULL,
8168                  capture_token->location);
8169
8170           if (capture_init_expr == error_mark_node)
8171             {
8172               unqualified_name_lookup_error (capture_id);
8173               continue;
8174             }
8175           else if (DECL_P (capture_init_expr)
8176                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8177                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8178             {
8179               error_at (capture_token->location,
8180                         "capture of non-variable %qD ",
8181                         capture_init_expr);
8182               inform (0, "%q+#D declared here", capture_init_expr);
8183               continue;
8184             }
8185           if (TREE_CODE (capture_init_expr) == VAR_DECL
8186               && decl_storage_duration (capture_init_expr) != dk_auto)
8187             {
8188               pedwarn (capture_token->location, 0, "capture of variable "
8189                        "%qD with non-automatic storage duration",
8190                        capture_init_expr);
8191               inform (0, "%q+#D declared here", capture_init_expr);
8192               continue;
8193             }
8194
8195           capture_init_expr
8196             = finish_id_expression
8197                 (capture_id,
8198                  capture_init_expr,
8199                  parser->scope,
8200                  &idk,
8201                  /*integral_constant_expression_p=*/false,
8202                  /*allow_non_integral_constant_expression_p=*/false,
8203                  /*non_integral_constant_expression_p=*/NULL,
8204                  /*template_p=*/false,
8205                  /*done=*/true,
8206                  /*address_p=*/false,
8207                  /*template_arg_p=*/false,
8208                  &error_msg,
8209                  capture_token->location);
8210         }
8211
8212       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8213           && !explicit_init_p)
8214         {
8215           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8216               && capture_kind == BY_COPY)
8217             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8218                      "of %qD redundant with by-copy capture default",
8219                      capture_id);
8220           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8221               && capture_kind == BY_REFERENCE)
8222             pedwarn (capture_token->location, 0, "explicit by-reference "
8223                      "capture of %qD redundant with by-reference capture "
8224                      "default", capture_id);
8225         }
8226
8227       add_capture (lambda_expr,
8228                    capture_id,
8229                    capture_init_expr,
8230                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8231                    explicit_init_p);
8232     }
8233
8234   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8235 }
8236
8237 /* Parse the (optional) middle of a lambda expression.
8238
8239    lambda-declarator:
8240      ( parameter-declaration-clause [opt] )
8241        attribute-specifier [opt]
8242        mutable [opt]
8243        exception-specification [opt]
8244        lambda-return-type-clause [opt]
8245
8246    LAMBDA_EXPR is the current representation of the lambda expression.  */
8247
8248 static bool
8249 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8250 {
8251   /* 5.1.1.4 of the standard says:
8252        If a lambda-expression does not include a lambda-declarator, it is as if
8253        the lambda-declarator were ().
8254      This means an empty parameter list, no attributes, and no exception
8255      specification.  */
8256   tree param_list = void_list_node;
8257   tree attributes = NULL_TREE;
8258   tree exception_spec = NULL_TREE;
8259   tree t;
8260
8261   /* The lambda-declarator is optional, but must begin with an opening
8262      parenthesis if present.  */
8263   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8264     {
8265       cp_lexer_consume_token (parser->lexer);
8266
8267       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8268
8269       /* Parse parameters.  */
8270       param_list = cp_parser_parameter_declaration_clause (parser);
8271
8272       /* Default arguments shall not be specified in the
8273          parameter-declaration-clause of a lambda-declarator.  */
8274       for (t = param_list; t; t = TREE_CHAIN (t))
8275         if (TREE_PURPOSE (t))
8276           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8277                    "default argument specified for lambda parameter");
8278
8279       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8280
8281       attributes = cp_parser_attributes_opt (parser);
8282
8283       /* Parse optional `mutable' keyword.  */
8284       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8285         {
8286           cp_lexer_consume_token (parser->lexer);
8287           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8288         }
8289
8290       /* Parse optional exception specification.  */
8291       exception_spec = cp_parser_exception_specification_opt (parser);
8292
8293       /* Parse optional trailing return type.  */
8294       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8295         {
8296           cp_lexer_consume_token (parser->lexer);
8297           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8298         }
8299
8300       /* The function parameters must be in scope all the way until after the
8301          trailing-return-type in case of decltype.  */
8302       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8303         pop_binding (DECL_NAME (t), t);
8304
8305       leave_scope ();
8306     }
8307
8308   /* Create the function call operator.
8309
8310      Messing with declarators like this is no uglier than building up the
8311      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8312      other code.  */
8313   {
8314     cp_decl_specifier_seq return_type_specs;
8315     cp_declarator* declarator;
8316     tree fco;
8317     int quals;
8318     void *p;
8319
8320     clear_decl_specs (&return_type_specs);
8321     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8322       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8323     else
8324       /* Maybe we will deduce the return type later, but we can use void
8325          as a placeholder return type anyways.  */
8326       return_type_specs.type = void_type_node;
8327
8328     p = obstack_alloc (&declarator_obstack, 0);
8329
8330     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8331                                      sfk_none);
8332
8333     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8334              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8335     declarator = make_call_declarator (declarator, param_list, quals,
8336                                        VIRT_SPEC_UNSPECIFIED,
8337                                        exception_spec,
8338                                        /*late_return_type=*/NULL_TREE);
8339     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8340
8341     fco = grokmethod (&return_type_specs,
8342                       declarator,
8343                       attributes);
8344     if (fco != error_mark_node)
8345       {
8346         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8347         DECL_ARTIFICIAL (fco) = 1;
8348         /* Give the object parameter a different name.  */
8349         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8350       }
8351
8352     finish_member_declaration (fco);
8353
8354     obstack_free (&declarator_obstack, p);
8355
8356     return (fco != error_mark_node);
8357   }
8358 }
8359
8360 /* Parse the body of a lambda expression, which is simply
8361
8362    compound-statement
8363
8364    but which requires special handling.
8365    LAMBDA_EXPR is the current representation of the lambda expression.  */
8366
8367 static void
8368 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8369 {
8370   bool nested = (current_function_decl != NULL_TREE);
8371   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8372   if (nested)
8373     push_function_context ();
8374   else
8375     /* Still increment function_depth so that we don't GC in the
8376        middle of an expression.  */
8377     ++function_depth;
8378   /* Clear this in case we're in the middle of a default argument.  */
8379   parser->local_variables_forbidden_p = false;
8380
8381   /* Finish the function call operator
8382      - class_specifier
8383      + late_parsing_for_member
8384      + function_definition_after_declarator
8385      + ctor_initializer_opt_and_function_body  */
8386   {
8387     tree fco = lambda_function (lambda_expr);
8388     tree body;
8389     bool done = false;
8390     tree compound_stmt;
8391     tree cap;
8392
8393     /* Let the front end know that we are going to be defining this
8394        function.  */
8395     start_preparsed_function (fco,
8396                               NULL_TREE,
8397                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8398
8399     start_lambda_scope (fco);
8400     body = begin_function_body ();
8401
8402     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8403       goto out;
8404
8405     /* Push the proxies for any explicit captures.  */
8406     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8407          cap = TREE_CHAIN (cap))
8408       build_capture_proxy (TREE_PURPOSE (cap));
8409
8410     compound_stmt = begin_compound_stmt (0);
8411
8412     /* 5.1.1.4 of the standard says:
8413          If a lambda-expression does not include a trailing-return-type, it
8414          is as if the trailing-return-type denotes the following type:
8415           * if the compound-statement is of the form
8416                { return attribute-specifier [opt] expression ; }
8417              the type of the returned expression after lvalue-to-rvalue
8418              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8419              (_conv.array_ 4.2), and function-to-pointer conversion
8420              (_conv.func_ 4.3);
8421           * otherwise, void.  */
8422
8423     /* In a lambda that has neither a lambda-return-type-clause
8424        nor a deducible form, errors should be reported for return statements
8425        in the body.  Since we used void as the placeholder return type, parsing
8426        the body as usual will give such desired behavior.  */
8427     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8428         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8429         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8430       {
8431         tree expr = NULL_TREE;
8432         cp_id_kind idk = CP_ID_KIND_NONE;
8433
8434         /* Parse tentatively in case there's more after the initial return
8435            statement.  */
8436         cp_parser_parse_tentatively (parser);
8437
8438         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8439
8440         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8441
8442         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8443         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8444
8445         if (cp_parser_parse_definitely (parser))
8446           {
8447             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8448
8449             /* Will get error here if type not deduced yet.  */
8450             finish_return_stmt (expr);
8451
8452             done = true;
8453           }
8454       }
8455
8456     if (!done)
8457       {
8458         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8459           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8460         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8461           cp_parser_label_declaration (parser);
8462         cp_parser_statement_seq_opt (parser, NULL_TREE);
8463         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8464         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8465       }
8466
8467     finish_compound_stmt (compound_stmt);
8468
8469   out:
8470     finish_function_body (body);
8471     finish_lambda_scope ();
8472
8473     /* Finish the function and generate code for it if necessary.  */
8474     expand_or_defer_fn (finish_function (/*inline*/2));
8475   }
8476
8477   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8478   if (nested)
8479     pop_function_context();
8480   else
8481     --function_depth;
8482 }
8483
8484 /* Statements [gram.stmt.stmt]  */
8485
8486 /* Parse a statement.
8487
8488    statement:
8489      labeled-statement
8490      expression-statement
8491      compound-statement
8492      selection-statement
8493      iteration-statement
8494      jump-statement
8495      declaration-statement
8496      try-block
8497
8498   IN_COMPOUND is true when the statement is nested inside a
8499   cp_parser_compound_statement; this matters for certain pragmas.
8500
8501   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8502   is a (possibly labeled) if statement which is not enclosed in braces
8503   and has an else clause.  This is used to implement -Wparentheses.  */
8504
8505 static void
8506 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8507                      bool in_compound, bool *if_p)
8508 {
8509   tree statement;
8510   cp_token *token;
8511   location_t statement_location;
8512
8513  restart:
8514   if (if_p != NULL)
8515     *if_p = false;
8516   /* There is no statement yet.  */
8517   statement = NULL_TREE;
8518   /* Peek at the next token.  */
8519   token = cp_lexer_peek_token (parser->lexer);
8520   /* Remember the location of the first token in the statement.  */
8521   statement_location = token->location;
8522   /* If this is a keyword, then that will often determine what kind of
8523      statement we have.  */
8524   if (token->type == CPP_KEYWORD)
8525     {
8526       enum rid keyword = token->keyword;
8527
8528       switch (keyword)
8529         {
8530         case RID_CASE:
8531         case RID_DEFAULT:
8532           /* Looks like a labeled-statement with a case label.
8533              Parse the label, and then use tail recursion to parse
8534              the statement.  */
8535           cp_parser_label_for_labeled_statement (parser);
8536           goto restart;
8537
8538         case RID_IF:
8539         case RID_SWITCH:
8540           statement = cp_parser_selection_statement (parser, if_p);
8541           break;
8542
8543         case RID_WHILE:
8544         case RID_DO:
8545         case RID_FOR:
8546           statement = cp_parser_iteration_statement (parser);
8547           break;
8548
8549         case RID_BREAK:
8550         case RID_CONTINUE:
8551         case RID_RETURN:
8552         case RID_GOTO:
8553           statement = cp_parser_jump_statement (parser);
8554           break;
8555
8556           /* Objective-C++ exception-handling constructs.  */
8557         case RID_AT_TRY:
8558         case RID_AT_CATCH:
8559         case RID_AT_FINALLY:
8560         case RID_AT_SYNCHRONIZED:
8561         case RID_AT_THROW:
8562           statement = cp_parser_objc_statement (parser);
8563           break;
8564
8565         case RID_TRY:
8566           statement = cp_parser_try_block (parser);
8567           break;
8568
8569         case RID_NAMESPACE:
8570           /* This must be a namespace alias definition.  */
8571           cp_parser_declaration_statement (parser);
8572           return;
8573           
8574         default:
8575           /* It might be a keyword like `int' that can start a
8576              declaration-statement.  */
8577           break;
8578         }
8579     }
8580   else if (token->type == CPP_NAME)
8581     {
8582       /* If the next token is a `:', then we are looking at a
8583          labeled-statement.  */
8584       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8585       if (token->type == CPP_COLON)
8586         {
8587           /* Looks like a labeled-statement with an ordinary label.
8588              Parse the label, and then use tail recursion to parse
8589              the statement.  */
8590           cp_parser_label_for_labeled_statement (parser);
8591           goto restart;
8592         }
8593     }
8594   /* Anything that starts with a `{' must be a compound-statement.  */
8595   else if (token->type == CPP_OPEN_BRACE)
8596     statement = cp_parser_compound_statement (parser, NULL, false, false);
8597   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8598      a statement all its own.  */
8599   else if (token->type == CPP_PRAGMA)
8600     {
8601       /* Only certain OpenMP pragmas are attached to statements, and thus
8602          are considered statements themselves.  All others are not.  In
8603          the context of a compound, accept the pragma as a "statement" and
8604          return so that we can check for a close brace.  Otherwise we
8605          require a real statement and must go back and read one.  */
8606       if (in_compound)
8607         cp_parser_pragma (parser, pragma_compound);
8608       else if (!cp_parser_pragma (parser, pragma_stmt))
8609         goto restart;
8610       return;
8611     }
8612   else if (token->type == CPP_EOF)
8613     {
8614       cp_parser_error (parser, "expected statement");
8615       return;
8616     }
8617
8618   /* Everything else must be a declaration-statement or an
8619      expression-statement.  Try for the declaration-statement
8620      first, unless we are looking at a `;', in which case we know that
8621      we have an expression-statement.  */
8622   if (!statement)
8623     {
8624       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8625         {
8626           cp_parser_parse_tentatively (parser);
8627           /* Try to parse the declaration-statement.  */
8628           cp_parser_declaration_statement (parser);
8629           /* If that worked, we're done.  */
8630           if (cp_parser_parse_definitely (parser))
8631             return;
8632         }
8633       /* Look for an expression-statement instead.  */
8634       statement = cp_parser_expression_statement (parser, in_statement_expr);
8635     }
8636
8637   /* Set the line number for the statement.  */
8638   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8639     SET_EXPR_LOCATION (statement, statement_location);
8640 }
8641
8642 /* Parse the label for a labeled-statement, i.e.
8643
8644    identifier :
8645    case constant-expression :
8646    default :
8647
8648    GNU Extension:
8649    case constant-expression ... constant-expression : statement
8650
8651    When a label is parsed without errors, the label is added to the
8652    parse tree by the finish_* functions, so this function doesn't
8653    have to return the label.  */
8654
8655 static void
8656 cp_parser_label_for_labeled_statement (cp_parser* parser)
8657 {
8658   cp_token *token;
8659   tree label = NULL_TREE;
8660   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8661
8662   /* The next token should be an identifier.  */
8663   token = cp_lexer_peek_token (parser->lexer);
8664   if (token->type != CPP_NAME
8665       && token->type != CPP_KEYWORD)
8666     {
8667       cp_parser_error (parser, "expected labeled-statement");
8668       return;
8669     }
8670
8671   parser->colon_corrects_to_scope_p = false;
8672   switch (token->keyword)
8673     {
8674     case RID_CASE:
8675       {
8676         tree expr, expr_hi;
8677         cp_token *ellipsis;
8678
8679         /* Consume the `case' token.  */
8680         cp_lexer_consume_token (parser->lexer);
8681         /* Parse the constant-expression.  */
8682         expr = cp_parser_constant_expression (parser,
8683                                               /*allow_non_constant_p=*/false,
8684                                               NULL);
8685
8686         ellipsis = cp_lexer_peek_token (parser->lexer);
8687         if (ellipsis->type == CPP_ELLIPSIS)
8688           {
8689             /* Consume the `...' token.  */
8690             cp_lexer_consume_token (parser->lexer);
8691             expr_hi =
8692               cp_parser_constant_expression (parser,
8693                                              /*allow_non_constant_p=*/false,
8694                                              NULL);
8695             /* We don't need to emit warnings here, as the common code
8696                will do this for us.  */
8697           }
8698         else
8699           expr_hi = NULL_TREE;
8700
8701         if (parser->in_switch_statement_p)
8702           finish_case_label (token->location, expr, expr_hi);
8703         else
8704           error_at (token->location,
8705                     "case label %qE not within a switch statement",
8706                     expr);
8707       }
8708       break;
8709
8710     case RID_DEFAULT:
8711       /* Consume the `default' token.  */
8712       cp_lexer_consume_token (parser->lexer);
8713
8714       if (parser->in_switch_statement_p)
8715         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8716       else
8717         error_at (token->location, "case label not within a switch statement");
8718       break;
8719
8720     default:
8721       /* Anything else must be an ordinary label.  */
8722       label = finish_label_stmt (cp_parser_identifier (parser));
8723       break;
8724     }
8725
8726   /* Require the `:' token.  */
8727   cp_parser_require (parser, CPP_COLON, RT_COLON);
8728
8729   /* An ordinary label may optionally be followed by attributes.
8730      However, this is only permitted if the attributes are then
8731      followed by a semicolon.  This is because, for backward
8732      compatibility, when parsing
8733        lab: __attribute__ ((unused)) int i;
8734      we want the attribute to attach to "i", not "lab".  */
8735   if (label != NULL_TREE
8736       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8737     {
8738       tree attrs;
8739
8740       cp_parser_parse_tentatively (parser);
8741       attrs = cp_parser_attributes_opt (parser);
8742       if (attrs == NULL_TREE
8743           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8744         cp_parser_abort_tentative_parse (parser);
8745       else if (!cp_parser_parse_definitely (parser))
8746         ;
8747       else
8748         cplus_decl_attributes (&label, attrs, 0);
8749     }
8750
8751   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8752 }
8753
8754 /* Parse an expression-statement.
8755
8756    expression-statement:
8757      expression [opt] ;
8758
8759    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8760    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8761    indicates whether this expression-statement is part of an
8762    expression statement.  */
8763
8764 static tree
8765 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8766 {
8767   tree statement = NULL_TREE;
8768   cp_token *token = cp_lexer_peek_token (parser->lexer);
8769
8770   /* If the next token is a ';', then there is no expression
8771      statement.  */
8772   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8773     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8774
8775   /* Give a helpful message for "A<T>::type t;" and the like.  */
8776   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8777       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8778     {
8779       if (TREE_CODE (statement) == SCOPE_REF)
8780         error_at (token->location, "need %<typename%> before %qE because "
8781                   "%qT is a dependent scope",
8782                   statement, TREE_OPERAND (statement, 0));
8783       else if (is_overloaded_fn (statement)
8784                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8785         {
8786           /* A::A a; */
8787           tree fn = get_first_fn (statement);
8788           error_at (token->location,
8789                     "%<%T::%D%> names the constructor, not the type",
8790                     DECL_CONTEXT (fn), DECL_NAME (fn));
8791         }
8792     }
8793
8794   /* Consume the final `;'.  */
8795   cp_parser_consume_semicolon_at_end_of_statement (parser);
8796
8797   if (in_statement_expr
8798       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8799     /* This is the final expression statement of a statement
8800        expression.  */
8801     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8802   else if (statement)
8803     statement = finish_expr_stmt (statement);
8804   else
8805     finish_stmt ();
8806
8807   return statement;
8808 }
8809
8810 /* Parse a compound-statement.
8811
8812    compound-statement:
8813      { statement-seq [opt] }
8814
8815    GNU extension:
8816
8817    compound-statement:
8818      { label-declaration-seq [opt] statement-seq [opt] }
8819
8820    label-declaration-seq:
8821      label-declaration
8822      label-declaration-seq label-declaration
8823
8824    Returns a tree representing the statement.  */
8825
8826 static tree
8827 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8828                               bool in_try, bool function_body)
8829 {
8830   tree compound_stmt;
8831
8832   /* Consume the `{'.  */
8833   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8834     return error_mark_node;
8835   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8836       && !function_body)
8837     pedwarn (input_location, OPT_pedantic,
8838              "compound-statement in constexpr function");
8839   /* Begin the compound-statement.  */
8840   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8841   /* If the next keyword is `__label__' we have a label declaration.  */
8842   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8843     cp_parser_label_declaration (parser);
8844   /* Parse an (optional) statement-seq.  */
8845   cp_parser_statement_seq_opt (parser, in_statement_expr);
8846   /* Finish the compound-statement.  */
8847   finish_compound_stmt (compound_stmt);
8848   /* Consume the `}'.  */
8849   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8850
8851   return compound_stmt;
8852 }
8853
8854 /* Parse an (optional) statement-seq.
8855
8856    statement-seq:
8857      statement
8858      statement-seq [opt] statement  */
8859
8860 static void
8861 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8862 {
8863   /* Scan statements until there aren't any more.  */
8864   while (true)
8865     {
8866       cp_token *token = cp_lexer_peek_token (parser->lexer);
8867
8868       /* If we are looking at a `}', then we have run out of
8869          statements; the same is true if we have reached the end
8870          of file, or have stumbled upon a stray '@end'.  */
8871       if (token->type == CPP_CLOSE_BRACE
8872           || token->type == CPP_EOF
8873           || token->type == CPP_PRAGMA_EOL
8874           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8875         break;
8876       
8877       /* If we are in a compound statement and find 'else' then
8878          something went wrong.  */
8879       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8880         {
8881           if (parser->in_statement & IN_IF_STMT) 
8882             break;
8883           else
8884             {
8885               token = cp_lexer_consume_token (parser->lexer);
8886               error_at (token->location, "%<else%> without a previous %<if%>");
8887             }
8888         }
8889
8890       /* Parse the statement.  */
8891       cp_parser_statement (parser, in_statement_expr, true, NULL);
8892     }
8893 }
8894
8895 /* Parse a selection-statement.
8896
8897    selection-statement:
8898      if ( condition ) statement
8899      if ( condition ) statement else statement
8900      switch ( condition ) statement
8901
8902    Returns the new IF_STMT or SWITCH_STMT.
8903
8904    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8905    is a (possibly labeled) if statement which is not enclosed in
8906    braces and has an else clause.  This is used to implement
8907    -Wparentheses.  */
8908
8909 static tree
8910 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8911 {
8912   cp_token *token;
8913   enum rid keyword;
8914
8915   if (if_p != NULL)
8916     *if_p = false;
8917
8918   /* Peek at the next token.  */
8919   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8920
8921   /* See what kind of keyword it is.  */
8922   keyword = token->keyword;
8923   switch (keyword)
8924     {
8925     case RID_IF:
8926     case RID_SWITCH:
8927       {
8928         tree statement;
8929         tree condition;
8930
8931         /* Look for the `('.  */
8932         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8933           {
8934             cp_parser_skip_to_end_of_statement (parser);
8935             return error_mark_node;
8936           }
8937
8938         /* Begin the selection-statement.  */
8939         if (keyword == RID_IF)
8940           statement = begin_if_stmt ();
8941         else
8942           statement = begin_switch_stmt ();
8943
8944         /* Parse the condition.  */
8945         condition = cp_parser_condition (parser);
8946         /* Look for the `)'.  */
8947         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8948           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8949                                                  /*consume_paren=*/true);
8950
8951         if (keyword == RID_IF)
8952           {
8953             bool nested_if;
8954             unsigned char in_statement;
8955
8956             /* Add the condition.  */
8957             finish_if_stmt_cond (condition, statement);
8958
8959             /* Parse the then-clause.  */
8960             in_statement = parser->in_statement;
8961             parser->in_statement |= IN_IF_STMT;
8962             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8963               {
8964                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8965                 add_stmt (build_empty_stmt (loc));
8966                 cp_lexer_consume_token (parser->lexer);
8967                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8968                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8969                               "empty body in an %<if%> statement");
8970                 nested_if = false;
8971               }
8972             else
8973               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8974             parser->in_statement = in_statement;
8975
8976             finish_then_clause (statement);
8977
8978             /* If the next token is `else', parse the else-clause.  */
8979             if (cp_lexer_next_token_is_keyword (parser->lexer,
8980                                                 RID_ELSE))
8981               {
8982                 /* Consume the `else' keyword.  */
8983                 cp_lexer_consume_token (parser->lexer);
8984                 begin_else_clause (statement);
8985                 /* Parse the else-clause.  */
8986                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8987                   {
8988                     location_t loc;
8989                     loc = cp_lexer_peek_token (parser->lexer)->location;
8990                     warning_at (loc,
8991                                 OPT_Wempty_body, "suggest braces around "
8992                                 "empty body in an %<else%> statement");
8993                     add_stmt (build_empty_stmt (loc));
8994                     cp_lexer_consume_token (parser->lexer);
8995                   }
8996                 else
8997                   cp_parser_implicitly_scoped_statement (parser, NULL);
8998
8999                 finish_else_clause (statement);
9000
9001                 /* If we are currently parsing a then-clause, then
9002                    IF_P will not be NULL.  We set it to true to
9003                    indicate that this if statement has an else clause.
9004                    This may trigger the Wparentheses warning below
9005                    when we get back up to the parent if statement.  */
9006                 if (if_p != NULL)
9007                   *if_p = true;
9008               }
9009             else
9010               {
9011                 /* This if statement does not have an else clause.  If
9012                    NESTED_IF is true, then the then-clause is an if
9013                    statement which does have an else clause.  We warn
9014                    about the potential ambiguity.  */
9015                 if (nested_if)
9016                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9017                               "suggest explicit braces to avoid ambiguous"
9018                               " %<else%>");
9019               }
9020
9021             /* Now we're all done with the if-statement.  */
9022             finish_if_stmt (statement);
9023           }
9024         else
9025           {
9026             bool in_switch_statement_p;
9027             unsigned char in_statement;
9028
9029             /* Add the condition.  */
9030             finish_switch_cond (condition, statement);
9031
9032             /* Parse the body of the switch-statement.  */
9033             in_switch_statement_p = parser->in_switch_statement_p;
9034             in_statement = parser->in_statement;
9035             parser->in_switch_statement_p = true;
9036             parser->in_statement |= IN_SWITCH_STMT;
9037             cp_parser_implicitly_scoped_statement (parser, NULL);
9038             parser->in_switch_statement_p = in_switch_statement_p;
9039             parser->in_statement = in_statement;
9040
9041             /* Now we're all done with the switch-statement.  */
9042             finish_switch_stmt (statement);
9043           }
9044
9045         return statement;
9046       }
9047       break;
9048
9049     default:
9050       cp_parser_error (parser, "expected selection-statement");
9051       return error_mark_node;
9052     }
9053 }
9054
9055 /* Parse a condition.
9056
9057    condition:
9058      expression
9059      type-specifier-seq declarator = initializer-clause
9060      type-specifier-seq declarator braced-init-list
9061
9062    GNU Extension:
9063
9064    condition:
9065      type-specifier-seq declarator asm-specification [opt]
9066        attributes [opt] = assignment-expression
9067
9068    Returns the expression that should be tested.  */
9069
9070 static tree
9071 cp_parser_condition (cp_parser* parser)
9072 {
9073   cp_decl_specifier_seq type_specifiers;
9074   const char *saved_message;
9075   int declares_class_or_enum;
9076
9077   /* Try the declaration first.  */
9078   cp_parser_parse_tentatively (parser);
9079   /* New types are not allowed in the type-specifier-seq for a
9080      condition.  */
9081   saved_message = parser->type_definition_forbidden_message;
9082   parser->type_definition_forbidden_message
9083     = G_("types may not be defined in conditions");
9084   /* Parse the type-specifier-seq.  */
9085   cp_parser_decl_specifier_seq (parser,
9086                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9087                                 &type_specifiers,
9088                                 &declares_class_or_enum);
9089   /* Restore the saved message.  */
9090   parser->type_definition_forbidden_message = saved_message;
9091   /* If all is well, we might be looking at a declaration.  */
9092   if (!cp_parser_error_occurred (parser))
9093     {
9094       tree decl;
9095       tree asm_specification;
9096       tree attributes;
9097       cp_declarator *declarator;
9098       tree initializer = NULL_TREE;
9099
9100       /* Parse the declarator.  */
9101       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9102                                          /*ctor_dtor_or_conv_p=*/NULL,
9103                                          /*parenthesized_p=*/NULL,
9104                                          /*member_p=*/false);
9105       /* Parse the attributes.  */
9106       attributes = cp_parser_attributes_opt (parser);
9107       /* Parse the asm-specification.  */
9108       asm_specification = cp_parser_asm_specification_opt (parser);
9109       /* If the next token is not an `=' or '{', then we might still be
9110          looking at an expression.  For example:
9111
9112            if (A(a).x)
9113
9114          looks like a decl-specifier-seq and a declarator -- but then
9115          there is no `=', so this is an expression.  */
9116       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9117           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9118         cp_parser_simulate_error (parser);
9119         
9120       /* If we did see an `=' or '{', then we are looking at a declaration
9121          for sure.  */
9122       if (cp_parser_parse_definitely (parser))
9123         {
9124           tree pushed_scope;
9125           bool non_constant_p;
9126           bool flags = LOOKUP_ONLYCONVERTING;
9127
9128           /* Create the declaration.  */
9129           decl = start_decl (declarator, &type_specifiers,
9130                              /*initialized_p=*/true,
9131                              attributes, /*prefix_attributes=*/NULL_TREE,
9132                              &pushed_scope);
9133
9134           /* Parse the initializer.  */
9135           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9136             {
9137               initializer = cp_parser_braced_list (parser, &non_constant_p);
9138               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9139               flags = 0;
9140             }
9141           else
9142             {
9143               /* Consume the `='.  */
9144               cp_parser_require (parser, CPP_EQ, RT_EQ);
9145               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9146             }
9147           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9148             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9149
9150           /* Process the initializer.  */
9151           cp_finish_decl (decl,
9152                           initializer, !non_constant_p,
9153                           asm_specification,
9154                           flags);
9155
9156           if (pushed_scope)
9157             pop_scope (pushed_scope);
9158
9159           return convert_from_reference (decl);
9160         }
9161     }
9162   /* If we didn't even get past the declarator successfully, we are
9163      definitely not looking at a declaration.  */
9164   else
9165     cp_parser_abort_tentative_parse (parser);
9166
9167   /* Otherwise, we are looking at an expression.  */
9168   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9169 }
9170
9171 /* Parses a for-statement or range-for-statement until the closing ')',
9172    not included. */
9173
9174 static tree
9175 cp_parser_for (cp_parser *parser)
9176 {
9177   tree init, scope, decl;
9178   bool is_range_for;
9179
9180   /* Begin the for-statement.  */
9181   scope = begin_for_scope (&init);
9182
9183   /* Parse the initialization.  */
9184   is_range_for = cp_parser_for_init_statement (parser, &decl);
9185
9186   if (is_range_for)
9187     return cp_parser_range_for (parser, scope, init, decl);
9188   else
9189     return cp_parser_c_for (parser, scope, init);
9190 }
9191
9192 static tree
9193 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9194 {
9195   /* Normal for loop */
9196   tree condition = NULL_TREE;
9197   tree expression = NULL_TREE;
9198   tree stmt;
9199
9200   stmt = begin_for_stmt (scope, init);
9201   /* The for-init-statement has already been parsed in
9202      cp_parser_for_init_statement, so no work is needed here.  */
9203   finish_for_init_stmt (stmt);
9204
9205   /* If there's a condition, process it.  */
9206   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9207     condition = cp_parser_condition (parser);
9208   finish_for_cond (condition, stmt);
9209   /* Look for the `;'.  */
9210   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9211
9212   /* If there's an expression, process it.  */
9213   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9214     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9215   finish_for_expr (expression, stmt);
9216
9217   return stmt;
9218 }
9219
9220 /* Tries to parse a range-based for-statement:
9221
9222   range-based-for:
9223     decl-specifier-seq declarator : expression
9224
9225   The decl-specifier-seq declarator and the `:' are already parsed by
9226   cp_parser_for_init_statement. If processing_template_decl it returns a
9227   newly created RANGE_FOR_STMT; if not, it is converted to a
9228   regular FOR_STMT.  */
9229
9230 static tree
9231 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9232 {
9233   tree stmt, range_expr;
9234
9235   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9236     {
9237       bool expr_non_constant_p;
9238       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9239     }
9240   else
9241     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9242
9243   /* If in template, STMT is converted to a normal for-statement
9244      at instantiation. If not, it is done just ahead. */
9245   if (processing_template_decl)
9246     {
9247       stmt = begin_range_for_stmt (scope, init);
9248       finish_range_for_decl (stmt, range_decl, range_expr);
9249       if (!type_dependent_expression_p (range_expr)
9250           /* do_auto_deduction doesn't mess with template init-lists.  */
9251           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9252         do_range_for_auto_deduction (range_decl, range_expr);
9253     }
9254   else
9255     {
9256       stmt = begin_for_stmt (scope, init);
9257       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9258     }
9259   return stmt;
9260 }
9261
9262 /* Subroutine of cp_convert_range_for: given the initializer expression,
9263    builds up the range temporary.  */
9264
9265 static tree
9266 build_range_temp (tree range_expr)
9267 {
9268   tree range_type, range_temp;
9269
9270   /* Find out the type deduced by the declaration
9271      `auto &&__range = range_expr'.  */
9272   range_type = cp_build_reference_type (make_auto (), true);
9273   range_type = do_auto_deduction (range_type, range_expr,
9274                                   type_uses_auto (range_type));
9275
9276   /* Create the __range variable.  */
9277   range_temp = build_decl (input_location, VAR_DECL,
9278                            get_identifier ("__for_range"), range_type);
9279   TREE_USED (range_temp) = 1;
9280   DECL_ARTIFICIAL (range_temp) = 1;
9281
9282   return range_temp;
9283 }
9284
9285 /* Used by cp_parser_range_for in template context: we aren't going to
9286    do a full conversion yet, but we still need to resolve auto in the
9287    type of the for-range-declaration if present.  This is basically
9288    a shortcut version of cp_convert_range_for.  */
9289
9290 static void
9291 do_range_for_auto_deduction (tree decl, tree range_expr)
9292 {
9293   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9294   if (auto_node)
9295     {
9296       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9297       range_temp = convert_from_reference (build_range_temp (range_expr));
9298       iter_type = (cp_parser_perform_range_for_lookup
9299                    (range_temp, &begin_dummy, &end_dummy));
9300       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9301       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9302                                         tf_warning_or_error);
9303       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9304                                             iter_decl, auto_node);
9305     }
9306 }
9307
9308 /* Converts a range-based for-statement into a normal
9309    for-statement, as per the definition.
9310
9311       for (RANGE_DECL : RANGE_EXPR)
9312         BLOCK
9313
9314    should be equivalent to:
9315
9316       {
9317         auto &&__range = RANGE_EXPR;
9318         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9319               __begin != __end;
9320               ++__begin)
9321           {
9322               RANGE_DECL = *__begin;
9323               BLOCK
9324           }
9325       }
9326
9327    If RANGE_EXPR is an array:
9328         BEGIN_EXPR = __range
9329         END_EXPR = __range + ARRAY_SIZE(__range)
9330    Else if RANGE_EXPR has a member 'begin' or 'end':
9331         BEGIN_EXPR = __range.begin()
9332         END_EXPR = __range.end()
9333    Else:
9334         BEGIN_EXPR = begin(__range)
9335         END_EXPR = end(__range);
9336
9337    If __range has a member 'begin' but not 'end', or vice versa, we must
9338    still use the second alternative (it will surely fail, however).
9339    When calling begin()/end() in the third alternative we must use
9340    argument dependent lookup, but always considering 'std' as an associated
9341    namespace.  */
9342
9343 tree
9344 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9345 {
9346   tree begin, end;
9347   tree iter_type, begin_expr, end_expr;
9348   tree condition, expression;
9349
9350   if (range_decl == error_mark_node || range_expr == error_mark_node)
9351     /* If an error happened previously do nothing or else a lot of
9352        unhelpful errors would be issued.  */
9353     begin_expr = end_expr = iter_type = error_mark_node;
9354   else
9355     {
9356       tree range_temp = build_range_temp (range_expr);
9357       pushdecl (range_temp);
9358       cp_finish_decl (range_temp, range_expr,
9359                       /*is_constant_init*/false, NULL_TREE,
9360                       LOOKUP_ONLYCONVERTING);
9361
9362       range_temp = convert_from_reference (range_temp);
9363       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9364                                                       &begin_expr, &end_expr);
9365     }
9366
9367   /* The new for initialization statement.  */
9368   begin = build_decl (input_location, VAR_DECL,
9369                       get_identifier ("__for_begin"), iter_type);
9370   TREE_USED (begin) = 1;
9371   DECL_ARTIFICIAL (begin) = 1;
9372   pushdecl (begin);
9373   cp_finish_decl (begin, begin_expr,
9374                   /*is_constant_init*/false, NULL_TREE,
9375                   LOOKUP_ONLYCONVERTING);
9376
9377   end = build_decl (input_location, VAR_DECL,
9378                     get_identifier ("__for_end"), iter_type);
9379   TREE_USED (end) = 1;
9380   DECL_ARTIFICIAL (end) = 1;
9381   pushdecl (end);
9382   cp_finish_decl (end, end_expr,
9383                   /*is_constant_init*/false, NULL_TREE,
9384                   LOOKUP_ONLYCONVERTING);
9385
9386   finish_for_init_stmt (statement);
9387
9388   /* The new for condition.  */
9389   condition = build_x_binary_op (NE_EXPR,
9390                                  begin, ERROR_MARK,
9391                                  end, ERROR_MARK,
9392                                  NULL, tf_warning_or_error);
9393   finish_for_cond (condition, statement);
9394
9395   /* The new increment expression.  */
9396   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9397   finish_for_expr (expression, statement);
9398
9399   /* The declaration is initialized with *__begin inside the loop body.  */
9400   cp_finish_decl (range_decl,
9401                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9402                   /*is_constant_init*/false, NULL_TREE,
9403                   LOOKUP_ONLYCONVERTING);
9404
9405   return statement;
9406 }
9407
9408 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9409    We need to solve both at the same time because the method used
9410    depends on the existence of members begin or end.
9411    Returns the type deduced for the iterator expression.  */
9412
9413 static tree
9414 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9415 {
9416   if (error_operand_p (range))
9417     {
9418       *begin = *end = error_mark_node;
9419       return error_mark_node;
9420     }
9421
9422   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9423     {
9424       error ("range-based %<for%> expression of type %qT "
9425              "has incomplete type", TREE_TYPE (range));
9426       *begin = *end = error_mark_node;
9427       return error_mark_node;
9428     }
9429   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9430     {
9431       /* If RANGE is an array, we will use pointer arithmetic.  */
9432       *begin = range;
9433       *end = build_binary_op (input_location, PLUS_EXPR,
9434                               range,
9435                               array_type_nelts_top (TREE_TYPE (range)),
9436                               0);
9437       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9438     }
9439   else
9440     {
9441       /* If it is not an array, we must do a bit of magic.  */
9442       tree id_begin, id_end;
9443       tree member_begin, member_end;
9444
9445       *begin = *end = error_mark_node;
9446
9447       id_begin = get_identifier ("begin");
9448       id_end = get_identifier ("end");
9449       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9450                                     /*protect=*/2, /*want_type=*/false);
9451       member_end = lookup_member (TREE_TYPE (range), id_end,
9452                                   /*protect=*/2, /*want_type=*/false);
9453
9454       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9455         {
9456           /* Use the member functions.  */
9457           if (member_begin != NULL_TREE)
9458             *begin = cp_parser_range_for_member_function (range, id_begin);
9459           else
9460             error ("range-based %<for%> expression of type %qT has an "
9461                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9462
9463           if (member_end != NULL_TREE)
9464             *end = cp_parser_range_for_member_function (range, id_end);
9465           else
9466             error ("range-based %<for%> expression of type %qT has a "
9467                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9468         }
9469       else
9470         {
9471           /* Use global functions with ADL.  */
9472           VEC(tree,gc) *vec;
9473           vec = make_tree_vector ();
9474
9475           VEC_safe_push (tree, gc, vec, range);
9476
9477           member_begin = perform_koenig_lookup (id_begin, vec,
9478                                                 /*include_std=*/true,
9479                                                 tf_warning_or_error);
9480           *begin = finish_call_expr (member_begin, &vec, false, true,
9481                                      tf_warning_or_error);
9482           member_end = perform_koenig_lookup (id_end, vec,
9483                                               /*include_std=*/true,
9484                                               tf_warning_or_error);
9485           *end = finish_call_expr (member_end, &vec, false, true,
9486                                    tf_warning_or_error);
9487
9488           release_tree_vector (vec);
9489         }
9490
9491       /* Last common checks.  */
9492       if (*begin == error_mark_node || *end == error_mark_node)
9493         {
9494           /* If one of the expressions is an error do no more checks.  */
9495           *begin = *end = error_mark_node;
9496           return error_mark_node;
9497         }
9498       else
9499         {
9500           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9501           /* The unqualified type of the __begin and __end temporaries should
9502              be the same, as required by the multiple auto declaration.  */
9503           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9504             error ("inconsistent begin/end types in range-based %<for%> "
9505                    "statement: %qT and %qT",
9506                    TREE_TYPE (*begin), TREE_TYPE (*end));
9507           return iter_type;
9508         }
9509     }
9510 }
9511
9512 /* Helper function for cp_parser_perform_range_for_lookup.
9513    Builds a tree for RANGE.IDENTIFIER().  */
9514
9515 static tree
9516 cp_parser_range_for_member_function (tree range, tree identifier)
9517 {
9518   tree member, res;
9519   VEC(tree,gc) *vec;
9520
9521   member = finish_class_member_access_expr (range, identifier,
9522                                             false, tf_warning_or_error);
9523   if (member == error_mark_node)
9524     return error_mark_node;
9525
9526   vec = make_tree_vector ();
9527   res = finish_call_expr (member, &vec,
9528                           /*disallow_virtual=*/false,
9529                           /*koenig_p=*/false,
9530                           tf_warning_or_error);
9531   release_tree_vector (vec);
9532   return res;
9533 }
9534
9535 /* Parse an iteration-statement.
9536
9537    iteration-statement:
9538      while ( condition ) statement
9539      do statement while ( expression ) ;
9540      for ( for-init-statement condition [opt] ; expression [opt] )
9541        statement
9542
9543    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9544
9545 static tree
9546 cp_parser_iteration_statement (cp_parser* parser)
9547 {
9548   cp_token *token;
9549   enum rid keyword;
9550   tree statement;
9551   unsigned char in_statement;
9552
9553   /* Peek at the next token.  */
9554   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9555   if (!token)
9556     return error_mark_node;
9557
9558   /* Remember whether or not we are already within an iteration
9559      statement.  */
9560   in_statement = parser->in_statement;
9561
9562   /* See what kind of keyword it is.  */
9563   keyword = token->keyword;
9564   switch (keyword)
9565     {
9566     case RID_WHILE:
9567       {
9568         tree condition;
9569
9570         /* Begin the while-statement.  */
9571         statement = begin_while_stmt ();
9572         /* Look for the `('.  */
9573         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9574         /* Parse the condition.  */
9575         condition = cp_parser_condition (parser);
9576         finish_while_stmt_cond (condition, statement);
9577         /* Look for the `)'.  */
9578         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9579         /* Parse the dependent statement.  */
9580         parser->in_statement = IN_ITERATION_STMT;
9581         cp_parser_already_scoped_statement (parser);
9582         parser->in_statement = in_statement;
9583         /* We're done with the while-statement.  */
9584         finish_while_stmt (statement);
9585       }
9586       break;
9587
9588     case RID_DO:
9589       {
9590         tree expression;
9591
9592         /* Begin the do-statement.  */
9593         statement = begin_do_stmt ();
9594         /* Parse the body of the do-statement.  */
9595         parser->in_statement = IN_ITERATION_STMT;
9596         cp_parser_implicitly_scoped_statement (parser, NULL);
9597         parser->in_statement = in_statement;
9598         finish_do_body (statement);
9599         /* Look for the `while' keyword.  */
9600         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9601         /* Look for the `('.  */
9602         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9603         /* Parse the expression.  */
9604         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9605         /* We're done with the do-statement.  */
9606         finish_do_stmt (expression, statement);
9607         /* Look for the `)'.  */
9608         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9609         /* Look for the `;'.  */
9610         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9611       }
9612       break;
9613
9614     case RID_FOR:
9615       {
9616         /* Look for the `('.  */
9617         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9618
9619         statement = cp_parser_for (parser);
9620
9621         /* Look for the `)'.  */
9622         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9623
9624         /* Parse the body of the for-statement.  */
9625         parser->in_statement = IN_ITERATION_STMT;
9626         cp_parser_already_scoped_statement (parser);
9627         parser->in_statement = in_statement;
9628
9629         /* We're done with the for-statement.  */
9630         finish_for_stmt (statement);
9631       }
9632       break;
9633
9634     default:
9635       cp_parser_error (parser, "expected iteration-statement");
9636       statement = error_mark_node;
9637       break;
9638     }
9639
9640   return statement;
9641 }
9642
9643 /* Parse a for-init-statement or the declarator of a range-based-for.
9644    Returns true if a range-based-for declaration is seen.
9645
9646    for-init-statement:
9647      expression-statement
9648      simple-declaration  */
9649
9650 static bool
9651 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9652 {
9653   /* If the next token is a `;', then we have an empty
9654      expression-statement.  Grammatically, this is also a
9655      simple-declaration, but an invalid one, because it does not
9656      declare anything.  Therefore, if we did not handle this case
9657      specially, we would issue an error message about an invalid
9658      declaration.  */
9659   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9660     {
9661       bool is_range_for = false;
9662       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9663
9664       parser->colon_corrects_to_scope_p = false;
9665
9666       /* We're going to speculatively look for a declaration, falling back
9667          to an expression, if necessary.  */
9668       cp_parser_parse_tentatively (parser);
9669       /* Parse the declaration.  */
9670       cp_parser_simple_declaration (parser,
9671                                     /*function_definition_allowed_p=*/false,
9672                                     decl);
9673       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9674       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9675         {
9676           /* It is a range-for, consume the ':' */
9677           cp_lexer_consume_token (parser->lexer);
9678           is_range_for = true;
9679           if (cxx_dialect < cxx0x)
9680             {
9681               error_at (cp_lexer_peek_token (parser->lexer)->location,
9682                         "range-based %<for%> loops are not allowed "
9683                         "in C++98 mode");
9684               *decl = error_mark_node;
9685             }
9686         }
9687       else
9688           /* The ';' is not consumed yet because we told
9689              cp_parser_simple_declaration not to.  */
9690           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9691
9692       if (cp_parser_parse_definitely (parser))
9693         return is_range_for;
9694       /* If the tentative parse failed, then we shall need to look for an
9695          expression-statement.  */
9696     }
9697   /* If we are here, it is an expression-statement.  */
9698   cp_parser_expression_statement (parser, NULL_TREE);
9699   return false;
9700 }
9701
9702 /* Parse a jump-statement.
9703
9704    jump-statement:
9705      break ;
9706      continue ;
9707      return expression [opt] ;
9708      return braced-init-list ;
9709      goto identifier ;
9710
9711    GNU extension:
9712
9713    jump-statement:
9714      goto * expression ;
9715
9716    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9717
9718 static tree
9719 cp_parser_jump_statement (cp_parser* parser)
9720 {
9721   tree statement = error_mark_node;
9722   cp_token *token;
9723   enum rid keyword;
9724   unsigned char in_statement;
9725
9726   /* Peek at the next token.  */
9727   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9728   if (!token)
9729     return error_mark_node;
9730
9731   /* See what kind of keyword it is.  */
9732   keyword = token->keyword;
9733   switch (keyword)
9734     {
9735     case RID_BREAK:
9736       in_statement = parser->in_statement & ~IN_IF_STMT;      
9737       switch (in_statement)
9738         {
9739         case 0:
9740           error_at (token->location, "break statement not within loop or switch");
9741           break;
9742         default:
9743           gcc_assert ((in_statement & IN_SWITCH_STMT)
9744                       || in_statement == IN_ITERATION_STMT);
9745           statement = finish_break_stmt ();
9746           break;
9747         case IN_OMP_BLOCK:
9748           error_at (token->location, "invalid exit from OpenMP structured block");
9749           break;
9750         case IN_OMP_FOR:
9751           error_at (token->location, "break statement used with OpenMP for loop");
9752           break;
9753         }
9754       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9755       break;
9756
9757     case RID_CONTINUE:
9758       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9759         {
9760         case 0:
9761           error_at (token->location, "continue statement not within a loop");
9762           break;
9763         case IN_ITERATION_STMT:
9764         case IN_OMP_FOR:
9765           statement = finish_continue_stmt ();
9766           break;
9767         case IN_OMP_BLOCK:
9768           error_at (token->location, "invalid exit from OpenMP structured block");
9769           break;
9770         default:
9771           gcc_unreachable ();
9772         }
9773       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9774       break;
9775
9776     case RID_RETURN:
9777       {
9778         tree expr;
9779         bool expr_non_constant_p;
9780
9781         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9782           {
9783             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9784             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9785           }
9786         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9787           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9788         else
9789           /* If the next token is a `;', then there is no
9790              expression.  */
9791           expr = NULL_TREE;
9792         /* Build the return-statement.  */
9793         statement = finish_return_stmt (expr);
9794         /* Look for the final `;'.  */
9795         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9796       }
9797       break;
9798
9799     case RID_GOTO:
9800       /* Create the goto-statement.  */
9801       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9802         {
9803           /* Issue a warning about this use of a GNU extension.  */
9804           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9805           /* Consume the '*' token.  */
9806           cp_lexer_consume_token (parser->lexer);
9807           /* Parse the dependent expression.  */
9808           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9809         }
9810       else
9811         finish_goto_stmt (cp_parser_identifier (parser));
9812       /* Look for the final `;'.  */
9813       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9814       break;
9815
9816     default:
9817       cp_parser_error (parser, "expected jump-statement");
9818       break;
9819     }
9820
9821   return statement;
9822 }
9823
9824 /* Parse a declaration-statement.
9825
9826    declaration-statement:
9827      block-declaration  */
9828
9829 static void
9830 cp_parser_declaration_statement (cp_parser* parser)
9831 {
9832   void *p;
9833
9834   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9835   p = obstack_alloc (&declarator_obstack, 0);
9836
9837  /* Parse the block-declaration.  */
9838   cp_parser_block_declaration (parser, /*statement_p=*/true);
9839
9840   /* Free any declarators allocated.  */
9841   obstack_free (&declarator_obstack, p);
9842
9843   /* Finish off the statement.  */
9844   finish_stmt ();
9845 }
9846
9847 /* Some dependent statements (like `if (cond) statement'), are
9848    implicitly in their own scope.  In other words, if the statement is
9849    a single statement (as opposed to a compound-statement), it is
9850    none-the-less treated as if it were enclosed in braces.  Any
9851    declarations appearing in the dependent statement are out of scope
9852    after control passes that point.  This function parses a statement,
9853    but ensures that is in its own scope, even if it is not a
9854    compound-statement.
9855
9856    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9857    is a (possibly labeled) if statement which is not enclosed in
9858    braces and has an else clause.  This is used to implement
9859    -Wparentheses.
9860
9861    Returns the new statement.  */
9862
9863 static tree
9864 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9865 {
9866   tree statement;
9867
9868   if (if_p != NULL)
9869     *if_p = false;
9870
9871   /* Mark if () ; with a special NOP_EXPR.  */
9872   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9873     {
9874       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9875       cp_lexer_consume_token (parser->lexer);
9876       statement = add_stmt (build_empty_stmt (loc));
9877     }
9878   /* if a compound is opened, we simply parse the statement directly.  */
9879   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9880     statement = cp_parser_compound_statement (parser, NULL, false, false);
9881   /* If the token is not a `{', then we must take special action.  */
9882   else
9883     {
9884       /* Create a compound-statement.  */
9885       statement = begin_compound_stmt (0);
9886       /* Parse the dependent-statement.  */
9887       cp_parser_statement (parser, NULL_TREE, false, if_p);
9888       /* Finish the dummy compound-statement.  */
9889       finish_compound_stmt (statement);
9890     }
9891
9892   /* Return the statement.  */
9893   return statement;
9894 }
9895
9896 /* For some dependent statements (like `while (cond) statement'), we
9897    have already created a scope.  Therefore, even if the dependent
9898    statement is a compound-statement, we do not want to create another
9899    scope.  */
9900
9901 static void
9902 cp_parser_already_scoped_statement (cp_parser* parser)
9903 {
9904   /* If the token is a `{', then we must take special action.  */
9905   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9906     cp_parser_statement (parser, NULL_TREE, false, NULL);
9907   else
9908     {
9909       /* Avoid calling cp_parser_compound_statement, so that we
9910          don't create a new scope.  Do everything else by hand.  */
9911       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9912       /* If the next keyword is `__label__' we have a label declaration.  */
9913       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9914         cp_parser_label_declaration (parser);
9915       /* Parse an (optional) statement-seq.  */
9916       cp_parser_statement_seq_opt (parser, NULL_TREE);
9917       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9918     }
9919 }
9920
9921 /* Declarations [gram.dcl.dcl] */
9922
9923 /* Parse an optional declaration-sequence.
9924
9925    declaration-seq:
9926      declaration
9927      declaration-seq declaration  */
9928
9929 static void
9930 cp_parser_declaration_seq_opt (cp_parser* parser)
9931 {
9932   while (true)
9933     {
9934       cp_token *token;
9935
9936       token = cp_lexer_peek_token (parser->lexer);
9937
9938       if (token->type == CPP_CLOSE_BRACE
9939           || token->type == CPP_EOF
9940           || token->type == CPP_PRAGMA_EOL)
9941         break;
9942
9943       if (token->type == CPP_SEMICOLON)
9944         {
9945           /* A declaration consisting of a single semicolon is
9946              invalid.  Allow it unless we're being pedantic.  */
9947           cp_lexer_consume_token (parser->lexer);
9948           if (!in_system_header)
9949             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9950           continue;
9951         }
9952
9953       /* If we're entering or exiting a region that's implicitly
9954          extern "C", modify the lang context appropriately.  */
9955       if (!parser->implicit_extern_c && token->implicit_extern_c)
9956         {
9957           push_lang_context (lang_name_c);
9958           parser->implicit_extern_c = true;
9959         }
9960       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9961         {
9962           pop_lang_context ();
9963           parser->implicit_extern_c = false;
9964         }
9965
9966       if (token->type == CPP_PRAGMA)
9967         {
9968           /* A top-level declaration can consist solely of a #pragma.
9969              A nested declaration cannot, so this is done here and not
9970              in cp_parser_declaration.  (A #pragma at block scope is
9971              handled in cp_parser_statement.)  */
9972           cp_parser_pragma (parser, pragma_external);
9973           continue;
9974         }
9975
9976       /* Parse the declaration itself.  */
9977       cp_parser_declaration (parser);
9978     }
9979 }
9980
9981 /* Parse a declaration.
9982
9983    declaration:
9984      block-declaration
9985      function-definition
9986      template-declaration
9987      explicit-instantiation
9988      explicit-specialization
9989      linkage-specification
9990      namespace-definition
9991
9992    GNU extension:
9993
9994    declaration:
9995       __extension__ declaration */
9996
9997 static void
9998 cp_parser_declaration (cp_parser* parser)
9999 {
10000   cp_token token1;
10001   cp_token token2;
10002   int saved_pedantic;
10003   void *p;
10004   tree attributes = NULL_TREE;
10005
10006   /* Check for the `__extension__' keyword.  */
10007   if (cp_parser_extension_opt (parser, &saved_pedantic))
10008     {
10009       /* Parse the qualified declaration.  */
10010       cp_parser_declaration (parser);
10011       /* Restore the PEDANTIC flag.  */
10012       pedantic = saved_pedantic;
10013
10014       return;
10015     }
10016
10017   /* Try to figure out what kind of declaration is present.  */
10018   token1 = *cp_lexer_peek_token (parser->lexer);
10019
10020   if (token1.type != CPP_EOF)
10021     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10022   else
10023     {
10024       token2.type = CPP_EOF;
10025       token2.keyword = RID_MAX;
10026     }
10027
10028   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10029   p = obstack_alloc (&declarator_obstack, 0);
10030
10031   /* If the next token is `extern' and the following token is a string
10032      literal, then we have a linkage specification.  */
10033   if (token1.keyword == RID_EXTERN
10034       && cp_parser_is_pure_string_literal (&token2))
10035     cp_parser_linkage_specification (parser);
10036   /* If the next token is `template', then we have either a template
10037      declaration, an explicit instantiation, or an explicit
10038      specialization.  */
10039   else if (token1.keyword == RID_TEMPLATE)
10040     {
10041       /* `template <>' indicates a template specialization.  */
10042       if (token2.type == CPP_LESS
10043           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10044         cp_parser_explicit_specialization (parser);
10045       /* `template <' indicates a template declaration.  */
10046       else if (token2.type == CPP_LESS)
10047         cp_parser_template_declaration (parser, /*member_p=*/false);
10048       /* Anything else must be an explicit instantiation.  */
10049       else
10050         cp_parser_explicit_instantiation (parser);
10051     }
10052   /* If the next token is `export', then we have a template
10053      declaration.  */
10054   else if (token1.keyword == RID_EXPORT)
10055     cp_parser_template_declaration (parser, /*member_p=*/false);
10056   /* If the next token is `extern', 'static' or 'inline' and the one
10057      after that is `template', we have a GNU extended explicit
10058      instantiation directive.  */
10059   else if (cp_parser_allow_gnu_extensions_p (parser)
10060            && (token1.keyword == RID_EXTERN
10061                || token1.keyword == RID_STATIC
10062                || token1.keyword == RID_INLINE)
10063            && token2.keyword == RID_TEMPLATE)
10064     cp_parser_explicit_instantiation (parser);
10065   /* If the next token is `namespace', check for a named or unnamed
10066      namespace definition.  */
10067   else if (token1.keyword == RID_NAMESPACE
10068            && (/* A named namespace definition.  */
10069                (token2.type == CPP_NAME
10070                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10071                     != CPP_EQ))
10072                /* An unnamed namespace definition.  */
10073                || token2.type == CPP_OPEN_BRACE
10074                || token2.keyword == RID_ATTRIBUTE))
10075     cp_parser_namespace_definition (parser);
10076   /* An inline (associated) namespace definition.  */
10077   else if (token1.keyword == RID_INLINE
10078            && token2.keyword == RID_NAMESPACE)
10079     cp_parser_namespace_definition (parser);
10080   /* Objective-C++ declaration/definition.  */
10081   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10082     cp_parser_objc_declaration (parser, NULL_TREE);
10083   else if (c_dialect_objc ()
10084            && token1.keyword == RID_ATTRIBUTE
10085            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10086     cp_parser_objc_declaration (parser, attributes);
10087   /* We must have either a block declaration or a function
10088      definition.  */
10089   else
10090     /* Try to parse a block-declaration, or a function-definition.  */
10091     cp_parser_block_declaration (parser, /*statement_p=*/false);
10092
10093   /* Free any declarators allocated.  */
10094   obstack_free (&declarator_obstack, p);
10095 }
10096
10097 /* Parse a block-declaration.
10098
10099    block-declaration:
10100      simple-declaration
10101      asm-definition
10102      namespace-alias-definition
10103      using-declaration
10104      using-directive
10105
10106    GNU Extension:
10107
10108    block-declaration:
10109      __extension__ block-declaration
10110
10111    C++0x Extension:
10112
10113    block-declaration:
10114      static_assert-declaration
10115
10116    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10117    part of a declaration-statement.  */
10118
10119 static void
10120 cp_parser_block_declaration (cp_parser *parser,
10121                              bool      statement_p)
10122 {
10123   cp_token *token1;
10124   int saved_pedantic;
10125
10126   /* Check for the `__extension__' keyword.  */
10127   if (cp_parser_extension_opt (parser, &saved_pedantic))
10128     {
10129       /* Parse the qualified declaration.  */
10130       cp_parser_block_declaration (parser, statement_p);
10131       /* Restore the PEDANTIC flag.  */
10132       pedantic = saved_pedantic;
10133
10134       return;
10135     }
10136
10137   /* Peek at the next token to figure out which kind of declaration is
10138      present.  */
10139   token1 = cp_lexer_peek_token (parser->lexer);
10140
10141   /* If the next keyword is `asm', we have an asm-definition.  */
10142   if (token1->keyword == RID_ASM)
10143     {
10144       if (statement_p)
10145         cp_parser_commit_to_tentative_parse (parser);
10146       cp_parser_asm_definition (parser);
10147     }
10148   /* If the next keyword is `namespace', we have a
10149      namespace-alias-definition.  */
10150   else if (token1->keyword == RID_NAMESPACE)
10151     cp_parser_namespace_alias_definition (parser);
10152   /* If the next keyword is `using', we have either a
10153      using-declaration or a using-directive.  */
10154   else if (token1->keyword == RID_USING)
10155     {
10156       cp_token *token2;
10157
10158       if (statement_p)
10159         cp_parser_commit_to_tentative_parse (parser);
10160       /* If the token after `using' is `namespace', then we have a
10161          using-directive.  */
10162       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10163       if (token2->keyword == RID_NAMESPACE)
10164         cp_parser_using_directive (parser);
10165       /* Otherwise, it's a using-declaration.  */
10166       else
10167         cp_parser_using_declaration (parser,
10168                                      /*access_declaration_p=*/false);
10169     }
10170   /* If the next keyword is `__label__' we have a misplaced label
10171      declaration.  */
10172   else if (token1->keyword == RID_LABEL)
10173     {
10174       cp_lexer_consume_token (parser->lexer);
10175       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10176       cp_parser_skip_to_end_of_statement (parser);
10177       /* If the next token is now a `;', consume it.  */
10178       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10179         cp_lexer_consume_token (parser->lexer);
10180     }
10181   /* If the next token is `static_assert' we have a static assertion.  */
10182   else if (token1->keyword == RID_STATIC_ASSERT)
10183     cp_parser_static_assert (parser, /*member_p=*/false);
10184   /* Anything else must be a simple-declaration.  */
10185   else
10186     cp_parser_simple_declaration (parser, !statement_p,
10187                                   /*maybe_range_for_decl*/NULL);
10188 }
10189
10190 /* Parse a simple-declaration.
10191
10192    simple-declaration:
10193      decl-specifier-seq [opt] init-declarator-list [opt] ;
10194
10195    init-declarator-list:
10196      init-declarator
10197      init-declarator-list , init-declarator
10198
10199    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10200    function-definition as a simple-declaration.
10201
10202    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10203    parsed declaration if it is an uninitialized single declarator not followed
10204    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10205    if present, will not be consumed.  */
10206
10207 static void
10208 cp_parser_simple_declaration (cp_parser* parser,
10209                               bool function_definition_allowed_p,
10210                               tree *maybe_range_for_decl)
10211 {
10212   cp_decl_specifier_seq decl_specifiers;
10213   int declares_class_or_enum;
10214   bool saw_declarator;
10215
10216   if (maybe_range_for_decl)
10217     *maybe_range_for_decl = NULL_TREE;
10218
10219   /* Defer access checks until we know what is being declared; the
10220      checks for names appearing in the decl-specifier-seq should be
10221      done as if we were in the scope of the thing being declared.  */
10222   push_deferring_access_checks (dk_deferred);
10223
10224   /* Parse the decl-specifier-seq.  We have to keep track of whether
10225      or not the decl-specifier-seq declares a named class or
10226      enumeration type, since that is the only case in which the
10227      init-declarator-list is allowed to be empty.
10228
10229      [dcl.dcl]
10230
10231      In a simple-declaration, the optional init-declarator-list can be
10232      omitted only when declaring a class or enumeration, that is when
10233      the decl-specifier-seq contains either a class-specifier, an
10234      elaborated-type-specifier, or an enum-specifier.  */
10235   cp_parser_decl_specifier_seq (parser,
10236                                 CP_PARSER_FLAGS_OPTIONAL,
10237                                 &decl_specifiers,
10238                                 &declares_class_or_enum);
10239   /* We no longer need to defer access checks.  */
10240   stop_deferring_access_checks ();
10241
10242   /* In a block scope, a valid declaration must always have a
10243      decl-specifier-seq.  By not trying to parse declarators, we can
10244      resolve the declaration/expression ambiguity more quickly.  */
10245   if (!function_definition_allowed_p
10246       && !decl_specifiers.any_specifiers_p)
10247     {
10248       cp_parser_error (parser, "expected declaration");
10249       goto done;
10250     }
10251
10252   /* If the next two tokens are both identifiers, the code is
10253      erroneous. The usual cause of this situation is code like:
10254
10255        T t;
10256
10257      where "T" should name a type -- but does not.  */
10258   if (!decl_specifiers.any_type_specifiers_p
10259       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10260     {
10261       /* If parsing tentatively, we should commit; we really are
10262          looking at a declaration.  */
10263       cp_parser_commit_to_tentative_parse (parser);
10264       /* Give up.  */
10265       goto done;
10266     }
10267
10268   /* If we have seen at least one decl-specifier, and the next token
10269      is not a parenthesis, then we must be looking at a declaration.
10270      (After "int (" we might be looking at a functional cast.)  */
10271   if (decl_specifiers.any_specifiers_p
10272       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10273       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10274       && !cp_parser_error_occurred (parser))
10275     cp_parser_commit_to_tentative_parse (parser);
10276
10277   /* Keep going until we hit the `;' at the end of the simple
10278      declaration.  */
10279   saw_declarator = false;
10280   while (cp_lexer_next_token_is_not (parser->lexer,
10281                                      CPP_SEMICOLON))
10282     {
10283       cp_token *token;
10284       bool function_definition_p;
10285       tree decl;
10286
10287       if (saw_declarator)
10288         {
10289           /* If we are processing next declarator, coma is expected */
10290           token = cp_lexer_peek_token (parser->lexer);
10291           gcc_assert (token->type == CPP_COMMA);
10292           cp_lexer_consume_token (parser->lexer);
10293           if (maybe_range_for_decl)
10294             *maybe_range_for_decl = error_mark_node;
10295         }
10296       else
10297         saw_declarator = true;
10298
10299       /* Parse the init-declarator.  */
10300       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10301                                         /*checks=*/NULL,
10302                                         function_definition_allowed_p,
10303                                         /*member_p=*/false,
10304                                         declares_class_or_enum,
10305                                         &function_definition_p,
10306                                         maybe_range_for_decl);
10307       /* If an error occurred while parsing tentatively, exit quickly.
10308          (That usually happens when in the body of a function; each
10309          statement is treated as a declaration-statement until proven
10310          otherwise.)  */
10311       if (cp_parser_error_occurred (parser))
10312         goto done;
10313       /* Handle function definitions specially.  */
10314       if (function_definition_p)
10315         {
10316           /* If the next token is a `,', then we are probably
10317              processing something like:
10318
10319                void f() {}, *p;
10320
10321              which is erroneous.  */
10322           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10323             {
10324               cp_token *token = cp_lexer_peek_token (parser->lexer);
10325               error_at (token->location,
10326                         "mixing"
10327                         " declarations and function-definitions is forbidden");
10328             }
10329           /* Otherwise, we're done with the list of declarators.  */
10330           else
10331             {
10332               pop_deferring_access_checks ();
10333               return;
10334             }
10335         }
10336       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10337         *maybe_range_for_decl = decl;
10338       /* The next token should be either a `,' or a `;'.  */
10339       token = cp_lexer_peek_token (parser->lexer);
10340       /* If it's a `,', there are more declarators to come.  */
10341       if (token->type == CPP_COMMA)
10342         /* will be consumed next time around */;
10343       /* If it's a `;', we are done.  */
10344       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10345         break;
10346       /* Anything else is an error.  */
10347       else
10348         {
10349           /* If we have already issued an error message we don't need
10350              to issue another one.  */
10351           if (decl != error_mark_node
10352               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10353             cp_parser_error (parser, "expected %<,%> or %<;%>");
10354           /* Skip tokens until we reach the end of the statement.  */
10355           cp_parser_skip_to_end_of_statement (parser);
10356           /* If the next token is now a `;', consume it.  */
10357           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10358             cp_lexer_consume_token (parser->lexer);
10359           goto done;
10360         }
10361       /* After the first time around, a function-definition is not
10362          allowed -- even if it was OK at first.  For example:
10363
10364            int i, f() {}
10365
10366          is not valid.  */
10367       function_definition_allowed_p = false;
10368     }
10369
10370   /* Issue an error message if no declarators are present, and the
10371      decl-specifier-seq does not itself declare a class or
10372      enumeration.  */
10373   if (!saw_declarator)
10374     {
10375       if (cp_parser_declares_only_class_p (parser))
10376         shadow_tag (&decl_specifiers);
10377       /* Perform any deferred access checks.  */
10378       perform_deferred_access_checks ();
10379     }
10380
10381   /* Consume the `;'.  */
10382   if (!maybe_range_for_decl)
10383       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10384
10385  done:
10386   pop_deferring_access_checks ();
10387 }
10388
10389 /* Parse a decl-specifier-seq.
10390
10391    decl-specifier-seq:
10392      decl-specifier-seq [opt] decl-specifier
10393
10394    decl-specifier:
10395      storage-class-specifier
10396      type-specifier
10397      function-specifier
10398      friend
10399      typedef
10400
10401    GNU Extension:
10402
10403    decl-specifier:
10404      attributes
10405
10406    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10407
10408    The parser flags FLAGS is used to control type-specifier parsing.
10409
10410    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10411    flags:
10412
10413      1: one of the decl-specifiers is an elaborated-type-specifier
10414         (i.e., a type declaration)
10415      2: one of the decl-specifiers is an enum-specifier or a
10416         class-specifier (i.e., a type definition)
10417
10418    */
10419
10420 static void
10421 cp_parser_decl_specifier_seq (cp_parser* parser,
10422                               cp_parser_flags flags,
10423                               cp_decl_specifier_seq *decl_specs,
10424                               int* declares_class_or_enum)
10425 {
10426   bool constructor_possible_p = !parser->in_declarator_p;
10427   cp_token *start_token = NULL;
10428
10429   /* Clear DECL_SPECS.  */
10430   clear_decl_specs (decl_specs);
10431
10432   /* Assume no class or enumeration type is declared.  */
10433   *declares_class_or_enum = 0;
10434
10435   /* Keep reading specifiers until there are no more to read.  */
10436   while (true)
10437     {
10438       bool constructor_p;
10439       bool found_decl_spec;
10440       cp_token *token;
10441
10442       /* Peek at the next token.  */
10443       token = cp_lexer_peek_token (parser->lexer);
10444
10445       /* Save the first token of the decl spec list for error
10446          reporting.  */
10447       if (!start_token)
10448         start_token = token;
10449       /* Handle attributes.  */
10450       if (token->keyword == RID_ATTRIBUTE)
10451         {
10452           /* Parse the attributes.  */
10453           decl_specs->attributes
10454             = chainon (decl_specs->attributes,
10455                        cp_parser_attributes_opt (parser));
10456           continue;
10457         }
10458       /* Assume we will find a decl-specifier keyword.  */
10459       found_decl_spec = true;
10460       /* If the next token is an appropriate keyword, we can simply
10461          add it to the list.  */
10462       switch (token->keyword)
10463         {
10464           /* decl-specifier:
10465                friend
10466                constexpr */
10467         case RID_FRIEND:
10468           if (!at_class_scope_p ())
10469             {
10470               error_at (token->location, "%<friend%> used outside of class");
10471               cp_lexer_purge_token (parser->lexer);
10472             }
10473           else
10474             {
10475               ++decl_specs->specs[(int) ds_friend];
10476               /* Consume the token.  */
10477               cp_lexer_consume_token (parser->lexer);
10478             }
10479           break;
10480
10481         case RID_CONSTEXPR:
10482           ++decl_specs->specs[(int) ds_constexpr];
10483           cp_lexer_consume_token (parser->lexer);
10484           break;
10485
10486           /* function-specifier:
10487                inline
10488                virtual
10489                explicit  */
10490         case RID_INLINE:
10491         case RID_VIRTUAL:
10492         case RID_EXPLICIT:
10493           cp_parser_function_specifier_opt (parser, decl_specs);
10494           break;
10495
10496           /* decl-specifier:
10497                typedef  */
10498         case RID_TYPEDEF:
10499           ++decl_specs->specs[(int) ds_typedef];
10500           /* Consume the token.  */
10501           cp_lexer_consume_token (parser->lexer);
10502           /* A constructor declarator cannot appear in a typedef.  */
10503           constructor_possible_p = false;
10504           /* The "typedef" keyword can only occur in a declaration; we
10505              may as well commit at this point.  */
10506           cp_parser_commit_to_tentative_parse (parser);
10507
10508           if (decl_specs->storage_class != sc_none)
10509             decl_specs->conflicting_specifiers_p = true;
10510           break;
10511
10512           /* storage-class-specifier:
10513                auto
10514                register
10515                static
10516                extern
10517                mutable
10518
10519              GNU Extension:
10520                thread  */
10521         case RID_AUTO:
10522           if (cxx_dialect == cxx98) 
10523             {
10524               /* Consume the token.  */
10525               cp_lexer_consume_token (parser->lexer);
10526
10527               /* Complain about `auto' as a storage specifier, if
10528                  we're complaining about C++0x compatibility.  */
10529               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10530                           " will change meaning in C++0x; please remove it");
10531
10532               /* Set the storage class anyway.  */
10533               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10534                                            token->location);
10535             }
10536           else
10537             /* C++0x auto type-specifier.  */
10538             found_decl_spec = false;
10539           break;
10540
10541         case RID_REGISTER:
10542         case RID_STATIC:
10543         case RID_EXTERN:
10544         case RID_MUTABLE:
10545           /* Consume the token.  */
10546           cp_lexer_consume_token (parser->lexer);
10547           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10548                                        token->location);
10549           break;
10550         case RID_THREAD:
10551           /* Consume the token.  */
10552           cp_lexer_consume_token (parser->lexer);
10553           ++decl_specs->specs[(int) ds_thread];
10554           break;
10555
10556         default:
10557           /* We did not yet find a decl-specifier yet.  */
10558           found_decl_spec = false;
10559           break;
10560         }
10561
10562       if (found_decl_spec
10563           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10564           && token->keyword != RID_CONSTEXPR)
10565         error ("decl-specifier invalid in condition");
10566
10567       /* Constructors are a special case.  The `S' in `S()' is not a
10568          decl-specifier; it is the beginning of the declarator.  */
10569       constructor_p
10570         = (!found_decl_spec
10571            && constructor_possible_p
10572            && (cp_parser_constructor_declarator_p
10573                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10574
10575       /* If we don't have a DECL_SPEC yet, then we must be looking at
10576          a type-specifier.  */
10577       if (!found_decl_spec && !constructor_p)
10578         {
10579           int decl_spec_declares_class_or_enum;
10580           bool is_cv_qualifier;
10581           tree type_spec;
10582
10583           type_spec
10584             = cp_parser_type_specifier (parser, flags,
10585                                         decl_specs,
10586                                         /*is_declaration=*/true,
10587                                         &decl_spec_declares_class_or_enum,
10588                                         &is_cv_qualifier);
10589           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10590
10591           /* If this type-specifier referenced a user-defined type
10592              (a typedef, class-name, etc.), then we can't allow any
10593              more such type-specifiers henceforth.
10594
10595              [dcl.spec]
10596
10597              The longest sequence of decl-specifiers that could
10598              possibly be a type name is taken as the
10599              decl-specifier-seq of a declaration.  The sequence shall
10600              be self-consistent as described below.
10601
10602              [dcl.type]
10603
10604              As a general rule, at most one type-specifier is allowed
10605              in the complete decl-specifier-seq of a declaration.  The
10606              only exceptions are the following:
10607
10608              -- const or volatile can be combined with any other
10609                 type-specifier.
10610
10611              -- signed or unsigned can be combined with char, long,
10612                 short, or int.
10613
10614              -- ..
10615
10616              Example:
10617
10618                typedef char* Pc;
10619                void g (const int Pc);
10620
10621              Here, Pc is *not* part of the decl-specifier seq; it's
10622              the declarator.  Therefore, once we see a type-specifier
10623              (other than a cv-qualifier), we forbid any additional
10624              user-defined types.  We *do* still allow things like `int
10625              int' to be considered a decl-specifier-seq, and issue the
10626              error message later.  */
10627           if (type_spec && !is_cv_qualifier)
10628             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10629           /* A constructor declarator cannot follow a type-specifier.  */
10630           if (type_spec)
10631             {
10632               constructor_possible_p = false;
10633               found_decl_spec = true;
10634               if (!is_cv_qualifier)
10635                 decl_specs->any_type_specifiers_p = true;
10636             }
10637         }
10638
10639       /* If we still do not have a DECL_SPEC, then there are no more
10640          decl-specifiers.  */
10641       if (!found_decl_spec)
10642         break;
10643
10644       decl_specs->any_specifiers_p = true;
10645       /* After we see one decl-specifier, further decl-specifiers are
10646          always optional.  */
10647       flags |= CP_PARSER_FLAGS_OPTIONAL;
10648     }
10649
10650   cp_parser_check_decl_spec (decl_specs, start_token->location);
10651
10652   /* Don't allow a friend specifier with a class definition.  */
10653   if (decl_specs->specs[(int) ds_friend] != 0
10654       && (*declares_class_or_enum & 2))
10655     error_at (start_token->location,
10656               "class definition may not be declared a friend");
10657 }
10658
10659 /* Parse an (optional) storage-class-specifier.
10660
10661    storage-class-specifier:
10662      auto
10663      register
10664      static
10665      extern
10666      mutable
10667
10668    GNU Extension:
10669
10670    storage-class-specifier:
10671      thread
10672
10673    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10674
10675 static tree
10676 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10677 {
10678   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10679     {
10680     case RID_AUTO:
10681       if (cxx_dialect != cxx98)
10682         return NULL_TREE;
10683       /* Fall through for C++98.  */
10684
10685     case RID_REGISTER:
10686     case RID_STATIC:
10687     case RID_EXTERN:
10688     case RID_MUTABLE:
10689     case RID_THREAD:
10690       /* Consume the token.  */
10691       return cp_lexer_consume_token (parser->lexer)->u.value;
10692
10693     default:
10694       return NULL_TREE;
10695     }
10696 }
10697
10698 /* Parse an (optional) function-specifier.
10699
10700    function-specifier:
10701      inline
10702      virtual
10703      explicit
10704
10705    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10706    Updates DECL_SPECS, if it is non-NULL.  */
10707
10708 static tree
10709 cp_parser_function_specifier_opt (cp_parser* parser,
10710                                   cp_decl_specifier_seq *decl_specs)
10711 {
10712   cp_token *token = cp_lexer_peek_token (parser->lexer);
10713   switch (token->keyword)
10714     {
10715     case RID_INLINE:
10716       if (decl_specs)
10717         ++decl_specs->specs[(int) ds_inline];
10718       break;
10719
10720     case RID_VIRTUAL:
10721       /* 14.5.2.3 [temp.mem]
10722
10723          A member function template shall not be virtual.  */
10724       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10725         error_at (token->location, "templates may not be %<virtual%>");
10726       else if (decl_specs)
10727         ++decl_specs->specs[(int) ds_virtual];
10728       break;
10729
10730     case RID_EXPLICIT:
10731       if (decl_specs)
10732         ++decl_specs->specs[(int) ds_explicit];
10733       break;
10734
10735     default:
10736       return NULL_TREE;
10737     }
10738
10739   /* Consume the token.  */
10740   return cp_lexer_consume_token (parser->lexer)->u.value;
10741 }
10742
10743 /* Parse a linkage-specification.
10744
10745    linkage-specification:
10746      extern string-literal { declaration-seq [opt] }
10747      extern string-literal declaration  */
10748
10749 static void
10750 cp_parser_linkage_specification (cp_parser* parser)
10751 {
10752   tree linkage;
10753
10754   /* Look for the `extern' keyword.  */
10755   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10756
10757   /* Look for the string-literal.  */
10758   linkage = cp_parser_string_literal (parser, false, false);
10759
10760   /* Transform the literal into an identifier.  If the literal is a
10761      wide-character string, or contains embedded NULs, then we can't
10762      handle it as the user wants.  */
10763   if (strlen (TREE_STRING_POINTER (linkage))
10764       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10765     {
10766       cp_parser_error (parser, "invalid linkage-specification");
10767       /* Assume C++ linkage.  */
10768       linkage = lang_name_cplusplus;
10769     }
10770   else
10771     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10772
10773   /* We're now using the new linkage.  */
10774   push_lang_context (linkage);
10775
10776   /* If the next token is a `{', then we're using the first
10777      production.  */
10778   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10779     {
10780       /* Consume the `{' token.  */
10781       cp_lexer_consume_token (parser->lexer);
10782       /* Parse the declarations.  */
10783       cp_parser_declaration_seq_opt (parser);
10784       /* Look for the closing `}'.  */
10785       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10786     }
10787   /* Otherwise, there's just one declaration.  */
10788   else
10789     {
10790       bool saved_in_unbraced_linkage_specification_p;
10791
10792       saved_in_unbraced_linkage_specification_p
10793         = parser->in_unbraced_linkage_specification_p;
10794       parser->in_unbraced_linkage_specification_p = true;
10795       cp_parser_declaration (parser);
10796       parser->in_unbraced_linkage_specification_p
10797         = saved_in_unbraced_linkage_specification_p;
10798     }
10799
10800   /* We're done with the linkage-specification.  */
10801   pop_lang_context ();
10802 }
10803
10804 /* Parse a static_assert-declaration.
10805
10806    static_assert-declaration:
10807      static_assert ( constant-expression , string-literal ) ; 
10808
10809    If MEMBER_P, this static_assert is a class member.  */
10810
10811 static void 
10812 cp_parser_static_assert(cp_parser *parser, bool member_p)
10813 {
10814   tree condition;
10815   tree message;
10816   cp_token *token;
10817   location_t saved_loc;
10818   bool dummy;
10819
10820   /* Peek at the `static_assert' token so we can keep track of exactly
10821      where the static assertion started.  */
10822   token = cp_lexer_peek_token (parser->lexer);
10823   saved_loc = token->location;
10824
10825   /* Look for the `static_assert' keyword.  */
10826   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10827                                   RT_STATIC_ASSERT))
10828     return;
10829
10830   /*  We know we are in a static assertion; commit to any tentative
10831       parse.  */
10832   if (cp_parser_parsing_tentatively (parser))
10833     cp_parser_commit_to_tentative_parse (parser);
10834
10835   /* Parse the `(' starting the static assertion condition.  */
10836   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10837
10838   /* Parse the constant-expression.  Allow a non-constant expression
10839      here in order to give better diagnostics in finish_static_assert.  */
10840   condition = 
10841     cp_parser_constant_expression (parser,
10842                                    /*allow_non_constant_p=*/true,
10843                                    /*non_constant_p=*/&dummy);
10844
10845   /* Parse the separating `,'.  */
10846   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10847
10848   /* Parse the string-literal message.  */
10849   message = cp_parser_string_literal (parser, 
10850                                       /*translate=*/false,
10851                                       /*wide_ok=*/true);
10852
10853   /* A `)' completes the static assertion.  */
10854   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10855     cp_parser_skip_to_closing_parenthesis (parser, 
10856                                            /*recovering=*/true, 
10857                                            /*or_comma=*/false,
10858                                            /*consume_paren=*/true);
10859
10860   /* A semicolon terminates the declaration.  */
10861   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10862
10863   /* Complete the static assertion, which may mean either processing 
10864      the static assert now or saving it for template instantiation.  */
10865   finish_static_assert (condition, message, saved_loc, member_p);
10866 }
10867
10868 /* Parse a `decltype' type. Returns the type. 
10869
10870    simple-type-specifier:
10871      decltype ( expression )  */
10872
10873 static tree
10874 cp_parser_decltype (cp_parser *parser)
10875 {
10876   tree expr;
10877   bool id_expression_or_member_access_p = false;
10878   const char *saved_message;
10879   bool saved_integral_constant_expression_p;
10880   bool saved_non_integral_constant_expression_p;
10881   cp_token *id_expr_start_token;
10882   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10883
10884   if (start_token->type == CPP_DECLTYPE)
10885     {
10886       /* Already parsed.  */
10887       cp_lexer_consume_token (parser->lexer);
10888       return start_token->u.value;
10889     }
10890
10891   /* Look for the `decltype' token.  */
10892   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10893     return error_mark_node;
10894
10895   /* Types cannot be defined in a `decltype' expression.  Save away the
10896      old message.  */
10897   saved_message = parser->type_definition_forbidden_message;
10898
10899   /* And create the new one.  */
10900   parser->type_definition_forbidden_message
10901     = G_("types may not be defined in %<decltype%> expressions");
10902
10903   /* The restrictions on constant-expressions do not apply inside
10904      decltype expressions.  */
10905   saved_integral_constant_expression_p
10906     = parser->integral_constant_expression_p;
10907   saved_non_integral_constant_expression_p
10908     = parser->non_integral_constant_expression_p;
10909   parser->integral_constant_expression_p = false;
10910
10911   /* Do not actually evaluate the expression.  */
10912   ++cp_unevaluated_operand;
10913
10914   /* Do not warn about problems with the expression.  */
10915   ++c_inhibit_evaluation_warnings;
10916
10917   /* Parse the opening `('.  */
10918   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10919     return error_mark_node;
10920   
10921   /* First, try parsing an id-expression.  */
10922   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10923   cp_parser_parse_tentatively (parser);
10924   expr = cp_parser_id_expression (parser,
10925                                   /*template_keyword_p=*/false,
10926                                   /*check_dependency_p=*/true,
10927                                   /*template_p=*/NULL,
10928                                   /*declarator_p=*/false,
10929                                   /*optional_p=*/false);
10930
10931   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10932     {
10933       bool non_integral_constant_expression_p = false;
10934       tree id_expression = expr;
10935       cp_id_kind idk;
10936       const char *error_msg;
10937
10938       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10939         /* Lookup the name we got back from the id-expression.  */
10940         expr = cp_parser_lookup_name (parser, expr,
10941                                       none_type,
10942                                       /*is_template=*/false,
10943                                       /*is_namespace=*/false,
10944                                       /*check_dependency=*/true,
10945                                       /*ambiguous_decls=*/NULL,
10946                                       id_expr_start_token->location);
10947
10948       if (expr
10949           && expr != error_mark_node
10950           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10951           && TREE_CODE (expr) != TYPE_DECL
10952           && (TREE_CODE (expr) != BIT_NOT_EXPR
10953               || !TYPE_P (TREE_OPERAND (expr, 0)))
10954           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10955         {
10956           /* Complete lookup of the id-expression.  */
10957           expr = (finish_id_expression
10958                   (id_expression, expr, parser->scope, &idk,
10959                    /*integral_constant_expression_p=*/false,
10960                    /*allow_non_integral_constant_expression_p=*/true,
10961                    &non_integral_constant_expression_p,
10962                    /*template_p=*/false,
10963                    /*done=*/true,
10964                    /*address_p=*/false,
10965                    /*template_arg_p=*/false,
10966                    &error_msg,
10967                    id_expr_start_token->location));
10968
10969           if (expr == error_mark_node)
10970             /* We found an id-expression, but it was something that we
10971                should not have found. This is an error, not something
10972                we can recover from, so note that we found an
10973                id-expression and we'll recover as gracefully as
10974                possible.  */
10975             id_expression_or_member_access_p = true;
10976         }
10977
10978       if (expr 
10979           && expr != error_mark_node
10980           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10981         /* We have an id-expression.  */
10982         id_expression_or_member_access_p = true;
10983     }
10984
10985   if (!id_expression_or_member_access_p)
10986     {
10987       /* Abort the id-expression parse.  */
10988       cp_parser_abort_tentative_parse (parser);
10989
10990       /* Parsing tentatively, again.  */
10991       cp_parser_parse_tentatively (parser);
10992
10993       /* Parse a class member access.  */
10994       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10995                                            /*cast_p=*/false,
10996                                            /*member_access_only_p=*/true, NULL);
10997
10998       if (expr 
10999           && expr != error_mark_node
11000           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11001         /* We have an id-expression.  */
11002         id_expression_or_member_access_p = true;
11003     }
11004
11005   if (id_expression_or_member_access_p)
11006     /* We have parsed the complete id-expression or member access.  */
11007     cp_parser_parse_definitely (parser);
11008   else
11009     {
11010       bool saved_greater_than_is_operator_p;
11011
11012       /* Abort our attempt to parse an id-expression or member access
11013          expression.  */
11014       cp_parser_abort_tentative_parse (parser);
11015
11016       /* Within a parenthesized expression, a `>' token is always
11017          the greater-than operator.  */
11018       saved_greater_than_is_operator_p
11019         = parser->greater_than_is_operator_p;
11020       parser->greater_than_is_operator_p = true;
11021
11022       /* Parse a full expression.  */
11023       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11024
11025       /* The `>' token might be the end of a template-id or
11026          template-parameter-list now.  */
11027       parser->greater_than_is_operator_p
11028         = saved_greater_than_is_operator_p;
11029     }
11030
11031   /* Go back to evaluating expressions.  */
11032   --cp_unevaluated_operand;
11033   --c_inhibit_evaluation_warnings;
11034
11035   /* Restore the old message and the integral constant expression
11036      flags.  */
11037   parser->type_definition_forbidden_message = saved_message;
11038   parser->integral_constant_expression_p
11039     = saved_integral_constant_expression_p;
11040   parser->non_integral_constant_expression_p
11041     = saved_non_integral_constant_expression_p;
11042
11043   /* Parse to the closing `)'.  */
11044   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11045     {
11046       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11047                                              /*consume_paren=*/true);
11048       return error_mark_node;
11049     }
11050
11051   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11052                                tf_warning_or_error);
11053
11054   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11055      it again.  */
11056   start_token->type = CPP_DECLTYPE;
11057   start_token->u.value = expr;
11058   start_token->keyword = RID_MAX;
11059   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11060
11061   return expr;
11062 }
11063
11064 /* Special member functions [gram.special] */
11065
11066 /* Parse a conversion-function-id.
11067
11068    conversion-function-id:
11069      operator conversion-type-id
11070
11071    Returns an IDENTIFIER_NODE representing the operator.  */
11072
11073 static tree
11074 cp_parser_conversion_function_id (cp_parser* parser)
11075 {
11076   tree type;
11077   tree saved_scope;
11078   tree saved_qualifying_scope;
11079   tree saved_object_scope;
11080   tree pushed_scope = NULL_TREE;
11081
11082   /* Look for the `operator' token.  */
11083   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11084     return error_mark_node;
11085   /* When we parse the conversion-type-id, the current scope will be
11086      reset.  However, we need that information in able to look up the
11087      conversion function later, so we save it here.  */
11088   saved_scope = parser->scope;
11089   saved_qualifying_scope = parser->qualifying_scope;
11090   saved_object_scope = parser->object_scope;
11091   /* We must enter the scope of the class so that the names of
11092      entities declared within the class are available in the
11093      conversion-type-id.  For example, consider:
11094
11095        struct S {
11096          typedef int I;
11097          operator I();
11098        };
11099
11100        S::operator I() { ... }
11101
11102      In order to see that `I' is a type-name in the definition, we
11103      must be in the scope of `S'.  */
11104   if (saved_scope)
11105     pushed_scope = push_scope (saved_scope);
11106   /* Parse the conversion-type-id.  */
11107   type = cp_parser_conversion_type_id (parser);
11108   /* Leave the scope of the class, if any.  */
11109   if (pushed_scope)
11110     pop_scope (pushed_scope);
11111   /* Restore the saved scope.  */
11112   parser->scope = saved_scope;
11113   parser->qualifying_scope = saved_qualifying_scope;
11114   parser->object_scope = saved_object_scope;
11115   /* If the TYPE is invalid, indicate failure.  */
11116   if (type == error_mark_node)
11117     return error_mark_node;
11118   return mangle_conv_op_name_for_type (type);
11119 }
11120
11121 /* Parse a conversion-type-id:
11122
11123    conversion-type-id:
11124      type-specifier-seq conversion-declarator [opt]
11125
11126    Returns the TYPE specified.  */
11127
11128 static tree
11129 cp_parser_conversion_type_id (cp_parser* parser)
11130 {
11131   tree attributes;
11132   cp_decl_specifier_seq type_specifiers;
11133   cp_declarator *declarator;
11134   tree type_specified;
11135
11136   /* Parse the attributes.  */
11137   attributes = cp_parser_attributes_opt (parser);
11138   /* Parse the type-specifiers.  */
11139   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11140                                 /*is_trailing_return=*/false,
11141                                 &type_specifiers);
11142   /* If that didn't work, stop.  */
11143   if (type_specifiers.type == error_mark_node)
11144     return error_mark_node;
11145   /* Parse the conversion-declarator.  */
11146   declarator = cp_parser_conversion_declarator_opt (parser);
11147
11148   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11149                                     /*initialized=*/0, &attributes);
11150   if (attributes)
11151     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11152
11153   /* Don't give this error when parsing tentatively.  This happens to
11154      work because we always parse this definitively once.  */
11155   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11156       && type_uses_auto (type_specified))
11157     {
11158       error ("invalid use of %<auto%> in conversion operator");
11159       return error_mark_node;
11160     }
11161
11162   return type_specified;
11163 }
11164
11165 /* Parse an (optional) conversion-declarator.
11166
11167    conversion-declarator:
11168      ptr-operator conversion-declarator [opt]
11169
11170    */
11171
11172 static cp_declarator *
11173 cp_parser_conversion_declarator_opt (cp_parser* parser)
11174 {
11175   enum tree_code code;
11176   tree class_type;
11177   cp_cv_quals cv_quals;
11178
11179   /* We don't know if there's a ptr-operator next, or not.  */
11180   cp_parser_parse_tentatively (parser);
11181   /* Try the ptr-operator.  */
11182   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11183   /* If it worked, look for more conversion-declarators.  */
11184   if (cp_parser_parse_definitely (parser))
11185     {
11186       cp_declarator *declarator;
11187
11188       /* Parse another optional declarator.  */
11189       declarator = cp_parser_conversion_declarator_opt (parser);
11190
11191       return cp_parser_make_indirect_declarator
11192         (code, class_type, cv_quals, declarator);
11193    }
11194
11195   return NULL;
11196 }
11197
11198 /* Parse an (optional) ctor-initializer.
11199
11200    ctor-initializer:
11201      : mem-initializer-list
11202
11203    Returns TRUE iff the ctor-initializer was actually present.  */
11204
11205 static bool
11206 cp_parser_ctor_initializer_opt (cp_parser* parser)
11207 {
11208   /* If the next token is not a `:', then there is no
11209      ctor-initializer.  */
11210   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11211     {
11212       /* Do default initialization of any bases and members.  */
11213       if (DECL_CONSTRUCTOR_P (current_function_decl))
11214         finish_mem_initializers (NULL_TREE);
11215
11216       return false;
11217     }
11218
11219   /* Consume the `:' token.  */
11220   cp_lexer_consume_token (parser->lexer);
11221   /* And the mem-initializer-list.  */
11222   cp_parser_mem_initializer_list (parser);
11223
11224   return true;
11225 }
11226
11227 /* Parse a mem-initializer-list.
11228
11229    mem-initializer-list:
11230      mem-initializer ... [opt]
11231      mem-initializer ... [opt] , mem-initializer-list  */
11232
11233 static void
11234 cp_parser_mem_initializer_list (cp_parser* parser)
11235 {
11236   tree mem_initializer_list = NULL_TREE;
11237   cp_token *token = cp_lexer_peek_token (parser->lexer);
11238
11239   /* Let the semantic analysis code know that we are starting the
11240      mem-initializer-list.  */
11241   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11242     error_at (token->location,
11243               "only constructors take member initializers");
11244
11245   /* Loop through the list.  */
11246   while (true)
11247     {
11248       tree mem_initializer;
11249
11250       token = cp_lexer_peek_token (parser->lexer);
11251       /* Parse the mem-initializer.  */
11252       mem_initializer = cp_parser_mem_initializer (parser);
11253       /* If the next token is a `...', we're expanding member initializers. */
11254       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11255         {
11256           /* Consume the `...'. */
11257           cp_lexer_consume_token (parser->lexer);
11258
11259           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11260              can be expanded but members cannot. */
11261           if (mem_initializer != error_mark_node
11262               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11263             {
11264               error_at (token->location,
11265                         "cannot expand initializer for member %<%D%>",
11266                         TREE_PURPOSE (mem_initializer));
11267               mem_initializer = error_mark_node;
11268             }
11269
11270           /* Construct the pack expansion type. */
11271           if (mem_initializer != error_mark_node)
11272             mem_initializer = make_pack_expansion (mem_initializer);
11273         }
11274       /* Add it to the list, unless it was erroneous.  */
11275       if (mem_initializer != error_mark_node)
11276         {
11277           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11278           mem_initializer_list = mem_initializer;
11279         }
11280       /* If the next token is not a `,', we're done.  */
11281       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11282         break;
11283       /* Consume the `,' token.  */
11284       cp_lexer_consume_token (parser->lexer);
11285     }
11286
11287   /* Perform semantic analysis.  */
11288   if (DECL_CONSTRUCTOR_P (current_function_decl))
11289     finish_mem_initializers (mem_initializer_list);
11290 }
11291
11292 /* Parse a mem-initializer.
11293
11294    mem-initializer:
11295      mem-initializer-id ( expression-list [opt] )
11296      mem-initializer-id braced-init-list
11297
11298    GNU extension:
11299
11300    mem-initializer:
11301      ( expression-list [opt] )
11302
11303    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11304    class) or FIELD_DECL (for a non-static data member) to initialize;
11305    the TREE_VALUE is the expression-list.  An empty initialization
11306    list is represented by void_list_node.  */
11307
11308 static tree
11309 cp_parser_mem_initializer (cp_parser* parser)
11310 {
11311   tree mem_initializer_id;
11312   tree expression_list;
11313   tree member;
11314   cp_token *token = cp_lexer_peek_token (parser->lexer);
11315
11316   /* Find out what is being initialized.  */
11317   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11318     {
11319       permerror (token->location,
11320                  "anachronistic old-style base class initializer");
11321       mem_initializer_id = NULL_TREE;
11322     }
11323   else
11324     {
11325       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11326       if (mem_initializer_id == error_mark_node)
11327         return mem_initializer_id;
11328     }
11329   member = expand_member_init (mem_initializer_id);
11330   if (member && !DECL_P (member))
11331     in_base_initializer = 1;
11332
11333   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11334     {
11335       bool expr_non_constant_p;
11336       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11337       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11338       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11339       expression_list = build_tree_list (NULL_TREE, expression_list);
11340     }
11341   else
11342     {
11343       VEC(tree,gc)* vec;
11344       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11345                                                      /*cast_p=*/false,
11346                                                      /*allow_expansion_p=*/true,
11347                                                      /*non_constant_p=*/NULL);
11348       if (vec == NULL)
11349         return error_mark_node;
11350       expression_list = build_tree_list_vec (vec);
11351       release_tree_vector (vec);
11352     }
11353
11354   if (expression_list == error_mark_node)
11355     return error_mark_node;
11356   if (!expression_list)
11357     expression_list = void_type_node;
11358
11359   in_base_initializer = 0;
11360
11361   return member ? build_tree_list (member, expression_list) : error_mark_node;
11362 }
11363
11364 /* Parse a mem-initializer-id.
11365
11366    mem-initializer-id:
11367      :: [opt] nested-name-specifier [opt] class-name
11368      identifier
11369
11370    Returns a TYPE indicating the class to be initializer for the first
11371    production.  Returns an IDENTIFIER_NODE indicating the data member
11372    to be initialized for the second production.  */
11373
11374 static tree
11375 cp_parser_mem_initializer_id (cp_parser* parser)
11376 {
11377   bool global_scope_p;
11378   bool nested_name_specifier_p;
11379   bool template_p = false;
11380   tree id;
11381
11382   cp_token *token = cp_lexer_peek_token (parser->lexer);
11383
11384   /* `typename' is not allowed in this context ([temp.res]).  */
11385   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11386     {
11387       error_at (token->location, 
11388                 "keyword %<typename%> not allowed in this context (a qualified "
11389                 "member initializer is implicitly a type)");
11390       cp_lexer_consume_token (parser->lexer);
11391     }
11392   /* Look for the optional `::' operator.  */
11393   global_scope_p
11394     = (cp_parser_global_scope_opt (parser,
11395                                    /*current_scope_valid_p=*/false)
11396        != NULL_TREE);
11397   /* Look for the optional nested-name-specifier.  The simplest way to
11398      implement:
11399
11400        [temp.res]
11401
11402        The keyword `typename' is not permitted in a base-specifier or
11403        mem-initializer; in these contexts a qualified name that
11404        depends on a template-parameter is implicitly assumed to be a
11405        type name.
11406
11407      is to assume that we have seen the `typename' keyword at this
11408      point.  */
11409   nested_name_specifier_p
11410     = (cp_parser_nested_name_specifier_opt (parser,
11411                                             /*typename_keyword_p=*/true,
11412                                             /*check_dependency_p=*/true,
11413                                             /*type_p=*/true,
11414                                             /*is_declaration=*/true)
11415        != NULL_TREE);
11416   if (nested_name_specifier_p)
11417     template_p = cp_parser_optional_template_keyword (parser);
11418   /* If there is a `::' operator or a nested-name-specifier, then we
11419      are definitely looking for a class-name.  */
11420   if (global_scope_p || nested_name_specifier_p)
11421     return cp_parser_class_name (parser,
11422                                  /*typename_keyword_p=*/true,
11423                                  /*template_keyword_p=*/template_p,
11424                                  typename_type,
11425                                  /*check_dependency_p=*/true,
11426                                  /*class_head_p=*/false,
11427                                  /*is_declaration=*/true);
11428   /* Otherwise, we could also be looking for an ordinary identifier.  */
11429   cp_parser_parse_tentatively (parser);
11430   /* Try a class-name.  */
11431   id = cp_parser_class_name (parser,
11432                              /*typename_keyword_p=*/true,
11433                              /*template_keyword_p=*/false,
11434                              none_type,
11435                              /*check_dependency_p=*/true,
11436                              /*class_head_p=*/false,
11437                              /*is_declaration=*/true);
11438   /* If we found one, we're done.  */
11439   if (cp_parser_parse_definitely (parser))
11440     return id;
11441   /* Otherwise, look for an ordinary identifier.  */
11442   return cp_parser_identifier (parser);
11443 }
11444
11445 /* Overloading [gram.over] */
11446
11447 /* Parse an operator-function-id.
11448
11449    operator-function-id:
11450      operator operator
11451
11452    Returns an IDENTIFIER_NODE for the operator which is a
11453    human-readable spelling of the identifier, e.g., `operator +'.  */
11454
11455 static tree
11456 cp_parser_operator_function_id (cp_parser* parser)
11457 {
11458   /* Look for the `operator' keyword.  */
11459   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11460     return error_mark_node;
11461   /* And then the name of the operator itself.  */
11462   return cp_parser_operator (parser);
11463 }
11464
11465 /* Return an identifier node for a user-defined literal operator.
11466    The suffix identifier is chained to the operator name identifier.  */
11467
11468 static tree
11469 cp_literal_operator_id (const char* name)
11470 {
11471   tree identifier;
11472   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11473                               + strlen (name) + 10);
11474   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11475   identifier = get_identifier (buffer);
11476   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11477
11478   return identifier;
11479 }
11480
11481 /* Parse an operator.
11482
11483    operator:
11484      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11485      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11486      || ++ -- , ->* -> () []
11487
11488    GNU Extensions:
11489
11490    operator:
11491      <? >? <?= >?=
11492
11493    Returns an IDENTIFIER_NODE for the operator which is a
11494    human-readable spelling of the identifier, e.g., `operator +'.  */
11495
11496 static tree
11497 cp_parser_operator (cp_parser* parser)
11498 {
11499   tree id = NULL_TREE;
11500   cp_token *token;
11501
11502   /* Peek at the next token.  */
11503   token = cp_lexer_peek_token (parser->lexer);
11504   /* Figure out which operator we have.  */
11505   switch (token->type)
11506     {
11507     case CPP_KEYWORD:
11508       {
11509         enum tree_code op;
11510
11511         /* The keyword should be either `new' or `delete'.  */
11512         if (token->keyword == RID_NEW)
11513           op = NEW_EXPR;
11514         else if (token->keyword == RID_DELETE)
11515           op = DELETE_EXPR;
11516         else
11517           break;
11518
11519         /* Consume the `new' or `delete' token.  */
11520         cp_lexer_consume_token (parser->lexer);
11521
11522         /* Peek at the next token.  */
11523         token = cp_lexer_peek_token (parser->lexer);
11524         /* If it's a `[' token then this is the array variant of the
11525            operator.  */
11526         if (token->type == CPP_OPEN_SQUARE)
11527           {
11528             /* Consume the `[' token.  */
11529             cp_lexer_consume_token (parser->lexer);
11530             /* Look for the `]' token.  */
11531             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11532             id = ansi_opname (op == NEW_EXPR
11533                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11534           }
11535         /* Otherwise, we have the non-array variant.  */
11536         else
11537           id = ansi_opname (op);
11538
11539         return id;
11540       }
11541
11542     case CPP_PLUS:
11543       id = ansi_opname (PLUS_EXPR);
11544       break;
11545
11546     case CPP_MINUS:
11547       id = ansi_opname (MINUS_EXPR);
11548       break;
11549
11550     case CPP_MULT:
11551       id = ansi_opname (MULT_EXPR);
11552       break;
11553
11554     case CPP_DIV:
11555       id = ansi_opname (TRUNC_DIV_EXPR);
11556       break;
11557
11558     case CPP_MOD:
11559       id = ansi_opname (TRUNC_MOD_EXPR);
11560       break;
11561
11562     case CPP_XOR:
11563       id = ansi_opname (BIT_XOR_EXPR);
11564       break;
11565
11566     case CPP_AND:
11567       id = ansi_opname (BIT_AND_EXPR);
11568       break;
11569
11570     case CPP_OR:
11571       id = ansi_opname (BIT_IOR_EXPR);
11572       break;
11573
11574     case CPP_COMPL:
11575       id = ansi_opname (BIT_NOT_EXPR);
11576       break;
11577
11578     case CPP_NOT:
11579       id = ansi_opname (TRUTH_NOT_EXPR);
11580       break;
11581
11582     case CPP_EQ:
11583       id = ansi_assopname (NOP_EXPR);
11584       break;
11585
11586     case CPP_LESS:
11587       id = ansi_opname (LT_EXPR);
11588       break;
11589
11590     case CPP_GREATER:
11591       id = ansi_opname (GT_EXPR);
11592       break;
11593
11594     case CPP_PLUS_EQ:
11595       id = ansi_assopname (PLUS_EXPR);
11596       break;
11597
11598     case CPP_MINUS_EQ:
11599       id = ansi_assopname (MINUS_EXPR);
11600       break;
11601
11602     case CPP_MULT_EQ:
11603       id = ansi_assopname (MULT_EXPR);
11604       break;
11605
11606     case CPP_DIV_EQ:
11607       id = ansi_assopname (TRUNC_DIV_EXPR);
11608       break;
11609
11610     case CPP_MOD_EQ:
11611       id = ansi_assopname (TRUNC_MOD_EXPR);
11612       break;
11613
11614     case CPP_XOR_EQ:
11615       id = ansi_assopname (BIT_XOR_EXPR);
11616       break;
11617
11618     case CPP_AND_EQ:
11619       id = ansi_assopname (BIT_AND_EXPR);
11620       break;
11621
11622     case CPP_OR_EQ:
11623       id = ansi_assopname (BIT_IOR_EXPR);
11624       break;
11625
11626     case CPP_LSHIFT:
11627       id = ansi_opname (LSHIFT_EXPR);
11628       break;
11629
11630     case CPP_RSHIFT:
11631       id = ansi_opname (RSHIFT_EXPR);
11632       break;
11633
11634     case CPP_LSHIFT_EQ:
11635       id = ansi_assopname (LSHIFT_EXPR);
11636       break;
11637
11638     case CPP_RSHIFT_EQ:
11639       id = ansi_assopname (RSHIFT_EXPR);
11640       break;
11641
11642     case CPP_EQ_EQ:
11643       id = ansi_opname (EQ_EXPR);
11644       break;
11645
11646     case CPP_NOT_EQ:
11647       id = ansi_opname (NE_EXPR);
11648       break;
11649
11650     case CPP_LESS_EQ:
11651       id = ansi_opname (LE_EXPR);
11652       break;
11653
11654     case CPP_GREATER_EQ:
11655       id = ansi_opname (GE_EXPR);
11656       break;
11657
11658     case CPP_AND_AND:
11659       id = ansi_opname (TRUTH_ANDIF_EXPR);
11660       break;
11661
11662     case CPP_OR_OR:
11663       id = ansi_opname (TRUTH_ORIF_EXPR);
11664       break;
11665
11666     case CPP_PLUS_PLUS:
11667       id = ansi_opname (POSTINCREMENT_EXPR);
11668       break;
11669
11670     case CPP_MINUS_MINUS:
11671       id = ansi_opname (PREDECREMENT_EXPR);
11672       break;
11673
11674     case CPP_COMMA:
11675       id = ansi_opname (COMPOUND_EXPR);
11676       break;
11677
11678     case CPP_DEREF_STAR:
11679       id = ansi_opname (MEMBER_REF);
11680       break;
11681
11682     case CPP_DEREF:
11683       id = ansi_opname (COMPONENT_REF);
11684       break;
11685
11686     case CPP_OPEN_PAREN:
11687       /* Consume the `('.  */
11688       cp_lexer_consume_token (parser->lexer);
11689       /* Look for the matching `)'.  */
11690       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11691       return ansi_opname (CALL_EXPR);
11692
11693     case CPP_OPEN_SQUARE:
11694       /* Consume the `['.  */
11695       cp_lexer_consume_token (parser->lexer);
11696       /* Look for the matching `]'.  */
11697       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11698       return ansi_opname (ARRAY_REF);
11699
11700     case CPP_STRING:
11701       if (cxx_dialect == cxx98)
11702         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11703       if (TREE_STRING_LENGTH (token->u.value) > 2)
11704         {
11705           error ("expected empty string after %<operator%> keyword");
11706           return error_mark_node;
11707         }
11708       /* Consume the string.  */
11709       cp_lexer_consume_token (parser->lexer);
11710       /* Look for the suffix identifier.  */
11711       token = cp_lexer_peek_token (parser->lexer);
11712       if (token->type == CPP_NAME)
11713         {
11714           id = cp_parser_identifier (parser);
11715           if (id != error_mark_node)
11716             {
11717               const char *name = IDENTIFIER_POINTER (id);
11718               return cp_literal_operator_id (name);
11719             }
11720         }
11721       else
11722         {
11723           error ("expected suffix identifier");
11724           return error_mark_node;
11725         }
11726
11727     case CPP_STRING_USERDEF:
11728       error ("missing space between %<\"\"%> and suffix identifier");
11729       return error_mark_node;
11730
11731     default:
11732       /* Anything else is an error.  */
11733       break;
11734     }
11735
11736   /* If we have selected an identifier, we need to consume the
11737      operator token.  */
11738   if (id)
11739     cp_lexer_consume_token (parser->lexer);
11740   /* Otherwise, no valid operator name was present.  */
11741   else
11742     {
11743       cp_parser_error (parser, "expected operator");
11744       id = error_mark_node;
11745     }
11746
11747   return id;
11748 }
11749
11750 /* Parse a template-declaration.
11751
11752    template-declaration:
11753      export [opt] template < template-parameter-list > declaration
11754
11755    If MEMBER_P is TRUE, this template-declaration occurs within a
11756    class-specifier.
11757
11758    The grammar rule given by the standard isn't correct.  What
11759    is really meant is:
11760
11761    template-declaration:
11762      export [opt] template-parameter-list-seq
11763        decl-specifier-seq [opt] init-declarator [opt] ;
11764      export [opt] template-parameter-list-seq
11765        function-definition
11766
11767    template-parameter-list-seq:
11768      template-parameter-list-seq [opt]
11769      template < template-parameter-list >  */
11770
11771 static void
11772 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11773 {
11774   /* Check for `export'.  */
11775   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11776     {
11777       /* Consume the `export' token.  */
11778       cp_lexer_consume_token (parser->lexer);
11779       /* Warn that we do not support `export'.  */
11780       warning (0, "keyword %<export%> not implemented, and will be ignored");
11781     }
11782
11783   cp_parser_template_declaration_after_export (parser, member_p);
11784 }
11785
11786 /* Parse a template-parameter-list.
11787
11788    template-parameter-list:
11789      template-parameter
11790      template-parameter-list , template-parameter
11791
11792    Returns a TREE_LIST.  Each node represents a template parameter.
11793    The nodes are connected via their TREE_CHAINs.  */
11794
11795 static tree
11796 cp_parser_template_parameter_list (cp_parser* parser)
11797 {
11798   tree parameter_list = NULL_TREE;
11799
11800   begin_template_parm_list ();
11801
11802   /* The loop below parses the template parms.  We first need to know
11803      the total number of template parms to be able to compute proper
11804      canonical types of each dependent type. So after the loop, when
11805      we know the total number of template parms,
11806      end_template_parm_list computes the proper canonical types and
11807      fixes up the dependent types accordingly.  */
11808   while (true)
11809     {
11810       tree parameter;
11811       bool is_non_type;
11812       bool is_parameter_pack;
11813       location_t parm_loc;
11814
11815       /* Parse the template-parameter.  */
11816       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11817       parameter = cp_parser_template_parameter (parser, 
11818                                                 &is_non_type,
11819                                                 &is_parameter_pack);
11820       /* Add it to the list.  */
11821       if (parameter != error_mark_node)
11822         parameter_list = process_template_parm (parameter_list,
11823                                                 parm_loc,
11824                                                 parameter,
11825                                                 is_non_type,
11826                                                 is_parameter_pack,
11827                                                 0);
11828       else
11829        {
11830          tree err_parm = build_tree_list (parameter, parameter);
11831          parameter_list = chainon (parameter_list, err_parm);
11832        }
11833
11834       /* If the next token is not a `,', we're done.  */
11835       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11836         break;
11837       /* Otherwise, consume the `,' token.  */
11838       cp_lexer_consume_token (parser->lexer);
11839     }
11840
11841   return end_template_parm_list (parameter_list);
11842 }
11843
11844 /* Parse a template-parameter.
11845
11846    template-parameter:
11847      type-parameter
11848      parameter-declaration
11849
11850    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11851    the parameter.  The TREE_PURPOSE is the default value, if any.
11852    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11853    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11854    set to true iff this parameter is a parameter pack. */
11855
11856 static tree
11857 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11858                               bool *is_parameter_pack)
11859 {
11860   cp_token *token;
11861   cp_parameter_declarator *parameter_declarator;
11862   cp_declarator *id_declarator;
11863   tree parm;
11864
11865   /* Assume it is a type parameter or a template parameter.  */
11866   *is_non_type = false;
11867   /* Assume it not a parameter pack. */
11868   *is_parameter_pack = false;
11869   /* Peek at the next token.  */
11870   token = cp_lexer_peek_token (parser->lexer);
11871   /* If it is `class' or `template', we have a type-parameter.  */
11872   if (token->keyword == RID_TEMPLATE)
11873     return cp_parser_type_parameter (parser, is_parameter_pack);
11874   /* If it is `class' or `typename' we do not know yet whether it is a
11875      type parameter or a non-type parameter.  Consider:
11876
11877        template <typename T, typename T::X X> ...
11878
11879      or:
11880
11881        template <class C, class D*> ...
11882
11883      Here, the first parameter is a type parameter, and the second is
11884      a non-type parameter.  We can tell by looking at the token after
11885      the identifier -- if it is a `,', `=', or `>' then we have a type
11886      parameter.  */
11887   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11888     {
11889       /* Peek at the token after `class' or `typename'.  */
11890       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11891       /* If it's an ellipsis, we have a template type parameter
11892          pack. */
11893       if (token->type == CPP_ELLIPSIS)
11894         return cp_parser_type_parameter (parser, is_parameter_pack);
11895       /* If it's an identifier, skip it.  */
11896       if (token->type == CPP_NAME)
11897         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11898       /* Now, see if the token looks like the end of a template
11899          parameter.  */
11900       if (token->type == CPP_COMMA
11901           || token->type == CPP_EQ
11902           || token->type == CPP_GREATER)
11903         return cp_parser_type_parameter (parser, is_parameter_pack);
11904     }
11905
11906   /* Otherwise, it is a non-type parameter.
11907
11908      [temp.param]
11909
11910      When parsing a default template-argument for a non-type
11911      template-parameter, the first non-nested `>' is taken as the end
11912      of the template parameter-list rather than a greater-than
11913      operator.  */
11914   *is_non_type = true;
11915   parameter_declarator
11916      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11917                                         /*parenthesized_p=*/NULL);
11918
11919   /* If the parameter declaration is marked as a parameter pack, set
11920      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11921      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11922      grokdeclarator. */
11923   if (parameter_declarator
11924       && parameter_declarator->declarator
11925       && parameter_declarator->declarator->parameter_pack_p)
11926     {
11927       *is_parameter_pack = true;
11928       parameter_declarator->declarator->parameter_pack_p = false;
11929     }
11930
11931   /* If the next token is an ellipsis, and we don't already have it
11932      marked as a parameter pack, then we have a parameter pack (that
11933      has no declarator).  */
11934   if (!*is_parameter_pack
11935       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11936       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11937     {
11938       /* Consume the `...'.  */
11939       cp_lexer_consume_token (parser->lexer);
11940       maybe_warn_variadic_templates ();
11941       
11942       *is_parameter_pack = true;
11943     }
11944   /* We might end up with a pack expansion as the type of the non-type
11945      template parameter, in which case this is a non-type template
11946      parameter pack.  */
11947   else if (parameter_declarator
11948            && parameter_declarator->decl_specifiers.type
11949            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11950     {
11951       *is_parameter_pack = true;
11952       parameter_declarator->decl_specifiers.type = 
11953         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11954     }
11955
11956   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11957     {
11958       /* Parameter packs cannot have default arguments.  However, a
11959          user may try to do so, so we'll parse them and give an
11960          appropriate diagnostic here.  */
11961
11962       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11963       
11964       /* Find the name of the parameter pack.  */     
11965       id_declarator = parameter_declarator->declarator;
11966       while (id_declarator && id_declarator->kind != cdk_id)
11967         id_declarator = id_declarator->declarator;
11968       
11969       if (id_declarator && id_declarator->kind == cdk_id)
11970         error_at (start_token->location,
11971                   "template parameter pack %qD cannot have a default argument",
11972                   id_declarator->u.id.unqualified_name);
11973       else
11974         error_at (start_token->location,
11975                   "template parameter pack cannot have a default argument");
11976       
11977       /* Parse the default argument, but throw away the result.  */
11978       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11979     }
11980
11981   parm = grokdeclarator (parameter_declarator->declarator,
11982                          &parameter_declarator->decl_specifiers,
11983                          TPARM, /*initialized=*/0,
11984                          /*attrlist=*/NULL);
11985   if (parm == error_mark_node)
11986     return error_mark_node;
11987
11988   return build_tree_list (parameter_declarator->default_argument, parm);
11989 }
11990
11991 /* Parse a type-parameter.
11992
11993    type-parameter:
11994      class identifier [opt]
11995      class identifier [opt] = type-id
11996      typename identifier [opt]
11997      typename identifier [opt] = type-id
11998      template < template-parameter-list > class identifier [opt]
11999      template < template-parameter-list > class identifier [opt]
12000        = id-expression
12001
12002    GNU Extension (variadic templates):
12003
12004    type-parameter:
12005      class ... identifier [opt]
12006      typename ... identifier [opt]
12007
12008    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12009    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12010    the declaration of the parameter.
12011
12012    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12013
12014 static tree
12015 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12016 {
12017   cp_token *token;
12018   tree parameter;
12019
12020   /* Look for a keyword to tell us what kind of parameter this is.  */
12021   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12022   if (!token)
12023     return error_mark_node;
12024
12025   switch (token->keyword)
12026     {
12027     case RID_CLASS:
12028     case RID_TYPENAME:
12029       {
12030         tree identifier;
12031         tree default_argument;
12032
12033         /* If the next token is an ellipsis, we have a template
12034            argument pack. */
12035         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12036           {
12037             /* Consume the `...' token. */
12038             cp_lexer_consume_token (parser->lexer);
12039             maybe_warn_variadic_templates ();
12040
12041             *is_parameter_pack = true;
12042           }
12043
12044         /* If the next token is an identifier, then it names the
12045            parameter.  */
12046         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12047           identifier = cp_parser_identifier (parser);
12048         else
12049           identifier = NULL_TREE;
12050
12051         /* Create the parameter.  */
12052         parameter = finish_template_type_parm (class_type_node, identifier);
12053
12054         /* If the next token is an `=', we have a default argument.  */
12055         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12056           {
12057             /* Consume the `=' token.  */
12058             cp_lexer_consume_token (parser->lexer);
12059             /* Parse the default-argument.  */
12060             push_deferring_access_checks (dk_no_deferred);
12061             default_argument = cp_parser_type_id (parser);
12062
12063             /* Template parameter packs cannot have default
12064                arguments. */
12065             if (*is_parameter_pack)
12066               {
12067                 if (identifier)
12068                   error_at (token->location,
12069                             "template parameter pack %qD cannot have a "
12070                             "default argument", identifier);
12071                 else
12072                   error_at (token->location,
12073                             "template parameter packs cannot have "
12074                             "default arguments");
12075                 default_argument = NULL_TREE;
12076               }
12077             pop_deferring_access_checks ();
12078           }
12079         else
12080           default_argument = NULL_TREE;
12081
12082         /* Create the combined representation of the parameter and the
12083            default argument.  */
12084         parameter = build_tree_list (default_argument, parameter);
12085       }
12086       break;
12087
12088     case RID_TEMPLATE:
12089       {
12090         tree identifier;
12091         tree default_argument;
12092
12093         /* Look for the `<'.  */
12094         cp_parser_require (parser, CPP_LESS, RT_LESS);
12095         /* Parse the template-parameter-list.  */
12096         cp_parser_template_parameter_list (parser);
12097         /* Look for the `>'.  */
12098         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12099         /* Look for the `class' keyword.  */
12100         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12101         /* If the next token is an ellipsis, we have a template
12102            argument pack. */
12103         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12104           {
12105             /* Consume the `...' token. */
12106             cp_lexer_consume_token (parser->lexer);
12107             maybe_warn_variadic_templates ();
12108
12109             *is_parameter_pack = true;
12110           }
12111         /* If the next token is an `=', then there is a
12112            default-argument.  If the next token is a `>', we are at
12113            the end of the parameter-list.  If the next token is a `,',
12114            then we are at the end of this parameter.  */
12115         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12116             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12117             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12118           {
12119             identifier = cp_parser_identifier (parser);
12120             /* Treat invalid names as if the parameter were nameless.  */
12121             if (identifier == error_mark_node)
12122               identifier = NULL_TREE;
12123           }
12124         else
12125           identifier = NULL_TREE;
12126
12127         /* Create the template parameter.  */
12128         parameter = finish_template_template_parm (class_type_node,
12129                                                    identifier);
12130
12131         /* If the next token is an `=', then there is a
12132            default-argument.  */
12133         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12134           {
12135             bool is_template;
12136
12137             /* Consume the `='.  */
12138             cp_lexer_consume_token (parser->lexer);
12139             /* Parse the id-expression.  */
12140             push_deferring_access_checks (dk_no_deferred);
12141             /* save token before parsing the id-expression, for error
12142                reporting */
12143             token = cp_lexer_peek_token (parser->lexer);
12144             default_argument
12145               = cp_parser_id_expression (parser,
12146                                          /*template_keyword_p=*/false,
12147                                          /*check_dependency_p=*/true,
12148                                          /*template_p=*/&is_template,
12149                                          /*declarator_p=*/false,
12150                                          /*optional_p=*/false);
12151             if (TREE_CODE (default_argument) == TYPE_DECL)
12152               /* If the id-expression was a template-id that refers to
12153                  a template-class, we already have the declaration here,
12154                  so no further lookup is needed.  */
12155                  ;
12156             else
12157               /* Look up the name.  */
12158               default_argument
12159                 = cp_parser_lookup_name (parser, default_argument,
12160                                          none_type,
12161                                          /*is_template=*/is_template,
12162                                          /*is_namespace=*/false,
12163                                          /*check_dependency=*/true,
12164                                          /*ambiguous_decls=*/NULL,
12165                                          token->location);
12166             /* See if the default argument is valid.  */
12167             default_argument
12168               = check_template_template_default_arg (default_argument);
12169
12170             /* Template parameter packs cannot have default
12171                arguments. */
12172             if (*is_parameter_pack)
12173               {
12174                 if (identifier)
12175                   error_at (token->location,
12176                             "template parameter pack %qD cannot "
12177                             "have a default argument",
12178                             identifier);
12179                 else
12180                   error_at (token->location, "template parameter packs cannot "
12181                             "have default arguments");
12182                 default_argument = NULL_TREE;
12183               }
12184             pop_deferring_access_checks ();
12185           }
12186         else
12187           default_argument = NULL_TREE;
12188
12189         /* Create the combined representation of the parameter and the
12190            default argument.  */
12191         parameter = build_tree_list (default_argument, parameter);
12192       }
12193       break;
12194
12195     default:
12196       gcc_unreachable ();
12197       break;
12198     }
12199
12200   return parameter;
12201 }
12202
12203 /* Parse a template-id.
12204
12205    template-id:
12206      template-name < template-argument-list [opt] >
12207
12208    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12209    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12210    returned.  Otherwise, if the template-name names a function, or set
12211    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12212    names a class, returns a TYPE_DECL for the specialization.
12213
12214    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12215    uninstantiated templates.  */
12216
12217 static tree
12218 cp_parser_template_id (cp_parser *parser,
12219                        bool template_keyword_p,
12220                        bool check_dependency_p,
12221                        bool is_declaration)
12222 {
12223   int i;
12224   tree templ;
12225   tree arguments;
12226   tree template_id;
12227   cp_token_position start_of_id = 0;
12228   deferred_access_check *chk;
12229   VEC (deferred_access_check,gc) *access_check;
12230   cp_token *next_token = NULL, *next_token_2 = NULL;
12231   bool is_identifier;
12232
12233   /* If the next token corresponds to a template-id, there is no need
12234      to reparse it.  */
12235   next_token = cp_lexer_peek_token (parser->lexer);
12236   if (next_token->type == CPP_TEMPLATE_ID)
12237     {
12238       struct tree_check *check_value;
12239
12240       /* Get the stored value.  */
12241       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12242       /* Perform any access checks that were deferred.  */
12243       access_check = check_value->checks;
12244       if (access_check)
12245         {
12246           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12247             perform_or_defer_access_check (chk->binfo,
12248                                            chk->decl,
12249                                            chk->diag_decl);
12250         }
12251       /* Return the stored value.  */
12252       return check_value->value;
12253     }
12254
12255   /* Avoid performing name lookup if there is no possibility of
12256      finding a template-id.  */
12257   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12258       || (next_token->type == CPP_NAME
12259           && !cp_parser_nth_token_starts_template_argument_list_p
12260                (parser, 2)))
12261     {
12262       cp_parser_error (parser, "expected template-id");
12263       return error_mark_node;
12264     }
12265
12266   /* Remember where the template-id starts.  */
12267   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12268     start_of_id = cp_lexer_token_position (parser->lexer, false);
12269
12270   push_deferring_access_checks (dk_deferred);
12271
12272   /* Parse the template-name.  */
12273   is_identifier = false;
12274   templ = cp_parser_template_name (parser, template_keyword_p,
12275                                    check_dependency_p,
12276                                    is_declaration,
12277                                    &is_identifier);
12278   if (templ == error_mark_node || is_identifier)
12279     {
12280       pop_deferring_access_checks ();
12281       return templ;
12282     }
12283
12284   /* If we find the sequence `[:' after a template-name, it's probably
12285      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12286      parse correctly the argument list.  */
12287   next_token = cp_lexer_peek_token (parser->lexer);
12288   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12289   if (next_token->type == CPP_OPEN_SQUARE
12290       && next_token->flags & DIGRAPH
12291       && next_token_2->type == CPP_COLON
12292       && !(next_token_2->flags & PREV_WHITE))
12293     {
12294       cp_parser_parse_tentatively (parser);
12295       /* Change `:' into `::'.  */
12296       next_token_2->type = CPP_SCOPE;
12297       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12298          CPP_LESS.  */
12299       cp_lexer_consume_token (parser->lexer);
12300
12301       /* Parse the arguments.  */
12302       arguments = cp_parser_enclosed_template_argument_list (parser);
12303       if (!cp_parser_parse_definitely (parser))
12304         {
12305           /* If we couldn't parse an argument list, then we revert our changes
12306              and return simply an error. Maybe this is not a template-id
12307              after all.  */
12308           next_token_2->type = CPP_COLON;
12309           cp_parser_error (parser, "expected %<<%>");
12310           pop_deferring_access_checks ();
12311           return error_mark_node;
12312         }
12313       /* Otherwise, emit an error about the invalid digraph, but continue
12314          parsing because we got our argument list.  */
12315       if (permerror (next_token->location,
12316                      "%<<::%> cannot begin a template-argument list"))
12317         {
12318           static bool hint = false;
12319           inform (next_token->location,
12320                   "%<<:%> is an alternate spelling for %<[%>."
12321                   " Insert whitespace between %<<%> and %<::%>");
12322           if (!hint && !flag_permissive)
12323             {
12324               inform (next_token->location, "(if you use %<-fpermissive%>"
12325                       " G++ will accept your code)");
12326               hint = true;
12327             }
12328         }
12329     }
12330   else
12331     {
12332       /* Look for the `<' that starts the template-argument-list.  */
12333       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12334         {
12335           pop_deferring_access_checks ();
12336           return error_mark_node;
12337         }
12338       /* Parse the arguments.  */
12339       arguments = cp_parser_enclosed_template_argument_list (parser);
12340     }
12341
12342   /* Build a representation of the specialization.  */
12343   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12344     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12345   else if (DECL_CLASS_TEMPLATE_P (templ)
12346            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12347     {
12348       bool entering_scope;
12349       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12350          template (rather than some instantiation thereof) only if
12351          is not nested within some other construct.  For example, in
12352          "template <typename T> void f(T) { A<T>::", A<T> is just an
12353          instantiation of A.  */
12354       entering_scope = (template_parm_scope_p ()
12355                         && cp_lexer_next_token_is (parser->lexer,
12356                                                    CPP_SCOPE));
12357       template_id
12358         = finish_template_type (templ, arguments, entering_scope);
12359     }
12360   else
12361     {
12362       /* If it's not a class-template or a template-template, it should be
12363          a function-template.  */
12364       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12365                    || TREE_CODE (templ) == OVERLOAD
12366                    || BASELINK_P (templ)));
12367
12368       template_id = lookup_template_function (templ, arguments);
12369     }
12370
12371   /* If parsing tentatively, replace the sequence of tokens that makes
12372      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12373      should we re-parse the token stream, we will not have to repeat
12374      the effort required to do the parse, nor will we issue duplicate
12375      error messages about problems during instantiation of the
12376      template.  */
12377   if (start_of_id)
12378     {
12379       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12380
12381       /* Reset the contents of the START_OF_ID token.  */
12382       token->type = CPP_TEMPLATE_ID;
12383       /* Retrieve any deferred checks.  Do not pop this access checks yet
12384          so the memory will not be reclaimed during token replacing below.  */
12385       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12386       token->u.tree_check_value->value = template_id;
12387       token->u.tree_check_value->checks = get_deferred_access_checks ();
12388       token->keyword = RID_MAX;
12389
12390       /* Purge all subsequent tokens.  */
12391       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12392
12393       /* ??? Can we actually assume that, if template_id ==
12394          error_mark_node, we will have issued a diagnostic to the
12395          user, as opposed to simply marking the tentative parse as
12396          failed?  */
12397       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12398         error_at (token->location, "parse error in template argument list");
12399     }
12400
12401   pop_deferring_access_checks ();
12402   return template_id;
12403 }
12404
12405 /* Parse a template-name.
12406
12407    template-name:
12408      identifier
12409
12410    The standard should actually say:
12411
12412    template-name:
12413      identifier
12414      operator-function-id
12415
12416    A defect report has been filed about this issue.
12417
12418    A conversion-function-id cannot be a template name because they cannot
12419    be part of a template-id. In fact, looking at this code:
12420
12421    a.operator K<int>()
12422
12423    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12424    It is impossible to call a templated conversion-function-id with an
12425    explicit argument list, since the only allowed template parameter is
12426    the type to which it is converting.
12427
12428    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12429    `template' keyword, in a construction like:
12430
12431      T::template f<3>()
12432
12433    In that case `f' is taken to be a template-name, even though there
12434    is no way of knowing for sure.
12435
12436    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12437    name refers to a set of overloaded functions, at least one of which
12438    is a template, or an IDENTIFIER_NODE with the name of the template,
12439    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12440    names are looked up inside uninstantiated templates.  */
12441
12442 static tree
12443 cp_parser_template_name (cp_parser* parser,
12444                          bool template_keyword_p,
12445                          bool check_dependency_p,
12446                          bool is_declaration,
12447                          bool *is_identifier)
12448 {
12449   tree identifier;
12450   tree decl;
12451   tree fns;
12452   cp_token *token = cp_lexer_peek_token (parser->lexer);
12453
12454   /* If the next token is `operator', then we have either an
12455      operator-function-id or a conversion-function-id.  */
12456   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12457     {
12458       /* We don't know whether we're looking at an
12459          operator-function-id or a conversion-function-id.  */
12460       cp_parser_parse_tentatively (parser);
12461       /* Try an operator-function-id.  */
12462       identifier = cp_parser_operator_function_id (parser);
12463       /* If that didn't work, try a conversion-function-id.  */
12464       if (!cp_parser_parse_definitely (parser))
12465         {
12466           cp_parser_error (parser, "expected template-name");
12467           return error_mark_node;
12468         }
12469     }
12470   /* Look for the identifier.  */
12471   else
12472     identifier = cp_parser_identifier (parser);
12473
12474   /* If we didn't find an identifier, we don't have a template-id.  */
12475   if (identifier == error_mark_node)
12476     return error_mark_node;
12477
12478   /* If the name immediately followed the `template' keyword, then it
12479      is a template-name.  However, if the next token is not `<', then
12480      we do not treat it as a template-name, since it is not being used
12481      as part of a template-id.  This enables us to handle constructs
12482      like:
12483
12484        template <typename T> struct S { S(); };
12485        template <typename T> S<T>::S();
12486
12487      correctly.  We would treat `S' as a template -- if it were `S<T>'
12488      -- but we do not if there is no `<'.  */
12489
12490   if (processing_template_decl
12491       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12492     {
12493       /* In a declaration, in a dependent context, we pretend that the
12494          "template" keyword was present in order to improve error
12495          recovery.  For example, given:
12496
12497            template <typename T> void f(T::X<int>);
12498
12499          we want to treat "X<int>" as a template-id.  */
12500       if (is_declaration
12501           && !template_keyword_p
12502           && parser->scope && TYPE_P (parser->scope)
12503           && check_dependency_p
12504           && dependent_scope_p (parser->scope)
12505           /* Do not do this for dtors (or ctors), since they never
12506              need the template keyword before their name.  */
12507           && !constructor_name_p (identifier, parser->scope))
12508         {
12509           cp_token_position start = 0;
12510
12511           /* Explain what went wrong.  */
12512           error_at (token->location, "non-template %qD used as template",
12513                     identifier);
12514           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12515                   parser->scope, identifier);
12516           /* If parsing tentatively, find the location of the "<" token.  */
12517           if (cp_parser_simulate_error (parser))
12518             start = cp_lexer_token_position (parser->lexer, true);
12519           /* Parse the template arguments so that we can issue error
12520              messages about them.  */
12521           cp_lexer_consume_token (parser->lexer);
12522           cp_parser_enclosed_template_argument_list (parser);
12523           /* Skip tokens until we find a good place from which to
12524              continue parsing.  */
12525           cp_parser_skip_to_closing_parenthesis (parser,
12526                                                  /*recovering=*/true,
12527                                                  /*or_comma=*/true,
12528                                                  /*consume_paren=*/false);
12529           /* If parsing tentatively, permanently remove the
12530              template argument list.  That will prevent duplicate
12531              error messages from being issued about the missing
12532              "template" keyword.  */
12533           if (start)
12534             cp_lexer_purge_tokens_after (parser->lexer, start);
12535           if (is_identifier)
12536             *is_identifier = true;
12537           return identifier;
12538         }
12539
12540       /* If the "template" keyword is present, then there is generally
12541          no point in doing name-lookup, so we just return IDENTIFIER.
12542          But, if the qualifying scope is non-dependent then we can
12543          (and must) do name-lookup normally.  */
12544       if (template_keyword_p
12545           && (!parser->scope
12546               || (TYPE_P (parser->scope)
12547                   && dependent_type_p (parser->scope))))
12548         return identifier;
12549     }
12550
12551   /* Look up the name.  */
12552   decl = cp_parser_lookup_name (parser, identifier,
12553                                 none_type,
12554                                 /*is_template=*/true,
12555                                 /*is_namespace=*/false,
12556                                 check_dependency_p,
12557                                 /*ambiguous_decls=*/NULL,
12558                                 token->location);
12559
12560   /* If DECL is a template, then the name was a template-name.  */
12561   if (TREE_CODE (decl) == TEMPLATE_DECL)
12562     ;
12563   else
12564     {
12565       tree fn = NULL_TREE;
12566
12567       /* The standard does not explicitly indicate whether a name that
12568          names a set of overloaded declarations, some of which are
12569          templates, is a template-name.  However, such a name should
12570          be a template-name; otherwise, there is no way to form a
12571          template-id for the overloaded templates.  */
12572       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12573       if (TREE_CODE (fns) == OVERLOAD)
12574         for (fn = fns; fn; fn = OVL_NEXT (fn))
12575           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12576             break;
12577
12578       if (!fn)
12579         {
12580           /* The name does not name a template.  */
12581           cp_parser_error (parser, "expected template-name");
12582           return error_mark_node;
12583         }
12584     }
12585
12586   /* If DECL is dependent, and refers to a function, then just return
12587      its name; we will look it up again during template instantiation.  */
12588   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12589     {
12590       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12591       if (TYPE_P (scope) && dependent_type_p (scope))
12592         return identifier;
12593     }
12594
12595   return decl;
12596 }
12597
12598 /* Parse a template-argument-list.
12599
12600    template-argument-list:
12601      template-argument ... [opt]
12602      template-argument-list , template-argument ... [opt]
12603
12604    Returns a TREE_VEC containing the arguments.  */
12605
12606 static tree
12607 cp_parser_template_argument_list (cp_parser* parser)
12608 {
12609   tree fixed_args[10];
12610   unsigned n_args = 0;
12611   unsigned alloced = 10;
12612   tree *arg_ary = fixed_args;
12613   tree vec;
12614   bool saved_in_template_argument_list_p;
12615   bool saved_ice_p;
12616   bool saved_non_ice_p;
12617
12618   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12619   parser->in_template_argument_list_p = true;
12620   /* Even if the template-id appears in an integral
12621      constant-expression, the contents of the argument list do
12622      not.  */
12623   saved_ice_p = parser->integral_constant_expression_p;
12624   parser->integral_constant_expression_p = false;
12625   saved_non_ice_p = parser->non_integral_constant_expression_p;
12626   parser->non_integral_constant_expression_p = false;
12627
12628   /* Parse the arguments.  */
12629   do
12630     {
12631       tree argument;
12632
12633       if (n_args)
12634         /* Consume the comma.  */
12635         cp_lexer_consume_token (parser->lexer);
12636
12637       /* Parse the template-argument.  */
12638       argument = cp_parser_template_argument (parser);
12639
12640       /* If the next token is an ellipsis, we're expanding a template
12641          argument pack. */
12642       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12643         {
12644           if (argument == error_mark_node)
12645             {
12646               cp_token *token = cp_lexer_peek_token (parser->lexer);
12647               error_at (token->location,
12648                         "expected parameter pack before %<...%>");
12649             }
12650           /* Consume the `...' token. */
12651           cp_lexer_consume_token (parser->lexer);
12652
12653           /* Make the argument into a TYPE_PACK_EXPANSION or
12654              EXPR_PACK_EXPANSION. */
12655           argument = make_pack_expansion (argument);
12656         }
12657
12658       if (n_args == alloced)
12659         {
12660           alloced *= 2;
12661
12662           if (arg_ary == fixed_args)
12663             {
12664               arg_ary = XNEWVEC (tree, alloced);
12665               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12666             }
12667           else
12668             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12669         }
12670       arg_ary[n_args++] = argument;
12671     }
12672   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12673
12674   vec = make_tree_vec (n_args);
12675
12676   while (n_args--)
12677     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12678
12679   if (arg_ary != fixed_args)
12680     free (arg_ary);
12681   parser->non_integral_constant_expression_p = saved_non_ice_p;
12682   parser->integral_constant_expression_p = saved_ice_p;
12683   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12684 #ifdef ENABLE_CHECKING
12685   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12686 #endif
12687   return vec;
12688 }
12689
12690 /* Parse a template-argument.
12691
12692    template-argument:
12693      assignment-expression
12694      type-id
12695      id-expression
12696
12697    The representation is that of an assignment-expression, type-id, or
12698    id-expression -- except that the qualified id-expression is
12699    evaluated, so that the value returned is either a DECL or an
12700    OVERLOAD.
12701
12702    Although the standard says "assignment-expression", it forbids
12703    throw-expressions or assignments in the template argument.
12704    Therefore, we use "conditional-expression" instead.  */
12705
12706 static tree
12707 cp_parser_template_argument (cp_parser* parser)
12708 {
12709   tree argument;
12710   bool template_p;
12711   bool address_p;
12712   bool maybe_type_id = false;
12713   cp_token *token = NULL, *argument_start_token = NULL;
12714   cp_id_kind idk;
12715
12716   /* There's really no way to know what we're looking at, so we just
12717      try each alternative in order.
12718
12719        [temp.arg]
12720
12721        In a template-argument, an ambiguity between a type-id and an
12722        expression is resolved to a type-id, regardless of the form of
12723        the corresponding template-parameter.
12724
12725      Therefore, we try a type-id first.  */
12726   cp_parser_parse_tentatively (parser);
12727   argument = cp_parser_template_type_arg (parser);
12728   /* If there was no error parsing the type-id but the next token is a
12729      '>>', our behavior depends on which dialect of C++ we're
12730      parsing. In C++98, we probably found a typo for '> >'. But there
12731      are type-id which are also valid expressions. For instance:
12732
12733      struct X { int operator >> (int); };
12734      template <int V> struct Foo {};
12735      Foo<X () >> 5> r;
12736
12737      Here 'X()' is a valid type-id of a function type, but the user just
12738      wanted to write the expression "X() >> 5". Thus, we remember that we
12739      found a valid type-id, but we still try to parse the argument as an
12740      expression to see what happens. 
12741
12742      In C++0x, the '>>' will be considered two separate '>'
12743      tokens.  */
12744   if (!cp_parser_error_occurred (parser)
12745       && cxx_dialect == cxx98
12746       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12747     {
12748       maybe_type_id = true;
12749       cp_parser_abort_tentative_parse (parser);
12750     }
12751   else
12752     {
12753       /* If the next token isn't a `,' or a `>', then this argument wasn't
12754       really finished. This means that the argument is not a valid
12755       type-id.  */
12756       if (!cp_parser_next_token_ends_template_argument_p (parser))
12757         cp_parser_error (parser, "expected template-argument");
12758       /* If that worked, we're done.  */
12759       if (cp_parser_parse_definitely (parser))
12760         return argument;
12761     }
12762   /* We're still not sure what the argument will be.  */
12763   cp_parser_parse_tentatively (parser);
12764   /* Try a template.  */
12765   argument_start_token = cp_lexer_peek_token (parser->lexer);
12766   argument = cp_parser_id_expression (parser,
12767                                       /*template_keyword_p=*/false,
12768                                       /*check_dependency_p=*/true,
12769                                       &template_p,
12770                                       /*declarator_p=*/false,
12771                                       /*optional_p=*/false);
12772   /* If the next token isn't a `,' or a `>', then this argument wasn't
12773      really finished.  */
12774   if (!cp_parser_next_token_ends_template_argument_p (parser))
12775     cp_parser_error (parser, "expected template-argument");
12776   if (!cp_parser_error_occurred (parser))
12777     {
12778       /* Figure out what is being referred to.  If the id-expression
12779          was for a class template specialization, then we will have a
12780          TYPE_DECL at this point.  There is no need to do name lookup
12781          at this point in that case.  */
12782       if (TREE_CODE (argument) != TYPE_DECL)
12783         argument = cp_parser_lookup_name (parser, argument,
12784                                           none_type,
12785                                           /*is_template=*/template_p,
12786                                           /*is_namespace=*/false,
12787                                           /*check_dependency=*/true,
12788                                           /*ambiguous_decls=*/NULL,
12789                                           argument_start_token->location);
12790       if (TREE_CODE (argument) != TEMPLATE_DECL
12791           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12792         cp_parser_error (parser, "expected template-name");
12793     }
12794   if (cp_parser_parse_definitely (parser))
12795     return argument;
12796   /* It must be a non-type argument.  There permitted cases are given
12797      in [temp.arg.nontype]:
12798
12799      -- an integral constant-expression of integral or enumeration
12800         type; or
12801
12802      -- the name of a non-type template-parameter; or
12803
12804      -- the name of an object or function with external linkage...
12805
12806      -- the address of an object or function with external linkage...
12807
12808      -- a pointer to member...  */
12809   /* Look for a non-type template parameter.  */
12810   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12811     {
12812       cp_parser_parse_tentatively (parser);
12813       argument = cp_parser_primary_expression (parser,
12814                                                /*address_p=*/false,
12815                                                /*cast_p=*/false,
12816                                                /*template_arg_p=*/true,
12817                                                &idk);
12818       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12819           || !cp_parser_next_token_ends_template_argument_p (parser))
12820         cp_parser_simulate_error (parser);
12821       if (cp_parser_parse_definitely (parser))
12822         return argument;
12823     }
12824
12825   /* If the next token is "&", the argument must be the address of an
12826      object or function with external linkage.  */
12827   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12828   if (address_p)
12829     cp_lexer_consume_token (parser->lexer);
12830   /* See if we might have an id-expression.  */
12831   token = cp_lexer_peek_token (parser->lexer);
12832   if (token->type == CPP_NAME
12833       || token->keyword == RID_OPERATOR
12834       || token->type == CPP_SCOPE
12835       || token->type == CPP_TEMPLATE_ID
12836       || token->type == CPP_NESTED_NAME_SPECIFIER)
12837     {
12838       cp_parser_parse_tentatively (parser);
12839       argument = cp_parser_primary_expression (parser,
12840                                                address_p,
12841                                                /*cast_p=*/false,
12842                                                /*template_arg_p=*/true,
12843                                                &idk);
12844       if (cp_parser_error_occurred (parser)
12845           || !cp_parser_next_token_ends_template_argument_p (parser))
12846         cp_parser_abort_tentative_parse (parser);
12847       else
12848         {
12849           tree probe;
12850
12851           if (TREE_CODE (argument) == INDIRECT_REF)
12852             {
12853               gcc_assert (REFERENCE_REF_P (argument));
12854               argument = TREE_OPERAND (argument, 0);
12855             }
12856
12857           /* If we're in a template, we represent a qualified-id referring
12858              to a static data member as a SCOPE_REF even if the scope isn't
12859              dependent so that we can check access control later.  */
12860           probe = argument;
12861           if (TREE_CODE (probe) == SCOPE_REF)
12862             probe = TREE_OPERAND (probe, 1);
12863           if (TREE_CODE (probe) == VAR_DECL)
12864             {
12865               /* A variable without external linkage might still be a
12866                  valid constant-expression, so no error is issued here
12867                  if the external-linkage check fails.  */
12868               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12869                 cp_parser_simulate_error (parser);
12870             }
12871           else if (is_overloaded_fn (argument))
12872             /* All overloaded functions are allowed; if the external
12873                linkage test does not pass, an error will be issued
12874                later.  */
12875             ;
12876           else if (address_p
12877                    && (TREE_CODE (argument) == OFFSET_REF
12878                        || TREE_CODE (argument) == SCOPE_REF))
12879             /* A pointer-to-member.  */
12880             ;
12881           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12882             ;
12883           else
12884             cp_parser_simulate_error (parser);
12885
12886           if (cp_parser_parse_definitely (parser))
12887             {
12888               if (address_p)
12889                 argument = build_x_unary_op (ADDR_EXPR, argument,
12890                                              tf_warning_or_error);
12891               return argument;
12892             }
12893         }
12894     }
12895   /* If the argument started with "&", there are no other valid
12896      alternatives at this point.  */
12897   if (address_p)
12898     {
12899       cp_parser_error (parser, "invalid non-type template argument");
12900       return error_mark_node;
12901     }
12902
12903   /* If the argument wasn't successfully parsed as a type-id followed
12904      by '>>', the argument can only be a constant expression now.
12905      Otherwise, we try parsing the constant-expression tentatively,
12906      because the argument could really be a type-id.  */
12907   if (maybe_type_id)
12908     cp_parser_parse_tentatively (parser);
12909   argument = cp_parser_constant_expression (parser,
12910                                             /*allow_non_constant_p=*/false,
12911                                             /*non_constant_p=*/NULL);
12912   argument = fold_non_dependent_expr (argument);
12913   if (!maybe_type_id)
12914     return argument;
12915   if (!cp_parser_next_token_ends_template_argument_p (parser))
12916     cp_parser_error (parser, "expected template-argument");
12917   if (cp_parser_parse_definitely (parser))
12918     return argument;
12919   /* We did our best to parse the argument as a non type-id, but that
12920      was the only alternative that matched (albeit with a '>' after
12921      it). We can assume it's just a typo from the user, and a
12922      diagnostic will then be issued.  */
12923   return cp_parser_template_type_arg (parser);
12924 }
12925
12926 /* Parse an explicit-instantiation.
12927
12928    explicit-instantiation:
12929      template declaration
12930
12931    Although the standard says `declaration', what it really means is:
12932
12933    explicit-instantiation:
12934      template decl-specifier-seq [opt] declarator [opt] ;
12935
12936    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12937    supposed to be allowed.  A defect report has been filed about this
12938    issue.
12939
12940    GNU Extension:
12941
12942    explicit-instantiation:
12943      storage-class-specifier template
12944        decl-specifier-seq [opt] declarator [opt] ;
12945      function-specifier template
12946        decl-specifier-seq [opt] declarator [opt] ;  */
12947
12948 static void
12949 cp_parser_explicit_instantiation (cp_parser* parser)
12950 {
12951   int declares_class_or_enum;
12952   cp_decl_specifier_seq decl_specifiers;
12953   tree extension_specifier = NULL_TREE;
12954
12955   timevar_push (TV_TEMPLATE_INST);
12956
12957   /* Look for an (optional) storage-class-specifier or
12958      function-specifier.  */
12959   if (cp_parser_allow_gnu_extensions_p (parser))
12960     {
12961       extension_specifier
12962         = cp_parser_storage_class_specifier_opt (parser);
12963       if (!extension_specifier)
12964         extension_specifier
12965           = cp_parser_function_specifier_opt (parser,
12966                                               /*decl_specs=*/NULL);
12967     }
12968
12969   /* Look for the `template' keyword.  */
12970   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12971   /* Let the front end know that we are processing an explicit
12972      instantiation.  */
12973   begin_explicit_instantiation ();
12974   /* [temp.explicit] says that we are supposed to ignore access
12975      control while processing explicit instantiation directives.  */
12976   push_deferring_access_checks (dk_no_check);
12977   /* Parse a decl-specifier-seq.  */
12978   cp_parser_decl_specifier_seq (parser,
12979                                 CP_PARSER_FLAGS_OPTIONAL,
12980                                 &decl_specifiers,
12981                                 &declares_class_or_enum);
12982   /* If there was exactly one decl-specifier, and it declared a class,
12983      and there's no declarator, then we have an explicit type
12984      instantiation.  */
12985   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12986     {
12987       tree type;
12988
12989       type = check_tag_decl (&decl_specifiers);
12990       /* Turn access control back on for names used during
12991          template instantiation.  */
12992       pop_deferring_access_checks ();
12993       if (type)
12994         do_type_instantiation (type, extension_specifier,
12995                                /*complain=*/tf_error);
12996     }
12997   else
12998     {
12999       cp_declarator *declarator;
13000       tree decl;
13001
13002       /* Parse the declarator.  */
13003       declarator
13004         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13005                                 /*ctor_dtor_or_conv_p=*/NULL,
13006                                 /*parenthesized_p=*/NULL,
13007                                 /*member_p=*/false);
13008       if (declares_class_or_enum & 2)
13009         cp_parser_check_for_definition_in_return_type (declarator,
13010                                                        decl_specifiers.type,
13011                                                        decl_specifiers.type_location);
13012       if (declarator != cp_error_declarator)
13013         {
13014           if (decl_specifiers.specs[(int)ds_inline])
13015             permerror (input_location, "explicit instantiation shall not use"
13016                        " %<inline%> specifier");
13017           if (decl_specifiers.specs[(int)ds_constexpr])
13018             permerror (input_location, "explicit instantiation shall not use"
13019                        " %<constexpr%> specifier");
13020
13021           decl = grokdeclarator (declarator, &decl_specifiers,
13022                                  NORMAL, 0, &decl_specifiers.attributes);
13023           /* Turn access control back on for names used during
13024              template instantiation.  */
13025           pop_deferring_access_checks ();
13026           /* Do the explicit instantiation.  */
13027           do_decl_instantiation (decl, extension_specifier);
13028         }
13029       else
13030         {
13031           pop_deferring_access_checks ();
13032           /* Skip the body of the explicit instantiation.  */
13033           cp_parser_skip_to_end_of_statement (parser);
13034         }
13035     }
13036   /* We're done with the instantiation.  */
13037   end_explicit_instantiation ();
13038
13039   cp_parser_consume_semicolon_at_end_of_statement (parser);
13040
13041   timevar_pop (TV_TEMPLATE_INST);
13042 }
13043
13044 /* Parse an explicit-specialization.
13045
13046    explicit-specialization:
13047      template < > declaration
13048
13049    Although the standard says `declaration', what it really means is:
13050
13051    explicit-specialization:
13052      template <> decl-specifier [opt] init-declarator [opt] ;
13053      template <> function-definition
13054      template <> explicit-specialization
13055      template <> template-declaration  */
13056
13057 static void
13058 cp_parser_explicit_specialization (cp_parser* parser)
13059 {
13060   bool need_lang_pop;
13061   cp_token *token = cp_lexer_peek_token (parser->lexer);
13062
13063   /* Look for the `template' keyword.  */
13064   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13065   /* Look for the `<'.  */
13066   cp_parser_require (parser, CPP_LESS, RT_LESS);
13067   /* Look for the `>'.  */
13068   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13069   /* We have processed another parameter list.  */
13070   ++parser->num_template_parameter_lists;
13071   /* [temp]
13072
13073      A template ... explicit specialization ... shall not have C
13074      linkage.  */
13075   if (current_lang_name == lang_name_c)
13076     {
13077       error_at (token->location, "template specialization with C linkage");
13078       /* Give it C++ linkage to avoid confusing other parts of the
13079          front end.  */
13080       push_lang_context (lang_name_cplusplus);
13081       need_lang_pop = true;
13082     }
13083   else
13084     need_lang_pop = false;
13085   /* Let the front end know that we are beginning a specialization.  */
13086   if (!begin_specialization ())
13087     {
13088       end_specialization ();
13089       return;
13090     }
13091
13092   /* If the next keyword is `template', we need to figure out whether
13093      or not we're looking a template-declaration.  */
13094   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13095     {
13096       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13097           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13098         cp_parser_template_declaration_after_export (parser,
13099                                                      /*member_p=*/false);
13100       else
13101         cp_parser_explicit_specialization (parser);
13102     }
13103   else
13104     /* Parse the dependent declaration.  */
13105     cp_parser_single_declaration (parser,
13106                                   /*checks=*/NULL,
13107                                   /*member_p=*/false,
13108                                   /*explicit_specialization_p=*/true,
13109                                   /*friend_p=*/NULL);
13110   /* We're done with the specialization.  */
13111   end_specialization ();
13112   /* For the erroneous case of a template with C linkage, we pushed an
13113      implicit C++ linkage scope; exit that scope now.  */
13114   if (need_lang_pop)
13115     pop_lang_context ();
13116   /* We're done with this parameter list.  */
13117   --parser->num_template_parameter_lists;
13118 }
13119
13120 /* Parse a type-specifier.
13121
13122    type-specifier:
13123      simple-type-specifier
13124      class-specifier
13125      enum-specifier
13126      elaborated-type-specifier
13127      cv-qualifier
13128
13129    GNU Extension:
13130
13131    type-specifier:
13132      __complex__
13133
13134    Returns a representation of the type-specifier.  For a
13135    class-specifier, enum-specifier, or elaborated-type-specifier, a
13136    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13137
13138    The parser flags FLAGS is used to control type-specifier parsing.
13139
13140    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13141    in a decl-specifier-seq.
13142
13143    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13144    class-specifier, enum-specifier, or elaborated-type-specifier, then
13145    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13146    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13147    zero.
13148
13149    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13150    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13151    is set to FALSE.  */
13152
13153 static tree
13154 cp_parser_type_specifier (cp_parser* parser,
13155                           cp_parser_flags flags,
13156                           cp_decl_specifier_seq *decl_specs,
13157                           bool is_declaration,
13158                           int* declares_class_or_enum,
13159                           bool* is_cv_qualifier)
13160 {
13161   tree type_spec = NULL_TREE;
13162   cp_token *token;
13163   enum rid keyword;
13164   cp_decl_spec ds = ds_last;
13165
13166   /* Assume this type-specifier does not declare a new type.  */
13167   if (declares_class_or_enum)
13168     *declares_class_or_enum = 0;
13169   /* And that it does not specify a cv-qualifier.  */
13170   if (is_cv_qualifier)
13171     *is_cv_qualifier = false;
13172   /* Peek at the next token.  */
13173   token = cp_lexer_peek_token (parser->lexer);
13174
13175   /* If we're looking at a keyword, we can use that to guide the
13176      production we choose.  */
13177   keyword = token->keyword;
13178   switch (keyword)
13179     {
13180     case RID_ENUM:
13181       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13182         goto elaborated_type_specifier;
13183
13184       /* Look for the enum-specifier.  */
13185       type_spec = cp_parser_enum_specifier (parser);
13186       /* If that worked, we're done.  */
13187       if (type_spec)
13188         {
13189           if (declares_class_or_enum)
13190             *declares_class_or_enum = 2;
13191           if (decl_specs)
13192             cp_parser_set_decl_spec_type (decl_specs,
13193                                           type_spec,
13194                                           token->location,
13195                                           /*type_definition_p=*/true);
13196           return type_spec;
13197         }
13198       else
13199         goto elaborated_type_specifier;
13200
13201       /* Any of these indicate either a class-specifier, or an
13202          elaborated-type-specifier.  */
13203     case RID_CLASS:
13204     case RID_STRUCT:
13205     case RID_UNION:
13206       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13207         goto elaborated_type_specifier;
13208
13209       /* Parse tentatively so that we can back up if we don't find a
13210          class-specifier.  */
13211       cp_parser_parse_tentatively (parser);
13212       /* Look for the class-specifier.  */
13213       type_spec = cp_parser_class_specifier (parser);
13214       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13215       /* If that worked, we're done.  */
13216       if (cp_parser_parse_definitely (parser))
13217         {
13218           if (declares_class_or_enum)
13219             *declares_class_or_enum = 2;
13220           if (decl_specs)
13221             cp_parser_set_decl_spec_type (decl_specs,
13222                                           type_spec,
13223                                           token->location,
13224                                           /*type_definition_p=*/true);
13225           return type_spec;
13226         }
13227
13228       /* Fall through.  */
13229     elaborated_type_specifier:
13230       /* We're declaring (not defining) a class or enum.  */
13231       if (declares_class_or_enum)
13232         *declares_class_or_enum = 1;
13233
13234       /* Fall through.  */
13235     case RID_TYPENAME:
13236       /* Look for an elaborated-type-specifier.  */
13237       type_spec
13238         = (cp_parser_elaborated_type_specifier
13239            (parser,
13240             decl_specs && decl_specs->specs[(int) ds_friend],
13241             is_declaration));
13242       if (decl_specs)
13243         cp_parser_set_decl_spec_type (decl_specs,
13244                                       type_spec,
13245                                       token->location,
13246                                       /*type_definition_p=*/false);
13247       return type_spec;
13248
13249     case RID_CONST:
13250       ds = ds_const;
13251       if (is_cv_qualifier)
13252         *is_cv_qualifier = true;
13253       break;
13254
13255     case RID_VOLATILE:
13256       ds = ds_volatile;
13257       if (is_cv_qualifier)
13258         *is_cv_qualifier = true;
13259       break;
13260
13261     case RID_RESTRICT:
13262       ds = ds_restrict;
13263       if (is_cv_qualifier)
13264         *is_cv_qualifier = true;
13265       break;
13266
13267     case RID_COMPLEX:
13268       /* The `__complex__' keyword is a GNU extension.  */
13269       ds = ds_complex;
13270       break;
13271
13272     default:
13273       break;
13274     }
13275
13276   /* Handle simple keywords.  */
13277   if (ds != ds_last)
13278     {
13279       if (decl_specs)
13280         {
13281           ++decl_specs->specs[(int)ds];
13282           decl_specs->any_specifiers_p = true;
13283         }
13284       return cp_lexer_consume_token (parser->lexer)->u.value;
13285     }
13286
13287   /* If we do not already have a type-specifier, assume we are looking
13288      at a simple-type-specifier.  */
13289   type_spec = cp_parser_simple_type_specifier (parser,
13290                                                decl_specs,
13291                                                flags);
13292
13293   /* If we didn't find a type-specifier, and a type-specifier was not
13294      optional in this context, issue an error message.  */
13295   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13296     {
13297       cp_parser_error (parser, "expected type specifier");
13298       return error_mark_node;
13299     }
13300
13301   return type_spec;
13302 }
13303
13304 /* Parse a simple-type-specifier.
13305
13306    simple-type-specifier:
13307      :: [opt] nested-name-specifier [opt] type-name
13308      :: [opt] nested-name-specifier template template-id
13309      char
13310      wchar_t
13311      bool
13312      short
13313      int
13314      long
13315      signed
13316      unsigned
13317      float
13318      double
13319      void
13320
13321    C++0x Extension:
13322
13323    simple-type-specifier:
13324      auto
13325      decltype ( expression )   
13326      char16_t
13327      char32_t
13328      __underlying_type ( type-id )
13329
13330    GNU Extension:
13331
13332    simple-type-specifier:
13333      __int128
13334      __typeof__ unary-expression
13335      __typeof__ ( type-id )
13336
13337    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13338    appropriately updated.  */
13339
13340 static tree
13341 cp_parser_simple_type_specifier (cp_parser* parser,
13342                                  cp_decl_specifier_seq *decl_specs,
13343                                  cp_parser_flags flags)
13344 {
13345   tree type = NULL_TREE;
13346   cp_token *token;
13347
13348   /* Peek at the next token.  */
13349   token = cp_lexer_peek_token (parser->lexer);
13350
13351   /* If we're looking at a keyword, things are easy.  */
13352   switch (token->keyword)
13353     {
13354     case RID_CHAR:
13355       if (decl_specs)
13356         decl_specs->explicit_char_p = true;
13357       type = char_type_node;
13358       break;
13359     case RID_CHAR16:
13360       type = char16_type_node;
13361       break;
13362     case RID_CHAR32:
13363       type = char32_type_node;
13364       break;
13365     case RID_WCHAR:
13366       type = wchar_type_node;
13367       break;
13368     case RID_BOOL:
13369       type = boolean_type_node;
13370       break;
13371     case RID_SHORT:
13372       if (decl_specs)
13373         ++decl_specs->specs[(int) ds_short];
13374       type = short_integer_type_node;
13375       break;
13376     case RID_INT:
13377       if (decl_specs)
13378         decl_specs->explicit_int_p = true;
13379       type = integer_type_node;
13380       break;
13381     case RID_INT128:
13382       if (!int128_integer_type_node)
13383         break;
13384       if (decl_specs)
13385         decl_specs->explicit_int128_p = true;
13386       type = int128_integer_type_node;
13387       break;
13388     case RID_LONG:
13389       if (decl_specs)
13390         ++decl_specs->specs[(int) ds_long];
13391       type = long_integer_type_node;
13392       break;
13393     case RID_SIGNED:
13394       if (decl_specs)
13395         ++decl_specs->specs[(int) ds_signed];
13396       type = integer_type_node;
13397       break;
13398     case RID_UNSIGNED:
13399       if (decl_specs)
13400         ++decl_specs->specs[(int) ds_unsigned];
13401       type = unsigned_type_node;
13402       break;
13403     case RID_FLOAT:
13404       type = float_type_node;
13405       break;
13406     case RID_DOUBLE:
13407       type = double_type_node;
13408       break;
13409     case RID_VOID:
13410       type = void_type_node;
13411       break;
13412       
13413     case RID_AUTO:
13414       maybe_warn_cpp0x (CPP0X_AUTO);
13415       type = make_auto ();
13416       break;
13417
13418     case RID_DECLTYPE:
13419       /* Since DR 743, decltype can either be a simple-type-specifier by
13420          itself or begin a nested-name-specifier.  Parsing it will replace
13421          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13422          handling below decide what to do.  */
13423       cp_parser_decltype (parser);
13424       cp_lexer_set_token_position (parser->lexer, token);
13425       break;
13426
13427     case RID_TYPEOF:
13428       /* Consume the `typeof' token.  */
13429       cp_lexer_consume_token (parser->lexer);
13430       /* Parse the operand to `typeof'.  */
13431       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13432       /* If it is not already a TYPE, take its type.  */
13433       if (!TYPE_P (type))
13434         type = finish_typeof (type);
13435
13436       if (decl_specs)
13437         cp_parser_set_decl_spec_type (decl_specs, type,
13438                                       token->location,
13439                                       /*type_definition_p=*/false);
13440
13441       return type;
13442
13443     case RID_UNDERLYING_TYPE:
13444       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13445       if (decl_specs)
13446         cp_parser_set_decl_spec_type (decl_specs, type,
13447                                       token->location,
13448                                       /*type_definition_p=*/false);
13449
13450       return type;
13451
13452     case RID_BASES:
13453     case RID_DIRECT_BASES:
13454       type = cp_parser_trait_expr (parser, token->keyword);
13455       if (decl_specs)
13456        cp_parser_set_decl_spec_type (decl_specs, type,
13457                                      token->location,
13458                                      /*type_definition_p=*/false);
13459       return type;
13460     default:
13461       break;
13462     }
13463
13464   /* If token is an already-parsed decltype not followed by ::,
13465      it's a simple-type-specifier.  */
13466   if (token->type == CPP_DECLTYPE
13467       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13468     {
13469       type = token->u.value;
13470       if (decl_specs)
13471         cp_parser_set_decl_spec_type (decl_specs, type,
13472                                       token->location,
13473                                       /*type_definition_p=*/false);
13474       cp_lexer_consume_token (parser->lexer);
13475       return type;
13476     }
13477
13478   /* If the type-specifier was for a built-in type, we're done.  */
13479   if (type)
13480     {
13481       /* Record the type.  */
13482       if (decl_specs
13483           && (token->keyword != RID_SIGNED
13484               && token->keyword != RID_UNSIGNED
13485               && token->keyword != RID_SHORT
13486               && token->keyword != RID_LONG))
13487         cp_parser_set_decl_spec_type (decl_specs,
13488                                       type,
13489                                       token->location,
13490                                       /*type_definition_p=*/false);
13491       if (decl_specs)
13492         decl_specs->any_specifiers_p = true;
13493
13494       /* Consume the token.  */
13495       cp_lexer_consume_token (parser->lexer);
13496
13497       /* There is no valid C++ program where a non-template type is
13498          followed by a "<".  That usually indicates that the user thought
13499          that the type was a template.  */
13500       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13501
13502       return TYPE_NAME (type);
13503     }
13504
13505   /* The type-specifier must be a user-defined type.  */
13506   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13507     {
13508       bool qualified_p;
13509       bool global_p;
13510
13511       /* Don't gobble tokens or issue error messages if this is an
13512          optional type-specifier.  */
13513       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13514         cp_parser_parse_tentatively (parser);
13515
13516       /* Look for the optional `::' operator.  */
13517       global_p
13518         = (cp_parser_global_scope_opt (parser,
13519                                        /*current_scope_valid_p=*/false)
13520            != NULL_TREE);
13521       /* Look for the nested-name specifier.  */
13522       qualified_p
13523         = (cp_parser_nested_name_specifier_opt (parser,
13524                                                 /*typename_keyword_p=*/false,
13525                                                 /*check_dependency_p=*/true,
13526                                                 /*type_p=*/false,
13527                                                 /*is_declaration=*/false)
13528            != NULL_TREE);
13529       token = cp_lexer_peek_token (parser->lexer);
13530       /* If we have seen a nested-name-specifier, and the next token
13531          is `template', then we are using the template-id production.  */
13532       if (parser->scope
13533           && cp_parser_optional_template_keyword (parser))
13534         {
13535           /* Look for the template-id.  */
13536           type = cp_parser_template_id (parser,
13537                                         /*template_keyword_p=*/true,
13538                                         /*check_dependency_p=*/true,
13539                                         /*is_declaration=*/false);
13540           /* If the template-id did not name a type, we are out of
13541              luck.  */
13542           if (TREE_CODE (type) != TYPE_DECL)
13543             {
13544               cp_parser_error (parser, "expected template-id for type");
13545               type = NULL_TREE;
13546             }
13547         }
13548       /* Otherwise, look for a type-name.  */
13549       else
13550         type = cp_parser_type_name (parser);
13551       /* Keep track of all name-lookups performed in class scopes.  */
13552       if (type
13553           && !global_p
13554           && !qualified_p
13555           && TREE_CODE (type) == TYPE_DECL
13556           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13557         maybe_note_name_used_in_class (DECL_NAME (type), type);
13558       /* If it didn't work out, we don't have a TYPE.  */
13559       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13560           && !cp_parser_parse_definitely (parser))
13561         type = NULL_TREE;
13562       if (type && decl_specs)
13563         cp_parser_set_decl_spec_type (decl_specs, type,
13564                                       token->location,
13565                                       /*type_definition_p=*/false);
13566     }
13567
13568   /* If we didn't get a type-name, issue an error message.  */
13569   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13570     {
13571       cp_parser_error (parser, "expected type-name");
13572       return error_mark_node;
13573     }
13574
13575   if (type && type != error_mark_node)
13576     {
13577       /* See if TYPE is an Objective-C type, and if so, parse and
13578          accept any protocol references following it.  Do this before
13579          the cp_parser_check_for_invalid_template_id() call, because
13580          Objective-C types can be followed by '<...>' which would
13581          enclose protocol names rather than template arguments, and so
13582          everything is fine.  */
13583       if (c_dialect_objc () && !parser->scope
13584           && (objc_is_id (type) || objc_is_class_name (type)))
13585         {
13586           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13587           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13588
13589           /* Clobber the "unqualified" type previously entered into
13590              DECL_SPECS with the new, improved protocol-qualified version.  */
13591           if (decl_specs)
13592             decl_specs->type = qual_type;
13593
13594           return qual_type;
13595         }
13596
13597       /* There is no valid C++ program where a non-template type is
13598          followed by a "<".  That usually indicates that the user
13599          thought that the type was a template.  */
13600       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13601                                                token->location);
13602     }
13603
13604   return type;
13605 }
13606
13607 /* Parse a type-name.
13608
13609    type-name:
13610      class-name
13611      enum-name
13612      typedef-name
13613
13614    enum-name:
13615      identifier
13616
13617    typedef-name:
13618      identifier
13619
13620    Returns a TYPE_DECL for the type.  */
13621
13622 static tree
13623 cp_parser_type_name (cp_parser* parser)
13624 {
13625   tree type_decl;
13626
13627   /* We can't know yet whether it is a class-name or not.  */
13628   cp_parser_parse_tentatively (parser);
13629   /* Try a class-name.  */
13630   type_decl = cp_parser_class_name (parser,
13631                                     /*typename_keyword_p=*/false,
13632                                     /*template_keyword_p=*/false,
13633                                     none_type,
13634                                     /*check_dependency_p=*/true,
13635                                     /*class_head_p=*/false,
13636                                     /*is_declaration=*/false);
13637   /* If it's not a class-name, keep looking.  */
13638   if (!cp_parser_parse_definitely (parser))
13639     {
13640       /* It must be a typedef-name or an enum-name.  */
13641       return cp_parser_nonclass_name (parser);
13642     }
13643
13644   return type_decl;
13645 }
13646
13647 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13648
13649    enum-name:
13650      identifier
13651
13652    typedef-name:
13653      identifier
13654
13655    Returns a TYPE_DECL for the type.  */
13656
13657 static tree
13658 cp_parser_nonclass_name (cp_parser* parser)
13659 {
13660   tree type_decl;
13661   tree identifier;
13662
13663   cp_token *token = cp_lexer_peek_token (parser->lexer);
13664   identifier = cp_parser_identifier (parser);
13665   if (identifier == error_mark_node)
13666     return error_mark_node;
13667
13668   /* Look up the type-name.  */
13669   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13670
13671   if (TREE_CODE (type_decl) != TYPE_DECL
13672       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13673     {
13674       /* See if this is an Objective-C type.  */
13675       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13676       tree type = objc_get_protocol_qualified_type (identifier, protos);
13677       if (type)
13678         type_decl = TYPE_NAME (type);
13679     }
13680
13681   /* Issue an error if we did not find a type-name.  */
13682   if (TREE_CODE (type_decl) != TYPE_DECL
13683       /* In Objective-C, we have the complication that class names are
13684          normally type names and start declarations (eg, the
13685          "NSObject" in "NSObject *object;"), but can be used in an
13686          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13687          is an expression.  So, a classname followed by a dot is not a
13688          valid type-name.  */
13689       || (objc_is_class_name (TREE_TYPE (type_decl))
13690           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13691     {
13692       if (!cp_parser_simulate_error (parser))
13693         cp_parser_name_lookup_error (parser, identifier, type_decl,
13694                                      NLE_TYPE, token->location);
13695       return error_mark_node;
13696     }
13697   /* Remember that the name was used in the definition of the
13698      current class so that we can check later to see if the
13699      meaning would have been different after the class was
13700      entirely defined.  */
13701   else if (type_decl != error_mark_node
13702            && !parser->scope)
13703     maybe_note_name_used_in_class (identifier, type_decl);
13704   
13705   return type_decl;
13706 }
13707
13708 /* Parse an elaborated-type-specifier.  Note that the grammar given
13709    here incorporates the resolution to DR68.
13710
13711    elaborated-type-specifier:
13712      class-key :: [opt] nested-name-specifier [opt] identifier
13713      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13714      enum-key :: [opt] nested-name-specifier [opt] identifier
13715      typename :: [opt] nested-name-specifier identifier
13716      typename :: [opt] nested-name-specifier template [opt]
13717        template-id
13718
13719    GNU extension:
13720
13721    elaborated-type-specifier:
13722      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13723      class-key attributes :: [opt] nested-name-specifier [opt]
13724                template [opt] template-id
13725      enum attributes :: [opt] nested-name-specifier [opt] identifier
13726
13727    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13728    declared `friend'.  If IS_DECLARATION is TRUE, then this
13729    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13730    something is being declared.
13731
13732    Returns the TYPE specified.  */
13733
13734 static tree
13735 cp_parser_elaborated_type_specifier (cp_parser* parser,
13736                                      bool is_friend,
13737                                      bool is_declaration)
13738 {
13739   enum tag_types tag_type;
13740   tree identifier;
13741   tree type = NULL_TREE;
13742   tree attributes = NULL_TREE;
13743   tree globalscope;
13744   cp_token *token = NULL;
13745
13746   /* See if we're looking at the `enum' keyword.  */
13747   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13748     {
13749       /* Consume the `enum' token.  */
13750       cp_lexer_consume_token (parser->lexer);
13751       /* Remember that it's an enumeration type.  */
13752       tag_type = enum_type;
13753       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13754          enums) is used here.  */
13755       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13756           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13757         {
13758             pedwarn (input_location, 0, "elaborated-type-specifier "
13759                       "for a scoped enum must not use the %<%D%> keyword",
13760                       cp_lexer_peek_token (parser->lexer)->u.value);
13761           /* Consume the `struct' or `class' and parse it anyway.  */
13762           cp_lexer_consume_token (parser->lexer);
13763         }
13764       /* Parse the attributes.  */
13765       attributes = cp_parser_attributes_opt (parser);
13766     }
13767   /* Or, it might be `typename'.  */
13768   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13769                                            RID_TYPENAME))
13770     {
13771       /* Consume the `typename' token.  */
13772       cp_lexer_consume_token (parser->lexer);
13773       /* Remember that it's a `typename' type.  */
13774       tag_type = typename_type;
13775     }
13776   /* Otherwise it must be a class-key.  */
13777   else
13778     {
13779       tag_type = cp_parser_class_key (parser);
13780       if (tag_type == none_type)
13781         return error_mark_node;
13782       /* Parse the attributes.  */
13783       attributes = cp_parser_attributes_opt (parser);
13784     }
13785
13786   /* Look for the `::' operator.  */
13787   globalscope =  cp_parser_global_scope_opt (parser,
13788                                              /*current_scope_valid_p=*/false);
13789   /* Look for the nested-name-specifier.  */
13790   if (tag_type == typename_type && !globalscope)
13791     {
13792       if (!cp_parser_nested_name_specifier (parser,
13793                                            /*typename_keyword_p=*/true,
13794                                            /*check_dependency_p=*/true,
13795                                            /*type_p=*/true,
13796                                             is_declaration))
13797         return error_mark_node;
13798     }
13799   else
13800     /* Even though `typename' is not present, the proposed resolution
13801        to Core Issue 180 says that in `class A<T>::B', `B' should be
13802        considered a type-name, even if `A<T>' is dependent.  */
13803     cp_parser_nested_name_specifier_opt (parser,
13804                                          /*typename_keyword_p=*/true,
13805                                          /*check_dependency_p=*/true,
13806                                          /*type_p=*/true,
13807                                          is_declaration);
13808  /* For everything but enumeration types, consider a template-id.
13809     For an enumeration type, consider only a plain identifier.  */
13810   if (tag_type != enum_type)
13811     {
13812       bool template_p = false;
13813       tree decl;
13814
13815       /* Allow the `template' keyword.  */
13816       template_p = cp_parser_optional_template_keyword (parser);
13817       /* If we didn't see `template', we don't know if there's a
13818          template-id or not.  */
13819       if (!template_p)
13820         cp_parser_parse_tentatively (parser);
13821       /* Parse the template-id.  */
13822       token = cp_lexer_peek_token (parser->lexer);
13823       decl = cp_parser_template_id (parser, template_p,
13824                                     /*check_dependency_p=*/true,
13825                                     is_declaration);
13826       /* If we didn't find a template-id, look for an ordinary
13827          identifier.  */
13828       if (!template_p && !cp_parser_parse_definitely (parser))
13829         ;
13830       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13831          in effect, then we must assume that, upon instantiation, the
13832          template will correspond to a class.  */
13833       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13834                && tag_type == typename_type)
13835         type = make_typename_type (parser->scope, decl,
13836                                    typename_type,
13837                                    /*complain=*/tf_error);
13838       /* If the `typename' keyword is in effect and DECL is not a type
13839          decl. Then type is non existant.   */
13840       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13841         type = NULL_TREE; 
13842       else 
13843         type = TREE_TYPE (decl);
13844     }
13845
13846   if (!type)
13847     {
13848       token = cp_lexer_peek_token (parser->lexer);
13849       identifier = cp_parser_identifier (parser);
13850
13851       if (identifier == error_mark_node)
13852         {
13853           parser->scope = NULL_TREE;
13854           return error_mark_node;
13855         }
13856
13857       /* For a `typename', we needn't call xref_tag.  */
13858       if (tag_type == typename_type
13859           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13860         return cp_parser_make_typename_type (parser, parser->scope,
13861                                              identifier,
13862                                              token->location);
13863       /* Look up a qualified name in the usual way.  */
13864       if (parser->scope)
13865         {
13866           tree decl;
13867           tree ambiguous_decls;
13868
13869           decl = cp_parser_lookup_name (parser, identifier,
13870                                         tag_type,
13871                                         /*is_template=*/false,
13872                                         /*is_namespace=*/false,
13873                                         /*check_dependency=*/true,
13874                                         &ambiguous_decls,
13875                                         token->location);
13876
13877           /* If the lookup was ambiguous, an error will already have been
13878              issued.  */
13879           if (ambiguous_decls)
13880             return error_mark_node;
13881
13882           /* If we are parsing friend declaration, DECL may be a
13883              TEMPLATE_DECL tree node here.  However, we need to check
13884              whether this TEMPLATE_DECL results in valid code.  Consider
13885              the following example:
13886
13887                namespace N {
13888                  template <class T> class C {};
13889                }
13890                class X {
13891                  template <class T> friend class N::C; // #1, valid code
13892                };
13893                template <class T> class Y {
13894                  friend class N::C;                    // #2, invalid code
13895                };
13896
13897              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13898              name lookup of `N::C'.  We see that friend declaration must
13899              be template for the code to be valid.  Note that
13900              processing_template_decl does not work here since it is
13901              always 1 for the above two cases.  */
13902
13903           decl = (cp_parser_maybe_treat_template_as_class
13904                   (decl, /*tag_name_p=*/is_friend
13905                          && parser->num_template_parameter_lists));
13906
13907           if (TREE_CODE (decl) != TYPE_DECL)
13908             {
13909               cp_parser_diagnose_invalid_type_name (parser,
13910                                                     parser->scope,
13911                                                     identifier,
13912                                                     token->location);
13913               return error_mark_node;
13914             }
13915
13916           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13917             {
13918               bool allow_template = (parser->num_template_parameter_lists
13919                                       || DECL_SELF_REFERENCE_P (decl));
13920               type = check_elaborated_type_specifier (tag_type, decl, 
13921                                                       allow_template);
13922
13923               if (type == error_mark_node)
13924                 return error_mark_node;
13925             }
13926
13927           /* Forward declarations of nested types, such as
13928
13929                class C1::C2;
13930                class C1::C2::C3;
13931
13932              are invalid unless all components preceding the final '::'
13933              are complete.  If all enclosing types are complete, these
13934              declarations become merely pointless.
13935
13936              Invalid forward declarations of nested types are errors
13937              caught elsewhere in parsing.  Those that are pointless arrive
13938              here.  */
13939
13940           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13941               && !is_friend && !processing_explicit_instantiation)
13942             warning (0, "declaration %qD does not declare anything", decl);
13943
13944           type = TREE_TYPE (decl);
13945         }
13946       else
13947         {
13948           /* An elaborated-type-specifier sometimes introduces a new type and
13949              sometimes names an existing type.  Normally, the rule is that it
13950              introduces a new type only if there is not an existing type of
13951              the same name already in scope.  For example, given:
13952
13953                struct S {};
13954                void f() { struct S s; }
13955
13956              the `struct S' in the body of `f' is the same `struct S' as in
13957              the global scope; the existing definition is used.  However, if
13958              there were no global declaration, this would introduce a new
13959              local class named `S'.
13960
13961              An exception to this rule applies to the following code:
13962
13963                namespace N { struct S; }
13964
13965              Here, the elaborated-type-specifier names a new type
13966              unconditionally; even if there is already an `S' in the
13967              containing scope this declaration names a new type.
13968              This exception only applies if the elaborated-type-specifier
13969              forms the complete declaration:
13970
13971                [class.name]
13972
13973                A declaration consisting solely of `class-key identifier ;' is
13974                either a redeclaration of the name in the current scope or a
13975                forward declaration of the identifier as a class name.  It
13976                introduces the name into the current scope.
13977
13978              We are in this situation precisely when the next token is a `;'.
13979
13980              An exception to the exception is that a `friend' declaration does
13981              *not* name a new type; i.e., given:
13982
13983                struct S { friend struct T; };
13984
13985              `T' is not a new type in the scope of `S'.
13986
13987              Also, `new struct S' or `sizeof (struct S)' never results in the
13988              definition of a new type; a new type can only be declared in a
13989              declaration context.  */
13990
13991           tag_scope ts;
13992           bool template_p;
13993
13994           if (is_friend)
13995             /* Friends have special name lookup rules.  */
13996             ts = ts_within_enclosing_non_class;
13997           else if (is_declaration
13998                    && cp_lexer_next_token_is (parser->lexer,
13999                                               CPP_SEMICOLON))
14000             /* This is a `class-key identifier ;' */
14001             ts = ts_current;
14002           else
14003             ts = ts_global;
14004
14005           template_p =
14006             (parser->num_template_parameter_lists
14007              && (cp_parser_next_token_starts_class_definition_p (parser)
14008                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14009           /* An unqualified name was used to reference this type, so
14010              there were no qualifying templates.  */
14011           if (!cp_parser_check_template_parameters (parser,
14012                                                     /*num_templates=*/0,
14013                                                     token->location,
14014                                                     /*declarator=*/NULL))
14015             return error_mark_node;
14016           type = xref_tag (tag_type, identifier, ts, template_p);
14017         }
14018     }
14019
14020   if (type == error_mark_node)
14021     return error_mark_node;
14022
14023   /* Allow attributes on forward declarations of classes.  */
14024   if (attributes)
14025     {
14026       if (TREE_CODE (type) == TYPENAME_TYPE)
14027         warning (OPT_Wattributes,
14028                  "attributes ignored on uninstantiated type");
14029       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14030                && ! processing_explicit_instantiation)
14031         warning (OPT_Wattributes,
14032                  "attributes ignored on template instantiation");
14033       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14034         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14035       else
14036         warning (OPT_Wattributes,
14037                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14038     }
14039
14040   if (tag_type != enum_type)
14041     {
14042       /* Indicate whether this class was declared as a `class' or as a
14043          `struct'.  */
14044       if (TREE_CODE (type) == RECORD_TYPE)
14045         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14046       cp_parser_check_class_key (tag_type, type);
14047     }
14048
14049   /* A "<" cannot follow an elaborated type specifier.  If that
14050      happens, the user was probably trying to form a template-id.  */
14051   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14052
14053   return type;
14054 }
14055
14056 /* Parse an enum-specifier.
14057
14058    enum-specifier:
14059      enum-head { enumerator-list [opt] }
14060
14061    enum-head:
14062      enum-key identifier [opt] enum-base [opt]
14063      enum-key nested-name-specifier identifier enum-base [opt]
14064
14065    enum-key:
14066      enum
14067      enum class   [C++0x]
14068      enum struct  [C++0x]
14069
14070    enum-base:   [C++0x]
14071      : type-specifier-seq
14072
14073    opaque-enum-specifier:
14074      enum-key identifier enum-base [opt] ;
14075
14076    GNU Extensions:
14077      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14078        { enumerator-list [opt] }attributes[opt]
14079
14080    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14081    if the token stream isn't an enum-specifier after all.  */
14082
14083 static tree
14084 cp_parser_enum_specifier (cp_parser* parser)
14085 {
14086   tree identifier;
14087   tree type = NULL_TREE;
14088   tree prev_scope;
14089   tree nested_name_specifier = NULL_TREE;
14090   tree attributes;
14091   bool scoped_enum_p = false;
14092   bool has_underlying_type = false;
14093   bool nested_being_defined = false;
14094   bool new_value_list = false;
14095   bool is_new_type = false;
14096   bool is_anonymous = false;
14097   tree underlying_type = NULL_TREE;
14098   cp_token *type_start_token = NULL;
14099   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14100
14101   parser->colon_corrects_to_scope_p = false;
14102
14103   /* Parse tentatively so that we can back up if we don't find a
14104      enum-specifier.  */
14105   cp_parser_parse_tentatively (parser);
14106
14107   /* Caller guarantees that the current token is 'enum', an identifier
14108      possibly follows, and the token after that is an opening brace.
14109      If we don't have an identifier, fabricate an anonymous name for
14110      the enumeration being defined.  */
14111   cp_lexer_consume_token (parser->lexer);
14112
14113   /* Parse the "class" or "struct", which indicates a scoped
14114      enumeration type in C++0x.  */
14115   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14116       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14117     {
14118       if (cxx_dialect < cxx0x)
14119         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14120
14121       /* Consume the `struct' or `class' token.  */
14122       cp_lexer_consume_token (parser->lexer);
14123
14124       scoped_enum_p = true;
14125     }
14126
14127   attributes = cp_parser_attributes_opt (parser);
14128
14129   /* Clear the qualification.  */
14130   parser->scope = NULL_TREE;
14131   parser->qualifying_scope = NULL_TREE;
14132   parser->object_scope = NULL_TREE;
14133
14134   /* Figure out in what scope the declaration is being placed.  */
14135   prev_scope = current_scope ();
14136
14137   type_start_token = cp_lexer_peek_token (parser->lexer);
14138
14139   push_deferring_access_checks (dk_no_check);
14140   nested_name_specifier
14141       = cp_parser_nested_name_specifier_opt (parser,
14142                                              /*typename_keyword_p=*/true,
14143                                              /*check_dependency_p=*/false,
14144                                              /*type_p=*/false,
14145                                              /*is_declaration=*/false);
14146
14147   if (nested_name_specifier)
14148     {
14149       tree name;
14150
14151       identifier = cp_parser_identifier (parser);
14152       name =  cp_parser_lookup_name (parser, identifier,
14153                                      enum_type,
14154                                      /*is_template=*/false,
14155                                      /*is_namespace=*/false,
14156                                      /*check_dependency=*/true,
14157                                      /*ambiguous_decls=*/NULL,
14158                                      input_location);
14159       if (name)
14160         {
14161           type = TREE_TYPE (name);
14162           if (TREE_CODE (type) == TYPENAME_TYPE)
14163             {
14164               /* Are template enums allowed in ISO? */
14165               if (template_parm_scope_p ())
14166                 pedwarn (type_start_token->location, OPT_pedantic,
14167                          "%qD is an enumeration template", name);
14168               /* ignore a typename reference, for it will be solved by name
14169                  in start_enum.  */
14170               type = NULL_TREE;
14171             }
14172         }
14173       else
14174         error_at (type_start_token->location,
14175                   "%qD is not an enumerator-name", identifier);
14176     }
14177   else
14178     {
14179       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14180         identifier = cp_parser_identifier (parser);
14181       else
14182         {
14183           identifier = make_anon_name ();
14184           is_anonymous = true;
14185         }
14186     }
14187   pop_deferring_access_checks ();
14188
14189   /* Check for the `:' that denotes a specified underlying type in C++0x.
14190      Note that a ':' could also indicate a bitfield width, however.  */
14191   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14192     {
14193       cp_decl_specifier_seq type_specifiers;
14194
14195       /* Consume the `:'.  */
14196       cp_lexer_consume_token (parser->lexer);
14197
14198       /* Parse the type-specifier-seq.  */
14199       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14200                                     /*is_trailing_return=*/false,
14201                                     &type_specifiers);
14202
14203       /* At this point this is surely not elaborated type specifier.  */
14204       if (!cp_parser_parse_definitely (parser))
14205         return NULL_TREE;
14206
14207       if (cxx_dialect < cxx0x)
14208         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14209
14210       has_underlying_type = true;
14211
14212       /* If that didn't work, stop.  */
14213       if (type_specifiers.type != error_mark_node)
14214         {
14215           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14216                                             /*initialized=*/0, NULL);
14217           if (underlying_type == error_mark_node)
14218             underlying_type = NULL_TREE;
14219         }
14220     }
14221
14222   /* Look for the `{' but don't consume it yet.  */
14223   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14224     {
14225       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14226         {
14227           cp_parser_error (parser, "expected %<{%>");
14228           if (has_underlying_type)
14229             {
14230               type = NULL_TREE;
14231               goto out;
14232             }
14233         }
14234       /* An opaque-enum-specifier must have a ';' here.  */
14235       if ((scoped_enum_p || underlying_type)
14236           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14237         {
14238           cp_parser_error (parser, "expected %<;%> or %<{%>");
14239           if (has_underlying_type)
14240             {
14241               type = NULL_TREE;
14242               goto out;
14243             }
14244         }
14245     }
14246
14247   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14248     return NULL_TREE;
14249
14250   if (nested_name_specifier)
14251     {
14252       if (CLASS_TYPE_P (nested_name_specifier))
14253         {
14254           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14255           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14256           push_scope (nested_name_specifier);
14257         }
14258       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14259         {
14260           push_nested_namespace (nested_name_specifier);
14261         }
14262     }
14263
14264   /* Issue an error message if type-definitions are forbidden here.  */
14265   if (!cp_parser_check_type_definition (parser))
14266     type = error_mark_node;
14267   else
14268     /* Create the new type.  We do this before consuming the opening
14269        brace so the enum will be recorded as being on the line of its
14270        tag (or the 'enum' keyword, if there is no tag).  */
14271     type = start_enum (identifier, type, underlying_type,
14272                        scoped_enum_p, &is_new_type);
14273
14274   /* If the next token is not '{' it is an opaque-enum-specifier or an
14275      elaborated-type-specifier.  */
14276   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14277     {
14278       timevar_push (TV_PARSE_ENUM);
14279       if (nested_name_specifier)
14280         {
14281           /* The following catches invalid code such as:
14282              enum class S<int>::E { A, B, C }; */
14283           if (!processing_specialization
14284               && CLASS_TYPE_P (nested_name_specifier)
14285               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14286             error_at (type_start_token->location, "cannot add an enumerator "
14287                       "list to a template instantiation");
14288
14289           /* If that scope does not contain the scope in which the
14290              class was originally declared, the program is invalid.  */
14291           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14292             {
14293               if (at_namespace_scope_p ())
14294                 error_at (type_start_token->location,
14295                           "declaration of %qD in namespace %qD which does not "
14296                           "enclose %qD",
14297                           type, prev_scope, nested_name_specifier);
14298               else
14299                 error_at (type_start_token->location,
14300                           "declaration of %qD in %qD which does not enclose %qD",
14301                           type, prev_scope, nested_name_specifier);
14302               type = error_mark_node;
14303             }
14304         }
14305
14306       if (scoped_enum_p)
14307         begin_scope (sk_scoped_enum, type);
14308
14309       /* Consume the opening brace.  */
14310       cp_lexer_consume_token (parser->lexer);
14311
14312       if (type == error_mark_node)
14313         ; /* Nothing to add */
14314       else if (OPAQUE_ENUM_P (type)
14315                || (cxx_dialect > cxx98 && processing_specialization))
14316         {
14317           new_value_list = true;
14318           SET_OPAQUE_ENUM_P (type, false);
14319           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14320         }
14321       else
14322         {
14323           error_at (type_start_token->location, "multiple definition of %q#T", type);
14324           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14325                     "previous definition here");
14326           type = error_mark_node;
14327         }
14328
14329       if (type == error_mark_node)
14330         cp_parser_skip_to_end_of_block_or_statement (parser);
14331       /* If the next token is not '}', then there are some enumerators.  */
14332       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14333         cp_parser_enumerator_list (parser, type);
14334
14335       /* Consume the final '}'.  */
14336       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14337
14338       if (scoped_enum_p)
14339         finish_scope ();
14340       timevar_pop (TV_PARSE_ENUM);
14341     }
14342   else
14343     {
14344       /* If a ';' follows, then it is an opaque-enum-specifier
14345         and additional restrictions apply.  */
14346       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14347         {
14348           if (is_anonymous)
14349             error_at (type_start_token->location,
14350                       "opaque-enum-specifier without name");
14351           else if (nested_name_specifier)
14352             error_at (type_start_token->location,
14353                       "opaque-enum-specifier must use a simple identifier");
14354         }
14355     }
14356
14357   /* Look for trailing attributes to apply to this enumeration, and
14358      apply them if appropriate.  */
14359   if (cp_parser_allow_gnu_extensions_p (parser))
14360     {
14361       tree trailing_attr = cp_parser_attributes_opt (parser);
14362       trailing_attr = chainon (trailing_attr, attributes);
14363       cplus_decl_attributes (&type,
14364                              trailing_attr,
14365                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14366     }
14367
14368   /* Finish up the enumeration.  */
14369   if (type != error_mark_node)
14370     {
14371       if (new_value_list)
14372         finish_enum_value_list (type);
14373       if (is_new_type)
14374         finish_enum (type);
14375     }
14376
14377   if (nested_name_specifier)
14378     {
14379       if (CLASS_TYPE_P (nested_name_specifier))
14380         {
14381           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14382           pop_scope (nested_name_specifier);
14383         }
14384       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14385         {
14386           pop_nested_namespace (nested_name_specifier);
14387         }
14388     }
14389  out:
14390   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14391   return type;
14392 }
14393
14394 /* Parse an enumerator-list.  The enumerators all have the indicated
14395    TYPE.
14396
14397    enumerator-list:
14398      enumerator-definition
14399      enumerator-list , enumerator-definition  */
14400
14401 static void
14402 cp_parser_enumerator_list (cp_parser* parser, tree type)
14403 {
14404   while (true)
14405     {
14406       /* Parse an enumerator-definition.  */
14407       cp_parser_enumerator_definition (parser, type);
14408
14409       /* If the next token is not a ',', we've reached the end of
14410          the list.  */
14411       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14412         break;
14413       /* Otherwise, consume the `,' and keep going.  */
14414       cp_lexer_consume_token (parser->lexer);
14415       /* If the next token is a `}', there is a trailing comma.  */
14416       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14417         {
14418           if (!in_system_header)
14419             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
14420           break;
14421         }
14422     }
14423 }
14424
14425 /* Parse an enumerator-definition.  The enumerator has the indicated
14426    TYPE.
14427
14428    enumerator-definition:
14429      enumerator
14430      enumerator = constant-expression
14431
14432    enumerator:
14433      identifier  */
14434
14435 static void
14436 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14437 {
14438   tree identifier;
14439   tree value;
14440   location_t loc;
14441
14442   /* Save the input location because we are interested in the location
14443      of the identifier and not the location of the explicit value.  */
14444   loc = cp_lexer_peek_token (parser->lexer)->location;
14445
14446   /* Look for the identifier.  */
14447   identifier = cp_parser_identifier (parser);
14448   if (identifier == error_mark_node)
14449     return;
14450
14451   /* If the next token is an '=', then there is an explicit value.  */
14452   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14453     {
14454       /* Consume the `=' token.  */
14455       cp_lexer_consume_token (parser->lexer);
14456       /* Parse the value.  */
14457       value = cp_parser_constant_expression (parser,
14458                                              /*allow_non_constant_p=*/false,
14459                                              NULL);
14460     }
14461   else
14462     value = NULL_TREE;
14463
14464   /* If we are processing a template, make sure the initializer of the
14465      enumerator doesn't contain any bare template parameter pack.  */
14466   if (check_for_bare_parameter_packs (value))
14467     value = error_mark_node;
14468
14469   /* integral_constant_value will pull out this expression, so make sure
14470      it's folded as appropriate.  */
14471   value = fold_non_dependent_expr (value);
14472
14473   /* Create the enumerator.  */
14474   build_enumerator (identifier, value, type, loc);
14475 }
14476
14477 /* Parse a namespace-name.
14478
14479    namespace-name:
14480      original-namespace-name
14481      namespace-alias
14482
14483    Returns the NAMESPACE_DECL for the namespace.  */
14484
14485 static tree
14486 cp_parser_namespace_name (cp_parser* parser)
14487 {
14488   tree identifier;
14489   tree namespace_decl;
14490
14491   cp_token *token = cp_lexer_peek_token (parser->lexer);
14492
14493   /* Get the name of the namespace.  */
14494   identifier = cp_parser_identifier (parser);
14495   if (identifier == error_mark_node)
14496     return error_mark_node;
14497
14498   /* Look up the identifier in the currently active scope.  Look only
14499      for namespaces, due to:
14500
14501        [basic.lookup.udir]
14502
14503        When looking up a namespace-name in a using-directive or alias
14504        definition, only namespace names are considered.
14505
14506      And:
14507
14508        [basic.lookup.qual]
14509
14510        During the lookup of a name preceding the :: scope resolution
14511        operator, object, function, and enumerator names are ignored.
14512
14513      (Note that cp_parser_qualifying_entity only calls this
14514      function if the token after the name is the scope resolution
14515      operator.)  */
14516   namespace_decl = cp_parser_lookup_name (parser, identifier,
14517                                           none_type,
14518                                           /*is_template=*/false,
14519                                           /*is_namespace=*/true,
14520                                           /*check_dependency=*/true,
14521                                           /*ambiguous_decls=*/NULL,
14522                                           token->location);
14523   /* If it's not a namespace, issue an error.  */
14524   if (namespace_decl == error_mark_node
14525       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14526     {
14527       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14528         error_at (token->location, "%qD is not a namespace-name", identifier);
14529       cp_parser_error (parser, "expected namespace-name");
14530       namespace_decl = error_mark_node;
14531     }
14532
14533   return namespace_decl;
14534 }
14535
14536 /* Parse a namespace-definition.
14537
14538    namespace-definition:
14539      named-namespace-definition
14540      unnamed-namespace-definition
14541
14542    named-namespace-definition:
14543      original-namespace-definition
14544      extension-namespace-definition
14545
14546    original-namespace-definition:
14547      namespace identifier { namespace-body }
14548
14549    extension-namespace-definition:
14550      namespace original-namespace-name { namespace-body }
14551
14552    unnamed-namespace-definition:
14553      namespace { namespace-body } */
14554
14555 static void
14556 cp_parser_namespace_definition (cp_parser* parser)
14557 {
14558   tree identifier, attribs;
14559   bool has_visibility;
14560   bool is_inline;
14561
14562   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14563     {
14564       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14565       is_inline = true;
14566       cp_lexer_consume_token (parser->lexer);
14567     }
14568   else
14569     is_inline = false;
14570
14571   /* Look for the `namespace' keyword.  */
14572   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14573
14574   /* Get the name of the namespace.  We do not attempt to distinguish
14575      between an original-namespace-definition and an
14576      extension-namespace-definition at this point.  The semantic
14577      analysis routines are responsible for that.  */
14578   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14579     identifier = cp_parser_identifier (parser);
14580   else
14581     identifier = NULL_TREE;
14582
14583   /* Parse any specified attributes.  */
14584   attribs = cp_parser_attributes_opt (parser);
14585
14586   /* Look for the `{' to start the namespace.  */
14587   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14588   /* Start the namespace.  */
14589   push_namespace (identifier);
14590
14591   /* "inline namespace" is equivalent to a stub namespace definition
14592      followed by a strong using directive.  */
14593   if (is_inline)
14594     {
14595       tree name_space = current_namespace;
14596       /* Set up namespace association.  */
14597       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14598         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14599                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14600       /* Import the contents of the inline namespace.  */
14601       pop_namespace ();
14602       do_using_directive (name_space);
14603       push_namespace (identifier);
14604     }
14605
14606   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14607
14608   /* Parse the body of the namespace.  */
14609   cp_parser_namespace_body (parser);
14610
14611   if (has_visibility)
14612     pop_visibility (1);
14613
14614   /* Finish the namespace.  */
14615   pop_namespace ();
14616   /* Look for the final `}'.  */
14617   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14618 }
14619
14620 /* Parse a namespace-body.
14621
14622    namespace-body:
14623      declaration-seq [opt]  */
14624
14625 static void
14626 cp_parser_namespace_body (cp_parser* parser)
14627 {
14628   cp_parser_declaration_seq_opt (parser);
14629 }
14630
14631 /* Parse a namespace-alias-definition.
14632
14633    namespace-alias-definition:
14634      namespace identifier = qualified-namespace-specifier ;  */
14635
14636 static void
14637 cp_parser_namespace_alias_definition (cp_parser* parser)
14638 {
14639   tree identifier;
14640   tree namespace_specifier;
14641
14642   cp_token *token = cp_lexer_peek_token (parser->lexer);
14643
14644   /* Look for the `namespace' keyword.  */
14645   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14646   /* Look for the identifier.  */
14647   identifier = cp_parser_identifier (parser);
14648   if (identifier == error_mark_node)
14649     return;
14650   /* Look for the `=' token.  */
14651   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14652       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14653     {
14654       error_at (token->location, "%<namespace%> definition is not allowed here");
14655       /* Skip the definition.  */
14656       cp_lexer_consume_token (parser->lexer);
14657       if (cp_parser_skip_to_closing_brace (parser))
14658         cp_lexer_consume_token (parser->lexer);
14659       return;
14660     }
14661   cp_parser_require (parser, CPP_EQ, RT_EQ);
14662   /* Look for the qualified-namespace-specifier.  */
14663   namespace_specifier
14664     = cp_parser_qualified_namespace_specifier (parser);
14665   /* Look for the `;' token.  */
14666   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14667
14668   /* Register the alias in the symbol table.  */
14669   do_namespace_alias (identifier, namespace_specifier);
14670 }
14671
14672 /* Parse a qualified-namespace-specifier.
14673
14674    qualified-namespace-specifier:
14675      :: [opt] nested-name-specifier [opt] namespace-name
14676
14677    Returns a NAMESPACE_DECL corresponding to the specified
14678    namespace.  */
14679
14680 static tree
14681 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14682 {
14683   /* Look for the optional `::'.  */
14684   cp_parser_global_scope_opt (parser,
14685                               /*current_scope_valid_p=*/false);
14686
14687   /* Look for the optional nested-name-specifier.  */
14688   cp_parser_nested_name_specifier_opt (parser,
14689                                        /*typename_keyword_p=*/false,
14690                                        /*check_dependency_p=*/true,
14691                                        /*type_p=*/false,
14692                                        /*is_declaration=*/true);
14693
14694   return cp_parser_namespace_name (parser);
14695 }
14696
14697 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14698    access declaration.
14699
14700    using-declaration:
14701      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14702      using :: unqualified-id ;  
14703
14704    access-declaration:
14705      qualified-id ;  
14706
14707    */
14708
14709 static bool
14710 cp_parser_using_declaration (cp_parser* parser, 
14711                              bool access_declaration_p)
14712 {
14713   cp_token *token;
14714   bool typename_p = false;
14715   bool global_scope_p;
14716   tree decl;
14717   tree identifier;
14718   tree qscope;
14719
14720   if (access_declaration_p)
14721     cp_parser_parse_tentatively (parser);
14722   else
14723     {
14724       /* Look for the `using' keyword.  */
14725       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14726       
14727       /* Peek at the next token.  */
14728       token = cp_lexer_peek_token (parser->lexer);
14729       /* See if it's `typename'.  */
14730       if (token->keyword == RID_TYPENAME)
14731         {
14732           /* Remember that we've seen it.  */
14733           typename_p = true;
14734           /* Consume the `typename' token.  */
14735           cp_lexer_consume_token (parser->lexer);
14736         }
14737     }
14738
14739   /* Look for the optional global scope qualification.  */
14740   global_scope_p
14741     = (cp_parser_global_scope_opt (parser,
14742                                    /*current_scope_valid_p=*/false)
14743        != NULL_TREE);
14744
14745   /* If we saw `typename', or didn't see `::', then there must be a
14746      nested-name-specifier present.  */
14747   if (typename_p || !global_scope_p)
14748     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14749                                               /*check_dependency_p=*/true,
14750                                               /*type_p=*/false,
14751                                               /*is_declaration=*/true);
14752   /* Otherwise, we could be in either of the two productions.  In that
14753      case, treat the nested-name-specifier as optional.  */
14754   else
14755     qscope = cp_parser_nested_name_specifier_opt (parser,
14756                                                   /*typename_keyword_p=*/false,
14757                                                   /*check_dependency_p=*/true,
14758                                                   /*type_p=*/false,
14759                                                   /*is_declaration=*/true);
14760   if (!qscope)
14761     qscope = global_namespace;
14762
14763   if (access_declaration_p && cp_parser_error_occurred (parser))
14764     /* Something has already gone wrong; there's no need to parse
14765        further.  Since an error has occurred, the return value of
14766        cp_parser_parse_definitely will be false, as required.  */
14767     return cp_parser_parse_definitely (parser);
14768
14769   token = cp_lexer_peek_token (parser->lexer);
14770   /* Parse the unqualified-id.  */
14771   identifier = cp_parser_unqualified_id (parser,
14772                                          /*template_keyword_p=*/false,
14773                                          /*check_dependency_p=*/true,
14774                                          /*declarator_p=*/true,
14775                                          /*optional_p=*/false);
14776
14777   if (access_declaration_p)
14778     {
14779       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14780         cp_parser_simulate_error (parser);
14781       if (!cp_parser_parse_definitely (parser))
14782         return false;
14783     }
14784
14785   /* The function we call to handle a using-declaration is different
14786      depending on what scope we are in.  */
14787   if (qscope == error_mark_node || identifier == error_mark_node)
14788     ;
14789   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14790            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14791     /* [namespace.udecl]
14792
14793        A using declaration shall not name a template-id.  */
14794     error_at (token->location,
14795               "a template-id may not appear in a using-declaration");
14796   else
14797     {
14798       if (at_class_scope_p ())
14799         {
14800           /* Create the USING_DECL.  */
14801           decl = do_class_using_decl (parser->scope, identifier);
14802
14803           if (check_for_bare_parameter_packs (decl))
14804             return false;
14805           else
14806             /* Add it to the list of members in this class.  */
14807             finish_member_declaration (decl);
14808         }
14809       else
14810         {
14811           decl = cp_parser_lookup_name_simple (parser,
14812                                                identifier,
14813                                                token->location);
14814           if (decl == error_mark_node)
14815             cp_parser_name_lookup_error (parser, identifier,
14816                                          decl, NLE_NULL,
14817                                          token->location);
14818           else if (check_for_bare_parameter_packs (decl))
14819             return false;
14820           else if (!at_namespace_scope_p ())
14821             do_local_using_decl (decl, qscope, identifier);
14822           else
14823             do_toplevel_using_decl (decl, qscope, identifier);
14824         }
14825     }
14826
14827   /* Look for the final `;'.  */
14828   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14829   
14830   return true;
14831 }
14832
14833 /* Parse a using-directive.
14834
14835    using-directive:
14836      using namespace :: [opt] nested-name-specifier [opt]
14837        namespace-name ;  */
14838
14839 static void
14840 cp_parser_using_directive (cp_parser* parser)
14841 {
14842   tree namespace_decl;
14843   tree attribs;
14844
14845   /* Look for the `using' keyword.  */
14846   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14847   /* And the `namespace' keyword.  */
14848   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14849   /* Look for the optional `::' operator.  */
14850   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14851   /* And the optional nested-name-specifier.  */
14852   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   /* Get the namespace being used.  */
14858   namespace_decl = cp_parser_namespace_name (parser);
14859   /* And any specified attributes.  */
14860   attribs = cp_parser_attributes_opt (parser);
14861   /* Update the symbol table.  */
14862   parse_using_directive (namespace_decl, attribs);
14863   /* Look for the final `;'.  */
14864   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14865 }
14866
14867 /* Parse an asm-definition.
14868
14869    asm-definition:
14870      asm ( string-literal ) ;
14871
14872    GNU Extension:
14873
14874    asm-definition:
14875      asm volatile [opt] ( string-literal ) ;
14876      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14877      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14878                           : asm-operand-list [opt] ) ;
14879      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14880                           : asm-operand-list [opt]
14881                           : asm-clobber-list [opt] ) ;
14882      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14883                                : asm-clobber-list [opt]
14884                                : asm-goto-list ) ;  */
14885
14886 static void
14887 cp_parser_asm_definition (cp_parser* parser)
14888 {
14889   tree string;
14890   tree outputs = NULL_TREE;
14891   tree inputs = NULL_TREE;
14892   tree clobbers = NULL_TREE;
14893   tree labels = NULL_TREE;
14894   tree asm_stmt;
14895   bool volatile_p = false;
14896   bool extended_p = false;
14897   bool invalid_inputs_p = false;
14898   bool invalid_outputs_p = false;
14899   bool goto_p = false;
14900   required_token missing = RT_NONE;
14901
14902   /* Look for the `asm' keyword.  */
14903   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14904   /* See if the next token is `volatile'.  */
14905   if (cp_parser_allow_gnu_extensions_p (parser)
14906       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14907     {
14908       /* Remember that we saw the `volatile' keyword.  */
14909       volatile_p = true;
14910       /* Consume the token.  */
14911       cp_lexer_consume_token (parser->lexer);
14912     }
14913   if (cp_parser_allow_gnu_extensions_p (parser)
14914       && parser->in_function_body
14915       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14916     {
14917       /* Remember that we saw the `goto' keyword.  */
14918       goto_p = true;
14919       /* Consume the token.  */
14920       cp_lexer_consume_token (parser->lexer);
14921     }
14922   /* Look for the opening `('.  */
14923   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14924     return;
14925   /* Look for the string.  */
14926   string = cp_parser_string_literal (parser, false, false);
14927   if (string == error_mark_node)
14928     {
14929       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14930                                              /*consume_paren=*/true);
14931       return;
14932     }
14933
14934   /* If we're allowing GNU extensions, check for the extended assembly
14935      syntax.  Unfortunately, the `:' tokens need not be separated by
14936      a space in C, and so, for compatibility, we tolerate that here
14937      too.  Doing that means that we have to treat the `::' operator as
14938      two `:' tokens.  */
14939   if (cp_parser_allow_gnu_extensions_p (parser)
14940       && parser->in_function_body
14941       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14942           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14943     {
14944       bool inputs_p = false;
14945       bool clobbers_p = false;
14946       bool labels_p = false;
14947
14948       /* The extended syntax was used.  */
14949       extended_p = true;
14950
14951       /* Look for outputs.  */
14952       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14953         {
14954           /* Consume the `:'.  */
14955           cp_lexer_consume_token (parser->lexer);
14956           /* Parse the output-operands.  */
14957           if (cp_lexer_next_token_is_not (parser->lexer,
14958                                           CPP_COLON)
14959               && cp_lexer_next_token_is_not (parser->lexer,
14960                                              CPP_SCOPE)
14961               && cp_lexer_next_token_is_not (parser->lexer,
14962                                              CPP_CLOSE_PAREN)
14963               && !goto_p)
14964             outputs = cp_parser_asm_operand_list (parser);
14965
14966             if (outputs == error_mark_node)
14967               invalid_outputs_p = true;
14968         }
14969       /* If the next token is `::', there are no outputs, and the
14970          next token is the beginning of the inputs.  */
14971       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14972         /* The inputs are coming next.  */
14973         inputs_p = true;
14974
14975       /* Look for inputs.  */
14976       if (inputs_p
14977           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14978         {
14979           /* Consume the `:' or `::'.  */
14980           cp_lexer_consume_token (parser->lexer);
14981           /* Parse the output-operands.  */
14982           if (cp_lexer_next_token_is_not (parser->lexer,
14983                                           CPP_COLON)
14984               && cp_lexer_next_token_is_not (parser->lexer,
14985                                              CPP_SCOPE)
14986               && cp_lexer_next_token_is_not (parser->lexer,
14987                                              CPP_CLOSE_PAREN))
14988             inputs = cp_parser_asm_operand_list (parser);
14989
14990             if (inputs == error_mark_node)
14991               invalid_inputs_p = true;
14992         }
14993       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14994         /* The clobbers are coming next.  */
14995         clobbers_p = true;
14996
14997       /* Look for clobbers.  */
14998       if (clobbers_p
14999           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15000         {
15001           clobbers_p = true;
15002           /* Consume the `:' or `::'.  */
15003           cp_lexer_consume_token (parser->lexer);
15004           /* Parse the clobbers.  */
15005           if (cp_lexer_next_token_is_not (parser->lexer,
15006                                           CPP_COLON)
15007               && cp_lexer_next_token_is_not (parser->lexer,
15008                                              CPP_CLOSE_PAREN))
15009             clobbers = cp_parser_asm_clobber_list (parser);
15010         }
15011       else if (goto_p
15012                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15013         /* The labels are coming next.  */
15014         labels_p = true;
15015
15016       /* Look for labels.  */
15017       if (labels_p
15018           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15019         {
15020           labels_p = true;
15021           /* Consume the `:' or `::'.  */
15022           cp_lexer_consume_token (parser->lexer);
15023           /* Parse the labels.  */
15024           labels = cp_parser_asm_label_list (parser);
15025         }
15026
15027       if (goto_p && !labels_p)
15028         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15029     }
15030   else if (goto_p)
15031     missing = RT_COLON_SCOPE;
15032
15033   /* Look for the closing `)'.  */
15034   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15035                           missing ? missing : RT_CLOSE_PAREN))
15036     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15037                                            /*consume_paren=*/true);
15038   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15039
15040   if (!invalid_inputs_p && !invalid_outputs_p)
15041     {
15042       /* Create the ASM_EXPR.  */
15043       if (parser->in_function_body)
15044         {
15045           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15046                                       inputs, clobbers, labels);
15047           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15048           if (!extended_p)
15049             {
15050               tree temp = asm_stmt;
15051               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15052                 temp = TREE_OPERAND (temp, 0);
15053
15054               ASM_INPUT_P (temp) = 1;
15055             }
15056         }
15057       else
15058         cgraph_add_asm_node (string);
15059     }
15060 }
15061
15062 /* Declarators [gram.dcl.decl] */
15063
15064 /* Parse an init-declarator.
15065
15066    init-declarator:
15067      declarator initializer [opt]
15068
15069    GNU Extension:
15070
15071    init-declarator:
15072      declarator asm-specification [opt] attributes [opt] initializer [opt]
15073
15074    function-definition:
15075      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15076        function-body
15077      decl-specifier-seq [opt] declarator function-try-block
15078
15079    GNU Extension:
15080
15081    function-definition:
15082      __extension__ function-definition
15083
15084    The DECL_SPECIFIERS apply to this declarator.  Returns a
15085    representation of the entity declared.  If MEMBER_P is TRUE, then
15086    this declarator appears in a class scope.  The new DECL created by
15087    this declarator is returned.
15088
15089    The CHECKS are access checks that should be performed once we know
15090    what entity is being declared (and, therefore, what classes have
15091    befriended it).
15092
15093    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15094    for a function-definition here as well.  If the declarator is a
15095    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15096    be TRUE upon return.  By that point, the function-definition will
15097    have been completely parsed.
15098
15099    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15100    is FALSE.
15101
15102    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15103    parsed declaration if it is an uninitialized single declarator not followed
15104    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15105    if present, will not be consumed.  If returned, this declarator will be
15106    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15107
15108 static tree
15109 cp_parser_init_declarator (cp_parser* parser,
15110                            cp_decl_specifier_seq *decl_specifiers,
15111                            VEC (deferred_access_check,gc)* checks,
15112                            bool function_definition_allowed_p,
15113                            bool member_p,
15114                            int declares_class_or_enum,
15115                            bool* function_definition_p,
15116                            tree* maybe_range_for_decl)
15117 {
15118   cp_token *token = NULL, *asm_spec_start_token = NULL,
15119            *attributes_start_token = NULL;
15120   cp_declarator *declarator;
15121   tree prefix_attributes;
15122   tree attributes;
15123   tree asm_specification;
15124   tree initializer;
15125   tree decl = NULL_TREE;
15126   tree scope;
15127   int is_initialized;
15128   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15129      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15130      "(...)".  */
15131   enum cpp_ttype initialization_kind;
15132   bool is_direct_init = false;
15133   bool is_non_constant_init;
15134   int ctor_dtor_or_conv_p;
15135   bool friend_p;
15136   tree pushed_scope = NULL_TREE;
15137   bool range_for_decl_p = false;
15138
15139   /* Gather the attributes that were provided with the
15140      decl-specifiers.  */
15141   prefix_attributes = decl_specifiers->attributes;
15142
15143   /* Assume that this is not the declarator for a function
15144      definition.  */
15145   if (function_definition_p)
15146     *function_definition_p = false;
15147
15148   /* Defer access checks while parsing the declarator; we cannot know
15149      what names are accessible until we know what is being
15150      declared.  */
15151   resume_deferring_access_checks ();
15152
15153   /* Parse the declarator.  */
15154   token = cp_lexer_peek_token (parser->lexer);
15155   declarator
15156     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15157                             &ctor_dtor_or_conv_p,
15158                             /*parenthesized_p=*/NULL,
15159                             member_p);
15160   /* Gather up the deferred checks.  */
15161   stop_deferring_access_checks ();
15162
15163   /* If the DECLARATOR was erroneous, there's no need to go
15164      further.  */
15165   if (declarator == cp_error_declarator)
15166     return error_mark_node;
15167
15168   /* Check that the number of template-parameter-lists is OK.  */
15169   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15170                                                        token->location))
15171     return error_mark_node;
15172
15173   if (declares_class_or_enum & 2)
15174     cp_parser_check_for_definition_in_return_type (declarator,
15175                                                    decl_specifiers->type,
15176                                                    decl_specifiers->type_location);
15177
15178   /* Figure out what scope the entity declared by the DECLARATOR is
15179      located in.  `grokdeclarator' sometimes changes the scope, so
15180      we compute it now.  */
15181   scope = get_scope_of_declarator (declarator);
15182
15183   /* Perform any lookups in the declared type which were thought to be
15184      dependent, but are not in the scope of the declarator.  */
15185   decl_specifiers->type
15186     = maybe_update_decl_type (decl_specifiers->type, scope);
15187
15188   /* If we're allowing GNU extensions, look for an asm-specification
15189      and attributes.  */
15190   if (cp_parser_allow_gnu_extensions_p (parser))
15191     {
15192       /* Look for an asm-specification.  */
15193       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15194       asm_specification = cp_parser_asm_specification_opt (parser);
15195       /* And attributes.  */
15196       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15197       attributes = cp_parser_attributes_opt (parser);
15198     }
15199   else
15200     {
15201       asm_specification = NULL_TREE;
15202       attributes = NULL_TREE;
15203     }
15204
15205   /* Peek at the next token.  */
15206   token = cp_lexer_peek_token (parser->lexer);
15207   /* Check to see if the token indicates the start of a
15208      function-definition.  */
15209   if (function_declarator_p (declarator)
15210       && cp_parser_token_starts_function_definition_p (token))
15211     {
15212       if (!function_definition_allowed_p)
15213         {
15214           /* If a function-definition should not appear here, issue an
15215              error message.  */
15216           cp_parser_error (parser,
15217                            "a function-definition is not allowed here");
15218           return error_mark_node;
15219         }
15220       else
15221         {
15222           location_t func_brace_location
15223             = cp_lexer_peek_token (parser->lexer)->location;
15224
15225           /* Neither attributes nor an asm-specification are allowed
15226              on a function-definition.  */
15227           if (asm_specification)
15228             error_at (asm_spec_start_token->location,
15229                       "an asm-specification is not allowed "
15230                       "on a function-definition");
15231           if (attributes)
15232             error_at (attributes_start_token->location,
15233                       "attributes are not allowed on a function-definition");
15234           /* This is a function-definition.  */
15235           *function_definition_p = true;
15236
15237           /* Parse the function definition.  */
15238           if (member_p)
15239             decl = cp_parser_save_member_function_body (parser,
15240                                                         decl_specifiers,
15241                                                         declarator,
15242                                                         prefix_attributes);
15243           else
15244             decl
15245               = (cp_parser_function_definition_from_specifiers_and_declarator
15246                  (parser, decl_specifiers, prefix_attributes, declarator));
15247
15248           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15249             {
15250               /* This is where the prologue starts...  */
15251               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15252                 = func_brace_location;
15253             }
15254
15255           return decl;
15256         }
15257     }
15258
15259   /* [dcl.dcl]
15260
15261      Only in function declarations for constructors, destructors, and
15262      type conversions can the decl-specifier-seq be omitted.
15263
15264      We explicitly postpone this check past the point where we handle
15265      function-definitions because we tolerate function-definitions
15266      that are missing their return types in some modes.  */
15267   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15268     {
15269       cp_parser_error (parser,
15270                        "expected constructor, destructor, or type conversion");
15271       return error_mark_node;
15272     }
15273
15274   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15275   if (token->type == CPP_EQ
15276       || token->type == CPP_OPEN_PAREN
15277       || token->type == CPP_OPEN_BRACE)
15278     {
15279       is_initialized = SD_INITIALIZED;
15280       initialization_kind = token->type;
15281       if (maybe_range_for_decl)
15282         *maybe_range_for_decl = error_mark_node;
15283
15284       if (token->type == CPP_EQ
15285           && function_declarator_p (declarator))
15286         {
15287           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15288           if (t2->keyword == RID_DEFAULT)
15289             is_initialized = SD_DEFAULTED;
15290           else if (t2->keyword == RID_DELETE)
15291             is_initialized = SD_DELETED;
15292         }
15293     }
15294   else
15295     {
15296       /* If the init-declarator isn't initialized and isn't followed by a
15297          `,' or `;', it's not a valid init-declarator.  */
15298       if (token->type != CPP_COMMA
15299           && token->type != CPP_SEMICOLON)
15300         {
15301           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15302             range_for_decl_p = true;
15303           else
15304             {
15305               cp_parser_error (parser, "expected initializer");
15306               return error_mark_node;
15307             }
15308         }
15309       is_initialized = SD_UNINITIALIZED;
15310       initialization_kind = CPP_EOF;
15311     }
15312
15313   /* Because start_decl has side-effects, we should only call it if we
15314      know we're going ahead.  By this point, we know that we cannot
15315      possibly be looking at any other construct.  */
15316   cp_parser_commit_to_tentative_parse (parser);
15317
15318   /* If the decl specifiers were bad, issue an error now that we're
15319      sure this was intended to be a declarator.  Then continue
15320      declaring the variable(s), as int, to try to cut down on further
15321      errors.  */
15322   if (decl_specifiers->any_specifiers_p
15323       && decl_specifiers->type == error_mark_node)
15324     {
15325       cp_parser_error (parser, "invalid type in declaration");
15326       decl_specifiers->type = integer_type_node;
15327     }
15328
15329   /* Check to see whether or not this declaration is a friend.  */
15330   friend_p = cp_parser_friend_p (decl_specifiers);
15331
15332   /* Enter the newly declared entry in the symbol table.  If we're
15333      processing a declaration in a class-specifier, we wait until
15334      after processing the initializer.  */
15335   if (!member_p)
15336     {
15337       if (parser->in_unbraced_linkage_specification_p)
15338         decl_specifiers->storage_class = sc_extern;
15339       decl = start_decl (declarator, decl_specifiers,
15340                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15341                          attributes, prefix_attributes,
15342                          &pushed_scope);
15343       /* Adjust location of decl if declarator->id_loc is more appropriate:
15344          set, and decl wasn't merged with another decl, in which case its
15345          location would be different from input_location, and more accurate.  */
15346       if (DECL_P (decl)
15347           && declarator->id_loc != UNKNOWN_LOCATION
15348           && DECL_SOURCE_LOCATION (decl) == input_location)
15349         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15350     }
15351   else if (scope)
15352     /* Enter the SCOPE.  That way unqualified names appearing in the
15353        initializer will be looked up in SCOPE.  */
15354     pushed_scope = push_scope (scope);
15355
15356   /* Perform deferred access control checks, now that we know in which
15357      SCOPE the declared entity resides.  */
15358   if (!member_p && decl)
15359     {
15360       tree saved_current_function_decl = NULL_TREE;
15361
15362       /* If the entity being declared is a function, pretend that we
15363          are in its scope.  If it is a `friend', it may have access to
15364          things that would not otherwise be accessible.  */
15365       if (TREE_CODE (decl) == FUNCTION_DECL)
15366         {
15367           saved_current_function_decl = current_function_decl;
15368           current_function_decl = decl;
15369         }
15370
15371       /* Perform access checks for template parameters.  */
15372       cp_parser_perform_template_parameter_access_checks (checks);
15373
15374       /* Perform the access control checks for the declarator and the
15375          decl-specifiers.  */
15376       perform_deferred_access_checks ();
15377
15378       /* Restore the saved value.  */
15379       if (TREE_CODE (decl) == FUNCTION_DECL)
15380         current_function_decl = saved_current_function_decl;
15381     }
15382
15383   /* Parse the initializer.  */
15384   initializer = NULL_TREE;
15385   is_direct_init = false;
15386   is_non_constant_init = true;
15387   if (is_initialized)
15388     {
15389       if (function_declarator_p (declarator))
15390         {
15391           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15392            if (initialization_kind == CPP_EQ)
15393              initializer = cp_parser_pure_specifier (parser);
15394            else
15395              {
15396                /* If the declaration was erroneous, we don't really
15397                   know what the user intended, so just silently
15398                   consume the initializer.  */
15399                if (decl != error_mark_node)
15400                  error_at (initializer_start_token->location,
15401                            "initializer provided for function");
15402                cp_parser_skip_to_closing_parenthesis (parser,
15403                                                       /*recovering=*/true,
15404                                                       /*or_comma=*/false,
15405                                                       /*consume_paren=*/true);
15406              }
15407         }
15408       else
15409         {
15410           /* We want to record the extra mangling scope for in-class
15411              initializers of class members and initializers of static data
15412              member templates.  The former is a C++0x feature which isn't
15413              implemented yet, and I expect it will involve deferring
15414              parsing of the initializer until end of class as with default
15415              arguments.  So right here we only handle the latter.  */
15416           if (!member_p && processing_template_decl)
15417             start_lambda_scope (decl);
15418           initializer = cp_parser_initializer (parser,
15419                                                &is_direct_init,
15420                                                &is_non_constant_init);
15421           if (!member_p && processing_template_decl)
15422             finish_lambda_scope ();
15423         }
15424     }
15425
15426   /* The old parser allows attributes to appear after a parenthesized
15427      initializer.  Mark Mitchell proposed removing this functionality
15428      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15429      attributes -- but ignores them.  */
15430   if (cp_parser_allow_gnu_extensions_p (parser)
15431       && initialization_kind == CPP_OPEN_PAREN)
15432     if (cp_parser_attributes_opt (parser))
15433       warning (OPT_Wattributes,
15434                "attributes after parenthesized initializer ignored");
15435
15436   /* For an in-class declaration, use `grokfield' to create the
15437      declaration.  */
15438   if (member_p)
15439     {
15440       if (pushed_scope)
15441         {
15442           pop_scope (pushed_scope);
15443           pushed_scope = NULL_TREE;
15444         }
15445       decl = grokfield (declarator, decl_specifiers,
15446                         initializer, !is_non_constant_init,
15447                         /*asmspec=*/NULL_TREE,
15448                         prefix_attributes);
15449       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15450         cp_parser_save_default_args (parser, decl);
15451     }
15452
15453   /* Finish processing the declaration.  But, skip member
15454      declarations.  */
15455   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15456     {
15457       cp_finish_decl (decl,
15458                       initializer, !is_non_constant_init,
15459                       asm_specification,
15460                       /* If the initializer is in parentheses, then this is
15461                          a direct-initialization, which means that an
15462                          `explicit' constructor is OK.  Otherwise, an
15463                          `explicit' constructor cannot be used.  */
15464                       ((is_direct_init || !is_initialized)
15465                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15466     }
15467   else if ((cxx_dialect != cxx98) && friend_p
15468            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15469     /* Core issue #226 (C++0x only): A default template-argument
15470        shall not be specified in a friend class template
15471        declaration. */
15472     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15473                              /*is_partial=*/0, /*is_friend_decl=*/1);
15474
15475   if (!friend_p && pushed_scope)
15476     pop_scope (pushed_scope);
15477
15478   return decl;
15479 }
15480
15481 /* Parse a declarator.
15482
15483    declarator:
15484      direct-declarator
15485      ptr-operator declarator
15486
15487    abstract-declarator:
15488      ptr-operator abstract-declarator [opt]
15489      direct-abstract-declarator
15490
15491    GNU Extensions:
15492
15493    declarator:
15494      attributes [opt] direct-declarator
15495      attributes [opt] ptr-operator declarator
15496
15497    abstract-declarator:
15498      attributes [opt] ptr-operator abstract-declarator [opt]
15499      attributes [opt] direct-abstract-declarator
15500
15501    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15502    detect constructor, destructor or conversion operators. It is set
15503    to -1 if the declarator is a name, and +1 if it is a
15504    function. Otherwise it is set to zero. Usually you just want to
15505    test for >0, but internally the negative value is used.
15506
15507    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15508    a decl-specifier-seq unless it declares a constructor, destructor,
15509    or conversion.  It might seem that we could check this condition in
15510    semantic analysis, rather than parsing, but that makes it difficult
15511    to handle something like `f()'.  We want to notice that there are
15512    no decl-specifiers, and therefore realize that this is an
15513    expression, not a declaration.)
15514
15515    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15516    the declarator is a direct-declarator of the form "(...)".
15517
15518    MEMBER_P is true iff this declarator is a member-declarator.  */
15519
15520 static cp_declarator *
15521 cp_parser_declarator (cp_parser* parser,
15522                       cp_parser_declarator_kind dcl_kind,
15523                       int* ctor_dtor_or_conv_p,
15524                       bool* parenthesized_p,
15525                       bool member_p)
15526 {
15527   cp_declarator *declarator;
15528   enum tree_code code;
15529   cp_cv_quals cv_quals;
15530   tree class_type;
15531   tree attributes = NULL_TREE;
15532
15533   /* Assume this is not a constructor, destructor, or type-conversion
15534      operator.  */
15535   if (ctor_dtor_or_conv_p)
15536     *ctor_dtor_or_conv_p = 0;
15537
15538   if (cp_parser_allow_gnu_extensions_p (parser))
15539     attributes = cp_parser_attributes_opt (parser);
15540
15541   /* Check for the ptr-operator production.  */
15542   cp_parser_parse_tentatively (parser);
15543   /* Parse the ptr-operator.  */
15544   code = cp_parser_ptr_operator (parser,
15545                                  &class_type,
15546                                  &cv_quals);
15547   /* If that worked, then we have a ptr-operator.  */
15548   if (cp_parser_parse_definitely (parser))
15549     {
15550       /* If a ptr-operator was found, then this declarator was not
15551          parenthesized.  */
15552       if (parenthesized_p)
15553         *parenthesized_p = true;
15554       /* The dependent declarator is optional if we are parsing an
15555          abstract-declarator.  */
15556       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15557         cp_parser_parse_tentatively (parser);
15558
15559       /* Parse the dependent declarator.  */
15560       declarator = cp_parser_declarator (parser, dcl_kind,
15561                                          /*ctor_dtor_or_conv_p=*/NULL,
15562                                          /*parenthesized_p=*/NULL,
15563                                          /*member_p=*/false);
15564
15565       /* If we are parsing an abstract-declarator, we must handle the
15566          case where the dependent declarator is absent.  */
15567       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15568           && !cp_parser_parse_definitely (parser))
15569         declarator = NULL;
15570
15571       declarator = cp_parser_make_indirect_declarator
15572         (code, class_type, cv_quals, declarator);
15573     }
15574   /* Everything else is a direct-declarator.  */
15575   else
15576     {
15577       if (parenthesized_p)
15578         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15579                                                    CPP_OPEN_PAREN);
15580       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15581                                                 ctor_dtor_or_conv_p,
15582                                                 member_p);
15583     }
15584
15585   if (attributes && declarator && declarator != cp_error_declarator)
15586     declarator->attributes = attributes;
15587
15588   return declarator;
15589 }
15590
15591 /* Parse a direct-declarator or direct-abstract-declarator.
15592
15593    direct-declarator:
15594      declarator-id
15595      direct-declarator ( parameter-declaration-clause )
15596        cv-qualifier-seq [opt]
15597        exception-specification [opt]
15598      direct-declarator [ constant-expression [opt] ]
15599      ( declarator )
15600
15601    direct-abstract-declarator:
15602      direct-abstract-declarator [opt]
15603        ( parameter-declaration-clause )
15604        cv-qualifier-seq [opt]
15605        exception-specification [opt]
15606      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15607      ( abstract-declarator )
15608
15609    Returns a representation of the declarator.  DCL_KIND is
15610    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15611    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15612    we are parsing a direct-declarator.  It is
15613    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15614    of ambiguity we prefer an abstract declarator, as per
15615    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15616    cp_parser_declarator.  */
15617
15618 static cp_declarator *
15619 cp_parser_direct_declarator (cp_parser* parser,
15620                              cp_parser_declarator_kind dcl_kind,
15621                              int* ctor_dtor_or_conv_p,
15622                              bool member_p)
15623 {
15624   cp_token *token;
15625   cp_declarator *declarator = NULL;
15626   tree scope = NULL_TREE;
15627   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15628   bool saved_in_declarator_p = parser->in_declarator_p;
15629   bool first = true;
15630   tree pushed_scope = NULL_TREE;
15631
15632   while (true)
15633     {
15634       /* Peek at the next token.  */
15635       token = cp_lexer_peek_token (parser->lexer);
15636       if (token->type == CPP_OPEN_PAREN)
15637         {
15638           /* This is either a parameter-declaration-clause, or a
15639              parenthesized declarator. When we know we are parsing a
15640              named declarator, it must be a parenthesized declarator
15641              if FIRST is true. For instance, `(int)' is a
15642              parameter-declaration-clause, with an omitted
15643              direct-abstract-declarator. But `((*))', is a
15644              parenthesized abstract declarator. Finally, when T is a
15645              template parameter `(T)' is a
15646              parameter-declaration-clause, and not a parenthesized
15647              named declarator.
15648
15649              We first try and parse a parameter-declaration-clause,
15650              and then try a nested declarator (if FIRST is true).
15651
15652              It is not an error for it not to be a
15653              parameter-declaration-clause, even when FIRST is
15654              false. Consider,
15655
15656                int i (int);
15657                int i (3);
15658
15659              The first is the declaration of a function while the
15660              second is the definition of a variable, including its
15661              initializer.
15662
15663              Having seen only the parenthesis, we cannot know which of
15664              these two alternatives should be selected.  Even more
15665              complex are examples like:
15666
15667                int i (int (a));
15668                int i (int (3));
15669
15670              The former is a function-declaration; the latter is a
15671              variable initialization.
15672
15673              Thus again, we try a parameter-declaration-clause, and if
15674              that fails, we back out and return.  */
15675
15676           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15677             {
15678               tree params;
15679               unsigned saved_num_template_parameter_lists;
15680               bool is_declarator = false;
15681               tree t;
15682
15683               /* In a member-declarator, the only valid interpretation
15684                  of a parenthesis is the start of a
15685                  parameter-declaration-clause.  (It is invalid to
15686                  initialize a static data member with a parenthesized
15687                  initializer; only the "=" form of initialization is
15688                  permitted.)  */
15689               if (!member_p)
15690                 cp_parser_parse_tentatively (parser);
15691
15692               /* Consume the `('.  */
15693               cp_lexer_consume_token (parser->lexer);
15694               if (first)
15695                 {
15696                   /* If this is going to be an abstract declarator, we're
15697                      in a declarator and we can't have default args.  */
15698                   parser->default_arg_ok_p = false;
15699                   parser->in_declarator_p = true;
15700                 }
15701
15702               /* Inside the function parameter list, surrounding
15703                  template-parameter-lists do not apply.  */
15704               saved_num_template_parameter_lists
15705                 = parser->num_template_parameter_lists;
15706               parser->num_template_parameter_lists = 0;
15707
15708               begin_scope (sk_function_parms, NULL_TREE);
15709
15710               /* Parse the parameter-declaration-clause.  */
15711               params = cp_parser_parameter_declaration_clause (parser);
15712
15713               parser->num_template_parameter_lists
15714                 = saved_num_template_parameter_lists;
15715
15716               /* Consume the `)'.  */
15717               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15718
15719               /* If all went well, parse the cv-qualifier-seq and the
15720                  exception-specification.  */
15721               if (member_p || cp_parser_parse_definitely (parser))
15722                 {
15723                   cp_cv_quals cv_quals;
15724                   cp_virt_specifiers virt_specifiers;
15725                   tree exception_specification;
15726                   tree late_return;
15727
15728                   is_declarator = true;
15729
15730                   if (ctor_dtor_or_conv_p)
15731                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15732                   first = false;
15733
15734                   /* Parse the cv-qualifier-seq.  */
15735                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15736                   /* And the exception-specification.  */
15737                   exception_specification
15738                     = cp_parser_exception_specification_opt (parser);
15739                   /* Parse the virt-specifier-seq.  */
15740                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15741
15742                   late_return = (cp_parser_late_return_type_opt
15743                                  (parser, member_p ? cv_quals : -1));
15744
15745                   /* Create the function-declarator.  */
15746                   declarator = make_call_declarator (declarator,
15747                                                      params,
15748                                                      cv_quals,
15749                                                      virt_specifiers,
15750                                                      exception_specification,
15751                                                      late_return);
15752                   /* Any subsequent parameter lists are to do with
15753                      return type, so are not those of the declared
15754                      function.  */
15755                   parser->default_arg_ok_p = false;
15756                 }
15757
15758               /* Remove the function parms from scope.  */
15759               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15760                 pop_binding (DECL_NAME (t), t);
15761               leave_scope();
15762
15763               if (is_declarator)
15764                 /* Repeat the main loop.  */
15765                 continue;
15766             }
15767
15768           /* If this is the first, we can try a parenthesized
15769              declarator.  */
15770           if (first)
15771             {
15772               bool saved_in_type_id_in_expr_p;
15773
15774               parser->default_arg_ok_p = saved_default_arg_ok_p;
15775               parser->in_declarator_p = saved_in_declarator_p;
15776
15777               /* Consume the `('.  */
15778               cp_lexer_consume_token (parser->lexer);
15779               /* Parse the nested declarator.  */
15780               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15781               parser->in_type_id_in_expr_p = true;
15782               declarator
15783                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15784                                         /*parenthesized_p=*/NULL,
15785                                         member_p);
15786               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15787               first = false;
15788               /* Expect a `)'.  */
15789               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15790                 declarator = cp_error_declarator;
15791               if (declarator == cp_error_declarator)
15792                 break;
15793
15794               goto handle_declarator;
15795             }
15796           /* Otherwise, we must be done.  */
15797           else
15798             break;
15799         }
15800       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15801                && token->type == CPP_OPEN_SQUARE)
15802         {
15803           /* Parse an array-declarator.  */
15804           tree bounds;
15805
15806           if (ctor_dtor_or_conv_p)
15807             *ctor_dtor_or_conv_p = 0;
15808
15809           first = false;
15810           parser->default_arg_ok_p = false;
15811           parser->in_declarator_p = true;
15812           /* Consume the `['.  */
15813           cp_lexer_consume_token (parser->lexer);
15814           /* Peek at the next token.  */
15815           token = cp_lexer_peek_token (parser->lexer);
15816           /* If the next token is `]', then there is no
15817              constant-expression.  */
15818           if (token->type != CPP_CLOSE_SQUARE)
15819             {
15820               bool non_constant_p;
15821
15822               bounds
15823                 = cp_parser_constant_expression (parser,
15824                                                  /*allow_non_constant=*/true,
15825                                                  &non_constant_p);
15826               if (!non_constant_p)
15827                 /* OK */;
15828               /* Normally, the array bound must be an integral constant
15829                  expression.  However, as an extension, we allow VLAs
15830                  in function scopes as long as they aren't part of a
15831                  parameter declaration.  */
15832               else if (!parser->in_function_body
15833                        || current_binding_level->kind == sk_function_parms)
15834                 {
15835                   cp_parser_error (parser,
15836                                    "array bound is not an integer constant");
15837                   bounds = error_mark_node;
15838                 }
15839               else if (processing_template_decl && !error_operand_p (bounds))
15840                 {
15841                   /* Remember this wasn't a constant-expression.  */
15842                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15843                   TREE_SIDE_EFFECTS (bounds) = 1;
15844                 }
15845             }
15846           else
15847             bounds = NULL_TREE;
15848           /* Look for the closing `]'.  */
15849           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15850             {
15851               declarator = cp_error_declarator;
15852               break;
15853             }
15854
15855           declarator = make_array_declarator (declarator, bounds);
15856         }
15857       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15858         {
15859           {
15860             tree qualifying_scope;
15861             tree unqualified_name;
15862             special_function_kind sfk;
15863             bool abstract_ok;
15864             bool pack_expansion_p = false;
15865             cp_token *declarator_id_start_token;
15866
15867             /* Parse a declarator-id */
15868             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15869             if (abstract_ok)
15870               {
15871                 cp_parser_parse_tentatively (parser);
15872
15873                 /* If we see an ellipsis, we should be looking at a
15874                    parameter pack. */
15875                 if (token->type == CPP_ELLIPSIS)
15876                   {
15877                     /* Consume the `...' */
15878                     cp_lexer_consume_token (parser->lexer);
15879
15880                     pack_expansion_p = true;
15881                   }
15882               }
15883
15884             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15885             unqualified_name
15886               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15887             qualifying_scope = parser->scope;
15888             if (abstract_ok)
15889               {
15890                 bool okay = false;
15891
15892                 if (!unqualified_name && pack_expansion_p)
15893                   {
15894                     /* Check whether an error occurred. */
15895                     okay = !cp_parser_error_occurred (parser);
15896
15897                     /* We already consumed the ellipsis to mark a
15898                        parameter pack, but we have no way to report it,
15899                        so abort the tentative parse. We will be exiting
15900                        immediately anyway. */
15901                     cp_parser_abort_tentative_parse (parser);
15902                   }
15903                 else
15904                   okay = cp_parser_parse_definitely (parser);
15905
15906                 if (!okay)
15907                   unqualified_name = error_mark_node;
15908                 else if (unqualified_name
15909                          && (qualifying_scope
15910                              || (TREE_CODE (unqualified_name)
15911                                  != IDENTIFIER_NODE)))
15912                   {
15913                     cp_parser_error (parser, "expected unqualified-id");
15914                     unqualified_name = error_mark_node;
15915                   }
15916               }
15917
15918             if (!unqualified_name)
15919               return NULL;
15920             if (unqualified_name == error_mark_node)
15921               {
15922                 declarator = cp_error_declarator;
15923                 pack_expansion_p = false;
15924                 declarator->parameter_pack_p = false;
15925                 break;
15926               }
15927
15928             if (qualifying_scope && at_namespace_scope_p ()
15929                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15930               {
15931                 /* In the declaration of a member of a template class
15932                    outside of the class itself, the SCOPE will sometimes
15933                    be a TYPENAME_TYPE.  For example, given:
15934
15935                    template <typename T>
15936                    int S<T>::R::i = 3;
15937
15938                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15939                    this context, we must resolve S<T>::R to an ordinary
15940                    type, rather than a typename type.
15941
15942                    The reason we normally avoid resolving TYPENAME_TYPEs
15943                    is that a specialization of `S' might render
15944                    `S<T>::R' not a type.  However, if `S' is
15945                    specialized, then this `i' will not be used, so there
15946                    is no harm in resolving the types here.  */
15947                 tree type;
15948
15949                 /* Resolve the TYPENAME_TYPE.  */
15950                 type = resolve_typename_type (qualifying_scope,
15951                                               /*only_current_p=*/false);
15952                 /* If that failed, the declarator is invalid.  */
15953                 if (TREE_CODE (type) == TYPENAME_TYPE)
15954                   {
15955                     if (typedef_variant_p (type))
15956                       error_at (declarator_id_start_token->location,
15957                                 "cannot define member of dependent typedef "
15958                                 "%qT", type);
15959                     else
15960                       error_at (declarator_id_start_token->location,
15961                                 "%<%T::%E%> is not a type",
15962                                 TYPE_CONTEXT (qualifying_scope),
15963                                 TYPE_IDENTIFIER (qualifying_scope));
15964                   }
15965                 qualifying_scope = type;
15966               }
15967
15968             sfk = sfk_none;
15969
15970             if (unqualified_name)
15971               {
15972                 tree class_type;
15973
15974                 if (qualifying_scope
15975                     && CLASS_TYPE_P (qualifying_scope))
15976                   class_type = qualifying_scope;
15977                 else
15978                   class_type = current_class_type;
15979
15980                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15981                   {
15982                     tree name_type = TREE_TYPE (unqualified_name);
15983                     if (class_type && same_type_p (name_type, class_type))
15984                       {
15985                         if (qualifying_scope
15986                             && CLASSTYPE_USE_TEMPLATE (name_type))
15987                           {
15988                             error_at (declarator_id_start_token->location,
15989                                       "invalid use of constructor as a template");
15990                             inform (declarator_id_start_token->location,
15991                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15992                                     "name the constructor in a qualified name",
15993                                     class_type,
15994                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15995                                     class_type, name_type);
15996                             declarator = cp_error_declarator;
15997                             break;
15998                           }
15999                         else
16000                           unqualified_name = constructor_name (class_type);
16001                       }
16002                     else
16003                       {
16004                         /* We do not attempt to print the declarator
16005                            here because we do not have enough
16006                            information about its original syntactic
16007                            form.  */
16008                         cp_parser_error (parser, "invalid declarator");
16009                         declarator = cp_error_declarator;
16010                         break;
16011                       }
16012                   }
16013
16014                 if (class_type)
16015                   {
16016                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16017                       sfk = sfk_destructor;
16018                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16019                       sfk = sfk_conversion;
16020                     else if (/* There's no way to declare a constructor
16021                                 for an anonymous type, even if the type
16022                                 got a name for linkage purposes.  */
16023                              !TYPE_WAS_ANONYMOUS (class_type)
16024                              && constructor_name_p (unqualified_name,
16025                                                     class_type))
16026                       {
16027                         unqualified_name = constructor_name (class_type);
16028                         sfk = sfk_constructor;
16029                       }
16030                     else if (is_overloaded_fn (unqualified_name)
16031                              && DECL_CONSTRUCTOR_P (get_first_fn
16032                                                     (unqualified_name)))
16033                       sfk = sfk_constructor;
16034
16035                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16036                       *ctor_dtor_or_conv_p = -1;
16037                   }
16038               }
16039             declarator = make_id_declarator (qualifying_scope,
16040                                              unqualified_name,
16041                                              sfk);
16042             declarator->id_loc = token->location;
16043             declarator->parameter_pack_p = pack_expansion_p;
16044
16045             if (pack_expansion_p)
16046               maybe_warn_variadic_templates ();
16047           }
16048
16049         handle_declarator:;
16050           scope = get_scope_of_declarator (declarator);
16051           if (scope)
16052             /* Any names that appear after the declarator-id for a
16053                member are looked up in the containing scope.  */
16054             pushed_scope = push_scope (scope);
16055           parser->in_declarator_p = true;
16056           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16057               || (declarator && declarator->kind == cdk_id))
16058             /* Default args are only allowed on function
16059                declarations.  */
16060             parser->default_arg_ok_p = saved_default_arg_ok_p;
16061           else
16062             parser->default_arg_ok_p = false;
16063
16064           first = false;
16065         }
16066       /* We're done.  */
16067       else
16068         break;
16069     }
16070
16071   /* For an abstract declarator, we might wind up with nothing at this
16072      point.  That's an error; the declarator is not optional.  */
16073   if (!declarator)
16074     cp_parser_error (parser, "expected declarator");
16075
16076   /* If we entered a scope, we must exit it now.  */
16077   if (pushed_scope)
16078     pop_scope (pushed_scope);
16079
16080   parser->default_arg_ok_p = saved_default_arg_ok_p;
16081   parser->in_declarator_p = saved_in_declarator_p;
16082
16083   return declarator;
16084 }
16085
16086 /* Parse a ptr-operator.
16087
16088    ptr-operator:
16089      * cv-qualifier-seq [opt]
16090      &
16091      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16092
16093    GNU Extension:
16094
16095    ptr-operator:
16096      & cv-qualifier-seq [opt]
16097
16098    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16099    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16100    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16101    filled in with the TYPE containing the member.  *CV_QUALS is
16102    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16103    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16104    Note that the tree codes returned by this function have nothing
16105    to do with the types of trees that will be eventually be created
16106    to represent the pointer or reference type being parsed. They are
16107    just constants with suggestive names. */
16108 static enum tree_code
16109 cp_parser_ptr_operator (cp_parser* parser,
16110                         tree* type,
16111                         cp_cv_quals *cv_quals)
16112 {
16113   enum tree_code code = ERROR_MARK;
16114   cp_token *token;
16115
16116   /* Assume that it's not a pointer-to-member.  */
16117   *type = NULL_TREE;
16118   /* And that there are no cv-qualifiers.  */
16119   *cv_quals = TYPE_UNQUALIFIED;
16120
16121   /* Peek at the next token.  */
16122   token = cp_lexer_peek_token (parser->lexer);
16123
16124   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16125   if (token->type == CPP_MULT)
16126     code = INDIRECT_REF;
16127   else if (token->type == CPP_AND)
16128     code = ADDR_EXPR;
16129   else if ((cxx_dialect != cxx98) &&
16130            token->type == CPP_AND_AND) /* C++0x only */
16131     code = NON_LVALUE_EXPR;
16132
16133   if (code != ERROR_MARK)
16134     {
16135       /* Consume the `*', `&' or `&&'.  */
16136       cp_lexer_consume_token (parser->lexer);
16137
16138       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16139          `&', if we are allowing GNU extensions.  (The only qualifier
16140          that can legally appear after `&' is `restrict', but that is
16141          enforced during semantic analysis.  */
16142       if (code == INDIRECT_REF
16143           || cp_parser_allow_gnu_extensions_p (parser))
16144         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16145     }
16146   else
16147     {
16148       /* Try the pointer-to-member case.  */
16149       cp_parser_parse_tentatively (parser);
16150       /* Look for the optional `::' operator.  */
16151       cp_parser_global_scope_opt (parser,
16152                                   /*current_scope_valid_p=*/false);
16153       /* Look for the nested-name specifier.  */
16154       token = cp_lexer_peek_token (parser->lexer);
16155       cp_parser_nested_name_specifier (parser,
16156                                        /*typename_keyword_p=*/false,
16157                                        /*check_dependency_p=*/true,
16158                                        /*type_p=*/false,
16159                                        /*is_declaration=*/false);
16160       /* If we found it, and the next token is a `*', then we are
16161          indeed looking at a pointer-to-member operator.  */
16162       if (!cp_parser_error_occurred (parser)
16163           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16164         {
16165           /* Indicate that the `*' operator was used.  */
16166           code = INDIRECT_REF;
16167
16168           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16169             error_at (token->location, "%qD is a namespace", parser->scope);
16170           else
16171             {
16172               /* The type of which the member is a member is given by the
16173                  current SCOPE.  */
16174               *type = parser->scope;
16175               /* The next name will not be qualified.  */
16176               parser->scope = NULL_TREE;
16177               parser->qualifying_scope = NULL_TREE;
16178               parser->object_scope = NULL_TREE;
16179               /* Look for the optional cv-qualifier-seq.  */
16180               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16181             }
16182         }
16183       /* If that didn't work we don't have a ptr-operator.  */
16184       if (!cp_parser_parse_definitely (parser))
16185         cp_parser_error (parser, "expected ptr-operator");
16186     }
16187
16188   return code;
16189 }
16190
16191 /* Parse an (optional) cv-qualifier-seq.
16192
16193    cv-qualifier-seq:
16194      cv-qualifier cv-qualifier-seq [opt]
16195
16196    cv-qualifier:
16197      const
16198      volatile
16199
16200    GNU Extension:
16201
16202    cv-qualifier:
16203      __restrict__
16204
16205    Returns a bitmask representing the cv-qualifiers.  */
16206
16207 static cp_cv_quals
16208 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16209 {
16210   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16211
16212   while (true)
16213     {
16214       cp_token *token;
16215       cp_cv_quals cv_qualifier;
16216
16217       /* Peek at the next token.  */
16218       token = cp_lexer_peek_token (parser->lexer);
16219       /* See if it's a cv-qualifier.  */
16220       switch (token->keyword)
16221         {
16222         case RID_CONST:
16223           cv_qualifier = TYPE_QUAL_CONST;
16224           break;
16225
16226         case RID_VOLATILE:
16227           cv_qualifier = TYPE_QUAL_VOLATILE;
16228           break;
16229
16230         case RID_RESTRICT:
16231           cv_qualifier = TYPE_QUAL_RESTRICT;
16232           break;
16233
16234         default:
16235           cv_qualifier = TYPE_UNQUALIFIED;
16236           break;
16237         }
16238
16239       if (!cv_qualifier)
16240         break;
16241
16242       if (cv_quals & cv_qualifier)
16243         {
16244           error_at (token->location, "duplicate cv-qualifier");
16245           cp_lexer_purge_token (parser->lexer);
16246         }
16247       else
16248         {
16249           cp_lexer_consume_token (parser->lexer);
16250           cv_quals |= cv_qualifier;
16251         }
16252     }
16253
16254   return cv_quals;
16255 }
16256
16257 /* Parse an (optional) virt-specifier-seq.
16258
16259    virt-specifier-seq:
16260      virt-specifier virt-specifier-seq [opt]
16261
16262    virt-specifier:
16263      override
16264      final
16265
16266    Returns a bitmask representing the virt-specifiers.  */
16267
16268 static cp_virt_specifiers
16269 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16270 {
16271   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16272
16273   while (true)
16274     {
16275       cp_token *token;
16276       cp_virt_specifiers virt_specifier;
16277
16278       /* Peek at the next token.  */
16279       token = cp_lexer_peek_token (parser->lexer);
16280       /* See if it's a virt-specifier-qualifier.  */
16281       if (token->type != CPP_NAME)
16282         break;
16283       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16284         {
16285           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16286           virt_specifier = VIRT_SPEC_OVERRIDE;
16287         }
16288       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16289         {
16290           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16291           virt_specifier = VIRT_SPEC_FINAL;
16292         }
16293       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16294         {
16295           virt_specifier = VIRT_SPEC_FINAL;
16296         }
16297       else
16298         break;
16299
16300       if (virt_specifiers & virt_specifier)
16301         {
16302           error_at (token->location, "duplicate virt-specifier");
16303           cp_lexer_purge_token (parser->lexer);
16304         }
16305       else
16306         {
16307           cp_lexer_consume_token (parser->lexer);
16308           virt_specifiers |= virt_specifier;
16309         }
16310     }
16311   return virt_specifiers;
16312 }
16313
16314 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16315    is in scope even though it isn't real.  */
16316
16317 static void
16318 inject_this_parameter (tree ctype, cp_cv_quals quals)
16319 {
16320   tree this_parm;
16321
16322   if (current_class_ptr)
16323     {
16324       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16325       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16326       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16327           && cp_type_quals (type) == quals)
16328         return;
16329     }
16330
16331   this_parm = build_this_parm (ctype, quals);
16332   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16333   current_class_ptr = NULL_TREE;
16334   current_class_ref
16335     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16336   current_class_ptr = this_parm;
16337 }
16338
16339 /* Parse a late-specified return type, if any.  This is not a separate
16340    non-terminal, but part of a function declarator, which looks like
16341
16342    -> trailing-type-specifier-seq abstract-declarator(opt)
16343
16344    Returns the type indicated by the type-id.
16345
16346    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16347    function.  */
16348
16349 static tree
16350 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16351 {
16352   cp_token *token;
16353   tree type;
16354
16355   /* Peek at the next token.  */
16356   token = cp_lexer_peek_token (parser->lexer);
16357   /* A late-specified return type is indicated by an initial '->'. */
16358   if (token->type != CPP_DEREF)
16359     return NULL_TREE;
16360
16361   /* Consume the ->.  */
16362   cp_lexer_consume_token (parser->lexer);
16363
16364   if (quals >= 0)
16365     {
16366       /* DR 1207: 'this' is in scope in the trailing return type.  */
16367       gcc_assert (current_class_ptr == NULL_TREE);
16368       inject_this_parameter (current_class_type, quals);
16369     }
16370
16371   type = cp_parser_trailing_type_id (parser);
16372
16373   if (quals >= 0)
16374     current_class_ptr = current_class_ref = NULL_TREE;
16375
16376   return type;
16377 }
16378
16379 /* Parse a declarator-id.
16380
16381    declarator-id:
16382      id-expression
16383      :: [opt] nested-name-specifier [opt] type-name
16384
16385    In the `id-expression' case, the value returned is as for
16386    cp_parser_id_expression if the id-expression was an unqualified-id.
16387    If the id-expression was a qualified-id, then a SCOPE_REF is
16388    returned.  The first operand is the scope (either a NAMESPACE_DECL
16389    or TREE_TYPE), but the second is still just a representation of an
16390    unqualified-id.  */
16391
16392 static tree
16393 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16394 {
16395   tree id;
16396   /* The expression must be an id-expression.  Assume that qualified
16397      names are the names of types so that:
16398
16399        template <class T>
16400        int S<T>::R::i = 3;
16401
16402      will work; we must treat `S<T>::R' as the name of a type.
16403      Similarly, assume that qualified names are templates, where
16404      required, so that:
16405
16406        template <class T>
16407        int S<T>::R<T>::i = 3;
16408
16409      will work, too.  */
16410   id = cp_parser_id_expression (parser,
16411                                 /*template_keyword_p=*/false,
16412                                 /*check_dependency_p=*/false,
16413                                 /*template_p=*/NULL,
16414                                 /*declarator_p=*/true,
16415                                 optional_p);
16416   if (id && BASELINK_P (id))
16417     id = BASELINK_FUNCTIONS (id);
16418   return id;
16419 }
16420
16421 /* Parse a type-id.
16422
16423    type-id:
16424      type-specifier-seq abstract-declarator [opt]
16425
16426    Returns the TYPE specified.  */
16427
16428 static tree
16429 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16430                      bool is_trailing_return)
16431 {
16432   cp_decl_specifier_seq type_specifier_seq;
16433   cp_declarator *abstract_declarator;
16434
16435   /* Parse the type-specifier-seq.  */
16436   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16437                                 is_trailing_return,
16438                                 &type_specifier_seq);
16439   if (type_specifier_seq.type == error_mark_node)
16440     return error_mark_node;
16441
16442   /* There might or might not be an abstract declarator.  */
16443   cp_parser_parse_tentatively (parser);
16444   /* Look for the declarator.  */
16445   abstract_declarator
16446     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16447                             /*parenthesized_p=*/NULL,
16448                             /*member_p=*/false);
16449   /* Check to see if there really was a declarator.  */
16450   if (!cp_parser_parse_definitely (parser))
16451     abstract_declarator = NULL;
16452
16453   if (type_specifier_seq.type
16454       && type_uses_auto (type_specifier_seq.type))
16455     {
16456       /* A type-id with type 'auto' is only ok if the abstract declarator
16457          is a function declarator with a late-specified return type.  */
16458       if (abstract_declarator
16459           && abstract_declarator->kind == cdk_function
16460           && abstract_declarator->u.function.late_return_type)
16461         /* OK */;
16462       else
16463         {
16464           error ("invalid use of %<auto%>");
16465           return error_mark_node;
16466         }
16467     }
16468   
16469   return groktypename (&type_specifier_seq, abstract_declarator,
16470                        is_template_arg);
16471 }
16472
16473 static tree cp_parser_type_id (cp_parser *parser)
16474 {
16475   return cp_parser_type_id_1 (parser, false, false);
16476 }
16477
16478 static tree cp_parser_template_type_arg (cp_parser *parser)
16479 {
16480   tree r;
16481   const char *saved_message = parser->type_definition_forbidden_message;
16482   parser->type_definition_forbidden_message
16483     = G_("types may not be defined in template arguments");
16484   r = cp_parser_type_id_1 (parser, true, false);
16485   parser->type_definition_forbidden_message = saved_message;
16486   return r;
16487 }
16488
16489 static tree cp_parser_trailing_type_id (cp_parser *parser)
16490 {
16491   return cp_parser_type_id_1 (parser, false, true);
16492 }
16493
16494 /* Parse a type-specifier-seq.
16495
16496    type-specifier-seq:
16497      type-specifier type-specifier-seq [opt]
16498
16499    GNU extension:
16500
16501    type-specifier-seq:
16502      attributes type-specifier-seq [opt]
16503
16504    If IS_DECLARATION is true, we are at the start of a "condition" or
16505    exception-declaration, so we might be followed by a declarator-id.
16506
16507    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16508    i.e. we've just seen "->".
16509
16510    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16511
16512 static void
16513 cp_parser_type_specifier_seq (cp_parser* parser,
16514                               bool is_declaration,
16515                               bool is_trailing_return,
16516                               cp_decl_specifier_seq *type_specifier_seq)
16517 {
16518   bool seen_type_specifier = false;
16519   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16520   cp_token *start_token = NULL;
16521
16522   /* Clear the TYPE_SPECIFIER_SEQ.  */
16523   clear_decl_specs (type_specifier_seq);
16524
16525   /* In the context of a trailing return type, enum E { } is an
16526      elaborated-type-specifier followed by a function-body, not an
16527      enum-specifier.  */
16528   if (is_trailing_return)
16529     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16530
16531   /* Parse the type-specifiers and attributes.  */
16532   while (true)
16533     {
16534       tree type_specifier;
16535       bool is_cv_qualifier;
16536
16537       /* Check for attributes first.  */
16538       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16539         {
16540           type_specifier_seq->attributes =
16541             chainon (type_specifier_seq->attributes,
16542                      cp_parser_attributes_opt (parser));
16543           continue;
16544         }
16545
16546       /* record the token of the beginning of the type specifier seq,
16547          for error reporting purposes*/
16548      if (!start_token)
16549        start_token = cp_lexer_peek_token (parser->lexer);
16550
16551       /* Look for the type-specifier.  */
16552       type_specifier = cp_parser_type_specifier (parser,
16553                                                  flags,
16554                                                  type_specifier_seq,
16555                                                  /*is_declaration=*/false,
16556                                                  NULL,
16557                                                  &is_cv_qualifier);
16558       if (!type_specifier)
16559         {
16560           /* If the first type-specifier could not be found, this is not a
16561              type-specifier-seq at all.  */
16562           if (!seen_type_specifier)
16563             {
16564               cp_parser_error (parser, "expected type-specifier");
16565               type_specifier_seq->type = error_mark_node;
16566               return;
16567             }
16568           /* If subsequent type-specifiers could not be found, the
16569              type-specifier-seq is complete.  */
16570           break;
16571         }
16572
16573       seen_type_specifier = true;
16574       /* The standard says that a condition can be:
16575
16576             type-specifier-seq declarator = assignment-expression
16577
16578          However, given:
16579
16580            struct S {};
16581            if (int S = ...)
16582
16583          we should treat the "S" as a declarator, not as a
16584          type-specifier.  The standard doesn't say that explicitly for
16585          type-specifier-seq, but it does say that for
16586          decl-specifier-seq in an ordinary declaration.  Perhaps it
16587          would be clearer just to allow a decl-specifier-seq here, and
16588          then add a semantic restriction that if any decl-specifiers
16589          that are not type-specifiers appear, the program is invalid.  */
16590       if (is_declaration && !is_cv_qualifier)
16591         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16592     }
16593
16594   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16595 }
16596
16597 /* Parse a parameter-declaration-clause.
16598
16599    parameter-declaration-clause:
16600      parameter-declaration-list [opt] ... [opt]
16601      parameter-declaration-list , ...
16602
16603    Returns a representation for the parameter declarations.  A return
16604    value of NULL indicates a parameter-declaration-clause consisting
16605    only of an ellipsis.  */
16606
16607 static tree
16608 cp_parser_parameter_declaration_clause (cp_parser* parser)
16609 {
16610   tree parameters;
16611   cp_token *token;
16612   bool ellipsis_p;
16613   bool is_error;
16614
16615   /* Peek at the next token.  */
16616   token = cp_lexer_peek_token (parser->lexer);
16617   /* Check for trivial parameter-declaration-clauses.  */
16618   if (token->type == CPP_ELLIPSIS)
16619     {
16620       /* Consume the `...' token.  */
16621       cp_lexer_consume_token (parser->lexer);
16622       return NULL_TREE;
16623     }
16624   else if (token->type == CPP_CLOSE_PAREN)
16625     /* There are no parameters.  */
16626     {
16627 #ifndef NO_IMPLICIT_EXTERN_C
16628       if (in_system_header && current_class_type == NULL
16629           && current_lang_name == lang_name_c)
16630         return NULL_TREE;
16631       else
16632 #endif
16633         return void_list_node;
16634     }
16635   /* Check for `(void)', too, which is a special case.  */
16636   else if (token->keyword == RID_VOID
16637            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16638                == CPP_CLOSE_PAREN))
16639     {
16640       /* Consume the `void' token.  */
16641       cp_lexer_consume_token (parser->lexer);
16642       /* There are no parameters.  */
16643       return void_list_node;
16644     }
16645
16646   /* Parse the parameter-declaration-list.  */
16647   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16648   /* If a parse error occurred while parsing the
16649      parameter-declaration-list, then the entire
16650      parameter-declaration-clause is erroneous.  */
16651   if (is_error)
16652     return NULL;
16653
16654   /* Peek at the next token.  */
16655   token = cp_lexer_peek_token (parser->lexer);
16656   /* If it's a `,', the clause should terminate with an ellipsis.  */
16657   if (token->type == CPP_COMMA)
16658     {
16659       /* Consume the `,'.  */
16660       cp_lexer_consume_token (parser->lexer);
16661       /* Expect an ellipsis.  */
16662       ellipsis_p
16663         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16664     }
16665   /* It might also be `...' if the optional trailing `,' was
16666      omitted.  */
16667   else if (token->type == CPP_ELLIPSIS)
16668     {
16669       /* Consume the `...' token.  */
16670       cp_lexer_consume_token (parser->lexer);
16671       /* And remember that we saw it.  */
16672       ellipsis_p = true;
16673     }
16674   else
16675     ellipsis_p = false;
16676
16677   /* Finish the parameter list.  */
16678   if (!ellipsis_p)
16679     parameters = chainon (parameters, void_list_node);
16680
16681   return parameters;
16682 }
16683
16684 /* Parse a parameter-declaration-list.
16685
16686    parameter-declaration-list:
16687      parameter-declaration
16688      parameter-declaration-list , parameter-declaration
16689
16690    Returns a representation of the parameter-declaration-list, as for
16691    cp_parser_parameter_declaration_clause.  However, the
16692    `void_list_node' is never appended to the list.  Upon return,
16693    *IS_ERROR will be true iff an error occurred.  */
16694
16695 static tree
16696 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16697 {
16698   tree parameters = NULL_TREE;
16699   tree *tail = &parameters; 
16700   bool saved_in_unbraced_linkage_specification_p;
16701   int index = 0;
16702
16703   /* Assume all will go well.  */
16704   *is_error = false;
16705   /* The special considerations that apply to a function within an
16706      unbraced linkage specifications do not apply to the parameters
16707      to the function.  */
16708   saved_in_unbraced_linkage_specification_p 
16709     = parser->in_unbraced_linkage_specification_p;
16710   parser->in_unbraced_linkage_specification_p = false;
16711
16712   /* Look for more parameters.  */
16713   while (true)
16714     {
16715       cp_parameter_declarator *parameter;
16716       tree decl = error_mark_node;
16717       bool parenthesized_p = false;
16718       /* Parse the parameter.  */
16719       parameter
16720         = cp_parser_parameter_declaration (parser,
16721                                            /*template_parm_p=*/false,
16722                                            &parenthesized_p);
16723
16724       /* We don't know yet if the enclosing context is deprecated, so wait
16725          and warn in grokparms if appropriate.  */
16726       deprecated_state = DEPRECATED_SUPPRESS;
16727
16728       if (parameter)
16729         decl = grokdeclarator (parameter->declarator,
16730                                &parameter->decl_specifiers,
16731                                PARM,
16732                                parameter->default_argument != NULL_TREE,
16733                                &parameter->decl_specifiers.attributes);
16734
16735       deprecated_state = DEPRECATED_NORMAL;
16736
16737       /* If a parse error occurred parsing the parameter declaration,
16738          then the entire parameter-declaration-list is erroneous.  */
16739       if (decl == error_mark_node)
16740         {
16741           *is_error = true;
16742           parameters = error_mark_node;
16743           break;
16744         }
16745
16746       if (parameter->decl_specifiers.attributes)
16747         cplus_decl_attributes (&decl,
16748                                parameter->decl_specifiers.attributes,
16749                                0);
16750       if (DECL_NAME (decl))
16751         decl = pushdecl (decl);
16752
16753       if (decl != error_mark_node)
16754         {
16755           retrofit_lang_decl (decl);
16756           DECL_PARM_INDEX (decl) = ++index;
16757           DECL_PARM_LEVEL (decl) = function_parm_depth ();
16758         }
16759
16760       /* Add the new parameter to the list.  */
16761       *tail = build_tree_list (parameter->default_argument, decl);
16762       tail = &TREE_CHAIN (*tail);
16763
16764       /* Peek at the next token.  */
16765       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16766           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16767           /* These are for Objective-C++ */
16768           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16769           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16770         /* The parameter-declaration-list is complete.  */
16771         break;
16772       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16773         {
16774           cp_token *token;
16775
16776           /* Peek at the next token.  */
16777           token = cp_lexer_peek_nth_token (parser->lexer, 2);
16778           /* If it's an ellipsis, then the list is complete.  */
16779           if (token->type == CPP_ELLIPSIS)
16780             break;
16781           /* Otherwise, there must be more parameters.  Consume the
16782              `,'.  */
16783           cp_lexer_consume_token (parser->lexer);
16784           /* When parsing something like:
16785
16786                 int i(float f, double d)
16787
16788              we can tell after seeing the declaration for "f" that we
16789              are not looking at an initialization of a variable "i",
16790              but rather at the declaration of a function "i".
16791
16792              Due to the fact that the parsing of template arguments
16793              (as specified to a template-id) requires backtracking we
16794              cannot use this technique when inside a template argument
16795              list.  */
16796           if (!parser->in_template_argument_list_p
16797               && !parser->in_type_id_in_expr_p
16798               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16799               /* However, a parameter-declaration of the form
16800                  "foat(f)" (which is a valid declaration of a
16801                  parameter "f") can also be interpreted as an
16802                  expression (the conversion of "f" to "float").  */
16803               && !parenthesized_p)
16804             cp_parser_commit_to_tentative_parse (parser);
16805         }
16806       else
16807         {
16808           cp_parser_error (parser, "expected %<,%> or %<...%>");
16809           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16810             cp_parser_skip_to_closing_parenthesis (parser,
16811                                                    /*recovering=*/true,
16812                                                    /*or_comma=*/false,
16813                                                    /*consume_paren=*/false);
16814           break;
16815         }
16816     }
16817
16818   parser->in_unbraced_linkage_specification_p
16819     = saved_in_unbraced_linkage_specification_p;
16820
16821   return parameters;
16822 }
16823
16824 /* Parse a parameter declaration.
16825
16826    parameter-declaration:
16827      decl-specifier-seq ... [opt] declarator
16828      decl-specifier-seq declarator = assignment-expression
16829      decl-specifier-seq ... [opt] abstract-declarator [opt]
16830      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16831
16832    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16833    declares a template parameter.  (In that case, a non-nested `>'
16834    token encountered during the parsing of the assignment-expression
16835    is not interpreted as a greater-than operator.)
16836
16837    Returns a representation of the parameter, or NULL if an error
16838    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16839    true iff the declarator is of the form "(p)".  */
16840
16841 static cp_parameter_declarator *
16842 cp_parser_parameter_declaration (cp_parser *parser,
16843                                  bool template_parm_p,
16844                                  bool *parenthesized_p)
16845 {
16846   int declares_class_or_enum;
16847   cp_decl_specifier_seq decl_specifiers;
16848   cp_declarator *declarator;
16849   tree default_argument;
16850   cp_token *token = NULL, *declarator_token_start = NULL;
16851   const char *saved_message;
16852
16853   /* In a template parameter, `>' is not an operator.
16854
16855      [temp.param]
16856
16857      When parsing a default template-argument for a non-type
16858      template-parameter, the first non-nested `>' is taken as the end
16859      of the template parameter-list rather than a greater-than
16860      operator.  */
16861
16862   /* Type definitions may not appear in parameter types.  */
16863   saved_message = parser->type_definition_forbidden_message;
16864   parser->type_definition_forbidden_message
16865     = G_("types may not be defined in parameter types");
16866
16867   /* Parse the declaration-specifiers.  */
16868   cp_parser_decl_specifier_seq (parser,
16869                                 CP_PARSER_FLAGS_NONE,
16870                                 &decl_specifiers,
16871                                 &declares_class_or_enum);
16872
16873   /* Complain about missing 'typename' or other invalid type names.  */
16874   if (!decl_specifiers.any_type_specifiers_p)
16875     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16876
16877   /* If an error occurred, there's no reason to attempt to parse the
16878      rest of the declaration.  */
16879   if (cp_parser_error_occurred (parser))
16880     {
16881       parser->type_definition_forbidden_message = saved_message;
16882       return NULL;
16883     }
16884
16885   /* Peek at the next token.  */
16886   token = cp_lexer_peek_token (parser->lexer);
16887
16888   /* If the next token is a `)', `,', `=', `>', or `...', then there
16889      is no declarator. However, when variadic templates are enabled,
16890      there may be a declarator following `...'.  */
16891   if (token->type == CPP_CLOSE_PAREN
16892       || token->type == CPP_COMMA
16893       || token->type == CPP_EQ
16894       || token->type == CPP_GREATER)
16895     {
16896       declarator = NULL;
16897       if (parenthesized_p)
16898         *parenthesized_p = false;
16899     }
16900   /* Otherwise, there should be a declarator.  */
16901   else
16902     {
16903       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16904       parser->default_arg_ok_p = false;
16905
16906       /* After seeing a decl-specifier-seq, if the next token is not a
16907          "(", there is no possibility that the code is a valid
16908          expression.  Therefore, if parsing tentatively, we commit at
16909          this point.  */
16910       if (!parser->in_template_argument_list_p
16911           /* In an expression context, having seen:
16912
16913                (int((char ...
16914
16915              we cannot be sure whether we are looking at a
16916              function-type (taking a "char" as a parameter) or a cast
16917              of some object of type "char" to "int".  */
16918           && !parser->in_type_id_in_expr_p
16919           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16920           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16921           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16922         cp_parser_commit_to_tentative_parse (parser);
16923       /* Parse the declarator.  */
16924       declarator_token_start = token;
16925       declarator = cp_parser_declarator (parser,
16926                                          CP_PARSER_DECLARATOR_EITHER,
16927                                          /*ctor_dtor_or_conv_p=*/NULL,
16928                                          parenthesized_p,
16929                                          /*member_p=*/false);
16930       parser->default_arg_ok_p = saved_default_arg_ok_p;
16931       /* After the declarator, allow more attributes.  */
16932       decl_specifiers.attributes
16933         = chainon (decl_specifiers.attributes,
16934                    cp_parser_attributes_opt (parser));
16935     }
16936
16937   /* If the next token is an ellipsis, and we have not seen a
16938      declarator name, and the type of the declarator contains parameter
16939      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16940      a parameter pack expansion expression. Otherwise, leave the
16941      ellipsis for a C-style variadic function. */
16942   token = cp_lexer_peek_token (parser->lexer);
16943   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16944     {
16945       tree type = decl_specifiers.type;
16946
16947       if (type && DECL_P (type))
16948         type = TREE_TYPE (type);
16949
16950       if (type
16951           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16952           && declarator_can_be_parameter_pack (declarator)
16953           && (!declarator || !declarator->parameter_pack_p)
16954           && uses_parameter_packs (type))
16955         {
16956           /* Consume the `...'. */
16957           cp_lexer_consume_token (parser->lexer);
16958           maybe_warn_variadic_templates ();
16959           
16960           /* Build a pack expansion type */
16961           if (declarator)
16962             declarator->parameter_pack_p = true;
16963           else
16964             decl_specifiers.type = make_pack_expansion (type);
16965         }
16966     }
16967
16968   /* The restriction on defining new types applies only to the type
16969      of the parameter, not to the default argument.  */
16970   parser->type_definition_forbidden_message = saved_message;
16971
16972   /* If the next token is `=', then process a default argument.  */
16973   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16974     {
16975       /* If we are defining a class, then the tokens that make up the
16976          default argument must be saved and processed later.  */
16977       if (!template_parm_p && at_class_scope_p ()
16978           && TYPE_BEING_DEFINED (current_class_type)
16979           && !LAMBDA_TYPE_P (current_class_type))
16980         {
16981           unsigned depth = 0;
16982           int maybe_template_id = 0;
16983           cp_token *first_token;
16984           cp_token *token;
16985
16986           /* Add tokens until we have processed the entire default
16987              argument.  We add the range [first_token, token).  */
16988           first_token = cp_lexer_peek_token (parser->lexer);
16989           while (true)
16990             {
16991               bool done = false;
16992
16993               /* Peek at the next token.  */
16994               token = cp_lexer_peek_token (parser->lexer);
16995               /* What we do depends on what token we have.  */
16996               switch (token->type)
16997                 {
16998                   /* In valid code, a default argument must be
16999                      immediately followed by a `,' `)', or `...'.  */
17000                 case CPP_COMMA:
17001                   if (depth == 0 && maybe_template_id)
17002                     {
17003                       /* If we've seen a '<', we might be in a
17004                          template-argument-list.  Until Core issue 325 is
17005                          resolved, we don't know how this situation ought
17006                          to be handled, so try to DTRT.  We check whether
17007                          what comes after the comma is a valid parameter
17008                          declaration list.  If it is, then the comma ends
17009                          the default argument; otherwise the default
17010                          argument continues.  */
17011                       bool error = false;
17012                       tree t;
17013
17014                       /* Set ITALP so cp_parser_parameter_declaration_list
17015                          doesn't decide to commit to this parse.  */
17016                       bool saved_italp = parser->in_template_argument_list_p;
17017                       parser->in_template_argument_list_p = true;
17018
17019                       cp_parser_parse_tentatively (parser);
17020                       cp_lexer_consume_token (parser->lexer);
17021                       begin_scope (sk_function_parms, NULL_TREE);
17022                       cp_parser_parameter_declaration_list (parser, &error);
17023                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17024                         pop_binding (DECL_NAME (t), t);
17025                       leave_scope ();
17026                       if (!cp_parser_error_occurred (parser) && !error)
17027                         done = true;
17028                       cp_parser_abort_tentative_parse (parser);
17029
17030                       parser->in_template_argument_list_p = saved_italp;
17031                       break;
17032                     }
17033                 case CPP_CLOSE_PAREN:
17034                 case CPP_ELLIPSIS:
17035                   /* If we run into a non-nested `;', `}', or `]',
17036                      then the code is invalid -- but the default
17037                      argument is certainly over.  */
17038                 case CPP_SEMICOLON:
17039                 case CPP_CLOSE_BRACE:
17040                 case CPP_CLOSE_SQUARE:
17041                   if (depth == 0)
17042                     done = true;
17043                   /* Update DEPTH, if necessary.  */
17044                   else if (token->type == CPP_CLOSE_PAREN
17045                            || token->type == CPP_CLOSE_BRACE
17046                            || token->type == CPP_CLOSE_SQUARE)
17047                     --depth;
17048                   break;
17049
17050                 case CPP_OPEN_PAREN:
17051                 case CPP_OPEN_SQUARE:
17052                 case CPP_OPEN_BRACE:
17053                   ++depth;
17054                   break;
17055
17056                 case CPP_LESS:
17057                   if (depth == 0)
17058                     /* This might be the comparison operator, or it might
17059                        start a template argument list.  */
17060                     ++maybe_template_id;
17061                   break;
17062
17063                 case CPP_RSHIFT:
17064                   if (cxx_dialect == cxx98)
17065                     break;
17066                   /* Fall through for C++0x, which treats the `>>'
17067                      operator like two `>' tokens in certain
17068                      cases.  */
17069
17070                 case CPP_GREATER:
17071                   if (depth == 0)
17072                     {
17073                       /* This might be an operator, or it might close a
17074                          template argument list.  But if a previous '<'
17075                          started a template argument list, this will have
17076                          closed it, so we can't be in one anymore.  */
17077                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17078                       if (maybe_template_id < 0)
17079                         maybe_template_id = 0;
17080                     }
17081                   break;
17082
17083                   /* If we run out of tokens, issue an error message.  */
17084                 case CPP_EOF:
17085                 case CPP_PRAGMA_EOL:
17086                   error_at (token->location, "file ends in default argument");
17087                   done = true;
17088                   break;
17089
17090                 case CPP_NAME:
17091                 case CPP_SCOPE:
17092                   /* In these cases, we should look for template-ids.
17093                      For example, if the default argument is
17094                      `X<int, double>()', we need to do name lookup to
17095                      figure out whether or not `X' is a template; if
17096                      so, the `,' does not end the default argument.
17097
17098                      That is not yet done.  */
17099                   break;
17100
17101                 default:
17102                   break;
17103                 }
17104
17105               /* If we've reached the end, stop.  */
17106               if (done)
17107                 break;
17108
17109               /* Add the token to the token block.  */
17110               token = cp_lexer_consume_token (parser->lexer);
17111             }
17112
17113           /* Create a DEFAULT_ARG to represent the unparsed default
17114              argument.  */
17115           default_argument = make_node (DEFAULT_ARG);
17116           DEFARG_TOKENS (default_argument)
17117             = cp_token_cache_new (first_token, token);
17118           DEFARG_INSTANTIATIONS (default_argument) = NULL;
17119         }
17120       /* Outside of a class definition, we can just parse the
17121          assignment-expression.  */
17122       else
17123         {
17124           token = cp_lexer_peek_token (parser->lexer);
17125           default_argument 
17126             = cp_parser_default_argument (parser, template_parm_p);
17127         }
17128
17129       if (!parser->default_arg_ok_p)
17130         {
17131           if (flag_permissive)
17132             warning (0, "deprecated use of default argument for parameter of non-function");
17133           else
17134             {
17135               error_at (token->location,
17136                         "default arguments are only "
17137                         "permitted for function parameters");
17138               default_argument = NULL_TREE;
17139             }
17140         }
17141       else if ((declarator && declarator->parameter_pack_p)
17142                || (decl_specifiers.type
17143                    && PACK_EXPANSION_P (decl_specifiers.type)))
17144         {
17145           /* Find the name of the parameter pack.  */     
17146           cp_declarator *id_declarator = declarator;
17147           while (id_declarator && id_declarator->kind != cdk_id)
17148             id_declarator = id_declarator->declarator;
17149           
17150           if (id_declarator && id_declarator->kind == cdk_id)
17151             error_at (declarator_token_start->location,
17152                       template_parm_p
17153                       ? G_("template parameter pack %qD "
17154                            "cannot have a default argument")
17155                       : G_("parameter pack %qD cannot have "
17156                            "a default argument"),
17157                       id_declarator->u.id.unqualified_name);
17158           else
17159             error_at (declarator_token_start->location,
17160                       template_parm_p
17161                       ? G_("template parameter pack cannot have "
17162                            "a default argument")
17163                       : G_("parameter pack cannot have a "
17164                            "default argument"));
17165
17166           default_argument = NULL_TREE;
17167         }
17168     }
17169   else
17170     default_argument = NULL_TREE;
17171
17172   return make_parameter_declarator (&decl_specifiers,
17173                                     declarator,
17174                                     default_argument);
17175 }
17176
17177 /* Parse a default argument and return it.
17178
17179    TEMPLATE_PARM_P is true if this is a default argument for a
17180    non-type template parameter.  */
17181 static tree
17182 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17183 {
17184   tree default_argument = NULL_TREE;
17185   bool saved_greater_than_is_operator_p;
17186   bool saved_local_variables_forbidden_p;
17187   bool non_constant_p, is_direct_init;
17188
17189   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17190      set correctly.  */
17191   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17192   parser->greater_than_is_operator_p = !template_parm_p;
17193   /* Local variable names (and the `this' keyword) may not
17194      appear in a default argument.  */
17195   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17196   parser->local_variables_forbidden_p = true;
17197   /* Parse the assignment-expression.  */
17198   if (template_parm_p)
17199     push_deferring_access_checks (dk_no_deferred);
17200   default_argument
17201     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17202   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17203     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17204   if (template_parm_p)
17205     pop_deferring_access_checks ();
17206   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17207   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17208
17209   return default_argument;
17210 }
17211
17212 /* Parse a function-body.
17213
17214    function-body:
17215      compound_statement  */
17216
17217 static void
17218 cp_parser_function_body (cp_parser *parser)
17219 {
17220   cp_parser_compound_statement (parser, NULL, false, true);
17221 }
17222
17223 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17224    true if a ctor-initializer was present.  */
17225
17226 static bool
17227 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17228 {
17229   tree body, list;
17230   bool ctor_initializer_p;
17231   const bool check_body_p =
17232      DECL_CONSTRUCTOR_P (current_function_decl)
17233      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17234   tree last = NULL;
17235
17236   /* Begin the function body.  */
17237   body = begin_function_body ();
17238   /* Parse the optional ctor-initializer.  */
17239   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17240
17241   /* If we're parsing a constexpr constructor definition, we need
17242      to check that the constructor body is indeed empty.  However,
17243      before we get to cp_parser_function_body lot of junk has been
17244      generated, so we can't just check that we have an empty block.
17245      Rather we take a snapshot of the outermost block, and check whether
17246      cp_parser_function_body changed its state.  */
17247   if (check_body_p)
17248     {
17249       list = body;
17250       if (TREE_CODE (list) == BIND_EXPR)
17251         list = BIND_EXPR_BODY (list);
17252       if (TREE_CODE (list) == STATEMENT_LIST
17253           && STATEMENT_LIST_TAIL (list) != NULL)
17254         last = STATEMENT_LIST_TAIL (list)->stmt;
17255     }
17256   /* Parse the function-body.  */
17257   cp_parser_function_body (parser);
17258   if (check_body_p)
17259     check_constexpr_ctor_body (last, list);
17260   /* Finish the function body.  */
17261   finish_function_body (body);
17262
17263   return ctor_initializer_p;
17264 }
17265
17266 /* Parse an initializer.
17267
17268    initializer:
17269      = initializer-clause
17270      ( expression-list )
17271
17272    Returns an expression representing the initializer.  If no
17273    initializer is present, NULL_TREE is returned.
17274
17275    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17276    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17277    set to TRUE if there is no initializer present.  If there is an
17278    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17279    is set to true; otherwise it is set to false.  */
17280
17281 static tree
17282 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17283                        bool* non_constant_p)
17284 {
17285   cp_token *token;
17286   tree init;
17287
17288   /* Peek at the next token.  */
17289   token = cp_lexer_peek_token (parser->lexer);
17290
17291   /* Let our caller know whether or not this initializer was
17292      parenthesized.  */
17293   *is_direct_init = (token->type != CPP_EQ);
17294   /* Assume that the initializer is constant.  */
17295   *non_constant_p = false;
17296
17297   if (token->type == CPP_EQ)
17298     {
17299       /* Consume the `='.  */
17300       cp_lexer_consume_token (parser->lexer);
17301       /* Parse the initializer-clause.  */
17302       init = cp_parser_initializer_clause (parser, non_constant_p);
17303     }
17304   else if (token->type == CPP_OPEN_PAREN)
17305     {
17306       VEC(tree,gc) *vec;
17307       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17308                                                      /*cast_p=*/false,
17309                                                      /*allow_expansion_p=*/true,
17310                                                      non_constant_p);
17311       if (vec == NULL)
17312         return error_mark_node;
17313       init = build_tree_list_vec (vec);
17314       release_tree_vector (vec);
17315     }
17316   else if (token->type == CPP_OPEN_BRACE)
17317     {
17318       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17319       init = cp_parser_braced_list (parser, non_constant_p);
17320       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17321     }
17322   else
17323     {
17324       /* Anything else is an error.  */
17325       cp_parser_error (parser, "expected initializer");
17326       init = error_mark_node;
17327     }
17328
17329   return init;
17330 }
17331
17332 /* Parse an initializer-clause.
17333
17334    initializer-clause:
17335      assignment-expression
17336      braced-init-list
17337
17338    Returns an expression representing the initializer.
17339
17340    If the `assignment-expression' production is used the value
17341    returned is simply a representation for the expression.
17342
17343    Otherwise, calls cp_parser_braced_list.  */
17344
17345 static tree
17346 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17347 {
17348   tree initializer;
17349
17350   /* Assume the expression is constant.  */
17351   *non_constant_p = false;
17352
17353   /* If it is not a `{', then we are looking at an
17354      assignment-expression.  */
17355   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17356     {
17357       initializer
17358         = cp_parser_constant_expression (parser,
17359                                         /*allow_non_constant_p=*/true,
17360                                         non_constant_p);
17361     }
17362   else
17363     initializer = cp_parser_braced_list (parser, non_constant_p);
17364
17365   return initializer;
17366 }
17367
17368 /* Parse a brace-enclosed initializer list.
17369
17370    braced-init-list:
17371      { initializer-list , [opt] }
17372      { }
17373
17374    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17375    the elements of the initializer-list (or NULL, if the last
17376    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17377    NULL_TREE.  There is no way to detect whether or not the optional
17378    trailing `,' was provided.  NON_CONSTANT_P is as for
17379    cp_parser_initializer.  */     
17380
17381 static tree
17382 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17383 {
17384   tree initializer;
17385
17386   /* Consume the `{' token.  */
17387   cp_lexer_consume_token (parser->lexer);
17388   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17389   initializer = make_node (CONSTRUCTOR);
17390   /* If it's not a `}', then there is a non-trivial initializer.  */
17391   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17392     {
17393       /* Parse the initializer list.  */
17394       CONSTRUCTOR_ELTS (initializer)
17395         = cp_parser_initializer_list (parser, non_constant_p);
17396       /* A trailing `,' token is allowed.  */
17397       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17398         cp_lexer_consume_token (parser->lexer);
17399     }
17400   /* Now, there should be a trailing `}'.  */
17401   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17402   TREE_TYPE (initializer) = init_list_type_node;
17403   return initializer;
17404 }
17405
17406 /* Parse an initializer-list.
17407
17408    initializer-list:
17409      initializer-clause ... [opt]
17410      initializer-list , initializer-clause ... [opt]
17411
17412    GNU Extension:
17413
17414    initializer-list:
17415      designation initializer-clause ...[opt]
17416      initializer-list , designation initializer-clause ...[opt]
17417
17418    designation:
17419      . identifier =
17420      identifier :
17421      [ constant-expression ] =
17422
17423    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17424    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17425    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17426    as for cp_parser_initializer.  */
17427
17428 static VEC(constructor_elt,gc) *
17429 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17430 {
17431   VEC(constructor_elt,gc) *v = NULL;
17432
17433   /* Assume all of the expressions are constant.  */
17434   *non_constant_p = false;
17435
17436   /* Parse the rest of the list.  */
17437   while (true)
17438     {
17439       cp_token *token;
17440       tree designator;
17441       tree initializer;
17442       bool clause_non_constant_p;
17443
17444       /* If the next token is an identifier and the following one is a
17445          colon, we are looking at the GNU designated-initializer
17446          syntax.  */
17447       if (cp_parser_allow_gnu_extensions_p (parser)
17448           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17449           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17450         {
17451           /* Warn the user that they are using an extension.  */
17452           pedwarn (input_location, OPT_pedantic, 
17453                    "ISO C++ does not allow designated initializers");
17454           /* Consume the identifier.  */
17455           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17456           /* Consume the `:'.  */
17457           cp_lexer_consume_token (parser->lexer);
17458         }
17459       /* Also handle the C99 syntax, '. id ='.  */
17460       else if (cp_parser_allow_gnu_extensions_p (parser)
17461                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17462                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17463                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17464         {
17465           /* Warn the user that they are using an extension.  */
17466           pedwarn (input_location, OPT_pedantic,
17467                    "ISO C++ does not allow C99 designated initializers");
17468           /* Consume the `.'.  */
17469           cp_lexer_consume_token (parser->lexer);
17470           /* Consume the identifier.  */
17471           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17472           /* Consume the `='.  */
17473           cp_lexer_consume_token (parser->lexer);
17474         }
17475       /* Also handle C99 array designators, '[ const ] ='.  */
17476       else if (cp_parser_allow_gnu_extensions_p (parser)
17477                && !c_dialect_objc ()
17478                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17479         {
17480           cp_lexer_consume_token (parser->lexer);
17481           designator = cp_parser_constant_expression (parser, false, NULL);
17482           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17483           cp_parser_require (parser, CPP_EQ, RT_EQ);
17484         }
17485       else
17486         designator = NULL_TREE;
17487
17488       /* Parse the initializer.  */
17489       initializer = cp_parser_initializer_clause (parser,
17490                                                   &clause_non_constant_p);
17491       /* If any clause is non-constant, so is the entire initializer.  */
17492       if (clause_non_constant_p)
17493         *non_constant_p = true;
17494
17495       /* If we have an ellipsis, this is an initializer pack
17496          expansion.  */
17497       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17498         {
17499           /* Consume the `...'.  */
17500           cp_lexer_consume_token (parser->lexer);
17501
17502           /* Turn the initializer into an initializer expansion.  */
17503           initializer = make_pack_expansion (initializer);
17504         }
17505
17506       /* Add it to the vector.  */
17507       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17508
17509       /* If the next token is not a comma, we have reached the end of
17510          the list.  */
17511       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17512         break;
17513
17514       /* Peek at the next token.  */
17515       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17516       /* If the next token is a `}', then we're still done.  An
17517          initializer-clause can have a trailing `,' after the
17518          initializer-list and before the closing `}'.  */
17519       if (token->type == CPP_CLOSE_BRACE)
17520         break;
17521
17522       /* Consume the `,' token.  */
17523       cp_lexer_consume_token (parser->lexer);
17524     }
17525
17526   return v;
17527 }
17528
17529 /* Classes [gram.class] */
17530
17531 /* Parse a class-name.
17532
17533    class-name:
17534      identifier
17535      template-id
17536
17537    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17538    to indicate that names looked up in dependent types should be
17539    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17540    keyword has been used to indicate that the name that appears next
17541    is a template.  TAG_TYPE indicates the explicit tag given before
17542    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17543    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17544    is the class being defined in a class-head.
17545
17546    Returns the TYPE_DECL representing the class.  */
17547
17548 static tree
17549 cp_parser_class_name (cp_parser *parser,
17550                       bool typename_keyword_p,
17551                       bool template_keyword_p,
17552                       enum tag_types tag_type,
17553                       bool check_dependency_p,
17554                       bool class_head_p,
17555                       bool is_declaration)
17556 {
17557   tree decl;
17558   tree scope;
17559   bool typename_p;
17560   cp_token *token;
17561   tree identifier = NULL_TREE;
17562
17563   /* All class-names start with an identifier.  */
17564   token = cp_lexer_peek_token (parser->lexer);
17565   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17566     {
17567       cp_parser_error (parser, "expected class-name");
17568       return error_mark_node;
17569     }
17570
17571   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17572      to a template-id, so we save it here.  */
17573   scope = parser->scope;
17574   if (scope == error_mark_node)
17575     return error_mark_node;
17576
17577   /* Any name names a type if we're following the `typename' keyword
17578      in a qualified name where the enclosing scope is type-dependent.  */
17579   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17580                 && dependent_type_p (scope));
17581   /* Handle the common case (an identifier, but not a template-id)
17582      efficiently.  */
17583   if (token->type == CPP_NAME
17584       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17585     {
17586       cp_token *identifier_token;
17587       bool ambiguous_p;
17588
17589       /* Look for the identifier.  */
17590       identifier_token = cp_lexer_peek_token (parser->lexer);
17591       ambiguous_p = identifier_token->ambiguous_p;
17592       identifier = cp_parser_identifier (parser);
17593       /* If the next token isn't an identifier, we are certainly not
17594          looking at a class-name.  */
17595       if (identifier == error_mark_node)
17596         decl = error_mark_node;
17597       /* If we know this is a type-name, there's no need to look it
17598          up.  */
17599       else if (typename_p)
17600         decl = identifier;
17601       else
17602         {
17603           tree ambiguous_decls;
17604           /* If we already know that this lookup is ambiguous, then
17605              we've already issued an error message; there's no reason
17606              to check again.  */
17607           if (ambiguous_p)
17608             {
17609               cp_parser_simulate_error (parser);
17610               return error_mark_node;
17611             }
17612           /* If the next token is a `::', then the name must be a type
17613              name.
17614
17615              [basic.lookup.qual]
17616
17617              During the lookup for a name preceding the :: scope
17618              resolution operator, object, function, and enumerator
17619              names are ignored.  */
17620           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17621             tag_type = typename_type;
17622           /* Look up the name.  */
17623           decl = cp_parser_lookup_name (parser, identifier,
17624                                         tag_type,
17625                                         /*is_template=*/false,
17626                                         /*is_namespace=*/false,
17627                                         check_dependency_p,
17628                                         &ambiguous_decls,
17629                                         identifier_token->location);
17630           if (ambiguous_decls)
17631             {
17632               if (cp_parser_parsing_tentatively (parser))
17633                 cp_parser_simulate_error (parser);
17634               return error_mark_node;
17635             }
17636         }
17637     }
17638   else
17639     {
17640       /* Try a template-id.  */
17641       decl = cp_parser_template_id (parser, template_keyword_p,
17642                                     check_dependency_p,
17643                                     is_declaration);
17644       if (decl == error_mark_node)
17645         return error_mark_node;
17646     }
17647
17648   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17649
17650   /* If this is a typename, create a TYPENAME_TYPE.  */
17651   if (typename_p && decl != error_mark_node)
17652     {
17653       decl = make_typename_type (scope, decl, typename_type,
17654                                  /*complain=*/tf_error);
17655       if (decl != error_mark_node)
17656         decl = TYPE_NAME (decl);
17657     }
17658
17659   /* Check to see that it is really the name of a class.  */
17660   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17661       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17662       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17663     /* Situations like this:
17664
17665          template <typename T> struct A {
17666            typename T::template X<int>::I i;
17667          };
17668
17669        are problematic.  Is `T::template X<int>' a class-name?  The
17670        standard does not seem to be definitive, but there is no other
17671        valid interpretation of the following `::'.  Therefore, those
17672        names are considered class-names.  */
17673     {
17674       decl = make_typename_type (scope, decl, tag_type, tf_error);
17675       if (decl != error_mark_node)
17676         decl = TYPE_NAME (decl);
17677     }
17678   else if (TREE_CODE (decl) != TYPE_DECL
17679            || TREE_TYPE (decl) == error_mark_node
17680            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17681            /* In Objective-C 2.0, a classname followed by '.' starts a
17682               dot-syntax expression, and it's not a type-name.  */
17683            || (c_dialect_objc ()
17684                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17685                && objc_is_class_name (decl)))
17686     decl = error_mark_node;
17687
17688   if (decl == error_mark_node)
17689     cp_parser_error (parser, "expected class-name");
17690   else if (identifier && !parser->scope)
17691     maybe_note_name_used_in_class (identifier, decl);
17692
17693   return decl;
17694 }
17695
17696 /* Parse a class-specifier.
17697
17698    class-specifier:
17699      class-head { member-specification [opt] }
17700
17701    Returns the TREE_TYPE representing the class.  */
17702
17703 static tree
17704 cp_parser_class_specifier_1 (cp_parser* parser)
17705 {
17706   tree type;
17707   tree attributes = NULL_TREE;
17708   bool nested_name_specifier_p;
17709   unsigned saved_num_template_parameter_lists;
17710   bool saved_in_function_body;
17711   unsigned char in_statement;
17712   bool in_switch_statement_p;
17713   bool saved_in_unbraced_linkage_specification_p;
17714   tree old_scope = NULL_TREE;
17715   tree scope = NULL_TREE;
17716   tree bases;
17717   cp_token *closing_brace;
17718
17719   push_deferring_access_checks (dk_no_deferred);
17720
17721   /* Parse the class-head.  */
17722   type = cp_parser_class_head (parser,
17723                                &nested_name_specifier_p,
17724                                &attributes,
17725                                &bases);
17726   /* If the class-head was a semantic disaster, skip the entire body
17727      of the class.  */
17728   if (!type)
17729     {
17730       cp_parser_skip_to_end_of_block_or_statement (parser);
17731       pop_deferring_access_checks ();
17732       return error_mark_node;
17733     }
17734
17735   /* Look for the `{'.  */
17736   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17737     {
17738       pop_deferring_access_checks ();
17739       return error_mark_node;
17740     }
17741
17742   /* Process the base classes. If they're invalid, skip the 
17743      entire class body.  */
17744   if (!xref_basetypes (type, bases))
17745     {
17746       /* Consuming the closing brace yields better error messages
17747          later on.  */
17748       if (cp_parser_skip_to_closing_brace (parser))
17749         cp_lexer_consume_token (parser->lexer);
17750       pop_deferring_access_checks ();
17751       return error_mark_node;
17752     }
17753
17754   /* Issue an error message if type-definitions are forbidden here.  */
17755   cp_parser_check_type_definition (parser);
17756   /* Remember that we are defining one more class.  */
17757   ++parser->num_classes_being_defined;
17758   /* Inside the class, surrounding template-parameter-lists do not
17759      apply.  */
17760   saved_num_template_parameter_lists
17761     = parser->num_template_parameter_lists;
17762   parser->num_template_parameter_lists = 0;
17763   /* We are not in a function body.  */
17764   saved_in_function_body = parser->in_function_body;
17765   parser->in_function_body = false;
17766   /* Or in a loop.  */
17767   in_statement = parser->in_statement;
17768   parser->in_statement = 0;
17769   /* Or in a switch.  */
17770   in_switch_statement_p = parser->in_switch_statement_p;
17771   parser->in_switch_statement_p = false;
17772   /* We are not immediately inside an extern "lang" block.  */
17773   saved_in_unbraced_linkage_specification_p
17774     = parser->in_unbraced_linkage_specification_p;
17775   parser->in_unbraced_linkage_specification_p = false;
17776
17777   /* Start the class.  */
17778   if (nested_name_specifier_p)
17779     {
17780       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17781       old_scope = push_inner_scope (scope);
17782     }
17783   type = begin_class_definition (type, attributes);
17784
17785   if (type == error_mark_node)
17786     /* If the type is erroneous, skip the entire body of the class.  */
17787     cp_parser_skip_to_closing_brace (parser);
17788   else
17789     /* Parse the member-specification.  */
17790     cp_parser_member_specification_opt (parser);
17791
17792   /* Look for the trailing `}'.  */
17793   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17794   /* Look for trailing attributes to apply to this class.  */
17795   if (cp_parser_allow_gnu_extensions_p (parser))
17796     attributes = cp_parser_attributes_opt (parser);
17797   if (type != error_mark_node)
17798     type = finish_struct (type, attributes);
17799   if (nested_name_specifier_p)
17800     pop_inner_scope (old_scope, scope);
17801
17802   /* We've finished a type definition.  Check for the common syntax
17803      error of forgetting a semicolon after the definition.  We need to
17804      be careful, as we can't just check for not-a-semicolon and be done
17805      with it; the user might have typed:
17806
17807      class X { } c = ...;
17808      class X { } *p = ...;
17809
17810      and so forth.  Instead, enumerate all the possible tokens that
17811      might follow this production; if we don't see one of them, then
17812      complain and silently insert the semicolon.  */
17813   {
17814     cp_token *token = cp_lexer_peek_token (parser->lexer);
17815     bool want_semicolon = true;
17816
17817     switch (token->type)
17818       {
17819       case CPP_NAME:
17820       case CPP_SEMICOLON:
17821       case CPP_MULT:
17822       case CPP_AND:
17823       case CPP_OPEN_PAREN:
17824       case CPP_CLOSE_PAREN:
17825       case CPP_COMMA:
17826         want_semicolon = false;
17827         break;
17828
17829         /* While it's legal for type qualifiers and storage class
17830            specifiers to follow type definitions in the grammar, only
17831            compiler testsuites contain code like that.  Assume that if
17832            we see such code, then what we're really seeing is a case
17833            like:
17834
17835            class X { }
17836            const <type> var = ...;
17837
17838            or
17839
17840            class Y { }
17841            static <type> func (...) ...
17842
17843            i.e. the qualifier or specifier applies to the next
17844            declaration.  To do so, however, we need to look ahead one
17845            more token to see if *that* token is a type specifier.
17846
17847            This code could be improved to handle:
17848
17849            class Z { }
17850            static const <type> var = ...;  */
17851       case CPP_KEYWORD:
17852         if (keyword_is_decl_specifier (token->keyword))
17853           {
17854             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17855
17856             /* Handling user-defined types here would be nice, but very
17857                tricky.  */
17858             want_semicolon
17859               = (lookahead->type == CPP_KEYWORD
17860                  && keyword_begins_type_specifier (lookahead->keyword));
17861           }
17862         break;
17863       default:
17864         break;
17865       }
17866
17867     /* If we don't have a type, then something is very wrong and we
17868        shouldn't try to do anything clever.  Likewise for not seeing the
17869        closing brace.  */
17870     if (closing_brace && TYPE_P (type) && want_semicolon)
17871       {
17872         cp_token_position prev
17873           = cp_lexer_previous_token_position (parser->lexer);
17874         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17875         location_t loc = prev_token->location;
17876
17877         if (CLASSTYPE_DECLARED_CLASS (type))
17878           error_at (loc, "expected %<;%> after class definition");
17879         else if (TREE_CODE (type) == RECORD_TYPE)
17880           error_at (loc, "expected %<;%> after struct definition");
17881         else if (TREE_CODE (type) == UNION_TYPE)
17882           error_at (loc, "expected %<;%> after union definition");
17883         else
17884           gcc_unreachable ();
17885
17886         /* Unget one token and smash it to look as though we encountered
17887            a semicolon in the input stream.  */
17888         cp_lexer_set_token_position (parser->lexer, prev);
17889         token = cp_lexer_peek_token (parser->lexer);
17890         token->type = CPP_SEMICOLON;
17891         token->keyword = RID_MAX;
17892       }
17893   }
17894
17895   /* If this class is not itself within the scope of another class,
17896      then we need to parse the bodies of all of the queued function
17897      definitions.  Note that the queued functions defined in a class
17898      are not always processed immediately following the
17899      class-specifier for that class.  Consider:
17900
17901        struct A {
17902          struct B { void f() { sizeof (A); } };
17903        };
17904
17905      If `f' were processed before the processing of `A' were
17906      completed, there would be no way to compute the size of `A'.
17907      Note that the nesting we are interested in here is lexical --
17908      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17909      for:
17910
17911        struct A { struct B; };
17912        struct A::B { void f() { } };
17913
17914      there is no need to delay the parsing of `A::B::f'.  */
17915   if (--parser->num_classes_being_defined == 0)
17916     {
17917       tree decl;
17918       tree class_type = NULL_TREE;
17919       tree pushed_scope = NULL_TREE;
17920       unsigned ix;
17921       cp_default_arg_entry *e;
17922       tree save_ccp, save_ccr;
17923
17924       /* In a first pass, parse default arguments to the functions.
17925          Then, in a second pass, parse the bodies of the functions.
17926          This two-phased approach handles cases like:
17927
17928             struct S {
17929               void f() { g(); }
17930               void g(int i = 3);
17931             };
17932
17933          */
17934       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17935                         ix, e)
17936         {
17937           decl = e->decl;
17938           /* If there are default arguments that have not yet been processed,
17939              take care of them now.  */
17940           if (class_type != e->class_type)
17941             {
17942               if (pushed_scope)
17943                 pop_scope (pushed_scope);
17944               class_type = e->class_type;
17945               pushed_scope = push_scope (class_type);
17946             }
17947           /* Make sure that any template parameters are in scope.  */
17948           maybe_begin_member_template_processing (decl);
17949           /* Parse the default argument expressions.  */
17950           cp_parser_late_parsing_default_args (parser, decl);
17951           /* Remove any template parameters from the symbol table.  */
17952           maybe_end_member_template_processing ();
17953         }
17954       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17955       /* Now parse any NSDMIs.  */
17956       save_ccp = current_class_ptr;
17957       save_ccr = current_class_ref;
17958       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
17959         {
17960           if (class_type != DECL_CONTEXT (decl))
17961             {
17962               if (pushed_scope)
17963                 pop_scope (pushed_scope);
17964               class_type = DECL_CONTEXT (decl);
17965               pushed_scope = push_scope (class_type);
17966             }
17967           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
17968           cp_parser_late_parsing_nsdmi (parser, decl);
17969         }
17970       VEC_truncate (tree, unparsed_nsdmis, 0);
17971       current_class_ptr = save_ccp;
17972       current_class_ref = save_ccr;
17973       if (pushed_scope)
17974         pop_scope (pushed_scope);
17975       /* Now parse the body of the functions.  */
17976       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
17977         cp_parser_late_parsing_for_member (parser, decl);
17978       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17979     }
17980
17981   /* Put back any saved access checks.  */
17982   pop_deferring_access_checks ();
17983
17984   /* Restore saved state.  */
17985   parser->in_switch_statement_p = in_switch_statement_p;
17986   parser->in_statement = in_statement;
17987   parser->in_function_body = saved_in_function_body;
17988   parser->num_template_parameter_lists
17989     = saved_num_template_parameter_lists;
17990   parser->in_unbraced_linkage_specification_p
17991     = saved_in_unbraced_linkage_specification_p;
17992
17993   return type;
17994 }
17995
17996 static tree
17997 cp_parser_class_specifier (cp_parser* parser)
17998 {
17999   tree ret;
18000   timevar_push (TV_PARSE_STRUCT);
18001   ret = cp_parser_class_specifier_1 (parser);
18002   timevar_pop (TV_PARSE_STRUCT);
18003   return ret;
18004 }
18005
18006 /* Parse a class-head.
18007
18008    class-head:
18009      class-key identifier [opt] base-clause [opt]
18010      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18011      class-key nested-name-specifier [opt] template-id
18012        base-clause [opt]
18013
18014    class-virt-specifier:
18015      final
18016
18017    GNU Extensions:
18018      class-key attributes identifier [opt] base-clause [opt]
18019      class-key attributes nested-name-specifier identifier base-clause [opt]
18020      class-key attributes nested-name-specifier [opt] template-id
18021        base-clause [opt]
18022
18023    Upon return BASES is initialized to the list of base classes (or
18024    NULL, if there are none) in the same form returned by
18025    cp_parser_base_clause.
18026
18027    Returns the TYPE of the indicated class.  Sets
18028    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18029    involving a nested-name-specifier was used, and FALSE otherwise.
18030
18031    Returns error_mark_node if this is not a class-head.
18032
18033    Returns NULL_TREE if the class-head is syntactically valid, but
18034    semantically invalid in a way that means we should skip the entire
18035    body of the class.  */
18036
18037 static tree
18038 cp_parser_class_head (cp_parser* parser,
18039                       bool* nested_name_specifier_p,
18040                       tree *attributes_p,
18041                       tree *bases)
18042 {
18043   tree nested_name_specifier;
18044   enum tag_types class_key;
18045   tree id = NULL_TREE;
18046   tree type = NULL_TREE;
18047   tree attributes;
18048   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18049   bool template_id_p = false;
18050   bool qualified_p = false;
18051   bool invalid_nested_name_p = false;
18052   bool invalid_explicit_specialization_p = false;
18053   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18054   tree pushed_scope = NULL_TREE;
18055   unsigned num_templates;
18056   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18057   /* Assume no nested-name-specifier will be present.  */
18058   *nested_name_specifier_p = false;
18059   /* Assume no template parameter lists will be used in defining the
18060      type.  */
18061   num_templates = 0;
18062   parser->colon_corrects_to_scope_p = false;
18063
18064   *bases = NULL_TREE;
18065
18066   /* Look for the class-key.  */
18067   class_key = cp_parser_class_key (parser);
18068   if (class_key == none_type)
18069     return error_mark_node;
18070
18071   /* Parse the attributes.  */
18072   attributes = cp_parser_attributes_opt (parser);
18073
18074   /* If the next token is `::', that is invalid -- but sometimes
18075      people do try to write:
18076
18077        struct ::S {};
18078
18079      Handle this gracefully by accepting the extra qualifier, and then
18080      issuing an error about it later if this really is a
18081      class-head.  If it turns out just to be an elaborated type
18082      specifier, remain silent.  */
18083   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18084     qualified_p = true;
18085
18086   push_deferring_access_checks (dk_no_check);
18087
18088   /* Determine the name of the class.  Begin by looking for an
18089      optional nested-name-specifier.  */
18090   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18091   nested_name_specifier
18092     = cp_parser_nested_name_specifier_opt (parser,
18093                                            /*typename_keyword_p=*/false,
18094                                            /*check_dependency_p=*/false,
18095                                            /*type_p=*/false,
18096                                            /*is_declaration=*/false);
18097   /* If there was a nested-name-specifier, then there *must* be an
18098      identifier.  */
18099   if (nested_name_specifier)
18100     {
18101       type_start_token = cp_lexer_peek_token (parser->lexer);
18102       /* Although the grammar says `identifier', it really means
18103          `class-name' or `template-name'.  You are only allowed to
18104          define a class that has already been declared with this
18105          syntax.
18106
18107          The proposed resolution for Core Issue 180 says that wherever
18108          you see `class T::X' you should treat `X' as a type-name.
18109
18110          It is OK to define an inaccessible class; for example:
18111
18112            class A { class B; };
18113            class A::B {};
18114
18115          We do not know if we will see a class-name, or a
18116          template-name.  We look for a class-name first, in case the
18117          class-name is a template-id; if we looked for the
18118          template-name first we would stop after the template-name.  */
18119       cp_parser_parse_tentatively (parser);
18120       type = cp_parser_class_name (parser,
18121                                    /*typename_keyword_p=*/false,
18122                                    /*template_keyword_p=*/false,
18123                                    class_type,
18124                                    /*check_dependency_p=*/false,
18125                                    /*class_head_p=*/true,
18126                                    /*is_declaration=*/false);
18127       /* If that didn't work, ignore the nested-name-specifier.  */
18128       if (!cp_parser_parse_definitely (parser))
18129         {
18130           invalid_nested_name_p = true;
18131           type_start_token = cp_lexer_peek_token (parser->lexer);
18132           id = cp_parser_identifier (parser);
18133           if (id == error_mark_node)
18134             id = NULL_TREE;
18135         }
18136       /* If we could not find a corresponding TYPE, treat this
18137          declaration like an unqualified declaration.  */
18138       if (type == error_mark_node)
18139         nested_name_specifier = NULL_TREE;
18140       /* Otherwise, count the number of templates used in TYPE and its
18141          containing scopes.  */
18142       else
18143         {
18144           tree scope;
18145
18146           for (scope = TREE_TYPE (type);
18147                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18148                scope = (TYPE_P (scope)
18149                         ? TYPE_CONTEXT (scope)
18150                         : DECL_CONTEXT (scope)))
18151             if (TYPE_P (scope)
18152                 && CLASS_TYPE_P (scope)
18153                 && CLASSTYPE_TEMPLATE_INFO (scope)
18154                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18155                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18156               ++num_templates;
18157         }
18158     }
18159   /* Otherwise, the identifier is optional.  */
18160   else
18161     {
18162       /* We don't know whether what comes next is a template-id,
18163          an identifier, or nothing at all.  */
18164       cp_parser_parse_tentatively (parser);
18165       /* Check for a template-id.  */
18166       type_start_token = cp_lexer_peek_token (parser->lexer);
18167       id = cp_parser_template_id (parser,
18168                                   /*template_keyword_p=*/false,
18169                                   /*check_dependency_p=*/true,
18170                                   /*is_declaration=*/true);
18171       /* If that didn't work, it could still be an identifier.  */
18172       if (!cp_parser_parse_definitely (parser))
18173         {
18174           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18175             {
18176               type_start_token = cp_lexer_peek_token (parser->lexer);
18177               id = cp_parser_identifier (parser);
18178             }
18179           else
18180             id = NULL_TREE;
18181         }
18182       else
18183         {
18184           template_id_p = true;
18185           ++num_templates;
18186         }
18187     }
18188
18189   pop_deferring_access_checks ();
18190
18191   if (id)
18192     {
18193       cp_parser_check_for_invalid_template_id (parser, id,
18194                                                type_start_token->location);
18195     }
18196   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18197
18198   /* If it's not a `:' or a `{' then we can't really be looking at a
18199      class-head, since a class-head only appears as part of a
18200      class-specifier.  We have to detect this situation before calling
18201      xref_tag, since that has irreversible side-effects.  */
18202   if (!cp_parser_next_token_starts_class_definition_p (parser))
18203     {
18204       cp_parser_error (parser, "expected %<{%> or %<:%>");
18205       type = error_mark_node;
18206       goto out;
18207     }
18208
18209   /* At this point, we're going ahead with the class-specifier, even
18210      if some other problem occurs.  */
18211   cp_parser_commit_to_tentative_parse (parser);
18212   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18213     {
18214       cp_parser_error (parser,
18215                        "cannot specify %<override%> for a class");
18216       type = error_mark_node;
18217       goto out;
18218     }
18219   /* Issue the error about the overly-qualified name now.  */
18220   if (qualified_p)
18221     {
18222       cp_parser_error (parser,
18223                        "global qualification of class name is invalid");
18224       type = error_mark_node;
18225       goto out;
18226     }
18227   else if (invalid_nested_name_p)
18228     {
18229       cp_parser_error (parser,
18230                        "qualified name does not name a class");
18231       type = error_mark_node;
18232       goto out;
18233     }
18234   else if (nested_name_specifier)
18235     {
18236       tree scope;
18237
18238       /* Reject typedef-names in class heads.  */
18239       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18240         {
18241           error_at (type_start_token->location,
18242                     "invalid class name in declaration of %qD",
18243                     type);
18244           type = NULL_TREE;
18245           goto done;
18246         }
18247
18248       /* Figure out in what scope the declaration is being placed.  */
18249       scope = current_scope ();
18250       /* If that scope does not contain the scope in which the
18251          class was originally declared, the program is invalid.  */
18252       if (scope && !is_ancestor (scope, nested_name_specifier))
18253         {
18254           if (at_namespace_scope_p ())
18255             error_at (type_start_token->location,
18256                       "declaration of %qD in namespace %qD which does not "
18257                       "enclose %qD",
18258                       type, scope, nested_name_specifier);
18259           else
18260             error_at (type_start_token->location,
18261                       "declaration of %qD in %qD which does not enclose %qD",
18262                       type, scope, nested_name_specifier);
18263           type = NULL_TREE;
18264           goto done;
18265         }
18266       /* [dcl.meaning]
18267
18268          A declarator-id shall not be qualified except for the
18269          definition of a ... nested class outside of its class
18270          ... [or] the definition or explicit instantiation of a
18271          class member of a namespace outside of its namespace.  */
18272       if (scope == nested_name_specifier)
18273         {
18274           permerror (nested_name_specifier_token_start->location,
18275                      "extra qualification not allowed");
18276           nested_name_specifier = NULL_TREE;
18277           num_templates = 0;
18278         }
18279     }
18280   /* An explicit-specialization must be preceded by "template <>".  If
18281      it is not, try to recover gracefully.  */
18282   if (at_namespace_scope_p ()
18283       && parser->num_template_parameter_lists == 0
18284       && template_id_p)
18285     {
18286       error_at (type_start_token->location,
18287                 "an explicit specialization must be preceded by %<template <>%>");
18288       invalid_explicit_specialization_p = true;
18289       /* Take the same action that would have been taken by
18290          cp_parser_explicit_specialization.  */
18291       ++parser->num_template_parameter_lists;
18292       begin_specialization ();
18293     }
18294   /* There must be no "return" statements between this point and the
18295      end of this function; set "type "to the correct return value and
18296      use "goto done;" to return.  */
18297   /* Make sure that the right number of template parameters were
18298      present.  */
18299   if (!cp_parser_check_template_parameters (parser, num_templates,
18300                                             type_start_token->location,
18301                                             /*declarator=*/NULL))
18302     {
18303       /* If something went wrong, there is no point in even trying to
18304          process the class-definition.  */
18305       type = NULL_TREE;
18306       goto done;
18307     }
18308
18309   /* Look up the type.  */
18310   if (template_id_p)
18311     {
18312       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18313           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18314               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18315         {
18316           error_at (type_start_token->location,
18317                     "function template %qD redeclared as a class template", id);
18318           type = error_mark_node;
18319         }
18320       else
18321         {
18322           type = TREE_TYPE (id);
18323           type = maybe_process_partial_specialization (type);
18324         }
18325       if (nested_name_specifier)
18326         pushed_scope = push_scope (nested_name_specifier);
18327     }
18328   else if (nested_name_specifier)
18329     {
18330       tree class_type;
18331
18332       /* Given:
18333
18334             template <typename T> struct S { struct T };
18335             template <typename T> struct S<T>::T { };
18336
18337          we will get a TYPENAME_TYPE when processing the definition of
18338          `S::T'.  We need to resolve it to the actual type before we
18339          try to define it.  */
18340       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18341         {
18342           class_type = resolve_typename_type (TREE_TYPE (type),
18343                                               /*only_current_p=*/false);
18344           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18345             type = TYPE_NAME (class_type);
18346           else
18347             {
18348               cp_parser_error (parser, "could not resolve typename type");
18349               type = error_mark_node;
18350             }
18351         }
18352
18353       if (maybe_process_partial_specialization (TREE_TYPE (type))
18354           == error_mark_node)
18355         {
18356           type = NULL_TREE;
18357           goto done;
18358         }
18359
18360       class_type = current_class_type;
18361       /* Enter the scope indicated by the nested-name-specifier.  */
18362       pushed_scope = push_scope (nested_name_specifier);
18363       /* Get the canonical version of this type.  */
18364       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18365       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18366           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18367         {
18368           type = push_template_decl (type);
18369           if (type == error_mark_node)
18370             {
18371               type = NULL_TREE;
18372               goto done;
18373             }
18374         }
18375
18376       type = TREE_TYPE (type);
18377       *nested_name_specifier_p = true;
18378     }
18379   else      /* The name is not a nested name.  */
18380     {
18381       /* If the class was unnamed, create a dummy name.  */
18382       if (!id)
18383         id = make_anon_name ();
18384       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18385                        parser->num_template_parameter_lists);
18386     }
18387
18388   /* Indicate whether this class was declared as a `class' or as a
18389      `struct'.  */
18390   if (TREE_CODE (type) == RECORD_TYPE)
18391     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18392   cp_parser_check_class_key (class_key, type);
18393
18394   /* If this type was already complete, and we see another definition,
18395      that's an error.  */
18396   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18397     {
18398       error_at (type_start_token->location, "redefinition of %q#T",
18399                 type);
18400       error_at (type_start_token->location, "previous definition of %q+#T",
18401                 type);
18402       type = NULL_TREE;
18403       goto done;
18404     }
18405   else if (type == error_mark_node)
18406     type = NULL_TREE;
18407
18408   /* We will have entered the scope containing the class; the names of
18409      base classes should be looked up in that context.  For example:
18410
18411        struct A { struct B {}; struct C; };
18412        struct A::C : B {};
18413
18414      is valid.  */
18415
18416   /* Get the list of base-classes, if there is one.  */
18417   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18418     *bases = cp_parser_base_clause (parser);
18419
18420  done:
18421   /* Leave the scope given by the nested-name-specifier.  We will
18422      enter the class scope itself while processing the members.  */
18423   if (pushed_scope)
18424     pop_scope (pushed_scope);
18425
18426   if (invalid_explicit_specialization_p)
18427     {
18428       end_specialization ();
18429       --parser->num_template_parameter_lists;
18430     }
18431
18432   if (type)
18433     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18434   *attributes_p = attributes;
18435   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18436     CLASSTYPE_FINAL (type) = 1;
18437  out:
18438   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18439   return type;
18440 }
18441
18442 /* Parse a class-key.
18443
18444    class-key:
18445      class
18446      struct
18447      union
18448
18449    Returns the kind of class-key specified, or none_type to indicate
18450    error.  */
18451
18452 static enum tag_types
18453 cp_parser_class_key (cp_parser* parser)
18454 {
18455   cp_token *token;
18456   enum tag_types tag_type;
18457
18458   /* Look for the class-key.  */
18459   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18460   if (!token)
18461     return none_type;
18462
18463   /* Check to see if the TOKEN is a class-key.  */
18464   tag_type = cp_parser_token_is_class_key (token);
18465   if (!tag_type)
18466     cp_parser_error (parser, "expected class-key");
18467   return tag_type;
18468 }
18469
18470 /* Parse an (optional) member-specification.
18471
18472    member-specification:
18473      member-declaration member-specification [opt]
18474      access-specifier : member-specification [opt]  */
18475
18476 static void
18477 cp_parser_member_specification_opt (cp_parser* parser)
18478 {
18479   while (true)
18480     {
18481       cp_token *token;
18482       enum rid keyword;
18483
18484       /* Peek at the next token.  */
18485       token = cp_lexer_peek_token (parser->lexer);
18486       /* If it's a `}', or EOF then we've seen all the members.  */
18487       if (token->type == CPP_CLOSE_BRACE
18488           || token->type == CPP_EOF
18489           || token->type == CPP_PRAGMA_EOL)
18490         break;
18491
18492       /* See if this token is a keyword.  */
18493       keyword = token->keyword;
18494       switch (keyword)
18495         {
18496         case RID_PUBLIC:
18497         case RID_PROTECTED:
18498         case RID_PRIVATE:
18499           /* Consume the access-specifier.  */
18500           cp_lexer_consume_token (parser->lexer);
18501           /* Remember which access-specifier is active.  */
18502           current_access_specifier = token->u.value;
18503           /* Look for the `:'.  */
18504           cp_parser_require (parser, CPP_COLON, RT_COLON);
18505           break;
18506
18507         default:
18508           /* Accept #pragmas at class scope.  */
18509           if (token->type == CPP_PRAGMA)
18510             {
18511               cp_parser_pragma (parser, pragma_external);
18512               break;
18513             }
18514
18515           /* Otherwise, the next construction must be a
18516              member-declaration.  */
18517           cp_parser_member_declaration (parser);
18518         }
18519     }
18520 }
18521
18522 /* Parse a member-declaration.
18523
18524    member-declaration:
18525      decl-specifier-seq [opt] member-declarator-list [opt] ;
18526      function-definition ; [opt]
18527      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18528      using-declaration
18529      template-declaration
18530
18531    member-declarator-list:
18532      member-declarator
18533      member-declarator-list , member-declarator
18534
18535    member-declarator:
18536      declarator pure-specifier [opt]
18537      declarator constant-initializer [opt]
18538      identifier [opt] : constant-expression
18539
18540    GNU Extensions:
18541
18542    member-declaration:
18543      __extension__ member-declaration
18544
18545    member-declarator:
18546      declarator attributes [opt] pure-specifier [opt]
18547      declarator attributes [opt] constant-initializer [opt]
18548      identifier [opt] attributes [opt] : constant-expression  
18549
18550    C++0x Extensions:
18551
18552    member-declaration:
18553      static_assert-declaration  */
18554
18555 static void
18556 cp_parser_member_declaration (cp_parser* parser)
18557 {
18558   cp_decl_specifier_seq decl_specifiers;
18559   tree prefix_attributes;
18560   tree decl;
18561   int declares_class_or_enum;
18562   bool friend_p;
18563   cp_token *token = NULL;
18564   cp_token *decl_spec_token_start = NULL;
18565   cp_token *initializer_token_start = NULL;
18566   int saved_pedantic;
18567   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18568
18569   /* Check for the `__extension__' keyword.  */
18570   if (cp_parser_extension_opt (parser, &saved_pedantic))
18571     {
18572       /* Recurse.  */
18573       cp_parser_member_declaration (parser);
18574       /* Restore the old value of the PEDANTIC flag.  */
18575       pedantic = saved_pedantic;
18576
18577       return;
18578     }
18579
18580   /* Check for a template-declaration.  */
18581   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18582     {
18583       /* An explicit specialization here is an error condition, and we
18584          expect the specialization handler to detect and report this.  */
18585       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18586           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18587         cp_parser_explicit_specialization (parser);
18588       else
18589         cp_parser_template_declaration (parser, /*member_p=*/true);
18590
18591       return;
18592     }
18593
18594   /* Check for a using-declaration.  */
18595   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18596     {
18597       /* Parse the using-declaration.  */
18598       cp_parser_using_declaration (parser,
18599                                    /*access_declaration_p=*/false);
18600       return;
18601     }
18602
18603   /* Check for @defs.  */
18604   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18605     {
18606       tree ivar, member;
18607       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18608       ivar = ivar_chains;
18609       while (ivar)
18610         {
18611           member = ivar;
18612           ivar = TREE_CHAIN (member);
18613           TREE_CHAIN (member) = NULL_TREE;
18614           finish_member_declaration (member);
18615         }
18616       return;
18617     }
18618
18619   /* If the next token is `static_assert' we have a static assertion.  */
18620   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18621     {
18622       cp_parser_static_assert (parser, /*member_p=*/true);
18623       return;
18624     }
18625
18626   parser->colon_corrects_to_scope_p = false;
18627
18628   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18629     goto out;
18630
18631   /* Parse the decl-specifier-seq.  */
18632   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18633   cp_parser_decl_specifier_seq (parser,
18634                                 CP_PARSER_FLAGS_OPTIONAL,
18635                                 &decl_specifiers,
18636                                 &declares_class_or_enum);
18637   prefix_attributes = decl_specifiers.attributes;
18638   decl_specifiers.attributes = NULL_TREE;
18639   /* Check for an invalid type-name.  */
18640   if (!decl_specifiers.any_type_specifiers_p
18641       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18642     goto out;
18643   /* If there is no declarator, then the decl-specifier-seq should
18644      specify a type.  */
18645   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18646     {
18647       /* If there was no decl-specifier-seq, and the next token is a
18648          `;', then we have something like:
18649
18650            struct S { ; };
18651
18652          [class.mem]
18653
18654          Each member-declaration shall declare at least one member
18655          name of the class.  */
18656       if (!decl_specifiers.any_specifiers_p)
18657         {
18658           cp_token *token = cp_lexer_peek_token (parser->lexer);
18659           if (!in_system_header_at (token->location))
18660             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18661         }
18662       else
18663         {
18664           tree type;
18665
18666           /* See if this declaration is a friend.  */
18667           friend_p = cp_parser_friend_p (&decl_specifiers);
18668           /* If there were decl-specifiers, check to see if there was
18669              a class-declaration.  */
18670           type = check_tag_decl (&decl_specifiers);
18671           /* Nested classes have already been added to the class, but
18672              a `friend' needs to be explicitly registered.  */
18673           if (friend_p)
18674             {
18675               /* If the `friend' keyword was present, the friend must
18676                  be introduced with a class-key.  */
18677                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18678                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18679                           "in C++03 a class-key must be used "
18680                           "when declaring a friend");
18681                /* In this case:
18682
18683                     template <typename T> struct A {
18684                       friend struct A<T>::B;
18685                     };
18686
18687                   A<T>::B will be represented by a TYPENAME_TYPE, and
18688                   therefore not recognized by check_tag_decl.  */
18689                if (!type)
18690                  {
18691                    type = decl_specifiers.type;
18692                    if (type && TREE_CODE (type) == TYPE_DECL)
18693                      type = TREE_TYPE (type);
18694                  }
18695                if (!type || !TYPE_P (type))
18696                  error_at (decl_spec_token_start->location,
18697                            "friend declaration does not name a class or "
18698                            "function");
18699                else
18700                  make_friend_class (current_class_type, type,
18701                                     /*complain=*/true);
18702             }
18703           /* If there is no TYPE, an error message will already have
18704              been issued.  */
18705           else if (!type || type == error_mark_node)
18706             ;
18707           /* An anonymous aggregate has to be handled specially; such
18708              a declaration really declares a data member (with a
18709              particular type), as opposed to a nested class.  */
18710           else if (ANON_AGGR_TYPE_P (type))
18711             {
18712               /* Remove constructors and such from TYPE, now that we
18713                  know it is an anonymous aggregate.  */
18714               fixup_anonymous_aggr (type);
18715               /* And make the corresponding data member.  */
18716               decl = build_decl (decl_spec_token_start->location,
18717                                  FIELD_DECL, NULL_TREE, type);
18718               /* Add it to the class.  */
18719               finish_member_declaration (decl);
18720             }
18721           else
18722             cp_parser_check_access_in_redeclaration
18723                                               (TYPE_NAME (type),
18724                                                decl_spec_token_start->location);
18725         }
18726     }
18727   else
18728     {
18729       bool assume_semicolon = false;
18730
18731       /* See if these declarations will be friends.  */
18732       friend_p = cp_parser_friend_p (&decl_specifiers);
18733
18734       /* Keep going until we hit the `;' at the end of the
18735          declaration.  */
18736       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18737         {
18738           tree attributes = NULL_TREE;
18739           tree first_attribute;
18740
18741           /* Peek at the next token.  */
18742           token = cp_lexer_peek_token (parser->lexer);
18743
18744           /* Check for a bitfield declaration.  */
18745           if (token->type == CPP_COLON
18746               || (token->type == CPP_NAME
18747                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18748                   == CPP_COLON))
18749             {
18750               tree identifier;
18751               tree width;
18752
18753               /* Get the name of the bitfield.  Note that we cannot just
18754                  check TOKEN here because it may have been invalidated by
18755                  the call to cp_lexer_peek_nth_token above.  */
18756               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18757                 identifier = cp_parser_identifier (parser);
18758               else
18759                 identifier = NULL_TREE;
18760
18761               /* Consume the `:' token.  */
18762               cp_lexer_consume_token (parser->lexer);
18763               /* Get the width of the bitfield.  */
18764               width
18765                 = cp_parser_constant_expression (parser,
18766                                                  /*allow_non_constant=*/false,
18767                                                  NULL);
18768
18769               /* Look for attributes that apply to the bitfield.  */
18770               attributes = cp_parser_attributes_opt (parser);
18771               /* Remember which attributes are prefix attributes and
18772                  which are not.  */
18773               first_attribute = attributes;
18774               /* Combine the attributes.  */
18775               attributes = chainon (prefix_attributes, attributes);
18776
18777               /* Create the bitfield declaration.  */
18778               decl = grokbitfield (identifier
18779                                    ? make_id_declarator (NULL_TREE,
18780                                                          identifier,
18781                                                          sfk_none)
18782                                    : NULL,
18783                                    &decl_specifiers,
18784                                    width,
18785                                    attributes);
18786             }
18787           else
18788             {
18789               cp_declarator *declarator;
18790               tree initializer;
18791               tree asm_specification;
18792               int ctor_dtor_or_conv_p;
18793
18794               /* Parse the declarator.  */
18795               declarator
18796                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18797                                         &ctor_dtor_or_conv_p,
18798                                         /*parenthesized_p=*/NULL,
18799                                         /*member_p=*/true);
18800
18801               /* If something went wrong parsing the declarator, make sure
18802                  that we at least consume some tokens.  */
18803               if (declarator == cp_error_declarator)
18804                 {
18805                   /* Skip to the end of the statement.  */
18806                   cp_parser_skip_to_end_of_statement (parser);
18807                   /* If the next token is not a semicolon, that is
18808                      probably because we just skipped over the body of
18809                      a function.  So, we consume a semicolon if
18810                      present, but do not issue an error message if it
18811                      is not present.  */
18812                   if (cp_lexer_next_token_is (parser->lexer,
18813                                               CPP_SEMICOLON))
18814                     cp_lexer_consume_token (parser->lexer);
18815                   goto out;
18816                 }
18817
18818               if (declares_class_or_enum & 2)
18819                 cp_parser_check_for_definition_in_return_type
18820                                             (declarator, decl_specifiers.type,
18821                                              decl_specifiers.type_location);
18822
18823               /* Look for an asm-specification.  */
18824               asm_specification = cp_parser_asm_specification_opt (parser);
18825               /* Look for attributes that apply to the declaration.  */
18826               attributes = cp_parser_attributes_opt (parser);
18827               /* Remember which attributes are prefix attributes and
18828                  which are not.  */
18829               first_attribute = attributes;
18830               /* Combine the attributes.  */
18831               attributes = chainon (prefix_attributes, attributes);
18832
18833               /* If it's an `=', then we have a constant-initializer or a
18834                  pure-specifier.  It is not correct to parse the
18835                  initializer before registering the member declaration
18836                  since the member declaration should be in scope while
18837                  its initializer is processed.  However, the rest of the
18838                  front end does not yet provide an interface that allows
18839                  us to handle this correctly.  */
18840               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18841                 {
18842                   /* In [class.mem]:
18843
18844                      A pure-specifier shall be used only in the declaration of
18845                      a virtual function.
18846
18847                      A member-declarator can contain a constant-initializer
18848                      only if it declares a static member of integral or
18849                      enumeration type.
18850
18851                      Therefore, if the DECLARATOR is for a function, we look
18852                      for a pure-specifier; otherwise, we look for a
18853                      constant-initializer.  When we call `grokfield', it will
18854                      perform more stringent semantics checks.  */
18855                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
18856                   if (function_declarator_p (declarator)
18857                       || (decl_specifiers.type
18858                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
18859                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
18860                               == FUNCTION_TYPE)))
18861                     initializer = cp_parser_pure_specifier (parser);
18862                   else if (decl_specifiers.storage_class != sc_static)
18863                     initializer = cp_parser_save_nsdmi (parser);
18864                   else if (cxx_dialect >= cxx0x)
18865                     {
18866                       bool nonconst;
18867                       /* Don't require a constant rvalue in C++11, since we
18868                          might want a reference constant.  We'll enforce
18869                          constancy later.  */
18870                       cp_lexer_consume_token (parser->lexer);
18871                       /* Parse the initializer.  */
18872                       initializer = cp_parser_initializer_clause (parser,
18873                                                                   &nonconst);
18874                     }
18875                   else
18876                     /* Parse the initializer.  */
18877                     initializer = cp_parser_constant_initializer (parser);
18878                 }
18879               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18880                        && !function_declarator_p (declarator))
18881                 {
18882                   bool x;
18883                   if (decl_specifiers.storage_class != sc_static)
18884                     initializer = cp_parser_save_nsdmi (parser);
18885                   else
18886                     initializer = cp_parser_initializer (parser, &x, &x);
18887                 }
18888               /* Otherwise, there is no initializer.  */
18889               else
18890                 initializer = NULL_TREE;
18891
18892               /* See if we are probably looking at a function
18893                  definition.  We are certainly not looking at a
18894                  member-declarator.  Calling `grokfield' has
18895                  side-effects, so we must not do it unless we are sure
18896                  that we are looking at a member-declarator.  */
18897               if (cp_parser_token_starts_function_definition_p
18898                   (cp_lexer_peek_token (parser->lexer)))
18899                 {
18900                   /* The grammar does not allow a pure-specifier to be
18901                      used when a member function is defined.  (It is
18902                      possible that this fact is an oversight in the
18903                      standard, since a pure function may be defined
18904                      outside of the class-specifier.  */
18905                   if (initializer)
18906                     error_at (initializer_token_start->location,
18907                               "pure-specifier on function-definition");
18908                   decl = cp_parser_save_member_function_body (parser,
18909                                                               &decl_specifiers,
18910                                                               declarator,
18911                                                               attributes);
18912                   /* If the member was not a friend, declare it here.  */
18913                   if (!friend_p)
18914                     finish_member_declaration (decl);
18915                   /* Peek at the next token.  */
18916                   token = cp_lexer_peek_token (parser->lexer);
18917                   /* If the next token is a semicolon, consume it.  */
18918                   if (token->type == CPP_SEMICOLON)
18919                     cp_lexer_consume_token (parser->lexer);
18920                   goto out;
18921                 }
18922               else
18923                 if (declarator->kind == cdk_function)
18924                   declarator->id_loc = token->location;
18925                 /* Create the declaration.  */
18926                 decl = grokfield (declarator, &decl_specifiers,
18927                                   initializer, /*init_const_expr_p=*/true,
18928                                   asm_specification,
18929                                   attributes);
18930             }
18931
18932           /* Reset PREFIX_ATTRIBUTES.  */
18933           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18934             attributes = TREE_CHAIN (attributes);
18935           if (attributes)
18936             TREE_CHAIN (attributes) = NULL_TREE;
18937
18938           /* If there is any qualification still in effect, clear it
18939              now; we will be starting fresh with the next declarator.  */
18940           parser->scope = NULL_TREE;
18941           parser->qualifying_scope = NULL_TREE;
18942           parser->object_scope = NULL_TREE;
18943           /* If it's a `,', then there are more declarators.  */
18944           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18945             cp_lexer_consume_token (parser->lexer);
18946           /* If the next token isn't a `;', then we have a parse error.  */
18947           else if (cp_lexer_next_token_is_not (parser->lexer,
18948                                                CPP_SEMICOLON))
18949             {
18950               /* The next token might be a ways away from where the
18951                  actual semicolon is missing.  Find the previous token
18952                  and use that for our error position.  */
18953               cp_token *token = cp_lexer_previous_token (parser->lexer);
18954               error_at (token->location,
18955                         "expected %<;%> at end of member declaration");
18956
18957               /* Assume that the user meant to provide a semicolon.  If
18958                  we were to cp_parser_skip_to_end_of_statement, we might
18959                  skip to a semicolon inside a member function definition
18960                  and issue nonsensical error messages.  */
18961               assume_semicolon = true;
18962             }
18963
18964           if (decl)
18965             {
18966               /* Add DECL to the list of members.  */
18967               if (!friend_p)
18968                 finish_member_declaration (decl);
18969
18970               if (TREE_CODE (decl) == FUNCTION_DECL)
18971                 cp_parser_save_default_args (parser, decl);
18972               else if (TREE_CODE (decl) == FIELD_DECL
18973                        && !DECL_C_BIT_FIELD (decl)
18974                        && DECL_INITIAL (decl))
18975                 /* Add DECL to the queue of NSDMI to be parsed later.  */
18976                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
18977             }
18978
18979           if (assume_semicolon)
18980             goto out;
18981         }
18982     }
18983
18984   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18985  out:
18986   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18987 }
18988
18989 /* Parse a pure-specifier.
18990
18991    pure-specifier:
18992      = 0
18993
18994    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18995    Otherwise, ERROR_MARK_NODE is returned.  */
18996
18997 static tree
18998 cp_parser_pure_specifier (cp_parser* parser)
18999 {
19000   cp_token *token;
19001
19002   /* Look for the `=' token.  */
19003   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19004     return error_mark_node;
19005   /* Look for the `0' token.  */
19006   token = cp_lexer_peek_token (parser->lexer);
19007
19008   if (token->type == CPP_EOF
19009       || token->type == CPP_PRAGMA_EOL)
19010     return error_mark_node;
19011
19012   cp_lexer_consume_token (parser->lexer);
19013
19014   /* Accept = default or = delete in c++0x mode.  */
19015   if (token->keyword == RID_DEFAULT
19016       || token->keyword == RID_DELETE)
19017     {
19018       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19019       return token->u.value;
19020     }
19021
19022   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19023   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19024     {
19025       cp_parser_error (parser,
19026                        "invalid pure specifier (only %<= 0%> is allowed)");
19027       cp_parser_skip_to_end_of_statement (parser);
19028       return error_mark_node;
19029     }
19030   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19031     {
19032       error_at (token->location, "templates may not be %<virtual%>");
19033       return error_mark_node;
19034     }
19035
19036   return integer_zero_node;
19037 }
19038
19039 /* Parse a constant-initializer.
19040
19041    constant-initializer:
19042      = constant-expression
19043
19044    Returns a representation of the constant-expression.  */
19045
19046 static tree
19047 cp_parser_constant_initializer (cp_parser* parser)
19048 {
19049   /* Look for the `=' token.  */
19050   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19051     return error_mark_node;
19052
19053   /* It is invalid to write:
19054
19055        struct S { static const int i = { 7 }; };
19056
19057      */
19058   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19059     {
19060       cp_parser_error (parser,
19061                        "a brace-enclosed initializer is not allowed here");
19062       /* Consume the opening brace.  */
19063       cp_lexer_consume_token (parser->lexer);
19064       /* Skip the initializer.  */
19065       cp_parser_skip_to_closing_brace (parser);
19066       /* Look for the trailing `}'.  */
19067       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19068
19069       return error_mark_node;
19070     }
19071
19072   return cp_parser_constant_expression (parser,
19073                                         /*allow_non_constant=*/false,
19074                                         NULL);
19075 }
19076
19077 /* Derived classes [gram.class.derived] */
19078
19079 /* Parse a base-clause.
19080
19081    base-clause:
19082      : base-specifier-list
19083
19084    base-specifier-list:
19085      base-specifier ... [opt]
19086      base-specifier-list , base-specifier ... [opt]
19087
19088    Returns a TREE_LIST representing the base-classes, in the order in
19089    which they were declared.  The representation of each node is as
19090    described by cp_parser_base_specifier.
19091
19092    In the case that no bases are specified, this function will return
19093    NULL_TREE, not ERROR_MARK_NODE.  */
19094
19095 static tree
19096 cp_parser_base_clause (cp_parser* parser)
19097 {
19098   tree bases = NULL_TREE;
19099
19100   /* Look for the `:' that begins the list.  */
19101   cp_parser_require (parser, CPP_COLON, RT_COLON);
19102
19103   /* Scan the base-specifier-list.  */
19104   while (true)
19105     {
19106       cp_token *token;
19107       tree base;
19108       bool pack_expansion_p = false;
19109
19110       /* Look for the base-specifier.  */
19111       base = cp_parser_base_specifier (parser);
19112       /* Look for the (optional) ellipsis. */
19113       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19114         {
19115           /* Consume the `...'. */
19116           cp_lexer_consume_token (parser->lexer);
19117
19118           pack_expansion_p = true;
19119         }
19120
19121       /* Add BASE to the front of the list.  */
19122       if (base && base != error_mark_node)
19123         {
19124           if (pack_expansion_p)
19125             /* Make this a pack expansion type. */
19126             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19127
19128           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19129             {
19130               TREE_CHAIN (base) = bases;
19131               bases = base;
19132             }
19133         }
19134       /* Peek at the next token.  */
19135       token = cp_lexer_peek_token (parser->lexer);
19136       /* If it's not a comma, then the list is complete.  */
19137       if (token->type != CPP_COMMA)
19138         break;
19139       /* Consume the `,'.  */
19140       cp_lexer_consume_token (parser->lexer);
19141     }
19142
19143   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19144      base class had a qualified name.  However, the next name that
19145      appears is certainly not qualified.  */
19146   parser->scope = NULL_TREE;
19147   parser->qualifying_scope = NULL_TREE;
19148   parser->object_scope = NULL_TREE;
19149
19150   return nreverse (bases);
19151 }
19152
19153 /* Parse a base-specifier.
19154
19155    base-specifier:
19156      :: [opt] nested-name-specifier [opt] class-name
19157      virtual access-specifier [opt] :: [opt] nested-name-specifier
19158        [opt] class-name
19159      access-specifier virtual [opt] :: [opt] nested-name-specifier
19160        [opt] class-name
19161
19162    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19163    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19164    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19165    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19166
19167 static tree
19168 cp_parser_base_specifier (cp_parser* parser)
19169 {
19170   cp_token *token;
19171   bool done = false;
19172   bool virtual_p = false;
19173   bool duplicate_virtual_error_issued_p = false;
19174   bool duplicate_access_error_issued_p = false;
19175   bool class_scope_p, template_p;
19176   tree access = access_default_node;
19177   tree type;
19178
19179   /* Process the optional `virtual' and `access-specifier'.  */
19180   while (!done)
19181     {
19182       /* Peek at the next token.  */
19183       token = cp_lexer_peek_token (parser->lexer);
19184       /* Process `virtual'.  */
19185       switch (token->keyword)
19186         {
19187         case RID_VIRTUAL:
19188           /* If `virtual' appears more than once, issue an error.  */
19189           if (virtual_p && !duplicate_virtual_error_issued_p)
19190             {
19191               cp_parser_error (parser,
19192                                "%<virtual%> specified more than once in base-specified");
19193               duplicate_virtual_error_issued_p = true;
19194             }
19195
19196           virtual_p = true;
19197
19198           /* Consume the `virtual' token.  */
19199           cp_lexer_consume_token (parser->lexer);
19200
19201           break;
19202
19203         case RID_PUBLIC:
19204         case RID_PROTECTED:
19205         case RID_PRIVATE:
19206           /* If more than one access specifier appears, issue an
19207              error.  */
19208           if (access != access_default_node
19209               && !duplicate_access_error_issued_p)
19210             {
19211               cp_parser_error (parser,
19212                                "more than one access specifier in base-specified");
19213               duplicate_access_error_issued_p = true;
19214             }
19215
19216           access = ridpointers[(int) token->keyword];
19217
19218           /* Consume the access-specifier.  */
19219           cp_lexer_consume_token (parser->lexer);
19220
19221           break;
19222
19223         default:
19224           done = true;
19225           break;
19226         }
19227     }
19228   /* It is not uncommon to see programs mechanically, erroneously, use
19229      the 'typename' keyword to denote (dependent) qualified types
19230      as base classes.  */
19231   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19232     {
19233       token = cp_lexer_peek_token (parser->lexer);
19234       if (!processing_template_decl)
19235         error_at (token->location,
19236                   "keyword %<typename%> not allowed outside of templates");
19237       else
19238         error_at (token->location,
19239                   "keyword %<typename%> not allowed in this context "
19240                   "(the base class is implicitly a type)");
19241       cp_lexer_consume_token (parser->lexer);
19242     }
19243
19244   /* Look for the optional `::' operator.  */
19245   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19246   /* Look for the nested-name-specifier.  The simplest way to
19247      implement:
19248
19249        [temp.res]
19250
19251        The keyword `typename' is not permitted in a base-specifier or
19252        mem-initializer; in these contexts a qualified name that
19253        depends on a template-parameter is implicitly assumed to be a
19254        type name.
19255
19256      is to pretend that we have seen the `typename' keyword at this
19257      point.  */
19258   cp_parser_nested_name_specifier_opt (parser,
19259                                        /*typename_keyword_p=*/true,
19260                                        /*check_dependency_p=*/true,
19261                                        typename_type,
19262                                        /*is_declaration=*/true);
19263   /* If the base class is given by a qualified name, assume that names
19264      we see are type names or templates, as appropriate.  */
19265   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19266   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19267
19268   if (!parser->scope
19269       && cp_lexer_next_token_is_decltype (parser->lexer))
19270     /* DR 950 allows decltype as a base-specifier.  */
19271     type = cp_parser_decltype (parser);
19272   else
19273     {
19274       /* Otherwise, look for the class-name.  */
19275       type = cp_parser_class_name (parser,
19276                                    class_scope_p,
19277                                    template_p,
19278                                    typename_type,
19279                                    /*check_dependency_p=*/true,
19280                                    /*class_head_p=*/false,
19281                                    /*is_declaration=*/true);
19282       type = TREE_TYPE (type);
19283     }
19284
19285   if (type == error_mark_node)
19286     return error_mark_node;
19287
19288   return finish_base_specifier (type, access, virtual_p);
19289 }
19290
19291 /* Exception handling [gram.exception] */
19292
19293 /* Parse an (optional) exception-specification.
19294
19295    exception-specification:
19296      throw ( type-id-list [opt] )
19297
19298    Returns a TREE_LIST representing the exception-specification.  The
19299    TREE_VALUE of each node is a type.  */
19300
19301 static tree
19302 cp_parser_exception_specification_opt (cp_parser* parser)
19303 {
19304   cp_token *token;
19305   tree type_id_list;
19306   const char *saved_message;
19307
19308   /* Peek at the next token.  */
19309   token = cp_lexer_peek_token (parser->lexer);
19310
19311   /* Is it a noexcept-specification?  */
19312   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19313     {
19314       tree expr;
19315       cp_lexer_consume_token (parser->lexer);
19316
19317       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19318         {
19319           cp_lexer_consume_token (parser->lexer);
19320
19321           /* Types may not be defined in an exception-specification.  */
19322           saved_message = parser->type_definition_forbidden_message;
19323           parser->type_definition_forbidden_message
19324             = G_("types may not be defined in an exception-specification");
19325
19326           expr = cp_parser_constant_expression (parser, false, NULL);
19327
19328           /* Restore the saved message.  */
19329           parser->type_definition_forbidden_message = saved_message;
19330
19331           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19332         }
19333       else
19334         expr = boolean_true_node;
19335
19336       return build_noexcept_spec (expr, tf_warning_or_error);
19337     }
19338
19339   /* If it's not `throw', then there's no exception-specification.  */
19340   if (!cp_parser_is_keyword (token, RID_THROW))
19341     return NULL_TREE;
19342
19343 #if 0
19344   /* Enable this once a lot of code has transitioned to noexcept?  */
19345   if (cxx_dialect == cxx0x && !in_system_header)
19346     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19347              "deprecated in C++0x; use %<noexcept%> instead");
19348 #endif
19349
19350   /* Consume the `throw'.  */
19351   cp_lexer_consume_token (parser->lexer);
19352
19353   /* Look for the `('.  */
19354   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19355
19356   /* Peek at the next token.  */
19357   token = cp_lexer_peek_token (parser->lexer);
19358   /* If it's not a `)', then there is a type-id-list.  */
19359   if (token->type != CPP_CLOSE_PAREN)
19360     {
19361       /* Types may not be defined in an exception-specification.  */
19362       saved_message = parser->type_definition_forbidden_message;
19363       parser->type_definition_forbidden_message
19364         = G_("types may not be defined in an exception-specification");
19365       /* Parse the type-id-list.  */
19366       type_id_list = cp_parser_type_id_list (parser);
19367       /* Restore the saved message.  */
19368       parser->type_definition_forbidden_message = saved_message;
19369     }
19370   else
19371     type_id_list = empty_except_spec;
19372
19373   /* Look for the `)'.  */
19374   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19375
19376   return type_id_list;
19377 }
19378
19379 /* Parse an (optional) type-id-list.
19380
19381    type-id-list:
19382      type-id ... [opt]
19383      type-id-list , type-id ... [opt]
19384
19385    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19386    in the order that the types were presented.  */
19387
19388 static tree
19389 cp_parser_type_id_list (cp_parser* parser)
19390 {
19391   tree types = NULL_TREE;
19392
19393   while (true)
19394     {
19395       cp_token *token;
19396       tree type;
19397
19398       /* Get the next type-id.  */
19399       type = cp_parser_type_id (parser);
19400       /* Parse the optional ellipsis. */
19401       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19402         {
19403           /* Consume the `...'. */
19404           cp_lexer_consume_token (parser->lexer);
19405
19406           /* Turn the type into a pack expansion expression. */
19407           type = make_pack_expansion (type);
19408         }
19409       /* Add it to the list.  */
19410       types = add_exception_specifier (types, type, /*complain=*/1);
19411       /* Peek at the next token.  */
19412       token = cp_lexer_peek_token (parser->lexer);
19413       /* If it is not a `,', we are done.  */
19414       if (token->type != CPP_COMMA)
19415         break;
19416       /* Consume the `,'.  */
19417       cp_lexer_consume_token (parser->lexer);
19418     }
19419
19420   return nreverse (types);
19421 }
19422
19423 /* Parse a try-block.
19424
19425    try-block:
19426      try compound-statement handler-seq  */
19427
19428 static tree
19429 cp_parser_try_block (cp_parser* parser)
19430 {
19431   tree try_block;
19432
19433   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19434   try_block = begin_try_block ();
19435   cp_parser_compound_statement (parser, NULL, true, false);
19436   finish_try_block (try_block);
19437   cp_parser_handler_seq (parser);
19438   finish_handler_sequence (try_block);
19439
19440   return try_block;
19441 }
19442
19443 /* Parse a function-try-block.
19444
19445    function-try-block:
19446      try ctor-initializer [opt] function-body handler-seq  */
19447
19448 static bool
19449 cp_parser_function_try_block (cp_parser* parser)
19450 {
19451   tree compound_stmt;
19452   tree try_block;
19453   bool ctor_initializer_p;
19454
19455   /* Look for the `try' keyword.  */
19456   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19457     return false;
19458   /* Let the rest of the front end know where we are.  */
19459   try_block = begin_function_try_block (&compound_stmt);
19460   /* Parse the function-body.  */
19461   ctor_initializer_p
19462     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19463   /* We're done with the `try' part.  */
19464   finish_function_try_block (try_block);
19465   /* Parse the handlers.  */
19466   cp_parser_handler_seq (parser);
19467   /* We're done with the handlers.  */
19468   finish_function_handler_sequence (try_block, compound_stmt);
19469
19470   return ctor_initializer_p;
19471 }
19472
19473 /* Parse a handler-seq.
19474
19475    handler-seq:
19476      handler handler-seq [opt]  */
19477
19478 static void
19479 cp_parser_handler_seq (cp_parser* parser)
19480 {
19481   while (true)
19482     {
19483       cp_token *token;
19484
19485       /* Parse the handler.  */
19486       cp_parser_handler (parser);
19487       /* Peek at the next token.  */
19488       token = cp_lexer_peek_token (parser->lexer);
19489       /* If it's not `catch' then there are no more handlers.  */
19490       if (!cp_parser_is_keyword (token, RID_CATCH))
19491         break;
19492     }
19493 }
19494
19495 /* Parse a handler.
19496
19497    handler:
19498      catch ( exception-declaration ) compound-statement  */
19499
19500 static void
19501 cp_parser_handler (cp_parser* parser)
19502 {
19503   tree handler;
19504   tree declaration;
19505
19506   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19507   handler = begin_handler ();
19508   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19509   declaration = cp_parser_exception_declaration (parser);
19510   finish_handler_parms (declaration, handler);
19511   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19512   cp_parser_compound_statement (parser, NULL, false, false);
19513   finish_handler (handler);
19514 }
19515
19516 /* Parse an exception-declaration.
19517
19518    exception-declaration:
19519      type-specifier-seq declarator
19520      type-specifier-seq abstract-declarator
19521      type-specifier-seq
19522      ...
19523
19524    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19525    ellipsis variant is used.  */
19526
19527 static tree
19528 cp_parser_exception_declaration (cp_parser* parser)
19529 {
19530   cp_decl_specifier_seq type_specifiers;
19531   cp_declarator *declarator;
19532   const char *saved_message;
19533
19534   /* If it's an ellipsis, it's easy to handle.  */
19535   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19536     {
19537       /* Consume the `...' token.  */
19538       cp_lexer_consume_token (parser->lexer);
19539       return NULL_TREE;
19540     }
19541
19542   /* Types may not be defined in exception-declarations.  */
19543   saved_message = parser->type_definition_forbidden_message;
19544   parser->type_definition_forbidden_message
19545     = G_("types may not be defined in exception-declarations");
19546
19547   /* Parse the type-specifier-seq.  */
19548   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19549                                 /*is_trailing_return=*/false,
19550                                 &type_specifiers);
19551   /* If it's a `)', then there is no declarator.  */
19552   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19553     declarator = NULL;
19554   else
19555     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19556                                        /*ctor_dtor_or_conv_p=*/NULL,
19557                                        /*parenthesized_p=*/NULL,
19558                                        /*member_p=*/false);
19559
19560   /* Restore the saved message.  */
19561   parser->type_definition_forbidden_message = saved_message;
19562
19563   if (!type_specifiers.any_specifiers_p)
19564     return error_mark_node;
19565
19566   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19567 }
19568
19569 /* Parse a throw-expression.
19570
19571    throw-expression:
19572      throw assignment-expression [opt]
19573
19574    Returns a THROW_EXPR representing the throw-expression.  */
19575
19576 static tree
19577 cp_parser_throw_expression (cp_parser* parser)
19578 {
19579   tree expression;
19580   cp_token* token;
19581
19582   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19583   token = cp_lexer_peek_token (parser->lexer);
19584   /* Figure out whether or not there is an assignment-expression
19585      following the "throw" keyword.  */
19586   if (token->type == CPP_COMMA
19587       || token->type == CPP_SEMICOLON
19588       || token->type == CPP_CLOSE_PAREN
19589       || token->type == CPP_CLOSE_SQUARE
19590       || token->type == CPP_CLOSE_BRACE
19591       || token->type == CPP_COLON)
19592     expression = NULL_TREE;
19593   else
19594     expression = cp_parser_assignment_expression (parser,
19595                                                   /*cast_p=*/false, NULL);
19596
19597   return build_throw (expression);
19598 }
19599
19600 /* GNU Extensions */
19601
19602 /* Parse an (optional) asm-specification.
19603
19604    asm-specification:
19605      asm ( string-literal )
19606
19607    If the asm-specification is present, returns a STRING_CST
19608    corresponding to the string-literal.  Otherwise, returns
19609    NULL_TREE.  */
19610
19611 static tree
19612 cp_parser_asm_specification_opt (cp_parser* parser)
19613 {
19614   cp_token *token;
19615   tree asm_specification;
19616
19617   /* Peek at the next token.  */
19618   token = cp_lexer_peek_token (parser->lexer);
19619   /* If the next token isn't the `asm' keyword, then there's no
19620      asm-specification.  */
19621   if (!cp_parser_is_keyword (token, RID_ASM))
19622     return NULL_TREE;
19623
19624   /* Consume the `asm' token.  */
19625   cp_lexer_consume_token (parser->lexer);
19626   /* Look for the `('.  */
19627   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19628
19629   /* Look for the string-literal.  */
19630   asm_specification = cp_parser_string_literal (parser, false, false);
19631
19632   /* Look for the `)'.  */
19633   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19634
19635   return asm_specification;
19636 }
19637
19638 /* Parse an asm-operand-list.
19639
19640    asm-operand-list:
19641      asm-operand
19642      asm-operand-list , asm-operand
19643
19644    asm-operand:
19645      string-literal ( expression )
19646      [ string-literal ] string-literal ( expression )
19647
19648    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19649    each node is the expression.  The TREE_PURPOSE is itself a
19650    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19651    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19652    is a STRING_CST for the string literal before the parenthesis. Returns
19653    ERROR_MARK_NODE if any of the operands are invalid.  */
19654
19655 static tree
19656 cp_parser_asm_operand_list (cp_parser* parser)
19657 {
19658   tree asm_operands = NULL_TREE;
19659   bool invalid_operands = false;
19660
19661   while (true)
19662     {
19663       tree string_literal;
19664       tree expression;
19665       tree name;
19666
19667       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19668         {
19669           /* Consume the `[' token.  */
19670           cp_lexer_consume_token (parser->lexer);
19671           /* Read the operand name.  */
19672           name = cp_parser_identifier (parser);
19673           if (name != error_mark_node)
19674             name = build_string (IDENTIFIER_LENGTH (name),
19675                                  IDENTIFIER_POINTER (name));
19676           /* Look for the closing `]'.  */
19677           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19678         }
19679       else
19680         name = NULL_TREE;
19681       /* Look for the string-literal.  */
19682       string_literal = cp_parser_string_literal (parser, false, false);
19683
19684       /* Look for the `('.  */
19685       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19686       /* Parse the expression.  */
19687       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19688       /* Look for the `)'.  */
19689       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19690
19691       if (name == error_mark_node 
19692           || string_literal == error_mark_node 
19693           || expression == error_mark_node)
19694         invalid_operands = true;
19695
19696       /* Add this operand to the list.  */
19697       asm_operands = tree_cons (build_tree_list (name, string_literal),
19698                                 expression,
19699                                 asm_operands);
19700       /* If the next token is not a `,', there are no more
19701          operands.  */
19702       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19703         break;
19704       /* Consume the `,'.  */
19705       cp_lexer_consume_token (parser->lexer);
19706     }
19707
19708   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19709 }
19710
19711 /* Parse an asm-clobber-list.
19712
19713    asm-clobber-list:
19714      string-literal
19715      asm-clobber-list , string-literal
19716
19717    Returns a TREE_LIST, indicating the clobbers in the order that they
19718    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19719
19720 static tree
19721 cp_parser_asm_clobber_list (cp_parser* parser)
19722 {
19723   tree clobbers = NULL_TREE;
19724
19725   while (true)
19726     {
19727       tree string_literal;
19728
19729       /* Look for the string literal.  */
19730       string_literal = cp_parser_string_literal (parser, false, false);
19731       /* Add it to the list.  */
19732       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19733       /* If the next token is not a `,', then the list is
19734          complete.  */
19735       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19736         break;
19737       /* Consume the `,' token.  */
19738       cp_lexer_consume_token (parser->lexer);
19739     }
19740
19741   return clobbers;
19742 }
19743
19744 /* Parse an asm-label-list.
19745
19746    asm-label-list:
19747      identifier
19748      asm-label-list , identifier
19749
19750    Returns a TREE_LIST, indicating the labels in the order that they
19751    appeared.  The TREE_VALUE of each node is a label.  */
19752
19753 static tree
19754 cp_parser_asm_label_list (cp_parser* parser)
19755 {
19756   tree labels = NULL_TREE;
19757
19758   while (true)
19759     {
19760       tree identifier, label, name;
19761
19762       /* Look for the identifier.  */
19763       identifier = cp_parser_identifier (parser);
19764       if (!error_operand_p (identifier))
19765         {
19766           label = lookup_label (identifier);
19767           if (TREE_CODE (label) == LABEL_DECL)
19768             {
19769               TREE_USED (label) = 1;
19770               check_goto (label);
19771               name = build_string (IDENTIFIER_LENGTH (identifier),
19772                                    IDENTIFIER_POINTER (identifier));
19773               labels = tree_cons (name, label, labels);
19774             }
19775         }
19776       /* If the next token is not a `,', then the list is
19777          complete.  */
19778       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19779         break;
19780       /* Consume the `,' token.  */
19781       cp_lexer_consume_token (parser->lexer);
19782     }
19783
19784   return nreverse (labels);
19785 }
19786
19787 /* Parse an (optional) series of attributes.
19788
19789    attributes:
19790      attributes attribute
19791
19792    attribute:
19793      __attribute__ (( attribute-list [opt] ))
19794
19795    The return value is as for cp_parser_attribute_list.  */
19796
19797 static tree
19798 cp_parser_attributes_opt (cp_parser* parser)
19799 {
19800   tree attributes = NULL_TREE;
19801
19802   while (true)
19803     {
19804       cp_token *token;
19805       tree attribute_list;
19806
19807       /* Peek at the next token.  */
19808       token = cp_lexer_peek_token (parser->lexer);
19809       /* If it's not `__attribute__', then we're done.  */
19810       if (token->keyword != RID_ATTRIBUTE)
19811         break;
19812
19813       /* Consume the `__attribute__' keyword.  */
19814       cp_lexer_consume_token (parser->lexer);
19815       /* Look for the two `(' tokens.  */
19816       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19817       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19818
19819       /* Peek at the next token.  */
19820       token = cp_lexer_peek_token (parser->lexer);
19821       if (token->type != CPP_CLOSE_PAREN)
19822         /* Parse the attribute-list.  */
19823         attribute_list = cp_parser_attribute_list (parser);
19824       else
19825         /* If the next token is a `)', then there is no attribute
19826            list.  */
19827         attribute_list = NULL;
19828
19829       /* Look for the two `)' tokens.  */
19830       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19831       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19832
19833       /* Add these new attributes to the list.  */
19834       attributes = chainon (attributes, attribute_list);
19835     }
19836
19837   return attributes;
19838 }
19839
19840 /* Parse an attribute-list.
19841
19842    attribute-list:
19843      attribute
19844      attribute-list , attribute
19845
19846    attribute:
19847      identifier
19848      identifier ( identifier )
19849      identifier ( identifier , expression-list )
19850      identifier ( expression-list )
19851
19852    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
19853    to an attribute.  The TREE_PURPOSE of each node is the identifier
19854    indicating which attribute is in use.  The TREE_VALUE represents
19855    the arguments, if any.  */
19856
19857 static tree
19858 cp_parser_attribute_list (cp_parser* parser)
19859 {
19860   tree attribute_list = NULL_TREE;
19861   bool save_translate_strings_p = parser->translate_strings_p;
19862
19863   parser->translate_strings_p = false;
19864   while (true)
19865     {
19866       cp_token *token;
19867       tree identifier;
19868       tree attribute;
19869
19870       /* Look for the identifier.  We also allow keywords here; for
19871          example `__attribute__ ((const))' is legal.  */
19872       token = cp_lexer_peek_token (parser->lexer);
19873       if (token->type == CPP_NAME
19874           || token->type == CPP_KEYWORD)
19875         {
19876           tree arguments = NULL_TREE;
19877
19878           /* Consume the token.  */
19879           token = cp_lexer_consume_token (parser->lexer);
19880
19881           /* Save away the identifier that indicates which attribute
19882              this is.  */
19883           identifier = (token->type == CPP_KEYWORD) 
19884             /* For keywords, use the canonical spelling, not the
19885                parsed identifier.  */
19886             ? ridpointers[(int) token->keyword]
19887             : token->u.value;
19888           
19889           attribute = build_tree_list (identifier, NULL_TREE);
19890
19891           /* Peek at the next token.  */
19892           token = cp_lexer_peek_token (parser->lexer);
19893           /* If it's an `(', then parse the attribute arguments.  */
19894           if (token->type == CPP_OPEN_PAREN)
19895             {
19896               VEC(tree,gc) *vec;
19897               int attr_flag = (attribute_takes_identifier_p (identifier)
19898                                ? id_attr : normal_attr);
19899               vec = cp_parser_parenthesized_expression_list
19900                     (parser, attr_flag, /*cast_p=*/false,
19901                      /*allow_expansion_p=*/false,
19902                      /*non_constant_p=*/NULL);
19903               if (vec == NULL)
19904                 arguments = error_mark_node;
19905               else
19906                 {
19907                   arguments = build_tree_list_vec (vec);
19908                   release_tree_vector (vec);
19909                 }
19910               /* Save the arguments away.  */
19911               TREE_VALUE (attribute) = arguments;
19912             }
19913
19914           if (arguments != error_mark_node)
19915             {
19916               /* Add this attribute to the list.  */
19917               TREE_CHAIN (attribute) = attribute_list;
19918               attribute_list = attribute;
19919             }
19920
19921           token = cp_lexer_peek_token (parser->lexer);
19922         }
19923       /* Now, look for more attributes.  If the next token isn't a
19924          `,', we're done.  */
19925       if (token->type != CPP_COMMA)
19926         break;
19927
19928       /* Consume the comma and keep going.  */
19929       cp_lexer_consume_token (parser->lexer);
19930     }
19931   parser->translate_strings_p = save_translate_strings_p;
19932
19933   /* We built up the list in reverse order.  */
19934   return nreverse (attribute_list);
19935 }
19936
19937 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19938    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19939    current value of the PEDANTIC flag, regardless of whether or not
19940    the `__extension__' keyword is present.  The caller is responsible
19941    for restoring the value of the PEDANTIC flag.  */
19942
19943 static bool
19944 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19945 {
19946   /* Save the old value of the PEDANTIC flag.  */
19947   *saved_pedantic = pedantic;
19948
19949   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19950     {
19951       /* Consume the `__extension__' token.  */
19952       cp_lexer_consume_token (parser->lexer);
19953       /* We're not being pedantic while the `__extension__' keyword is
19954          in effect.  */
19955       pedantic = 0;
19956
19957       return true;
19958     }
19959
19960   return false;
19961 }
19962
19963 /* Parse a label declaration.
19964
19965    label-declaration:
19966      __label__ label-declarator-seq ;
19967
19968    label-declarator-seq:
19969      identifier , label-declarator-seq
19970      identifier  */
19971
19972 static void
19973 cp_parser_label_declaration (cp_parser* parser)
19974 {
19975   /* Look for the `__label__' keyword.  */
19976   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19977
19978   while (true)
19979     {
19980       tree identifier;
19981
19982       /* Look for an identifier.  */
19983       identifier = cp_parser_identifier (parser);
19984       /* If we failed, stop.  */
19985       if (identifier == error_mark_node)
19986         break;
19987       /* Declare it as a label.  */
19988       finish_label_decl (identifier);
19989       /* If the next token is a `;', stop.  */
19990       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19991         break;
19992       /* Look for the `,' separating the label declarations.  */
19993       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19994     }
19995
19996   /* Look for the final `;'.  */
19997   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19998 }
19999
20000 /* Support Functions */
20001
20002 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20003    NAME should have one of the representations used for an
20004    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20005    is returned.  If PARSER->SCOPE is a dependent type, then a
20006    SCOPE_REF is returned.
20007
20008    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20009    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20010    was formed.  Abstractly, such entities should not be passed to this
20011    function, because they do not need to be looked up, but it is
20012    simpler to check for this special case here, rather than at the
20013    call-sites.
20014
20015    In cases not explicitly covered above, this function returns a
20016    DECL, OVERLOAD, or baselink representing the result of the lookup.
20017    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20018    is returned.
20019
20020    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20021    (e.g., "struct") that was used.  In that case bindings that do not
20022    refer to types are ignored.
20023
20024    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20025    ignored.
20026
20027    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20028    are ignored.
20029
20030    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20031    types.
20032
20033    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20034    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20035    NULL_TREE otherwise.  */
20036
20037 static tree
20038 cp_parser_lookup_name (cp_parser *parser, tree name,
20039                        enum tag_types tag_type,
20040                        bool is_template,
20041                        bool is_namespace,
20042                        bool check_dependency,
20043                        tree *ambiguous_decls,
20044                        location_t name_location)
20045 {
20046   int flags = 0;
20047   tree decl;
20048   tree object_type = parser->context->object_type;
20049
20050   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20051     flags |= LOOKUP_COMPLAIN;
20052
20053   /* Assume that the lookup will be unambiguous.  */
20054   if (ambiguous_decls)
20055     *ambiguous_decls = NULL_TREE;
20056
20057   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20058      no longer valid.  Note that if we are parsing tentatively, and
20059      the parse fails, OBJECT_TYPE will be automatically restored.  */
20060   parser->context->object_type = NULL_TREE;
20061
20062   if (name == error_mark_node)
20063     return error_mark_node;
20064
20065   /* A template-id has already been resolved; there is no lookup to
20066      do.  */
20067   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20068     return name;
20069   if (BASELINK_P (name))
20070     {
20071       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20072                   == TEMPLATE_ID_EXPR);
20073       return name;
20074     }
20075
20076   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20077      it should already have been checked to make sure that the name
20078      used matches the type being destroyed.  */
20079   if (TREE_CODE (name) == BIT_NOT_EXPR)
20080     {
20081       tree type;
20082
20083       /* Figure out to which type this destructor applies.  */
20084       if (parser->scope)
20085         type = parser->scope;
20086       else if (object_type)
20087         type = object_type;
20088       else
20089         type = current_class_type;
20090       /* If that's not a class type, there is no destructor.  */
20091       if (!type || !CLASS_TYPE_P (type))
20092         return error_mark_node;
20093       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20094         lazily_declare_fn (sfk_destructor, type);
20095       if (!CLASSTYPE_DESTRUCTORS (type))
20096           return error_mark_node;
20097       /* If it was a class type, return the destructor.  */
20098       return CLASSTYPE_DESTRUCTORS (type);
20099     }
20100
20101   /* By this point, the NAME should be an ordinary identifier.  If
20102      the id-expression was a qualified name, the qualifying scope is
20103      stored in PARSER->SCOPE at this point.  */
20104   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20105
20106   /* Perform the lookup.  */
20107   if (parser->scope)
20108     {
20109       bool dependent_p;
20110
20111       if (parser->scope == error_mark_node)
20112         return error_mark_node;
20113
20114       /* If the SCOPE is dependent, the lookup must be deferred until
20115          the template is instantiated -- unless we are explicitly
20116          looking up names in uninstantiated templates.  Even then, we
20117          cannot look up the name if the scope is not a class type; it
20118          might, for example, be a template type parameter.  */
20119       dependent_p = (TYPE_P (parser->scope)
20120                      && dependent_scope_p (parser->scope));
20121       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20122           && dependent_p)
20123         /* Defer lookup.  */
20124         decl = error_mark_node;
20125       else
20126         {
20127           tree pushed_scope = NULL_TREE;
20128
20129           /* If PARSER->SCOPE is a dependent type, then it must be a
20130              class type, and we must not be checking dependencies;
20131              otherwise, we would have processed this lookup above.  So
20132              that PARSER->SCOPE is not considered a dependent base by
20133              lookup_member, we must enter the scope here.  */
20134           if (dependent_p)
20135             pushed_scope = push_scope (parser->scope);
20136
20137           /* If the PARSER->SCOPE is a template specialization, it
20138              may be instantiated during name lookup.  In that case,
20139              errors may be issued.  Even if we rollback the current
20140              tentative parse, those errors are valid.  */
20141           decl = lookup_qualified_name (parser->scope, name,
20142                                         tag_type != none_type,
20143                                         /*complain=*/true);
20144
20145           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20146              lookup result and the nested-name-specifier nominates a class C:
20147                * if the name specified after the nested-name-specifier, when
20148                looked up in C, is the injected-class-name of C (Clause 9), or
20149                * if the name specified after the nested-name-specifier is the
20150                same as the identifier or the simple-template-id's template-
20151                name in the last component of the nested-name-specifier,
20152              the name is instead considered to name the constructor of
20153              class C. [ Note: for example, the constructor is not an
20154              acceptable lookup result in an elaborated-type-specifier so
20155              the constructor would not be used in place of the
20156              injected-class-name. --end note ] Such a constructor name
20157              shall be used only in the declarator-id of a declaration that
20158              names a constructor or in a using-declaration.  */
20159           if (tag_type == none_type
20160               && DECL_SELF_REFERENCE_P (decl)
20161               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20162             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20163                                           tag_type != none_type,
20164                                           /*complain=*/true);
20165
20166           /* If we have a single function from a using decl, pull it out.  */
20167           if (TREE_CODE (decl) == OVERLOAD
20168               && !really_overloaded_fn (decl))
20169             decl = OVL_FUNCTION (decl);
20170
20171           if (pushed_scope)
20172             pop_scope (pushed_scope);
20173         }
20174
20175       /* If the scope is a dependent type and either we deferred lookup or
20176          we did lookup but didn't find the name, rememeber the name.  */
20177       if (decl == error_mark_node && TYPE_P (parser->scope)
20178           && dependent_type_p (parser->scope))
20179         {
20180           if (tag_type)
20181             {
20182               tree type;
20183
20184               /* The resolution to Core Issue 180 says that `struct
20185                  A::B' should be considered a type-name, even if `A'
20186                  is dependent.  */
20187               type = make_typename_type (parser->scope, name, tag_type,
20188                                          /*complain=*/tf_error);
20189               decl = TYPE_NAME (type);
20190             }
20191           else if (is_template
20192                    && (cp_parser_next_token_ends_template_argument_p (parser)
20193                        || cp_lexer_next_token_is (parser->lexer,
20194                                                   CPP_CLOSE_PAREN)))
20195             decl = make_unbound_class_template (parser->scope,
20196                                                 name, NULL_TREE,
20197                                                 /*complain=*/tf_error);
20198           else
20199             decl = build_qualified_name (/*type=*/NULL_TREE,
20200                                          parser->scope, name,
20201                                          is_template);
20202         }
20203       parser->qualifying_scope = parser->scope;
20204       parser->object_scope = NULL_TREE;
20205     }
20206   else if (object_type)
20207     {
20208       tree object_decl = NULL_TREE;
20209       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20210          OBJECT_TYPE is not a class.  */
20211       if (CLASS_TYPE_P (object_type))
20212         /* If the OBJECT_TYPE is a template specialization, it may
20213            be instantiated during name lookup.  In that case, errors
20214            may be issued.  Even if we rollback the current tentative
20215            parse, those errors are valid.  */
20216         object_decl = lookup_member (object_type,
20217                                      name,
20218                                      /*protect=*/0,
20219                                      tag_type != none_type);
20220       /* Look it up in the enclosing context, too.  */
20221       decl = lookup_name_real (name, tag_type != none_type,
20222                                /*nonclass=*/0,
20223                                /*block_p=*/true, is_namespace, flags);
20224       parser->object_scope = object_type;
20225       parser->qualifying_scope = NULL_TREE;
20226       if (object_decl)
20227         decl = object_decl;
20228     }
20229   else
20230     {
20231       decl = lookup_name_real (name, tag_type != none_type,
20232                                /*nonclass=*/0,
20233                                /*block_p=*/true, is_namespace, flags);
20234       parser->qualifying_scope = NULL_TREE;
20235       parser->object_scope = NULL_TREE;
20236     }
20237
20238   /* If the lookup failed, let our caller know.  */
20239   if (!decl || decl == error_mark_node)
20240     return error_mark_node;
20241
20242   /* Pull out the template from an injected-class-name (or multiple).  */
20243   if (is_template)
20244     decl = maybe_get_template_decl_from_type_decl (decl);
20245
20246   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20247   if (TREE_CODE (decl) == TREE_LIST)
20248     {
20249       if (ambiguous_decls)
20250         *ambiguous_decls = decl;
20251       /* The error message we have to print is too complicated for
20252          cp_parser_error, so we incorporate its actions directly.  */
20253       if (!cp_parser_simulate_error (parser))
20254         {
20255           error_at (name_location, "reference to %qD is ambiguous",
20256                     name);
20257           print_candidates (decl);
20258         }
20259       return error_mark_node;
20260     }
20261
20262   gcc_assert (DECL_P (decl)
20263               || TREE_CODE (decl) == OVERLOAD
20264               || TREE_CODE (decl) == SCOPE_REF
20265               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20266               || BASELINK_P (decl));
20267
20268   /* If we have resolved the name of a member declaration, check to
20269      see if the declaration is accessible.  When the name resolves to
20270      set of overloaded functions, accessibility is checked when
20271      overload resolution is done.
20272
20273      During an explicit instantiation, access is not checked at all,
20274      as per [temp.explicit].  */
20275   if (DECL_P (decl))
20276     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20277
20278   maybe_record_typedef_use (decl);
20279
20280   return decl;
20281 }
20282
20283 /* Like cp_parser_lookup_name, but for use in the typical case where
20284    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20285    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20286
20287 static tree
20288 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20289 {
20290   return cp_parser_lookup_name (parser, name,
20291                                 none_type,
20292                                 /*is_template=*/false,
20293                                 /*is_namespace=*/false,
20294                                 /*check_dependency=*/true,
20295                                 /*ambiguous_decls=*/NULL,
20296                                 location);
20297 }
20298
20299 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20300    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20301    true, the DECL indicates the class being defined in a class-head,
20302    or declared in an elaborated-type-specifier.
20303
20304    Otherwise, return DECL.  */
20305
20306 static tree
20307 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20308 {
20309   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20310      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20311
20312        struct A {
20313          template <typename T> struct B;
20314        };
20315
20316        template <typename T> struct A::B {};
20317
20318      Similarly, in an elaborated-type-specifier:
20319
20320        namespace N { struct X{}; }
20321
20322        struct A {
20323          template <typename T> friend struct N::X;
20324        };
20325
20326      However, if the DECL refers to a class type, and we are in
20327      the scope of the class, then the name lookup automatically
20328      finds the TYPE_DECL created by build_self_reference rather
20329      than a TEMPLATE_DECL.  For example, in:
20330
20331        template <class T> struct S {
20332          S s;
20333        };
20334
20335      there is no need to handle such case.  */
20336
20337   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20338     return DECL_TEMPLATE_RESULT (decl);
20339
20340   return decl;
20341 }
20342
20343 /* If too many, or too few, template-parameter lists apply to the
20344    declarator, issue an error message.  Returns TRUE if all went well,
20345    and FALSE otherwise.  */
20346
20347 static bool
20348 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20349                                                 cp_declarator *declarator,
20350                                                 location_t declarator_location)
20351 {
20352   unsigned num_templates;
20353
20354   /* We haven't seen any classes that involve template parameters yet.  */
20355   num_templates = 0;
20356
20357   switch (declarator->kind)
20358     {
20359     case cdk_id:
20360       if (declarator->u.id.qualifying_scope)
20361         {
20362           tree scope;
20363
20364           scope = declarator->u.id.qualifying_scope;
20365
20366           while (scope && CLASS_TYPE_P (scope))
20367             {
20368               /* You're supposed to have one `template <...>'
20369                  for every template class, but you don't need one
20370                  for a full specialization.  For example:
20371
20372                  template <class T> struct S{};
20373                  template <> struct S<int> { void f(); };
20374                  void S<int>::f () {}
20375
20376                  is correct; there shouldn't be a `template <>' for
20377                  the definition of `S<int>::f'.  */
20378               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20379                 /* If SCOPE does not have template information of any
20380                    kind, then it is not a template, nor is it nested
20381                    within a template.  */
20382                 break;
20383               if (explicit_class_specialization_p (scope))
20384                 break;
20385               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20386                 ++num_templates;
20387
20388               scope = TYPE_CONTEXT (scope);
20389             }
20390         }
20391       else if (TREE_CODE (declarator->u.id.unqualified_name)
20392                == TEMPLATE_ID_EXPR)
20393         /* If the DECLARATOR has the form `X<y>' then it uses one
20394            additional level of template parameters.  */
20395         ++num_templates;
20396
20397       return cp_parser_check_template_parameters 
20398         (parser, num_templates, declarator_location, declarator);
20399
20400
20401     case cdk_function:
20402     case cdk_array:
20403     case cdk_pointer:
20404     case cdk_reference:
20405     case cdk_ptrmem:
20406       return (cp_parser_check_declarator_template_parameters
20407               (parser, declarator->declarator, declarator_location));
20408
20409     case cdk_error:
20410       return true;
20411
20412     default:
20413       gcc_unreachable ();
20414     }
20415   return false;
20416 }
20417
20418 /* NUM_TEMPLATES were used in the current declaration.  If that is
20419    invalid, return FALSE and issue an error messages.  Otherwise,
20420    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20421    declarator and we can print more accurate diagnostics.  */
20422
20423 static bool
20424 cp_parser_check_template_parameters (cp_parser* parser,
20425                                      unsigned num_templates,
20426                                      location_t location,
20427                                      cp_declarator *declarator)
20428 {
20429   /* If there are the same number of template classes and parameter
20430      lists, that's OK.  */
20431   if (parser->num_template_parameter_lists == num_templates)
20432     return true;
20433   /* If there are more, but only one more, then we are referring to a
20434      member template.  That's OK too.  */
20435   if (parser->num_template_parameter_lists == num_templates + 1)
20436     return true;
20437   /* If there are more template classes than parameter lists, we have
20438      something like:
20439
20440        template <class T> void S<T>::R<T>::f ();  */
20441   if (parser->num_template_parameter_lists < num_templates)
20442     {
20443       if (declarator && !current_function_decl)
20444         error_at (location, "specializing member %<%T::%E%> "
20445                   "requires %<template<>%> syntax", 
20446                   declarator->u.id.qualifying_scope,
20447                   declarator->u.id.unqualified_name);
20448       else if (declarator)
20449         error_at (location, "invalid declaration of %<%T::%E%>",
20450                   declarator->u.id.qualifying_scope,
20451                   declarator->u.id.unqualified_name);
20452       else 
20453         error_at (location, "too few template-parameter-lists");
20454       return false;
20455     }
20456   /* Otherwise, there are too many template parameter lists.  We have
20457      something like:
20458
20459      template <class T> template <class U> void S::f();  */
20460   error_at (location, "too many template-parameter-lists");
20461   return false;
20462 }
20463
20464 /* Parse an optional `::' token indicating that the following name is
20465    from the global namespace.  If so, PARSER->SCOPE is set to the
20466    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20467    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20468    Returns the new value of PARSER->SCOPE, if the `::' token is
20469    present, and NULL_TREE otherwise.  */
20470
20471 static tree
20472 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20473 {
20474   cp_token *token;
20475
20476   /* Peek at the next token.  */
20477   token = cp_lexer_peek_token (parser->lexer);
20478   /* If we're looking at a `::' token then we're starting from the
20479      global namespace, not our current location.  */
20480   if (token->type == CPP_SCOPE)
20481     {
20482       /* Consume the `::' token.  */
20483       cp_lexer_consume_token (parser->lexer);
20484       /* Set the SCOPE so that we know where to start the lookup.  */
20485       parser->scope = global_namespace;
20486       parser->qualifying_scope = global_namespace;
20487       parser->object_scope = NULL_TREE;
20488
20489       return parser->scope;
20490     }
20491   else if (!current_scope_valid_p)
20492     {
20493       parser->scope = NULL_TREE;
20494       parser->qualifying_scope = NULL_TREE;
20495       parser->object_scope = NULL_TREE;
20496     }
20497
20498   return NULL_TREE;
20499 }
20500
20501 /* Returns TRUE if the upcoming token sequence is the start of a
20502    constructor declarator.  If FRIEND_P is true, the declarator is
20503    preceded by the `friend' specifier.  */
20504
20505 static bool
20506 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20507 {
20508   bool constructor_p;
20509   tree nested_name_specifier;
20510   cp_token *next_token;
20511
20512   /* The common case is that this is not a constructor declarator, so
20513      try to avoid doing lots of work if at all possible.  It's not
20514      valid declare a constructor at function scope.  */
20515   if (parser->in_function_body)
20516     return false;
20517   /* And only certain tokens can begin a constructor declarator.  */
20518   next_token = cp_lexer_peek_token (parser->lexer);
20519   if (next_token->type != CPP_NAME
20520       && next_token->type != CPP_SCOPE
20521       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20522       && next_token->type != CPP_TEMPLATE_ID)
20523     return false;
20524
20525   /* Parse tentatively; we are going to roll back all of the tokens
20526      consumed here.  */
20527   cp_parser_parse_tentatively (parser);
20528   /* Assume that we are looking at a constructor declarator.  */
20529   constructor_p = true;
20530
20531   /* Look for the optional `::' operator.  */
20532   cp_parser_global_scope_opt (parser,
20533                               /*current_scope_valid_p=*/false);
20534   /* Look for the nested-name-specifier.  */
20535   nested_name_specifier
20536     = (cp_parser_nested_name_specifier_opt (parser,
20537                                             /*typename_keyword_p=*/false,
20538                                             /*check_dependency_p=*/false,
20539                                             /*type_p=*/false,
20540                                             /*is_declaration=*/false));
20541   /* Outside of a class-specifier, there must be a
20542      nested-name-specifier.  */
20543   if (!nested_name_specifier &&
20544       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20545        || friend_p))
20546     constructor_p = false;
20547   else if (nested_name_specifier == error_mark_node)
20548     constructor_p = false;
20549
20550   /* If we have a class scope, this is easy; DR 147 says that S::S always
20551      names the constructor, and no other qualified name could.  */
20552   if (constructor_p && nested_name_specifier
20553       && CLASS_TYPE_P (nested_name_specifier))
20554     {
20555       tree id = cp_parser_unqualified_id (parser,
20556                                           /*template_keyword_p=*/false,
20557                                           /*check_dependency_p=*/false,
20558                                           /*declarator_p=*/true,
20559                                           /*optional_p=*/false);
20560       if (is_overloaded_fn (id))
20561         id = DECL_NAME (get_first_fn (id));
20562       if (!constructor_name_p (id, nested_name_specifier))
20563         constructor_p = false;
20564     }
20565   /* If we still think that this might be a constructor-declarator,
20566      look for a class-name.  */
20567   else if (constructor_p)
20568     {
20569       /* If we have:
20570
20571            template <typename T> struct S {
20572              S();
20573            };
20574
20575          we must recognize that the nested `S' names a class.  */
20576       tree type_decl;
20577       type_decl = cp_parser_class_name (parser,
20578                                         /*typename_keyword_p=*/false,
20579                                         /*template_keyword_p=*/false,
20580                                         none_type,
20581                                         /*check_dependency_p=*/false,
20582                                         /*class_head_p=*/false,
20583                                         /*is_declaration=*/false);
20584       /* If there was no class-name, then this is not a constructor.  */
20585       constructor_p = !cp_parser_error_occurred (parser);
20586
20587       /* If we're still considering a constructor, we have to see a `(',
20588          to begin the parameter-declaration-clause, followed by either a
20589          `)', an `...', or a decl-specifier.  We need to check for a
20590          type-specifier to avoid being fooled into thinking that:
20591
20592            S (f) (int);
20593
20594          is a constructor.  (It is actually a function named `f' that
20595          takes one parameter (of type `int') and returns a value of type
20596          `S'.  */
20597       if (constructor_p
20598           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20599         constructor_p = false;
20600
20601       if (constructor_p
20602           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20603           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20604           /* A parameter declaration begins with a decl-specifier,
20605              which is either the "attribute" keyword, a storage class
20606              specifier, or (usually) a type-specifier.  */
20607           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20608         {
20609           tree type;
20610           tree pushed_scope = NULL_TREE;
20611           unsigned saved_num_template_parameter_lists;
20612
20613           /* Names appearing in the type-specifier should be looked up
20614              in the scope of the class.  */
20615           if (current_class_type)
20616             type = NULL_TREE;
20617           else
20618             {
20619               type = TREE_TYPE (type_decl);
20620               if (TREE_CODE (type) == TYPENAME_TYPE)
20621                 {
20622                   type = resolve_typename_type (type,
20623                                                 /*only_current_p=*/false);
20624                   if (TREE_CODE (type) == TYPENAME_TYPE)
20625                     {
20626                       cp_parser_abort_tentative_parse (parser);
20627                       return false;
20628                     }
20629                 }
20630               pushed_scope = push_scope (type);
20631             }
20632
20633           /* Inside the constructor parameter list, surrounding
20634              template-parameter-lists do not apply.  */
20635           saved_num_template_parameter_lists
20636             = parser->num_template_parameter_lists;
20637           parser->num_template_parameter_lists = 0;
20638
20639           /* Look for the type-specifier.  */
20640           cp_parser_type_specifier (parser,
20641                                     CP_PARSER_FLAGS_NONE,
20642                                     /*decl_specs=*/NULL,
20643                                     /*is_declarator=*/true,
20644                                     /*declares_class_or_enum=*/NULL,
20645                                     /*is_cv_qualifier=*/NULL);
20646
20647           parser->num_template_parameter_lists
20648             = saved_num_template_parameter_lists;
20649
20650           /* Leave the scope of the class.  */
20651           if (pushed_scope)
20652             pop_scope (pushed_scope);
20653
20654           constructor_p = !cp_parser_error_occurred (parser);
20655         }
20656     }
20657
20658   /* We did not really want to consume any tokens.  */
20659   cp_parser_abort_tentative_parse (parser);
20660
20661   return constructor_p;
20662 }
20663
20664 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20665    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20666    they must be performed once we are in the scope of the function.
20667
20668    Returns the function defined.  */
20669
20670 static tree
20671 cp_parser_function_definition_from_specifiers_and_declarator
20672   (cp_parser* parser,
20673    cp_decl_specifier_seq *decl_specifiers,
20674    tree attributes,
20675    const cp_declarator *declarator)
20676 {
20677   tree fn;
20678   bool success_p;
20679
20680   /* Begin the function-definition.  */
20681   success_p = start_function (decl_specifiers, declarator, attributes);
20682
20683   /* The things we're about to see are not directly qualified by any
20684      template headers we've seen thus far.  */
20685   reset_specialization ();
20686
20687   /* If there were names looked up in the decl-specifier-seq that we
20688      did not check, check them now.  We must wait until we are in the
20689      scope of the function to perform the checks, since the function
20690      might be a friend.  */
20691   perform_deferred_access_checks ();
20692
20693   if (!success_p)
20694     {
20695       /* Skip the entire function.  */
20696       cp_parser_skip_to_end_of_block_or_statement (parser);
20697       fn = error_mark_node;
20698     }
20699   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20700     {
20701       /* Seen already, skip it.  An error message has already been output.  */
20702       cp_parser_skip_to_end_of_block_or_statement (parser);
20703       fn = current_function_decl;
20704       current_function_decl = NULL_TREE;
20705       /* If this is a function from a class, pop the nested class.  */
20706       if (current_class_name)
20707         pop_nested_class ();
20708     }
20709   else
20710     {
20711       timevar_id_t tv;
20712       if (DECL_DECLARED_INLINE_P (current_function_decl))
20713         tv = TV_PARSE_INLINE;
20714       else
20715         tv = TV_PARSE_FUNC;
20716       timevar_push (tv);
20717       fn = cp_parser_function_definition_after_declarator (parser,
20718                                                          /*inline_p=*/false);
20719       timevar_pop (tv);
20720     }
20721
20722   return fn;
20723 }
20724
20725 /* Parse the part of a function-definition that follows the
20726    declarator.  INLINE_P is TRUE iff this function is an inline
20727    function defined within a class-specifier.
20728
20729    Returns the function defined.  */
20730
20731 static tree
20732 cp_parser_function_definition_after_declarator (cp_parser* parser,
20733                                                 bool inline_p)
20734 {
20735   tree fn;
20736   bool ctor_initializer_p = false;
20737   bool saved_in_unbraced_linkage_specification_p;
20738   bool saved_in_function_body;
20739   unsigned saved_num_template_parameter_lists;
20740   cp_token *token;
20741
20742   saved_in_function_body = parser->in_function_body;
20743   parser->in_function_body = true;
20744   /* If the next token is `return', then the code may be trying to
20745      make use of the "named return value" extension that G++ used to
20746      support.  */
20747   token = cp_lexer_peek_token (parser->lexer);
20748   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20749     {
20750       /* Consume the `return' keyword.  */
20751       cp_lexer_consume_token (parser->lexer);
20752       /* Look for the identifier that indicates what value is to be
20753          returned.  */
20754       cp_parser_identifier (parser);
20755       /* Issue an error message.  */
20756       error_at (token->location,
20757                 "named return values are no longer supported");
20758       /* Skip tokens until we reach the start of the function body.  */
20759       while (true)
20760         {
20761           cp_token *token = cp_lexer_peek_token (parser->lexer);
20762           if (token->type == CPP_OPEN_BRACE
20763               || token->type == CPP_EOF
20764               || token->type == CPP_PRAGMA_EOL)
20765             break;
20766           cp_lexer_consume_token (parser->lexer);
20767         }
20768     }
20769   /* The `extern' in `extern "C" void f () { ... }' does not apply to
20770      anything declared inside `f'.  */
20771   saved_in_unbraced_linkage_specification_p
20772     = parser->in_unbraced_linkage_specification_p;
20773   parser->in_unbraced_linkage_specification_p = false;
20774   /* Inside the function, surrounding template-parameter-lists do not
20775      apply.  */
20776   saved_num_template_parameter_lists
20777     = parser->num_template_parameter_lists;
20778   parser->num_template_parameter_lists = 0;
20779
20780   start_lambda_scope (current_function_decl);
20781
20782   /* If the next token is `try', then we are looking at a
20783      function-try-block.  */
20784   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20785     ctor_initializer_p = cp_parser_function_try_block (parser);
20786   /* A function-try-block includes the function-body, so we only do
20787      this next part if we're not processing a function-try-block.  */
20788   else
20789     ctor_initializer_p
20790       = cp_parser_ctor_initializer_opt_and_function_body (parser);
20791
20792   finish_lambda_scope ();
20793
20794   /* Finish the function.  */
20795   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20796                         (inline_p ? 2 : 0));
20797   /* Generate code for it, if necessary.  */
20798   expand_or_defer_fn (fn);
20799   /* Restore the saved values.  */
20800   parser->in_unbraced_linkage_specification_p
20801     = saved_in_unbraced_linkage_specification_p;
20802   parser->num_template_parameter_lists
20803     = saved_num_template_parameter_lists;
20804   parser->in_function_body = saved_in_function_body;
20805
20806   return fn;
20807 }
20808
20809 /* Parse a template-declaration, assuming that the `export' (and
20810    `extern') keywords, if present, has already been scanned.  MEMBER_P
20811    is as for cp_parser_template_declaration.  */
20812
20813 static void
20814 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20815 {
20816   tree decl = NULL_TREE;
20817   VEC (deferred_access_check,gc) *checks;
20818   tree parameter_list;
20819   bool friend_p = false;
20820   bool need_lang_pop;
20821   cp_token *token;
20822
20823   /* Look for the `template' keyword.  */
20824   token = cp_lexer_peek_token (parser->lexer);
20825   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20826     return;
20827
20828   /* And the `<'.  */
20829   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20830     return;
20831   if (at_class_scope_p () && current_function_decl)
20832     {
20833       /* 14.5.2.2 [temp.mem]
20834
20835          A local class shall not have member templates.  */
20836       error_at (token->location,
20837                 "invalid declaration of member template in local class");
20838       cp_parser_skip_to_end_of_block_or_statement (parser);
20839       return;
20840     }
20841   /* [temp]
20842
20843      A template ... shall not have C linkage.  */
20844   if (current_lang_name == lang_name_c)
20845     {
20846       error_at (token->location, "template with C linkage");
20847       /* Give it C++ linkage to avoid confusing other parts of the
20848          front end.  */
20849       push_lang_context (lang_name_cplusplus);
20850       need_lang_pop = true;
20851     }
20852   else
20853     need_lang_pop = false;
20854
20855   /* We cannot perform access checks on the template parameter
20856      declarations until we know what is being declared, just as we
20857      cannot check the decl-specifier list.  */
20858   push_deferring_access_checks (dk_deferred);
20859
20860   /* If the next token is `>', then we have an invalid
20861      specialization.  Rather than complain about an invalid template
20862      parameter, issue an error message here.  */
20863   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20864     {
20865       cp_parser_error (parser, "invalid explicit specialization");
20866       begin_specialization ();
20867       parameter_list = NULL_TREE;
20868     }
20869   else
20870     {
20871       /* Parse the template parameters.  */
20872       parameter_list = cp_parser_template_parameter_list (parser);
20873       fixup_template_parms ();
20874     }
20875
20876   /* Get the deferred access checks from the parameter list.  These
20877      will be checked once we know what is being declared, as for a
20878      member template the checks must be performed in the scope of the
20879      class containing the member.  */
20880   checks = get_deferred_access_checks ();
20881
20882   /* Look for the `>'.  */
20883   cp_parser_skip_to_end_of_template_parameter_list (parser);
20884   /* We just processed one more parameter list.  */
20885   ++parser->num_template_parameter_lists;
20886   /* If the next token is `template', there are more template
20887      parameters.  */
20888   if (cp_lexer_next_token_is_keyword (parser->lexer,
20889                                       RID_TEMPLATE))
20890     cp_parser_template_declaration_after_export (parser, member_p);
20891   else
20892     {
20893       /* There are no access checks when parsing a template, as we do not
20894          know if a specialization will be a friend.  */
20895       push_deferring_access_checks (dk_no_check);
20896       token = cp_lexer_peek_token (parser->lexer);
20897       decl = cp_parser_single_declaration (parser,
20898                                            checks,
20899                                            member_p,
20900                                            /*explicit_specialization_p=*/false,
20901                                            &friend_p);
20902       pop_deferring_access_checks ();
20903
20904       /* If this is a member template declaration, let the front
20905          end know.  */
20906       if (member_p && !friend_p && decl)
20907         {
20908           if (TREE_CODE (decl) == TYPE_DECL)
20909             cp_parser_check_access_in_redeclaration (decl, token->location);
20910
20911           decl = finish_member_template_decl (decl);
20912         }
20913       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20914         make_friend_class (current_class_type, TREE_TYPE (decl),
20915                            /*complain=*/true);
20916     }
20917   /* We are done with the current parameter list.  */
20918   --parser->num_template_parameter_lists;
20919
20920   pop_deferring_access_checks ();
20921
20922   /* Finish up.  */
20923   finish_template_decl (parameter_list);
20924
20925   /* Check the template arguments for a literal operator template.  */
20926   if (decl
20927       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
20928       && UDLIT_OPER_P (DECL_NAME (decl)))
20929     {
20930       bool ok = true;
20931       if (parameter_list == NULL_TREE)
20932         ok = false;
20933       else
20934         {
20935           int num_parms = TREE_VEC_LENGTH (parameter_list);
20936           if (num_parms != 1)
20937             ok = false;
20938           else
20939             {
20940               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
20941               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
20942               if (TREE_TYPE (parm) != char_type_node
20943                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
20944                 ok = false;
20945             }
20946         }
20947       if (!ok)
20948         error ("literal operator template %qD has invalid parameter list."
20949                "  Expected non-type template argument pack <char...>",
20950                decl);
20951     }
20952   /* Register member declarations.  */
20953   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20954     finish_member_declaration (decl);
20955   /* For the erroneous case of a template with C linkage, we pushed an
20956      implicit C++ linkage scope; exit that scope now.  */
20957   if (need_lang_pop)
20958     pop_lang_context ();
20959   /* If DECL is a function template, we must return to parse it later.
20960      (Even though there is no definition, there might be default
20961      arguments that need handling.)  */
20962   if (member_p && decl
20963       && (TREE_CODE (decl) == FUNCTION_DECL
20964           || DECL_FUNCTION_TEMPLATE_P (decl)))
20965     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20966 }
20967
20968 /* Perform the deferred access checks from a template-parameter-list.
20969    CHECKS is a TREE_LIST of access checks, as returned by
20970    get_deferred_access_checks.  */
20971
20972 static void
20973 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20974 {
20975   ++processing_template_parmlist;
20976   perform_access_checks (checks);
20977   --processing_template_parmlist;
20978 }
20979
20980 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20981    `function-definition' sequence.  MEMBER_P is true, this declaration
20982    appears in a class scope.
20983
20984    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20985    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20986
20987 static tree
20988 cp_parser_single_declaration (cp_parser* parser,
20989                               VEC (deferred_access_check,gc)* checks,
20990                               bool member_p,
20991                               bool explicit_specialization_p,
20992                               bool* friend_p)
20993 {
20994   int declares_class_or_enum;
20995   tree decl = NULL_TREE;
20996   cp_decl_specifier_seq decl_specifiers;
20997   bool function_definition_p = false;
20998   cp_token *decl_spec_token_start;
20999
21000   /* This function is only used when processing a template
21001      declaration.  */
21002   gcc_assert (innermost_scope_kind () == sk_template_parms
21003               || innermost_scope_kind () == sk_template_spec);
21004
21005   /* Defer access checks until we know what is being declared.  */
21006   push_deferring_access_checks (dk_deferred);
21007
21008   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21009      alternative.  */
21010   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21011   cp_parser_decl_specifier_seq (parser,
21012                                 CP_PARSER_FLAGS_OPTIONAL,
21013                                 &decl_specifiers,
21014                                 &declares_class_or_enum);
21015   if (friend_p)
21016     *friend_p = cp_parser_friend_p (&decl_specifiers);
21017
21018   /* There are no template typedefs.  */
21019   if (decl_specifiers.specs[(int) ds_typedef])
21020     {
21021       error_at (decl_spec_token_start->location,
21022                 "template declaration of %<typedef%>");
21023       decl = error_mark_node;
21024     }
21025
21026   /* Gather up the access checks that occurred the
21027      decl-specifier-seq.  */
21028   stop_deferring_access_checks ();
21029
21030   /* Check for the declaration of a template class.  */
21031   if (declares_class_or_enum)
21032     {
21033       if (cp_parser_declares_only_class_p (parser))
21034         {
21035           decl = shadow_tag (&decl_specifiers);
21036
21037           /* In this case:
21038
21039                struct C {
21040                  friend template <typename T> struct A<T>::B;
21041                };
21042
21043              A<T>::B will be represented by a TYPENAME_TYPE, and
21044              therefore not recognized by shadow_tag.  */
21045           if (friend_p && *friend_p
21046               && !decl
21047               && decl_specifiers.type
21048               && TYPE_P (decl_specifiers.type))
21049             decl = decl_specifiers.type;
21050
21051           if (decl && decl != error_mark_node)
21052             decl = TYPE_NAME (decl);
21053           else
21054             decl = error_mark_node;
21055
21056           /* Perform access checks for template parameters.  */
21057           cp_parser_perform_template_parameter_access_checks (checks);
21058         }
21059     }
21060
21061   /* Complain about missing 'typename' or other invalid type names.  */
21062   if (!decl_specifiers.any_type_specifiers_p
21063       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21064     {
21065       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21066          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21067          the rest of this declaration.  */
21068       decl = error_mark_node;
21069       goto out;
21070     }
21071
21072   /* If it's not a template class, try for a template function.  If
21073      the next token is a `;', then this declaration does not declare
21074      anything.  But, if there were errors in the decl-specifiers, then
21075      the error might well have come from an attempted class-specifier.
21076      In that case, there's no need to warn about a missing declarator.  */
21077   if (!decl
21078       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21079           || decl_specifiers.type != error_mark_node))
21080     {
21081       decl = cp_parser_init_declarator (parser,
21082                                         &decl_specifiers,
21083                                         checks,
21084                                         /*function_definition_allowed_p=*/true,
21085                                         member_p,
21086                                         declares_class_or_enum,
21087                                         &function_definition_p,
21088                                         NULL);
21089
21090     /* 7.1.1-1 [dcl.stc]
21091
21092        A storage-class-specifier shall not be specified in an explicit
21093        specialization...  */
21094     if (decl
21095         && explicit_specialization_p
21096         && decl_specifiers.storage_class != sc_none)
21097       {
21098         error_at (decl_spec_token_start->location,
21099                   "explicit template specialization cannot have a storage class");
21100         decl = error_mark_node;
21101       }
21102     }
21103
21104   /* Look for a trailing `;' after the declaration.  */
21105   if (!function_definition_p
21106       && (decl == error_mark_node
21107           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21108     cp_parser_skip_to_end_of_block_or_statement (parser);
21109
21110  out:
21111   pop_deferring_access_checks ();
21112
21113   /* Clear any current qualification; whatever comes next is the start
21114      of something new.  */
21115   parser->scope = NULL_TREE;
21116   parser->qualifying_scope = NULL_TREE;
21117   parser->object_scope = NULL_TREE;
21118
21119   return decl;
21120 }
21121
21122 /* Parse a cast-expression that is not the operand of a unary "&".  */
21123
21124 static tree
21125 cp_parser_simple_cast_expression (cp_parser *parser)
21126 {
21127   return cp_parser_cast_expression (parser, /*address_p=*/false,
21128                                     /*cast_p=*/false, NULL);
21129 }
21130
21131 /* Parse a functional cast to TYPE.  Returns an expression
21132    representing the cast.  */
21133
21134 static tree
21135 cp_parser_functional_cast (cp_parser* parser, tree type)
21136 {
21137   VEC(tree,gc) *vec;
21138   tree expression_list;
21139   tree cast;
21140   bool nonconst_p;
21141
21142   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21143     {
21144       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21145       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21146       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21147       if (TREE_CODE (type) == TYPE_DECL)
21148         type = TREE_TYPE (type);
21149       return finish_compound_literal (type, expression_list,
21150                                       tf_warning_or_error);
21151     }
21152
21153
21154   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21155                                                  /*cast_p=*/true,
21156                                                  /*allow_expansion_p=*/true,
21157                                                  /*non_constant_p=*/NULL);
21158   if (vec == NULL)
21159     expression_list = error_mark_node;
21160   else
21161     {
21162       expression_list = build_tree_list_vec (vec);
21163       release_tree_vector (vec);
21164     }
21165
21166   cast = build_functional_cast (type, expression_list,
21167                                 tf_warning_or_error);
21168   /* [expr.const]/1: In an integral constant expression "only type
21169      conversions to integral or enumeration type can be used".  */
21170   if (TREE_CODE (type) == TYPE_DECL)
21171     type = TREE_TYPE (type);
21172   if (cast != error_mark_node
21173       && !cast_valid_in_integral_constant_expression_p (type)
21174       && cp_parser_non_integral_constant_expression (parser,
21175                                                      NIC_CONSTRUCTOR))
21176     return error_mark_node;
21177   return cast;
21178 }
21179
21180 /* Save the tokens that make up the body of a member function defined
21181    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21182    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21183    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21184    for the member function.  */
21185
21186 static tree
21187 cp_parser_save_member_function_body (cp_parser* parser,
21188                                      cp_decl_specifier_seq *decl_specifiers,
21189                                      cp_declarator *declarator,
21190                                      tree attributes)
21191 {
21192   cp_token *first;
21193   cp_token *last;
21194   tree fn;
21195
21196   /* Create the FUNCTION_DECL.  */
21197   fn = grokmethod (decl_specifiers, declarator, attributes);
21198   /* If something went badly wrong, bail out now.  */
21199   if (fn == error_mark_node)
21200     {
21201       /* If there's a function-body, skip it.  */
21202       if (cp_parser_token_starts_function_definition_p
21203           (cp_lexer_peek_token (parser->lexer)))
21204         cp_parser_skip_to_end_of_block_or_statement (parser);
21205       return error_mark_node;
21206     }
21207
21208   /* Remember it, if there default args to post process.  */
21209   cp_parser_save_default_args (parser, fn);
21210
21211   /* Save away the tokens that make up the body of the
21212      function.  */
21213   first = parser->lexer->next_token;
21214   /* We can have braced-init-list mem-initializers before the fn body.  */
21215   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21216     {
21217       cp_lexer_consume_token (parser->lexer);
21218       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21219              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21220         {
21221           /* cache_group will stop after an un-nested { } pair, too.  */
21222           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21223             break;
21224
21225           /* variadic mem-inits have ... after the ')'.  */
21226           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21227             cp_lexer_consume_token (parser->lexer);
21228         }
21229     }
21230   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21231   /* Handle function try blocks.  */
21232   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21233     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21234   last = parser->lexer->next_token;
21235
21236   /* Save away the inline definition; we will process it when the
21237      class is complete.  */
21238   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21239   DECL_PENDING_INLINE_P (fn) = 1;
21240
21241   /* We need to know that this was defined in the class, so that
21242      friend templates are handled correctly.  */
21243   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21244
21245   /* Add FN to the queue of functions to be parsed later.  */
21246   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21247
21248   return fn;
21249 }
21250
21251 /* Save the tokens that make up the in-class initializer for a non-static
21252    data member.  Returns a DEFAULT_ARG.  */
21253
21254 static tree
21255 cp_parser_save_nsdmi (cp_parser* parser)
21256 {
21257   /* Save away the tokens that make up the body of the
21258      function.  */
21259   cp_token *first = parser->lexer->next_token;
21260   cp_token *last;
21261   tree node;
21262
21263   /* Save tokens until the next comma or semicolon.  */
21264   cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21265
21266   last = parser->lexer->next_token;
21267
21268   node = make_node (DEFAULT_ARG);
21269   DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21270   DEFARG_INSTANTIATIONS (node) = NULL;
21271
21272   return node;
21273 }
21274
21275
21276 /* Parse a template-argument-list, as well as the trailing ">" (but
21277    not the opening "<").  See cp_parser_template_argument_list for the
21278    return value.  */
21279
21280 static tree
21281 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21282 {
21283   tree arguments;
21284   tree saved_scope;
21285   tree saved_qualifying_scope;
21286   tree saved_object_scope;
21287   bool saved_greater_than_is_operator_p;
21288   int saved_unevaluated_operand;
21289   int saved_inhibit_evaluation_warnings;
21290
21291   /* [temp.names]
21292
21293      When parsing a template-id, the first non-nested `>' is taken as
21294      the end of the template-argument-list rather than a greater-than
21295      operator.  */
21296   saved_greater_than_is_operator_p
21297     = parser->greater_than_is_operator_p;
21298   parser->greater_than_is_operator_p = false;
21299   /* Parsing the argument list may modify SCOPE, so we save it
21300      here.  */
21301   saved_scope = parser->scope;
21302   saved_qualifying_scope = parser->qualifying_scope;
21303   saved_object_scope = parser->object_scope;
21304   /* We need to evaluate the template arguments, even though this
21305      template-id may be nested within a "sizeof".  */
21306   saved_unevaluated_operand = cp_unevaluated_operand;
21307   cp_unevaluated_operand = 0;
21308   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21309   c_inhibit_evaluation_warnings = 0;
21310   /* Parse the template-argument-list itself.  */
21311   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21312       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21313     arguments = NULL_TREE;
21314   else
21315     arguments = cp_parser_template_argument_list (parser);
21316   /* Look for the `>' that ends the template-argument-list. If we find
21317      a '>>' instead, it's probably just a typo.  */
21318   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21319     {
21320       if (cxx_dialect != cxx98)
21321         {
21322           /* In C++0x, a `>>' in a template argument list or cast
21323              expression is considered to be two separate `>'
21324              tokens. So, change the current token to a `>', but don't
21325              consume it: it will be consumed later when the outer
21326              template argument list (or cast expression) is parsed.
21327              Note that this replacement of `>' for `>>' is necessary
21328              even if we are parsing tentatively: in the tentative
21329              case, after calling
21330              cp_parser_enclosed_template_argument_list we will always
21331              throw away all of the template arguments and the first
21332              closing `>', either because the template argument list
21333              was erroneous or because we are replacing those tokens
21334              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21335              not have been thrown away) is needed either to close an
21336              outer template argument list or to complete a new-style
21337              cast.  */
21338           cp_token *token = cp_lexer_peek_token (parser->lexer);
21339           token->type = CPP_GREATER;
21340         }
21341       else if (!saved_greater_than_is_operator_p)
21342         {
21343           /* If we're in a nested template argument list, the '>>' has
21344             to be a typo for '> >'. We emit the error message, but we
21345             continue parsing and we push a '>' as next token, so that
21346             the argument list will be parsed correctly.  Note that the
21347             global source location is still on the token before the
21348             '>>', so we need to say explicitly where we want it.  */
21349           cp_token *token = cp_lexer_peek_token (parser->lexer);
21350           error_at (token->location, "%<>>%> should be %<> >%> "
21351                     "within a nested template argument list");
21352
21353           token->type = CPP_GREATER;
21354         }
21355       else
21356         {
21357           /* If this is not a nested template argument list, the '>>'
21358             is a typo for '>'. Emit an error message and continue.
21359             Same deal about the token location, but here we can get it
21360             right by consuming the '>>' before issuing the diagnostic.  */
21361           cp_token *token = cp_lexer_consume_token (parser->lexer);
21362           error_at (token->location,
21363                     "spurious %<>>%>, use %<>%> to terminate "
21364                     "a template argument list");
21365         }
21366     }
21367   else
21368     cp_parser_skip_to_end_of_template_parameter_list (parser);
21369   /* The `>' token might be a greater-than operator again now.  */
21370   parser->greater_than_is_operator_p
21371     = saved_greater_than_is_operator_p;
21372   /* Restore the SAVED_SCOPE.  */
21373   parser->scope = saved_scope;
21374   parser->qualifying_scope = saved_qualifying_scope;
21375   parser->object_scope = saved_object_scope;
21376   cp_unevaluated_operand = saved_unevaluated_operand;
21377   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21378
21379   return arguments;
21380 }
21381
21382 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21383    arguments, or the body of the function have not yet been parsed,
21384    parse them now.  */
21385
21386 static void
21387 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21388 {
21389   timevar_push (TV_PARSE_INMETH);
21390   /* If this member is a template, get the underlying
21391      FUNCTION_DECL.  */
21392   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21393     member_function = DECL_TEMPLATE_RESULT (member_function);
21394
21395   /* There should not be any class definitions in progress at this
21396      point; the bodies of members are only parsed outside of all class
21397      definitions.  */
21398   gcc_assert (parser->num_classes_being_defined == 0);
21399   /* While we're parsing the member functions we might encounter more
21400      classes.  We want to handle them right away, but we don't want
21401      them getting mixed up with functions that are currently in the
21402      queue.  */
21403   push_unparsed_function_queues (parser);
21404
21405   /* Make sure that any template parameters are in scope.  */
21406   maybe_begin_member_template_processing (member_function);
21407
21408   /* If the body of the function has not yet been parsed, parse it
21409      now.  */
21410   if (DECL_PENDING_INLINE_P (member_function))
21411     {
21412       tree function_scope;
21413       cp_token_cache *tokens;
21414
21415       /* The function is no longer pending; we are processing it.  */
21416       tokens = DECL_PENDING_INLINE_INFO (member_function);
21417       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21418       DECL_PENDING_INLINE_P (member_function) = 0;
21419
21420       /* If this is a local class, enter the scope of the containing
21421          function.  */
21422       function_scope = current_function_decl;
21423       if (function_scope)
21424         push_function_context ();
21425
21426       /* Push the body of the function onto the lexer stack.  */
21427       cp_parser_push_lexer_for_tokens (parser, tokens);
21428
21429       /* Let the front end know that we going to be defining this
21430          function.  */
21431       start_preparsed_function (member_function, NULL_TREE,
21432                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21433
21434       /* Don't do access checking if it is a templated function.  */
21435       if (processing_template_decl)
21436         push_deferring_access_checks (dk_no_check);
21437
21438       /* Now, parse the body of the function.  */
21439       cp_parser_function_definition_after_declarator (parser,
21440                                                       /*inline_p=*/true);
21441
21442       if (processing_template_decl)
21443         pop_deferring_access_checks ();
21444
21445       /* Leave the scope of the containing function.  */
21446       if (function_scope)
21447         pop_function_context ();
21448       cp_parser_pop_lexer (parser);
21449     }
21450
21451   /* Remove any template parameters from the symbol table.  */
21452   maybe_end_member_template_processing ();
21453
21454   /* Restore the queue.  */
21455   pop_unparsed_function_queues (parser);
21456   timevar_pop (TV_PARSE_INMETH);
21457 }
21458
21459 /* If DECL contains any default args, remember it on the unparsed
21460    functions queue.  */
21461
21462 static void
21463 cp_parser_save_default_args (cp_parser* parser, tree decl)
21464 {
21465   tree probe;
21466
21467   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21468        probe;
21469        probe = TREE_CHAIN (probe))
21470     if (TREE_PURPOSE (probe))
21471       {
21472         cp_default_arg_entry *entry
21473           = VEC_safe_push (cp_default_arg_entry, gc,
21474                            unparsed_funs_with_default_args, NULL);
21475         entry->class_type = current_class_type;
21476         entry->decl = decl;
21477         break;
21478       }
21479 }
21480
21481 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21482    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21483    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21484    from the parameter-type-list.  */
21485
21486 static tree
21487 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21488                                       tree default_arg, tree parmtype)
21489 {
21490   cp_token_cache *tokens;
21491   tree parsed_arg;
21492   bool dummy;
21493
21494   /* Push the saved tokens for the default argument onto the parser's
21495      lexer stack.  */
21496   tokens = DEFARG_TOKENS (default_arg);
21497   cp_parser_push_lexer_for_tokens (parser, tokens);
21498
21499   start_lambda_scope (decl);
21500
21501   /* Parse the default argument.  */
21502   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21503   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21504     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21505
21506   finish_lambda_scope ();
21507
21508   if (!processing_template_decl)
21509     {
21510       /* In a non-template class, check conversions now.  In a template,
21511          we'll wait and instantiate these as needed.  */
21512       if (TREE_CODE (decl) == PARM_DECL)
21513         parsed_arg = check_default_argument (parmtype, parsed_arg);
21514       else
21515         {
21516           int flags = LOOKUP_IMPLICIT;
21517           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21518               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21519             flags = LOOKUP_NORMAL;
21520           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21521         }
21522     }
21523
21524   /* If the token stream has not been completely used up, then
21525      there was extra junk after the end of the default
21526      argument.  */
21527   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21528     {
21529       if (TREE_CODE (decl) == PARM_DECL)
21530         cp_parser_error (parser, "expected %<,%>");
21531       else
21532         cp_parser_error (parser, "expected %<;%>");
21533     }
21534
21535   /* Revert to the main lexer.  */
21536   cp_parser_pop_lexer (parser);
21537
21538   return parsed_arg;
21539 }
21540
21541 /* FIELD is a non-static data member with an initializer which we saved for
21542    later; parse it now.  */
21543
21544 static void
21545 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21546 {
21547   tree def;
21548
21549   push_unparsed_function_queues (parser);
21550   def = cp_parser_late_parse_one_default_arg (parser, field,
21551                                               DECL_INITIAL (field),
21552                                               NULL_TREE);
21553   pop_unparsed_function_queues (parser);
21554
21555   DECL_INITIAL (field) = def;
21556 }
21557
21558 /* FN is a FUNCTION_DECL which may contains a parameter with an
21559    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21560    assumes that the current scope is the scope in which the default
21561    argument should be processed.  */
21562
21563 static void
21564 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21565 {
21566   bool saved_local_variables_forbidden_p;
21567   tree parm, parmdecl;
21568
21569   /* While we're parsing the default args, we might (due to the
21570      statement expression extension) encounter more classes.  We want
21571      to handle them right away, but we don't want them getting mixed
21572      up with default args that are currently in the queue.  */
21573   push_unparsed_function_queues (parser);
21574
21575   /* Local variable names (and the `this' keyword) may not appear
21576      in a default argument.  */
21577   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21578   parser->local_variables_forbidden_p = true;
21579
21580   push_defarg_context (fn);
21581
21582   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21583          parmdecl = DECL_ARGUMENTS (fn);
21584        parm && parm != void_list_node;
21585        parm = TREE_CHAIN (parm),
21586          parmdecl = DECL_CHAIN (parmdecl))
21587     {
21588       tree default_arg = TREE_PURPOSE (parm);
21589       tree parsed_arg;
21590       VEC(tree,gc) *insts;
21591       tree copy;
21592       unsigned ix;
21593
21594       if (!default_arg)
21595         continue;
21596
21597       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21598         /* This can happen for a friend declaration for a function
21599            already declared with default arguments.  */
21600         continue;
21601
21602       parsed_arg
21603         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21604                                                 default_arg,
21605                                                 TREE_VALUE (parm));
21606       if (parsed_arg == error_mark_node)
21607         {
21608           continue;
21609         }
21610
21611       TREE_PURPOSE (parm) = parsed_arg;
21612
21613       /* Update any instantiations we've already created.  */
21614       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21615            VEC_iterate (tree, insts, ix, copy); ix++)
21616         TREE_PURPOSE (copy) = parsed_arg;
21617     }
21618
21619   pop_defarg_context ();
21620
21621   /* Make sure no default arg is missing.  */
21622   check_default_args (fn);
21623
21624   /* Restore the state of local_variables_forbidden_p.  */
21625   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21626
21627   /* Restore the queue.  */
21628   pop_unparsed_function_queues (parser);
21629 }
21630
21631 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21632    either a TYPE or an expression, depending on the form of the
21633    input.  The KEYWORD indicates which kind of expression we have
21634    encountered.  */
21635
21636 static tree
21637 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21638 {
21639   tree expr = NULL_TREE;
21640   const char *saved_message;
21641   char *tmp;
21642   bool saved_integral_constant_expression_p;
21643   bool saved_non_integral_constant_expression_p;
21644   bool pack_expansion_p = false;
21645
21646   /* Types cannot be defined in a `sizeof' expression.  Save away the
21647      old message.  */
21648   saved_message = parser->type_definition_forbidden_message;
21649   /* And create the new one.  */
21650   tmp = concat ("types may not be defined in %<",
21651                 IDENTIFIER_POINTER (ridpointers[keyword]),
21652                 "%> expressions", NULL);
21653   parser->type_definition_forbidden_message = tmp;
21654
21655   /* The restrictions on constant-expressions do not apply inside
21656      sizeof expressions.  */
21657   saved_integral_constant_expression_p
21658     = parser->integral_constant_expression_p;
21659   saved_non_integral_constant_expression_p
21660     = parser->non_integral_constant_expression_p;
21661   parser->integral_constant_expression_p = false;
21662
21663   /* If it's a `...', then we are computing the length of a parameter
21664      pack.  */
21665   if (keyword == RID_SIZEOF
21666       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21667     {
21668       /* Consume the `...'.  */
21669       cp_lexer_consume_token (parser->lexer);
21670       maybe_warn_variadic_templates ();
21671
21672       /* Note that this is an expansion.  */
21673       pack_expansion_p = true;
21674     }
21675
21676   /* Do not actually evaluate the expression.  */
21677   ++cp_unevaluated_operand;
21678   ++c_inhibit_evaluation_warnings;
21679   /* If it's a `(', then we might be looking at the type-id
21680      construction.  */
21681   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21682     {
21683       tree type;
21684       bool saved_in_type_id_in_expr_p;
21685
21686       /* We can't be sure yet whether we're looking at a type-id or an
21687          expression.  */
21688       cp_parser_parse_tentatively (parser);
21689       /* Consume the `('.  */
21690       cp_lexer_consume_token (parser->lexer);
21691       /* Parse the type-id.  */
21692       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21693       parser->in_type_id_in_expr_p = true;
21694       type = cp_parser_type_id (parser);
21695       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21696       /* Now, look for the trailing `)'.  */
21697       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21698       /* If all went well, then we're done.  */
21699       if (cp_parser_parse_definitely (parser))
21700         {
21701           cp_decl_specifier_seq decl_specs;
21702
21703           /* Build a trivial decl-specifier-seq.  */
21704           clear_decl_specs (&decl_specs);
21705           decl_specs.type = type;
21706
21707           /* Call grokdeclarator to figure out what type this is.  */
21708           expr = grokdeclarator (NULL,
21709                                  &decl_specs,
21710                                  TYPENAME,
21711                                  /*initialized=*/0,
21712                                  /*attrlist=*/NULL);
21713         }
21714     }
21715
21716   /* If the type-id production did not work out, then we must be
21717      looking at the unary-expression production.  */
21718   if (!expr)
21719     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21720                                        /*cast_p=*/false, NULL);
21721
21722   if (pack_expansion_p)
21723     /* Build a pack expansion. */
21724     expr = make_pack_expansion (expr);
21725
21726   /* Go back to evaluating expressions.  */
21727   --cp_unevaluated_operand;
21728   --c_inhibit_evaluation_warnings;
21729
21730   /* Free the message we created.  */
21731   free (tmp);
21732   /* And restore the old one.  */
21733   parser->type_definition_forbidden_message = saved_message;
21734   parser->integral_constant_expression_p
21735     = saved_integral_constant_expression_p;
21736   parser->non_integral_constant_expression_p
21737     = saved_non_integral_constant_expression_p;
21738
21739   return expr;
21740 }
21741
21742 /* If the current declaration has no declarator, return true.  */
21743
21744 static bool
21745 cp_parser_declares_only_class_p (cp_parser *parser)
21746 {
21747   /* If the next token is a `;' or a `,' then there is no
21748      declarator.  */
21749   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21750           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21751 }
21752
21753 /* Update the DECL_SPECS to reflect the storage class indicated by
21754    KEYWORD.  */
21755
21756 static void
21757 cp_parser_set_storage_class (cp_parser *parser,
21758                              cp_decl_specifier_seq *decl_specs,
21759                              enum rid keyword,
21760                              location_t location)
21761 {
21762   cp_storage_class storage_class;
21763
21764   if (parser->in_unbraced_linkage_specification_p)
21765     {
21766       error_at (location, "invalid use of %qD in linkage specification",
21767                 ridpointers[keyword]);
21768       return;
21769     }
21770   else if (decl_specs->storage_class != sc_none)
21771     {
21772       decl_specs->conflicting_specifiers_p = true;
21773       return;
21774     }
21775
21776   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21777       && decl_specs->specs[(int) ds_thread])
21778     {
21779       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21780       decl_specs->specs[(int) ds_thread] = 0;
21781     }
21782
21783   switch (keyword)
21784     {
21785     case RID_AUTO:
21786       storage_class = sc_auto;
21787       break;
21788     case RID_REGISTER:
21789       storage_class = sc_register;
21790       break;
21791     case RID_STATIC:
21792       storage_class = sc_static;
21793       break;
21794     case RID_EXTERN:
21795       storage_class = sc_extern;
21796       break;
21797     case RID_MUTABLE:
21798       storage_class = sc_mutable;
21799       break;
21800     default:
21801       gcc_unreachable ();
21802     }
21803   decl_specs->storage_class = storage_class;
21804
21805   /* A storage class specifier cannot be applied alongside a typedef 
21806      specifier. If there is a typedef specifier present then set 
21807      conflicting_specifiers_p which will trigger an error later
21808      on in grokdeclarator. */
21809   if (decl_specs->specs[(int)ds_typedef])
21810     decl_specs->conflicting_specifiers_p = true;
21811 }
21812
21813 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
21814    is true, the type is a class or enum definition.  */
21815
21816 static void
21817 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
21818                               tree type_spec,
21819                               location_t location,
21820                               bool type_definition_p)
21821 {
21822   decl_specs->any_specifiers_p = true;
21823
21824   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
21825      (with, for example, in "typedef int wchar_t;") we remember that
21826      this is what happened.  In system headers, we ignore these
21827      declarations so that G++ can work with system headers that are not
21828      C++-safe.  */
21829   if (decl_specs->specs[(int) ds_typedef]
21830       && !type_definition_p
21831       && (type_spec == boolean_type_node
21832           || type_spec == char16_type_node
21833           || type_spec == char32_type_node
21834           || type_spec == wchar_type_node)
21835       && (decl_specs->type
21836           || decl_specs->specs[(int) ds_long]
21837           || decl_specs->specs[(int) ds_short]
21838           || decl_specs->specs[(int) ds_unsigned]
21839           || decl_specs->specs[(int) ds_signed]))
21840     {
21841       decl_specs->redefined_builtin_type = type_spec;
21842       if (!decl_specs->type)
21843         {
21844           decl_specs->type = type_spec;
21845           decl_specs->type_definition_p = false;
21846           decl_specs->type_location = location;
21847         }
21848     }
21849   else if (decl_specs->type)
21850     decl_specs->multiple_types_p = true;
21851   else
21852     {
21853       decl_specs->type = type_spec;
21854       decl_specs->type_definition_p = type_definition_p;
21855       decl_specs->redefined_builtin_type = NULL_TREE;
21856       decl_specs->type_location = location;
21857     }
21858 }
21859
21860 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21861    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
21862
21863 static bool
21864 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21865 {
21866   return decl_specifiers->specs[(int) ds_friend] != 0;
21867 }
21868
21869 /* Issue an error message indicating that TOKEN_DESC was expected.
21870    If KEYWORD is true, it indicated this function is called by
21871    cp_parser_require_keword and the required token can only be
21872    a indicated keyword. */
21873
21874 static void
21875 cp_parser_required_error (cp_parser *parser,
21876                           required_token token_desc,
21877                           bool keyword)
21878 {
21879   switch (token_desc)
21880     {
21881       case RT_NEW:
21882         cp_parser_error (parser, "expected %<new%>");
21883         return;
21884       case RT_DELETE:
21885         cp_parser_error (parser, "expected %<delete%>");
21886         return;
21887       case RT_RETURN:
21888         cp_parser_error (parser, "expected %<return%>");
21889         return;
21890       case RT_WHILE:
21891         cp_parser_error (parser, "expected %<while%>");
21892         return;
21893       case RT_EXTERN:
21894         cp_parser_error (parser, "expected %<extern%>");
21895         return;
21896       case RT_STATIC_ASSERT:
21897         cp_parser_error (parser, "expected %<static_assert%>");
21898         return;
21899       case RT_DECLTYPE:
21900         cp_parser_error (parser, "expected %<decltype%>");
21901         return;
21902       case RT_OPERATOR:
21903         cp_parser_error (parser, "expected %<operator%>");
21904         return;
21905       case RT_CLASS:
21906         cp_parser_error (parser, "expected %<class%>");
21907         return;
21908       case RT_TEMPLATE:
21909         cp_parser_error (parser, "expected %<template%>");
21910         return;
21911       case RT_NAMESPACE:
21912         cp_parser_error (parser, "expected %<namespace%>");
21913         return;
21914       case RT_USING:
21915         cp_parser_error (parser, "expected %<using%>");
21916         return;
21917       case RT_ASM:
21918         cp_parser_error (parser, "expected %<asm%>");
21919         return;
21920       case RT_TRY:
21921         cp_parser_error (parser, "expected %<try%>");
21922         return;
21923       case RT_CATCH:
21924         cp_parser_error (parser, "expected %<catch%>");
21925         return;
21926       case RT_THROW:
21927         cp_parser_error (parser, "expected %<throw%>");
21928         return;
21929       case RT_LABEL:
21930         cp_parser_error (parser, "expected %<__label__%>");
21931         return;
21932       case RT_AT_TRY:
21933         cp_parser_error (parser, "expected %<@try%>");
21934         return;
21935       case RT_AT_SYNCHRONIZED:
21936         cp_parser_error (parser, "expected %<@synchronized%>");
21937         return;
21938       case RT_AT_THROW:
21939         cp_parser_error (parser, "expected %<@throw%>");
21940         return;
21941       default:
21942         break;
21943     }
21944   if (!keyword)
21945     {
21946       switch (token_desc)
21947         {
21948           case RT_SEMICOLON:
21949             cp_parser_error (parser, "expected %<;%>");
21950             return;
21951           case RT_OPEN_PAREN:
21952             cp_parser_error (parser, "expected %<(%>");
21953             return;
21954           case RT_CLOSE_BRACE:
21955             cp_parser_error (parser, "expected %<}%>");
21956             return;
21957           case RT_OPEN_BRACE:
21958             cp_parser_error (parser, "expected %<{%>");
21959             return;
21960           case RT_CLOSE_SQUARE:
21961             cp_parser_error (parser, "expected %<]%>");
21962             return;
21963           case RT_OPEN_SQUARE:
21964             cp_parser_error (parser, "expected %<[%>");
21965             return;
21966           case RT_COMMA:
21967             cp_parser_error (parser, "expected %<,%>");
21968             return;
21969           case RT_SCOPE:
21970             cp_parser_error (parser, "expected %<::%>");
21971             return;
21972           case RT_LESS:
21973             cp_parser_error (parser, "expected %<<%>");
21974             return;
21975           case RT_GREATER:
21976             cp_parser_error (parser, "expected %<>%>");
21977             return;
21978           case RT_EQ:
21979             cp_parser_error (parser, "expected %<=%>");
21980             return;
21981           case RT_ELLIPSIS:
21982             cp_parser_error (parser, "expected %<...%>");
21983             return;
21984           case RT_MULT:
21985             cp_parser_error (parser, "expected %<*%>");
21986             return;
21987           case RT_COMPL:
21988             cp_parser_error (parser, "expected %<~%>");
21989             return;
21990           case RT_COLON:
21991             cp_parser_error (parser, "expected %<:%>");
21992             return;
21993           case RT_COLON_SCOPE:
21994             cp_parser_error (parser, "expected %<:%> or %<::%>");
21995             return;
21996           case RT_CLOSE_PAREN:
21997             cp_parser_error (parser, "expected %<)%>");
21998             return;
21999           case RT_COMMA_CLOSE_PAREN:
22000             cp_parser_error (parser, "expected %<,%> or %<)%>");
22001             return;
22002           case RT_PRAGMA_EOL:
22003             cp_parser_error (parser, "expected end of line");
22004             return;
22005           case RT_NAME:
22006             cp_parser_error (parser, "expected identifier");
22007             return;
22008           case RT_SELECT:
22009             cp_parser_error (parser, "expected selection-statement");
22010             return;
22011           case RT_INTERATION:
22012             cp_parser_error (parser, "expected iteration-statement");
22013             return;
22014           case RT_JUMP:
22015             cp_parser_error (parser, "expected jump-statement");
22016             return;
22017           case RT_CLASS_KEY:
22018             cp_parser_error (parser, "expected class-key");
22019             return;
22020           case RT_CLASS_TYPENAME_TEMPLATE:
22021             cp_parser_error (parser,
22022                  "expected %<class%>, %<typename%>, or %<template%>");
22023             return;
22024           default:
22025             gcc_unreachable ();
22026         }
22027     }
22028   else
22029     gcc_unreachable ();
22030 }
22031
22032
22033
22034 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22035    issue an error message indicating that TOKEN_DESC was expected.
22036
22037    Returns the token consumed, if the token had the appropriate type.
22038    Otherwise, returns NULL.  */
22039
22040 static cp_token *
22041 cp_parser_require (cp_parser* parser,
22042                    enum cpp_ttype type,
22043                    required_token token_desc)
22044 {
22045   if (cp_lexer_next_token_is (parser->lexer, type))
22046     return cp_lexer_consume_token (parser->lexer);
22047   else
22048     {
22049       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22050       if (!cp_parser_simulate_error (parser))
22051         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22052       return NULL;
22053     }
22054 }
22055
22056 /* An error message is produced if the next token is not '>'.
22057    All further tokens are skipped until the desired token is
22058    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22059
22060 static void
22061 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22062 {
22063   /* Current level of '< ... >'.  */
22064   unsigned level = 0;
22065   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22066   unsigned nesting_depth = 0;
22067
22068   /* Are we ready, yet?  If not, issue error message.  */
22069   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22070     return;
22071
22072   /* Skip tokens until the desired token is found.  */
22073   while (true)
22074     {
22075       /* Peek at the next token.  */
22076       switch (cp_lexer_peek_token (parser->lexer)->type)
22077         {
22078         case CPP_LESS:
22079           if (!nesting_depth)
22080             ++level;
22081           break;
22082
22083         case CPP_RSHIFT:
22084           if (cxx_dialect == cxx98)
22085             /* C++0x views the `>>' operator as two `>' tokens, but
22086                C++98 does not. */
22087             break;
22088           else if (!nesting_depth && level-- == 0)
22089             {
22090               /* We've hit a `>>' where the first `>' closes the
22091                  template argument list, and the second `>' is
22092                  spurious.  Just consume the `>>' and stop; we've
22093                  already produced at least one error.  */
22094               cp_lexer_consume_token (parser->lexer);
22095               return;
22096             }
22097           /* Fall through for C++0x, so we handle the second `>' in
22098              the `>>'.  */
22099
22100         case CPP_GREATER:
22101           if (!nesting_depth && level-- == 0)
22102             {
22103               /* We've reached the token we want, consume it and stop.  */
22104               cp_lexer_consume_token (parser->lexer);
22105               return;
22106             }
22107           break;
22108
22109         case CPP_OPEN_PAREN:
22110         case CPP_OPEN_SQUARE:
22111           ++nesting_depth;
22112           break;
22113
22114         case CPP_CLOSE_PAREN:
22115         case CPP_CLOSE_SQUARE:
22116           if (nesting_depth-- == 0)
22117             return;
22118           break;
22119
22120         case CPP_EOF:
22121         case CPP_PRAGMA_EOL:
22122         case CPP_SEMICOLON:
22123         case CPP_OPEN_BRACE:
22124         case CPP_CLOSE_BRACE:
22125           /* The '>' was probably forgotten, don't look further.  */
22126           return;
22127
22128         default:
22129           break;
22130         }
22131
22132       /* Consume this token.  */
22133       cp_lexer_consume_token (parser->lexer);
22134     }
22135 }
22136
22137 /* If the next token is the indicated keyword, consume it.  Otherwise,
22138    issue an error message indicating that TOKEN_DESC was expected.
22139
22140    Returns the token consumed, if the token had the appropriate type.
22141    Otherwise, returns NULL.  */
22142
22143 static cp_token *
22144 cp_parser_require_keyword (cp_parser* parser,
22145                            enum rid keyword,
22146                            required_token token_desc)
22147 {
22148   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22149
22150   if (token && token->keyword != keyword)
22151     {
22152       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22153       return NULL;
22154     }
22155
22156   return token;
22157 }
22158
22159 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22160    function-definition.  */
22161
22162 static bool
22163 cp_parser_token_starts_function_definition_p (cp_token* token)
22164 {
22165   return (/* An ordinary function-body begins with an `{'.  */
22166           token->type == CPP_OPEN_BRACE
22167           /* A ctor-initializer begins with a `:'.  */
22168           || token->type == CPP_COLON
22169           /* A function-try-block begins with `try'.  */
22170           || token->keyword == RID_TRY
22171           /* The named return value extension begins with `return'.  */
22172           || token->keyword == RID_RETURN);
22173 }
22174
22175 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22176    definition.  */
22177
22178 static bool
22179 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22180 {
22181   cp_token *token;
22182
22183   token = cp_lexer_peek_token (parser->lexer);
22184   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22185 }
22186
22187 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22188    C++0x) ending a template-argument.  */
22189
22190 static bool
22191 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22192 {
22193   cp_token *token;
22194
22195   token = cp_lexer_peek_token (parser->lexer);
22196   return (token->type == CPP_COMMA 
22197           || token->type == CPP_GREATER
22198           || token->type == CPP_ELLIPSIS
22199           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22200 }
22201
22202 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22203    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22204
22205 static bool
22206 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22207                                                      size_t n)
22208 {
22209   cp_token *token;
22210
22211   token = cp_lexer_peek_nth_token (parser->lexer, n);
22212   if (token->type == CPP_LESS)
22213     return true;
22214   /* Check for the sequence `<::' in the original code. It would be lexed as
22215      `[:', where `[' is a digraph, and there is no whitespace before
22216      `:'.  */
22217   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22218     {
22219       cp_token *token2;
22220       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22221       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22222         return true;
22223     }
22224   return false;
22225 }
22226
22227 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22228    or none_type otherwise.  */
22229
22230 static enum tag_types
22231 cp_parser_token_is_class_key (cp_token* token)
22232 {
22233   switch (token->keyword)
22234     {
22235     case RID_CLASS:
22236       return class_type;
22237     case RID_STRUCT:
22238       return record_type;
22239     case RID_UNION:
22240       return union_type;
22241
22242     default:
22243       return none_type;
22244     }
22245 }
22246
22247 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22248
22249 static void
22250 cp_parser_check_class_key (enum tag_types class_key, tree type)
22251 {
22252   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22253     permerror (input_location, "%qs tag used in naming %q#T",
22254             class_key == union_type ? "union"
22255              : class_key == record_type ? "struct" : "class",
22256              type);
22257 }
22258
22259 /* Issue an error message if DECL is redeclared with different
22260    access than its original declaration [class.access.spec/3].
22261    This applies to nested classes and nested class templates.
22262    [class.mem/1].  */
22263
22264 static void
22265 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22266 {
22267   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22268     return;
22269
22270   if ((TREE_PRIVATE (decl)
22271        != (current_access_specifier == access_private_node))
22272       || (TREE_PROTECTED (decl)
22273           != (current_access_specifier == access_protected_node)))
22274     error_at (location, "%qD redeclared with different access", decl);
22275 }
22276
22277 /* Look for the `template' keyword, as a syntactic disambiguator.
22278    Return TRUE iff it is present, in which case it will be
22279    consumed.  */
22280
22281 static bool
22282 cp_parser_optional_template_keyword (cp_parser *parser)
22283 {
22284   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22285     {
22286       /* The `template' keyword can only be used within templates;
22287          outside templates the parser can always figure out what is a
22288          template and what is not.  */
22289       if (!processing_template_decl)
22290         {
22291           cp_token *token = cp_lexer_peek_token (parser->lexer);
22292           error_at (token->location,
22293                     "%<template%> (as a disambiguator) is only allowed "
22294                     "within templates");
22295           /* If this part of the token stream is rescanned, the same
22296              error message would be generated.  So, we purge the token
22297              from the stream.  */
22298           cp_lexer_purge_token (parser->lexer);
22299           return false;
22300         }
22301       else
22302         {
22303           /* Consume the `template' keyword.  */
22304           cp_lexer_consume_token (parser->lexer);
22305           return true;
22306         }
22307     }
22308
22309   return false;
22310 }
22311
22312 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22313    set PARSER->SCOPE, and perform other related actions.  */
22314
22315 static void
22316 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22317 {
22318   int i;
22319   struct tree_check *check_value;
22320   deferred_access_check *chk;
22321   VEC (deferred_access_check,gc) *checks;
22322
22323   /* Get the stored value.  */
22324   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22325   /* Perform any access checks that were deferred.  */
22326   checks = check_value->checks;
22327   if (checks)
22328     {
22329       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22330         perform_or_defer_access_check (chk->binfo,
22331                                        chk->decl,
22332                                        chk->diag_decl);
22333     }
22334   /* Set the scope from the stored value.  */
22335   parser->scope = check_value->value;
22336   parser->qualifying_scope = check_value->qualifying_scope;
22337   parser->object_scope = NULL_TREE;
22338 }
22339
22340 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22341    encounter the end of a block before what we were looking for.  */
22342
22343 static bool
22344 cp_parser_cache_group (cp_parser *parser,
22345                        enum cpp_ttype end,
22346                        unsigned depth)
22347 {
22348   while (true)
22349     {
22350       cp_token *token = cp_lexer_peek_token (parser->lexer);
22351
22352       /* Abort a parenthesized expression if we encounter a semicolon.  */
22353       if ((end == CPP_CLOSE_PAREN || depth == 0)
22354           && token->type == CPP_SEMICOLON)
22355         return true;
22356       /* If we've reached the end of the file, stop.  */
22357       if (token->type == CPP_EOF
22358           || (end != CPP_PRAGMA_EOL
22359               && token->type == CPP_PRAGMA_EOL))
22360         return true;
22361       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22362         /* We've hit the end of an enclosing block, so there's been some
22363            kind of syntax error.  */
22364         return true;
22365
22366       /* If we're caching something finished by a comma (or semicolon),
22367          such as an NSDMI, don't consume the comma.  */
22368       if (end == CPP_COMMA
22369           && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22370         return false;
22371
22372       /* Consume the token.  */
22373       cp_lexer_consume_token (parser->lexer);
22374       /* See if it starts a new group.  */
22375       if (token->type == CPP_OPEN_BRACE)
22376         {
22377           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22378           /* In theory this should probably check end == '}', but
22379              cp_parser_save_member_function_body needs it to exit
22380              after either '}' or ')' when called with ')'.  */
22381           if (depth == 0)
22382             return false;
22383         }
22384       else if (token->type == CPP_OPEN_PAREN)
22385         {
22386           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22387           if (depth == 0 && end == CPP_CLOSE_PAREN)
22388             return false;
22389         }
22390       else if (token->type == CPP_PRAGMA)
22391         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22392       else if (token->type == end)
22393         return false;
22394     }
22395 }
22396
22397 /* Begin parsing tentatively.  We always save tokens while parsing
22398    tentatively so that if the tentative parsing fails we can restore the
22399    tokens.  */
22400
22401 static void
22402 cp_parser_parse_tentatively (cp_parser* parser)
22403 {
22404   /* Enter a new parsing context.  */
22405   parser->context = cp_parser_context_new (parser->context);
22406   /* Begin saving tokens.  */
22407   cp_lexer_save_tokens (parser->lexer);
22408   /* In order to avoid repetitive access control error messages,
22409      access checks are queued up until we are no longer parsing
22410      tentatively.  */
22411   push_deferring_access_checks (dk_deferred);
22412 }
22413
22414 /* Commit to the currently active tentative parse.  */
22415
22416 static void
22417 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22418 {
22419   cp_parser_context *context;
22420   cp_lexer *lexer;
22421
22422   /* Mark all of the levels as committed.  */
22423   lexer = parser->lexer;
22424   for (context = parser->context; context->next; context = context->next)
22425     {
22426       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22427         break;
22428       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22429       while (!cp_lexer_saving_tokens (lexer))
22430         lexer = lexer->next;
22431       cp_lexer_commit_tokens (lexer);
22432     }
22433 }
22434
22435 /* Abort the currently active tentative parse.  All consumed tokens
22436    will be rolled back, and no diagnostics will be issued.  */
22437
22438 static void
22439 cp_parser_abort_tentative_parse (cp_parser* parser)
22440 {
22441   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22442               || errorcount > 0);
22443   cp_parser_simulate_error (parser);
22444   /* Now, pretend that we want to see if the construct was
22445      successfully parsed.  */
22446   cp_parser_parse_definitely (parser);
22447 }
22448
22449 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22450    token stream.  Otherwise, commit to the tokens we have consumed.
22451    Returns true if no error occurred; false otherwise.  */
22452
22453 static bool
22454 cp_parser_parse_definitely (cp_parser* parser)
22455 {
22456   bool error_occurred;
22457   cp_parser_context *context;
22458
22459   /* Remember whether or not an error occurred, since we are about to
22460      destroy that information.  */
22461   error_occurred = cp_parser_error_occurred (parser);
22462   /* Remove the topmost context from the stack.  */
22463   context = parser->context;
22464   parser->context = context->next;
22465   /* If no parse errors occurred, commit to the tentative parse.  */
22466   if (!error_occurred)
22467     {
22468       /* Commit to the tokens read tentatively, unless that was
22469          already done.  */
22470       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22471         cp_lexer_commit_tokens (parser->lexer);
22472
22473       pop_to_parent_deferring_access_checks ();
22474     }
22475   /* Otherwise, if errors occurred, roll back our state so that things
22476      are just as they were before we began the tentative parse.  */
22477   else
22478     {
22479       cp_lexer_rollback_tokens (parser->lexer);
22480       pop_deferring_access_checks ();
22481     }
22482   /* Add the context to the front of the free list.  */
22483   context->next = cp_parser_context_free_list;
22484   cp_parser_context_free_list = context;
22485
22486   return !error_occurred;
22487 }
22488
22489 /* Returns true if we are parsing tentatively and are not committed to
22490    this tentative parse.  */
22491
22492 static bool
22493 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22494 {
22495   return (cp_parser_parsing_tentatively (parser)
22496           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22497 }
22498
22499 /* Returns nonzero iff an error has occurred during the most recent
22500    tentative parse.  */
22501
22502 static bool
22503 cp_parser_error_occurred (cp_parser* parser)
22504 {
22505   return (cp_parser_parsing_tentatively (parser)
22506           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22507 }
22508
22509 /* Returns nonzero if GNU extensions are allowed.  */
22510
22511 static bool
22512 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22513 {
22514   return parser->allow_gnu_extensions_p;
22515 }
22516 \f
22517 /* Objective-C++ Productions */
22518
22519
22520 /* Parse an Objective-C expression, which feeds into a primary-expression
22521    above.
22522
22523    objc-expression:
22524      objc-message-expression
22525      objc-string-literal
22526      objc-encode-expression
22527      objc-protocol-expression
22528      objc-selector-expression
22529
22530   Returns a tree representation of the expression.  */
22531
22532 static tree
22533 cp_parser_objc_expression (cp_parser* parser)
22534 {
22535   /* Try to figure out what kind of declaration is present.  */
22536   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22537
22538   switch (kwd->type)
22539     {
22540     case CPP_OPEN_SQUARE:
22541       return cp_parser_objc_message_expression (parser);
22542
22543     case CPP_OBJC_STRING:
22544       kwd = cp_lexer_consume_token (parser->lexer);
22545       return objc_build_string_object (kwd->u.value);
22546
22547     case CPP_KEYWORD:
22548       switch (kwd->keyword)
22549         {
22550         case RID_AT_ENCODE:
22551           return cp_parser_objc_encode_expression (parser);
22552
22553         case RID_AT_PROTOCOL:
22554           return cp_parser_objc_protocol_expression (parser);
22555
22556         case RID_AT_SELECTOR:
22557           return cp_parser_objc_selector_expression (parser);
22558
22559         default:
22560           break;
22561         }
22562     default:
22563       error_at (kwd->location,
22564                 "misplaced %<@%D%> Objective-C++ construct",
22565                 kwd->u.value);
22566       cp_parser_skip_to_end_of_block_or_statement (parser);
22567     }
22568
22569   return error_mark_node;
22570 }
22571
22572 /* Parse an Objective-C message expression.
22573
22574    objc-message-expression:
22575      [ objc-message-receiver objc-message-args ]
22576
22577    Returns a representation of an Objective-C message.  */
22578
22579 static tree
22580 cp_parser_objc_message_expression (cp_parser* parser)
22581 {
22582   tree receiver, messageargs;
22583
22584   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
22585   receiver = cp_parser_objc_message_receiver (parser);
22586   messageargs = cp_parser_objc_message_args (parser);
22587   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22588
22589   return objc_build_message_expr (receiver, messageargs);
22590 }
22591
22592 /* Parse an objc-message-receiver.
22593
22594    objc-message-receiver:
22595      expression
22596      simple-type-specifier
22597
22598   Returns a representation of the type or expression.  */
22599
22600 static tree
22601 cp_parser_objc_message_receiver (cp_parser* parser)
22602 {
22603   tree rcv;
22604
22605   /* An Objective-C message receiver may be either (1) a type
22606      or (2) an expression.  */
22607   cp_parser_parse_tentatively (parser);
22608   rcv = cp_parser_expression (parser, false, NULL);
22609
22610   if (cp_parser_parse_definitely (parser))
22611     return rcv;
22612
22613   rcv = cp_parser_simple_type_specifier (parser,
22614                                          /*decl_specs=*/NULL,
22615                                          CP_PARSER_FLAGS_NONE);
22616
22617   return objc_get_class_reference (rcv);
22618 }
22619
22620 /* Parse the arguments and selectors comprising an Objective-C message.
22621
22622    objc-message-args:
22623      objc-selector
22624      objc-selector-args
22625      objc-selector-args , objc-comma-args
22626
22627    objc-selector-args:
22628      objc-selector [opt] : assignment-expression
22629      objc-selector-args objc-selector [opt] : assignment-expression
22630
22631    objc-comma-args:
22632      assignment-expression
22633      objc-comma-args , assignment-expression
22634
22635    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22636    selector arguments and TREE_VALUE containing a list of comma
22637    arguments.  */
22638
22639 static tree
22640 cp_parser_objc_message_args (cp_parser* parser)
22641 {
22642   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22643   bool maybe_unary_selector_p = true;
22644   cp_token *token = cp_lexer_peek_token (parser->lexer);
22645
22646   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22647     {
22648       tree selector = NULL_TREE, arg;
22649
22650       if (token->type != CPP_COLON)
22651         selector = cp_parser_objc_selector (parser);
22652
22653       /* Detect if we have a unary selector.  */
22654       if (maybe_unary_selector_p
22655           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22656         return build_tree_list (selector, NULL_TREE);
22657
22658       maybe_unary_selector_p = false;
22659       cp_parser_require (parser, CPP_COLON, RT_COLON);
22660       arg = cp_parser_assignment_expression (parser, false, NULL);
22661
22662       sel_args
22663         = chainon (sel_args,
22664                    build_tree_list (selector, arg));
22665
22666       token = cp_lexer_peek_token (parser->lexer);
22667     }
22668
22669   /* Handle non-selector arguments, if any. */
22670   while (token->type == CPP_COMMA)
22671     {
22672       tree arg;
22673
22674       cp_lexer_consume_token (parser->lexer);
22675       arg = cp_parser_assignment_expression (parser, false, NULL);
22676
22677       addl_args
22678         = chainon (addl_args,
22679                    build_tree_list (NULL_TREE, arg));
22680
22681       token = cp_lexer_peek_token (parser->lexer);
22682     }
22683
22684   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22685     {
22686       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22687       return build_tree_list (error_mark_node, error_mark_node);
22688     }
22689
22690   return build_tree_list (sel_args, addl_args);
22691 }
22692
22693 /* Parse an Objective-C encode expression.
22694
22695    objc-encode-expression:
22696      @encode objc-typename
22697
22698    Returns an encoded representation of the type argument.  */
22699
22700 static tree
22701 cp_parser_objc_encode_expression (cp_parser* parser)
22702 {
22703   tree type;
22704   cp_token *token;
22705
22706   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
22707   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22708   token = cp_lexer_peek_token (parser->lexer);
22709   type = complete_type (cp_parser_type_id (parser));
22710   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22711
22712   if (!type)
22713     {
22714       error_at (token->location, 
22715                 "%<@encode%> must specify a type as an argument");
22716       return error_mark_node;
22717     }
22718
22719   /* This happens if we find @encode(T) (where T is a template
22720      typename or something dependent on a template typename) when
22721      parsing a template.  In that case, we can't compile it
22722      immediately, but we rather create an AT_ENCODE_EXPR which will
22723      need to be instantiated when the template is used.
22724   */
22725   if (dependent_type_p (type))
22726     {
22727       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22728       TREE_READONLY (value) = 1;
22729       return value;
22730     }
22731
22732   return objc_build_encode_expr (type);
22733 }
22734
22735 /* Parse an Objective-C @defs expression.  */
22736
22737 static tree
22738 cp_parser_objc_defs_expression (cp_parser *parser)
22739 {
22740   tree name;
22741
22742   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
22743   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22744   name = cp_parser_identifier (parser);
22745   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22746
22747   return objc_get_class_ivars (name);
22748 }
22749
22750 /* Parse an Objective-C protocol expression.
22751
22752   objc-protocol-expression:
22753     @protocol ( identifier )
22754
22755   Returns a representation of the protocol expression.  */
22756
22757 static tree
22758 cp_parser_objc_protocol_expression (cp_parser* parser)
22759 {
22760   tree proto;
22761
22762   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22763   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22764   proto = cp_parser_identifier (parser);
22765   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22766
22767   return objc_build_protocol_expr (proto);
22768 }
22769
22770 /* Parse an Objective-C selector expression.
22771
22772    objc-selector-expression:
22773      @selector ( objc-method-signature )
22774
22775    objc-method-signature:
22776      objc-selector
22777      objc-selector-seq
22778
22779    objc-selector-seq:
22780      objc-selector :
22781      objc-selector-seq objc-selector :
22782
22783   Returns a representation of the method selector.  */
22784
22785 static tree
22786 cp_parser_objc_selector_expression (cp_parser* parser)
22787 {
22788   tree sel_seq = NULL_TREE;
22789   bool maybe_unary_selector_p = true;
22790   cp_token *token;
22791   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22792
22793   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
22794   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22795   token = cp_lexer_peek_token (parser->lexer);
22796
22797   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
22798          || token->type == CPP_SCOPE)
22799     {
22800       tree selector = NULL_TREE;
22801
22802       if (token->type != CPP_COLON
22803           || token->type == CPP_SCOPE)
22804         selector = cp_parser_objc_selector (parser);
22805
22806       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
22807           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22808         {
22809           /* Detect if we have a unary selector.  */
22810           if (maybe_unary_selector_p)
22811             {
22812               sel_seq = selector;
22813               goto finish_selector;
22814             }
22815           else
22816             {
22817               cp_parser_error (parser, "expected %<:%>");
22818             }
22819         }
22820       maybe_unary_selector_p = false;
22821       token = cp_lexer_consume_token (parser->lexer);
22822
22823       if (token->type == CPP_SCOPE)
22824         {
22825           sel_seq
22826             = chainon (sel_seq,
22827                        build_tree_list (selector, NULL_TREE));
22828           sel_seq
22829             = chainon (sel_seq,
22830                        build_tree_list (NULL_TREE, NULL_TREE));
22831         }
22832       else
22833         sel_seq
22834           = chainon (sel_seq,
22835                      build_tree_list (selector, NULL_TREE));
22836
22837       token = cp_lexer_peek_token (parser->lexer);
22838     }
22839
22840  finish_selector:
22841   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22842
22843   return objc_build_selector_expr (loc, sel_seq);
22844 }
22845
22846 /* Parse a list of identifiers.
22847
22848    objc-identifier-list:
22849      identifier
22850      objc-identifier-list , identifier
22851
22852    Returns a TREE_LIST of identifier nodes.  */
22853
22854 static tree
22855 cp_parser_objc_identifier_list (cp_parser* parser)
22856 {
22857   tree identifier;
22858   tree list;
22859   cp_token *sep;
22860
22861   identifier = cp_parser_identifier (parser);
22862   if (identifier == error_mark_node)
22863     return error_mark_node;      
22864
22865   list = build_tree_list (NULL_TREE, identifier);
22866   sep = cp_lexer_peek_token (parser->lexer);
22867
22868   while (sep->type == CPP_COMMA)
22869     {
22870       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22871       identifier = cp_parser_identifier (parser);
22872       if (identifier == error_mark_node)
22873         return list;
22874
22875       list = chainon (list, build_tree_list (NULL_TREE,
22876                                              identifier));
22877       sep = cp_lexer_peek_token (parser->lexer);
22878     }
22879   
22880   return list;
22881 }
22882
22883 /* Parse an Objective-C alias declaration.
22884
22885    objc-alias-declaration:
22886      @compatibility_alias identifier identifier ;
22887
22888    This function registers the alias mapping with the Objective-C front end.
22889    It returns nothing.  */
22890
22891 static void
22892 cp_parser_objc_alias_declaration (cp_parser* parser)
22893 {
22894   tree alias, orig;
22895
22896   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
22897   alias = cp_parser_identifier (parser);
22898   orig = cp_parser_identifier (parser);
22899   objc_declare_alias (alias, orig);
22900   cp_parser_consume_semicolon_at_end_of_statement (parser);
22901 }
22902
22903 /* Parse an Objective-C class forward-declaration.
22904
22905    objc-class-declaration:
22906      @class objc-identifier-list ;
22907
22908    The function registers the forward declarations with the Objective-C
22909    front end.  It returns nothing.  */
22910
22911 static void
22912 cp_parser_objc_class_declaration (cp_parser* parser)
22913 {
22914   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
22915   while (true)
22916     {
22917       tree id;
22918       
22919       id = cp_parser_identifier (parser);
22920       if (id == error_mark_node)
22921         break;
22922       
22923       objc_declare_class (id);
22924
22925       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22926         cp_lexer_consume_token (parser->lexer);
22927       else
22928         break;
22929     }
22930   cp_parser_consume_semicolon_at_end_of_statement (parser);
22931 }
22932
22933 /* Parse a list of Objective-C protocol references.
22934
22935    objc-protocol-refs-opt:
22936      objc-protocol-refs [opt]
22937
22938    objc-protocol-refs:
22939      < objc-identifier-list >
22940
22941    Returns a TREE_LIST of identifiers, if any.  */
22942
22943 static tree
22944 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22945 {
22946   tree protorefs = NULL_TREE;
22947
22948   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22949     {
22950       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
22951       protorefs = cp_parser_objc_identifier_list (parser);
22952       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22953     }
22954
22955   return protorefs;
22956 }
22957
22958 /* Parse a Objective-C visibility specification.  */
22959
22960 static void
22961 cp_parser_objc_visibility_spec (cp_parser* parser)
22962 {
22963   cp_token *vis = cp_lexer_peek_token (parser->lexer);
22964
22965   switch (vis->keyword)
22966     {
22967     case RID_AT_PRIVATE:
22968       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22969       break;
22970     case RID_AT_PROTECTED:
22971       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22972       break;
22973     case RID_AT_PUBLIC:
22974       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22975       break;
22976     case RID_AT_PACKAGE:
22977       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22978       break;
22979     default:
22980       return;
22981     }
22982
22983   /* Eat '@private'/'@protected'/'@public'.  */
22984   cp_lexer_consume_token (parser->lexer);
22985 }
22986
22987 /* Parse an Objective-C method type.  Return 'true' if it is a class
22988    (+) method, and 'false' if it is an instance (-) method.  */
22989
22990 static inline bool
22991 cp_parser_objc_method_type (cp_parser* parser)
22992 {
22993   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
22994     return true;
22995   else
22996     return false;
22997 }
22998
22999 /* Parse an Objective-C protocol qualifier.  */
23000
23001 static tree
23002 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23003 {
23004   tree quals = NULL_TREE, node;
23005   cp_token *token = cp_lexer_peek_token (parser->lexer);
23006
23007   node = token->u.value;
23008
23009   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23010          && (node == ridpointers [(int) RID_IN]
23011              || node == ridpointers [(int) RID_OUT]
23012              || node == ridpointers [(int) RID_INOUT]
23013              || node == ridpointers [(int) RID_BYCOPY]
23014              || node == ridpointers [(int) RID_BYREF]
23015              || node == ridpointers [(int) RID_ONEWAY]))
23016     {
23017       quals = tree_cons (NULL_TREE, node, quals);
23018       cp_lexer_consume_token (parser->lexer);
23019       token = cp_lexer_peek_token (parser->lexer);
23020       node = token->u.value;
23021     }
23022
23023   return quals;
23024 }
23025
23026 /* Parse an Objective-C typename.  */
23027
23028 static tree
23029 cp_parser_objc_typename (cp_parser* parser)
23030 {
23031   tree type_name = NULL_TREE;
23032
23033   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23034     {
23035       tree proto_quals, cp_type = NULL_TREE;
23036
23037       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23038       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23039
23040       /* An ObjC type name may consist of just protocol qualifiers, in which
23041          case the type shall default to 'id'.  */
23042       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23043         {
23044           cp_type = cp_parser_type_id (parser);
23045           
23046           /* If the type could not be parsed, an error has already
23047              been produced.  For error recovery, behave as if it had
23048              not been specified, which will use the default type
23049              'id'.  */
23050           if (cp_type == error_mark_node)
23051             {
23052               cp_type = NULL_TREE;
23053               /* We need to skip to the closing parenthesis as
23054                  cp_parser_type_id() does not seem to do it for
23055                  us.  */
23056               cp_parser_skip_to_closing_parenthesis (parser,
23057                                                      /*recovering=*/true,
23058                                                      /*or_comma=*/false,
23059                                                      /*consume_paren=*/false);
23060             }
23061         }
23062
23063       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23064       type_name = build_tree_list (proto_quals, cp_type);
23065     }
23066
23067   return type_name;
23068 }
23069
23070 /* Check to see if TYPE refers to an Objective-C selector name.  */
23071
23072 static bool
23073 cp_parser_objc_selector_p (enum cpp_ttype type)
23074 {
23075   return (type == CPP_NAME || type == CPP_KEYWORD
23076           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23077           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23078           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23079           || type == CPP_XOR || type == CPP_XOR_EQ);
23080 }
23081
23082 /* Parse an Objective-C selector.  */
23083
23084 static tree
23085 cp_parser_objc_selector (cp_parser* parser)
23086 {
23087   cp_token *token = cp_lexer_consume_token (parser->lexer);
23088
23089   if (!cp_parser_objc_selector_p (token->type))
23090     {
23091       error_at (token->location, "invalid Objective-C++ selector name");
23092       return error_mark_node;
23093     }
23094
23095   /* C++ operator names are allowed to appear in ObjC selectors.  */
23096   switch (token->type)
23097     {
23098     case CPP_AND_AND: return get_identifier ("and");
23099     case CPP_AND_EQ: return get_identifier ("and_eq");
23100     case CPP_AND: return get_identifier ("bitand");
23101     case CPP_OR: return get_identifier ("bitor");
23102     case CPP_COMPL: return get_identifier ("compl");
23103     case CPP_NOT: return get_identifier ("not");
23104     case CPP_NOT_EQ: return get_identifier ("not_eq");
23105     case CPP_OR_OR: return get_identifier ("or");
23106     case CPP_OR_EQ: return get_identifier ("or_eq");
23107     case CPP_XOR: return get_identifier ("xor");
23108     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23109     default: return token->u.value;
23110     }
23111 }
23112
23113 /* Parse an Objective-C params list.  */
23114
23115 static tree
23116 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23117 {
23118   tree params = NULL_TREE;
23119   bool maybe_unary_selector_p = true;
23120   cp_token *token = cp_lexer_peek_token (parser->lexer);
23121
23122   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23123     {
23124       tree selector = NULL_TREE, type_name, identifier;
23125       tree parm_attr = NULL_TREE;
23126
23127       if (token->keyword == RID_ATTRIBUTE)
23128         break;
23129
23130       if (token->type != CPP_COLON)
23131         selector = cp_parser_objc_selector (parser);
23132
23133       /* Detect if we have a unary selector.  */
23134       if (maybe_unary_selector_p
23135           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23136         {
23137           params = selector; /* Might be followed by attributes.  */
23138           break;
23139         }
23140
23141       maybe_unary_selector_p = false;
23142       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23143         {
23144           /* Something went quite wrong.  There should be a colon
23145              here, but there is not.  Stop parsing parameters.  */
23146           break;
23147         }
23148       type_name = cp_parser_objc_typename (parser);
23149       /* New ObjC allows attributes on parameters too.  */
23150       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23151         parm_attr = cp_parser_attributes_opt (parser);
23152       identifier = cp_parser_identifier (parser);
23153
23154       params
23155         = chainon (params,
23156                    objc_build_keyword_decl (selector,
23157                                             type_name,
23158                                             identifier,
23159                                             parm_attr));
23160
23161       token = cp_lexer_peek_token (parser->lexer);
23162     }
23163
23164   if (params == NULL_TREE)
23165     {
23166       cp_parser_error (parser, "objective-c++ method declaration is expected");
23167       return error_mark_node;
23168     }
23169
23170   /* We allow tail attributes for the method.  */
23171   if (token->keyword == RID_ATTRIBUTE)
23172     {
23173       *attributes = cp_parser_attributes_opt (parser);
23174       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23175           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23176         return params;
23177       cp_parser_error (parser, 
23178                        "method attributes must be specified at the end");
23179       return error_mark_node;
23180     }
23181
23182   if (params == NULL_TREE)
23183     {
23184       cp_parser_error (parser, "objective-c++ method declaration is expected");
23185       return error_mark_node;
23186     }
23187   return params;
23188 }
23189
23190 /* Parse the non-keyword Objective-C params.  */
23191
23192 static tree
23193 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23194                                        tree* attributes)
23195 {
23196   tree params = make_node (TREE_LIST);
23197   cp_token *token = cp_lexer_peek_token (parser->lexer);
23198   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23199
23200   while (token->type == CPP_COMMA)
23201     {
23202       cp_parameter_declarator *parmdecl;
23203       tree parm;
23204
23205       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23206       token = cp_lexer_peek_token (parser->lexer);
23207
23208       if (token->type == CPP_ELLIPSIS)
23209         {
23210           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23211           *ellipsisp = true;
23212           token = cp_lexer_peek_token (parser->lexer);
23213           break;
23214         }
23215
23216       /* TODO: parse attributes for tail parameters.  */
23217       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23218       parm = grokdeclarator (parmdecl->declarator,
23219                              &parmdecl->decl_specifiers,
23220                              PARM, /*initialized=*/0,
23221                              /*attrlist=*/NULL);
23222
23223       chainon (params, build_tree_list (NULL_TREE, parm));
23224       token = cp_lexer_peek_token (parser->lexer);
23225     }
23226
23227   /* We allow tail attributes for the method.  */
23228   if (token->keyword == RID_ATTRIBUTE)
23229     {
23230       if (*attributes == NULL_TREE)
23231         {
23232           *attributes = cp_parser_attributes_opt (parser);
23233           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23234               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23235             return params;
23236         }
23237       else        
23238         /* We have an error, but parse the attributes, so that we can 
23239            carry on.  */
23240         *attributes = cp_parser_attributes_opt (parser);
23241
23242       cp_parser_error (parser, 
23243                        "method attributes must be specified at the end");
23244       return error_mark_node;
23245     }
23246
23247   return params;
23248 }
23249
23250 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23251
23252 static void
23253 cp_parser_objc_interstitial_code (cp_parser* parser)
23254 {
23255   cp_token *token = cp_lexer_peek_token (parser->lexer);
23256
23257   /* If the next token is `extern' and the following token is a string
23258      literal, then we have a linkage specification.  */
23259   if (token->keyword == RID_EXTERN
23260       && cp_parser_is_pure_string_literal
23261          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23262     cp_parser_linkage_specification (parser);
23263   /* Handle #pragma, if any.  */
23264   else if (token->type == CPP_PRAGMA)
23265     cp_parser_pragma (parser, pragma_external);
23266   /* Allow stray semicolons.  */
23267   else if (token->type == CPP_SEMICOLON)
23268     cp_lexer_consume_token (parser->lexer);
23269   /* Mark methods as optional or required, when building protocols.  */
23270   else if (token->keyword == RID_AT_OPTIONAL)
23271     {
23272       cp_lexer_consume_token (parser->lexer);
23273       objc_set_method_opt (true);
23274     }
23275   else if (token->keyword == RID_AT_REQUIRED)
23276     {
23277       cp_lexer_consume_token (parser->lexer);
23278       objc_set_method_opt (false);
23279     }
23280   else if (token->keyword == RID_NAMESPACE)
23281     cp_parser_namespace_definition (parser);
23282   /* Other stray characters must generate errors.  */
23283   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23284     {
23285       cp_lexer_consume_token (parser->lexer);
23286       error ("stray %qs between Objective-C++ methods",
23287              token->type == CPP_OPEN_BRACE ? "{" : "}");
23288     }
23289   /* Finally, try to parse a block-declaration, or a function-definition.  */
23290   else
23291     cp_parser_block_declaration (parser, /*statement_p=*/false);
23292 }
23293
23294 /* Parse a method signature.  */
23295
23296 static tree
23297 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23298 {
23299   tree rettype, kwdparms, optparms;
23300   bool ellipsis = false;
23301   bool is_class_method;
23302
23303   is_class_method = cp_parser_objc_method_type (parser);
23304   rettype = cp_parser_objc_typename (parser);
23305   *attributes = NULL_TREE;
23306   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23307   if (kwdparms == error_mark_node)
23308     return error_mark_node;
23309   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23310   if (optparms == error_mark_node)
23311     return error_mark_node;
23312
23313   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23314 }
23315
23316 static bool
23317 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23318 {
23319   tree tattr;  
23320   cp_lexer_save_tokens (parser->lexer);
23321   tattr = cp_parser_attributes_opt (parser);
23322   gcc_assert (tattr) ;
23323   
23324   /* If the attributes are followed by a method introducer, this is not allowed.
23325      Dump the attributes and flag the situation.  */
23326   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23327       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23328     return true;
23329
23330   /* Otherwise, the attributes introduce some interstitial code, possibly so
23331      rewind to allow that check.  */
23332   cp_lexer_rollback_tokens (parser->lexer);
23333   return false;  
23334 }
23335
23336 /* Parse an Objective-C method prototype list.  */
23337
23338 static void
23339 cp_parser_objc_method_prototype_list (cp_parser* parser)
23340 {
23341   cp_token *token = cp_lexer_peek_token (parser->lexer);
23342
23343   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23344     {
23345       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23346         {
23347           tree attributes, sig;
23348           bool is_class_method;
23349           if (token->type == CPP_PLUS)
23350             is_class_method = true;
23351           else
23352             is_class_method = false;
23353           sig = cp_parser_objc_method_signature (parser, &attributes);
23354           if (sig == error_mark_node)
23355             {
23356               cp_parser_skip_to_end_of_block_or_statement (parser);
23357               token = cp_lexer_peek_token (parser->lexer);
23358               continue;
23359             }
23360           objc_add_method_declaration (is_class_method, sig, attributes);
23361           cp_parser_consume_semicolon_at_end_of_statement (parser);
23362         }
23363       else if (token->keyword == RID_AT_PROPERTY)
23364         cp_parser_objc_at_property_declaration (parser);
23365       else if (token->keyword == RID_ATTRIBUTE 
23366                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23367         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23368                     OPT_Wattributes, 
23369                     "prefix attributes are ignored for methods");
23370       else
23371         /* Allow for interspersed non-ObjC++ code.  */
23372         cp_parser_objc_interstitial_code (parser);
23373
23374       token = cp_lexer_peek_token (parser->lexer);
23375     }
23376
23377   if (token->type != CPP_EOF)
23378     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23379   else
23380     cp_parser_error (parser, "expected %<@end%>");
23381
23382   objc_finish_interface ();
23383 }
23384
23385 /* Parse an Objective-C method definition list.  */
23386
23387 static void
23388 cp_parser_objc_method_definition_list (cp_parser* parser)
23389 {
23390   cp_token *token = cp_lexer_peek_token (parser->lexer);
23391
23392   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23393     {
23394       tree meth;
23395
23396       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23397         {
23398           cp_token *ptk;
23399           tree sig, attribute;
23400           bool is_class_method;
23401           if (token->type == CPP_PLUS)
23402             is_class_method = true;
23403           else
23404             is_class_method = false;
23405           push_deferring_access_checks (dk_deferred);
23406           sig = cp_parser_objc_method_signature (parser, &attribute);
23407           if (sig == error_mark_node)
23408             {
23409               cp_parser_skip_to_end_of_block_or_statement (parser);
23410               token = cp_lexer_peek_token (parser->lexer);
23411               continue;
23412             }
23413           objc_start_method_definition (is_class_method, sig, attribute,
23414                                         NULL_TREE);
23415
23416           /* For historical reasons, we accept an optional semicolon.  */
23417           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23418             cp_lexer_consume_token (parser->lexer);
23419
23420           ptk = cp_lexer_peek_token (parser->lexer);
23421           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23422                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23423             {
23424               perform_deferred_access_checks ();
23425               stop_deferring_access_checks ();
23426               meth = cp_parser_function_definition_after_declarator (parser,
23427                                                                      false);
23428               pop_deferring_access_checks ();
23429               objc_finish_method_definition (meth);
23430             }
23431         }
23432       /* The following case will be removed once @synthesize is
23433          completely implemented.  */
23434       else if (token->keyword == RID_AT_PROPERTY)
23435         cp_parser_objc_at_property_declaration (parser);
23436       else if (token->keyword == RID_AT_SYNTHESIZE)
23437         cp_parser_objc_at_synthesize_declaration (parser);
23438       else if (token->keyword == RID_AT_DYNAMIC)
23439         cp_parser_objc_at_dynamic_declaration (parser);
23440       else if (token->keyword == RID_ATTRIBUTE 
23441                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23442         warning_at (token->location, OPT_Wattributes,
23443                     "prefix attributes are ignored for methods");
23444       else
23445         /* Allow for interspersed non-ObjC++ code.  */
23446         cp_parser_objc_interstitial_code (parser);
23447
23448       token = cp_lexer_peek_token (parser->lexer);
23449     }
23450
23451   if (token->type != CPP_EOF)
23452     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23453   else
23454     cp_parser_error (parser, "expected %<@end%>");
23455
23456   objc_finish_implementation ();
23457 }
23458
23459 /* Parse Objective-C ivars.  */
23460
23461 static void
23462 cp_parser_objc_class_ivars (cp_parser* parser)
23463 {
23464   cp_token *token = cp_lexer_peek_token (parser->lexer);
23465
23466   if (token->type != CPP_OPEN_BRACE)
23467     return;     /* No ivars specified.  */
23468
23469   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23470   token = cp_lexer_peek_token (parser->lexer);
23471
23472   while (token->type != CPP_CLOSE_BRACE 
23473         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23474     {
23475       cp_decl_specifier_seq declspecs;
23476       int decl_class_or_enum_p;
23477       tree prefix_attributes;
23478
23479       cp_parser_objc_visibility_spec (parser);
23480
23481       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23482         break;
23483
23484       cp_parser_decl_specifier_seq (parser,
23485                                     CP_PARSER_FLAGS_OPTIONAL,
23486                                     &declspecs,
23487                                     &decl_class_or_enum_p);
23488
23489       /* auto, register, static, extern, mutable.  */
23490       if (declspecs.storage_class != sc_none)
23491         {
23492           cp_parser_error (parser, "invalid type for instance variable");         
23493           declspecs.storage_class = sc_none;
23494         }
23495
23496       /* __thread.  */
23497       if (declspecs.specs[(int) ds_thread])
23498         {
23499           cp_parser_error (parser, "invalid type for instance variable");
23500           declspecs.specs[(int) ds_thread] = 0;
23501         }
23502       
23503       /* typedef.  */
23504       if (declspecs.specs[(int) ds_typedef])
23505         {
23506           cp_parser_error (parser, "invalid type for instance variable");
23507           declspecs.specs[(int) ds_typedef] = 0;
23508         }
23509
23510       prefix_attributes = declspecs.attributes;
23511       declspecs.attributes = NULL_TREE;
23512
23513       /* Keep going until we hit the `;' at the end of the
23514          declaration.  */
23515       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23516         {
23517           tree width = NULL_TREE, attributes, first_attribute, decl;
23518           cp_declarator *declarator = NULL;
23519           int ctor_dtor_or_conv_p;
23520
23521           /* Check for a (possibly unnamed) bitfield declaration.  */
23522           token = cp_lexer_peek_token (parser->lexer);
23523           if (token->type == CPP_COLON)
23524             goto eat_colon;
23525
23526           if (token->type == CPP_NAME
23527               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23528                   == CPP_COLON))
23529             {
23530               /* Get the name of the bitfield.  */
23531               declarator = make_id_declarator (NULL_TREE,
23532                                                cp_parser_identifier (parser),
23533                                                sfk_none);
23534
23535              eat_colon:
23536               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23537               /* Get the width of the bitfield.  */
23538               width
23539                 = cp_parser_constant_expression (parser,
23540                                                  /*allow_non_constant=*/false,
23541                                                  NULL);
23542             }
23543           else
23544             {
23545               /* Parse the declarator.  */
23546               declarator
23547                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23548                                         &ctor_dtor_or_conv_p,
23549                                         /*parenthesized_p=*/NULL,
23550                                         /*member_p=*/false);
23551             }
23552
23553           /* Look for attributes that apply to the ivar.  */
23554           attributes = cp_parser_attributes_opt (parser);
23555           /* Remember which attributes are prefix attributes and
23556              which are not.  */
23557           first_attribute = attributes;
23558           /* Combine the attributes.  */
23559           attributes = chainon (prefix_attributes, attributes);
23560
23561           if (width)
23562               /* Create the bitfield declaration.  */
23563               decl = grokbitfield (declarator, &declspecs,
23564                                    width,
23565                                    attributes);
23566           else
23567             decl = grokfield (declarator, &declspecs,
23568                               NULL_TREE, /*init_const_expr_p=*/false,
23569                               NULL_TREE, attributes);
23570
23571           /* Add the instance variable.  */
23572           if (decl != error_mark_node && decl != NULL_TREE)
23573             objc_add_instance_variable (decl);
23574
23575           /* Reset PREFIX_ATTRIBUTES.  */
23576           while (attributes && TREE_CHAIN (attributes) != first_attribute)
23577             attributes = TREE_CHAIN (attributes);
23578           if (attributes)
23579             TREE_CHAIN (attributes) = NULL_TREE;
23580
23581           token = cp_lexer_peek_token (parser->lexer);
23582
23583           if (token->type == CPP_COMMA)
23584             {
23585               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23586               continue;
23587             }
23588           break;
23589         }
23590
23591       cp_parser_consume_semicolon_at_end_of_statement (parser);
23592       token = cp_lexer_peek_token (parser->lexer);
23593     }
23594
23595   if (token->keyword == RID_AT_END)
23596     cp_parser_error (parser, "expected %<}%>");
23597
23598   /* Do not consume the RID_AT_END, so it will be read again as terminating
23599      the @interface of @implementation.  */ 
23600   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23601     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
23602     
23603   /* For historical reasons, we accept an optional semicolon.  */
23604   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23605     cp_lexer_consume_token (parser->lexer);
23606 }
23607
23608 /* Parse an Objective-C protocol declaration.  */
23609
23610 static void
23611 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23612 {
23613   tree proto, protorefs;
23614   cp_token *tok;
23615
23616   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23617   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23618     {
23619       tok = cp_lexer_peek_token (parser->lexer);
23620       error_at (tok->location, "identifier expected after %<@protocol%>");
23621       cp_parser_consume_semicolon_at_end_of_statement (parser);
23622       return;
23623     }
23624
23625   /* See if we have a forward declaration or a definition.  */
23626   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23627
23628   /* Try a forward declaration first.  */
23629   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23630     {
23631       while (true)
23632         {
23633           tree id;
23634           
23635           id = cp_parser_identifier (parser);
23636           if (id == error_mark_node)
23637             break;
23638           
23639           objc_declare_protocol (id, attributes);
23640           
23641           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23642             cp_lexer_consume_token (parser->lexer);
23643           else
23644             break;
23645         }
23646       cp_parser_consume_semicolon_at_end_of_statement (parser);
23647     }
23648
23649   /* Ok, we got a full-fledged definition (or at least should).  */
23650   else
23651     {
23652       proto = cp_parser_identifier (parser);
23653       protorefs = cp_parser_objc_protocol_refs_opt (parser);
23654       objc_start_protocol (proto, protorefs, attributes);
23655       cp_parser_objc_method_prototype_list (parser);
23656     }
23657 }
23658
23659 /* Parse an Objective-C superclass or category.  */
23660
23661 static void
23662 cp_parser_objc_superclass_or_category (cp_parser *parser, 
23663                                        bool iface_p,
23664                                        tree *super,
23665                                        tree *categ, bool *is_class_extension)
23666 {
23667   cp_token *next = cp_lexer_peek_token (parser->lexer);
23668
23669   *super = *categ = NULL_TREE;
23670   *is_class_extension = false;
23671   if (next->type == CPP_COLON)
23672     {
23673       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23674       *super = cp_parser_identifier (parser);
23675     }
23676   else if (next->type == CPP_OPEN_PAREN)
23677     {
23678       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23679
23680       /* If there is no category name, and this is an @interface, we
23681          have a class extension.  */
23682       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23683         {
23684           *categ = NULL_TREE;
23685           *is_class_extension = true;
23686         }
23687       else
23688         *categ = cp_parser_identifier (parser);
23689
23690       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23691     }
23692 }
23693
23694 /* Parse an Objective-C class interface.  */
23695
23696 static void
23697 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23698 {
23699   tree name, super, categ, protos;
23700   bool is_class_extension;
23701
23702   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
23703   name = cp_parser_identifier (parser);
23704   if (name == error_mark_node)
23705     {
23706       /* It's hard to recover because even if valid @interface stuff
23707          is to follow, we can't compile it (or validate it) if we
23708          don't even know which class it refers to.  Let's assume this
23709          was a stray '@interface' token in the stream and skip it.
23710       */
23711       return;
23712     }
23713   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23714                                          &is_class_extension);
23715   protos = cp_parser_objc_protocol_refs_opt (parser);
23716
23717   /* We have either a class or a category on our hands.  */
23718   if (categ || is_class_extension)
23719     objc_start_category_interface (name, categ, protos, attributes);
23720   else
23721     {
23722       objc_start_class_interface (name, super, protos, attributes);
23723       /* Handle instance variable declarations, if any.  */
23724       cp_parser_objc_class_ivars (parser);
23725       objc_continue_interface ();
23726     }
23727
23728   cp_parser_objc_method_prototype_list (parser);
23729 }
23730
23731 /* Parse an Objective-C class implementation.  */
23732
23733 static void
23734 cp_parser_objc_class_implementation (cp_parser* parser)
23735 {
23736   tree name, super, categ;
23737   bool is_class_extension;
23738
23739   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
23740   name = cp_parser_identifier (parser);
23741   if (name == error_mark_node)
23742     {
23743       /* It's hard to recover because even if valid @implementation
23744          stuff is to follow, we can't compile it (or validate it) if
23745          we don't even know which class it refers to.  Let's assume
23746          this was a stray '@implementation' token in the stream and
23747          skip it.
23748       */
23749       return;
23750     }
23751   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23752                                          &is_class_extension);
23753
23754   /* We have either a class or a category on our hands.  */
23755   if (categ)
23756     objc_start_category_implementation (name, categ);
23757   else
23758     {
23759       objc_start_class_implementation (name, super);
23760       /* Handle instance variable declarations, if any.  */
23761       cp_parser_objc_class_ivars (parser);
23762       objc_continue_implementation ();
23763     }
23764
23765   cp_parser_objc_method_definition_list (parser);
23766 }
23767
23768 /* Consume the @end token and finish off the implementation.  */
23769
23770 static void
23771 cp_parser_objc_end_implementation (cp_parser* parser)
23772 {
23773   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23774   objc_finish_implementation ();
23775 }
23776
23777 /* Parse an Objective-C declaration.  */
23778
23779 static void
23780 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23781 {
23782   /* Try to figure out what kind of declaration is present.  */
23783   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23784
23785   if (attributes)
23786     switch (kwd->keyword)
23787       {
23788         case RID_AT_ALIAS:
23789         case RID_AT_CLASS:
23790         case RID_AT_END:
23791           error_at (kwd->location, "attributes may not be specified before"
23792                     " the %<@%D%> Objective-C++ keyword",
23793                     kwd->u.value);
23794           attributes = NULL;
23795           break;
23796         case RID_AT_IMPLEMENTATION:
23797           warning_at (kwd->location, OPT_Wattributes,
23798                       "prefix attributes are ignored before %<@%D%>",
23799                       kwd->u.value);
23800           attributes = NULL;
23801         default:
23802           break;
23803       }
23804
23805   switch (kwd->keyword)
23806     {
23807     case RID_AT_ALIAS:
23808       cp_parser_objc_alias_declaration (parser);
23809       break;
23810     case RID_AT_CLASS:
23811       cp_parser_objc_class_declaration (parser);
23812       break;
23813     case RID_AT_PROTOCOL:
23814       cp_parser_objc_protocol_declaration (parser, attributes);
23815       break;
23816     case RID_AT_INTERFACE:
23817       cp_parser_objc_class_interface (parser, attributes);
23818       break;
23819     case RID_AT_IMPLEMENTATION:
23820       cp_parser_objc_class_implementation (parser);
23821       break;
23822     case RID_AT_END:
23823       cp_parser_objc_end_implementation (parser);
23824       break;
23825     default:
23826       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23827                 kwd->u.value);
23828       cp_parser_skip_to_end_of_block_or_statement (parser);
23829     }
23830 }
23831
23832 /* Parse an Objective-C try-catch-finally statement.
23833
23834    objc-try-catch-finally-stmt:
23835      @try compound-statement objc-catch-clause-seq [opt]
23836        objc-finally-clause [opt]
23837
23838    objc-catch-clause-seq:
23839      objc-catch-clause objc-catch-clause-seq [opt]
23840
23841    objc-catch-clause:
23842      @catch ( objc-exception-declaration ) compound-statement
23843
23844    objc-finally-clause:
23845      @finally compound-statement
23846
23847    objc-exception-declaration:
23848      parameter-declaration
23849      '...'
23850
23851    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
23852
23853    Returns NULL_TREE.
23854
23855    PS: This function is identical to c_parser_objc_try_catch_finally_statement
23856    for C.  Keep them in sync.  */   
23857
23858 static tree
23859 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
23860 {
23861   location_t location;
23862   tree stmt;
23863
23864   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23865   location = cp_lexer_peek_token (parser->lexer)->location;
23866   objc_maybe_warn_exceptions (location);
23867   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
23868      node, lest it get absorbed into the surrounding block.  */
23869   stmt = push_stmt_list ();
23870   cp_parser_compound_statement (parser, NULL, false, false);
23871   objc_begin_try_stmt (location, pop_stmt_list (stmt));
23872
23873   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
23874     {
23875       cp_parameter_declarator *parm;
23876       tree parameter_declaration = error_mark_node;
23877       bool seen_open_paren = false;
23878
23879       cp_lexer_consume_token (parser->lexer);
23880       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23881         seen_open_paren = true;
23882       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23883         {
23884           /* We have "@catch (...)" (where the '...' are literally
23885              what is in the code).  Skip the '...'.
23886              parameter_declaration is set to NULL_TREE, and
23887              objc_being_catch_clauses() knows that that means
23888              '...'.  */
23889           cp_lexer_consume_token (parser->lexer);
23890           parameter_declaration = NULL_TREE;
23891         }
23892       else
23893         {
23894           /* We have "@catch (NSException *exception)" or something
23895              like that.  Parse the parameter declaration.  */
23896           parm = cp_parser_parameter_declaration (parser, false, NULL);
23897           if (parm == NULL)
23898             parameter_declaration = error_mark_node;
23899           else
23900             parameter_declaration = grokdeclarator (parm->declarator,
23901                                                     &parm->decl_specifiers,
23902                                                     PARM, /*initialized=*/0,
23903                                                     /*attrlist=*/NULL);
23904         }
23905       if (seen_open_paren)
23906         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23907       else
23908         {
23909           /* If there was no open parenthesis, we are recovering from
23910              an error, and we are trying to figure out what mistake
23911              the user has made.  */
23912
23913           /* If there is an immediate closing parenthesis, the user
23914              probably forgot the opening one (ie, they typed "@catch
23915              NSException *e)".  Parse the closing parenthesis and keep
23916              going.  */
23917           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23918             cp_lexer_consume_token (parser->lexer);
23919           
23920           /* If these is no immediate closing parenthesis, the user
23921              probably doesn't know that parenthesis are required at
23922              all (ie, they typed "@catch NSException *e").  So, just
23923              forget about the closing parenthesis and keep going.  */
23924         }
23925       objc_begin_catch_clause (parameter_declaration);
23926       cp_parser_compound_statement (parser, NULL, false, false);
23927       objc_finish_catch_clause ();
23928     }
23929   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23930     {
23931       cp_lexer_consume_token (parser->lexer);
23932       location = cp_lexer_peek_token (parser->lexer)->location;
23933       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23934          node, lest it get absorbed into the surrounding block.  */
23935       stmt = push_stmt_list ();
23936       cp_parser_compound_statement (parser, NULL, false, false);
23937       objc_build_finally_clause (location, pop_stmt_list (stmt));
23938     }
23939
23940   return objc_finish_try_stmt ();
23941 }
23942
23943 /* Parse an Objective-C synchronized statement.
23944
23945    objc-synchronized-stmt:
23946      @synchronized ( expression ) compound-statement
23947
23948    Returns NULL_TREE.  */
23949
23950 static tree
23951 cp_parser_objc_synchronized_statement (cp_parser *parser)
23952 {
23953   location_t location;
23954   tree lock, stmt;
23955
23956   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23957
23958   location = cp_lexer_peek_token (parser->lexer)->location;
23959   objc_maybe_warn_exceptions (location);
23960   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23961   lock = cp_parser_expression (parser, false, NULL);
23962   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23963
23964   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23965      node, lest it get absorbed into the surrounding block.  */
23966   stmt = push_stmt_list ();
23967   cp_parser_compound_statement (parser, NULL, false, false);
23968
23969   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23970 }
23971
23972 /* Parse an Objective-C throw statement.
23973
23974    objc-throw-stmt:
23975      @throw assignment-expression [opt] ;
23976
23977    Returns a constructed '@throw' statement.  */
23978
23979 static tree
23980 cp_parser_objc_throw_statement (cp_parser *parser)
23981 {
23982   tree expr = NULL_TREE;
23983   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23984
23985   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23986
23987   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23988     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23989
23990   cp_parser_consume_semicolon_at_end_of_statement (parser);
23991
23992   return objc_build_throw_stmt (loc, expr);
23993 }
23994
23995 /* Parse an Objective-C statement.  */
23996
23997 static tree
23998 cp_parser_objc_statement (cp_parser * parser)
23999 {
24000   /* Try to figure out what kind of declaration is present.  */
24001   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24002
24003   switch (kwd->keyword)
24004     {
24005     case RID_AT_TRY:
24006       return cp_parser_objc_try_catch_finally_statement (parser);
24007     case RID_AT_SYNCHRONIZED:
24008       return cp_parser_objc_synchronized_statement (parser);
24009     case RID_AT_THROW:
24010       return cp_parser_objc_throw_statement (parser);
24011     default:
24012       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24013                kwd->u.value);
24014       cp_parser_skip_to_end_of_block_or_statement (parser);
24015     }
24016
24017   return error_mark_node;
24018 }
24019
24020 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24021    look ahead to see if an objc keyword follows the attributes.  This
24022    is to detect the use of prefix attributes on ObjC @interface and 
24023    @protocol.  */
24024
24025 static bool
24026 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24027 {
24028   cp_lexer_save_tokens (parser->lexer);
24029   *attrib = cp_parser_attributes_opt (parser);
24030   gcc_assert (*attrib);
24031   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24032     {
24033       cp_lexer_commit_tokens (parser->lexer);
24034       return true;
24035     }
24036   cp_lexer_rollback_tokens (parser->lexer);
24037   return false;  
24038 }
24039
24040 /* This routine is a minimal replacement for
24041    c_parser_struct_declaration () used when parsing the list of
24042    types/names or ObjC++ properties.  For example, when parsing the
24043    code
24044
24045    @property (readonly) int a, b, c;
24046
24047    this function is responsible for parsing "int a, int b, int c" and
24048    returning the declarations as CHAIN of DECLs.
24049
24050    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24051    similar parsing.  */
24052 static tree
24053 cp_parser_objc_struct_declaration (cp_parser *parser)
24054 {
24055   tree decls = NULL_TREE;
24056   cp_decl_specifier_seq declspecs;
24057   int decl_class_or_enum_p;
24058   tree prefix_attributes;
24059
24060   cp_parser_decl_specifier_seq (parser,
24061                                 CP_PARSER_FLAGS_NONE,
24062                                 &declspecs,
24063                                 &decl_class_or_enum_p);
24064
24065   if (declspecs.type == error_mark_node)
24066     return error_mark_node;
24067
24068   /* auto, register, static, extern, mutable.  */
24069   if (declspecs.storage_class != sc_none)
24070     {
24071       cp_parser_error (parser, "invalid type for property");
24072       declspecs.storage_class = sc_none;
24073     }
24074   
24075   /* __thread.  */
24076   if (declspecs.specs[(int) ds_thread])
24077     {
24078       cp_parser_error (parser, "invalid type for property");
24079       declspecs.specs[(int) ds_thread] = 0;
24080     }
24081   
24082   /* typedef.  */
24083   if (declspecs.specs[(int) ds_typedef])
24084     {
24085       cp_parser_error (parser, "invalid type for property");
24086       declspecs.specs[(int) ds_typedef] = 0;
24087     }
24088
24089   prefix_attributes = declspecs.attributes;
24090   declspecs.attributes = NULL_TREE;
24091
24092   /* Keep going until we hit the `;' at the end of the declaration. */
24093   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24094     {
24095       tree attributes, first_attribute, decl;
24096       cp_declarator *declarator;
24097       cp_token *token;
24098
24099       /* Parse the declarator.  */
24100       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24101                                          NULL, NULL, false);
24102
24103       /* Look for attributes that apply to the ivar.  */
24104       attributes = cp_parser_attributes_opt (parser);
24105       /* Remember which attributes are prefix attributes and
24106          which are not.  */
24107       first_attribute = attributes;
24108       /* Combine the attributes.  */
24109       attributes = chainon (prefix_attributes, attributes);
24110       
24111       decl = grokfield (declarator, &declspecs,
24112                         NULL_TREE, /*init_const_expr_p=*/false,
24113                         NULL_TREE, attributes);
24114
24115       if (decl == error_mark_node || decl == NULL_TREE)
24116         return error_mark_node;
24117       
24118       /* Reset PREFIX_ATTRIBUTES.  */
24119       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24120         attributes = TREE_CHAIN (attributes);
24121       if (attributes)
24122         TREE_CHAIN (attributes) = NULL_TREE;
24123
24124       DECL_CHAIN (decl) = decls;
24125       decls = decl;
24126
24127       token = cp_lexer_peek_token (parser->lexer);
24128       if (token->type == CPP_COMMA)
24129         {
24130           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24131           continue;
24132         }
24133       else
24134         break;
24135     }
24136   return decls;
24137 }
24138
24139 /* Parse an Objective-C @property declaration.  The syntax is:
24140
24141    objc-property-declaration:
24142      '@property' objc-property-attributes[opt] struct-declaration ;
24143
24144    objc-property-attributes:
24145     '(' objc-property-attribute-list ')'
24146
24147    objc-property-attribute-list:
24148      objc-property-attribute
24149      objc-property-attribute-list, objc-property-attribute
24150
24151    objc-property-attribute
24152      'getter' = identifier
24153      'setter' = identifier
24154      'readonly'
24155      'readwrite'
24156      'assign'
24157      'retain'
24158      'copy'
24159      'nonatomic'
24160
24161   For example:
24162     @property NSString *name;
24163     @property (readonly) id object;
24164     @property (retain, nonatomic, getter=getTheName) id name;
24165     @property int a, b, c;
24166
24167    PS: This function is identical to
24168    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24169 static void 
24170 cp_parser_objc_at_property_declaration (cp_parser *parser)
24171 {
24172   /* The following variables hold the attributes of the properties as
24173      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24174      seen.  When we see an attribute, we set them to 'true' (if they
24175      are boolean properties) or to the identifier (if they have an
24176      argument, ie, for getter and setter).  Note that here we only
24177      parse the list of attributes, check the syntax and accumulate the
24178      attributes that we find.  objc_add_property_declaration() will
24179      then process the information.  */
24180   bool property_assign = false;
24181   bool property_copy = false;
24182   tree property_getter_ident = NULL_TREE;
24183   bool property_nonatomic = false;
24184   bool property_readonly = false;
24185   bool property_readwrite = false;
24186   bool property_retain = false;
24187   tree property_setter_ident = NULL_TREE;
24188
24189   /* 'properties' is the list of properties that we read.  Usually a
24190      single one, but maybe more (eg, in "@property int a, b, c;" there
24191      are three).  */
24192   tree properties;
24193   location_t loc;
24194
24195   loc = cp_lexer_peek_token (parser->lexer)->location;
24196
24197   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24198
24199   /* Parse the optional attribute list...  */
24200   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24201     {
24202       /* Eat the '('.  */
24203       cp_lexer_consume_token (parser->lexer);
24204
24205       while (true)
24206         {
24207           bool syntax_error = false;
24208           cp_token *token = cp_lexer_peek_token (parser->lexer);
24209           enum rid keyword;
24210
24211           if (token->type != CPP_NAME)
24212             {
24213               cp_parser_error (parser, "expected identifier");
24214               break;
24215             }
24216           keyword = C_RID_CODE (token->u.value);
24217           cp_lexer_consume_token (parser->lexer);
24218           switch (keyword)
24219             {
24220             case RID_ASSIGN:    property_assign = true;    break;
24221             case RID_COPY:      property_copy = true;      break;
24222             case RID_NONATOMIC: property_nonatomic = true; break;
24223             case RID_READONLY:  property_readonly = true;  break;
24224             case RID_READWRITE: property_readwrite = true; break;
24225             case RID_RETAIN:    property_retain = true;    break;
24226
24227             case RID_GETTER:
24228             case RID_SETTER:
24229               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24230                 {
24231                   if (keyword == RID_GETTER)
24232                     cp_parser_error (parser,
24233                                      "missing %<=%> (after %<getter%> attribute)");
24234                   else
24235                     cp_parser_error (parser,
24236                                      "missing %<=%> (after %<setter%> attribute)");
24237                   syntax_error = true;
24238                   break;
24239                 }
24240               cp_lexer_consume_token (parser->lexer); /* eat the = */
24241               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24242                 {
24243                   cp_parser_error (parser, "expected identifier");
24244                   syntax_error = true;
24245                   break;
24246                 }
24247               if (keyword == RID_SETTER)
24248                 {
24249                   if (property_setter_ident != NULL_TREE)
24250                     {
24251                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24252                       cp_lexer_consume_token (parser->lexer);
24253                     }
24254                   else
24255                     property_setter_ident = cp_parser_objc_selector (parser);
24256                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24257                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24258                   else
24259                     cp_lexer_consume_token (parser->lexer);
24260                 }
24261               else
24262                 {
24263                   if (property_getter_ident != NULL_TREE)
24264                     {
24265                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24266                       cp_lexer_consume_token (parser->lexer);
24267                     }
24268                   else
24269                     property_getter_ident = cp_parser_objc_selector (parser);
24270                 }
24271               break;
24272             default:
24273               cp_parser_error (parser, "unknown property attribute");
24274               syntax_error = true;
24275               break;
24276             }
24277
24278           if (syntax_error)
24279             break;
24280
24281           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24282             cp_lexer_consume_token (parser->lexer);
24283           else
24284             break;
24285         }
24286
24287       /* FIXME: "@property (setter, assign);" will generate a spurious
24288          "error: expected â€˜)’ before â€˜,’ token".  This is because
24289          cp_parser_require, unlike the C counterpart, will produce an
24290          error even if we are in error recovery.  */
24291       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24292         {
24293           cp_parser_skip_to_closing_parenthesis (parser,
24294                                                  /*recovering=*/true,
24295                                                  /*or_comma=*/false,
24296                                                  /*consume_paren=*/true);
24297         }
24298     }
24299
24300   /* ... and the property declaration(s).  */
24301   properties = cp_parser_objc_struct_declaration (parser);
24302
24303   if (properties == error_mark_node)
24304     {
24305       cp_parser_skip_to_end_of_statement (parser);
24306       /* If the next token is now a `;', consume it.  */
24307       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24308         cp_lexer_consume_token (parser->lexer);
24309       return;
24310     }
24311
24312   if (properties == NULL_TREE)
24313     cp_parser_error (parser, "expected identifier");
24314   else
24315     {
24316       /* Comma-separated properties are chained together in
24317          reverse order; add them one by one.  */
24318       properties = nreverse (properties);
24319       
24320       for (; properties; properties = TREE_CHAIN (properties))
24321         objc_add_property_declaration (loc, copy_node (properties),
24322                                        property_readonly, property_readwrite,
24323                                        property_assign, property_retain,
24324                                        property_copy, property_nonatomic,
24325                                        property_getter_ident, property_setter_ident);
24326     }
24327   
24328   cp_parser_consume_semicolon_at_end_of_statement (parser);
24329 }
24330
24331 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24332
24333    objc-synthesize-declaration:
24334      @synthesize objc-synthesize-identifier-list ;
24335
24336    objc-synthesize-identifier-list:
24337      objc-synthesize-identifier
24338      objc-synthesize-identifier-list, objc-synthesize-identifier
24339
24340    objc-synthesize-identifier
24341      identifier
24342      identifier = identifier
24343
24344   For example:
24345     @synthesize MyProperty;
24346     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24347
24348   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24349   for C.  Keep them in sync.
24350 */
24351 static void 
24352 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24353 {
24354   tree list = NULL_TREE;
24355   location_t loc;
24356   loc = cp_lexer_peek_token (parser->lexer)->location;
24357
24358   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24359   while (true)
24360     {
24361       tree property, ivar;
24362       property = cp_parser_identifier (parser);
24363       if (property == error_mark_node)
24364         {
24365           cp_parser_consume_semicolon_at_end_of_statement (parser);
24366           return;
24367         }
24368       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24369         {
24370           cp_lexer_consume_token (parser->lexer);
24371           ivar = cp_parser_identifier (parser);
24372           if (ivar == error_mark_node)
24373             {
24374               cp_parser_consume_semicolon_at_end_of_statement (parser);
24375               return;
24376             }
24377         }
24378       else
24379         ivar = NULL_TREE;
24380       list = chainon (list, build_tree_list (ivar, property));
24381       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24382         cp_lexer_consume_token (parser->lexer);
24383       else
24384         break;
24385     }
24386   cp_parser_consume_semicolon_at_end_of_statement (parser);
24387   objc_add_synthesize_declaration (loc, list);
24388 }
24389
24390 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24391
24392    objc-dynamic-declaration:
24393      @dynamic identifier-list ;
24394
24395    For example:
24396      @dynamic MyProperty;
24397      @dynamic MyProperty, AnotherProperty;
24398
24399   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24400   for C.  Keep them in sync.
24401 */
24402 static void 
24403 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24404 {
24405   tree list = NULL_TREE;
24406   location_t loc;
24407   loc = cp_lexer_peek_token (parser->lexer)->location;
24408
24409   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24410   while (true)
24411     {
24412       tree property;
24413       property = cp_parser_identifier (parser);
24414       if (property == error_mark_node)
24415         {
24416           cp_parser_consume_semicolon_at_end_of_statement (parser);
24417           return;
24418         }
24419       list = chainon (list, build_tree_list (NULL, property));
24420       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24421         cp_lexer_consume_token (parser->lexer);
24422       else
24423         break;
24424     }
24425   cp_parser_consume_semicolon_at_end_of_statement (parser);
24426   objc_add_dynamic_declaration (loc, list);
24427 }
24428
24429 \f
24430 /* OpenMP 2.5 parsing routines.  */
24431
24432 /* Returns name of the next clause.
24433    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24434    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24435    returned and the token is consumed.  */
24436
24437 static pragma_omp_clause
24438 cp_parser_omp_clause_name (cp_parser *parser)
24439 {
24440   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24441
24442   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24443     result = PRAGMA_OMP_CLAUSE_IF;
24444   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24445     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24446   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24447     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24448   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24449     {
24450       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24451       const char *p = IDENTIFIER_POINTER (id);
24452
24453       switch (p[0])
24454         {
24455         case 'c':
24456           if (!strcmp ("collapse", p))
24457             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24458           else if (!strcmp ("copyin", p))
24459             result = PRAGMA_OMP_CLAUSE_COPYIN;
24460           else if (!strcmp ("copyprivate", p))
24461             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24462           break;
24463         case 'f':
24464           if (!strcmp ("final", p))
24465             result = PRAGMA_OMP_CLAUSE_FINAL;
24466           else if (!strcmp ("firstprivate", p))
24467             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24468           break;
24469         case 'l':
24470           if (!strcmp ("lastprivate", p))
24471             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24472           break;
24473         case 'm':
24474           if (!strcmp ("mergeable", p))
24475             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24476           break;
24477         case 'n':
24478           if (!strcmp ("nowait", p))
24479             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24480           else if (!strcmp ("num_threads", p))
24481             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24482           break;
24483         case 'o':
24484           if (!strcmp ("ordered", p))
24485             result = PRAGMA_OMP_CLAUSE_ORDERED;
24486           break;
24487         case 'r':
24488           if (!strcmp ("reduction", p))
24489             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24490           break;
24491         case 's':
24492           if (!strcmp ("schedule", p))
24493             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24494           else if (!strcmp ("shared", p))
24495             result = PRAGMA_OMP_CLAUSE_SHARED;
24496           break;
24497         case 'u':
24498           if (!strcmp ("untied", p))
24499             result = PRAGMA_OMP_CLAUSE_UNTIED;
24500           break;
24501         }
24502     }
24503
24504   if (result != PRAGMA_OMP_CLAUSE_NONE)
24505     cp_lexer_consume_token (parser->lexer);
24506
24507   return result;
24508 }
24509
24510 /* Validate that a clause of the given type does not already exist.  */
24511
24512 static void
24513 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24514                            const char *name, location_t location)
24515 {
24516   tree c;
24517
24518   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24519     if (OMP_CLAUSE_CODE (c) == code)
24520       {
24521         error_at (location, "too many %qs clauses", name);
24522         break;
24523       }
24524 }
24525
24526 /* OpenMP 2.5:
24527    variable-list:
24528      identifier
24529      variable-list , identifier
24530
24531    In addition, we match a closing parenthesis.  An opening parenthesis
24532    will have been consumed by the caller.
24533
24534    If KIND is nonzero, create the appropriate node and install the decl
24535    in OMP_CLAUSE_DECL and add the node to the head of the list.
24536
24537    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24538    return the list created.  */
24539
24540 static tree
24541 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24542                                 tree list)
24543 {
24544   cp_token *token;
24545   while (1)
24546     {
24547       tree name, decl;
24548
24549       token = cp_lexer_peek_token (parser->lexer);
24550       name = cp_parser_id_expression (parser, /*template_p=*/false,
24551                                       /*check_dependency_p=*/true,
24552                                       /*template_p=*/NULL,
24553                                       /*declarator_p=*/false,
24554                                       /*optional_p=*/false);
24555       if (name == error_mark_node)
24556         goto skip_comma;
24557
24558       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24559       if (decl == error_mark_node)
24560         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24561                                      token->location);
24562       else if (kind != 0)
24563         {
24564           tree u = build_omp_clause (token->location, kind);
24565           OMP_CLAUSE_DECL (u) = decl;
24566           OMP_CLAUSE_CHAIN (u) = list;
24567           list = u;
24568         }
24569       else
24570         list = tree_cons (decl, NULL_TREE, list);
24571
24572     get_comma:
24573       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24574         break;
24575       cp_lexer_consume_token (parser->lexer);
24576     }
24577
24578   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24579     {
24580       int ending;
24581
24582       /* Try to resync to an unnested comma.  Copied from
24583          cp_parser_parenthesized_expression_list.  */
24584     skip_comma:
24585       ending = cp_parser_skip_to_closing_parenthesis (parser,
24586                                                       /*recovering=*/true,
24587                                                       /*or_comma=*/true,
24588                                                       /*consume_paren=*/true);
24589       if (ending < 0)
24590         goto get_comma;
24591     }
24592
24593   return list;
24594 }
24595
24596 /* Similarly, but expect leading and trailing parenthesis.  This is a very
24597    common case for omp clauses.  */
24598
24599 static tree
24600 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24601 {
24602   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24603     return cp_parser_omp_var_list_no_open (parser, kind, list);
24604   return list;
24605 }
24606
24607 /* OpenMP 3.0:
24608    collapse ( constant-expression ) */
24609
24610 static tree
24611 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24612 {
24613   tree c, num;
24614   location_t loc;
24615   HOST_WIDE_INT n;
24616
24617   loc = cp_lexer_peek_token (parser->lexer)->location;
24618   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24619     return list;
24620
24621   num = cp_parser_constant_expression (parser, false, NULL);
24622
24623   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24624     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24625                                            /*or_comma=*/false,
24626                                            /*consume_paren=*/true);
24627
24628   if (num == error_mark_node)
24629     return list;
24630   num = fold_non_dependent_expr (num);
24631   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24632       || !host_integerp (num, 0)
24633       || (n = tree_low_cst (num, 0)) <= 0
24634       || (int) n != n)
24635     {
24636       error_at (loc, "collapse argument needs positive constant integer expression");
24637       return list;
24638     }
24639
24640   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24641   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24642   OMP_CLAUSE_CHAIN (c) = list;
24643   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24644
24645   return c;
24646 }
24647
24648 /* OpenMP 2.5:
24649    default ( shared | none ) */
24650
24651 static tree
24652 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24653 {
24654   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24655   tree c;
24656
24657   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24658     return list;
24659   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24660     {
24661       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24662       const char *p = IDENTIFIER_POINTER (id);
24663
24664       switch (p[0])
24665         {
24666         case 'n':
24667           if (strcmp ("none", p) != 0)
24668             goto invalid_kind;
24669           kind = OMP_CLAUSE_DEFAULT_NONE;
24670           break;
24671
24672         case 's':
24673           if (strcmp ("shared", p) != 0)
24674             goto invalid_kind;
24675           kind = OMP_CLAUSE_DEFAULT_SHARED;
24676           break;
24677
24678         default:
24679           goto invalid_kind;
24680         }
24681
24682       cp_lexer_consume_token (parser->lexer);
24683     }
24684   else
24685     {
24686     invalid_kind:
24687       cp_parser_error (parser, "expected %<none%> or %<shared%>");
24688     }
24689
24690   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24691     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24692                                            /*or_comma=*/false,
24693                                            /*consume_paren=*/true);
24694
24695   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24696     return list;
24697
24698   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24699   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24700   OMP_CLAUSE_CHAIN (c) = list;
24701   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24702
24703   return c;
24704 }
24705
24706 /* OpenMP 3.1:
24707    final ( expression ) */
24708
24709 static tree
24710 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24711 {
24712   tree t, c;
24713
24714   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24715     return list;
24716
24717   t = cp_parser_condition (parser);
24718
24719   if (t == error_mark_node
24720       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24721     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24722                                            /*or_comma=*/false,
24723                                            /*consume_paren=*/true);
24724
24725   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24726
24727   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24728   OMP_CLAUSE_FINAL_EXPR (c) = t;
24729   OMP_CLAUSE_CHAIN (c) = list;
24730
24731   return c;
24732 }
24733
24734 /* OpenMP 2.5:
24735    if ( expression ) */
24736
24737 static tree
24738 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24739 {
24740   tree t, c;
24741
24742   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24743     return list;
24744
24745   t = cp_parser_condition (parser);
24746
24747   if (t == error_mark_node
24748       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24749     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24750                                            /*or_comma=*/false,
24751                                            /*consume_paren=*/true);
24752
24753   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24754
24755   c = build_omp_clause (location, OMP_CLAUSE_IF);
24756   OMP_CLAUSE_IF_EXPR (c) = t;
24757   OMP_CLAUSE_CHAIN (c) = list;
24758
24759   return c;
24760 }
24761
24762 /* OpenMP 3.1:
24763    mergeable */
24764
24765 static tree
24766 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24767                                 tree list, location_t location)
24768 {
24769   tree c;
24770
24771   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24772                              location);
24773
24774   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24775   OMP_CLAUSE_CHAIN (c) = list;
24776   return c;
24777 }
24778
24779 /* OpenMP 2.5:
24780    nowait */
24781
24782 static tree
24783 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24784                              tree list, location_t location)
24785 {
24786   tree c;
24787
24788   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
24789
24790   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
24791   OMP_CLAUSE_CHAIN (c) = list;
24792   return c;
24793 }
24794
24795 /* OpenMP 2.5:
24796    num_threads ( expression ) */
24797
24798 static tree
24799 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
24800                                   location_t location)
24801 {
24802   tree t, c;
24803
24804   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24805     return list;
24806
24807   t = cp_parser_expression (parser, false, NULL);
24808
24809   if (t == error_mark_node
24810       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24811     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24812                                            /*or_comma=*/false,
24813                                            /*consume_paren=*/true);
24814
24815   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
24816                              "num_threads", location);
24817
24818   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
24819   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
24820   OMP_CLAUSE_CHAIN (c) = list;
24821
24822   return c;
24823 }
24824
24825 /* OpenMP 2.5:
24826    ordered */
24827
24828 static tree
24829 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
24830                               tree list, location_t location)
24831 {
24832   tree c;
24833
24834   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
24835                              "ordered", location);
24836
24837   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
24838   OMP_CLAUSE_CHAIN (c) = list;
24839   return c;
24840 }
24841
24842 /* OpenMP 2.5:
24843    reduction ( reduction-operator : variable-list )
24844
24845    reduction-operator:
24846      One of: + * - & ^ | && ||
24847
24848    OpenMP 3.1:
24849
24850    reduction-operator:
24851      One of: + * - & ^ | && || min max  */
24852
24853 static tree
24854 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
24855 {
24856   enum tree_code code;
24857   tree nlist, c;
24858
24859   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24860     return list;
24861
24862   switch (cp_lexer_peek_token (parser->lexer)->type)
24863     {
24864     case CPP_PLUS:
24865       code = PLUS_EXPR;
24866       break;
24867     case CPP_MULT:
24868       code = MULT_EXPR;
24869       break;
24870     case CPP_MINUS:
24871       code = MINUS_EXPR;
24872       break;
24873     case CPP_AND:
24874       code = BIT_AND_EXPR;
24875       break;
24876     case CPP_XOR:
24877       code = BIT_XOR_EXPR;
24878       break;
24879     case CPP_OR:
24880       code = BIT_IOR_EXPR;
24881       break;
24882     case CPP_AND_AND:
24883       code = TRUTH_ANDIF_EXPR;
24884       break;
24885     case CPP_OR_OR:
24886       code = TRUTH_ORIF_EXPR;
24887       break;
24888     case CPP_NAME:
24889       {
24890         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24891         const char *p = IDENTIFIER_POINTER (id);
24892
24893         if (strcmp (p, "min") == 0)
24894           {
24895             code = MIN_EXPR;
24896             break;
24897           }
24898         if (strcmp (p, "max") == 0)
24899           {
24900             code = MAX_EXPR;
24901             break;
24902           }
24903       }
24904       /* FALLTHROUGH */
24905     default:
24906       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
24907                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
24908     resync_fail:
24909       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24910                                              /*or_comma=*/false,
24911                                              /*consume_paren=*/true);
24912       return list;
24913     }
24914   cp_lexer_consume_token (parser->lexer);
24915
24916   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24917     goto resync_fail;
24918
24919   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
24920   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
24921     OMP_CLAUSE_REDUCTION_CODE (c) = code;
24922
24923   return nlist;
24924 }
24925
24926 /* OpenMP 2.5:
24927    schedule ( schedule-kind )
24928    schedule ( schedule-kind , expression )
24929
24930    schedule-kind:
24931      static | dynamic | guided | runtime | auto  */
24932
24933 static tree
24934 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
24935 {
24936   tree c, t;
24937
24938   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24939     return list;
24940
24941   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
24942
24943   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24944     {
24945       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24946       const char *p = IDENTIFIER_POINTER (id);
24947
24948       switch (p[0])
24949         {
24950         case 'd':
24951           if (strcmp ("dynamic", p) != 0)
24952             goto invalid_kind;
24953           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
24954           break;
24955
24956         case 'g':
24957           if (strcmp ("guided", p) != 0)
24958             goto invalid_kind;
24959           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
24960           break;
24961
24962         case 'r':
24963           if (strcmp ("runtime", p) != 0)
24964             goto invalid_kind;
24965           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
24966           break;
24967
24968         default:
24969           goto invalid_kind;
24970         }
24971     }
24972   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24973     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24974   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24975     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24976   else
24977     goto invalid_kind;
24978   cp_lexer_consume_token (parser->lexer);
24979
24980   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24981     {
24982       cp_token *token;
24983       cp_lexer_consume_token (parser->lexer);
24984
24985       token = cp_lexer_peek_token (parser->lexer);
24986       t = cp_parser_assignment_expression (parser, false, NULL);
24987
24988       if (t == error_mark_node)
24989         goto resync_fail;
24990       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24991         error_at (token->location, "schedule %<runtime%> does not take "
24992                   "a %<chunk_size%> parameter");
24993       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24994         error_at (token->location, "schedule %<auto%> does not take "
24995                   "a %<chunk_size%> parameter");
24996       else
24997         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
24998
24999       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25000         goto resync_fail;
25001     }
25002   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25003     goto resync_fail;
25004
25005   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25006   OMP_CLAUSE_CHAIN (c) = list;
25007   return c;
25008
25009  invalid_kind:
25010   cp_parser_error (parser, "invalid schedule kind");
25011  resync_fail:
25012   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25013                                          /*or_comma=*/false,
25014                                          /*consume_paren=*/true);
25015   return list;
25016 }
25017
25018 /* OpenMP 3.0:
25019    untied */
25020
25021 static tree
25022 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25023                              tree list, location_t location)
25024 {
25025   tree c;
25026
25027   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25028
25029   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25030   OMP_CLAUSE_CHAIN (c) = list;
25031   return c;
25032 }
25033
25034 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25035    is a bitmask in MASK.  Return the list of clauses found; the result
25036    of clause default goes in *pdefault.  */
25037
25038 static tree
25039 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25040                            const char *where, cp_token *pragma_tok)
25041 {
25042   tree clauses = NULL;
25043   bool first = true;
25044   cp_token *token = NULL;
25045
25046   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25047     {
25048       pragma_omp_clause c_kind;
25049       const char *c_name;
25050       tree prev = clauses;
25051
25052       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25053         cp_lexer_consume_token (parser->lexer);
25054
25055       token = cp_lexer_peek_token (parser->lexer);
25056       c_kind = cp_parser_omp_clause_name (parser);
25057       first = false;
25058
25059       switch (c_kind)
25060         {
25061         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25062           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25063                                                    token->location);
25064           c_name = "collapse";
25065           break;
25066         case PRAGMA_OMP_CLAUSE_COPYIN:
25067           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25068           c_name = "copyin";
25069           break;
25070         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25071           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25072                                             clauses);
25073           c_name = "copyprivate";
25074           break;
25075         case PRAGMA_OMP_CLAUSE_DEFAULT:
25076           clauses = cp_parser_omp_clause_default (parser, clauses,
25077                                                   token->location);
25078           c_name = "default";
25079           break;
25080         case PRAGMA_OMP_CLAUSE_FINAL:
25081           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25082           c_name = "final";
25083           break;
25084         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25085           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25086                                             clauses);
25087           c_name = "firstprivate";
25088           break;
25089         case PRAGMA_OMP_CLAUSE_IF:
25090           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25091           c_name = "if";
25092           break;
25093         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25094           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25095                                             clauses);
25096           c_name = "lastprivate";
25097           break;
25098         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25099           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25100                                                     token->location);
25101           c_name = "mergeable";
25102           break;
25103         case PRAGMA_OMP_CLAUSE_NOWAIT:
25104           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25105           c_name = "nowait";
25106           break;
25107         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25108           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25109                                                       token->location);
25110           c_name = "num_threads";
25111           break;
25112         case PRAGMA_OMP_CLAUSE_ORDERED:
25113           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25114                                                   token->location);
25115           c_name = "ordered";
25116           break;
25117         case PRAGMA_OMP_CLAUSE_PRIVATE:
25118           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25119                                             clauses);
25120           c_name = "private";
25121           break;
25122         case PRAGMA_OMP_CLAUSE_REDUCTION:
25123           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25124           c_name = "reduction";
25125           break;
25126         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25127           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25128                                                    token->location);
25129           c_name = "schedule";
25130           break;
25131         case PRAGMA_OMP_CLAUSE_SHARED:
25132           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25133                                             clauses);
25134           c_name = "shared";
25135           break;
25136         case PRAGMA_OMP_CLAUSE_UNTIED:
25137           clauses = cp_parser_omp_clause_untied (parser, clauses,
25138                                                  token->location);
25139           c_name = "nowait";
25140           break;
25141         default:
25142           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25143           goto saw_error;
25144         }
25145
25146       if (((mask >> c_kind) & 1) == 0)
25147         {
25148           /* Remove the invalid clause(s) from the list to avoid
25149              confusing the rest of the compiler.  */
25150           clauses = prev;
25151           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25152         }
25153     }
25154  saw_error:
25155   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25156   return finish_omp_clauses (clauses);
25157 }
25158
25159 /* OpenMP 2.5:
25160    structured-block:
25161      statement
25162
25163    In practice, we're also interested in adding the statement to an
25164    outer node.  So it is convenient if we work around the fact that
25165    cp_parser_statement calls add_stmt.  */
25166
25167 static unsigned
25168 cp_parser_begin_omp_structured_block (cp_parser *parser)
25169 {
25170   unsigned save = parser->in_statement;
25171
25172   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25173      This preserves the "not within loop or switch" style error messages
25174      for nonsense cases like
25175         void foo() {
25176         #pragma omp single
25177           break;
25178         }
25179   */
25180   if (parser->in_statement)
25181     parser->in_statement = IN_OMP_BLOCK;
25182
25183   return save;
25184 }
25185
25186 static void
25187 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25188 {
25189   parser->in_statement = save;
25190 }
25191
25192 static tree
25193 cp_parser_omp_structured_block (cp_parser *parser)
25194 {
25195   tree stmt = begin_omp_structured_block ();
25196   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25197
25198   cp_parser_statement (parser, NULL_TREE, false, NULL);
25199
25200   cp_parser_end_omp_structured_block (parser, save);
25201   return finish_omp_structured_block (stmt);
25202 }
25203
25204 /* OpenMP 2.5:
25205    # pragma omp atomic new-line
25206      expression-stmt
25207
25208    expression-stmt:
25209      x binop= expr | x++ | ++x | x-- | --x
25210    binop:
25211      +, *, -, /, &, ^, |, <<, >>
25212
25213   where x is an lvalue expression with scalar type.
25214
25215    OpenMP 3.1:
25216    # pragma omp atomic new-line
25217      update-stmt
25218
25219    # pragma omp atomic read new-line
25220      read-stmt
25221
25222    # pragma omp atomic write new-line
25223      write-stmt
25224
25225    # pragma omp atomic update new-line
25226      update-stmt
25227
25228    # pragma omp atomic capture new-line
25229      capture-stmt
25230
25231    # pragma omp atomic capture new-line
25232      capture-block
25233
25234    read-stmt:
25235      v = x
25236    write-stmt:
25237      x = expr
25238    update-stmt:
25239      expression-stmt | x = x binop expr
25240    capture-stmt:
25241      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25242    capture-block:
25243      { v = x; update-stmt; } | { update-stmt; v = x; }
25244
25245   where x and v are lvalue expressions with scalar type.  */
25246
25247 static void
25248 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25249 {
25250   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25251   tree rhs1 = NULL_TREE, orig_lhs;
25252   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25253   bool structured_block = false;
25254
25255   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25256     {
25257       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25258       const char *p = IDENTIFIER_POINTER (id);
25259
25260       if (!strcmp (p, "read"))
25261         code = OMP_ATOMIC_READ;
25262       else if (!strcmp (p, "write"))
25263         code = NOP_EXPR;
25264       else if (!strcmp (p, "update"))
25265         code = OMP_ATOMIC;
25266       else if (!strcmp (p, "capture"))
25267         code = OMP_ATOMIC_CAPTURE_NEW;
25268       else
25269         p = NULL;
25270       if (p)
25271         cp_lexer_consume_token (parser->lexer);
25272     }
25273   cp_parser_require_pragma_eol (parser, pragma_tok);
25274
25275   switch (code)
25276     {
25277     case OMP_ATOMIC_READ:
25278     case NOP_EXPR: /* atomic write */
25279       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25280                                       /*cast_p=*/false, NULL);
25281       if (v == error_mark_node)
25282         goto saw_error;
25283       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25284         goto saw_error;
25285       if (code == NOP_EXPR)
25286         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25287       else
25288         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25289                                           /*cast_p=*/false, NULL);
25290       if (lhs == error_mark_node)
25291         goto saw_error;
25292       if (code == NOP_EXPR)
25293         {
25294           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25295              opcode.  */
25296           code = OMP_ATOMIC;
25297           rhs = lhs;
25298           lhs = v;
25299           v = NULL_TREE;
25300         }
25301       goto done;
25302     case OMP_ATOMIC_CAPTURE_NEW:
25303       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25304         {
25305           cp_lexer_consume_token (parser->lexer);
25306           structured_block = true;
25307         }
25308       else
25309         {
25310           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25311                                           /*cast_p=*/false, NULL);
25312           if (v == error_mark_node)
25313             goto saw_error;
25314           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25315             goto saw_error;
25316         }
25317     default:
25318       break;
25319     }
25320
25321 restart:
25322   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25323                                     /*cast_p=*/false, NULL);
25324   orig_lhs = lhs;
25325   switch (TREE_CODE (lhs))
25326     {
25327     case ERROR_MARK:
25328       goto saw_error;
25329
25330     case POSTINCREMENT_EXPR:
25331       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25332         code = OMP_ATOMIC_CAPTURE_OLD;
25333       /* FALLTHROUGH */
25334     case PREINCREMENT_EXPR:
25335       lhs = TREE_OPERAND (lhs, 0);
25336       opcode = PLUS_EXPR;
25337       rhs = integer_one_node;
25338       break;
25339
25340     case POSTDECREMENT_EXPR:
25341       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25342         code = OMP_ATOMIC_CAPTURE_OLD;
25343       /* FALLTHROUGH */
25344     case PREDECREMENT_EXPR:
25345       lhs = TREE_OPERAND (lhs, 0);
25346       opcode = MINUS_EXPR;
25347       rhs = integer_one_node;
25348       break;
25349
25350     case COMPOUND_EXPR:
25351       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25352          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25353          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25354          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25355          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25356                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25357             == BOOLEAN_TYPE)
25358        /* Undo effects of boolean_increment for post {in,de}crement.  */
25359        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25360       /* FALLTHRU */
25361     case MODIFY_EXPR:
25362       if (TREE_CODE (lhs) == MODIFY_EXPR
25363          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25364         {
25365           /* Undo effects of boolean_increment.  */
25366           if (integer_onep (TREE_OPERAND (lhs, 1)))
25367             {
25368               /* This is pre or post increment.  */
25369               rhs = TREE_OPERAND (lhs, 1);
25370               lhs = TREE_OPERAND (lhs, 0);
25371               opcode = NOP_EXPR;
25372               if (code == OMP_ATOMIC_CAPTURE_NEW
25373                   && !structured_block
25374                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25375                 code = OMP_ATOMIC_CAPTURE_OLD;
25376               break;
25377             }
25378         }
25379       /* FALLTHRU */
25380     default:
25381       switch (cp_lexer_peek_token (parser->lexer)->type)
25382         {
25383         case CPP_MULT_EQ:
25384           opcode = MULT_EXPR;
25385           break;
25386         case CPP_DIV_EQ:
25387           opcode = TRUNC_DIV_EXPR;
25388           break;
25389         case CPP_PLUS_EQ:
25390           opcode = PLUS_EXPR;
25391           break;
25392         case CPP_MINUS_EQ:
25393           opcode = MINUS_EXPR;
25394           break;
25395         case CPP_LSHIFT_EQ:
25396           opcode = LSHIFT_EXPR;
25397           break;
25398         case CPP_RSHIFT_EQ:
25399           opcode = RSHIFT_EXPR;
25400           break;
25401         case CPP_AND_EQ:
25402           opcode = BIT_AND_EXPR;
25403           break;
25404         case CPP_OR_EQ:
25405           opcode = BIT_IOR_EXPR;
25406           break;
25407         case CPP_XOR_EQ:
25408           opcode = BIT_XOR_EXPR;
25409           break;
25410         case CPP_EQ:
25411           if (structured_block || code == OMP_ATOMIC)
25412             {
25413               enum cp_parser_prec oprec;
25414               cp_token *token;
25415               cp_lexer_consume_token (parser->lexer);
25416               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25417                                                  /*cast_p=*/false, NULL);
25418               if (rhs1 == error_mark_node)
25419                 goto saw_error;
25420               token = cp_lexer_peek_token (parser->lexer);
25421               switch (token->type)
25422                 {
25423                 case CPP_SEMICOLON:
25424                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25425                     {
25426                       code = OMP_ATOMIC_CAPTURE_OLD;
25427                       v = lhs;
25428                       lhs = NULL_TREE;
25429                       lhs1 = rhs1;
25430                       rhs1 = NULL_TREE;
25431                       cp_lexer_consume_token (parser->lexer);
25432                       goto restart;
25433                     }
25434                   cp_parser_error (parser,
25435                                    "invalid form of %<#pragma omp atomic%>");
25436                   goto saw_error;
25437                 case CPP_MULT:
25438                   opcode = MULT_EXPR;
25439                   break;
25440                 case CPP_DIV:
25441                   opcode = TRUNC_DIV_EXPR;
25442                   break;
25443                 case CPP_PLUS:
25444                   opcode = PLUS_EXPR;
25445                   break;
25446                 case CPP_MINUS:
25447                   opcode = MINUS_EXPR;
25448                   break;
25449                 case CPP_LSHIFT:
25450                   opcode = LSHIFT_EXPR;
25451                   break;
25452                 case CPP_RSHIFT:
25453                   opcode = RSHIFT_EXPR;
25454                   break;
25455                 case CPP_AND:
25456                   opcode = BIT_AND_EXPR;
25457                   break;
25458                 case CPP_OR:
25459                   opcode = BIT_IOR_EXPR;
25460                   break;
25461                 case CPP_XOR:
25462                   opcode = BIT_XOR_EXPR;
25463                   break;
25464                 default:
25465                   cp_parser_error (parser,
25466                                    "invalid operator for %<#pragma omp atomic%>");
25467                   goto saw_error;
25468                 }
25469               oprec = TOKEN_PRECEDENCE (token);
25470               gcc_assert (oprec != PREC_NOT_OPERATOR);
25471               if (commutative_tree_code (opcode))
25472                 oprec = (enum cp_parser_prec) (oprec - 1);
25473               cp_lexer_consume_token (parser->lexer);
25474               rhs = cp_parser_binary_expression (parser, false, false,
25475                                                  oprec, NULL);
25476               if (rhs == error_mark_node)
25477                 goto saw_error;
25478               goto stmt_done;
25479             }
25480           /* FALLTHROUGH */
25481         default:
25482           cp_parser_error (parser,
25483                            "invalid operator for %<#pragma omp atomic%>");
25484           goto saw_error;
25485         }
25486       cp_lexer_consume_token (parser->lexer);
25487
25488       rhs = cp_parser_expression (parser, false, NULL);
25489       if (rhs == error_mark_node)
25490         goto saw_error;
25491       break;
25492     }
25493 stmt_done:
25494   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25495     {
25496       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25497         goto saw_error;
25498       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25499                                       /*cast_p=*/false, NULL);
25500       if (v == error_mark_node)
25501         goto saw_error;
25502       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25503         goto saw_error;
25504       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25505                                          /*cast_p=*/false, NULL);
25506       if (lhs1 == error_mark_node)
25507         goto saw_error;
25508     }
25509   if (structured_block)
25510     {
25511       cp_parser_consume_semicolon_at_end_of_statement (parser);
25512       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25513     }
25514 done:
25515   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25516   if (!structured_block)
25517     cp_parser_consume_semicolon_at_end_of_statement (parser);
25518   return;
25519
25520  saw_error:
25521   cp_parser_skip_to_end_of_block_or_statement (parser);
25522   if (structured_block)
25523     {
25524       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25525         cp_lexer_consume_token (parser->lexer);
25526       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25527         {
25528           cp_parser_skip_to_end_of_block_or_statement (parser);
25529           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25530             cp_lexer_consume_token (parser->lexer);
25531         }
25532     }
25533 }
25534
25535
25536 /* OpenMP 2.5:
25537    # pragma omp barrier new-line  */
25538
25539 static void
25540 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25541 {
25542   cp_parser_require_pragma_eol (parser, pragma_tok);
25543   finish_omp_barrier ();
25544 }
25545
25546 /* OpenMP 2.5:
25547    # pragma omp critical [(name)] new-line
25548      structured-block  */
25549
25550 static tree
25551 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25552 {
25553   tree stmt, name = NULL;
25554
25555   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25556     {
25557       cp_lexer_consume_token (parser->lexer);
25558
25559       name = cp_parser_identifier (parser);
25560
25561       if (name == error_mark_node
25562           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25563         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25564                                                /*or_comma=*/false,
25565                                                /*consume_paren=*/true);
25566       if (name == error_mark_node)
25567         name = NULL;
25568     }
25569   cp_parser_require_pragma_eol (parser, pragma_tok);
25570
25571   stmt = cp_parser_omp_structured_block (parser);
25572   return c_finish_omp_critical (input_location, stmt, name);
25573 }
25574
25575 /* OpenMP 2.5:
25576    # pragma omp flush flush-vars[opt] new-line
25577
25578    flush-vars:
25579      ( variable-list ) */
25580
25581 static void
25582 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25583 {
25584   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25585     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25586   cp_parser_require_pragma_eol (parser, pragma_tok);
25587
25588   finish_omp_flush ();
25589 }
25590
25591 /* Helper function, to parse omp for increment expression.  */
25592
25593 static tree
25594 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25595 {
25596   tree cond = cp_parser_binary_expression (parser, false, true,
25597                                            PREC_NOT_OPERATOR, NULL);
25598   if (cond == error_mark_node
25599       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25600     {
25601       cp_parser_skip_to_end_of_statement (parser);
25602       return error_mark_node;
25603     }
25604
25605   switch (TREE_CODE (cond))
25606     {
25607     case GT_EXPR:
25608     case GE_EXPR:
25609     case LT_EXPR:
25610     case LE_EXPR:
25611       break;
25612     default:
25613       return error_mark_node;
25614     }
25615
25616   /* If decl is an iterator, preserve LHS and RHS of the relational
25617      expr until finish_omp_for.  */
25618   if (decl
25619       && (type_dependent_expression_p (decl)
25620           || CLASS_TYPE_P (TREE_TYPE (decl))))
25621     return cond;
25622
25623   return build_x_binary_op (TREE_CODE (cond),
25624                             TREE_OPERAND (cond, 0), ERROR_MARK,
25625                             TREE_OPERAND (cond, 1), ERROR_MARK,
25626                             /*overload=*/NULL, tf_warning_or_error);
25627 }
25628
25629 /* Helper function, to parse omp for increment expression.  */
25630
25631 static tree
25632 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25633 {
25634   cp_token *token = cp_lexer_peek_token (parser->lexer);
25635   enum tree_code op;
25636   tree lhs, rhs;
25637   cp_id_kind idk;
25638   bool decl_first;
25639
25640   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25641     {
25642       op = (token->type == CPP_PLUS_PLUS
25643             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25644       cp_lexer_consume_token (parser->lexer);
25645       lhs = cp_parser_cast_expression (parser, false, false, NULL);
25646       if (lhs != decl)
25647         return error_mark_node;
25648       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25649     }
25650
25651   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25652   if (lhs != decl)
25653     return error_mark_node;
25654
25655   token = cp_lexer_peek_token (parser->lexer);
25656   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25657     {
25658       op = (token->type == CPP_PLUS_PLUS
25659             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25660       cp_lexer_consume_token (parser->lexer);
25661       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25662     }
25663
25664   op = cp_parser_assignment_operator_opt (parser);
25665   if (op == ERROR_MARK)
25666     return error_mark_node;
25667
25668   if (op != NOP_EXPR)
25669     {
25670       rhs = cp_parser_assignment_expression (parser, false, NULL);
25671       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25672       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25673     }
25674
25675   lhs = cp_parser_binary_expression (parser, false, false,
25676                                      PREC_ADDITIVE_EXPRESSION, NULL);
25677   token = cp_lexer_peek_token (parser->lexer);
25678   decl_first = lhs == decl;
25679   if (decl_first)
25680     lhs = NULL_TREE;
25681   if (token->type != CPP_PLUS
25682       && token->type != CPP_MINUS)
25683     return error_mark_node;
25684
25685   do
25686     {
25687       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25688       cp_lexer_consume_token (parser->lexer);
25689       rhs = cp_parser_binary_expression (parser, false, false,
25690                                          PREC_ADDITIVE_EXPRESSION, NULL);
25691       token = cp_lexer_peek_token (parser->lexer);
25692       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25693         {
25694           if (lhs == NULL_TREE)
25695             {
25696               if (op == PLUS_EXPR)
25697                 lhs = rhs;
25698               else
25699                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25700             }
25701           else
25702             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25703                                      NULL, tf_warning_or_error);
25704         }
25705     }
25706   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25707
25708   if (!decl_first)
25709     {
25710       if (rhs != decl || op == MINUS_EXPR)
25711         return error_mark_node;
25712       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25713     }
25714   else
25715     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25716
25717   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25718 }
25719
25720 /* Parse the restricted form of the for statement allowed by OpenMP.  */
25721
25722 static tree
25723 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25724 {
25725   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25726   tree real_decl, initv, condv, incrv, declv;
25727   tree this_pre_body, cl;
25728   location_t loc_first;
25729   bool collapse_err = false;
25730   int i, collapse = 1, nbraces = 0;
25731   VEC(tree,gc) *for_block = make_tree_vector ();
25732
25733   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25734     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25735       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25736
25737   gcc_assert (collapse >= 1);
25738
25739   declv = make_tree_vec (collapse);
25740   initv = make_tree_vec (collapse);
25741   condv = make_tree_vec (collapse);
25742   incrv = make_tree_vec (collapse);
25743
25744   loc_first = cp_lexer_peek_token (parser->lexer)->location;
25745
25746   for (i = 0; i < collapse; i++)
25747     {
25748       int bracecount = 0;
25749       bool add_private_clause = false;
25750       location_t loc;
25751
25752       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25753         {
25754           cp_parser_error (parser, "for statement expected");
25755           return NULL;
25756         }
25757       loc = cp_lexer_consume_token (parser->lexer)->location;
25758
25759       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25760         return NULL;
25761
25762       init = decl = real_decl = NULL;
25763       this_pre_body = push_stmt_list ();
25764       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25765         {
25766           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25767
25768              init-expr:
25769                        var = lb
25770                        integer-type var = lb
25771                        random-access-iterator-type var = lb
25772                        pointer-type var = lb
25773           */
25774           cp_decl_specifier_seq type_specifiers;
25775
25776           /* First, try to parse as an initialized declaration.  See
25777              cp_parser_condition, from whence the bulk of this is copied.  */
25778
25779           cp_parser_parse_tentatively (parser);
25780           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25781                                         /*is_trailing_return=*/false,
25782                                         &type_specifiers);
25783           if (cp_parser_parse_definitely (parser))
25784             {
25785               /* If parsing a type specifier seq succeeded, then this
25786                  MUST be a initialized declaration.  */
25787               tree asm_specification, attributes;
25788               cp_declarator *declarator;
25789
25790               declarator = cp_parser_declarator (parser,
25791                                                  CP_PARSER_DECLARATOR_NAMED,
25792                                                  /*ctor_dtor_or_conv_p=*/NULL,
25793                                                  /*parenthesized_p=*/NULL,
25794                                                  /*member_p=*/false);
25795               attributes = cp_parser_attributes_opt (parser);
25796               asm_specification = cp_parser_asm_specification_opt (parser);
25797
25798               if (declarator == cp_error_declarator) 
25799                 cp_parser_skip_to_end_of_statement (parser);
25800
25801               else 
25802                 {
25803                   tree pushed_scope, auto_node;
25804
25805                   decl = start_decl (declarator, &type_specifiers,
25806                                      SD_INITIALIZED, attributes,
25807                                      /*prefix_attributes=*/NULL_TREE,
25808                                      &pushed_scope);
25809
25810                   auto_node = type_uses_auto (TREE_TYPE (decl));
25811                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25812                     {
25813                       if (cp_lexer_next_token_is (parser->lexer, 
25814                                                   CPP_OPEN_PAREN))
25815                         error ("parenthesized initialization is not allowed in "
25816                                "OpenMP %<for%> loop");
25817                       else
25818                         /* Trigger an error.  */
25819                         cp_parser_require (parser, CPP_EQ, RT_EQ);
25820
25821                       init = error_mark_node;
25822                       cp_parser_skip_to_end_of_statement (parser);
25823                     }
25824                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
25825                            || type_dependent_expression_p (decl)
25826                            || auto_node)
25827                     {
25828                       bool is_direct_init, is_non_constant_init;
25829
25830                       init = cp_parser_initializer (parser,
25831                                                     &is_direct_init,
25832                                                     &is_non_constant_init);
25833
25834                       if (auto_node)
25835                         {
25836                           TREE_TYPE (decl)
25837                             = do_auto_deduction (TREE_TYPE (decl), init,
25838                                                  auto_node);
25839
25840                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
25841                               && !type_dependent_expression_p (decl))
25842                             goto non_class;
25843                         }
25844                       
25845                       cp_finish_decl (decl, init, !is_non_constant_init,
25846                                       asm_specification,
25847                                       LOOKUP_ONLYCONVERTING);
25848                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
25849                         {
25850                           VEC_safe_push (tree, gc, for_block, this_pre_body);
25851                           init = NULL_TREE;
25852                         }
25853                       else
25854                         init = pop_stmt_list (this_pre_body);
25855                       this_pre_body = NULL_TREE;
25856                     }
25857                   else
25858                     {
25859                       /* Consume '='.  */
25860                       cp_lexer_consume_token (parser->lexer);
25861                       init = cp_parser_assignment_expression (parser, false, NULL);
25862
25863                     non_class:
25864                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
25865                         init = error_mark_node;
25866                       else
25867                         cp_finish_decl (decl, NULL_TREE,
25868                                         /*init_const_expr_p=*/false,
25869                                         asm_specification,
25870                                         LOOKUP_ONLYCONVERTING);
25871                     }
25872
25873                   if (pushed_scope)
25874                     pop_scope (pushed_scope);
25875                 }
25876             }
25877           else 
25878             {
25879               cp_id_kind idk;
25880               /* If parsing a type specifier sequence failed, then
25881                  this MUST be a simple expression.  */
25882               cp_parser_parse_tentatively (parser);
25883               decl = cp_parser_primary_expression (parser, false, false,
25884                                                    false, &idk);
25885               if (!cp_parser_error_occurred (parser)
25886                   && decl
25887                   && DECL_P (decl)
25888                   && CLASS_TYPE_P (TREE_TYPE (decl)))
25889                 {
25890                   tree rhs;
25891
25892                   cp_parser_parse_definitely (parser);
25893                   cp_parser_require (parser, CPP_EQ, RT_EQ);
25894                   rhs = cp_parser_assignment_expression (parser, false, NULL);
25895                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
25896                                                          rhs,
25897                                                          tf_warning_or_error));
25898                   add_private_clause = true;
25899                 }
25900               else
25901                 {
25902                   decl = NULL;
25903                   cp_parser_abort_tentative_parse (parser);
25904                   init = cp_parser_expression (parser, false, NULL);
25905                   if (init)
25906                     {
25907                       if (TREE_CODE (init) == MODIFY_EXPR
25908                           || TREE_CODE (init) == MODOP_EXPR)
25909                         real_decl = TREE_OPERAND (init, 0);
25910                     }
25911                 }
25912             }
25913         }
25914       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25915       if (this_pre_body)
25916         {
25917           this_pre_body = pop_stmt_list (this_pre_body);
25918           if (pre_body)
25919             {
25920               tree t = pre_body;
25921               pre_body = push_stmt_list ();
25922               add_stmt (t);
25923               add_stmt (this_pre_body);
25924               pre_body = pop_stmt_list (pre_body);
25925             }
25926           else
25927             pre_body = this_pre_body;
25928         }
25929
25930       if (decl)
25931         real_decl = decl;
25932       if (par_clauses != NULL && real_decl != NULL_TREE)
25933         {
25934           tree *c;
25935           for (c = par_clauses; *c ; )
25936             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
25937                 && OMP_CLAUSE_DECL (*c) == real_decl)
25938               {
25939                 error_at (loc, "iteration variable %qD"
25940                           " should not be firstprivate", real_decl);
25941                 *c = OMP_CLAUSE_CHAIN (*c);
25942               }
25943             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
25944                      && OMP_CLAUSE_DECL (*c) == real_decl)
25945               {
25946                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
25947                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
25948                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
25949                 OMP_CLAUSE_DECL (l) = real_decl;
25950                 OMP_CLAUSE_CHAIN (l) = clauses;
25951                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
25952                 clauses = l;
25953                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
25954                 CP_OMP_CLAUSE_INFO (*c) = NULL;
25955                 add_private_clause = false;
25956               }
25957             else
25958               {
25959                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
25960                     && OMP_CLAUSE_DECL (*c) == real_decl)
25961                   add_private_clause = false;
25962                 c = &OMP_CLAUSE_CHAIN (*c);
25963               }
25964         }
25965
25966       if (add_private_clause)
25967         {
25968           tree c;
25969           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25970             {
25971               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
25972                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
25973                   && OMP_CLAUSE_DECL (c) == decl)
25974                 break;
25975               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
25976                        && OMP_CLAUSE_DECL (c) == decl)
25977                 error_at (loc, "iteration variable %qD "
25978                           "should not be firstprivate",
25979                           decl);
25980               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
25981                        && OMP_CLAUSE_DECL (c) == decl)
25982                 error_at (loc, "iteration variable %qD should not be reduction",
25983                           decl);
25984             }
25985           if (c == NULL)
25986             {
25987               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
25988               OMP_CLAUSE_DECL (c) = decl;
25989               c = finish_omp_clauses (c);
25990               if (c)
25991                 {
25992                   OMP_CLAUSE_CHAIN (c) = clauses;
25993                   clauses = c;
25994                 }
25995             }
25996         }
25997
25998       cond = NULL;
25999       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26000         cond = cp_parser_omp_for_cond (parser, decl);
26001       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26002
26003       incr = NULL;
26004       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26005         {
26006           /* If decl is an iterator, preserve the operator on decl
26007              until finish_omp_for.  */
26008           if (decl
26009               && ((type_dependent_expression_p (decl)
26010                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
26011                   || CLASS_TYPE_P (TREE_TYPE (decl))))
26012             incr = cp_parser_omp_for_incr (parser, decl);
26013           else
26014             incr = cp_parser_expression (parser, false, NULL);
26015         }
26016
26017       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26018         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26019                                                /*or_comma=*/false,
26020                                                /*consume_paren=*/true);
26021
26022       TREE_VEC_ELT (declv, i) = decl;
26023       TREE_VEC_ELT (initv, i) = init;
26024       TREE_VEC_ELT (condv, i) = cond;
26025       TREE_VEC_ELT (incrv, i) = incr;
26026
26027       if (i == collapse - 1)
26028         break;
26029
26030       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26031          in between the collapsed for loops to be still considered perfectly
26032          nested.  Hopefully the final version clarifies this.
26033          For now handle (multiple) {'s and empty statements.  */
26034       cp_parser_parse_tentatively (parser);
26035       do
26036         {
26037           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26038             break;
26039           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26040             {
26041               cp_lexer_consume_token (parser->lexer);
26042               bracecount++;
26043             }
26044           else if (bracecount
26045                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26046             cp_lexer_consume_token (parser->lexer);
26047           else
26048             {
26049               loc = cp_lexer_peek_token (parser->lexer)->location;
26050               error_at (loc, "not enough collapsed for loops");
26051               collapse_err = true;
26052               cp_parser_abort_tentative_parse (parser);
26053               declv = NULL_TREE;
26054               break;
26055             }
26056         }
26057       while (1);
26058
26059       if (declv)
26060         {
26061           cp_parser_parse_definitely (parser);
26062           nbraces += bracecount;
26063         }
26064     }
26065
26066   /* Note that we saved the original contents of this flag when we entered
26067      the structured block, and so we don't need to re-save it here.  */
26068   parser->in_statement = IN_OMP_FOR;
26069
26070   /* Note that the grammar doesn't call for a structured block here,
26071      though the loop as a whole is a structured block.  */
26072   body = push_stmt_list ();
26073   cp_parser_statement (parser, NULL_TREE, false, NULL);
26074   body = pop_stmt_list (body);
26075
26076   if (declv == NULL_TREE)
26077     ret = NULL_TREE;
26078   else
26079     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26080                           pre_body, clauses);
26081
26082   while (nbraces)
26083     {
26084       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26085         {
26086           cp_lexer_consume_token (parser->lexer);
26087           nbraces--;
26088         }
26089       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26090         cp_lexer_consume_token (parser->lexer);
26091       else
26092         {
26093           if (!collapse_err)
26094             {
26095               error_at (cp_lexer_peek_token (parser->lexer)->location,
26096                         "collapsed loops not perfectly nested");
26097             }
26098           collapse_err = true;
26099           cp_parser_statement_seq_opt (parser, NULL);
26100           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26101             break;
26102         }
26103     }
26104
26105   while (!VEC_empty (tree, for_block))
26106     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26107   release_tree_vector (for_block);
26108
26109   return ret;
26110 }
26111
26112 /* OpenMP 2.5:
26113    #pragma omp for for-clause[optseq] new-line
26114      for-loop  */
26115
26116 #define OMP_FOR_CLAUSE_MASK                             \
26117         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26118         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26119         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26120         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26121         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26122         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26123         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26124         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26125
26126 static tree
26127 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26128 {
26129   tree clauses, sb, ret;
26130   unsigned int save;
26131
26132   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26133                                        "#pragma omp for", pragma_tok);
26134
26135   sb = begin_omp_structured_block ();
26136   save = cp_parser_begin_omp_structured_block (parser);
26137
26138   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26139
26140   cp_parser_end_omp_structured_block (parser, save);
26141   add_stmt (finish_omp_structured_block (sb));
26142
26143   return ret;
26144 }
26145
26146 /* OpenMP 2.5:
26147    # pragma omp master new-line
26148      structured-block  */
26149
26150 static tree
26151 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26152 {
26153   cp_parser_require_pragma_eol (parser, pragma_tok);
26154   return c_finish_omp_master (input_location,
26155                               cp_parser_omp_structured_block (parser));
26156 }
26157
26158 /* OpenMP 2.5:
26159    # pragma omp ordered new-line
26160      structured-block  */
26161
26162 static tree
26163 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26164 {
26165   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26166   cp_parser_require_pragma_eol (parser, pragma_tok);
26167   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26168 }
26169
26170 /* OpenMP 2.5:
26171
26172    section-scope:
26173      { section-sequence }
26174
26175    section-sequence:
26176      section-directive[opt] structured-block
26177      section-sequence section-directive structured-block  */
26178
26179 static tree
26180 cp_parser_omp_sections_scope (cp_parser *parser)
26181 {
26182   tree stmt, substmt;
26183   bool error_suppress = false;
26184   cp_token *tok;
26185
26186   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26187     return NULL_TREE;
26188
26189   stmt = push_stmt_list ();
26190
26191   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26192     {
26193       unsigned save;
26194
26195       substmt = begin_omp_structured_block ();
26196       save = cp_parser_begin_omp_structured_block (parser);
26197
26198       while (1)
26199         {
26200           cp_parser_statement (parser, NULL_TREE, false, NULL);
26201
26202           tok = cp_lexer_peek_token (parser->lexer);
26203           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26204             break;
26205           if (tok->type == CPP_CLOSE_BRACE)
26206             break;
26207           if (tok->type == CPP_EOF)
26208             break;
26209         }
26210
26211       cp_parser_end_omp_structured_block (parser, save);
26212       substmt = finish_omp_structured_block (substmt);
26213       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26214       add_stmt (substmt);
26215     }
26216
26217   while (1)
26218     {
26219       tok = cp_lexer_peek_token (parser->lexer);
26220       if (tok->type == CPP_CLOSE_BRACE)
26221         break;
26222       if (tok->type == CPP_EOF)
26223         break;
26224
26225       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26226         {
26227           cp_lexer_consume_token (parser->lexer);
26228           cp_parser_require_pragma_eol (parser, tok);
26229           error_suppress = false;
26230         }
26231       else if (!error_suppress)
26232         {
26233           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26234           error_suppress = true;
26235         }
26236
26237       substmt = cp_parser_omp_structured_block (parser);
26238       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26239       add_stmt (substmt);
26240     }
26241   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26242
26243   substmt = pop_stmt_list (stmt);
26244
26245   stmt = make_node (OMP_SECTIONS);
26246   TREE_TYPE (stmt) = void_type_node;
26247   OMP_SECTIONS_BODY (stmt) = substmt;
26248
26249   add_stmt (stmt);
26250   return stmt;
26251 }
26252
26253 /* OpenMP 2.5:
26254    # pragma omp sections sections-clause[optseq] newline
26255      sections-scope  */
26256
26257 #define OMP_SECTIONS_CLAUSE_MASK                        \
26258         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26259         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26260         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26261         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26262         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26263
26264 static tree
26265 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26266 {
26267   tree clauses, ret;
26268
26269   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26270                                        "#pragma omp sections", pragma_tok);
26271
26272   ret = cp_parser_omp_sections_scope (parser);
26273   if (ret)
26274     OMP_SECTIONS_CLAUSES (ret) = clauses;
26275
26276   return ret;
26277 }
26278
26279 /* OpenMP 2.5:
26280    # pragma parallel parallel-clause new-line
26281    # pragma parallel for parallel-for-clause new-line
26282    # pragma parallel sections parallel-sections-clause new-line  */
26283
26284 #define OMP_PARALLEL_CLAUSE_MASK                        \
26285         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26286         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26287         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26288         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26289         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26290         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26291         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26292         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26293
26294 static tree
26295 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26296 {
26297   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26298   const char *p_name = "#pragma omp parallel";
26299   tree stmt, clauses, par_clause, ws_clause, block;
26300   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26301   unsigned int save;
26302   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26303
26304   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26305     {
26306       cp_lexer_consume_token (parser->lexer);
26307       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26308       p_name = "#pragma omp parallel for";
26309       mask |= OMP_FOR_CLAUSE_MASK;
26310       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26311     }
26312   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26313     {
26314       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26315       const char *p = IDENTIFIER_POINTER (id);
26316       if (strcmp (p, "sections") == 0)
26317         {
26318           cp_lexer_consume_token (parser->lexer);
26319           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26320           p_name = "#pragma omp parallel sections";
26321           mask |= OMP_SECTIONS_CLAUSE_MASK;
26322           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26323         }
26324     }
26325
26326   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26327   block = begin_omp_parallel ();
26328   save = cp_parser_begin_omp_structured_block (parser);
26329
26330   switch (p_kind)
26331     {
26332     case PRAGMA_OMP_PARALLEL:
26333       cp_parser_statement (parser, NULL_TREE, false, NULL);
26334       par_clause = clauses;
26335       break;
26336
26337     case PRAGMA_OMP_PARALLEL_FOR:
26338       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26339       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26340       break;
26341
26342     case PRAGMA_OMP_PARALLEL_SECTIONS:
26343       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26344       stmt = cp_parser_omp_sections_scope (parser);
26345       if (stmt)
26346         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26347       break;
26348
26349     default:
26350       gcc_unreachable ();
26351     }
26352
26353   cp_parser_end_omp_structured_block (parser, save);
26354   stmt = finish_omp_parallel (par_clause, block);
26355   if (p_kind != PRAGMA_OMP_PARALLEL)
26356     OMP_PARALLEL_COMBINED (stmt) = 1;
26357   return stmt;
26358 }
26359
26360 /* OpenMP 2.5:
26361    # pragma omp single single-clause[optseq] new-line
26362      structured-block  */
26363
26364 #define OMP_SINGLE_CLAUSE_MASK                          \
26365         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26366         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26367         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26368         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26369
26370 static tree
26371 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26372 {
26373   tree stmt = make_node (OMP_SINGLE);
26374   TREE_TYPE (stmt) = void_type_node;
26375
26376   OMP_SINGLE_CLAUSES (stmt)
26377     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26378                                  "#pragma omp single", pragma_tok);
26379   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26380
26381   return add_stmt (stmt);
26382 }
26383
26384 /* OpenMP 3.0:
26385    # pragma omp task task-clause[optseq] new-line
26386      structured-block  */
26387
26388 #define OMP_TASK_CLAUSE_MASK                            \
26389         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26390         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26391         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26392         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26393         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26394         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26395         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26396         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26397
26398 static tree
26399 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26400 {
26401   tree clauses, block;
26402   unsigned int save;
26403
26404   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26405                                        "#pragma omp task", pragma_tok);
26406   block = begin_omp_task ();
26407   save = cp_parser_begin_omp_structured_block (parser);
26408   cp_parser_statement (parser, NULL_TREE, false, NULL);
26409   cp_parser_end_omp_structured_block (parser, save);
26410   return finish_omp_task (clauses, block);
26411 }
26412
26413 /* OpenMP 3.0:
26414    # pragma omp taskwait new-line  */
26415
26416 static void
26417 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26418 {
26419   cp_parser_require_pragma_eol (parser, pragma_tok);
26420   finish_omp_taskwait ();
26421 }
26422
26423 /* OpenMP 3.1:
26424    # pragma omp taskyield new-line  */
26425
26426 static void
26427 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26428 {
26429   cp_parser_require_pragma_eol (parser, pragma_tok);
26430   finish_omp_taskyield ();
26431 }
26432
26433 /* OpenMP 2.5:
26434    # pragma omp threadprivate (variable-list) */
26435
26436 static void
26437 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26438 {
26439   tree vars;
26440
26441   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26442   cp_parser_require_pragma_eol (parser, pragma_tok);
26443
26444   finish_omp_threadprivate (vars);
26445 }
26446
26447 /* Main entry point to OpenMP statement pragmas.  */
26448
26449 static void
26450 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26451 {
26452   tree stmt;
26453
26454   switch (pragma_tok->pragma_kind)
26455     {
26456     case PRAGMA_OMP_ATOMIC:
26457       cp_parser_omp_atomic (parser, pragma_tok);
26458       return;
26459     case PRAGMA_OMP_CRITICAL:
26460       stmt = cp_parser_omp_critical (parser, pragma_tok);
26461       break;
26462     case PRAGMA_OMP_FOR:
26463       stmt = cp_parser_omp_for (parser, pragma_tok);
26464       break;
26465     case PRAGMA_OMP_MASTER:
26466       stmt = cp_parser_omp_master (parser, pragma_tok);
26467       break;
26468     case PRAGMA_OMP_ORDERED:
26469       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26470       break;
26471     case PRAGMA_OMP_PARALLEL:
26472       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26473       break;
26474     case PRAGMA_OMP_SECTIONS:
26475       stmt = cp_parser_omp_sections (parser, pragma_tok);
26476       break;
26477     case PRAGMA_OMP_SINGLE:
26478       stmt = cp_parser_omp_single (parser, pragma_tok);
26479       break;
26480     case PRAGMA_OMP_TASK:
26481       stmt = cp_parser_omp_task (parser, pragma_tok);
26482       break;
26483     default:
26484       gcc_unreachable ();
26485     }
26486
26487   if (stmt)
26488     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26489 }
26490 \f
26491 /* The parser.  */
26492
26493 static GTY (()) cp_parser *the_parser;
26494
26495 \f
26496 /* Special handling for the first token or line in the file.  The first
26497    thing in the file might be #pragma GCC pch_preprocess, which loads a
26498    PCH file, which is a GC collection point.  So we need to handle this
26499    first pragma without benefit of an existing lexer structure.
26500
26501    Always returns one token to the caller in *FIRST_TOKEN.  This is
26502    either the true first token of the file, or the first token after
26503    the initial pragma.  */
26504
26505 static void
26506 cp_parser_initial_pragma (cp_token *first_token)
26507 {
26508   tree name = NULL;
26509
26510   cp_lexer_get_preprocessor_token (NULL, first_token);
26511   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
26512     return;
26513
26514   cp_lexer_get_preprocessor_token (NULL, first_token);
26515   if (first_token->type == CPP_STRING)
26516     {
26517       name = first_token->u.value;
26518
26519       cp_lexer_get_preprocessor_token (NULL, first_token);
26520       if (first_token->type != CPP_PRAGMA_EOL)
26521         error_at (first_token->location,
26522                   "junk at end of %<#pragma GCC pch_preprocess%>");
26523     }
26524   else
26525     error_at (first_token->location, "expected string literal");
26526
26527   /* Skip to the end of the pragma.  */
26528   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
26529     cp_lexer_get_preprocessor_token (NULL, first_token);
26530
26531   /* Now actually load the PCH file.  */
26532   if (name)
26533     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
26534
26535   /* Read one more token to return to our caller.  We have to do this
26536      after reading the PCH file in, since its pointers have to be
26537      live.  */
26538   cp_lexer_get_preprocessor_token (NULL, first_token);
26539 }
26540
26541 /* Normal parsing of a pragma token.  Here we can (and must) use the
26542    regular lexer.  */
26543
26544 static bool
26545 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
26546 {
26547   cp_token *pragma_tok;
26548   unsigned int id;
26549
26550   pragma_tok = cp_lexer_consume_token (parser->lexer);
26551   gcc_assert (pragma_tok->type == CPP_PRAGMA);
26552   parser->lexer->in_pragma = true;
26553
26554   id = pragma_tok->pragma_kind;
26555   switch (id)
26556     {
26557     case PRAGMA_GCC_PCH_PREPROCESS:
26558       error_at (pragma_tok->location,
26559                 "%<#pragma GCC pch_preprocess%> must be first");
26560       break;
26561
26562     case PRAGMA_OMP_BARRIER:
26563       switch (context)
26564         {
26565         case pragma_compound:
26566           cp_parser_omp_barrier (parser, pragma_tok);
26567           return false;
26568         case pragma_stmt:
26569           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
26570                     "used in compound statements");
26571           break;
26572         default:
26573           goto bad_stmt;
26574         }
26575       break;
26576
26577     case PRAGMA_OMP_FLUSH:
26578       switch (context)
26579         {
26580         case pragma_compound:
26581           cp_parser_omp_flush (parser, pragma_tok);
26582           return false;
26583         case pragma_stmt:
26584           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
26585                     "used in compound statements");
26586           break;
26587         default:
26588           goto bad_stmt;
26589         }
26590       break;
26591
26592     case PRAGMA_OMP_TASKWAIT:
26593       switch (context)
26594         {
26595         case pragma_compound:
26596           cp_parser_omp_taskwait (parser, pragma_tok);
26597           return false;
26598         case pragma_stmt:
26599           error_at (pragma_tok->location,
26600                     "%<#pragma omp taskwait%> may only be "
26601                     "used in compound statements");
26602           break;
26603         default:
26604           goto bad_stmt;
26605         }
26606       break;
26607
26608     case PRAGMA_OMP_TASKYIELD:
26609       switch (context)
26610         {
26611         case pragma_compound:
26612           cp_parser_omp_taskyield (parser, pragma_tok);
26613           return false;
26614         case pragma_stmt:
26615           error_at (pragma_tok->location,
26616                     "%<#pragma omp taskyield%> may only be "
26617                     "used in compound statements");
26618           break;
26619         default:
26620           goto bad_stmt;
26621         }
26622       break;
26623
26624     case PRAGMA_OMP_THREADPRIVATE:
26625       cp_parser_omp_threadprivate (parser, pragma_tok);
26626       return false;
26627
26628     case PRAGMA_OMP_ATOMIC:
26629     case PRAGMA_OMP_CRITICAL:
26630     case PRAGMA_OMP_FOR:
26631     case PRAGMA_OMP_MASTER:
26632     case PRAGMA_OMP_ORDERED:
26633     case PRAGMA_OMP_PARALLEL:
26634     case PRAGMA_OMP_SECTIONS:
26635     case PRAGMA_OMP_SINGLE:
26636     case PRAGMA_OMP_TASK:
26637       if (context == pragma_external)
26638         goto bad_stmt;
26639       cp_parser_omp_construct (parser, pragma_tok);
26640       return true;
26641
26642     case PRAGMA_OMP_SECTION:
26643       error_at (pragma_tok->location, 
26644                 "%<#pragma omp section%> may only be used in "
26645                 "%<#pragma omp sections%> construct");
26646       break;
26647
26648     default:
26649       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
26650       c_invoke_pragma_handler (id);
26651       break;
26652
26653     bad_stmt:
26654       cp_parser_error (parser, "expected declaration specifiers");
26655       break;
26656     }
26657
26658   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26659   return false;
26660 }
26661
26662 /* The interface the pragma parsers have to the lexer.  */
26663
26664 enum cpp_ttype
26665 pragma_lex (tree *value)
26666 {
26667   cp_token *tok;
26668   enum cpp_ttype ret;
26669
26670   tok = cp_lexer_peek_token (the_parser->lexer);
26671
26672   ret = tok->type;
26673   *value = tok->u.value;
26674
26675   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
26676     ret = CPP_EOF;
26677   else if (ret == CPP_STRING)
26678     *value = cp_parser_string_literal (the_parser, false, false);
26679   else
26680     {
26681       cp_lexer_consume_token (the_parser->lexer);
26682       if (ret == CPP_KEYWORD)
26683         ret = CPP_NAME;
26684     }
26685
26686   return ret;
26687 }
26688
26689 \f
26690 /* External interface.  */
26691
26692 /* Parse one entire translation unit.  */
26693
26694 void
26695 c_parse_file (void)
26696 {
26697   static bool already_called = false;
26698
26699   if (already_called)
26700     {
26701       sorry ("inter-module optimizations not implemented for C++");
26702       return;
26703     }
26704   already_called = true;
26705
26706   the_parser = cp_parser_new ();
26707   push_deferring_access_checks (flag_access_control
26708                                 ? dk_no_deferred : dk_no_check);
26709   cp_parser_translation_unit (the_parser);
26710   the_parser = NULL;
26711 }
26712
26713 #include "gt-cp-parser.h"