OSDN Git Service

build_string comments
[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
3671 static tree
3672 cp_parser_userdef_string_literal (cp_token *token)
3673 {
3674   tree literal, suffix_id, value;
3675   tree name, decl;
3676   tree result;
3677   VEC(tree,gc) *vec;
3678   int len;
3679
3680   literal = token->u.value;
3681   suffix_id = USERDEF_LITERAL_SUFFIX_ID (literal);
3682   name = cp_literal_operator_id (IDENTIFIER_POINTER (suffix_id));
3683   value = USERDEF_LITERAL_VALUE (literal);
3684   len = TREE_STRING_LENGTH (value) - 1;
3685
3686   /* Build up a call to the user-defined operator  */
3687   /* Lookup the name we got back from the id-expression.  */
3688   vec = make_tree_vector ();
3689   VEC_safe_push (tree, gc, vec, value);
3690   VEC_safe_push (tree, gc, vec, build_int_cst (size_type_node, len));
3691   decl = lookup_function_nonclass (name, vec, /*block_p=*/false);
3692   if (!decl || decl == error_mark_node)
3693     {
3694       error ("unable to find user-defined string literal operator %qD", name);
3695       release_tree_vector (vec);
3696       return error_mark_node;
3697     }
3698   result = finish_call_expr (decl, &vec, false, true, tf_none);
3699   if (result == error_mark_node)
3700     error ("unable to find valid user-defined string literal operator %qD."
3701            "  Possible missing length argument in string literal operator.",
3702            name);
3703   release_tree_vector (vec);
3704
3705   return result;
3706 }
3707
3708
3709 /* Basic concepts [gram.basic]  */
3710
3711 /* Parse a translation-unit.
3712
3713    translation-unit:
3714      declaration-seq [opt]
3715
3716    Returns TRUE if all went well.  */
3717
3718 static bool
3719 cp_parser_translation_unit (cp_parser* parser)
3720 {
3721   /* The address of the first non-permanent object on the declarator
3722      obstack.  */
3723   static void *declarator_obstack_base;
3724
3725   bool success;
3726
3727   /* Create the declarator obstack, if necessary.  */
3728   if (!cp_error_declarator)
3729     {
3730       gcc_obstack_init (&declarator_obstack);
3731       /* Create the error declarator.  */
3732       cp_error_declarator = make_declarator (cdk_error);
3733       /* Create the empty parameter list.  */
3734       no_parameters = make_parameter_declarator (NULL, NULL, NULL_TREE);
3735       /* Remember where the base of the declarator obstack lies.  */
3736       declarator_obstack_base = obstack_next_free (&declarator_obstack);
3737     }
3738
3739   cp_parser_declaration_seq_opt (parser);
3740
3741   /* If there are no tokens left then all went well.  */
3742   if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
3743     {
3744       /* Get rid of the token array; we don't need it any more.  */
3745       cp_lexer_destroy (parser->lexer);
3746       parser->lexer = NULL;
3747
3748       /* This file might have been a context that's implicitly extern
3749          "C".  If so, pop the lang context.  (Only relevant for PCH.) */
3750       if (parser->implicit_extern_c)
3751         {
3752           pop_lang_context ();
3753           parser->implicit_extern_c = false;
3754         }
3755
3756       /* Finish up.  */
3757       finish_translation_unit ();
3758
3759       success = true;
3760     }
3761   else
3762     {
3763       cp_parser_error (parser, "expected declaration");
3764       success = false;
3765     }
3766
3767   /* Make sure the declarator obstack was fully cleaned up.  */
3768   gcc_assert (obstack_next_free (&declarator_obstack)
3769               == declarator_obstack_base);
3770
3771   /* All went well.  */
3772   return success;
3773 }
3774
3775 /* Expressions [gram.expr] */
3776
3777 /* Parse a primary-expression.
3778
3779    primary-expression:
3780      literal
3781      this
3782      ( expression )
3783      id-expression
3784
3785    GNU Extensions:
3786
3787    primary-expression:
3788      ( compound-statement )
3789      __builtin_va_arg ( assignment-expression , type-id )
3790      __builtin_offsetof ( type-id , offsetof-expression )
3791
3792    C++ Extensions:
3793      __has_nothrow_assign ( type-id )   
3794      __has_nothrow_constructor ( type-id )
3795      __has_nothrow_copy ( type-id )
3796      __has_trivial_assign ( type-id )   
3797      __has_trivial_constructor ( type-id )
3798      __has_trivial_copy ( type-id )
3799      __has_trivial_destructor ( type-id )
3800      __has_virtual_destructor ( type-id )     
3801      __is_abstract ( type-id )
3802      __is_base_of ( type-id , type-id )
3803      __is_class ( type-id )
3804      __is_convertible_to ( type-id , type-id )     
3805      __is_empty ( type-id )
3806      __is_enum ( type-id )
3807      __is_literal_type ( type-id )
3808      __is_pod ( type-id )
3809      __is_polymorphic ( type-id )
3810      __is_std_layout ( type-id )
3811      __is_trivial ( type-id )
3812      __is_union ( type-id )
3813
3814    Objective-C++ Extension:
3815
3816    primary-expression:
3817      objc-expression
3818
3819    literal:
3820      __null
3821
3822    ADDRESS_P is true iff this expression was immediately preceded by
3823    "&" and therefore might denote a pointer-to-member.  CAST_P is true
3824    iff this expression is the target of a cast.  TEMPLATE_ARG_P is
3825    true iff this expression is a template argument.
3826
3827    Returns a representation of the expression.  Upon return, *IDK
3828    indicates what kind of id-expression (if any) was present.  */
3829
3830 static tree
3831 cp_parser_primary_expression (cp_parser *parser,
3832                               bool address_p,
3833                               bool cast_p,
3834                               bool template_arg_p,
3835                               cp_id_kind *idk)
3836 {
3837   cp_token *token = NULL;
3838
3839   /* Assume the primary expression is not an id-expression.  */
3840   *idk = CP_ID_KIND_NONE;
3841
3842   /* Peek at the next token.  */
3843   token = cp_lexer_peek_token (parser->lexer);
3844   switch (token->type)
3845     {
3846       /* literal:
3847            integer-literal
3848            character-literal
3849            floating-literal
3850            string-literal
3851            boolean-literal
3852            pointer-literal
3853            user-defined-literal  */
3854     case CPP_CHAR:
3855     case CPP_CHAR16:
3856     case CPP_CHAR32:
3857     case CPP_WCHAR:
3858     case CPP_NUMBER:
3859       if (TREE_CODE (token->u.value) == USERDEF_LITERAL)
3860         return cp_parser_userdef_numeric_literal (parser);
3861       token = cp_lexer_consume_token (parser->lexer);
3862       if (TREE_CODE (token->u.value) == FIXED_CST)
3863         {
3864           error_at (token->location,
3865                     "fixed-point types not supported in C++");
3866           return error_mark_node;
3867         }
3868       /* Floating-point literals are only allowed in an integral
3869          constant expression if they are cast to an integral or
3870          enumeration type.  */
3871       if (TREE_CODE (token->u.value) == REAL_CST
3872           && parser->integral_constant_expression_p
3873           && pedantic)
3874         {
3875           /* CAST_P will be set even in invalid code like "int(2.7 +
3876              ...)".   Therefore, we have to check that the next token
3877              is sure to end the cast.  */
3878           if (cast_p)
3879             {
3880               cp_token *next_token;
3881
3882               next_token = cp_lexer_peek_token (parser->lexer);
3883               if (/* The comma at the end of an
3884                      enumerator-definition.  */
3885                   next_token->type != CPP_COMMA
3886                   /* The curly brace at the end of an enum-specifier.  */
3887                   && next_token->type != CPP_CLOSE_BRACE
3888                   /* The end of a statement.  */
3889                   && next_token->type != CPP_SEMICOLON
3890                   /* The end of the cast-expression.  */
3891                   && next_token->type != CPP_CLOSE_PAREN
3892                   /* The end of an array bound.  */
3893                   && next_token->type != CPP_CLOSE_SQUARE
3894                   /* The closing ">" in a template-argument-list.  */
3895                   && (next_token->type != CPP_GREATER
3896                       || parser->greater_than_is_operator_p)
3897                   /* C++0x only: A ">>" treated like two ">" tokens,
3898                      in a template-argument-list.  */
3899                   && (next_token->type != CPP_RSHIFT
3900                       || (cxx_dialect == cxx98)
3901                       || parser->greater_than_is_operator_p))
3902                 cast_p = false;
3903             }
3904
3905           /* If we are within a cast, then the constraint that the
3906              cast is to an integral or enumeration type will be
3907              checked at that point.  If we are not within a cast, then
3908              this code is invalid.  */
3909           if (!cast_p)
3910             cp_parser_non_integral_constant_expression (parser, NIC_FLOAT);
3911         }
3912       return token->u.value;
3913
3914     case CPP_CHAR_USERDEF:
3915     case CPP_CHAR16_USERDEF:
3916     case CPP_CHAR32_USERDEF:
3917     case CPP_WCHAR_USERDEF:
3918       return cp_parser_userdef_char_literal (parser);
3919
3920     case CPP_STRING:
3921     case CPP_STRING16:
3922     case CPP_STRING32:
3923     case CPP_WSTRING:
3924     case CPP_UTF8STRING:
3925     case CPP_STRING_USERDEF:
3926     case CPP_STRING16_USERDEF:
3927     case CPP_STRING32_USERDEF:
3928     case CPP_WSTRING_USERDEF:
3929     case CPP_UTF8STRING_USERDEF:
3930       /* ??? Should wide strings be allowed when parser->translate_strings_p
3931          is false (i.e. in attributes)?  If not, we can kill the third
3932          argument to cp_parser_string_literal.  */
3933       return cp_parser_string_literal (parser,
3934                                        parser->translate_strings_p,
3935                                        true);
3936
3937     case CPP_OPEN_PAREN:
3938       {
3939         tree expr;
3940         bool saved_greater_than_is_operator_p;
3941
3942         /* Consume the `('.  */
3943         cp_lexer_consume_token (parser->lexer);
3944         /* Within a parenthesized expression, a `>' token is always
3945            the greater-than operator.  */
3946         saved_greater_than_is_operator_p
3947           = parser->greater_than_is_operator_p;
3948         parser->greater_than_is_operator_p = true;
3949         /* If we see `( { ' then we are looking at the beginning of
3950            a GNU statement-expression.  */
3951         if (cp_parser_allow_gnu_extensions_p (parser)
3952             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
3953           {
3954             /* Statement-expressions are not allowed by the standard.  */
3955             pedwarn (token->location, OPT_pedantic, 
3956                      "ISO C++ forbids braced-groups within expressions");
3957
3958             /* And they're not allowed outside of a function-body; you
3959                cannot, for example, write:
3960
3961                  int i = ({ int j = 3; j + 1; });
3962
3963                at class or namespace scope.  */
3964             if (!parser->in_function_body
3965                 || parser->in_template_argument_list_p)
3966               {
3967                 error_at (token->location,
3968                           "statement-expressions are not allowed outside "
3969                           "functions nor in template-argument lists");
3970                 cp_parser_skip_to_end_of_block_or_statement (parser);
3971                 expr = error_mark_node;
3972               }
3973             else
3974               {
3975                 /* Start the statement-expression.  */
3976                 expr = begin_stmt_expr ();
3977                 /* Parse the compound-statement.  */
3978                 cp_parser_compound_statement (parser, expr, false, false);
3979                 /* Finish up.  */
3980                 expr = finish_stmt_expr (expr, false);
3981               }
3982           }
3983         else
3984           {
3985             /* Parse the parenthesized expression.  */
3986             expr = cp_parser_expression (parser, cast_p, idk);
3987             /* Let the front end know that this expression was
3988                enclosed in parentheses. This matters in case, for
3989                example, the expression is of the form `A::B', since
3990                `&A::B' might be a pointer-to-member, but `&(A::B)' is
3991                not.  */
3992             finish_parenthesized_expr (expr);
3993             /* DR 705: Wrapping an unqualified name in parentheses
3994                suppresses arg-dependent lookup.  We want to pass back
3995                CP_ID_KIND_QUALIFIED for suppressing vtable lookup
3996                (c++/37862), but none of the others.  */
3997             if (*idk != CP_ID_KIND_QUALIFIED)
3998               *idk = CP_ID_KIND_NONE;
3999           }
4000         /* The `>' token might be the end of a template-id or
4001            template-parameter-list now.  */
4002         parser->greater_than_is_operator_p
4003           = saved_greater_than_is_operator_p;
4004         /* Consume the `)'.  */
4005         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
4006           cp_parser_skip_to_end_of_statement (parser);
4007
4008         return expr;
4009       }
4010
4011     case CPP_OPEN_SQUARE:
4012       if (c_dialect_objc ())
4013         /* We have an Objective-C++ message. */
4014         return cp_parser_objc_expression (parser);
4015       {
4016         tree lam = cp_parser_lambda_expression (parser);
4017         /* Don't warn about a failed tentative parse.  */
4018         if (cp_parser_error_occurred (parser))
4019           return error_mark_node;
4020         maybe_warn_cpp0x (CPP0X_LAMBDA_EXPR);
4021         return lam;
4022       }
4023
4024     case CPP_OBJC_STRING:
4025       if (c_dialect_objc ())
4026         /* We have an Objective-C++ string literal. */
4027         return cp_parser_objc_expression (parser);
4028       cp_parser_error (parser, "expected primary-expression");
4029       return error_mark_node;
4030
4031     case CPP_KEYWORD:
4032       switch (token->keyword)
4033         {
4034           /* These two are the boolean literals.  */
4035         case RID_TRUE:
4036           cp_lexer_consume_token (parser->lexer);
4037           return boolean_true_node;
4038         case RID_FALSE:
4039           cp_lexer_consume_token (parser->lexer);
4040           return boolean_false_node;
4041
4042           /* The `__null' literal.  */
4043         case RID_NULL:
4044           cp_lexer_consume_token (parser->lexer);
4045           return null_node;
4046
4047           /* The `nullptr' literal.  */
4048         case RID_NULLPTR:
4049           cp_lexer_consume_token (parser->lexer);
4050           return nullptr_node;
4051
4052           /* Recognize the `this' keyword.  */
4053         case RID_THIS:
4054           cp_lexer_consume_token (parser->lexer);
4055           if (parser->local_variables_forbidden_p)
4056             {
4057               error_at (token->location,
4058                         "%<this%> may not be used in this context");
4059               return error_mark_node;
4060             }
4061           /* Pointers cannot appear in constant-expressions.  */
4062           if (cp_parser_non_integral_constant_expression (parser, NIC_THIS))
4063             return error_mark_node;
4064           return finish_this_expr ();
4065
4066           /* The `operator' keyword can be the beginning of an
4067              id-expression.  */
4068         case RID_OPERATOR:
4069           goto id_expression;
4070
4071         case RID_FUNCTION_NAME:
4072         case RID_PRETTY_FUNCTION_NAME:
4073         case RID_C99_FUNCTION_NAME:
4074           {
4075             non_integral_constant name;
4076
4077             /* The symbols __FUNCTION__, __PRETTY_FUNCTION__, and
4078                __func__ are the names of variables -- but they are
4079                treated specially.  Therefore, they are handled here,
4080                rather than relying on the generic id-expression logic
4081                below.  Grammatically, these names are id-expressions.
4082
4083                Consume the token.  */
4084             token = cp_lexer_consume_token (parser->lexer);
4085
4086             switch (token->keyword)
4087               {
4088               case RID_FUNCTION_NAME:
4089                 name = NIC_FUNC_NAME;
4090                 break;
4091               case RID_PRETTY_FUNCTION_NAME:
4092                 name = NIC_PRETTY_FUNC;
4093                 break;
4094               case RID_C99_FUNCTION_NAME:
4095                 name = NIC_C99_FUNC;
4096                 break;
4097               default:
4098                 gcc_unreachable ();
4099               }
4100
4101             if (cp_parser_non_integral_constant_expression (parser, name))
4102               return error_mark_node;
4103
4104             /* Look up the name.  */
4105             return finish_fname (token->u.value);
4106           }
4107
4108         case RID_VA_ARG:
4109           {
4110             tree expression;
4111             tree type;
4112
4113             /* The `__builtin_va_arg' construct is used to handle
4114                `va_arg'.  Consume the `__builtin_va_arg' token.  */
4115             cp_lexer_consume_token (parser->lexer);
4116             /* Look for the opening `('.  */
4117             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
4118             /* Now, parse the assignment-expression.  */
4119             expression = cp_parser_assignment_expression (parser,
4120                                                           /*cast_p=*/false, NULL);
4121             /* Look for the `,'.  */
4122             cp_parser_require (parser, CPP_COMMA, RT_COMMA);
4123             /* Parse the type-id.  */
4124             type = cp_parser_type_id (parser);
4125             /* Look for the closing `)'.  */
4126             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
4127             /* Using `va_arg' in a constant-expression is not
4128                allowed.  */
4129             if (cp_parser_non_integral_constant_expression (parser,
4130                                                             NIC_VA_ARG))
4131               return error_mark_node;
4132             return build_x_va_arg (expression, type);
4133           }
4134
4135         case RID_OFFSETOF:
4136           return cp_parser_builtin_offsetof (parser);
4137
4138         case RID_HAS_NOTHROW_ASSIGN:
4139         case RID_HAS_NOTHROW_CONSTRUCTOR:
4140         case RID_HAS_NOTHROW_COPY:        
4141         case RID_HAS_TRIVIAL_ASSIGN:
4142         case RID_HAS_TRIVIAL_CONSTRUCTOR:
4143         case RID_HAS_TRIVIAL_COPY:        
4144         case RID_HAS_TRIVIAL_DESTRUCTOR:
4145         case RID_HAS_VIRTUAL_DESTRUCTOR:
4146         case RID_IS_ABSTRACT:
4147         case RID_IS_BASE_OF:
4148         case RID_IS_CLASS:
4149         case RID_IS_CONVERTIBLE_TO:
4150         case RID_IS_EMPTY:
4151         case RID_IS_ENUM:
4152         case RID_IS_LITERAL_TYPE:
4153         case RID_IS_POD:
4154         case RID_IS_POLYMORPHIC:
4155         case RID_IS_STD_LAYOUT:
4156         case RID_IS_TRIVIAL:
4157         case RID_IS_UNION:
4158           return cp_parser_trait_expr (parser, token->keyword);
4159
4160         /* Objective-C++ expressions.  */
4161         case RID_AT_ENCODE:
4162         case RID_AT_PROTOCOL:
4163         case RID_AT_SELECTOR:
4164           return cp_parser_objc_expression (parser);
4165
4166         case RID_TEMPLATE:
4167           if (parser->in_function_body
4168               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4169                   == CPP_LESS))
4170             {
4171               error_at (token->location,
4172                         "a template declaration cannot appear at block scope");
4173               cp_parser_skip_to_end_of_block_or_statement (parser);
4174               return error_mark_node;
4175             }
4176         default:
4177           cp_parser_error (parser, "expected primary-expression");
4178           return error_mark_node;
4179         }
4180
4181       /* An id-expression can start with either an identifier, a
4182          `::' as the beginning of a qualified-id, or the "operator"
4183          keyword.  */
4184     case CPP_NAME:
4185     case CPP_SCOPE:
4186     case CPP_TEMPLATE_ID:
4187     case CPP_NESTED_NAME_SPECIFIER:
4188       {
4189         tree id_expression;
4190         tree decl;
4191         const char *error_msg;
4192         bool template_p;
4193         bool done;
4194         cp_token *id_expr_token;
4195
4196       id_expression:
4197         /* Parse the id-expression.  */
4198         id_expression
4199           = cp_parser_id_expression (parser,
4200                                      /*template_keyword_p=*/false,
4201                                      /*check_dependency_p=*/true,
4202                                      &template_p,
4203                                      /*declarator_p=*/false,
4204                                      /*optional_p=*/false);
4205         if (id_expression == error_mark_node)
4206           return error_mark_node;
4207         id_expr_token = token;
4208         token = cp_lexer_peek_token (parser->lexer);
4209         done = (token->type != CPP_OPEN_SQUARE
4210                 && token->type != CPP_OPEN_PAREN
4211                 && token->type != CPP_DOT
4212                 && token->type != CPP_DEREF
4213                 && token->type != CPP_PLUS_PLUS
4214                 && token->type != CPP_MINUS_MINUS);
4215         /* If we have a template-id, then no further lookup is
4216            required.  If the template-id was for a template-class, we
4217            will sometimes have a TYPE_DECL at this point.  */
4218         if (TREE_CODE (id_expression) == TEMPLATE_ID_EXPR
4219                  || TREE_CODE (id_expression) == TYPE_DECL)
4220           decl = id_expression;
4221         /* Look up the name.  */
4222         else
4223           {
4224             tree ambiguous_decls;
4225
4226             /* If we already know that this lookup is ambiguous, then
4227                we've already issued an error message; there's no reason
4228                to check again.  */
4229             if (id_expr_token->type == CPP_NAME
4230                 && id_expr_token->ambiguous_p)
4231               {
4232                 cp_parser_simulate_error (parser);
4233                 return error_mark_node;
4234               }
4235
4236             decl = cp_parser_lookup_name (parser, id_expression,
4237                                           none_type,
4238                                           template_p,
4239                                           /*is_namespace=*/false,
4240                                           /*check_dependency=*/true,
4241                                           &ambiguous_decls,
4242                                           id_expr_token->location);
4243             /* If the lookup was ambiguous, an error will already have
4244                been issued.  */
4245             if (ambiguous_decls)
4246               return error_mark_node;
4247
4248             /* In Objective-C++, we may have an Objective-C 2.0
4249                dot-syntax for classes here.  */
4250             if (c_dialect_objc ()
4251                 && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT
4252                 && TREE_CODE (decl) == TYPE_DECL
4253                 && objc_is_class_name (decl))
4254               {
4255                 tree component;
4256                 cp_lexer_consume_token (parser->lexer);
4257                 component = cp_parser_identifier (parser);
4258                 if (component == error_mark_node)
4259                   return error_mark_node;
4260
4261                 return objc_build_class_component_ref (id_expression, component);
4262               }
4263
4264             /* In Objective-C++, an instance variable (ivar) may be preferred
4265                to whatever cp_parser_lookup_name() found.  */
4266             decl = objc_lookup_ivar (decl, id_expression);
4267
4268             /* If name lookup gives us a SCOPE_REF, then the
4269                qualifying scope was dependent.  */
4270             if (TREE_CODE (decl) == SCOPE_REF)
4271               {
4272                 /* At this point, we do not know if DECL is a valid
4273                    integral constant expression.  We assume that it is
4274                    in fact such an expression, so that code like:
4275
4276                       template <int N> struct A {
4277                         int a[B<N>::i];
4278                       };
4279                      
4280                    is accepted.  At template-instantiation time, we
4281                    will check that B<N>::i is actually a constant.  */
4282                 return decl;
4283               }
4284             /* Check to see if DECL is a local variable in a context
4285                where that is forbidden.  */
4286             if (parser->local_variables_forbidden_p
4287                 && local_variable_p (decl))
4288               {
4289                 /* It might be that we only found DECL because we are
4290                    trying to be generous with pre-ISO scoping rules.
4291                    For example, consider:
4292
4293                      int i;
4294                      void g() {
4295                        for (int i = 0; i < 10; ++i) {}
4296                        extern void f(int j = i);
4297                      }
4298
4299                    Here, name look up will originally find the out
4300                    of scope `i'.  We need to issue a warning message,
4301                    but then use the global `i'.  */
4302                 decl = check_for_out_of_scope_variable (decl);
4303                 if (local_variable_p (decl))
4304                   {
4305                     error_at (id_expr_token->location,
4306                               "local variable %qD may not appear in this context",
4307                               decl);
4308                     return error_mark_node;
4309                   }
4310               }
4311           }
4312
4313         decl = (finish_id_expression
4314                 (id_expression, decl, parser->scope,
4315                  idk,
4316                  parser->integral_constant_expression_p,
4317                  parser->allow_non_integral_constant_expression_p,
4318                  &parser->non_integral_constant_expression_p,
4319                  template_p, done, address_p,
4320                  template_arg_p,
4321                  &error_msg,
4322                  id_expr_token->location));
4323         if (error_msg)
4324           cp_parser_error (parser, error_msg);
4325         return decl;
4326       }
4327
4328       /* Anything else is an error.  */
4329     default:
4330       cp_parser_error (parser, "expected primary-expression");
4331       return error_mark_node;
4332     }
4333 }
4334
4335 /* Parse an id-expression.
4336
4337    id-expression:
4338      unqualified-id
4339      qualified-id
4340
4341    qualified-id:
4342      :: [opt] nested-name-specifier template [opt] unqualified-id
4343      :: identifier
4344      :: operator-function-id
4345      :: template-id
4346
4347    Return a representation of the unqualified portion of the
4348    identifier.  Sets PARSER->SCOPE to the qualifying scope if there is
4349    a `::' or nested-name-specifier.
4350
4351    Often, if the id-expression was a qualified-id, the caller will
4352    want to make a SCOPE_REF to represent the qualified-id.  This
4353    function does not do this in order to avoid wastefully creating
4354    SCOPE_REFs when they are not required.
4355
4356    If TEMPLATE_KEYWORD_P is true, then we have just seen the
4357    `template' keyword.
4358
4359    If CHECK_DEPENDENCY_P is false, then names are looked up inside
4360    uninstantiated templates.
4361
4362    If *TEMPLATE_P is non-NULL, it is set to true iff the
4363    `template' keyword is used to explicitly indicate that the entity
4364    named is a template.
4365
4366    If DECLARATOR_P is true, the id-expression is appearing as part of
4367    a declarator, rather than as part of an expression.  */
4368
4369 static tree
4370 cp_parser_id_expression (cp_parser *parser,
4371                          bool template_keyword_p,
4372                          bool check_dependency_p,
4373                          bool *template_p,
4374                          bool declarator_p,
4375                          bool optional_p)
4376 {
4377   bool global_scope_p;
4378   bool nested_name_specifier_p;
4379
4380   /* Assume the `template' keyword was not used.  */
4381   if (template_p)
4382     *template_p = template_keyword_p;
4383
4384   /* Look for the optional `::' operator.  */
4385   global_scope_p
4386     = (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false)
4387        != NULL_TREE);
4388   /* Look for the optional nested-name-specifier.  */
4389   nested_name_specifier_p
4390     = (cp_parser_nested_name_specifier_opt (parser,
4391                                             /*typename_keyword_p=*/false,
4392                                             check_dependency_p,
4393                                             /*type_p=*/false,
4394                                             declarator_p)
4395        != NULL_TREE);
4396   /* If there is a nested-name-specifier, then we are looking at
4397      the first qualified-id production.  */
4398   if (nested_name_specifier_p)
4399     {
4400       tree saved_scope;
4401       tree saved_object_scope;
4402       tree saved_qualifying_scope;
4403       tree unqualified_id;
4404       bool is_template;
4405
4406       /* See if the next token is the `template' keyword.  */
4407       if (!template_p)
4408         template_p = &is_template;
4409       *template_p = cp_parser_optional_template_keyword (parser);
4410       /* Name lookup we do during the processing of the
4411          unqualified-id might obliterate SCOPE.  */
4412       saved_scope = parser->scope;
4413       saved_object_scope = parser->object_scope;
4414       saved_qualifying_scope = parser->qualifying_scope;
4415       /* Process the final unqualified-id.  */
4416       unqualified_id = cp_parser_unqualified_id (parser, *template_p,
4417                                                  check_dependency_p,
4418                                                  declarator_p,
4419                                                  /*optional_p=*/false);
4420       /* Restore the SAVED_SCOPE for our caller.  */
4421       parser->scope = saved_scope;
4422       parser->object_scope = saved_object_scope;
4423       parser->qualifying_scope = saved_qualifying_scope;
4424
4425       return unqualified_id;
4426     }
4427   /* Otherwise, if we are in global scope, then we are looking at one
4428      of the other qualified-id productions.  */
4429   else if (global_scope_p)
4430     {
4431       cp_token *token;
4432       tree id;
4433
4434       /* Peek at the next token.  */
4435       token = cp_lexer_peek_token (parser->lexer);
4436
4437       /* If it's an identifier, and the next token is not a "<", then
4438          we can avoid the template-id case.  This is an optimization
4439          for this common case.  */
4440       if (token->type == CPP_NAME
4441           && !cp_parser_nth_token_starts_template_argument_list_p
4442                (parser, 2))
4443         return cp_parser_identifier (parser);
4444
4445       cp_parser_parse_tentatively (parser);
4446       /* Try a template-id.  */
4447       id = cp_parser_template_id (parser,
4448                                   /*template_keyword_p=*/false,
4449                                   /*check_dependency_p=*/true,
4450                                   declarator_p);
4451       /* If that worked, we're done.  */
4452       if (cp_parser_parse_definitely (parser))
4453         return id;
4454
4455       /* Peek at the next token.  (Changes in the token buffer may
4456          have invalidated the pointer obtained above.)  */
4457       token = cp_lexer_peek_token (parser->lexer);
4458
4459       switch (token->type)
4460         {
4461         case CPP_NAME:
4462           return cp_parser_identifier (parser);
4463
4464         case CPP_KEYWORD:
4465           if (token->keyword == RID_OPERATOR)
4466             return cp_parser_operator_function_id (parser);
4467           /* Fall through.  */
4468
4469         default:
4470           cp_parser_error (parser, "expected id-expression");
4471           return error_mark_node;
4472         }
4473     }
4474   else
4475     return cp_parser_unqualified_id (parser, template_keyword_p,
4476                                      /*check_dependency_p=*/true,
4477                                      declarator_p,
4478                                      optional_p);
4479 }
4480
4481 /* Parse an unqualified-id.
4482
4483    unqualified-id:
4484      identifier
4485      operator-function-id
4486      conversion-function-id
4487      ~ class-name
4488      template-id
4489
4490    If TEMPLATE_KEYWORD_P is TRUE, we have just seen the `template'
4491    keyword, in a construct like `A::template ...'.
4492
4493    Returns a representation of unqualified-id.  For the `identifier'
4494    production, an IDENTIFIER_NODE is returned.  For the `~ class-name'
4495    production a BIT_NOT_EXPR is returned; the operand of the
4496    BIT_NOT_EXPR is an IDENTIFIER_NODE for the class-name.  For the
4497    other productions, see the documentation accompanying the
4498    corresponding parsing functions.  If CHECK_DEPENDENCY_P is false,
4499    names are looked up in uninstantiated templates.  If DECLARATOR_P
4500    is true, the unqualified-id is appearing as part of a declarator,
4501    rather than as part of an expression.  */
4502
4503 static tree
4504 cp_parser_unqualified_id (cp_parser* parser,
4505                           bool template_keyword_p,
4506                           bool check_dependency_p,
4507                           bool declarator_p,
4508                           bool optional_p)
4509 {
4510   cp_token *token;
4511
4512   /* Peek at the next token.  */
4513   token = cp_lexer_peek_token (parser->lexer);
4514
4515   switch (token->type)
4516     {
4517     case CPP_NAME:
4518       {
4519         tree id;
4520
4521         /* We don't know yet whether or not this will be a
4522            template-id.  */
4523         cp_parser_parse_tentatively (parser);
4524         /* Try a template-id.  */
4525         id = cp_parser_template_id (parser, template_keyword_p,
4526                                     check_dependency_p,
4527                                     declarator_p);
4528         /* If it worked, we're done.  */
4529         if (cp_parser_parse_definitely (parser))
4530           return id;
4531         /* Otherwise, it's an ordinary identifier.  */
4532         return cp_parser_identifier (parser);
4533       }
4534
4535     case CPP_TEMPLATE_ID:
4536       return cp_parser_template_id (parser, template_keyword_p,
4537                                     check_dependency_p,
4538                                     declarator_p);
4539
4540     case CPP_COMPL:
4541       {
4542         tree type_decl;
4543         tree qualifying_scope;
4544         tree object_scope;
4545         tree scope;
4546         bool done;
4547
4548         /* Consume the `~' token.  */
4549         cp_lexer_consume_token (parser->lexer);
4550         /* Parse the class-name.  The standard, as written, seems to
4551            say that:
4552
4553              template <typename T> struct S { ~S (); };
4554              template <typename T> S<T>::~S() {}
4555
4556            is invalid, since `~' must be followed by a class-name, but
4557            `S<T>' is dependent, and so not known to be a class.
4558            That's not right; we need to look in uninstantiated
4559            templates.  A further complication arises from:
4560
4561              template <typename T> void f(T t) {
4562                t.T::~T();
4563              }
4564
4565            Here, it is not possible to look up `T' in the scope of `T'
4566            itself.  We must look in both the current scope, and the
4567            scope of the containing complete expression.
4568
4569            Yet another issue is:
4570
4571              struct S {
4572                int S;
4573                ~S();
4574              };
4575
4576              S::~S() {}
4577
4578            The standard does not seem to say that the `S' in `~S'
4579            should refer to the type `S' and not the data member
4580            `S::S'.  */
4581
4582         /* DR 244 says that we look up the name after the "~" in the
4583            same scope as we looked up the qualifying name.  That idea
4584            isn't fully worked out; it's more complicated than that.  */
4585         scope = parser->scope;
4586         object_scope = parser->object_scope;
4587         qualifying_scope = parser->qualifying_scope;
4588
4589         /* Check for invalid scopes.  */
4590         if (scope == error_mark_node)
4591           {
4592             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4593               cp_lexer_consume_token (parser->lexer);
4594             return error_mark_node;
4595           }
4596         if (scope && TREE_CODE (scope) == NAMESPACE_DECL)
4597           {
4598             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4599               error_at (token->location,
4600                         "scope %qT before %<~%> is not a class-name",
4601                         scope);
4602             cp_parser_simulate_error (parser);
4603             if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
4604               cp_lexer_consume_token (parser->lexer);
4605             return error_mark_node;
4606           }
4607         gcc_assert (!scope || TYPE_P (scope));
4608
4609         /* If the name is of the form "X::~X" it's OK even if X is a
4610            typedef.  */
4611         token = cp_lexer_peek_token (parser->lexer);
4612         if (scope
4613             && token->type == CPP_NAME
4614             && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4615                 != CPP_LESS)
4616             && (token->u.value == TYPE_IDENTIFIER (scope)
4617                 || (CLASS_TYPE_P (scope)
4618                     && constructor_name_p (token->u.value, scope))))
4619           {
4620             cp_lexer_consume_token (parser->lexer);
4621             return build_nt (BIT_NOT_EXPR, scope);
4622           }
4623
4624         /* If there was an explicit qualification (S::~T), first look
4625            in the scope given by the qualification (i.e., S).
4626
4627            Note: in the calls to cp_parser_class_name below we pass
4628            typename_type so that lookup finds the injected-class-name
4629            rather than the constructor.  */
4630         done = false;
4631         type_decl = NULL_TREE;
4632         if (scope)
4633           {
4634             cp_parser_parse_tentatively (parser);
4635             type_decl = cp_parser_class_name (parser,
4636                                               /*typename_keyword_p=*/false,
4637                                               /*template_keyword_p=*/false,
4638                                               typename_type,
4639                                               /*check_dependency=*/false,
4640                                               /*class_head_p=*/false,
4641                                               declarator_p);
4642             if (cp_parser_parse_definitely (parser))
4643               done = true;
4644           }
4645         /* In "N::S::~S", look in "N" as well.  */
4646         if (!done && scope && qualifying_scope)
4647           {
4648             cp_parser_parse_tentatively (parser);
4649             parser->scope = qualifying_scope;
4650             parser->object_scope = NULL_TREE;
4651             parser->qualifying_scope = NULL_TREE;
4652             type_decl
4653               = cp_parser_class_name (parser,
4654                                       /*typename_keyword_p=*/false,
4655                                       /*template_keyword_p=*/false,
4656                                       typename_type,
4657                                       /*check_dependency=*/false,
4658                                       /*class_head_p=*/false,
4659                                       declarator_p);
4660             if (cp_parser_parse_definitely (parser))
4661               done = true;
4662           }
4663         /* In "p->S::~T", look in the scope given by "*p" as well.  */
4664         else if (!done && object_scope)
4665           {
4666             cp_parser_parse_tentatively (parser);
4667             parser->scope = object_scope;
4668             parser->object_scope = NULL_TREE;
4669             parser->qualifying_scope = NULL_TREE;
4670             type_decl
4671               = cp_parser_class_name (parser,
4672                                       /*typename_keyword_p=*/false,
4673                                       /*template_keyword_p=*/false,
4674                                       typename_type,
4675                                       /*check_dependency=*/false,
4676                                       /*class_head_p=*/false,
4677                                       declarator_p);
4678             if (cp_parser_parse_definitely (parser))
4679               done = true;
4680           }
4681         /* Look in the surrounding context.  */
4682         if (!done)
4683           {
4684             parser->scope = NULL_TREE;
4685             parser->object_scope = NULL_TREE;
4686             parser->qualifying_scope = NULL_TREE;
4687             if (processing_template_decl)
4688               cp_parser_parse_tentatively (parser);
4689             type_decl
4690               = cp_parser_class_name (parser,
4691                                       /*typename_keyword_p=*/false,
4692                                       /*template_keyword_p=*/false,
4693                                       typename_type,
4694                                       /*check_dependency=*/false,
4695                                       /*class_head_p=*/false,
4696                                       declarator_p);
4697             if (processing_template_decl
4698                 && ! cp_parser_parse_definitely (parser))
4699               {
4700                 /* We couldn't find a type with this name, so just accept
4701                    it and check for a match at instantiation time.  */
4702                 type_decl = cp_parser_identifier (parser);
4703                 if (type_decl != error_mark_node)
4704                   type_decl = build_nt (BIT_NOT_EXPR, type_decl);
4705                 return type_decl;
4706               }
4707           }
4708         /* If an error occurred, assume that the name of the
4709            destructor is the same as the name of the qualifying
4710            class.  That allows us to keep parsing after running
4711            into ill-formed destructor names.  */
4712         if (type_decl == error_mark_node && scope)
4713           return build_nt (BIT_NOT_EXPR, scope);
4714         else if (type_decl == error_mark_node)
4715           return error_mark_node;
4716
4717         /* Check that destructor name and scope match.  */
4718         if (declarator_p && scope && !check_dtor_name (scope, type_decl))
4719           {
4720             if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
4721               error_at (token->location,
4722                         "declaration of %<~%T%> as member of %qT",
4723                         type_decl, scope);
4724             cp_parser_simulate_error (parser);
4725             return error_mark_node;
4726           }
4727
4728         /* [class.dtor]
4729
4730            A typedef-name that names a class shall not be used as the
4731            identifier in the declarator for a destructor declaration.  */
4732         if (declarator_p
4733             && !DECL_IMPLICIT_TYPEDEF_P (type_decl)
4734             && !DECL_SELF_REFERENCE_P (type_decl)
4735             && !cp_parser_uncommitted_to_tentative_parse_p (parser))
4736           error_at (token->location,
4737                     "typedef-name %qD used as destructor declarator",
4738                     type_decl);
4739
4740         return build_nt (BIT_NOT_EXPR, TREE_TYPE (type_decl));
4741       }
4742
4743     case CPP_KEYWORD:
4744       if (token->keyword == RID_OPERATOR)
4745         {
4746           tree id;
4747
4748           /* This could be a template-id, so we try that first.  */
4749           cp_parser_parse_tentatively (parser);
4750           /* Try a template-id.  */
4751           id = cp_parser_template_id (parser, template_keyword_p,
4752                                       /*check_dependency_p=*/true,
4753                                       declarator_p);
4754           /* If that worked, we're done.  */
4755           if (cp_parser_parse_definitely (parser))
4756             return id;
4757           /* We still don't know whether we're looking at an
4758              operator-function-id or a conversion-function-id.  */
4759           cp_parser_parse_tentatively (parser);
4760           /* Try an operator-function-id.  */
4761           id = cp_parser_operator_function_id (parser);
4762           /* If that didn't work, try a conversion-function-id.  */
4763           if (!cp_parser_parse_definitely (parser))
4764             id = cp_parser_conversion_function_id (parser);
4765           else if (UDLIT_OPER_P (id))
4766             {
4767               /* 17.6.3.3.5  */
4768               const char *name = UDLIT_OP_SUFFIX (id);
4769               if (name[0] != '_' && !in_system_header)
4770                 warning (0, "literal operator suffixes not preceded by %<_%>"
4771                             " are reserved for future standardization");
4772             }
4773
4774           return id;
4775         }
4776       /* Fall through.  */
4777
4778     default:
4779       if (optional_p)
4780         return NULL_TREE;
4781       cp_parser_error (parser, "expected unqualified-id");
4782       return error_mark_node;
4783     }
4784 }
4785
4786 /* Parse an (optional) nested-name-specifier.
4787
4788    nested-name-specifier: [C++98]
4789      class-or-namespace-name :: nested-name-specifier [opt]
4790      class-or-namespace-name :: template nested-name-specifier [opt]
4791
4792    nested-name-specifier: [C++0x]
4793      type-name ::
4794      namespace-name ::
4795      nested-name-specifier identifier ::
4796      nested-name-specifier template [opt] simple-template-id ::
4797
4798    PARSER->SCOPE should be set appropriately before this function is
4799    called.  TYPENAME_KEYWORD_P is TRUE if the `typename' keyword is in
4800    effect.  TYPE_P is TRUE if we non-type bindings should be ignored
4801    in name lookups.
4802
4803    Sets PARSER->SCOPE to the class (TYPE) or namespace
4804    (NAMESPACE_DECL) specified by the nested-name-specifier, or leaves
4805    it unchanged if there is no nested-name-specifier.  Returns the new
4806    scope iff there is a nested-name-specifier, or NULL_TREE otherwise.
4807
4808    If IS_DECLARATION is TRUE, the nested-name-specifier is known to be
4809    part of a declaration and/or decl-specifier.  */
4810
4811 static tree
4812 cp_parser_nested_name_specifier_opt (cp_parser *parser,
4813                                      bool typename_keyword_p,
4814                                      bool check_dependency_p,
4815                                      bool type_p,
4816                                      bool is_declaration)
4817 {
4818   bool success = false;
4819   cp_token_position start = 0;
4820   cp_token *token;
4821
4822   /* Remember where the nested-name-specifier starts.  */
4823   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4824     {
4825       start = cp_lexer_token_position (parser->lexer, false);
4826       push_deferring_access_checks (dk_deferred);
4827     }
4828
4829   while (true)
4830     {
4831       tree new_scope;
4832       tree old_scope;
4833       tree saved_qualifying_scope;
4834       bool template_keyword_p;
4835
4836       /* Spot cases that cannot be the beginning of a
4837          nested-name-specifier.  */
4838       token = cp_lexer_peek_token (parser->lexer);
4839
4840       /* If the next token is CPP_NESTED_NAME_SPECIFIER, just process
4841          the already parsed nested-name-specifier.  */
4842       if (token->type == CPP_NESTED_NAME_SPECIFIER)
4843         {
4844           /* Grab the nested-name-specifier and continue the loop.  */
4845           cp_parser_pre_parsed_nested_name_specifier (parser);
4846           /* If we originally encountered this nested-name-specifier
4847              with IS_DECLARATION set to false, we will not have
4848              resolved TYPENAME_TYPEs, so we must do so here.  */
4849           if (is_declaration
4850               && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4851             {
4852               new_scope = resolve_typename_type (parser->scope,
4853                                                  /*only_current_p=*/false);
4854               if (TREE_CODE (new_scope) != TYPENAME_TYPE)
4855                 parser->scope = new_scope;
4856             }
4857           success = true;
4858           continue;
4859         }
4860
4861       /* Spot cases that cannot be the beginning of a
4862          nested-name-specifier.  On the second and subsequent times
4863          through the loop, we look for the `template' keyword.  */
4864       if (success && token->keyword == RID_TEMPLATE)
4865         ;
4866       /* A template-id can start a nested-name-specifier.  */
4867       else if (token->type == CPP_TEMPLATE_ID)
4868         ;
4869       /* DR 743: decltype can be used in a nested-name-specifier.  */
4870       else if (token_is_decltype (token))
4871         ;
4872       else
4873         {
4874           /* If the next token is not an identifier, then it is
4875              definitely not a type-name or namespace-name.  */
4876           if (token->type != CPP_NAME)
4877             break;
4878           /* If the following token is neither a `<' (to begin a
4879              template-id), nor a `::', then we are not looking at a
4880              nested-name-specifier.  */
4881           token = cp_lexer_peek_nth_token (parser->lexer, 2);
4882
4883           if (token->type == CPP_COLON
4884               && parser->colon_corrects_to_scope_p
4885               && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_NAME)
4886             {
4887               error_at (token->location,
4888                         "found %<:%> in nested-name-specifier, expected %<::%>");
4889               token->type = CPP_SCOPE;
4890             }
4891
4892           if (token->type != CPP_SCOPE
4893               && !cp_parser_nth_token_starts_template_argument_list_p
4894                   (parser, 2))
4895             break;
4896         }
4897
4898       /* The nested-name-specifier is optional, so we parse
4899          tentatively.  */
4900       cp_parser_parse_tentatively (parser);
4901
4902       /* Look for the optional `template' keyword, if this isn't the
4903          first time through the loop.  */
4904       if (success)
4905         template_keyword_p = cp_parser_optional_template_keyword (parser);
4906       else
4907         template_keyword_p = false;
4908
4909       /* Save the old scope since the name lookup we are about to do
4910          might destroy it.  */
4911       old_scope = parser->scope;
4912       saved_qualifying_scope = parser->qualifying_scope;
4913       /* In a declarator-id like "X<T>::I::Y<T>" we must be able to
4914          look up names in "X<T>::I" in order to determine that "Y" is
4915          a template.  So, if we have a typename at this point, we make
4916          an effort to look through it.  */
4917       if (is_declaration
4918           && !typename_keyword_p
4919           && parser->scope
4920           && TREE_CODE (parser->scope) == TYPENAME_TYPE)
4921         parser->scope = resolve_typename_type (parser->scope,
4922                                                /*only_current_p=*/false);
4923       /* Parse the qualifying entity.  */
4924       new_scope
4925         = cp_parser_qualifying_entity (parser,
4926                                        typename_keyword_p,
4927                                        template_keyword_p,
4928                                        check_dependency_p,
4929                                        type_p,
4930                                        is_declaration);
4931       /* Look for the `::' token.  */
4932       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
4933
4934       /* If we found what we wanted, we keep going; otherwise, we're
4935          done.  */
4936       if (!cp_parser_parse_definitely (parser))
4937         {
4938           bool error_p = false;
4939
4940           /* Restore the OLD_SCOPE since it was valid before the
4941              failed attempt at finding the last
4942              class-or-namespace-name.  */
4943           parser->scope = old_scope;
4944           parser->qualifying_scope = saved_qualifying_scope;
4945
4946           /* If the next token is a decltype, and the one after that is a
4947              `::', then the decltype has failed to resolve to a class or
4948              enumeration type.  Give this error even when parsing
4949              tentatively since it can't possibly be valid--and we're going
4950              to replace it with a CPP_NESTED_NAME_SPECIFIER below, so we
4951              won't get another chance.*/
4952           if (cp_lexer_next_token_is (parser->lexer, CPP_DECLTYPE)
4953               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4954                   == CPP_SCOPE))
4955             {
4956               token = cp_lexer_consume_token (parser->lexer);
4957               error_at (token->location, "decltype evaluates to %qT, "
4958                         "which is not a class or enumeration type",
4959                         token->u.value);
4960               parser->scope = error_mark_node;
4961               error_p = true;
4962               /* As below.  */
4963               success = true;
4964               cp_lexer_consume_token (parser->lexer);
4965             }
4966
4967           if (cp_parser_uncommitted_to_tentative_parse_p (parser))
4968             break;
4969           /* If the next token is an identifier, and the one after
4970              that is a `::', then any valid interpretation would have
4971              found a class-or-namespace-name.  */
4972           while (cp_lexer_next_token_is (parser->lexer, CPP_NAME)
4973                  && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
4974                      == CPP_SCOPE)
4975                  && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
4976                      != CPP_COMPL))
4977             {
4978               token = cp_lexer_consume_token (parser->lexer);
4979               if (!error_p)
4980                 {
4981                   if (!token->ambiguous_p)
4982                     {
4983                       tree decl;
4984                       tree ambiguous_decls;
4985
4986                       decl = cp_parser_lookup_name (parser, token->u.value,
4987                                                     none_type,
4988                                                     /*is_template=*/false,
4989                                                     /*is_namespace=*/false,
4990                                                     /*check_dependency=*/true,
4991                                                     &ambiguous_decls,
4992                                                     token->location);
4993                       if (TREE_CODE (decl) == TEMPLATE_DECL)
4994                         error_at (token->location,
4995                                   "%qD used without template parameters",
4996                                   decl);
4997                       else if (ambiguous_decls)
4998                         {
4999                           error_at (token->location,
5000                                     "reference to %qD is ambiguous",
5001                                     token->u.value);
5002                           print_candidates (ambiguous_decls);
5003                           decl = error_mark_node;
5004                         }
5005                       else
5006                         {
5007                           if (cxx_dialect != cxx98)
5008                             cp_parser_name_lookup_error
5009                             (parser, token->u.value, decl, NLE_NOT_CXX98,
5010                              token->location);
5011                           else
5012                             cp_parser_name_lookup_error
5013                             (parser, token->u.value, decl, NLE_CXX98,
5014                              token->location);
5015                         }
5016                     }
5017                   parser->scope = error_mark_node;
5018                   error_p = true;
5019                   /* Treat this as a successful nested-name-specifier
5020                      due to:
5021
5022                      [basic.lookup.qual]
5023
5024                      If the name found is not a class-name (clause
5025                      _class_) or namespace-name (_namespace.def_), the
5026                      program is ill-formed.  */
5027                   success = true;
5028                 }
5029               cp_lexer_consume_token (parser->lexer);
5030             }
5031           break;
5032         }
5033       /* We've found one valid nested-name-specifier.  */
5034       success = true;
5035       /* Name lookup always gives us a DECL.  */
5036       if (TREE_CODE (new_scope) == TYPE_DECL)
5037         new_scope = TREE_TYPE (new_scope);
5038       /* Uses of "template" must be followed by actual templates.  */
5039       if (template_keyword_p
5040           && !(CLASS_TYPE_P (new_scope)
5041                && ((CLASSTYPE_USE_TEMPLATE (new_scope)
5042                     && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (new_scope)))
5043                    || CLASSTYPE_IS_TEMPLATE (new_scope)))
5044           && !(TREE_CODE (new_scope) == TYPENAME_TYPE
5045                && (TREE_CODE (TYPENAME_TYPE_FULLNAME (new_scope))
5046                    == TEMPLATE_ID_EXPR)))
5047         permerror (input_location, TYPE_P (new_scope)
5048                    ? G_("%qT is not a template")
5049                    : G_("%qD is not a template"),
5050                    new_scope);
5051       /* If it is a class scope, try to complete it; we are about to
5052          be looking up names inside the class.  */
5053       if (TYPE_P (new_scope)
5054           /* Since checking types for dependency can be expensive,
5055              avoid doing it if the type is already complete.  */
5056           && !COMPLETE_TYPE_P (new_scope)
5057           /* Do not try to complete dependent types.  */
5058           && !dependent_type_p (new_scope))
5059         {
5060           new_scope = complete_type (new_scope);
5061           /* If it is a typedef to current class, use the current
5062              class instead, as the typedef won't have any names inside
5063              it yet.  */
5064           if (!COMPLETE_TYPE_P (new_scope)
5065               && currently_open_class (new_scope))
5066             new_scope = TYPE_MAIN_VARIANT (new_scope);
5067         }
5068       /* Make sure we look in the right scope the next time through
5069          the loop.  */
5070       parser->scope = new_scope;
5071     }
5072
5073   /* If parsing tentatively, replace the sequence of tokens that makes
5074      up the nested-name-specifier with a CPP_NESTED_NAME_SPECIFIER
5075      token.  That way, should we re-parse the token stream, we will
5076      not have to repeat the effort required to do the parse, nor will
5077      we issue duplicate error messages.  */
5078   if (success && start)
5079     {
5080       cp_token *token;
5081
5082       token = cp_lexer_token_at (parser->lexer, start);
5083       /* Reset the contents of the START token.  */
5084       token->type = CPP_NESTED_NAME_SPECIFIER;
5085       /* Retrieve any deferred checks.  Do not pop this access checks yet
5086          so the memory will not be reclaimed during token replacing below.  */
5087       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
5088       token->u.tree_check_value->value = parser->scope;
5089       token->u.tree_check_value->checks = get_deferred_access_checks ();
5090       token->u.tree_check_value->qualifying_scope =
5091         parser->qualifying_scope;
5092       token->keyword = RID_MAX;
5093
5094       /* Purge all subsequent tokens.  */
5095       cp_lexer_purge_tokens_after (parser->lexer, start);
5096     }
5097
5098   if (start)
5099     pop_to_parent_deferring_access_checks ();
5100
5101   return success ? parser->scope : NULL_TREE;
5102 }
5103
5104 /* Parse a nested-name-specifier.  See
5105    cp_parser_nested_name_specifier_opt for details.  This function
5106    behaves identically, except that it will an issue an error if no
5107    nested-name-specifier is present.  */
5108
5109 static tree
5110 cp_parser_nested_name_specifier (cp_parser *parser,
5111                                  bool typename_keyword_p,
5112                                  bool check_dependency_p,
5113                                  bool type_p,
5114                                  bool is_declaration)
5115 {
5116   tree scope;
5117
5118   /* Look for the nested-name-specifier.  */
5119   scope = cp_parser_nested_name_specifier_opt (parser,
5120                                                typename_keyword_p,
5121                                                check_dependency_p,
5122                                                type_p,
5123                                                is_declaration);
5124   /* If it was not present, issue an error message.  */
5125   if (!scope)
5126     {
5127       cp_parser_error (parser, "expected nested-name-specifier");
5128       parser->scope = NULL_TREE;
5129     }
5130
5131   return scope;
5132 }
5133
5134 /* Parse the qualifying entity in a nested-name-specifier. For C++98,
5135    this is either a class-name or a namespace-name (which corresponds
5136    to the class-or-namespace-name production in the grammar). For
5137    C++0x, it can also be a type-name that refers to an enumeration
5138    type.
5139
5140    TYPENAME_KEYWORD_P is TRUE iff the `typename' keyword is in effect.
5141    TEMPLATE_KEYWORD_P is TRUE iff the `template' keyword is in effect.
5142    CHECK_DEPENDENCY_P is FALSE iff dependent names should be looked up.
5143    TYPE_P is TRUE iff the next name should be taken as a class-name,
5144    even the same name is declared to be another entity in the same
5145    scope.
5146
5147    Returns the class (TYPE_DECL) or namespace (NAMESPACE_DECL)
5148    specified by the class-or-namespace-name.  If neither is found the
5149    ERROR_MARK_NODE is returned.  */
5150
5151 static tree
5152 cp_parser_qualifying_entity (cp_parser *parser,
5153                              bool typename_keyword_p,
5154                              bool template_keyword_p,
5155                              bool check_dependency_p,
5156                              bool type_p,
5157                              bool is_declaration)
5158 {
5159   tree saved_scope;
5160   tree saved_qualifying_scope;
5161   tree saved_object_scope;
5162   tree scope;
5163   bool only_class_p;
5164   bool successful_parse_p;
5165
5166   /* DR 743: decltype can appear in a nested-name-specifier.  */
5167   if (cp_lexer_next_token_is_decltype (parser->lexer))
5168     {
5169       scope = cp_parser_decltype (parser);
5170       if (TREE_CODE (scope) != ENUMERAL_TYPE
5171           && !MAYBE_CLASS_TYPE_P (scope))
5172         {
5173           cp_parser_simulate_error (parser);
5174           return error_mark_node;
5175         }
5176       if (TYPE_NAME (scope))
5177         scope = TYPE_NAME (scope);
5178       return scope;
5179     }
5180
5181   /* Before we try to parse the class-name, we must save away the
5182      current PARSER->SCOPE since cp_parser_class_name will destroy
5183      it.  */
5184   saved_scope = parser->scope;
5185   saved_qualifying_scope = parser->qualifying_scope;
5186   saved_object_scope = parser->object_scope;
5187   /* Try for a class-name first.  If the SAVED_SCOPE is a type, then
5188      there is no need to look for a namespace-name.  */
5189   only_class_p = template_keyword_p 
5190     || (saved_scope && TYPE_P (saved_scope) && cxx_dialect == cxx98);
5191   if (!only_class_p)
5192     cp_parser_parse_tentatively (parser);
5193   scope = cp_parser_class_name (parser,
5194                                 typename_keyword_p,
5195                                 template_keyword_p,
5196                                 type_p ? class_type : none_type,
5197                                 check_dependency_p,
5198                                 /*class_head_p=*/false,
5199                                 is_declaration);
5200   successful_parse_p = only_class_p || cp_parser_parse_definitely (parser);
5201   /* If that didn't work and we're in C++0x mode, try for a type-name.  */
5202   if (!only_class_p 
5203       && cxx_dialect != cxx98
5204       && !successful_parse_p)
5205     {
5206       /* Restore the saved scope.  */
5207       parser->scope = saved_scope;
5208       parser->qualifying_scope = saved_qualifying_scope;
5209       parser->object_scope = saved_object_scope;
5210
5211       /* Parse tentatively.  */
5212       cp_parser_parse_tentatively (parser);
5213      
5214       /* Parse a typedef-name or enum-name.  */
5215       scope = cp_parser_nonclass_name (parser);
5216
5217       /* "If the name found does not designate a namespace or a class,
5218          enumeration, or dependent type, the program is ill-formed."
5219
5220          We cover classes and dependent types above and namespaces below,
5221          so this code is only looking for enums.  */
5222       if (!scope || TREE_CODE (scope) != TYPE_DECL
5223           || TREE_CODE (TREE_TYPE (scope)) != ENUMERAL_TYPE)
5224         cp_parser_simulate_error (parser);
5225
5226       successful_parse_p = cp_parser_parse_definitely (parser);
5227     }
5228   /* If that didn't work, try for a namespace-name.  */
5229   if (!only_class_p && !successful_parse_p)
5230     {
5231       /* Restore the saved scope.  */
5232       parser->scope = saved_scope;
5233       parser->qualifying_scope = saved_qualifying_scope;
5234       parser->object_scope = saved_object_scope;
5235       /* If we are not looking at an identifier followed by the scope
5236          resolution operator, then this is not part of a
5237          nested-name-specifier.  (Note that this function is only used
5238          to parse the components of a nested-name-specifier.)  */
5239       if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME)
5240           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
5241         return error_mark_node;
5242       scope = cp_parser_namespace_name (parser);
5243     }
5244
5245   return scope;
5246 }
5247
5248 /* Parse a postfix-expression.
5249
5250    postfix-expression:
5251      primary-expression
5252      postfix-expression [ expression ]
5253      postfix-expression ( expression-list [opt] )
5254      simple-type-specifier ( expression-list [opt] )
5255      typename :: [opt] nested-name-specifier identifier
5256        ( expression-list [opt] )
5257      typename :: [opt] nested-name-specifier template [opt] template-id
5258        ( expression-list [opt] )
5259      postfix-expression . template [opt] id-expression
5260      postfix-expression -> template [opt] id-expression
5261      postfix-expression . pseudo-destructor-name
5262      postfix-expression -> pseudo-destructor-name
5263      postfix-expression ++
5264      postfix-expression --
5265      dynamic_cast < type-id > ( expression )
5266      static_cast < type-id > ( expression )
5267      reinterpret_cast < type-id > ( expression )
5268      const_cast < type-id > ( expression )
5269      typeid ( expression )
5270      typeid ( type-id )
5271
5272    GNU Extension:
5273
5274    postfix-expression:
5275      ( type-id ) { initializer-list , [opt] }
5276
5277    This extension is a GNU version of the C99 compound-literal
5278    construct.  (The C99 grammar uses `type-name' instead of `type-id',
5279    but they are essentially the same concept.)
5280
5281    If ADDRESS_P is true, the postfix expression is the operand of the
5282    `&' operator.  CAST_P is true if this expression is the target of a
5283    cast.
5284
5285    If MEMBER_ACCESS_ONLY_P, we only allow postfix expressions that are
5286    class member access expressions [expr.ref].
5287
5288    Returns a representation of the expression.  */
5289
5290 static tree
5291 cp_parser_postfix_expression (cp_parser *parser, bool address_p, bool cast_p,
5292                               bool member_access_only_p,
5293                               cp_id_kind * pidk_return)
5294 {
5295   cp_token *token;
5296   enum rid keyword;
5297   cp_id_kind idk = CP_ID_KIND_NONE;
5298   tree postfix_expression = NULL_TREE;
5299   bool is_member_access = false;
5300
5301   /* Peek at the next token.  */
5302   token = cp_lexer_peek_token (parser->lexer);
5303   /* Some of the productions are determined by keywords.  */
5304   keyword = token->keyword;
5305   switch (keyword)
5306     {
5307     case RID_DYNCAST:
5308     case RID_STATCAST:
5309     case RID_REINTCAST:
5310     case RID_CONSTCAST:
5311       {
5312         tree type;
5313         tree expression;
5314         const char *saved_message;
5315
5316         /* All of these can be handled in the same way from the point
5317            of view of parsing.  Begin by consuming the token
5318            identifying the cast.  */
5319         cp_lexer_consume_token (parser->lexer);
5320
5321         /* New types cannot be defined in the cast.  */
5322         saved_message = parser->type_definition_forbidden_message;
5323         parser->type_definition_forbidden_message
5324           = G_("types may not be defined in casts");
5325
5326         /* Look for the opening `<'.  */
5327         cp_parser_require (parser, CPP_LESS, RT_LESS);
5328         /* Parse the type to which we are casting.  */
5329         type = cp_parser_type_id (parser);
5330         /* Look for the closing `>'.  */
5331         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
5332         /* Restore the old message.  */
5333         parser->type_definition_forbidden_message = saved_message;
5334
5335         /* And the expression which is being cast.  */
5336         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5337         expression = cp_parser_expression (parser, /*cast_p=*/true, & idk);
5338         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5339
5340         /* Only type conversions to integral or enumeration types
5341            can be used in constant-expressions.  */
5342         if (!cast_valid_in_integral_constant_expression_p (type)
5343             && cp_parser_non_integral_constant_expression (parser, NIC_CAST))
5344           return error_mark_node;
5345
5346         switch (keyword)
5347           {
5348           case RID_DYNCAST:
5349             postfix_expression
5350               = build_dynamic_cast (type, expression, tf_warning_or_error);
5351             break;
5352           case RID_STATCAST:
5353             postfix_expression
5354               = build_static_cast (type, expression, tf_warning_or_error);
5355             break;
5356           case RID_REINTCAST:
5357             postfix_expression
5358               = build_reinterpret_cast (type, expression, 
5359                                         tf_warning_or_error);
5360             break;
5361           case RID_CONSTCAST:
5362             postfix_expression
5363               = build_const_cast (type, expression, tf_warning_or_error);
5364             break;
5365           default:
5366             gcc_unreachable ();
5367           }
5368       }
5369       break;
5370
5371     case RID_TYPEID:
5372       {
5373         tree type;
5374         const char *saved_message;
5375         bool saved_in_type_id_in_expr_p;
5376
5377         /* Consume the `typeid' token.  */
5378         cp_lexer_consume_token (parser->lexer);
5379         /* Look for the `(' token.  */
5380         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
5381         /* Types cannot be defined in a `typeid' expression.  */
5382         saved_message = parser->type_definition_forbidden_message;
5383         parser->type_definition_forbidden_message
5384           = G_("types may not be defined in a %<typeid%> expression");
5385         /* We can't be sure yet whether we're looking at a type-id or an
5386            expression.  */
5387         cp_parser_parse_tentatively (parser);
5388         /* Try a type-id first.  */
5389         saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5390         parser->in_type_id_in_expr_p = true;
5391         type = cp_parser_type_id (parser);
5392         parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5393         /* Look for the `)' token.  Otherwise, we can't be sure that
5394            we're not looking at an expression: consider `typeid (int
5395            (3))', for example.  */
5396         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5397         /* If all went well, simply lookup the type-id.  */
5398         if (cp_parser_parse_definitely (parser))
5399           postfix_expression = get_typeid (type);
5400         /* Otherwise, fall back to the expression variant.  */
5401         else
5402           {
5403             tree expression;
5404
5405             /* Look for an expression.  */
5406             expression = cp_parser_expression (parser, /*cast_p=*/false, & idk);
5407             /* Compute its typeid.  */
5408             postfix_expression = build_typeid (expression);
5409             /* Look for the `)' token.  */
5410             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5411           }
5412         /* Restore the saved message.  */
5413         parser->type_definition_forbidden_message = saved_message;
5414         /* `typeid' may not appear in an integral constant expression.  */
5415         if (cp_parser_non_integral_constant_expression (parser, NIC_TYPEID))
5416           return error_mark_node;
5417       }
5418       break;
5419
5420     case RID_TYPENAME:
5421       {
5422         tree type;
5423         /* The syntax permitted here is the same permitted for an
5424            elaborated-type-specifier.  */
5425         type = cp_parser_elaborated_type_specifier (parser,
5426                                                     /*is_friend=*/false,
5427                                                     /*is_declaration=*/false);
5428         postfix_expression = cp_parser_functional_cast (parser, type);
5429       }
5430       break;
5431
5432     default:
5433       {
5434         tree type;
5435
5436         /* If the next thing is a simple-type-specifier, we may be
5437            looking at a functional cast.  We could also be looking at
5438            an id-expression.  So, we try the functional cast, and if
5439            that doesn't work we fall back to the primary-expression.  */
5440         cp_parser_parse_tentatively (parser);
5441         /* Look for the simple-type-specifier.  */
5442         type = cp_parser_simple_type_specifier (parser,
5443                                                 /*decl_specs=*/NULL,
5444                                                 CP_PARSER_FLAGS_NONE);
5445         /* Parse the cast itself.  */
5446         if (!cp_parser_error_occurred (parser))
5447           postfix_expression
5448             = cp_parser_functional_cast (parser, type);
5449         /* If that worked, we're done.  */
5450         if (cp_parser_parse_definitely (parser))
5451           break;
5452
5453         /* If the functional-cast didn't work out, try a
5454            compound-literal.  */
5455         if (cp_parser_allow_gnu_extensions_p (parser)
5456             && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
5457           {
5458             VEC(constructor_elt,gc) *initializer_list = NULL;
5459             bool saved_in_type_id_in_expr_p;
5460
5461             cp_parser_parse_tentatively (parser);
5462             /* Consume the `('.  */
5463             cp_lexer_consume_token (parser->lexer);
5464             /* Parse the type.  */
5465             saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
5466             parser->in_type_id_in_expr_p = true;
5467             type = cp_parser_type_id (parser);
5468             parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
5469             /* Look for the `)'.  */
5470             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
5471             /* Look for the `{'.  */
5472             cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
5473             /* If things aren't going well, there's no need to
5474                keep going.  */
5475             if (!cp_parser_error_occurred (parser))
5476               {
5477                 bool non_constant_p;
5478                 /* Parse the initializer-list.  */
5479                 initializer_list
5480                   = cp_parser_initializer_list (parser, &non_constant_p);
5481                 /* Allow a trailing `,'.  */
5482                 if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
5483                   cp_lexer_consume_token (parser->lexer);
5484                 /* Look for the final `}'.  */
5485                 cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
5486               }
5487             /* If that worked, we're definitely looking at a
5488                compound-literal expression.  */
5489             if (cp_parser_parse_definitely (parser))
5490               {
5491                 /* Warn the user that a compound literal is not
5492                    allowed in standard C++.  */
5493                 pedwarn (input_location, OPT_pedantic, "ISO C++ forbids compound-literals");
5494                 /* For simplicity, we disallow compound literals in
5495                    constant-expressions.  We could
5496                    allow compound literals of integer type, whose
5497                    initializer was a constant, in constant
5498                    expressions.  Permitting that usage, as a further
5499                    extension, would not change the meaning of any
5500                    currently accepted programs.  (Of course, as
5501                    compound literals are not part of ISO C++, the
5502                    standard has nothing to say.)  */
5503                 if (cp_parser_non_integral_constant_expression (parser,
5504                                                                 NIC_NCC))
5505                   {
5506                     postfix_expression = error_mark_node;
5507                     break;
5508                   }
5509                 /* Form the representation of the compound-literal.  */
5510                 postfix_expression
5511                   = (finish_compound_literal
5512                      (type, build_constructor (init_list_type_node,
5513                                                initializer_list),
5514                       tf_warning_or_error));
5515                 break;
5516               }
5517           }
5518
5519         /* It must be a primary-expression.  */
5520         postfix_expression
5521           = cp_parser_primary_expression (parser, address_p, cast_p,
5522                                           /*template_arg_p=*/false,
5523                                           &idk);
5524       }
5525       break;
5526     }
5527
5528   /* Keep looping until the postfix-expression is complete.  */
5529   while (true)
5530     {
5531       if (idk == CP_ID_KIND_UNQUALIFIED
5532           && TREE_CODE (postfix_expression) == IDENTIFIER_NODE
5533           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
5534         /* It is not a Koenig lookup function call.  */
5535         postfix_expression
5536           = unqualified_name_lookup_error (postfix_expression);
5537
5538       /* Peek at the next token.  */
5539       token = cp_lexer_peek_token (parser->lexer);
5540
5541       switch (token->type)
5542         {
5543         case CPP_OPEN_SQUARE:
5544           postfix_expression
5545             = cp_parser_postfix_open_square_expression (parser,
5546                                                         postfix_expression,
5547                                                         false);
5548           idk = CP_ID_KIND_NONE;
5549           is_member_access = false;
5550           break;
5551
5552         case CPP_OPEN_PAREN:
5553           /* postfix-expression ( expression-list [opt] ) */
5554           {
5555             bool koenig_p;
5556             bool is_builtin_constant_p;
5557             bool saved_integral_constant_expression_p = false;
5558             bool saved_non_integral_constant_expression_p = false;
5559             VEC(tree,gc) *args;
5560
5561             is_member_access = false;
5562
5563             is_builtin_constant_p
5564               = DECL_IS_BUILTIN_CONSTANT_P (postfix_expression);
5565             if (is_builtin_constant_p)
5566               {
5567                 /* The whole point of __builtin_constant_p is to allow
5568                    non-constant expressions to appear as arguments.  */
5569                 saved_integral_constant_expression_p
5570                   = parser->integral_constant_expression_p;
5571                 saved_non_integral_constant_expression_p
5572                   = parser->non_integral_constant_expression_p;
5573                 parser->integral_constant_expression_p = false;
5574               }
5575             args = (cp_parser_parenthesized_expression_list
5576                     (parser, non_attr,
5577                      /*cast_p=*/false, /*allow_expansion_p=*/true,
5578                      /*non_constant_p=*/NULL));
5579             if (is_builtin_constant_p)
5580               {
5581                 parser->integral_constant_expression_p
5582                   = saved_integral_constant_expression_p;
5583                 parser->non_integral_constant_expression_p
5584                   = saved_non_integral_constant_expression_p;
5585               }
5586
5587             if (args == NULL)
5588               {
5589                 postfix_expression = error_mark_node;
5590                 break;
5591               }
5592
5593             /* Function calls are not permitted in
5594                constant-expressions.  */
5595             if (! builtin_valid_in_constant_expr_p (postfix_expression)
5596                 && cp_parser_non_integral_constant_expression (parser,
5597                                                                NIC_FUNC_CALL))
5598               {
5599                 postfix_expression = error_mark_node;
5600                 release_tree_vector (args);
5601                 break;
5602               }
5603
5604             koenig_p = false;
5605             if (idk == CP_ID_KIND_UNQUALIFIED
5606                 || idk == CP_ID_KIND_TEMPLATE_ID)
5607               {
5608                 if (TREE_CODE (postfix_expression) == IDENTIFIER_NODE)
5609                   {
5610                     if (!VEC_empty (tree, args))
5611                       {
5612                         koenig_p = true;
5613                         if (!any_type_dependent_arguments_p (args))
5614                           postfix_expression
5615                             = perform_koenig_lookup (postfix_expression, args,
5616                                                      /*include_std=*/false,
5617                                                      tf_warning_or_error);
5618                       }
5619                     else
5620                       postfix_expression
5621                         = unqualified_fn_lookup_error (postfix_expression);
5622                   }
5623                 /* We do not perform argument-dependent lookup if
5624                    normal lookup finds a non-function, in accordance
5625                    with the expected resolution of DR 218.  */
5626                 else if (!VEC_empty (tree, args)
5627                          && is_overloaded_fn (postfix_expression))
5628                   {
5629                     tree fn = get_first_fn (postfix_expression);
5630                     fn = STRIP_TEMPLATE (fn);
5631
5632                     /* Do not do argument dependent lookup if regular
5633                        lookup finds a member function or a block-scope
5634                        function declaration.  [basic.lookup.argdep]/3  */
5635                     if (!DECL_FUNCTION_MEMBER_P (fn)
5636                         && !DECL_LOCAL_FUNCTION_P (fn))
5637                       {
5638                         koenig_p = true;
5639                         if (!any_type_dependent_arguments_p (args))
5640                           postfix_expression
5641                             = perform_koenig_lookup (postfix_expression, args,
5642                                                      /*include_std=*/false,
5643                                                      tf_warning_or_error);
5644                       }
5645                   }
5646               }
5647
5648             if (TREE_CODE (postfix_expression) == COMPONENT_REF)
5649               {
5650                 tree instance = TREE_OPERAND (postfix_expression, 0);
5651                 tree fn = TREE_OPERAND (postfix_expression, 1);
5652
5653                 if (processing_template_decl
5654                     && (type_dependent_expression_p (instance)
5655                         || (!BASELINK_P (fn)
5656                             && TREE_CODE (fn) != FIELD_DECL)
5657                         || type_dependent_expression_p (fn)
5658                         || any_type_dependent_arguments_p (args)))
5659                   {
5660                     postfix_expression
5661                       = build_nt_call_vec (postfix_expression, args);
5662                     release_tree_vector (args);
5663                     break;
5664                   }
5665
5666                 if (BASELINK_P (fn))
5667                   {
5668                   postfix_expression
5669                     = (build_new_method_call
5670                        (instance, fn, &args, NULL_TREE,
5671                         (idk == CP_ID_KIND_QUALIFIED
5672                          ? LOOKUP_NORMAL|LOOKUP_NONVIRTUAL
5673                          : LOOKUP_NORMAL),
5674                         /*fn_p=*/NULL,
5675                         tf_warning_or_error));
5676                   }
5677                 else
5678                   postfix_expression
5679                     = finish_call_expr (postfix_expression, &args,
5680                                         /*disallow_virtual=*/false,
5681                                         /*koenig_p=*/false,
5682                                         tf_warning_or_error);
5683               }
5684             else if (TREE_CODE (postfix_expression) == OFFSET_REF
5685                      || TREE_CODE (postfix_expression) == MEMBER_REF
5686                      || TREE_CODE (postfix_expression) == DOTSTAR_EXPR)
5687               postfix_expression = (build_offset_ref_call_from_tree
5688                                     (postfix_expression, &args));
5689             else if (idk == CP_ID_KIND_QUALIFIED)
5690               /* A call to a static class member, or a namespace-scope
5691                  function.  */
5692               postfix_expression
5693                 = finish_call_expr (postfix_expression, &args,
5694                                     /*disallow_virtual=*/true,
5695                                     koenig_p,
5696                                     tf_warning_or_error);
5697             else
5698               /* All other function calls.  */
5699               postfix_expression
5700                 = finish_call_expr (postfix_expression, &args,
5701                                     /*disallow_virtual=*/false,
5702                                     koenig_p,
5703                                     tf_warning_or_error);
5704
5705             /* The POSTFIX_EXPRESSION is certainly no longer an id.  */
5706             idk = CP_ID_KIND_NONE;
5707
5708             release_tree_vector (args);
5709           }
5710           break;
5711
5712         case CPP_DOT:
5713         case CPP_DEREF:
5714           /* postfix-expression . template [opt] id-expression
5715              postfix-expression . pseudo-destructor-name
5716              postfix-expression -> template [opt] id-expression
5717              postfix-expression -> pseudo-destructor-name */
5718
5719           /* Consume the `.' or `->' operator.  */
5720           cp_lexer_consume_token (parser->lexer);
5721
5722           postfix_expression
5723             = cp_parser_postfix_dot_deref_expression (parser, token->type,
5724                                                       postfix_expression,
5725                                                       false, &idk,
5726                                                       token->location);
5727
5728           is_member_access = true;
5729           break;
5730
5731         case CPP_PLUS_PLUS:
5732           /* postfix-expression ++  */
5733           /* Consume the `++' token.  */
5734           cp_lexer_consume_token (parser->lexer);
5735           /* Generate a representation for the complete expression.  */
5736           postfix_expression
5737             = finish_increment_expr (postfix_expression,
5738                                      POSTINCREMENT_EXPR);
5739           /* Increments may not appear in constant-expressions.  */
5740           if (cp_parser_non_integral_constant_expression (parser, NIC_INC))
5741             postfix_expression = error_mark_node;
5742           idk = CP_ID_KIND_NONE;
5743           is_member_access = false;
5744           break;
5745
5746         case CPP_MINUS_MINUS:
5747           /* postfix-expression -- */
5748           /* Consume the `--' token.  */
5749           cp_lexer_consume_token (parser->lexer);
5750           /* Generate a representation for the complete expression.  */
5751           postfix_expression
5752             = finish_increment_expr (postfix_expression,
5753                                      POSTDECREMENT_EXPR);
5754           /* Decrements may not appear in constant-expressions.  */
5755           if (cp_parser_non_integral_constant_expression (parser, NIC_DEC))
5756             postfix_expression = error_mark_node;
5757           idk = CP_ID_KIND_NONE;
5758           is_member_access = false;
5759           break;
5760
5761         default:
5762           if (pidk_return != NULL)
5763             * pidk_return = idk;
5764           if (member_access_only_p)
5765             return is_member_access? postfix_expression : error_mark_node;
5766           else
5767             return postfix_expression;
5768         }
5769     }
5770
5771   /* We should never get here.  */
5772   gcc_unreachable ();
5773   return error_mark_node;
5774 }
5775
5776 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5777    by cp_parser_builtin_offsetof.  We're looking for
5778
5779      postfix-expression [ expression ]
5780
5781    FOR_OFFSETOF is set if we're being called in that context, which
5782    changes how we deal with integer constant expressions.  */
5783
5784 static tree
5785 cp_parser_postfix_open_square_expression (cp_parser *parser,
5786                                           tree postfix_expression,
5787                                           bool for_offsetof)
5788 {
5789   tree index;
5790
5791   /* Consume the `[' token.  */
5792   cp_lexer_consume_token (parser->lexer);
5793
5794   /* Parse the index expression.  */
5795   /* ??? For offsetof, there is a question of what to allow here.  If
5796      offsetof is not being used in an integral constant expression context,
5797      then we *could* get the right answer by computing the value at runtime.
5798      If we are in an integral constant expression context, then we might
5799      could accept any constant expression; hard to say without analysis.
5800      Rather than open the barn door too wide right away, allow only integer
5801      constant expressions here.  */
5802   if (for_offsetof)
5803     index = cp_parser_constant_expression (parser, false, NULL);
5804   else
5805     index = cp_parser_expression (parser, /*cast_p=*/false, NULL);
5806
5807   /* Look for the closing `]'.  */
5808   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
5809
5810   /* Build the ARRAY_REF.  */
5811   postfix_expression = grok_array_decl (postfix_expression, index);
5812
5813   /* When not doing offsetof, array references are not permitted in
5814      constant-expressions.  */
5815   if (!for_offsetof
5816       && (cp_parser_non_integral_constant_expression (parser, NIC_ARRAY_REF)))
5817     postfix_expression = error_mark_node;
5818
5819   return postfix_expression;
5820 }
5821
5822 /* A subroutine of cp_parser_postfix_expression that also gets hijacked
5823    by cp_parser_builtin_offsetof.  We're looking for
5824
5825      postfix-expression . template [opt] id-expression
5826      postfix-expression . pseudo-destructor-name
5827      postfix-expression -> template [opt] id-expression
5828      postfix-expression -> pseudo-destructor-name
5829
5830    FOR_OFFSETOF is set if we're being called in that context.  That sorta
5831    limits what of the above we'll actually accept, but nevermind.
5832    TOKEN_TYPE is the "." or "->" token, which will already have been
5833    removed from the stream.  */
5834
5835 static tree
5836 cp_parser_postfix_dot_deref_expression (cp_parser *parser,
5837                                         enum cpp_ttype token_type,
5838                                         tree postfix_expression,
5839                                         bool for_offsetof, cp_id_kind *idk,
5840                                         location_t location)
5841 {
5842   tree name;
5843   bool dependent_p;
5844   bool pseudo_destructor_p;
5845   tree scope = NULL_TREE;
5846
5847   /* If this is a `->' operator, dereference the pointer.  */
5848   if (token_type == CPP_DEREF)
5849     postfix_expression = build_x_arrow (postfix_expression);
5850   /* Check to see whether or not the expression is type-dependent.  */
5851   dependent_p = type_dependent_expression_p (postfix_expression);
5852   /* The identifier following the `->' or `.' is not qualified.  */
5853   parser->scope = NULL_TREE;
5854   parser->qualifying_scope = NULL_TREE;
5855   parser->object_scope = NULL_TREE;
5856   *idk = CP_ID_KIND_NONE;
5857
5858   /* Enter the scope corresponding to the type of the object
5859      given by the POSTFIX_EXPRESSION.  */
5860   if (!dependent_p && TREE_TYPE (postfix_expression) != NULL_TREE)
5861     {
5862       scope = TREE_TYPE (postfix_expression);
5863       /* According to the standard, no expression should ever have
5864          reference type.  Unfortunately, we do not currently match
5865          the standard in this respect in that our internal representation
5866          of an expression may have reference type even when the standard
5867          says it does not.  Therefore, we have to manually obtain the
5868          underlying type here.  */
5869       scope = non_reference (scope);
5870       /* The type of the POSTFIX_EXPRESSION must be complete.  */
5871       if (scope == unknown_type_node)
5872         {
5873           error_at (location, "%qE does not have class type",
5874                     postfix_expression);
5875           scope = NULL_TREE;
5876         }
5877       /* Unlike the object expression in other contexts, *this is not
5878          required to be of complete type for purposes of class member
5879          access (5.2.5) outside the member function body.  */
5880       else if (scope != current_class_ref
5881                && !(processing_template_decl && scope == current_class_type))
5882         scope = complete_type_or_else (scope, NULL_TREE);
5883       /* Let the name lookup machinery know that we are processing a
5884          class member access expression.  */
5885       parser->context->object_type = scope;
5886       /* If something went wrong, we want to be able to discern that case,
5887          as opposed to the case where there was no SCOPE due to the type
5888          of expression being dependent.  */
5889       if (!scope)
5890         scope = error_mark_node;
5891       /* If the SCOPE was erroneous, make the various semantic analysis
5892          functions exit quickly -- and without issuing additional error
5893          messages.  */
5894       if (scope == error_mark_node)
5895         postfix_expression = error_mark_node;
5896     }
5897
5898   /* Assume this expression is not a pseudo-destructor access.  */
5899   pseudo_destructor_p = false;
5900
5901   /* If the SCOPE is a scalar type, then, if this is a valid program,
5902      we must be looking at a pseudo-destructor-name.  If POSTFIX_EXPRESSION
5903      is type dependent, it can be pseudo-destructor-name or something else.
5904      Try to parse it as pseudo-destructor-name first.  */
5905   if ((scope && SCALAR_TYPE_P (scope)) || dependent_p)
5906     {
5907       tree s;
5908       tree type;
5909
5910       cp_parser_parse_tentatively (parser);
5911       /* Parse the pseudo-destructor-name.  */
5912       s = NULL_TREE;
5913       cp_parser_pseudo_destructor_name (parser, &s, &type);
5914       if (dependent_p
5915           && (cp_parser_error_occurred (parser)
5916               || TREE_CODE (type) != TYPE_DECL
5917               || !SCALAR_TYPE_P (TREE_TYPE (type))))
5918         cp_parser_abort_tentative_parse (parser);
5919       else if (cp_parser_parse_definitely (parser))
5920         {
5921           pseudo_destructor_p = true;
5922           postfix_expression
5923             = finish_pseudo_destructor_expr (postfix_expression,
5924                                              s, TREE_TYPE (type));
5925         }
5926     }
5927
5928   if (!pseudo_destructor_p)
5929     {
5930       /* If the SCOPE is not a scalar type, we are looking at an
5931          ordinary class member access expression, rather than a
5932          pseudo-destructor-name.  */
5933       bool template_p;
5934       cp_token *token = cp_lexer_peek_token (parser->lexer);
5935       /* Parse the id-expression.  */
5936       name = (cp_parser_id_expression
5937               (parser,
5938                cp_parser_optional_template_keyword (parser),
5939                /*check_dependency_p=*/true,
5940                &template_p,
5941                /*declarator_p=*/false,
5942                /*optional_p=*/false));
5943       /* In general, build a SCOPE_REF if the member name is qualified.
5944          However, if the name was not dependent and has already been
5945          resolved; there is no need to build the SCOPE_REF.  For example;
5946
5947              struct X { void f(); };
5948              template <typename T> void f(T* t) { t->X::f(); }
5949
5950          Even though "t" is dependent, "X::f" is not and has been resolved
5951          to a BASELINK; there is no need to include scope information.  */
5952
5953       /* But we do need to remember that there was an explicit scope for
5954          virtual function calls.  */
5955       if (parser->scope)
5956         *idk = CP_ID_KIND_QUALIFIED;
5957
5958       /* If the name is a template-id that names a type, we will get a
5959          TYPE_DECL here.  That is invalid code.  */
5960       if (TREE_CODE (name) == TYPE_DECL)
5961         {
5962           error_at (token->location, "invalid use of %qD", name);
5963           postfix_expression = error_mark_node;
5964         }
5965       else
5966         {
5967           if (name != error_mark_node && !BASELINK_P (name) && parser->scope)
5968             {
5969               name = build_qualified_name (/*type=*/NULL_TREE,
5970                                            parser->scope,
5971                                            name,
5972                                            template_p);
5973               parser->scope = NULL_TREE;
5974               parser->qualifying_scope = NULL_TREE;
5975               parser->object_scope = NULL_TREE;
5976             }
5977           if (scope && name && BASELINK_P (name))
5978             adjust_result_of_qualified_name_lookup
5979               (name, BINFO_TYPE (BASELINK_ACCESS_BINFO (name)), scope);
5980           postfix_expression
5981             = finish_class_member_access_expr (postfix_expression, name,
5982                                                template_p, 
5983                                                tf_warning_or_error);
5984         }
5985     }
5986
5987   /* We no longer need to look up names in the scope of the object on
5988      the left-hand side of the `.' or `->' operator.  */
5989   parser->context->object_type = NULL_TREE;
5990
5991   /* Outside of offsetof, these operators may not appear in
5992      constant-expressions.  */
5993   if (!for_offsetof
5994       && (cp_parser_non_integral_constant_expression
5995           (parser, token_type == CPP_DEREF ? NIC_ARROW : NIC_POINT)))
5996     postfix_expression = error_mark_node;
5997
5998   return postfix_expression;
5999 }
6000
6001 /* Parse a parenthesized expression-list.
6002
6003    expression-list:
6004      assignment-expression
6005      expression-list, assignment-expression
6006
6007    attribute-list:
6008      expression-list
6009      identifier
6010      identifier, expression-list
6011
6012    CAST_P is true if this expression is the target of a cast.
6013
6014    ALLOW_EXPANSION_P is true if this expression allows expansion of an
6015    argument pack.
6016
6017    Returns a vector of trees.  Each element is a representation of an
6018    assignment-expression.  NULL is returned if the ( and or ) are
6019    missing.  An empty, but allocated, vector is returned on no
6020    expressions.  The parentheses are eaten.  IS_ATTRIBUTE_LIST is id_attr
6021    if we are parsing an attribute list for an attribute that wants a
6022    plain identifier argument, normal_attr for an attribute that wants
6023    an expression, or non_attr if we aren't parsing an attribute list.  If
6024    NON_CONSTANT_P is non-NULL, *NON_CONSTANT_P indicates whether or
6025    not all of the expressions in the list were constant.  */
6026
6027 static VEC(tree,gc) *
6028 cp_parser_parenthesized_expression_list (cp_parser* parser,
6029                                          int is_attribute_list,
6030                                          bool cast_p,
6031                                          bool allow_expansion_p,
6032                                          bool *non_constant_p)
6033 {
6034   VEC(tree,gc) *expression_list;
6035   bool fold_expr_p = is_attribute_list != non_attr;
6036   tree identifier = NULL_TREE;
6037   bool saved_greater_than_is_operator_p;
6038
6039   /* Assume all the expressions will be constant.  */
6040   if (non_constant_p)
6041     *non_constant_p = false;
6042
6043   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
6044     return NULL;
6045
6046   expression_list = make_tree_vector ();
6047
6048   /* Within a parenthesized expression, a `>' token is always
6049      the greater-than operator.  */
6050   saved_greater_than_is_operator_p
6051     = parser->greater_than_is_operator_p;
6052   parser->greater_than_is_operator_p = true;
6053
6054   /* Consume expressions until there are no more.  */
6055   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
6056     while (true)
6057       {
6058         tree expr;
6059
6060         /* At the beginning of attribute lists, check to see if the
6061            next token is an identifier.  */
6062         if (is_attribute_list == id_attr
6063             && cp_lexer_peek_token (parser->lexer)->type == CPP_NAME)
6064           {
6065             cp_token *token;
6066
6067             /* Consume the identifier.  */
6068             token = cp_lexer_consume_token (parser->lexer);
6069             /* Save the identifier.  */
6070             identifier = token->u.value;
6071           }
6072         else
6073           {
6074             bool expr_non_constant_p;
6075
6076             /* Parse the next assignment-expression.  */
6077             if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6078               {
6079                 /* A braced-init-list.  */
6080                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6081                 expr = cp_parser_braced_list (parser, &expr_non_constant_p);
6082                 if (non_constant_p && expr_non_constant_p)
6083                   *non_constant_p = true;
6084               }
6085             else if (non_constant_p)
6086               {
6087                 expr = (cp_parser_constant_expression
6088                         (parser, /*allow_non_constant_p=*/true,
6089                          &expr_non_constant_p));
6090                 if (expr_non_constant_p)
6091                   *non_constant_p = true;
6092               }
6093             else
6094               expr = cp_parser_assignment_expression (parser, cast_p, NULL);
6095
6096             if (fold_expr_p)
6097               expr = fold_non_dependent_expr (expr);
6098
6099             /* If we have an ellipsis, then this is an expression
6100                expansion.  */
6101             if (allow_expansion_p
6102                 && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
6103               {
6104                 /* Consume the `...'.  */
6105                 cp_lexer_consume_token (parser->lexer);
6106
6107                 /* Build the argument pack.  */
6108                 expr = make_pack_expansion (expr);
6109               }
6110
6111              /* Add it to the list.  We add error_mark_node
6112                 expressions to the list, so that we can still tell if
6113                 the correct form for a parenthesized expression-list
6114                 is found. That gives better errors.  */
6115             VEC_safe_push (tree, gc, expression_list, expr);
6116
6117             if (expr == error_mark_node)
6118               goto skip_comma;
6119           }
6120
6121         /* After the first item, attribute lists look the same as
6122            expression lists.  */
6123         is_attribute_list = non_attr;
6124
6125       get_comma:;
6126         /* If the next token isn't a `,', then we are done.  */
6127         if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
6128           break;
6129
6130         /* Otherwise, consume the `,' and keep going.  */
6131         cp_lexer_consume_token (parser->lexer);
6132       }
6133
6134   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
6135     {
6136       int ending;
6137
6138     skip_comma:;
6139       /* We try and resync to an unnested comma, as that will give the
6140          user better diagnostics.  */
6141       ending = cp_parser_skip_to_closing_parenthesis (parser,
6142                                                       /*recovering=*/true,
6143                                                       /*or_comma=*/true,
6144                                                       /*consume_paren=*/true);
6145       if (ending < 0)
6146         goto get_comma;
6147       if (!ending)
6148         {
6149           parser->greater_than_is_operator_p
6150             = saved_greater_than_is_operator_p;
6151           return NULL;
6152         }
6153     }
6154
6155   parser->greater_than_is_operator_p
6156     = saved_greater_than_is_operator_p;
6157
6158   if (identifier)
6159     VEC_safe_insert (tree, gc, expression_list, 0, identifier);
6160
6161   return expression_list;
6162 }
6163
6164 /* Parse a pseudo-destructor-name.
6165
6166    pseudo-destructor-name:
6167      :: [opt] nested-name-specifier [opt] type-name :: ~ type-name
6168      :: [opt] nested-name-specifier template template-id :: ~ type-name
6169      :: [opt] nested-name-specifier [opt] ~ type-name
6170
6171    If either of the first two productions is used, sets *SCOPE to the
6172    TYPE specified before the final `::'.  Otherwise, *SCOPE is set to
6173    NULL_TREE.  *TYPE is set to the TYPE_DECL for the final type-name,
6174    or ERROR_MARK_NODE if the parse fails.  */
6175
6176 static void
6177 cp_parser_pseudo_destructor_name (cp_parser* parser,
6178                                   tree* scope,
6179                                   tree* type)
6180 {
6181   bool nested_name_specifier_p;
6182
6183   /* Assume that things will not work out.  */
6184   *type = error_mark_node;
6185
6186   /* Look for the optional `::' operator.  */
6187   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/true);
6188   /* Look for the optional nested-name-specifier.  */
6189   nested_name_specifier_p
6190     = (cp_parser_nested_name_specifier_opt (parser,
6191                                             /*typename_keyword_p=*/false,
6192                                             /*check_dependency_p=*/true,
6193                                             /*type_p=*/false,
6194                                             /*is_declaration=*/false)
6195        != NULL_TREE);
6196   /* Now, if we saw a nested-name-specifier, we might be doing the
6197      second production.  */
6198   if (nested_name_specifier_p
6199       && cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
6200     {
6201       /* Consume the `template' keyword.  */
6202       cp_lexer_consume_token (parser->lexer);
6203       /* Parse the template-id.  */
6204       cp_parser_template_id (parser,
6205                              /*template_keyword_p=*/true,
6206                              /*check_dependency_p=*/false,
6207                              /*is_declaration=*/true);
6208       /* Look for the `::' token.  */
6209       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6210     }
6211   /* If the next token is not a `~', then there might be some
6212      additional qualification.  */
6213   else if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMPL))
6214     {
6215       /* At this point, we're looking for "type-name :: ~".  The type-name
6216          must not be a class-name, since this is a pseudo-destructor.  So,
6217          it must be either an enum-name, or a typedef-name -- both of which
6218          are just identifiers.  So, we peek ahead to check that the "::"
6219          and "~" tokens are present; if they are not, then we can avoid
6220          calling type_name.  */
6221       if (cp_lexer_peek_token (parser->lexer)->type != CPP_NAME
6222           || cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE
6223           || cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_COMPL)
6224         {
6225           cp_parser_error (parser, "non-scalar type");
6226           return;
6227         }
6228
6229       /* Look for the type-name.  */
6230       *scope = TREE_TYPE (cp_parser_nonclass_name (parser));
6231       if (*scope == error_mark_node)
6232         return;
6233
6234       /* Look for the `::' token.  */
6235       cp_parser_require (parser, CPP_SCOPE, RT_SCOPE);
6236     }
6237   else
6238     *scope = NULL_TREE;
6239
6240   /* Look for the `~'.  */
6241   cp_parser_require (parser, CPP_COMPL, RT_COMPL);
6242
6243   /* Once we see the ~, this has to be a pseudo-destructor.  */
6244   if (!processing_template_decl && !cp_parser_error_occurred (parser))
6245     cp_parser_commit_to_tentative_parse (parser);
6246
6247   /* Look for the type-name again.  We are not responsible for
6248      checking that it matches the first type-name.  */
6249   *type = cp_parser_nonclass_name (parser);
6250 }
6251
6252 /* Parse a unary-expression.
6253
6254    unary-expression:
6255      postfix-expression
6256      ++ cast-expression
6257      -- cast-expression
6258      unary-operator cast-expression
6259      sizeof unary-expression
6260      sizeof ( type-id )
6261      alignof ( type-id )  [C++0x]
6262      new-expression
6263      delete-expression
6264
6265    GNU Extensions:
6266
6267    unary-expression:
6268      __extension__ cast-expression
6269      __alignof__ unary-expression
6270      __alignof__ ( type-id )
6271      alignof unary-expression  [C++0x]
6272      __real__ cast-expression
6273      __imag__ cast-expression
6274      && identifier
6275
6276    ADDRESS_P is true iff the unary-expression is appearing as the
6277    operand of the `&' operator.   CAST_P is true if this expression is
6278    the target of a cast.
6279
6280    Returns a representation of the expression.  */
6281
6282 static tree
6283 cp_parser_unary_expression (cp_parser *parser, bool address_p, bool cast_p,
6284                             cp_id_kind * pidk)
6285 {
6286   cp_token *token;
6287   enum tree_code unary_operator;
6288
6289   /* Peek at the next token.  */
6290   token = cp_lexer_peek_token (parser->lexer);
6291   /* Some keywords give away the kind of expression.  */
6292   if (token->type == CPP_KEYWORD)
6293     {
6294       enum rid keyword = token->keyword;
6295
6296       switch (keyword)
6297         {
6298         case RID_ALIGNOF:
6299         case RID_SIZEOF:
6300           {
6301             tree operand;
6302             enum tree_code op;
6303
6304             op = keyword == RID_ALIGNOF ? ALIGNOF_EXPR : SIZEOF_EXPR;
6305             /* Consume the token.  */
6306             cp_lexer_consume_token (parser->lexer);
6307             /* Parse the operand.  */
6308             operand = cp_parser_sizeof_operand (parser, keyword);
6309
6310             if (TYPE_P (operand))
6311               return cxx_sizeof_or_alignof_type (operand, op, true);
6312             else
6313               {
6314                 /* ISO C++ defines alignof only with types, not with
6315                    expressions. So pedwarn if alignof is used with a non-
6316                    type expression. However, __alignof__ is ok.  */
6317                 if (!strcmp (IDENTIFIER_POINTER (token->u.value), "alignof"))
6318                   pedwarn (token->location, OPT_pedantic,
6319                            "ISO C++ does not allow %<alignof%> "
6320                            "with a non-type");
6321
6322                 return cxx_sizeof_or_alignof_expr (operand, op, true);
6323               }
6324           }
6325
6326         case RID_NEW:
6327           return cp_parser_new_expression (parser);
6328
6329         case RID_DELETE:
6330           return cp_parser_delete_expression (parser);
6331
6332         case RID_EXTENSION:
6333           {
6334             /* The saved value of the PEDANTIC flag.  */
6335             int saved_pedantic;
6336             tree expr;
6337
6338             /* Save away the PEDANTIC flag.  */
6339             cp_parser_extension_opt (parser, &saved_pedantic);
6340             /* Parse the cast-expression.  */
6341             expr = cp_parser_simple_cast_expression (parser);
6342             /* Restore the PEDANTIC flag.  */
6343             pedantic = saved_pedantic;
6344
6345             return expr;
6346           }
6347
6348         case RID_REALPART:
6349         case RID_IMAGPART:
6350           {
6351             tree expression;
6352
6353             /* Consume the `__real__' or `__imag__' token.  */
6354             cp_lexer_consume_token (parser->lexer);
6355             /* Parse the cast-expression.  */
6356             expression = cp_parser_simple_cast_expression (parser);
6357             /* Create the complete representation.  */
6358             return build_x_unary_op ((keyword == RID_REALPART
6359                                       ? REALPART_EXPR : IMAGPART_EXPR),
6360                                      expression,
6361                                      tf_warning_or_error);
6362           }
6363           break;
6364
6365         case RID_NOEXCEPT:
6366           {
6367             tree expr;
6368             const char *saved_message;
6369             bool saved_integral_constant_expression_p;
6370             bool saved_non_integral_constant_expression_p;
6371             bool saved_greater_than_is_operator_p;
6372
6373             cp_lexer_consume_token (parser->lexer);
6374             cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
6375
6376             saved_message = parser->type_definition_forbidden_message;
6377             parser->type_definition_forbidden_message
6378               = G_("types may not be defined in %<noexcept%> expressions");
6379
6380             saved_integral_constant_expression_p
6381               = parser->integral_constant_expression_p;
6382             saved_non_integral_constant_expression_p
6383               = parser->non_integral_constant_expression_p;
6384             parser->integral_constant_expression_p = false;
6385
6386             saved_greater_than_is_operator_p
6387               = parser->greater_than_is_operator_p;
6388             parser->greater_than_is_operator_p = true;
6389
6390             ++cp_unevaluated_operand;
6391             ++c_inhibit_evaluation_warnings;
6392             expr = cp_parser_expression (parser, false, NULL);
6393             --c_inhibit_evaluation_warnings;
6394             --cp_unevaluated_operand;
6395
6396             parser->greater_than_is_operator_p
6397               = saved_greater_than_is_operator_p;
6398
6399             parser->integral_constant_expression_p
6400               = saved_integral_constant_expression_p;
6401             parser->non_integral_constant_expression_p
6402               = saved_non_integral_constant_expression_p;
6403
6404             parser->type_definition_forbidden_message = saved_message;
6405
6406             cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6407             return finish_noexcept_expr (expr, tf_warning_or_error);
6408           }
6409
6410         default:
6411           break;
6412         }
6413     }
6414
6415   /* Look for the `:: new' and `:: delete', which also signal the
6416      beginning of a new-expression, or delete-expression,
6417      respectively.  If the next token is `::', then it might be one of
6418      these.  */
6419   if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
6420     {
6421       enum rid keyword;
6422
6423       /* See if the token after the `::' is one of the keywords in
6424          which we're interested.  */
6425       keyword = cp_lexer_peek_nth_token (parser->lexer, 2)->keyword;
6426       /* If it's `new', we have a new-expression.  */
6427       if (keyword == RID_NEW)
6428         return cp_parser_new_expression (parser);
6429       /* Similarly, for `delete'.  */
6430       else if (keyword == RID_DELETE)
6431         return cp_parser_delete_expression (parser);
6432     }
6433
6434   /* Look for a unary operator.  */
6435   unary_operator = cp_parser_unary_operator (token);
6436   /* The `++' and `--' operators can be handled similarly, even though
6437      they are not technically unary-operators in the grammar.  */
6438   if (unary_operator == ERROR_MARK)
6439     {
6440       if (token->type == CPP_PLUS_PLUS)
6441         unary_operator = PREINCREMENT_EXPR;
6442       else if (token->type == CPP_MINUS_MINUS)
6443         unary_operator = PREDECREMENT_EXPR;
6444       /* Handle the GNU address-of-label extension.  */
6445       else if (cp_parser_allow_gnu_extensions_p (parser)
6446                && token->type == CPP_AND_AND)
6447         {
6448           tree identifier;
6449           tree expression;
6450           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
6451
6452           /* Consume the '&&' token.  */
6453           cp_lexer_consume_token (parser->lexer);
6454           /* Look for the identifier.  */
6455           identifier = cp_parser_identifier (parser);
6456           /* Create an expression representing the address.  */
6457           expression = finish_label_address_expr (identifier, loc);
6458           if (cp_parser_non_integral_constant_expression (parser,
6459                                                           NIC_ADDR_LABEL))
6460             expression = error_mark_node;
6461           return expression;
6462         }
6463     }
6464   if (unary_operator != ERROR_MARK)
6465     {
6466       tree cast_expression;
6467       tree expression = error_mark_node;
6468       non_integral_constant non_constant_p = NIC_NONE;
6469
6470       /* Consume the operator token.  */
6471       token = cp_lexer_consume_token (parser->lexer);
6472       /* Parse the cast-expression.  */
6473       cast_expression
6474         = cp_parser_cast_expression (parser,
6475                                      unary_operator == ADDR_EXPR,
6476                                      /*cast_p=*/false, pidk);
6477       /* Now, build an appropriate representation.  */
6478       switch (unary_operator)
6479         {
6480         case INDIRECT_REF:
6481           non_constant_p = NIC_STAR;
6482           expression = build_x_indirect_ref (cast_expression, RO_UNARY_STAR,
6483                                              tf_warning_or_error);
6484           break;
6485
6486         case ADDR_EXPR:
6487            non_constant_p = NIC_ADDR;
6488           /* Fall through.  */
6489         case BIT_NOT_EXPR:
6490           expression = build_x_unary_op (unary_operator, cast_expression,
6491                                          tf_warning_or_error);
6492           break;
6493
6494         case PREINCREMENT_EXPR:
6495         case PREDECREMENT_EXPR:
6496           non_constant_p = unary_operator == PREINCREMENT_EXPR
6497                            ? NIC_PREINCREMENT : NIC_PREDECREMENT;
6498           /* Fall through.  */
6499         case UNARY_PLUS_EXPR:
6500         case NEGATE_EXPR:
6501         case TRUTH_NOT_EXPR:
6502           expression = finish_unary_op_expr (unary_operator, cast_expression);
6503           break;
6504
6505         default:
6506           gcc_unreachable ();
6507         }
6508
6509       if (non_constant_p != NIC_NONE
6510           && cp_parser_non_integral_constant_expression (parser,
6511                                                          non_constant_p))
6512         expression = error_mark_node;
6513
6514       return expression;
6515     }
6516
6517   return cp_parser_postfix_expression (parser, address_p, cast_p,
6518                                        /*member_access_only_p=*/false,
6519                                        pidk);
6520 }
6521
6522 /* Returns ERROR_MARK if TOKEN is not a unary-operator.  If TOKEN is a
6523    unary-operator, the corresponding tree code is returned.  */
6524
6525 static enum tree_code
6526 cp_parser_unary_operator (cp_token* token)
6527 {
6528   switch (token->type)
6529     {
6530     case CPP_MULT:
6531       return INDIRECT_REF;
6532
6533     case CPP_AND:
6534       return ADDR_EXPR;
6535
6536     case CPP_PLUS:
6537       return UNARY_PLUS_EXPR;
6538
6539     case CPP_MINUS:
6540       return NEGATE_EXPR;
6541
6542     case CPP_NOT:
6543       return TRUTH_NOT_EXPR;
6544
6545     case CPP_COMPL:
6546       return BIT_NOT_EXPR;
6547
6548     default:
6549       return ERROR_MARK;
6550     }
6551 }
6552
6553 /* Parse a new-expression.
6554
6555    new-expression:
6556      :: [opt] new new-placement [opt] new-type-id new-initializer [opt]
6557      :: [opt] new new-placement [opt] ( type-id ) new-initializer [opt]
6558
6559    Returns a representation of the expression.  */
6560
6561 static tree
6562 cp_parser_new_expression (cp_parser* parser)
6563 {
6564   bool global_scope_p;
6565   VEC(tree,gc) *placement;
6566   tree type;
6567   VEC(tree,gc) *initializer;
6568   tree nelts;
6569   tree ret;
6570
6571   /* Look for the optional `::' operator.  */
6572   global_scope_p
6573     = (cp_parser_global_scope_opt (parser,
6574                                    /*current_scope_valid_p=*/false)
6575        != NULL_TREE);
6576   /* Look for the `new' operator.  */
6577   cp_parser_require_keyword (parser, RID_NEW, RT_NEW);
6578   /* There's no easy way to tell a new-placement from the
6579      `( type-id )' construct.  */
6580   cp_parser_parse_tentatively (parser);
6581   /* Look for a new-placement.  */
6582   placement = cp_parser_new_placement (parser);
6583   /* If that didn't work out, there's no new-placement.  */
6584   if (!cp_parser_parse_definitely (parser))
6585     {
6586       if (placement != NULL)
6587         release_tree_vector (placement);
6588       placement = NULL;
6589     }
6590
6591   /* If the next token is a `(', then we have a parenthesized
6592      type-id.  */
6593   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6594     {
6595       cp_token *token;
6596       /* Consume the `('.  */
6597       cp_lexer_consume_token (parser->lexer);
6598       /* Parse the type-id.  */
6599       type = cp_parser_type_id (parser);
6600       /* Look for the closing `)'.  */
6601       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
6602       token = cp_lexer_peek_token (parser->lexer);
6603       /* There should not be a direct-new-declarator in this production,
6604          but GCC used to allowed this, so we check and emit a sensible error
6605          message for this case.  */
6606       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6607         {
6608           error_at (token->location,
6609                     "array bound forbidden after parenthesized type-id");
6610           inform (token->location, 
6611                   "try removing the parentheses around the type-id");
6612           cp_parser_direct_new_declarator (parser);
6613         }
6614       nelts = NULL_TREE;
6615     }
6616   /* Otherwise, there must be a new-type-id.  */
6617   else
6618     type = cp_parser_new_type_id (parser, &nelts);
6619
6620   /* If the next token is a `(' or '{', then we have a new-initializer.  */
6621   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN)
6622       || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6623     initializer = cp_parser_new_initializer (parser);
6624   else
6625     initializer = NULL;
6626
6627   /* A new-expression may not appear in an integral constant
6628      expression.  */
6629   if (cp_parser_non_integral_constant_expression (parser, NIC_NEW))
6630     ret = error_mark_node;
6631   else
6632     {
6633       /* Create a representation of the new-expression.  */
6634       ret = build_new (&placement, type, nelts, &initializer, global_scope_p,
6635                        tf_warning_or_error);
6636     }
6637
6638   if (placement != NULL)
6639     release_tree_vector (placement);
6640   if (initializer != NULL)
6641     release_tree_vector (initializer);
6642
6643   return ret;
6644 }
6645
6646 /* Parse a new-placement.
6647
6648    new-placement:
6649      ( expression-list )
6650
6651    Returns the same representation as for an expression-list.  */
6652
6653 static VEC(tree,gc) *
6654 cp_parser_new_placement (cp_parser* parser)
6655 {
6656   VEC(tree,gc) *expression_list;
6657
6658   /* Parse the expression-list.  */
6659   expression_list = (cp_parser_parenthesized_expression_list
6660                      (parser, non_attr, /*cast_p=*/false,
6661                       /*allow_expansion_p=*/true,
6662                       /*non_constant_p=*/NULL));
6663
6664   return expression_list;
6665 }
6666
6667 /* Parse a new-type-id.
6668
6669    new-type-id:
6670      type-specifier-seq new-declarator [opt]
6671
6672    Returns the TYPE allocated.  If the new-type-id indicates an array
6673    type, *NELTS is set to the number of elements in the last array
6674    bound; the TYPE will not include the last array bound.  */
6675
6676 static tree
6677 cp_parser_new_type_id (cp_parser* parser, tree *nelts)
6678 {
6679   cp_decl_specifier_seq type_specifier_seq;
6680   cp_declarator *new_declarator;
6681   cp_declarator *declarator;
6682   cp_declarator *outer_declarator;
6683   const char *saved_message;
6684   tree type;
6685
6686   /* The type-specifier sequence must not contain type definitions.
6687      (It cannot contain declarations of new types either, but if they
6688      are not definitions we will catch that because they are not
6689      complete.)  */
6690   saved_message = parser->type_definition_forbidden_message;
6691   parser->type_definition_forbidden_message
6692     = G_("types may not be defined in a new-type-id");
6693   /* Parse the type-specifier-seq.  */
6694   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
6695                                 /*is_trailing_return=*/false,
6696                                 &type_specifier_seq);
6697   /* Restore the old message.  */
6698   parser->type_definition_forbidden_message = saved_message;
6699   /* Parse the new-declarator.  */
6700   new_declarator = cp_parser_new_declarator_opt (parser);
6701
6702   /* Determine the number of elements in the last array dimension, if
6703      any.  */
6704   *nelts = NULL_TREE;
6705   /* Skip down to the last array dimension.  */
6706   declarator = new_declarator;
6707   outer_declarator = NULL;
6708   while (declarator && (declarator->kind == cdk_pointer
6709                         || declarator->kind == cdk_ptrmem))
6710     {
6711       outer_declarator = declarator;
6712       declarator = declarator->declarator;
6713     }
6714   while (declarator
6715          && declarator->kind == cdk_array
6716          && declarator->declarator
6717          && declarator->declarator->kind == cdk_array)
6718     {
6719       outer_declarator = declarator;
6720       declarator = declarator->declarator;
6721     }
6722
6723   if (declarator && declarator->kind == cdk_array)
6724     {
6725       *nelts = declarator->u.array.bounds;
6726       if (*nelts == error_mark_node)
6727         *nelts = integer_one_node;
6728
6729       if (outer_declarator)
6730         outer_declarator->declarator = declarator->declarator;
6731       else
6732         new_declarator = NULL;
6733     }
6734
6735   type = groktypename (&type_specifier_seq, new_declarator, false);
6736   return type;
6737 }
6738
6739 /* Parse an (optional) new-declarator.
6740
6741    new-declarator:
6742      ptr-operator new-declarator [opt]
6743      direct-new-declarator
6744
6745    Returns the declarator.  */
6746
6747 static cp_declarator *
6748 cp_parser_new_declarator_opt (cp_parser* parser)
6749 {
6750   enum tree_code code;
6751   tree type;
6752   cp_cv_quals cv_quals;
6753
6754   /* We don't know if there's a ptr-operator next, or not.  */
6755   cp_parser_parse_tentatively (parser);
6756   /* Look for a ptr-operator.  */
6757   code = cp_parser_ptr_operator (parser, &type, &cv_quals);
6758   /* If that worked, look for more new-declarators.  */
6759   if (cp_parser_parse_definitely (parser))
6760     {
6761       cp_declarator *declarator;
6762
6763       /* Parse another optional declarator.  */
6764       declarator = cp_parser_new_declarator_opt (parser);
6765
6766       return cp_parser_make_indirect_declarator
6767         (code, type, cv_quals, declarator);
6768     }
6769
6770   /* If the next token is a `[', there is a direct-new-declarator.  */
6771   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6772     return cp_parser_direct_new_declarator (parser);
6773
6774   return NULL;
6775 }
6776
6777 /* Parse a direct-new-declarator.
6778
6779    direct-new-declarator:
6780      [ expression ]
6781      direct-new-declarator [constant-expression]
6782
6783    */
6784
6785 static cp_declarator *
6786 cp_parser_direct_new_declarator (cp_parser* parser)
6787 {
6788   cp_declarator *declarator = NULL;
6789
6790   while (true)
6791     {
6792       tree expression;
6793
6794       /* Look for the opening `['.  */
6795       cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
6796       /* The first expression is not required to be constant.  */
6797       if (!declarator)
6798         {
6799           cp_token *token = cp_lexer_peek_token (parser->lexer);
6800           expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
6801           /* The standard requires that the expression have integral
6802              type.  DR 74 adds enumeration types.  We believe that the
6803              real intent is that these expressions be handled like the
6804              expression in a `switch' condition, which also allows
6805              classes with a single conversion to integral or
6806              enumeration type.  */
6807           if (!processing_template_decl)
6808             {
6809               expression
6810                 = build_expr_type_conversion (WANT_INT | WANT_ENUM,
6811                                               expression,
6812                                               /*complain=*/true);
6813               if (!expression)
6814                 {
6815                   error_at (token->location,
6816                             "expression in new-declarator must have integral "
6817                             "or enumeration type");
6818                   expression = error_mark_node;
6819                 }
6820             }
6821         }
6822       /* But all the other expressions must be.  */
6823       else
6824         expression
6825           = cp_parser_constant_expression (parser,
6826                                            /*allow_non_constant=*/false,
6827                                            NULL);
6828       /* Look for the closing `]'.  */
6829       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6830
6831       /* Add this bound to the declarator.  */
6832       declarator = make_array_declarator (declarator, expression);
6833
6834       /* If the next token is not a `[', then there are no more
6835          bounds.  */
6836       if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_SQUARE))
6837         break;
6838     }
6839
6840   return declarator;
6841 }
6842
6843 /* Parse a new-initializer.
6844
6845    new-initializer:
6846      ( expression-list [opt] )
6847      braced-init-list
6848
6849    Returns a representation of the expression-list.  */
6850
6851 static VEC(tree,gc) *
6852 cp_parser_new_initializer (cp_parser* parser)
6853 {
6854   VEC(tree,gc) *expression_list;
6855
6856   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
6857     {
6858       tree t;
6859       bool expr_non_constant_p;
6860       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
6861       t = cp_parser_braced_list (parser, &expr_non_constant_p);
6862       CONSTRUCTOR_IS_DIRECT_INIT (t) = 1;
6863       expression_list = make_tree_vector_single (t);
6864     }
6865   else
6866     expression_list = (cp_parser_parenthesized_expression_list
6867                        (parser, non_attr, /*cast_p=*/false,
6868                         /*allow_expansion_p=*/true,
6869                         /*non_constant_p=*/NULL));
6870
6871   return expression_list;
6872 }
6873
6874 /* Parse a delete-expression.
6875
6876    delete-expression:
6877      :: [opt] delete cast-expression
6878      :: [opt] delete [ ] cast-expression
6879
6880    Returns a representation of the expression.  */
6881
6882 static tree
6883 cp_parser_delete_expression (cp_parser* parser)
6884 {
6885   bool global_scope_p;
6886   bool array_p;
6887   tree expression;
6888
6889   /* Look for the optional `::' operator.  */
6890   global_scope_p
6891     = (cp_parser_global_scope_opt (parser,
6892                                    /*current_scope_valid_p=*/false)
6893        != NULL_TREE);
6894   /* Look for the `delete' keyword.  */
6895   cp_parser_require_keyword (parser, RID_DELETE, RT_DELETE);
6896   /* See if the array syntax is in use.  */
6897   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
6898     {
6899       /* Consume the `[' token.  */
6900       cp_lexer_consume_token (parser->lexer);
6901       /* Look for the `]' token.  */
6902       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
6903       /* Remember that this is the `[]' construct.  */
6904       array_p = true;
6905     }
6906   else
6907     array_p = false;
6908
6909   /* Parse the cast-expression.  */
6910   expression = cp_parser_simple_cast_expression (parser);
6911
6912   /* A delete-expression may not appear in an integral constant
6913      expression.  */
6914   if (cp_parser_non_integral_constant_expression (parser, NIC_DEL))
6915     return error_mark_node;
6916
6917   return delete_sanity (expression, NULL_TREE, array_p, global_scope_p,
6918                         tf_warning_or_error);
6919 }
6920
6921 /* Returns true if TOKEN may start a cast-expression and false
6922    otherwise.  */
6923
6924 static bool
6925 cp_parser_token_starts_cast_expression (cp_token *token)
6926 {
6927   switch (token->type)
6928     {
6929     case CPP_COMMA:
6930     case CPP_SEMICOLON:
6931     case CPP_QUERY:
6932     case CPP_COLON:
6933     case CPP_CLOSE_SQUARE:
6934     case CPP_CLOSE_PAREN:
6935     case CPP_CLOSE_BRACE:
6936     case CPP_DOT:
6937     case CPP_DOT_STAR:
6938     case CPP_DEREF:
6939     case CPP_DEREF_STAR:
6940     case CPP_DIV:
6941     case CPP_MOD:
6942     case CPP_LSHIFT:
6943     case CPP_RSHIFT:
6944     case CPP_LESS:
6945     case CPP_GREATER:
6946     case CPP_LESS_EQ:
6947     case CPP_GREATER_EQ:
6948     case CPP_EQ_EQ:
6949     case CPP_NOT_EQ:
6950     case CPP_EQ:
6951     case CPP_MULT_EQ:
6952     case CPP_DIV_EQ:
6953     case CPP_MOD_EQ:
6954     case CPP_PLUS_EQ:
6955     case CPP_MINUS_EQ:
6956     case CPP_RSHIFT_EQ:
6957     case CPP_LSHIFT_EQ:
6958     case CPP_AND_EQ:
6959     case CPP_XOR_EQ:
6960     case CPP_OR_EQ:
6961     case CPP_XOR:
6962     case CPP_OR:
6963     case CPP_OR_OR:
6964     case CPP_EOF:
6965       return false;
6966
6967       /* '[' may start a primary-expression in obj-c++.  */
6968     case CPP_OPEN_SQUARE:
6969       return c_dialect_objc ();
6970
6971     default:
6972       return true;
6973     }
6974 }
6975
6976 /* Parse a cast-expression.
6977
6978    cast-expression:
6979      unary-expression
6980      ( type-id ) cast-expression
6981
6982    ADDRESS_P is true iff the unary-expression is appearing as the
6983    operand of the `&' operator.   CAST_P is true if this expression is
6984    the target of a cast.
6985
6986    Returns a representation of the expression.  */
6987
6988 static tree
6989 cp_parser_cast_expression (cp_parser *parser, bool address_p, bool cast_p,
6990                            cp_id_kind * pidk)
6991 {
6992   /* If it's a `(', then we might be looking at a cast.  */
6993   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
6994     {
6995       tree type = NULL_TREE;
6996       tree expr = NULL_TREE;
6997       bool compound_literal_p;
6998       const char *saved_message;
6999
7000       /* There's no way to know yet whether or not this is a cast.
7001          For example, `(int (3))' is a unary-expression, while `(int)
7002          3' is a cast.  So, we resort to parsing tentatively.  */
7003       cp_parser_parse_tentatively (parser);
7004       /* Types may not be defined in a cast.  */
7005       saved_message = parser->type_definition_forbidden_message;
7006       parser->type_definition_forbidden_message
7007         = G_("types may not be defined in casts");
7008       /* Consume the `('.  */
7009       cp_lexer_consume_token (parser->lexer);
7010       /* A very tricky bit is that `(struct S) { 3 }' is a
7011          compound-literal (which we permit in C++ as an extension).
7012          But, that construct is not a cast-expression -- it is a
7013          postfix-expression.  (The reason is that `(struct S) { 3 }.i'
7014          is legal; if the compound-literal were a cast-expression,
7015          you'd need an extra set of parentheses.)  But, if we parse
7016          the type-id, and it happens to be a class-specifier, then we
7017          will commit to the parse at that point, because we cannot
7018          undo the action that is done when creating a new class.  So,
7019          then we cannot back up and do a postfix-expression.
7020
7021          Therefore, we scan ahead to the closing `)', and check to see
7022          if the token after the `)' is a `{'.  If so, we are not
7023          looking at a cast-expression.
7024
7025          Save tokens so that we can put them back.  */
7026       cp_lexer_save_tokens (parser->lexer);
7027       /* Skip tokens until the next token is a closing parenthesis.
7028          If we find the closing `)', and the next token is a `{', then
7029          we are looking at a compound-literal.  */
7030       compound_literal_p
7031         = (cp_parser_skip_to_closing_parenthesis (parser, false, false,
7032                                                   /*consume_paren=*/true)
7033            && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE));
7034       /* Roll back the tokens we skipped.  */
7035       cp_lexer_rollback_tokens (parser->lexer);
7036       /* If we were looking at a compound-literal, simulate an error
7037          so that the call to cp_parser_parse_definitely below will
7038          fail.  */
7039       if (compound_literal_p)
7040         cp_parser_simulate_error (parser);
7041       else
7042         {
7043           bool saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
7044           parser->in_type_id_in_expr_p = true;
7045           /* Look for the type-id.  */
7046           type = cp_parser_type_id (parser);
7047           /* Look for the closing `)'.  */
7048           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7049           parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
7050         }
7051
7052       /* Restore the saved message.  */
7053       parser->type_definition_forbidden_message = saved_message;
7054
7055       /* At this point this can only be either a cast or a
7056          parenthesized ctor such as `(T ())' that looks like a cast to
7057          function returning T.  */
7058       if (!cp_parser_error_occurred (parser)
7059           && cp_parser_token_starts_cast_expression (cp_lexer_peek_token
7060                                                      (parser->lexer)))
7061         {
7062           cp_parser_parse_definitely (parser);
7063           expr = cp_parser_cast_expression (parser,
7064                                             /*address_p=*/false,
7065                                             /*cast_p=*/true, pidk);
7066
7067           /* Warn about old-style casts, if so requested.  */
7068           if (warn_old_style_cast
7069               && !in_system_header
7070               && !VOID_TYPE_P (type)
7071               && current_lang_name != lang_name_c)
7072             warning (OPT_Wold_style_cast, "use of old-style cast");
7073
7074           /* Only type conversions to integral or enumeration types
7075              can be used in constant-expressions.  */
7076           if (!cast_valid_in_integral_constant_expression_p (type)
7077               && cp_parser_non_integral_constant_expression (parser,
7078                                                              NIC_CAST))
7079             return error_mark_node;
7080
7081           /* Perform the cast.  */
7082           expr = build_c_cast (input_location, type, expr);
7083           return expr;
7084         }
7085       else 
7086         cp_parser_abort_tentative_parse (parser);
7087     }
7088
7089   /* If we get here, then it's not a cast, so it must be a
7090      unary-expression.  */
7091   return cp_parser_unary_expression (parser, address_p, cast_p, pidk);
7092 }
7093
7094 /* Parse a binary expression of the general form:
7095
7096    pm-expression:
7097      cast-expression
7098      pm-expression .* cast-expression
7099      pm-expression ->* cast-expression
7100
7101    multiplicative-expression:
7102      pm-expression
7103      multiplicative-expression * pm-expression
7104      multiplicative-expression / pm-expression
7105      multiplicative-expression % pm-expression
7106
7107    additive-expression:
7108      multiplicative-expression
7109      additive-expression + multiplicative-expression
7110      additive-expression - multiplicative-expression
7111
7112    shift-expression:
7113      additive-expression
7114      shift-expression << additive-expression
7115      shift-expression >> additive-expression
7116
7117    relational-expression:
7118      shift-expression
7119      relational-expression < shift-expression
7120      relational-expression > shift-expression
7121      relational-expression <= shift-expression
7122      relational-expression >= shift-expression
7123
7124   GNU Extension:
7125
7126    relational-expression:
7127      relational-expression <? shift-expression
7128      relational-expression >? shift-expression
7129
7130    equality-expression:
7131      relational-expression
7132      equality-expression == relational-expression
7133      equality-expression != relational-expression
7134
7135    and-expression:
7136      equality-expression
7137      and-expression & equality-expression
7138
7139    exclusive-or-expression:
7140      and-expression
7141      exclusive-or-expression ^ and-expression
7142
7143    inclusive-or-expression:
7144      exclusive-or-expression
7145      inclusive-or-expression | exclusive-or-expression
7146
7147    logical-and-expression:
7148      inclusive-or-expression
7149      logical-and-expression && inclusive-or-expression
7150
7151    logical-or-expression:
7152      logical-and-expression
7153      logical-or-expression || logical-and-expression
7154
7155    All these are implemented with a single function like:
7156
7157    binary-expression:
7158      simple-cast-expression
7159      binary-expression <token> binary-expression
7160
7161    CAST_P is true if this expression is the target of a cast.
7162
7163    The binops_by_token map is used to get the tree codes for each <token> type.
7164    binary-expressions are associated according to a precedence table.  */
7165
7166 #define TOKEN_PRECEDENCE(token)                              \
7167 (((token->type == CPP_GREATER                                \
7168    || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT)) \
7169   && !parser->greater_than_is_operator_p)                    \
7170  ? PREC_NOT_OPERATOR                                         \
7171  : binops_by_token[token->type].prec)
7172
7173 static tree
7174 cp_parser_binary_expression (cp_parser* parser, bool cast_p,
7175                              bool no_toplevel_fold_p,
7176                              enum cp_parser_prec prec,
7177                              cp_id_kind * pidk)
7178 {
7179   cp_parser_expression_stack stack;
7180   cp_parser_expression_stack_entry *sp = &stack[0];
7181   tree lhs, rhs;
7182   cp_token *token;
7183   enum tree_code tree_type, lhs_type, rhs_type;
7184   enum cp_parser_prec new_prec, lookahead_prec;
7185   tree overload;
7186
7187   /* Parse the first expression.  */
7188   lhs = cp_parser_cast_expression (parser, /*address_p=*/false, cast_p, pidk);
7189   lhs_type = ERROR_MARK;
7190
7191   for (;;)
7192     {
7193       /* Get an operator token.  */
7194       token = cp_lexer_peek_token (parser->lexer);
7195
7196       if (warn_cxx0x_compat
7197           && token->type == CPP_RSHIFT
7198           && !parser->greater_than_is_operator_p)
7199         {
7200           if (warning_at (token->location, OPT_Wc__0x_compat, 
7201                           "%<>>%> operator will be treated as"
7202                           " two right angle brackets in C++0x"))
7203             inform (token->location,
7204                     "suggest parentheses around %<>>%> expression");
7205         }
7206
7207       new_prec = TOKEN_PRECEDENCE (token);
7208
7209       /* Popping an entry off the stack means we completed a subexpression:
7210          - either we found a token which is not an operator (`>' where it is not
7211            an operator, or prec == PREC_NOT_OPERATOR), in which case popping
7212            will happen repeatedly;
7213          - or, we found an operator which has lower priority.  This is the case
7214            where the recursive descent *ascends*, as in `3 * 4 + 5' after
7215            parsing `3 * 4'.  */
7216       if (new_prec <= prec)
7217         {
7218           if (sp == stack)
7219             break;
7220           else
7221             goto pop;
7222         }
7223
7224      get_rhs:
7225       tree_type = binops_by_token[token->type].tree_type;
7226
7227       /* We used the operator token.  */
7228       cp_lexer_consume_token (parser->lexer);
7229
7230       /* For "false && x" or "true || x", x will never be executed;
7231          disable warnings while evaluating it.  */
7232       if (tree_type == TRUTH_ANDIF_EXPR)
7233         c_inhibit_evaluation_warnings += lhs == truthvalue_false_node;
7234       else if (tree_type == TRUTH_ORIF_EXPR)
7235         c_inhibit_evaluation_warnings += lhs == truthvalue_true_node;
7236
7237       /* Extract another operand.  It may be the RHS of this expression
7238          or the LHS of a new, higher priority expression.  */
7239       rhs = cp_parser_simple_cast_expression (parser);
7240       rhs_type = ERROR_MARK;
7241
7242       /* Get another operator token.  Look up its precedence to avoid
7243          building a useless (immediately popped) stack entry for common
7244          cases such as 3 + 4 + 5 or 3 * 4 + 5.  */
7245       token = cp_lexer_peek_token (parser->lexer);
7246       lookahead_prec = TOKEN_PRECEDENCE (token);
7247       if (lookahead_prec > new_prec)
7248         {
7249           /* ... and prepare to parse the RHS of the new, higher priority
7250              expression.  Since precedence levels on the stack are
7251              monotonically increasing, we do not have to care about
7252              stack overflows.  */
7253           sp->prec = prec;
7254           sp->tree_type = tree_type;
7255           sp->lhs = lhs;
7256           sp->lhs_type = lhs_type;
7257           sp++;
7258           lhs = rhs;
7259           lhs_type = rhs_type;
7260           prec = new_prec;
7261           new_prec = lookahead_prec;
7262           goto get_rhs;
7263
7264          pop:
7265           lookahead_prec = new_prec;
7266           /* If the stack is not empty, we have parsed into LHS the right side
7267              (`4' in the example above) of an expression we had suspended.
7268              We can use the information on the stack to recover the LHS (`3')
7269              from the stack together with the tree code (`MULT_EXPR'), and
7270              the precedence of the higher level subexpression
7271              (`PREC_ADDITIVE_EXPRESSION').  TOKEN is the CPP_PLUS token,
7272              which will be used to actually build the additive expression.  */
7273           --sp;
7274           prec = sp->prec;
7275           tree_type = sp->tree_type;
7276           rhs = lhs;
7277           rhs_type = lhs_type;
7278           lhs = sp->lhs;
7279           lhs_type = sp->lhs_type;
7280         }
7281
7282       /* Undo the disabling of warnings done above.  */
7283       if (tree_type == TRUTH_ANDIF_EXPR)
7284         c_inhibit_evaluation_warnings -= lhs == truthvalue_false_node;
7285       else if (tree_type == TRUTH_ORIF_EXPR)
7286         c_inhibit_evaluation_warnings -= lhs == truthvalue_true_node;
7287
7288       overload = NULL;
7289       /* ??? Currently we pass lhs_type == ERROR_MARK and rhs_type ==
7290          ERROR_MARK for everything that is not a binary expression.
7291          This makes warn_about_parentheses miss some warnings that
7292          involve unary operators.  For unary expressions we should
7293          pass the correct tree_code unless the unary expression was
7294          surrounded by parentheses.
7295       */
7296       if (no_toplevel_fold_p
7297           && lookahead_prec <= prec
7298           && sp == stack
7299           && TREE_CODE_CLASS (tree_type) == tcc_comparison)
7300         lhs = build2 (tree_type, boolean_type_node, lhs, rhs);
7301       else
7302         lhs = build_x_binary_op (tree_type, lhs, lhs_type, rhs, rhs_type,
7303                                  &overload, tf_warning_or_error);
7304       lhs_type = tree_type;
7305
7306       /* If the binary operator required the use of an overloaded operator,
7307          then this expression cannot be an integral constant-expression.
7308          An overloaded operator can be used even if both operands are
7309          otherwise permissible in an integral constant-expression if at
7310          least one of the operands is of enumeration type.  */
7311
7312       if (overload
7313           && cp_parser_non_integral_constant_expression (parser,
7314                                                          NIC_OVERLOADED))
7315         return error_mark_node;
7316     }
7317
7318   return lhs;
7319 }
7320
7321
7322 /* Parse the `? expression : assignment-expression' part of a
7323    conditional-expression.  The LOGICAL_OR_EXPR is the
7324    logical-or-expression that started the conditional-expression.
7325    Returns a representation of the entire conditional-expression.
7326
7327    This routine is used by cp_parser_assignment_expression.
7328
7329      ? expression : assignment-expression
7330
7331    GNU Extensions:
7332
7333      ? : assignment-expression */
7334
7335 static tree
7336 cp_parser_question_colon_clause (cp_parser* parser, tree logical_or_expr)
7337 {
7338   tree expr;
7339   tree assignment_expr;
7340   struct cp_token *token;
7341
7342   /* Consume the `?' token.  */
7343   cp_lexer_consume_token (parser->lexer);
7344   token = cp_lexer_peek_token (parser->lexer);
7345   if (cp_parser_allow_gnu_extensions_p (parser)
7346       && token->type == CPP_COLON)
7347     {
7348       pedwarn (token->location, OPT_pedantic, 
7349                "ISO C++ does not allow ?: with omitted middle operand");
7350       /* Implicit true clause.  */
7351       expr = NULL_TREE;
7352       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_true_node;
7353       warn_for_omitted_condop (token->location, logical_or_expr);
7354     }
7355   else
7356     {
7357       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
7358       parser->colon_corrects_to_scope_p = false;
7359       /* Parse the expression.  */
7360       c_inhibit_evaluation_warnings += logical_or_expr == truthvalue_false_node;
7361       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
7362       c_inhibit_evaluation_warnings +=
7363         ((logical_or_expr == truthvalue_true_node)
7364          - (logical_or_expr == truthvalue_false_node));
7365       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
7366     }
7367
7368   /* The next token should be a `:'.  */
7369   cp_parser_require (parser, CPP_COLON, RT_COLON);
7370   /* Parse the assignment-expression.  */
7371   assignment_expr = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7372   c_inhibit_evaluation_warnings -= logical_or_expr == truthvalue_true_node;
7373
7374   /* Build the conditional-expression.  */
7375   return build_x_conditional_expr (logical_or_expr,
7376                                    expr,
7377                                    assignment_expr,
7378                                    tf_warning_or_error);
7379 }
7380
7381 /* Parse an assignment-expression.
7382
7383    assignment-expression:
7384      conditional-expression
7385      logical-or-expression assignment-operator assignment_expression
7386      throw-expression
7387
7388    CAST_P is true if this expression is the target of a cast.
7389
7390    Returns a representation for the expression.  */
7391
7392 static tree
7393 cp_parser_assignment_expression (cp_parser* parser, bool cast_p,
7394                                  cp_id_kind * pidk)
7395 {
7396   tree expr;
7397
7398   /* If the next token is the `throw' keyword, then we're looking at
7399      a throw-expression.  */
7400   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THROW))
7401     expr = cp_parser_throw_expression (parser);
7402   /* Otherwise, it must be that we are looking at a
7403      logical-or-expression.  */
7404   else
7405     {
7406       /* Parse the binary expressions (logical-or-expression).  */
7407       expr = cp_parser_binary_expression (parser, cast_p, false,
7408                                           PREC_NOT_OPERATOR, pidk);
7409       /* If the next token is a `?' then we're actually looking at a
7410          conditional-expression.  */
7411       if (cp_lexer_next_token_is (parser->lexer, CPP_QUERY))
7412         return cp_parser_question_colon_clause (parser, expr);
7413       else
7414         {
7415           enum tree_code assignment_operator;
7416
7417           /* If it's an assignment-operator, we're using the second
7418              production.  */
7419           assignment_operator
7420             = cp_parser_assignment_operator_opt (parser);
7421           if (assignment_operator != ERROR_MARK)
7422             {
7423               bool non_constant_p;
7424
7425               /* Parse the right-hand side of the assignment.  */
7426               tree rhs = cp_parser_initializer_clause (parser, &non_constant_p);
7427
7428               if (BRACE_ENCLOSED_INITIALIZER_P (rhs))
7429                 maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
7430
7431               /* An assignment may not appear in a
7432                  constant-expression.  */
7433               if (cp_parser_non_integral_constant_expression (parser,
7434                                                               NIC_ASSIGNMENT))
7435                 return error_mark_node;
7436               /* Build the assignment expression.  */
7437               expr = build_x_modify_expr (expr,
7438                                           assignment_operator,
7439                                           rhs,
7440                                           tf_warning_or_error);
7441             }
7442         }
7443     }
7444
7445   return expr;
7446 }
7447
7448 /* Parse an (optional) assignment-operator.
7449
7450    assignment-operator: one of
7451      = *= /= %= += -= >>= <<= &= ^= |=
7452
7453    GNU Extension:
7454
7455    assignment-operator: one of
7456      <?= >?=
7457
7458    If the next token is an assignment operator, the corresponding tree
7459    code is returned, and the token is consumed.  For example, for
7460    `+=', PLUS_EXPR is returned.  For `=' itself, the code returned is
7461    NOP_EXPR.  For `/', TRUNC_DIV_EXPR is returned; for `%',
7462    TRUNC_MOD_EXPR is returned.  If TOKEN is not an assignment
7463    operator, ERROR_MARK is returned.  */
7464
7465 static enum tree_code
7466 cp_parser_assignment_operator_opt (cp_parser* parser)
7467 {
7468   enum tree_code op;
7469   cp_token *token;
7470
7471   /* Peek at the next token.  */
7472   token = cp_lexer_peek_token (parser->lexer);
7473
7474   switch (token->type)
7475     {
7476     case CPP_EQ:
7477       op = NOP_EXPR;
7478       break;
7479
7480     case CPP_MULT_EQ:
7481       op = MULT_EXPR;
7482       break;
7483
7484     case CPP_DIV_EQ:
7485       op = TRUNC_DIV_EXPR;
7486       break;
7487
7488     case CPP_MOD_EQ:
7489       op = TRUNC_MOD_EXPR;
7490       break;
7491
7492     case CPP_PLUS_EQ:
7493       op = PLUS_EXPR;
7494       break;
7495
7496     case CPP_MINUS_EQ:
7497       op = MINUS_EXPR;
7498       break;
7499
7500     case CPP_RSHIFT_EQ:
7501       op = RSHIFT_EXPR;
7502       break;
7503
7504     case CPP_LSHIFT_EQ:
7505       op = LSHIFT_EXPR;
7506       break;
7507
7508     case CPP_AND_EQ:
7509       op = BIT_AND_EXPR;
7510       break;
7511
7512     case CPP_XOR_EQ:
7513       op = BIT_XOR_EXPR;
7514       break;
7515
7516     case CPP_OR_EQ:
7517       op = BIT_IOR_EXPR;
7518       break;
7519
7520     default:
7521       /* Nothing else is an assignment operator.  */
7522       op = ERROR_MARK;
7523     }
7524
7525   /* If it was an assignment operator, consume it.  */
7526   if (op != ERROR_MARK)
7527     cp_lexer_consume_token (parser->lexer);
7528
7529   return op;
7530 }
7531
7532 /* Parse an expression.
7533
7534    expression:
7535      assignment-expression
7536      expression , assignment-expression
7537
7538    CAST_P is true if this expression is the target of a cast.
7539
7540    Returns a representation of the expression.  */
7541
7542 static tree
7543 cp_parser_expression (cp_parser* parser, bool cast_p, cp_id_kind * pidk)
7544 {
7545   tree expression = NULL_TREE;
7546
7547   while (true)
7548     {
7549       tree assignment_expression;
7550
7551       /* Parse the next assignment-expression.  */
7552       assignment_expression
7553         = cp_parser_assignment_expression (parser, cast_p, pidk);
7554       /* If this is the first assignment-expression, we can just
7555          save it away.  */
7556       if (!expression)
7557         expression = assignment_expression;
7558       else
7559         expression = build_x_compound_expr (expression,
7560                                             assignment_expression,
7561                                             tf_warning_or_error);
7562       /* If the next token is not a comma, then we are done with the
7563          expression.  */
7564       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
7565         break;
7566       /* Consume the `,'.  */
7567       cp_lexer_consume_token (parser->lexer);
7568       /* A comma operator cannot appear in a constant-expression.  */
7569       if (cp_parser_non_integral_constant_expression (parser, NIC_COMMA))
7570         expression = error_mark_node;
7571     }
7572
7573   return expression;
7574 }
7575
7576 /* Parse a constant-expression.
7577
7578    constant-expression:
7579      conditional-expression
7580
7581   If ALLOW_NON_CONSTANT_P a non-constant expression is silently
7582   accepted.  If ALLOW_NON_CONSTANT_P is true and the expression is not
7583   constant, *NON_CONSTANT_P is set to TRUE.  If ALLOW_NON_CONSTANT_P
7584   is false, NON_CONSTANT_P should be NULL.  */
7585
7586 static tree
7587 cp_parser_constant_expression (cp_parser* parser,
7588                                bool allow_non_constant_p,
7589                                bool *non_constant_p)
7590 {
7591   bool saved_integral_constant_expression_p;
7592   bool saved_allow_non_integral_constant_expression_p;
7593   bool saved_non_integral_constant_expression_p;
7594   tree expression;
7595
7596   /* It might seem that we could simply parse the
7597      conditional-expression, and then check to see if it were
7598      TREE_CONSTANT.  However, an expression that is TREE_CONSTANT is
7599      one that the compiler can figure out is constant, possibly after
7600      doing some simplifications or optimizations.  The standard has a
7601      precise definition of constant-expression, and we must honor
7602      that, even though it is somewhat more restrictive.
7603
7604      For example:
7605
7606        int i[(2, 3)];
7607
7608      is not a legal declaration, because `(2, 3)' is not a
7609      constant-expression.  The `,' operator is forbidden in a
7610      constant-expression.  However, GCC's constant-folding machinery
7611      will fold this operation to an INTEGER_CST for `3'.  */
7612
7613   /* Save the old settings.  */
7614   saved_integral_constant_expression_p = parser->integral_constant_expression_p;
7615   saved_allow_non_integral_constant_expression_p
7616     = parser->allow_non_integral_constant_expression_p;
7617   saved_non_integral_constant_expression_p = parser->non_integral_constant_expression_p;
7618   /* We are now parsing a constant-expression.  */
7619   parser->integral_constant_expression_p = true;
7620   parser->allow_non_integral_constant_expression_p
7621     = (allow_non_constant_p || cxx_dialect >= cxx0x);
7622   parser->non_integral_constant_expression_p = false;
7623   /* Although the grammar says "conditional-expression", we parse an
7624      "assignment-expression", which also permits "throw-expression"
7625      and the use of assignment operators.  In the case that
7626      ALLOW_NON_CONSTANT_P is false, we get better errors than we would
7627      otherwise.  In the case that ALLOW_NON_CONSTANT_P is true, it is
7628      actually essential that we look for an assignment-expression.
7629      For example, cp_parser_initializer_clauses uses this function to
7630      determine whether a particular assignment-expression is in fact
7631      constant.  */
7632   expression = cp_parser_assignment_expression (parser, /*cast_p=*/false, NULL);
7633   /* Restore the old settings.  */
7634   parser->integral_constant_expression_p
7635     = saved_integral_constant_expression_p;
7636   parser->allow_non_integral_constant_expression_p
7637     = saved_allow_non_integral_constant_expression_p;
7638   if (cxx_dialect >= cxx0x)
7639     {
7640       /* Require an rvalue constant expression here; that's what our
7641          callers expect.  Reference constant expressions are handled
7642          separately in e.g. cp_parser_template_argument.  */
7643       bool is_const = potential_rvalue_constant_expression (expression);
7644       parser->non_integral_constant_expression_p = !is_const;
7645       if (!is_const && !allow_non_constant_p)
7646         require_potential_rvalue_constant_expression (expression);
7647     }
7648   if (allow_non_constant_p)
7649     *non_constant_p = parser->non_integral_constant_expression_p;
7650   parser->non_integral_constant_expression_p
7651     = saved_non_integral_constant_expression_p;
7652
7653   return expression;
7654 }
7655
7656 /* Parse __builtin_offsetof.
7657
7658    offsetof-expression:
7659      "__builtin_offsetof" "(" type-id "," offsetof-member-designator ")"
7660
7661    offsetof-member-designator:
7662      id-expression
7663      | offsetof-member-designator "." id-expression
7664      | offsetof-member-designator "[" expression "]"
7665      | offsetof-member-designator "->" id-expression  */
7666
7667 static tree
7668 cp_parser_builtin_offsetof (cp_parser *parser)
7669 {
7670   int save_ice_p, save_non_ice_p;
7671   tree type, expr;
7672   cp_id_kind dummy;
7673   cp_token *token;
7674
7675   /* We're about to accept non-integral-constant things, but will
7676      definitely yield an integral constant expression.  Save and
7677      restore these values around our local parsing.  */
7678   save_ice_p = parser->integral_constant_expression_p;
7679   save_non_ice_p = parser->non_integral_constant_expression_p;
7680
7681   /* Consume the "__builtin_offsetof" token.  */
7682   cp_lexer_consume_token (parser->lexer);
7683   /* Consume the opening `('.  */
7684   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7685   /* Parse the type-id.  */
7686   type = cp_parser_type_id (parser);
7687   /* Look for the `,'.  */
7688   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7689   token = cp_lexer_peek_token (parser->lexer);
7690
7691   /* Build the (type *)null that begins the traditional offsetof macro.  */
7692   expr = build_static_cast (build_pointer_type (type), null_pointer_node,
7693                             tf_warning_or_error);
7694
7695   /* Parse the offsetof-member-designator.  We begin as if we saw "expr->".  */
7696   expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DEREF, expr,
7697                                                  true, &dummy, token->location);
7698   while (true)
7699     {
7700       token = cp_lexer_peek_token (parser->lexer);
7701       switch (token->type)
7702         {
7703         case CPP_OPEN_SQUARE:
7704           /* offsetof-member-designator "[" expression "]" */
7705           expr = cp_parser_postfix_open_square_expression (parser, expr, true);
7706           break;
7707
7708         case CPP_DEREF:
7709           /* offsetof-member-designator "->" identifier */
7710           expr = grok_array_decl (expr, integer_zero_node);
7711           /* FALLTHRU */
7712
7713         case CPP_DOT:
7714           /* offsetof-member-designator "." identifier */
7715           cp_lexer_consume_token (parser->lexer);
7716           expr = cp_parser_postfix_dot_deref_expression (parser, CPP_DOT,
7717                                                          expr, true, &dummy,
7718                                                          token->location);
7719           break;
7720
7721         case CPP_CLOSE_PAREN:
7722           /* Consume the ")" token.  */
7723           cp_lexer_consume_token (parser->lexer);
7724           goto success;
7725
7726         default:
7727           /* Error.  We know the following require will fail, but
7728              that gives the proper error message.  */
7729           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7730           cp_parser_skip_to_closing_parenthesis (parser, true, false, true);
7731           expr = error_mark_node;
7732           goto failure;
7733         }
7734     }
7735
7736  success:
7737   /* If we're processing a template, we can't finish the semantics yet.
7738      Otherwise we can fold the entire expression now.  */
7739   if (processing_template_decl)
7740     expr = build1 (OFFSETOF_EXPR, size_type_node, expr);
7741   else
7742     expr = finish_offsetof (expr);
7743
7744  failure:
7745   parser->integral_constant_expression_p = save_ice_p;
7746   parser->non_integral_constant_expression_p = save_non_ice_p;
7747
7748   return expr;
7749 }
7750
7751 /* Parse a trait expression.
7752
7753    Returns a representation of the expression, the underlying type
7754    of the type at issue when KEYWORD is RID_UNDERLYING_TYPE.  */
7755
7756 static tree
7757 cp_parser_trait_expr (cp_parser* parser, enum rid keyword)
7758 {
7759   cp_trait_kind kind;
7760   tree type1, type2 = NULL_TREE;
7761   bool binary = false;
7762   cp_decl_specifier_seq decl_specs;
7763
7764   switch (keyword)
7765     {
7766     case RID_HAS_NOTHROW_ASSIGN:
7767       kind = CPTK_HAS_NOTHROW_ASSIGN;
7768       break;
7769     case RID_HAS_NOTHROW_CONSTRUCTOR:
7770       kind = CPTK_HAS_NOTHROW_CONSTRUCTOR;
7771       break;
7772     case RID_HAS_NOTHROW_COPY:
7773       kind = CPTK_HAS_NOTHROW_COPY;
7774       break;
7775     case RID_HAS_TRIVIAL_ASSIGN:
7776       kind = CPTK_HAS_TRIVIAL_ASSIGN;
7777       break;
7778     case RID_HAS_TRIVIAL_CONSTRUCTOR:
7779       kind = CPTK_HAS_TRIVIAL_CONSTRUCTOR;
7780       break;
7781     case RID_HAS_TRIVIAL_COPY:
7782       kind = CPTK_HAS_TRIVIAL_COPY;
7783       break;
7784     case RID_HAS_TRIVIAL_DESTRUCTOR:
7785       kind = CPTK_HAS_TRIVIAL_DESTRUCTOR;
7786       break;
7787     case RID_HAS_VIRTUAL_DESTRUCTOR:
7788       kind = CPTK_HAS_VIRTUAL_DESTRUCTOR;
7789       break;
7790     case RID_IS_ABSTRACT:
7791       kind = CPTK_IS_ABSTRACT;
7792       break;
7793     case RID_IS_BASE_OF:
7794       kind = CPTK_IS_BASE_OF;
7795       binary = true;
7796       break;
7797     case RID_IS_CLASS:
7798       kind = CPTK_IS_CLASS;
7799       break;
7800     case RID_IS_CONVERTIBLE_TO:
7801       kind = CPTK_IS_CONVERTIBLE_TO;
7802       binary = true;
7803       break;
7804     case RID_IS_EMPTY:
7805       kind = CPTK_IS_EMPTY;
7806       break;
7807     case RID_IS_ENUM:
7808       kind = CPTK_IS_ENUM;
7809       break;
7810     case RID_IS_LITERAL_TYPE:
7811       kind = CPTK_IS_LITERAL_TYPE;
7812       break;
7813     case RID_IS_POD:
7814       kind = CPTK_IS_POD;
7815       break;
7816     case RID_IS_POLYMORPHIC:
7817       kind = CPTK_IS_POLYMORPHIC;
7818       break;
7819     case RID_IS_STD_LAYOUT:
7820       kind = CPTK_IS_STD_LAYOUT;
7821       break;
7822     case RID_IS_TRIVIAL:
7823       kind = CPTK_IS_TRIVIAL;
7824       break;
7825     case RID_IS_UNION:
7826       kind = CPTK_IS_UNION;
7827       break;
7828     case RID_UNDERLYING_TYPE:
7829       kind = CPTK_UNDERLYING_TYPE;
7830       break;
7831     case RID_BASES:
7832       kind = CPTK_BASES;
7833       break;
7834     case RID_DIRECT_BASES:
7835       kind = CPTK_DIRECT_BASES;
7836       break;
7837     default:
7838       gcc_unreachable ();
7839     }
7840
7841   /* Consume the token.  */
7842   cp_lexer_consume_token (parser->lexer);
7843
7844   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
7845
7846   type1 = cp_parser_type_id (parser);
7847
7848   if (type1 == error_mark_node)
7849     return error_mark_node;
7850
7851   /* Build a trivial decl-specifier-seq.  */
7852   clear_decl_specs (&decl_specs);
7853   decl_specs.type = type1;
7854
7855   /* Call grokdeclarator to figure out what type this is.  */
7856   type1 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7857                           /*initialized=*/0, /*attrlist=*/NULL);
7858
7859   if (binary)
7860     {
7861       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
7862  
7863       type2 = cp_parser_type_id (parser);
7864
7865       if (type2 == error_mark_node)
7866         return error_mark_node;
7867
7868       /* Build a trivial decl-specifier-seq.  */
7869       clear_decl_specs (&decl_specs);
7870       decl_specs.type = type2;
7871
7872       /* Call grokdeclarator to figure out what type this is.  */
7873       type2 = grokdeclarator (NULL, &decl_specs, TYPENAME,
7874                               /*initialized=*/0, /*attrlist=*/NULL);
7875     }
7876
7877   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
7878
7879   /* Complete the trait expression, which may mean either processing
7880      the trait expr now or saving it for template instantiation.  */
7881   switch(kind)
7882     {
7883     case CPTK_UNDERLYING_TYPE:
7884       return finish_underlying_type (type1);
7885     case CPTK_BASES:
7886       return finish_bases (type1, false);
7887     case CPTK_DIRECT_BASES:
7888       return finish_bases (type1, true);
7889     default:
7890       return finish_trait_expr (kind, type1, type2);
7891     }
7892 }
7893
7894 /* Lambdas that appear in variable initializer or default argument scope
7895    get that in their mangling, so we need to record it.  We might as well
7896    use the count for function and namespace scopes as well.  */
7897 static GTY(()) tree lambda_scope;
7898 static GTY(()) int lambda_count;
7899 typedef struct GTY(()) tree_int
7900 {
7901   tree t;
7902   int i;
7903 } tree_int;
7904 DEF_VEC_O(tree_int);
7905 DEF_VEC_ALLOC_O(tree_int,gc);
7906 static GTY(()) VEC(tree_int,gc) *lambda_scope_stack;
7907
7908 static void
7909 start_lambda_scope (tree decl)
7910 {
7911   tree_int ti;
7912   gcc_assert (decl);
7913   /* Once we're inside a function, we ignore other scopes and just push
7914      the function again so that popping works properly.  */
7915   if (current_function_decl && TREE_CODE (decl) != FUNCTION_DECL)
7916     decl = current_function_decl;
7917   ti.t = lambda_scope;
7918   ti.i = lambda_count;
7919   VEC_safe_push (tree_int, gc, lambda_scope_stack, &ti);
7920   if (lambda_scope != decl)
7921     {
7922       /* Don't reset the count if we're still in the same function.  */
7923       lambda_scope = decl;
7924       lambda_count = 0;
7925     }
7926 }
7927
7928 static void
7929 record_lambda_scope (tree lambda)
7930 {
7931   LAMBDA_EXPR_EXTRA_SCOPE (lambda) = lambda_scope;
7932   LAMBDA_EXPR_DISCRIMINATOR (lambda) = lambda_count++;
7933 }
7934
7935 static void
7936 finish_lambda_scope (void)
7937 {
7938   tree_int *p = VEC_last (tree_int, lambda_scope_stack);
7939   if (lambda_scope != p->t)
7940     {
7941       lambda_scope = p->t;
7942       lambda_count = p->i;
7943     }
7944   VEC_pop (tree_int, lambda_scope_stack);
7945 }
7946
7947 /* Parse a lambda expression.
7948
7949    lambda-expression:
7950      lambda-introducer lambda-declarator [opt] compound-statement
7951
7952    Returns a representation of the expression.  */
7953
7954 static tree
7955 cp_parser_lambda_expression (cp_parser* parser)
7956 {
7957   tree lambda_expr = build_lambda_expr ();
7958   tree type;
7959   bool ok;
7960
7961   LAMBDA_EXPR_LOCATION (lambda_expr)
7962     = cp_lexer_peek_token (parser->lexer)->location;
7963
7964   if (cp_unevaluated_operand)
7965     error_at (LAMBDA_EXPR_LOCATION (lambda_expr),
7966               "lambda-expression in unevaluated context");
7967
7968   /* We may be in the middle of deferred access check.  Disable
7969      it now.  */
7970   push_deferring_access_checks (dk_no_deferred);
7971
7972   cp_parser_lambda_introducer (parser, lambda_expr);
7973
7974   type = begin_lambda_type (lambda_expr);
7975
7976   record_lambda_scope (lambda_expr);
7977
7978   /* Do this again now that LAMBDA_EXPR_EXTRA_SCOPE is set.  */
7979   determine_visibility (TYPE_NAME (type));
7980
7981   /* Now that we've started the type, add the capture fields for any
7982      explicit captures.  */
7983   register_capture_members (LAMBDA_EXPR_CAPTURE_LIST (lambda_expr));
7984
7985   {
7986     /* Inside the class, surrounding template-parameter-lists do not apply.  */
7987     unsigned int saved_num_template_parameter_lists
7988         = parser->num_template_parameter_lists;
7989     unsigned char in_statement = parser->in_statement;
7990     bool in_switch_statement_p = parser->in_switch_statement_p;
7991
7992     parser->num_template_parameter_lists = 0;
7993     parser->in_statement = 0;
7994     parser->in_switch_statement_p = false;
7995
7996     /* By virtue of defining a local class, a lambda expression has access to
7997        the private variables of enclosing classes.  */
7998
7999     ok = cp_parser_lambda_declarator_opt (parser, lambda_expr);
8000
8001     if (ok)
8002       cp_parser_lambda_body (parser, lambda_expr);
8003     else if (cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8004       cp_parser_skip_to_end_of_block_or_statement (parser);
8005
8006     /* The capture list was built up in reverse order; fix that now.  */
8007     {
8008       tree newlist = NULL_TREE;
8009       tree elt, next;
8010
8011       for (elt = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr);
8012            elt; elt = next)
8013         {
8014           next = TREE_CHAIN (elt);
8015           TREE_CHAIN (elt) = newlist;
8016           newlist = elt;
8017         }
8018       LAMBDA_EXPR_CAPTURE_LIST (lambda_expr) = newlist;
8019     }
8020
8021     if (ok)
8022       maybe_add_lambda_conv_op (type);
8023
8024     type = finish_struct (type, /*attributes=*/NULL_TREE);
8025
8026     parser->num_template_parameter_lists = saved_num_template_parameter_lists;
8027     parser->in_statement = in_statement;
8028     parser->in_switch_statement_p = in_switch_statement_p;
8029   }
8030
8031   pop_deferring_access_checks ();
8032
8033   /* This field is only used during parsing of the lambda.  */
8034   LAMBDA_EXPR_THIS_CAPTURE (lambda_expr) = NULL_TREE;
8035
8036   /* This lambda shouldn't have any proxies left at this point.  */
8037   gcc_assert (LAMBDA_EXPR_PENDING_PROXIES (lambda_expr) == NULL);
8038   /* And now that we're done, push proxies for an enclosing lambda.  */
8039   insert_pending_capture_proxies ();
8040
8041   if (ok)
8042     return build_lambda_object (lambda_expr);
8043   else
8044     return error_mark_node;
8045 }
8046
8047 /* Parse the beginning of a lambda expression.
8048
8049    lambda-introducer:
8050      [ lambda-capture [opt] ]
8051
8052    LAMBDA_EXPR is the current representation of the lambda expression.  */
8053
8054 static void
8055 cp_parser_lambda_introducer (cp_parser* parser, tree lambda_expr)
8056 {
8057   /* Need commas after the first capture.  */
8058   bool first = true;
8059
8060   /* Eat the leading `['.  */
8061   cp_parser_require (parser, CPP_OPEN_SQUARE, RT_OPEN_SQUARE);
8062
8063   /* Record default capture mode.  "[&" "[=" "[&," "[=,"  */
8064   if (cp_lexer_next_token_is (parser->lexer, CPP_AND)
8065       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_NAME)
8066     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_REFERENCE;
8067   else if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8068     LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) = CPLD_COPY;
8069
8070   if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE)
8071     {
8072       cp_lexer_consume_token (parser->lexer);
8073       first = false;
8074     }
8075
8076   while (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_SQUARE))
8077     {
8078       cp_token* capture_token;
8079       tree capture_id;
8080       tree capture_init_expr;
8081       cp_id_kind idk = CP_ID_KIND_NONE;
8082       bool explicit_init_p = false;
8083
8084       enum capture_kind_type
8085       {
8086         BY_COPY,
8087         BY_REFERENCE
8088       };
8089       enum capture_kind_type capture_kind = BY_COPY;
8090
8091       if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
8092         {
8093           error ("expected end of capture-list");
8094           return;
8095         }
8096
8097       if (first)
8098         first = false;
8099       else
8100         cp_parser_require (parser, CPP_COMMA, RT_COMMA);
8101
8102       /* Possibly capture `this'.  */
8103       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_THIS))
8104         {
8105           location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8106           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY)
8107             pedwarn (loc, 0, "explicit by-copy capture of %<this%> redundant "
8108                      "with by-copy capture default");
8109           cp_lexer_consume_token (parser->lexer);
8110           add_capture (lambda_expr,
8111                        /*id=*/this_identifier,
8112                        /*initializer=*/finish_this_expr(),
8113                        /*by_reference_p=*/false,
8114                        explicit_init_p);
8115           continue;
8116         }
8117
8118       /* Remember whether we want to capture as a reference or not.  */
8119       if (cp_lexer_next_token_is (parser->lexer, CPP_AND))
8120         {
8121           capture_kind = BY_REFERENCE;
8122           cp_lexer_consume_token (parser->lexer);
8123         }
8124
8125       /* Get the identifier.  */
8126       capture_token = cp_lexer_peek_token (parser->lexer);
8127       capture_id = cp_parser_identifier (parser);
8128
8129       if (capture_id == error_mark_node)
8130         /* Would be nice to have a cp_parser_skip_to_closing_x for general
8131            delimiters, but I modified this to stop on unnested ']' as well.  It
8132            was already changed to stop on unnested '}', so the
8133            "closing_parenthesis" name is no more misleading with my change.  */
8134         {
8135           cp_parser_skip_to_closing_parenthesis (parser,
8136                                                  /*recovering=*/true,
8137                                                  /*or_comma=*/true,
8138                                                  /*consume_paren=*/true);
8139           break;
8140         }
8141
8142       /* Find the initializer for this capture.  */
8143       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
8144         {
8145           /* An explicit expression exists.  */
8146           cp_lexer_consume_token (parser->lexer);
8147           pedwarn (input_location, OPT_pedantic,
8148                    "ISO C++ does not allow initializers "
8149                    "in lambda expression capture lists");
8150           capture_init_expr = cp_parser_assignment_expression (parser,
8151                                                                /*cast_p=*/true,
8152                                                                &idk);
8153           explicit_init_p = true;
8154         }
8155       else
8156         {
8157           const char* error_msg;
8158
8159           /* Turn the identifier into an id-expression.  */
8160           capture_init_expr
8161             = cp_parser_lookup_name
8162                 (parser,
8163                  capture_id,
8164                  none_type,
8165                  /*is_template=*/false,
8166                  /*is_namespace=*/false,
8167                  /*check_dependency=*/true,
8168                  /*ambiguous_decls=*/NULL,
8169                  capture_token->location);
8170
8171           if (capture_init_expr == error_mark_node)
8172             {
8173               unqualified_name_lookup_error (capture_id);
8174               continue;
8175             }
8176           else if (DECL_P (capture_init_expr)
8177                    && (TREE_CODE (capture_init_expr) != VAR_DECL
8178                        && TREE_CODE (capture_init_expr) != PARM_DECL))
8179             {
8180               error_at (capture_token->location,
8181                         "capture of non-variable %qD ",
8182                         capture_init_expr);
8183               inform (0, "%q+#D declared here", capture_init_expr);
8184               continue;
8185             }
8186           if (TREE_CODE (capture_init_expr) == VAR_DECL
8187               && decl_storage_duration (capture_init_expr) != dk_auto)
8188             {
8189               pedwarn (capture_token->location, 0, "capture of variable "
8190                        "%qD with non-automatic storage duration",
8191                        capture_init_expr);
8192               inform (0, "%q+#D declared here", capture_init_expr);
8193               continue;
8194             }
8195
8196           capture_init_expr
8197             = finish_id_expression
8198                 (capture_id,
8199                  capture_init_expr,
8200                  parser->scope,
8201                  &idk,
8202                  /*integral_constant_expression_p=*/false,
8203                  /*allow_non_integral_constant_expression_p=*/false,
8204                  /*non_integral_constant_expression_p=*/NULL,
8205                  /*template_p=*/false,
8206                  /*done=*/true,
8207                  /*address_p=*/false,
8208                  /*template_arg_p=*/false,
8209                  &error_msg,
8210                  capture_token->location);
8211         }
8212
8213       if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) != CPLD_NONE
8214           && !explicit_init_p)
8215         {
8216           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_COPY
8217               && capture_kind == BY_COPY)
8218             pedwarn (capture_token->location, 0, "explicit by-copy capture "
8219                      "of %qD redundant with by-copy capture default",
8220                      capture_id);
8221           if (LAMBDA_EXPR_DEFAULT_CAPTURE_MODE (lambda_expr) == CPLD_REFERENCE
8222               && capture_kind == BY_REFERENCE)
8223             pedwarn (capture_token->location, 0, "explicit by-reference "
8224                      "capture of %qD redundant with by-reference capture "
8225                      "default", capture_id);
8226         }
8227
8228       add_capture (lambda_expr,
8229                    capture_id,
8230                    capture_init_expr,
8231                    /*by_reference_p=*/capture_kind == BY_REFERENCE,
8232                    explicit_init_p);
8233     }
8234
8235   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
8236 }
8237
8238 /* Parse the (optional) middle of a lambda expression.
8239
8240    lambda-declarator:
8241      ( parameter-declaration-clause [opt] )
8242        attribute-specifier [opt]
8243        mutable [opt]
8244        exception-specification [opt]
8245        lambda-return-type-clause [opt]
8246
8247    LAMBDA_EXPR is the current representation of the lambda expression.  */
8248
8249 static bool
8250 cp_parser_lambda_declarator_opt (cp_parser* parser, tree lambda_expr)
8251 {
8252   /* 5.1.1.4 of the standard says:
8253        If a lambda-expression does not include a lambda-declarator, it is as if
8254        the lambda-declarator were ().
8255      This means an empty parameter list, no attributes, and no exception
8256      specification.  */
8257   tree param_list = void_list_node;
8258   tree attributes = NULL_TREE;
8259   tree exception_spec = NULL_TREE;
8260   tree t;
8261
8262   /* The lambda-declarator is optional, but must begin with an opening
8263      parenthesis if present.  */
8264   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
8265     {
8266       cp_lexer_consume_token (parser->lexer);
8267
8268       begin_scope (sk_function_parms, /*entity=*/NULL_TREE);
8269
8270       /* Parse parameters.  */
8271       param_list = cp_parser_parameter_declaration_clause (parser);
8272
8273       /* Default arguments shall not be specified in the
8274          parameter-declaration-clause of a lambda-declarator.  */
8275       for (t = param_list; t; t = TREE_CHAIN (t))
8276         if (TREE_PURPOSE (t))
8277           pedwarn (DECL_SOURCE_LOCATION (TREE_VALUE (t)), OPT_pedantic,
8278                    "default argument specified for lambda parameter");
8279
8280       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
8281
8282       attributes = cp_parser_attributes_opt (parser);
8283
8284       /* Parse optional `mutable' keyword.  */
8285       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_MUTABLE))
8286         {
8287           cp_lexer_consume_token (parser->lexer);
8288           LAMBDA_EXPR_MUTABLE_P (lambda_expr) = 1;
8289         }
8290
8291       /* Parse optional exception specification.  */
8292       exception_spec = cp_parser_exception_specification_opt (parser);
8293
8294       /* Parse optional trailing return type.  */
8295       if (cp_lexer_next_token_is (parser->lexer, CPP_DEREF))
8296         {
8297           cp_lexer_consume_token (parser->lexer);
8298           LAMBDA_EXPR_RETURN_TYPE (lambda_expr) = cp_parser_type_id (parser);
8299         }
8300
8301       /* The function parameters must be in scope all the way until after the
8302          trailing-return-type in case of decltype.  */
8303       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
8304         pop_binding (DECL_NAME (t), t);
8305
8306       leave_scope ();
8307     }
8308
8309   /* Create the function call operator.
8310
8311      Messing with declarators like this is no uglier than building up the
8312      FUNCTION_DECL by hand, and this is less likely to get out of sync with
8313      other code.  */
8314   {
8315     cp_decl_specifier_seq return_type_specs;
8316     cp_declarator* declarator;
8317     tree fco;
8318     int quals;
8319     void *p;
8320
8321     clear_decl_specs (&return_type_specs);
8322     if (LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8323       return_type_specs.type = LAMBDA_EXPR_RETURN_TYPE (lambda_expr);
8324     else
8325       /* Maybe we will deduce the return type later, but we can use void
8326          as a placeholder return type anyways.  */
8327       return_type_specs.type = void_type_node;
8328
8329     p = obstack_alloc (&declarator_obstack, 0);
8330
8331     declarator = make_id_declarator (NULL_TREE, ansi_opname (CALL_EXPR),
8332                                      sfk_none);
8333
8334     quals = (LAMBDA_EXPR_MUTABLE_P (lambda_expr)
8335              ? TYPE_UNQUALIFIED : TYPE_QUAL_CONST);
8336     declarator = make_call_declarator (declarator, param_list, quals,
8337                                        VIRT_SPEC_UNSPECIFIED,
8338                                        exception_spec,
8339                                        /*late_return_type=*/NULL_TREE);
8340     declarator->id_loc = LAMBDA_EXPR_LOCATION (lambda_expr);
8341
8342     fco = grokmethod (&return_type_specs,
8343                       declarator,
8344                       attributes);
8345     if (fco != error_mark_node)
8346       {
8347         DECL_INITIALIZED_IN_CLASS_P (fco) = 1;
8348         DECL_ARTIFICIAL (fco) = 1;
8349         /* Give the object parameter a different name.  */
8350         DECL_NAME (DECL_ARGUMENTS (fco)) = get_identifier ("__closure");
8351       }
8352
8353     finish_member_declaration (fco);
8354
8355     obstack_free (&declarator_obstack, p);
8356
8357     return (fco != error_mark_node);
8358   }
8359 }
8360
8361 /* Parse the body of a lambda expression, which is simply
8362
8363    compound-statement
8364
8365    but which requires special handling.
8366    LAMBDA_EXPR is the current representation of the lambda expression.  */
8367
8368 static void
8369 cp_parser_lambda_body (cp_parser* parser, tree lambda_expr)
8370 {
8371   bool nested = (current_function_decl != NULL_TREE);
8372   bool local_variables_forbidden_p = parser->local_variables_forbidden_p;
8373   if (nested)
8374     push_function_context ();
8375   else
8376     /* Still increment function_depth so that we don't GC in the
8377        middle of an expression.  */
8378     ++function_depth;
8379   /* Clear this in case we're in the middle of a default argument.  */
8380   parser->local_variables_forbidden_p = false;
8381
8382   /* Finish the function call operator
8383      - class_specifier
8384      + late_parsing_for_member
8385      + function_definition_after_declarator
8386      + ctor_initializer_opt_and_function_body  */
8387   {
8388     tree fco = lambda_function (lambda_expr);
8389     tree body;
8390     bool done = false;
8391     tree compound_stmt;
8392     tree cap;
8393
8394     /* Let the front end know that we are going to be defining this
8395        function.  */
8396     start_preparsed_function (fco,
8397                               NULL_TREE,
8398                               SF_PRE_PARSED | SF_INCLASS_INLINE);
8399
8400     start_lambda_scope (fco);
8401     body = begin_function_body ();
8402
8403     if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8404       goto out;
8405
8406     /* Push the proxies for any explicit captures.  */
8407     for (cap = LAMBDA_EXPR_CAPTURE_LIST (lambda_expr); cap;
8408          cap = TREE_CHAIN (cap))
8409       build_capture_proxy (TREE_PURPOSE (cap));
8410
8411     compound_stmt = begin_compound_stmt (0);
8412
8413     /* 5.1.1.4 of the standard says:
8414          If a lambda-expression does not include a trailing-return-type, it
8415          is as if the trailing-return-type denotes the following type:
8416           * if the compound-statement is of the form
8417                { return attribute-specifier [opt] expression ; }
8418              the type of the returned expression after lvalue-to-rvalue
8419              conversion (_conv.lval_ 4.1), array-to-pointer conversion
8420              (_conv.array_ 4.2), and function-to-pointer conversion
8421              (_conv.func_ 4.3);
8422           * otherwise, void.  */
8423
8424     /* In a lambda that has neither a lambda-return-type-clause
8425        nor a deducible form, errors should be reported for return statements
8426        in the body.  Since we used void as the placeholder return type, parsing
8427        the body as usual will give such desired behavior.  */
8428     if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr)
8429         && cp_lexer_peek_nth_token (parser->lexer, 1)->keyword == RID_RETURN
8430         && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SEMICOLON)
8431       {
8432         tree expr = NULL_TREE;
8433         cp_id_kind idk = CP_ID_KIND_NONE;
8434
8435         /* Parse tentatively in case there's more after the initial return
8436            statement.  */
8437         cp_parser_parse_tentatively (parser);
8438
8439         cp_parser_require_keyword (parser, RID_RETURN, RT_RETURN);
8440
8441         expr = cp_parser_expression (parser, /*cast_p=*/false, &idk);
8442
8443         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
8444         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8445
8446         if (cp_parser_parse_definitely (parser))
8447           {
8448             apply_lambda_return_type (lambda_expr, lambda_return_type (expr));
8449
8450             /* Will get error here if type not deduced yet.  */
8451             finish_return_stmt (expr);
8452
8453             done = true;
8454           }
8455       }
8456
8457     if (!done)
8458       {
8459         if (!LAMBDA_EXPR_RETURN_TYPE (lambda_expr))
8460           LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = true;
8461         while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8462           cp_parser_label_declaration (parser);
8463         cp_parser_statement_seq_opt (parser, NULL_TREE);
8464         cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8465         LAMBDA_EXPR_DEDUCE_RETURN_TYPE_P (lambda_expr) = false;
8466       }
8467
8468     finish_compound_stmt (compound_stmt);
8469
8470   out:
8471     finish_function_body (body);
8472     finish_lambda_scope ();
8473
8474     /* Finish the function and generate code for it if necessary.  */
8475     expand_or_defer_fn (finish_function (/*inline*/2));
8476   }
8477
8478   parser->local_variables_forbidden_p = local_variables_forbidden_p;
8479   if (nested)
8480     pop_function_context();
8481   else
8482     --function_depth;
8483 }
8484
8485 /* Statements [gram.stmt.stmt]  */
8486
8487 /* Parse a statement.
8488
8489    statement:
8490      labeled-statement
8491      expression-statement
8492      compound-statement
8493      selection-statement
8494      iteration-statement
8495      jump-statement
8496      declaration-statement
8497      try-block
8498
8499   IN_COMPOUND is true when the statement is nested inside a
8500   cp_parser_compound_statement; this matters for certain pragmas.
8501
8502   If IF_P is not NULL, *IF_P is set to indicate whether the statement
8503   is a (possibly labeled) if statement which is not enclosed in braces
8504   and has an else clause.  This is used to implement -Wparentheses.  */
8505
8506 static void
8507 cp_parser_statement (cp_parser* parser, tree in_statement_expr,
8508                      bool in_compound, bool *if_p)
8509 {
8510   tree statement;
8511   cp_token *token;
8512   location_t statement_location;
8513
8514  restart:
8515   if (if_p != NULL)
8516     *if_p = false;
8517   /* There is no statement yet.  */
8518   statement = NULL_TREE;
8519   /* Peek at the next token.  */
8520   token = cp_lexer_peek_token (parser->lexer);
8521   /* Remember the location of the first token in the statement.  */
8522   statement_location = token->location;
8523   /* If this is a keyword, then that will often determine what kind of
8524      statement we have.  */
8525   if (token->type == CPP_KEYWORD)
8526     {
8527       enum rid keyword = token->keyword;
8528
8529       switch (keyword)
8530         {
8531         case RID_CASE:
8532         case RID_DEFAULT:
8533           /* Looks like a labeled-statement with a case label.
8534              Parse the label, and then use tail recursion to parse
8535              the statement.  */
8536           cp_parser_label_for_labeled_statement (parser);
8537           goto restart;
8538
8539         case RID_IF:
8540         case RID_SWITCH:
8541           statement = cp_parser_selection_statement (parser, if_p);
8542           break;
8543
8544         case RID_WHILE:
8545         case RID_DO:
8546         case RID_FOR:
8547           statement = cp_parser_iteration_statement (parser);
8548           break;
8549
8550         case RID_BREAK:
8551         case RID_CONTINUE:
8552         case RID_RETURN:
8553         case RID_GOTO:
8554           statement = cp_parser_jump_statement (parser);
8555           break;
8556
8557           /* Objective-C++ exception-handling constructs.  */
8558         case RID_AT_TRY:
8559         case RID_AT_CATCH:
8560         case RID_AT_FINALLY:
8561         case RID_AT_SYNCHRONIZED:
8562         case RID_AT_THROW:
8563           statement = cp_parser_objc_statement (parser);
8564           break;
8565
8566         case RID_TRY:
8567           statement = cp_parser_try_block (parser);
8568           break;
8569
8570         case RID_NAMESPACE:
8571           /* This must be a namespace alias definition.  */
8572           cp_parser_declaration_statement (parser);
8573           return;
8574           
8575         default:
8576           /* It might be a keyword like `int' that can start a
8577              declaration-statement.  */
8578           break;
8579         }
8580     }
8581   else if (token->type == CPP_NAME)
8582     {
8583       /* If the next token is a `:', then we are looking at a
8584          labeled-statement.  */
8585       token = cp_lexer_peek_nth_token (parser->lexer, 2);
8586       if (token->type == CPP_COLON)
8587         {
8588           /* Looks like a labeled-statement with an ordinary label.
8589              Parse the label, and then use tail recursion to parse
8590              the statement.  */
8591           cp_parser_label_for_labeled_statement (parser);
8592           goto restart;
8593         }
8594     }
8595   /* Anything that starts with a `{' must be a compound-statement.  */
8596   else if (token->type == CPP_OPEN_BRACE)
8597     statement = cp_parser_compound_statement (parser, NULL, false, false);
8598   /* CPP_PRAGMA is a #pragma inside a function body, which constitutes
8599      a statement all its own.  */
8600   else if (token->type == CPP_PRAGMA)
8601     {
8602       /* Only certain OpenMP pragmas are attached to statements, and thus
8603          are considered statements themselves.  All others are not.  In
8604          the context of a compound, accept the pragma as a "statement" and
8605          return so that we can check for a close brace.  Otherwise we
8606          require a real statement and must go back and read one.  */
8607       if (in_compound)
8608         cp_parser_pragma (parser, pragma_compound);
8609       else if (!cp_parser_pragma (parser, pragma_stmt))
8610         goto restart;
8611       return;
8612     }
8613   else if (token->type == CPP_EOF)
8614     {
8615       cp_parser_error (parser, "expected statement");
8616       return;
8617     }
8618
8619   /* Everything else must be a declaration-statement or an
8620      expression-statement.  Try for the declaration-statement
8621      first, unless we are looking at a `;', in which case we know that
8622      we have an expression-statement.  */
8623   if (!statement)
8624     {
8625       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8626         {
8627           cp_parser_parse_tentatively (parser);
8628           /* Try to parse the declaration-statement.  */
8629           cp_parser_declaration_statement (parser);
8630           /* If that worked, we're done.  */
8631           if (cp_parser_parse_definitely (parser))
8632             return;
8633         }
8634       /* Look for an expression-statement instead.  */
8635       statement = cp_parser_expression_statement (parser, in_statement_expr);
8636     }
8637
8638   /* Set the line number for the statement.  */
8639   if (statement && STATEMENT_CODE_P (TREE_CODE (statement)))
8640     SET_EXPR_LOCATION (statement, statement_location);
8641 }
8642
8643 /* Parse the label for a labeled-statement, i.e.
8644
8645    identifier :
8646    case constant-expression :
8647    default :
8648
8649    GNU Extension:
8650    case constant-expression ... constant-expression : statement
8651
8652    When a label is parsed without errors, the label is added to the
8653    parse tree by the finish_* functions, so this function doesn't
8654    have to return the label.  */
8655
8656 static void
8657 cp_parser_label_for_labeled_statement (cp_parser* parser)
8658 {
8659   cp_token *token;
8660   tree label = NULL_TREE;
8661   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
8662
8663   /* The next token should be an identifier.  */
8664   token = cp_lexer_peek_token (parser->lexer);
8665   if (token->type != CPP_NAME
8666       && token->type != CPP_KEYWORD)
8667     {
8668       cp_parser_error (parser, "expected labeled-statement");
8669       return;
8670     }
8671
8672   parser->colon_corrects_to_scope_p = false;
8673   switch (token->keyword)
8674     {
8675     case RID_CASE:
8676       {
8677         tree expr, expr_hi;
8678         cp_token *ellipsis;
8679
8680         /* Consume the `case' token.  */
8681         cp_lexer_consume_token (parser->lexer);
8682         /* Parse the constant-expression.  */
8683         expr = cp_parser_constant_expression (parser,
8684                                               /*allow_non_constant_p=*/false,
8685                                               NULL);
8686
8687         ellipsis = cp_lexer_peek_token (parser->lexer);
8688         if (ellipsis->type == CPP_ELLIPSIS)
8689           {
8690             /* Consume the `...' token.  */
8691             cp_lexer_consume_token (parser->lexer);
8692             expr_hi =
8693               cp_parser_constant_expression (parser,
8694                                              /*allow_non_constant_p=*/false,
8695                                              NULL);
8696             /* We don't need to emit warnings here, as the common code
8697                will do this for us.  */
8698           }
8699         else
8700           expr_hi = NULL_TREE;
8701
8702         if (parser->in_switch_statement_p)
8703           finish_case_label (token->location, expr, expr_hi);
8704         else
8705           error_at (token->location,
8706                     "case label %qE not within a switch statement",
8707                     expr);
8708       }
8709       break;
8710
8711     case RID_DEFAULT:
8712       /* Consume the `default' token.  */
8713       cp_lexer_consume_token (parser->lexer);
8714
8715       if (parser->in_switch_statement_p)
8716         finish_case_label (token->location, NULL_TREE, NULL_TREE);
8717       else
8718         error_at (token->location, "case label not within a switch statement");
8719       break;
8720
8721     default:
8722       /* Anything else must be an ordinary label.  */
8723       label = finish_label_stmt (cp_parser_identifier (parser));
8724       break;
8725     }
8726
8727   /* Require the `:' token.  */
8728   cp_parser_require (parser, CPP_COLON, RT_COLON);
8729
8730   /* An ordinary label may optionally be followed by attributes.
8731      However, this is only permitted if the attributes are then
8732      followed by a semicolon.  This is because, for backward
8733      compatibility, when parsing
8734        lab: __attribute__ ((unused)) int i;
8735      we want the attribute to attach to "i", not "lab".  */
8736   if (label != NULL_TREE
8737       && cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
8738     {
8739       tree attrs;
8740
8741       cp_parser_parse_tentatively (parser);
8742       attrs = cp_parser_attributes_opt (parser);
8743       if (attrs == NULL_TREE
8744           || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8745         cp_parser_abort_tentative_parse (parser);
8746       else if (!cp_parser_parse_definitely (parser))
8747         ;
8748       else
8749         cplus_decl_attributes (&label, attrs, 0);
8750     }
8751
8752   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
8753 }
8754
8755 /* Parse an expression-statement.
8756
8757    expression-statement:
8758      expression [opt] ;
8759
8760    Returns the new EXPR_STMT -- or NULL_TREE if the expression
8761    statement consists of nothing more than an `;'. IN_STATEMENT_EXPR_P
8762    indicates whether this expression-statement is part of an
8763    expression statement.  */
8764
8765 static tree
8766 cp_parser_expression_statement (cp_parser* parser, tree in_statement_expr)
8767 {
8768   tree statement = NULL_TREE;
8769   cp_token *token = cp_lexer_peek_token (parser->lexer);
8770
8771   /* If the next token is a ';', then there is no expression
8772      statement.  */
8773   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
8774     statement = cp_parser_expression (parser, /*cast_p=*/false, NULL);
8775
8776   /* Give a helpful message for "A<T>::type t;" and the like.  */
8777   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
8778       && !cp_parser_uncommitted_to_tentative_parse_p (parser))
8779     {
8780       if (TREE_CODE (statement) == SCOPE_REF)
8781         error_at (token->location, "need %<typename%> before %qE because "
8782                   "%qT is a dependent scope",
8783                   statement, TREE_OPERAND (statement, 0));
8784       else if (is_overloaded_fn (statement)
8785                && DECL_CONSTRUCTOR_P (get_first_fn (statement)))
8786         {
8787           /* A::A a; */
8788           tree fn = get_first_fn (statement);
8789           error_at (token->location,
8790                     "%<%T::%D%> names the constructor, not the type",
8791                     DECL_CONTEXT (fn), DECL_NAME (fn));
8792         }
8793     }
8794
8795   /* Consume the final `;'.  */
8796   cp_parser_consume_semicolon_at_end_of_statement (parser);
8797
8798   if (in_statement_expr
8799       && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
8800     /* This is the final expression statement of a statement
8801        expression.  */
8802     statement = finish_stmt_expr_expr (statement, in_statement_expr);
8803   else if (statement)
8804     statement = finish_expr_stmt (statement);
8805   else
8806     finish_stmt ();
8807
8808   return statement;
8809 }
8810
8811 /* Parse a compound-statement.
8812
8813    compound-statement:
8814      { statement-seq [opt] }
8815
8816    GNU extension:
8817
8818    compound-statement:
8819      { label-declaration-seq [opt] statement-seq [opt] }
8820
8821    label-declaration-seq:
8822      label-declaration
8823      label-declaration-seq label-declaration
8824
8825    Returns a tree representing the statement.  */
8826
8827 static tree
8828 cp_parser_compound_statement (cp_parser *parser, tree in_statement_expr,
8829                               bool in_try, bool function_body)
8830 {
8831   tree compound_stmt;
8832
8833   /* Consume the `{'.  */
8834   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
8835     return error_mark_node;
8836   if (DECL_DECLARED_CONSTEXPR_P (current_function_decl)
8837       && !function_body)
8838     pedwarn (input_location, OPT_pedantic,
8839              "compound-statement in constexpr function");
8840   /* Begin the compound-statement.  */
8841   compound_stmt = begin_compound_stmt (in_try ? BCS_TRY_BLOCK : 0);
8842   /* If the next keyword is `__label__' we have a label declaration.  */
8843   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
8844     cp_parser_label_declaration (parser);
8845   /* Parse an (optional) statement-seq.  */
8846   cp_parser_statement_seq_opt (parser, in_statement_expr);
8847   /* Finish the compound-statement.  */
8848   finish_compound_stmt (compound_stmt);
8849   /* Consume the `}'.  */
8850   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
8851
8852   return compound_stmt;
8853 }
8854
8855 /* Parse an (optional) statement-seq.
8856
8857    statement-seq:
8858      statement
8859      statement-seq [opt] statement  */
8860
8861 static void
8862 cp_parser_statement_seq_opt (cp_parser* parser, tree in_statement_expr)
8863 {
8864   /* Scan statements until there aren't any more.  */
8865   while (true)
8866     {
8867       cp_token *token = cp_lexer_peek_token (parser->lexer);
8868
8869       /* If we are looking at a `}', then we have run out of
8870          statements; the same is true if we have reached the end
8871          of file, or have stumbled upon a stray '@end'.  */
8872       if (token->type == CPP_CLOSE_BRACE
8873           || token->type == CPP_EOF
8874           || token->type == CPP_PRAGMA_EOL
8875           || (token->type == CPP_KEYWORD && token->keyword == RID_AT_END))
8876         break;
8877       
8878       /* If we are in a compound statement and find 'else' then
8879          something went wrong.  */
8880       else if (token->type == CPP_KEYWORD && token->keyword == RID_ELSE)
8881         {
8882           if (parser->in_statement & IN_IF_STMT) 
8883             break;
8884           else
8885             {
8886               token = cp_lexer_consume_token (parser->lexer);
8887               error_at (token->location, "%<else%> without a previous %<if%>");
8888             }
8889         }
8890
8891       /* Parse the statement.  */
8892       cp_parser_statement (parser, in_statement_expr, true, NULL);
8893     }
8894 }
8895
8896 /* Parse a selection-statement.
8897
8898    selection-statement:
8899      if ( condition ) statement
8900      if ( condition ) statement else statement
8901      switch ( condition ) statement
8902
8903    Returns the new IF_STMT or SWITCH_STMT.
8904
8905    If IF_P is not NULL, *IF_P is set to indicate whether the statement
8906    is a (possibly labeled) if statement which is not enclosed in
8907    braces and has an else clause.  This is used to implement
8908    -Wparentheses.  */
8909
8910 static tree
8911 cp_parser_selection_statement (cp_parser* parser, bool *if_p)
8912 {
8913   cp_token *token;
8914   enum rid keyword;
8915
8916   if (if_p != NULL)
8917     *if_p = false;
8918
8919   /* Peek at the next token.  */
8920   token = cp_parser_require (parser, CPP_KEYWORD, RT_SELECT);
8921
8922   /* See what kind of keyword it is.  */
8923   keyword = token->keyword;
8924   switch (keyword)
8925     {
8926     case RID_IF:
8927     case RID_SWITCH:
8928       {
8929         tree statement;
8930         tree condition;
8931
8932         /* Look for the `('.  */
8933         if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
8934           {
8935             cp_parser_skip_to_end_of_statement (parser);
8936             return error_mark_node;
8937           }
8938
8939         /* Begin the selection-statement.  */
8940         if (keyword == RID_IF)
8941           statement = begin_if_stmt ();
8942         else
8943           statement = begin_switch_stmt ();
8944
8945         /* Parse the condition.  */
8946         condition = cp_parser_condition (parser);
8947         /* Look for the `)'.  */
8948         if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
8949           cp_parser_skip_to_closing_parenthesis (parser, true, false,
8950                                                  /*consume_paren=*/true);
8951
8952         if (keyword == RID_IF)
8953           {
8954             bool nested_if;
8955             unsigned char in_statement;
8956
8957             /* Add the condition.  */
8958             finish_if_stmt_cond (condition, statement);
8959
8960             /* Parse the then-clause.  */
8961             in_statement = parser->in_statement;
8962             parser->in_statement |= IN_IF_STMT;
8963             if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8964               {
8965                 location_t loc = cp_lexer_peek_token (parser->lexer)->location;
8966                 add_stmt (build_empty_stmt (loc));
8967                 cp_lexer_consume_token (parser->lexer);
8968                 if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_ELSE))
8969                   warning_at (loc, OPT_Wempty_body, "suggest braces around "
8970                               "empty body in an %<if%> statement");
8971                 nested_if = false;
8972               }
8973             else
8974               cp_parser_implicitly_scoped_statement (parser, &nested_if);
8975             parser->in_statement = in_statement;
8976
8977             finish_then_clause (statement);
8978
8979             /* If the next token is `else', parse the else-clause.  */
8980             if (cp_lexer_next_token_is_keyword (parser->lexer,
8981                                                 RID_ELSE))
8982               {
8983                 /* Consume the `else' keyword.  */
8984                 cp_lexer_consume_token (parser->lexer);
8985                 begin_else_clause (statement);
8986                 /* Parse the else-clause.  */
8987                 if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
8988                   {
8989                     location_t loc;
8990                     loc = cp_lexer_peek_token (parser->lexer)->location;
8991                     warning_at (loc,
8992                                 OPT_Wempty_body, "suggest braces around "
8993                                 "empty body in an %<else%> statement");
8994                     add_stmt (build_empty_stmt (loc));
8995                     cp_lexer_consume_token (parser->lexer);
8996                   }
8997                 else
8998                   cp_parser_implicitly_scoped_statement (parser, NULL);
8999
9000                 finish_else_clause (statement);
9001
9002                 /* If we are currently parsing a then-clause, then
9003                    IF_P will not be NULL.  We set it to true to
9004                    indicate that this if statement has an else clause.
9005                    This may trigger the Wparentheses warning below
9006                    when we get back up to the parent if statement.  */
9007                 if (if_p != NULL)
9008                   *if_p = true;
9009               }
9010             else
9011               {
9012                 /* This if statement does not have an else clause.  If
9013                    NESTED_IF is true, then the then-clause is an if
9014                    statement which does have an else clause.  We warn
9015                    about the potential ambiguity.  */
9016                 if (nested_if)
9017                   warning_at (EXPR_LOCATION (statement), OPT_Wparentheses,
9018                               "suggest explicit braces to avoid ambiguous"
9019                               " %<else%>");
9020               }
9021
9022             /* Now we're all done with the if-statement.  */
9023             finish_if_stmt (statement);
9024           }
9025         else
9026           {
9027             bool in_switch_statement_p;
9028             unsigned char in_statement;
9029
9030             /* Add the condition.  */
9031             finish_switch_cond (condition, statement);
9032
9033             /* Parse the body of the switch-statement.  */
9034             in_switch_statement_p = parser->in_switch_statement_p;
9035             in_statement = parser->in_statement;
9036             parser->in_switch_statement_p = true;
9037             parser->in_statement |= IN_SWITCH_STMT;
9038             cp_parser_implicitly_scoped_statement (parser, NULL);
9039             parser->in_switch_statement_p = in_switch_statement_p;
9040             parser->in_statement = in_statement;
9041
9042             /* Now we're all done with the switch-statement.  */
9043             finish_switch_stmt (statement);
9044           }
9045
9046         return statement;
9047       }
9048       break;
9049
9050     default:
9051       cp_parser_error (parser, "expected selection-statement");
9052       return error_mark_node;
9053     }
9054 }
9055
9056 /* Parse a condition.
9057
9058    condition:
9059      expression
9060      type-specifier-seq declarator = initializer-clause
9061      type-specifier-seq declarator braced-init-list
9062
9063    GNU Extension:
9064
9065    condition:
9066      type-specifier-seq declarator asm-specification [opt]
9067        attributes [opt] = assignment-expression
9068
9069    Returns the expression that should be tested.  */
9070
9071 static tree
9072 cp_parser_condition (cp_parser* parser)
9073 {
9074   cp_decl_specifier_seq type_specifiers;
9075   const char *saved_message;
9076   int declares_class_or_enum;
9077
9078   /* Try the declaration first.  */
9079   cp_parser_parse_tentatively (parser);
9080   /* New types are not allowed in the type-specifier-seq for a
9081      condition.  */
9082   saved_message = parser->type_definition_forbidden_message;
9083   parser->type_definition_forbidden_message
9084     = G_("types may not be defined in conditions");
9085   /* Parse the type-specifier-seq.  */
9086   cp_parser_decl_specifier_seq (parser,
9087                                 CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR,
9088                                 &type_specifiers,
9089                                 &declares_class_or_enum);
9090   /* Restore the saved message.  */
9091   parser->type_definition_forbidden_message = saved_message;
9092   /* If all is well, we might be looking at a declaration.  */
9093   if (!cp_parser_error_occurred (parser))
9094     {
9095       tree decl;
9096       tree asm_specification;
9097       tree attributes;
9098       cp_declarator *declarator;
9099       tree initializer = NULL_TREE;
9100
9101       /* Parse the declarator.  */
9102       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
9103                                          /*ctor_dtor_or_conv_p=*/NULL,
9104                                          /*parenthesized_p=*/NULL,
9105                                          /*member_p=*/false);
9106       /* Parse the attributes.  */
9107       attributes = cp_parser_attributes_opt (parser);
9108       /* Parse the asm-specification.  */
9109       asm_specification = cp_parser_asm_specification_opt (parser);
9110       /* If the next token is not an `=' or '{', then we might still be
9111          looking at an expression.  For example:
9112
9113            if (A(a).x)
9114
9115          looks like a decl-specifier-seq and a declarator -- but then
9116          there is no `=', so this is an expression.  */
9117       if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
9118           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9119         cp_parser_simulate_error (parser);
9120         
9121       /* If we did see an `=' or '{', then we are looking at a declaration
9122          for sure.  */
9123       if (cp_parser_parse_definitely (parser))
9124         {
9125           tree pushed_scope;
9126           bool non_constant_p;
9127           bool flags = LOOKUP_ONLYCONVERTING;
9128
9129           /* Create the declaration.  */
9130           decl = start_decl (declarator, &type_specifiers,
9131                              /*initialized_p=*/true,
9132                              attributes, /*prefix_attributes=*/NULL_TREE,
9133                              &pushed_scope);
9134
9135           /* Parse the initializer.  */
9136           if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9137             {
9138               initializer = cp_parser_braced_list (parser, &non_constant_p);
9139               CONSTRUCTOR_IS_DIRECT_INIT (initializer) = 1;
9140               flags = 0;
9141             }
9142           else
9143             {
9144               /* Consume the `='.  */
9145               cp_parser_require (parser, CPP_EQ, RT_EQ);
9146               initializer = cp_parser_initializer_clause (parser, &non_constant_p);
9147             }
9148           if (BRACE_ENCLOSED_INITIALIZER_P (initializer))
9149             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9150
9151           /* Process the initializer.  */
9152           cp_finish_decl (decl,
9153                           initializer, !non_constant_p,
9154                           asm_specification,
9155                           flags);
9156
9157           if (pushed_scope)
9158             pop_scope (pushed_scope);
9159
9160           return convert_from_reference (decl);
9161         }
9162     }
9163   /* If we didn't even get past the declarator successfully, we are
9164      definitely not looking at a declaration.  */
9165   else
9166     cp_parser_abort_tentative_parse (parser);
9167
9168   /* Otherwise, we are looking at an expression.  */
9169   return cp_parser_expression (parser, /*cast_p=*/false, NULL);
9170 }
9171
9172 /* Parses a for-statement or range-for-statement until the closing ')',
9173    not included. */
9174
9175 static tree
9176 cp_parser_for (cp_parser *parser)
9177 {
9178   tree init, scope, decl;
9179   bool is_range_for;
9180
9181   /* Begin the for-statement.  */
9182   scope = begin_for_scope (&init);
9183
9184   /* Parse the initialization.  */
9185   is_range_for = cp_parser_for_init_statement (parser, &decl);
9186
9187   if (is_range_for)
9188     return cp_parser_range_for (parser, scope, init, decl);
9189   else
9190     return cp_parser_c_for (parser, scope, init);
9191 }
9192
9193 static tree
9194 cp_parser_c_for (cp_parser *parser, tree scope, tree init)
9195 {
9196   /* Normal for loop */
9197   tree condition = NULL_TREE;
9198   tree expression = NULL_TREE;
9199   tree stmt;
9200
9201   stmt = begin_for_stmt (scope, init);
9202   /* The for-init-statement has already been parsed in
9203      cp_parser_for_init_statement, so no work is needed here.  */
9204   finish_for_init_stmt (stmt);
9205
9206   /* If there's a condition, process it.  */
9207   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9208     condition = cp_parser_condition (parser);
9209   finish_for_cond (condition, stmt);
9210   /* Look for the `;'.  */
9211   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9212
9213   /* If there's an expression, process it.  */
9214   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
9215     expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9216   finish_for_expr (expression, stmt);
9217
9218   return stmt;
9219 }
9220
9221 /* Tries to parse a range-based for-statement:
9222
9223   range-based-for:
9224     decl-specifier-seq declarator : expression
9225
9226   The decl-specifier-seq declarator and the `:' are already parsed by
9227   cp_parser_for_init_statement. If processing_template_decl it returns a
9228   newly created RANGE_FOR_STMT; if not, it is converted to a
9229   regular FOR_STMT.  */
9230
9231 static tree
9232 cp_parser_range_for (cp_parser *parser, tree scope, tree init, tree range_decl)
9233 {
9234   tree stmt, range_expr;
9235
9236   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9237     {
9238       bool expr_non_constant_p;
9239       range_expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9240     }
9241   else
9242     range_expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9243
9244   /* If in template, STMT is converted to a normal for-statement
9245      at instantiation. If not, it is done just ahead. */
9246   if (processing_template_decl)
9247     {
9248       stmt = begin_range_for_stmt (scope, init);
9249       finish_range_for_decl (stmt, range_decl, range_expr);
9250       if (!type_dependent_expression_p (range_expr)
9251           /* do_auto_deduction doesn't mess with template init-lists.  */
9252           && !BRACE_ENCLOSED_INITIALIZER_P (range_expr))
9253         do_range_for_auto_deduction (range_decl, range_expr);
9254     }
9255   else
9256     {
9257       stmt = begin_for_stmt (scope, init);
9258       stmt = cp_convert_range_for (stmt, range_decl, range_expr);
9259     }
9260   return stmt;
9261 }
9262
9263 /* Subroutine of cp_convert_range_for: given the initializer expression,
9264    builds up the range temporary.  */
9265
9266 static tree
9267 build_range_temp (tree range_expr)
9268 {
9269   tree range_type, range_temp;
9270
9271   /* Find out the type deduced by the declaration
9272      `auto &&__range = range_expr'.  */
9273   range_type = cp_build_reference_type (make_auto (), true);
9274   range_type = do_auto_deduction (range_type, range_expr,
9275                                   type_uses_auto (range_type));
9276
9277   /* Create the __range variable.  */
9278   range_temp = build_decl (input_location, VAR_DECL,
9279                            get_identifier ("__for_range"), range_type);
9280   TREE_USED (range_temp) = 1;
9281   DECL_ARTIFICIAL (range_temp) = 1;
9282
9283   return range_temp;
9284 }
9285
9286 /* Used by cp_parser_range_for in template context: we aren't going to
9287    do a full conversion yet, but we still need to resolve auto in the
9288    type of the for-range-declaration if present.  This is basically
9289    a shortcut version of cp_convert_range_for.  */
9290
9291 static void
9292 do_range_for_auto_deduction (tree decl, tree range_expr)
9293 {
9294   tree auto_node = type_uses_auto (TREE_TYPE (decl));
9295   if (auto_node)
9296     {
9297       tree begin_dummy, end_dummy, range_temp, iter_type, iter_decl;
9298       range_temp = convert_from_reference (build_range_temp (range_expr));
9299       iter_type = (cp_parser_perform_range_for_lookup
9300                    (range_temp, &begin_dummy, &end_dummy));
9301       iter_decl = build_decl (input_location, VAR_DECL, NULL_TREE, iter_type);
9302       iter_decl = build_x_indirect_ref (iter_decl, RO_NULL,
9303                                         tf_warning_or_error);
9304       TREE_TYPE (decl) = do_auto_deduction (TREE_TYPE (decl),
9305                                             iter_decl, auto_node);
9306     }
9307 }
9308
9309 /* Converts a range-based for-statement into a normal
9310    for-statement, as per the definition.
9311
9312       for (RANGE_DECL : RANGE_EXPR)
9313         BLOCK
9314
9315    should be equivalent to:
9316
9317       {
9318         auto &&__range = RANGE_EXPR;
9319         for (auto __begin = BEGIN_EXPR, end = END_EXPR;
9320               __begin != __end;
9321               ++__begin)
9322           {
9323               RANGE_DECL = *__begin;
9324               BLOCK
9325           }
9326       }
9327
9328    If RANGE_EXPR is an array:
9329         BEGIN_EXPR = __range
9330         END_EXPR = __range + ARRAY_SIZE(__range)
9331    Else if RANGE_EXPR has a member 'begin' or 'end':
9332         BEGIN_EXPR = __range.begin()
9333         END_EXPR = __range.end()
9334    Else:
9335         BEGIN_EXPR = begin(__range)
9336         END_EXPR = end(__range);
9337
9338    If __range has a member 'begin' but not 'end', or vice versa, we must
9339    still use the second alternative (it will surely fail, however).
9340    When calling begin()/end() in the third alternative we must use
9341    argument dependent lookup, but always considering 'std' as an associated
9342    namespace.  */
9343
9344 tree
9345 cp_convert_range_for (tree statement, tree range_decl, tree range_expr)
9346 {
9347   tree begin, end;
9348   tree iter_type, begin_expr, end_expr;
9349   tree condition, expression;
9350
9351   if (range_decl == error_mark_node || range_expr == error_mark_node)
9352     /* If an error happened previously do nothing or else a lot of
9353        unhelpful errors would be issued.  */
9354     begin_expr = end_expr = iter_type = error_mark_node;
9355   else
9356     {
9357       tree range_temp = build_range_temp (range_expr);
9358       pushdecl (range_temp);
9359       cp_finish_decl (range_temp, range_expr,
9360                       /*is_constant_init*/false, NULL_TREE,
9361                       LOOKUP_ONLYCONVERTING);
9362
9363       range_temp = convert_from_reference (range_temp);
9364       iter_type = cp_parser_perform_range_for_lookup (range_temp,
9365                                                       &begin_expr, &end_expr);
9366     }
9367
9368   /* The new for initialization statement.  */
9369   begin = build_decl (input_location, VAR_DECL,
9370                       get_identifier ("__for_begin"), iter_type);
9371   TREE_USED (begin) = 1;
9372   DECL_ARTIFICIAL (begin) = 1;
9373   pushdecl (begin);
9374   cp_finish_decl (begin, begin_expr,
9375                   /*is_constant_init*/false, NULL_TREE,
9376                   LOOKUP_ONLYCONVERTING);
9377
9378   end = build_decl (input_location, VAR_DECL,
9379                     get_identifier ("__for_end"), iter_type);
9380   TREE_USED (end) = 1;
9381   DECL_ARTIFICIAL (end) = 1;
9382   pushdecl (end);
9383   cp_finish_decl (end, end_expr,
9384                   /*is_constant_init*/false, NULL_TREE,
9385                   LOOKUP_ONLYCONVERTING);
9386
9387   finish_for_init_stmt (statement);
9388
9389   /* The new for condition.  */
9390   condition = build_x_binary_op (NE_EXPR,
9391                                  begin, ERROR_MARK,
9392                                  end, ERROR_MARK,
9393                                  NULL, tf_warning_or_error);
9394   finish_for_cond (condition, statement);
9395
9396   /* The new increment expression.  */
9397   expression = finish_unary_op_expr (PREINCREMENT_EXPR, begin);
9398   finish_for_expr (expression, statement);
9399
9400   /* The declaration is initialized with *__begin inside the loop body.  */
9401   cp_finish_decl (range_decl,
9402                   build_x_indirect_ref (begin, RO_NULL, tf_warning_or_error),
9403                   /*is_constant_init*/false, NULL_TREE,
9404                   LOOKUP_ONLYCONVERTING);
9405
9406   return statement;
9407 }
9408
9409 /* Solves BEGIN_EXPR and END_EXPR as described in cp_convert_range_for.
9410    We need to solve both at the same time because the method used
9411    depends on the existence of members begin or end.
9412    Returns the type deduced for the iterator expression.  */
9413
9414 static tree
9415 cp_parser_perform_range_for_lookup (tree range, tree *begin, tree *end)
9416 {
9417   if (error_operand_p (range))
9418     {
9419       *begin = *end = error_mark_node;
9420       return error_mark_node;
9421     }
9422
9423   if (!COMPLETE_TYPE_P (complete_type (TREE_TYPE (range))))
9424     {
9425       error ("range-based %<for%> expression of type %qT "
9426              "has incomplete type", TREE_TYPE (range));
9427       *begin = *end = error_mark_node;
9428       return error_mark_node;
9429     }
9430   if (TREE_CODE (TREE_TYPE (range)) == ARRAY_TYPE)
9431     {
9432       /* If RANGE is an array, we will use pointer arithmetic.  */
9433       *begin = range;
9434       *end = build_binary_op (input_location, PLUS_EXPR,
9435                               range,
9436                               array_type_nelts_top (TREE_TYPE (range)),
9437                               0);
9438       return build_pointer_type (TREE_TYPE (TREE_TYPE (range)));
9439     }
9440   else
9441     {
9442       /* If it is not an array, we must do a bit of magic.  */
9443       tree id_begin, id_end;
9444       tree member_begin, member_end;
9445
9446       *begin = *end = error_mark_node;
9447
9448       id_begin = get_identifier ("begin");
9449       id_end = get_identifier ("end");
9450       member_begin = lookup_member (TREE_TYPE (range), id_begin,
9451                                     /*protect=*/2, /*want_type=*/false);
9452       member_end = lookup_member (TREE_TYPE (range), id_end,
9453                                   /*protect=*/2, /*want_type=*/false);
9454
9455       if (member_begin != NULL_TREE || member_end != NULL_TREE)
9456         {
9457           /* Use the member functions.  */
9458           if (member_begin != NULL_TREE)
9459             *begin = cp_parser_range_for_member_function (range, id_begin);
9460           else
9461             error ("range-based %<for%> expression of type %qT has an "
9462                    "%<end%> member but not a %<begin%>", TREE_TYPE (range));
9463
9464           if (member_end != NULL_TREE)
9465             *end = cp_parser_range_for_member_function (range, id_end);
9466           else
9467             error ("range-based %<for%> expression of type %qT has a "
9468                    "%<begin%> member but not an %<end%>", TREE_TYPE (range));
9469         }
9470       else
9471         {
9472           /* Use global functions with ADL.  */
9473           VEC(tree,gc) *vec;
9474           vec = make_tree_vector ();
9475
9476           VEC_safe_push (tree, gc, vec, range);
9477
9478           member_begin = perform_koenig_lookup (id_begin, vec,
9479                                                 /*include_std=*/true,
9480                                                 tf_warning_or_error);
9481           *begin = finish_call_expr (member_begin, &vec, false, true,
9482                                      tf_warning_or_error);
9483           member_end = perform_koenig_lookup (id_end, vec,
9484                                               /*include_std=*/true,
9485                                               tf_warning_or_error);
9486           *end = finish_call_expr (member_end, &vec, false, true,
9487                                    tf_warning_or_error);
9488
9489           release_tree_vector (vec);
9490         }
9491
9492       /* Last common checks.  */
9493       if (*begin == error_mark_node || *end == error_mark_node)
9494         {
9495           /* If one of the expressions is an error do no more checks.  */
9496           *begin = *end = error_mark_node;
9497           return error_mark_node;
9498         }
9499       else
9500         {
9501           tree iter_type = cv_unqualified (TREE_TYPE (*begin));
9502           /* The unqualified type of the __begin and __end temporaries should
9503              be the same, as required by the multiple auto declaration.  */
9504           if (!same_type_p (iter_type, cv_unqualified (TREE_TYPE (*end))))
9505             error ("inconsistent begin/end types in range-based %<for%> "
9506                    "statement: %qT and %qT",
9507                    TREE_TYPE (*begin), TREE_TYPE (*end));
9508           return iter_type;
9509         }
9510     }
9511 }
9512
9513 /* Helper function for cp_parser_perform_range_for_lookup.
9514    Builds a tree for RANGE.IDENTIFIER().  */
9515
9516 static tree
9517 cp_parser_range_for_member_function (tree range, tree identifier)
9518 {
9519   tree member, res;
9520   VEC(tree,gc) *vec;
9521
9522   member = finish_class_member_access_expr (range, identifier,
9523                                             false, tf_warning_or_error);
9524   if (member == error_mark_node)
9525     return error_mark_node;
9526
9527   vec = make_tree_vector ();
9528   res = finish_call_expr (member, &vec,
9529                           /*disallow_virtual=*/false,
9530                           /*koenig_p=*/false,
9531                           tf_warning_or_error);
9532   release_tree_vector (vec);
9533   return res;
9534 }
9535
9536 /* Parse an iteration-statement.
9537
9538    iteration-statement:
9539      while ( condition ) statement
9540      do statement while ( expression ) ;
9541      for ( for-init-statement condition [opt] ; expression [opt] )
9542        statement
9543
9544    Returns the new WHILE_STMT, DO_STMT, FOR_STMT or RANGE_FOR_STMT.  */
9545
9546 static tree
9547 cp_parser_iteration_statement (cp_parser* parser)
9548 {
9549   cp_token *token;
9550   enum rid keyword;
9551   tree statement;
9552   unsigned char in_statement;
9553
9554   /* Peek at the next token.  */
9555   token = cp_parser_require (parser, CPP_KEYWORD, RT_INTERATION);
9556   if (!token)
9557     return error_mark_node;
9558
9559   /* Remember whether or not we are already within an iteration
9560      statement.  */
9561   in_statement = parser->in_statement;
9562
9563   /* See what kind of keyword it is.  */
9564   keyword = token->keyword;
9565   switch (keyword)
9566     {
9567     case RID_WHILE:
9568       {
9569         tree condition;
9570
9571         /* Begin the while-statement.  */
9572         statement = begin_while_stmt ();
9573         /* Look for the `('.  */
9574         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9575         /* Parse the condition.  */
9576         condition = cp_parser_condition (parser);
9577         finish_while_stmt_cond (condition, statement);
9578         /* Look for the `)'.  */
9579         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9580         /* Parse the dependent statement.  */
9581         parser->in_statement = IN_ITERATION_STMT;
9582         cp_parser_already_scoped_statement (parser);
9583         parser->in_statement = in_statement;
9584         /* We're done with the while-statement.  */
9585         finish_while_stmt (statement);
9586       }
9587       break;
9588
9589     case RID_DO:
9590       {
9591         tree expression;
9592
9593         /* Begin the do-statement.  */
9594         statement = begin_do_stmt ();
9595         /* Parse the body of the do-statement.  */
9596         parser->in_statement = IN_ITERATION_STMT;
9597         cp_parser_implicitly_scoped_statement (parser, NULL);
9598         parser->in_statement = in_statement;
9599         finish_do_body (statement);
9600         /* Look for the `while' keyword.  */
9601         cp_parser_require_keyword (parser, RID_WHILE, RT_WHILE);
9602         /* Look for the `('.  */
9603         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9604         /* Parse the expression.  */
9605         expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9606         /* We're done with the do-statement.  */
9607         finish_do_stmt (expression, statement);
9608         /* Look for the `)'.  */
9609         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9610         /* Look for the `;'.  */
9611         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9612       }
9613       break;
9614
9615     case RID_FOR:
9616       {
9617         /* Look for the `('.  */
9618         cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
9619
9620         statement = cp_parser_for (parser);
9621
9622         /* Look for the `)'.  */
9623         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
9624
9625         /* Parse the body of the for-statement.  */
9626         parser->in_statement = IN_ITERATION_STMT;
9627         cp_parser_already_scoped_statement (parser);
9628         parser->in_statement = in_statement;
9629
9630         /* We're done with the for-statement.  */
9631         finish_for_stmt (statement);
9632       }
9633       break;
9634
9635     default:
9636       cp_parser_error (parser, "expected iteration-statement");
9637       statement = error_mark_node;
9638       break;
9639     }
9640
9641   return statement;
9642 }
9643
9644 /* Parse a for-init-statement or the declarator of a range-based-for.
9645    Returns true if a range-based-for declaration is seen.
9646
9647    for-init-statement:
9648      expression-statement
9649      simple-declaration  */
9650
9651 static bool
9652 cp_parser_for_init_statement (cp_parser* parser, tree *decl)
9653 {
9654   /* If the next token is a `;', then we have an empty
9655      expression-statement.  Grammatically, this is also a
9656      simple-declaration, but an invalid one, because it does not
9657      declare anything.  Therefore, if we did not handle this case
9658      specially, we would issue an error message about an invalid
9659      declaration.  */
9660   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9661     {
9662       bool is_range_for = false;
9663       bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
9664
9665       parser->colon_corrects_to_scope_p = false;
9666
9667       /* We're going to speculatively look for a declaration, falling back
9668          to an expression, if necessary.  */
9669       cp_parser_parse_tentatively (parser);
9670       /* Parse the declaration.  */
9671       cp_parser_simple_declaration (parser,
9672                                     /*function_definition_allowed_p=*/false,
9673                                     decl);
9674       parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
9675       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
9676         {
9677           /* It is a range-for, consume the ':' */
9678           cp_lexer_consume_token (parser->lexer);
9679           is_range_for = true;
9680           if (cxx_dialect < cxx0x)
9681             {
9682               error_at (cp_lexer_peek_token (parser->lexer)->location,
9683                         "range-based %<for%> loops are not allowed "
9684                         "in C++98 mode");
9685               *decl = error_mark_node;
9686             }
9687         }
9688       else
9689           /* The ';' is not consumed yet because we told
9690              cp_parser_simple_declaration not to.  */
9691           cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9692
9693       if (cp_parser_parse_definitely (parser))
9694         return is_range_for;
9695       /* If the tentative parse failed, then we shall need to look for an
9696          expression-statement.  */
9697     }
9698   /* If we are here, it is an expression-statement.  */
9699   cp_parser_expression_statement (parser, NULL_TREE);
9700   return false;
9701 }
9702
9703 /* Parse a jump-statement.
9704
9705    jump-statement:
9706      break ;
9707      continue ;
9708      return expression [opt] ;
9709      return braced-init-list ;
9710      goto identifier ;
9711
9712    GNU extension:
9713
9714    jump-statement:
9715      goto * expression ;
9716
9717    Returns the new BREAK_STMT, CONTINUE_STMT, RETURN_EXPR, or GOTO_EXPR.  */
9718
9719 static tree
9720 cp_parser_jump_statement (cp_parser* parser)
9721 {
9722   tree statement = error_mark_node;
9723   cp_token *token;
9724   enum rid keyword;
9725   unsigned char in_statement;
9726
9727   /* Peek at the next token.  */
9728   token = cp_parser_require (parser, CPP_KEYWORD, RT_JUMP);
9729   if (!token)
9730     return error_mark_node;
9731
9732   /* See what kind of keyword it is.  */
9733   keyword = token->keyword;
9734   switch (keyword)
9735     {
9736     case RID_BREAK:
9737       in_statement = parser->in_statement & ~IN_IF_STMT;      
9738       switch (in_statement)
9739         {
9740         case 0:
9741           error_at (token->location, "break statement not within loop or switch");
9742           break;
9743         default:
9744           gcc_assert ((in_statement & IN_SWITCH_STMT)
9745                       || in_statement == IN_ITERATION_STMT);
9746           statement = finish_break_stmt ();
9747           break;
9748         case IN_OMP_BLOCK:
9749           error_at (token->location, "invalid exit from OpenMP structured block");
9750           break;
9751         case IN_OMP_FOR:
9752           error_at (token->location, "break statement used with OpenMP for loop");
9753           break;
9754         }
9755       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9756       break;
9757
9758     case RID_CONTINUE:
9759       switch (parser->in_statement & ~(IN_SWITCH_STMT | IN_IF_STMT))
9760         {
9761         case 0:
9762           error_at (token->location, "continue statement not within a loop");
9763           break;
9764         case IN_ITERATION_STMT:
9765         case IN_OMP_FOR:
9766           statement = finish_continue_stmt ();
9767           break;
9768         case IN_OMP_BLOCK:
9769           error_at (token->location, "invalid exit from OpenMP structured block");
9770           break;
9771         default:
9772           gcc_unreachable ();
9773         }
9774       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9775       break;
9776
9777     case RID_RETURN:
9778       {
9779         tree expr;
9780         bool expr_non_constant_p;
9781
9782         if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9783           {
9784             maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
9785             expr = cp_parser_braced_list (parser, &expr_non_constant_p);
9786           }
9787         else if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
9788           expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
9789         else
9790           /* If the next token is a `;', then there is no
9791              expression.  */
9792           expr = NULL_TREE;
9793         /* Build the return-statement.  */
9794         statement = finish_return_stmt (expr);
9795         /* Look for the final `;'.  */
9796         cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9797       }
9798       break;
9799
9800     case RID_GOTO:
9801       /* Create the goto-statement.  */
9802       if (cp_lexer_next_token_is (parser->lexer, CPP_MULT))
9803         {
9804           /* Issue a warning about this use of a GNU extension.  */
9805           pedwarn (token->location, OPT_pedantic, "ISO C++ forbids computed gotos");
9806           /* Consume the '*' token.  */
9807           cp_lexer_consume_token (parser->lexer);
9808           /* Parse the dependent expression.  */
9809           finish_goto_stmt (cp_parser_expression (parser, /*cast_p=*/false, NULL));
9810         }
9811       else
9812         finish_goto_stmt (cp_parser_identifier (parser));
9813       /* Look for the final `;'.  */
9814       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
9815       break;
9816
9817     default:
9818       cp_parser_error (parser, "expected jump-statement");
9819       break;
9820     }
9821
9822   return statement;
9823 }
9824
9825 /* Parse a declaration-statement.
9826
9827    declaration-statement:
9828      block-declaration  */
9829
9830 static void
9831 cp_parser_declaration_statement (cp_parser* parser)
9832 {
9833   void *p;
9834
9835   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
9836   p = obstack_alloc (&declarator_obstack, 0);
9837
9838  /* Parse the block-declaration.  */
9839   cp_parser_block_declaration (parser, /*statement_p=*/true);
9840
9841   /* Free any declarators allocated.  */
9842   obstack_free (&declarator_obstack, p);
9843
9844   /* Finish off the statement.  */
9845   finish_stmt ();
9846 }
9847
9848 /* Some dependent statements (like `if (cond) statement'), are
9849    implicitly in their own scope.  In other words, if the statement is
9850    a single statement (as opposed to a compound-statement), it is
9851    none-the-less treated as if it were enclosed in braces.  Any
9852    declarations appearing in the dependent statement are out of scope
9853    after control passes that point.  This function parses a statement,
9854    but ensures that is in its own scope, even if it is not a
9855    compound-statement.
9856
9857    If IF_P is not NULL, *IF_P is set to indicate whether the statement
9858    is a (possibly labeled) if statement which is not enclosed in
9859    braces and has an else clause.  This is used to implement
9860    -Wparentheses.
9861
9862    Returns the new statement.  */
9863
9864 static tree
9865 cp_parser_implicitly_scoped_statement (cp_parser* parser, bool *if_p)
9866 {
9867   tree statement;
9868
9869   if (if_p != NULL)
9870     *if_p = false;
9871
9872   /* Mark if () ; with a special NOP_EXPR.  */
9873   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
9874     {
9875       location_t loc = cp_lexer_peek_token (parser->lexer)->location;
9876       cp_lexer_consume_token (parser->lexer);
9877       statement = add_stmt (build_empty_stmt (loc));
9878     }
9879   /* if a compound is opened, we simply parse the statement directly.  */
9880   else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
9881     statement = cp_parser_compound_statement (parser, NULL, false, false);
9882   /* If the token is not a `{', then we must take special action.  */
9883   else
9884     {
9885       /* Create a compound-statement.  */
9886       statement = begin_compound_stmt (0);
9887       /* Parse the dependent-statement.  */
9888       cp_parser_statement (parser, NULL_TREE, false, if_p);
9889       /* Finish the dummy compound-statement.  */
9890       finish_compound_stmt (statement);
9891     }
9892
9893   /* Return the statement.  */
9894   return statement;
9895 }
9896
9897 /* For some dependent statements (like `while (cond) statement'), we
9898    have already created a scope.  Therefore, even if the dependent
9899    statement is a compound-statement, we do not want to create another
9900    scope.  */
9901
9902 static void
9903 cp_parser_already_scoped_statement (cp_parser* parser)
9904 {
9905   /* If the token is a `{', then we must take special action.  */
9906   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
9907     cp_parser_statement (parser, NULL_TREE, false, NULL);
9908   else
9909     {
9910       /* Avoid calling cp_parser_compound_statement, so that we
9911          don't create a new scope.  Do everything else by hand.  */
9912       cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
9913       /* If the next keyword is `__label__' we have a label declaration.  */
9914       while (cp_lexer_next_token_is_keyword (parser->lexer, RID_LABEL))
9915         cp_parser_label_declaration (parser);
9916       /* Parse an (optional) statement-seq.  */
9917       cp_parser_statement_seq_opt (parser, NULL_TREE);
9918       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
9919     }
9920 }
9921
9922 /* Declarations [gram.dcl.dcl] */
9923
9924 /* Parse an optional declaration-sequence.
9925
9926    declaration-seq:
9927      declaration
9928      declaration-seq declaration  */
9929
9930 static void
9931 cp_parser_declaration_seq_opt (cp_parser* parser)
9932 {
9933   while (true)
9934     {
9935       cp_token *token;
9936
9937       token = cp_lexer_peek_token (parser->lexer);
9938
9939       if (token->type == CPP_CLOSE_BRACE
9940           || token->type == CPP_EOF
9941           || token->type == CPP_PRAGMA_EOL)
9942         break;
9943
9944       if (token->type == CPP_SEMICOLON)
9945         {
9946           /* A declaration consisting of a single semicolon is
9947              invalid.  Allow it unless we're being pedantic.  */
9948           cp_lexer_consume_token (parser->lexer);
9949           if (!in_system_header)
9950             pedwarn (input_location, OPT_pedantic, "extra %<;%>");
9951           continue;
9952         }
9953
9954       /* If we're entering or exiting a region that's implicitly
9955          extern "C", modify the lang context appropriately.  */
9956       if (!parser->implicit_extern_c && token->implicit_extern_c)
9957         {
9958           push_lang_context (lang_name_c);
9959           parser->implicit_extern_c = true;
9960         }
9961       else if (parser->implicit_extern_c && !token->implicit_extern_c)
9962         {
9963           pop_lang_context ();
9964           parser->implicit_extern_c = false;
9965         }
9966
9967       if (token->type == CPP_PRAGMA)
9968         {
9969           /* A top-level declaration can consist solely of a #pragma.
9970              A nested declaration cannot, so this is done here and not
9971              in cp_parser_declaration.  (A #pragma at block scope is
9972              handled in cp_parser_statement.)  */
9973           cp_parser_pragma (parser, pragma_external);
9974           continue;
9975         }
9976
9977       /* Parse the declaration itself.  */
9978       cp_parser_declaration (parser);
9979     }
9980 }
9981
9982 /* Parse a declaration.
9983
9984    declaration:
9985      block-declaration
9986      function-definition
9987      template-declaration
9988      explicit-instantiation
9989      explicit-specialization
9990      linkage-specification
9991      namespace-definition
9992
9993    GNU extension:
9994
9995    declaration:
9996       __extension__ declaration */
9997
9998 static void
9999 cp_parser_declaration (cp_parser* parser)
10000 {
10001   cp_token token1;
10002   cp_token token2;
10003   int saved_pedantic;
10004   void *p;
10005   tree attributes = NULL_TREE;
10006
10007   /* Check for the `__extension__' keyword.  */
10008   if (cp_parser_extension_opt (parser, &saved_pedantic))
10009     {
10010       /* Parse the qualified declaration.  */
10011       cp_parser_declaration (parser);
10012       /* Restore the PEDANTIC flag.  */
10013       pedantic = saved_pedantic;
10014
10015       return;
10016     }
10017
10018   /* Try to figure out what kind of declaration is present.  */
10019   token1 = *cp_lexer_peek_token (parser->lexer);
10020
10021   if (token1.type != CPP_EOF)
10022     token2 = *cp_lexer_peek_nth_token (parser->lexer, 2);
10023   else
10024     {
10025       token2.type = CPP_EOF;
10026       token2.keyword = RID_MAX;
10027     }
10028
10029   /* Get the high-water mark for the DECLARATOR_OBSTACK.  */
10030   p = obstack_alloc (&declarator_obstack, 0);
10031
10032   /* If the next token is `extern' and the following token is a string
10033      literal, then we have a linkage specification.  */
10034   if (token1.keyword == RID_EXTERN
10035       && cp_parser_is_pure_string_literal (&token2))
10036     cp_parser_linkage_specification (parser);
10037   /* If the next token is `template', then we have either a template
10038      declaration, an explicit instantiation, or an explicit
10039      specialization.  */
10040   else if (token1.keyword == RID_TEMPLATE)
10041     {
10042       /* `template <>' indicates a template specialization.  */
10043       if (token2.type == CPP_LESS
10044           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
10045         cp_parser_explicit_specialization (parser);
10046       /* `template <' indicates a template declaration.  */
10047       else if (token2.type == CPP_LESS)
10048         cp_parser_template_declaration (parser, /*member_p=*/false);
10049       /* Anything else must be an explicit instantiation.  */
10050       else
10051         cp_parser_explicit_instantiation (parser);
10052     }
10053   /* If the next token is `export', then we have a template
10054      declaration.  */
10055   else if (token1.keyword == RID_EXPORT)
10056     cp_parser_template_declaration (parser, /*member_p=*/false);
10057   /* If the next token is `extern', 'static' or 'inline' and the one
10058      after that is `template', we have a GNU extended explicit
10059      instantiation directive.  */
10060   else if (cp_parser_allow_gnu_extensions_p (parser)
10061            && (token1.keyword == RID_EXTERN
10062                || token1.keyword == RID_STATIC
10063                || token1.keyword == RID_INLINE)
10064            && token2.keyword == RID_TEMPLATE)
10065     cp_parser_explicit_instantiation (parser);
10066   /* If the next token is `namespace', check for a named or unnamed
10067      namespace definition.  */
10068   else if (token1.keyword == RID_NAMESPACE
10069            && (/* A named namespace definition.  */
10070                (token2.type == CPP_NAME
10071                 && (cp_lexer_peek_nth_token (parser->lexer, 3)->type
10072                     != CPP_EQ))
10073                /* An unnamed namespace definition.  */
10074                || token2.type == CPP_OPEN_BRACE
10075                || token2.keyword == RID_ATTRIBUTE))
10076     cp_parser_namespace_definition (parser);
10077   /* An inline (associated) namespace definition.  */
10078   else if (token1.keyword == RID_INLINE
10079            && token2.keyword == RID_NAMESPACE)
10080     cp_parser_namespace_definition (parser);
10081   /* Objective-C++ declaration/definition.  */
10082   else if (c_dialect_objc () && OBJC_IS_AT_KEYWORD (token1.keyword))
10083     cp_parser_objc_declaration (parser, NULL_TREE);
10084   else if (c_dialect_objc ()
10085            && token1.keyword == RID_ATTRIBUTE
10086            && cp_parser_objc_valid_prefix_attributes (parser, &attributes))
10087     cp_parser_objc_declaration (parser, attributes);
10088   /* We must have either a block declaration or a function
10089      definition.  */
10090   else
10091     /* Try to parse a block-declaration, or a function-definition.  */
10092     cp_parser_block_declaration (parser, /*statement_p=*/false);
10093
10094   /* Free any declarators allocated.  */
10095   obstack_free (&declarator_obstack, p);
10096 }
10097
10098 /* Parse a block-declaration.
10099
10100    block-declaration:
10101      simple-declaration
10102      asm-definition
10103      namespace-alias-definition
10104      using-declaration
10105      using-directive
10106
10107    GNU Extension:
10108
10109    block-declaration:
10110      __extension__ block-declaration
10111
10112    C++0x Extension:
10113
10114    block-declaration:
10115      static_assert-declaration
10116
10117    If STATEMENT_P is TRUE, then this block-declaration is occurring as
10118    part of a declaration-statement.  */
10119
10120 static void
10121 cp_parser_block_declaration (cp_parser *parser,
10122                              bool      statement_p)
10123 {
10124   cp_token *token1;
10125   int saved_pedantic;
10126
10127   /* Check for the `__extension__' keyword.  */
10128   if (cp_parser_extension_opt (parser, &saved_pedantic))
10129     {
10130       /* Parse the qualified declaration.  */
10131       cp_parser_block_declaration (parser, statement_p);
10132       /* Restore the PEDANTIC flag.  */
10133       pedantic = saved_pedantic;
10134
10135       return;
10136     }
10137
10138   /* Peek at the next token to figure out which kind of declaration is
10139      present.  */
10140   token1 = cp_lexer_peek_token (parser->lexer);
10141
10142   /* If the next keyword is `asm', we have an asm-definition.  */
10143   if (token1->keyword == RID_ASM)
10144     {
10145       if (statement_p)
10146         cp_parser_commit_to_tentative_parse (parser);
10147       cp_parser_asm_definition (parser);
10148     }
10149   /* If the next keyword is `namespace', we have a
10150      namespace-alias-definition.  */
10151   else if (token1->keyword == RID_NAMESPACE)
10152     cp_parser_namespace_alias_definition (parser);
10153   /* If the next keyword is `using', we have either a
10154      using-declaration or a using-directive.  */
10155   else if (token1->keyword == RID_USING)
10156     {
10157       cp_token *token2;
10158
10159       if (statement_p)
10160         cp_parser_commit_to_tentative_parse (parser);
10161       /* If the token after `using' is `namespace', then we have a
10162          using-directive.  */
10163       token2 = cp_lexer_peek_nth_token (parser->lexer, 2);
10164       if (token2->keyword == RID_NAMESPACE)
10165         cp_parser_using_directive (parser);
10166       /* Otherwise, it's a using-declaration.  */
10167       else
10168         cp_parser_using_declaration (parser,
10169                                      /*access_declaration_p=*/false);
10170     }
10171   /* If the next keyword is `__label__' we have a misplaced label
10172      declaration.  */
10173   else if (token1->keyword == RID_LABEL)
10174     {
10175       cp_lexer_consume_token (parser->lexer);
10176       error_at (token1->location, "%<__label__%> not at the beginning of a block");
10177       cp_parser_skip_to_end_of_statement (parser);
10178       /* If the next token is now a `;', consume it.  */
10179       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10180         cp_lexer_consume_token (parser->lexer);
10181     }
10182   /* If the next token is `static_assert' we have a static assertion.  */
10183   else if (token1->keyword == RID_STATIC_ASSERT)
10184     cp_parser_static_assert (parser, /*member_p=*/false);
10185   /* Anything else must be a simple-declaration.  */
10186   else
10187     cp_parser_simple_declaration (parser, !statement_p,
10188                                   /*maybe_range_for_decl*/NULL);
10189 }
10190
10191 /* Parse a simple-declaration.
10192
10193    simple-declaration:
10194      decl-specifier-seq [opt] init-declarator-list [opt] ;
10195
10196    init-declarator-list:
10197      init-declarator
10198      init-declarator-list , init-declarator
10199
10200    If FUNCTION_DEFINITION_ALLOWED_P is TRUE, then we also recognize a
10201    function-definition as a simple-declaration.
10202
10203    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
10204    parsed declaration if it is an uninitialized single declarator not followed
10205    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
10206    if present, will not be consumed.  */
10207
10208 static void
10209 cp_parser_simple_declaration (cp_parser* parser,
10210                               bool function_definition_allowed_p,
10211                               tree *maybe_range_for_decl)
10212 {
10213   cp_decl_specifier_seq decl_specifiers;
10214   int declares_class_or_enum;
10215   bool saw_declarator;
10216
10217   if (maybe_range_for_decl)
10218     *maybe_range_for_decl = NULL_TREE;
10219
10220   /* Defer access checks until we know what is being declared; the
10221      checks for names appearing in the decl-specifier-seq should be
10222      done as if we were in the scope of the thing being declared.  */
10223   push_deferring_access_checks (dk_deferred);
10224
10225   /* Parse the decl-specifier-seq.  We have to keep track of whether
10226      or not the decl-specifier-seq declares a named class or
10227      enumeration type, since that is the only case in which the
10228      init-declarator-list is allowed to be empty.
10229
10230      [dcl.dcl]
10231
10232      In a simple-declaration, the optional init-declarator-list can be
10233      omitted only when declaring a class or enumeration, that is when
10234      the decl-specifier-seq contains either a class-specifier, an
10235      elaborated-type-specifier, or an enum-specifier.  */
10236   cp_parser_decl_specifier_seq (parser,
10237                                 CP_PARSER_FLAGS_OPTIONAL,
10238                                 &decl_specifiers,
10239                                 &declares_class_or_enum);
10240   /* We no longer need to defer access checks.  */
10241   stop_deferring_access_checks ();
10242
10243   /* In a block scope, a valid declaration must always have a
10244      decl-specifier-seq.  By not trying to parse declarators, we can
10245      resolve the declaration/expression ambiguity more quickly.  */
10246   if (!function_definition_allowed_p
10247       && !decl_specifiers.any_specifiers_p)
10248     {
10249       cp_parser_error (parser, "expected declaration");
10250       goto done;
10251     }
10252
10253   /* If the next two tokens are both identifiers, the code is
10254      erroneous. The usual cause of this situation is code like:
10255
10256        T t;
10257
10258      where "T" should name a type -- but does not.  */
10259   if (!decl_specifiers.any_type_specifiers_p
10260       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
10261     {
10262       /* If parsing tentatively, we should commit; we really are
10263          looking at a declaration.  */
10264       cp_parser_commit_to_tentative_parse (parser);
10265       /* Give up.  */
10266       goto done;
10267     }
10268
10269   /* If we have seen at least one decl-specifier, and the next token
10270      is not a parenthesis, then we must be looking at a declaration.
10271      (After "int (" we might be looking at a functional cast.)  */
10272   if (decl_specifiers.any_specifiers_p
10273       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN)
10274       && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
10275       && !cp_parser_error_occurred (parser))
10276     cp_parser_commit_to_tentative_parse (parser);
10277
10278   /* Keep going until we hit the `;' at the end of the simple
10279      declaration.  */
10280   saw_declarator = false;
10281   while (cp_lexer_next_token_is_not (parser->lexer,
10282                                      CPP_SEMICOLON))
10283     {
10284       cp_token *token;
10285       bool function_definition_p;
10286       tree decl;
10287
10288       if (saw_declarator)
10289         {
10290           /* If we are processing next declarator, coma is expected */
10291           token = cp_lexer_peek_token (parser->lexer);
10292           gcc_assert (token->type == CPP_COMMA);
10293           cp_lexer_consume_token (parser->lexer);
10294           if (maybe_range_for_decl)
10295             *maybe_range_for_decl = error_mark_node;
10296         }
10297       else
10298         saw_declarator = true;
10299
10300       /* Parse the init-declarator.  */
10301       decl = cp_parser_init_declarator (parser, &decl_specifiers,
10302                                         /*checks=*/NULL,
10303                                         function_definition_allowed_p,
10304                                         /*member_p=*/false,
10305                                         declares_class_or_enum,
10306                                         &function_definition_p,
10307                                         maybe_range_for_decl);
10308       /* If an error occurred while parsing tentatively, exit quickly.
10309          (That usually happens when in the body of a function; each
10310          statement is treated as a declaration-statement until proven
10311          otherwise.)  */
10312       if (cp_parser_error_occurred (parser))
10313         goto done;
10314       /* Handle function definitions specially.  */
10315       if (function_definition_p)
10316         {
10317           /* If the next token is a `,', then we are probably
10318              processing something like:
10319
10320                void f() {}, *p;
10321
10322              which is erroneous.  */
10323           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
10324             {
10325               cp_token *token = cp_lexer_peek_token (parser->lexer);
10326               error_at (token->location,
10327                         "mixing"
10328                         " declarations and function-definitions is forbidden");
10329             }
10330           /* Otherwise, we're done with the list of declarators.  */
10331           else
10332             {
10333               pop_deferring_access_checks ();
10334               return;
10335             }
10336         }
10337       if (maybe_range_for_decl && *maybe_range_for_decl == NULL_TREE)
10338         *maybe_range_for_decl = decl;
10339       /* The next token should be either a `,' or a `;'.  */
10340       token = cp_lexer_peek_token (parser->lexer);
10341       /* If it's a `,', there are more declarators to come.  */
10342       if (token->type == CPP_COMMA)
10343         /* will be consumed next time around */;
10344       /* If it's a `;', we are done.  */
10345       else if (token->type == CPP_SEMICOLON || maybe_range_for_decl)
10346         break;
10347       /* Anything else is an error.  */
10348       else
10349         {
10350           /* If we have already issued an error message we don't need
10351              to issue another one.  */
10352           if (decl != error_mark_node
10353               || cp_parser_uncommitted_to_tentative_parse_p (parser))
10354             cp_parser_error (parser, "expected %<,%> or %<;%>");
10355           /* Skip tokens until we reach the end of the statement.  */
10356           cp_parser_skip_to_end_of_statement (parser);
10357           /* If the next token is now a `;', consume it.  */
10358           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
10359             cp_lexer_consume_token (parser->lexer);
10360           goto done;
10361         }
10362       /* After the first time around, a function-definition is not
10363          allowed -- even if it was OK at first.  For example:
10364
10365            int i, f() {}
10366
10367          is not valid.  */
10368       function_definition_allowed_p = false;
10369     }
10370
10371   /* Issue an error message if no declarators are present, and the
10372      decl-specifier-seq does not itself declare a class or
10373      enumeration.  */
10374   if (!saw_declarator)
10375     {
10376       if (cp_parser_declares_only_class_p (parser))
10377         shadow_tag (&decl_specifiers);
10378       /* Perform any deferred access checks.  */
10379       perform_deferred_access_checks ();
10380     }
10381
10382   /* Consume the `;'.  */
10383   if (!maybe_range_for_decl)
10384       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10385
10386  done:
10387   pop_deferring_access_checks ();
10388 }
10389
10390 /* Parse a decl-specifier-seq.
10391
10392    decl-specifier-seq:
10393      decl-specifier-seq [opt] decl-specifier
10394
10395    decl-specifier:
10396      storage-class-specifier
10397      type-specifier
10398      function-specifier
10399      friend
10400      typedef
10401
10402    GNU Extension:
10403
10404    decl-specifier:
10405      attributes
10406
10407    Set *DECL_SPECS to a representation of the decl-specifier-seq.
10408
10409    The parser flags FLAGS is used to control type-specifier parsing.
10410
10411    *DECLARES_CLASS_OR_ENUM is set to the bitwise or of the following
10412    flags:
10413
10414      1: one of the decl-specifiers is an elaborated-type-specifier
10415         (i.e., a type declaration)
10416      2: one of the decl-specifiers is an enum-specifier or a
10417         class-specifier (i.e., a type definition)
10418
10419    */
10420
10421 static void
10422 cp_parser_decl_specifier_seq (cp_parser* parser,
10423                               cp_parser_flags flags,
10424                               cp_decl_specifier_seq *decl_specs,
10425                               int* declares_class_or_enum)
10426 {
10427   bool constructor_possible_p = !parser->in_declarator_p;
10428   cp_token *start_token = NULL;
10429
10430   /* Clear DECL_SPECS.  */
10431   clear_decl_specs (decl_specs);
10432
10433   /* Assume no class or enumeration type is declared.  */
10434   *declares_class_or_enum = 0;
10435
10436   /* Keep reading specifiers until there are no more to read.  */
10437   while (true)
10438     {
10439       bool constructor_p;
10440       bool found_decl_spec;
10441       cp_token *token;
10442
10443       /* Peek at the next token.  */
10444       token = cp_lexer_peek_token (parser->lexer);
10445
10446       /* Save the first token of the decl spec list for error
10447          reporting.  */
10448       if (!start_token)
10449         start_token = token;
10450       /* Handle attributes.  */
10451       if (token->keyword == RID_ATTRIBUTE)
10452         {
10453           /* Parse the attributes.  */
10454           decl_specs->attributes
10455             = chainon (decl_specs->attributes,
10456                        cp_parser_attributes_opt (parser));
10457           continue;
10458         }
10459       /* Assume we will find a decl-specifier keyword.  */
10460       found_decl_spec = true;
10461       /* If the next token is an appropriate keyword, we can simply
10462          add it to the list.  */
10463       switch (token->keyword)
10464         {
10465           /* decl-specifier:
10466                friend
10467                constexpr */
10468         case RID_FRIEND:
10469           if (!at_class_scope_p ())
10470             {
10471               error_at (token->location, "%<friend%> used outside of class");
10472               cp_lexer_purge_token (parser->lexer);
10473             }
10474           else
10475             {
10476               ++decl_specs->specs[(int) ds_friend];
10477               /* Consume the token.  */
10478               cp_lexer_consume_token (parser->lexer);
10479             }
10480           break;
10481
10482         case RID_CONSTEXPR:
10483           ++decl_specs->specs[(int) ds_constexpr];
10484           cp_lexer_consume_token (parser->lexer);
10485           break;
10486
10487           /* function-specifier:
10488                inline
10489                virtual
10490                explicit  */
10491         case RID_INLINE:
10492         case RID_VIRTUAL:
10493         case RID_EXPLICIT:
10494           cp_parser_function_specifier_opt (parser, decl_specs);
10495           break;
10496
10497           /* decl-specifier:
10498                typedef  */
10499         case RID_TYPEDEF:
10500           ++decl_specs->specs[(int) ds_typedef];
10501           /* Consume the token.  */
10502           cp_lexer_consume_token (parser->lexer);
10503           /* A constructor declarator cannot appear in a typedef.  */
10504           constructor_possible_p = false;
10505           /* The "typedef" keyword can only occur in a declaration; we
10506              may as well commit at this point.  */
10507           cp_parser_commit_to_tentative_parse (parser);
10508
10509           if (decl_specs->storage_class != sc_none)
10510             decl_specs->conflicting_specifiers_p = true;
10511           break;
10512
10513           /* storage-class-specifier:
10514                auto
10515                register
10516                static
10517                extern
10518                mutable
10519
10520              GNU Extension:
10521                thread  */
10522         case RID_AUTO:
10523           if (cxx_dialect == cxx98) 
10524             {
10525               /* Consume the token.  */
10526               cp_lexer_consume_token (parser->lexer);
10527
10528               /* Complain about `auto' as a storage specifier, if
10529                  we're complaining about C++0x compatibility.  */
10530               warning_at (token->location, OPT_Wc__0x_compat, "%<auto%>"
10531                           " will change meaning in C++0x; please remove it");
10532
10533               /* Set the storage class anyway.  */
10534               cp_parser_set_storage_class (parser, decl_specs, RID_AUTO,
10535                                            token->location);
10536             }
10537           else
10538             /* C++0x auto type-specifier.  */
10539             found_decl_spec = false;
10540           break;
10541
10542         case RID_REGISTER:
10543         case RID_STATIC:
10544         case RID_EXTERN:
10545         case RID_MUTABLE:
10546           /* Consume the token.  */
10547           cp_lexer_consume_token (parser->lexer);
10548           cp_parser_set_storage_class (parser, decl_specs, token->keyword,
10549                                        token->location);
10550           break;
10551         case RID_THREAD:
10552           /* Consume the token.  */
10553           cp_lexer_consume_token (parser->lexer);
10554           ++decl_specs->specs[(int) ds_thread];
10555           break;
10556
10557         default:
10558           /* We did not yet find a decl-specifier yet.  */
10559           found_decl_spec = false;
10560           break;
10561         }
10562
10563       if (found_decl_spec
10564           && (flags & CP_PARSER_FLAGS_ONLY_TYPE_OR_CONSTEXPR)
10565           && token->keyword != RID_CONSTEXPR)
10566         error ("decl-specifier invalid in condition");
10567
10568       /* Constructors are a special case.  The `S' in `S()' is not a
10569          decl-specifier; it is the beginning of the declarator.  */
10570       constructor_p
10571         = (!found_decl_spec
10572            && constructor_possible_p
10573            && (cp_parser_constructor_declarator_p
10574                (parser, decl_specs->specs[(int) ds_friend] != 0)));
10575
10576       /* If we don't have a DECL_SPEC yet, then we must be looking at
10577          a type-specifier.  */
10578       if (!found_decl_spec && !constructor_p)
10579         {
10580           int decl_spec_declares_class_or_enum;
10581           bool is_cv_qualifier;
10582           tree type_spec;
10583
10584           type_spec
10585             = cp_parser_type_specifier (parser, flags,
10586                                         decl_specs,
10587                                         /*is_declaration=*/true,
10588                                         &decl_spec_declares_class_or_enum,
10589                                         &is_cv_qualifier);
10590           *declares_class_or_enum |= decl_spec_declares_class_or_enum;
10591
10592           /* If this type-specifier referenced a user-defined type
10593              (a typedef, class-name, etc.), then we can't allow any
10594              more such type-specifiers henceforth.
10595
10596              [dcl.spec]
10597
10598              The longest sequence of decl-specifiers that could
10599              possibly be a type name is taken as the
10600              decl-specifier-seq of a declaration.  The sequence shall
10601              be self-consistent as described below.
10602
10603              [dcl.type]
10604
10605              As a general rule, at most one type-specifier is allowed
10606              in the complete decl-specifier-seq of a declaration.  The
10607              only exceptions are the following:
10608
10609              -- const or volatile can be combined with any other
10610                 type-specifier.
10611
10612              -- signed or unsigned can be combined with char, long,
10613                 short, or int.
10614
10615              -- ..
10616
10617              Example:
10618
10619                typedef char* Pc;
10620                void g (const int Pc);
10621
10622              Here, Pc is *not* part of the decl-specifier seq; it's
10623              the declarator.  Therefore, once we see a type-specifier
10624              (other than a cv-qualifier), we forbid any additional
10625              user-defined types.  We *do* still allow things like `int
10626              int' to be considered a decl-specifier-seq, and issue the
10627              error message later.  */
10628           if (type_spec && !is_cv_qualifier)
10629             flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
10630           /* A constructor declarator cannot follow a type-specifier.  */
10631           if (type_spec)
10632             {
10633               constructor_possible_p = false;
10634               found_decl_spec = true;
10635               if (!is_cv_qualifier)
10636                 decl_specs->any_type_specifiers_p = true;
10637             }
10638         }
10639
10640       /* If we still do not have a DECL_SPEC, then there are no more
10641          decl-specifiers.  */
10642       if (!found_decl_spec)
10643         break;
10644
10645       decl_specs->any_specifiers_p = true;
10646       /* After we see one decl-specifier, further decl-specifiers are
10647          always optional.  */
10648       flags |= CP_PARSER_FLAGS_OPTIONAL;
10649     }
10650
10651   cp_parser_check_decl_spec (decl_specs, start_token->location);
10652
10653   /* Don't allow a friend specifier with a class definition.  */
10654   if (decl_specs->specs[(int) ds_friend] != 0
10655       && (*declares_class_or_enum & 2))
10656     error_at (start_token->location,
10657               "class definition may not be declared a friend");
10658 }
10659
10660 /* Parse an (optional) storage-class-specifier.
10661
10662    storage-class-specifier:
10663      auto
10664      register
10665      static
10666      extern
10667      mutable
10668
10669    GNU Extension:
10670
10671    storage-class-specifier:
10672      thread
10673
10674    Returns an IDENTIFIER_NODE corresponding to the keyword used.  */
10675
10676 static tree
10677 cp_parser_storage_class_specifier_opt (cp_parser* parser)
10678 {
10679   switch (cp_lexer_peek_token (parser->lexer)->keyword)
10680     {
10681     case RID_AUTO:
10682       if (cxx_dialect != cxx98)
10683         return NULL_TREE;
10684       /* Fall through for C++98.  */
10685
10686     case RID_REGISTER:
10687     case RID_STATIC:
10688     case RID_EXTERN:
10689     case RID_MUTABLE:
10690     case RID_THREAD:
10691       /* Consume the token.  */
10692       return cp_lexer_consume_token (parser->lexer)->u.value;
10693
10694     default:
10695       return NULL_TREE;
10696     }
10697 }
10698
10699 /* Parse an (optional) function-specifier.
10700
10701    function-specifier:
10702      inline
10703      virtual
10704      explicit
10705
10706    Returns an IDENTIFIER_NODE corresponding to the keyword used.
10707    Updates DECL_SPECS, if it is non-NULL.  */
10708
10709 static tree
10710 cp_parser_function_specifier_opt (cp_parser* parser,
10711                                   cp_decl_specifier_seq *decl_specs)
10712 {
10713   cp_token *token = cp_lexer_peek_token (parser->lexer);
10714   switch (token->keyword)
10715     {
10716     case RID_INLINE:
10717       if (decl_specs)
10718         ++decl_specs->specs[(int) ds_inline];
10719       break;
10720
10721     case RID_VIRTUAL:
10722       /* 14.5.2.3 [temp.mem]
10723
10724          A member function template shall not be virtual.  */
10725       if (PROCESSING_REAL_TEMPLATE_DECL_P ())
10726         error_at (token->location, "templates may not be %<virtual%>");
10727       else if (decl_specs)
10728         ++decl_specs->specs[(int) ds_virtual];
10729       break;
10730
10731     case RID_EXPLICIT:
10732       if (decl_specs)
10733         ++decl_specs->specs[(int) ds_explicit];
10734       break;
10735
10736     default:
10737       return NULL_TREE;
10738     }
10739
10740   /* Consume the token.  */
10741   return cp_lexer_consume_token (parser->lexer)->u.value;
10742 }
10743
10744 /* Parse a linkage-specification.
10745
10746    linkage-specification:
10747      extern string-literal { declaration-seq [opt] }
10748      extern string-literal declaration  */
10749
10750 static void
10751 cp_parser_linkage_specification (cp_parser* parser)
10752 {
10753   tree linkage;
10754
10755   /* Look for the `extern' keyword.  */
10756   cp_parser_require_keyword (parser, RID_EXTERN, RT_EXTERN);
10757
10758   /* Look for the string-literal.  */
10759   linkage = cp_parser_string_literal (parser, false, false);
10760
10761   /* Transform the literal into an identifier.  If the literal is a
10762      wide-character string, or contains embedded NULs, then we can't
10763      handle it as the user wants.  */
10764   if (strlen (TREE_STRING_POINTER (linkage))
10765       != (size_t) (TREE_STRING_LENGTH (linkage) - 1))
10766     {
10767       cp_parser_error (parser, "invalid linkage-specification");
10768       /* Assume C++ linkage.  */
10769       linkage = lang_name_cplusplus;
10770     }
10771   else
10772     linkage = get_identifier (TREE_STRING_POINTER (linkage));
10773
10774   /* We're now using the new linkage.  */
10775   push_lang_context (linkage);
10776
10777   /* If the next token is a `{', then we're using the first
10778      production.  */
10779   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
10780     {
10781       /* Consume the `{' token.  */
10782       cp_lexer_consume_token (parser->lexer);
10783       /* Parse the declarations.  */
10784       cp_parser_declaration_seq_opt (parser);
10785       /* Look for the closing `}'.  */
10786       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
10787     }
10788   /* Otherwise, there's just one declaration.  */
10789   else
10790     {
10791       bool saved_in_unbraced_linkage_specification_p;
10792
10793       saved_in_unbraced_linkage_specification_p
10794         = parser->in_unbraced_linkage_specification_p;
10795       parser->in_unbraced_linkage_specification_p = true;
10796       cp_parser_declaration (parser);
10797       parser->in_unbraced_linkage_specification_p
10798         = saved_in_unbraced_linkage_specification_p;
10799     }
10800
10801   /* We're done with the linkage-specification.  */
10802   pop_lang_context ();
10803 }
10804
10805 /* Parse a static_assert-declaration.
10806
10807    static_assert-declaration:
10808      static_assert ( constant-expression , string-literal ) ; 
10809
10810    If MEMBER_P, this static_assert is a class member.  */
10811
10812 static void 
10813 cp_parser_static_assert(cp_parser *parser, bool member_p)
10814 {
10815   tree condition;
10816   tree message;
10817   cp_token *token;
10818   location_t saved_loc;
10819   bool dummy;
10820
10821   /* Peek at the `static_assert' token so we can keep track of exactly
10822      where the static assertion started.  */
10823   token = cp_lexer_peek_token (parser->lexer);
10824   saved_loc = token->location;
10825
10826   /* Look for the `static_assert' keyword.  */
10827   if (!cp_parser_require_keyword (parser, RID_STATIC_ASSERT, 
10828                                   RT_STATIC_ASSERT))
10829     return;
10830
10831   /*  We know we are in a static assertion; commit to any tentative
10832       parse.  */
10833   if (cp_parser_parsing_tentatively (parser))
10834     cp_parser_commit_to_tentative_parse (parser);
10835
10836   /* Parse the `(' starting the static assertion condition.  */
10837   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
10838
10839   /* Parse the constant-expression.  Allow a non-constant expression
10840      here in order to give better diagnostics in finish_static_assert.  */
10841   condition = 
10842     cp_parser_constant_expression (parser,
10843                                    /*allow_non_constant_p=*/true,
10844                                    /*non_constant_p=*/&dummy);
10845
10846   /* Parse the separating `,'.  */
10847   cp_parser_require (parser, CPP_COMMA, RT_COMMA);
10848
10849   /* Parse the string-literal message.  */
10850   message = cp_parser_string_literal (parser, 
10851                                       /*translate=*/false,
10852                                       /*wide_ok=*/true);
10853
10854   /* A `)' completes the static assertion.  */
10855   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
10856     cp_parser_skip_to_closing_parenthesis (parser, 
10857                                            /*recovering=*/true, 
10858                                            /*or_comma=*/false,
10859                                            /*consume_paren=*/true);
10860
10861   /* A semicolon terminates the declaration.  */
10862   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
10863
10864   /* Complete the static assertion, which may mean either processing 
10865      the static assert now or saving it for template instantiation.  */
10866   finish_static_assert (condition, message, saved_loc, member_p);
10867 }
10868
10869 /* Parse a `decltype' type. Returns the type. 
10870
10871    simple-type-specifier:
10872      decltype ( expression )  */
10873
10874 static tree
10875 cp_parser_decltype (cp_parser *parser)
10876 {
10877   tree expr;
10878   bool id_expression_or_member_access_p = false;
10879   const char *saved_message;
10880   bool saved_integral_constant_expression_p;
10881   bool saved_non_integral_constant_expression_p;
10882   cp_token *id_expr_start_token;
10883   cp_token *start_token = cp_lexer_peek_token (parser->lexer);
10884
10885   if (start_token->type == CPP_DECLTYPE)
10886     {
10887       /* Already parsed.  */
10888       cp_lexer_consume_token (parser->lexer);
10889       return start_token->u.value;
10890     }
10891
10892   /* Look for the `decltype' token.  */
10893   if (!cp_parser_require_keyword (parser, RID_DECLTYPE, RT_DECLTYPE))
10894     return error_mark_node;
10895
10896   /* Types cannot be defined in a `decltype' expression.  Save away the
10897      old message.  */
10898   saved_message = parser->type_definition_forbidden_message;
10899
10900   /* And create the new one.  */
10901   parser->type_definition_forbidden_message
10902     = G_("types may not be defined in %<decltype%> expressions");
10903
10904   /* The restrictions on constant-expressions do not apply inside
10905      decltype expressions.  */
10906   saved_integral_constant_expression_p
10907     = parser->integral_constant_expression_p;
10908   saved_non_integral_constant_expression_p
10909     = parser->non_integral_constant_expression_p;
10910   parser->integral_constant_expression_p = false;
10911
10912   /* Do not actually evaluate the expression.  */
10913   ++cp_unevaluated_operand;
10914
10915   /* Do not warn about problems with the expression.  */
10916   ++c_inhibit_evaluation_warnings;
10917
10918   /* Parse the opening `('.  */
10919   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
10920     return error_mark_node;
10921   
10922   /* First, try parsing an id-expression.  */
10923   id_expr_start_token = cp_lexer_peek_token (parser->lexer);
10924   cp_parser_parse_tentatively (parser);
10925   expr = cp_parser_id_expression (parser,
10926                                   /*template_keyword_p=*/false,
10927                                   /*check_dependency_p=*/true,
10928                                   /*template_p=*/NULL,
10929                                   /*declarator_p=*/false,
10930                                   /*optional_p=*/false);
10931
10932   if (!cp_parser_error_occurred (parser) && expr != error_mark_node)
10933     {
10934       bool non_integral_constant_expression_p = false;
10935       tree id_expression = expr;
10936       cp_id_kind idk;
10937       const char *error_msg;
10938
10939       if (TREE_CODE (expr) == IDENTIFIER_NODE)
10940         /* Lookup the name we got back from the id-expression.  */
10941         expr = cp_parser_lookup_name (parser, expr,
10942                                       none_type,
10943                                       /*is_template=*/false,
10944                                       /*is_namespace=*/false,
10945                                       /*check_dependency=*/true,
10946                                       /*ambiguous_decls=*/NULL,
10947                                       id_expr_start_token->location);
10948
10949       if (expr
10950           && expr != error_mark_node
10951           && TREE_CODE (expr) != TEMPLATE_ID_EXPR
10952           && TREE_CODE (expr) != TYPE_DECL
10953           && (TREE_CODE (expr) != BIT_NOT_EXPR
10954               || !TYPE_P (TREE_OPERAND (expr, 0)))
10955           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10956         {
10957           /* Complete lookup of the id-expression.  */
10958           expr = (finish_id_expression
10959                   (id_expression, expr, parser->scope, &idk,
10960                    /*integral_constant_expression_p=*/false,
10961                    /*allow_non_integral_constant_expression_p=*/true,
10962                    &non_integral_constant_expression_p,
10963                    /*template_p=*/false,
10964                    /*done=*/true,
10965                    /*address_p=*/false,
10966                    /*template_arg_p=*/false,
10967                    &error_msg,
10968                    id_expr_start_token->location));
10969
10970           if (expr == error_mark_node)
10971             /* We found an id-expression, but it was something that we
10972                should not have found. This is an error, not something
10973                we can recover from, so note that we found an
10974                id-expression and we'll recover as gracefully as
10975                possible.  */
10976             id_expression_or_member_access_p = true;
10977         }
10978
10979       if (expr 
10980           && expr != error_mark_node
10981           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
10982         /* We have an id-expression.  */
10983         id_expression_or_member_access_p = true;
10984     }
10985
10986   if (!id_expression_or_member_access_p)
10987     {
10988       /* Abort the id-expression parse.  */
10989       cp_parser_abort_tentative_parse (parser);
10990
10991       /* Parsing tentatively, again.  */
10992       cp_parser_parse_tentatively (parser);
10993
10994       /* Parse a class member access.  */
10995       expr = cp_parser_postfix_expression (parser, /*address_p=*/false,
10996                                            /*cast_p=*/false,
10997                                            /*member_access_only_p=*/true, NULL);
10998
10999       if (expr 
11000           && expr != error_mark_node
11001           && cp_lexer_peek_token (parser->lexer)->type == CPP_CLOSE_PAREN)
11002         /* We have an id-expression.  */
11003         id_expression_or_member_access_p = true;
11004     }
11005
11006   if (id_expression_or_member_access_p)
11007     /* We have parsed the complete id-expression or member access.  */
11008     cp_parser_parse_definitely (parser);
11009   else
11010     {
11011       bool saved_greater_than_is_operator_p;
11012
11013       /* Abort our attempt to parse an id-expression or member access
11014          expression.  */
11015       cp_parser_abort_tentative_parse (parser);
11016
11017       /* Within a parenthesized expression, a `>' token is always
11018          the greater-than operator.  */
11019       saved_greater_than_is_operator_p
11020         = parser->greater_than_is_operator_p;
11021       parser->greater_than_is_operator_p = true;
11022
11023       /* Parse a full expression.  */
11024       expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
11025
11026       /* The `>' token might be the end of a template-id or
11027          template-parameter-list now.  */
11028       parser->greater_than_is_operator_p
11029         = saved_greater_than_is_operator_p;
11030     }
11031
11032   /* Go back to evaluating expressions.  */
11033   --cp_unevaluated_operand;
11034   --c_inhibit_evaluation_warnings;
11035
11036   /* Restore the old message and the integral constant expression
11037      flags.  */
11038   parser->type_definition_forbidden_message = saved_message;
11039   parser->integral_constant_expression_p
11040     = saved_integral_constant_expression_p;
11041   parser->non_integral_constant_expression_p
11042     = saved_non_integral_constant_expression_p;
11043
11044   /* Parse to the closing `)'.  */
11045   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
11046     {
11047       cp_parser_skip_to_closing_parenthesis (parser, true, false,
11048                                              /*consume_paren=*/true);
11049       return error_mark_node;
11050     }
11051
11052   expr = finish_decltype_type (expr, id_expression_or_member_access_p,
11053                                tf_warning_or_error);
11054
11055   /* Replace the decltype with a CPP_DECLTYPE so we don't need to parse
11056      it again.  */
11057   start_token->type = CPP_DECLTYPE;
11058   start_token->u.value = expr;
11059   start_token->keyword = RID_MAX;
11060   cp_lexer_purge_tokens_after (parser->lexer, start_token);
11061
11062   return expr;
11063 }
11064
11065 /* Special member functions [gram.special] */
11066
11067 /* Parse a conversion-function-id.
11068
11069    conversion-function-id:
11070      operator conversion-type-id
11071
11072    Returns an IDENTIFIER_NODE representing the operator.  */
11073
11074 static tree
11075 cp_parser_conversion_function_id (cp_parser* parser)
11076 {
11077   tree type;
11078   tree saved_scope;
11079   tree saved_qualifying_scope;
11080   tree saved_object_scope;
11081   tree pushed_scope = NULL_TREE;
11082
11083   /* Look for the `operator' token.  */
11084   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11085     return error_mark_node;
11086   /* When we parse the conversion-type-id, the current scope will be
11087      reset.  However, we need that information in able to look up the
11088      conversion function later, so we save it here.  */
11089   saved_scope = parser->scope;
11090   saved_qualifying_scope = parser->qualifying_scope;
11091   saved_object_scope = parser->object_scope;
11092   /* We must enter the scope of the class so that the names of
11093      entities declared within the class are available in the
11094      conversion-type-id.  For example, consider:
11095
11096        struct S {
11097          typedef int I;
11098          operator I();
11099        };
11100
11101        S::operator I() { ... }
11102
11103      In order to see that `I' is a type-name in the definition, we
11104      must be in the scope of `S'.  */
11105   if (saved_scope)
11106     pushed_scope = push_scope (saved_scope);
11107   /* Parse the conversion-type-id.  */
11108   type = cp_parser_conversion_type_id (parser);
11109   /* Leave the scope of the class, if any.  */
11110   if (pushed_scope)
11111     pop_scope (pushed_scope);
11112   /* Restore the saved scope.  */
11113   parser->scope = saved_scope;
11114   parser->qualifying_scope = saved_qualifying_scope;
11115   parser->object_scope = saved_object_scope;
11116   /* If the TYPE is invalid, indicate failure.  */
11117   if (type == error_mark_node)
11118     return error_mark_node;
11119   return mangle_conv_op_name_for_type (type);
11120 }
11121
11122 /* Parse a conversion-type-id:
11123
11124    conversion-type-id:
11125      type-specifier-seq conversion-declarator [opt]
11126
11127    Returns the TYPE specified.  */
11128
11129 static tree
11130 cp_parser_conversion_type_id (cp_parser* parser)
11131 {
11132   tree attributes;
11133   cp_decl_specifier_seq type_specifiers;
11134   cp_declarator *declarator;
11135   tree type_specified;
11136
11137   /* Parse the attributes.  */
11138   attributes = cp_parser_attributes_opt (parser);
11139   /* Parse the type-specifiers.  */
11140   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
11141                                 /*is_trailing_return=*/false,
11142                                 &type_specifiers);
11143   /* If that didn't work, stop.  */
11144   if (type_specifiers.type == error_mark_node)
11145     return error_mark_node;
11146   /* Parse the conversion-declarator.  */
11147   declarator = cp_parser_conversion_declarator_opt (parser);
11148
11149   type_specified =  grokdeclarator (declarator, &type_specifiers, TYPENAME,
11150                                     /*initialized=*/0, &attributes);
11151   if (attributes)
11152     cplus_decl_attributes (&type_specified, attributes, /*flags=*/0);
11153
11154   /* Don't give this error when parsing tentatively.  This happens to
11155      work because we always parse this definitively once.  */
11156   if (! cp_parser_uncommitted_to_tentative_parse_p (parser)
11157       && type_uses_auto (type_specified))
11158     {
11159       error ("invalid use of %<auto%> in conversion operator");
11160       return error_mark_node;
11161     }
11162
11163   return type_specified;
11164 }
11165
11166 /* Parse an (optional) conversion-declarator.
11167
11168    conversion-declarator:
11169      ptr-operator conversion-declarator [opt]
11170
11171    */
11172
11173 static cp_declarator *
11174 cp_parser_conversion_declarator_opt (cp_parser* parser)
11175 {
11176   enum tree_code code;
11177   tree class_type;
11178   cp_cv_quals cv_quals;
11179
11180   /* We don't know if there's a ptr-operator next, or not.  */
11181   cp_parser_parse_tentatively (parser);
11182   /* Try the ptr-operator.  */
11183   code = cp_parser_ptr_operator (parser, &class_type, &cv_quals);
11184   /* If it worked, look for more conversion-declarators.  */
11185   if (cp_parser_parse_definitely (parser))
11186     {
11187       cp_declarator *declarator;
11188
11189       /* Parse another optional declarator.  */
11190       declarator = cp_parser_conversion_declarator_opt (parser);
11191
11192       return cp_parser_make_indirect_declarator
11193         (code, class_type, cv_quals, declarator);
11194    }
11195
11196   return NULL;
11197 }
11198
11199 /* Parse an (optional) ctor-initializer.
11200
11201    ctor-initializer:
11202      : mem-initializer-list
11203
11204    Returns TRUE iff the ctor-initializer was actually present.  */
11205
11206 static bool
11207 cp_parser_ctor_initializer_opt (cp_parser* parser)
11208 {
11209   /* If the next token is not a `:', then there is no
11210      ctor-initializer.  */
11211   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
11212     {
11213       /* Do default initialization of any bases and members.  */
11214       if (DECL_CONSTRUCTOR_P (current_function_decl))
11215         finish_mem_initializers (NULL_TREE);
11216
11217       return false;
11218     }
11219
11220   /* Consume the `:' token.  */
11221   cp_lexer_consume_token (parser->lexer);
11222   /* And the mem-initializer-list.  */
11223   cp_parser_mem_initializer_list (parser);
11224
11225   return true;
11226 }
11227
11228 /* Parse a mem-initializer-list.
11229
11230    mem-initializer-list:
11231      mem-initializer ... [opt]
11232      mem-initializer ... [opt] , mem-initializer-list  */
11233
11234 static void
11235 cp_parser_mem_initializer_list (cp_parser* parser)
11236 {
11237   tree mem_initializer_list = NULL_TREE;
11238   cp_token *token = cp_lexer_peek_token (parser->lexer);
11239
11240   /* Let the semantic analysis code know that we are starting the
11241      mem-initializer-list.  */
11242   if (!DECL_CONSTRUCTOR_P (current_function_decl))
11243     error_at (token->location,
11244               "only constructors take member initializers");
11245
11246   /* Loop through the list.  */
11247   while (true)
11248     {
11249       tree mem_initializer;
11250
11251       token = cp_lexer_peek_token (parser->lexer);
11252       /* Parse the mem-initializer.  */
11253       mem_initializer = cp_parser_mem_initializer (parser);
11254       /* If the next token is a `...', we're expanding member initializers. */
11255       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
11256         {
11257           /* Consume the `...'. */
11258           cp_lexer_consume_token (parser->lexer);
11259
11260           /* The TREE_PURPOSE must be a _TYPE, because base-specifiers
11261              can be expanded but members cannot. */
11262           if (mem_initializer != error_mark_node
11263               && !TYPE_P (TREE_PURPOSE (mem_initializer)))
11264             {
11265               error_at (token->location,
11266                         "cannot expand initializer for member %<%D%>",
11267                         TREE_PURPOSE (mem_initializer));
11268               mem_initializer = error_mark_node;
11269             }
11270
11271           /* Construct the pack expansion type. */
11272           if (mem_initializer != error_mark_node)
11273             mem_initializer = make_pack_expansion (mem_initializer);
11274         }
11275       /* Add it to the list, unless it was erroneous.  */
11276       if (mem_initializer != error_mark_node)
11277         {
11278           TREE_CHAIN (mem_initializer) = mem_initializer_list;
11279           mem_initializer_list = mem_initializer;
11280         }
11281       /* If the next token is not a `,', we're done.  */
11282       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11283         break;
11284       /* Consume the `,' token.  */
11285       cp_lexer_consume_token (parser->lexer);
11286     }
11287
11288   /* Perform semantic analysis.  */
11289   if (DECL_CONSTRUCTOR_P (current_function_decl))
11290     finish_mem_initializers (mem_initializer_list);
11291 }
11292
11293 /* Parse a mem-initializer.
11294
11295    mem-initializer:
11296      mem-initializer-id ( expression-list [opt] )
11297      mem-initializer-id braced-init-list
11298
11299    GNU extension:
11300
11301    mem-initializer:
11302      ( expression-list [opt] )
11303
11304    Returns a TREE_LIST.  The TREE_PURPOSE is the TYPE (for a base
11305    class) or FIELD_DECL (for a non-static data member) to initialize;
11306    the TREE_VALUE is the expression-list.  An empty initialization
11307    list is represented by void_list_node.  */
11308
11309 static tree
11310 cp_parser_mem_initializer (cp_parser* parser)
11311 {
11312   tree mem_initializer_id;
11313   tree expression_list;
11314   tree member;
11315   cp_token *token = cp_lexer_peek_token (parser->lexer);
11316
11317   /* Find out what is being initialized.  */
11318   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
11319     {
11320       permerror (token->location,
11321                  "anachronistic old-style base class initializer");
11322       mem_initializer_id = NULL_TREE;
11323     }
11324   else
11325     {
11326       mem_initializer_id = cp_parser_mem_initializer_id (parser);
11327       if (mem_initializer_id == error_mark_node)
11328         return mem_initializer_id;
11329     }
11330   member = expand_member_init (mem_initializer_id);
11331   if (member && !DECL_P (member))
11332     in_base_initializer = 1;
11333
11334   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
11335     {
11336       bool expr_non_constant_p;
11337       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
11338       expression_list = cp_parser_braced_list (parser, &expr_non_constant_p);
11339       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
11340       expression_list = build_tree_list (NULL_TREE, expression_list);
11341     }
11342   else
11343     {
11344       VEC(tree,gc)* vec;
11345       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
11346                                                      /*cast_p=*/false,
11347                                                      /*allow_expansion_p=*/true,
11348                                                      /*non_constant_p=*/NULL);
11349       if (vec == NULL)
11350         return error_mark_node;
11351       expression_list = build_tree_list_vec (vec);
11352       release_tree_vector (vec);
11353     }
11354
11355   if (expression_list == error_mark_node)
11356     return error_mark_node;
11357   if (!expression_list)
11358     expression_list = void_type_node;
11359
11360   in_base_initializer = 0;
11361
11362   return member ? build_tree_list (member, expression_list) : error_mark_node;
11363 }
11364
11365 /* Parse a mem-initializer-id.
11366
11367    mem-initializer-id:
11368      :: [opt] nested-name-specifier [opt] class-name
11369      identifier
11370
11371    Returns a TYPE indicating the class to be initializer for the first
11372    production.  Returns an IDENTIFIER_NODE indicating the data member
11373    to be initialized for the second production.  */
11374
11375 static tree
11376 cp_parser_mem_initializer_id (cp_parser* parser)
11377 {
11378   bool global_scope_p;
11379   bool nested_name_specifier_p;
11380   bool template_p = false;
11381   tree id;
11382
11383   cp_token *token = cp_lexer_peek_token (parser->lexer);
11384
11385   /* `typename' is not allowed in this context ([temp.res]).  */
11386   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
11387     {
11388       error_at (token->location, 
11389                 "keyword %<typename%> not allowed in this context (a qualified "
11390                 "member initializer is implicitly a type)");
11391       cp_lexer_consume_token (parser->lexer);
11392     }
11393   /* Look for the optional `::' operator.  */
11394   global_scope_p
11395     = (cp_parser_global_scope_opt (parser,
11396                                    /*current_scope_valid_p=*/false)
11397        != NULL_TREE);
11398   /* Look for the optional nested-name-specifier.  The simplest way to
11399      implement:
11400
11401        [temp.res]
11402
11403        The keyword `typename' is not permitted in a base-specifier or
11404        mem-initializer; in these contexts a qualified name that
11405        depends on a template-parameter is implicitly assumed to be a
11406        type name.
11407
11408      is to assume that we have seen the `typename' keyword at this
11409      point.  */
11410   nested_name_specifier_p
11411     = (cp_parser_nested_name_specifier_opt (parser,
11412                                             /*typename_keyword_p=*/true,
11413                                             /*check_dependency_p=*/true,
11414                                             /*type_p=*/true,
11415                                             /*is_declaration=*/true)
11416        != NULL_TREE);
11417   if (nested_name_specifier_p)
11418     template_p = cp_parser_optional_template_keyword (parser);
11419   /* If there is a `::' operator or a nested-name-specifier, then we
11420      are definitely looking for a class-name.  */
11421   if (global_scope_p || nested_name_specifier_p)
11422     return cp_parser_class_name (parser,
11423                                  /*typename_keyword_p=*/true,
11424                                  /*template_keyword_p=*/template_p,
11425                                  typename_type,
11426                                  /*check_dependency_p=*/true,
11427                                  /*class_head_p=*/false,
11428                                  /*is_declaration=*/true);
11429   /* Otherwise, we could also be looking for an ordinary identifier.  */
11430   cp_parser_parse_tentatively (parser);
11431   /* Try a class-name.  */
11432   id = cp_parser_class_name (parser,
11433                              /*typename_keyword_p=*/true,
11434                              /*template_keyword_p=*/false,
11435                              none_type,
11436                              /*check_dependency_p=*/true,
11437                              /*class_head_p=*/false,
11438                              /*is_declaration=*/true);
11439   /* If we found one, we're done.  */
11440   if (cp_parser_parse_definitely (parser))
11441     return id;
11442   /* Otherwise, look for an ordinary identifier.  */
11443   return cp_parser_identifier (parser);
11444 }
11445
11446 /* Overloading [gram.over] */
11447
11448 /* Parse an operator-function-id.
11449
11450    operator-function-id:
11451      operator operator
11452
11453    Returns an IDENTIFIER_NODE for the operator which is a
11454    human-readable spelling of the identifier, e.g., `operator +'.  */
11455
11456 static tree
11457 cp_parser_operator_function_id (cp_parser* parser)
11458 {
11459   /* Look for the `operator' keyword.  */
11460   if (!cp_parser_require_keyword (parser, RID_OPERATOR, RT_OPERATOR))
11461     return error_mark_node;
11462   /* And then the name of the operator itself.  */
11463   return cp_parser_operator (parser);
11464 }
11465
11466 /* Return an identifier node for a user-defined literal operator.
11467    The suffix identifier is chained to the operator name identifier.  */
11468
11469 static tree
11470 cp_literal_operator_id (const char* name)
11471 {
11472   tree identifier;
11473   char *buffer = XNEWVEC (char, strlen (UDLIT_OP_ANSI_PREFIX)
11474                               + strlen (name) + 10);
11475   sprintf (buffer, UDLIT_OP_ANSI_FORMAT, name);
11476   identifier = get_identifier (buffer);
11477   /*IDENTIFIER_UDLIT_OPNAME_P (identifier) = 1; If we get a flag someday. */
11478
11479   return identifier;
11480 }
11481
11482 /* Parse an operator.
11483
11484    operator:
11485      new delete new[] delete[] + - * / % ^ & | ~ ! = < >
11486      += -= *= /= %= ^= &= |= << >> >>= <<= == != <= >= &&
11487      || ++ -- , ->* -> () []
11488
11489    GNU Extensions:
11490
11491    operator:
11492      <? >? <?= >?=
11493
11494    Returns an IDENTIFIER_NODE for the operator which is a
11495    human-readable spelling of the identifier, e.g., `operator +'.  */
11496
11497 static tree
11498 cp_parser_operator (cp_parser* parser)
11499 {
11500   tree id = NULL_TREE;
11501   cp_token *token;
11502
11503   /* Peek at the next token.  */
11504   token = cp_lexer_peek_token (parser->lexer);
11505   /* Figure out which operator we have.  */
11506   switch (token->type)
11507     {
11508     case CPP_KEYWORD:
11509       {
11510         enum tree_code op;
11511
11512         /* The keyword should be either `new' or `delete'.  */
11513         if (token->keyword == RID_NEW)
11514           op = NEW_EXPR;
11515         else if (token->keyword == RID_DELETE)
11516           op = DELETE_EXPR;
11517         else
11518           break;
11519
11520         /* Consume the `new' or `delete' token.  */
11521         cp_lexer_consume_token (parser->lexer);
11522
11523         /* Peek at the next token.  */
11524         token = cp_lexer_peek_token (parser->lexer);
11525         /* If it's a `[' token then this is the array variant of the
11526            operator.  */
11527         if (token->type == CPP_OPEN_SQUARE)
11528           {
11529             /* Consume the `[' token.  */
11530             cp_lexer_consume_token (parser->lexer);
11531             /* Look for the `]' token.  */
11532             cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11533             id = ansi_opname (op == NEW_EXPR
11534                               ? VEC_NEW_EXPR : VEC_DELETE_EXPR);
11535           }
11536         /* Otherwise, we have the non-array variant.  */
11537         else
11538           id = ansi_opname (op);
11539
11540         return id;
11541       }
11542
11543     case CPP_PLUS:
11544       id = ansi_opname (PLUS_EXPR);
11545       break;
11546
11547     case CPP_MINUS:
11548       id = ansi_opname (MINUS_EXPR);
11549       break;
11550
11551     case CPP_MULT:
11552       id = ansi_opname (MULT_EXPR);
11553       break;
11554
11555     case CPP_DIV:
11556       id = ansi_opname (TRUNC_DIV_EXPR);
11557       break;
11558
11559     case CPP_MOD:
11560       id = ansi_opname (TRUNC_MOD_EXPR);
11561       break;
11562
11563     case CPP_XOR:
11564       id = ansi_opname (BIT_XOR_EXPR);
11565       break;
11566
11567     case CPP_AND:
11568       id = ansi_opname (BIT_AND_EXPR);
11569       break;
11570
11571     case CPP_OR:
11572       id = ansi_opname (BIT_IOR_EXPR);
11573       break;
11574
11575     case CPP_COMPL:
11576       id = ansi_opname (BIT_NOT_EXPR);
11577       break;
11578
11579     case CPP_NOT:
11580       id = ansi_opname (TRUTH_NOT_EXPR);
11581       break;
11582
11583     case CPP_EQ:
11584       id = ansi_assopname (NOP_EXPR);
11585       break;
11586
11587     case CPP_LESS:
11588       id = ansi_opname (LT_EXPR);
11589       break;
11590
11591     case CPP_GREATER:
11592       id = ansi_opname (GT_EXPR);
11593       break;
11594
11595     case CPP_PLUS_EQ:
11596       id = ansi_assopname (PLUS_EXPR);
11597       break;
11598
11599     case CPP_MINUS_EQ:
11600       id = ansi_assopname (MINUS_EXPR);
11601       break;
11602
11603     case CPP_MULT_EQ:
11604       id = ansi_assopname (MULT_EXPR);
11605       break;
11606
11607     case CPP_DIV_EQ:
11608       id = ansi_assopname (TRUNC_DIV_EXPR);
11609       break;
11610
11611     case CPP_MOD_EQ:
11612       id = ansi_assopname (TRUNC_MOD_EXPR);
11613       break;
11614
11615     case CPP_XOR_EQ:
11616       id = ansi_assopname (BIT_XOR_EXPR);
11617       break;
11618
11619     case CPP_AND_EQ:
11620       id = ansi_assopname (BIT_AND_EXPR);
11621       break;
11622
11623     case CPP_OR_EQ:
11624       id = ansi_assopname (BIT_IOR_EXPR);
11625       break;
11626
11627     case CPP_LSHIFT:
11628       id = ansi_opname (LSHIFT_EXPR);
11629       break;
11630
11631     case CPP_RSHIFT:
11632       id = ansi_opname (RSHIFT_EXPR);
11633       break;
11634
11635     case CPP_LSHIFT_EQ:
11636       id = ansi_assopname (LSHIFT_EXPR);
11637       break;
11638
11639     case CPP_RSHIFT_EQ:
11640       id = ansi_assopname (RSHIFT_EXPR);
11641       break;
11642
11643     case CPP_EQ_EQ:
11644       id = ansi_opname (EQ_EXPR);
11645       break;
11646
11647     case CPP_NOT_EQ:
11648       id = ansi_opname (NE_EXPR);
11649       break;
11650
11651     case CPP_LESS_EQ:
11652       id = ansi_opname (LE_EXPR);
11653       break;
11654
11655     case CPP_GREATER_EQ:
11656       id = ansi_opname (GE_EXPR);
11657       break;
11658
11659     case CPP_AND_AND:
11660       id = ansi_opname (TRUTH_ANDIF_EXPR);
11661       break;
11662
11663     case CPP_OR_OR:
11664       id = ansi_opname (TRUTH_ORIF_EXPR);
11665       break;
11666
11667     case CPP_PLUS_PLUS:
11668       id = ansi_opname (POSTINCREMENT_EXPR);
11669       break;
11670
11671     case CPP_MINUS_MINUS:
11672       id = ansi_opname (PREDECREMENT_EXPR);
11673       break;
11674
11675     case CPP_COMMA:
11676       id = ansi_opname (COMPOUND_EXPR);
11677       break;
11678
11679     case CPP_DEREF_STAR:
11680       id = ansi_opname (MEMBER_REF);
11681       break;
11682
11683     case CPP_DEREF:
11684       id = ansi_opname (COMPONENT_REF);
11685       break;
11686
11687     case CPP_OPEN_PAREN:
11688       /* Consume the `('.  */
11689       cp_lexer_consume_token (parser->lexer);
11690       /* Look for the matching `)'.  */
11691       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
11692       return ansi_opname (CALL_EXPR);
11693
11694     case CPP_OPEN_SQUARE:
11695       /* Consume the `['.  */
11696       cp_lexer_consume_token (parser->lexer);
11697       /* Look for the matching `]'.  */
11698       cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
11699       return ansi_opname (ARRAY_REF);
11700
11701     case CPP_STRING:
11702       if (cxx_dialect == cxx98)
11703         maybe_warn_cpp0x (CPP0X_USER_DEFINED_LITERALS);
11704       if (TREE_STRING_LENGTH (token->u.value) > 2)
11705         {
11706           error ("expected empty string after %<operator%> keyword");
11707           return error_mark_node;
11708         }
11709       /* Consume the string.  */
11710       cp_lexer_consume_token (parser->lexer);
11711       /* Look for the suffix identifier.  */
11712       token = cp_lexer_peek_token (parser->lexer);
11713       if (token->type == CPP_NAME)
11714         {
11715           id = cp_parser_identifier (parser);
11716           if (id != error_mark_node)
11717             {
11718               const char *name = IDENTIFIER_POINTER (id);
11719               return cp_literal_operator_id (name);
11720             }
11721         }
11722       else
11723         {
11724           error ("expected suffix identifier");
11725           return error_mark_node;
11726         }
11727
11728     case CPP_STRING_USERDEF:
11729       error ("missing space between %<\"\"%> and suffix identifier");
11730       return error_mark_node;
11731
11732     default:
11733       /* Anything else is an error.  */
11734       break;
11735     }
11736
11737   /* If we have selected an identifier, we need to consume the
11738      operator token.  */
11739   if (id)
11740     cp_lexer_consume_token (parser->lexer);
11741   /* Otherwise, no valid operator name was present.  */
11742   else
11743     {
11744       cp_parser_error (parser, "expected operator");
11745       id = error_mark_node;
11746     }
11747
11748   return id;
11749 }
11750
11751 /* Parse a template-declaration.
11752
11753    template-declaration:
11754      export [opt] template < template-parameter-list > declaration
11755
11756    If MEMBER_P is TRUE, this template-declaration occurs within a
11757    class-specifier.
11758
11759    The grammar rule given by the standard isn't correct.  What
11760    is really meant is:
11761
11762    template-declaration:
11763      export [opt] template-parameter-list-seq
11764        decl-specifier-seq [opt] init-declarator [opt] ;
11765      export [opt] template-parameter-list-seq
11766        function-definition
11767
11768    template-parameter-list-seq:
11769      template-parameter-list-seq [opt]
11770      template < template-parameter-list >  */
11771
11772 static void
11773 cp_parser_template_declaration (cp_parser* parser, bool member_p)
11774 {
11775   /* Check for `export'.  */
11776   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXPORT))
11777     {
11778       /* Consume the `export' token.  */
11779       cp_lexer_consume_token (parser->lexer);
11780       /* Warn that we do not support `export'.  */
11781       warning (0, "keyword %<export%> not implemented, and will be ignored");
11782     }
11783
11784   cp_parser_template_declaration_after_export (parser, member_p);
11785 }
11786
11787 /* Parse a template-parameter-list.
11788
11789    template-parameter-list:
11790      template-parameter
11791      template-parameter-list , template-parameter
11792
11793    Returns a TREE_LIST.  Each node represents a template parameter.
11794    The nodes are connected via their TREE_CHAINs.  */
11795
11796 static tree
11797 cp_parser_template_parameter_list (cp_parser* parser)
11798 {
11799   tree parameter_list = NULL_TREE;
11800
11801   begin_template_parm_list ();
11802
11803   /* The loop below parses the template parms.  We first need to know
11804      the total number of template parms to be able to compute proper
11805      canonical types of each dependent type. So after the loop, when
11806      we know the total number of template parms,
11807      end_template_parm_list computes the proper canonical types and
11808      fixes up the dependent types accordingly.  */
11809   while (true)
11810     {
11811       tree parameter;
11812       bool is_non_type;
11813       bool is_parameter_pack;
11814       location_t parm_loc;
11815
11816       /* Parse the template-parameter.  */
11817       parm_loc = cp_lexer_peek_token (parser->lexer)->location;
11818       parameter = cp_parser_template_parameter (parser, 
11819                                                 &is_non_type,
11820                                                 &is_parameter_pack);
11821       /* Add it to the list.  */
11822       if (parameter != error_mark_node)
11823         parameter_list = process_template_parm (parameter_list,
11824                                                 parm_loc,
11825                                                 parameter,
11826                                                 is_non_type,
11827                                                 is_parameter_pack,
11828                                                 0);
11829       else
11830        {
11831          tree err_parm = build_tree_list (parameter, parameter);
11832          parameter_list = chainon (parameter_list, err_parm);
11833        }
11834
11835       /* If the next token is not a `,', we're done.  */
11836       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
11837         break;
11838       /* Otherwise, consume the `,' token.  */
11839       cp_lexer_consume_token (parser->lexer);
11840     }
11841
11842   return end_template_parm_list (parameter_list);
11843 }
11844
11845 /* Parse a template-parameter.
11846
11847    template-parameter:
11848      type-parameter
11849      parameter-declaration
11850
11851    If all goes well, returns a TREE_LIST.  The TREE_VALUE represents
11852    the parameter.  The TREE_PURPOSE is the default value, if any.
11853    Returns ERROR_MARK_NODE on failure.  *IS_NON_TYPE is set to true
11854    iff this parameter is a non-type parameter.  *IS_PARAMETER_PACK is
11855    set to true iff this parameter is a parameter pack. */
11856
11857 static tree
11858 cp_parser_template_parameter (cp_parser* parser, bool *is_non_type,
11859                               bool *is_parameter_pack)
11860 {
11861   cp_token *token;
11862   cp_parameter_declarator *parameter_declarator;
11863   cp_declarator *id_declarator;
11864   tree parm;
11865
11866   /* Assume it is a type parameter or a template parameter.  */
11867   *is_non_type = false;
11868   /* Assume it not a parameter pack. */
11869   *is_parameter_pack = false;
11870   /* Peek at the next token.  */
11871   token = cp_lexer_peek_token (parser->lexer);
11872   /* If it is `class' or `template', we have a type-parameter.  */
11873   if (token->keyword == RID_TEMPLATE)
11874     return cp_parser_type_parameter (parser, is_parameter_pack);
11875   /* If it is `class' or `typename' we do not know yet whether it is a
11876      type parameter or a non-type parameter.  Consider:
11877
11878        template <typename T, typename T::X X> ...
11879
11880      or:
11881
11882        template <class C, class D*> ...
11883
11884      Here, the first parameter is a type parameter, and the second is
11885      a non-type parameter.  We can tell by looking at the token after
11886      the identifier -- if it is a `,', `=', or `>' then we have a type
11887      parameter.  */
11888   if (token->keyword == RID_TYPENAME || token->keyword == RID_CLASS)
11889     {
11890       /* Peek at the token after `class' or `typename'.  */
11891       token = cp_lexer_peek_nth_token (parser->lexer, 2);
11892       /* If it's an ellipsis, we have a template type parameter
11893          pack. */
11894       if (token->type == CPP_ELLIPSIS)
11895         return cp_parser_type_parameter (parser, is_parameter_pack);
11896       /* If it's an identifier, skip it.  */
11897       if (token->type == CPP_NAME)
11898         token = cp_lexer_peek_nth_token (parser->lexer, 3);
11899       /* Now, see if the token looks like the end of a template
11900          parameter.  */
11901       if (token->type == CPP_COMMA
11902           || token->type == CPP_EQ
11903           || token->type == CPP_GREATER)
11904         return cp_parser_type_parameter (parser, is_parameter_pack);
11905     }
11906
11907   /* Otherwise, it is a non-type parameter.
11908
11909      [temp.param]
11910
11911      When parsing a default template-argument for a non-type
11912      template-parameter, the first non-nested `>' is taken as the end
11913      of the template parameter-list rather than a greater-than
11914      operator.  */
11915   *is_non_type = true;
11916   parameter_declarator
11917      = cp_parser_parameter_declaration (parser, /*template_parm_p=*/true,
11918                                         /*parenthesized_p=*/NULL);
11919
11920   /* If the parameter declaration is marked as a parameter pack, set
11921      *IS_PARAMETER_PACK to notify the caller. Also, unmark the
11922      declarator's PACK_EXPANSION_P, otherwise we'll get errors from
11923      grokdeclarator. */
11924   if (parameter_declarator
11925       && parameter_declarator->declarator
11926       && parameter_declarator->declarator->parameter_pack_p)
11927     {
11928       *is_parameter_pack = true;
11929       parameter_declarator->declarator->parameter_pack_p = false;
11930     }
11931
11932   /* If the next token is an ellipsis, and we don't already have it
11933      marked as a parameter pack, then we have a parameter pack (that
11934      has no declarator).  */
11935   if (!*is_parameter_pack
11936       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
11937       && declarator_can_be_parameter_pack (parameter_declarator->declarator))
11938     {
11939       /* Consume the `...'.  */
11940       cp_lexer_consume_token (parser->lexer);
11941       maybe_warn_variadic_templates ();
11942       
11943       *is_parameter_pack = true;
11944     }
11945   /* We might end up with a pack expansion as the type of the non-type
11946      template parameter, in which case this is a non-type template
11947      parameter pack.  */
11948   else if (parameter_declarator
11949            && parameter_declarator->decl_specifiers.type
11950            && PACK_EXPANSION_P (parameter_declarator->decl_specifiers.type))
11951     {
11952       *is_parameter_pack = true;
11953       parameter_declarator->decl_specifiers.type = 
11954         PACK_EXPANSION_PATTERN (parameter_declarator->decl_specifiers.type);
11955     }
11956
11957   if (*is_parameter_pack && cp_lexer_next_token_is (parser->lexer, CPP_EQ))
11958     {
11959       /* Parameter packs cannot have default arguments.  However, a
11960          user may try to do so, so we'll parse them and give an
11961          appropriate diagnostic here.  */
11962
11963       cp_token *start_token = cp_lexer_peek_token (parser->lexer);
11964       
11965       /* Find the name of the parameter pack.  */     
11966       id_declarator = parameter_declarator->declarator;
11967       while (id_declarator && id_declarator->kind != cdk_id)
11968         id_declarator = id_declarator->declarator;
11969       
11970       if (id_declarator && id_declarator->kind == cdk_id)
11971         error_at (start_token->location,
11972                   "template parameter pack %qD cannot have a default argument",
11973                   id_declarator->u.id.unqualified_name);
11974       else
11975         error_at (start_token->location,
11976                   "template parameter pack cannot have a default argument");
11977       
11978       /* Parse the default argument, but throw away the result.  */
11979       cp_parser_default_argument (parser, /*template_parm_p=*/true);
11980     }
11981
11982   parm = grokdeclarator (parameter_declarator->declarator,
11983                          &parameter_declarator->decl_specifiers,
11984                          TPARM, /*initialized=*/0,
11985                          /*attrlist=*/NULL);
11986   if (parm == error_mark_node)
11987     return error_mark_node;
11988
11989   return build_tree_list (parameter_declarator->default_argument, parm);
11990 }
11991
11992 /* Parse a type-parameter.
11993
11994    type-parameter:
11995      class identifier [opt]
11996      class identifier [opt] = type-id
11997      typename identifier [opt]
11998      typename identifier [opt] = type-id
11999      template < template-parameter-list > class identifier [opt]
12000      template < template-parameter-list > class identifier [opt]
12001        = id-expression
12002
12003    GNU Extension (variadic templates):
12004
12005    type-parameter:
12006      class ... identifier [opt]
12007      typename ... identifier [opt]
12008
12009    Returns a TREE_LIST.  The TREE_VALUE is itself a TREE_LIST.  The
12010    TREE_PURPOSE is the default-argument, if any.  The TREE_VALUE is
12011    the declaration of the parameter.
12012
12013    Sets *IS_PARAMETER_PACK if this is a template parameter pack. */
12014
12015 static tree
12016 cp_parser_type_parameter (cp_parser* parser, bool *is_parameter_pack)
12017 {
12018   cp_token *token;
12019   tree parameter;
12020
12021   /* Look for a keyword to tell us what kind of parameter this is.  */
12022   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_TYPENAME_TEMPLATE);
12023   if (!token)
12024     return error_mark_node;
12025
12026   switch (token->keyword)
12027     {
12028     case RID_CLASS:
12029     case RID_TYPENAME:
12030       {
12031         tree identifier;
12032         tree default_argument;
12033
12034         /* If the next token is an ellipsis, we have a template
12035            argument pack. */
12036         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12037           {
12038             /* Consume the `...' token. */
12039             cp_lexer_consume_token (parser->lexer);
12040             maybe_warn_variadic_templates ();
12041
12042             *is_parameter_pack = true;
12043           }
12044
12045         /* If the next token is an identifier, then it names the
12046            parameter.  */
12047         if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12048           identifier = cp_parser_identifier (parser);
12049         else
12050           identifier = NULL_TREE;
12051
12052         /* Create the parameter.  */
12053         parameter = finish_template_type_parm (class_type_node, identifier);
12054
12055         /* If the next token is an `=', we have a default argument.  */
12056         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12057           {
12058             /* Consume the `=' token.  */
12059             cp_lexer_consume_token (parser->lexer);
12060             /* Parse the default-argument.  */
12061             push_deferring_access_checks (dk_no_deferred);
12062             default_argument = cp_parser_type_id (parser);
12063
12064             /* Template parameter packs cannot have default
12065                arguments. */
12066             if (*is_parameter_pack)
12067               {
12068                 if (identifier)
12069                   error_at (token->location,
12070                             "template parameter pack %qD cannot have a "
12071                             "default argument", identifier);
12072                 else
12073                   error_at (token->location,
12074                             "template parameter packs cannot have "
12075                             "default arguments");
12076                 default_argument = NULL_TREE;
12077               }
12078             pop_deferring_access_checks ();
12079           }
12080         else
12081           default_argument = NULL_TREE;
12082
12083         /* Create the combined representation of the parameter and the
12084            default argument.  */
12085         parameter = build_tree_list (default_argument, parameter);
12086       }
12087       break;
12088
12089     case RID_TEMPLATE:
12090       {
12091         tree identifier;
12092         tree default_argument;
12093
12094         /* Look for the `<'.  */
12095         cp_parser_require (parser, CPP_LESS, RT_LESS);
12096         /* Parse the template-parameter-list.  */
12097         cp_parser_template_parameter_list (parser);
12098         /* Look for the `>'.  */
12099         cp_parser_require (parser, CPP_GREATER, RT_GREATER);
12100         /* Look for the `class' keyword.  */
12101         cp_parser_require_keyword (parser, RID_CLASS, RT_CLASS);
12102         /* If the next token is an ellipsis, we have a template
12103            argument pack. */
12104         if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12105           {
12106             /* Consume the `...' token. */
12107             cp_lexer_consume_token (parser->lexer);
12108             maybe_warn_variadic_templates ();
12109
12110             *is_parameter_pack = true;
12111           }
12112         /* If the next token is an `=', then there is a
12113            default-argument.  If the next token is a `>', we are at
12114            the end of the parameter-list.  If the next token is a `,',
12115            then we are at the end of this parameter.  */
12116         if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ)
12117             && cp_lexer_next_token_is_not (parser->lexer, CPP_GREATER)
12118             && cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
12119           {
12120             identifier = cp_parser_identifier (parser);
12121             /* Treat invalid names as if the parameter were nameless.  */
12122             if (identifier == error_mark_node)
12123               identifier = NULL_TREE;
12124           }
12125         else
12126           identifier = NULL_TREE;
12127
12128         /* Create the template parameter.  */
12129         parameter = finish_template_template_parm (class_type_node,
12130                                                    identifier);
12131
12132         /* If the next token is an `=', then there is a
12133            default-argument.  */
12134         if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
12135           {
12136             bool is_template;
12137
12138             /* Consume the `='.  */
12139             cp_lexer_consume_token (parser->lexer);
12140             /* Parse the id-expression.  */
12141             push_deferring_access_checks (dk_no_deferred);
12142             /* save token before parsing the id-expression, for error
12143                reporting */
12144             token = cp_lexer_peek_token (parser->lexer);
12145             default_argument
12146               = cp_parser_id_expression (parser,
12147                                          /*template_keyword_p=*/false,
12148                                          /*check_dependency_p=*/true,
12149                                          /*template_p=*/&is_template,
12150                                          /*declarator_p=*/false,
12151                                          /*optional_p=*/false);
12152             if (TREE_CODE (default_argument) == TYPE_DECL)
12153               /* If the id-expression was a template-id that refers to
12154                  a template-class, we already have the declaration here,
12155                  so no further lookup is needed.  */
12156                  ;
12157             else
12158               /* Look up the name.  */
12159               default_argument
12160                 = cp_parser_lookup_name (parser, default_argument,
12161                                          none_type,
12162                                          /*is_template=*/is_template,
12163                                          /*is_namespace=*/false,
12164                                          /*check_dependency=*/true,
12165                                          /*ambiguous_decls=*/NULL,
12166                                          token->location);
12167             /* See if the default argument is valid.  */
12168             default_argument
12169               = check_template_template_default_arg (default_argument);
12170
12171             /* Template parameter packs cannot have default
12172                arguments. */
12173             if (*is_parameter_pack)
12174               {
12175                 if (identifier)
12176                   error_at (token->location,
12177                             "template parameter pack %qD cannot "
12178                             "have a default argument",
12179                             identifier);
12180                 else
12181                   error_at (token->location, "template parameter packs cannot "
12182                             "have default arguments");
12183                 default_argument = NULL_TREE;
12184               }
12185             pop_deferring_access_checks ();
12186           }
12187         else
12188           default_argument = NULL_TREE;
12189
12190         /* Create the combined representation of the parameter and the
12191            default argument.  */
12192         parameter = build_tree_list (default_argument, parameter);
12193       }
12194       break;
12195
12196     default:
12197       gcc_unreachable ();
12198       break;
12199     }
12200
12201   return parameter;
12202 }
12203
12204 /* Parse a template-id.
12205
12206    template-id:
12207      template-name < template-argument-list [opt] >
12208
12209    If TEMPLATE_KEYWORD_P is TRUE, then we have just seen the
12210    `template' keyword.  In this case, a TEMPLATE_ID_EXPR will be
12211    returned.  Otherwise, if the template-name names a function, or set
12212    of functions, returns a TEMPLATE_ID_EXPR.  If the template-name
12213    names a class, returns a TYPE_DECL for the specialization.
12214
12215    If CHECK_DEPENDENCY_P is FALSE, names are looked up in
12216    uninstantiated templates.  */
12217
12218 static tree
12219 cp_parser_template_id (cp_parser *parser,
12220                        bool template_keyword_p,
12221                        bool check_dependency_p,
12222                        bool is_declaration)
12223 {
12224   int i;
12225   tree templ;
12226   tree arguments;
12227   tree template_id;
12228   cp_token_position start_of_id = 0;
12229   deferred_access_check *chk;
12230   VEC (deferred_access_check,gc) *access_check;
12231   cp_token *next_token = NULL, *next_token_2 = NULL;
12232   bool is_identifier;
12233
12234   /* If the next token corresponds to a template-id, there is no need
12235      to reparse it.  */
12236   next_token = cp_lexer_peek_token (parser->lexer);
12237   if (next_token->type == CPP_TEMPLATE_ID)
12238     {
12239       struct tree_check *check_value;
12240
12241       /* Get the stored value.  */
12242       check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
12243       /* Perform any access checks that were deferred.  */
12244       access_check = check_value->checks;
12245       if (access_check)
12246         {
12247           FOR_EACH_VEC_ELT (deferred_access_check, access_check, i, chk)
12248             perform_or_defer_access_check (chk->binfo,
12249                                            chk->decl,
12250                                            chk->diag_decl);
12251         }
12252       /* Return the stored value.  */
12253       return check_value->value;
12254     }
12255
12256   /* Avoid performing name lookup if there is no possibility of
12257      finding a template-id.  */
12258   if ((next_token->type != CPP_NAME && next_token->keyword != RID_OPERATOR)
12259       || (next_token->type == CPP_NAME
12260           && !cp_parser_nth_token_starts_template_argument_list_p
12261                (parser, 2)))
12262     {
12263       cp_parser_error (parser, "expected template-id");
12264       return error_mark_node;
12265     }
12266
12267   /* Remember where the template-id starts.  */
12268   if (cp_parser_uncommitted_to_tentative_parse_p (parser))
12269     start_of_id = cp_lexer_token_position (parser->lexer, false);
12270
12271   push_deferring_access_checks (dk_deferred);
12272
12273   /* Parse the template-name.  */
12274   is_identifier = false;
12275   templ = cp_parser_template_name (parser, template_keyword_p,
12276                                    check_dependency_p,
12277                                    is_declaration,
12278                                    &is_identifier);
12279   if (templ == error_mark_node || is_identifier)
12280     {
12281       pop_deferring_access_checks ();
12282       return templ;
12283     }
12284
12285   /* If we find the sequence `[:' after a template-name, it's probably
12286      a digraph-typo for `< ::'. Substitute the tokens and check if we can
12287      parse correctly the argument list.  */
12288   next_token = cp_lexer_peek_token (parser->lexer);
12289   next_token_2 = cp_lexer_peek_nth_token (parser->lexer, 2);
12290   if (next_token->type == CPP_OPEN_SQUARE
12291       && next_token->flags & DIGRAPH
12292       && next_token_2->type == CPP_COLON
12293       && !(next_token_2->flags & PREV_WHITE))
12294     {
12295       cp_parser_parse_tentatively (parser);
12296       /* Change `:' into `::'.  */
12297       next_token_2->type = CPP_SCOPE;
12298       /* Consume the first token (CPP_OPEN_SQUARE - which we pretend it is
12299          CPP_LESS.  */
12300       cp_lexer_consume_token (parser->lexer);
12301
12302       /* Parse the arguments.  */
12303       arguments = cp_parser_enclosed_template_argument_list (parser);
12304       if (!cp_parser_parse_definitely (parser))
12305         {
12306           /* If we couldn't parse an argument list, then we revert our changes
12307              and return simply an error. Maybe this is not a template-id
12308              after all.  */
12309           next_token_2->type = CPP_COLON;
12310           cp_parser_error (parser, "expected %<<%>");
12311           pop_deferring_access_checks ();
12312           return error_mark_node;
12313         }
12314       /* Otherwise, emit an error about the invalid digraph, but continue
12315          parsing because we got our argument list.  */
12316       if (permerror (next_token->location,
12317                      "%<<::%> cannot begin a template-argument list"))
12318         {
12319           static bool hint = false;
12320           inform (next_token->location,
12321                   "%<<:%> is an alternate spelling for %<[%>."
12322                   " Insert whitespace between %<<%> and %<::%>");
12323           if (!hint && !flag_permissive)
12324             {
12325               inform (next_token->location, "(if you use %<-fpermissive%>"
12326                       " G++ will accept your code)");
12327               hint = true;
12328             }
12329         }
12330     }
12331   else
12332     {
12333       /* Look for the `<' that starts the template-argument-list.  */
12334       if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
12335         {
12336           pop_deferring_access_checks ();
12337           return error_mark_node;
12338         }
12339       /* Parse the arguments.  */
12340       arguments = cp_parser_enclosed_template_argument_list (parser);
12341     }
12342
12343   /* Build a representation of the specialization.  */
12344   if (TREE_CODE (templ) == IDENTIFIER_NODE)
12345     template_id = build_min_nt (TEMPLATE_ID_EXPR, templ, arguments);
12346   else if (DECL_CLASS_TEMPLATE_P (templ)
12347            || DECL_TEMPLATE_TEMPLATE_PARM_P (templ))
12348     {
12349       bool entering_scope;
12350       /* In "template <typename T> ... A<T>::", A<T> is the abstract A
12351          template (rather than some instantiation thereof) only if
12352          is not nested within some other construct.  For example, in
12353          "template <typename T> void f(T) { A<T>::", A<T> is just an
12354          instantiation of A.  */
12355       entering_scope = (template_parm_scope_p ()
12356                         && cp_lexer_next_token_is (parser->lexer,
12357                                                    CPP_SCOPE));
12358       template_id
12359         = finish_template_type (templ, arguments, entering_scope);
12360     }
12361   else
12362     {
12363       /* If it's not a class-template or a template-template, it should be
12364          a function-template.  */
12365       gcc_assert ((DECL_FUNCTION_TEMPLATE_P (templ)
12366                    || TREE_CODE (templ) == OVERLOAD
12367                    || BASELINK_P (templ)));
12368
12369       template_id = lookup_template_function (templ, arguments);
12370     }
12371
12372   /* If parsing tentatively, replace the sequence of tokens that makes
12373      up the template-id with a CPP_TEMPLATE_ID token.  That way,
12374      should we re-parse the token stream, we will not have to repeat
12375      the effort required to do the parse, nor will we issue duplicate
12376      error messages about problems during instantiation of the
12377      template.  */
12378   if (start_of_id)
12379     {
12380       cp_token *token = cp_lexer_token_at (parser->lexer, start_of_id);
12381
12382       /* Reset the contents of the START_OF_ID token.  */
12383       token->type = CPP_TEMPLATE_ID;
12384       /* Retrieve any deferred checks.  Do not pop this access checks yet
12385          so the memory will not be reclaimed during token replacing below.  */
12386       token->u.tree_check_value = ggc_alloc_cleared_tree_check ();
12387       token->u.tree_check_value->value = template_id;
12388       token->u.tree_check_value->checks = get_deferred_access_checks ();
12389       token->keyword = RID_MAX;
12390
12391       /* Purge all subsequent tokens.  */
12392       cp_lexer_purge_tokens_after (parser->lexer, start_of_id);
12393
12394       /* ??? Can we actually assume that, if template_id ==
12395          error_mark_node, we will have issued a diagnostic to the
12396          user, as opposed to simply marking the tentative parse as
12397          failed?  */
12398       if (cp_parser_error_occurred (parser) && template_id != error_mark_node)
12399         error_at (token->location, "parse error in template argument list");
12400     }
12401
12402   pop_deferring_access_checks ();
12403   return template_id;
12404 }
12405
12406 /* Parse a template-name.
12407
12408    template-name:
12409      identifier
12410
12411    The standard should actually say:
12412
12413    template-name:
12414      identifier
12415      operator-function-id
12416
12417    A defect report has been filed about this issue.
12418
12419    A conversion-function-id cannot be a template name because they cannot
12420    be part of a template-id. In fact, looking at this code:
12421
12422    a.operator K<int>()
12423
12424    the conversion-function-id is "operator K<int>", and K<int> is a type-id.
12425    It is impossible to call a templated conversion-function-id with an
12426    explicit argument list, since the only allowed template parameter is
12427    the type to which it is converting.
12428
12429    If TEMPLATE_KEYWORD_P is true, then we have just seen the
12430    `template' keyword, in a construction like:
12431
12432      T::template f<3>()
12433
12434    In that case `f' is taken to be a template-name, even though there
12435    is no way of knowing for sure.
12436
12437    Returns the TEMPLATE_DECL for the template, or an OVERLOAD if the
12438    name refers to a set of overloaded functions, at least one of which
12439    is a template, or an IDENTIFIER_NODE with the name of the template,
12440    if TEMPLATE_KEYWORD_P is true.  If CHECK_DEPENDENCY_P is FALSE,
12441    names are looked up inside uninstantiated templates.  */
12442
12443 static tree
12444 cp_parser_template_name (cp_parser* parser,
12445                          bool template_keyword_p,
12446                          bool check_dependency_p,
12447                          bool is_declaration,
12448                          bool *is_identifier)
12449 {
12450   tree identifier;
12451   tree decl;
12452   tree fns;
12453   cp_token *token = cp_lexer_peek_token (parser->lexer);
12454
12455   /* If the next token is `operator', then we have either an
12456      operator-function-id or a conversion-function-id.  */
12457   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_OPERATOR))
12458     {
12459       /* We don't know whether we're looking at an
12460          operator-function-id or a conversion-function-id.  */
12461       cp_parser_parse_tentatively (parser);
12462       /* Try an operator-function-id.  */
12463       identifier = cp_parser_operator_function_id (parser);
12464       /* If that didn't work, try a conversion-function-id.  */
12465       if (!cp_parser_parse_definitely (parser))
12466         {
12467           cp_parser_error (parser, "expected template-name");
12468           return error_mark_node;
12469         }
12470     }
12471   /* Look for the identifier.  */
12472   else
12473     identifier = cp_parser_identifier (parser);
12474
12475   /* If we didn't find an identifier, we don't have a template-id.  */
12476   if (identifier == error_mark_node)
12477     return error_mark_node;
12478
12479   /* If the name immediately followed the `template' keyword, then it
12480      is a template-name.  However, if the next token is not `<', then
12481      we do not treat it as a template-name, since it is not being used
12482      as part of a template-id.  This enables us to handle constructs
12483      like:
12484
12485        template <typename T> struct S { S(); };
12486        template <typename T> S<T>::S();
12487
12488      correctly.  We would treat `S' as a template -- if it were `S<T>'
12489      -- but we do not if there is no `<'.  */
12490
12491   if (processing_template_decl
12492       && cp_parser_nth_token_starts_template_argument_list_p (parser, 1))
12493     {
12494       /* In a declaration, in a dependent context, we pretend that the
12495          "template" keyword was present in order to improve error
12496          recovery.  For example, given:
12497
12498            template <typename T> void f(T::X<int>);
12499
12500          we want to treat "X<int>" as a template-id.  */
12501       if (is_declaration
12502           && !template_keyword_p
12503           && parser->scope && TYPE_P (parser->scope)
12504           && check_dependency_p
12505           && dependent_scope_p (parser->scope)
12506           /* Do not do this for dtors (or ctors), since they never
12507              need the template keyword before their name.  */
12508           && !constructor_name_p (identifier, parser->scope))
12509         {
12510           cp_token_position start = 0;
12511
12512           /* Explain what went wrong.  */
12513           error_at (token->location, "non-template %qD used as template",
12514                     identifier);
12515           inform (token->location, "use %<%T::template %D%> to indicate that it is a template",
12516                   parser->scope, identifier);
12517           /* If parsing tentatively, find the location of the "<" token.  */
12518           if (cp_parser_simulate_error (parser))
12519             start = cp_lexer_token_position (parser->lexer, true);
12520           /* Parse the template arguments so that we can issue error
12521              messages about them.  */
12522           cp_lexer_consume_token (parser->lexer);
12523           cp_parser_enclosed_template_argument_list (parser);
12524           /* Skip tokens until we find a good place from which to
12525              continue parsing.  */
12526           cp_parser_skip_to_closing_parenthesis (parser,
12527                                                  /*recovering=*/true,
12528                                                  /*or_comma=*/true,
12529                                                  /*consume_paren=*/false);
12530           /* If parsing tentatively, permanently remove the
12531              template argument list.  That will prevent duplicate
12532              error messages from being issued about the missing
12533              "template" keyword.  */
12534           if (start)
12535             cp_lexer_purge_tokens_after (parser->lexer, start);
12536           if (is_identifier)
12537             *is_identifier = true;
12538           return identifier;
12539         }
12540
12541       /* If the "template" keyword is present, then there is generally
12542          no point in doing name-lookup, so we just return IDENTIFIER.
12543          But, if the qualifying scope is non-dependent then we can
12544          (and must) do name-lookup normally.  */
12545       if (template_keyword_p
12546           && (!parser->scope
12547               || (TYPE_P (parser->scope)
12548                   && dependent_type_p (parser->scope))))
12549         return identifier;
12550     }
12551
12552   /* Look up the name.  */
12553   decl = cp_parser_lookup_name (parser, identifier,
12554                                 none_type,
12555                                 /*is_template=*/true,
12556                                 /*is_namespace=*/false,
12557                                 check_dependency_p,
12558                                 /*ambiguous_decls=*/NULL,
12559                                 token->location);
12560
12561   /* If DECL is a template, then the name was a template-name.  */
12562   if (TREE_CODE (decl) == TEMPLATE_DECL)
12563     ;
12564   else
12565     {
12566       tree fn = NULL_TREE;
12567
12568       /* The standard does not explicitly indicate whether a name that
12569          names a set of overloaded declarations, some of which are
12570          templates, is a template-name.  However, such a name should
12571          be a template-name; otherwise, there is no way to form a
12572          template-id for the overloaded templates.  */
12573       fns = BASELINK_P (decl) ? BASELINK_FUNCTIONS (decl) : decl;
12574       if (TREE_CODE (fns) == OVERLOAD)
12575         for (fn = fns; fn; fn = OVL_NEXT (fn))
12576           if (TREE_CODE (OVL_CURRENT (fn)) == TEMPLATE_DECL)
12577             break;
12578
12579       if (!fn)
12580         {
12581           /* The name does not name a template.  */
12582           cp_parser_error (parser, "expected template-name");
12583           return error_mark_node;
12584         }
12585     }
12586
12587   /* If DECL is dependent, and refers to a function, then just return
12588      its name; we will look it up again during template instantiation.  */
12589   if (DECL_FUNCTION_TEMPLATE_P (decl) || !DECL_P (decl))
12590     {
12591       tree scope = CP_DECL_CONTEXT (get_first_fn (decl));
12592       if (TYPE_P (scope) && dependent_type_p (scope))
12593         return identifier;
12594     }
12595
12596   return decl;
12597 }
12598
12599 /* Parse a template-argument-list.
12600
12601    template-argument-list:
12602      template-argument ... [opt]
12603      template-argument-list , template-argument ... [opt]
12604
12605    Returns a TREE_VEC containing the arguments.  */
12606
12607 static tree
12608 cp_parser_template_argument_list (cp_parser* parser)
12609 {
12610   tree fixed_args[10];
12611   unsigned n_args = 0;
12612   unsigned alloced = 10;
12613   tree *arg_ary = fixed_args;
12614   tree vec;
12615   bool saved_in_template_argument_list_p;
12616   bool saved_ice_p;
12617   bool saved_non_ice_p;
12618
12619   saved_in_template_argument_list_p = parser->in_template_argument_list_p;
12620   parser->in_template_argument_list_p = true;
12621   /* Even if the template-id appears in an integral
12622      constant-expression, the contents of the argument list do
12623      not.  */
12624   saved_ice_p = parser->integral_constant_expression_p;
12625   parser->integral_constant_expression_p = false;
12626   saved_non_ice_p = parser->non_integral_constant_expression_p;
12627   parser->non_integral_constant_expression_p = false;
12628
12629   /* Parse the arguments.  */
12630   do
12631     {
12632       tree argument;
12633
12634       if (n_args)
12635         /* Consume the comma.  */
12636         cp_lexer_consume_token (parser->lexer);
12637
12638       /* Parse the template-argument.  */
12639       argument = cp_parser_template_argument (parser);
12640
12641       /* If the next token is an ellipsis, we're expanding a template
12642          argument pack. */
12643       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
12644         {
12645           if (argument == error_mark_node)
12646             {
12647               cp_token *token = cp_lexer_peek_token (parser->lexer);
12648               error_at (token->location,
12649                         "expected parameter pack before %<...%>");
12650             }
12651           /* Consume the `...' token. */
12652           cp_lexer_consume_token (parser->lexer);
12653
12654           /* Make the argument into a TYPE_PACK_EXPANSION or
12655              EXPR_PACK_EXPANSION. */
12656           argument = make_pack_expansion (argument);
12657         }
12658
12659       if (n_args == alloced)
12660         {
12661           alloced *= 2;
12662
12663           if (arg_ary == fixed_args)
12664             {
12665               arg_ary = XNEWVEC (tree, alloced);
12666               memcpy (arg_ary, fixed_args, sizeof (tree) * n_args);
12667             }
12668           else
12669             arg_ary = XRESIZEVEC (tree, arg_ary, alloced);
12670         }
12671       arg_ary[n_args++] = argument;
12672     }
12673   while (cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
12674
12675   vec = make_tree_vec (n_args);
12676
12677   while (n_args--)
12678     TREE_VEC_ELT (vec, n_args) = arg_ary[n_args];
12679
12680   if (arg_ary != fixed_args)
12681     free (arg_ary);
12682   parser->non_integral_constant_expression_p = saved_non_ice_p;
12683   parser->integral_constant_expression_p = saved_ice_p;
12684   parser->in_template_argument_list_p = saved_in_template_argument_list_p;
12685 #ifdef ENABLE_CHECKING
12686   SET_NON_DEFAULT_TEMPLATE_ARGS_COUNT (vec, TREE_VEC_LENGTH (vec));
12687 #endif
12688   return vec;
12689 }
12690
12691 /* Parse a template-argument.
12692
12693    template-argument:
12694      assignment-expression
12695      type-id
12696      id-expression
12697
12698    The representation is that of an assignment-expression, type-id, or
12699    id-expression -- except that the qualified id-expression is
12700    evaluated, so that the value returned is either a DECL or an
12701    OVERLOAD.
12702
12703    Although the standard says "assignment-expression", it forbids
12704    throw-expressions or assignments in the template argument.
12705    Therefore, we use "conditional-expression" instead.  */
12706
12707 static tree
12708 cp_parser_template_argument (cp_parser* parser)
12709 {
12710   tree argument;
12711   bool template_p;
12712   bool address_p;
12713   bool maybe_type_id = false;
12714   cp_token *token = NULL, *argument_start_token = NULL;
12715   cp_id_kind idk;
12716
12717   /* There's really no way to know what we're looking at, so we just
12718      try each alternative in order.
12719
12720        [temp.arg]
12721
12722        In a template-argument, an ambiguity between a type-id and an
12723        expression is resolved to a type-id, regardless of the form of
12724        the corresponding template-parameter.
12725
12726      Therefore, we try a type-id first.  */
12727   cp_parser_parse_tentatively (parser);
12728   argument = cp_parser_template_type_arg (parser);
12729   /* If there was no error parsing the type-id but the next token is a
12730      '>>', our behavior depends on which dialect of C++ we're
12731      parsing. In C++98, we probably found a typo for '> >'. But there
12732      are type-id which are also valid expressions. For instance:
12733
12734      struct X { int operator >> (int); };
12735      template <int V> struct Foo {};
12736      Foo<X () >> 5> r;
12737
12738      Here 'X()' is a valid type-id of a function type, but the user just
12739      wanted to write the expression "X() >> 5". Thus, we remember that we
12740      found a valid type-id, but we still try to parse the argument as an
12741      expression to see what happens. 
12742
12743      In C++0x, the '>>' will be considered two separate '>'
12744      tokens.  */
12745   if (!cp_parser_error_occurred (parser)
12746       && cxx_dialect == cxx98
12747       && cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
12748     {
12749       maybe_type_id = true;
12750       cp_parser_abort_tentative_parse (parser);
12751     }
12752   else
12753     {
12754       /* If the next token isn't a `,' or a `>', then this argument wasn't
12755       really finished. This means that the argument is not a valid
12756       type-id.  */
12757       if (!cp_parser_next_token_ends_template_argument_p (parser))
12758         cp_parser_error (parser, "expected template-argument");
12759       /* If that worked, we're done.  */
12760       if (cp_parser_parse_definitely (parser))
12761         return argument;
12762     }
12763   /* We're still not sure what the argument will be.  */
12764   cp_parser_parse_tentatively (parser);
12765   /* Try a template.  */
12766   argument_start_token = cp_lexer_peek_token (parser->lexer);
12767   argument = cp_parser_id_expression (parser,
12768                                       /*template_keyword_p=*/false,
12769                                       /*check_dependency_p=*/true,
12770                                       &template_p,
12771                                       /*declarator_p=*/false,
12772                                       /*optional_p=*/false);
12773   /* If the next token isn't a `,' or a `>', then this argument wasn't
12774      really finished.  */
12775   if (!cp_parser_next_token_ends_template_argument_p (parser))
12776     cp_parser_error (parser, "expected template-argument");
12777   if (!cp_parser_error_occurred (parser))
12778     {
12779       /* Figure out what is being referred to.  If the id-expression
12780          was for a class template specialization, then we will have a
12781          TYPE_DECL at this point.  There is no need to do name lookup
12782          at this point in that case.  */
12783       if (TREE_CODE (argument) != TYPE_DECL)
12784         argument = cp_parser_lookup_name (parser, argument,
12785                                           none_type,
12786                                           /*is_template=*/template_p,
12787                                           /*is_namespace=*/false,
12788                                           /*check_dependency=*/true,
12789                                           /*ambiguous_decls=*/NULL,
12790                                           argument_start_token->location);
12791       if (TREE_CODE (argument) != TEMPLATE_DECL
12792           && TREE_CODE (argument) != UNBOUND_CLASS_TEMPLATE)
12793         cp_parser_error (parser, "expected template-name");
12794     }
12795   if (cp_parser_parse_definitely (parser))
12796     return argument;
12797   /* It must be a non-type argument.  There permitted cases are given
12798      in [temp.arg.nontype]:
12799
12800      -- an integral constant-expression of integral or enumeration
12801         type; or
12802
12803      -- the name of a non-type template-parameter; or
12804
12805      -- the name of an object or function with external linkage...
12806
12807      -- the address of an object or function with external linkage...
12808
12809      -- a pointer to member...  */
12810   /* Look for a non-type template parameter.  */
12811   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
12812     {
12813       cp_parser_parse_tentatively (parser);
12814       argument = cp_parser_primary_expression (parser,
12815                                                /*address_p=*/false,
12816                                                /*cast_p=*/false,
12817                                                /*template_arg_p=*/true,
12818                                                &idk);
12819       if (TREE_CODE (argument) != TEMPLATE_PARM_INDEX
12820           || !cp_parser_next_token_ends_template_argument_p (parser))
12821         cp_parser_simulate_error (parser);
12822       if (cp_parser_parse_definitely (parser))
12823         return argument;
12824     }
12825
12826   /* If the next token is "&", the argument must be the address of an
12827      object or function with external linkage.  */
12828   address_p = cp_lexer_next_token_is (parser->lexer, CPP_AND);
12829   if (address_p)
12830     cp_lexer_consume_token (parser->lexer);
12831   /* See if we might have an id-expression.  */
12832   token = cp_lexer_peek_token (parser->lexer);
12833   if (token->type == CPP_NAME
12834       || token->keyword == RID_OPERATOR
12835       || token->type == CPP_SCOPE
12836       || token->type == CPP_TEMPLATE_ID
12837       || token->type == CPP_NESTED_NAME_SPECIFIER)
12838     {
12839       cp_parser_parse_tentatively (parser);
12840       argument = cp_parser_primary_expression (parser,
12841                                                address_p,
12842                                                /*cast_p=*/false,
12843                                                /*template_arg_p=*/true,
12844                                                &idk);
12845       if (cp_parser_error_occurred (parser)
12846           || !cp_parser_next_token_ends_template_argument_p (parser))
12847         cp_parser_abort_tentative_parse (parser);
12848       else
12849         {
12850           tree probe;
12851
12852           if (TREE_CODE (argument) == INDIRECT_REF)
12853             {
12854               gcc_assert (REFERENCE_REF_P (argument));
12855               argument = TREE_OPERAND (argument, 0);
12856             }
12857
12858           /* If we're in a template, we represent a qualified-id referring
12859              to a static data member as a SCOPE_REF even if the scope isn't
12860              dependent so that we can check access control later.  */
12861           probe = argument;
12862           if (TREE_CODE (probe) == SCOPE_REF)
12863             probe = TREE_OPERAND (probe, 1);
12864           if (TREE_CODE (probe) == VAR_DECL)
12865             {
12866               /* A variable without external linkage might still be a
12867                  valid constant-expression, so no error is issued here
12868                  if the external-linkage check fails.  */
12869               if (!address_p && !DECL_EXTERNAL_LINKAGE_P (probe))
12870                 cp_parser_simulate_error (parser);
12871             }
12872           else if (is_overloaded_fn (argument))
12873             /* All overloaded functions are allowed; if the external
12874                linkage test does not pass, an error will be issued
12875                later.  */
12876             ;
12877           else if (address_p
12878                    && (TREE_CODE (argument) == OFFSET_REF
12879                        || TREE_CODE (argument) == SCOPE_REF))
12880             /* A pointer-to-member.  */
12881             ;
12882           else if (TREE_CODE (argument) == TEMPLATE_PARM_INDEX)
12883             ;
12884           else
12885             cp_parser_simulate_error (parser);
12886
12887           if (cp_parser_parse_definitely (parser))
12888             {
12889               if (address_p)
12890                 argument = build_x_unary_op (ADDR_EXPR, argument,
12891                                              tf_warning_or_error);
12892               return argument;
12893             }
12894         }
12895     }
12896   /* If the argument started with "&", there are no other valid
12897      alternatives at this point.  */
12898   if (address_p)
12899     {
12900       cp_parser_error (parser, "invalid non-type template argument");
12901       return error_mark_node;
12902     }
12903
12904   /* If the argument wasn't successfully parsed as a type-id followed
12905      by '>>', the argument can only be a constant expression now.
12906      Otherwise, we try parsing the constant-expression tentatively,
12907      because the argument could really be a type-id.  */
12908   if (maybe_type_id)
12909     cp_parser_parse_tentatively (parser);
12910   argument = cp_parser_constant_expression (parser,
12911                                             /*allow_non_constant_p=*/false,
12912                                             /*non_constant_p=*/NULL);
12913   argument = fold_non_dependent_expr (argument);
12914   if (!maybe_type_id)
12915     return argument;
12916   if (!cp_parser_next_token_ends_template_argument_p (parser))
12917     cp_parser_error (parser, "expected template-argument");
12918   if (cp_parser_parse_definitely (parser))
12919     return argument;
12920   /* We did our best to parse the argument as a non type-id, but that
12921      was the only alternative that matched (albeit with a '>' after
12922      it). We can assume it's just a typo from the user, and a
12923      diagnostic will then be issued.  */
12924   return cp_parser_template_type_arg (parser);
12925 }
12926
12927 /* Parse an explicit-instantiation.
12928
12929    explicit-instantiation:
12930      template declaration
12931
12932    Although the standard says `declaration', what it really means is:
12933
12934    explicit-instantiation:
12935      template decl-specifier-seq [opt] declarator [opt] ;
12936
12937    Things like `template int S<int>::i = 5, int S<double>::j;' are not
12938    supposed to be allowed.  A defect report has been filed about this
12939    issue.
12940
12941    GNU Extension:
12942
12943    explicit-instantiation:
12944      storage-class-specifier template
12945        decl-specifier-seq [opt] declarator [opt] ;
12946      function-specifier template
12947        decl-specifier-seq [opt] declarator [opt] ;  */
12948
12949 static void
12950 cp_parser_explicit_instantiation (cp_parser* parser)
12951 {
12952   int declares_class_or_enum;
12953   cp_decl_specifier_seq decl_specifiers;
12954   tree extension_specifier = NULL_TREE;
12955
12956   timevar_push (TV_TEMPLATE_INST);
12957
12958   /* Look for an (optional) storage-class-specifier or
12959      function-specifier.  */
12960   if (cp_parser_allow_gnu_extensions_p (parser))
12961     {
12962       extension_specifier
12963         = cp_parser_storage_class_specifier_opt (parser);
12964       if (!extension_specifier)
12965         extension_specifier
12966           = cp_parser_function_specifier_opt (parser,
12967                                               /*decl_specs=*/NULL);
12968     }
12969
12970   /* Look for the `template' keyword.  */
12971   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
12972   /* Let the front end know that we are processing an explicit
12973      instantiation.  */
12974   begin_explicit_instantiation ();
12975   /* [temp.explicit] says that we are supposed to ignore access
12976      control while processing explicit instantiation directives.  */
12977   push_deferring_access_checks (dk_no_check);
12978   /* Parse a decl-specifier-seq.  */
12979   cp_parser_decl_specifier_seq (parser,
12980                                 CP_PARSER_FLAGS_OPTIONAL,
12981                                 &decl_specifiers,
12982                                 &declares_class_or_enum);
12983   /* If there was exactly one decl-specifier, and it declared a class,
12984      and there's no declarator, then we have an explicit type
12985      instantiation.  */
12986   if (declares_class_or_enum && cp_parser_declares_only_class_p (parser))
12987     {
12988       tree type;
12989
12990       type = check_tag_decl (&decl_specifiers);
12991       /* Turn access control back on for names used during
12992          template instantiation.  */
12993       pop_deferring_access_checks ();
12994       if (type)
12995         do_type_instantiation (type, extension_specifier,
12996                                /*complain=*/tf_error);
12997     }
12998   else
12999     {
13000       cp_declarator *declarator;
13001       tree decl;
13002
13003       /* Parse the declarator.  */
13004       declarator
13005         = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
13006                                 /*ctor_dtor_or_conv_p=*/NULL,
13007                                 /*parenthesized_p=*/NULL,
13008                                 /*member_p=*/false);
13009       if (declares_class_or_enum & 2)
13010         cp_parser_check_for_definition_in_return_type (declarator,
13011                                                        decl_specifiers.type,
13012                                                        decl_specifiers.type_location);
13013       if (declarator != cp_error_declarator)
13014         {
13015           if (decl_specifiers.specs[(int)ds_inline])
13016             permerror (input_location, "explicit instantiation shall not use"
13017                        " %<inline%> specifier");
13018           if (decl_specifiers.specs[(int)ds_constexpr])
13019             permerror (input_location, "explicit instantiation shall not use"
13020                        " %<constexpr%> specifier");
13021
13022           decl = grokdeclarator (declarator, &decl_specifiers,
13023                                  NORMAL, 0, &decl_specifiers.attributes);
13024           /* Turn access control back on for names used during
13025              template instantiation.  */
13026           pop_deferring_access_checks ();
13027           /* Do the explicit instantiation.  */
13028           do_decl_instantiation (decl, extension_specifier);
13029         }
13030       else
13031         {
13032           pop_deferring_access_checks ();
13033           /* Skip the body of the explicit instantiation.  */
13034           cp_parser_skip_to_end_of_statement (parser);
13035         }
13036     }
13037   /* We're done with the instantiation.  */
13038   end_explicit_instantiation ();
13039
13040   cp_parser_consume_semicolon_at_end_of_statement (parser);
13041
13042   timevar_pop (TV_TEMPLATE_INST);
13043 }
13044
13045 /* Parse an explicit-specialization.
13046
13047    explicit-specialization:
13048      template < > declaration
13049
13050    Although the standard says `declaration', what it really means is:
13051
13052    explicit-specialization:
13053      template <> decl-specifier [opt] init-declarator [opt] ;
13054      template <> function-definition
13055      template <> explicit-specialization
13056      template <> template-declaration  */
13057
13058 static void
13059 cp_parser_explicit_specialization (cp_parser* parser)
13060 {
13061   bool need_lang_pop;
13062   cp_token *token = cp_lexer_peek_token (parser->lexer);
13063
13064   /* Look for the `template' keyword.  */
13065   cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE);
13066   /* Look for the `<'.  */
13067   cp_parser_require (parser, CPP_LESS, RT_LESS);
13068   /* Look for the `>'.  */
13069   cp_parser_require (parser, CPP_GREATER, RT_GREATER);
13070   /* We have processed another parameter list.  */
13071   ++parser->num_template_parameter_lists;
13072   /* [temp]
13073
13074      A template ... explicit specialization ... shall not have C
13075      linkage.  */
13076   if (current_lang_name == lang_name_c)
13077     {
13078       error_at (token->location, "template specialization with C linkage");
13079       /* Give it C++ linkage to avoid confusing other parts of the
13080          front end.  */
13081       push_lang_context (lang_name_cplusplus);
13082       need_lang_pop = true;
13083     }
13084   else
13085     need_lang_pop = false;
13086   /* Let the front end know that we are beginning a specialization.  */
13087   if (!begin_specialization ())
13088     {
13089       end_specialization ();
13090       return;
13091     }
13092
13093   /* If the next keyword is `template', we need to figure out whether
13094      or not we're looking a template-declaration.  */
13095   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
13096     {
13097       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
13098           && cp_lexer_peek_nth_token (parser->lexer, 3)->type != CPP_GREATER)
13099         cp_parser_template_declaration_after_export (parser,
13100                                                      /*member_p=*/false);
13101       else
13102         cp_parser_explicit_specialization (parser);
13103     }
13104   else
13105     /* Parse the dependent declaration.  */
13106     cp_parser_single_declaration (parser,
13107                                   /*checks=*/NULL,
13108                                   /*member_p=*/false,
13109                                   /*explicit_specialization_p=*/true,
13110                                   /*friend_p=*/NULL);
13111   /* We're done with the specialization.  */
13112   end_specialization ();
13113   /* For the erroneous case of a template with C linkage, we pushed an
13114      implicit C++ linkage scope; exit that scope now.  */
13115   if (need_lang_pop)
13116     pop_lang_context ();
13117   /* We're done with this parameter list.  */
13118   --parser->num_template_parameter_lists;
13119 }
13120
13121 /* Parse a type-specifier.
13122
13123    type-specifier:
13124      simple-type-specifier
13125      class-specifier
13126      enum-specifier
13127      elaborated-type-specifier
13128      cv-qualifier
13129
13130    GNU Extension:
13131
13132    type-specifier:
13133      __complex__
13134
13135    Returns a representation of the type-specifier.  For a
13136    class-specifier, enum-specifier, or elaborated-type-specifier, a
13137    TREE_TYPE is returned; otherwise, a TYPE_DECL is returned.
13138
13139    The parser flags FLAGS is used to control type-specifier parsing.
13140
13141    If IS_DECLARATION is TRUE, then this type-specifier is appearing
13142    in a decl-specifier-seq.
13143
13144    If DECLARES_CLASS_OR_ENUM is non-NULL, and the type-specifier is a
13145    class-specifier, enum-specifier, or elaborated-type-specifier, then
13146    *DECLARES_CLASS_OR_ENUM is set to a nonzero value.  The value is 1
13147    if a type is declared; 2 if it is defined.  Otherwise, it is set to
13148    zero.
13149
13150    If IS_CV_QUALIFIER is non-NULL, and the type-specifier is a
13151    cv-qualifier, then IS_CV_QUALIFIER is set to TRUE.  Otherwise, it
13152    is set to FALSE.  */
13153
13154 static tree
13155 cp_parser_type_specifier (cp_parser* parser,
13156                           cp_parser_flags flags,
13157                           cp_decl_specifier_seq *decl_specs,
13158                           bool is_declaration,
13159                           int* declares_class_or_enum,
13160                           bool* is_cv_qualifier)
13161 {
13162   tree type_spec = NULL_TREE;
13163   cp_token *token;
13164   enum rid keyword;
13165   cp_decl_spec ds = ds_last;
13166
13167   /* Assume this type-specifier does not declare a new type.  */
13168   if (declares_class_or_enum)
13169     *declares_class_or_enum = 0;
13170   /* And that it does not specify a cv-qualifier.  */
13171   if (is_cv_qualifier)
13172     *is_cv_qualifier = false;
13173   /* Peek at the next token.  */
13174   token = cp_lexer_peek_token (parser->lexer);
13175
13176   /* If we're looking at a keyword, we can use that to guide the
13177      production we choose.  */
13178   keyword = token->keyword;
13179   switch (keyword)
13180     {
13181     case RID_ENUM:
13182       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13183         goto elaborated_type_specifier;
13184
13185       /* Look for the enum-specifier.  */
13186       type_spec = cp_parser_enum_specifier (parser);
13187       /* If that worked, we're done.  */
13188       if (type_spec)
13189         {
13190           if (declares_class_or_enum)
13191             *declares_class_or_enum = 2;
13192           if (decl_specs)
13193             cp_parser_set_decl_spec_type (decl_specs,
13194                                           type_spec,
13195                                           token->location,
13196                                           /*type_definition_p=*/true);
13197           return type_spec;
13198         }
13199       else
13200         goto elaborated_type_specifier;
13201
13202       /* Any of these indicate either a class-specifier, or an
13203          elaborated-type-specifier.  */
13204     case RID_CLASS:
13205     case RID_STRUCT:
13206     case RID_UNION:
13207       if ((flags & CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS))
13208         goto elaborated_type_specifier;
13209
13210       /* Parse tentatively so that we can back up if we don't find a
13211          class-specifier.  */
13212       cp_parser_parse_tentatively (parser);
13213       /* Look for the class-specifier.  */
13214       type_spec = cp_parser_class_specifier (parser);
13215       invoke_plugin_callbacks (PLUGIN_FINISH_TYPE, type_spec);
13216       /* If that worked, we're done.  */
13217       if (cp_parser_parse_definitely (parser))
13218         {
13219           if (declares_class_or_enum)
13220             *declares_class_or_enum = 2;
13221           if (decl_specs)
13222             cp_parser_set_decl_spec_type (decl_specs,
13223                                           type_spec,
13224                                           token->location,
13225                                           /*type_definition_p=*/true);
13226           return type_spec;
13227         }
13228
13229       /* Fall through.  */
13230     elaborated_type_specifier:
13231       /* We're declaring (not defining) a class or enum.  */
13232       if (declares_class_or_enum)
13233         *declares_class_or_enum = 1;
13234
13235       /* Fall through.  */
13236     case RID_TYPENAME:
13237       /* Look for an elaborated-type-specifier.  */
13238       type_spec
13239         = (cp_parser_elaborated_type_specifier
13240            (parser,
13241             decl_specs && decl_specs->specs[(int) ds_friend],
13242             is_declaration));
13243       if (decl_specs)
13244         cp_parser_set_decl_spec_type (decl_specs,
13245                                       type_spec,
13246                                       token->location,
13247                                       /*type_definition_p=*/false);
13248       return type_spec;
13249
13250     case RID_CONST:
13251       ds = ds_const;
13252       if (is_cv_qualifier)
13253         *is_cv_qualifier = true;
13254       break;
13255
13256     case RID_VOLATILE:
13257       ds = ds_volatile;
13258       if (is_cv_qualifier)
13259         *is_cv_qualifier = true;
13260       break;
13261
13262     case RID_RESTRICT:
13263       ds = ds_restrict;
13264       if (is_cv_qualifier)
13265         *is_cv_qualifier = true;
13266       break;
13267
13268     case RID_COMPLEX:
13269       /* The `__complex__' keyword is a GNU extension.  */
13270       ds = ds_complex;
13271       break;
13272
13273     default:
13274       break;
13275     }
13276
13277   /* Handle simple keywords.  */
13278   if (ds != ds_last)
13279     {
13280       if (decl_specs)
13281         {
13282           ++decl_specs->specs[(int)ds];
13283           decl_specs->any_specifiers_p = true;
13284         }
13285       return cp_lexer_consume_token (parser->lexer)->u.value;
13286     }
13287
13288   /* If we do not already have a type-specifier, assume we are looking
13289      at a simple-type-specifier.  */
13290   type_spec = cp_parser_simple_type_specifier (parser,
13291                                                decl_specs,
13292                                                flags);
13293
13294   /* If we didn't find a type-specifier, and a type-specifier was not
13295      optional in this context, issue an error message.  */
13296   if (!type_spec && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13297     {
13298       cp_parser_error (parser, "expected type specifier");
13299       return error_mark_node;
13300     }
13301
13302   return type_spec;
13303 }
13304
13305 /* Parse a simple-type-specifier.
13306
13307    simple-type-specifier:
13308      :: [opt] nested-name-specifier [opt] type-name
13309      :: [opt] nested-name-specifier template template-id
13310      char
13311      wchar_t
13312      bool
13313      short
13314      int
13315      long
13316      signed
13317      unsigned
13318      float
13319      double
13320      void
13321
13322    C++0x Extension:
13323
13324    simple-type-specifier:
13325      auto
13326      decltype ( expression )   
13327      char16_t
13328      char32_t
13329      __underlying_type ( type-id )
13330
13331    GNU Extension:
13332
13333    simple-type-specifier:
13334      __int128
13335      __typeof__ unary-expression
13336      __typeof__ ( type-id )
13337
13338    Returns the indicated TYPE_DECL.  If DECL_SPECS is not NULL, it is
13339    appropriately updated.  */
13340
13341 static tree
13342 cp_parser_simple_type_specifier (cp_parser* parser,
13343                                  cp_decl_specifier_seq *decl_specs,
13344                                  cp_parser_flags flags)
13345 {
13346   tree type = NULL_TREE;
13347   cp_token *token;
13348
13349   /* Peek at the next token.  */
13350   token = cp_lexer_peek_token (parser->lexer);
13351
13352   /* If we're looking at a keyword, things are easy.  */
13353   switch (token->keyword)
13354     {
13355     case RID_CHAR:
13356       if (decl_specs)
13357         decl_specs->explicit_char_p = true;
13358       type = char_type_node;
13359       break;
13360     case RID_CHAR16:
13361       type = char16_type_node;
13362       break;
13363     case RID_CHAR32:
13364       type = char32_type_node;
13365       break;
13366     case RID_WCHAR:
13367       type = wchar_type_node;
13368       break;
13369     case RID_BOOL:
13370       type = boolean_type_node;
13371       break;
13372     case RID_SHORT:
13373       if (decl_specs)
13374         ++decl_specs->specs[(int) ds_short];
13375       type = short_integer_type_node;
13376       break;
13377     case RID_INT:
13378       if (decl_specs)
13379         decl_specs->explicit_int_p = true;
13380       type = integer_type_node;
13381       break;
13382     case RID_INT128:
13383       if (!int128_integer_type_node)
13384         break;
13385       if (decl_specs)
13386         decl_specs->explicit_int128_p = true;
13387       type = int128_integer_type_node;
13388       break;
13389     case RID_LONG:
13390       if (decl_specs)
13391         ++decl_specs->specs[(int) ds_long];
13392       type = long_integer_type_node;
13393       break;
13394     case RID_SIGNED:
13395       if (decl_specs)
13396         ++decl_specs->specs[(int) ds_signed];
13397       type = integer_type_node;
13398       break;
13399     case RID_UNSIGNED:
13400       if (decl_specs)
13401         ++decl_specs->specs[(int) ds_unsigned];
13402       type = unsigned_type_node;
13403       break;
13404     case RID_FLOAT:
13405       type = float_type_node;
13406       break;
13407     case RID_DOUBLE:
13408       type = double_type_node;
13409       break;
13410     case RID_VOID:
13411       type = void_type_node;
13412       break;
13413       
13414     case RID_AUTO:
13415       maybe_warn_cpp0x (CPP0X_AUTO);
13416       type = make_auto ();
13417       break;
13418
13419     case RID_DECLTYPE:
13420       /* Since DR 743, decltype can either be a simple-type-specifier by
13421          itself or begin a nested-name-specifier.  Parsing it will replace
13422          it with a CPP_DECLTYPE, so just rewind and let the CPP_DECLTYPE
13423          handling below decide what to do.  */
13424       cp_parser_decltype (parser);
13425       cp_lexer_set_token_position (parser->lexer, token);
13426       break;
13427
13428     case RID_TYPEOF:
13429       /* Consume the `typeof' token.  */
13430       cp_lexer_consume_token (parser->lexer);
13431       /* Parse the operand to `typeof'.  */
13432       type = cp_parser_sizeof_operand (parser, RID_TYPEOF);
13433       /* If it is not already a TYPE, take its type.  */
13434       if (!TYPE_P (type))
13435         type = finish_typeof (type);
13436
13437       if (decl_specs)
13438         cp_parser_set_decl_spec_type (decl_specs, type,
13439                                       token->location,
13440                                       /*type_definition_p=*/false);
13441
13442       return type;
13443
13444     case RID_UNDERLYING_TYPE:
13445       type = cp_parser_trait_expr (parser, RID_UNDERLYING_TYPE);
13446       if (decl_specs)
13447         cp_parser_set_decl_spec_type (decl_specs, type,
13448                                       token->location,
13449                                       /*type_definition_p=*/false);
13450
13451       return type;
13452
13453     case RID_BASES:
13454     case RID_DIRECT_BASES:
13455       type = cp_parser_trait_expr (parser, token->keyword);
13456       if (decl_specs)
13457        cp_parser_set_decl_spec_type (decl_specs, type,
13458                                      token->location,
13459                                      /*type_definition_p=*/false);
13460       return type;
13461     default:
13462       break;
13463     }
13464
13465   /* If token is an already-parsed decltype not followed by ::,
13466      it's a simple-type-specifier.  */
13467   if (token->type == CPP_DECLTYPE
13468       && cp_lexer_peek_nth_token (parser->lexer, 2)->type != CPP_SCOPE)
13469     {
13470       type = token->u.value;
13471       if (decl_specs)
13472         cp_parser_set_decl_spec_type (decl_specs, type,
13473                                       token->location,
13474                                       /*type_definition_p=*/false);
13475       cp_lexer_consume_token (parser->lexer);
13476       return type;
13477     }
13478
13479   /* If the type-specifier was for a built-in type, we're done.  */
13480   if (type)
13481     {
13482       /* Record the type.  */
13483       if (decl_specs
13484           && (token->keyword != RID_SIGNED
13485               && token->keyword != RID_UNSIGNED
13486               && token->keyword != RID_SHORT
13487               && token->keyword != RID_LONG))
13488         cp_parser_set_decl_spec_type (decl_specs,
13489                                       type,
13490                                       token->location,
13491                                       /*type_definition_p=*/false);
13492       if (decl_specs)
13493         decl_specs->any_specifiers_p = true;
13494
13495       /* Consume the token.  */
13496       cp_lexer_consume_token (parser->lexer);
13497
13498       /* There is no valid C++ program where a non-template type is
13499          followed by a "<".  That usually indicates that the user thought
13500          that the type was a template.  */
13501       cp_parser_check_for_invalid_template_id (parser, type, token->location);
13502
13503       return TYPE_NAME (type);
13504     }
13505
13506   /* The type-specifier must be a user-defined type.  */
13507   if (!(flags & CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES))
13508     {
13509       bool qualified_p;
13510       bool global_p;
13511
13512       /* Don't gobble tokens or issue error messages if this is an
13513          optional type-specifier.  */
13514       if (flags & CP_PARSER_FLAGS_OPTIONAL)
13515         cp_parser_parse_tentatively (parser);
13516
13517       /* Look for the optional `::' operator.  */
13518       global_p
13519         = (cp_parser_global_scope_opt (parser,
13520                                        /*current_scope_valid_p=*/false)
13521            != NULL_TREE);
13522       /* Look for the nested-name specifier.  */
13523       qualified_p
13524         = (cp_parser_nested_name_specifier_opt (parser,
13525                                                 /*typename_keyword_p=*/false,
13526                                                 /*check_dependency_p=*/true,
13527                                                 /*type_p=*/false,
13528                                                 /*is_declaration=*/false)
13529            != NULL_TREE);
13530       token = cp_lexer_peek_token (parser->lexer);
13531       /* If we have seen a nested-name-specifier, and the next token
13532          is `template', then we are using the template-id production.  */
13533       if (parser->scope
13534           && cp_parser_optional_template_keyword (parser))
13535         {
13536           /* Look for the template-id.  */
13537           type = cp_parser_template_id (parser,
13538                                         /*template_keyword_p=*/true,
13539                                         /*check_dependency_p=*/true,
13540                                         /*is_declaration=*/false);
13541           /* If the template-id did not name a type, we are out of
13542              luck.  */
13543           if (TREE_CODE (type) != TYPE_DECL)
13544             {
13545               cp_parser_error (parser, "expected template-id for type");
13546               type = NULL_TREE;
13547             }
13548         }
13549       /* Otherwise, look for a type-name.  */
13550       else
13551         type = cp_parser_type_name (parser);
13552       /* Keep track of all name-lookups performed in class scopes.  */
13553       if (type
13554           && !global_p
13555           && !qualified_p
13556           && TREE_CODE (type) == TYPE_DECL
13557           && TREE_CODE (DECL_NAME (type)) == IDENTIFIER_NODE)
13558         maybe_note_name_used_in_class (DECL_NAME (type), type);
13559       /* If it didn't work out, we don't have a TYPE.  */
13560       if ((flags & CP_PARSER_FLAGS_OPTIONAL)
13561           && !cp_parser_parse_definitely (parser))
13562         type = NULL_TREE;
13563       if (type && decl_specs)
13564         cp_parser_set_decl_spec_type (decl_specs, type,
13565                                       token->location,
13566                                       /*type_definition_p=*/false);
13567     }
13568
13569   /* If we didn't get a type-name, issue an error message.  */
13570   if (!type && !(flags & CP_PARSER_FLAGS_OPTIONAL))
13571     {
13572       cp_parser_error (parser, "expected type-name");
13573       return error_mark_node;
13574     }
13575
13576   if (type && type != error_mark_node)
13577     {
13578       /* See if TYPE is an Objective-C type, and if so, parse and
13579          accept any protocol references following it.  Do this before
13580          the cp_parser_check_for_invalid_template_id() call, because
13581          Objective-C types can be followed by '<...>' which would
13582          enclose protocol names rather than template arguments, and so
13583          everything is fine.  */
13584       if (c_dialect_objc () && !parser->scope
13585           && (objc_is_id (type) || objc_is_class_name (type)))
13586         {
13587           tree protos = cp_parser_objc_protocol_refs_opt (parser);
13588           tree qual_type = objc_get_protocol_qualified_type (type, protos);
13589
13590           /* Clobber the "unqualified" type previously entered into
13591              DECL_SPECS with the new, improved protocol-qualified version.  */
13592           if (decl_specs)
13593             decl_specs->type = qual_type;
13594
13595           return qual_type;
13596         }
13597
13598       /* There is no valid C++ program where a non-template type is
13599          followed by a "<".  That usually indicates that the user
13600          thought that the type was a template.  */
13601       cp_parser_check_for_invalid_template_id (parser, TREE_TYPE (type),
13602                                                token->location);
13603     }
13604
13605   return type;
13606 }
13607
13608 /* Parse a type-name.
13609
13610    type-name:
13611      class-name
13612      enum-name
13613      typedef-name
13614
13615    enum-name:
13616      identifier
13617
13618    typedef-name:
13619      identifier
13620
13621    Returns a TYPE_DECL for the type.  */
13622
13623 static tree
13624 cp_parser_type_name (cp_parser* parser)
13625 {
13626   tree type_decl;
13627
13628   /* We can't know yet whether it is a class-name or not.  */
13629   cp_parser_parse_tentatively (parser);
13630   /* Try a class-name.  */
13631   type_decl = cp_parser_class_name (parser,
13632                                     /*typename_keyword_p=*/false,
13633                                     /*template_keyword_p=*/false,
13634                                     none_type,
13635                                     /*check_dependency_p=*/true,
13636                                     /*class_head_p=*/false,
13637                                     /*is_declaration=*/false);
13638   /* If it's not a class-name, keep looking.  */
13639   if (!cp_parser_parse_definitely (parser))
13640     {
13641       /* It must be a typedef-name or an enum-name.  */
13642       return cp_parser_nonclass_name (parser);
13643     }
13644
13645   return type_decl;
13646 }
13647
13648 /* Parse a non-class type-name, that is, either an enum-name or a typedef-name.
13649
13650    enum-name:
13651      identifier
13652
13653    typedef-name:
13654      identifier
13655
13656    Returns a TYPE_DECL for the type.  */
13657
13658 static tree
13659 cp_parser_nonclass_name (cp_parser* parser)
13660 {
13661   tree type_decl;
13662   tree identifier;
13663
13664   cp_token *token = cp_lexer_peek_token (parser->lexer);
13665   identifier = cp_parser_identifier (parser);
13666   if (identifier == error_mark_node)
13667     return error_mark_node;
13668
13669   /* Look up the type-name.  */
13670   type_decl = cp_parser_lookup_name_simple (parser, identifier, token->location);
13671
13672   if (TREE_CODE (type_decl) != TYPE_DECL
13673       && (objc_is_id (identifier) || objc_is_class_name (identifier)))
13674     {
13675       /* See if this is an Objective-C type.  */
13676       tree protos = cp_parser_objc_protocol_refs_opt (parser);
13677       tree type = objc_get_protocol_qualified_type (identifier, protos);
13678       if (type)
13679         type_decl = TYPE_NAME (type);
13680     }
13681
13682   /* Issue an error if we did not find a type-name.  */
13683   if (TREE_CODE (type_decl) != TYPE_DECL
13684       /* In Objective-C, we have the complication that class names are
13685          normally type names and start declarations (eg, the
13686          "NSObject" in "NSObject *object;"), but can be used in an
13687          Objective-C 2.0 dot-syntax (as in "NSObject.version") which
13688          is an expression.  So, a classname followed by a dot is not a
13689          valid type-name.  */
13690       || (objc_is_class_name (TREE_TYPE (type_decl))
13691           && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT))
13692     {
13693       if (!cp_parser_simulate_error (parser))
13694         cp_parser_name_lookup_error (parser, identifier, type_decl,
13695                                      NLE_TYPE, token->location);
13696       return error_mark_node;
13697     }
13698   /* Remember that the name was used in the definition of the
13699      current class so that we can check later to see if the
13700      meaning would have been different after the class was
13701      entirely defined.  */
13702   else if (type_decl != error_mark_node
13703            && !parser->scope)
13704     maybe_note_name_used_in_class (identifier, type_decl);
13705   
13706   return type_decl;
13707 }
13708
13709 /* Parse an elaborated-type-specifier.  Note that the grammar given
13710    here incorporates the resolution to DR68.
13711
13712    elaborated-type-specifier:
13713      class-key :: [opt] nested-name-specifier [opt] identifier
13714      class-key :: [opt] nested-name-specifier [opt] template [opt] template-id
13715      enum-key :: [opt] nested-name-specifier [opt] identifier
13716      typename :: [opt] nested-name-specifier identifier
13717      typename :: [opt] nested-name-specifier template [opt]
13718        template-id
13719
13720    GNU extension:
13721
13722    elaborated-type-specifier:
13723      class-key attributes :: [opt] nested-name-specifier [opt] identifier
13724      class-key attributes :: [opt] nested-name-specifier [opt]
13725                template [opt] template-id
13726      enum attributes :: [opt] nested-name-specifier [opt] identifier
13727
13728    If IS_FRIEND is TRUE, then this elaborated-type-specifier is being
13729    declared `friend'.  If IS_DECLARATION is TRUE, then this
13730    elaborated-type-specifier appears in a decl-specifiers-seq, i.e.,
13731    something is being declared.
13732
13733    Returns the TYPE specified.  */
13734
13735 static tree
13736 cp_parser_elaborated_type_specifier (cp_parser* parser,
13737                                      bool is_friend,
13738                                      bool is_declaration)
13739 {
13740   enum tag_types tag_type;
13741   tree identifier;
13742   tree type = NULL_TREE;
13743   tree attributes = NULL_TREE;
13744   tree globalscope;
13745   cp_token *token = NULL;
13746
13747   /* See if we're looking at the `enum' keyword.  */
13748   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ENUM))
13749     {
13750       /* Consume the `enum' token.  */
13751       cp_lexer_consume_token (parser->lexer);
13752       /* Remember that it's an enumeration type.  */
13753       tag_type = enum_type;
13754       /* Issue a warning if the `struct' or `class' key (for C++0x scoped
13755          enums) is used here.  */
13756       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
13757           || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
13758         {
13759             pedwarn (input_location, 0, "elaborated-type-specifier "
13760                       "for a scoped enum must not use the %<%D%> keyword",
13761                       cp_lexer_peek_token (parser->lexer)->u.value);
13762           /* Consume the `struct' or `class' and parse it anyway.  */
13763           cp_lexer_consume_token (parser->lexer);
13764         }
13765       /* Parse the attributes.  */
13766       attributes = cp_parser_attributes_opt (parser);
13767     }
13768   /* Or, it might be `typename'.  */
13769   else if (cp_lexer_next_token_is_keyword (parser->lexer,
13770                                            RID_TYPENAME))
13771     {
13772       /* Consume the `typename' token.  */
13773       cp_lexer_consume_token (parser->lexer);
13774       /* Remember that it's a `typename' type.  */
13775       tag_type = typename_type;
13776     }
13777   /* Otherwise it must be a class-key.  */
13778   else
13779     {
13780       tag_type = cp_parser_class_key (parser);
13781       if (tag_type == none_type)
13782         return error_mark_node;
13783       /* Parse the attributes.  */
13784       attributes = cp_parser_attributes_opt (parser);
13785     }
13786
13787   /* Look for the `::' operator.  */
13788   globalscope =  cp_parser_global_scope_opt (parser,
13789                                              /*current_scope_valid_p=*/false);
13790   /* Look for the nested-name-specifier.  */
13791   if (tag_type == typename_type && !globalscope)
13792     {
13793       if (!cp_parser_nested_name_specifier (parser,
13794                                            /*typename_keyword_p=*/true,
13795                                            /*check_dependency_p=*/true,
13796                                            /*type_p=*/true,
13797                                             is_declaration))
13798         return error_mark_node;
13799     }
13800   else
13801     /* Even though `typename' is not present, the proposed resolution
13802        to Core Issue 180 says that in `class A<T>::B', `B' should be
13803        considered a type-name, even if `A<T>' is dependent.  */
13804     cp_parser_nested_name_specifier_opt (parser,
13805                                          /*typename_keyword_p=*/true,
13806                                          /*check_dependency_p=*/true,
13807                                          /*type_p=*/true,
13808                                          is_declaration);
13809  /* For everything but enumeration types, consider a template-id.
13810     For an enumeration type, consider only a plain identifier.  */
13811   if (tag_type != enum_type)
13812     {
13813       bool template_p = false;
13814       tree decl;
13815
13816       /* Allow the `template' keyword.  */
13817       template_p = cp_parser_optional_template_keyword (parser);
13818       /* If we didn't see `template', we don't know if there's a
13819          template-id or not.  */
13820       if (!template_p)
13821         cp_parser_parse_tentatively (parser);
13822       /* Parse the template-id.  */
13823       token = cp_lexer_peek_token (parser->lexer);
13824       decl = cp_parser_template_id (parser, template_p,
13825                                     /*check_dependency_p=*/true,
13826                                     is_declaration);
13827       /* If we didn't find a template-id, look for an ordinary
13828          identifier.  */
13829       if (!template_p && !cp_parser_parse_definitely (parser))
13830         ;
13831       /* If DECL is a TEMPLATE_ID_EXPR, and the `typename' keyword is
13832          in effect, then we must assume that, upon instantiation, the
13833          template will correspond to a class.  */
13834       else if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
13835                && tag_type == typename_type)
13836         type = make_typename_type (parser->scope, decl,
13837                                    typename_type,
13838                                    /*complain=*/tf_error);
13839       /* If the `typename' keyword is in effect and DECL is not a type
13840          decl. Then type is non existant.   */
13841       else if (tag_type == typename_type && TREE_CODE (decl) != TYPE_DECL)
13842         type = NULL_TREE; 
13843       else 
13844         type = TREE_TYPE (decl);
13845     }
13846
13847   if (!type)
13848     {
13849       token = cp_lexer_peek_token (parser->lexer);
13850       identifier = cp_parser_identifier (parser);
13851
13852       if (identifier == error_mark_node)
13853         {
13854           parser->scope = NULL_TREE;
13855           return error_mark_node;
13856         }
13857
13858       /* For a `typename', we needn't call xref_tag.  */
13859       if (tag_type == typename_type
13860           && TREE_CODE (parser->scope) != NAMESPACE_DECL)
13861         return cp_parser_make_typename_type (parser, parser->scope,
13862                                              identifier,
13863                                              token->location);
13864       /* Look up a qualified name in the usual way.  */
13865       if (parser->scope)
13866         {
13867           tree decl;
13868           tree ambiguous_decls;
13869
13870           decl = cp_parser_lookup_name (parser, identifier,
13871                                         tag_type,
13872                                         /*is_template=*/false,
13873                                         /*is_namespace=*/false,
13874                                         /*check_dependency=*/true,
13875                                         &ambiguous_decls,
13876                                         token->location);
13877
13878           /* If the lookup was ambiguous, an error will already have been
13879              issued.  */
13880           if (ambiguous_decls)
13881             return error_mark_node;
13882
13883           /* If we are parsing friend declaration, DECL may be a
13884              TEMPLATE_DECL tree node here.  However, we need to check
13885              whether this TEMPLATE_DECL results in valid code.  Consider
13886              the following example:
13887
13888                namespace N {
13889                  template <class T> class C {};
13890                }
13891                class X {
13892                  template <class T> friend class N::C; // #1, valid code
13893                };
13894                template <class T> class Y {
13895                  friend class N::C;                    // #2, invalid code
13896                };
13897
13898              For both case #1 and #2, we arrive at a TEMPLATE_DECL after
13899              name lookup of `N::C'.  We see that friend declaration must
13900              be template for the code to be valid.  Note that
13901              processing_template_decl does not work here since it is
13902              always 1 for the above two cases.  */
13903
13904           decl = (cp_parser_maybe_treat_template_as_class
13905                   (decl, /*tag_name_p=*/is_friend
13906                          && parser->num_template_parameter_lists));
13907
13908           if (TREE_CODE (decl) != TYPE_DECL)
13909             {
13910               cp_parser_diagnose_invalid_type_name (parser,
13911                                                     parser->scope,
13912                                                     identifier,
13913                                                     token->location);
13914               return error_mark_node;
13915             }
13916
13917           if (TREE_CODE (TREE_TYPE (decl)) != TYPENAME_TYPE)
13918             {
13919               bool allow_template = (parser->num_template_parameter_lists
13920                                       || DECL_SELF_REFERENCE_P (decl));
13921               type = check_elaborated_type_specifier (tag_type, decl, 
13922                                                       allow_template);
13923
13924               if (type == error_mark_node)
13925                 return error_mark_node;
13926             }
13927
13928           /* Forward declarations of nested types, such as
13929
13930                class C1::C2;
13931                class C1::C2::C3;
13932
13933              are invalid unless all components preceding the final '::'
13934              are complete.  If all enclosing types are complete, these
13935              declarations become merely pointless.
13936
13937              Invalid forward declarations of nested types are errors
13938              caught elsewhere in parsing.  Those that are pointless arrive
13939              here.  */
13940
13941           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
13942               && !is_friend && !processing_explicit_instantiation)
13943             warning (0, "declaration %qD does not declare anything", decl);
13944
13945           type = TREE_TYPE (decl);
13946         }
13947       else
13948         {
13949           /* An elaborated-type-specifier sometimes introduces a new type and
13950              sometimes names an existing type.  Normally, the rule is that it
13951              introduces a new type only if there is not an existing type of
13952              the same name already in scope.  For example, given:
13953
13954                struct S {};
13955                void f() { struct S s; }
13956
13957              the `struct S' in the body of `f' is the same `struct S' as in
13958              the global scope; the existing definition is used.  However, if
13959              there were no global declaration, this would introduce a new
13960              local class named `S'.
13961
13962              An exception to this rule applies to the following code:
13963
13964                namespace N { struct S; }
13965
13966              Here, the elaborated-type-specifier names a new type
13967              unconditionally; even if there is already an `S' in the
13968              containing scope this declaration names a new type.
13969              This exception only applies if the elaborated-type-specifier
13970              forms the complete declaration:
13971
13972                [class.name]
13973
13974                A declaration consisting solely of `class-key identifier ;' is
13975                either a redeclaration of the name in the current scope or a
13976                forward declaration of the identifier as a class name.  It
13977                introduces the name into the current scope.
13978
13979              We are in this situation precisely when the next token is a `;'.
13980
13981              An exception to the exception is that a `friend' declaration does
13982              *not* name a new type; i.e., given:
13983
13984                struct S { friend struct T; };
13985
13986              `T' is not a new type in the scope of `S'.
13987
13988              Also, `new struct S' or `sizeof (struct S)' never results in the
13989              definition of a new type; a new type can only be declared in a
13990              declaration context.  */
13991
13992           tag_scope ts;
13993           bool template_p;
13994
13995           if (is_friend)
13996             /* Friends have special name lookup rules.  */
13997             ts = ts_within_enclosing_non_class;
13998           else if (is_declaration
13999                    && cp_lexer_next_token_is (parser->lexer,
14000                                               CPP_SEMICOLON))
14001             /* This is a `class-key identifier ;' */
14002             ts = ts_current;
14003           else
14004             ts = ts_global;
14005
14006           template_p =
14007             (parser->num_template_parameter_lists
14008              && (cp_parser_next_token_starts_class_definition_p (parser)
14009                  || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)));
14010           /* An unqualified name was used to reference this type, so
14011              there were no qualifying templates.  */
14012           if (!cp_parser_check_template_parameters (parser,
14013                                                     /*num_templates=*/0,
14014                                                     token->location,
14015                                                     /*declarator=*/NULL))
14016             return error_mark_node;
14017           type = xref_tag (tag_type, identifier, ts, template_p);
14018         }
14019     }
14020
14021   if (type == error_mark_node)
14022     return error_mark_node;
14023
14024   /* Allow attributes on forward declarations of classes.  */
14025   if (attributes)
14026     {
14027       if (TREE_CODE (type) == TYPENAME_TYPE)
14028         warning (OPT_Wattributes,
14029                  "attributes ignored on uninstantiated type");
14030       else if (tag_type != enum_type && CLASSTYPE_TEMPLATE_INSTANTIATION (type)
14031                && ! processing_explicit_instantiation)
14032         warning (OPT_Wattributes,
14033                  "attributes ignored on template instantiation");
14034       else if (is_declaration && cp_parser_declares_only_class_p (parser))
14035         cplus_decl_attributes (&type, attributes, (int) ATTR_FLAG_TYPE_IN_PLACE);
14036       else
14037         warning (OPT_Wattributes,
14038                  "attributes ignored on elaborated-type-specifier that is not a forward declaration");
14039     }
14040
14041   if (tag_type != enum_type)
14042     {
14043       /* Indicate whether this class was declared as a `class' or as a
14044          `struct'.  */
14045       if (TREE_CODE (type) == RECORD_TYPE)
14046         CLASSTYPE_DECLARED_CLASS (type) = (tag_type == class_type);
14047       cp_parser_check_class_key (tag_type, type);
14048     }
14049
14050   /* A "<" cannot follow an elaborated type specifier.  If that
14051      happens, the user was probably trying to form a template-id.  */
14052   cp_parser_check_for_invalid_template_id (parser, type, token->location);
14053
14054   return type;
14055 }
14056
14057 /* Parse an enum-specifier.
14058
14059    enum-specifier:
14060      enum-head { enumerator-list [opt] }
14061
14062    enum-head:
14063      enum-key identifier [opt] enum-base [opt]
14064      enum-key nested-name-specifier identifier enum-base [opt]
14065
14066    enum-key:
14067      enum
14068      enum class   [C++0x]
14069      enum struct  [C++0x]
14070
14071    enum-base:   [C++0x]
14072      : type-specifier-seq
14073
14074    opaque-enum-specifier:
14075      enum-key identifier enum-base [opt] ;
14076
14077    GNU Extensions:
14078      enum-key attributes[opt] identifier [opt] enum-base [opt] 
14079        { enumerator-list [opt] }attributes[opt]
14080
14081    Returns an ENUM_TYPE representing the enumeration, or NULL_TREE
14082    if the token stream isn't an enum-specifier after all.  */
14083
14084 static tree
14085 cp_parser_enum_specifier (cp_parser* parser)
14086 {
14087   tree identifier;
14088   tree type = NULL_TREE;
14089   tree prev_scope;
14090   tree nested_name_specifier = NULL_TREE;
14091   tree attributes;
14092   bool scoped_enum_p = false;
14093   bool has_underlying_type = false;
14094   bool nested_being_defined = false;
14095   bool new_value_list = false;
14096   bool is_new_type = false;
14097   bool is_anonymous = false;
14098   tree underlying_type = NULL_TREE;
14099   cp_token *type_start_token = NULL;
14100   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
14101
14102   parser->colon_corrects_to_scope_p = false;
14103
14104   /* Parse tentatively so that we can back up if we don't find a
14105      enum-specifier.  */
14106   cp_parser_parse_tentatively (parser);
14107
14108   /* Caller guarantees that the current token is 'enum', an identifier
14109      possibly follows, and the token after that is an opening brace.
14110      If we don't have an identifier, fabricate an anonymous name for
14111      the enumeration being defined.  */
14112   cp_lexer_consume_token (parser->lexer);
14113
14114   /* Parse the "class" or "struct", which indicates a scoped
14115      enumeration type in C++0x.  */
14116   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_CLASS)
14117       || cp_lexer_next_token_is_keyword (parser->lexer, RID_STRUCT))
14118     {
14119       if (cxx_dialect < cxx0x)
14120         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14121
14122       /* Consume the `struct' or `class' token.  */
14123       cp_lexer_consume_token (parser->lexer);
14124
14125       scoped_enum_p = true;
14126     }
14127
14128   attributes = cp_parser_attributes_opt (parser);
14129
14130   /* Clear the qualification.  */
14131   parser->scope = NULL_TREE;
14132   parser->qualifying_scope = NULL_TREE;
14133   parser->object_scope = NULL_TREE;
14134
14135   /* Figure out in what scope the declaration is being placed.  */
14136   prev_scope = current_scope ();
14137
14138   type_start_token = cp_lexer_peek_token (parser->lexer);
14139
14140   push_deferring_access_checks (dk_no_check);
14141   nested_name_specifier
14142       = cp_parser_nested_name_specifier_opt (parser,
14143                                              /*typename_keyword_p=*/true,
14144                                              /*check_dependency_p=*/false,
14145                                              /*type_p=*/false,
14146                                              /*is_declaration=*/false);
14147
14148   if (nested_name_specifier)
14149     {
14150       tree name;
14151
14152       identifier = cp_parser_identifier (parser);
14153       name =  cp_parser_lookup_name (parser, identifier,
14154                                      enum_type,
14155                                      /*is_template=*/false,
14156                                      /*is_namespace=*/false,
14157                                      /*check_dependency=*/true,
14158                                      /*ambiguous_decls=*/NULL,
14159                                      input_location);
14160       if (name)
14161         {
14162           type = TREE_TYPE (name);
14163           if (TREE_CODE (type) == TYPENAME_TYPE)
14164             {
14165               /* Are template enums allowed in ISO? */
14166               if (template_parm_scope_p ())
14167                 pedwarn (type_start_token->location, OPT_pedantic,
14168                          "%qD is an enumeration template", name);
14169               /* ignore a typename reference, for it will be solved by name
14170                  in start_enum.  */
14171               type = NULL_TREE;
14172             }
14173         }
14174       else
14175         error_at (type_start_token->location,
14176                   "%qD is not an enumerator-name", identifier);
14177     }
14178   else
14179     {
14180       if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14181         identifier = cp_parser_identifier (parser);
14182       else
14183         {
14184           identifier = make_anon_name ();
14185           is_anonymous = true;
14186         }
14187     }
14188   pop_deferring_access_checks ();
14189
14190   /* Check for the `:' that denotes a specified underlying type in C++0x.
14191      Note that a ':' could also indicate a bitfield width, however.  */
14192   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14193     {
14194       cp_decl_specifier_seq type_specifiers;
14195
14196       /* Consume the `:'.  */
14197       cp_lexer_consume_token (parser->lexer);
14198
14199       /* Parse the type-specifier-seq.  */
14200       cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
14201                                     /*is_trailing_return=*/false,
14202                                     &type_specifiers);
14203
14204       /* At this point this is surely not elaborated type specifier.  */
14205       if (!cp_parser_parse_definitely (parser))
14206         return NULL_TREE;
14207
14208       if (cxx_dialect < cxx0x)
14209         maybe_warn_cpp0x (CPP0X_SCOPED_ENUMS);
14210
14211       has_underlying_type = true;
14212
14213       /* If that didn't work, stop.  */
14214       if (type_specifiers.type != error_mark_node)
14215         {
14216           underlying_type = grokdeclarator (NULL, &type_specifiers, TYPENAME,
14217                                             /*initialized=*/0, NULL);
14218           if (underlying_type == error_mark_node)
14219             underlying_type = NULL_TREE;
14220         }
14221     }
14222
14223   /* Look for the `{' but don't consume it yet.  */
14224   if (!cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14225     {
14226       if (cxx_dialect < cxx0x || (!scoped_enum_p && !underlying_type))
14227         {
14228           cp_parser_error (parser, "expected %<{%>");
14229           if (has_underlying_type)
14230             {
14231               type = NULL_TREE;
14232               goto out;
14233             }
14234         }
14235       /* An opaque-enum-specifier must have a ';' here.  */
14236       if ((scoped_enum_p || underlying_type)
14237           && cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14238         {
14239           cp_parser_error (parser, "expected %<;%> or %<{%>");
14240           if (has_underlying_type)
14241             {
14242               type = NULL_TREE;
14243               goto out;
14244             }
14245         }
14246     }
14247
14248   if (!has_underlying_type && !cp_parser_parse_definitely (parser))
14249     return NULL_TREE;
14250
14251   if (nested_name_specifier)
14252     {
14253       if (CLASS_TYPE_P (nested_name_specifier))
14254         {
14255           nested_being_defined = TYPE_BEING_DEFINED (nested_name_specifier);
14256           TYPE_BEING_DEFINED (nested_name_specifier) = 1;
14257           push_scope (nested_name_specifier);
14258         }
14259       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14260         {
14261           push_nested_namespace (nested_name_specifier);
14262         }
14263     }
14264
14265   /* Issue an error message if type-definitions are forbidden here.  */
14266   if (!cp_parser_check_type_definition (parser))
14267     type = error_mark_node;
14268   else
14269     /* Create the new type.  We do this before consuming the opening
14270        brace so the enum will be recorded as being on the line of its
14271        tag (or the 'enum' keyword, if there is no tag).  */
14272     type = start_enum (identifier, type, underlying_type,
14273                        scoped_enum_p, &is_new_type);
14274
14275   /* If the next token is not '{' it is an opaque-enum-specifier or an
14276      elaborated-type-specifier.  */
14277   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
14278     {
14279       timevar_push (TV_PARSE_ENUM);
14280       if (nested_name_specifier)
14281         {
14282           /* The following catches invalid code such as:
14283              enum class S<int>::E { A, B, C }; */
14284           if (!processing_specialization
14285               && CLASS_TYPE_P (nested_name_specifier)
14286               && CLASSTYPE_USE_TEMPLATE (nested_name_specifier))
14287             error_at (type_start_token->location, "cannot add an enumerator "
14288                       "list to a template instantiation");
14289
14290           /* If that scope does not contain the scope in which the
14291              class was originally declared, the program is invalid.  */
14292           if (prev_scope && !is_ancestor (prev_scope, nested_name_specifier))
14293             {
14294               if (at_namespace_scope_p ())
14295                 error_at (type_start_token->location,
14296                           "declaration of %qD in namespace %qD which does not "
14297                           "enclose %qD",
14298                           type, prev_scope, nested_name_specifier);
14299               else
14300                 error_at (type_start_token->location,
14301                           "declaration of %qD in %qD which does not enclose %qD",
14302                           type, prev_scope, nested_name_specifier);
14303               type = error_mark_node;
14304             }
14305         }
14306
14307       if (scoped_enum_p)
14308         begin_scope (sk_scoped_enum, type);
14309
14310       /* Consume the opening brace.  */
14311       cp_lexer_consume_token (parser->lexer);
14312
14313       if (type == error_mark_node)
14314         ; /* Nothing to add */
14315       else if (OPAQUE_ENUM_P (type)
14316                || (cxx_dialect > cxx98 && processing_specialization))
14317         {
14318           new_value_list = true;
14319           SET_OPAQUE_ENUM_P (type, false);
14320           DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
14321         }
14322       else
14323         {
14324           error_at (type_start_token->location, "multiple definition of %q#T", type);
14325           error_at (DECL_SOURCE_LOCATION (TYPE_MAIN_DECL (type)),
14326                     "previous definition here");
14327           type = error_mark_node;
14328         }
14329
14330       if (type == error_mark_node)
14331         cp_parser_skip_to_end_of_block_or_statement (parser);
14332       /* If the next token is not '}', then there are some enumerators.  */
14333       else if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
14334         cp_parser_enumerator_list (parser, type);
14335
14336       /* Consume the final '}'.  */
14337       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14338
14339       if (scoped_enum_p)
14340         finish_scope ();
14341       timevar_pop (TV_PARSE_ENUM);
14342     }
14343   else
14344     {
14345       /* If a ';' follows, then it is an opaque-enum-specifier
14346         and additional restrictions apply.  */
14347       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
14348         {
14349           if (is_anonymous)
14350             error_at (type_start_token->location,
14351                       "opaque-enum-specifier without name");
14352           else if (nested_name_specifier)
14353             error_at (type_start_token->location,
14354                       "opaque-enum-specifier must use a simple identifier");
14355         }
14356     }
14357
14358   /* Look for trailing attributes to apply to this enumeration, and
14359      apply them if appropriate.  */
14360   if (cp_parser_allow_gnu_extensions_p (parser))
14361     {
14362       tree trailing_attr = cp_parser_attributes_opt (parser);
14363       trailing_attr = chainon (trailing_attr, attributes);
14364       cplus_decl_attributes (&type,
14365                              trailing_attr,
14366                              (int) ATTR_FLAG_TYPE_IN_PLACE);
14367     }
14368
14369   /* Finish up the enumeration.  */
14370   if (type != error_mark_node)
14371     {
14372       if (new_value_list)
14373         finish_enum_value_list (type);
14374       if (is_new_type)
14375         finish_enum (type);
14376     }
14377
14378   if (nested_name_specifier)
14379     {
14380       if (CLASS_TYPE_P (nested_name_specifier))
14381         {
14382           TYPE_BEING_DEFINED (nested_name_specifier) = nested_being_defined;
14383           pop_scope (nested_name_specifier);
14384         }
14385       else if (TREE_CODE (nested_name_specifier) == NAMESPACE_DECL)
14386         {
14387           pop_nested_namespace (nested_name_specifier);
14388         }
14389     }
14390  out:
14391   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
14392   return type;
14393 }
14394
14395 /* Parse an enumerator-list.  The enumerators all have the indicated
14396    TYPE.
14397
14398    enumerator-list:
14399      enumerator-definition
14400      enumerator-list , enumerator-definition  */
14401
14402 static void
14403 cp_parser_enumerator_list (cp_parser* parser, tree type)
14404 {
14405   while (true)
14406     {
14407       /* Parse an enumerator-definition.  */
14408       cp_parser_enumerator_definition (parser, type);
14409
14410       /* If the next token is not a ',', we've reached the end of
14411          the list.  */
14412       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
14413         break;
14414       /* Otherwise, consume the `,' and keep going.  */
14415       cp_lexer_consume_token (parser->lexer);
14416       /* If the next token is a `}', there is a trailing comma.  */
14417       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
14418         {
14419           if (!in_system_header)
14420             pedwarn (input_location, OPT_pedantic, "comma at end of enumerator list");
14421           break;
14422         }
14423     }
14424 }
14425
14426 /* Parse an enumerator-definition.  The enumerator has the indicated
14427    TYPE.
14428
14429    enumerator-definition:
14430      enumerator
14431      enumerator = constant-expression
14432
14433    enumerator:
14434      identifier  */
14435
14436 static void
14437 cp_parser_enumerator_definition (cp_parser* parser, tree type)
14438 {
14439   tree identifier;
14440   tree value;
14441   location_t loc;
14442
14443   /* Save the input location because we are interested in the location
14444      of the identifier and not the location of the explicit value.  */
14445   loc = cp_lexer_peek_token (parser->lexer)->location;
14446
14447   /* Look for the identifier.  */
14448   identifier = cp_parser_identifier (parser);
14449   if (identifier == error_mark_node)
14450     return;
14451
14452   /* If the next token is an '=', then there is an explicit value.  */
14453   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
14454     {
14455       /* Consume the `=' token.  */
14456       cp_lexer_consume_token (parser->lexer);
14457       /* Parse the value.  */
14458       value = cp_parser_constant_expression (parser,
14459                                              /*allow_non_constant_p=*/false,
14460                                              NULL);
14461     }
14462   else
14463     value = NULL_TREE;
14464
14465   /* If we are processing a template, make sure the initializer of the
14466      enumerator doesn't contain any bare template parameter pack.  */
14467   if (check_for_bare_parameter_packs (value))
14468     value = error_mark_node;
14469
14470   /* integral_constant_value will pull out this expression, so make sure
14471      it's folded as appropriate.  */
14472   value = fold_non_dependent_expr (value);
14473
14474   /* Create the enumerator.  */
14475   build_enumerator (identifier, value, type, loc);
14476 }
14477
14478 /* Parse a namespace-name.
14479
14480    namespace-name:
14481      original-namespace-name
14482      namespace-alias
14483
14484    Returns the NAMESPACE_DECL for the namespace.  */
14485
14486 static tree
14487 cp_parser_namespace_name (cp_parser* parser)
14488 {
14489   tree identifier;
14490   tree namespace_decl;
14491
14492   cp_token *token = cp_lexer_peek_token (parser->lexer);
14493
14494   /* Get the name of the namespace.  */
14495   identifier = cp_parser_identifier (parser);
14496   if (identifier == error_mark_node)
14497     return error_mark_node;
14498
14499   /* Look up the identifier in the currently active scope.  Look only
14500      for namespaces, due to:
14501
14502        [basic.lookup.udir]
14503
14504        When looking up a namespace-name in a using-directive or alias
14505        definition, only namespace names are considered.
14506
14507      And:
14508
14509        [basic.lookup.qual]
14510
14511        During the lookup of a name preceding the :: scope resolution
14512        operator, object, function, and enumerator names are ignored.
14513
14514      (Note that cp_parser_qualifying_entity only calls this
14515      function if the token after the name is the scope resolution
14516      operator.)  */
14517   namespace_decl = cp_parser_lookup_name (parser, identifier,
14518                                           none_type,
14519                                           /*is_template=*/false,
14520                                           /*is_namespace=*/true,
14521                                           /*check_dependency=*/true,
14522                                           /*ambiguous_decls=*/NULL,
14523                                           token->location);
14524   /* If it's not a namespace, issue an error.  */
14525   if (namespace_decl == error_mark_node
14526       || TREE_CODE (namespace_decl) != NAMESPACE_DECL)
14527     {
14528       if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
14529         error_at (token->location, "%qD is not a namespace-name", identifier);
14530       cp_parser_error (parser, "expected namespace-name");
14531       namespace_decl = error_mark_node;
14532     }
14533
14534   return namespace_decl;
14535 }
14536
14537 /* Parse a namespace-definition.
14538
14539    namespace-definition:
14540      named-namespace-definition
14541      unnamed-namespace-definition
14542
14543    named-namespace-definition:
14544      original-namespace-definition
14545      extension-namespace-definition
14546
14547    original-namespace-definition:
14548      namespace identifier { namespace-body }
14549
14550    extension-namespace-definition:
14551      namespace original-namespace-name { namespace-body }
14552
14553    unnamed-namespace-definition:
14554      namespace { namespace-body } */
14555
14556 static void
14557 cp_parser_namespace_definition (cp_parser* parser)
14558 {
14559   tree identifier, attribs;
14560   bool has_visibility;
14561   bool is_inline;
14562
14563   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_INLINE))
14564     {
14565       maybe_warn_cpp0x (CPP0X_INLINE_NAMESPACES);
14566       is_inline = true;
14567       cp_lexer_consume_token (parser->lexer);
14568     }
14569   else
14570     is_inline = false;
14571
14572   /* Look for the `namespace' keyword.  */
14573   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14574
14575   /* Get the name of the namespace.  We do not attempt to distinguish
14576      between an original-namespace-definition and an
14577      extension-namespace-definition at this point.  The semantic
14578      analysis routines are responsible for that.  */
14579   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
14580     identifier = cp_parser_identifier (parser);
14581   else
14582     identifier = NULL_TREE;
14583
14584   /* Parse any specified attributes.  */
14585   attribs = cp_parser_attributes_opt (parser);
14586
14587   /* Look for the `{' to start the namespace.  */
14588   cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE);
14589   /* Start the namespace.  */
14590   push_namespace (identifier);
14591
14592   /* "inline namespace" is equivalent to a stub namespace definition
14593      followed by a strong using directive.  */
14594   if (is_inline)
14595     {
14596       tree name_space = current_namespace;
14597       /* Set up namespace association.  */
14598       DECL_NAMESPACE_ASSOCIATIONS (name_space)
14599         = tree_cons (CP_DECL_CONTEXT (name_space), NULL_TREE,
14600                      DECL_NAMESPACE_ASSOCIATIONS (name_space));
14601       /* Import the contents of the inline namespace.  */
14602       pop_namespace ();
14603       do_using_directive (name_space);
14604       push_namespace (identifier);
14605     }
14606
14607   has_visibility = handle_namespace_attrs (current_namespace, attribs);
14608
14609   /* Parse the body of the namespace.  */
14610   cp_parser_namespace_body (parser);
14611
14612   if (has_visibility)
14613     pop_visibility (1);
14614
14615   /* Finish the namespace.  */
14616   pop_namespace ();
14617   /* Look for the final `}'.  */
14618   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
14619 }
14620
14621 /* Parse a namespace-body.
14622
14623    namespace-body:
14624      declaration-seq [opt]  */
14625
14626 static void
14627 cp_parser_namespace_body (cp_parser* parser)
14628 {
14629   cp_parser_declaration_seq_opt (parser);
14630 }
14631
14632 /* Parse a namespace-alias-definition.
14633
14634    namespace-alias-definition:
14635      namespace identifier = qualified-namespace-specifier ;  */
14636
14637 static void
14638 cp_parser_namespace_alias_definition (cp_parser* parser)
14639 {
14640   tree identifier;
14641   tree namespace_specifier;
14642
14643   cp_token *token = cp_lexer_peek_token (parser->lexer);
14644
14645   /* Look for the `namespace' keyword.  */
14646   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14647   /* Look for the identifier.  */
14648   identifier = cp_parser_identifier (parser);
14649   if (identifier == error_mark_node)
14650     return;
14651   /* Look for the `=' token.  */
14652   if (!cp_parser_uncommitted_to_tentative_parse_p (parser)
14653       && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)) 
14654     {
14655       error_at (token->location, "%<namespace%> definition is not allowed here");
14656       /* Skip the definition.  */
14657       cp_lexer_consume_token (parser->lexer);
14658       if (cp_parser_skip_to_closing_brace (parser))
14659         cp_lexer_consume_token (parser->lexer);
14660       return;
14661     }
14662   cp_parser_require (parser, CPP_EQ, RT_EQ);
14663   /* Look for the qualified-namespace-specifier.  */
14664   namespace_specifier
14665     = cp_parser_qualified_namespace_specifier (parser);
14666   /* Look for the `;' token.  */
14667   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14668
14669   /* Register the alias in the symbol table.  */
14670   do_namespace_alias (identifier, namespace_specifier);
14671 }
14672
14673 /* Parse a qualified-namespace-specifier.
14674
14675    qualified-namespace-specifier:
14676      :: [opt] nested-name-specifier [opt] namespace-name
14677
14678    Returns a NAMESPACE_DECL corresponding to the specified
14679    namespace.  */
14680
14681 static tree
14682 cp_parser_qualified_namespace_specifier (cp_parser* parser)
14683 {
14684   /* Look for the optional `::'.  */
14685   cp_parser_global_scope_opt (parser,
14686                               /*current_scope_valid_p=*/false);
14687
14688   /* Look for the optional nested-name-specifier.  */
14689   cp_parser_nested_name_specifier_opt (parser,
14690                                        /*typename_keyword_p=*/false,
14691                                        /*check_dependency_p=*/true,
14692                                        /*type_p=*/false,
14693                                        /*is_declaration=*/true);
14694
14695   return cp_parser_namespace_name (parser);
14696 }
14697
14698 /* Parse a using-declaration, or, if ACCESS_DECLARATION_P is true, an
14699    access declaration.
14700
14701    using-declaration:
14702      using typename [opt] :: [opt] nested-name-specifier unqualified-id ;
14703      using :: unqualified-id ;  
14704
14705    access-declaration:
14706      qualified-id ;  
14707
14708    */
14709
14710 static bool
14711 cp_parser_using_declaration (cp_parser* parser, 
14712                              bool access_declaration_p)
14713 {
14714   cp_token *token;
14715   bool typename_p = false;
14716   bool global_scope_p;
14717   tree decl;
14718   tree identifier;
14719   tree qscope;
14720
14721   if (access_declaration_p)
14722     cp_parser_parse_tentatively (parser);
14723   else
14724     {
14725       /* Look for the `using' keyword.  */
14726       cp_parser_require_keyword (parser, RID_USING, RT_USING);
14727       
14728       /* Peek at the next token.  */
14729       token = cp_lexer_peek_token (parser->lexer);
14730       /* See if it's `typename'.  */
14731       if (token->keyword == RID_TYPENAME)
14732         {
14733           /* Remember that we've seen it.  */
14734           typename_p = true;
14735           /* Consume the `typename' token.  */
14736           cp_lexer_consume_token (parser->lexer);
14737         }
14738     }
14739
14740   /* Look for the optional global scope qualification.  */
14741   global_scope_p
14742     = (cp_parser_global_scope_opt (parser,
14743                                    /*current_scope_valid_p=*/false)
14744        != NULL_TREE);
14745
14746   /* If we saw `typename', or didn't see `::', then there must be a
14747      nested-name-specifier present.  */
14748   if (typename_p || !global_scope_p)
14749     qscope = cp_parser_nested_name_specifier (parser, typename_p,
14750                                               /*check_dependency_p=*/true,
14751                                               /*type_p=*/false,
14752                                               /*is_declaration=*/true);
14753   /* Otherwise, we could be in either of the two productions.  In that
14754      case, treat the nested-name-specifier as optional.  */
14755   else
14756     qscope = cp_parser_nested_name_specifier_opt (parser,
14757                                                   /*typename_keyword_p=*/false,
14758                                                   /*check_dependency_p=*/true,
14759                                                   /*type_p=*/false,
14760                                                   /*is_declaration=*/true);
14761   if (!qscope)
14762     qscope = global_namespace;
14763
14764   if (access_declaration_p && cp_parser_error_occurred (parser))
14765     /* Something has already gone wrong; there's no need to parse
14766        further.  Since an error has occurred, the return value of
14767        cp_parser_parse_definitely will be false, as required.  */
14768     return cp_parser_parse_definitely (parser);
14769
14770   token = cp_lexer_peek_token (parser->lexer);
14771   /* Parse the unqualified-id.  */
14772   identifier = cp_parser_unqualified_id (parser,
14773                                          /*template_keyword_p=*/false,
14774                                          /*check_dependency_p=*/true,
14775                                          /*declarator_p=*/true,
14776                                          /*optional_p=*/false);
14777
14778   if (access_declaration_p)
14779     {
14780       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
14781         cp_parser_simulate_error (parser);
14782       if (!cp_parser_parse_definitely (parser))
14783         return false;
14784     }
14785
14786   /* The function we call to handle a using-declaration is different
14787      depending on what scope we are in.  */
14788   if (qscope == error_mark_node || identifier == error_mark_node)
14789     ;
14790   else if (TREE_CODE (identifier) != IDENTIFIER_NODE
14791            && TREE_CODE (identifier) != BIT_NOT_EXPR)
14792     /* [namespace.udecl]
14793
14794        A using declaration shall not name a template-id.  */
14795     error_at (token->location,
14796               "a template-id may not appear in a using-declaration");
14797   else
14798     {
14799       if (at_class_scope_p ())
14800         {
14801           /* Create the USING_DECL.  */
14802           decl = do_class_using_decl (parser->scope, identifier);
14803
14804           if (check_for_bare_parameter_packs (decl))
14805             return false;
14806           else
14807             /* Add it to the list of members in this class.  */
14808             finish_member_declaration (decl);
14809         }
14810       else
14811         {
14812           decl = cp_parser_lookup_name_simple (parser,
14813                                                identifier,
14814                                                token->location);
14815           if (decl == error_mark_node)
14816             cp_parser_name_lookup_error (parser, identifier,
14817                                          decl, NLE_NULL,
14818                                          token->location);
14819           else if (check_for_bare_parameter_packs (decl))
14820             return false;
14821           else if (!at_namespace_scope_p ())
14822             do_local_using_decl (decl, qscope, identifier);
14823           else
14824             do_toplevel_using_decl (decl, qscope, identifier);
14825         }
14826     }
14827
14828   /* Look for the final `;'.  */
14829   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14830   
14831   return true;
14832 }
14833
14834 /* Parse a using-directive.
14835
14836    using-directive:
14837      using namespace :: [opt] nested-name-specifier [opt]
14838        namespace-name ;  */
14839
14840 static void
14841 cp_parser_using_directive (cp_parser* parser)
14842 {
14843   tree namespace_decl;
14844   tree attribs;
14845
14846   /* Look for the `using' keyword.  */
14847   cp_parser_require_keyword (parser, RID_USING, RT_USING);
14848   /* And the `namespace' keyword.  */
14849   cp_parser_require_keyword (parser, RID_NAMESPACE, RT_NAMESPACE);
14850   /* Look for the optional `::' operator.  */
14851   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
14852   /* And the optional nested-name-specifier.  */
14853   cp_parser_nested_name_specifier_opt (parser,
14854                                        /*typename_keyword_p=*/false,
14855                                        /*check_dependency_p=*/true,
14856                                        /*type_p=*/false,
14857                                        /*is_declaration=*/true);
14858   /* Get the namespace being used.  */
14859   namespace_decl = cp_parser_namespace_name (parser);
14860   /* And any specified attributes.  */
14861   attribs = cp_parser_attributes_opt (parser);
14862   /* Update the symbol table.  */
14863   parse_using_directive (namespace_decl, attribs);
14864   /* Look for the final `;'.  */
14865   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
14866 }
14867
14868 /* Parse an asm-definition.
14869
14870    asm-definition:
14871      asm ( string-literal ) ;
14872
14873    GNU Extension:
14874
14875    asm-definition:
14876      asm volatile [opt] ( string-literal ) ;
14877      asm volatile [opt] ( string-literal : asm-operand-list [opt] ) ;
14878      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14879                           : asm-operand-list [opt] ) ;
14880      asm volatile [opt] ( string-literal : asm-operand-list [opt]
14881                           : asm-operand-list [opt]
14882                           : asm-clobber-list [opt] ) ;
14883      asm volatile [opt] goto ( string-literal : : asm-operand-list [opt]
14884                                : asm-clobber-list [opt]
14885                                : asm-goto-list ) ;  */
14886
14887 static void
14888 cp_parser_asm_definition (cp_parser* parser)
14889 {
14890   tree string;
14891   tree outputs = NULL_TREE;
14892   tree inputs = NULL_TREE;
14893   tree clobbers = NULL_TREE;
14894   tree labels = NULL_TREE;
14895   tree asm_stmt;
14896   bool volatile_p = false;
14897   bool extended_p = false;
14898   bool invalid_inputs_p = false;
14899   bool invalid_outputs_p = false;
14900   bool goto_p = false;
14901   required_token missing = RT_NONE;
14902
14903   /* Look for the `asm' keyword.  */
14904   cp_parser_require_keyword (parser, RID_ASM, RT_ASM);
14905   /* See if the next token is `volatile'.  */
14906   if (cp_parser_allow_gnu_extensions_p (parser)
14907       && cp_lexer_next_token_is_keyword (parser->lexer, RID_VOLATILE))
14908     {
14909       /* Remember that we saw the `volatile' keyword.  */
14910       volatile_p = true;
14911       /* Consume the token.  */
14912       cp_lexer_consume_token (parser->lexer);
14913     }
14914   if (cp_parser_allow_gnu_extensions_p (parser)
14915       && parser->in_function_body
14916       && cp_lexer_next_token_is_keyword (parser->lexer, RID_GOTO))
14917     {
14918       /* Remember that we saw the `goto' keyword.  */
14919       goto_p = true;
14920       /* Consume the token.  */
14921       cp_lexer_consume_token (parser->lexer);
14922     }
14923   /* Look for the opening `('.  */
14924   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
14925     return;
14926   /* Look for the string.  */
14927   string = cp_parser_string_literal (parser, false, false);
14928   if (string == error_mark_node)
14929     {
14930       cp_parser_skip_to_closing_parenthesis (parser, true, false,
14931                                              /*consume_paren=*/true);
14932       return;
14933     }
14934
14935   /* If we're allowing GNU extensions, check for the extended assembly
14936      syntax.  Unfortunately, the `:' tokens need not be separated by
14937      a space in C, and so, for compatibility, we tolerate that here
14938      too.  Doing that means that we have to treat the `::' operator as
14939      two `:' tokens.  */
14940   if (cp_parser_allow_gnu_extensions_p (parser)
14941       && parser->in_function_body
14942       && (cp_lexer_next_token_is (parser->lexer, CPP_COLON)
14943           || cp_lexer_next_token_is (parser->lexer, CPP_SCOPE)))
14944     {
14945       bool inputs_p = false;
14946       bool clobbers_p = false;
14947       bool labels_p = false;
14948
14949       /* The extended syntax was used.  */
14950       extended_p = true;
14951
14952       /* Look for outputs.  */
14953       if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14954         {
14955           /* Consume the `:'.  */
14956           cp_lexer_consume_token (parser->lexer);
14957           /* Parse the output-operands.  */
14958           if (cp_lexer_next_token_is_not (parser->lexer,
14959                                           CPP_COLON)
14960               && cp_lexer_next_token_is_not (parser->lexer,
14961                                              CPP_SCOPE)
14962               && cp_lexer_next_token_is_not (parser->lexer,
14963                                              CPP_CLOSE_PAREN)
14964               && !goto_p)
14965             outputs = cp_parser_asm_operand_list (parser);
14966
14967             if (outputs == error_mark_node)
14968               invalid_outputs_p = true;
14969         }
14970       /* If the next token is `::', there are no outputs, and the
14971          next token is the beginning of the inputs.  */
14972       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14973         /* The inputs are coming next.  */
14974         inputs_p = true;
14975
14976       /* Look for inputs.  */
14977       if (inputs_p
14978           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
14979         {
14980           /* Consume the `:' or `::'.  */
14981           cp_lexer_consume_token (parser->lexer);
14982           /* Parse the output-operands.  */
14983           if (cp_lexer_next_token_is_not (parser->lexer,
14984                                           CPP_COLON)
14985               && cp_lexer_next_token_is_not (parser->lexer,
14986                                              CPP_SCOPE)
14987               && cp_lexer_next_token_is_not (parser->lexer,
14988                                              CPP_CLOSE_PAREN))
14989             inputs = cp_parser_asm_operand_list (parser);
14990
14991             if (inputs == error_mark_node)
14992               invalid_inputs_p = true;
14993         }
14994       else if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
14995         /* The clobbers are coming next.  */
14996         clobbers_p = true;
14997
14998       /* Look for clobbers.  */
14999       if (clobbers_p
15000           || cp_lexer_next_token_is (parser->lexer, CPP_COLON))
15001         {
15002           clobbers_p = true;
15003           /* Consume the `:' or `::'.  */
15004           cp_lexer_consume_token (parser->lexer);
15005           /* Parse the clobbers.  */
15006           if (cp_lexer_next_token_is_not (parser->lexer,
15007                                           CPP_COLON)
15008               && cp_lexer_next_token_is_not (parser->lexer,
15009                                              CPP_CLOSE_PAREN))
15010             clobbers = cp_parser_asm_clobber_list (parser);
15011         }
15012       else if (goto_p
15013                && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
15014         /* The labels are coming next.  */
15015         labels_p = true;
15016
15017       /* Look for labels.  */
15018       if (labels_p
15019           || (goto_p && cp_lexer_next_token_is (parser->lexer, CPP_COLON)))
15020         {
15021           labels_p = true;
15022           /* Consume the `:' or `::'.  */
15023           cp_lexer_consume_token (parser->lexer);
15024           /* Parse the labels.  */
15025           labels = cp_parser_asm_label_list (parser);
15026         }
15027
15028       if (goto_p && !labels_p)
15029         missing = clobbers_p ? RT_COLON : RT_COLON_SCOPE;
15030     }
15031   else if (goto_p)
15032     missing = RT_COLON_SCOPE;
15033
15034   /* Look for the closing `)'.  */
15035   if (!cp_parser_require (parser, missing ? CPP_COLON : CPP_CLOSE_PAREN,
15036                           missing ? missing : RT_CLOSE_PAREN))
15037     cp_parser_skip_to_closing_parenthesis (parser, true, false,
15038                                            /*consume_paren=*/true);
15039   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
15040
15041   if (!invalid_inputs_p && !invalid_outputs_p)
15042     {
15043       /* Create the ASM_EXPR.  */
15044       if (parser->in_function_body)
15045         {
15046           asm_stmt = finish_asm_stmt (volatile_p, string, outputs,
15047                                       inputs, clobbers, labels);
15048           /* If the extended syntax was not used, mark the ASM_EXPR.  */
15049           if (!extended_p)
15050             {
15051               tree temp = asm_stmt;
15052               if (TREE_CODE (temp) == CLEANUP_POINT_EXPR)
15053                 temp = TREE_OPERAND (temp, 0);
15054
15055               ASM_INPUT_P (temp) = 1;
15056             }
15057         }
15058       else
15059         cgraph_add_asm_node (string);
15060     }
15061 }
15062
15063 /* Declarators [gram.dcl.decl] */
15064
15065 /* Parse an init-declarator.
15066
15067    init-declarator:
15068      declarator initializer [opt]
15069
15070    GNU Extension:
15071
15072    init-declarator:
15073      declarator asm-specification [opt] attributes [opt] initializer [opt]
15074
15075    function-definition:
15076      decl-specifier-seq [opt] declarator ctor-initializer [opt]
15077        function-body
15078      decl-specifier-seq [opt] declarator function-try-block
15079
15080    GNU Extension:
15081
15082    function-definition:
15083      __extension__ function-definition
15084
15085    The DECL_SPECIFIERS apply to this declarator.  Returns a
15086    representation of the entity declared.  If MEMBER_P is TRUE, then
15087    this declarator appears in a class scope.  The new DECL created by
15088    this declarator is returned.
15089
15090    The CHECKS are access checks that should be performed once we know
15091    what entity is being declared (and, therefore, what classes have
15092    befriended it).
15093
15094    If FUNCTION_DEFINITION_ALLOWED_P then we handle the declarator and
15095    for a function-definition here as well.  If the declarator is a
15096    declarator for a function-definition, *FUNCTION_DEFINITION_P will
15097    be TRUE upon return.  By that point, the function-definition will
15098    have been completely parsed.
15099
15100    FUNCTION_DEFINITION_P may be NULL if FUNCTION_DEFINITION_ALLOWED_P
15101    is FALSE.
15102
15103    If MAYBE_RANGE_FOR_DECL is not NULL, the pointed tree will be set to the
15104    parsed declaration if it is an uninitialized single declarator not followed
15105    by a `;', or to error_mark_node otherwise. Either way, the trailing `;',
15106    if present, will not be consumed.  If returned, this declarator will be
15107    created with SD_INITIALIZED but will not call cp_finish_decl.  */
15108
15109 static tree
15110 cp_parser_init_declarator (cp_parser* parser,
15111                            cp_decl_specifier_seq *decl_specifiers,
15112                            VEC (deferred_access_check,gc)* checks,
15113                            bool function_definition_allowed_p,
15114                            bool member_p,
15115                            int declares_class_or_enum,
15116                            bool* function_definition_p,
15117                            tree* maybe_range_for_decl)
15118 {
15119   cp_token *token = NULL, *asm_spec_start_token = NULL,
15120            *attributes_start_token = NULL;
15121   cp_declarator *declarator;
15122   tree prefix_attributes;
15123   tree attributes;
15124   tree asm_specification;
15125   tree initializer;
15126   tree decl = NULL_TREE;
15127   tree scope;
15128   int is_initialized;
15129   /* Only valid if IS_INITIALIZED is true.  In that case, CPP_EQ if
15130      initialized with "= ..", CPP_OPEN_PAREN if initialized with
15131      "(...)".  */
15132   enum cpp_ttype initialization_kind;
15133   bool is_direct_init = false;
15134   bool is_non_constant_init;
15135   int ctor_dtor_or_conv_p;
15136   bool friend_p;
15137   tree pushed_scope = NULL_TREE;
15138   bool range_for_decl_p = false;
15139
15140   /* Gather the attributes that were provided with the
15141      decl-specifiers.  */
15142   prefix_attributes = decl_specifiers->attributes;
15143
15144   /* Assume that this is not the declarator for a function
15145      definition.  */
15146   if (function_definition_p)
15147     *function_definition_p = false;
15148
15149   /* Defer access checks while parsing the declarator; we cannot know
15150      what names are accessible until we know what is being
15151      declared.  */
15152   resume_deferring_access_checks ();
15153
15154   /* Parse the declarator.  */
15155   token = cp_lexer_peek_token (parser->lexer);
15156   declarator
15157     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
15158                             &ctor_dtor_or_conv_p,
15159                             /*parenthesized_p=*/NULL,
15160                             member_p);
15161   /* Gather up the deferred checks.  */
15162   stop_deferring_access_checks ();
15163
15164   /* If the DECLARATOR was erroneous, there's no need to go
15165      further.  */
15166   if (declarator == cp_error_declarator)
15167     return error_mark_node;
15168
15169   /* Check that the number of template-parameter-lists is OK.  */
15170   if (!cp_parser_check_declarator_template_parameters (parser, declarator,
15171                                                        token->location))
15172     return error_mark_node;
15173
15174   if (declares_class_or_enum & 2)
15175     cp_parser_check_for_definition_in_return_type (declarator,
15176                                                    decl_specifiers->type,
15177                                                    decl_specifiers->type_location);
15178
15179   /* Figure out what scope the entity declared by the DECLARATOR is
15180      located in.  `grokdeclarator' sometimes changes the scope, so
15181      we compute it now.  */
15182   scope = get_scope_of_declarator (declarator);
15183
15184   /* Perform any lookups in the declared type which were thought to be
15185      dependent, but are not in the scope of the declarator.  */
15186   decl_specifiers->type
15187     = maybe_update_decl_type (decl_specifiers->type, scope);
15188
15189   /* If we're allowing GNU extensions, look for an asm-specification
15190      and attributes.  */
15191   if (cp_parser_allow_gnu_extensions_p (parser))
15192     {
15193       /* Look for an asm-specification.  */
15194       asm_spec_start_token = cp_lexer_peek_token (parser->lexer);
15195       asm_specification = cp_parser_asm_specification_opt (parser);
15196       /* And attributes.  */
15197       attributes_start_token = cp_lexer_peek_token (parser->lexer);
15198       attributes = cp_parser_attributes_opt (parser);
15199     }
15200   else
15201     {
15202       asm_specification = NULL_TREE;
15203       attributes = NULL_TREE;
15204     }
15205
15206   /* Peek at the next token.  */
15207   token = cp_lexer_peek_token (parser->lexer);
15208   /* Check to see if the token indicates the start of a
15209      function-definition.  */
15210   if (function_declarator_p (declarator)
15211       && cp_parser_token_starts_function_definition_p (token))
15212     {
15213       if (!function_definition_allowed_p)
15214         {
15215           /* If a function-definition should not appear here, issue an
15216              error message.  */
15217           cp_parser_error (parser,
15218                            "a function-definition is not allowed here");
15219           return error_mark_node;
15220         }
15221       else
15222         {
15223           location_t func_brace_location
15224             = cp_lexer_peek_token (parser->lexer)->location;
15225
15226           /* Neither attributes nor an asm-specification are allowed
15227              on a function-definition.  */
15228           if (asm_specification)
15229             error_at (asm_spec_start_token->location,
15230                       "an asm-specification is not allowed "
15231                       "on a function-definition");
15232           if (attributes)
15233             error_at (attributes_start_token->location,
15234                       "attributes are not allowed on a function-definition");
15235           /* This is a function-definition.  */
15236           *function_definition_p = true;
15237
15238           /* Parse the function definition.  */
15239           if (member_p)
15240             decl = cp_parser_save_member_function_body (parser,
15241                                                         decl_specifiers,
15242                                                         declarator,
15243                                                         prefix_attributes);
15244           else
15245             decl
15246               = (cp_parser_function_definition_from_specifiers_and_declarator
15247                  (parser, decl_specifiers, prefix_attributes, declarator));
15248
15249           if (decl != error_mark_node && DECL_STRUCT_FUNCTION (decl))
15250             {
15251               /* This is where the prologue starts...  */
15252               DECL_STRUCT_FUNCTION (decl)->function_start_locus
15253                 = func_brace_location;
15254             }
15255
15256           return decl;
15257         }
15258     }
15259
15260   /* [dcl.dcl]
15261
15262      Only in function declarations for constructors, destructors, and
15263      type conversions can the decl-specifier-seq be omitted.
15264
15265      We explicitly postpone this check past the point where we handle
15266      function-definitions because we tolerate function-definitions
15267      that are missing their return types in some modes.  */
15268   if (!decl_specifiers->any_specifiers_p && ctor_dtor_or_conv_p <= 0)
15269     {
15270       cp_parser_error (parser,
15271                        "expected constructor, destructor, or type conversion");
15272       return error_mark_node;
15273     }
15274
15275   /* An `=' or an `(', or an '{' in C++0x, indicates an initializer.  */
15276   if (token->type == CPP_EQ
15277       || token->type == CPP_OPEN_PAREN
15278       || token->type == CPP_OPEN_BRACE)
15279     {
15280       is_initialized = SD_INITIALIZED;
15281       initialization_kind = token->type;
15282       if (maybe_range_for_decl)
15283         *maybe_range_for_decl = error_mark_node;
15284
15285       if (token->type == CPP_EQ
15286           && function_declarator_p (declarator))
15287         {
15288           cp_token *t2 = cp_lexer_peek_nth_token (parser->lexer, 2);
15289           if (t2->keyword == RID_DEFAULT)
15290             is_initialized = SD_DEFAULTED;
15291           else if (t2->keyword == RID_DELETE)
15292             is_initialized = SD_DELETED;
15293         }
15294     }
15295   else
15296     {
15297       /* If the init-declarator isn't initialized and isn't followed by a
15298          `,' or `;', it's not a valid init-declarator.  */
15299       if (token->type != CPP_COMMA
15300           && token->type != CPP_SEMICOLON)
15301         {
15302           if (maybe_range_for_decl && *maybe_range_for_decl != error_mark_node)
15303             range_for_decl_p = true;
15304           else
15305             {
15306               cp_parser_error (parser, "expected initializer");
15307               return error_mark_node;
15308             }
15309         }
15310       is_initialized = SD_UNINITIALIZED;
15311       initialization_kind = CPP_EOF;
15312     }
15313
15314   /* Because start_decl has side-effects, we should only call it if we
15315      know we're going ahead.  By this point, we know that we cannot
15316      possibly be looking at any other construct.  */
15317   cp_parser_commit_to_tentative_parse (parser);
15318
15319   /* If the decl specifiers were bad, issue an error now that we're
15320      sure this was intended to be a declarator.  Then continue
15321      declaring the variable(s), as int, to try to cut down on further
15322      errors.  */
15323   if (decl_specifiers->any_specifiers_p
15324       && decl_specifiers->type == error_mark_node)
15325     {
15326       cp_parser_error (parser, "invalid type in declaration");
15327       decl_specifiers->type = integer_type_node;
15328     }
15329
15330   /* Check to see whether or not this declaration is a friend.  */
15331   friend_p = cp_parser_friend_p (decl_specifiers);
15332
15333   /* Enter the newly declared entry in the symbol table.  If we're
15334      processing a declaration in a class-specifier, we wait until
15335      after processing the initializer.  */
15336   if (!member_p)
15337     {
15338       if (parser->in_unbraced_linkage_specification_p)
15339         decl_specifiers->storage_class = sc_extern;
15340       decl = start_decl (declarator, decl_specifiers,
15341                          range_for_decl_p? SD_INITIALIZED : is_initialized,
15342                          attributes, prefix_attributes,
15343                          &pushed_scope);
15344       /* Adjust location of decl if declarator->id_loc is more appropriate:
15345          set, and decl wasn't merged with another decl, in which case its
15346          location would be different from input_location, and more accurate.  */
15347       if (DECL_P (decl)
15348           && declarator->id_loc != UNKNOWN_LOCATION
15349           && DECL_SOURCE_LOCATION (decl) == input_location)
15350         DECL_SOURCE_LOCATION (decl) = declarator->id_loc;
15351     }
15352   else if (scope)
15353     /* Enter the SCOPE.  That way unqualified names appearing in the
15354        initializer will be looked up in SCOPE.  */
15355     pushed_scope = push_scope (scope);
15356
15357   /* Perform deferred access control checks, now that we know in which
15358      SCOPE the declared entity resides.  */
15359   if (!member_p && decl)
15360     {
15361       tree saved_current_function_decl = NULL_TREE;
15362
15363       /* If the entity being declared is a function, pretend that we
15364          are in its scope.  If it is a `friend', it may have access to
15365          things that would not otherwise be accessible.  */
15366       if (TREE_CODE (decl) == FUNCTION_DECL)
15367         {
15368           saved_current_function_decl = current_function_decl;
15369           current_function_decl = decl;
15370         }
15371
15372       /* Perform access checks for template parameters.  */
15373       cp_parser_perform_template_parameter_access_checks (checks);
15374
15375       /* Perform the access control checks for the declarator and the
15376          decl-specifiers.  */
15377       perform_deferred_access_checks ();
15378
15379       /* Restore the saved value.  */
15380       if (TREE_CODE (decl) == FUNCTION_DECL)
15381         current_function_decl = saved_current_function_decl;
15382     }
15383
15384   /* Parse the initializer.  */
15385   initializer = NULL_TREE;
15386   is_direct_init = false;
15387   is_non_constant_init = true;
15388   if (is_initialized)
15389     {
15390       if (function_declarator_p (declarator))
15391         {
15392           cp_token *initializer_start_token = cp_lexer_peek_token (parser->lexer);
15393            if (initialization_kind == CPP_EQ)
15394              initializer = cp_parser_pure_specifier (parser);
15395            else
15396              {
15397                /* If the declaration was erroneous, we don't really
15398                   know what the user intended, so just silently
15399                   consume the initializer.  */
15400                if (decl != error_mark_node)
15401                  error_at (initializer_start_token->location,
15402                            "initializer provided for function");
15403                cp_parser_skip_to_closing_parenthesis (parser,
15404                                                       /*recovering=*/true,
15405                                                       /*or_comma=*/false,
15406                                                       /*consume_paren=*/true);
15407              }
15408         }
15409       else
15410         {
15411           /* We want to record the extra mangling scope for in-class
15412              initializers of class members and initializers of static data
15413              member templates.  The former is a C++0x feature which isn't
15414              implemented yet, and I expect it will involve deferring
15415              parsing of the initializer until end of class as with default
15416              arguments.  So right here we only handle the latter.  */
15417           if (!member_p && processing_template_decl)
15418             start_lambda_scope (decl);
15419           initializer = cp_parser_initializer (parser,
15420                                                &is_direct_init,
15421                                                &is_non_constant_init);
15422           if (!member_p && processing_template_decl)
15423             finish_lambda_scope ();
15424         }
15425     }
15426
15427   /* The old parser allows attributes to appear after a parenthesized
15428      initializer.  Mark Mitchell proposed removing this functionality
15429      on the GCC mailing lists on 2002-08-13.  This parser accepts the
15430      attributes -- but ignores them.  */
15431   if (cp_parser_allow_gnu_extensions_p (parser)
15432       && initialization_kind == CPP_OPEN_PAREN)
15433     if (cp_parser_attributes_opt (parser))
15434       warning (OPT_Wattributes,
15435                "attributes after parenthesized initializer ignored");
15436
15437   /* For an in-class declaration, use `grokfield' to create the
15438      declaration.  */
15439   if (member_p)
15440     {
15441       if (pushed_scope)
15442         {
15443           pop_scope (pushed_scope);
15444           pushed_scope = NULL_TREE;
15445         }
15446       decl = grokfield (declarator, decl_specifiers,
15447                         initializer, !is_non_constant_init,
15448                         /*asmspec=*/NULL_TREE,
15449                         prefix_attributes);
15450       if (decl && TREE_CODE (decl) == FUNCTION_DECL)
15451         cp_parser_save_default_args (parser, decl);
15452     }
15453
15454   /* Finish processing the declaration.  But, skip member
15455      declarations.  */
15456   if (!member_p && decl && decl != error_mark_node && !range_for_decl_p)
15457     {
15458       cp_finish_decl (decl,
15459                       initializer, !is_non_constant_init,
15460                       asm_specification,
15461                       /* If the initializer is in parentheses, then this is
15462                          a direct-initialization, which means that an
15463                          `explicit' constructor is OK.  Otherwise, an
15464                          `explicit' constructor cannot be used.  */
15465                       ((is_direct_init || !is_initialized)
15466                        ? LOOKUP_NORMAL : LOOKUP_IMPLICIT));
15467     }
15468   else if ((cxx_dialect != cxx98) && friend_p
15469            && decl && TREE_CODE (decl) == FUNCTION_DECL)
15470     /* Core issue #226 (C++0x only): A default template-argument
15471        shall not be specified in a friend class template
15472        declaration. */
15473     check_default_tmpl_args (decl, current_template_parms, /*is_primary=*/1, 
15474                              /*is_partial=*/0, /*is_friend_decl=*/1);
15475
15476   if (!friend_p && pushed_scope)
15477     pop_scope (pushed_scope);
15478
15479   return decl;
15480 }
15481
15482 /* Parse a declarator.
15483
15484    declarator:
15485      direct-declarator
15486      ptr-operator declarator
15487
15488    abstract-declarator:
15489      ptr-operator abstract-declarator [opt]
15490      direct-abstract-declarator
15491
15492    GNU Extensions:
15493
15494    declarator:
15495      attributes [opt] direct-declarator
15496      attributes [opt] ptr-operator declarator
15497
15498    abstract-declarator:
15499      attributes [opt] ptr-operator abstract-declarator [opt]
15500      attributes [opt] direct-abstract-declarator
15501
15502    If CTOR_DTOR_OR_CONV_P is not NULL, *CTOR_DTOR_OR_CONV_P is used to
15503    detect constructor, destructor or conversion operators. It is set
15504    to -1 if the declarator is a name, and +1 if it is a
15505    function. Otherwise it is set to zero. Usually you just want to
15506    test for >0, but internally the negative value is used.
15507
15508    (The reason for CTOR_DTOR_OR_CONV_P is that a declaration must have
15509    a decl-specifier-seq unless it declares a constructor, destructor,
15510    or conversion.  It might seem that we could check this condition in
15511    semantic analysis, rather than parsing, but that makes it difficult
15512    to handle something like `f()'.  We want to notice that there are
15513    no decl-specifiers, and therefore realize that this is an
15514    expression, not a declaration.)
15515
15516    If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to true iff
15517    the declarator is a direct-declarator of the form "(...)".
15518
15519    MEMBER_P is true iff this declarator is a member-declarator.  */
15520
15521 static cp_declarator *
15522 cp_parser_declarator (cp_parser* parser,
15523                       cp_parser_declarator_kind dcl_kind,
15524                       int* ctor_dtor_or_conv_p,
15525                       bool* parenthesized_p,
15526                       bool member_p)
15527 {
15528   cp_declarator *declarator;
15529   enum tree_code code;
15530   cp_cv_quals cv_quals;
15531   tree class_type;
15532   tree attributes = NULL_TREE;
15533
15534   /* Assume this is not a constructor, destructor, or type-conversion
15535      operator.  */
15536   if (ctor_dtor_or_conv_p)
15537     *ctor_dtor_or_conv_p = 0;
15538
15539   if (cp_parser_allow_gnu_extensions_p (parser))
15540     attributes = cp_parser_attributes_opt (parser);
15541
15542   /* Check for the ptr-operator production.  */
15543   cp_parser_parse_tentatively (parser);
15544   /* Parse the ptr-operator.  */
15545   code = cp_parser_ptr_operator (parser,
15546                                  &class_type,
15547                                  &cv_quals);
15548   /* If that worked, then we have a ptr-operator.  */
15549   if (cp_parser_parse_definitely (parser))
15550     {
15551       /* If a ptr-operator was found, then this declarator was not
15552          parenthesized.  */
15553       if (parenthesized_p)
15554         *parenthesized_p = true;
15555       /* The dependent declarator is optional if we are parsing an
15556          abstract-declarator.  */
15557       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15558         cp_parser_parse_tentatively (parser);
15559
15560       /* Parse the dependent declarator.  */
15561       declarator = cp_parser_declarator (parser, dcl_kind,
15562                                          /*ctor_dtor_or_conv_p=*/NULL,
15563                                          /*parenthesized_p=*/NULL,
15564                                          /*member_p=*/false);
15565
15566       /* If we are parsing an abstract-declarator, we must handle the
15567          case where the dependent declarator is absent.  */
15568       if (dcl_kind != CP_PARSER_DECLARATOR_NAMED
15569           && !cp_parser_parse_definitely (parser))
15570         declarator = NULL;
15571
15572       declarator = cp_parser_make_indirect_declarator
15573         (code, class_type, cv_quals, declarator);
15574     }
15575   /* Everything else is a direct-declarator.  */
15576   else
15577     {
15578       if (parenthesized_p)
15579         *parenthesized_p = cp_lexer_next_token_is (parser->lexer,
15580                                                    CPP_OPEN_PAREN);
15581       declarator = cp_parser_direct_declarator (parser, dcl_kind,
15582                                                 ctor_dtor_or_conv_p,
15583                                                 member_p);
15584     }
15585
15586   if (attributes && declarator && declarator != cp_error_declarator)
15587     declarator->attributes = attributes;
15588
15589   return declarator;
15590 }
15591
15592 /* Parse a direct-declarator or direct-abstract-declarator.
15593
15594    direct-declarator:
15595      declarator-id
15596      direct-declarator ( parameter-declaration-clause )
15597        cv-qualifier-seq [opt]
15598        exception-specification [opt]
15599      direct-declarator [ constant-expression [opt] ]
15600      ( declarator )
15601
15602    direct-abstract-declarator:
15603      direct-abstract-declarator [opt]
15604        ( parameter-declaration-clause )
15605        cv-qualifier-seq [opt]
15606        exception-specification [opt]
15607      direct-abstract-declarator [opt] [ constant-expression [opt] ]
15608      ( abstract-declarator )
15609
15610    Returns a representation of the declarator.  DCL_KIND is
15611    CP_PARSER_DECLARATOR_ABSTRACT, if we are parsing a
15612    direct-abstract-declarator.  It is CP_PARSER_DECLARATOR_NAMED, if
15613    we are parsing a direct-declarator.  It is
15614    CP_PARSER_DECLARATOR_EITHER, if we can accept either - in the case
15615    of ambiguity we prefer an abstract declarator, as per
15616    [dcl.ambig.res].  CTOR_DTOR_OR_CONV_P and MEMBER_P are as for
15617    cp_parser_declarator.  */
15618
15619 static cp_declarator *
15620 cp_parser_direct_declarator (cp_parser* parser,
15621                              cp_parser_declarator_kind dcl_kind,
15622                              int* ctor_dtor_or_conv_p,
15623                              bool member_p)
15624 {
15625   cp_token *token;
15626   cp_declarator *declarator = NULL;
15627   tree scope = NULL_TREE;
15628   bool saved_default_arg_ok_p = parser->default_arg_ok_p;
15629   bool saved_in_declarator_p = parser->in_declarator_p;
15630   bool first = true;
15631   tree pushed_scope = NULL_TREE;
15632
15633   while (true)
15634     {
15635       /* Peek at the next token.  */
15636       token = cp_lexer_peek_token (parser->lexer);
15637       if (token->type == CPP_OPEN_PAREN)
15638         {
15639           /* This is either a parameter-declaration-clause, or a
15640              parenthesized declarator. When we know we are parsing a
15641              named declarator, it must be a parenthesized declarator
15642              if FIRST is true. For instance, `(int)' is a
15643              parameter-declaration-clause, with an omitted
15644              direct-abstract-declarator. But `((*))', is a
15645              parenthesized abstract declarator. Finally, when T is a
15646              template parameter `(T)' is a
15647              parameter-declaration-clause, and not a parenthesized
15648              named declarator.
15649
15650              We first try and parse a parameter-declaration-clause,
15651              and then try a nested declarator (if FIRST is true).
15652
15653              It is not an error for it not to be a
15654              parameter-declaration-clause, even when FIRST is
15655              false. Consider,
15656
15657                int i (int);
15658                int i (3);
15659
15660              The first is the declaration of a function while the
15661              second is the definition of a variable, including its
15662              initializer.
15663
15664              Having seen only the parenthesis, we cannot know which of
15665              these two alternatives should be selected.  Even more
15666              complex are examples like:
15667
15668                int i (int (a));
15669                int i (int (3));
15670
15671              The former is a function-declaration; the latter is a
15672              variable initialization.
15673
15674              Thus again, we try a parameter-declaration-clause, and if
15675              that fails, we back out and return.  */
15676
15677           if (!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15678             {
15679               tree params;
15680               unsigned saved_num_template_parameter_lists;
15681               bool is_declarator = false;
15682               tree t;
15683
15684               /* In a member-declarator, the only valid interpretation
15685                  of a parenthesis is the start of a
15686                  parameter-declaration-clause.  (It is invalid to
15687                  initialize a static data member with a parenthesized
15688                  initializer; only the "=" form of initialization is
15689                  permitted.)  */
15690               if (!member_p)
15691                 cp_parser_parse_tentatively (parser);
15692
15693               /* Consume the `('.  */
15694               cp_lexer_consume_token (parser->lexer);
15695               if (first)
15696                 {
15697                   /* If this is going to be an abstract declarator, we're
15698                      in a declarator and we can't have default args.  */
15699                   parser->default_arg_ok_p = false;
15700                   parser->in_declarator_p = true;
15701                 }
15702
15703               /* Inside the function parameter list, surrounding
15704                  template-parameter-lists do not apply.  */
15705               saved_num_template_parameter_lists
15706                 = parser->num_template_parameter_lists;
15707               parser->num_template_parameter_lists = 0;
15708
15709               begin_scope (sk_function_parms, NULL_TREE);
15710
15711               /* Parse the parameter-declaration-clause.  */
15712               params = cp_parser_parameter_declaration_clause (parser);
15713
15714               parser->num_template_parameter_lists
15715                 = saved_num_template_parameter_lists;
15716
15717               /* Consume the `)'.  */
15718               cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
15719
15720               /* If all went well, parse the cv-qualifier-seq and the
15721                  exception-specification.  */
15722               if (member_p || cp_parser_parse_definitely (parser))
15723                 {
15724                   cp_cv_quals cv_quals;
15725                   cp_virt_specifiers virt_specifiers;
15726                   tree exception_specification;
15727                   tree late_return;
15728
15729                   is_declarator = true;
15730
15731                   if (ctor_dtor_or_conv_p)
15732                     *ctor_dtor_or_conv_p = *ctor_dtor_or_conv_p < 0;
15733                   first = false;
15734
15735                   /* Parse the cv-qualifier-seq.  */
15736                   cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
15737                   /* And the exception-specification.  */
15738                   exception_specification
15739                     = cp_parser_exception_specification_opt (parser);
15740                   /* Parse the virt-specifier-seq.  */
15741                   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
15742
15743                   late_return = (cp_parser_late_return_type_opt
15744                                  (parser, member_p ? cv_quals : -1));
15745
15746                   /* Create the function-declarator.  */
15747                   declarator = make_call_declarator (declarator,
15748                                                      params,
15749                                                      cv_quals,
15750                                                      virt_specifiers,
15751                                                      exception_specification,
15752                                                      late_return);
15753                   /* Any subsequent parameter lists are to do with
15754                      return type, so are not those of the declared
15755                      function.  */
15756                   parser->default_arg_ok_p = false;
15757                 }
15758
15759               /* Remove the function parms from scope.  */
15760               for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
15761                 pop_binding (DECL_NAME (t), t);
15762               leave_scope();
15763
15764               if (is_declarator)
15765                 /* Repeat the main loop.  */
15766                 continue;
15767             }
15768
15769           /* If this is the first, we can try a parenthesized
15770              declarator.  */
15771           if (first)
15772             {
15773               bool saved_in_type_id_in_expr_p;
15774
15775               parser->default_arg_ok_p = saved_default_arg_ok_p;
15776               parser->in_declarator_p = saved_in_declarator_p;
15777
15778               /* Consume the `('.  */
15779               cp_lexer_consume_token (parser->lexer);
15780               /* Parse the nested declarator.  */
15781               saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
15782               parser->in_type_id_in_expr_p = true;
15783               declarator
15784                 = cp_parser_declarator (parser, dcl_kind, ctor_dtor_or_conv_p,
15785                                         /*parenthesized_p=*/NULL,
15786                                         member_p);
15787               parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
15788               first = false;
15789               /* Expect a `)'.  */
15790               if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
15791                 declarator = cp_error_declarator;
15792               if (declarator == cp_error_declarator)
15793                 break;
15794
15795               goto handle_declarator;
15796             }
15797           /* Otherwise, we must be done.  */
15798           else
15799             break;
15800         }
15801       else if ((!first || dcl_kind != CP_PARSER_DECLARATOR_NAMED)
15802                && token->type == CPP_OPEN_SQUARE)
15803         {
15804           /* Parse an array-declarator.  */
15805           tree bounds;
15806
15807           if (ctor_dtor_or_conv_p)
15808             *ctor_dtor_or_conv_p = 0;
15809
15810           first = false;
15811           parser->default_arg_ok_p = false;
15812           parser->in_declarator_p = true;
15813           /* Consume the `['.  */
15814           cp_lexer_consume_token (parser->lexer);
15815           /* Peek at the next token.  */
15816           token = cp_lexer_peek_token (parser->lexer);
15817           /* If the next token is `]', then there is no
15818              constant-expression.  */
15819           if (token->type != CPP_CLOSE_SQUARE)
15820             {
15821               bool non_constant_p;
15822
15823               bounds
15824                 = cp_parser_constant_expression (parser,
15825                                                  /*allow_non_constant=*/true,
15826                                                  &non_constant_p);
15827               if (!non_constant_p)
15828                 /* OK */;
15829               /* Normally, the array bound must be an integral constant
15830                  expression.  However, as an extension, we allow VLAs
15831                  in function scopes as long as they aren't part of a
15832                  parameter declaration.  */
15833               else if (!parser->in_function_body
15834                        || current_binding_level->kind == sk_function_parms)
15835                 {
15836                   cp_parser_error (parser,
15837                                    "array bound is not an integer constant");
15838                   bounds = error_mark_node;
15839                 }
15840               else if (processing_template_decl && !error_operand_p (bounds))
15841                 {
15842                   /* Remember this wasn't a constant-expression.  */
15843                   bounds = build_nop (TREE_TYPE (bounds), bounds);
15844                   TREE_SIDE_EFFECTS (bounds) = 1;
15845                 }
15846             }
15847           else
15848             bounds = NULL_TREE;
15849           /* Look for the closing `]'.  */
15850           if (!cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE))
15851             {
15852               declarator = cp_error_declarator;
15853               break;
15854             }
15855
15856           declarator = make_array_declarator (declarator, bounds);
15857         }
15858       else if (first && dcl_kind != CP_PARSER_DECLARATOR_ABSTRACT)
15859         {
15860           {
15861             tree qualifying_scope;
15862             tree unqualified_name;
15863             special_function_kind sfk;
15864             bool abstract_ok;
15865             bool pack_expansion_p = false;
15866             cp_token *declarator_id_start_token;
15867
15868             /* Parse a declarator-id */
15869             abstract_ok = (dcl_kind == CP_PARSER_DECLARATOR_EITHER);
15870             if (abstract_ok)
15871               {
15872                 cp_parser_parse_tentatively (parser);
15873
15874                 /* If we see an ellipsis, we should be looking at a
15875                    parameter pack. */
15876                 if (token->type == CPP_ELLIPSIS)
15877                   {
15878                     /* Consume the `...' */
15879                     cp_lexer_consume_token (parser->lexer);
15880
15881                     pack_expansion_p = true;
15882                   }
15883               }
15884
15885             declarator_id_start_token = cp_lexer_peek_token (parser->lexer);
15886             unqualified_name
15887               = cp_parser_declarator_id (parser, /*optional_p=*/abstract_ok);
15888             qualifying_scope = parser->scope;
15889             if (abstract_ok)
15890               {
15891                 bool okay = false;
15892
15893                 if (!unqualified_name && pack_expansion_p)
15894                   {
15895                     /* Check whether an error occurred. */
15896                     okay = !cp_parser_error_occurred (parser);
15897
15898                     /* We already consumed the ellipsis to mark a
15899                        parameter pack, but we have no way to report it,
15900                        so abort the tentative parse. We will be exiting
15901                        immediately anyway. */
15902                     cp_parser_abort_tentative_parse (parser);
15903                   }
15904                 else
15905                   okay = cp_parser_parse_definitely (parser);
15906
15907                 if (!okay)
15908                   unqualified_name = error_mark_node;
15909                 else if (unqualified_name
15910                          && (qualifying_scope
15911                              || (TREE_CODE (unqualified_name)
15912                                  != IDENTIFIER_NODE)))
15913                   {
15914                     cp_parser_error (parser, "expected unqualified-id");
15915                     unqualified_name = error_mark_node;
15916                   }
15917               }
15918
15919             if (!unqualified_name)
15920               return NULL;
15921             if (unqualified_name == error_mark_node)
15922               {
15923                 declarator = cp_error_declarator;
15924                 pack_expansion_p = false;
15925                 declarator->parameter_pack_p = false;
15926                 break;
15927               }
15928
15929             if (qualifying_scope && at_namespace_scope_p ()
15930                 && TREE_CODE (qualifying_scope) == TYPENAME_TYPE)
15931               {
15932                 /* In the declaration of a member of a template class
15933                    outside of the class itself, the SCOPE will sometimes
15934                    be a TYPENAME_TYPE.  For example, given:
15935
15936                    template <typename T>
15937                    int S<T>::R::i = 3;
15938
15939                    the SCOPE will be a TYPENAME_TYPE for `S<T>::R'.  In
15940                    this context, we must resolve S<T>::R to an ordinary
15941                    type, rather than a typename type.
15942
15943                    The reason we normally avoid resolving TYPENAME_TYPEs
15944                    is that a specialization of `S' might render
15945                    `S<T>::R' not a type.  However, if `S' is
15946                    specialized, then this `i' will not be used, so there
15947                    is no harm in resolving the types here.  */
15948                 tree type;
15949
15950                 /* Resolve the TYPENAME_TYPE.  */
15951                 type = resolve_typename_type (qualifying_scope,
15952                                               /*only_current_p=*/false);
15953                 /* If that failed, the declarator is invalid.  */
15954                 if (TREE_CODE (type) == TYPENAME_TYPE)
15955                   {
15956                     if (typedef_variant_p (type))
15957                       error_at (declarator_id_start_token->location,
15958                                 "cannot define member of dependent typedef "
15959                                 "%qT", type);
15960                     else
15961                       error_at (declarator_id_start_token->location,
15962                                 "%<%T::%E%> is not a type",
15963                                 TYPE_CONTEXT (qualifying_scope),
15964                                 TYPE_IDENTIFIER (qualifying_scope));
15965                   }
15966                 qualifying_scope = type;
15967               }
15968
15969             sfk = sfk_none;
15970
15971             if (unqualified_name)
15972               {
15973                 tree class_type;
15974
15975                 if (qualifying_scope
15976                     && CLASS_TYPE_P (qualifying_scope))
15977                   class_type = qualifying_scope;
15978                 else
15979                   class_type = current_class_type;
15980
15981                 if (TREE_CODE (unqualified_name) == TYPE_DECL)
15982                   {
15983                     tree name_type = TREE_TYPE (unqualified_name);
15984                     if (class_type && same_type_p (name_type, class_type))
15985                       {
15986                         if (qualifying_scope
15987                             && CLASSTYPE_USE_TEMPLATE (name_type))
15988                           {
15989                             error_at (declarator_id_start_token->location,
15990                                       "invalid use of constructor as a template");
15991                             inform (declarator_id_start_token->location,
15992                                     "use %<%T::%D%> instead of %<%T::%D%> to "
15993                                     "name the constructor in a qualified name",
15994                                     class_type,
15995                                     DECL_NAME (TYPE_TI_TEMPLATE (class_type)),
15996                                     class_type, name_type);
15997                             declarator = cp_error_declarator;
15998                             break;
15999                           }
16000                         else
16001                           unqualified_name = constructor_name (class_type);
16002                       }
16003                     else
16004                       {
16005                         /* We do not attempt to print the declarator
16006                            here because we do not have enough
16007                            information about its original syntactic
16008                            form.  */
16009                         cp_parser_error (parser, "invalid declarator");
16010                         declarator = cp_error_declarator;
16011                         break;
16012                       }
16013                   }
16014
16015                 if (class_type)
16016                   {
16017                     if (TREE_CODE (unqualified_name) == BIT_NOT_EXPR)
16018                       sfk = sfk_destructor;
16019                     else if (IDENTIFIER_TYPENAME_P (unqualified_name))
16020                       sfk = sfk_conversion;
16021                     else if (/* There's no way to declare a constructor
16022                                 for an anonymous type, even if the type
16023                                 got a name for linkage purposes.  */
16024                              !TYPE_WAS_ANONYMOUS (class_type)
16025                              && constructor_name_p (unqualified_name,
16026                                                     class_type))
16027                       {
16028                         unqualified_name = constructor_name (class_type);
16029                         sfk = sfk_constructor;
16030                       }
16031                     else if (is_overloaded_fn (unqualified_name)
16032                              && DECL_CONSTRUCTOR_P (get_first_fn
16033                                                     (unqualified_name)))
16034                       sfk = sfk_constructor;
16035
16036                     if (ctor_dtor_or_conv_p && sfk != sfk_none)
16037                       *ctor_dtor_or_conv_p = -1;
16038                   }
16039               }
16040             declarator = make_id_declarator (qualifying_scope,
16041                                              unqualified_name,
16042                                              sfk);
16043             declarator->id_loc = token->location;
16044             declarator->parameter_pack_p = pack_expansion_p;
16045
16046             if (pack_expansion_p)
16047               maybe_warn_variadic_templates ();
16048           }
16049
16050         handle_declarator:;
16051           scope = get_scope_of_declarator (declarator);
16052           if (scope)
16053             /* Any names that appear after the declarator-id for a
16054                member are looked up in the containing scope.  */
16055             pushed_scope = push_scope (scope);
16056           parser->in_declarator_p = true;
16057           if ((ctor_dtor_or_conv_p && *ctor_dtor_or_conv_p)
16058               || (declarator && declarator->kind == cdk_id))
16059             /* Default args are only allowed on function
16060                declarations.  */
16061             parser->default_arg_ok_p = saved_default_arg_ok_p;
16062           else
16063             parser->default_arg_ok_p = false;
16064
16065           first = false;
16066         }
16067       /* We're done.  */
16068       else
16069         break;
16070     }
16071
16072   /* For an abstract declarator, we might wind up with nothing at this
16073      point.  That's an error; the declarator is not optional.  */
16074   if (!declarator)
16075     cp_parser_error (parser, "expected declarator");
16076
16077   /* If we entered a scope, we must exit it now.  */
16078   if (pushed_scope)
16079     pop_scope (pushed_scope);
16080
16081   parser->default_arg_ok_p = saved_default_arg_ok_p;
16082   parser->in_declarator_p = saved_in_declarator_p;
16083
16084   return declarator;
16085 }
16086
16087 /* Parse a ptr-operator.
16088
16089    ptr-operator:
16090      * cv-qualifier-seq [opt]
16091      &
16092      :: [opt] nested-name-specifier * cv-qualifier-seq [opt]
16093
16094    GNU Extension:
16095
16096    ptr-operator:
16097      & cv-qualifier-seq [opt]
16098
16099    Returns INDIRECT_REF if a pointer, or pointer-to-member, was used.
16100    Returns ADDR_EXPR if a reference was used, or NON_LVALUE_EXPR for
16101    an rvalue reference. In the case of a pointer-to-member, *TYPE is
16102    filled in with the TYPE containing the member.  *CV_QUALS is
16103    filled in with the cv-qualifier-seq, or TYPE_UNQUALIFIED, if there
16104    are no cv-qualifiers.  Returns ERROR_MARK if an error occurred.
16105    Note that the tree codes returned by this function have nothing
16106    to do with the types of trees that will be eventually be created
16107    to represent the pointer or reference type being parsed. They are
16108    just constants with suggestive names. */
16109 static enum tree_code
16110 cp_parser_ptr_operator (cp_parser* parser,
16111                         tree* type,
16112                         cp_cv_quals *cv_quals)
16113 {
16114   enum tree_code code = ERROR_MARK;
16115   cp_token *token;
16116
16117   /* Assume that it's not a pointer-to-member.  */
16118   *type = NULL_TREE;
16119   /* And that there are no cv-qualifiers.  */
16120   *cv_quals = TYPE_UNQUALIFIED;
16121
16122   /* Peek at the next token.  */
16123   token = cp_lexer_peek_token (parser->lexer);
16124
16125   /* If it's a `*', `&' or `&&' we have a pointer or reference.  */
16126   if (token->type == CPP_MULT)
16127     code = INDIRECT_REF;
16128   else if (token->type == CPP_AND)
16129     code = ADDR_EXPR;
16130   else if ((cxx_dialect != cxx98) &&
16131            token->type == CPP_AND_AND) /* C++0x only */
16132     code = NON_LVALUE_EXPR;
16133
16134   if (code != ERROR_MARK)
16135     {
16136       /* Consume the `*', `&' or `&&'.  */
16137       cp_lexer_consume_token (parser->lexer);
16138
16139       /* A `*' can be followed by a cv-qualifier-seq, and so can a
16140          `&', if we are allowing GNU extensions.  (The only qualifier
16141          that can legally appear after `&' is `restrict', but that is
16142          enforced during semantic analysis.  */
16143       if (code == INDIRECT_REF
16144           || cp_parser_allow_gnu_extensions_p (parser))
16145         *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16146     }
16147   else
16148     {
16149       /* Try the pointer-to-member case.  */
16150       cp_parser_parse_tentatively (parser);
16151       /* Look for the optional `::' operator.  */
16152       cp_parser_global_scope_opt (parser,
16153                                   /*current_scope_valid_p=*/false);
16154       /* Look for the nested-name specifier.  */
16155       token = cp_lexer_peek_token (parser->lexer);
16156       cp_parser_nested_name_specifier (parser,
16157                                        /*typename_keyword_p=*/false,
16158                                        /*check_dependency_p=*/true,
16159                                        /*type_p=*/false,
16160                                        /*is_declaration=*/false);
16161       /* If we found it, and the next token is a `*', then we are
16162          indeed looking at a pointer-to-member operator.  */
16163       if (!cp_parser_error_occurred (parser)
16164           && cp_parser_require (parser, CPP_MULT, RT_MULT))
16165         {
16166           /* Indicate that the `*' operator was used.  */
16167           code = INDIRECT_REF;
16168
16169           if (TREE_CODE (parser->scope) == NAMESPACE_DECL)
16170             error_at (token->location, "%qD is a namespace", parser->scope);
16171           else
16172             {
16173               /* The type of which the member is a member is given by the
16174                  current SCOPE.  */
16175               *type = parser->scope;
16176               /* The next name will not be qualified.  */
16177               parser->scope = NULL_TREE;
16178               parser->qualifying_scope = NULL_TREE;
16179               parser->object_scope = NULL_TREE;
16180               /* Look for the optional cv-qualifier-seq.  */
16181               *cv_quals = cp_parser_cv_qualifier_seq_opt (parser);
16182             }
16183         }
16184       /* If that didn't work we don't have a ptr-operator.  */
16185       if (!cp_parser_parse_definitely (parser))
16186         cp_parser_error (parser, "expected ptr-operator");
16187     }
16188
16189   return code;
16190 }
16191
16192 /* Parse an (optional) cv-qualifier-seq.
16193
16194    cv-qualifier-seq:
16195      cv-qualifier cv-qualifier-seq [opt]
16196
16197    cv-qualifier:
16198      const
16199      volatile
16200
16201    GNU Extension:
16202
16203    cv-qualifier:
16204      __restrict__
16205
16206    Returns a bitmask representing the cv-qualifiers.  */
16207
16208 static cp_cv_quals
16209 cp_parser_cv_qualifier_seq_opt (cp_parser* parser)
16210 {
16211   cp_cv_quals cv_quals = TYPE_UNQUALIFIED;
16212
16213   while (true)
16214     {
16215       cp_token *token;
16216       cp_cv_quals cv_qualifier;
16217
16218       /* Peek at the next token.  */
16219       token = cp_lexer_peek_token (parser->lexer);
16220       /* See if it's a cv-qualifier.  */
16221       switch (token->keyword)
16222         {
16223         case RID_CONST:
16224           cv_qualifier = TYPE_QUAL_CONST;
16225           break;
16226
16227         case RID_VOLATILE:
16228           cv_qualifier = TYPE_QUAL_VOLATILE;
16229           break;
16230
16231         case RID_RESTRICT:
16232           cv_qualifier = TYPE_QUAL_RESTRICT;
16233           break;
16234
16235         default:
16236           cv_qualifier = TYPE_UNQUALIFIED;
16237           break;
16238         }
16239
16240       if (!cv_qualifier)
16241         break;
16242
16243       if (cv_quals & cv_qualifier)
16244         {
16245           error_at (token->location, "duplicate cv-qualifier");
16246           cp_lexer_purge_token (parser->lexer);
16247         }
16248       else
16249         {
16250           cp_lexer_consume_token (parser->lexer);
16251           cv_quals |= cv_qualifier;
16252         }
16253     }
16254
16255   return cv_quals;
16256 }
16257
16258 /* Parse an (optional) virt-specifier-seq.
16259
16260    virt-specifier-seq:
16261      virt-specifier virt-specifier-seq [opt]
16262
16263    virt-specifier:
16264      override
16265      final
16266
16267    Returns a bitmask representing the virt-specifiers.  */
16268
16269 static cp_virt_specifiers
16270 cp_parser_virt_specifier_seq_opt (cp_parser* parser)
16271 {
16272   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
16273
16274   while (true)
16275     {
16276       cp_token *token;
16277       cp_virt_specifiers virt_specifier;
16278
16279       /* Peek at the next token.  */
16280       token = cp_lexer_peek_token (parser->lexer);
16281       /* See if it's a virt-specifier-qualifier.  */
16282       if (token->type != CPP_NAME)
16283         break;
16284       if (!strcmp (IDENTIFIER_POINTER(token->u.value), "override"))
16285         {
16286           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16287           virt_specifier = VIRT_SPEC_OVERRIDE;
16288         }
16289       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "final"))
16290         {
16291           maybe_warn_cpp0x (CPP0X_OVERRIDE_CONTROLS);
16292           virt_specifier = VIRT_SPEC_FINAL;
16293         }
16294       else if (!strcmp (IDENTIFIER_POINTER(token->u.value), "__final"))
16295         {
16296           virt_specifier = VIRT_SPEC_FINAL;
16297         }
16298       else
16299         break;
16300
16301       if (virt_specifiers & virt_specifier)
16302         {
16303           error_at (token->location, "duplicate virt-specifier");
16304           cp_lexer_purge_token (parser->lexer);
16305         }
16306       else
16307         {
16308           cp_lexer_consume_token (parser->lexer);
16309           virt_specifiers |= virt_specifier;
16310         }
16311     }
16312   return virt_specifiers;
16313 }
16314
16315 /* Used by handling of trailing-return-types and NSDMI, in which 'this'
16316    is in scope even though it isn't real.  */
16317
16318 static void
16319 inject_this_parameter (tree ctype, cp_cv_quals quals)
16320 {
16321   tree this_parm;
16322
16323   if (current_class_ptr)
16324     {
16325       /* We don't clear this between NSDMIs.  Is it already what we want?  */
16326       tree type = TREE_TYPE (TREE_TYPE (current_class_ptr));
16327       if (same_type_ignoring_top_level_qualifiers_p (ctype, type)
16328           && cp_type_quals (type) == quals)
16329         return;
16330     }
16331
16332   this_parm = build_this_parm (ctype, quals);
16333   /* Clear this first to avoid shortcut in cp_build_indirect_ref.  */
16334   current_class_ptr = NULL_TREE;
16335   current_class_ref
16336     = cp_build_indirect_ref (this_parm, RO_NULL, tf_warning_or_error);
16337   current_class_ptr = this_parm;
16338 }
16339
16340 /* Parse a late-specified return type, if any.  This is not a separate
16341    non-terminal, but part of a function declarator, which looks like
16342
16343    -> trailing-type-specifier-seq abstract-declarator(opt)
16344
16345    Returns the type indicated by the type-id.
16346
16347    QUALS is either a bitmask of cv_qualifiers or -1 for a non-member
16348    function.  */
16349
16350 static tree
16351 cp_parser_late_return_type_opt (cp_parser* parser, cp_cv_quals quals)
16352 {
16353   cp_token *token;
16354   tree type;
16355
16356   /* Peek at the next token.  */
16357   token = cp_lexer_peek_token (parser->lexer);
16358   /* A late-specified return type is indicated by an initial '->'. */
16359   if (token->type != CPP_DEREF)
16360     return NULL_TREE;
16361
16362   /* Consume the ->.  */
16363   cp_lexer_consume_token (parser->lexer);
16364
16365   if (quals >= 0)
16366     {
16367       /* DR 1207: 'this' is in scope in the trailing return type.  */
16368       gcc_assert (current_class_ptr == NULL_TREE);
16369       inject_this_parameter (current_class_type, quals);
16370     }
16371
16372   type = cp_parser_trailing_type_id (parser);
16373
16374   if (quals >= 0)
16375     current_class_ptr = current_class_ref = NULL_TREE;
16376
16377   return type;
16378 }
16379
16380 /* Parse a declarator-id.
16381
16382    declarator-id:
16383      id-expression
16384      :: [opt] nested-name-specifier [opt] type-name
16385
16386    In the `id-expression' case, the value returned is as for
16387    cp_parser_id_expression if the id-expression was an unqualified-id.
16388    If the id-expression was a qualified-id, then a SCOPE_REF is
16389    returned.  The first operand is the scope (either a NAMESPACE_DECL
16390    or TREE_TYPE), but the second is still just a representation of an
16391    unqualified-id.  */
16392
16393 static tree
16394 cp_parser_declarator_id (cp_parser* parser, bool optional_p)
16395 {
16396   tree id;
16397   /* The expression must be an id-expression.  Assume that qualified
16398      names are the names of types so that:
16399
16400        template <class T>
16401        int S<T>::R::i = 3;
16402
16403      will work; we must treat `S<T>::R' as the name of a type.
16404      Similarly, assume that qualified names are templates, where
16405      required, so that:
16406
16407        template <class T>
16408        int S<T>::R<T>::i = 3;
16409
16410      will work, too.  */
16411   id = cp_parser_id_expression (parser,
16412                                 /*template_keyword_p=*/false,
16413                                 /*check_dependency_p=*/false,
16414                                 /*template_p=*/NULL,
16415                                 /*declarator_p=*/true,
16416                                 optional_p);
16417   if (id && BASELINK_P (id))
16418     id = BASELINK_FUNCTIONS (id);
16419   return id;
16420 }
16421
16422 /* Parse a type-id.
16423
16424    type-id:
16425      type-specifier-seq abstract-declarator [opt]
16426
16427    Returns the TYPE specified.  */
16428
16429 static tree
16430 cp_parser_type_id_1 (cp_parser* parser, bool is_template_arg,
16431                      bool is_trailing_return)
16432 {
16433   cp_decl_specifier_seq type_specifier_seq;
16434   cp_declarator *abstract_declarator;
16435
16436   /* Parse the type-specifier-seq.  */
16437   cp_parser_type_specifier_seq (parser, /*is_declaration=*/false,
16438                                 is_trailing_return,
16439                                 &type_specifier_seq);
16440   if (type_specifier_seq.type == error_mark_node)
16441     return error_mark_node;
16442
16443   /* There might or might not be an abstract declarator.  */
16444   cp_parser_parse_tentatively (parser);
16445   /* Look for the declarator.  */
16446   abstract_declarator
16447     = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_ABSTRACT, NULL,
16448                             /*parenthesized_p=*/NULL,
16449                             /*member_p=*/false);
16450   /* Check to see if there really was a declarator.  */
16451   if (!cp_parser_parse_definitely (parser))
16452     abstract_declarator = NULL;
16453
16454   if (type_specifier_seq.type
16455       && type_uses_auto (type_specifier_seq.type))
16456     {
16457       /* A type-id with type 'auto' is only ok if the abstract declarator
16458          is a function declarator with a late-specified return type.  */
16459       if (abstract_declarator
16460           && abstract_declarator->kind == cdk_function
16461           && abstract_declarator->u.function.late_return_type)
16462         /* OK */;
16463       else
16464         {
16465           error ("invalid use of %<auto%>");
16466           return error_mark_node;
16467         }
16468     }
16469   
16470   return groktypename (&type_specifier_seq, abstract_declarator,
16471                        is_template_arg);
16472 }
16473
16474 static tree cp_parser_type_id (cp_parser *parser)
16475 {
16476   return cp_parser_type_id_1 (parser, false, false);
16477 }
16478
16479 static tree cp_parser_template_type_arg (cp_parser *parser)
16480 {
16481   tree r;
16482   const char *saved_message = parser->type_definition_forbidden_message;
16483   parser->type_definition_forbidden_message
16484     = G_("types may not be defined in template arguments");
16485   r = cp_parser_type_id_1 (parser, true, false);
16486   parser->type_definition_forbidden_message = saved_message;
16487   return r;
16488 }
16489
16490 static tree cp_parser_trailing_type_id (cp_parser *parser)
16491 {
16492   return cp_parser_type_id_1 (parser, false, true);
16493 }
16494
16495 /* Parse a type-specifier-seq.
16496
16497    type-specifier-seq:
16498      type-specifier type-specifier-seq [opt]
16499
16500    GNU extension:
16501
16502    type-specifier-seq:
16503      attributes type-specifier-seq [opt]
16504
16505    If IS_DECLARATION is true, we are at the start of a "condition" or
16506    exception-declaration, so we might be followed by a declarator-id.
16507
16508    If IS_TRAILING_RETURN is true, we are in a trailing-return-type,
16509    i.e. we've just seen "->".
16510
16511    Sets *TYPE_SPECIFIER_SEQ to represent the sequence.  */
16512
16513 static void
16514 cp_parser_type_specifier_seq (cp_parser* parser,
16515                               bool is_declaration,
16516                               bool is_trailing_return,
16517                               cp_decl_specifier_seq *type_specifier_seq)
16518 {
16519   bool seen_type_specifier = false;
16520   cp_parser_flags flags = CP_PARSER_FLAGS_OPTIONAL;
16521   cp_token *start_token = NULL;
16522
16523   /* Clear the TYPE_SPECIFIER_SEQ.  */
16524   clear_decl_specs (type_specifier_seq);
16525
16526   /* In the context of a trailing return type, enum E { } is an
16527      elaborated-type-specifier followed by a function-body, not an
16528      enum-specifier.  */
16529   if (is_trailing_return)
16530     flags |= CP_PARSER_FLAGS_NO_TYPE_DEFINITIONS;
16531
16532   /* Parse the type-specifiers and attributes.  */
16533   while (true)
16534     {
16535       tree type_specifier;
16536       bool is_cv_qualifier;
16537
16538       /* Check for attributes first.  */
16539       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
16540         {
16541           type_specifier_seq->attributes =
16542             chainon (type_specifier_seq->attributes,
16543                      cp_parser_attributes_opt (parser));
16544           continue;
16545         }
16546
16547       /* record the token of the beginning of the type specifier seq,
16548          for error reporting purposes*/
16549      if (!start_token)
16550        start_token = cp_lexer_peek_token (parser->lexer);
16551
16552       /* Look for the type-specifier.  */
16553       type_specifier = cp_parser_type_specifier (parser,
16554                                                  flags,
16555                                                  type_specifier_seq,
16556                                                  /*is_declaration=*/false,
16557                                                  NULL,
16558                                                  &is_cv_qualifier);
16559       if (!type_specifier)
16560         {
16561           /* If the first type-specifier could not be found, this is not a
16562              type-specifier-seq at all.  */
16563           if (!seen_type_specifier)
16564             {
16565               cp_parser_error (parser, "expected type-specifier");
16566               type_specifier_seq->type = error_mark_node;
16567               return;
16568             }
16569           /* If subsequent type-specifiers could not be found, the
16570              type-specifier-seq is complete.  */
16571           break;
16572         }
16573
16574       seen_type_specifier = true;
16575       /* The standard says that a condition can be:
16576
16577             type-specifier-seq declarator = assignment-expression
16578
16579          However, given:
16580
16581            struct S {};
16582            if (int S = ...)
16583
16584          we should treat the "S" as a declarator, not as a
16585          type-specifier.  The standard doesn't say that explicitly for
16586          type-specifier-seq, but it does say that for
16587          decl-specifier-seq in an ordinary declaration.  Perhaps it
16588          would be clearer just to allow a decl-specifier-seq here, and
16589          then add a semantic restriction that if any decl-specifiers
16590          that are not type-specifiers appear, the program is invalid.  */
16591       if (is_declaration && !is_cv_qualifier)
16592         flags |= CP_PARSER_FLAGS_NO_USER_DEFINED_TYPES;
16593     }
16594
16595   cp_parser_check_decl_spec (type_specifier_seq, start_token->location);
16596 }
16597
16598 /* Parse a parameter-declaration-clause.
16599
16600    parameter-declaration-clause:
16601      parameter-declaration-list [opt] ... [opt]
16602      parameter-declaration-list , ...
16603
16604    Returns a representation for the parameter declarations.  A return
16605    value of NULL indicates a parameter-declaration-clause consisting
16606    only of an ellipsis.  */
16607
16608 static tree
16609 cp_parser_parameter_declaration_clause (cp_parser* parser)
16610 {
16611   tree parameters;
16612   cp_token *token;
16613   bool ellipsis_p;
16614   bool is_error;
16615
16616   /* Peek at the next token.  */
16617   token = cp_lexer_peek_token (parser->lexer);
16618   /* Check for trivial parameter-declaration-clauses.  */
16619   if (token->type == CPP_ELLIPSIS)
16620     {
16621       /* Consume the `...' token.  */
16622       cp_lexer_consume_token (parser->lexer);
16623       return NULL_TREE;
16624     }
16625   else if (token->type == CPP_CLOSE_PAREN)
16626     /* There are no parameters.  */
16627     {
16628 #ifndef NO_IMPLICIT_EXTERN_C
16629       if (in_system_header && current_class_type == NULL
16630           && current_lang_name == lang_name_c)
16631         return NULL_TREE;
16632       else
16633 #endif
16634         return void_list_node;
16635     }
16636   /* Check for `(void)', too, which is a special case.  */
16637   else if (token->keyword == RID_VOID
16638            && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
16639                == CPP_CLOSE_PAREN))
16640     {
16641       /* Consume the `void' token.  */
16642       cp_lexer_consume_token (parser->lexer);
16643       /* There are no parameters.  */
16644       return void_list_node;
16645     }
16646
16647   /* Parse the parameter-declaration-list.  */
16648   parameters = cp_parser_parameter_declaration_list (parser, &is_error);
16649   /* If a parse error occurred while parsing the
16650      parameter-declaration-list, then the entire
16651      parameter-declaration-clause is erroneous.  */
16652   if (is_error)
16653     return NULL;
16654
16655   /* Peek at the next token.  */
16656   token = cp_lexer_peek_token (parser->lexer);
16657   /* If it's a `,', the clause should terminate with an ellipsis.  */
16658   if (token->type == CPP_COMMA)
16659     {
16660       /* Consume the `,'.  */
16661       cp_lexer_consume_token (parser->lexer);
16662       /* Expect an ellipsis.  */
16663       ellipsis_p
16664         = (cp_parser_require (parser, CPP_ELLIPSIS, RT_ELLIPSIS) != NULL);
16665     }
16666   /* It might also be `...' if the optional trailing `,' was
16667      omitted.  */
16668   else if (token->type == CPP_ELLIPSIS)
16669     {
16670       /* Consume the `...' token.  */
16671       cp_lexer_consume_token (parser->lexer);
16672       /* And remember that we saw it.  */
16673       ellipsis_p = true;
16674     }
16675   else
16676     ellipsis_p = false;
16677
16678   /* Finish the parameter list.  */
16679   if (!ellipsis_p)
16680     parameters = chainon (parameters, void_list_node);
16681
16682   return parameters;
16683 }
16684
16685 /* Parse a parameter-declaration-list.
16686
16687    parameter-declaration-list:
16688      parameter-declaration
16689      parameter-declaration-list , parameter-declaration
16690
16691    Returns a representation of the parameter-declaration-list, as for
16692    cp_parser_parameter_declaration_clause.  However, the
16693    `void_list_node' is never appended to the list.  Upon return,
16694    *IS_ERROR will be true iff an error occurred.  */
16695
16696 static tree
16697 cp_parser_parameter_declaration_list (cp_parser* parser, bool *is_error)
16698 {
16699   tree parameters = NULL_TREE;
16700   tree *tail = &parameters; 
16701   bool saved_in_unbraced_linkage_specification_p;
16702   int index = 0;
16703
16704   /* Assume all will go well.  */
16705   *is_error = false;
16706   /* The special considerations that apply to a function within an
16707      unbraced linkage specifications do not apply to the parameters
16708      to the function.  */
16709   saved_in_unbraced_linkage_specification_p 
16710     = parser->in_unbraced_linkage_specification_p;
16711   parser->in_unbraced_linkage_specification_p = false;
16712
16713   /* Look for more parameters.  */
16714   while (true)
16715     {
16716       cp_parameter_declarator *parameter;
16717       tree decl = error_mark_node;
16718       bool parenthesized_p = false;
16719       /* Parse the parameter.  */
16720       parameter
16721         = cp_parser_parameter_declaration (parser,
16722                                            /*template_parm_p=*/false,
16723                                            &parenthesized_p);
16724
16725       /* We don't know yet if the enclosing context is deprecated, so wait
16726          and warn in grokparms if appropriate.  */
16727       deprecated_state = DEPRECATED_SUPPRESS;
16728
16729       if (parameter)
16730         decl = grokdeclarator (parameter->declarator,
16731                                &parameter->decl_specifiers,
16732                                PARM,
16733                                parameter->default_argument != NULL_TREE,
16734                                &parameter->decl_specifiers.attributes);
16735
16736       deprecated_state = DEPRECATED_NORMAL;
16737
16738       /* If a parse error occurred parsing the parameter declaration,
16739          then the entire parameter-declaration-list is erroneous.  */
16740       if (decl == error_mark_node)
16741         {
16742           *is_error = true;
16743           parameters = error_mark_node;
16744           break;
16745         }
16746
16747       if (parameter->decl_specifiers.attributes)
16748         cplus_decl_attributes (&decl,
16749                                parameter->decl_specifiers.attributes,
16750                                0);
16751       if (DECL_NAME (decl))
16752         decl = pushdecl (decl);
16753
16754       if (decl != error_mark_node)
16755         {
16756           retrofit_lang_decl (decl);
16757           DECL_PARM_INDEX (decl) = ++index;
16758           DECL_PARM_LEVEL (decl) = function_parm_depth ();
16759         }
16760
16761       /* Add the new parameter to the list.  */
16762       *tail = build_tree_list (parameter->default_argument, decl);
16763       tail = &TREE_CHAIN (*tail);
16764
16765       /* Peek at the next token.  */
16766       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN)
16767           || cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS)
16768           /* These are for Objective-C++ */
16769           || cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
16770           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
16771         /* The parameter-declaration-list is complete.  */
16772         break;
16773       else if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
16774         {
16775           cp_token *token;
16776
16777           /* Peek at the next token.  */
16778           token = cp_lexer_peek_nth_token (parser->lexer, 2);
16779           /* If it's an ellipsis, then the list is complete.  */
16780           if (token->type == CPP_ELLIPSIS)
16781             break;
16782           /* Otherwise, there must be more parameters.  Consume the
16783              `,'.  */
16784           cp_lexer_consume_token (parser->lexer);
16785           /* When parsing something like:
16786
16787                 int i(float f, double d)
16788
16789              we can tell after seeing the declaration for "f" that we
16790              are not looking at an initialization of a variable "i",
16791              but rather at the declaration of a function "i".
16792
16793              Due to the fact that the parsing of template arguments
16794              (as specified to a template-id) requires backtracking we
16795              cannot use this technique when inside a template argument
16796              list.  */
16797           if (!parser->in_template_argument_list_p
16798               && !parser->in_type_id_in_expr_p
16799               && cp_parser_uncommitted_to_tentative_parse_p (parser)
16800               /* However, a parameter-declaration of the form
16801                  "foat(f)" (which is a valid declaration of a
16802                  parameter "f") can also be interpreted as an
16803                  expression (the conversion of "f" to "float").  */
16804               && !parenthesized_p)
16805             cp_parser_commit_to_tentative_parse (parser);
16806         }
16807       else
16808         {
16809           cp_parser_error (parser, "expected %<,%> or %<...%>");
16810           if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
16811             cp_parser_skip_to_closing_parenthesis (parser,
16812                                                    /*recovering=*/true,
16813                                                    /*or_comma=*/false,
16814                                                    /*consume_paren=*/false);
16815           break;
16816         }
16817     }
16818
16819   parser->in_unbraced_linkage_specification_p
16820     = saved_in_unbraced_linkage_specification_p;
16821
16822   return parameters;
16823 }
16824
16825 /* Parse a parameter declaration.
16826
16827    parameter-declaration:
16828      decl-specifier-seq ... [opt] declarator
16829      decl-specifier-seq declarator = assignment-expression
16830      decl-specifier-seq ... [opt] abstract-declarator [opt]
16831      decl-specifier-seq abstract-declarator [opt] = assignment-expression
16832
16833    If TEMPLATE_PARM_P is TRUE, then this parameter-declaration
16834    declares a template parameter.  (In that case, a non-nested `>'
16835    token encountered during the parsing of the assignment-expression
16836    is not interpreted as a greater-than operator.)
16837
16838    Returns a representation of the parameter, or NULL if an error
16839    occurs.  If PARENTHESIZED_P is non-NULL, *PARENTHESIZED_P is set to
16840    true iff the declarator is of the form "(p)".  */
16841
16842 static cp_parameter_declarator *
16843 cp_parser_parameter_declaration (cp_parser *parser,
16844                                  bool template_parm_p,
16845                                  bool *parenthesized_p)
16846 {
16847   int declares_class_or_enum;
16848   cp_decl_specifier_seq decl_specifiers;
16849   cp_declarator *declarator;
16850   tree default_argument;
16851   cp_token *token = NULL, *declarator_token_start = NULL;
16852   const char *saved_message;
16853
16854   /* In a template parameter, `>' is not an operator.
16855
16856      [temp.param]
16857
16858      When parsing a default template-argument for a non-type
16859      template-parameter, the first non-nested `>' is taken as the end
16860      of the template parameter-list rather than a greater-than
16861      operator.  */
16862
16863   /* Type definitions may not appear in parameter types.  */
16864   saved_message = parser->type_definition_forbidden_message;
16865   parser->type_definition_forbidden_message
16866     = G_("types may not be defined in parameter types");
16867
16868   /* Parse the declaration-specifiers.  */
16869   cp_parser_decl_specifier_seq (parser,
16870                                 CP_PARSER_FLAGS_NONE,
16871                                 &decl_specifiers,
16872                                 &declares_class_or_enum);
16873
16874   /* Complain about missing 'typename' or other invalid type names.  */
16875   if (!decl_specifiers.any_type_specifiers_p)
16876     cp_parser_parse_and_diagnose_invalid_type_name (parser);
16877
16878   /* If an error occurred, there's no reason to attempt to parse the
16879      rest of the declaration.  */
16880   if (cp_parser_error_occurred (parser))
16881     {
16882       parser->type_definition_forbidden_message = saved_message;
16883       return NULL;
16884     }
16885
16886   /* Peek at the next token.  */
16887   token = cp_lexer_peek_token (parser->lexer);
16888
16889   /* If the next token is a `)', `,', `=', `>', or `...', then there
16890      is no declarator. However, when variadic templates are enabled,
16891      there may be a declarator following `...'.  */
16892   if (token->type == CPP_CLOSE_PAREN
16893       || token->type == CPP_COMMA
16894       || token->type == CPP_EQ
16895       || token->type == CPP_GREATER)
16896     {
16897       declarator = NULL;
16898       if (parenthesized_p)
16899         *parenthesized_p = false;
16900     }
16901   /* Otherwise, there should be a declarator.  */
16902   else
16903     {
16904       bool saved_default_arg_ok_p = parser->default_arg_ok_p;
16905       parser->default_arg_ok_p = false;
16906
16907       /* After seeing a decl-specifier-seq, if the next token is not a
16908          "(", there is no possibility that the code is a valid
16909          expression.  Therefore, if parsing tentatively, we commit at
16910          this point.  */
16911       if (!parser->in_template_argument_list_p
16912           /* In an expression context, having seen:
16913
16914                (int((char ...
16915
16916              we cannot be sure whether we are looking at a
16917              function-type (taking a "char" as a parameter) or a cast
16918              of some object of type "char" to "int".  */
16919           && !parser->in_type_id_in_expr_p
16920           && cp_parser_uncommitted_to_tentative_parse_p (parser)
16921           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
16922           && cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_PAREN))
16923         cp_parser_commit_to_tentative_parse (parser);
16924       /* Parse the declarator.  */
16925       declarator_token_start = token;
16926       declarator = cp_parser_declarator (parser,
16927                                          CP_PARSER_DECLARATOR_EITHER,
16928                                          /*ctor_dtor_or_conv_p=*/NULL,
16929                                          parenthesized_p,
16930                                          /*member_p=*/false);
16931       parser->default_arg_ok_p = saved_default_arg_ok_p;
16932       /* After the declarator, allow more attributes.  */
16933       decl_specifiers.attributes
16934         = chainon (decl_specifiers.attributes,
16935                    cp_parser_attributes_opt (parser));
16936     }
16937
16938   /* If the next token is an ellipsis, and we have not seen a
16939      declarator name, and the type of the declarator contains parameter
16940      packs but it is not a TYPE_PACK_EXPANSION, then we actually have
16941      a parameter pack expansion expression. Otherwise, leave the
16942      ellipsis for a C-style variadic function. */
16943   token = cp_lexer_peek_token (parser->lexer);
16944   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
16945     {
16946       tree type = decl_specifiers.type;
16947
16948       if (type && DECL_P (type))
16949         type = TREE_TYPE (type);
16950
16951       if (type
16952           && TREE_CODE (type) != TYPE_PACK_EXPANSION
16953           && declarator_can_be_parameter_pack (declarator)
16954           && (!declarator || !declarator->parameter_pack_p)
16955           && uses_parameter_packs (type))
16956         {
16957           /* Consume the `...'. */
16958           cp_lexer_consume_token (parser->lexer);
16959           maybe_warn_variadic_templates ();
16960           
16961           /* Build a pack expansion type */
16962           if (declarator)
16963             declarator->parameter_pack_p = true;
16964           else
16965             decl_specifiers.type = make_pack_expansion (type);
16966         }
16967     }
16968
16969   /* The restriction on defining new types applies only to the type
16970      of the parameter, not to the default argument.  */
16971   parser->type_definition_forbidden_message = saved_message;
16972
16973   /* If the next token is `=', then process a default argument.  */
16974   if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
16975     {
16976       /* If we are defining a class, then the tokens that make up the
16977          default argument must be saved and processed later.  */
16978       if (!template_parm_p && at_class_scope_p ()
16979           && TYPE_BEING_DEFINED (current_class_type)
16980           && !LAMBDA_TYPE_P (current_class_type))
16981         {
16982           unsigned depth = 0;
16983           int maybe_template_id = 0;
16984           cp_token *first_token;
16985           cp_token *token;
16986
16987           /* Add tokens until we have processed the entire default
16988              argument.  We add the range [first_token, token).  */
16989           first_token = cp_lexer_peek_token (parser->lexer);
16990           while (true)
16991             {
16992               bool done = false;
16993
16994               /* Peek at the next token.  */
16995               token = cp_lexer_peek_token (parser->lexer);
16996               /* What we do depends on what token we have.  */
16997               switch (token->type)
16998                 {
16999                   /* In valid code, a default argument must be
17000                      immediately followed by a `,' `)', or `...'.  */
17001                 case CPP_COMMA:
17002                   if (depth == 0 && maybe_template_id)
17003                     {
17004                       /* If we've seen a '<', we might be in a
17005                          template-argument-list.  Until Core issue 325 is
17006                          resolved, we don't know how this situation ought
17007                          to be handled, so try to DTRT.  We check whether
17008                          what comes after the comma is a valid parameter
17009                          declaration list.  If it is, then the comma ends
17010                          the default argument; otherwise the default
17011                          argument continues.  */
17012                       bool error = false;
17013                       tree t;
17014
17015                       /* Set ITALP so cp_parser_parameter_declaration_list
17016                          doesn't decide to commit to this parse.  */
17017                       bool saved_italp = parser->in_template_argument_list_p;
17018                       parser->in_template_argument_list_p = true;
17019
17020                       cp_parser_parse_tentatively (parser);
17021                       cp_lexer_consume_token (parser->lexer);
17022                       begin_scope (sk_function_parms, NULL_TREE);
17023                       cp_parser_parameter_declaration_list (parser, &error);
17024                       for (t = current_binding_level->names; t; t = DECL_CHAIN (t))
17025                         pop_binding (DECL_NAME (t), t);
17026                       leave_scope ();
17027                       if (!cp_parser_error_occurred (parser) && !error)
17028                         done = true;
17029                       cp_parser_abort_tentative_parse (parser);
17030
17031                       parser->in_template_argument_list_p = saved_italp;
17032                       break;
17033                     }
17034                 case CPP_CLOSE_PAREN:
17035                 case CPP_ELLIPSIS:
17036                   /* If we run into a non-nested `;', `}', or `]',
17037                      then the code is invalid -- but the default
17038                      argument is certainly over.  */
17039                 case CPP_SEMICOLON:
17040                 case CPP_CLOSE_BRACE:
17041                 case CPP_CLOSE_SQUARE:
17042                   if (depth == 0)
17043                     done = true;
17044                   /* Update DEPTH, if necessary.  */
17045                   else if (token->type == CPP_CLOSE_PAREN
17046                            || token->type == CPP_CLOSE_BRACE
17047                            || token->type == CPP_CLOSE_SQUARE)
17048                     --depth;
17049                   break;
17050
17051                 case CPP_OPEN_PAREN:
17052                 case CPP_OPEN_SQUARE:
17053                 case CPP_OPEN_BRACE:
17054                   ++depth;
17055                   break;
17056
17057                 case CPP_LESS:
17058                   if (depth == 0)
17059                     /* This might be the comparison operator, or it might
17060                        start a template argument list.  */
17061                     ++maybe_template_id;
17062                   break;
17063
17064                 case CPP_RSHIFT:
17065                   if (cxx_dialect == cxx98)
17066                     break;
17067                   /* Fall through for C++0x, which treats the `>>'
17068                      operator like two `>' tokens in certain
17069                      cases.  */
17070
17071                 case CPP_GREATER:
17072                   if (depth == 0)
17073                     {
17074                       /* This might be an operator, or it might close a
17075                          template argument list.  But if a previous '<'
17076                          started a template argument list, this will have
17077                          closed it, so we can't be in one anymore.  */
17078                       maybe_template_id -= 1 + (token->type == CPP_RSHIFT);
17079                       if (maybe_template_id < 0)
17080                         maybe_template_id = 0;
17081                     }
17082                   break;
17083
17084                   /* If we run out of tokens, issue an error message.  */
17085                 case CPP_EOF:
17086                 case CPP_PRAGMA_EOL:
17087                   error_at (token->location, "file ends in default argument");
17088                   done = true;
17089                   break;
17090
17091                 case CPP_NAME:
17092                 case CPP_SCOPE:
17093                   /* In these cases, we should look for template-ids.
17094                      For example, if the default argument is
17095                      `X<int, double>()', we need to do name lookup to
17096                      figure out whether or not `X' is a template; if
17097                      so, the `,' does not end the default argument.
17098
17099                      That is not yet done.  */
17100                   break;
17101
17102                 default:
17103                   break;
17104                 }
17105
17106               /* If we've reached the end, stop.  */
17107               if (done)
17108                 break;
17109
17110               /* Add the token to the token block.  */
17111               token = cp_lexer_consume_token (parser->lexer);
17112             }
17113
17114           /* Create a DEFAULT_ARG to represent the unparsed default
17115              argument.  */
17116           default_argument = make_node (DEFAULT_ARG);
17117           DEFARG_TOKENS (default_argument)
17118             = cp_token_cache_new (first_token, token);
17119           DEFARG_INSTANTIATIONS (default_argument) = NULL;
17120         }
17121       /* Outside of a class definition, we can just parse the
17122          assignment-expression.  */
17123       else
17124         {
17125           token = cp_lexer_peek_token (parser->lexer);
17126           default_argument 
17127             = cp_parser_default_argument (parser, template_parm_p);
17128         }
17129
17130       if (!parser->default_arg_ok_p)
17131         {
17132           if (flag_permissive)
17133             warning (0, "deprecated use of default argument for parameter of non-function");
17134           else
17135             {
17136               error_at (token->location,
17137                         "default arguments are only "
17138                         "permitted for function parameters");
17139               default_argument = NULL_TREE;
17140             }
17141         }
17142       else if ((declarator && declarator->parameter_pack_p)
17143                || (decl_specifiers.type
17144                    && PACK_EXPANSION_P (decl_specifiers.type)))
17145         {
17146           /* Find the name of the parameter pack.  */     
17147           cp_declarator *id_declarator = declarator;
17148           while (id_declarator && id_declarator->kind != cdk_id)
17149             id_declarator = id_declarator->declarator;
17150           
17151           if (id_declarator && id_declarator->kind == cdk_id)
17152             error_at (declarator_token_start->location,
17153                       template_parm_p
17154                       ? G_("template parameter pack %qD "
17155                            "cannot have a default argument")
17156                       : G_("parameter pack %qD cannot have "
17157                            "a default argument"),
17158                       id_declarator->u.id.unqualified_name);
17159           else
17160             error_at (declarator_token_start->location,
17161                       template_parm_p
17162                       ? G_("template parameter pack cannot have "
17163                            "a default argument")
17164                       : G_("parameter pack cannot have a "
17165                            "default argument"));
17166
17167           default_argument = NULL_TREE;
17168         }
17169     }
17170   else
17171     default_argument = NULL_TREE;
17172
17173   return make_parameter_declarator (&decl_specifiers,
17174                                     declarator,
17175                                     default_argument);
17176 }
17177
17178 /* Parse a default argument and return it.
17179
17180    TEMPLATE_PARM_P is true if this is a default argument for a
17181    non-type template parameter.  */
17182 static tree
17183 cp_parser_default_argument (cp_parser *parser, bool template_parm_p)
17184 {
17185   tree default_argument = NULL_TREE;
17186   bool saved_greater_than_is_operator_p;
17187   bool saved_local_variables_forbidden_p;
17188   bool non_constant_p, is_direct_init;
17189
17190   /* Make sure that PARSER->GREATER_THAN_IS_OPERATOR_P is
17191      set correctly.  */
17192   saved_greater_than_is_operator_p = parser->greater_than_is_operator_p;
17193   parser->greater_than_is_operator_p = !template_parm_p;
17194   /* Local variable names (and the `this' keyword) may not
17195      appear in a default argument.  */
17196   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
17197   parser->local_variables_forbidden_p = true;
17198   /* Parse the assignment-expression.  */
17199   if (template_parm_p)
17200     push_deferring_access_checks (dk_no_deferred);
17201   default_argument
17202     = cp_parser_initializer (parser, &is_direct_init, &non_constant_p);
17203   if (BRACE_ENCLOSED_INITIALIZER_P (default_argument))
17204     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17205   if (template_parm_p)
17206     pop_deferring_access_checks ();
17207   parser->greater_than_is_operator_p = saved_greater_than_is_operator_p;
17208   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
17209
17210   return default_argument;
17211 }
17212
17213 /* Parse a function-body.
17214
17215    function-body:
17216      compound_statement  */
17217
17218 static void
17219 cp_parser_function_body (cp_parser *parser)
17220 {
17221   cp_parser_compound_statement (parser, NULL, false, true);
17222 }
17223
17224 /* Parse a ctor-initializer-opt followed by a function-body.  Return
17225    true if a ctor-initializer was present.  */
17226
17227 static bool
17228 cp_parser_ctor_initializer_opt_and_function_body (cp_parser *parser)
17229 {
17230   tree body, list;
17231   bool ctor_initializer_p;
17232   const bool check_body_p =
17233      DECL_CONSTRUCTOR_P (current_function_decl)
17234      && DECL_DECLARED_CONSTEXPR_P (current_function_decl);
17235   tree last = NULL;
17236
17237   /* Begin the function body.  */
17238   body = begin_function_body ();
17239   /* Parse the optional ctor-initializer.  */
17240   ctor_initializer_p = cp_parser_ctor_initializer_opt (parser);
17241
17242   /* If we're parsing a constexpr constructor definition, we need
17243      to check that the constructor body is indeed empty.  However,
17244      before we get to cp_parser_function_body lot of junk has been
17245      generated, so we can't just check that we have an empty block.
17246      Rather we take a snapshot of the outermost block, and check whether
17247      cp_parser_function_body changed its state.  */
17248   if (check_body_p)
17249     {
17250       list = body;
17251       if (TREE_CODE (list) == BIND_EXPR)
17252         list = BIND_EXPR_BODY (list);
17253       if (TREE_CODE (list) == STATEMENT_LIST
17254           && STATEMENT_LIST_TAIL (list) != NULL)
17255         last = STATEMENT_LIST_TAIL (list)->stmt;
17256     }
17257   /* Parse the function-body.  */
17258   cp_parser_function_body (parser);
17259   if (check_body_p)
17260     check_constexpr_ctor_body (last, list);
17261   /* Finish the function body.  */
17262   finish_function_body (body);
17263
17264   return ctor_initializer_p;
17265 }
17266
17267 /* Parse an initializer.
17268
17269    initializer:
17270      = initializer-clause
17271      ( expression-list )
17272
17273    Returns an expression representing the initializer.  If no
17274    initializer is present, NULL_TREE is returned.
17275
17276    *IS_DIRECT_INIT is set to FALSE if the `= initializer-clause'
17277    production is used, and TRUE otherwise.  *IS_DIRECT_INIT is
17278    set to TRUE if there is no initializer present.  If there is an
17279    initializer, and it is not a constant-expression, *NON_CONSTANT_P
17280    is set to true; otherwise it is set to false.  */
17281
17282 static tree
17283 cp_parser_initializer (cp_parser* parser, bool* is_direct_init,
17284                        bool* non_constant_p)
17285 {
17286   cp_token *token;
17287   tree init;
17288
17289   /* Peek at the next token.  */
17290   token = cp_lexer_peek_token (parser->lexer);
17291
17292   /* Let our caller know whether or not this initializer was
17293      parenthesized.  */
17294   *is_direct_init = (token->type != CPP_EQ);
17295   /* Assume that the initializer is constant.  */
17296   *non_constant_p = false;
17297
17298   if (token->type == CPP_EQ)
17299     {
17300       /* Consume the `='.  */
17301       cp_lexer_consume_token (parser->lexer);
17302       /* Parse the initializer-clause.  */
17303       init = cp_parser_initializer_clause (parser, non_constant_p);
17304     }
17305   else if (token->type == CPP_OPEN_PAREN)
17306     {
17307       VEC(tree,gc) *vec;
17308       vec = cp_parser_parenthesized_expression_list (parser, non_attr,
17309                                                      /*cast_p=*/false,
17310                                                      /*allow_expansion_p=*/true,
17311                                                      non_constant_p);
17312       if (vec == NULL)
17313         return error_mark_node;
17314       init = build_tree_list_vec (vec);
17315       release_tree_vector (vec);
17316     }
17317   else if (token->type == CPP_OPEN_BRACE)
17318     {
17319       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
17320       init = cp_parser_braced_list (parser, non_constant_p);
17321       CONSTRUCTOR_IS_DIRECT_INIT (init) = 1;
17322     }
17323   else
17324     {
17325       /* Anything else is an error.  */
17326       cp_parser_error (parser, "expected initializer");
17327       init = error_mark_node;
17328     }
17329
17330   return init;
17331 }
17332
17333 /* Parse an initializer-clause.
17334
17335    initializer-clause:
17336      assignment-expression
17337      braced-init-list
17338
17339    Returns an expression representing the initializer.
17340
17341    If the `assignment-expression' production is used the value
17342    returned is simply a representation for the expression.
17343
17344    Otherwise, calls cp_parser_braced_list.  */
17345
17346 static tree
17347 cp_parser_initializer_clause (cp_parser* parser, bool* non_constant_p)
17348 {
17349   tree initializer;
17350
17351   /* Assume the expression is constant.  */
17352   *non_constant_p = false;
17353
17354   /* If it is not a `{', then we are looking at an
17355      assignment-expression.  */
17356   if (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE))
17357     {
17358       initializer
17359         = cp_parser_constant_expression (parser,
17360                                         /*allow_non_constant_p=*/true,
17361                                         non_constant_p);
17362     }
17363   else
17364     initializer = cp_parser_braced_list (parser, non_constant_p);
17365
17366   return initializer;
17367 }
17368
17369 /* Parse a brace-enclosed initializer list.
17370
17371    braced-init-list:
17372      { initializer-list , [opt] }
17373      { }
17374
17375    Returns a CONSTRUCTOR.  The CONSTRUCTOR_ELTS will be
17376    the elements of the initializer-list (or NULL, if the last
17377    production is used).  The TREE_TYPE for the CONSTRUCTOR will be
17378    NULL_TREE.  There is no way to detect whether or not the optional
17379    trailing `,' was provided.  NON_CONSTANT_P is as for
17380    cp_parser_initializer.  */     
17381
17382 static tree
17383 cp_parser_braced_list (cp_parser* parser, bool* non_constant_p)
17384 {
17385   tree initializer;
17386
17387   /* Consume the `{' token.  */
17388   cp_lexer_consume_token (parser->lexer);
17389   /* Create a CONSTRUCTOR to represent the braced-initializer.  */
17390   initializer = make_node (CONSTRUCTOR);
17391   /* If it's not a `}', then there is a non-trivial initializer.  */
17392   if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_BRACE))
17393     {
17394       /* Parse the initializer list.  */
17395       CONSTRUCTOR_ELTS (initializer)
17396         = cp_parser_initializer_list (parser, non_constant_p);
17397       /* A trailing `,' token is allowed.  */
17398       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
17399         cp_lexer_consume_token (parser->lexer);
17400     }
17401   /* Now, there should be a trailing `}'.  */
17402   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17403   TREE_TYPE (initializer) = init_list_type_node;
17404   return initializer;
17405 }
17406
17407 /* Parse an initializer-list.
17408
17409    initializer-list:
17410      initializer-clause ... [opt]
17411      initializer-list , initializer-clause ... [opt]
17412
17413    GNU Extension:
17414
17415    initializer-list:
17416      designation initializer-clause ...[opt]
17417      initializer-list , designation initializer-clause ...[opt]
17418
17419    designation:
17420      . identifier =
17421      identifier :
17422      [ constant-expression ] =
17423
17424    Returns a VEC of constructor_elt.  The VALUE of each elt is an expression
17425    for the initializer.  If the INDEX of the elt is non-NULL, it is the
17426    IDENTIFIER_NODE naming the field to initialize.  NON_CONSTANT_P is
17427    as for cp_parser_initializer.  */
17428
17429 static VEC(constructor_elt,gc) *
17430 cp_parser_initializer_list (cp_parser* parser, bool* non_constant_p)
17431 {
17432   VEC(constructor_elt,gc) *v = NULL;
17433
17434   /* Assume all of the expressions are constant.  */
17435   *non_constant_p = false;
17436
17437   /* Parse the rest of the list.  */
17438   while (true)
17439     {
17440       cp_token *token;
17441       tree designator;
17442       tree initializer;
17443       bool clause_non_constant_p;
17444
17445       /* If the next token is an identifier and the following one is a
17446          colon, we are looking at the GNU designated-initializer
17447          syntax.  */
17448       if (cp_parser_allow_gnu_extensions_p (parser)
17449           && cp_lexer_next_token_is (parser->lexer, CPP_NAME)
17450           && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_COLON)
17451         {
17452           /* Warn the user that they are using an extension.  */
17453           pedwarn (input_location, OPT_pedantic, 
17454                    "ISO C++ does not allow designated initializers");
17455           /* Consume the identifier.  */
17456           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17457           /* Consume the `:'.  */
17458           cp_lexer_consume_token (parser->lexer);
17459         }
17460       /* Also handle the C99 syntax, '. id ='.  */
17461       else if (cp_parser_allow_gnu_extensions_p (parser)
17462                && cp_lexer_next_token_is (parser->lexer, CPP_DOT)
17463                && cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_NAME
17464                && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_EQ)
17465         {
17466           /* Warn the user that they are using an extension.  */
17467           pedwarn (input_location, OPT_pedantic,
17468                    "ISO C++ does not allow C99 designated initializers");
17469           /* Consume the `.'.  */
17470           cp_lexer_consume_token (parser->lexer);
17471           /* Consume the identifier.  */
17472           designator = cp_lexer_consume_token (parser->lexer)->u.value;
17473           /* Consume the `='.  */
17474           cp_lexer_consume_token (parser->lexer);
17475         }
17476       /* Also handle C99 array designators, '[ const ] ='.  */
17477       else if (cp_parser_allow_gnu_extensions_p (parser)
17478                && !c_dialect_objc ()
17479                && cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
17480         {
17481           cp_lexer_consume_token (parser->lexer);
17482           designator = cp_parser_constant_expression (parser, false, NULL);
17483           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
17484           cp_parser_require (parser, CPP_EQ, RT_EQ);
17485         }
17486       else
17487         designator = NULL_TREE;
17488
17489       /* Parse the initializer.  */
17490       initializer = cp_parser_initializer_clause (parser,
17491                                                   &clause_non_constant_p);
17492       /* If any clause is non-constant, so is the entire initializer.  */
17493       if (clause_non_constant_p)
17494         *non_constant_p = true;
17495
17496       /* If we have an ellipsis, this is an initializer pack
17497          expansion.  */
17498       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
17499         {
17500           /* Consume the `...'.  */
17501           cp_lexer_consume_token (parser->lexer);
17502
17503           /* Turn the initializer into an initializer expansion.  */
17504           initializer = make_pack_expansion (initializer);
17505         }
17506
17507       /* Add it to the vector.  */
17508       CONSTRUCTOR_APPEND_ELT (v, designator, initializer);
17509
17510       /* If the next token is not a comma, we have reached the end of
17511          the list.  */
17512       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
17513         break;
17514
17515       /* Peek at the next token.  */
17516       token = cp_lexer_peek_nth_token (parser->lexer, 2);
17517       /* If the next token is a `}', then we're still done.  An
17518          initializer-clause can have a trailing `,' after the
17519          initializer-list and before the closing `}'.  */
17520       if (token->type == CPP_CLOSE_BRACE)
17521         break;
17522
17523       /* Consume the `,' token.  */
17524       cp_lexer_consume_token (parser->lexer);
17525     }
17526
17527   return v;
17528 }
17529
17530 /* Classes [gram.class] */
17531
17532 /* Parse a class-name.
17533
17534    class-name:
17535      identifier
17536      template-id
17537
17538    TYPENAME_KEYWORD_P is true iff the `typename' keyword has been used
17539    to indicate that names looked up in dependent types should be
17540    assumed to be types.  TEMPLATE_KEYWORD_P is true iff the `template'
17541    keyword has been used to indicate that the name that appears next
17542    is a template.  TAG_TYPE indicates the explicit tag given before
17543    the type name, if any.  If CHECK_DEPENDENCY_P is FALSE, names are
17544    looked up in dependent scopes.  If CLASS_HEAD_P is TRUE, this class
17545    is the class being defined in a class-head.
17546
17547    Returns the TYPE_DECL representing the class.  */
17548
17549 static tree
17550 cp_parser_class_name (cp_parser *parser,
17551                       bool typename_keyword_p,
17552                       bool template_keyword_p,
17553                       enum tag_types tag_type,
17554                       bool check_dependency_p,
17555                       bool class_head_p,
17556                       bool is_declaration)
17557 {
17558   tree decl;
17559   tree scope;
17560   bool typename_p;
17561   cp_token *token;
17562   tree identifier = NULL_TREE;
17563
17564   /* All class-names start with an identifier.  */
17565   token = cp_lexer_peek_token (parser->lexer);
17566   if (token->type != CPP_NAME && token->type != CPP_TEMPLATE_ID)
17567     {
17568       cp_parser_error (parser, "expected class-name");
17569       return error_mark_node;
17570     }
17571
17572   /* PARSER->SCOPE can be cleared when parsing the template-arguments
17573      to a template-id, so we save it here.  */
17574   scope = parser->scope;
17575   if (scope == error_mark_node)
17576     return error_mark_node;
17577
17578   /* Any name names a type if we're following the `typename' keyword
17579      in a qualified name where the enclosing scope is type-dependent.  */
17580   typename_p = (typename_keyword_p && scope && TYPE_P (scope)
17581                 && dependent_type_p (scope));
17582   /* Handle the common case (an identifier, but not a template-id)
17583      efficiently.  */
17584   if (token->type == CPP_NAME
17585       && !cp_parser_nth_token_starts_template_argument_list_p (parser, 2))
17586     {
17587       cp_token *identifier_token;
17588       bool ambiguous_p;
17589
17590       /* Look for the identifier.  */
17591       identifier_token = cp_lexer_peek_token (parser->lexer);
17592       ambiguous_p = identifier_token->ambiguous_p;
17593       identifier = cp_parser_identifier (parser);
17594       /* If the next token isn't an identifier, we are certainly not
17595          looking at a class-name.  */
17596       if (identifier == error_mark_node)
17597         decl = error_mark_node;
17598       /* If we know this is a type-name, there's no need to look it
17599          up.  */
17600       else if (typename_p)
17601         decl = identifier;
17602       else
17603         {
17604           tree ambiguous_decls;
17605           /* If we already know that this lookup is ambiguous, then
17606              we've already issued an error message; there's no reason
17607              to check again.  */
17608           if (ambiguous_p)
17609             {
17610               cp_parser_simulate_error (parser);
17611               return error_mark_node;
17612             }
17613           /* If the next token is a `::', then the name must be a type
17614              name.
17615
17616              [basic.lookup.qual]
17617
17618              During the lookup for a name preceding the :: scope
17619              resolution operator, object, function, and enumerator
17620              names are ignored.  */
17621           if (cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17622             tag_type = typename_type;
17623           /* Look up the name.  */
17624           decl = cp_parser_lookup_name (parser, identifier,
17625                                         tag_type,
17626                                         /*is_template=*/false,
17627                                         /*is_namespace=*/false,
17628                                         check_dependency_p,
17629                                         &ambiguous_decls,
17630                                         identifier_token->location);
17631           if (ambiguous_decls)
17632             {
17633               if (cp_parser_parsing_tentatively (parser))
17634                 cp_parser_simulate_error (parser);
17635               return error_mark_node;
17636             }
17637         }
17638     }
17639   else
17640     {
17641       /* Try a template-id.  */
17642       decl = cp_parser_template_id (parser, template_keyword_p,
17643                                     check_dependency_p,
17644                                     is_declaration);
17645       if (decl == error_mark_node)
17646         return error_mark_node;
17647     }
17648
17649   decl = cp_parser_maybe_treat_template_as_class (decl, class_head_p);
17650
17651   /* If this is a typename, create a TYPENAME_TYPE.  */
17652   if (typename_p && decl != error_mark_node)
17653     {
17654       decl = make_typename_type (scope, decl, typename_type,
17655                                  /*complain=*/tf_error);
17656       if (decl != error_mark_node)
17657         decl = TYPE_NAME (decl);
17658     }
17659
17660   /* Check to see that it is really the name of a class.  */
17661   if (TREE_CODE (decl) == TEMPLATE_ID_EXPR
17662       && TREE_CODE (TREE_OPERAND (decl, 0)) == IDENTIFIER_NODE
17663       && cp_lexer_next_token_is (parser->lexer, CPP_SCOPE))
17664     /* Situations like this:
17665
17666          template <typename T> struct A {
17667            typename T::template X<int>::I i;
17668          };
17669
17670        are problematic.  Is `T::template X<int>' a class-name?  The
17671        standard does not seem to be definitive, but there is no other
17672        valid interpretation of the following `::'.  Therefore, those
17673        names are considered class-names.  */
17674     {
17675       decl = make_typename_type (scope, decl, tag_type, tf_error);
17676       if (decl != error_mark_node)
17677         decl = TYPE_NAME (decl);
17678     }
17679   else if (TREE_CODE (decl) != TYPE_DECL
17680            || TREE_TYPE (decl) == error_mark_node
17681            || !MAYBE_CLASS_TYPE_P (TREE_TYPE (decl))
17682            /* In Objective-C 2.0, a classname followed by '.' starts a
17683               dot-syntax expression, and it's not a type-name.  */
17684            || (c_dialect_objc ()
17685                && cp_lexer_peek_token (parser->lexer)->type == CPP_DOT 
17686                && objc_is_class_name (decl)))
17687     decl = error_mark_node;
17688
17689   if (decl == error_mark_node)
17690     cp_parser_error (parser, "expected class-name");
17691   else if (identifier && !parser->scope)
17692     maybe_note_name_used_in_class (identifier, decl);
17693
17694   return decl;
17695 }
17696
17697 /* Parse a class-specifier.
17698
17699    class-specifier:
17700      class-head { member-specification [opt] }
17701
17702    Returns the TREE_TYPE representing the class.  */
17703
17704 static tree
17705 cp_parser_class_specifier_1 (cp_parser* parser)
17706 {
17707   tree type;
17708   tree attributes = NULL_TREE;
17709   bool nested_name_specifier_p;
17710   unsigned saved_num_template_parameter_lists;
17711   bool saved_in_function_body;
17712   unsigned char in_statement;
17713   bool in_switch_statement_p;
17714   bool saved_in_unbraced_linkage_specification_p;
17715   tree old_scope = NULL_TREE;
17716   tree scope = NULL_TREE;
17717   tree bases;
17718   cp_token *closing_brace;
17719
17720   push_deferring_access_checks (dk_no_deferred);
17721
17722   /* Parse the class-head.  */
17723   type = cp_parser_class_head (parser,
17724                                &nested_name_specifier_p,
17725                                &attributes,
17726                                &bases);
17727   /* If the class-head was a semantic disaster, skip the entire body
17728      of the class.  */
17729   if (!type)
17730     {
17731       cp_parser_skip_to_end_of_block_or_statement (parser);
17732       pop_deferring_access_checks ();
17733       return error_mark_node;
17734     }
17735
17736   /* Look for the `{'.  */
17737   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
17738     {
17739       pop_deferring_access_checks ();
17740       return error_mark_node;
17741     }
17742
17743   /* Process the base classes. If they're invalid, skip the 
17744      entire class body.  */
17745   if (!xref_basetypes (type, bases))
17746     {
17747       /* Consuming the closing brace yields better error messages
17748          later on.  */
17749       if (cp_parser_skip_to_closing_brace (parser))
17750         cp_lexer_consume_token (parser->lexer);
17751       pop_deferring_access_checks ();
17752       return error_mark_node;
17753     }
17754
17755   /* Issue an error message if type-definitions are forbidden here.  */
17756   cp_parser_check_type_definition (parser);
17757   /* Remember that we are defining one more class.  */
17758   ++parser->num_classes_being_defined;
17759   /* Inside the class, surrounding template-parameter-lists do not
17760      apply.  */
17761   saved_num_template_parameter_lists
17762     = parser->num_template_parameter_lists;
17763   parser->num_template_parameter_lists = 0;
17764   /* We are not in a function body.  */
17765   saved_in_function_body = parser->in_function_body;
17766   parser->in_function_body = false;
17767   /* Or in a loop.  */
17768   in_statement = parser->in_statement;
17769   parser->in_statement = 0;
17770   /* Or in a switch.  */
17771   in_switch_statement_p = parser->in_switch_statement_p;
17772   parser->in_switch_statement_p = false;
17773   /* We are not immediately inside an extern "lang" block.  */
17774   saved_in_unbraced_linkage_specification_p
17775     = parser->in_unbraced_linkage_specification_p;
17776   parser->in_unbraced_linkage_specification_p = false;
17777
17778   /* Start the class.  */
17779   if (nested_name_specifier_p)
17780     {
17781       scope = CP_DECL_CONTEXT (TYPE_MAIN_DECL (type));
17782       old_scope = push_inner_scope (scope);
17783     }
17784   type = begin_class_definition (type, attributes);
17785
17786   if (type == error_mark_node)
17787     /* If the type is erroneous, skip the entire body of the class.  */
17788     cp_parser_skip_to_closing_brace (parser);
17789   else
17790     /* Parse the member-specification.  */
17791     cp_parser_member_specification_opt (parser);
17792
17793   /* Look for the trailing `}'.  */
17794   closing_brace = cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
17795   /* Look for trailing attributes to apply to this class.  */
17796   if (cp_parser_allow_gnu_extensions_p (parser))
17797     attributes = cp_parser_attributes_opt (parser);
17798   if (type != error_mark_node)
17799     type = finish_struct (type, attributes);
17800   if (nested_name_specifier_p)
17801     pop_inner_scope (old_scope, scope);
17802
17803   /* We've finished a type definition.  Check for the common syntax
17804      error of forgetting a semicolon after the definition.  We need to
17805      be careful, as we can't just check for not-a-semicolon and be done
17806      with it; the user might have typed:
17807
17808      class X { } c = ...;
17809      class X { } *p = ...;
17810
17811      and so forth.  Instead, enumerate all the possible tokens that
17812      might follow this production; if we don't see one of them, then
17813      complain and silently insert the semicolon.  */
17814   {
17815     cp_token *token = cp_lexer_peek_token (parser->lexer);
17816     bool want_semicolon = true;
17817
17818     switch (token->type)
17819       {
17820       case CPP_NAME:
17821       case CPP_SEMICOLON:
17822       case CPP_MULT:
17823       case CPP_AND:
17824       case CPP_OPEN_PAREN:
17825       case CPP_CLOSE_PAREN:
17826       case CPP_COMMA:
17827         want_semicolon = false;
17828         break;
17829
17830         /* While it's legal for type qualifiers and storage class
17831            specifiers to follow type definitions in the grammar, only
17832            compiler testsuites contain code like that.  Assume that if
17833            we see such code, then what we're really seeing is a case
17834            like:
17835
17836            class X { }
17837            const <type> var = ...;
17838
17839            or
17840
17841            class Y { }
17842            static <type> func (...) ...
17843
17844            i.e. the qualifier or specifier applies to the next
17845            declaration.  To do so, however, we need to look ahead one
17846            more token to see if *that* token is a type specifier.
17847
17848            This code could be improved to handle:
17849
17850            class Z { }
17851            static const <type> var = ...;  */
17852       case CPP_KEYWORD:
17853         if (keyword_is_decl_specifier (token->keyword))
17854           {
17855             cp_token *lookahead = cp_lexer_peek_nth_token (parser->lexer, 2);
17856
17857             /* Handling user-defined types here would be nice, but very
17858                tricky.  */
17859             want_semicolon
17860               = (lookahead->type == CPP_KEYWORD
17861                  && keyword_begins_type_specifier (lookahead->keyword));
17862           }
17863         break;
17864       default:
17865         break;
17866       }
17867
17868     /* If we don't have a type, then something is very wrong and we
17869        shouldn't try to do anything clever.  Likewise for not seeing the
17870        closing brace.  */
17871     if (closing_brace && TYPE_P (type) && want_semicolon)
17872       {
17873         cp_token_position prev
17874           = cp_lexer_previous_token_position (parser->lexer);
17875         cp_token *prev_token = cp_lexer_token_at (parser->lexer, prev);
17876         location_t loc = prev_token->location;
17877
17878         if (CLASSTYPE_DECLARED_CLASS (type))
17879           error_at (loc, "expected %<;%> after class definition");
17880         else if (TREE_CODE (type) == RECORD_TYPE)
17881           error_at (loc, "expected %<;%> after struct definition");
17882         else if (TREE_CODE (type) == UNION_TYPE)
17883           error_at (loc, "expected %<;%> after union definition");
17884         else
17885           gcc_unreachable ();
17886
17887         /* Unget one token and smash it to look as though we encountered
17888            a semicolon in the input stream.  */
17889         cp_lexer_set_token_position (parser->lexer, prev);
17890         token = cp_lexer_peek_token (parser->lexer);
17891         token->type = CPP_SEMICOLON;
17892         token->keyword = RID_MAX;
17893       }
17894   }
17895
17896   /* If this class is not itself within the scope of another class,
17897      then we need to parse the bodies of all of the queued function
17898      definitions.  Note that the queued functions defined in a class
17899      are not always processed immediately following the
17900      class-specifier for that class.  Consider:
17901
17902        struct A {
17903          struct B { void f() { sizeof (A); } };
17904        };
17905
17906      If `f' were processed before the processing of `A' were
17907      completed, there would be no way to compute the size of `A'.
17908      Note that the nesting we are interested in here is lexical --
17909      not the semantic nesting given by TYPE_CONTEXT.  In particular,
17910      for:
17911
17912        struct A { struct B; };
17913        struct A::B { void f() { } };
17914
17915      there is no need to delay the parsing of `A::B::f'.  */
17916   if (--parser->num_classes_being_defined == 0)
17917     {
17918       tree decl;
17919       tree class_type = NULL_TREE;
17920       tree pushed_scope = NULL_TREE;
17921       unsigned ix;
17922       cp_default_arg_entry *e;
17923       tree save_ccp, save_ccr;
17924
17925       /* In a first pass, parse default arguments to the functions.
17926          Then, in a second pass, parse the bodies of the functions.
17927          This two-phased approach handles cases like:
17928
17929             struct S {
17930               void f() { g(); }
17931               void g(int i = 3);
17932             };
17933
17934          */
17935       FOR_EACH_VEC_ELT (cp_default_arg_entry, unparsed_funs_with_default_args,
17936                         ix, e)
17937         {
17938           decl = e->decl;
17939           /* If there are default arguments that have not yet been processed,
17940              take care of them now.  */
17941           if (class_type != e->class_type)
17942             {
17943               if (pushed_scope)
17944                 pop_scope (pushed_scope);
17945               class_type = e->class_type;
17946               pushed_scope = push_scope (class_type);
17947             }
17948           /* Make sure that any template parameters are in scope.  */
17949           maybe_begin_member_template_processing (decl);
17950           /* Parse the default argument expressions.  */
17951           cp_parser_late_parsing_default_args (parser, decl);
17952           /* Remove any template parameters from the symbol table.  */
17953           maybe_end_member_template_processing ();
17954         }
17955       VEC_truncate (cp_default_arg_entry, unparsed_funs_with_default_args, 0);
17956       /* Now parse any NSDMIs.  */
17957       save_ccp = current_class_ptr;
17958       save_ccr = current_class_ref;
17959       FOR_EACH_VEC_ELT (tree, unparsed_nsdmis, ix, decl)
17960         {
17961           if (class_type != DECL_CONTEXT (decl))
17962             {
17963               if (pushed_scope)
17964                 pop_scope (pushed_scope);
17965               class_type = DECL_CONTEXT (decl);
17966               pushed_scope = push_scope (class_type);
17967             }
17968           inject_this_parameter (class_type, TYPE_UNQUALIFIED);
17969           cp_parser_late_parsing_nsdmi (parser, decl);
17970         }
17971       VEC_truncate (tree, unparsed_nsdmis, 0);
17972       current_class_ptr = save_ccp;
17973       current_class_ref = save_ccr;
17974       if (pushed_scope)
17975         pop_scope (pushed_scope);
17976       /* Now parse the body of the functions.  */
17977       FOR_EACH_VEC_ELT (tree, unparsed_funs_with_definitions, ix, decl)
17978         cp_parser_late_parsing_for_member (parser, decl);
17979       VEC_truncate (tree, unparsed_funs_with_definitions, 0);
17980     }
17981
17982   /* Put back any saved access checks.  */
17983   pop_deferring_access_checks ();
17984
17985   /* Restore saved state.  */
17986   parser->in_switch_statement_p = in_switch_statement_p;
17987   parser->in_statement = in_statement;
17988   parser->in_function_body = saved_in_function_body;
17989   parser->num_template_parameter_lists
17990     = saved_num_template_parameter_lists;
17991   parser->in_unbraced_linkage_specification_p
17992     = saved_in_unbraced_linkage_specification_p;
17993
17994   return type;
17995 }
17996
17997 static tree
17998 cp_parser_class_specifier (cp_parser* parser)
17999 {
18000   tree ret;
18001   timevar_push (TV_PARSE_STRUCT);
18002   ret = cp_parser_class_specifier_1 (parser);
18003   timevar_pop (TV_PARSE_STRUCT);
18004   return ret;
18005 }
18006
18007 /* Parse a class-head.
18008
18009    class-head:
18010      class-key identifier [opt] base-clause [opt]
18011      class-key nested-name-specifier identifier class-virt-specifier [opt] base-clause [opt]
18012      class-key nested-name-specifier [opt] template-id
18013        base-clause [opt]
18014
18015    class-virt-specifier:
18016      final
18017
18018    GNU Extensions:
18019      class-key attributes identifier [opt] base-clause [opt]
18020      class-key attributes nested-name-specifier identifier base-clause [opt]
18021      class-key attributes nested-name-specifier [opt] template-id
18022        base-clause [opt]
18023
18024    Upon return BASES is initialized to the list of base classes (or
18025    NULL, if there are none) in the same form returned by
18026    cp_parser_base_clause.
18027
18028    Returns the TYPE of the indicated class.  Sets
18029    *NESTED_NAME_SPECIFIER_P to TRUE iff one of the productions
18030    involving a nested-name-specifier was used, and FALSE otherwise.
18031
18032    Returns error_mark_node if this is not a class-head.
18033
18034    Returns NULL_TREE if the class-head is syntactically valid, but
18035    semantically invalid in a way that means we should skip the entire
18036    body of the class.  */
18037
18038 static tree
18039 cp_parser_class_head (cp_parser* parser,
18040                       bool* nested_name_specifier_p,
18041                       tree *attributes_p,
18042                       tree *bases)
18043 {
18044   tree nested_name_specifier;
18045   enum tag_types class_key;
18046   tree id = NULL_TREE;
18047   tree type = NULL_TREE;
18048   tree attributes;
18049   cp_virt_specifiers virt_specifiers = VIRT_SPEC_UNSPECIFIED;
18050   bool template_id_p = false;
18051   bool qualified_p = false;
18052   bool invalid_nested_name_p = false;
18053   bool invalid_explicit_specialization_p = false;
18054   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18055   tree pushed_scope = NULL_TREE;
18056   unsigned num_templates;
18057   cp_token *type_start_token = NULL, *nested_name_specifier_token_start = NULL;
18058   /* Assume no nested-name-specifier will be present.  */
18059   *nested_name_specifier_p = false;
18060   /* Assume no template parameter lists will be used in defining the
18061      type.  */
18062   num_templates = 0;
18063   parser->colon_corrects_to_scope_p = false;
18064
18065   *bases = NULL_TREE;
18066
18067   /* Look for the class-key.  */
18068   class_key = cp_parser_class_key (parser);
18069   if (class_key == none_type)
18070     return error_mark_node;
18071
18072   /* Parse the attributes.  */
18073   attributes = cp_parser_attributes_opt (parser);
18074
18075   /* If the next token is `::', that is invalid -- but sometimes
18076      people do try to write:
18077
18078        struct ::S {};
18079
18080      Handle this gracefully by accepting the extra qualifier, and then
18081      issuing an error about it later if this really is a
18082      class-head.  If it turns out just to be an elaborated type
18083      specifier, remain silent.  */
18084   if (cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false))
18085     qualified_p = true;
18086
18087   push_deferring_access_checks (dk_no_check);
18088
18089   /* Determine the name of the class.  Begin by looking for an
18090      optional nested-name-specifier.  */
18091   nested_name_specifier_token_start = cp_lexer_peek_token (parser->lexer);
18092   nested_name_specifier
18093     = cp_parser_nested_name_specifier_opt (parser,
18094                                            /*typename_keyword_p=*/false,
18095                                            /*check_dependency_p=*/false,
18096                                            /*type_p=*/false,
18097                                            /*is_declaration=*/false);
18098   /* If there was a nested-name-specifier, then there *must* be an
18099      identifier.  */
18100   if (nested_name_specifier)
18101     {
18102       type_start_token = cp_lexer_peek_token (parser->lexer);
18103       /* Although the grammar says `identifier', it really means
18104          `class-name' or `template-name'.  You are only allowed to
18105          define a class that has already been declared with this
18106          syntax.
18107
18108          The proposed resolution for Core Issue 180 says that wherever
18109          you see `class T::X' you should treat `X' as a type-name.
18110
18111          It is OK to define an inaccessible class; for example:
18112
18113            class A { class B; };
18114            class A::B {};
18115
18116          We do not know if we will see a class-name, or a
18117          template-name.  We look for a class-name first, in case the
18118          class-name is a template-id; if we looked for the
18119          template-name first we would stop after the template-name.  */
18120       cp_parser_parse_tentatively (parser);
18121       type = cp_parser_class_name (parser,
18122                                    /*typename_keyword_p=*/false,
18123                                    /*template_keyword_p=*/false,
18124                                    class_type,
18125                                    /*check_dependency_p=*/false,
18126                                    /*class_head_p=*/true,
18127                                    /*is_declaration=*/false);
18128       /* If that didn't work, ignore the nested-name-specifier.  */
18129       if (!cp_parser_parse_definitely (parser))
18130         {
18131           invalid_nested_name_p = true;
18132           type_start_token = cp_lexer_peek_token (parser->lexer);
18133           id = cp_parser_identifier (parser);
18134           if (id == error_mark_node)
18135             id = NULL_TREE;
18136         }
18137       /* If we could not find a corresponding TYPE, treat this
18138          declaration like an unqualified declaration.  */
18139       if (type == error_mark_node)
18140         nested_name_specifier = NULL_TREE;
18141       /* Otherwise, count the number of templates used in TYPE and its
18142          containing scopes.  */
18143       else
18144         {
18145           tree scope;
18146
18147           for (scope = TREE_TYPE (type);
18148                scope && TREE_CODE (scope) != NAMESPACE_DECL;
18149                scope = (TYPE_P (scope)
18150                         ? TYPE_CONTEXT (scope)
18151                         : DECL_CONTEXT (scope)))
18152             if (TYPE_P (scope)
18153                 && CLASS_TYPE_P (scope)
18154                 && CLASSTYPE_TEMPLATE_INFO (scope)
18155                 && PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope))
18156                 && !CLASSTYPE_TEMPLATE_SPECIALIZATION (scope))
18157               ++num_templates;
18158         }
18159     }
18160   /* Otherwise, the identifier is optional.  */
18161   else
18162     {
18163       /* We don't know whether what comes next is a template-id,
18164          an identifier, or nothing at all.  */
18165       cp_parser_parse_tentatively (parser);
18166       /* Check for a template-id.  */
18167       type_start_token = cp_lexer_peek_token (parser->lexer);
18168       id = cp_parser_template_id (parser,
18169                                   /*template_keyword_p=*/false,
18170                                   /*check_dependency_p=*/true,
18171                                   /*is_declaration=*/true);
18172       /* If that didn't work, it could still be an identifier.  */
18173       if (!cp_parser_parse_definitely (parser))
18174         {
18175           if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
18176             {
18177               type_start_token = cp_lexer_peek_token (parser->lexer);
18178               id = cp_parser_identifier (parser);
18179             }
18180           else
18181             id = NULL_TREE;
18182         }
18183       else
18184         {
18185           template_id_p = true;
18186           ++num_templates;
18187         }
18188     }
18189
18190   pop_deferring_access_checks ();
18191
18192   if (id)
18193     {
18194       cp_parser_check_for_invalid_template_id (parser, id,
18195                                                type_start_token->location);
18196     }
18197   virt_specifiers = cp_parser_virt_specifier_seq_opt (parser);
18198
18199   /* If it's not a `:' or a `{' then we can't really be looking at a
18200      class-head, since a class-head only appears as part of a
18201      class-specifier.  We have to detect this situation before calling
18202      xref_tag, since that has irreversible side-effects.  */
18203   if (!cp_parser_next_token_starts_class_definition_p (parser))
18204     {
18205       cp_parser_error (parser, "expected %<{%> or %<:%>");
18206       type = error_mark_node;
18207       goto out;
18208     }
18209
18210   /* At this point, we're going ahead with the class-specifier, even
18211      if some other problem occurs.  */
18212   cp_parser_commit_to_tentative_parse (parser);
18213   if (virt_specifiers & VIRT_SPEC_OVERRIDE)
18214     {
18215       cp_parser_error (parser,
18216                        "cannot specify %<override%> for a class");
18217       type = error_mark_node;
18218       goto out;
18219     }
18220   /* Issue the error about the overly-qualified name now.  */
18221   if (qualified_p)
18222     {
18223       cp_parser_error (parser,
18224                        "global qualification of class name is invalid");
18225       type = error_mark_node;
18226       goto out;
18227     }
18228   else if (invalid_nested_name_p)
18229     {
18230       cp_parser_error (parser,
18231                        "qualified name does not name a class");
18232       type = error_mark_node;
18233       goto out;
18234     }
18235   else if (nested_name_specifier)
18236     {
18237       tree scope;
18238
18239       /* Reject typedef-names in class heads.  */
18240       if (!DECL_IMPLICIT_TYPEDEF_P (type))
18241         {
18242           error_at (type_start_token->location,
18243                     "invalid class name in declaration of %qD",
18244                     type);
18245           type = NULL_TREE;
18246           goto done;
18247         }
18248
18249       /* Figure out in what scope the declaration is being placed.  */
18250       scope = current_scope ();
18251       /* If that scope does not contain the scope in which the
18252          class was originally declared, the program is invalid.  */
18253       if (scope && !is_ancestor (scope, nested_name_specifier))
18254         {
18255           if (at_namespace_scope_p ())
18256             error_at (type_start_token->location,
18257                       "declaration of %qD in namespace %qD which does not "
18258                       "enclose %qD",
18259                       type, scope, nested_name_specifier);
18260           else
18261             error_at (type_start_token->location,
18262                       "declaration of %qD in %qD which does not enclose %qD",
18263                       type, scope, nested_name_specifier);
18264           type = NULL_TREE;
18265           goto done;
18266         }
18267       /* [dcl.meaning]
18268
18269          A declarator-id shall not be qualified except for the
18270          definition of a ... nested class outside of its class
18271          ... [or] the definition or explicit instantiation of a
18272          class member of a namespace outside of its namespace.  */
18273       if (scope == nested_name_specifier)
18274         {
18275           permerror (nested_name_specifier_token_start->location,
18276                      "extra qualification not allowed");
18277           nested_name_specifier = NULL_TREE;
18278           num_templates = 0;
18279         }
18280     }
18281   /* An explicit-specialization must be preceded by "template <>".  If
18282      it is not, try to recover gracefully.  */
18283   if (at_namespace_scope_p ()
18284       && parser->num_template_parameter_lists == 0
18285       && template_id_p)
18286     {
18287       error_at (type_start_token->location,
18288                 "an explicit specialization must be preceded by %<template <>%>");
18289       invalid_explicit_specialization_p = true;
18290       /* Take the same action that would have been taken by
18291          cp_parser_explicit_specialization.  */
18292       ++parser->num_template_parameter_lists;
18293       begin_specialization ();
18294     }
18295   /* There must be no "return" statements between this point and the
18296      end of this function; set "type "to the correct return value and
18297      use "goto done;" to return.  */
18298   /* Make sure that the right number of template parameters were
18299      present.  */
18300   if (!cp_parser_check_template_parameters (parser, num_templates,
18301                                             type_start_token->location,
18302                                             /*declarator=*/NULL))
18303     {
18304       /* If something went wrong, there is no point in even trying to
18305          process the class-definition.  */
18306       type = NULL_TREE;
18307       goto done;
18308     }
18309
18310   /* Look up the type.  */
18311   if (template_id_p)
18312     {
18313       if (TREE_CODE (id) == TEMPLATE_ID_EXPR
18314           && (DECL_FUNCTION_TEMPLATE_P (TREE_OPERAND (id, 0))
18315               || TREE_CODE (TREE_OPERAND (id, 0)) == OVERLOAD))
18316         {
18317           error_at (type_start_token->location,
18318                     "function template %qD redeclared as a class template", id);
18319           type = error_mark_node;
18320         }
18321       else
18322         {
18323           type = TREE_TYPE (id);
18324           type = maybe_process_partial_specialization (type);
18325         }
18326       if (nested_name_specifier)
18327         pushed_scope = push_scope (nested_name_specifier);
18328     }
18329   else if (nested_name_specifier)
18330     {
18331       tree class_type;
18332
18333       /* Given:
18334
18335             template <typename T> struct S { struct T };
18336             template <typename T> struct S<T>::T { };
18337
18338          we will get a TYPENAME_TYPE when processing the definition of
18339          `S::T'.  We need to resolve it to the actual type before we
18340          try to define it.  */
18341       if (TREE_CODE (TREE_TYPE (type)) == TYPENAME_TYPE)
18342         {
18343           class_type = resolve_typename_type (TREE_TYPE (type),
18344                                               /*only_current_p=*/false);
18345           if (TREE_CODE (class_type) != TYPENAME_TYPE)
18346             type = TYPE_NAME (class_type);
18347           else
18348             {
18349               cp_parser_error (parser, "could not resolve typename type");
18350               type = error_mark_node;
18351             }
18352         }
18353
18354       if (maybe_process_partial_specialization (TREE_TYPE (type))
18355           == error_mark_node)
18356         {
18357           type = NULL_TREE;
18358           goto done;
18359         }
18360
18361       class_type = current_class_type;
18362       /* Enter the scope indicated by the nested-name-specifier.  */
18363       pushed_scope = push_scope (nested_name_specifier);
18364       /* Get the canonical version of this type.  */
18365       type = TYPE_MAIN_DECL (TREE_TYPE (type));
18366       if (PROCESSING_REAL_TEMPLATE_DECL_P ()
18367           && !CLASSTYPE_TEMPLATE_SPECIALIZATION (TREE_TYPE (type)))
18368         {
18369           type = push_template_decl (type);
18370           if (type == error_mark_node)
18371             {
18372               type = NULL_TREE;
18373               goto done;
18374             }
18375         }
18376
18377       type = TREE_TYPE (type);
18378       *nested_name_specifier_p = true;
18379     }
18380   else      /* The name is not a nested name.  */
18381     {
18382       /* If the class was unnamed, create a dummy name.  */
18383       if (!id)
18384         id = make_anon_name ();
18385       type = xref_tag (class_key, id, /*tag_scope=*/ts_current,
18386                        parser->num_template_parameter_lists);
18387     }
18388
18389   /* Indicate whether this class was declared as a `class' or as a
18390      `struct'.  */
18391   if (TREE_CODE (type) == RECORD_TYPE)
18392     CLASSTYPE_DECLARED_CLASS (type) = (class_key == class_type);
18393   cp_parser_check_class_key (class_key, type);
18394
18395   /* If this type was already complete, and we see another definition,
18396      that's an error.  */
18397   if (type != error_mark_node && COMPLETE_TYPE_P (type))
18398     {
18399       error_at (type_start_token->location, "redefinition of %q#T",
18400                 type);
18401       error_at (type_start_token->location, "previous definition of %q+#T",
18402                 type);
18403       type = NULL_TREE;
18404       goto done;
18405     }
18406   else if (type == error_mark_node)
18407     type = NULL_TREE;
18408
18409   /* We will have entered the scope containing the class; the names of
18410      base classes should be looked up in that context.  For example:
18411
18412        struct A { struct B {}; struct C; };
18413        struct A::C : B {};
18414
18415      is valid.  */
18416
18417   /* Get the list of base-classes, if there is one.  */
18418   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
18419     *bases = cp_parser_base_clause (parser);
18420
18421  done:
18422   /* Leave the scope given by the nested-name-specifier.  We will
18423      enter the class scope itself while processing the members.  */
18424   if (pushed_scope)
18425     pop_scope (pushed_scope);
18426
18427   if (invalid_explicit_specialization_p)
18428     {
18429       end_specialization ();
18430       --parser->num_template_parameter_lists;
18431     }
18432
18433   if (type)
18434     DECL_SOURCE_LOCATION (TYPE_NAME (type)) = type_start_token->location;
18435   *attributes_p = attributes;
18436   if (type && (virt_specifiers & VIRT_SPEC_FINAL))
18437     CLASSTYPE_FINAL (type) = 1;
18438  out:
18439   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18440   return type;
18441 }
18442
18443 /* Parse a class-key.
18444
18445    class-key:
18446      class
18447      struct
18448      union
18449
18450    Returns the kind of class-key specified, or none_type to indicate
18451    error.  */
18452
18453 static enum tag_types
18454 cp_parser_class_key (cp_parser* parser)
18455 {
18456   cp_token *token;
18457   enum tag_types tag_type;
18458
18459   /* Look for the class-key.  */
18460   token = cp_parser_require (parser, CPP_KEYWORD, RT_CLASS_KEY);
18461   if (!token)
18462     return none_type;
18463
18464   /* Check to see if the TOKEN is a class-key.  */
18465   tag_type = cp_parser_token_is_class_key (token);
18466   if (!tag_type)
18467     cp_parser_error (parser, "expected class-key");
18468   return tag_type;
18469 }
18470
18471 /* Parse an (optional) member-specification.
18472
18473    member-specification:
18474      member-declaration member-specification [opt]
18475      access-specifier : member-specification [opt]  */
18476
18477 static void
18478 cp_parser_member_specification_opt (cp_parser* parser)
18479 {
18480   while (true)
18481     {
18482       cp_token *token;
18483       enum rid keyword;
18484
18485       /* Peek at the next token.  */
18486       token = cp_lexer_peek_token (parser->lexer);
18487       /* If it's a `}', or EOF then we've seen all the members.  */
18488       if (token->type == CPP_CLOSE_BRACE
18489           || token->type == CPP_EOF
18490           || token->type == CPP_PRAGMA_EOL)
18491         break;
18492
18493       /* See if this token is a keyword.  */
18494       keyword = token->keyword;
18495       switch (keyword)
18496         {
18497         case RID_PUBLIC:
18498         case RID_PROTECTED:
18499         case RID_PRIVATE:
18500           /* Consume the access-specifier.  */
18501           cp_lexer_consume_token (parser->lexer);
18502           /* Remember which access-specifier is active.  */
18503           current_access_specifier = token->u.value;
18504           /* Look for the `:'.  */
18505           cp_parser_require (parser, CPP_COLON, RT_COLON);
18506           break;
18507
18508         default:
18509           /* Accept #pragmas at class scope.  */
18510           if (token->type == CPP_PRAGMA)
18511             {
18512               cp_parser_pragma (parser, pragma_external);
18513               break;
18514             }
18515
18516           /* Otherwise, the next construction must be a
18517              member-declaration.  */
18518           cp_parser_member_declaration (parser);
18519         }
18520     }
18521 }
18522
18523 /* Parse a member-declaration.
18524
18525    member-declaration:
18526      decl-specifier-seq [opt] member-declarator-list [opt] ;
18527      function-definition ; [opt]
18528      :: [opt] nested-name-specifier template [opt] unqualified-id ;
18529      using-declaration
18530      template-declaration
18531
18532    member-declarator-list:
18533      member-declarator
18534      member-declarator-list , member-declarator
18535
18536    member-declarator:
18537      declarator pure-specifier [opt]
18538      declarator constant-initializer [opt]
18539      identifier [opt] : constant-expression
18540
18541    GNU Extensions:
18542
18543    member-declaration:
18544      __extension__ member-declaration
18545
18546    member-declarator:
18547      declarator attributes [opt] pure-specifier [opt]
18548      declarator attributes [opt] constant-initializer [opt]
18549      identifier [opt] attributes [opt] : constant-expression  
18550
18551    C++0x Extensions:
18552
18553    member-declaration:
18554      static_assert-declaration  */
18555
18556 static void
18557 cp_parser_member_declaration (cp_parser* parser)
18558 {
18559   cp_decl_specifier_seq decl_specifiers;
18560   tree prefix_attributes;
18561   tree decl;
18562   int declares_class_or_enum;
18563   bool friend_p;
18564   cp_token *token = NULL;
18565   cp_token *decl_spec_token_start = NULL;
18566   cp_token *initializer_token_start = NULL;
18567   int saved_pedantic;
18568   bool saved_colon_corrects_to_scope_p = parser->colon_corrects_to_scope_p;
18569
18570   /* Check for the `__extension__' keyword.  */
18571   if (cp_parser_extension_opt (parser, &saved_pedantic))
18572     {
18573       /* Recurse.  */
18574       cp_parser_member_declaration (parser);
18575       /* Restore the old value of the PEDANTIC flag.  */
18576       pedantic = saved_pedantic;
18577
18578       return;
18579     }
18580
18581   /* Check for a template-declaration.  */
18582   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
18583     {
18584       /* An explicit specialization here is an error condition, and we
18585          expect the specialization handler to detect and report this.  */
18586       if (cp_lexer_peek_nth_token (parser->lexer, 2)->type == CPP_LESS
18587           && cp_lexer_peek_nth_token (parser->lexer, 3)->type == CPP_GREATER)
18588         cp_parser_explicit_specialization (parser);
18589       else
18590         cp_parser_template_declaration (parser, /*member_p=*/true);
18591
18592       return;
18593     }
18594
18595   /* Check for a using-declaration.  */
18596   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_USING))
18597     {
18598       /* Parse the using-declaration.  */
18599       cp_parser_using_declaration (parser,
18600                                    /*access_declaration_p=*/false);
18601       return;
18602     }
18603
18604   /* Check for @defs.  */
18605   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_DEFS))
18606     {
18607       tree ivar, member;
18608       tree ivar_chains = cp_parser_objc_defs_expression (parser);
18609       ivar = ivar_chains;
18610       while (ivar)
18611         {
18612           member = ivar;
18613           ivar = TREE_CHAIN (member);
18614           TREE_CHAIN (member) = NULL_TREE;
18615           finish_member_declaration (member);
18616         }
18617       return;
18618     }
18619
18620   /* If the next token is `static_assert' we have a static assertion.  */
18621   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC_ASSERT))
18622     {
18623       cp_parser_static_assert (parser, /*member_p=*/true);
18624       return;
18625     }
18626
18627   parser->colon_corrects_to_scope_p = false;
18628
18629   if (cp_parser_using_declaration (parser, /*access_declaration=*/true))
18630     goto out;
18631
18632   /* Parse the decl-specifier-seq.  */
18633   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
18634   cp_parser_decl_specifier_seq (parser,
18635                                 CP_PARSER_FLAGS_OPTIONAL,
18636                                 &decl_specifiers,
18637                                 &declares_class_or_enum);
18638   prefix_attributes = decl_specifiers.attributes;
18639   decl_specifiers.attributes = NULL_TREE;
18640   /* Check for an invalid type-name.  */
18641   if (!decl_specifiers.any_type_specifiers_p
18642       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
18643     goto out;
18644   /* If there is no declarator, then the decl-specifier-seq should
18645      specify a type.  */
18646   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
18647     {
18648       /* If there was no decl-specifier-seq, and the next token is a
18649          `;', then we have something like:
18650
18651            struct S { ; };
18652
18653          [class.mem]
18654
18655          Each member-declaration shall declare at least one member
18656          name of the class.  */
18657       if (!decl_specifiers.any_specifiers_p)
18658         {
18659           cp_token *token = cp_lexer_peek_token (parser->lexer);
18660           if (!in_system_header_at (token->location))
18661             pedwarn (token->location, OPT_pedantic, "extra %<;%>");
18662         }
18663       else
18664         {
18665           tree type;
18666
18667           /* See if this declaration is a friend.  */
18668           friend_p = cp_parser_friend_p (&decl_specifiers);
18669           /* If there were decl-specifiers, check to see if there was
18670              a class-declaration.  */
18671           type = check_tag_decl (&decl_specifiers);
18672           /* Nested classes have already been added to the class, but
18673              a `friend' needs to be explicitly registered.  */
18674           if (friend_p)
18675             {
18676               /* If the `friend' keyword was present, the friend must
18677                  be introduced with a class-key.  */
18678                if (!declares_class_or_enum && cxx_dialect < cxx0x)
18679                  pedwarn (decl_spec_token_start->location, OPT_pedantic,
18680                           "in C++03 a class-key must be used "
18681                           "when declaring a friend");
18682                /* In this case:
18683
18684                     template <typename T> struct A {
18685                       friend struct A<T>::B;
18686                     };
18687
18688                   A<T>::B will be represented by a TYPENAME_TYPE, and
18689                   therefore not recognized by check_tag_decl.  */
18690                if (!type)
18691                  {
18692                    type = decl_specifiers.type;
18693                    if (type && TREE_CODE (type) == TYPE_DECL)
18694                      type = TREE_TYPE (type);
18695                  }
18696                if (!type || !TYPE_P (type))
18697                  error_at (decl_spec_token_start->location,
18698                            "friend declaration does not name a class or "
18699                            "function");
18700                else
18701                  make_friend_class (current_class_type, type,
18702                                     /*complain=*/true);
18703             }
18704           /* If there is no TYPE, an error message will already have
18705              been issued.  */
18706           else if (!type || type == error_mark_node)
18707             ;
18708           /* An anonymous aggregate has to be handled specially; such
18709              a declaration really declares a data member (with a
18710              particular type), as opposed to a nested class.  */
18711           else if (ANON_AGGR_TYPE_P (type))
18712             {
18713               /* Remove constructors and such from TYPE, now that we
18714                  know it is an anonymous aggregate.  */
18715               fixup_anonymous_aggr (type);
18716               /* And make the corresponding data member.  */
18717               decl = build_decl (decl_spec_token_start->location,
18718                                  FIELD_DECL, NULL_TREE, type);
18719               /* Add it to the class.  */
18720               finish_member_declaration (decl);
18721             }
18722           else
18723             cp_parser_check_access_in_redeclaration
18724                                               (TYPE_NAME (type),
18725                                                decl_spec_token_start->location);
18726         }
18727     }
18728   else
18729     {
18730       bool assume_semicolon = false;
18731
18732       /* See if these declarations will be friends.  */
18733       friend_p = cp_parser_friend_p (&decl_specifiers);
18734
18735       /* Keep going until we hit the `;' at the end of the
18736          declaration.  */
18737       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
18738         {
18739           tree attributes = NULL_TREE;
18740           tree first_attribute;
18741
18742           /* Peek at the next token.  */
18743           token = cp_lexer_peek_token (parser->lexer);
18744
18745           /* Check for a bitfield declaration.  */
18746           if (token->type == CPP_COLON
18747               || (token->type == CPP_NAME
18748                   && cp_lexer_peek_nth_token (parser->lexer, 2)->type
18749                   == CPP_COLON))
18750             {
18751               tree identifier;
18752               tree width;
18753
18754               /* Get the name of the bitfield.  Note that we cannot just
18755                  check TOKEN here because it may have been invalidated by
18756                  the call to cp_lexer_peek_nth_token above.  */
18757               if (cp_lexer_peek_token (parser->lexer)->type != CPP_COLON)
18758                 identifier = cp_parser_identifier (parser);
18759               else
18760                 identifier = NULL_TREE;
18761
18762               /* Consume the `:' token.  */
18763               cp_lexer_consume_token (parser->lexer);
18764               /* Get the width of the bitfield.  */
18765               width
18766                 = cp_parser_constant_expression (parser,
18767                                                  /*allow_non_constant=*/false,
18768                                                  NULL);
18769
18770               /* Look for attributes that apply to the bitfield.  */
18771               attributes = cp_parser_attributes_opt (parser);
18772               /* Remember which attributes are prefix attributes and
18773                  which are not.  */
18774               first_attribute = attributes;
18775               /* Combine the attributes.  */
18776               attributes = chainon (prefix_attributes, attributes);
18777
18778               /* Create the bitfield declaration.  */
18779               decl = grokbitfield (identifier
18780                                    ? make_id_declarator (NULL_TREE,
18781                                                          identifier,
18782                                                          sfk_none)
18783                                    : NULL,
18784                                    &decl_specifiers,
18785                                    width,
18786                                    attributes);
18787             }
18788           else
18789             {
18790               cp_declarator *declarator;
18791               tree initializer;
18792               tree asm_specification;
18793               int ctor_dtor_or_conv_p;
18794
18795               /* Parse the declarator.  */
18796               declarator
18797                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
18798                                         &ctor_dtor_or_conv_p,
18799                                         /*parenthesized_p=*/NULL,
18800                                         /*member_p=*/true);
18801
18802               /* If something went wrong parsing the declarator, make sure
18803                  that we at least consume some tokens.  */
18804               if (declarator == cp_error_declarator)
18805                 {
18806                   /* Skip to the end of the statement.  */
18807                   cp_parser_skip_to_end_of_statement (parser);
18808                   /* If the next token is not a semicolon, that is
18809                      probably because we just skipped over the body of
18810                      a function.  So, we consume a semicolon if
18811                      present, but do not issue an error message if it
18812                      is not present.  */
18813                   if (cp_lexer_next_token_is (parser->lexer,
18814                                               CPP_SEMICOLON))
18815                     cp_lexer_consume_token (parser->lexer);
18816                   goto out;
18817                 }
18818
18819               if (declares_class_or_enum & 2)
18820                 cp_parser_check_for_definition_in_return_type
18821                                             (declarator, decl_specifiers.type,
18822                                              decl_specifiers.type_location);
18823
18824               /* Look for an asm-specification.  */
18825               asm_specification = cp_parser_asm_specification_opt (parser);
18826               /* Look for attributes that apply to the declaration.  */
18827               attributes = cp_parser_attributes_opt (parser);
18828               /* Remember which attributes are prefix attributes and
18829                  which are not.  */
18830               first_attribute = attributes;
18831               /* Combine the attributes.  */
18832               attributes = chainon (prefix_attributes, attributes);
18833
18834               /* If it's an `=', then we have a constant-initializer or a
18835                  pure-specifier.  It is not correct to parse the
18836                  initializer before registering the member declaration
18837                  since the member declaration should be in scope while
18838                  its initializer is processed.  However, the rest of the
18839                  front end does not yet provide an interface that allows
18840                  us to handle this correctly.  */
18841               if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
18842                 {
18843                   /* In [class.mem]:
18844
18845                      A pure-specifier shall be used only in the declaration of
18846                      a virtual function.
18847
18848                      A member-declarator can contain a constant-initializer
18849                      only if it declares a static member of integral or
18850                      enumeration type.
18851
18852                      Therefore, if the DECLARATOR is for a function, we look
18853                      for a pure-specifier; otherwise, we look for a
18854                      constant-initializer.  When we call `grokfield', it will
18855                      perform more stringent semantics checks.  */
18856                   initializer_token_start = cp_lexer_peek_token (parser->lexer);
18857                   if (function_declarator_p (declarator)
18858                       || (decl_specifiers.type
18859                           && TREE_CODE (decl_specifiers.type) == TYPE_DECL
18860                           && (TREE_CODE (TREE_TYPE (decl_specifiers.type))
18861                               == FUNCTION_TYPE)))
18862                     initializer = cp_parser_pure_specifier (parser);
18863                   else if (decl_specifiers.storage_class != sc_static)
18864                     initializer = cp_parser_save_nsdmi (parser);
18865                   else if (cxx_dialect >= cxx0x)
18866                     {
18867                       bool nonconst;
18868                       /* Don't require a constant rvalue in C++11, since we
18869                          might want a reference constant.  We'll enforce
18870                          constancy later.  */
18871                       cp_lexer_consume_token (parser->lexer);
18872                       /* Parse the initializer.  */
18873                       initializer = cp_parser_initializer_clause (parser,
18874                                                                   &nonconst);
18875                     }
18876                   else
18877                     /* Parse the initializer.  */
18878                     initializer = cp_parser_constant_initializer (parser);
18879                 }
18880               else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE)
18881                        && !function_declarator_p (declarator))
18882                 {
18883                   bool x;
18884                   if (decl_specifiers.storage_class != sc_static)
18885                     initializer = cp_parser_save_nsdmi (parser);
18886                   else
18887                     initializer = cp_parser_initializer (parser, &x, &x);
18888                 }
18889               /* Otherwise, there is no initializer.  */
18890               else
18891                 initializer = NULL_TREE;
18892
18893               /* See if we are probably looking at a function
18894                  definition.  We are certainly not looking at a
18895                  member-declarator.  Calling `grokfield' has
18896                  side-effects, so we must not do it unless we are sure
18897                  that we are looking at a member-declarator.  */
18898               if (cp_parser_token_starts_function_definition_p
18899                   (cp_lexer_peek_token (parser->lexer)))
18900                 {
18901                   /* The grammar does not allow a pure-specifier to be
18902                      used when a member function is defined.  (It is
18903                      possible that this fact is an oversight in the
18904                      standard, since a pure function may be defined
18905                      outside of the class-specifier.  */
18906                   if (initializer)
18907                     error_at (initializer_token_start->location,
18908                               "pure-specifier on function-definition");
18909                   decl = cp_parser_save_member_function_body (parser,
18910                                                               &decl_specifiers,
18911                                                               declarator,
18912                                                               attributes);
18913                   /* If the member was not a friend, declare it here.  */
18914                   if (!friend_p)
18915                     finish_member_declaration (decl);
18916                   /* Peek at the next token.  */
18917                   token = cp_lexer_peek_token (parser->lexer);
18918                   /* If the next token is a semicolon, consume it.  */
18919                   if (token->type == CPP_SEMICOLON)
18920                     cp_lexer_consume_token (parser->lexer);
18921                   goto out;
18922                 }
18923               else
18924                 if (declarator->kind == cdk_function)
18925                   declarator->id_loc = token->location;
18926                 /* Create the declaration.  */
18927                 decl = grokfield (declarator, &decl_specifiers,
18928                                   initializer, /*init_const_expr_p=*/true,
18929                                   asm_specification,
18930                                   attributes);
18931             }
18932
18933           /* Reset PREFIX_ATTRIBUTES.  */
18934           while (attributes && TREE_CHAIN (attributes) != first_attribute)
18935             attributes = TREE_CHAIN (attributes);
18936           if (attributes)
18937             TREE_CHAIN (attributes) = NULL_TREE;
18938
18939           /* If there is any qualification still in effect, clear it
18940              now; we will be starting fresh with the next declarator.  */
18941           parser->scope = NULL_TREE;
18942           parser->qualifying_scope = NULL_TREE;
18943           parser->object_scope = NULL_TREE;
18944           /* If it's a `,', then there are more declarators.  */
18945           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
18946             cp_lexer_consume_token (parser->lexer);
18947           /* If the next token isn't a `;', then we have a parse error.  */
18948           else if (cp_lexer_next_token_is_not (parser->lexer,
18949                                                CPP_SEMICOLON))
18950             {
18951               /* The next token might be a ways away from where the
18952                  actual semicolon is missing.  Find the previous token
18953                  and use that for our error position.  */
18954               cp_token *token = cp_lexer_previous_token (parser->lexer);
18955               error_at (token->location,
18956                         "expected %<;%> at end of member declaration");
18957
18958               /* Assume that the user meant to provide a semicolon.  If
18959                  we were to cp_parser_skip_to_end_of_statement, we might
18960                  skip to a semicolon inside a member function definition
18961                  and issue nonsensical error messages.  */
18962               assume_semicolon = true;
18963             }
18964
18965           if (decl)
18966             {
18967               /* Add DECL to the list of members.  */
18968               if (!friend_p)
18969                 finish_member_declaration (decl);
18970
18971               if (TREE_CODE (decl) == FUNCTION_DECL)
18972                 cp_parser_save_default_args (parser, decl);
18973               else if (TREE_CODE (decl) == FIELD_DECL
18974                        && !DECL_C_BIT_FIELD (decl)
18975                        && DECL_INITIAL (decl))
18976                 /* Add DECL to the queue of NSDMI to be parsed later.  */
18977                 VEC_safe_push (tree, gc, unparsed_nsdmis, decl);
18978             }
18979
18980           if (assume_semicolon)
18981             goto out;
18982         }
18983     }
18984
18985   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
18986  out:
18987   parser->colon_corrects_to_scope_p = saved_colon_corrects_to_scope_p;
18988 }
18989
18990 /* Parse a pure-specifier.
18991
18992    pure-specifier:
18993      = 0
18994
18995    Returns INTEGER_ZERO_NODE if a pure specifier is found.
18996    Otherwise, ERROR_MARK_NODE is returned.  */
18997
18998 static tree
18999 cp_parser_pure_specifier (cp_parser* parser)
19000 {
19001   cp_token *token;
19002
19003   /* Look for the `=' token.  */
19004   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19005     return error_mark_node;
19006   /* Look for the `0' token.  */
19007   token = cp_lexer_peek_token (parser->lexer);
19008
19009   if (token->type == CPP_EOF
19010       || token->type == CPP_PRAGMA_EOL)
19011     return error_mark_node;
19012
19013   cp_lexer_consume_token (parser->lexer);
19014
19015   /* Accept = default or = delete in c++0x mode.  */
19016   if (token->keyword == RID_DEFAULT
19017       || token->keyword == RID_DELETE)
19018     {
19019       maybe_warn_cpp0x (CPP0X_DEFAULTED_DELETED);
19020       return token->u.value;
19021     }
19022
19023   /* c_lex_with_flags marks a single digit '0' with PURE_ZERO.  */
19024   if (token->type != CPP_NUMBER || !(token->flags & PURE_ZERO))
19025     {
19026       cp_parser_error (parser,
19027                        "invalid pure specifier (only %<= 0%> is allowed)");
19028       cp_parser_skip_to_end_of_statement (parser);
19029       return error_mark_node;
19030     }
19031   if (PROCESSING_REAL_TEMPLATE_DECL_P ())
19032     {
19033       error_at (token->location, "templates may not be %<virtual%>");
19034       return error_mark_node;
19035     }
19036
19037   return integer_zero_node;
19038 }
19039
19040 /* Parse a constant-initializer.
19041
19042    constant-initializer:
19043      = constant-expression
19044
19045    Returns a representation of the constant-expression.  */
19046
19047 static tree
19048 cp_parser_constant_initializer (cp_parser* parser)
19049 {
19050   /* Look for the `=' token.  */
19051   if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
19052     return error_mark_node;
19053
19054   /* It is invalid to write:
19055
19056        struct S { static const int i = { 7 }; };
19057
19058      */
19059   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
19060     {
19061       cp_parser_error (parser,
19062                        "a brace-enclosed initializer is not allowed here");
19063       /* Consume the opening brace.  */
19064       cp_lexer_consume_token (parser->lexer);
19065       /* Skip the initializer.  */
19066       cp_parser_skip_to_closing_brace (parser);
19067       /* Look for the trailing `}'.  */
19068       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
19069
19070       return error_mark_node;
19071     }
19072
19073   return cp_parser_constant_expression (parser,
19074                                         /*allow_non_constant=*/false,
19075                                         NULL);
19076 }
19077
19078 /* Derived classes [gram.class.derived] */
19079
19080 /* Parse a base-clause.
19081
19082    base-clause:
19083      : base-specifier-list
19084
19085    base-specifier-list:
19086      base-specifier ... [opt]
19087      base-specifier-list , base-specifier ... [opt]
19088
19089    Returns a TREE_LIST representing the base-classes, in the order in
19090    which they were declared.  The representation of each node is as
19091    described by cp_parser_base_specifier.
19092
19093    In the case that no bases are specified, this function will return
19094    NULL_TREE, not ERROR_MARK_NODE.  */
19095
19096 static tree
19097 cp_parser_base_clause (cp_parser* parser)
19098 {
19099   tree bases = NULL_TREE;
19100
19101   /* Look for the `:' that begins the list.  */
19102   cp_parser_require (parser, CPP_COLON, RT_COLON);
19103
19104   /* Scan the base-specifier-list.  */
19105   while (true)
19106     {
19107       cp_token *token;
19108       tree base;
19109       bool pack_expansion_p = false;
19110
19111       /* Look for the base-specifier.  */
19112       base = cp_parser_base_specifier (parser);
19113       /* Look for the (optional) ellipsis. */
19114       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19115         {
19116           /* Consume the `...'. */
19117           cp_lexer_consume_token (parser->lexer);
19118
19119           pack_expansion_p = true;
19120         }
19121
19122       /* Add BASE to the front of the list.  */
19123       if (base && base != error_mark_node)
19124         {
19125           if (pack_expansion_p)
19126             /* Make this a pack expansion type. */
19127             TREE_VALUE (base) = make_pack_expansion (TREE_VALUE (base));
19128
19129           if (!check_for_bare_parameter_packs (TREE_VALUE (base)))
19130             {
19131               TREE_CHAIN (base) = bases;
19132               bases = base;
19133             }
19134         }
19135       /* Peek at the next token.  */
19136       token = cp_lexer_peek_token (parser->lexer);
19137       /* If it's not a comma, then the list is complete.  */
19138       if (token->type != CPP_COMMA)
19139         break;
19140       /* Consume the `,'.  */
19141       cp_lexer_consume_token (parser->lexer);
19142     }
19143
19144   /* PARSER->SCOPE may still be non-NULL at this point, if the last
19145      base class had a qualified name.  However, the next name that
19146      appears is certainly not qualified.  */
19147   parser->scope = NULL_TREE;
19148   parser->qualifying_scope = NULL_TREE;
19149   parser->object_scope = NULL_TREE;
19150
19151   return nreverse (bases);
19152 }
19153
19154 /* Parse a base-specifier.
19155
19156    base-specifier:
19157      :: [opt] nested-name-specifier [opt] class-name
19158      virtual access-specifier [opt] :: [opt] nested-name-specifier
19159        [opt] class-name
19160      access-specifier virtual [opt] :: [opt] nested-name-specifier
19161        [opt] class-name
19162
19163    Returns a TREE_LIST.  The TREE_PURPOSE will be one of
19164    ACCESS_{DEFAULT,PUBLIC,PROTECTED,PRIVATE}_[VIRTUAL]_NODE to
19165    indicate the specifiers provided.  The TREE_VALUE will be a TYPE
19166    (or the ERROR_MARK_NODE) indicating the type that was specified.  */
19167
19168 static tree
19169 cp_parser_base_specifier (cp_parser* parser)
19170 {
19171   cp_token *token;
19172   bool done = false;
19173   bool virtual_p = false;
19174   bool duplicate_virtual_error_issued_p = false;
19175   bool duplicate_access_error_issued_p = false;
19176   bool class_scope_p, template_p;
19177   tree access = access_default_node;
19178   tree type;
19179
19180   /* Process the optional `virtual' and `access-specifier'.  */
19181   while (!done)
19182     {
19183       /* Peek at the next token.  */
19184       token = cp_lexer_peek_token (parser->lexer);
19185       /* Process `virtual'.  */
19186       switch (token->keyword)
19187         {
19188         case RID_VIRTUAL:
19189           /* If `virtual' appears more than once, issue an error.  */
19190           if (virtual_p && !duplicate_virtual_error_issued_p)
19191             {
19192               cp_parser_error (parser,
19193                                "%<virtual%> specified more than once in base-specified");
19194               duplicate_virtual_error_issued_p = true;
19195             }
19196
19197           virtual_p = true;
19198
19199           /* Consume the `virtual' token.  */
19200           cp_lexer_consume_token (parser->lexer);
19201
19202           break;
19203
19204         case RID_PUBLIC:
19205         case RID_PROTECTED:
19206         case RID_PRIVATE:
19207           /* If more than one access specifier appears, issue an
19208              error.  */
19209           if (access != access_default_node
19210               && !duplicate_access_error_issued_p)
19211             {
19212               cp_parser_error (parser,
19213                                "more than one access specifier in base-specified");
19214               duplicate_access_error_issued_p = true;
19215             }
19216
19217           access = ridpointers[(int) token->keyword];
19218
19219           /* Consume the access-specifier.  */
19220           cp_lexer_consume_token (parser->lexer);
19221
19222           break;
19223
19224         default:
19225           done = true;
19226           break;
19227         }
19228     }
19229   /* It is not uncommon to see programs mechanically, erroneously, use
19230      the 'typename' keyword to denote (dependent) qualified types
19231      as base classes.  */
19232   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TYPENAME))
19233     {
19234       token = cp_lexer_peek_token (parser->lexer);
19235       if (!processing_template_decl)
19236         error_at (token->location,
19237                   "keyword %<typename%> not allowed outside of templates");
19238       else
19239         error_at (token->location,
19240                   "keyword %<typename%> not allowed in this context "
19241                   "(the base class is implicitly a type)");
19242       cp_lexer_consume_token (parser->lexer);
19243     }
19244
19245   /* Look for the optional `::' operator.  */
19246   cp_parser_global_scope_opt (parser, /*current_scope_valid_p=*/false);
19247   /* Look for the nested-name-specifier.  The simplest way to
19248      implement:
19249
19250        [temp.res]
19251
19252        The keyword `typename' is not permitted in a base-specifier or
19253        mem-initializer; in these contexts a qualified name that
19254        depends on a template-parameter is implicitly assumed to be a
19255        type name.
19256
19257      is to pretend that we have seen the `typename' keyword at this
19258      point.  */
19259   cp_parser_nested_name_specifier_opt (parser,
19260                                        /*typename_keyword_p=*/true,
19261                                        /*check_dependency_p=*/true,
19262                                        typename_type,
19263                                        /*is_declaration=*/true);
19264   /* If the base class is given by a qualified name, assume that names
19265      we see are type names or templates, as appropriate.  */
19266   class_scope_p = (parser->scope && TYPE_P (parser->scope));
19267   template_p = class_scope_p && cp_parser_optional_template_keyword (parser);
19268
19269   if (!parser->scope
19270       && cp_lexer_next_token_is_decltype (parser->lexer))
19271     /* DR 950 allows decltype as a base-specifier.  */
19272     type = cp_parser_decltype (parser);
19273   else
19274     {
19275       /* Otherwise, look for the class-name.  */
19276       type = cp_parser_class_name (parser,
19277                                    class_scope_p,
19278                                    template_p,
19279                                    typename_type,
19280                                    /*check_dependency_p=*/true,
19281                                    /*class_head_p=*/false,
19282                                    /*is_declaration=*/true);
19283       type = TREE_TYPE (type);
19284     }
19285
19286   if (type == error_mark_node)
19287     return error_mark_node;
19288
19289   return finish_base_specifier (type, access, virtual_p);
19290 }
19291
19292 /* Exception handling [gram.exception] */
19293
19294 /* Parse an (optional) exception-specification.
19295
19296    exception-specification:
19297      throw ( type-id-list [opt] )
19298
19299    Returns a TREE_LIST representing the exception-specification.  The
19300    TREE_VALUE of each node is a type.  */
19301
19302 static tree
19303 cp_parser_exception_specification_opt (cp_parser* parser)
19304 {
19305   cp_token *token;
19306   tree type_id_list;
19307   const char *saved_message;
19308
19309   /* Peek at the next token.  */
19310   token = cp_lexer_peek_token (parser->lexer);
19311
19312   /* Is it a noexcept-specification?  */
19313   if (cp_parser_is_keyword (token, RID_NOEXCEPT))
19314     {
19315       tree expr;
19316       cp_lexer_consume_token (parser->lexer);
19317
19318       if (cp_lexer_peek_token (parser->lexer)->type == CPP_OPEN_PAREN)
19319         {
19320           cp_lexer_consume_token (parser->lexer);
19321
19322           /* Types may not be defined in an exception-specification.  */
19323           saved_message = parser->type_definition_forbidden_message;
19324           parser->type_definition_forbidden_message
19325             = G_("types may not be defined in an exception-specification");
19326
19327           expr = cp_parser_constant_expression (parser, false, NULL);
19328
19329           /* Restore the saved message.  */
19330           parser->type_definition_forbidden_message = saved_message;
19331
19332           cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19333         }
19334       else
19335         expr = boolean_true_node;
19336
19337       return build_noexcept_spec (expr, tf_warning_or_error);
19338     }
19339
19340   /* If it's not `throw', then there's no exception-specification.  */
19341   if (!cp_parser_is_keyword (token, RID_THROW))
19342     return NULL_TREE;
19343
19344 #if 0
19345   /* Enable this once a lot of code has transitioned to noexcept?  */
19346   if (cxx_dialect == cxx0x && !in_system_header)
19347     warning (OPT_Wdeprecated, "dynamic exception specifications are "
19348              "deprecated in C++0x; use %<noexcept%> instead");
19349 #endif
19350
19351   /* Consume the `throw'.  */
19352   cp_lexer_consume_token (parser->lexer);
19353
19354   /* Look for the `('.  */
19355   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19356
19357   /* Peek at the next token.  */
19358   token = cp_lexer_peek_token (parser->lexer);
19359   /* If it's not a `)', then there is a type-id-list.  */
19360   if (token->type != CPP_CLOSE_PAREN)
19361     {
19362       /* Types may not be defined in an exception-specification.  */
19363       saved_message = parser->type_definition_forbidden_message;
19364       parser->type_definition_forbidden_message
19365         = G_("types may not be defined in an exception-specification");
19366       /* Parse the type-id-list.  */
19367       type_id_list = cp_parser_type_id_list (parser);
19368       /* Restore the saved message.  */
19369       parser->type_definition_forbidden_message = saved_message;
19370     }
19371   else
19372     type_id_list = empty_except_spec;
19373
19374   /* Look for the `)'.  */
19375   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19376
19377   return type_id_list;
19378 }
19379
19380 /* Parse an (optional) type-id-list.
19381
19382    type-id-list:
19383      type-id ... [opt]
19384      type-id-list , type-id ... [opt]
19385
19386    Returns a TREE_LIST.  The TREE_VALUE of each node is a TYPE,
19387    in the order that the types were presented.  */
19388
19389 static tree
19390 cp_parser_type_id_list (cp_parser* parser)
19391 {
19392   tree types = NULL_TREE;
19393
19394   while (true)
19395     {
19396       cp_token *token;
19397       tree type;
19398
19399       /* Get the next type-id.  */
19400       type = cp_parser_type_id (parser);
19401       /* Parse the optional ellipsis. */
19402       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19403         {
19404           /* Consume the `...'. */
19405           cp_lexer_consume_token (parser->lexer);
19406
19407           /* Turn the type into a pack expansion expression. */
19408           type = make_pack_expansion (type);
19409         }
19410       /* Add it to the list.  */
19411       types = add_exception_specifier (types, type, /*complain=*/1);
19412       /* Peek at the next token.  */
19413       token = cp_lexer_peek_token (parser->lexer);
19414       /* If it is not a `,', we are done.  */
19415       if (token->type != CPP_COMMA)
19416         break;
19417       /* Consume the `,'.  */
19418       cp_lexer_consume_token (parser->lexer);
19419     }
19420
19421   return nreverse (types);
19422 }
19423
19424 /* Parse a try-block.
19425
19426    try-block:
19427      try compound-statement handler-seq  */
19428
19429 static tree
19430 cp_parser_try_block (cp_parser* parser)
19431 {
19432   tree try_block;
19433
19434   cp_parser_require_keyword (parser, RID_TRY, RT_TRY);
19435   try_block = begin_try_block ();
19436   cp_parser_compound_statement (parser, NULL, true, false);
19437   finish_try_block (try_block);
19438   cp_parser_handler_seq (parser);
19439   finish_handler_sequence (try_block);
19440
19441   return try_block;
19442 }
19443
19444 /* Parse a function-try-block.
19445
19446    function-try-block:
19447      try ctor-initializer [opt] function-body handler-seq  */
19448
19449 static bool
19450 cp_parser_function_try_block (cp_parser* parser)
19451 {
19452   tree compound_stmt;
19453   tree try_block;
19454   bool ctor_initializer_p;
19455
19456   /* Look for the `try' keyword.  */
19457   if (!cp_parser_require_keyword (parser, RID_TRY, RT_TRY))
19458     return false;
19459   /* Let the rest of the front end know where we are.  */
19460   try_block = begin_function_try_block (&compound_stmt);
19461   /* Parse the function-body.  */
19462   ctor_initializer_p
19463     = cp_parser_ctor_initializer_opt_and_function_body (parser);
19464   /* We're done with the `try' part.  */
19465   finish_function_try_block (try_block);
19466   /* Parse the handlers.  */
19467   cp_parser_handler_seq (parser);
19468   /* We're done with the handlers.  */
19469   finish_function_handler_sequence (try_block, compound_stmt);
19470
19471   return ctor_initializer_p;
19472 }
19473
19474 /* Parse a handler-seq.
19475
19476    handler-seq:
19477      handler handler-seq [opt]  */
19478
19479 static void
19480 cp_parser_handler_seq (cp_parser* parser)
19481 {
19482   while (true)
19483     {
19484       cp_token *token;
19485
19486       /* Parse the handler.  */
19487       cp_parser_handler (parser);
19488       /* Peek at the next token.  */
19489       token = cp_lexer_peek_token (parser->lexer);
19490       /* If it's not `catch' then there are no more handlers.  */
19491       if (!cp_parser_is_keyword (token, RID_CATCH))
19492         break;
19493     }
19494 }
19495
19496 /* Parse a handler.
19497
19498    handler:
19499      catch ( exception-declaration ) compound-statement  */
19500
19501 static void
19502 cp_parser_handler (cp_parser* parser)
19503 {
19504   tree handler;
19505   tree declaration;
19506
19507   cp_parser_require_keyword (parser, RID_CATCH, RT_CATCH);
19508   handler = begin_handler ();
19509   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19510   declaration = cp_parser_exception_declaration (parser);
19511   finish_handler_parms (declaration, handler);
19512   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19513   cp_parser_compound_statement (parser, NULL, false, false);
19514   finish_handler (handler);
19515 }
19516
19517 /* Parse an exception-declaration.
19518
19519    exception-declaration:
19520      type-specifier-seq declarator
19521      type-specifier-seq abstract-declarator
19522      type-specifier-seq
19523      ...
19524
19525    Returns a VAR_DECL for the declaration, or NULL_TREE if the
19526    ellipsis variant is used.  */
19527
19528 static tree
19529 cp_parser_exception_declaration (cp_parser* parser)
19530 {
19531   cp_decl_specifier_seq type_specifiers;
19532   cp_declarator *declarator;
19533   const char *saved_message;
19534
19535   /* If it's an ellipsis, it's easy to handle.  */
19536   if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
19537     {
19538       /* Consume the `...' token.  */
19539       cp_lexer_consume_token (parser->lexer);
19540       return NULL_TREE;
19541     }
19542
19543   /* Types may not be defined in exception-declarations.  */
19544   saved_message = parser->type_definition_forbidden_message;
19545   parser->type_definition_forbidden_message
19546     = G_("types may not be defined in exception-declarations");
19547
19548   /* Parse the type-specifier-seq.  */
19549   cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
19550                                 /*is_trailing_return=*/false,
19551                                 &type_specifiers);
19552   /* If it's a `)', then there is no declarator.  */
19553   if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
19554     declarator = NULL;
19555   else
19556     declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_EITHER,
19557                                        /*ctor_dtor_or_conv_p=*/NULL,
19558                                        /*parenthesized_p=*/NULL,
19559                                        /*member_p=*/false);
19560
19561   /* Restore the saved message.  */
19562   parser->type_definition_forbidden_message = saved_message;
19563
19564   if (!type_specifiers.any_specifiers_p)
19565     return error_mark_node;
19566
19567   return grokdeclarator (declarator, &type_specifiers, CATCHPARM, 1, NULL);
19568 }
19569
19570 /* Parse a throw-expression.
19571
19572    throw-expression:
19573      throw assignment-expression [opt]
19574
19575    Returns a THROW_EXPR representing the throw-expression.  */
19576
19577 static tree
19578 cp_parser_throw_expression (cp_parser* parser)
19579 {
19580   tree expression;
19581   cp_token* token;
19582
19583   cp_parser_require_keyword (parser, RID_THROW, RT_THROW);
19584   token = cp_lexer_peek_token (parser->lexer);
19585   /* Figure out whether or not there is an assignment-expression
19586      following the "throw" keyword.  */
19587   if (token->type == CPP_COMMA
19588       || token->type == CPP_SEMICOLON
19589       || token->type == CPP_CLOSE_PAREN
19590       || token->type == CPP_CLOSE_SQUARE
19591       || token->type == CPP_CLOSE_BRACE
19592       || token->type == CPP_COLON)
19593     expression = NULL_TREE;
19594   else
19595     expression = cp_parser_assignment_expression (parser,
19596                                                   /*cast_p=*/false, NULL);
19597
19598   return build_throw (expression);
19599 }
19600
19601 /* GNU Extensions */
19602
19603 /* Parse an (optional) asm-specification.
19604
19605    asm-specification:
19606      asm ( string-literal )
19607
19608    If the asm-specification is present, returns a STRING_CST
19609    corresponding to the string-literal.  Otherwise, returns
19610    NULL_TREE.  */
19611
19612 static tree
19613 cp_parser_asm_specification_opt (cp_parser* parser)
19614 {
19615   cp_token *token;
19616   tree asm_specification;
19617
19618   /* Peek at the next token.  */
19619   token = cp_lexer_peek_token (parser->lexer);
19620   /* If the next token isn't the `asm' keyword, then there's no
19621      asm-specification.  */
19622   if (!cp_parser_is_keyword (token, RID_ASM))
19623     return NULL_TREE;
19624
19625   /* Consume the `asm' token.  */
19626   cp_lexer_consume_token (parser->lexer);
19627   /* Look for the `('.  */
19628   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19629
19630   /* Look for the string-literal.  */
19631   asm_specification = cp_parser_string_literal (parser, false, false);
19632
19633   /* Look for the `)'.  */
19634   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19635
19636   return asm_specification;
19637 }
19638
19639 /* Parse an asm-operand-list.
19640
19641    asm-operand-list:
19642      asm-operand
19643      asm-operand-list , asm-operand
19644
19645    asm-operand:
19646      string-literal ( expression )
19647      [ string-literal ] string-literal ( expression )
19648
19649    Returns a TREE_LIST representing the operands.  The TREE_VALUE of
19650    each node is the expression.  The TREE_PURPOSE is itself a
19651    TREE_LIST whose TREE_PURPOSE is a STRING_CST for the bracketed
19652    string-literal (or NULL_TREE if not present) and whose TREE_VALUE
19653    is a STRING_CST for the string literal before the parenthesis. Returns
19654    ERROR_MARK_NODE if any of the operands are invalid.  */
19655
19656 static tree
19657 cp_parser_asm_operand_list (cp_parser* parser)
19658 {
19659   tree asm_operands = NULL_TREE;
19660   bool invalid_operands = false;
19661
19662   while (true)
19663     {
19664       tree string_literal;
19665       tree expression;
19666       tree name;
19667
19668       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_SQUARE))
19669         {
19670           /* Consume the `[' token.  */
19671           cp_lexer_consume_token (parser->lexer);
19672           /* Read the operand name.  */
19673           name = cp_parser_identifier (parser);
19674           if (name != error_mark_node)
19675             name = build_string (IDENTIFIER_LENGTH (name),
19676                                  IDENTIFIER_POINTER (name));
19677           /* Look for the closing `]'.  */
19678           cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
19679         }
19680       else
19681         name = NULL_TREE;
19682       /* Look for the string-literal.  */
19683       string_literal = cp_parser_string_literal (parser, false, false);
19684
19685       /* Look for the `('.  */
19686       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19687       /* Parse the expression.  */
19688       expression = cp_parser_expression (parser, /*cast_p=*/false, NULL);
19689       /* Look for the `)'.  */
19690       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19691
19692       if (name == error_mark_node 
19693           || string_literal == error_mark_node 
19694           || expression == error_mark_node)
19695         invalid_operands = true;
19696
19697       /* Add this operand to the list.  */
19698       asm_operands = tree_cons (build_tree_list (name, string_literal),
19699                                 expression,
19700                                 asm_operands);
19701       /* If the next token is not a `,', there are no more
19702          operands.  */
19703       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19704         break;
19705       /* Consume the `,'.  */
19706       cp_lexer_consume_token (parser->lexer);
19707     }
19708
19709   return invalid_operands ? error_mark_node : nreverse (asm_operands);
19710 }
19711
19712 /* Parse an asm-clobber-list.
19713
19714    asm-clobber-list:
19715      string-literal
19716      asm-clobber-list , string-literal
19717
19718    Returns a TREE_LIST, indicating the clobbers in the order that they
19719    appeared.  The TREE_VALUE of each node is a STRING_CST.  */
19720
19721 static tree
19722 cp_parser_asm_clobber_list (cp_parser* parser)
19723 {
19724   tree clobbers = NULL_TREE;
19725
19726   while (true)
19727     {
19728       tree string_literal;
19729
19730       /* Look for the string literal.  */
19731       string_literal = cp_parser_string_literal (parser, false, false);
19732       /* Add it to the list.  */
19733       clobbers = tree_cons (NULL_TREE, string_literal, clobbers);
19734       /* If the next token is not a `,', then the list is
19735          complete.  */
19736       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19737         break;
19738       /* Consume the `,' token.  */
19739       cp_lexer_consume_token (parser->lexer);
19740     }
19741
19742   return clobbers;
19743 }
19744
19745 /* Parse an asm-label-list.
19746
19747    asm-label-list:
19748      identifier
19749      asm-label-list , identifier
19750
19751    Returns a TREE_LIST, indicating the labels in the order that they
19752    appeared.  The TREE_VALUE of each node is a label.  */
19753
19754 static tree
19755 cp_parser_asm_label_list (cp_parser* parser)
19756 {
19757   tree labels = NULL_TREE;
19758
19759   while (true)
19760     {
19761       tree identifier, label, name;
19762
19763       /* Look for the identifier.  */
19764       identifier = cp_parser_identifier (parser);
19765       if (!error_operand_p (identifier))
19766         {
19767           label = lookup_label (identifier);
19768           if (TREE_CODE (label) == LABEL_DECL)
19769             {
19770               TREE_USED (label) = 1;
19771               check_goto (label);
19772               name = build_string (IDENTIFIER_LENGTH (identifier),
19773                                    IDENTIFIER_POINTER (identifier));
19774               labels = tree_cons (name, label, labels);
19775             }
19776         }
19777       /* If the next token is not a `,', then the list is
19778          complete.  */
19779       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
19780         break;
19781       /* Consume the `,' token.  */
19782       cp_lexer_consume_token (parser->lexer);
19783     }
19784
19785   return nreverse (labels);
19786 }
19787
19788 /* Parse an (optional) series of attributes.
19789
19790    attributes:
19791      attributes attribute
19792
19793    attribute:
19794      __attribute__ (( attribute-list [opt] ))
19795
19796    The return value is as for cp_parser_attribute_list.  */
19797
19798 static tree
19799 cp_parser_attributes_opt (cp_parser* parser)
19800 {
19801   tree attributes = NULL_TREE;
19802
19803   while (true)
19804     {
19805       cp_token *token;
19806       tree attribute_list;
19807
19808       /* Peek at the next token.  */
19809       token = cp_lexer_peek_token (parser->lexer);
19810       /* If it's not `__attribute__', then we're done.  */
19811       if (token->keyword != RID_ATTRIBUTE)
19812         break;
19813
19814       /* Consume the `__attribute__' keyword.  */
19815       cp_lexer_consume_token (parser->lexer);
19816       /* Look for the two `(' tokens.  */
19817       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19818       cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
19819
19820       /* Peek at the next token.  */
19821       token = cp_lexer_peek_token (parser->lexer);
19822       if (token->type != CPP_CLOSE_PAREN)
19823         /* Parse the attribute-list.  */
19824         attribute_list = cp_parser_attribute_list (parser);
19825       else
19826         /* If the next token is a `)', then there is no attribute
19827            list.  */
19828         attribute_list = NULL;
19829
19830       /* Look for the two `)' tokens.  */
19831       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19832       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
19833
19834       /* Add these new attributes to the list.  */
19835       attributes = chainon (attributes, attribute_list);
19836     }
19837
19838   return attributes;
19839 }
19840
19841 /* Parse an attribute-list.
19842
19843    attribute-list:
19844      attribute
19845      attribute-list , attribute
19846
19847    attribute:
19848      identifier
19849      identifier ( identifier )
19850      identifier ( identifier , expression-list )
19851      identifier ( expression-list )
19852
19853    Returns a TREE_LIST, or NULL_TREE on error.  Each node corresponds
19854    to an attribute.  The TREE_PURPOSE of each node is the identifier
19855    indicating which attribute is in use.  The TREE_VALUE represents
19856    the arguments, if any.  */
19857
19858 static tree
19859 cp_parser_attribute_list (cp_parser* parser)
19860 {
19861   tree attribute_list = NULL_TREE;
19862   bool save_translate_strings_p = parser->translate_strings_p;
19863
19864   parser->translate_strings_p = false;
19865   while (true)
19866     {
19867       cp_token *token;
19868       tree identifier;
19869       tree attribute;
19870
19871       /* Look for the identifier.  We also allow keywords here; for
19872          example `__attribute__ ((const))' is legal.  */
19873       token = cp_lexer_peek_token (parser->lexer);
19874       if (token->type == CPP_NAME
19875           || token->type == CPP_KEYWORD)
19876         {
19877           tree arguments = NULL_TREE;
19878
19879           /* Consume the token.  */
19880           token = cp_lexer_consume_token (parser->lexer);
19881
19882           /* Save away the identifier that indicates which attribute
19883              this is.  */
19884           identifier = (token->type == CPP_KEYWORD) 
19885             /* For keywords, use the canonical spelling, not the
19886                parsed identifier.  */
19887             ? ridpointers[(int) token->keyword]
19888             : token->u.value;
19889           
19890           attribute = build_tree_list (identifier, NULL_TREE);
19891
19892           /* Peek at the next token.  */
19893           token = cp_lexer_peek_token (parser->lexer);
19894           /* If it's an `(', then parse the attribute arguments.  */
19895           if (token->type == CPP_OPEN_PAREN)
19896             {
19897               VEC(tree,gc) *vec;
19898               int attr_flag = (attribute_takes_identifier_p (identifier)
19899                                ? id_attr : normal_attr);
19900               vec = cp_parser_parenthesized_expression_list
19901                     (parser, attr_flag, /*cast_p=*/false,
19902                      /*allow_expansion_p=*/false,
19903                      /*non_constant_p=*/NULL);
19904               if (vec == NULL)
19905                 arguments = error_mark_node;
19906               else
19907                 {
19908                   arguments = build_tree_list_vec (vec);
19909                   release_tree_vector (vec);
19910                 }
19911               /* Save the arguments away.  */
19912               TREE_VALUE (attribute) = arguments;
19913             }
19914
19915           if (arguments != error_mark_node)
19916             {
19917               /* Add this attribute to the list.  */
19918               TREE_CHAIN (attribute) = attribute_list;
19919               attribute_list = attribute;
19920             }
19921
19922           token = cp_lexer_peek_token (parser->lexer);
19923         }
19924       /* Now, look for more attributes.  If the next token isn't a
19925          `,', we're done.  */
19926       if (token->type != CPP_COMMA)
19927         break;
19928
19929       /* Consume the comma and keep going.  */
19930       cp_lexer_consume_token (parser->lexer);
19931     }
19932   parser->translate_strings_p = save_translate_strings_p;
19933
19934   /* We built up the list in reverse order.  */
19935   return nreverse (attribute_list);
19936 }
19937
19938 /* Parse an optional `__extension__' keyword.  Returns TRUE if it is
19939    present, and FALSE otherwise.  *SAVED_PEDANTIC is set to the
19940    current value of the PEDANTIC flag, regardless of whether or not
19941    the `__extension__' keyword is present.  The caller is responsible
19942    for restoring the value of the PEDANTIC flag.  */
19943
19944 static bool
19945 cp_parser_extension_opt (cp_parser* parser, int* saved_pedantic)
19946 {
19947   /* Save the old value of the PEDANTIC flag.  */
19948   *saved_pedantic = pedantic;
19949
19950   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_EXTENSION))
19951     {
19952       /* Consume the `__extension__' token.  */
19953       cp_lexer_consume_token (parser->lexer);
19954       /* We're not being pedantic while the `__extension__' keyword is
19955          in effect.  */
19956       pedantic = 0;
19957
19958       return true;
19959     }
19960
19961   return false;
19962 }
19963
19964 /* Parse a label declaration.
19965
19966    label-declaration:
19967      __label__ label-declarator-seq ;
19968
19969    label-declarator-seq:
19970      identifier , label-declarator-seq
19971      identifier  */
19972
19973 static void
19974 cp_parser_label_declaration (cp_parser* parser)
19975 {
19976   /* Look for the `__label__' keyword.  */
19977   cp_parser_require_keyword (parser, RID_LABEL, RT_LABEL);
19978
19979   while (true)
19980     {
19981       tree identifier;
19982
19983       /* Look for an identifier.  */
19984       identifier = cp_parser_identifier (parser);
19985       /* If we failed, stop.  */
19986       if (identifier == error_mark_node)
19987         break;
19988       /* Declare it as a label.  */
19989       finish_label_decl (identifier);
19990       /* If the next token is a `;', stop.  */
19991       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
19992         break;
19993       /* Look for the `,' separating the label declarations.  */
19994       cp_parser_require (parser, CPP_COMMA, RT_COMMA);
19995     }
19996
19997   /* Look for the final `;'.  */
19998   cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
19999 }
20000
20001 /* Support Functions */
20002
20003 /* Looks up NAME in the current scope, as given by PARSER->SCOPE.
20004    NAME should have one of the representations used for an
20005    id-expression.  If NAME is the ERROR_MARK_NODE, the ERROR_MARK_NODE
20006    is returned.  If PARSER->SCOPE is a dependent type, then a
20007    SCOPE_REF is returned.
20008
20009    If NAME is a TEMPLATE_ID_EXPR, then it will be immediately
20010    returned; the name was already resolved when the TEMPLATE_ID_EXPR
20011    was formed.  Abstractly, such entities should not be passed to this
20012    function, because they do not need to be looked up, but it is
20013    simpler to check for this special case here, rather than at the
20014    call-sites.
20015
20016    In cases not explicitly covered above, this function returns a
20017    DECL, OVERLOAD, or baselink representing the result of the lookup.
20018    If there was no entity with the indicated NAME, the ERROR_MARK_NODE
20019    is returned.
20020
20021    If TAG_TYPE is not NONE_TYPE, it indicates an explicit type keyword
20022    (e.g., "struct") that was used.  In that case bindings that do not
20023    refer to types are ignored.
20024
20025    If IS_TEMPLATE is TRUE, bindings that do not refer to templates are
20026    ignored.
20027
20028    If IS_NAMESPACE is TRUE, bindings that do not refer to namespaces
20029    are ignored.
20030
20031    If CHECK_DEPENDENCY is TRUE, names are not looked up in dependent
20032    types.
20033
20034    If AMBIGUOUS_DECLS is non-NULL, *AMBIGUOUS_DECLS is set to a
20035    TREE_LIST of candidates if name-lookup results in an ambiguity, and
20036    NULL_TREE otherwise.  */
20037
20038 static tree
20039 cp_parser_lookup_name (cp_parser *parser, tree name,
20040                        enum tag_types tag_type,
20041                        bool is_template,
20042                        bool is_namespace,
20043                        bool check_dependency,
20044                        tree *ambiguous_decls,
20045                        location_t name_location)
20046 {
20047   int flags = 0;
20048   tree decl;
20049   tree object_type = parser->context->object_type;
20050
20051   if (!cp_parser_uncommitted_to_tentative_parse_p (parser))
20052     flags |= LOOKUP_COMPLAIN;
20053
20054   /* Assume that the lookup will be unambiguous.  */
20055   if (ambiguous_decls)
20056     *ambiguous_decls = NULL_TREE;
20057
20058   /* Now that we have looked up the name, the OBJECT_TYPE (if any) is
20059      no longer valid.  Note that if we are parsing tentatively, and
20060      the parse fails, OBJECT_TYPE will be automatically restored.  */
20061   parser->context->object_type = NULL_TREE;
20062
20063   if (name == error_mark_node)
20064     return error_mark_node;
20065
20066   /* A template-id has already been resolved; there is no lookup to
20067      do.  */
20068   if (TREE_CODE (name) == TEMPLATE_ID_EXPR)
20069     return name;
20070   if (BASELINK_P (name))
20071     {
20072       gcc_assert (TREE_CODE (BASELINK_FUNCTIONS (name))
20073                   == TEMPLATE_ID_EXPR);
20074       return name;
20075     }
20076
20077   /* A BIT_NOT_EXPR is used to represent a destructor.  By this point,
20078      it should already have been checked to make sure that the name
20079      used matches the type being destroyed.  */
20080   if (TREE_CODE (name) == BIT_NOT_EXPR)
20081     {
20082       tree type;
20083
20084       /* Figure out to which type this destructor applies.  */
20085       if (parser->scope)
20086         type = parser->scope;
20087       else if (object_type)
20088         type = object_type;
20089       else
20090         type = current_class_type;
20091       /* If that's not a class type, there is no destructor.  */
20092       if (!type || !CLASS_TYPE_P (type))
20093         return error_mark_node;
20094       if (CLASSTYPE_LAZY_DESTRUCTOR (type))
20095         lazily_declare_fn (sfk_destructor, type);
20096       if (!CLASSTYPE_DESTRUCTORS (type))
20097           return error_mark_node;
20098       /* If it was a class type, return the destructor.  */
20099       return CLASSTYPE_DESTRUCTORS (type);
20100     }
20101
20102   /* By this point, the NAME should be an ordinary identifier.  If
20103      the id-expression was a qualified name, the qualifying scope is
20104      stored in PARSER->SCOPE at this point.  */
20105   gcc_assert (TREE_CODE (name) == IDENTIFIER_NODE);
20106
20107   /* Perform the lookup.  */
20108   if (parser->scope)
20109     {
20110       bool dependent_p;
20111
20112       if (parser->scope == error_mark_node)
20113         return error_mark_node;
20114
20115       /* If the SCOPE is dependent, the lookup must be deferred until
20116          the template is instantiated -- unless we are explicitly
20117          looking up names in uninstantiated templates.  Even then, we
20118          cannot look up the name if the scope is not a class type; it
20119          might, for example, be a template type parameter.  */
20120       dependent_p = (TYPE_P (parser->scope)
20121                      && dependent_scope_p (parser->scope));
20122       if ((check_dependency || !CLASS_TYPE_P (parser->scope))
20123           && dependent_p)
20124         /* Defer lookup.  */
20125         decl = error_mark_node;
20126       else
20127         {
20128           tree pushed_scope = NULL_TREE;
20129
20130           /* If PARSER->SCOPE is a dependent type, then it must be a
20131              class type, and we must not be checking dependencies;
20132              otherwise, we would have processed this lookup above.  So
20133              that PARSER->SCOPE is not considered a dependent base by
20134              lookup_member, we must enter the scope here.  */
20135           if (dependent_p)
20136             pushed_scope = push_scope (parser->scope);
20137
20138           /* If the PARSER->SCOPE is a template specialization, it
20139              may be instantiated during name lookup.  In that case,
20140              errors may be issued.  Even if we rollback the current
20141              tentative parse, those errors are valid.  */
20142           decl = lookup_qualified_name (parser->scope, name,
20143                                         tag_type != none_type,
20144                                         /*complain=*/true);
20145
20146           /* 3.4.3.1: In a lookup in which the constructor is an acceptable
20147              lookup result and the nested-name-specifier nominates a class C:
20148                * if the name specified after the nested-name-specifier, when
20149                looked up in C, is the injected-class-name of C (Clause 9), or
20150                * if the name specified after the nested-name-specifier is the
20151                same as the identifier or the simple-template-id's template-
20152                name in the last component of the nested-name-specifier,
20153              the name is instead considered to name the constructor of
20154              class C. [ Note: for example, the constructor is not an
20155              acceptable lookup result in an elaborated-type-specifier so
20156              the constructor would not be used in place of the
20157              injected-class-name. --end note ] Such a constructor name
20158              shall be used only in the declarator-id of a declaration that
20159              names a constructor or in a using-declaration.  */
20160           if (tag_type == none_type
20161               && DECL_SELF_REFERENCE_P (decl)
20162               && same_type_p (DECL_CONTEXT (decl), parser->scope))
20163             decl = lookup_qualified_name (parser->scope, ctor_identifier,
20164                                           tag_type != none_type,
20165                                           /*complain=*/true);
20166
20167           /* If we have a single function from a using decl, pull it out.  */
20168           if (TREE_CODE (decl) == OVERLOAD
20169               && !really_overloaded_fn (decl))
20170             decl = OVL_FUNCTION (decl);
20171
20172           if (pushed_scope)
20173             pop_scope (pushed_scope);
20174         }
20175
20176       /* If the scope is a dependent type and either we deferred lookup or
20177          we did lookup but didn't find the name, rememeber the name.  */
20178       if (decl == error_mark_node && TYPE_P (parser->scope)
20179           && dependent_type_p (parser->scope))
20180         {
20181           if (tag_type)
20182             {
20183               tree type;
20184
20185               /* The resolution to Core Issue 180 says that `struct
20186                  A::B' should be considered a type-name, even if `A'
20187                  is dependent.  */
20188               type = make_typename_type (parser->scope, name, tag_type,
20189                                          /*complain=*/tf_error);
20190               decl = TYPE_NAME (type);
20191             }
20192           else if (is_template
20193                    && (cp_parser_next_token_ends_template_argument_p (parser)
20194                        || cp_lexer_next_token_is (parser->lexer,
20195                                                   CPP_CLOSE_PAREN)))
20196             decl = make_unbound_class_template (parser->scope,
20197                                                 name, NULL_TREE,
20198                                                 /*complain=*/tf_error);
20199           else
20200             decl = build_qualified_name (/*type=*/NULL_TREE,
20201                                          parser->scope, name,
20202                                          is_template);
20203         }
20204       parser->qualifying_scope = parser->scope;
20205       parser->object_scope = NULL_TREE;
20206     }
20207   else if (object_type)
20208     {
20209       tree object_decl = NULL_TREE;
20210       /* Look up the name in the scope of the OBJECT_TYPE, unless the
20211          OBJECT_TYPE is not a class.  */
20212       if (CLASS_TYPE_P (object_type))
20213         /* If the OBJECT_TYPE is a template specialization, it may
20214            be instantiated during name lookup.  In that case, errors
20215            may be issued.  Even if we rollback the current tentative
20216            parse, those errors are valid.  */
20217         object_decl = lookup_member (object_type,
20218                                      name,
20219                                      /*protect=*/0,
20220                                      tag_type != none_type);
20221       /* Look it up in the enclosing context, too.  */
20222       decl = lookup_name_real (name, tag_type != none_type,
20223                                /*nonclass=*/0,
20224                                /*block_p=*/true, is_namespace, flags);
20225       parser->object_scope = object_type;
20226       parser->qualifying_scope = NULL_TREE;
20227       if (object_decl)
20228         decl = object_decl;
20229     }
20230   else
20231     {
20232       decl = lookup_name_real (name, tag_type != none_type,
20233                                /*nonclass=*/0,
20234                                /*block_p=*/true, is_namespace, flags);
20235       parser->qualifying_scope = NULL_TREE;
20236       parser->object_scope = NULL_TREE;
20237     }
20238
20239   /* If the lookup failed, let our caller know.  */
20240   if (!decl || decl == error_mark_node)
20241     return error_mark_node;
20242
20243   /* Pull out the template from an injected-class-name (or multiple).  */
20244   if (is_template)
20245     decl = maybe_get_template_decl_from_type_decl (decl);
20246
20247   /* If it's a TREE_LIST, the result of the lookup was ambiguous.  */
20248   if (TREE_CODE (decl) == TREE_LIST)
20249     {
20250       if (ambiguous_decls)
20251         *ambiguous_decls = decl;
20252       /* The error message we have to print is too complicated for
20253          cp_parser_error, so we incorporate its actions directly.  */
20254       if (!cp_parser_simulate_error (parser))
20255         {
20256           error_at (name_location, "reference to %qD is ambiguous",
20257                     name);
20258           print_candidates (decl);
20259         }
20260       return error_mark_node;
20261     }
20262
20263   gcc_assert (DECL_P (decl)
20264               || TREE_CODE (decl) == OVERLOAD
20265               || TREE_CODE (decl) == SCOPE_REF
20266               || TREE_CODE (decl) == UNBOUND_CLASS_TEMPLATE
20267               || BASELINK_P (decl));
20268
20269   /* If we have resolved the name of a member declaration, check to
20270      see if the declaration is accessible.  When the name resolves to
20271      set of overloaded functions, accessibility is checked when
20272      overload resolution is done.
20273
20274      During an explicit instantiation, access is not checked at all,
20275      as per [temp.explicit].  */
20276   if (DECL_P (decl))
20277     check_accessibility_of_qualified_id (decl, object_type, parser->scope);
20278
20279   maybe_record_typedef_use (decl);
20280
20281   return decl;
20282 }
20283
20284 /* Like cp_parser_lookup_name, but for use in the typical case where
20285    CHECK_ACCESS is TRUE, IS_TYPE is FALSE, IS_TEMPLATE is FALSE,
20286    IS_NAMESPACE is FALSE, and CHECK_DEPENDENCY is TRUE.  */
20287
20288 static tree
20289 cp_parser_lookup_name_simple (cp_parser* parser, tree name, location_t location)
20290 {
20291   return cp_parser_lookup_name (parser, name,
20292                                 none_type,
20293                                 /*is_template=*/false,
20294                                 /*is_namespace=*/false,
20295                                 /*check_dependency=*/true,
20296                                 /*ambiguous_decls=*/NULL,
20297                                 location);
20298 }
20299
20300 /* If DECL is a TEMPLATE_DECL that can be treated like a TYPE_DECL in
20301    the current context, return the TYPE_DECL.  If TAG_NAME_P is
20302    true, the DECL indicates the class being defined in a class-head,
20303    or declared in an elaborated-type-specifier.
20304
20305    Otherwise, return DECL.  */
20306
20307 static tree
20308 cp_parser_maybe_treat_template_as_class (tree decl, bool tag_name_p)
20309 {
20310   /* If the TEMPLATE_DECL is being declared as part of a class-head,
20311      the translation from TEMPLATE_DECL to TYPE_DECL occurs:
20312
20313        struct A {
20314          template <typename T> struct B;
20315        };
20316
20317        template <typename T> struct A::B {};
20318
20319      Similarly, in an elaborated-type-specifier:
20320
20321        namespace N { struct X{}; }
20322
20323        struct A {
20324          template <typename T> friend struct N::X;
20325        };
20326
20327      However, if the DECL refers to a class type, and we are in
20328      the scope of the class, then the name lookup automatically
20329      finds the TYPE_DECL created by build_self_reference rather
20330      than a TEMPLATE_DECL.  For example, in:
20331
20332        template <class T> struct S {
20333          S s;
20334        };
20335
20336      there is no need to handle such case.  */
20337
20338   if (DECL_CLASS_TEMPLATE_P (decl) && tag_name_p)
20339     return DECL_TEMPLATE_RESULT (decl);
20340
20341   return decl;
20342 }
20343
20344 /* If too many, or too few, template-parameter lists apply to the
20345    declarator, issue an error message.  Returns TRUE if all went well,
20346    and FALSE otherwise.  */
20347
20348 static bool
20349 cp_parser_check_declarator_template_parameters (cp_parser* parser,
20350                                                 cp_declarator *declarator,
20351                                                 location_t declarator_location)
20352 {
20353   unsigned num_templates;
20354
20355   /* We haven't seen any classes that involve template parameters yet.  */
20356   num_templates = 0;
20357
20358   switch (declarator->kind)
20359     {
20360     case cdk_id:
20361       if (declarator->u.id.qualifying_scope)
20362         {
20363           tree scope;
20364
20365           scope = declarator->u.id.qualifying_scope;
20366
20367           while (scope && CLASS_TYPE_P (scope))
20368             {
20369               /* You're supposed to have one `template <...>'
20370                  for every template class, but you don't need one
20371                  for a full specialization.  For example:
20372
20373                  template <class T> struct S{};
20374                  template <> struct S<int> { void f(); };
20375                  void S<int>::f () {}
20376
20377                  is correct; there shouldn't be a `template <>' for
20378                  the definition of `S<int>::f'.  */
20379               if (!CLASSTYPE_TEMPLATE_INFO (scope))
20380                 /* If SCOPE does not have template information of any
20381                    kind, then it is not a template, nor is it nested
20382                    within a template.  */
20383                 break;
20384               if (explicit_class_specialization_p (scope))
20385                 break;
20386               if (PRIMARY_TEMPLATE_P (CLASSTYPE_TI_TEMPLATE (scope)))
20387                 ++num_templates;
20388
20389               scope = TYPE_CONTEXT (scope);
20390             }
20391         }
20392       else if (TREE_CODE (declarator->u.id.unqualified_name)
20393                == TEMPLATE_ID_EXPR)
20394         /* If the DECLARATOR has the form `X<y>' then it uses one
20395            additional level of template parameters.  */
20396         ++num_templates;
20397
20398       return cp_parser_check_template_parameters 
20399         (parser, num_templates, declarator_location, declarator);
20400
20401
20402     case cdk_function:
20403     case cdk_array:
20404     case cdk_pointer:
20405     case cdk_reference:
20406     case cdk_ptrmem:
20407       return (cp_parser_check_declarator_template_parameters
20408               (parser, declarator->declarator, declarator_location));
20409
20410     case cdk_error:
20411       return true;
20412
20413     default:
20414       gcc_unreachable ();
20415     }
20416   return false;
20417 }
20418
20419 /* NUM_TEMPLATES were used in the current declaration.  If that is
20420    invalid, return FALSE and issue an error messages.  Otherwise,
20421    return TRUE.  If DECLARATOR is non-NULL, then we are checking a
20422    declarator and we can print more accurate diagnostics.  */
20423
20424 static bool
20425 cp_parser_check_template_parameters (cp_parser* parser,
20426                                      unsigned num_templates,
20427                                      location_t location,
20428                                      cp_declarator *declarator)
20429 {
20430   /* If there are the same number of template classes and parameter
20431      lists, that's OK.  */
20432   if (parser->num_template_parameter_lists == num_templates)
20433     return true;
20434   /* If there are more, but only one more, then we are referring to a
20435      member template.  That's OK too.  */
20436   if (parser->num_template_parameter_lists == num_templates + 1)
20437     return true;
20438   /* If there are more template classes than parameter lists, we have
20439      something like:
20440
20441        template <class T> void S<T>::R<T>::f ();  */
20442   if (parser->num_template_parameter_lists < num_templates)
20443     {
20444       if (declarator && !current_function_decl)
20445         error_at (location, "specializing member %<%T::%E%> "
20446                   "requires %<template<>%> syntax", 
20447                   declarator->u.id.qualifying_scope,
20448                   declarator->u.id.unqualified_name);
20449       else if (declarator)
20450         error_at (location, "invalid declaration of %<%T::%E%>",
20451                   declarator->u.id.qualifying_scope,
20452                   declarator->u.id.unqualified_name);
20453       else 
20454         error_at (location, "too few template-parameter-lists");
20455       return false;
20456     }
20457   /* Otherwise, there are too many template parameter lists.  We have
20458      something like:
20459
20460      template <class T> template <class U> void S::f();  */
20461   error_at (location, "too many template-parameter-lists");
20462   return false;
20463 }
20464
20465 /* Parse an optional `::' token indicating that the following name is
20466    from the global namespace.  If so, PARSER->SCOPE is set to the
20467    GLOBAL_NAMESPACE. Otherwise, PARSER->SCOPE is set to NULL_TREE,
20468    unless CURRENT_SCOPE_VALID_P is TRUE, in which case it is left alone.
20469    Returns the new value of PARSER->SCOPE, if the `::' token is
20470    present, and NULL_TREE otherwise.  */
20471
20472 static tree
20473 cp_parser_global_scope_opt (cp_parser* parser, bool current_scope_valid_p)
20474 {
20475   cp_token *token;
20476
20477   /* Peek at the next token.  */
20478   token = cp_lexer_peek_token (parser->lexer);
20479   /* If we're looking at a `::' token then we're starting from the
20480      global namespace, not our current location.  */
20481   if (token->type == CPP_SCOPE)
20482     {
20483       /* Consume the `::' token.  */
20484       cp_lexer_consume_token (parser->lexer);
20485       /* Set the SCOPE so that we know where to start the lookup.  */
20486       parser->scope = global_namespace;
20487       parser->qualifying_scope = global_namespace;
20488       parser->object_scope = NULL_TREE;
20489
20490       return parser->scope;
20491     }
20492   else if (!current_scope_valid_p)
20493     {
20494       parser->scope = NULL_TREE;
20495       parser->qualifying_scope = NULL_TREE;
20496       parser->object_scope = NULL_TREE;
20497     }
20498
20499   return NULL_TREE;
20500 }
20501
20502 /* Returns TRUE if the upcoming token sequence is the start of a
20503    constructor declarator.  If FRIEND_P is true, the declarator is
20504    preceded by the `friend' specifier.  */
20505
20506 static bool
20507 cp_parser_constructor_declarator_p (cp_parser *parser, bool friend_p)
20508 {
20509   bool constructor_p;
20510   tree nested_name_specifier;
20511   cp_token *next_token;
20512
20513   /* The common case is that this is not a constructor declarator, so
20514      try to avoid doing lots of work if at all possible.  It's not
20515      valid declare a constructor at function scope.  */
20516   if (parser->in_function_body)
20517     return false;
20518   /* And only certain tokens can begin a constructor declarator.  */
20519   next_token = cp_lexer_peek_token (parser->lexer);
20520   if (next_token->type != CPP_NAME
20521       && next_token->type != CPP_SCOPE
20522       && next_token->type != CPP_NESTED_NAME_SPECIFIER
20523       && next_token->type != CPP_TEMPLATE_ID)
20524     return false;
20525
20526   /* Parse tentatively; we are going to roll back all of the tokens
20527      consumed here.  */
20528   cp_parser_parse_tentatively (parser);
20529   /* Assume that we are looking at a constructor declarator.  */
20530   constructor_p = true;
20531
20532   /* Look for the optional `::' operator.  */
20533   cp_parser_global_scope_opt (parser,
20534                               /*current_scope_valid_p=*/false);
20535   /* Look for the nested-name-specifier.  */
20536   nested_name_specifier
20537     = (cp_parser_nested_name_specifier_opt (parser,
20538                                             /*typename_keyword_p=*/false,
20539                                             /*check_dependency_p=*/false,
20540                                             /*type_p=*/false,
20541                                             /*is_declaration=*/false));
20542   /* Outside of a class-specifier, there must be a
20543      nested-name-specifier.  */
20544   if (!nested_name_specifier &&
20545       (!at_class_scope_p () || !TYPE_BEING_DEFINED (current_class_type)
20546        || friend_p))
20547     constructor_p = false;
20548   else if (nested_name_specifier == error_mark_node)
20549     constructor_p = false;
20550
20551   /* If we have a class scope, this is easy; DR 147 says that S::S always
20552      names the constructor, and no other qualified name could.  */
20553   if (constructor_p && nested_name_specifier
20554       && CLASS_TYPE_P (nested_name_specifier))
20555     {
20556       tree id = cp_parser_unqualified_id (parser,
20557                                           /*template_keyword_p=*/false,
20558                                           /*check_dependency_p=*/false,
20559                                           /*declarator_p=*/true,
20560                                           /*optional_p=*/false);
20561       if (is_overloaded_fn (id))
20562         id = DECL_NAME (get_first_fn (id));
20563       if (!constructor_name_p (id, nested_name_specifier))
20564         constructor_p = false;
20565     }
20566   /* If we still think that this might be a constructor-declarator,
20567      look for a class-name.  */
20568   else if (constructor_p)
20569     {
20570       /* If we have:
20571
20572            template <typename T> struct S {
20573              S();
20574            };
20575
20576          we must recognize that the nested `S' names a class.  */
20577       tree type_decl;
20578       type_decl = cp_parser_class_name (parser,
20579                                         /*typename_keyword_p=*/false,
20580                                         /*template_keyword_p=*/false,
20581                                         none_type,
20582                                         /*check_dependency_p=*/false,
20583                                         /*class_head_p=*/false,
20584                                         /*is_declaration=*/false);
20585       /* If there was no class-name, then this is not a constructor.  */
20586       constructor_p = !cp_parser_error_occurred (parser);
20587
20588       /* If we're still considering a constructor, we have to see a `(',
20589          to begin the parameter-declaration-clause, followed by either a
20590          `)', an `...', or a decl-specifier.  We need to check for a
20591          type-specifier to avoid being fooled into thinking that:
20592
20593            S (f) (int);
20594
20595          is a constructor.  (It is actually a function named `f' that
20596          takes one parameter (of type `int') and returns a value of type
20597          `S'.  */
20598       if (constructor_p
20599           && !cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
20600         constructor_p = false;
20601
20602       if (constructor_p
20603           && cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN)
20604           && cp_lexer_next_token_is_not (parser->lexer, CPP_ELLIPSIS)
20605           /* A parameter declaration begins with a decl-specifier,
20606              which is either the "attribute" keyword, a storage class
20607              specifier, or (usually) a type-specifier.  */
20608           && !cp_lexer_next_token_is_decl_specifier_keyword (parser->lexer))
20609         {
20610           tree type;
20611           tree pushed_scope = NULL_TREE;
20612           unsigned saved_num_template_parameter_lists;
20613
20614           /* Names appearing in the type-specifier should be looked up
20615              in the scope of the class.  */
20616           if (current_class_type)
20617             type = NULL_TREE;
20618           else
20619             {
20620               type = TREE_TYPE (type_decl);
20621               if (TREE_CODE (type) == TYPENAME_TYPE)
20622                 {
20623                   type = resolve_typename_type (type,
20624                                                 /*only_current_p=*/false);
20625                   if (TREE_CODE (type) == TYPENAME_TYPE)
20626                     {
20627                       cp_parser_abort_tentative_parse (parser);
20628                       return false;
20629                     }
20630                 }
20631               pushed_scope = push_scope (type);
20632             }
20633
20634           /* Inside the constructor parameter list, surrounding
20635              template-parameter-lists do not apply.  */
20636           saved_num_template_parameter_lists
20637             = parser->num_template_parameter_lists;
20638           parser->num_template_parameter_lists = 0;
20639
20640           /* Look for the type-specifier.  */
20641           cp_parser_type_specifier (parser,
20642                                     CP_PARSER_FLAGS_NONE,
20643                                     /*decl_specs=*/NULL,
20644                                     /*is_declarator=*/true,
20645                                     /*declares_class_or_enum=*/NULL,
20646                                     /*is_cv_qualifier=*/NULL);
20647
20648           parser->num_template_parameter_lists
20649             = saved_num_template_parameter_lists;
20650
20651           /* Leave the scope of the class.  */
20652           if (pushed_scope)
20653             pop_scope (pushed_scope);
20654
20655           constructor_p = !cp_parser_error_occurred (parser);
20656         }
20657     }
20658
20659   /* We did not really want to consume any tokens.  */
20660   cp_parser_abort_tentative_parse (parser);
20661
20662   return constructor_p;
20663 }
20664
20665 /* Parse the definition of the function given by the DECL_SPECIFIERS,
20666    ATTRIBUTES, and DECLARATOR.  The access checks have been deferred;
20667    they must be performed once we are in the scope of the function.
20668
20669    Returns the function defined.  */
20670
20671 static tree
20672 cp_parser_function_definition_from_specifiers_and_declarator
20673   (cp_parser* parser,
20674    cp_decl_specifier_seq *decl_specifiers,
20675    tree attributes,
20676    const cp_declarator *declarator)
20677 {
20678   tree fn;
20679   bool success_p;
20680
20681   /* Begin the function-definition.  */
20682   success_p = start_function (decl_specifiers, declarator, attributes);
20683
20684   /* The things we're about to see are not directly qualified by any
20685      template headers we've seen thus far.  */
20686   reset_specialization ();
20687
20688   /* If there were names looked up in the decl-specifier-seq that we
20689      did not check, check them now.  We must wait until we are in the
20690      scope of the function to perform the checks, since the function
20691      might be a friend.  */
20692   perform_deferred_access_checks ();
20693
20694   if (!success_p)
20695     {
20696       /* Skip the entire function.  */
20697       cp_parser_skip_to_end_of_block_or_statement (parser);
20698       fn = error_mark_node;
20699     }
20700   else if (DECL_INITIAL (current_function_decl) != error_mark_node)
20701     {
20702       /* Seen already, skip it.  An error message has already been output.  */
20703       cp_parser_skip_to_end_of_block_or_statement (parser);
20704       fn = current_function_decl;
20705       current_function_decl = NULL_TREE;
20706       /* If this is a function from a class, pop the nested class.  */
20707       if (current_class_name)
20708         pop_nested_class ();
20709     }
20710   else
20711     {
20712       timevar_id_t tv;
20713       if (DECL_DECLARED_INLINE_P (current_function_decl))
20714         tv = TV_PARSE_INLINE;
20715       else
20716         tv = TV_PARSE_FUNC;
20717       timevar_push (tv);
20718       fn = cp_parser_function_definition_after_declarator (parser,
20719                                                          /*inline_p=*/false);
20720       timevar_pop (tv);
20721     }
20722
20723   return fn;
20724 }
20725
20726 /* Parse the part of a function-definition that follows the
20727    declarator.  INLINE_P is TRUE iff this function is an inline
20728    function defined within a class-specifier.
20729
20730    Returns the function defined.  */
20731
20732 static tree
20733 cp_parser_function_definition_after_declarator (cp_parser* parser,
20734                                                 bool inline_p)
20735 {
20736   tree fn;
20737   bool ctor_initializer_p = false;
20738   bool saved_in_unbraced_linkage_specification_p;
20739   bool saved_in_function_body;
20740   unsigned saved_num_template_parameter_lists;
20741   cp_token *token;
20742
20743   saved_in_function_body = parser->in_function_body;
20744   parser->in_function_body = true;
20745   /* If the next token is `return', then the code may be trying to
20746      make use of the "named return value" extension that G++ used to
20747      support.  */
20748   token = cp_lexer_peek_token (parser->lexer);
20749   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_RETURN))
20750     {
20751       /* Consume the `return' keyword.  */
20752       cp_lexer_consume_token (parser->lexer);
20753       /* Look for the identifier that indicates what value is to be
20754          returned.  */
20755       cp_parser_identifier (parser);
20756       /* Issue an error message.  */
20757       error_at (token->location,
20758                 "named return values are no longer supported");
20759       /* Skip tokens until we reach the start of the function body.  */
20760       while (true)
20761         {
20762           cp_token *token = cp_lexer_peek_token (parser->lexer);
20763           if (token->type == CPP_OPEN_BRACE
20764               || token->type == CPP_EOF
20765               || token->type == CPP_PRAGMA_EOL)
20766             break;
20767           cp_lexer_consume_token (parser->lexer);
20768         }
20769     }
20770   /* The `extern' in `extern "C" void f () { ... }' does not apply to
20771      anything declared inside `f'.  */
20772   saved_in_unbraced_linkage_specification_p
20773     = parser->in_unbraced_linkage_specification_p;
20774   parser->in_unbraced_linkage_specification_p = false;
20775   /* Inside the function, surrounding template-parameter-lists do not
20776      apply.  */
20777   saved_num_template_parameter_lists
20778     = parser->num_template_parameter_lists;
20779   parser->num_template_parameter_lists = 0;
20780
20781   start_lambda_scope (current_function_decl);
20782
20783   /* If the next token is `try', then we are looking at a
20784      function-try-block.  */
20785   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TRY))
20786     ctor_initializer_p = cp_parser_function_try_block (parser);
20787   /* A function-try-block includes the function-body, so we only do
20788      this next part if we're not processing a function-try-block.  */
20789   else
20790     ctor_initializer_p
20791       = cp_parser_ctor_initializer_opt_and_function_body (parser);
20792
20793   finish_lambda_scope ();
20794
20795   /* Finish the function.  */
20796   fn = finish_function ((ctor_initializer_p ? 1 : 0) |
20797                         (inline_p ? 2 : 0));
20798   /* Generate code for it, if necessary.  */
20799   expand_or_defer_fn (fn);
20800   /* Restore the saved values.  */
20801   parser->in_unbraced_linkage_specification_p
20802     = saved_in_unbraced_linkage_specification_p;
20803   parser->num_template_parameter_lists
20804     = saved_num_template_parameter_lists;
20805   parser->in_function_body = saved_in_function_body;
20806
20807   return fn;
20808 }
20809
20810 /* Parse a template-declaration, assuming that the `export' (and
20811    `extern') keywords, if present, has already been scanned.  MEMBER_P
20812    is as for cp_parser_template_declaration.  */
20813
20814 static void
20815 cp_parser_template_declaration_after_export (cp_parser* parser, bool member_p)
20816 {
20817   tree decl = NULL_TREE;
20818   VEC (deferred_access_check,gc) *checks;
20819   tree parameter_list;
20820   bool friend_p = false;
20821   bool need_lang_pop;
20822   cp_token *token;
20823
20824   /* Look for the `template' keyword.  */
20825   token = cp_lexer_peek_token (parser->lexer);
20826   if (!cp_parser_require_keyword (parser, RID_TEMPLATE, RT_TEMPLATE))
20827     return;
20828
20829   /* And the `<'.  */
20830   if (!cp_parser_require (parser, CPP_LESS, RT_LESS))
20831     return;
20832   if (at_class_scope_p () && current_function_decl)
20833     {
20834       /* 14.5.2.2 [temp.mem]
20835
20836          A local class shall not have member templates.  */
20837       error_at (token->location,
20838                 "invalid declaration of member template in local class");
20839       cp_parser_skip_to_end_of_block_or_statement (parser);
20840       return;
20841     }
20842   /* [temp]
20843
20844      A template ... shall not have C linkage.  */
20845   if (current_lang_name == lang_name_c)
20846     {
20847       error_at (token->location, "template with C linkage");
20848       /* Give it C++ linkage to avoid confusing other parts of the
20849          front end.  */
20850       push_lang_context (lang_name_cplusplus);
20851       need_lang_pop = true;
20852     }
20853   else
20854     need_lang_pop = false;
20855
20856   /* We cannot perform access checks on the template parameter
20857      declarations until we know what is being declared, just as we
20858      cannot check the decl-specifier list.  */
20859   push_deferring_access_checks (dk_deferred);
20860
20861   /* If the next token is `>', then we have an invalid
20862      specialization.  Rather than complain about an invalid template
20863      parameter, issue an error message here.  */
20864   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER))
20865     {
20866       cp_parser_error (parser, "invalid explicit specialization");
20867       begin_specialization ();
20868       parameter_list = NULL_TREE;
20869     }
20870   else
20871     {
20872       /* Parse the template parameters.  */
20873       parameter_list = cp_parser_template_parameter_list (parser);
20874       fixup_template_parms ();
20875     }
20876
20877   /* Get the deferred access checks from the parameter list.  These
20878      will be checked once we know what is being declared, as for a
20879      member template the checks must be performed in the scope of the
20880      class containing the member.  */
20881   checks = get_deferred_access_checks ();
20882
20883   /* Look for the `>'.  */
20884   cp_parser_skip_to_end_of_template_parameter_list (parser);
20885   /* We just processed one more parameter list.  */
20886   ++parser->num_template_parameter_lists;
20887   /* If the next token is `template', there are more template
20888      parameters.  */
20889   if (cp_lexer_next_token_is_keyword (parser->lexer,
20890                                       RID_TEMPLATE))
20891     cp_parser_template_declaration_after_export (parser, member_p);
20892   else
20893     {
20894       /* There are no access checks when parsing a template, as we do not
20895          know if a specialization will be a friend.  */
20896       push_deferring_access_checks (dk_no_check);
20897       token = cp_lexer_peek_token (parser->lexer);
20898       decl = cp_parser_single_declaration (parser,
20899                                            checks,
20900                                            member_p,
20901                                            /*explicit_specialization_p=*/false,
20902                                            &friend_p);
20903       pop_deferring_access_checks ();
20904
20905       /* If this is a member template declaration, let the front
20906          end know.  */
20907       if (member_p && !friend_p && decl)
20908         {
20909           if (TREE_CODE (decl) == TYPE_DECL)
20910             cp_parser_check_access_in_redeclaration (decl, token->location);
20911
20912           decl = finish_member_template_decl (decl);
20913         }
20914       else if (friend_p && decl && TREE_CODE (decl) == TYPE_DECL)
20915         make_friend_class (current_class_type, TREE_TYPE (decl),
20916                            /*complain=*/true);
20917     }
20918   /* We are done with the current parameter list.  */
20919   --parser->num_template_parameter_lists;
20920
20921   pop_deferring_access_checks ();
20922
20923   /* Finish up.  */
20924   finish_template_decl (parameter_list);
20925
20926   /* Check the template arguments for a literal operator template.  */
20927   if (decl
20928       && (TREE_CODE (decl) == FUNCTION_DECL || DECL_FUNCTION_TEMPLATE_P (decl))
20929       && UDLIT_OPER_P (DECL_NAME (decl)))
20930     {
20931       bool ok = true;
20932       if (parameter_list == NULL_TREE)
20933         ok = false;
20934       else
20935         {
20936           int num_parms = TREE_VEC_LENGTH (parameter_list);
20937           if (num_parms != 1)
20938             ok = false;
20939           else
20940             {
20941               tree parm_list = TREE_VEC_ELT (parameter_list, 0);
20942               tree parm = INNERMOST_TEMPLATE_PARMS (parm_list);
20943               if (TREE_TYPE (parm) != char_type_node
20944                   || !TEMPLATE_PARM_PARAMETER_PACK (DECL_INITIAL (parm)))
20945                 ok = false;
20946             }
20947         }
20948       if (!ok)
20949         error ("literal operator template %qD has invalid parameter list."
20950                "  Expected non-type template argument pack <char...>",
20951                decl);
20952     }
20953   /* Register member declarations.  */
20954   if (member_p && !friend_p && decl && !DECL_CLASS_TEMPLATE_P (decl))
20955     finish_member_declaration (decl);
20956   /* For the erroneous case of a template with C linkage, we pushed an
20957      implicit C++ linkage scope; exit that scope now.  */
20958   if (need_lang_pop)
20959     pop_lang_context ();
20960   /* If DECL is a function template, we must return to parse it later.
20961      (Even though there is no definition, there might be default
20962      arguments that need handling.)  */
20963   if (member_p && decl
20964       && (TREE_CODE (decl) == FUNCTION_DECL
20965           || DECL_FUNCTION_TEMPLATE_P (decl)))
20966     VEC_safe_push (tree, gc, unparsed_funs_with_definitions, decl);
20967 }
20968
20969 /* Perform the deferred access checks from a template-parameter-list.
20970    CHECKS is a TREE_LIST of access checks, as returned by
20971    get_deferred_access_checks.  */
20972
20973 static void
20974 cp_parser_perform_template_parameter_access_checks (VEC (deferred_access_check,gc)* checks)
20975 {
20976   ++processing_template_parmlist;
20977   perform_access_checks (checks);
20978   --processing_template_parmlist;
20979 }
20980
20981 /* Parse a `decl-specifier-seq [opt] init-declarator [opt] ;' or
20982    `function-definition' sequence.  MEMBER_P is true, this declaration
20983    appears in a class scope.
20984
20985    Returns the DECL for the declared entity.  If FRIEND_P is non-NULL,
20986    *FRIEND_P is set to TRUE iff the declaration is a friend.  */
20987
20988 static tree
20989 cp_parser_single_declaration (cp_parser* parser,
20990                               VEC (deferred_access_check,gc)* checks,
20991                               bool member_p,
20992                               bool explicit_specialization_p,
20993                               bool* friend_p)
20994 {
20995   int declares_class_or_enum;
20996   tree decl = NULL_TREE;
20997   cp_decl_specifier_seq decl_specifiers;
20998   bool function_definition_p = false;
20999   cp_token *decl_spec_token_start;
21000
21001   /* This function is only used when processing a template
21002      declaration.  */
21003   gcc_assert (innermost_scope_kind () == sk_template_parms
21004               || innermost_scope_kind () == sk_template_spec);
21005
21006   /* Defer access checks until we know what is being declared.  */
21007   push_deferring_access_checks (dk_deferred);
21008
21009   /* Try the `decl-specifier-seq [opt] init-declarator [opt]'
21010      alternative.  */
21011   decl_spec_token_start = cp_lexer_peek_token (parser->lexer);
21012   cp_parser_decl_specifier_seq (parser,
21013                                 CP_PARSER_FLAGS_OPTIONAL,
21014                                 &decl_specifiers,
21015                                 &declares_class_or_enum);
21016   if (friend_p)
21017     *friend_p = cp_parser_friend_p (&decl_specifiers);
21018
21019   /* There are no template typedefs.  */
21020   if (decl_specifiers.specs[(int) ds_typedef])
21021     {
21022       error_at (decl_spec_token_start->location,
21023                 "template declaration of %<typedef%>");
21024       decl = error_mark_node;
21025     }
21026
21027   /* Gather up the access checks that occurred the
21028      decl-specifier-seq.  */
21029   stop_deferring_access_checks ();
21030
21031   /* Check for the declaration of a template class.  */
21032   if (declares_class_or_enum)
21033     {
21034       if (cp_parser_declares_only_class_p (parser))
21035         {
21036           decl = shadow_tag (&decl_specifiers);
21037
21038           /* In this case:
21039
21040                struct C {
21041                  friend template <typename T> struct A<T>::B;
21042                };
21043
21044              A<T>::B will be represented by a TYPENAME_TYPE, and
21045              therefore not recognized by shadow_tag.  */
21046           if (friend_p && *friend_p
21047               && !decl
21048               && decl_specifiers.type
21049               && TYPE_P (decl_specifiers.type))
21050             decl = decl_specifiers.type;
21051
21052           if (decl && decl != error_mark_node)
21053             decl = TYPE_NAME (decl);
21054           else
21055             decl = error_mark_node;
21056
21057           /* Perform access checks for template parameters.  */
21058           cp_parser_perform_template_parameter_access_checks (checks);
21059         }
21060     }
21061
21062   /* Complain about missing 'typename' or other invalid type names.  */
21063   if (!decl_specifiers.any_type_specifiers_p
21064       && cp_parser_parse_and_diagnose_invalid_type_name (parser))
21065     {
21066       /* cp_parser_parse_and_diagnose_invalid_type_name calls
21067          cp_parser_skip_to_end_of_block_or_statement, so don't try to parse
21068          the rest of this declaration.  */
21069       decl = error_mark_node;
21070       goto out;
21071     }
21072
21073   /* If it's not a template class, try for a template function.  If
21074      the next token is a `;', then this declaration does not declare
21075      anything.  But, if there were errors in the decl-specifiers, then
21076      the error might well have come from an attempted class-specifier.
21077      In that case, there's no need to warn about a missing declarator.  */
21078   if (!decl
21079       && (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON)
21080           || decl_specifiers.type != error_mark_node))
21081     {
21082       decl = cp_parser_init_declarator (parser,
21083                                         &decl_specifiers,
21084                                         checks,
21085                                         /*function_definition_allowed_p=*/true,
21086                                         member_p,
21087                                         declares_class_or_enum,
21088                                         &function_definition_p,
21089                                         NULL);
21090
21091     /* 7.1.1-1 [dcl.stc]
21092
21093        A storage-class-specifier shall not be specified in an explicit
21094        specialization...  */
21095     if (decl
21096         && explicit_specialization_p
21097         && decl_specifiers.storage_class != sc_none)
21098       {
21099         error_at (decl_spec_token_start->location,
21100                   "explicit template specialization cannot have a storage class");
21101         decl = error_mark_node;
21102       }
21103     }
21104
21105   /* Look for a trailing `;' after the declaration.  */
21106   if (!function_definition_p
21107       && (decl == error_mark_node
21108           || !cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON)))
21109     cp_parser_skip_to_end_of_block_or_statement (parser);
21110
21111  out:
21112   pop_deferring_access_checks ();
21113
21114   /* Clear any current qualification; whatever comes next is the start
21115      of something new.  */
21116   parser->scope = NULL_TREE;
21117   parser->qualifying_scope = NULL_TREE;
21118   parser->object_scope = NULL_TREE;
21119
21120   return decl;
21121 }
21122
21123 /* Parse a cast-expression that is not the operand of a unary "&".  */
21124
21125 static tree
21126 cp_parser_simple_cast_expression (cp_parser *parser)
21127 {
21128   return cp_parser_cast_expression (parser, /*address_p=*/false,
21129                                     /*cast_p=*/false, NULL);
21130 }
21131
21132 /* Parse a functional cast to TYPE.  Returns an expression
21133    representing the cast.  */
21134
21135 static tree
21136 cp_parser_functional_cast (cp_parser* parser, tree type)
21137 {
21138   VEC(tree,gc) *vec;
21139   tree expression_list;
21140   tree cast;
21141   bool nonconst_p;
21142
21143   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
21144     {
21145       maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21146       expression_list = cp_parser_braced_list (parser, &nonconst_p);
21147       CONSTRUCTOR_IS_DIRECT_INIT (expression_list) = 1;
21148       if (TREE_CODE (type) == TYPE_DECL)
21149         type = TREE_TYPE (type);
21150       return finish_compound_literal (type, expression_list,
21151                                       tf_warning_or_error);
21152     }
21153
21154
21155   vec = cp_parser_parenthesized_expression_list (parser, non_attr,
21156                                                  /*cast_p=*/true,
21157                                                  /*allow_expansion_p=*/true,
21158                                                  /*non_constant_p=*/NULL);
21159   if (vec == NULL)
21160     expression_list = error_mark_node;
21161   else
21162     {
21163       expression_list = build_tree_list_vec (vec);
21164       release_tree_vector (vec);
21165     }
21166
21167   cast = build_functional_cast (type, expression_list,
21168                                 tf_warning_or_error);
21169   /* [expr.const]/1: In an integral constant expression "only type
21170      conversions to integral or enumeration type can be used".  */
21171   if (TREE_CODE (type) == TYPE_DECL)
21172     type = TREE_TYPE (type);
21173   if (cast != error_mark_node
21174       && !cast_valid_in_integral_constant_expression_p (type)
21175       && cp_parser_non_integral_constant_expression (parser,
21176                                                      NIC_CONSTRUCTOR))
21177     return error_mark_node;
21178   return cast;
21179 }
21180
21181 /* Save the tokens that make up the body of a member function defined
21182    in a class-specifier.  The DECL_SPECIFIERS and DECLARATOR have
21183    already been parsed.  The ATTRIBUTES are any GNU "__attribute__"
21184    specifiers applied to the declaration.  Returns the FUNCTION_DECL
21185    for the member function.  */
21186
21187 static tree
21188 cp_parser_save_member_function_body (cp_parser* parser,
21189                                      cp_decl_specifier_seq *decl_specifiers,
21190                                      cp_declarator *declarator,
21191                                      tree attributes)
21192 {
21193   cp_token *first;
21194   cp_token *last;
21195   tree fn;
21196
21197   /* Create the FUNCTION_DECL.  */
21198   fn = grokmethod (decl_specifiers, declarator, attributes);
21199   /* If something went badly wrong, bail out now.  */
21200   if (fn == error_mark_node)
21201     {
21202       /* If there's a function-body, skip it.  */
21203       if (cp_parser_token_starts_function_definition_p
21204           (cp_lexer_peek_token (parser->lexer)))
21205         cp_parser_skip_to_end_of_block_or_statement (parser);
21206       return error_mark_node;
21207     }
21208
21209   /* Remember it, if there default args to post process.  */
21210   cp_parser_save_default_args (parser, fn);
21211
21212   /* Save away the tokens that make up the body of the
21213      function.  */
21214   first = parser->lexer->next_token;
21215   /* We can have braced-init-list mem-initializers before the fn body.  */
21216   if (cp_lexer_next_token_is (parser->lexer, CPP_COLON))
21217     {
21218       cp_lexer_consume_token (parser->lexer);
21219       while (cp_lexer_next_token_is_not (parser->lexer, CPP_OPEN_BRACE)
21220              && cp_lexer_next_token_is_not_keyword (parser->lexer, RID_TRY))
21221         {
21222           /* cache_group will stop after an un-nested { } pair, too.  */
21223           if (cp_parser_cache_group (parser, CPP_CLOSE_PAREN, /*depth=*/0))
21224             break;
21225
21226           /* variadic mem-inits have ... after the ')'.  */
21227           if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21228             cp_lexer_consume_token (parser->lexer);
21229         }
21230     }
21231   cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21232   /* Handle function try blocks.  */
21233   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_CATCH))
21234     cp_parser_cache_group (parser, CPP_CLOSE_BRACE, /*depth=*/0);
21235   last = parser->lexer->next_token;
21236
21237   /* Save away the inline definition; we will process it when the
21238      class is complete.  */
21239   DECL_PENDING_INLINE_INFO (fn) = cp_token_cache_new (first, last);
21240   DECL_PENDING_INLINE_P (fn) = 1;
21241
21242   /* We need to know that this was defined in the class, so that
21243      friend templates are handled correctly.  */
21244   DECL_INITIALIZED_IN_CLASS_P (fn) = 1;
21245
21246   /* Add FN to the queue of functions to be parsed later.  */
21247   VEC_safe_push (tree, gc, unparsed_funs_with_definitions, fn);
21248
21249   return fn;
21250 }
21251
21252 /* Save the tokens that make up the in-class initializer for a non-static
21253    data member.  Returns a DEFAULT_ARG.  */
21254
21255 static tree
21256 cp_parser_save_nsdmi (cp_parser* parser)
21257 {
21258   /* Save away the tokens that make up the body of the
21259      function.  */
21260   cp_token *first = parser->lexer->next_token;
21261   cp_token *last;
21262   tree node;
21263
21264   /* Save tokens until the next comma or semicolon.  */
21265   cp_parser_cache_group (parser, CPP_COMMA, /*depth=*/0);
21266
21267   last = parser->lexer->next_token;
21268
21269   node = make_node (DEFAULT_ARG);
21270   DEFARG_TOKENS (node) = cp_token_cache_new (first, last);
21271   DEFARG_INSTANTIATIONS (node) = NULL;
21272
21273   return node;
21274 }
21275
21276
21277 /* Parse a template-argument-list, as well as the trailing ">" (but
21278    not the opening "<").  See cp_parser_template_argument_list for the
21279    return value.  */
21280
21281 static tree
21282 cp_parser_enclosed_template_argument_list (cp_parser* parser)
21283 {
21284   tree arguments;
21285   tree saved_scope;
21286   tree saved_qualifying_scope;
21287   tree saved_object_scope;
21288   bool saved_greater_than_is_operator_p;
21289   int saved_unevaluated_operand;
21290   int saved_inhibit_evaluation_warnings;
21291
21292   /* [temp.names]
21293
21294      When parsing a template-id, the first non-nested `>' is taken as
21295      the end of the template-argument-list rather than a greater-than
21296      operator.  */
21297   saved_greater_than_is_operator_p
21298     = parser->greater_than_is_operator_p;
21299   parser->greater_than_is_operator_p = false;
21300   /* Parsing the argument list may modify SCOPE, so we save it
21301      here.  */
21302   saved_scope = parser->scope;
21303   saved_qualifying_scope = parser->qualifying_scope;
21304   saved_object_scope = parser->object_scope;
21305   /* We need to evaluate the template arguments, even though this
21306      template-id may be nested within a "sizeof".  */
21307   saved_unevaluated_operand = cp_unevaluated_operand;
21308   cp_unevaluated_operand = 0;
21309   saved_inhibit_evaluation_warnings = c_inhibit_evaluation_warnings;
21310   c_inhibit_evaluation_warnings = 0;
21311   /* Parse the template-argument-list itself.  */
21312   if (cp_lexer_next_token_is (parser->lexer, CPP_GREATER)
21313       || cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21314     arguments = NULL_TREE;
21315   else
21316     arguments = cp_parser_template_argument_list (parser);
21317   /* Look for the `>' that ends the template-argument-list. If we find
21318      a '>>' instead, it's probably just a typo.  */
21319   if (cp_lexer_next_token_is (parser->lexer, CPP_RSHIFT))
21320     {
21321       if (cxx_dialect != cxx98)
21322         {
21323           /* In C++0x, a `>>' in a template argument list or cast
21324              expression is considered to be two separate `>'
21325              tokens. So, change the current token to a `>', but don't
21326              consume it: it will be consumed later when the outer
21327              template argument list (or cast expression) is parsed.
21328              Note that this replacement of `>' for `>>' is necessary
21329              even if we are parsing tentatively: in the tentative
21330              case, after calling
21331              cp_parser_enclosed_template_argument_list we will always
21332              throw away all of the template arguments and the first
21333              closing `>', either because the template argument list
21334              was erroneous or because we are replacing those tokens
21335              with a CPP_TEMPLATE_ID token.  The second `>' (which will
21336              not have been thrown away) is needed either to close an
21337              outer template argument list or to complete a new-style
21338              cast.  */
21339           cp_token *token = cp_lexer_peek_token (parser->lexer);
21340           token->type = CPP_GREATER;
21341         }
21342       else if (!saved_greater_than_is_operator_p)
21343         {
21344           /* If we're in a nested template argument list, the '>>' has
21345             to be a typo for '> >'. We emit the error message, but we
21346             continue parsing and we push a '>' as next token, so that
21347             the argument list will be parsed correctly.  Note that the
21348             global source location is still on the token before the
21349             '>>', so we need to say explicitly where we want it.  */
21350           cp_token *token = cp_lexer_peek_token (parser->lexer);
21351           error_at (token->location, "%<>>%> should be %<> >%> "
21352                     "within a nested template argument list");
21353
21354           token->type = CPP_GREATER;
21355         }
21356       else
21357         {
21358           /* If this is not a nested template argument list, the '>>'
21359             is a typo for '>'. Emit an error message and continue.
21360             Same deal about the token location, but here we can get it
21361             right by consuming the '>>' before issuing the diagnostic.  */
21362           cp_token *token = cp_lexer_consume_token (parser->lexer);
21363           error_at (token->location,
21364                     "spurious %<>>%>, use %<>%> to terminate "
21365                     "a template argument list");
21366         }
21367     }
21368   else
21369     cp_parser_skip_to_end_of_template_parameter_list (parser);
21370   /* The `>' token might be a greater-than operator again now.  */
21371   parser->greater_than_is_operator_p
21372     = saved_greater_than_is_operator_p;
21373   /* Restore the SAVED_SCOPE.  */
21374   parser->scope = saved_scope;
21375   parser->qualifying_scope = saved_qualifying_scope;
21376   parser->object_scope = saved_object_scope;
21377   cp_unevaluated_operand = saved_unevaluated_operand;
21378   c_inhibit_evaluation_warnings = saved_inhibit_evaluation_warnings;
21379
21380   return arguments;
21381 }
21382
21383 /* MEMBER_FUNCTION is a member function, or a friend.  If default
21384    arguments, or the body of the function have not yet been parsed,
21385    parse them now.  */
21386
21387 static void
21388 cp_parser_late_parsing_for_member (cp_parser* parser, tree member_function)
21389 {
21390   timevar_push (TV_PARSE_INMETH);
21391   /* If this member is a template, get the underlying
21392      FUNCTION_DECL.  */
21393   if (DECL_FUNCTION_TEMPLATE_P (member_function))
21394     member_function = DECL_TEMPLATE_RESULT (member_function);
21395
21396   /* There should not be any class definitions in progress at this
21397      point; the bodies of members are only parsed outside of all class
21398      definitions.  */
21399   gcc_assert (parser->num_classes_being_defined == 0);
21400   /* While we're parsing the member functions we might encounter more
21401      classes.  We want to handle them right away, but we don't want
21402      them getting mixed up with functions that are currently in the
21403      queue.  */
21404   push_unparsed_function_queues (parser);
21405
21406   /* Make sure that any template parameters are in scope.  */
21407   maybe_begin_member_template_processing (member_function);
21408
21409   /* If the body of the function has not yet been parsed, parse it
21410      now.  */
21411   if (DECL_PENDING_INLINE_P (member_function))
21412     {
21413       tree function_scope;
21414       cp_token_cache *tokens;
21415
21416       /* The function is no longer pending; we are processing it.  */
21417       tokens = DECL_PENDING_INLINE_INFO (member_function);
21418       DECL_PENDING_INLINE_INFO (member_function) = NULL;
21419       DECL_PENDING_INLINE_P (member_function) = 0;
21420
21421       /* If this is a local class, enter the scope of the containing
21422          function.  */
21423       function_scope = current_function_decl;
21424       if (function_scope)
21425         push_function_context ();
21426
21427       /* Push the body of the function onto the lexer stack.  */
21428       cp_parser_push_lexer_for_tokens (parser, tokens);
21429
21430       /* Let the front end know that we going to be defining this
21431          function.  */
21432       start_preparsed_function (member_function, NULL_TREE,
21433                                 SF_PRE_PARSED | SF_INCLASS_INLINE);
21434
21435       /* Don't do access checking if it is a templated function.  */
21436       if (processing_template_decl)
21437         push_deferring_access_checks (dk_no_check);
21438
21439       /* Now, parse the body of the function.  */
21440       cp_parser_function_definition_after_declarator (parser,
21441                                                       /*inline_p=*/true);
21442
21443       if (processing_template_decl)
21444         pop_deferring_access_checks ();
21445
21446       /* Leave the scope of the containing function.  */
21447       if (function_scope)
21448         pop_function_context ();
21449       cp_parser_pop_lexer (parser);
21450     }
21451
21452   /* Remove any template parameters from the symbol table.  */
21453   maybe_end_member_template_processing ();
21454
21455   /* Restore the queue.  */
21456   pop_unparsed_function_queues (parser);
21457   timevar_pop (TV_PARSE_INMETH);
21458 }
21459
21460 /* If DECL contains any default args, remember it on the unparsed
21461    functions queue.  */
21462
21463 static void
21464 cp_parser_save_default_args (cp_parser* parser, tree decl)
21465 {
21466   tree probe;
21467
21468   for (probe = TYPE_ARG_TYPES (TREE_TYPE (decl));
21469        probe;
21470        probe = TREE_CHAIN (probe))
21471     if (TREE_PURPOSE (probe))
21472       {
21473         cp_default_arg_entry *entry
21474           = VEC_safe_push (cp_default_arg_entry, gc,
21475                            unparsed_funs_with_default_args, NULL);
21476         entry->class_type = current_class_type;
21477         entry->decl = decl;
21478         break;
21479       }
21480 }
21481
21482 /* DEFAULT_ARG contains the saved tokens for the initializer of DECL,
21483    which is either a FIELD_DECL or PARM_DECL.  Parse it and return
21484    the result.  For a PARM_DECL, PARMTYPE is the corresponding type
21485    from the parameter-type-list.  */
21486
21487 static tree
21488 cp_parser_late_parse_one_default_arg (cp_parser *parser, tree decl,
21489                                       tree default_arg, tree parmtype)
21490 {
21491   cp_token_cache *tokens;
21492   tree parsed_arg;
21493   bool dummy;
21494
21495   /* Push the saved tokens for the default argument onto the parser's
21496      lexer stack.  */
21497   tokens = DEFARG_TOKENS (default_arg);
21498   cp_parser_push_lexer_for_tokens (parser, tokens);
21499
21500   start_lambda_scope (decl);
21501
21502   /* Parse the default argument.  */
21503   parsed_arg = cp_parser_initializer (parser, &dummy, &dummy);
21504   if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg))
21505     maybe_warn_cpp0x (CPP0X_INITIALIZER_LISTS);
21506
21507   finish_lambda_scope ();
21508
21509   if (!processing_template_decl)
21510     {
21511       /* In a non-template class, check conversions now.  In a template,
21512          we'll wait and instantiate these as needed.  */
21513       if (TREE_CODE (decl) == PARM_DECL)
21514         parsed_arg = check_default_argument (parmtype, parsed_arg);
21515       else
21516         {
21517           int flags = LOOKUP_IMPLICIT;
21518           if (BRACE_ENCLOSED_INITIALIZER_P (parsed_arg)
21519               && CONSTRUCTOR_IS_DIRECT_INIT (parsed_arg))
21520             flags = LOOKUP_NORMAL;
21521           parsed_arg = digest_init_flags (TREE_TYPE (decl), parsed_arg, flags);
21522         }
21523     }
21524
21525   /* If the token stream has not been completely used up, then
21526      there was extra junk after the end of the default
21527      argument.  */
21528   if (!cp_lexer_next_token_is (parser->lexer, CPP_EOF))
21529     {
21530       if (TREE_CODE (decl) == PARM_DECL)
21531         cp_parser_error (parser, "expected %<,%>");
21532       else
21533         cp_parser_error (parser, "expected %<;%>");
21534     }
21535
21536   /* Revert to the main lexer.  */
21537   cp_parser_pop_lexer (parser);
21538
21539   return parsed_arg;
21540 }
21541
21542 /* FIELD is a non-static data member with an initializer which we saved for
21543    later; parse it now.  */
21544
21545 static void
21546 cp_parser_late_parsing_nsdmi (cp_parser *parser, tree field)
21547 {
21548   tree def;
21549
21550   push_unparsed_function_queues (parser);
21551   def = cp_parser_late_parse_one_default_arg (parser, field,
21552                                               DECL_INITIAL (field),
21553                                               NULL_TREE);
21554   pop_unparsed_function_queues (parser);
21555
21556   DECL_INITIAL (field) = def;
21557 }
21558
21559 /* FN is a FUNCTION_DECL which may contains a parameter with an
21560    unparsed DEFAULT_ARG.  Parse the default args now.  This function
21561    assumes that the current scope is the scope in which the default
21562    argument should be processed.  */
21563
21564 static void
21565 cp_parser_late_parsing_default_args (cp_parser *parser, tree fn)
21566 {
21567   bool saved_local_variables_forbidden_p;
21568   tree parm, parmdecl;
21569
21570   /* While we're parsing the default args, we might (due to the
21571      statement expression extension) encounter more classes.  We want
21572      to handle them right away, but we don't want them getting mixed
21573      up with default args that are currently in the queue.  */
21574   push_unparsed_function_queues (parser);
21575
21576   /* Local variable names (and the `this' keyword) may not appear
21577      in a default argument.  */
21578   saved_local_variables_forbidden_p = parser->local_variables_forbidden_p;
21579   parser->local_variables_forbidden_p = true;
21580
21581   push_defarg_context (fn);
21582
21583   for (parm = TYPE_ARG_TYPES (TREE_TYPE (fn)),
21584          parmdecl = DECL_ARGUMENTS (fn);
21585        parm && parm != void_list_node;
21586        parm = TREE_CHAIN (parm),
21587          parmdecl = DECL_CHAIN (parmdecl))
21588     {
21589       tree default_arg = TREE_PURPOSE (parm);
21590       tree parsed_arg;
21591       VEC(tree,gc) *insts;
21592       tree copy;
21593       unsigned ix;
21594
21595       if (!default_arg)
21596         continue;
21597
21598       if (TREE_CODE (default_arg) != DEFAULT_ARG)
21599         /* This can happen for a friend declaration for a function
21600            already declared with default arguments.  */
21601         continue;
21602
21603       parsed_arg
21604         = cp_parser_late_parse_one_default_arg (parser, parmdecl,
21605                                                 default_arg,
21606                                                 TREE_VALUE (parm));
21607       if (parsed_arg == error_mark_node)
21608         {
21609           continue;
21610         }
21611
21612       TREE_PURPOSE (parm) = parsed_arg;
21613
21614       /* Update any instantiations we've already created.  */
21615       for (insts = DEFARG_INSTANTIATIONS (default_arg), ix = 0;
21616            VEC_iterate (tree, insts, ix, copy); ix++)
21617         TREE_PURPOSE (copy) = parsed_arg;
21618     }
21619
21620   pop_defarg_context ();
21621
21622   /* Make sure no default arg is missing.  */
21623   check_default_args (fn);
21624
21625   /* Restore the state of local_variables_forbidden_p.  */
21626   parser->local_variables_forbidden_p = saved_local_variables_forbidden_p;
21627
21628   /* Restore the queue.  */
21629   pop_unparsed_function_queues (parser);
21630 }
21631
21632 /* Parse the operand of `sizeof' (or a similar operator).  Returns
21633    either a TYPE or an expression, depending on the form of the
21634    input.  The KEYWORD indicates which kind of expression we have
21635    encountered.  */
21636
21637 static tree
21638 cp_parser_sizeof_operand (cp_parser* parser, enum rid keyword)
21639 {
21640   tree expr = NULL_TREE;
21641   const char *saved_message;
21642   char *tmp;
21643   bool saved_integral_constant_expression_p;
21644   bool saved_non_integral_constant_expression_p;
21645   bool pack_expansion_p = false;
21646
21647   /* Types cannot be defined in a `sizeof' expression.  Save away the
21648      old message.  */
21649   saved_message = parser->type_definition_forbidden_message;
21650   /* And create the new one.  */
21651   tmp = concat ("types may not be defined in %<",
21652                 IDENTIFIER_POINTER (ridpointers[keyword]),
21653                 "%> expressions", NULL);
21654   parser->type_definition_forbidden_message = tmp;
21655
21656   /* The restrictions on constant-expressions do not apply inside
21657      sizeof expressions.  */
21658   saved_integral_constant_expression_p
21659     = parser->integral_constant_expression_p;
21660   saved_non_integral_constant_expression_p
21661     = parser->non_integral_constant_expression_p;
21662   parser->integral_constant_expression_p = false;
21663
21664   /* If it's a `...', then we are computing the length of a parameter
21665      pack.  */
21666   if (keyword == RID_SIZEOF
21667       && cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
21668     {
21669       /* Consume the `...'.  */
21670       cp_lexer_consume_token (parser->lexer);
21671       maybe_warn_variadic_templates ();
21672
21673       /* Note that this is an expansion.  */
21674       pack_expansion_p = true;
21675     }
21676
21677   /* Do not actually evaluate the expression.  */
21678   ++cp_unevaluated_operand;
21679   ++c_inhibit_evaluation_warnings;
21680   /* If it's a `(', then we might be looking at the type-id
21681      construction.  */
21682   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
21683     {
21684       tree type;
21685       bool saved_in_type_id_in_expr_p;
21686
21687       /* We can't be sure yet whether we're looking at a type-id or an
21688          expression.  */
21689       cp_parser_parse_tentatively (parser);
21690       /* Consume the `('.  */
21691       cp_lexer_consume_token (parser->lexer);
21692       /* Parse the type-id.  */
21693       saved_in_type_id_in_expr_p = parser->in_type_id_in_expr_p;
21694       parser->in_type_id_in_expr_p = true;
21695       type = cp_parser_type_id (parser);
21696       parser->in_type_id_in_expr_p = saved_in_type_id_in_expr_p;
21697       /* Now, look for the trailing `)'.  */
21698       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
21699       /* If all went well, then we're done.  */
21700       if (cp_parser_parse_definitely (parser))
21701         {
21702           cp_decl_specifier_seq decl_specs;
21703
21704           /* Build a trivial decl-specifier-seq.  */
21705           clear_decl_specs (&decl_specs);
21706           decl_specs.type = type;
21707
21708           /* Call grokdeclarator to figure out what type this is.  */
21709           expr = grokdeclarator (NULL,
21710                                  &decl_specs,
21711                                  TYPENAME,
21712                                  /*initialized=*/0,
21713                                  /*attrlist=*/NULL);
21714         }
21715     }
21716
21717   /* If the type-id production did not work out, then we must be
21718      looking at the unary-expression production.  */
21719   if (!expr)
21720     expr = cp_parser_unary_expression (parser, /*address_p=*/false,
21721                                        /*cast_p=*/false, NULL);
21722
21723   if (pack_expansion_p)
21724     /* Build a pack expansion. */
21725     expr = make_pack_expansion (expr);
21726
21727   /* Go back to evaluating expressions.  */
21728   --cp_unevaluated_operand;
21729   --c_inhibit_evaluation_warnings;
21730
21731   /* Free the message we created.  */
21732   free (tmp);
21733   /* And restore the old one.  */
21734   parser->type_definition_forbidden_message = saved_message;
21735   parser->integral_constant_expression_p
21736     = saved_integral_constant_expression_p;
21737   parser->non_integral_constant_expression_p
21738     = saved_non_integral_constant_expression_p;
21739
21740   return expr;
21741 }
21742
21743 /* If the current declaration has no declarator, return true.  */
21744
21745 static bool
21746 cp_parser_declares_only_class_p (cp_parser *parser)
21747 {
21748   /* If the next token is a `;' or a `,' then there is no
21749      declarator.  */
21750   return (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
21751           || cp_lexer_next_token_is (parser->lexer, CPP_COMMA));
21752 }
21753
21754 /* Update the DECL_SPECS to reflect the storage class indicated by
21755    KEYWORD.  */
21756
21757 static void
21758 cp_parser_set_storage_class (cp_parser *parser,
21759                              cp_decl_specifier_seq *decl_specs,
21760                              enum rid keyword,
21761                              location_t location)
21762 {
21763   cp_storage_class storage_class;
21764
21765   if (parser->in_unbraced_linkage_specification_p)
21766     {
21767       error_at (location, "invalid use of %qD in linkage specification",
21768                 ridpointers[keyword]);
21769       return;
21770     }
21771   else if (decl_specs->storage_class != sc_none)
21772     {
21773       decl_specs->conflicting_specifiers_p = true;
21774       return;
21775     }
21776
21777   if ((keyword == RID_EXTERN || keyword == RID_STATIC)
21778       && decl_specs->specs[(int) ds_thread])
21779     {
21780       error_at (location, "%<__thread%> before %qD", ridpointers[keyword]);
21781       decl_specs->specs[(int) ds_thread] = 0;
21782     }
21783
21784   switch (keyword)
21785     {
21786     case RID_AUTO:
21787       storage_class = sc_auto;
21788       break;
21789     case RID_REGISTER:
21790       storage_class = sc_register;
21791       break;
21792     case RID_STATIC:
21793       storage_class = sc_static;
21794       break;
21795     case RID_EXTERN:
21796       storage_class = sc_extern;
21797       break;
21798     case RID_MUTABLE:
21799       storage_class = sc_mutable;
21800       break;
21801     default:
21802       gcc_unreachable ();
21803     }
21804   decl_specs->storage_class = storage_class;
21805
21806   /* A storage class specifier cannot be applied alongside a typedef 
21807      specifier. If there is a typedef specifier present then set 
21808      conflicting_specifiers_p which will trigger an error later
21809      on in grokdeclarator. */
21810   if (decl_specs->specs[(int)ds_typedef])
21811     decl_specs->conflicting_specifiers_p = true;
21812 }
21813
21814 /* Update the DECL_SPECS to reflect the TYPE_SPEC.  If TYPE_DEFINITION_P
21815    is true, the type is a class or enum definition.  */
21816
21817 static void
21818 cp_parser_set_decl_spec_type (cp_decl_specifier_seq *decl_specs,
21819                               tree type_spec,
21820                               location_t location,
21821                               bool type_definition_p)
21822 {
21823   decl_specs->any_specifiers_p = true;
21824
21825   /* If the user tries to redeclare bool, char16_t, char32_t, or wchar_t
21826      (with, for example, in "typedef int wchar_t;") we remember that
21827      this is what happened.  In system headers, we ignore these
21828      declarations so that G++ can work with system headers that are not
21829      C++-safe.  */
21830   if (decl_specs->specs[(int) ds_typedef]
21831       && !type_definition_p
21832       && (type_spec == boolean_type_node
21833           || type_spec == char16_type_node
21834           || type_spec == char32_type_node
21835           || type_spec == wchar_type_node)
21836       && (decl_specs->type
21837           || decl_specs->specs[(int) ds_long]
21838           || decl_specs->specs[(int) ds_short]
21839           || decl_specs->specs[(int) ds_unsigned]
21840           || decl_specs->specs[(int) ds_signed]))
21841     {
21842       decl_specs->redefined_builtin_type = type_spec;
21843       if (!decl_specs->type)
21844         {
21845           decl_specs->type = type_spec;
21846           decl_specs->type_definition_p = false;
21847           decl_specs->type_location = location;
21848         }
21849     }
21850   else if (decl_specs->type)
21851     decl_specs->multiple_types_p = true;
21852   else
21853     {
21854       decl_specs->type = type_spec;
21855       decl_specs->type_definition_p = type_definition_p;
21856       decl_specs->redefined_builtin_type = NULL_TREE;
21857       decl_specs->type_location = location;
21858     }
21859 }
21860
21861 /* DECL_SPECIFIERS is the representation of a decl-specifier-seq.
21862    Returns TRUE iff `friend' appears among the DECL_SPECIFIERS.  */
21863
21864 static bool
21865 cp_parser_friend_p (const cp_decl_specifier_seq *decl_specifiers)
21866 {
21867   return decl_specifiers->specs[(int) ds_friend] != 0;
21868 }
21869
21870 /* Issue an error message indicating that TOKEN_DESC was expected.
21871    If KEYWORD is true, it indicated this function is called by
21872    cp_parser_require_keword and the required token can only be
21873    a indicated keyword. */
21874
21875 static void
21876 cp_parser_required_error (cp_parser *parser,
21877                           required_token token_desc,
21878                           bool keyword)
21879 {
21880   switch (token_desc)
21881     {
21882       case RT_NEW:
21883         cp_parser_error (parser, "expected %<new%>");
21884         return;
21885       case RT_DELETE:
21886         cp_parser_error (parser, "expected %<delete%>");
21887         return;
21888       case RT_RETURN:
21889         cp_parser_error (parser, "expected %<return%>");
21890         return;
21891       case RT_WHILE:
21892         cp_parser_error (parser, "expected %<while%>");
21893         return;
21894       case RT_EXTERN:
21895         cp_parser_error (parser, "expected %<extern%>");
21896         return;
21897       case RT_STATIC_ASSERT:
21898         cp_parser_error (parser, "expected %<static_assert%>");
21899         return;
21900       case RT_DECLTYPE:
21901         cp_parser_error (parser, "expected %<decltype%>");
21902         return;
21903       case RT_OPERATOR:
21904         cp_parser_error (parser, "expected %<operator%>");
21905         return;
21906       case RT_CLASS:
21907         cp_parser_error (parser, "expected %<class%>");
21908         return;
21909       case RT_TEMPLATE:
21910         cp_parser_error (parser, "expected %<template%>");
21911         return;
21912       case RT_NAMESPACE:
21913         cp_parser_error (parser, "expected %<namespace%>");
21914         return;
21915       case RT_USING:
21916         cp_parser_error (parser, "expected %<using%>");
21917         return;
21918       case RT_ASM:
21919         cp_parser_error (parser, "expected %<asm%>");
21920         return;
21921       case RT_TRY:
21922         cp_parser_error (parser, "expected %<try%>");
21923         return;
21924       case RT_CATCH:
21925         cp_parser_error (parser, "expected %<catch%>");
21926         return;
21927       case RT_THROW:
21928         cp_parser_error (parser, "expected %<throw%>");
21929         return;
21930       case RT_LABEL:
21931         cp_parser_error (parser, "expected %<__label__%>");
21932         return;
21933       case RT_AT_TRY:
21934         cp_parser_error (parser, "expected %<@try%>");
21935         return;
21936       case RT_AT_SYNCHRONIZED:
21937         cp_parser_error (parser, "expected %<@synchronized%>");
21938         return;
21939       case RT_AT_THROW:
21940         cp_parser_error (parser, "expected %<@throw%>");
21941         return;
21942       default:
21943         break;
21944     }
21945   if (!keyword)
21946     {
21947       switch (token_desc)
21948         {
21949           case RT_SEMICOLON:
21950             cp_parser_error (parser, "expected %<;%>");
21951             return;
21952           case RT_OPEN_PAREN:
21953             cp_parser_error (parser, "expected %<(%>");
21954             return;
21955           case RT_CLOSE_BRACE:
21956             cp_parser_error (parser, "expected %<}%>");
21957             return;
21958           case RT_OPEN_BRACE:
21959             cp_parser_error (parser, "expected %<{%>");
21960             return;
21961           case RT_CLOSE_SQUARE:
21962             cp_parser_error (parser, "expected %<]%>");
21963             return;
21964           case RT_OPEN_SQUARE:
21965             cp_parser_error (parser, "expected %<[%>");
21966             return;
21967           case RT_COMMA:
21968             cp_parser_error (parser, "expected %<,%>");
21969             return;
21970           case RT_SCOPE:
21971             cp_parser_error (parser, "expected %<::%>");
21972             return;
21973           case RT_LESS:
21974             cp_parser_error (parser, "expected %<<%>");
21975             return;
21976           case RT_GREATER:
21977             cp_parser_error (parser, "expected %<>%>");
21978             return;
21979           case RT_EQ:
21980             cp_parser_error (parser, "expected %<=%>");
21981             return;
21982           case RT_ELLIPSIS:
21983             cp_parser_error (parser, "expected %<...%>");
21984             return;
21985           case RT_MULT:
21986             cp_parser_error (parser, "expected %<*%>");
21987             return;
21988           case RT_COMPL:
21989             cp_parser_error (parser, "expected %<~%>");
21990             return;
21991           case RT_COLON:
21992             cp_parser_error (parser, "expected %<:%>");
21993             return;
21994           case RT_COLON_SCOPE:
21995             cp_parser_error (parser, "expected %<:%> or %<::%>");
21996             return;
21997           case RT_CLOSE_PAREN:
21998             cp_parser_error (parser, "expected %<)%>");
21999             return;
22000           case RT_COMMA_CLOSE_PAREN:
22001             cp_parser_error (parser, "expected %<,%> or %<)%>");
22002             return;
22003           case RT_PRAGMA_EOL:
22004             cp_parser_error (parser, "expected end of line");
22005             return;
22006           case RT_NAME:
22007             cp_parser_error (parser, "expected identifier");
22008             return;
22009           case RT_SELECT:
22010             cp_parser_error (parser, "expected selection-statement");
22011             return;
22012           case RT_INTERATION:
22013             cp_parser_error (parser, "expected iteration-statement");
22014             return;
22015           case RT_JUMP:
22016             cp_parser_error (parser, "expected jump-statement");
22017             return;
22018           case RT_CLASS_KEY:
22019             cp_parser_error (parser, "expected class-key");
22020             return;
22021           case RT_CLASS_TYPENAME_TEMPLATE:
22022             cp_parser_error (parser,
22023                  "expected %<class%>, %<typename%>, or %<template%>");
22024             return;
22025           default:
22026             gcc_unreachable ();
22027         }
22028     }
22029   else
22030     gcc_unreachable ();
22031 }
22032
22033
22034
22035 /* If the next token is of the indicated TYPE, consume it.  Otherwise,
22036    issue an error message indicating that TOKEN_DESC was expected.
22037
22038    Returns the token consumed, if the token had the appropriate type.
22039    Otherwise, returns NULL.  */
22040
22041 static cp_token *
22042 cp_parser_require (cp_parser* parser,
22043                    enum cpp_ttype type,
22044                    required_token token_desc)
22045 {
22046   if (cp_lexer_next_token_is (parser->lexer, type))
22047     return cp_lexer_consume_token (parser->lexer);
22048   else
22049     {
22050       /* Output the MESSAGE -- unless we're parsing tentatively.  */
22051       if (!cp_parser_simulate_error (parser))
22052         cp_parser_required_error (parser, token_desc, /*keyword=*/false);
22053       return NULL;
22054     }
22055 }
22056
22057 /* An error message is produced if the next token is not '>'.
22058    All further tokens are skipped until the desired token is
22059    found or '{', '}', ';' or an unbalanced ')' or ']'.  */
22060
22061 static void
22062 cp_parser_skip_to_end_of_template_parameter_list (cp_parser* parser)
22063 {
22064   /* Current level of '< ... >'.  */
22065   unsigned level = 0;
22066   /* Ignore '<' and '>' nested inside '( ... )' or '[ ... ]'.  */
22067   unsigned nesting_depth = 0;
22068
22069   /* Are we ready, yet?  If not, issue error message.  */
22070   if (cp_parser_require (parser, CPP_GREATER, RT_GREATER))
22071     return;
22072
22073   /* Skip tokens until the desired token is found.  */
22074   while (true)
22075     {
22076       /* Peek at the next token.  */
22077       switch (cp_lexer_peek_token (parser->lexer)->type)
22078         {
22079         case CPP_LESS:
22080           if (!nesting_depth)
22081             ++level;
22082           break;
22083
22084         case CPP_RSHIFT:
22085           if (cxx_dialect == cxx98)
22086             /* C++0x views the `>>' operator as two `>' tokens, but
22087                C++98 does not. */
22088             break;
22089           else if (!nesting_depth && level-- == 0)
22090             {
22091               /* We've hit a `>>' where the first `>' closes the
22092                  template argument list, and the second `>' is
22093                  spurious.  Just consume the `>>' and stop; we've
22094                  already produced at least one error.  */
22095               cp_lexer_consume_token (parser->lexer);
22096               return;
22097             }
22098           /* Fall through for C++0x, so we handle the second `>' in
22099              the `>>'.  */
22100
22101         case CPP_GREATER:
22102           if (!nesting_depth && level-- == 0)
22103             {
22104               /* We've reached the token we want, consume it and stop.  */
22105               cp_lexer_consume_token (parser->lexer);
22106               return;
22107             }
22108           break;
22109
22110         case CPP_OPEN_PAREN:
22111         case CPP_OPEN_SQUARE:
22112           ++nesting_depth;
22113           break;
22114
22115         case CPP_CLOSE_PAREN:
22116         case CPP_CLOSE_SQUARE:
22117           if (nesting_depth-- == 0)
22118             return;
22119           break;
22120
22121         case CPP_EOF:
22122         case CPP_PRAGMA_EOL:
22123         case CPP_SEMICOLON:
22124         case CPP_OPEN_BRACE:
22125         case CPP_CLOSE_BRACE:
22126           /* The '>' was probably forgotten, don't look further.  */
22127           return;
22128
22129         default:
22130           break;
22131         }
22132
22133       /* Consume this token.  */
22134       cp_lexer_consume_token (parser->lexer);
22135     }
22136 }
22137
22138 /* If the next token is the indicated keyword, consume it.  Otherwise,
22139    issue an error message indicating that TOKEN_DESC was expected.
22140
22141    Returns the token consumed, if the token had the appropriate type.
22142    Otherwise, returns NULL.  */
22143
22144 static cp_token *
22145 cp_parser_require_keyword (cp_parser* parser,
22146                            enum rid keyword,
22147                            required_token token_desc)
22148 {
22149   cp_token *token = cp_parser_require (parser, CPP_KEYWORD, token_desc);
22150
22151   if (token && token->keyword != keyword)
22152     {
22153       cp_parser_required_error (parser, token_desc, /*keyword=*/true); 
22154       return NULL;
22155     }
22156
22157   return token;
22158 }
22159
22160 /* Returns TRUE iff TOKEN is a token that can begin the body of a
22161    function-definition.  */
22162
22163 static bool
22164 cp_parser_token_starts_function_definition_p (cp_token* token)
22165 {
22166   return (/* An ordinary function-body begins with an `{'.  */
22167           token->type == CPP_OPEN_BRACE
22168           /* A ctor-initializer begins with a `:'.  */
22169           || token->type == CPP_COLON
22170           /* A function-try-block begins with `try'.  */
22171           || token->keyword == RID_TRY
22172           /* The named return value extension begins with `return'.  */
22173           || token->keyword == RID_RETURN);
22174 }
22175
22176 /* Returns TRUE iff the next token is the ":" or "{" beginning a class
22177    definition.  */
22178
22179 static bool
22180 cp_parser_next_token_starts_class_definition_p (cp_parser *parser)
22181 {
22182   cp_token *token;
22183
22184   token = cp_lexer_peek_token (parser->lexer);
22185   return (token->type == CPP_OPEN_BRACE || token->type == CPP_COLON);
22186 }
22187
22188 /* Returns TRUE iff the next token is the "," or ">" (or `>>', in
22189    C++0x) ending a template-argument.  */
22190
22191 static bool
22192 cp_parser_next_token_ends_template_argument_p (cp_parser *parser)
22193 {
22194   cp_token *token;
22195
22196   token = cp_lexer_peek_token (parser->lexer);
22197   return (token->type == CPP_COMMA 
22198           || token->type == CPP_GREATER
22199           || token->type == CPP_ELLIPSIS
22200           || ((cxx_dialect != cxx98) && token->type == CPP_RSHIFT));
22201 }
22202
22203 /* Returns TRUE iff the n-th token is a "<", or the n-th is a "[" and the
22204    (n+1)-th is a ":" (which is a possible digraph typo for "< ::").  */
22205
22206 static bool
22207 cp_parser_nth_token_starts_template_argument_list_p (cp_parser * parser,
22208                                                      size_t n)
22209 {
22210   cp_token *token;
22211
22212   token = cp_lexer_peek_nth_token (parser->lexer, n);
22213   if (token->type == CPP_LESS)
22214     return true;
22215   /* Check for the sequence `<::' in the original code. It would be lexed as
22216      `[:', where `[' is a digraph, and there is no whitespace before
22217      `:'.  */
22218   if (token->type == CPP_OPEN_SQUARE && token->flags & DIGRAPH)
22219     {
22220       cp_token *token2;
22221       token2 = cp_lexer_peek_nth_token (parser->lexer, n+1);
22222       if (token2->type == CPP_COLON && !(token2->flags & PREV_WHITE))
22223         return true;
22224     }
22225   return false;
22226 }
22227
22228 /* Returns the kind of tag indicated by TOKEN, if it is a class-key,
22229    or none_type otherwise.  */
22230
22231 static enum tag_types
22232 cp_parser_token_is_class_key (cp_token* token)
22233 {
22234   switch (token->keyword)
22235     {
22236     case RID_CLASS:
22237       return class_type;
22238     case RID_STRUCT:
22239       return record_type;
22240     case RID_UNION:
22241       return union_type;
22242
22243     default:
22244       return none_type;
22245     }
22246 }
22247
22248 /* Issue an error message if the CLASS_KEY does not match the TYPE.  */
22249
22250 static void
22251 cp_parser_check_class_key (enum tag_types class_key, tree type)
22252 {
22253   if ((TREE_CODE (type) == UNION_TYPE) != (class_key == union_type))
22254     permerror (input_location, "%qs tag used in naming %q#T",
22255             class_key == union_type ? "union"
22256              : class_key == record_type ? "struct" : "class",
22257              type);
22258 }
22259
22260 /* Issue an error message if DECL is redeclared with different
22261    access than its original declaration [class.access.spec/3].
22262    This applies to nested classes and nested class templates.
22263    [class.mem/1].  */
22264
22265 static void
22266 cp_parser_check_access_in_redeclaration (tree decl, location_t location)
22267 {
22268   if (!decl || !CLASS_TYPE_P (TREE_TYPE (decl)))
22269     return;
22270
22271   if ((TREE_PRIVATE (decl)
22272        != (current_access_specifier == access_private_node))
22273       || (TREE_PROTECTED (decl)
22274           != (current_access_specifier == access_protected_node)))
22275     error_at (location, "%qD redeclared with different access", decl);
22276 }
22277
22278 /* Look for the `template' keyword, as a syntactic disambiguator.
22279    Return TRUE iff it is present, in which case it will be
22280    consumed.  */
22281
22282 static bool
22283 cp_parser_optional_template_keyword (cp_parser *parser)
22284 {
22285   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_TEMPLATE))
22286     {
22287       /* The `template' keyword can only be used within templates;
22288          outside templates the parser can always figure out what is a
22289          template and what is not.  */
22290       if (!processing_template_decl)
22291         {
22292           cp_token *token = cp_lexer_peek_token (parser->lexer);
22293           error_at (token->location,
22294                     "%<template%> (as a disambiguator) is only allowed "
22295                     "within templates");
22296           /* If this part of the token stream is rescanned, the same
22297              error message would be generated.  So, we purge the token
22298              from the stream.  */
22299           cp_lexer_purge_token (parser->lexer);
22300           return false;
22301         }
22302       else
22303         {
22304           /* Consume the `template' keyword.  */
22305           cp_lexer_consume_token (parser->lexer);
22306           return true;
22307         }
22308     }
22309
22310   return false;
22311 }
22312
22313 /* The next token is a CPP_NESTED_NAME_SPECIFIER.  Consume the token,
22314    set PARSER->SCOPE, and perform other related actions.  */
22315
22316 static void
22317 cp_parser_pre_parsed_nested_name_specifier (cp_parser *parser)
22318 {
22319   int i;
22320   struct tree_check *check_value;
22321   deferred_access_check *chk;
22322   VEC (deferred_access_check,gc) *checks;
22323
22324   /* Get the stored value.  */
22325   check_value = cp_lexer_consume_token (parser->lexer)->u.tree_check_value;
22326   /* Perform any access checks that were deferred.  */
22327   checks = check_value->checks;
22328   if (checks)
22329     {
22330       FOR_EACH_VEC_ELT (deferred_access_check, checks, i, chk)
22331         perform_or_defer_access_check (chk->binfo,
22332                                        chk->decl,
22333                                        chk->diag_decl);
22334     }
22335   /* Set the scope from the stored value.  */
22336   parser->scope = check_value->value;
22337   parser->qualifying_scope = check_value->qualifying_scope;
22338   parser->object_scope = NULL_TREE;
22339 }
22340
22341 /* Consume tokens up through a non-nested END token.  Returns TRUE if we
22342    encounter the end of a block before what we were looking for.  */
22343
22344 static bool
22345 cp_parser_cache_group (cp_parser *parser,
22346                        enum cpp_ttype end,
22347                        unsigned depth)
22348 {
22349   while (true)
22350     {
22351       cp_token *token = cp_lexer_peek_token (parser->lexer);
22352
22353       /* Abort a parenthesized expression if we encounter a semicolon.  */
22354       if ((end == CPP_CLOSE_PAREN || depth == 0)
22355           && token->type == CPP_SEMICOLON)
22356         return true;
22357       /* If we've reached the end of the file, stop.  */
22358       if (token->type == CPP_EOF
22359           || (end != CPP_PRAGMA_EOL
22360               && token->type == CPP_PRAGMA_EOL))
22361         return true;
22362       if (token->type == CPP_CLOSE_BRACE && depth == 0)
22363         /* We've hit the end of an enclosing block, so there's been some
22364            kind of syntax error.  */
22365         return true;
22366
22367       /* If we're caching something finished by a comma (or semicolon),
22368          such as an NSDMI, don't consume the comma.  */
22369       if (end == CPP_COMMA
22370           && (token->type == CPP_SEMICOLON || token->type == CPP_COMMA))
22371         return false;
22372
22373       /* Consume the token.  */
22374       cp_lexer_consume_token (parser->lexer);
22375       /* See if it starts a new group.  */
22376       if (token->type == CPP_OPEN_BRACE)
22377         {
22378           cp_parser_cache_group (parser, CPP_CLOSE_BRACE, depth + 1);
22379           /* In theory this should probably check end == '}', but
22380              cp_parser_save_member_function_body needs it to exit
22381              after either '}' or ')' when called with ')'.  */
22382           if (depth == 0)
22383             return false;
22384         }
22385       else if (token->type == CPP_OPEN_PAREN)
22386         {
22387           cp_parser_cache_group (parser, CPP_CLOSE_PAREN, depth + 1);
22388           if (depth == 0 && end == CPP_CLOSE_PAREN)
22389             return false;
22390         }
22391       else if (token->type == CPP_PRAGMA)
22392         cp_parser_cache_group (parser, CPP_PRAGMA_EOL, depth + 1);
22393       else if (token->type == end)
22394         return false;
22395     }
22396 }
22397
22398 /* Begin parsing tentatively.  We always save tokens while parsing
22399    tentatively so that if the tentative parsing fails we can restore the
22400    tokens.  */
22401
22402 static void
22403 cp_parser_parse_tentatively (cp_parser* parser)
22404 {
22405   /* Enter a new parsing context.  */
22406   parser->context = cp_parser_context_new (parser->context);
22407   /* Begin saving tokens.  */
22408   cp_lexer_save_tokens (parser->lexer);
22409   /* In order to avoid repetitive access control error messages,
22410      access checks are queued up until we are no longer parsing
22411      tentatively.  */
22412   push_deferring_access_checks (dk_deferred);
22413 }
22414
22415 /* Commit to the currently active tentative parse.  */
22416
22417 static void
22418 cp_parser_commit_to_tentative_parse (cp_parser* parser)
22419 {
22420   cp_parser_context *context;
22421   cp_lexer *lexer;
22422
22423   /* Mark all of the levels as committed.  */
22424   lexer = parser->lexer;
22425   for (context = parser->context; context->next; context = context->next)
22426     {
22427       if (context->status == CP_PARSER_STATUS_KIND_COMMITTED)
22428         break;
22429       context->status = CP_PARSER_STATUS_KIND_COMMITTED;
22430       while (!cp_lexer_saving_tokens (lexer))
22431         lexer = lexer->next;
22432       cp_lexer_commit_tokens (lexer);
22433     }
22434 }
22435
22436 /* Abort the currently active tentative parse.  All consumed tokens
22437    will be rolled back, and no diagnostics will be issued.  */
22438
22439 static void
22440 cp_parser_abort_tentative_parse (cp_parser* parser)
22441 {
22442   gcc_assert (parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED
22443               || errorcount > 0);
22444   cp_parser_simulate_error (parser);
22445   /* Now, pretend that we want to see if the construct was
22446      successfully parsed.  */
22447   cp_parser_parse_definitely (parser);
22448 }
22449
22450 /* Stop parsing tentatively.  If a parse error has occurred, restore the
22451    token stream.  Otherwise, commit to the tokens we have consumed.
22452    Returns true if no error occurred; false otherwise.  */
22453
22454 static bool
22455 cp_parser_parse_definitely (cp_parser* parser)
22456 {
22457   bool error_occurred;
22458   cp_parser_context *context;
22459
22460   /* Remember whether or not an error occurred, since we are about to
22461      destroy that information.  */
22462   error_occurred = cp_parser_error_occurred (parser);
22463   /* Remove the topmost context from the stack.  */
22464   context = parser->context;
22465   parser->context = context->next;
22466   /* If no parse errors occurred, commit to the tentative parse.  */
22467   if (!error_occurred)
22468     {
22469       /* Commit to the tokens read tentatively, unless that was
22470          already done.  */
22471       if (context->status != CP_PARSER_STATUS_KIND_COMMITTED)
22472         cp_lexer_commit_tokens (parser->lexer);
22473
22474       pop_to_parent_deferring_access_checks ();
22475     }
22476   /* Otherwise, if errors occurred, roll back our state so that things
22477      are just as they were before we began the tentative parse.  */
22478   else
22479     {
22480       cp_lexer_rollback_tokens (parser->lexer);
22481       pop_deferring_access_checks ();
22482     }
22483   /* Add the context to the front of the free list.  */
22484   context->next = cp_parser_context_free_list;
22485   cp_parser_context_free_list = context;
22486
22487   return !error_occurred;
22488 }
22489
22490 /* Returns true if we are parsing tentatively and are not committed to
22491    this tentative parse.  */
22492
22493 static bool
22494 cp_parser_uncommitted_to_tentative_parse_p (cp_parser* parser)
22495 {
22496   return (cp_parser_parsing_tentatively (parser)
22497           && parser->context->status != CP_PARSER_STATUS_KIND_COMMITTED);
22498 }
22499
22500 /* Returns nonzero iff an error has occurred during the most recent
22501    tentative parse.  */
22502
22503 static bool
22504 cp_parser_error_occurred (cp_parser* parser)
22505 {
22506   return (cp_parser_parsing_tentatively (parser)
22507           && parser->context->status == CP_PARSER_STATUS_KIND_ERROR);
22508 }
22509
22510 /* Returns nonzero if GNU extensions are allowed.  */
22511
22512 static bool
22513 cp_parser_allow_gnu_extensions_p (cp_parser* parser)
22514 {
22515   return parser->allow_gnu_extensions_p;
22516 }
22517 \f
22518 /* Objective-C++ Productions */
22519
22520
22521 /* Parse an Objective-C expression, which feeds into a primary-expression
22522    above.
22523
22524    objc-expression:
22525      objc-message-expression
22526      objc-string-literal
22527      objc-encode-expression
22528      objc-protocol-expression
22529      objc-selector-expression
22530
22531   Returns a tree representation of the expression.  */
22532
22533 static tree
22534 cp_parser_objc_expression (cp_parser* parser)
22535 {
22536   /* Try to figure out what kind of declaration is present.  */
22537   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
22538
22539   switch (kwd->type)
22540     {
22541     case CPP_OPEN_SQUARE:
22542       return cp_parser_objc_message_expression (parser);
22543
22544     case CPP_OBJC_STRING:
22545       kwd = cp_lexer_consume_token (parser->lexer);
22546       return objc_build_string_object (kwd->u.value);
22547
22548     case CPP_KEYWORD:
22549       switch (kwd->keyword)
22550         {
22551         case RID_AT_ENCODE:
22552           return cp_parser_objc_encode_expression (parser);
22553
22554         case RID_AT_PROTOCOL:
22555           return cp_parser_objc_protocol_expression (parser);
22556
22557         case RID_AT_SELECTOR:
22558           return cp_parser_objc_selector_expression (parser);
22559
22560         default:
22561           break;
22562         }
22563     default:
22564       error_at (kwd->location,
22565                 "misplaced %<@%D%> Objective-C++ construct",
22566                 kwd->u.value);
22567       cp_parser_skip_to_end_of_block_or_statement (parser);
22568     }
22569
22570   return error_mark_node;
22571 }
22572
22573 /* Parse an Objective-C message expression.
22574
22575    objc-message-expression:
22576      [ objc-message-receiver objc-message-args ]
22577
22578    Returns a representation of an Objective-C message.  */
22579
22580 static tree
22581 cp_parser_objc_message_expression (cp_parser* parser)
22582 {
22583   tree receiver, messageargs;
22584
22585   cp_lexer_consume_token (parser->lexer);  /* Eat '['.  */
22586   receiver = cp_parser_objc_message_receiver (parser);
22587   messageargs = cp_parser_objc_message_args (parser);
22588   cp_parser_require (parser, CPP_CLOSE_SQUARE, RT_CLOSE_SQUARE);
22589
22590   return objc_build_message_expr (receiver, messageargs);
22591 }
22592
22593 /* Parse an objc-message-receiver.
22594
22595    objc-message-receiver:
22596      expression
22597      simple-type-specifier
22598
22599   Returns a representation of the type or expression.  */
22600
22601 static tree
22602 cp_parser_objc_message_receiver (cp_parser* parser)
22603 {
22604   tree rcv;
22605
22606   /* An Objective-C message receiver may be either (1) a type
22607      or (2) an expression.  */
22608   cp_parser_parse_tentatively (parser);
22609   rcv = cp_parser_expression (parser, false, NULL);
22610
22611   if (cp_parser_parse_definitely (parser))
22612     return rcv;
22613
22614   rcv = cp_parser_simple_type_specifier (parser,
22615                                          /*decl_specs=*/NULL,
22616                                          CP_PARSER_FLAGS_NONE);
22617
22618   return objc_get_class_reference (rcv);
22619 }
22620
22621 /* Parse the arguments and selectors comprising an Objective-C message.
22622
22623    objc-message-args:
22624      objc-selector
22625      objc-selector-args
22626      objc-selector-args , objc-comma-args
22627
22628    objc-selector-args:
22629      objc-selector [opt] : assignment-expression
22630      objc-selector-args objc-selector [opt] : assignment-expression
22631
22632    objc-comma-args:
22633      assignment-expression
22634      objc-comma-args , assignment-expression
22635
22636    Returns a TREE_LIST, with TREE_PURPOSE containing a list of
22637    selector arguments and TREE_VALUE containing a list of comma
22638    arguments.  */
22639
22640 static tree
22641 cp_parser_objc_message_args (cp_parser* parser)
22642 {
22643   tree sel_args = NULL_TREE, addl_args = NULL_TREE;
22644   bool maybe_unary_selector_p = true;
22645   cp_token *token = cp_lexer_peek_token (parser->lexer);
22646
22647   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
22648     {
22649       tree selector = NULL_TREE, arg;
22650
22651       if (token->type != CPP_COLON)
22652         selector = cp_parser_objc_selector (parser);
22653
22654       /* Detect if we have a unary selector.  */
22655       if (maybe_unary_selector_p
22656           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
22657         return build_tree_list (selector, NULL_TREE);
22658
22659       maybe_unary_selector_p = false;
22660       cp_parser_require (parser, CPP_COLON, RT_COLON);
22661       arg = cp_parser_assignment_expression (parser, false, NULL);
22662
22663       sel_args
22664         = chainon (sel_args,
22665                    build_tree_list (selector, arg));
22666
22667       token = cp_lexer_peek_token (parser->lexer);
22668     }
22669
22670   /* Handle non-selector arguments, if any. */
22671   while (token->type == CPP_COMMA)
22672     {
22673       tree arg;
22674
22675       cp_lexer_consume_token (parser->lexer);
22676       arg = cp_parser_assignment_expression (parser, false, NULL);
22677
22678       addl_args
22679         = chainon (addl_args,
22680                    build_tree_list (NULL_TREE, arg));
22681
22682       token = cp_lexer_peek_token (parser->lexer);
22683     }
22684
22685   if (sel_args == NULL_TREE && addl_args == NULL_TREE)
22686     {
22687       cp_parser_error (parser, "objective-c++ message argument(s) are expected");
22688       return build_tree_list (error_mark_node, error_mark_node);
22689     }
22690
22691   return build_tree_list (sel_args, addl_args);
22692 }
22693
22694 /* Parse an Objective-C encode expression.
22695
22696    objc-encode-expression:
22697      @encode objc-typename
22698
22699    Returns an encoded representation of the type argument.  */
22700
22701 static tree
22702 cp_parser_objc_encode_expression (cp_parser* parser)
22703 {
22704   tree type;
22705   cp_token *token;
22706
22707   cp_lexer_consume_token (parser->lexer);  /* Eat '@encode'.  */
22708   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22709   token = cp_lexer_peek_token (parser->lexer);
22710   type = complete_type (cp_parser_type_id (parser));
22711   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22712
22713   if (!type)
22714     {
22715       error_at (token->location, 
22716                 "%<@encode%> must specify a type as an argument");
22717       return error_mark_node;
22718     }
22719
22720   /* This happens if we find @encode(T) (where T is a template
22721      typename or something dependent on a template typename) when
22722      parsing a template.  In that case, we can't compile it
22723      immediately, but we rather create an AT_ENCODE_EXPR which will
22724      need to be instantiated when the template is used.
22725   */
22726   if (dependent_type_p (type))
22727     {
22728       tree value = build_min (AT_ENCODE_EXPR, size_type_node, type);
22729       TREE_READONLY (value) = 1;
22730       return value;
22731     }
22732
22733   return objc_build_encode_expr (type);
22734 }
22735
22736 /* Parse an Objective-C @defs expression.  */
22737
22738 static tree
22739 cp_parser_objc_defs_expression (cp_parser *parser)
22740 {
22741   tree name;
22742
22743   cp_lexer_consume_token (parser->lexer);  /* Eat '@defs'.  */
22744   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22745   name = cp_parser_identifier (parser);
22746   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22747
22748   return objc_get_class_ivars (name);
22749 }
22750
22751 /* Parse an Objective-C protocol expression.
22752
22753   objc-protocol-expression:
22754     @protocol ( identifier )
22755
22756   Returns a representation of the protocol expression.  */
22757
22758 static tree
22759 cp_parser_objc_protocol_expression (cp_parser* parser)
22760 {
22761   tree proto;
22762
22763   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
22764   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22765   proto = cp_parser_identifier (parser);
22766   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22767
22768   return objc_build_protocol_expr (proto);
22769 }
22770
22771 /* Parse an Objective-C selector expression.
22772
22773    objc-selector-expression:
22774      @selector ( objc-method-signature )
22775
22776    objc-method-signature:
22777      objc-selector
22778      objc-selector-seq
22779
22780    objc-selector-seq:
22781      objc-selector :
22782      objc-selector-seq objc-selector :
22783
22784   Returns a representation of the method selector.  */
22785
22786 static tree
22787 cp_parser_objc_selector_expression (cp_parser* parser)
22788 {
22789   tree sel_seq = NULL_TREE;
22790   bool maybe_unary_selector_p = true;
22791   cp_token *token;
22792   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
22793
22794   cp_lexer_consume_token (parser->lexer);  /* Eat '@selector'.  */
22795   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
22796   token = cp_lexer_peek_token (parser->lexer);
22797
22798   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON
22799          || token->type == CPP_SCOPE)
22800     {
22801       tree selector = NULL_TREE;
22802
22803       if (token->type != CPP_COLON
22804           || token->type == CPP_SCOPE)
22805         selector = cp_parser_objc_selector (parser);
22806
22807       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON)
22808           && cp_lexer_next_token_is_not (parser->lexer, CPP_SCOPE))
22809         {
22810           /* Detect if we have a unary selector.  */
22811           if (maybe_unary_selector_p)
22812             {
22813               sel_seq = selector;
22814               goto finish_selector;
22815             }
22816           else
22817             {
22818               cp_parser_error (parser, "expected %<:%>");
22819             }
22820         }
22821       maybe_unary_selector_p = false;
22822       token = cp_lexer_consume_token (parser->lexer);
22823
22824       if (token->type == CPP_SCOPE)
22825         {
22826           sel_seq
22827             = chainon (sel_seq,
22828                        build_tree_list (selector, NULL_TREE));
22829           sel_seq
22830             = chainon (sel_seq,
22831                        build_tree_list (NULL_TREE, NULL_TREE));
22832         }
22833       else
22834         sel_seq
22835           = chainon (sel_seq,
22836                      build_tree_list (selector, NULL_TREE));
22837
22838       token = cp_lexer_peek_token (parser->lexer);
22839     }
22840
22841  finish_selector:
22842   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
22843
22844   return objc_build_selector_expr (loc, sel_seq);
22845 }
22846
22847 /* Parse a list of identifiers.
22848
22849    objc-identifier-list:
22850      identifier
22851      objc-identifier-list , identifier
22852
22853    Returns a TREE_LIST of identifier nodes.  */
22854
22855 static tree
22856 cp_parser_objc_identifier_list (cp_parser* parser)
22857 {
22858   tree identifier;
22859   tree list;
22860   cp_token *sep;
22861
22862   identifier = cp_parser_identifier (parser);
22863   if (identifier == error_mark_node)
22864     return error_mark_node;      
22865
22866   list = build_tree_list (NULL_TREE, identifier);
22867   sep = cp_lexer_peek_token (parser->lexer);
22868
22869   while (sep->type == CPP_COMMA)
22870     {
22871       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
22872       identifier = cp_parser_identifier (parser);
22873       if (identifier == error_mark_node)
22874         return list;
22875
22876       list = chainon (list, build_tree_list (NULL_TREE,
22877                                              identifier));
22878       sep = cp_lexer_peek_token (parser->lexer);
22879     }
22880   
22881   return list;
22882 }
22883
22884 /* Parse an Objective-C alias declaration.
22885
22886    objc-alias-declaration:
22887      @compatibility_alias identifier identifier ;
22888
22889    This function registers the alias mapping with the Objective-C front end.
22890    It returns nothing.  */
22891
22892 static void
22893 cp_parser_objc_alias_declaration (cp_parser* parser)
22894 {
22895   tree alias, orig;
22896
22897   cp_lexer_consume_token (parser->lexer);  /* Eat '@compatibility_alias'.  */
22898   alias = cp_parser_identifier (parser);
22899   orig = cp_parser_identifier (parser);
22900   objc_declare_alias (alias, orig);
22901   cp_parser_consume_semicolon_at_end_of_statement (parser);
22902 }
22903
22904 /* Parse an Objective-C class forward-declaration.
22905
22906    objc-class-declaration:
22907      @class objc-identifier-list ;
22908
22909    The function registers the forward declarations with the Objective-C
22910    front end.  It returns nothing.  */
22911
22912 static void
22913 cp_parser_objc_class_declaration (cp_parser* parser)
22914 {
22915   cp_lexer_consume_token (parser->lexer);  /* Eat '@class'.  */
22916   while (true)
22917     {
22918       tree id;
22919       
22920       id = cp_parser_identifier (parser);
22921       if (id == error_mark_node)
22922         break;
22923       
22924       objc_declare_class (id);
22925
22926       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
22927         cp_lexer_consume_token (parser->lexer);
22928       else
22929         break;
22930     }
22931   cp_parser_consume_semicolon_at_end_of_statement (parser);
22932 }
22933
22934 /* Parse a list of Objective-C protocol references.
22935
22936    objc-protocol-refs-opt:
22937      objc-protocol-refs [opt]
22938
22939    objc-protocol-refs:
22940      < objc-identifier-list >
22941
22942    Returns a TREE_LIST of identifiers, if any.  */
22943
22944 static tree
22945 cp_parser_objc_protocol_refs_opt (cp_parser* parser)
22946 {
22947   tree protorefs = NULL_TREE;
22948
22949   if(cp_lexer_next_token_is (parser->lexer, CPP_LESS))
22950     {
22951       cp_lexer_consume_token (parser->lexer);  /* Eat '<'.  */
22952       protorefs = cp_parser_objc_identifier_list (parser);
22953       cp_parser_require (parser, CPP_GREATER, RT_GREATER);
22954     }
22955
22956   return protorefs;
22957 }
22958
22959 /* Parse a Objective-C visibility specification.  */
22960
22961 static void
22962 cp_parser_objc_visibility_spec (cp_parser* parser)
22963 {
22964   cp_token *vis = cp_lexer_peek_token (parser->lexer);
22965
22966   switch (vis->keyword)
22967     {
22968     case RID_AT_PRIVATE:
22969       objc_set_visibility (OBJC_IVAR_VIS_PRIVATE);
22970       break;
22971     case RID_AT_PROTECTED:
22972       objc_set_visibility (OBJC_IVAR_VIS_PROTECTED);
22973       break;
22974     case RID_AT_PUBLIC:
22975       objc_set_visibility (OBJC_IVAR_VIS_PUBLIC);
22976       break;
22977     case RID_AT_PACKAGE:
22978       objc_set_visibility (OBJC_IVAR_VIS_PACKAGE);
22979       break;
22980     default:
22981       return;
22982     }
22983
22984   /* Eat '@private'/'@protected'/'@public'.  */
22985   cp_lexer_consume_token (parser->lexer);
22986 }
22987
22988 /* Parse an Objective-C method type.  Return 'true' if it is a class
22989    (+) method, and 'false' if it is an instance (-) method.  */
22990
22991 static inline bool
22992 cp_parser_objc_method_type (cp_parser* parser)
22993 {
22994   if (cp_lexer_consume_token (parser->lexer)->type == CPP_PLUS)
22995     return true;
22996   else
22997     return false;
22998 }
22999
23000 /* Parse an Objective-C protocol qualifier.  */
23001
23002 static tree
23003 cp_parser_objc_protocol_qualifiers (cp_parser* parser)
23004 {
23005   tree quals = NULL_TREE, node;
23006   cp_token *token = cp_lexer_peek_token (parser->lexer);
23007
23008   node = token->u.value;
23009
23010   while (node && TREE_CODE (node) == IDENTIFIER_NODE
23011          && (node == ridpointers [(int) RID_IN]
23012              || node == ridpointers [(int) RID_OUT]
23013              || node == ridpointers [(int) RID_INOUT]
23014              || node == ridpointers [(int) RID_BYCOPY]
23015              || node == ridpointers [(int) RID_BYREF]
23016              || node == ridpointers [(int) RID_ONEWAY]))
23017     {
23018       quals = tree_cons (NULL_TREE, node, quals);
23019       cp_lexer_consume_token (parser->lexer);
23020       token = cp_lexer_peek_token (parser->lexer);
23021       node = token->u.value;
23022     }
23023
23024   return quals;
23025 }
23026
23027 /* Parse an Objective-C typename.  */
23028
23029 static tree
23030 cp_parser_objc_typename (cp_parser* parser)
23031 {
23032   tree type_name = NULL_TREE;
23033
23034   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
23035     {
23036       tree proto_quals, cp_type = NULL_TREE;
23037
23038       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23039       proto_quals = cp_parser_objc_protocol_qualifiers (parser);
23040
23041       /* An ObjC type name may consist of just protocol qualifiers, in which
23042          case the type shall default to 'id'.  */
23043       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
23044         {
23045           cp_type = cp_parser_type_id (parser);
23046           
23047           /* If the type could not be parsed, an error has already
23048              been produced.  For error recovery, behave as if it had
23049              not been specified, which will use the default type
23050              'id'.  */
23051           if (cp_type == error_mark_node)
23052             {
23053               cp_type = NULL_TREE;
23054               /* We need to skip to the closing parenthesis as
23055                  cp_parser_type_id() does not seem to do it for
23056                  us.  */
23057               cp_parser_skip_to_closing_parenthesis (parser,
23058                                                      /*recovering=*/true,
23059                                                      /*or_comma=*/false,
23060                                                      /*consume_paren=*/false);
23061             }
23062         }
23063
23064       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23065       type_name = build_tree_list (proto_quals, cp_type);
23066     }
23067
23068   return type_name;
23069 }
23070
23071 /* Check to see if TYPE refers to an Objective-C selector name.  */
23072
23073 static bool
23074 cp_parser_objc_selector_p (enum cpp_ttype type)
23075 {
23076   return (type == CPP_NAME || type == CPP_KEYWORD
23077           || type == CPP_AND_AND || type == CPP_AND_EQ || type == CPP_AND
23078           || type == CPP_OR || type == CPP_COMPL || type == CPP_NOT
23079           || type == CPP_NOT_EQ || type == CPP_OR_OR || type == CPP_OR_EQ
23080           || type == CPP_XOR || type == CPP_XOR_EQ);
23081 }
23082
23083 /* Parse an Objective-C selector.  */
23084
23085 static tree
23086 cp_parser_objc_selector (cp_parser* parser)
23087 {
23088   cp_token *token = cp_lexer_consume_token (parser->lexer);
23089
23090   if (!cp_parser_objc_selector_p (token->type))
23091     {
23092       error_at (token->location, "invalid Objective-C++ selector name");
23093       return error_mark_node;
23094     }
23095
23096   /* C++ operator names are allowed to appear in ObjC selectors.  */
23097   switch (token->type)
23098     {
23099     case CPP_AND_AND: return get_identifier ("and");
23100     case CPP_AND_EQ: return get_identifier ("and_eq");
23101     case CPP_AND: return get_identifier ("bitand");
23102     case CPP_OR: return get_identifier ("bitor");
23103     case CPP_COMPL: return get_identifier ("compl");
23104     case CPP_NOT: return get_identifier ("not");
23105     case CPP_NOT_EQ: return get_identifier ("not_eq");
23106     case CPP_OR_OR: return get_identifier ("or");
23107     case CPP_OR_EQ: return get_identifier ("or_eq");
23108     case CPP_XOR: return get_identifier ("xor");
23109     case CPP_XOR_EQ: return get_identifier ("xor_eq");
23110     default: return token->u.value;
23111     }
23112 }
23113
23114 /* Parse an Objective-C params list.  */
23115
23116 static tree
23117 cp_parser_objc_method_keyword_params (cp_parser* parser, tree* attributes)
23118 {
23119   tree params = NULL_TREE;
23120   bool maybe_unary_selector_p = true;
23121   cp_token *token = cp_lexer_peek_token (parser->lexer);
23122
23123   while (cp_parser_objc_selector_p (token->type) || token->type == CPP_COLON)
23124     {
23125       tree selector = NULL_TREE, type_name, identifier;
23126       tree parm_attr = NULL_TREE;
23127
23128       if (token->keyword == RID_ATTRIBUTE)
23129         break;
23130
23131       if (token->type != CPP_COLON)
23132         selector = cp_parser_objc_selector (parser);
23133
23134       /* Detect if we have a unary selector.  */
23135       if (maybe_unary_selector_p
23136           && cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
23137         {
23138           params = selector; /* Might be followed by attributes.  */
23139           break;
23140         }
23141
23142       maybe_unary_selector_p = false;
23143       if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
23144         {
23145           /* Something went quite wrong.  There should be a colon
23146              here, but there is not.  Stop parsing parameters.  */
23147           break;
23148         }
23149       type_name = cp_parser_objc_typename (parser);
23150       /* New ObjC allows attributes on parameters too.  */
23151       if (cp_lexer_next_token_is_keyword (parser->lexer, RID_ATTRIBUTE))
23152         parm_attr = cp_parser_attributes_opt (parser);
23153       identifier = cp_parser_identifier (parser);
23154
23155       params
23156         = chainon (params,
23157                    objc_build_keyword_decl (selector,
23158                                             type_name,
23159                                             identifier,
23160                                             parm_attr));
23161
23162       token = cp_lexer_peek_token (parser->lexer);
23163     }
23164
23165   if (params == NULL_TREE)
23166     {
23167       cp_parser_error (parser, "objective-c++ method declaration is expected");
23168       return error_mark_node;
23169     }
23170
23171   /* We allow tail attributes for the method.  */
23172   if (token->keyword == RID_ATTRIBUTE)
23173     {
23174       *attributes = cp_parser_attributes_opt (parser);
23175       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23176           || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23177         return params;
23178       cp_parser_error (parser, 
23179                        "method attributes must be specified at the end");
23180       return error_mark_node;
23181     }
23182
23183   if (params == NULL_TREE)
23184     {
23185       cp_parser_error (parser, "objective-c++ method declaration is expected");
23186       return error_mark_node;
23187     }
23188   return params;
23189 }
23190
23191 /* Parse the non-keyword Objective-C params.  */
23192
23193 static tree
23194 cp_parser_objc_method_tail_params_opt (cp_parser* parser, bool *ellipsisp, 
23195                                        tree* attributes)
23196 {
23197   tree params = make_node (TREE_LIST);
23198   cp_token *token = cp_lexer_peek_token (parser->lexer);
23199   *ellipsisp = false;  /* Initially, assume no ellipsis.  */
23200
23201   while (token->type == CPP_COMMA)
23202     {
23203       cp_parameter_declarator *parmdecl;
23204       tree parm;
23205
23206       cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23207       token = cp_lexer_peek_token (parser->lexer);
23208
23209       if (token->type == CPP_ELLIPSIS)
23210         {
23211           cp_lexer_consume_token (parser->lexer);  /* Eat '...'.  */
23212           *ellipsisp = true;
23213           token = cp_lexer_peek_token (parser->lexer);
23214           break;
23215         }
23216
23217       /* TODO: parse attributes for tail parameters.  */
23218       parmdecl = cp_parser_parameter_declaration (parser, false, NULL);
23219       parm = grokdeclarator (parmdecl->declarator,
23220                              &parmdecl->decl_specifiers,
23221                              PARM, /*initialized=*/0,
23222                              /*attrlist=*/NULL);
23223
23224       chainon (params, build_tree_list (NULL_TREE, parm));
23225       token = cp_lexer_peek_token (parser->lexer);
23226     }
23227
23228   /* We allow tail attributes for the method.  */
23229   if (token->keyword == RID_ATTRIBUTE)
23230     {
23231       if (*attributes == NULL_TREE)
23232         {
23233           *attributes = cp_parser_attributes_opt (parser);
23234           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON)
23235               || cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
23236             return params;
23237         }
23238       else        
23239         /* We have an error, but parse the attributes, so that we can 
23240            carry on.  */
23241         *attributes = cp_parser_attributes_opt (parser);
23242
23243       cp_parser_error (parser, 
23244                        "method attributes must be specified at the end");
23245       return error_mark_node;
23246     }
23247
23248   return params;
23249 }
23250
23251 /* Parse a linkage specification, a pragma, an extra semicolon or a block.  */
23252
23253 static void
23254 cp_parser_objc_interstitial_code (cp_parser* parser)
23255 {
23256   cp_token *token = cp_lexer_peek_token (parser->lexer);
23257
23258   /* If the next token is `extern' and the following token is a string
23259      literal, then we have a linkage specification.  */
23260   if (token->keyword == RID_EXTERN
23261       && cp_parser_is_pure_string_literal
23262          (cp_lexer_peek_nth_token (parser->lexer, 2)))
23263     cp_parser_linkage_specification (parser);
23264   /* Handle #pragma, if any.  */
23265   else if (token->type == CPP_PRAGMA)
23266     cp_parser_pragma (parser, pragma_external);
23267   /* Allow stray semicolons.  */
23268   else if (token->type == CPP_SEMICOLON)
23269     cp_lexer_consume_token (parser->lexer);
23270   /* Mark methods as optional or required, when building protocols.  */
23271   else if (token->keyword == RID_AT_OPTIONAL)
23272     {
23273       cp_lexer_consume_token (parser->lexer);
23274       objc_set_method_opt (true);
23275     }
23276   else if (token->keyword == RID_AT_REQUIRED)
23277     {
23278       cp_lexer_consume_token (parser->lexer);
23279       objc_set_method_opt (false);
23280     }
23281   else if (token->keyword == RID_NAMESPACE)
23282     cp_parser_namespace_definition (parser);
23283   /* Other stray characters must generate errors.  */
23284   else if (token->type == CPP_OPEN_BRACE || token->type == CPP_CLOSE_BRACE)
23285     {
23286       cp_lexer_consume_token (parser->lexer);
23287       error ("stray %qs between Objective-C++ methods",
23288              token->type == CPP_OPEN_BRACE ? "{" : "}");
23289     }
23290   /* Finally, try to parse a block-declaration, or a function-definition.  */
23291   else
23292     cp_parser_block_declaration (parser, /*statement_p=*/false);
23293 }
23294
23295 /* Parse a method signature.  */
23296
23297 static tree
23298 cp_parser_objc_method_signature (cp_parser* parser, tree* attributes)
23299 {
23300   tree rettype, kwdparms, optparms;
23301   bool ellipsis = false;
23302   bool is_class_method;
23303
23304   is_class_method = cp_parser_objc_method_type (parser);
23305   rettype = cp_parser_objc_typename (parser);
23306   *attributes = NULL_TREE;
23307   kwdparms = cp_parser_objc_method_keyword_params (parser, attributes);
23308   if (kwdparms == error_mark_node)
23309     return error_mark_node;
23310   optparms = cp_parser_objc_method_tail_params_opt (parser, &ellipsis, attributes);
23311   if (optparms == error_mark_node)
23312     return error_mark_node;
23313
23314   return objc_build_method_signature (is_class_method, rettype, kwdparms, optparms, ellipsis);
23315 }
23316
23317 static bool
23318 cp_parser_objc_method_maybe_bad_prefix_attributes (cp_parser* parser)
23319 {
23320   tree tattr;  
23321   cp_lexer_save_tokens (parser->lexer);
23322   tattr = cp_parser_attributes_opt (parser);
23323   gcc_assert (tattr) ;
23324   
23325   /* If the attributes are followed by a method introducer, this is not allowed.
23326      Dump the attributes and flag the situation.  */
23327   if (cp_lexer_next_token_is (parser->lexer, CPP_PLUS)
23328       || cp_lexer_next_token_is (parser->lexer, CPP_MINUS))
23329     return true;
23330
23331   /* Otherwise, the attributes introduce some interstitial code, possibly so
23332      rewind to allow that check.  */
23333   cp_lexer_rollback_tokens (parser->lexer);
23334   return false;  
23335 }
23336
23337 /* Parse an Objective-C method prototype list.  */
23338
23339 static void
23340 cp_parser_objc_method_prototype_list (cp_parser* parser)
23341 {
23342   cp_token *token = cp_lexer_peek_token (parser->lexer);
23343
23344   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23345     {
23346       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23347         {
23348           tree attributes, sig;
23349           bool is_class_method;
23350           if (token->type == CPP_PLUS)
23351             is_class_method = true;
23352           else
23353             is_class_method = false;
23354           sig = cp_parser_objc_method_signature (parser, &attributes);
23355           if (sig == error_mark_node)
23356             {
23357               cp_parser_skip_to_end_of_block_or_statement (parser);
23358               token = cp_lexer_peek_token (parser->lexer);
23359               continue;
23360             }
23361           objc_add_method_declaration (is_class_method, sig, attributes);
23362           cp_parser_consume_semicolon_at_end_of_statement (parser);
23363         }
23364       else if (token->keyword == RID_AT_PROPERTY)
23365         cp_parser_objc_at_property_declaration (parser);
23366       else if (token->keyword == RID_ATTRIBUTE 
23367                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23368         warning_at (cp_lexer_peek_token (parser->lexer)->location, 
23369                     OPT_Wattributes, 
23370                     "prefix attributes are ignored for methods");
23371       else
23372         /* Allow for interspersed non-ObjC++ code.  */
23373         cp_parser_objc_interstitial_code (parser);
23374
23375       token = cp_lexer_peek_token (parser->lexer);
23376     }
23377
23378   if (token->type != CPP_EOF)
23379     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23380   else
23381     cp_parser_error (parser, "expected %<@end%>");
23382
23383   objc_finish_interface ();
23384 }
23385
23386 /* Parse an Objective-C method definition list.  */
23387
23388 static void
23389 cp_parser_objc_method_definition_list (cp_parser* parser)
23390 {
23391   cp_token *token = cp_lexer_peek_token (parser->lexer);
23392
23393   while (token->keyword != RID_AT_END && token->type != CPP_EOF)
23394     {
23395       tree meth;
23396
23397       if (token->type == CPP_PLUS || token->type == CPP_MINUS)
23398         {
23399           cp_token *ptk;
23400           tree sig, attribute;
23401           bool is_class_method;
23402           if (token->type == CPP_PLUS)
23403             is_class_method = true;
23404           else
23405             is_class_method = false;
23406           push_deferring_access_checks (dk_deferred);
23407           sig = cp_parser_objc_method_signature (parser, &attribute);
23408           if (sig == error_mark_node)
23409             {
23410               cp_parser_skip_to_end_of_block_or_statement (parser);
23411               token = cp_lexer_peek_token (parser->lexer);
23412               continue;
23413             }
23414           objc_start_method_definition (is_class_method, sig, attribute,
23415                                         NULL_TREE);
23416
23417           /* For historical reasons, we accept an optional semicolon.  */
23418           if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23419             cp_lexer_consume_token (parser->lexer);
23420
23421           ptk = cp_lexer_peek_token (parser->lexer);
23422           if (!(ptk->type == CPP_PLUS || ptk->type == CPP_MINUS 
23423                 || ptk->type == CPP_EOF || ptk->keyword == RID_AT_END))
23424             {
23425               perform_deferred_access_checks ();
23426               stop_deferring_access_checks ();
23427               meth = cp_parser_function_definition_after_declarator (parser,
23428                                                                      false);
23429               pop_deferring_access_checks ();
23430               objc_finish_method_definition (meth);
23431             }
23432         }
23433       /* The following case will be removed once @synthesize is
23434          completely implemented.  */
23435       else if (token->keyword == RID_AT_PROPERTY)
23436         cp_parser_objc_at_property_declaration (parser);
23437       else if (token->keyword == RID_AT_SYNTHESIZE)
23438         cp_parser_objc_at_synthesize_declaration (parser);
23439       else if (token->keyword == RID_AT_DYNAMIC)
23440         cp_parser_objc_at_dynamic_declaration (parser);
23441       else if (token->keyword == RID_ATTRIBUTE 
23442                && cp_parser_objc_method_maybe_bad_prefix_attributes(parser))
23443         warning_at (token->location, OPT_Wattributes,
23444                     "prefix attributes are ignored for methods");
23445       else
23446         /* Allow for interspersed non-ObjC++ code.  */
23447         cp_parser_objc_interstitial_code (parser);
23448
23449       token = cp_lexer_peek_token (parser->lexer);
23450     }
23451
23452   if (token->type != CPP_EOF)
23453     cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23454   else
23455     cp_parser_error (parser, "expected %<@end%>");
23456
23457   objc_finish_implementation ();
23458 }
23459
23460 /* Parse Objective-C ivars.  */
23461
23462 static void
23463 cp_parser_objc_class_ivars (cp_parser* parser)
23464 {
23465   cp_token *token = cp_lexer_peek_token (parser->lexer);
23466
23467   if (token->type != CPP_OPEN_BRACE)
23468     return;     /* No ivars specified.  */
23469
23470   cp_lexer_consume_token (parser->lexer);  /* Eat '{'.  */
23471   token = cp_lexer_peek_token (parser->lexer);
23472
23473   while (token->type != CPP_CLOSE_BRACE 
23474         && token->keyword != RID_AT_END && token->type != CPP_EOF)
23475     {
23476       cp_decl_specifier_seq declspecs;
23477       int decl_class_or_enum_p;
23478       tree prefix_attributes;
23479
23480       cp_parser_objc_visibility_spec (parser);
23481
23482       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
23483         break;
23484
23485       cp_parser_decl_specifier_seq (parser,
23486                                     CP_PARSER_FLAGS_OPTIONAL,
23487                                     &declspecs,
23488                                     &decl_class_or_enum_p);
23489
23490       /* auto, register, static, extern, mutable.  */
23491       if (declspecs.storage_class != sc_none)
23492         {
23493           cp_parser_error (parser, "invalid type for instance variable");         
23494           declspecs.storage_class = sc_none;
23495         }
23496
23497       /* __thread.  */
23498       if (declspecs.specs[(int) ds_thread])
23499         {
23500           cp_parser_error (parser, "invalid type for instance variable");
23501           declspecs.specs[(int) ds_thread] = 0;
23502         }
23503       
23504       /* typedef.  */
23505       if (declspecs.specs[(int) ds_typedef])
23506         {
23507           cp_parser_error (parser, "invalid type for instance variable");
23508           declspecs.specs[(int) ds_typedef] = 0;
23509         }
23510
23511       prefix_attributes = declspecs.attributes;
23512       declspecs.attributes = NULL_TREE;
23513
23514       /* Keep going until we hit the `;' at the end of the
23515          declaration.  */
23516       while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23517         {
23518           tree width = NULL_TREE, attributes, first_attribute, decl;
23519           cp_declarator *declarator = NULL;
23520           int ctor_dtor_or_conv_p;
23521
23522           /* Check for a (possibly unnamed) bitfield declaration.  */
23523           token = cp_lexer_peek_token (parser->lexer);
23524           if (token->type == CPP_COLON)
23525             goto eat_colon;
23526
23527           if (token->type == CPP_NAME
23528               && (cp_lexer_peek_nth_token (parser->lexer, 2)->type
23529                   == CPP_COLON))
23530             {
23531               /* Get the name of the bitfield.  */
23532               declarator = make_id_declarator (NULL_TREE,
23533                                                cp_parser_identifier (parser),
23534                                                sfk_none);
23535
23536              eat_colon:
23537               cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23538               /* Get the width of the bitfield.  */
23539               width
23540                 = cp_parser_constant_expression (parser,
23541                                                  /*allow_non_constant=*/false,
23542                                                  NULL);
23543             }
23544           else
23545             {
23546               /* Parse the declarator.  */
23547               declarator
23548                 = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
23549                                         &ctor_dtor_or_conv_p,
23550                                         /*parenthesized_p=*/NULL,
23551                                         /*member_p=*/false);
23552             }
23553
23554           /* Look for attributes that apply to the ivar.  */
23555           attributes = cp_parser_attributes_opt (parser);
23556           /* Remember which attributes are prefix attributes and
23557              which are not.  */
23558           first_attribute = attributes;
23559           /* Combine the attributes.  */
23560           attributes = chainon (prefix_attributes, attributes);
23561
23562           if (width)
23563               /* Create the bitfield declaration.  */
23564               decl = grokbitfield (declarator, &declspecs,
23565                                    width,
23566                                    attributes);
23567           else
23568             decl = grokfield (declarator, &declspecs,
23569                               NULL_TREE, /*init_const_expr_p=*/false,
23570                               NULL_TREE, attributes);
23571
23572           /* Add the instance variable.  */
23573           if (decl != error_mark_node && decl != NULL_TREE)
23574             objc_add_instance_variable (decl);
23575
23576           /* Reset PREFIX_ATTRIBUTES.  */
23577           while (attributes && TREE_CHAIN (attributes) != first_attribute)
23578             attributes = TREE_CHAIN (attributes);
23579           if (attributes)
23580             TREE_CHAIN (attributes) = NULL_TREE;
23581
23582           token = cp_lexer_peek_token (parser->lexer);
23583
23584           if (token->type == CPP_COMMA)
23585             {
23586               cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
23587               continue;
23588             }
23589           break;
23590         }
23591
23592       cp_parser_consume_semicolon_at_end_of_statement (parser);
23593       token = cp_lexer_peek_token (parser->lexer);
23594     }
23595
23596   if (token->keyword == RID_AT_END)
23597     cp_parser_error (parser, "expected %<}%>");
23598
23599   /* Do not consume the RID_AT_END, so it will be read again as terminating
23600      the @interface of @implementation.  */ 
23601   if (token->keyword != RID_AT_END && token->type != CPP_EOF)
23602     cp_lexer_consume_token (parser->lexer);  /* Eat '}'.  */
23603     
23604   /* For historical reasons, we accept an optional semicolon.  */
23605   if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
23606     cp_lexer_consume_token (parser->lexer);
23607 }
23608
23609 /* Parse an Objective-C protocol declaration.  */
23610
23611 static void
23612 cp_parser_objc_protocol_declaration (cp_parser* parser, tree attributes)
23613 {
23614   tree proto, protorefs;
23615   cp_token *tok;
23616
23617   cp_lexer_consume_token (parser->lexer);  /* Eat '@protocol'.  */
23618   if (cp_lexer_next_token_is_not (parser->lexer, CPP_NAME))
23619     {
23620       tok = cp_lexer_peek_token (parser->lexer);
23621       error_at (tok->location, "identifier expected after %<@protocol%>");
23622       cp_parser_consume_semicolon_at_end_of_statement (parser);
23623       return;
23624     }
23625
23626   /* See if we have a forward declaration or a definition.  */
23627   tok = cp_lexer_peek_nth_token (parser->lexer, 2);
23628
23629   /* Try a forward declaration first.  */
23630   if (tok->type == CPP_COMMA || tok->type == CPP_SEMICOLON)
23631     {
23632       while (true)
23633         {
23634           tree id;
23635           
23636           id = cp_parser_identifier (parser);
23637           if (id == error_mark_node)
23638             break;
23639           
23640           objc_declare_protocol (id, attributes);
23641           
23642           if(cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
23643             cp_lexer_consume_token (parser->lexer);
23644           else
23645             break;
23646         }
23647       cp_parser_consume_semicolon_at_end_of_statement (parser);
23648     }
23649
23650   /* Ok, we got a full-fledged definition (or at least should).  */
23651   else
23652     {
23653       proto = cp_parser_identifier (parser);
23654       protorefs = cp_parser_objc_protocol_refs_opt (parser);
23655       objc_start_protocol (proto, protorefs, attributes);
23656       cp_parser_objc_method_prototype_list (parser);
23657     }
23658 }
23659
23660 /* Parse an Objective-C superclass or category.  */
23661
23662 static void
23663 cp_parser_objc_superclass_or_category (cp_parser *parser, 
23664                                        bool iface_p,
23665                                        tree *super,
23666                                        tree *categ, bool *is_class_extension)
23667 {
23668   cp_token *next = cp_lexer_peek_token (parser->lexer);
23669
23670   *super = *categ = NULL_TREE;
23671   *is_class_extension = false;
23672   if (next->type == CPP_COLON)
23673     {
23674       cp_lexer_consume_token (parser->lexer);  /* Eat ':'.  */
23675       *super = cp_parser_identifier (parser);
23676     }
23677   else if (next->type == CPP_OPEN_PAREN)
23678     {
23679       cp_lexer_consume_token (parser->lexer);  /* Eat '('.  */
23680
23681       /* If there is no category name, and this is an @interface, we
23682          have a class extension.  */
23683       if (iface_p && cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23684         {
23685           *categ = NULL_TREE;
23686           *is_class_extension = true;
23687         }
23688       else
23689         *categ = cp_parser_identifier (parser);
23690
23691       cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23692     }
23693 }
23694
23695 /* Parse an Objective-C class interface.  */
23696
23697 static void
23698 cp_parser_objc_class_interface (cp_parser* parser, tree attributes)
23699 {
23700   tree name, super, categ, protos;
23701   bool is_class_extension;
23702
23703   cp_lexer_consume_token (parser->lexer);  /* Eat '@interface'.  */
23704   name = cp_parser_identifier (parser);
23705   if (name == error_mark_node)
23706     {
23707       /* It's hard to recover because even if valid @interface stuff
23708          is to follow, we can't compile it (or validate it) if we
23709          don't even know which class it refers to.  Let's assume this
23710          was a stray '@interface' token in the stream and skip it.
23711       */
23712       return;
23713     }
23714   cp_parser_objc_superclass_or_category (parser, true, &super, &categ,
23715                                          &is_class_extension);
23716   protos = cp_parser_objc_protocol_refs_opt (parser);
23717
23718   /* We have either a class or a category on our hands.  */
23719   if (categ || is_class_extension)
23720     objc_start_category_interface (name, categ, protos, attributes);
23721   else
23722     {
23723       objc_start_class_interface (name, super, protos, attributes);
23724       /* Handle instance variable declarations, if any.  */
23725       cp_parser_objc_class_ivars (parser);
23726       objc_continue_interface ();
23727     }
23728
23729   cp_parser_objc_method_prototype_list (parser);
23730 }
23731
23732 /* Parse an Objective-C class implementation.  */
23733
23734 static void
23735 cp_parser_objc_class_implementation (cp_parser* parser)
23736 {
23737   tree name, super, categ;
23738   bool is_class_extension;
23739
23740   cp_lexer_consume_token (parser->lexer);  /* Eat '@implementation'.  */
23741   name = cp_parser_identifier (parser);
23742   if (name == error_mark_node)
23743     {
23744       /* It's hard to recover because even if valid @implementation
23745          stuff is to follow, we can't compile it (or validate it) if
23746          we don't even know which class it refers to.  Let's assume
23747          this was a stray '@implementation' token in the stream and
23748          skip it.
23749       */
23750       return;
23751     }
23752   cp_parser_objc_superclass_or_category (parser, false, &super, &categ,
23753                                          &is_class_extension);
23754
23755   /* We have either a class or a category on our hands.  */
23756   if (categ)
23757     objc_start_category_implementation (name, categ);
23758   else
23759     {
23760       objc_start_class_implementation (name, super);
23761       /* Handle instance variable declarations, if any.  */
23762       cp_parser_objc_class_ivars (parser);
23763       objc_continue_implementation ();
23764     }
23765
23766   cp_parser_objc_method_definition_list (parser);
23767 }
23768
23769 /* Consume the @end token and finish off the implementation.  */
23770
23771 static void
23772 cp_parser_objc_end_implementation (cp_parser* parser)
23773 {
23774   cp_lexer_consume_token (parser->lexer);  /* Eat '@end'.  */
23775   objc_finish_implementation ();
23776 }
23777
23778 /* Parse an Objective-C declaration.  */
23779
23780 static void
23781 cp_parser_objc_declaration (cp_parser* parser, tree attributes)
23782 {
23783   /* Try to figure out what kind of declaration is present.  */
23784   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
23785
23786   if (attributes)
23787     switch (kwd->keyword)
23788       {
23789         case RID_AT_ALIAS:
23790         case RID_AT_CLASS:
23791         case RID_AT_END:
23792           error_at (kwd->location, "attributes may not be specified before"
23793                     " the %<@%D%> Objective-C++ keyword",
23794                     kwd->u.value);
23795           attributes = NULL;
23796           break;
23797         case RID_AT_IMPLEMENTATION:
23798           warning_at (kwd->location, OPT_Wattributes,
23799                       "prefix attributes are ignored before %<@%D%>",
23800                       kwd->u.value);
23801           attributes = NULL;
23802         default:
23803           break;
23804       }
23805
23806   switch (kwd->keyword)
23807     {
23808     case RID_AT_ALIAS:
23809       cp_parser_objc_alias_declaration (parser);
23810       break;
23811     case RID_AT_CLASS:
23812       cp_parser_objc_class_declaration (parser);
23813       break;
23814     case RID_AT_PROTOCOL:
23815       cp_parser_objc_protocol_declaration (parser, attributes);
23816       break;
23817     case RID_AT_INTERFACE:
23818       cp_parser_objc_class_interface (parser, attributes);
23819       break;
23820     case RID_AT_IMPLEMENTATION:
23821       cp_parser_objc_class_implementation (parser);
23822       break;
23823     case RID_AT_END:
23824       cp_parser_objc_end_implementation (parser);
23825       break;
23826     default:
23827       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
23828                 kwd->u.value);
23829       cp_parser_skip_to_end_of_block_or_statement (parser);
23830     }
23831 }
23832
23833 /* Parse an Objective-C try-catch-finally statement.
23834
23835    objc-try-catch-finally-stmt:
23836      @try compound-statement objc-catch-clause-seq [opt]
23837        objc-finally-clause [opt]
23838
23839    objc-catch-clause-seq:
23840      objc-catch-clause objc-catch-clause-seq [opt]
23841
23842    objc-catch-clause:
23843      @catch ( objc-exception-declaration ) compound-statement
23844
23845    objc-finally-clause:
23846      @finally compound-statement
23847
23848    objc-exception-declaration:
23849      parameter-declaration
23850      '...'
23851
23852    where '...' is to be interpreted literally, that is, it means CPP_ELLIPSIS.
23853
23854    Returns NULL_TREE.
23855
23856    PS: This function is identical to c_parser_objc_try_catch_finally_statement
23857    for C.  Keep them in sync.  */   
23858
23859 static tree
23860 cp_parser_objc_try_catch_finally_statement (cp_parser *parser)
23861 {
23862   location_t location;
23863   tree stmt;
23864
23865   cp_parser_require_keyword (parser, RID_AT_TRY, RT_AT_TRY);
23866   location = cp_lexer_peek_token (parser->lexer)->location;
23867   objc_maybe_warn_exceptions (location);
23868   /* NB: The @try block needs to be wrapped in its own STATEMENT_LIST
23869      node, lest it get absorbed into the surrounding block.  */
23870   stmt = push_stmt_list ();
23871   cp_parser_compound_statement (parser, NULL, false, false);
23872   objc_begin_try_stmt (location, pop_stmt_list (stmt));
23873
23874   while (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_CATCH))
23875     {
23876       cp_parameter_declarator *parm;
23877       tree parameter_declaration = error_mark_node;
23878       bool seen_open_paren = false;
23879
23880       cp_lexer_consume_token (parser->lexer);
23881       if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
23882         seen_open_paren = true;
23883       if (cp_lexer_next_token_is (parser->lexer, CPP_ELLIPSIS))
23884         {
23885           /* We have "@catch (...)" (where the '...' are literally
23886              what is in the code).  Skip the '...'.
23887              parameter_declaration is set to NULL_TREE, and
23888              objc_being_catch_clauses() knows that that means
23889              '...'.  */
23890           cp_lexer_consume_token (parser->lexer);
23891           parameter_declaration = NULL_TREE;
23892         }
23893       else
23894         {
23895           /* We have "@catch (NSException *exception)" or something
23896              like that.  Parse the parameter declaration.  */
23897           parm = cp_parser_parameter_declaration (parser, false, NULL);
23898           if (parm == NULL)
23899             parameter_declaration = error_mark_node;
23900           else
23901             parameter_declaration = grokdeclarator (parm->declarator,
23902                                                     &parm->decl_specifiers,
23903                                                     PARM, /*initialized=*/0,
23904                                                     /*attrlist=*/NULL);
23905         }
23906       if (seen_open_paren)
23907         cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23908       else
23909         {
23910           /* If there was no open parenthesis, we are recovering from
23911              an error, and we are trying to figure out what mistake
23912              the user has made.  */
23913
23914           /* If there is an immediate closing parenthesis, the user
23915              probably forgot the opening one (ie, they typed "@catch
23916              NSException *e)".  Parse the closing parenthesis and keep
23917              going.  */
23918           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_PAREN))
23919             cp_lexer_consume_token (parser->lexer);
23920           
23921           /* If these is no immediate closing parenthesis, the user
23922              probably doesn't know that parenthesis are required at
23923              all (ie, they typed "@catch NSException *e").  So, just
23924              forget about the closing parenthesis and keep going.  */
23925         }
23926       objc_begin_catch_clause (parameter_declaration);
23927       cp_parser_compound_statement (parser, NULL, false, false);
23928       objc_finish_catch_clause ();
23929     }
23930   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AT_FINALLY))
23931     {
23932       cp_lexer_consume_token (parser->lexer);
23933       location = cp_lexer_peek_token (parser->lexer)->location;
23934       /* NB: The @finally block needs to be wrapped in its own STATEMENT_LIST
23935          node, lest it get absorbed into the surrounding block.  */
23936       stmt = push_stmt_list ();
23937       cp_parser_compound_statement (parser, NULL, false, false);
23938       objc_build_finally_clause (location, pop_stmt_list (stmt));
23939     }
23940
23941   return objc_finish_try_stmt ();
23942 }
23943
23944 /* Parse an Objective-C synchronized statement.
23945
23946    objc-synchronized-stmt:
23947      @synchronized ( expression ) compound-statement
23948
23949    Returns NULL_TREE.  */
23950
23951 static tree
23952 cp_parser_objc_synchronized_statement (cp_parser *parser)
23953 {
23954   location_t location;
23955   tree lock, stmt;
23956
23957   cp_parser_require_keyword (parser, RID_AT_SYNCHRONIZED, RT_AT_SYNCHRONIZED);
23958
23959   location = cp_lexer_peek_token (parser->lexer)->location;
23960   objc_maybe_warn_exceptions (location);
23961   cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN);
23962   lock = cp_parser_expression (parser, false, NULL);
23963   cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN);
23964
23965   /* NB: The @synchronized block needs to be wrapped in its own STATEMENT_LIST
23966      node, lest it get absorbed into the surrounding block.  */
23967   stmt = push_stmt_list ();
23968   cp_parser_compound_statement (parser, NULL, false, false);
23969
23970   return objc_build_synchronized (location, lock, pop_stmt_list (stmt));
23971 }
23972
23973 /* Parse an Objective-C throw statement.
23974
23975    objc-throw-stmt:
23976      @throw assignment-expression [opt] ;
23977
23978    Returns a constructed '@throw' statement.  */
23979
23980 static tree
23981 cp_parser_objc_throw_statement (cp_parser *parser)
23982 {
23983   tree expr = NULL_TREE;
23984   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
23985
23986   cp_parser_require_keyword (parser, RID_AT_THROW, RT_AT_THROW);
23987
23988   if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
23989     expr = cp_parser_expression (parser, /*cast_p=*/false, NULL);
23990
23991   cp_parser_consume_semicolon_at_end_of_statement (parser);
23992
23993   return objc_build_throw_stmt (loc, expr);
23994 }
23995
23996 /* Parse an Objective-C statement.  */
23997
23998 static tree
23999 cp_parser_objc_statement (cp_parser * parser)
24000 {
24001   /* Try to figure out what kind of declaration is present.  */
24002   cp_token *kwd = cp_lexer_peek_token (parser->lexer);
24003
24004   switch (kwd->keyword)
24005     {
24006     case RID_AT_TRY:
24007       return cp_parser_objc_try_catch_finally_statement (parser);
24008     case RID_AT_SYNCHRONIZED:
24009       return cp_parser_objc_synchronized_statement (parser);
24010     case RID_AT_THROW:
24011       return cp_parser_objc_throw_statement (parser);
24012     default:
24013       error_at (kwd->location, "misplaced %<@%D%> Objective-C++ construct",
24014                kwd->u.value);
24015       cp_parser_skip_to_end_of_block_or_statement (parser);
24016     }
24017
24018   return error_mark_node;
24019 }
24020
24021 /* If we are compiling ObjC++ and we see an __attribute__ we neeed to 
24022    look ahead to see if an objc keyword follows the attributes.  This
24023    is to detect the use of prefix attributes on ObjC @interface and 
24024    @protocol.  */
24025
24026 static bool
24027 cp_parser_objc_valid_prefix_attributes (cp_parser* parser, tree *attrib)
24028 {
24029   cp_lexer_save_tokens (parser->lexer);
24030   *attrib = cp_parser_attributes_opt (parser);
24031   gcc_assert (*attrib);
24032   if (OBJC_IS_AT_KEYWORD (cp_lexer_peek_token (parser->lexer)->keyword))
24033     {
24034       cp_lexer_commit_tokens (parser->lexer);
24035       return true;
24036     }
24037   cp_lexer_rollback_tokens (parser->lexer);
24038   return false;  
24039 }
24040
24041 /* This routine is a minimal replacement for
24042    c_parser_struct_declaration () used when parsing the list of
24043    types/names or ObjC++ properties.  For example, when parsing the
24044    code
24045
24046    @property (readonly) int a, b, c;
24047
24048    this function is responsible for parsing "int a, int b, int c" and
24049    returning the declarations as CHAIN of DECLs.
24050
24051    TODO: Share this code with cp_parser_objc_class_ivars.  It's very
24052    similar parsing.  */
24053 static tree
24054 cp_parser_objc_struct_declaration (cp_parser *parser)
24055 {
24056   tree decls = NULL_TREE;
24057   cp_decl_specifier_seq declspecs;
24058   int decl_class_or_enum_p;
24059   tree prefix_attributes;
24060
24061   cp_parser_decl_specifier_seq (parser,
24062                                 CP_PARSER_FLAGS_NONE,
24063                                 &declspecs,
24064                                 &decl_class_or_enum_p);
24065
24066   if (declspecs.type == error_mark_node)
24067     return error_mark_node;
24068
24069   /* auto, register, static, extern, mutable.  */
24070   if (declspecs.storage_class != sc_none)
24071     {
24072       cp_parser_error (parser, "invalid type for property");
24073       declspecs.storage_class = sc_none;
24074     }
24075   
24076   /* __thread.  */
24077   if (declspecs.specs[(int) ds_thread])
24078     {
24079       cp_parser_error (parser, "invalid type for property");
24080       declspecs.specs[(int) ds_thread] = 0;
24081     }
24082   
24083   /* typedef.  */
24084   if (declspecs.specs[(int) ds_typedef])
24085     {
24086       cp_parser_error (parser, "invalid type for property");
24087       declspecs.specs[(int) ds_typedef] = 0;
24088     }
24089
24090   prefix_attributes = declspecs.attributes;
24091   declspecs.attributes = NULL_TREE;
24092
24093   /* Keep going until we hit the `;' at the end of the declaration. */
24094   while (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
24095     {
24096       tree attributes, first_attribute, decl;
24097       cp_declarator *declarator;
24098       cp_token *token;
24099
24100       /* Parse the declarator.  */
24101       declarator = cp_parser_declarator (parser, CP_PARSER_DECLARATOR_NAMED,
24102                                          NULL, NULL, false);
24103
24104       /* Look for attributes that apply to the ivar.  */
24105       attributes = cp_parser_attributes_opt (parser);
24106       /* Remember which attributes are prefix attributes and
24107          which are not.  */
24108       first_attribute = attributes;
24109       /* Combine the attributes.  */
24110       attributes = chainon (prefix_attributes, attributes);
24111       
24112       decl = grokfield (declarator, &declspecs,
24113                         NULL_TREE, /*init_const_expr_p=*/false,
24114                         NULL_TREE, attributes);
24115
24116       if (decl == error_mark_node || decl == NULL_TREE)
24117         return error_mark_node;
24118       
24119       /* Reset PREFIX_ATTRIBUTES.  */
24120       while (attributes && TREE_CHAIN (attributes) != first_attribute)
24121         attributes = TREE_CHAIN (attributes);
24122       if (attributes)
24123         TREE_CHAIN (attributes) = NULL_TREE;
24124
24125       DECL_CHAIN (decl) = decls;
24126       decls = decl;
24127
24128       token = cp_lexer_peek_token (parser->lexer);
24129       if (token->type == CPP_COMMA)
24130         {
24131           cp_lexer_consume_token (parser->lexer);  /* Eat ','.  */
24132           continue;
24133         }
24134       else
24135         break;
24136     }
24137   return decls;
24138 }
24139
24140 /* Parse an Objective-C @property declaration.  The syntax is:
24141
24142    objc-property-declaration:
24143      '@property' objc-property-attributes[opt] struct-declaration ;
24144
24145    objc-property-attributes:
24146     '(' objc-property-attribute-list ')'
24147
24148    objc-property-attribute-list:
24149      objc-property-attribute
24150      objc-property-attribute-list, objc-property-attribute
24151
24152    objc-property-attribute
24153      'getter' = identifier
24154      'setter' = identifier
24155      'readonly'
24156      'readwrite'
24157      'assign'
24158      'retain'
24159      'copy'
24160      'nonatomic'
24161
24162   For example:
24163     @property NSString *name;
24164     @property (readonly) id object;
24165     @property (retain, nonatomic, getter=getTheName) id name;
24166     @property int a, b, c;
24167
24168    PS: This function is identical to
24169    c_parser_objc_at_property_declaration for C.  Keep them in sync.  */
24170 static void 
24171 cp_parser_objc_at_property_declaration (cp_parser *parser)
24172 {
24173   /* The following variables hold the attributes of the properties as
24174      parsed.  They are 'false' or 'NULL_TREE' if the attribute was not
24175      seen.  When we see an attribute, we set them to 'true' (if they
24176      are boolean properties) or to the identifier (if they have an
24177      argument, ie, for getter and setter).  Note that here we only
24178      parse the list of attributes, check the syntax and accumulate the
24179      attributes that we find.  objc_add_property_declaration() will
24180      then process the information.  */
24181   bool property_assign = false;
24182   bool property_copy = false;
24183   tree property_getter_ident = NULL_TREE;
24184   bool property_nonatomic = false;
24185   bool property_readonly = false;
24186   bool property_readwrite = false;
24187   bool property_retain = false;
24188   tree property_setter_ident = NULL_TREE;
24189
24190   /* 'properties' is the list of properties that we read.  Usually a
24191      single one, but maybe more (eg, in "@property int a, b, c;" there
24192      are three).  */
24193   tree properties;
24194   location_t loc;
24195
24196   loc = cp_lexer_peek_token (parser->lexer)->location;
24197
24198   cp_lexer_consume_token (parser->lexer);  /* Eat '@property'.  */
24199
24200   /* Parse the optional attribute list...  */
24201   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
24202     {
24203       /* Eat the '('.  */
24204       cp_lexer_consume_token (parser->lexer);
24205
24206       while (true)
24207         {
24208           bool syntax_error = false;
24209           cp_token *token = cp_lexer_peek_token (parser->lexer);
24210           enum rid keyword;
24211
24212           if (token->type != CPP_NAME)
24213             {
24214               cp_parser_error (parser, "expected identifier");
24215               break;
24216             }
24217           keyword = C_RID_CODE (token->u.value);
24218           cp_lexer_consume_token (parser->lexer);
24219           switch (keyword)
24220             {
24221             case RID_ASSIGN:    property_assign = true;    break;
24222             case RID_COPY:      property_copy = true;      break;
24223             case RID_NONATOMIC: property_nonatomic = true; break;
24224             case RID_READONLY:  property_readonly = true;  break;
24225             case RID_READWRITE: property_readwrite = true; break;
24226             case RID_RETAIN:    property_retain = true;    break;
24227
24228             case RID_GETTER:
24229             case RID_SETTER:
24230               if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
24231                 {
24232                   if (keyword == RID_GETTER)
24233                     cp_parser_error (parser,
24234                                      "missing %<=%> (after %<getter%> attribute)");
24235                   else
24236                     cp_parser_error (parser,
24237                                      "missing %<=%> (after %<setter%> attribute)");
24238                   syntax_error = true;
24239                   break;
24240                 }
24241               cp_lexer_consume_token (parser->lexer); /* eat the = */
24242               if (!cp_parser_objc_selector_p (cp_lexer_peek_token (parser->lexer)->type))
24243                 {
24244                   cp_parser_error (parser, "expected identifier");
24245                   syntax_error = true;
24246                   break;
24247                 }
24248               if (keyword == RID_SETTER)
24249                 {
24250                   if (property_setter_ident != NULL_TREE)
24251                     {
24252                       cp_parser_error (parser, "the %<setter%> attribute may only be specified once");
24253                       cp_lexer_consume_token (parser->lexer);
24254                     }
24255                   else
24256                     property_setter_ident = cp_parser_objc_selector (parser);
24257                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_COLON))
24258                     cp_parser_error (parser, "setter name must terminate with %<:%>");
24259                   else
24260                     cp_lexer_consume_token (parser->lexer);
24261                 }
24262               else
24263                 {
24264                   if (property_getter_ident != NULL_TREE)
24265                     {
24266                       cp_parser_error (parser, "the %<getter%> attribute may only be specified once");
24267                       cp_lexer_consume_token (parser->lexer);
24268                     }
24269                   else
24270                     property_getter_ident = cp_parser_objc_selector (parser);
24271                 }
24272               break;
24273             default:
24274               cp_parser_error (parser, "unknown property attribute");
24275               syntax_error = true;
24276               break;
24277             }
24278
24279           if (syntax_error)
24280             break;
24281
24282           if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24283             cp_lexer_consume_token (parser->lexer);
24284           else
24285             break;
24286         }
24287
24288       /* FIXME: "@property (setter, assign);" will generate a spurious
24289          "error: expected â€˜)’ before â€˜,’ token".  This is because
24290          cp_parser_require, unlike the C counterpart, will produce an
24291          error even if we are in error recovery.  */
24292       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24293         {
24294           cp_parser_skip_to_closing_parenthesis (parser,
24295                                                  /*recovering=*/true,
24296                                                  /*or_comma=*/false,
24297                                                  /*consume_paren=*/true);
24298         }
24299     }
24300
24301   /* ... and the property declaration(s).  */
24302   properties = cp_parser_objc_struct_declaration (parser);
24303
24304   if (properties == error_mark_node)
24305     {
24306       cp_parser_skip_to_end_of_statement (parser);
24307       /* If the next token is now a `;', consume it.  */
24308       if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
24309         cp_lexer_consume_token (parser->lexer);
24310       return;
24311     }
24312
24313   if (properties == NULL_TREE)
24314     cp_parser_error (parser, "expected identifier");
24315   else
24316     {
24317       /* Comma-separated properties are chained together in
24318          reverse order; add them one by one.  */
24319       properties = nreverse (properties);
24320       
24321       for (; properties; properties = TREE_CHAIN (properties))
24322         objc_add_property_declaration (loc, copy_node (properties),
24323                                        property_readonly, property_readwrite,
24324                                        property_assign, property_retain,
24325                                        property_copy, property_nonatomic,
24326                                        property_getter_ident, property_setter_ident);
24327     }
24328   
24329   cp_parser_consume_semicolon_at_end_of_statement (parser);
24330 }
24331
24332 /* Parse an Objective-C++ @synthesize declaration.  The syntax is:
24333
24334    objc-synthesize-declaration:
24335      @synthesize objc-synthesize-identifier-list ;
24336
24337    objc-synthesize-identifier-list:
24338      objc-synthesize-identifier
24339      objc-synthesize-identifier-list, objc-synthesize-identifier
24340
24341    objc-synthesize-identifier
24342      identifier
24343      identifier = identifier
24344
24345   For example:
24346     @synthesize MyProperty;
24347     @synthesize OneProperty, AnotherProperty=MyIvar, YetAnotherProperty;
24348
24349   PS: This function is identical to c_parser_objc_at_synthesize_declaration
24350   for C.  Keep them in sync.
24351 */
24352 static void 
24353 cp_parser_objc_at_synthesize_declaration (cp_parser *parser)
24354 {
24355   tree list = NULL_TREE;
24356   location_t loc;
24357   loc = cp_lexer_peek_token (parser->lexer)->location;
24358
24359   cp_lexer_consume_token (parser->lexer);  /* Eat '@synthesize'.  */
24360   while (true)
24361     {
24362       tree property, ivar;
24363       property = cp_parser_identifier (parser);
24364       if (property == error_mark_node)
24365         {
24366           cp_parser_consume_semicolon_at_end_of_statement (parser);
24367           return;
24368         }
24369       if (cp_lexer_next_token_is (parser->lexer, CPP_EQ))
24370         {
24371           cp_lexer_consume_token (parser->lexer);
24372           ivar = cp_parser_identifier (parser);
24373           if (ivar == error_mark_node)
24374             {
24375               cp_parser_consume_semicolon_at_end_of_statement (parser);
24376               return;
24377             }
24378         }
24379       else
24380         ivar = NULL_TREE;
24381       list = chainon (list, build_tree_list (ivar, property));
24382       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24383         cp_lexer_consume_token (parser->lexer);
24384       else
24385         break;
24386     }
24387   cp_parser_consume_semicolon_at_end_of_statement (parser);
24388   objc_add_synthesize_declaration (loc, list);
24389 }
24390
24391 /* Parse an Objective-C++ @dynamic declaration.  The syntax is:
24392
24393    objc-dynamic-declaration:
24394      @dynamic identifier-list ;
24395
24396    For example:
24397      @dynamic MyProperty;
24398      @dynamic MyProperty, AnotherProperty;
24399
24400   PS: This function is identical to c_parser_objc_at_dynamic_declaration
24401   for C.  Keep them in sync.
24402 */
24403 static void 
24404 cp_parser_objc_at_dynamic_declaration (cp_parser *parser)
24405 {
24406   tree list = NULL_TREE;
24407   location_t loc;
24408   loc = cp_lexer_peek_token (parser->lexer)->location;
24409
24410   cp_lexer_consume_token (parser->lexer);  /* Eat '@dynamic'.  */
24411   while (true)
24412     {
24413       tree property;
24414       property = cp_parser_identifier (parser);
24415       if (property == error_mark_node)
24416         {
24417           cp_parser_consume_semicolon_at_end_of_statement (parser);
24418           return;
24419         }
24420       list = chainon (list, build_tree_list (NULL, property));
24421       if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24422         cp_lexer_consume_token (parser->lexer);
24423       else
24424         break;
24425     }
24426   cp_parser_consume_semicolon_at_end_of_statement (parser);
24427   objc_add_dynamic_declaration (loc, list);
24428 }
24429
24430 \f
24431 /* OpenMP 2.5 parsing routines.  */
24432
24433 /* Returns name of the next clause.
24434    If the clause is not recognized PRAGMA_OMP_CLAUSE_NONE is returned and
24435    the token is not consumed.  Otherwise appropriate pragma_omp_clause is
24436    returned and the token is consumed.  */
24437
24438 static pragma_omp_clause
24439 cp_parser_omp_clause_name (cp_parser *parser)
24440 {
24441   pragma_omp_clause result = PRAGMA_OMP_CLAUSE_NONE;
24442
24443   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_IF))
24444     result = PRAGMA_OMP_CLAUSE_IF;
24445   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_DEFAULT))
24446     result = PRAGMA_OMP_CLAUSE_DEFAULT;
24447   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_PRIVATE))
24448     result = PRAGMA_OMP_CLAUSE_PRIVATE;
24449   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24450     {
24451       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24452       const char *p = IDENTIFIER_POINTER (id);
24453
24454       switch (p[0])
24455         {
24456         case 'c':
24457           if (!strcmp ("collapse", p))
24458             result = PRAGMA_OMP_CLAUSE_COLLAPSE;
24459           else if (!strcmp ("copyin", p))
24460             result = PRAGMA_OMP_CLAUSE_COPYIN;
24461           else if (!strcmp ("copyprivate", p))
24462             result = PRAGMA_OMP_CLAUSE_COPYPRIVATE;
24463           break;
24464         case 'f':
24465           if (!strcmp ("final", p))
24466             result = PRAGMA_OMP_CLAUSE_FINAL;
24467           else if (!strcmp ("firstprivate", p))
24468             result = PRAGMA_OMP_CLAUSE_FIRSTPRIVATE;
24469           break;
24470         case 'l':
24471           if (!strcmp ("lastprivate", p))
24472             result = PRAGMA_OMP_CLAUSE_LASTPRIVATE;
24473           break;
24474         case 'm':
24475           if (!strcmp ("mergeable", p))
24476             result = PRAGMA_OMP_CLAUSE_MERGEABLE;
24477           break;
24478         case 'n':
24479           if (!strcmp ("nowait", p))
24480             result = PRAGMA_OMP_CLAUSE_NOWAIT;
24481           else if (!strcmp ("num_threads", p))
24482             result = PRAGMA_OMP_CLAUSE_NUM_THREADS;
24483           break;
24484         case 'o':
24485           if (!strcmp ("ordered", p))
24486             result = PRAGMA_OMP_CLAUSE_ORDERED;
24487           break;
24488         case 'r':
24489           if (!strcmp ("reduction", p))
24490             result = PRAGMA_OMP_CLAUSE_REDUCTION;
24491           break;
24492         case 's':
24493           if (!strcmp ("schedule", p))
24494             result = PRAGMA_OMP_CLAUSE_SCHEDULE;
24495           else if (!strcmp ("shared", p))
24496             result = PRAGMA_OMP_CLAUSE_SHARED;
24497           break;
24498         case 'u':
24499           if (!strcmp ("untied", p))
24500             result = PRAGMA_OMP_CLAUSE_UNTIED;
24501           break;
24502         }
24503     }
24504
24505   if (result != PRAGMA_OMP_CLAUSE_NONE)
24506     cp_lexer_consume_token (parser->lexer);
24507
24508   return result;
24509 }
24510
24511 /* Validate that a clause of the given type does not already exist.  */
24512
24513 static void
24514 check_no_duplicate_clause (tree clauses, enum omp_clause_code code,
24515                            const char *name, location_t location)
24516 {
24517   tree c;
24518
24519   for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
24520     if (OMP_CLAUSE_CODE (c) == code)
24521       {
24522         error_at (location, "too many %qs clauses", name);
24523         break;
24524       }
24525 }
24526
24527 /* OpenMP 2.5:
24528    variable-list:
24529      identifier
24530      variable-list , identifier
24531
24532    In addition, we match a closing parenthesis.  An opening parenthesis
24533    will have been consumed by the caller.
24534
24535    If KIND is nonzero, create the appropriate node and install the decl
24536    in OMP_CLAUSE_DECL and add the node to the head of the list.
24537
24538    If KIND is zero, create a TREE_LIST with the decl in TREE_PURPOSE;
24539    return the list created.  */
24540
24541 static tree
24542 cp_parser_omp_var_list_no_open (cp_parser *parser, enum omp_clause_code kind,
24543                                 tree list)
24544 {
24545   cp_token *token;
24546   while (1)
24547     {
24548       tree name, decl;
24549
24550       token = cp_lexer_peek_token (parser->lexer);
24551       name = cp_parser_id_expression (parser, /*template_p=*/false,
24552                                       /*check_dependency_p=*/true,
24553                                       /*template_p=*/NULL,
24554                                       /*declarator_p=*/false,
24555                                       /*optional_p=*/false);
24556       if (name == error_mark_node)
24557         goto skip_comma;
24558
24559       decl = cp_parser_lookup_name_simple (parser, name, token->location);
24560       if (decl == error_mark_node)
24561         cp_parser_name_lookup_error (parser, name, decl, NLE_NULL,
24562                                      token->location);
24563       else if (kind != 0)
24564         {
24565           tree u = build_omp_clause (token->location, kind);
24566           OMP_CLAUSE_DECL (u) = decl;
24567           OMP_CLAUSE_CHAIN (u) = list;
24568           list = u;
24569         }
24570       else
24571         list = tree_cons (decl, NULL_TREE, list);
24572
24573     get_comma:
24574       if (cp_lexer_next_token_is_not (parser->lexer, CPP_COMMA))
24575         break;
24576       cp_lexer_consume_token (parser->lexer);
24577     }
24578
24579   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24580     {
24581       int ending;
24582
24583       /* Try to resync to an unnested comma.  Copied from
24584          cp_parser_parenthesized_expression_list.  */
24585     skip_comma:
24586       ending = cp_parser_skip_to_closing_parenthesis (parser,
24587                                                       /*recovering=*/true,
24588                                                       /*or_comma=*/true,
24589                                                       /*consume_paren=*/true);
24590       if (ending < 0)
24591         goto get_comma;
24592     }
24593
24594   return list;
24595 }
24596
24597 /* Similarly, but expect leading and trailing parenthesis.  This is a very
24598    common case for omp clauses.  */
24599
24600 static tree
24601 cp_parser_omp_var_list (cp_parser *parser, enum omp_clause_code kind, tree list)
24602 {
24603   if (cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24604     return cp_parser_omp_var_list_no_open (parser, kind, list);
24605   return list;
24606 }
24607
24608 /* OpenMP 3.0:
24609    collapse ( constant-expression ) */
24610
24611 static tree
24612 cp_parser_omp_clause_collapse (cp_parser *parser, tree list, location_t location)
24613 {
24614   tree c, num;
24615   location_t loc;
24616   HOST_WIDE_INT n;
24617
24618   loc = cp_lexer_peek_token (parser->lexer)->location;
24619   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24620     return list;
24621
24622   num = cp_parser_constant_expression (parser, false, NULL);
24623
24624   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24625     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24626                                            /*or_comma=*/false,
24627                                            /*consume_paren=*/true);
24628
24629   if (num == error_mark_node)
24630     return list;
24631   num = fold_non_dependent_expr (num);
24632   if (!INTEGRAL_TYPE_P (TREE_TYPE (num))
24633       || !host_integerp (num, 0)
24634       || (n = tree_low_cst (num, 0)) <= 0
24635       || (int) n != n)
24636     {
24637       error_at (loc, "collapse argument needs positive constant integer expression");
24638       return list;
24639     }
24640
24641   check_no_duplicate_clause (list, OMP_CLAUSE_COLLAPSE, "collapse", location);
24642   c = build_omp_clause (loc, OMP_CLAUSE_COLLAPSE);
24643   OMP_CLAUSE_CHAIN (c) = list;
24644   OMP_CLAUSE_COLLAPSE_EXPR (c) = num;
24645
24646   return c;
24647 }
24648
24649 /* OpenMP 2.5:
24650    default ( shared | none ) */
24651
24652 static tree
24653 cp_parser_omp_clause_default (cp_parser *parser, tree list, location_t location)
24654 {
24655   enum omp_clause_default_kind kind = OMP_CLAUSE_DEFAULT_UNSPECIFIED;
24656   tree c;
24657
24658   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24659     return list;
24660   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24661     {
24662       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24663       const char *p = IDENTIFIER_POINTER (id);
24664
24665       switch (p[0])
24666         {
24667         case 'n':
24668           if (strcmp ("none", p) != 0)
24669             goto invalid_kind;
24670           kind = OMP_CLAUSE_DEFAULT_NONE;
24671           break;
24672
24673         case 's':
24674           if (strcmp ("shared", p) != 0)
24675             goto invalid_kind;
24676           kind = OMP_CLAUSE_DEFAULT_SHARED;
24677           break;
24678
24679         default:
24680           goto invalid_kind;
24681         }
24682
24683       cp_lexer_consume_token (parser->lexer);
24684     }
24685   else
24686     {
24687     invalid_kind:
24688       cp_parser_error (parser, "expected %<none%> or %<shared%>");
24689     }
24690
24691   if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24692     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24693                                            /*or_comma=*/false,
24694                                            /*consume_paren=*/true);
24695
24696   if (kind == OMP_CLAUSE_DEFAULT_UNSPECIFIED)
24697     return list;
24698
24699   check_no_duplicate_clause (list, OMP_CLAUSE_DEFAULT, "default", location);
24700   c = build_omp_clause (location, OMP_CLAUSE_DEFAULT);
24701   OMP_CLAUSE_CHAIN (c) = list;
24702   OMP_CLAUSE_DEFAULT_KIND (c) = kind;
24703
24704   return c;
24705 }
24706
24707 /* OpenMP 3.1:
24708    final ( expression ) */
24709
24710 static tree
24711 cp_parser_omp_clause_final (cp_parser *parser, tree list, location_t location)
24712 {
24713   tree t, c;
24714
24715   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24716     return list;
24717
24718   t = cp_parser_condition (parser);
24719
24720   if (t == error_mark_node
24721       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24722     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24723                                            /*or_comma=*/false,
24724                                            /*consume_paren=*/true);
24725
24726   check_no_duplicate_clause (list, OMP_CLAUSE_FINAL, "final", location);
24727
24728   c = build_omp_clause (location, OMP_CLAUSE_FINAL);
24729   OMP_CLAUSE_FINAL_EXPR (c) = t;
24730   OMP_CLAUSE_CHAIN (c) = list;
24731
24732   return c;
24733 }
24734
24735 /* OpenMP 2.5:
24736    if ( expression ) */
24737
24738 static tree
24739 cp_parser_omp_clause_if (cp_parser *parser, tree list, location_t location)
24740 {
24741   tree t, c;
24742
24743   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24744     return list;
24745
24746   t = cp_parser_condition (parser);
24747
24748   if (t == error_mark_node
24749       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24750     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24751                                            /*or_comma=*/false,
24752                                            /*consume_paren=*/true);
24753
24754   check_no_duplicate_clause (list, OMP_CLAUSE_IF, "if", location);
24755
24756   c = build_omp_clause (location, OMP_CLAUSE_IF);
24757   OMP_CLAUSE_IF_EXPR (c) = t;
24758   OMP_CLAUSE_CHAIN (c) = list;
24759
24760   return c;
24761 }
24762
24763 /* OpenMP 3.1:
24764    mergeable */
24765
24766 static tree
24767 cp_parser_omp_clause_mergeable (cp_parser *parser ATTRIBUTE_UNUSED,
24768                                 tree list, location_t location)
24769 {
24770   tree c;
24771
24772   check_no_duplicate_clause (list, OMP_CLAUSE_MERGEABLE, "mergeable",
24773                              location);
24774
24775   c = build_omp_clause (location, OMP_CLAUSE_MERGEABLE);
24776   OMP_CLAUSE_CHAIN (c) = list;
24777   return c;
24778 }
24779
24780 /* OpenMP 2.5:
24781    nowait */
24782
24783 static tree
24784 cp_parser_omp_clause_nowait (cp_parser *parser ATTRIBUTE_UNUSED,
24785                              tree list, location_t location)
24786 {
24787   tree c;
24788
24789   check_no_duplicate_clause (list, OMP_CLAUSE_NOWAIT, "nowait", location);
24790
24791   c = build_omp_clause (location, OMP_CLAUSE_NOWAIT);
24792   OMP_CLAUSE_CHAIN (c) = list;
24793   return c;
24794 }
24795
24796 /* OpenMP 2.5:
24797    num_threads ( expression ) */
24798
24799 static tree
24800 cp_parser_omp_clause_num_threads (cp_parser *parser, tree list,
24801                                   location_t location)
24802 {
24803   tree t, c;
24804
24805   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24806     return list;
24807
24808   t = cp_parser_expression (parser, false, NULL);
24809
24810   if (t == error_mark_node
24811       || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
24812     cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24813                                            /*or_comma=*/false,
24814                                            /*consume_paren=*/true);
24815
24816   check_no_duplicate_clause (list, OMP_CLAUSE_NUM_THREADS,
24817                              "num_threads", location);
24818
24819   c = build_omp_clause (location, OMP_CLAUSE_NUM_THREADS);
24820   OMP_CLAUSE_NUM_THREADS_EXPR (c) = t;
24821   OMP_CLAUSE_CHAIN (c) = list;
24822
24823   return c;
24824 }
24825
24826 /* OpenMP 2.5:
24827    ordered */
24828
24829 static tree
24830 cp_parser_omp_clause_ordered (cp_parser *parser ATTRIBUTE_UNUSED,
24831                               tree list, location_t location)
24832 {
24833   tree c;
24834
24835   check_no_duplicate_clause (list, OMP_CLAUSE_ORDERED,
24836                              "ordered", location);
24837
24838   c = build_omp_clause (location, OMP_CLAUSE_ORDERED);
24839   OMP_CLAUSE_CHAIN (c) = list;
24840   return c;
24841 }
24842
24843 /* OpenMP 2.5:
24844    reduction ( reduction-operator : variable-list )
24845
24846    reduction-operator:
24847      One of: + * - & ^ | && ||
24848
24849    OpenMP 3.1:
24850
24851    reduction-operator:
24852      One of: + * - & ^ | && || min max  */
24853
24854 static tree
24855 cp_parser_omp_clause_reduction (cp_parser *parser, tree list)
24856 {
24857   enum tree_code code;
24858   tree nlist, c;
24859
24860   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24861     return list;
24862
24863   switch (cp_lexer_peek_token (parser->lexer)->type)
24864     {
24865     case CPP_PLUS:
24866       code = PLUS_EXPR;
24867       break;
24868     case CPP_MULT:
24869       code = MULT_EXPR;
24870       break;
24871     case CPP_MINUS:
24872       code = MINUS_EXPR;
24873       break;
24874     case CPP_AND:
24875       code = BIT_AND_EXPR;
24876       break;
24877     case CPP_XOR:
24878       code = BIT_XOR_EXPR;
24879       break;
24880     case CPP_OR:
24881       code = BIT_IOR_EXPR;
24882       break;
24883     case CPP_AND_AND:
24884       code = TRUTH_ANDIF_EXPR;
24885       break;
24886     case CPP_OR_OR:
24887       code = TRUTH_ORIF_EXPR;
24888       break;
24889     case CPP_NAME:
24890       {
24891         tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24892         const char *p = IDENTIFIER_POINTER (id);
24893
24894         if (strcmp (p, "min") == 0)
24895           {
24896             code = MIN_EXPR;
24897             break;
24898           }
24899         if (strcmp (p, "max") == 0)
24900           {
24901             code = MAX_EXPR;
24902             break;
24903           }
24904       }
24905       /* FALLTHROUGH */
24906     default:
24907       cp_parser_error (parser, "expected %<+%>, %<*%>, %<-%>, %<&%>, %<^%>, "
24908                                "%<|%>, %<&&%>, %<||%>, %<min%> or %<max%>");
24909     resync_fail:
24910       cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
24911                                              /*or_comma=*/false,
24912                                              /*consume_paren=*/true);
24913       return list;
24914     }
24915   cp_lexer_consume_token (parser->lexer);
24916
24917   if (!cp_parser_require (parser, CPP_COLON, RT_COLON))
24918     goto resync_fail;
24919
24920   nlist = cp_parser_omp_var_list_no_open (parser, OMP_CLAUSE_REDUCTION, list);
24921   for (c = nlist; c != list; c = OMP_CLAUSE_CHAIN (c))
24922     OMP_CLAUSE_REDUCTION_CODE (c) = code;
24923
24924   return nlist;
24925 }
24926
24927 /* OpenMP 2.5:
24928    schedule ( schedule-kind )
24929    schedule ( schedule-kind , expression )
24930
24931    schedule-kind:
24932      static | dynamic | guided | runtime | auto  */
24933
24934 static tree
24935 cp_parser_omp_clause_schedule (cp_parser *parser, tree list, location_t location)
24936 {
24937   tree c, t;
24938
24939   if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
24940     return list;
24941
24942   c = build_omp_clause (location, OMP_CLAUSE_SCHEDULE);
24943
24944   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
24945     {
24946       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
24947       const char *p = IDENTIFIER_POINTER (id);
24948
24949       switch (p[0])
24950         {
24951         case 'd':
24952           if (strcmp ("dynamic", p) != 0)
24953             goto invalid_kind;
24954           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_DYNAMIC;
24955           break;
24956
24957         case 'g':
24958           if (strcmp ("guided", p) != 0)
24959             goto invalid_kind;
24960           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_GUIDED;
24961           break;
24962
24963         case 'r':
24964           if (strcmp ("runtime", p) != 0)
24965             goto invalid_kind;
24966           OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_RUNTIME;
24967           break;
24968
24969         default:
24970           goto invalid_kind;
24971         }
24972     }
24973   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_STATIC))
24974     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_STATIC;
24975   else if (cp_lexer_next_token_is_keyword (parser->lexer, RID_AUTO))
24976     OMP_CLAUSE_SCHEDULE_KIND (c) = OMP_CLAUSE_SCHEDULE_AUTO;
24977   else
24978     goto invalid_kind;
24979   cp_lexer_consume_token (parser->lexer);
24980
24981   if (cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
24982     {
24983       cp_token *token;
24984       cp_lexer_consume_token (parser->lexer);
24985
24986       token = cp_lexer_peek_token (parser->lexer);
24987       t = cp_parser_assignment_expression (parser, false, NULL);
24988
24989       if (t == error_mark_node)
24990         goto resync_fail;
24991       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_RUNTIME)
24992         error_at (token->location, "schedule %<runtime%> does not take "
24993                   "a %<chunk_size%> parameter");
24994       else if (OMP_CLAUSE_SCHEDULE_KIND (c) == OMP_CLAUSE_SCHEDULE_AUTO)
24995         error_at (token->location, "schedule %<auto%> does not take "
24996                   "a %<chunk_size%> parameter");
24997       else
24998         OMP_CLAUSE_SCHEDULE_CHUNK_EXPR (c) = t;
24999
25000       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25001         goto resync_fail;
25002     }
25003   else if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_COMMA_CLOSE_PAREN))
25004     goto resync_fail;
25005
25006   check_no_duplicate_clause (list, OMP_CLAUSE_SCHEDULE, "schedule", location);
25007   OMP_CLAUSE_CHAIN (c) = list;
25008   return c;
25009
25010  invalid_kind:
25011   cp_parser_error (parser, "invalid schedule kind");
25012  resync_fail:
25013   cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25014                                          /*or_comma=*/false,
25015                                          /*consume_paren=*/true);
25016   return list;
25017 }
25018
25019 /* OpenMP 3.0:
25020    untied */
25021
25022 static tree
25023 cp_parser_omp_clause_untied (cp_parser *parser ATTRIBUTE_UNUSED,
25024                              tree list, location_t location)
25025 {
25026   tree c;
25027
25028   check_no_duplicate_clause (list, OMP_CLAUSE_UNTIED, "untied", location);
25029
25030   c = build_omp_clause (location, OMP_CLAUSE_UNTIED);
25031   OMP_CLAUSE_CHAIN (c) = list;
25032   return c;
25033 }
25034
25035 /* Parse all OpenMP clauses.  The set clauses allowed by the directive
25036    is a bitmask in MASK.  Return the list of clauses found; the result
25037    of clause default goes in *pdefault.  */
25038
25039 static tree
25040 cp_parser_omp_all_clauses (cp_parser *parser, unsigned int mask,
25041                            const char *where, cp_token *pragma_tok)
25042 {
25043   tree clauses = NULL;
25044   bool first = true;
25045   cp_token *token = NULL;
25046
25047   while (cp_lexer_next_token_is_not (parser->lexer, CPP_PRAGMA_EOL))
25048     {
25049       pragma_omp_clause c_kind;
25050       const char *c_name;
25051       tree prev = clauses;
25052
25053       if (!first && cp_lexer_next_token_is (parser->lexer, CPP_COMMA))
25054         cp_lexer_consume_token (parser->lexer);
25055
25056       token = cp_lexer_peek_token (parser->lexer);
25057       c_kind = cp_parser_omp_clause_name (parser);
25058       first = false;
25059
25060       switch (c_kind)
25061         {
25062         case PRAGMA_OMP_CLAUSE_COLLAPSE:
25063           clauses = cp_parser_omp_clause_collapse (parser, clauses,
25064                                                    token->location);
25065           c_name = "collapse";
25066           break;
25067         case PRAGMA_OMP_CLAUSE_COPYIN:
25068           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYIN, clauses);
25069           c_name = "copyin";
25070           break;
25071         case PRAGMA_OMP_CLAUSE_COPYPRIVATE:
25072           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_COPYPRIVATE,
25073                                             clauses);
25074           c_name = "copyprivate";
25075           break;
25076         case PRAGMA_OMP_CLAUSE_DEFAULT:
25077           clauses = cp_parser_omp_clause_default (parser, clauses,
25078                                                   token->location);
25079           c_name = "default";
25080           break;
25081         case PRAGMA_OMP_CLAUSE_FINAL:
25082           clauses = cp_parser_omp_clause_final (parser, clauses, token->location);
25083           c_name = "final";
25084           break;
25085         case PRAGMA_OMP_CLAUSE_FIRSTPRIVATE:
25086           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_FIRSTPRIVATE,
25087                                             clauses);
25088           c_name = "firstprivate";
25089           break;
25090         case PRAGMA_OMP_CLAUSE_IF:
25091           clauses = cp_parser_omp_clause_if (parser, clauses, token->location);
25092           c_name = "if";
25093           break;
25094         case PRAGMA_OMP_CLAUSE_LASTPRIVATE:
25095           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_LASTPRIVATE,
25096                                             clauses);
25097           c_name = "lastprivate";
25098           break;
25099         case PRAGMA_OMP_CLAUSE_MERGEABLE:
25100           clauses = cp_parser_omp_clause_mergeable (parser, clauses,
25101                                                     token->location);
25102           c_name = "mergeable";
25103           break;
25104         case PRAGMA_OMP_CLAUSE_NOWAIT:
25105           clauses = cp_parser_omp_clause_nowait (parser, clauses, token->location);
25106           c_name = "nowait";
25107           break;
25108         case PRAGMA_OMP_CLAUSE_NUM_THREADS:
25109           clauses = cp_parser_omp_clause_num_threads (parser, clauses,
25110                                                       token->location);
25111           c_name = "num_threads";
25112           break;
25113         case PRAGMA_OMP_CLAUSE_ORDERED:
25114           clauses = cp_parser_omp_clause_ordered (parser, clauses,
25115                                                   token->location);
25116           c_name = "ordered";
25117           break;
25118         case PRAGMA_OMP_CLAUSE_PRIVATE:
25119           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_PRIVATE,
25120                                             clauses);
25121           c_name = "private";
25122           break;
25123         case PRAGMA_OMP_CLAUSE_REDUCTION:
25124           clauses = cp_parser_omp_clause_reduction (parser, clauses);
25125           c_name = "reduction";
25126           break;
25127         case PRAGMA_OMP_CLAUSE_SCHEDULE:
25128           clauses = cp_parser_omp_clause_schedule (parser, clauses,
25129                                                    token->location);
25130           c_name = "schedule";
25131           break;
25132         case PRAGMA_OMP_CLAUSE_SHARED:
25133           clauses = cp_parser_omp_var_list (parser, OMP_CLAUSE_SHARED,
25134                                             clauses);
25135           c_name = "shared";
25136           break;
25137         case PRAGMA_OMP_CLAUSE_UNTIED:
25138           clauses = cp_parser_omp_clause_untied (parser, clauses,
25139                                                  token->location);
25140           c_name = "nowait";
25141           break;
25142         default:
25143           cp_parser_error (parser, "expected %<#pragma omp%> clause");
25144           goto saw_error;
25145         }
25146
25147       if (((mask >> c_kind) & 1) == 0)
25148         {
25149           /* Remove the invalid clause(s) from the list to avoid
25150              confusing the rest of the compiler.  */
25151           clauses = prev;
25152           error_at (token->location, "%qs is not valid for %qs", c_name, where);
25153         }
25154     }
25155  saw_error:
25156   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
25157   return finish_omp_clauses (clauses);
25158 }
25159
25160 /* OpenMP 2.5:
25161    structured-block:
25162      statement
25163
25164    In practice, we're also interested in adding the statement to an
25165    outer node.  So it is convenient if we work around the fact that
25166    cp_parser_statement calls add_stmt.  */
25167
25168 static unsigned
25169 cp_parser_begin_omp_structured_block (cp_parser *parser)
25170 {
25171   unsigned save = parser->in_statement;
25172
25173   /* Only move the values to IN_OMP_BLOCK if they weren't false.
25174      This preserves the "not within loop or switch" style error messages
25175      for nonsense cases like
25176         void foo() {
25177         #pragma omp single
25178           break;
25179         }
25180   */
25181   if (parser->in_statement)
25182     parser->in_statement = IN_OMP_BLOCK;
25183
25184   return save;
25185 }
25186
25187 static void
25188 cp_parser_end_omp_structured_block (cp_parser *parser, unsigned save)
25189 {
25190   parser->in_statement = save;
25191 }
25192
25193 static tree
25194 cp_parser_omp_structured_block (cp_parser *parser)
25195 {
25196   tree stmt = begin_omp_structured_block ();
25197   unsigned int save = cp_parser_begin_omp_structured_block (parser);
25198
25199   cp_parser_statement (parser, NULL_TREE, false, NULL);
25200
25201   cp_parser_end_omp_structured_block (parser, save);
25202   return finish_omp_structured_block (stmt);
25203 }
25204
25205 /* OpenMP 2.5:
25206    # pragma omp atomic new-line
25207      expression-stmt
25208
25209    expression-stmt:
25210      x binop= expr | x++ | ++x | x-- | --x
25211    binop:
25212      +, *, -, /, &, ^, |, <<, >>
25213
25214   where x is an lvalue expression with scalar type.
25215
25216    OpenMP 3.1:
25217    # pragma omp atomic new-line
25218      update-stmt
25219
25220    # pragma omp atomic read new-line
25221      read-stmt
25222
25223    # pragma omp atomic write new-line
25224      write-stmt
25225
25226    # pragma omp atomic update new-line
25227      update-stmt
25228
25229    # pragma omp atomic capture new-line
25230      capture-stmt
25231
25232    # pragma omp atomic capture new-line
25233      capture-block
25234
25235    read-stmt:
25236      v = x
25237    write-stmt:
25238      x = expr
25239    update-stmt:
25240      expression-stmt | x = x binop expr
25241    capture-stmt:
25242      v = x binop= expr | v = x++ | v = ++x | v = x-- | v = --x
25243    capture-block:
25244      { v = x; update-stmt; } | { update-stmt; v = x; }
25245
25246   where x and v are lvalue expressions with scalar type.  */
25247
25248 static void
25249 cp_parser_omp_atomic (cp_parser *parser, cp_token *pragma_tok)
25250 {
25251   tree lhs = NULL_TREE, rhs = NULL_TREE, v = NULL_TREE, lhs1 = NULL_TREE;
25252   tree rhs1 = NULL_TREE, orig_lhs;
25253   enum tree_code code = OMP_ATOMIC, opcode = NOP_EXPR;
25254   bool structured_block = false;
25255
25256   if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
25257     {
25258       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
25259       const char *p = IDENTIFIER_POINTER (id);
25260
25261       if (!strcmp (p, "read"))
25262         code = OMP_ATOMIC_READ;
25263       else if (!strcmp (p, "write"))
25264         code = NOP_EXPR;
25265       else if (!strcmp (p, "update"))
25266         code = OMP_ATOMIC;
25267       else if (!strcmp (p, "capture"))
25268         code = OMP_ATOMIC_CAPTURE_NEW;
25269       else
25270         p = NULL;
25271       if (p)
25272         cp_lexer_consume_token (parser->lexer);
25273     }
25274   cp_parser_require_pragma_eol (parser, pragma_tok);
25275
25276   switch (code)
25277     {
25278     case OMP_ATOMIC_READ:
25279     case NOP_EXPR: /* atomic write */
25280       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25281                                       /*cast_p=*/false, NULL);
25282       if (v == error_mark_node)
25283         goto saw_error;
25284       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25285         goto saw_error;
25286       if (code == NOP_EXPR)
25287         lhs = cp_parser_expression (parser, /*cast_p=*/false, NULL);
25288       else
25289         lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25290                                           /*cast_p=*/false, NULL);
25291       if (lhs == error_mark_node)
25292         goto saw_error;
25293       if (code == NOP_EXPR)
25294         {
25295           /* atomic write is represented by OMP_ATOMIC with NOP_EXPR
25296              opcode.  */
25297           code = OMP_ATOMIC;
25298           rhs = lhs;
25299           lhs = v;
25300           v = NULL_TREE;
25301         }
25302       goto done;
25303     case OMP_ATOMIC_CAPTURE_NEW:
25304       if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
25305         {
25306           cp_lexer_consume_token (parser->lexer);
25307           structured_block = true;
25308         }
25309       else
25310         {
25311           v = cp_parser_unary_expression (parser, /*address_p=*/false,
25312                                           /*cast_p=*/false, NULL);
25313           if (v == error_mark_node)
25314             goto saw_error;
25315           if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25316             goto saw_error;
25317         }
25318     default:
25319       break;
25320     }
25321
25322 restart:
25323   lhs = cp_parser_unary_expression (parser, /*address_p=*/false,
25324                                     /*cast_p=*/false, NULL);
25325   orig_lhs = lhs;
25326   switch (TREE_CODE (lhs))
25327     {
25328     case ERROR_MARK:
25329       goto saw_error;
25330
25331     case POSTINCREMENT_EXPR:
25332       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25333         code = OMP_ATOMIC_CAPTURE_OLD;
25334       /* FALLTHROUGH */
25335     case PREINCREMENT_EXPR:
25336       lhs = TREE_OPERAND (lhs, 0);
25337       opcode = PLUS_EXPR;
25338       rhs = integer_one_node;
25339       break;
25340
25341     case POSTDECREMENT_EXPR:
25342       if (code == OMP_ATOMIC_CAPTURE_NEW && !structured_block)
25343         code = OMP_ATOMIC_CAPTURE_OLD;
25344       /* FALLTHROUGH */
25345     case PREDECREMENT_EXPR:
25346       lhs = TREE_OPERAND (lhs, 0);
25347       opcode = MINUS_EXPR;
25348       rhs = integer_one_node;
25349       break;
25350
25351     case COMPOUND_EXPR:
25352       if (TREE_CODE (TREE_OPERAND (lhs, 0)) == SAVE_EXPR
25353          && TREE_CODE (TREE_OPERAND (lhs, 1)) == COMPOUND_EXPR
25354          && TREE_CODE (TREE_OPERAND (TREE_OPERAND (lhs, 1), 0)) == MODIFY_EXPR
25355          && TREE_OPERAND (TREE_OPERAND (lhs, 1), 1) == TREE_OPERAND (lhs, 0)
25356          && TREE_CODE (TREE_TYPE (TREE_OPERAND (TREE_OPERAND
25357                                              (TREE_OPERAND (lhs, 1), 0), 0)))
25358             == BOOLEAN_TYPE)
25359        /* Undo effects of boolean_increment for post {in,de}crement.  */
25360        lhs = TREE_OPERAND (TREE_OPERAND (lhs, 1), 0);
25361       /* FALLTHRU */
25362     case MODIFY_EXPR:
25363       if (TREE_CODE (lhs) == MODIFY_EXPR
25364          && TREE_CODE (TREE_TYPE (TREE_OPERAND (lhs, 0))) == BOOLEAN_TYPE)
25365         {
25366           /* Undo effects of boolean_increment.  */
25367           if (integer_onep (TREE_OPERAND (lhs, 1)))
25368             {
25369               /* This is pre or post increment.  */
25370               rhs = TREE_OPERAND (lhs, 1);
25371               lhs = TREE_OPERAND (lhs, 0);
25372               opcode = NOP_EXPR;
25373               if (code == OMP_ATOMIC_CAPTURE_NEW
25374                   && !structured_block
25375                   && TREE_CODE (orig_lhs) == COMPOUND_EXPR)
25376                 code = OMP_ATOMIC_CAPTURE_OLD;
25377               break;
25378             }
25379         }
25380       /* FALLTHRU */
25381     default:
25382       switch (cp_lexer_peek_token (parser->lexer)->type)
25383         {
25384         case CPP_MULT_EQ:
25385           opcode = MULT_EXPR;
25386           break;
25387         case CPP_DIV_EQ:
25388           opcode = TRUNC_DIV_EXPR;
25389           break;
25390         case CPP_PLUS_EQ:
25391           opcode = PLUS_EXPR;
25392           break;
25393         case CPP_MINUS_EQ:
25394           opcode = MINUS_EXPR;
25395           break;
25396         case CPP_LSHIFT_EQ:
25397           opcode = LSHIFT_EXPR;
25398           break;
25399         case CPP_RSHIFT_EQ:
25400           opcode = RSHIFT_EXPR;
25401           break;
25402         case CPP_AND_EQ:
25403           opcode = BIT_AND_EXPR;
25404           break;
25405         case CPP_OR_EQ:
25406           opcode = BIT_IOR_EXPR;
25407           break;
25408         case CPP_XOR_EQ:
25409           opcode = BIT_XOR_EXPR;
25410           break;
25411         case CPP_EQ:
25412           if (structured_block || code == OMP_ATOMIC)
25413             {
25414               enum cp_parser_prec oprec;
25415               cp_token *token;
25416               cp_lexer_consume_token (parser->lexer);
25417               rhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25418                                                  /*cast_p=*/false, NULL);
25419               if (rhs1 == error_mark_node)
25420                 goto saw_error;
25421               token = cp_lexer_peek_token (parser->lexer);
25422               switch (token->type)
25423                 {
25424                 case CPP_SEMICOLON:
25425                   if (code == OMP_ATOMIC_CAPTURE_NEW)
25426                     {
25427                       code = OMP_ATOMIC_CAPTURE_OLD;
25428                       v = lhs;
25429                       lhs = NULL_TREE;
25430                       lhs1 = rhs1;
25431                       rhs1 = NULL_TREE;
25432                       cp_lexer_consume_token (parser->lexer);
25433                       goto restart;
25434                     }
25435                   cp_parser_error (parser,
25436                                    "invalid form of %<#pragma omp atomic%>");
25437                   goto saw_error;
25438                 case CPP_MULT:
25439                   opcode = MULT_EXPR;
25440                   break;
25441                 case CPP_DIV:
25442                   opcode = TRUNC_DIV_EXPR;
25443                   break;
25444                 case CPP_PLUS:
25445                   opcode = PLUS_EXPR;
25446                   break;
25447                 case CPP_MINUS:
25448                   opcode = MINUS_EXPR;
25449                   break;
25450                 case CPP_LSHIFT:
25451                   opcode = LSHIFT_EXPR;
25452                   break;
25453                 case CPP_RSHIFT:
25454                   opcode = RSHIFT_EXPR;
25455                   break;
25456                 case CPP_AND:
25457                   opcode = BIT_AND_EXPR;
25458                   break;
25459                 case CPP_OR:
25460                   opcode = BIT_IOR_EXPR;
25461                   break;
25462                 case CPP_XOR:
25463                   opcode = BIT_XOR_EXPR;
25464                   break;
25465                 default:
25466                   cp_parser_error (parser,
25467                                    "invalid operator for %<#pragma omp atomic%>");
25468                   goto saw_error;
25469                 }
25470               oprec = TOKEN_PRECEDENCE (token);
25471               gcc_assert (oprec != PREC_NOT_OPERATOR);
25472               if (commutative_tree_code (opcode))
25473                 oprec = (enum cp_parser_prec) (oprec - 1);
25474               cp_lexer_consume_token (parser->lexer);
25475               rhs = cp_parser_binary_expression (parser, false, false,
25476                                                  oprec, NULL);
25477               if (rhs == error_mark_node)
25478                 goto saw_error;
25479               goto stmt_done;
25480             }
25481           /* FALLTHROUGH */
25482         default:
25483           cp_parser_error (parser,
25484                            "invalid operator for %<#pragma omp atomic%>");
25485           goto saw_error;
25486         }
25487       cp_lexer_consume_token (parser->lexer);
25488
25489       rhs = cp_parser_expression (parser, false, NULL);
25490       if (rhs == error_mark_node)
25491         goto saw_error;
25492       break;
25493     }
25494 stmt_done:
25495   if (structured_block && code == OMP_ATOMIC_CAPTURE_NEW)
25496     {
25497       if (!cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON))
25498         goto saw_error;
25499       v = cp_parser_unary_expression (parser, /*address_p=*/false,
25500                                       /*cast_p=*/false, NULL);
25501       if (v == error_mark_node)
25502         goto saw_error;
25503       if (!cp_parser_require (parser, CPP_EQ, RT_EQ))
25504         goto saw_error;
25505       lhs1 = cp_parser_unary_expression (parser, /*address_p=*/false,
25506                                          /*cast_p=*/false, NULL);
25507       if (lhs1 == error_mark_node)
25508         goto saw_error;
25509     }
25510   if (structured_block)
25511     {
25512       cp_parser_consume_semicolon_at_end_of_statement (parser);
25513       cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
25514     }
25515 done:
25516   finish_omp_atomic (code, opcode, lhs, rhs, v, lhs1, rhs1);
25517   if (!structured_block)
25518     cp_parser_consume_semicolon_at_end_of_statement (parser);
25519   return;
25520
25521  saw_error:
25522   cp_parser_skip_to_end_of_block_or_statement (parser);
25523   if (structured_block)
25524     {
25525       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25526         cp_lexer_consume_token (parser->lexer);
25527       else if (code == OMP_ATOMIC_CAPTURE_NEW)
25528         {
25529           cp_parser_skip_to_end_of_block_or_statement (parser);
25530           if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
25531             cp_lexer_consume_token (parser->lexer);
25532         }
25533     }
25534 }
25535
25536
25537 /* OpenMP 2.5:
25538    # pragma omp barrier new-line  */
25539
25540 static void
25541 cp_parser_omp_barrier (cp_parser *parser, cp_token *pragma_tok)
25542 {
25543   cp_parser_require_pragma_eol (parser, pragma_tok);
25544   finish_omp_barrier ();
25545 }
25546
25547 /* OpenMP 2.5:
25548    # pragma omp critical [(name)] new-line
25549      structured-block  */
25550
25551 static tree
25552 cp_parser_omp_critical (cp_parser *parser, cp_token *pragma_tok)
25553 {
25554   tree stmt, name = NULL;
25555
25556   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25557     {
25558       cp_lexer_consume_token (parser->lexer);
25559
25560       name = cp_parser_identifier (parser);
25561
25562       if (name == error_mark_node
25563           || !cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
25564         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
25565                                                /*or_comma=*/false,
25566                                                /*consume_paren=*/true);
25567       if (name == error_mark_node)
25568         name = NULL;
25569     }
25570   cp_parser_require_pragma_eol (parser, pragma_tok);
25571
25572   stmt = cp_parser_omp_structured_block (parser);
25573   return c_finish_omp_critical (input_location, stmt, name);
25574 }
25575
25576 /* OpenMP 2.5:
25577    # pragma omp flush flush-vars[opt] new-line
25578
25579    flush-vars:
25580      ( variable-list ) */
25581
25582 static void
25583 cp_parser_omp_flush (cp_parser *parser, cp_token *pragma_tok)
25584 {
25585   if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_PAREN))
25586     (void) cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
25587   cp_parser_require_pragma_eol (parser, pragma_tok);
25588
25589   finish_omp_flush ();
25590 }
25591
25592 /* Helper function, to parse omp for increment expression.  */
25593
25594 static tree
25595 cp_parser_omp_for_cond (cp_parser *parser, tree decl)
25596 {
25597   tree cond = cp_parser_binary_expression (parser, false, true,
25598                                            PREC_NOT_OPERATOR, NULL);
25599   if (cond == error_mark_node
25600       || cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25601     {
25602       cp_parser_skip_to_end_of_statement (parser);
25603       return error_mark_node;
25604     }
25605
25606   switch (TREE_CODE (cond))
25607     {
25608     case GT_EXPR:
25609     case GE_EXPR:
25610     case LT_EXPR:
25611     case LE_EXPR:
25612       break;
25613     default:
25614       return error_mark_node;
25615     }
25616
25617   /* If decl is an iterator, preserve LHS and RHS of the relational
25618      expr until finish_omp_for.  */
25619   if (decl
25620       && (type_dependent_expression_p (decl)
25621           || CLASS_TYPE_P (TREE_TYPE (decl))))
25622     return cond;
25623
25624   return build_x_binary_op (TREE_CODE (cond),
25625                             TREE_OPERAND (cond, 0), ERROR_MARK,
25626                             TREE_OPERAND (cond, 1), ERROR_MARK,
25627                             /*overload=*/NULL, tf_warning_or_error);
25628 }
25629
25630 /* Helper function, to parse omp for increment expression.  */
25631
25632 static tree
25633 cp_parser_omp_for_incr (cp_parser *parser, tree decl)
25634 {
25635   cp_token *token = cp_lexer_peek_token (parser->lexer);
25636   enum tree_code op;
25637   tree lhs, rhs;
25638   cp_id_kind idk;
25639   bool decl_first;
25640
25641   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25642     {
25643       op = (token->type == CPP_PLUS_PLUS
25644             ? PREINCREMENT_EXPR : PREDECREMENT_EXPR);
25645       cp_lexer_consume_token (parser->lexer);
25646       lhs = cp_parser_cast_expression (parser, false, false, NULL);
25647       if (lhs != decl)
25648         return error_mark_node;
25649       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25650     }
25651
25652   lhs = cp_parser_primary_expression (parser, false, false, false, &idk);
25653   if (lhs != decl)
25654     return error_mark_node;
25655
25656   token = cp_lexer_peek_token (parser->lexer);
25657   if (token->type == CPP_PLUS_PLUS || token->type == CPP_MINUS_MINUS)
25658     {
25659       op = (token->type == CPP_PLUS_PLUS
25660             ? POSTINCREMENT_EXPR : POSTDECREMENT_EXPR);
25661       cp_lexer_consume_token (parser->lexer);
25662       return build2 (op, TREE_TYPE (decl), decl, NULL_TREE);
25663     }
25664
25665   op = cp_parser_assignment_operator_opt (parser);
25666   if (op == ERROR_MARK)
25667     return error_mark_node;
25668
25669   if (op != NOP_EXPR)
25670     {
25671       rhs = cp_parser_assignment_expression (parser, false, NULL);
25672       rhs = build2 (op, TREE_TYPE (decl), decl, rhs);
25673       return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25674     }
25675
25676   lhs = cp_parser_binary_expression (parser, false, false,
25677                                      PREC_ADDITIVE_EXPRESSION, NULL);
25678   token = cp_lexer_peek_token (parser->lexer);
25679   decl_first = lhs == decl;
25680   if (decl_first)
25681     lhs = NULL_TREE;
25682   if (token->type != CPP_PLUS
25683       && token->type != CPP_MINUS)
25684     return error_mark_node;
25685
25686   do
25687     {
25688       op = token->type == CPP_PLUS ? PLUS_EXPR : MINUS_EXPR;
25689       cp_lexer_consume_token (parser->lexer);
25690       rhs = cp_parser_binary_expression (parser, false, false,
25691                                          PREC_ADDITIVE_EXPRESSION, NULL);
25692       token = cp_lexer_peek_token (parser->lexer);
25693       if (token->type == CPP_PLUS || token->type == CPP_MINUS || decl_first)
25694         {
25695           if (lhs == NULL_TREE)
25696             {
25697               if (op == PLUS_EXPR)
25698                 lhs = rhs;
25699               else
25700                 lhs = build_x_unary_op (NEGATE_EXPR, rhs, tf_warning_or_error);
25701             }
25702           else
25703             lhs = build_x_binary_op (op, lhs, ERROR_MARK, rhs, ERROR_MARK,
25704                                      NULL, tf_warning_or_error);
25705         }
25706     }
25707   while (token->type == CPP_PLUS || token->type == CPP_MINUS);
25708
25709   if (!decl_first)
25710     {
25711       if (rhs != decl || op == MINUS_EXPR)
25712         return error_mark_node;
25713       rhs = build2 (op, TREE_TYPE (decl), lhs, decl);
25714     }
25715   else
25716     rhs = build2 (PLUS_EXPR, TREE_TYPE (decl), decl, lhs);
25717
25718   return build2 (MODIFY_EXPR, TREE_TYPE (decl), decl, rhs);
25719 }
25720
25721 /* Parse the restricted form of the for statement allowed by OpenMP.  */
25722
25723 static tree
25724 cp_parser_omp_for_loop (cp_parser *parser, tree clauses, tree *par_clauses)
25725 {
25726   tree init, cond, incr, body, decl, pre_body = NULL_TREE, ret;
25727   tree real_decl, initv, condv, incrv, declv;
25728   tree this_pre_body, cl;
25729   location_t loc_first;
25730   bool collapse_err = false;
25731   int i, collapse = 1, nbraces = 0;
25732   VEC(tree,gc) *for_block = make_tree_vector ();
25733
25734   for (cl = clauses; cl; cl = OMP_CLAUSE_CHAIN (cl))
25735     if (OMP_CLAUSE_CODE (cl) == OMP_CLAUSE_COLLAPSE)
25736       collapse = tree_low_cst (OMP_CLAUSE_COLLAPSE_EXPR (cl), 0);
25737
25738   gcc_assert (collapse >= 1);
25739
25740   declv = make_tree_vec (collapse);
25741   initv = make_tree_vec (collapse);
25742   condv = make_tree_vec (collapse);
25743   incrv = make_tree_vec (collapse);
25744
25745   loc_first = cp_lexer_peek_token (parser->lexer)->location;
25746
25747   for (i = 0; i < collapse; i++)
25748     {
25749       int bracecount = 0;
25750       bool add_private_clause = false;
25751       location_t loc;
25752
25753       if (!cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
25754         {
25755           cp_parser_error (parser, "for statement expected");
25756           return NULL;
25757         }
25758       loc = cp_lexer_consume_token (parser->lexer)->location;
25759
25760       if (!cp_parser_require (parser, CPP_OPEN_PAREN, RT_OPEN_PAREN))
25761         return NULL;
25762
25763       init = decl = real_decl = NULL;
25764       this_pre_body = push_stmt_list ();
25765       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
25766         {
25767           /* See 2.5.1 (in OpenMP 3.0, similar wording is in 2.5 standard too):
25768
25769              init-expr:
25770                        var = lb
25771                        integer-type var = lb
25772                        random-access-iterator-type var = lb
25773                        pointer-type var = lb
25774           */
25775           cp_decl_specifier_seq type_specifiers;
25776
25777           /* First, try to parse as an initialized declaration.  See
25778              cp_parser_condition, from whence the bulk of this is copied.  */
25779
25780           cp_parser_parse_tentatively (parser);
25781           cp_parser_type_specifier_seq (parser, /*is_declaration=*/true,
25782                                         /*is_trailing_return=*/false,
25783                                         &type_specifiers);
25784           if (cp_parser_parse_definitely (parser))
25785             {
25786               /* If parsing a type specifier seq succeeded, then this
25787                  MUST be a initialized declaration.  */
25788               tree asm_specification, attributes;
25789               cp_declarator *declarator;
25790
25791               declarator = cp_parser_declarator (parser,
25792                                                  CP_PARSER_DECLARATOR_NAMED,
25793                                                  /*ctor_dtor_or_conv_p=*/NULL,
25794                                                  /*parenthesized_p=*/NULL,
25795                                                  /*member_p=*/false);
25796               attributes = cp_parser_attributes_opt (parser);
25797               asm_specification = cp_parser_asm_specification_opt (parser);
25798
25799               if (declarator == cp_error_declarator) 
25800                 cp_parser_skip_to_end_of_statement (parser);
25801
25802               else 
25803                 {
25804                   tree pushed_scope, auto_node;
25805
25806                   decl = start_decl (declarator, &type_specifiers,
25807                                      SD_INITIALIZED, attributes,
25808                                      /*prefix_attributes=*/NULL_TREE,
25809                                      &pushed_scope);
25810
25811                   auto_node = type_uses_auto (TREE_TYPE (decl));
25812                   if (cp_lexer_next_token_is_not (parser->lexer, CPP_EQ))
25813                     {
25814                       if (cp_lexer_next_token_is (parser->lexer, 
25815                                                   CPP_OPEN_PAREN))
25816                         error ("parenthesized initialization is not allowed in "
25817                                "OpenMP %<for%> loop");
25818                       else
25819                         /* Trigger an error.  */
25820                         cp_parser_require (parser, CPP_EQ, RT_EQ);
25821
25822                       init = error_mark_node;
25823                       cp_parser_skip_to_end_of_statement (parser);
25824                     }
25825                   else if (CLASS_TYPE_P (TREE_TYPE (decl))
25826                            || type_dependent_expression_p (decl)
25827                            || auto_node)
25828                     {
25829                       bool is_direct_init, is_non_constant_init;
25830
25831                       init = cp_parser_initializer (parser,
25832                                                     &is_direct_init,
25833                                                     &is_non_constant_init);
25834
25835                       if (auto_node)
25836                         {
25837                           TREE_TYPE (decl)
25838                             = do_auto_deduction (TREE_TYPE (decl), init,
25839                                                  auto_node);
25840
25841                           if (!CLASS_TYPE_P (TREE_TYPE (decl))
25842                               && !type_dependent_expression_p (decl))
25843                             goto non_class;
25844                         }
25845                       
25846                       cp_finish_decl (decl, init, !is_non_constant_init,
25847                                       asm_specification,
25848                                       LOOKUP_ONLYCONVERTING);
25849                       if (CLASS_TYPE_P (TREE_TYPE (decl)))
25850                         {
25851                           VEC_safe_push (tree, gc, for_block, this_pre_body);
25852                           init = NULL_TREE;
25853                         }
25854                       else
25855                         init = pop_stmt_list (this_pre_body);
25856                       this_pre_body = NULL_TREE;
25857                     }
25858                   else
25859                     {
25860                       /* Consume '='.  */
25861                       cp_lexer_consume_token (parser->lexer);
25862                       init = cp_parser_assignment_expression (parser, false, NULL);
25863
25864                     non_class:
25865                       if (TREE_CODE (TREE_TYPE (decl)) == REFERENCE_TYPE)
25866                         init = error_mark_node;
25867                       else
25868                         cp_finish_decl (decl, NULL_TREE,
25869                                         /*init_const_expr_p=*/false,
25870                                         asm_specification,
25871                                         LOOKUP_ONLYCONVERTING);
25872                     }
25873
25874                   if (pushed_scope)
25875                     pop_scope (pushed_scope);
25876                 }
25877             }
25878           else 
25879             {
25880               cp_id_kind idk;
25881               /* If parsing a type specifier sequence failed, then
25882                  this MUST be a simple expression.  */
25883               cp_parser_parse_tentatively (parser);
25884               decl = cp_parser_primary_expression (parser, false, false,
25885                                                    false, &idk);
25886               if (!cp_parser_error_occurred (parser)
25887                   && decl
25888                   && DECL_P (decl)
25889                   && CLASS_TYPE_P (TREE_TYPE (decl)))
25890                 {
25891                   tree rhs;
25892
25893                   cp_parser_parse_definitely (parser);
25894                   cp_parser_require (parser, CPP_EQ, RT_EQ);
25895                   rhs = cp_parser_assignment_expression (parser, false, NULL);
25896                   finish_expr_stmt (build_x_modify_expr (decl, NOP_EXPR,
25897                                                          rhs,
25898                                                          tf_warning_or_error));
25899                   add_private_clause = true;
25900                 }
25901               else
25902                 {
25903                   decl = NULL;
25904                   cp_parser_abort_tentative_parse (parser);
25905                   init = cp_parser_expression (parser, false, NULL);
25906                   if (init)
25907                     {
25908                       if (TREE_CODE (init) == MODIFY_EXPR
25909                           || TREE_CODE (init) == MODOP_EXPR)
25910                         real_decl = TREE_OPERAND (init, 0);
25911                     }
25912                 }
25913             }
25914         }
25915       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
25916       if (this_pre_body)
25917         {
25918           this_pre_body = pop_stmt_list (this_pre_body);
25919           if (pre_body)
25920             {
25921               tree t = pre_body;
25922               pre_body = push_stmt_list ();
25923               add_stmt (t);
25924               add_stmt (this_pre_body);
25925               pre_body = pop_stmt_list (pre_body);
25926             }
25927           else
25928             pre_body = this_pre_body;
25929         }
25930
25931       if (decl)
25932         real_decl = decl;
25933       if (par_clauses != NULL && real_decl != NULL_TREE)
25934         {
25935           tree *c;
25936           for (c = par_clauses; *c ; )
25937             if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_FIRSTPRIVATE
25938                 && OMP_CLAUSE_DECL (*c) == real_decl)
25939               {
25940                 error_at (loc, "iteration variable %qD"
25941                           " should not be firstprivate", real_decl);
25942                 *c = OMP_CLAUSE_CHAIN (*c);
25943               }
25944             else if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_LASTPRIVATE
25945                      && OMP_CLAUSE_DECL (*c) == real_decl)
25946               {
25947                 /* Add lastprivate (decl) clause to OMP_FOR_CLAUSES,
25948                    change it to shared (decl) in OMP_PARALLEL_CLAUSES.  */
25949                 tree l = build_omp_clause (loc, OMP_CLAUSE_LASTPRIVATE);
25950                 OMP_CLAUSE_DECL (l) = real_decl;
25951                 OMP_CLAUSE_CHAIN (l) = clauses;
25952                 CP_OMP_CLAUSE_INFO (l) = CP_OMP_CLAUSE_INFO (*c);
25953                 clauses = l;
25954                 OMP_CLAUSE_SET_CODE (*c, OMP_CLAUSE_SHARED);
25955                 CP_OMP_CLAUSE_INFO (*c) = NULL;
25956                 add_private_clause = false;
25957               }
25958             else
25959               {
25960                 if (OMP_CLAUSE_CODE (*c) == OMP_CLAUSE_PRIVATE
25961                     && OMP_CLAUSE_DECL (*c) == real_decl)
25962                   add_private_clause = false;
25963                 c = &OMP_CLAUSE_CHAIN (*c);
25964               }
25965         }
25966
25967       if (add_private_clause)
25968         {
25969           tree c;
25970           for (c = clauses; c ; c = OMP_CLAUSE_CHAIN (c))
25971             {
25972               if ((OMP_CLAUSE_CODE (c) == OMP_CLAUSE_PRIVATE
25973                    || OMP_CLAUSE_CODE (c) == OMP_CLAUSE_LASTPRIVATE)
25974                   && OMP_CLAUSE_DECL (c) == decl)
25975                 break;
25976               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_FIRSTPRIVATE
25977                        && OMP_CLAUSE_DECL (c) == decl)
25978                 error_at (loc, "iteration variable %qD "
25979                           "should not be firstprivate",
25980                           decl);
25981               else if (OMP_CLAUSE_CODE (c) == OMP_CLAUSE_REDUCTION
25982                        && OMP_CLAUSE_DECL (c) == decl)
25983                 error_at (loc, "iteration variable %qD should not be reduction",
25984                           decl);
25985             }
25986           if (c == NULL)
25987             {
25988               c = build_omp_clause (loc, OMP_CLAUSE_PRIVATE);
25989               OMP_CLAUSE_DECL (c) = decl;
25990               c = finish_omp_clauses (c);
25991               if (c)
25992                 {
25993                   OMP_CLAUSE_CHAIN (c) = clauses;
25994                   clauses = c;
25995                 }
25996             }
25997         }
25998
25999       cond = NULL;
26000       if (cp_lexer_next_token_is_not (parser->lexer, CPP_SEMICOLON))
26001         cond = cp_parser_omp_for_cond (parser, decl);
26002       cp_parser_require (parser, CPP_SEMICOLON, RT_SEMICOLON);
26003
26004       incr = NULL;
26005       if (cp_lexer_next_token_is_not (parser->lexer, CPP_CLOSE_PAREN))
26006         {
26007           /* If decl is an iterator, preserve the operator on decl
26008              until finish_omp_for.  */
26009           if (decl
26010               && ((type_dependent_expression_p (decl)
26011                    && !POINTER_TYPE_P (TREE_TYPE (decl)))
26012                   || CLASS_TYPE_P (TREE_TYPE (decl))))
26013             incr = cp_parser_omp_for_incr (parser, decl);
26014           else
26015             incr = cp_parser_expression (parser, false, NULL);
26016         }
26017
26018       if (!cp_parser_require (parser, CPP_CLOSE_PAREN, RT_CLOSE_PAREN))
26019         cp_parser_skip_to_closing_parenthesis (parser, /*recovering=*/true,
26020                                                /*or_comma=*/false,
26021                                                /*consume_paren=*/true);
26022
26023       TREE_VEC_ELT (declv, i) = decl;
26024       TREE_VEC_ELT (initv, i) = init;
26025       TREE_VEC_ELT (condv, i) = cond;
26026       TREE_VEC_ELT (incrv, i) = incr;
26027
26028       if (i == collapse - 1)
26029         break;
26030
26031       /* FIXME: OpenMP 3.0 draft isn't very clear on what exactly is allowed
26032          in between the collapsed for loops to be still considered perfectly
26033          nested.  Hopefully the final version clarifies this.
26034          For now handle (multiple) {'s and empty statements.  */
26035       cp_parser_parse_tentatively (parser);
26036       do
26037         {
26038           if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26039             break;
26040           else if (cp_lexer_next_token_is (parser->lexer, CPP_OPEN_BRACE))
26041             {
26042               cp_lexer_consume_token (parser->lexer);
26043               bracecount++;
26044             }
26045           else if (bracecount
26046                    && cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26047             cp_lexer_consume_token (parser->lexer);
26048           else
26049             {
26050               loc = cp_lexer_peek_token (parser->lexer)->location;
26051               error_at (loc, "not enough collapsed for loops");
26052               collapse_err = true;
26053               cp_parser_abort_tentative_parse (parser);
26054               declv = NULL_TREE;
26055               break;
26056             }
26057         }
26058       while (1);
26059
26060       if (declv)
26061         {
26062           cp_parser_parse_definitely (parser);
26063           nbraces += bracecount;
26064         }
26065     }
26066
26067   /* Note that we saved the original contents of this flag when we entered
26068      the structured block, and so we don't need to re-save it here.  */
26069   parser->in_statement = IN_OMP_FOR;
26070
26071   /* Note that the grammar doesn't call for a structured block here,
26072      though the loop as a whole is a structured block.  */
26073   body = push_stmt_list ();
26074   cp_parser_statement (parser, NULL_TREE, false, NULL);
26075   body = pop_stmt_list (body);
26076
26077   if (declv == NULL_TREE)
26078     ret = NULL_TREE;
26079   else
26080     ret = finish_omp_for (loc_first, declv, initv, condv, incrv, body,
26081                           pre_body, clauses);
26082
26083   while (nbraces)
26084     {
26085       if (cp_lexer_next_token_is (parser->lexer, CPP_CLOSE_BRACE))
26086         {
26087           cp_lexer_consume_token (parser->lexer);
26088           nbraces--;
26089         }
26090       else if (cp_lexer_next_token_is (parser->lexer, CPP_SEMICOLON))
26091         cp_lexer_consume_token (parser->lexer);
26092       else
26093         {
26094           if (!collapse_err)
26095             {
26096               error_at (cp_lexer_peek_token (parser->lexer)->location,
26097                         "collapsed loops not perfectly nested");
26098             }
26099           collapse_err = true;
26100           cp_parser_statement_seq_opt (parser, NULL);
26101           if (cp_lexer_next_token_is (parser->lexer, CPP_EOF))
26102             break;
26103         }
26104     }
26105
26106   while (!VEC_empty (tree, for_block))
26107     add_stmt (pop_stmt_list (VEC_pop (tree, for_block)));
26108   release_tree_vector (for_block);
26109
26110   return ret;
26111 }
26112
26113 /* OpenMP 2.5:
26114    #pragma omp for for-clause[optseq] new-line
26115      for-loop  */
26116
26117 #define OMP_FOR_CLAUSE_MASK                             \
26118         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26119         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26120         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26121         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26122         | (1u << PRAGMA_OMP_CLAUSE_ORDERED)             \
26123         | (1u << PRAGMA_OMP_CLAUSE_SCHEDULE)            \
26124         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT)              \
26125         | (1u << PRAGMA_OMP_CLAUSE_COLLAPSE))
26126
26127 static tree
26128 cp_parser_omp_for (cp_parser *parser, cp_token *pragma_tok)
26129 {
26130   tree clauses, sb, ret;
26131   unsigned int save;
26132
26133   clauses = cp_parser_omp_all_clauses (parser, OMP_FOR_CLAUSE_MASK,
26134                                        "#pragma omp for", pragma_tok);
26135
26136   sb = begin_omp_structured_block ();
26137   save = cp_parser_begin_omp_structured_block (parser);
26138
26139   ret = cp_parser_omp_for_loop (parser, clauses, NULL);
26140
26141   cp_parser_end_omp_structured_block (parser, save);
26142   add_stmt (finish_omp_structured_block (sb));
26143
26144   return ret;
26145 }
26146
26147 /* OpenMP 2.5:
26148    # pragma omp master new-line
26149      structured-block  */
26150
26151 static tree
26152 cp_parser_omp_master (cp_parser *parser, cp_token *pragma_tok)
26153 {
26154   cp_parser_require_pragma_eol (parser, pragma_tok);
26155   return c_finish_omp_master (input_location,
26156                               cp_parser_omp_structured_block (parser));
26157 }
26158
26159 /* OpenMP 2.5:
26160    # pragma omp ordered new-line
26161      structured-block  */
26162
26163 static tree
26164 cp_parser_omp_ordered (cp_parser *parser, cp_token *pragma_tok)
26165 {
26166   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26167   cp_parser_require_pragma_eol (parser, pragma_tok);
26168   return c_finish_omp_ordered (loc, cp_parser_omp_structured_block (parser));
26169 }
26170
26171 /* OpenMP 2.5:
26172
26173    section-scope:
26174      { section-sequence }
26175
26176    section-sequence:
26177      section-directive[opt] structured-block
26178      section-sequence section-directive structured-block  */
26179
26180 static tree
26181 cp_parser_omp_sections_scope (cp_parser *parser)
26182 {
26183   tree stmt, substmt;
26184   bool error_suppress = false;
26185   cp_token *tok;
26186
26187   if (!cp_parser_require (parser, CPP_OPEN_BRACE, RT_OPEN_BRACE))
26188     return NULL_TREE;
26189
26190   stmt = push_stmt_list ();
26191
26192   if (cp_lexer_peek_token (parser->lexer)->pragma_kind != PRAGMA_OMP_SECTION)
26193     {
26194       unsigned save;
26195
26196       substmt = begin_omp_structured_block ();
26197       save = cp_parser_begin_omp_structured_block (parser);
26198
26199       while (1)
26200         {
26201           cp_parser_statement (parser, NULL_TREE, false, NULL);
26202
26203           tok = cp_lexer_peek_token (parser->lexer);
26204           if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26205             break;
26206           if (tok->type == CPP_CLOSE_BRACE)
26207             break;
26208           if (tok->type == CPP_EOF)
26209             break;
26210         }
26211
26212       cp_parser_end_omp_structured_block (parser, save);
26213       substmt = finish_omp_structured_block (substmt);
26214       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26215       add_stmt (substmt);
26216     }
26217
26218   while (1)
26219     {
26220       tok = cp_lexer_peek_token (parser->lexer);
26221       if (tok->type == CPP_CLOSE_BRACE)
26222         break;
26223       if (tok->type == CPP_EOF)
26224         break;
26225
26226       if (tok->pragma_kind == PRAGMA_OMP_SECTION)
26227         {
26228           cp_lexer_consume_token (parser->lexer);
26229           cp_parser_require_pragma_eol (parser, tok);
26230           error_suppress = false;
26231         }
26232       else if (!error_suppress)
26233         {
26234           cp_parser_error (parser, "expected %<#pragma omp section%> or %<}%>");
26235           error_suppress = true;
26236         }
26237
26238       substmt = cp_parser_omp_structured_block (parser);
26239       substmt = build1 (OMP_SECTION, void_type_node, substmt);
26240       add_stmt (substmt);
26241     }
26242   cp_parser_require (parser, CPP_CLOSE_BRACE, RT_CLOSE_BRACE);
26243
26244   substmt = pop_stmt_list (stmt);
26245
26246   stmt = make_node (OMP_SECTIONS);
26247   TREE_TYPE (stmt) = void_type_node;
26248   OMP_SECTIONS_BODY (stmt) = substmt;
26249
26250   add_stmt (stmt);
26251   return stmt;
26252 }
26253
26254 /* OpenMP 2.5:
26255    # pragma omp sections sections-clause[optseq] newline
26256      sections-scope  */
26257
26258 #define OMP_SECTIONS_CLAUSE_MASK                        \
26259         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26260         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26261         | (1u << PRAGMA_OMP_CLAUSE_LASTPRIVATE)         \
26262         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26263         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26264
26265 static tree
26266 cp_parser_omp_sections (cp_parser *parser, cp_token *pragma_tok)
26267 {
26268   tree clauses, ret;
26269
26270   clauses = cp_parser_omp_all_clauses (parser, OMP_SECTIONS_CLAUSE_MASK,
26271                                        "#pragma omp sections", pragma_tok);
26272
26273   ret = cp_parser_omp_sections_scope (parser);
26274   if (ret)
26275     OMP_SECTIONS_CLAUSES (ret) = clauses;
26276
26277   return ret;
26278 }
26279
26280 /* OpenMP 2.5:
26281    # pragma parallel parallel-clause new-line
26282    # pragma parallel for parallel-for-clause new-line
26283    # pragma parallel sections parallel-sections-clause new-line  */
26284
26285 #define OMP_PARALLEL_CLAUSE_MASK                        \
26286         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26287         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26288         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26289         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26290         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26291         | (1u << PRAGMA_OMP_CLAUSE_COPYIN)              \
26292         | (1u << PRAGMA_OMP_CLAUSE_REDUCTION)           \
26293         | (1u << PRAGMA_OMP_CLAUSE_NUM_THREADS))
26294
26295 static tree
26296 cp_parser_omp_parallel (cp_parser *parser, cp_token *pragma_tok)
26297 {
26298   enum pragma_kind p_kind = PRAGMA_OMP_PARALLEL;
26299   const char *p_name = "#pragma omp parallel";
26300   tree stmt, clauses, par_clause, ws_clause, block;
26301   unsigned int mask = OMP_PARALLEL_CLAUSE_MASK;
26302   unsigned int save;
26303   location_t loc = cp_lexer_peek_token (parser->lexer)->location;
26304
26305   if (cp_lexer_next_token_is_keyword (parser->lexer, RID_FOR))
26306     {
26307       cp_lexer_consume_token (parser->lexer);
26308       p_kind = PRAGMA_OMP_PARALLEL_FOR;
26309       p_name = "#pragma omp parallel for";
26310       mask |= OMP_FOR_CLAUSE_MASK;
26311       mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26312     }
26313   else if (cp_lexer_next_token_is (parser->lexer, CPP_NAME))
26314     {
26315       tree id = cp_lexer_peek_token (parser->lexer)->u.value;
26316       const char *p = IDENTIFIER_POINTER (id);
26317       if (strcmp (p, "sections") == 0)
26318         {
26319           cp_lexer_consume_token (parser->lexer);
26320           p_kind = PRAGMA_OMP_PARALLEL_SECTIONS;
26321           p_name = "#pragma omp parallel sections";
26322           mask |= OMP_SECTIONS_CLAUSE_MASK;
26323           mask &= ~(1u << PRAGMA_OMP_CLAUSE_NOWAIT);
26324         }
26325     }
26326
26327   clauses = cp_parser_omp_all_clauses (parser, mask, p_name, pragma_tok);
26328   block = begin_omp_parallel ();
26329   save = cp_parser_begin_omp_structured_block (parser);
26330
26331   switch (p_kind)
26332     {
26333     case PRAGMA_OMP_PARALLEL:
26334       cp_parser_statement (parser, NULL_TREE, false, NULL);
26335       par_clause = clauses;
26336       break;
26337
26338     case PRAGMA_OMP_PARALLEL_FOR:
26339       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26340       cp_parser_omp_for_loop (parser, ws_clause, &par_clause);
26341       break;
26342
26343     case PRAGMA_OMP_PARALLEL_SECTIONS:
26344       c_split_parallel_clauses (loc, clauses, &par_clause, &ws_clause);
26345       stmt = cp_parser_omp_sections_scope (parser);
26346       if (stmt)
26347         OMP_SECTIONS_CLAUSES (stmt) = ws_clause;
26348       break;
26349
26350     default:
26351       gcc_unreachable ();
26352     }
26353
26354   cp_parser_end_omp_structured_block (parser, save);
26355   stmt = finish_omp_parallel (par_clause, block);
26356   if (p_kind != PRAGMA_OMP_PARALLEL)
26357     OMP_PARALLEL_COMBINED (stmt) = 1;
26358   return stmt;
26359 }
26360
26361 /* OpenMP 2.5:
26362    # pragma omp single single-clause[optseq] new-line
26363      structured-block  */
26364
26365 #define OMP_SINGLE_CLAUSE_MASK                          \
26366         ( (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26367         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26368         | (1u << PRAGMA_OMP_CLAUSE_COPYPRIVATE)         \
26369         | (1u << PRAGMA_OMP_CLAUSE_NOWAIT))
26370
26371 static tree
26372 cp_parser_omp_single (cp_parser *parser, cp_token *pragma_tok)
26373 {
26374   tree stmt = make_node (OMP_SINGLE);
26375   TREE_TYPE (stmt) = void_type_node;
26376
26377   OMP_SINGLE_CLAUSES (stmt)
26378     = cp_parser_omp_all_clauses (parser, OMP_SINGLE_CLAUSE_MASK,
26379                                  "#pragma omp single", pragma_tok);
26380   OMP_SINGLE_BODY (stmt) = cp_parser_omp_structured_block (parser);
26381
26382   return add_stmt (stmt);
26383 }
26384
26385 /* OpenMP 3.0:
26386    # pragma omp task task-clause[optseq] new-line
26387      structured-block  */
26388
26389 #define OMP_TASK_CLAUSE_MASK                            \
26390         ( (1u << PRAGMA_OMP_CLAUSE_IF)                  \
26391         | (1u << PRAGMA_OMP_CLAUSE_UNTIED)              \
26392         | (1u << PRAGMA_OMP_CLAUSE_DEFAULT)             \
26393         | (1u << PRAGMA_OMP_CLAUSE_PRIVATE)             \
26394         | (1u << PRAGMA_OMP_CLAUSE_FIRSTPRIVATE)        \
26395         | (1u << PRAGMA_OMP_CLAUSE_SHARED)              \
26396         | (1u << PRAGMA_OMP_CLAUSE_FINAL)               \
26397         | (1u << PRAGMA_OMP_CLAUSE_MERGEABLE))
26398
26399 static tree
26400 cp_parser_omp_task (cp_parser *parser, cp_token *pragma_tok)
26401 {
26402   tree clauses, block;
26403   unsigned int save;
26404
26405   clauses = cp_parser_omp_all_clauses (parser, OMP_TASK_CLAUSE_MASK,
26406                                        "#pragma omp task", pragma_tok);
26407   block = begin_omp_task ();
26408   save = cp_parser_begin_omp_structured_block (parser);
26409   cp_parser_statement (parser, NULL_TREE, false, NULL);
26410   cp_parser_end_omp_structured_block (parser, save);
26411   return finish_omp_task (clauses, block);
26412 }
26413
26414 /* OpenMP 3.0:
26415    # pragma omp taskwait new-line  */
26416
26417 static void
26418 cp_parser_omp_taskwait (cp_parser *parser, cp_token *pragma_tok)
26419 {
26420   cp_parser_require_pragma_eol (parser, pragma_tok);
26421   finish_omp_taskwait ();
26422 }
26423
26424 /* OpenMP 3.1:
26425    # pragma omp taskyield new-line  */
26426
26427 static void
26428 cp_parser_omp_taskyield (cp_parser *parser, cp_token *pragma_tok)
26429 {
26430   cp_parser_require_pragma_eol (parser, pragma_tok);
26431   finish_omp_taskyield ();
26432 }
26433
26434 /* OpenMP 2.5:
26435    # pragma omp threadprivate (variable-list) */
26436
26437 static void
26438 cp_parser_omp_threadprivate (cp_parser *parser, cp_token *pragma_tok)
26439 {
26440   tree vars;
26441
26442   vars = cp_parser_omp_var_list (parser, OMP_CLAUSE_ERROR, NULL);
26443   cp_parser_require_pragma_eol (parser, pragma_tok);
26444
26445   finish_omp_threadprivate (vars);
26446 }
26447
26448 /* Main entry point to OpenMP statement pragmas.  */
26449
26450 static void
26451 cp_parser_omp_construct (cp_parser *parser, cp_token *pragma_tok)
26452 {
26453   tree stmt;
26454
26455   switch (pragma_tok->pragma_kind)
26456     {
26457     case PRAGMA_OMP_ATOMIC:
26458       cp_parser_omp_atomic (parser, pragma_tok);
26459       return;
26460     case PRAGMA_OMP_CRITICAL:
26461       stmt = cp_parser_omp_critical (parser, pragma_tok);
26462       break;
26463     case PRAGMA_OMP_FOR:
26464       stmt = cp_parser_omp_for (parser, pragma_tok);
26465       break;
26466     case PRAGMA_OMP_MASTER:
26467       stmt = cp_parser_omp_master (parser, pragma_tok);
26468       break;
26469     case PRAGMA_OMP_ORDERED:
26470       stmt = cp_parser_omp_ordered (parser, pragma_tok);
26471       break;
26472     case PRAGMA_OMP_PARALLEL:
26473       stmt = cp_parser_omp_parallel (parser, pragma_tok);
26474       break;
26475     case PRAGMA_OMP_SECTIONS:
26476       stmt = cp_parser_omp_sections (parser, pragma_tok);
26477       break;
26478     case PRAGMA_OMP_SINGLE:
26479       stmt = cp_parser_omp_single (parser, pragma_tok);
26480       break;
26481     case PRAGMA_OMP_TASK:
26482       stmt = cp_parser_omp_task (parser, pragma_tok);
26483       break;
26484     default:
26485       gcc_unreachable ();
26486     }
26487
26488   if (stmt)
26489     SET_EXPR_LOCATION (stmt, pragma_tok->location);
26490 }
26491 \f
26492 /* The parser.  */
26493
26494 static GTY (()) cp_parser *the_parser;
26495
26496 \f
26497 /* Special handling for the first token or line in the file.  The first
26498    thing in the file might be #pragma GCC pch_preprocess, which loads a
26499    PCH file, which is a GC collection point.  So we need to handle this
26500    first pragma without benefit of an existing lexer structure.
26501
26502    Always returns one token to the caller in *FIRST_TOKEN.  This is
26503    either the true first token of the file, or the first token after
26504    the initial pragma.  */
26505
26506 static void
26507 cp_parser_initial_pragma (cp_token *first_token)
26508 {
26509   tree name = NULL;
26510
26511   cp_lexer_get_preprocessor_token (NULL, first_token);
26512   if (first_token->pragma_kind != PRAGMA_GCC_PCH_PREPROCESS)
26513     return;
26514
26515   cp_lexer_get_preprocessor_token (NULL, first_token);
26516   if (first_token->type == CPP_STRING)
26517     {
26518       name = first_token->u.value;
26519
26520       cp_lexer_get_preprocessor_token (NULL, first_token);
26521       if (first_token->type != CPP_PRAGMA_EOL)
26522         error_at (first_token->location,
26523                   "junk at end of %<#pragma GCC pch_preprocess%>");
26524     }
26525   else
26526     error_at (first_token->location, "expected string literal");
26527
26528   /* Skip to the end of the pragma.  */
26529   while (first_token->type != CPP_PRAGMA_EOL && first_token->type != CPP_EOF)
26530     cp_lexer_get_preprocessor_token (NULL, first_token);
26531
26532   /* Now actually load the PCH file.  */
26533   if (name)
26534     c_common_pch_pragma (parse_in, TREE_STRING_POINTER (name));
26535
26536   /* Read one more token to return to our caller.  We have to do this
26537      after reading the PCH file in, since its pointers have to be
26538      live.  */
26539   cp_lexer_get_preprocessor_token (NULL, first_token);
26540 }
26541
26542 /* Normal parsing of a pragma token.  Here we can (and must) use the
26543    regular lexer.  */
26544
26545 static bool
26546 cp_parser_pragma (cp_parser *parser, enum pragma_context context)
26547 {
26548   cp_token *pragma_tok;
26549   unsigned int id;
26550
26551   pragma_tok = cp_lexer_consume_token (parser->lexer);
26552   gcc_assert (pragma_tok->type == CPP_PRAGMA);
26553   parser->lexer->in_pragma = true;
26554
26555   id = pragma_tok->pragma_kind;
26556   switch (id)
26557     {
26558     case PRAGMA_GCC_PCH_PREPROCESS:
26559       error_at (pragma_tok->location,
26560                 "%<#pragma GCC pch_preprocess%> must be first");
26561       break;
26562
26563     case PRAGMA_OMP_BARRIER:
26564       switch (context)
26565         {
26566         case pragma_compound:
26567           cp_parser_omp_barrier (parser, pragma_tok);
26568           return false;
26569         case pragma_stmt:
26570           error_at (pragma_tok->location, "%<#pragma omp barrier%> may only be "
26571                     "used in compound statements");
26572           break;
26573         default:
26574           goto bad_stmt;
26575         }
26576       break;
26577
26578     case PRAGMA_OMP_FLUSH:
26579       switch (context)
26580         {
26581         case pragma_compound:
26582           cp_parser_omp_flush (parser, pragma_tok);
26583           return false;
26584         case pragma_stmt:
26585           error_at (pragma_tok->location, "%<#pragma omp flush%> may only be "
26586                     "used in compound statements");
26587           break;
26588         default:
26589           goto bad_stmt;
26590         }
26591       break;
26592
26593     case PRAGMA_OMP_TASKWAIT:
26594       switch (context)
26595         {
26596         case pragma_compound:
26597           cp_parser_omp_taskwait (parser, pragma_tok);
26598           return false;
26599         case pragma_stmt:
26600           error_at (pragma_tok->location,
26601                     "%<#pragma omp taskwait%> may only be "
26602                     "used in compound statements");
26603           break;
26604         default:
26605           goto bad_stmt;
26606         }
26607       break;
26608
26609     case PRAGMA_OMP_TASKYIELD:
26610       switch (context)
26611         {
26612         case pragma_compound:
26613           cp_parser_omp_taskyield (parser, pragma_tok);
26614           return false;
26615         case pragma_stmt:
26616           error_at (pragma_tok->location,
26617                     "%<#pragma omp taskyield%> may only be "
26618                     "used in compound statements");
26619           break;
26620         default:
26621           goto bad_stmt;
26622         }
26623       break;
26624
26625     case PRAGMA_OMP_THREADPRIVATE:
26626       cp_parser_omp_threadprivate (parser, pragma_tok);
26627       return false;
26628
26629     case PRAGMA_OMP_ATOMIC:
26630     case PRAGMA_OMP_CRITICAL:
26631     case PRAGMA_OMP_FOR:
26632     case PRAGMA_OMP_MASTER:
26633     case PRAGMA_OMP_ORDERED:
26634     case PRAGMA_OMP_PARALLEL:
26635     case PRAGMA_OMP_SECTIONS:
26636     case PRAGMA_OMP_SINGLE:
26637     case PRAGMA_OMP_TASK:
26638       if (context == pragma_external)
26639         goto bad_stmt;
26640       cp_parser_omp_construct (parser, pragma_tok);
26641       return true;
26642
26643     case PRAGMA_OMP_SECTION:
26644       error_at (pragma_tok->location, 
26645                 "%<#pragma omp section%> may only be used in "
26646                 "%<#pragma omp sections%> construct");
26647       break;
26648
26649     default:
26650       gcc_assert (id >= PRAGMA_FIRST_EXTERNAL);
26651       c_invoke_pragma_handler (id);
26652       break;
26653
26654     bad_stmt:
26655       cp_parser_error (parser, "expected declaration specifiers");
26656       break;
26657     }
26658
26659   cp_parser_skip_to_pragma_eol (parser, pragma_tok);
26660   return false;
26661 }
26662
26663 /* The interface the pragma parsers have to the lexer.  */
26664
26665 enum cpp_ttype
26666 pragma_lex (tree *value)
26667 {
26668   cp_token *tok;
26669   enum cpp_ttype ret;
26670
26671   tok = cp_lexer_peek_token (the_parser->lexer);
26672
26673   ret = tok->type;
26674   *value = tok->u.value;
26675
26676   if (ret == CPP_PRAGMA_EOL || ret == CPP_EOF)
26677     ret = CPP_EOF;
26678   else if (ret == CPP_STRING)
26679     *value = cp_parser_string_literal (the_parser, false, false);
26680   else
26681     {
26682       cp_lexer_consume_token (the_parser->lexer);
26683       if (ret == CPP_KEYWORD)
26684         ret = CPP_NAME;
26685     }
26686
26687   return ret;
26688 }
26689
26690 \f
26691 /* External interface.  */
26692
26693 /* Parse one entire translation unit.  */
26694
26695 void
26696 c_parse_file (void)
26697 {
26698   static bool already_called = false;
26699
26700   if (already_called)
26701     {
26702       sorry ("inter-module optimizations not implemented for C++");
26703       return;
26704     }
26705   already_called = true;
26706
26707   the_parser = cp_parser_new ();
26708   push_deferring_access_checks (flag_access_control
26709                                 ? dk_no_deferred : dk_no_check);
26710   cp_parser_translation_unit (the_parser);
26711   the_parser = NULL;
26712 }
26713
26714 #include "gt-cp-parser.h"